toneport.c 14.9 KB
Newer Older
M
Markus Grabner 已提交
1
/*
2
 * Line6 Linux USB driver - 0.9.1beta
M
Markus Grabner 已提交
3
 *
4
 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
M
Markus Grabner 已提交
5 6 7 8 9 10 11 12
 *                         Emil Myhrman (emil.myhrman@gmail.com)
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation, version 2.
 *
 */

13
#include <linux/wait.h>
T
Takashi Iwai 已提交
14 15 16 17
#include <linux/usb.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <sound/core.h>
18
#include <sound/control.h>
M
Markus Grabner 已提交
19 20 21

#include "audio.h"
#include "capture.h"
22
#include "driver.h"
M
Markus Grabner 已提交
23
#include "playback.h"
T
Takashi Iwai 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "usbdefs.h"

enum line6_device_type {
	LINE6_GUITARPORT,
	LINE6_PODSTUDIO_GX,
	LINE6_PODSTUDIO_UX1,
	LINE6_PODSTUDIO_UX2,
	LINE6_TONEPORT_GX,
	LINE6_TONEPORT_UX1,
	LINE6_TONEPORT_UX2,
};

struct usb_line6_toneport {
	/**
		Generic Line6 USB data.
	*/
	struct usb_line6 line6;

	/**
		Source selector.
	*/
	int source;

	/**
		Serial number of device.
	*/
	int serial_number;

	/**
		Firmware version (x 100).
	*/
	int firmware_version;

	/**
		 Timer for delayed PCM startup.
	*/
	struct timer_list timer;

	/**
		 Device type.
	*/
	enum line6_device_type type;
};
M
Markus Grabner 已提交
67 68 69

static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);

70 71
#define TONEPORT_PCM_DELAY 1

M
Markus Grabner 已提交
72 73 74 75 76 77 78 79
static struct snd_ratden toneport_ratden = {
	.num_min = 44100,
	.num_max = 44100,
	.num_step = 1,
	.den = 1
};

static struct line6_pcm_properties toneport_pcm_properties = {
80
	.snd_line6_playback_hw = {
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
				  .info = (SNDRV_PCM_INFO_MMAP |
					   SNDRV_PCM_INFO_INTERLEAVED |
					   SNDRV_PCM_INFO_BLOCK_TRANSFER |
					   SNDRV_PCM_INFO_MMAP_VALID |
					   SNDRV_PCM_INFO_PAUSE |
					   SNDRV_PCM_INFO_SYNC_START),
				  .formats = SNDRV_PCM_FMTBIT_S16_LE,
				  .rates = SNDRV_PCM_RATE_KNOT,
				  .rate_min = 44100,
				  .rate_max = 44100,
				  .channels_min = 2,
				  .channels_max = 2,
				  .buffer_bytes_max = 60000,
				  .period_bytes_min = 64,
				  .period_bytes_max = 8192,
				  .periods_min = 1,
				  .periods_max = 1024},
98
	.snd_line6_capture_hw = {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
				 .info = (SNDRV_PCM_INFO_MMAP |
					  SNDRV_PCM_INFO_INTERLEAVED |
					  SNDRV_PCM_INFO_BLOCK_TRANSFER |
					  SNDRV_PCM_INFO_MMAP_VALID |
					  SNDRV_PCM_INFO_SYNC_START),
				 .formats = SNDRV_PCM_FMTBIT_S16_LE,
				 .rates = SNDRV_PCM_RATE_KNOT,
				 .rate_min = 44100,
				 .rate_max = 44100,
				 .channels_min = 2,
				 .channels_max = 2,
				 .buffer_bytes_max = 60000,
				 .period_bytes_min = 64,
				 .period_bytes_max = 8192,
				 .periods_min = 1,
				 .periods_max = 1024},
M
Markus Grabner 已提交
115
	.snd_line6_rates = {
116 117
			    .nrats = 1,
			    .rats = &toneport_ratden},
M
Markus Grabner 已提交
118 119 120 121 122
	.bytes_per_frame = 4
};

