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