midi.c 63 KB

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