2 **********************************************************************
3 * midi.c - /dev/midi interface for emu10k1 driver
4 * Copyright 1999, 2000 Creative Labs, Inc.
6 **********************************************************************
8 * Date Author Summary of changes
9 * ---- ------ ------------------
10 * October 20, 1999 Bertrand Lee base code release
12 **********************************************************************
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public
25 * License along with this program; if not, write to the Free
26 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
29 **********************************************************************
32 #include <linux/module.h>
33 #include <linux/poll.h>
34 #include <linux/slab.h>
35 #include <linux/sched.h>
36 #include <linux/smp_lock.h>
37 #include <asm/uaccess.h>
44 #ifdef EMU10K1_SEQUENCER
45 #include "../sound_config.h"
48 static DEFINE_SPINLOCK(midi_spinlock
__attribute((unused
)));
50 static void init_midi_hdr(struct midi_hdr
*midihdr
)
52 midihdr
->bufferlength
= MIDIIN_BUFLEN
;
53 midihdr
->bytesrecorded
= 0;
57 static int midiin_add_buffer(struct emu10k1_mididevice
*midi_dev
, struct midi_hdr
**midihdrptr
)
59 struct midi_hdr
*midihdr
;
61 if ((midihdr
= (struct midi_hdr
*) kmalloc(sizeof(struct midi_hdr
), GFP_KERNEL
)) == NULL
) {
66 init_midi_hdr(midihdr
);
68 if ((midihdr
->data
= (u8
*) kmalloc(MIDIIN_BUFLEN
, GFP_KERNEL
)) == NULL
) {
74 if (emu10k1_mpuin_add_buffer(midi_dev
->card
->mpuin
, midihdr
) < 0) {
81 *midihdrptr
= midihdr
;
82 list_add_tail(&midihdr
->list
, &midi_dev
->mid_hdrs
);
87 static int emu10k1_midi_open(struct inode
*inode
, struct file
*file
)
89 int minor
= iminor(inode
);
90 struct emu10k1_card
*card
= NULL
;
91 struct emu10k1_mididevice
*midi_dev
;
92 struct list_head
*entry
;
94 DPF(2, "emu10k1_midi_open()\n");
96 /* Check for correct device to open */
97 list_for_each(entry
, &emu10k1_devs
) {
98 card
= list_entry(entry
, struct emu10k1_card
, list
);
100 if (card
->midi_dev
== minor
)
107 #ifdef EMU10K1_SEQUENCER
108 if (card
->seq_mididev
) /* card is opened by sequencer */
112 /* Wait for device to become free */
113 down(&card
->open_sem
);
114 while (card
->open_mode
& (file
->f_mode
<< FMODE_MIDI_SHIFT
)) {
115 if (file
->f_flags
& O_NONBLOCK
) {
121 interruptible_sleep_on(&card
->open_wait
);
123 if (signal_pending(current
)) {
127 down(&card
->open_sem
);
130 if ((midi_dev
= (struct emu10k1_mididevice
*) kmalloc(sizeof(*midi_dev
), GFP_KERNEL
)) == NULL
)
133 midi_dev
->card
= card
;
134 midi_dev
->mistate
= MIDIIN_STATE_STOPPED
;
135 init_waitqueue_head(&midi_dev
->oWait
);
136 init_waitqueue_head(&midi_dev
->iWait
);
140 INIT_LIST_HEAD(&midi_dev
->mid_hdrs
);
142 if (file
->f_mode
& FMODE_READ
) {
143 struct midi_openinfo dsCardMidiOpenInfo
;
144 struct midi_hdr
*midihdr1
;
145 struct midi_hdr
*midihdr2
;
147 dsCardMidiOpenInfo
.refdata
= (unsigned long) midi_dev
;
149 if (emu10k1_mpuin_open(card
, &dsCardMidiOpenInfo
) < 0) {
155 /* Add two buffers to receive sysex buffer */
156 if (midiin_add_buffer(midi_dev
, &midihdr1
) < 0) {
161 if (midiin_add_buffer(midi_dev
, &midihdr2
) < 0) {
162 list_del(&midihdr1
->list
);
163 kfree(midihdr1
->data
);
170 if (file
->f_mode
& FMODE_WRITE
) {
171 struct midi_openinfo dsCardMidiOpenInfo
;
173 dsCardMidiOpenInfo
.refdata
= (unsigned long) midi_dev
;
175 if (emu10k1_mpuout_open(card
, &dsCardMidiOpenInfo
) < 0) {
182 file
->private_data
= (void *) midi_dev
;
184 card
->open_mode
|= (file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
);
188 return nonseekable_open(inode
, file
);
191 static int emu10k1_midi_release(struct inode
*inode
, struct file
*file
)
193 struct emu10k1_mididevice
*midi_dev
= (struct emu10k1_mididevice
*) file
->private_data
;
194 struct emu10k1_card
*card
;
198 card
= midi_dev
->card
;
199 DPF(2, "emu10k1_midi_release()\n");
201 if (file
->f_mode
& FMODE_WRITE
) {
202 if (!(file
->f_flags
& O_NONBLOCK
)) {
204 while (!signal_pending(current
) && (card
->mpuout
->firstmidiq
!= NULL
)) {
205 DPF(4, "Cannot close - buffers not empty\n");
207 interruptible_sleep_on(&midi_dev
->oWait
);
212 emu10k1_mpuout_close(card
);
215 if (file
->f_mode
& FMODE_READ
) {
216 struct midi_hdr
*midihdr
;
218 if (midi_dev
->mistate
== MIDIIN_STATE_STARTED
) {
219 emu10k1_mpuin_stop(card
);
220 midi_dev
->mistate
= MIDIIN_STATE_STOPPED
;
223 emu10k1_mpuin_reset(card
);
224 emu10k1_mpuin_close(card
);
226 while (!list_empty(&midi_dev
->mid_hdrs
)) {
227 midihdr
= list_entry(midi_dev
->mid_hdrs
.next
, struct midi_hdr
, list
);
229 list_del(midi_dev
->mid_hdrs
.next
);
230 kfree(midihdr
->data
);
237 down(&card
->open_sem
);
238 card
->open_mode
&= ~((file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
));
240 wake_up_interruptible(&card
->open_wait
);
247 static ssize_t
emu10k1_midi_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
* pos
)
249 struct emu10k1_mididevice
*midi_dev
= (struct emu10k1_mididevice
*) file
->private_data
;
254 DPD(4, "emu10k1_midi_read(), count %#x\n", (u32
) count
);
256 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
259 if (midi_dev
->mistate
== MIDIIN_STATE_STOPPED
) {
260 if (emu10k1_mpuin_start(midi_dev
->card
) < 0) {
265 midi_dev
->mistate
= MIDIIN_STATE_STARTED
;
269 cnt
= MIDIIN_BUFLEN
- midi_dev
->ird
;
271 spin_lock_irqsave(&midi_spinlock
, flags
);
273 if (midi_dev
->icnt
< cnt
)
274 cnt
= midi_dev
->icnt
;
276 spin_unlock_irqrestore(&midi_spinlock
, flags
);
282 if (file
->f_flags
& O_NONBLOCK
)
283 return ret
? ret
: -EAGAIN
;
284 DPF(2, " Go to sleep...\n");
286 interruptible_sleep_on(&midi_dev
->iWait
);
288 if (signal_pending(current
))
289 return ret
? ret
: -ERESTARTSYS
;
294 if (copy_to_user(buffer
, midi_dev
->iBuf
+ midi_dev
->ird
, cnt
)) {
296 return ret
? ret
: -EFAULT
;
299 midi_dev
->ird
+= cnt
;
300 midi_dev
->ird
%= MIDIIN_BUFLEN
;
302 spin_lock_irqsave(&midi_spinlock
, flags
);
304 midi_dev
->icnt
-= cnt
;
306 spin_unlock_irqrestore(&midi_spinlock
, flags
);
312 if (midi_dev
->icnt
== 0)
319 static ssize_t
emu10k1_midi_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
* pos
)
321 struct emu10k1_mididevice
*midi_dev
= (struct emu10k1_mididevice
*) file
->private_data
;
322 struct midi_hdr
*midihdr
;
325 DPD(4, "emu10k1_midi_write(), count=%#x\n", (u32
) count
);
327 if (!access_ok(VERIFY_READ
, buffer
, count
))
330 if ((midihdr
= (struct midi_hdr
*) kmalloc(sizeof(struct midi_hdr
), GFP_KERNEL
)) == NULL
)
333 midihdr
->bufferlength
= count
;
334 midihdr
->bytesrecorded
= 0;
337 if ((midihdr
->data
= (u8
*) kmalloc(count
, GFP_KERNEL
)) == NULL
) {
343 if (copy_from_user(midihdr
->data
, buffer
, count
)) {
344 kfree(midihdr
->data
);
349 spin_lock_irqsave(&midi_spinlock
, flags
);
351 if (emu10k1_mpuout_add_buffer(midi_dev
->card
, midihdr
) < 0) {
353 kfree(midihdr
->data
);
355 spin_unlock_irqrestore(&midi_spinlock
, flags
);
359 spin_unlock_irqrestore(&midi_spinlock
, flags
);
364 static unsigned int emu10k1_midi_poll(struct file
*file
, struct poll_table_struct
*wait
)
366 struct emu10k1_mididevice
*midi_dev
= (struct emu10k1_mididevice
*) file
->private_data
;
368 unsigned int mask
= 0;
370 DPF(4, "emu10k1_midi_poll() called\n");
372 if (file
->f_mode
& FMODE_WRITE
)
373 poll_wait(file
, &midi_dev
->oWait
, wait
);
375 if (file
->f_mode
& FMODE_READ
)
376 poll_wait(file
, &midi_dev
->iWait
, wait
);
378 spin_lock_irqsave(&midi_spinlock
, flags
);
380 if (file
->f_mode
& FMODE_WRITE
)
381 mask
|= POLLOUT
| POLLWRNORM
;
383 if (file
->f_mode
& FMODE_READ
) {
384 if (midi_dev
->mistate
== MIDIIN_STATE_STARTED
)
385 if (midi_dev
->icnt
> 0)
386 mask
|= POLLIN
| POLLRDNORM
;
389 spin_unlock_irqrestore(&midi_spinlock
, flags
);
394 int emu10k1_midi_callback(unsigned long msg
, unsigned long refdata
, unsigned long *pmsg
)
396 struct emu10k1_mididevice
*midi_dev
= (struct emu10k1_mididevice
*) refdata
;
397 struct midi_hdr
*midihdr
= NULL
;
401 DPF(4, "emu10k1_midi_callback()\n");
403 spin_lock_irqsave(&midi_spinlock
, flags
);
406 case ICARDMIDI_OUTLONGDATA
:
407 midihdr
= (struct midi_hdr
*) pmsg
[2];
409 kfree(midihdr
->data
);
411 wake_up_interruptible(&midi_dev
->oWait
);
415 case ICARDMIDI_INLONGDATA
:
416 midihdr
= (struct midi_hdr
*) pmsg
[2];
418 for (i
= 0; i
< midihdr
->bytesrecorded
; i
++) {
419 midi_dev
->iBuf
[midi_dev
->iwr
++] = midihdr
->data
[i
];
420 midi_dev
->iwr
%= MIDIIN_BUFLEN
;
423 midi_dev
->icnt
+= midihdr
->bytesrecorded
;
425 if (midi_dev
->mistate
== MIDIIN_STATE_STARTED
) {
426 init_midi_hdr(midihdr
);
427 emu10k1_mpuin_add_buffer(midi_dev
->card
->mpuin
, midihdr
);
428 wake_up_interruptible(&midi_dev
->iWait
);
432 case ICARDMIDI_INDATA
:
434 u8
*pBuf
= (u8
*) & pmsg
[1];
435 u16 bytesvalid
= pmsg
[2];
437 for (i
= 0; i
< bytesvalid
; i
++) {
438 midi_dev
->iBuf
[midi_dev
->iwr
++] = pBuf
[i
];
439 midi_dev
->iwr
%= MIDIIN_BUFLEN
;
442 midi_dev
->icnt
+= bytesvalid
;
445 wake_up_interruptible(&midi_dev
->iWait
);
448 default: /* Unknown message */
449 spin_unlock_irqrestore(&midi_spinlock
, flags
);
453 spin_unlock_irqrestore(&midi_spinlock
, flags
);
458 /* MIDI file operations */
459 struct file_operations emu10k1_midi_fops
= {
460 .owner
= THIS_MODULE
,
461 .read
= emu10k1_midi_read
,
462 .write
= emu10k1_midi_write
,
463 .poll
= emu10k1_midi_poll
,
464 .open
= emu10k1_midi_open
,
465 .release
= emu10k1_midi_release
,
469 #ifdef EMU10K1_SEQUENCER
471 /* functions used for sequencer access */
473 int emu10k1_seq_midi_open(int dev
, int mode
,
474 void (*input
) (int dev
, unsigned char data
),
475 void (*output
) (int dev
))
477 struct emu10k1_card
*card
;
478 struct midi_openinfo dsCardMidiOpenInfo
;
479 struct emu10k1_mididevice
*midi_dev
;
481 if (midi_devs
[dev
] == NULL
|| midi_devs
[dev
]->devc
== NULL
)
484 card
= midi_devs
[dev
]->devc
;
486 if (card
->open_mode
) /* card is opened native */
489 DPF(2, "emu10k1_seq_midi_open()\n");
491 if ((midi_dev
= (struct emu10k1_mididevice
*) kmalloc(sizeof(*midi_dev
), GFP_KERNEL
)) == NULL
)
494 midi_dev
->card
= card
;
495 midi_dev
->mistate
= MIDIIN_STATE_STOPPED
;
496 init_waitqueue_head(&midi_dev
->oWait
);
497 init_waitqueue_head(&midi_dev
->iWait
);
501 INIT_LIST_HEAD(&midi_dev
->mid_hdrs
);
503 dsCardMidiOpenInfo
.refdata
= (unsigned long) midi_dev
;
505 if (emu10k1_mpuout_open(card
, &dsCardMidiOpenInfo
) < 0) {
510 card
->seq_mididev
= midi_dev
;
515 void emu10k1_seq_midi_close(int dev
)
517 struct emu10k1_card
*card
;
519 DPF(2, "emu10k1_seq_midi_close()\n");
520 if (midi_devs
[dev
] == NULL
|| midi_devs
[dev
]->devc
== NULL
)
523 card
= midi_devs
[dev
]->devc
;
524 emu10k1_mpuout_close(card
);
526 if (card
->seq_mididev
) {
527 kfree(card
->seq_mididev
);
528 card
->seq_mididev
= NULL
;
532 int emu10k1_seq_midi_out(int dev
, unsigned char midi_byte
)
534 struct emu10k1_card
*card
;
535 struct midi_hdr
*midihdr
;
538 if (midi_devs
[dev
] == NULL
|| midi_devs
[dev
]->devc
== NULL
)
541 card
= midi_devs
[dev
]->devc
;
543 if ((midihdr
= (struct midi_hdr
*) kmalloc(sizeof(struct midi_hdr
), GFP_KERNEL
)) == NULL
)
546 midihdr
->bufferlength
= 1;
547 midihdr
->bytesrecorded
= 0;
550 if ((midihdr
->data
= (u8
*) kmalloc(1, GFP_KERNEL
)) == NULL
) {
556 *(midihdr
->data
) = midi_byte
;
558 spin_lock_irqsave(&midi_spinlock
, flags
);
560 if (emu10k1_mpuout_add_buffer(card
, midihdr
) < 0) {
562 kfree(midihdr
->data
);
564 spin_unlock_irqrestore(&midi_spinlock
, flags
);
568 spin_unlock_irqrestore(&midi_spinlock
, flags
);
573 int emu10k1_seq_midi_start_read(int dev
)
578 int emu10k1_seq_midi_end_read(int dev
)
583 void emu10k1_seq_midi_kick(int dev
)
587 int emu10k1_seq_midi_buffer_status(int dev
)
590 struct midi_queue
*queue
;
591 struct emu10k1_card
*card
;
593 if (midi_devs
[dev
] == NULL
|| midi_devs
[dev
]->devc
== NULL
)
598 card
= midi_devs
[dev
]->devc
;
599 queue
= card
->mpuout
->firstmidiq
;
601 while (queue
!= NULL
) {
603 if (queue
== card
->mpuout
->lastmidiq
)