usbmidi.c 50 KB

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