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