midi.c 70 KB

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