usbmidi.c 53 KB

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