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.
19 #define VARIAX_SYSEX_CODE 7
20 #define VARIAX_SYSEX_PARAM 0x3b
21 #define VARIAX_SYSEX_ACTIVATE 0x2a
22 #define VARIAX_MODEL_HEADER_LENGTH 7
23 #define VARIAX_MODEL_MESSAGE_LENGTH 199
24 #define VARIAX_OFFSET_ACTIVATE 7
27 static const char variax_activate[] = {
28 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x2a, 0x01,
31 static const char variax_request_bank[] = {
32 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x6d, 0xf7
34 static const char variax_request_model1[] = {
35 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x3c, 0x00,
36 0x02, 0x00, 0x00, 0x00, 0x00, 0x03, 0x05, 0x03,
37 0x00, 0x00, 0x00, 0xf7
39 static const char variax_request_model2[] = {
40 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x3c, 0x00,
41 0x02, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x03,
42 0x00, 0x00, 0x00, 0xf7
47 Decode data transmitted by workbench.
49 static void variax_decode(const unsigned char *raw_data, unsigned char *data, int raw_size)
51 for(; raw_size > 0; raw_size -= 6) {
52 data[2] = raw_data[0] | (raw_data[1] << 4);
53 data[1] = raw_data[2] | (raw_data[3] << 4);
54 data[0] = raw_data[4] | (raw_data[5] << 4);
60 static void variax_activate_timeout(unsigned long arg)
62 struct usb_line6_variax *variax = (struct usb_line6_variax *)arg;
63 variax->buffer_activate[VARIAX_OFFSET_ACTIVATE] = 1;
64 line6_send_raw_message_async(&variax->line6, variax->buffer_activate, sizeof(variax_activate));
68 Send an asynchronous activation request after a given interval.
70 static void variax_activate_delayed(struct usb_line6_variax *variax, int seconds)
72 variax->activate_timer.expires = jiffies + seconds * HZ;
73 variax->activate_timer.function = variax_activate_timeout;
74 variax->activate_timer.data = (unsigned long)variax;
75 add_timer(&variax->activate_timer);
78 static void variax_startup_timeout(unsigned long arg)
80 struct usb_line6_variax *variax = (struct usb_line6_variax *)arg;
82 if(variax->dumpreq.ok)
85 line6_dump_request_async(&variax->dumpreq, &variax->line6, 0);
86 line6_startup_delayed(&variax->dumpreq, 1, variax_startup_timeout, variax);
90 Process a completely received message.
92 void variax_process_message(struct usb_line6_variax *variax)
94 const unsigned char *buf = variax->line6.buffer_message;
97 case LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST:
99 case VARIAXMIDI_volume:
100 variax->volume = buf[2];
103 case VARIAXMIDI_tone:
104 variax->tone = buf[2];
109 case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_DEVICE:
110 case LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST:
111 variax->model = buf[1];
112 line6_dump_request_async(&variax->dumpreq, &variax->line6, 0);
116 dev_info(variax->line6.ifcdev, "VARIAX reset\n");
117 variax_activate_delayed(variax, VARIAX_ACTIVATE_DELAY);
120 case LINE6_SYSEX_BEGIN:
121 if(memcmp(buf + 1, variax_request_model1 + 1, VARIAX_MODEL_HEADER_LENGTH - 1) == 0) {
122 if(variax->line6.message_length == VARIAX_MODEL_MESSAGE_LENGTH) {
123 switch(variax->dumpreq.in_progress) {
124 case VARIAX_DUMP_PASS1:
125 variax_decode(buf + VARIAX_MODEL_HEADER_LENGTH, (unsigned char *)&variax->model_data,
126 (sizeof(variax->model_data.name) + sizeof(variax->model_data.control) / 2) * 2);
127 line6_dump_request_async(&variax->dumpreq, &variax->line6, 1);
128 line6_dump_started(&variax->dumpreq, VARIAX_DUMP_PASS2);
131 case VARIAX_DUMP_PASS2:
132 /* model name is transmitted twice, so skip it here: */
133 variax_decode(buf + VARIAX_MODEL_HEADER_LENGTH,
134 (unsigned char *)&variax->model_data.control + sizeof(variax->model_data.control) / 2,
135 sizeof(variax->model_data.control) / 2 * 2);
136 variax->dumpreq.ok = 1;
137 line6_dump_request_async(&variax->dumpreq, &variax->line6, 2);
138 line6_dump_started(&variax->dumpreq, VARIAX_DUMP_PASS3);
142 DEBUG_MESSAGES(dev_err(variax->line6.ifcdev, "illegal length %d of model data\n", variax->line6.message_length));
143 line6_dump_finished(&variax->dumpreq);
146 else if(memcmp(buf + 1, variax_request_bank + 1, sizeof(variax_request_bank) - 2) == 0) {
147 memcpy(variax->bank, buf + sizeof(variax_request_bank) - 1, sizeof(variax->bank));
148 variax->dumpreq.ok = 1;
149 line6_dump_finished(&variax->dumpreq);
154 case LINE6_SYSEX_END:
158 DEBUG_MESSAGES(dev_err(variax->line6.ifcdev, "Variax: unknown message %02X\n", buf[0]));
163 "read" request on "volume" special file.
165 static ssize_t variax_get_volume(struct device *dev,
166 struct device_attribute *attr, char *buf)
168 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
169 return sprintf(buf, "%d\n", variax->volume);
173 "write" request on "volume" special file.
175 static ssize_t variax_set_volume(struct device *dev,
176 struct device_attribute *attr,
177 const char *buf, size_t count)
179 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
180 int value = simple_strtoul(buf, NULL, 10);
182 if(line6_transmit_parameter(&variax->line6, VARIAXMIDI_volume, value) == 0)
183 variax->volume = value;
189 "read" request on "model" special file.
191 static ssize_t variax_get_model(struct device *dev,
192 struct device_attribute *attr, char *buf)
194 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
195 return sprintf(buf, "%d\n", variax->model);
199 "write" request on "model" special file.
201 static ssize_t variax_set_model(struct device *dev,
202 struct device_attribute *attr,
203 const char *buf, size_t count)
205 struct usb_line6_variax *variax = usb_get_intfdata( to_usb_interface(dev));
206 int value = simple_strtoul(buf, NULL, 10);
208 if(line6_send_program(&variax->line6, value) == 0)
209 variax->model = value;
215 "read" request on "active" special file.
217 static ssize_t variax_get_active(struct device *dev,
218 struct device_attribute *attr, char *buf)
220 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
221 return sprintf(buf, "%d\n", variax->buffer_activate[VARIAX_OFFSET_ACTIVATE]);
225 "write" request on "active" special file.
227 static ssize_t variax_set_active(struct device *dev,
228 struct device_attribute *attr,
229 const char *buf, size_t count)
231 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
232 int value = simple_strtoul(buf, NULL, 10) ? 1 : 0;
233 variax->buffer_activate[VARIAX_OFFSET_ACTIVATE] = value;
234 line6_send_raw_message_async(&variax->line6, variax->buffer_activate, sizeof(variax_activate));
239 "read" request on "tone" special file.
241 static ssize_t variax_get_tone(struct device *dev,
242 struct device_attribute *attr, char *buf)
244 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
245 return sprintf(buf, "%d\n", variax->tone);
249 "write" request on "tone" special file.
251 static ssize_t variax_set_tone(struct device *dev,
252 struct device_attribute *attr,
253 const char *buf, size_t count)
255 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
256 int value = simple_strtoul(buf, NULL, 10);
258 if(line6_transmit_parameter(&variax->line6, VARIAXMIDI_tone, value) == 0)
259 variax->tone = value;
264 static ssize_t get_string(char *buf, const char *data, int length)
267 memcpy(buf, data, length);
269 for(i = length; i--;) {
272 if((c != 0) && (c != ' '))
281 "read" request on "name" special file.
283 static ssize_t variax_get_name(struct device *dev,
284 struct device_attribute *attr, char *buf)
286 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
287 line6_wait_dump(&variax->dumpreq, 0);
288 return get_string(buf, variax->model_data.name, sizeof(variax->model_data.name));
292 "read" request on "bank" special file.
294 static ssize_t variax_get_bank(struct device *dev,
295 struct device_attribute *attr, char *buf)
297 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
298 line6_wait_dump(&variax->dumpreq, 0);
299 return get_string(buf, variax->bank, sizeof(variax->bank));
303 "read" request on "dump" special file.
305 static ssize_t variax_get_dump(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
310 retval = line6_wait_dump(&variax->dumpreq, 0);
311 if(retval < 0) return retval;
312 memcpy(buf, &variax->model_data.control, sizeof(variax->model_data.control));
313 return sizeof(variax->model_data.control);
319 "write" request on "raw" special file.
321 static ssize_t variax_set_raw2(struct device *dev,
322 struct device_attribute *attr,
323 const char *buf, size_t count)
325 struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
332 sysex = variax_alloc_sysex_buffer(variax, VARIAX_SYSEX_PARAM, size);
337 for(i = 0; i < count; i += 3) {
338 const unsigned char *p1 = buf + i;
339 char *p2 = sysex + SYSEX_DATA_OFS + i * 2;
340 p2[0] = p1[2] & 0x0f;
342 p2[2] = p1[1] & 0x0f;
344 p2[4] = p1[0] & 0x0f;
348 line6_send_sysex_message(&variax->line6, sysex, size);
355 /* Variax workbench special files: */
356 static DEVICE_ATTR(model, S_IWUGO | S_IRUGO, variax_get_model, variax_set_model);
357 static DEVICE_ATTR(volume, S_IWUGO | S_IRUGO, variax_get_volume, variax_set_volume);
358 static DEVICE_ATTR(tone, S_IWUGO | S_IRUGO, variax_get_tone, variax_set_tone);
359 static DEVICE_ATTR(name, S_IRUGO, variax_get_name, line6_nop_write);
360 static DEVICE_ATTR(bank, S_IRUGO, variax_get_bank, line6_nop_write);
361 static DEVICE_ATTR(dump, S_IRUGO, variax_get_dump, line6_nop_write);
362 static DEVICE_ATTR(active, S_IWUGO | S_IRUGO, variax_get_active, variax_set_active);
365 static DEVICE_ATTR(raw, S_IWUGO, line6_nop_read, line6_set_raw);
366 static DEVICE_ATTR(raw2, S_IWUGO, line6_nop_read, variax_set_raw2);
373 static void variax_destruct(struct usb_interface *interface)
375 struct usb_line6_variax *variax = usb_get_intfdata(interface);
376 struct usb_line6 *line6;
378 if(variax == NULL) return;
379 line6 = &variax->line6;
380 if(line6 == NULL) return;
381 line6_cleanup_audio(line6);
383 /* free dump request data: */
384 line6_dumpreq_destructbuf(&variax->dumpreq, 2);
385 line6_dumpreq_destructbuf(&variax->dumpreq, 1);
386 line6_dumpreq_destruct(&variax->dumpreq);
388 if(variax->buffer_activate) kfree(variax->buffer_activate);
389 del_timer_sync(&variax->activate_timer);
393 Create sysfs entries.
395 int variax_create_files2(struct device *dev)
398 CHECK_RETURN(device_create_file(dev, &dev_attr_model));
399 CHECK_RETURN(device_create_file(dev, &dev_attr_volume));
400 CHECK_RETURN(device_create_file(dev, &dev_attr_tone));
401 CHECK_RETURN(device_create_file(dev, &dev_attr_name));
402 CHECK_RETURN(device_create_file(dev, &dev_attr_bank));
403 CHECK_RETURN(device_create_file(dev, &dev_attr_dump));
404 CHECK_RETURN(device_create_file(dev, &dev_attr_active));
406 CHECK_RETURN(device_create_file(dev, &dev_attr_raw));
407 CHECK_RETURN(device_create_file(dev, &dev_attr_raw2));
413 Init workbench device.
415 int variax_init(struct usb_interface *interface, struct usb_line6_variax *variax)
419 if((interface == NULL) || (variax == NULL)) return -ENODEV;
421 /* initialize USB buffers: */
422 err = line6_dumpreq_init(&variax->dumpreq, variax_request_model1, sizeof(variax_request_model1));
425 dev_err(&interface->dev, "Out of memory\n");
426 variax_destruct(interface);
430 err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_model2, sizeof(variax_request_model2), 1);
433 dev_err(&interface->dev, "Out of memory\n");
434 variax_destruct(interface);
438 err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_bank, sizeof(variax_request_bank), 2);
441 dev_err(&interface->dev, "Out of memory\n");
442 variax_destruct(interface);
446 variax->buffer_activate = kmalloc(sizeof(variax_activate), GFP_KERNEL);
448 if(variax->buffer_activate == NULL) {
449 dev_err(&interface->dev, "Out of memory\n");
450 variax_destruct(interface);
454 memcpy(variax->buffer_activate, variax_activate, sizeof(variax_activate));
455 init_timer(&variax->activate_timer);
457 /* create sysfs entries: */
458 if((err = variax_create_files(0, 0, &interface->dev)) < 0) {
459 variax_destruct(interface);
463 if((err = variax_create_files2(&interface->dev)) < 0) {
464 variax_destruct(interface);
468 /* initialize audio system: */
469 if((err = line6_init_audio(&variax->line6)) < 0) {
470 variax_destruct(interface);
474 /* initialize MIDI subsystem: */
475 if((err = line6_init_midi(&variax->line6)) < 0) {
476 variax_destruct(interface);
480 /* register audio system: */
481 if((err = line6_register_audio(&variax->line6)) < 0) {
482 variax_destruct(interface);
486 variax_activate_delayed(variax, VARIAX_ACTIVATE_DELAY);
487 line6_startup_delayed(&variax->dumpreq, VARIAX_STARTUP_DELAY, variax_startup_timeout, variax);
492 Workbench device disconnected.
494 void variax_disconnect(struct usb_interface *interface)
498 if(interface == NULL) return;
499 dev = &interface->dev;
502 /* remove sysfs entries: */
503 variax_remove_files(0, 0, dev);
504 device_remove_file(dev, &dev_attr_model);
505 device_remove_file(dev, &dev_attr_volume);
506 device_remove_file(dev, &dev_attr_tone);
507 device_remove_file(dev, &dev_attr_name);
508 device_remove_file(dev, &dev_attr_bank);
509 device_remove_file(dev, &dev_attr_dump);
510 device_remove_file(dev, &dev_attr_active);
512 device_remove_file(dev, &dev_attr_raw);
513 device_remove_file(dev, &dev_attr_raw2);
517 variax_destruct(interface);