Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
midi.c
Go to the documentation of this file.
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/module.h>
51 
52 #include <sound/core.h>
53 #include <sound/control.h>
54 #include <sound/rawmidi.h>
55 #include <sound/asequencer.h>
56 #include "usbaudio.h"
57 #include "midi.h"
58 #include "power.h"
59 #include "helper.h"
60 
61 /*
62  * define this to log all USB packets
63  */
64 /* #define DUMP_PACKETS */
65 
66 /*
67  * how long to wait after some USB errors, so that khubd can disconnect() us
68  * without too many spurious errors
69  */
70 #define ERROR_DELAY_JIFFIES (HZ / 10)
71 
72 #define OUTPUT_URBS 7
73 #define INPUT_URBS 7
74 
75 
76 MODULE_AUTHOR("Clemens Ladisch <[email protected]>");
77 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
78 MODULE_LICENSE("Dual BSD/GPL");
79 
80 
82  __u8 bLength;
87 } __attribute__ ((packed));
88 
95 } __attribute__ ((packed));
96 
99 struct snd_usb_midi_endpoint;
103  void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
107 };
108 
109 struct snd_usb_midi {
110  struct usb_device *dev;
111  struct snd_card *card;
113  const struct snd_usb_audio_quirk *quirk;
116  struct list_head list;
119  struct mutex mutex;
122 
127  unsigned long input_triggered;
128  unsigned int opened;
129  unsigned char disconnected;
130 
132 };
133 
137  struct urb *urb;
139  } urbs[OUTPUT_URBS];
140  unsigned int active_urbs;
141  unsigned int drain_urbs;
142  int max_transfer; /* size of urb buffer */
144  unsigned int next_urb;
146 
150  int active;
152  uint8_t cable; /* cable number << 4 */
154 #define STATE_UNKNOWN 0
155 #define STATE_1PARAM 1
156 #define STATE_2PARAM_1 2
157 #define STATE_2PARAM_2 3
158 #define STATE_SYSEX_0 4
159 #define STATE_SYSEX_1 5
160 #define STATE_SYSEX_2 6
162  } ports[0x10];
164 
166 };
167 
170  struct urb* urbs[INPUT_URBS];
174  } ports[0x10];
178 };
179 
180 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
181 
182 static const uint8_t snd_usbmidi_cin_length[] = {
183  0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
184 };
185 
186 /*
187  * Submits the URB, with error handling.
188  */
189 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
190 {
191  int err = usb_submit_urb(urb, flags);
192  if (err < 0 && err != -ENODEV)
193  snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
194  return err;
195 }
196 
197 /*
198  * Error handling for URB completion functions.
199  */
200 static int snd_usbmidi_urb_error(int status)
201 {
202  switch (status) {
203  /* manually unlinked, or device gone */
204  case -ENOENT:
205  case -ECONNRESET:
206  case -ESHUTDOWN:
207  case -ENODEV:
208  return -ENODEV;
209  /* errors that might occur during unplugging */
210  case -EPROTO:
211  case -ETIME:
212  case -EILSEQ:
213  return -EIO;
214  default:
215  snd_printk(KERN_ERR "urb status %d\n", status);
216  return 0; /* continue */
217  }
218 }
219 
220 /*
221  * Receives a chunk of MIDI data.
222  */
223 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
224  uint8_t* data, int length)
225 {
226  struct usbmidi_in_port* port = &ep->ports[portidx];
227 
228  if (!port->substream) {
229  snd_printd("unexpected port %d!\n", portidx);
230  return;
231  }
232  if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
233  return;
234  snd_rawmidi_receive(port->substream, data, length);
235 }
236 
237 #ifdef DUMP_PACKETS
238 static void dump_urb(const char *type, const u8 *data, int length)
239 {
240  snd_printk(KERN_DEBUG "%s packet: [", type);
241  for (; length > 0; ++data, --length)
242  printk(" %02x", *data);
243  printk(" ]\n");
244 }
245 #else
246 #define dump_urb(type, data, length) /* nothing */
247 #endif
248 
249 /*
250  * Processes the data read from the device.
251  */
252 static void snd_usbmidi_in_urb_complete(struct urb* urb)
253 {
254  struct snd_usb_midi_in_endpoint* ep = urb->context;
255 
256  if (urb->status == 0) {
257  dump_urb("received", urb->transfer_buffer, urb->actual_length);
258  ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
259  urb->actual_length);
260  } else {
261  int err = snd_usbmidi_urb_error(urb->status);
262  if (err < 0) {
263  if (err != -ENODEV) {
264  ep->error_resubmit = 1;
265  mod_timer(&ep->umidi->error_timer,
267  }
268  return;
269  }
270  }
271 
272  urb->dev = ep->umidi->dev;
273  snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
274 }
275 
276 static void snd_usbmidi_out_urb_complete(struct urb* urb)
277 {
278  struct out_urb_context *context = urb->context;
279  struct snd_usb_midi_out_endpoint* ep = context->ep;
280  unsigned int urb_index;
281 
282  spin_lock(&ep->buffer_lock);
283  urb_index = context - ep->urbs;
284  ep->active_urbs &= ~(1 << urb_index);
285  if (unlikely(ep->drain_urbs)) {
286  ep->drain_urbs &= ~(1 << urb_index);
287  wake_up(&ep->drain_wait);
288  }
289  spin_unlock(&ep->buffer_lock);
290  if (urb->status < 0) {
291  int err = snd_usbmidi_urb_error(urb->status);
292  if (err < 0) {
293  if (err != -ENODEV)
294  mod_timer(&ep->umidi->error_timer,
295  jiffies + ERROR_DELAY_JIFFIES);
296  return;
297  }
298  }
299  snd_usbmidi_do_output(ep);
300 }
301 
302 /*
303  * This is called when some data should be transferred to the device
304  * (from one or more substreams).
305  */
306 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
307 {
308  unsigned int urb_index;
309  struct urb* urb;
310  unsigned long flags;
311 
312  spin_lock_irqsave(&ep->buffer_lock, flags);
313  if (ep->umidi->disconnected) {
314  spin_unlock_irqrestore(&ep->buffer_lock, flags);
315  return;
316  }
317 
318  urb_index = ep->next_urb;
319  for (;;) {
320  if (!(ep->active_urbs & (1 << urb_index))) {
321  urb = ep->urbs[urb_index].urb;
322  urb->transfer_buffer_length = 0;
323  ep->umidi->usb_protocol_ops->output(ep, urb);
324  if (urb->transfer_buffer_length == 0)
325  break;
326 
327  dump_urb("sending", urb->transfer_buffer,
328  urb->transfer_buffer_length);
329  urb->dev = ep->umidi->dev;
330  if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
331  break;
332  ep->active_urbs |= 1 << urb_index;
333  }
334  if (++urb_index >= OUTPUT_URBS)
335  urb_index = 0;
336  if (urb_index == ep->next_urb)
337  break;
338  }
339  ep->next_urb = urb_index;
340  spin_unlock_irqrestore(&ep->buffer_lock, flags);
341 }
342 
343 static void snd_usbmidi_out_tasklet(unsigned long data)
344 {
345  struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
346 
347  snd_usbmidi_do_output(ep);
348 }
349 
350 /* called after transfers had been interrupted due to some USB error */
351 static void snd_usbmidi_error_timer(unsigned long data)
352 {
353  struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
354  unsigned int i, j;
355 
356  spin_lock(&umidi->disc_lock);
357  if (umidi->disconnected) {
358  spin_unlock(&umidi->disc_lock);
359  return;
360  }
361  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
362  struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
363  if (in && in->error_resubmit) {
364  in->error_resubmit = 0;
365  for (j = 0; j < INPUT_URBS; ++j) {
366  in->urbs[j]->dev = umidi->dev;
367  snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
368  }
369  }
370  if (umidi->endpoints[i].out)
371  snd_usbmidi_do_output(umidi->endpoints[i].out);
372  }
373  spin_unlock(&umidi->disc_lock);
374 }
375 
376 /* helper function to send static data that may not DMA-able */
377 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
378  const void *data, int len)
379 {
380  int err = 0;
381  void *buf = kmemdup(data, len, GFP_KERNEL);
382  if (!buf)
383  return -ENOMEM;
384  dump_urb("sending", buf, len);
385  if (ep->urbs[0].urb)
386  err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
387  buf, len, NULL, 250);
388  kfree(buf);
389  return err;
390 }
391 
392 /*
393  * Standard USB MIDI protocol: see the spec.
394  * Midiman protocol: like the standard protocol, but the control byte is the
395  * fourth byte in each packet, and uses length instead of CIN.
396  */
397 
398 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
400 {
401  int i;
402 
403  for (i = 0; i + 3 < buffer_length; i += 4)
404  if (buffer[i] != 0) {
405  int cable = buffer[i] >> 4;
406  int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
407  snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
408  }
409 }
410 
411 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
412  uint8_t* buffer, int buffer_length)
413 {
414  int i;
415 
416  for (i = 0; i + 3 < buffer_length; i += 4)
417  if (buffer[i + 3] != 0) {
418  int port = buffer[i + 3] >> 4;
419  int length = buffer[i + 3] & 3;
420  snd_usbmidi_input_data(ep, port, &buffer[i], length);
421  }
422 }
423 
424 /*
425  * Buggy M-Audio device: running status on input results in a packet that has
426  * the data bytes but not the status byte and that is marked with CIN 4.
427  */
428 static void snd_usbmidi_maudio_broken_running_status_input(
429  struct snd_usb_midi_in_endpoint* ep,
430  uint8_t* buffer, int buffer_length)
431 {
432  int i;
433 
434  for (i = 0; i + 3 < buffer_length; i += 4)
435  if (buffer[i] != 0) {
436  int cable = buffer[i] >> 4;
437  u8 cin = buffer[i] & 0x0f;
438  struct usbmidi_in_port *port = &ep->ports[cable];
439  int length;
440 
441  length = snd_usbmidi_cin_length[cin];
442  if (cin == 0xf && buffer[i + 1] >= 0xf8)
443  ; /* realtime msg: no running status change */
444  else if (cin >= 0x8 && cin <= 0xe)
445  /* channel msg */
446  port->running_status_length = length - 1;
447  else if (cin == 0x4 &&
448  port->running_status_length != 0 &&
449  buffer[i + 1] < 0x80)
450  /* CIN 4 that is not a SysEx */
451  length = port->running_status_length;
452  else
453  /*
454  * All other msgs cannot begin running status.
455  * (A channel msg sent as two or three CIN 0xF
456  * packets could in theory, but this device
457  * doesn't use this format.)
458  */
459  port->running_status_length = 0;
460  snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
461  }
462 }
463 
464 /*
465  * CME protocol: like the standard protocol, but SysEx commands are sent as a
466  * single USB packet preceded by a 0x0F byte.
467  */
468 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
469  uint8_t *buffer, int buffer_length)
470 {
471  if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
472  snd_usbmidi_standard_input(ep, buffer, buffer_length);
473  else
474  snd_usbmidi_input_data(ep, buffer[0] >> 4,
475  &buffer[1], buffer_length - 1);
476 }
477 
478 /*
479  * Adds one USB MIDI packet to the output buffer.
480  */
481 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
482  uint8_t p1, uint8_t p2, uint8_t p3)
483 {
484 
485  uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
486  buf[0] = p0;
487  buf[1] = p1;
488  buf[2] = p2;
489  buf[3] = p3;
490  urb->transfer_buffer_length += 4;
491 }
492 
493 /*
494  * Adds one Midiman packet to the output buffer.
495  */
496 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
497  uint8_t p1, uint8_t p2, uint8_t p3)
498 {
499 
500  uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
501  buf[0] = p1;
502  buf[1] = p2;
503  buf[2] = p3;
504  buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
505  urb->transfer_buffer_length += 4;
506 }
507 
508 /*
509  * Converts MIDI commands to USB MIDI packets.
510  */
511 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
512  uint8_t b, struct urb* urb)
513 {
514  uint8_t p0 = port->cable;
515  void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
516  port->ep->umidi->usb_protocol_ops->output_packet;
517 
518  if (b >= 0xf8) {
519  output_packet(urb, p0 | 0x0f, b, 0, 0);
520  } else if (b >= 0xf0) {
521  switch (b) {
522  case 0xf0:
523  port->data[0] = b;
524  port->state = STATE_SYSEX_1;
525  break;
526  case 0xf1:
527  case 0xf3:
528  port->data[0] = b;
529  port->state = STATE_1PARAM;
530  break;
531  case 0xf2:
532  port->data[0] = b;
533  port->state = STATE_2PARAM_1;
534  break;
535  case 0xf4:
536  case 0xf5:
537  port->state = STATE_UNKNOWN;
538  break;
539  case 0xf6:
540  output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
541  port->state = STATE_UNKNOWN;
542  break;
543  case 0xf7:
544  switch (port->state) {
545  case STATE_SYSEX_0:
546  output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
547  break;
548  case STATE_SYSEX_1:
549  output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
550  break;
551  case STATE_SYSEX_2:
552  output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
553  break;
554  }
555  port->state = STATE_UNKNOWN;
556  break;
557  }
558  } else if (b >= 0x80) {
559  port->data[0] = b;
560  if (b >= 0xc0 && b <= 0xdf)
561  port->state = STATE_1PARAM;
562  else
563  port->state = STATE_2PARAM_1;
564  } else { /* b < 0x80 */
565  switch (port->state) {
566  case STATE_1PARAM:
567  if (port->data[0] < 0xf0) {
568  p0 |= port->data[0] >> 4;
569  } else {
570  p0 |= 0x02;
571  port->state = STATE_UNKNOWN;
572  }
573  output_packet(urb, p0, port->data[0], b, 0);
574  break;
575  case STATE_2PARAM_1:
576  port->data[1] = b;
577  port->state = STATE_2PARAM_2;
578  break;
579  case STATE_2PARAM_2:
580  if (port->data[0] < 0xf0) {
581  p0 |= port->data[0] >> 4;
582  port->state = STATE_2PARAM_1;
583  } else {
584  p0 |= 0x03;
585  port->state = STATE_UNKNOWN;
586  }
587  output_packet(urb, p0, port->data[0], port->data[1], b);
588  break;
589  case STATE_SYSEX_0:
590  port->data[0] = b;
591  port->state = STATE_SYSEX_1;
592  break;
593  case STATE_SYSEX_1:
594  port->data[1] = b;
595  port->state = STATE_SYSEX_2;
596  break;
597  case STATE_SYSEX_2:
598  output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
599  port->state = STATE_SYSEX_0;
600  break;
601  }
602  }
603 }
604 
605 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
606  struct urb *urb)
607 {
608  int p;
609 
610  /* FIXME: lower-numbered ports can starve higher-numbered ports */
611  for (p = 0; p < 0x10; ++p) {
612  struct usbmidi_out_port* port = &ep->ports[p];
613  if (!port->active)
614  continue;
615  while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
616  uint8_t b;
617  if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
618  port->active = 0;
619  break;
620  }
621  snd_usbmidi_transmit_byte(port, b, urb);
622  }
623  }
624 }
625 
626 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
627  .input = snd_usbmidi_standard_input,
628  .output = snd_usbmidi_standard_output,
629  .output_packet = snd_usbmidi_output_standard_packet,
630 };
631 
632 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
633  .input = snd_usbmidi_midiman_input,
634  .output = snd_usbmidi_standard_output,
635  .output_packet = snd_usbmidi_output_midiman_packet,
636 };
637 
638 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
639  .input = snd_usbmidi_maudio_broken_running_status_input,
640  .output = snd_usbmidi_standard_output,
641  .output_packet = snd_usbmidi_output_standard_packet,
642 };
643 
644 static struct usb_protocol_ops snd_usbmidi_cme_ops = {
645  .input = snd_usbmidi_cme_input,
646  .output = snd_usbmidi_standard_output,
647  .output_packet = snd_usbmidi_output_standard_packet,
648 };
649 
650 /*
651  * AKAI MPD16 protocol:
652  *
653  * For control port (endpoint 1):
654  * ==============================
655  * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
656  * SysEx message (msg_len=9 bytes long).
657  *
658  * For data port (endpoint 2):
659  * ===========================
660  * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
661  * MIDI message (msg_len bytes long)
662  *
663  * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
664  */
665 static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
666  uint8_t *buffer, int buffer_length)
667 {
668  unsigned int pos = 0;
669  unsigned int len = (unsigned int)buffer_length;
670  while (pos < len) {
671  unsigned int port = (buffer[pos] >> 4) - 1;
672  unsigned int msg_len = buffer[pos] & 0x0f;
673  pos++;
674  if (pos + msg_len <= len && port < 2)
675  snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
676  pos += msg_len;
677  }
678 }
679 
680 #define MAX_AKAI_SYSEX_LEN 9
681 
682 static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
683  struct urb *urb)
684 {
685  uint8_t *msg;
686  int pos, end, count, buf_end;
687  uint8_t tmp[MAX_AKAI_SYSEX_LEN];
688  struct snd_rawmidi_substream *substream = ep->ports[0].substream;
689 
690  if (!ep->ports[0].active)
691  return;
692 
693  msg = urb->transfer_buffer + urb->transfer_buffer_length;
694  buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
695 
696  /* only try adding more data when there's space for at least 1 SysEx */
697  while (urb->transfer_buffer_length < buf_end) {
698  count = snd_rawmidi_transmit_peek(substream,
699  tmp, MAX_AKAI_SYSEX_LEN);
700  if (!count) {
701  ep->ports[0].active = 0;
702  return;
703  }
704  /* try to skip non-SysEx data */
705  for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
706  ;
707 
708  if (pos > 0) {
709  snd_rawmidi_transmit_ack(substream, pos);
710  continue;
711  }
712 
713  /* look for the start or end marker */
714  for (end = 1; end < count && tmp[end] < 0xF0; end++)
715  ;
716 
717  /* next SysEx started before the end of current one */
718  if (end < count && tmp[end] == 0xF0) {
719  /* it's incomplete - drop it */
720  snd_rawmidi_transmit_ack(substream, end);
721  continue;
722  }
723  /* SysEx complete */
724  if (end < count && tmp[end] == 0xF7) {
725  /* queue it, ack it, and get the next one */
726  count = end + 1;
727  msg[0] = 0x10 | count;
728  memcpy(&msg[1], tmp, count);
729  snd_rawmidi_transmit_ack(substream, count);
730  urb->transfer_buffer_length += count + 1;
731  msg += count + 1;
732  continue;
733  }
734  /* less than 9 bytes and no end byte - wait for more */
735  if (count < MAX_AKAI_SYSEX_LEN) {
736  ep->ports[0].active = 0;
737  return;
738  }
739  /* 9 bytes and no end marker in sight - malformed, skip it */
740  snd_rawmidi_transmit_ack(substream, count);
741  }
742 }
743 
744 static struct usb_protocol_ops snd_usbmidi_akai_ops = {
745  .input = snd_usbmidi_akai_input,
746  .output = snd_usbmidi_akai_output,
747 };
748 
749 /*
750  * Novation USB MIDI protocol: number of data bytes is in the first byte
751  * (when receiving) (+1!) or in the second byte (when sending); data begins
752  * at the third byte.
753  */
754 
755 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
756  uint8_t* buffer, int buffer_length)
757 {
758  if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
759  return;
760  snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
761 }
762 
763 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
764  struct urb *urb)
765 {
766  uint8_t* transfer_buffer;
767  int count;
768 
769  if (!ep->ports[0].active)
770  return;
771  transfer_buffer = urb->transfer_buffer;
772  count = snd_rawmidi_transmit(ep->ports[0].substream,
773  &transfer_buffer[2],
774  ep->max_transfer - 2);
775  if (count < 1) {
776  ep->ports[0].active = 0;
777  return;
778  }
779  transfer_buffer[0] = 0;
780  transfer_buffer[1] = count;
781  urb->transfer_buffer_length = 2 + count;
782 }
783 
784 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
785  .input = snd_usbmidi_novation_input,
786  .output = snd_usbmidi_novation_output,
787 };
788 
789 /*
790  * "raw" protocol: just move raw MIDI bytes from/to the endpoint
791  */
792 
793 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
794  uint8_t* buffer, int buffer_length)
795 {
796  snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
797 }
798 
799 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
800  struct urb *urb)
801 {
802  int count;
803 
804  if (!ep->ports[0].active)
805  return;
806  count = snd_rawmidi_transmit(ep->ports[0].substream,
807  urb->transfer_buffer,
808  ep->max_transfer);
809  if (count < 1) {
810  ep->ports[0].active = 0;
811  return;
812  }
813  urb->transfer_buffer_length = count;
814 }
815 
816 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
817  .input = snd_usbmidi_raw_input,
818  .output = snd_usbmidi_raw_output,
819 };
820 
821 /*
822  * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
823  */
824 
825 static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint* ep,
826  uint8_t* buffer, int buffer_length)
827 {
828  if (buffer_length > 2)
829  snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
830 }
831 
832 static struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
833  .input = snd_usbmidi_ftdi_input,
834  .output = snd_usbmidi_raw_output,
835 };
836 
837 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
838  uint8_t *buffer, int buffer_length)
839 {
840  if (buffer_length != 9)
841  return;
842  buffer_length = 8;
843  while (buffer_length && buffer[buffer_length - 1] == 0xFD)
844  buffer_length--;
845  if (buffer_length)
846  snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
847 }
848 
849 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
850  struct urb *urb)
851 {
852  int count;
853 
854  if (!ep->ports[0].active)
855  return;
856  switch (snd_usb_get_speed(ep->umidi->dev)) {
857  case USB_SPEED_HIGH:
858  case USB_SPEED_SUPER:
859  count = 1;
860  break;
861  default:
862  count = 2;
863  }
864  count = snd_rawmidi_transmit(ep->ports[0].substream,
865  urb->transfer_buffer,
866  count);
867  if (count < 1) {
868  ep->ports[0].active = 0;
869  return;
870  }
871 
872  memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
873  urb->transfer_buffer_length = ep->max_transfer;
874 }
875 
876 static struct usb_protocol_ops snd_usbmidi_122l_ops = {
877  .input = snd_usbmidi_us122l_input,
878  .output = snd_usbmidi_us122l_output,
879 };
880 
881 /*
882  * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
883  */
884 
885 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
886 {
887  static const u8 init_data[] = {
888  /* initialization magic: "get version" */
889  0xf0,
890  0x00, 0x20, 0x31, /* Emagic */
891  0x64, /* Unitor8 */
892  0x0b, /* version number request */
893  0x00, /* command version */
894  0x00, /* EEPROM, box 0 */
895  0xf7
896  };
897  send_bulk_static_data(ep, init_data, sizeof(init_data));
898  /* while we're at it, pour on more magic */
899  send_bulk_static_data(ep, init_data, sizeof(init_data));
900 }
901 
902 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
903 {
904  static const u8 finish_data[] = {
905  /* switch to patch mode with last preset */
906  0xf0,
907  0x00, 0x20, 0x31, /* Emagic */
908  0x64, /* Unitor8 */
909  0x10, /* patch switch command */
910  0x00, /* command version */
911  0x7f, /* to all boxes */
912  0x40, /* last preset in EEPROM */
913  0xf7
914  };
915  send_bulk_static_data(ep, finish_data, sizeof(finish_data));
916 }
917 
918 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
919  uint8_t* buffer, int buffer_length)
920 {
921  int i;
922 
923  /* FF indicates end of valid data */
924  for (i = 0; i < buffer_length; ++i)
925  if (buffer[i] == 0xff) {
926  buffer_length = i;
927  break;
928  }
929 
930  /* handle F5 at end of last buffer */
931  if (ep->seen_f5)
932  goto switch_port;
933 
934  while (buffer_length > 0) {
935  /* determine size of data until next F5 */
936  for (i = 0; i < buffer_length; ++i)
937  if (buffer[i] == 0xf5)
938  break;
939  snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
940  buffer += i;
941  buffer_length -= i;
942 
943  if (buffer_length <= 0)
944  break;
945  /* assert(buffer[0] == 0xf5); */
946  ep->seen_f5 = 1;
947  ++buffer;
948  --buffer_length;
949 
950  switch_port:
951  if (buffer_length <= 0)
952  break;
953  if (buffer[0] < 0x80) {
954  ep->current_port = (buffer[0] - 1) & 15;
955  ++buffer;
956  --buffer_length;
957  }
958  ep->seen_f5 = 0;
959  }
960 }
961 
962 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
963  struct urb *urb)
964 {
965  int port0 = ep->current_port;
966  uint8_t* buf = urb->transfer_buffer;
967  int buf_free = ep->max_transfer;
968  int length, i;
969 
970  for (i = 0; i < 0x10; ++i) {
971  /* round-robin, starting at the last current port */
972  int portnum = (port0 + i) & 15;
973  struct usbmidi_out_port* port = &ep->ports[portnum];
974 
975  if (!port->active)
976  continue;
977  if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
978  port->active = 0;
979  continue;
980  }
981 
982  if (portnum != ep->current_port) {
983  if (buf_free < 2)
984  break;
985  ep->current_port = portnum;
986  buf[0] = 0xf5;
987  buf[1] = (portnum + 1) & 15;
988  buf += 2;
989  buf_free -= 2;
990  }
991 
992  if (buf_free < 1)
993  break;
994  length = snd_rawmidi_transmit(port->substream, buf, buf_free);
995  if (length > 0) {
996  buf += length;
997  buf_free -= length;
998  if (buf_free < 1)
999  break;
1000  }
1001  }
1002  if (buf_free < ep->max_transfer && buf_free > 0) {
1003  *buf = 0xff;
1004  --buf_free;
1005  }
1006  urb->transfer_buffer_length = ep->max_transfer - buf_free;
1007 }
1008 
1009 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
1010  .input = snd_usbmidi_emagic_input,
1011  .output = snd_usbmidi_emagic_output,
1012  .init_out_endpoint = snd_usbmidi_emagic_init_out,
1013  .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
1014 };
1015 
1016 
1017 static void update_roland_altsetting(struct snd_usb_midi* umidi)
1018 {
1019  struct usb_interface *intf;
1020  struct usb_host_interface *hostif;
1021  struct usb_interface_descriptor *intfd;
1022  int is_light_load;
1023 
1024  intf = umidi->iface;
1025  is_light_load = intf->cur_altsetting != intf->altsetting;
1026  if (umidi->roland_load_ctl->private_value == is_light_load)
1027  return;
1028  hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
1029  intfd = get_iface_desc(hostif);
1030  snd_usbmidi_input_stop(&umidi->list);
1031  usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1032  intfd->bAlternateSetting);
1033  snd_usbmidi_input_start(&umidi->list);
1034 }
1035 
1036 static void substream_open(struct snd_rawmidi_substream *substream, int open)
1037 {
1038  struct snd_usb_midi* umidi = substream->rmidi->private_data;
1039  struct snd_kcontrol *ctl;
1040 
1041  mutex_lock(&umidi->mutex);
1042  if (open) {
1043  if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
1044  ctl = umidi->roland_load_ctl;
1045  ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1046  snd_ctl_notify(umidi->card,
1047  SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1048  update_roland_altsetting(umidi);
1049  }
1050  } else {
1051  if (--umidi->opened == 0 && umidi->roland_load_ctl) {
1052  ctl = umidi->roland_load_ctl;
1053  ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1054  snd_ctl_notify(umidi->card,
1055  SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1056  }
1057  }
1058  mutex_unlock(&umidi->mutex);
1059 }
1060 
1061 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
1062 {
1063  struct snd_usb_midi* umidi = substream->rmidi->private_data;
1064  struct usbmidi_out_port* port = NULL;
1065  int i, j;
1066  int err;
1067 
1068  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1069  if (umidi->endpoints[i].out)
1070  for (j = 0; j < 0x10; ++j)
1071  if (umidi->endpoints[i].out->ports[j].substream == substream) {
1072  port = &umidi->endpoints[i].out->ports[j];
1073  break;
1074  }
1075  if (!port) {
1076  snd_BUG();
1077  return -ENXIO;
1078  }
1079  err = usb_autopm_get_interface(umidi->iface);
1080  port->autopm_reference = err >= 0;
1081  if (err < 0 && err != -EACCES)
1082  return -EIO;
1083  substream->runtime->private_data = port;
1084  port->state = STATE_UNKNOWN;
1085  substream_open(substream, 1);
1086  return 0;
1087 }
1088 
1089 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
1090 {
1091  struct snd_usb_midi* umidi = substream->rmidi->private_data;
1092  struct usbmidi_out_port *port = substream->runtime->private_data;
1093 
1094  substream_open(substream, 0);
1095  if (port->autopm_reference)
1096  usb_autopm_put_interface(umidi->iface);
1097  return 0;
1098 }
1099 
1100 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1101 {
1102  struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
1103 
1104  port->active = up;
1105  if (up) {
1106  if (port->ep->umidi->disconnected) {
1107  /* gobble up remaining bytes to prevent wait in
1108  * snd_rawmidi_drain_output */
1109  while (!snd_rawmidi_transmit_empty(substream))
1110  snd_rawmidi_transmit_ack(substream, 1);
1111  return;
1112  }
1113  tasklet_schedule(&port->ep->tasklet);
1114  }
1115 }
1116 
1117 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
1118 {
1119  struct usbmidi_out_port* port = substream->runtime->private_data;
1120  struct snd_usb_midi_out_endpoint *ep = port->ep;
1121  unsigned int drain_urbs;
1122  DEFINE_WAIT(wait);
1123  long timeout = msecs_to_jiffies(50);
1124 
1125  if (ep->umidi->disconnected)
1126  return;
1127  /*
1128  * The substream buffer is empty, but some data might still be in the
1129  * currently active URBs, so we have to wait for those to complete.
1130  */
1131  spin_lock_irq(&ep->buffer_lock);
1132  drain_urbs = ep->active_urbs;
1133  if (drain_urbs) {
1134  ep->drain_urbs |= drain_urbs;
1135  do {
1138  spin_unlock_irq(&ep->buffer_lock);
1139  timeout = schedule_timeout(timeout);
1140  spin_lock_irq(&ep->buffer_lock);
1141  drain_urbs &= ep->drain_urbs;
1142  } while (drain_urbs && timeout);
1143  finish_wait(&ep->drain_wait, &wait);
1144  }
1145  spin_unlock_irq(&ep->buffer_lock);
1146 }
1147 
1148 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
1149 {
1150  substream_open(substream, 1);
1151  return 0;
1152 }
1153 
1154 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
1155 {
1156  substream_open(substream, 0);
1157  return 0;
1158 }
1159 
1160 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1161 {
1162  struct snd_usb_midi* umidi = substream->rmidi->private_data;
1163 
1164  if (up)
1165  set_bit(substream->number, &umidi->input_triggered);
1166  else
1167  clear_bit(substream->number, &umidi->input_triggered);
1168 }
1169 
1170 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
1171  .open = snd_usbmidi_output_open,
1172  .close = snd_usbmidi_output_close,
1173  .trigger = snd_usbmidi_output_trigger,
1174  .drain = snd_usbmidi_output_drain,
1175 };
1176 
1177 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
1178  .open = snd_usbmidi_input_open,
1179  .close = snd_usbmidi_input_close,
1180  .trigger = snd_usbmidi_input_trigger
1181 };
1182 
1183 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1184  unsigned int buffer_length)
1185 {
1186  usb_free_coherent(umidi->dev, buffer_length,
1187  urb->transfer_buffer, urb->transfer_dma);
1188  usb_free_urb(urb);
1189 }
1190 
1191 /*
1192  * Frees an input endpoint.
1193  * May be called when ep hasn't been initialized completely.
1194  */
1195 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
1196 {
1197  unsigned int i;
1198 
1199  for (i = 0; i < INPUT_URBS; ++i)
1200  if (ep->urbs[i])
1201  free_urb_and_buffer(ep->umidi, ep->urbs[i],
1202  ep->urbs[i]->transfer_buffer_length);
1203  kfree(ep);
1204 }
1205 
1206 /*
1207  * Creates an input endpoint.
1208  */
1209 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1211  struct snd_usb_midi_endpoint* rep)
1212 {
1213  struct snd_usb_midi_in_endpoint* ep;
1214  void* buffer;
1215  unsigned int pipe;
1216  int length;
1217  unsigned int i;
1218 
1219  rep->in = NULL;
1220  ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1221  if (!ep)
1222  return -ENOMEM;
1223  ep->umidi = umidi;
1224 
1225  for (i = 0; i < INPUT_URBS; ++i) {
1226  ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1227  if (!ep->urbs[i]) {
1228  snd_usbmidi_in_endpoint_delete(ep);
1229  return -ENOMEM;
1230  }
1231  }
1232  if (ep_info->in_interval)
1233  pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
1234  else
1235  pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1236  length = usb_maxpacket(umidi->dev, pipe, 0);
1237  for (i = 0; i < INPUT_URBS; ++i) {
1238  buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1239  &ep->urbs[i]->transfer_dma);
1240  if (!buffer) {
1241  snd_usbmidi_in_endpoint_delete(ep);
1242  return -ENOMEM;
1243  }
1244  if (ep_info->in_interval)
1245  usb_fill_int_urb(ep->urbs[i], umidi->dev,
1246  pipe, buffer, length,
1247  snd_usbmidi_in_urb_complete,
1248  ep, ep_info->in_interval);
1249  else
1250  usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
1251  pipe, buffer, length,
1252  snd_usbmidi_in_urb_complete, ep);
1253  ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1254  }
1255 
1256  rep->in = ep;
1257  return 0;
1258 }
1259 
1260 /*
1261  * Frees an output endpoint.
1262  * May be called when ep hasn't been initialized completely.
1263  */
1264 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
1265 {
1266  unsigned int i;
1267 
1268  for (i = 0; i < OUTPUT_URBS; ++i)
1269  if (ep->urbs[i].urb) {
1270  free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1271  ep->max_transfer);
1272  ep->urbs[i].urb = NULL;
1273  }
1274 }
1275 
1276 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1277 {
1278  snd_usbmidi_out_endpoint_clear(ep);
1279  kfree(ep);
1280 }
1281 
1282 /*
1283  * Creates an output endpoint, and initializes output ports.
1284  */
1285 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1286  struct snd_usb_midi_endpoint_info* ep_info,
1287  struct snd_usb_midi_endpoint* rep)
1288 {
1289  struct snd_usb_midi_out_endpoint* ep;
1290  unsigned int i;
1291  unsigned int pipe;
1292  void* buffer;
1293 
1294  rep->out = NULL;
1295  ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1296  if (!ep)
1297  return -ENOMEM;
1298  ep->umidi = umidi;
1299 
1300  for (i = 0; i < OUTPUT_URBS; ++i) {
1301  ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1302  if (!ep->urbs[i].urb) {
1303  snd_usbmidi_out_endpoint_delete(ep);
1304  return -ENOMEM;
1305  }
1306  ep->urbs[i].ep = ep;
1307  }
1308  if (ep_info->out_interval)
1309  pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
1310  else
1311  pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
1312  switch (umidi->usb_id) {
1313  default:
1314  ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
1315  break;
1316  /*
1317  * Various chips declare a packet size larger than 4 bytes, but
1318  * do not actually work with larger packets:
1319  */
1320  case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1321  case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1322  case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1323  case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1324  case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1325  case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
1326  ep->max_transfer = 4;
1327  break;
1328  /*
1329  * Some devices only work with 9 bytes packet size:
1330  */
1331  case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
1332  case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
1333  ep->max_transfer = 9;
1334  break;
1335  }
1336  for (i = 0; i < OUTPUT_URBS; ++i) {
1337  buffer = usb_alloc_coherent(umidi->dev,
1338  ep->max_transfer, GFP_KERNEL,
1339  &ep->urbs[i].urb->transfer_dma);
1340  if (!buffer) {
1341  snd_usbmidi_out_endpoint_delete(ep);
1342  return -ENOMEM;
1343  }
1344  if (ep_info->out_interval)
1345  usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
1346  pipe, buffer, ep->max_transfer,
1347  snd_usbmidi_out_urb_complete,
1348  &ep->urbs[i], ep_info->out_interval);
1349  else
1350  usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
1351  pipe, buffer, ep->max_transfer,
1352  snd_usbmidi_out_urb_complete,
1353  &ep->urbs[i]);
1354  ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1355  }
1356 
1358  tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1360 
1361  for (i = 0; i < 0x10; ++i)
1362  if (ep_info->out_cables & (1 << i)) {
1363  ep->ports[i].ep = ep;
1364  ep->ports[i].cable = i << 4;
1365  }
1366 
1367  if (umidi->usb_protocol_ops->init_out_endpoint)
1368  umidi->usb_protocol_ops->init_out_endpoint(ep);
1369 
1370  rep->out = ep;
1371  return 0;
1372 }
1373 
1374 /*
1375  * Frees everything.
1376  */
1377 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1378 {
1379  int i;
1380 
1381  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1382  struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1383  if (ep->out)
1384  snd_usbmidi_out_endpoint_delete(ep->out);
1385  if (ep->in)
1386  snd_usbmidi_in_endpoint_delete(ep->in);
1387  }
1388  mutex_destroy(&umidi->mutex);
1389  kfree(umidi);
1390 }
1391 
1392 /*
1393  * Unlinks all URBs (must be done before the usb_device is deleted).
1394  */
1396 {
1397  struct snd_usb_midi* umidi;
1398  unsigned int i, j;
1399 
1400  umidi = list_entry(p, struct snd_usb_midi, list);
1401  /*
1402  * an URB's completion handler may start the timer and
1403  * a timer may submit an URB. To reliably break the cycle
1404  * a flag under lock must be used
1405  */
1406  spin_lock_irq(&umidi->disc_lock);
1407  umidi->disconnected = 1;
1408  spin_unlock_irq(&umidi->disc_lock);
1409  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1410  struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1411  if (ep->out)
1412  tasklet_kill(&ep->out->tasklet);
1413  if (ep->out) {
1414  for (j = 0; j < OUTPUT_URBS; ++j)
1415  usb_kill_urb(ep->out->urbs[j].urb);
1416  if (umidi->usb_protocol_ops->finish_out_endpoint)
1417  umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1418  ep->out->active_urbs = 0;
1419  if (ep->out->drain_urbs) {
1420  ep->out->drain_urbs = 0;
1421  wake_up(&ep->out->drain_wait);
1422  }
1423  }
1424  if (ep->in)
1425  for (j = 0; j < INPUT_URBS; ++j)
1426  usb_kill_urb(ep->in->urbs[j]);
1427  /* free endpoints here; later call can result in Oops */
1428  if (ep->out)
1429  snd_usbmidi_out_endpoint_clear(ep->out);
1430  if (ep->in) {
1431  snd_usbmidi_in_endpoint_delete(ep->in);
1432  ep->in = NULL;
1433  }
1434  }
1435  del_timer_sync(&umidi->error_timer);
1436 }
1437 
1438 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1439 {
1440  struct snd_usb_midi* umidi = rmidi->private_data;
1441  snd_usbmidi_free(umidi);
1442 }
1443 
1444 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1445  int stream, int number)
1446 {
1447  struct list_head* list;
1448 
1449  list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1450  struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1451  if (substream->number == number)
1452  return substream;
1453  }
1454  return NULL;
1455 }
1456 
1457 /*
1458  * This list specifies names for ports that do not fit into the standard
1459  * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1460  * such as internal control or synthesizer ports.
1461  */
1462 static struct port_info {
1464  short int port;
1465  short int voices;
1466  const char *name;
1467  unsigned int seq_flags;
1468 } snd_usbmidi_port_info[] = {
1469 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1470  { .id = USB_ID(vendor, product), \
1471  .port = num, .voices = voices_, \
1472  .name = name_, .seq_flags = flags }
1473 #define EXTERNAL_PORT(vendor, product, num, name) \
1474  PORT_INFO(vendor, product, num, name, 0, \
1475  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1476  SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1477  SNDRV_SEQ_PORT_TYPE_PORT)
1478 #define CONTROL_PORT(vendor, product, num, name) \
1479  PORT_INFO(vendor, product, num, name, 0, \
1480  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1481  SNDRV_SEQ_PORT_TYPE_HARDWARE)
1482 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1483  PORT_INFO(vendor, product, num, name, voices, \
1484  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1485  SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1486  SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1487  SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1488  SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1489  SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1490  SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1491 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1492  PORT_INFO(vendor, product, num, name, voices, \
1493  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1494  SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1495  SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1496  SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1497  SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1498  SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1499  SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1500  SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1501  /* Roland UA-100 */
1502  CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1503  /* Roland SC-8850 */
1504  SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1505  SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1506  SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1507  SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1508  EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1509  EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1510  /* Roland U-8 */
1511  EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1512  CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1513  /* Roland SC-8820 */
1514  SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1515  SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1516  EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1517  /* Roland SK-500 */
1518  SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1519  SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1520  EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1521  /* Roland SC-D70 */
1522  SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1523  SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1524  EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1525  /* Edirol UM-880 */
1526  CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1527  /* Edirol SD-90 */
1528  ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1529  ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1530  EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1531  EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1532  /* Edirol UM-550 */
1533  CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1534  /* Edirol SD-20 */
1535  ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1536  ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1537  EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1538  /* Edirol SD-80 */
1539  ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1540  ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1541  EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1542  EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1543  /* Edirol UA-700 */
1544  EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1545  CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1546  /* Roland VariOS */
1547  EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1548  EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1549  EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1550  /* Edirol PCR */
1551  EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1552  EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1553  EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1554  /* BOSS GS-10 */
1555  EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1556  CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1557  /* Edirol UA-1000 */
1558  EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1559  CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1560  /* Edirol UR-80 */
1561  EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1562  EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1563  EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1564  /* Edirol PCR-A */
1565  EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1566  EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1567  EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1568  /* Edirol UM-3EX */
1569  CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1570  /* M-Audio MidiSport 8x8 */
1571  CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1572  CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1573  /* MOTU Fastlane */
1574  EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1575  EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1576  /* Emagic Unitor8/AMT8/MT4 */
1577  EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1578  EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1579  EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1580  /* Akai MPD16 */
1581  CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1582  PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1585  /* Access Music Virus TI */
1586  EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1587  PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1591 };
1592 
1593 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1594 {
1595  int i;
1596 
1597  for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1598  if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
1599  snd_usbmidi_port_info[i].port == number)
1600  return &snd_usbmidi_port_info[i];
1601  }
1602  return NULL;
1603 }
1604 
1605 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1606  struct snd_seq_port_info *seq_port_info)
1607 {
1608  struct snd_usb_midi *umidi = rmidi->private_data;
1609  struct port_info *port_info;
1610 
1611  /* TODO: read port flags from descriptors */
1612  port_info = find_port_info(umidi, number);
1613  if (port_info) {
1614  seq_port_info->type = port_info->seq_flags;
1615  seq_port_info->midi_voices = port_info->voices;
1616  }
1617 }
1618 
1619 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1620  int stream, int number,
1621  struct snd_rawmidi_substream ** rsubstream)
1622 {
1623  struct port_info *port_info;
1624  const char *name_format;
1625 
1626  struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1627  if (!substream) {
1628  snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1629  return;
1630  }
1631 
1632  /* TODO: read port name from jack descriptor */
1633  port_info = find_port_info(umidi, number);
1634  name_format = port_info ? port_info->name : "%s MIDI %d";
1635  snprintf(substream->name, sizeof(substream->name),
1636  name_format, umidi->card->shortname, number + 1);
1637 
1638  *rsubstream = substream;
1639 }
1640 
1641 /*
1642  * Creates the endpoints and their ports.
1643  */
1644 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1646 {
1647  int i, j, err;
1648  int out_ports = 0, in_ports = 0;
1649 
1650  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1651  if (endpoints[i].out_cables) {
1652  err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1653  &umidi->endpoints[i]);
1654  if (err < 0)
1655  return err;
1656  }
1657  if (endpoints[i].in_cables) {
1658  err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1659  &umidi->endpoints[i]);
1660  if (err < 0)
1661  return err;
1662  }
1663 
1664  for (j = 0; j < 0x10; ++j) {
1665  if (endpoints[i].out_cables & (1 << j)) {
1666  snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1667  &umidi->endpoints[i].out->ports[j].substream);
1668  ++out_ports;
1669  }
1670  if (endpoints[i].in_cables & (1 << j)) {
1671  snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1672  &umidi->endpoints[i].in->ports[j].substream);
1673  ++in_ports;
1674  }
1675  }
1676  }
1677  snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1678  out_ports, in_ports);
1679  return 0;
1680 }
1681 
1682 /*
1683  * Returns MIDIStreaming device capabilities.
1684  */
1685 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1686  struct snd_usb_midi_endpoint_info* endpoints)
1687 {
1688  struct usb_interface* intf;
1689  struct usb_host_interface *hostif;
1690  struct usb_interface_descriptor* intfd;
1691  struct usb_ms_header_descriptor* ms_header;
1692  struct usb_host_endpoint *hostep;
1693  struct usb_endpoint_descriptor* ep;
1694  struct usb_ms_endpoint_descriptor* ms_ep;
1695  int i, epidx;
1696 
1697  intf = umidi->iface;
1698  if (!intf)
1699  return -ENXIO;
1700  hostif = &intf->altsetting[0];
1701  intfd = get_iface_desc(hostif);
1702  ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1703  if (hostif->extralen >= 7 &&
1704  ms_header->bLength >= 7 &&
1705  ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1706  ms_header->bDescriptorSubtype == UAC_HEADER)
1707  snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1708  ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1709  else
1710  snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1711 
1712  epidx = 0;
1713  for (i = 0; i < intfd->bNumEndpoints; ++i) {
1714  hostep = &hostif->endpoint[i];
1715  ep = get_ep_desc(hostep);
1716  if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
1717  continue;
1718  ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1719  if (hostep->extralen < 4 ||
1720  ms_ep->bLength < 4 ||
1721  ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1723  continue;
1724  if (usb_endpoint_dir_out(ep)) {
1725  if (endpoints[epidx].out_ep) {
1726  if (++epidx >= MIDI_MAX_ENDPOINTS) {
1727  snd_printk(KERN_WARNING "too many endpoints\n");
1728  break;
1729  }
1730  }
1731  endpoints[epidx].out_ep = usb_endpoint_num(ep);
1732  if (usb_endpoint_xfer_int(ep))
1733  endpoints[epidx].out_interval = ep->bInterval;
1734  else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1735  /*
1736  * Low speed bulk transfers don't exist, so
1737  * force interrupt transfers for devices like
1738  * ESI MIDI Mate that try to use them anyway.
1739  */
1740  endpoints[epidx].out_interval = 1;
1741  endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1742  snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1743  ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1744  } else {
1745  if (endpoints[epidx].in_ep) {
1746  if (++epidx >= MIDI_MAX_ENDPOINTS) {
1747  snd_printk(KERN_WARNING "too many endpoints\n");
1748  break;
1749  }
1750  }
1751  endpoints[epidx].in_ep = usb_endpoint_num(ep);
1752  if (usb_endpoint_xfer_int(ep))
1753  endpoints[epidx].in_interval = ep->bInterval;
1754  else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1755  endpoints[epidx].in_interval = 1;
1756  endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1757  snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1758  ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1759  }
1760  }
1761  return 0;
1762 }
1763 
1764 static int roland_load_info(struct snd_kcontrol *kcontrol,
1765  struct snd_ctl_elem_info *info)
1766 {
1767  static const char *const names[] = { "High Load", "Light Load" };
1768 
1769  return snd_ctl_enum_info(info, 1, 2, names);
1770 }
1771 
1772 static int roland_load_get(struct snd_kcontrol *kcontrol,
1773  struct snd_ctl_elem_value *value)
1774 {
1775  value->value.enumerated.item[0] = kcontrol->private_value;
1776  return 0;
1777 }
1778 
1779 static int roland_load_put(struct snd_kcontrol *kcontrol,
1780  struct snd_ctl_elem_value *value)
1781 {
1782  struct snd_usb_midi* umidi = kcontrol->private_data;
1783  int changed;
1784 
1785  if (value->value.enumerated.item[0] > 1)
1786  return -EINVAL;
1787  mutex_lock(&umidi->mutex);
1788  changed = value->value.enumerated.item[0] != kcontrol->private_value;
1789  if (changed)
1790  kcontrol->private_value = value->value.enumerated.item[0];
1791  mutex_unlock(&umidi->mutex);
1792  return changed;
1793 }
1794 
1795 static struct snd_kcontrol_new roland_load_ctl = {
1796  .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1797  .name = "MIDI Input Mode",
1798  .info = roland_load_info,
1799  .get = roland_load_get,
1800  .put = roland_load_put,
1801  .private_value = 1,
1802 };
1803 
1804 /*
1805  * On Roland devices, use the second alternate setting to be able to use
1806  * the interrupt input endpoint.
1807  */
1808 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1809 {
1810  struct usb_interface* intf;
1811  struct usb_host_interface *hostif;
1812  struct usb_interface_descriptor* intfd;
1813 
1814  intf = umidi->iface;
1815  if (!intf || intf->num_altsetting != 2)
1816  return;
1817 
1818  hostif = &intf->altsetting[1];
1819  intfd = get_iface_desc(hostif);
1820  if (intfd->bNumEndpoints != 2 ||
1822  (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1823  return;
1824 
1825  snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1826  intfd->bAlternateSetting);
1827  usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1828  intfd->bAlternateSetting);
1829 
1830  umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
1831  if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
1832  umidi->roland_load_ctl = NULL;
1833 }
1834 
1835 /*
1836  * Try to find any usable endpoints in the interface.
1837  */
1838 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1840  int max_endpoints)
1841 {
1842  struct usb_interface* intf;
1843  struct usb_host_interface *hostif;
1844  struct usb_interface_descriptor* intfd;
1845  struct usb_endpoint_descriptor* epd;
1846  int i, out_eps = 0, in_eps = 0;
1847 
1848  if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
1849  snd_usbmidi_switch_roland_altsetting(umidi);
1850 
1851  if (endpoint[0].out_ep || endpoint[0].in_ep)
1852  return 0;
1853 
1854  intf = umidi->iface;
1855  if (!intf || intf->num_altsetting < 1)
1856  return -ENOENT;
1857  hostif = intf->cur_altsetting;
1858  intfd = get_iface_desc(hostif);
1859 
1860  for (i = 0; i < intfd->bNumEndpoints; ++i) {
1861  epd = get_endpoint(hostif, i);
1862  if (!usb_endpoint_xfer_bulk(epd) &&
1863  !usb_endpoint_xfer_int(epd))
1864  continue;
1865  if (out_eps < max_endpoints &&
1866  usb_endpoint_dir_out(epd)) {
1867  endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1868  if (usb_endpoint_xfer_int(epd))
1869  endpoint[out_eps].out_interval = epd->bInterval;
1870  ++out_eps;
1871  }
1872  if (in_eps < max_endpoints &&
1873  usb_endpoint_dir_in(epd)) {
1874  endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1875  if (usb_endpoint_xfer_int(epd))
1876  endpoint[in_eps].in_interval = epd->bInterval;
1877  ++in_eps;
1878  }
1879  }
1880  return (out_eps || in_eps) ? 0 : -ENOENT;
1881 }
1882 
1883 /*
1884  * Detects the endpoints for one-port-per-endpoint protocols.
1885  */
1886 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1887  struct snd_usb_midi_endpoint_info* endpoints)
1888 {
1889  int err, i;
1890 
1891  err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1892  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1893  if (endpoints[i].out_ep)
1894  endpoints[i].out_cables = 0x0001;
1895  if (endpoints[i].in_ep)
1896  endpoints[i].in_cables = 0x0001;
1897  }
1898  return err;
1899 }
1900 
1901 /*
1902  * Detects the endpoints and ports of Yamaha devices.
1903  */
1904 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1905  struct snd_usb_midi_endpoint_info* endpoint)
1906 {
1907  struct usb_interface* intf;
1908  struct usb_host_interface *hostif;
1909  struct usb_interface_descriptor* intfd;
1910  uint8_t* cs_desc;
1911 
1912  intf = umidi->iface;
1913  if (!intf)
1914  return -ENOENT;
1915  hostif = intf->altsetting;
1916  intfd = get_iface_desc(hostif);
1917  if (intfd->bNumEndpoints < 1)
1918  return -ENOENT;
1919 
1920  /*
1921  * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1922  * necessarily with any useful contents. So simply count 'em.
1923  */
1924  for (cs_desc = hostif->extra;
1925  cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1926  cs_desc += cs_desc[0]) {
1927  if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1928  if (cs_desc[2] == UAC_MIDI_IN_JACK)
1929  endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1930  else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
1931  endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1932  }
1933  }
1934  if (!endpoint->in_cables && !endpoint->out_cables)
1935  return -ENOENT;
1936 
1937  return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1938 }
1939 
1940 /*
1941  * Creates the endpoints and their ports for Midiman devices.
1942  */
1943 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1944  struct snd_usb_midi_endpoint_info* endpoint)
1945 {
1946  struct snd_usb_midi_endpoint_info ep_info;
1947  struct usb_interface* intf;
1948  struct usb_host_interface *hostif;
1949  struct usb_interface_descriptor* intfd;
1950  struct usb_endpoint_descriptor* epd;
1951  int cable, err;
1952 
1953  intf = umidi->iface;
1954  if (!intf)
1955  return -ENOENT;
1956  hostif = intf->altsetting;
1957  intfd = get_iface_desc(hostif);
1958  /*
1959  * The various MidiSport devices have more or less random endpoint
1960  * numbers, so we have to identify the endpoints by their index in
1961  * the descriptor array, like the driver for that other OS does.
1962  *
1963  * There is one interrupt input endpoint for all input ports, one
1964  * bulk output endpoint for even-numbered ports, and one for odd-
1965  * numbered ports. Both bulk output endpoints have corresponding
1966  * input bulk endpoints (at indices 1 and 3) which aren't used.
1967  */
1968  if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1969  snd_printdd(KERN_ERR "not enough endpoints\n");
1970  return -ENOENT;
1971  }
1972 
1973  epd = get_endpoint(hostif, 0);
1974  if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
1975  snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1976  return -ENXIO;
1977  }
1978  epd = get_endpoint(hostif, 2);
1979  if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
1980  snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1981  return -ENXIO;
1982  }
1983  if (endpoint->out_cables > 0x0001) {
1984  epd = get_endpoint(hostif, 4);
1985  if (!usb_endpoint_dir_out(epd) ||
1986  !usb_endpoint_xfer_bulk(epd)) {
1987  snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1988  return -ENXIO;
1989  }
1990  }
1991 
1992  ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1993  ep_info.out_interval = 0;
1994  ep_info.out_cables = endpoint->out_cables & 0x5555;
1995  err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1996  if (err < 0)
1997  return err;
1998 
1999  ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2000  ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
2001  ep_info.in_cables = endpoint->in_cables;
2002  err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
2003  if (err < 0)
2004  return err;
2005 
2006  if (endpoint->out_cables > 0x0001) {
2007  ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2008  ep_info.out_cables = endpoint->out_cables & 0xaaaa;
2009  err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
2010  if (err < 0)
2011  return err;
2012  }
2013 
2014  for (cable = 0; cable < 0x10; ++cable) {
2015  if (endpoint->out_cables & (1 << cable))
2016  snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
2017  &umidi->endpoints[cable & 1].out->ports[cable].substream);
2018  if (endpoint->in_cables & (1 << cable))
2019  snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
2020  &umidi->endpoints[0].in->ports[cable].substream);
2021  }
2022  return 0;
2023 }
2024 
2025 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
2026  .get_port_info = snd_usbmidi_get_port_info,
2027 };
2028 
2029 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
2030  int out_ports, int in_ports)
2031 {
2032  struct snd_rawmidi *rmidi;
2033  int err;
2034 
2035  err = snd_rawmidi_new(umidi->card, "USB MIDI",
2036  umidi->next_midi_device++,
2037  out_ports, in_ports, &rmidi);
2038  if (err < 0)
2039  return err;
2040  strcpy(rmidi->name, umidi->card->shortname);
2044  rmidi->ops = &snd_usbmidi_ops;
2045  rmidi->private_data = umidi;
2046  rmidi->private_free = snd_usbmidi_rawmidi_free;
2047  snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
2048  snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
2049 
2050  umidi->rmidi = rmidi;
2051  return 0;
2052 }
2053 
2054 /*
2055  * Temporarily stop input.
2056  */
2058 {
2059  struct snd_usb_midi* umidi;
2060  unsigned int i, j;
2061 
2062  umidi = list_entry(p, struct snd_usb_midi, list);
2063  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2064  struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
2065  if (ep->in)
2066  for (j = 0; j < INPUT_URBS; ++j)
2067  usb_kill_urb(ep->in->urbs[j]);
2068  }
2069 }
2070 
2071 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
2072 {
2073  unsigned int i;
2074 
2075  if (!ep)
2076  return;
2077  for (i = 0; i < INPUT_URBS; ++i) {
2078  struct urb* urb = ep->urbs[i];
2079  urb->dev = ep->umidi->dev;
2080  snd_usbmidi_submit_urb(urb, GFP_KERNEL);
2081  }
2082 }
2083 
2084 /*
2085  * Resume input after a call to snd_usbmidi_input_stop().
2086  */
2088 {
2089  struct snd_usb_midi* umidi;
2090  int i;
2091 
2092  umidi = list_entry(p, struct snd_usb_midi, list);
2093  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2094  snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2095 }
2096 
2097 /*
2098  * Creates and registers everything needed for a MIDI streaming interface.
2099  */
2101  struct usb_interface* iface,
2102  struct list_head *midi_list,
2103  const struct snd_usb_audio_quirk* quirk)
2104 {
2105  struct snd_usb_midi* umidi;
2106  struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
2107  int out_ports, in_ports;
2108  int i, err;
2109 
2110  umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
2111  if (!umidi)
2112  return -ENOMEM;
2113  umidi->dev = interface_to_usbdev(iface);
2114  umidi->card = card;
2115  umidi->iface = iface;
2116  umidi->quirk = quirk;
2117  umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
2118  init_timer(&umidi->error_timer);
2119  spin_lock_init(&umidi->disc_lock);
2120  mutex_init(&umidi->mutex);
2121  umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2122  le16_to_cpu(umidi->dev->descriptor.idProduct));
2123  umidi->error_timer.function = snd_usbmidi_error_timer;
2124  umidi->error_timer.data = (unsigned long)umidi;
2125 
2126  /* detect the endpoint(s) to use */
2127  memset(endpoints, 0, sizeof(endpoints));
2128  switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2129  case QUIRK_MIDI_STANDARD_INTERFACE:
2130  err = snd_usbmidi_get_ms_info(umidi, endpoints);
2131  if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
2132  umidi->usb_protocol_ops =
2133  &snd_usbmidi_maudio_broken_running_status_ops;
2134  break;
2135  case QUIRK_MIDI_US122L:
2136  umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2137  /* fall through */
2139  memcpy(&endpoints[0], quirk->data,
2140  sizeof(struct snd_usb_midi_endpoint_info));
2141  err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2142  break;
2143  case QUIRK_MIDI_YAMAHA:
2144  err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2145  break;
2146  case QUIRK_MIDI_MIDIMAN:
2147  umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2148  memcpy(&endpoints[0], quirk->data,
2149  sizeof(struct snd_usb_midi_endpoint_info));
2150  err = 0;
2151  break;
2152  case QUIRK_MIDI_NOVATION:
2153  umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2154  err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2155  break;
2156  case QUIRK_MIDI_RAW_BYTES:
2157  umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
2158  /*
2159  * Interface 1 contains isochronous endpoints, but with the same
2160  * numbers as in interface 0. Since it is interface 1 that the
2161  * USB core has most recently seen, these descriptors are now
2162  * associated with the endpoint numbers. This will foul up our
2163  * attempts to submit bulk/interrupt URBs to the endpoints in
2164  * interface 0, so we have to make sure that the USB core looks
2165  * again at interface 0 by calling usb_set_interface() on it.
2166  */
2167  if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2168  usb_set_interface(umidi->dev, 0, 0);
2169  err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2170  break;
2171  case QUIRK_MIDI_EMAGIC:
2172  umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2173  memcpy(&endpoints[0], quirk->data,
2174  sizeof(struct snd_usb_midi_endpoint_info));
2175  err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2176  break;
2177  case QUIRK_MIDI_CME:
2178  umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
2179  err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2180  break;
2181  case QUIRK_MIDI_AKAI:
2182  umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2183  err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2184  /* endpoint 1 is input-only */
2185  endpoints[1].out_cables = 0;
2186  break;
2187  case QUIRK_MIDI_FTDI:
2188  umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2189 
2190  /* set baud rate to 31250 (48 MHz / 16 / 96) */
2191  err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2192  3, 0x40, 0x60, 0, NULL, 0, 1000);
2193  if (err < 0)
2194  break;
2195 
2196  err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2197  break;
2198  default:
2199  snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2200  err = -ENXIO;
2201  break;
2202  }
2203  if (err < 0) {
2204  kfree(umidi);
2205  return err;
2206  }
2207 
2208  /* create rawmidi device */
2209  out_ports = 0;
2210  in_ports = 0;
2211  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2212  out_ports += hweight16(endpoints[i].out_cables);
2213  in_ports += hweight16(endpoints[i].in_cables);
2214  }
2215  err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2216  if (err < 0) {
2217  kfree(umidi);
2218  return err;
2219  }
2220 
2221  /* create endpoint/port structures */
2222  if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2223  err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2224  else
2225  err = snd_usbmidi_create_endpoints(umidi, endpoints);
2226  if (err < 0) {
2227  snd_usbmidi_free(umidi);
2228  return err;
2229  }
2230 
2231  list_add_tail(&umidi->list, midi_list);
2232 
2233  for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2234  snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2235  return 0;
2236 }
2237