xref: /openbmc/linux/sound/usb/line6/toneport.c (revision b8d312aa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Line 6 Linux USB driver
4  *
5  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
6  *                         Emil Myhrman (emil.myhrman@gmail.com)
7  */
8 
9 #include <linux/wait.h>
10 #include <linux/usb.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/leds.h>
14 #include <sound/core.h>
15 #include <sound/control.h>
16 
17 #include "capture.h"
18 #include "driver.h"
19 #include "playback.h"
20 
21 enum line6_device_type {
22 	LINE6_GUITARPORT,
23 	LINE6_PODSTUDIO_GX,
24 	LINE6_PODSTUDIO_UX1,
25 	LINE6_PODSTUDIO_UX2,
26 	LINE6_TONEPORT_GX,
27 	LINE6_TONEPORT_UX1,
28 	LINE6_TONEPORT_UX2,
29 };
30 
31 struct usb_line6_toneport;
32 
33 struct toneport_led {
34 	struct led_classdev dev;
35 	char name[64];
36 	struct usb_line6_toneport *toneport;
37 	bool registered;
38 };
39 
40 struct usb_line6_toneport {
41 	/* Generic Line 6 USB data */
42 	struct usb_line6 line6;
43 
44 	/* Source selector */
45 	int source;
46 
47 	/* Serial number of device */
48 	u32 serial_number;
49 
50 	/* Firmware version (x 100) */
51 	u8 firmware_version;
52 
53 	/* Device type */
54 	enum line6_device_type type;
55 
56 	/* LED instances */
57 	struct toneport_led leds[2];
58 };
59 
60 #define line6_to_toneport(x) container_of(x, struct usb_line6_toneport, line6)
61 
62 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
63 
64 #define TONEPORT_PCM_DELAY 1
65 
66 static struct snd_ratden toneport_ratden = {
67 	.num_min = 44100,
68 	.num_max = 44100,
69 	.num_step = 1,
70 	.den = 1
71 };
72 
73 static struct line6_pcm_properties toneport_pcm_properties = {
74 	.playback_hw = {
75 				  .info = (SNDRV_PCM_INFO_MMAP |
76 					   SNDRV_PCM_INFO_INTERLEAVED |
77 					   SNDRV_PCM_INFO_BLOCK_TRANSFER |
78 					   SNDRV_PCM_INFO_MMAP_VALID |
79 					   SNDRV_PCM_INFO_PAUSE |
80 					   SNDRV_PCM_INFO_SYNC_START),
81 				  .formats = SNDRV_PCM_FMTBIT_S16_LE,
82 				  .rates = SNDRV_PCM_RATE_KNOT,
83 				  .rate_min = 44100,
84 				  .rate_max = 44100,
85 				  .channels_min = 2,
86 				  .channels_max = 2,
87 				  .buffer_bytes_max = 60000,
88 				  .period_bytes_min = 64,
89 				  .period_bytes_max = 8192,
90 				  .periods_min = 1,
91 				  .periods_max = 1024},
92 	.capture_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_SYNC_START),
98 				 .formats = SNDRV_PCM_FMTBIT_S16_LE,
99 				 .rates = SNDRV_PCM_RATE_KNOT,
100 				 .rate_min = 44100,
101 				 .rate_max = 44100,
102 				 .channels_min = 2,
103 				 .channels_max = 2,
104 				 .buffer_bytes_max = 60000,
105 				 .period_bytes_min = 64,
106 				 .period_bytes_max = 8192,
107 				 .periods_min = 1,
108 				 .periods_max = 1024},
109 	.rates = {
110 			    .nrats = 1,
111 			    .rats = &toneport_ratden},
112 	.bytes_per_channel = 2
113 };
114 
115 static const struct {
116 	const char *name;
117 	int code;
118 } toneport_source_info[] = {
119 	{"Microphone", 0x0a01},
120 	{"Line", 0x0801},
121 	{"Instrument", 0x0b01},
122 	{"Inst & Mic", 0x0901}
123 };
124 
125 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
126 {
127 	int ret;
128 
129 	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
130 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
131 			      cmd1, cmd2, NULL, 0, LINE6_TIMEOUT * HZ);
132 
133 	if (ret < 0) {
134 		dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
135 		return ret;
136 	}
137 
138 	return 0;
139 }
140 
141 /* monitor info callback */
142 static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
143 				     struct snd_ctl_elem_info *uinfo)
144 {
145 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
146 	uinfo->count = 1;
147 	uinfo->value.integer.min = 0;
148 	uinfo->value.integer.max = 256;
149 	return 0;
150 }
151 
152 /* monitor get callback */
153 static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
154 				    struct snd_ctl_elem_value *ucontrol)
155 {
156 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
157 
158 	ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
159 	return 0;
160 }
161 
162 /* monitor put callback */
163 static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
164 				    struct snd_ctl_elem_value *ucontrol)
165 {
166 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
167 	int err;
168 
169 	if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
170 		return 0;
171 
172 	line6pcm->volume_monitor = ucontrol->value.integer.value[0];
173 
174 	if (line6pcm->volume_monitor > 0) {
175 		err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR, true);
176 		if (err < 0) {
177 			line6pcm->volume_monitor = 0;
178 			line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
179 			return err;
180 		}
181 	} else {
182 		line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
183 	}
184 
185 	return 1;
186 }
187 
188 /* source info callback */
189 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
190 				    struct snd_ctl_elem_info *uinfo)
191 {
192 	const int size = ARRAY_SIZE(toneport_source_info);
193 
194 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
195 	uinfo->count = 1;
196 	uinfo->value.enumerated.items = size;
197 
198 	if (uinfo->value.enumerated.item >= size)
199 		uinfo->value.enumerated.item = size - 1;
200 
201 	strcpy(uinfo->value.enumerated.name,
202 	       toneport_source_info[uinfo->value.enumerated.item].name);
203 
204 	return 0;
205 }
206 
207 /* source get callback */
208 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
209 				   struct snd_ctl_elem_value *ucontrol)
210 {
211 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
212 	struct usb_line6_toneport *toneport = line6_to_toneport(line6pcm->line6);
213 
214 	ucontrol->value.enumerated.item[0] = toneport->source;
215 	return 0;
216 }
217 
218 /* source put callback */
219 static int snd_toneport_source_put(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 = line6_to_toneport(line6pcm->line6);
224 	unsigned int source;
225 
226 	source = ucontrol->value.enumerated.item[0];
227 	if (source >= ARRAY_SIZE(toneport_source_info))
228 		return -EINVAL;
229 	if (source == toneport->source)
230 		return 0;
231 
232 	toneport->source = source;
233 	toneport_send_cmd(toneport->line6.usbdev,
234 			  toneport_source_info[source].code, 0x0000);
235 	return 1;
236 }
237 
238 static void toneport_startup(struct usb_line6 *line6)
239 {
240 	line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR, true);
241 }
242 
243 /* control definition */
244 static const struct snd_kcontrol_new toneport_control_monitor = {
245 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
246 	.name = "Monitor Playback Volume",
247 	.index = 0,
248 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
249 	.info = snd_toneport_monitor_info,
250 	.get = snd_toneport_monitor_get,
251 	.put = snd_toneport_monitor_put
252 };
253 
254 /* source selector definition */
255 static const struct snd_kcontrol_new toneport_control_source = {
256 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
257 	.name = "PCM Capture Source",
258 	.index = 0,
259 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
260 	.info = snd_toneport_source_info,
261 	.get = snd_toneport_source_get,
262 	.put = snd_toneport_source_put
263 };
264 
265 /*
266 	For the led on Guitarport.
267 	Brightness goes from 0x00 to 0x26. Set a value above this to have led
268 	blink.
269 	(void cmd_0x02(byte red, byte green)
270 */
271 
272 static bool toneport_has_led(struct usb_line6_toneport *toneport)
273 {
274 	switch (toneport->type) {
275 	case LINE6_GUITARPORT:
276 	case LINE6_TONEPORT_GX:
277 	/* add your device here if you are missing support for the LEDs */
278 		return true;
279 
280 	default:
281 		return false;
282 	}
283 }
284 
285 static const char * const toneport_led_colors[2] = { "red", "green" };
286 static const int toneport_led_init_vals[2] = { 0x00, 0x26 };
287 
288 static void toneport_update_led(struct usb_line6_toneport *toneport)
289 {
290 	toneport_send_cmd(toneport->line6.usbdev,
291 			  (toneport->leds[0].dev.brightness << 8) | 0x0002,
292 			  toneport->leds[1].dev.brightness);
293 }
294 
295 static void toneport_led_brightness_set(struct led_classdev *led_cdev,
296 					enum led_brightness brightness)
297 {
298 	struct toneport_led *leds =
299 		container_of(led_cdev, struct toneport_led, dev);
300 	toneport_update_led(leds->toneport);
301 }
302 
303 static int toneport_init_leds(struct usb_line6_toneport *toneport)
304 {
305 	struct device *dev = &toneport->line6.usbdev->dev;
306 	int i, err;
307 
308 	for (i = 0; i < 2; i++) {
309 		struct toneport_led *led = &toneport->leds[i];
310 		struct led_classdev *leddev = &led->dev;
311 
312 		led->toneport = toneport;
313 		snprintf(led->name, sizeof(led->name), "%s::%s",
314 			 dev_name(dev), toneport_led_colors[i]);
315 		leddev->name = led->name;
316 		leddev->brightness = toneport_led_init_vals[i];
317 		leddev->max_brightness = 0x26;
318 		leddev->brightness_set = toneport_led_brightness_set;
319 		err = led_classdev_register(dev, leddev);
320 		if (err)
321 			return err;
322 		led->registered = true;
323 	}
324 
325 	return 0;
326 }
327 
328 static void toneport_remove_leds(struct usb_line6_toneport *toneport)
329 {
330 	struct toneport_led *led;
331 	int i;
332 
333 	for (i = 0; i < 2; i++) {
334 		led = &toneport->leds[i];
335 		if (!led->registered)
336 			break;
337 		led_classdev_unregister(&led->dev);
338 		led->registered = false;
339 	}
340 }
341 
342 static bool toneport_has_source_select(struct usb_line6_toneport *toneport)
343 {
344 	switch (toneport->type) {
345 	case LINE6_TONEPORT_UX1:
346 	case LINE6_TONEPORT_UX2:
347 	case LINE6_PODSTUDIO_UX1:
348 	case LINE6_PODSTUDIO_UX2:
349 		return true;
350 
351 	default:
352 		return false;
353 	}
354 }
355 
356 /*
357 	Setup Toneport device.
358 */
359 static int toneport_setup(struct usb_line6_toneport *toneport)
360 {
361 	u32 *ticks;
362 	struct usb_line6 *line6 = &toneport->line6;
363 	struct usb_device *usbdev = line6->usbdev;
364 
365 	ticks = kmalloc(sizeof(*ticks), GFP_KERNEL);
366 	if (!ticks)
367 		return -ENOMEM;
368 
369 	/* sync time on device with host: */
370 	/* note: 32-bit timestamps overflow in year 2106 */
371 	*ticks = (u32)ktime_get_real_seconds();
372 	line6_write_data(line6, 0x80c6, ticks, 4);
373 	kfree(ticks);
374 
375 	/* enable device: */
376 	toneport_send_cmd(usbdev, 0x0301, 0x0000);
377 
378 	/* initialize source select: */
379 	if (toneport_has_source_select(toneport))
380 		toneport_send_cmd(usbdev,
381 				  toneport_source_info[toneport->source].code,
382 				  0x0000);
383 
384 	if (toneport_has_led(toneport))
385 		toneport_update_led(toneport);
386 
387 	schedule_delayed_work(&toneport->line6.startup_work,
388 			      msecs_to_jiffies(TONEPORT_PCM_DELAY * 1000));
389 	return 0;
390 }
391 
392 /*
393 	Toneport device disconnected.
394 */
395 static void line6_toneport_disconnect(struct usb_line6 *line6)
396 {
397 	struct usb_line6_toneport *toneport = line6_to_toneport(line6);
398 
399 	if (toneport_has_led(toneport))
400 		toneport_remove_leds(toneport);
401 }
402 
403 
404 /*
405 	 Try to init Toneport device.
406 */
407 static int toneport_init(struct usb_line6 *line6,
408 			 const struct usb_device_id *id)
409 {
410 	int err;
411 	struct usb_line6_toneport *toneport = line6_to_toneport(line6);
412 
413 	toneport->type = id->driver_info;
414 
415 	line6->disconnect = line6_toneport_disconnect;
416 	line6->startup = toneport_startup;
417 
418 	/* initialize PCM subsystem: */
419 	err = line6_init_pcm(line6, &toneport_pcm_properties);
420 	if (err < 0)
421 		return err;
422 
423 	/* register monitor control: */
424 	err = snd_ctl_add(line6->card,
425 			  snd_ctl_new1(&toneport_control_monitor,
426 				       line6->line6pcm));
427 	if (err < 0)
428 		return err;
429 
430 	/* register source select control: */
431 	if (toneport_has_source_select(toneport)) {
432 		err =
433 		    snd_ctl_add(line6->card,
434 				snd_ctl_new1(&toneport_control_source,
435 					     line6->line6pcm));
436 		if (err < 0)
437 			return err;
438 	}
439 
440 	line6_read_serial_number(line6, &toneport->serial_number);
441 	line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
442 
443 	if (toneport_has_led(toneport)) {
444 		err = toneport_init_leds(toneport);
445 		if (err < 0)
446 			return err;
447 	}
448 
449 	err = toneport_setup(toneport);
450 	if (err)
451 		return err;
452 
453 	/* register audio system: */
454 	return snd_card_register(line6->card);
455 }
456 
457 #ifdef CONFIG_PM
458 /*
459 	Resume Toneport device after reset.
460 */
461 static int toneport_reset_resume(struct usb_interface *interface)
462 {
463 	int err;
464 
465 	err = toneport_setup(usb_get_intfdata(interface));
466 	if (err)
467 		return err;
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, "Line6-TonePort",
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