f_midi.c 33 KB

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