usbmidi.c 45 KB

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