1 /*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
4 * Copyright (c) 2002-2009 Clemens Ladisch
5 * All rights reserved.
6 *
7 * Based on the OSS usb-midi driver by NAGANO Daisuke,
8 * NetBSD's umidi driver by Takuya SHIOZAKI,
9 * the "USB Device Class Definition for MIDI Devices" by Roland
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed and/or modified under the
21 * terms of the GNU General Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at your option) any later
23 * version.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
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 <linux/wait.h>
49 #include <linux/usb/audio.h>
50 #include <linux/usb/midi.h>
51 #include <linux/module.h>
52
53 #include <sound/core.h>
54 #include <sound/control.h>
55 #include <sound/rawmidi.h>
56 #include <sound/asequencer.h>
57 #include "usbaudio.h"
58 #include "midi.h"
59 #include "power.h"
60 #include "helper.h"
61
62 /*
63 * define this to log all USB packets
64 */
65 /* #define DUMP_PACKETS */
66
67 /*
68 * how long to wait after some USB errors, so that hub_wq can disconnect() us
69 * without too many spurious errors
70 */
71 #define ERROR_DELAY_JIFFIES (HZ / 10)
72
73 #define OUTPUT_URBS 7
74 #define INPUT_URBS 7
75
76
77 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
78 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
79 MODULE_LICENSE("Dual BSD/GPL");
80
81 struct snd_usb_midi_in_endpoint;
82 struct snd_usb_midi_out_endpoint;
83 struct snd_usb_midi_endpoint;
84
85 struct usb_protocol_ops {
86 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
87 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
88 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
89 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint *);
90 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint *);
91 };
92
93 struct snd_usb_midi {
94 struct usb_device *dev;
95 struct snd_card *card;
96 struct usb_interface *iface;
97 const struct snd_usb_audio_quirk *quirk;
98 struct snd_rawmidi *rmidi;
99 const struct usb_protocol_ops *usb_protocol_ops;
100 struct list_head list;
101 struct timer_list error_timer;
102 spinlock_t disc_lock;
103 struct rw_semaphore disc_rwsem;
104 struct mutex mutex;
105 u32 usb_id;
106 int next_midi_device;
107
108 struct snd_usb_midi_endpoint {
109 struct snd_usb_midi_out_endpoint *out;
110 struct snd_usb_midi_in_endpoint *in;
111 } endpoints[MIDI_MAX_ENDPOINTS];
112 unsigned long input_triggered;
113 unsigned int opened[2];
114 unsigned char disconnected;
115 unsigned char input_running;
116
117 struct snd_kcontrol *roland_load_ctl;
118 };
119
120 struct snd_usb_midi_out_endpoint {
121 struct snd_usb_midi *umidi;
122 struct out_urb_context {
123 struct urb *urb;
124 struct snd_usb_midi_out_endpoint *ep;
125 } urbs[OUTPUT_URBS];
126 unsigned int active_urbs;
127 unsigned int drain_urbs;
128 int max_transfer; /* size of urb buffer */
129 struct work_struct work;
130 unsigned int next_urb;
131 spinlock_t buffer_lock;
132
133 struct usbmidi_out_port {
134 struct snd_usb_midi_out_endpoint *ep;
135 struct snd_rawmidi_substream *substream;
136 int active;
137 uint8_t cable; /* cable number << 4 */
138 uint8_t state;
139 #define STATE_UNKNOWN 0
140 #define STATE_1PARAM 1
141 #define STATE_2PARAM_1 2
142 #define STATE_2PARAM_2 3
143 #define STATE_SYSEX_0 4
144 #define STATE_SYSEX_1 5
145 #define STATE_SYSEX_2 6
146 uint8_t data[2];
147 } ports[0x10];
148 int current_port;
149
150 wait_queue_head_t drain_wait;
151 };
152
153 struct snd_usb_midi_in_endpoint {
154 struct snd_usb_midi *umidi;
155 struct urb *urbs[INPUT_URBS];
156 struct usbmidi_in_port {
157 struct snd_rawmidi_substream *substream;
158 u8 running_status_length;
159 } ports[0x10];
160 u8 seen_f5;
161 bool in_sysex;
162 u8 last_cin;
163 u8 error_resubmit;
164 int current_port;
165 };
166
167 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep);
168
169 static const uint8_t snd_usbmidi_cin_length[] = {
170 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
171 };
172
173 /*
174 * Submits the URB, with error handling.
175 */
snd_usbmidi_submit_urb(struct urb * urb,gfp_t flags)176 static int snd_usbmidi_submit_urb(struct urb *urb, gfp_t flags)
177 {
178 int err = usb_submit_urb(urb, flags);
179 if (err < 0 && err != -ENODEV)
180 dev_err(&urb->dev->dev, "usb_submit_urb: %d\n", err);
181 return err;
182 }
183
184 /*
185 * Error handling for URB completion functions.
186 */
snd_usbmidi_urb_error(const struct urb * urb)187 static int snd_usbmidi_urb_error(const struct urb *urb)
188 {
189 switch (urb->status) {
190 /* manually unlinked, or device gone */
191 case -ENOENT:
192 case -ECONNRESET:
193 case -ESHUTDOWN:
194 case -ENODEV:
195 return -ENODEV;
196 /* errors that might occur during unplugging */
197 case -EPROTO:
198 case -ETIME:
199 case -EILSEQ:
200 return -EIO;
201 default:
202 dev_err(&urb->dev->dev, "urb status %d\n", urb->status);
203 return 0; /* continue */
204 }
205 }
206
207 /*
208 * Receives a chunk of MIDI data.
209 */
snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint * ep,int portidx,uint8_t * data,int length)210 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint *ep,
211 int portidx, uint8_t *data, int length)
212 {
213 struct usbmidi_in_port *port = &ep->ports[portidx];
214
215 if (!port->substream) {
216 dev_dbg(&ep->umidi->dev->dev, "unexpected port %d!\n", portidx);
217 return;
218 }
219 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
220 return;
221 snd_rawmidi_receive(port->substream, data, length);
222 }
223
224 #ifdef DUMP_PACKETS
dump_urb(const char * type,const u8 * data,int length)225 static void dump_urb(const char *type, const u8 *data, int length)
226 {
227 snd_printk(KERN_DEBUG "%s packet: [", type);
228 for (; length > 0; ++data, --length)
229 printk(KERN_CONT " %02x", *data);
230 printk(KERN_CONT " ]\n");
231 }
232 #else
233 #define dump_urb(type, data, length) /* nothing */
234 #endif
235
236 /*
237 * Processes the data read from the device.
238 */
snd_usbmidi_in_urb_complete(struct urb * urb)239 static void snd_usbmidi_in_urb_complete(struct urb *urb)
240 {
241 struct snd_usb_midi_in_endpoint *ep = urb->context;
242
243 if (urb->status == 0) {
244 dump_urb("received", urb->transfer_buffer, urb->actual_length);
245 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
246 urb->actual_length);
247 } else {
248 int err = snd_usbmidi_urb_error(urb);
249 if (err < 0) {
250 if (err != -ENODEV) {
251 ep->error_resubmit = 1;
252 mod_timer(&ep->umidi->error_timer,
253 jiffies + ERROR_DELAY_JIFFIES);
254 }
255 return;
256 }
257 }
258
259 urb->dev = ep->umidi->dev;
260 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
261 }
262
snd_usbmidi_out_urb_complete(struct urb * urb)263 static void snd_usbmidi_out_urb_complete(struct urb *urb)
264 {
265 struct out_urb_context *context = urb->context;
266 struct snd_usb_midi_out_endpoint *ep = context->ep;
267 unsigned int urb_index;
268 unsigned long flags;
269
270 spin_lock_irqsave(&ep->buffer_lock, flags);
271 urb_index = context - ep->urbs;
272 ep->active_urbs &= ~(1 << urb_index);
273 if (unlikely(ep->drain_urbs)) {
274 ep->drain_urbs &= ~(1 << urb_index);
275 wake_up(&ep->drain_wait);
276 }
277 spin_unlock_irqrestore(&ep->buffer_lock, flags);
278 if (urb->status < 0) {
279 int err = snd_usbmidi_urb_error(urb);
280 if (err < 0) {
281 if (err != -ENODEV)
282 mod_timer(&ep->umidi->error_timer,
283 jiffies + ERROR_DELAY_JIFFIES);
284 return;
285 }
286 }
287 snd_usbmidi_do_output(ep);
288 }
289
290 /*
291 * This is called when some data should be transferred to the device
292 * (from one or more substreams).
293 */
snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint * ep)294 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep)
295 {
296 unsigned int urb_index;
297 struct urb *urb;
298 unsigned long flags;
299
300 spin_lock_irqsave(&ep->buffer_lock, flags);
301 if (ep->umidi->disconnected) {
302 spin_unlock_irqrestore(&ep->buffer_lock, flags);
303 return;
304 }
305
306 urb_index = ep->next_urb;
307 for (;;) {
308 if (!(ep->active_urbs & (1 << urb_index))) {
309 urb = ep->urbs[urb_index].urb;
310 urb->transfer_buffer_length = 0;
311 ep->umidi->usb_protocol_ops->output(ep, urb);
312 if (urb->transfer_buffer_length == 0)
313 break;
314
315 dump_urb("sending", urb->transfer_buffer,
316 urb->transfer_buffer_length);
317 urb->dev = ep->umidi->dev;
318 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
319 break;
320 ep->active_urbs |= 1 << urb_index;
321 }
322 if (++urb_index >= OUTPUT_URBS)
323 urb_index = 0;
324 if (urb_index == ep->next_urb)
325 break;
326 }
327 ep->next_urb = urb_index;
328 spin_unlock_irqrestore(&ep->buffer_lock, flags);
329 }
330
snd_usbmidi_out_work(struct work_struct * work)331 static void snd_usbmidi_out_work(struct work_struct *work)
332 {
333 struct snd_usb_midi_out_endpoint *ep =
334 container_of(work, struct snd_usb_midi_out_endpoint, work);
335
336 snd_usbmidi_do_output(ep);
337 }
338
339 /* called after transfers had been interrupted due to some USB error */
snd_usbmidi_error_timer(struct timer_list * t)340 static void snd_usbmidi_error_timer(struct timer_list *t)
341 {
342 struct snd_usb_midi *umidi = from_timer(umidi, t, error_timer);
343 unsigned int i, j;
344
345 spin_lock(&umidi->disc_lock);
346 if (umidi->disconnected) {
347 spin_unlock(&umidi->disc_lock);
348 return;
349 }
350 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
351 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
352 if (in && in->error_resubmit) {
353 in->error_resubmit = 0;
354 for (j = 0; j < INPUT_URBS; ++j) {
355 if (atomic_read(&in->urbs[j]->use_count))
356 continue;
357 in->urbs[j]->dev = umidi->dev;
358 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
359 }
360 }
361 if (umidi->endpoints[i].out)
362 snd_usbmidi_do_output(umidi->endpoints[i].out);
363 }
364 spin_unlock(&umidi->disc_lock);
365 }
366
367 /* helper function to send static data that may not DMA-able */
send_bulk_static_data(struct snd_usb_midi_out_endpoint * ep,const void * data,int len)368 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint *ep,
369 const void *data, int len)
370 {
371 int err = 0;
372 void *buf = kmemdup(data, len, GFP_KERNEL);
373 if (!buf)
374 return -ENOMEM;
375 dump_urb("sending", buf, len);
376 if (ep->urbs[0].urb)
377 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
378 buf, len, NULL, 250);
379 kfree(buf);
380 return err;
381 }
382
383 /*
384 * Standard USB MIDI protocol: see the spec.
385 * Midiman protocol: like the standard protocol, but the control byte is the
386 * fourth byte in each packet, and uses length instead of CIN.
387 */
388
snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)389 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint *ep,
390 uint8_t *buffer, int buffer_length)
391 {
392 int i;
393
394 for (i = 0; i + 3 < buffer_length; i += 4)
395 if (buffer[i] != 0) {
396 int cable = buffer[i] >> 4;
397 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
398 snd_usbmidi_input_data(ep, cable, &buffer[i + 1],
399 length);
400 }
401 }
402
snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)403 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint *ep,
404 uint8_t *buffer, int buffer_length)
405 {
406 int i;
407
408 for (i = 0; i + 3 < buffer_length; i += 4)
409 if (buffer[i + 3] != 0) {
410 int port = buffer[i + 3] >> 4;
411 int length = buffer[i + 3] & 3;
412 snd_usbmidi_input_data(ep, port, &buffer[i], length);
413 }
414 }
415
416 /*
417 * Buggy M-Audio device: running status on input results in a packet that has
418 * the data bytes but not the status byte and that is marked with CIN 4.
419 */
snd_usbmidi_maudio_broken_running_status_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)420 static void snd_usbmidi_maudio_broken_running_status_input(
421 struct snd_usb_midi_in_endpoint *ep,
422 uint8_t *buffer, int buffer_length)
423 {
424 int i;
425
426 for (i = 0; i + 3 < buffer_length; i += 4)
427 if (buffer[i] != 0) {
428 int cable = buffer[i] >> 4;
429 u8 cin = buffer[i] & 0x0f;
430 struct usbmidi_in_port *port = &ep->ports[cable];
431 int length;
432
433 length = snd_usbmidi_cin_length[cin];
434 if (cin == 0xf && buffer[i + 1] >= 0xf8)
435 ; /* realtime msg: no running status change */
436 else if (cin >= 0x8 && cin <= 0xe)
437 /* channel msg */
438 port->running_status_length = length - 1;
439 else if (cin == 0x4 &&
440 port->running_status_length != 0 &&
441 buffer[i + 1] < 0x80)
442 /* CIN 4 that is not a SysEx */
443 length = port->running_status_length;
444 else
445 /*
446 * All other msgs cannot begin running status.
447 * (A channel msg sent as two or three CIN 0xF
448 * packets could in theory, but this device
449 * doesn't use this format.)
450 */
451 port->running_status_length = 0;
452 snd_usbmidi_input_data(ep, cable, &buffer[i + 1],
453 length);
454 }
455 }
456
457 /*
458 * QinHeng CH345 is buggy: every second packet inside a SysEx has not CIN 4
459 * but the previously seen CIN, but still with three data bytes.
460 */
ch345_broken_sysex_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)461 static void ch345_broken_sysex_input(struct snd_usb_midi_in_endpoint *ep,
462 uint8_t *buffer, int buffer_length)
463 {
464 unsigned int i, cin, length;
465
466 for (i = 0; i + 3 < buffer_length; i += 4) {
467 if (buffer[i] == 0 && i > 0)
468 break;
469 cin = buffer[i] & 0x0f;
470 if (ep->in_sysex &&
471 cin == ep->last_cin &&
472 (buffer[i + 1 + (cin == 0x6)] & 0x80) == 0)
473 cin = 0x4;
474 #if 0
475 if (buffer[i + 1] == 0x90) {
476 /*
477 * Either a corrupted running status or a real note-on
478 * message; impossible to detect reliably.
479 */
480 }
481 #endif
482 length = snd_usbmidi_cin_length[cin];
483 snd_usbmidi_input_data(ep, 0, &buffer[i + 1], length);
484 ep->in_sysex = cin == 0x4;
485 if (!ep->in_sysex)
486 ep->last_cin = cin;
487 }
488 }
489
490 /*
491 * CME protocol: like the standard protocol, but SysEx commands are sent as a
492 * single USB packet preceded by a 0x0F byte, as are system realtime
493 * messages and MIDI Active Sensing.
494 * Also, multiple messages can be sent in the same packet.
495 */
snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)496 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
497 uint8_t *buffer, int buffer_length)
498 {
499 int remaining = buffer_length;
500
501 /*
502 * CME send sysex, song position pointer, system realtime
503 * and active sensing using CIN 0x0f, which in the standard
504 * is only intended for single byte unparsed data.
505 * So we need to interpret these here before sending them on.
506 * By default, we assume single byte data, which is true
507 * for system realtime (midi clock, start, stop and continue)
508 * and active sensing, and handle the other (known) cases
509 * separately.
510 * In contrast to the standard, CME does not split sysex
511 * into multiple 4-byte packets, but lumps everything together
512 * into one. In addition, CME can string multiple messages
513 * together in the same packet; pressing the Record button
514 * on an UF6 sends a sysex message directly followed
515 * by a song position pointer in the same packet.
516 * For it to have any reasonable meaning, a sysex message
517 * needs to be at least 3 bytes in length (0xf0, id, 0xf7),
518 * corresponding to a packet size of 4 bytes, and the ones sent
519 * by CME devices are 6 or 7 bytes, making the packet fragments
520 * 7 or 8 bytes long (six or seven bytes plus preceding CN+CIN byte).
521 * For the other types, the packet size is always 4 bytes,
522 * as per the standard, with the data size being 3 for SPP
523 * and 1 for the others.
524 * Thus all packet fragments are at least 4 bytes long, so we can
525 * skip anything that is shorter; this also conveniantly skips
526 * packets with size 0, which CME devices continuously send when
527 * they have nothing better to do.
528 * Another quirk is that sometimes multiple messages are sent
529 * in the same packet. This has been observed for midi clock
530 * and active sensing i.e. 0x0f 0xf8 0x00 0x00 0x0f 0xfe 0x00 0x00,
531 * but also multiple note ons/offs, and control change together
532 * with MIDI clock. Similarly, some sysex messages are followed by
533 * the song position pointer in the same packet, and occasionally
534 * additionally by a midi clock or active sensing.
535 * We handle this by looping over all data and parsing it along the way.
536 */
537 while (remaining >= 4) {
538 int source_length = 4; /* default */
539
540 if ((buffer[0] & 0x0f) == 0x0f) {
541 int data_length = 1; /* default */
542
543 if (buffer[1] == 0xf0) {
544 /* Sysex: Find EOX and send on whole message. */
545 /* To kick off the search, skip the first
546 * two bytes (CN+CIN and SYSEX (0xf0).
547 */
548 uint8_t *tmp_buf = buffer + 2;
549 int tmp_length = remaining - 2;
550
551 while (tmp_length > 1 && *tmp_buf != 0xf7) {
552 tmp_buf++;
553 tmp_length--;
554 }
555 data_length = tmp_buf - buffer;
556 source_length = data_length + 1;
557 } else if (buffer[1] == 0xf2) {
558 /* Three byte song position pointer */
559 data_length = 3;
560 }
561 snd_usbmidi_input_data(ep, buffer[0] >> 4,
562 &buffer[1], data_length);
563 } else {
564 /* normal channel events */
565 snd_usbmidi_standard_input(ep, buffer, source_length);
566 }
567 buffer += source_length;
568 remaining -= source_length;
569 }
570 }
571
572 /*
573 * Adds one USB MIDI packet to the output buffer.
574 */
snd_usbmidi_output_standard_packet(struct urb * urb,uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3)575 static void snd_usbmidi_output_standard_packet(struct urb *urb, uint8_t p0,
576 uint8_t p1, uint8_t p2,
577 uint8_t p3)
578 {
579
580 uint8_t *buf =
581 (uint8_t *)urb->transfer_buffer + urb->transfer_buffer_length;
582 buf[0] = p0;
583 buf[1] = p1;
584 buf[2] = p2;
585 buf[3] = p3;
586 urb->transfer_buffer_length += 4;
587 }
588
589 /*
590 * Adds one Midiman packet to the output buffer.
591 */
snd_usbmidi_output_midiman_packet(struct urb * urb,uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3)592 static void snd_usbmidi_output_midiman_packet(struct urb *urb, uint8_t p0,
593 uint8_t p1, uint8_t p2,
594 uint8_t p3)
595 {
596
597 uint8_t *buf =
598 (uint8_t *)urb->transfer_buffer + urb->transfer_buffer_length;
599 buf[0] = p1;
600 buf[1] = p2;
601 buf[2] = p3;
602 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
603 urb->transfer_buffer_length += 4;
604 }
605
606 /*
607 * Converts MIDI commands to USB MIDI packets.
608 */
snd_usbmidi_transmit_byte(struct usbmidi_out_port * port,uint8_t b,struct urb * urb)609 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port *port,
610 uint8_t b, struct urb *urb)
611 {
612 uint8_t p0 = port->cable;
613 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
614 port->ep->umidi->usb_protocol_ops->output_packet;
615
616 if (b >= 0xf8) {
617 output_packet(urb, p0 | 0x0f, b, 0, 0);
618 } else if (b >= 0xf0) {
619 switch (b) {
620 case 0xf0:
621 port->data[0] = b;
622 port->state = STATE_SYSEX_1;
623 break;
624 case 0xf1:
625 case 0xf3:
626 port->data[0] = b;
627 port->state = STATE_1PARAM;
628 break;
629 case 0xf2:
630 port->data[0] = b;
631 port->state = STATE_2PARAM_1;
632 break;
633 case 0xf4:
634 case 0xf5:
635 port->state = STATE_UNKNOWN;
636 break;
637 case 0xf6:
638 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
639 port->state = STATE_UNKNOWN;
640 break;
641 case 0xf7:
642 switch (port->state) {
643 case STATE_SYSEX_0:
644 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
645 break;
646 case STATE_SYSEX_1:
647 output_packet(urb, p0 | 0x06, port->data[0],
648 0xf7, 0);
649 break;
650 case STATE_SYSEX_2:
651 output_packet(urb, p0 | 0x07, port->data[0],
652 port->data[1], 0xf7);
653 break;
654 }
655 port->state = STATE_UNKNOWN;
656 break;
657 }
658 } else if (b >= 0x80) {
659 port->data[0] = b;
660 if (b >= 0xc0 && b <= 0xdf)
661 port->state = STATE_1PARAM;
662 else
663 port->state = STATE_2PARAM_1;
664 } else { /* b < 0x80 */
665 switch (port->state) {
666 case STATE_1PARAM:
667 if (port->data[0] < 0xf0) {
668 p0 |= port->data[0] >> 4;
669 } else {
670 p0 |= 0x02;
671 port->state = STATE_UNKNOWN;
672 }
673 output_packet(urb, p0, port->data[0], b, 0);
674 break;
675 case STATE_2PARAM_1:
676 port->data[1] = b;
677 port->state = STATE_2PARAM_2;
678 break;
679 case STATE_2PARAM_2:
680 if (port->data[0] < 0xf0) {
681 p0 |= port->data[0] >> 4;
682 port->state = STATE_2PARAM_1;
683 } else {
684 p0 |= 0x03;
685 port->state = STATE_UNKNOWN;
686 }
687 output_packet(urb, p0, port->data[0], port->data[1], b);
688 break;
689 case STATE_SYSEX_0:
690 port->data[0] = b;
691 port->state = STATE_SYSEX_1;
692 break;
693 case STATE_SYSEX_1:
694 port->data[1] = b;
695 port->state = STATE_SYSEX_2;
696 break;
697 case STATE_SYSEX_2:
698 output_packet(urb, p0 | 0x04, port->data[0],
699 port->data[1], b);
700 port->state = STATE_SYSEX_0;
701 break;
702 }
703 }
704 }
705
snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint * ep,struct urb * urb)706 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint *ep,
707 struct urb *urb)
708 {
709 int p;
710
711 /* FIXME: lower-numbered ports can starve higher-numbered ports */
712 for (p = 0; p < 0x10; ++p) {
713 struct usbmidi_out_port *port = &ep->ports[p];
714 if (!port->active)
715 continue;
716 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
717 uint8_t b;
718 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
719 port->active = 0;
720 break;
721 }
722 snd_usbmidi_transmit_byte(port, b, urb);
723 }
724 }
725 }
726
727 static const struct usb_protocol_ops snd_usbmidi_standard_ops = {
728 .input = snd_usbmidi_standard_input,
729 .output = snd_usbmidi_standard_output,
730 .output_packet = snd_usbmidi_output_standard_packet,
731 };
732
733 static const struct usb_protocol_ops snd_usbmidi_midiman_ops = {
734 .input = snd_usbmidi_midiman_input,
735 .output = snd_usbmidi_standard_output,
736 .output_packet = snd_usbmidi_output_midiman_packet,
737 };
738
739 static const
740 struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
741 .input = snd_usbmidi_maudio_broken_running_status_input,
742 .output = snd_usbmidi_standard_output,
743 .output_packet = snd_usbmidi_output_standard_packet,
744 };
745
746 static const struct usb_protocol_ops snd_usbmidi_cme_ops = {
747 .input = snd_usbmidi_cme_input,
748 .output = snd_usbmidi_standard_output,
749 .output_packet = snd_usbmidi_output_standard_packet,
750 };
751
752 static const struct usb_protocol_ops snd_usbmidi_ch345_broken_sysex_ops = {
753 .input = ch345_broken_sysex_input,
754 .output = snd_usbmidi_standard_output,
755 .output_packet = snd_usbmidi_output_standard_packet,
756 };
757
758 /*
759 * AKAI MPD16 protocol:
760 *
761 * For control port (endpoint 1):
762 * ==============================
763 * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
764 * SysEx message (msg_len=9 bytes long).
765 *
766 * For data port (endpoint 2):
767 * ===========================
768 * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
769 * MIDI message (msg_len bytes long)
770 *
771 * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
772 */
snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)773 static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
774 uint8_t *buffer, int buffer_length)
775 {
776 unsigned int pos = 0;
777 unsigned int len = (unsigned int)buffer_length;
778 while (pos < len) {
779 unsigned int port = (buffer[pos] >> 4) - 1;
780 unsigned int msg_len = buffer[pos] & 0x0f;
781 pos++;
782 if (pos + msg_len <= len && port < 2)
783 snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
784 pos += msg_len;
785 }
786 }
787
788 #define MAX_AKAI_SYSEX_LEN 9
789
snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint * ep,struct urb * urb)790 static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
791 struct urb *urb)
792 {
793 uint8_t *msg;
794 int pos, end, count, buf_end;
795 uint8_t tmp[MAX_AKAI_SYSEX_LEN];
796 struct snd_rawmidi_substream *substream = ep->ports[0].substream;
797
798 if (!ep->ports[0].active)
799 return;
800
801 msg = urb->transfer_buffer + urb->transfer_buffer_length;
802 buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
803
804 /* only try adding more data when there's space for at least 1 SysEx */
805 while (urb->transfer_buffer_length < buf_end) {
806 count = snd_rawmidi_transmit_peek(substream,
807 tmp, MAX_AKAI_SYSEX_LEN);
808 if (!count) {
809 ep->ports[0].active = 0;
810 return;
811 }
812 /* try to skip non-SysEx data */
813 for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
814 ;
815
816 if (pos > 0) {
817 snd_rawmidi_transmit_ack(substream, pos);
818 continue;
819 }
820
821 /* look for the start or end marker */
822 for (end = 1; end < count && tmp[end] < 0xF0; end++)
823 ;
824
825 /* next SysEx started before the end of current one */
826 if (end < count && tmp[end] == 0xF0) {
827 /* it's incomplete - drop it */
828 snd_rawmidi_transmit_ack(substream, end);
829 continue;
830 }
831 /* SysEx complete */
832 if (end < count && tmp[end] == 0xF7) {
833 /* queue it, ack it, and get the next one */
834 count = end + 1;
835 msg[0] = 0x10 | count;
836 memcpy(&msg[1], tmp, count);
837 snd_rawmidi_transmit_ack(substream, count);
838 urb->transfer_buffer_length += count + 1;
839 msg += count + 1;
840 continue;
841 }
842 /* less than 9 bytes and no end byte - wait for more */
843 if (count < MAX_AKAI_SYSEX_LEN) {
844 ep->ports[0].active = 0;
845 return;
846 }
847 /* 9 bytes and no end marker in sight - malformed, skip it */
848 snd_rawmidi_transmit_ack(substream, count);
849 }
850 }
851
852 static const struct usb_protocol_ops snd_usbmidi_akai_ops = {
853 .input = snd_usbmidi_akai_input,
854 .output = snd_usbmidi_akai_output,
855 };
856
857 /*
858 * Novation USB MIDI protocol: number of data bytes is in the first byte
859 * (when receiving) (+1!) or in the second byte (when sending); data begins
860 * at the third byte.
861 */
862
snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)863 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint *ep,
864 uint8_t *buffer, int buffer_length)
865 {
866 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
867 return;
868 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
869 }
870
snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint * ep,struct urb * urb)871 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint *ep,
872 struct urb *urb)
873 {
874 uint8_t *transfer_buffer;
875 int count;
876
877 if (!ep->ports[0].active)
878 return;
879 transfer_buffer = urb->transfer_buffer;
880 count = snd_rawmidi_transmit(ep->ports[0].substream,
881 &transfer_buffer[2],
882 ep->max_transfer - 2);
883 if (count < 1) {
884 ep->ports[0].active = 0;
885 return;
886 }
887 transfer_buffer[0] = 0;
888 transfer_buffer[1] = count;
889 urb->transfer_buffer_length = 2 + count;
890 }
891
892 static const struct usb_protocol_ops snd_usbmidi_novation_ops = {
893 .input = snd_usbmidi_novation_input,
894 .output = snd_usbmidi_novation_output,
895 };
896
897 /*
898 * "raw" protocol: just move raw MIDI bytes from/to the endpoint
899 */
900
snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)901 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint *ep,
902 uint8_t *buffer, int buffer_length)
903 {
904 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
905 }
906
snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint * ep,struct urb * urb)907 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint *ep,
908 struct urb *urb)
909 {
910 int count;
911
912 if (!ep->ports[0].active)
913 return;
914 count = snd_rawmidi_transmit(ep->ports[0].substream,
915 urb->transfer_buffer,
916 ep->max_transfer);
917 if (count < 1) {
918 ep->ports[0].active = 0;
919 return;
920 }
921 urb->transfer_buffer_length = count;
922 }
923
924 static const struct usb_protocol_ops snd_usbmidi_raw_ops = {
925 .input = snd_usbmidi_raw_input,
926 .output = snd_usbmidi_raw_output,
927 };
928
929 /*
930 * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
931 */
932
snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)933 static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint *ep,
934 uint8_t *buffer, int buffer_length)
935 {
936 if (buffer_length > 2)
937 snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
938 }
939
940 static const struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
941 .input = snd_usbmidi_ftdi_input,
942 .output = snd_usbmidi_raw_output,
943 };
944
snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)945 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
946 uint8_t *buffer, int buffer_length)
947 {
948 if (buffer_length != 9)
949 return;
950 buffer_length = 8;
951 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
952 buffer_length--;
953 if (buffer_length)
954 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
955 }
956
snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint * ep,struct urb * urb)957 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
958 struct urb *urb)
959 {
960 int count;
961
962 if (!ep->ports[0].active)
963 return;
964 switch (snd_usb_get_speed(ep->umidi->dev)) {
965 case USB_SPEED_HIGH:
966 case USB_SPEED_SUPER:
967 case USB_SPEED_SUPER_PLUS:
968 count = 1;
969 break;
970 default:
971 count = 2;
972 }
973 count = snd_rawmidi_transmit(ep->ports[0].substream,
974 urb->transfer_buffer,
975 count);
976 if (count < 1) {
977 ep->ports[0].active = 0;
978 return;
979 }
980
981 memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
982 urb->transfer_buffer_length = ep->max_transfer;
983 }
984
985 static const struct usb_protocol_ops snd_usbmidi_122l_ops = {
986 .input = snd_usbmidi_us122l_input,
987 .output = snd_usbmidi_us122l_output,
988 };
989
990 /*
991 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
992 */
993
snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint * ep)994 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint *ep)
995 {
996 static const u8 init_data[] = {
997 /* initialization magic: "get version" */
998 0xf0,
999 0x00, 0x20, 0x31, /* Emagic */
1000 0x64, /* Unitor8 */
1001 0x0b, /* version number request */
1002 0x00, /* command version */
1003 0x00, /* EEPROM, box 0 */
1004 0xf7
1005 };
1006 send_bulk_static_data(ep, init_data, sizeof(init_data));
1007 /* while we're at it, pour on more magic */
1008 send_bulk_static_data(ep, init_data, sizeof(init_data));
1009 }
1010
snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint * ep)1011 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint *ep)
1012 {
1013 static const u8 finish_data[] = {
1014 /* switch to patch mode with last preset */
1015 0xf0,
1016 0x00, 0x20, 0x31, /* Emagic */
1017 0x64, /* Unitor8 */
1018 0x10, /* patch switch command */
1019 0x00, /* command version */
1020 0x7f, /* to all boxes */
1021 0x40, /* last preset in EEPROM */
1022 0xf7
1023 };
1024 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
1025 }
1026
snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint * ep,uint8_t * buffer,int buffer_length)1027 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint *ep,
1028 uint8_t *buffer, int buffer_length)
1029 {
1030 int i;
1031
1032 /* FF indicates end of valid data */
1033 for (i = 0; i < buffer_length; ++i)
1034 if (buffer[i] == 0xff) {
1035 buffer_length = i;
1036 break;
1037 }
1038
1039 /* handle F5 at end of last buffer */
1040 if (ep->seen_f5)
1041 goto switch_port;
1042
1043 while (buffer_length > 0) {
1044 /* determine size of data until next F5 */
1045 for (i = 0; i < buffer_length; ++i)
1046 if (buffer[i] == 0xf5)
1047 break;
1048 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
1049 buffer += i;
1050 buffer_length -= i;
1051
1052 if (buffer_length <= 0)
1053 break;
1054 /* assert(buffer[0] == 0xf5); */
1055 ep->seen_f5 = 1;
1056 ++buffer;
1057 --buffer_length;
1058
1059 switch_port:
1060 if (buffer_length <= 0)
1061 break;
1062 if (buffer[0] < 0x80) {
1063 ep->current_port = (buffer[0] - 1) & 15;
1064 ++buffer;
1065 --buffer_length;
1066 }
1067 ep->seen_f5 = 0;
1068 }
1069 }
1070
snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint * ep,struct urb * urb)1071 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint *ep,
1072 struct urb *urb)
1073 {
1074 int port0 = ep->current_port;
1075 uint8_t *buf = urb->transfer_buffer;
1076 int buf_free = ep->max_transfer;
1077 int length, i;
1078
1079 for (i = 0; i < 0x10; ++i) {
1080 /* round-robin, starting at the last current port */
1081 int portnum = (port0 + i) & 15;
1082 struct usbmidi_out_port *port = &ep->ports[portnum];
1083
1084 if (!port->active)
1085 continue;
1086 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
1087 port->active = 0;
1088 continue;
1089 }
1090
1091 if (portnum != ep->current_port) {
1092 if (buf_free < 2)
1093 break;
1094 ep->current_port = portnum;
1095 buf[0] = 0xf5;
1096 buf[1] = (portnum + 1) & 15;
1097 buf += 2;
1098 buf_free -= 2;
1099 }
1100
1101 if (buf_free < 1)
1102 break;
1103 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
1104 if (length > 0) {
1105 buf += length;
1106 buf_free -= length;
1107 if (buf_free < 1)
1108 break;
1109 }
1110 }
1111 if (buf_free < ep->max_transfer && buf_free > 0) {
1112 *buf = 0xff;
1113 --buf_free;
1114 }
1115 urb->transfer_buffer_length = ep->max_transfer - buf_free;
1116 }
1117
1118 static const struct usb_protocol_ops snd_usbmidi_emagic_ops = {
1119 .input = snd_usbmidi_emagic_input,
1120 .output = snd_usbmidi_emagic_output,
1121 .init_out_endpoint = snd_usbmidi_emagic_init_out,
1122 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
1123 };
1124
1125
update_roland_altsetting(struct snd_usb_midi * umidi)1126 static void update_roland_altsetting(struct snd_usb_midi *umidi)
1127 {
1128 struct usb_interface *intf;
1129 struct usb_host_interface *hostif;
1130 struct usb_interface_descriptor *intfd;
1131 int is_light_load;
1132
1133 intf = umidi->iface;
1134 is_light_load = intf->cur_altsetting != intf->altsetting;
1135 if (umidi->roland_load_ctl->private_value == is_light_load)
1136 return;
1137 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
1138 intfd = get_iface_desc(hostif);
1139 snd_usbmidi_input_stop(&umidi->list);
1140 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1141 intfd->bAlternateSetting);
1142 snd_usbmidi_input_start(&umidi->list);
1143 }
1144
substream_open(struct snd_rawmidi_substream * substream,int dir,int open)1145 static int substream_open(struct snd_rawmidi_substream *substream, int dir,
1146 int open)
1147 {
1148 struct snd_usb_midi *umidi = substream->rmidi->private_data;
1149 struct snd_kcontrol *ctl;
1150
1151 down_read(&umidi->disc_rwsem);
1152 if (umidi->disconnected) {
1153 up_read(&umidi->disc_rwsem);
1154 return open ? -ENODEV : 0;
1155 }
1156
1157 mutex_lock(&umidi->mutex);
1158 if (open) {
1159 if (!umidi->opened[0] && !umidi->opened[1]) {
1160 if (umidi->roland_load_ctl) {
1161 ctl = umidi->roland_load_ctl;
1162 ctl->vd[0].access |=
1163 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1164 snd_ctl_notify(umidi->card,
1165 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1166 update_roland_altsetting(umidi);
1167 }
1168 }
1169 umidi->opened[dir]++;
1170 if (umidi->opened[1])
1171 snd_usbmidi_input_start(&umidi->list);
1172 } else {
1173 umidi->opened[dir]--;
1174 if (!umidi->opened[1])
1175 snd_usbmidi_input_stop(&umidi->list);
1176 if (!umidi->opened[0] && !umidi->opened[1]) {
1177 if (umidi->roland_load_ctl) {
1178 ctl = umidi->roland_load_ctl;
1179 ctl->vd[0].access &=
1180 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1181 snd_ctl_notify(umidi->card,
1182 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1183 }
1184 }
1185 }
1186 mutex_unlock(&umidi->mutex);
1187 up_read(&umidi->disc_rwsem);
1188 return 0;
1189 }
1190
snd_usbmidi_output_open(struct snd_rawmidi_substream * substream)1191 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
1192 {
1193 struct snd_usb_midi *umidi = substream->rmidi->private_data;
1194 struct usbmidi_out_port *port = NULL;
1195 int i, j;
1196
1197 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1198 if (umidi->endpoints[i].out)
1199 for (j = 0; j < 0x10; ++j)
1200 if (umidi->endpoints[i].out->ports[j].substream == substream) {
1201 port = &umidi->endpoints[i].out->ports[j];
1202 break;
1203 }
1204 if (!port)
1205 return -ENXIO;
1206
1207 substream->runtime->private_data = port;
1208 port->state = STATE_UNKNOWN;
1209 return substream_open(substream, 0, 1);
1210 }
1211
snd_usbmidi_output_close(struct snd_rawmidi_substream * substream)1212 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
1213 {
1214 struct usbmidi_out_port *port = substream->runtime->private_data;
1215
1216 flush_work(&port->ep->work);
1217 return substream_open(substream, 0, 0);
1218 }
1219
snd_usbmidi_output_trigger(struct snd_rawmidi_substream * substream,int up)1220 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream,
1221 int up)
1222 {
1223 struct usbmidi_out_port *port =
1224 (struct usbmidi_out_port *)substream->runtime->private_data;
1225
1226 port->active = up;
1227 if (up) {
1228 if (port->ep->umidi->disconnected) {
1229 /* gobble up remaining bytes to prevent wait in
1230 * snd_rawmidi_drain_output */
1231 snd_rawmidi_proceed(substream);
1232 return;
1233 }
1234 queue_work(system_highpri_wq, &port->ep->work);
1235 }
1236 }
1237
snd_usbmidi_output_drain(struct snd_rawmidi_substream * substream)1238 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
1239 {
1240 struct usbmidi_out_port *port = substream->runtime->private_data;
1241 struct snd_usb_midi_out_endpoint *ep = port->ep;
1242 unsigned int drain_urbs;
1243 DEFINE_WAIT(wait);
1244 long timeout = msecs_to_jiffies(50);
1245
1246 if (ep->umidi->disconnected)
1247 return;
1248 /*
1249 * The substream buffer is empty, but some data might still be in the
1250 * currently active URBs, so we have to wait for those to complete.
1251 */
1252 spin_lock_irq(&ep->buffer_lock);
1253 drain_urbs = ep->active_urbs;
1254 if (drain_urbs) {
1255 ep->drain_urbs |= drain_urbs;
1256 do {
1257 prepare_to_wait(&ep->drain_wait, &wait,
1258 TASK_UNINTERRUPTIBLE);
1259 spin_unlock_irq(&ep->buffer_lock);
1260 timeout = schedule_timeout(timeout);
1261 spin_lock_irq(&ep->buffer_lock);
1262 drain_urbs &= ep->drain_urbs;
1263 } while (drain_urbs && timeout);
1264 finish_wait(&ep->drain_wait, &wait);
1265 }
1266 port->active = 0;
1267 spin_unlock_irq(&ep->buffer_lock);
1268 }
1269
snd_usbmidi_input_open(struct snd_rawmidi_substream * substream)1270 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
1271 {
1272 return substream_open(substream, 1, 1);
1273 }
1274
snd_usbmidi_input_close(struct snd_rawmidi_substream * substream)1275 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
1276 {
1277 return substream_open(substream, 1, 0);
1278 }
1279
snd_usbmidi_input_trigger(struct snd_rawmidi_substream * substream,int up)1280 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream,
1281 int up)
1282 {
1283 struct snd_usb_midi *umidi = substream->rmidi->private_data;
1284
1285 if (up)
1286 set_bit(substream->number, &umidi->input_triggered);
1287 else
1288 clear_bit(substream->number, &umidi->input_triggered);
1289 }
1290
1291 static const struct snd_rawmidi_ops snd_usbmidi_output_ops = {
1292 .open = snd_usbmidi_output_open,
1293 .close = snd_usbmidi_output_close,
1294 .trigger = snd_usbmidi_output_trigger,
1295 .drain = snd_usbmidi_output_drain,
1296 };
1297
1298 static const struct snd_rawmidi_ops snd_usbmidi_input_ops = {
1299 .open = snd_usbmidi_input_open,
1300 .close = snd_usbmidi_input_close,
1301 .trigger = snd_usbmidi_input_trigger
1302 };
1303
free_urb_and_buffer(struct snd_usb_midi * umidi,struct urb * urb,unsigned int buffer_length)1304 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1305 unsigned int buffer_length)
1306 {
1307 usb_free_coherent(umidi->dev, buffer_length,
1308 urb->transfer_buffer, urb->transfer_dma);
1309 usb_free_urb(urb);
1310 }
1311
1312 /*
1313 * Frees an input endpoint.
1314 * May be called when ep hasn't been initialized completely.
1315 */
snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint * ep)1316 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint *ep)
1317 {
1318 unsigned int i;
1319
1320 for (i = 0; i < INPUT_URBS; ++i)
1321 if (ep->urbs[i])
1322 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1323 ep->urbs[i]->transfer_buffer_length);
1324 kfree(ep);
1325 }
1326
1327 /*
1328 * Creates an input endpoint.
1329 */
snd_usbmidi_in_endpoint_create(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * ep_info,struct snd_usb_midi_endpoint * rep)1330 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi *umidi,
1331 struct snd_usb_midi_endpoint_info *ep_info,
1332 struct snd_usb_midi_endpoint *rep)
1333 {
1334 struct snd_usb_midi_in_endpoint *ep;
1335 void *buffer;
1336 unsigned int pipe;
1337 int length;
1338 unsigned int i;
1339 int err;
1340
1341 rep->in = NULL;
1342 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1343 if (!ep)
1344 return -ENOMEM;
1345 ep->umidi = umidi;
1346
1347 for (i = 0; i < INPUT_URBS; ++i) {
1348 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1349 if (!ep->urbs[i]) {
1350 err = -ENOMEM;
1351 goto error;
1352 }
1353 }
1354 if (ep_info->in_interval)
1355 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
1356 else
1357 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1358 length = usb_maxpacket(umidi->dev, pipe);
1359 for (i = 0; i < INPUT_URBS; ++i) {
1360 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1361 &ep->urbs[i]->transfer_dma);
1362 if (!buffer) {
1363 err = -ENOMEM;
1364 goto error;
1365 }
1366 if (ep_info->in_interval)
1367 usb_fill_int_urb(ep->urbs[i], umidi->dev,
1368 pipe, buffer, length,
1369 snd_usbmidi_in_urb_complete,
1370 ep, ep_info->in_interval);
1371 else
1372 usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
1373 pipe, buffer, length,
1374 snd_usbmidi_in_urb_complete, ep);
1375 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1376 err = usb_urb_ep_type_check(ep->urbs[i]);
1377 if (err < 0) {
1378 dev_err(&umidi->dev->dev, "invalid MIDI in EP %x\n",
1379 ep_info->in_ep);
1380 goto error;
1381 }
1382 }
1383
1384 rep->in = ep;
1385 return 0;
1386
1387 error:
1388 snd_usbmidi_in_endpoint_delete(ep);
1389 return err;
1390 }
1391
1392 /*
1393 * Frees an output endpoint.
1394 * May be called when ep hasn't been initialized completely.
1395 */
snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint * ep)1396 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
1397 {
1398 unsigned int i;
1399
1400 for (i = 0; i < OUTPUT_URBS; ++i)
1401 if (ep->urbs[i].urb) {
1402 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1403 ep->max_transfer);
1404 ep->urbs[i].urb = NULL;
1405 }
1406 }
1407
snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint * ep)1408 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1409 {
1410 snd_usbmidi_out_endpoint_clear(ep);
1411 kfree(ep);
1412 }
1413
1414 /*
1415 * Creates an output endpoint, and initializes output ports.
1416 */
snd_usbmidi_out_endpoint_create(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * ep_info,struct snd_usb_midi_endpoint * rep)1417 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi *umidi,
1418 struct snd_usb_midi_endpoint_info *ep_info,
1419 struct snd_usb_midi_endpoint *rep)
1420 {
1421 struct snd_usb_midi_out_endpoint *ep;
1422 unsigned int i;
1423 unsigned int pipe;
1424 void *buffer;
1425 int err;
1426
1427 rep->out = NULL;
1428 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1429 if (!ep)
1430 return -ENOMEM;
1431 ep->umidi = umidi;
1432
1433 for (i = 0; i < OUTPUT_URBS; ++i) {
1434 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1435 if (!ep->urbs[i].urb) {
1436 err = -ENOMEM;
1437 goto error;
1438 }
1439 ep->urbs[i].ep = ep;
1440 }
1441 if (ep_info->out_interval)
1442 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
1443 else
1444 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
1445 switch (umidi->usb_id) {
1446 default:
1447 ep->max_transfer = usb_maxpacket(umidi->dev, pipe);
1448 break;
1449 /*
1450 * Various chips declare a packet size larger than 4 bytes, but
1451 * do not actually work with larger packets:
1452 */
1453 case USB_ID(0x0a67, 0x5011): /* Medeli DD305 */
1454 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1455 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1456 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1457 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1458 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1459 case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
1460 ep->max_transfer = 4;
1461 break;
1462 /*
1463 * Some devices only work with 9 bytes packet size:
1464 */
1465 case USB_ID(0x0644, 0x800e): /* Tascam US-122L */
1466 case USB_ID(0x0644, 0x800f): /* Tascam US-144 */
1467 ep->max_transfer = 9;
1468 break;
1469 }
1470 for (i = 0; i < OUTPUT_URBS; ++i) {
1471 buffer = usb_alloc_coherent(umidi->dev,
1472 ep->max_transfer, GFP_KERNEL,
1473 &ep->urbs[i].urb->transfer_dma);
1474 if (!buffer) {
1475 err = -ENOMEM;
1476 goto error;
1477 }
1478 if (ep_info->out_interval)
1479 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
1480 pipe, buffer, ep->max_transfer,
1481 snd_usbmidi_out_urb_complete,
1482 &ep->urbs[i], ep_info->out_interval);
1483 else
1484 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
1485 pipe, buffer, ep->max_transfer,
1486 snd_usbmidi_out_urb_complete,
1487 &ep->urbs[i]);
1488 err = usb_urb_ep_type_check(ep->urbs[i].urb);
1489 if (err < 0) {
1490 dev_err(&umidi->dev->dev, "invalid MIDI out EP %x\n",
1491 ep_info->out_ep);
1492 goto error;
1493 }
1494 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1495 }
1496
1497 spin_lock_init(&ep->buffer_lock);
1498 INIT_WORK(&ep->work, snd_usbmidi_out_work);
1499 init_waitqueue_head(&ep->drain_wait);
1500
1501 for (i = 0; i < 0x10; ++i)
1502 if (ep_info->out_cables & (1 << i)) {
1503 ep->ports[i].ep = ep;
1504 ep->ports[i].cable = i << 4;
1505 }
1506
1507 if (umidi->usb_protocol_ops->init_out_endpoint)
1508 umidi->usb_protocol_ops->init_out_endpoint(ep);
1509
1510 rep->out = ep;
1511 return 0;
1512
1513 error:
1514 snd_usbmidi_out_endpoint_delete(ep);
1515 return err;
1516 }
1517
1518 /*
1519 * Frees everything.
1520 */
snd_usbmidi_free(struct snd_usb_midi * umidi)1521 static void snd_usbmidi_free(struct snd_usb_midi *umidi)
1522 {
1523 int i;
1524
1525 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1526 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
1527 if (ep->out)
1528 snd_usbmidi_out_endpoint_delete(ep->out);
1529 if (ep->in)
1530 snd_usbmidi_in_endpoint_delete(ep->in);
1531 }
1532 mutex_destroy(&umidi->mutex);
1533 kfree(umidi);
1534 }
1535
1536 /*
1537 * Unlinks all URBs (must be done before the usb_device is deleted).
1538 */
snd_usbmidi_disconnect(struct list_head * p)1539 void snd_usbmidi_disconnect(struct list_head *p)
1540 {
1541 struct snd_usb_midi *umidi;
1542 unsigned int i, j;
1543
1544 umidi = list_entry(p, struct snd_usb_midi, list);
1545 /*
1546 * an URB's completion handler may start the timer and
1547 * a timer may submit an URB. To reliably break the cycle
1548 * a flag under lock must be used
1549 */
1550 down_write(&umidi->disc_rwsem);
1551 spin_lock_irq(&umidi->disc_lock);
1552 umidi->disconnected = 1;
1553 spin_unlock_irq(&umidi->disc_lock);
1554 up_write(&umidi->disc_rwsem);
1555
1556 del_timer_sync(&umidi->error_timer);
1557
1558 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1559 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
1560 if (ep->out)
1561 cancel_work_sync(&ep->out->work);
1562 if (ep->out) {
1563 for (j = 0; j < OUTPUT_URBS; ++j)
1564 usb_kill_urb(ep->out->urbs[j].urb);
1565 if (umidi->usb_protocol_ops->finish_out_endpoint)
1566 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1567 ep->out->active_urbs = 0;
1568 if (ep->out->drain_urbs) {
1569 ep->out->drain_urbs = 0;
1570 wake_up(&ep->out->drain_wait);
1571 }
1572 }
1573 if (ep->in)
1574 for (j = 0; j < INPUT_URBS; ++j)
1575 usb_kill_urb(ep->in->urbs[j]);
1576 /* free endpoints here; later call can result in Oops */
1577 if (ep->out)
1578 snd_usbmidi_out_endpoint_clear(ep->out);
1579 if (ep->in) {
1580 snd_usbmidi_in_endpoint_delete(ep->in);
1581 ep->in = NULL;
1582 }
1583 }
1584 }
1585 EXPORT_SYMBOL(snd_usbmidi_disconnect);
1586
snd_usbmidi_rawmidi_free(struct snd_rawmidi * rmidi)1587 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1588 {
1589 struct snd_usb_midi *umidi = rmidi->private_data;
1590 snd_usbmidi_free(umidi);
1591 }
1592
snd_usbmidi_find_substream(struct snd_usb_midi * umidi,int stream,int number)1593 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi *umidi,
1594 int stream,
1595 int number)
1596 {
1597 struct snd_rawmidi_substream *substream;
1598
1599 list_for_each_entry(substream, &umidi->rmidi->streams[stream].substreams,
1600 list) {
1601 if (substream->number == number)
1602 return substream;
1603 }
1604 return NULL;
1605 }
1606
1607 /*
1608 * This list specifies names for ports that do not fit into the standard
1609 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1610 * such as internal control or synthesizer ports.
1611 */
1612 static struct port_info {
1613 u32 id;
1614 short int port;
1615 short int voices;
1616 const char *name;
1617 unsigned int seq_flags;
1618 } snd_usbmidi_port_info[] = {
1619 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1620 { .id = USB_ID(vendor, product), \
1621 .port = num, .voices = voices_, \
1622 .name = name_, .seq_flags = flags }
1623 #define EXTERNAL_PORT(vendor, product, num, name) \
1624 PORT_INFO(vendor, product, num, name, 0, \
1625 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1626 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1627 SNDRV_SEQ_PORT_TYPE_PORT)
1628 #define CONTROL_PORT(vendor, product, num, name) \
1629 PORT_INFO(vendor, product, num, name, 0, \
1630 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1631 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1632 #define GM_SYNTH_PORT(vendor, product, num, name, voices) \
1633 PORT_INFO(vendor, product, num, name, voices, \
1634 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1635 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1636 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1637 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1638 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1639 PORT_INFO(vendor, product, num, name, voices, \
1640 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1641 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1642 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1643 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1644 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1645 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1646 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1647 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1648 PORT_INFO(vendor, product, num, name, voices, \
1649 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1650 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1651 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1652 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1653 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1654 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1655 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1656 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1657 /* Yamaha MOTIF XF */
1658 GM_SYNTH_PORT(0x0499, 0x105c, 0, "%s Tone Generator", 128),
1659 CONTROL_PORT(0x0499, 0x105c, 1, "%s Remote Control"),
1660 EXTERNAL_PORT(0x0499, 0x105c, 2, "%s Thru"),
1661 CONTROL_PORT(0x0499, 0x105c, 3, "%s Editor"),
1662 /* Roland UA-100 */
1663 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1664 /* Roland SC-8850 */
1665 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1666 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1667 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1668 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1669 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1670 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1671 /* Roland U-8 */
1672 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1673 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1674 /* Roland SC-8820 */
1675 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1676 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1677 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1678 /* Roland SK-500 */
1679 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1680 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1681 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1682 /* Roland SC-D70 */
1683 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1684 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1685 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1686 /* Edirol UM-880 */
1687 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1688 /* Edirol SD-90 */
1689 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1690 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1691 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1692 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1693 /* Edirol UM-550 */
1694 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1695 /* Edirol SD-20 */
1696 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1697 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1698 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1699 /* Edirol SD-80 */
1700 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1701 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1702 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1703 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1704 /* Edirol UA-700 */
1705 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1706 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1707 /* Roland VariOS */
1708 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1709 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1710 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1711 /* Edirol PCR */
1712 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1713 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1714 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1715 /* BOSS GS-10 */
1716 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1717 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1718 /* Edirol UA-1000 */
1719 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1720 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1721 /* Edirol UR-80 */
1722 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1723 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1724 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1725 /* Edirol PCR-A */
1726 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1727 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1728 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1729 /* BOSS GT-PRO */
1730 CONTROL_PORT(0x0582, 0x0089, 0, "%s Control"),
1731 /* Edirol UM-3EX */
1732 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1733 /* Roland VG-99 */
1734 CONTROL_PORT(0x0582, 0x00b2, 0, "%s Control"),
1735 EXTERNAL_PORT(0x0582, 0x00b2, 1, "%s MIDI"),
1736 /* Cakewalk Sonar V-Studio 100 */
1737 EXTERNAL_PORT(0x0582, 0x00eb, 0, "%s MIDI"),
1738 CONTROL_PORT(0x0582, 0x00eb, 1, "%s Control"),
1739 /* Roland VB-99 */
1740 CONTROL_PORT(0x0582, 0x0102, 0, "%s Control"),
1741 EXTERNAL_PORT(0x0582, 0x0102, 1, "%s MIDI"),
1742 /* Roland A-PRO */
1743 EXTERNAL_PORT(0x0582, 0x010f, 0, "%s MIDI"),
1744 CONTROL_PORT(0x0582, 0x010f, 1, "%s 1"),
1745 CONTROL_PORT(0x0582, 0x010f, 2, "%s 2"),
1746 /* Roland SD-50 */
1747 ROLAND_SYNTH_PORT(0x0582, 0x0114, 0, "%s Synth", 128),
1748 EXTERNAL_PORT(0x0582, 0x0114, 1, "%s MIDI"),
1749 CONTROL_PORT(0x0582, 0x0114, 2, "%s Control"),
1750 /* Roland OCTA-CAPTURE */
1751 EXTERNAL_PORT(0x0582, 0x0120, 0, "%s MIDI"),
1752 CONTROL_PORT(0x0582, 0x0120, 1, "%s Control"),
1753 EXTERNAL_PORT(0x0582, 0x0121, 0, "%s MIDI"),
1754 CONTROL_PORT(0x0582, 0x0121, 1, "%s Control"),
1755 /* Roland SPD-SX */
1756 CONTROL_PORT(0x0582, 0x0145, 0, "%s Control"),
1757 EXTERNAL_PORT(0x0582, 0x0145, 1, "%s MIDI"),
1758 /* Roland A-Series */
1759 CONTROL_PORT(0x0582, 0x0156, 0, "%s Keyboard"),
1760 EXTERNAL_PORT(0x0582, 0x0156, 1, "%s MIDI"),
1761 /* Roland INTEGRA-7 */
1762 ROLAND_SYNTH_PORT(0x0582, 0x015b, 0, "%s Synth", 128),
1763 CONTROL_PORT(0x0582, 0x015b, 1, "%s Control"),
1764 /* M-Audio MidiSport 8x8 */
1765 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1766 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1767 /* MOTU Fastlane */
1768 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1769 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1770 /* Emagic Unitor8/AMT8/MT4 */
1771 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1772 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1773 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1774 /* Akai MPD16 */
1775 CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1776 PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1777 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1778 SNDRV_SEQ_PORT_TYPE_HARDWARE),
1779 /* Access Music Virus TI */
1780 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1781 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1782 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1783 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1784 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
1785 };
1786
find_port_info(struct snd_usb_midi * umidi,int number)1787 static struct port_info *find_port_info(struct snd_usb_midi *umidi, int number)
1788 {
1789 int i;
1790
1791 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1792 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
1793 snd_usbmidi_port_info[i].port == number)
1794 return &snd_usbmidi_port_info[i];
1795 }
1796 return NULL;
1797 }
1798
snd_usbmidi_get_port_info(struct snd_rawmidi * rmidi,int number,struct snd_seq_port_info * seq_port_info)1799 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1800 struct snd_seq_port_info *seq_port_info)
1801 {
1802 struct snd_usb_midi *umidi = rmidi->private_data;
1803 struct port_info *port_info;
1804
1805 /* TODO: read port flags from descriptors */
1806 port_info = find_port_info(umidi, number);
1807 if (port_info) {
1808 seq_port_info->type = port_info->seq_flags;
1809 seq_port_info->midi_voices = port_info->voices;
1810 }
1811 }
1812
find_usb_in_jack_descriptor(struct usb_host_interface * hostif,uint8_t jack_id)1813 static struct usb_midi_in_jack_descriptor *find_usb_in_jack_descriptor(
1814 struct usb_host_interface *hostif, uint8_t jack_id)
1815 {
1816 unsigned char *extra = hostif->extra;
1817 int extralen = hostif->extralen;
1818
1819 while (extralen > 4) {
1820 struct usb_midi_in_jack_descriptor *injd =
1821 (struct usb_midi_in_jack_descriptor *)extra;
1822
1823 if (injd->bLength >= sizeof(*injd) &&
1824 injd->bDescriptorType == USB_DT_CS_INTERFACE &&
1825 injd->bDescriptorSubtype == UAC_MIDI_IN_JACK &&
1826 injd->bJackID == jack_id)
1827 return injd;
1828 if (!extra[0])
1829 break;
1830 extralen -= extra[0];
1831 extra += extra[0];
1832 }
1833 return NULL;
1834 }
1835
find_usb_out_jack_descriptor(struct usb_host_interface * hostif,uint8_t jack_id)1836 static struct usb_midi_out_jack_descriptor *find_usb_out_jack_descriptor(
1837 struct usb_host_interface *hostif, uint8_t jack_id)
1838 {
1839 unsigned char *extra = hostif->extra;
1840 int extralen = hostif->extralen;
1841
1842 while (extralen > 4) {
1843 struct usb_midi_out_jack_descriptor *outjd =
1844 (struct usb_midi_out_jack_descriptor *)extra;
1845
1846 if (outjd->bLength >= sizeof(*outjd) &&
1847 outjd->bDescriptorType == USB_DT_CS_INTERFACE &&
1848 outjd->bDescriptorSubtype == UAC_MIDI_OUT_JACK &&
1849 outjd->bJackID == jack_id)
1850 return outjd;
1851 if (!extra[0])
1852 break;
1853 extralen -= extra[0];
1854 extra += extra[0];
1855 }
1856 return NULL;
1857 }
1858
snd_usbmidi_init_substream(struct snd_usb_midi * umidi,int stream,int number,int jack_id,struct snd_rawmidi_substream ** rsubstream)1859 static void snd_usbmidi_init_substream(struct snd_usb_midi *umidi,
1860 int stream, int number, int jack_id,
1861 struct snd_rawmidi_substream **rsubstream)
1862 {
1863 struct port_info *port_info;
1864 const char *name_format;
1865 struct usb_interface *intf;
1866 struct usb_host_interface *hostif;
1867 struct usb_midi_in_jack_descriptor *injd;
1868 struct usb_midi_out_jack_descriptor *outjd;
1869 uint8_t jack_name_buf[32];
1870 uint8_t *default_jack_name = "MIDI";
1871 uint8_t *jack_name = default_jack_name;
1872 uint8_t iJack;
1873 size_t sz;
1874 int res;
1875
1876 struct snd_rawmidi_substream *substream =
1877 snd_usbmidi_find_substream(umidi, stream, number);
1878 if (!substream) {
1879 dev_err(&umidi->dev->dev, "substream %d:%d not found\n", stream,
1880 number);
1881 return;
1882 }
1883
1884 intf = umidi->iface;
1885 if (intf && jack_id >= 0) {
1886 hostif = intf->cur_altsetting;
1887 iJack = 0;
1888 if (stream != SNDRV_RAWMIDI_STREAM_OUTPUT) {
1889 /* in jacks connect to outs */
1890 outjd = find_usb_out_jack_descriptor(hostif, jack_id);
1891 if (outjd) {
1892 sz = USB_DT_MIDI_OUT_SIZE(outjd->bNrInputPins);
1893 if (outjd->bLength >= sz)
1894 iJack = *(((uint8_t *) outjd) + sz - sizeof(uint8_t));
1895 }
1896 } else {
1897 /* and out jacks connect to ins */
1898 injd = find_usb_in_jack_descriptor(hostif, jack_id);
1899 if (injd)
1900 iJack = injd->iJack;
1901 }
1902 if (iJack != 0) {
1903 res = usb_string(umidi->dev, iJack, jack_name_buf,
1904 ARRAY_SIZE(jack_name_buf));
1905 if (res)
1906 jack_name = jack_name_buf;
1907 }
1908 }
1909
1910 port_info = find_port_info(umidi, number);
1911 name_format = port_info ? port_info->name :
1912 (jack_name != default_jack_name ? "%s %s" : "%s %s %d");
1913 snprintf(substream->name, sizeof(substream->name),
1914 name_format, umidi->card->shortname, jack_name, number + 1);
1915
1916 *rsubstream = substream;
1917 }
1918
1919 /*
1920 * Creates the endpoints and their ports.
1921 */
snd_usbmidi_create_endpoints(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoints)1922 static int snd_usbmidi_create_endpoints(struct snd_usb_midi *umidi,
1923 struct snd_usb_midi_endpoint_info *endpoints)
1924 {
1925 int i, j, err;
1926 int out_ports = 0, in_ports = 0;
1927
1928 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1929 if (endpoints[i].out_cables) {
1930 err = snd_usbmidi_out_endpoint_create(umidi,
1931 &endpoints[i],
1932 &umidi->endpoints[i]);
1933 if (err < 0)
1934 return err;
1935 }
1936 if (endpoints[i].in_cables) {
1937 err = snd_usbmidi_in_endpoint_create(umidi,
1938 &endpoints[i],
1939 &umidi->endpoints[i]);
1940 if (err < 0)
1941 return err;
1942 }
1943
1944 for (j = 0; j < 0x10; ++j) {
1945 if (endpoints[i].out_cables & (1 << j)) {
1946 snd_usbmidi_init_substream(umidi,
1947 SNDRV_RAWMIDI_STREAM_OUTPUT,
1948 out_ports,
1949 endpoints[i].assoc_out_jacks[j],
1950 &umidi->endpoints[i].out->ports[j].substream);
1951 ++out_ports;
1952 }
1953 if (endpoints[i].in_cables & (1 << j)) {
1954 snd_usbmidi_init_substream(umidi,
1955 SNDRV_RAWMIDI_STREAM_INPUT,
1956 in_ports,
1957 endpoints[i].assoc_in_jacks[j],
1958 &umidi->endpoints[i].in->ports[j].substream);
1959 ++in_ports;
1960 }
1961 }
1962 }
1963 dev_dbg(&umidi->dev->dev, "created %d output and %d input ports\n",
1964 out_ports, in_ports);
1965 return 0;
1966 }
1967
find_usb_ms_endpoint_descriptor(struct usb_host_endpoint * hostep)1968 static struct usb_ms_endpoint_descriptor *find_usb_ms_endpoint_descriptor(
1969 struct usb_host_endpoint *hostep)
1970 {
1971 unsigned char *extra = hostep->extra;
1972 int extralen = hostep->extralen;
1973
1974 while (extralen > 3) {
1975 struct usb_ms_endpoint_descriptor *ms_ep =
1976 (struct usb_ms_endpoint_descriptor *)extra;
1977
1978 if (ms_ep->bLength > 3 &&
1979 ms_ep->bDescriptorType == USB_DT_CS_ENDPOINT &&
1980 ms_ep->bDescriptorSubtype == UAC_MS_GENERAL)
1981 return ms_ep;
1982 if (!extra[0])
1983 break;
1984 extralen -= extra[0];
1985 extra += extra[0];
1986 }
1987 return NULL;
1988 }
1989
1990 /*
1991 * Returns MIDIStreaming device capabilities.
1992 */
snd_usbmidi_get_ms_info(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoints)1993 static int snd_usbmidi_get_ms_info(struct snd_usb_midi *umidi,
1994 struct snd_usb_midi_endpoint_info *endpoints)
1995 {
1996 struct usb_interface *intf;
1997 struct usb_host_interface *hostif;
1998 struct usb_interface_descriptor *intfd;
1999 struct usb_ms_header_descriptor *ms_header;
2000 struct usb_host_endpoint *hostep;
2001 struct usb_endpoint_descriptor *ep;
2002 struct usb_ms_endpoint_descriptor *ms_ep;
2003 int i, j, epidx;
2004
2005 intf = umidi->iface;
2006 if (!intf)
2007 return -ENXIO;
2008 hostif = &intf->altsetting[0];
2009 intfd = get_iface_desc(hostif);
2010 ms_header = (struct usb_ms_header_descriptor *)hostif->extra;
2011 if (hostif->extralen >= 7 &&
2012 ms_header->bLength >= 7 &&
2013 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
2014 ms_header->bDescriptorSubtype == UAC_HEADER)
2015 dev_dbg(&umidi->dev->dev, "MIDIStreaming version %02x.%02x\n",
2016 ((uint8_t *)&ms_header->bcdMSC)[1], ((uint8_t *)&ms_header->bcdMSC)[0]);
2017 else
2018 dev_warn(&umidi->dev->dev,
2019 "MIDIStreaming interface descriptor not found\n");
2020
2021 epidx = 0;
2022 for (i = 0; i < intfd->bNumEndpoints; ++i) {
2023 hostep = &hostif->endpoint[i];
2024 ep = get_ep_desc(hostep);
2025 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
2026 continue;
2027 ms_ep = find_usb_ms_endpoint_descriptor(hostep);
2028 if (!ms_ep)
2029 continue;
2030 if (ms_ep->bLength <= sizeof(*ms_ep))
2031 continue;
2032 if (ms_ep->bNumEmbMIDIJack > 0x10)
2033 continue;
2034 if (ms_ep->bLength < sizeof(*ms_ep) + ms_ep->bNumEmbMIDIJack)
2035 continue;
2036 if (usb_endpoint_dir_out(ep)) {
2037 if (endpoints[epidx].out_ep) {
2038 if (++epidx >= MIDI_MAX_ENDPOINTS) {
2039 dev_warn(&umidi->dev->dev,
2040 "too many endpoints\n");
2041 break;
2042 }
2043 }
2044 endpoints[epidx].out_ep = usb_endpoint_num(ep);
2045 if (usb_endpoint_xfer_int(ep))
2046 endpoints[epidx].out_interval = ep->bInterval;
2047 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
2048 /*
2049 * Low speed bulk transfers don't exist, so
2050 * force interrupt transfers for devices like
2051 * ESI MIDI Mate that try to use them anyway.
2052 */
2053 endpoints[epidx].out_interval = 1;
2054 endpoints[epidx].out_cables =
2055 (1 << ms_ep->bNumEmbMIDIJack) - 1;
2056 for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j)
2057 endpoints[epidx].assoc_out_jacks[j] = ms_ep->baAssocJackID[j];
2058 for (; j < ARRAY_SIZE(endpoints[epidx].assoc_out_jacks); ++j)
2059 endpoints[epidx].assoc_out_jacks[j] = -1;
2060 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
2061 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
2062 } else {
2063 if (endpoints[epidx].in_ep) {
2064 if (++epidx >= MIDI_MAX_ENDPOINTS) {
2065 dev_warn(&umidi->dev->dev,
2066 "too many endpoints\n");
2067 break;
2068 }
2069 }
2070 endpoints[epidx].in_ep = usb_endpoint_num(ep);
2071 if (usb_endpoint_xfer_int(ep))
2072 endpoints[epidx].in_interval = ep->bInterval;
2073 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
2074 endpoints[epidx].in_interval = 1;
2075 endpoints[epidx].in_cables =
2076 (1 << ms_ep->bNumEmbMIDIJack) - 1;
2077 for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j)
2078 endpoints[epidx].assoc_in_jacks[j] = ms_ep->baAssocJackID[j];
2079 for (; j < ARRAY_SIZE(endpoints[epidx].assoc_in_jacks); ++j)
2080 endpoints[epidx].assoc_in_jacks[j] = -1;
2081 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
2082 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
2083 }
2084 }
2085 return 0;
2086 }
2087
roland_load_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * info)2088 static int roland_load_info(struct snd_kcontrol *kcontrol,
2089 struct snd_ctl_elem_info *info)
2090 {
2091 static const char *const names[] = { "High Load", "Light Load" };
2092
2093 return snd_ctl_enum_info(info, 1, 2, names);
2094 }
2095
roland_load_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)2096 static int roland_load_get(struct snd_kcontrol *kcontrol,
2097 struct snd_ctl_elem_value *value)
2098 {
2099 value->value.enumerated.item[0] = kcontrol->private_value;
2100 return 0;
2101 }
2102
roland_load_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)2103 static int roland_load_put(struct snd_kcontrol *kcontrol,
2104 struct snd_ctl_elem_value *value)
2105 {
2106 struct snd_usb_midi *umidi = kcontrol->private_data;
2107 int changed;
2108
2109 if (value->value.enumerated.item[0] > 1)
2110 return -EINVAL;
2111 mutex_lock(&umidi->mutex);
2112 changed = value->value.enumerated.item[0] != kcontrol->private_value;
2113 if (changed)
2114 kcontrol->private_value = value->value.enumerated.item[0];
2115 mutex_unlock(&umidi->mutex);
2116 return changed;
2117 }
2118
2119 static const struct snd_kcontrol_new roland_load_ctl = {
2120 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2121 .name = "MIDI Input Mode",
2122 .info = roland_load_info,
2123 .get = roland_load_get,
2124 .put = roland_load_put,
2125 .private_value = 1,
2126 };
2127
2128 /*
2129 * On Roland devices, use the second alternate setting to be able to use
2130 * the interrupt input endpoint.
2131 */
snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi * umidi)2132 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi *umidi)
2133 {
2134 struct usb_interface *intf;
2135 struct usb_host_interface *hostif;
2136 struct usb_interface_descriptor *intfd;
2137
2138 intf = umidi->iface;
2139 if (!intf || intf->num_altsetting != 2)
2140 return;
2141
2142 hostif = &intf->altsetting[1];
2143 intfd = get_iface_desc(hostif);
2144 /* If either or both of the endpoints support interrupt transfer,
2145 * then use the alternate setting
2146 */
2147 if (intfd->bNumEndpoints != 2 ||
2148 !((get_endpoint(hostif, 0)->bmAttributes &
2149 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT ||
2150 (get_endpoint(hostif, 1)->bmAttributes &
2151 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
2152 return;
2153
2154 dev_dbg(&umidi->dev->dev, "switching to altsetting %d with int ep\n",
2155 intfd->bAlternateSetting);
2156 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
2157 intfd->bAlternateSetting);
2158
2159 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
2160 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
2161 umidi->roland_load_ctl = NULL;
2162 }
2163
2164 /*
2165 * Try to find any usable endpoints in the interface.
2166 */
snd_usbmidi_detect_endpoints(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoint,int max_endpoints)2167 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi *umidi,
2168 struct snd_usb_midi_endpoint_info *endpoint,
2169 int max_endpoints)
2170 {
2171 struct usb_interface *intf;
2172 struct usb_host_interface *hostif;
2173 struct usb_interface_descriptor *intfd;
2174 struct usb_endpoint_descriptor *epd;
2175 int i, out_eps = 0, in_eps = 0;
2176
2177 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
2178 snd_usbmidi_switch_roland_altsetting(umidi);
2179
2180 if (endpoint[0].out_ep || endpoint[0].in_ep)
2181 return 0;
2182
2183 intf = umidi->iface;
2184 if (!intf || intf->num_altsetting < 1)
2185 return -ENOENT;
2186 hostif = intf->cur_altsetting;
2187 intfd = get_iface_desc(hostif);
2188
2189 for (i = 0; i < intfd->bNumEndpoints; ++i) {
2190 epd = get_endpoint(hostif, i);
2191 if (!usb_endpoint_xfer_bulk(epd) &&
2192 !usb_endpoint_xfer_int(epd))
2193 continue;
2194 if (out_eps < max_endpoints &&
2195 usb_endpoint_dir_out(epd)) {
2196 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
2197 if (usb_endpoint_xfer_int(epd))
2198 endpoint[out_eps].out_interval = epd->bInterval;
2199 ++out_eps;
2200 }
2201 if (in_eps < max_endpoints &&
2202 usb_endpoint_dir_in(epd)) {
2203 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
2204 if (usb_endpoint_xfer_int(epd))
2205 endpoint[in_eps].in_interval = epd->bInterval;
2206 ++in_eps;
2207 }
2208 }
2209 return (out_eps || in_eps) ? 0 : -ENOENT;
2210 }
2211
2212 /*
2213 * Detects the endpoints for one-port-per-endpoint protocols.
2214 */
snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoints)2215 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi *umidi,
2216 struct snd_usb_midi_endpoint_info *endpoints)
2217 {
2218 int err, i;
2219
2220 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
2221 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2222 if (endpoints[i].out_ep)
2223 endpoints[i].out_cables = 0x0001;
2224 if (endpoints[i].in_ep)
2225 endpoints[i].in_cables = 0x0001;
2226 }
2227 return err;
2228 }
2229
2230 /*
2231 * Detects the endpoints and ports of Yamaha devices.
2232 */
snd_usbmidi_detect_yamaha(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoint)2233 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi *umidi,
2234 struct snd_usb_midi_endpoint_info *endpoint)
2235 {
2236 struct usb_interface *intf;
2237 struct usb_host_interface *hostif;
2238 struct usb_interface_descriptor *intfd;
2239 uint8_t *cs_desc;
2240
2241 intf = umidi->iface;
2242 if (!intf)
2243 return -ENOENT;
2244 hostif = intf->altsetting;
2245 intfd = get_iface_desc(hostif);
2246 if (intfd->bNumEndpoints < 1)
2247 return -ENOENT;
2248
2249 /*
2250 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
2251 * necessarily with any useful contents. So simply count 'em.
2252 */
2253 for (cs_desc = hostif->extra;
2254 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
2255 cs_desc += cs_desc[0]) {
2256 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
2257 if (cs_desc[2] == UAC_MIDI_IN_JACK)
2258 endpoint->in_cables =
2259 (endpoint->in_cables << 1) | 1;
2260 else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
2261 endpoint->out_cables =
2262 (endpoint->out_cables << 1) | 1;
2263 }
2264 }
2265 if (!endpoint->in_cables && !endpoint->out_cables)
2266 return -ENOENT;
2267
2268 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
2269 }
2270
2271 /*
2272 * Detects the endpoints and ports of Roland devices.
2273 */
snd_usbmidi_detect_roland(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoint)2274 static int snd_usbmidi_detect_roland(struct snd_usb_midi *umidi,
2275 struct snd_usb_midi_endpoint_info *endpoint)
2276 {
2277 struct usb_interface *intf;
2278 struct usb_host_interface *hostif;
2279 u8 *cs_desc;
2280
2281 intf = umidi->iface;
2282 if (!intf)
2283 return -ENOENT;
2284 hostif = intf->altsetting;
2285 /*
2286 * Some devices have a descriptor <06 24 F1 02 <inputs> <outputs>>,
2287 * some have standard class descriptors, or both kinds, or neither.
2288 */
2289 for (cs_desc = hostif->extra;
2290 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
2291 cs_desc += cs_desc[0]) {
2292 if (cs_desc[0] >= 6 &&
2293 cs_desc[1] == USB_DT_CS_INTERFACE &&
2294 cs_desc[2] == 0xf1 &&
2295 cs_desc[3] == 0x02) {
2296 if (cs_desc[4] > 0x10 || cs_desc[5] > 0x10)
2297 continue;
2298 endpoint->in_cables = (1 << cs_desc[4]) - 1;
2299 endpoint->out_cables = (1 << cs_desc[5]) - 1;
2300 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
2301 } else if (cs_desc[0] >= 7 &&
2302 cs_desc[1] == USB_DT_CS_INTERFACE &&
2303 cs_desc[2] == UAC_HEADER) {
2304 return snd_usbmidi_get_ms_info(umidi, endpoint);
2305 }
2306 }
2307
2308 return -ENODEV;
2309 }
2310
2311 /*
2312 * Creates the endpoints and their ports for Midiman devices.
2313 */
snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoint)2314 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi *umidi,
2315 struct snd_usb_midi_endpoint_info *endpoint)
2316 {
2317 struct snd_usb_midi_endpoint_info ep_info;
2318 struct usb_interface *intf;
2319 struct usb_host_interface *hostif;
2320 struct usb_interface_descriptor *intfd;
2321 struct usb_endpoint_descriptor *epd;
2322 int cable, err;
2323
2324 intf = umidi->iface;
2325 if (!intf)
2326 return -ENOENT;
2327 hostif = intf->altsetting;
2328 intfd = get_iface_desc(hostif);
2329 /*
2330 * The various MidiSport devices have more or less random endpoint
2331 * numbers, so we have to identify the endpoints by their index in
2332 * the descriptor array, like the driver for that other OS does.
2333 *
2334 * There is one interrupt input endpoint for all input ports, one
2335 * bulk output endpoint for even-numbered ports, and one for odd-
2336 * numbered ports. Both bulk output endpoints have corresponding
2337 * input bulk endpoints (at indices 1 and 3) which aren't used.
2338 */
2339 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
2340 dev_dbg(&umidi->dev->dev, "not enough endpoints\n");
2341 return -ENOENT;
2342 }
2343
2344 epd = get_endpoint(hostif, 0);
2345 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
2346 dev_dbg(&umidi->dev->dev, "endpoint[0] isn't interrupt\n");
2347 return -ENXIO;
2348 }
2349 epd = get_endpoint(hostif, 2);
2350 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
2351 dev_dbg(&umidi->dev->dev, "endpoint[2] isn't bulk output\n");
2352 return -ENXIO;
2353 }
2354 if (endpoint->out_cables > 0x0001) {
2355 epd = get_endpoint(hostif, 4);
2356 if (!usb_endpoint_dir_out(epd) ||
2357 !usb_endpoint_xfer_bulk(epd)) {
2358 dev_dbg(&umidi->dev->dev,
2359 "endpoint[4] isn't bulk output\n");
2360 return -ENXIO;
2361 }
2362 }
2363
2364 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress &
2365 USB_ENDPOINT_NUMBER_MASK;
2366 ep_info.out_interval = 0;
2367 ep_info.out_cables = endpoint->out_cables & 0x5555;
2368 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
2369 &umidi->endpoints[0]);
2370 if (err < 0)
2371 return err;
2372
2373 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress &
2374 USB_ENDPOINT_NUMBER_MASK;
2375 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
2376 ep_info.in_cables = endpoint->in_cables;
2377 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info,
2378 &umidi->endpoints[0]);
2379 if (err < 0)
2380 return err;
2381
2382 if (endpoint->out_cables > 0x0001) {
2383 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress &
2384 USB_ENDPOINT_NUMBER_MASK;
2385 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
2386 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
2387 &umidi->endpoints[1]);
2388 if (err < 0)
2389 return err;
2390 }
2391
2392 for (cable = 0; cable < 0x10; ++cable) {
2393 if (endpoint->out_cables & (1 << cable))
2394 snd_usbmidi_init_substream(umidi,
2395 SNDRV_RAWMIDI_STREAM_OUTPUT,
2396 cable,
2397 -1 /* prevent trying to find jack */,
2398 &umidi->endpoints[cable & 1].out->ports[cable].substream);
2399 if (endpoint->in_cables & (1 << cable))
2400 snd_usbmidi_init_substream(umidi,
2401 SNDRV_RAWMIDI_STREAM_INPUT,
2402 cable,
2403 -1 /* prevent trying to find jack */,
2404 &umidi->endpoints[0].in->ports[cable].substream);
2405 }
2406 return 0;
2407 }
2408
2409 static const struct snd_rawmidi_global_ops snd_usbmidi_ops = {
2410 .get_port_info = snd_usbmidi_get_port_info,
2411 };
2412
snd_usbmidi_create_rawmidi(struct snd_usb_midi * umidi,int out_ports,int in_ports)2413 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi *umidi,
2414 int out_ports, int in_ports)
2415 {
2416 struct snd_rawmidi *rmidi;
2417 int err;
2418
2419 err = snd_rawmidi_new(umidi->card, "USB MIDI",
2420 umidi->next_midi_device++,
2421 out_ports, in_ports, &rmidi);
2422 if (err < 0)
2423 return err;
2424 strcpy(rmidi->name, umidi->card->shortname);
2425 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2426 SNDRV_RAWMIDI_INFO_INPUT |
2427 SNDRV_RAWMIDI_INFO_DUPLEX;
2428 rmidi->ops = &snd_usbmidi_ops;
2429 rmidi->private_data = umidi;
2430 rmidi->private_free = snd_usbmidi_rawmidi_free;
2431 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
2432 &snd_usbmidi_output_ops);
2433 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
2434 &snd_usbmidi_input_ops);
2435
2436 umidi->rmidi = rmidi;
2437 return 0;
2438 }
2439
2440 /*
2441 * Temporarily stop input.
2442 */
snd_usbmidi_input_stop(struct list_head * p)2443 void snd_usbmidi_input_stop(struct list_head *p)
2444 {
2445 struct snd_usb_midi *umidi;
2446 unsigned int i, j;
2447
2448 umidi = list_entry(p, struct snd_usb_midi, list);
2449 if (!umidi->input_running)
2450 return;
2451 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2452 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
2453 if (ep->in)
2454 for (j = 0; j < INPUT_URBS; ++j)
2455 usb_kill_urb(ep->in->urbs[j]);
2456 }
2457 umidi->input_running = 0;
2458 }
2459 EXPORT_SYMBOL(snd_usbmidi_input_stop);
2460
snd_usbmidi_input_start_ep(struct snd_usb_midi * umidi,struct snd_usb_midi_in_endpoint * ep)2461 static void snd_usbmidi_input_start_ep(struct snd_usb_midi *umidi,
2462 struct snd_usb_midi_in_endpoint *ep)
2463 {
2464 unsigned int i;
2465 unsigned long flags;
2466
2467 if (!ep)
2468 return;
2469 for (i = 0; i < INPUT_URBS; ++i) {
2470 struct urb *urb = ep->urbs[i];
2471 spin_lock_irqsave(&umidi->disc_lock, flags);
2472 if (!atomic_read(&urb->use_count)) {
2473 urb->dev = ep->umidi->dev;
2474 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
2475 }
2476 spin_unlock_irqrestore(&umidi->disc_lock, flags);
2477 }
2478 }
2479
2480 /*
2481 * Resume input after a call to snd_usbmidi_input_stop().
2482 */
snd_usbmidi_input_start(struct list_head * p)2483 void snd_usbmidi_input_start(struct list_head *p)
2484 {
2485 struct snd_usb_midi *umidi;
2486 int i;
2487
2488 umidi = list_entry(p, struct snd_usb_midi, list);
2489 if (umidi->input_running || !umidi->opened[1])
2490 return;
2491 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2492 snd_usbmidi_input_start_ep(umidi, umidi->endpoints[i].in);
2493 umidi->input_running = 1;
2494 }
2495 EXPORT_SYMBOL(snd_usbmidi_input_start);
2496
2497 /*
2498 * Prepare for suspend. Typically called from the USB suspend callback.
2499 */
snd_usbmidi_suspend(struct list_head * p)2500 void snd_usbmidi_suspend(struct list_head *p)
2501 {
2502 struct snd_usb_midi *umidi;
2503
2504 umidi = list_entry(p, struct snd_usb_midi, list);
2505 mutex_lock(&umidi->mutex);
2506 snd_usbmidi_input_stop(p);
2507 mutex_unlock(&umidi->mutex);
2508 }
2509 EXPORT_SYMBOL(snd_usbmidi_suspend);
2510
2511 /*
2512 * Resume. Typically called from the USB resume callback.
2513 */
snd_usbmidi_resume(struct list_head * p)2514 void snd_usbmidi_resume(struct list_head *p)
2515 {
2516 struct snd_usb_midi *umidi;
2517
2518 umidi = list_entry(p, struct snd_usb_midi, list);
2519 mutex_lock(&umidi->mutex);
2520 snd_usbmidi_input_start(p);
2521 mutex_unlock(&umidi->mutex);
2522 }
2523 EXPORT_SYMBOL(snd_usbmidi_resume);
2524
2525 /*
2526 * Creates and registers everything needed for a MIDI streaming interface.
2527 */
__snd_usbmidi_create(struct snd_card * card,struct usb_interface * iface,struct list_head * midi_list,const struct snd_usb_audio_quirk * quirk,unsigned int usb_id,unsigned int * num_rawmidis)2528 int __snd_usbmidi_create(struct snd_card *card,
2529 struct usb_interface *iface,
2530 struct list_head *midi_list,
2531 const struct snd_usb_audio_quirk *quirk,
2532 unsigned int usb_id,
2533 unsigned int *num_rawmidis)
2534 {
2535 struct snd_usb_midi *umidi;
2536 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
2537 int out_ports, in_ports;
2538 int i, err;
2539
2540 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
2541 if (!umidi)
2542 return -ENOMEM;
2543 umidi->dev = interface_to_usbdev(iface);
2544 umidi->card = card;
2545 umidi->iface = iface;
2546 umidi->quirk = quirk;
2547 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
2548 if (num_rawmidis)
2549 umidi->next_midi_device = *num_rawmidis;
2550 spin_lock_init(&umidi->disc_lock);
2551 init_rwsem(&umidi->disc_rwsem);
2552 mutex_init(&umidi->mutex);
2553 if (!usb_id)
2554 usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2555 le16_to_cpu(umidi->dev->descriptor.idProduct));
2556 umidi->usb_id = usb_id;
2557 timer_setup(&umidi->error_timer, snd_usbmidi_error_timer, 0);
2558
2559 /* detect the endpoint(s) to use */
2560 memset(endpoints, 0, sizeof(endpoints));
2561 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2562 case QUIRK_MIDI_STANDARD_INTERFACE:
2563 err = snd_usbmidi_get_ms_info(umidi, endpoints);
2564 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
2565 umidi->usb_protocol_ops =
2566 &snd_usbmidi_maudio_broken_running_status_ops;
2567 break;
2568 case QUIRK_MIDI_US122L:
2569 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2570 fallthrough;
2571 case QUIRK_MIDI_FIXED_ENDPOINT:
2572 memcpy(&endpoints[0], quirk->data,
2573 sizeof(struct snd_usb_midi_endpoint_info));
2574 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2575 break;
2576 case QUIRK_MIDI_YAMAHA:
2577 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2578 break;
2579 case QUIRK_MIDI_ROLAND:
2580 err = snd_usbmidi_detect_roland(umidi, &endpoints[0]);
2581 break;
2582 case QUIRK_MIDI_MIDIMAN:
2583 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2584 memcpy(&endpoints[0], quirk->data,
2585 sizeof(struct snd_usb_midi_endpoint_info));
2586 err = 0;
2587 break;
2588 case QUIRK_MIDI_NOVATION:
2589 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2590 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2591 break;
2592 case QUIRK_MIDI_RAW_BYTES:
2593 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
2594 /*
2595 * Interface 1 contains isochronous endpoints, but with the same
2596 * numbers as in interface 0. Since it is interface 1 that the
2597 * USB core has most recently seen, these descriptors are now
2598 * associated with the endpoint numbers. This will foul up our
2599 * attempts to submit bulk/interrupt URBs to the endpoints in
2600 * interface 0, so we have to make sure that the USB core looks
2601 * again at interface 0 by calling usb_set_interface() on it.
2602 */
2603 if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2604 usb_set_interface(umidi->dev, 0, 0);
2605 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2606 break;
2607 case QUIRK_MIDI_EMAGIC:
2608 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2609 memcpy(&endpoints[0], quirk->data,
2610 sizeof(struct snd_usb_midi_endpoint_info));
2611 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2612 break;
2613 case QUIRK_MIDI_CME:
2614 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
2615 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2616 break;
2617 case QUIRK_MIDI_AKAI:
2618 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2619 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2620 /* endpoint 1 is input-only */
2621 endpoints[1].out_cables = 0;
2622 break;
2623 case QUIRK_MIDI_FTDI:
2624 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2625
2626 /* set baud rate to 31250 (48 MHz / 16 / 96) */
2627 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2628 3, 0x40, 0x60, 0, NULL, 0, 1000);
2629 if (err < 0)
2630 break;
2631
2632 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2633 break;
2634 case QUIRK_MIDI_CH345:
2635 umidi->usb_protocol_ops = &snd_usbmidi_ch345_broken_sysex_ops;
2636 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2637 break;
2638 default:
2639 dev_err(&umidi->dev->dev, "invalid quirk type %d\n",
2640 quirk->type);
2641 err = -ENXIO;
2642 break;
2643 }
2644 if (err < 0)
2645 goto free_midi;
2646
2647 /* create rawmidi device */
2648 out_ports = 0;
2649 in_ports = 0;
2650 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2651 out_ports += hweight16(endpoints[i].out_cables);
2652 in_ports += hweight16(endpoints[i].in_cables);
2653 }
2654 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2655 if (err < 0)
2656 goto free_midi;
2657
2658 /* create endpoint/port structures */
2659 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2660 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2661 else
2662 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2663 if (err < 0)
2664 goto exit;
2665
2666 usb_autopm_get_interface_no_resume(umidi->iface);
2667
2668 list_add_tail(&umidi->list, midi_list);
2669 if (num_rawmidis)
2670 *num_rawmidis = umidi->next_midi_device;
2671 return 0;
2672
2673 free_midi:
2674 kfree(umidi);
2675 exit:
2676 return err;
2677 }
2678 EXPORT_SYMBOL(__snd_usbmidi_create);
2679