usbmidi.c 50 KB

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