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 186 if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor) 187 return 0; 188 189 line6pcm->volume_monitor = ucontrol->value.integer.value[0]; 190 191 if (line6pcm->volume_monitor > 0) 192 line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_MONITOR); 193 else 194 line6_pcm_release(line6pcm, LINE6_BITS_PCM_MONITOR); 195 196 return 1; 197 } 198 199 /* source info callback */ 200 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol, 201 struct snd_ctl_elem_info *uinfo) 202 { 203 const int size = ARRAY_SIZE(toneport_source_info); 204 205 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 206 uinfo->count = 1; 207 uinfo->value.enumerated.items = size; 208 209 if (uinfo->value.enumerated.item >= size) 210 uinfo->value.enumerated.item = size - 1; 211 212 strcpy(uinfo->value.enumerated.name, 213 toneport_source_info[uinfo->value.enumerated.item].name); 214 215 return 0; 216 } 217 218 /* source get callback */ 219 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol, 220 struct snd_ctl_elem_value *ucontrol) 221 { 222 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); 223 struct usb_line6_toneport *toneport = 224 (struct usb_line6_toneport *)line6pcm->line6; 225 ucontrol->value.enumerated.item[0] = toneport->source; 226 return 0; 227 } 228 229 /* source put callback */ 230 static int snd_toneport_source_put(struct snd_kcontrol *kcontrol, 231 struct snd_ctl_elem_value *ucontrol) 232 { 233 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); 234 struct usb_line6_toneport *toneport = 235 (struct usb_line6_toneport *)line6pcm->line6; 236 unsigned int source; 237 238 source = ucontrol->value.enumerated.item[0]; 239 if (source >= ARRAY_SIZE(toneport_source_info)) 240 return -EINVAL; 241 if (source == toneport->source) 242 return 0; 243 244 toneport->source = source; 245 toneport_send_cmd(toneport->line6.usbdev, 246 toneport_source_info[source].code, 0x0000); 247 return 1; 248 } 249 250 static void toneport_start_pcm(unsigned long arg) 251 { 252 struct usb_line6_toneport *toneport = (struct usb_line6_toneport *)arg; 253 struct usb_line6 *line6 = &toneport->line6; 254 255 line6_pcm_acquire(line6->line6pcm, LINE6_BITS_PCM_MONITOR); 256 } 257 258 /* control definition */ 259 static struct snd_kcontrol_new toneport_control_monitor = { 260 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 261 .name = "Monitor Playback Volume", 262 .index = 0, 263 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 264 .info = snd_toneport_monitor_info, 265 .get = snd_toneport_monitor_get, 266 .put = snd_toneport_monitor_put 267 }; 268 269 /* source selector definition */ 270 static struct snd_kcontrol_new toneport_control_source = { 271 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 272 .name = "PCM Capture Source", 273 .index = 0, 274 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 275 .info = snd_toneport_source_info, 276 .get = snd_toneport_source_get, 277 .put = snd_toneport_source_put 278 }; 279 280 /* 281 For the led on Guitarport. 282 Brightness goes from 0x00 to 0x26. Set a value above this to have led 283 blink. 284 (void cmd_0x02(byte red, byte green) 285 */ 286 287 static bool toneport_has_led(enum line6_device_type type) 288 { 289 return 290 (type == LINE6_GUITARPORT) || 291 (type == LINE6_TONEPORT_GX); 292 /* add your device here if you are missing support for the LEDs */ 293 } 294 295 static const char * const led_colors[2] = { "red", "green" }; 296 static const int led_init_vals[2] = { 0x00, 0x26 }; 297 298 static void toneport_update_led(struct usb_line6_toneport *toneport) 299 { 300 toneport_send_cmd(toneport->line6.usbdev, 301 (toneport->leds[0].dev.brightness << 8) | 0x0002, 302 toneport->leds[1].dev.brightness); 303 } 304 305 static void toneport_led_brightness_set(struct led_classdev *led_cdev, 306 enum led_brightness brightness) 307 { 308 struct toneport_led *leds = 309 container_of(led_cdev, struct toneport_led, dev); 310 toneport_update_led(leds->toneport); 311 } 312 313 static int toneport_init_leds(struct usb_line6_toneport *toneport) 314 { 315 struct device *dev = &toneport->line6.usbdev->dev; 316 int i, err; 317 318 for (i = 0; i < 2; i++) { 319 struct toneport_led *led = &toneport->leds[i]; 320 struct led_classdev *leddev = &led->dev; 321 322 led->toneport = toneport; 323 snprintf(led->name, sizeof(led->name), "%s::%s", 324 dev_name(dev), led_colors[i]); 325 leddev->name = led->name; 326 leddev->brightness = led_init_vals[i]; 327 leddev->max_brightness = 0x26; 328 leddev->brightness_set = toneport_led_brightness_set; 329 err = led_classdev_register(dev, leddev); 330 if (err) 331 return err; 332 led->registered = true; 333 } 334 335 return 0; 336 } 337 338 static void toneport_remove_leds(struct usb_line6_toneport *toneport) 339 { 340 struct toneport_led *led; 341 int i; 342 343 for (i = 0; i < 2; i++) { 344 led = &toneport->leds[i]; 345 if (!led->registered) 346 break; 347 led_classdev_unregister(&led->dev); 348 led->registered = false; 349 } 350 } 351 352 /* 353 Setup Toneport device. 354 */ 355 static void toneport_setup(struct usb_line6_toneport *toneport) 356 { 357 int ticks; 358 struct usb_line6 *line6 = &toneport->line6; 359 struct usb_device *usbdev = line6->usbdev; 360 361 /* sync time on device with host: */ 362 ticks = (int)get_seconds(); 363 line6_write_data(line6, 0x80c6, &ticks, 4); 364 365 /* enable device: */ 366 toneport_send_cmd(usbdev, 0x0301, 0x0000); 367 368 /* initialize source select: */ 369 switch (toneport->type) { 370 case LINE6_TONEPORT_UX1: 371 case LINE6_TONEPORT_UX2: 372 case LINE6_PODSTUDIO_UX1: 373 case LINE6_PODSTUDIO_UX2: 374 toneport_send_cmd(usbdev, 375 toneport_source_info[toneport->source].code, 376 0x0000); 377 default: 378 break; 379 } 380 381 if (toneport_has_led(toneport->type)) 382 toneport_update_led(toneport); 383 384 mod_timer(&toneport->timer, jiffies + TONEPORT_PCM_DELAY * HZ); 385 } 386 387 /* 388 Toneport device disconnected. 389 */ 390 static void line6_toneport_disconnect(struct usb_interface *interface) 391 { 392 struct usb_line6_toneport *toneport; 393 394 toneport = usb_get_intfdata(interface); 395 del_timer_sync(&toneport->timer); 396 397 if (toneport_has_led(toneport->type)) 398 toneport_remove_leds(toneport); 399 } 400 401 402 /* 403 Try to init Toneport device. 404 */ 405 static int toneport_init(struct usb_interface *interface, 406 struct usb_line6 *line6) 407 { 408 int err; 409 struct usb_line6_toneport *toneport = (struct usb_line6_toneport *) line6; 410 411 setup_timer(&toneport->timer, toneport_start_pcm, 412 (unsigned long)toneport); 413 414 line6->disconnect = line6_toneport_disconnect; 415 416 /* initialize PCM subsystem: */ 417 err = line6_init_pcm(line6, &toneport_pcm_properties); 418 if (err < 0) 419 return err; 420 421 /* register monitor control: */ 422 err = snd_ctl_add(line6->card, 423 snd_ctl_new1(&toneport_control_monitor, 424 line6->line6pcm)); 425 if (err < 0) 426 return err; 427 428 /* register source select control: */ 429 switch (toneport->type) { 430 case LINE6_TONEPORT_UX1: 431 case LINE6_TONEPORT_UX2: 432 case LINE6_PODSTUDIO_UX1: 433 case LINE6_PODSTUDIO_UX2: 434 err = 435 snd_ctl_add(line6->card, 436 snd_ctl_new1(&toneport_control_source, 437 line6->line6pcm)); 438 if (err < 0) 439 return err; 440 441 default: 442 break; 443 } 444 445 line6_read_serial_number(line6, &toneport->serial_number); 446 line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1); 447 448 if (toneport_has_led(toneport->type)) { 449 err = toneport_init_leds(toneport); 450 if (err < 0) 451 return err; 452 } 453 454 toneport_setup(toneport); 455 456 /* register audio system: */ 457 return snd_card_register(line6->card); 458 } 459 460 #ifdef CONFIG_PM 461 /* 462 Resume Toneport device after reset. 463 */ 464 static int toneport_reset_resume(struct usb_interface *interface) 465 { 466 toneport_setup(usb_get_intfdata(interface)); 467 return line6_resume(interface); 468 } 469 #endif 470 471 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod) 472 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n) 473 474 /* table of devices that work with this driver */ 475 static const struct usb_device_id toneport_id_table[] = { 476 { LINE6_DEVICE(0x4750), .driver_info = LINE6_GUITARPORT }, 477 { LINE6_DEVICE(0x4153), .driver_info = LINE6_PODSTUDIO_GX }, 478 { LINE6_DEVICE(0x4150), .driver_info = LINE6_PODSTUDIO_UX1 }, 479 { LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 }, 480 { LINE6_DEVICE(0x4147), .driver_info = LINE6_TONEPORT_GX }, 481 { LINE6_DEVICE(0x4141), .driver_info = LINE6_TONEPORT_UX1 }, 482 { LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 }, 483 {} 484 }; 485 486 MODULE_DEVICE_TABLE(usb, toneport_id_table); 487 488 static const struct line6_properties toneport_properties_table[] = { 489 [LINE6_GUITARPORT] = { 490 .id = "GuitarPort", 491 .name = "GuitarPort", 492 .capabilities = LINE6_CAP_PCM, 493 .altsetting = 2, /* 1..4 seem to be ok */ 494 /* no control channel */ 495 .ep_audio_r = 0x82, 496 .ep_audio_w = 0x01, 497 }, 498 [LINE6_PODSTUDIO_GX] = { 499 .id = "PODStudioGX", 500 .name = "POD Studio GX", 501 .capabilities = LINE6_CAP_PCM, 502 .altsetting = 2, /* 1..4 seem to be ok */ 503 /* no control channel */ 504 .ep_audio_r = 0x82, 505 .ep_audio_w = 0x01, 506 }, 507 [LINE6_PODSTUDIO_UX1] = { 508 .id = "PODStudioUX1", 509 .name = "POD Studio UX1", 510 .capabilities = LINE6_CAP_PCM, 511 .altsetting = 2, /* 1..4 seem to be ok */ 512 /* no control channel */ 513 .ep_audio_r = 0x82, 514 .ep_audio_w = 0x01, 515 }, 516 [LINE6_PODSTUDIO_UX2] = { 517 .id = "PODStudioUX2", 518 .name = "POD Studio UX2", 519 .capabilities = LINE6_CAP_PCM, 520 .altsetting = 2, /* defaults to 44.1kHz, 16-bit */ 521 /* no control channel */ 522 .ep_audio_r = 0x82, 523 .ep_audio_w = 0x01, 524 }, 525 [LINE6_TONEPORT_GX] = { 526 .id = "TonePortGX", 527 .name = "TonePort GX", 528 .capabilities = LINE6_CAP_PCM, 529 .altsetting = 2, /* 1..4 seem to be ok */ 530 /* no control channel */ 531 .ep_audio_r = 0x82, 532 .ep_audio_w = 0x01, 533 }, 534 [LINE6_TONEPORT_UX1] = { 535 .id = "TonePortUX1", 536 .name = "TonePort UX1", 537 .capabilities = LINE6_CAP_PCM, 538 .altsetting = 2, /* 1..4 seem to be ok */ 539 /* no control channel */ 540 .ep_audio_r = 0x82, 541 .ep_audio_w = 0x01, 542 }, 543 [LINE6_TONEPORT_UX2] = { 544 .id = "TonePortUX2", 545 .name = "TonePort UX2", 546 .capabilities = LINE6_CAP_PCM, 547 .altsetting = 2, /* defaults to 44.1kHz, 16-bit */ 548 /* no control channel */ 549 .ep_audio_r = 0x82, 550 .ep_audio_w = 0x01, 551 }, 552 }; 553 554 /* 555 Probe USB device. 556 */ 557 static int toneport_probe(struct usb_interface *interface, 558 const struct usb_device_id *id) 559 { 560 struct usb_line6_toneport *toneport; 561 562 toneport = kzalloc(sizeof(*toneport), GFP_KERNEL); 563 if (!toneport) 564 return -ENODEV; 565 toneport->type = id->driver_info; 566 return line6_probe(interface, &toneport->line6, 567 &toneport_properties_table[id->driver_info], 568 toneport_init); 569 } 570 571 static struct usb_driver toneport_driver = { 572 .name = KBUILD_MODNAME, 573 .probe = toneport_probe, 574 .disconnect = line6_disconnect, 575 #ifdef CONFIG_PM 576 .suspend = line6_suspend, 577 .resume = line6_resume, 578 .reset_resume = toneport_reset_resume, 579 #endif 580 .id_table = toneport_id_table, 581 }; 582 583 module_usb_driver(toneport_driver); 584 585 MODULE_DESCRIPTION("TonePort USB driver"); 586 MODULE_LICENSE("GPL"); 587