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