xref: /linux/sound/usb/line6/toneport.c (revision 63e20df1e5b2ef8d871ecbdb6c038d554ed1ca74)
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_STREAM_MONITOR);
193 	else
194 		line6_pcm_release(line6pcm, LINE6_STREAM_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_STREAM_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_line6 *line6)
391 {
392 	struct usb_line6_toneport *toneport =
393 		(struct usb_line6_toneport *)line6;
394 
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_line6 *line6,
406 			 const struct usb_device_id *id)
407 {
408 	int err;
409 	struct usb_line6_toneport *toneport =  (struct usb_line6_toneport *) line6;
410 
411 	toneport->type = id->driver_info;
412 	setup_timer(&toneport->timer, toneport_start_pcm,
413 		    (unsigned long)toneport);
414 
415 	line6->disconnect = line6_toneport_disconnect;
416 
417 	/* initialize PCM subsystem: */
418 	err = line6_init_pcm(line6, &toneport_pcm_properties);
419 	if (err < 0)
420 		return err;
421 
422 	/* register monitor control: */
423 	err = snd_ctl_add(line6->card,
424 			  snd_ctl_new1(&toneport_control_monitor,
425 				       line6->line6pcm));
426 	if (err < 0)
427 		return err;
428 
429 	/* register source select control: */
430 	switch (toneport->type) {
431 	case LINE6_TONEPORT_UX1:
432 	case LINE6_TONEPORT_UX2:
433 	case LINE6_PODSTUDIO_UX1:
434 	case LINE6_PODSTUDIO_UX2:
435 		err =
436 		    snd_ctl_add(line6->card,
437 				snd_ctl_new1(&toneport_control_source,
438 					     line6->line6pcm));
439 		if (err < 0)
440 			return err;
441 
442 	default:
443 		break;
444 	}
445 
446 	line6_read_serial_number(line6, &toneport->serial_number);
447 	line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
448 
449 	if (toneport_has_led(toneport->type)) {
450 		err = toneport_init_leds(toneport);
451 		if (err < 0)
452 			return err;
453 	}
454 
455 	toneport_setup(toneport);
456 
457 	/* register audio system: */
458 	return snd_card_register(line6->card);
459 }
460 
461 #ifdef CONFIG_PM
462 /*
463 	Resume Toneport device after reset.
464 */
465 static int toneport_reset_resume(struct usb_interface *interface)
466 {
467 	toneport_setup(usb_get_intfdata(interface));
468 	return line6_resume(interface);
469 }
470 #endif
471 
472 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
473 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
474 
475 /* table of devices that work with this driver */
476 static const struct usb_device_id toneport_id_table[] = {
477 	{ LINE6_DEVICE(0x4750),    .driver_info = LINE6_GUITARPORT },
478 	{ LINE6_DEVICE(0x4153),    .driver_info = LINE6_PODSTUDIO_GX },
479 	{ LINE6_DEVICE(0x4150),    .driver_info = LINE6_PODSTUDIO_UX1 },
480 	{ LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 },
481 	{ LINE6_DEVICE(0x4147),    .driver_info = LINE6_TONEPORT_GX },
482 	{ LINE6_DEVICE(0x4141),    .driver_info = LINE6_TONEPORT_UX1 },
483 	{ LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 },
484 	{}
485 };
486 
487 MODULE_DEVICE_TABLE(usb, toneport_id_table);
488 
489 static const struct line6_properties toneport_properties_table[] = {
490 	[LINE6_GUITARPORT] = {
491 		.id = "GuitarPort",
492 		.name = "GuitarPort",
493 		.capabilities	= LINE6_CAP_PCM,
494 		.altsetting = 2,  /* 1..4 seem to be ok */
495 		/* no control channel */
496 		.ep_audio_r = 0x82,
497 		.ep_audio_w = 0x01,
498 	},
499 	[LINE6_PODSTUDIO_GX] = {
500 		.id = "PODStudioGX",
501 		.name = "POD Studio GX",
502 		.capabilities	= LINE6_CAP_PCM,
503 		.altsetting = 2,  /* 1..4 seem to be ok */
504 		/* no control channel */
505 		.ep_audio_r = 0x82,
506 		.ep_audio_w = 0x01,
507 	},
508 	[LINE6_PODSTUDIO_UX1] = {
509 		.id = "PODStudioUX1",
510 		.name = "POD Studio UX1",
511 		.capabilities	= LINE6_CAP_PCM,
512 		.altsetting = 2,  /* 1..4 seem to be ok */
513 		/* no control channel */
514 		.ep_audio_r = 0x82,
515 		.ep_audio_w = 0x01,
516 	},
517 	[LINE6_PODSTUDIO_UX2] = {
518 		.id = "PODStudioUX2",
519 		.name = "POD Studio UX2",
520 		.capabilities	= LINE6_CAP_PCM,
521 		.altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
522 		/* no control channel */
523 		.ep_audio_r = 0x82,
524 		.ep_audio_w = 0x01,
525 	},
526 	[LINE6_TONEPORT_GX] = {
527 		.id = "TonePortGX",
528 		.name = "TonePort GX",
529 		.capabilities	= LINE6_CAP_PCM,
530 		.altsetting = 2,  /* 1..4 seem to be ok */
531 		/* no control channel */
532 		.ep_audio_r = 0x82,
533 		.ep_audio_w = 0x01,
534 	},
535 	[LINE6_TONEPORT_UX1] = {
536 		.id = "TonePortUX1",
537 		.name = "TonePort UX1",
538 		.capabilities	= LINE6_CAP_PCM,
539 		.altsetting = 2,  /* 1..4 seem to be ok */
540 		/* no control channel */
541 		.ep_audio_r = 0x82,
542 		.ep_audio_w = 0x01,
543 	},
544 	[LINE6_TONEPORT_UX2] = {
545 		.id = "TonePortUX2",
546 		.name = "TonePort UX2",
547 		.capabilities	= LINE6_CAP_PCM,
548 		.altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
549 		/* no control channel */
550 		.ep_audio_r = 0x82,
551 		.ep_audio_w = 0x01,
552 	},
553 };
554 
555 /*
556 	Probe USB device.
557 */
558 static int toneport_probe(struct usb_interface *interface,
559 			  const struct usb_device_id *id)
560 {
561 	return line6_probe(interface, id,
562 			   &toneport_properties_table[id->driver_info],
563 			   toneport_init, sizeof(struct usb_line6_toneport));
564 }
565 
566 static struct usb_driver toneport_driver = {
567 	.name = KBUILD_MODNAME,
568 	.probe = toneport_probe,
569 	.disconnect = line6_disconnect,
570 #ifdef CONFIG_PM
571 	.suspend = line6_suspend,
572 	.resume = line6_resume,
573 	.reset_resume = toneport_reset_resume,
574 #endif
575 	.id_table = toneport_id_table,
576 };
577 
578 module_usb_driver(toneport_driver);
579 
580 MODULE_DESCRIPTION("TonePort USB driver");
581 MODULE_LICENSE("GPL");
582