dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / usb / gadget / function / f_midi.c
blob5ead414586a1442d70f8349933d0643a11c9b942
1 /*
2 * f_midi.c -- USB MIDI class function driver
4 * Copyright (C) 2006 Thumtronics Pty Ltd.
5 * Developed for Thumtronics by Grey Innovation
6 * Ben Williamson <ben.williamson@greyinnovation.com>
8 * Rewritten for the composite framework
9 * Copyright (C) 2011 Daniel Mack <zonque@gmail.com>
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
15 * and drivers/usb/gadget/midi.c,
16 * Copyright (C) 2006 Thumtronics Pty Ltd.
17 * Ben Williamson <ben.williamson@greyinnovation.com>
19 * Licensed under the GPL-2 or later.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/device.h>
27 #include <sound/core.h>
28 #include <sound/initval.h>
29 #include <sound/rawmidi.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/gadget.h>
33 #include <linux/usb/audio.h>
34 #include <linux/usb/midi.h>
36 #include "u_f.h"
37 #include "u_midi.h"
39 MODULE_AUTHOR("Ben Williamson");
40 MODULE_LICENSE("GPL v2");
42 static const char f_midi_shortname[] = "f_midi";
43 static const char f_midi_longname[] = "MIDI Gadget";
46 * We can only handle 16 cables on one single endpoint, as cable numbers are
47 * stored in 4-bit fields. And as the interface currently only holds one
48 * single endpoint, this is the maximum number of ports we can allow.
50 #define MAX_PORTS 16
53 * This is a gadget, and the IN/OUT naming is from the host's perspective.
54 * USB -> OUT endpoint -> rawmidi
55 * USB <- IN endpoint <- rawmidi
57 struct gmidi_in_port {
58 struct f_midi *midi;
59 int active;
60 uint8_t cable;
61 uint8_t state;
62 #define STATE_UNKNOWN 0
63 #define STATE_1PARAM 1
64 #define STATE_2PARAM_1 2
65 #define STATE_2PARAM_2 3
66 #define STATE_SYSEX_0 4
67 #define STATE_SYSEX_1 5
68 #define STATE_SYSEX_2 6
69 uint8_t data[2];
72 struct f_midi {
73 struct usb_function func;
74 struct usb_gadget *gadget;
75 struct usb_ep *in_ep, *out_ep;
76 struct snd_card *card;
77 struct snd_rawmidi *rmidi;
79 struct snd_rawmidi_substream *in_substream[MAX_PORTS];
80 struct snd_rawmidi_substream *out_substream[MAX_PORTS];
81 struct gmidi_in_port *in_port[MAX_PORTS];
83 unsigned long out_triggered;
84 struct tasklet_struct tasklet;
85 unsigned int in_ports;
86 unsigned int out_ports;
87 int index;
88 char *id;
89 unsigned int buflen, qlen;
92 static inline struct f_midi *func_to_midi(struct usb_function *f)
94 return container_of(f, struct f_midi, func);
97 static void f_midi_transmit(struct f_midi *midi, struct usb_request *req);
99 DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
100 DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
101 DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
103 /* B.3.1 Standard AC Interface Descriptor */
104 static struct usb_interface_descriptor ac_interface_desc = {
105 .bLength = USB_DT_INTERFACE_SIZE,
106 .bDescriptorType = USB_DT_INTERFACE,
107 /* .bInterfaceNumber = DYNAMIC */
108 /* .bNumEndpoints = DYNAMIC */
109 .bInterfaceClass = USB_CLASS_AUDIO,
110 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
111 /* .iInterface = DYNAMIC */
114 /* B.3.2 Class-Specific AC Interface Descriptor */
115 static struct uac1_ac_header_descriptor_1 ac_header_desc = {
116 .bLength = UAC_DT_AC_HEADER_SIZE(1),
117 .bDescriptorType = USB_DT_CS_INTERFACE,
118 .bDescriptorSubtype = USB_MS_HEADER,
119 .bcdADC = cpu_to_le16(0x0100),
120 .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)),
121 .bInCollection = 1,
122 /* .baInterfaceNr = DYNAMIC */
125 /* B.4.1 Standard MS Interface Descriptor */
126 static struct usb_interface_descriptor ms_interface_desc = {
127 .bLength = USB_DT_INTERFACE_SIZE,
128 .bDescriptorType = USB_DT_INTERFACE,
129 /* .bInterfaceNumber = DYNAMIC */
130 .bNumEndpoints = 2,
131 .bInterfaceClass = USB_CLASS_AUDIO,
132 .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
133 /* .iInterface = DYNAMIC */
136 /* B.4.2 Class-Specific MS Interface Descriptor */
137 static struct usb_ms_header_descriptor ms_header_desc = {
138 .bLength = USB_DT_MS_HEADER_SIZE,
139 .bDescriptorType = USB_DT_CS_INTERFACE,
140 .bDescriptorSubtype = USB_MS_HEADER,
141 .bcdMSC = cpu_to_le16(0x0100),
142 /* .wTotalLength = DYNAMIC */
145 /* B.5.1 Standard Bulk OUT Endpoint Descriptor */
146 static struct usb_endpoint_descriptor bulk_out_desc = {
147 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
148 .bDescriptorType = USB_DT_ENDPOINT,
149 .bEndpointAddress = USB_DIR_OUT,
150 .bmAttributes = USB_ENDPOINT_XFER_BULK,
153 /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */
154 static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
155 /* .bLength = DYNAMIC */
156 .bDescriptorType = USB_DT_CS_ENDPOINT,
157 .bDescriptorSubtype = USB_MS_GENERAL,
158 /* .bNumEmbMIDIJack = DYNAMIC */
159 /* .baAssocJackID = DYNAMIC */
162 /* B.6.1 Standard Bulk IN Endpoint Descriptor */
163 static struct usb_endpoint_descriptor bulk_in_desc = {
164 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = USB_DIR_IN,
167 .bmAttributes = USB_ENDPOINT_XFER_BULK,
170 /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */
171 static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
172 /* .bLength = DYNAMIC */
173 .bDescriptorType = USB_DT_CS_ENDPOINT,
174 .bDescriptorSubtype = USB_MS_GENERAL,
175 /* .bNumEmbMIDIJack = DYNAMIC */
176 /* .baAssocJackID = DYNAMIC */
179 /* string IDs are assigned dynamically */
181 #define STRING_FUNC_IDX 0
183 static struct usb_string midi_string_defs[] = {
184 [STRING_FUNC_IDX].s = "MIDI function",
185 { } /* end of list */
188 static struct usb_gadget_strings midi_stringtab = {
189 .language = 0x0409, /* en-us */
190 .strings = midi_string_defs,
193 static struct usb_gadget_strings *midi_strings[] = {
194 &midi_stringtab,
195 NULL,
198 static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep,
199 unsigned length)
201 return alloc_ep_req(ep, length, length);
204 static const uint8_t f_midi_cin_length[] = {
205 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
209 * Receives a chunk of MIDI data.
211 static void f_midi_read_data(struct usb_ep *ep, int cable,
212 uint8_t *data, int length)
214 struct f_midi *midi = ep->driver_data;
215 struct snd_rawmidi_substream *substream = midi->out_substream[cable];
217 if (!substream)
218 /* Nobody is listening - throw it on the floor. */
219 return;
221 if (!test_bit(cable, &midi->out_triggered))
222 return;
224 snd_rawmidi_receive(substream, data, length);
227 static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req)
229 unsigned int i;
230 u8 *buf = req->buf;
232 for (i = 0; i + 3 < req->actual; i += 4)
233 if (buf[i] != 0) {
234 int cable = buf[i] >> 4;
235 int length = f_midi_cin_length[buf[i] & 0x0f];
236 f_midi_read_data(ep, cable, &buf[i + 1], length);
240 static void
241 f_midi_complete(struct usb_ep *ep, struct usb_request *req)
243 struct f_midi *midi = ep->driver_data;
244 struct usb_composite_dev *cdev = midi->func.config->cdev;
245 int status = req->status;
247 switch (status) {
248 case 0: /* normal completion */
249 if (ep == midi->out_ep) {
250 /* We received stuff. req is queued again, below */
251 f_midi_handle_out_data(ep, req);
252 } else if (ep == midi->in_ep) {
253 /* Our transmit completed. See if there's more to go.
254 * f_midi_transmit eats req, don't queue it again. */
255 f_midi_transmit(midi, req);
256 return;
258 break;
260 /* this endpoint is normally active while we're configured */
261 case -ECONNABORTED: /* hardware forced ep reset */
262 case -ECONNRESET: /* request dequeued */
263 case -ESHUTDOWN: /* disconnect from host */
264 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
265 req->actual, req->length);
266 if (ep == midi->out_ep)
267 f_midi_handle_out_data(ep, req);
269 free_ep_req(ep, req);
270 return;
272 case -EOVERFLOW: /* buffer overrun on read means that
273 * we didn't provide a big enough buffer.
275 default:
276 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
277 status, req->actual, req->length);
278 break;
279 case -EREMOTEIO: /* short read */
280 break;
283 status = usb_ep_queue(ep, req, GFP_ATOMIC);
284 if (status) {
285 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
286 ep->name, req->length, status);
287 usb_ep_set_halt(ep);
288 /* FIXME recover later ... somehow */
292 static int f_midi_start_ep(struct f_midi *midi,
293 struct usb_function *f,
294 struct usb_ep *ep)
296 int err;
297 struct usb_composite_dev *cdev = f->config->cdev;
299 usb_ep_disable(ep);
301 err = config_ep_by_speed(midi->gadget, f, ep);
302 if (err) {
303 ERROR(cdev, "can't configure %s: %d\n", ep->name, err);
304 return err;
307 err = usb_ep_enable(ep);
308 if (err) {
309 ERROR(cdev, "can't start %s: %d\n", ep->name, err);
310 return err;
313 ep->driver_data = midi;
315 return 0;
318 static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
320 struct f_midi *midi = func_to_midi(f);
321 struct usb_composite_dev *cdev = f->config->cdev;
322 unsigned i;
323 int err;
325 /* For Control Device interface we do nothing */
326 if (intf == 0)
327 return 0;
329 err = f_midi_start_ep(midi, f, midi->in_ep);
330 if (err)
331 return err;
333 err = f_midi_start_ep(midi, f, midi->out_ep);
334 if (err)
335 return err;
337 usb_ep_disable(midi->out_ep);
339 err = config_ep_by_speed(midi->gadget, f, midi->out_ep);
340 if (err) {
341 ERROR(cdev, "can't configure %s: %d\n",
342 midi->out_ep->name, err);
343 return err;
346 err = usb_ep_enable(midi->out_ep);
347 if (err) {
348 ERROR(cdev, "can't start %s: %d\n",
349 midi->out_ep->name, err);
350 return err;
353 midi->out_ep->driver_data = midi;
355 /* allocate a bunch of read buffers and queue them all at once. */
356 for (i = 0; i < midi->qlen && err == 0; i++) {
357 struct usb_request *req =
358 midi_alloc_ep_req(midi->out_ep,
359 max_t(unsigned, midi->buflen,
360 bulk_out_desc.wMaxPacketSize));
361 if (req == NULL)
362 return -ENOMEM;
364 req->complete = f_midi_complete;
365 err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC);
366 if (err) {
367 ERROR(midi, "%s queue req: %d\n",
368 midi->out_ep->name, err);
369 free_ep_req(midi->out_ep, req);
373 return 0;
376 static void f_midi_disable(struct usb_function *f)
378 struct f_midi *midi = func_to_midi(f);
379 struct usb_composite_dev *cdev = f->config->cdev;
381 DBG(cdev, "disable\n");
384 * just disable endpoints, forcing completion of pending i/o.
385 * all our completion handlers free their requests in this case.
387 usb_ep_disable(midi->in_ep);
388 usb_ep_disable(midi->out_ep);
391 static int f_midi_snd_free(struct snd_device *device)
393 return 0;
396 static void f_midi_transmit_packet(struct usb_request *req, uint8_t p0,
397 uint8_t p1, uint8_t p2, uint8_t p3)
399 unsigned length = req->length;
400 u8 *buf = (u8 *)req->buf + length;
402 buf[0] = p0;
403 buf[1] = p1;
404 buf[2] = p2;
405 buf[3] = p3;
406 req->length = length + 4;
410 * Converts MIDI commands to USB MIDI packets.
412 static void f_midi_transmit_byte(struct usb_request *req,
413 struct gmidi_in_port *port, uint8_t b)
415 uint8_t p0 = port->cable << 4;
417 if (b >= 0xf8) {
418 f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
419 } else if (b >= 0xf0) {
420 switch (b) {
421 case 0xf0:
422 port->data[0] = b;
423 port->state = STATE_SYSEX_1;
424 break;
425 case 0xf1:
426 case 0xf3:
427 port->data[0] = b;
428 port->state = STATE_1PARAM;
429 break;
430 case 0xf2:
431 port->data[0] = b;
432 port->state = STATE_2PARAM_1;
433 break;
434 case 0xf4:
435 case 0xf5:
436 port->state = STATE_UNKNOWN;
437 break;
438 case 0xf6:
439 f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
440 port->state = STATE_UNKNOWN;
441 break;
442 case 0xf7:
443 switch (port->state) {
444 case STATE_SYSEX_0:
445 f_midi_transmit_packet(req,
446 p0 | 0x05, 0xf7, 0, 0);
447 break;
448 case STATE_SYSEX_1:
449 f_midi_transmit_packet(req,
450 p0 | 0x06, port->data[0], 0xf7, 0);
451 break;
452 case STATE_SYSEX_2:
453 f_midi_transmit_packet(req,
454 p0 | 0x07, port->data[0],
455 port->data[1], 0xf7);
456 break;
458 port->state = STATE_UNKNOWN;
459 break;
461 } else if (b >= 0x80) {
462 port->data[0] = b;
463 if (b >= 0xc0 && b <= 0xdf)
464 port->state = STATE_1PARAM;
465 else
466 port->state = STATE_2PARAM_1;
467 } else { /* b < 0x80 */
468 switch (port->state) {
469 case STATE_1PARAM:
470 if (port->data[0] < 0xf0) {
471 p0 |= port->data[0] >> 4;
472 } else {
473 p0 |= 0x02;
474 port->state = STATE_UNKNOWN;
476 f_midi_transmit_packet(req, p0, port->data[0], b, 0);
477 break;
478 case STATE_2PARAM_1:
479 port->data[1] = b;
480 port->state = STATE_2PARAM_2;
481 break;
482 case STATE_2PARAM_2:
483 if (port->data[0] < 0xf0) {
484 p0 |= port->data[0] >> 4;
485 port->state = STATE_2PARAM_1;
486 } else {
487 p0 |= 0x03;
488 port->state = STATE_UNKNOWN;
490 f_midi_transmit_packet(req,
491 p0, port->data[0], port->data[1], b);
492 break;
493 case STATE_SYSEX_0:
494 port->data[0] = b;
495 port->state = STATE_SYSEX_1;
496 break;
497 case STATE_SYSEX_1:
498 port->data[1] = b;
499 port->state = STATE_SYSEX_2;
500 break;
501 case STATE_SYSEX_2:
502 f_midi_transmit_packet(req,
503 p0 | 0x04, port->data[0], port->data[1], b);
504 port->state = STATE_SYSEX_0;
505 break;
510 static void f_midi_transmit(struct f_midi *midi, struct usb_request *req)
512 struct usb_ep *ep = midi->in_ep;
513 int i;
515 if (!ep)
516 return;
518 if (!req)
519 req = midi_alloc_ep_req(ep, midi->buflen);
521 if (!req) {
522 ERROR(midi, "%s: alloc_ep_request failed\n", __func__);
523 return;
525 req->length = 0;
526 req->complete = f_midi_complete;
528 for (i = 0; i < MAX_PORTS; i++) {
529 struct gmidi_in_port *port = midi->in_port[i];
530 struct snd_rawmidi_substream *substream = midi->in_substream[i];
532 if (!port || !port->active || !substream)
533 continue;
535 while (req->length + 3 < midi->buflen) {
536 uint8_t b;
537 if (snd_rawmidi_transmit(substream, &b, 1) != 1) {
538 port->active = 0;
539 break;
541 f_midi_transmit_byte(req, port, b);
545 if (req->length > 0 && ep->enabled) {
546 int err;
548 err = usb_ep_queue(ep, req, GFP_ATOMIC);
549 if (err < 0)
550 ERROR(midi, "%s queue req: %d\n",
551 midi->in_ep->name, err);
552 } else {
553 free_ep_req(ep, req);
557 static void f_midi_in_tasklet(unsigned long data)
559 struct f_midi *midi = (struct f_midi *) data;
560 f_midi_transmit(midi, NULL);
563 static int f_midi_in_open(struct snd_rawmidi_substream *substream)
565 struct f_midi *midi = substream->rmidi->private_data;
567 if (!midi->in_port[substream->number])
568 return -EINVAL;
570 VDBG(midi, "%s()\n", __func__);
571 midi->in_substream[substream->number] = substream;
572 midi->in_port[substream->number]->state = STATE_UNKNOWN;
573 return 0;
576 static int f_midi_in_close(struct snd_rawmidi_substream *substream)
578 struct f_midi *midi = substream->rmidi->private_data;
580 VDBG(midi, "%s()\n", __func__);
581 return 0;
584 static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up)
586 struct f_midi *midi = substream->rmidi->private_data;
588 if (!midi->in_port[substream->number])
589 return;
591 VDBG(midi, "%s() %d\n", __func__, up);
592 midi->in_port[substream->number]->active = up;
593 if (up)
594 tasklet_hi_schedule(&midi->tasklet);
597 static int f_midi_out_open(struct snd_rawmidi_substream *substream)
599 struct f_midi *midi = substream->rmidi->private_data;
601 if (substream->number >= MAX_PORTS)
602 return -EINVAL;
604 VDBG(midi, "%s()\n", __func__);
605 midi->out_substream[substream->number] = substream;
606 return 0;
609 static int f_midi_out_close(struct snd_rawmidi_substream *substream)
611 struct f_midi *midi = substream->rmidi->private_data;
613 VDBG(midi, "%s()\n", __func__);
614 return 0;
617 static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up)
619 struct f_midi *midi = substream->rmidi->private_data;
621 VDBG(midi, "%s()\n", __func__);
623 if (up)
624 set_bit(substream->number, &midi->out_triggered);
625 else
626 clear_bit(substream->number, &midi->out_triggered);
629 static struct snd_rawmidi_ops gmidi_in_ops = {
630 .open = f_midi_in_open,
631 .close = f_midi_in_close,
632 .trigger = f_midi_in_trigger,
635 static struct snd_rawmidi_ops gmidi_out_ops = {
636 .open = f_midi_out_open,
637 .close = f_midi_out_close,
638 .trigger = f_midi_out_trigger
641 static inline void f_midi_unregister_card(struct f_midi *midi)
643 if (midi->card) {
644 snd_card_free(midi->card);
645 midi->card = NULL;
649 /* register as a sound "card" */
650 static int f_midi_register_card(struct f_midi *midi)
652 struct snd_card *card;
653 struct snd_rawmidi *rmidi;
654 int err;
655 static struct snd_device_ops ops = {
656 .dev_free = f_midi_snd_free,
659 err = snd_card_new(&midi->gadget->dev, midi->index, midi->id,
660 THIS_MODULE, 0, &card);
661 if (err < 0) {
662 ERROR(midi, "snd_card_new() failed\n");
663 goto fail;
665 midi->card = card;
667 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops);
668 if (err < 0) {
669 ERROR(midi, "snd_device_new() failed: error %d\n", err);
670 goto fail;
673 strcpy(card->driver, f_midi_longname);
674 strcpy(card->longname, f_midi_longname);
675 strcpy(card->shortname, f_midi_shortname);
677 /* Set up rawmidi */
678 snd_component_add(card, "MIDI");
679 err = snd_rawmidi_new(card, card->longname, 0,
680 midi->out_ports, midi->in_ports, &rmidi);
681 if (err < 0) {
682 ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err);
683 goto fail;
685 midi->rmidi = rmidi;
686 strcpy(rmidi->name, card->shortname);
687 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
688 SNDRV_RAWMIDI_INFO_INPUT |
689 SNDRV_RAWMIDI_INFO_DUPLEX;
690 rmidi->private_data = midi;
693 * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT.
694 * It's an upside-down world being a gadget.
696 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops);
697 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops);
699 /* register it - we're ready to go */
700 err = snd_card_register(card);
701 if (err < 0) {
702 ERROR(midi, "snd_card_register() failed\n");
703 goto fail;
706 VDBG(midi, "%s() finished ok\n", __func__);
707 return 0;
709 fail:
710 f_midi_unregister_card(midi);
711 return err;
714 /* MIDI function driver setup/binding */
716 static int f_midi_bind(struct usb_configuration *c, struct usb_function *f)
718 struct usb_descriptor_header **midi_function;
719 struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS];
720 struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS];
721 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
722 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
723 struct usb_composite_dev *cdev = c->cdev;
724 struct f_midi *midi = func_to_midi(f);
725 struct usb_string *us;
726 int status, n, jack = 1, i = 0;
728 midi->gadget = cdev->gadget;
729 tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi);
730 status = f_midi_register_card(midi);
731 if (status < 0)
732 goto fail_register;
734 /* maybe allocate device-global string ID */
735 us = usb_gstrings_attach(c->cdev, midi_strings,
736 ARRAY_SIZE(midi_string_defs));
737 if (IS_ERR(us)) {
738 status = PTR_ERR(us);
739 goto fail;
741 ac_interface_desc.iInterface = us[STRING_FUNC_IDX].id;
743 /* We have two interfaces, AudioControl and MIDIStreaming */
744 status = usb_interface_id(c, f);
745 if (status < 0)
746 goto fail;
747 ac_interface_desc.bInterfaceNumber = status;
749 status = usb_interface_id(c, f);
750 if (status < 0)
751 goto fail;
752 ms_interface_desc.bInterfaceNumber = status;
753 ac_header_desc.baInterfaceNr[0] = status;
755 status = -ENODEV;
757 /* allocate instance-specific endpoints */
758 midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc);
759 if (!midi->in_ep)
760 goto fail;
762 midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc);
763 if (!midi->out_ep)
764 goto fail;
766 /* allocate temporary function list */
767 midi_function = kcalloc((MAX_PORTS * 4) + 9, sizeof(*midi_function),
768 GFP_KERNEL);
769 if (!midi_function) {
770 status = -ENOMEM;
771 goto fail;
775 * construct the function's descriptor set. As the number of
776 * input and output MIDI ports is configurable, we have to do
777 * it that way.
780 /* add the headers - these are always the same */
781 midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc;
782 midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc;
783 midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc;
785 /* calculate the header's wTotalLength */
786 n = USB_DT_MS_HEADER_SIZE
787 + (midi->in_ports + midi->out_ports) *
788 (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
789 ms_header_desc.wTotalLength = cpu_to_le16(n);
791 midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc;
793 /* configure the external IN jacks, each linked to an embedded OUT jack */
794 for (n = 0; n < midi->in_ports; n++) {
795 struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n];
796 struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n];
798 in_ext->bLength = USB_DT_MIDI_IN_SIZE;
799 in_ext->bDescriptorType = USB_DT_CS_INTERFACE;
800 in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
801 in_ext->bJackType = USB_MS_EXTERNAL;
802 in_ext->bJackID = jack++;
803 in_ext->iJack = 0;
804 midi_function[i++] = (struct usb_descriptor_header *) in_ext;
806 out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1);
807 out_emb->bDescriptorType = USB_DT_CS_INTERFACE;
808 out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
809 out_emb->bJackType = USB_MS_EMBEDDED;
810 out_emb->bJackID = jack++;
811 out_emb->bNrInputPins = 1;
812 out_emb->pins[0].baSourcePin = 1;
813 out_emb->pins[0].baSourceID = in_ext->bJackID;
814 out_emb->iJack = 0;
815 midi_function[i++] = (struct usb_descriptor_header *) out_emb;
817 /* link it to the endpoint */
818 ms_in_desc.baAssocJackID[n] = out_emb->bJackID;
821 /* configure the external OUT jacks, each linked to an embedded IN jack */
822 for (n = 0; n < midi->out_ports; n++) {
823 struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n];
824 struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n];
826 in_emb->bLength = USB_DT_MIDI_IN_SIZE;
827 in_emb->bDescriptorType = USB_DT_CS_INTERFACE;
828 in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
829 in_emb->bJackType = USB_MS_EMBEDDED;
830 in_emb->bJackID = jack++;
831 in_emb->iJack = 0;
832 midi_function[i++] = (struct usb_descriptor_header *) in_emb;
834 out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1);
835 out_ext->bDescriptorType = USB_DT_CS_INTERFACE;
836 out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
837 out_ext->bJackType = USB_MS_EXTERNAL;
838 out_ext->bJackID = jack++;
839 out_ext->bNrInputPins = 1;
840 out_ext->iJack = 0;
841 out_ext->pins[0].baSourceID = in_emb->bJackID;
842 out_ext->pins[0].baSourcePin = 1;
843 midi_function[i++] = (struct usb_descriptor_header *) out_ext;
845 /* link it to the endpoint */
846 ms_out_desc.baAssocJackID[n] = in_emb->bJackID;
849 /* configure the endpoint descriptors ... */
850 ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports);
851 ms_out_desc.bNumEmbMIDIJack = midi->in_ports;
853 ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports);
854 ms_in_desc.bNumEmbMIDIJack = midi->out_ports;
856 /* ... and add them to the list */
857 midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc;
858 midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc;
859 midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc;
860 midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc;
861 midi_function[i++] = NULL;
864 * support all relevant hardware speeds... we expect that when
865 * hardware is dual speed, all bulk-capable endpoints work at
866 * both speeds
868 /* copy descriptors, and track endpoint copies */
869 f->fs_descriptors = usb_copy_descriptors(midi_function);
870 if (!f->fs_descriptors)
871 goto fail_f_midi;
873 if (gadget_is_dualspeed(c->cdev->gadget)) {
874 bulk_in_desc.wMaxPacketSize = cpu_to_le16(512);
875 bulk_out_desc.wMaxPacketSize = cpu_to_le16(512);
876 f->hs_descriptors = usb_copy_descriptors(midi_function);
877 if (!f->hs_descriptors)
878 goto fail_f_midi;
881 kfree(midi_function);
883 return 0;
885 fail_f_midi:
886 kfree(midi_function);
887 usb_free_descriptors(f->hs_descriptors);
888 fail:
889 f_midi_unregister_card(midi);
890 fail_register:
891 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
893 return status;
896 static inline struct f_midi_opts *to_f_midi_opts(struct config_item *item)
898 return container_of(to_config_group(item), struct f_midi_opts,
899 func_inst.group);
902 static void midi_attr_release(struct config_item *item)
904 struct f_midi_opts *opts = to_f_midi_opts(item);
906 usb_put_function_instance(&opts->func_inst);
909 static struct configfs_item_operations midi_item_ops = {
910 .release = midi_attr_release,
913 #define F_MIDI_OPT(name, test_limit, limit) \
914 static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \
916 struct f_midi_opts *opts = to_f_midi_opts(item); \
917 int result; \
919 mutex_lock(&opts->lock); \
920 result = sprintf(page, "%d\n", opts->name); \
921 mutex_unlock(&opts->lock); \
923 return result; \
926 static ssize_t f_midi_opts_##name##_store(struct config_item *item, \
927 const char *page, size_t len) \
929 struct f_midi_opts *opts = to_f_midi_opts(item); \
930 int ret; \
931 u32 num; \
933 mutex_lock(&opts->lock); \
934 if (opts->refcnt) { \
935 ret = -EBUSY; \
936 goto end; \
939 ret = kstrtou32(page, 0, &num); \
940 if (ret) \
941 goto end; \
943 if (test_limit && num > limit) { \
944 ret = -EINVAL; \
945 goto end; \
947 opts->name = num; \
948 ret = len; \
950 end: \
951 mutex_unlock(&opts->lock); \
952 return ret; \
955 CONFIGFS_ATTR(f_midi_opts_, name);
957 F_MIDI_OPT(index, true, SNDRV_CARDS);
958 F_MIDI_OPT(buflen, false, 0);
959 F_MIDI_OPT(qlen, false, 0);
960 F_MIDI_OPT(in_ports, true, MAX_PORTS);
961 F_MIDI_OPT(out_ports, true, MAX_PORTS);
963 static ssize_t f_midi_opts_id_show(struct config_item *item, char *page)
965 struct f_midi_opts *opts = to_f_midi_opts(item);
966 int result;
968 mutex_lock(&opts->lock);
969 if (opts->id) {
970 result = strlcpy(page, opts->id, PAGE_SIZE);
971 } else {
972 page[0] = 0;
973 result = 0;
976 mutex_unlock(&opts->lock);
978 return result;
981 static ssize_t f_midi_opts_id_store(struct config_item *item,
982 const char *page, size_t len)
984 struct f_midi_opts *opts = to_f_midi_opts(item);
985 int ret;
986 char *c;
988 mutex_lock(&opts->lock);
989 if (opts->refcnt) {
990 ret = -EBUSY;
991 goto end;
994 c = kstrndup(page, len, GFP_KERNEL);
995 if (!c) {
996 ret = -ENOMEM;
997 goto end;
999 if (opts->id_allocated)
1000 kfree(opts->id);
1001 opts->id = c;
1002 opts->id_allocated = true;
1003 ret = len;
1004 end:
1005 mutex_unlock(&opts->lock);
1006 return ret;
1009 CONFIGFS_ATTR(f_midi_opts_, id);
1011 static struct configfs_attribute *midi_attrs[] = {
1012 &f_midi_opts_attr_index,
1013 &f_midi_opts_attr_buflen,
1014 &f_midi_opts_attr_qlen,
1015 &f_midi_opts_attr_in_ports,
1016 &f_midi_opts_attr_out_ports,
1017 &f_midi_opts_attr_id,
1018 NULL,
1021 static struct config_item_type midi_func_type = {
1022 .ct_item_ops = &midi_item_ops,
1023 .ct_attrs = midi_attrs,
1024 .ct_owner = THIS_MODULE,
1027 static void f_midi_free_inst(struct usb_function_instance *f)
1029 struct f_midi_opts *opts;
1031 opts = container_of(f, struct f_midi_opts, func_inst);
1033 if (opts->id_allocated)
1034 kfree(opts->id);
1036 kfree(opts);
1039 static struct usb_function_instance *f_midi_alloc_inst(void)
1041 struct f_midi_opts *opts;
1043 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1044 if (!opts)
1045 return ERR_PTR(-ENOMEM);
1047 mutex_init(&opts->lock);
1048 opts->func_inst.free_func_inst = f_midi_free_inst;
1049 opts->index = SNDRV_DEFAULT_IDX1;
1050 opts->id = SNDRV_DEFAULT_STR1;
1051 opts->buflen = 256;
1052 opts->qlen = 32;
1053 opts->in_ports = 1;
1054 opts->out_ports = 1;
1056 config_group_init_type_name(&opts->func_inst.group, "",
1057 &midi_func_type);
1059 return &opts->func_inst;
1062 static void f_midi_free(struct usb_function *f)
1064 struct f_midi *midi;
1065 struct f_midi_opts *opts;
1066 int i;
1068 midi = func_to_midi(f);
1069 opts = container_of(f->fi, struct f_midi_opts, func_inst);
1070 kfree(midi->id);
1071 mutex_lock(&opts->lock);
1072 for (i = opts->in_ports - 1; i >= 0; --i)
1073 kfree(midi->in_port[i]);
1074 kfree(midi);
1075 --opts->refcnt;
1076 mutex_unlock(&opts->lock);
1079 static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f)
1081 struct usb_composite_dev *cdev = f->config->cdev;
1082 struct f_midi *midi = func_to_midi(f);
1083 struct snd_card *card;
1085 DBG(cdev, "unbind\n");
1087 /* just to be sure */
1088 f_midi_disable(f);
1090 card = midi->card;
1091 midi->card = NULL;
1092 if (card)
1093 snd_card_free(card);
1095 usb_free_all_descriptors(f);
1098 static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
1100 struct f_midi *midi;
1101 struct f_midi_opts *opts;
1102 int status, i;
1104 opts = container_of(fi, struct f_midi_opts, func_inst);
1106 mutex_lock(&opts->lock);
1107 /* sanity check */
1108 if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) {
1109 mutex_unlock(&opts->lock);
1110 return ERR_PTR(-EINVAL);
1113 /* allocate and initialize one new instance */
1114 midi = kzalloc(sizeof(*midi), GFP_KERNEL);
1115 if (!midi) {
1116 mutex_unlock(&opts->lock);
1117 return ERR_PTR(-ENOMEM);
1120 for (i = 0; i < opts->in_ports; i++) {
1121 struct gmidi_in_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
1123 if (!port) {
1124 status = -ENOMEM;
1125 mutex_unlock(&opts->lock);
1126 goto setup_fail;
1129 port->midi = midi;
1130 port->active = 0;
1131 port->cable = i;
1132 midi->in_port[i] = port;
1135 /* set up ALSA midi devices */
1136 midi->id = kstrdup(opts->id, GFP_KERNEL);
1137 if (opts->id && !midi->id) {
1138 status = -ENOMEM;
1139 mutex_unlock(&opts->lock);
1140 goto setup_fail;
1142 midi->in_ports = opts->in_ports;
1143 midi->out_ports = opts->out_ports;
1144 midi->index = opts->index;
1145 midi->buflen = opts->buflen;
1146 midi->qlen = opts->qlen;
1147 ++opts->refcnt;
1148 mutex_unlock(&opts->lock);
1150 midi->func.name = "gmidi function";
1151 midi->func.bind = f_midi_bind;
1152 midi->func.unbind = f_midi_unbind;
1153 midi->func.set_alt = f_midi_set_alt;
1154 midi->func.disable = f_midi_disable;
1155 midi->func.free_func = f_midi_free;
1157 return &midi->func;
1159 setup_fail:
1160 for (--i; i >= 0; i--)
1161 kfree(midi->in_port[i]);
1162 kfree(midi);
1163 return ERR_PTR(status);
1166 DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc);