278976da03494ba87324891a6e84810b37c1a0a0
[firefly-linux-kernel-4.4.55.git] / drivers / staging / 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 <sound/control.h>
15
16 #include "audio.h"
17 #include "capture.h"
18 #include "driver.h"
19 #include "playback.h"
20 #include "pod.h"
21
22 #define POD_SYSEX_CODE 3
23 #define POD_BYTES_PER_FRAME 6   /* 24bit audio (stereo) */
24
25 /* *INDENT-OFF* */
26
27 enum {
28         POD_SYSEX_SAVE      = 0x24,
29         POD_SYSEX_SYSTEM    = 0x56,
30         POD_SYSEX_SYSTEMREQ = 0x57,
31         /* POD_SYSEX_UPDATE    = 0x6c, */  /* software update! */
32         POD_SYSEX_STORE     = 0x71,
33         POD_SYSEX_FINISH    = 0x72,
34         POD_SYSEX_DUMPMEM   = 0x73,
35         POD_SYSEX_DUMP      = 0x74,
36         POD_SYSEX_DUMPREQ   = 0x75
37         /* POD_SYSEX_DUMPMEM2  = 0x76 */   /* dumps entire internal memory of PODxt Pro */
38 };
39
40 enum {
41         POD_monitor_level  = 0x04,
42         POD_system_invalid = 0x10000
43 };
44
45 /* *INDENT-ON* */
46
47 enum {
48         POD_DUMP_MEMORY = 2
49 };
50
51 enum {
52         POD_BUSY_READ,
53         POD_BUSY_WRITE,
54         POD_CHANNEL_DIRTY,
55         POD_SAVE_PRESSED,
56         POD_BUSY_MIDISEND
57 };
58
59 static struct snd_ratden pod_ratden = {
60         .num_min = 78125,
61         .num_max = 78125,
62         .num_step = 1,
63         .den = 2
64 };
65
66 static struct line6_pcm_properties pod_pcm_properties = {
67         .snd_line6_playback_hw = {
68                                   .info = (SNDRV_PCM_INFO_MMAP |
69                                            SNDRV_PCM_INFO_INTERLEAVED |
70                                            SNDRV_PCM_INFO_BLOCK_TRANSFER |
71                                            SNDRV_PCM_INFO_MMAP_VALID |
72                                            SNDRV_PCM_INFO_PAUSE |
73 #ifdef CONFIG_PM
74                                            SNDRV_PCM_INFO_RESUME |
75 #endif
76                                            SNDRV_PCM_INFO_SYNC_START),
77                                   .formats = SNDRV_PCM_FMTBIT_S24_3LE,
78                                   .rates = SNDRV_PCM_RATE_KNOT,
79                                   .rate_min = 39062,
80                                   .rate_max = 39063,
81                                   .channels_min = 2,
82                                   .channels_max = 2,
83                                   .buffer_bytes_max = 60000,
84                                   .period_bytes_min = 64,
85                                   .period_bytes_max = 8192,
86                                   .periods_min = 1,
87                                   .periods_max = 1024},
88         .snd_line6_capture_hw = {
89                                  .info = (SNDRV_PCM_INFO_MMAP |
90                                           SNDRV_PCM_INFO_INTERLEAVED |
91                                           SNDRV_PCM_INFO_BLOCK_TRANSFER |
92                                           SNDRV_PCM_INFO_MMAP_VALID |
93 #ifdef CONFIG_PM
94                                           SNDRV_PCM_INFO_RESUME |
95 #endif
96                                           SNDRV_PCM_INFO_SYNC_START),
97                                  .formats = SNDRV_PCM_FMTBIT_S24_3LE,
98                                  .rates = SNDRV_PCM_RATE_KNOT,
99                                  .rate_min = 39062,
100                                  .rate_max = 39063,
101                                  .channels_min = 2,
102                                  .channels_max = 2,
103                                  .buffer_bytes_max = 60000,
104                                  .period_bytes_min = 64,
105                                  .period_bytes_max = 8192,
106                                  .periods_min = 1,
107                                  .periods_max = 1024},
108         .snd_line6_rates = {
109                             .nrats = 1,
110                             .rats = &pod_ratden},
111         .bytes_per_frame = POD_BYTES_PER_FRAME
112 };
113
114 static const char pod_request_channel[] = {
115         0xf0, 0x00, 0x01, 0x0c, 0x03, 0x75, 0xf7
116 };
117
118 static const char pod_version_header[] = {
119         0xf2, 0x7e, 0x7f, 0x06, 0x02
120 };
121
122 /* forward declarations: */
123 static void pod_startup2(unsigned long data);
124 static void pod_startup3(struct usb_line6_pod *pod);
125 static void pod_startup4(struct usb_line6_pod *pod);
126
127 static char *pod_alloc_sysex_buffer(struct usb_line6_pod *pod, int code,
128                                     int size)
129 {
130         return line6_alloc_sysex_buffer(&pod->line6, POD_SYSEX_CODE, code,
131                                         size);
132 }
133
134 /*
135         Store parameter value in driver memory.
136 */
137 static void pod_store_parameter(struct usb_line6_pod *pod, int param, int value)
138 {
139         pod->prog_data.control[param] = value;
140 }
141
142 /*
143         Handle SAVE button.
144 */
145 static void pod_save_button_pressed(struct usb_line6_pod *pod, int type,
146                                     int index)
147 {
148         set_bit(POD_SAVE_PRESSED, &pod->atomic_flags);
149 }
150
151 /*
152         Process a completely received message.
153 */
154 void line6_pod_process_message(struct usb_line6_pod *pod)
155 {
156         const unsigned char *buf = pod->line6.buffer_message;
157
158         /* filter messages by type */
159         switch (buf[0] & 0xf0) {
160         case LINE6_PARAM_CHANGE:
161         case LINE6_PROGRAM_CHANGE:
162         case LINE6_SYSEX_BEGIN:
163                 break;          /* handle these further down */
164
165         default:
166                 return;         /* ignore all others */
167         }
168
169         /* process all remaining messages */
170         switch (buf[0]) {
171         case LINE6_PARAM_CHANGE | LINE6_CHANNEL_DEVICE:
172                 pod_store_parameter(pod, buf[1], buf[2]);
173                 /* intentionally no break here! */
174
175         case LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST:
176                 break;
177
178         case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_DEVICE:
179         case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST:
180                 set_bit(POD_CHANNEL_DIRTY, &pod->atomic_flags);
181                 line6_dump_request_async(&pod->dumpreq, &pod->line6, 0,
182                                          LINE6_DUMP_CURRENT);
183                 break;
184
185         case LINE6_SYSEX_BEGIN | LINE6_CHANNEL_DEVICE:
186         case LINE6_SYSEX_BEGIN | LINE6_CHANNEL_UNKNOWN:
187                 if (memcmp(buf + 1, line6_midi_id, sizeof(line6_midi_id)) == 0) {
188                         switch (buf[5]) {
189                         case POD_SYSEX_DUMP:
190                                 if (pod->line6.message_length ==
191                                     sizeof(pod->prog_data) + 7) {
192                                         switch (pod->dumpreq.in_progress) {
193                                         case LINE6_DUMP_CURRENT:
194                                                 memcpy(&pod->prog_data, buf + 7,
195                                                        sizeof(pod->prog_data));
196                                                 break;
197
198                                         case POD_DUMP_MEMORY:
199                                                 memcpy(&pod->prog_data_buf,
200                                                        buf + 7,
201                                                        sizeof
202                                                        (pod->prog_data_buf));
203                                                 break;
204
205                                         default:
206                                                 dev_dbg(pod->line6.ifcdev,
207                                                         "unknown dump code %02X\n",
208                                                         pod->dumpreq.in_progress);
209                                         }
210
211                                         line6_dump_finished(&pod->dumpreq);
212                                         pod_startup3(pod);
213                                 } else
214                                         dev_dbg(pod->line6.ifcdev,
215                                                 "wrong size of channel dump message (%d instead of %d)\n",
216                                                 pod->line6.message_length,
217                                                 (int)sizeof(pod->prog_data) +
218                                                 7);
219
220                                 break;
221
222                         case POD_SYSEX_SYSTEM:{
223                                         short value =
224                                             ((int)buf[7] << 12) | ((int)buf[8]
225                                                                    << 8) |
226                                             ((int)buf[9] << 4) | (int)buf[10];
227
228                                         if (buf[6] == POD_monitor_level)
229                                                 pod->monitor_level = value;
230                                         break;
231                                 }
232
233                         case POD_SYSEX_FINISH:
234                                 /* do we need to respond to this? */
235                                 break;
236
237                         case POD_SYSEX_SAVE:
238                                 pod_save_button_pressed(pod, buf[6], buf[7]);
239                                 break;
240
241                         case POD_SYSEX_STORE:
242                                 dev_dbg(pod->line6.ifcdev,
243                                         "message %02X not yet implemented\n",
244                                         buf[5]);
245                                 break;
246
247                         default:
248                                 dev_dbg(pod->line6.ifcdev,
249                                         "unknown sysex message %02X\n",
250                                         buf[5]);
251                         }
252                 } else
253                     if (memcmp
254                         (buf, pod_version_header,
255                          sizeof(pod_version_header)) == 0) {
256                         pod->firmware_version =
257                             buf[13] * 100 + buf[14] * 10 + buf[15];
258                         pod->device_id =
259                             ((int)buf[8] << 16) | ((int)buf[9] << 8) | (int)
260                             buf[10];
261                         pod_startup4(pod);
262                 } else
263                         dev_dbg(pod->line6.ifcdev, "unknown sysex header\n");
264
265                 break;
266
267         case LINE6_SYSEX_END:
268                 break;
269
270         default:
271                 dev_dbg(pod->line6.ifcdev, "POD: unknown message %02X\n",
272                         buf[0]);
273         }
274 }
275
276 /*
277         Transmit PODxt Pro control parameter.
278 */
279 void line6_pod_transmit_parameter(struct usb_line6_pod *pod, int param,
280                                   u8 value)
281 {
282         if (line6_transmit_parameter(&pod->line6, param, value) == 0)
283                 pod_store_parameter(pod, param, value);
284 }
285
286 /*
287         Send system parameter (from integer).
288 */
289 static int pod_set_system_param_int(struct usb_line6_pod *pod, int value,
290                                     int code)
291 {
292         char *sysex;
293         static const int size = 5;
294
295         sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEM, size);
296         if (!sysex)
297                 return -ENOMEM;
298         sysex[SYSEX_DATA_OFS] = code;
299         sysex[SYSEX_DATA_OFS + 1] = (value >> 12) & 0x0f;
300         sysex[SYSEX_DATA_OFS + 2] = (value >> 8) & 0x0f;
301         sysex[SYSEX_DATA_OFS + 3] = (value >> 4) & 0x0f;
302         sysex[SYSEX_DATA_OFS + 4] = (value) & 0x0f;
303         line6_send_sysex_message(&pod->line6, sysex, size);
304         kfree(sysex);
305         return 0;
306 }
307
308 /*
309         "read" request on "serial_number" special file.
310 */
311 static ssize_t pod_get_serial_number(struct device *dev,
312                                      struct device_attribute *attr, char *buf)
313 {
314         struct usb_interface *interface = to_usb_interface(dev);
315         struct usb_line6_pod *pod = usb_get_intfdata(interface);
316         return sprintf(buf, "%d\n", pod->serial_number);
317 }
318
319 /*
320         "read" request on "firmware_version" special file.
321 */
322 static ssize_t pod_get_firmware_version(struct device *dev,
323                                         struct device_attribute *attr,
324                                         char *buf)
325 {
326         struct usb_interface *interface = to_usb_interface(dev);
327         struct usb_line6_pod *pod = usb_get_intfdata(interface);
328         return sprintf(buf, "%d.%02d\n", pod->firmware_version / 100,
329                        pod->firmware_version % 100);
330 }
331
332 /*
333         "read" request on "device_id" special file.
334 */
335 static ssize_t pod_get_device_id(struct device *dev,
336                                  struct device_attribute *attr, char *buf)
337 {
338         struct usb_interface *interface = to_usb_interface(dev);
339         struct usb_line6_pod *pod = usb_get_intfdata(interface);
340         return sprintf(buf, "%d\n", pod->device_id);
341 }
342
343 /*
344         POD startup procedure.
345         This is a sequence of functions with special requirements (e.g., must
346         not run immediately after initialization, must not run in interrupt
347         context). After the last one has finished, the device is ready to use.
348 */
349
350 static void pod_startup1(struct usb_line6_pod *pod)
351 {
352         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_INIT);
353
354         /* delay startup procedure: */
355         line6_start_timer(&pod->startup_timer, POD_STARTUP_DELAY, pod_startup2,
356                           (unsigned long)pod);
357 }
358
359 static void pod_startup2(unsigned long data)
360 {
361         struct usb_line6_pod *pod = (struct usb_line6_pod *)data;
362
363         /* schedule another startup procedure until startup is complete: */
364         if (pod->startup_progress >= POD_STARTUP_LAST)
365                 return;
366
367         pod->startup_progress = POD_STARTUP_DUMPREQ;
368         line6_start_timer(&pod->startup_timer, POD_STARTUP_DELAY, pod_startup2,
369                           (unsigned long)pod);
370
371         /* current channel dump: */
372         line6_dump_request_async(&pod->dumpreq, &pod->line6, 0,
373                                  LINE6_DUMP_CURRENT);
374 }
375
376 static void pod_startup3(struct usb_line6_pod *pod)
377 {
378         struct usb_line6 *line6 = &pod->line6;
379         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_VERSIONREQ);
380
381         /* request firmware version: */
382         line6_version_request_async(line6);
383 }
384
385 static void pod_startup4(struct usb_line6_pod *pod)
386 {
387         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_WORKQUEUE);
388
389         /* schedule work for global work queue: */
390         schedule_work(&pod->startup_work);
391 }
392
393 static void pod_startup5(struct work_struct *work)
394 {
395         struct usb_line6_pod *pod =
396             container_of(work, struct usb_line6_pod, startup_work);
397         struct usb_line6 *line6 = &pod->line6;
398
399         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_SETUP);
400
401         /* serial number: */
402         line6_read_serial_number(&pod->line6, &pod->serial_number);
403
404         /* ALSA audio interface: */
405         line6_register_audio(line6);
406 }
407
408 /* POD special files: */
409 static DEVICE_ATTR(device_id, S_IRUGO, pod_get_device_id, line6_nop_write);
410 static DEVICE_ATTR(firmware_version, S_IRUGO, pod_get_firmware_version,
411                    line6_nop_write);
412 static DEVICE_ATTR(serial_number, S_IRUGO, pod_get_serial_number,
413                    line6_nop_write);
414
415 /* control info callback */
416 static int snd_pod_control_monitor_info(struct snd_kcontrol *kcontrol,
417                                         struct snd_ctl_elem_info *uinfo)
418 {
419         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
420         uinfo->count = 1;
421         uinfo->value.integer.min = 0;
422         uinfo->value.integer.max = 65535;
423         return 0;
424 }
425
426 /* control get callback */
427 static int snd_pod_control_monitor_get(struct snd_kcontrol *kcontrol,
428                                        struct snd_ctl_elem_value *ucontrol)
429 {
430         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
431         struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
432         ucontrol->value.integer.value[0] = pod->monitor_level;
433         return 0;
434 }
435
436 /* control put callback */
437 static int snd_pod_control_monitor_put(struct snd_kcontrol *kcontrol,
438                                        struct snd_ctl_elem_value *ucontrol)
439 {
440         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
441         struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
442
443         if (ucontrol->value.integer.value[0] == pod->monitor_level)
444                 return 0;
445
446         pod->monitor_level = ucontrol->value.integer.value[0];
447         pod_set_system_param_int(pod, ucontrol->value.integer.value[0],
448                                  POD_monitor_level);
449         return 1;
450 }
451
452 /* control definition */
453 static struct snd_kcontrol_new pod_control_monitor = {
454         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
455         .name = "Monitor Playback Volume",
456         .index = 0,
457         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
458         .info = snd_pod_control_monitor_info,
459         .get = snd_pod_control_monitor_get,
460         .put = snd_pod_control_monitor_put
461 };
462
463 /*
464         POD destructor.
465 */
466 static void pod_destruct(struct usb_interface *interface)
467 {
468         struct usb_line6_pod *pod = usb_get_intfdata(interface);
469
470         if (pod == NULL)
471                 return;
472         line6_cleanup_audio(&pod->line6);
473
474         del_timer(&pod->startup_timer);
475         cancel_work_sync(&pod->startup_work);
476
477         /* free dump request data: */
478         line6_dumpreq_destruct(&pod->dumpreq);
479 }
480
481 /*
482         Create sysfs entries.
483 */
484 static int pod_create_files2(struct device *dev)
485 {
486         int err;
487
488         CHECK_RETURN(device_create_file(dev, &dev_attr_device_id));
489         CHECK_RETURN(device_create_file(dev, &dev_attr_firmware_version));
490         CHECK_RETURN(device_create_file(dev, &dev_attr_serial_number));
491         return 0;
492 }
493
494 /*
495          Try to init POD device.
496 */
497 static int pod_try_init(struct usb_interface *interface,
498                         struct usb_line6_pod *pod)
499 {
500         int err;
501         struct usb_line6 *line6 = &pod->line6;
502
503         init_timer(&pod->startup_timer);
504         INIT_WORK(&pod->startup_work, pod_startup5);
505
506         if ((interface == NULL) || (pod == NULL))
507                 return -ENODEV;
508
509         /* initialize USB buffers: */
510         err = line6_dumpreq_init(&pod->dumpreq, pod_request_channel,
511                                  sizeof(pod_request_channel));
512         if (err < 0) {
513                 dev_err(&interface->dev, "Out of memory\n");
514                 return -ENOMEM;
515         }
516
517         /* create sysfs entries: */
518         err = pod_create_files2(&interface->dev);
519         if (err < 0)
520                 return err;
521
522         /* initialize audio system: */
523         err = line6_init_audio(line6);
524         if (err < 0)
525                 return err;
526
527         /* initialize MIDI subsystem: */
528         err = line6_init_midi(line6);
529         if (err < 0)
530                 return err;
531
532         /* initialize PCM subsystem: */
533         err = line6_init_pcm(line6, &pod_pcm_properties);
534         if (err < 0)
535                 return err;
536
537         /* register monitor control: */
538         err = snd_ctl_add(line6->card,
539                           snd_ctl_new1(&pod_control_monitor, line6->line6pcm));
540         if (err < 0)
541                 return err;
542
543         /*
544            When the sound card is registered at this point, the PODxt Live
545            displays "Invalid Code Error 07", so we do it later in the event
546            handler.
547          */
548
549         if (pod->line6.properties->capabilities & LINE6_BIT_CONTROL) {
550                 pod->monitor_level = POD_system_invalid;
551
552                 /* initiate startup procedure: */
553                 pod_startup1(pod);
554         }
555
556         return 0;
557 }
558
559 /*
560          Init POD device (and clean up in case of failure).
561 */
562 int line6_pod_init(struct usb_interface *interface, struct usb_line6_pod *pod)
563 {
564         int err = pod_try_init(interface, pod);
565
566         if (err < 0)
567                 pod_destruct(interface);
568
569         return err;
570 }
571
572 /*
573         POD device disconnected.
574 */
575 void line6_pod_disconnect(struct usb_interface *interface)
576 {
577         struct usb_line6_pod *pod;
578
579         if (interface == NULL)
580                 return;
581         pod = usb_get_intfdata(interface);
582
583         if (pod != NULL) {
584                 struct snd_line6_pcm *line6pcm = pod->line6.line6pcm;
585                 struct device *dev = &interface->dev;
586
587                 if (line6pcm != NULL)
588                         line6_pcm_disconnect(line6pcm);
589
590                 if (dev != NULL) {
591                         /* remove sysfs entries: */
592                         device_remove_file(dev, &dev_attr_device_id);
593                         device_remove_file(dev, &dev_attr_firmware_version);
594                         device_remove_file(dev, &dev_attr_serial_number);
595                 }
596         }
597
598         pod_destruct(interface);
599 }