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. usb_ep_disable(ep);
  262. err = config_ep_by_speed(midi->gadget, f, ep);
  263. if (err) {
  264. ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
  265. return err;
  266. }
  267. err = usb_ep_enable(ep);
  268. if (err) {
  269. ERROR(cdev, "can't start %s: %d\n", ep->name, err);
  270. return err;
  271. }
  272. ep->driver_data = midi;
  273. return 0;
  274. }
  275. static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  276. {
  277. struct f_midi *midi = func_to_midi(f);
  278. struct usb_composite_dev *cdev = f->config->cdev;
  279. unsigned i;
  280. int err;
  281. /* For Control Device interface we do nothing */
  282. if (intf == 0)
  283. return 0;
  284. err = f_midi_start_ep(midi, f, midi->in_ep);
  285. if (err)
  286. return err;
  287. err = f_midi_start_ep(midi, f, midi->out_ep);
  288. if (err)
  289. return err;
  290. usb_ep_disable(midi->out_ep);
  291. err = config_ep_by_speed(midi->gadget, f, midi->out_ep);
  292. if (err) {
  293. ERROR(cdev, "can't configure %s: %d\n",
  294. midi->out_ep->name, err);
  295. return err;
  296. }
  297. err = usb_ep_enable(midi->out_ep);
  298. if (err) {
  299. ERROR(cdev, "can't start %s: %d\n",
  300. midi->out_ep->name, err);
  301. return err;
  302. }
  303. midi->out_ep->driver_data = midi;
  304. /* allocate a bunch of read buffers and queue them all at once. */
  305. for (i = 0; i < midi->qlen && err == 0; i++) {
  306. struct usb_request *req =
  307. midi_alloc_ep_req(midi->out_ep, midi->buflen);
  308. if (req == NULL)
  309. return -ENOMEM;
  310. req->complete = f_midi_complete;
  311. err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
  312. if (err) {
  313. ERROR(midi, "%s queue req: %d\n",
  314. midi->out_ep->name, err);
  315. free_ep_req(midi->out_ep, req);
  316. }
  317. }
  318. return 0;
  319. }
  320. static void f_midi_disable(struct usb_function *f)
  321. {
  322. struct f_midi *midi = func_to_midi(f);
  323. struct usb_composite_dev *cdev = f->config->cdev;
  324. DBG(cdev, "disable\n");
  325. /*
  326. * just disable endpoints, forcing completion of pending i/o.
  327. * all our completion handlers free their requests in this case.
  328. */
  329. usb_ep_disable(midi->in_ep);
  330. usb_ep_disable(midi->out_ep);
  331. }
  332. static int f_midi_snd_free(struct snd_device *device)
  333. {
  334. return 0;
  335. }
  336. static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
  337. uint8_t p1, uint8_t p2, uint8_t p3)
  338. {
  339. unsigned length = req->length;
  340. u8 *buf = (u8 *)req->buf + length;
  341. buf[0] = p0;
  342. buf[1] = p1;
  343. buf[2] = p2;
  344. buf[3] = p3;
  345. req->length = length + 4;
  346. }
  347. /*
  348. * Converts MIDI commands to USB MIDI packets.
  349. */
  350. static void f_midi_transmit_byte(struct usb_request *req,
  351. struct gmidi_in_port *port, uint8_t b)
  352. {
  353. uint8_t p0 = port->cable << 4;
  354. if (b >= 0xf8) {
  355. f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
  356. } else if (b >= 0xf0) {
  357. switch (b) {
  358. case 0xf0:
  359. port->data[0] = b;
  360. port->state = STATE_SYSEX_1;
  361. break;
  362. case 0xf1:
  363. case 0xf3:
  364. port->data[0] = b;
  365. port->state = STATE_1PARAM;
  366. break;
  367. case 0xf2:
  368. port->data[0] = b;
  369. port->state = STATE_2PARAM_1;
  370. break;
  371. case 0xf4:
  372. case 0xf5:
  373. port->state = STATE_UNKNOWN;
  374. break;
  375. case 0xf6:
  376. f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
  377. port->state = STATE_UNKNOWN;
  378. break;
  379. case 0xf7:
  380. switch (port->state) {
  381. case STATE_SYSEX_0:
  382. f_midi_transmit_packet(req,
  383. p0 | 0x05, 0xf7, 0, 0);
  384. break;
  385. case STATE_SYSEX_1:
  386. f_midi_transmit_packet(req,
  387. p0 | 0x06, port->data[0], 0xf7, 0);
  388. break;
  389. case STATE_SYSEX_2:
  390. f_midi_transmit_packet(req,
  391. p0 | 0x07, port->data[0],
  392. port->data[1], 0xf7);
  393. break;
  394. }
  395. port->state = STATE_UNKNOWN;
  396. break;
  397. }
  398. } else if (b >= 0x80) {
  399. port->data[0] = b;
  400. if (b >= 0xc0 && b <= 0xdf)
  401. port->state = STATE_1PARAM;
  402. else
  403. port->state = STATE_2PARAM_1;
  404. } else { /* b < 0x80 */
  405. switch (port->state) {
  406. case STATE_1PARAM:
  407. if (port->data[0] < 0xf0) {
  408. p0 |= port->data[0] >> 4;
  409. } else {
  410. p0 |= 0x02;
  411. port->state = STATE_UNKNOWN;
  412. }
  413. f_midi_transmit_packet(req, p0, port->data[0], b, 0);
  414. break;
  415. case STATE_2PARAM_1:
  416. port->data[1] = b;
  417. port->state = STATE_2PARAM_2;
  418. break;
  419. case STATE_2PARAM_2:
  420. if (port->data[0] < 0xf0) {
  421. p0 |= port->data[0] >> 4;
  422. port->state = STATE_2PARAM_1;
  423. } else {
  424. p0 |= 0x03;
  425. port->state = STATE_UNKNOWN;
  426. }
  427. f_midi_transmit_packet(req,
  428. p0, port->data[0], port->data[1], b);
  429. break;
  430. case STATE_SYSEX_0:
  431. port->data[0] = b;
  432. port->state = STATE_SYSEX_1;
  433. break;
  434. case STATE_SYSEX_1:
  435. port->data[1] = b;
  436. port->state = STATE_SYSEX_2;
  437. break;
  438. case STATE_SYSEX_2:
  439. f_midi_transmit_packet(req,
  440. p0 | 0x04, port->data[0], port->data[1], b);
  441. port->state = STATE_SYSEX_0;
  442. break;
  443. }
  444. }
  445. }
  446. static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
  447. {
  448. struct usb_ep *ep = midi->in_ep;
  449. int i;
  450. if (!ep)
  451. return;
  452. if (!req)
  453. req = midi_alloc_ep_req(ep, midi->buflen);
  454. if (!req) {
  455. ERROR(midi, "%s: alloc_ep_request failed\n", __func__);
  456. return;
  457. }
  458. req->length = 0;
  459. req->complete = f_midi_complete;
  460. for (i = 0; i < MAX_PORTS; i++) {
  461. struct gmidi_in_port *port = midi->in_port[i];
  462. struct snd_rawmidi_substream *substream = midi->in_substream[i];
  463. if (!port || !port->active || !substream)
  464. continue;
  465. while (req->length + 3 < midi->buflen) {
  466. uint8_t b;
  467. if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
  468. port->active = 0;
  469. break;
  470. }
  471. f_midi_transmit_byte(req, port, b);
  472. }
  473. }
  474. if (req->length > 0 && ep->enabled) {
  475. int err;
  476. err = usb_ep_queue(ep, req, GFP_ATOMIC);
  477. if (err < 0)
  478. ERROR(midi, "%s queue req: %d\n",
  479. midi->in_ep->name, err);
  480. } else {
  481. free_ep_req(ep, req);
  482. }
  483. }
  484. static void f_midi_in_tasklet(unsigned long data)
  485. {
  486. struct f_midi *midi = (struct f_midi *) data;
  487. f_midi_transmit(midi, NULL);
  488. }
  489. static int f_midi_in_open(struct snd_rawmidi_substream *substream)
  490. {
  491. struct f_midi *midi = substream->rmidi->private_data;
  492. if (!midi->in_port[substream->number])
  493. return -EINVAL;
  494. VDBG(midi, "%s()\n", __func__);
  495. midi->in_substream[substream->number] = substream;
  496. midi->in_port[substream->number]->state = STATE_UNKNOWN;
  497. return 0;
  498. }
  499. static int f_midi_in_close(struct snd_rawmidi_substream *substream)
  500. {
  501. struct f_midi *midi = substream->rmidi->private_data;
  502. VDBG(midi, "%s()\n", __func__);
  503. return 0;
  504. }
  505. static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
  506. {
  507. struct f_midi *midi = substream->rmidi->private_data;
  508. if (!midi->in_port[substream->number])
  509. return;
  510. VDBG(midi, "%s() %d\n", __func__, up);
  511. midi->in_port[substream->number]->active = up;
  512. if (up)
  513. tasklet_hi_schedule(&midi->tasklet);
  514. }
  515. static int f_midi_out_open(struct snd_rawmidi_substream *substream)
  516. {
  517. struct f_midi *midi = substream->rmidi->private_data;
  518. if (substream->number >= MAX_PORTS)
  519. return -EINVAL;
  520. VDBG(midi, "%s()\n", __func__);
  521. midi->out_substream[substream->number] = substream;
  522. return 0;
  523. }
  524. static int f_midi_out_close(struct snd_rawmidi_substream *substream)
  525. {
  526. struct f_midi *midi = substream->rmidi->private_data;
  527. VDBG(midi, "%s()\n", __func__);
  528. return 0;
  529. }
  530. static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
  531. {
  532. struct f_midi *midi = substream->rmidi->private_data;
  533. VDBG(midi, "%s()\n", __func__);
  534. if (up)
  535. set_bit(substream->number, &midi->out_triggered);
  536. else
  537. clear_bit(substream->number, &midi->out_triggered);
  538. }
  539. static struct snd_rawmidi_ops gmidi_in_ops = {
  540. .open = f_midi_in_open,
  541. .close = f_midi_in_close,
  542. .trigger = f_midi_in_trigger,
  543. };
  544. static struct snd_rawmidi_ops gmidi_out_ops = {
  545. .open = f_midi_out_open,
  546. .close = f_midi_out_close,
  547. .trigger = f_midi_out_trigger
  548. };
  549. static inline void f_midi_unregister_card(struct f_midi *midi)
  550. {
  551. if (midi->card) {
  552. snd_card_free(midi->card);
  553. midi->card = NULL;
  554. }
  555. }
  556. /* register as a sound "card" */
  557. static int f_midi_register_card(struct f_midi *midi)
  558. {
  559. struct snd_card *card;
  560. struct snd_rawmidi *rmidi;
  561. int err;
  562. static struct snd_device_ops ops = {
  563. .dev_free = f_midi_snd_free,
  564. };
  565. err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
  566. THIS_MODULE, 0, &card);
  567. if (err < 0) {
  568. ERROR(midi, "snd_card_new() failed\n");
  569. goto fail;
  570. }
  571. midi->card = card;
  572. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
  573. if (err < 0) {
  574. ERROR(midi, "snd_device_new() failed: error %d\n", err);
  575. goto fail;
  576. }
  577. strcpy(card->driver, f_midi_longname);
  578. strcpy(card->longname, f_midi_longname);
  579. strcpy(card->shortname, f_midi_shortname);
  580. /* Set up rawmidi */
  581. snd_component_add(card, "MIDI");
  582. err = snd_rawmidi_new(card, card->longname, 0,
  583. midi->out_ports, midi->in_ports, &rmidi);
  584. if (err < 0) {
  585. ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
  586. goto fail;
  587. }
  588. midi->rmidi = rmidi;
  589. strcpy(rmidi->name, card->shortname);
  590. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  591. SNDRV_RAWMIDI_INFO_INPUT |
  592. SNDRV_RAWMIDI_INFO_DUPLEX;
  593. rmidi->private_data = midi;
  594. /*
  595. * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
  596. * It's an upside-down world being a gadget.
  597. */
  598. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
  599. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
  600. /* register it - we're ready to go */
  601. err = snd_card_register(card);
  602. if (err < 0) {
  603. ERROR(midi, "snd_card_register() failed\n");
  604. goto fail;
  605. }
  606. VDBG(midi, "%s() finished ok\n", __func__);
  607. return 0;
  608. fail:
  609. f_midi_unregister_card(midi);
  610. return err;
  611. }
  612. /* MIDI function driver setup/binding */
  613. static int f_midi_bind(struct usb_configuration *c, struct usb_function *f)
  614. {
  615. struct usb_descriptor_header **midi_function;
  616. struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
  617. struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
  618. struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
  619. struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
  620. struct usb_composite_dev *cdev = c->cdev;
  621. struct f_midi *midi = func_to_midi(f);
  622. struct usb_string *us;
  623. int status, n, jack = 1, i = 0;
  624. midi->gadget = cdev->gadget;
  625. tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
  626. status = f_midi_register_card(midi);
  627. if (status < 0)
  628. goto fail_register;
  629. /* maybe allocate device-global string ID */
  630. us = usb_gstrings_attach(c->cdev, midi_strings,
  631. ARRAY_SIZE(midi_string_defs));
  632. if (IS_ERR(us)) {
  633. status = PTR_ERR(us);
  634. goto fail;
  635. }
  636. ac_interface_desc.iInterface = us[STRING_FUNC_IDX].id;
  637. /* We have two interfaces, AudioControl and MIDIStreaming */
  638. status = usb_interface_id(c, f);
  639. if (status < 0)
  640. goto fail;
  641. ac_interface_desc.bInterfaceNumber = status;
  642. status = usb_interface_id(c, f);
  643. if (status < 0)
  644. goto fail;
  645. ms_interface_desc.bInterfaceNumber = status;
  646. ac_header_desc.baInterfaceNr[0] = status;
  647. status = -ENODEV;
  648. /* allocate instance-specific endpoints */
  649. midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
  650. if (!midi->in_ep)
  651. goto fail;
  652. midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
  653. if (!midi->out_ep)
  654. goto fail;
  655. /* allocate temporary function list */
  656. midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
  657. GFP_KERNEL);
  658. if (!midi_function) {
  659. status = -ENOMEM;
  660. goto fail;
  661. }
  662. /*
  663. * construct the function's descriptor set. As the number of
  664. * input and output MIDI ports is configurable, we have to do
  665. * it that way.
  666. */
  667. /* add the headers - these are always the same */
  668. midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
  669. midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
  670. midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
  671. /* calculate the header's wTotalLength */
  672. n = USB_DT_MS_HEADER_SIZE
  673. + (midi->in_ports + midi->out_ports) *
  674. (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
  675. ms_header_desc.wTotalLength = cpu_to_le16(n);
  676. midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
  677. /* configure the external IN jacks, each linked to an embedded OUT jack */
  678. for (n = 0; n < midi->in_ports; n++) {
  679. struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
  680. struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
  681. in_ext->bLength = USB_DT_MIDI_IN_SIZE;
  682. in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  683. in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  684. in_ext->bJackType = USB_MS_EXTERNAL;
  685. in_ext->bJackID = jack++;
  686. in_ext->iJack = 0;
  687. midi_function[i++] = (struct usb_descriptor_header *) in_ext;
  688. out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
  689. out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  690. out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  691. out_emb->bJackType = USB_MS_EMBEDDED;
  692. out_emb->bJackID = jack++;
  693. out_emb->bNrInputPins = 1;
  694. out_emb->pins[0].baSourcePin = 1;
  695. out_emb->pins[0].baSourceID = in_ext->bJackID;
  696. out_emb->iJack = 0;
  697. midi_function[i++] = (struct usb_descriptor_header *) out_emb;
  698. /* link it to the endpoint */
  699. ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
  700. }
  701. /* configure the external OUT jacks, each linked to an embedded IN jack */
  702. for (n = 0; n < midi->out_ports; n++) {
  703. struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
  704. struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
  705. in_emb->bLength = USB_DT_MIDI_IN_SIZE;
  706. in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
  707. in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  708. in_emb->bJackType = USB_MS_EMBEDDED;
  709. in_emb->bJackID = jack++;
  710. in_emb->iJack = 0;
  711. midi_function[i++] = (struct usb_descriptor_header *) in_emb;
  712. out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
  713. out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
  714. out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  715. out_ext->bJackType = USB_MS_EXTERNAL;
  716. out_ext->bJackID = jack++;
  717. out_ext->bNrInputPins = 1;
  718. out_ext->iJack = 0;
  719. out_ext->pins[0].baSourceID = in_emb->bJackID;
  720. out_ext->pins[0].baSourcePin = 1;
  721. midi_function[i++] = (struct usb_descriptor_header *) out_ext;
  722. /* link it to the endpoint */
  723. ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
  724. }
  725. /* configure the endpoint descriptors ... */
  726. ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
  727. ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
  728. ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
  729. ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
  730. /* ... and add them to the list */
  731. midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
  732. midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
  733. midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
  734. midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
  735. midi_function[i++] = NULL;
  736. /*
  737. * support all relevant hardware speeds... we expect that when
  738. * hardware is dual speed, all bulk-capable endpoints work at
  739. * both speeds
  740. */
  741. /* copy descriptors, and track endpoint copies */
  742. f->fs_descriptors = usb_copy_descriptors(midi_function);
  743. if (!f->fs_descriptors)
  744. goto fail_f_midi;
  745. if (gadget_is_dualspeed(c->cdev->gadget)) {
  746. bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
  747. bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
  748. f->hs_descriptors = usb_copy_descriptors(midi_function);
  749. if (!f->hs_descriptors)
  750. goto fail_f_midi;
  751. }
  752. kfree(midi_function);
  753. return 0;
  754. fail_f_midi:
  755. kfree(midi_function);
  756. usb_free_descriptors(f->hs_descriptors);
  757. fail:
  758. f_midi_unregister_card(midi);
  759. fail_register:
  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. static void midi_attr_release(struct config_item *item)
  769. {
  770. struct f_midi_opts *opts = to_f_midi_opts(item);
  771. usb_put_function_instance(&opts->func_inst);
  772. }
  773. static struct configfs_item_operations midi_item_ops = {
  774. .release = midi_attr_release,
  775. };
  776. #define F_MIDI_OPT(name, test_limit, limit) \
  777. static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
  778. { \
  779. struct f_midi_opts *opts = to_f_midi_opts(item); \
  780. int result; \
  781. \
  782. mutex_lock(&opts->lock); \
  783. result = sprintf(page, "%d\n", opts->name); \
  784. mutex_unlock(&opts->lock); \
  785. \
  786. return result; \
  787. } \
  788. \
  789. static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
  790. const char *page, size_t len) \
  791. { \
  792. struct f_midi_opts *opts = to_f_midi_opts(item); \
  793. int ret; \
  794. u32 num; \
  795. \
  796. mutex_lock(&opts->lock); \
  797. if (opts->refcnt) { \
  798. ret = -EBUSY; \
  799. goto end; \
  800. } \
  801. \
  802. ret = kstrtou32(page, 0, &num); \
  803. if (ret) \
  804. goto end; \
  805. \
  806. if (test_limit && num > limit) { \
  807. ret = -EINVAL; \
  808. goto end; \
  809. } \
  810. opts->name = num; \
  811. ret = len; \
  812. \
  813. end: \
  814. mutex_unlock(&opts->lock); \
  815. return ret; \
  816. } \
  817. \
  818. CONFIGFS_ATTR(f_midi_opts_, name);
  819. F_MIDI_OPT(index, true, SNDRV_CARDS);
  820. F_MIDI_OPT(buflen, false, 0);
  821. F_MIDI_OPT(qlen, false, 0);
  822. F_MIDI_OPT(in_ports, true, MAX_PORTS);
  823. F_MIDI_OPT(out_ports, true, MAX_PORTS);
  824. static ssize_t f_midi_opts_id_show(struct config_item *item, char *page)
  825. {
  826. struct f_midi_opts *opts = to_f_midi_opts(item);
  827. int result;
  828. mutex_lock(&opts->lock);
  829. if (opts->id) {
  830. result = strlcpy(page, opts->id, PAGE_SIZE);
  831. } else {
  832. page[0] = 0;
  833. result = 0;
  834. }
  835. mutex_unlock(&opts->lock);
  836. return result;
  837. }
  838. static ssize_t f_midi_opts_id_store(struct config_item *item,
  839. const char *page, size_t len)
  840. {
  841. struct f_midi_opts *opts = to_f_midi_opts(item);
  842. int ret;
  843. char *c;
  844. mutex_lock(&opts->lock);
  845. if (opts->refcnt) {
  846. ret = -EBUSY;
  847. goto end;
  848. }
  849. c = kstrndup(page, len, GFP_KERNEL);
  850. if (!c) {
  851. ret = -ENOMEM;
  852. goto end;
  853. }
  854. if (opts->id_allocated)
  855. kfree(opts->id);
  856. opts->id = c;
  857. opts->id_allocated = true;
  858. ret = len;
  859. end:
  860. mutex_unlock(&opts->lock);
  861. return ret;
  862. }
  863. CONFIGFS_ATTR(f_midi_opts_, id);
  864. static struct configfs_attribute *midi_attrs[] = {
  865. &f_midi_opts_attr_index,
  866. &f_midi_opts_attr_buflen,
  867. &f_midi_opts_attr_qlen,
  868. &f_midi_opts_attr_in_ports,
  869. &f_midi_opts_attr_out_ports,
  870. &f_midi_opts_attr_id,
  871. NULL,
  872. };
  873. static struct config_item_type midi_func_type = {
  874. .ct_item_ops = &midi_item_ops,
  875. .ct_attrs = midi_attrs,
  876. .ct_owner = THIS_MODULE,
  877. };
  878. static void f_midi_free_inst(struct usb_function_instance *f)
  879. {
  880. struct f_midi_opts *opts;
  881. opts = container_of(f, struct f_midi_opts, func_inst);
  882. if (opts->id_allocated)
  883. kfree(opts->id);
  884. kfree(opts);
  885. }
  886. static struct usb_function_instance *f_midi_alloc_inst(void)
  887. {
  888. struct f_midi_opts *opts;
  889. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  890. if (!opts)
  891. return ERR_PTR(-ENOMEM);
  892. mutex_init(&opts->lock);
  893. opts->func_inst.free_func_inst = f_midi_free_inst;
  894. opts->index = SNDRV_DEFAULT_IDX1;
  895. opts->id = SNDRV_DEFAULT_STR1;
  896. opts->buflen = 256;
  897. opts->qlen = 32;
  898. opts->in_ports = 1;
  899. opts->out_ports = 1;
  900. config_group_init_type_name(&opts->func_inst.group, "",
  901. &midi_func_type);
  902. return &opts->func_inst;
  903. }
  904. static void f_midi_free(struct usb_function *f)
  905. {
  906. struct f_midi *midi;
  907. struct f_midi_opts *opts;
  908. int i;
  909. midi = func_to_midi(f);
  910. opts = container_of(f->fi, struct f_midi_opts, func_inst);
  911. kfree(midi->id);
  912. mutex_lock(&opts->lock);
  913. for (i = opts->in_ports - 1; i >= 0; --i)
  914. kfree(midi->in_port[i]);
  915. kfree(midi);
  916. --opts->refcnt;
  917. mutex_unlock(&opts->lock);
  918. }
  919. static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
  920. {
  921. struct usb_composite_dev *cdev = f->config->cdev;
  922. struct f_midi *midi = func_to_midi(f);
  923. struct snd_card *card;
  924. DBG(cdev, "unbind\n");
  925. /* just to be sure */
  926. f_midi_disable(f);
  927. card = midi->card;
  928. midi->card = NULL;
  929. if (card)
  930. snd_card_free(card);
  931. usb_free_all_descriptors(f);
  932. }
  933. static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
  934. {
  935. struct f_midi *midi;
  936. struct f_midi_opts *opts;
  937. int status, i;
  938. opts = container_of(fi, struct f_midi_opts, func_inst);
  939. mutex_lock(&opts->lock);
  940. /* sanity check */
  941. if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) {
  942. mutex_unlock(&opts->lock);
  943. return ERR_PTR(-EINVAL);
  944. }
  945. /* allocate and initialize one new instance */
  946. midi = kzalloc(sizeof(*midi), GFP_KERNEL);
  947. if (!midi) {
  948. mutex_unlock(&opts->lock);
  949. return ERR_PTR(-ENOMEM);
  950. }
  951. for (i = 0; i < opts->in_ports; i++) {
  952. struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
  953. if (!port) {
  954. status = -ENOMEM;
  955. mutex_unlock(&opts->lock);
  956. goto setup_fail;
  957. }
  958. port->midi = midi;
  959. port->active = 0;
  960. port->cable = i;
  961. midi->in_port[i] = port;
  962. }
  963. /* set up ALSA midi devices */
  964. midi->id = kstrdup(opts->id, GFP_KERNEL);
  965. if (opts->id && !midi->id) {
  966. status = -ENOMEM;
  967. mutex_unlock(&opts->lock);
  968. goto setup_fail;
  969. }
  970. midi->in_ports = opts->in_ports;
  971. midi->out_ports = opts->out_ports;
  972. midi->index = opts->index;
  973. midi->buflen = opts->buflen;
  974. midi->qlen = opts->qlen;
  975. ++opts->refcnt;
  976. mutex_unlock(&opts->lock);
  977. midi->func.name = "gmidi function";
  978. midi->func.bind = f_midi_bind;
  979. midi->func.unbind = f_midi_unbind;
  980. midi->func.set_alt = f_midi_set_alt;
  981. midi->func.disable = f_midi_disable;
  982. midi->func.free_func = f_midi_free;
  983. return &midi->func;
  984. setup_fail:
  985. for (--i; i >= 0; i--)
  986. kfree(midi->in_port[i]);
  987. kfree(midi);
  988. return ERR_PTR(status);
  989. }
  990. DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc);