midi.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  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. const 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 const 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 const 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 const
  625. struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
  626. .input = snd_usbmidi_maudio_broken_running_status_input,
  627. .output = snd_usbmidi_standard_output,
  628. .output_packet = snd_usbmidi_output_standard_packet,
  629. };
  630. static const struct usb_protocol_ops snd_usbmidi_cme_ops = {
  631. .input = snd_usbmidi_cme_input,
  632. .output = snd_usbmidi_standard_output,
  633. .output_packet = snd_usbmidi_output_standard_packet,
  634. };
  635. static const struct usb_protocol_ops snd_usbmidi_ch345_broken_sysex_ops = {
  636. .input = ch345_broken_sysex_input,
  637. .output = snd_usbmidi_standard_output,
  638. .output_packet = snd_usbmidi_output_standard_packet,
  639. };
  640. /*
  641. * AKAI MPD16 protocol:
  642. *
  643. * For control port (endpoint 1):
  644. * ==============================
  645. * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
  646. * SysEx message (msg_len=9 bytes long).
  647. *
  648. * For data port (endpoint 2):
  649. * ===========================
  650. * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
  651. * MIDI message (msg_len bytes long)
  652. *
  653. * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
  654. */
  655. static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
  656. uint8_t *buffer, int buffer_length)
  657. {
  658. unsigned int pos = 0;
  659. unsigned int len = (unsigned int)buffer_length;
  660. while (pos < len) {
  661. unsigned int port = (buffer[pos] >> 4) - 1;
  662. unsigned int msg_len = buffer[pos] & 0x0f;
  663. pos++;
  664. if (pos + msg_len <= len && port < 2)
  665. snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
  666. pos += msg_len;
  667. }
  668. }
  669. #define MAX_AKAI_SYSEX_LEN 9
  670. static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
  671. struct urb *urb)
  672. {
  673. uint8_t *msg;
  674. int pos, end, count, buf_end;
  675. uint8_t tmp[MAX_AKAI_SYSEX_LEN];
  676. struct snd_rawmidi_substream *substream = ep->ports[0].substream;
  677. if (!ep->ports[0].active)
  678. return;
  679. msg = urb->transfer_buffer + urb->transfer_buffer_length;
  680. buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
  681. /* only try adding more data when there's space for at least 1 SysEx */
  682. while (urb->transfer_buffer_length < buf_end) {
  683. count = snd_rawmidi_transmit_peek(substream,
  684. tmp, MAX_AKAI_SYSEX_LEN);
  685. if (!count) {
  686. ep->ports[0].active = 0;
  687. return;
  688. }
  689. /* try to skip non-SysEx data */
  690. for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
  691. ;
  692. if (pos > 0) {
  693. snd_rawmidi_transmit_ack(substream, pos);
  694. continue;
  695. }
  696. /* look for the start or end marker */
  697. for (end = 1; end < count && tmp[end] < 0xF0; end++)
  698. ;
  699. /* next SysEx started before the end of current one */
  700. if (end < count && tmp[end] == 0xF0) {
  701. /* it's incomplete - drop it */
  702. snd_rawmidi_transmit_ack(substream, end);
  703. continue;
  704. }
  705. /* SysEx complete */
  706. if (end < count && tmp[end] == 0xF7) {
  707. /* queue it, ack it, and get the next one */
  708. count = end + 1;
  709. msg[0] = 0x10 | count;
  710. memcpy(&msg[1], tmp, count);
  711. snd_rawmidi_transmit_ack(substream, count);
  712. urb->transfer_buffer_length += count + 1;
  713. msg += count + 1;
  714. continue;
  715. }
  716. /* less than 9 bytes and no end byte - wait for more */
  717. if (count < MAX_AKAI_SYSEX_LEN) {
  718. ep->ports[0].active = 0;
  719. return;
  720. }
  721. /* 9 bytes and no end marker in sight - malformed, skip it */
  722. snd_rawmidi_transmit_ack(substream, count);
  723. }
  724. }
  725. static const struct usb_protocol_ops snd_usbmidi_akai_ops = {
  726. .input = snd_usbmidi_akai_input,
  727. .output = snd_usbmidi_akai_output,
  728. };
  729. /*
  730. * Novation USB MIDI protocol: number of data bytes is in the first byte
  731. * (when receiving) (+1!) or in the second byte (when sending); data begins
  732. * at the third byte.
  733. */
  734. static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint *ep,
  735. uint8_t *buffer, int buffer_length)
  736. {
  737. if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
  738. return;
  739. snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
  740. }
  741. static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint *ep,
  742. struct urb *urb)
  743. {
  744. uint8_t *transfer_buffer;
  745. int count;
  746. if (!ep->ports[0].active)
  747. return;
  748. transfer_buffer = urb->transfer_buffer;
  749. count = snd_rawmidi_transmit(ep->ports[0].substream,
  750. &transfer_buffer[2],
  751. ep->max_transfer - 2);
  752. if (count < 1) {
  753. ep->ports[0].active = 0;
  754. return;
  755. }
  756. transfer_buffer[0] = 0;
  757. transfer_buffer[1] = count;
  758. urb->transfer_buffer_length = 2 + count;
  759. }
  760. static const struct usb_protocol_ops snd_usbmidi_novation_ops = {
  761. .input = snd_usbmidi_novation_input,
  762. .output = snd_usbmidi_novation_output,
  763. };
  764. /*
  765. * "raw" protocol: just move raw MIDI bytes from/to the endpoint
  766. */
  767. static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint *ep,
  768. uint8_t *buffer, int buffer_length)
  769. {
  770. snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
  771. }
  772. static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint *ep,
  773. struct urb *urb)
  774. {
  775. int count;
  776. if (!ep->ports[0].active)
  777. return;
  778. count = snd_rawmidi_transmit(ep->ports[0].substream,
  779. urb->transfer_buffer,
  780. ep->max_transfer);
  781. if (count < 1) {
  782. ep->ports[0].active = 0;
  783. return;
  784. }
  785. urb->transfer_buffer_length = count;
  786. }
  787. static const struct usb_protocol_ops snd_usbmidi_raw_ops = {
  788. .input = snd_usbmidi_raw_input,
  789. .output = snd_usbmidi_raw_output,
  790. };
  791. /*
  792. * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
  793. */
  794. static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint *ep,
  795. uint8_t *buffer, int buffer_length)
  796. {
  797. if (buffer_length > 2)
  798. snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
  799. }
  800. static const struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
  801. .input = snd_usbmidi_ftdi_input,
  802. .output = snd_usbmidi_raw_output,
  803. };
  804. static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
  805. uint8_t *buffer, int buffer_length)
  806. {
  807. if (buffer_length != 9)
  808. return;
  809. buffer_length = 8;
  810. while (buffer_length && buffer[buffer_length - 1] == 0xFD)
  811. buffer_length--;
  812. if (buffer_length)
  813. snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
  814. }
  815. static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
  816. struct urb *urb)
  817. {
  818. int count;
  819. if (!ep->ports[0].active)
  820. return;
  821. switch (snd_usb_get_speed(ep->umidi->dev)) {
  822. case USB_SPEED_HIGH:
  823. case USB_SPEED_SUPER:
  824. case USB_SPEED_SUPER_PLUS:
  825. count = 1;
  826. break;
  827. default:
  828. count = 2;
  829. }
  830. count = snd_rawmidi_transmit(ep->ports[0].substream,
  831. urb->transfer_buffer,
  832. count);
  833. if (count < 1) {
  834. ep->ports[0].active = 0;
  835. return;
  836. }
  837. memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
  838. urb->transfer_buffer_length = ep->max_transfer;
  839. }
  840. static const struct usb_protocol_ops snd_usbmidi_122l_ops = {
  841. .input = snd_usbmidi_us122l_input,
  842. .output = snd_usbmidi_us122l_output,
  843. };
  844. /*
  845. * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
  846. */
  847. static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint *ep)
  848. {
  849. static const u8 init_data[] = {
  850. /* initialization magic: "get version" */
  851. 0xf0,
  852. 0x00, 0x20, 0x31, /* Emagic */
  853. 0x64, /* Unitor8 */
  854. 0x0b, /* version number request */
  855. 0x00, /* command version */
  856. 0x00, /* EEPROM, box 0 */
  857. 0xf7
  858. };
  859. send_bulk_static_data(ep, init_data, sizeof(init_data));
  860. /* while we're at it, pour on more magic */
  861. send_bulk_static_data(ep, init_data, sizeof(init_data));
  862. }
  863. static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint *ep)
  864. {
  865. static const u8 finish_data[] = {
  866. /* switch to patch mode with last preset */
  867. 0xf0,
  868. 0x00, 0x20, 0x31, /* Emagic */
  869. 0x64, /* Unitor8 */
  870. 0x10, /* patch switch command */
  871. 0x00, /* command version */
  872. 0x7f, /* to all boxes */
  873. 0x40, /* last preset in EEPROM */
  874. 0xf7
  875. };
  876. send_bulk_static_data(ep, finish_data, sizeof(finish_data));
  877. }
  878. static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint *ep,
  879. uint8_t *buffer, int buffer_length)
  880. {
  881. int i;
  882. /* FF indicates end of valid data */
  883. for (i = 0; i < buffer_length; ++i)
  884. if (buffer[i] == 0xff) {
  885. buffer_length = i;
  886. break;
  887. }
  888. /* handle F5 at end of last buffer */
  889. if (ep->seen_f5)
  890. goto switch_port;
  891. while (buffer_length > 0) {
  892. /* determine size of data until next F5 */
  893. for (i = 0; i < buffer_length; ++i)
  894. if (buffer[i] == 0xf5)
  895. break;
  896. snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
  897. buffer += i;
  898. buffer_length -= i;
  899. if (buffer_length <= 0)
  900. break;
  901. /* assert(buffer[0] == 0xf5); */
  902. ep->seen_f5 = 1;
  903. ++buffer;
  904. --buffer_length;
  905. switch_port:
  906. if (buffer_length <= 0)
  907. break;
  908. if (buffer[0] < 0x80) {
  909. ep->current_port = (buffer[0] - 1) & 15;
  910. ++buffer;
  911. --buffer_length;
  912. }
  913. ep->seen_f5 = 0;
  914. }
  915. }
  916. static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint *ep,
  917. struct urb *urb)
  918. {
  919. int port0 = ep->current_port;
  920. uint8_t *buf = urb->transfer_buffer;
  921. int buf_free = ep->max_transfer;
  922. int length, i;
  923. for (i = 0; i < 0x10; ++i) {
  924. /* round-robin, starting at the last current port */
  925. int portnum = (port0 + i) & 15;
  926. struct usbmidi_out_port *port = &ep->ports[portnum];
  927. if (!port->active)
  928. continue;
  929. if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
  930. port->active = 0;
  931. continue;
  932. }
  933. if (portnum != ep->current_port) {
  934. if (buf_free < 2)
  935. break;
  936. ep->current_port = portnum;
  937. buf[0] = 0xf5;
  938. buf[1] = (portnum + 1) & 15;
  939. buf += 2;
  940. buf_free -= 2;
  941. }
  942. if (buf_free < 1)
  943. break;
  944. length = snd_rawmidi_transmit(port->substream, buf, buf_free);
  945. if (length > 0) {
  946. buf += length;
  947. buf_free -= length;
  948. if (buf_free < 1)
  949. break;
  950. }
  951. }
  952. if (buf_free < ep->max_transfer && buf_free > 0) {
  953. *buf = 0xff;
  954. --buf_free;
  955. }
  956. urb->transfer_buffer_length = ep->max_transfer - buf_free;
  957. }
  958. static const struct usb_protocol_ops snd_usbmidi_emagic_ops = {
  959. .input = snd_usbmidi_emagic_input,
  960. .output = snd_usbmidi_emagic_output,
  961. .init_out_endpoint = snd_usbmidi_emagic_init_out,
  962. .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
  963. };
  964. static void update_roland_altsetting(struct snd_usb_midi *umidi)
  965. {
  966. struct usb_interface *intf;
  967. struct usb_host_interface *hostif;
  968. struct usb_interface_descriptor *intfd;
  969. int is_light_load;
  970. intf = umidi->iface;
  971. is_light_load = intf->cur_altsetting != intf->altsetting;
  972. if (umidi->roland_load_ctl->private_value == is_light_load)
  973. return;
  974. hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
  975. intfd = get_iface_desc(hostif);
  976. snd_usbmidi_input_stop(&umidi->list);
  977. usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
  978. intfd->bAlternateSetting);
  979. snd_usbmidi_input_start(&umidi->list);
  980. }
  981. static int substream_open(struct snd_rawmidi_substream *substream, int dir,
  982. int open)
  983. {
  984. struct snd_usb_midi *umidi = substream->rmidi->private_data;
  985. struct snd_kcontrol *ctl;
  986. down_read(&umidi->disc_rwsem);
  987. if (umidi->disconnected) {
  988. up_read(&umidi->disc_rwsem);
  989. return open ? -ENODEV : 0;
  990. }
  991. mutex_lock(&umidi->mutex);
  992. if (open) {
  993. if (!umidi->opened[0] && !umidi->opened[1]) {
  994. if (umidi->roland_load_ctl) {
  995. ctl = umidi->roland_load_ctl;
  996. ctl->vd[0].access |=
  997. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  998. snd_ctl_notify(umidi->card,
  999. SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
  1000. update_roland_altsetting(umidi);
  1001. }
  1002. }
  1003. umidi->opened[dir]++;
  1004. if (umidi->opened[1])
  1005. snd_usbmidi_input_start(&umidi->list);
  1006. } else {
  1007. umidi->opened[dir]--;
  1008. if (!umidi->opened[1])
  1009. snd_usbmidi_input_stop(&umidi->list);
  1010. if (!umidi->opened[0] && !umidi->opened[1]) {
  1011. if (umidi->roland_load_ctl) {
  1012. ctl = umidi->roland_load_ctl;
  1013. ctl->vd[0].access &=
  1014. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1015. snd_ctl_notify(umidi->card,
  1016. SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
  1017. }
  1018. }
  1019. }
  1020. mutex_unlock(&umidi->mutex);
  1021. up_read(&umidi->disc_rwsem);
  1022. return 0;
  1023. }
  1024. static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
  1025. {
  1026. struct snd_usb_midi *umidi = substream->rmidi->private_data;
  1027. struct usbmidi_out_port *port = NULL;
  1028. int i, j;
  1029. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
  1030. if (umidi->endpoints[i].out)
  1031. for (j = 0; j < 0x10; ++j)
  1032. if (umidi->endpoints[i].out->ports[j].substream == substream) {
  1033. port = &umidi->endpoints[i].out->ports[j];
  1034. break;
  1035. }
  1036. if (!port) {
  1037. snd_BUG();
  1038. return -ENXIO;
  1039. }
  1040. substream->runtime->private_data = port;
  1041. port->state = STATE_UNKNOWN;
  1042. return substream_open(substream, 0, 1);
  1043. }
  1044. static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
  1045. {
  1046. return substream_open(substream, 0, 0);
  1047. }
  1048. static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream,
  1049. int up)
  1050. {
  1051. struct usbmidi_out_port *port =
  1052. (struct usbmidi_out_port *)substream->runtime->private_data;
  1053. port->active = up;
  1054. if (up) {
  1055. if (port->ep->umidi->disconnected) {
  1056. /* gobble up remaining bytes to prevent wait in
  1057. * snd_rawmidi_drain_output */
  1058. while (!snd_rawmidi_transmit_empty(substream))
  1059. snd_rawmidi_transmit_ack(substream, 1);
  1060. return;
  1061. }
  1062. tasklet_schedule(&port->ep->tasklet);
  1063. }
  1064. }
  1065. static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
  1066. {
  1067. struct usbmidi_out_port *port = substream->runtime->private_data;
  1068. struct snd_usb_midi_out_endpoint *ep = port->ep;
  1069. unsigned int drain_urbs;
  1070. DEFINE_WAIT(wait);
  1071. long timeout = msecs_to_jiffies(50);
  1072. if (ep->umidi->disconnected)
  1073. return;
  1074. /*
  1075. * The substream buffer is empty, but some data might still be in the
  1076. * currently active URBs, so we have to wait for those to complete.
  1077. */
  1078. spin_lock_irq(&ep->buffer_lock);
  1079. drain_urbs = ep->active_urbs;
  1080. if (drain_urbs) {
  1081. ep->drain_urbs |= drain_urbs;
  1082. do {
  1083. prepare_to_wait(&ep->drain_wait, &wait,
  1084. TASK_UNINTERRUPTIBLE);
  1085. spin_unlock_irq(&ep->buffer_lock);
  1086. timeout = schedule_timeout(timeout);
  1087. spin_lock_irq(&ep->buffer_lock);
  1088. drain_urbs &= ep->drain_urbs;
  1089. } while (drain_urbs && timeout);
  1090. finish_wait(&ep->drain_wait, &wait);
  1091. }
  1092. spin_unlock_irq(&ep->buffer_lock);
  1093. }
  1094. static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
  1095. {
  1096. return substream_open(substream, 1, 1);
  1097. }
  1098. static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
  1099. {
  1100. return substream_open(substream, 1, 0);
  1101. }
  1102. static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream,
  1103. int up)
  1104. {
  1105. struct snd_usb_midi *umidi = substream->rmidi->private_data;
  1106. if (up)
  1107. set_bit(substream->number, &umidi->input_triggered);
  1108. else
  1109. clear_bit(substream->number, &umidi->input_triggered);
  1110. }
  1111. static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
  1112. .open = snd_usbmidi_output_open,
  1113. .close = snd_usbmidi_output_close,
  1114. .trigger = snd_usbmidi_output_trigger,
  1115. .drain = snd_usbmidi_output_drain,
  1116. };
  1117. static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
  1118. .open = snd_usbmidi_input_open,
  1119. .close = snd_usbmidi_input_close,
  1120. .trigger = snd_usbmidi_input_trigger
  1121. };
  1122. static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
  1123. unsigned int buffer_length)
  1124. {
  1125. usb_free_coherent(umidi->dev, buffer_length,
  1126. urb->transfer_buffer, urb->transfer_dma);
  1127. usb_free_urb(urb);
  1128. }
  1129. /*
  1130. * Frees an input endpoint.
  1131. * May be called when ep hasn't been initialized completely.
  1132. */
  1133. static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint *ep)
  1134. {
  1135. unsigned int i;
  1136. for (i = 0; i < INPUT_URBS; ++i)
  1137. if (ep->urbs[i])
  1138. free_urb_and_buffer(ep->umidi, ep->urbs[i],
  1139. ep->urbs[i]->transfer_buffer_length);
  1140. kfree(ep);
  1141. }
  1142. /*
  1143. * Creates an input endpoint.
  1144. */
  1145. static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi *umidi,
  1146. struct snd_usb_midi_endpoint_info *ep_info,
  1147. struct snd_usb_midi_endpoint *rep)
  1148. {
  1149. struct snd_usb_midi_in_endpoint *ep;
  1150. void *buffer;
  1151. unsigned int pipe;
  1152. int length;
  1153. unsigned int i;
  1154. rep->in = NULL;
  1155. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1156. if (!ep)
  1157. return -ENOMEM;
  1158. ep->umidi = umidi;
  1159. for (i = 0; i < INPUT_URBS; ++i) {
  1160. ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
  1161. if (!ep->urbs[i]) {
  1162. snd_usbmidi_in_endpoint_delete(ep);
  1163. return -ENOMEM;
  1164. }
  1165. }
  1166. if (ep_info->in_interval)
  1167. pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
  1168. else
  1169. pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
  1170. length = usb_maxpacket(umidi->dev, pipe, 0);
  1171. for (i = 0; i < INPUT_URBS; ++i) {
  1172. buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
  1173. &ep->urbs[i]->transfer_dma);
  1174. if (!buffer) {
  1175. snd_usbmidi_in_endpoint_delete(ep);
  1176. return -ENOMEM;
  1177. }
  1178. if (ep_info->in_interval)
  1179. usb_fill_int_urb(ep->urbs[i], umidi->dev,
  1180. pipe, buffer, length,
  1181. snd_usbmidi_in_urb_complete,
  1182. ep, ep_info->in_interval);
  1183. else
  1184. usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
  1185. pipe, buffer, length,
  1186. snd_usbmidi_in_urb_complete, ep);
  1187. ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1188. }
  1189. rep->in = ep;
  1190. return 0;
  1191. }
  1192. /*
  1193. * Frees an output endpoint.
  1194. * May be called when ep hasn't been initialized completely.
  1195. */
  1196. static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
  1197. {
  1198. unsigned int i;
  1199. for (i = 0; i < OUTPUT_URBS; ++i)
  1200. if (ep->urbs[i].urb) {
  1201. free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
  1202. ep->max_transfer);
  1203. ep->urbs[i].urb = NULL;
  1204. }
  1205. }
  1206. static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
  1207. {
  1208. snd_usbmidi_out_endpoint_clear(ep);
  1209. kfree(ep);
  1210. }
  1211. /*
  1212. * Creates an output endpoint, and initializes output ports.
  1213. */
  1214. static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi *umidi,
  1215. struct snd_usb_midi_endpoint_info *ep_info,
  1216. struct snd_usb_midi_endpoint *rep)
  1217. {
  1218. struct snd_usb_midi_out_endpoint *ep;
  1219. unsigned int i;
  1220. unsigned int pipe;
  1221. void *buffer;
  1222. rep->out = NULL;
  1223. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1224. if (!ep)
  1225. return -ENOMEM;
  1226. ep->umidi = umidi;
  1227. for (i = 0; i < OUTPUT_URBS; ++i) {
  1228. ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
  1229. if (!ep->urbs[i].urb) {
  1230. snd_usbmidi_out_endpoint_delete(ep);
  1231. return -ENOMEM;
  1232. }
  1233. ep->urbs[i].ep = ep;
  1234. }
  1235. if (ep_info->out_interval)
  1236. pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
  1237. else
  1238. pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
  1239. switch (umidi->usb_id) {
  1240. default:
  1241. ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
  1242. break;
  1243. /*
  1244. * Various chips declare a packet size larger than 4 bytes, but
  1245. * do not actually work with larger packets:
  1246. */
  1247. case USB_ID(0x0a67, 0x5011): /* Medeli DD305 */
  1248. case USB_ID(0x0a92, 0x1020): /* ESI M4U */
  1249. case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
  1250. case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
  1251. case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
  1252. case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
  1253. case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
  1254. ep->max_transfer = 4;
  1255. break;
  1256. /*
  1257. * Some devices only work with 9 bytes packet size:
  1258. */
  1259. case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
  1260. case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
  1261. ep->max_transfer = 9;
  1262. break;
  1263. }
  1264. for (i = 0; i < OUTPUT_URBS; ++i) {
  1265. buffer = usb_alloc_coherent(umidi->dev,
  1266. ep->max_transfer, GFP_KERNEL,
  1267. &ep->urbs[i].urb->transfer_dma);
  1268. if (!buffer) {
  1269. snd_usbmidi_out_endpoint_delete(ep);
  1270. return -ENOMEM;
  1271. }
  1272. if (ep_info->out_interval)
  1273. usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
  1274. pipe, buffer, ep->max_transfer,
  1275. snd_usbmidi_out_urb_complete,
  1276. &ep->urbs[i], ep_info->out_interval);
  1277. else
  1278. usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
  1279. pipe, buffer, ep->max_transfer,
  1280. snd_usbmidi_out_urb_complete,
  1281. &ep->urbs[i]);
  1282. ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1283. }
  1284. spin_lock_init(&ep->buffer_lock);
  1285. tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
  1286. init_waitqueue_head(&ep->drain_wait);
  1287. for (i = 0; i < 0x10; ++i)
  1288. if (ep_info->out_cables & (1 << i)) {
  1289. ep->ports[i].ep = ep;
  1290. ep->ports[i].cable = i << 4;
  1291. }
  1292. if (umidi->usb_protocol_ops->init_out_endpoint)
  1293. umidi->usb_protocol_ops->init_out_endpoint(ep);
  1294. rep->out = ep;
  1295. return 0;
  1296. }
  1297. /*
  1298. * Frees everything.
  1299. */
  1300. static void snd_usbmidi_free(struct snd_usb_midi *umidi)
  1301. {
  1302. int i;
  1303. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1304. struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
  1305. if (ep->out)
  1306. snd_usbmidi_out_endpoint_delete(ep->out);
  1307. if (ep->in)
  1308. snd_usbmidi_in_endpoint_delete(ep->in);
  1309. }
  1310. mutex_destroy(&umidi->mutex);
  1311. kfree(umidi);
  1312. }
  1313. /*
  1314. * Unlinks all URBs (must be done before the usb_device is deleted).
  1315. */
  1316. void snd_usbmidi_disconnect(struct list_head *p)
  1317. {
  1318. struct snd_usb_midi *umidi;
  1319. unsigned int i, j;
  1320. umidi = list_entry(p, struct snd_usb_midi, list);
  1321. /*
  1322. * an URB's completion handler may start the timer and
  1323. * a timer may submit an URB. To reliably break the cycle
  1324. * a flag under lock must be used
  1325. */
  1326. down_write(&umidi->disc_rwsem);
  1327. spin_lock_irq(&umidi->disc_lock);
  1328. umidi->disconnected = 1;
  1329. spin_unlock_irq(&umidi->disc_lock);
  1330. up_write(&umidi->disc_rwsem);
  1331. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1332. struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
  1333. if (ep->out)
  1334. tasklet_kill(&ep->out->tasklet);
  1335. if (ep->out) {
  1336. for (j = 0; j < OUTPUT_URBS; ++j)
  1337. usb_kill_urb(ep->out->urbs[j].urb);
  1338. if (umidi->usb_protocol_ops->finish_out_endpoint)
  1339. umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
  1340. ep->out->active_urbs = 0;
  1341. if (ep->out->drain_urbs) {
  1342. ep->out->drain_urbs = 0;
  1343. wake_up(&ep->out->drain_wait);
  1344. }
  1345. }
  1346. if (ep->in)
  1347. for (j = 0; j < INPUT_URBS; ++j)
  1348. usb_kill_urb(ep->in->urbs[j]);
  1349. /* free endpoints here; later call can result in Oops */
  1350. if (ep->out)
  1351. snd_usbmidi_out_endpoint_clear(ep->out);
  1352. if (ep->in) {
  1353. snd_usbmidi_in_endpoint_delete(ep->in);
  1354. ep->in = NULL;
  1355. }
  1356. }
  1357. del_timer_sync(&umidi->error_timer);
  1358. }
  1359. EXPORT_SYMBOL(snd_usbmidi_disconnect);
  1360. static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
  1361. {
  1362. struct snd_usb_midi *umidi = rmidi->private_data;
  1363. snd_usbmidi_free(umidi);
  1364. }
  1365. static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi *umidi,
  1366. int stream,
  1367. int number)
  1368. {
  1369. struct snd_rawmidi_substream *substream;
  1370. list_for_each_entry(substream, &umidi->rmidi->streams[stream].substreams,
  1371. list) {
  1372. if (substream->number == number)
  1373. return substream;
  1374. }
  1375. return NULL;
  1376. }
  1377. /*
  1378. * This list specifies names for ports that do not fit into the standard
  1379. * "(product) MIDI (n)" schema because they aren't external MIDI ports,
  1380. * such as internal control or synthesizer ports.
  1381. */
  1382. static struct port_info {
  1383. u32 id;
  1384. short int port;
  1385. short int voices;
  1386. const char *name;
  1387. unsigned int seq_flags;
  1388. } snd_usbmidi_port_info[] = {
  1389. #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
  1390. { .id = USB_ID(vendor, product), \
  1391. .port = num, .voices = voices_, \
  1392. .name = name_, .seq_flags = flags }
  1393. #define EXTERNAL_PORT(vendor, product, num, name) \
  1394. PORT_INFO(vendor, product, num, name, 0, \
  1395. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1396. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1397. SNDRV_SEQ_PORT_TYPE_PORT)
  1398. #define CONTROL_PORT(vendor, product, num, name) \
  1399. PORT_INFO(vendor, product, num, name, 0, \
  1400. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1401. SNDRV_SEQ_PORT_TYPE_HARDWARE)
  1402. #define GM_SYNTH_PORT(vendor, product, num, name, voices) \
  1403. PORT_INFO(vendor, product, num, name, voices, \
  1404. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1405. SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
  1406. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1407. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
  1408. #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
  1409. PORT_INFO(vendor, product, num, name, voices, \
  1410. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1411. SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
  1412. SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
  1413. SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
  1414. SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
  1415. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1416. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
  1417. #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
  1418. PORT_INFO(vendor, product, num, name, voices, \
  1419. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
  1420. SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
  1421. SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
  1422. SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
  1423. SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
  1424. SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
  1425. SNDRV_SEQ_PORT_TYPE_HARDWARE | \
  1426. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
  1427. /* Yamaha MOTIF XF */
  1428. GM_SYNTH_PORT(0x0499, 0x105c, 0, "%s Tone Generator", 128),
  1429. CONTROL_PORT(0x0499, 0x105c, 1, "%s Remote Control"),
  1430. EXTERNAL_PORT(0x0499, 0x105c, 2, "%s Thru"),
  1431. CONTROL_PORT(0x0499, 0x105c, 3, "%s Editor"),
  1432. /* Roland UA-100 */
  1433. CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
  1434. /* Roland SC-8850 */
  1435. SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
  1436. SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
  1437. SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
  1438. SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
  1439. EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
  1440. EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
  1441. /* Roland U-8 */
  1442. EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
  1443. CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
  1444. /* Roland SC-8820 */
  1445. SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
  1446. SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
  1447. EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
  1448. /* Roland SK-500 */
  1449. SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
  1450. SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
  1451. EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
  1452. /* Roland SC-D70 */
  1453. SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
  1454. SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
  1455. EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
  1456. /* Edirol UM-880 */
  1457. CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
  1458. /* Edirol SD-90 */
  1459. ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
  1460. ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
  1461. EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
  1462. EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
  1463. /* Edirol UM-550 */
  1464. CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
  1465. /* Edirol SD-20 */
  1466. ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
  1467. ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
  1468. EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
  1469. /* Edirol SD-80 */
  1470. ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
  1471. ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
  1472. EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
  1473. EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
  1474. /* Edirol UA-700 */
  1475. EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
  1476. CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
  1477. /* Roland VariOS */
  1478. EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
  1479. EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
  1480. EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
  1481. /* Edirol PCR */
  1482. EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
  1483. EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
  1484. EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
  1485. /* BOSS GS-10 */
  1486. EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
  1487. CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
  1488. /* Edirol UA-1000 */
  1489. EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
  1490. CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
  1491. /* Edirol UR-80 */
  1492. EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
  1493. EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
  1494. EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
  1495. /* Edirol PCR-A */
  1496. EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
  1497. EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
  1498. EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
  1499. /* BOSS GT-PRO */
  1500. CONTROL_PORT(0x0582, 0x0089, 0, "%s Control"),
  1501. /* Edirol UM-3EX */
  1502. CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
  1503. /* Roland VG-99 */
  1504. CONTROL_PORT(0x0582, 0x00b2, 0, "%s Control"),
  1505. EXTERNAL_PORT(0x0582, 0x00b2, 1, "%s MIDI"),
  1506. /* Cakewalk Sonar V-Studio 100 */
  1507. EXTERNAL_PORT(0x0582, 0x00eb, 0, "%s MIDI"),
  1508. CONTROL_PORT(0x0582, 0x00eb, 1, "%s Control"),
  1509. /* Roland VB-99 */
  1510. CONTROL_PORT(0x0582, 0x0102, 0, "%s Control"),
  1511. EXTERNAL_PORT(0x0582, 0x0102, 1, "%s MIDI"),
  1512. /* Roland A-PRO */
  1513. EXTERNAL_PORT(0x0582, 0x010f, 0, "%s MIDI"),
  1514. CONTROL_PORT(0x0582, 0x010f, 1, "%s 1"),
  1515. CONTROL_PORT(0x0582, 0x010f, 2, "%s 2"),
  1516. /* Roland SD-50 */
  1517. ROLAND_SYNTH_PORT(0x0582, 0x0114, 0, "%s Synth", 128),
  1518. EXTERNAL_PORT(0x0582, 0x0114, 1, "%s MIDI"),
  1519. CONTROL_PORT(0x0582, 0x0114, 2, "%s Control"),
  1520. /* Roland OCTA-CAPTURE */
  1521. EXTERNAL_PORT(0x0582, 0x0120, 0, "%s MIDI"),
  1522. CONTROL_PORT(0x0582, 0x0120, 1, "%s Control"),
  1523. EXTERNAL_PORT(0x0582, 0x0121, 0, "%s MIDI"),
  1524. CONTROL_PORT(0x0582, 0x0121, 1, "%s Control"),
  1525. /* Roland SPD-SX */
  1526. CONTROL_PORT(0x0582, 0x0145, 0, "%s Control"),
  1527. EXTERNAL_PORT(0x0582, 0x0145, 1, "%s MIDI"),
  1528. /* Roland A-Series */
  1529. CONTROL_PORT(0x0582, 0x0156, 0, "%s Keyboard"),
  1530. EXTERNAL_PORT(0x0582, 0x0156, 1, "%s MIDI"),
  1531. /* Roland INTEGRA-7 */
  1532. ROLAND_SYNTH_PORT(0x0582, 0x015b, 0, "%s Synth", 128),
  1533. CONTROL_PORT(0x0582, 0x015b, 1, "%s Control"),
  1534. /* M-Audio MidiSport 8x8 */
  1535. CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
  1536. CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
  1537. /* MOTU Fastlane */
  1538. EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
  1539. EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
  1540. /* Emagic Unitor8/AMT8/MT4 */
  1541. EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
  1542. EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
  1543. EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
  1544. /* Akai MPD16 */
  1545. CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
  1546. PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
  1547. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  1548. SNDRV_SEQ_PORT_TYPE_HARDWARE),
  1549. /* Access Music Virus TI */
  1550. EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
  1551. PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
  1552. SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
  1553. SNDRV_SEQ_PORT_TYPE_HARDWARE |
  1554. SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
  1555. };
  1556. static struct port_info *find_port_info(struct snd_usb_midi *umidi, int number)
  1557. {
  1558. int i;
  1559. for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
  1560. if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
  1561. snd_usbmidi_port_info[i].port == number)
  1562. return &snd_usbmidi_port_info[i];
  1563. }
  1564. return NULL;
  1565. }
  1566. static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
  1567. struct snd_seq_port_info *seq_port_info)
  1568. {
  1569. struct snd_usb_midi *umidi = rmidi->private_data;
  1570. struct port_info *port_info;
  1571. /* TODO: read port flags from descriptors */
  1572. port_info = find_port_info(umidi, number);
  1573. if (port_info) {
  1574. seq_port_info->type = port_info->seq_flags;
  1575. seq_port_info->midi_voices = port_info->voices;
  1576. }
  1577. }
  1578. static void snd_usbmidi_init_substream(struct snd_usb_midi *umidi,
  1579. int stream, int number,
  1580. struct snd_rawmidi_substream **rsubstream)
  1581. {
  1582. struct port_info *port_info;
  1583. const char *name_format;
  1584. struct snd_rawmidi_substream *substream =
  1585. snd_usbmidi_find_substream(umidi, stream, number);
  1586. if (!substream) {
  1587. dev_err(&umidi->dev->dev, "substream %d:%d not found\n", stream,
  1588. number);
  1589. return;
  1590. }
  1591. /* TODO: read port name from jack descriptor */
  1592. port_info = find_port_info(umidi, number);
  1593. name_format = port_info ? port_info->name : "%s MIDI %d";
  1594. snprintf(substream->name, sizeof(substream->name),
  1595. name_format, umidi->card->shortname, number + 1);
  1596. *rsubstream = substream;
  1597. }
  1598. /*
  1599. * Creates the endpoints and their ports.
  1600. */
  1601. static int snd_usbmidi_create_endpoints(struct snd_usb_midi *umidi,
  1602. struct snd_usb_midi_endpoint_info *endpoints)
  1603. {
  1604. int i, j, err;
  1605. int out_ports = 0, in_ports = 0;
  1606. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1607. if (endpoints[i].out_cables) {
  1608. err = snd_usbmidi_out_endpoint_create(umidi,
  1609. &endpoints[i],
  1610. &umidi->endpoints[i]);
  1611. if (err < 0)
  1612. return err;
  1613. }
  1614. if (endpoints[i].in_cables) {
  1615. err = snd_usbmidi_in_endpoint_create(umidi,
  1616. &endpoints[i],
  1617. &umidi->endpoints[i]);
  1618. if (err < 0)
  1619. return err;
  1620. }
  1621. for (j = 0; j < 0x10; ++j) {
  1622. if (endpoints[i].out_cables & (1 << j)) {
  1623. snd_usbmidi_init_substream(umidi,
  1624. SNDRV_RAWMIDI_STREAM_OUTPUT,
  1625. out_ports,
  1626. &umidi->endpoints[i].out->ports[j].substream);
  1627. ++out_ports;
  1628. }
  1629. if (endpoints[i].in_cables & (1 << j)) {
  1630. snd_usbmidi_init_substream(umidi,
  1631. SNDRV_RAWMIDI_STREAM_INPUT,
  1632. in_ports,
  1633. &umidi->endpoints[i].in->ports[j].substream);
  1634. ++in_ports;
  1635. }
  1636. }
  1637. }
  1638. dev_dbg(&umidi->dev->dev, "created %d output and %d input ports\n",
  1639. out_ports, in_ports);
  1640. return 0;
  1641. }
  1642. /*
  1643. * Returns MIDIStreaming device capabilities.
  1644. */
  1645. static int snd_usbmidi_get_ms_info(struct snd_usb_midi *umidi,
  1646. struct snd_usb_midi_endpoint_info *endpoints)
  1647. {
  1648. struct usb_interface *intf;
  1649. struct usb_host_interface *hostif;
  1650. struct usb_interface_descriptor *intfd;
  1651. struct usb_ms_header_descriptor *ms_header;
  1652. struct usb_host_endpoint *hostep;
  1653. struct usb_endpoint_descriptor *ep;
  1654. struct usb_ms_endpoint_descriptor *ms_ep;
  1655. int i, epidx;
  1656. intf = umidi->iface;
  1657. if (!intf)
  1658. return -ENXIO;
  1659. hostif = &intf->altsetting[0];
  1660. intfd = get_iface_desc(hostif);
  1661. ms_header = (struct usb_ms_header_descriptor *)hostif->extra;
  1662. if (hostif->extralen >= 7 &&
  1663. ms_header->bLength >= 7 &&
  1664. ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
  1665. ms_header->bDescriptorSubtype == UAC_HEADER)
  1666. dev_dbg(&umidi->dev->dev, "MIDIStreaming version %02x.%02x\n",
  1667. ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
  1668. else
  1669. dev_warn(&umidi->dev->dev,
  1670. "MIDIStreaming interface descriptor not found\n");
  1671. epidx = 0;
  1672. for (i = 0; i < intfd->bNumEndpoints; ++i) {
  1673. hostep = &hostif->endpoint[i];
  1674. ep = get_ep_desc(hostep);
  1675. if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
  1676. continue;
  1677. ms_ep = (struct usb_ms_endpoint_descriptor *)hostep->extra;
  1678. if (hostep->extralen < 4 ||
  1679. ms_ep->bLength < 4 ||
  1680. ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
  1681. ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
  1682. continue;
  1683. if (usb_endpoint_dir_out(ep)) {
  1684. if (endpoints[epidx].out_ep) {
  1685. if (++epidx >= MIDI_MAX_ENDPOINTS) {
  1686. dev_warn(&umidi->dev->dev,
  1687. "too many endpoints\n");
  1688. break;
  1689. }
  1690. }
  1691. endpoints[epidx].out_ep = usb_endpoint_num(ep);
  1692. if (usb_endpoint_xfer_int(ep))
  1693. endpoints[epidx].out_interval = ep->bInterval;
  1694. else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
  1695. /*
  1696. * Low speed bulk transfers don't exist, so
  1697. * force interrupt transfers for devices like
  1698. * ESI MIDI Mate that try to use them anyway.
  1699. */
  1700. endpoints[epidx].out_interval = 1;
  1701. endpoints[epidx].out_cables =
  1702. (1 << ms_ep->bNumEmbMIDIJack) - 1;
  1703. dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
  1704. ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
  1705. } else {
  1706. if (endpoints[epidx].in_ep) {
  1707. if (++epidx >= MIDI_MAX_ENDPOINTS) {
  1708. dev_warn(&umidi->dev->dev,
  1709. "too many endpoints\n");
  1710. break;
  1711. }
  1712. }
  1713. endpoints[epidx].in_ep = usb_endpoint_num(ep);
  1714. if (usb_endpoint_xfer_int(ep))
  1715. endpoints[epidx].in_interval = ep->bInterval;
  1716. else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
  1717. endpoints[epidx].in_interval = 1;
  1718. endpoints[epidx].in_cables =
  1719. (1 << ms_ep->bNumEmbMIDIJack) - 1;
  1720. dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
  1721. ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
  1722. }
  1723. }
  1724. return 0;
  1725. }
  1726. static int roland_load_info(struct snd_kcontrol *kcontrol,
  1727. struct snd_ctl_elem_info *info)
  1728. {
  1729. static const char *const names[] = { "High Load", "Light Load" };
  1730. return snd_ctl_enum_info(info, 1, 2, names);
  1731. }
  1732. static int roland_load_get(struct snd_kcontrol *kcontrol,
  1733. struct snd_ctl_elem_value *value)
  1734. {
  1735. value->value.enumerated.item[0] = kcontrol->private_value;
  1736. return 0;
  1737. }
  1738. static int roland_load_put(struct snd_kcontrol *kcontrol,
  1739. struct snd_ctl_elem_value *value)
  1740. {
  1741. struct snd_usb_midi *umidi = kcontrol->private_data;
  1742. int changed;
  1743. if (value->value.enumerated.item[0] > 1)
  1744. return -EINVAL;
  1745. mutex_lock(&umidi->mutex);
  1746. changed = value->value.enumerated.item[0] != kcontrol->private_value;
  1747. if (changed)
  1748. kcontrol->private_value = value->value.enumerated.item[0];
  1749. mutex_unlock(&umidi->mutex);
  1750. return changed;
  1751. }
  1752. static struct snd_kcontrol_new roland_load_ctl = {
  1753. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1754. .name = "MIDI Input Mode",
  1755. .info = roland_load_info,
  1756. .get = roland_load_get,
  1757. .put = roland_load_put,
  1758. .private_value = 1,
  1759. };
  1760. /*
  1761. * On Roland devices, use the second alternate setting to be able to use
  1762. * the interrupt input endpoint.
  1763. */
  1764. static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi *umidi)
  1765. {
  1766. struct usb_interface *intf;
  1767. struct usb_host_interface *hostif;
  1768. struct usb_interface_descriptor *intfd;
  1769. intf = umidi->iface;
  1770. if (!intf || intf->num_altsetting != 2)
  1771. return;
  1772. hostif = &intf->altsetting[1];
  1773. intfd = get_iface_desc(hostif);
  1774. /* If either or both of the endpoints support interrupt transfer,
  1775. * then use the alternate setting
  1776. */
  1777. if (intfd->bNumEndpoints != 2 ||
  1778. !((get_endpoint(hostif, 0)->bmAttributes &
  1779. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT ||
  1780. (get_endpoint(hostif, 1)->bmAttributes &
  1781. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
  1782. return;
  1783. dev_dbg(&umidi->dev->dev, "switching to altsetting %d with int ep\n",
  1784. intfd->bAlternateSetting);
  1785. usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
  1786. intfd->bAlternateSetting);
  1787. umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
  1788. if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
  1789. umidi->roland_load_ctl = NULL;
  1790. }
  1791. /*
  1792. * Try to find any usable endpoints in the interface.
  1793. */
  1794. static int snd_usbmidi_detect_endpoints(struct snd_usb_midi *umidi,
  1795. struct snd_usb_midi_endpoint_info *endpoint,
  1796. int max_endpoints)
  1797. {
  1798. struct usb_interface *intf;
  1799. struct usb_host_interface *hostif;
  1800. struct usb_interface_descriptor *intfd;
  1801. struct usb_endpoint_descriptor *epd;
  1802. int i, out_eps = 0, in_eps = 0;
  1803. if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
  1804. snd_usbmidi_switch_roland_altsetting(umidi);
  1805. if (endpoint[0].out_ep || endpoint[0].in_ep)
  1806. return 0;
  1807. intf = umidi->iface;
  1808. if (!intf || intf->num_altsetting < 1)
  1809. return -ENOENT;
  1810. hostif = intf->cur_altsetting;
  1811. intfd = get_iface_desc(hostif);
  1812. for (i = 0; i < intfd->bNumEndpoints; ++i) {
  1813. epd = get_endpoint(hostif, i);
  1814. if (!usb_endpoint_xfer_bulk(epd) &&
  1815. !usb_endpoint_xfer_int(epd))
  1816. continue;
  1817. if (out_eps < max_endpoints &&
  1818. usb_endpoint_dir_out(epd)) {
  1819. endpoint[out_eps].out_ep = usb_endpoint_num(epd);
  1820. if (usb_endpoint_xfer_int(epd))
  1821. endpoint[out_eps].out_interval = epd->bInterval;
  1822. ++out_eps;
  1823. }
  1824. if (in_eps < max_endpoints &&
  1825. usb_endpoint_dir_in(epd)) {
  1826. endpoint[in_eps].in_ep = usb_endpoint_num(epd);
  1827. if (usb_endpoint_xfer_int(epd))
  1828. endpoint[in_eps].in_interval = epd->bInterval;
  1829. ++in_eps;
  1830. }
  1831. }
  1832. return (out_eps || in_eps) ? 0 : -ENOENT;
  1833. }
  1834. /*
  1835. * Detects the endpoints for one-port-per-endpoint protocols.
  1836. */
  1837. static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi *umidi,
  1838. struct snd_usb_midi_endpoint_info *endpoints)
  1839. {
  1840. int err, i;
  1841. err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
  1842. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  1843. if (endpoints[i].out_ep)
  1844. endpoints[i].out_cables = 0x0001;
  1845. if (endpoints[i].in_ep)
  1846. endpoints[i].in_cables = 0x0001;
  1847. }
  1848. return err;
  1849. }
  1850. /*
  1851. * Detects the endpoints and ports of Yamaha devices.
  1852. */
  1853. static int snd_usbmidi_detect_yamaha(struct snd_usb_midi *umidi,
  1854. struct snd_usb_midi_endpoint_info *endpoint)
  1855. {
  1856. struct usb_interface *intf;
  1857. struct usb_host_interface *hostif;
  1858. struct usb_interface_descriptor *intfd;
  1859. uint8_t *cs_desc;
  1860. intf = umidi->iface;
  1861. if (!intf)
  1862. return -ENOENT;
  1863. hostif = intf->altsetting;
  1864. intfd = get_iface_desc(hostif);
  1865. if (intfd->bNumEndpoints < 1)
  1866. return -ENOENT;
  1867. /*
  1868. * For each port there is one MIDI_IN/OUT_JACK descriptor, not
  1869. * necessarily with any useful contents. So simply count 'em.
  1870. */
  1871. for (cs_desc = hostif->extra;
  1872. cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
  1873. cs_desc += cs_desc[0]) {
  1874. if (cs_desc[1] == USB_DT_CS_INTERFACE) {
  1875. if (cs_desc[2] == UAC_MIDI_IN_JACK)
  1876. endpoint->in_cables =
  1877. (endpoint->in_cables << 1) | 1;
  1878. else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
  1879. endpoint->out_cables =
  1880. (endpoint->out_cables << 1) | 1;
  1881. }
  1882. }
  1883. if (!endpoint->in_cables && !endpoint->out_cables)
  1884. return -ENOENT;
  1885. return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
  1886. }
  1887. /*
  1888. * Detects the endpoints and ports of Roland devices.
  1889. */
  1890. static int snd_usbmidi_detect_roland(struct snd_usb_midi *umidi,
  1891. struct snd_usb_midi_endpoint_info *endpoint)
  1892. {
  1893. struct usb_interface *intf;
  1894. struct usb_host_interface *hostif;
  1895. u8 *cs_desc;
  1896. intf = umidi->iface;
  1897. if (!intf)
  1898. return -ENOENT;
  1899. hostif = intf->altsetting;
  1900. /*
  1901. * Some devices have a descriptor <06 24 F1 02 <inputs> <outputs>>,
  1902. * some have standard class descriptors, or both kinds, or neither.
  1903. */
  1904. for (cs_desc = hostif->extra;
  1905. cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
  1906. cs_desc += cs_desc[0]) {
  1907. if (cs_desc[0] >= 6 &&
  1908. cs_desc[1] == USB_DT_CS_INTERFACE &&
  1909. cs_desc[2] == 0xf1 &&
  1910. cs_desc[3] == 0x02) {
  1911. endpoint->in_cables = (1 << cs_desc[4]) - 1;
  1912. endpoint->out_cables = (1 << cs_desc[5]) - 1;
  1913. return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
  1914. } else if (cs_desc[0] >= 7 &&
  1915. cs_desc[1] == USB_DT_CS_INTERFACE &&
  1916. cs_desc[2] == UAC_HEADER) {
  1917. return snd_usbmidi_get_ms_info(umidi, endpoint);
  1918. }
  1919. }
  1920. return -ENODEV;
  1921. }
  1922. /*
  1923. * Creates the endpoints and their ports for Midiman devices.
  1924. */
  1925. static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi *umidi,
  1926. struct snd_usb_midi_endpoint_info *endpoint)
  1927. {
  1928. struct snd_usb_midi_endpoint_info ep_info;
  1929. struct usb_interface *intf;
  1930. struct usb_host_interface *hostif;
  1931. struct usb_interface_descriptor *intfd;
  1932. struct usb_endpoint_descriptor *epd;
  1933. int cable, err;
  1934. intf = umidi->iface;
  1935. if (!intf)
  1936. return -ENOENT;
  1937. hostif = intf->altsetting;
  1938. intfd = get_iface_desc(hostif);
  1939. /*
  1940. * The various MidiSport devices have more or less random endpoint
  1941. * numbers, so we have to identify the endpoints by their index in
  1942. * the descriptor array, like the driver for that other OS does.
  1943. *
  1944. * There is one interrupt input endpoint for all input ports, one
  1945. * bulk output endpoint for even-numbered ports, and one for odd-
  1946. * numbered ports. Both bulk output endpoints have corresponding
  1947. * input bulk endpoints (at indices 1 and 3) which aren't used.
  1948. */
  1949. if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
  1950. dev_dbg(&umidi->dev->dev, "not enough endpoints\n");
  1951. return -ENOENT;
  1952. }
  1953. epd = get_endpoint(hostif, 0);
  1954. if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
  1955. dev_dbg(&umidi->dev->dev, "endpoint[0] isn't interrupt\n");
  1956. return -ENXIO;
  1957. }
  1958. epd = get_endpoint(hostif, 2);
  1959. if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
  1960. dev_dbg(&umidi->dev->dev, "endpoint[2] isn't bulk output\n");
  1961. return -ENXIO;
  1962. }
  1963. if (endpoint->out_cables > 0x0001) {
  1964. epd = get_endpoint(hostif, 4);
  1965. if (!usb_endpoint_dir_out(epd) ||
  1966. !usb_endpoint_xfer_bulk(epd)) {
  1967. dev_dbg(&umidi->dev->dev,
  1968. "endpoint[4] isn't bulk output\n");
  1969. return -ENXIO;
  1970. }
  1971. }
  1972. ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress &
  1973. USB_ENDPOINT_NUMBER_MASK;
  1974. ep_info.out_interval = 0;
  1975. ep_info.out_cables = endpoint->out_cables & 0x5555;
  1976. err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
  1977. &umidi->endpoints[0]);
  1978. if (err < 0)
  1979. return err;
  1980. ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress &
  1981. USB_ENDPOINT_NUMBER_MASK;
  1982. ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
  1983. ep_info.in_cables = endpoint->in_cables;
  1984. err = snd_usbmidi_in_endpoint_create(umidi, &ep_info,
  1985. &umidi->endpoints[0]);
  1986. if (err < 0)
  1987. return err;
  1988. if (endpoint->out_cables > 0x0001) {
  1989. ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress &
  1990. USB_ENDPOINT_NUMBER_MASK;
  1991. ep_info.out_cables = endpoint->out_cables & 0xaaaa;
  1992. err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
  1993. &umidi->endpoints[1]);
  1994. if (err < 0)
  1995. return err;
  1996. }
  1997. for (cable = 0; cable < 0x10; ++cable) {
  1998. if (endpoint->out_cables & (1 << cable))
  1999. snd_usbmidi_init_substream(umidi,
  2000. SNDRV_RAWMIDI_STREAM_OUTPUT,
  2001. cable,
  2002. &umidi->endpoints[cable & 1].out->ports[cable].substream);
  2003. if (endpoint->in_cables & (1 << cable))
  2004. snd_usbmidi_init_substream(umidi,
  2005. SNDRV_RAWMIDI_STREAM_INPUT,
  2006. cable,
  2007. &umidi->endpoints[0].in->ports[cable].substream);
  2008. }
  2009. return 0;
  2010. }
  2011. static const struct snd_rawmidi_global_ops snd_usbmidi_ops = {
  2012. .get_port_info = snd_usbmidi_get_port_info,
  2013. };
  2014. static int snd_usbmidi_create_rawmidi(struct snd_usb_midi *umidi,
  2015. int out_ports, int in_ports)
  2016. {
  2017. struct snd_rawmidi *rmidi;
  2018. int err;
  2019. err = snd_rawmidi_new(umidi->card, "USB MIDI",
  2020. umidi->next_midi_device++,
  2021. out_ports, in_ports, &rmidi);
  2022. if (err < 0)
  2023. return err;
  2024. strcpy(rmidi->name, umidi->card->shortname);
  2025. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  2026. SNDRV_RAWMIDI_INFO_INPUT |
  2027. SNDRV_RAWMIDI_INFO_DUPLEX;
  2028. rmidi->ops = &snd_usbmidi_ops;
  2029. rmidi->private_data = umidi;
  2030. rmidi->private_free = snd_usbmidi_rawmidi_free;
  2031. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  2032. &snd_usbmidi_output_ops);
  2033. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  2034. &snd_usbmidi_input_ops);
  2035. umidi->rmidi = rmidi;
  2036. return 0;
  2037. }
  2038. /*
  2039. * Temporarily stop input.
  2040. */
  2041. void snd_usbmidi_input_stop(struct list_head *p)
  2042. {
  2043. struct snd_usb_midi *umidi;
  2044. unsigned int i, j;
  2045. umidi = list_entry(p, struct snd_usb_midi, list);
  2046. if (!umidi->input_running)
  2047. return;
  2048. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  2049. struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
  2050. if (ep->in)
  2051. for (j = 0; j < INPUT_URBS; ++j)
  2052. usb_kill_urb(ep->in->urbs[j]);
  2053. }
  2054. umidi->input_running = 0;
  2055. }
  2056. EXPORT_SYMBOL(snd_usbmidi_input_stop);
  2057. static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint *ep)
  2058. {
  2059. unsigned int i;
  2060. if (!ep)
  2061. return;
  2062. for (i = 0; i < INPUT_URBS; ++i) {
  2063. struct urb *urb = ep->urbs[i];
  2064. urb->dev = ep->umidi->dev;
  2065. snd_usbmidi_submit_urb(urb, GFP_KERNEL);
  2066. }
  2067. }
  2068. /*
  2069. * Resume input after a call to snd_usbmidi_input_stop().
  2070. */
  2071. void snd_usbmidi_input_start(struct list_head *p)
  2072. {
  2073. struct snd_usb_midi *umidi;
  2074. int i;
  2075. umidi = list_entry(p, struct snd_usb_midi, list);
  2076. if (umidi->input_running || !umidi->opened[1])
  2077. return;
  2078. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
  2079. snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
  2080. umidi->input_running = 1;
  2081. }
  2082. EXPORT_SYMBOL(snd_usbmidi_input_start);
  2083. /*
  2084. * Prepare for suspend. Typically called from the USB suspend callback.
  2085. */
  2086. void snd_usbmidi_suspend(struct list_head *p)
  2087. {
  2088. struct snd_usb_midi *umidi;
  2089. umidi = list_entry(p, struct snd_usb_midi, list);
  2090. mutex_lock(&umidi->mutex);
  2091. snd_usbmidi_input_stop(p);
  2092. mutex_unlock(&umidi->mutex);
  2093. }
  2094. EXPORT_SYMBOL(snd_usbmidi_suspend);
  2095. /*
  2096. * Resume. Typically called from the USB resume callback.
  2097. */
  2098. void snd_usbmidi_resume(struct list_head *p)
  2099. {
  2100. struct snd_usb_midi *umidi;
  2101. umidi = list_entry(p, struct snd_usb_midi, list);
  2102. mutex_lock(&umidi->mutex);
  2103. snd_usbmidi_input_start(p);
  2104. mutex_unlock(&umidi->mutex);
  2105. }
  2106. EXPORT_SYMBOL(snd_usbmidi_resume);
  2107. /*
  2108. * Creates and registers everything needed for a MIDI streaming interface.
  2109. */
  2110. int __snd_usbmidi_create(struct snd_card *card,
  2111. struct usb_interface *iface,
  2112. struct list_head *midi_list,
  2113. const struct snd_usb_audio_quirk *quirk,
  2114. unsigned int usb_id)
  2115. {
  2116. struct snd_usb_midi *umidi;
  2117. struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
  2118. int out_ports, in_ports;
  2119. int i, err;
  2120. umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
  2121. if (!umidi)
  2122. return -ENOMEM;
  2123. umidi->dev = interface_to_usbdev(iface);
  2124. umidi->card = card;
  2125. umidi->iface = iface;
  2126. umidi->quirk = quirk;
  2127. umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
  2128. spin_lock_init(&umidi->disc_lock);
  2129. init_rwsem(&umidi->disc_rwsem);
  2130. mutex_init(&umidi->mutex);
  2131. if (!usb_id)
  2132. usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
  2133. le16_to_cpu(umidi->dev->descriptor.idProduct));
  2134. umidi->usb_id = usb_id;
  2135. setup_timer(&umidi->error_timer, snd_usbmidi_error_timer,
  2136. (unsigned long)umidi);
  2137. /* detect the endpoint(s) to use */
  2138. memset(endpoints, 0, sizeof(endpoints));
  2139. switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
  2140. case QUIRK_MIDI_STANDARD_INTERFACE:
  2141. err = snd_usbmidi_get_ms_info(umidi, endpoints);
  2142. if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
  2143. umidi->usb_protocol_ops =
  2144. &snd_usbmidi_maudio_broken_running_status_ops;
  2145. break;
  2146. case QUIRK_MIDI_US122L:
  2147. umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
  2148. /* fall through */
  2149. case QUIRK_MIDI_FIXED_ENDPOINT:
  2150. memcpy(&endpoints[0], quirk->data,
  2151. sizeof(struct snd_usb_midi_endpoint_info));
  2152. err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
  2153. break;
  2154. case QUIRK_MIDI_YAMAHA:
  2155. err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
  2156. break;
  2157. case QUIRK_MIDI_ROLAND:
  2158. err = snd_usbmidi_detect_roland(umidi, &endpoints[0]);
  2159. break;
  2160. case QUIRK_MIDI_MIDIMAN:
  2161. umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
  2162. memcpy(&endpoints[0], quirk->data,
  2163. sizeof(struct snd_usb_midi_endpoint_info));
  2164. err = 0;
  2165. break;
  2166. case QUIRK_MIDI_NOVATION:
  2167. umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
  2168. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  2169. break;
  2170. case QUIRK_MIDI_RAW_BYTES:
  2171. umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
  2172. /*
  2173. * Interface 1 contains isochronous endpoints, but with the same
  2174. * numbers as in interface 0. Since it is interface 1 that the
  2175. * USB core has most recently seen, these descriptors are now
  2176. * associated with the endpoint numbers. This will foul up our
  2177. * attempts to submit bulk/interrupt URBs to the endpoints in
  2178. * interface 0, so we have to make sure that the USB core looks
  2179. * again at interface 0 by calling usb_set_interface() on it.
  2180. */
  2181. if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
  2182. usb_set_interface(umidi->dev, 0, 0);
  2183. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  2184. break;
  2185. case QUIRK_MIDI_EMAGIC:
  2186. umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
  2187. memcpy(&endpoints[0], quirk->data,
  2188. sizeof(struct snd_usb_midi_endpoint_info));
  2189. err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
  2190. break;
  2191. case QUIRK_MIDI_CME:
  2192. umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
  2193. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  2194. break;
  2195. case QUIRK_MIDI_AKAI:
  2196. umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
  2197. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  2198. /* endpoint 1 is input-only */
  2199. endpoints[1].out_cables = 0;
  2200. break;
  2201. case QUIRK_MIDI_FTDI:
  2202. umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
  2203. /* set baud rate to 31250 (48 MHz / 16 / 96) */
  2204. err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
  2205. 3, 0x40, 0x60, 0, NULL, 0, 1000);
  2206. if (err < 0)
  2207. break;
  2208. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  2209. break;
  2210. case QUIRK_MIDI_CH345:
  2211. umidi->usb_protocol_ops = &snd_usbmidi_ch345_broken_sysex_ops;
  2212. err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
  2213. break;
  2214. default:
  2215. dev_err(&umidi->dev->dev, "invalid quirk type %d\n",
  2216. quirk->type);
  2217. err = -ENXIO;
  2218. break;
  2219. }
  2220. if (err < 0) {
  2221. kfree(umidi);
  2222. return err;
  2223. }
  2224. /* create rawmidi device */
  2225. out_ports = 0;
  2226. in_ports = 0;
  2227. for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
  2228. out_ports += hweight16(endpoints[i].out_cables);
  2229. in_ports += hweight16(endpoints[i].in_cables);
  2230. }
  2231. err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
  2232. if (err < 0) {
  2233. kfree(umidi);
  2234. return err;
  2235. }
  2236. /* create endpoint/port structures */
  2237. if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
  2238. err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
  2239. else
  2240. err = snd_usbmidi_create_endpoints(umidi, endpoints);
  2241. if (err < 0) {
  2242. return err;
  2243. }
  2244. usb_autopm_get_interface_no_resume(umidi->iface);
  2245. list_add_tail(&umidi->list, midi_list);
  2246. return 0;
  2247. }
  2248. EXPORT_SYMBOL(__snd_usbmidi_create);