midi.c 63 KB

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