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