midi.c 62 KB

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