midi.c 63 KB

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