usbmidi.c 51 KB

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