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