midi.c 63 KB

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