/*
	For the led on Guitarport.
123 124
	Brightness goes from 0x00 to 0x26. Set a value above this to have led
	blink.
M
Markus Grabner 已提交
125 126 127 128 129
	(void cmd_0x02(byte red, byte green)
*/
static int led_red = 0x00;
static int led_green = 0x26;

130
static const struct {
131 132
	const char *name;
	int code;
133
} toneport_source_info[] = {
134 135 136 137
	{"Microphone", 0x0a01},
	{"Line", 0x0801},
	{"Instrument", 0x0b01},
	{"Inst & Mic", 0x0901}
138 139
};

140
static bool toneport_has_led(enum line6_device_type type)
141 142
{
	return
143 144
	    (type == LINE6_GUITARPORT) ||
	    (type == LINE6_TONEPORT_GX);
145 146 147
	/* add your device here if you are missing support for the LEDs */
}

148 149 150 151 152
static void toneport_update_led(struct device *dev)
{
	struct usb_interface *interface = to_usb_interface(dev);
	struct usb_line6_toneport *tp = usb_get_intfdata(interface);
	struct usb_line6 *line6;
M
Markus Grabner 已提交
153

154 155 156 157 158 159 160
	if (!tp)
		return;

	line6 = &tp->line6;
	if (line6)
		toneport_send_cmd(line6->usbdev, (led_red << 8) | 0x0002,
				  led_green);
M
Markus Grabner 已提交
161
}
162

163 164
static ssize_t toneport_set_led_red(struct device *dev,
				    struct device_attribute *attr,
165 166
				    const char *buf, size_t count)
{
167 168
	int retval;

169
	retval = kstrtoint(buf, 10, &led_red);
170 171 172
	if (retval)
		return retval;

M
Markus Grabner 已提交
173 174 175
	toneport_update_led(dev);
	return count;
}
176

177 178
static ssize_t toneport_set_led_green(struct device *dev,
				      struct device_attribute *attr,
179 180
				      const char *buf, size_t count)
{
181 182
	int retval;

183
	retval = kstrtoint(buf, 10, &led_green);
184 185 186
	if (retval)
		return retval;

M
Markus Grabner 已提交
187 188 189 190
	toneport_update_led(dev);
	return count;
}

191
static DEVICE_ATTR(led_red, S_IWUSR | S_IRUGO, line6_nop_read,
192
		   toneport_set_led_red);
193
static DEVICE_ATTR(led_green, S_IWUSR | S_IRUGO, line6_nop_read,
194
		   toneport_set_led_green);
M
Markus Grabner 已提交
195 196 197 198 199

static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
{
	int ret;

200 201 202 203 204
	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
			      cmd1, cmd2, NULL, 0, LINE6_TIMEOUT * HZ);

	if (ret < 0) {
205
		dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
M
Markus Grabner 已提交
206 207 208 209 210 211
		return ret;
	}

	return 0;
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
/* monitor info callback */
static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 256;
	return 0;
}

/* monitor get callback */
static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
				    struct snd_ctl_elem_value *ucontrol)
{
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
228

229 230 231 232 233 234 235 236 237 238
	ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
	return 0;
}

/* monitor put callback */
static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
				    struct snd_ctl_elem_value *ucontrol)
{
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);

239
	if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
240 241 242
		return 0;

	line6pcm->volume_monitor = ucontrol->value.integer.value[0];
243 244

	if (line6pcm->volume_monitor > 0)
245
		line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_MONITOR);
246
	else
247
		line6_pcm_release(line6pcm, LINE6_BITS_PCM_MONITOR);
248

249 250 251 252 253 254 255 256
	return 1;
}

/* source info callback */
static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
				    struct snd_ctl_elem_info *uinfo)
{
	const int size = ARRAY_SIZE(toneport_source_info);
257

258 259 260 261
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = size;

262
	if (uinfo->value.enumerated.item >= size)
263 264 265 266 267 268 269 270 271 272 273 274 275
		uinfo->value.enumerated.item = size - 1;

