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 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
1813 /* return iJack for the corresponding jackID */
find_usb_ijack(struct usb_host_interface * hostif,uint8_t jack_id)1814 static int find_usb_ijack(struct usb_host_interface *hostif, uint8_t jack_id)
1815 {
1816 unsigned char *extra = hostif->extra;
1817 int extralen = hostif->extralen;
1818 struct usb_descriptor_header *h;
1819 struct usb_midi_out_jack_descriptor *outjd;
1820 struct usb_midi_in_jack_descriptor *injd;
1821 size_t sz;
1822
1823 while (extralen > 4) {
1824 h = (struct usb_descriptor_header *)extra;
1825 if (h->bDescriptorType != USB_DT_CS_INTERFACE)
1826 goto next;
1827
1828 outjd = (struct usb_midi_out_jack_descriptor *)h;
1829 if (h->bLength >= sizeof(*outjd) &&
1830 outjd->bDescriptorSubtype == UAC_MIDI_OUT_JACK &&
1831 outjd->bJackID == jack_id) {
1832 sz = USB_DT_MIDI_OUT_SIZE(outjd->bNrInputPins);
1833 if (outjd->bLength < sz)
1834 goto next;
1835 return *(extra + sz - 1);
1836 }
1837
1838 injd = (struct usb_midi_in_jack_descriptor *)h;
1839 if (injd->bLength >= sizeof(*injd) &&
1840 injd->bDescriptorSubtype == UAC_MIDI_IN_JACK &&
1841 injd->bJackID == jack_id)
1842 return injd->iJack;
1843
1844 next:
1845 if (!extra[0])
1846 break;
1847 extralen -= extra[0];
1848 extra += extra[0];
1849 }
1850 return 0;
1851 }
1852
snd_usbmidi_init_substream(struct snd_usb_midi * umidi,int stream,int number,int jack_id,struct snd_rawmidi_substream ** rsubstream)1853 static void snd_usbmidi_init_substream(struct snd_usb_midi *umidi,
1854 int stream, int number, int jack_id,
1855 struct snd_rawmidi_substream **rsubstream)
1856 {
1857 struct port_info *port_info;
1858 const char *name_format;
1859 struct usb_interface *intf;
1860 struct usb_host_interface *hostif;
1861 uint8_t jack_name_buf[32];
1862 uint8_t *default_jack_name = "MIDI";
1863 uint8_t *jack_name = default_jack_name;
1864 uint8_t iJack;
1865 int res;
1866
1867 struct snd_rawmidi_substream *substream =
1868 snd_usbmidi_find_substream(umidi, stream, number);
1869 if (!substream) {
1870 dev_err(&umidi->dev->dev, "substream %d:%d not found\n", stream,
1871 number);
1872 return;
1873 }
1874
1875 intf = umidi->iface;
1876 if (intf && jack_id >= 0) {
1877 hostif = intf->cur_altsetting;
1878 iJack = find_usb_ijack(hostif, jack_id);
1879 if (iJack != 0) {
1880 res = usb_string(umidi->dev, iJack, jack_name_buf,
1881 ARRAY_SIZE(jack_name_buf));
1882 if (res)
1883 jack_name = jack_name_buf;
1884 }
1885 }
1886
1887 port_info = find_port_info(umidi, number);
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
1893 *rsubstream = substream;
1894 }
1895
1896 /*
1897 * Creates the endpoints and their ports.
1898 */
snd_usbmidi_create_endpoints(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoints)1899 static int snd_usbmidi_create_endpoints(struct snd_usb_midi *umidi,
1900 struct snd_usb_midi_endpoint_info *endpoints)
1901 {
1902 int i, j, err;
1903 int out_ports = 0, in_ports = 0;
1904
1905 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1906 if (endpoints[i].out_cables) {
1907 err = snd_usbmidi_out_endpoint_create(umidi,
1908 &endpoints[i],
1909 &umidi->endpoints[i]);
1910 if (err < 0)
1911 return err;
1912 }
1913 if (endpoints[i].in_cables) {
1914 err = snd_usbmidi_in_endpoint_create(umidi,
1915 &endpoints[i],
1916 &umidi->endpoints[i]);
1917 if (err < 0)
1918 return err;
1919 }
1920
1921 for (j = 0; j < 0x10; ++j) {
1922 if (endpoints[i].out_cables & (1 << j)) {
1923 snd_usbmidi_init_substream(umidi,
1924 SNDRV_RAWMIDI_STREAM_OUTPUT,
1925 out_ports,
1926 endpoints[i].assoc_out_jacks[j],
1927 &umidi->endpoints[i].out->ports[j].substream);
1928 ++out_ports;
1929 }
1930 if (endpoints[i].in_cables & (1 << j)) {
1931 snd_usbmidi_init_substream(umidi,
1932 SNDRV_RAWMIDI_STREAM_INPUT,
1933 in_ports,
1934 endpoints[i].assoc_in_jacks[j],
1935 &umidi->endpoints[i].in->ports[j].substream);
1936 ++in_ports;
1937 }
1938 }
1939 }
1940 dev_dbg(&umidi->dev->dev, "created %d output and %d input ports\n",
1941 out_ports, in_ports);
1942 return 0;
1943 }
1944
find_usb_ms_endpoint_descriptor(struct usb_host_endpoint * hostep)1945 static struct usb_ms_endpoint_descriptor *find_usb_ms_endpoint_descriptor(
1946 struct usb_host_endpoint *hostep)
1947 {
1948 unsigned char *extra = hostep->extra;
1949 int extralen = hostep->extralen;
1950
1951 while (extralen > 3) {
1952 struct usb_ms_endpoint_descriptor *ms_ep =
1953 (struct usb_ms_endpoint_descriptor *)extra;
1954
1955 if (ms_ep->bLength > 3 &&
1956 ms_ep->bDescriptorType == USB_DT_CS_ENDPOINT &&
1957 ms_ep->bDescriptorSubtype == UAC_MS_GENERAL)
1958 return ms_ep;
1959 if (!extra[0])
1960 break;
1961 extralen -= extra[0];
1962 extra += extra[0];
1963 }
1964 return NULL;
1965 }
1966
1967 /*
1968 * Returns MIDIStreaming device capabilities.
1969 */
snd_usbmidi_get_ms_info(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoints)1970 static int snd_usbmidi_get_ms_info(struct snd_usb_midi *umidi,
1971 struct snd_usb_midi_endpoint_info *endpoints)
1972 {
1973 struct usb_interface *intf;
1974 struct usb_host_interface *hostif;
1975 struct usb_interface_descriptor *intfd;
1976 struct usb_ms_header_descriptor *ms_header;
1977 struct usb_host_endpoint *hostep;
1978 struct usb_endpoint_descriptor *ep;
1979 struct usb_ms_endpoint_descriptor *ms_ep;
1980 int i, j, epidx;
1981
1982 intf = umidi->iface;
1983 if (!intf)
1984 return -ENXIO;
1985 hostif = &intf->altsetting[0];
1986 intfd = get_iface_desc(hostif);
1987 ms_header = (struct usb_ms_header_descriptor *)hostif->extra;
1988 if (hostif->extralen >= 7 &&
1989 ms_header->bLength >= 7 &&
1990 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1991 ms_header->bDescriptorSubtype == UAC_HEADER)
1992 dev_dbg(&umidi->dev->dev, "MIDIStreaming version %02x.%02x\n",
1993 ((uint8_t *)&ms_header->bcdMSC)[1], ((uint8_t *)&ms_header->bcdMSC)[0]);
1994 else
1995 dev_warn(&umidi->dev->dev,
1996 "MIDIStreaming interface descriptor not found\n");
1997
1998 epidx = 0;
1999 for (i = 0; i < intfd->bNumEndpoints; ++i) {
2000 hostep = &hostif->endpoint[i];
2001 ep = get_ep_desc(hostep);
2002 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
2003 continue;
2004 ms_ep = find_usb_ms_endpoint_descriptor(hostep);
2005 if (!ms_ep)
2006 continue;
2007 if (ms_ep->bLength <= sizeof(*ms_ep))
2008 continue;
2009 if (ms_ep->bNumEmbMIDIJack > 0x10)
2010 continue;
2011 if (ms_ep->bLength < sizeof(*ms_ep) + ms_ep->bNumEmbMIDIJack)
2012 continue;
2013 if (usb_endpoint_dir_out(ep)) {
2014 if (endpoints[epidx].out_ep) {
2015 if (++epidx >= MIDI_MAX_ENDPOINTS) {
2016 dev_warn(&umidi->dev->dev,
2017 "too many endpoints\n");
2018 break;
2019 }
2020 }
2021 endpoints[epidx].out_ep = usb_endpoint_num(ep);
2022 if (usb_endpoint_xfer_int(ep))
2023 endpoints[epidx].out_interval = ep->bInterval;
2024 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
2025 /*
2026 * Low speed bulk transfers don't exist, so
2027 * force interrupt transfers for devices like
2028 * ESI MIDI Mate that try to use them anyway.
2029 */
2030 endpoints[epidx].out_interval = 1;
2031 endpoints[epidx].out_cables =
2032 (1 << ms_ep->bNumEmbMIDIJack) - 1;
2033 for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j)
2034 endpoints[epidx].assoc_out_jacks[j] = ms_ep->baAssocJackID[j];
2035 for (; j < ARRAY_SIZE(endpoints[epidx].assoc_out_jacks); ++j)
2036 endpoints[epidx].assoc_out_jacks[j] = -1;
2037 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
2038 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
2039 } else {
2040 if (endpoints[epidx].in_ep) {
2041 if (++epidx >= MIDI_MAX_ENDPOINTS) {
2042 dev_warn(&umidi->dev->dev,
2043 "too many endpoints\n");
2044 break;
2045 }
2046 }
2047 endpoints[epidx].in_ep = usb_endpoint_num(ep);
2048 if (usb_endpoint_xfer_int(ep))
2049 endpoints[epidx].in_interval = ep->bInterval;
2050 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
2051 endpoints[epidx].in_interval = 1;
2052 endpoints[epidx].in_cables =
2053 (1 << ms_ep->bNumEmbMIDIJack) - 1;
2054 for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j)
2055 endpoints[epidx].assoc_in_jacks[j] = ms_ep->baAssocJackID[j];
2056 for (; j < ARRAY_SIZE(endpoints[epidx].assoc_in_jacks); ++j)
2057 endpoints[epidx].assoc_in_jacks[j] = -1;
2058 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
2059 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
2060 }
2061 }
2062 return 0;
2063 }
2064
roland_load_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * info)2065 static int roland_load_info(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_info *info)
2067 {
2068 static const char *const names[] = { "High Load", "Light Load" };
2069
2070 return snd_ctl_enum_info(info, 1, 2, names);
2071 }
2072
roland_load_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)2073 static int roland_load_get(struct snd_kcontrol *kcontrol,
2074 struct snd_ctl_elem_value *value)
2075 {
2076 value->value.enumerated.item[0] = kcontrol->private_value;
2077 return 0;
2078 }
2079
roland_load_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)2080 static int roland_load_put(struct snd_kcontrol *kcontrol,
2081 struct snd_ctl_elem_value *value)
2082 {
2083 struct snd_usb_midi *umidi = kcontrol->private_data;
2084 int changed;
2085
2086 if (value->value.enumerated.item[0] > 1)
2087 return -EINVAL;
2088 mutex_lock(&umidi->mutex);
2089 changed = value->value.enumerated.item[0] != kcontrol->private_value;
2090 if (changed)
2091 kcontrol->private_value = value->value.enumerated.item[0];
2092 mutex_unlock(&umidi->mutex);
2093 return changed;
2094 }
2095
2096 static const struct snd_kcontrol_new roland_load_ctl = {
2097 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2098 .name = "MIDI Input Mode",
2099 .info = roland_load_info,
2100 .get = roland_load_get,
2101 .put = roland_load_put,
2102 .private_value = 1,
2103 };
2104
2105 /*
2106 * On Roland devices, use the second alternate setting to be able to use
2107 * the interrupt input endpoint.
2108 */
snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi * umidi)2109 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi *umidi)
2110 {
2111 struct usb_interface *intf;
2112 struct usb_host_interface *hostif;
2113 struct usb_interface_descriptor *intfd;
2114
2115 intf = umidi->iface;
2116 if (!intf || intf->num_altsetting != 2)
2117 return;
2118
2119 hostif = &intf->altsetting[1];
2120 intfd = get_iface_desc(hostif);
2121 /* If either or both of the endpoints support interrupt transfer,
2122 * then use the alternate setting
2123 */
2124 if (intfd->bNumEndpoints != 2 ||
2125 !((get_endpoint(hostif, 0)->bmAttributes &
2126 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT ||
2127 (get_endpoint(hostif, 1)->bmAttributes &
2128 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
2129 return;
2130
2131 dev_dbg(&umidi->dev->dev, "switching to altsetting %d with int ep\n",
2132 intfd->bAlternateSetting);
2133 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
2134 intfd->bAlternateSetting);
2135
2136 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
2137 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
2138 umidi->roland_load_ctl = NULL;
2139 }
2140
2141 /*
2142 * Try to find any usable endpoints in the interface.
2143 */
snd_usbmidi_detect_endpoints(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoint,int max_endpoints)2144 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi *umidi,
2145 struct snd_usb_midi_endpoint_info *endpoint,
2146 int max_endpoints)
2147 {
2148 struct usb_interface *intf;
2149 struct usb_host_interface *hostif;
2150 struct usb_interface_descriptor *intfd;
2151 struct usb_endpoint_descriptor *epd;
2152 int i, out_eps = 0, in_eps = 0;
2153
2154 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
2155 snd_usbmidi_switch_roland_altsetting(umidi);
2156
2157 if (endpoint[0].out_ep || endpoint[0].in_ep)
2158 return 0;
2159
2160 intf = umidi->iface;
2161 if (!intf || intf->num_altsetting < 1)
2162 return -ENOENT;
2163 hostif = intf->cur_altsetting;
2164 intfd = get_iface_desc(hostif);
2165
2166 for (i = 0; i < intfd->bNumEndpoints; ++i) {
2167 epd = get_endpoint(hostif, i);
2168 if (!usb_endpoint_xfer_bulk(epd) &&
2169 !usb_endpoint_xfer_int(epd))
2170 continue;
2171 if (out_eps < max_endpoints &&
2172 usb_endpoint_dir_out(epd)) {
2173 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
2174 if (usb_endpoint_xfer_int(epd))
2175 endpoint[out_eps].out_interval = epd->bInterval;
2176 ++out_eps;
2177 }
2178 if (in_eps < max_endpoints &&
2179 usb_endpoint_dir_in(epd)) {
2180 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
2181 if (usb_endpoint_xfer_int(epd))
2182 endpoint[in_eps].in_interval = epd->bInterval;
2183 ++in_eps;
2184 }
2185 }
2186 return (out_eps || in_eps) ? 0 : -ENOENT;
2187 }
2188
2189 /*
2190 * Detects the endpoints for one-port-per-endpoint protocols.
2191 */
snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoints)2192 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi *umidi,
2193 struct snd_usb_midi_endpoint_info *endpoints)
2194 {
2195 int err, i;
2196
2197 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
2198 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2199 if (endpoints[i].out_ep)
2200 endpoints[i].out_cables = 0x0001;
2201 if (endpoints[i].in_ep)
2202 endpoints[i].in_cables = 0x0001;
2203 }
2204 return err;
2205 }
2206
2207 /*
2208 * Detects the endpoints and ports of Yamaha devices.
2209 */
snd_usbmidi_detect_yamaha(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoint)2210 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi *umidi,
2211 struct snd_usb_midi_endpoint_info *endpoint)
2212 {
2213 struct usb_interface *intf;
2214 struct usb_host_interface *hostif;
2215 struct usb_interface_descriptor *intfd;
2216 uint8_t *cs_desc;
2217
2218 intf = umidi->iface;
2219 if (!intf)
2220 return -ENOENT;
2221 hostif = intf->altsetting;
2222 intfd = get_iface_desc(hostif);
2223 if (intfd->bNumEndpoints < 1)
2224 return -ENOENT;
2225
2226 /*
2227 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
2228 * necessarily with any useful contents. So simply count 'em.
2229 */
2230 for (cs_desc = hostif->extra;
2231 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
2232 cs_desc += cs_desc[0]) {
2233 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
2234 if (cs_desc[2] == UAC_MIDI_IN_JACK)
2235 endpoint->in_cables =
2236 (endpoint->in_cables << 1) | 1;
2237 else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
2238 endpoint->out_cables =
2239 (endpoint->out_cables << 1) | 1;
2240 }
2241 }
2242 if (!endpoint->in_cables && !endpoint->out_cables)
2243 return -ENOENT;
2244
2245 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
2246 }
2247
2248 /*
2249 * Detects the endpoints and ports of Roland devices.
2250 */
snd_usbmidi_detect_roland(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoint)2251 static int snd_usbmidi_detect_roland(struct snd_usb_midi *umidi,
2252 struct snd_usb_midi_endpoint_info *endpoint)
2253 {
2254 struct usb_interface *intf;
2255 struct usb_host_interface *hostif;
2256 u8 *cs_desc;
2257
2258 intf = umidi->iface;
2259 if (!intf)
2260 return -ENOENT;
2261 hostif = intf->altsetting;
2262 /*
2263 * Some devices have a descriptor <06 24 F1 02 <inputs> <outputs>>,
2264 * some have standard class descriptors, or both kinds, or neither.
2265 */
2266 for (cs_desc = hostif->extra;
2267 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
2268 cs_desc += cs_desc[0]) {
2269 if (cs_desc[0] >= 6 &&
2270 cs_desc[1] == USB_DT_CS_INTERFACE &&
2271 cs_desc[2] == 0xf1 &&
2272 cs_desc[3] == 0x02) {
2273 if (cs_desc[4] > 0x10 || cs_desc[5] > 0x10)
2274 continue;
2275 endpoint->in_cables = (1 << cs_desc[4]) - 1;
2276 endpoint->out_cables = (1 << cs_desc[5]) - 1;
2277 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
2278 } else if (cs_desc[0] >= 7 &&
2279 cs_desc[1] == USB_DT_CS_INTERFACE &&
2280 cs_desc[2] == UAC_HEADER) {
2281 return snd_usbmidi_get_ms_info(umidi, endpoint);
2282 }
2283 }
2284
2285 return -ENODEV;
2286 }
2287
2288 /*
2289 * Creates the endpoints and their ports for Midiman devices.
2290 */
snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi * umidi,struct snd_usb_midi_endpoint_info * endpoint)2291 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi *umidi,
2292 struct snd_usb_midi_endpoint_info *endpoint)
2293 {
2294 struct snd_usb_midi_endpoint_info ep_info;
2295 struct usb_interface *intf;
2296 struct usb_host_interface *hostif;
2297 struct usb_interface_descriptor *intfd;
2298 struct usb_endpoint_descriptor *epd;
2299 int cable, err;
2300
2301 intf = umidi->iface;
2302 if (!intf)
2303 return -ENOENT;
2304 hostif = intf->altsetting;
2305 intfd = get_iface_desc(hostif);
2306 /*
2307 * The various MidiSport devices have more or less random endpoint
2308 * numbers, so we have to identify the endpoints by their index in
2309 * the descriptor array, like the driver for that other OS does.
2310 *
2311 * There is one interrupt input endpoint for all input ports, one
2312 * bulk output endpoint for even-numbered ports, and one for odd-
2313 * numbered ports. Both bulk output endpoints have corresponding
2314 * input bulk endpoints (at indices 1 and 3) which aren't used.
2315 */
2316 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
2317 dev_dbg(&umidi->dev->dev, "not enough endpoints\n");
2318 return -ENOENT;
2319 }
2320
2321 epd = get_endpoint(hostif, 0);
2322 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
2323 dev_dbg(&umidi->dev->dev, "endpoint[0] isn't interrupt\n");
2324 return -ENXIO;
2325 }
2326 epd = get_endpoint(hostif, 2);
2327 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
2328 dev_dbg(&umidi->dev->dev, "endpoint[2] isn't bulk output\n");
2329 return -ENXIO;
2330 }
2331 if (endpoint->out_cables > 0x0001) {
2332 epd = get_endpoint(hostif, 4);
2333 if (!usb_endpoint_dir_out(epd) ||
2334 !usb_endpoint_xfer_bulk(epd)) {
2335 dev_dbg(&umidi->dev->dev,
2336 "endpoint[4] isn't bulk output\n");
2337 return -ENXIO;
2338 }
2339 }
2340
2341 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress &
2342 USB_ENDPOINT_NUMBER_MASK;
2343 ep_info.out_interval = 0;
2344 ep_info.out_cables = endpoint->out_cables & 0x5555;
2345 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
2346 &umidi->endpoints[0]);
2347 if (err < 0)
2348 return err;
2349
2350 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress &
2351 USB_ENDPOINT_NUMBER_MASK;
2352 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
2353 ep_info.in_cables = endpoint->in_cables;
2354 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info,
2355 &umidi->endpoints[0]);
2356 if (err < 0)
2357 return err;
2358
2359 if (endpoint->out_cables > 0x0001) {
2360 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress &
2361 USB_ENDPOINT_NUMBER_MASK;
2362 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
2363 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
2364 &umidi->endpoints[1]);
2365 if (err < 0)
2366 return err;
2367 }
2368
2369 for (cable = 0; cable < 0x10; ++cable) {
2370 if (endpoint->out_cables & (1 << cable))
2371 snd_usbmidi_init_substream(umidi,
2372 SNDRV_RAWMIDI_STREAM_OUTPUT,
2373 cable,
2374 -1 /* prevent trying to find jack */,
2375 &umidi->endpoints[cable & 1].out->ports[cable].substream);
2376 if (endpoint->in_cables & (1 << cable))
2377 snd_usbmidi_init_substream(umidi,
2378 SNDRV_RAWMIDI_STREAM_INPUT,
2379 cable,
2380 -1 /* prevent trying to find jack */,
2381 &umidi->endpoints[0].in->ports[cable].substream);
2382 }
2383 return 0;
2384 }
2385
2386 static const struct snd_rawmidi_global_ops snd_usbmidi_ops = {
2387 .get_port_info = snd_usbmidi_get_port_info,
2388 };
2389
snd_usbmidi_create_rawmidi(struct snd_usb_midi * umidi,int out_ports,int in_ports)2390 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi *umidi,
2391 int out_ports, int in_ports)
2392 {
2393 struct snd_rawmidi *rmidi;
2394 int err;
2395
2396 err = snd_rawmidi_new(umidi->card, "USB MIDI",
2397 umidi->next_midi_device++,
2398 out_ports, in_ports, &rmidi);
2399 if (err < 0)
2400 return err;
2401 strcpy(rmidi->name, umidi->card->shortname);
2402 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2403 SNDRV_RAWMIDI_INFO_INPUT |
2404 SNDRV_RAWMIDI_INFO_DUPLEX;
2405 rmidi->ops = &snd_usbmidi_ops;
2406 rmidi->private_data = umidi;
2407 rmidi->private_free = snd_usbmidi_rawmidi_free;
2408 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
2409 &snd_usbmidi_output_ops);
2410 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
2411 &snd_usbmidi_input_ops);
2412
2413 umidi->rmidi = rmidi;
2414 return 0;
2415 }
2416
2417 /*
2418 * Temporarily stop input.
2419 */
snd_usbmidi_input_stop(struct list_head * p)2420 void snd_usbmidi_input_stop(struct list_head *p)
2421 {
2422 struct snd_usb_midi *umidi;
2423 unsigned int i, j;
2424
2425 umidi = list_entry(p, struct snd_usb_midi, list);
2426 if (!umidi->input_running)
2427 return;
2428 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2429 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
2430 if (ep->in)
2431 for (j = 0; j < INPUT_URBS; ++j)
2432 usb_kill_urb(ep->in->urbs[j]);
2433 }
2434 umidi->input_running = 0;
2435 }
2436 EXPORT_SYMBOL(snd_usbmidi_input_stop);
2437
snd_usbmidi_input_start_ep(struct snd_usb_midi * umidi,struct snd_usb_midi_in_endpoint * ep)2438 static void snd_usbmidi_input_start_ep(struct snd_usb_midi *umidi,
2439 struct snd_usb_midi_in_endpoint *ep)
2440 {
2441 unsigned int i;
2442 unsigned long flags;
2443
2444 if (!ep)
2445 return;
2446 for (i = 0; i < INPUT_URBS; ++i) {
2447 struct urb *urb = ep->urbs[i];
2448 spin_lock_irqsave(&umidi->disc_lock, flags);
2449 if (!atomic_read(&urb->use_count)) {
2450 urb->dev = ep->umidi->dev;
2451 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
2452 }
2453 spin_unlock_irqrestore(&umidi->disc_lock, flags);
2454 }
2455 }
2456
2457 /*
2458 * Resume input after a call to snd_usbmidi_input_stop().
2459 */
snd_usbmidi_input_start(struct list_head * p)2460 void snd_usbmidi_input_start(struct list_head *p)
2461 {
2462 struct snd_usb_midi *umidi;
2463 int i;
2464
2465 umidi = list_entry(p, struct snd_usb_midi, list);
2466 if (umidi->input_running || !umidi->opened[1])
2467 return;
2468 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2469 snd_usbmidi_input_start_ep(umidi, umidi->endpoints[i].in);
2470 umidi->input_running = 1;
2471 }
2472 EXPORT_SYMBOL(snd_usbmidi_input_start);
2473
2474 /*
2475 * Prepare for suspend. Typically called from the USB suspend callback.
2476 */
snd_usbmidi_suspend(struct list_head * p)2477 void snd_usbmidi_suspend(struct list_head *p)
2478 {
2479 struct snd_usb_midi *umidi;
2480
2481 umidi = list_entry(p, struct snd_usb_midi, list);
2482 mutex_lock(&umidi->mutex);
2483 snd_usbmidi_input_stop(p);
2484 mutex_unlock(&umidi->mutex);
2485 }
2486 EXPORT_SYMBOL(snd_usbmidi_suspend);
2487
2488 /*
2489 * Resume. Typically called from the USB resume callback.
2490 */
snd_usbmidi_resume(struct list_head * p)2491 void snd_usbmidi_resume(struct list_head *p)
2492 {
2493 struct snd_usb_midi *umidi;
2494
2495 umidi = list_entry(p, struct snd_usb_midi, list);
2496 mutex_lock(&umidi->mutex);
2497 snd_usbmidi_input_start(p);
2498 mutex_unlock(&umidi->mutex);
2499 }
2500 EXPORT_SYMBOL(snd_usbmidi_resume);
2501
2502 /*
2503 * Creates and registers everything needed for a MIDI streaming interface.
2504 */
__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)2505 int __snd_usbmidi_create(struct snd_card *card,
2506 struct usb_interface *iface,
2507 struct list_head *midi_list,
2508 const struct snd_usb_audio_quirk *quirk,
2509 unsigned int usb_id,
2510 unsigned int *num_rawmidis)
2511 {
2512 struct snd_usb_midi *umidi;
2513 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
2514 int out_ports, in_ports;
2515 int i, err;
2516
2517 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
2518 if (!umidi)
2519 return -ENOMEM;
2520 umidi->dev = interface_to_usbdev(iface);
2521 umidi->card = card;
2522 umidi->iface = iface;
2523 umidi->quirk = quirk;
2524 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
2525 if (num_rawmidis)
2526 umidi->next_midi_device = *num_rawmidis;
2527 spin_lock_init(&umidi->disc_lock);
2528 init_rwsem(&umidi->disc_rwsem);
2529 mutex_init(&umidi->mutex);
2530 if (!usb_id)
2531 usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2532 le16_to_cpu(umidi->dev->descriptor.idProduct));
2533 umidi->usb_id = usb_id;
2534 timer_setup(&umidi->error_timer, snd_usbmidi_error_timer, 0);
2535
2536 /* detect the endpoint(s) to use */
2537 memset(endpoints, 0, sizeof(endpoints));
2538 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2539 case QUIRK_MIDI_STANDARD_INTERFACE:
2540 err = snd_usbmidi_get_ms_info(umidi, endpoints);
2541 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
2542 umidi->usb_protocol_ops =
2543 &snd_usbmidi_maudio_broken_running_status_ops;
2544 break;
2545 case QUIRK_MIDI_US122L:
2546 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2547 fallthrough;
2548 case QUIRK_MIDI_FIXED_ENDPOINT:
2549 memcpy(&endpoints[0], quirk->data,
2550 sizeof(struct snd_usb_midi_endpoint_info));
2551 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2552 break;
2553 case QUIRK_MIDI_YAMAHA:
2554 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2555 break;
2556 case QUIRK_MIDI_ROLAND:
2557 err = snd_usbmidi_detect_roland(umidi, &endpoints[0]);
2558 break;
2559 case QUIRK_MIDI_MIDIMAN:
2560 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2561 memcpy(&endpoints[0], quirk->data,
2562 sizeof(struct snd_usb_midi_endpoint_info));
2563 err = 0;
2564 break;
2565 case QUIRK_MIDI_NOVATION:
2566 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2567 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2568 break;
2569 case QUIRK_MIDI_RAW_BYTES:
2570 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
2571 /*
2572 * Interface 1 contains isochronous endpoints, but with the same
2573 * numbers as in interface 0. Since it is interface 1 that the
2574 * USB core has most recently seen, these descriptors are now
2575 * associated with the endpoint numbers. This will foul up our
2576 * attempts to submit bulk/interrupt URBs to the endpoints in
2577 * interface 0, so we have to make sure that the USB core looks
2578 * again at interface 0 by calling usb_set_interface() on it.
2579 */
2580 if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2581 usb_set_interface(umidi->dev, 0, 0);
2582 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2583 break;
2584 case QUIRK_MIDI_EMAGIC:
2585 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2586 memcpy(&endpoints[0], quirk->data,
2587 sizeof(struct snd_usb_midi_endpoint_info));
2588 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2589 break;
2590 case QUIRK_MIDI_CME:
2591 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
2592 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2593 break;
2594 case QUIRK_MIDI_AKAI:
2595 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2596 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2597 /* endpoint 1 is input-only */
2598 endpoints[1].out_cables = 0;
2599 break;
2600 case QUIRK_MIDI_FTDI:
2601 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2602
2603 /* set baud rate to 31250 (48 MHz / 16 / 96) */
2604 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2605 3, 0x40, 0x60, 0, NULL, 0, 1000);
2606 if (err < 0)
2607 break;
2608
2609 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2610 break;
2611 case QUIRK_MIDI_CH345:
2612 umidi->usb_protocol_ops = &snd_usbmidi_ch345_broken_sysex_ops;
2613 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2614 break;
2615 default:
2616 dev_err(&umidi->dev->dev, "invalid quirk type %d\n",
2617 quirk->type);
2618 err = -ENXIO;
2619 break;
2620 }
2621 if (err < 0)
2622 goto free_midi;
2623
2624 /* create rawmidi device */
2625 out_ports = 0;
2626 in_ports = 0;
2627 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2628 out_ports += hweight16(endpoints[i].out_cables);
2629 in_ports += hweight16(endpoints[i].in_cables);
2630 }
2631 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2632 if (err < 0)
2633 goto free_midi;
2634
2635 /* create endpoint/port structures */
2636 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2637 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2638 else
2639 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2640 if (err < 0)
2641 goto exit;
2642
2643 usb_autopm_get_interface_no_resume(umidi->iface);
2644
2645 list_add_tail(&umidi->list, midi_list);
2646 if (num_rawmidis)
2647 *num_rawmidis = umidi->next_midi_device;
2648 return 0;
2649
2650 free_midi:
2651 kfree(umidi);
2652 exit:
2653 return err;
2654 }
2655 EXPORT_SYMBOL(__snd_usbmidi_create);
2656