midi.c 65 KB

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