f_midi.c 29 KB

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