xref: /openbmc/linux/sound/usb/line6/podhd.c (revision 217188d9)
1 /*
2  * Line 6 Pod HD
3  *
4  * Copyright (C) 2011 Stefan Hajnoczi <stefanha@gmail.com>
5  * Copyright (C) 2015 Andrej Krutak <dev@andree.sk>
6  * Copyright (C) 2017 Hans P. Moller <hmoller@uc.cl>
7  *
8  *	This program is free software; you can redistribute it and/or
9  *	modify it under the terms of the GNU General Public License as
10  *	published by the Free Software Foundation, version 2.
11  *
12  */
13 
14 #include <linux/usb.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 
20 #include "driver.h"
21 #include "pcm.h"
22 
23 #define PODHD_STARTUP_DELAY 500
24 
25 /*
26  * Stages of POD startup procedure
27  */
28 enum {
29 	PODHD_STARTUP_INIT = 1,
30 	PODHD_STARTUP_SCHEDULE_WORKQUEUE,
31 	PODHD_STARTUP_SETUP,
32 	PODHD_STARTUP_LAST = PODHD_STARTUP_SETUP - 1
33 };
34 
35 enum {
36 	LINE6_PODHD300,
37 	LINE6_PODHD400,
38 	LINE6_PODHD500_0,
39 	LINE6_PODHD500_1,
40 	LINE6_PODX3,
41 	LINE6_PODX3LIVE,
42 	LINE6_PODHD500X,
43 	LINE6_PODHDDESKTOP
44 };
45 
46 struct usb_line6_podhd {
47 	/* Generic Line 6 USB data */
48 	struct usb_line6 line6;
49 
50 	/* Timer for device initialization */
51 	struct timer_list startup_timer;
52 
53 	/* Work handler for device initialization */
54 	struct work_struct startup_work;
55 
56 	/* Current progress in startup procedure */
57 	int startup_progress;
58 
59 	/* Serial number of device */
60 	u32 serial_number;
61 
62 	/* Firmware version */
63 	int firmware_version;
64 };
65 
66 static struct snd_ratden podhd_ratden = {
67 	.num_min = 48000,
68 	.num_max = 48000,
69 	.num_step = 1,
70 	.den = 1,
71 };
72 
73 static struct line6_pcm_properties podhd_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_S24_3LE,
82 				  .rates = SNDRV_PCM_RATE_48000,
83 				  .rate_min = 48000,
84 				  .rate_max = 48000,
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_S24_3LE,
99 				 .rates = SNDRV_PCM_RATE_48000,
100 				 .rate_min = 48000,
101 				 .rate_max = 48000,
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 = &podhd_ratden},
112 	.bytes_per_channel = 3 /* SNDRV_PCM_FMTBIT_S24_3LE */
113 };
114 
115 static struct line6_pcm_properties podx3_pcm_properties = {
116 	.playback_hw = {
117 				  .info = (SNDRV_PCM_INFO_MMAP |
118 					   SNDRV_PCM_INFO_INTERLEAVED |
119 					   SNDRV_PCM_INFO_BLOCK_TRANSFER |
120 					   SNDRV_PCM_INFO_MMAP_VALID |
121 					   SNDRV_PCM_INFO_PAUSE |
122 					   SNDRV_PCM_INFO_SYNC_START),
123 				  .formats = SNDRV_PCM_FMTBIT_S24_3LE,
124 				  .rates = SNDRV_PCM_RATE_48000,
125 				  .rate_min = 48000,
126 				  .rate_max = 48000,
127 				  .channels_min = 2,
128 				  .channels_max = 2,
129 				  .buffer_bytes_max = 60000,
130 				  .period_bytes_min = 64,
131 				  .period_bytes_max = 8192,
132 				  .periods_min = 1,
133 				  .periods_max = 1024},
134 	.capture_hw = {
135 				 .info = (SNDRV_PCM_INFO_MMAP |
136 					  SNDRV_PCM_INFO_INTERLEAVED |
137 					  SNDRV_PCM_INFO_BLOCK_TRANSFER |
138 					  SNDRV_PCM_INFO_MMAP_VALID |
139 					  SNDRV_PCM_INFO_SYNC_START),
140 				 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
141 				 .rates = SNDRV_PCM_RATE_48000,
142 				 .rate_min = 48000,
143 				 .rate_max = 48000,
144 				 /* 1+2: Main signal (out), 3+4: Tone 1,
145 				  * 5+6: Tone 2, 7+8: raw
146 				  */
147 				 .channels_min = 8,
148 				 .channels_max = 8,
149 				 .buffer_bytes_max = 60000,
150 				 .period_bytes_min = 64,
151 				 .period_bytes_max = 8192,
152 				 .periods_min = 1,
153 				 .periods_max = 1024},
154 	.rates = {
155 			    .nrats = 1,
156 			    .rats = &podhd_ratden},
157 	.bytes_per_channel = 3 /* SNDRV_PCM_FMTBIT_S24_3LE */
158 };
159 static struct usb_driver podhd_driver;
160 
161 static void podhd_startup_start_workqueue(struct timer_list *t);
162 static void podhd_startup_workqueue(struct work_struct *work);
163 static int podhd_startup_finalize(struct usb_line6_podhd *pod);
164 
165 static ssize_t serial_number_show(struct device *dev,
166 				  struct device_attribute *attr, char *buf)
167 {
168 	struct snd_card *card = dev_to_snd_card(dev);
169 	struct usb_line6_podhd *pod = card->private_data;
170 
171 	return sprintf(buf, "%u\n", pod->serial_number);
172 }
173 
174 static ssize_t firmware_version_show(struct device *dev,
175 				     struct device_attribute *attr, char *buf)
176 {
177 	struct snd_card *card = dev_to_snd_card(dev);
178 	struct usb_line6_podhd *pod = card->private_data;
179 
180 	return sprintf(buf, "%06x\n", pod->firmware_version);
181 }
182 
183 static DEVICE_ATTR_RO(firmware_version);
184 static DEVICE_ATTR_RO(serial_number);
185 
186 static struct attribute *podhd_dev_attrs[] = {
187 	&dev_attr_firmware_version.attr,
188 	&dev_attr_serial_number.attr,
189 	NULL
190 };
191 
192 static const struct attribute_group podhd_dev_attr_group = {
193 	.name = "podhd",
194 	.attrs = podhd_dev_attrs,
195 };
196 
197 /*
198  * POD X3 startup procedure.
199  *
200  * May be compatible with other POD HD's, since it's also similar to the
201  * previous POD setup. In any case, it doesn't seem to be required for the
202  * audio nor bulk interfaces to work.
203  */
204 
205 static void podhd_startup(struct usb_line6_podhd *pod)
206 {
207 	CHECK_STARTUP_PROGRESS(pod->startup_progress, PODHD_STARTUP_INIT);
208 
209 	/* delay startup procedure: */
210 	line6_start_timer(&pod->startup_timer, PODHD_STARTUP_DELAY,
211 		podhd_startup_start_workqueue);
212 }
213 
214 static void podhd_startup_start_workqueue(struct timer_list *t)
215 {
216 	struct usb_line6_podhd *pod = from_timer(pod, t, startup_timer);
217 
218 	CHECK_STARTUP_PROGRESS(pod->startup_progress,
219 		PODHD_STARTUP_SCHEDULE_WORKQUEUE);
220 
221 	/* schedule work for global work queue: */
222 	schedule_work(&pod->startup_work);
223 }
224 
225 static int podhd_dev_start(struct usb_line6_podhd *pod)
226 {
227 	int ret;
228 	u8 init_bytes[8];
229 	int i;
230 	struct usb_device *usbdev = pod->line6.usbdev;
231 
232 	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0),
233 					0x67, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
234 					0x11, 0,
235 					NULL, 0, LINE6_TIMEOUT * HZ);
236 	if (ret < 0) {
237 		dev_err(pod->line6.ifcdev, "read request failed (error %d)\n", ret);
238 		return ret;
239 	}
240 
241 	/* NOTE: looks like some kind of ping message */
242 	ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
243 					USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
244 					0x11, 0x0,
245 					&init_bytes, 3, LINE6_TIMEOUT * HZ);
246 	if (ret < 0) {
247 		dev_err(pod->line6.ifcdev,
248 			"receive length failed (error %d)\n", ret);
249 		return ret;
250 	}
251 
252 	pod->firmware_version =
253 		(init_bytes[0] << 16) | (init_bytes[1] << 8) | (init_bytes[2] << 0);
254 
255 	for (i = 0; i <= 16; i++) {
256 		ret = line6_read_data(&pod->line6, 0xf000 + 0x08 * i, init_bytes, 8);
257 		if (ret < 0)
258 			return ret;
259 	}
260 
261 	ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0),
262 					USB_REQ_SET_FEATURE,
263 					USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_DIR_OUT,
264 					1, 0,
265 					NULL, 0, LINE6_TIMEOUT * HZ);
266 	if (ret < 0)
267 		return ret;
268 
269 	return 0;
270 }
271 
272 static void podhd_startup_workqueue(struct work_struct *work)
273 {
274 	struct usb_line6_podhd *pod =
275 	    container_of(work, struct usb_line6_podhd, startup_work);
276 
277 	CHECK_STARTUP_PROGRESS(pod->startup_progress, PODHD_STARTUP_SETUP);
278 
279 	podhd_dev_start(pod);
280 	line6_read_serial_number(&pod->line6, &pod->serial_number);
281 
282 	podhd_startup_finalize(pod);
283 }
284 
285 static int podhd_startup_finalize(struct usb_line6_podhd *pod)
286 {
287 	struct usb_line6 *line6 = &pod->line6;
288 
289 	/* ALSA audio interface: */
290 	return snd_card_register(line6->card);
291 }
292 
293 static void podhd_disconnect(struct usb_line6 *line6)
294 {
295 	struct usb_line6_podhd *pod = (struct usb_line6_podhd *)line6;
296 
297 	if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL_INFO) {
298 		struct usb_interface *intf;
299 
300 		del_timer_sync(&pod->startup_timer);
301 		cancel_work_sync(&pod->startup_work);
302 
303 		intf = usb_ifnum_to_if(line6->usbdev,
304 					pod->line6.properties->ctrl_if);
305 		if (intf)
306 			usb_driver_release_interface(&podhd_driver, intf);
307 	}
308 }
309 
310 /*
311 	Try to init POD HD device.
312 */
313 static int podhd_init(struct usb_line6 *line6,
314 		      const struct usb_device_id *id)
315 {
316 	int err;
317 	struct usb_line6_podhd *pod = (struct usb_line6_podhd *) line6;
318 	struct usb_interface *intf;
319 
320 	line6->disconnect = podhd_disconnect;
321 
322 	timer_setup(&pod->startup_timer, NULL, 0);
323 	INIT_WORK(&pod->startup_work, podhd_startup_workqueue);
324 
325 	if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL) {
326 		/* claim the data interface */
327 		intf = usb_ifnum_to_if(line6->usbdev,
328 					pod->line6.properties->ctrl_if);
329 		if (!intf) {
330 			dev_err(pod->line6.ifcdev, "interface %d not found\n",
331 				pod->line6.properties->ctrl_if);
332 			return -ENODEV;
333 		}
334 
335 		err = usb_driver_claim_interface(&podhd_driver, intf, NULL);
336 		if (err != 0) {
337 			dev_err(pod->line6.ifcdev, "can't claim interface %d, error %d\n",
338 				pod->line6.properties->ctrl_if, err);
339 			return err;
340 		}
341 	}
342 
343 	if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL_INFO) {
344 		/* create sysfs entries: */
345 		err = snd_card_add_dev_attr(line6->card, &podhd_dev_attr_group);
346 		if (err < 0)
347 			return err;
348 	}
349 
350 	if (pod->line6.properties->capabilities & LINE6_CAP_PCM) {
351 		/* initialize PCM subsystem: */
352 		err = line6_init_pcm(line6,
353 			(id->driver_info == LINE6_PODX3 ||
354 			id->driver_info == LINE6_PODX3LIVE) ? &podx3_pcm_properties :
355 			&podhd_pcm_properties);
356 		if (err < 0)
357 			return err;
358 	}
359 
360 	if (!(pod->line6.properties->capabilities & LINE6_CAP_CONTROL_INFO)) {
361 		/* register USB audio system directly */
362 		return podhd_startup_finalize(pod);
363 	}
364 
365 	/* init device and delay registering */
366 	podhd_startup(pod);
367 	return 0;
368 }
369 
370 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
371 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
372 
373 /* table of devices that work with this driver */
374 static const struct usb_device_id podhd_id_table[] = {
375 	/* TODO: no need to alloc data interfaces when only audio is used */
376 	{ LINE6_DEVICE(0x5057),    .driver_info = LINE6_PODHD300 },
377 	{ LINE6_DEVICE(0x5058),    .driver_info = LINE6_PODHD400 },
378 	{ LINE6_IF_NUM(0x414D, 0), .driver_info = LINE6_PODHD500_0 },
379 	{ LINE6_IF_NUM(0x414D, 1), .driver_info = LINE6_PODHD500_1 },
380 	{ LINE6_IF_NUM(0x414A, 0), .driver_info = LINE6_PODX3 },
381 	{ LINE6_IF_NUM(0x414B, 0), .driver_info = LINE6_PODX3LIVE },
382 	{ LINE6_IF_NUM(0x4159, 0), .driver_info = LINE6_PODHD500X },
383 	{ LINE6_IF_NUM(0x4156, 0), .driver_info = LINE6_PODHDDESKTOP },
384 	{}
385 };
386 
387 MODULE_DEVICE_TABLE(usb, podhd_id_table);
388 
389 static const struct line6_properties podhd_properties_table[] = {
390 	[LINE6_PODHD300] = {
391 		.id = "PODHD300",
392 		.name = "POD HD300",
393 		.capabilities	= LINE6_CAP_PCM
394 				| LINE6_CAP_HWMON,
395 		.altsetting = 5,
396 		.ep_ctrl_r = 0x84,
397 		.ep_ctrl_w = 0x03,
398 		.ep_audio_r = 0x82,
399 		.ep_audio_w = 0x01,
400 	},
401 	[LINE6_PODHD400] = {
402 		.id = "PODHD400",
403 		.name = "POD HD400",
404 		.capabilities	= LINE6_CAP_PCM
405 				| LINE6_CAP_HWMON,
406 		.altsetting = 5,
407 		.ep_ctrl_r = 0x84,
408 		.ep_ctrl_w = 0x03,
409 		.ep_audio_r = 0x82,
410 		.ep_audio_w = 0x01,
411 	},
412 	[LINE6_PODHD500_0] = {
413 		.id = "PODHD500",
414 		.name = "POD HD500",
415 		.capabilities	= LINE6_CAP_PCM
416 				| LINE6_CAP_HWMON,
417 		.altsetting = 1,
418 		.ep_ctrl_r = 0x81,
419 		.ep_ctrl_w = 0x01,
420 		.ep_audio_r = 0x86,
421 		.ep_audio_w = 0x02,
422 	},
423 	[LINE6_PODHD500_1] = {
424 		.id = "PODHD500",
425 		.name = "POD HD500",
426 		.capabilities	= LINE6_CAP_PCM
427 				| LINE6_CAP_HWMON,
428 		.altsetting = 1,
429 		.ep_ctrl_r = 0x81,
430 		.ep_ctrl_w = 0x01,
431 		.ep_audio_r = 0x86,
432 		.ep_audio_w = 0x02,
433 	},
434 	[LINE6_PODX3] = {
435 		.id = "PODX3",
436 		.name = "POD X3",
437 		.capabilities	= LINE6_CAP_CONTROL | LINE6_CAP_CONTROL_INFO
438 				| LINE6_CAP_PCM | LINE6_CAP_HWMON | LINE6_CAP_IN_NEEDS_OUT,
439 		.altsetting = 1,
440 		.ep_ctrl_r = 0x81,
441 		.ep_ctrl_w = 0x01,
442 		.ctrl_if = 1,
443 		.ep_audio_r = 0x86,
444 		.ep_audio_w = 0x02,
445 	},
446 	[LINE6_PODX3LIVE] = {
447 		.id = "PODX3LIVE",
448 		.name = "POD X3 LIVE",
449 		.capabilities	= LINE6_CAP_CONTROL | LINE6_CAP_CONTROL_INFO
450 				| LINE6_CAP_PCM | LINE6_CAP_HWMON | LINE6_CAP_IN_NEEDS_OUT,
451 		.altsetting = 1,
452 		.ep_ctrl_r = 0x81,
453 		.ep_ctrl_w = 0x01,
454 		.ctrl_if = 1,
455 		.ep_audio_r = 0x86,
456 		.ep_audio_w = 0x02,
457 	},
458 	[LINE6_PODHD500X] = {
459 		.id = "PODHD500X",
460 		.name = "POD HD500X",
461 		.capabilities	= LINE6_CAP_CONTROL
462 				| LINE6_CAP_PCM | LINE6_CAP_HWMON,
463 		.altsetting = 1,
464 		.ep_ctrl_r = 0x81,
465 		.ep_ctrl_w = 0x01,
466 		.ctrl_if = 1,
467 		.ep_audio_r = 0x86,
468 		.ep_audio_w = 0x02,
469 	},
470 	[LINE6_PODHDDESKTOP] = {
471 		.id = "PODHDDESKTOP",
472 		.name = "POD HDDESKTOP",
473 		.capabilities    = LINE6_CAP_CONTROL
474 			| LINE6_CAP_PCM | LINE6_CAP_HWMON,
475 		.altsetting = 1,
476 		.ep_ctrl_r = 0x81,
477 		.ep_ctrl_w = 0x01,
478 		.ctrl_if = 1,
479 		.ep_audio_r = 0x86,
480 		.ep_audio_w = 0x02,
481 	},
482 };
483 
484 /*
485 	Probe USB device.
486 */
487 static int podhd_probe(struct usb_interface *interface,
488 		       const struct usb_device_id *id)
489 {
490 	return line6_probe(interface, id, "Line6-PODHD",
491 			   &podhd_properties_table[id->driver_info],
492 			   podhd_init, sizeof(struct usb_line6_podhd));
493 }
494 
495 static struct usb_driver podhd_driver = {
496 	.name = KBUILD_MODNAME,
497 	.probe = podhd_probe,
498 	.disconnect = line6_disconnect,
499 #ifdef CONFIG_PM
500 	.suspend = line6_suspend,
501 	.resume = line6_resume,
502 	.reset_resume = line6_resume,
503 #endif
504 	.id_table = podhd_id_table,
505 };
506 
507 module_usb_driver(podhd_driver);
508 
509 MODULE_DESCRIPTION("Line 6 PODHD USB driver");
510 MODULE_LICENSE("GPL");
511