include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-block.git] / drivers / staging / line6 / driver.c
CommitLineData
705ececd
MG
1/*
2 * Line6 Linux USB driver - 0.8.0
3 *
4 * Copyright (C) 2004-2009 Markus Grabner (grabner@icg.tugraz.at)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2.
9 *
10 */
11
12#include "driver.h"
13
14#include <linux/kernel.h>
15#include <linux/module.h>
5a0e3ad6 16#include <linux/slab.h>
705ececd
MG
17#include <linux/usb.h>
18
19#include "audio.h"
20#include "capture.h"
21#include "control.h"
22#include "midi.h"
23#include "playback.h"
24#include "pod.h"
25#include "revision.h"
26#include "toneport.h"
27#include "usbdefs.h"
28#include "variax.h"
29
30
31#define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
32#define DRIVER_DESC "Line6 USB Driver"
33#define DRIVER_VERSION "0.8.0"
34
35
36/* table of devices that work with this driver */
a457732b 37static const struct usb_device_id line6_id_table[] = {
705ececd
MG
38 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXT) },
39 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTLIVE) },
40 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_BASSPODXTPRO) },
41 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_GUITARPORT) },
42 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_POCKETPOD) },
43 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3) },
44 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODX3LIVE) },
45 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXT) },
46 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTLIVE) },
47 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_PODXTPRO) },
48 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_GX) },
49 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX1) },
50 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_TONEPORT_UX2) },
51 { USB_DEVICE(LINE6_VENDOR_ID, LINE6_DEVID_VARIAX) },
52 { },
53};
36445bc1 54MODULE_DEVICE_TABLE(usb, line6_id_table);
705ececd
MG
55
56static struct line6_properties line6_properties_table[] = {
57 { "BassPODxt", LINE6_BIT_BASSPODXT, LINE6_BIT_CONTROL_PCM },
58 { "BassPODxt Live", LINE6_BIT_BASSPODXTLIVE, LINE6_BIT_CONTROL_PCM },
59 { "BassPODxt Pro", LINE6_BIT_BASSPODXTPRO, LINE6_BIT_CONTROL_PCM },
60 { "GuitarPort", LINE6_BIT_GUITARPORT, LINE6_BIT_PCM },
61 { "Pocket POD", LINE6_BIT_POCKETPOD, LINE6_BIT_CONTROL_PCM },
62 { "POD X3", LINE6_BIT_PODX3, LINE6_BIT_PCM },
63 { "POD X3 Live", LINE6_BIT_PODX3LIVE, LINE6_BIT_PCM },
64 { "PODxt", LINE6_BIT_PODXT, LINE6_BIT_CONTROL_PCM },
65 { "PODxt Live", LINE6_BIT_PODXTLIVE, LINE6_BIT_CONTROL_PCM },
66 { "PODxt Pro", LINE6_BIT_PODXTPRO, LINE6_BIT_CONTROL_PCM },
67 { "TonePort GX", LINE6_BIT_TONEPORT_GX, LINE6_BIT_PCM },
68 { "TonePort UX1", LINE6_BIT_TONEPORT_UX1, LINE6_BIT_PCM },
69 { "TonePort UX2", LINE6_BIT_TONEPORT_UX2, LINE6_BIT_PCM },
70 { "Variax Workbench", LINE6_BIT_VARIAX, LINE6_BIT_CONTROL }
71};
72
73
74/*
75 This is Line6's MIDI manufacturer ID.
76*/
77const unsigned char line6_midi_id[] = { 0x00, 0x01, 0x0c };
78
79struct usb_line6 *line6_devices[LINE6_MAX_DEVICES];
80struct workqueue_struct *line6_workqueue;
81
82
83/**
84 Class for asynchronous messages.
85*/
36445bc1 86struct message {
705ececd
MG
87 struct usb_line6 *line6;
88 const char *buffer;
89 int size;
90 int done;
91};
92
93
94/*
95 Forward declarations.
96*/
0c7ab158 97static void line6_data_received(struct urb *urb);
36445bc1
GKH
98static int line6_send_raw_message_async_part(struct message *msg,
99 struct urb *urb);
705ececd
MG
100
101
102/*
103 Start to listen on endpoint.
104*/
105static int line6_start_listen(struct usb_line6 *line6)
106{
36445bc1
GKH
107 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
108 usb_rcvintpipe(line6->usbdev, line6->ep_control_read),
109 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
110 line6_data_received, line6, line6->interval);
705ececd
MG
111 line6->urb_listen->actual_length = 0;
112 return usb_submit_urb(line6->urb_listen, GFP_KERNEL);
113}
114
115#if DO_DUMP_ANY
116/*
117 Write hexdump to syslog.
118*/
36445bc1
GKH
119void line6_write_hexdump(struct usb_line6 *line6, char dir,
120 const unsigned char *buffer, int size)
705ececd
MG
121{
122 static const int BYTES_PER_LINE = 8;
123 char hexdump[100];
124 char asc[BYTES_PER_LINE + 1];
125 int i, j;
126
36445bc1 127 for (i = 0; i < size; i += BYTES_PER_LINE) {
705ececd
MG
128 int hexdumpsize = sizeof(hexdump);
129 char *p = hexdump;
130 int n = min(size - i, BYTES_PER_LINE);
131 asc[n] = 0;
132
36445bc1 133 for (j = 0; j < BYTES_PER_LINE; ++j) {
705ececd
MG
134 int bytes;
135
36445bc1 136 if (j < n) {
705ececd
MG
137 unsigned char val = buffer[i + j];
138 bytes = snprintf(p, hexdumpsize, " %02X", val);
139 asc[j] = ((val >= 0x20) && (val < 0x7f)) ? val : '.';
36445bc1 140 } else
705ececd
MG
141 bytes = snprintf(p, hexdumpsize, " ");
142
36445bc1 143 if (bytes > hexdumpsize)
705ececd
MG
144 break; /* buffer overflow */
145
146 p += bytes;
147 hexdumpsize -= bytes;
148 }
149
150 dev_info(line6->ifcdev, "%c%04X:%s %s\n", dir, i, hexdump, asc);
151 }
152}
153#endif
154
155#if DO_DUMP_URB_RECEIVE
156/*
157 Dump URB data to syslog.
158*/
159static void line6_dump_urb(struct urb *urb)
160{
161 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
162
36445bc1 163 if (urb->status < 0)
705ececd
MG
164 return;
165
36445bc1
GKH
166 line6_write_hexdump(line6, 'R', (unsigned char *)urb->transfer_buffer,
167 urb->actual_length);
705ececd
MG
168}
169#endif
170
171/*
d722a510 172 Send raw message in pieces of max_packet_size bytes.
705ececd 173*/
36445bc1
GKH
174int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
175 int size)
705ececd
MG
176{
177 int i, done = 0;
d722a510 178 int actual_size;
705ececd
MG
179
180#if DO_DUMP_URB_SEND
181 line6_write_hexdump(line6, 'S', buffer, size);
182#endif
183
d722a510 184 for (i = 0; i < size; i += actual_size) {
705ececd
MG
185 const char *frag_buf = buffer + i;
186 int frag_size = min(line6->max_packet_size, size - i);
36445bc1
GKH
187 int retval;
188
189 retval = usb_interrupt_msg(line6->usbdev,
190 usb_sndintpipe(line6->usbdev,
191 line6->ep_control_write),
192 (char *)frag_buf, frag_size,
d722a510 193 &actual_size, LINE6_TIMEOUT * HZ);
705ececd 194
36445bc1
GKH
195 if (retval) {
196 dev_err(line6->ifcdev,
197 "usb_interrupt_msg failed (%d)\n", retval);
705ececd
MG
198 break;
199 }
200
d722a510 201 done += actual_size;
705ececd
MG
202 }
203
204 return done;
205}
206
207/*
208 Notification of completion of asynchronous request transmission.
209*/
0c7ab158 210static void line6_async_request_sent(struct urb *urb)
705ececd
MG
211{
212 struct message *msg = (struct message *)urb->context;
213
36445bc1 214 if (msg->done >= msg->size) {
705ececd
MG
215 usb_free_urb(urb);
216 kfree(msg);
36445bc1 217 } else
705ececd
MG
218 line6_send_raw_message_async_part(msg, urb);
219}
220
221/*
222 Asynchronously send part of a raw message.
223*/
36445bc1
GKH
224static int line6_send_raw_message_async_part(struct message *msg,
225 struct urb *urb)
705ececd
MG
226{
227 int retval;
228 struct usb_line6 *line6 = msg->line6;
229 int done = msg->done;
230 int bytes = min(msg->size - done, line6->max_packet_size);
231
36445bc1
GKH
232 usb_fill_int_urb(urb, line6->usbdev,
233 usb_sndintpipe(line6->usbdev, line6->ep_control_write),
234 (char *)msg->buffer + done, bytes,
235 line6_async_request_sent, msg, line6->interval);
705ececd
MG
236
237#if DO_DUMP_URB_SEND
238 line6_write_hexdump(line6, 'S', (char *)msg->buffer + done, bytes);
239#endif
240
241 msg->done += bytes;
242 retval = usb_submit_urb(urb, GFP_ATOMIC);
243
36445bc1
GKH
244 if (retval < 0) {
245 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
246 __func__, retval);
705ececd
MG
247 usb_free_urb(urb);
248 kfree(msg);
249 return -EINVAL;
250 }
251
252 return 0;
253}
254
255/*
256 Asynchronously send raw message.
257*/
36445bc1
GKH
258int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
259 int size)
705ececd
MG
260{
261 struct message *msg;
262 struct urb *urb;
263
264 /* create message: */
265 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
266
36445bc1 267 if (msg == NULL) {
705ececd
MG
268 dev_err(line6->ifcdev, "Out of memory\n");
269 return -ENOMEM;
270 }
271
272 /* create URB: */
273 urb = usb_alloc_urb(0, GFP_ATOMIC);
274
36445bc1 275 if (urb == NULL) {
705ececd
MG
276 kfree(msg);
277 dev_err(line6->ifcdev, "Out of memory\n");
278 return -ENOMEM;
279 }
280
281 /* set message data: */
282 msg->line6 = line6;
283 msg->buffer = buffer;
284 msg->size = size;
285 msg->done = 0;
286
287 /* start sending: */
288 return line6_send_raw_message_async_part(msg, urb);
289}
290
291/*
292 Send sysex message in pieces of wMaxPacketSize bytes.
293*/
36445bc1
GKH
294int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
295 int size)
705ececd
MG
296{
297 return line6_send_raw_message(line6, buffer, size + SYSEX_EXTRA_SIZE) - SYSEX_EXTRA_SIZE;
298}
299
300/*
301 Allocate buffer for sysex message and prepare header.
302 @param code sysex message code
303 @param size number of bytes between code and sysex end
304*/
36445bc1
GKH
305char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
306 int size)
705ececd
MG
307{
308 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_KERNEL);
309
36445bc1 310 if (!buffer) {
705ececd 311 dev_err(line6->ifcdev, "out of memory\n");
536165d8 312 return NULL;
705ececd
MG
313 }
314
315 buffer[0] = LINE6_SYSEX_BEGIN;
316 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
317 buffer[sizeof(line6_midi_id) + 1] = code1;
318 buffer[sizeof(line6_midi_id) + 2] = code2;
319 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
320 return buffer;
321}
322
323/*
324 Notification of data received from the Line6 device.
325*/
0c7ab158 326static void line6_data_received(struct urb *urb)
705ececd
MG
327{
328 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
329 struct MidiBuffer *mb = &line6->line6midi->midibuf_in;
330 int done;
331
36445bc1 332 if (urb->status == -ESHUTDOWN)
705ececd
MG
333 return;
334
335#if DO_DUMP_URB_RECEIVE
336 line6_dump_urb(urb);
337#endif
338
339 done = midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
340
36445bc1 341 if (done < urb->actual_length) {
705ececd
MG
342 midibuf_ignore(mb, done);
343 DEBUG_MESSAGES(dev_err(line6->ifcdev, "%d %d buffer overflow - message skipped\n", done, urb->actual_length));
344 }
345
36445bc1 346 for (;;) {
705ececd
MG
347 done = midibuf_read(mb, line6->buffer_message, LINE6_MESSAGE_MAXLEN);
348
36445bc1 349 if (done == 0)
705ececd
MG
350 break;
351
352 /* MIDI input filter */
36445bc1 353 if (midibuf_skip_message(mb, line6->line6midi->midi_mask_receive))
705ececd
MG
354 continue;
355
356 line6->message_length = done;
357#if DO_DUMP_MIDI_RECEIVE
358 line6_write_hexdump(line6, 'r', line6->buffer_message, done);
359#endif
360 line6_midi_receive(line6, line6->buffer_message, done);
361
36445bc1 362 switch (line6->usbdev->descriptor.idProduct) {
705ececd
MG
363 case LINE6_DEVID_BASSPODXT:
364 case LINE6_DEVID_BASSPODXTLIVE:
365 case LINE6_DEVID_BASSPODXTPRO:
366 case LINE6_DEVID_PODXT:
367 case LINE6_DEVID_PODXTPRO:
368 case LINE6_DEVID_POCKETPOD:
369 pod_process_message((struct usb_line6_pod *)line6);
370 break;
371
372 case LINE6_DEVID_PODXTLIVE:
36445bc1 373 switch (line6->interface_number) {
705ececd
MG
374 case PODXTLIVE_INTERFACE_POD:
375 pod_process_message((struct usb_line6_pod *)line6);
376 break;
377
378 case PODXTLIVE_INTERFACE_VARIAX:
379 variax_process_message((struct usb_line6_variax *)line6);
380 break;
381
382 default:
383 dev_err(line6->ifcdev, "PODxt Live interface %d not supported\n", line6->interface_number);
384 }
385 break;
386
387 case LINE6_DEVID_VARIAX:
388 variax_process_message((struct usb_line6_variax *)line6);
389 break;
390
391 default:
392 MISSING_CASE;
393 }
394 }
395
396 line6_start_listen(line6);
397}
398
d722a510
FD
399static int line6_send(struct usb_line6 *line6, unsigned char *buf, size_t len)
400{
401 int retval;
402 unsigned int partial;
403
404#if DO_DUMP_URB_SEND
405 line6_write_hexdump(line6, 'S', buf, len);
406#endif
407
408 retval = usb_interrupt_msg(line6->usbdev,
409 usb_sndintpipe(line6->usbdev,
410 line6->ep_control_write),
411 buf, len, &partial,
412 LINE6_TIMEOUT * HZ);
413
414 if (retval) {
415 dev_err(line6->ifcdev,
416 "usb_interrupt_msg failed (%d)\n", retval);
417 }
418
419 if (partial != len) {
420 dev_err(line6->ifcdev,
421 "usb_interrupt_msg sent partial message (%d)\n",
422 retval);
423 }
424
425 return retval;
426}
427
705ececd
MG
428/*
429 Send channel number (i.e., switch to a different sound).
430*/
431int line6_send_program(struct usb_line6 *line6, int value)
432{
705ececd 433 unsigned char *buffer;
d722a510 434 size_t len = 2;
705ececd 435
d722a510 436 buffer = kmalloc(len, GFP_KERNEL);
36445bc1 437 if (!buffer) {
705ececd
MG
438 dev_err(line6->ifcdev, "out of memory\n");
439 return -ENOMEM;
440 }
441
442 buffer[0] = LINE6_PROGRAM_CHANGE | LINE6_CHANNEL_HOST;
443 buffer[1] = value;
444
d722a510 445 return line6_send(line6, buffer, len);
705ececd
MG
446}
447
448/*
449 Transmit Line6 control parameter.
450*/
451int line6_transmit_parameter(struct usb_line6 *line6, int param, int value)
452{
705ececd 453 unsigned char *buffer;
d722a510 454 size_t len = 3;
705ececd 455
d722a510 456 buffer = kmalloc(len, GFP_KERNEL);
36445bc1 457 if (!buffer) {
705ececd
MG
458 dev_err(line6->ifcdev, "out of memory\n");
459 return -ENOMEM;
460 }
461
462 buffer[0] = LINE6_PARAM_CHANGE | LINE6_CHANNEL_HOST;
463 buffer[1] = param;
464 buffer[2] = value;
465
d722a510 466 return line6_send(line6, buffer, len);
705ececd
MG
467}
468
469/*
470 Read data from device.
471*/
472int line6_read_data(struct usb_line6 *line6, int address, void *data, size_t datalen)
473{
474 struct usb_device *usbdev = line6->usbdev;
475 int ret;
476 unsigned char len;
477
478 /* query the serial number: */
479 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
d722a510
FD
480 USB_TYPE_VENDOR | USB_RECIP_DEVICE
481 | USB_DIR_OUT,
482 (datalen << 8) | 0x21, address,
483 NULL, 0, LINE6_TIMEOUT * HZ);
705ececd 484
36445bc1 485 if (ret < 0) {
705ececd
MG
486 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
487 return ret;
488 }
489
490 /* Wait for data length. We'll get a couple of 0xff until length arrives. */
491 do {
492 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
36445bc1
GKH
493 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
494 USB_DIR_IN,
495 0x0012, 0x0000, &len, 1,
496 LINE6_TIMEOUT * HZ);
497 if (ret < 0) {
498 dev_err(line6->ifcdev,
499 "receive length failed (error %d)\n", ret);
705ececd
MG
500 return ret;
501 }
d722a510 502 } while (len == 0xff);
36445bc1
GKH
503
504 if (len != datalen) {
505 /* should be equal or something went wrong */
506 dev_err(line6->ifcdev,
507 "length mismatch (expected %d, got %d)\n",
508 (int)datalen, (int)len);
705ececd
MG
509 return -EINVAL;
510 }
511
512 /* receive the result: */
513 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
36445bc1
GKH
514 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
515 0x0013, 0x0000, data, datalen,
516 LINE6_TIMEOUT * HZ);
705ececd 517
36445bc1 518 if (ret < 0) {
705ececd
MG
519 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
520 return ret;
521 }
522
523 return 0;
524}
525
526/*
527 Write data to device.
528*/
36445bc1
GKH
529int line6_write_data(struct usb_line6 *line6, int address, void *data,
530 size_t datalen)
705ececd
MG
531{
532 struct usb_device *usbdev = line6->usbdev;
533 int ret;
534 unsigned char status;
535
36445bc1
GKH
536 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
537 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
538 0x0022, address, data, datalen,
539 LINE6_TIMEOUT * HZ);
705ececd 540
36445bc1
GKH
541 if (ret < 0) {
542 dev_err(line6->ifcdev,
543 "write request failed (error %d)\n", ret);
705ececd
MG
544 return ret;
545 }
546
547 do {
36445bc1
GKH
548 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
549 0x67,
550 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
551 USB_DIR_IN,
552 0x0012, 0x0000,
553 &status, 1, LINE6_TIMEOUT * HZ);
554
555 if (ret < 0) {
556 dev_err(line6->ifcdev,
557 "receiving status failed (error %d)\n", ret);
705ececd
MG
558 return ret;
559 }
560 }
36445bc1
GKH
561 while (status == 0xff)
562 ;
705ececd 563
36445bc1 564 if (status != 0) {
705ececd
MG
565 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
566 return -EINVAL;
567 }
568
569 return 0;
570}
571
572/*
573 Read Line6 device serial number.
574 (POD, TonePort, GuitarPort)
575*/
576int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
577{
578 return line6_read_data(line6, 0x80d0, serial_number, sizeof(*serial_number));
579}
580
581/*
582 No operation (i.e., unsupported).
583*/
77491e52
GKH
584ssize_t line6_nop_read(struct device *dev, struct device_attribute *attr,
585 char *buf)
705ececd
MG
586{
587 return 0;
588}
589
590/*
591 No operation (i.e., unsupported).
592*/
77491e52
GKH
593ssize_t line6_nop_write(struct device *dev, struct device_attribute *attr,
594 const char *buf, size_t count)
705ececd
MG
595{
596 return count;
597}
598
599/*
600 "write" request on "raw" special file.
601*/
602#if CREATE_RAW_FILE
77491e52
GKH
603ssize_t line6_set_raw(struct device *dev, struct device_attribute *attr,
604 const char *buf, size_t count)
705ececd
MG
605{
606 struct usb_interface *interface = to_usb_interface(dev);
607 struct usb_line6 *line6 = usb_get_intfdata(interface);
608 line6_send_raw_message(line6, buf, count);
609 return count;
610}
611#endif
612
613/*
614 Generic destructor.
615*/
616static void line6_destruct(struct usb_interface *interface)
617{
618 struct usb_line6 *line6;
36445bc1
GKH
619
620 if (interface == NULL)
621 return;
705ececd 622 line6 = usb_get_intfdata(interface);
36445bc1
GKH
623 if (line6 == NULL)
624 return;
705ececd
MG
625
626 /* free buffer memory first: */
36445bc1
GKH
627 kfree(line6->buffer_message);
628 kfree(line6->buffer_listen);
705ececd
MG
629
630 /* then free URBs: */
36445bc1 631 usb_free_urb(line6->urb_listen);
705ececd
MG
632
633 /* make sure the device isn't destructed twice: */
634 usb_set_intfdata(interface, NULL);
635
636 /* free interface data: */
637 kfree(line6);
638}
639
640static void line6_list_devices(void)
641{
642 int i;
643
36445bc1 644 for (i = 0; i < LINE6_MAX_DEVICES; ++i) {
705ececd
MG
645 struct usb_line6 *dev = line6_devices[i];
646 printk(KERN_INFO "Line6 device %d: ", i);
647
36445bc1 648 if (dev == NULL)
705ececd
MG
649 printk("(not used)\n");
650 else
651 printk("%s:%d\n", dev->properties->name, dev->interface_number);
652 }
653}
654
655/*
656 Probe USB device.
657*/
658static int line6_probe(struct usb_interface *interface, const struct usb_device_id *id)
659{
660 int devtype;
536165d8
GKH
661 struct usb_device *usbdev = NULL;
662 struct usb_line6 *line6 = NULL;
705ececd
MG
663 const struct line6_properties *properties;
664 int devnum;
665 int interface_number, alternate = 0;
666 int product;
667 int size = 0;
668 int ep_read = 0, ep_write = 0;
669 int ret;
670
36445bc1
GKH
671 if (interface == NULL)
672 return -ENODEV;
705ececd 673 usbdev = interface_to_usbdev(interface);
36445bc1
GKH
674 if (usbdev == NULL)
675 return -ENODEV;
705ececd
MG
676
677 /* increment reference counters: */
678 usb_get_intf(interface);
679 usb_get_dev(usbdev);
680
681 /* we don't handle multiple configurations */
36445bc1 682 if (usbdev->descriptor.bNumConfigurations != 1)
705ececd
MG
683 return -ENODEV;
684
685 /* check vendor and product id */
d722a510
FD
686 for (devtype = ARRAY_SIZE(line6_id_table) - 1; devtype--;) {
687 u16 vendor = le16_to_cpu(usbdev->descriptor.idVendor);
688 u16 product = le16_to_cpu(usbdev->descriptor.idProduct);
689
690 if (vendor == line6_id_table[devtype].idVendor
691 && product == line6_id_table[devtype].idProduct)
705ececd 692 break;
d722a510 693 }
705ececd 694
36445bc1 695 if (devtype < 0)
705ececd
MG
696 return -ENODEV;
697
698 /* find free slot in device table: */
36445bc1
GKH
699 for (devnum = 0; devnum < LINE6_MAX_DEVICES; ++devnum)
700 if (line6_devices[devnum] == NULL)
705ececd
MG
701 break;
702
36445bc1 703 if (devnum == LINE6_MAX_DEVICES)
705ececd
MG
704 return -ENODEV;
705
706 /* initialize device info: */
707 properties = &line6_properties_table[devtype];
708 dev_info(&interface->dev, "Line6 %s found\n", properties->name);
709 product = le16_to_cpu(usbdev->descriptor.idProduct);
710
711 /* query interface number */
712 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
713
36445bc1 714 switch (product) {
705ececd
MG
715 case LINE6_DEVID_BASSPODXTLIVE:
716 case LINE6_DEVID_POCKETPOD:
717 case LINE6_DEVID_PODXTLIVE:
718 case LINE6_DEVID_VARIAX:
719 alternate = 1;
720 break;
721
722 case LINE6_DEVID_PODX3:
723 case LINE6_DEVID_PODX3LIVE:
36445bc1
GKH
724 switch (interface_number) {
725 case 0:
726 alternate = 1;
727 break;
728 case 1:
729 alternate = 0;
730 break;
731 default:
732 MISSING_CASE;
705ececd
MG
733 }
734 break;
735
736 case LINE6_DEVID_BASSPODXT:
737 case LINE6_DEVID_BASSPODXTPRO:
738 case LINE6_DEVID_PODXT:
739 case LINE6_DEVID_PODXTPRO:
740 alternate = 5;
741 break;
742
743 case LINE6_DEVID_TONEPORT_GX:
744 case LINE6_DEVID_GUITARPORT:
36445bc1 745 alternate = 2; /* 1..4 seem to be ok */
705ececd
MG
746 break;
747
748 case LINE6_DEVID_TONEPORT_UX1:
749 case LINE6_DEVID_TONEPORT_UX2:
36445bc1
GKH
750 switch (interface_number) {
751 case 0:
752 /* defaults to 44.1kHz, 16-bit */
753 alternate = 2;
754 break;
755 case 1:
756 alternate = 0;
757 break;
758 default:
759 MISSING_CASE;
705ececd
MG
760 }
761 break;
762
763 default:
764 MISSING_CASE;
765 return -ENODEV;
766 }
767
36445bc1
GKH
768 ret = usb_set_interface(usbdev, interface_number, alternate);
769 if (ret < 0) {
705ececd
MG
770 dev_err(&interface->dev, "set_interface failed\n");
771 return ret;
772 }
773
774 /* initialize device data based on product id: */
36445bc1 775 switch (product) {
705ececd
MG
776 case LINE6_DEVID_BASSPODXT:
777 case LINE6_DEVID_BASSPODXTLIVE:
778 case LINE6_DEVID_BASSPODXTPRO:
779 case LINE6_DEVID_POCKETPOD:
780 case LINE6_DEVID_PODXT:
781 case LINE6_DEVID_PODXTPRO:
782 size = sizeof(struct usb_line6_pod);
783 ep_read = 0x84;
784 ep_write = 0x03;
785 break;
786
787 case LINE6_DEVID_PODX3:
788 case LINE6_DEVID_PODX3LIVE:
789 /* currently unused! */
790 size = sizeof(struct usb_line6_pod);
791 ep_read = 0x81;
792 ep_write = 0x01;
793 break;
794
795 case LINE6_DEVID_TONEPORT_GX:
796 case LINE6_DEVID_TONEPORT_UX1:
797 case LINE6_DEVID_TONEPORT_UX2:
798 case LINE6_DEVID_GUITARPORT:
799 size = sizeof(struct usb_line6_toneport);
800 /* these don't have a control channel */
801 break;
802
803 case LINE6_DEVID_PODXTLIVE:
36445bc1 804 switch (interface_number) {
705ececd
MG
805 case PODXTLIVE_INTERFACE_POD:
806 size = sizeof(struct usb_line6_pod);
807 ep_read = 0x84;
808 ep_write = 0x03;
809 break;
810
811 case PODXTLIVE_INTERFACE_VARIAX:
812 size = sizeof(struct usb_line6_variax);
813 ep_read = 0x86;
814 ep_write = 0x05;
815 break;
816
817 default:
818 return -ENODEV;
819 }
820 break;
821
822 case LINE6_DEVID_VARIAX:
823 size = sizeof(struct usb_line6_variax);
824 ep_read = 0x82;
825 ep_write = 0x01;
826 break;
827
828 default:
829 MISSING_CASE;
830 return -ENODEV;
831 }
832
36445bc1 833 if (size == 0) {
705ececd
MG
834 dev_err(line6->ifcdev, "driver bug: interface data size not set\n");
835 return -ENODEV;
836 }
837
838 line6 = kzalloc(size, GFP_KERNEL);
839
36445bc1 840 if (line6 == NULL) {
705ececd
MG
841 dev_err(&interface->dev, "Out of memory\n");
842 return -ENOMEM;
843 }
844
845 /* store basic data: */
846 line6->interface_number = interface_number;
847 line6->properties = properties;
848 line6->usbdev = usbdev;
849 line6->ifcdev = &interface->dev;
850 line6->ep_control_read = ep_read;
851 line6->ep_control_write = ep_write;
852 line6->product = product;
853
854 /* get data from endpoint descriptor (see usb_maxpacket): */
855 {
856 struct usb_host_endpoint *ep;
857 unsigned epnum = usb_pipeendpoint(usb_rcvintpipe(usbdev, ep_read));
858 ep = usbdev->ep_in[epnum];
859
36445bc1 860 if (ep != NULL) {
705ececd
MG
861 line6->interval = ep->desc.bInterval;
862 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
36445bc1 863 } else {
705ececd
MG
864 line6->interval = LINE6_FALLBACK_INTERVAL;
865 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
866 dev_err(line6->ifcdev, "endpoint not available, using fallback values");
867 }
868 }
869
870 usb_set_intfdata(interface, line6);
871
36445bc1 872 if (properties->capabilities & LINE6_BIT_CONTROL) {
705ececd
MG
873 /* initialize USB buffers: */
874 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
875
36445bc1 876 if (line6->buffer_listen == NULL) {
705ececd
MG
877 dev_err(&interface->dev, "Out of memory\n");
878 line6_destruct(interface);
879 return -ENOMEM;
880 }
881
882 line6->buffer_message = kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
883
36445bc1 884 if (line6->buffer_message == NULL) {
705ececd
MG
885 dev_err(&interface->dev, "Out of memory\n");
886 line6_destruct(interface);
887 return -ENOMEM;
888 }
889
890 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
891
36445bc1 892 if (line6->urb_listen == NULL) {
705ececd
MG
893 dev_err(&interface->dev, "Out of memory\n");
894 line6_destruct(interface);
895 return -ENOMEM;
896 }
897
36445bc1
GKH
898 ret = line6_start_listen(line6);
899 if (ret < 0) {
900 dev_err(&interface->dev, "%s: usb_submit_urb failed\n",
901 __func__);
705ececd
MG
902 line6_destruct(interface);
903 return ret;
904 }
905 }
906
907 /* initialize device data based on product id: */
36445bc1 908 switch (product) {
705ececd
MG
909 case LINE6_DEVID_BASSPODXT:
910 case LINE6_DEVID_BASSPODXTLIVE:
911 case LINE6_DEVID_BASSPODXTPRO:
912 case LINE6_DEVID_POCKETPOD:
913 case LINE6_DEVID_PODX3:
914 case LINE6_DEVID_PODX3LIVE:
915 case LINE6_DEVID_PODXT:
916 case LINE6_DEVID_PODXTPRO:
917 ret = pod_init(interface, (struct usb_line6_pod *)line6);
918 break;
919
920 case LINE6_DEVID_PODXTLIVE:
36445bc1 921 switch (interface_number) {
705ececd
MG
922 case PODXTLIVE_INTERFACE_POD:
923 ret = pod_init(interface, (struct usb_line6_pod *)line6);
924 break;
925
926 case PODXTLIVE_INTERFACE_VARIAX:
927 ret = variax_init(interface, (struct usb_line6_variax *)line6);
928 break;
929
930 default:
36445bc1
GKH
931 dev_err(&interface->dev,
932 "PODxt Live interface %d not supported\n",
933 interface_number);
705ececd
MG
934 ret = -ENODEV;
935 }
936
937 break;
938
939 case LINE6_DEVID_VARIAX:
940 ret = variax_init(interface, (struct usb_line6_variax *)line6);
941 break;
942
943 case LINE6_DEVID_TONEPORT_GX:
944 case LINE6_DEVID_TONEPORT_UX1:
945 case LINE6_DEVID_TONEPORT_UX2:
946 case LINE6_DEVID_GUITARPORT:
947 ret = toneport_init(interface, (struct usb_line6_toneport *)line6);
948 break;
949
950 default:
951 MISSING_CASE;
952 ret = -ENODEV;
953 }
954
36445bc1 955 if (ret < 0) {
705ececd
MG
956 line6_destruct(interface);
957 return ret;
958 }
959
36445bc1
GKH
960 ret = sysfs_create_link(&interface->dev.kobj, &usbdev->dev.kobj,
961 "usb_device");
962 if (ret < 0) {
705ececd
MG
963 line6_destruct(interface);
964 return ret;
965 }
966
36445bc1
GKH
967 dev_info(&interface->dev, "Line6 %s now attached\n",
968 line6->properties->name);
705ececd
MG
969 line6_devices[devnum] = line6;
970 line6_list_devices();
971 return ret;
972}
973
974/*
975 Line6 device disconnected.
976*/
977static void line6_disconnect(struct usb_interface *interface)
978{
979 struct usb_line6 *line6;
980 struct usb_device *usbdev;
981 int interface_number, i;
982
36445bc1
GKH
983 if (interface == NULL)
984 return;
705ececd 985 usbdev = interface_to_usbdev(interface);
36445bc1
GKH
986 if (usbdev == NULL)
987 return;
705ececd
MG
988
989 sysfs_remove_link(&interface->dev.kobj, "usb_device");
990
991 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
992 line6 = usb_get_intfdata(interface);
993
36445bc1
GKH
994 if (line6 != NULL) {
995 if (line6->urb_listen != NULL)
996 usb_kill_urb(line6->urb_listen);
705ececd 997
36445bc1
GKH
998 if (usbdev != line6->usbdev)
999 dev_err(line6->ifcdev,
1000 "driver bug: inconsistent usb device\n");
705ececd 1001
36445bc1 1002 switch (line6->usbdev->descriptor.idProduct) {
705ececd
MG
1003 case LINE6_DEVID_BASSPODXT:
1004 case LINE6_DEVID_BASSPODXTLIVE:
1005 case LINE6_DEVID_BASSPODXTPRO:
1006 case LINE6_DEVID_POCKETPOD:
1007 case LINE6_DEVID_PODX3:
1008 case LINE6_DEVID_PODX3LIVE:
1009 case LINE6_DEVID_PODXT:
1010 case LINE6_DEVID_PODXTPRO:
1011 pod_disconnect(interface);
1012 break;
1013
1014 case LINE6_DEVID_PODXTLIVE:
36445bc1 1015 switch (interface_number) {
705ececd
MG
1016 case PODXTLIVE_INTERFACE_POD:
1017 pod_disconnect(interface);
1018 break;
1019
1020 case PODXTLIVE_INTERFACE_VARIAX:
1021 variax_disconnect(interface);
1022 break;
1023 }
1024
1025 break;
1026
1027 case LINE6_DEVID_VARIAX:
1028 variax_disconnect(interface);
1029 break;
1030
1031 case LINE6_DEVID_TONEPORT_GX:
1032 case LINE6_DEVID_TONEPORT_UX1:
1033 case LINE6_DEVID_TONEPORT_UX2:
1034 case LINE6_DEVID_GUITARPORT:
1035 toneport_disconnect(interface);
1036 break;
1037
1038 default:
1039 MISSING_CASE;
1040 }
1041
1042 dev_info(&interface->dev, "Line6 %s now disconnected\n", line6->properties->name);
1043
d722a510 1044 for (i = LINE6_MAX_DEVICES; i--;) {
36445bc1 1045 if (line6_devices[i] == line6)
536165d8 1046 line6_devices[i] = NULL;
d722a510 1047 }
705ececd
MG
1048 }
1049
1050 line6_destruct(interface);
1051
1052 /* decrement reference counters: */
1053 usb_put_intf(interface);
1054 usb_put_dev(usbdev);
1055
1056 line6_list_devices();
1057}
1058
1059static struct usb_driver line6_driver = {
705ececd
MG
1060 .name = DRIVER_NAME,
1061 .probe = line6_probe,
1062 .disconnect = line6_disconnect,
1063 .id_table = line6_id_table,
1064};
1065
1066/*
1067 Module initialization.
1068*/
1069static int __init line6_init(void)
1070{
1071 int i, retval;
1072
36445bc1
GKH
1073 printk(KERN_INFO "%s driver version %s%s\n",
1074 DRIVER_NAME, DRIVER_VERSION, DRIVER_REVISION);
705ececd
MG
1075 line6_workqueue = create_workqueue(DRIVER_NAME);
1076
536165d8 1077 if (line6_workqueue == NULL) {
705ececd
MG
1078 err("couldn't create workqueue");
1079 return -EINVAL;
1080 }
1081
36445bc1 1082 for (i = LINE6_MAX_DEVICES; i--;)
536165d8 1083 line6_devices[i] = NULL;
705ececd
MG
1084
1085 retval = usb_register(&line6_driver);
1086
36445bc1 1087 if (retval)
705ececd
MG
1088 err("usb_register failed. Error number %d", retval);
1089
1090 return retval;
1091}
1092
1093/*
1094 Module cleanup.
1095*/
1096static void __exit line6_exit(void)
1097{
1098 destroy_workqueue(line6_workqueue);
1099 usb_deregister(&line6_driver);
1100}
1101
1102module_init(line6_init);
1103module_exit(line6_exit);
1104
1105MODULE_AUTHOR(DRIVER_AUTHOR);
1106MODULE_DESCRIPTION(DRIVER_DESC);
1107MODULE_LICENSE("GPL");
1108MODULE_VERSION(DRIVER_VERSION);