usbmidi.c 52 KB

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