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