ALSA: line6: Remove driver version string
[linux-2.6-block.git] / sound / usb / line6 / driver.c
1 /*
2  * Line6 Linux USB driver - 0.9.1beta
3  *
4  * Copyright (C) 2004-2010 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 <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/usb.h>
17
18 #include <sound/core.h>
19 #include <sound/initval.h>
20
21 #include "capture.h"
22 #include "driver.h"
23 #include "midi.h"
24 #include "playback.h"
25 #include "revision.h"
26 #include "usbdefs.h"
27
28 #define DRIVER_AUTHOR  "Markus Grabner <grabner@icg.tugraz.at>"
29 #define DRIVER_DESC    "Line6 USB Driver"
30
31 /*
32         This is Line6's MIDI manufacturer ID.
33 */
34 const unsigned char line6_midi_id[] = {
35         0x00, 0x01, 0x0c
36 };
37 EXPORT_SYMBOL_GPL(line6_midi_id);
38
39 /*
40         Code to request version of POD, Variax interface
41         (and maybe other devices).
42 */
43 static const char line6_request_version[] = {
44         0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
45 };
46
47 /**
48          Class for asynchronous messages.
49 */
50 struct message {
51         struct usb_line6 *line6;
52         const char *buffer;
53         int size;
54         int done;
55 };
56
57 /*
58         Forward declarations.
59 */
60 static void line6_data_received(struct urb *urb);
61 static int line6_send_raw_message_async_part(struct message *msg,
62                                              struct urb *urb);
63
64 /*
65         Start to listen on endpoint.
66 */
67 static int line6_start_listen(struct usb_line6 *line6)
68 {
69         int err;
70
71         usb_fill_int_urb(line6->urb_listen, line6->usbdev,
72                 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
73                 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
74                 line6_data_received, line6, line6->interval);
75         line6->urb_listen->actual_length = 0;
76         err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
77         return err;
78 }
79
80 /*
81         Stop listening on endpoint.
82 */
83 static void line6_stop_listen(struct usb_line6 *line6)
84 {
85         usb_kill_urb(line6->urb_listen);
86 }
87
88 /*
89         Send raw message in pieces of wMaxPacketSize bytes.
90 */
91 int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
92                            int size)
93 {
94         int i, done = 0;
95
96         for (i = 0; i < size; i += line6->max_packet_size) {
97                 int partial;
98                 const char *frag_buf = buffer + i;
99                 int frag_size = min(line6->max_packet_size, size - i);
100                 int retval;
101
102                 retval = usb_interrupt_msg(line6->usbdev,
103                                         usb_sndintpipe(line6->usbdev,
104                                                 line6->properties->ep_ctrl_w),
105                                         (char *)frag_buf, frag_size,
106                                         &partial, LINE6_TIMEOUT * HZ);
107
108                 if (retval) {
109                         dev_err(line6->ifcdev,
110                                 "usb_interrupt_msg failed (%d)\n", retval);
111                         break;
112                 }
113
114                 done += frag_size;
115         }
116
117         return done;
118 }
119
120 /*
121         Notification of completion of asynchronous request transmission.
122 */
123 static void line6_async_request_sent(struct urb *urb)
124 {
125         struct message *msg = (struct message *)urb->context;
126
127         if (msg->done >= msg->size) {
128                 usb_free_urb(urb);
129                 kfree(msg);
130         } else
131                 line6_send_raw_message_async_part(msg, urb);
132 }
133
134 /*
135         Asynchronously send part of a raw message.
136 */
137 static int line6_send_raw_message_async_part(struct message *msg,
138                                              struct urb *urb)
139 {
140         int retval;
141         struct usb_line6 *line6 = msg->line6;
142         int done = msg->done;
143         int bytes = min(msg->size - done, line6->max_packet_size);
144
145         usb_fill_int_urb(urb, line6->usbdev,
146                 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
147                 (char *)msg->buffer + done, bytes,
148                 line6_async_request_sent, msg, line6->interval);
149
150         msg->done += bytes;
151         retval = usb_submit_urb(urb, GFP_ATOMIC);
152
153         if (retval < 0) {
154                 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
155                         __func__, retval);
156                 usb_free_urb(urb);
157                 kfree(msg);
158                 return retval;
159         }
160
161         return 0;
162 }
163
164 /*
165         Setup and start timer.
166 */
167 void line6_start_timer(struct timer_list *timer, unsigned int msecs,
168                        void (*function)(unsigned long), unsigned long data)
169 {
170         setup_timer(timer, function, data);
171         mod_timer(timer, jiffies + msecs * HZ / 1000);
172 }
173 EXPORT_SYMBOL_GPL(line6_start_timer);
174
175 /*
176         Asynchronously send raw message.
177 */
178 int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
179                                  int size)
180 {
181         struct message *msg;
182         struct urb *urb;
183
184         /* create message: */
185         msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
186         if (msg == NULL)
187                 return -ENOMEM;
188
189         /* create URB: */
190         urb = usb_alloc_urb(0, GFP_ATOMIC);
191
192         if (urb == NULL) {
193                 kfree(msg);
194                 return -ENOMEM;
195         }
196
197         /* set message data: */
198         msg->line6 = line6;
199         msg->buffer = buffer;
200         msg->size = size;
201         msg->done = 0;
202
203         /* start sending: */
204         return line6_send_raw_message_async_part(msg, urb);
205 }
206 EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
207
208 /*
209         Send asynchronous device version request.
210 */
211 int line6_version_request_async(struct usb_line6 *line6)
212 {
213         char *buffer;
214         int retval;
215
216         buffer = kmemdup(line6_request_version,
217                         sizeof(line6_request_version), GFP_ATOMIC);
218         if (buffer == NULL)
219                 return -ENOMEM;
220
221         retval = line6_send_raw_message_async(line6, buffer,
222                                               sizeof(line6_request_version));
223         kfree(buffer);
224         return retval;
225 }
226 EXPORT_SYMBOL_GPL(line6_version_request_async);
227
228 /*
229         Send sysex message in pieces of wMaxPacketSize bytes.
230 */
231 int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
232                              int size)
233 {
234         return line6_send_raw_message(line6, buffer,
235                                       size + SYSEX_EXTRA_SIZE) -
236             SYSEX_EXTRA_SIZE;
237 }
238 EXPORT_SYMBOL_GPL(line6_send_sysex_message);
239
240 /*
241         Allocate buffer for sysex message and prepare header.
242         @param code sysex message code
243         @param size number of bytes between code and sysex end
244 */
245 char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
246                                int size)
247 {
248         char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
249
250         if (!buffer)
251                 return NULL;
252
253         buffer[0] = LINE6_SYSEX_BEGIN;
254         memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
255         buffer[sizeof(line6_midi_id) + 1] = code1;
256         buffer[sizeof(line6_midi_id) + 2] = code2;
257         buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
258         return buffer;
259 }
260 EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
261
262 /*
263         Notification of data received from the Line6 device.
264 */
265 static void line6_data_received(struct urb *urb)
266 {
267         struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
268         struct midi_buffer *mb = &line6->line6midi->midibuf_in;
269         int done;
270
271         if (urb->status == -ESHUTDOWN)
272                 return;
273
274         done =
275             line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
276
277         if (done < urb->actual_length) {
278                 line6_midibuf_ignore(mb, done);
279                 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
280                         done, urb->actual_length);
281         }
282
283         for (;;) {
284                 done =
285                     line6_midibuf_read(mb, line6->buffer_message,
286                                        LINE6_MESSAGE_MAXLEN);
287
288                 if (done == 0)
289                         break;
290
291                 line6->message_length = done;
292                 line6_midi_receive(line6, line6->buffer_message, done);
293
294                 if (line6->process_message)
295                         line6->process_message(line6);
296         }
297
298         line6_start_listen(line6);
299 }
300
301 /*
302         Send channel number (i.e., switch to a different sound).
303 */
304 int line6_send_program(struct usb_line6 *line6, u8 value)
305 {
306         int retval;
307         unsigned char *buffer;
308         int partial;
309
310         buffer = kmalloc(2, GFP_KERNEL);
311         if (!buffer)
312                 return -ENOMEM;
313
314         buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
315         buffer[1] = value;
316
317         retval = usb_interrupt_msg(line6->usbdev,
318                                    usb_sndintpipe(line6->usbdev,
319                                                   line6->properties->ep_ctrl_w),
320                                    buffer, 2, &partial, LINE6_TIMEOUT * HZ);
321
322         if (retval)
323                 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
324                         retval);
325
326         kfree(buffer);
327         return retval;
328 }
329
330 /*
331         Transmit Line6 control parameter.
332 */
333 int line6_transmit_parameter(struct usb_line6 *line6, int param, u8 value)
334 {
335         int retval;
336         unsigned char *buffer;
337         int partial;
338
339         buffer = kmalloc(3, GFP_KERNEL);
340         if (!buffer)
341                 return -ENOMEM;
342
343         buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
344         buffer[1] = param;
345         buffer[2] = value;
346
347         retval = usb_interrupt_msg(line6->usbdev,
348                                    usb_sndintpipe(line6->usbdev,
349                                                   line6->properties->ep_ctrl_w),
350                                    buffer, 3, &partial, LINE6_TIMEOUT * HZ);
351
352         if (retval)
353                 dev_err(line6->ifcdev, "usb_interrupt_msg failed (%d)\n",
354                         retval);
355
356         kfree(buffer);
357         return retval;
358 }
359
360 /*
361         Read data from device.
362 */
363 int line6_read_data(struct usb_line6 *line6, int address, void *data,
364                     size_t datalen)
365 {
366         struct usb_device *usbdev = line6->usbdev;
367         int ret;
368         unsigned char len;
369
370         /* query the serial number: */
371         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
372                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
373                               (datalen << 8) | 0x21, address,
374                               NULL, 0, LINE6_TIMEOUT * HZ);
375
376         if (ret < 0) {
377                 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
378                 return ret;
379         }
380
381         /* Wait for data length. We'll get 0xff until length arrives. */
382         do {
383                 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
384                                       USB_TYPE_VENDOR | USB_RECIP_DEVICE |
385                                       USB_DIR_IN,
386                                       0x0012, 0x0000, &len, 1,
387                                       LINE6_TIMEOUT * HZ);
388                 if (ret < 0) {
389                         dev_err(line6->ifcdev,
390                                 "receive length failed (error %d)\n", ret);
391                         return ret;
392                 }
393         } while (len == 0xff);
394
395         if (len != datalen) {
396                 /* should be equal or something went wrong */
397                 dev_err(line6->ifcdev,
398                         "length mismatch (expected %d, got %d)\n",
399                         (int)datalen, (int)len);
400                 return -EINVAL;
401         }
402
403         /* receive the result: */
404         ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
405                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
406                               0x0013, 0x0000, data, datalen,
407                               LINE6_TIMEOUT * HZ);
408
409         if (ret < 0) {
410                 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
411                 return ret;
412         }
413
414         return 0;
415 }
416 EXPORT_SYMBOL_GPL(line6_read_data);
417
418 /*
419         Write data to device.
420 */
421 int line6_write_data(struct usb_line6 *line6, int address, void *data,
422                      size_t datalen)
423 {
424         struct usb_device *usbdev = line6->usbdev;
425         int ret;
426         unsigned char status;
427
428         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
429                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
430                               0x0022, address, data, datalen,
431                               LINE6_TIMEOUT * HZ);
432
433         if (ret < 0) {
434                 dev_err(line6->ifcdev,
435                         "write request failed (error %d)\n", ret);
436                 return ret;
437         }
438
439         do {
440                 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
441                                       0x67,
442                                       USB_TYPE_VENDOR | USB_RECIP_DEVICE |
443                                       USB_DIR_IN,
444                                       0x0012, 0x0000,
445                                       &status, 1, LINE6_TIMEOUT * HZ);
446
447                 if (ret < 0) {
448                         dev_err(line6->ifcdev,
449                                 "receiving status failed (error %d)\n", ret);
450                         return ret;
451                 }
452         } while (status == 0xff);
453
454         if (status != 0) {
455                 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
456                 return -EINVAL;
457         }
458
459         return 0;
460 }
461 EXPORT_SYMBOL_GPL(line6_write_data);
462
463 /*
464         Read Line6 device serial number.
465         (POD, TonePort, GuitarPort)
466 */
467 int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
468 {
469         return line6_read_data(line6, 0x80d0, serial_number,
470                                sizeof(*serial_number));
471 }
472 EXPORT_SYMBOL_GPL(line6_read_serial_number);
473
474 /*
475         No operation (i.e., unsupported).
476 */
477 ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
478                        char *buf)
479 {
480         return 0;
481 }
482 EXPORT_SYMBOL_GPL(line6_nop_read);
483
484 /*
485         Card destructor.
486 */
487 static void line6_destruct(struct snd_card *card)
488 {
489         struct usb_line6 *line6 = card->private_data;
490         struct usb_device *usbdev;
491
492         if (!line6)
493                 return;
494         usbdev = line6->usbdev;
495
496         /* free buffer memory first: */
497         kfree(line6->buffer_message);
498         kfree(line6->buffer_listen);
499
500         /* then free URBs: */
501         usb_free_urb(line6->urb_listen);
502
503         /* free interface data: */
504         kfree(line6);
505
506         /* decrement reference counters: */
507         usb_put_dev(usbdev);
508 }
509
510 /*
511         Probe USB device.
512 */
513 int line6_probe(struct usb_interface *interface,
514                 struct usb_line6 *line6,
515                 const struct line6_properties *properties,
516                 int (*private_init)(struct usb_interface *, struct usb_line6 *))
517 {
518         struct usb_device *usbdev;
519         struct snd_card *card;
520         int interface_number;
521         int ret;
522
523         if (!interface) {
524                 ret = -ENODEV;
525                 goto err_put;
526         }
527         usbdev = interface_to_usbdev(interface);
528         if (!usbdev) {
529                 ret = -ENODEV;
530                 goto err_put;
531         }
532
533         /* we don't handle multiple configurations */
534         if (usbdev->descriptor.bNumConfigurations != 1) {
535                 ret = -ENODEV;
536                 goto err_put;
537         }
538
539         /* initialize device info: */
540         dev_info(&interface->dev, "Line6 %s found\n", properties->name);
541
542         /* query interface number */
543         interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
544
545         ret = usb_set_interface(usbdev, interface_number,
546                         properties->altsetting);
547         if (ret < 0) {
548                 dev_err(&interface->dev, "set_interface failed\n");
549                 goto err_put;
550         }
551
552         /* store basic data: */
553         line6->properties = properties;
554         line6->usbdev = usbdev;
555         line6->ifcdev = &interface->dev;
556
557         /* get data from endpoint descriptor (see usb_maxpacket): */
558         {
559                 struct usb_host_endpoint *ep;
560                 unsigned pipe = usb_rcvintpipe(usbdev, properties->ep_ctrl_r);
561                 unsigned epnum = usb_pipeendpoint(pipe);
562                 ep = usbdev->ep_in[epnum];
563
564                 if (ep != NULL) {
565                         line6->interval = ep->desc.bInterval;
566                         line6->max_packet_size =
567                             le16_to_cpu(ep->desc.wMaxPacketSize);
568                 } else {
569                         line6->interval = LINE6_FALLBACK_INTERVAL;
570                         line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
571                         dev_err(line6->ifcdev,
572                                 "endpoint not available, using fallback values");
573                 }
574         }
575
576         ret = snd_card_new(line6->ifcdev,
577                            SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
578                            THIS_MODULE, 0, &card);
579         if (ret < 0)
580                 goto err_put;
581
582         line6->card = card;
583         strcpy(card->id, line6->properties->id);
584         strcpy(card->driver, DRIVER_NAME);
585         strcpy(card->shortname, line6->properties->name);
586         sprintf(card->longname, "Line6 %s at USB %s", line6->properties->name,
587                 dev_name(line6->ifcdev));
588         card->private_data = line6;
589         card->private_free = line6_destruct;
590
591         usb_set_intfdata(interface, line6);
592
593         /* increment reference counters: */
594         usb_get_dev(usbdev);
595
596         if (properties->capabilities & LINE6_CAP_CONTROL) {
597                 /* initialize USB buffers: */
598                 line6->buffer_listen =
599                     kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
600                 if (line6->buffer_listen == NULL) {
601                         ret = -ENOMEM;
602                         goto err_destruct;
603                 }
604
605                 line6->buffer_message =
606                     kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
607                 if (line6->buffer_message == NULL) {
608                         ret = -ENOMEM;
609                         goto err_destruct;
610                 }
611
612                 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
613
614                 if (line6->urb_listen == NULL) {
615                         ret = -ENOMEM;
616                         goto err_destruct;
617                 }
618
619                 ret = line6_start_listen(line6);
620                 if (ret < 0) {
621                         dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
622                                 __func__);
623                         goto err_destruct;
624                 }
625         }
626
627         /* initialize device data based on device: */
628         ret = private_init(interface, line6);
629         if (ret < 0)
630                 goto err_destruct;
631
632         /* creation of additional special files should go here */
633
634         dev_info(&interface->dev, "Line6 %s now attached\n",
635                  line6->properties->name);
636
637         return 0;
638
639  err_destruct:
640         snd_card_free(card);
641  err_put:
642         return ret;
643 }
644 EXPORT_SYMBOL_GPL(line6_probe);
645
646 /*
647         Line6 device disconnected.
648 */
649 void line6_disconnect(struct usb_interface *interface)
650 {
651         struct usb_line6 *line6;
652         struct usb_device *usbdev;
653         int interface_number;
654
655         if (interface == NULL)
656                 return;
657         usbdev = interface_to_usbdev(interface);
658         if (usbdev == NULL)
659                 return;
660
661         interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
662         line6 = usb_get_intfdata(interface);
663         if (!line6)
664                 return;
665
666         if (line6->urb_listen != NULL)
667                 line6_stop_listen(line6);
668
669         if (usbdev != line6->usbdev)
670                 dev_err(line6->ifcdev, "driver bug: inconsistent usb device\n");
671
672         snd_card_disconnect(line6->card);
673         if (line6->disconnect)
674                 line6->disconnect(interface);
675
676         dev_info(&interface->dev, "Line6 %s now disconnected\n",
677                  line6->properties->name);
678
679         /* make sure the device isn't destructed twice: */
680         usb_set_intfdata(interface, NULL);
681
682         snd_card_free_when_closed(line6->card);
683 }
684 EXPORT_SYMBOL_GPL(line6_disconnect);
685
686 #ifdef CONFIG_PM
687
688 /*
689         Suspend Line6 device.
690 */
691 int line6_suspend(struct usb_interface *interface, pm_message_t message)
692 {
693         struct usb_line6 *line6 = usb_get_intfdata(interface);
694         struct snd_line6_pcm *line6pcm = line6->line6pcm;
695
696         snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
697
698         if (line6->properties->capabilities & LINE6_CAP_CONTROL)
699                 line6_stop_listen(line6);
700
701         if (line6pcm != NULL) {
702                 snd_pcm_suspend_all(line6pcm->pcm);
703                 line6_pcm_disconnect(line6pcm);
704                 line6pcm->flags = 0;
705         }
706
707         return 0;
708 }
709 EXPORT_SYMBOL_GPL(line6_suspend);
710
711 /*
712         Resume Line6 device.
713 */
714 int line6_resume(struct usb_interface *interface)
715 {
716         struct usb_line6 *line6 = usb_get_intfdata(interface);
717
718         if (line6->properties->capabilities & LINE6_CAP_CONTROL)
719                 line6_start_listen(line6);
720
721         snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
722         return 0;
723 }
724 EXPORT_SYMBOL_GPL(line6_resume);
725
726 #endif /* CONFIG_PM */
727
728 MODULE_AUTHOR(DRIVER_AUTHOR);
729 MODULE_DESCRIPTION(DRIVER_DESC);
730 MODULE_LICENSE("GPL");