staging: line6: drop store_amp_setup sysfs attr
[cascardo/linux.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 "control.h"
19 #include "driver.h"
20 #include "playback.h"
21 #include "pod.h"
22
23 #define POD_SYSEX_CODE 3
24 #define POD_BYTES_PER_FRAME 6   /* 24bit audio (stereo) */
25
26 /* *INDENT-OFF* */
27
28 enum {
29         POD_SYSEX_SAVE      = 0x24,
30         POD_SYSEX_SYSTEM    = 0x56,
31         POD_SYSEX_SYSTEMREQ = 0x57,
32         /* POD_SYSEX_UPDATE    = 0x6c, */  /* software update! */
33         POD_SYSEX_STORE     = 0x71,
34         POD_SYSEX_FINISH    = 0x72,
35         POD_SYSEX_DUMPMEM   = 0x73,
36         POD_SYSEX_DUMP      = 0x74,
37         POD_SYSEX_DUMPREQ   = 0x75
38         /* POD_SYSEX_DUMPMEM2  = 0x76 */   /* dumps entire internal memory of PODxt Pro */
39 };
40
41 enum {
42         POD_monitor_level  = 0x04,
43         POD_routing        = 0x05,
44         POD_tuner_mute     = 0x13,
45         POD_tuner_freq     = 0x15,
46         POD_tuner_note     = 0x16,
47         POD_tuner_pitch    = 0x17,
48         POD_system_invalid = 0x10000
49 };
50
51 /* *INDENT-ON* */
52
53 enum {
54         POD_DUMP_MEMORY = 2
55 };
56
57 enum {
58         POD_BUSY_READ,
59         POD_BUSY_WRITE,
60         POD_CHANNEL_DIRTY,
61         POD_SAVE_PRESSED,
62         POD_BUSY_MIDISEND
63 };
64
65 static struct snd_ratden pod_ratden = {
66         .num_min = 78125,
67         .num_max = 78125,
68         .num_step = 1,
69         .den = 2
70 };
71
72 static struct line6_pcm_properties pod_pcm_properties = {
73         .snd_line6_playback_hw = {
74                                   .info = (SNDRV_PCM_INFO_MMAP |
75                                            SNDRV_PCM_INFO_INTERLEAVED |
76                                            SNDRV_PCM_INFO_BLOCK_TRANSFER |
77                                            SNDRV_PCM_INFO_MMAP_VALID |
78                                            SNDRV_PCM_INFO_PAUSE |
79 #ifdef CONFIG_PM
80                                            SNDRV_PCM_INFO_RESUME |
81 #endif
82                                            SNDRV_PCM_INFO_SYNC_START),
83                                   .formats = SNDRV_PCM_FMTBIT_S24_3LE,
84                                   .rates = SNDRV_PCM_RATE_KNOT,
85                                   .rate_min = 39062,
86                                   .rate_max = 39063,
87                                   .channels_min = 2,
88                                   .channels_max = 2,
89                                   .buffer_bytes_max = 60000,
90                                   .period_bytes_min = 64,
91                                   .period_bytes_max = 8192,
92                                   .periods_min = 1,
93                                   .periods_max = 1024},
94         .snd_line6_capture_hw = {
95                                  .info = (SNDRV_PCM_INFO_MMAP |
96                                           SNDRV_PCM_INFO_INTERLEAVED |
97                                           SNDRV_PCM_INFO_BLOCK_TRANSFER |
98                                           SNDRV_PCM_INFO_MMAP_VALID |
99 #ifdef CONFIG_PM
100                                           SNDRV_PCM_INFO_RESUME |
101 #endif
102                                           SNDRV_PCM_INFO_SYNC_START),
103                                  .formats = SNDRV_PCM_FMTBIT_S24_3LE,
104                                  .rates = SNDRV_PCM_RATE_KNOT,
105                                  .rate_min = 39062,
106                                  .rate_max = 39063,
107                                  .channels_min = 2,
108                                  .channels_max = 2,
109                                  .buffer_bytes_max = 60000,
110                                  .period_bytes_min = 64,
111                                  .period_bytes_max = 8192,
112                                  .periods_min = 1,
113                                  .periods_max = 1024},
114         .snd_line6_rates = {
115                             .nrats = 1,
116                             .rats = &pod_ratden},
117         .bytes_per_frame = POD_BYTES_PER_FRAME
118 };
119
120 static const char pod_request_channel[] = {
121         0xf0, 0x00, 0x01, 0x0c, 0x03, 0x75, 0xf7
122 };
123
124 static const char pod_version_header[] = {
125         0xf2, 0x7e, 0x7f, 0x06, 0x02
126 };
127
128 /* forward declarations: */
129 static void pod_startup2(unsigned long data);
130 static void pod_startup3(struct usb_line6_pod *pod);
131 static void pod_startup4(struct usb_line6_pod *pod);
132
133 static char *pod_alloc_sysex_buffer(struct usb_line6_pod *pod, int code,
134                                     int size)
135 {
136         return line6_alloc_sysex_buffer(&pod->line6, POD_SYSEX_CODE, code,
137                                         size);
138 }
139
140 /*
141         Store parameter value in driver memory.
142 */
143 static void pod_store_parameter(struct usb_line6_pod *pod, int param, int value)
144 {
145         pod->prog_data.control[param] = value;
146 }
147
148 /*
149         Handle SAVE button.
150 */
151 static void pod_save_button_pressed(struct usb_line6_pod *pod, int type,
152                                     int index)
153 {
154         set_bit(POD_SAVE_PRESSED, &pod->atomic_flags);
155 }
156
157 /*
158         Process a completely received message.
159 */
160 void line6_pod_process_message(struct usb_line6_pod *pod)
161 {
162         const unsigned char *buf = pod->line6.buffer_message;
163
164         /* filter messages by type */
165         switch (buf[0] & 0xf0) {
166         case LINE6_PARAM_CHANGE:
167         case LINE6_PROGRAM_CHANGE:
168         case LINE6_SYSEX_BEGIN:
169                 break;          /* handle these further down */
170
171         default:
172                 return;         /* ignore all others */
173         }
174
175         /* process all remaining messages */
176         switch (buf[0]) {
177         case LINE6_PARAM_CHANGE | LINE6_CHANNEL_DEVICE:
178                 pod_store_parameter(pod, buf[1], buf[2]);
179                 /* intentionally no break here! */
180
181         case LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST:
182                 if ((buf[1] == POD_amp_model_setup) ||
183                     (buf[1] == POD_effect_setup))
184                         /* these also affect other settings */
185                         line6_dump_request_async(&pod->dumpreq, &pod->line6, 0,
186                                                  LINE6_DUMP_CURRENT);
187
188                 break;
189
190         case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_DEVICE:
191         case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST:
192                 set_bit(POD_CHANNEL_DIRTY, &pod->atomic_flags);
193                 line6_dump_request_async(&pod->dumpreq, &pod->line6, 0,
194                                          LINE6_DUMP_CURRENT);
195                 break;
196
197         case LINE6_SYSEX_BEGIN | LINE6_CHANNEL_DEVICE:
198         case LINE6_SYSEX_BEGIN | LINE6_CHANNEL_UNKNOWN:
199                 if (memcmp(buf + 1, line6_midi_id, sizeof(line6_midi_id)) == 0) {
200                         switch (buf[5]) {
201                         case POD_SYSEX_DUMP:
202                                 if (pod->line6.message_length ==
203                                     sizeof(pod->prog_data) + 7) {
204                                         switch (pod->dumpreq.in_progress) {
205                                         case LINE6_DUMP_CURRENT:
206                                                 memcpy(&pod->prog_data, buf + 7,
207                                                        sizeof(pod->prog_data));
208                                                 break;
209
210                                         case POD_DUMP_MEMORY:
211                                                 memcpy(&pod->prog_data_buf,
212                                                        buf + 7,
213                                                        sizeof
214                                                        (pod->prog_data_buf));
215                                                 break;
216
217                                         default:
218                                                 dev_dbg(pod->line6.ifcdev,
219                                                         "unknown dump code %02X\n",
220                                                         pod->dumpreq.in_progress);
221                                         }
222
223                                         line6_dump_finished(&pod->dumpreq);
224                                         pod_startup3(pod);
225                                 } else
226                                         dev_dbg(pod->line6.ifcdev,
227                                                 "wrong size of channel dump message (%d instead of %d)\n",
228                                                 pod->line6.message_length,
229                                                 (int)sizeof(pod->prog_data) +
230                                                 7);
231
232                                 break;
233
234                         case POD_SYSEX_SYSTEM:{
235                                         short value =
236                                             ((int)buf[7] << 12) | ((int)buf[8]
237                                                                    << 8) |
238                                             ((int)buf[9] << 4) | (int)buf[10];
239
240 #define PROCESS_SYSTEM_PARAM(x) \
241                                         case POD_ ## x: \
242                                                 pod->x.value = value; \
243                                                 wake_up(&pod->x.wait); \
244                                                 break;
245
246                                         switch (buf[6]) {
247                                         case POD_monitor_level:
248                                                 pod->monitor_level = value;
249                                                 break;
250
251                                                 PROCESS_SYSTEM_PARAM(routing);
252                                                 PROCESS_SYSTEM_PARAM
253                                                     (tuner_mute);
254                                                 PROCESS_SYSTEM_PARAM
255                                                     (tuner_freq);
256                                                 PROCESS_SYSTEM_PARAM
257                                                     (tuner_note);
258                                                 PROCESS_SYSTEM_PARAM
259                                                     (tuner_pitch);
260
261 #undef PROCESS_SYSTEM_PARAM
262
263                                         default:
264                                                 dev_dbg(pod->line6.ifcdev,
265                                                         "unknown tuner/system response %02X\n",
266                                                         buf[6]);
267                                         }
268
269                                         break;
270                                 }
271
272                         case POD_SYSEX_FINISH:
273                                 /* do we need to respond to this? */
274                                 break;
275
276                         case POD_SYSEX_SAVE:
277                                 pod_save_button_pressed(pod, buf[6], buf[7]);
278                                 break;
279
280                         case POD_SYSEX_STORE:
281                                 dev_dbg(pod->line6.ifcdev,
282                                         "message %02X not yet implemented\n",
283                                         buf[5]);
284                                 break;
285
286                         default:
287                                 dev_dbg(pod->line6.ifcdev,
288                                         "unknown sysex message %02X\n",
289                                         buf[5]);
290                         }
291                 } else
292                     if (memcmp
293                         (buf, pod_version_header,
294                          sizeof(pod_version_header)) == 0) {
295                         pod->firmware_version =
296                             buf[13] * 100 + buf[14] * 10 + buf[15];
297                         pod->device_id =
298                             ((int)buf[8] << 16) | ((int)buf[9] << 8) | (int)
299                             buf[10];
300                         pod_startup4(pod);
301                 } else
302                         dev_dbg(pod->line6.ifcdev, "unknown sysex header\n");
303
304                 break;
305
306         case LINE6_SYSEX_END:
307                 break;
308
309         default:
310                 dev_dbg(pod->line6.ifcdev, "POD: unknown message %02X\n",
311                         buf[0]);
312         }
313 }
314
315 /*
316         Detect some cases that require a channel dump after sending a command to the
317         device. Important notes:
318         *) The actual dump request can not be sent here since we are not allowed to
319         wait for the completion of the first message in this context, and sending
320         the dump request before completion of the previous message leaves the POD
321         in an undefined state. The dump request will be sent when the echoed
322         commands are received.
323         *) This method fails if a param change message is "chopped" after the first
324         byte.
325 */
326 void line6_pod_midi_postprocess(struct usb_line6_pod *pod, unsigned char *data,
327                                 int length)
328 {
329         int i;
330
331         if (!pod->midi_postprocess)
332                 return;
333
334         for (i = 0; i < length; ++i) {
335                 if (data[i] == (LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST)) {
336                         line6_invalidate_current(&pod->dumpreq);
337                         break;
338                 } else
339                     if ((data[i] == (LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST))
340                         && (i < length - 1))
341                         if ((data[i + 1] == POD_amp_model_setup)
342                             || (data[i + 1] == POD_effect_setup)) {
343                                 line6_invalidate_current(&pod->dumpreq);
344                                 break;
345                         }
346         }
347 }
348
349 /*
350         Transmit PODxt Pro control parameter.
351 */
352 void line6_pod_transmit_parameter(struct usb_line6_pod *pod, int param,
353                                   u8 value)
354 {
355         if (line6_transmit_parameter(&pod->line6, param, value) == 0)
356                 pod_store_parameter(pod, param, value);
357
358         if ((param == POD_amp_model_setup) || (param == POD_effect_setup))      /* these also affect other settings */
359                 line6_invalidate_current(&pod->dumpreq);
360 }
361
362 /*
363         Resolve value to memory location.
364 */
365 static int pod_resolve(const char *buf, short block0, short block1,
366                        unsigned char *location)
367 {
368         u8 value;
369         short block;
370         int ret;
371
372         ret = kstrtou8(buf, 10, &value);
373         if (ret)
374                 return ret;
375
376         block = (value < 0x40) ? block0 : block1;
377         value &= 0x3f;
378         location[0] = block >> 7;
379         location[1] = value | (block & 0x7f);
380         return 0;
381 }
382
383 /*
384         Send command to retrieve channel/effects setup/amp setup to PODxt Pro.
385 */
386 static ssize_t pod_send_retrieve_command(struct device *dev, const char *buf,
387                                          size_t count, short block0,
388                                          short block1)
389 {
390         struct usb_interface *interface = to_usb_interface(dev);
391         struct usb_line6_pod *pod = usb_get_intfdata(interface);
392         int ret;
393         int size = 4;
394         char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_DUMPMEM, size);
395
396         if (!sysex)
397                 return 0;
398
399         ret = pod_resolve(buf, block0, block1, sysex + SYSEX_DATA_OFS);
400         if (ret) {
401                 kfree(sysex);
402                 return ret;
403         }
404         sysex[SYSEX_DATA_OFS + 2] = 0;
405         sysex[SYSEX_DATA_OFS + 3] = 0;
406         line6_dump_started(&pod->dumpreq, POD_DUMP_MEMORY);
407
408         if (line6_send_sysex_message(&pod->line6, sysex, size) < size)
409                 line6_dump_finished(&pod->dumpreq);
410
411         kfree(sysex);
412         /* needs some delay here on AMD64 platform */
413         return count;
414 }
415
416 /*
417         Identify system parameters related to the tuner.
418 */
419 static bool pod_is_tuner(int code)
420 {
421         return
422             (code == POD_tuner_mute) ||
423             (code == POD_tuner_freq) ||
424             (code == POD_tuner_note) || (code == POD_tuner_pitch);
425 }
426
427 /*
428         Get system parameter (as integer).
429         @param tuner non-zero, if code refers to a tuner parameter
430 */
431 static int pod_get_system_param_int(struct usb_line6_pod *pod, int *value,
432                                     int code, struct ValueWait *param, int sign)
433 {
434         char *sysex;
435         static const int size = 1;
436         int retval = 0;
437
438         if (((pod->prog_data.control[POD_tuner] & 0x40) == 0)
439             && pod_is_tuner(code))
440                 return -ENODEV;
441
442         /* send value request to device: */
443         param->value = POD_system_invalid;
444         sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEMREQ, size);
445
446         if (!sysex)
447                 return -ENOMEM;
448
449         sysex[SYSEX_DATA_OFS] = code;
450         line6_send_sysex_message(&pod->line6, sysex, size);
451         kfree(sysex);
452
453         /* wait for device to respond: */
454         retval =
455             wait_event_interruptible(param->wait,
456                                      param->value != POD_system_invalid);
457
458         if (retval < 0)
459                 return retval;
460
461         *value = sign ? (int)(signed short)param->value : (int)(unsigned short)
462             param->value;
463
464         if (*value == POD_system_invalid)
465                 *value = 0;     /* don't report uninitialized values */
466
467         return 0;
468 }
469
470 /*
471         Get system parameter (as string).
472         @param tuner non-zero, if code refers to a tuner parameter
473 */
474 static ssize_t pod_get_system_param_string(struct usb_line6_pod *pod, char *buf,
475                                            int code, struct ValueWait *param,
476                                            int sign)
477 {
478         int retval, value = 0;
479         retval = pod_get_system_param_int(pod, &value, code, param, sign);
480
481         if (retval < 0)
482                 return retval;
483
484         return sprintf(buf, "%d\n", value);
485 }
486
487 /*
488         Send system parameter (from integer).
489         @param tuner non-zero, if code refers to a tuner parameter
490 */
491 static int pod_set_system_param_int(struct usb_line6_pod *pod, int value,
492                                     int code)
493 {
494         char *sysex;
495         static const int size = 5;
496
497         if (((pod->prog_data.control[POD_tuner] & 0x40) == 0)
498             && pod_is_tuner(code))
499                 return -EINVAL;
500
501         /* send value to tuner: */
502         sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEM, size);
503         if (!sysex)
504                 return -ENOMEM;
505         sysex[SYSEX_DATA_OFS] = code;
506         sysex[SYSEX_DATA_OFS + 1] = (value >> 12) & 0x0f;
507         sysex[SYSEX_DATA_OFS + 2] = (value >> 8) & 0x0f;
508         sysex[SYSEX_DATA_OFS + 3] = (value >> 4) & 0x0f;
509         sysex[SYSEX_DATA_OFS + 4] = (value) & 0x0f;
510         line6_send_sysex_message(&pod->line6, sysex, size);
511         kfree(sysex);
512         return 0;
513 }
514
515 /*
516         Send system parameter (from string).
517         @param tuner non-zero, if code refers to a tuner parameter
518 */
519 static ssize_t pod_set_system_param_string(struct usb_line6_pod *pod,
520                                            const char *buf, int count, int code,
521                                            unsigned short mask)
522 {
523         int retval;
524         unsigned short value = simple_strtoul(buf, NULL, 10) & mask;
525         retval = pod_set_system_param_int(pod, value, code);
526         return (retval < 0) ? retval : count;
527 }
528
529 /*
530         "write" request on "finish" special file.
531 */
532 static ssize_t pod_set_finish(struct device *dev,
533                               struct device_attribute *attr,
534                               const char *buf, size_t count)
535 {
536         struct usb_interface *interface = to_usb_interface(dev);
537         struct usb_line6_pod *pod = usb_get_intfdata(interface);
538         int size = 0;
539         char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_FINISH, size);
540         if (!sysex)
541                 return 0;
542         line6_send_sysex_message(&pod->line6, sysex, size);
543         kfree(sysex);
544         return count;
545 }
546
547 /*
548         "write" request on "store_channel" special file.
549 */
550 static ssize_t pod_set_store_channel(struct device *dev,
551                                      struct device_attribute *attr,
552                                      const char *buf, size_t count)
553 {
554         return pod_send_store_command(dev, buf, count, 0x0000, 0x00c0);
555 }
556
557 /*
558         "write" request on "store_effects_setup" special file.
559 */
560 static ssize_t pod_set_store_effects_setup(struct device *dev,
561                                            struct device_attribute *attr,
562                                            const char *buf, size_t count)
563 {
564         return pod_send_store_command(dev, buf, count, 0x0080, 0x0080);
565 }
566
567 /*
568         "read" request on "midi_postprocess" special file.
569 */
570 static ssize_t pod_get_midi_postprocess(struct device *dev,
571                                         struct device_attribute *attr,
572                                         char *buf)
573 {
574         struct usb_interface *interface = to_usb_interface(dev);
575         struct usb_line6_pod *pod = usb_get_intfdata(interface);
576         return sprintf(buf, "%d\n", pod->midi_postprocess);
577 }
578
579 /*
580         "write" request on "midi_postprocess" special file.
581 */
582 static ssize_t pod_set_midi_postprocess(struct device *dev,
583                                         struct device_attribute *attr,
584                                         const char *buf, size_t count)
585 {
586         struct usb_interface *interface = to_usb_interface(dev);
587         struct usb_line6_pod *pod = usb_get_intfdata(interface);
588         u8 value;
589         int ret;
590
591         ret = kstrtou8(buf, 10, &value);
592         if (ret)
593                 return ret;
594
595         pod->midi_postprocess = value ? 1 : 0;
596         return count;
597 }
598
599 /*
600         "read" request on "serial_number" special file.
601 */
602 static ssize_t pod_get_serial_number(struct device *dev,
603                                      struct device_attribute *attr, char *buf)
604 {
605         struct usb_interface *interface = to_usb_interface(dev);
606         struct usb_line6_pod *pod = usb_get_intfdata(interface);
607         return sprintf(buf, "%d\n", pod->serial_number);
608 }
609
610 /*
611         "read" request on "firmware_version" special file.
612 */
613 static ssize_t pod_get_firmware_version(struct device *dev,
614                                         struct device_attribute *attr,
615                                         char *buf)
616 {
617         struct usb_interface *interface = to_usb_interface(dev);
618         struct usb_line6_pod *pod = usb_get_intfdata(interface);
619         return sprintf(buf, "%d.%02d\n", pod->firmware_version / 100,
620                        pod->firmware_version % 100);
621 }
622
623 /*
624         "read" request on "device_id" special file.
625 */
626 static ssize_t pod_get_device_id(struct device *dev,
627                                  struct device_attribute *attr, char *buf)
628 {
629         struct usb_interface *interface = to_usb_interface(dev);
630         struct usb_line6_pod *pod = usb_get_intfdata(interface);
631         return sprintf(buf, "%d\n", pod->device_id);
632 }
633
634 /*
635         POD startup procedure.
636         This is a sequence of functions with special requirements (e.g., must
637         not run immediately after initialization, must not run in interrupt
638         context). After the last one has finished, the device is ready to use.
639 */
640
641 static void pod_startup1(struct usb_line6_pod *pod)
642 {
643         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_INIT);
644
645         /* delay startup procedure: */
646         line6_start_timer(&pod->startup_timer, POD_STARTUP_DELAY, pod_startup2,
647                           (unsigned long)pod);
648 }
649
650 static void pod_startup2(unsigned long data)
651 {
652         struct usb_line6_pod *pod = (struct usb_line6_pod *)data;
653
654         /* schedule another startup procedure until startup is complete: */
655         if (pod->startup_progress >= POD_STARTUP_LAST)
656                 return;
657
658         pod->startup_progress = POD_STARTUP_DUMPREQ;
659         line6_start_timer(&pod->startup_timer, POD_STARTUP_DELAY, pod_startup2,
660                           (unsigned long)pod);
661
662         /* current channel dump: */
663         line6_dump_request_async(&pod->dumpreq, &pod->line6, 0,
664                                  LINE6_DUMP_CURRENT);
665 }
666
667 static void pod_startup3(struct usb_line6_pod *pod)
668 {
669         struct usb_line6 *line6 = &pod->line6;
670         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_VERSIONREQ);
671
672         /* request firmware version: */
673         line6_version_request_async(line6);
674 }
675
676 static void pod_startup4(struct usb_line6_pod *pod)
677 {
678         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_WORKQUEUE);
679
680         /* schedule work for global work queue: */
681         schedule_work(&pod->startup_work);
682 }
683
684 static void pod_startup5(struct work_struct *work)
685 {
686         struct usb_line6_pod *pod =
687             container_of(work, struct usb_line6_pod, startup_work);
688         struct usb_line6 *line6 = &pod->line6;
689
690         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_SETUP);
691
692         /* serial number: */
693         line6_read_serial_number(&pod->line6, &pod->serial_number);
694
695         /* ALSA audio interface: */
696         line6_register_audio(line6);
697
698         /* device files: */
699         line6_pod_create_files(pod->firmware_version,
700                                line6->properties->device_bit, line6->ifcdev);
701 }
702
703 #define POD_GET_SYSTEM_PARAM(code, sign) \
704 static ssize_t pod_get_ ## code(struct device *dev, \
705                                 struct device_attribute *attr, char *buf) \
706 { \
707         struct usb_interface *interface = to_usb_interface(dev); \
708         struct usb_line6_pod *pod = usb_get_intfdata(interface); \
709         return pod_get_system_param_string(pod, buf, POD_ ## code,      \
710                                            &pod->code, sign);           \
711 }
712
713 #define POD_GET_SET_SYSTEM_PARAM(code, mask, sign) \
714 POD_GET_SYSTEM_PARAM(code, sign) \
715 static ssize_t pod_set_ ## code(struct device *dev, \
716                                 struct device_attribute *attr, \
717                                 const char *buf, size_t count) \
718 { \
719         struct usb_interface *interface = to_usb_interface(dev); \
720         struct usb_line6_pod *pod = usb_get_intfdata(interface); \
721         return pod_set_system_param_string(pod, buf, count, POD_ ## code, mask); \
722 }
723
724 POD_GET_SET_SYSTEM_PARAM(routing, 0x0003, 0);
725 POD_GET_SET_SYSTEM_PARAM(tuner_mute, 0x0001, 0);
726 POD_GET_SET_SYSTEM_PARAM(tuner_freq, 0xffff, 0);
727 POD_GET_SYSTEM_PARAM(tuner_note, 1);
728 POD_GET_SYSTEM_PARAM(tuner_pitch, 1);
729
730 #undef GET_SET_SYSTEM_PARAM
731 #undef GET_SYSTEM_PARAM
732
733 /* POD special files: */
734 static DEVICE_ATTR(device_id, S_IRUGO, pod_get_device_id, line6_nop_write);
735 static DEVICE_ATTR(finish, S_IWUSR, line6_nop_read, pod_set_finish);
736 static DEVICE_ATTR(firmware_version, S_IRUGO, pod_get_firmware_version,
737                    line6_nop_write);
738 static DEVICE_ATTR(midi_postprocess, S_IWUSR | S_IRUGO,
739                    pod_get_midi_postprocess, pod_set_midi_postprocess);
740 static DEVICE_ATTR(routing, S_IWUSR | S_IRUGO, pod_get_routing,
741                    pod_set_routing);
742 static DEVICE_ATTR(serial_number, S_IRUGO, pod_get_serial_number,
743                    line6_nop_write);
744 static DEVICE_ATTR(store_channel, S_IWUSR, line6_nop_read,
745                    pod_set_store_channel);
746 static DEVICE_ATTR(store_effects_setup, S_IWUSR, line6_nop_read,
747                    pod_set_store_effects_setup);
748 static DEVICE_ATTR(tuner_freq, S_IWUSR | S_IRUGO, pod_get_tuner_freq,
749                    pod_set_tuner_freq);
750 static DEVICE_ATTR(tuner_mute, S_IWUSR | S_IRUGO, pod_get_tuner_mute,
751                    pod_set_tuner_mute);
752 static DEVICE_ATTR(tuner_note, S_IRUGO, pod_get_tuner_note, line6_nop_write);
753 static DEVICE_ATTR(tuner_pitch, S_IRUGO, pod_get_tuner_pitch, line6_nop_write);
754
755 #ifdef CONFIG_LINE6_USB_RAW
756 static DEVICE_ATTR(raw, S_IWUSR, line6_nop_read, line6_set_raw);
757 #endif
758
759 /* control info callback */
760 static int snd_pod_control_monitor_info(struct snd_kcontrol *kcontrol,
761                                         struct snd_ctl_elem_info *uinfo)
762 {
763         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
764         uinfo->count = 1;
765         uinfo->value.integer.min = 0;
766         uinfo->value.integer.max = 65535;
767         return 0;
768 }
769
770 /* control get callback */
771 static int snd_pod_control_monitor_get(struct snd_kcontrol *kcontrol,
772                                        struct snd_ctl_elem_value *ucontrol)
773 {
774         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
775         struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
776         ucontrol->value.integer.value[0] = pod->monitor_level;
777         return 0;
778 }
779
780 /* control put callback */
781 static int snd_pod_control_monitor_put(struct snd_kcontrol *kcontrol,
782                                        struct snd_ctl_elem_value *ucontrol)
783 {
784         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
785         struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
786
787         if (ucontrol->value.integer.value[0] == pod->monitor_level)
788                 return 0;
789
790         pod->monitor_level = ucontrol->value.integer.value[0];
791         pod_set_system_param_int(pod, ucontrol->value.integer.value[0],
792                                  POD_monitor_level);
793         return 1;
794 }
795
796 /* control definition */
797 static struct snd_kcontrol_new pod_control_monitor = {
798         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
799         .name = "Monitor Playback Volume",
800         .index = 0,
801         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
802         .info = snd_pod_control_monitor_info,
803         .get = snd_pod_control_monitor_get,
804         .put = snd_pod_control_monitor_put
805 };
806
807 /*
808         POD destructor.
809 */
810 static void pod_destruct(struct usb_interface *interface)
811 {
812         struct usb_line6_pod *pod = usb_get_intfdata(interface);
813
814         if (pod == NULL)
815                 return;
816         line6_cleanup_audio(&pod->line6);
817
818         del_timer(&pod->startup_timer);
819         cancel_work_sync(&pod->startup_work);
820
821         /* free dump request data: */
822         line6_dumpreq_destruct(&pod->dumpreq);
823 }
824
825 /*
826         Create sysfs entries.
827 */
828 static int pod_create_files2(struct device *dev)
829 {
830         int err;
831
832         CHECK_RETURN(device_create_file(dev, &dev_attr_device_id));
833         CHECK_RETURN(device_create_file(dev, &dev_attr_finish));
834         CHECK_RETURN(device_create_file(dev, &dev_attr_firmware_version));
835         CHECK_RETURN(device_create_file(dev, &dev_attr_midi_postprocess));
836         CHECK_RETURN(device_create_file(dev, &dev_attr_routing));
837         CHECK_RETURN(device_create_file(dev, &dev_attr_serial_number));
838         CHECK_RETURN(device_create_file(dev, &dev_attr_store_channel));
839         CHECK_RETURN(device_create_file(dev, &dev_attr_store_effects_setup));
840         CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_freq));
841         CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_mute));
842         CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_note));
843         CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_pitch));
844
845 #ifdef CONFIG_LINE6_USB_RAW
846         CHECK_RETURN(device_create_file(dev, &dev_attr_raw));
847 #endif
848
849         return 0;
850 }
851
852 /*
853          Try to init POD device.
854 */
855 static int pod_try_init(struct usb_interface *interface,
856                         struct usb_line6_pod *pod)
857 {
858         int err;
859         struct usb_line6 *line6 = &pod->line6;
860
861         init_timer(&pod->startup_timer);
862         INIT_WORK(&pod->startup_work, pod_startup5);
863
864         if ((interface == NULL) || (pod == NULL))
865                 return -ENODEV;
866
867         /* initialize wait queues: */
868         init_waitqueue_head(&pod->routing.wait);
869         init_waitqueue_head(&pod->tuner_mute.wait);
870         init_waitqueue_head(&pod->tuner_freq.wait);
871         init_waitqueue_head(&pod->tuner_note.wait);
872         init_waitqueue_head(&pod->tuner_pitch.wait);
873
874         /* initialize USB buffers: */
875         err = line6_dumpreq_init(&pod->dumpreq, pod_request_channel,
876                                  sizeof(pod_request_channel));
877         if (err < 0) {
878                 dev_err(&interface->dev, "Out of memory\n");
879                 return -ENOMEM;
880         }
881
882         /* create sysfs entries: */
883         err = pod_create_files2(&interface->dev);
884         if (err < 0)
885                 return err;
886
887         /* initialize audio system: */
888         err = line6_init_audio(line6);
889         if (err < 0)
890                 return err;
891
892         /* initialize MIDI subsystem: */
893         err = line6_init_midi(line6);
894         if (err < 0)
895                 return err;
896
897         /* initialize PCM subsystem: */
898         err = line6_init_pcm(line6, &pod_pcm_properties);
899         if (err < 0)
900                 return err;
901
902         /* register monitor control: */
903         err = snd_ctl_add(line6->card,
904                           snd_ctl_new1(&pod_control_monitor, line6->line6pcm));
905         if (err < 0)
906                 return err;
907
908         /*
909            When the sound card is registered at this point, the PODxt Live
910            displays "Invalid Code Error 07", so we do it later in the event
911            handler.
912          */
913
914         if (pod->line6.properties->capabilities & LINE6_BIT_CONTROL) {
915                 pod->monitor_level = POD_system_invalid;
916
917                 /* initiate startup procedure: */
918                 pod_startup1(pod);
919         }
920
921         return 0;
922 }
923
924 /*
925          Init POD device (and clean up in case of failure).
926 */
927 int line6_pod_init(struct usb_interface *interface, struct usb_line6_pod *pod)
928 {
929         int err = pod_try_init(interface, pod);
930
931         if (err < 0)
932                 pod_destruct(interface);
933
934         return err;
935 }
936
937 /*
938         POD device disconnected.
939 */
940 void line6_pod_disconnect(struct usb_interface *interface)
941 {
942         struct usb_line6_pod *pod;
943
944         if (interface == NULL)
945                 return;
946         pod = usb_get_intfdata(interface);
947
948         if (pod != NULL) {
949                 struct snd_line6_pcm *line6pcm = pod->line6.line6pcm;
950                 struct device *dev = &interface->dev;
951
952                 if (line6pcm != NULL)
953                         line6_pcm_disconnect(line6pcm);
954
955                 if (dev != NULL) {
956                         /* remove sysfs entries: */
957                         line6_pod_remove_files(pod->firmware_version,
958                                                pod->line6.
959                                                properties->device_bit, dev);
960
961                         device_remove_file(dev, &dev_attr_device_id);
962                         device_remove_file(dev, &dev_attr_finish);
963                         device_remove_file(dev, &dev_attr_firmware_version);
964                         device_remove_file(dev, &dev_attr_midi_postprocess);
965                         device_remove_file(dev, &dev_attr_routing);
966                         device_remove_file(dev, &dev_attr_serial_number);
967                         device_remove_file(dev, &dev_attr_store_channel);
968                         device_remove_file(dev, &dev_attr_store_effects_setup);
969                         device_remove_file(dev, &dev_attr_tuner_freq);
970                         device_remove_file(dev, &dev_attr_tuner_mute);
971                         device_remove_file(dev, &dev_attr_tuner_note);
972                         device_remove_file(dev, &dev_attr_tuner_pitch);
973
974 #ifdef CONFIG_LINE6_USB_RAW
975                         device_remove_file(dev, &dev_attr_raw);
976 #endif
977                 }
978         }
979
980         pod_destruct(interface);
981 }