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