| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Line 6 Linux USB driver |
| 4 | * |
| 5 | * Copyright (C) 2004-2010 Markus Grabner (line6@grabner-graz.at) |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/export.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/usb.h> |
| 13 | |
| 14 | #include <sound/core.h> |
| 15 | #include <sound/initval.h> |
| 16 | #include <sound/hwdep.h> |
| 17 | |
| 18 | #include "capture.h" |
| 19 | #include "driver.h" |
| 20 | #include "midi.h" |
| 21 | #include "playback.h" |
| 22 | |
| 23 | #define DRIVER_AUTHOR "Markus Grabner <line6@grabner-graz.at>" |
| 24 | #define DRIVER_DESC "Line 6 USB Driver" |
| 25 | |
| 26 | /* |
| 27 | This is Line 6's MIDI manufacturer ID. |
| 28 | */ |
| 29 | const unsigned char line6_midi_id[3] = { |
| 30 | 0x00, 0x01, 0x0c |
| 31 | }; |
| 32 | EXPORT_SYMBOL_GPL(line6_midi_id); |
| 33 | |
| 34 | /* |
| 35 | Code to request version of POD, Variax interface |
| 36 | (and maybe other devices). |
| 37 | */ |
| 38 | static const char line6_request_version[] = { |
| 39 | 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7 |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | Class for asynchronous messages. |
| 44 | */ |
| 45 | struct message { |
| 46 | struct usb_line6 *line6; |
| 47 | const char *buffer; |
| 48 | int size; |
| 49 | int done; |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | Forward declarations. |
| 54 | */ |
| 55 | static void line6_data_received(struct urb *urb); |
| 56 | static int line6_send_raw_message_async_part(struct message *msg, |
| 57 | struct urb *urb); |
| 58 | |
| 59 | /* |
| 60 | Start to listen on endpoint. |
| 61 | */ |
| 62 | static int line6_start_listen(struct usb_line6 *line6) |
| 63 | { |
| 64 | int err; |
| 65 | |
| 66 | if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 67 | usb_fill_int_urb(urb: line6->urb_listen, dev: line6->usbdev, |
| 68 | usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r), |
| 69 | transfer_buffer: line6->buffer_listen, LINE6_BUFSIZE_LISTEN, |
| 70 | complete_fn: line6_data_received, context: line6, interval: line6->interval); |
| 71 | } else { |
| 72 | usb_fill_bulk_urb(urb: line6->urb_listen, dev: line6->usbdev, |
| 73 | usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r), |
| 74 | transfer_buffer: line6->buffer_listen, LINE6_BUFSIZE_LISTEN, |
| 75 | complete_fn: line6_data_received, context: line6); |
| 76 | } |
| 77 | |
| 78 | /* sanity checks of EP before actually submitting */ |
| 79 | if (usb_urb_ep_type_check(urb: line6->urb_listen)) { |
| 80 | dev_err(line6->ifcdev, "invalid control EP\n" ); |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | |
| 84 | line6->urb_listen->actual_length = 0; |
| 85 | err = usb_submit_urb(urb: line6->urb_listen, GFP_ATOMIC); |
| 86 | return err; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | Stop listening on endpoint. |
| 91 | */ |
| 92 | static void line6_stop_listen(struct usb_line6 *line6) |
| 93 | { |
| 94 | usb_kill_urb(urb: line6->urb_listen); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | Send raw message in pieces of wMaxPacketSize bytes. |
| 99 | */ |
| 100 | int line6_send_raw_message(struct usb_line6 *line6, const char *buffer, |
| 101 | int size) |
| 102 | { |
| 103 | int i, done = 0; |
| 104 | const struct line6_properties *properties = line6->properties; |
| 105 | |
| 106 | for (i = 0; i < size; i += line6->max_packet_size) { |
| 107 | int partial; |
| 108 | const char *frag_buf = buffer + i; |
| 109 | int frag_size = min(line6->max_packet_size, size - i); |
| 110 | int retval; |
| 111 | |
| 112 | if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 113 | retval = usb_interrupt_msg(usb_dev: line6->usbdev, |
| 114 | usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w), |
| 115 | data: (char *)frag_buf, len: frag_size, |
| 116 | actual_length: &partial, LINE6_TIMEOUT); |
| 117 | } else { |
| 118 | retval = usb_bulk_msg(usb_dev: line6->usbdev, |
| 119 | usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w), |
| 120 | data: (char *)frag_buf, len: frag_size, |
| 121 | actual_length: &partial, LINE6_TIMEOUT); |
| 122 | } |
| 123 | |
| 124 | if (retval) { |
| 125 | dev_err(line6->ifcdev, |
| 126 | "usb_bulk_msg failed (%d)\n" , retval); |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | done += frag_size; |
| 131 | } |
| 132 | |
| 133 | return done; |
| 134 | } |
| 135 | EXPORT_SYMBOL_GPL(line6_send_raw_message); |
| 136 | |
| 137 | /* |
| 138 | Notification of completion of asynchronous request transmission. |
| 139 | */ |
| 140 | static void line6_async_request_sent(struct urb *urb) |
| 141 | { |
| 142 | struct message *msg = (struct message *)urb->context; |
| 143 | |
| 144 | if (msg->done >= msg->size) { |
| 145 | usb_free_urb(urb); |
| 146 | kfree(objp: msg); |
| 147 | } else |
| 148 | line6_send_raw_message_async_part(msg, urb); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | Asynchronously send part of a raw message. |
| 153 | */ |
| 154 | static int line6_send_raw_message_async_part(struct message *msg, |
| 155 | struct urb *urb) |
| 156 | { |
| 157 | int retval; |
| 158 | struct usb_line6 *line6 = msg->line6; |
| 159 | int done = msg->done; |
| 160 | int bytes = min(msg->size - done, line6->max_packet_size); |
| 161 | |
| 162 | if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 163 | usb_fill_int_urb(urb, dev: line6->usbdev, |
| 164 | usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w), |
| 165 | transfer_buffer: (char *)msg->buffer + done, buffer_length: bytes, |
| 166 | complete_fn: line6_async_request_sent, context: msg, interval: line6->interval); |
| 167 | } else { |
| 168 | usb_fill_bulk_urb(urb, dev: line6->usbdev, |
| 169 | usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w), |
| 170 | transfer_buffer: (char *)msg->buffer + done, buffer_length: bytes, |
| 171 | complete_fn: line6_async_request_sent, context: msg); |
| 172 | } |
| 173 | |
| 174 | msg->done += bytes; |
| 175 | |
| 176 | /* sanity checks of EP before actually submitting */ |
| 177 | retval = usb_urb_ep_type_check(urb); |
| 178 | if (retval < 0) |
| 179 | goto error; |
| 180 | |
| 181 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 182 | if (retval < 0) |
| 183 | goto error; |
| 184 | |
| 185 | return 0; |
| 186 | |
| 187 | error: |
| 188 | dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n" , |
| 189 | __func__, retval); |
| 190 | usb_free_urb(urb); |
| 191 | kfree(objp: msg); |
| 192 | return retval; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | Asynchronously send raw message. |
| 197 | */ |
| 198 | int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer, |
| 199 | int size) |
| 200 | { |
| 201 | struct message *msg; |
| 202 | struct urb *urb; |
| 203 | |
| 204 | /* create message: */ |
| 205 | msg = kzalloc(sizeof(struct message), GFP_ATOMIC); |
| 206 | if (msg == NULL) |
| 207 | return -ENOMEM; |
| 208 | |
| 209 | /* create URB: */ |
| 210 | urb = usb_alloc_urb(iso_packets: 0, GFP_ATOMIC); |
| 211 | |
| 212 | if (urb == NULL) { |
| 213 | kfree(objp: msg); |
| 214 | return -ENOMEM; |
| 215 | } |
| 216 | |
| 217 | /* set message data: */ |
| 218 | msg->line6 = line6; |
| 219 | msg->buffer = buffer; |
| 220 | msg->size = size; |
| 221 | msg->done = 0; |
| 222 | |
| 223 | /* start sending: */ |
| 224 | return line6_send_raw_message_async_part(msg, urb); |
| 225 | } |
| 226 | EXPORT_SYMBOL_GPL(line6_send_raw_message_async); |
| 227 | |
| 228 | /* |
| 229 | Send asynchronous device version request. |
| 230 | */ |
| 231 | int line6_version_request_async(struct usb_line6 *line6) |
| 232 | { |
| 233 | char *buffer; |
| 234 | int retval; |
| 235 | |
| 236 | buffer = kmemdup(line6_request_version, |
| 237 | sizeof(line6_request_version), GFP_ATOMIC); |
| 238 | if (buffer == NULL) |
| 239 | return -ENOMEM; |
| 240 | |
| 241 | retval = line6_send_raw_message_async(line6, buffer, |
| 242 | sizeof(line6_request_version)); |
| 243 | kfree(objp: buffer); |
| 244 | return retval; |
| 245 | } |
| 246 | EXPORT_SYMBOL_GPL(line6_version_request_async); |
| 247 | |
| 248 | /* |
| 249 | Send sysex message in pieces of wMaxPacketSize bytes. |
| 250 | */ |
| 251 | int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer, |
| 252 | int size) |
| 253 | { |
| 254 | return line6_send_raw_message(line6, buffer, |
| 255 | size + SYSEX_EXTRA_SIZE) - |
| 256 | SYSEX_EXTRA_SIZE; |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(line6_send_sysex_message); |
| 259 | |
| 260 | /* |
| 261 | Allocate buffer for sysex message and prepare header. |
| 262 | @param code sysex message code |
| 263 | @param size number of bytes between code and sysex end |
| 264 | */ |
| 265 | char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2, |
| 266 | int size) |
| 267 | { |
| 268 | char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC); |
| 269 | |
| 270 | if (!buffer) |
| 271 | return NULL; |
| 272 | |
| 273 | buffer[0] = LINE6_SYSEX_BEGIN; |
| 274 | memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id)); |
| 275 | buffer[sizeof(line6_midi_id) + 1] = code1; |
| 276 | buffer[sizeof(line6_midi_id) + 2] = code2; |
| 277 | buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END; |
| 278 | return buffer; |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer); |
| 281 | |
| 282 | /* |
| 283 | Notification of data received from the Line 6 device. |
| 284 | */ |
| 285 | static void line6_data_received(struct urb *urb) |
| 286 | { |
| 287 | struct usb_line6 *line6 = (struct usb_line6 *)urb->context; |
| 288 | struct midi_buffer *mb = &line6->line6midi->midibuf_in; |
| 289 | int done; |
| 290 | |
| 291 | if (urb->status == -ESHUTDOWN) |
| 292 | return; |
| 293 | |
| 294 | if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 295 | scoped_guard(spinlock_irqsave, &line6->line6midi->lock) { |
| 296 | done = |
| 297 | line6_midibuf_write(mb, data: urb->transfer_buffer, length: urb->actual_length); |
| 298 | |
| 299 | if (done < urb->actual_length) { |
| 300 | line6_midibuf_ignore(mb, length: done); |
| 301 | dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n" , |
| 302 | done, urb->actual_length); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | for (;;) { |
| 307 | scoped_guard(spinlock_irqsave, &line6->line6midi->lock) { |
| 308 | done = |
| 309 | line6_midibuf_read(mb, data: line6->buffer_message, |
| 310 | LINE6_MIDI_MESSAGE_MAXLEN, |
| 311 | LINE6_MIDIBUF_READ_RX); |
| 312 | } |
| 313 | |
| 314 | if (done <= 0) |
| 315 | break; |
| 316 | |
| 317 | line6->message_length = done; |
| 318 | line6_midi_receive(line6, data: line6->buffer_message, length: done); |
| 319 | |
| 320 | if (line6->process_message) |
| 321 | line6->process_message(line6); |
| 322 | } |
| 323 | } else { |
| 324 | line6->buffer_message = urb->transfer_buffer; |
| 325 | line6->message_length = urb->actual_length; |
| 326 | if (line6->process_message) |
| 327 | line6->process_message(line6); |
| 328 | line6->buffer_message = NULL; |
| 329 | } |
| 330 | |
| 331 | line6_start_listen(line6); |
| 332 | } |
| 333 | |
| 334 | #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */ |
| 335 | #define LINE6_READ_WRITE_MAX_RETRIES 50 |
| 336 | |
| 337 | /* |
| 338 | Read data from device. |
| 339 | */ |
| 340 | int line6_read_data(struct usb_line6 *line6, unsigned address, void *data, |
| 341 | unsigned datalen) |
| 342 | { |
| 343 | struct usb_device *usbdev = line6->usbdev; |
| 344 | int ret; |
| 345 | u8 len; |
| 346 | unsigned count; |
| 347 | |
| 348 | if (address > 0xffff || datalen > 0xff) |
| 349 | return -EINVAL; |
| 350 | |
| 351 | /* query the serial number: */ |
| 352 | ret = usb_control_msg_send(dev: usbdev, endpoint: 0, request: 0x67, |
| 353 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 354 | value: (datalen << 8) | 0x21, index: address, NULL, size: 0, |
| 355 | LINE6_TIMEOUT, GFP_KERNEL); |
| 356 | if (ret) { |
| 357 | dev_err(line6->ifcdev, "read request failed (error %d)\n" , ret); |
| 358 | goto exit; |
| 359 | } |
| 360 | |
| 361 | /* Wait for data length. We'll get 0xff until length arrives. */ |
| 362 | for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) { |
| 363 | mdelay(LINE6_READ_WRITE_STATUS_DELAY); |
| 364 | |
| 365 | ret = usb_control_msg_recv(dev: usbdev, endpoint: 0, request: 0x67, |
| 366 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 367 | value: 0x0012, index: 0x0000, data: &len, size: 1, |
| 368 | LINE6_TIMEOUT, GFP_KERNEL); |
| 369 | if (ret) { |
| 370 | dev_err(line6->ifcdev, |
| 371 | "receive length failed (error %d)\n" , ret); |
| 372 | goto exit; |
| 373 | } |
| 374 | |
| 375 | if (len != 0xff) |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | ret = -EIO; |
| 380 | if (len == 0xff) { |
| 381 | dev_err(line6->ifcdev, "read failed after %d retries\n" , |
| 382 | count); |
| 383 | goto exit; |
| 384 | } else if (len != datalen) { |
| 385 | /* should be equal or something went wrong */ |
| 386 | dev_err(line6->ifcdev, |
| 387 | "length mismatch (expected %d, got %d)\n" , |
| 388 | (int)datalen, len); |
| 389 | goto exit; |
| 390 | } |
| 391 | |
| 392 | /* receive the result: */ |
| 393 | ret = usb_control_msg_recv(dev: usbdev, endpoint: 0, request: 0x67, |
| 394 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 395 | value: 0x0013, index: 0x0000, data, size: datalen, LINE6_TIMEOUT, |
| 396 | GFP_KERNEL); |
| 397 | if (ret) |
| 398 | dev_err(line6->ifcdev, "read failed (error %d)\n" , ret); |
| 399 | |
| 400 | exit: |
| 401 | return ret; |
| 402 | } |
| 403 | EXPORT_SYMBOL_GPL(line6_read_data); |
| 404 | |
| 405 | /* |
| 406 | Write data to device. |
| 407 | */ |
| 408 | int line6_write_data(struct usb_line6 *line6, unsigned address, void *data, |
| 409 | unsigned datalen) |
| 410 | { |
| 411 | struct usb_device *usbdev = line6->usbdev; |
| 412 | int ret; |
| 413 | unsigned char *status; |
| 414 | int count; |
| 415 | |
| 416 | if (address > 0xffff || datalen > 0xffff) |
| 417 | return -EINVAL; |
| 418 | |
| 419 | status = kmalloc(1, GFP_KERNEL); |
| 420 | if (!status) |
| 421 | return -ENOMEM; |
| 422 | |
| 423 | ret = usb_control_msg_send(dev: usbdev, endpoint: 0, request: 0x67, |
| 424 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 425 | value: 0x0022, index: address, data, size: datalen, LINE6_TIMEOUT, |
| 426 | GFP_KERNEL); |
| 427 | if (ret) { |
| 428 | dev_err(line6->ifcdev, |
| 429 | "write request failed (error %d)\n" , ret); |
| 430 | goto exit; |
| 431 | } |
| 432 | |
| 433 | for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) { |
| 434 | mdelay(LINE6_READ_WRITE_STATUS_DELAY); |
| 435 | |
| 436 | ret = usb_control_msg_recv(dev: usbdev, endpoint: 0, request: 0x67, |
| 437 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 438 | value: 0x0012, index: 0x0000, data: status, size: 1, LINE6_TIMEOUT, |
| 439 | GFP_KERNEL); |
| 440 | if (ret) { |
| 441 | dev_err(line6->ifcdev, |
| 442 | "receiving status failed (error %d)\n" , ret); |
| 443 | goto exit; |
| 444 | } |
| 445 | |
| 446 | if (*status != 0xff) |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | if (*status == 0xff) { |
| 451 | dev_err(line6->ifcdev, "write failed after %d retries\n" , |
| 452 | count); |
| 453 | ret = -EIO; |
| 454 | } else if (*status != 0) { |
| 455 | dev_err(line6->ifcdev, "write failed (error %d)\n" , ret); |
| 456 | ret = -EIO; |
| 457 | } |
| 458 | exit: |
| 459 | kfree(objp: status); |
| 460 | return ret; |
| 461 | } |
| 462 | EXPORT_SYMBOL_GPL(line6_write_data); |
| 463 | |
| 464 | /* |
| 465 | Read Line 6 device serial number. |
| 466 | (POD, TonePort, GuitarPort) |
| 467 | */ |
| 468 | int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number) |
| 469 | { |
| 470 | return line6_read_data(line6, 0x80d0, serial_number, |
| 471 | sizeof(*serial_number)); |
| 472 | } |
| 473 | EXPORT_SYMBOL_GPL(line6_read_serial_number); |
| 474 | |
| 475 | /* |
| 476 | Card destructor. |
| 477 | */ |
| 478 | static void line6_destruct(struct snd_card *card) |
| 479 | { |
| 480 | struct usb_line6 *line6 = card->private_data; |
| 481 | struct usb_device *usbdev = line6->usbdev; |
| 482 | |
| 483 | /* Free buffer memory first. We cannot depend on the existence of private |
| 484 | * data from the (podhd) module, it may be gone already during this call |
| 485 | */ |
| 486 | kfree(objp: line6->buffer_message); |
| 487 | |
| 488 | kfree(objp: line6->buffer_listen); |
| 489 | |
| 490 | /* then free URBs: */ |
| 491 | usb_free_urb(urb: line6->urb_listen); |
| 492 | line6->urb_listen = NULL; |
| 493 | |
| 494 | /* decrement reference counters: */ |
| 495 | usb_put_dev(dev: usbdev); |
| 496 | } |
| 497 | |
| 498 | static void line6_get_usb_properties(struct usb_line6 *line6) |
| 499 | { |
| 500 | struct usb_device *usbdev = line6->usbdev; |
| 501 | const struct line6_properties *properties = line6->properties; |
| 502 | int pipe; |
| 503 | struct usb_host_endpoint *ep = NULL; |
| 504 | |
| 505 | if (properties->capabilities & LINE6_CAP_CONTROL) { |
| 506 | if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 507 | pipe = usb_rcvintpipe(line6->usbdev, |
| 508 | line6->properties->ep_ctrl_r); |
| 509 | } else { |
| 510 | pipe = usb_rcvbulkpipe(line6->usbdev, |
| 511 | line6->properties->ep_ctrl_r); |
| 512 | } |
| 513 | ep = usbdev->ep_in[usb_pipeendpoint(pipe)]; |
| 514 | } |
| 515 | |
| 516 | /* Control data transfer properties */ |
| 517 | if (ep) { |
| 518 | line6->interval = ep->desc.bInterval; |
| 519 | line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize); |
| 520 | } else { |
| 521 | if (properties->capabilities & LINE6_CAP_CONTROL) { |
| 522 | dev_err(line6->ifcdev, |
| 523 | "endpoint not available, using fallback values" ); |
| 524 | } |
| 525 | line6->interval = LINE6_FALLBACK_INTERVAL; |
| 526 | line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE; |
| 527 | } |
| 528 | |
| 529 | /* Isochronous transfer properties */ |
| 530 | if (usbdev->speed == USB_SPEED_LOW) { |
| 531 | line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND; |
| 532 | line6->iso_buffers = USB_LOW_ISO_BUFFERS; |
| 533 | } else { |
| 534 | line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND; |
| 535 | line6->iso_buffers = USB_HIGH_ISO_BUFFERS; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /* Enable buffering of incoming messages, flush the buffer */ |
| 540 | static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file) |
| 541 | { |
| 542 | struct usb_line6 *line6 = hw->private_data; |
| 543 | |
| 544 | /* NOTE: hwdep layer provides atomicity here */ |
| 545 | |
| 546 | line6->messages.active = 1; |
| 547 | line6->messages.nonblock = file->f_flags & O_NONBLOCK ? 1 : 0; |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | /* Stop buffering */ |
| 553 | static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file) |
| 554 | { |
| 555 | struct usb_line6 *line6 = hw->private_data; |
| 556 | |
| 557 | line6->messages.active = 0; |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /* Read from circular buffer, return to user */ |
| 563 | static long |
| 564 | line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count, |
| 565 | loff_t *offset) |
| 566 | { |
| 567 | struct usb_line6 *line6 = hwdep->private_data; |
| 568 | long rv = 0; |
| 569 | unsigned int out_count; |
| 570 | |
| 571 | if (mutex_lock_interruptible(&line6->messages.read_lock)) |
| 572 | return -ERESTARTSYS; |
| 573 | |
| 574 | while (kfifo_len(&line6->messages.fifo) == 0) { |
| 575 | mutex_unlock(lock: &line6->messages.read_lock); |
| 576 | |
| 577 | if (line6->messages.nonblock) |
| 578 | return -EAGAIN; |
| 579 | |
| 580 | rv = wait_event_interruptible( |
| 581 | line6->messages.wait_queue, |
| 582 | kfifo_len(&line6->messages.fifo) != 0); |
| 583 | if (rv < 0) |
| 584 | return rv; |
| 585 | |
| 586 | if (mutex_lock_interruptible(&line6->messages.read_lock)) |
| 587 | return -ERESTARTSYS; |
| 588 | } |
| 589 | |
| 590 | if (kfifo_peek_len(&line6->messages.fifo) > count) { |
| 591 | /* Buffer too small; allow re-read of the current item... */ |
| 592 | rv = -EINVAL; |
| 593 | } else { |
| 594 | rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count); |
| 595 | if (rv == 0) |
| 596 | rv = out_count; |
| 597 | } |
| 598 | |
| 599 | mutex_unlock(lock: &line6->messages.read_lock); |
| 600 | return rv; |
| 601 | } |
| 602 | |
| 603 | /* Write directly (no buffering) to device by user*/ |
| 604 | static long |
| 605 | line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count, |
| 606 | loff_t *offset) |
| 607 | { |
| 608 | struct usb_line6 *line6 = hwdep->private_data; |
| 609 | int rv; |
| 610 | char *data_copy; |
| 611 | |
| 612 | if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) { |
| 613 | /* This is an arbitrary limit - still better than nothing... */ |
| 614 | return -EINVAL; |
| 615 | } |
| 616 | |
| 617 | data_copy = memdup_user(data, count); |
| 618 | if (IS_ERR(ptr: data_copy)) |
| 619 | return PTR_ERR(ptr: data_copy); |
| 620 | |
| 621 | rv = line6_send_raw_message(line6, data_copy, count); |
| 622 | |
| 623 | kfree(objp: data_copy); |
| 624 | return rv; |
| 625 | } |
| 626 | |
| 627 | static __poll_t |
| 628 | line6_hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait) |
| 629 | { |
| 630 | struct usb_line6 *line6 = hwdep->private_data; |
| 631 | |
| 632 | poll_wait(filp: file, wait_address: &line6->messages.wait_queue, p: wait); |
| 633 | |
| 634 | guard(mutex)(T: &line6->messages.read_lock); |
| 635 | return kfifo_len(&line6->messages.fifo) == 0 ? 0 : EPOLLIN | EPOLLRDNORM; |
| 636 | } |
| 637 | |
| 638 | static const struct snd_hwdep_ops hwdep_ops = { |
| 639 | .open = line6_hwdep_open, |
| 640 | .release = line6_hwdep_release, |
| 641 | .read = line6_hwdep_read, |
| 642 | .write = line6_hwdep_write, |
| 643 | .poll = line6_hwdep_poll, |
| 644 | }; |
| 645 | |
| 646 | /* Insert into circular buffer */ |
| 647 | static void line6_hwdep_push_message(struct usb_line6 *line6) |
| 648 | { |
| 649 | if (!line6->messages.active) |
| 650 | return; |
| 651 | |
| 652 | if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) { |
| 653 | /* No race condition here, there's only one writer */ |
| 654 | kfifo_in(&line6->messages.fifo, |
| 655 | line6->buffer_message, line6->message_length); |
| 656 | } /* else TODO: signal overflow */ |
| 657 | |
| 658 | wake_up_interruptible(&line6->messages.wait_queue); |
| 659 | } |
| 660 | |
| 661 | static int line6_hwdep_init(struct usb_line6 *line6) |
| 662 | { |
| 663 | int err; |
| 664 | struct snd_hwdep *hwdep; |
| 665 | |
| 666 | /* TODO: usb_driver_claim_interface(); */ |
| 667 | line6->process_message = line6_hwdep_push_message; |
| 668 | line6->messages.active = 0; |
| 669 | init_waitqueue_head(&line6->messages.wait_queue); |
| 670 | mutex_init(&line6->messages.read_lock); |
| 671 | INIT_KFIFO(line6->messages.fifo); |
| 672 | |
| 673 | err = snd_hwdep_new(card: line6->card, id: "config" , device: 0, rhwdep: &hwdep); |
| 674 | if (err < 0) |
| 675 | goto end; |
| 676 | strscpy(hwdep->name, "config" ); |
| 677 | hwdep->iface = SNDRV_HWDEP_IFACE_LINE6; |
| 678 | hwdep->ops = hwdep_ops; |
| 679 | hwdep->private_data = line6; |
| 680 | hwdep->exclusive = true; |
| 681 | |
| 682 | end: |
| 683 | return err; |
| 684 | } |
| 685 | |
| 686 | static int line6_init_cap_control(struct usb_line6 *line6) |
| 687 | { |
| 688 | int ret; |
| 689 | |
| 690 | /* initialize USB buffers: */ |
| 691 | line6->buffer_listen = kzalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL); |
| 692 | if (!line6->buffer_listen) |
| 693 | return -ENOMEM; |
| 694 | |
| 695 | line6->urb_listen = usb_alloc_urb(iso_packets: 0, GFP_KERNEL); |
| 696 | if (!line6->urb_listen) |
| 697 | return -ENOMEM; |
| 698 | |
| 699 | if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 700 | line6->buffer_message = kzalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL); |
| 701 | if (!line6->buffer_message) |
| 702 | return -ENOMEM; |
| 703 | |
| 704 | ret = line6_init_midi(line6); |
| 705 | if (ret < 0) |
| 706 | return ret; |
| 707 | } else { |
| 708 | ret = line6_hwdep_init(line6); |
| 709 | if (ret < 0) |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | ret = line6_start_listen(line6); |
| 714 | if (ret < 0) { |
| 715 | dev_err(line6->ifcdev, "cannot start listening: %d\n" , ret); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static void line6_startup_work(struct work_struct *work) |
| 723 | { |
| 724 | struct usb_line6 *line6 = |
| 725 | container_of(work, struct usb_line6, startup_work.work); |
| 726 | |
| 727 | if (line6->startup) |
| 728 | line6->startup(line6); |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | Probe USB device. |
| 733 | */ |
| 734 | int line6_probe(struct usb_interface *interface, |
| 735 | const struct usb_device_id *id, |
| 736 | const char *driver_name, |
| 737 | const struct line6_properties *properties, |
| 738 | int (*private_init)(struct usb_line6 *, const struct usb_device_id *id), |
| 739 | size_t data_size) |
| 740 | { |
| 741 | struct usb_device *usbdev = interface_to_usbdev(interface); |
| 742 | struct snd_card *card; |
| 743 | struct usb_line6 *line6; |
| 744 | int interface_number; |
| 745 | int ret; |
| 746 | |
| 747 | if (WARN_ON(data_size < sizeof(*line6))) |
| 748 | return -EINVAL; |
| 749 | |
| 750 | /* we don't handle multiple configurations */ |
| 751 | if (usbdev->descriptor.bNumConfigurations != 1) |
| 752 | return -ENODEV; |
| 753 | |
| 754 | ret = snd_card_new(parent: &interface->dev, |
| 755 | SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, |
| 756 | THIS_MODULE, extra_size: data_size, card_ret: &card); |
| 757 | if (ret < 0) |
| 758 | return ret; |
| 759 | |
| 760 | /* store basic data: */ |
| 761 | line6 = card->private_data; |
| 762 | line6->card = card; |
| 763 | line6->properties = properties; |
| 764 | line6->usbdev = usbdev; |
| 765 | line6->ifcdev = &interface->dev; |
| 766 | INIT_DELAYED_WORK(&line6->startup_work, line6_startup_work); |
| 767 | |
| 768 | strscpy(card->id, properties->id); |
| 769 | strscpy(card->driver, driver_name); |
| 770 | strscpy(card->shortname, properties->name); |
| 771 | sprintf(buf: card->longname, fmt: "Line 6 %s at USB %s" , properties->name, |
| 772 | dev_name(dev: line6->ifcdev)); |
| 773 | card->private_free = line6_destruct; |
| 774 | |
| 775 | usb_set_intfdata(intf: interface, data: line6); |
| 776 | |
| 777 | /* increment reference counters: */ |
| 778 | usb_get_dev(dev: usbdev); |
| 779 | |
| 780 | /* initialize device info: */ |
| 781 | dev_info(&interface->dev, "Line 6 %s found\n" , properties->name); |
| 782 | |
| 783 | /* query interface number */ |
| 784 | interface_number = interface->cur_altsetting->desc.bInterfaceNumber; |
| 785 | |
| 786 | /* TODO reserves the bus bandwidth even without actual transfer */ |
| 787 | ret = usb_set_interface(dev: usbdev, ifnum: interface_number, |
| 788 | alternate: properties->altsetting); |
| 789 | if (ret < 0) { |
| 790 | dev_err(&interface->dev, "set_interface failed\n" ); |
| 791 | goto error; |
| 792 | } |
| 793 | |
| 794 | line6_get_usb_properties(line6); |
| 795 | |
| 796 | if (properties->capabilities & LINE6_CAP_CONTROL) { |
| 797 | ret = line6_init_cap_control(line6); |
| 798 | if (ret < 0) |
| 799 | goto error; |
| 800 | } |
| 801 | |
| 802 | /* initialize device data based on device: */ |
| 803 | ret = private_init(line6, id); |
| 804 | if (ret < 0) |
| 805 | goto error; |
| 806 | |
| 807 | /* creation of additional special files should go here */ |
| 808 | |
| 809 | dev_info(&interface->dev, "Line 6 %s now attached\n" , |
| 810 | properties->name); |
| 811 | |
| 812 | return 0; |
| 813 | |
| 814 | error: |
| 815 | /* we can call disconnect callback here because no close-sync is |
| 816 | * needed yet at this point |
| 817 | */ |
| 818 | line6_disconnect(interface); |
| 819 | return ret; |
| 820 | } |
| 821 | EXPORT_SYMBOL_GPL(line6_probe); |
| 822 | |
| 823 | /* |
| 824 | Line 6 device disconnected. |
| 825 | */ |
| 826 | void line6_disconnect(struct usb_interface *interface) |
| 827 | { |
| 828 | struct usb_line6 *line6 = usb_get_intfdata(intf: interface); |
| 829 | struct usb_device *usbdev = interface_to_usbdev(interface); |
| 830 | |
| 831 | if (!line6) |
| 832 | return; |
| 833 | |
| 834 | if (WARN_ON(usbdev != line6->usbdev)) |
| 835 | return; |
| 836 | |
| 837 | cancel_delayed_work_sync(dwork: &line6->startup_work); |
| 838 | |
| 839 | if (line6->urb_listen != NULL) |
| 840 | line6_stop_listen(line6); |
| 841 | |
| 842 | snd_card_disconnect(card: line6->card); |
| 843 | if (line6->line6pcm) |
| 844 | line6_pcm_disconnect(line6pcm: line6->line6pcm); |
| 845 | if (line6->disconnect) |
| 846 | line6->disconnect(line6); |
| 847 | |
| 848 | dev_info(&interface->dev, "Line 6 %s now disconnected\n" , |
| 849 | line6->properties->name); |
| 850 | |
| 851 | /* make sure the device isn't destructed twice: */ |
| 852 | usb_set_intfdata(intf: interface, NULL); |
| 853 | |
| 854 | snd_card_free_when_closed(card: line6->card); |
| 855 | } |
| 856 | EXPORT_SYMBOL_GPL(line6_disconnect); |
| 857 | |
| 858 | #ifdef CONFIG_PM |
| 859 | |
| 860 | /* |
| 861 | Suspend Line 6 device. |
| 862 | */ |
| 863 | int line6_suspend(struct usb_interface *interface, pm_message_t message) |
| 864 | { |
| 865 | struct usb_line6 *line6 = usb_get_intfdata(intf: interface); |
| 866 | struct snd_line6_pcm *line6pcm = line6->line6pcm; |
| 867 | |
| 868 | snd_power_change_state(card: line6->card, SNDRV_CTL_POWER_D3hot); |
| 869 | |
| 870 | if (line6->properties->capabilities & LINE6_CAP_CONTROL) |
| 871 | line6_stop_listen(line6); |
| 872 | |
| 873 | if (line6pcm != NULL) |
| 874 | line6pcm->flags = 0; |
| 875 | |
| 876 | return 0; |
| 877 | } |
| 878 | EXPORT_SYMBOL_GPL(line6_suspend); |
| 879 | |
| 880 | /* |
| 881 | Resume Line 6 device. |
| 882 | */ |
| 883 | int line6_resume(struct usb_interface *interface) |
| 884 | { |
| 885 | struct usb_line6 *line6 = usb_get_intfdata(intf: interface); |
| 886 | |
| 887 | if (line6->properties->capabilities & LINE6_CAP_CONTROL) |
| 888 | line6_start_listen(line6); |
| 889 | |
| 890 | snd_power_change_state(card: line6->card, SNDRV_CTL_POWER_D0); |
| 891 | return 0; |
| 892 | } |
| 893 | EXPORT_SYMBOL_GPL(line6_resume); |
| 894 | |
| 895 | #endif /* CONFIG_PM */ |
| 896 | |
| 897 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 898 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 899 | MODULE_LICENSE("GPL" ); |
| 900 | |
| 901 | |