1 /* 2 * Line 6 Linux USB driver 3 * 4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) 5 * Emil Myhrman (emil.myhrman@gmail.com) 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation, version 2. 10 * 11 */ 12 13 #include <linux/wait.h> 14 #include <linux/usb.h> 15 #include <linux/slab.h> 16 #include <linux/module.h> 17 #include <linux/leds.h> 18 #include <sound/core.h> 19 #include <sound/control.h> 20 21 #include "capture.h" 22 #include "driver.h" 23 #include "playback.h" 24 #include "usbdefs.h" 25 26 enum line6_device_type { 27 LINE6_GUITARPORT, 28 LINE6_PODSTUDIO_GX, 29 LINE6_PODSTUDIO_UX1, 30 LINE6_PODSTUDIO_UX2, 31 LINE6_TONEPORT_GX, 32 LINE6_TONEPORT_UX1, 33 LINE6_TONEPORT_UX2, 34 }; 35 36 struct usb_line6_toneport; 37 38 struct toneport_led { 39 struct led_classdev dev; 40 char name[64]; 41 struct usb_line6_toneport *toneport; 42 bool registered; 43 }; 44 45 struct usb_line6_toneport { 46 /** 47 Generic Line 6 USB data. 48 */ 49 struct usb_line6 line6; 50 51 /** 52 Source selector. 53 */ 54 int source; 55 56 /** 57 Serial number of device. 58 */ 59 int serial_number; 60 61 /** 62 Firmware version (x 100). 63 */ 64 int firmware_version; 65 66 /** 67 Timer for delayed PCM startup. 68 */ 69 struct timer_list timer; 70 71 /** 72 Device type. 73 */ 74 enum line6_device_type type; 75 76 /* LED instances */ 77 struct toneport_led leds[2]; 78 }; 79 80 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2); 81 82 #define TONEPORT_PCM_DELAY 1 83 84 static struct snd_ratden toneport_ratden = { 85 .num_min = 44100, 86 .num_max = 44100, 87 .num_step = 1, 88 .den = 1 89 }; 90 91 static struct line6_pcm_properties toneport_pcm_properties = { 92 .snd_line6_playback_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_PAUSE | 98 SNDRV_PCM_INFO_SYNC_START), 99 .formats = SNDRV_PCM_FMTBIT_S16_LE, 100 .rates = SNDRV_PCM_RATE_KNOT, 101 .rate_min = 44100, 102 .rate_max = 44100, 103 .channels_min = 2, 104 .channels_max = 2, 105 .buffer_bytes_max = 60000, 106 .period_bytes_min = 64, 107 .period_bytes_max = 8192, 108 .periods_min = 1, 109 .periods_max = 1024}, 110 .snd_line6_capture_hw = { 111 .info = (SNDRV_PCM_INFO_MMAP | 112 SNDRV_PCM_INFO_INTERLEAVED | 113 SNDRV_PCM_INFO_BLOCK_TRANSFER | 114 SNDRV_PCM_INFO_MMAP_VALID | 115 SNDRV_PCM_INFO_SYNC_START), 116 .formats = SNDRV_PCM_FMTBIT_S16_LE, 117 .rates = SNDRV_PCM_RATE_KNOT, 118 .rate_min = 44100, 119 .rate_max = 44100, 120 .channels_min = 2, 121 .channels_max = 2, 122 .buffer_bytes_max = 60000, 123 .period_bytes_min = 64, 124 .period_bytes_max = 8192, 125 .periods_min = 1, 126 .periods_max = 1024}, 127 .snd_line6_rates = { 128 .nrats = 1, 129 .rats = &toneport_ratden}, 130 .bytes_per_frame = 4 131 }; 132 133 static const struct { 134 const char *name; 135 int code; 136 } toneport_source_info[] = { 137 {"Microphone", 0x0a01}, 138 {"Line", 0x0801}, 139 {"Instrument", 0x0b01}, 140 {"Inst & Mic", 0x0901} 141 }; 142 143 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2) 144 { 145 int ret; 146 147 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67, 148 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 149 cmd1, cmd2, NULL, 0, LINE6_TIMEOUT * HZ); 150 151 if (ret < 0) { 152 dev_err(&usbdev->dev, "send failed (error %d)\n", ret); 153 return ret; 154 } 155 156 return 0; 157 } 158 159 /* monitor info callback */ 160 static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol, 161 struct snd_ctl_elem_info *uinfo) 162 { 163 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 164 uinfo->count = 1; 165 uinfo->value.integer.min = 0; 166 uinfo->value.integer.max = 256; 167 return 0; 168 } 169 170 /* monitor get callback */ 171 static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol, 172 struct snd_ctl_elem_value *ucontrol) 173 { 174 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); 175 176 ucontrol->value.integer.value[0] = line6pcm->volume_monitor; 177 return 0; 178 } 179 180 /* monitor put callback */ 181 static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol, 182 struct snd_ctl_elem_value *ucontrol) 183 { 184 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); 185 int err; 186 187 if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor) 188 return 0; 189 190 line6pcm->volume_monitor = ucontrol->value.integer.value[0]; 191 192 if (line6pcm->volume_monitor > 0) { 193 err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR); 194 if (err < 0) { 195 line6pcm->volume_monitor = 0; 196 line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR); 197 return err; 198 } 199 } else { 200 line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR); 201 } 202 203 return 1; 204 } 205 206 /* source info callback */ 207 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol, 208 struct snd_ctl_elem_info *uinfo) 209 { 210 const int size = ARRAY_SIZE(toneport_source_info); 211 212 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 213 uinfo->count = 1; 214 uinfo->value.enumerated.items = size; 215 216 if (uinfo->value.enumerated.item >= size) 217 uinfo->value.enumerated.item = size - 1; 218 219 strcpy(uinfo->value.enumerated.name, 220 toneport_source_info[uinfo->value.enumerated.item].name); 221 222 return 0; 223 } 224 225 /* source get callback */ 226 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol, 227 struct snd_ctl_elem_value *ucontrol) 228 { 229 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); 230 struct usb_line6_toneport *toneport = 231 (struct usb_line6_toneport *)line6pcm->line6; 232 ucontrol->value.enumerated.item[0] = toneport->source; 233 return 0; 234 } 235 236 /* source put callback */ 237 static int snd_toneport_source_put(struct snd_kcontrol *kcontrol, 238 struct snd_ctl_elem_value *ucontrol) 239 { 240 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); 241 struct usb_line6_toneport *toneport = 242 (struct usb_line6_toneport *)line6pcm->line6; 243 unsigned int source; 244 245 source = ucontrol->value.enumerated.item[0]; 246 if (source >= ARRAY_SIZE(toneport_source_info)) 247 return -EINVAL; 248 if (source == toneport->source) 249 return 0; 250 251 toneport->source = source; 252 toneport_send_cmd(toneport->line6.usbdev, 253 toneport_source_info[source].code, 0x0000); 254 return 1; 255 } 256 257 static void toneport_start_pcm(unsigned long arg) 258 { 259 struct usb_line6_toneport *toneport = (struct usb_line6_toneport *)arg; 260 struct usb_line6 *line6 = &toneport->line6; 261 262 line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR); 263 } 264 265 /* control definition */ 266 static struct snd_kcontrol_new toneport_control_monitor = { 267 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 268 .name = "Monitor Playback Volume", 269 .index = 0, 270 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 271 .info = snd_toneport_monitor_info, 272 .get = snd_toneport_monitor_get, 273 .put = snd_toneport_monitor_put 274 }; 275 276 /* source selector definition */ 277 static struct snd_kcontrol_new toneport_control_source = { 278 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 279 .name = "PCM Capture Source", 280 .index = 0, 281 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 282 .info = snd_toneport_source_info, 283 .get = snd_toneport_source_get, 284 .put = snd_toneport_source_put 285 }; 286 287 /* 288 For the led on Guitarport. 289 Brightness goes from 0x00 to 0x26. Set a value above this to have led 290 blink. 291 (void cmd_0x02(byte red, byte green) 292 */ 293 294 static bool toneport_has_led(enum line6_device_type type) 295 { 296 return 297 (type == LINE6_GUITARPORT) || 298 (type == LINE6_TONEPORT_GX); 299 /* add your device here if you are missing support for the LEDs */ 300 } 301 302 static const char * const led_colors[2] = { "red", "green" }; 303 static const int led_init_vals[2] = { 0x00, 0x26 }; 304 305 static void toneport_update_led(struct usb_line6_toneport *toneport) 306 { 307 toneport_send_cmd(toneport->line6.usbdev, 308 (toneport->leds[0].dev.brightness << 8) | 0x0002, 309 toneport->leds[1].dev.brightness); 310 } 311 312 static void toneport_led_brightness_set(struct led_classdev *led_cdev, 313 enum led_brightness brightness) 314 { 315 struct toneport_led *leds = 316 container_of(led_cdev, struct toneport_led, dev); 317 toneport_update_led(leds->toneport); 318 } 319 320 static int toneport_init_leds(struct usb_line6_toneport *toneport) 321 { 322 struct device *dev = &toneport->line6.usbdev->dev; 323 int i, err; 324 325 for (i = 0; i < 2; i++) { 326 struct toneport_led *led = &toneport->leds[i]; 327 struct led_classdev *leddev = &led->dev; 328 329 led->toneport = toneport; 330 snprintf(led->name, sizeof(led->name), "%s::%s", 331 dev_name(dev), led_colors[i]); 332 leddev->name = led->name; 333 leddev->brightness = led_init_vals[i]; 334 leddev->max_brightness = 0x26; 335 leddev->brightness_set = toneport_led_brightness_set; 336 err = led_classdev_register(dev, leddev); 337 if (err) 338 return err; 339 led->registered = true; 340 } 341 342 return 0; 343 } 344 345 static void toneport_remove_leds(struct usb_line6_toneport *toneport) 346 { 347 struct toneport_led *led; 348 int i; 349 350 for (i = 0; i < 2; i++) { 351 led = &toneport->leds[i]; 352 if (!led->registered) 353 break; 354 led_classdev_unregister(&led->dev); 355 led->registered = false; 356 } 357 } 358 359 /* 360 Setup Toneport device. 361 */ 362 static void toneport_setup(struct usb_line6_toneport *toneport) 363 { 364 int ticks; 365 struct usb_line6 *line6 = &toneport->line6; 366 struct usb_device *usbdev = line6->usbdev; 367 368 /* sync time on device with host: */ 369 ticks = (int)get_seconds(); 370 line6_write_data(line6, 0x80c6, &ticks, 4); 371 372 /* enable device: */ 373 toneport_send_cmd(usbdev, 0x0301, 0x0000); 374 375 /* initialize source select: */ 376 switch (toneport->type) { 377 case LINE6_TONEPORT_UX1: 378 case LINE6_TONEPORT_UX2: 379 case LINE6_PODSTUDIO_UX1: 380 case LINE6_PODSTUDIO_UX2: 381 toneport_send_cmd(usbdev, 382 toneport_source_info[toneport->source].code, 383 0x0000); 384 default: 385 break; 386 } 387 388 if (toneport_has_led(toneport->type)) 389 toneport_update_led(toneport); 390 391 mod_timer(&toneport->timer, jiffies + TONEPORT_PCM_DELAY * HZ); 392 } 393 394 /* 395 Toneport device disconnected. 396 */ 397 static void line6_toneport_disconnect(struct usb_line6 *line6) 398 { 399 struct usb_line6_toneport *toneport = 400 (struct usb_line6_toneport *)line6; 401 402 del_timer_sync(&toneport->timer); 403 404 if (toneport_has_led(toneport->type)) 405 toneport_remove_leds(toneport); 406 } 407 408 409 /* 410 Try to init Toneport device. 411 */ 412 static int toneport_init(struct usb_line6 *line6, 413 const struct usb_device_id *id) 414 { 415 int err; 416 struct usb_line6_toneport *toneport = (struct usb_line6_toneport *) line6; 417 418 toneport->type = id->driver_info; 419 setup_timer(&toneport->timer, toneport_start_pcm, 420 (unsigned long)toneport); 421 422 line6->disconnect = line6_toneport_disconnect; 423 424 /* initialize PCM subsystem: */ 425 err = line6_init_pcm(line6, &toneport_pcm_properties); 426 if (err < 0) 427 return err; 428 429 /* register monitor control: */ 430 err = snd_ctl_add(line6->card, 431 snd_ctl_new1(&toneport_control_monitor, 432 line6->line6pcm)); 433 if (err < 0) 434 return err; 435 436 /* register source select control: */ 437 switch (toneport->type) { 438 case LINE6_TONEPORT_UX1: 439 case LINE6_TONEPORT_UX2: 440 case LINE6_PODSTUDIO_UX1: 441 case LINE6_PODSTUDIO_UX2: 442 err = 443 snd_ctl_add(line6->card, 444 snd_ctl_new1(&toneport_control_source, 445 line6->line6pcm)); 446 if (err < 0) 447 return err; 448 449 default: 450 break; 451 } 452 453 line6_read_serial_number(line6, &toneport->serial_number); 454 line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1); 455 456 if (toneport_has_led(toneport->type)) { 457 err = toneport_init_leds(toneport); 458 if (err < 0) 459 return err; 460 } 461 462 toneport_setup(toneport); 463 464 /* register audio system: */ 465 return snd_card_register(line6->card); 466 } 467 468 #ifdef CONFIG_PM 469 /* 470 Resume Toneport device after reset. 471 */ 472 static int toneport_reset_resume(struct usb_interface *interface) 473 { 474 toneport_setup(usb_get_intfdata(interface)); 475 return line6_resume(interface); 476 } 477 #endif 478 479 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod) 480 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n) 481 482 /* table of devices that work with this driver */ 483 static const struct usb_device_id toneport_id_table[] = { 484 { LINE6_DEVICE(0x4750), .driver_info = LINE6_GUITARPORT }, 485 { LINE6_DEVICE(0x4153), .driver_info = LINE6_PODSTUDIO_GX }, 486 { LINE6_DEVICE(0x4150), .driver_info = LINE6_PODSTUDIO_UX1 }, 487 { LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 }, 488 { LINE6_DEVICE(0x4147), .driver_info = LINE6_TONEPORT_GX }, 489 { LINE6_DEVICE(0x4141), .driver_info = LINE6_TONEPORT_UX1 }, 490 { LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 }, 491 {} 492 }; 493 494 MODULE_DEVICE_TABLE(usb, toneport_id_table); 495 496 static const struct line6_properties toneport_properties_table[] = { 497 [LINE6_GUITARPORT] = { 498 .id = "GuitarPort", 499 .name = "GuitarPort", 500 .capabilities = LINE6_CAP_PCM, 501 .altsetting = 2, /* 1..4 seem to be ok */ 502 /* no control channel */ 503 .ep_audio_r = 0x82, 504 .ep_audio_w = 0x01, 505 }, 506 [LINE6_PODSTUDIO_GX] = { 507 .id = "PODStudioGX", 508 .name = "POD Studio GX", 509 .capabilities = LINE6_CAP_PCM, 510 .altsetting = 2, /* 1..4 seem to be ok */ 511 /* no control channel */ 512 .ep_audio_r = 0x82, 513 .ep_audio_w = 0x01, 514 }, 515 [LINE6_PODSTUDIO_UX1] = { 516 .id = "PODStudioUX1", 517 .name = "POD Studio UX1", 518 .capabilities = LINE6_CAP_PCM, 519 .altsetting = 2, /* 1..4 seem to be ok */ 520 /* no control channel */ 521 .ep_audio_r = 0x82, 522 .ep_audio_w = 0x01, 523 }, 524 [LINE6_PODSTUDIO_UX2] = { 525 .id = "PODStudioUX2", 526 .name = "POD Studio UX2", 527 .capabilities = LINE6_CAP_PCM, 528 .altsetting = 2, /* defaults to 44.1kHz, 16-bit */ 529 /* no control channel */ 530 .ep_audio_r = 0x82, 531 .ep_audio_w = 0x01, 532 }, 533 [LINE6_TONEPORT_GX] = { 534 .id = "TonePortGX", 535 .name = "TonePort GX", 536 .capabilities = LINE6_CAP_PCM, 537 .altsetting = 2, /* 1..4 seem to be ok */ 538 /* no control channel */ 539 .ep_audio_r = 0x82, 540 .ep_audio_w = 0x01, 541 }, 542 [LINE6_TONEPORT_UX1] = { 543 .id = "TonePortUX1", 544 .name = "TonePort UX1", 545 .capabilities = LINE6_CAP_PCM, 546 .altsetting = 2, /* 1..4 seem to be ok */ 547 /* no control channel */ 548 .ep_audio_r = 0x82, 549 .ep_audio_w = 0x01, 550 }, 551 [LINE6_TONEPORT_UX2] = { 552 .id = "TonePortUX2", 553 .name = "TonePort UX2", 554 .capabilities = LINE6_CAP_PCM, 555 .altsetting = 2, /* defaults to 44.1kHz, 16-bit */ 556 /* no control channel */ 557 .ep_audio_r = 0x82, 558 .ep_audio_w = 0x01, 559 }, 560 }; 561 562 /* 563 Probe USB device. 564 */ 565 static int toneport_probe(struct usb_interface *interface, 566 const struct usb_device_id *id) 567 { 568 return line6_probe(interface, id, 569 &toneport_properties_table[id->driver_info], 570 toneport_init, sizeof(struct usb_line6_toneport)); 571 } 572 573 static struct usb_driver toneport_driver = { 574 .name = KBUILD_MODNAME, 575 .probe = toneport_probe, 576 .disconnect = line6_disconnect, 577 #ifdef CONFIG_PM 578 .suspend = line6_suspend, 579 .resume = line6_resume, 580 .reset_resume = toneport_reset_resume, 581 #endif 582 .id_table = toneport_id_table, 583 }; 584 585 module_usb_driver(toneport_driver); 586 587 MODULE_DESCRIPTION("TonePort USB driver"); 588 MODULE_LICENSE("GPL"); 589