f_midi.c 29 KB

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