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