midi.c 62 KB

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