usbmidi.c 50 KB

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