usbmidi.c 57 KB

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