midi.c 67 KB

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