f_midi.c 29 KB

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