xref: /openbmc/linux/sound/usb/line6/pod.c (revision cddbd4f1)
1 /*
2  * Line 6 Linux USB driver
3  *
4  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License as
8  *	published by the Free Software Foundation, version 2.
9  *
10  */
11 
12 #include <linux/slab.h>
13 #include <linux/wait.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 
18 #include <sound/core.h>
19 #include <sound/control.h>
20 
21 #include "capture.h"
22 #include "driver.h"
23 #include "playback.h"
24 #include "usbdefs.h"
25 
26 /*
27 	Locate name in binary program dump
28 */
29 #define	POD_NAME_OFFSET 0
30 #define	POD_NAME_LENGTH 16
31 
32 /*
33 	Other constants
34 */
35 #define POD_CONTROL_SIZE 0x80
36 #define POD_BUFSIZE_DUMPREQ 7
37 #define POD_STARTUP_DELAY 1000
38 
39 /*
40 	Stages of POD startup procedure
41 */
42 enum {
43 	POD_STARTUP_INIT = 1,
44 	POD_STARTUP_VERSIONREQ,
45 	POD_STARTUP_WORKQUEUE,
46 	POD_STARTUP_SETUP,
47 	POD_STARTUP_LAST = POD_STARTUP_SETUP - 1
48 };
49 
50 enum {
51 	LINE6_BASSPODXT,
52 	LINE6_BASSPODXTLIVE,
53 	LINE6_BASSPODXTPRO,
54 	LINE6_POCKETPOD,
55 	LINE6_PODXT,
56 	LINE6_PODXTLIVE_POD,
57 	LINE6_PODXTPRO,
58 };
59 
60 struct usb_line6_pod {
61 	/* Generic Line 6 USB data */
62 	struct usb_line6 line6;
63 
64 	/* Instrument monitor level */
65 	int monitor_level;
66 
67 	/* Timer for device initialization */
68 	struct timer_list startup_timer;
69 
70 	/* Work handler for device initialization */
71 	struct work_struct startup_work;
72 
73 	/* Current progress in startup procedure */
74 	int startup_progress;
75 
76 	/* Serial number of device */
77 	int serial_number;
78 
79 	/* Firmware version (x 100) */
80 	int firmware_version;
81 
82 	/* Device ID */
83 	int device_id;
84 };
85 
86 #define POD_SYSEX_CODE 3
87 #define POD_BYTES_PER_FRAME 6	/* 24bit audio (stereo) */
88 
89 /* *INDENT-OFF* */
90 
91 enum {
92 	POD_SYSEX_SAVE      = 0x24,
93 	POD_SYSEX_SYSTEM    = 0x56,
94 	POD_SYSEX_SYSTEMREQ = 0x57,
95 	/* POD_SYSEX_UPDATE    = 0x6c, */  /* software update! */
96 	POD_SYSEX_STORE     = 0x71,
97 	POD_SYSEX_FINISH    = 0x72,
98 	POD_SYSEX_DUMPMEM   = 0x73,
99 	POD_SYSEX_DUMP      = 0x74,
100 	POD_SYSEX_DUMPREQ   = 0x75
101 
102 	/* dumps entire internal memory of PODxt Pro */
103 	/* POD_SYSEX_DUMPMEM2  = 0x76 */
104 };
105 
106 enum {
107 	POD_MONITOR_LEVEL  = 0x04,
108 	POD_SYSTEM_INVALID = 0x10000
109 };
110 
111 /* *INDENT-ON* */
112 
113 enum {
114 	POD_DUMP_MEMORY = 2
115 };
116 
117 enum {
118 	POD_BUSY_READ,
119 	POD_BUSY_WRITE,
120 	POD_CHANNEL_DIRTY,
121 	POD_SAVE_PRESSED,
122 	POD_BUSY_MIDISEND
123 };
124 
125 static struct snd_ratden pod_ratden = {
126 	.num_min = 78125,
127 	.num_max = 78125,
128 	.num_step = 1,
129 	.den = 2
130 };
131 
132 static struct line6_pcm_properties pod_pcm_properties = {
133 	.snd_line6_playback_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_PAUSE |
139 					   SNDRV_PCM_INFO_SYNC_START),
140 				  .formats = SNDRV_PCM_FMTBIT_S24_3LE,
141 				  .rates = SNDRV_PCM_RATE_KNOT,
142 				  .rate_min = 39062,
143 				  .rate_max = 39063,
144 				  .channels_min = 2,
145 				  .channels_max = 2,
146 				  .buffer_bytes_max = 60000,
147 				  .period_bytes_min = 64,
148 				  .period_bytes_max = 8192,
149 				  .periods_min = 1,
150 				  .periods_max = 1024},
151 	.snd_line6_capture_hw = {
152 				 .info = (SNDRV_PCM_INFO_MMAP |
153 					  SNDRV_PCM_INFO_INTERLEAVED |
154 					  SNDRV_PCM_INFO_BLOCK_TRANSFER |
155 					  SNDRV_PCM_INFO_MMAP_VALID |
156 					  SNDRV_PCM_INFO_SYNC_START),
157 				 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
158 				 .rates = SNDRV_PCM_RATE_KNOT,
159 				 .rate_min = 39062,
160 				 .rate_max = 39063,
161 				 .channels_min = 2,
162 				 .channels_max = 2,
163 				 .buffer_bytes_max = 60000,
164 				 .period_bytes_min = 64,
165 				 .period_bytes_max = 8192,
166 				 .periods_min = 1,
167 				 .periods_max = 1024},
168 	.snd_line6_rates = {
169 			    .nrats = 1,
170 			    .rats = &pod_ratden},
171 	.bytes_per_frame = POD_BYTES_PER_FRAME
172 };
173 
174 static const char pod_version_header[] = {
175 	0xf2, 0x7e, 0x7f, 0x06, 0x02
176 };
177 
178 /* forward declarations: */
179 static void pod_startup2(unsigned long data);
180 static void pod_startup3(struct usb_line6_pod *pod);
181 
182 static char *pod_alloc_sysex_buffer(struct usb_line6_pod *pod, int code,
183 				    int size)
184 {
185 	return line6_alloc_sysex_buffer(&pod->line6, POD_SYSEX_CODE, code,
186 					size);
187 }
188 
189 /*
190 	Process a completely received message.
191 */
192 static void line6_pod_process_message(struct usb_line6 *line6)
193 {
194 	struct usb_line6_pod *pod = (struct usb_line6_pod *) line6;
195 	const unsigned char *buf = pod->line6.buffer_message;
196 
197 	if (memcmp(buf, pod_version_header, sizeof(pod_version_header)) == 0) {
198 		pod->firmware_version = buf[13] * 100 + buf[14] * 10 + buf[15];
199 		pod->device_id = ((int)buf[8] << 16) | ((int)buf[9] << 8) |
200 				 (int) buf[10];
201 		pod_startup3(pod);
202 		return;
203 	}
204 
205 	/* Only look for sysex messages from this device */
206 	if (buf[0] != (LINE6_SYSEX_BEGIN | LINE6_CHANNEL_DEVICE) &&
207 	    buf[0] != (LINE6_SYSEX_BEGIN | LINE6_CHANNEL_UNKNOWN)) {
208 		return;
209 	}
210 	if (memcmp(buf + 1, line6_midi_id, sizeof(line6_midi_id)) != 0)
211 		return;
212 
213 	if (buf[5] == POD_SYSEX_SYSTEM && buf[6] == POD_MONITOR_LEVEL) {
214 		short value = ((int)buf[7] << 12) | ((int)buf[8] << 8) |
215 			      ((int)buf[9] << 4) | (int)buf[10];
216 		pod->monitor_level = value;
217 	}
218 }
219 
220 /*
221 	Send system parameter (from integer).
222 */
223 static int pod_set_system_param_int(struct usb_line6_pod *pod, int value,
224 				    int code)
225 {
226 	char *sysex;
227 	static const int size = 5;
228 
229 	sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEM, size);
230 	if (!sysex)
231 		return -ENOMEM;
232 	sysex[SYSEX_DATA_OFS] = code;
233 	sysex[SYSEX_DATA_OFS + 1] = (value >> 12) & 0x0f;
234 	sysex[SYSEX_DATA_OFS + 2] = (value >> 8) & 0x0f;
235 	sysex[SYSEX_DATA_OFS + 3] = (value >> 4) & 0x0f;
236 	sysex[SYSEX_DATA_OFS + 4] = (value) & 0x0f;
237 	line6_send_sysex_message(&pod->line6, sysex, size);
238 	kfree(sysex);
239 	return 0;
240 }
241 
242 /*
243 	"read" request on "serial_number" special file.
244 */
245 static ssize_t serial_number_show(struct device *dev,
246 				  struct device_attribute *attr, char *buf)
247 {
248 	struct usb_interface *interface = to_usb_interface(dev);
249 	struct usb_line6_pod *pod = usb_get_intfdata(interface);
250 
251 	return sprintf(buf, "%d\n", pod->serial_number);
252 }
253 
254 /*
255 	"read" request on "firmware_version" special file.
256 */
257 static ssize_t firmware_version_show(struct device *dev,
258 				     struct device_attribute *attr, char *buf)
259 {
260 	struct usb_interface *interface = to_usb_interface(dev);
261 	struct usb_line6_pod *pod = usb_get_intfdata(interface);
262 
263 	return sprintf(buf, "%d.%02d\n", pod->firmware_version / 100,
264 		       pod->firmware_version % 100);
265 }
266 
267 /*
268 	"read" request on "device_id" special file.
269 */
270 static ssize_t device_id_show(struct device *dev,
271 			      struct device_attribute *attr, char *buf)
272 {
273 	struct usb_interface *interface = to_usb_interface(dev);
274 	struct usb_line6_pod *pod = usb_get_intfdata(interface);
275 
276 	return sprintf(buf, "%d\n", pod->device_id);
277 }
278 
279 /*
280 	POD startup procedure.
281 	This is a sequence of functions with special requirements (e.g., must
282 	not run immediately after initialization, must not run in interrupt
283 	context). After the last one has finished, the device is ready to use.
284 */
285 
286 static void pod_startup1(struct usb_line6_pod *pod)
287 {
288 	CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_INIT);
289 
290 	/* delay startup procedure: */
291 	line6_start_timer(&pod->startup_timer, POD_STARTUP_DELAY, pod_startup2,
292 			  (unsigned long)pod);
293 }
294 
295 static void pod_startup2(unsigned long data)
296 {
297 	struct usb_line6_pod *pod = (struct usb_line6_pod *)data;
298 	struct usb_line6 *line6 = &pod->line6;
299 
300 	CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_VERSIONREQ);
301 
302 	/* request firmware version: */
303 	line6_version_request_async(line6);
304 }
305 
306 static void pod_startup3(struct usb_line6_pod *pod)
307 {
308 	CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_WORKQUEUE);
309 
310 	/* schedule work for global work queue: */
311 	schedule_work(&pod->startup_work);
312 }
313 
314 static void pod_startup4(struct work_struct *work)
315 {
316 	struct usb_line6_pod *pod =
317 	    container_of(work, struct usb_line6_pod, startup_work);
318 	struct usb_line6 *line6 = &pod->line6;
319 
320 	CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_SETUP);
321 
322 	/* serial number: */
323 	line6_read_serial_number(&pod->line6, &pod->serial_number);
324 
325 	/* ALSA audio interface: */
326 	snd_card_register(line6->card);
327 }
328 
329 /* POD special files: */
330 static DEVICE_ATTR_RO(device_id);
331 static DEVICE_ATTR_RO(firmware_version);
332 static DEVICE_ATTR_RO(serial_number);
333 
334 /* control info callback */
335 static int snd_pod_control_monitor_info(struct snd_kcontrol *kcontrol,
336 					struct snd_ctl_elem_info *uinfo)
337 {
338 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
339 	uinfo->count = 1;
340 	uinfo->value.integer.min = 0;
341 	uinfo->value.integer.max = 65535;
342 	return 0;
343 }
344 
345 /* control get callback */
346 static int snd_pod_control_monitor_get(struct snd_kcontrol *kcontrol,
347 				       struct snd_ctl_elem_value *ucontrol)
348 {
349 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
350 	struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
351 
352 	ucontrol->value.integer.value[0] = pod->monitor_level;
353 	return 0;
354 }
355 
356 /* control put callback */
357 static int snd_pod_control_monitor_put(struct snd_kcontrol *kcontrol,
358 				       struct snd_ctl_elem_value *ucontrol)
359 {
360 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
361 	struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
362 
363 	if (ucontrol->value.integer.value[0] == pod->monitor_level)
364 		return 0;
365 
366 	pod->monitor_level = ucontrol->value.integer.value[0];
367 	pod_set_system_param_int(pod, ucontrol->value.integer.value[0],
368 				 POD_MONITOR_LEVEL);
369 	return 1;
370 }
371 
372 /* control definition */
373 static struct snd_kcontrol_new pod_control_monitor = {
374 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
375 	.name = "Monitor Playback Volume",
376 	.index = 0,
377 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
378 	.info = snd_pod_control_monitor_info,
379 	.get = snd_pod_control_monitor_get,
380 	.put = snd_pod_control_monitor_put
381 };
382 
383 /*
384 	POD device disconnected.
385 */
386 static void line6_pod_disconnect(struct usb_line6 *line6)
387 {
388 	struct usb_line6_pod *pod = (struct usb_line6_pod *)line6;
389 	struct device *dev = line6->ifcdev;
390 
391 	/* remove sysfs entries: */
392 	device_remove_file(dev, &dev_attr_device_id);
393 	device_remove_file(dev, &dev_attr_firmware_version);
394 	device_remove_file(dev, &dev_attr_serial_number);
395 
396 	del_timer_sync(&pod->startup_timer);
397 	cancel_work_sync(&pod->startup_work);
398 }
399 
400 /*
401 	Create sysfs entries.
402 */
403 static int pod_create_files2(struct device *dev)
404 {
405 	int err;
406 
407 	err = device_create_file(dev, &dev_attr_device_id);
408 	if (err < 0)
409 		return err;
410 	err = device_create_file(dev, &dev_attr_firmware_version);
411 	if (err < 0)
412 		return err;
413 	err = device_create_file(dev, &dev_attr_serial_number);
414 	if (err < 0)
415 		return err;
416 	return 0;
417 }
418 
419 /*
420 	 Try to init POD device.
421 */
422 static int pod_init(struct usb_line6 *line6,
423 		    const struct usb_device_id *id)
424 {
425 	int err;
426 	struct usb_line6_pod *pod = (struct usb_line6_pod *) line6;
427 
428 	line6->process_message = line6_pod_process_message;
429 	line6->disconnect = line6_pod_disconnect;
430 
431 	init_timer(&pod->startup_timer);
432 	INIT_WORK(&pod->startup_work, pod_startup4);
433 
434 	/* create sysfs entries: */
435 	err = pod_create_files2(line6->ifcdev);
436 	if (err < 0)
437 		return err;
438 
439 	/* initialize MIDI subsystem: */
440 	err = line6_init_midi(line6);
441 	if (err < 0)
442 		return err;
443 
444 	/* initialize PCM subsystem: */
445 	err = line6_init_pcm(line6, &pod_pcm_properties);
446 	if (err < 0)
447 		return err;
448 
449 	/* register monitor control: */
450 	err = snd_ctl_add(line6->card,
451 			  snd_ctl_new1(&pod_control_monitor, line6->line6pcm));
452 	if (err < 0)
453 		return err;
454 
455 	/*
456 	   When the sound card is registered at this point, the PODxt Live
457 	   displays "Invalid Code Error 07", so we do it later in the event
458 	   handler.
459 	 */
460 
461 	if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL) {
462 		pod->monitor_level = POD_SYSTEM_INVALID;
463 
464 		/* initiate startup procedure: */
465 		pod_startup1(pod);
466 	}
467 
468 	return 0;
469 }
470 
471 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
472 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
473 
474 /* table of devices that work with this driver */
475 static const struct usb_device_id pod_id_table[] = {
476 	{ LINE6_DEVICE(0x4250),    .driver_info = LINE6_BASSPODXT },
477 	{ LINE6_DEVICE(0x4642),    .driver_info = LINE6_BASSPODXTLIVE },
478 	{ LINE6_DEVICE(0x4252),    .driver_info = LINE6_BASSPODXTPRO },
479 	{ LINE6_IF_NUM(0x5051, 1), .driver_info = LINE6_POCKETPOD },
480 	{ LINE6_DEVICE(0x5044),    .driver_info = LINE6_PODXT },
481 	{ LINE6_IF_NUM(0x4650, 0), .driver_info = LINE6_PODXTLIVE_POD },
482 	{ LINE6_DEVICE(0x5050),    .driver_info = LINE6_PODXTPRO },
483 	{}
484 };
485 
486 MODULE_DEVICE_TABLE(usb, pod_id_table);
487 
488 static const struct line6_properties pod_properties_table[] = {
489 	[LINE6_BASSPODXT] = {
490 		.id = "BassPODxt",
491 		.name = "BassPODxt",
492 		.capabilities	= LINE6_CAP_CONTROL
493 				| LINE6_CAP_PCM
494 				| LINE6_CAP_HWMON,
495 		.altsetting = 5,
496 		.ep_ctrl_r = 0x84,
497 		.ep_ctrl_w = 0x03,
498 		.ep_audio_r = 0x82,
499 		.ep_audio_w = 0x01,
500 	},
501 	[LINE6_BASSPODXTLIVE] = {
502 		.id = "BassPODxtLive",
503 		.name = "BassPODxt Live",
504 		.capabilities	= LINE6_CAP_CONTROL
505 				| LINE6_CAP_PCM
506 				| LINE6_CAP_HWMON,
507 		.altsetting = 1,
508 		.ep_ctrl_r = 0x84,
509 		.ep_ctrl_w = 0x03,
510 		.ep_audio_r = 0x82,
511 		.ep_audio_w = 0x01,
512 	},
513 	[LINE6_BASSPODXTPRO] = {
514 		.id = "BassPODxtPro",
515 		.name = "BassPODxt Pro",
516 		.capabilities	= LINE6_CAP_CONTROL
517 				| LINE6_CAP_PCM
518 				| LINE6_CAP_HWMON,
519 		.altsetting = 5,
520 		.ep_ctrl_r = 0x84,
521 		.ep_ctrl_w = 0x03,
522 		.ep_audio_r = 0x82,
523 		.ep_audio_w = 0x01,
524 	},
525 	[LINE6_POCKETPOD] = {
526 		.id = "PocketPOD",
527 		.name = "Pocket POD",
528 		.capabilities	= LINE6_CAP_CONTROL,
529 		.altsetting = 0,
530 		.ep_ctrl_r = 0x82,
531 		.ep_ctrl_w = 0x02,
532 		/* no audio channel */
533 	},
534 	[LINE6_PODXT] = {
535 		.id = "PODxt",
536 		.name = "PODxt",
537 		.capabilities	= LINE6_CAP_CONTROL
538 				| LINE6_CAP_PCM
539 				| LINE6_CAP_HWMON,
540 		.altsetting = 5,
541 		.ep_ctrl_r = 0x84,
542 		.ep_ctrl_w = 0x03,
543 		.ep_audio_r = 0x82,
544 		.ep_audio_w = 0x01,
545 	},
546 	[LINE6_PODXTLIVE_POD] = {
547 		.id = "PODxtLive",
548 		.name = "PODxt Live",
549 		.capabilities	= LINE6_CAP_CONTROL
550 				| LINE6_CAP_PCM
551 				| LINE6_CAP_HWMON,
552 		.altsetting = 1,
553 		.ep_ctrl_r = 0x84,
554 		.ep_ctrl_w = 0x03,
555 		.ep_audio_r = 0x82,
556 		.ep_audio_w = 0x01,
557 	},
558 	[LINE6_PODXTPRO] = {
559 		.id = "PODxtPro",
560 		.name = "PODxt Pro",
561 		.capabilities	= LINE6_CAP_CONTROL
562 				| LINE6_CAP_PCM
563 				| LINE6_CAP_HWMON,
564 		.altsetting = 5,
565 		.ep_ctrl_r = 0x84,
566 		.ep_ctrl_w = 0x03,
567 		.ep_audio_r = 0x82,
568 		.ep_audio_w = 0x01,
569 	},
570 };
571 
572 /*
573 	Probe USB device.
574 */
575 static int pod_probe(struct usb_interface *interface,
576 		     const struct usb_device_id *id)
577 {
578 	return line6_probe(interface, id,
579 			   &pod_properties_table[id->driver_info],
580 			   pod_init, sizeof(struct usb_line6_pod));
581 }
582 
583 static struct usb_driver pod_driver = {
584 	.name = KBUILD_MODNAME,
585 	.probe = pod_probe,
586 	.disconnect = line6_disconnect,
587 #ifdef CONFIG_PM
588 	.suspend = line6_suspend,
589 	.resume = line6_resume,
590 	.reset_resume = line6_resume,
591 #endif
592 	.id_table = pod_id_table,
593 };
594 
595 module_usb_driver(pod_driver);
596 
597 MODULE_DESCRIPTION("Line 6 POD USB driver");
598 MODULE_LICENSE("GPL");
599