midi.c 72 KB

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