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