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