f_midi.c 33 KB

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