usbmidi.c 43 KB

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