midi.c 61 KB

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