midi.c 74 KB

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