	strcpy(uinfo->value.enumerated.name,
	       toneport_source_info[uinfo->value.enumerated.item].name);

	return 0;
}

/* source get callback */
static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
276 277
	struct usb_line6_toneport *toneport =
	    (struct usb_line6_toneport *)line6pcm->line6;
278 279 280 281 282 283 284 285 286
	ucontrol->value.enumerated.item[0] = toneport->source;
	return 0;
}

/* source put callback */
static int snd_toneport_source_put(struct snd_kcontrol *kcontrol,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
287 288
	struct usb_line6_toneport *toneport =
	    (struct usb_line6_toneport *)line6pcm->line6;
289
	unsigned int source;
290

291 292 293 294
	source = ucontrol->value.enumerated.item[0];
	if (source >= ARRAY_SIZE(toneport_source_info))
		return -EINVAL;
	if (source == toneport->source)
295 296
		return 0;

297
	toneport->source = source;
298
	toneport_send_cmd(toneport->line6.usbdev,
299
			  toneport_source_info[source].code, 0x0000);
300 301 302 303 304 305 306
	return 1;
}

static void toneport_start_pcm(unsigned long arg)
{
	struct usb_line6_toneport *toneport = (struct usb_line6_toneport *)arg;
	struct usb_line6 *line6 = &toneport->line6;
307

308
	line6_pcm_acquire(line6->line6pcm, LINE6_BITS_PCM_MONITOR);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
}

/* control definition */
static struct snd_kcontrol_new toneport_control_monitor = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Monitor Playback Volume",
	.index = 0,
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
	.info = snd_toneport_monitor_info,
	.get = snd_toneport_monitor_get,
	.put = snd_toneport_monitor_put
};

/* source selector definition */
static struct snd_kcontrol_new toneport_control_source = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "PCM Capture Source",
	.index = 0,
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
	.info = snd_toneport_source_info,
	.get = snd_toneport_source_get,
	.put = snd_toneport_source_put
};

M
Markus Grabner 已提交
333 334 335 336 337 338 339
/*
	Toneport destructor.
*/
static void toneport_destruct(struct usb_interface *interface)
{
	struct usb_line6_toneport *toneport = usb_get_intfdata(interface);

340 341
	if (toneport == NULL)
		return;
342
	line6_cleanup_audio(&toneport->line6);
M
Markus Grabner 已提交
343 344 345
}

