f_midi.c 31 KB

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