xref: /openbmc/linux/sound/usb/line6/toneport.c (revision ae213c44)
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 
25 enum line6_device_type {
26 	LINE6_GUITARPORT,
27 	LINE6_PODSTUDIO_GX,
28 	LINE6_PODSTUDIO_UX1,
29 	LINE6_PODSTUDIO_UX2,
30 	LINE6_TONEPORT_GX,
31 	LINE6_TONEPORT_UX1,
32 	LINE6_TONEPORT_UX2,
33 };
34 
35 struct usb_line6_toneport;
36 
37 struct toneport_led {
38 	struct led_classdev dev;
39 	char name[64];
40 	struct usb_line6_toneport *toneport;
41 	bool registered;
42 };
43 
44 struct usb_line6_toneport {
45 	/* Generic Line 6 USB data */
46 	struct usb_line6 line6;
47 
48 	/* Source selector */
49 	int source;
50 
51 	/* Serial number of device */
52 	u32 serial_number;
53 
54 	/* Firmware version (x 100) */
55 	u8 firmware_version;
56 
57 	/* Work for delayed PCM startup */
58 	struct delayed_work pcm_work;
59 
60 	/* Device type */
61 	enum line6_device_type type;
62 
63 	/* LED instances */
64 	struct toneport_led leds[2];
65 };
66 
67 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
68 
69 #define TONEPORT_PCM_DELAY 1
70 
71 static struct snd_ratden toneport_ratden = {
72 	.num_min = 44100,
73 	.num_max = 44100,
74 	.num_step = 1,
75 	.den = 1
76 };
77 
78 static struct line6_pcm_properties toneport_pcm_properties = {
79 	.playback_hw = {
80 				  .info = (SNDRV_PCM_INFO_MMAP |
81 					   SNDRV_PCM_INFO_INTERLEAVED |
82 					   SNDRV_PCM_INFO_BLOCK_TRANSFER |
83 					   SNDRV_PCM_INFO_MMAP_VALID |
84 					   SNDRV_PCM_INFO_PAUSE |
85 					   SNDRV_PCM_INFO_SYNC_START),
86 				  .formats = SNDRV_PCM_FMTBIT_S16_LE,
87 				  .rates = SNDRV_PCM_RATE_KNOT,
88 				  .rate_min = 44100,
89 				  .rate_max = 44100,
90 				  .channels_min = 2,
91 				  .channels_max = 2,
92 				  .buffer_bytes_max = 60000,
93 				  .period_bytes_min = 64,
94 				  .period_bytes_max = 8192,
95 				  .periods_min = 1,
96 				  .periods_max = 1024},
97 	.capture_hw = {
98 				 .info = (SNDRV_PCM_INFO_MMAP |
99 					  SNDRV_PCM_INFO_INTERLEAVED |
100 					  SNDRV_PCM_INFO_BLOCK_TRANSFER |
101 					  SNDRV_PCM_INFO_MMAP_VALID |
102 					  SNDRV_PCM_INFO_SYNC_START),
103 				 .formats = SNDRV_PCM_FMTBIT_S16_LE,
104 				 .rates = SNDRV_PCM_RATE_KNOT,
105 				 .rate_min = 44100,
106 				 .rate_max = 44100,
107 				 .channels_min = 2,
108 				 .channels_max = 2,
109 				 .buffer_bytes_max = 60000,
110 				 .period_bytes_min = 64,
111 				 .period_bytes_max = 8192,
112 				 .periods_min = 1,
113 				 .periods_max = 1024},
114 	.rates = {
115 			    .nrats = 1,
116 			    .rats = &toneport_ratden},
117 	.bytes_per_channel = 2
118 };
119 
120 static const struct {
121 	const char *name;
122 	int code;
123 } toneport_source_info[] = {
124 	{"Microphone", 0x0a01},
125 	{"Line", 0x0801},
126 	{"Instrument", 0x0b01},
127 	{"Inst & Mic", 0x0901}
128 };
129 
130 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
131 {
132 	int ret;
133 
134 	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
135 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
136 			      cmd1, cmd2, NULL, 0, LINE6_TIMEOUT * HZ);
137 
138 	if (ret < 0) {
139 		dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
140 		return ret;
141 	}
142 
143 	return 0;
144 }
145 
146 /* monitor info callback */
147 static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
148 				     struct snd_ctl_elem_info *uinfo)
149 {
150 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
151 	uinfo->count = 1;
152 	uinfo->value.integer.min = 0;
153 	uinfo->value.integer.max = 256;
154 	return 0;
155 }
156 
157 /* monitor get callback */
158 static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
159 				    struct snd_ctl_elem_value *ucontrol)
160 {
161 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
162 
163 	ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
164 	return 0;
165 }
166 
167 /* monitor put callback */
168 static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
169 				    struct snd_ctl_elem_value *ucontrol)
170 {
171 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
172 	int err;
173 
174 	if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
175 		return 0;
176 
177 	line6pcm->volume_monitor = ucontrol->value.integer.value[0];
178 
179 	if (line6pcm->volume_monitor > 0) {
180 		err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR, true);
181 		if (err < 0) {
182 			line6pcm->volume_monitor = 0;
183 			line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
184 			return err;
185 		}
186 	} else {
187 		line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
188 	}
189 
190 	return 1;
191 }
192 
193 /* source info callback */
194 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
195 				    struct snd_ctl_elem_info *uinfo)
196 {
197 	const int size = ARRAY_SIZE(toneport_source_info);
198 
199 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
200 	uinfo->count = 1;
201 	uinfo->value.enumerated.items = size;
202 
203 	if (uinfo->value.enumerated.item >= size)
204 		uinfo->value.enumerated.item = size - 1;
205 
206 	strcpy(uinfo->value.enumerated.name,
207 	       toneport_source_info[uinfo->value.enumerated.item].name);
208 
209 	return 0;
210 }
211 
212 /* source get callback */
213 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
214 				   struct snd_ctl_elem_value *ucontrol)
215 {
216 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
217 	struct usb_line6_toneport *toneport =
218 	    (struct usb_line6_toneport *)line6pcm->line6;
219 	ucontrol->value.enumerated.item[0] = toneport->source;
220 	return 0;
221 }
222 
223 /* source put callback */
224 static int snd_toneport_source_put(struct snd_kcontrol *kcontrol,
225 				   struct snd_ctl_elem_value *ucontrol)
226 {
227 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
228 	struct usb_line6_toneport *toneport =
229 	    (struct usb_line6_toneport *)line6pcm->line6;
230 	unsigned int source;
231 
232 	source = ucontrol->value.enumerated.item[0];
233 	if (source >= ARRAY_SIZE(toneport_source_info))
234 		return -EINVAL;
235 	if (source == toneport->source)
236 		return 0;
237 
238 	toneport->source = source;
239 	toneport_send_cmd(toneport->line6.usbdev,
240 			  toneport_source_info[source].code, 0x0000);
241 	return 1;
242 }
243 
244 static void toneport_start_pcm(struct work_struct *work)
245 {
246 	struct usb_line6_toneport *toneport =
247 		container_of(work, struct usb_line6_toneport, pcm_work.work);
248 	struct usb_line6 *line6 = &toneport->line6;
249 
250 	line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR, true);
251 }
252 
253 /* control definition */
254 static const 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 const 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(struct usb_line6_toneport *toneport)
283 {
284 	switch (toneport->type) {
285 	case LINE6_GUITARPORT:
286 	case LINE6_TONEPORT_GX:
287 	/* add your device here if you are missing support for the LEDs */
288 		return true;
289 
290 	default:
291 		return false;
292 	}
293 }
294 
295 static const char * const toneport_led_colors[2] = { "red", "green" };
296 static const int toneport_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), toneport_led_colors[i]);
325 		leddev->name = led->name;
326 		leddev->brightness = toneport_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 static bool toneport_has_source_select(struct usb_line6_toneport *toneport)
353 {
354 	switch (toneport->type) {
355 	case LINE6_TONEPORT_UX1:
356 	case LINE6_TONEPORT_UX2:
357 	case LINE6_PODSTUDIO_UX1:
358 	case LINE6_PODSTUDIO_UX2:
359 		return true;
360 
361 	default:
362 		return false;
363 	}
364 }
365 
366 /*
367 	Setup Toneport device.
368 */
369 static int toneport_setup(struct usb_line6_toneport *toneport)
370 {
371 	u32 *ticks;
372 	struct usb_line6 *line6 = &toneport->line6;
373 	struct usb_device *usbdev = line6->usbdev;
374 
375 	ticks = kmalloc(sizeof(*ticks), GFP_KERNEL);
376 	if (!ticks)
377 		return -ENOMEM;
378 
379 	/* sync time on device with host: */
380 	/* note: 32-bit timestamps overflow in year 2106 */
381 	*ticks = (u32)ktime_get_real_seconds();
382 	line6_write_data(line6, 0x80c6, ticks, 4);
383 	kfree(ticks);
384 
385 	/* enable device: */
386 	toneport_send_cmd(usbdev, 0x0301, 0x0000);
387 
388 	/* initialize source select: */
389 	if (toneport_has_source_select(toneport))
390 		toneport_send_cmd(usbdev,
391 				  toneport_source_info[toneport->source].code,
392 				  0x0000);
393 
394 	if (toneport_has_led(toneport))
395 		toneport_update_led(toneport);
396 
397 	schedule_delayed_work(&toneport->pcm_work,
398 			      msecs_to_jiffies(TONEPORT_PCM_DELAY * 1000));
399 	return 0;
400 }
401 
402 /*
403 	Toneport device disconnected.
404 */
405 static void line6_toneport_disconnect(struct usb_line6 *line6)
406 {
407 	struct usb_line6_toneport *toneport =
408 		(struct usb_line6_toneport *)line6;
409 
410 	cancel_delayed_work_sync(&toneport->pcm_work);
411 
412 	if (toneport_has_led(toneport))
413 		toneport_remove_leds(toneport);
414 }
415 
416 
417 /*
418 	 Try to init Toneport device.
419 */
420 static int toneport_init(struct usb_line6 *line6,
421 			 const struct usb_device_id *id)
422 {
423 	int err;
424 	struct usb_line6_toneport *toneport =  (struct usb_line6_toneport *) line6;
425 
426 	toneport->type = id->driver_info;
427 	INIT_DELAYED_WORK(&toneport->pcm_work, toneport_start_pcm);
428 
429 	line6->disconnect = line6_toneport_disconnect;
430 
431 	/* initialize PCM subsystem: */
432 	err = line6_init_pcm(line6, &toneport_pcm_properties);
433 	if (err < 0)
434 		return err;
435 
436 	/* register monitor control: */
437 	err = snd_ctl_add(line6->card,
438 			  snd_ctl_new1(&toneport_control_monitor,
439 				       line6->line6pcm));
440 	if (err < 0)
441 		return err;
442 
443 	/* register source select control: */
444 	if (toneport_has_source_select(toneport)) {
445 		err =
446 		    snd_ctl_add(line6->card,
447 				snd_ctl_new1(&toneport_control_source,
448 					     line6->line6pcm));
449 		if (err < 0)
450 			return err;
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)) {
457 		err = toneport_init_leds(toneport);
458 		if (err < 0)
459 			return err;
460 	}
461 
462 	err = toneport_setup(toneport);
463 	if (err)
464 		return err;
465 
466 	/* register audio system: */
467 	return snd_card_register(line6->card);
468 }
469 
470 #ifdef CONFIG_PM
471 /*
472 	Resume Toneport device after reset.
473 */
474 static int toneport_reset_resume(struct usb_interface *interface)
475 {
476 	int err;
477 
478 	err = toneport_setup(usb_get_intfdata(interface));
479 	if (err)
480 		return err;
481 	return line6_resume(interface);
482 }
483 #endif
484 
485 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
486 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
487 
488 /* table of devices that work with this driver */
489 static const struct usb_device_id toneport_id_table[] = {
490 	{ LINE6_DEVICE(0x4750),    .driver_info = LINE6_GUITARPORT },
491 	{ LINE6_DEVICE(0x4153),    .driver_info = LINE6_PODSTUDIO_GX },
492 	{ LINE6_DEVICE(0x4150),    .driver_info = LINE6_PODSTUDIO_UX1 },
493 	{ LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 },
494 	{ LINE6_DEVICE(0x4147),    .driver_info = LINE6_TONEPORT_GX },
495 	{ LINE6_DEVICE(0x4141),    .driver_info = LINE6_TONEPORT_UX1 },
496 	{ LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 },
497 	{}
498 };
499 
500 MODULE_DEVICE_TABLE(usb, toneport_id_table);
501 
502 static const struct line6_properties toneport_properties_table[] = {
503 	[LINE6_GUITARPORT] = {
504 		.id = "GuitarPort",
505 		.name = "GuitarPort",
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_GX] = {
513 		.id = "PODStudioGX",
514 		.name = "POD Studio GX",
515 		.capabilities	= LINE6_CAP_PCM,
516 		.altsetting = 2,  /* 1..4 seem to be ok */
517 		/* no control channel */
518 		.ep_audio_r = 0x82,
519 		.ep_audio_w = 0x01,
520 	},
521 	[LINE6_PODSTUDIO_UX1] = {
522 		.id = "PODStudioUX1",
523 		.name = "POD Studio UX1",
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_PODSTUDIO_UX2] = {
531 		.id = "PODStudioUX2",
532 		.name = "POD Studio UX2",
533 		.capabilities	= LINE6_CAP_PCM,
534 		.altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
535 		/* no control channel */
536 		.ep_audio_r = 0x82,
537 		.ep_audio_w = 0x01,
538 	},
539 	[LINE6_TONEPORT_GX] = {
540 		.id = "TonePortGX",
541 		.name = "TonePort GX",
542 		.capabilities	= LINE6_CAP_PCM,
543 		.altsetting = 2,  /* 1..4 seem to be ok */
544 		/* no control channel */
545 		.ep_audio_r = 0x82,
546 		.ep_audio_w = 0x01,
547 	},
548 	[LINE6_TONEPORT_UX1] = {
549 		.id = "TonePortUX1",
550 		.name = "TonePort UX1",
551 		.capabilities	= LINE6_CAP_PCM,
552 		.altsetting = 2,  /* 1..4 seem to be ok */
553 		/* no control channel */
554 		.ep_audio_r = 0x82,
555 		.ep_audio_w = 0x01,
556 	},
557 	[LINE6_TONEPORT_UX2] = {
558 		.id = "TonePortUX2",
559 		.name = "TonePort UX2",
560 		.capabilities	= LINE6_CAP_PCM,
561 		.altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
562 		/* no control channel */
563 		.ep_audio_r = 0x82,
564 		.ep_audio_w = 0x01,
565 	},
566 };
567 
568 /*
569 	Probe USB device.
570 */
571 static int toneport_probe(struct usb_interface *interface,
572 			  const struct usb_device_id *id)
573 {
574 	return line6_probe(interface, id, "Line6-TonePort",
575 			   &toneport_properties_table[id->driver_info],
576 			   toneport_init, sizeof(struct usb_line6_toneport));
577 }
578 
579 static struct usb_driver toneport_driver = {
580 	.name = KBUILD_MODNAME,
581 	.probe = toneport_probe,
582 	.disconnect = line6_disconnect,
583 #ifdef CONFIG_PM
584 	.suspend = line6_suspend,
585 	.resume = line6_resume,
586 	.reset_resume = toneport_reset_resume,
587 #endif
588 	.id_table = toneport_id_table,
589 };
590 
591 module_usb_driver(toneport_driver);
592 
593 MODULE_DESCRIPTION("TonePort USB driver");
594 MODULE_LICENSE("GPL");
595