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