usbmidi.c 58 KB

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