midi.c 68 KB

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