usbmidi.c 54 KB

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