f_midi.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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 <[email protected]>
  8. *
  9. * Rewritten for the composite framework
  10. * Copyright (C) 2011 Daniel Mack <[email protected]>
  11. *
  12. * Based on drivers/usb/gadget/f_audio.c,
  13. * Copyright (C) 2008 Bryan Wu <[email protected]>
  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 <[email protected]>
  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 work_struct work;
  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_work(struct work_struct *work)
  599. {
  600. struct f_midi *midi;
  601. midi = container_of(work, struct f_midi, work);
  602. f_midi_transmit(midi);
  603. }
  604. static int f_midi_in_open(struct snd_rawmidi_substream *substream)
  605. {
  606. struct f_midi *midi = substream->rmidi->private_data;
  607. struct gmidi_in_port *port;
  608. if (substream->number >= midi->in_ports)
  609. return -EINVAL;
  610. VDBG(midi, "%s()\n", __func__);
  611. port = midi->in_ports_array + substream->number;
  612. port->substream = substream;
  613. port->state = STATE_INITIAL;
  614. return 0;
  615. }
  616. static int f_midi_in_close(struct snd_rawmidi_substream *substream)
  617. {
  618. struct f_midi *midi = substream->rmidi->private_data;
  619. VDBG(midi, "%s()\n", __func__);
  620. return 0;
  621. }
  622. static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
  623. {
  624. struct f_midi *midi = substream->rmidi->private_data;
  625. if (substream->number >= midi->in_ports)
  626. return;
  627. VDBG(midi, "%s() %d\n", __func__, up);
  628. midi->in_ports_array[substream->number].active = up;
  629. if (up)
  630. queue_work(system_highpri_wq, &midi->work);
  631. }
  632. static int f_midi_out_open(struct snd_rawmidi_substream *substream)
  633. {
  634. struct f_midi *midi = substream->rmidi->private_data;
  635. if (substream->number >= MAX_PORTS)
  636. return -EINVAL;
  637. VDBG(midi, "%s()\n", __func__);
  638. midi->out_substream[substream->number] = substream;
  639. return 0;
  640. }
  641. static int f_midi_out_close(struct snd_rawmidi_substream *substream)
  642. {
  643. struct f_midi *midi = substream->rmidi->private_data;
  644. VDBG(midi, "%s()\n", __func__);
  645. return 0;
  646. }
  647. static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
  648. {
  649. struct f_midi *midi = substream->rmidi->private_data;
  650. VDBG(midi, "%s()\n", __func__);
  651. if (up)
  652. set_bit(substream->number, &midi->out_triggered);
  653. else
  654. clear_bit(substream->number, &midi->out_triggered);
  655. }
  656. static const struct snd_rawmidi_ops gmidi_in_ops = {
  657. .open = f_midi_in_open,
  658. .close = f_midi_in_close,
  659. .trigger = f_midi_in_trigger,
  660. };
  661. static const struct snd_rawmidi_ops gmidi_out_ops = {
  662. .open = f_midi_out_open,
  663. .close = f_midi_out_close,
  664. .trigger = f_midi_out_trigger
  665. };
  666. static inline void f_midi_unregister_card(struct f_midi *midi)
  667. {
  668. if (midi->card) {
  669. snd_card_free(midi->card);
  670. midi->card = NULL;
  671. }
  672. }
  673. /* register as a sound "card" */
  674. static int f_midi_register_card(struct f_midi *midi)
  675. {
  676. struct snd_card *card;
  677. struct snd_rawmidi *rmidi;
  678. int err;
  679. static struct snd_device_ops ops = {
  680. .dev_free = f_midi_snd_free,
  681. };
  682. err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
  683. THIS_MODULE, 0, &card);
  684. if (err < 0) {
  685. ERROR(midi, "snd_card_new() failed\n");
  686. goto fail;
  687. }
  688. midi->card = card;
  689. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
  690. if (err < 0) {
  691. ERROR(midi, "snd_device_new() failed: error %d\n", err);
  692. goto fail;
  693. }
  694. strcpy(card->driver, f_midi_longname);
  695. strcpy(card->longname, f_midi_longname);
  696. strcpy(card->shortname, f_midi_shortname);
  697. /* Set up rawmidi */
  698. snd_component_add(card, "MIDI");
  699. err = snd_rawmidi_new(card, card->longname, 0,
  700. midi->out_ports, midi->in_ports, &rmidi);
  701. if (err < 0) {
  702. ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
  703. goto fail;
  704. }
  705. midi->rmidi = rmidi;
  706. midi->in_last_port = 0;
  707. strcpy(rmidi->name, card->shortname);
  708. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  709. SNDRV_RAWMIDI_INFO_INPUT |
  710. SNDRV_RAWMIDI_INFO_DUPLEX;
  711. rmidi->private_data = midi;
  712. rmidi->private_free = f_midi_rmidi_free;
  713. midi->free_ref++;
  714. /*
  715. * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
  716. * It's an upside-down world being a gadget.
  717. */
  718. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
  719. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
  720. /* register it - we're ready to go */
  721. err = snd_card_register(card);
  722. if (err < 0) {
  723. ERROR(midi, "snd_card_register() failed\n");
  724. goto fail;
  725. }
  726. VDBG(midi, "%s() finished ok\n", __func__);
  727. return 0;
  728. fail:
  729. f_midi_unregister_card(midi);
  730. return err;
  731. }
  732. /* MIDI function driver setup/binding */
  733. static int f_midi_bind(struct usb_configuration *c, struct usb_function *f)
  734. {
  735. struct usb_descriptor_header **midi_function;
  736. struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
  737. struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
  738. struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
  739. struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
  740. struct usb_composite_dev *cdev = c->cdev;
  741. struct f_midi *midi = func_to_midi(f);
  742. struct usb_string *us;
  743. int status, n, jack = 1, i = 0, endpoint_descriptor_index = 0;
  744. midi->gadget = cdev->gadget;
  745. INIT_WORK(&midi->work, f_midi_in_work);
  746. status = f_midi_register_card(midi);
  747. if (status < 0)
  748. goto fail_register;
  749. /* maybe allocate device-global string ID */
  750. us = usb_gstrings_attach(c->cdev, midi_strings,
  751. ARRAY_SIZE(midi_string_defs));
  752. if (IS_ERR(us)) {
  753. status = PTR_ERR(us);
  754. goto fail;
  755. }
  756. ac_interface_desc.iInterface = us[STRING_FUNC_IDX].id;
  757. /* We have two interfaces, AudioControl and MIDIStreaming */
  758. status = usb_interface_id(c, f);
  759. if (status < 0)
  760. goto fail;
  761. ac_interface_desc.bInterfaceNumber = status;
  762. status = usb_interface_id(c, f);
  763. if (status < 0)
  764. goto fail;
  765. ms_interface_desc.bInterfaceNumber = status;
  766. ac_header_desc.baInterfaceNr[0] = status;
  767. midi->ms_id = status;
  768. status = -ENODEV;
  769. /* allocate instance-specific endpoints */
  770. midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
  771. if (!midi->in_ep)
  772. goto fail;
  773. midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
  774. if (!midi->out_ep)
  775. goto fail;
  776. /* allocate temporary function list */
  777. midi_function = kcalloc((MAX_PORTS * 4) + 11, sizeof(*midi_function),
  778. GFP_KERNEL);
  779. if (!midi_function) {
  780. status = -ENOMEM;
  781. goto fail;
  782. }
  783. /*
  784. * construct the function's descriptor set. As the number of
  785. * input and output MIDI ports is configurable, we have to do
  786. * it that way.
  787. */
  788. /* add the headers - these are always the same */
  789. midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
  790. midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
  791. midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
  792. /* calculate the header's wTotalLength */
  793. n = USB_DT_MS_HEADER_SIZE
  794. + (midi->in_ports + midi->out_ports) *
  795. (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
  796. ms_header_desc.wTotalLength = cpu_to_le16(n);
  797. midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
  798. /* configure the external IN jacks, each linked to an embedded OUT jack */
  799. for (n = 0; n < midi->in_ports; n++) {
  800. struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
  801. struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
  802. in_ext->bLength = USB_DT_MIDI_IN_SIZE;
  803. in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  804. in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  805. in_ext->bJackType = USB_MS_EXTERNAL;
  806. in_ext->bJackID = jack++;
  807. in_ext->iJack = 0;
  808. midi_function[i++] = (struct usb_descriptor_header *) in_ext;
  809. out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
  810. out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  811. out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  812. out_emb->bJackType = USB_MS_EMBEDDED;
  813. out_emb->bJackID = jack++;
  814. out_emb->bNrInputPins = 1;
  815. out_emb->pins[0].baSourcePin = 1;
  816. out_emb->pins[0].baSourceID = in_ext->bJackID;
  817. out_emb->iJack = 0;
  818. midi_function[i++] = (struct usb_descriptor_header *) out_emb;
  819. /* link it to the endpoint */
  820. ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
  821. }
  822. /* configure the external OUT jacks, each linked to an embedded IN jack */
  823. for (n = 0; n < midi->out_ports; n++) {
  824. struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
  825. struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
  826. in_emb->bLength = USB_DT_MIDI_IN_SIZE;
  827. in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  828. in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  829. in_emb->bJackType = USB_MS_EMBEDDED;
  830. in_emb->bJackID = jack++;
  831. in_emb->iJack = 0;
  832. midi_function[i++] = (struct usb_descriptor_header *) in_emb;
  833. out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
  834. out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  835. out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  836. out_ext->bJackType = USB_MS_EXTERNAL;
  837. out_ext->bJackID = jack++;
  838. out_ext->bNrInputPins = 1;
  839. out_ext->iJack = 0;
  840. out_ext->pins[0].baSourceID = in_emb->bJackID;
  841. out_ext->pins[0].baSourcePin = 1;
  842. midi_function[i++] = (struct usb_descriptor_header *) out_ext;
  843. /* link it to the endpoint */
  844. ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
  845. }
  846. /* configure the endpoint descriptors ... */
  847. ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
  848. ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
  849. ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
  850. ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
  851. /* ... and add them to the list */
  852. endpoint_descriptor_index = i;
  853. midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
  854. midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
  855. midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
  856. midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
  857. midi_function[i++] = NULL;
  858. /*
  859. * support all relevant hardware speeds... we expect that when
  860. * hardware is dual speed, all bulk-capable endpoints work at
  861. * both speeds
  862. */
  863. /* copy descriptors, and track endpoint copies */
  864. f->fs_descriptors = usb_copy_descriptors(midi_function);
  865. if (!f->fs_descriptors)
  866. goto fail_f_midi;
  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. 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. kfree(midi_function);
  891. return 0;
  892. fail_f_midi:
  893. kfree(midi_function);
  894. usb_free_all_descriptors(f);
  895. fail:
  896. f_midi_unregister_card(midi);
  897. fail_register:
  898. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  899. return status;
  900. }
  901. static inline struct f_midi_opts *to_f_midi_opts(struct config_item *item)
  902. {
  903. return container_of(to_config_group(item), struct f_midi_opts,
  904. func_inst.group);
  905. }
  906. static void midi_attr_release(struct config_item *item)
  907. {
  908. struct f_midi_opts *opts = to_f_midi_opts(item);
  909. usb_put_function_instance(&opts->func_inst);
  910. }
  911. static struct configfs_item_operations midi_item_ops = {
  912. .release = midi_attr_release,
  913. };
  914. #define F_MIDI_OPT(name, test_limit, limit) \
  915. static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
  916. { \
  917. struct f_midi_opts *opts = to_f_midi_opts(item); \
  918. int result; \
  919. \
  920. mutex_lock(&opts->lock); \
  921. result = sprintf(page, "%u\n", opts->name); \
  922. mutex_unlock(&opts->lock); \
  923. \
  924. return result; \
  925. } \
  926. \
  927. static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
  928. const char *page, size_t len) \
  929. { \
  930. struct f_midi_opts *opts = to_f_midi_opts(item); \
  931. int ret; \
  932. u32 num; \
  933. \
  934. mutex_lock(&opts->lock); \
  935. if (opts->refcnt > 1) { \
  936. ret = -EBUSY; \
  937. goto end; \
  938. } \
  939. \
  940. ret = kstrtou32(page, 0, &num); \
  941. if (ret) \
  942. goto end; \
  943. \
  944. if (test_limit && num > limit) { \
  945. ret = -EINVAL; \
  946. goto end; \
  947. } \
  948. opts->name = num; \
  949. ret = len; \
  950. \
  951. end: \
  952. mutex_unlock(&opts->lock); \
  953. return ret; \
  954. } \
  955. \
  956. CONFIGFS_ATTR(f_midi_opts_, name);
  957. #define F_MIDI_OPT_SIGNED(name, test_limit, limit) \
  958. static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
  959. { \
  960. struct f_midi_opts *opts = to_f_midi_opts(item); \
  961. int result; \
  962. \
  963. mutex_lock(&opts->lock); \
  964. result = sprintf(page, "%d\n", opts->name); \
  965. mutex_unlock(&opts->lock); \
  966. \
  967. return result; \
  968. } \
  969. \
  970. static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
  971. const char *page, size_t len) \
  972. { \
  973. struct f_midi_opts *opts = to_f_midi_opts(item); \
  974. int ret; \
  975. s32 num; \
  976. \
  977. mutex_lock(&opts->lock); \
  978. if (opts->refcnt > 1) { \
  979. ret = -EBUSY; \
  980. goto end; \
  981. } \
  982. \
  983. ret = kstrtos32(page, 0, &num); \
  984. if (ret) \
  985. goto end; \
  986. \
  987. if (test_limit && num > limit) { \
  988. ret = -EINVAL; \
  989. goto end; \
  990. } \
  991. opts->name = num; \
  992. ret = len; \
  993. \
  994. end: \
  995. mutex_unlock(&opts->lock); \
  996. return ret; \
  997. } \
  998. \
  999. CONFIGFS_ATTR(f_midi_opts_, name);
  1000. F_MIDI_OPT_SIGNED(index, true, SNDRV_CARDS);
  1001. F_MIDI_OPT(buflen, false, 0);
  1002. F_MIDI_OPT(qlen, false, 0);
  1003. F_MIDI_OPT(in_ports, true, MAX_PORTS);
  1004. F_MIDI_OPT(out_ports, true, MAX_PORTS);
  1005. static ssize_t f_midi_opts_id_show(struct config_item *item, char *page)
  1006. {
  1007. struct f_midi_opts *opts = to_f_midi_opts(item);
  1008. int result;
  1009. mutex_lock(&opts->lock);
  1010. if (opts->id) {
  1011. result = strlcpy(page, opts->id, PAGE_SIZE);
  1012. } else {
  1013. page[0] = 0;
  1014. result = 0;
  1015. }
  1016. mutex_unlock(&opts->lock);
  1017. return result;
  1018. }
  1019. static ssize_t f_midi_opts_id_store(struct config_item *item,
  1020. const char *page, size_t len)
  1021. {
  1022. struct f_midi_opts *opts = to_f_midi_opts(item);
  1023. int ret;
  1024. char *c;
  1025. mutex_lock(&opts->lock);
  1026. if (opts->refcnt > 1) {
  1027. ret = -EBUSY;
  1028. goto end;
  1029. }
  1030. c = kstrndup(page, len, GFP_KERNEL);
  1031. if (!c) {
  1032. ret = -ENOMEM;
  1033. goto end;
  1034. }
  1035. if (opts->id_allocated)
  1036. kfree(opts->id);
  1037. opts->id = c;
  1038. opts->id_allocated = true;
  1039. ret = len;
  1040. end:
  1041. mutex_unlock(&opts->lock);
  1042. return ret;
  1043. }
  1044. CONFIGFS_ATTR(f_midi_opts_, id);
  1045. static struct configfs_attribute *midi_attrs[] = {
  1046. &f_midi_opts_attr_index,
  1047. &f_midi_opts_attr_buflen,
  1048. &f_midi_opts_attr_qlen,
  1049. &f_midi_opts_attr_in_ports,
  1050. &f_midi_opts_attr_out_ports,
  1051. &f_midi_opts_attr_id,
  1052. NULL,
  1053. };
  1054. static const struct config_item_type midi_func_type = {
  1055. .ct_item_ops = &midi_item_ops,
  1056. .ct_attrs = midi_attrs,
  1057. .ct_owner = THIS_MODULE,
  1058. };
  1059. static void f_midi_free_inst(struct usb_function_instance *f)
  1060. {
  1061. struct f_midi_opts *opts;
  1062. bool free = false;
  1063. opts = container_of(f, struct f_midi_opts, func_inst);
  1064. mutex_lock(&opts->lock);
  1065. if (!--opts->refcnt) {
  1066. free = true;
  1067. }
  1068. mutex_unlock(&opts->lock);
  1069. if (free) {
  1070. if (opts->id_allocated)
  1071. kfree(opts->id);
  1072. kfree(opts);
  1073. }
  1074. }
  1075. #ifdef CONFIG_USB_CONFIGFS_UEVENT
  1076. extern struct device *create_function_device(char *name);
  1077. static ssize_t alsa_show(struct device *dev,
  1078. struct device_attribute *attr, char *buf)
  1079. {
  1080. struct usb_function_instance *fi_midi = dev_get_drvdata(dev);
  1081. struct f_midi *midi;
  1082. if (!fi_midi->f)
  1083. dev_warn(dev, "f_midi: function not set\n");
  1084. if (fi_midi && fi_midi->f) {
  1085. midi = func_to_midi(fi_midi->f);
  1086. if (midi->rmidi && midi->card && midi->rmidi->card)
  1087. return sprintf(buf, "%d %d\n",
  1088. midi->rmidi->card->number, midi->rmidi->device);
  1089. }
  1090. /* print PCM card and device numbers */
  1091. return sprintf(buf, "%d %d\n", -1, -1);
  1092. }
  1093. static DEVICE_ATTR(alsa, S_IRUGO, alsa_show, NULL);
  1094. static struct device_attribute *alsa_function_attributes[] = {
  1095. &dev_attr_alsa,
  1096. NULL
  1097. };
  1098. static int create_alsa_device(struct usb_function_instance *fi)
  1099. {
  1100. struct device *dev;
  1101. struct device_attribute **attrs;
  1102. struct device_attribute *attr;
  1103. int err = 0;
  1104. dev = create_function_device("f_midi");
  1105. if (IS_ERR(dev))
  1106. return PTR_ERR(dev);
  1107. attrs = alsa_function_attributes;
  1108. if (attrs) {
  1109. while ((attr = *attrs++) && !err)
  1110. err = device_create_file(dev, attr);
  1111. if (err) {
  1112. device_destroy(dev->class, dev->devt);
  1113. return -EINVAL;
  1114. }
  1115. }
  1116. dev_set_drvdata(dev, fi);
  1117. return 0;
  1118. }
  1119. #else
  1120. static int create_alsa_device(struct usb_function_instance *fi)
  1121. {
  1122. return 0;
  1123. }
  1124. #endif
  1125. static struct usb_function_instance *f_midi_alloc_inst(void)
  1126. {
  1127. struct f_midi_opts *opts;
  1128. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1129. if (!opts)
  1130. return ERR_PTR(-ENOMEM);
  1131. mutex_init(&opts->lock);
  1132. opts->func_inst.free_func_inst = f_midi_free_inst;
  1133. opts->index = SNDRV_DEFAULT_IDX1;
  1134. opts->id = SNDRV_DEFAULT_STR1;
  1135. opts->buflen = 512;
  1136. opts->qlen = 32;
  1137. opts->in_ports = 1;
  1138. opts->out_ports = 1;
  1139. opts->refcnt = 1;
  1140. if (create_alsa_device(&opts->func_inst)) {
  1141. kfree(opts);
  1142. return ERR_PTR(-ENODEV);
  1143. }
  1144. config_group_init_type_name(&opts->func_inst.group, "",
  1145. &midi_func_type);
  1146. return &opts->func_inst;
  1147. }
  1148. static void f_midi_free(struct usb_function *f)
  1149. {
  1150. struct f_midi *midi;
  1151. struct f_midi_opts *opts;
  1152. bool free = false;
  1153. midi = func_to_midi(f);
  1154. opts = container_of(f->fi, struct f_midi_opts, func_inst);
  1155. mutex_lock(&opts->lock);
  1156. if (!--midi->free_ref) {
  1157. kfree(midi->id);
  1158. kfifo_free(&midi->in_req_fifo);
  1159. kfree(midi);
  1160. free = true;
  1161. opts->func_inst.f = NULL;
  1162. }
  1163. mutex_unlock(&opts->lock);
  1164. if (free)
  1165. f_midi_free_inst(&opts->func_inst);
  1166. }
  1167. static void f_midi_rmidi_free(struct snd_rawmidi *rmidi)
  1168. {
  1169. f_midi_free(rmidi->private_data);
  1170. }
  1171. static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
  1172. {
  1173. struct usb_composite_dev *cdev = f->config->cdev;
  1174. struct f_midi *midi = func_to_midi(f);
  1175. struct snd_card *card;
  1176. DBG(cdev, "unbind\n");
  1177. /* just to be sure */
  1178. f_midi_disable(f);
  1179. card = midi->card;
  1180. midi->card = NULL;
  1181. if (card)
  1182. snd_card_free_when_closed(card);
  1183. usb_free_all_descriptors(f);
  1184. }
  1185. static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
  1186. {
  1187. struct f_midi *midi = NULL;
  1188. struct f_midi_opts *opts;
  1189. int status, i;
  1190. opts = container_of(fi, struct f_midi_opts, func_inst);
  1191. mutex_lock(&opts->lock);
  1192. /* sanity check */
  1193. if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) {
  1194. status = -EINVAL;
  1195. goto setup_fail;
  1196. }
  1197. /* allocate and initialize one new instance */
  1198. midi = kzalloc(struct_size(midi, in_ports_array, opts->in_ports),
  1199. GFP_KERNEL);
  1200. if (!midi) {
  1201. status = -ENOMEM;
  1202. goto setup_fail;
  1203. }
  1204. for (i = 0; i < opts->in_ports; i++)
  1205. midi->in_ports_array[i].cable = i;
  1206. /* set up ALSA midi devices */
  1207. midi->id = kstrdup(opts->id, GFP_KERNEL);
  1208. if (opts->id && !midi->id) {
  1209. status = -ENOMEM;
  1210. goto midi_free;
  1211. }
  1212. midi->in_ports = opts->in_ports;
  1213. midi->out_ports = opts->out_ports;
  1214. midi->index = opts->index;
  1215. midi->buflen = opts->buflen;
  1216. midi->qlen = opts->qlen;
  1217. midi->in_last_port = 0;
  1218. midi->free_ref = 1;
  1219. status = kfifo_alloc(&midi->in_req_fifo, midi->qlen, GFP_KERNEL);
  1220. if (status)
  1221. goto midi_free;
  1222. spin_lock_init(&midi->transmit_lock);
  1223. ++opts->refcnt;
  1224. mutex_unlock(&opts->lock);
  1225. midi->func.name = "gmidi function";
  1226. midi->func.bind = f_midi_bind;
  1227. midi->func.unbind = f_midi_unbind;
  1228. midi->func.set_alt = f_midi_set_alt;
  1229. midi->func.disable = f_midi_disable;
  1230. midi->func.free_func = f_midi_free;
  1231. fi->f = &midi->func;
  1232. return &midi->func;
  1233. midi_free:
  1234. if (midi)
  1235. kfree(midi->id);
  1236. kfree(midi);
  1237. setup_fail:
  1238. mutex_unlock(&opts->lock);
  1239. return ERR_PTR(status);
  1240. }
  1241. DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc);