midi.c 69 KB

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