usbmidi.c 52 KB

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