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