Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Line 6 Linux USB driver
4 *
5 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/export.h>
11#include <linux/slab.h>
12#include <linux/usb.h>
13
14#include <sound/core.h>
15#include <sound/initval.h>
16#include <sound/hwdep.h>
17
18#include "capture.h"
19#include "driver.h"
20#include "midi.h"
21#include "playback.h"
22
23#define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
24#define DRIVER_DESC "Line 6 USB Driver"
25
26/*
27 This is Line 6's MIDI manufacturer ID.
28*/
29const unsigned char line6_midi_id[3] = {
30 0x00, 0x01, 0x0c
31};
32EXPORT_SYMBOL_GPL(line6_midi_id);
33
34/*
35 Code to request version of POD, Variax interface
36 (and maybe other devices).
37*/
38static const char line6_request_version[] = {
39 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
40};
41
42/*
43 Class for asynchronous messages.
44*/
45struct message {
46 struct usb_line6 *line6;
47 const char *buffer;
48 int size;
49 int done;
50};
51
52/*
53 Forward declarations.
54*/
55static void line6_data_received(struct urb *urb);
56static int line6_send_raw_message_async_part(struct message *msg,
57 struct urb *urb);
58
59/*
60 Start to listen on endpoint.
61*/
62static int line6_start_listen(struct usb_line6 *line6)
63{
64 int err;
65
66 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
67 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
68 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
69 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
70 line6_data_received, line6, line6->interval);
71 } else {
72 usb_fill_bulk_urb(line6->urb_listen, line6->usbdev,
73 usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r),
74 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
75 line6_data_received, line6);
76 }
77
78 /* sanity checks of EP before actually submitting */
79 if (usb_urb_ep_type_check(line6->urb_listen)) {
80 dev_err(line6->ifcdev, "invalid control EP\n");
81 return -EINVAL;
82 }
83
84 line6->urb_listen->actual_length = 0;
85 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
86 return err;
87}
88
89/*
90 Stop listening on endpoint.
91*/
92static void line6_stop_listen(struct usb_line6 *line6)
93{
94 usb_kill_urb(line6->urb_listen);
95}
96
97/*
98 Send raw message in pieces of wMaxPacketSize bytes.
99*/
100int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
101 int size)
102{
103 int i, done = 0;
104 const struct line6_properties *properties = line6->properties;
105
106 for (i = 0; i < size; i += line6->max_packet_size) {
107 int partial;
108 const char *frag_buf = buffer + i;
109 int frag_size = min(line6->max_packet_size, size - i);
110 int retval;
111
112 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
113 retval = usb_interrupt_msg(line6->usbdev,
114 usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
115 (char *)frag_buf, frag_size,
116 &partial, LINE6_TIMEOUT * HZ);
117 } else {
118 retval = usb_bulk_msg(line6->usbdev,
119 usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
120 (char *)frag_buf, frag_size,
121 &partial, LINE6_TIMEOUT * HZ);
122 }
123
124 if (retval) {
125 dev_err(line6->ifcdev,
126 "usb_bulk_msg failed (%d)\n", retval);
127 break;
128 }
129
130 done += frag_size;
131 }
132
133 return done;
134}
135EXPORT_SYMBOL_GPL(line6_send_raw_message);
136
137/*
138 Notification of completion of asynchronous request transmission.
139*/
140static void line6_async_request_sent(struct urb *urb)
141{
142 struct message *msg = (struct message *)urb->context;
143
144 if (msg->done >= msg->size) {
145 usb_free_urb(urb);
146 kfree(msg);
147 } else
148 line6_send_raw_message_async_part(msg, urb);
149}
150
151/*
152 Asynchronously send part of a raw message.
153*/
154static int line6_send_raw_message_async_part(struct message *msg,
155 struct urb *urb)
156{
157 int retval;
158 struct usb_line6 *line6 = msg->line6;
159 int done = msg->done;
160 int bytes = min(msg->size - done, line6->max_packet_size);
161
162 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
163 usb_fill_int_urb(urb, line6->usbdev,
164 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
165 (char *)msg->buffer + done, bytes,
166 line6_async_request_sent, msg, line6->interval);
167 } else {
168 usb_fill_bulk_urb(urb, line6->usbdev,
169 usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
170 (char *)msg->buffer + done, bytes,
171 line6_async_request_sent, msg);
172 }
173
174 msg->done += bytes;
175
176 /* sanity checks of EP before actually submitting */
177 retval = usb_urb_ep_type_check(urb);
178 if (retval < 0)
179 goto error;
180
181 retval = usb_submit_urb(urb, GFP_ATOMIC);
182 if (retval < 0)
183 goto error;
184
185 return 0;
186
187 error:
188 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
189 __func__, retval);
190 usb_free_urb(urb);
191 kfree(msg);
192 return retval;
193}
194
195/*
196 Asynchronously send raw message.
197*/
198int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
199 int size)
200{
201 struct message *msg;
202 struct urb *urb;
203
204 /* create message: */
205 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
206 if (msg == NULL)
207 return -ENOMEM;
208
209 /* create URB: */
210 urb = usb_alloc_urb(0, GFP_ATOMIC);
211
212 if (urb == NULL) {
213 kfree(msg);
214 return -ENOMEM;
215 }
216
217 /* set message data: */
218 msg->line6 = line6;
219 msg->buffer = buffer;
220 msg->size = size;
221 msg->done = 0;
222
223 /* start sending: */
224 return line6_send_raw_message_async_part(msg, urb);
225}
226EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
227
228/*
229 Send asynchronous device version request.
230*/
231int line6_version_request_async(struct usb_line6 *line6)
232{
233 char *buffer;
234 int retval;
235
236 buffer = kmemdup(line6_request_version,
237 sizeof(line6_request_version), GFP_ATOMIC);
238 if (buffer == NULL)
239 return -ENOMEM;
240
241 retval = line6_send_raw_message_async(line6, buffer,
242 sizeof(line6_request_version));
243 kfree(buffer);
244 return retval;
245}
246EXPORT_SYMBOL_GPL(line6_version_request_async);
247
248/*
249 Send sysex message in pieces of wMaxPacketSize bytes.
250*/
251int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
252 int size)
253{
254 return line6_send_raw_message(line6, buffer,
255 size + SYSEX_EXTRA_SIZE) -
256 SYSEX_EXTRA_SIZE;
257}
258EXPORT_SYMBOL_GPL(line6_send_sysex_message);
259
260/*
261 Allocate buffer for sysex message and prepare header.
262 @param code sysex message code
263 @param size number of bytes between code and sysex end
264*/
265char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
266 int size)
267{
268 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
269
270 if (!buffer)
271 return NULL;
272
273 buffer[0] = LINE6_SYSEX_BEGIN;
274 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
275 buffer[sizeof(line6_midi_id) + 1] = code1;
276 buffer[sizeof(line6_midi_id) + 2] = code2;
277 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
278 return buffer;
279}
280EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
281
282/*
283 Notification of data received from the Line 6 device.
284*/
285static void line6_data_received(struct urb *urb)
286{
287 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
288 struct midi_buffer *mb = &line6->line6midi->midibuf_in;
289 int done;
290
291 if (urb->status == -ESHUTDOWN)
292 return;
293
294 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
295 done =
296 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
297
298 if (done < urb->actual_length) {
299 line6_midibuf_ignore(mb, done);
300 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
301 done, urb->actual_length);
302 }
303
304 for (;;) {
305 done =
306 line6_midibuf_read(mb, line6->buffer_message,
307 LINE6_MIDI_MESSAGE_MAXLEN);
308
309 if (done <= 0)
310 break;
311
312 line6->message_length = done;
313 line6_midi_receive(line6, line6->buffer_message, done);
314
315 if (line6->process_message)
316 line6->process_message(line6);
317 }
318 } else {
319 line6->buffer_message = urb->transfer_buffer;
320 line6->message_length = urb->actual_length;
321 if (line6->process_message)
322 line6->process_message(line6);
323 line6->buffer_message = NULL;
324 }
325
326 line6_start_listen(line6);
327}
328
329#define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
330#define LINE6_READ_WRITE_MAX_RETRIES 50
331
332/*
333 Read data from device.
334*/
335int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
336 unsigned datalen)
337{
338 struct usb_device *usbdev = line6->usbdev;
339 int ret;
340 unsigned char *len;
341 unsigned count;
342
343 if (address > 0xffff || datalen > 0xff)
344 return -EINVAL;
345
346 len = kmalloc(1, GFP_KERNEL);
347 if (!len)
348 return -ENOMEM;
349
350 /* query the serial number: */
351 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
352 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
353 (datalen << 8) | 0x21, address,
354 NULL, 0, LINE6_TIMEOUT * HZ);
355
356 if (ret < 0) {
357 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
358 goto exit;
359 }
360
361 /* Wait for data length. We'll get 0xff until length arrives. */
362 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
363 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
364
365 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
366 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
367 USB_DIR_IN,
368 0x0012, 0x0000, len, 1,
369 LINE6_TIMEOUT * HZ);
370 if (ret < 0) {
371 dev_err(line6->ifcdev,
372 "receive length failed (error %d)\n", ret);
373 goto exit;
374 }
375
376 if (*len != 0xff)
377 break;
378 }
379
380 ret = -EIO;
381 if (*len == 0xff) {
382 dev_err(line6->ifcdev, "read failed after %d retries\n",
383 count);
384 goto exit;
385 } else if (*len != datalen) {
386 /* should be equal or something went wrong */
387 dev_err(line6->ifcdev,
388 "length mismatch (expected %d, got %d)\n",
389 (int)datalen, (int)*len);
390 goto exit;
391 }
392
393 /* receive the result: */
394 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
395 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
396 0x0013, 0x0000, data, datalen,
397 LINE6_TIMEOUT * HZ);
398
399 if (ret < 0)
400 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
401
402exit:
403 kfree(len);
404 return ret;
405}
406EXPORT_SYMBOL_GPL(line6_read_data);
407
408/*
409 Write data to device.
410*/
411int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
412 unsigned datalen)
413{
414 struct usb_device *usbdev = line6->usbdev;
415 int ret;
416 unsigned char *status;
417 int count;
418
419 if (address > 0xffff || datalen > 0xffff)
420 return -EINVAL;
421
422 status = kmalloc(1, GFP_KERNEL);
423 if (!status)
424 return -ENOMEM;
425
426 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
427 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
428 0x0022, address, data, datalen,
429 LINE6_TIMEOUT * HZ);
430
431 if (ret < 0) {
432 dev_err(line6->ifcdev,
433 "write request failed (error %d)\n", ret);
434 goto exit;
435 }
436
437 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
438 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
439
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 goto exit;
451 }
452
453 if (*status != 0xff)
454 break;
455 }
456
457 if (*status == 0xff) {
458 dev_err(line6->ifcdev, "write failed after %d retries\n",
459 count);
460 ret = -EIO;
461 } else if (*status != 0) {
462 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
463 ret = -EIO;
464 }
465exit:
466 kfree(status);
467 return ret;
468}
469EXPORT_SYMBOL_GPL(line6_write_data);
470
471/*
472 Read Line 6 device serial number.
473 (POD, TonePort, GuitarPort)
474*/
475int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
476{
477 return line6_read_data(line6, 0x80d0, serial_number,
478 sizeof(*serial_number));
479}
480EXPORT_SYMBOL_GPL(line6_read_serial_number);
481
482/*
483 Card destructor.
484*/
485static void line6_destruct(struct snd_card *card)
486{
487 struct usb_line6 *line6 = card->private_data;
488 struct usb_device *usbdev = line6->usbdev;
489
490 /* Free buffer memory first. We cannot depend on the existence of private
491 * data from the (podhd) module, it may be gone already during this call
492 */
493 kfree(line6->buffer_message);
494
495 kfree(line6->buffer_listen);
496
497 /* then free URBs: */
498 usb_free_urb(line6->urb_listen);
499 line6->urb_listen = NULL;
500
501 /* decrement reference counters: */
502 usb_put_dev(usbdev);
503}
504
505static void line6_get_usb_properties(struct usb_line6 *line6)
506{
507 struct usb_device *usbdev = line6->usbdev;
508 const struct line6_properties *properties = line6->properties;
509 int pipe;
510 struct usb_host_endpoint *ep = NULL;
511
512 if (properties->capabilities & LINE6_CAP_CONTROL) {
513 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
514 pipe = usb_rcvintpipe(line6->usbdev,
515 line6->properties->ep_ctrl_r);
516 } else {
517 pipe = usb_rcvbulkpipe(line6->usbdev,
518 line6->properties->ep_ctrl_r);
519 }
520 ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
521 }
522
523 /* Control data transfer properties */
524 if (ep) {
525 line6->interval = ep->desc.bInterval;
526 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
527 } else {
528 if (properties->capabilities & LINE6_CAP_CONTROL) {
529 dev_err(line6->ifcdev,
530 "endpoint not available, using fallback values");
531 }
532 line6->interval = LINE6_FALLBACK_INTERVAL;
533 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
534 }
535
536 /* Isochronous transfer properties */
537 if (usbdev->speed == USB_SPEED_LOW) {
538 line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
539 line6->iso_buffers = USB_LOW_ISO_BUFFERS;
540 } else {
541 line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
542 line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
543 }
544}
545
546/* Enable buffering of incoming messages, flush the buffer */
547static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
548{
549 struct usb_line6 *line6 = hw->private_data;
550
551 /* NOTE: hwdep layer provides atomicity here */
552
553 line6->messages.active = 1;
554 line6->messages.nonblock = file->f_flags & O_NONBLOCK ? 1 : 0;
555
556 return 0;
557}
558
559/* Stop buffering */
560static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
561{
562 struct usb_line6 *line6 = hw->private_data;
563
564 line6->messages.active = 0;
565
566 return 0;
567}
568
569/* Read from circular buffer, return to user */
570static long
571line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
572 loff_t *offset)
573{
574 struct usb_line6 *line6 = hwdep->private_data;
575 long rv = 0;
576 unsigned int out_count;
577
578 if (mutex_lock_interruptible(&line6->messages.read_lock))
579 return -ERESTARTSYS;
580
581 while (kfifo_len(&line6->messages.fifo) == 0) {
582 mutex_unlock(&line6->messages.read_lock);
583
584 if (line6->messages.nonblock)
585 return -EAGAIN;
586
587 rv = wait_event_interruptible(
588 line6->messages.wait_queue,
589 kfifo_len(&line6->messages.fifo) != 0);
590 if (rv < 0)
591 return rv;
592
593 if (mutex_lock_interruptible(&line6->messages.read_lock))
594 return -ERESTARTSYS;
595 }
596
597 if (kfifo_peek_len(&line6->messages.fifo) > count) {
598 /* Buffer too small; allow re-read of the current item... */
599 rv = -EINVAL;
600 } else {
601 rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
602 if (rv == 0)
603 rv = out_count;
604 }
605
606 mutex_unlock(&line6->messages.read_lock);
607 return rv;
608}
609
610/* Write directly (no buffering) to device by user*/
611static long
612line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
613 loff_t *offset)
614{
615 struct usb_line6 *line6 = hwdep->private_data;
616 int rv;
617 char *data_copy;
618
619 if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
620 /* This is an arbitrary limit - still better than nothing... */
621 return -EINVAL;
622 }
623
624 data_copy = memdup_user(data, count);
625 if (IS_ERR(data_copy))
626 return PTR_ERR(data_copy);
627
628 rv = line6_send_raw_message(line6, data_copy, count);
629
630 kfree(data_copy);
631 return rv;
632}
633
634static __poll_t
635line6_hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
636{
637 __poll_t rv;
638 struct usb_line6 *line6 = hwdep->private_data;
639
640 poll_wait(file, &line6->messages.wait_queue, wait);
641
642 mutex_lock(&line6->messages.read_lock);
643 rv = kfifo_len(&line6->messages.fifo) == 0 ? 0 : EPOLLIN | EPOLLRDNORM;
644 mutex_unlock(&line6->messages.read_lock);
645
646 return rv;
647}
648
649static const struct snd_hwdep_ops hwdep_ops = {
650 .open = line6_hwdep_open,
651 .release = line6_hwdep_release,
652 .read = line6_hwdep_read,
653 .write = line6_hwdep_write,
654 .poll = line6_hwdep_poll,
655};
656
657/* Insert into circular buffer */
658static void line6_hwdep_push_message(struct usb_line6 *line6)
659{
660 if (!line6->messages.active)
661 return;
662
663 if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
664 /* No race condition here, there's only one writer */
665 kfifo_in(&line6->messages.fifo,
666 line6->buffer_message, line6->message_length);
667 } /* else TODO: signal overflow */
668
669 wake_up_interruptible(&line6->messages.wait_queue);
670}
671
672static int line6_hwdep_init(struct usb_line6 *line6)
673{
674 int err;
675 struct snd_hwdep *hwdep;
676
677 /* TODO: usb_driver_claim_interface(); */
678 line6->process_message = line6_hwdep_push_message;
679 line6->messages.active = 0;
680 init_waitqueue_head(&line6->messages.wait_queue);
681 mutex_init(&line6->messages.read_lock);
682 INIT_KFIFO(line6->messages.fifo);
683
684 err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
685 if (err < 0)
686 goto end;
687 strcpy(hwdep->name, "config");
688 hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
689 hwdep->ops = hwdep_ops;
690 hwdep->private_data = line6;
691 hwdep->exclusive = true;
692
693end:
694 return err;
695}
696
697static int line6_init_cap_control(struct usb_line6 *line6)
698{
699 int ret;
700
701 /* initialize USB buffers: */
702 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
703 if (!line6->buffer_listen)
704 return -ENOMEM;
705
706 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
707 if (!line6->urb_listen)
708 return -ENOMEM;
709
710 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
711 line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
712 if (!line6->buffer_message)
713 return -ENOMEM;
714 } else {
715 ret = line6_hwdep_init(line6);
716 if (ret < 0)
717 return ret;
718 }
719
720 ret = line6_start_listen(line6);
721 if (ret < 0) {
722 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
723 return ret;
724 }
725
726 return 0;
727}
728
729static void line6_startup_work(struct work_struct *work)
730{
731 struct usb_line6 *line6 =
732 container_of(work, struct usb_line6, startup_work.work);
733
734 if (line6->startup)
735 line6->startup(line6);
736}
737
738/*
739 Probe USB device.
740*/
741int line6_probe(struct usb_interface *interface,
742 const struct usb_device_id *id,
743 const char *driver_name,
744 const struct line6_properties *properties,
745 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
746 size_t data_size)
747{
748 struct usb_device *usbdev = interface_to_usbdev(interface);
749 struct snd_card *card;
750 struct usb_line6 *line6;
751 int interface_number;
752 int ret;
753
754 if (WARN_ON(data_size < sizeof(*line6)))
755 return -EINVAL;
756
757 /* we don't handle multiple configurations */
758 if (usbdev->descriptor.bNumConfigurations != 1)
759 return -ENODEV;
760
761 ret = snd_card_new(&interface->dev,
762 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
763 THIS_MODULE, data_size, &card);
764 if (ret < 0)
765 return ret;
766
767 /* store basic data: */
768 line6 = card->private_data;
769 line6->card = card;
770 line6->properties = properties;
771 line6->usbdev = usbdev;
772 line6->ifcdev = &interface->dev;
773 INIT_DELAYED_WORK(&line6->startup_work, line6_startup_work);
774
775 strcpy(card->id, properties->id);
776 strcpy(card->driver, driver_name);
777 strcpy(card->shortname, properties->name);
778 sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
779 dev_name(line6->ifcdev));
780 card->private_free = line6_destruct;
781
782 usb_set_intfdata(interface, line6);
783
784 /* increment reference counters: */
785 usb_get_dev(usbdev);
786
787 /* initialize device info: */
788 dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
789
790 /* query interface number */
791 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
792
793 /* TODO reserves the bus bandwidth even without actual transfer */
794 ret = usb_set_interface(usbdev, interface_number,
795 properties->altsetting);
796 if (ret < 0) {
797 dev_err(&interface->dev, "set_interface failed\n");
798 goto error;
799 }
800
801 line6_get_usb_properties(line6);
802
803 if (properties->capabilities & LINE6_CAP_CONTROL) {
804 ret = line6_init_cap_control(line6);
805 if (ret < 0)
806 goto error;
807 }
808
809 /* initialize device data based on device: */
810 ret = private_init(line6, id);
811 if (ret < 0)
812 goto error;
813
814 /* creation of additional special files should go here */
815
816 dev_info(&interface->dev, "Line 6 %s now attached\n",
817 properties->name);
818
819 return 0;
820
821 error:
822 /* we can call disconnect callback here because no close-sync is
823 * needed yet at this point
824 */
825 line6_disconnect(interface);
826 return ret;
827}
828EXPORT_SYMBOL_GPL(line6_probe);
829
830/*
831 Line 6 device disconnected.
832*/
833void line6_disconnect(struct usb_interface *interface)
834{
835 struct usb_line6 *line6 = usb_get_intfdata(interface);
836 struct usb_device *usbdev = interface_to_usbdev(interface);
837
838 if (!line6)
839 return;
840
841 if (WARN_ON(usbdev != line6->usbdev))
842 return;
843
844 cancel_delayed_work_sync(&line6->startup_work);
845
846 if (line6->urb_listen != NULL)
847 line6_stop_listen(line6);
848
849 snd_card_disconnect(line6->card);
850 if (line6->line6pcm)
851 line6_pcm_disconnect(line6->line6pcm);
852 if (line6->disconnect)
853 line6->disconnect(line6);
854
855 dev_info(&interface->dev, "Line 6 %s now disconnected\n",
856 line6->properties->name);
857
858 /* make sure the device isn't destructed twice: */
859 usb_set_intfdata(interface, NULL);
860
861 snd_card_free_when_closed(line6->card);
862}
863EXPORT_SYMBOL_GPL(line6_disconnect);
864
865#ifdef CONFIG_PM
866
867/*
868 Suspend Line 6 device.
869*/
870int line6_suspend(struct usb_interface *interface, pm_message_t message)
871{
872 struct usb_line6 *line6 = usb_get_intfdata(interface);
873 struct snd_line6_pcm *line6pcm = line6->line6pcm;
874
875 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
876
877 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
878 line6_stop_listen(line6);
879
880 if (line6pcm != NULL)
881 line6pcm->flags = 0;
882
883 return 0;
884}
885EXPORT_SYMBOL_GPL(line6_suspend);
886
887/*
888 Resume Line 6 device.
889*/
890int line6_resume(struct usb_interface *interface)
891{
892 struct usb_line6 *line6 = usb_get_intfdata(interface);
893
894 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
895 line6_start_listen(line6);
896
897 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
898 return 0;
899}
900EXPORT_SYMBOL_GPL(line6_resume);
901
902#endif /* CONFIG_PM */
903
904MODULE_AUTHOR(DRIVER_AUTHOR);
905MODULE_DESCRIPTION(DRIVER_DESC);
906MODULE_LICENSE("GPL");
907
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Line 6 Linux USB driver
4 *
5 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/export.h>
11#include <linux/slab.h>
12#include <linux/usb.h>
13
14#include <sound/core.h>
15#include <sound/initval.h>
16#include <sound/hwdep.h>
17
18#include "capture.h"
19#include "driver.h"
20#include "midi.h"
21#include "playback.h"
22
23#define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
24#define DRIVER_DESC "Line 6 USB Driver"
25
26/*
27 This is Line 6's MIDI manufacturer ID.
28*/
29const unsigned char line6_midi_id[3] = {
30 0x00, 0x01, 0x0c
31};
32EXPORT_SYMBOL_GPL(line6_midi_id);
33
34/*
35 Code to request version of POD, Variax interface
36 (and maybe other devices).
37*/
38static const char line6_request_version[] = {
39 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
40};
41
42/*
43 Class for asynchronous messages.
44*/
45struct message {
46 struct usb_line6 *line6;
47 const char *buffer;
48 int size;
49 int done;
50};
51
52/*
53 Forward declarations.
54*/
55static void line6_data_received(struct urb *urb);
56static int line6_send_raw_message_async_part(struct message *msg,
57 struct urb *urb);
58
59/*
60 Start to listen on endpoint.
61*/
62static int line6_start_listen(struct usb_line6 *line6)
63{
64 int err;
65
66 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
67 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
68 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
69 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
70 line6_data_received, line6, line6->interval);
71 } else {
72 usb_fill_bulk_urb(line6->urb_listen, line6->usbdev,
73 usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r),
74 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
75 line6_data_received, line6);
76 }
77
78 /* sanity checks of EP before actually submitting */
79 if (usb_urb_ep_type_check(line6->urb_listen)) {
80 dev_err(line6->ifcdev, "invalid control EP\n");
81 return -EINVAL;
82 }
83
84 line6->urb_listen->actual_length = 0;
85 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
86 return err;
87}
88
89/*
90 Stop listening on endpoint.
91*/
92static void line6_stop_listen(struct usb_line6 *line6)
93{
94 usb_kill_urb(line6->urb_listen);
95}
96
97/*
98 Send raw message in pieces of wMaxPacketSize bytes.
99*/
100int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
101 int size)
102{
103 int i, done = 0;
104 const struct line6_properties *properties = line6->properties;
105
106 for (i = 0; i < size; i += line6->max_packet_size) {
107 int partial;
108 const char *frag_buf = buffer + i;
109 int frag_size = min(line6->max_packet_size, size - i);
110 int retval;
111
112 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
113 retval = usb_interrupt_msg(line6->usbdev,
114 usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
115 (char *)frag_buf, frag_size,
116 &partial, LINE6_TIMEOUT);
117 } else {
118 retval = usb_bulk_msg(line6->usbdev,
119 usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
120 (char *)frag_buf, frag_size,
121 &partial, LINE6_TIMEOUT);
122 }
123
124 if (retval) {
125 dev_err(line6->ifcdev,
126 "usb_bulk_msg failed (%d)\n", retval);
127 break;
128 }
129
130 done += frag_size;
131 }
132
133 return done;
134}
135EXPORT_SYMBOL_GPL(line6_send_raw_message);
136
137/*
138 Notification of completion of asynchronous request transmission.
139*/
140static void line6_async_request_sent(struct urb *urb)
141{
142 struct message *msg = (struct message *)urb->context;
143
144 if (msg->done >= msg->size) {
145 usb_free_urb(urb);
146 kfree(msg);
147 } else
148 line6_send_raw_message_async_part(msg, urb);
149}
150
151/*
152 Asynchronously send part of a raw message.
153*/
154static int line6_send_raw_message_async_part(struct message *msg,
155 struct urb *urb)
156{
157 int retval;
158 struct usb_line6 *line6 = msg->line6;
159 int done = msg->done;
160 int bytes = min(msg->size - done, line6->max_packet_size);
161
162 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
163 usb_fill_int_urb(urb, line6->usbdev,
164 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
165 (char *)msg->buffer + done, bytes,
166 line6_async_request_sent, msg, line6->interval);
167 } else {
168 usb_fill_bulk_urb(urb, line6->usbdev,
169 usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
170 (char *)msg->buffer + done, bytes,
171 line6_async_request_sent, msg);
172 }
173
174 msg->done += bytes;
175
176 /* sanity checks of EP before actually submitting */
177 retval = usb_urb_ep_type_check(urb);
178 if (retval < 0)
179 goto error;
180
181 retval = usb_submit_urb(urb, GFP_ATOMIC);
182 if (retval < 0)
183 goto error;
184
185 return 0;
186
187 error:
188 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
189 __func__, retval);
190 usb_free_urb(urb);
191 kfree(msg);
192 return retval;
193}
194
195/*
196 Asynchronously send raw message.
197*/
198int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
199 int size)
200{
201 struct message *msg;
202 struct urb *urb;
203
204 /* create message: */
205 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
206 if (msg == NULL)
207 return -ENOMEM;
208
209 /* create URB: */
210 urb = usb_alloc_urb(0, GFP_ATOMIC);
211
212 if (urb == NULL) {
213 kfree(msg);
214 return -ENOMEM;
215 }
216
217 /* set message data: */
218 msg->line6 = line6;
219 msg->buffer = buffer;
220 msg->size = size;
221 msg->done = 0;
222
223 /* start sending: */
224 return line6_send_raw_message_async_part(msg, urb);
225}
226EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
227
228/*
229 Send asynchronous device version request.
230*/
231int line6_version_request_async(struct usb_line6 *line6)
232{
233 char *buffer;
234 int retval;
235
236 buffer = kmemdup(line6_request_version,
237 sizeof(line6_request_version), GFP_ATOMIC);
238 if (buffer == NULL)
239 return -ENOMEM;
240
241 retval = line6_send_raw_message_async(line6, buffer,
242 sizeof(line6_request_version));
243 kfree(buffer);
244 return retval;
245}
246EXPORT_SYMBOL_GPL(line6_version_request_async);
247
248/*
249 Send sysex message in pieces of wMaxPacketSize bytes.
250*/
251int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
252 int size)
253{
254 return line6_send_raw_message(line6, buffer,
255 size + SYSEX_EXTRA_SIZE) -
256 SYSEX_EXTRA_SIZE;
257}
258EXPORT_SYMBOL_GPL(line6_send_sysex_message);
259
260/*
261 Allocate buffer for sysex message and prepare header.
262 @param code sysex message code
263 @param size number of bytes between code and sysex end
264*/
265char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
266 int size)
267{
268 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
269
270 if (!buffer)
271 return NULL;
272
273 buffer[0] = LINE6_SYSEX_BEGIN;
274 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
275 buffer[sizeof(line6_midi_id) + 1] = code1;
276 buffer[sizeof(line6_midi_id) + 2] = code2;
277 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
278 return buffer;
279}
280EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
281
282/*
283 Notification of data received from the Line 6 device.
284*/
285static void line6_data_received(struct urb *urb)
286{
287 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
288 struct midi_buffer *mb = &line6->line6midi->midibuf_in;
289 int done;
290
291 if (urb->status == -ESHUTDOWN)
292 return;
293
294 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
295 done =
296 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
297
298 if (done < urb->actual_length) {
299 line6_midibuf_ignore(mb, done);
300 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
301 done, urb->actual_length);
302 }
303
304 for (;;) {
305 done =
306 line6_midibuf_read(mb, line6->buffer_message,
307 LINE6_MIDI_MESSAGE_MAXLEN,
308 LINE6_MIDIBUF_READ_RX);
309
310 if (done <= 0)
311 break;
312
313 line6->message_length = done;
314 line6_midi_receive(line6, line6->buffer_message, done);
315
316 if (line6->process_message)
317 line6->process_message(line6);
318 }
319 } else {
320 line6->buffer_message = urb->transfer_buffer;
321 line6->message_length = urb->actual_length;
322 if (line6->process_message)
323 line6->process_message(line6);
324 line6->buffer_message = NULL;
325 }
326
327 line6_start_listen(line6);
328}
329
330#define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
331#define LINE6_READ_WRITE_MAX_RETRIES 50
332
333/*
334 Read data from device.
335*/
336int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
337 unsigned datalen)
338{
339 struct usb_device *usbdev = line6->usbdev;
340 int ret;
341 u8 len;
342 unsigned count;
343
344 if (address > 0xffff || datalen > 0xff)
345 return -EINVAL;
346
347 /* query the serial number: */
348 ret = usb_control_msg_send(usbdev, 0, 0x67,
349 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
350 (datalen << 8) | 0x21, address, NULL, 0,
351 LINE6_TIMEOUT, GFP_KERNEL);
352 if (ret) {
353 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
354 goto exit;
355 }
356
357 /* Wait for data length. We'll get 0xff until length arrives. */
358 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
359 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
360
361 ret = usb_control_msg_recv(usbdev, 0, 0x67,
362 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
363 0x0012, 0x0000, &len, 1,
364 LINE6_TIMEOUT, GFP_KERNEL);
365 if (ret) {
366 dev_err(line6->ifcdev,
367 "receive length failed (error %d)\n", ret);
368 goto exit;
369 }
370
371 if (len != 0xff)
372 break;
373 }
374
375 ret = -EIO;
376 if (len == 0xff) {
377 dev_err(line6->ifcdev, "read failed after %d retries\n",
378 count);
379 goto exit;
380 } else if (len != datalen) {
381 /* should be equal or something went wrong */
382 dev_err(line6->ifcdev,
383 "length mismatch (expected %d, got %d)\n",
384 (int)datalen, len);
385 goto exit;
386 }
387
388 /* receive the result: */
389 ret = usb_control_msg_recv(usbdev, 0, 0x67,
390 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
391 0x0013, 0x0000, data, datalen, LINE6_TIMEOUT,
392 GFP_KERNEL);
393 if (ret)
394 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
395
396exit:
397 return ret;
398}
399EXPORT_SYMBOL_GPL(line6_read_data);
400
401/*
402 Write data to device.
403*/
404int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
405 unsigned datalen)
406{
407 struct usb_device *usbdev = line6->usbdev;
408 int ret;
409 unsigned char *status;
410 int count;
411
412 if (address > 0xffff || datalen > 0xffff)
413 return -EINVAL;
414
415 status = kmalloc(1, GFP_KERNEL);
416 if (!status)
417 return -ENOMEM;
418
419 ret = usb_control_msg_send(usbdev, 0, 0x67,
420 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
421 0x0022, address, data, datalen, LINE6_TIMEOUT,
422 GFP_KERNEL);
423 if (ret) {
424 dev_err(line6->ifcdev,
425 "write request failed (error %d)\n", ret);
426 goto exit;
427 }
428
429 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
430 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
431
432 ret = usb_control_msg_recv(usbdev, 0, 0x67,
433 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
434 0x0012, 0x0000, status, 1, LINE6_TIMEOUT,
435 GFP_KERNEL);
436 if (ret) {
437 dev_err(line6->ifcdev,
438 "receiving status failed (error %d)\n", ret);
439 goto exit;
440 }
441
442 if (*status != 0xff)
443 break;
444 }
445
446 if (*status == 0xff) {
447 dev_err(line6->ifcdev, "write failed after %d retries\n",
448 count);
449 ret = -EIO;
450 } else if (*status != 0) {
451 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
452 ret = -EIO;
453 }
454exit:
455 kfree(status);
456 return ret;
457}
458EXPORT_SYMBOL_GPL(line6_write_data);
459
460/*
461 Read Line 6 device serial number.
462 (POD, TonePort, GuitarPort)
463*/
464int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
465{
466 return line6_read_data(line6, 0x80d0, serial_number,
467 sizeof(*serial_number));
468}
469EXPORT_SYMBOL_GPL(line6_read_serial_number);
470
471/*
472 Card destructor.
473*/
474static void line6_destruct(struct snd_card *card)
475{
476 struct usb_line6 *line6 = card->private_data;
477 struct usb_device *usbdev = line6->usbdev;
478
479 /* Free buffer memory first. We cannot depend on the existence of private
480 * data from the (podhd) module, it may be gone already during this call
481 */
482 kfree(line6->buffer_message);
483
484 kfree(line6->buffer_listen);
485
486 /* then free URBs: */
487 usb_free_urb(line6->urb_listen);
488 line6->urb_listen = NULL;
489
490 /* decrement reference counters: */
491 usb_put_dev(usbdev);
492}
493
494static void line6_get_usb_properties(struct usb_line6 *line6)
495{
496 struct usb_device *usbdev = line6->usbdev;
497 const struct line6_properties *properties = line6->properties;
498 int pipe;
499 struct usb_host_endpoint *ep = NULL;
500
501 if (properties->capabilities & LINE6_CAP_CONTROL) {
502 if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
503 pipe = usb_rcvintpipe(line6->usbdev,
504 line6->properties->ep_ctrl_r);
505 } else {
506 pipe = usb_rcvbulkpipe(line6->usbdev,
507 line6->properties->ep_ctrl_r);
508 }
509 ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
510 }
511
512 /* Control data transfer properties */
513 if (ep) {
514 line6->interval = ep->desc.bInterval;
515 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
516 } else {
517 if (properties->capabilities & LINE6_CAP_CONTROL) {
518 dev_err(line6->ifcdev,
519 "endpoint not available, using fallback values");
520 }
521 line6->interval = LINE6_FALLBACK_INTERVAL;
522 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
523 }
524
525 /* Isochronous transfer properties */
526 if (usbdev->speed == USB_SPEED_LOW) {
527 line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
528 line6->iso_buffers = USB_LOW_ISO_BUFFERS;
529 } else {
530 line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
531 line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
532 }
533}
534
535/* Enable buffering of incoming messages, flush the buffer */
536static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
537{
538 struct usb_line6 *line6 = hw->private_data;
539
540 /* NOTE: hwdep layer provides atomicity here */
541
542 line6->messages.active = 1;
543 line6->messages.nonblock = file->f_flags & O_NONBLOCK ? 1 : 0;
544
545 return 0;
546}
547
548/* Stop buffering */
549static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
550{
551 struct usb_line6 *line6 = hw->private_data;
552
553 line6->messages.active = 0;
554
555 return 0;
556}
557
558/* Read from circular buffer, return to user */
559static long
560line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
561 loff_t *offset)
562{
563 struct usb_line6 *line6 = hwdep->private_data;
564 long rv = 0;
565 unsigned int out_count;
566
567 if (mutex_lock_interruptible(&line6->messages.read_lock))
568 return -ERESTARTSYS;
569
570 while (kfifo_len(&line6->messages.fifo) == 0) {
571 mutex_unlock(&line6->messages.read_lock);
572
573 if (line6->messages.nonblock)
574 return -EAGAIN;
575
576 rv = wait_event_interruptible(
577 line6->messages.wait_queue,
578 kfifo_len(&line6->messages.fifo) != 0);
579 if (rv < 0)
580 return rv;
581
582 if (mutex_lock_interruptible(&line6->messages.read_lock))
583 return -ERESTARTSYS;
584 }
585
586 if (kfifo_peek_len(&line6->messages.fifo) > count) {
587 /* Buffer too small; allow re-read of the current item... */
588 rv = -EINVAL;
589 } else {
590 rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
591 if (rv == 0)
592 rv = out_count;
593 }
594
595 mutex_unlock(&line6->messages.read_lock);
596 return rv;
597}
598
599/* Write directly (no buffering) to device by user*/
600static long
601line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
602 loff_t *offset)
603{
604 struct usb_line6 *line6 = hwdep->private_data;
605 int rv;
606 char *data_copy;
607
608 if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
609 /* This is an arbitrary limit - still better than nothing... */
610 return -EINVAL;
611 }
612
613 data_copy = memdup_user(data, count);
614 if (IS_ERR(data_copy))
615 return PTR_ERR(data_copy);
616
617 rv = line6_send_raw_message(line6, data_copy, count);
618
619 kfree(data_copy);
620 return rv;
621}
622
623static __poll_t
624line6_hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
625{
626 __poll_t rv;
627 struct usb_line6 *line6 = hwdep->private_data;
628
629 poll_wait(file, &line6->messages.wait_queue, wait);
630
631 mutex_lock(&line6->messages.read_lock);
632 rv = kfifo_len(&line6->messages.fifo) == 0 ? 0 : EPOLLIN | EPOLLRDNORM;
633 mutex_unlock(&line6->messages.read_lock);
634
635 return rv;
636}
637
638static const struct snd_hwdep_ops hwdep_ops = {
639 .open = line6_hwdep_open,
640 .release = line6_hwdep_release,
641 .read = line6_hwdep_read,
642 .write = line6_hwdep_write,
643 .poll = line6_hwdep_poll,
644};
645
646/* Insert into circular buffer */
647static void line6_hwdep_push_message(struct usb_line6 *line6)
648{
649 if (!line6->messages.active)
650 return;
651
652 if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
653 /* No race condition here, there's only one writer */
654 kfifo_in(&line6->messages.fifo,
655 line6->buffer_message, line6->message_length);
656 } /* else TODO: signal overflow */
657
658 wake_up_interruptible(&line6->messages.wait_queue);
659}
660
661static int line6_hwdep_init(struct usb_line6 *line6)
662{
663 int err;
664 struct snd_hwdep *hwdep;
665
666 /* TODO: usb_driver_claim_interface(); */
667 line6->process_message = line6_hwdep_push_message;
668 line6->messages.active = 0;
669 init_waitqueue_head(&line6->messages.wait_queue);
670 mutex_init(&line6->messages.read_lock);
671 INIT_KFIFO(line6->messages.fifo);
672
673 err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
674 if (err < 0)
675 goto end;
676 strcpy(hwdep->name, "config");
677 hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
678 hwdep->ops = hwdep_ops;
679 hwdep->private_data = line6;
680 hwdep->exclusive = true;
681
682end:
683 return err;
684}
685
686static int line6_init_cap_control(struct usb_line6 *line6)
687{
688 int ret;
689
690 /* initialize USB buffers: */
691 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
692 if (!line6->buffer_listen)
693 return -ENOMEM;
694
695 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
696 if (!line6->urb_listen)
697 return -ENOMEM;
698
699 if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
700 line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
701 if (!line6->buffer_message)
702 return -ENOMEM;
703
704 ret = line6_init_midi(line6);
705 if (ret < 0)
706 return ret;
707 } else {
708 ret = line6_hwdep_init(line6);
709 if (ret < 0)
710 return ret;
711 }
712
713 ret = line6_start_listen(line6);
714 if (ret < 0) {
715 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
716 return ret;
717 }
718
719 return 0;
720}
721
722static void line6_startup_work(struct work_struct *work)
723{
724 struct usb_line6 *line6 =
725 container_of(work, struct usb_line6, startup_work.work);
726
727 if (line6->startup)
728 line6->startup(line6);
729}
730
731/*
732 Probe USB device.
733*/
734int line6_probe(struct usb_interface *interface,
735 const struct usb_device_id *id,
736 const char *driver_name,
737 const struct line6_properties *properties,
738 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
739 size_t data_size)
740{
741 struct usb_device *usbdev = interface_to_usbdev(interface);
742 struct snd_card *card;
743 struct usb_line6 *line6;
744 int interface_number;
745 int ret;
746
747 if (WARN_ON(data_size < sizeof(*line6)))
748 return -EINVAL;
749
750 /* we don't handle multiple configurations */
751 if (usbdev->descriptor.bNumConfigurations != 1)
752 return -ENODEV;
753
754 ret = snd_card_new(&interface->dev,
755 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
756 THIS_MODULE, data_size, &card);
757 if (ret < 0)
758 return ret;
759
760 /* store basic data: */
761 line6 = card->private_data;
762 line6->card = card;
763 line6->properties = properties;
764 line6->usbdev = usbdev;
765 line6->ifcdev = &interface->dev;
766 INIT_DELAYED_WORK(&line6->startup_work, line6_startup_work);
767
768 strcpy(card->id, properties->id);
769 strcpy(card->driver, driver_name);
770 strcpy(card->shortname, properties->name);
771 sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
772 dev_name(line6->ifcdev));
773 card->private_free = line6_destruct;
774
775 usb_set_intfdata(interface, line6);
776
777 /* increment reference counters: */
778 usb_get_dev(usbdev);
779
780 /* initialize device info: */
781 dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
782
783 /* query interface number */
784 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
785
786 /* TODO reserves the bus bandwidth even without actual transfer */
787 ret = usb_set_interface(usbdev, interface_number,
788 properties->altsetting);
789 if (ret < 0) {
790 dev_err(&interface->dev, "set_interface failed\n");
791 goto error;
792 }
793
794 line6_get_usb_properties(line6);
795
796 if (properties->capabilities & LINE6_CAP_CONTROL) {
797 ret = line6_init_cap_control(line6);
798 if (ret < 0)
799 goto error;
800 }
801
802 /* initialize device data based on device: */
803 ret = private_init(line6, id);
804 if (ret < 0)
805 goto error;
806
807 /* creation of additional special files should go here */
808
809 dev_info(&interface->dev, "Line 6 %s now attached\n",
810 properties->name);
811
812 return 0;
813
814 error:
815 /* we can call disconnect callback here because no close-sync is
816 * needed yet at this point
817 */
818 line6_disconnect(interface);
819 return ret;
820}
821EXPORT_SYMBOL_GPL(line6_probe);
822
823/*
824 Line 6 device disconnected.
825*/
826void line6_disconnect(struct usb_interface *interface)
827{
828 struct usb_line6 *line6 = usb_get_intfdata(interface);
829 struct usb_device *usbdev = interface_to_usbdev(interface);
830
831 if (!line6)
832 return;
833
834 if (WARN_ON(usbdev != line6->usbdev))
835 return;
836
837 cancel_delayed_work_sync(&line6->startup_work);
838
839 if (line6->urb_listen != NULL)
840 line6_stop_listen(line6);
841
842 snd_card_disconnect(line6->card);
843 if (line6->line6pcm)
844 line6_pcm_disconnect(line6->line6pcm);
845 if (line6->disconnect)
846 line6->disconnect(line6);
847
848 dev_info(&interface->dev, "Line 6 %s now disconnected\n",
849 line6->properties->name);
850
851 /* make sure the device isn't destructed twice: */
852 usb_set_intfdata(interface, NULL);
853
854 snd_card_free_when_closed(line6->card);
855}
856EXPORT_SYMBOL_GPL(line6_disconnect);
857
858#ifdef CONFIG_PM
859
860/*
861 Suspend Line 6 device.
862*/
863int line6_suspend(struct usb_interface *interface, pm_message_t message)
864{
865 struct usb_line6 *line6 = usb_get_intfdata(interface);
866 struct snd_line6_pcm *line6pcm = line6->line6pcm;
867
868 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
869
870 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
871 line6_stop_listen(line6);
872
873 if (line6pcm != NULL)
874 line6pcm->flags = 0;
875
876 return 0;
877}
878EXPORT_SYMBOL_GPL(line6_suspend);
879
880/*
881 Resume Line 6 device.
882*/
883int line6_resume(struct usb_interface *interface)
884{
885 struct usb_line6 *line6 = usb_get_intfdata(interface);
886
887 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
888 line6_start_listen(line6);
889
890 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
891 return 0;
892}
893EXPORT_SYMBOL_GPL(line6_resume);
894
895#endif /* CONFIG_PM */
896
897MODULE_AUTHOR(DRIVER_AUTHOR);
898MODULE_DESCRIPTION(DRIVER_DESC);
899MODULE_LICENSE("GPL");
900