22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/device.h>
30 #include <linux/usb/ch9.h>
32 #include <linux/usb/audio.h>
38 static const char f_midi_shortname[] =
"f_midi";
39 static const char f_midi_longname[] =
"MIDI Gadget";
58 #define STATE_UNKNOWN 0
59 #define STATE_1PARAM 1
60 #define STATE_2PARAM_1 2
61 #define STATE_2PARAM_2 3
62 #define STATE_SYSEX_0 4
63 #define STATE_SYSEX_1 5
64 #define STATE_SYSEX_2 6
111 static struct uac1_ac_header_descriptor_1 ac_header_desc __initdata = {
150 static struct usb_ms_endpoint_descriptor_16 ms_out_desc = {
167 static struct usb_ms_endpoint_descriptor_16 ms_in_desc = {
177 #define STRING_FUNC_IDX 0
179 static struct usb_string midi_string_defs[] = {
186 .strings = midi_string_defs,
203 usb_ep_free_request(ep, req);
213 usb_ep_free_request(ep, req);
216 static const uint8_t f_midi_cin_length[] = {
217 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
223 static void f_midi_read_data(
struct usb_ep *ep,
int cable,
244 for (i = 0; i + 3 < req->
actual; i += 4)
246 int cable = buf[
i] >> 4;
247 int length = f_midi_cin_length[buf[
i] & 0x0f];
248 f_midi_read_data(ep, cable, &buf[i + 1], length);
263 f_midi_handle_out_data(ep, req);
264 }
else if (ep == midi->
in_ep) {
267 f_midi_transmit(midi, req);
276 VDBG(cdev,
"%s gone (%d), %d/%d\n", ep->
name, status,
279 f_midi_handle_out_data(ep, req);
281 free_ep_req(ep, req);
288 DBG(cdev,
"%s complete --> %d, %d/%d\n", ep->
name,
297 ERROR(cdev,
"kill %s: resubmit %d bytes --> %d\n",
304 static int f_midi_start_ep(
struct f_midi *midi,
316 ERROR(cdev,
"can't configure %s: %d\n", ep->
name, err);
320 err = usb_ep_enable(ep);
322 ERROR(cdev,
"can't start %s: %d\n", ep->
name, err);
331 static int f_midi_set_alt(
struct usb_function *f,
unsigned intf,
unsigned alt)
333 struct f_midi *midi = func_to_midi(f);
338 err = f_midi_start_ep(midi, f, midi->
in_ep);
342 err = f_midi_start_ep(midi, f, midi->
out_ep);
346 if (midi->
out_ep->driver_data)
347 usb_ep_disable(midi->
out_ep);
351 ERROR(cdev,
"can't configure %s: %d\n",
356 err = usb_ep_enable(midi->
out_ep);
358 ERROR(cdev,
"can't start %s: %d\n",
363 midi->
out_ep->driver_data = midi;
366 for (i = 0; i < midi->
qlen && err == 0; i++) {
375 ERROR(midi,
"%s queue req: %d\n",
385 struct f_midi *midi = func_to_midi(f);
388 DBG(cdev,
"disable\n");
394 usb_ep_disable(midi->
in_ep);
395 usb_ep_disable(midi->
out_ep);
401 struct f_midi *midi = func_to_midi(f);
404 DBG(cdev,
"unbind\n");
429 unsigned length = req->
length;
430 u8 *buf = (
u8 *)req->
buf + length;
442 static void f_midi_transmit_byte(
struct usb_request *req,
448 f_midi_transmit_packet(req, p0 | 0x0f, b, 0, 0);
449 }
else if (b >= 0xf0) {
469 f_midi_transmit_packet(req, p0 | 0x05, 0xf6, 0, 0);
473 switch (port->
state) {
475 f_midi_transmit_packet(req,
476 p0 | 0x05, 0xf7, 0, 0);
479 f_midi_transmit_packet(req,
480 p0 | 0x06, port->
data[0], 0xf7, 0);
483 f_midi_transmit_packet(req,
484 p0 | 0x07, port->
data[0],
485 port->
data[1], 0xf7);
491 }
else if (b >= 0x80) {
493 if (b >= 0xc0 && b <= 0xdf)
498 switch (port->
state) {
500 if (port->
data[0] < 0xf0) {
501 p0 |= port->
data[0] >> 4;
506 f_midi_transmit_packet(req, p0, port->
data[0], b, 0);
513 if (port->
data[0] < 0xf0) {
514 p0 |= port->
data[0] >> 4;
520 f_midi_transmit_packet(req,
521 p0, port->
data[0], port->
data[1], b);
532 f_midi_transmit_packet(req,
533 p0 | 0x04, port->
data[0], port->
data[1], b);
549 req = alloc_ep_req(ep, midi->
buflen);
552 ERROR(midi,
"gmidi_transmit: alloc_ep_request failed\n");
562 if (!port || !port->
active || !substream)
571 f_midi_transmit_byte(req, port, b);
578 free_ep_req(ep, req);
581 static void f_midi_in_tasklet(
unsigned long data)
584 f_midi_transmit(midi,
NULL);
589 struct f_midi *midi = substream->
rmidi->private_data;
594 VDBG(midi,
"%s()\n", __func__);
602 struct f_midi *midi = substream->
rmidi->private_data;
604 VDBG(midi,
"%s()\n", __func__);
610 struct f_midi *midi = substream->
rmidi->private_data;
615 VDBG(midi,
"%s() %d\n", __func__, up);
618 tasklet_hi_schedule(&midi->
tasklet);
623 struct f_midi *midi = substream->
rmidi->private_data;
625 if (substream->
number >= MAX_PORTS)
628 VDBG(midi,
"%s()\n", __func__);
635 struct f_midi *midi = substream->
rmidi->private_data;
637 VDBG(midi,
"%s()\n", __func__);
643 struct f_midi *midi = substream->
rmidi->private_data;
645 VDBG(midi,
"%s()\n", __func__);
654 .open = f_midi_in_open,
655 .close = f_midi_in_close,
656 .trigger = f_midi_in_trigger,
660 .open = f_midi_out_open,
661 .close = f_midi_out_close,
662 .trigger = f_midi_out_trigger
666 static int f_midi_register_card(
struct f_midi *midi)
672 .dev_free = f_midi_snd_free,
677 ERROR(midi,
"snd_card_create() failed\n");
684 ERROR(midi,
"snd_device_new() failed: error %d\n", err);
697 ERROR(midi,
"snd_rawmidi_new() failed: error %d\n", err);
719 ERROR(midi,
"snd_card_register() failed\n");
723 VDBG(midi,
"%s() finished ok\n", __func__);
742 struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS];
743 struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS];
745 struct f_midi *midi = func_to_midi(f);
746 int status,
n, jack = 1, i = 0;
749 if (midi_string_defs[0].
id == 0) {
760 ac_interface_desc.bInterfaceNumber =
status;
765 ms_interface_desc.bInterfaceNumber =
status;
766 ac_header_desc.baInterfaceNr[0] =
status;
782 midi_function = kcalloc((MAX_PORTS * 4) + 9,
sizeof(*midi_function),
784 if (!midi_function) {
809 for (n = 0; n < midi->
in_ports; n++) {
811 struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[
n];
825 out_emb->bJackID = jack++;
826 out_emb->bNrInputPins = 1;
827 out_emb->pins[0].baSourcePin = 1;
828 out_emb->pins[0].baSourceID = in_ext->
bJackID;
833 ms_in_desc.baAssocJackID[
n] = out_emb->bJackID;
839 struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[
n];
853 out_ext->bJackID = jack++;
854 out_ext->bNrInputPins = 1;
856 out_ext->pins[0].baSourceID = in_emb->
bJackID;
857 out_ext->pins[0].baSourcePin = 1;
861 ms_out_desc.baAssocJackID[
n] = in_emb->
bJackID;
866 ms_out_desc.bNumEmbMIDIJack = midi->
in_ports;
869 ms_in_desc.bNumEmbMIDIJack = midi->
out_ports;
876 midi_function[i++] =
NULL;
884 if (gadget_is_dualspeed(c->
cdev->gadget)) {
893 kfree(midi_function);
904 ERROR(cdev,
"%s: can't bind, err %d\n", f->
name, status);
922 unsigned int in_ports,
923 unsigned int out_ports,
931 if (in_ports > MAX_PORTS || out_ports > MAX_PORTS)
958 midi->in_ports = in_ports;
959 midi->out_ports = out_ports;
960 status = f_midi_register_card(midi);
964 midi->func.name =
"gmidi function";
965 midi->func.strings = midi_strings;
966 midi->func.bind = f_midi_bind;
967 midi->func.unbind = f_midi_unbind;
968 midi->func.set_alt = f_midi_set_alt;
969 midi->func.disable = f_midi_disable;
983 for (--i; i >= 0; i--)
984 kfree(midi->in_port[i]);