usbmidi.c 49 KB

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