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/wrapper.h>
37 #include <linux/usb.h>
38 #include <linux/poll.h>
39 #include <linux/sound.h>
40 #include <linux/init.h>
41 #include <asm/semaphore.h>
43 /** This declaration is missing from linux/usb.h **/
44 extern int usb_get_string(struct usb_device *dev, unsigned short langid, unsigned char index, void *buf, int size);
48 /* ------------------------------------------------------------------------- */
50 /* More verbose on syslog */
53 #define MIDI_IN_BUFSIZ 1024
55 #define HAVE_SUPPORT_USB_MIDI_CLASS
57 #undef HAVE_SUPPORT_ALSA
59 #undef MOD_INC_EACH_PROBE
61 /* ------------------------------------------------------------------------- */
63 static int singlebyte = 0;
64 MODULE_PARM(singlebyte,"i");
65 MODULE_PARM_DESC(singlebyte,"Enable sending MIDI messages with single message packet");
67 static int maxdevices = 4;
68 MODULE_PARM(maxdevices,"i");
69 MODULE_PARM_DESC(maxdevices,"Max number of allocatable MIDI device");
71 static int uvendor = -1;
72 MODULE_PARM(uvendor,"i");
73 MODULE_PARM_DESC(uvendor, "The USB Vendor ID of a semi-compliant interface");
75 static int uproduct = -1;
76 MODULE_PARM(uproduct,"i");
77 MODULE_PARM_DESC(uproduct, "The USB Product ID of a semi-compliant interface");
79 static int uinterface = -1;
80 MODULE_PARM(uinterface,"i");
81 MODULE_PARM_DESC(uinterface, "The Interface number of a semi-compliant interface");
84 MODULE_PARM(ualt,"i");
85 MODULE_PARM_DESC(ualt, "The optional alternative setting of a semi-compliant interface");
88 MODULE_PARM(umin,"i");
89 MODULE_PARM_DESC(umin, "The input endpoint of a semi-compliant interface");
91 static int umout = -1;
92 MODULE_PARM(umout,"i");
93 MODULE_PARM_DESC(umout, "The output endpoint of a semi-compliant interface");
95 static int ucable = -1;
96 MODULE_PARM(ucable,"i");
97 MODULE_PARM_DESC(ucable, "The cable number used for a semi-compliant interface");
99 /** Note -- the usb_string() returns only Latin-1 characters.
100 * (unicode chars <= 255). To support Japanese, a unicode16LE-to-EUC or
101 * unicode16LE-to-JIS routine is needed to wrap around usb_get_string().
103 static unsigned short ulangid = 0x0409; /** 0x0411 for Japanese **/
104 MODULE_PARM(ulangid,"h");
105 MODULE_PARM_DESC(ulangid, "The optional preferred USB Language ID for all devices");
107 MODULE_AUTHOR("NAGANO Daisuke <breeze.nagano@nifty.ne.jp>");
108 MODULE_DESCRIPTION("USB-MIDI driver");
109 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,14)
110 MODULE_LICENSE("GPL");
113 /* ------------------------------------------------------------------------- */
115 /** MIDIStreaming Class-Specific Interface Descriptor Subtypes **/
117 #define MS_DESCRIPTOR_UNDEFINED 0
119 #define MIDI_IN_JACK 2
120 #define MIDI_OUT_JACK 3
121 /* Spec reads: ELEMENT */
122 #define ELEMENT_DESCRIPTOR 4
124 #define MS_HEADER_LENGTH 7
126 /** MIDIStreaming Class-Specific Endpoint Descriptor Subtypes **/
128 #define DESCRIPTOR_UNDEFINED 0
129 /* Spec reads: MS_GENERAL */
130 #define MS_GENERAL_ENDPOINT 1
132 /** MIDIStreaming MIDI IN and OUT Jack Types **/
134 #define JACK_TYPE_UNDEFINED 0
135 /* Spec reads: EMBEDDED */
136 #define EMBEDDED_JACK 1
137 /* Spec reads: EXTERNAL */
138 #define EXTERNAL_JACK 2
143 usb_midi_state usb_device
150 | | (cable to device pairing magic)
152 usb_midi_dev dev_id (major,minor) == file->private_data
156 /* usb_midi_state: corresponds to a USB-MIDI module */
157 struct usb_midi_state {
158 struct list_head mididev;
160 struct usb_device *usbdev;
162 struct list_head midiDevList;
163 struct list_head inEndpointList;
164 struct list_head outEndpointList;
168 unsigned int count; /* usage counter */
171 /* midi_out_endpoint: corresponds to an output endpoint */
172 struct midi_out_endpoint {
173 struct list_head list;
175 struct usb_device *usbdev;
178 wait_queue_head_t wait;
187 /* midi_in_endpoint: corresponds to an input endpoint */
188 struct midi_in_endpoint {
189 struct list_head list;
191 struct usb_device *usbdev;
194 wait_queue_head_t wait;
196 struct usb_mididev *cables[16]; // cables open for read
197 int readers; // number of cables open for read
200 unsigned char *recvBuf;
202 int urbSubmitted; //FIXME: == readers > 0
205 /* usb_mididev: corresponds to a logical device */
207 struct list_head list;
209 struct usb_midi_state *midi;
214 struct midi_in_endpoint *ep;
217 // as we are pushing data from usb_bulk_read to usb_midi_read,
218 // we need a larger, cyclic buffer here.
219 unsigned char buf[MIDI_IN_BUFSIZ];
226 struct midi_out_endpoint *ep;
229 unsigned char buf[3];
234 unsigned char lastEvent;
240 /** Map the high nybble of MIDI voice messages to number of Message bytes.
241 * High nyble ranges from 0x8 to 0xe
244 static int remains_80e0[] = {
245 3, /** 0x8X Note Off **/
246 3, /** 0x9X Note On **/
247 3, /** 0xAX Poly-key pressure **/
248 3, /** 0xBX Control Change **/
249 2, /** 0xCX Program Change **/
250 2, /** 0xDX Channel pressure **/
251 3 /** 0xEX PitchBend Change **/
254 /** Map the messages to a number of Message bytes.
257 static int remains_f0f6[] = {
262 2, /** 0XF4 (Undefined by MIDI Spec, and subject to change) **/
263 2, /** 0XF5 (Undefined by MIDI Spec, and subject to change) **/
267 /** Map the messages to a CIN (Code Index Number).
270 static int cin_f0ff[] = {
271 4, /** 0xF0 System Exclusive Message Start (special cases may be 6 or 7) */
278 5, /** 0xF7 End of System Exclusive Message (May be 6 or 7) **/
289 /** Map MIDIStreaming Event packet Code Index Number (low nybble of byte 0)
290 * to the number of bytes of valid MIDI data.
292 * CIN of 0 and 1 are NOT USED in MIDIStreaming 1.0.
295 static int cin_to_len[] = {
303 /* ------------------------------------------------------------------------- */
305 static struct list_head mididevs = LIST_HEAD_INIT(mididevs);
307 static DECLARE_MUTEX(open_sem);
308 static DECLARE_WAIT_QUEUE_HEAD(open_wait);
311 /* ------------------------------------------------------------------------- */
313 static void usb_write_callback(struct urb *urb)
315 struct midi_out_endpoint *ep = (struct midi_out_endpoint *)urb->context;
317 if ( waitqueue_active( &ep->wait ) )
318 wake_up_interruptible( &ep->wait );
322 static int usb_write( struct midi_out_endpoint *ep, unsigned char *buf, int len )
324 struct usb_device *d;
330 DECLARE_WAITQUEUE(wait,current);
331 init_waitqueue_head(&ep->wait);
334 pipe = usb_sndbulkpipe(d, ep->endpoint);
335 FILL_BULK_URB( ep->urb, d, pipe, (unsigned char*)buf, len,
336 (usb_complete_t)usb_write_callback, ep );
338 status = usb_submit_urb(ep->urb);
341 printk(KERN_ERR "usbmidi: Cannot submit urb (%d)\n",status);
345 add_wait_queue( &ep->wait, &wait );
346 set_current_state( TASK_INTERRUPTIBLE );
348 while( ep->urb->status == -EINPROGRESS ) {
349 if ( maxretry-- < 0 ) {
350 printk(KERN_ERR "usbmidi: usb_bulk_msg timed out\n");
354 interruptible_sleep_on_timeout( &ep->wait, 10 );
356 set_current_state( TASK_RUNNING );
357 remove_wait_queue( &ep->wait, &wait );
363 /** Copy data from URB to In endpoint buf.
364 * Discard if CIN == 0 or CIN = 1.
369 static void usb_bulk_read(struct urb *urb)
371 struct midi_in_endpoint *ep = (struct midi_in_endpoint *)(urb->context);
372 unsigned char *data = urb->transfer_buffer;
374 unsigned long int flags;
376 if ( !ep->urbSubmitted ) {
380 if ( (urb->status == 0) && (urb->actual_length > 0) ) {
382 spin_lock_irqsave( &ep->lock, flags );
384 for(j = 0; j < urb->actual_length; j += 4) {
385 int cin = (data[j]>>0)&0xf;
386 int cab = (data[j]>>4)&0xf;
387 struct usb_mididev *cable = ep->cables[cab];
389 int len = cin_to_len[cin]; /** length of MIDI data **/
390 for (i = 0; i < len; i++) {
391 cable->min.buf[cable->min.bufWrPtr] = data[1+i+j];
392 cable->min.bufWrPtr = (cable->min.bufWrPtr+1)%MIDI_IN_BUFSIZ;
393 if (cable->min.bufRemains < MIDI_IN_BUFSIZ)
394 cable->min.bufRemains += 1;
395 else /** need to drop data **/
396 cable->min.bufRdPtr += (cable->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
402 spin_unlock_irqrestore( &ep->lock, flags );
404 wake_up( &ep->wait );
408 /* urb->dev must be reinitialized on 2.4.x kernels */
409 urb->dev = ep->usbdev;
411 urb->actual_length = 0;
417 /* ------------------------------------------------------------------------- */
419 /* This routine must be called with spin_lock */
421 /** Wrapper around usb_write().
422 * This routine must be called with spin_lock held on ep.
423 * Called by midiWrite(), putOneMidiEvent(), and usb_midi_write();
425 static int flush_midi_buffer( struct midi_out_endpoint *ep )
429 if ( ep->bufWrPtr > 0 ) {
430 ret = usb_write( ep, ep->buf, ep->bufWrPtr );
438 /* ------------------------------------------------------------------------- */
441 /** Given a MIDI Event, determine size of data to be attached to
444 * Called by midiWrite();
445 * Uses remains_80e0 and remains_f0f6;
447 static int get_remains(int event)
451 if ( event < 0x80 ) {
453 } else if ( event < 0xf0 ) {
454 ret = remains_80e0[((event-0x80)>>4)&0x0f];
455 } else if ( event < 0xf7 ) {
456 ret = remains_f0f6[event-0xf0];
464 /** Given the output MIDI data in the output buffer, computes a reasonable
466 * Called by putOneMidiEvent().
468 static int get_CIN( struct usb_mididev *m )
472 if ( m->mout.buf[0] == 0xf7 ) {
475 else if ( m->mout.buf[1] == 0xf7 ) {
478 else if ( m->mout.buf[2] == 0xf7 ) {
482 if ( m->mout.isInExclusive == 1 ) {
484 } else if ( m->mout.buf[0] < 0x80 ) {
485 /** One byte that we know nothing about. **/
487 } else if ( m->mout.buf[0] < 0xf0 ) {
488 /** MIDI Voice messages 0x8X to 0xEX map to cin 0x8 to 0xE. **/
489 cin = (m->mout.buf[0]>>4)&0x0f;
492 /** Special lookup table exists for real-time events. **/
493 cin = cin_f0ff[m->mout.buf[0]-0xf0];
501 /* ------------------------------------------------------------------------- */
505 /** Move data to USB endpoint buffer.
508 static int put_one_midi_event(struct usb_mididev *m)
512 struct midi_out_endpoint *ep = m->mout.ep;
516 if ( cin > 0x0f || cin < 0 ) {
520 spin_lock_irqsave( &ep->lock, flags );
521 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | cin;
522 ep->buf[ep->bufWrPtr++] = m->mout.buf[0];
523 ep->buf[ep->bufWrPtr++] = m->mout.buf[1];
524 ep->buf[ep->bufWrPtr++] = m->mout.buf[2];
525 if ( ep->bufWrPtr >= ep->bufSize ) {
526 ret = flush_midi_buffer( ep );
528 spin_unlock_irqrestore( &ep->lock, flags);
530 m->mout.buf[0] = m->mout.buf[1] = m->mout.buf[2] = 0;
536 /** Write the MIDI message v on the midi device.
537 * Called by usb_midi_write();
538 * Responsible for packaging a MIDI data stream into USB-MIDI packets.
541 static int midi_write( struct usb_mididev *m, int v )
544 struct midi_out_endpoint *ep = m->mout.ep;
546 unsigned char c = (unsigned char)v;
547 unsigned char sysrt_buf[4];
549 if ( m->singlebyte != 0 ) {
550 /** Simple code to handle the single-byte USB-MIDI protocol. */
551 spin_lock_irqsave( &ep->lock, flags );
552 if ( ep->bufWrPtr+4 > ep->bufSize ) {
553 ret = flush_midi_buffer( ep );
555 spin_unlock_irqrestore( &ep->lock, flags );
559 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | 0x0f; /* single byte */
560 ep->buf[ep->bufWrPtr++] = c;
561 ep->buf[ep->bufWrPtr++] = 0;
562 ep->buf[ep->bufWrPtr++] = 0;
563 if ( ep->bufWrPtr >= ep->bufSize ) {
564 ret = flush_midi_buffer( ep );
566 spin_unlock_irqrestore( &ep->lock, flags );
570 /** Normal USB-MIDI protocol begins here. */
572 if ( c > 0xf7 ) { /* system: Realtime messages */
573 /** Realtime messages are written IMMEDIATELY. */
574 sysrt_buf[0] = (m->mout.cableId<<4) | 0x0f;
578 spin_lock_irqsave( &ep->lock, flags );
579 ret = usb_write( ep, sysrt_buf, 4 );
580 spin_unlock_irqrestore( &ep->lock, flags );
581 /* m->mout.lastEvent = 0; */
588 m->mout.lastEvent = c;
589 m->mout.isInExclusive = 0;
590 m->mout.bufRemains = get_remains(c);
591 } else if ( c == 0xf0 ) {
592 /* m->mout.lastEvent = 0; */
593 m->mout.isInExclusive = 1;
594 m->mout.bufRemains = get_remains(c);
595 } else if ( c == 0xf7 && m->mout.isInExclusive == 1 ) {
596 /* m->mout.lastEvent = 0; */
597 m->mout.isInExclusive = 0;
598 m->mout.bufRemains = 1;
599 } else if ( c > 0xf0 ) {
600 /* m->mout.lastEvent = 0; */
601 m->mout.isInExclusive = 0;
602 m->mout.bufRemains = get_remains(c);
605 } else if ( m->mout.bufRemains == 0 && m->mout.isInExclusive == 0 ) {
606 if ( m->mout.lastEvent == 0 ) {
607 return 0; /* discard, waiting for the first event */
610 m->mout.buf[0] = m->mout.lastEvent;
612 m->mout.bufRemains = get_remains(m->mout.lastEvent)-1;
615 m->mout.buf[m->mout.bufPtr++] = c;
616 m->mout.bufRemains--;
617 if ( m->mout.bufRemains == 0 || m->mout.bufPtr >= 3) {
618 ret = put_one_midi_event(m);
625 /* ------------------------------------------------------------------------- */
627 /** Basic operation on /dev/midiXX as registered through struct file_operations.
629 * Basic contract: Used to change the current read/write position in a file.
630 * On success, the non-negative position is reported.
631 * On failure, the negative of an error code is reported.
633 * Because a MIDIStream is not a file, all seek operations are doomed to fail.
636 static loff_t usb_midi_llseek(struct file *file, loff_t offset, int origin)
638 /** Tell user you cannot seek on a PIPE-like device. **/
643 /** Basic operation on /dev/midiXX as registered through struct file_operations.
645 * Basic contract: Block until count bytes have been read or an error occurs.
649 static ssize_t usb_midi_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
651 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
652 struct midi_in_endpoint *ep = m->min.ep;
654 DECLARE_WAITQUEUE(wait, current);
656 if ( ppos != &file->f_pos ) {
659 if ( !access_ok(VERIFY_READ, buffer, count) ) {
666 add_wait_queue( &ep->wait, &wait );
672 cnt = m->min.bufRemains;
678 if ( file->f_flags & O_NONBLOCK ) {
683 __set_current_state(TASK_INTERRUPTIBLE);
685 if (signal_pending(current)) {
695 unsigned long flags; /* used to synchronize access to the endpoint */
696 spin_lock_irqsave( &ep->lock, flags );
697 for (i = 0; i < cnt; i++) {
698 if ( copy_to_user( buffer+i, m->min.buf+m->min.bufRdPtr, 1 ) ) {
703 m->min.bufRdPtr = (m->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
704 m->min.bufRemains -= 1;
706 spin_unlock_irqrestore( &ep->lock, flags );
716 remove_wait_queue( &ep->wait, &wait );
717 set_current_state(TASK_RUNNING);
723 /** Basic operation on /dev/midiXX as registered through struct file_operations.
725 * Basic Contract: Take MIDI data byte-by-byte and pass it to
726 * writeMidi() which packages MIDI data into USB-MIDI stream.
727 * Then flushMidiData() is called to ensure all bytes have been written
728 * in a timely fashion.
732 static ssize_t usb_midi_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
734 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
736 unsigned long int flags;
738 if ( ppos != &file->f_pos ) {
741 if ( !access_ok(VERIFY_READ, buffer, count) ) {
752 if (copy_from_user((unsigned char *)&c, buffer, 1)) {
757 if( midi_write(m, (int)c) ) {
767 spin_lock_irqsave( &m->mout.ep->lock, flags );
768 if ( flush_midi_buffer(m->mout.ep) < 0 ) {
771 spin_unlock_irqrestore( &m->mout.ep->lock, flags );
776 /** Basic operation on /dev/midiXX as registered through struct file_operations.
778 * Basic contract: Wait (spin) until ready to read or write on the file.
781 static unsigned int usb_midi_poll(struct file *file, struct poll_table_struct *wait)
783 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
784 struct midi_in_endpoint *iep = m->min.ep;
785 struct midi_out_endpoint *oep = m->mout.ep;
787 unsigned int mask = 0;
789 if ( file->f_mode & FMODE_READ ) {
790 poll_wait( file, &iep->wait, wait );
791 spin_lock_irqsave( &iep->lock, flags );
792 if ( m->min.bufRemains > 0 )
793 mask |= POLLIN | POLLRDNORM;
794 spin_unlock_irqrestore( &iep->lock, flags );
797 if ( file->f_mode & FMODE_WRITE ) {
798 poll_wait( file, &oep->wait, wait );
799 spin_lock_irqsave( &oep->lock, flags );
800 if ( oep->bufWrPtr < oep->bufSize )
801 mask |= POLLOUT | POLLWRNORM;
802 spin_unlock_irqrestore( &oep->lock, flags );
809 /** Basic operation on /dev/midiXX as registered through struct file_operations.
811 * Basic contract: This is always the first operation performed on the
812 * device node. If no method is defined, the open succeeds without any
813 * notification given to the module.
817 static int usb_midi_open(struct inode *inode, struct file *file)
819 int minor = MINOR(inode->i_rdev);
820 DECLARE_WAITQUEUE(wait, current);
821 struct list_head *devs, *mdevs;
822 struct usb_midi_state *s;
823 struct usb_mididev *m;
828 printk(KERN_INFO "usb-midi: Open minor= %d.\n", minor);
833 for (devs = mididevs.next; devs != &mididevs; devs = devs->next) {
834 s = list_entry(devs, struct usb_midi_state, mididev);
835 for (mdevs = s->midiDevList.next; mdevs != &s->midiDevList; mdevs = mdevs->next) {
836 m = list_entry(mdevs, struct usb_mididev, list);
837 if ( !((m->dev_midi ^ minor) & ~0xf) )
849 if ( !(m->open_mode & file->f_mode) ) {
852 if ( file->f_flags & O_NONBLOCK ) {
856 __set_current_state(TASK_INTERRUPTIBLE);
857 add_wait_queue( &open_wait, &wait );
860 __set_current_state(TASK_RUNNING);
861 remove_wait_queue( &open_wait, &wait );
862 if ( signal_pending(current) ) {
867 file->private_data = m;
868 spin_lock_irqsave( &s->lock, flags );
870 if ( !(m->open_mode & (FMODE_READ | FMODE_WRITE)) ) {
871 //FIXME: intented semantics unclear here
874 m->min.bufRemains = 0;
875 spin_lock_init(&m->min.ep->lock);
878 m->mout.bufRemains = 0;
879 m->mout.isInExclusive = 0;
880 m->mout.lastEvent = 0;
881 spin_lock_init(&m->mout.ep->lock);
884 if ( (file->f_mode & FMODE_READ) && m->min.ep != NULL ) {
885 unsigned long int flagsep;
886 spin_lock_irqsave( &m->min.ep->lock, flagsep );
887 m->min.ep->cables[m->min.cableId] = m;
888 m->min.ep->readers += 1;
891 m->min.bufRemains = 0;
892 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
894 if ( !(m->min.ep->urbSubmitted)) {
896 /* urb->dev must be reinitialized on 2.4.x kernels */
897 m->min.ep->urb->dev = m->min.ep->usbdev;
899 if ( usb_submit_urb(m->min.ep->urb) ) {
900 printk(KERN_ERR "usbmidi: Cannot submit urb for MIDI-IN\n");
902 m->min.ep->urbSubmitted = 1;
904 m->open_mode |= FMODE_READ;
908 if ( (file->f_mode & FMODE_WRITE) && m->mout.ep != NULL ) {
910 m->mout.bufRemains = 0;
911 m->mout.isInExclusive = 0;
912 m->mout.lastEvent = 0;
913 m->open_mode |= FMODE_WRITE;
917 spin_unlock_irqrestore( &s->lock, flags );
922 /** Changed to prevent extra increments to USE_COUNT. **/
928 printk(KERN_INFO "usb-midi: Open Succeeded. minor= %d.\n", minor);
931 /** Side-effect: module cannot be removed until USE_COUNT is 0. **/
932 #ifndef MOD_INC_EACH_PROBE
936 return 0; /** Success. **/
940 /** Basic operation on /dev/midiXX as registered through struct file_operations.
942 * Basic contract: Close an opened file and deallocate anything we allocated.
943 * Like open(), this can be missing. If open set file->private_data,
944 * release() must clear it.
948 static int usb_midi_release(struct inode *inode, struct file *file)
950 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
951 struct usb_midi_state *s = (struct usb_midi_state *)m->midi;
954 printk(KERN_INFO "usb-midi: Close.\n");
959 if ( m->open_mode & FMODE_WRITE ) {
960 m->open_mode &= ~FMODE_WRITE;
961 usb_unlink_urb( m->mout.ep->urb );
964 if ( m->open_mode & FMODE_READ ) {
965 unsigned long int flagsep;
966 spin_lock_irqsave( &m->min.ep->lock, flagsep );
967 m->min.ep->cables[m->min.cableId] = 0; // discard cable
968 m->min.ep->readers -= 1;
969 m->open_mode &= ~FMODE_READ;
970 if ( m->min.ep->readers == 0 &&
971 m->min.ep->urbSubmitted ) {
972 m->min.ep->urbSubmitted = 0;
973 usb_unlink_urb(m->min.ep->urb);
975 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
983 file->private_data = 0;
984 /** Sideeffect: Module cannot be removed until usecount is 0. */
985 #ifndef MOD_INC_EACH_PROBE
992 static struct file_operations usb_midi_fops = {
993 llseek: usb_midi_llseek,
995 write: usb_midi_write,
998 release: usb_midi_release,
1001 /* ------------------------------------------------------------------------- */
1003 /** Returns filled midi_in_endpoint structure or null on failure.
1007 * endPoint - An usb endpoint in the range 0 to 15.
1008 * Called by allocUsbMidiDev();
1012 static struct midi_in_endpoint *alloc_midi_in_endpoint( struct usb_device *d, int endPoint )
1014 struct midi_in_endpoint *ep;
1018 endPoint &= 0x0f; /* Silently force endPoint to lie in range 0 to 15. */
1020 pipe = usb_rcvbulkpipe( d, endPoint );
1021 bufSize = usb_maxpacket( d, pipe, usb_pipein(pipe) );
1022 /* usb_pipein() = ! usb_pipeout() = true for an in Endpoint */
1024 ep = (struct midi_in_endpoint *)kmalloc(sizeof(struct midi_in_endpoint), GFP_KERNEL);
1026 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint\n");
1029 memset( ep, 0, sizeof(struct midi_in_endpoint) );
1030 // this sets cables[] and readers to 0, too.
1031 // for (i=0; i<16; i++) ep->cables[i] = 0; // discard cable
1034 ep->endpoint = endPoint;
1036 ep->recvBuf = (unsigned char *)kmalloc(sizeof(unsigned char)*(bufSize), GFP_KERNEL);
1037 if ( !ep->recvBuf ) {
1038 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint buffer\n");
1043 ep->urb = usb_alloc_urb(0); /* no ISO */
1045 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint urb\n");
1050 FILL_BULK_URB( ep->urb, d,
1051 usb_rcvbulkpipe(d, endPoint),
1052 (unsigned char *)ep->recvBuf, bufSize,
1053 (usb_complete_t)usb_bulk_read, ep );
1055 /* ep->bufRdPtr = 0; */
1056 /* ep->bufWrPtr = 0; */
1057 /* ep->bufRemains = 0; */
1058 /* ep->urbSubmitted = 0; */
1059 ep->recvBufSize = bufSize;
1061 init_waitqueue_head(&ep->wait);
1066 static int remove_midi_in_endpoint( struct midi_in_endpoint *min )
1068 usb_unlink_urb( min->urb );
1069 usb_free_urb( min->urb );
1070 kfree( min->recvBuf );
1076 /** Returns filled midi_out_endpoint structure or null on failure.
1080 * endPoint - An usb endpoint in the range 0 to 15.
1081 * Called by allocUsbMidiDev();
1084 static struct midi_out_endpoint *alloc_midi_out_endpoint( struct usb_device *d, int endPoint )
1086 struct midi_out_endpoint *ep = NULL;
1091 pipe = usb_sndbulkpipe( d, endPoint );
1092 bufSize = usb_maxpacket( d, pipe, usb_pipeout(pipe) );
1094 ep = (struct midi_out_endpoint *)kmalloc(sizeof(struct midi_out_endpoint), GFP_KERNEL);
1096 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint\n");
1099 memset( ep, 0, sizeof(struct midi_out_endpoint) );
1101 ep->endpoint = endPoint;
1102 ep->buf = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1104 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint buffer\n");
1109 ep->urb = usb_alloc_urb(0); /* no ISO */
1111 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint urb\n");
1117 ep->bufSize = bufSize;
1118 /* ep->bufWrPtr = 0; */
1120 init_waitqueue_head(&ep->wait);
1126 static int remove_midi_out_endpoint( struct midi_out_endpoint *mout )
1128 usb_unlink_urb( mout->urb );
1129 usb_free_urb( mout->urb );
1137 /** Returns a filled usb_mididev structure, registered as a Linux MIDI device.
1139 * Returns null if memory is not available or the device cannot be registered.
1140 * Called by allocUsbMidiDev();
1143 static struct usb_mididev *allocMidiDev(
1144 struct usb_midi_state *s,
1145 struct midi_in_endpoint *min,
1146 struct midi_out_endpoint *mout,
1150 struct usb_mididev *m;
1152 m = (struct usb_mididev *)kmalloc(sizeof(struct usb_mididev), GFP_KERNEL);
1154 printk(KERN_ERR "usbmidi: no memory for midi device\n");
1158 memset(m, 0, sizeof(struct usb_mididev));
1160 if ((m->dev_midi = register_sound_midi(&usb_midi_fops, -1)) < 0) {
1161 printk(KERN_ERR "usbmidi: cannot register midi device\n");
1167 /* m->open_mode = 0; */
1171 m->min.ep->usbdev = s->usbdev;
1172 m->min.cableId = inCableId;
1174 /* m->min.bufPtr = 0; */
1175 /* m->min.bufRemains = 0; */
1179 m->mout.ep->usbdev = s->usbdev;
1180 m->mout.cableId = outCableId;
1182 /* m->mout.bufPtr = 0; */
1183 /* m->mout.bufRemains = 0; */
1184 /* m->mout.isInExclusive = 0; */
1185 /* m->mout.lastEvent = 0; */
1187 m->singlebyte = singlebyte;
1193 static void release_midi_device( struct usb_midi_state *s )
1195 struct usb_mididev *m;
1196 struct midi_in_endpoint *min;
1197 struct midi_out_endpoint *mout;
1199 if ( s->count > 0 ) {
1204 wake_up( &open_wait );
1206 while(!list_empty(&s->inEndpointList)) {
1207 min = list_entry(s->inEndpointList.next, struct midi_in_endpoint, list);
1208 list_del(&min->list);
1209 remove_midi_in_endpoint(min);
1212 while(!list_empty(&s->outEndpointList)) {
1213 mout = list_entry(s->outEndpointList.next, struct midi_out_endpoint, list);
1214 list_del(&mout->list);
1215 remove_midi_out_endpoint(mout);
1218 while(!list_empty(&s->midiDevList)) {
1219 m = list_entry(s->midiDevList.next, struct usb_mididev, list);
1230 /* ------------------------------------------------------------------------- */
1232 /** Utility routine to find a descriptor in a dump of many descriptors.
1233 * Returns start of descriptor or NULL if not found.
1234 * descStart pointer to list of interfaces.
1235 * descLength length (in bytes) of dump
1236 * after (ignored if NULL) this routine returns only descriptors after "after"
1237 * dtype (mandatory) The descriptor type.
1238 * iface (ignored if -1) returns descriptor at/following given interface
1239 * altSetting (ignored if -1) returns descriptor at/following given altSetting
1242 * Called by parseDescriptor(), find_csinterface_descriptor();
1245 static void *find_descriptor( void *descStart, unsigned int descLength, void *after, unsigned char dtype, int iface, int altSetting )
1247 unsigned char *p, *end, *next;
1248 int interfaceNumber = -1, altSet = -1;
1251 end = p + descLength;
1258 if ( p[1] == USB_DT_INTERFACE ) {
1259 if ( p[0] < USB_DT_INTERFACE_SIZE )
1261 interfaceNumber = p[2];
1264 if ( p[1] == dtype &&
1265 ( !after || ( p > (unsigned char *)after) ) &&
1266 ( ( iface == -1) || (iface == interfaceNumber) ) &&
1267 ( (altSetting == -1) || (altSetting == altSet) )) {
1275 /** Utility to find a class-specfic interface descriptor.
1276 * dsubtype is a descriptor subtype
1277 * Called by parseDescriptor();
1279 static void *find_csinterface_descriptor(void *descStart, unsigned int descLength, void *after, u8 dsubtype, int iface, int altSetting)
1283 p = find_descriptor( descStart, descLength, after, USB_DT_CS_INTERFACE, iface, altSetting );
1285 if ( p[0] >= 3 && p[2] == dsubtype )
1287 p = find_descriptor( descStart, descLength, p, USB_DT_CS_INTERFACE,
1288 iface, altSetting );
1294 /** The magic of making a new usb_midi_device from config happens here.
1296 * The caller is responsible for free-ing this return value (if not NULL).
1299 static struct usb_midi_device *parse_descriptor( struct usb_device *d, unsigned char *buffer, int bufSize, unsigned int ifnum , unsigned int altSetting, int quirks)
1301 struct usb_midi_device *u;
1304 unsigned char *next;
1307 unsigned long longBits;
1308 int pins, nbytes, offset, shift, jack;
1309 #ifdef HAVE_JACK_STRINGS
1310 /** Jacks can have associated names. **/
1311 unsigned char jack2string[256];
1315 /* find audiocontrol interface */
1316 p1 = find_csinterface_descriptor( buffer, bufSize, NULL,
1317 MS_HEADER, ifnum, altSetting);
1323 if ( p1[0] < MS_HEADER_LENGTH ) {
1327 /* Assume success. Since the device corresponds to USB-MIDI spec, we assume
1328 that the rest of the USB 2.0 spec is obeyed. */
1330 u = (struct usb_midi_device *)kmalloc( sizeof(struct usb_midi_device), GFP_KERNEL );
1335 u->idVendor = d->descriptor.idVendor;
1336 u->idProduct = d->descriptor.idProduct;
1337 u->interface = ifnum;
1338 u->altSetting = altSetting;
1339 u->in[0].endpoint = -1;
1340 u->in[0].cableId = -1;
1341 u->out[0].endpoint = -1;
1342 u->out[0].cableId = -1;
1345 printk(KERN_INFO "usb-midi: Found MIDIStreaming device corresponding to Release %d.%02d of spec.\n",
1346 (p1[4] >> 4) * 10 + (p1[4] & 0x0f ),
1347 (p1[3] >> 4) * 10 + (p1[3] & 0x0f )
1350 length = p1[5] | (p1[6] << 8);
1352 #ifdef HAVE_JACK_STRINGS
1353 memset(jack2string, 0, sizeof(unsigned char) * 256);
1357 for (p2 = p1 + p1[0]; length > 0; p2 = next) {
1361 if (p2[0] < 2 ) break;
1362 if (p2[1] != USB_DT_CS_INTERFACE) break;
1363 if (p2[2] == MIDI_IN_JACK && p2[0] >= 6 ) {
1365 #ifdef HAVE_JACK_STRINGS
1366 jack2string[jack] = p2[5];
1368 printk(KERN_INFO "usb-midi: Found IN Jack 0x%02x %s\n",
1369 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL" );
1370 } else if ( p2[2] == MIDI_OUT_JACK && p2[0] >= 6) {
1372 if ( p2[0] < (6 + 2 * pins) ) continue;
1374 #ifdef HAVE_JACK_STRINGS
1375 jack2string[jack] = p2[5 + 2 * pins];
1377 printk(KERN_INFO "usb-midi: Found OUT Jack 0x%02x %s, %d pins\n",
1378 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL", pins );
1379 } else if ( p2[2] == ELEMENT_DESCRIPTOR && p2[0] >= 10) {
1381 if ( p2[0] < (9 + 2 * pins ) ) continue;
1382 nbytes = p2[8 + 2 * pins ];
1383 if ( p2[0] < (10 + 2 * pins + nbytes) ) continue;
1385 for ( offset = 0, shift = 0; offset < nbytes && offset < 8; offset ++, shift += 8) {
1386 longBits |= ((long)(p2[9 + 2 * pins + offset])) << shift;
1389 #ifdef HAVE_JACK_STRINGS
1390 jack2string[jack] = p2[9 + 2 * pins + nbytes];
1392 printk(KERN_INFO "usb-midi: Found ELEMENT 0x%02x, %d/%d pins in/out, bits: 0x%016lx\n",
1393 jack, pins, (int)(p2[5 + 2 * pins]), (long)longBits );
1404 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1405 ifnum, altSetting ); p1; p1 = next ) {
1406 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1407 ifnum, altSetting );
1408 p2 = find_descriptor(buffer, bufSize, p1, USB_DT_CS_ENDPOINT,
1409 ifnum, altSetting );
1411 if ( p2 && next && ( p2 > next ) )
1414 if ( p1[0] < 9 || !p2 || p2[0] < 4 ) continue;
1416 if ( (p1[2] & 0x80) == 0x80 ) {
1418 pins = p2[3]; /* not pins -- actually "cables" */
1421 u->in[iep].endpoint = p1[2];
1422 u->in[iep].cableId = ( 1 << pins ) - 1;
1423 if ( u->in[iep].cableId ) iep ++;
1425 u->in[iep].endpoint = -1;
1426 u->in[iep].cableId = -1;
1431 pins = p2[3]; /* not pins -- actually "cables" */
1434 u->out[oep].endpoint = p1[2];
1435 u->out[oep].cableId = ( 1 << pins ) - 1;
1436 if ( u->out[oep].cableId ) oep ++;
1438 u->out[oep].endpoint = -1;
1439 u->out[oep].cableId = -1;
1445 } else if (quirks==1) {
1447 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1448 ifnum, altSetting ); p1; p1 = next ) {
1449 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1450 ifnum, altSetting );
1452 if ( p1[0] < 7 ) continue;
1454 if ( (p1[2] & 0x80) == 0x80 ) {
1459 u->in[iep].endpoint = p1[2];
1460 u->in[iep].cableId = ( 1 << pins ) - 1;
1461 if ( u->in[iep].cableId ) iep ++;
1463 u->in[iep].endpoint = -1;
1464 u->in[iep].cableId = -1;
1472 u->out[oep].endpoint = p1[2];
1473 u->out[oep].cableId = ( 1 << pins ) - 1;
1474 if ( u->out[oep].cableId ) oep ++;
1476 u->out[oep].endpoint = -1;
1477 u->out[oep].cableId = -1;
1485 if ( !iep && ! oep ) {
1496 /* ------------------------------------------------------------------------- */
1498 /** Returns number between 0 and 16.
1501 static int on_bits( unsigned short v )
1506 for ( i=0 ; i<16 ; i++ ) {
1507 if ( v & (1<<i) ) ret++;
1514 /** USB-device will be interrogated for altSetting.
1516 * Returns negative on error.
1517 * Called by allocUsbMidiDev();
1521 static int get_alt_setting( struct usb_device *d, int ifnum )
1524 struct usb_interface_descriptor *interface;
1525 struct usb_endpoint_descriptor *ep;
1529 alts = d->actconfig->interface[ifnum].num_altsetting;
1531 for ( alt=0 ; alt<alts ; alt++ ) {
1532 interface = &d->actconfig->interface[ifnum].altsetting[alt];
1536 for ( i=0 ; i<interface->bNumEndpoints ; i++ ) {
1537 ep = &interface->endpoint[i];
1538 if ( (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ) {
1541 if ( (ep->bEndpointAddress & USB_DIR_IN) && epin < 0 ) {
1543 } else if ( epout < 0 ) {
1546 if ( epin >= 0 && epout >= 0 ) {
1556 /* ------------------------------------------------------------------------- */
1559 /** Returns 0 if successful in allocating and registering internal structures.
1560 * Returns negative on failure.
1561 * Calls allocMidiDev which additionally registers /dev/midiXX devices.
1562 * Writes messages on success to indicate which /dev/midiXX is which physical
1566 static int alloc_usb_midi_device( struct usb_device *d, struct usb_midi_state *s, struct usb_midi_device *u )
1568 struct usb_mididev **mdevs=NULL;
1569 struct midi_in_endpoint *mins[15], *min;
1570 struct midi_out_endpoint *mouts[15], *mout;
1571 int inDevs=0, outDevs=0;
1572 int inEndpoints=0, outEndpoints=0;
1573 int inEndpoint, outEndpoint;
1574 int inCableId, outCableId;
1579 /* Obtain altSetting or die.. */
1580 alt = u->altSetting;
1582 alt = get_alt_setting( d, u->interface );
1584 if ( alt < 0 ) { return -ENXIO; }
1586 /* Configure interface */
1587 if ( usb_set_interface( d, u->interface, alt ) < 0 ) {
1591 for ( i = 0 ; i < 15 ; i++ ) {
1596 /* Begin Allocation */
1597 while( inEndpoints < 15
1598 && inDevs < maxdevices
1599 && u->in[inEndpoints].cableId >= 0 ) {
1600 inDevs += on_bits((unsigned short)u->in[inEndpoints].cableId);
1601 mins[inEndpoints] = alloc_midi_in_endpoint( d, u->in[inEndpoints].endpoint );
1602 if ( mins[inEndpoints] == NULL ) { goto error_end; }
1606 while( outEndpoints < 15
1607 && outDevs < maxdevices
1608 && u->out[outEndpoints].cableId >= 0 ) {
1609 outDevs += on_bits((unsigned short)u->out[outEndpoints].cableId);
1610 mouts[outEndpoints] = alloc_midi_out_endpoint( d, u->out[outEndpoints].endpoint );
1611 if ( mouts[outEndpoints] == NULL ) { goto error_end; }
1615 devices = inDevs > outDevs ? inDevs : outDevs;
1616 devices = maxdevices > devices ? devices : maxdevices;
1618 /* obtain space for device name (iProduct) if not known. */
1619 if ( ! u->deviceName ) {
1620 mdevs = (struct usb_mididev **)
1621 kmalloc(sizeof(struct usb_mididevs *)*devices
1622 + sizeof(char) * 256, GFP_KERNEL);
1624 mdevs = (struct usb_mididev **)
1625 kmalloc(sizeof(struct usb_mididevs *)*devices, GFP_KERNEL);
1633 for ( i=0 ; i<devices ; i++ ) {
1637 /* obtain device name (iProduct) if not known. */
1638 if ( ! u->deviceName ) {
1639 u->deviceName = (char *) (mdevs + devices);
1640 if ( ! d->have_langid && d->descriptor.iProduct) {
1641 alt = usb_get_string(d, 0, 0, u->deviceName, 250);
1643 printk(KERN_INFO "error getting string descriptor 0 (error=%d)\n", alt);
1644 } else if (u->deviceName[0] < 4) {
1645 printk(KERN_INFO "string descriptor 0 too short (length = %d)\n", alt);
1647 printk(KERN_INFO "string descriptor 0 found (length = %d)\n", alt);
1648 for(; alt >= 4; alt -= 2) {
1649 i = u->deviceName[alt-2] | (u->deviceName[alt-1]<< 8);
1650 printk(KERN_INFO "usb-midi: langid(%d) 0x%04x\n",
1652 if ( ( ( i ^ ulangid ) & 0xff ) == 0 ) {
1654 d->string_langid = i;
1655 printk(KERN_INFO "usb-midi: langid(match) 0x%04x\n", i);
1662 u->deviceName[0] = (char) 0;
1663 if (d->descriptor.iProduct) {
1664 printk(KERN_INFO "usb-midi: fetchString(%d)\n", d->descriptor.iProduct);
1665 alt = usb_string(d, d->descriptor.iProduct, u->deviceName, 255);
1667 u->deviceName[0] = (char) 0;
1669 printk(KERN_INFO "usb-midi: fetchString = %d\n", alt);
1672 if ( !u->deviceName[0] ) {
1673 if ( d->descriptor.idVendor == USB_VENDOR_ID_ROLAND ) {
1674 strcpy(u->deviceName, "Unknown Roland");
1675 } else if ( d->descriptor.idVendor == USB_VENDOR_ID_STEINBERG ) {
1676 strcpy(u->deviceName, "Unknown Steinberg");
1677 } else if ( d->descriptor.idVendor == USB_VENDOR_ID_YAMAHA ) {
1678 strcpy(u->deviceName, "Unknown Yamaha");
1680 strcpy(u->deviceName, "Unknown");
1685 inEndpoint = 0; inCableId = -1;
1686 outEndpoint = 0; outCableId = -1;
1688 for ( i=0 ; i<devices ; i++ ) {
1689 for ( inCableId ++ ;
1692 && !(u->in[inEndpoint].cableId & (1<<inCableId)) ;
1694 if ( inCableId >= 16 ) {
1699 min = mins[inEndpoint];
1700 for ( outCableId ++ ;
1702 && mouts[outEndpoint]
1703 && !(u->out[outEndpoint].cableId & (1<<outCableId)) ;
1705 if ( outCableId >= 16 ) {
1710 mout = mouts[outEndpoint];
1712 mdevs[i] = allocMidiDev( s, min, mout, inCableId, outCableId );
1713 if ( mdevs[i] == NULL ) { goto error_end; }
1718 for ( i=0 ; i<devices ; i++ ) {
1719 list_add_tail( &mdevs[i]->list, &s->midiDevList );
1721 for ( i=0 ; i<inEndpoints ; i++ ) {
1722 list_add_tail( &mins[i]->list, &s->inEndpointList );
1724 for ( i=0 ; i<outEndpoints ; i++ ) {
1725 list_add_tail( &mouts[i]->list, &s->outEndpointList );
1728 printk(KERN_INFO "usbmidi: found [ %s ] (0x%04x:0x%04x), attached:\n", u->deviceName, u->idVendor, u->idProduct );
1729 for ( i=0 ; i<devices ; i++ ) {
1730 int dm = (mdevs[i]->dev_midi-2)>>4;
1731 if ( mdevs[i]->mout.ep != NULL && mdevs[i]->min.ep != NULL ) {
1732 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%2d) out (ep:%02x cid:%2d bufsiz:%2d)\n",
1734 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize,
1735 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1736 } else if ( mdevs[i]->min.ep != NULL ) {
1737 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%02d)\n",
1739 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize);
1740 } else if ( mdevs[i]->mout.ep != NULL ) {
1741 printk(KERN_INFO "usbmidi: /dev/midi%02d: out (ep:%02x cid:%2d bufsiz:%02d)\n",
1743 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1751 if ( mdevs != NULL && devices > 0 ) {
1752 for ( i=0 ; i<devices ; i++ ) {
1753 if ( mdevs[i] != NULL ) {
1754 unregister_sound_midi( mdevs[i]->dev_midi );
1761 for ( i=0 ; i<15 ; i++ ) {
1762 if ( mins[i] != NULL ) {
1763 remove_midi_in_endpoint( mins[i] );
1765 if ( mouts[i] != NULL ) {
1766 remove_midi_out_endpoint( mouts[i] );
1773 /* ------------------------------------------------------------------------- */
1775 /** Attempt to scan YAMAHA's device descriptor and detect correct values of
1777 * Return 0 on succes, negative on failure.
1778 * Called by usb_midi_probe();
1781 static int detect_yamaha_device( struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1783 struct usb_config_descriptor *c = d->actconfig;
1784 struct usb_interface_descriptor *interface;
1785 struct usb_midi_device *u;
1786 unsigned char buf[USB_DT_CONFIG_SIZE], *buffer;
1792 if (d->descriptor.idVendor != USB_VENDOR_ID_YAMAHA) {
1796 for ( i=0 ; i < c->interface[ifnum].num_altsetting; i++ ) {
1797 interface = c->interface[ifnum].altsetting + i;
1799 if ( interface->bInterfaceClass != 255 ||
1800 interface->bInterfaceSubClass != 0 )
1808 printk(KERN_INFO "usb-midi: Found YAMAHA USB-MIDI device on dev %04x:%04x, iface %d\n",
1809 d->descriptor.idVendor, d->descriptor.idProduct, ifnum);
1811 for ( i=0 ; i < d->descriptor.bNumConfigurations ; i++ ) {
1812 if ( d->config+i == c ) goto configfound;
1815 printk(KERN_INFO "usb-midi: Config not found.\n");
1821 /* this may not be necessary. */
1822 if ( usb_set_configuration( d, c->bConfigurationValue ) < 0 ) {
1823 printk(KERN_INFO "usb-midi: Could not set config.\n");
1827 ret = usb_get_descriptor( d, USB_DT_CONFIG, i, buf, USB_DT_CONFIG_SIZE );
1829 printk(KERN_INFO "usb-midi: Could not get config (error=%d).\n", ret);
1832 if ( buf[1] != USB_DT_CONFIG || buf[0] < USB_DT_CONFIG_SIZE ) {
1833 printk(KERN_INFO "usb-midi: config not as expected.\n");
1836 bufSize = buf[2] | buf[3]<<8;
1837 buffer = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1839 printk(KERN_INFO "usb-midi: Could not allocate memory.\n");
1842 ret = usb_get_descriptor( d, USB_DT_CONFIG, i, buffer, bufSize );
1844 printk(KERN_INFO "usb-midi: Could not get full config (error=%d).\n", ret);
1849 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 1);
1855 ret = alloc_usb_midi_device( d, s, u );
1863 /** Scan table of known devices which are only partially compliant with
1864 * the MIDIStreaming specification.
1865 * Called by usb_midi_probe();
1869 static int detect_vendor_specific_device( struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s )
1871 struct usb_midi_device *u;
1875 for ( i=0; i<VENDOR_SPECIFIC_USB_MIDI_DEVICES ; i++ ) {
1876 u=&(usb_midi_devices[i]);
1878 if ( d->descriptor.idVendor != u->idVendor ||
1879 d->descriptor.idProduct != u->idProduct ||
1880 ifnum != u->interface )
1883 ret = alloc_usb_midi_device( d, s, u );
1891 /** Attempt to match any config of an interface to a MIDISTREAMING interface.
1892 * Returns 0 on success, negative on failure.
1893 * Called by usb_midi_probe();
1895 static int detect_midi_subclass(struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1897 struct usb_config_descriptor *c = d->actconfig;
1898 struct usb_interface_descriptor *interface;
1899 struct usb_midi_device *u;
1900 unsigned char buf[USB_DT_CONFIG_SIZE], *buffer;
1906 for ( i=0 ; i < c->interface[ifnum].num_altsetting; i++ ) {
1907 interface = c->interface[ifnum].altsetting + i;
1909 if ( interface->bInterfaceClass != USB_CLASS_AUDIO ||
1910 interface->bInterfaceSubClass != USB_SUBCLASS_MIDISTREAMING )
1918 printk(KERN_INFO "usb-midi: Found MIDISTREAMING on dev %04x:%04x, iface %d\n",
1919 d->descriptor.idVendor, d->descriptor.idProduct, ifnum);
1921 for ( i=0 ; i < d->descriptor.bNumConfigurations ; i++ ) {
1922 if ( d->config+i == c ) goto configfound;
1925 printk(KERN_INFO "usb-midi: Config not found.\n");
1931 /* this may not be necessary. */
1932 if ( usb_set_configuration( d, c->bConfigurationValue ) < 0 ) {
1933 printk(KERN_INFO "usb-midi: Could not set config.\n");
1937 /* From USB Spec v2.0, Section 9.5.
1938 If the class or vendor specific descriptors use the same format
1939 as standard descriptors (e.g., start with a length byte and
1940 followed by a type byte), they must be returned interleaved with
1941 standard descriptors in the configuration information returned by
1942 a GetDescriptor(Configuration) request. In this case, the class
1943 or vendor-specific descriptors must follow a related standard
1944 descriptor they modify or extend.
1947 ret = usb_get_descriptor( d, USB_DT_CONFIG, i, buf, USB_DT_CONFIG_SIZE );
1949 printk(KERN_INFO "usb-midi: Could not get config (error=%d).\n", ret);
1952 if ( buf[1] != USB_DT_CONFIG || buf[0] < USB_DT_CONFIG_SIZE ) {
1953 printk(KERN_INFO "usb-midi: config not as expected.\n");
1956 bufSize = buf[2] | buf[3]<<8;
1957 buffer = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1959 printk(KERN_INFO "usb-midi: Could not allocate memory.\n");
1962 ret = usb_get_descriptor( d, USB_DT_CONFIG, i, buffer, bufSize );
1964 printk(KERN_INFO "usb-midi: Could not get full config (error=%d).\n", ret);
1969 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 0);
1975 ret = alloc_usb_midi_device( d, s, u );
1983 /** When user has requested a specific device, match it exactly.
1985 * Uses uvendor, uproduct, uinterface, ualt, umin, umout and ucable.
1986 * Called by usb_midi_probe();
1989 static int detect_by_hand(struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1991 struct usb_midi_device u;
1993 if ( d->descriptor.idVendor != uvendor ||
1994 d->descriptor.idProduct != uproduct ||
1995 ifnum != uinterface ) {
1999 if ( ualt < 0 ) { ualt = -1; }
2001 if ( umin < 0 || umin > 15 ) { umin = 0x01 | USB_DIR_IN; }
2002 if ( umout < 0 || umout > 15 ) { umout = 0x01; }
2003 if ( ucable < 0 || ucable > 15 ) { ucable = 0; }
2005 u.deviceName = 0; /* A flag for alloc_usb_midi_device to get device name
2007 u.idVendor = uvendor;
2008 u.idProduct = uproduct;
2009 u.interface = uinterface;
2010 u.altSetting = ualt;
2012 u.in[0].endpoint = umin;
2013 u.in[0].cableId = (1<<ucable);
2015 u.out[0].endpoint = umout;
2016 u.out[0].cableId = (1<<ucable);
2018 return alloc_usb_midi_device( d, s, &u );
2023 /* ------------------------------------------------------------------------- */
2025 static void *usb_midi_probe(struct usb_device *dev, unsigned int ifnum,
2026 const struct usb_device_id *id)
2028 struct usb_midi_state *s;
2030 s = (struct usb_midi_state *)kmalloc(sizeof(struct usb_midi_state), GFP_KERNEL);
2031 if ( !s ) { return NULL; }
2033 memset( s, 0, sizeof(struct usb_midi_state) );
2034 INIT_LIST_HEAD(&s->midiDevList);
2035 INIT_LIST_HEAD(&s->inEndpointList);
2036 INIT_LIST_HEAD(&s->outEndpointList);
2039 spin_lock_init(&s->lock);
2042 detect_by_hand( dev, ifnum, s ) &&
2043 detect_midi_subclass( dev, ifnum, s ) &&
2044 detect_vendor_specific_device( dev, ifnum, s ) &&
2045 detect_yamaha_device( dev, ifnum, s) ) {
2051 list_add_tail(&s->mididev, &mididevs);
2054 #ifdef MOD_INC_EACH_PROBE
2062 static void usb_midi_disconnect(struct usb_device *dev, void *ptr)
2064 struct usb_midi_state *s = (struct usb_midi_state *)ptr;
2065 struct list_head *list;
2066 struct usb_mididev *m;
2068 if ( s == (struct usb_midi_state *)-1 ) {
2075 list_del(&s->mididev);
2076 INIT_LIST_HEAD(&s->mididev);
2079 for ( list = s->midiDevList.next; list != &s->midiDevList; list = list->next ) {
2080 m = list_entry(list, struct usb_mididev, list);
2081 wake_up(&(m->min.ep->wait));
2082 wake_up(&(m->mout.ep->wait));
2083 if ( m->dev_midi >= 0 ) {
2084 unregister_sound_midi(m->dev_midi);
2088 release_midi_device(s);
2089 wake_up(&open_wait);
2090 #ifdef MOD_INC_EACH_PROBE
2099 static struct usb_driver usb_midi_driver = {
2101 probe: usb_midi_probe,
2102 disconnect: usb_midi_disconnect,
2103 id_table: NULL, /* check all devices */
2104 driver_list: LIST_HEAD_INIT(usb_midi_driver.driver_list)
2107 /* ------------------------------------------------------------------------- */
2109 int __init usb_midi_init(void)
2111 if ( usb_register(&usb_midi_driver) < 0 )
2118 void __exit usb_midi_exit(void)
2120 usb_deregister(&usb_midi_driver);
2123 module_init(usb_midi_init) ;
2124 module_exit(usb_midi_exit) ;
2126 #ifdef HAVE_ALSA_SUPPORT
2127 #define SNDRV_MAIN_OBJECT_FILE
2128 #include "../../include/driver.h"
2129 #include "../../include/control.h"
2130 #include "../../include/info.h"
2131 #include "../../include/cs46xx.h"
2133 /* ------------------------------------------------------------------------- */
2135 static int snd_usbmidi_input_close(snd_rawmidi_substream_t * substream)
2140 static int snd_usbmidi_input_open(snd_rawmidi_substream_t * substream )
2145 static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t * substream, int up)
2151 /* ------------------------------------------------------------------------- */
2153 static int snd_usbmidi_output_close(snd_rawmidi_substream_t * substream)
2158 static int snd_usbmidi_output_open(snd_rawmidi_substream_t * substream)
2163 static void snd_usb_midi_output_trigger(snd_rawmidi_substream_t * substream,
2169 /* ------------------------------------------------------------------------- */
2171 static snd_rawmidi_ops_t snd_usbmidi_output =
2173 open: snd_usbmidi_output_open,
2174 close: snd_usbmidi_output_close,
2175 trigger: snd_usbmidi_output_trigger,
2177 static snd_rawmidi_ops_t snd_usbmidi_input =
2179 open: snd_usbmidi_input_open,
2180 close: snd_usbmidi_input_close,
2181 trigger: snd_usbmidi_input_trigger,
2184 int snd_usbmidi_midi(cs46xx_t *chip, int device, snd_rawmidi_t **rrawmidi)
2186 snd_rawmidi_t *rmidi;
2191 if ((err = snd_rawmidi_new(chip->card, "USB-MIDI", device, 1, 1, &rmidi)) < 0)
2193 strcpy(rmidi->name, "USB-MIDI");
2195 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output );
2196 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input );
2198 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
2200 rmidi->private_data = chip;
2201 chip->rmidi = rmidi;
2208 int snd_usbmidi_create( snd_card_t * card,
2209 struct pci_dev * pci,
2210 usbmidi_t ** rchip )
2214 snd_region_t *region;
2215 static snd_device_opt_t ops = {
2216 dev_free: snd_usbmidi_dev_free,
2220 chip = snd_magic_kcalloc( usbmidi_t, 0, GFP_KERNEL );
2225 EXPORT_SYMBOL(snd_usbmidi_create);
2226 EXPORT_SYMBOL(snd_usbmidi_midi);
2227 #endif /* HAVE_ALSA_SUPPORT */