midi.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  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: just move raw MIDI bytes from/to the endpoint
  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. switch (snd_usb_get_speed(ep->umidi->dev)) {
  754. case USB_SPEED_HIGH:
  755. case USB_SPEED_SUPER:
  756. count = 1;
  757. break;
  758. default:
  759. count = 2;
  760. }
  761. count = snd_rawmidi_transmit(ep->ports[0].substream,
  762. urb->transfer_buffer,
  763. count);
  764. if (count < 1) {
  765. ep->ports[0].active = 0;
  766. return;
  767. }
  768. memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
  769. urb->transfer_buffer_length = ep->max_transfer;
  770. }
  771. static struct usb_protocol_ops snd_usbmidi_122l_ops = {
  772. .input = snd_usbmidi_us122l_input,
  773. .output = snd_usbmidi_us122l_output,
  774. };
  775. /*
  776. * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
  777. */
  778. static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
  779. {
  780. static const u8 init_data[] = {
  781. /* initialization magic: "get version" */
  782. 0xf0,
  783. 0x00, 0x20, 0x31, /* Emagic */
  784. 0x64, /* Unitor8 */
  785. 0x0b, /* version number request */
  786. 0x00, /* command version */
  787. 0x00, /* EEPROM, box 0 */
  788. 0xf7
  789. };
  790. send_bulk_static_data(ep, init_data, sizeof(init_data));
  791. /* while we're at it, pour on more magic */
  792. send_bulk_static_data(ep, init_data, sizeof(init_data));
  793. }
  794. static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
  795. {
  796. static const u8 finish_data[] = {
  797. /* switch to patch mode with last preset */
  798. 0xf0,
  799. 0x00, 0x20, 0x31, /* Emagic */
  800. 0x64, /* Unitor8 */
  801. 0x10, /* patch switch command */
  802. 0x00, /* command version */
  803. 0x7f, /* to all boxes */
  804. 0x40, /* last preset in EEPROM */
  805. 0xf7
  806. };
  807. send_bulk_static_data(ep, finish_data, sizeof(finish_data));
  808. }
  809. static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
  810. uint8_t* buffer, int buffer_length)
  811. {
  812. int i;
  813. /* FF indicates end of valid data */
  814. for (i = 0; i < buffer_length; ++i)
  815. if (buffer[i] == 0xff) {
  816. buffer_length = i;
  817. break;
  818. }
  819. /* handle F5 at end of last buffer */
  820. if (ep->seen_f5)
  821. goto switch_port;
  822. while (buffer_length > 0) {
  823. /* determine size of data until next F5 */
  824. for (i = 0; i < buffer_length; ++i)
  825. if (buffer[i] == 0xf5)
  826. break;
  827. snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
  828. buffer += i;
  829. buffer_length -= i;
  830. if (buffer_length <= 0)
  831. break;
  832. /* assert(buffer[0] == 0xf5); */
  833. ep->seen_f5 = 1;
  834. ++buffer;
  835. --buffer_length;
  836. switch_port:
  837. if (buffer_length <= 0)
  838. break;
  839. if (buffer[0] < 0x80) {
  840. ep->current_port = (buffer[0] - 1) & 15;
  841. ++buffer;
  842. --buffer_length;
  843. }
  844. ep->seen_f5 = 0;
  845. }
  846. }
  847. static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
  848. struct urb *urb)
  849. {
  850. int port0 = ep->current_port;
  851. uint8_t* buf = urb->transfer_buffer;
  852. int buf_free = ep->max_transfer;
  853. int length, i;
  854. for (i = 0; i < 0x10; ++i) {
  855. /* round-robin, starting at the last current port */
  856. int portnum = (port0 + i) & 15;
  857. struct usbmidi_out_port* port = &ep->ports[portnum];
  858. if (!port->active)
  859. continue;
  860. if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
  861. port->active = 0;
  862. continue;
  863. }
  864. if (portnum != ep->current_port) {
  865. if (buf_free < 2)
  866. break;
  867. ep->current_port = portnum;
  868. buf[0] = 0xf5;
  869. buf[1] = (portnum + 1) & 15;
  870. buf += 2;
  871. buf_free -= 2;
  872. }
  873. if (buf_free < 1)
  874. break;
  875. length = snd_rawmidi_transmit(port->substream, buf, buf_free);
  876. if (length > 0) {
  877. buf += length;
  878. buf_free -= length;
  879. if (buf_free < 1)
  880. break;
  881. }
  882. }
  883. if (buf_free < ep->max_transfer && buf_free > 0) {
  884. *buf = 0xff;
  885. --buf_free;
  886. }
  887. urb->transfer_buffer_length = ep->max_transfer - buf_free;
  888. }
  889. static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
  890. .input = snd_usbmidi_emagic_input,
  891. .output = snd_usbmidi_emagic_output,
  892. .init_out_endpoint = snd_usbmidi_emagic_init_out,
  893. .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
  894. };
  895. static void update_roland_altsetting(struct snd_usb_midi* umidi)
  896. {
  897. struct usb_interface *intf;
  898. struct usb_host_interface *hostif;
  899. struct usb_interface_descriptor *intfd;
  900. int is_light_load;
  901. intf = umidi->iface;
  902. is_light_load = intf->cur_altsetting != intf->altsetting;
  903. if (umidi->roland_load_ctl->private_value == is_light_load)
  904. return;
  905. hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
  906. intfd = get_iface_desc(hostif);
  907. snd_usbmidi_input_stop(&umidi->list);
  908. usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
  909. intfd->bAlternateSetting);
  910. snd_usbmidi_input_start(&umidi->list);
  911. }
  912. static void substream_open(struct snd_rawmidi_substream *substream, int open)
  913. {
  914. struct snd_usb_midi* umidi = substream->rmidi->private_data;
  915. struct snd_kcontrol *ctl;
  916. mutex_lock(&umidi->mutex);
  917. if (open) {
  918. if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
  919. ctl = umidi->roland_load_ctl;
  920. ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  921. snd_ctl_notify(umidi->card,
  922. SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
  923. update_roland_altsetting(umidi);
  924. }
  925. } else {
  926. if (--umidi->opened == 0 && umidi->roland_load_ctl) {
  927. ctl = umidi->roland_load_ctl;
  928. ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  929. snd_ctl_notify(umidi->card,
  930. SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
  931. }
  932. }
  933. mutex_unlock(&umidi->mutex);
  934. }
  935. static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
  936. {
  937. struct snd_usb_midi* umidi = substream->rmidi->private_data;
  938. struct usbmidi_out_port* port = NULL;
  939. int i, j;
  940. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
  941. if (umidi->endpoints[i].out)
  942. for (j = 0; j < 0x10; ++j)
  943. if (umidi->endpoints[i].out->ports[j].substream == substream) {
  944. port = &umidi->endpoints[i].out->ports[j];
  945. break;
  946. }
  947. if (!port) {
  948. snd_BUG();
  949. return -ENXIO;
  950. }
  951. substream->runtime->private_data = port;
  952. port->state = STATE_UNKNOWN;
  953. substream_open(substream, 1);
  954. return 0;
  955. }
  956. static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
  957. {
  958. substream_open(substream, 0);
  959. return 0;
  960. }
  961. static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  962. {
  963. struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
  964. port->active = up;
  965. if (up) {
  966. if (port->ep->umidi->disconnected) {
  967. /* gobble up remaining bytes to prevent wait in
  968. * snd_rawmidi_drain_output */
  969. while (!snd_rawmidi_transmit_empty(substream))
  970. snd_rawmidi_transmit_ack(substream, 1);
  971. return;
  972. }
  973. tasklet_schedule(&port->ep->tasklet);
  974. }
  975. }
  976. static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
  977. {
  978. struct usbmidi_out_port* port = substream->runtime->private_data;
  979. struct snd_usb_midi_out_endpoint *ep = port->ep;
  980. unsigned int drain_urbs;
  981. DEFINE_WAIT(wait);
  982. long timeout = msecs_to_jiffies(50);
  983. if (ep->umidi->disconnected)
  984. return;
  985. /*
  986. * The substream buffer is empty, but some data might still be in the
  987. * currently active URBs, so we have to wait for those to complete.
  988. */
  989. spin_lock_irq(&ep->buffer_lock);
  990. drain_urbs = ep->active_urbs;
  991. if (drain_urbs) {
  992. ep->drain_urbs |= drain_urbs;
  993. do {
  994. prepare_to_wait(&ep->drain_wait, &wait,
  995. TASK_UNINTERRUPTIBLE);
  996. spin_unlock_irq(&ep->buffer_lock);
  997. timeout = schedule_timeout(timeout);
  998. spin_lock_irq(&ep->buffer_lock);
  999. drain_urbs &= ep->drain_urbs;
  1000. } while (drain_urbs && timeout);
  1001. finish_wait(&ep->drain_wait, &wait);
  1002. }
  1003. spin_unlock_irq(&ep->buffer_lock);
  1004. }
  1005. static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
  1006. {
  1007. substream_open(substream, 1);
  1008. return 0;
  1009. }
  1010. static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
  1011. {
  1012. substream_open(substream, 0);
  1013. return 0;
  1014. }
  1015. static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  1016. {
  1017. struct snd_usb_midi* umidi = substream->rmidi->private_data;
  1018. if (up)
  1019. set_bit(substream->number, &umidi->input_triggered);
  1020. else
  1021. clear_bit(substream->number, &umidi->input_triggered);
  1022. }
  1023. static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
  1024. .open = snd_usbmidi_output_open,
  1025. .close = snd_usbmidi_output_close,
  1026. .trigger = snd_usbmidi_output_trigger,
  1027. .drain = snd_usbmidi_output_drain,
  1028. };
  1029. static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
  1030. .open = snd_usbmidi_input_open,
  1031. .close = snd_usbmidi_input_close,
  1032. .trigger = snd_usbmidi_input_trigger
  1033. };
  1034. static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
  1035. unsigned int buffer_length)
  1036. {
  1037. usb_free_coherent(umidi->dev, buffer_length,
  1038. urb->transfer_buffer, urb->transfer_dma);
  1039. usb_free_urb(urb);
  1040. }
  1041. /*
  1042. * Frees an input endpoint.
  1043. * May be called when ep hasn't been initialized completely.
  1044. */
  1045. static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
  1046. {
  1047. unsigned int i;
  1048. for (i = 0; i < INPUT_URBS; ++i)
  1049. if (ep->urbs[i])
  1050. free_urb_and_buffer(ep->umidi, ep->urbs[i],
  1051. ep->urbs[i]->transfer_buffer_length);
  1052. kfree(ep);
  1053. }
  1054. /*
  1055. * Creates an input endpoint.
  1056. */
  1057. static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
  1058. struct snd_usb_midi_endpoint_info* ep_info,
  1059. struct snd_usb_midi_endpoint* rep)
  1060. {
  1061. struct snd_usb_midi_in_endpoint* ep;
  1062. void* buffer;
  1063. unsigned int pipe;
  1064. int length;
  1065. unsigned int i;
  1066. rep->in = NULL;
  1067. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1068. if (!ep)
  1069. return -ENOMEM;
  1070. ep->umidi = umidi;
  1071. for (i = 0; i < INPUT_URBS; ++i) {
  1072. ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
  1073. if (!ep->urbs[i]) {
  1074. snd_usbmidi_in_endpoint_delete(ep);
  1075. return -ENOMEM;
  1076. }
  1077. }
  1078. if (ep_info->in_interval)
  1079. pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
  1080. else
  1081. pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
  1082. length = usb_maxpacket(umidi->dev, pipe, 0);
  1083. for (i = 0; i < INPUT_URBS; ++i) {
  1084. buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
  1085. &ep->urbs[i]->transfer_dma);
  1086. if (!buffer) {
  1087. snd_usbmidi_in_endpoint_delete(ep);
  1088. return -ENOMEM;
  1089. }
  1090. if (ep_info->in_interval)
  1091. usb_fill_int_urb(ep->urbs[i], umidi->dev,
  1092. pipe, buffer, length,
  1093. snd_usbmidi_in_urb_complete,
  1094. ep, ep_info->in_interval);
  1095. else
  1096. usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
  1097. pipe, buffer, length,
  1098. snd_usbmidi_in_urb_complete, ep);
  1099. ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1100. }
  1101. rep->in = ep;
  1102. return 0;
  1103. }
  1104. /*
  1105. * Frees an output endpoint.
  1106. * May be called when ep hasn't been initialized completely.
  1107. */
  1108. static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
  1109. {
  1110. unsigned int i;
  1111. for (i = 0; i < OUTPUT_URBS; ++i)
  1112. if (ep->urbs[i].urb) {
  1113. free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
  1114. ep->max_transfer);
  1115. ep->urbs[i].urb = NULL;
  1116. }
  1117. }
  1118. static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
  1119. {
  1120. snd_usbmidi_out_endpoint_clear(ep);
  1121. kfree(ep);
  1122. }
  1123. /*
  1124. * Creates an output endpoint, and initializes output ports.
  1125. */
  1126. static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
  1127. struct snd_usb_midi_endpoint_info* ep_info,
  1128. struct snd_usb_midi_endpoint* rep)
  1129. {
  1130. struct snd_usb_midi_out_endpoint* ep;
  1131. unsigned int i;
  1132. unsigned int pipe;
  1133. void* buffer;
  1134. rep->out = NULL;
  1135. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1136. if (!ep)
  1137. return -ENOMEM;
  1138. ep->umidi = umidi;
  1139. for (i = 0; i < OUTPUT_URBS; ++i) {
  1140. ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
  1141. if (!ep->urbs[i].urb) {
  1142. snd_usbmidi_out_endpoint_delete(ep);
  1143. return -ENOMEM;
  1144. }
  1145. ep->urbs[i].ep = ep;
  1146. }
  1147. if (ep_info->out_interval)
  1148. pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
  1149. else
  1150. pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
  1151. switch (umidi->usb_id) {
  1152. default:
  1153. ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
  1154. break;
  1155. /*
  1156. * Various chips declare a packet size larger than 4 bytes, but
  1157. * do not actually work with larger packets:
  1158. */
  1159. case USB_ID(0x0a92, 0x1020): /* ESI M4U */
  1160. case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
  1161. case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
  1162. case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
  1163. case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
  1164. ep->max_transfer = 4;
  1165. break;
  1166. /*
  1167. * Some devices only work with 9 bytes packet size:
  1168. */
  1169. case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
  1170. case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
  1171. ep->max_transfer = 9;
  1172. break;
  1173. }
  1174. for (i = 0; i < OUTPUT_URBS; ++i) {
  1175. buffer = usb_alloc_coherent(umidi->dev,
  1176. ep->max_transfer, GFP_KERNEL,
  1177. &ep->urbs[i].urb->transfer_dma);
  1178. if (!buffer) {
  1179. snd_usbmidi_out_endpoint_delete(ep);
  1180. return -ENOMEM;
  1181. }
  1182. if (ep_info->out_interval)
  1183. usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
  1184. pipe, buffer, ep->max_transfer,
  1185. snd_usbmidi_out_urb_complete,
  1186. &ep->urbs[i], ep_info->out_interval);
  1187. else
  1188. usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
  1189. pipe, buffer, ep->max_transfer,
  1190. snd_usbmidi_out_urb_complete,
  1191. &ep->urbs[i]);
  1192. ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1193. }
  1194. spin_lock_init(&ep->buffer_lock);
  1195. tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
  1196. init_waitqueue_head(&ep->drain_wait);
  1197. for (i = 0; i < 0x10; ++i)
  1198. if (ep_info->out_cables & (1 << i)) {
  1199. ep->ports[i].ep = ep;
  1200. ep->ports[i].cable = i << 4;
  1201. }
  1202. if (umidi->usb_protocol_ops->init_out_endpoint)
  1203. umidi->usb_protocol_ops->init_out_endpoint(ep);
  1204. rep->out = ep;
  1205. return 0;
  1206. }
  1207. /*
  1208. * Frees everything.
  1209. */
  1210. static void snd_usbmidi_free(struct snd_usb_midi* umidi)
  1211. {
  1212. int i;
  1213. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1214. struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
  1215. if (ep->out)
  1216. snd_usbmidi_out_endpoint_delete(ep->out);
  1217. if (ep->in)
  1218. snd_usbmidi_in_endpoint_delete(ep->in);
  1219. }
  1220. mutex_destroy(&umidi->mutex);
  1221. kfree(umidi);
  1222. }
  1223. /*
  1224. * Unlinks all URBs (must be done before the usb_device is deleted).
  1225. */
  1226. void snd_usbmidi_disconnect(struct list_head* p)
  1227. {
  1228. struct snd_usb_midi* umidi;
  1229. unsigned int i, j;
  1230. umidi = list_entry(p, struct snd_usb_midi, list);
  1231. /*
  1232. * an URB's completion handler may start the timer and
  1233. * a timer may submit an URB. To reliably break the cycle
  1234. * a flag under lock must be used
  1235. */
  1236. spin_lock_irq(&umidi->disc_lock);
  1237. umidi->disconnected = 1;
  1238. spin_unlock_irq(&umidi->disc_lock);
  1239. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1240. struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
  1241. if (ep->out)
  1242. tasklet_kill(&ep->out->tasklet);
  1243. if (ep->out) {
  1244. for (j = 0; j < OUTPUT_URBS; ++j)
  1245. usb_kill_urb(ep->out->urbs[j].urb);
  1246. if (umidi->usb_protocol_ops->finish_out_endpoint)
  1247. umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
  1248. ep->out->active_urbs = 0;
  1249. if (ep->out->drain_urbs) {
  1250. ep->out->drain_urbs = 0;
  1251. wake_up(&ep->out->drain_wait);
  1252. }
  1253. }
  1254. if (ep->in)
  1255. for (j = 0; j < INPUT_URBS; ++j)
  1256. usb_kill_urb(ep->in->urbs[j]);
  1257. /* free endpoints here; later call can result in Oops */
  1258. if (ep->out)
  1259. snd_usbmidi_out_endpoint_clear(ep->out);
  1260. if (ep->in) {
  1261. snd_usbmidi_in_endpoint_delete(ep->in);
  1262. ep->in = NULL;
  1263. }
  1264. }
  1265. del_timer_sync(&umidi->error_timer);
  1266. }
  1267. static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
  1268. {
  1269. struct snd_usb_midi* umidi = rmidi->private_data;
  1270. snd_usbmidi_free(umidi);
  1271. }
  1272. static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
  1273. int stream, int number)
  1274. {
  1275. struct list_head* list;
  1276. list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
  1277. struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
  1278. if (substream->number == number)
  1279. return substream;
  1280. }
  1281. return NULL;
  1282. }
  1283. /*
  1284. * This list specifies names for ports that do not fit into the standard
  1285. * "(product) MIDI (n)" schema because they aren't external MIDI ports,
  1286. * such as internal control or synthesizer ports.
  1287. */
  1288. static struct port_info {
  1289. u32 id;
  1290. short int port;
  1291. short int voices;
  1292. const char *name;
  1293. unsigned int seq_flags;
  1294. } snd_usbmidi_port_info[] = {
  1295. #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
  1296. { .id = USB_ID(vendor, product), \
  1297. .port = num, .voices = voices_, \
  1298. .name = name_, .seq_flags = flags }
  1299. #define EXTERNAL_PORT(vendor, product, num, name) \
  1300. PORT_INFO(vendor, product, num, name, 0, \
  1301. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1302. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1303. SNDRV_SEQ_PORT_TYPE_PORT)
  1304. #define CONTROL_PORT(vendor, product, num, name) \
  1305. PORT_INFO(vendor, product, num, name, 0, \
  1306. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1307. SNDRV_SEQ_PORT_TYPE_HARDWARE)
  1308. #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
  1309. PORT_INFO(vendor, product, num, name, voices, \
  1310. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1311. SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
  1312. SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
  1313. SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
  1314. SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
  1315. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1316. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
  1317. #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
  1318. PORT_INFO(vendor, product, num, name, voices, \
  1319. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1320. SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
  1321. SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
  1322. SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
  1323. SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
  1324. SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
  1325. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1326. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
  1327. /* Roland UA-100 */
  1328. CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
  1329. /* Roland SC-8850 */
  1330. SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
  1331. SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
  1332. SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
  1333. SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
  1334. EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
  1335. EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
  1336. /* Roland U-8 */
  1337. EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
  1338. CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
  1339. /* Roland SC-8820 */
  1340. SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
  1341. SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
  1342. EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
  1343. /* Roland SK-500 */
  1344. SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
  1345. SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
  1346. EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
  1347. /* Roland SC-D70 */
  1348. SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
  1349. SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
  1350. EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
  1351. /* Edirol UM-880 */
  1352. CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
  1353. /* Edirol SD-90 */
  1354. ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
  1355. ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
  1356. EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
  1357. EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
  1358. /* Edirol UM-550 */
  1359. CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
  1360. /* Edirol SD-20 */
  1361. ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
  1362. ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
  1363. EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
  1364. /* Edirol SD-80 */
  1365. ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
  1366. ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
  1367. EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
  1368. EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
  1369. /* Edirol UA-700 */
  1370. EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
  1371. CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
  1372. /* Roland VariOS */
  1373. EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
  1374. EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
  1375. EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
  1376. /* Edirol PCR */
  1377. EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
  1378. EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
  1379. EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
  1380. /* BOSS GS-10 */
  1381. EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
  1382. CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
  1383. /* Edirol UA-1000 */
  1384. EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
  1385. CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
  1386. /* Edirol UR-80 */
  1387. EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
  1388. EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
  1389. EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
  1390. /* Edirol PCR-A */
  1391. EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
  1392. EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
  1393. EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
  1394. /* Edirol UM-3EX */
  1395. CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
  1396. /* M-Audio MidiSport 8x8 */
  1397. CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
  1398. CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
  1399. /* MOTU Fastlane */
  1400. EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
  1401. EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
  1402. /* Emagic Unitor8/AMT8/MT4 */
  1403. EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
  1404. EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
  1405. EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
  1406. /* Akai MPD16 */
  1407. CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
  1408. PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
  1409. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  1410. SNDRV_SEQ_PORT_TYPE_HARDWARE),
  1411. /* Access Music Virus TI */
  1412. EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
  1413. PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
  1414. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  1415. SNDRV_SEQ_PORT_TYPE_HARDWARE |
  1416. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
  1417. };
  1418. static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
  1419. {
  1420. int i;
  1421. for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
  1422. if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
  1423. snd_usbmidi_port_info[i].port == number)
  1424. return &snd_usbmidi_port_info[i];
  1425. }
  1426. return NULL;
  1427. }
  1428. static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
  1429. struct snd_seq_port_info *seq_port_info)
  1430. {
  1431. struct snd_usb_midi *umidi = rmidi->private_data;
  1432. struct port_info *port_info;
  1433. /* TODO: read port flags from descriptors */
  1434. port_info = find_port_info(umidi, number);
  1435. if (port_info) {
  1436. seq_port_info->type = port_info->seq_flags;
  1437. seq_port_info->midi_voices = port_info->voices;
  1438. }
  1439. }
  1440. static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
  1441. int stream, int number,
  1442. struct snd_rawmidi_substream ** rsubstream)
  1443. {
  1444. struct port_info *port_info;
  1445. const char *name_format;
  1446. struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
  1447. if (!substream) {
  1448. snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
  1449. return;
  1450. }
  1451. /* TODO: read port name from jack descriptor */
  1452. port_info = find_port_info(umidi, number);
  1453. name_format = port_info ? port_info->name : "%s MIDI %d";
  1454. snprintf(substream->name, sizeof(substream->name),
  1455. name_format, umidi->card->shortname, number + 1);
  1456. *rsubstream = substream;
  1457. }
  1458. /*
  1459. * Creates the endpoints and their ports.
  1460. */
  1461. static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
  1462. struct snd_usb_midi_endpoint_info* endpoints)
  1463. {
  1464. int i, j, err;
  1465. int out_ports = 0, in_ports = 0;
  1466. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1467. if (endpoints[i].out_cables) {
  1468. err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
  1469. &umidi->endpoints[i]);
  1470. if (err < 0)
  1471. return err;
  1472. }
  1473. if (endpoints[i].in_cables) {
  1474. err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
  1475. &umidi->endpoints[i]);
  1476. if (err < 0)
  1477. return err;
  1478. }
  1479. for (j = 0; j < 0x10; ++j) {
  1480. if (endpoints[i].out_cables & (1 << j)) {
  1481. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
  1482. &umidi->endpoints[i].out->ports[j].substream);
  1483. ++out_ports;
  1484. }
  1485. if (endpoints[i].in_cables & (1 << j)) {
  1486. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
  1487. &umidi->endpoints[i].in->ports[j].substream);
  1488. ++in_ports;
  1489. }
  1490. }
  1491. }
  1492. snd_printdd(KERN_INFO "created %d output and %d input ports\n",
  1493. out_ports, in_ports);
  1494. return 0;
  1495. }
  1496. /*
  1497. * Returns MIDIStreaming device capabilities.
  1498. */
  1499. static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
  1500. struct snd_usb_midi_endpoint_info* endpoints)
  1501. {
  1502. struct usb_interface* intf;
  1503. struct usb_host_interface *hostif;
  1504. struct usb_interface_descriptor* intfd;
  1505. struct usb_ms_header_descriptor* ms_header;
  1506. struct usb_host_endpoint *hostep;
  1507. struct usb_endpoint_descriptor* ep;
  1508. struct usb_ms_endpoint_descriptor* ms_ep;
  1509. int i, epidx;
  1510. intf = umidi->iface;
  1511. if (!intf)
  1512. return -ENXIO;
  1513. hostif = &intf->altsetting[0];
  1514. intfd = get_iface_desc(hostif);
  1515. ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
  1516. if (hostif->extralen >= 7 &&
  1517. ms_header->bLength >= 7 &&
  1518. ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
  1519. ms_header->bDescriptorSubtype == UAC_HEADER)
  1520. snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
  1521. ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
  1522. else
  1523. snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
  1524. epidx = 0;
  1525. for (i = 0; i < intfd->bNumEndpoints; ++i) {
  1526. hostep = &hostif->endpoint[i];
  1527. ep = get_ep_desc(hostep);
  1528. if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
  1529. continue;
  1530. ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
  1531. if (hostep->extralen < 4 ||
  1532. ms_ep->bLength < 4 ||
  1533. ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
  1534. ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
  1535. continue;
  1536. if (usb_endpoint_dir_out(ep)) {
  1537. if (endpoints[epidx].out_ep) {
  1538. if (++epidx >= MIDI_MAX_ENDPOINTS) {
  1539. snd_printk(KERN_WARNING "too many endpoints\n");
  1540. break;
  1541. }
  1542. }
  1543. endpoints[epidx].out_ep = usb_endpoint_num(ep);
  1544. if (usb_endpoint_xfer_int(ep))
  1545. endpoints[epidx].out_interval = ep->bInterval;
  1546. else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
  1547. /*
  1548. * Low speed bulk transfers don't exist, so
  1549. * force interrupt transfers for devices like
  1550. * ESI MIDI Mate that try to use them anyway.
  1551. */
  1552. endpoints[epidx].out_interval = 1;
  1553. endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
  1554. snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
  1555. ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
  1556. } else {
  1557. if (endpoints[epidx].in_ep) {
  1558. if (++epidx >= MIDI_MAX_ENDPOINTS) {
  1559. snd_printk(KERN_WARNING "too many endpoints\n");
  1560. break;
  1561. }
  1562. }
  1563. endpoints[epidx].in_ep = usb_endpoint_num(ep);
  1564. if (usb_endpoint_xfer_int(ep))
  1565. endpoints[epidx].in_interval = ep->bInterval;
  1566. else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
  1567. endpoints[epidx].in_interval = 1;
  1568. endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
  1569. snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
  1570. ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
  1571. }
  1572. }
  1573. return 0;
  1574. }
  1575. static int roland_load_info(struct snd_kcontrol *kcontrol,
  1576. struct snd_ctl_elem_info *info)
  1577. {
  1578. static const char *const names[] = { "High Load", "Light Load" };
  1579. return snd_ctl_enum_info(info, 1, 2, names);
  1580. }
  1581. static int roland_load_get(struct snd_kcontrol *kcontrol,
  1582. struct snd_ctl_elem_value *value)
  1583. {
  1584. value->value.enumerated.item[0] = kcontrol->private_value;
  1585. return 0;
  1586. }
  1587. static int roland_load_put(struct snd_kcontrol *kcontrol,
  1588. struct snd_ctl_elem_value *value)
  1589. {
  1590. struct snd_usb_midi* umidi = kcontrol->private_data;
  1591. int changed;
  1592. if (value->value.enumerated.item[0] > 1)
  1593. return -EINVAL;
  1594. mutex_lock(&umidi->mutex);
  1595. changed = value->value.enumerated.item[0] != kcontrol->private_value;
  1596. if (changed)
  1597. kcontrol->private_value = value->value.enumerated.item[0];
  1598. mutex_unlock(&umidi->mutex);
  1599. return changed;
  1600. }
  1601. static struct snd_kcontrol_new roland_load_ctl = {
  1602. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1603. .name = "MIDI Input Mode",
  1604. .info = roland_load_info,
  1605. .get = roland_load_get,
  1606. .put = roland_load_put,
  1607. .private_value = 1,
  1608. };
  1609. /*
  1610. * On Roland devices, use the second alternate setting to be able to use
  1611. * the interrupt input endpoint.
  1612. */
  1613. static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
  1614. {
  1615. struct usb_interface* intf;
  1616. struct usb_host_interface *hostif;
  1617. struct usb_interface_descriptor* intfd;
  1618. intf = umidi->iface;
  1619. if (!intf || intf->num_altsetting != 2)
  1620. return;
  1621. hostif = &intf->altsetting[1];
  1622. intfd = get_iface_desc(hostif);
  1623. if (intfd->bNumEndpoints != 2 ||
  1624. (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
  1625. (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  1626. return;
  1627. snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
  1628. intfd->bAlternateSetting);
  1629. usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
  1630. intfd->bAlternateSetting);
  1631. umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
  1632. if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
  1633. umidi->roland_load_ctl = NULL;
  1634. }
  1635. /*
  1636. * Try to find any usable endpoints in the interface.
  1637. */
  1638. static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
  1639. struct snd_usb_midi_endpoint_info* endpoint,
  1640. int max_endpoints)
  1641. {
  1642. struct usb_interface* intf;
  1643. struct usb_host_interface *hostif;
  1644. struct usb_interface_descriptor* intfd;
  1645. struct usb_endpoint_descriptor* epd;
  1646. int i, out_eps = 0, in_eps = 0;
  1647. if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
  1648. snd_usbmidi_switch_roland_altsetting(umidi);
  1649. if (endpoint[0].out_ep || endpoint[0].in_ep)
  1650. return 0;
  1651. intf = umidi->iface;
  1652. if (!intf || intf->num_altsetting < 1)
  1653. return -ENOENT;
  1654. hostif = intf->cur_altsetting;
  1655. intfd = get_iface_desc(hostif);
  1656. for (i = 0; i < intfd->bNumEndpoints; ++i) {
  1657. epd = get_endpoint(hostif, i);
  1658. if (!usb_endpoint_xfer_bulk(epd) &&
  1659. !usb_endpoint_xfer_int(epd))
  1660. continue;
  1661. if (out_eps < max_endpoints &&
  1662. usb_endpoint_dir_out(epd)) {
  1663. endpoint[out_eps].out_ep = usb_endpoint_num(epd);
  1664. if (usb_endpoint_xfer_int(epd))
  1665. endpoint[out_eps].out_interval = epd->bInterval;
  1666. ++out_eps;
  1667. }
  1668. if (in_eps < max_endpoints &&
  1669. usb_endpoint_dir_in(epd)) {
  1670. endpoint[in_eps].in_ep = usb_endpoint_num(epd);
  1671. if (usb_endpoint_xfer_int(epd))
  1672. endpoint[in_eps].in_interval = epd->bInterval;
  1673. ++in_eps;
  1674. }
  1675. }
  1676. return (out_eps || in_eps) ? 0 : -ENOENT;
  1677. }
  1678. /*
  1679. * Detects the endpoints for one-port-per-endpoint protocols.
  1680. */
  1681. static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
  1682. struct snd_usb_midi_endpoint_info* endpoints)
  1683. {
  1684. int err, i;
  1685. err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
  1686. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1687. if (endpoints[i].out_ep)
  1688. endpoints[i].out_cables = 0x0001;
  1689. if (endpoints[i].in_ep)
  1690. endpoints[i].in_cables = 0x0001;
  1691. }
  1692. return err;
  1693. }
  1694. /*
  1695. * Detects the endpoints and ports of Yamaha devices.
  1696. */
  1697. static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
  1698. struct snd_usb_midi_endpoint_info* endpoint)
  1699. {
  1700. struct usb_interface* intf;
  1701. struct usb_host_interface *hostif;
  1702. struct usb_interface_descriptor* intfd;
  1703. uint8_t* cs_desc;
  1704. intf = umidi->iface;
  1705. if (!intf)
  1706. return -ENOENT;
  1707. hostif = intf->altsetting;
  1708. intfd = get_iface_desc(hostif);
  1709. if (intfd->bNumEndpoints < 1)
  1710. return -ENOENT;
  1711. /*
  1712. * For each port there is one MIDI_IN/OUT_JACK descriptor, not
  1713. * necessarily with any useful contents. So simply count 'em.
  1714. */
  1715. for (cs_desc = hostif->extra;
  1716. cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
  1717. cs_desc += cs_desc[0]) {
  1718. if (cs_desc[1] == USB_DT_CS_INTERFACE) {
  1719. if (cs_desc[2] == UAC_MIDI_IN_JACK)
  1720. endpoint->in_cables = (endpoint->in_cables << 1) | 1;
  1721. else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
  1722. endpoint->out_cables = (endpoint->out_cables << 1) | 1;
  1723. }
  1724. }
  1725. if (!endpoint->in_cables && !endpoint->out_cables)
  1726. return -ENOENT;
  1727. return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
  1728. }
  1729. /*
  1730. * Creates the endpoints and their ports for Midiman devices.
  1731. */
  1732. static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
  1733. struct snd_usb_midi_endpoint_info* endpoint)
  1734. {
  1735. struct snd_usb_midi_endpoint_info ep_info;
  1736. struct usb_interface* intf;
  1737. struct usb_host_interface *hostif;
  1738. struct usb_interface_descriptor* intfd;
  1739. struct usb_endpoint_descriptor* epd;
  1740. int cable, err;
  1741. intf = umidi->iface;
  1742. if (!intf)
  1743. return -ENOENT;
  1744. hostif = intf->altsetting;
  1745. intfd = get_iface_desc(hostif);
  1746. /*
  1747. * The various MidiSport devices have more or less random endpoint
  1748. * numbers, so we have to identify the endpoints by their index in
  1749. * the descriptor array, like the driver for that other OS does.
  1750. *
  1751. * There is one interrupt input endpoint for all input ports, one
  1752. * bulk output endpoint for even-numbered ports, and one for odd-
  1753. * numbered ports. Both bulk output endpoints have corresponding
  1754. * input bulk endpoints (at indices 1 and 3) which aren't used.
  1755. */
  1756. if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
  1757. snd_printdd(KERN_ERR "not enough endpoints\n");
  1758. return -ENOENT;
  1759. }
  1760. epd = get_endpoint(hostif, 0);
  1761. if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
  1762. snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
  1763. return -ENXIO;
  1764. }
  1765. epd = get_endpoint(hostif, 2);
  1766. if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
  1767. snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
  1768. return -ENXIO;
  1769. }
  1770. if (endpoint->out_cables > 0x0001) {
  1771. epd = get_endpoint(hostif, 4);
  1772. if (!usb_endpoint_dir_out(epd) ||
  1773. !usb_endpoint_xfer_bulk(epd)) {
  1774. snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
  1775. return -ENXIO;
  1776. }
  1777. }
  1778. ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1779. ep_info.out_interval = 0;
  1780. ep_info.out_cables = endpoint->out_cables & 0x5555;
  1781. err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
  1782. if (err < 0)
  1783. return err;
  1784. ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1785. ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
  1786. ep_info.in_cables = endpoint->in_cables;
  1787. err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
  1788. if (err < 0)
  1789. return err;
  1790. if (endpoint->out_cables > 0x0001) {
  1791. ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1792. ep_info.out_cables = endpoint->out_cables & 0xaaaa;
  1793. err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
  1794. if (err < 0)
  1795. return err;
  1796. }
  1797. for (cable = 0; cable < 0x10; ++cable) {
  1798. if (endpoint->out_cables & (1 << cable))
  1799. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
  1800. &umidi->endpoints[cable & 1].out->ports[cable].substream);
  1801. if (endpoint->in_cables & (1 << cable))
  1802. snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
  1803. &umidi->endpoints[0].in->ports[cable].substream);
  1804. }
  1805. return 0;
  1806. }
  1807. static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
  1808. .get_port_info = snd_usbmidi_get_port_info,
  1809. };
  1810. static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
  1811. int out_ports, int in_ports)
  1812. {
  1813. struct snd_rawmidi *rmidi;
  1814. int err;
  1815. err = snd_rawmidi_new(umidi->card, "USB MIDI",
  1816. umidi->next_midi_device++,
  1817. out_ports, in_ports, &rmidi);
  1818. if (err < 0)
  1819. return err;
  1820. strcpy(rmidi->name, umidi->card->shortname);
  1821. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  1822. SNDRV_RAWMIDI_INFO_INPUT |
  1823. SNDRV_RAWMIDI_INFO_DUPLEX;
  1824. rmidi->ops = &snd_usbmidi_ops;
  1825. rmidi->private_data = umidi;
  1826. rmidi->private_free = snd_usbmidi_rawmidi_free;
  1827. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
  1828. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
  1829. umidi->rmidi = rmidi;
  1830. return 0;
  1831. }
  1832. /*
  1833. * Temporarily stop input.
  1834. */
  1835. void snd_usbmidi_input_stop(struct list_head* p)
  1836. {
  1837. struct snd_usb_midi* umidi;
  1838. unsigned int i, j;
  1839. umidi = list_entry(p, struct snd_usb_midi, list);
  1840. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1841. struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
  1842. if (ep->in)
  1843. for (j = 0; j < INPUT_URBS; ++j)
  1844. usb_kill_urb(ep->in->urbs[j]);
  1845. }
  1846. }
  1847. static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
  1848. {
  1849. unsigned int i;
  1850. if (!ep)
  1851. return;
  1852. for (i = 0; i < INPUT_URBS; ++i) {
  1853. struct urb* urb = ep->urbs[i];
  1854. urb->dev = ep->umidi->dev;
  1855. snd_usbmidi_submit_urb(urb, GFP_KERNEL);
  1856. }
  1857. }
  1858. /*
  1859. * Resume input after a call to snd_usbmidi_input_stop().
  1860. */
  1861. void snd_usbmidi_input_start(struct list_head* p)
  1862. {
  1863. struct snd_usb_midi* umidi;
  1864. int i;
  1865. umidi = list_entry(p, struct snd_usb_midi, list);
  1866. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
  1867. snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
  1868. }
  1869. /*
  1870. * Creates and registers everything needed for a MIDI streaming interface.
  1871. */
  1872. int snd_usbmidi_create(struct snd_card *card,
  1873. struct usb_interface* iface,
  1874. struct list_head *midi_list,
  1875. const struct snd_usb_audio_quirk* quirk)
  1876. {
  1877. struct snd_usb_midi* umidi;
  1878. struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
  1879. int out_ports, in_ports;
  1880. int i, err;
  1881. umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
  1882. if (!umidi)
  1883. return -ENOMEM;
  1884. umidi->dev = interface_to_usbdev(iface);
  1885. umidi->card = card;
  1886. umidi->iface = iface;
  1887. umidi->quirk = quirk;
  1888. umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
  1889. init_timer(&umidi->error_timer);
  1890. spin_lock_init(&umidi->disc_lock);
  1891. mutex_init(&umidi->mutex);
  1892. umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
  1893. le16_to_cpu(umidi->dev->descriptor.idProduct));
  1894. umidi->error_timer.function = snd_usbmidi_error_timer;
  1895. umidi->error_timer.data = (unsigned long)umidi;
  1896. /* detect the endpoint(s) to use */
  1897. memset(endpoints, 0, sizeof(endpoints));
  1898. switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
  1899. case QUIRK_MIDI_STANDARD_INTERFACE:
  1900. err = snd_usbmidi_get_ms_info(umidi, endpoints);
  1901. if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
  1902. umidi->usb_protocol_ops =
  1903. &snd_usbmidi_maudio_broken_running_status_ops;
  1904. break;
  1905. case QUIRK_MIDI_US122L:
  1906. umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
  1907. /* fall through */
  1908. case QUIRK_MIDI_FIXED_ENDPOINT:
  1909. memcpy(&endpoints[0], quirk->data,
  1910. sizeof(struct snd_usb_midi_endpoint_info));
  1911. err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
  1912. break;
  1913. case QUIRK_MIDI_YAMAHA:
  1914. err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
  1915. break;
  1916. case QUIRK_MIDI_MIDIMAN:
  1917. umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
  1918. memcpy(&endpoints[0], quirk->data,
  1919. sizeof(struct snd_usb_midi_endpoint_info));
  1920. err = 0;
  1921. break;
  1922. case QUIRK_MIDI_NOVATION:
  1923. umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
  1924. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  1925. break;
  1926. case QUIRK_MIDI_RAW_BYTES:
  1927. umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
  1928. /*
  1929. * Interface 1 contains isochronous endpoints, but with the same
  1930. * numbers as in interface 0. Since it is interface 1 that the
  1931. * USB core has most recently seen, these descriptors are now
  1932. * associated with the endpoint numbers. This will foul up our
  1933. * attempts to submit bulk/interrupt URBs to the endpoints in
  1934. * interface 0, so we have to make sure that the USB core looks
  1935. * again at interface 0 by calling usb_set_interface() on it.
  1936. */
  1937. if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
  1938. usb_set_interface(umidi->dev, 0, 0);
  1939. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  1940. break;
  1941. case QUIRK_MIDI_EMAGIC:
  1942. umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
  1943. memcpy(&endpoints[0], quirk->data,
  1944. sizeof(struct snd_usb_midi_endpoint_info));
  1945. err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
  1946. break;
  1947. case QUIRK_MIDI_CME:
  1948. umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
  1949. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  1950. break;
  1951. case QUIRK_MIDI_AKAI:
  1952. umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
  1953. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  1954. /* endpoint 1 is input-only */
  1955. endpoints[1].out_cables = 0;
  1956. break;
  1957. default:
  1958. snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
  1959. err = -ENXIO;
  1960. break;
  1961. }
  1962. if (err < 0) {
  1963. kfree(umidi);
  1964. return err;
  1965. }
  1966. /* create rawmidi device */
  1967. out_ports = 0;
  1968. in_ports = 0;
  1969. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1970. out_ports += hweight16(endpoints[i].out_cables);
  1971. in_ports += hweight16(endpoints[i].in_cables);
  1972. }
  1973. err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
  1974. if (err < 0) {
  1975. kfree(umidi);
  1976. return err;
  1977. }
  1978. /* create endpoint/port structures */
  1979. if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
  1980. err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
  1981. else
  1982. err = snd_usbmidi_create_endpoints(umidi, endpoints);
  1983. if (err < 0) {
  1984. snd_usbmidi_free(umidi);
  1985. return err;
  1986. }
  1987. list_add_tail(&umidi->list, midi_list);
  1988. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
  1989. snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
  1990. return 0;
  1991. }
  1992. EXPORT_SYMBOL(snd_usbmidi_create);
  1993. EXPORT_SYMBOL(snd_usbmidi_input_stop);
  1994. EXPORT_SYMBOL(snd_usbmidi_input_start);
  1995. EXPORT_SYMBOL(snd_usbmidi_disconnect);