midi.c 63 KB

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