f_midi.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. * Licensed under the GPL-2 or later.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/device.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/spinlock.h>
  28. #include <sound/core.h>
  29. #include <sound/initval.h>
  30. #include <sound/rawmidi.h>
  31. #include <linux/usb/ch9.h>
  32. #include <linux/usb/gadget.h>
  33. #include <linux/usb/audio.h>
  34. #include <linux/usb/midi.h>
  35. #include "u_f.h"
  36. #include "u_midi.h"
  37. MODULE_AUTHOR("Ben Williamson");
  38. MODULE_LICENSE("GPL v2");
  39. static const char f_midi_shortname[] = "f_midi";
  40. static const char f_midi_longname[] = "MIDI Gadget";
  41. /*
  42. * We can only handle 16 cables on one single endpoint, as cable numbers are
  43. * stored in 4-bit fields. And as the interface currently only holds one
  44. * single endpoint, this is the maximum number of ports we can allow.
  45. */
  46. #define MAX_PORTS 16
  47. /* MIDI message states */
  48. enum {
  49. STATE_INITIAL = 0, /* pseudo state */
  50. STATE_1PARAM,
  51. STATE_2PARAM_1,
  52. STATE_2PARAM_2,
  53. STATE_SYSEX_0,
  54. STATE_SYSEX_1,
  55. STATE_SYSEX_2,
  56. STATE_REAL_TIME,
  57. STATE_FINISHED, /* pseudo state */
  58. };
  59. /*
  60. * This is a gadget, and the IN/OUT naming is from the host's perspective.
  61. * USB -> OUT endpoint -> rawmidi
  62. * USB <- IN endpoint <- rawmidi
  63. */
  64. struct gmidi_in_port {
  65. struct snd_rawmidi_substream *substream;
  66. int active;
  67. uint8_t cable;
  68. uint8_t state;
  69. uint8_t data[2];
  70. };
  71. struct f_midi {
  72. struct usb_function func;
  73. struct usb_gadget *gadget;
  74. struct usb_ep *in_ep, *out_ep;
  75. struct snd_card *card;
  76. struct snd_rawmidi *rmidi;
  77. u8 ms_id;
  78. struct snd_rawmidi_substream *out_substream[MAX_PORTS];
  79. unsigned long out_triggered;
  80. struct tasklet_struct tasklet;
  81. unsigned int in_ports;
  82. unsigned int out_ports;
  83. int index;
  84. char *id;
  85. unsigned int buflen, qlen;
  86. /* This fifo is used as a buffer ring for pre-allocated IN usb_requests */
  87. DECLARE_KFIFO_PTR(in_req_fifo, struct usb_request *);
  88. spinlock_t transmit_lock;
  89. unsigned int in_last_port;
  90. unsigned char free_ref;
  91. struct gmidi_in_port in_ports_array[/* in_ports */];
  92. };
  93. static inline struct f_midi *func_to_midi(struct usb_function *f)
  94. {
  95. return container_of(f, struct f_midi, func);
  96. }
  97. static void f_midi_transmit(struct f_midi *midi);
  98. static void f_midi_rmidi_free(struct snd_rawmidi *rmidi);
  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. f_midi_transmit(midi);
  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. 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) { \
  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) {
  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 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. opts = container_of(f, struct f_midi_opts, func_inst);
  1023. if (opts->id_allocated)
  1024. kfree(opts->id);
  1025. kfree(opts);
  1026. }
  1027. static struct usb_function_instance *f_midi_alloc_inst(void)
  1028. {
  1029. struct f_midi_opts *opts;
  1030. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1031. if (!opts)
  1032. return ERR_PTR(-ENOMEM);
  1033. mutex_init(&opts->lock);
  1034. opts->func_inst.free_func_inst = f_midi_free_inst;
  1035. opts->index = SNDRV_DEFAULT_IDX1;
  1036. opts->id = SNDRV_DEFAULT_STR1;
  1037. opts->buflen = 512;
  1038. opts->qlen = 32;
  1039. opts->in_ports = 1;
  1040. opts->out_ports = 1;
  1041. config_group_init_type_name(&opts->func_inst.group, "",
  1042. &midi_func_type);
  1043. return &opts->func_inst;
  1044. }
  1045. static void f_midi_free(struct usb_function *f)
  1046. {
  1047. struct f_midi *midi;
  1048. struct f_midi_opts *opts;
  1049. midi = func_to_midi(f);
  1050. opts = container_of(f->fi, struct f_midi_opts, func_inst);
  1051. mutex_lock(&opts->lock);
  1052. if (!--midi->free_ref) {
  1053. kfree(midi->id);
  1054. kfifo_free(&midi->in_req_fifo);
  1055. kfree(midi);
  1056. --opts->refcnt;
  1057. }
  1058. mutex_unlock(&opts->lock);
  1059. }
  1060. static void f_midi_rmidi_free(struct snd_rawmidi *rmidi)
  1061. {
  1062. f_midi_free(rmidi->private_data);
  1063. }
  1064. static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
  1065. {
  1066. struct usb_composite_dev *cdev = f->config->cdev;
  1067. struct f_midi *midi = func_to_midi(f);
  1068. struct snd_card *card;
  1069. DBG(cdev, "unbind\n");
  1070. /* just to be sure */
  1071. f_midi_disable(f);
  1072. card = midi->card;
  1073. midi->card = NULL;
  1074. if (card)
  1075. snd_card_free_when_closed(card);
  1076. usb_free_all_descriptors(f);
  1077. }
  1078. static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
  1079. {
  1080. struct f_midi *midi = NULL;
  1081. struct f_midi_opts *opts;
  1082. int status, i;
  1083. opts = container_of(fi, struct f_midi_opts, func_inst);
  1084. mutex_lock(&opts->lock);
  1085. /* sanity check */
  1086. if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) {
  1087. status = -EINVAL;
  1088. goto setup_fail;
  1089. }
  1090. /* allocate and initialize one new instance */
  1091. midi = kzalloc(
  1092. sizeof(*midi) + opts->in_ports * sizeof(*midi->in_ports_array),
  1093. GFP_KERNEL);
  1094. if (!midi) {
  1095. status = -ENOMEM;
  1096. goto setup_fail;
  1097. }
  1098. for (i = 0; i < opts->in_ports; i++)
  1099. midi->in_ports_array[i].cable = i;
  1100. /* set up ALSA midi devices */
  1101. midi->id = kstrdup(opts->id, GFP_KERNEL);
  1102. if (opts->id && !midi->id) {
  1103. status = -ENOMEM;
  1104. goto setup_fail;
  1105. }
  1106. midi->in_ports = opts->in_ports;
  1107. midi->out_ports = opts->out_ports;
  1108. midi->index = opts->index;
  1109. midi->buflen = opts->buflen;
  1110. midi->qlen = opts->qlen;
  1111. midi->in_last_port = 0;
  1112. midi->free_ref = 1;
  1113. status = kfifo_alloc(&midi->in_req_fifo, midi->qlen, GFP_KERNEL);
  1114. if (status)
  1115. goto setup_fail;
  1116. spin_lock_init(&midi->transmit_lock);
  1117. ++opts->refcnt;
  1118. mutex_unlock(&opts->lock);
  1119. midi->func.name = "gmidi function";
  1120. midi->func.bind = f_midi_bind;
  1121. midi->func.unbind = f_midi_unbind;
  1122. midi->func.set_alt = f_midi_set_alt;
  1123. midi->func.disable = f_midi_disable;
  1124. midi->func.free_func = f_midi_free;
  1125. return &midi->func;
  1126. setup_fail:
  1127. mutex_unlock(&opts->lock);
  1128. kfree(midi);
  1129. return ERR_PTR(status);
  1130. }
  1131. DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc);