midi.c 63 KB

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