midi.c 67 KB

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