f_midi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/device.h>
  25. #include <linux/kfifo.h>
  26. #include <sound/core.h>
  27. #include <sound/initval.h>
  28. #include <sound/rawmidi.h>
  29. #include <linux/usb/ch9.h>
  30. #include <linux/usb/gadget.h>
  31. #include <linux/usb/audio.h>
  32. #include <linux/usb/midi.h>
  33. #include "u_f.h"
  34. #include "u_midi.h"
  35. MODULE_AUTHOR("Ben Williamson");
  36. MODULE_LICENSE("GPL v2");
  37. static const char f_midi_shortname[] = "f_midi";
  38. static const char f_midi_longname[] = "MIDI Gadget";
  39. /*
  40. * We can only handle 16 cables on one single endpoint, as cable numbers are
  41. * stored in 4-bit fields. And as the interface currently only holds one
  42. * single endpoint, this is the maximum number of ports we can allow.
  43. */
  44. #define MAX_PORTS 16
  45. /*
  46. * This is a gadget, and the IN/OUT naming is from the host's perspective.
  47. * USB -> OUT endpoint -> rawmidi
  48. * USB <- IN endpoint <- rawmidi
  49. */
  50. struct gmidi_in_port {
  51. struct snd_rawmidi_substream *substream;
  52. int active;
  53. uint8_t cable;
  54. uint8_t state;
  55. #define STATE_UNKNOWN 0
  56. #define STATE_1PARAM 1
  57. #define STATE_2PARAM_1 2
  58. #define STATE_2PARAM_2 3
  59. #define STATE_SYSEX_0 4
  60. #define STATE_SYSEX_1 5
  61. #define STATE_SYSEX_2 6
  62. uint8_t data[2];
  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. u8 ms_id;
  71. struct snd_rawmidi_substream *out_substream[MAX_PORTS];
  72. unsigned long out_triggered;
  73. struct tasklet_struct tasklet;
  74. unsigned int in_ports;
  75. unsigned int out_ports;
  76. int index;
  77. char *id;
  78. unsigned int buflen, qlen;
  79. /* This fifo is used as a buffer ring for pre-allocated IN usb_requests */
  80. DECLARE_KFIFO_PTR(in_req_fifo, struct usb_request *);
  81. unsigned int in_last_port;
  82. struct gmidi_in_port in_ports_array[/* in_ports */];
  83. };
  84. static inline struct f_midi *func_to_midi(struct usb_function *f)
  85. {
  86. return container_of(f, struct f_midi, func);
  87. }
  88. static void f_midi_transmit(struct f_midi *midi);
  89. DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
  90. DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
  91. DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
  92. /* B.3.1 Standard AC Interface Descriptor */
  93. static struct usb_interface_descriptor ac_interface_desc = {
  94. .bLength = USB_DT_INTERFACE_SIZE,
  95. .bDescriptorType = USB_DT_INTERFACE,
  96. /* .bInterfaceNumber = DYNAMIC */
  97. /* .bNumEndpoints = DYNAMIC */
  98. .bInterfaceClass = USB_CLASS_AUDIO,
  99. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  100. /* .iInterface = DYNAMIC */
  101. };
  102. /* B.3.2 Class-Specific AC Interface Descriptor */
  103. static struct uac1_ac_header_descriptor_1 ac_header_desc = {
  104. .bLength = UAC_DT_AC_HEADER_SIZE(1),
  105. .bDescriptorType = USB_DT_CS_INTERFACE,
  106. .bDescriptorSubtype = USB_MS_HEADER,
  107. .bcdADC = cpu_to_le16(0x0100),
  108. .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
  109. .bInCollection = 1,
  110. /* .baInterfaceNr = DYNAMIC */
  111. };
  112. /* B.4.1 Standard MS Interface Descriptor */
  113. static struct usb_interface_descriptor ms_interface_desc = {
  114. .bLength = USB_DT_INTERFACE_SIZE,
  115. .bDescriptorType = USB_DT_INTERFACE,
  116. /* .bInterfaceNumber = DYNAMIC */
  117. .bNumEndpoints = 2,
  118. .bInterfaceClass = USB_CLASS_AUDIO,
  119. .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
  120. /* .iInterface = DYNAMIC */
  121. };
  122. /* B.4.2 Class-Specific MS Interface Descriptor */
  123. static struct usb_ms_header_descriptor ms_header_desc = {
  124. .bLength = USB_DT_MS_HEADER_SIZE,
  125. .bDescriptorType = USB_DT_CS_INTERFACE,
  126. .bDescriptorSubtype = USB_MS_HEADER,
  127. .bcdMSC = cpu_to_le16(0x0100),
  128. /* .wTotalLength = DYNAMIC */
  129. };
  130. /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
  131. static struct usb_endpoint_descriptor bulk_out_desc = {
  132. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  133. .bDescriptorType = USB_DT_ENDPOINT,
  134. .bEndpointAddress = USB_DIR_OUT,
  135. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  136. };
  137. /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
  138. static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
  139. /* .bLength = DYNAMIC */
  140. .bDescriptorType = USB_DT_CS_ENDPOINT,
  141. .bDescriptorSubtype = USB_MS_GENERAL,
  142. /* .bNumEmbMIDIJack = DYNAMIC */
  143. /* .baAssocJackID = DYNAMIC */
  144. };
  145. /* B.6.1 Standard Bulk IN Endpoint Descriptor */
  146. static struct usb_endpoint_descriptor bulk_in_desc = {
  147. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  148. .bDescriptorType = USB_DT_ENDPOINT,
  149. .bEndpointAddress = USB_DIR_IN,
  150. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  151. };
  152. /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
  153. static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
  154. /* .bLength = DYNAMIC */
  155. .bDescriptorType = USB_DT_CS_ENDPOINT,
  156. .bDescriptorSubtype = USB_MS_GENERAL,
  157. /* .bNumEmbMIDIJack = DYNAMIC */
  158. /* .baAssocJackID = DYNAMIC */
  159. };
  160. /* string IDs are assigned dynamically */
  161. #define STRING_FUNC_IDX 0
  162. static struct usb_string midi_string_defs[] = {
  163. [STRING_FUNC_IDX].s = "MIDI function",
  164. { } /* end of list */
  165. };
  166. static struct usb_gadget_strings midi_stringtab = {
  167. .language = 0x0409, /* en-us */
  168. .strings = midi_string_defs,
  169. };
  170. static struct usb_gadget_strings *midi_strings[] = {
  171. &midi_stringtab,
  172. NULL,
  173. };
  174. static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
  175. unsigned length)
  176. {
  177. return alloc_ep_req(ep, length, length);
  178. }
  179. static const uint8_t f_midi_cin_length[] = {
  180. 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
  181. };
  182. /*
  183. * Receives a chunk of MIDI data.
  184. */
  185. static void f_midi_read_data(struct usb_ep *ep, int cable,
  186. uint8_t *data, int length)
  187. {
  188. struct f_midi *midi = ep->driver_data;
  189. struct snd_rawmidi_substream *substream = midi->out_substream[cable];
  190. if (!substream)
  191. /* Nobody is listening - throw it on the floor. */
  192. return;
  193. if (!test_bit(cable, &midi->out_triggered))
  194. return;
  195. snd_rawmidi_receive(substream, data, length);
  196. }
  197. static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
  198. {
  199. unsigned int i;
  200. u8 *buf = req->buf;
  201. for (i = 0; i + 3 < req->actual; i += 4)
  202. if (buf[i] != 0) {
  203. int cable = buf[i] >> 4;
  204. int length = f_midi_cin_length[buf[i] & 0x0f];
  205. f_midi_read_data(ep, cable, &buf[i + 1], length);
  206. }
  207. }
  208. static void
  209. f_midi_complete(struct usb_ep *ep, struct usb_request *req)
  210. {
  211. struct f_midi *midi = ep->driver_data;
  212. struct usb_composite_dev *cdev = midi->func.config->cdev;
  213. int status = req->status;
  214. switch (status) {
  215. case 0: /* normal completion */
  216. if (ep == midi->out_ep) {
  217. /* We received stuff. req is queued again, below */
  218. f_midi_handle_out_data(ep, req);
  219. } else if (ep == midi->in_ep) {
  220. /* Our transmit completed. See if there's more to go.
  221. * f_midi_transmit eats req, don't queue it again. */
  222. req->length = 0;
  223. f_midi_transmit(midi);
  224. return;
  225. }
  226. break;
  227. /* this endpoint is normally active while we're configured */
  228. case -ECONNABORTED: /* hardware forced ep reset */
  229. case -ECONNRESET: /* request dequeued */
  230. case -ESHUTDOWN: /* disconnect from host */
  231. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  232. req->actual, req->length);
  233. if (ep == midi->out_ep) {
  234. f_midi_handle_out_data(ep, req);
  235. /* We don't need to free IN requests because it's handled
  236. * by the midi->in_req_fifo. */
  237. free_ep_req(ep, req);
  238. }
  239. return;
  240. case -EOVERFLOW: /* buffer overrun on read means that
  241. * we didn't provide a big enough buffer.
  242. */
  243. default:
  244. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  245. status, req->actual, req->length);
  246. break;
  247. case -EREMOTEIO: /* short read */
  248. break;
  249. }
  250. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  251. if (status) {
  252. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  253. ep->name, req->length, status);
  254. usb_ep_set_halt(ep);
  255. /* FIXME recover later ... somehow */
  256. }
  257. }
  258. static int f_midi_start_ep(struct f_midi *midi,
  259. struct usb_function *f,
  260. struct usb_ep *ep)
  261. {
  262. int err;
  263. struct usb_composite_dev *cdev = f->config->cdev;
  264. usb_ep_disable(ep);
  265. err = config_ep_by_speed(midi->gadget, f, ep);
  266. if (err) {
  267. ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
  268. return err;
  269. }
  270. err = usb_ep_enable(ep);
  271. if (err) {
  272. ERROR(cdev, "can't start %s: %d\n", ep->name, err);
  273. return err;
  274. }
  275. ep->driver_data = midi;
  276. return 0;
  277. }
  278. static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  279. {
  280. struct f_midi *midi = func_to_midi(f);
  281. unsigned i;
  282. int err;
  283. /* we only set alt for MIDIStreaming interface */
  284. if (intf != midi->ms_id)
  285. return 0;
  286. err = f_midi_start_ep(midi, f, midi->in_ep);
  287. if (err)
  288. return err;
  289. err = f_midi_start_ep(midi, f, midi->out_ep);
  290. if (err)
  291. return err;
  292. /* pre-allocate write usb requests to use on f_midi_transmit. */
  293. while (kfifo_avail(&midi->in_req_fifo)) {
  294. struct usb_request *req =
  295. midi_alloc_ep_req(midi->in_ep, midi->buflen);
  296. if (req == NULL)
  297. return -ENOMEM;
  298. req->length = 0;
  299. req->complete = f_midi_complete;
  300. kfifo_put(&midi->in_req_fifo, req);
  301. }
  302. /* allocate a bunch of read buffers and queue them all at once. */
  303. for (i = 0; i < midi->qlen && err == 0; i++) {
  304. struct usb_request *req =
  305. midi_alloc_ep_req(midi->out_ep, midi->buflen);
  306. if (req == NULL)
  307. return -ENOMEM;
  308. req->complete = f_midi_complete;
  309. err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
  310. if (err) {
  311. ERROR(midi, "%s: couldn't enqueue request: %d\n",
  312. midi->out_ep->name, err);
  313. free_ep_req(midi->out_ep, req);
  314. return err;
  315. }
  316. }
  317. return 0;
  318. }
  319. static void f_midi_disable(struct usb_function *f)
  320. {
  321. struct f_midi *midi = func_to_midi(f);
  322. struct usb_composite_dev *cdev = f->config->cdev;
  323. struct usb_request *req = NULL;
  324. DBG(cdev, "disable\n");
  325. /*
  326. * just disable endpoints, forcing completion of pending i/o.
  327. * all our completion handlers free their requests in this case.
  328. */
  329. usb_ep_disable(midi->in_ep);
  330. usb_ep_disable(midi->out_ep);
  331. /* release IN requests */
  332. while (kfifo_get(&midi->in_req_fifo, &req))
  333. free_ep_req(midi->in_ep, req);
  334. }
  335. static int f_midi_snd_free(struct snd_device *device)
  336. {
  337. return 0;
  338. }
  339. static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
  340. uint8_t p1, uint8_t p2, uint8_t p3)
  341. {
  342. unsigned length = req->length;
  343. u8 *buf = (u8 *)req->buf + length;
  344. buf[0] = p0;
  345. buf[1] = p1;
  346. buf[2] = p2;
  347. buf[3] = p3;
  348. req->length = length + 4;
  349. }
  350. /*
  351. * Converts MIDI commands to USB MIDI packets.
  352. */
  353. static void f_midi_transmit_byte(struct usb_request *req,
  354. struct gmidi_in_port *port, uint8_t b)
  355. {
  356. uint8_t p0 = port->cable << 4;
  357. if (b >= 0xf8) {
  358. f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
  359. } else if (b >= 0xf0) {
  360. switch (b) {
  361. case 0xf0:
  362. port->data[0] = b;
  363. port->state = STATE_SYSEX_1;
  364. break;
  365. case 0xf1:
  366. case 0xf3:
  367. port->data[0] = b;
  368. port->state = STATE_1PARAM;
  369. break;
  370. case 0xf2:
  371. port->data[0] = b;
  372. port->state = STATE_2PARAM_1;
  373. break;
  374. case 0xf4:
  375. case 0xf5:
  376. port->state = STATE_UNKNOWN;
  377. break;
  378. case 0xf6:
  379. f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
  380. port->state = STATE_UNKNOWN;
  381. break;
  382. case 0xf7:
  383. switch (port->state) {
  384. case STATE_SYSEX_0:
  385. f_midi_transmit_packet(req,
  386. p0 | 0x05, 0xf7, 0, 0);
  387. break;
  388. case STATE_SYSEX_1:
  389. f_midi_transmit_packet(req,
  390. p0 | 0x06, port->data[0], 0xf7, 0);
  391. break;
  392. case STATE_SYSEX_2:
  393. f_midi_transmit_packet(req,
  394. p0 | 0x07, port->data[0],
  395. port->data[1], 0xf7);
  396. break;
  397. }
  398. port->state = STATE_UNKNOWN;
  399. break;
  400. }
  401. } else if (b >= 0x80) {
  402. port->data[0] = b;
  403. if (b >= 0xc0 && b <= 0xdf)
  404. port->state = STATE_1PARAM;
  405. else
  406. port->state = STATE_2PARAM_1;
  407. } else { /* b < 0x80 */
  408. switch (port->state) {
  409. case STATE_1PARAM:
  410. if (port->data[0] < 0xf0) {
  411. p0 |= port->data[0] >> 4;
  412. } else {
  413. p0 |= 0x02;
  414. port->state = STATE_UNKNOWN;
  415. }
  416. f_midi_transmit_packet(req, p0, port->data[0], b, 0);
  417. break;
  418. case STATE_2PARAM_1:
  419. port->data[1] = b;
  420. port->state = STATE_2PARAM_2;
  421. break;
  422. case STATE_2PARAM_2:
  423. if (port->data[0] < 0xf0) {
  424. p0 |= port->data[0] >> 4;
  425. port->state = STATE_2PARAM_1;
  426. } else {
  427. p0 |= 0x03;
  428. port->state = STATE_UNKNOWN;
  429. }
  430. f_midi_transmit_packet(req,
  431. p0, port->data[0], port->data[1], b);
  432. break;
  433. case STATE_SYSEX_0:
  434. port->data[0] = b;
  435. port->state = STATE_SYSEX_1;
  436. break;
  437. case STATE_SYSEX_1:
  438. port->data[1] = b;
  439. port->state = STATE_SYSEX_2;
  440. break;
  441. case STATE_SYSEX_2:
  442. f_midi_transmit_packet(req,
  443. p0 | 0x04, port->data[0], port->data[1], b);
  444. port->state = STATE_SYSEX_0;
  445. break;
  446. }
  447. }
  448. }
  449. static void f_midi_drop_out_substreams(struct f_midi *midi)
  450. {
  451. unsigned int i;
  452. for (i = 0; i < midi->in_ports; i++) {
  453. struct gmidi_in_port *port = midi->in_ports_array + i;
  454. struct snd_rawmidi_substream *substream = port->substream;
  455. if (port->active && substream)
  456. snd_rawmidi_drop_output(substream);
  457. }
  458. }
  459. static int f_midi_do_transmit(struct f_midi *midi, struct usb_ep *ep)
  460. {
  461. struct usb_request *req = NULL;
  462. unsigned int len, i;
  463. bool active = false;
  464. int err;
  465. /*
  466. * We peek the request in order to reuse it if it fails to enqueue on
  467. * its endpoint
  468. */
  469. len = kfifo_peek(&midi->in_req_fifo, &req);
  470. if (len != 1) {
  471. ERROR(midi, "%s: Couldn't get usb request\n", __func__);
  472. return -1;
  473. }
  474. /*
  475. * If buffer overrun, then we ignore this transmission.
  476. * IMPORTANT: This will cause the user-space rawmidi device to block
  477. * until a) usb requests have been completed or b) snd_rawmidi_write()
  478. * times out.
  479. */
  480. if (req->length > 0)
  481. return 0;
  482. for (i = midi->in_last_port; i < midi->in_ports; ++i) {
  483. struct gmidi_in_port *port = midi->in_ports_array + i;
  484. struct snd_rawmidi_substream *substream = port->substream;
  485. if (!port->active || !substream)
  486. continue;
  487. while (req->length + 3 < midi->buflen) {
  488. uint8_t b;
  489. if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
  490. port->active = 0;
  491. break;
  492. }
  493. f_midi_transmit_byte(req, port, b);
  494. }
  495. active = !!port->active;
  496. if (active)
  497. break;
  498. }
  499. midi->in_last_port = active ? i : 0;
  500. if (req->length <= 0)
  501. goto done;
  502. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  503. if (err < 0) {
  504. ERROR(midi, "%s failed to queue req: %d\n",
  505. midi->in_ep->name, err);
  506. req->length = 0; /* Re-use request next time. */
  507. } else {
  508. /* Upon success, put request at the back of the queue. */
  509. kfifo_skip(&midi->in_req_fifo);
  510. kfifo_put(&midi->in_req_fifo, req);
  511. }
  512. done:
  513. return active;
  514. }
  515. static void f_midi_transmit(struct f_midi *midi)
  516. {
  517. struct usb_ep *ep = midi->in_ep;
  518. int ret;
  519. /* We only care about USB requests if IN endpoint is enabled */
  520. if (!ep || !ep->enabled)
  521. goto drop_out;
  522. do {
  523. ret = f_midi_do_transmit(midi, ep);
  524. if (ret < 0)
  525. goto drop_out;
  526. } while (ret);
  527. return;
  528. drop_out:
  529. f_midi_drop_out_substreams(midi);
  530. }
  531. static void f_midi_in_tasklet(unsigned long data)
  532. {
  533. struct f_midi *midi = (struct f_midi *) data;
  534. f_midi_transmit(midi);
  535. }
  536. static int f_midi_in_open(struct snd_rawmidi_substream *substream)
  537. {
  538. struct f_midi *midi = substream->rmidi->private_data;
  539. struct gmidi_in_port *port;
  540. if (substream->number >= midi->in_ports)
  541. return -EINVAL;
  542. VDBG(midi, "%s()\n", __func__);
  543. port = midi->in_ports_array + substream->number;
  544. port->substream = substream;
  545. port->state = STATE_UNKNOWN;
  546. return 0;
  547. }
  548. static int f_midi_in_close(struct snd_rawmidi_substream *substream)
  549. {
  550. struct f_midi *midi = substream->rmidi->private_data;
  551. VDBG(midi, "%s()\n", __func__);
  552. return 0;
  553. }
  554. static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
  555. {
  556. struct f_midi *midi = substream->rmidi->private_data;
  557. if (substream->number >= midi->in_ports)
  558. return;
  559. VDBG(midi, "%s() %d\n", __func__, up);
  560. midi->in_ports_array[substream->number].active = up;
  561. if (up)
  562. tasklet_hi_schedule(&midi->tasklet);
  563. }
  564. static int f_midi_out_open(struct snd_rawmidi_substream *substream)
  565. {
  566. struct f_midi *midi = substream->rmidi->private_data;
  567. if (substream->number >= MAX_PORTS)
  568. return -EINVAL;
  569. VDBG(midi, "%s()\n", __func__);
  570. midi->out_substream[substream->number] = substream;
  571. return 0;
  572. }
  573. static int f_midi_out_close(struct snd_rawmidi_substream *substream)
  574. {
  575. struct f_midi *midi = substream->rmidi->private_data;
  576. VDBG(midi, "%s()\n", __func__);
  577. return 0;
  578. }
  579. static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
  580. {
  581. struct f_midi *midi = substream->rmidi->private_data;
  582. VDBG(midi, "%s()\n", __func__);
  583. if (up)
  584. set_bit(substream->number, &midi->out_triggered);
  585. else
  586. clear_bit(substream->number, &midi->out_triggered);
  587. }
  588. static struct snd_rawmidi_ops gmidi_in_ops = {
  589. .open = f_midi_in_open,
  590. .close = f_midi_in_close,
  591. .trigger = f_midi_in_trigger,
  592. };
  593. static struct snd_rawmidi_ops gmidi_out_ops = {
  594. .open = f_midi_out_open,
  595. .close = f_midi_out_close,
  596. .trigger = f_midi_out_trigger
  597. };
  598. static inline void f_midi_unregister_card(struct f_midi *midi)
  599. {
  600. if (midi->card) {
  601. snd_card_free(midi->card);
  602. midi->card = NULL;
  603. }
  604. }
  605. /* register as a sound "card" */
  606. static int f_midi_register_card(struct f_midi *midi)
  607. {
  608. struct snd_card *card;
  609. struct snd_rawmidi *rmidi;
  610. int err;
  611. static struct snd_device_ops ops = {
  612. .dev_free = f_midi_snd_free,
  613. };
  614. err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
  615. THIS_MODULE, 0, &card);
  616. if (err < 0) {
  617. ERROR(midi, "snd_card_new() failed\n");
  618. goto fail;
  619. }
  620. midi->card = card;
  621. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
  622. if (err < 0) {
  623. ERROR(midi, "snd_device_new() failed: error %d\n", err);
  624. goto fail;
  625. }
  626. strcpy(card->driver, f_midi_longname);
  627. strcpy(card->longname, f_midi_longname);
  628. strcpy(card->shortname, f_midi_shortname);
  629. /* Set up rawmidi */
  630. snd_component_add(card, "MIDI");
  631. err = snd_rawmidi_new(card, card->longname, 0,
  632. midi->out_ports, midi->in_ports, &rmidi);
  633. if (err < 0) {
  634. ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
  635. goto fail;
  636. }
  637. midi->rmidi = rmidi;
  638. midi->in_last_port = 0;
  639. strcpy(rmidi->name, card->shortname);
  640. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  641. SNDRV_RAWMIDI_INFO_INPUT |
  642. SNDRV_RAWMIDI_INFO_DUPLEX;
  643. rmidi->private_data = midi;
  644. /*
  645. * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
  646. * It's an upside-down world being a gadget.
  647. */
  648. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
  649. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
  650. /* register it - we're ready to go */
  651. err = snd_card_register(card);
  652. if (err < 0) {
  653. ERROR(midi, "snd_card_register() failed\n");
  654. goto fail;
  655. }
  656. VDBG(midi, "%s() finished ok\n", __func__);
  657. return 0;
  658. fail:
  659. f_midi_unregister_card(midi);
  660. return err;
  661. }
  662. /* MIDI function driver setup/binding */
  663. static int f_midi_bind(struct usb_configuration *c, struct usb_function *f)
  664. {
  665. struct usb_descriptor_header **midi_function;
  666. struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
  667. struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
  668. struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
  669. struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
  670. struct usb_composite_dev *cdev = c->cdev;
  671. struct f_midi *midi = func_to_midi(f);
  672. struct usb_string *us;
  673. int status, n, jack = 1, i = 0;
  674. midi->gadget = cdev->gadget;
  675. tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
  676. status = f_midi_register_card(midi);
  677. if (status < 0)
  678. goto fail_register;
  679. /* maybe allocate device-global string ID */
  680. us = usb_gstrings_attach(c->cdev, midi_strings,
  681. ARRAY_SIZE(midi_string_defs));
  682. if (IS_ERR(us)) {
  683. status = PTR_ERR(us);
  684. goto fail;
  685. }
  686. ac_interface_desc.iInterface = us[STRING_FUNC_IDX].id;
  687. /* We have two interfaces, AudioControl and MIDIStreaming */
  688. status = usb_interface_id(c, f);
  689. if (status < 0)
  690. goto fail;
  691. ac_interface_desc.bInterfaceNumber = status;
  692. status = usb_interface_id(c, f);
  693. if (status < 0)
  694. goto fail;
  695. ms_interface_desc.bInterfaceNumber = status;
  696. ac_header_desc.baInterfaceNr[0] = status;
  697. midi->ms_id = status;
  698. status = -ENODEV;
  699. /* allocate instance-specific endpoints */
  700. midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
  701. if (!midi->in_ep)
  702. goto fail;
  703. midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
  704. if (!midi->out_ep)
  705. goto fail;
  706. /* allocate temporary function list */
  707. midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
  708. GFP_KERNEL);
  709. if (!midi_function) {
  710. status = -ENOMEM;
  711. goto fail;
  712. }
  713. /*
  714. * construct the function's descriptor set. As the number of
  715. * input and output MIDI ports is configurable, we have to do
  716. * it that way.
  717. */
  718. /* add the headers - these are always the same */
  719. midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
  720. midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
  721. midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
  722. /* calculate the header's wTotalLength */
  723. n = USB_DT_MS_HEADER_SIZE
  724. + (midi->in_ports + midi->out_ports) *
  725. (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
  726. ms_header_desc.wTotalLength = cpu_to_le16(n);
  727. midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
  728. /* configure the external IN jacks, each linked to an embedded OUT jack */
  729. for (n = 0; n < midi->in_ports; n++) {
  730. struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
  731. struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
  732. in_ext->bLength = USB_DT_MIDI_IN_SIZE;
  733. in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  734. in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  735. in_ext->bJackType = USB_MS_EXTERNAL;
  736. in_ext->bJackID = jack++;
  737. in_ext->iJack = 0;
  738. midi_function[i++] = (struct usb_descriptor_header *) in_ext;
  739. out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
  740. out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  741. out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  742. out_emb->bJackType = USB_MS_EMBEDDED;
  743. out_emb->bJackID = jack++;
  744. out_emb->bNrInputPins = 1;
  745. out_emb->pins[0].baSourcePin = 1;
  746. out_emb->pins[0].baSourceID = in_ext->bJackID;
  747. out_emb->iJack = 0;
  748. midi_function[i++] = (struct usb_descriptor_header *) out_emb;
  749. /* link it to the endpoint */
  750. ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
  751. }
  752. /* configure the external OUT jacks, each linked to an embedded IN jack */
  753. for (n = 0; n < midi->out_ports; n++) {
  754. struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
  755. struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
  756. in_emb->bLength = USB_DT_MIDI_IN_SIZE;
  757. in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  758. in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  759. in_emb->bJackType = USB_MS_EMBEDDED;
  760. in_emb->bJackID = jack++;
  761. in_emb->iJack = 0;
  762. midi_function[i++] = (struct usb_descriptor_header *) in_emb;
  763. out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
  764. out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  765. out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  766. out_ext->bJackType = USB_MS_EXTERNAL;
  767. out_ext->bJackID = jack++;
  768. out_ext->bNrInputPins = 1;
  769. out_ext->iJack = 0;
  770. out_ext->pins[0].baSourceID = in_emb->bJackID;
  771. out_ext->pins[0].baSourcePin = 1;
  772. midi_function[i++] = (struct usb_descriptor_header *) out_ext;
  773. /* link it to the endpoint */
  774. ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
  775. }
  776. /* configure the endpoint descriptors ... */
  777. ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
  778. ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
  779. ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
  780. ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
  781. /* ... and add them to the list */
  782. midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
  783. midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
  784. midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
  785. midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
  786. midi_function[i++] = NULL;
  787. /*
  788. * support all relevant hardware speeds... we expect that when
  789. * hardware is dual speed, all bulk-capable endpoints work at
  790. * both speeds
  791. */
  792. /* copy descriptors, and track endpoint copies */
  793. f->fs_descriptors = usb_copy_descriptors(midi_function);
  794. if (!f->fs_descriptors)
  795. goto fail_f_midi;
  796. if (gadget_is_dualspeed(c->cdev->gadget)) {
  797. bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
  798. bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
  799. f->hs_descriptors = usb_copy_descriptors(midi_function);
  800. if (!f->hs_descriptors)
  801. goto fail_f_midi;
  802. }
  803. kfree(midi_function);
  804. return 0;
  805. fail_f_midi:
  806. kfree(midi_function);
  807. usb_free_descriptors(f->hs_descriptors);
  808. fail:
  809. f_midi_unregister_card(midi);
  810. fail_register:
  811. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  812. return status;
  813. }
  814. static inline struct f_midi_opts *to_f_midi_opts(struct config_item *item)
  815. {
  816. return container_of(to_config_group(item), struct f_midi_opts,
  817. func_inst.group);
  818. }
  819. static void midi_attr_release(struct config_item *item)
  820. {
  821. struct f_midi_opts *opts = to_f_midi_opts(item);
  822. usb_put_function_instance(&opts->func_inst);
  823. }
  824. static struct configfs_item_operations midi_item_ops = {
  825. .release = midi_attr_release,
  826. };
  827. #define F_MIDI_OPT(name, test_limit, limit) \
  828. static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
  829. { \
  830. struct f_midi_opts *opts = to_f_midi_opts(item); \
  831. int result; \
  832. \
  833. mutex_lock(&opts->lock); \
  834. result = sprintf(page, "%d\n", opts->name); \
  835. mutex_unlock(&opts->lock); \
  836. \
  837. return result; \
  838. } \
  839. \
  840. static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
  841. const char *page, size_t len) \
  842. { \
  843. struct f_midi_opts *opts = to_f_midi_opts(item); \
  844. int ret; \
  845. u32 num; \
  846. \
  847. mutex_lock(&opts->lock); \
  848. if (opts->refcnt) { \
  849. ret = -EBUSY; \
  850. goto end; \
  851. } \
  852. \
  853. ret = kstrtou32(page, 0, &num); \
  854. if (ret) \
  855. goto end; \
  856. \
  857. if (test_limit && num > limit) { \
  858. ret = -EINVAL; \
  859. goto end; \
  860. } \
  861. opts->name = num; \
  862. ret = len; \
  863. \
  864. end: \
  865. mutex_unlock(&opts->lock); \
  866. return ret; \
  867. } \
  868. \
  869. CONFIGFS_ATTR(f_midi_opts_, name);
  870. F_MIDI_OPT(index, true, SNDRV_CARDS);
  871. F_MIDI_OPT(buflen, false, 0);
  872. F_MIDI_OPT(qlen, false, 0);
  873. F_MIDI_OPT(in_ports, true, MAX_PORTS);
  874. F_MIDI_OPT(out_ports, true, MAX_PORTS);
  875. static ssize_t f_midi_opts_id_show(struct config_item *item, char *page)
  876. {
  877. struct f_midi_opts *opts = to_f_midi_opts(item);
  878. int result;
  879. mutex_lock(&opts->lock);
  880. if (opts->id) {
  881. result = strlcpy(page, opts->id, PAGE_SIZE);
  882. } else {
  883. page[0] = 0;
  884. result = 0;
  885. }
  886. mutex_unlock(&opts->lock);
  887. return result;
  888. }
  889. static ssize_t f_midi_opts_id_store(struct config_item *item,
  890. const char *page, size_t len)
  891. {
  892. struct f_midi_opts *opts = to_f_midi_opts(item);
  893. int ret;
  894. char *c;
  895. mutex_lock(&opts->lock);
  896. if (opts->refcnt) {
  897. ret = -EBUSY;
  898. goto end;
  899. }
  900. c = kstrndup(page, len, GFP_KERNEL);
  901. if (!c) {
  902. ret = -ENOMEM;
  903. goto end;
  904. }
  905. if (opts->id_allocated)
  906. kfree(opts->id);
  907. opts->id = c;
  908. opts->id_allocated = true;
  909. ret = len;
  910. end:
  911. mutex_unlock(&opts->lock);
  912. return ret;
  913. }
  914. CONFIGFS_ATTR(f_midi_opts_, id);
  915. static struct configfs_attribute *midi_attrs[] = {
  916. &f_midi_opts_attr_index,
  917. &f_midi_opts_attr_buflen,
  918. &f_midi_opts_attr_qlen,
  919. &f_midi_opts_attr_in_ports,
  920. &f_midi_opts_attr_out_ports,
  921. &f_midi_opts_attr_id,
  922. NULL,
  923. };
  924. static struct config_item_type midi_func_type = {
  925. .ct_item_ops = &midi_item_ops,
  926. .ct_attrs = midi_attrs,
  927. .ct_owner = THIS_MODULE,
  928. };
  929. static void f_midi_free_inst(struct usb_function_instance *f)
  930. {
  931. struct f_midi_opts *opts;
  932. opts = container_of(f, struct f_midi_opts, func_inst);
  933. if (opts->id_allocated)
  934. kfree(opts->id);
  935. kfree(opts);
  936. }
  937. static struct usb_function_instance *f_midi_alloc_inst(void)
  938. {
  939. struct f_midi_opts *opts;
  940. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  941. if (!opts)
  942. return ERR_PTR(-ENOMEM);
  943. mutex_init(&opts->lock);
  944. opts->func_inst.free_func_inst = f_midi_free_inst;
  945. opts->index = SNDRV_DEFAULT_IDX1;
  946. opts->id = SNDRV_DEFAULT_STR1;
  947. opts->buflen = 256;
  948. opts->qlen = 32;
  949. opts->in_ports = 1;
  950. opts->out_ports = 1;
  951. config_group_init_type_name(&opts->func_inst.group, "",
  952. &midi_func_type);
  953. return &opts->func_inst;
  954. }
  955. static void f_midi_free(struct usb_function *f)
  956. {
  957. struct f_midi *midi;
  958. struct f_midi_opts *opts;
  959. midi = func_to_midi(f);
  960. opts = container_of(f->fi, struct f_midi_opts, func_inst);
  961. kfree(midi->id);
  962. mutex_lock(&opts->lock);
  963. kfifo_free(&midi->in_req_fifo);
  964. kfree(midi);
  965. --opts->refcnt;
  966. mutex_unlock(&opts->lock);
  967. }
  968. static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
  969. {
  970. struct usb_composite_dev *cdev = f->config->cdev;
  971. struct f_midi *midi = func_to_midi(f);
  972. struct snd_card *card;
  973. DBG(cdev, "unbind\n");
  974. /* just to be sure */
  975. f_midi_disable(f);
  976. card = midi->card;
  977. midi->card = NULL;
  978. if (card)
  979. snd_card_free(card);
  980. usb_free_all_descriptors(f);
  981. }
  982. static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
  983. {
  984. struct f_midi *midi = NULL;
  985. struct f_midi_opts *opts;
  986. int status, i;
  987. opts = container_of(fi, struct f_midi_opts, func_inst);
  988. mutex_lock(&opts->lock);
  989. /* sanity check */
  990. if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) {
  991. status = -EINVAL;
  992. goto setup_fail;
  993. }
  994. /* allocate and initialize one new instance */
  995. midi = kzalloc(
  996. sizeof(*midi) + opts->in_ports * sizeof(*midi->in_ports_array),
  997. GFP_KERNEL);
  998. if (!midi) {
  999. status = -ENOMEM;
  1000. goto setup_fail;
  1001. }
  1002. for (i = 0; i < opts->in_ports; i++)
  1003. midi->in_ports_array[i].cable = i;
  1004. /* set up ALSA midi devices */
  1005. midi->id = kstrdup(opts->id, GFP_KERNEL);
  1006. if (opts->id && !midi->id) {
  1007. status = -ENOMEM;
  1008. goto setup_fail;
  1009. }
  1010. midi->in_ports = opts->in_ports;
  1011. midi->out_ports = opts->out_ports;
  1012. midi->index = opts->index;
  1013. midi->buflen = opts->buflen;
  1014. midi->qlen = opts->qlen;
  1015. midi->in_last_port = 0;
  1016. status = kfifo_alloc(&midi->in_req_fifo, midi->qlen, GFP_KERNEL);
  1017. if (status)
  1018. goto setup_fail;
  1019. ++opts->refcnt;
  1020. mutex_unlock(&opts->lock);
  1021. midi->func.name = "gmidi function";
  1022. midi->func.bind = f_midi_bind;
  1023. midi->func.unbind = f_midi_unbind;
  1024. midi->func.set_alt = f_midi_set_alt;
  1025. midi->func.disable = f_midi_disable;
  1026. midi->func.free_func = f_midi_free;
  1027. return &midi->func;
  1028. setup_fail:
  1029. mutex_unlock(&opts->lock);
  1030. kfree(midi);
  1031. return ERR_PTR(status);
  1032. }
  1033. DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc);