1 /* 2 * Line 6 Pod HD 3 * 4 * Copyright (C) 2011 Stefan Hajnoczi <stefanha@gmail.com> 5 * Copyright (C) 2015 Andrej Krutak <dev@andree.sk> 6 * Copyright (C) 2017 Hans P. Moller <hmoller@uc.cl> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation, version 2. 11 * 12 */ 13 14 #include <linux/usb.h> 15 #include <linux/slab.h> 16 #include <linux/module.h> 17 #include <sound/core.h> 18 #include <sound/pcm.h> 19 20 #include "driver.h" 21 #include "pcm.h" 22 23 #define PODHD_STARTUP_DELAY 500 24 25 /* 26 * Stages of POD startup procedure 27 */ 28 enum { 29 PODHD_STARTUP_INIT = 1, 30 PODHD_STARTUP_SCHEDULE_WORKQUEUE, 31 PODHD_STARTUP_SETUP, 32 PODHD_STARTUP_LAST = PODHD_STARTUP_SETUP - 1 33 }; 34 35 enum { 36 LINE6_PODHD300, 37 LINE6_PODHD400, 38 LINE6_PODHD500_0, 39 LINE6_PODHD500_1, 40 LINE6_PODX3, 41 LINE6_PODX3LIVE, 42 LINE6_PODHD500X, 43 LINE6_PODHDDESKTOP 44 }; 45 46 struct usb_line6_podhd { 47 /* Generic Line 6 USB data */ 48 struct usb_line6 line6; 49 50 /* Timer for device initialization */ 51 struct timer_list startup_timer; 52 53 /* Work handler for device initialization */ 54 struct work_struct startup_work; 55 56 /* Current progress in startup procedure */ 57 int startup_progress; 58 59 /* Serial number of device */ 60 u32 serial_number; 61 62 /* Firmware version */ 63 int firmware_version; 64 }; 65 66 static struct snd_ratden podhd_ratden = { 67 .num_min = 48000, 68 .num_max = 48000, 69 .num_step = 1, 70 .den = 1, 71 }; 72 73 static struct line6_pcm_properties podhd_pcm_properties = { 74 .playback_hw = { 75 .info = (SNDRV_PCM_INFO_MMAP | 76 SNDRV_PCM_INFO_INTERLEAVED | 77 SNDRV_PCM_INFO_BLOCK_TRANSFER | 78 SNDRV_PCM_INFO_MMAP_VALID | 79 SNDRV_PCM_INFO_PAUSE | 80 SNDRV_PCM_INFO_SYNC_START), 81 .formats = SNDRV_PCM_FMTBIT_S24_3LE, 82 .rates = SNDRV_PCM_RATE_48000, 83 .rate_min = 48000, 84 .rate_max = 48000, 85 .channels_min = 2, 86 .channels_max = 2, 87 .buffer_bytes_max = 60000, 88 .period_bytes_min = 64, 89 .period_bytes_max = 8192, 90 .periods_min = 1, 91 .periods_max = 1024}, 92 .capture_hw = { 93 .info = (SNDRV_PCM_INFO_MMAP | 94 SNDRV_PCM_INFO_INTERLEAVED | 95 SNDRV_PCM_INFO_BLOCK_TRANSFER | 96 SNDRV_PCM_INFO_MMAP_VALID | 97 SNDRV_PCM_INFO_SYNC_START), 98 .formats = SNDRV_PCM_FMTBIT_S24_3LE, 99 .rates = SNDRV_PCM_RATE_48000, 100 .rate_min = 48000, 101 .rate_max = 48000, 102 .channels_min = 2, 103 .channels_max = 2, 104 .buffer_bytes_max = 60000, 105 .period_bytes_min = 64, 106 .period_bytes_max = 8192, 107 .periods_min = 1, 108 .periods_max = 1024}, 109 .rates = { 110 .nrats = 1, 111 .rats = &podhd_ratden}, 112 .bytes_per_channel = 3 /* SNDRV_PCM_FMTBIT_S24_3LE */ 113 }; 114 115 static struct line6_pcm_properties podx3_pcm_properties = { 116 .playback_hw = { 117 .info = (SNDRV_PCM_INFO_MMAP | 118 SNDRV_PCM_INFO_INTERLEAVED | 119 SNDRV_PCM_INFO_BLOCK_TRANSFER | 120 SNDRV_PCM_INFO_MMAP_VALID | 121 SNDRV_PCM_INFO_PAUSE | 122 SNDRV_PCM_INFO_SYNC_START), 123 .formats = SNDRV_PCM_FMTBIT_S24_3LE, 124 .rates = SNDRV_PCM_RATE_48000, 125 .rate_min = 48000, 126 .rate_max = 48000, 127 .channels_min = 2, 128 .channels_max = 2, 129 .buffer_bytes_max = 60000, 130 .period_bytes_min = 64, 131 .period_bytes_max = 8192, 132 .periods_min = 1, 133 .periods_max = 1024}, 134 .capture_hw = { 135 .info = (SNDRV_PCM_INFO_MMAP | 136 SNDRV_PCM_INFO_INTERLEAVED | 137 SNDRV_PCM_INFO_BLOCK_TRANSFER | 138 SNDRV_PCM_INFO_MMAP_VALID | 139 SNDRV_PCM_INFO_SYNC_START), 140 .formats = SNDRV_PCM_FMTBIT_S24_3LE, 141 .rates = SNDRV_PCM_RATE_48000, 142 .rate_min = 48000, 143 .rate_max = 48000, 144 /* 1+2: Main signal (out), 3+4: Tone 1, 145 * 5+6: Tone 2, 7+8: raw 146 */ 147 .channels_min = 8, 148 .channels_max = 8, 149 .buffer_bytes_max = 60000, 150 .period_bytes_min = 64, 151 .period_bytes_max = 8192, 152 .periods_min = 1, 153 .periods_max = 1024}, 154 .rates = { 155 .nrats = 1, 156 .rats = &podhd_ratden}, 157 .bytes_per_channel = 3 /* SNDRV_PCM_FMTBIT_S24_3LE */ 158 }; 159 static struct usb_driver podhd_driver; 160 161 static void podhd_startup_start_workqueue(unsigned long data); 162 static void podhd_startup_workqueue(struct work_struct *work); 163 static int podhd_startup_finalize(struct usb_line6_podhd *pod); 164 165 static ssize_t serial_number_show(struct device *dev, 166 struct device_attribute *attr, char *buf) 167 { 168 struct snd_card *card = dev_to_snd_card(dev); 169 struct usb_line6_podhd *pod = card->private_data; 170 171 return sprintf(buf, "%u\n", pod->serial_number); 172 } 173 174 static ssize_t firmware_version_show(struct device *dev, 175 struct device_attribute *attr, char *buf) 176 { 177 struct snd_card *card = dev_to_snd_card(dev); 178 struct usb_line6_podhd *pod = card->private_data; 179 180 return sprintf(buf, "%06x\n", pod->firmware_version); 181 } 182 183 static DEVICE_ATTR_RO(firmware_version); 184 static DEVICE_ATTR_RO(serial_number); 185 186 static struct attribute *podhd_dev_attrs[] = { 187 &dev_attr_firmware_version.attr, 188 &dev_attr_serial_number.attr, 189 NULL 190 }; 191 192 static const struct attribute_group podhd_dev_attr_group = { 193 .name = "podhd", 194 .attrs = podhd_dev_attrs, 195 }; 196 197 /* 198 * POD X3 startup procedure. 199 * 200 * May be compatible with other POD HD's, since it's also similar to the 201 * previous POD setup. In any case, it doesn't seem to be required for the 202 * audio nor bulk interfaces to work. 203 */ 204 205 static void podhd_startup(struct usb_line6_podhd *pod) 206 { 207 CHECK_STARTUP_PROGRESS(pod->startup_progress, PODHD_STARTUP_INIT); 208 209 /* delay startup procedure: */ 210 line6_start_timer(&pod->startup_timer, PODHD_STARTUP_DELAY, 211 podhd_startup_start_workqueue, (unsigned long)pod); 212 } 213 214 static void podhd_startup_start_workqueue(unsigned long data) 215 { 216 struct usb_line6_podhd *pod = (struct usb_line6_podhd *)data; 217 218 CHECK_STARTUP_PROGRESS(pod->startup_progress, 219 PODHD_STARTUP_SCHEDULE_WORKQUEUE); 220 221 /* schedule work for global work queue: */ 222 schedule_work(&pod->startup_work); 223 } 224 225 static int podhd_dev_start(struct usb_line6_podhd *pod) 226 { 227 int ret; 228 u8 init_bytes[8]; 229 int i; 230 struct usb_device *usbdev = pod->line6.usbdev; 231 232 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 233 0x67, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 234 0x11, 0, 235 NULL, 0, LINE6_TIMEOUT * HZ); 236 if (ret < 0) { 237 dev_err(pod->line6.ifcdev, "read request failed (error %d)\n", ret); 238 return ret; 239 } 240 241 /* NOTE: looks like some kind of ping message */ 242 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67, 243 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 244 0x11, 0x0, 245 &init_bytes, 3, LINE6_TIMEOUT * HZ); 246 if (ret < 0) { 247 dev_err(pod->line6.ifcdev, 248 "receive length failed (error %d)\n", ret); 249 return ret; 250 } 251 252 pod->firmware_version = 253 (init_bytes[0] << 16) | (init_bytes[1] << 8) | (init_bytes[2] << 0); 254 255 for (i = 0; i <= 16; i++) { 256 ret = line6_read_data(&pod->line6, 0xf000 + 0x08 * i, init_bytes, 8); 257 if (ret < 0) 258 return ret; 259 } 260 261 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 262 USB_REQ_SET_FEATURE, 263 USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_DIR_OUT, 264 1, 0, 265 NULL, 0, LINE6_TIMEOUT * HZ); 266 if (ret < 0) 267 return ret; 268 269 return 0; 270 } 271 272 static void podhd_startup_workqueue(struct work_struct *work) 273 { 274 struct usb_line6_podhd *pod = 275 container_of(work, struct usb_line6_podhd, startup_work); 276 277 CHECK_STARTUP_PROGRESS(pod->startup_progress, PODHD_STARTUP_SETUP); 278 279 podhd_dev_start(pod); 280 line6_read_serial_number(&pod->line6, &pod->serial_number); 281 282 podhd_startup_finalize(pod); 283 } 284 285 static int podhd_startup_finalize(struct usb_line6_podhd *pod) 286 { 287 struct usb_line6 *line6 = &pod->line6; 288 289 /* ALSA audio interface: */ 290 return snd_card_register(line6->card); 291 } 292 293 static void podhd_disconnect(struct usb_line6 *line6) 294 { 295 struct usb_line6_podhd *pod = (struct usb_line6_podhd *)line6; 296 297 if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL_INFO) { 298 struct usb_interface *intf; 299 300 del_timer_sync(&pod->startup_timer); 301 cancel_work_sync(&pod->startup_work); 302 303 intf = usb_ifnum_to_if(line6->usbdev, 304 pod->line6.properties->ctrl_if); 305 usb_driver_release_interface(&podhd_driver, intf); 306 } 307 } 308 309 /* 310 Try to init POD HD device. 311 */ 312 static int podhd_init(struct usb_line6 *line6, 313 const struct usb_device_id *id) 314 { 315 int err; 316 struct usb_line6_podhd *pod = (struct usb_line6_podhd *) line6; 317 struct usb_interface *intf; 318 319 line6->disconnect = podhd_disconnect; 320 321 if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL) { 322 /* claim the data interface */ 323 intf = usb_ifnum_to_if(line6->usbdev, 324 pod->line6.properties->ctrl_if); 325 if (!intf) { 326 dev_err(pod->line6.ifcdev, "interface %d not found\n", 327 pod->line6.properties->ctrl_if); 328 return -ENODEV; 329 } 330 331 err = usb_driver_claim_interface(&podhd_driver, intf, NULL); 332 if (err != 0) { 333 dev_err(pod->line6.ifcdev, "can't claim interface %d, error %d\n", 334 pod->line6.properties->ctrl_if, err); 335 return err; 336 } 337 } 338 339 if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL_INFO) { 340 /* create sysfs entries: */ 341 err = snd_card_add_dev_attr(line6->card, &podhd_dev_attr_group); 342 if (err < 0) 343 return err; 344 } 345 346 if (pod->line6.properties->capabilities & LINE6_CAP_PCM) { 347 /* initialize PCM subsystem: */ 348 err = line6_init_pcm(line6, 349 (id->driver_info == LINE6_PODX3 || 350 id->driver_info == LINE6_PODX3LIVE) ? &podx3_pcm_properties : 351 &podhd_pcm_properties); 352 if (err < 0) 353 return err; 354 } 355 356 if (!(pod->line6.properties->capabilities & LINE6_CAP_CONTROL_INFO)) { 357 /* register USB audio system directly */ 358 return podhd_startup_finalize(pod); 359 } 360 361 /* init device and delay registering */ 362 init_timer(&pod->startup_timer); 363 INIT_WORK(&pod->startup_work, podhd_startup_workqueue); 364 podhd_startup(pod); 365 return 0; 366 } 367 368 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod) 369 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n) 370 371 /* table of devices that work with this driver */ 372 static const struct usb_device_id podhd_id_table[] = { 373 /* TODO: no need to alloc data interfaces when only audio is used */ 374 { LINE6_DEVICE(0x5057), .driver_info = LINE6_PODHD300 }, 375 { LINE6_DEVICE(0x5058), .driver_info = LINE6_PODHD400 }, 376 { LINE6_IF_NUM(0x414D, 0), .driver_info = LINE6_PODHD500_0 }, 377 { LINE6_IF_NUM(0x414D, 1), .driver_info = LINE6_PODHD500_1 }, 378 { LINE6_IF_NUM(0x414A, 0), .driver_info = LINE6_PODX3 }, 379 { LINE6_IF_NUM(0x414B, 0), .driver_info = LINE6_PODX3LIVE }, 380 { LINE6_IF_NUM(0x4159, 0), .driver_info = LINE6_PODHD500X }, 381 { LINE6_IF_NUM(0x4156, 0), .driver_info = LINE6_PODHDDESKTOP }, 382 {} 383 }; 384 385 MODULE_DEVICE_TABLE(usb, podhd_id_table); 386 387 static const struct line6_properties podhd_properties_table[] = { 388 [LINE6_PODHD300] = { 389 .id = "PODHD300", 390 .name = "POD HD300", 391 .capabilities = LINE6_CAP_PCM 392 | LINE6_CAP_HWMON, 393 .altsetting = 5, 394 .ep_ctrl_r = 0x84, 395 .ep_ctrl_w = 0x03, 396 .ep_audio_r = 0x82, 397 .ep_audio_w = 0x01, 398 }, 399 [LINE6_PODHD400] = { 400 .id = "PODHD400", 401 .name = "POD HD400", 402 .capabilities = LINE6_CAP_PCM 403 | LINE6_CAP_HWMON, 404 .altsetting = 5, 405 .ep_ctrl_r = 0x84, 406 .ep_ctrl_w = 0x03, 407 .ep_audio_r = 0x82, 408 .ep_audio_w = 0x01, 409 }, 410 [LINE6_PODHD500_0] = { 411 .id = "PODHD500", 412 .name = "POD HD500", 413 .capabilities = LINE6_CAP_PCM 414 | LINE6_CAP_HWMON, 415 .altsetting = 1, 416 .ep_ctrl_r = 0x81, 417 .ep_ctrl_w = 0x01, 418 .ep_audio_r = 0x86, 419 .ep_audio_w = 0x02, 420 }, 421 [LINE6_PODHD500_1] = { 422 .id = "PODHD500", 423 .name = "POD HD500", 424 .capabilities = LINE6_CAP_PCM 425 | LINE6_CAP_HWMON, 426 .altsetting = 1, 427 .ep_ctrl_r = 0x81, 428 .ep_ctrl_w = 0x01, 429 .ep_audio_r = 0x86, 430 .ep_audio_w = 0x02, 431 }, 432 [LINE6_PODX3] = { 433 .id = "PODX3", 434 .name = "POD X3", 435 .capabilities = LINE6_CAP_CONTROL | LINE6_CAP_CONTROL_INFO 436 | LINE6_CAP_PCM | LINE6_CAP_HWMON | LINE6_CAP_IN_NEEDS_OUT, 437 .altsetting = 1, 438 .ep_ctrl_r = 0x81, 439 .ep_ctrl_w = 0x01, 440 .ctrl_if = 1, 441 .ep_audio_r = 0x86, 442 .ep_audio_w = 0x02, 443 }, 444 [LINE6_PODX3LIVE] = { 445 .id = "PODX3LIVE", 446 .name = "POD X3 LIVE", 447 .capabilities = LINE6_CAP_CONTROL | LINE6_CAP_CONTROL_INFO 448 | LINE6_CAP_PCM | LINE6_CAP_HWMON | LINE6_CAP_IN_NEEDS_OUT, 449 .altsetting = 1, 450 .ep_ctrl_r = 0x81, 451 .ep_ctrl_w = 0x01, 452 .ctrl_if = 1, 453 .ep_audio_r = 0x86, 454 .ep_audio_w = 0x02, 455 }, 456 [LINE6_PODHD500X] = { 457 .id = "PODHD500X", 458 .name = "POD HD500X", 459 .capabilities = LINE6_CAP_CONTROL 460 | LINE6_CAP_PCM | LINE6_CAP_HWMON, 461 .altsetting = 1, 462 .ep_ctrl_r = 0x81, 463 .ep_ctrl_w = 0x01, 464 .ctrl_if = 1, 465 .ep_audio_r = 0x86, 466 .ep_audio_w = 0x02, 467 }, 468 [LINE6_PODHDDESKTOP] = { 469 .id = "PODHDDESKTOP", 470 .name = "POD HDDESKTOP", 471 .capabilities = LINE6_CAP_CONTROL 472 | LINE6_CAP_PCM | LINE6_CAP_HWMON, 473 .altsetting = 1, 474 .ep_ctrl_r = 0x81, 475 .ep_ctrl_w = 0x01, 476 .ctrl_if = 1, 477 .ep_audio_r = 0x86, 478 .ep_audio_w = 0x02, 479 }, 480 }; 481 482 /* 483 Probe USB device. 484 */ 485 static int podhd_probe(struct usb_interface *interface, 486 const struct usb_device_id *id) 487 { 488 return line6_probe(interface, id, "Line6-PODHD", 489 &podhd_properties_table[id->driver_info], 490 podhd_init, sizeof(struct usb_line6_podhd)); 491 } 492 493 static struct usb_driver podhd_driver = { 494 .name = KBUILD_MODNAME, 495 .probe = podhd_probe, 496 .disconnect = line6_disconnect, 497 #ifdef CONFIG_PM 498 .suspend = line6_suspend, 499 .resume = line6_resume, 500 .reset_resume = line6_resume, 501 #endif 502 .id_table = podhd_id_table, 503 }; 504 505 module_usb_driver(podhd_driver); 506 507 MODULE_DESCRIPTION("Line 6 PODHD USB driver"); 508 MODULE_LICENSE("GPL"); 509