Merge branch 'llseek' of git://git.kernel.org/pub/scm/linux/kernel/git/arnd/bkl
[deliverable/linux.git] / drivers / staging / line6 / variax.c
1 /*
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 <linux/slab.h>
15
16 #include "audio.h"
17 #include "control.h"
18 #include "variax.h"
19
20
21 #define VARIAX_SYSEX_CODE 7
22 #define VARIAX_SYSEX_PARAM 0x3b
23 #define VARIAX_SYSEX_ACTIVATE 0x2a
24 #define VARIAX_MODEL_HEADER_LENGTH 7
25 #define VARIAX_MODEL_MESSAGE_LENGTH 199
26 #define VARIAX_OFFSET_ACTIVATE 7
27
28
29 static const char variax_activate[] = {
30 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x2a, 0x01,
31 0xf7
32 };
33 static const char variax_request_bank[] = {
34 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x6d, 0xf7
35 };
36 static const char variax_request_model1[] = {
37 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x3c, 0x00,
38 0x02, 0x00, 0x00, 0x00, 0x00, 0x03, 0x05, 0x03,
39 0x00, 0x00, 0x00, 0xf7
40 };
41 static const char variax_request_model2[] = {
42 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x3c, 0x00,
43 0x02, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x03,
44 0x00, 0x00, 0x00, 0xf7
45 };
46
47
48 /*
49 Decode data transmitted by workbench.
50 */
51 static void variax_decode(const unsigned char *raw_data, unsigned char *data,
52 int raw_size)
53 {
54 for (; raw_size > 0; raw_size -= 6) {
55 data[2] = raw_data[0] | (raw_data[1] << 4);
56 data[1] = raw_data[2] | (raw_data[3] << 4);
57 data[0] = raw_data[4] | (raw_data[5] << 4);
58 raw_data += 6;
59 data += 3;
60 }
61 }
62
63 static void variax_activate_timeout(unsigned long arg)
64 {
65 struct usb_line6_variax *variax = (struct usb_line6_variax *)arg;
66 variax->buffer_activate[VARIAX_OFFSET_ACTIVATE] = 1;
67 line6_send_raw_message_async(&variax->line6, variax->buffer_activate,
68 sizeof(variax_activate));
69 }
70
71 /*
72 Send an asynchronous activation request after a given interval.
73 */
74 static void variax_activate_delayed(struct usb_line6_variax *variax,
75 int seconds)
76 {
77 variax->activate_timer.expires = jiffies + seconds * HZ;
78 variax->activate_timer.function = variax_activate_timeout;
79 variax->activate_timer.data = (unsigned long)variax;
80 add_timer(&variax->activate_timer);
81 }
82
83 static void variax_startup_timeout(unsigned long arg)
84 {
85 struct usb_line6_variax *variax = (struct usb_line6_variax *)arg;
86
87 if (variax->dumpreq.ok)
88 return;
89
90 line6_dump_request_async(&variax->dumpreq, &variax->line6, 0);
91 line6_startup_delayed(&variax->dumpreq, 1, variax_startup_timeout,
92 variax);
93 }
94
95 /*
96 Process a completely received message.
97 */
98 void variax_process_message(struct usb_line6_variax *variax)
99 {
100 const unsigned char *buf = variax->line6.buffer_message;
101
102 switch (buf[0]) {
103 case LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST:
104 switch (buf[1]) {
105 case VARIAXMIDI_volume:
106 variax->volume = buf[2];
107 break;
108
109 case VARIAXMIDI_tone:
110 variax->tone = buf[2];
111 }
112
113 break;
114
115 case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_DEVICE:
116 case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST:
117 variax->model = buf[1];
118 line6_dump_request_async(&variax->dumpreq, &variax->line6, 0);
119 break;
120
121 case LINE6_RESET:
122 dev_info(variax->line6.ifcdev, "VARIAX reset\n");
123 variax_activate_delayed(variax, VARIAX_ACTIVATE_DELAY);
124 break;
125
126 case LINE6_SYSEX_BEGIN:
127 if (memcmp(buf + 1, variax_request_model1 + 1,
128 VARIAX_MODEL_HEADER_LENGTH - 1) == 0) {
129 if (variax->line6.message_length ==
130 VARIAX_MODEL_MESSAGE_LENGTH) {
131 switch (variax->dumpreq.in_progress) {
132 case VARIAX_DUMP_PASS1:
133 variax_decode(buf + VARIAX_MODEL_HEADER_LENGTH, (unsigned char *)&variax->model_data,
134 (sizeof(variax->model_data.name) + sizeof(variax->model_data.control) / 2) * 2);
135 line6_dump_request_async(&variax->dumpreq, &variax->line6, 1);
136 line6_dump_started(&variax->dumpreq, VARIAX_DUMP_PASS2);
137 break;
138
139 case VARIAX_DUMP_PASS2:
140 /* model name is transmitted twice, so skip it here: */
141 variax_decode(buf + VARIAX_MODEL_HEADER_LENGTH,
142 (unsigned char *)&variax->model_data.control + sizeof(variax->model_data.control) / 2,
143 sizeof(variax->model_data.control) / 2 * 2);
144 variax->dumpreq.ok = 1;
145 line6_dump_request_async(&variax->dumpreq, &variax->line6, 2);
146 line6_dump_started(&variax->dumpreq, VARIAX_DUMP_PASS3);
147 }
148 } else {
149 DEBUG_MESSAGES(dev_err(variax->line6.ifcdev, "illegal length %d of model data\n", variax->line6.message_length));
150 line6_dump_finished(&variax->dumpreq);
151 }
152 } else if (memcmp(buf + 1, variax_request_bank + 1,
153 sizeof(variax_request_bank) - 2) == 0) {
154 memcpy(variax->bank,
155 buf + sizeof(variax_request_bank) - 1,
156 sizeof(variax->bank));
157 variax->dumpreq.ok = 1;
158 line6_dump_finished(&variax->dumpreq);
159 }
160
161 break;
162
163 case LINE6_SYSEX_END:
164 break;
165
166 default:
167 DEBUG_MESSAGES(dev_err(variax->line6.ifcdev, "Variax: unknown message %02X\n", buf[0]));
168 }
169 }
170
171 /*
172 "read" request on "volume" special file.
173 */
174 static ssize_t variax_get_volume(struct device *dev,
175 struct device_attribute *attr, char *buf)
176 {
177 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
178 return sprintf(buf, "%d\n", variax->volume);
179 }
180
181 /*
182 "write" request on "volume" special file.
183 */
184 static ssize_t variax_set_volume(struct device *dev,
185 struct device_attribute *attr,
186 const char *buf, size_t count)
187 {
188 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
189 unsigned long value;
190 int ret;
191
192 ret = strict_strtoul(buf, 10, &value);
193 if (ret)
194 return ret;
195
196 if (line6_transmit_parameter(&variax->line6, VARIAXMIDI_volume,
197 value) == 0)
198 variax->volume = value;
199
200 return count;
201 }
202
203 /*
204 "read" request on "model" special file.
205 */
206 static ssize_t variax_get_model(struct device *dev,
207 struct device_attribute *attr, char *buf)
208 {
209 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
210 return sprintf(buf, "%d\n", variax->model);
211 }
212
213 /*
214 "write" request on "model" special file.
215 */
216 static ssize_t variax_set_model(struct device *dev,
217 struct device_attribute *attr,
218 const char *buf, size_t count)
219 {
220 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
221 unsigned long value;
222 int ret;
223
224 ret = strict_strtoul(buf, 10, &value);
225 if (ret)
226 return ret;
227
228 if (line6_send_program(&variax->line6, value) == 0)
229 variax->model = value;
230
231 return count;
232 }
233
234 /*
235 "read" request on "active" special file.
236 */
237 static ssize_t variax_get_active(struct device *dev,
238 struct device_attribute *attr, char *buf)
239 {
240 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
241 return sprintf(buf, "%d\n", variax->buffer_activate[VARIAX_OFFSET_ACTIVATE]);
242 }
243
244 /*
245 "write" request on "active" special file.
246 */
247 static ssize_t variax_set_active(struct device *dev,
248 struct device_attribute *attr,
249 const char *buf, size_t count)
250 {
251 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
252 unsigned long value;
253 int ret;
254
255 ret = strict_strtoul(buf, 10, &value);
256 if (ret)
257 return ret;
258
259 variax->buffer_activate[VARIAX_OFFSET_ACTIVATE] = value ? 1 : 0;
260 line6_send_raw_message_async(&variax->line6, variax->buffer_activate,
261 sizeof(variax_activate));
262 return count;
263 }
264
265 /*
266 "read" request on "tone" special file.
267 */
268 static ssize_t variax_get_tone(struct device *dev,
269 struct device_attribute *attr, char *buf)
270 {
271 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
272 return sprintf(buf, "%d\n", variax->tone);
273 }
274
275 /*
276 "write" request on "tone" special file.
277 */
278 static ssize_t variax_set_tone(struct device *dev,
279 struct device_attribute *attr,
280 const char *buf, size_t count)
281 {
282 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
283 unsigned long value;
284 int ret;
285
286 ret = strict_strtoul(buf, 10, &value);
287 if (ret)
288 return ret;
289
290 if (line6_transmit_parameter(&variax->line6, VARIAXMIDI_tone,
291 value) == 0)
292 variax->tone = value;
293
294 return count;
295 }
296
297 static ssize_t get_string(char *buf, const char *data, int length)
298 {
299 int i;
300 memcpy(buf, data, length);
301
302 for (i = length; i--;) {
303 char c = buf[i];
304
305 if ((c != 0) && (c != ' '))
306 break;
307 }
308
309 buf[i + 1] = '\n';
310 return i + 2;
311 }
312
313 /*
314 "read" request on "name" special file.
315 */
316 static ssize_t variax_get_name(struct device *dev,
317 struct device_attribute *attr, char *buf)
318 {
319 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
320 line6_wait_dump(&variax->dumpreq, 0);
321 return get_string(buf, variax->model_data.name,
322 sizeof(variax->model_data.name));
323 }
324
325 /*
326 "read" request on "bank" special file.
327 */
328 static ssize_t variax_get_bank(struct device *dev,
329 struct device_attribute *attr, char *buf)
330 {
331 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
332 line6_wait_dump(&variax->dumpreq, 0);
333 return get_string(buf, variax->bank, sizeof(variax->bank));
334 }
335
336 /*
337 "read" request on "dump" special file.
338 */
339 static ssize_t variax_get_dump(struct device *dev,
340 struct device_attribute *attr, char *buf)
341 {
342 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
343 int retval;
344 retval = line6_wait_dump(&variax->dumpreq, 0);
345 if (retval < 0)
346 return retval;
347 memcpy(buf, &variax->model_data.control,
348 sizeof(variax->model_data.control));
349 return sizeof(variax->model_data.control);
350 }
351
352 #if CREATE_RAW_FILE
353
354 /*
355 "write" request on "raw" special file.
356 */
357 static ssize_t variax_set_raw2(struct device *dev,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
360 {
361 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
362 int size;
363 int i;
364 char *sysex;
365
366 count -= count % 3;
367 size = count * 2;
368 sysex = variax_alloc_sysex_buffer(variax, VARIAX_SYSEX_PARAM, size);
369
370 if (!sysex)
371 return 0;
372
373 for (i = 0; i < count; i += 3) {
374 const unsigned char *p1 = buf + i;
375 char *p2 = sysex + SYSEX_DATA_OFS + i * 2;
376 p2[0] = p1[2] & 0x0f;
377 p2[1] = p1[2] >> 4;
378 p2[2] = p1[1] & 0x0f;
379 p2[3] = p1[1] >> 4;
380 p2[4] = p1[0] & 0x0f;
381 p2[5] = p1[0] >> 4;
382 }
383
384 line6_send_sysex_message(&variax->line6, sysex, size);
385 kfree(sysex);
386 return count;
387 }
388
389 #endif
390
391 /* Variax workbench special files: */
392 static DEVICE_ATTR(model, S_IWUGO | S_IRUGO, variax_get_model, variax_set_model);
393 static DEVICE_ATTR(volume, S_IWUGO | S_IRUGO, variax_get_volume, variax_set_volume);
394 static DEVICE_ATTR(tone, S_IWUGO | S_IRUGO, variax_get_tone, variax_set_tone);
395 static DEVICE_ATTR(name, S_IRUGO, variax_get_name, line6_nop_write);
396 static DEVICE_ATTR(bank, S_IRUGO, variax_get_bank, line6_nop_write);
397 static DEVICE_ATTR(dump, S_IRUGO, variax_get_dump, line6_nop_write);
398 static DEVICE_ATTR(active, S_IWUGO | S_IRUGO, variax_get_active, variax_set_active);
399
400 #if CREATE_RAW_FILE
401 static DEVICE_ATTR(raw, S_IWUGO, line6_nop_read, line6_set_raw);
402 static DEVICE_ATTR(raw2, S_IWUGO, line6_nop_read, variax_set_raw2);
403 #endif
404
405
406 /*
407 Variax destructor.
408 */
409 static void variax_destruct(struct usb_interface *interface)
410 {
411 struct usb_line6_variax *variax = usb_get_intfdata(interface);
412 struct usb_line6 *line6;
413
414 if (variax == NULL)
415 return;
416 line6 = &variax->line6;
417 if (line6 == NULL)
418 return;
419 line6_cleanup_audio(line6);
420
421 /* free dump request data: */
422 line6_dumpreq_destructbuf(&variax->dumpreq, 2);
423 line6_dumpreq_destructbuf(&variax->dumpreq, 1);
424 line6_dumpreq_destruct(&variax->dumpreq);
425
426 kfree(variax->buffer_activate);
427 del_timer_sync(&variax->activate_timer);
428 }
429
430 /*
431 Create sysfs entries.
432 */
433 static int variax_create_files2(struct device *dev)
434 {
435 int err;
436 CHECK_RETURN(device_create_file(dev, &dev_attr_model));
437 CHECK_RETURN(device_create_file(dev, &dev_attr_volume));
438 CHECK_RETURN(device_create_file(dev, &dev_attr_tone));
439 CHECK_RETURN(device_create_file(dev, &dev_attr_name));
440 CHECK_RETURN(device_create_file(dev, &dev_attr_bank));
441 CHECK_RETURN(device_create_file(dev, &dev_attr_dump));
442 CHECK_RETURN(device_create_file(dev, &dev_attr_active));
443 #if CREATE_RAW_FILE
444 CHECK_RETURN(device_create_file(dev, &dev_attr_raw));
445 CHECK_RETURN(device_create_file(dev, &dev_attr_raw2));
446 #endif
447 return 0;
448 }
449
450 /*
451 Init workbench device.
452 */
453 int variax_init(struct usb_interface *interface,
454 struct usb_line6_variax *variax)
455 {
456 int err;
457
458 if ((interface == NULL) || (variax == NULL))
459 return -ENODEV;
460
461 /* initialize USB buffers: */
462 err = line6_dumpreq_init(&variax->dumpreq, variax_request_model1,
463 sizeof(variax_request_model1));
464
465 if (err < 0) {
466 dev_err(&interface->dev, "Out of memory\n");
467 variax_destruct(interface);
468 return err;
469 }
470
471 err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_model2,
472 sizeof(variax_request_model2), 1);
473
474 if (err < 0) {
475 dev_err(&interface->dev, "Out of memory\n");
476 variax_destruct(interface);
477 return err;
478 }
479
480 err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_bank,
481 sizeof(variax_request_bank), 2);
482
483 if (err < 0) {
484 dev_err(&interface->dev, "Out of memory\n");
485 variax_destruct(interface);
486 return err;
487 }
488
489 variax->buffer_activate = kmemdup(variax_activate,
490 sizeof(variax_activate), GFP_KERNEL);
491
492 if (variax->buffer_activate == NULL) {
493 dev_err(&interface->dev, "Out of memory\n");
494 variax_destruct(interface);
495 return -ENOMEM;
496 }
497
498 init_timer(&variax->activate_timer);
499
500 /* create sysfs entries: */
501 err = variax_create_files(0, 0, &interface->dev);
502 if (err < 0) {
503 variax_destruct(interface);
504 return err;
505 }
506
507 err = variax_create_files2(&interface->dev);
508 if (err < 0) {
509 variax_destruct(interface);
510 return err;
511 }
512
513 /* initialize audio system: */
514 err = line6_init_audio(&variax->line6);
515 if (err < 0) {
516 variax_destruct(interface);
517 return err;
518 }
519
520 /* initialize MIDI subsystem: */
521 err = line6_init_midi(&variax->line6);
522 if (err < 0) {
523 variax_destruct(interface);
524 return err;
525 }
526
527 /* register audio system: */
528 err = line6_register_audio(&variax->line6);
529 if (err < 0) {
530 variax_destruct(interface);
531 return err;
532 }
533
534 variax_activate_delayed(variax, VARIAX_ACTIVATE_DELAY);
535 line6_startup_delayed(&variax->dumpreq, VARIAX_STARTUP_DELAY,
536 variax_startup_timeout, variax);
537 return 0;
538 }
539
540 /*
541 Workbench device disconnected.
542 */
543 void variax_disconnect(struct usb_interface *interface)
544 {
545 struct device *dev;
546
547 if (interface == NULL)
548 return;
549 dev = &interface->dev;
550
551 if (dev != NULL) {
552 /* remove sysfs entries: */
553 variax_remove_files(0, 0, dev);
554 device_remove_file(dev, &dev_attr_model);
555 device_remove_file(dev, &dev_attr_volume);
556 device_remove_file(dev, &dev_attr_tone);
557 device_remove_file(dev, &dev_attr_name);
558 device_remove_file(dev, &dev_attr_bank);
559 device_remove_file(dev, &dev_attr_dump);
560 device_remove_file(dev, &dev_attr_active);
561 #if CREATE_RAW_FILE
562 device_remove_file(dev, &dev_attr_raw);
563 device_remove_file(dev, &dev_attr_raw2);
564 #endif
565 }
566
567 variax_destruct(interface);
568 }
This page took 0.047079 seconds and 5 git commands to generate.