ALSA: line6: Split to each driver
[firefly-linux-kernel-4.4.55.git] / sound / usb / line6 / pod.c
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 #ifdef CONFIG_PM
157                                            SNDRV_PCM_INFO_RESUME |
158 #endif
159                                            SNDRV_PCM_INFO_SYNC_START),
160                                   .formats = SNDRV_PCM_FMTBIT_S24_3LE,
161                                   .rates = SNDRV_PCM_RATE_KNOT,
162                                   .rate_min = 39062,
163                                   .rate_max = 39063,
164                                   .channels_min = 2,
165                                   .channels_max = 2,
166                                   .buffer_bytes_max = 60000,
167                                   .period_bytes_min = 64,
168                                   .period_bytes_max = 8192,
169                                   .periods_min = 1,
170                                   .periods_max = 1024},
171         .snd_line6_capture_hw = {
172                                  .info = (SNDRV_PCM_INFO_MMAP |
173                                           SNDRV_PCM_INFO_INTERLEAVED |
174                                           SNDRV_PCM_INFO_BLOCK_TRANSFER |
175                                           SNDRV_PCM_INFO_MMAP_VALID |
176 #ifdef CONFIG_PM
177                                           SNDRV_PCM_INFO_RESUME |
178 #endif
179                                           SNDRV_PCM_INFO_SYNC_START),
180                                  .formats = SNDRV_PCM_FMTBIT_S24_3LE,
181                                  .rates = SNDRV_PCM_RATE_KNOT,
182                                  .rate_min = 39062,
183                                  .rate_max = 39063,
184                                  .channels_min = 2,
185                                  .channels_max = 2,
186                                  .buffer_bytes_max = 60000,
187                                  .period_bytes_min = 64,
188                                  .period_bytes_max = 8192,
189                                  .periods_min = 1,
190                                  .periods_max = 1024},
191         .snd_line6_rates = {
192                             .nrats = 1,
193                             .rats = &pod_ratden},
194         .bytes_per_frame = POD_BYTES_PER_FRAME
195 };
196
197 static const char pod_version_header[] = {
198         0xf2, 0x7e, 0x7f, 0x06, 0x02
199 };
200
201 /* forward declarations: */
202 static void pod_startup2(unsigned long data);
203 static void pod_startup3(struct usb_line6_pod *pod);
204
205 static char *pod_alloc_sysex_buffer(struct usb_line6_pod *pod, int code,
206                                     int size)
207 {
208         return line6_alloc_sysex_buffer(&pod->line6, POD_SYSEX_CODE, code,
209                                         size);
210 }
211
212 /*
213         Process a completely received message.
214 */
215 static void line6_pod_process_message(struct usb_line6 *line6)
216 {
217         struct usb_line6_pod *pod = (struct usb_line6_pod *) line6;
218         const unsigned char *buf = pod->line6.buffer_message;
219
220         if (memcmp(buf, pod_version_header, sizeof(pod_version_header)) == 0) {
221                 pod->firmware_version = buf[13] * 100 + buf[14] * 10 + buf[15];
222                 pod->device_id = ((int)buf[8] << 16) | ((int)buf[9] << 8) |
223                                  (int) buf[10];
224                 pod_startup3(pod);
225                 return;
226         }
227
228         /* Only look for sysex messages from this device */
229         if (buf[0] != (LINE6_SYSEX_BEGIN | LINE6_CHANNEL_DEVICE) &&
230             buf[0] != (LINE6_SYSEX_BEGIN | LINE6_CHANNEL_UNKNOWN)) {
231                 return;
232         }
233         if (memcmp(buf + 1, line6_midi_id, sizeof(line6_midi_id)) != 0)
234                 return;
235
236         if (buf[5] == POD_SYSEX_SYSTEM && buf[6] == POD_MONITOR_LEVEL) {
237                 short value = ((int)buf[7] << 12) | ((int)buf[8] << 8) |
238                               ((int)buf[9] << 4) | (int)buf[10];
239                 pod->monitor_level = value;
240         }
241 }
242
243 /*
244         Send system parameter (from integer).
245 */
246 static int pod_set_system_param_int(struct usb_line6_pod *pod, int value,
247                                     int code)
248 {
249         char *sysex;
250         static const int size = 5;
251
252         sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEM, size);
253         if (!sysex)
254                 return -ENOMEM;
255         sysex[SYSEX_DATA_OFS] = code;
256         sysex[SYSEX_DATA_OFS + 1] = (value >> 12) & 0x0f;
257         sysex[SYSEX_DATA_OFS + 2] = (value >> 8) & 0x0f;
258         sysex[SYSEX_DATA_OFS + 3] = (value >> 4) & 0x0f;
259         sysex[SYSEX_DATA_OFS + 4] = (value) & 0x0f;
260         line6_send_sysex_message(&pod->line6, sysex, size);
261         kfree(sysex);
262         return 0;
263 }
264
265 /*
266         "read" request on "serial_number" special file.
267 */
268 static ssize_t serial_number_show(struct device *dev,
269                                   struct device_attribute *attr, char *buf)
270 {
271         struct usb_interface *interface = to_usb_interface(dev);
272         struct usb_line6_pod *pod = usb_get_intfdata(interface);
273
274         return sprintf(buf, "%d\n", pod->serial_number);
275 }
276
277 /*
278         "read" request on "firmware_version" special file.
279 */
280 static ssize_t firmware_version_show(struct device *dev,
281                                      struct device_attribute *attr, char *buf)
282 {
283         struct usb_interface *interface = to_usb_interface(dev);
284         struct usb_line6_pod *pod = usb_get_intfdata(interface);
285
286         return sprintf(buf, "%d.%02d\n", pod->firmware_version / 100,
287                        pod->firmware_version % 100);
288 }
289
290 /*
291         "read" request on "device_id" special file.
292 */
293 static ssize_t device_id_show(struct device *dev,
294                               struct device_attribute *attr, char *buf)
295 {
296         struct usb_interface *interface = to_usb_interface(dev);
297         struct usb_line6_pod *pod = usb_get_intfdata(interface);
298
299         return sprintf(buf, "%d\n", pod->device_id);
300 }
301
302 /*
303         POD startup procedure.
304         This is a sequence of functions with special requirements (e.g., must
305         not run immediately after initialization, must not run in interrupt
306         context). After the last one has finished, the device is ready to use.
307 */
308
309 static void pod_startup1(struct usb_line6_pod *pod)
310 {
311         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_INIT);
312
313         /* delay startup procedure: */
314         line6_start_timer(&pod->startup_timer, POD_STARTUP_DELAY, pod_startup2,
315                           (unsigned long)pod);
316 }
317
318 static void pod_startup2(unsigned long data)
319 {
320         struct usb_line6_pod *pod = (struct usb_line6_pod *)data;
321         struct usb_line6 *line6 = &pod->line6;
322
323         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_VERSIONREQ);
324
325         /* request firmware version: */
326         line6_version_request_async(line6);
327 }
328
329 static void pod_startup3(struct usb_line6_pod *pod)
330 {
331         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_WORKQUEUE);
332
333         /* schedule work for global work queue: */
334         schedule_work(&pod->startup_work);
335 }
336
337 static void pod_startup4(struct work_struct *work)
338 {
339         struct usb_line6_pod *pod =
340             container_of(work, struct usb_line6_pod, startup_work);
341         struct usb_line6 *line6 = &pod->line6;
342
343         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_SETUP);
344
345         /* serial number: */
346         line6_read_serial_number(&pod->line6, &pod->serial_number);
347
348         /* ALSA audio interface: */
349         line6_register_audio(line6);
350 }
351
352 /* POD special files: */
353 static DEVICE_ATTR_RO(device_id);
354 static DEVICE_ATTR_RO(firmware_version);
355 static DEVICE_ATTR_RO(serial_number);
356
357 /* control info callback */
358 static int snd_pod_control_monitor_info(struct snd_kcontrol *kcontrol,
359                                         struct snd_ctl_elem_info *uinfo)
360 {
361         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
362         uinfo->count = 1;
363         uinfo->value.integer.min = 0;
364         uinfo->value.integer.max = 65535;
365         return 0;
366 }
367
368 /* control get callback */
369 static int snd_pod_control_monitor_get(struct snd_kcontrol *kcontrol,
370                                        struct snd_ctl_elem_value *ucontrol)
371 {
372         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
373         struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
374
375         ucontrol->value.integer.value[0] = pod->monitor_level;
376         return 0;
377 }
378
379 /* control put callback */
380 static int snd_pod_control_monitor_put(struct snd_kcontrol *kcontrol,
381                                        struct snd_ctl_elem_value *ucontrol)
382 {
383         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
384         struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
385
386         if (ucontrol->value.integer.value[0] == pod->monitor_level)
387                 return 0;
388
389         pod->monitor_level = ucontrol->value.integer.value[0];
390         pod_set_system_param_int(pod, ucontrol->value.integer.value[0],
391                                  POD_MONITOR_LEVEL);
392         return 1;
393 }
394
395 /* control definition */
396 static struct snd_kcontrol_new pod_control_monitor = {
397         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
398         .name = "Monitor Playback Volume",
399         .index = 0,
400         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
401         .info = snd_pod_control_monitor_info,
402         .get = snd_pod_control_monitor_get,
403         .put = snd_pod_control_monitor_put
404 };
405
406 /*
407         POD destructor.
408 */
409 static void pod_destruct(struct usb_interface *interface)
410 {
411         struct usb_line6_pod *pod = usb_get_intfdata(interface);
412
413         if (pod == NULL)
414                 return;
415         line6_cleanup_audio(&pod->line6);
416
417         del_timer(&pod->startup_timer);
418         cancel_work_sync(&pod->startup_work);
419 }
420
421 /*
422         POD device disconnected.
423 */
424 static void line6_pod_disconnect(struct usb_interface *interface)
425 {
426         struct usb_line6_pod *pod;
427
428         if (interface == NULL)
429                 return;
430         pod = usb_get_intfdata(interface);
431
432         if (pod != NULL) {
433                 struct snd_line6_pcm *line6pcm = pod->line6.line6pcm;
434                 struct device *dev = &interface->dev;
435
436                 if (line6pcm != NULL)
437                         line6_pcm_disconnect(line6pcm);
438
439                 if (dev != NULL) {
440                         /* remove sysfs entries: */
441                         device_remove_file(dev, &dev_attr_device_id);
442                         device_remove_file(dev, &dev_attr_firmware_version);
443                         device_remove_file(dev, &dev_attr_serial_number);
444                 }
445         }
446
447         pod_destruct(interface);
448 }
449
450 /*
451         Create sysfs entries.
452 */
453 static int pod_create_files2(struct device *dev)
454 {
455         int err;
456
457         CHECK_RETURN(device_create_file(dev, &dev_attr_device_id));
458         CHECK_RETURN(device_create_file(dev, &dev_attr_firmware_version));
459         CHECK_RETURN(device_create_file(dev, &dev_attr_serial_number));
460         return 0;
461 }
462
463 /*
464          Try to init POD device.
465 */
466 static int pod_try_init(struct usb_interface *interface,
467                         struct usb_line6 *line6)
468 {
469         int err;
470         struct usb_line6_pod *pod = (struct usb_line6_pod *) line6;
471
472         line6->process_message = line6_pod_process_message;
473         line6->disconnect = line6_pod_disconnect;
474
475         init_timer(&pod->startup_timer);
476         INIT_WORK(&pod->startup_work, pod_startup4);
477
478         if ((interface == NULL) || (pod == NULL))
479                 return -ENODEV;
480
481         /* create sysfs entries: */
482         err = pod_create_files2(&interface->dev);
483         if (err < 0)
484                 return err;
485
486         /* initialize audio system: */
487         err = line6_init_audio(line6);
488         if (err < 0)
489                 return err;
490
491         /* initialize MIDI subsystem: */
492         err = line6_init_midi(line6);
493         if (err < 0)
494                 return err;
495
496         /* initialize PCM subsystem: */
497         err = line6_init_pcm(line6, &pod_pcm_properties);
498         if (err < 0)
499                 return err;
500
501         /* register monitor control: */
502         err = snd_ctl_add(line6->card,
503                           snd_ctl_new1(&pod_control_monitor, line6->line6pcm));
504         if (err < 0)
505                 return err;
506
507         /*
508            When the sound card is registered at this point, the PODxt Live
509            displays "Invalid Code Error 07", so we do it later in the event
510            handler.
511          */
512
513         if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL) {
514                 pod->monitor_level = POD_SYSTEM_INVALID;
515
516                 /* initiate startup procedure: */
517                 pod_startup1(pod);
518         }
519
520         return 0;
521 }
522
523 /*
524          Init POD device (and clean up in case of failure).
525 */
526 static int pod_init(struct usb_interface *interface,
527                     struct usb_line6 *line6)
528 {
529         int err = pod_try_init(interface, line6);
530
531         if (err < 0)
532                 pod_destruct(interface);
533
534         return err;
535 }
536
537 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
538 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
539
540 /* table of devices that work with this driver */
541 static const struct usb_device_id pod_id_table[] = {
542         { LINE6_DEVICE(0x4250),    .driver_info = LINE6_BASSPODXT },
543         { LINE6_DEVICE(0x4642),    .driver_info = LINE6_BASSPODXTLIVE },
544         { LINE6_DEVICE(0x4252),    .driver_info = LINE6_BASSPODXTPRO },
545         { LINE6_IF_NUM(0x5051, 1), .driver_info = LINE6_POCKETPOD },
546         { LINE6_DEVICE(0x5044),    .driver_info = LINE6_PODXT },
547         { LINE6_IF_NUM(0x4650, 0), .driver_info = LINE6_PODXTLIVE_POD },
548         { LINE6_DEVICE(0x5050),    .driver_info = LINE6_PODXTPRO },
549         {}
550 };
551
552 MODULE_DEVICE_TABLE(usb, pod_id_table);
553
554 static const struct line6_properties pod_properties_table[] = {
555         [LINE6_BASSPODXT] = {
556                 .id = "BassPODxt",
557                 .name = "BassPODxt",
558                 .capabilities   = LINE6_CAP_CONTROL
559                                 | LINE6_CAP_PCM
560                                 | LINE6_CAP_HWMON,
561                 .altsetting = 5,
562                 .ep_ctrl_r = 0x84,
563                 .ep_ctrl_w = 0x03,
564                 .ep_audio_r = 0x82,
565                 .ep_audio_w = 0x01,
566         },
567         [LINE6_BASSPODXTLIVE] = {
568                 .id = "BassPODxtLive",
569                 .name = "BassPODxt Live",
570                 .capabilities   = LINE6_CAP_CONTROL
571                                 | LINE6_CAP_PCM
572                                 | LINE6_CAP_HWMON,
573                 .altsetting = 1,
574                 .ep_ctrl_r = 0x84,
575                 .ep_ctrl_w = 0x03,
576                 .ep_audio_r = 0x82,
577                 .ep_audio_w = 0x01,
578         },
579         [LINE6_BASSPODXTPRO] = {
580                 .id = "BassPODxtPro",
581                 .name = "BassPODxt Pro",
582                 .capabilities   = LINE6_CAP_CONTROL
583                                 | LINE6_CAP_PCM
584                                 | LINE6_CAP_HWMON,
585                 .altsetting = 5,
586                 .ep_ctrl_r = 0x84,
587                 .ep_ctrl_w = 0x03,
588                 .ep_audio_r = 0x82,
589                 .ep_audio_w = 0x01,
590         },
591         [LINE6_POCKETPOD] = {
592                 .id = "PocketPOD",
593                 .name = "Pocket POD",
594                 .capabilities   = LINE6_CAP_CONTROL,
595                 .altsetting = 0,
596                 .ep_ctrl_r = 0x82,
597                 .ep_ctrl_w = 0x02,
598                 /* no audio channel */
599         },
600         [LINE6_PODXT] = {
601                 .id = "PODxt",
602                 .name = "PODxt",
603                 .capabilities   = LINE6_CAP_CONTROL
604                                 | LINE6_CAP_PCM
605                                 | LINE6_CAP_HWMON,
606                 .altsetting = 5,
607                 .ep_ctrl_r = 0x84,
608                 .ep_ctrl_w = 0x03,
609                 .ep_audio_r = 0x82,
610                 .ep_audio_w = 0x01,
611         },
612         [LINE6_PODXTLIVE_POD] = {
613                 .id = "PODxtLive",
614                 .name = "PODxt Live",
615                 .capabilities   = LINE6_CAP_CONTROL
616                                 | LINE6_CAP_PCM
617                                 | LINE6_CAP_HWMON,
618                 .altsetting = 1,
619                 .ep_ctrl_r = 0x84,
620                 .ep_ctrl_w = 0x03,
621                 .ep_audio_r = 0x82,
622                 .ep_audio_w = 0x01,
623         },
624         [LINE6_PODXTPRO] = {
625                 .id = "PODxtPro",
626                 .name = "PODxt Pro",
627                 .capabilities   = LINE6_CAP_CONTROL
628                                 | LINE6_CAP_PCM
629                                 | LINE6_CAP_HWMON,
630                 .altsetting = 5,
631                 .ep_ctrl_r = 0x84,
632                 .ep_ctrl_w = 0x03,
633                 .ep_audio_r = 0x82,
634                 .ep_audio_w = 0x01,
635         },
636 };
637
638 /*
639         Probe USB device.
640 */
641 static int pod_probe(struct usb_interface *interface,
642                      const struct usb_device_id *id)
643 {
644         struct usb_line6_pod *pod;
645         int err;
646
647         pod = kzalloc(sizeof(*pod), GFP_KERNEL);
648         if (!pod)
649                 return -ENODEV;
650         err = line6_probe(interface, &pod->line6,
651                           &pod_properties_table[id->driver_info],
652                           pod_init);
653         if (err < 0)
654                 kfree(pod);
655         return err;
656 }
657
658 static struct usb_driver pod_driver = {
659         .name = KBUILD_MODNAME,
660         .probe = pod_probe,
661         .disconnect = line6_disconnect,
662 #ifdef CONFIG_PM
663         .suspend = line6_suspend,
664         .resume = line6_resume,
665         .reset_resume = line6_resume,
666 #endif
667         .id_table = pod_id_table,
668 };
669
670 module_usb_driver(pod_driver);
671
672 MODULE_DESCRIPTION("Line6 POD USB driver");
673 MODULE_LICENSE("GPL");