]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/line6/variax.c
Staging: line6: static function cleanups
[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,
166                                  struct device_attribute *attr, char *buf)
167 {
168         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
169         return sprintf(buf, "%d\n", variax->volume);
170 }
171
172 /*
173         "write" request on "volume" special file.
174 */
175 static ssize_t variax_set_volume(struct device *dev,
176                                  struct device_attribute *attr,
177                                  const char *buf, size_t count)
178 {
179         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
180         int value = simple_strtoul(buf, NULL, 10);
181
182         if(line6_transmit_parameter(&variax->line6, VARIAXMIDI_volume, value) == 0)
183                 variax->volume = value;
184
185         return count;
186 }
187
188 /*
189         "read" request on "model" special file.
190 */
191 static ssize_t variax_get_model(struct device *dev,
192                                 struct device_attribute *attr, char *buf)
193 {
194         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
195         return sprintf(buf, "%d\n", variax->model);
196 }
197
198 /*
199         "write" request on "model" special file.
200 */
201 static ssize_t variax_set_model(struct device *dev,
202                                 struct device_attribute *attr,
203                                 const char *buf, size_t count)
204 {
205         struct usb_line6_variax *variax = usb_get_intfdata( to_usb_interface(dev));
206         int value = simple_strtoul(buf, NULL, 10);
207
208         if(line6_send_program(&variax->line6, value) == 0)
209                 variax->model = value;
210
211         return count;
212 }
213
214 /*
215         "read" request on "active" special file.
216 */
217 static ssize_t variax_get_active(struct device *dev,
218                                  struct device_attribute *attr, char *buf)
219 {
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]);
222 }
223
224 /*
225         "write" request on "active" special file.
226 */
227 static ssize_t variax_set_active(struct device *dev,
228                                  struct device_attribute *attr,
229                                  const char *buf, size_t count)
230 {
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));
235         return count;
236 }
237
238 /*
239         "read" request on "tone" special file.
240 */
241 static ssize_t variax_get_tone(struct device *dev,
242                                struct device_attribute *attr, char *buf)
243 {
244         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
245         return sprintf(buf, "%d\n", variax->tone);
246 }
247
248 /*
249         "write" request on "tone" special file.
250 */
251 static ssize_t variax_set_tone(struct device *dev,
252                                struct device_attribute *attr,
253                                const char *buf, size_t count)
254 {
255         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
256         int value = simple_strtoul(buf, NULL, 10);
257
258         if(line6_transmit_parameter(&variax->line6, VARIAXMIDI_tone, value) == 0)
259                 variax->tone = value;
260
261         return count;
262 }
263
264 static ssize_t get_string(char *buf, const char *data, int length)
265 {
266         int i;
267         memcpy(buf, data, length);
268
269         for(i = length; i--;) {
270                 char c = buf[i];
271
272                 if((c != 0) && (c != ' '))
273                         break;
274         }
275
276         buf[i + 1] = '\n';
277         return i + 2;
278 }
279
280 /*
281         "read" request on "name" special file.
282 */
283 static ssize_t variax_get_name(struct device *dev,
284                                struct device_attribute *attr, char *buf)
285 {
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));
289 }
290
291 /*
292         "read" request on "bank" special file.
293 */
294 static ssize_t variax_get_bank(struct device *dev,
295                                struct device_attribute *attr, char *buf)
296 {
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));
300 }
301
302 /*
303         "read" request on "dump" special file.
304 */
305 static ssize_t variax_get_dump(struct device *dev,
306                                struct device_attribute *attr, char *buf)
307 {
308         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
309         int retval;
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);
314 }
315
316 #if CREATE_RAW_FILE
317
318 /*
319         "write" request on "raw" special file.
320 */
321 static ssize_t variax_set_raw2(struct device *dev,
322                                struct device_attribute *attr,
323                                const char *buf, size_t count)
324 {
325         struct usb_line6_variax *variax = usb_get_intfdata(to_usb_interface(dev));
326         int size;
327         int i;
328         char *sysex;
329
330         count -= count % 3;
331         size = count * 2;
332         sysex = variax_alloc_sysex_buffer(variax, VARIAX_SYSEX_PARAM, size);
333
334         if(!sysex)
335                 return 0;
336
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;
341                 p2[1] = p1[2] >> 4;
342                 p2[2] = p1[1] & 0x0f;
343                 p2[3] = p1[1] >> 4;
344                 p2[4] = p1[0] & 0x0f;
345                 p2[5] = p1[0] >> 4;
346         }
347
348         line6_send_sysex_message(&variax->line6, sysex, size);
349         kfree(sysex);
350         return count;
351 }
352
353 #endif
354
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);
363
364 #if CREATE_RAW_FILE
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);
367 #endif
368
369
370 /*
371         Variax destructor.
372 */
373 static void variax_destruct(struct usb_interface *interface)
374 {
375         struct usb_line6_variax *variax = usb_get_intfdata(interface);
376         struct usb_line6 *line6;
377
378         if(variax == NULL) return;
379         line6 = &variax->line6;
380         if(line6 == NULL) return;
381         line6_cleanup_audio(line6);
382
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);
387
388         if(variax->buffer_activate) kfree(variax->buffer_activate);
389         del_timer_sync(&variax->activate_timer);
390 }
391
392 /*
393         Create sysfs entries.
394 */
395 static int variax_create_files2(struct device *dev)
396 {
397         int err;
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));
405 #if CREATE_RAW_FILE
406         CHECK_RETURN(device_create_file(dev, &dev_attr_raw));
407         CHECK_RETURN(device_create_file(dev, &dev_attr_raw2));
408 #endif
409         return 0;
410 }
411
412 /*
413          Init workbench device.
414 */
415 int variax_init(struct usb_interface *interface, struct usb_line6_variax *variax)
416 {
417         int err;
418
419         if((interface == NULL) || (variax == NULL)) return -ENODEV;
420
421         /* initialize USB buffers: */
422         err = line6_dumpreq_init(&variax->dumpreq, variax_request_model1, sizeof(variax_request_model1));
423
424         if(err < 0) {
425                 dev_err(&interface->dev, "Out of memory\n");
426                 variax_destruct(interface);
427                 return err;
428         }
429
430         err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_model2, sizeof(variax_request_model2), 1);
431
432         if(err < 0) {
433                 dev_err(&interface->dev, "Out of memory\n");
434                 variax_destruct(interface);
435                 return err;
436         }
437
438         err = line6_dumpreq_initbuf(&variax->dumpreq, variax_request_bank, sizeof(variax_request_bank), 2);
439
440         if(err < 0) {
441                 dev_err(&interface->dev, "Out of memory\n");
442                 variax_destruct(interface);
443                 return err;
444         }
445
446         variax->buffer_activate = kmalloc(sizeof(variax_activate), GFP_KERNEL);
447
448         if(variax->buffer_activate == NULL) {
449                 dev_err(&interface->dev, "Out of memory\n");
450                 variax_destruct(interface);
451                 return -ENOMEM;
452         }
453
454         memcpy(variax->buffer_activate, variax_activate, sizeof(variax_activate));
455         init_timer(&variax->activate_timer);
456
457         /* create sysfs entries: */
458         if((err = variax_create_files(0, 0, &interface->dev)) < 0) {
459                 variax_destruct(interface);
460                 return err;
461         }
462
463         if((err =       variax_create_files2(&interface->dev)) < 0) {
464                 variax_destruct(interface);
465                 return err;
466         }
467
468         /* initialize audio system: */
469         if((err = line6_init_audio(&variax->line6)) < 0) {
470                 variax_destruct(interface);
471                 return err;
472         }
473
474         /* initialize MIDI subsystem: */
475         if((err = line6_init_midi(&variax->line6)) < 0) {
476                 variax_destruct(interface);
477                 return err;
478         }
479
480         /* register audio system: */
481         if((err = line6_register_audio(&variax->line6)) < 0) {
482                 variax_destruct(interface);
483                 return err;
484         }
485
486         variax_activate_delayed(variax, VARIAX_ACTIVATE_DELAY);
487         line6_startup_delayed(&variax->dumpreq, VARIAX_STARTUP_DELAY, variax_startup_timeout, variax);
488         return 0;
489 }
490
491 /*
492         Workbench device disconnected.
493 */
494 void variax_disconnect(struct usb_interface *interface)
495 {
496         struct device *dev;
497
498         if(interface == NULL) return;
499         dev = &interface->dev;
500
501         if(dev != NULL) {
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);
511 #if CREATE_RAW_FILE
512                 device_remove_file(dev, &dev_attr_raw);
513                 device_remove_file(dev, &dev_attr_raw2);
514 #endif
515         }
516
517         variax_destruct(interface);
518 }