f_midi.c 26 KB

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