usbmidi.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * usbmidi.c - ALSA USB MIDI driver
  3. *
  4. * Copyright (c) 2002-2005 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. #include <sound/driver.h>
  38. #include <linux/kernel.h>
  39. #include <linux/types.h>
  40. #include <linux/bitops.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/string.h>
  44. #include <linux/init.h>
  45. #include <linux/slab.h>
  46. #include <linux/timer.h>
  47. #include <linux/usb.h>
  48. #include <sound/core.h>
  49. #include <sound/rawmidi.h>
  50. #include "usbaudio.h"
  51. /*
  52. * define this to log all USB packets
  53. */
  54. /* #define DUMP_PACKETS */
  55. /*
  56. * how long to wait after some USB errors, so that khubd can disconnect() us
  57. * without too many spurious errors
  58. */
  59. #define ERROR_DELAY_JIFFIES (HZ / 10)
  60. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  61. MODULE_DESCRIPTION("USB Audio/MIDI helper module");
  62. MODULE_LICENSE("Dual BSD/GPL");
  63. struct usb_ms_header_descriptor {
  64. __u8 bLength;
  65. __u8 bDescriptorType;
  66. __u8 bDescriptorSubtype;
  67. __u8 bcdMSC[2];
  68. __le16 wTotalLength;
  69. } __attribute__ ((packed));
  70. struct usb_ms_endpoint_descriptor {
  71. __u8 bLength;
  72. __u8 bDescriptorType;
  73. __u8 bDescriptorSubtype;
  74. __u8 bNumEmbMIDIJack;
  75. __u8 baAssocJackID[0];
  76. } __attribute__ ((packed));
  77. typedef struct snd_usb_midi snd_usb_midi_t;
  78. typedef struct snd_usb_midi_endpoint snd_usb_midi_endpoint_t;
  79. typedef struct snd_usb_midi_out_endpoint snd_usb_midi_out_endpoint_t;
  80. typedef struct snd_usb_midi_in_endpoint snd_usb_midi_in_endpoint_t;
  81. typedef struct usbmidi_out_port usbmidi_out_port_t;
  82. typedef struct usbmidi_in_port usbmidi_in_port_t;
  83. struct usb_protocol_ops {
  84. void (*input)(snd_usb_midi_in_endpoint_t*, uint8_t*, int);
  85. void (*output)(snd_usb_midi_out_endpoint_t*);
  86. void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
  87. void (*init_out_endpoint)(snd_usb_midi_out_endpoint_t*);
  88. void (*finish_out_endpoint)(snd_usb_midi_out_endpoint_t*);
  89. };
  90. struct snd_usb_midi {
  91. snd_usb_audio_t *chip;
  92. struct usb_interface *iface;
  93. const snd_usb_audio_quirk_t *quirk;
  94. snd_rawmidi_t* rmidi;
  95. struct usb_protocol_ops* usb_protocol_ops;
  96. struct list_head list;
  97. struct timer_list error_timer;
  98. struct snd_usb_midi_endpoint {
  99. snd_usb_midi_out_endpoint_t *out;
  100. snd_usb_midi_in_endpoint_t *in;
  101. } endpoints[MIDI_MAX_ENDPOINTS];
  102. unsigned long input_triggered;
  103. };
  104. struct snd_usb_midi_out_endpoint {
  105. snd_usb_midi_t* umidi;
  106. struct urb* urb;
  107. int urb_active;
  108. int max_transfer; /* size of urb buffer */
  109. struct tasklet_struct tasklet;
  110. spinlock_t buffer_lock;
  111. struct usbmidi_out_port {
  112. snd_usb_midi_out_endpoint_t* ep;
  113. snd_rawmidi_substream_t* substream;
  114. int active;
  115. uint8_t cable; /* cable number << 4 */
  116. uint8_t state;
  117. #define STATE_UNKNOWN 0
  118. #define STATE_1PARAM 1
  119. #define STATE_2PARAM_1 2
  120. #define STATE_2PARAM_2 3
  121. #define STATE_SYSEX_0 4
  122. #define STATE_SYSEX_1 5
  123. #define STATE_SYSEX_2 6
  124. uint8_t data[2];
  125. } ports[0x10];
  126. int current_port;
  127. };
  128. struct snd_usb_midi_in_endpoint {
  129. snd_usb_midi_t* umidi;
  130. struct urb* urb;
  131. struct usbmidi_in_port {
  132. snd_rawmidi_substream_t* substream;
  133. } ports[0x10];
  134. u8 seen_f5;
  135. u8 error_resubmit;
  136. int current_port;
  137. };
  138. static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep);
  139. static const uint8_t snd_usbmidi_cin_length[] = {
  140. 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
  141. };
  142. /*
  143. * Submits the URB, with error handling.
  144. */
  145. static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
  146. {
  147. int err = usb_submit_urb(urb, flags);
  148. if (err < 0 && err != -ENODEV)
  149. snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
  150. return err;
  151. }
  152. /*
  153. * Error handling for URB completion functions.
  154. */
  155. static int snd_usbmidi_urb_error(int status)
  156. {
  157. switch (status) {
  158. /* manually unlinked, or device gone */
  159. case -ENOENT:
  160. case -ECONNRESET:
  161. case -ESHUTDOWN:
  162. case -ENODEV:
  163. return -ENODEV;
  164. /* errors that might occur during unplugging */
  165. case -EPROTO: /* EHCI */
  166. case -ETIMEDOUT: /* OHCI */
  167. case -EILSEQ: /* UHCI */
  168. return -EIO;
  169. default:
  170. snd_printk(KERN_ERR "urb status %d\n", status);
  171. return 0; /* continue */
  172. }
  173. }
  174. /*
  175. * Receives a chunk of MIDI data.
  176. */
  177. static void snd_usbmidi_input_data(snd_usb_midi_in_endpoint_t* ep, int portidx,
  178. uint8_t* data, int length)
  179. {
  180. usbmidi_in_port_t* port = &ep->ports[portidx];
  181. if (!port->substream) {
  182. snd_printd("unexpected port %d!\n", portidx);
  183. return;
  184. }
  185. if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
  186. return;
  187. snd_rawmidi_receive(port->substream, data, length);
  188. }
  189. #ifdef DUMP_PACKETS
  190. static void dump_urb(const char *type, const u8 *data, int length)
  191. {
  192. snd_printk(KERN_DEBUG "%s packet: [", type);
  193. for (; length > 0; ++data, --length)
  194. printk(" %02x", *data);
  195. printk(" ]\n");
  196. }
  197. #else
  198. #define dump_urb(type, data, length) /* nothing */
  199. #endif
  200. /*
  201. * Processes the data read from the device.
  202. */
  203. static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs)
  204. {
  205. snd_usb_midi_in_endpoint_t* ep = urb->context;
  206. if (urb->status == 0) {
  207. dump_urb("received", urb->transfer_buffer, urb->actual_length);
  208. ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
  209. urb->actual_length);
  210. } else {
  211. int err = snd_usbmidi_urb_error(urb->status);
  212. if (err < 0) {
  213. if (err != -ENODEV) {
  214. ep->error_resubmit = 1;
  215. mod_timer(&ep->umidi->error_timer,
  216. jiffies + ERROR_DELAY_JIFFIES);
  217. }
  218. return;
  219. }
  220. }
  221. urb->dev = ep->umidi->chip->dev;
  222. snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
  223. }
  224. static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs)
  225. {
  226. snd_usb_midi_out_endpoint_t* ep = urb->context;
  227. spin_lock(&ep->buffer_lock);
  228. ep->urb_active = 0;
  229. spin_unlock(&ep->buffer_lock);
  230. if (urb->status < 0) {
  231. int err = snd_usbmidi_urb_error(urb->status);
  232. if (err < 0) {
  233. if (err != -ENODEV)
  234. mod_timer(&ep->umidi->error_timer,
  235. jiffies + ERROR_DELAY_JIFFIES);
  236. return;
  237. }
  238. }
  239. snd_usbmidi_do_output(ep);
  240. }
  241. /*
  242. * This is called when some data should be transferred to the device
  243. * (from one or more substreams).
  244. */
  245. static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep)
  246. {
  247. struct urb* urb = ep->urb;
  248. unsigned long flags;
  249. spin_lock_irqsave(&ep->buffer_lock, flags);
  250. if (ep->urb_active || ep->umidi->chip->shutdown) {
  251. spin_unlock_irqrestore(&ep->buffer_lock, flags);
  252. return;
  253. }
  254. urb->transfer_buffer_length = 0;
  255. ep->umidi->usb_protocol_ops->output(ep);
  256. if (urb->transfer_buffer_length > 0) {
  257. dump_urb("sending", urb->transfer_buffer,
  258. urb->transfer_buffer_length);
  259. urb->dev = ep->umidi->chip->dev;
  260. ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
  261. }
  262. spin_unlock_irqrestore(&ep->buffer_lock, flags);
  263. }
  264. static void snd_usbmidi_out_tasklet(unsigned long data)
  265. {
  266. snd_usb_midi_out_endpoint_t* ep = (snd_usb_midi_out_endpoint_t *) data;
  267. snd_usbmidi_do_output(ep);
  268. }
  269. /* called after transfers had been interrupted due to some USB error */
  270. static void snd_usbmidi_error_timer(unsigned long data)
  271. {
  272. snd_usb_midi_t *umidi = (snd_usb_midi_t *)data;
  273. int i;
  274. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  275. snd_usb_midi_in_endpoint_t *in = umidi->endpoints[i].in;
  276. if (in && in->error_resubmit) {
  277. in->error_resubmit = 0;
  278. in->urb->dev = umidi->chip->dev;
  279. snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
  280. }
  281. if (umidi->endpoints[i].out)
  282. snd_usbmidi_do_output(umidi->endpoints[i].out);
  283. }
  284. }
  285. /* helper function to send static data that may not DMA-able */
  286. static int send_bulk_static_data(snd_usb_midi_out_endpoint_t* ep,
  287. const void *data, int len)
  288. {
  289. int err;
  290. void *buf = kmalloc(len, GFP_KERNEL);
  291. if (!buf)
  292. return -ENOMEM;
  293. memcpy(buf, data, len);
  294. dump_urb("sending", buf, len);
  295. err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len,
  296. NULL, 250);
  297. kfree(buf);
  298. return err;
  299. }
  300. /*
  301. * Standard USB MIDI protocol: see the spec.
  302. * Midiman protocol: like the standard protocol, but the control byte is the
  303. * fourth byte in each packet, and uses length instead of CIN.
  304. */
  305. static void snd_usbmidi_standard_input(snd_usb_midi_in_endpoint_t* ep,
  306. uint8_t* buffer, int buffer_length)
  307. {
  308. int i;
  309. for (i = 0; i + 3 < buffer_length; i += 4)
  310. if (buffer[i] != 0) {
  311. int cable = buffer[i] >> 4;
  312. int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
  313. snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
  314. }
  315. }
  316. static void snd_usbmidi_midiman_input(snd_usb_midi_in_endpoint_t* ep,
  317. uint8_t* buffer, int buffer_length)
  318. {
  319. int i;
  320. for (i = 0; i + 3 < buffer_length; i += 4)
  321. if (buffer[i + 3] != 0) {
  322. int port = buffer[i + 3] >> 4;
  323. int length = buffer[i + 3] & 3;
  324. snd_usbmidi_input_data(ep, port, &buffer[i], length);
  325. }
  326. }
  327. /*
  328. * Adds one USB MIDI packet to the output buffer.
  329. */
  330. static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
  331. uint8_t p1, uint8_t p2, uint8_t p3)
  332. {
  333. uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
  334. buf[0] = p0;
  335. buf[1] = p1;
  336. buf[2] = p2;
  337. buf[3] = p3;
  338. urb->transfer_buffer_length += 4;
  339. }
  340. /*
  341. * Adds one Midiman packet to the output buffer.
  342. */
  343. static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
  344. uint8_t p1, uint8_t p2, uint8_t p3)
  345. {
  346. uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
  347. buf[0] = p1;
  348. buf[1] = p2;
  349. buf[2] = p3;
  350. buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
  351. urb->transfer_buffer_length += 4;
  352. }
  353. /*
  354. * Converts MIDI commands to USB MIDI packets.
  355. */
  356. static void snd_usbmidi_transmit_byte(usbmidi_out_port_t* port,
  357. uint8_t b, struct urb* urb)
  358. {
  359. uint8_t p0 = port->cable;
  360. void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
  361. port->ep->umidi->usb_protocol_ops->output_packet;
  362. if (b >= 0xf8) {
  363. output_packet(urb, p0 | 0x0f, b, 0, 0);
  364. } else if (b >= 0xf0) {
  365. switch (b) {
  366. case 0xf0:
  367. port->data[0] = b;
  368. port->state = STATE_SYSEX_1;
  369. break;
  370. case 0xf1:
  371. case 0xf3:
  372. port->data[0] = b;
  373. port->state = STATE_1PARAM;
  374. break;
  375. case 0xf2:
  376. port->data[0] = b;
  377. port->state = STATE_2PARAM_1;
  378. break;
  379. case 0xf4:
  380. case 0xf5:
  381. port->state = STATE_UNKNOWN;
  382. break;
  383. case 0xf6:
  384. output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
  385. port->state = STATE_UNKNOWN;
  386. break;
  387. case 0xf7:
  388. switch (port->state) {
  389. case STATE_SYSEX_0:
  390. output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
  391. break;
  392. case STATE_SYSEX_1:
  393. output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
  394. break;
  395. case STATE_SYSEX_2:
  396. output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
  397. break;
  398. }
  399. port->state = STATE_UNKNOWN;
  400. break;
  401. }
  402. } else if (b >= 0x80) {
  403. port->data[0] = b;
  404. if (b >= 0xc0 && b <= 0xdf)
  405. port->state = STATE_1PARAM;
  406. else
  407. port->state = STATE_2PARAM_1;
  408. } else { /* b < 0x80 */
  409. switch (port->state) {
  410. case STATE_1PARAM:
  411. if (port->data[0] < 0xf0) {
  412. p0 |= port->data[0] >> 4;
  413. } else {
  414. p0 |= 0x02;
  415. port->state = STATE_UNKNOWN;
  416. }
  417. output_packet(urb, p0, port->data[0], b, 0);
  418. break;
  419. case STATE_2PARAM_1:
  420. port->data[1] = b;
  421. port->state = STATE_2PARAM_2;
  422. break;
  423. case STATE_2PARAM_2:
  424. if (port->data[0] < 0xf0) {
  425. p0 |= port->data[0] >> 4;
  426. port->state = STATE_2PARAM_1;
  427. } else {
  428. p0 |= 0x03;
  429. port->state = STATE_UNKNOWN;
  430. }
  431. output_packet(urb, p0, port->data[0], port->data[1], b);
  432. break;
  433. case STATE_SYSEX_0:
  434. port->data[0] = b;
  435. port->state = STATE_SYSEX_1;
  436. break;
  437. case STATE_SYSEX_1:
  438. port->data[1] = b;
  439. port->state = STATE_SYSEX_2;
  440. break;
  441. case STATE_SYSEX_2:
  442. output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
  443. port->state = STATE_SYSEX_0;
  444. break;
  445. }
  446. }
  447. }
  448. static void snd_usbmidi_standard_output(snd_usb_midi_out_endpoint_t* ep)
  449. {
  450. struct urb* urb = ep->urb;
  451. int p;
  452. /* FIXME: lower-numbered ports can starve higher-numbered ports */
  453. for (p = 0; p < 0x10; ++p) {
  454. usbmidi_out_port_t* port = &ep->ports[p];
  455. if (!port->active)
  456. continue;
  457. while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
  458. uint8_t b;
  459. if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
  460. port->active = 0;
  461. break;
  462. }
  463. snd_usbmidi_transmit_byte(port, b, urb);
  464. }
  465. }
  466. }
  467. static struct usb_protocol_ops snd_usbmidi_standard_ops = {
  468. .input = snd_usbmidi_standard_input,
  469. .output = snd_usbmidi_standard_output,
  470. .output_packet = snd_usbmidi_output_standard_packet,
  471. };
  472. static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
  473. .input = snd_usbmidi_midiman_input,
  474. .output = snd_usbmidi_standard_output,
  475. .output_packet = snd_usbmidi_output_midiman_packet,
  476. };
  477. /*
  478. * Novation USB MIDI protocol: number of data bytes is in the first byte
  479. * (when receiving) (+1!) or in the second byte (when sending); data begins
  480. * at the third byte.
  481. */
  482. static void snd_usbmidi_novation_input(snd_usb_midi_in_endpoint_t* ep,
  483. uint8_t* buffer, int buffer_length)
  484. {
  485. if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
  486. return;
  487. snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
  488. }
  489. static void snd_usbmidi_novation_output(snd_usb_midi_out_endpoint_t* ep)
  490. {
  491. uint8_t* transfer_buffer;
  492. int count;
  493. if (!ep->ports[0].active)
  494. return;
  495. transfer_buffer = ep->urb->transfer_buffer;
  496. count = snd_rawmidi_transmit(ep->ports[0].substream,
  497. &transfer_buffer[2],
  498. ep->max_transfer - 2);
  499. if (count < 1) {
  500. ep->ports[0].active = 0;
  501. return;
  502. }
  503. transfer_buffer[0] = 0;
  504. transfer_buffer[1] = count;
  505. ep->urb->transfer_buffer_length = 2 + count;
  506. }
  507. static struct usb_protocol_ops snd_usbmidi_novation_ops = {
  508. .input = snd_usbmidi_novation_input,
  509. .output = snd_usbmidi_novation_output,
  510. };
  511. /*
  512. * "raw" protocol: used by the MOTU FastLane.
  513. */
  514. static void snd_usbmidi_raw_input(snd_usb_midi_in_endpoint_t* ep,
  515. uint8_t* buffer, int buffer_length)
  516. {
  517. snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
  518. }
  519. static void snd_usbmidi_raw_output(snd_usb_midi_out_endpoint_t* ep)
  520. {
  521. int count;
  522. if (!ep->ports[0].active)
  523. return;
  524. count = snd_rawmidi_transmit(ep->ports[0].substream,
  525. ep->urb->transfer_buffer,
  526. ep->max_transfer);
  527. if (count < 1) {
  528. ep->ports[0].active = 0;
  529. return;
  530. }
  531. ep->urb->transfer_buffer_length = count;
  532. }
  533. static struct usb_protocol_ops snd_usbmidi_raw_ops = {
  534. .input = snd_usbmidi_raw_input,
  535. .output = snd_usbmidi_raw_output,
  536. };
  537. /*
  538. * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
  539. */
  540. static void snd_usbmidi_emagic_init_out(snd_usb_midi_out_endpoint_t* ep)
  541. {
  542. static const u8 init_data[] = {
  543. /* initialization magic: "get version" */
  544. 0xf0,
  545. 0x00, 0x20, 0x31, /* Emagic */
  546. 0x64, /* Unitor8 */
  547. 0x0b, /* version number request */
  548. 0x00, /* command version */
  549. 0x00, /* EEPROM, box 0 */
  550. 0xf7
  551. };
  552. send_bulk_static_data(ep, init_data, sizeof(init_data));
  553. /* while we're at it, pour on more magic */
  554. send_bulk_static_data(ep, init_data, sizeof(init_data));
  555. }
  556. static void snd_usbmidi_emagic_finish_out(snd_usb_midi_out_endpoint_t* ep)
  557. {
  558. static const u8 finish_data[] = {
  559. /* switch to patch mode with last preset */
  560. 0xf0,
  561. 0x00, 0x20, 0x31, /* Emagic */
  562. 0x64, /* Unitor8 */
  563. 0x10, /* patch switch command */
  564. 0x00, /* command version */
  565. 0x7f, /* to all boxes */
  566. 0x40, /* last preset in EEPROM */
  567. 0xf7
  568. };
  569. send_bulk_static_data(ep, finish_data, sizeof(finish_data));
  570. }
  571. static void snd_usbmidi_emagic_input(snd_usb_midi_in_endpoint_t* ep,
  572. uint8_t* buffer, int buffer_length)
  573. {
  574. int i;
  575. /* FF indicates end of valid data */
  576. for (i = 0; i < buffer_length; ++i)
  577. if (buffer[i] == 0xff) {
  578. buffer_length = i;
  579. break;
  580. }
  581. /* handle F5 at end of last buffer */
  582. if (ep->seen_f5)
  583. goto switch_port;
  584. while (buffer_length > 0) {
  585. /* determine size of data until next F5 */
  586. for (i = 0; i < buffer_length; ++i)
  587. if (buffer[i] == 0xf5)
  588. break;
  589. snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
  590. buffer += i;
  591. buffer_length -= i;
  592. if (buffer_length <= 0)
  593. break;
  594. /* assert(buffer[0] == 0xf5); */
  595. ep->seen_f5 = 1;
  596. ++buffer;
  597. --buffer_length;
  598. switch_port:
  599. if (buffer_length <= 0)
  600. break;
  601. if (buffer[0] < 0x80) {
  602. ep->current_port = (buffer[0] - 1) & 15;
  603. ++buffer;
  604. --buffer_length;
  605. }
  606. ep->seen_f5 = 0;
  607. }
  608. }
  609. static void snd_usbmidi_emagic_output(snd_usb_midi_out_endpoint_t* ep)
  610. {
  611. int port0 = ep->current_port;
  612. uint8_t* buf = ep->urb->transfer_buffer;
  613. int buf_free = ep->max_transfer;
  614. int length, i;
  615. for (i = 0; i < 0x10; ++i) {
  616. /* round-robin, starting at the last current port */
  617. int portnum = (port0 + i) & 15;
  618. usbmidi_out_port_t* port = &ep->ports[portnum];
  619. if (!port->active)
  620. continue;
  621. if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
  622. port->active = 0;
  623. continue;
  624. }
  625. if (portnum != ep->current_port) {
  626. if (buf_free < 2)
  627. break;
  628. ep->current_port = portnum;
  629. buf[0] = 0xf5;
  630. buf[1] = (portnum + 1) & 15;
  631. buf += 2;
  632. buf_free -= 2;
  633. }
  634. if (buf_free < 1)
  635. break;
  636. length = snd_rawmidi_transmit(port->substream, buf, buf_free);
  637. if (length > 0) {
  638. buf += length;
  639. buf_free -= length;
  640. if (buf_free < 1)
  641. break;
  642. }
  643. }
  644. if (buf_free < ep->max_transfer && buf_free > 0) {
  645. *buf = 0xff;
  646. --buf_free;
  647. }
  648. ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
  649. }
  650. static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
  651. .input = snd_usbmidi_emagic_input,
  652. .output = snd_usbmidi_emagic_output,
  653. .init_out_endpoint = snd_usbmidi_emagic_init_out,
  654. .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
  655. };
  656. static int snd_usbmidi_output_open(snd_rawmidi_substream_t* substream)
  657. {
  658. snd_usb_midi_t* umidi = substream->rmidi->private_data;
  659. usbmidi_out_port_t* port = NULL;
  660. int i, j;
  661. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
  662. if (umidi->endpoints[i].out)
  663. for (j = 0; j < 0x10; ++j)
  664. if (umidi->endpoints[i].out->ports[j].substream == substream) {
  665. port = &umidi->endpoints[i].out->ports[j];
  666. break;
  667. }
  668. if (!port) {
  669. snd_BUG();
  670. return -ENXIO;
  671. }
  672. substream->runtime->private_data = port;
  673. port->state = STATE_UNKNOWN;
  674. return 0;
  675. }
  676. static int snd_usbmidi_output_close(snd_rawmidi_substream_t* substream)
  677. {
  678. return 0;
  679. }
  680. static void snd_usbmidi_output_trigger(snd_rawmidi_substream_t* substream, int up)
  681. {
  682. usbmidi_out_port_t* port = (usbmidi_out_port_t*)substream->runtime->private_data;
  683. port->active = up;
  684. if (up) {
  685. if (port->ep->umidi->chip->shutdown) {
  686. /* gobble up remaining bytes to prevent wait in
  687. * snd_rawmidi_drain_output */
  688. while (!snd_rawmidi_transmit_empty(substream))
  689. snd_rawmidi_transmit_ack(substream, 1);
  690. return;
  691. }
  692. tasklet_hi_schedule(&port->ep->tasklet);
  693. }
  694. }
  695. static int snd_usbmidi_input_open(snd_rawmidi_substream_t* substream)
  696. {
  697. return 0;
  698. }
  699. static int snd_usbmidi_input_close(snd_rawmidi_substream_t* substream)
  700. {
  701. return 0;
  702. }
  703. static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t* substream, int up)
  704. {
  705. snd_usb_midi_t* umidi = substream->rmidi->private_data;
  706. if (up)
  707. set_bit(substream->number, &umidi->input_triggered);
  708. else
  709. clear_bit(substream->number, &umidi->input_triggered);
  710. }
  711. static snd_rawmidi_ops_t snd_usbmidi_output_ops = {
  712. .open = snd_usbmidi_output_open,
  713. .close = snd_usbmidi_output_close,
  714. .trigger = snd_usbmidi_output_trigger,
  715. };
  716. static snd_rawmidi_ops_t snd_usbmidi_input_ops = {
  717. .open = snd_usbmidi_input_open,
  718. .close = snd_usbmidi_input_close,
  719. .trigger = snd_usbmidi_input_trigger
  720. };
  721. /*
  722. * Frees an input endpoint.
  723. * May be called when ep hasn't been initialized completely.
  724. */
  725. static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t* ep)
  726. {
  727. if (ep->urb) {
  728. usb_buffer_free(ep->umidi->chip->dev,
  729. ep->urb->transfer_buffer_length,
  730. ep->urb->transfer_buffer,
  731. ep->urb->transfer_dma);
  732. usb_free_urb(ep->urb);
  733. }
  734. kfree(ep);
  735. }
  736. /*
  737. * Creates an input endpoint.
  738. */
  739. static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t* umidi,
  740. snd_usb_midi_endpoint_info_t* ep_info,
  741. snd_usb_midi_endpoint_t* rep)
  742. {
  743. snd_usb_midi_in_endpoint_t* ep;
  744. void* buffer;
  745. unsigned int pipe;
  746. int length;
  747. rep->in = NULL;
  748. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  749. if (!ep)
  750. return -ENOMEM;
  751. ep->umidi = umidi;
  752. ep->urb = usb_alloc_urb(0, GFP_KERNEL);
  753. if (!ep->urb) {
  754. snd_usbmidi_in_endpoint_delete(ep);
  755. return -ENOMEM;
  756. }
  757. if (ep_info->in_interval)
  758. pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
  759. else
  760. pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
  761. length = usb_maxpacket(umidi->chip->dev, pipe, 0);
  762. buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
  763. &ep->urb->transfer_dma);
  764. if (!buffer) {
  765. snd_usbmidi_in_endpoint_delete(ep);
  766. return -ENOMEM;
  767. }
  768. if (ep_info->in_interval)
  769. usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
  770. length, snd_usbmidi_in_urb_complete, ep,
  771. ep_info->in_interval);
  772. else
  773. usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
  774. length, snd_usbmidi_in_urb_complete, ep);
  775. ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  776. rep->in = ep;
  777. return 0;
  778. }
  779. static unsigned int snd_usbmidi_count_bits(unsigned int x)
  780. {
  781. unsigned int bits = 0;
  782. for (; x; x >>= 1)
  783. bits += x & 1;
  784. return bits;
  785. }
  786. /*
  787. * Frees an output endpoint.
  788. * May be called when ep hasn't been initialized completely.
  789. */
  790. static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t* ep)
  791. {
  792. if (ep->urb) {
  793. usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
  794. ep->urb->transfer_buffer,
  795. ep->urb->transfer_dma);
  796. usb_free_urb(ep->urb);
  797. }
  798. kfree(ep);
  799. }
  800. /*
  801. * Creates an output endpoint, and initializes output ports.
  802. */
  803. static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t* umidi,
  804. snd_usb_midi_endpoint_info_t* ep_info,
  805. snd_usb_midi_endpoint_t* rep)
  806. {
  807. snd_usb_midi_out_endpoint_t* ep;
  808. int i;
  809. unsigned int pipe;
  810. void* buffer;
  811. rep->out = NULL;
  812. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  813. if (!ep)
  814. return -ENOMEM;
  815. ep->umidi = umidi;
  816. ep->urb = usb_alloc_urb(0, GFP_KERNEL);
  817. if (!ep->urb) {
  818. snd_usbmidi_out_endpoint_delete(ep);
  819. return -ENOMEM;
  820. }
  821. /* we never use interrupt output pipes */
  822. pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
  823. ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
  824. buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
  825. GFP_KERNEL, &ep->urb->transfer_dma);
  826. if (!buffer) {
  827. snd_usbmidi_out_endpoint_delete(ep);
  828. return -ENOMEM;
  829. }
  830. usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
  831. ep->max_transfer, snd_usbmidi_out_urb_complete, ep);
  832. ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  833. spin_lock_init(&ep->buffer_lock);
  834. tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
  835. for (i = 0; i < 0x10; ++i)
  836. if (ep_info->out_cables & (1 << i)) {
  837. ep->ports[i].ep = ep;
  838. ep->ports[i].cable = i << 4;
  839. }
  840. if (umidi->usb_protocol_ops->init_out_endpoint)
  841. umidi->usb_protocol_ops->init_out_endpoint(ep);
  842. rep->out = ep;
  843. return 0;
  844. }
  845. /*
  846. * Frees everything.
  847. */
  848. static void snd_usbmidi_free(snd_usb_midi_t* umidi)
  849. {
  850. int i;
  851. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  852. snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
  853. if (ep->out)
  854. snd_usbmidi_out_endpoint_delete(ep->out);
  855. if (ep->in)
  856. snd_usbmidi_in_endpoint_delete(ep->in);
  857. }
  858. kfree(umidi);
  859. }
  860. /*
  861. * Unlinks all URBs (must be done before the usb_device is deleted).
  862. */
  863. void snd_usbmidi_disconnect(struct list_head* p)
  864. {
  865. snd_usb_midi_t* umidi;
  866. int i;
  867. umidi = list_entry(p, snd_usb_midi_t, list);
  868. del_timer_sync(&umidi->error_timer);
  869. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  870. snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
  871. if (ep->out)
  872. tasklet_kill(&ep->out->tasklet);
  873. if (ep->out && ep->out->urb) {
  874. usb_kill_urb(ep->out->urb);
  875. if (umidi->usb_protocol_ops->finish_out_endpoint)
  876. umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
  877. }
  878. if (ep->in && ep->in->urb)
  879. usb_kill_urb(ep->in->urb);
  880. }
  881. }
  882. static void snd_usbmidi_rawmidi_free(snd_rawmidi_t* rmidi)
  883. {
  884. snd_usb_midi_t* umidi = rmidi->private_data;
  885. snd_usbmidi_free(umidi);
  886. }
  887. static snd_rawmidi_substream_t* snd_usbmidi_find_substream(snd_usb_midi_t* umidi,
  888. int stream, int number)
  889. {
  890. struct list_head* list;
  891. list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
  892. snd_rawmidi_substream_t* substream = list_entry(list, snd_rawmidi_substream_t, list);
  893. if (substream->number == number)
  894. return substream;
  895. }
  896. return NULL;
  897. }
  898. /*
  899. * This list specifies names for ports that do not fit into the standard
  900. * "(product) MIDI (n)" schema because they aren't external MIDI ports,
  901. * such as internal control or synthesizer ports.
  902. */
  903. static struct {
  904. u32 id;
  905. int port;
  906. const char *name_format;
  907. } snd_usbmidi_port_names[] = {
  908. /* Roland UA-100 */
  909. { USB_ID(0x0582, 0x0000), 2, "%s Control" },
  910. /* Roland SC-8850 */
  911. { USB_ID(0x0582, 0x0003), 0, "%s Part A" },
  912. { USB_ID(0x0582, 0x0003), 1, "%s Part B" },
  913. { USB_ID(0x0582, 0x0003), 2, "%s Part C" },
  914. { USB_ID(0x0582, 0x0003), 3, "%s Part D" },
  915. { USB_ID(0x0582, 0x0003), 4, "%s MIDI 1" },
  916. { USB_ID(0x0582, 0x0003), 5, "%s MIDI 2" },
  917. /* Roland U-8 */
  918. { USB_ID(0x0582, 0x0004), 0, "%s MIDI" },
  919. { USB_ID(0x0582, 0x0004), 1, "%s Control" },
  920. /* Roland SC-8820 */
  921. { USB_ID(0x0582, 0x0007), 0, "%s Part A" },
  922. { USB_ID(0x0582, 0x0007), 1, "%s Part B" },
  923. { USB_ID(0x0582, 0x0007), 2, "%s MIDI" },
  924. /* Roland SK-500 */
  925. { USB_ID(0x0582, 0x000b), 0, "%s Part A" },
  926. { USB_ID(0x0582, 0x000b), 1, "%s Part B" },
  927. { USB_ID(0x0582, 0x000b), 2, "%s MIDI" },
  928. /* Roland SC-D70 */
  929. { USB_ID(0x0582, 0x000c), 0, "%s Part A" },
  930. { USB_ID(0x0582, 0x000c), 1, "%s Part B" },
  931. { USB_ID(0x0582, 0x000c), 2, "%s MIDI" },
  932. /* Edirol UM-880 */
  933. { USB_ID(0x0582, 0x0014), 8, "%s Control" },
  934. /* Edirol SD-90 */
  935. { USB_ID(0x0582, 0x0016), 0, "%s Part A" },
  936. { USB_ID(0x0582, 0x0016), 1, "%s Part B" },
  937. { USB_ID(0x0582, 0x0016), 2, "%s MIDI 1" },
  938. { USB_ID(0x0582, 0x0016), 3, "%s MIDI 2" },
  939. /* Edirol UM-550 */
  940. { USB_ID(0x0582, 0x0023), 5, "%s Control" },
  941. /* Edirol SD-20 */
  942. { USB_ID(0x0582, 0x0027), 0, "%s Part A" },
  943. { USB_ID(0x0582, 0x0027), 1, "%s Part B" },
  944. { USB_ID(0x0582, 0x0027), 2, "%s MIDI" },
  945. /* Edirol SD-80 */
  946. { USB_ID(0x0582, 0x0029), 0, "%s Part A" },
  947. { USB_ID(0x0582, 0x0029), 1, "%s Part B" },
  948. { USB_ID(0x0582, 0x0029), 2, "%s MIDI 1" },
  949. { USB_ID(0x0582, 0x0029), 3, "%s MIDI 2" },
  950. /* Edirol UA-700 */
  951. { USB_ID(0x0582, 0x002b), 0, "%s MIDI" },
  952. { USB_ID(0x0582, 0x002b), 1, "%s Control" },
  953. /* Roland VariOS */
  954. { USB_ID(0x0582, 0x002f), 0, "%s MIDI" },
  955. { USB_ID(0x0582, 0x002f), 1, "%s External MIDI" },
  956. { USB_ID(0x0582, 0x002f), 2, "%s Sync" },
  957. /* Edirol PCR */
  958. { USB_ID(0x0582, 0x0033), 0, "%s MIDI" },
  959. { USB_ID(0x0582, 0x0033), 1, "%s 1" },
  960. { USB_ID(0x0582, 0x0033), 2, "%s 2" },
  961. /* BOSS GS-10 */
  962. { USB_ID(0x0582, 0x003b), 0, "%s MIDI" },
  963. { USB_ID(0x0582, 0x003b), 1, "%s Control" },
  964. /* Edirol UA-1000 */
  965. { USB_ID(0x0582, 0x0044), 0, "%s MIDI" },
  966. { USB_ID(0x0582, 0x0044), 1, "%s Control" },
  967. /* Edirol UR-80 */
  968. { USB_ID(0x0582, 0x0048), 0, "%s MIDI" },
  969. { USB_ID(0x0582, 0x0048), 1, "%s 1" },
  970. { USB_ID(0x0582, 0x0048), 2, "%s 2" },
  971. /* Edirol PCR-A */
  972. { USB_ID(0x0582, 0x004d), 0, "%s MIDI" },
  973. { USB_ID(0x0582, 0x004d), 1, "%s 1" },
  974. { USB_ID(0x0582, 0x004d), 2, "%s 2" },
  975. /* M-Audio MidiSport 8x8 */
  976. { USB_ID(0x0763, 0x1031), 8, "%s Control" },
  977. { USB_ID(0x0763, 0x1033), 8, "%s Control" },
  978. /* MOTU Fastlane */
  979. { USB_ID(0x07fd, 0x0001), 0, "%s MIDI A" },
  980. { USB_ID(0x07fd, 0x0001), 1, "%s MIDI B" },
  981. /* Emagic Unitor8/AMT8/MT4 */
  982. { USB_ID(0x086a, 0x0001), 8, "%s Broadcast" },
  983. { USB_ID(0x086a, 0x0002), 8, "%s Broadcast" },
  984. { USB_ID(0x086a, 0x0003), 4, "%s Broadcast" },
  985. };
  986. static void snd_usbmidi_init_substream(snd_usb_midi_t* umidi,
  987. int stream, int number,
  988. snd_rawmidi_substream_t** rsubstream)
  989. {
  990. int i;
  991. const char *name_format;
  992. snd_rawmidi_substream_t* substream = snd_usbmidi_find_substream(umidi, stream, number);
  993. if (!substream) {
  994. snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
  995. return;
  996. }
  997. /* TODO: read port name from jack descriptor */
  998. name_format = "%s MIDI %d";
  999. for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_names); ++i) {
  1000. if (snd_usbmidi_port_names[i].id == umidi->chip->usb_id &&
  1001. snd_usbmidi_port_names[i].port == number) {
  1002. name_format = snd_usbmidi_port_names[i].name_format;
  1003. break;
  1004. }
  1005. }
  1006. snprintf(substream->name, sizeof(substream->name),
  1007. name_format, umidi->chip->card->shortname, number + 1);
  1008. *rsubstream = substream;
  1009. }
  1010. /*
  1011. * Creates the endpoints and their ports.
  1012. */
  1013. static int snd_usbmidi_create_endpoints(snd_usb_midi_t* umidi,
  1014. snd_usb_midi_endpoint_info_t* endpoints)
  1015. {
  1016. int i, j, err;
  1017. int out_ports = 0, in_ports = 0;
  1018. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1019. if (endpoints[i].out_cables) {
  1020. err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
  1021. &umidi->endpoints[i]);
  1022. if (err < 0)
  1023. return err;
  1024. }
  1025. if (endpoints[i].in_cables) {
  1026. err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
  1027. &umidi->endpoints[i]);
  1028. if (err < 0)
  1029. return err;
  1030. }
  1031. for (j = 0; j < 0x10; ++j) {
  1032. if (endpoints[i].out_cables & (1 << j)) {
  1033. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
  1034. &umidi->endpoints[i].out->ports[j].substream);
  1035. ++out_ports;
  1036. }
  1037. if (endpoints[i].in_cables & (1 << j)) {
  1038. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
  1039. &umidi->endpoints[i].in->ports[j].substream);
  1040. ++in_ports;
  1041. }
  1042. }
  1043. }
  1044. snd_printdd(KERN_INFO "created %d output and %d input ports\n",
  1045. out_ports, in_ports);
  1046. return 0;
  1047. }
  1048. /*
  1049. * Returns MIDIStreaming device capabilities.
  1050. */
  1051. static int snd_usbmidi_get_ms_info(snd_usb_midi_t* umidi,
  1052. snd_usb_midi_endpoint_info_t* endpoints)
  1053. {
  1054. struct usb_interface* intf;
  1055. struct usb_host_interface *hostif;
  1056. struct usb_interface_descriptor* intfd;
  1057. struct usb_ms_header_descriptor* ms_header;
  1058. struct usb_host_endpoint *hostep;
  1059. struct usb_endpoint_descriptor* ep;
  1060. struct usb_ms_endpoint_descriptor* ms_ep;
  1061. int i, epidx;
  1062. intf = umidi->iface;
  1063. if (!intf)
  1064. return -ENXIO;
  1065. hostif = &intf->altsetting[0];
  1066. intfd = get_iface_desc(hostif);
  1067. ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
  1068. if (hostif->extralen >= 7 &&
  1069. ms_header->bLength >= 7 &&
  1070. ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
  1071. ms_header->bDescriptorSubtype == HEADER)
  1072. snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
  1073. ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
  1074. else
  1075. snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
  1076. epidx = 0;
  1077. for (i = 0; i < intfd->bNumEndpoints; ++i) {
  1078. hostep = &hostif->endpoint[i];
  1079. ep = get_ep_desc(hostep);
  1080. if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
  1081. (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  1082. continue;
  1083. ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
  1084. if (hostep->extralen < 4 ||
  1085. ms_ep->bLength < 4 ||
  1086. ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
  1087. ms_ep->bDescriptorSubtype != MS_GENERAL)
  1088. continue;
  1089. if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1090. if (endpoints[epidx].out_ep) {
  1091. if (++epidx >= MIDI_MAX_ENDPOINTS) {
  1092. snd_printk(KERN_WARNING "too many endpoints\n");
  1093. break;
  1094. }
  1095. }
  1096. endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1097. if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
  1098. endpoints[epidx].out_interval = ep->bInterval;
  1099. endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
  1100. snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
  1101. ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
  1102. } else {
  1103. if (endpoints[epidx].in_ep) {
  1104. if (++epidx >= MIDI_MAX_ENDPOINTS) {
  1105. snd_printk(KERN_WARNING "too many endpoints\n");
  1106. break;
  1107. }
  1108. }
  1109. endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1110. if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
  1111. endpoints[epidx].in_interval = ep->bInterval;
  1112. endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
  1113. snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
  1114. ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
  1115. }
  1116. }
  1117. return 0;
  1118. }
  1119. /*
  1120. * On Roland devices, use the second alternate setting to be able to use
  1121. * the interrupt input endpoint.
  1122. */
  1123. static void snd_usbmidi_switch_roland_altsetting(snd_usb_midi_t* umidi)
  1124. {
  1125. struct usb_interface* intf;
  1126. struct usb_host_interface *hostif;
  1127. struct usb_interface_descriptor* intfd;
  1128. intf = umidi->iface;
  1129. if (!intf || intf->num_altsetting != 2)
  1130. return;
  1131. hostif = &intf->altsetting[1];
  1132. intfd = get_iface_desc(hostif);
  1133. if (intfd->bNumEndpoints != 2 ||
  1134. (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
  1135. (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  1136. return;
  1137. snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
  1138. intfd->bAlternateSetting);
  1139. usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
  1140. intfd->bAlternateSetting);
  1141. }
  1142. /*
  1143. * Try to find any usable endpoints in the interface.
  1144. */
  1145. static int snd_usbmidi_detect_endpoints(snd_usb_midi_t* umidi,
  1146. snd_usb_midi_endpoint_info_t* endpoint,
  1147. int max_endpoints)
  1148. {
  1149. struct usb_interface* intf;
  1150. struct usb_host_interface *hostif;
  1151. struct usb_interface_descriptor* intfd;
  1152. struct usb_endpoint_descriptor* epd;
  1153. int i, out_eps = 0, in_eps = 0;
  1154. if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
  1155. snd_usbmidi_switch_roland_altsetting(umidi);
  1156. if (endpoint[0].out_ep || endpoint[0].in_ep)
  1157. return 0;
  1158. intf = umidi->iface;
  1159. if (!intf || intf->num_altsetting < 1)
  1160. return -ENOENT;
  1161. hostif = intf->cur_altsetting;
  1162. intfd = get_iface_desc(hostif);
  1163. for (i = 0; i < intfd->bNumEndpoints; ++i) {
  1164. epd = get_endpoint(hostif, i);
  1165. if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
  1166. (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  1167. continue;
  1168. if (out_eps < max_endpoints &&
  1169. (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1170. endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1171. if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
  1172. endpoint[out_eps].out_interval = epd->bInterval;
  1173. ++out_eps;
  1174. }
  1175. if (in_eps < max_endpoints &&
  1176. (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  1177. endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1178. if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
  1179. endpoint[in_eps].in_interval = epd->bInterval;
  1180. ++in_eps;
  1181. }
  1182. }
  1183. return (out_eps || in_eps) ? 0 : -ENOENT;
  1184. }
  1185. /*
  1186. * Detects the endpoints for one-port-per-endpoint protocols.
  1187. */
  1188. static int snd_usbmidi_detect_per_port_endpoints(snd_usb_midi_t* umidi,
  1189. snd_usb_midi_endpoint_info_t* endpoints)
  1190. {
  1191. int err, i;
  1192. err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
  1193. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1194. if (endpoints[i].out_ep)
  1195. endpoints[i].out_cables = 0x0001;
  1196. if (endpoints[i].in_ep)
  1197. endpoints[i].in_cables = 0x0001;
  1198. }
  1199. return err;
  1200. }
  1201. /*
  1202. * Detects the endpoints and ports of Yamaha devices.
  1203. */
  1204. static int snd_usbmidi_detect_yamaha(snd_usb_midi_t* umidi,
  1205. snd_usb_midi_endpoint_info_t* endpoint)
  1206. {
  1207. struct usb_interface* intf;
  1208. struct usb_host_interface *hostif;
  1209. struct usb_interface_descriptor* intfd;
  1210. uint8_t* cs_desc;
  1211. intf = umidi->iface;
  1212. if (!intf)
  1213. return -ENOENT;
  1214. hostif = intf->altsetting;
  1215. intfd = get_iface_desc(hostif);
  1216. if (intfd->bNumEndpoints < 1)
  1217. return -ENOENT;
  1218. /*
  1219. * For each port there is one MIDI_IN/OUT_JACK descriptor, not
  1220. * necessarily with any useful contents. So simply count 'em.
  1221. */
  1222. for (cs_desc = hostif->extra;
  1223. cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
  1224. cs_desc += cs_desc[0]) {
  1225. if (cs_desc[1] == CS_AUDIO_INTERFACE) {
  1226. if (cs_desc[2] == MIDI_IN_JACK)
  1227. endpoint->in_cables = (endpoint->in_cables << 1) | 1;
  1228. else if (cs_desc[2] == MIDI_OUT_JACK)
  1229. endpoint->out_cables = (endpoint->out_cables << 1) | 1;
  1230. }
  1231. }
  1232. if (!endpoint->in_cables && !endpoint->out_cables)
  1233. return -ENOENT;
  1234. return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
  1235. }
  1236. /*
  1237. * Creates the endpoints and their ports for Midiman devices.
  1238. */
  1239. static int snd_usbmidi_create_endpoints_midiman(snd_usb_midi_t* umidi,
  1240. snd_usb_midi_endpoint_info_t* endpoint)
  1241. {
  1242. snd_usb_midi_endpoint_info_t ep_info;
  1243. struct usb_interface* intf;
  1244. struct usb_host_interface *hostif;
  1245. struct usb_interface_descriptor* intfd;
  1246. struct usb_endpoint_descriptor* epd;
  1247. int cable, err;
  1248. intf = umidi->iface;
  1249. if (!intf)
  1250. return -ENOENT;
  1251. hostif = intf->altsetting;
  1252. intfd = get_iface_desc(hostif);
  1253. /*
  1254. * The various MidiSport devices have more or less random endpoint
  1255. * numbers, so we have to identify the endpoints by their index in
  1256. * the descriptor array, like the driver for that other OS does.
  1257. *
  1258. * There is one interrupt input endpoint for all input ports, one
  1259. * bulk output endpoint for even-numbered ports, and one for odd-
  1260. * numbered ports. Both bulk output endpoints have corresponding
  1261. * input bulk endpoints (at indices 1 and 3) which aren't used.
  1262. */
  1263. if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
  1264. snd_printdd(KERN_ERR "not enough endpoints\n");
  1265. return -ENOENT;
  1266. }
  1267. epd = get_endpoint(hostif, 0);
  1268. if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
  1269. (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
  1270. snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
  1271. return -ENXIO;
  1272. }
  1273. epd = get_endpoint(hostif, 2);
  1274. if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
  1275. (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
  1276. snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
  1277. return -ENXIO;
  1278. }
  1279. if (endpoint->out_cables > 0x0001) {
  1280. epd = get_endpoint(hostif, 4);
  1281. if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
  1282. (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
  1283. snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
  1284. return -ENXIO;
  1285. }
  1286. }
  1287. ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1288. ep_info.out_cables = endpoint->out_cables & 0x5555;
  1289. err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
  1290. if (err < 0)
  1291. return err;
  1292. ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1293. ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
  1294. ep_info.in_cables = endpoint->in_cables;
  1295. err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
  1296. if (err < 0)
  1297. return err;
  1298. if (endpoint->out_cables > 0x0001) {
  1299. ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1300. ep_info.out_cables = endpoint->out_cables & 0xaaaa;
  1301. err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
  1302. if (err < 0)
  1303. return err;
  1304. }
  1305. for (cable = 0; cable < 0x10; ++cable) {
  1306. if (endpoint->out_cables & (1 << cable))
  1307. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
  1308. &umidi->endpoints[cable & 1].out->ports[cable].substream);
  1309. if (endpoint->in_cables & (1 << cable))
  1310. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
  1311. &umidi->endpoints[0].in->ports[cable].substream);
  1312. }
  1313. return 0;
  1314. }
  1315. static int snd_usbmidi_create_rawmidi(snd_usb_midi_t* umidi,
  1316. int out_ports, int in_ports)
  1317. {
  1318. snd_rawmidi_t* rmidi;
  1319. int err;
  1320. err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
  1321. umidi->chip->next_midi_device++,
  1322. out_ports, in_ports, &rmidi);
  1323. if (err < 0)
  1324. return err;
  1325. strcpy(rmidi->name, umidi->chip->card->shortname);
  1326. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  1327. SNDRV_RAWMIDI_INFO_INPUT |
  1328. SNDRV_RAWMIDI_INFO_DUPLEX;
  1329. rmidi->private_data = umidi;
  1330. rmidi->private_free = snd_usbmidi_rawmidi_free;
  1331. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
  1332. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
  1333. umidi->rmidi = rmidi;
  1334. return 0;
  1335. }
  1336. /*
  1337. * Temporarily stop input.
  1338. */
  1339. void snd_usbmidi_input_stop(struct list_head* p)
  1340. {
  1341. snd_usb_midi_t* umidi;
  1342. int i;
  1343. umidi = list_entry(p, snd_usb_midi_t, list);
  1344. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1345. snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
  1346. if (ep->in)
  1347. usb_kill_urb(ep->in->urb);
  1348. }
  1349. }
  1350. static void snd_usbmidi_input_start_ep(snd_usb_midi_in_endpoint_t* ep)
  1351. {
  1352. if (ep) {
  1353. struct urb* urb = ep->urb;
  1354. urb->dev = ep->umidi->chip->dev;
  1355. snd_usbmidi_submit_urb(urb, GFP_KERNEL);
  1356. }
  1357. }
  1358. /*
  1359. * Resume input after a call to snd_usbmidi_input_stop().
  1360. */
  1361. void snd_usbmidi_input_start(struct list_head* p)
  1362. {
  1363. snd_usb_midi_t* umidi;
  1364. int i;
  1365. umidi = list_entry(p, snd_usb_midi_t, list);
  1366. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
  1367. snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
  1368. }
  1369. /*
  1370. * Creates and registers everything needed for a MIDI streaming interface.
  1371. */
  1372. int snd_usb_create_midi_interface(snd_usb_audio_t* chip,
  1373. struct usb_interface* iface,
  1374. const snd_usb_audio_quirk_t* quirk)
  1375. {
  1376. snd_usb_midi_t* umidi;
  1377. snd_usb_midi_endpoint_info_t endpoints[MIDI_MAX_ENDPOINTS];
  1378. int out_ports, in_ports;
  1379. int i, err;
  1380. umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
  1381. if (!umidi)
  1382. return -ENOMEM;
  1383. umidi->chip = chip;
  1384. umidi->iface = iface;
  1385. umidi->quirk = quirk;
  1386. umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
  1387. init_timer(&umidi->error_timer);
  1388. umidi->error_timer.function = snd_usbmidi_error_timer;
  1389. umidi->error_timer.data = (unsigned long)umidi;
  1390. /* detect the endpoint(s) to use */
  1391. memset(endpoints, 0, sizeof(endpoints));
  1392. switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
  1393. case QUIRK_MIDI_STANDARD_INTERFACE:
  1394. err = snd_usbmidi_get_ms_info(umidi, endpoints);
  1395. break;
  1396. case QUIRK_MIDI_FIXED_ENDPOINT:
  1397. memcpy(&endpoints[0], quirk->data,
  1398. sizeof(snd_usb_midi_endpoint_info_t));
  1399. err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
  1400. break;
  1401. case QUIRK_MIDI_YAMAHA:
  1402. err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
  1403. break;
  1404. case QUIRK_MIDI_MIDIMAN:
  1405. umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
  1406. memcpy(&endpoints[0], quirk->data,
  1407. sizeof(snd_usb_midi_endpoint_info_t));
  1408. err = 0;
  1409. break;
  1410. case QUIRK_MIDI_NOVATION:
  1411. umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
  1412. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  1413. break;
  1414. case QUIRK_MIDI_RAW:
  1415. umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
  1416. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  1417. break;
  1418. case QUIRK_MIDI_EMAGIC:
  1419. umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
  1420. memcpy(&endpoints[0], quirk->data,
  1421. sizeof(snd_usb_midi_endpoint_info_t));
  1422. err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
  1423. break;
  1424. case QUIRK_MIDI_MIDITECH:
  1425. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  1426. break;
  1427. default:
  1428. snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
  1429. err = -ENXIO;
  1430. break;
  1431. }
  1432. if (err < 0) {
  1433. kfree(umidi);
  1434. return err;
  1435. }
  1436. /* create rawmidi device */
  1437. out_ports = 0;
  1438. in_ports = 0;
  1439. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1440. out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
  1441. in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
  1442. }
  1443. err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
  1444. if (err < 0) {
  1445. kfree(umidi);
  1446. return err;
  1447. }
  1448. /* create endpoint/port structures */
  1449. if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
  1450. err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
  1451. else
  1452. err = snd_usbmidi_create_endpoints(umidi, endpoints);
  1453. if (err < 0) {
  1454. snd_usbmidi_free(umidi);
  1455. return err;
  1456. }
  1457. list_add(&umidi->list, &umidi->chip->midi_list);
  1458. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
  1459. snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
  1460. return 0;
  1461. }
  1462. EXPORT_SYMBOL(snd_usb_create_midi_interface);
  1463. EXPORT_SYMBOL(snd_usbmidi_input_stop);
  1464. EXPORT_SYMBOL(snd_usbmidi_input_start);
  1465. EXPORT_SYMBOL(snd_usbmidi_disconnect);