OSDN Git Service

staging: line6: drop clip sysfs attr
[android-x86/kernel.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 /*
134         Mark all parameters as dirty and notify waiting processes.
135 */
136 static void pod_mark_batch_all_dirty(struct usb_line6_pod *pod)
137 {
138         int i;
139
140         for (i = 0; i < POD_CONTROL_SIZE; i++)
141                 set_bit(i, pod->param_dirty);
142 }
143
144 static char *pod_alloc_sysex_buffer(struct usb_line6_pod *pod, int code,
145                                     int size)
146 {
147         return line6_alloc_sysex_buffer(&pod->line6, POD_SYSEX_CODE, code,
148                                         size);
149 }
150
151 /*
152         Send channel dump data to the PODxt Pro.
153 */
154 static void pod_dump(struct usb_line6_pod *pod, const unsigned char *data)
155 {
156         int size = 1 + sizeof(pod->prog_data);
157         char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_DUMP, size);
158         if (!sysex)
159                 return;
160         /* Don't know what this is good for, but PODxt Pro transmits it, so we
161          * also do... */
162         sysex[SYSEX_DATA_OFS] = 5;
163         memcpy(sysex + SYSEX_DATA_OFS + 1, data, sizeof(pod->prog_data));
164         line6_send_sysex_message(&pod->line6, sysex, size);
165         memcpy(&pod->prog_data, data, sizeof(pod->prog_data));
166         pod_mark_batch_all_dirty(pod);
167         kfree(sysex);
168 }
169
170 /*
171         Store parameter value in driver memory and mark it as dirty.
172 */
173 static void pod_store_parameter(struct usb_line6_pod *pod, int param, int value)
174 {
175         pod->prog_data.control[param] = value;
176         set_bit(param, pod->param_dirty);
177         pod->dirty = 1;
178 }
179
180 /*
181         Handle SAVE button.
182 */
183 static void pod_save_button_pressed(struct usb_line6_pod *pod, int type,
184                                     int index)
185 {
186         pod->dirty = 0;
187         set_bit(POD_SAVE_PRESSED, &pod->atomic_flags);
188 }
189
190 /*
191         Process a completely received message.
192 */
193 void line6_pod_process_message(struct usb_line6_pod *pod)
194 {
195         const unsigned char *buf = pod->line6.buffer_message;
196
197         /* filter messages by type */
198         switch (buf[0] & 0xf0) {
199         case LINE6_PARAM_CHANGE:
200         case LINE6_PROGRAM_CHANGE:
201         case LINE6_SYSEX_BEGIN:
202                 break;          /* handle these further down */
203
204         default:
205                 return;         /* ignore all others */
206         }
207
208         /* process all remaining messages */
209         switch (buf[0]) {
210         case LINE6_PARAM_CHANGE | LINE6_CHANNEL_DEVICE:
211                 pod_store_parameter(pod, buf[1], buf[2]);
212                 /* intentionally no break here! */
213
214         case LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST:
215                 if ((buf[1] == POD_amp_model_setup) ||
216                     (buf[1] == POD_effect_setup))
217                         /* these also affect other settings */
218                         line6_dump_request_async(&pod->dumpreq, &pod->line6, 0,
219                                                  LINE6_DUMP_CURRENT);
220
221                 break;
222
223         case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_DEVICE:
224         case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST:
225                 pod->dirty = 0;
226                 set_bit(POD_CHANNEL_DIRTY, &pod->atomic_flags);
227                 line6_dump_request_async(&pod->dumpreq, &pod->line6, 0,
228                                          LINE6_DUMP_CURRENT);
229                 break;
230
231         case LINE6_SYSEX_BEGIN | LINE6_CHANNEL_DEVICE:
232         case LINE6_SYSEX_BEGIN | LINE6_CHANNEL_UNKNOWN:
233                 if (memcmp(buf + 1, line6_midi_id, sizeof(line6_midi_id)) == 0) {
234                         switch (buf[5]) {
235                         case POD_SYSEX_DUMP:
236                                 if (pod->line6.message_length ==
237                                     sizeof(pod->prog_data) + 7) {
238                                         switch (pod->dumpreq.in_progress) {
239                                         case LINE6_DUMP_CURRENT:
240                                                 memcpy(&pod->prog_data, buf + 7,
241                                                        sizeof(pod->prog_data));
242                                                 pod_mark_batch_all_dirty(pod);
243                                                 break;
244
245                                         case POD_DUMP_MEMORY:
246                                                 memcpy(&pod->prog_data_buf,
247                                                        buf + 7,
248                                                        sizeof
249                                                        (pod->prog_data_buf));
250                                                 break;
251
252                                         default:
253                                                 dev_dbg(pod->line6.ifcdev,
254                                                         "unknown dump code %02X\n",
255                                                         pod->dumpreq.in_progress);
256                                         }
257
258                                         line6_dump_finished(&pod->dumpreq);
259                                         pod_startup3(pod);
260                                 } else
261                                         dev_dbg(pod->line6.ifcdev,
262                                                 "wrong size of channel dump message (%d instead of %d)\n",
263                                                 pod->line6.message_length,
264                                                 (int)sizeof(pod->prog_data) +
265                                                 7);
266
267                                 break;
268
269                         case POD_SYSEX_SYSTEM:{
270                                         short value =
271                                             ((int)buf[7] << 12) | ((int)buf[8]
272                                                                    << 8) |
273                                             ((int)buf[9] << 4) | (int)buf[10];
274
275 #define PROCESS_SYSTEM_PARAM(x) \
276                                         case POD_ ## x: \
277                                                 pod->x.value = value; \
278                                                 wake_up(&pod->x.wait); \
279                                                 break;
280
281                                         switch (buf[6]) {
282                                                 PROCESS_SYSTEM_PARAM
283                                                     (monitor_level);
284                                                 PROCESS_SYSTEM_PARAM(routing);
285                                                 PROCESS_SYSTEM_PARAM
286                                                     (tuner_mute);
287                                                 PROCESS_SYSTEM_PARAM
288                                                     (tuner_freq);
289                                                 PROCESS_SYSTEM_PARAM
290                                                     (tuner_note);
291                                                 PROCESS_SYSTEM_PARAM
292                                                     (tuner_pitch);
293
294 #undef PROCESS_SYSTEM_PARAM
295
296                                         default:
297                                                 dev_dbg(pod->line6.ifcdev,
298                                                         "unknown tuner/system response %02X\n",
299                                                         buf[6]);
300                                         }
301
302                                         break;
303                                 }
304
305                         case POD_SYSEX_FINISH:
306                                 /* do we need to respond to this? */
307                                 break;
308
309                         case POD_SYSEX_SAVE:
310                                 pod_save_button_pressed(pod, buf[6], buf[7]);
311                                 break;
312
313                         case POD_SYSEX_STORE:
314                                 dev_dbg(pod->line6.ifcdev,
315                                         "message %02X not yet implemented\n",
316                                         buf[5]);
317                                 break;
318
319                         default:
320                                 dev_dbg(pod->line6.ifcdev,
321                                         "unknown sysex message %02X\n",
322                                         buf[5]);
323                         }
324                 } else
325                     if (memcmp
326                         (buf, pod_version_header,
327                          sizeof(pod_version_header)) == 0) {
328                         pod->firmware_version =
329                             buf[13] * 100 + buf[14] * 10 + buf[15];
330                         pod->device_id =
331                             ((int)buf[8] << 16) | ((int)buf[9] << 8) | (int)
332                             buf[10];
333                         pod_startup4(pod);
334                 } else
335                         dev_dbg(pod->line6.ifcdev, "unknown sysex header\n");
336
337                 break;
338
339         case LINE6_SYSEX_END:
340                 break;
341
342         default:
343                 dev_dbg(pod->line6.ifcdev, "POD: unknown message %02X\n",
344                         buf[0]);
345         }
346 }
347
348 /*
349         Detect some cases that require a channel dump after sending a command to the
350         device. Important notes:
351         *) The actual dump request can not be sent here since we are not allowed to
352         wait for the completion of the first message in this context, and sending
353         the dump request before completion of the previous message leaves the POD
354         in an undefined state. The dump request will be sent when the echoed
355         commands are received.
356         *) This method fails if a param change message is "chopped" after the first
357         byte.
358 */
359 void line6_pod_midi_postprocess(struct usb_line6_pod *pod, unsigned char *data,
360                                 int length)
361 {
362         int i;
363
364         if (!pod->midi_postprocess)
365                 return;
366
367         for (i = 0; i < length; ++i) {
368                 if (data[i] == (LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST)) {
369                         line6_invalidate_current(&pod->dumpreq);
370                         break;
371                 } else
372                     if ((data[i] == (LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST))
373                         && (i < length - 1))
374                         if ((data[i + 1] == POD_amp_model_setup)
375                             || (data[i + 1] == POD_effect_setup)) {
376                                 line6_invalidate_current(&pod->dumpreq);
377                                 break;
378                         }
379         }
380 }
381
382 /*
383         Transmit PODxt Pro control parameter.
384 */
385 void line6_pod_transmit_parameter(struct usb_line6_pod *pod, int param,
386                                   u8 value)
387 {
388         if (line6_transmit_parameter(&pod->line6, param, value) == 0)
389                 pod_store_parameter(pod, param, value);
390
391         if ((param == POD_amp_model_setup) || (param == POD_effect_setup))      /* these also affect other settings */
392                 line6_invalidate_current(&pod->dumpreq);
393 }
394
395 /*
396         Resolve value to memory location.
397 */
398 static int pod_resolve(const char *buf, short block0, short block1,
399                        unsigned char *location)
400 {
401         u8 value;
402         short block;
403         int ret;
404
405         ret = kstrtou8(buf, 10, &value);
406         if (ret)
407                 return ret;
408
409         block = (value < 0x40) ? block0 : block1;
410         value &= 0x3f;
411         location[0] = block >> 7;
412         location[1] = value | (block & 0x7f);
413         return 0;
414 }
415
416 /*
417         Send command to store channel/effects setup/amp setup to PODxt Pro.
418 */
419 static ssize_t pod_send_store_command(struct device *dev, const char *buf,
420                                       size_t count, short block0, short block1)
421 {
422         struct usb_interface *interface = to_usb_interface(dev);
423         struct usb_line6_pod *pod = usb_get_intfdata(interface);
424         int ret;
425         int size = 3 + sizeof(pod->prog_data_buf);
426         char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_STORE, size);
427
428         if (!sysex)
429                 return 0;
430
431         sysex[SYSEX_DATA_OFS] = 5;      /* see pod_dump() */
432         ret = pod_resolve(buf, block0, block1, sysex + SYSEX_DATA_OFS + 1);
433         if (ret) {
434                 kfree(sysex);
435                 return ret;
436         }
437
438         memcpy(sysex + SYSEX_DATA_OFS + 3, &pod->prog_data_buf,
439                sizeof(pod->prog_data_buf));
440
441         line6_send_sysex_message(&pod->line6, sysex, size);
442         kfree(sysex);
443         /* needs some delay here on AMD64 platform */
444         return count;
445 }
446
447 /*
448         Send command to retrieve channel/effects setup/amp setup to PODxt Pro.
449 */
450 static ssize_t pod_send_retrieve_command(struct device *dev, const char *buf,
451                                          size_t count, short block0,
452                                          short block1)
453 {
454         struct usb_interface *interface = to_usb_interface(dev);
455         struct usb_line6_pod *pod = usb_get_intfdata(interface);
456         int ret;
457         int size = 4;
458         char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_DUMPMEM, size);
459
460         if (!sysex)
461                 return 0;
462
463         ret = pod_resolve(buf, block0, block1, sysex + SYSEX_DATA_OFS);
464         if (ret) {
465                 kfree(sysex);
466                 return ret;
467         }
468         sysex[SYSEX_DATA_OFS + 2] = 0;
469         sysex[SYSEX_DATA_OFS + 3] = 0;
470         line6_dump_started(&pod->dumpreq, POD_DUMP_MEMORY);
471
472         if (line6_send_sysex_message(&pod->line6, sysex, size) < size)
473                 line6_dump_finished(&pod->dumpreq);
474
475         kfree(sysex);
476         /* needs some delay here on AMD64 platform */
477         return count;
478 }
479
480 /*
481         Generic get name function.
482 */
483 static ssize_t get_name_generic(struct usb_line6_pod *pod, const char *str,
484                                 char *buf)
485 {
486         int length = 0;
487         const char *p1;
488         char *p2;
489         char *last_non_space = buf;
490
491         int retval = line6_dump_wait_interruptible(&pod->dumpreq);
492         if (retval < 0)
493                 return retval;
494
495         for (p1 = str, p2 = buf; *p1; ++p1, ++p2) {
496                 *p2 = *p1;
497                 if (*p2 != ' ')
498                         last_non_space = p2;
499                 if (++length == POD_NAME_LENGTH)
500                         break;
501         }
502
503         *(last_non_space + 1) = '\n';
504         return last_non_space - buf + 2;
505 }
506
507 /*
508         "read" request on "name" special file.
509 */
510 static ssize_t pod_get_name(struct device *dev, struct device_attribute *attr,
511                             char *buf)
512 {
513         struct usb_interface *interface = to_usb_interface(dev);
514         struct usb_line6_pod *pod = usb_get_intfdata(interface);
515         return get_name_generic(pod, pod->prog_data.header + POD_NAME_OFFSET,
516                                 buf);
517 }
518
519 /*
520         "read" request on "name" special file.
521 */
522 static ssize_t pod_get_name_buf(struct device *dev,
523                                 struct device_attribute *attr, char *buf)
524 {
525         struct usb_interface *interface = to_usb_interface(dev);
526         struct usb_line6_pod *pod = usb_get_intfdata(interface);
527         return get_name_generic(pod,
528                                 pod->prog_data_buf.header + POD_NAME_OFFSET,
529                                 buf);
530 }
531
532 /*
533         "read" request on "dump" special file.
534 */
535 static ssize_t pod_get_dump(struct device *dev, struct device_attribute *attr,
536                             char *buf)
537 {
538         struct usb_interface *interface = to_usb_interface(dev);
539         struct usb_line6_pod *pod = usb_get_intfdata(interface);
540         int retval = line6_dump_wait_interruptible(&pod->dumpreq);
541         if (retval < 0)
542                 return retval;
543         memcpy(buf, &pod->prog_data, sizeof(pod->prog_data));
544         return sizeof(pod->prog_data);
545 }
546
547 /*
548         "write" request on "dump" special file.
549 */
550 static ssize_t pod_set_dump(struct device *dev, struct device_attribute *attr,
551                             const char *buf, size_t count)
552 {
553         struct usb_interface *interface = to_usb_interface(dev);
554         struct usb_line6_pod *pod = usb_get_intfdata(interface);
555
556         if (count != sizeof(pod->prog_data)) {
557                 dev_err(pod->line6.ifcdev,
558                         "data block must be exactly %d bytes\n",
559                         (int)sizeof(pod->prog_data));
560                 return -EINVAL;
561         }
562
563         pod_dump(pod, buf);
564         return sizeof(pod->prog_data);
565 }
566
567 /*
568         Identify system parameters related to the tuner.
569 */
570 static bool pod_is_tuner(int code)
571 {
572         return
573             (code == POD_tuner_mute) ||
574             (code == POD_tuner_freq) ||
575             (code == POD_tuner_note) || (code == POD_tuner_pitch);
576 }
577
578 /*
579         Get system parameter (as integer).
580         @param tuner non-zero, if code refers to a tuner parameter
581 */
582 static int pod_get_system_param_int(struct usb_line6_pod *pod, int *value,
583                                     int code, struct ValueWait *param, int sign)
584 {
585         char *sysex;
586         static const int size = 1;
587         int retval = 0;
588
589         if (((pod->prog_data.control[POD_tuner] & 0x40) == 0)
590             && pod_is_tuner(code))
591                 return -ENODEV;
592
593         /* send value request to device: */
594         param->value = POD_system_invalid;
595         sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEMREQ, size);
596
597         if (!sysex)
598                 return -ENOMEM;
599
600         sysex[SYSEX_DATA_OFS] = code;
601         line6_send_sysex_message(&pod->line6, sysex, size);
602         kfree(sysex);
603
604         /* wait for device to respond: */
605         retval =
606             wait_event_interruptible(param->wait,
607                                      param->value != POD_system_invalid);
608
609         if (retval < 0)
610                 return retval;
611
612         *value = sign ? (int)(signed short)param->value : (int)(unsigned short)
613             param->value;
614
615         if (*value == POD_system_invalid)
616                 *value = 0;     /* don't report uninitialized values */
617
618         return 0;
619 }
620
621 /*
622         Get system parameter (as string).
623         @param tuner non-zero, if code refers to a tuner parameter
624 */
625 static ssize_t pod_get_system_param_string(struct usb_line6_pod *pod, char *buf,
626                                            int code, struct ValueWait *param,
627                                            int sign)
628 {
629         int retval, value = 0;
630         retval = pod_get_system_param_int(pod, &value, code, param, sign);
631
632         if (retval < 0)
633                 return retval;
634
635         return sprintf(buf, "%d\n", value);
636 }
637
638 /*
639         Send system parameter (from integer).
640         @param tuner non-zero, if code refers to a tuner parameter
641 */
642 static int pod_set_system_param_int(struct usb_line6_pod *pod, int value,
643                                     int code)
644 {
645         char *sysex;
646         static const int size = 5;
647
648         if (((pod->prog_data.control[POD_tuner] & 0x40) == 0)
649             && pod_is_tuner(code))
650                 return -EINVAL;
651
652         /* send value to tuner: */
653         sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEM, size);
654         if (!sysex)
655                 return -ENOMEM;
656         sysex[SYSEX_DATA_OFS] = code;
657         sysex[SYSEX_DATA_OFS + 1] = (value >> 12) & 0x0f;
658         sysex[SYSEX_DATA_OFS + 2] = (value >> 8) & 0x0f;
659         sysex[SYSEX_DATA_OFS + 3] = (value >> 4) & 0x0f;
660         sysex[SYSEX_DATA_OFS + 4] = (value) & 0x0f;
661         line6_send_sysex_message(&pod->line6, sysex, size);
662         kfree(sysex);
663         return 0;
664 }
665
666 /*
667         Send system parameter (from string).
668         @param tuner non-zero, if code refers to a tuner parameter
669 */
670 static ssize_t pod_set_system_param_string(struct usb_line6_pod *pod,
671                                            const char *buf, int count, int code,
672                                            unsigned short mask)
673 {
674         int retval;
675         unsigned short value = simple_strtoul(buf, NULL, 10) & mask;
676         retval = pod_set_system_param_int(pod, value, code);
677         return (retval < 0) ? retval : count;
678 }
679
680 /*
681         "read" request on "dump_buf" special file.
682 */
683 static ssize_t pod_get_dump_buf(struct device *dev,
684                                 struct device_attribute *attr, char *buf)
685 {
686         struct usb_interface *interface = to_usb_interface(dev);
687         struct usb_line6_pod *pod = usb_get_intfdata(interface);
688         int retval = line6_dump_wait_interruptible(&pod->dumpreq);
689         if (retval < 0)
690                 return retval;
691         memcpy(buf, &pod->prog_data_buf, sizeof(pod->prog_data_buf));
692         return sizeof(pod->prog_data_buf);
693 }
694
695 /*
696         "write" request on "dump_buf" special file.
697 */
698 static ssize_t pod_set_dump_buf(struct device *dev,
699                                 struct device_attribute *attr,
700                                 const char *buf, size_t count)
701 {
702         struct usb_interface *interface = to_usb_interface(dev);
703         struct usb_line6_pod *pod = usb_get_intfdata(interface);
704
705         if (count != sizeof(pod->prog_data)) {
706                 dev_err(pod->line6.ifcdev,
707                         "data block must be exactly %d bytes\n",
708                         (int)sizeof(pod->prog_data));
709                 return -EINVAL;
710         }
711
712         memcpy(&pod->prog_data_buf, buf, sizeof(pod->prog_data));
713         return sizeof(pod->prog_data);
714 }
715
716 /*
717         "write" request on "finish" special file.
718 */
719 static ssize_t pod_set_finish(struct device *dev,
720                               struct device_attribute *attr,
721                               const char *buf, size_t count)
722 {
723         struct usb_interface *interface = to_usb_interface(dev);
724         struct usb_line6_pod *pod = usb_get_intfdata(interface);
725         int size = 0;
726         char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_FINISH, size);
727         if (!sysex)
728                 return 0;
729         line6_send_sysex_message(&pod->line6, sysex, size);
730         kfree(sysex);
731         return count;
732 }
733
734 /*
735         "write" request on "store_channel" special file.
736 */
737 static ssize_t pod_set_store_channel(struct device *dev,
738                                      struct device_attribute *attr,
739                                      const char *buf, size_t count)
740 {
741         return pod_send_store_command(dev, buf, count, 0x0000, 0x00c0);
742 }
743
744 /*
745         "write" request on "store_effects_setup" special file.
746 */
747 static ssize_t pod_set_store_effects_setup(struct device *dev,
748                                            struct device_attribute *attr,
749                                            const char *buf, size_t count)
750 {
751         return pod_send_store_command(dev, buf, count, 0x0080, 0x0080);
752 }
753
754 /*
755         "write" request on "store_amp_setup" special file.
756 */
757 static ssize_t pod_set_store_amp_setup(struct device *dev,
758                                        struct device_attribute *attr,
759                                        const char *buf, size_t count)
760 {
761         return pod_send_store_command(dev, buf, count, 0x0040, 0x0100);
762 }
763
764 /*
765         "write" request on "retrieve_channel" special file.
766 */
767 static ssize_t pod_set_retrieve_channel(struct device *dev,
768                                         struct device_attribute *attr,
769                                         const char *buf, size_t count)
770 {
771         return pod_send_retrieve_command(dev, buf, count, 0x0000, 0x00c0);
772 }
773
774 /*
775         "write" request on "retrieve_effects_setup" special file.
776 */
777 static ssize_t pod_set_retrieve_effects_setup(struct device *dev,
778                                               struct device_attribute *attr,
779                                               const char *buf, size_t count)
780 {
781         return pod_send_retrieve_command(dev, buf, count, 0x0080, 0x0080);
782 }
783
784 /*
785         "write" request on "retrieve_amp_setup" special file.
786 */
787 static ssize_t pod_set_retrieve_amp_setup(struct device *dev,
788                                           struct device_attribute *attr,
789                                           const char *buf, size_t count)
790 {
791         return pod_send_retrieve_command(dev, buf, count, 0x0040, 0x0100);
792 }
793
794 /*
795         "read" request on "dirty" special file.
796 */
797 static ssize_t pod_get_dirty(struct device *dev, struct device_attribute *attr,
798                              char *buf)
799 {
800         struct usb_interface *interface = to_usb_interface(dev);
801         struct usb_line6_pod *pod = usb_get_intfdata(interface);
802         buf[0] = pod->dirty ? '1' : '0';
803         buf[1] = '\n';
804         return 2;
805 }
806
807 /*
808         "read" request on "midi_postprocess" special file.
809 */
810 static ssize_t pod_get_midi_postprocess(struct device *dev,
811                                         struct device_attribute *attr,
812                                         char *buf)
813 {
814         struct usb_interface *interface = to_usb_interface(dev);
815         struct usb_line6_pod *pod = usb_get_intfdata(interface);
816         return sprintf(buf, "%d\n", pod->midi_postprocess);
817 }
818
819 /*
820         "write" request on "midi_postprocess" special file.
821 */
822 static ssize_t pod_set_midi_postprocess(struct device *dev,
823                                         struct device_attribute *attr,
824                                         const char *buf, size_t count)
825 {
826         struct usb_interface *interface = to_usb_interface(dev);
827         struct usb_line6_pod *pod = usb_get_intfdata(interface);
828         u8 value;
829         int ret;
830
831         ret = kstrtou8(buf, 10, &value);
832         if (ret)
833                 return ret;
834
835         pod->midi_postprocess = value ? 1 : 0;
836         return count;
837 }
838
839 /*
840         "read" request on "serial_number" special file.
841 */
842 static ssize_t pod_get_serial_number(struct device *dev,
843                                      struct device_attribute *attr, char *buf)
844 {
845         struct usb_interface *interface = to_usb_interface(dev);
846         struct usb_line6_pod *pod = usb_get_intfdata(interface);
847         return sprintf(buf, "%d\n", pod->serial_number);
848 }
849
850 /*
851         "read" request on "firmware_version" special file.
852 */
853 static ssize_t pod_get_firmware_version(struct device *dev,
854                                         struct device_attribute *attr,
855                                         char *buf)
856 {
857         struct usb_interface *interface = to_usb_interface(dev);
858         struct usb_line6_pod *pod = usb_get_intfdata(interface);
859         return sprintf(buf, "%d.%02d\n", pod->firmware_version / 100,
860                        pod->firmware_version % 100);
861 }
862
863 /*
864         "read" request on "device_id" special file.
865 */
866 static ssize_t pod_get_device_id(struct device *dev,
867                                  struct device_attribute *attr, char *buf)
868 {
869         struct usb_interface *interface = to_usb_interface(dev);
870         struct usb_line6_pod *pod = usb_get_intfdata(interface);
871         return sprintf(buf, "%d\n", pod->device_id);
872 }
873
874 /*
875         POD startup procedure.
876         This is a sequence of functions with special requirements (e.g., must
877         not run immediately after initialization, must not run in interrupt
878         context). After the last one has finished, the device is ready to use.
879 */
880
881 static void pod_startup1(struct usb_line6_pod *pod)
882 {
883         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_INIT);
884
885         /* delay startup procedure: */
886         line6_start_timer(&pod->startup_timer, POD_STARTUP_DELAY, pod_startup2,
887                           (unsigned long)pod);
888 }
889
890 static void pod_startup2(unsigned long data)
891 {
892         struct usb_line6_pod *pod = (struct usb_line6_pod *)data;
893
894         /* schedule another startup procedure until startup is complete: */
895         if (pod->startup_progress >= POD_STARTUP_LAST)
896                 return;
897
898         pod->startup_progress = POD_STARTUP_DUMPREQ;
899         line6_start_timer(&pod->startup_timer, POD_STARTUP_DELAY, pod_startup2,
900                           (unsigned long)pod);
901
902         /* current channel dump: */
903         line6_dump_request_async(&pod->dumpreq, &pod->line6, 0,
904                                  LINE6_DUMP_CURRENT);
905 }
906
907 static void pod_startup3(struct usb_line6_pod *pod)
908 {
909         struct usb_line6 *line6 = &pod->line6;
910         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_VERSIONREQ);
911
912         /* request firmware version: */
913         line6_version_request_async(line6);
914 }
915
916 static void pod_startup4(struct usb_line6_pod *pod)
917 {
918         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_WORKQUEUE);
919
920         /* schedule work for global work queue: */
921         schedule_work(&pod->startup_work);
922 }
923
924 static void pod_startup5(struct work_struct *work)
925 {
926         struct usb_line6_pod *pod =
927             container_of(work, struct usb_line6_pod, startup_work);
928         struct usb_line6 *line6 = &pod->line6;
929
930         CHECK_STARTUP_PROGRESS(pod->startup_progress, POD_STARTUP_SETUP);
931
932         /* serial number: */
933         line6_read_serial_number(&pod->line6, &pod->serial_number);
934
935         /* ALSA audio interface: */
936         line6_register_audio(line6);
937
938         /* device files: */
939         line6_pod_create_files(pod->firmware_version,
940                                line6->properties->device_bit, line6->ifcdev);
941 }
942
943 #define POD_GET_SYSTEM_PARAM(code, sign) \
944 static ssize_t pod_get_ ## code(struct device *dev, \
945                                 struct device_attribute *attr, char *buf) \
946 { \
947         struct usb_interface *interface = to_usb_interface(dev); \
948         struct usb_line6_pod *pod = usb_get_intfdata(interface); \
949         return pod_get_system_param_string(pod, buf, POD_ ## code,      \
950                                            &pod->code, sign);           \
951 }
952
953 #define POD_GET_SET_SYSTEM_PARAM(code, mask, sign) \
954 POD_GET_SYSTEM_PARAM(code, sign) \
955 static ssize_t pod_set_ ## code(struct device *dev, \
956                                 struct device_attribute *attr, \
957                                 const char *buf, size_t count) \
958 { \
959         struct usb_interface *interface = to_usb_interface(dev); \
960         struct usb_line6_pod *pod = usb_get_intfdata(interface); \
961         return pod_set_system_param_string(pod, buf, count, POD_ ## code, mask); \
962 }
963
964 POD_GET_SET_SYSTEM_PARAM(monitor_level, 0xffff, 0);
965 POD_GET_SET_SYSTEM_PARAM(routing, 0x0003, 0);
966 POD_GET_SET_SYSTEM_PARAM(tuner_mute, 0x0001, 0);
967 POD_GET_SET_SYSTEM_PARAM(tuner_freq, 0xffff, 0);
968 POD_GET_SYSTEM_PARAM(tuner_note, 1);
969 POD_GET_SYSTEM_PARAM(tuner_pitch, 1);
970
971 #undef GET_SET_SYSTEM_PARAM
972 #undef GET_SYSTEM_PARAM
973
974 /* POD special files: */
975 static DEVICE_ATTR(device_id, S_IRUGO, pod_get_device_id, line6_nop_write);
976 static DEVICE_ATTR(dirty, S_IRUGO, pod_get_dirty, line6_nop_write);
977 static DEVICE_ATTR(dump, S_IWUSR | S_IRUGO, pod_get_dump, pod_set_dump);
978 static DEVICE_ATTR(dump_buf, S_IWUSR | S_IRUGO, pod_get_dump_buf,
979                    pod_set_dump_buf);
980 static DEVICE_ATTR(finish, S_IWUSR, line6_nop_read, pod_set_finish);
981 static DEVICE_ATTR(firmware_version, S_IRUGO, pod_get_firmware_version,
982                    line6_nop_write);
983 static DEVICE_ATTR(midi_postprocess, S_IWUSR | S_IRUGO,
984                    pod_get_midi_postprocess, pod_set_midi_postprocess);
985 static DEVICE_ATTR(monitor_level, S_IWUSR | S_IRUGO, pod_get_monitor_level,
986                    pod_set_monitor_level);
987 static DEVICE_ATTR(name, S_IRUGO, pod_get_name, line6_nop_write);
988 static DEVICE_ATTR(name_buf, S_IRUGO, pod_get_name_buf, line6_nop_write);
989 static DEVICE_ATTR(retrieve_amp_setup, S_IWUSR, line6_nop_read,
990                    pod_set_retrieve_amp_setup);
991 static DEVICE_ATTR(retrieve_channel, S_IWUSR, line6_nop_read,
992                    pod_set_retrieve_channel);
993 static DEVICE_ATTR(retrieve_effects_setup, S_IWUSR, line6_nop_read,
994                    pod_set_retrieve_effects_setup);
995 static DEVICE_ATTR(routing, S_IWUSR | S_IRUGO, pod_get_routing,
996                    pod_set_routing);
997 static DEVICE_ATTR(serial_number, S_IRUGO, pod_get_serial_number,
998                    line6_nop_write);
999 static DEVICE_ATTR(store_amp_setup, S_IWUSR, line6_nop_read,
1000                    pod_set_store_amp_setup);
1001 static DEVICE_ATTR(store_channel, S_IWUSR, line6_nop_read,
1002                    pod_set_store_channel);
1003 static DEVICE_ATTR(store_effects_setup, S_IWUSR, line6_nop_read,
1004                    pod_set_store_effects_setup);
1005 static DEVICE_ATTR(tuner_freq, S_IWUSR | S_IRUGO, pod_get_tuner_freq,
1006                    pod_set_tuner_freq);
1007 static DEVICE_ATTR(tuner_mute, S_IWUSR | S_IRUGO, pod_get_tuner_mute,
1008                    pod_set_tuner_mute);
1009 static DEVICE_ATTR(tuner_note, S_IRUGO, pod_get_tuner_note, line6_nop_write);
1010 static DEVICE_ATTR(tuner_pitch, S_IRUGO, pod_get_tuner_pitch, line6_nop_write);
1011
1012 #ifdef CONFIG_LINE6_USB_RAW
1013 static DEVICE_ATTR(raw, S_IWUSR, line6_nop_read, line6_set_raw);
1014 #endif
1015
1016 /* control info callback */
1017 static int snd_pod_control_monitor_info(struct snd_kcontrol *kcontrol,
1018                                         struct snd_ctl_elem_info *uinfo)
1019 {
1020         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1021         uinfo->count = 1;
1022         uinfo->value.integer.min = 0;
1023         uinfo->value.integer.max = 65535;
1024         return 0;
1025 }
1026
1027 /* control get callback */
1028 static int snd_pod_control_monitor_get(struct snd_kcontrol *kcontrol,
1029                                        struct snd_ctl_elem_value *ucontrol)
1030 {
1031         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
1032         struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
1033         ucontrol->value.integer.value[0] = pod->monitor_level.value;
1034         return 0;
1035 }
1036
1037 /* control put callback */
1038 static int snd_pod_control_monitor_put(struct snd_kcontrol *kcontrol,
1039                                        struct snd_ctl_elem_value *ucontrol)
1040 {
1041         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
1042         struct usb_line6_pod *pod = (struct usb_line6_pod *)line6pcm->line6;
1043
1044         if (ucontrol->value.integer.value[0] == pod->monitor_level.value)
1045                 return 0;
1046
1047         pod->monitor_level.value = ucontrol->value.integer.value[0];
1048         pod_set_system_param_int(pod, ucontrol->value.integer.value[0],
1049                                  POD_monitor_level);
1050         return 1;
1051 }
1052
1053 /* control definition */
1054 static struct snd_kcontrol_new pod_control_monitor = {
1055         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1056         .name = "Monitor Playback Volume",
1057         .index = 0,
1058         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1059         .info = snd_pod_control_monitor_info,
1060         .get = snd_pod_control_monitor_get,
1061         .put = snd_pod_control_monitor_put
1062 };
1063
1064 /*
1065         POD destructor.
1066 */
1067 static void pod_destruct(struct usb_interface *interface)
1068 {
1069         struct usb_line6_pod *pod = usb_get_intfdata(interface);
1070
1071         if (pod == NULL)
1072                 return;
1073         line6_cleanup_audio(&pod->line6);
1074
1075         del_timer(&pod->startup_timer);
1076         cancel_work_sync(&pod->startup_work);
1077
1078         /* free dump request data: */
1079         line6_dumpreq_destruct(&pod->dumpreq);
1080 }
1081
1082 /*
1083         Create sysfs entries.
1084 */
1085 static int pod_create_files2(struct device *dev)
1086 {
1087         int err;
1088
1089         CHECK_RETURN(device_create_file(dev, &dev_attr_device_id));
1090         CHECK_RETURN(device_create_file(dev, &dev_attr_dirty));
1091         CHECK_RETURN(device_create_file(dev, &dev_attr_dump));
1092         CHECK_RETURN(device_create_file(dev, &dev_attr_dump_buf));
1093         CHECK_RETURN(device_create_file(dev, &dev_attr_finish));
1094         CHECK_RETURN(device_create_file(dev, &dev_attr_firmware_version));
1095         CHECK_RETURN(device_create_file(dev, &dev_attr_midi_postprocess));
1096         CHECK_RETURN(device_create_file(dev, &dev_attr_monitor_level));
1097         CHECK_RETURN(device_create_file(dev, &dev_attr_name));
1098         CHECK_RETURN(device_create_file(dev, &dev_attr_name_buf));
1099         CHECK_RETURN(device_create_file(dev, &dev_attr_retrieve_amp_setup));
1100         CHECK_RETURN(device_create_file(dev, &dev_attr_retrieve_channel));
1101         CHECK_RETURN(device_create_file(dev, &dev_attr_retrieve_effects_setup));
1102         CHECK_RETURN(device_create_file(dev, &dev_attr_routing));
1103         CHECK_RETURN(device_create_file(dev, &dev_attr_serial_number));
1104         CHECK_RETURN(device_create_file(dev, &dev_attr_store_amp_setup));
1105         CHECK_RETURN(device_create_file(dev, &dev_attr_store_channel));
1106         CHECK_RETURN(device_create_file(dev, &dev_attr_store_effects_setup));
1107         CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_freq));
1108         CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_mute));
1109         CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_note));
1110         CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_pitch));
1111
1112 #ifdef CONFIG_LINE6_USB_RAW
1113         CHECK_RETURN(device_create_file(dev, &dev_attr_raw));
1114 #endif
1115
1116         return 0;
1117 }
1118
1119 /*
1120          Try to init POD device.
1121 */
1122 static int pod_try_init(struct usb_interface *interface,
1123                         struct usb_line6_pod *pod)
1124 {
1125         int err;
1126         struct usb_line6 *line6 = &pod->line6;
1127
1128         init_timer(&pod->startup_timer);
1129         INIT_WORK(&pod->startup_work, pod_startup5);
1130
1131         if ((interface == NULL) || (pod == NULL))
1132                 return -ENODEV;
1133
1134         /* initialize wait queues: */
1135         init_waitqueue_head(&pod->monitor_level.wait);
1136         init_waitqueue_head(&pod->routing.wait);
1137         init_waitqueue_head(&pod->tuner_mute.wait);
1138         init_waitqueue_head(&pod->tuner_freq.wait);
1139         init_waitqueue_head(&pod->tuner_note.wait);
1140         init_waitqueue_head(&pod->tuner_pitch.wait);
1141
1142         memset(pod->param_dirty, 0xff, sizeof(pod->param_dirty));
1143
1144         /* initialize USB buffers: */
1145         err = line6_dumpreq_init(&pod->dumpreq, pod_request_channel,
1146                                  sizeof(pod_request_channel));
1147         if (err < 0) {
1148                 dev_err(&interface->dev, "Out of memory\n");
1149                 return -ENOMEM;
1150         }
1151
1152         /* create sysfs entries: */
1153         err = pod_create_files2(&interface->dev);
1154         if (err < 0)
1155                 return err;
1156
1157         /* initialize audio system: */
1158         err = line6_init_audio(line6);
1159         if (err < 0)
1160                 return err;
1161
1162         /* initialize MIDI subsystem: */
1163         err = line6_init_midi(line6);
1164         if (err < 0)
1165                 return err;
1166
1167         /* initialize PCM subsystem: */
1168         err = line6_init_pcm(line6, &pod_pcm_properties);
1169         if (err < 0)
1170                 return err;
1171
1172         /* register monitor control: */
1173         err = snd_ctl_add(line6->card,
1174                           snd_ctl_new1(&pod_control_monitor, line6->line6pcm));
1175         if (err < 0)
1176                 return err;
1177
1178         /*
1179            When the sound card is registered at this point, the PODxt Live
1180            displays "Invalid Code Error 07", so we do it later in the event
1181            handler.
1182          */
1183
1184         if (pod->line6.properties->capabilities & LINE6_BIT_CONTROL) {
1185                 pod->monitor_level.value = POD_system_invalid;
1186
1187                 /* initiate startup procedure: */
1188                 pod_startup1(pod);
1189         }
1190
1191         return 0;
1192 }
1193
1194 /*
1195          Init POD device (and clean up in case of failure).
1196 */
1197 int line6_pod_init(struct usb_interface *interface, struct usb_line6_pod *pod)
1198 {
1199         int err = pod_try_init(interface, pod);
1200
1201         if (err < 0)
1202                 pod_destruct(interface);
1203
1204         return err;
1205 }
1206
1207 /*
1208         POD device disconnected.
1209 */
1210 void line6_pod_disconnect(struct usb_interface *interface)
1211 {
1212         struct usb_line6_pod *pod;
1213
1214         if (interface == NULL)
1215                 return;
1216         pod = usb_get_intfdata(interface);
1217
1218         if (pod != NULL) {
1219                 struct snd_line6_pcm *line6pcm = pod->line6.line6pcm;
1220                 struct device *dev = &interface->dev;
1221
1222                 if (line6pcm != NULL)
1223                         line6_pcm_disconnect(line6pcm);
1224
1225                 if (dev != NULL) {
1226                         /* remove sysfs entries: */
1227                         line6_pod_remove_files(pod->firmware_version,
1228                                                pod->line6.
1229                                                properties->device_bit, dev);
1230
1231                         device_remove_file(dev, &dev_attr_device_id);
1232                         device_remove_file(dev, &dev_attr_dirty);
1233                         device_remove_file(dev, &dev_attr_dump);
1234                         device_remove_file(dev, &dev_attr_dump_buf);
1235                         device_remove_file(dev, &dev_attr_finish);
1236                         device_remove_file(dev, &dev_attr_firmware_version);
1237                         device_remove_file(dev, &dev_attr_midi_postprocess);
1238                         device_remove_file(dev, &dev_attr_monitor_level);
1239                         device_remove_file(dev, &dev_attr_name);
1240                         device_remove_file(dev, &dev_attr_name_buf);
1241                         device_remove_file(dev, &dev_attr_retrieve_amp_setup);
1242                         device_remove_file(dev, &dev_attr_retrieve_channel);
1243                         device_remove_file(dev,
1244                                            &dev_attr_retrieve_effects_setup);
1245                         device_remove_file(dev, &dev_attr_routing);
1246                         device_remove_file(dev, &dev_attr_serial_number);
1247                         device_remove_file(dev, &dev_attr_store_amp_setup);
1248                         device_remove_file(dev, &dev_attr_store_channel);
1249                         device_remove_file(dev, &dev_attr_store_effects_setup);
1250                         device_remove_file(dev, &dev_attr_tuner_freq);
1251                         device_remove_file(dev, &dev_attr_tuner_mute);
1252                         device_remove_file(dev, &dev_attr_tuner_note);
1253                         device_remove_file(dev, &dev_attr_tuner_pitch);
1254
1255 #ifdef CONFIG_LINE6_USB_RAW
1256                         device_remove_file(dev, &dev_attr_raw);
1257 #endif
1258                 }
1259         }
1260
1261         pod_destruct(interface);
1262 }