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