f_midi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * f_midi.c -- USB MIDI class function driver
  3. *
  4. * Copyright (C) 2006 Thumtronics Pty Ltd.
  5. * Developed for Thumtronics by Grey Innovation
  6. * Ben Williamson <ben.williamson@greyinnovation.com>
  7. *
  8. * Rewritten for the composite framework
  9. * Copyright (C) 2011 Daniel Mack <zonque@gmail.com>
  10. *
  11. * Based on drivers/usb/gadget/f_audio.c,
  12. * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
  13. * Copyright (C) 2008 Analog Devices, Inc
  14. *
  15. * and drivers/usb/gadget/midi.c,
  16. * Copyright (C) 2006 Thumtronics Pty Ltd.
  17. * Ben Williamson <ben.williamson@greyinnovation.com>
  18. *
  19. * Licensed under the GPL-2 or later.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/device.h>
  24. #include <sound/core.h>
  25. #include <sound/initval.h>
  26. #include <sound/rawmidi.h>
  27. #include <linux/usb/ch9.h>
  28. #include <linux/usb/gadget.h>
  29. #include <linux/usb/audio.h>
  30. #include <linux/usb/midi.h>
  31. MODULE_AUTHOR("Ben Williamson");
  32. MODULE_LICENSE("GPL v2");
  33. static const char f_midi_shortname[] = "f_midi";
  34. static const char f_midi_longname[] = "MIDI Gadget";
  35. /*
  36. * We can only handle 16 cables on one single endpoint, as cable numbers are
  37. * stored in 4-bit fields. And as the interface currently only holds one
  38. * single endpoint, this is the maximum number of ports we can allow.
  39. */
  40. #define MAX_PORTS 16
  41. /*
  42. * This is a gadget, and the IN/OUT naming is from the host's perspective.
  43. * USB -> OUT endpoint -> rawmidi
  44. * USB <- IN endpoint <- rawmidi
  45. */
  46. struct gmidi_in_port {
  47. struct f_midi *midi;
  48. int active;
  49. uint8_t cable;
  50. uint8_t state;
  51. #define STATE_UNKNOWN 0
  52. #define STATE_1PARAM 1
  53. #define STATE_2PARAM_1 2
  54. #define STATE_2PARAM_2 3
  55. #define STATE_SYSEX_0 4
  56. #define STATE_SYSEX_1 5
  57. #define STATE_SYSEX_2 6
  58. uint8_t data[2];
  59. };
  60. struct midi_alsa_config {
  61. int card;
  62. int device;
  63. };
  64. struct f_midi {
  65. struct usb_function func;
  66. struct usb_gadget *gadget;
  67. struct usb_ep *in_ep, *out_ep;
  68. struct snd_card *card;
  69. struct snd_rawmidi *rmidi;
  70. struct snd_rawmidi_substream *in_substream[MAX_PORTS];
  71. struct snd_rawmidi_substream *out_substream[MAX_PORTS];
  72. struct gmidi_in_port *in_port[MAX_PORTS];
  73. unsigned long out_triggered;
  74. struct tasklet_struct tasklet;
  75. unsigned int in_ports;
  76. unsigned int out_ports;
  77. int index;
  78. char *id;
  79. unsigned int buflen, qlen;
  80. };
  81. static inline struct f_midi *func_to_midi(struct usb_function *f)
  82. {
  83. return container_of(f, struct f_midi, func);
  84. }
  85. static void f_midi_transmit(struct f_midi *midi, struct usb_request *req);
  86. DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
  87. DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
  88. DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
  89. /* B.3.1 Standard AC Interface Descriptor */
  90. static struct usb_interface_descriptor ac_interface_desc /* __initdata */ = {
  91. .bLength = USB_DT_INTERFACE_SIZE,
  92. .bDescriptorType = USB_DT_INTERFACE,
  93. /* .bInterfaceNumber = DYNAMIC */
  94. /* .bNumEndpoints = DYNAMIC */
  95. .bInterfaceClass = USB_CLASS_AUDIO,
  96. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  97. /* .iInterface = DYNAMIC */
  98. };
  99. /* B.3.2 Class-Specific AC Interface Descriptor */
  100. static struct uac1_ac_header_descriptor_1 ac_header_desc /* __initdata */ = {
  101. .bLength = UAC_DT_AC_HEADER_SIZE(1),
  102. .bDescriptorType = USB_DT_CS_INTERFACE,
  103. .bDescriptorSubtype = USB_MS_HEADER,
  104. .bcdADC = cpu_to_le16(0x0100),
  105. .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
  106. .bInCollection = 1,
  107. /* .baInterfaceNr = DYNAMIC */
  108. };
  109. /* B.4.1 Standard MS Interface Descriptor */
  110. static struct usb_interface_descriptor ms_interface_desc /* __initdata */ = {
  111. .bLength = USB_DT_INTERFACE_SIZE,
  112. .bDescriptorType = USB_DT_INTERFACE,
  113. /* .bInterfaceNumber = DYNAMIC */
  114. .bNumEndpoints = 2,
  115. .bInterfaceClass = USB_CLASS_AUDIO,
  116. .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
  117. /* .iInterface = DYNAMIC */
  118. };
  119. /* B.4.2 Class-Specific MS Interface Descriptor */
  120. static struct usb_ms_header_descriptor ms_header_desc /* __initdata */ = {
  121. .bLength = USB_DT_MS_HEADER_SIZE,
  122. .bDescriptorType = USB_DT_CS_INTERFACE,
  123. .bDescriptorSubtype = USB_MS_HEADER,
  124. .bcdMSC = cpu_to_le16(0x0100),
  125. /* .wTotalLength = DYNAMIC */
  126. };
  127. /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
  128. static struct usb_endpoint_descriptor bulk_out_desc = {
  129. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  130. .bDescriptorType = USB_DT_ENDPOINT,
  131. .bEndpointAddress = USB_DIR_OUT,
  132. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  133. };
  134. /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
  135. static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
  136. /* .bLength = DYNAMIC */
  137. .bDescriptorType = USB_DT_CS_ENDPOINT,
  138. .bDescriptorSubtype = USB_MS_GENERAL,
  139. /* .bNumEmbMIDIJack = DYNAMIC */
  140. /* .baAssocJackID = DYNAMIC */
  141. };
  142. /* B.6.1 Standard Bulk IN Endpoint Descriptor */
  143. static struct usb_endpoint_descriptor bulk_in_desc = {
  144. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  145. .bDescriptorType = USB_DT_ENDPOINT,
  146. .bEndpointAddress = USB_DIR_IN,
  147. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  148. };
  149. /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
  150. static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
  151. /* .bLength = DYNAMIC */
  152. .bDescriptorType = USB_DT_CS_ENDPOINT,
  153. .bDescriptorSubtype = USB_MS_GENERAL,
  154. /* .bNumEmbMIDIJack = DYNAMIC */
  155. /* .baAssocJackID = DYNAMIC */
  156. };
  157. /* string IDs are assigned dynamically */
  158. #define STRING_FUNC_IDX 0
  159. static struct usb_string midi_string_defs[] = {
  160. [STRING_FUNC_IDX].s = "MIDI function",
  161. { } /* end of list */
  162. };
  163. static struct usb_gadget_strings midi_stringtab = {
  164. .language = 0x0409, /* en-us */
  165. .strings = midi_string_defs,
  166. };
  167. static struct usb_gadget_strings *midi_strings[] = {
  168. &midi_stringtab,
  169. NULL,
  170. };
  171. static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
  172. {
  173. struct usb_request *req;
  174. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  175. if (req) {
  176. req->length = length;
  177. req->buf = kmalloc(length, GFP_ATOMIC);
  178. if (!req->buf) {
  179. usb_ep_free_request(ep, req);
  180. req = NULL;
  181. }
  182. }
  183. return req;
  184. }
  185. static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  186. {
  187. kfree(req->buf);
  188. usb_ep_free_request(ep, req);
  189. }
  190. static const uint8_t f_midi_cin_length[] = {
  191. 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
  192. };
  193. /*
  194. * Receives a chunk of MIDI data.
  195. */
  196. static void f_midi_read_data(struct usb_ep *ep, int cable,
  197. uint8_t *data, int length)
  198. {
  199. struct f_midi *midi = ep->driver_data;
  200. struct snd_rawmidi_substream *substream = midi->out_substream[cable];
  201. if (!substream)
  202. /* Nobody is listening - throw it on the floor. */
  203. return;
  204. if (!test_bit(cable, &midi->out_triggered))
  205. return;
  206. snd_rawmidi_receive(substream, data, length);
  207. }
  208. static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
  209. {
  210. unsigned int i;
  211. u8 *buf = req->buf;
  212. for (i = 0; i + 3 < req->actual; i += 4)
  213. if (buf[i] != 0) {
  214. int cable = buf[i] >> 4;
  215. int length = f_midi_cin_length[buf[i] & 0x0f];
  216. f_midi_read_data(ep, cable, &buf[i + 1], length);
  217. }
  218. }
  219. static void
  220. f_midi_complete(struct usb_ep *ep, struct usb_request *req)
  221. {
  222. struct f_midi *midi = ep->driver_data;
  223. struct usb_composite_dev *cdev = midi->func.config->cdev;
  224. int status = req->status;
  225. switch (status) {
  226. case 0: /* normal completion */
  227. if (ep == midi->out_ep) {
  228. /* We received stuff. req is queued again, below */
  229. f_midi_handle_out_data(ep, req);
  230. } else if (ep == midi->in_ep) {
  231. /* Our transmit completed. See if there's more to go.
  232. * f_midi_transmit eats req, don't queue it again. */
  233. f_midi_transmit(midi, req);
  234. return;
  235. }
  236. break;
  237. /* this endpoint is normally active while we're configured */
  238. case -ECONNABORTED: /* hardware forced ep reset */
  239. case -ECONNRESET: /* request dequeued */
  240. case -ESHUTDOWN: /* disconnect from host */
  241. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  242. req->actual, req->length);
  243. if (ep == midi->out_ep)
  244. f_midi_handle_out_data(ep, req);
  245. free_ep_req(ep, req);
  246. return;
  247. case -EOVERFLOW: /* buffer overrun on read means that
  248. * we didn't provide a big enough buffer.
  249. */
  250. default:
  251. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  252. status, req->actual, req->length);
  253. break;
  254. case -EREMOTEIO: /* short read */
  255. break;
  256. }
  257. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  258. if (status) {
  259. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  260. ep->name, req->length, status);
  261. usb_ep_set_halt(ep);
  262. /* FIXME recover later ... somehow */
  263. }
  264. }
  265. static int f_midi_start_ep(struct f_midi *midi,
  266. struct usb_function *f,
  267. struct usb_ep *ep)
  268. {
  269. int err;
  270. struct usb_composite_dev *cdev = f->config->cdev;
  271. if (ep->driver_data)
  272. usb_ep_disable(ep);
  273. err = config_ep_by_speed(midi->gadget, f, ep);
  274. if (err) {
  275. ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
  276. return err;
  277. }
  278. err = usb_ep_enable(ep);
  279. if (err) {
  280. ERROR(cdev, "can't start %s: %d\n", ep->name, err);
  281. return err;
  282. }
  283. ep->driver_data = midi;
  284. return 0;
  285. }
  286. static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  287. {
  288. struct f_midi *midi = func_to_midi(f);
  289. struct usb_composite_dev *cdev = f->config->cdev;
  290. unsigned i;
  291. int err;
  292. err = f_midi_start_ep(midi, f, midi->in_ep);
  293. if (err)
  294. return err;
  295. err = f_midi_start_ep(midi, f, midi->out_ep);
  296. if (err)
  297. return err;
  298. if (midi->out_ep->driver_data)
  299. usb_ep_disable(midi->out_ep);
  300. err = config_ep_by_speed(midi->gadget, f, midi->out_ep);
  301. if (err) {
  302. ERROR(cdev, "can't configure %s: %d\n",
  303. midi->out_ep->name, err);
  304. return err;
  305. }
  306. err = usb_ep_enable(midi->out_ep);
  307. if (err) {
  308. ERROR(cdev, "can't start %s: %d\n",
  309. midi->out_ep->name, err);
  310. return err;
  311. }
  312. midi->out_ep->driver_data = midi;
  313. /* allocate a bunch of read buffers and queue them all at once. */
  314. for (i = 0; i < midi->qlen && err == 0; i++) {
  315. struct usb_request *req =
  316. alloc_ep_req(midi->out_ep, midi->buflen);
  317. if (req == NULL)
  318. return -ENOMEM;
  319. req->complete = f_midi_complete;
  320. err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
  321. if (err) {
  322. ERROR(midi, "%s queue req: %d\n",
  323. midi->out_ep->name, err);
  324. }
  325. }
  326. return 0;
  327. }
  328. static void f_midi_disable(struct usb_function *f)
  329. {
  330. struct f_midi *midi = func_to_midi(f);
  331. struct usb_composite_dev *cdev = f->config->cdev;
  332. DBG(cdev, "disable\n");
  333. /*
  334. * just disable endpoints, forcing completion of pending i/o.
  335. * all our completion handlers free their requests in this case.
  336. */
  337. usb_ep_disable(midi->in_ep);
  338. usb_ep_disable(midi->out_ep);
  339. }
  340. static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
  341. {
  342. struct usb_composite_dev *cdev = f->config->cdev;
  343. struct f_midi *midi = func_to_midi(f);
  344. struct snd_card *card;
  345. DBG(cdev, "unbind\n");
  346. /* just to be sure */
  347. f_midi_disable(f);
  348. card = midi->card;
  349. midi->card = NULL;
  350. if (card)
  351. snd_card_free_when_closed(card);
  352. kfree(midi->id);
  353. midi->id = NULL;
  354. usb_free_all_descriptors(f);
  355. kfree(midi);
  356. }
  357. static int f_midi_snd_free(struct snd_device *device)
  358. {
  359. return 0;
  360. }
  361. static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
  362. uint8_t p1, uint8_t p2, uint8_t p3)
  363. {
  364. unsigned length = req->length;
  365. u8 *buf = (u8 *)req->buf + length;
  366. buf[0] = p0;
  367. buf[1] = p1;
  368. buf[2] = p2;
  369. buf[3] = p3;
  370. req->length = length + 4;
  371. }
  372. /*
  373. * Converts MIDI commands to USB MIDI packets.
  374. */
  375. static void f_midi_transmit_byte(struct usb_request *req,
  376. struct gmidi_in_port *port, uint8_t b)
  377. {
  378. uint8_t p0 = port->cable << 4;
  379. if (b >= 0xf8) {
  380. f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
  381. } else if (b >= 0xf0) {
  382. switch (b) {
  383. case 0xf0:
  384. port->data[0] = b;
  385. port->state = STATE_SYSEX_1;
  386. break;
  387. case 0xf1:
  388. case 0xf3:
  389. port->data[0] = b;
  390. port->state = STATE_1PARAM;
  391. break;
  392. case 0xf2:
  393. port->data[0] = b;
  394. port->state = STATE_2PARAM_1;
  395. break;
  396. case 0xf4:
  397. case 0xf5:
  398. port->state = STATE_UNKNOWN;
  399. break;
  400. case 0xf6:
  401. f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
  402. port->state = STATE_UNKNOWN;
  403. break;
  404. case 0xf7:
  405. switch (port->state) {
  406. case STATE_SYSEX_0:
  407. f_midi_transmit_packet(req,
  408. p0 | 0x05, 0xf7, 0, 0);
  409. break;
  410. case STATE_SYSEX_1:
  411. f_midi_transmit_packet(req,
  412. p0 | 0x06, port->data[0], 0xf7, 0);
  413. break;
  414. case STATE_SYSEX_2:
  415. f_midi_transmit_packet(req,
  416. p0 | 0x07, port->data[0],
  417. port->data[1], 0xf7);
  418. break;
  419. }
  420. port->state = STATE_UNKNOWN;
  421. break;
  422. }
  423. } else if (b >= 0x80) {
  424. port->data[0] = b;
  425. if (b >= 0xc0 && b <= 0xdf)
  426. port->state = STATE_1PARAM;
  427. else
  428. port->state = STATE_2PARAM_1;
  429. } else { /* b < 0x80 */
  430. switch (port->state) {
  431. case STATE_1PARAM:
  432. if (port->data[0] < 0xf0) {
  433. p0 |= port->data[0] >> 4;
  434. } else {
  435. p0 |= 0x02;
  436. port->state = STATE_UNKNOWN;
  437. }
  438. f_midi_transmit_packet(req, p0, port->data[0], b, 0);
  439. break;
  440. case STATE_2PARAM_1:
  441. port->data[1] = b;
  442. port->state = STATE_2PARAM_2;
  443. break;
  444. case STATE_2PARAM_2:
  445. if (port->data[0] < 0xf0) {
  446. p0 |= port->data[0] >> 4;
  447. port->state = STATE_2PARAM_1;
  448. } else {
  449. p0 |= 0x03;
  450. port->state = STATE_UNKNOWN;
  451. }
  452. f_midi_transmit_packet(req,
  453. p0, port->data[0], port->data[1], b);
  454. break;
  455. case STATE_SYSEX_0:
  456. port->data[0] = b;
  457. port->state = STATE_SYSEX_1;
  458. break;
  459. case STATE_SYSEX_1:
  460. port->data[1] = b;
  461. port->state = STATE_SYSEX_2;
  462. break;
  463. case STATE_SYSEX_2:
  464. f_midi_transmit_packet(req,
  465. p0 | 0x04, port->data[0], port->data[1], b);
  466. port->state = STATE_SYSEX_0;
  467. break;
  468. }
  469. }
  470. }
  471. static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
  472. {
  473. struct usb_ep *ep = midi->in_ep;
  474. int i;
  475. if (!ep)
  476. return;
  477. if (!req)
  478. req = alloc_ep_req(ep, midi->buflen);
  479. if (!req) {
  480. ERROR(midi, "gmidi_transmit: alloc_ep_request failed\n");
  481. return;
  482. }
  483. req->length = 0;
  484. req->complete = f_midi_complete;
  485. for (i = 0; i < MAX_PORTS; i++) {
  486. struct gmidi_in_port *port = midi->in_port[i];
  487. struct snd_rawmidi_substream *substream = midi->in_substream[i];
  488. if (!port || !port->active || !substream)
  489. continue;
  490. while (req->length + 3 < midi->buflen) {
  491. uint8_t b;
  492. if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
  493. port->active = 0;
  494. break;
  495. }
  496. f_midi_transmit_byte(req, port, b);
  497. }
  498. }
  499. if (req->length > 0)
  500. usb_ep_queue(ep, req, GFP_ATOMIC);
  501. else
  502. free_ep_req(ep, req);
  503. }
  504. static void f_midi_in_tasklet(unsigned long data)
  505. {
  506. struct f_midi *midi = (struct f_midi *) data;
  507. f_midi_transmit(midi, NULL);
  508. }
  509. static int f_midi_in_open(struct snd_rawmidi_substream *substream)
  510. {
  511. struct f_midi *midi = substream->rmidi->private_data;
  512. if (!midi->in_port[substream->number])
  513. return -EINVAL;
  514. VDBG(midi, "%s()\n", __func__);
  515. midi->in_substream[substream->number] = substream;
  516. midi->in_port[substream->number]->state = STATE_UNKNOWN;
  517. return 0;
  518. }
  519. static int f_midi_in_close(struct snd_rawmidi_substream *substream)
  520. {
  521. struct f_midi *midi = substream->rmidi->private_data;
  522. VDBG(midi, "%s()\n", __func__);
  523. return 0;
  524. }
  525. static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
  526. {
  527. struct f_midi *midi = substream->rmidi->private_data;
  528. if (!midi->in_port[substream->number])
  529. return;
  530. VDBG(midi, "%s() %d\n", __func__, up);
  531. midi->in_port[substream->number]->active = up;
  532. if (up)
  533. tasklet_hi_schedule(&midi->tasklet);
  534. }
  535. static int f_midi_out_open(struct snd_rawmidi_substream *substream)
  536. {
  537. struct f_midi *midi = substream->rmidi->private_data;
  538. if (substream->number >= MAX_PORTS)
  539. return -EINVAL;
  540. VDBG(midi, "%s()\n", __func__);
  541. midi->out_substream[substream->number] = substream;
  542. return 0;
  543. }
  544. static int f_midi_out_close(struct snd_rawmidi_substream *substream)
  545. {
  546. struct f_midi *midi = substream->rmidi->private_data;
  547. VDBG(midi, "%s()\n", __func__);
  548. return 0;
  549. }
  550. static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
  551. {
  552. struct f_midi *midi = substream->rmidi->private_data;
  553. VDBG(midi, "%s()\n", __func__);
  554. if (up)
  555. set_bit(substream->number, &midi->out_triggered);
  556. else
  557. clear_bit(substream->number, &midi->out_triggered);
  558. }
  559. static struct snd_rawmidi_ops gmidi_in_ops = {
  560. .open = f_midi_in_open,
  561. .close = f_midi_in_close,
  562. .trigger = f_midi_in_trigger,
  563. };
  564. static struct snd_rawmidi_ops gmidi_out_ops = {
  565. .open = f_midi_out_open,
  566. .close = f_midi_out_close,
  567. .trigger = f_midi_out_trigger
  568. };
  569. /* register as a sound "card" */
  570. static int f_midi_register_card(struct f_midi *midi)
  571. {
  572. struct snd_card *card;
  573. struct snd_rawmidi *rmidi;
  574. int err;
  575. static struct snd_device_ops ops = {
  576. .dev_free = f_midi_snd_free,
  577. };
  578. err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
  579. THIS_MODULE, 0, &card);
  580. if (err < 0) {
  581. ERROR(midi, "snd_card_new() failed\n");
  582. goto fail;
  583. }
  584. midi->card = card;
  585. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
  586. if (err < 0) {
  587. ERROR(midi, "snd_device_new() failed: error %d\n", err);
  588. goto fail;
  589. }
  590. strcpy(card->driver, f_midi_longname);
  591. strcpy(card->longname, f_midi_longname);
  592. strcpy(card->shortname, f_midi_shortname);
  593. /* Set up rawmidi */
  594. snd_component_add(card, "MIDI");
  595. err = snd_rawmidi_new(card, card->longname, 0,
  596. midi->out_ports, midi->in_ports, &rmidi);
  597. if (err < 0) {
  598. ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
  599. goto fail;
  600. }
  601. midi->rmidi = rmidi;
  602. strcpy(rmidi->name, card->shortname);
  603. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  604. SNDRV_RAWMIDI_INFO_INPUT |
  605. SNDRV_RAWMIDI_INFO_DUPLEX;
  606. rmidi->private_data = midi;
  607. /*
  608. * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
  609. * It's an upside-down world being a gadget.
  610. */
  611. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
  612. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
  613. snd_card_set_dev(card, &midi->gadget->dev);
  614. /* register it - we're ready to go */
  615. err = snd_card_register(card);
  616. if (err < 0) {
  617. ERROR(midi, "snd_card_register() failed\n");
  618. goto fail;
  619. }
  620. VDBG(midi, "%s() finished ok\n", __func__);
  621. return 0;
  622. fail:
  623. if (midi->card) {
  624. snd_card_free(midi->card);
  625. midi->card = NULL;
  626. }
  627. return err;
  628. }
  629. /* MIDI function driver setup/binding */
  630. static int /* __init */
  631. f_midi_bind(struct usb_configuration *c, struct usb_function *f)
  632. {
  633. struct usb_descriptor_header **midi_function;
  634. struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
  635. struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
  636. struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
  637. struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
  638. struct usb_composite_dev *cdev = c->cdev;
  639. struct f_midi *midi = func_to_midi(f);
  640. int status, n, jack = 1, i = 0;
  641. /* maybe allocate device-global string ID */
  642. if (midi_string_defs[0].id == 0) {
  643. status = usb_string_id(c->cdev);
  644. if (status < 0)
  645. goto fail;
  646. midi_string_defs[0].id = status;
  647. }
  648. /* We have two interfaces, AudioControl and MIDIStreaming */
  649. status = usb_interface_id(c, f);
  650. if (status < 0)
  651. goto fail;
  652. ac_interface_desc.bInterfaceNumber = status;
  653. status = usb_interface_id(c, f);
  654. if (status < 0)
  655. goto fail;
  656. ms_interface_desc.bInterfaceNumber = status;
  657. ac_header_desc.baInterfaceNr[0] = status;
  658. status = -ENODEV;
  659. /* allocate instance-specific endpoints */
  660. midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
  661. if (!midi->in_ep)
  662. goto fail;
  663. midi->in_ep->driver_data = cdev; /* claim */
  664. midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
  665. if (!midi->out_ep)
  666. goto fail;
  667. midi->out_ep->driver_data = cdev; /* claim */
  668. /* allocate temporary function list */
  669. midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
  670. GFP_KERNEL);
  671. if (!midi_function) {
  672. status = -ENOMEM;
  673. goto fail;
  674. }
  675. /*
  676. * construct the function's descriptor set. As the number of
  677. * input and output MIDI ports is configurable, we have to do
  678. * it that way.
  679. */
  680. /* add the headers - these are always the same */
  681. midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
  682. midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
  683. midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
  684. /* calculate the header's wTotalLength */
  685. n = USB_DT_MS_HEADER_SIZE
  686. + (midi->in_ports + midi->out_ports) *
  687. (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
  688. ms_header_desc.wTotalLength = cpu_to_le16(n);
  689. midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
  690. /* configure the external IN jacks, each linked to an embedded OUT jack */
  691. for (n = 0; n < midi->in_ports; n++) {
  692. struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
  693. struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
  694. in_ext->bLength = USB_DT_MIDI_IN_SIZE;
  695. in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  696. in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  697. in_ext->bJackType = USB_MS_EXTERNAL;
  698. in_ext->bJackID = jack++;
  699. in_ext->iJack = 0;
  700. midi_function[i++] = (struct usb_descriptor_header *) in_ext;
  701. out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
  702. out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  703. out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  704. out_emb->bJackType = USB_MS_EMBEDDED;
  705. out_emb->bJackID = jack++;
  706. out_emb->bNrInputPins = 1;
  707. out_emb->pins[0].baSourcePin = 1;
  708. out_emb->pins[0].baSourceID = in_ext->bJackID;
  709. out_emb->iJack = 0;
  710. midi_function[i++] = (struct usb_descriptor_header *) out_emb;
  711. /* link it to the endpoint */
  712. ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
  713. }
  714. /* configure the external OUT jacks, each linked to an embedded IN jack */
  715. for (n = 0; n < midi->out_ports; n++) {
  716. struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
  717. struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
  718. in_emb->bLength = USB_DT_MIDI_IN_SIZE;
  719. in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  720. in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  721. in_emb->bJackType = USB_MS_EMBEDDED;
  722. in_emb->bJackID = jack++;
  723. in_emb->iJack = 0;
  724. midi_function[i++] = (struct usb_descriptor_header *) in_emb;
  725. out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
  726. out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  727. out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  728. out_ext->bJackType = USB_MS_EXTERNAL;
  729. out_ext->bJackID = jack++;
  730. out_ext->bNrInputPins = 1;
  731. out_ext->iJack = 0;
  732. out_ext->pins[0].baSourceID = in_emb->bJackID;
  733. out_ext->pins[0].baSourcePin = 1;
  734. midi_function[i++] = (struct usb_descriptor_header *) out_ext;
  735. /* link it to the endpoint */
  736. ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
  737. }
  738. /* configure the endpoint descriptors ... */
  739. ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
  740. ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
  741. ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
  742. ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
  743. /* ... and add them to the list */
  744. midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
  745. midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
  746. midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
  747. midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
  748. midi_function[i++] = NULL;
  749. /*
  750. * support all relevant hardware speeds... we expect that when
  751. * hardware is dual speed, all bulk-capable endpoints work at
  752. * both speeds
  753. */
  754. /* copy descriptors, and track endpoint copies */
  755. f->fs_descriptors = usb_copy_descriptors(midi_function);
  756. if (!f->fs_descriptors)
  757. goto fail_f_midi;
  758. if (gadget_is_dualspeed(c->cdev->gadget)) {
  759. bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
  760. bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
  761. f->hs_descriptors = usb_copy_descriptors(midi_function);
  762. if (!f->hs_descriptors)
  763. goto fail_f_midi;
  764. }
  765. kfree(midi_function);
  766. return 0;
  767. fail_f_midi:
  768. kfree(midi_function);
  769. usb_free_descriptors(f->hs_descriptors);
  770. fail:
  771. /* we might as well release our claims on endpoints */
  772. if (midi->out_ep)
  773. midi->out_ep->driver_data = NULL;
  774. if (midi->in_ep)
  775. midi->in_ep->driver_data = NULL;
  776. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  777. return status;
  778. }
  779. /**
  780. * f_midi_bind_config - add USB MIDI function to a configuration
  781. * @c: the configuration to supcard the USB audio function
  782. * @index: the soundcard index to use for the ALSA device creation
  783. * @id: the soundcard id to use for the ALSA device creation
  784. * @buflen: the buffer length to use
  785. * @qlen the number of read requests to pre-allocate
  786. * Context: single threaded during gadget setup
  787. *
  788. * Returns zero on success, else negative errno.
  789. */
  790. int /* __init */ f_midi_bind_config(struct usb_configuration *c,
  791. int index, char *id,
  792. unsigned int in_ports,
  793. unsigned int out_ports,
  794. unsigned int buflen,
  795. unsigned int qlen,
  796. struct midi_alsa_config* config)
  797. {
  798. struct f_midi *midi;
  799. int status, i;
  800. if (config) {
  801. config->card = -1;
  802. config->device = -1;
  803. }
  804. /* sanity check */
  805. if (in_ports > MAX_PORTS || out_ports > MAX_PORTS)
  806. return -EINVAL;
  807. /* allocate and initialize one new instance */
  808. midi = kzalloc(sizeof *midi, GFP_KERNEL);
  809. if (!midi) {
  810. status = -ENOMEM;
  811. goto fail;
  812. }
  813. for (i = 0; i < in_ports; i++) {
  814. struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
  815. if (!port) {
  816. status = -ENOMEM;
  817. goto setup_fail;
  818. }
  819. port->midi = midi;
  820. port->active = 0;
  821. port->cable = i;
  822. midi->in_port[i] = port;
  823. }
  824. midi->gadget = c->cdev->gadget;
  825. tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
  826. /* set up ALSA midi devices */
  827. midi->id = kstrdup(id, GFP_KERNEL);
  828. midi->index = index;
  829. midi->buflen = buflen;
  830. midi->qlen = qlen;
  831. midi->in_ports = in_ports;
  832. midi->out_ports = out_ports;
  833. status = f_midi_register_card(midi);
  834. if (status < 0)
  835. goto setup_fail;
  836. midi->func.name = "gmidi function";
  837. midi->func.strings = midi_strings;
  838. midi->func.bind = f_midi_bind;
  839. midi->func.unbind = f_midi_unbind;
  840. midi->func.set_alt = f_midi_set_alt;
  841. midi->func.disable = f_midi_disable;
  842. status = usb_add_function(c, &midi->func);
  843. if (status)
  844. goto setup_fail;
  845. if (config) {
  846. config->card = midi->rmidi->card->number;
  847. config->device = midi->rmidi->device;
  848. }
  849. return 0;
  850. setup_fail:
  851. for (--i; i >= 0; i--)
  852. kfree(midi->in_port[i]);
  853. kfree(midi);
  854. fail:
  855. return status;
  856. }