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