midi.c 64 KB

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