1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include <linux/kernel.h>
39#include <linux/types.h>
40#include <linux/bitops.h>
41#include <linux/interrupt.h>
42#include <linux/spinlock.h>
43#include <linux/string.h>
44#include <linux/init.h>
45#include <linux/slab.h>
46#include <linux/timer.h>
47#include <linux/usb.h>
48#include <sound/core.h>
49#include <sound/rawmidi.h>
50#include <sound/asequencer.h>
51#include "usbaudio.h"
52
53
54
55
56
57
58
59
60
61
62
63#define ERROR_DELAY_JIFFIES (HZ / 10)
64
65
66MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
67MODULE_DESCRIPTION("USB Audio/MIDI helper module");
68MODULE_LICENSE("Dual BSD/GPL");
69
70
71struct usb_ms_header_descriptor {
72 __u8 bLength;
73 __u8 bDescriptorType;
74 __u8 bDescriptorSubtype;
75 __u8 bcdMSC[2];
76 __le16 wTotalLength;
77} __attribute__ ((packed));
78
79struct usb_ms_endpoint_descriptor {
80 __u8 bLength;
81 __u8 bDescriptorType;
82 __u8 bDescriptorSubtype;
83 __u8 bNumEmbMIDIJack;
84 __u8 baAssocJackID[0];
85} __attribute__ ((packed));
86
87struct snd_usb_midi_in_endpoint;
88struct snd_usb_midi_out_endpoint;
89struct snd_usb_midi_endpoint;
90
91struct usb_protocol_ops {
92 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
93 void (*output)(struct snd_usb_midi_out_endpoint*);
94 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
95 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
96 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
97};
98
99struct snd_usb_midi {
100 struct snd_usb_audio *chip;
101 struct usb_interface *iface;
102 const struct snd_usb_audio_quirk *quirk;
103 struct snd_rawmidi *rmidi;
104 struct usb_protocol_ops* usb_protocol_ops;
105 struct list_head list;
106 struct timer_list error_timer;
107 spinlock_t disc_lock;
108
109 struct snd_usb_midi_endpoint {
110 struct snd_usb_midi_out_endpoint *out;
111 struct snd_usb_midi_in_endpoint *in;
112 } endpoints[MIDI_MAX_ENDPOINTS];
113 unsigned long input_triggered;
114 unsigned char disconnected;
115};
116
117struct snd_usb_midi_out_endpoint {
118 struct snd_usb_midi* umidi;
119 struct urb* urb;
120 int urb_active;
121 int max_transfer;
122 struct tasklet_struct tasklet;
123
124 spinlock_t buffer_lock;
125
126 struct usbmidi_out_port {
127 struct snd_usb_midi_out_endpoint* ep;
128 struct snd_rawmidi_substream *substream;
129 int active;
130 uint8_t cable;
131 uint8_t state;
132#define STATE_UNKNOWN 0
133#define STATE_1PARAM 1
134#define STATE_2PARAM_1 2
135#define STATE_2PARAM_2 3
136#define STATE_SYSEX_0 4
137#define STATE_SYSEX_1 5
138#define STATE_SYSEX_2 6
139 uint8_t data[2];
140 } ports[0x10];
141 int current_port;
142};
143
144struct snd_usb_midi_in_endpoint {
145 struct snd_usb_midi* umidi;
146 struct urb* urb;
147 struct usbmidi_in_port {
148 struct snd_rawmidi_substream *substream;
149 u8 running_status_length;
150 } ports[0x10];
151 u8 seen_f5;
152 u8 error_resubmit;
153 int current_port;
154};
155
156static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
157
158static const uint8_t snd_usbmidi_cin_length[] = {
159 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
160};
161
162
163
164
165static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
166{
167 int err = usb_submit_urb(urb, flags);
168 if (err < 0 && err != -ENODEV)
169 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
170 return err;
171}
172
173
174
175
176static int snd_usbmidi_urb_error(int status)
177{
178 switch (status) {
179
180 case -ENOENT:
181 case -ECONNRESET:
182 case -ESHUTDOWN:
183 case -ENODEV:
184 return -ENODEV;
185
186 case -EPROTO:
187 case -ETIME:
188 case -EILSEQ:
189 return -EIO;
190 default:
191 snd_printk(KERN_ERR "urb status %d\n", status);
192 return 0;
193 }
194}
195
196
197
198
199static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
200 uint8_t* data, int length)
201{
202 struct usbmidi_in_port* port = &ep->ports[portidx];
203
204 if (!port->substream) {
205 snd_printd("unexpected port %d!\n", portidx);
206 return;
207 }
208 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
209 return;
210 snd_rawmidi_receive(port->substream, data, length);
211}
212
213#ifdef DUMP_PACKETS
214static void dump_urb(const char *type, const u8 *data, int length)
215{
216 snd_printk(KERN_DEBUG "%s packet: [", type);
217 for (; length > 0; ++data, --length)
218 printk(" %02x", *data);
219 printk(" ]\n");
220}
221#else
222#define dump_urb(type, data, length)
223#endif
224
225
226
227
228static void snd_usbmidi_in_urb_complete(struct urb* urb)
229{
230 struct snd_usb_midi_in_endpoint* ep = urb->context;
231
232 if (urb->status == 0) {
233 dump_urb("received", urb->transfer_buffer, urb->actual_length);
234 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
235 urb->actual_length);
236 } else {
237 int err = snd_usbmidi_urb_error(urb->status);
238 if (err < 0) {
239 if (err != -ENODEV) {
240 ep->error_resubmit = 1;
241 mod_timer(&ep->umidi->error_timer,
242 jiffies + ERROR_DELAY_JIFFIES);
243 }
244 return;
245 }
246 }
247
248 urb->dev = ep->umidi->chip->dev;
249 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
250}
251
252static void snd_usbmidi_out_urb_complete(struct urb* urb)
253{
254 struct snd_usb_midi_out_endpoint* ep = urb->context;
255
256 spin_lock(&ep->buffer_lock);
257 ep->urb_active = 0;
258 spin_unlock(&ep->buffer_lock);
259 if (urb->status < 0) {
260 int err = snd_usbmidi_urb_error(urb->status);
261 if (err < 0) {
262 if (err != -ENODEV)
263 mod_timer(&ep->umidi->error_timer,
264 jiffies + ERROR_DELAY_JIFFIES);
265 return;
266 }
267 }
268 snd_usbmidi_do_output(ep);
269}
270
271
272
273
274
275static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
276{
277 struct urb* urb = ep->urb;
278 unsigned long flags;
279
280 spin_lock_irqsave(&ep->buffer_lock, flags);
281 if (ep->urb_active || ep->umidi->chip->shutdown) {
282 spin_unlock_irqrestore(&ep->buffer_lock, flags);
283 return;
284 }
285
286 urb->transfer_buffer_length = 0;
287 ep->umidi->usb_protocol_ops->output(ep);
288
289 if (urb->transfer_buffer_length > 0) {
290 dump_urb("sending", urb->transfer_buffer,
291 urb->transfer_buffer_length);
292 urb->dev = ep->umidi->chip->dev;
293 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
294 }
295 spin_unlock_irqrestore(&ep->buffer_lock, flags);
296}
297
298static void snd_usbmidi_out_tasklet(unsigned long data)
299{
300 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
301
302 snd_usbmidi_do_output(ep);
303}
304
305
306static void snd_usbmidi_error_timer(unsigned long data)
307{
308 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
309 int i;
310
311 spin_lock(&umidi->disc_lock);
312 if (umidi->disconnected) {
313 spin_unlock(&umidi->disc_lock);
314 return;
315 }
316 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
317 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
318 if (in && in->error_resubmit) {
319 in->error_resubmit = 0;
320 in->urb->dev = umidi->chip->dev;
321 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
322 }
323 if (umidi->endpoints[i].out)
324 snd_usbmidi_do_output(umidi->endpoints[i].out);
325 }
326 spin_unlock(&umidi->disc_lock);
327}
328
329
330static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
331 const void *data, int len)
332{
333 int err;
334 void *buf = kmemdup(data, len, GFP_KERNEL);
335 if (!buf)
336 return -ENOMEM;
337 dump_urb("sending", buf, len);
338 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len,
339 NULL, 250);
340 kfree(buf);
341 return err;
342}
343
344
345
346
347
348
349
350static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
351 uint8_t* buffer, int buffer_length)
352{
353 int i;
354
355 for (i = 0; i + 3 < buffer_length; i += 4)
356 if (buffer[i] != 0) {
357 int cable = buffer[i] >> 4;
358 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
359 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
360 }
361}
362
363static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
364 uint8_t* buffer, int buffer_length)
365{
366 int i;
367
368 for (i = 0; i + 3 < buffer_length; i += 4)
369 if (buffer[i + 3] != 0) {
370 int port = buffer[i + 3] >> 4;
371 int length = buffer[i + 3] & 3;
372 snd_usbmidi_input_data(ep, port, &buffer[i], length);
373 }
374}
375
376
377
378
379
380static void snd_usbmidi_maudio_broken_running_status_input(
381 struct snd_usb_midi_in_endpoint* ep,
382 uint8_t* buffer, int buffer_length)
383{
384 int i;
385
386 for (i = 0; i + 3 < buffer_length; i += 4)
387 if (buffer[i] != 0) {
388 int cable = buffer[i] >> 4;
389 u8 cin = buffer[i] & 0x0f;
390 struct usbmidi_in_port *port = &ep->ports[cable];
391 int length;
392
393 length = snd_usbmidi_cin_length[cin];
394 if (cin == 0xf && buffer[i + 1] >= 0xf8)
395 ;
396 else if (cin >= 0x8 && cin <= 0xe)
397
398 port->running_status_length = length - 1;
399 else if (cin == 0x4 &&
400 port->running_status_length != 0 &&
401 buffer[i + 1] < 0x80)
402
403 length = port->running_status_length;
404 else
405
406
407
408
409
410
411 port->running_status_length = 0;
412 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
413 }
414}
415
416
417
418
419
420static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
421 uint8_t *buffer, int buffer_length)
422{
423 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
424 snd_usbmidi_standard_input(ep, buffer, buffer_length);
425 else
426 snd_usbmidi_input_data(ep, buffer[0] >> 4,
427 &buffer[1], buffer_length - 1);
428}
429
430
431
432
433static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
434 uint8_t p1, uint8_t p2, uint8_t p3)
435{
436
437 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
438 buf[0] = p0;
439 buf[1] = p1;
440 buf[2] = p2;
441 buf[3] = p3;
442 urb->transfer_buffer_length += 4;
443}
444
445
446
447
448static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
449 uint8_t p1, uint8_t p2, uint8_t p3)
450{
451
452 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
453 buf[0] = p1;
454 buf[1] = p2;
455 buf[2] = p3;
456 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
457 urb->transfer_buffer_length += 4;
458}
459
460
461
462
463static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
464 uint8_t b, struct urb* urb)
465{
466 uint8_t p0 = port->cable;
467 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
468 port->ep->umidi->usb_protocol_ops->output_packet;
469
470 if (b >= 0xf8) {
471 output_packet(urb, p0 | 0x0f, b, 0, 0);
472 } else if (b >= 0xf0) {
473 switch (b) {
474 case 0xf0:
475 port->data[0] = b;
476 port->state = STATE_SYSEX_1;
477 break;
478 case 0xf1:
479 case 0xf3:
480 port->data[0] = b;
481 port->state = STATE_1PARAM;
482 break;
483 case 0xf2:
484 port->data[0] = b;
485 port->state = STATE_2PARAM_1;
486 break;
487 case 0xf4:
488 case 0xf5:
489 port->state = STATE_UNKNOWN;
490 break;
491 case 0xf6:
492 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
493 port->state = STATE_UNKNOWN;
494 break;
495 case 0xf7:
496 switch (port->state) {
497 case STATE_SYSEX_0:
498 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
499 break;
500 case STATE_SYSEX_1:
501 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
502 break;
503 case STATE_SYSEX_2:
504 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
505 break;
506 }
507 port->state = STATE_UNKNOWN;
508 break;
509 }
510 } else if (b >= 0x80) {
511 port->data[0] = b;
512 if (b >= 0xc0 && b <= 0xdf)
513 port->state = STATE_1PARAM;
514 else
515 port->state = STATE_2PARAM_1;
516 } else {
517 switch (port->state) {
518 case STATE_1PARAM:
519 if (port->data[0] < 0xf0) {
520 p0 |= port->data[0] >> 4;
521 } else {
522 p0 |= 0x02;
523 port->state = STATE_UNKNOWN;
524 }
525 output_packet(urb, p0, port->data[0], b, 0);
526 break;
527 case STATE_2PARAM_1:
528 port->data[1] = b;
529 port->state = STATE_2PARAM_2;
530 break;
531 case STATE_2PARAM_2:
532 if (port->data[0] < 0xf0) {
533 p0 |= port->data[0] >> 4;
534 port->state = STATE_2PARAM_1;
535 } else {
536 p0 |= 0x03;
537 port->state = STATE_UNKNOWN;
538 }
539 output_packet(urb, p0, port->data[0], port->data[1], b);
540 break;
541 case STATE_SYSEX_0:
542 port->data[0] = b;
543 port->state = STATE_SYSEX_1;
544 break;
545 case STATE_SYSEX_1:
546 port->data[1] = b;
547 port->state = STATE_SYSEX_2;
548 break;
549 case STATE_SYSEX_2:
550 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
551 port->state = STATE_SYSEX_0;
552 break;
553 }
554 }
555}
556
557static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
558{
559 struct urb* urb = ep->urb;
560 int p;
561
562
563 for (p = 0; p < 0x10; ++p) {
564 struct usbmidi_out_port* port = &ep->ports[p];
565 if (!port->active)
566 continue;
567 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
568 uint8_t b;
569 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
570 port->active = 0;
571 break;
572 }
573 snd_usbmidi_transmit_byte(port, b, urb);
574 }
575 }
576}
577
578static struct usb_protocol_ops snd_usbmidi_standard_ops = {
579 .input = snd_usbmidi_standard_input,
580 .output = snd_usbmidi_standard_output,
581 .output_packet = snd_usbmidi_output_standard_packet,
582};
583
584static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
585 .input = snd_usbmidi_midiman_input,
586 .output = snd_usbmidi_standard_output,
587 .output_packet = snd_usbmidi_output_midiman_packet,
588};
589
590static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
591 .input = snd_usbmidi_maudio_broken_running_status_input,
592 .output = snd_usbmidi_standard_output,
593 .output_packet = snd_usbmidi_output_standard_packet,
594};
595
596static struct usb_protocol_ops snd_usbmidi_cme_ops = {
597 .input = snd_usbmidi_cme_input,
598 .output = snd_usbmidi_standard_output,
599 .output_packet = snd_usbmidi_output_standard_packet,
600};
601
602
603
604
605
606
607
608static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
609 uint8_t* buffer, int buffer_length)
610{
611 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
612 return;
613 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
614}
615
616static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
617{
618 uint8_t* transfer_buffer;
619 int count;
620
621 if (!ep->ports[0].active)
622 return;
623 transfer_buffer = ep->urb->transfer_buffer;
624 count = snd_rawmidi_transmit(ep->ports[0].substream,
625 &transfer_buffer[2],
626 ep->max_transfer - 2);
627 if (count < 1) {
628 ep->ports[0].active = 0;
629 return;
630 }
631 transfer_buffer[0] = 0;
632 transfer_buffer[1] = count;
633 ep->urb->transfer_buffer_length = 2 + count;
634}
635
636static struct usb_protocol_ops snd_usbmidi_novation_ops = {
637 .input = snd_usbmidi_novation_input,
638 .output = snd_usbmidi_novation_output,
639};
640
641
642
643
644
645static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
646 uint8_t* buffer, int buffer_length)
647{
648 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
649}
650
651static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
652{
653 int count;
654
655 if (!ep->ports[0].active)
656 return;
657 count = snd_rawmidi_transmit(ep->ports[0].substream,
658 ep->urb->transfer_buffer,
659 ep->max_transfer);
660 if (count < 1) {
661 ep->ports[0].active = 0;
662 return;
663 }
664 ep->urb->transfer_buffer_length = count;
665}
666
667static struct usb_protocol_ops snd_usbmidi_raw_ops = {
668 .input = snd_usbmidi_raw_input,
669 .output = snd_usbmidi_raw_output,
670};
671
672static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
673 uint8_t *buffer, int buffer_length)
674{
675 if (buffer_length != 9)
676 return;
677 buffer_length = 8;
678 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
679 buffer_length--;
680 if (buffer_length)
681 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
682}
683
684static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep)
685{
686 int count;
687
688 if (!ep->ports[0].active)
689 return;
690 count = ep->urb->dev->speed == USB_SPEED_HIGH ? 1 : 2;
691 count = snd_rawmidi_transmit(ep->ports[0].substream,
692 ep->urb->transfer_buffer,
693 count);
694 if (count < 1) {
695 ep->ports[0].active = 0;
696 return;
697 }
698
699 memset(ep->urb->transfer_buffer + count, 0xFD, 9 - count);
700 ep->urb->transfer_buffer_length = count;
701}
702
703static struct usb_protocol_ops snd_usbmidi_122l_ops = {
704 .input = snd_usbmidi_us122l_input,
705 .output = snd_usbmidi_us122l_output,
706};
707
708
709
710
711
712static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
713{
714 static const u8 init_data[] = {
715
716 0xf0,
717 0x00, 0x20, 0x31,
718 0x64,
719 0x0b,
720 0x00,
721 0x00,
722 0xf7
723 };
724 send_bulk_static_data(ep, init_data, sizeof(init_data));
725
726 send_bulk_static_data(ep, init_data, sizeof(init_data));
727}
728
729static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
730{
731 static const u8 finish_data[] = {
732
733 0xf0,
734 0x00, 0x20, 0x31,
735 0x64,
736 0x10,
737 0x00,
738 0x7f,
739 0x40,
740 0xf7
741 };
742 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
743}
744
745static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
746 uint8_t* buffer, int buffer_length)
747{
748 int i;
749
750
751 for (i = 0; i < buffer_length; ++i)
752 if (buffer[i] == 0xff) {
753 buffer_length = i;
754 break;
755 }
756
757
758 if (ep->seen_f5)
759 goto switch_port;
760
761 while (buffer_length > 0) {
762
763 for (i = 0; i < buffer_length; ++i)
764 if (buffer[i] == 0xf5)
765 break;
766 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
767 buffer += i;
768 buffer_length -= i;
769
770 if (buffer_length <= 0)
771 break;
772
773 ep->seen_f5 = 1;
774 ++buffer;
775 --buffer_length;
776
777 switch_port:
778 if (buffer_length <= 0)
779 break;
780 if (buffer[0] < 0x80) {
781 ep->current_port = (buffer[0] - 1) & 15;
782 ++buffer;
783 --buffer_length;
784 }
785 ep->seen_f5 = 0;
786 }
787}
788
789static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
790{
791 int port0 = ep->current_port;
792 uint8_t* buf = ep->urb->transfer_buffer;
793 int buf_free = ep->max_transfer;
794 int length, i;
795
796 for (i = 0; i < 0x10; ++i) {
797
798 int portnum = (port0 + i) & 15;
799 struct usbmidi_out_port* port = &ep->ports[portnum];
800
801 if (!port->active)
802 continue;
803 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
804 port->active = 0;
805 continue;
806 }
807
808 if (portnum != ep->current_port) {
809 if (buf_free < 2)
810 break;
811 ep->current_port = portnum;
812 buf[0] = 0xf5;
813 buf[1] = (portnum + 1) & 15;
814 buf += 2;
815 buf_free -= 2;
816 }
817
818 if (buf_free < 1)
819 break;
820 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
821 if (length > 0) {
822 buf += length;
823 buf_free -= length;
824 if (buf_free < 1)
825 break;
826 }
827 }
828 if (buf_free < ep->max_transfer && buf_free > 0) {
829 *buf = 0xff;
830 --buf_free;
831 }
832 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
833}
834
835static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
836 .input = snd_usbmidi_emagic_input,
837 .output = snd_usbmidi_emagic_output,
838 .init_out_endpoint = snd_usbmidi_emagic_init_out,
839 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
840};
841
842
843static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
844{
845 struct snd_usb_midi* umidi = substream->rmidi->private_data;
846 struct usbmidi_out_port* port = NULL;
847 int i, j;
848
849 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
850 if (umidi->endpoints[i].out)
851 for (j = 0; j < 0x10; ++j)
852 if (umidi->endpoints[i].out->ports[j].substream == substream) {
853 port = &umidi->endpoints[i].out->ports[j];
854 break;
855 }
856 if (!port) {
857 snd_BUG();
858 return -ENXIO;
859 }
860 substream->runtime->private_data = port;
861 port->state = STATE_UNKNOWN;
862 return 0;
863}
864
865static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
866{
867 return 0;
868}
869
870static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
871{
872 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
873
874 port->active = up;
875 if (up) {
876 if (port->ep->umidi->chip->shutdown) {
877
878
879 while (!snd_rawmidi_transmit_empty(substream))
880 snd_rawmidi_transmit_ack(substream, 1);
881 return;
882 }
883 tasklet_hi_schedule(&port->ep->tasklet);
884 }
885}
886
887static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
888{
889 return 0;
890}
891
892static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
893{
894 return 0;
895}
896
897static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
898{
899 struct snd_usb_midi* umidi = substream->rmidi->private_data;
900
901 if (up)
902 set_bit(substream->number, &umidi->input_triggered);
903 else
904 clear_bit(substream->number, &umidi->input_triggered);
905}
906
907static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
908 .open = snd_usbmidi_output_open,
909 .close = snd_usbmidi_output_close,
910 .trigger = snd_usbmidi_output_trigger,
911};
912
913static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
914 .open = snd_usbmidi_input_open,
915 .close = snd_usbmidi_input_close,
916 .trigger = snd_usbmidi_input_trigger
917};
918
919
920
921
922
923static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
924{
925 if (ep->urb) {
926 usb_buffer_free(ep->umidi->chip->dev,
927 ep->urb->transfer_buffer_length,
928 ep->urb->transfer_buffer,
929 ep->urb->transfer_dma);
930 usb_free_urb(ep->urb);
931 }
932 kfree(ep);
933}
934
935
936
937
938static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
939 struct snd_usb_midi_endpoint_info* ep_info,
940 struct snd_usb_midi_endpoint* rep)
941{
942 struct snd_usb_midi_in_endpoint* ep;
943 void* buffer;
944 unsigned int pipe;
945 int length;
946
947 rep->in = NULL;
948 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
949 if (!ep)
950 return -ENOMEM;
951 ep->umidi = umidi;
952
953 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
954 if (!ep->urb) {
955 snd_usbmidi_in_endpoint_delete(ep);
956 return -ENOMEM;
957 }
958 if (ep_info->in_interval)
959 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
960 else
961 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
962 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
963 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
964 &ep->urb->transfer_dma);
965 if (!buffer) {
966 snd_usbmidi_in_endpoint_delete(ep);
967 return -ENOMEM;
968 }
969 if (ep_info->in_interval)
970 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
971 length, snd_usbmidi_in_urb_complete, ep,
972 ep_info->in_interval);
973 else
974 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
975 length, snd_usbmidi_in_urb_complete, ep);
976 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
977
978 rep->in = ep;
979 return 0;
980}
981
982static unsigned int snd_usbmidi_count_bits(unsigned int x)
983{
984 unsigned int bits;
985
986 for (bits = 0; x; ++bits)
987 x &= x - 1;
988 return bits;
989}
990
991
992
993
994
995static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
996{
997 if (ep->urb) {
998 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
999 ep->urb->transfer_buffer,
1000 ep->urb->transfer_dma);
1001 usb_free_urb(ep->urb);
1002 }
1003 kfree(ep);
1004}
1005
1006
1007
1008
1009static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1010 struct snd_usb_midi_endpoint_info* ep_info,
1011 struct snd_usb_midi_endpoint* rep)
1012{
1013 struct snd_usb_midi_out_endpoint* ep;
1014 int i;
1015 unsigned int pipe;
1016 void* buffer;
1017
1018 rep->out = NULL;
1019 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1020 if (!ep)
1021 return -ENOMEM;
1022 ep->umidi = umidi;
1023
1024 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
1025 if (!ep->urb) {
1026 snd_usbmidi_out_endpoint_delete(ep);
1027 return -ENOMEM;
1028 }
1029 if (ep_info->out_interval)
1030 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
1031 else
1032 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
1033 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020))
1034
1035 ep->max_transfer = 4;
1036 else
1037 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
1038 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
1039 GFP_KERNEL, &ep->urb->transfer_dma);
1040 if (!buffer) {
1041 snd_usbmidi_out_endpoint_delete(ep);
1042 return -ENOMEM;
1043 }
1044 if (ep_info->out_interval)
1045 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
1046 ep->max_transfer, snd_usbmidi_out_urb_complete,
1047 ep, ep_info->out_interval);
1048 else
1049 usb_fill_bulk_urb(ep->urb, umidi->chip->dev,
1050 pipe, buffer, ep->max_transfer,
1051 snd_usbmidi_out_urb_complete, ep);
1052 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1053
1054 spin_lock_init(&ep->buffer_lock);
1055 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1056
1057 for (i = 0; i < 0x10; ++i)
1058 if (ep_info->out_cables & (1 << i)) {
1059 ep->ports[i].ep = ep;
1060 ep->ports[i].cable = i << 4;
1061 }
1062
1063 if (umidi->usb_protocol_ops->init_out_endpoint)
1064 umidi->usb_protocol_ops->init_out_endpoint(ep);
1065
1066 rep->out = ep;
1067 return 0;
1068}
1069
1070
1071
1072
1073static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1074{
1075 int i;
1076
1077 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1078 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1079 if (ep->out)
1080 snd_usbmidi_out_endpoint_delete(ep->out);
1081 if (ep->in)
1082 snd_usbmidi_in_endpoint_delete(ep->in);
1083 }
1084 kfree(umidi);
1085}
1086
1087
1088
1089
1090void snd_usbmidi_disconnect(struct list_head* p)
1091{
1092 struct snd_usb_midi* umidi;
1093 int i;
1094
1095 umidi = list_entry(p, struct snd_usb_midi, list);
1096
1097
1098
1099
1100
1101 spin_lock_irq(&umidi->disc_lock);
1102 umidi->disconnected = 1;
1103 spin_unlock_irq(&umidi->disc_lock);
1104 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1105 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1106 if (ep->out)
1107 tasklet_kill(&ep->out->tasklet);
1108 if (ep->out && ep->out->urb) {
1109 usb_kill_urb(ep->out->urb);
1110 if (umidi->usb_protocol_ops->finish_out_endpoint)
1111 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1112 }
1113 if (ep->in)
1114 usb_kill_urb(ep->in->urb);
1115
1116 if (ep->out) {
1117 snd_usbmidi_out_endpoint_delete(ep->out);
1118 ep->out = NULL;
1119 }
1120 if (ep->in) {
1121 snd_usbmidi_in_endpoint_delete(ep->in);
1122 ep->in = NULL;
1123 }
1124 }
1125 del_timer_sync(&umidi->error_timer);
1126}
1127
1128static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1129{
1130 struct snd_usb_midi* umidi = rmidi->private_data;
1131 snd_usbmidi_free(umidi);
1132}
1133
1134static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1135 int stream, int number)
1136{
1137 struct list_head* list;
1138
1139 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1140 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1141 if (substream->number == number)
1142 return substream;
1143 }
1144 return NULL;
1145}
1146
1147
1148
1149
1150
1151
1152static struct port_info {
1153 u32 id;
1154 short int port;
1155 short int voices;
1156 const char *name;
1157 unsigned int seq_flags;
1158} snd_usbmidi_port_info[] = {
1159#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1160 { .id = USB_ID(vendor, product), \
1161 .port = num, .voices = voices_, \
1162 .name = name_, .seq_flags = flags }
1163#define EXTERNAL_PORT(vendor, product, num, name) \
1164 PORT_INFO(vendor, product, num, name, 0, \
1165 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1166 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1167 SNDRV_SEQ_PORT_TYPE_PORT)
1168#define CONTROL_PORT(vendor, product, num, name) \
1169 PORT_INFO(vendor, product, num, name, 0, \
1170 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1171 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1172#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1173 PORT_INFO(vendor, product, num, name, voices, \
1174 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1175 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1176 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1177 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1178 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1179 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1180 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1181#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1182 PORT_INFO(vendor, product, num, name, voices, \
1183 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1184 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1185 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1186 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1187 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1188 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1189 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1190 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1191
1192 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1193
1194 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1195 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1196 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1197 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1198 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1199 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1200
1201 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1202 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1203
1204 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1205 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1206 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1207
1208 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1209 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1210 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1211
1212 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1213 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1214 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1215
1216 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1217
1218 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1219 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1220 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1221 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1222
1223 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1224
1225 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1226 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1227 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1228
1229 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1230 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1231 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1232 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1233
1234 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1235 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1236
1237 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1238 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1239 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1240
1241 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1242 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1243 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1244
1245 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1246 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1247
1248 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1249 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1250
1251 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1252 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1253 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1254
1255 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1256 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1257 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1258
1259 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1260
1261 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1262 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1263
1264 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1265 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1266
1267 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1268 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1269 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1270};
1271
1272static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1273{
1274 int i;
1275
1276 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1277 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1278 snd_usbmidi_port_info[i].port == number)
1279 return &snd_usbmidi_port_info[i];
1280 }
1281 return NULL;
1282}
1283
1284static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1285 struct snd_seq_port_info *seq_port_info)
1286{
1287 struct snd_usb_midi *umidi = rmidi->private_data;
1288 struct port_info *port_info;
1289
1290
1291 port_info = find_port_info(umidi, number);
1292 if (port_info) {
1293 seq_port_info->type = port_info->seq_flags;
1294 seq_port_info->midi_voices = port_info->voices;
1295 }
1296}
1297
1298static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1299 int stream, int number,
1300 struct snd_rawmidi_substream ** rsubstream)
1301{
1302 struct port_info *port_info;
1303 const char *name_format;
1304
1305 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1306 if (!substream) {
1307 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1308 return;
1309 }
1310
1311
1312 port_info = find_port_info(umidi, number);
1313 name_format = port_info ? port_info->name : "%s MIDI %d";
1314 snprintf(substream->name, sizeof(substream->name),
1315 name_format, umidi->chip->card->shortname, number + 1);
1316
1317 *rsubstream = substream;
1318}
1319
1320
1321
1322
1323static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1324 struct snd_usb_midi_endpoint_info* endpoints)
1325{
1326 int i, j, err;
1327 int out_ports = 0, in_ports = 0;
1328
1329 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1330 if (endpoints[i].out_cables) {
1331 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1332 &umidi->endpoints[i]);
1333 if (err < 0)
1334 return err;
1335 }
1336 if (endpoints[i].in_cables) {
1337 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1338 &umidi->endpoints[i]);
1339 if (err < 0)
1340 return err;
1341 }
1342
1343 for (j = 0; j < 0x10; ++j) {
1344 if (endpoints[i].out_cables & (1 << j)) {
1345 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1346 &umidi->endpoints[i].out->ports[j].substream);
1347 ++out_ports;
1348 }
1349 if (endpoints[i].in_cables & (1 << j)) {
1350 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1351 &umidi->endpoints[i].in->ports[j].substream);
1352 ++in_ports;
1353 }
1354 }
1355 }
1356 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1357 out_ports, in_ports);
1358 return 0;
1359}
1360
1361
1362
1363
1364static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1365 struct snd_usb_midi_endpoint_info* endpoints)
1366{
1367 struct usb_interface* intf;
1368 struct usb_host_interface *hostif;
1369 struct usb_interface_descriptor* intfd;
1370 struct usb_ms_header_descriptor* ms_header;
1371 struct usb_host_endpoint *hostep;
1372 struct usb_endpoint_descriptor* ep;
1373 struct usb_ms_endpoint_descriptor* ms_ep;
1374 int i, epidx;
1375
1376 intf = umidi->iface;
1377 if (!intf)
1378 return -ENXIO;
1379 hostif = &intf->altsetting[0];
1380 intfd = get_iface_desc(hostif);
1381 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1382 if (hostif->extralen >= 7 &&
1383 ms_header->bLength >= 7 &&
1384 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1385 ms_header->bDescriptorSubtype == HEADER)
1386 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1387 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1388 else
1389 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1390
1391 epidx = 0;
1392 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1393 hostep = &hostif->endpoint[i];
1394 ep = get_ep_desc(hostep);
1395 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1396 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1397 continue;
1398 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1399 if (hostep->extralen < 4 ||
1400 ms_ep->bLength < 4 ||
1401 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1402 ms_ep->bDescriptorSubtype != MS_GENERAL)
1403 continue;
1404 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1405 if (endpoints[epidx].out_ep) {
1406 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1407 snd_printk(KERN_WARNING "too many endpoints\n");
1408 break;
1409 }
1410 }
1411 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1412 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1413 endpoints[epidx].out_interval = ep->bInterval;
1414 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1415
1416
1417
1418
1419
1420 endpoints[epidx].out_interval = 1;
1421 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1422 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1423 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1424 } else {
1425 if (endpoints[epidx].in_ep) {
1426 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1427 snd_printk(KERN_WARNING "too many endpoints\n");
1428 break;
1429 }
1430 }
1431 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1432 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1433 endpoints[epidx].in_interval = ep->bInterval;
1434 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1435 endpoints[epidx].in_interval = 1;
1436 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1437 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1438 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1439 }
1440 }
1441 return 0;
1442}
1443
1444
1445
1446
1447
1448static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1449{
1450 struct usb_interface* intf;
1451 struct usb_host_interface *hostif;
1452 struct usb_interface_descriptor* intfd;
1453
1454 intf = umidi->iface;
1455 if (!intf || intf->num_altsetting != 2)
1456 return;
1457
1458 hostif = &intf->altsetting[1];
1459 intfd = get_iface_desc(hostif);
1460 if (intfd->bNumEndpoints != 2 ||
1461 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1462 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1463 return;
1464
1465 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1466 intfd->bAlternateSetting);
1467 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1468 intfd->bAlternateSetting);
1469}
1470
1471
1472
1473
1474static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1475 struct snd_usb_midi_endpoint_info* endpoint,
1476 int max_endpoints)
1477{
1478 struct usb_interface* intf;
1479 struct usb_host_interface *hostif;
1480 struct usb_interface_descriptor* intfd;
1481 struct usb_endpoint_descriptor* epd;
1482 int i, out_eps = 0, in_eps = 0;
1483
1484 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
1485 snd_usbmidi_switch_roland_altsetting(umidi);
1486
1487 if (endpoint[0].out_ep || endpoint[0].in_ep)
1488 return 0;
1489
1490 intf = umidi->iface;
1491 if (!intf || intf->num_altsetting < 1)
1492 return -ENOENT;
1493 hostif = intf->cur_altsetting;
1494 intfd = get_iface_desc(hostif);
1495
1496 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1497 epd = get_endpoint(hostif, i);
1498 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1499 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1500 continue;
1501 if (out_eps < max_endpoints &&
1502 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1503 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1504 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1505 endpoint[out_eps].out_interval = epd->bInterval;
1506 ++out_eps;
1507 }
1508 if (in_eps < max_endpoints &&
1509 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1510 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1511 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1512 endpoint[in_eps].in_interval = epd->bInterval;
1513 ++in_eps;
1514 }
1515 }
1516 return (out_eps || in_eps) ? 0 : -ENOENT;
1517}
1518
1519
1520
1521
1522static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1523 struct snd_usb_midi_endpoint_info* endpoints)
1524{
1525 int err, i;
1526
1527 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1528 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1529 if (endpoints[i].out_ep)
1530 endpoints[i].out_cables = 0x0001;
1531 if (endpoints[i].in_ep)
1532 endpoints[i].in_cables = 0x0001;
1533 }
1534 return err;
1535}
1536
1537
1538
1539
1540static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1541 struct snd_usb_midi_endpoint_info* endpoint)
1542{
1543 struct usb_interface* intf;
1544 struct usb_host_interface *hostif;
1545 struct usb_interface_descriptor* intfd;
1546 uint8_t* cs_desc;
1547
1548 intf = umidi->iface;
1549 if (!intf)
1550 return -ENOENT;
1551 hostif = intf->altsetting;
1552 intfd = get_iface_desc(hostif);
1553 if (intfd->bNumEndpoints < 1)
1554 return -ENOENT;
1555
1556
1557
1558
1559
1560 for (cs_desc = hostif->extra;
1561 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1562 cs_desc += cs_desc[0]) {
1563 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1564 if (cs_desc[2] == MIDI_IN_JACK)
1565 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1566 else if (cs_desc[2] == MIDI_OUT_JACK)
1567 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1568 }
1569 }
1570 if (!endpoint->in_cables && !endpoint->out_cables)
1571 return -ENOENT;
1572
1573 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1574}
1575
1576
1577
1578
1579static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1580 struct snd_usb_midi_endpoint_info* endpoint)
1581{
1582 struct snd_usb_midi_endpoint_info ep_info;
1583 struct usb_interface* intf;
1584 struct usb_host_interface *hostif;
1585 struct usb_interface_descriptor* intfd;
1586 struct usb_endpoint_descriptor* epd;
1587 int cable, err;
1588
1589 intf = umidi->iface;
1590 if (!intf)
1591 return -ENOENT;
1592 hostif = intf->altsetting;
1593 intfd = get_iface_desc(hostif);
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1605 snd_printdd(KERN_ERR "not enough endpoints\n");
1606 return -ENOENT;
1607 }
1608
1609 epd = get_endpoint(hostif, 0);
1610 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1611 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1612 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1613 return -ENXIO;
1614 }
1615 epd = get_endpoint(hostif, 2);
1616 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1617 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1618 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1619 return -ENXIO;
1620 }
1621 if (endpoint->out_cables > 0x0001) {
1622 epd = get_endpoint(hostif, 4);
1623 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1624 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1625 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1626 return -ENXIO;
1627 }
1628 }
1629
1630 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1631 ep_info.out_cables = endpoint->out_cables & 0x5555;
1632 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1633 if (err < 0)
1634 return err;
1635
1636 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1637 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1638 ep_info.in_cables = endpoint->in_cables;
1639 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1640 if (err < 0)
1641 return err;
1642
1643 if (endpoint->out_cables > 0x0001) {
1644 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1645 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1646 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1647 if (err < 0)
1648 return err;
1649 }
1650
1651 for (cable = 0; cable < 0x10; ++cable) {
1652 if (endpoint->out_cables & (1 << cable))
1653 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1654 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1655 if (endpoint->in_cables & (1 << cable))
1656 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1657 &umidi->endpoints[0].in->ports[cable].substream);
1658 }
1659 return 0;
1660}
1661
1662static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1663 .get_port_info = snd_usbmidi_get_port_info,
1664};
1665
1666static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
1667 int out_ports, int in_ports)
1668{
1669 struct snd_rawmidi *rmidi;
1670 int err;
1671
1672 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1673 umidi->chip->next_midi_device++,
1674 out_ports, in_ports, &rmidi);
1675 if (err < 0)
1676 return err;
1677 strcpy(rmidi->name, umidi->chip->card->shortname);
1678 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1679 SNDRV_RAWMIDI_INFO_INPUT |
1680 SNDRV_RAWMIDI_INFO_DUPLEX;
1681 rmidi->ops = &snd_usbmidi_ops;
1682 rmidi->private_data = umidi;
1683 rmidi->private_free = snd_usbmidi_rawmidi_free;
1684 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1685 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1686
1687 umidi->rmidi = rmidi;
1688 return 0;
1689}
1690
1691
1692
1693
1694void snd_usbmidi_input_stop(struct list_head* p)
1695{
1696 struct snd_usb_midi* umidi;
1697 int i;
1698
1699 umidi = list_entry(p, struct snd_usb_midi, list);
1700 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1701 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1702 if (ep->in)
1703 usb_kill_urb(ep->in->urb);
1704 }
1705}
1706
1707static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
1708{
1709 if (ep) {
1710 struct urb* urb = ep->urb;
1711 urb->dev = ep->umidi->chip->dev;
1712 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1713 }
1714}
1715
1716
1717
1718
1719void snd_usbmidi_input_start(struct list_head* p)
1720{
1721 struct snd_usb_midi* umidi;
1722 int i;
1723
1724 umidi = list_entry(p, struct snd_usb_midi, list);
1725 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1726 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1727}
1728
1729
1730
1731
1732int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
1733 struct usb_interface* iface,
1734 const struct snd_usb_audio_quirk* quirk)
1735{
1736 struct snd_usb_midi* umidi;
1737 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
1738 int out_ports, in_ports;
1739 int i, err;
1740
1741 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
1742 if (!umidi)
1743 return -ENOMEM;
1744 umidi->chip = chip;
1745 umidi->iface = iface;
1746 umidi->quirk = quirk;
1747 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
1748 init_timer(&umidi->error_timer);
1749 spin_lock_init(&umidi->disc_lock);
1750 umidi->error_timer.function = snd_usbmidi_error_timer;
1751 umidi->error_timer.data = (unsigned long)umidi;
1752
1753
1754 memset(endpoints, 0, sizeof(endpoints));
1755 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1756 case QUIRK_MIDI_STANDARD_INTERFACE:
1757 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1758 if (chip->usb_id == USB_ID(0x0763, 0x0150))
1759 umidi->usb_protocol_ops =
1760 &snd_usbmidi_maudio_broken_running_status_ops;
1761 break;
1762 case QUIRK_MIDI_US122L:
1763 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1764
1765 case QUIRK_MIDI_FIXED_ENDPOINT:
1766 memcpy(&endpoints[0], quirk->data,
1767 sizeof(struct snd_usb_midi_endpoint_info));
1768 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1769 break;
1770 case QUIRK_MIDI_YAMAHA:
1771 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1772 break;
1773 case QUIRK_MIDI_MIDIMAN:
1774 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1775 memcpy(&endpoints[0], quirk->data,
1776 sizeof(struct snd_usb_midi_endpoint_info));
1777 err = 0;
1778 break;
1779 case QUIRK_MIDI_NOVATION:
1780 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1781 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1782 break;
1783 case QUIRK_MIDI_RAW:
1784 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1785 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1786 break;
1787 case QUIRK_MIDI_EMAGIC:
1788 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1789 memcpy(&endpoints[0], quirk->data,
1790 sizeof(struct snd_usb_midi_endpoint_info));
1791 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1792 break;
1793 case QUIRK_MIDI_CME:
1794 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
1795 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1796 break;
1797 default:
1798 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1799 err = -ENXIO;
1800 break;
1801 }
1802 if (err < 0) {
1803 kfree(umidi);
1804 return err;
1805 }
1806
1807
1808 out_ports = 0;
1809 in_ports = 0;
1810 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1811 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1812 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1813 }
1814 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1815 if (err < 0) {
1816 kfree(umidi);
1817 return err;
1818 }
1819
1820
1821 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1822 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1823 else
1824 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1825 if (err < 0) {
1826 snd_usbmidi_free(umidi);
1827 return err;
1828 }
1829
1830 list_add(&umidi->list, &umidi->chip->midi_list);
1831
1832 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1833 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1834 return 0;
1835}
1836
1837EXPORT_SYMBOL(snd_usb_create_midi_interface);
1838EXPORT_SYMBOL(snd_usbmidi_input_stop);
1839EXPORT_SYMBOL(snd_usbmidi_input_start);
1840EXPORT_SYMBOL(snd_usbmidi_disconnect);