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