usbmidi.c 47 KB

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