f_midi.c 35 KB

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