/*
346
	Setup Toneport device.
M
Markus Grabner 已提交
347
*/
348
static void toneport_setup(struct usb_line6_toneport *toneport)
M
Markus Grabner 已提交
349
{
350
	int ticks;
M
Markus Grabner 已提交
351
	struct usb_line6 *line6 = &toneport->line6;
352 353 354 355 356 357 358 359 360 361
	struct usb_device *usbdev = line6->usbdev;

	/* sync time on device with host: */
	ticks = (int)get_seconds();
	line6_write_data(line6, 0x80c6, &ticks, 4);

	/* enable device: */
	toneport_send_cmd(usbdev, 0x0301, 0x0000);

	/* initialize source select: */
T
Takashi Iwai 已提交
362
	switch (toneport->type) {
363 364 365 366
	case LINE6_TONEPORT_UX1:
	case LINE6_TONEPORT_UX2:
	case LINE6_PODSTUDIO_UX1:
	case LINE6_PODSTUDIO_UX2:
367 368 369
		toneport_send_cmd(usbdev,
				  toneport_source_info[toneport->source].code,
				  0x0000);
370 371
	default:
		break;
372 373
	}

T
Takashi Iwai 已提交
374
	if (toneport_has_led(toneport->type))
375 376 377
		toneport_update_led(&usbdev->dev);
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/*
	Toneport device disconnected.
*/
static void line6_toneport_disconnect(struct usb_interface *interface)
{
	struct usb_line6_toneport *toneport;
	u16 idProduct;

	if (interface == NULL)
		return;

	toneport = usb_get_intfdata(interface);
	del_timer_sync(&toneport->timer);
	idProduct = le16_to_cpu(toneport->line6.usbdev->descriptor.idProduct);

	if (toneport_has_led(idProduct)) {
		device_remove_file(&interface->dev, &dev_attr_led_red);
		device_remove_file(&interface->dev, &dev_attr_led_green);
	}

	if (toneport != NULL) {
		struct snd_line6_pcm *line6pcm = toneport->line6.line6pcm;

		if (line6pcm != NULL) {
			line6_pcm_release(line6pcm, LINE6_BITS_PCM_MONITOR);
			line6_pcm_disconnect(line6pcm);
		}
	}

	toneport_destruct(interface);
}


411 412 413 414
/*
	 Try to init Toneport device.
*/
static int toneport_try_init(struct usb_interface *interface,
415
			     struct usb_line6 *line6)
416 417
{
	int err;
418
	struct usb_line6_toneport *toneport =  (struct usb_line6_toneport *) line6;
M
Markus Grabner 已提交
419

420
	if ((interface == NULL) || (toneport == NULL))
M
Markus Grabner 已提交
421 422
		return -ENODEV;

423 424
	line6->disconnect = line6_toneport_disconnect;

M
Markus Grabner 已提交
425
	/* initialize audio system: */
426
	err = line6_init_audio(line6);
427
	if (err < 0)
M
Markus Grabner 已提交
428 429 430
		return err;

	/* initialize PCM subsystem: */
431
	err = line6_init_pcm(line6, &toneport_pcm_properties);
432
	if (err < 0)
M
Markus Grabner 已提交
433 434
		return err;

435
	/* register monitor control: */
436 437 438 439
	err = snd_ctl_add(line6->card,
			  snd_ctl_new1(&toneport_control_monitor,
				       line6->line6pcm));
	if (err < 0)
440 441 442
		return err;

	/* register source select control: */
T
Takashi Iwai 已提交
443
	switch (toneport->type) {
444 445 446 447
	case LINE6_TONEPORT_UX1:
	case LINE6_TONEPORT_UX2:
	case LINE6_PODSTUDIO_UX1:
	case LINE6_PODSTUDIO_UX2:
448 449 450 451
		err =
		    snd_ctl_add(line6->card,
				snd_ctl_new1(&toneport_control_source,
					     line6->line6pcm));
452
		if (err < 0)
453
			return err;
454 455 456

	default:
		break;
457 458
	}

M
Markus Grabner 已提交
459
	/* register audio system: */
460
	err = line6_register_audio(line6);
461
	if (err < 0)
M
Markus Grabner 已提交
462 463 464 465 466
		return err;

	line6_read_serial_number(line6, &toneport->serial_number);
	line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);

T
Takashi Iwai 已提交
467
	if (toneport_has_led(toneport->type)) {
468 469 470 471
		CHECK_RETURN(device_create_file
			     (&interface->dev, &dev_attr_led_red));
		CHECK_RETURN(device_create_file
			     (&interface->dev, &dev_attr_led_green));
472
	}
M
Markus Grabner 已提交
473

474
	toneport_setup(toneport);
M
Markus Grabner 已提交
475

476 477 478
	setup_timer(&toneport->timer, toneport_start_pcm,
		    (unsigned long)toneport);
	mod_timer(&toneport->timer, jiffies + TONEPORT_PCM_DELAY * HZ);
M
Markus Grabner 已提交
479 480 481 482

	return 0;
}

483 484 485
/*
	 Init Toneport device (and clean up in case of failure).
*/
T
Takashi Iwai 已提交
486 487
static int toneport_init(struct usb_interface *interface,
			 struct usb_line6 *line6)
488
{
489
	int err = toneport_try_init(interface, line6);
490

491
	if (err < 0)
492 493 494 495 496
		toneport_destruct(interface);

	return err;
}

T
Takashi Iwai 已提交
497
#ifdef CONFIG_PM
498 499 500
/*
	Resume Toneport device after reset.
*/
T
Takashi Iwai 已提交
501
static int toneport_reset_resume(struct usb_interface *interface)
502
{
T
Takashi Iwai 已提交
503 504
	toneport_setup(usb_get_intfdata(interface));
	return line6_resume(interface);
505
}
T
Takashi Iwai 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
#endif

