f_midi.c 31 KB

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