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);
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 midihdr->data = kmalloc(MIDIIN_BUFLEN, GFP_KERNEL);
75 if (emu10k1_mpuin_add_buffer(midi_dev->card->mpuin, midihdr) < 0) {
82 *midihdrptr = midihdr;
83 list_add_tail(&midihdr->list, &midi_dev->mid_hdrs);
88 static int emu10k1_midi_open(struct inode *inode, struct file *file)
90 int minor = iminor(inode);
91 struct emu10k1_card *card = NULL;
92 struct emu10k1_mididevice *midi_dev;
93 struct list_head *entry;
95 DPF(2, "emu10k1_midi_open()\n");
97 /* Check for correct device to open */
98 list_for_each(entry, &emu10k1_devs) {
99 card = list_entry(entry, struct emu10k1_card, list);
101 if (card->midi_dev == minor)
108 #ifdef EMU10K1_SEQUENCER
109 if (card->seq_mididev) /* card is opened by sequencer */
113 /* Wait for device to become free */
114 mutex_lock(&card->open_sem);
115 while (card->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
116 if (file->f_flags & O_NONBLOCK) {
117 mutex_unlock(&card->open_sem);
121 mutex_unlock(&card->open_sem);
122 interruptible_sleep_on(&card->open_wait);
124 if (signal_pending(current)) {
128 mutex_lock(&card->open_sem);
131 if ((midi_dev = (struct emu10k1_mididevice *) kmalloc(sizeof(*midi_dev), GFP_KERNEL)) == NULL)
134 midi_dev->card = card;
135 midi_dev->mistate = MIDIIN_STATE_STOPPED;
136 init_waitqueue_head(&midi_dev->oWait);
137 init_waitqueue_head(&midi_dev->iWait);
141 INIT_LIST_HEAD(&midi_dev->mid_hdrs);
143 if (file->f_mode & FMODE_READ) {
144 struct midi_openinfo dsCardMidiOpenInfo;
145 struct midi_hdr *midihdr1;
146 struct midi_hdr *midihdr2;
148 dsCardMidiOpenInfo.refdata = (unsigned long) midi_dev;
150 if (emu10k1_mpuin_open(card, &dsCardMidiOpenInfo) < 0) {
156 /* Add two buffers to receive sysex buffer */
157 if (midiin_add_buffer(midi_dev, &midihdr1) < 0) {
162 if (midiin_add_buffer(midi_dev, &midihdr2) < 0) {
163 list_del(&midihdr1->list);
164 kfree(midihdr1->data);
171 if (file->f_mode & FMODE_WRITE) {
172 struct midi_openinfo dsCardMidiOpenInfo;
174 dsCardMidiOpenInfo.refdata = (unsigned long) midi_dev;
176 if (emu10k1_mpuout_open(card, &dsCardMidiOpenInfo) < 0) {
183 file->private_data = (void *) midi_dev;
185 card->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
187 mutex_unlock(&card->open_sem);
189 return nonseekable_open(inode, file);
192 static int emu10k1_midi_release(struct inode *inode, struct file *file)
194 struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
195 struct emu10k1_card *card;
199 card = midi_dev->card;
200 DPF(2, "emu10k1_midi_release()\n");
202 if (file->f_mode & FMODE_WRITE) {
203 if (!(file->f_flags & O_NONBLOCK)) {
205 while (!signal_pending(current) && (card->mpuout->firstmidiq != NULL)) {
206 DPF(4, "Cannot close - buffers not empty\n");
208 interruptible_sleep_on(&midi_dev->oWait);
213 emu10k1_mpuout_close(card);
216 if (file->f_mode & FMODE_READ) {
217 struct midi_hdr *midihdr;
219 if (midi_dev->mistate == MIDIIN_STATE_STARTED) {
220 emu10k1_mpuin_stop(card);
221 midi_dev->mistate = MIDIIN_STATE_STOPPED;
224 emu10k1_mpuin_reset(card);
225 emu10k1_mpuin_close(card);
227 while (!list_empty(&midi_dev->mid_hdrs)) {
228 midihdr = list_entry(midi_dev->mid_hdrs.next, struct midi_hdr, list);
230 list_del(midi_dev->mid_hdrs.next);
231 kfree(midihdr->data);
238 mutex_lock(&card->open_sem);
239 card->open_mode &= ~((file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE));
240 mutex_unlock(&card->open_sem);
241 wake_up_interruptible(&card->open_wait);
248 static ssize_t emu10k1_midi_read(struct file *file, char __user *buffer, size_t count, loff_t * pos)
250 struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
255 DPD(4, "emu10k1_midi_read(), count %#x\n", (u32) count);
257 if (!access_ok(VERIFY_WRITE, buffer, count))
260 if (midi_dev->mistate == MIDIIN_STATE_STOPPED) {
261 if (emu10k1_mpuin_start(midi_dev->card) < 0) {
266 midi_dev->mistate = MIDIIN_STATE_STARTED;
270 cnt = MIDIIN_BUFLEN - midi_dev->ird;
272 spin_lock_irqsave(&midi_spinlock, flags);
274 if (midi_dev->icnt < cnt)
275 cnt = midi_dev->icnt;
277 spin_unlock_irqrestore(&midi_spinlock, flags);
283 if (file->f_flags & O_NONBLOCK)
284 return ret ? ret : -EAGAIN;
285 DPF(2, " Go to sleep...\n");
287 interruptible_sleep_on(&midi_dev->iWait);
289 if (signal_pending(current))
290 return ret ? ret : -ERESTARTSYS;
295 if (copy_to_user(buffer, midi_dev->iBuf + midi_dev->ird, cnt)) {
297 return ret ? ret : -EFAULT;
300 midi_dev->ird += cnt;
301 midi_dev->ird %= MIDIIN_BUFLEN;
303 spin_lock_irqsave(&midi_spinlock, flags);
305 midi_dev->icnt -= cnt;
307 spin_unlock_irqrestore(&midi_spinlock, flags);
313 if (midi_dev->icnt == 0)
320 static ssize_t emu10k1_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t * pos)
322 struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
323 struct midi_hdr *midihdr;
326 DPD(4, "emu10k1_midi_write(), count=%#x\n", (u32) count);
328 if (!access_ok(VERIFY_READ, buffer, count))
331 if ((midihdr = (struct midi_hdr *) kmalloc(sizeof(struct midi_hdr), GFP_KERNEL)) == NULL)
334 midihdr->bufferlength = count;
335 midihdr->bytesrecorded = 0;
338 midihdr->data = kmalloc(count, GFP_KERNEL);
339 if (!midihdr->data) {
345 if (copy_from_user(midihdr->data, buffer, count)) {
346 kfree(midihdr->data);
351 spin_lock_irqsave(&midi_spinlock, flags);
353 if (emu10k1_mpuout_add_buffer(midi_dev->card, midihdr) < 0) {
355 kfree(midihdr->data);
357 spin_unlock_irqrestore(&midi_spinlock, flags);
361 spin_unlock_irqrestore(&midi_spinlock, flags);
366 static unsigned int emu10k1_midi_poll(struct file *file, struct poll_table_struct *wait)
368 struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
370 unsigned int mask = 0;
372 DPF(4, "emu10k1_midi_poll() called\n");
374 if (file->f_mode & FMODE_WRITE)
375 poll_wait(file, &midi_dev->oWait, wait);
377 if (file->f_mode & FMODE_READ)
378 poll_wait(file, &midi_dev->iWait, wait);
380 spin_lock_irqsave(&midi_spinlock, flags);
382 if (file->f_mode & FMODE_WRITE)
383 mask |= POLLOUT | POLLWRNORM;
385 if (file->f_mode & FMODE_READ) {
386 if (midi_dev->mistate == MIDIIN_STATE_STARTED)
387 if (midi_dev->icnt > 0)
388 mask |= POLLIN | POLLRDNORM;
391 spin_unlock_irqrestore(&midi_spinlock, flags);
396 int emu10k1_midi_callback(unsigned long msg, unsigned long refdata, unsigned long *pmsg)
398 struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) refdata;
399 struct midi_hdr *midihdr = NULL;
403 DPF(4, "emu10k1_midi_callback()\n");
405 spin_lock_irqsave(&midi_spinlock, flags);
408 case ICARDMIDI_OUTLONGDATA:
409 midihdr = (struct midi_hdr *) pmsg[2];
411 kfree(midihdr->data);
413 wake_up_interruptible(&midi_dev->oWait);
417 case ICARDMIDI_INLONGDATA:
418 midihdr = (struct midi_hdr *) pmsg[2];
420 for (i = 0; i < midihdr->bytesrecorded; i++) {
421 midi_dev->iBuf[midi_dev->iwr++] = midihdr->data[i];
422 midi_dev->iwr %= MIDIIN_BUFLEN;
425 midi_dev->icnt += midihdr->bytesrecorded;
427 if (midi_dev->mistate == MIDIIN_STATE_STARTED) {
428 init_midi_hdr(midihdr);
429 emu10k1_mpuin_add_buffer(midi_dev->card->mpuin, midihdr);
430 wake_up_interruptible(&midi_dev->iWait);
434 case ICARDMIDI_INDATA:
436 u8 *pBuf = (u8 *) & pmsg[1];
437 u16 bytesvalid = pmsg[2];
439 for (i = 0; i < bytesvalid; i++) {
440 midi_dev->iBuf[midi_dev->iwr++] = pBuf[i];
441 midi_dev->iwr %= MIDIIN_BUFLEN;
444 midi_dev->icnt += bytesvalid;
447 wake_up_interruptible(&midi_dev->iWait);
450 default: /* Unknown message */
451 spin_unlock_irqrestore(&midi_spinlock, flags);
455 spin_unlock_irqrestore(&midi_spinlock, flags);
460 /* MIDI file operations */
461 struct file_operations emu10k1_midi_fops = {
462 .owner = THIS_MODULE,
463 .read = emu10k1_midi_read,
464 .write = emu10k1_midi_write,
465 .poll = emu10k1_midi_poll,
466 .open = emu10k1_midi_open,
467 .release = emu10k1_midi_release,
471 #ifdef EMU10K1_SEQUENCER
473 /* functions used for sequencer access */
475 int emu10k1_seq_midi_open(int dev, int mode,
476 void (*input) (int dev, unsigned char data),
477 void (*output) (int dev))
479 struct emu10k1_card *card;
480 struct midi_openinfo dsCardMidiOpenInfo;
481 struct emu10k1_mididevice *midi_dev;
483 if (midi_devs[dev] == NULL || midi_devs[dev]->devc == NULL)
486 card = midi_devs[dev]->devc;
488 if (card->open_mode) /* card is opened native */
491 DPF(2, "emu10k1_seq_midi_open()\n");
493 if ((midi_dev = (struct emu10k1_mididevice *) kmalloc(sizeof(*midi_dev), GFP_KERNEL)) == NULL)
496 midi_dev->card = card;
497 midi_dev->mistate = MIDIIN_STATE_STOPPED;
498 init_waitqueue_head(&midi_dev->oWait);
499 init_waitqueue_head(&midi_dev->iWait);
503 INIT_LIST_HEAD(&midi_dev->mid_hdrs);
505 dsCardMidiOpenInfo.refdata = (unsigned long) midi_dev;
507 if (emu10k1_mpuout_open(card, &dsCardMidiOpenInfo) < 0) {
512 card->seq_mididev = midi_dev;
517 void emu10k1_seq_midi_close(int dev)
519 struct emu10k1_card *card;
521 DPF(2, "emu10k1_seq_midi_close()\n");
522 if (midi_devs[dev] == NULL || midi_devs[dev]->devc == NULL)
525 card = midi_devs[dev]->devc;
526 emu10k1_mpuout_close(card);
528 kfree(card->seq_mididev);
529 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 midihdr->data = kmalloc(1, GFP_KERNEL);
551 if (!midihdr->data) {
557 *(midihdr->data) = midi_byte;
559 spin_lock_irqsave(&midi_spinlock, flags);
561 if (emu10k1_mpuout_add_buffer(card, midihdr) < 0) {
563 kfree(midihdr->data);
565 spin_unlock_irqrestore(&midi_spinlock, flags);
569 spin_unlock_irqrestore(&midi_spinlock, flags);
574 int emu10k1_seq_midi_start_read(int dev)
579 int emu10k1_seq_midi_end_read(int dev)
584 void emu10k1_seq_midi_kick(int dev)
588 int emu10k1_seq_midi_buffer_status(int dev)
591 struct midi_queue *queue;
592 struct emu10k1_card *card;
594 if (midi_devs[dev] == NULL || midi_devs[dev]->devc == NULL)
599 card = midi_devs[dev]->devc;
600 queue = card->mpuout->firstmidiq;
602 while (queue != NULL) {
604 if (queue == card->mpuout->lastmidiq)