midi.c 75 KB

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