usbmidi.c 47 KB

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