blob: af18087f1ed148dc587c9bab35d54fdaf68cc863 [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*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800518static ssize_t pod_get_channel(struct device *dev,
519 struct device_attribute *attr, char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800520{
521 struct usb_interface *interface = to_usb_interface(dev);
522 struct usb_line6_pod *pod = usb_get_intfdata(interface);
523 return sprintf(buf, "%d\n", pod->channel_num);
524}
525
526/*
527 "write" request on "channel" special file.
528*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800529static ssize_t pod_set_channel(struct device *dev,
530 struct device_attribute *attr,
531 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800532{
533 struct usb_interface *interface = to_usb_interface(dev);
534 struct usb_line6_pod *pod = usb_get_intfdata(interface);
535 int value = simple_strtoul(buf, NULL, 10);
536 pod_send_channel(pod, value);
537 return count;
538}
539
540/*
541 "read" request on "name" special file.
542*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800543static ssize_t pod_get_name(struct device *dev, struct device_attribute *attr,
544 char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800545{
546 struct usb_interface *interface = to_usb_interface(dev);
547 struct usb_line6_pod *pod = usb_get_intfdata(interface);
548 return get_name_generic(pod, pod->prog_data.header + POD_NAME_OFFSET, buf);
549}
550
551/*
552 "read" request on "name" special file.
553*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800554static ssize_t pod_get_name_buf(struct device *dev,
555 struct device_attribute *attr, char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800556{
557 struct usb_interface *interface = to_usb_interface(dev);
558 struct usb_line6_pod *pod = usb_get_intfdata(interface);
559 return get_name_generic(pod, pod->prog_data_buf.header + POD_NAME_OFFSET, buf);
560}
561
562/*
563 "read" request on "dump" special file.
564*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800565static ssize_t pod_get_dump(struct device *dev, struct device_attribute *attr,
566 char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800567{
568 struct usb_interface *interface = to_usb_interface(dev);
569 struct usb_line6_pod *pod = usb_get_intfdata(interface);
570 int retval = line6_wait_dump(&pod->dumpreq, 0);
571 if(retval < 0) return retval;
572 memcpy(buf, &pod->prog_data, sizeof(pod->prog_data));
573 return sizeof(pod->prog_data);
574}
575
576/*
577 "write" request on "dump" special file.
578*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800579static ssize_t pod_set_dump(struct device *dev, struct device_attribute *attr,
580 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800581{
582 struct usb_interface *interface = to_usb_interface(dev);
583 struct usb_line6_pod *pod = usb_get_intfdata(interface);
584
585 if(count != sizeof(pod->prog_data)) {
586 dev_err(pod->line6.ifcdev,
587 "data block must be exactly %d bytes\n",
588 (int)sizeof(pod->prog_data));
589 return -EINVAL;
590 }
591
592 pod_dump(pod, buf);
593 return sizeof(pod->prog_data);
594}
595
596/*
597 Request system parameter.
598 @param tuner non-zero, if code refers to a tuner parameter
599*/
600static ssize_t pod_get_system_param(struct usb_line6_pod *pod, char *buf, int code, struct ValueWait *param, int tuner, int sign)
601{
602 char *sysex;
603 int value;
604 static const int size = 1;
605 int retval = 0;
606 DECLARE_WAITQUEUE(wait, current);
607
608 if(((pod->prog_data.control[POD_tuner] & 0x40) == 0) && tuner)
609 return -ENODEV;
610
611 /* send value request to tuner: */
612 param->value = POD_system_invalid;
613 sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEMREQ, size);
614 if(!sysex) return 0;
615 sysex[SYSEX_DATA_OFS] = code;
616 line6_send_sysex_message(&pod->line6, sysex, size);
617 kfree(sysex);
618
619 /* wait for tuner to respond: */
620 add_wait_queue(&param->wait, &wait);
621 current->state = TASK_INTERRUPTIBLE;
622
623 while(param->value == POD_system_invalid) {
624 if(signal_pending(current)) {
625 retval = -ERESTARTSYS;
626 break;
627 }
628 else
629 schedule();
630 }
631
632 current->state = TASK_RUNNING;
633 remove_wait_queue(&param->wait, &wait);
634
635 if(retval < 0)
636 return retval;
637
638 value = sign ? (int)(signed short)param->value : (int)(unsigned short)param->value;
639 return sprintf(buf, "%d\n", value);
640}
641
642/*
643 Send system parameter.
644 @param tuner non-zero, if code refers to a tuner parameter
645*/
646static ssize_t pod_set_system_param(struct usb_line6_pod *pod, const char *buf, int count, int code, unsigned short mask, int tuner)
647{
648 char *sysex;
649 static const int size = 5;
650 unsigned short value;
651
652 if(((pod->prog_data.control[POD_tuner] & 0x40) == 0) && tuner)
653 return -EINVAL;
654
655 /* send value to tuner: */
656 sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEM, size);
657 if(!sysex) return 0;
658 value = simple_strtoul(buf, NULL, 10) & mask;
659 sysex[SYSEX_DATA_OFS] = code;
660 sysex[SYSEX_DATA_OFS + 1] = (value >> 12) & 0x0f;
661 sysex[SYSEX_DATA_OFS + 2] = (value >> 8) & 0x0f;
662 sysex[SYSEX_DATA_OFS + 3] = (value >> 4) & 0x0f;
663 sysex[SYSEX_DATA_OFS + 4] = (value ) & 0x0f;
664 line6_send_sysex_message(&pod->line6, sysex, size);
665 kfree(sysex);
666 return count;
667}
668
669/*
670 "read" request on "dump_buf" special file.
671*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800672static ssize_t pod_get_dump_buf(struct device *dev,
673 struct device_attribute *attr, char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800674{
675 struct usb_interface *interface = to_usb_interface(dev);
676 struct usb_line6_pod *pod = usb_get_intfdata(interface);
677 int retval = line6_wait_dump(&pod->dumpreq, 0);
678 if(retval < 0) return retval;
679 memcpy(buf, &pod->prog_data_buf, sizeof(pod->prog_data_buf));
680 return sizeof(pod->prog_data_buf);
681}
682
683/*
684 "write" request on "dump_buf" special file.
685*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800686static ssize_t pod_set_dump_buf(struct device *dev,
687 struct device_attribute *attr,
688 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800689{
690 struct usb_interface *interface = to_usb_interface(dev);
691 struct usb_line6_pod *pod = usb_get_intfdata(interface);
692
693 if(count != sizeof(pod->prog_data)) {
694 dev_err(pod->line6.ifcdev,
695 "data block must be exactly %d bytes\n",
696 (int)sizeof(pod->prog_data));
697 return -EINVAL;
698 }
699
700 memcpy(&pod->prog_data_buf, buf, sizeof(pod->prog_data));
701 return sizeof(pod->prog_data);
702}
703
704/*
705 "write" request on "finish" special file.
706*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800707static ssize_t pod_set_finish(struct device *dev,
708 struct device_attribute *attr,
709 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800710{
711 struct usb_interface *interface = to_usb_interface(dev);
712 struct usb_line6_pod *pod = usb_get_intfdata(interface);
713 int size = 0;
714 char *sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_FINISH, size);
715 if(!sysex) return 0;
716 line6_send_sysex_message(&pod->line6, sysex, size);
717 kfree(sysex);
718 return count;
719}
720
721/*
722 "write" request on "store_channel" special file.
723*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800724static ssize_t pod_set_store_channel(struct device *dev,
725 struct device_attribute *attr,
726 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800727{
728 return pod_send_store_command(dev, buf, count, 0x0000, 0x00c0);
729}
730
731/*
732 "write" request on "store_effects_setup" special file.
733*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800734static ssize_t pod_set_store_effects_setup(struct device *dev,
735 struct device_attribute *attr,
736 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800737{
738 return pod_send_store_command(dev, buf, count, 0x0080, 0x0080);
739}
740
741/*
742 "write" request on "store_amp_setup" special file.
743*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800744static ssize_t pod_set_store_amp_setup(struct device *dev,
745 struct device_attribute *attr,
746 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800747{
748 return pod_send_store_command(dev, buf, count, 0x0040, 0x0100);
749}
750
751/*
752 "write" request on "retrieve_channel" special file.
753*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800754static ssize_t pod_set_retrieve_channel(struct device *dev,
755 struct device_attribute *attr,
756 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800757{
758 return pod_send_retrieve_command(dev, buf, count, 0x0000, 0x00c0);
759}
760
761/*
762 "write" request on "retrieve_effects_setup" special file.
763*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800764static ssize_t pod_set_retrieve_effects_setup(struct device *dev,
765 struct device_attribute *attr,
766 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800767{
768 return pod_send_retrieve_command(dev, buf, count, 0x0080, 0x0080);
769}
770
771/*
772 "write" request on "retrieve_amp_setup" special file.
773*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800774static ssize_t pod_set_retrieve_amp_setup(struct device *dev,
775 struct device_attribute *attr,
776 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800777{
778 return pod_send_retrieve_command(dev, buf, count, 0x0040, 0x0100);
779}
780
781/*
782 "read" request on "dirty" special file.
783*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800784static ssize_t pod_get_dirty(struct device *dev, struct device_attribute *attr,
785 char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800786{
787 struct usb_interface *interface = to_usb_interface(dev);
788 struct usb_line6_pod *pod = usb_get_intfdata(interface);
789 buf[0] = pod->dirty ? '1' : '0';
790 buf[1] = '\n';
791 return 2;
792}
793
794/*
795 "read" request on "midi_postprocess" special file.
796*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800797static ssize_t pod_get_midi_postprocess(struct device *dev,
798 struct device_attribute *attr,
799 char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800800{
801 struct usb_interface *interface = to_usb_interface(dev);
802 struct usb_line6_pod *pod = usb_get_intfdata(interface);
803 return sprintf(buf, "%d\n", pod->midi_postprocess);
804}
805
806/*
807 "write" request on "midi_postprocess" special file.
808*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800809static ssize_t pod_set_midi_postprocess(struct device *dev,
810 struct device_attribute *attr,
811 const char *buf, size_t count)
Markus Grabner705ecec2009-02-27 19:43:04 -0800812{
813 struct usb_interface *interface = to_usb_interface(dev);
814 struct usb_line6_pod *pod = usb_get_intfdata(interface);
815 int value = simple_strtoul(buf, NULL, 10);
816 pod->midi_postprocess = value ? 1 : 0;
817 return count;
818}
819
820/*
821 "read" request on "serial_number" special file.
822*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800823static ssize_t pod_get_serial_number(struct device *dev,
824 struct device_attribute *attr, char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800825{
826 struct usb_interface *interface = to_usb_interface(dev);
827 struct usb_line6_pod *pod = usb_get_intfdata(interface);
828 return sprintf(buf, "%d\n", pod->serial_number);
829}
830
831/*
832 "read" request on "firmware_version" special file.
833*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800834static ssize_t pod_get_firmware_version(struct device *dev,
835 struct device_attribute *attr,
836 char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800837{
838 struct usb_interface *interface = to_usb_interface(dev);
839 struct usb_line6_pod *pod = usb_get_intfdata(interface);
840 return sprintf(buf, "%d.%02d\n", pod->firmware_version / 100, pod->firmware_version % 100);
841}
842
843/*
844 "read" request on "device_id" special file.
845*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800846static ssize_t pod_get_device_id(struct device *dev,
847 struct device_attribute *attr, char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800848{
849 struct usb_interface *interface = to_usb_interface(dev);
850 struct usb_line6_pod *pod = usb_get_intfdata(interface);
851 return sprintf(buf, "%d\n", pod->device_id);
852}
853
854/*
855 "read" request on "clip" special file.
856*/
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800857static ssize_t pod_wait_for_clip(struct device *dev,
858 struct device_attribute *attr, char *buf)
Markus Grabner705ecec2009-02-27 19:43:04 -0800859{
860 struct usb_interface *interface = to_usb_interface(dev);
861 struct usb_line6_pod *pod = usb_get_intfdata(interface);
862 int err = 0;
863 DECLARE_WAITQUEUE(wait, current);
864 pod->clipping.value = 0;
865 add_wait_queue(&pod->clipping.wait, &wait);
866 current->state = TASK_INTERRUPTIBLE;
867
868 while(pod->clipping.value == 0) {
869 if(signal_pending(current)) {
870 err = -ERESTARTSYS;
871 break;
872 }
873 else
874 schedule();
875 }
876
877 current->state = TASK_RUNNING;
878 remove_wait_queue(&pod->clipping.wait, &wait);
879 return err;
880}
881
882#define POD_GET_SYSTEM_PARAM(code, tuner, sign) \
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800883static ssize_t pod_get_ ## code(struct device *dev, \
884 struct device_attribute *attr, char *buf) \
Markus Grabner705ecec2009-02-27 19:43:04 -0800885{ \
886 struct usb_interface *interface = to_usb_interface(dev); \
887 struct usb_line6_pod *pod = usb_get_intfdata(interface); \
888 return pod_get_system_param(pod, buf, POD_ ## code, &pod->code, tuner, sign); \
889}
890
891#define POD_GET_SET_SYSTEM_PARAM(code, mask, tuner, sign) \
892POD_GET_SYSTEM_PARAM(code, tuner, sign) \
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800893static ssize_t pod_set_ ## code(struct device *dev, \
894 struct device_attribute *attr, const char *buf, \
895 size_t count) \
Markus Grabner705ecec2009-02-27 19:43:04 -0800896{ \
897 struct usb_interface *interface = to_usb_interface(dev); \
898 struct usb_line6_pod *pod = usb_get_intfdata(interface); \
899 return pod_set_system_param(pod, buf, count, POD_ ## code, mask, tuner); \
900}
901
902POD_GET_SET_SYSTEM_PARAM(monitor_level, 0xffff, 0, 0);
903POD_GET_SET_SYSTEM_PARAM(routing, 0x0003, 0, 0);
904POD_GET_SET_SYSTEM_PARAM(tuner_mute, 0x0001, 1, 0);
905POD_GET_SET_SYSTEM_PARAM(tuner_freq, 0xffff, 1, 0);
906POD_GET_SYSTEM_PARAM(tuner_note, 1, 1);
907POD_GET_SYSTEM_PARAM(tuner_pitch, 1, 1);
908
909#undef GET_SET_SYSTEM_PARAM
910#undef GET_SYSTEM_PARAM
911
912/* POD special files: */
913static DEVICE_ATTR(channel, S_IWUGO | S_IRUGO, pod_get_channel, pod_set_channel);
914static DEVICE_ATTR(clip, S_IRUGO, pod_wait_for_clip, line6_nop_write);
915static DEVICE_ATTR(device_id, S_IRUGO, pod_get_device_id, line6_nop_write);
916static DEVICE_ATTR(dirty, S_IRUGO, pod_get_dirty, line6_nop_write);
917static DEVICE_ATTR(dump, S_IWUGO | S_IRUGO, pod_get_dump, pod_set_dump);
918static DEVICE_ATTR(dump_buf, S_IWUGO | S_IRUGO, pod_get_dump_buf, pod_set_dump_buf);
919static DEVICE_ATTR(finish, S_IWUGO, line6_nop_read, pod_set_finish);
920static DEVICE_ATTR(firmware_version, S_IRUGO, pod_get_firmware_version, line6_nop_write);
921static DEVICE_ATTR(midi_postprocess, S_IWUGO | S_IRUGO, pod_get_midi_postprocess, pod_set_midi_postprocess);
922static DEVICE_ATTR(monitor_level, S_IWUGO | S_IRUGO, pod_get_monitor_level, pod_set_monitor_level);
923static DEVICE_ATTR(name, S_IRUGO, pod_get_name, line6_nop_write);
924static DEVICE_ATTR(name_buf, S_IRUGO, pod_get_name_buf, line6_nop_write);
925static DEVICE_ATTR(retrieve_amp_setup, S_IWUGO, line6_nop_read, pod_set_retrieve_amp_setup);
926static DEVICE_ATTR(retrieve_channel, S_IWUGO, line6_nop_read, pod_set_retrieve_channel);
927static DEVICE_ATTR(retrieve_effects_setup, S_IWUGO, line6_nop_read, pod_set_retrieve_effects_setup);
928static DEVICE_ATTR(routing, S_IWUGO | S_IRUGO, pod_get_routing, pod_set_routing);
929static DEVICE_ATTR(serial_number, S_IRUGO, pod_get_serial_number, line6_nop_write);
930static DEVICE_ATTR(store_amp_setup, S_IWUGO, line6_nop_read, pod_set_store_amp_setup);
931static DEVICE_ATTR(store_channel, S_IWUGO, line6_nop_read, pod_set_store_channel);
932static DEVICE_ATTR(store_effects_setup, S_IWUGO, line6_nop_read, pod_set_store_effects_setup);
933static DEVICE_ATTR(tuner_freq, S_IWUGO | S_IRUGO, pod_get_tuner_freq, pod_set_tuner_freq);
934static DEVICE_ATTR(tuner_mute, S_IWUGO | S_IRUGO, pod_get_tuner_mute, pod_set_tuner_mute);
935static DEVICE_ATTR(tuner_note, S_IRUGO, pod_get_tuner_note, line6_nop_write);
936static DEVICE_ATTR(tuner_pitch, S_IRUGO, pod_get_tuner_pitch, line6_nop_write);
937
938#if CREATE_RAW_FILE
939static DEVICE_ATTR(raw, S_IWUGO, line6_nop_read, line6_set_raw);
940#endif
941
942/*
943 POD destructor.
944*/
945static void pod_destruct(struct usb_interface *interface)
946{
947 struct usb_line6_pod *pod = usb_get_intfdata(interface);
948 struct usb_line6 *line6;
949
950 if(pod == NULL) return;
951 line6 = &pod->line6;
952 if(line6 == NULL) return;
953 line6_cleanup_audio(line6);
954
955 /* free dump request data: */
956 line6_dumpreq_destruct(&pod->dumpreq);
957
958 if(pod->buffer_versionreq) kfree(pod->buffer_versionreq);
959}
960
961/*
962 Create sysfs entries.
963*/
964int pod_create_files2(struct device *dev)
965{
966 int err;
967
968 CHECK_RETURN(device_create_file(dev, &dev_attr_channel));
969 CHECK_RETURN(device_create_file(dev, &dev_attr_clip));
970 CHECK_RETURN(device_create_file(dev, &dev_attr_device_id));
971 CHECK_RETURN(device_create_file(dev, &dev_attr_dirty));
972 CHECK_RETURN(device_create_file(dev, &dev_attr_dump));
973 CHECK_RETURN(device_create_file(dev, &dev_attr_dump_buf));
974 CHECK_RETURN(device_create_file(dev, &dev_attr_finish));
975 CHECK_RETURN(device_create_file(dev, &dev_attr_firmware_version));
976 CHECK_RETURN(device_create_file(dev, &dev_attr_midi_postprocess));
977 CHECK_RETURN(device_create_file(dev, &dev_attr_monitor_level));
978 CHECK_RETURN(device_create_file(dev, &dev_attr_name));
979 CHECK_RETURN(device_create_file(dev, &dev_attr_name_buf));
980 CHECK_RETURN(device_create_file(dev, &dev_attr_retrieve_amp_setup));
981 CHECK_RETURN(device_create_file(dev, &dev_attr_retrieve_channel));
982 CHECK_RETURN(device_create_file(dev, &dev_attr_retrieve_effects_setup));
983 CHECK_RETURN(device_create_file(dev, &dev_attr_routing));
984 CHECK_RETURN(device_create_file(dev, &dev_attr_serial_number));
985 CHECK_RETURN(device_create_file(dev, &dev_attr_store_amp_setup));
986 CHECK_RETURN(device_create_file(dev, &dev_attr_store_channel));
987 CHECK_RETURN(device_create_file(dev, &dev_attr_store_effects_setup));
988 CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_freq));
989 CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_mute));
990 CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_note));
991 CHECK_RETURN(device_create_file(dev, &dev_attr_tuner_pitch));
992
993#if CREATE_RAW_FILE
994 CHECK_RETURN(device_create_file(dev, &dev_attr_raw));
995#endif
996
997 return 0;
998}
999
1000/*
1001 Init POD device.
1002*/
1003int pod_init(struct usb_interface *interface, struct usb_line6_pod *pod)
1004{
1005 int err;
1006 struct usb_line6 *line6 = &pod->line6;
1007
1008 if((interface == NULL) || (pod == NULL)) return -ENODEV;
1009
1010 pod->channel_num = 255;
1011
1012 /* initialize wait queues: */
1013 init_waitqueue_head(&pod->monitor_level.wait);
1014 init_waitqueue_head(&pod->routing.wait);
1015 init_waitqueue_head(&pod->tuner_mute.wait);
1016 init_waitqueue_head(&pod->tuner_freq.wait);
1017 init_waitqueue_head(&pod->tuner_note.wait);
1018 init_waitqueue_head(&pod->tuner_pitch.wait);
1019 init_waitqueue_head(&pod->clipping.wait);
1020
1021 memset(pod->param_dirty, 0xff, sizeof(pod->param_dirty));
1022
1023 /* initialize USB buffers: */
1024 err = line6_dumpreq_init(&pod->dumpreq, pod_request_channel, sizeof(pod_request_channel));
1025
1026 if(err < 0) {
1027 dev_err(&interface->dev, "Out of memory\n");
1028 pod_destruct(interface);
1029 return -ENOMEM;
1030 }
1031
1032 pod->buffer_versionreq = kmalloc(sizeof(pod_request_version), GFP_KERNEL);
1033
1034 if(pod->buffer_versionreq == NULL) {
1035 dev_err(&interface->dev, "Out of memory\n");
1036 pod_destruct(interface);
1037 return -ENOMEM;
1038 }
1039
1040 memcpy(pod->buffer_versionreq, pod_request_version, sizeof(pod_request_version));
1041
1042 /* create sysfs entries: */
1043 if((err = pod_create_files2(&interface->dev)) < 0) {
1044 pod_destruct(interface);
1045 return err;
1046 }
1047
1048 /* initialize audio system: */
1049 if((err = line6_init_audio(line6)) < 0) {
1050 pod_destruct(interface);
1051 return err;
1052 }
1053
1054 /* initialize MIDI subsystem: */
1055 if((err = line6_init_midi(line6)) < 0) {
1056 pod_destruct(interface);
1057 return err;
1058 }
1059
1060 /* initialize PCM subsystem: */
1061 if((err = line6_init_pcm(line6, &pod_pcm_properties)) < 0) {
1062 pod_destruct(interface);
1063 return err;
1064 }
1065
1066 /* register audio system: */
1067 if((err = line6_register_audio(line6)) < 0) {
1068 pod_destruct(interface);
1069 return err;
1070 }
1071
1072 if(pod->line6.properties->capabilities & LINE6_BIT_CONTROL) {
1073 /* query some data: */
1074 line6_startup_delayed(&pod->dumpreq, POD_STARTUP_DELAY, pod_startup_timeout, pod);
1075 line6_read_serial_number(&pod->line6, &pod->serial_number);
1076 }
1077
1078 return 0;
1079}
1080
1081/*
1082 POD device disconnected.
1083*/
1084void pod_disconnect(struct usb_interface *interface)
1085{
1086 struct usb_line6_pod *pod;
1087
1088 if(interface == NULL) return;
1089 pod = usb_get_intfdata(interface);
1090
1091 if(pod != NULL) {
1092 struct snd_line6_pcm *line6pcm = pod->line6.line6pcm;
1093 struct device *dev = &interface->dev;
1094
1095 if(line6pcm != NULL) {
1096 unlink_wait_clear_audio_out_urbs(line6pcm);
1097 unlink_wait_clear_audio_in_urbs(line6pcm);
1098 }
1099
1100 if(dev != NULL) {
1101 /* remove sysfs entries: */
1102 if(pod->versionreq_ok)
1103 pod_remove_files(pod->firmware_version, pod->line6.properties->device_bit, dev);
1104
1105 device_remove_file(dev, &dev_attr_channel);
1106 device_remove_file(dev, &dev_attr_clip);
1107 device_remove_file(dev, &dev_attr_device_id);
1108 device_remove_file(dev, &dev_attr_dirty);
1109 device_remove_file(dev, &dev_attr_dump);
1110 device_remove_file(dev, &dev_attr_dump_buf);
1111 device_remove_file(dev, &dev_attr_finish);
1112 device_remove_file(dev, &dev_attr_firmware_version);
1113 device_remove_file(dev, &dev_attr_midi_postprocess);
1114 device_remove_file(dev, &dev_attr_monitor_level);
1115 device_remove_file(dev, &dev_attr_name);
1116 device_remove_file(dev, &dev_attr_name_buf);
1117 device_remove_file(dev, &dev_attr_retrieve_amp_setup);
1118 device_remove_file(dev, &dev_attr_retrieve_channel);
1119 device_remove_file(dev, &dev_attr_retrieve_effects_setup);
1120 device_remove_file(dev, &dev_attr_routing);
1121 device_remove_file(dev, &dev_attr_serial_number);
1122 device_remove_file(dev, &dev_attr_store_amp_setup);
1123 device_remove_file(dev, &dev_attr_store_channel);
1124 device_remove_file(dev, &dev_attr_store_effects_setup);
1125 device_remove_file(dev, &dev_attr_tuner_freq);
1126 device_remove_file(dev, &dev_attr_tuner_mute);
1127 device_remove_file(dev, &dev_attr_tuner_note);
1128 device_remove_file(dev, &dev_attr_tuner_pitch);
1129
1130#if CREATE_RAW_FILE
1131 device_remove_file(dev, &dev_attr_raw);
1132#endif
1133 }
1134 }
1135
1136 pod_destruct(interface);
1137}