]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/line6/variax.c
Staging: add line6 usb driver
[karo-tx-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 "audio.h"
15 #include "control.h"
16 #include "variax.h"
17
18
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
25
26
27 static const char variax_activate[] = {
28         0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x2a, 0x01,
29         0xf7
30 };
31 static const char variax_request_bank[] = {
32         0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x6d, 0xf7
33 };
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
38 };
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
43 };
44
45
46 /*
47         Decode data transmitted by workbench.
48 */
49 static void variax_decode(const unsigned char *raw_data, unsigned char *data, int raw_size)
50 {
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);
55                 raw_data += 6;
56                 data += 3;
57         }
58 }
59
60 static void variax_activate_timeout(unsigned long arg)
61 {
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));
65 }
66
67 /*
68         Send an asynchronous activation request after a given interval.
69 */
70 static void variax_activate_delayed(struct usb_line6_variax *variax, int seconds)
71 {
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);
76 }
77
78 static void variax_startup_timeout(unsigned long arg)
79 {
80         struct usb_line6_variax *variax = (struct usb_line6_variax *)arg;
81
82         if(variax->dumpreq.ok)
83                 return;
84
85         line6_dump_request_async(&variax->dumpreq, &variax->line6, 0);
86         line6_startup_delayed(&variax->dumpreq, 1, variax_startup_timeout, variax);
87 }
88
89 /*
90         Process a completely received message.
91 */
92 void variax_process_message(struct usb_line6_variax *variax)
93 {
94         const unsigned char *buf = variax->line6.buffer_message;
95
96         switch(buf[0]) {
97         case LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST:
98                 switch(buf[1]) {
99                 case VARIAXMIDI_volume:
100                         variax->volume = buf[2];
101                         break;
102
103                 case VARIAXMIDI_tone:
104                         variax->tone = buf[2];
105                 }
106
107                 break;
108
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);
113                 break;
114
115         case LINE6_RESET:
116                 dev_info(variax->line6.ifcdev, "VARIAX reset\n");
117                 variax_activate_delayed(variax, VARIAX_ACTIVATE_DELAY);
118                 break;
119
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);
129                                         break;
130
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);
139                                 }
140                         }
141                         else {
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);
144                         }
145                 }
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);
150                 }
151
152                 break;
153
154         case LINE6_SYSEX_END:
155                 break;
156
157         default:
158                 DEBUG_MESSAGES(dev_err(variax->line6.ifcdev, "Variax: unknown message %02X\n", buf[0]));
159         }
160 }
161
162 /*
163         "read" request on "volume" special file.
164 */
165 static ssize_t variax_get_volume(struct device *dev, DEVICE_ATTRIBUTE char *buf)
166 {
167         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
168         return sprintf(buf, "%d\n", variax->volume);
169 }
170
171 /*
172         "write" request on "volume" special file.
173 */
174 static ssize_t variax_set_volume(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
175 {
176         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
177         int value = simple_strtoul(buf, NULL, 10);
178
179         if(line6_transmit_parameter(&variax->line6, VARIAXMIDI_volume, value) == 0)
180                 variax->volume = value;
181
182         return count;
183 }
184
185 /*
186         "read" request on "model" special file.
187 */
188 static ssize_t variax_get_model(struct device *dev, DEVICE_ATTRIBUTE char *buf)
189 {
190         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
191         return sprintf(buf, "%d\n", variax->model);
192 }
193
194 /*
195         "write" request on "model" special file.
196 */
197 static ssize_t variax_set_model(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
198 {
199         struct usb_line6_variax *variax = usb_get_intfdata( to_usb_interface(dev));
200         int value = simple_strtoul(buf, NULL, 10);
201
202         if(line6_send_program(&variax->line6, value) == 0)
203                 variax->model = value;
204
205         return count;
206 }
207
208 /*
209         "read" request on "active" special file.
210 */
211 static ssize_t variax_get_active(struct device *dev, DEVICE_ATTRIBUTE char *buf)
212 {
213         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
214         return sprintf(buf, "%d\n", variax->buffer_activate[VARIAX_OFFSET_ACTIVATE]);
215 }
216
217 /*
218         "write" request on "active" special file.
219 */
220 static ssize_t variax_set_active(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
221 {
222         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
223         int value = simple_strtoul(buf, NULL, 10) ? 1 : 0;
224         variax->buffer_activate[VARIAX_OFFSET_ACTIVATE] = value;
225         line6_send_raw_message_async(&variax->line6, variax->buffer_activate, sizeof(variax_activate));
226         return count;
227 }
228
229 /*
230         "read" request on "tone" special file.
231 */
232 static ssize_t variax_get_tone(struct device *dev, DEVICE_ATTRIBUTE char *buf)
233 {
234         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
235         return sprintf(buf, "%d\n", variax->tone);
236 }
237
238 /*
239         "write" request on "tone" special file.
240 */
241 static ssize_t variax_set_tone(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
242 {
243         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
244         int value = simple_strtoul(buf, NULL, 10);
245
246         if(line6_transmit_parameter(&variax->line6, VARIAXMIDI_tone, value) == 0)
247                 variax->tone = value;
248
249         return count;
250 }
251
252 static ssize_t get_string(char *buf, const char *data, int length)
253 {
254         int i;
255         memcpy(buf, data, length);
256
257         for(i = length; i--;) {
258                 char c = buf[i];
259
260                 if((c != 0) && (c != ' '))
261                         break;
262         }
263
264         buf[i + 1] = '\n';
265         return i + 2;
266 }
267
268 /*
269         "read" request on "name" special file.
270 */
271 static ssize_t variax_get_name(struct device *dev, DEVICE_ATTRIBUTE char *buf)
272 {
273         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
274         line6_wait_dump(&variax->dumpreq, 0);
275         return get_string(buf, variax->model_data.name, sizeof(variax->model_data.name));
276 }
277
278 /*
279         "read" request on "bank" special file.
280 */
281 static ssize_t variax_get_bank(struct device *dev, DEVICE_ATTRIBUTE char *buf)
282 {
283         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
284         line6_wait_dump(&variax->dumpreq, 0);
285         return get_string(buf, variax->bank, sizeof(variax->bank));
286 }
287
288 /*
289         "read" request on "dump" special file.
290 */
291 static ssize_t variax_get_dump(struct device *dev, DEVICE_ATTRIBUTE char *buf)
292 {
293         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
294         int retval;
295         retval = line6_wait_dump(&variax->dumpreq, 0);
296         if(retval < 0) return retval;
297         memcpy(buf, &variax->model_data.control, sizeof(variax->model_data.control));
298         return sizeof(variax->model_data.control);
299 }
300
301 #if CREATE_RAW_FILE
302
303 /*
304         "write" request on "raw" special file.
305 */
306 static ssize_t variax_set_raw2(struct device *dev, DEVICE_ATTRIBUTE const char *buf, size_t count)
307 {
308         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
309         int size;
310         int i;
311         char *sysex;
312
313         count -= count % 3;
314         size = count * 2;
315         sysex = variax_alloc_sysex_buffer(variax, VARIAX_SYSEX_PARAM, size);
316
317         if(!sysex)
318                 return 0;
319
320         for(i = 0; i < count; i += 3) {
321                 const unsigned char *p1 = buf + i;
322                 char *p2 = sysex + SYSEX_DATA_OFS + i * 2;
323                 p2[0] = p1[2] & 0x0f;
324                 p2[1] = p1[2] >> 4;
325                 p2[2] = p1[1] & 0x0f;
326                 p2[3] = p1[1] >> 4;
327                 p2[4] = p1[0] & 0x0f;
328                 p2[5] = p1[0] >> 4;
329         }
330
331         line6_send_sysex_message(&variax->line6, sysex, size);
332         kfree(sysex);
333         return count;
334 }
335
336 #endif
337
338 /* Variax workbench special files: */
339 static DEVICE_ATTR(model, S_IWUGO | S_IRUGO, variax_get_model, variax_set_model);
340 static DEVICE_ATTR(volume, S_IWUGO | S_IRUGO, variax_get_volume, variax_set_volume);
341 static DEVICE_ATTR(tone, S_IWUGO | S_IRUGO, variax_get_tone, variax_set_tone);
342 static DEVICE_ATTR(name, S_IRUGO, variax_get_name, line6_nop_write);
343 static DEVICE_ATTR(bank, S_IRUGO, variax_get_bank, line6_nop_write);
344 static DEVICE_ATTR(dump, S_IRUGO, variax_get_dump, line6_nop_write);
345 static DEVICE_ATTR(active, S_IWUGO | S_IRUGO, variax_get_active, variax_set_active);
346
347 #if CREATE_RAW_FILE
348 static DEVICE_ATTR(raw, S_IWUGO, line6_nop_read, line6_set_raw);
349 static DEVICE_ATTR(raw2, S_IWUGO, line6_nop_read, variax_set_raw2);
350 #endif
351
352
353 /*
354         Variax destructor.
355 */
356 static void variax_destruct(struct usb_interface *interface)
357 {
358         struct usb_line6_variax *variax = usb_get_intfdata(interface);
359         struct usb_line6 *line6;
360
361         if(variax == NULL) return;
362         line6 = &variax->line6;
363         if(line6 == NULL) return;
364         line6_cleanup_audio(line6);
365
366         /* free dump request data: */
367         line6_dumpreq_destructbuf(&variax->dumpreq, 2);
368         line6_dumpreq_destructbuf(&variax->dumpreq, 1);
369         line6_dumpreq_destruct(&variax->dumpreq);
370
371         if(variax->buffer_activate) kfree(variax->buffer_activate);
372         del_timer_sync(&variax->activate_timer);
373 }
374
375 /*
376         Create sysfs entries.
377 */
378 int variax_create_files2(struct device *dev)
379 {
380         int err;
381         CHECK_RETURN(device_create_file(dev, &dev_attr_model));
382         CHECK_RETURN(device_create_file(dev, &dev_attr_volume));
383         CHECK_RETURN(device_create_file(dev, &dev_attr_tone));
384         CHECK_RETURN(device_create_file(dev, &dev_attr_name));
385         CHECK_RETURN(device_create_file(dev, &dev_attr_bank));
386         CHECK_RETURN(device_create_file(dev, &dev_attr_dump));
387         CHECK_RETURN(device_create_file(dev, &dev_attr_active));
388 #if CREATE_RAW_FILE
389         CHECK_RETURN(device_create_file(dev, &dev_attr_raw));
390         CHECK_RETURN(device_create_file(dev, &dev_attr_raw2));
391 #endif
392         return 0;
393 }
394
395 /*
396          Init workbench device.
397 */
398 int variax_init(struct usb_interface *interface, struct usb_line6_variax *variax)
399 {
400         int err;
401
402         if((interface == NULL) || (variax == NULL)) return -ENODEV;
403
404         /* initialize USB buffers: */
405         err = line6_dumpreq_init(&variax->dumpreq, variax_request_model1, sizeof(variax_request_model1));
406
407         if(err < 0) {
408                 dev_err(&interface->dev, "Out of memory\n");
409                 variax_destruct(interface);
410                 return err;
411         }
412
413         err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_model2, sizeof(variax_request_model2), 1);
414
415         if(err < 0) {
416                 dev_err(&interface->dev, "Out of memory\n");
417                 variax_destruct(interface);
418                 return err;
419         }
420
421         err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_bank, sizeof(variax_request_bank), 2);
422
423         if(err < 0) {
424                 dev_err(&interface->dev, "Out of memory\n");
425                 variax_destruct(interface);
426                 return err;
427         }
428
429         variax->buffer_activate = kmalloc(sizeof(variax_activate), GFP_KERNEL);
430
431         if(variax->buffer_activate == NULL) {
432                 dev_err(&interface->dev, "Out of memory\n");
433                 variax_destruct(interface);
434                 return -ENOMEM;
435         }
436
437         memcpy(variax->buffer_activate, variax_activate, sizeof(variax_activate));
438         init_timer(&variax->activate_timer);
439
440         /* create sysfs entries: */
441         if((err = variax_create_files(0, 0, &interface->dev)) < 0) {
442                 variax_destruct(interface);
443                 return err;
444         }
445
446         if((err =       variax_create_files2(&interface->dev)) < 0) {
447                 variax_destruct(interface);
448                 return err;
449         }
450
451         /* initialize audio system: */
452         if((err = line6_init_audio(&variax->line6)) < 0) {
453                 variax_destruct(interface);
454                 return err;
455         }
456
457         /* initialize MIDI subsystem: */
458         if((err = line6_init_midi(&variax->line6)) < 0) {
459                 variax_destruct(interface);
460                 return err;
461         }
462
463         /* register audio system: */
464         if((err = line6_register_audio(&variax->line6)) < 0) {
465                 variax_destruct(interface);
466                 return err;
467         }
468
469         variax_activate_delayed(variax, VARIAX_ACTIVATE_DELAY);
470         line6_startup_delayed(&variax->dumpreq, VARIAX_STARTUP_DELAY, variax_startup_timeout, variax);
471         return 0;
472 }
473
474 /*
475         Workbench device disconnected.
476 */
477 void variax_disconnect(struct usb_interface *interface)
478 {
479         struct device *dev;
480
481         if(interface == NULL) return;
482         dev = &interface->dev;
483
484         if(dev != NULL) {
485                 /* remove sysfs entries: */
486                 variax_remove_files(0, 0, dev);
487                 device_remove_file(dev, &dev_attr_model);
488                 device_remove_file(dev, &dev_attr_volume);
489                 device_remove_file(dev, &dev_attr_tone);
490                 device_remove_file(dev, &dev_attr_name);
491                 device_remove_file(dev, &dev_attr_bank);
492                 device_remove_file(dev, &dev_attr_dump);
493                 device_remove_file(dev, &dev_attr_active);
494 #if CREATE_RAW_FILE
495                 device_remove_file(dev, &dev_attr_raw);
496                 device_remove_file(dev, &dev_attr_raw2);
497 #endif
498         }
499
500         variax_destruct(interface);
501 }