midi.c 63 KB

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