xref: /linux/sound/usb/line6/toneport.c (revision 371bb62158d53c1fc33e2fb9b6aeb9522caf6cf4)
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 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
61 
62 #define TONEPORT_PCM_DELAY 1
63 
64 static struct snd_ratden toneport_ratden = {
65 	.num_min = 44100,
66 	.num_max = 44100,
67 	.num_step = 1,
68 	.den = 1
69 };
70 
71 static struct line6_pcm_properties toneport_pcm_properties = {
72 	.playback_hw = {
73 				  .info = (SNDRV_PCM_INFO_MMAP |
74 					   SNDRV_PCM_INFO_INTERLEAVED |
75 					   SNDRV_PCM_INFO_BLOCK_TRANSFER |
76 					   SNDRV_PCM_INFO_MMAP_VALID |
77 					   SNDRV_PCM_INFO_PAUSE |
78 					   SNDRV_PCM_INFO_SYNC_START),
79 				  .formats = SNDRV_PCM_FMTBIT_S16_LE,
80 				  .rates = SNDRV_PCM_RATE_KNOT,
81 				  .rate_min = 44100,
82 				  .rate_max = 44100,
83 				  .channels_min = 2,
84 				  .channels_max = 2,
85 				  .buffer_bytes_max = 60000,
86 				  .period_bytes_min = 64,
87 				  .period_bytes_max = 8192,
88 				  .periods_min = 1,
89 				  .periods_max = 1024},
90 	.capture_hw = {
91 				 .info = (SNDRV_PCM_INFO_MMAP |
92 					  SNDRV_PCM_INFO_INTERLEAVED |
93 					  SNDRV_PCM_INFO_BLOCK_TRANSFER |
94 					  SNDRV_PCM_INFO_MMAP_VALID |
95 					  SNDRV_PCM_INFO_SYNC_START),
96 				 .formats = SNDRV_PCM_FMTBIT_S16_LE,
97 				 .rates = SNDRV_PCM_RATE_KNOT,
98 				 .rate_min = 44100,
99 				 .rate_max = 44100,
100 				 .channels_min = 2,
101 				 .channels_max = 2,
102 				 .buffer_bytes_max = 60000,
103 				 .period_bytes_min = 64,
104 				 .period_bytes_max = 8192,
105 				 .periods_min = 1,
106 				 .periods_max = 1024},
107 	.rates = {
108 			    .nrats = 1,
109 			    .rats = &toneport_ratden},
110 	.bytes_per_channel = 2
111 };
112 
113 static const struct {
114 	const char *name;
115 	int code;
116 } toneport_source_info[] = {
117 	{"Microphone", 0x0a01},
118 	{"Line", 0x0801},
119 	{"Instrument", 0x0b01},
120 	{"Inst & Mic", 0x0901}
121 };
122 
123 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
124 {
125 	int ret;
126 
127 	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
128 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
129 			      cmd1, cmd2, NULL, 0, LINE6_TIMEOUT * HZ);
130 
131 	if (ret < 0) {
132 		dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
133 		return ret;
134 	}
135 
136 	return 0;
137 }
138 
139 /* monitor info callback */
140 static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
141 				     struct snd_ctl_elem_info *uinfo)
142 {
143 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
144 	uinfo->count = 1;
145 	uinfo->value.integer.min = 0;
146 	uinfo->value.integer.max = 256;
147 	return 0;
148 }
149 
150 /* monitor get callback */
151 static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
152 				    struct snd_ctl_elem_value *ucontrol)
153 {
154 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
155 
156 	ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
157 	return 0;
158 }
159 
160 /* monitor put callback */
161 static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
162 				    struct snd_ctl_elem_value *ucontrol)
163 {
164 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
165 	int err;
166 
167 	if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
168 		return 0;
169 
170 	line6pcm->volume_monitor = ucontrol->value.integer.value[0];
171 
172 	if (line6pcm->volume_monitor > 0) {
173 		err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR, true);
174 		if (err < 0) {
175 			line6pcm->volume_monitor = 0;
176 			line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
177 			return err;
178 		}
179 	} else {
180 		line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
181 	}
182 
183 	return 1;
184 }
185 
186 /* source info callback */
187 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
188 				    struct snd_ctl_elem_info *uinfo)
189 {
190 	const int size = ARRAY_SIZE(toneport_source_info);
191 
192 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
193 	uinfo->count = 1;
194 	uinfo->value.enumerated.items = size;
195 
196 	if (uinfo->value.enumerated.item >= size)
197 		uinfo->value.enumerated.item = size - 1;
198 
199 	strcpy(uinfo->value.enumerated.name,
200 	       toneport_source_info[uinfo->value.enumerated.item].name);
201 
202 	return 0;
203 }
204 
205 /* source get callback */
206 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
207 				   struct snd_ctl_elem_value *ucontrol)
208 {
209 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
210 	struct usb_line6_toneport *toneport =
211 	    (struct usb_line6_toneport *)line6pcm->line6;
212 	ucontrol->value.enumerated.item[0] = toneport->source;
213 	return 0;
214 }
215 
216 /* source put callback */
217 static int snd_toneport_source_put(struct snd_kcontrol *kcontrol,
218 				   struct snd_ctl_elem_value *ucontrol)
219 {
220 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
221 	struct usb_line6_toneport *toneport =
222 	    (struct usb_line6_toneport *)line6pcm->line6;
223 	unsigned int source;
224 
225 	source = ucontrol->value.enumerated.item[0];
226 	if (source >= ARRAY_SIZE(toneport_source_info))
227 		return -EINVAL;
228 	if (source == toneport->source)
229 		return 0;
230 
231 	toneport->source = source;
232 	toneport_send_cmd(toneport->line6.usbdev,
233 			  toneport_source_info[source].code, 0x0000);
234 	return 1;
235 }
236 
237 static void toneport_startup(struct usb_line6 *line6)
238 {
239 	line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR, true);
240 }
241 
242 /* control definition */
243 static const struct snd_kcontrol_new toneport_control_monitor = {
244 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
245 	.name = "Monitor Playback Volume",
246 	.index = 0,
247 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
248 	.info = snd_toneport_monitor_info,
249 	.get = snd_toneport_monitor_get,
250 	.put = snd_toneport_monitor_put
251 };
252 
253 /* source selector definition */
254 static const struct snd_kcontrol_new toneport_control_source = {
255 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
256 	.name = "PCM Capture Source",
257 	.index = 0,
258 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
259 	.info = snd_toneport_source_info,
260 	.get = snd_toneport_source_get,
261 	.put = snd_toneport_source_put
262 };
263 
264 /*
265 	For the led on Guitarport.
266 	Brightness goes from 0x00 to 0x26. Set a value above this to have led
267 	blink.
268 	(void cmd_0x02(byte red, byte green)
269 */
270 
271 static bool toneport_has_led(struct usb_line6_toneport *toneport)
272 {
273 	switch (toneport->type) {
274 	case LINE6_GUITARPORT:
275 	case LINE6_TONEPORT_GX:
276 	/* add your device here if you are missing support for the LEDs */
277 		return true;
278 
279 	default:
280 		return false;
281 	}
282 }
283 
284 static const char * const toneport_led_colors[2] = { "red", "green" };
285 static const int toneport_led_init_vals[2] = { 0x00, 0x26 };
286 
287 static void toneport_update_led(struct usb_line6_toneport *toneport)
288 {
289 	toneport_send_cmd(toneport->line6.usbdev,
290 			  (toneport->leds[0].dev.brightness << 8) | 0x0002,
291 			  toneport->leds[1].dev.brightness);
292 }
293 
294 static void toneport_led_brightness_set(struct led_classdev *led_cdev,
295 					enum led_brightness brightness)
296 {
297 	struct toneport_led *leds =
298 		container_of(led_cdev, struct toneport_led, dev);
299 	toneport_update_led(leds->toneport);
300 }
301 
302 static int toneport_init_leds(struct usb_line6_toneport *toneport)
303 {
304 	struct device *dev = &toneport->line6.usbdev->dev;
305 	int i, err;
306 
307 	for (i = 0; i < 2; i++) {
308 		struct toneport_led *led = &toneport->leds[i];
309 		struct led_classdev *leddev = &led->dev;
310 
311 		led->toneport = toneport;
312 		snprintf(led->name, sizeof(led->name), "%s::%s",
313 			 dev_name(dev), toneport_led_colors[i]);
314 		leddev->name = led->name;
315 		leddev->brightness = toneport_led_init_vals[i];
316 		leddev->max_brightness = 0x26;
317 		leddev->brightness_set = toneport_led_brightness_set;
318 		err = led_classdev_register(dev, leddev);
319 		if (err)
320 			return err;
321 		led->registered = true;
322 	}
323 
324 	return 0;
325 }
326 
327 static void toneport_remove_leds(struct usb_line6_toneport *toneport)
328 {
329 	struct toneport_led *led;
330 	int i;
331 
332 	for (i = 0; i < 2; i++) {
333 		led = &toneport->leds[i];
334 		if (!led->registered)
335 			break;
336 		led_classdev_unregister(&led->dev);
337 		led->registered = false;
338 	}
339 }
340 
341 static bool toneport_has_source_select(struct usb_line6_toneport *toneport)
342 {
343 	switch (toneport->type) {
344 	case LINE6_TONEPORT_UX1:
345 	case LINE6_TONEPORT_UX2:
346 	case LINE6_PODSTUDIO_UX1:
347 	case LINE6_PODSTUDIO_UX2:
348 		return true;
349 
350 	default:
351 		return false;
352 	}
353 }
354 
355 /*
356 	Setup Toneport device.
357 */
358 static int toneport_setup(struct usb_line6_toneport *toneport)
359 {
360 	u32 *ticks;
361 	struct usb_line6 *line6 = &toneport->line6;
362 	struct usb_device *usbdev = line6->usbdev;
363 
364 	ticks = kmalloc(sizeof(*ticks), GFP_KERNEL);
365 	if (!ticks)
366 		return -ENOMEM;
367 
368 	/* sync time on device with host: */
369 	/* note: 32-bit timestamps overflow in year 2106 */
370 	*ticks = (u32)ktime_get_real_seconds();
371 	line6_write_data(line6, 0x80c6, ticks, 4);
372 	kfree(ticks);
373 
374 	/* enable device: */
375 	toneport_send_cmd(usbdev, 0x0301, 0x0000);
376 
377 	/* initialize source select: */
378 	if (toneport_has_source_select(toneport))
379 		toneport_send_cmd(usbdev,
380 				  toneport_source_info[toneport->source].code,
381 				  0x0000);
382 
383 	if (toneport_has_led(toneport))
384 		toneport_update_led(toneport);
385 
386 	schedule_delayed_work(&toneport->line6.startup_work,
387 			      msecs_to_jiffies(TONEPORT_PCM_DELAY * 1000));
388 	return 0;
389 }
390 
391 /*
392 	Toneport device disconnected.
393 */
394 static void line6_toneport_disconnect(struct usb_line6 *line6)
395 {
396 	struct usb_line6_toneport *toneport =
397 		(struct usb_line6_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 =  (struct usb_line6_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