b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * f_midi.c -- USB MIDI class function driver |
| 4 | * |
| 5 | * Copyright (C) 2006 Thumtronics Pty Ltd. |
| 6 | * Developed for Thumtronics by Grey Innovation |
| 7 | * Ben Williamson <ben.williamson@greyinnovation.com> |
| 8 | * |
| 9 | * Rewritten for the composite framework |
| 10 | * Copyright (C) 2011 Daniel Mack <zonque@gmail.com> |
| 11 | * |
| 12 | * Based on drivers/usb/gadget/f_audio.c, |
| 13 | * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> |
| 14 | * Copyright (C) 2008 Analog Devices, Inc |
| 15 | * |
| 16 | * and drivers/usb/gadget/midi.c, |
| 17 | * Copyright (C) 2006 Thumtronics Pty Ltd. |
| 18 | * Ben Williamson <ben.williamson@greyinnovation.com> |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/device.h> |
| 25 | #include <linux/kfifo.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | |
| 28 | #include <sound/core.h> |
| 29 | #include <sound/initval.h> |
| 30 | #include <sound/rawmidi.h> |
| 31 | |
| 32 | #include <linux/usb/ch9.h> |
| 33 | #include <linux/usb/gadget.h> |
| 34 | #include <linux/usb/audio.h> |
| 35 | #include <linux/usb/midi.h> |
| 36 | |
| 37 | #include "u_f.h" |
| 38 | #include "u_midi.h" |
| 39 | |
| 40 | MODULE_AUTHOR("Ben Williamson"); |
| 41 | MODULE_LICENSE("GPL v2"); |
| 42 | |
| 43 | static const char f_midi_shortname[] = "f_midi"; |
| 44 | static const char f_midi_longname[] = "MIDI Gadget"; |
| 45 | |
| 46 | /* |
| 47 | * We can only handle 16 cables on one single endpoint, as cable numbers are |
| 48 | * stored in 4-bit fields. And as the interface currently only holds one |
| 49 | * single endpoint, this is the maximum number of ports we can allow. |
| 50 | */ |
| 51 | #define MAX_PORTS 16 |
| 52 | |
| 53 | /* MIDI message states */ |
| 54 | enum { |
| 55 | STATE_INITIAL = 0, /* pseudo state */ |
| 56 | STATE_1PARAM, |
| 57 | STATE_2PARAM_1, |
| 58 | STATE_2PARAM_2, |
| 59 | STATE_SYSEX_0, |
| 60 | STATE_SYSEX_1, |
| 61 | STATE_SYSEX_2, |
| 62 | STATE_REAL_TIME, |
| 63 | STATE_FINISHED, /* pseudo state */ |
| 64 | }; |
| 65 | |
| 66 | /* |
| 67 | * This is a gadget, and the IN/OUT naming is from the host's perspective. |
| 68 | * USB -> OUT endpoint -> rawmidi |
| 69 | * USB <- IN endpoint <- rawmidi |
| 70 | */ |
| 71 | struct gmidi_in_port { |
| 72 | struct snd_rawmidi_substream *substream; |
| 73 | int active; |
| 74 | uint8_t cable; |
| 75 | uint8_t state; |
| 76 | uint8_t data[2]; |
| 77 | }; |
| 78 | |
| 79 | struct f_midi { |
| 80 | struct usb_function func; |
| 81 | struct usb_gadget *gadget; |
| 82 | struct usb_ep *in_ep, *out_ep; |
| 83 | struct snd_card *card; |
| 84 | struct snd_rawmidi *rmidi; |
| 85 | u8 ms_id; |
| 86 | |
| 87 | struct snd_rawmidi_substream *out_substream[MAX_PORTS]; |
| 88 | |
| 89 | unsigned long out_triggered; |
| 90 | struct tasklet_struct tasklet; |
| 91 | unsigned int in_ports; |
| 92 | unsigned int out_ports; |
| 93 | int index; |
| 94 | char *id; |
| 95 | unsigned int buflen, qlen; |
| 96 | /* This fifo is used as a buffer ring for pre-allocated IN usb_requests */ |
| 97 | DECLARE_KFIFO_PTR(in_req_fifo, struct usb_request *); |
| 98 | spinlock_t transmit_lock; |
| 99 | unsigned int in_last_port; |
| 100 | unsigned char free_ref; |
| 101 | |
| 102 | struct gmidi_in_port in_ports_array[/* in_ports */]; |
| 103 | }; |
| 104 | |
| 105 | static inline struct f_midi *func_to_midi(struct usb_function *f) |
| 106 | { |
| 107 | return container_of(f, struct f_midi, func); |
| 108 | } |
| 109 | |
| 110 | static void f_midi_transmit(struct f_midi *midi); |
| 111 | static void f_midi_rmidi_free(struct snd_rawmidi *rmidi); |
| 112 | static void f_midi_free_inst(struct usb_function_instance *f); |
| 113 | |
| 114 | DECLARE_UAC_AC_HEADER_DESCRIPTOR(1); |
| 115 | DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1); |
| 116 | DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16); |
| 117 | |
| 118 | /* B.3.1 Standard AC Interface Descriptor */ |
| 119 | static struct usb_interface_descriptor ac_interface_desc = { |
| 120 | .bLength = USB_DT_INTERFACE_SIZE, |
| 121 | .bDescriptorType = USB_DT_INTERFACE, |
| 122 | /* .bInterfaceNumber = DYNAMIC */ |
| 123 | /* .bNumEndpoints = DYNAMIC */ |
| 124 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 125 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, |
| 126 | /* .iInterface = DYNAMIC */ |
| 127 | }; |
| 128 | |
| 129 | /* B.3.2 Class-Specific AC Interface Descriptor */ |
| 130 | static struct uac1_ac_header_descriptor_1 ac_header_desc = { |
| 131 | .bLength = UAC_DT_AC_HEADER_SIZE(1), |
| 132 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 133 | .bDescriptorSubtype = USB_MS_HEADER, |
| 134 | .bcdADC = cpu_to_le16(0x0100), |
| 135 | .wTotalLength = cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1)), |
| 136 | .bInCollection = 1, |
| 137 | /* .baInterfaceNr = DYNAMIC */ |
| 138 | }; |
| 139 | |
| 140 | /* B.4.1 Standard MS Interface Descriptor */ |
| 141 | static struct usb_interface_descriptor ms_interface_desc = { |
| 142 | .bLength = USB_DT_INTERFACE_SIZE, |
| 143 | .bDescriptorType = USB_DT_INTERFACE, |
| 144 | /* .bInterfaceNumber = DYNAMIC */ |
| 145 | .bNumEndpoints = 2, |
| 146 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 147 | .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING, |
| 148 | /* .iInterface = DYNAMIC */ |
| 149 | }; |
| 150 | |
| 151 | /* B.4.2 Class-Specific MS Interface Descriptor */ |
| 152 | static struct usb_ms_header_descriptor ms_header_desc = { |
| 153 | .bLength = USB_DT_MS_HEADER_SIZE, |
| 154 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 155 | .bDescriptorSubtype = USB_MS_HEADER, |
| 156 | .bcdMSC = cpu_to_le16(0x0100), |
| 157 | /* .wTotalLength = DYNAMIC */ |
| 158 | }; |
| 159 | |
| 160 | /* B.5.1 Standard Bulk OUT Endpoint Descriptor */ |
| 161 | static struct usb_endpoint_descriptor bulk_out_desc = { |
| 162 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
| 163 | .bDescriptorType = USB_DT_ENDPOINT, |
| 164 | .bEndpointAddress = USB_DIR_OUT, |
| 165 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 166 | }; |
| 167 | |
| 168 | static struct usb_ss_ep_comp_descriptor bulk_out_ss_comp_desc = { |
| 169 | .bLength = sizeof(bulk_out_ss_comp_desc), |
| 170 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 171 | /* .bMaxBurst = 0, */ |
| 172 | /* .bmAttributes = 0, */ |
| 173 | }; |
| 174 | |
| 175 | /* B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor */ |
| 176 | static struct usb_ms_endpoint_descriptor_16 ms_out_desc = { |
| 177 | /* .bLength = DYNAMIC */ |
| 178 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
| 179 | .bDescriptorSubtype = USB_MS_GENERAL, |
| 180 | /* .bNumEmbMIDIJack = DYNAMIC */ |
| 181 | /* .baAssocJackID = DYNAMIC */ |
| 182 | }; |
| 183 | |
| 184 | /* B.6.1 Standard Bulk IN Endpoint Descriptor */ |
| 185 | static struct usb_endpoint_descriptor bulk_in_desc = { |
| 186 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
| 187 | .bDescriptorType = USB_DT_ENDPOINT, |
| 188 | .bEndpointAddress = USB_DIR_IN, |
| 189 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 190 | }; |
| 191 | |
| 192 | static struct usb_ss_ep_comp_descriptor bulk_in_ss_comp_desc = { |
| 193 | .bLength = sizeof(bulk_in_ss_comp_desc), |
| 194 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 195 | /* .bMaxBurst = 0, */ |
| 196 | /* .bmAttributes = 0, */ |
| 197 | }; |
| 198 | |
| 199 | /* B.6.2 Class-specific MS Bulk IN Endpoint Descriptor */ |
| 200 | static struct usb_ms_endpoint_descriptor_16 ms_in_desc = { |
| 201 | /* .bLength = DYNAMIC */ |
| 202 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
| 203 | .bDescriptorSubtype = USB_MS_GENERAL, |
| 204 | /* .bNumEmbMIDIJack = DYNAMIC */ |
| 205 | /* .baAssocJackID = DYNAMIC */ |
| 206 | }; |
| 207 | |
| 208 | /* string IDs are assigned dynamically */ |
| 209 | |
| 210 | #define STRING_FUNC_IDX 0 |
| 211 | |
| 212 | static struct usb_string midi_string_defs[] = { |
| 213 | [STRING_FUNC_IDX].s = "MIDI function", |
| 214 | { } /* end of list */ |
| 215 | }; |
| 216 | |
| 217 | static struct usb_gadget_strings midi_stringtab = { |
| 218 | .language = 0x0409, /* en-us */ |
| 219 | .strings = midi_string_defs, |
| 220 | }; |
| 221 | |
| 222 | static struct usb_gadget_strings *midi_strings[] = { |
| 223 | &midi_stringtab, |
| 224 | NULL, |
| 225 | }; |
| 226 | |
| 227 | static inline struct usb_request *midi_alloc_ep_req(struct usb_ep *ep, |
| 228 | unsigned length) |
| 229 | { |
| 230 | return alloc_ep_req(ep, length); |
| 231 | } |
| 232 | |
| 233 | static const uint8_t f_midi_cin_length[] = { |
| 234 | 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1 |
| 235 | }; |
| 236 | |
| 237 | /* |
| 238 | * Receives a chunk of MIDI data. |
| 239 | */ |
| 240 | static void f_midi_read_data(struct usb_ep *ep, int cable, |
| 241 | uint8_t *data, int length) |
| 242 | { |
| 243 | struct f_midi *midi = ep->driver_data; |
| 244 | struct snd_rawmidi_substream *substream = midi->out_substream[cable]; |
| 245 | |
| 246 | if (!substream) |
| 247 | /* Nobody is listening - throw it on the floor. */ |
| 248 | return; |
| 249 | |
| 250 | if (!test_bit(cable, &midi->out_triggered)) |
| 251 | return; |
| 252 | |
| 253 | snd_rawmidi_receive(substream, data, length); |
| 254 | } |
| 255 | |
| 256 | static void f_midi_handle_out_data(struct usb_ep *ep, struct usb_request *req) |
| 257 | { |
| 258 | unsigned int i; |
| 259 | u8 *buf = req->buf; |
| 260 | |
| 261 | for (i = 0; i + 3 < req->actual; i += 4) |
| 262 | if (buf[i] != 0) { |
| 263 | int cable = buf[i] >> 4; |
| 264 | int length = f_midi_cin_length[buf[i] & 0x0f]; |
| 265 | f_midi_read_data(ep, cable, &buf[i + 1], length); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | static void |
| 270 | f_midi_complete(struct usb_ep *ep, struct usb_request *req) |
| 271 | { |
| 272 | struct f_midi *midi = ep->driver_data; |
| 273 | struct usb_composite_dev *cdev = midi->func.config->cdev; |
| 274 | int status = req->status; |
| 275 | |
| 276 | switch (status) { |
| 277 | case 0: /* normal completion */ |
| 278 | if (ep == midi->out_ep) { |
| 279 | /* We received stuff. req is queued again, below */ |
| 280 | f_midi_handle_out_data(ep, req); |
| 281 | } else if (ep == midi->in_ep) { |
| 282 | /* Our transmit completed. See if there's more to go. |
| 283 | * f_midi_transmit eats req, don't queue it again. */ |
| 284 | req->length = 0; |
| 285 | f_midi_transmit(midi); |
| 286 | return; |
| 287 | } |
| 288 | break; |
| 289 | |
| 290 | /* this endpoint is normally active while we're configured */ |
| 291 | case -ECONNABORTED: /* hardware forced ep reset */ |
| 292 | case -ECONNRESET: /* request dequeued */ |
| 293 | case -ESHUTDOWN: /* disconnect from host */ |
| 294 | VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status, |
| 295 | req->actual, req->length); |
| 296 | if (ep == midi->out_ep) { |
| 297 | f_midi_handle_out_data(ep, req); |
| 298 | /* We don't need to free IN requests because it's handled |
| 299 | * by the midi->in_req_fifo. */ |
| 300 | free_ep_req(ep, req); |
| 301 | } |
| 302 | return; |
| 303 | |
| 304 | case -EOVERFLOW: /* buffer overrun on read means that |
| 305 | * we didn't provide a big enough buffer. |
| 306 | */ |
| 307 | default: |
| 308 | DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name, |
| 309 | status, req->actual, req->length); |
| 310 | break; |
| 311 | case -EREMOTEIO: /* short read */ |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | status = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 316 | if (status) { |
| 317 | ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n", |
| 318 | ep->name, req->length, status); |
| 319 | usb_ep_set_halt(ep); |
| 320 | /* FIXME recover later ... somehow */ |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static void f_midi_drop_out_substreams(struct f_midi *midi) |
| 325 | { |
| 326 | unsigned int i; |
| 327 | |
| 328 | for (i = 0; i < midi->in_ports; i++) { |
| 329 | struct gmidi_in_port *port = midi->in_ports_array + i; |
| 330 | struct snd_rawmidi_substream *substream = port->substream; |
| 331 | |
| 332 | if (port->active && substream) |
| 333 | snd_rawmidi_drop_output(substream); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | static int f_midi_start_ep(struct f_midi *midi, |
| 338 | struct usb_function *f, |
| 339 | struct usb_ep *ep) |
| 340 | { |
| 341 | int err; |
| 342 | struct usb_composite_dev *cdev = f->config->cdev; |
| 343 | |
| 344 | usb_ep_disable(ep); |
| 345 | |
| 346 | err = config_ep_by_speed(midi->gadget, f, ep); |
| 347 | if (err) { |
| 348 | ERROR(cdev, "can't configure %s: %d\n", ep->name, err); |
| 349 | return err; |
| 350 | } |
| 351 | |
| 352 | err = usb_ep_enable(ep); |
| 353 | if (err) { |
| 354 | ERROR(cdev, "can't start %s: %d\n", ep->name, err); |
| 355 | return err; |
| 356 | } |
| 357 | |
| 358 | ep->driver_data = midi; |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int f_midi_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 364 | { |
| 365 | struct f_midi *midi = func_to_midi(f); |
| 366 | unsigned i; |
| 367 | int err; |
| 368 | |
| 369 | /* we only set alt for MIDIStreaming interface */ |
| 370 | if (intf != midi->ms_id) |
| 371 | return 0; |
| 372 | |
| 373 | err = f_midi_start_ep(midi, f, midi->in_ep); |
| 374 | if (err) |
| 375 | return err; |
| 376 | |
| 377 | err = f_midi_start_ep(midi, f, midi->out_ep); |
| 378 | if (err) |
| 379 | return err; |
| 380 | |
| 381 | /* pre-allocate write usb requests to use on f_midi_transmit. */ |
| 382 | while (kfifo_avail(&midi->in_req_fifo)) { |
| 383 | struct usb_request *req = |
| 384 | midi_alloc_ep_req(midi->in_ep, midi->buflen); |
| 385 | |
| 386 | if (req == NULL) |
| 387 | return -ENOMEM; |
| 388 | |
| 389 | req->length = 0; |
| 390 | req->complete = f_midi_complete; |
| 391 | |
| 392 | kfifo_put(&midi->in_req_fifo, req); |
| 393 | } |
| 394 | |
| 395 | /* allocate a bunch of read buffers and queue them all at once. */ |
| 396 | for (i = 0; i < midi->qlen && err == 0; i++) { |
| 397 | struct usb_request *req = |
| 398 | midi_alloc_ep_req(midi->out_ep, midi->buflen); |
| 399 | |
| 400 | if (req == NULL) |
| 401 | return -ENOMEM; |
| 402 | |
| 403 | req->complete = f_midi_complete; |
| 404 | err = usb_ep_queue(midi->out_ep, req, GFP_ATOMIC); |
| 405 | if (err) { |
| 406 | ERROR(midi, "%s: couldn't enqueue request: %d\n", |
| 407 | midi->out_ep->name, err); |
| 408 | if (req->buf != NULL) |
| 409 | free_ep_req(midi->out_ep, req); |
| 410 | return err; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static void f_midi_disable(struct usb_function *f) |
| 418 | { |
| 419 | struct f_midi *midi = func_to_midi(f); |
| 420 | struct usb_composite_dev *cdev = f->config->cdev; |
| 421 | struct usb_request *req = NULL; |
| 422 | |
| 423 | DBG(cdev, "disable\n"); |
| 424 | |
| 425 | /* |
| 426 | * just disable endpoints, forcing completion of pending i/o. |
| 427 | * all our completion handlers free their requests in this case. |
| 428 | */ |
| 429 | usb_ep_disable(midi->in_ep); |
| 430 | usb_ep_disable(midi->out_ep); |
| 431 | |
| 432 | /* release IN requests */ |
| 433 | while (kfifo_get(&midi->in_req_fifo, &req)) |
| 434 | free_ep_req(midi->in_ep, req); |
| 435 | |
| 436 | f_midi_drop_out_substreams(midi); |
| 437 | } |
| 438 | |
| 439 | static int f_midi_snd_free(struct snd_device *device) |
| 440 | { |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Converts MIDI commands to USB MIDI packets. |
| 446 | */ |
| 447 | static void f_midi_transmit_byte(struct usb_request *req, |
| 448 | struct gmidi_in_port *port, uint8_t b) |
| 449 | { |
| 450 | uint8_t p[4] = { port->cable << 4, 0, 0, 0 }; |
| 451 | uint8_t next_state = STATE_INITIAL; |
| 452 | |
| 453 | switch (b) { |
| 454 | case 0xf8 ... 0xff: |
| 455 | /* System Real-Time Messages */ |
| 456 | p[0] |= 0x0f; |
| 457 | p[1] = b; |
| 458 | next_state = port->state; |
| 459 | port->state = STATE_REAL_TIME; |
| 460 | break; |
| 461 | |
| 462 | case 0xf7: |
| 463 | /* End of SysEx */ |
| 464 | switch (port->state) { |
| 465 | case STATE_SYSEX_0: |
| 466 | p[0] |= 0x05; |
| 467 | p[1] = 0xf7; |
| 468 | next_state = STATE_FINISHED; |
| 469 | break; |
| 470 | case STATE_SYSEX_1: |
| 471 | p[0] |= 0x06; |
| 472 | p[1] = port->data[0]; |
| 473 | p[2] = 0xf7; |
| 474 | next_state = STATE_FINISHED; |
| 475 | break; |
| 476 | case STATE_SYSEX_2: |
| 477 | p[0] |= 0x07; |
| 478 | p[1] = port->data[0]; |
| 479 | p[2] = port->data[1]; |
| 480 | p[3] = 0xf7; |
| 481 | next_state = STATE_FINISHED; |
| 482 | break; |
| 483 | default: |
| 484 | /* Ignore byte */ |
| 485 | next_state = port->state; |
| 486 | port->state = STATE_INITIAL; |
| 487 | } |
| 488 | break; |
| 489 | |
| 490 | case 0xf0 ... 0xf6: |
| 491 | /* System Common Messages */ |
| 492 | port->data[0] = port->data[1] = 0; |
| 493 | port->state = STATE_INITIAL; |
| 494 | switch (b) { |
| 495 | case 0xf0: |
| 496 | port->data[0] = b; |
| 497 | port->data[1] = 0; |
| 498 | next_state = STATE_SYSEX_1; |
| 499 | break; |
| 500 | case 0xf1: |
| 501 | case 0xf3: |
| 502 | port->data[0] = b; |
| 503 | next_state = STATE_1PARAM; |
| 504 | break; |
| 505 | case 0xf2: |
| 506 | port->data[0] = b; |
| 507 | next_state = STATE_2PARAM_1; |
| 508 | break; |
| 509 | case 0xf4: |
| 510 | case 0xf5: |
| 511 | next_state = STATE_INITIAL; |
| 512 | break; |
| 513 | case 0xf6: |
| 514 | p[0] |= 0x05; |
| 515 | p[1] = 0xf6; |
| 516 | next_state = STATE_FINISHED; |
| 517 | break; |
| 518 | } |
| 519 | break; |
| 520 | |
| 521 | case 0x80 ... 0xef: |
| 522 | /* |
| 523 | * Channel Voice Messages, Channel Mode Messages |
| 524 | * and Control Change Messages. |
| 525 | */ |
| 526 | port->data[0] = b; |
| 527 | port->data[1] = 0; |
| 528 | port->state = STATE_INITIAL; |
| 529 | if (b >= 0xc0 && b <= 0xdf) |
| 530 | next_state = STATE_1PARAM; |
| 531 | else |
| 532 | next_state = STATE_2PARAM_1; |
| 533 | break; |
| 534 | |
| 535 | case 0x00 ... 0x7f: |
| 536 | /* Message parameters */ |
| 537 | switch (port->state) { |
| 538 | case STATE_1PARAM: |
| 539 | if (port->data[0] < 0xf0) |
| 540 | p[0] |= port->data[0] >> 4; |
| 541 | else |
| 542 | p[0] |= 0x02; |
| 543 | |
| 544 | p[1] = port->data[0]; |
| 545 | p[2] = b; |
| 546 | /* This is to allow Running State Messages */ |
| 547 | next_state = STATE_1PARAM; |
| 548 | break; |
| 549 | case STATE_2PARAM_1: |
| 550 | port->data[1] = b; |
| 551 | next_state = STATE_2PARAM_2; |
| 552 | break; |
| 553 | case STATE_2PARAM_2: |
| 554 | if (port->data[0] < 0xf0) |
| 555 | p[0] |= port->data[0] >> 4; |
| 556 | else |
| 557 | p[0] |= 0x03; |
| 558 | |
| 559 | p[1] = port->data[0]; |
| 560 | p[2] = port->data[1]; |
| 561 | p[3] = b; |
| 562 | /* This is to allow Running State Messages */ |
| 563 | next_state = STATE_2PARAM_1; |
| 564 | break; |
| 565 | case STATE_SYSEX_0: |
| 566 | port->data[0] = b; |
| 567 | next_state = STATE_SYSEX_1; |
| 568 | break; |
| 569 | case STATE_SYSEX_1: |
| 570 | port->data[1] = b; |
| 571 | next_state = STATE_SYSEX_2; |
| 572 | break; |
| 573 | case STATE_SYSEX_2: |
| 574 | p[0] |= 0x04; |
| 575 | p[1] = port->data[0]; |
| 576 | p[2] = port->data[1]; |
| 577 | p[3] = b; |
| 578 | next_state = STATE_SYSEX_0; |
| 579 | break; |
| 580 | } |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | /* States where we have to write into the USB request */ |
| 585 | if (next_state == STATE_FINISHED || |
| 586 | port->state == STATE_SYSEX_2 || |
| 587 | port->state == STATE_1PARAM || |
| 588 | port->state == STATE_2PARAM_2 || |
| 589 | port->state == STATE_REAL_TIME) { |
| 590 | |
| 591 | unsigned int length = req->length; |
| 592 | u8 *buf = (u8 *)req->buf + length; |
| 593 | |
| 594 | memcpy(buf, p, sizeof(p)); |
| 595 | req->length = length + sizeof(p); |
| 596 | |
| 597 | if (next_state == STATE_FINISHED) { |
| 598 | next_state = STATE_INITIAL; |
| 599 | port->data[0] = port->data[1] = 0; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | port->state = next_state; |
| 604 | } |
| 605 | |
| 606 | static int f_midi_do_transmit(struct f_midi *midi, struct usb_ep *ep) |
| 607 | { |
| 608 | struct usb_request *req = NULL; |
| 609 | unsigned int len, i; |
| 610 | bool active = false; |
| 611 | int err; |
| 612 | |
| 613 | /* |
| 614 | * We peek the request in order to reuse it if it fails to enqueue on |
| 615 | * its endpoint |
| 616 | */ |
| 617 | len = kfifo_peek(&midi->in_req_fifo, &req); |
| 618 | if (len != 1) { |
| 619 | ERROR(midi, "%s: Couldn't get usb request\n", __func__); |
| 620 | return -1; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * If buffer overrun, then we ignore this transmission. |
| 625 | * IMPORTANT: This will cause the user-space rawmidi device to block |
| 626 | * until a) usb requests have been completed or b) snd_rawmidi_write() |
| 627 | * times out. |
| 628 | */ |
| 629 | if (req->length > 0) |
| 630 | return 0; |
| 631 | |
| 632 | for (i = midi->in_last_port; i < midi->in_ports; ++i) { |
| 633 | struct gmidi_in_port *port = midi->in_ports_array + i; |
| 634 | struct snd_rawmidi_substream *substream = port->substream; |
| 635 | |
| 636 | if (!port->active || !substream) |
| 637 | continue; |
| 638 | |
| 639 | while (req->length + 3 < midi->buflen) { |
| 640 | uint8_t b; |
| 641 | |
| 642 | if (snd_rawmidi_transmit(substream, &b, 1) != 1) { |
| 643 | port->active = 0; |
| 644 | break; |
| 645 | } |
| 646 | f_midi_transmit_byte(req, port, b); |
| 647 | } |
| 648 | |
| 649 | active = !!port->active; |
| 650 | if (active) |
| 651 | break; |
| 652 | } |
| 653 | midi->in_last_port = active ? i : 0; |
| 654 | |
| 655 | if (req->length <= 0) |
| 656 | goto done; |
| 657 | |
| 658 | err = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 659 | if (err < 0) { |
| 660 | ERROR(midi, "%s failed to queue req: %d\n", |
| 661 | midi->in_ep->name, err); |
| 662 | req->length = 0; /* Re-use request next time. */ |
| 663 | } else { |
| 664 | /* Upon success, put request at the back of the queue. */ |
| 665 | kfifo_skip(&midi->in_req_fifo); |
| 666 | kfifo_put(&midi->in_req_fifo, req); |
| 667 | } |
| 668 | |
| 669 | done: |
| 670 | return active; |
| 671 | } |
| 672 | |
| 673 | static void f_midi_transmit(struct f_midi *midi) |
| 674 | { |
| 675 | struct usb_ep *ep = midi->in_ep; |
| 676 | int ret; |
| 677 | unsigned long flags; |
| 678 | |
| 679 | /* We only care about USB requests if IN endpoint is enabled */ |
| 680 | if (!ep || !ep->enabled) |
| 681 | goto drop_out; |
| 682 | |
| 683 | spin_lock_irqsave(&midi->transmit_lock, flags); |
| 684 | |
| 685 | do { |
| 686 | ret = f_midi_do_transmit(midi, ep); |
| 687 | if (ret < 0) { |
| 688 | spin_unlock_irqrestore(&midi->transmit_lock, flags); |
| 689 | goto drop_out; |
| 690 | } |
| 691 | } while (ret); |
| 692 | |
| 693 | spin_unlock_irqrestore(&midi->transmit_lock, flags); |
| 694 | |
| 695 | return; |
| 696 | |
| 697 | drop_out: |
| 698 | f_midi_drop_out_substreams(midi); |
| 699 | } |
| 700 | |
| 701 | static void f_midi_in_tasklet(unsigned long data) |
| 702 | { |
| 703 | struct f_midi *midi = (struct f_midi *) data; |
| 704 | f_midi_transmit(midi); |
| 705 | } |
| 706 | |
| 707 | static int f_midi_in_open(struct snd_rawmidi_substream *substream) |
| 708 | { |
| 709 | struct f_midi *midi = substream->rmidi->private_data; |
| 710 | struct gmidi_in_port *port; |
| 711 | |
| 712 | if (substream->number >= midi->in_ports) |
| 713 | return -EINVAL; |
| 714 | |
| 715 | VDBG(midi, "%s()\n", __func__); |
| 716 | port = midi->in_ports_array + substream->number; |
| 717 | port->substream = substream; |
| 718 | port->state = STATE_INITIAL; |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int f_midi_in_close(struct snd_rawmidi_substream *substream) |
| 723 | { |
| 724 | struct f_midi *midi = substream->rmidi->private_data; |
| 725 | |
| 726 | VDBG(midi, "%s()\n", __func__); |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static void f_midi_in_trigger(struct snd_rawmidi_substream *substream, int up) |
| 731 | { |
| 732 | struct f_midi *midi = substream->rmidi->private_data; |
| 733 | |
| 734 | if (substream->number >= midi->in_ports) |
| 735 | return; |
| 736 | |
| 737 | VDBG(midi, "%s() %d\n", __func__, up); |
| 738 | midi->in_ports_array[substream->number].active = up; |
| 739 | if (up) |
| 740 | tasklet_hi_schedule(&midi->tasklet); |
| 741 | } |
| 742 | |
| 743 | static int f_midi_out_open(struct snd_rawmidi_substream *substream) |
| 744 | { |
| 745 | struct f_midi *midi = substream->rmidi->private_data; |
| 746 | |
| 747 | if (substream->number >= MAX_PORTS) |
| 748 | return -EINVAL; |
| 749 | |
| 750 | VDBG(midi, "%s()\n", __func__); |
| 751 | midi->out_substream[substream->number] = substream; |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static int f_midi_out_close(struct snd_rawmidi_substream *substream) |
| 756 | { |
| 757 | struct f_midi *midi = substream->rmidi->private_data; |
| 758 | |
| 759 | VDBG(midi, "%s()\n", __func__); |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static void f_midi_out_trigger(struct snd_rawmidi_substream *substream, int up) |
| 764 | { |
| 765 | struct f_midi *midi = substream->rmidi->private_data; |
| 766 | |
| 767 | VDBG(midi, "%s()\n", __func__); |
| 768 | |
| 769 | if (up) |
| 770 | set_bit(substream->number, &midi->out_triggered); |
| 771 | else |
| 772 | clear_bit(substream->number, &midi->out_triggered); |
| 773 | } |
| 774 | |
| 775 | static const struct snd_rawmidi_ops gmidi_in_ops = { |
| 776 | .open = f_midi_in_open, |
| 777 | .close = f_midi_in_close, |
| 778 | .trigger = f_midi_in_trigger, |
| 779 | }; |
| 780 | |
| 781 | static const struct snd_rawmidi_ops gmidi_out_ops = { |
| 782 | .open = f_midi_out_open, |
| 783 | .close = f_midi_out_close, |
| 784 | .trigger = f_midi_out_trigger |
| 785 | }; |
| 786 | |
| 787 | static inline void f_midi_unregister_card(struct f_midi *midi) |
| 788 | { |
| 789 | if (midi->card) { |
| 790 | snd_card_free(midi->card); |
| 791 | midi->card = NULL; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | /* register as a sound "card" */ |
| 796 | static int f_midi_register_card(struct f_midi *midi) |
| 797 | { |
| 798 | struct snd_card *card; |
| 799 | struct snd_rawmidi *rmidi; |
| 800 | int err; |
| 801 | static struct snd_device_ops ops = { |
| 802 | .dev_free = f_midi_snd_free, |
| 803 | }; |
| 804 | |
| 805 | err = snd_card_new(&midi->gadget->dev, midi->index, midi->id, |
| 806 | THIS_MODULE, 0, &card); |
| 807 | if (err < 0) { |
| 808 | ERROR(midi, "snd_card_new() failed\n"); |
| 809 | goto fail; |
| 810 | } |
| 811 | midi->card = card; |
| 812 | |
| 813 | err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, midi, &ops); |
| 814 | if (err < 0) { |
| 815 | ERROR(midi, "snd_device_new() failed: error %d\n", err); |
| 816 | goto fail; |
| 817 | } |
| 818 | |
| 819 | strcpy(card->driver, f_midi_longname); |
| 820 | strcpy(card->longname, f_midi_longname); |
| 821 | strcpy(card->shortname, f_midi_shortname); |
| 822 | |
| 823 | /* Set up rawmidi */ |
| 824 | snd_component_add(card, "MIDI"); |
| 825 | err = snd_rawmidi_new(card, card->longname, 0, |
| 826 | midi->out_ports, midi->in_ports, &rmidi); |
| 827 | if (err < 0) { |
| 828 | ERROR(midi, "snd_rawmidi_new() failed: error %d\n", err); |
| 829 | goto fail; |
| 830 | } |
| 831 | midi->rmidi = rmidi; |
| 832 | midi->in_last_port = 0; |
| 833 | strcpy(rmidi->name, card->shortname); |
| 834 | rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | |
| 835 | SNDRV_RAWMIDI_INFO_INPUT | |
| 836 | SNDRV_RAWMIDI_INFO_DUPLEX; |
| 837 | rmidi->private_data = midi; |
| 838 | rmidi->private_free = f_midi_rmidi_free; |
| 839 | midi->free_ref++; |
| 840 | |
| 841 | /* |
| 842 | * Yes, rawmidi OUTPUT = USB IN, and rawmidi INPUT = USB OUT. |
| 843 | * It's an upside-down world being a gadget. |
| 844 | */ |
| 845 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &gmidi_in_ops); |
| 846 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &gmidi_out_ops); |
| 847 | |
| 848 | /* register it - we're ready to go */ |
| 849 | err = snd_card_register(card); |
| 850 | if (err < 0) { |
| 851 | ERROR(midi, "snd_card_register() failed\n"); |
| 852 | goto fail; |
| 853 | } |
| 854 | |
| 855 | VDBG(midi, "%s() finished ok\n", __func__); |
| 856 | return 0; |
| 857 | |
| 858 | fail: |
| 859 | f_midi_unregister_card(midi); |
| 860 | return err; |
| 861 | } |
| 862 | |
| 863 | /* MIDI function driver setup/binding */ |
| 864 | |
| 865 | static int f_midi_bind(struct usb_configuration *c, struct usb_function *f) |
| 866 | { |
| 867 | struct usb_descriptor_header **midi_function; |
| 868 | struct usb_midi_in_jack_descriptor jack_in_ext_desc[MAX_PORTS]; |
| 869 | struct usb_midi_in_jack_descriptor jack_in_emb_desc[MAX_PORTS]; |
| 870 | struct usb_midi_out_jack_descriptor_1 jack_out_ext_desc[MAX_PORTS]; |
| 871 | struct usb_midi_out_jack_descriptor_1 jack_out_emb_desc[MAX_PORTS]; |
| 872 | struct usb_composite_dev *cdev = c->cdev; |
| 873 | struct f_midi *midi = func_to_midi(f); |
| 874 | struct usb_string *us; |
| 875 | int status, n, jack = 1, i = 0, endpoint_descriptor_index = 0; |
| 876 | |
| 877 | midi->gadget = cdev->gadget; |
| 878 | tasklet_init(&midi->tasklet, f_midi_in_tasklet, (unsigned long) midi); |
| 879 | status = f_midi_register_card(midi); |
| 880 | if (status < 0) |
| 881 | goto fail_register; |
| 882 | |
| 883 | /* maybe allocate device-global string ID */ |
| 884 | us = usb_gstrings_attach(c->cdev, midi_strings, |
| 885 | ARRAY_SIZE(midi_string_defs)); |
| 886 | if (IS_ERR(us)) { |
| 887 | status = PTR_ERR(us); |
| 888 | goto fail; |
| 889 | } |
| 890 | ac_interface_desc.iInterface = us[STRING_FUNC_IDX].id; |
| 891 | |
| 892 | /* We have two interfaces, AudioControl and MIDIStreaming */ |
| 893 | status = usb_interface_id(c, f); |
| 894 | if (status < 0) |
| 895 | goto fail; |
| 896 | ac_interface_desc.bInterfaceNumber = status; |
| 897 | |
| 898 | status = usb_interface_id(c, f); |
| 899 | if (status < 0) |
| 900 | goto fail; |
| 901 | ms_interface_desc.bInterfaceNumber = status; |
| 902 | ac_header_desc.baInterfaceNr[0] = status; |
| 903 | midi->ms_id = status; |
| 904 | |
| 905 | status = -ENODEV; |
| 906 | |
| 907 | /* allocate instance-specific endpoints */ |
| 908 | midi->in_ep = usb_ep_autoconfig(cdev->gadget, &bulk_in_desc); |
| 909 | if (!midi->in_ep) |
| 910 | goto fail; |
| 911 | |
| 912 | midi->out_ep = usb_ep_autoconfig(cdev->gadget, &bulk_out_desc); |
| 913 | if (!midi->out_ep) |
| 914 | goto fail; |
| 915 | |
| 916 | /* allocate temporary function list */ |
| 917 | midi_function = kcalloc((MAX_PORTS * 4) + 11, sizeof(*midi_function), |
| 918 | GFP_KERNEL); |
| 919 | if (!midi_function) { |
| 920 | status = -ENOMEM; |
| 921 | goto fail; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * construct the function's descriptor set. As the number of |
| 926 | * input and output MIDI ports is configurable, we have to do |
| 927 | * it that way. |
| 928 | */ |
| 929 | |
| 930 | /* add the headers - these are always the same */ |
| 931 | midi_function[i++] = (struct usb_descriptor_header *) &ac_interface_desc; |
| 932 | midi_function[i++] = (struct usb_descriptor_header *) &ac_header_desc; |
| 933 | midi_function[i++] = (struct usb_descriptor_header *) &ms_interface_desc; |
| 934 | |
| 935 | /* calculate the header's wTotalLength */ |
| 936 | n = USB_DT_MS_HEADER_SIZE |
| 937 | + (midi->in_ports + midi->out_ports) * |
| 938 | (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1)); |
| 939 | ms_header_desc.wTotalLength = cpu_to_le16(n); |
| 940 | |
| 941 | midi_function[i++] = (struct usb_descriptor_header *) &ms_header_desc; |
| 942 | |
| 943 | /* configure the external IN jacks, each linked to an embedded OUT jack */ |
| 944 | for (n = 0; n < midi->in_ports; n++) { |
| 945 | struct usb_midi_in_jack_descriptor *in_ext = &jack_in_ext_desc[n]; |
| 946 | struct usb_midi_out_jack_descriptor_1 *out_emb = &jack_out_emb_desc[n]; |
| 947 | |
| 948 | in_ext->bLength = USB_DT_MIDI_IN_SIZE; |
| 949 | in_ext->bDescriptorType = USB_DT_CS_INTERFACE; |
| 950 | in_ext->bDescriptorSubtype = USB_MS_MIDI_IN_JACK; |
| 951 | in_ext->bJackType = USB_MS_EXTERNAL; |
| 952 | in_ext->bJackID = jack++; |
| 953 | in_ext->iJack = 0; |
| 954 | midi_function[i++] = (struct usb_descriptor_header *) in_ext; |
| 955 | |
| 956 | out_emb->bLength = USB_DT_MIDI_OUT_SIZE(1); |
| 957 | out_emb->bDescriptorType = USB_DT_CS_INTERFACE; |
| 958 | out_emb->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK; |
| 959 | out_emb->bJackType = USB_MS_EMBEDDED; |
| 960 | out_emb->bJackID = jack++; |
| 961 | out_emb->bNrInputPins = 1; |
| 962 | out_emb->pins[0].baSourcePin = 1; |
| 963 | out_emb->pins[0].baSourceID = in_ext->bJackID; |
| 964 | out_emb->iJack = 0; |
| 965 | midi_function[i++] = (struct usb_descriptor_header *) out_emb; |
| 966 | |
| 967 | /* link it to the endpoint */ |
| 968 | ms_in_desc.baAssocJackID[n] = out_emb->bJackID; |
| 969 | } |
| 970 | |
| 971 | /* configure the external OUT jacks, each linked to an embedded IN jack */ |
| 972 | for (n = 0; n < midi->out_ports; n++) { |
| 973 | struct usb_midi_in_jack_descriptor *in_emb = &jack_in_emb_desc[n]; |
| 974 | struct usb_midi_out_jack_descriptor_1 *out_ext = &jack_out_ext_desc[n]; |
| 975 | |
| 976 | in_emb->bLength = USB_DT_MIDI_IN_SIZE; |
| 977 | in_emb->bDescriptorType = USB_DT_CS_INTERFACE; |
| 978 | in_emb->bDescriptorSubtype = USB_MS_MIDI_IN_JACK; |
| 979 | in_emb->bJackType = USB_MS_EMBEDDED; |
| 980 | in_emb->bJackID = jack++; |
| 981 | in_emb->iJack = 0; |
| 982 | midi_function[i++] = (struct usb_descriptor_header *) in_emb; |
| 983 | |
| 984 | out_ext->bLength = USB_DT_MIDI_OUT_SIZE(1); |
| 985 | out_ext->bDescriptorType = USB_DT_CS_INTERFACE; |
| 986 | out_ext->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK; |
| 987 | out_ext->bJackType = USB_MS_EXTERNAL; |
| 988 | out_ext->bJackID = jack++; |
| 989 | out_ext->bNrInputPins = 1; |
| 990 | out_ext->iJack = 0; |
| 991 | out_ext->pins[0].baSourceID = in_emb->bJackID; |
| 992 | out_ext->pins[0].baSourcePin = 1; |
| 993 | midi_function[i++] = (struct usb_descriptor_header *) out_ext; |
| 994 | |
| 995 | /* link it to the endpoint */ |
| 996 | ms_out_desc.baAssocJackID[n] = in_emb->bJackID; |
| 997 | } |
| 998 | |
| 999 | /* configure the endpoint descriptors ... */ |
| 1000 | ms_out_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->in_ports); |
| 1001 | ms_out_desc.bNumEmbMIDIJack = midi->in_ports; |
| 1002 | |
| 1003 | ms_in_desc.bLength = USB_DT_MS_ENDPOINT_SIZE(midi->out_ports); |
| 1004 | ms_in_desc.bNumEmbMIDIJack = midi->out_ports; |
| 1005 | |
| 1006 | /* ... and add them to the list */ |
| 1007 | endpoint_descriptor_index = i; |
| 1008 | midi_function[i++] = (struct usb_descriptor_header *) &bulk_out_desc; |
| 1009 | midi_function[i++] = (struct usb_descriptor_header *) &ms_out_desc; |
| 1010 | midi_function[i++] = (struct usb_descriptor_header *) &bulk_in_desc; |
| 1011 | midi_function[i++] = (struct usb_descriptor_header *) &ms_in_desc; |
| 1012 | midi_function[i++] = NULL; |
| 1013 | |
| 1014 | /* |
| 1015 | * support all relevant hardware speeds... we expect that when |
| 1016 | * hardware is dual speed, all bulk-capable endpoints work at |
| 1017 | * both speeds |
| 1018 | */ |
| 1019 | /* copy descriptors, and track endpoint copies */ |
| 1020 | f->fs_descriptors = usb_copy_descriptors(midi_function); |
| 1021 | if (!f->fs_descriptors) |
| 1022 | goto fail_f_midi; |
| 1023 | |
| 1024 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 1025 | bulk_in_desc.wMaxPacketSize = cpu_to_le16(512); |
| 1026 | bulk_out_desc.wMaxPacketSize = cpu_to_le16(512); |
| 1027 | f->hs_descriptors = usb_copy_descriptors(midi_function); |
| 1028 | if (!f->hs_descriptors) |
| 1029 | goto fail_f_midi; |
| 1030 | } |
| 1031 | |
| 1032 | if (gadget_is_superspeed(c->cdev->gadget)) { |
| 1033 | bulk_in_desc.wMaxPacketSize = cpu_to_le16(1024); |
| 1034 | bulk_out_desc.wMaxPacketSize = cpu_to_le16(1024); |
| 1035 | i = endpoint_descriptor_index; |
| 1036 | midi_function[i++] = (struct usb_descriptor_header *) |
| 1037 | &bulk_out_desc; |
| 1038 | midi_function[i++] = (struct usb_descriptor_header *) |
| 1039 | &bulk_out_ss_comp_desc; |
| 1040 | midi_function[i++] = (struct usb_descriptor_header *) |
| 1041 | &ms_out_desc; |
| 1042 | midi_function[i++] = (struct usb_descriptor_header *) |
| 1043 | &bulk_in_desc; |
| 1044 | midi_function[i++] = (struct usb_descriptor_header *) |
| 1045 | &bulk_in_ss_comp_desc; |
| 1046 | midi_function[i++] = (struct usb_descriptor_header *) |
| 1047 | &ms_in_desc; |
| 1048 | f->ss_descriptors = usb_copy_descriptors(midi_function); |
| 1049 | if (!f->ss_descriptors) |
| 1050 | goto fail_f_midi; |
| 1051 | |
| 1052 | if (gadget_is_superspeed_plus(c->cdev->gadget)) { |
| 1053 | f->ssp_descriptors = usb_copy_descriptors(midi_function); |
| 1054 | if (!f->ssp_descriptors) |
| 1055 | goto fail_f_midi; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | kfree(midi_function); |
| 1060 | |
| 1061 | return 0; |
| 1062 | |
| 1063 | fail_f_midi: |
| 1064 | kfree(midi_function); |
| 1065 | usb_free_all_descriptors(f); |
| 1066 | fail: |
| 1067 | f_midi_unregister_card(midi); |
| 1068 | fail_register: |
| 1069 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); |
| 1070 | |
| 1071 | return status; |
| 1072 | } |
| 1073 | |
| 1074 | static inline struct f_midi_opts *to_f_midi_opts(struct config_item *item) |
| 1075 | { |
| 1076 | return container_of(to_config_group(item), struct f_midi_opts, |
| 1077 | func_inst.group); |
| 1078 | } |
| 1079 | |
| 1080 | static void midi_attr_release(struct config_item *item) |
| 1081 | { |
| 1082 | struct f_midi_opts *opts = to_f_midi_opts(item); |
| 1083 | |
| 1084 | usb_put_function_instance(&opts->func_inst); |
| 1085 | } |
| 1086 | |
| 1087 | static struct configfs_item_operations midi_item_ops = { |
| 1088 | .release = midi_attr_release, |
| 1089 | }; |
| 1090 | |
| 1091 | #define F_MIDI_OPT(name, test_limit, limit) \ |
| 1092 | static ssize_t f_midi_opts_##name##_show(struct config_item *item, char *page) \ |
| 1093 | { \ |
| 1094 | struct f_midi_opts *opts = to_f_midi_opts(item); \ |
| 1095 | int result; \ |
| 1096 | \ |
| 1097 | mutex_lock(&opts->lock); \ |
| 1098 | result = sprintf(page, "%d\n", opts->name); \ |
| 1099 | mutex_unlock(&opts->lock); \ |
| 1100 | \ |
| 1101 | return result; \ |
| 1102 | } \ |
| 1103 | \ |
| 1104 | static ssize_t f_midi_opts_##name##_store(struct config_item *item, \ |
| 1105 | const char *page, size_t len) \ |
| 1106 | { \ |
| 1107 | struct f_midi_opts *opts = to_f_midi_opts(item); \ |
| 1108 | int ret; \ |
| 1109 | u32 num; \ |
| 1110 | \ |
| 1111 | mutex_lock(&opts->lock); \ |
| 1112 | if (opts->refcnt > 1) { \ |
| 1113 | ret = -EBUSY; \ |
| 1114 | goto end; \ |
| 1115 | } \ |
| 1116 | \ |
| 1117 | ret = kstrtou32(page, 0, &num); \ |
| 1118 | if (ret) \ |
| 1119 | goto end; \ |
| 1120 | \ |
| 1121 | if (test_limit && num > limit) { \ |
| 1122 | ret = -EINVAL; \ |
| 1123 | goto end; \ |
| 1124 | } \ |
| 1125 | opts->name = num; \ |
| 1126 | ret = len; \ |
| 1127 | \ |
| 1128 | end: \ |
| 1129 | mutex_unlock(&opts->lock); \ |
| 1130 | return ret; \ |
| 1131 | } \ |
| 1132 | \ |
| 1133 | CONFIGFS_ATTR(f_midi_opts_, name); |
| 1134 | |
| 1135 | F_MIDI_OPT(index, true, SNDRV_CARDS); |
| 1136 | F_MIDI_OPT(buflen, false, 0); |
| 1137 | F_MIDI_OPT(qlen, false, 0); |
| 1138 | F_MIDI_OPT(in_ports, true, MAX_PORTS); |
| 1139 | F_MIDI_OPT(out_ports, true, MAX_PORTS); |
| 1140 | |
| 1141 | static ssize_t f_midi_opts_id_show(struct config_item *item, char *page) |
| 1142 | { |
| 1143 | struct f_midi_opts *opts = to_f_midi_opts(item); |
| 1144 | int result; |
| 1145 | |
| 1146 | mutex_lock(&opts->lock); |
| 1147 | if (opts->id) { |
| 1148 | result = strlcpy(page, opts->id, PAGE_SIZE); |
| 1149 | } else { |
| 1150 | page[0] = 0; |
| 1151 | result = 0; |
| 1152 | } |
| 1153 | |
| 1154 | mutex_unlock(&opts->lock); |
| 1155 | |
| 1156 | return result; |
| 1157 | } |
| 1158 | |
| 1159 | static ssize_t f_midi_opts_id_store(struct config_item *item, |
| 1160 | const char *page, size_t len) |
| 1161 | { |
| 1162 | struct f_midi_opts *opts = to_f_midi_opts(item); |
| 1163 | int ret; |
| 1164 | char *c; |
| 1165 | |
| 1166 | mutex_lock(&opts->lock); |
| 1167 | if (opts->refcnt > 1) { |
| 1168 | ret = -EBUSY; |
| 1169 | goto end; |
| 1170 | } |
| 1171 | |
| 1172 | c = kstrndup(page, len, GFP_KERNEL); |
| 1173 | if (!c) { |
| 1174 | ret = -ENOMEM; |
| 1175 | goto end; |
| 1176 | } |
| 1177 | if (opts->id_allocated) |
| 1178 | kfree(opts->id); |
| 1179 | opts->id = c; |
| 1180 | opts->id_allocated = true; |
| 1181 | ret = len; |
| 1182 | end: |
| 1183 | mutex_unlock(&opts->lock); |
| 1184 | return ret; |
| 1185 | } |
| 1186 | |
| 1187 | CONFIGFS_ATTR(f_midi_opts_, id); |
| 1188 | |
| 1189 | static struct configfs_attribute *midi_attrs[] = { |
| 1190 | &f_midi_opts_attr_index, |
| 1191 | &f_midi_opts_attr_buflen, |
| 1192 | &f_midi_opts_attr_qlen, |
| 1193 | &f_midi_opts_attr_in_ports, |
| 1194 | &f_midi_opts_attr_out_ports, |
| 1195 | &f_midi_opts_attr_id, |
| 1196 | NULL, |
| 1197 | }; |
| 1198 | |
| 1199 | static const struct config_item_type midi_func_type = { |
| 1200 | .ct_item_ops = &midi_item_ops, |
| 1201 | .ct_attrs = midi_attrs, |
| 1202 | .ct_owner = THIS_MODULE, |
| 1203 | }; |
| 1204 | |
| 1205 | static void f_midi_free_inst(struct usb_function_instance *f) |
| 1206 | { |
| 1207 | struct f_midi_opts *opts; |
| 1208 | bool free = false; |
| 1209 | |
| 1210 | opts = container_of(f, struct f_midi_opts, func_inst); |
| 1211 | |
| 1212 | mutex_lock(&opts->lock); |
| 1213 | if (!--opts->refcnt) { |
| 1214 | free = true; |
| 1215 | } |
| 1216 | mutex_unlock(&opts->lock); |
| 1217 | |
| 1218 | if (free) { |
| 1219 | if (opts->id_allocated) |
| 1220 | kfree(opts->id); |
| 1221 | kfree(opts); |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | #ifdef CONFIG_USB_CONFIGFS_UEVENT |
| 1226 | extern struct device *create_function_device(char *name); |
| 1227 | static ssize_t alsa_show(struct device *dev, |
| 1228 | struct device_attribute *attr, char *buf) |
| 1229 | { |
| 1230 | struct usb_function_instance *fi_midi = dev_get_drvdata(dev); |
| 1231 | struct f_midi *midi; |
| 1232 | |
| 1233 | if (!fi_midi->f) |
| 1234 | dev_warn(dev, "f_midi: function not set\n"); |
| 1235 | |
| 1236 | if (fi_midi && fi_midi->f) { |
| 1237 | midi = func_to_midi(fi_midi->f); |
| 1238 | if (midi->rmidi && midi->rmidi->card) |
| 1239 | return sprintf(buf, "%d %d\n", |
| 1240 | midi->rmidi->card->number, midi->rmidi->device); |
| 1241 | } |
| 1242 | |
| 1243 | /* print PCM card and device numbers */ |
| 1244 | return sprintf(buf, "%d %d\n", -1, -1); |
| 1245 | } |
| 1246 | |
| 1247 | static DEVICE_ATTR(alsa, S_IRUGO, alsa_show, NULL); |
| 1248 | |
| 1249 | static struct device_attribute *alsa_function_attributes[] = { |
| 1250 | &dev_attr_alsa, |
| 1251 | NULL |
| 1252 | }; |
| 1253 | |
| 1254 | static int create_alsa_device(struct usb_function_instance *fi) |
| 1255 | { |
| 1256 | struct device *dev; |
| 1257 | struct device_attribute **attrs; |
| 1258 | struct device_attribute *attr; |
| 1259 | int err = 0; |
| 1260 | |
| 1261 | dev = create_function_device("f_midi"); |
| 1262 | if (IS_ERR(dev)) |
| 1263 | return PTR_ERR(dev); |
| 1264 | |
| 1265 | attrs = alsa_function_attributes; |
| 1266 | if (attrs) { |
| 1267 | while ((attr = *attrs++) && !err) |
| 1268 | err = device_create_file(dev, attr); |
| 1269 | if (err) { |
| 1270 | device_destroy(dev->class, dev->devt); |
| 1271 | return -EINVAL; |
| 1272 | } |
| 1273 | } |
| 1274 | dev_set_drvdata(dev, fi); |
| 1275 | return 0; |
| 1276 | } |
| 1277 | #else |
| 1278 | static int create_alsa_device(struct usb_function_instance *fi) |
| 1279 | { |
| 1280 | return 0; |
| 1281 | } |
| 1282 | #endif |
| 1283 | |
| 1284 | static struct usb_function_instance *f_midi_alloc_inst(void) |
| 1285 | { |
| 1286 | struct f_midi_opts *opts; |
| 1287 | |
| 1288 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 1289 | if (!opts) |
| 1290 | return ERR_PTR(-ENOMEM); |
| 1291 | |
| 1292 | mutex_init(&opts->lock); |
| 1293 | opts->func_inst.free_func_inst = f_midi_free_inst; |
| 1294 | opts->index = SNDRV_DEFAULT_IDX1; |
| 1295 | opts->id = SNDRV_DEFAULT_STR1; |
| 1296 | opts->buflen = 512; |
| 1297 | opts->qlen = 32; |
| 1298 | opts->in_ports = 1; |
| 1299 | opts->out_ports = 1; |
| 1300 | opts->refcnt = 1; |
| 1301 | |
| 1302 | if (create_alsa_device(&opts->func_inst)) { |
| 1303 | kfree(opts); |
| 1304 | return ERR_PTR(-ENODEV); |
| 1305 | } |
| 1306 | |
| 1307 | config_group_init_type_name(&opts->func_inst.group, "", |
| 1308 | &midi_func_type); |
| 1309 | |
| 1310 | return &opts->func_inst; |
| 1311 | } |
| 1312 | |
| 1313 | static void f_midi_free(struct usb_function *f) |
| 1314 | { |
| 1315 | struct f_midi *midi; |
| 1316 | struct f_midi_opts *opts; |
| 1317 | bool free = false; |
| 1318 | |
| 1319 | midi = func_to_midi(f); |
| 1320 | opts = container_of(f->fi, struct f_midi_opts, func_inst); |
| 1321 | mutex_lock(&opts->lock); |
| 1322 | if (!--midi->free_ref) { |
| 1323 | kfree(midi->id); |
| 1324 | kfifo_free(&midi->in_req_fifo); |
| 1325 | kfree(midi); |
| 1326 | free = true; |
| 1327 | opts->func_inst.f = NULL; |
| 1328 | } |
| 1329 | mutex_unlock(&opts->lock); |
| 1330 | |
| 1331 | if (free) |
| 1332 | f_midi_free_inst(&opts->func_inst); |
| 1333 | } |
| 1334 | |
| 1335 | static void f_midi_rmidi_free(struct snd_rawmidi *rmidi) |
| 1336 | { |
| 1337 | f_midi_free(rmidi->private_data); |
| 1338 | } |
| 1339 | |
| 1340 | static void f_midi_unbind(struct usb_configuration *c, struct usb_function *f) |
| 1341 | { |
| 1342 | struct usb_composite_dev *cdev = f->config->cdev; |
| 1343 | struct f_midi *midi = func_to_midi(f); |
| 1344 | struct snd_card *card; |
| 1345 | |
| 1346 | DBG(cdev, "unbind\n"); |
| 1347 | |
| 1348 | /* just to be sure */ |
| 1349 | f_midi_disable(f); |
| 1350 | |
| 1351 | card = midi->card; |
| 1352 | midi->card = NULL; |
| 1353 | if (card) |
| 1354 | snd_card_free_when_closed(card); |
| 1355 | |
| 1356 | usb_free_all_descriptors(f); |
| 1357 | } |
| 1358 | |
| 1359 | static struct usb_function *f_midi_alloc(struct usb_function_instance *fi) |
| 1360 | { |
| 1361 | struct f_midi *midi = NULL; |
| 1362 | struct f_midi_opts *opts; |
| 1363 | int status, i; |
| 1364 | |
| 1365 | opts = container_of(fi, struct f_midi_opts, func_inst); |
| 1366 | |
| 1367 | mutex_lock(&opts->lock); |
| 1368 | /* sanity check */ |
| 1369 | if (opts->in_ports > MAX_PORTS || opts->out_ports > MAX_PORTS) { |
| 1370 | status = -EINVAL; |
| 1371 | goto setup_fail; |
| 1372 | } |
| 1373 | |
| 1374 | /* allocate and initialize one new instance */ |
| 1375 | midi = kzalloc(struct_size(midi, in_ports_array, opts->in_ports), |
| 1376 | GFP_KERNEL); |
| 1377 | if (!midi) { |
| 1378 | status = -ENOMEM; |
| 1379 | goto setup_fail; |
| 1380 | } |
| 1381 | |
| 1382 | for (i = 0; i < opts->in_ports; i++) |
| 1383 | midi->in_ports_array[i].cable = i; |
| 1384 | |
| 1385 | /* set up ALSA midi devices */ |
| 1386 | midi->id = kstrdup(opts->id, GFP_KERNEL); |
| 1387 | if (opts->id && !midi->id) { |
| 1388 | status = -ENOMEM; |
| 1389 | goto midi_free; |
| 1390 | } |
| 1391 | midi->in_ports = opts->in_ports; |
| 1392 | midi->out_ports = opts->out_ports; |
| 1393 | midi->index = opts->index; |
| 1394 | midi->buflen = opts->buflen; |
| 1395 | midi->qlen = opts->qlen; |
| 1396 | midi->in_last_port = 0; |
| 1397 | midi->free_ref = 1; |
| 1398 | |
| 1399 | status = kfifo_alloc(&midi->in_req_fifo, midi->qlen, GFP_KERNEL); |
| 1400 | if (status) |
| 1401 | goto midi_free; |
| 1402 | |
| 1403 | spin_lock_init(&midi->transmit_lock); |
| 1404 | |
| 1405 | ++opts->refcnt; |
| 1406 | mutex_unlock(&opts->lock); |
| 1407 | |
| 1408 | midi->func.name = "gmidi function"; |
| 1409 | midi->func.bind = f_midi_bind; |
| 1410 | midi->func.unbind = f_midi_unbind; |
| 1411 | midi->func.set_alt = f_midi_set_alt; |
| 1412 | midi->func.disable = f_midi_disable; |
| 1413 | midi->func.free_func = f_midi_free; |
| 1414 | |
| 1415 | fi->f = &midi->func; |
| 1416 | return &midi->func; |
| 1417 | |
| 1418 | midi_free: |
| 1419 | if (midi) |
| 1420 | kfree(midi->id); |
| 1421 | kfree(midi); |
| 1422 | setup_fail: |
| 1423 | mutex_unlock(&opts->lock); |
| 1424 | |
| 1425 | return ERR_PTR(status); |
| 1426 | } |
| 1427 | |
| 1428 | DECLARE_USB_FUNCTION_INIT(midi, f_midi_alloc_inst, f_midi_alloc); |