#define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
#define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)

/* table of devices that work with this driver */
static const struct usb_device_id toneport_id_table[] = {
	{ LINE6_DEVICE(0x4750),    .driver_info = LINE6_GUITARPORT },
	{ LINE6_DEVICE(0x4153),    .driver_info = LINE6_PODSTUDIO_GX },
	{ LINE6_DEVICE(0x4150),    .driver_info = LINE6_PODSTUDIO_UX1 },
	{ LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 },
	{ LINE6_DEVICE(0x4147),    .driver_info = LINE6_TONEPORT_GX },
	{ LINE6_DEVICE(0x4141),    .driver_info = LINE6_TONEPORT_UX1 },
	{ LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 },
	{}
};

MODULE_DEVICE_TABLE(usb, toneport_id_table);

static const struct line6_properties toneport_properties_table[] = {
	[LINE6_GUITARPORT] = {
		.id = "GuitarPort",
		.name = "GuitarPort",
		.capabilities	= LINE6_CAP_PCM,
		.altsetting = 2,  /* 1..4 seem to be ok */
		/* no control channel */
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
	},
	[LINE6_PODSTUDIO_GX] = {
		.id = "PODStudioGX",
		.name = "POD Studio GX",
		.capabilities	= LINE6_CAP_PCM,
		.altsetting = 2,  /* 1..4 seem to be ok */
		/* no control channel */
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
	},
	[LINE6_PODSTUDIO_UX1] = {
		.id = "PODStudioUX1",
		.name = "POD Studio UX1",
		.capabilities	= LINE6_CAP_PCM,
		.altsetting = 2,  /* 1..4 seem to be ok */
		/* no control channel */
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
	},
	[LINE6_PODSTUDIO_UX2] = {
		.id = "PODStudioUX2",
		.name = "POD Studio UX2",
		.capabilities	= LINE6_CAP_PCM,
		.altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
		/* no control channel */
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
	},
	[LINE6_TONEPORT_GX] = {
		.id = "TonePortGX",
		.name = "TonePort GX",
		.capabilities	= LINE6_CAP_PCM,
		.altsetting = 2,  /* 1..4 seem to be ok */
		/* no control channel */
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
	},
	[LINE6_TONEPORT_UX1] = {
		.id = "TonePortUX1",
		.name = "TonePort UX1",
		.capabilities	= LINE6_CAP_PCM,
		.altsetting = 2,  /* 1..4 seem to be ok */
		/* no control channel */
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
	},
	[LINE6_TONEPORT_UX2] = {
		.id = "TonePortUX2",
		.name = "TonePort UX2",
		.capabilities	= LINE6_CAP_PCM,
		.altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
		/* no control channel */
		.ep_audio_r = 0x82,
		.ep_audio_w = 0x01,
	},
};

/*
	Probe USB device.
*/
static int toneport_probe(struct usb_interface *interface,
			  const struct usb_device_id *id)
{
	struct usb_line6_toneport *toneport;
	int err;

	toneport = kzalloc(sizeof(*toneport), GFP_KERNEL);
	if (!toneport)
		return -ENODEV;
	toneport->type = id->driver_info;
	err = line6_probe(interface, &toneport->line6,
			  &toneport_properties_table[id->driver_info],
			  toneport_init);
	if (err < 0)
		kfree(toneport);
	return err;
}

static struct usb_driver toneport_driver = {
	.name = KBUILD_MODNAME,
	.probe = toneport_probe,
	.disconnect = line6_disconnect,
#ifdef CONFIG_PM
	.suspend = line6_suspend,
	.resume = line6_resume,
	.reset_resume = toneport_reset_resume,
#endif
	.id_table = toneport_id_table,
};

module_usb_driver(toneport_driver);

MODULE_DESCRIPTION("TonePort USB driver");
MODULE_LICENSE("GPL");