blob: 154985aeb69825ad14b05d8d01d58de033101b6f [file] [log] [blame]
Markus Grabner705ecec2009-02-27 19:43:04 -08001/*
2 * Line6 Linux USB driver - 0.8.0
3 *
4 * Copyright (C) 2004-2009 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 "driver.h"
13
14#include "audio.h"
15#include "capture.h"
16#include "control.h"
17#include "playback.h"
18#include "pod.h"
19
20
21#define POD_SYSEX_CODE 3
22#define POD_BYTES_PER_FRAME 6 /* 24bit audio (stereo) */
23
24
25enum {
26 POD_SYSEX_CLIP = 0x0f,
27 POD_SYSEX_SAVE = 0x24,
28 POD_SYSEX_SYSTEM = 0x56,
29 POD_SYSEX_SYSTEMREQ = 0x57,
30 /* POD_SYSEX_UPDATE = 0x6c, */ /* software update! */
31 POD_SYSEX_STORE = 0x71,
32 POD_SYSEX_FINISH = 0x72,
33 POD_SYSEX_DUMPMEM = 0x73,
34 POD_SYSEX_DUMP = 0x74,
35 POD_SYSEX_DUMPREQ = 0x75
36 /* POD_SYSEX_DUMPMEM2 = 0x76 */ /* dumps entire internal memory of PODxt Pro */
37};
38
39enum {
40 POD_monitor_level = 0x04,
41 POD_routing = 0x05,
42 POD_tuner_mute = 0x13,
43 POD_tuner_freq = 0x15,
44 POD_tuner_note = 0x16,
45 POD_tuner_pitch = 0x17,
46 POD_system_invalid = 0x7fff
47};
48
49enum {
50 POD_DUMP_MEMORY = 2
51};
52
53enum {
54 POD_BUSY_READ,
55 POD_BUSY_WRITE,
56 POD_CHANNEL_DIRTY,
57 POD_SAVE_PRESSED,
58 POD_BUSY_MIDISEND
59};
60
61
62static struct snd_ratden pod_ratden = {
63 .num_min = 78125,
64 .num_max = 78125,
65 .num_step = 1,
66 .den = 2
67};
68
69static struct line6_pcm_properties pod_pcm_properties = {
70 .snd_line6_playback_hw = {
71 .info = (SNDRV_PCM_INFO_MMAP |
72 SNDRV_PCM_INFO_INTERLEAVED |
73 SNDRV_PCM_INFO_BLOCK_TRANSFER |
74 SNDRV_PCM_INFO_MMAP_VALID |
75 SNDRV_PCM_INFO_PAUSE |
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 = LINE6_ISO_PACKET_SIZE_MAX * POD_BYTES_PER_FRAME, /* at least one URB must fit into one period */
85 .period_bytes_max = 8192,
86 .periods_min = 1,
87 .periods_max = 1024
88 },
89 .snd_line6_capture_hw = {
90 .info = (SNDRV_PCM_INFO_MMAP |
91 SNDRV_PCM_INFO_INTERLEAVED |
92 SNDRV_PCM_INFO_BLOCK_TRANSFER |
93 SNDRV_PCM_INFO_MMAP_VALID |
94 SNDRV_PCM_INFO_SYNC_START),
95 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
96 .rates = SNDRV_PCM_RATE_KNOT,
97 .rate_min = 39062,
98 .rate_max = 39063,
99 .channels_min = 2,
100 .channels_max = 2,
101 .buffer_bytes_max = 60000,
102 .period_bytes_min = LINE6_ISO_PACKET_SIZE_MAX * POD_BYTES_PER_FRAME, /* at least one URB must fit into one period */
103 .period_bytes_max = 8192,
104 .periods_min = 1,
105 .periods_max = 1024
106 },
107 .snd_line6_rates = {
108 .nrats = 1,
109 .rats = &pod_ratden
110 },
111 .bytes_per_frame = POD_BYTES_PER_FRAME
112};
113
114static const char pod_request_version[] = { 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7 };
115static const char pod_request_channel[] = { 0xf0, 0x00, 0x01, 0x0c, 0x03, 0x75, 0xf7 };
116static const char pod_version_header [] = { 0xf2, 0x7e, 0x7f, 0x06, 0x02 };
117
118
119/*
120 Mark all parameters as dirty and notify waiting processes.
121*/
122static void pod_mark_batch_all_dirty(struct usb_line6_pod *pod)
123{
124 int i;
125
126 for(i = POD_CONTROL_SIZE; i--;)
127 set_bit(i, pod->param_dirty);
128}
129
130/*
131 Send an asynchronous request for the POD firmware version and device ID.
132*/
133static int pod_version_request_async(struct usb_line6_pod *pod)
134{
135 return line6_send_raw_message_async(&pod->line6, pod->buffer_versionreq, sizeof(pod_request_version));
136}
137
138#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
139static void pod_create_files_work(struct work_struct *work)
140{
141 struct usb_line6_pod *pod = container_of(work, struct usb_line6_pod, create_files_work);
142#else
143static void pod_create_files_work(void *work)
144{
145 struct usb_line6_pod *pod = (struct usb_line6_pod *)work;
146#endif
147
148 pod_create_files(pod->firmware_version, pod->line6.properties->device_bit, pod->line6.ifcdev);
149}
150
151static void pod_startup_timeout(unsigned long arg)
152{
153 enum {
154 REQUEST_NONE,
155 REQUEST_DUMP,
156 REQUEST_VERSION
157 };
158
159 int request = REQUEST_NONE;
160 struct usb_line6_pod *pod = (struct usb_line6_pod *)arg;
161
162 if(pod->dumpreq.ok) {
163 if(!pod->versionreq_ok)
164 request = REQUEST_VERSION;
165 }
166 else {
167 if(pod->versionreq_ok)
168 request = REQUEST_DUMP;
169 else if(pod->startup_count++ & 1)
170 request = REQUEST_DUMP;
171 else
172 request = REQUEST_VERSION;
173 }
174
175 switch(request) {
176 case REQUEST_DUMP:
177 line6_dump_request_async(&pod->dumpreq, &pod->line6, 0);
178 break;
179
180 case REQUEST_VERSION:
181 pod_version_request_async(pod);
182 break;
183
184 default:
185 return;
186 }
187
188 line6_startup_delayed(&pod->dumpreq, 1, pod_startup_timeout, pod);
189}
190
191static char *pod_alloc_sysex_buffer(struct usb_line6_pod *pod, int code, int size)
192{
193 return line6_alloc_sysex_buffer(&pod->line6, POD_SYSEX_CODE, code, size);
194}
195
196/*
197 Send channel dump data to the PODxt Pro.
198*/
199static void pod_dump(struct usb_line6_pod *pod, const unsigned char *data)
200{
201 int size = 1 + sizeof(pod->prog_data);
202 char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_DUMP, size);
203 if(!sysex) return;
204 sysex[SYSEX_DATA_OFS] = 5; /* Don't know what this is good for, but PODxt Pro transmits it, so we also do... */
205 memcpy(sysex + SYSEX_DATA_OFS + 1, data, sizeof(pod->prog_data));
206 line6_send_sysex_message(&pod->line6, sysex, size);
207 memcpy(&pod->prog_data, data, sizeof(pod->prog_data));
208 pod_mark_batch_all_dirty(pod);
209 kfree(sysex);
210}
211
212/*
213 Store parameter value in driver memory and mark it as dirty.
214*/
215static void pod_store_parameter(struct usb_line6_pod *pod, int param, int value)
216{
217 pod->prog_data.control[param] = value;
218 set_bit(param, pod->param_dirty);
219 pod->dirty = 1;
220}
221
222/*
223 Handle SAVE button
224*/
225static void pod_save_button_pressed(struct usb_line6_pod *pod, int type, int index)
226{
227 pod->dirty = 0;
228 set_bit(POD_SAVE_PRESSED, &pod->atomic_flags);
229}
230
231/*
232 Process a completely received message.
233*/
234void pod_process_message(struct usb_line6_pod *pod)
235{
236 const unsigned char *buf = pod->line6.buffer_message;
237
238 /* filter messages by type */
239 switch(buf[0] & 0xf0) {
240 case LINE6_PARAM_CHANGE:
241 case LINE6_PROGRAM_CHANGE:
242 case LINE6_SYSEX_BEGIN:
243 break; /* handle these further down */
244
245 default:
246 return; /* ignore all others */
247 }
248
249 /* process all remaining messages */
250 switch(buf[0]) {
251 case LINE6_PARAM_CHANGE | LINE6_CHANNEL_DEVICE:
252 pod_store_parameter(pod, buf[1], buf[2]);
253 /* intentionally no break here! */
254
255 case LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST:
256 if((buf[1] == POD_amp_model_setup) || (buf[1] == POD_effect_setup)) /* these also affect other settings */
257 line6_dump_request_async(&pod->dumpreq, &pod->line6, 0);
258
259 break;
260
261 case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_DEVICE:
262 case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST:
263 pod->channel_num = buf[1];
264 pod->dirty = 0;
265 set_bit(POD_CHANNEL_DIRTY, &pod->atomic_flags);
266 line6_dump_request_async(&pod->dumpreq, &pod->line6, 0);
267 break;
268
269 case LINE6_SYSEX_BEGIN | LINE6_CHANNEL_DEVICE:
270 case LINE6_SYSEX_BEGIN | LINE6_CHANNEL_UNKNOWN:
271 if(memcmp(buf + 1, line6_midi_id, sizeof(line6_midi_id)) == 0) {
272 switch(buf[5]) {
273 case POD_SYSEX_DUMP:
274 if(pod->line6.message_length == sizeof(pod->prog_data) + 7) {
275 switch(pod->dumpreq.in_progress) {
276 case LINE6_DUMP_CURRENT:
277 memcpy(&pod->prog_data, buf + 7, sizeof(pod->prog_data));
278 pod_mark_batch_all_dirty(pod);
279 pod->dumpreq.ok = 1;
280 break;
281
282 case POD_DUMP_MEMORY:
283 memcpy(&pod->prog_data_buf, buf + 7, sizeof(pod->prog_data_buf));
284 break;
285
286 default:
287 DEBUG_MESSAGES(dev_err(pod->line6.ifcdev, "unknown dump code %02X\n", pod->dumpreq.in_progress));
288 }
289
290 line6_dump_finished(&pod->dumpreq);
291 }
292 else
293 DEBUG_MESSAGES(dev_err(pod->line6.ifcdev, "wrong size of channel dump message (%d instead of %d)\n",
294 pod->line6.message_length, (int)sizeof(pod->prog_data) + 7));
295
296 break;
297
298 case POD_SYSEX_SYSTEM: {
299 short value = ((int)buf[7] << 12) | ((int)buf[8] << 8) | ((int)buf[9] << 4) | (int)buf[10];
300
301#define PROCESS_SYSTEM_PARAM(x) \
302 case POD_ ## x: \
303 pod->x.value = value; \
304 wake_up_interruptible(&pod->x.wait); \
305 break;
306
307 switch(buf[6]) {
308 PROCESS_SYSTEM_PARAM(monitor_level);
309 PROCESS_SYSTEM_PARAM(routing);
310 PROCESS_SYSTEM_PARAM(tuner_mute);
311 PROCESS_SYSTEM_PARAM(tuner_freq);
312 PROCESS_SYSTEM_PARAM(tuner_note);
313 PROCESS_SYSTEM_PARAM(tuner_pitch);
314
315#undef PROCESS_SYSTEM_PARAM
316
317 default:
318 DEBUG_MESSAGES(dev_err(pod->line6.ifcdev, "unknown tuner/system response %02X\n", buf[6]));
319 }
320
321 break;
322 }
323
324 case POD_SYSEX_FINISH:
325 /* do we need to respond to this? */
326 break;
327
328 case POD_SYSEX_SAVE:
329 pod_save_button_pressed(pod, buf[6], buf[7]);
330 break;
331
332 case POD_SYSEX_CLIP:
333 DEBUG_MESSAGES(dev_err(pod->line6.ifcdev, "audio clipped\n"));
334 pod->clipping.value = 1;
335 wake_up_interruptible(&pod->clipping.wait);
336 break;
337
338 case POD_SYSEX_STORE:
339 DEBUG_MESSAGES(dev_err(pod->line6.ifcdev, "message %02X not yet implemented\n", buf[5]));
340 break;
341
342 default:
343 DEBUG_MESSAGES(dev_err(pod->line6.ifcdev, "unknown sysex message %02X\n", buf[5]));
344 }
345 }
346 else if(memcmp(buf, pod_version_header, sizeof(pod_version_header)) == 0) {
347 if(pod->versionreq_ok == 0) {
348 pod->firmware_version = buf[13] * 100 + buf[14] * 10 + buf[15];
349 pod->device_id = ((int)buf[8] << 16) | ((int)buf[9] << 8) | (int)buf[10];
350 pod->versionreq_ok = 1;
351
352 /* Now we know the firmware version, so we schedule a bottom half
353 handler to create the special files: */
354#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
355 INIT_WORK(&pod->create_files_work, pod_create_files_work);
356#else
357 INIT_WORK(&pod->create_files_work, pod_create_files_work, pod);
358#endif
359 queue_work(line6_workqueue, &pod->create_files_work);
360 }
361 else
362 DEBUG_MESSAGES(dev_err(pod->line6.ifcdev, "multiple firmware version message\n"));
363 }
364 else
365 DEBUG_MESSAGES(dev_err(pod->line6.ifcdev, "unknown sysex header\n"));
366
367 break;
368
369 case LINE6_SYSEX_END:
370 break;
371
372 default:
373 DEBUG_MESSAGES(dev_err(pod->line6.ifcdev, "POD: unknown message %02X\n", buf[0]));
374 }
375}
376
377/*
378 Detect some cases that require a channel dump after sending a command to the
379 device. Important notes:
380 *) The actual dump request can not be sent here since we are not allowed to
381 wait for the completion of the first message in this context, and sending
382 the dump request before completion of the previous message leaves the POD
383 in an undefined state. The dump request will be sent when the echoed
384 commands are received.
385 *) This method fails if a param change message is "chopped" after the first
386 byte.
387*/
388void pod_midi_postprocess(struct usb_line6_pod *pod, unsigned char *data, int length)
389{
390 int i;
391
392 if(!pod->midi_postprocess)
393 return;
394
395 for(i = 0; i < length; ++i) {
396 if(data[i] == (LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST)) {
397 line6_invalidate_current(&pod->dumpreq);
398 break;
399 }
400 else if((data[i] == (LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST)) && (i < length - 1))
401 if((data[i + 1] == POD_amp_model_setup) || (data[i + 1] == POD_effect_setup)) {
402 line6_invalidate_current(&pod->dumpreq);
403 break;
404 }
405 }
406}
407
408/*
409 Send channel number (i.e., switch to a different sound).
410*/
411void pod_send_channel(struct usb_line6_pod *pod, int value)
412{
413 line6_invalidate_current(&pod->dumpreq);
414
415 if(line6_send_program(&pod->line6, value) == 0)
416 pod->channel_num = value;
417 else
418 line6_dump_finished(&pod->dumpreq);
419}
420
421/*
422 Transmit PODxt Pro control parameter.
423*/
424void pod_transmit_parameter(struct usb_line6_pod *pod, int param, int value)
425{
426 if(line6_transmit_parameter(&pod->line6, param, value) == 0)
427 pod_store_parameter(pod, param, value);
428
429 if((param == POD_amp_model_setup) || (param == POD_effect_setup)) /* these also affect other settings */
430 line6_invalidate_current(&pod->dumpreq);
431}
432
433/*
434 Resolve value to memory location.
435*/
436static void pod_resolve(const char *buf, short block0, short block1, unsigned char *location)
437{
438 int value = simple_strtoul(buf, NULL, 10);
439 short block = (value < 0x40) ? block0 : block1;
440 value &= 0x3f;
441 location[0] = block >> 7;
442 location[1] = value | (block & 0x7f);
443}
444
445/*
446 Send command to store channel/effects setup/amp setup to PODxt Pro.
447*/
448static ssize_t pod_send_store_command(struct device *dev, const char *buf, size_t count, short block0, short block1)
449{
450 struct usb_interface *interface = to_usb_interface(dev);
451 struct usb_line6_pod *pod = usb_get_intfdata(interface);
452
453 int size = 3 + sizeof(pod->prog_data_buf);
454 char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_STORE, size);
455 if(!sysex) return 0;
456
457 sysex[SYSEX_DATA_OFS] = 5; /* see pod_dump() */
458 pod_resolve(buf, block0, block1, sysex + SYSEX_DATA_OFS + 1);
459 memcpy(sysex + SYSEX_DATA_OFS + 3, &pod->prog_data_buf, sizeof(pod->prog_data_buf));
460
461 line6_send_sysex_message(&pod->line6, sysex, size);
462 kfree(sysex);
463 /* needs some delay here on AMD64 platform */
464 return count;
465}
466
467/*
468 Send command to retrieve channel/effects setup/amp setup to PODxt Pro.
469*/
470static ssize_t pod_send_retrieve_command(struct device *dev, const char *buf, size_t count, short block0, short block1)
471{
472 struct usb_interface *interface = to_usb_interface(dev);
473 struct usb_line6_pod *pod = usb_get_intfdata(interface);
474
475 int size = 4;
476 char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_DUMPMEM, size);
477 if(!sysex) return 0;
478
479 pod_resolve(buf, block0, block1, sysex + SYSEX_DATA_OFS);
480 sysex[SYSEX_DATA_OFS + 2] = 0;
481 sysex[SYSEX_DATA_OFS + 3] = 0;
482 line6_dump_started(&pod->dumpreq, POD_DUMP_MEMORY);
483
484 if(line6_send_sysex_message(&pod->line6, sysex, size) < size)
485 line6_dump_finished(&pod->dumpreq);
486
487 kfree(sysex);
488 /* needs some delay here on AMD64 platform */
489 return count;
490}
491
492/*
493 Generic get name function.
494*/
495static ssize_t get_name_generic(struct usb_line6_pod *pod, const char *str, char *buf)
496{
497 int length = 0;
498 const char *p1;
499 char *p2;
500 char *last_non_space = buf;
501
502 int retval = line6_wait_dump(&pod->dumpreq, 0);
503 if(retval < 0) return retval;
504
505 for(p1 = str, p2 = buf; *p1; ++p1, ++p2) {
506 *p2 = *p1;
507 if(*p2 != ' ') last_non_space = p2;
508 if(++length == POD_NAME_LENGTH) break;
509 }
510
511 *(last_non_space + 1) = '\n';
512 return last_non_space - buf + 2;
513}
514
515/*
516 "read" request on "channel" special file.
517*/
518static ssize_t pod_get_channel(struct device *dev, DEVICE_ATTRIBUTE char *buf)
519{
520 struct usb_interface *interface = to_usb_interface(dev);
521 struct usb_line6_pod *pod = usb_get_intfdata(interface);
522 return sprintf(buf, "%d\n", pod->channel_num);
523}
524
525/*
526 "write" request on "channel" special file.
527*/
528static ssize_t pod_set_channel(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
529{
530 struct usb_interface *interface = to_usb_interface(dev);
531 struct usb_line6_pod *pod = usb_get_intfdata(interface);
532 int value = simple_strtoul(buf, NULL, 10);
533 pod_send_channel(pod, value);
534 return count;
535}
536
537/*
538 "read" request on "name" special file.
539*/
540static ssize_t pod_get_name(struct device *dev, DEVICE_ATTRIBUTE char *buf)
541{
542 struct usb_interface *interface = to_usb_interface(dev);
543 struct usb_line6_pod *pod = usb_get_intfdata(interface);
544 return get_name_generic(pod, pod->prog_data.header + POD_NAME_OFFSET, buf);
545}
546
547/*
548 "read" request on "name" special file.
549*/
550static ssize_t pod_get_name_buf(struct device *dev, DEVICE_ATTRIBUTE char *buf)
551{
552 struct usb_interface *interface = to_usb_interface(dev);
553 struct usb_line6_pod *pod = usb_get_intfdata(interface);
554 return get_name_generic(pod, pod->prog_data_buf.header + POD_NAME_OFFSET, buf);
555}
556
557/*
558 "read" request on "dump" special file.
559*/
560static ssize_t pod_get_dump(struct device *dev, DEVICE_ATTRIBUTE char *buf)
561{
562 struct usb_interface *interface = to_usb_interface(dev);
563 struct usb_line6_pod *pod = usb_get_intfdata(interface);
564 int retval = line6_wait_dump(&pod->dumpreq, 0);
565 if(retval < 0) return retval;
566 memcpy(buf, &pod->prog_data, sizeof(pod->prog_data));
567 return sizeof(pod->prog_data);
568}
569
570/*
571 "write" request on "dump" special file.
572*/
573static ssize_t pod_set_dump(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
574{
575 struct usb_interface *interface = to_usb_interface(dev);
576 struct usb_line6_pod *pod = usb_get_intfdata(interface);
577
578 if(count != sizeof(pod->prog_data)) {
579 dev_err(pod->line6.ifcdev,
580 "data block must be exactly %d bytes\n",
581 (int)sizeof(pod->prog_data));
582 return -EINVAL;
583 }
584
585 pod_dump(pod, buf);
586 return sizeof(pod->prog_data);
587}
588
589/*
590 Request system parameter.
591 @param tuner non-zero, if code refers to a tuner parameter
592*/
593static ssize_t pod_get_system_param(struct usb_line6_pod *pod, char *buf, int code, struct ValueWait *param, int tuner, int sign)
594{
595 char *sysex;
596 int value;
597 static const int size = 1;
598 int retval = 0;
599 DECLARE_WAITQUEUE(wait, current);
600
601 if(((pod->prog_data.control[POD_tuner] & 0x40) == 0) && tuner)
602 return -ENODEV;
603
604 /* send value request to tuner: */
605 param->value = POD_system_invalid;
606 sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEMREQ, size);
607 if(!sysex) return 0;
608 sysex[SYSEX_DATA_OFS] = code;
609 line6_send_sysex_message(&pod->line6, sysex, size);
610 kfree(sysex);
611
612 /* wait for tuner to respond: */
613 add_wait_queue(&param->wait, &wait);
614 current->state = TASK_INTERRUPTIBLE;
615
616 while(param->value == POD_system_invalid) {
617 if(signal_pending(current)) {
618 retval = -ERESTARTSYS;
619 break;
620 }
621 else
622 schedule();
623 }
624
625 current->state = TASK_RUNNING;
626 remove_wait_queue(&param->wait, &wait);
627
628 if(retval < 0)
629 return retval;
630
631 value = sign ? (int)(signed short)param->value : (int)(unsigned short)param->value;
632 return sprintf(buf, "%d\n", value);
633}
634
635/*
636 Send system parameter.
637 @param tuner non-zero, if code refers to a tuner parameter
638*/
639static ssize_t pod_set_system_param(struct usb_line6_pod *pod, const char *buf, int count, int code, unsigned short mask, int tuner)
640{
641 char *sysex;
642 static const int size = 5;
643 unsigned short value;
644
645 if(((pod->prog_data.control[POD_tuner] & 0x40) == 0) && tuner)
646 return -EINVAL;
647
648 /* send value to tuner: */
649 sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEM, size);
650 if(!sysex) return 0;
651 value = simple_strtoul(buf, NULL, 10) & mask;
652 sysex[SYSEX_DATA_OFS] = code;
653 sysex[SYSEX_DATA_OFS + 1] = (value >> 12) & 0x0f;
654 sysex[SYSEX_DATA_OFS + 2] = (value >> 8) & 0x0f;
655 sysex[SYSEX_DATA_OFS + 3] = (value >> 4) & 0x0f;
656 sysex[SYSEX_DATA_OFS + 4] = (value ) & 0x0f;
657 line6_send_sysex_message(&pod->line6, sysex, size);
658 kfree(sysex);
659 return count;
660}
661
662/*
663 "read" request on "dump_buf" special file.
664*/
665static ssize_t pod_get_dump_buf(struct device *dev, DEVICE_ATTRIBUTE char *buf)
666{
667 struct usb_interface *interface = to_usb_interface(dev);
668 struct usb_line6_pod *pod = usb_get_intfdata(interface);
669 int retval = line6_wait_dump(&pod->dumpreq, 0);
670 if(retval < 0) return retval;
671 memcpy(buf, &pod->prog_data_buf, sizeof(pod->prog_data_buf));
672 return sizeof(pod->prog_data_buf);
673}
674
675/*
676 "write" request on "dump_buf" special file.
677*/
678static ssize_t pod_set_dump_buf(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
679{
680 struct usb_interface *interface = to_usb_interface(dev);
681 struct usb_line6_pod *pod = usb_get_intfdata(interface);
682
683 if(count != sizeof(pod->prog_data)) {
684 dev_err(pod->line6.ifcdev,
685 "data block must be exactly %d bytes\n",
686 (int)sizeof(pod->prog_data));
687 return -EINVAL;
688 }
689
690 memcpy(&pod->prog_data_buf, buf, sizeof(pod->prog_data));
691 return sizeof(pod->prog_data);
692}
693
694/*
695 "write" request on "finish" special file.
696*/
697static ssize_t pod_set_finish(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
698{
699 struct usb_interface *interface = to_usb_interface(dev);
700 struct usb_line6_pod *pod = usb_get_intfdata(interface);
701 int size = 0;
702 char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_FINISH, size);
703 if(!sysex) return 0;
704 line6_send_sysex_message(&pod->line6, sysex, size);
705 kfree(sysex);
706 return count;
707}
708
709/*
710 "write" request on "store_channel" special file.
711*/
712static ssize_t pod_set_store_channel(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
713{
714 return pod_send_store_command(dev, buf, count, 0x0000, 0x00c0);
715}
716
717/*
718 "write" request on "store_effects_setup" special file.
719*/
720static ssize_t pod_set_store_effects_setup(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
721{
722 return pod_send_store_command(dev, buf, count, 0x0080, 0x0080);
723}
724
725/*
726 "write" request on "store_amp_setup" special file.
727*/
728static ssize_t pod_set_store_amp_setup(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
729{
730 return pod_send_store_command(dev, buf, count, 0x0040, 0x0100);
731}
732
733/*
734 "write" request on "retrieve_channel" special file.
735*/
736static ssize_t pod_set_retrieve_channel(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
737{
738 return pod_send_retrieve_command(dev, buf, count, 0x0000, 0x00c0);
739}
740
741/*
742 "write" request on "retrieve_effects_setup" special file.
743*/
744static ssize_t pod_set_retrieve_effects_setup(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
745{
746 return pod_send_retrieve_command(dev, buf, count, 0x0080, 0x0080);
747}
748
749/*
750 "write" request on "retrieve_amp_setup" special file.
751*/
752static ssize_t pod_set_retrieve_amp_setup(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
753{
754 return pod_send_retrieve_command(dev, buf, count, 0x0040, 0x0100);
755}
756
757/*
758 "read" request on "dirty" special file.
759*/
760static ssize_t pod_get_dirty(struct device *dev, DEVICE_ATTRIBUTE char *buf)
761{
762 struct usb_interface *interface = to_usb_interface(dev);
763 struct usb_line6_pod *pod = usb_get_intfdata(interface);
764 buf[0] = pod->dirty ? '1' : '0';
765 buf[1] = '\n';
766 return 2;
767}
768
769/*
770 "read" request on "midi_postprocess" special file.
771*/
772static ssize_t pod_get_midi_postprocess(struct device *dev, DEVICE_ATTRIBUTE char *buf)
773{
774 struct usb_interface *interface = to_usb_interface(dev);
775 struct usb_line6_pod *pod = usb_get_intfdata(interface);
776 return sprintf(buf, "%d\n", pod->midi_postprocess);
777}
778
779/*
780 "write" request on "midi_postprocess" special file.
781*/
782static ssize_t pod_set_midi_postprocess(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
783{
784 struct usb_interface *interface = to_usb_interface(dev);
785 struct usb_line6_pod *pod = usb_get_intfdata(interface);
786 int value = simple_strtoul(buf, NULL, 10);
787 pod->midi_postprocess = value ? 1 : 0;
788 return count;
789}
790
791/*
792 "read" request on "serial_number" special file.
793*/
794static ssize_t pod_get_serial_number(struct device *dev, DEVICE_ATTRIBUTE char *buf)
795{
796 struct usb_interface *interface = to_usb_interface(dev);
797 struct usb_line6_pod *pod = usb_get_intfdata(interface);
798 return sprintf(buf, "%d\n", pod->serial_number);
799}
800
801/*
802 "read" request on "firmware_version" special file.
803*/
804static ssize_t pod_get_firmware_version(struct device *dev, DEVICE_ATTRIBUTE char *buf)
805{
806 struct usb_interface *interface = to_usb_interface(dev);
807 struct usb_line6_pod *pod = usb_get_intfdata(interface);
808 return sprintf(buf, "%d.%02d\n", pod->firmware_version / 100, pod->firmware_version % 100);
809}
810
811/*
812 "read" request on "device_id" special file.
813*/
814static ssize_t pod_get_device_id(struct device *dev, DEVICE_ATTRIBUTE char *buf)
815{
816 struct usb_interface *interface = to_usb_interface(dev);
817 struct usb_line6_pod *pod = usb_get_intfdata(interface);
818 return sprintf(buf, "%d\n", pod->device_id);
819}
820
821/*
822 "read" request on "clip" special file.
823*/
824static ssize_t pod_wait_for_clip(struct device *dev, DEVICE_ATTRIBUTE char *buf)
825{
826 struct usb_interface *interface = to_usb_interface(dev);
827 struct usb_line6_pod *pod = usb_get_intfdata(interface);
828 int err = 0;
829 DECLARE_WAITQUEUE(wait, current);
830 pod->clipping.value = 0;
831 add_wait_queue(&pod->clipping.wait, &wait);
832 current->state = TASK_INTERRUPTIBLE;
833
834 while(pod->clipping.value == 0) {
835 if(signal_pending(current)) {
836 err = -ERESTARTSYS;
837 break;
838 }
839 else
840 schedule();
841 }
842
843 current->state = TASK_RUNNING;
844 remove_wait_queue(&pod->clipping.wait, &wait);
845 return err;
846}
847
848#define POD_GET_SYSTEM_PARAM(code, tuner, sign) \
849static ssize_t pod_get_ ## code(struct device *dev, DEVICE_ATTRIBUTE char *buf) \
850{ \
851 struct usb_interface *interface = to_usb_interface(dev); \
852 struct usb_line6_pod *pod = usb_get_intfdata(interface); \
853 return pod_get_system_param(pod, buf, POD_ ## code, &pod->code, tuner, sign); \
854}
855
856#define POD_GET_SET_SYSTEM_PARAM(code, mask, tuner, sign) \
857POD_GET_SYSTEM_PARAM(code, tuner, sign) \
858static ssize_t pod_set_ ## code(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count) \
859{ \
860 struct usb_interface *interface = to_usb_interface(dev); \
861 struct usb_line6_pod *pod = usb_get_intfdata(interface); \
862 return pod_set_system_param(pod, buf, count, POD_ ## code, mask, tuner); \
863}
864
865POD_GET_SET_SYSTEM_PARAM(monitor_level, 0xffff, 0, 0);
866POD_GET_SET_SYSTEM_PARAM(routing, 0x0003, 0, 0);
867POD_GET_SET_SYSTEM_PARAM(tuner_mute, 0x0001, 1, 0);
868POD_GET_SET_SYSTEM_PARAM(tuner_freq, 0xffff, 1, 0);
869POD_GET_SYSTEM_PARAM(tuner_note, 1, 1);
870POD_GET_SYSTEM_PARAM(tuner_pitch, 1, 1);
871
872#undef GET_SET_SYSTEM_PARAM
873#undef GET_SYSTEM_PARAM
874
875/* POD special files: */
876static DEVICE_ATTR(channel, S_IWUGO | S_IRUGO, pod_get_channel, pod_set_channel);
877static DEVICE_ATTR(clip, S_IRUGO, pod_wait_for_clip, line6_nop_write);
878static DEVICE_ATTR(device_id, S_IRUGO, pod_get_device_id, line6_nop_write);
879static DEVICE_ATTR(dirty, S_IRUGO, pod_get_dirty, line6_nop_write);
880static DEVICE_ATTR(dump, S_IWUGO | S_IRUGO, pod_get_dump, pod_set_dump);
881static DEVICE_ATTR(dump_buf, S_IWUGO | S_IRUGO, pod_get_dump_buf, pod_set_dump_buf);
882static DEVICE_ATTR(finish, S_IWUGO, line6_nop_read, pod_set_finish);
883static DEVICE_ATTR(firmware_version, S_IRUGO, pod_get_firmware_version, line6_nop_write);
884static DEVICE_ATTR(midi_postprocess, S_IWUGO | S_IRUGO, pod_get_midi_postprocess, pod_set_midi_postprocess);
885static DEVICE_ATTR(monitor_level, S_IWUGO | S_IRUGO, pod_get_monitor_level, pod_set_monitor_level);
886static DEVICE_ATTR(name, S_IRUGO, pod_get_name, line6_nop_write);
887static DEVICE_ATTR(name_buf, S_IRUGO, pod_get_name_buf, line6_nop_write);
888static DEVICE_ATTR(retrieve_amp_setup, S_IWUGO, line6_nop_read, pod_set_retrieve_amp_setup);
889static DEVICE_ATTR(retrieve_channel, S_IWUGO, line6_nop_read, pod_set_retrieve_channel);
890static DEVICE_ATTR(retrieve_effects_setup, S_IWUGO, line6_nop_read, pod_set_retrieve_effects_setup);
891static DEVICE_ATTR(routing, S_IWUGO | S_IRUGO, pod_get_routing, pod_set_routing);
892static DEVICE_ATTR(serial_number, S_IRUGO, pod_get_serial_number, line6_nop_write);
893static DEVICE_ATTR(store_amp_setup, S_IWUGO, line6_nop_read, pod_set_store_amp_setup);
894static DEVICE_ATTR(store_channel, S_IWUGO, line6_nop_read, pod_set_store_channel);
895static DEVICE_ATTR(store_effects_setup, S_IWUGO, line6_nop_read, pod_set_store_effects_setup);
896static DEVICE_ATTR(tuner_freq, S_IWUGO | S_IRUGO, pod_get_tuner_freq, pod_set_tuner_freq);
897static DEVICE_ATTR(tuner_mute, S_IWUGO | S_IRUGO, pod_get_tuner_mute, pod_set_tuner_mute);
898static DEVICE_ATTR(tuner_note, S_IRUGO, pod_get_tuner_note, line6_nop_write);
899static DEVICE_ATTR(tuner_pitch, S_IRUGO, pod_get_tuner_pitch, line6_nop_write);
900
901#if CREATE_RAW_FILE
902static DEVICE_ATTR(raw, S_IWUGO, line6_nop_read, line6_set_raw);
903#endif
904
905/*
906 POD destructor.
907*/
908static void pod_destruct(struct usb_interface *interface)
909{
910 struct usb_line6_pod *pod = usb_get_intfdata(interface);
911 struct usb_line6 *line6;
912
913 if(pod == NULL) return;
914 line6 = &pod->line6;
915 if(line6 == NULL) return;
916 line6_cleanup_audio(line6);
917
918 /* free dump request data: */
919 line6_dumpreq_destruct(&pod->dumpreq);
920
921 if(pod->buffer_versionreq) kfree(pod->buffer_versionreq);
922}
923
924/*
925 Create sysfs entries.
926*/
927int pod_create_files2(struct device *dev)
928{
929 int err;
930
931 CHECK_RETURN(device_create_file(dev, &dev_attr_channel));
932 CHECK_RETURN(device_create_file(dev, &dev_attr_clip));
933 CHECK_RETURN(device_create_file(dev, &dev_attr_device_id));
934 CHECK_RETURN(device_create_file(dev, &dev_attr_dirty));
935 CHECK_RETURN(device_create_file(dev, &dev_attr_dump));
936 CHECK_RETURN(device_create_file(dev, &dev_attr_dump_buf));
937 CHECK_RETURN(device_create_file(dev, &dev_attr_finish));
938 CHECK_RETURN(device_create_file(dev, &dev_attr_firmware_version));
939 CHECK_RETURN(device_create_file(dev, &dev_attr_midi_postprocess));
940 CHECK_RETURN(device_create_file(dev, &dev_attr_monitor_level));
941 CHECK_RETURN(device_create_file(dev, &dev_attr_name));
942 CHECK_RETURN(device_create_file(dev, &dev_attr_name_buf));
943 CHECK_RETURN(device_create_file(dev, &dev_attr_retrieve_amp_setup));
944 CHECK_RETURN(device_create_file(dev, &dev_attr_retrieve_channel));
945 CHECK_RETURN(device_create_file(dev, &dev_attr_retrieve_effects_setup));
946 CHECK_RETURN(device_create_file(dev, &dev_attr_routing));
947 CHECK_RETURN(device_create_file(dev, &dev_attr_serial_number));
948 CHECK_RETURN(device_create_file(dev, &dev_attr_store_amp_setup));
949 CHECK_RETURN(device_create_file(dev, &dev_attr_store_channel));
950 CHECK_RETURN(device_create_file(dev, &dev_attr_store_effects_setup));
951 CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_freq));
952 CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_mute));
953 CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_note));
954 CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_pitch));
955
956#if CREATE_RAW_FILE
957 CHECK_RETURN(device_create_file(dev, &dev_attr_raw));
958#endif
959
960 return 0;
961}
962
963/*
964 Init POD device.
965*/
966int pod_init(struct usb_interface *interface, struct usb_line6_pod *pod)
967{
968 int err;
969 struct usb_line6 *line6 = &pod->line6;
970
971 if((interface == NULL) || (pod == NULL)) return -ENODEV;
972
973 pod->channel_num = 255;
974
975 /* initialize wait queues: */
976 init_waitqueue_head(&pod->monitor_level.wait);
977 init_waitqueue_head(&pod->routing.wait);
978 init_waitqueue_head(&pod->tuner_mute.wait);
979 init_waitqueue_head(&pod->tuner_freq.wait);
980 init_waitqueue_head(&pod->tuner_note.wait);
981 init_waitqueue_head(&pod->tuner_pitch.wait);
982 init_waitqueue_head(&pod->clipping.wait);
983
984 memset(pod->param_dirty, 0xff, sizeof(pod->param_dirty));
985
986 /* initialize USB buffers: */
987 err = line6_dumpreq_init(&pod->dumpreq, pod_request_channel, sizeof(pod_request_channel));
988
989 if(err < 0) {
990 dev_err(&interface->dev, "Out of memory\n");
991 pod_destruct(interface);
992 return -ENOMEM;
993 }
994
995 pod->buffer_versionreq = kmalloc(sizeof(pod_request_version), GFP_KERNEL);
996
997 if(pod->buffer_versionreq == NULL) {
998 dev_err(&interface->dev, "Out of memory\n");
999 pod_destruct(interface);
1000 return -ENOMEM;
1001 }
1002
1003 memcpy(pod->buffer_versionreq, pod_request_version, sizeof(pod_request_version));
1004
1005 /* create sysfs entries: */
1006 if((err = pod_create_files2(&interface->dev)) < 0) {
1007 pod_destruct(interface);
1008 return err;
1009 }
1010
1011 /* initialize audio system: */
1012 if((err = line6_init_audio(line6)) < 0) {
1013 pod_destruct(interface);
1014 return err;
1015 }
1016
1017 /* initialize MIDI subsystem: */
1018 if((err = line6_init_midi(line6)) < 0) {
1019 pod_destruct(interface);
1020 return err;
1021 }
1022
1023 /* initialize PCM subsystem: */
1024 if((err = line6_init_pcm(line6, &pod_pcm_properties)) < 0) {
1025 pod_destruct(interface);
1026 return err;
1027 }
1028
1029 /* register audio system: */
1030 if((err = line6_register_audio(line6)) < 0) {
1031 pod_destruct(interface);
1032 return err;
1033 }
1034
1035 if(pod->line6.properties->capabilities & LINE6_BIT_CONTROL) {
1036 /* query some data: */
1037 line6_startup_delayed(&pod->dumpreq, POD_STARTUP_DELAY, pod_startup_timeout, pod);
1038 line6_read_serial_number(&pod->line6, &pod->serial_number);
1039 }
1040
1041 return 0;
1042}
1043
1044/*
1045 POD device disconnected.
1046*/
1047void pod_disconnect(struct usb_interface *interface)
1048{
1049 struct usb_line6_pod *pod;
1050
1051 if(interface == NULL) return;
1052 pod = usb_get_intfdata(interface);
1053
1054 if(pod != NULL) {
1055 struct snd_line6_pcm *line6pcm = pod->line6.line6pcm;
1056 struct device *dev = &interface->dev;
1057
1058 if(line6pcm != NULL) {
1059 unlink_wait_clear_audio_out_urbs(line6pcm);
1060 unlink_wait_clear_audio_in_urbs(line6pcm);
1061 }
1062
1063 if(dev != NULL) {
1064 /* remove sysfs entries: */
1065 if(pod->versionreq_ok)
1066 pod_remove_files(pod->firmware_version, pod->line6.properties->device_bit, dev);
1067
1068 device_remove_file(dev, &dev_attr_channel);
1069 device_remove_file(dev, &dev_attr_clip);
1070 device_remove_file(dev, &dev_attr_device_id);
1071 device_remove_file(dev, &dev_attr_dirty);
1072 device_remove_file(dev, &dev_attr_dump);
1073 device_remove_file(dev, &dev_attr_dump_buf);
1074 device_remove_file(dev, &dev_attr_finish);
1075 device_remove_file(dev, &dev_attr_firmware_version);
1076 device_remove_file(dev, &dev_attr_midi_postprocess);
1077 device_remove_file(dev, &dev_attr_monitor_level);
1078 device_remove_file(dev, &dev_attr_name);
1079 device_remove_file(dev, &dev_attr_name_buf);
1080 device_remove_file(dev, &dev_attr_retrieve_amp_setup);
1081 device_remove_file(dev, &dev_attr_retrieve_channel);
1082 device_remove_file(dev, &dev_attr_retrieve_effects_setup);
1083 device_remove_file(dev, &dev_attr_routing);
1084 device_remove_file(dev, &dev_attr_serial_number);
1085 device_remove_file(dev, &dev_attr_store_amp_setup);
1086 device_remove_file(dev, &dev_attr_store_channel);
1087 device_remove_file(dev, &dev_attr_store_effects_setup);
1088 device_remove_file(dev, &dev_attr_tuner_freq);
1089 device_remove_file(dev, &dev_attr_tuner_mute);
1090 device_remove_file(dev, &dev_attr_tuner_note);
1091 device_remove_file(dev, &dev_attr_tuner_pitch);
1092
1093#if CREATE_RAW_FILE
1094 device_remove_file(dev, &dev_attr_raw);
1095#endif
1096 }
1097 }
1098
1099 pod_destruct(interface);
1100}