midi.c 58 KB

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