usbmidi.c 58 KB

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