2 usb-midi.c -- USB-MIDI driver
5 NAGANO Daisuke <breeze.nagano@nifty.ne.jp>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 This driver is based on:
22 - 'Universal Serial Bus Device Class Definition for MIDI Device'
23 - linux/drivers/sound/es1371.c, linux/drivers/usb/audio.c
24 - alsa/lowlevel/pci/cs64xx.c
28 /* ------------------------------------------------------------------------- */
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/list.h>
35 #include <linux/slab.h>
36 #include <linux/usb.h>
37 #include <linux/poll.h>
38 #include <linux/sound.h>
39 #include <linux/init.h>
40 #include <asm/semaphore.h>
44 /* ------------------------------------------------------------------------- */
46 /* More verbose on syslog */
49 #define MIDI_IN_BUFSIZ 1024
51 #define HAVE_SUPPORT_USB_MIDI_CLASS
53 #undef HAVE_SUPPORT_ALSA
55 /* ------------------------------------------------------------------------- */
57 static int singlebyte = 0;
58 module_param(singlebyte, int, 0);
59 MODULE_PARM_DESC(singlebyte,"Enable sending MIDI messages with single message packet");
61 static int maxdevices = 4;
62 module_param(maxdevices, int, 0);
63 MODULE_PARM_DESC(maxdevices,"Max number of allocatable MIDI device");
65 static int uvendor = -1;
66 module_param(uvendor, int, 0);
67 MODULE_PARM_DESC(uvendor, "The USB Vendor ID of a semi-compliant interface");
69 static int uproduct = -1;
70 module_param(uproduct, int, 0);
71 MODULE_PARM_DESC(uproduct, "The USB Product ID of a semi-compliant interface");
73 static int uinterface = -1;
74 module_param(uinterface, int, 0);
75 MODULE_PARM_DESC(uinterface, "The Interface number of a semi-compliant interface");
78 module_param(ualt, int, 0);
79 MODULE_PARM_DESC(ualt, "The optional alternative setting of a semi-compliant interface");
82 module_param(umin, int, 0);
83 MODULE_PARM_DESC(umin, "The input endpoint of a semi-compliant interface");
85 static int umout = -1;
86 module_param(umout, int, 0);
87 MODULE_PARM_DESC(umout, "The output endpoint of a semi-compliant interface");
89 static int ucable = -1;
90 module_param(ucable, int, 0);
91 MODULE_PARM_DESC(ucable, "The cable number used for a semi-compliant interface");
93 /** Note -- the usb_string() returns only Latin-1 characters.
94 * (unicode chars <= 255). To support Japanese, a unicode16LE-to-EUC or
95 * unicode16LE-to-JIS routine is needed to wrap around usb_get_string().
97 static unsigned short ulangid = 0x0409; /** 0x0411 for Japanese **/
98 module_param(ulangid, ushort, 0);
99 MODULE_PARM_DESC(ulangid, "The optional preferred USB Language ID for all devices");
101 MODULE_AUTHOR("NAGANO Daisuke <breeze.nagano@nifty.ne.jp>");
102 MODULE_DESCRIPTION("USB-MIDI driver");
103 MODULE_LICENSE("GPL");
105 /* ------------------------------------------------------------------------- */
107 /** MIDIStreaming Class-Specific Interface Descriptor Subtypes **/
109 #define MS_DESCRIPTOR_UNDEFINED 0
111 #define MIDI_IN_JACK 2
112 #define MIDI_OUT_JACK 3
113 /* Spec reads: ELEMENT */
114 #define ELEMENT_DESCRIPTOR 4
116 #define MS_HEADER_LENGTH 7
118 /** MIDIStreaming Class-Specific Endpoint Descriptor Subtypes **/
120 #define DESCRIPTOR_UNDEFINED 0
121 /* Spec reads: MS_GENERAL */
122 #define MS_GENERAL_ENDPOINT 1
124 /** MIDIStreaming MIDI IN and OUT Jack Types **/
126 #define JACK_TYPE_UNDEFINED 0
127 /* Spec reads: EMBEDDED */
128 #define EMBEDDED_JACK 1
129 /* Spec reads: EXTERNAL */
130 #define EXTERNAL_JACK 2
135 usb_midi_state usb_device
142 | | (cable to device pairing magic)
144 usb_midi_dev dev_id (major,minor) == file->private_data
148 /* usb_midi_state: corresponds to a USB-MIDI module */
149 struct usb_midi_state {
150 struct list_head mididev;
152 struct usb_device *usbdev;
154 struct list_head midiDevList;
155 struct list_head inEndpointList;
156 struct list_head outEndpointList;
160 unsigned int count; /* usage counter */
163 /* midi_out_endpoint: corresponds to an output endpoint */
164 struct midi_out_endpoint {
165 struct list_head list;
167 struct usb_device *usbdev;
170 wait_queue_head_t wait;
179 /* midi_in_endpoint: corresponds to an input endpoint */
180 struct midi_in_endpoint {
181 struct list_head list;
183 struct usb_device *usbdev;
186 wait_queue_head_t wait;
188 struct usb_mididev *cables[16]; // cables open for read
189 int readers; // number of cables open for read
192 unsigned char *recvBuf;
194 int urbSubmitted; //FIXME: == readers > 0
197 /* usb_mididev: corresponds to a logical device */
199 struct list_head list;
201 struct usb_midi_state *midi;
206 struct midi_in_endpoint *ep;
209 // as we are pushing data from usb_bulk_read to usb_midi_read,
210 // we need a larger, cyclic buffer here.
211 unsigned char buf[MIDI_IN_BUFSIZ];
218 struct midi_out_endpoint *ep;
221 unsigned char buf[3];
226 unsigned char lastEvent;
232 /** Map the high nybble of MIDI voice messages to number of Message bytes.
233 * High nyble ranges from 0x8 to 0xe
236 static int remains_80e0[] = {
237 3, /** 0x8X Note Off **/
238 3, /** 0x9X Note On **/
239 3, /** 0xAX Poly-key pressure **/
240 3, /** 0xBX Control Change **/
241 2, /** 0xCX Program Change **/
242 2, /** 0xDX Channel pressure **/
243 3 /** 0xEX PitchBend Change **/
246 /** Map the messages to a number of Message bytes.
249 static int remains_f0f6[] = {
254 2, /** 0XF4 (Undefined by MIDI Spec, and subject to change) **/
255 2, /** 0XF5 (Undefined by MIDI Spec, and subject to change) **/
259 /** Map the messages to a CIN (Code Index Number).
262 static int cin_f0ff[] = {
263 4, /** 0xF0 System Exclusive Message Start (special cases may be 6 or 7) */
270 5, /** 0xF7 End of System Exclusive Message (May be 6 or 7) **/
281 /** Map MIDIStreaming Event packet Code Index Number (low nybble of byte 0)
282 * to the number of bytes of valid MIDI data.
284 * CIN of 0 and 1 are NOT USED in MIDIStreaming 1.0.
287 static int cin_to_len[] = {
295 /* ------------------------------------------------------------------------- */
297 static struct list_head mididevs = LIST_HEAD_INIT(mididevs);
299 static DECLARE_MUTEX(open_sem);
300 static DECLARE_WAIT_QUEUE_HEAD(open_wait);
303 /* ------------------------------------------------------------------------- */
305 static void usb_write_callback(struct urb *urb, struct pt_regs *regs)
307 struct midi_out_endpoint *ep = (struct midi_out_endpoint *)urb->context;
309 if ( waitqueue_active( &ep->wait ) )
310 wake_up_interruptible( &ep->wait );
314 static int usb_write( struct midi_out_endpoint *ep, unsigned char *buf, int len )
316 struct usb_device *d;
322 DECLARE_WAITQUEUE(wait,current);
323 init_waitqueue_head(&ep->wait);
326 pipe = usb_sndbulkpipe(d, ep->endpoint);
327 usb_fill_bulk_urb( ep->urb, d, pipe, (unsigned char*)buf, len,
328 usb_write_callback, ep );
330 status = usb_submit_urb(ep->urb, GFP_KERNEL);
333 printk(KERN_ERR "usbmidi: Cannot submit urb (%d)\n",status);
338 add_wait_queue( &ep->wait, &wait );
339 set_current_state( TASK_INTERRUPTIBLE );
341 while( ep->urb->status == -EINPROGRESS ) {
342 if ( maxretry-- < 0 ) {
343 printk(KERN_ERR "usbmidi: usb_bulk_msg timed out\n");
347 interruptible_sleep_on_timeout( &ep->wait, 10 );
349 set_current_state( TASK_RUNNING );
350 remove_wait_queue( &ep->wait, &wait );
357 /** Copy data from URB to In endpoint buf.
358 * Discard if CIN == 0 or CIN = 1.
363 static void usb_bulk_read(struct urb *urb, struct pt_regs *regs)
365 struct midi_in_endpoint *ep = (struct midi_in_endpoint *)(urb->context);
366 unsigned char *data = urb->transfer_buffer;
369 if ( !ep->urbSubmitted ) {
373 if ( (urb->status == 0) && (urb->actual_length > 0) ) {
375 spin_lock( &ep->lock );
377 for(j = 0; j < urb->actual_length; j += 4) {
378 int cin = (data[j]>>0)&0xf;
379 int cab = (data[j]>>4)&0xf;
380 struct usb_mididev *cable = ep->cables[cab];
382 int len = cin_to_len[cin]; /** length of MIDI data **/
383 for (i = 0; i < len; i++) {
384 cable->min.buf[cable->min.bufWrPtr] = data[1+i+j];
385 cable->min.bufWrPtr = (cable->min.bufWrPtr+1)%MIDI_IN_BUFSIZ;
386 if (cable->min.bufRemains < MIDI_IN_BUFSIZ)
387 cable->min.bufRemains += 1;
388 else /** need to drop data **/
389 cable->min.bufRdPtr += (cable->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
395 spin_unlock ( &ep->lock );
397 wake_up( &ep->wait );
401 /* urb->dev must be reinitialized on 2.4.x kernels */
402 urb->dev = ep->usbdev;
404 urb->actual_length = 0;
405 usb_submit_urb(urb, GFP_ATOMIC);
410 /* ------------------------------------------------------------------------- */
412 /* This routine must be called with spin_lock */
414 /** Wrapper around usb_write().
415 * This routine must be called with spin_lock held on ep.
416 * Called by midiWrite(), putOneMidiEvent(), and usb_midi_write();
418 static int flush_midi_buffer( struct midi_out_endpoint *ep )
422 if ( ep->bufWrPtr > 0 ) {
423 ret = usb_write( ep, ep->buf, ep->bufWrPtr );
431 /* ------------------------------------------------------------------------- */
434 /** Given a MIDI Event, determine size of data to be attached to
437 * Called by midiWrite();
438 * Uses remains_80e0 and remains_f0f6;
440 static int get_remains(int event)
444 if ( event < 0x80 ) {
446 } else if ( event < 0xf0 ) {
447 ret = remains_80e0[((event-0x80)>>4)&0x0f];
448 } else if ( event < 0xf7 ) {
449 ret = remains_f0f6[event-0xf0];
457 /** Given the output MIDI data in the output buffer, computes a reasonable
459 * Called by putOneMidiEvent().
461 static int get_CIN( struct usb_mididev *m )
465 if ( m->mout.buf[0] == 0xf7 ) {
468 else if ( m->mout.buf[1] == 0xf7 ) {
471 else if ( m->mout.buf[2] == 0xf7 ) {
475 if ( m->mout.isInExclusive == 1 ) {
477 } else if ( m->mout.buf[0] < 0x80 ) {
478 /** One byte that we know nothing about. **/
480 } else if ( m->mout.buf[0] < 0xf0 ) {
481 /** MIDI Voice messages 0x8X to 0xEX map to cin 0x8 to 0xE. **/
482 cin = (m->mout.buf[0]>>4)&0x0f;
485 /** Special lookup table exists for real-time events. **/
486 cin = cin_f0ff[m->mout.buf[0]-0xf0];
494 /* ------------------------------------------------------------------------- */
498 /** Move data to USB endpoint buffer.
501 static int put_one_midi_event(struct usb_mididev *m)
505 struct midi_out_endpoint *ep = m->mout.ep;
509 if ( cin > 0x0f || cin < 0 ) {
513 spin_lock_irqsave( &ep->lock, flags );
514 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | cin;
515 ep->buf[ep->bufWrPtr++] = m->mout.buf[0];
516 ep->buf[ep->bufWrPtr++] = m->mout.buf[1];
517 ep->buf[ep->bufWrPtr++] = m->mout.buf[2];
518 if ( ep->bufWrPtr >= ep->bufSize ) {
519 ret = flush_midi_buffer( ep );
521 spin_unlock_irqrestore( &ep->lock, flags);
523 m->mout.buf[0] = m->mout.buf[1] = m->mout.buf[2] = 0;
529 /** Write the MIDI message v on the midi device.
530 * Called by usb_midi_write();
531 * Responsible for packaging a MIDI data stream into USB-MIDI packets.
534 static int midi_write( struct usb_mididev *m, int v )
537 struct midi_out_endpoint *ep = m->mout.ep;
539 unsigned char c = (unsigned char)v;
540 unsigned char sysrt_buf[4];
542 if ( m->singlebyte != 0 ) {
543 /** Simple code to handle the single-byte USB-MIDI protocol. */
544 spin_lock_irqsave( &ep->lock, flags );
545 if ( ep->bufWrPtr+4 > ep->bufSize ) {
546 ret = flush_midi_buffer( ep );
548 spin_unlock_irqrestore( &ep->lock, flags );
552 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | 0x0f; /* single byte */
553 ep->buf[ep->bufWrPtr++] = c;
554 ep->buf[ep->bufWrPtr++] = 0;
555 ep->buf[ep->bufWrPtr++] = 0;
556 if ( ep->bufWrPtr >= ep->bufSize ) {
557 ret = flush_midi_buffer( ep );
559 spin_unlock_irqrestore( &ep->lock, flags );
563 /** Normal USB-MIDI protocol begins here. */
565 if ( c > 0xf7 ) { /* system: Realtime messages */
566 /** Realtime messages are written IMMEDIATELY. */
567 sysrt_buf[0] = (m->mout.cableId<<4) | 0x0f;
571 spin_lock_irqsave( &ep->lock, flags );
572 ret = usb_write( ep, sysrt_buf, 4 );
573 spin_unlock_irqrestore( &ep->lock, flags );
574 /* m->mout.lastEvent = 0; */
581 m->mout.lastEvent = c;
582 m->mout.isInExclusive = 0;
583 m->mout.bufRemains = get_remains(c);
584 } else if ( c == 0xf0 ) {
585 /* m->mout.lastEvent = 0; */
586 m->mout.isInExclusive = 1;
587 m->mout.bufRemains = get_remains(c);
588 } else if ( c == 0xf7 && m->mout.isInExclusive == 1 ) {
589 /* m->mout.lastEvent = 0; */
590 m->mout.isInExclusive = 0;
591 m->mout.bufRemains = 1;
592 } else if ( c > 0xf0 ) {
593 /* m->mout.lastEvent = 0; */
594 m->mout.isInExclusive = 0;
595 m->mout.bufRemains = get_remains(c);
598 } else if ( m->mout.bufRemains == 0 && m->mout.isInExclusive == 0 ) {
599 if ( m->mout.lastEvent == 0 ) {
600 return 0; /* discard, waiting for the first event */
603 m->mout.buf[0] = m->mout.lastEvent;
605 m->mout.bufRemains = get_remains(m->mout.lastEvent)-1;
608 m->mout.buf[m->mout.bufPtr++] = c;
609 m->mout.bufRemains--;
610 if ( m->mout.bufRemains == 0 || m->mout.bufPtr >= 3) {
611 ret = put_one_midi_event(m);
618 /* ------------------------------------------------------------------------- */
620 /** Basic operation on /dev/midiXX as registered through struct file_operations.
622 * Basic contract: Used to change the current read/write position in a file.
623 * On success, the non-negative position is reported.
624 * On failure, the negative of an error code is reported.
626 * Because a MIDIStream is not a file, all seek operations are doomed to fail.
629 static loff_t usb_midi_llseek(struct file *file, loff_t offset, int origin)
631 /** Tell user you cannot seek on a PIPE-like device. **/
636 /** Basic operation on /dev/midiXX as registered through struct file_operations.
638 * Basic contract: Block until count bytes have been read or an error occurs.
642 static ssize_t usb_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
644 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
645 struct midi_in_endpoint *ep = m->min.ep;
647 DECLARE_WAITQUEUE(wait, current);
649 if ( !access_ok(VERIFY_READ, buffer, count) ) {
656 add_wait_queue( &ep->wait, &wait );
662 cnt = m->min.bufRemains;
668 if ( file->f_flags & O_NONBLOCK ) {
673 __set_current_state(TASK_INTERRUPTIBLE);
675 if (signal_pending(current)) {
685 unsigned long flags; /* used to synchronize access to the endpoint */
686 spin_lock_irqsave( &ep->lock, flags );
687 for (i = 0; i < cnt; i++) {
688 if ( copy_to_user( buffer+i, m->min.buf+m->min.bufRdPtr, 1 ) ) {
693 m->min.bufRdPtr = (m->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
694 m->min.bufRemains -= 1;
696 spin_unlock_irqrestore( &ep->lock, flags );
706 remove_wait_queue( &ep->wait, &wait );
707 set_current_state(TASK_RUNNING);
713 /** Basic operation on /dev/midiXX as registered through struct file_operations.
715 * Basic Contract: Take MIDI data byte-by-byte and pass it to
716 * writeMidi() which packages MIDI data into USB-MIDI stream.
717 * Then flushMidiData() is called to ensure all bytes have been written
718 * in a timely fashion.
722 static ssize_t usb_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
724 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
726 unsigned long int flags;
728 if ( !access_ok(VERIFY_READ, buffer, count) ) {
739 if (copy_from_user((unsigned char *)&c, buffer, 1)) {
744 if( midi_write(m, (int)c) ) {
754 spin_lock_irqsave( &m->mout.ep->lock, flags );
755 if ( flush_midi_buffer(m->mout.ep) < 0 ) {
758 spin_unlock_irqrestore( &m->mout.ep->lock, flags );
763 /** Basic operation on /dev/midiXX as registered through struct file_operations.
765 * Basic contract: Wait (spin) until ready to read or write on the file.
768 static unsigned int usb_midi_poll(struct file *file, struct poll_table_struct *wait)
770 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
771 struct midi_in_endpoint *iep = m->min.ep;
772 struct midi_out_endpoint *oep = m->mout.ep;
774 unsigned int mask = 0;
776 if ( file->f_mode & FMODE_READ ) {
777 poll_wait( file, &iep->wait, wait );
778 spin_lock_irqsave( &iep->lock, flags );
779 if ( m->min.bufRemains > 0 )
780 mask |= POLLIN | POLLRDNORM;
781 spin_unlock_irqrestore( &iep->lock, flags );
784 if ( file->f_mode & FMODE_WRITE ) {
785 poll_wait( file, &oep->wait, wait );
786 spin_lock_irqsave( &oep->lock, flags );
787 if ( oep->bufWrPtr < oep->bufSize )
788 mask |= POLLOUT | POLLWRNORM;
789 spin_unlock_irqrestore( &oep->lock, flags );
796 /** Basic operation on /dev/midiXX as registered through struct file_operations.
798 * Basic contract: This is always the first operation performed on the
799 * device node. If no method is defined, the open succeeds without any
800 * notification given to the module.
804 static int usb_midi_open(struct inode *inode, struct file *file)
806 int minor = iminor(inode);
807 DECLARE_WAITQUEUE(wait, current);
808 struct list_head *devs, *mdevs;
809 struct usb_midi_state *s;
810 struct usb_mididev *m;
815 printk(KERN_INFO "usb-midi: Open minor= %d.\n", minor);
820 list_for_each(devs, &mididevs) {
821 s = list_entry(devs, struct usb_midi_state, mididev);
822 list_for_each(mdevs, &s->midiDevList) {
823 m = list_entry(mdevs, struct usb_mididev, list);
824 if ( !((m->dev_midi ^ minor) & ~0xf) )
836 if ( !(m->open_mode & file->f_mode) ) {
839 if ( file->f_flags & O_NONBLOCK ) {
843 __set_current_state(TASK_INTERRUPTIBLE);
844 add_wait_queue( &open_wait, &wait );
847 remove_wait_queue( &open_wait, &wait );
848 if ( signal_pending(current) ) {
853 file->private_data = m;
854 spin_lock_irqsave( &s->lock, flags );
856 if ( !(m->open_mode & (FMODE_READ | FMODE_WRITE)) ) {
857 //FIXME: intented semantics unclear here
860 m->min.bufRemains = 0;
861 spin_lock_init(&m->min.ep->lock);
864 m->mout.bufRemains = 0;
865 m->mout.isInExclusive = 0;
866 m->mout.lastEvent = 0;
867 spin_lock_init(&m->mout.ep->lock);
870 if ( (file->f_mode & FMODE_READ) && m->min.ep != NULL ) {
871 unsigned long int flagsep;
872 spin_lock_irqsave( &m->min.ep->lock, flagsep );
873 m->min.ep->cables[m->min.cableId] = m;
874 m->min.ep->readers += 1;
877 m->min.bufRemains = 0;
878 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
880 if ( !(m->min.ep->urbSubmitted)) {
882 /* urb->dev must be reinitialized on 2.4.x kernels */
883 m->min.ep->urb->dev = m->min.ep->usbdev;
885 if ( usb_submit_urb(m->min.ep->urb, GFP_ATOMIC) ) {
886 printk(KERN_ERR "usbmidi: Cannot submit urb for MIDI-IN\n");
888 m->min.ep->urbSubmitted = 1;
890 m->open_mode |= FMODE_READ;
894 if ( (file->f_mode & FMODE_WRITE) && m->mout.ep != NULL ) {
896 m->mout.bufRemains = 0;
897 m->mout.isInExclusive = 0;
898 m->mout.lastEvent = 0;
899 m->open_mode |= FMODE_WRITE;
903 spin_unlock_irqrestore( &s->lock, flags );
908 /** Changed to prevent extra increments to USE_COUNT. **/
914 printk(KERN_INFO "usb-midi: Open Succeeded. minor= %d.\n", minor);
917 return nonseekable_open(inode, file); /** Success. **/
921 /** Basic operation on /dev/midiXX as registered through struct file_operations.
923 * Basic contract: Close an opened file and deallocate anything we allocated.
924 * Like open(), this can be missing. If open set file->private_data,
925 * release() must clear it.
929 static int usb_midi_release(struct inode *inode, struct file *file)
931 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
932 struct usb_midi_state *s = (struct usb_midi_state *)m->midi;
935 printk(KERN_INFO "usb-midi: Close.\n");
940 if ( m->open_mode & FMODE_WRITE ) {
941 m->open_mode &= ~FMODE_WRITE;
942 usb_unlink_urb( m->mout.ep->urb );
945 if ( m->open_mode & FMODE_READ ) {
946 unsigned long int flagsep;
947 spin_lock_irqsave( &m->min.ep->lock, flagsep );
948 m->min.ep->cables[m->min.cableId] = NULL; // discard cable
949 m->min.ep->readers -= 1;
950 m->open_mode &= ~FMODE_READ;
951 if ( m->min.ep->readers == 0 &&
952 m->min.ep->urbSubmitted ) {
953 m->min.ep->urbSubmitted = 0;
954 usb_unlink_urb(m->min.ep->urb);
956 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
964 file->private_data = NULL;
968 static struct file_operations usb_midi_fops = {
969 .owner = THIS_MODULE,
970 .llseek = usb_midi_llseek,
971 .read = usb_midi_read,
972 .write = usb_midi_write,
973 .poll = usb_midi_poll,
974 .open = usb_midi_open,
975 .release = usb_midi_release,
978 /* ------------------------------------------------------------------------- */
980 /** Returns filled midi_in_endpoint structure or null on failure.
984 * endPoint - An usb endpoint in the range 0 to 15.
985 * Called by allocUsbMidiDev();
989 static struct midi_in_endpoint *alloc_midi_in_endpoint( struct usb_device *d, int endPoint )
991 struct midi_in_endpoint *ep;
995 endPoint &= 0x0f; /* Silently force endPoint to lie in range 0 to 15. */
997 pipe = usb_rcvbulkpipe( d, endPoint );
998 bufSize = usb_maxpacket( d, pipe, usb_pipein(pipe) );
999 /* usb_pipein() = ! usb_pipeout() = true for an in Endpoint */
1001 ep = (struct midi_in_endpoint *)kmalloc(sizeof(struct midi_in_endpoint), GFP_KERNEL);
1003 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint\n");
1006 memset( ep, 0, sizeof(struct midi_in_endpoint) );
1007 // this sets cables[] and readers to 0, too.
1008 // for (i=0; i<16; i++) ep->cables[i] = 0; // discard cable
1011 ep->endpoint = endPoint;
1013 ep->recvBuf = (unsigned char *)kmalloc(sizeof(unsigned char)*(bufSize), GFP_KERNEL);
1014 if ( !ep->recvBuf ) {
1015 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint buffer\n");
1020 ep->urb = usb_alloc_urb(0, GFP_KERNEL); /* no ISO */
1022 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint urb\n");
1027 usb_fill_bulk_urb( ep->urb, d,
1028 usb_rcvbulkpipe(d, endPoint),
1029 (unsigned char *)ep->recvBuf, bufSize,
1030 usb_bulk_read, ep );
1032 /* ep->bufRdPtr = 0; */
1033 /* ep->bufWrPtr = 0; */
1034 /* ep->bufRemains = 0; */
1035 /* ep->urbSubmitted = 0; */
1036 ep->recvBufSize = bufSize;
1038 init_waitqueue_head(&ep->wait);
1043 static int remove_midi_in_endpoint( struct midi_in_endpoint *min )
1045 usb_unlink_urb( min->urb );
1046 usb_free_urb( min->urb );
1047 kfree( min->recvBuf );
1053 /** Returns filled midi_out_endpoint structure or null on failure.
1057 * endPoint - An usb endpoint in the range 0 to 15.
1058 * Called by allocUsbMidiDev();
1061 static struct midi_out_endpoint *alloc_midi_out_endpoint( struct usb_device *d, int endPoint )
1063 struct midi_out_endpoint *ep = NULL;
1068 pipe = usb_sndbulkpipe( d, endPoint );
1069 bufSize = usb_maxpacket( d, pipe, usb_pipeout(pipe) );
1071 ep = (struct midi_out_endpoint *)kmalloc(sizeof(struct midi_out_endpoint), GFP_KERNEL);
1073 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint\n");
1076 memset( ep, 0, sizeof(struct midi_out_endpoint) );
1078 ep->endpoint = endPoint;
1079 ep->buf = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1081 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint buffer\n");
1086 ep->urb = usb_alloc_urb(0, GFP_KERNEL); /* no ISO */
1088 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint urb\n");
1094 ep->bufSize = bufSize;
1095 /* ep->bufWrPtr = 0; */
1097 init_waitqueue_head(&ep->wait);
1103 static int remove_midi_out_endpoint( struct midi_out_endpoint *mout )
1105 usb_unlink_urb( mout->urb );
1106 usb_free_urb( mout->urb );
1114 /** Returns a filled usb_mididev structure, registered as a Linux MIDI device.
1116 * Returns null if memory is not available or the device cannot be registered.
1117 * Called by allocUsbMidiDev();
1120 static struct usb_mididev *allocMidiDev(
1121 struct usb_midi_state *s,
1122 struct midi_in_endpoint *min,
1123 struct midi_out_endpoint *mout,
1127 struct usb_mididev *m;
1129 m = (struct usb_mididev *)kmalloc(sizeof(struct usb_mididev), GFP_KERNEL);
1131 printk(KERN_ERR "usbmidi: no memory for midi device\n");
1135 memset(m, 0, sizeof(struct usb_mididev));
1137 if ((m->dev_midi = register_sound_midi(&usb_midi_fops, -1)) < 0) {
1138 printk(KERN_ERR "usbmidi: cannot register midi device\n");
1144 /* m->open_mode = 0; */
1148 m->min.ep->usbdev = s->usbdev;
1149 m->min.cableId = inCableId;
1151 /* m->min.bufPtr = 0; */
1152 /* m->min.bufRemains = 0; */
1156 m->mout.ep->usbdev = s->usbdev;
1157 m->mout.cableId = outCableId;
1159 /* m->mout.bufPtr = 0; */
1160 /* m->mout.bufRemains = 0; */
1161 /* m->mout.isInExclusive = 0; */
1162 /* m->mout.lastEvent = 0; */
1164 m->singlebyte = singlebyte;
1170 static void release_midi_device( struct usb_midi_state *s )
1172 struct usb_mididev *m;
1173 struct midi_in_endpoint *min;
1174 struct midi_out_endpoint *mout;
1176 if ( s->count > 0 ) {
1181 wake_up( &open_wait );
1183 while(!list_empty(&s->inEndpointList)) {
1184 min = list_entry(s->inEndpointList.next, struct midi_in_endpoint, list);
1185 list_del(&min->list);
1186 remove_midi_in_endpoint(min);
1189 while(!list_empty(&s->outEndpointList)) {
1190 mout = list_entry(s->outEndpointList.next, struct midi_out_endpoint, list);
1191 list_del(&mout->list);
1192 remove_midi_out_endpoint(mout);
1195 while(!list_empty(&s->midiDevList)) {
1196 m = list_entry(s->midiDevList.next, struct usb_mididev, list);
1207 /* ------------------------------------------------------------------------- */
1209 /** Utility routine to find a descriptor in a dump of many descriptors.
1210 * Returns start of descriptor or NULL if not found.
1211 * descStart pointer to list of interfaces.
1212 * descLength length (in bytes) of dump
1213 * after (ignored if NULL) this routine returns only descriptors after "after"
1214 * dtype (mandatory) The descriptor type.
1215 * iface (ignored if -1) returns descriptor at/following given interface
1216 * altSetting (ignored if -1) returns descriptor at/following given altSetting
1219 * Called by parseDescriptor(), find_csinterface_descriptor();
1222 static void *find_descriptor( void *descStart, unsigned int descLength, void *after, unsigned char dtype, int iface, int altSetting )
1224 unsigned char *p, *end, *next;
1225 int interfaceNumber = -1, altSet = -1;
1228 end = p + descLength;
1235 if ( p[1] == USB_DT_INTERFACE ) {
1236 if ( p[0] < USB_DT_INTERFACE_SIZE )
1238 interfaceNumber = p[2];
1241 if ( p[1] == dtype &&
1242 ( !after || ( p > (unsigned char *)after) ) &&
1243 ( ( iface == -1) || (iface == interfaceNumber) ) &&
1244 ( (altSetting == -1) || (altSetting == altSet) )) {
1252 /** Utility to find a class-specific interface descriptor.
1253 * dsubtype is a descriptor subtype
1254 * Called by parseDescriptor();
1256 static void *find_csinterface_descriptor(void *descStart, unsigned int descLength, void *after, u8 dsubtype, int iface, int altSetting)
1260 p = find_descriptor( descStart, descLength, after, USB_DT_CS_INTERFACE, iface, altSetting );
1262 if ( p[0] >= 3 && p[2] == dsubtype )
1264 p = find_descriptor( descStart, descLength, p, USB_DT_CS_INTERFACE,
1265 iface, altSetting );
1271 /** The magic of making a new usb_midi_device from config happens here.
1273 * The caller is responsible for free-ing this return value (if not NULL).
1276 static struct usb_midi_device *parse_descriptor( struct usb_device *d, unsigned char *buffer, int bufSize, unsigned int ifnum , unsigned int altSetting, int quirks)
1278 struct usb_midi_device *u;
1281 unsigned char *next;
1284 unsigned long longBits;
1285 int pins, nbytes, offset, shift, jack;
1286 #ifdef HAVE_JACK_STRINGS
1287 /** Jacks can have associated names. **/
1288 unsigned char jack2string[256];
1292 /* find audiocontrol interface */
1293 p1 = find_csinterface_descriptor( buffer, bufSize, NULL,
1294 MS_HEADER, ifnum, altSetting);
1300 if ( p1[0] < MS_HEADER_LENGTH ) {
1304 /* Assume success. Since the device corresponds to USB-MIDI spec, we assume
1305 that the rest of the USB 2.0 spec is obeyed. */
1307 u = (struct usb_midi_device *)kmalloc( sizeof(struct usb_midi_device), GFP_KERNEL );
1311 u->deviceName = NULL;
1312 u->idVendor = d->descriptor.idVendor;
1313 u->idProduct = d->descriptor.idProduct;
1314 u->interface = ifnum;
1315 u->altSetting = altSetting;
1316 u->in[0].endpoint = -1;
1317 u->in[0].cableId = -1;
1318 u->out[0].endpoint = -1;
1319 u->out[0].cableId = -1;
1322 printk(KERN_INFO "usb-midi: Found MIDIStreaming device corresponding to Release %d.%02d of spec.\n",
1323 (p1[4] >> 4) * 10 + (p1[4] & 0x0f ),
1324 (p1[3] >> 4) * 10 + (p1[3] & 0x0f )
1327 length = p1[5] | (p1[6] << 8);
1329 #ifdef HAVE_JACK_STRINGS
1330 memset(jack2string, 0, sizeof(unsigned char) * 256);
1334 for (p2 = p1 + p1[0]; length > 0; p2 = next) {
1340 if (p2[1] != USB_DT_CS_INTERFACE)
1342 if (p2[2] == MIDI_IN_JACK && p2[0] >= 6 ) {
1344 #ifdef HAVE_JACK_STRINGS
1345 jack2string[jack] = p2[5];
1347 printk(KERN_INFO "usb-midi: Found IN Jack 0x%02x %s\n",
1348 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL" );
1349 } else if ( p2[2] == MIDI_OUT_JACK && p2[0] >= 6) {
1351 if ( p2[0] < (6 + 2 * pins) )
1354 #ifdef HAVE_JACK_STRINGS
1355 jack2string[jack] = p2[5 + 2 * pins];
1357 printk(KERN_INFO "usb-midi: Found OUT Jack 0x%02x %s, %d pins\n",
1358 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL", pins );
1359 } else if ( p2[2] == ELEMENT_DESCRIPTOR && p2[0] >= 10) {
1361 if ( p2[0] < (9 + 2 * pins ) )
1363 nbytes = p2[8 + 2 * pins ];
1364 if ( p2[0] < (10 + 2 * pins + nbytes) )
1367 for ( offset = 0, shift = 0; offset < nbytes && offset < 8; offset ++, shift += 8) {
1368 longBits |= ((long)(p2[9 + 2 * pins + offset])) << shift;
1371 #ifdef HAVE_JACK_STRINGS
1372 jack2string[jack] = p2[9 + 2 * pins + nbytes];
1374 printk(KERN_INFO "usb-midi: Found ELEMENT 0x%02x, %d/%d pins in/out, bits: 0x%016lx\n",
1375 jack, pins, (int)(p2[5 + 2 * pins]), (long)longBits );
1386 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1387 ifnum, altSetting ); p1; p1 = next ) {
1388 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1389 ifnum, altSetting );
1390 p2 = find_descriptor(buffer, bufSize, p1, USB_DT_CS_ENDPOINT,
1391 ifnum, altSetting );
1393 if ( p2 && next && ( p2 > next ) )
1396 if ( p1[0] < 9 || !p2 || p2[0] < 4 )
1399 if ( (p1[2] & 0x80) == 0x80 ) {
1401 pins = p2[3]; /* not pins -- actually "cables" */
1404 u->in[iep].endpoint = p1[2];
1405 u->in[iep].cableId = ( 1 << pins ) - 1;
1406 if ( u->in[iep].cableId )
1409 u->in[iep].endpoint = -1;
1410 u->in[iep].cableId = -1;
1415 pins = p2[3]; /* not pins -- actually "cables" */
1418 u->out[oep].endpoint = p1[2];
1419 u->out[oep].cableId = ( 1 << pins ) - 1;
1420 if ( u->out[oep].cableId )
1423 u->out[oep].endpoint = -1;
1424 u->out[oep].cableId = -1;
1430 } else if (quirks==1) {
1432 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1433 ifnum, altSetting ); p1; p1 = next ) {
1434 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1435 ifnum, altSetting );
1440 if ( (p1[2] & 0x80) == 0x80 ) {
1445 u->in[iep].endpoint = p1[2];
1446 u->in[iep].cableId = ( 1 << pins ) - 1;
1447 if ( u->in[iep].cableId )
1450 u->in[iep].endpoint = -1;
1451 u->in[iep].cableId = -1;
1459 u->out[oep].endpoint = p1[2];
1460 u->out[oep].cableId = ( 1 << pins ) - 1;
1461 if ( u->out[oep].cableId )
1464 u->out[oep].endpoint = -1;
1465 u->out[oep].cableId = -1;
1473 if ( !iep && ! oep ) {
1484 /* ------------------------------------------------------------------------- */
1486 /** Returns number between 0 and 16.
1489 static int on_bits( unsigned short v )
1494 for ( i=0 ; i<16 ; i++ ) {
1503 /** USB-device will be interrogated for altSetting.
1505 * Returns negative on error.
1506 * Called by allocUsbMidiDev();
1510 static int get_alt_setting( struct usb_device *d, int ifnum )
1513 struct usb_interface *iface;
1514 struct usb_host_interface *interface;
1515 struct usb_endpoint_descriptor *ep;
1519 iface = usb_ifnum_to_if( d, ifnum );
1520 alts = iface->num_altsetting;
1522 for ( alt=0 ; alt<alts ; alt++ ) {
1523 interface = &iface->altsetting[alt];
1527 for ( i=0 ; i<interface->desc.bNumEndpoints ; i++ ) {
1528 ep = &interface->endpoint[i].desc;
1529 if ( (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ) {
1532 if ( (ep->bEndpointAddress & USB_DIR_IN) && epin < 0 ) {
1534 } else if ( epout < 0 ) {
1537 if ( epin >= 0 && epout >= 0 ) {
1538 return interface->desc.bAlternateSetting;
1547 /* ------------------------------------------------------------------------- */
1550 /** Returns 0 if successful in allocating and registering internal structures.
1551 * Returns negative on failure.
1552 * Calls allocMidiDev which additionally registers /dev/midiXX devices.
1553 * Writes messages on success to indicate which /dev/midiXX is which physical
1557 static int alloc_usb_midi_device( struct usb_device *d, struct usb_midi_state *s, struct usb_midi_device *u )
1559 struct usb_mididev **mdevs=NULL;
1560 struct midi_in_endpoint *mins[15], *min;
1561 struct midi_out_endpoint *mouts[15], *mout;
1562 int inDevs=0, outDevs=0;
1563 int inEndpoints=0, outEndpoints=0;
1564 int inEndpoint, outEndpoint;
1565 int inCableId, outCableId;
1570 /* Obtain altSetting or die.. */
1571 alt = u->altSetting;
1573 alt = get_alt_setting( d, u->interface );
1578 /* Configure interface */
1579 if ( usb_set_interface( d, u->interface, alt ) < 0 ) {
1583 for ( i = 0 ; i < 15 ; i++ ) {
1588 /* Begin Allocation */
1589 while( inEndpoints < 15
1590 && inDevs < maxdevices
1591 && u->in[inEndpoints].cableId >= 0 ) {
1592 inDevs += on_bits((unsigned short)u->in[inEndpoints].cableId);
1593 mins[inEndpoints] = alloc_midi_in_endpoint( d, u->in[inEndpoints].endpoint );
1594 if ( mins[inEndpoints] == NULL )
1599 while( outEndpoints < 15
1600 && outDevs < maxdevices
1601 && u->out[outEndpoints].cableId >= 0 ) {
1602 outDevs += on_bits((unsigned short)u->out[outEndpoints].cableId);
1603 mouts[outEndpoints] = alloc_midi_out_endpoint( d, u->out[outEndpoints].endpoint );
1604 if ( mouts[outEndpoints] == NULL )
1609 devices = inDevs > outDevs ? inDevs : outDevs;
1610 devices = maxdevices > devices ? devices : maxdevices;
1612 /* obtain space for device name (iProduct) if not known. */
1613 if ( ! u->deviceName ) {
1614 mdevs = (struct usb_mididev **)
1615 kmalloc(sizeof(struct usb_mididevs *)*devices
1616 + sizeof(char) * 256, GFP_KERNEL);
1618 mdevs = (struct usb_mididev **)
1619 kmalloc(sizeof(struct usb_mididevs *)*devices, GFP_KERNEL);
1627 for ( i=0 ; i<devices ; i++ ) {
1631 /* obtain device name (iProduct) if not known. */
1632 if ( ! u->deviceName ) {
1633 u->deviceName = (char *) (mdevs + devices);
1634 if ( ! d->have_langid && d->descriptor.iProduct) {
1635 alt = usb_get_string(d, 0, 0, u->deviceName, 250);
1637 printk(KERN_INFO "error getting string descriptor 0 (error=%d)\n", alt);
1638 } else if (u->deviceName[0] < 4) {
1639 printk(KERN_INFO "string descriptor 0 too short (length = %d)\n", alt);
1641 printk(KERN_INFO "string descriptor 0 found (length = %d)\n", alt);
1642 for(; alt >= 4; alt -= 2) {
1643 i = u->deviceName[alt-2] | (u->deviceName[alt-1]<< 8);
1644 printk(KERN_INFO "usb-midi: langid(%d) 0x%04x\n",
1646 if ( ( ( i ^ ulangid ) & 0xff ) == 0 ) {
1648 d->string_langid = i;
1649 printk(KERN_INFO "usb-midi: langid(match) 0x%04x\n", i);
1656 u->deviceName[0] = (char) 0;
1657 if (d->descriptor.iProduct) {
1658 printk(KERN_INFO "usb-midi: fetchString(%d)\n", d->descriptor.iProduct);
1659 alt = usb_string(d, d->descriptor.iProduct, u->deviceName, 255);
1661 u->deviceName[0] = (char) 0;
1663 printk(KERN_INFO "usb-midi: fetchString = %d\n", alt);
1666 if ( !u->deviceName[0] ) {
1667 if ( d->descriptor.idVendor == USB_VENDOR_ID_ROLAND ) {
1668 strcpy(u->deviceName, "Unknown Roland");
1669 } else if ( d->descriptor.idVendor == USB_VENDOR_ID_STEINBERG ) {
1670 strcpy(u->deviceName, "Unknown Steinberg");
1671 } else if ( d->descriptor.idVendor == USB_VENDOR_ID_YAMAHA ) {
1672 strcpy(u->deviceName, "Unknown Yamaha");
1674 strcpy(u->deviceName, "Unknown");
1679 inEndpoint = 0; inCableId = -1;
1680 outEndpoint = 0; outCableId = -1;
1682 for ( i=0 ; i<devices ; i++ ) {
1683 for ( inCableId ++ ;
1686 && !(u->in[inEndpoint].cableId & (1<<inCableId)) ;
1688 if ( inCableId >= 16 ) {
1693 min = mins[inEndpoint];
1694 for ( outCableId ++ ;
1696 && mouts[outEndpoint]
1697 && !(u->out[outEndpoint].cableId & (1<<outCableId)) ;
1699 if ( outCableId >= 16 ) {
1704 mout = mouts[outEndpoint];
1706 mdevs[i] = allocMidiDev( s, min, mout, inCableId, outCableId );
1707 if ( mdevs[i] == NULL )
1713 for ( i=0 ; i<devices ; i++ ) {
1714 list_add_tail( &mdevs[i]->list, &s->midiDevList );
1716 for ( i=0 ; i<inEndpoints ; i++ ) {
1717 list_add_tail( &mins[i]->list, &s->inEndpointList );
1719 for ( i=0 ; i<outEndpoints ; i++ ) {
1720 list_add_tail( &mouts[i]->list, &s->outEndpointList );
1723 printk(KERN_INFO "usbmidi: found [ %s ] (0x%04x:0x%04x), attached:\n", u->deviceName, u->idVendor, u->idProduct );
1724 for ( i=0 ; i<devices ; i++ ) {
1725 int dm = (mdevs[i]->dev_midi-2)>>4;
1726 if ( mdevs[i]->mout.ep != NULL && mdevs[i]->min.ep != NULL ) {
1727 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%2d) out (ep:%02x cid:%2d bufsiz:%2d)\n",
1729 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize,
1730 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1731 } else if ( mdevs[i]->min.ep != NULL ) {
1732 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%02d)\n",
1734 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize);
1735 } else if ( mdevs[i]->mout.ep != NULL ) {
1736 printk(KERN_INFO "usbmidi: /dev/midi%02d: out (ep:%02x cid:%2d bufsiz:%02d)\n",
1738 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1746 if ( mdevs != NULL ) {
1747 for ( i=0 ; i<devices ; i++ ) {
1748 if ( mdevs[i] != NULL ) {
1749 unregister_sound_midi( mdevs[i]->dev_midi );
1756 for ( i=0 ; i<15 ; i++ ) {
1757 if ( mins[i] != NULL ) {
1758 remove_midi_in_endpoint( mins[i] );
1760 if ( mouts[i] != NULL ) {
1761 remove_midi_out_endpoint( mouts[i] );
1768 /* ------------------------------------------------------------------------- */
1770 /** Attempt to scan YAMAHA's device descriptor and detect correct values of
1772 * Return 0 on succes, negative on failure.
1773 * Called by usb_midi_probe();
1776 static int detect_yamaha_device( struct usb_device *d,
1777 struct usb_interface *iface, unsigned int ifnum,
1778 struct usb_midi_state *s)
1780 struct usb_host_interface *interface;
1781 struct usb_midi_device *u;
1782 unsigned char *buffer;
1788 if (d->descriptor.idVendor != USB_VENDOR_ID_YAMAHA) {
1792 for ( i=0 ; i < iface->num_altsetting; i++ ) {
1793 interface = iface->altsetting + i;
1795 if ( interface->desc.bInterfaceClass != 255 ||
1796 interface->desc.bInterfaceSubClass != 0 )
1798 alts = interface->desc.bAlternateSetting;
1804 printk(KERN_INFO "usb-midi: Found YAMAHA USB-MIDI device on dev %04x:%04x, iface %d\n",
1805 d->descriptor.idVendor, d->descriptor.idProduct, ifnum);
1807 i = d->actconfig - d->config;
1808 buffer = d->rawdescriptors[i];
1809 bufSize = d->actconfig->desc.wTotalLength;
1811 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 1);
1816 ret = alloc_usb_midi_device( d, s, u );
1824 /** Scan table of known devices which are only partially compliant with
1825 * the MIDIStreaming specification.
1826 * Called by usb_midi_probe();
1830 static int detect_vendor_specific_device( struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s )
1832 struct usb_midi_device *u;
1836 for ( i=0; i<VENDOR_SPECIFIC_USB_MIDI_DEVICES ; i++ ) {
1837 u=&(usb_midi_devices[i]);
1839 if ( d->descriptor.idVendor != u->idVendor ||
1840 d->descriptor.idProduct != u->idProduct ||
1841 ifnum != u->interface )
1844 ret = alloc_usb_midi_device( d, s, u );
1852 /** Attempt to match any config of an interface to a MIDISTREAMING interface.
1853 * Returns 0 on success, negative on failure.
1854 * Called by usb_midi_probe();
1856 static int detect_midi_subclass(struct usb_device *d,
1857 struct usb_interface *iface, unsigned int ifnum,
1858 struct usb_midi_state *s)
1860 struct usb_host_interface *interface;
1861 struct usb_midi_device *u;
1862 unsigned char *buffer;
1868 for ( i=0 ; i < iface->num_altsetting; i++ ) {
1869 interface = iface->altsetting + i;
1871 if ( interface->desc.bInterfaceClass != USB_CLASS_AUDIO ||
1872 interface->desc.bInterfaceSubClass != USB_SUBCLASS_MIDISTREAMING )
1874 alts = interface->desc.bAlternateSetting;
1880 printk(KERN_INFO "usb-midi: Found MIDISTREAMING on dev %04x:%04x, iface %d\n",
1881 d->descriptor.idVendor, d->descriptor.idProduct, ifnum);
1884 /* From USB Spec v2.0, Section 9.5.
1885 If the class or vendor specific descriptors use the same format
1886 as standard descriptors (e.g., start with a length byte and
1887 followed by a type byte), they must be returned interleaved with
1888 standard descriptors in the configuration information returned by
1889 a GetDescriptor(Configuration) request. In this case, the class
1890 or vendor-specific descriptors must follow a related standard
1891 descriptor they modify or extend.
1894 i = d->actconfig - d->config;
1895 buffer = d->rawdescriptors[i];
1896 bufSize = d->actconfig->desc.wTotalLength;
1898 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 0);
1903 ret = alloc_usb_midi_device( d, s, u );
1911 /** When user has requested a specific device, match it exactly.
1913 * Uses uvendor, uproduct, uinterface, ualt, umin, umout and ucable.
1914 * Called by usb_midi_probe();
1917 static int detect_by_hand(struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1919 struct usb_midi_device u;
1921 if ( d->descriptor.idVendor != uvendor ||
1922 d->descriptor.idProduct != uproduct ||
1923 ifnum != uinterface ) {
1930 if ( umin < 0 || umin > 15 )
1931 umin = 0x01 | USB_DIR_IN;
1932 if ( umout < 0 || umout > 15 )
1934 if ( ucable < 0 || ucable > 15 )
1937 u.deviceName = NULL; /* A flag for alloc_usb_midi_device to get device
1938 name from device. */
1939 u.idVendor = uvendor;
1940 u.idProduct = uproduct;
1941 u.interface = uinterface;
1942 u.altSetting = ualt;
1944 u.in[0].endpoint = umin;
1945 u.in[0].cableId = (1<<ucable);
1947 u.out[0].endpoint = umout;
1948 u.out[0].cableId = (1<<ucable);
1950 return alloc_usb_midi_device( d, s, &u );
1955 /* ------------------------------------------------------------------------- */
1957 static int usb_midi_probe(struct usb_interface *intf,
1958 const struct usb_device_id *id)
1960 struct usb_midi_state *s;
1961 struct usb_device *dev = interface_to_usbdev(intf);
1962 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1964 s = (struct usb_midi_state *)kmalloc(sizeof(struct usb_midi_state), GFP_KERNEL);
1968 memset( s, 0, sizeof(struct usb_midi_state) );
1969 INIT_LIST_HEAD(&s->midiDevList);
1970 INIT_LIST_HEAD(&s->inEndpointList);
1971 INIT_LIST_HEAD(&s->outEndpointList);
1974 spin_lock_init(&s->lock);
1977 detect_by_hand( dev, ifnum, s ) &&
1978 detect_midi_subclass( dev, intf, ifnum, s ) &&
1979 detect_vendor_specific_device( dev, ifnum, s ) &&
1980 detect_yamaha_device( dev, intf, ifnum, s) ) {
1986 list_add_tail(&s->mididev, &mididevs);
1989 usb_set_intfdata (intf, s);
1994 static void usb_midi_disconnect(struct usb_interface *intf)
1996 struct usb_midi_state *s = usb_get_intfdata (intf);
1997 struct list_head *list;
1998 struct usb_mididev *m;
2003 if ( s == (struct usb_midi_state *)-1 ) {
2010 list_del(&s->mididev);
2011 INIT_LIST_HEAD(&s->mididev);
2013 usb_set_intfdata (intf, NULL);
2015 list_for_each(list, &s->midiDevList) {
2016 m = list_entry(list, struct usb_mididev, list);
2017 wake_up(&(m->min.ep->wait));
2018 wake_up(&(m->mout.ep->wait));
2019 if ( m->dev_midi >= 0 ) {
2020 unregister_sound_midi(m->dev_midi);
2024 release_midi_device(s);
2025 wake_up(&open_wait);
2028 /* we want to look at all devices by hand */
2029 static struct usb_device_id id_table[] = {
2030 {.driver_info = 42},
2034 static struct usb_driver usb_midi_driver = {
2035 .owner = THIS_MODULE,
2037 .probe = usb_midi_probe,
2038 .disconnect = usb_midi_disconnect,
2039 .id_table = id_table,
2042 /* ------------------------------------------------------------------------- */
2044 static int __init usb_midi_init(void)
2046 return usb_register(&usb_midi_driver);
2049 static void __exit usb_midi_exit(void)
2051 usb_deregister(&usb_midi_driver);
2054 module_init(usb_midi_init) ;
2055 module_exit(usb_midi_exit) ;
2057 #ifdef HAVE_ALSA_SUPPORT
2058 #define SNDRV_MAIN_OBJECT_FILE
2059 #include "../../include/driver.h"
2060 #include "../../include/control.h"
2061 #include "../../include/info.h"
2062 #include "../../include/cs46xx.h"
2064 /* ------------------------------------------------------------------------- */
2066 static int snd_usbmidi_input_close(snd_rawmidi_substream_t * substream)
2071 static int snd_usbmidi_input_open(snd_rawmidi_substream_t * substream )
2076 static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t * substream, int up)
2082 /* ------------------------------------------------------------------------- */
2084 static int snd_usbmidi_output_close(snd_rawmidi_substream_t * substream)
2089 static int snd_usbmidi_output_open(snd_rawmidi_substream_t * substream)
2094 static void snd_usb_midi_output_trigger(snd_rawmidi_substream_t * substream,
2100 /* ------------------------------------------------------------------------- */
2102 static snd_rawmidi_ops_t snd_usbmidi_output =
2104 .open = snd_usbmidi_output_open,
2105 .close = snd_usbmidi_output_close,
2106 .trigger = snd_usbmidi_output_trigger,
2108 static snd_rawmidi_ops_t snd_usbmidi_input =
2110 .open = snd_usbmidi_input_open,
2111 .close = snd_usbmidi_input_close,
2112 .trigger = snd_usbmidi_input_trigger,
2115 int snd_usbmidi_midi(cs46xx_t *chip, int device, snd_rawmidi_t **rrawmidi)
2117 snd_rawmidi_t *rmidi;
2122 if ((err = snd_rawmidi_new(chip->card, "USB-MIDI", device, 1, 1, &rmidi)) < 0)
2124 strcpy(rmidi->name, "USB-MIDI");
2126 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output );
2127 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input );
2129 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
2131 rmidi->private_data = chip;
2132 chip->rmidi = rmidi;
2139 int snd_usbmidi_create( snd_card_t * card,
2140 struct pci_dev * pci,
2141 usbmidi_t ** rchip )
2145 snd_region_t *region;
2146 static snd_device_opt_t ops = {
2147 .dev_free = snd_usbmidi_dev_free,
2151 chip = snd_magic_kcalloc( usbmidi_t, 0, GFP_KERNEL );
2156 EXPORT_SYMBOL(snd_usbmidi_create);
2157 EXPORT_SYMBOL(snd_usbmidi_midi);
2158 #endif /* HAVE_ALSA_SUPPORT */