midi.c 62 KB

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