midi.c 69 KB

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