writeback: split writeback_inodes_wb
[linux-2.6/next.git] / drivers / staging / line6 / variax.c
blob58ddbe6393ff7e3bf2ba2e17c4166c423fcafd97
1 /*
2 * Line6 Linux USB driver - 0.8.0
4 * Copyright (C) 2004-2009 Markus Grabner (grabner@icg.tugraz.at)
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.
12 #include "driver.h"
14 #include <linux/slab.h>
16 #include "audio.h"
17 #include "control.h"
18 #include "variax.h"
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
29 static const char variax_activate[] = {
30 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x2a, 0x01,
31 0xf7
33 static const char variax_request_bank[] = {
34 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x6d, 0xf7
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
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
49 Decode data transmitted by workbench.
51 static void variax_decode(const unsigned char *raw_data, unsigned char *data,
52 int raw_size)
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;
63 static void variax_activate_timeout(unsigned long arg)
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));
72 Send an asynchronous activation request after a given interval.
74 static void variax_activate_delayed(struct usb_line6_variax *variax,
75 int seconds)
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);
83 static void variax_startup_timeout(unsigned long arg)
85 struct usb_line6_variax *variax = (struct usb_line6_variax *)arg;
87 if (variax->dumpreq.ok)
88 return;
90 line6_dump_request_async(&variax->dumpreq, &variax->line6, 0);
91 line6_startup_delayed(&variax->dumpreq, 1, variax_startup_timeout,
92 variax);
96 Process a completely received message.
98 void variax_process_message(struct usb_line6_variax *variax)
100 const unsigned char *buf = variax->line6.buffer_message;
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;
109 case VARIAXMIDI_tone:
110 variax->tone = buf[2];
113 break;
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;
121 case LINE6_RESET:
122 dev_info(variax->line6.ifcdev, "VARIAX reset\n");
123 variax_activate_delayed(variax, VARIAX_ACTIVATE_DELAY);
124 break;
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;
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);
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);
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);
161 break;
163 case LINE6_SYSEX_END:
164 break;
166 default:
167 DEBUG_MESSAGES(dev_err(variax->line6.ifcdev, "Variax: unknown message %02X\n", buf[0]));
172 "read" request on "volume" special file.
174 static ssize_t variax_get_volume(struct device *dev,
175 struct device_attribute *attr, char *buf)
177 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
178 return sprintf(buf, "%d\n", variax->volume);
182 "write" request on "volume" special file.
184 static ssize_t variax_set_volume(struct device *dev,
185 struct device_attribute *attr,
186 const char *buf, size_t count)
188 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
189 unsigned long value;
190 int ret;
192 ret = strict_strtoul(buf, 10, &value);
193 if (ret)
194 return ret;
196 if (line6_transmit_parameter(&variax->line6, VARIAXMIDI_volume,
197 value) == 0)
198 variax->volume = value;
200 return count;
204 "read" request on "model" special file.
206 static ssize_t variax_get_model(struct device *dev,
207 struct device_attribute *attr, char *buf)
209 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
210 return sprintf(buf, "%d\n", variax->model);
214 "write" request on "model" special file.
216 static ssize_t variax_set_model(struct device *dev,
217 struct device_attribute *attr,
218 const char *buf, size_t count)
220 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
221 unsigned long value;
222 int ret;
224 ret = strict_strtoul(buf, 10, &value);
225 if (ret)
226 return ret;
228 if (line6_send_program(&variax->line6, value) == 0)
229 variax->model = value;
231 return count;
235 "read" request on "active" special file.
237 static ssize_t variax_get_active(struct device *dev,
238 struct device_attribute *attr, char *buf)
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]);
245 "write" request on "active" special file.
247 static ssize_t variax_set_active(struct device *dev,
248 struct device_attribute *attr,
249 const char *buf, size_t count)
251 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
252 unsigned long value;
253 int ret;
255 ret = strict_strtoul(buf, 10, &value);
256 if (ret)
257 return ret;
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;
266 "read" request on "tone" special file.
268 static ssize_t variax_get_tone(struct device *dev,
269 struct device_attribute *attr, char *buf)
271 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
272 return sprintf(buf, "%d\n", variax->tone);
276 "write" request on "tone" special file.
278 static ssize_t variax_set_tone(struct device *dev,
279 struct device_attribute *attr,
280 const char *buf, size_t count)
282 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
283 unsigned long value;
284 int ret;
286 ret = strict_strtoul(buf, 10, &value);
287 if (ret)
288 return ret;
290 if (line6_transmit_parameter(&variax->line6, VARIAXMIDI_tone,
291 value) == 0)
292 variax->tone = value;
294 return count;
297 static ssize_t get_string(char *buf, const char *data, int length)
299 int i;
300 memcpy(buf, data, length);
302 for (i = length; i--;) {
303 char c = buf[i];
305 if ((c != 0) && (c != ' '))
306 break;
309 buf[i + 1] = '\n';
310 return i + 2;
314 "read" request on "name" special file.
316 static ssize_t variax_get_name(struct device *dev,
317 struct device_attribute *attr, char *buf)
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));
326 "read" request on "bank" special file.
328 static ssize_t variax_get_bank(struct device *dev,
329 struct device_attribute *attr, char *buf)
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));
337 "read" request on "dump" special file.
339 static ssize_t variax_get_dump(struct device *dev,
340 struct device_attribute *attr, char *buf)
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);
352 #if CREATE_RAW_FILE
355 "write" request on "raw" special file.
357 static ssize_t variax_set_raw2(struct device *dev,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
361 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
362 int size;
363 int i;
364 char *sysex;
366 count -= count % 3;
367 size = count * 2;
368 sysex = variax_alloc_sysex_buffer(variax, VARIAX_SYSEX_PARAM, size);
370 if (!sysex)
371 return 0;
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;
384 line6_send_sysex_message(&variax->line6, sysex, size);
385 kfree(sysex);
386 return count;
389 #endif
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);
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
407 Variax destructor.
409 static void variax_destruct(struct usb_interface *interface)
411 struct usb_line6_variax *variax = usb_get_intfdata(interface);
412 struct usb_line6 *line6;
414 if (variax == NULL)
415 return;
416 line6 = &variax->line6;
417 if (line6 == NULL)
418 return;
419 line6_cleanup_audio(line6);
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);
426 kfree(variax->buffer_activate);
427 del_timer_sync(&variax->activate_timer);
431 Create sysfs entries.
433 static int variax_create_files2(struct device *dev)
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;
451 Init workbench device.
453 int variax_init(struct usb_interface *interface,
454 struct usb_line6_variax *variax)
456 int err;
458 if ((interface == NULL) || (variax == NULL))
459 return -ENODEV;
461 /* initialize USB buffers: */
462 err = line6_dumpreq_init(&variax->dumpreq, variax_request_model1,
463 sizeof(variax_request_model1));
465 if (err < 0) {
466 dev_err(&interface->dev, "Out of memory\n");
467 variax_destruct(interface);
468 return err;
471 err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_model2,
472 sizeof(variax_request_model2), 1);
474 if (err < 0) {
475 dev_err(&interface->dev, "Out of memory\n");
476 variax_destruct(interface);
477 return err;
480 err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_bank,
481 sizeof(variax_request_bank), 2);
483 if (err < 0) {
484 dev_err(&interface->dev, "Out of memory\n");
485 variax_destruct(interface);
486 return err;
489 variax->buffer_activate = kmemdup(variax_activate,
490 sizeof(variax_activate), GFP_KERNEL);
492 if (variax->buffer_activate == NULL) {
493 dev_err(&interface->dev, "Out of memory\n");
494 variax_destruct(interface);
495 return -ENOMEM;
498 init_timer(&variax->activate_timer);
500 /* create sysfs entries: */
501 err = variax_create_files(0, 0, &interface->dev);
502 if (err < 0) {
503 variax_destruct(interface);
504 return err;
507 err = variax_create_files2(&interface->dev);
508 if (err < 0) {
509 variax_destruct(interface);
510 return err;
513 /* initialize audio system: */
514 err = line6_init_audio(&variax->line6);
515 if (err < 0) {
516 variax_destruct(interface);
517 return err;
520 /* initialize MIDI subsystem: */
521 err = line6_init_midi(&variax->line6);
522 if (err < 0) {
523 variax_destruct(interface);
524 return err;
527 /* register audio system: */
528 err = line6_register_audio(&variax->line6);
529 if (err < 0) {
530 variax_destruct(interface);
531 return err;
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;
541 Workbench device disconnected.
543 void variax_disconnect(struct usb_interface *interface)
545 struct device *dev;
547 if (interface == NULL)
548 return;
549 dev = &interface->dev;
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
567 variax_destruct(interface);