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