blob: 81b7da8e56d39e352a5b629bbdf73aa460889b4d [file] [log] [blame]
Markus Grabner705ecec2009-02-27 19:43:04 -08001/*
Chris Rorvickc078a4a2015-01-20 02:20:50 -06002 * Line 6 Linux USB driver
Markus Grabner705ecec2009-02-27 19:43:04 -08003 *
Markus Grabner1027f472010-08-12 01:35:30 +02004 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
Markus Grabner705ecec2009-02-27 19:43:04 -08005 *
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
Markus Grabner705ecec2009-02-27 19:43:04 -080012#include <linux/kernel.h>
13#include <linux/module.h>
Takashi Iwaiccddbe42015-01-15 08:22:31 +010014#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Markus Grabner705ecec2009-02-27 19:43:04 -080016#include <linux/usb.h>
17
Takashi Iwai85a93392015-01-19 15:54:00 +010018#include <sound/core.h>
19#include <sound/initval.h>
20
Markus Grabner705ecec2009-02-27 19:43:04 -080021#include "capture.h"
Markus Grabner1027f472010-08-12 01:35:30 +020022#include "driver.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080023#include "midi.h"
24#include "playback.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080025
Markus Grabner705ecec2009-02-27 19:43:04 -080026#define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
Chris Rorvickc6fffce2015-01-20 02:20:49 -060027#define DRIVER_DESC "Line 6 USB Driver"
Markus Grabner705ecec2009-02-27 19:43:04 -080028
Markus Grabner705ecec2009-02-27 19:43:04 -080029/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -060030 This is Line 6's MIDI manufacturer ID.
Markus Grabner705ecec2009-02-27 19:43:04 -080031*/
Markus Grabner1027f472010-08-12 01:35:30 +020032const unsigned char line6_midi_id[] = {
33 0x00, 0x01, 0x0c
34};
Takashi Iwaiccddbe42015-01-15 08:22:31 +010035EXPORT_SYMBOL_GPL(line6_midi_id);
Markus Grabner1027f472010-08-12 01:35:30 +020036
37/*
38 Code to request version of POD, Variax interface
39 (and maybe other devices).
40*/
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -070041static const char line6_request_version[] = {
Markus Grabner1027f472010-08-12 01:35:30 +020042 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
43};
44
Takashi Iwaicddbd4f2015-01-28 14:43:11 +010045/*
Markus Grabner705ecec2009-02-27 19:43:04 -080046 Class for asynchronous messages.
47*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -080048struct message {
Markus Grabner705ecec2009-02-27 19:43:04 -080049 struct usb_line6 *line6;
50 const char *buffer;
51 int size;
52 int done;
53};
54
Markus Grabner705ecec2009-02-27 19:43:04 -080055/*
56 Forward declarations.
57*/
Greg Kroah-Hartman0c7ab152009-02-27 20:28:04 -080058static void line6_data_received(struct urb *urb);
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -080059static int line6_send_raw_message_async_part(struct message *msg,
60 struct urb *urb);
Markus Grabner705ecec2009-02-27 19:43:04 -080061
Markus Grabner705ecec2009-02-27 19:43:04 -080062/*
63 Start to listen on endpoint.
64*/
65static int line6_start_listen(struct usb_line6 *line6)
66{
Markus Grabner1027f472010-08-12 01:35:30 +020067 int err;
Fabian Mewesa6b46992014-03-24 23:46:31 +010068
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -080069 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
Chris Rorvick9e165be2015-01-12 12:42:53 -080070 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
71 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
72 line6_data_received, line6, line6->interval);
Markus Grabner705ecec2009-02-27 19:43:04 -080073 line6->urb_listen->actual_length = 0;
Markus Grabnere1a164d2010-08-23 01:08:25 +020074 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
Markus Grabner1027f472010-08-12 01:35:30 +020075 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -080076}
77
Markus Grabner1027f472010-08-12 01:35:30 +020078/*
79 Stop listening on endpoint.
80*/
81static void line6_stop_listen(struct usb_line6 *line6)
82{
83 usb_kill_urb(line6->urb_listen);
84}
85
Markus Grabner705ecec2009-02-27 19:43:04 -080086/*
Markus Grabner1027f472010-08-12 01:35:30 +020087 Send raw message in pieces of wMaxPacketSize bytes.
Markus Grabner705ecec2009-02-27 19:43:04 -080088*/
Takashi Iwai73723192015-01-19 16:36:21 +010089static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
90 int size)
Markus Grabner705ecec2009-02-27 19:43:04 -080091{
92 int i, done = 0;
93
Markus Grabner1027f472010-08-12 01:35:30 +020094 for (i = 0; i < size; i += line6->max_packet_size) {
95 int partial;
Markus Grabner705ecec2009-02-27 19:43:04 -080096 const char *frag_buf = buffer + i;
97 int frag_size = min(line6->max_packet_size, size - i);
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -080098 int retval;
Markus Grabner705ecec2009-02-27 19:43:04 -080099
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800100 retval = usb_interrupt_msg(line6->usbdev,
Davide Berardiba79e522014-03-17 03:34:41 +0100101 usb_sndintpipe(line6->usbdev,
Chris Rorvick9e165be2015-01-12 12:42:53 -0800102 line6->properties->ep_ctrl_w),
Davide Berardiba79e522014-03-17 03:34:41 +0100103 (char *)frag_buf, frag_size,
104 &partial, LINE6_TIMEOUT * HZ);
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800105
106 if (retval) {
107 dev_err(line6->ifcdev,
108 "usb_interrupt_msg failed (%d)\n", retval);
Markus Grabner705ecec2009-02-27 19:43:04 -0800109 break;
110 }
111
Markus Grabner1027f472010-08-12 01:35:30 +0200112 done += frag_size;
Markus Grabner705ecec2009-02-27 19:43:04 -0800113 }
114
115 return done;
116}
117
118/*
119 Notification of completion of asynchronous request transmission.
120*/
Greg Kroah-Hartman0c7ab152009-02-27 20:28:04 -0800121static void line6_async_request_sent(struct urb *urb)
Markus Grabner705ecec2009-02-27 19:43:04 -0800122{
123 struct message *msg = (struct message *)urb->context;
124
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800125 if (msg->done >= msg->size) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800126 usb_free_urb(urb);
127 kfree(msg);
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800128 } else
Markus Grabner705ecec2009-02-27 19:43:04 -0800129 line6_send_raw_message_async_part(msg, urb);
130}
131
132/*
133 Asynchronously send part of a raw message.
134*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800135static int line6_send_raw_message_async_part(struct message *msg,
136 struct urb *urb)
Markus Grabner705ecec2009-02-27 19:43:04 -0800137{
138 int retval;
139 struct usb_line6 *line6 = msg->line6;
140 int done = msg->done;
141 int bytes = min(msg->size - done, line6->max_packet_size);
142
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800143 usb_fill_int_urb(urb, line6->usbdev,
Chris Rorvick9e165be2015-01-12 12:42:53 -0800144 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
145 (char *)msg->buffer + done, bytes,
146 line6_async_request_sent, msg, line6->interval);
Markus Grabner705ecec2009-02-27 19:43:04 -0800147
Markus Grabner705ecec2009-02-27 19:43:04 -0800148 msg->done += bytes;
149 retval = usb_submit_urb(urb, GFP_ATOMIC);
150
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800151 if (retval < 0) {
152 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
153 __func__, retval);
Markus Grabner705ecec2009-02-27 19:43:04 -0800154 usb_free_urb(urb);
155 kfree(msg);
Ashvini Varatharaj056e0af2013-10-19 22:01:22 +0530156 return retval;
Markus Grabner705ecec2009-02-27 19:43:04 -0800157 }
158
159 return 0;
160}
161
162/*
Markus Grabner1027f472010-08-12 01:35:30 +0200163 Setup and start timer.
164*/
Nicholas Mc Guire6ccd93b2015-02-03 02:38:56 -0500165void line6_start_timer(struct timer_list *timer, unsigned long msecs,
Monam Agarwal1cad6082014-02-27 21:05:18 +0530166 void (*function)(unsigned long), unsigned long data)
Markus Grabner1027f472010-08-12 01:35:30 +0200167{
168 setup_timer(timer, function, data);
Nicholas Mc Guire695758c2015-02-03 02:38:39 -0500169 mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
Markus Grabner1027f472010-08-12 01:35:30 +0200170}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100171EXPORT_SYMBOL_GPL(line6_start_timer);
Markus Grabner1027f472010-08-12 01:35:30 +0200172
173/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800174 Asynchronously send raw message.
175*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800176int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
177 int size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800178{
179 struct message *msg;
180 struct urb *urb;
181
182 /* create message: */
183 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
Joe Perches78110bb2013-02-11 09:41:29 -0800184 if (msg == NULL)
Markus Grabner705ecec2009-02-27 19:43:04 -0800185 return -ENOMEM;
Markus Grabner705ecec2009-02-27 19:43:04 -0800186
187 /* create URB: */
188 urb = usb_alloc_urb(0, GFP_ATOMIC);
189
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800190 if (urb == NULL) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800191 kfree(msg);
Markus Grabner705ecec2009-02-27 19:43:04 -0800192 return -ENOMEM;
193 }
194
195 /* set message data: */
196 msg->line6 = line6;
197 msg->buffer = buffer;
198 msg->size = size;
199 msg->done = 0;
200
201 /* start sending: */
202 return line6_send_raw_message_async_part(msg, urb);
203}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100204EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
Markus Grabner705ecec2009-02-27 19:43:04 -0800205
206/*
Markus Grabner1027f472010-08-12 01:35:30 +0200207 Send asynchronous device version request.
208*/
209int line6_version_request_async(struct usb_line6 *line6)
210{
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -0700211 char *buffer;
212 int retval;
213
Laurent Navet77ecb6f2012-12-03 14:20:25 +0100214 buffer = kmemdup(line6_request_version,
215 sizeof(line6_request_version), GFP_ATOMIC);
Takashi Iwaia019f5e2015-01-19 15:05:10 +0100216 if (buffer == NULL)
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -0700217 return -ENOMEM;
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -0700218
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -0700219 retval = line6_send_raw_message_async(line6, buffer,
220 sizeof(line6_request_version));
221 kfree(buffer);
222 return retval;
Markus Grabner1027f472010-08-12 01:35:30 +0200223}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100224EXPORT_SYMBOL_GPL(line6_version_request_async);
Markus Grabner1027f472010-08-12 01:35:30 +0200225
226/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800227 Send sysex message in pieces of wMaxPacketSize bytes.
228*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800229int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
230 int size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800231{
Markus Grabnere1a164d2010-08-23 01:08:25 +0200232 return line6_send_raw_message(line6, buffer,
233 size + SYSEX_EXTRA_SIZE) -
234 SYSEX_EXTRA_SIZE;
Markus Grabner705ecec2009-02-27 19:43:04 -0800235}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100236EXPORT_SYMBOL_GPL(line6_send_sysex_message);
Markus Grabner705ecec2009-02-27 19:43:04 -0800237
238/*
239 Allocate buffer for sysex message and prepare header.
240 @param code sysex message code
241 @param size number of bytes between code and sysex end
242*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800243char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
244 int size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800245{
Markus Grabner1027f472010-08-12 01:35:30 +0200246 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
Markus Grabner705ecec2009-02-27 19:43:04 -0800247
Joe Perches78110bb2013-02-11 09:41:29 -0800248 if (!buffer)
Greg Kroah-Hartman536165d2009-02-27 20:49:46 -0800249 return NULL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800250
251 buffer[0] = LINE6_SYSEX_BEGIN;
252 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
253 buffer[sizeof(line6_midi_id) + 1] = code1;
254 buffer[sizeof(line6_midi_id) + 2] = code2;
255 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
256 return buffer;
257}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100258EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
Markus Grabner705ecec2009-02-27 19:43:04 -0800259
260/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600261 Notification of data received from the Line 6 device.
Markus Grabner705ecec2009-02-27 19:43:04 -0800262*/
Greg Kroah-Hartman0c7ab152009-02-27 20:28:04 -0800263static void line6_data_received(struct urb *urb)
Markus Grabner705ecec2009-02-27 19:43:04 -0800264{
265 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100266 struct midi_buffer *mb = &line6->line6midi->midibuf_in;
Markus Grabner705ecec2009-02-27 19:43:04 -0800267 int done;
268
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800269 if (urb->status == -ESHUTDOWN)
Markus Grabner705ecec2009-02-27 19:43:04 -0800270 return;
271
Markus Grabnere1a164d2010-08-23 01:08:25 +0200272 done =
273 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
Markus Grabner705ecec2009-02-27 19:43:04 -0800274
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800275 if (done < urb->actual_length) {
Markus Grabner1027f472010-08-12 01:35:30 +0200276 line6_midibuf_ignore(mb, done);
Stefan Hajnoczie00d33c2012-11-11 13:52:24 +0100277 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
278 done, urb->actual_length);
Markus Grabner705ecec2009-02-27 19:43:04 -0800279 }
280
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800281 for (;;) {
Markus Grabnere1a164d2010-08-23 01:08:25 +0200282 done =
283 line6_midibuf_read(mb, line6->buffer_message,
284 LINE6_MESSAGE_MAXLEN);
Markus Grabner705ecec2009-02-27 19:43:04 -0800285
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800286 if (done == 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800287 break;
288
Markus Grabner705ecec2009-02-27 19:43:04 -0800289 line6->message_length = done;
Markus Grabner705ecec2009-02-27 19:43:04 -0800290 line6_midi_receive(line6, line6->buffer_message, done);
291
Chris Rorvick01f6b2b2015-01-12 12:42:58 -0800292 if (line6->process_message)
293 line6->process_message(line6);
Markus Grabner705ecec2009-02-27 19:43:04 -0800294 }
295
296 line6_start_listen(line6);
297}
298
Chris Rorvicke64e94d2015-02-10 23:03:13 -0600299#define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
Chris Rorvickf3dfd1b2015-02-10 23:03:14 -0600300#define LINE6_READ_WRITE_MAX_RETRIES 50
Chris Rorvicke64e94d2015-02-10 23:03:13 -0600301
Markus Grabner705ecec2009-02-27 19:43:04 -0800302/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800303 Read data from device.
304*/
Chris Rorvick25a07072015-02-11 06:03:31 -0600305int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
306 unsigned datalen)
Markus Grabner705ecec2009-02-27 19:43:04 -0800307{
308 struct usb_device *usbdev = line6->usbdev;
309 int ret;
310 unsigned char len;
Chris Rorvickf3dfd1b2015-02-10 23:03:14 -0600311 unsigned count;
Markus Grabner705ecec2009-02-27 19:43:04 -0800312
Chris Rorvick25a07072015-02-11 06:03:31 -0600313 if (address > 0xffff || datalen > 0xff)
314 return -EINVAL;
315
Markus Grabner705ecec2009-02-27 19:43:04 -0800316 /* query the serial number: */
317 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
Markus Grabner1027f472010-08-12 01:35:30 +0200318 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
319 (datalen << 8) | 0x21, address,
320 NULL, 0, LINE6_TIMEOUT * HZ);
Markus Grabner705ecec2009-02-27 19:43:04 -0800321
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800322 if (ret < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800323 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
324 return ret;
325 }
326
Stefan Hajnoczibc776f22013-01-11 23:08:10 +0100327 /* Wait for data length. We'll get 0xff until length arrives. */
Chris Rorvickf3dfd1b2015-02-10 23:03:14 -0600328 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
Chris Rorvicke64e94d2015-02-10 23:03:13 -0600329 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
330
Markus Grabner705ecec2009-02-27 19:43:04 -0800331 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800332 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
333 USB_DIR_IN,
334 0x0012, 0x0000, &len, 1,
335 LINE6_TIMEOUT * HZ);
336 if (ret < 0) {
337 dev_err(line6->ifcdev,
338 "receive length failed (error %d)\n", ret);
Markus Grabner705ecec2009-02-27 19:43:04 -0800339 return ret;
340 }
Markus Grabner705ecec2009-02-27 19:43:04 -0800341
Chris Rorvickf3dfd1b2015-02-10 23:03:14 -0600342 if (len != 0xff)
343 break;
344 }
345
346 if (len == 0xff) {
347 dev_err(line6->ifcdev, "read failed after %d retries\n",
348 count);
349 return -EIO;
350 } else if (len != datalen) {
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800351 /* should be equal or something went wrong */
352 dev_err(line6->ifcdev,
353 "length mismatch (expected %d, got %d)\n",
354 (int)datalen, (int)len);
Chris Rorvicke474e7f2015-02-10 23:03:15 -0600355 return -EIO;
Markus Grabner705ecec2009-02-27 19:43:04 -0800356 }
357
358 /* receive the result: */
359 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800360 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
361 0x0013, 0x0000, data, datalen,
362 LINE6_TIMEOUT * HZ);
Markus Grabner705ecec2009-02-27 19:43:04 -0800363
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800364 if (ret < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800365 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
366 return ret;
367 }
368
369 return 0;
370}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100371EXPORT_SYMBOL_GPL(line6_read_data);
Markus Grabner705ecec2009-02-27 19:43:04 -0800372
373/*
374 Write data to device.
375*/
Chris Rorvick25a07072015-02-11 06:03:31 -0600376int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
377 unsigned datalen)
Markus Grabner705ecec2009-02-27 19:43:04 -0800378{
379 struct usb_device *usbdev = line6->usbdev;
380 int ret;
381 unsigned char status;
Chris Rorvickf3dfd1b2015-02-10 23:03:14 -0600382 int count;
Markus Grabner705ecec2009-02-27 19:43:04 -0800383
Chris Rorvick25a07072015-02-11 06:03:31 -0600384 if (address > 0xffff || datalen > 0xffff)
385 return -EINVAL;
386
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800387 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
388 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
389 0x0022, address, data, datalen,
390 LINE6_TIMEOUT * HZ);
Markus Grabner705ecec2009-02-27 19:43:04 -0800391
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800392 if (ret < 0) {
393 dev_err(line6->ifcdev,
394 "write request failed (error %d)\n", ret);
Markus Grabner705ecec2009-02-27 19:43:04 -0800395 return ret;
396 }
397
Chris Rorvickf3dfd1b2015-02-10 23:03:14 -0600398 for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
Chris Rorvicke64e94d2015-02-10 23:03:13 -0600399 mdelay(LINE6_READ_WRITE_STATUS_DELAY);
400
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800401 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
402 0x67,
403 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
404 USB_DIR_IN,
405 0x0012, 0x0000,
406 &status, 1, LINE6_TIMEOUT * HZ);
Markus Grabner705ecec2009-02-27 19:43:04 -0800407
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800408 if (ret < 0) {
409 dev_err(line6->ifcdev,
410 "receiving status failed (error %d)\n", ret);
Markus Grabner705ecec2009-02-27 19:43:04 -0800411 return ret;
412 }
Markus Grabner705ecec2009-02-27 19:43:04 -0800413
Chris Rorvickf3dfd1b2015-02-10 23:03:14 -0600414 if (status != 0xff)
415 break;
416 }
417
418 if (status == 0xff) {
419 dev_err(line6->ifcdev, "write failed after %d retries\n",
420 count);
421 return -EIO;
422 } else if (status != 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800423 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
Chris Rorvicke474e7f2015-02-10 23:03:15 -0600424 return -EIO;
Markus Grabner705ecec2009-02-27 19:43:04 -0800425 }
426
427 return 0;
428}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100429EXPORT_SYMBOL_GPL(line6_write_data);
Markus Grabner705ecec2009-02-27 19:43:04 -0800430
431/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600432 Read Line 6 device serial number.
Markus Grabner705ecec2009-02-27 19:43:04 -0800433 (POD, TonePort, GuitarPort)
434*/
Chris Rorvick12b00152015-02-10 23:03:16 -0600435int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
Markus Grabner705ecec2009-02-27 19:43:04 -0800436{
Markus Grabnere1a164d2010-08-23 01:08:25 +0200437 return line6_read_data(line6, 0x80d0, serial_number,
438 sizeof(*serial_number));
Markus Grabner705ecec2009-02-27 19:43:04 -0800439}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100440EXPORT_SYMBOL_GPL(line6_read_serial_number);
Markus Grabner705ecec2009-02-27 19:43:04 -0800441
442/*
Takashi Iwai85a93392015-01-19 15:54:00 +0100443 Card destructor.
Markus Grabner705ecec2009-02-27 19:43:04 -0800444*/
Takashi Iwai85a93392015-01-19 15:54:00 +0100445static void line6_destruct(struct snd_card *card)
Markus Grabner705ecec2009-02-27 19:43:04 -0800446{
Takashi Iwai85a93392015-01-19 15:54:00 +0100447 struct usb_line6 *line6 = card->private_data;
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100448 struct usb_device *usbdev = line6->usbdev;
Markus Grabner705ecec2009-02-27 19:43:04 -0800449
450 /* free buffer memory first: */
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800451 kfree(line6->buffer_message);
452 kfree(line6->buffer_listen);
Markus Grabner705ecec2009-02-27 19:43:04 -0800453
454 /* then free URBs: */
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800455 usb_free_urb(line6->urb_listen);
Markus Grabner705ecec2009-02-27 19:43:04 -0800456
Takashi Iwai85a93392015-01-19 15:54:00 +0100457 /* decrement reference counters: */
458 usb_put_dev(usbdev);
Markus Grabner705ecec2009-02-27 19:43:04 -0800459}
460
Takashi Iwai644d9082015-01-23 12:24:03 +0100461/* get data from endpoint descriptor (see usb_maxpacket): */
462static void line6_get_interval(struct usb_line6 *line6)
463{
464 struct usb_device *usbdev = line6->usbdev;
465 struct usb_host_endpoint *ep;
466 unsigned pipe = usb_rcvintpipe(usbdev, line6->properties->ep_ctrl_r);
467 unsigned epnum = usb_pipeendpoint(pipe);
468
469 ep = usbdev->ep_in[epnum];
470 if (ep) {
471 line6->interval = ep->desc.bInterval;
472 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
473 } else {
474 dev_err(line6->ifcdev,
475 "endpoint not available, using fallback values");
476 line6->interval = LINE6_FALLBACK_INTERVAL;
477 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
478 }
479}
480
481static int line6_init_cap_control(struct usb_line6 *line6)
482{
483 int ret;
484
485 /* initialize USB buffers: */
486 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
487 if (!line6->buffer_listen)
488 return -ENOMEM;
489
490 line6->buffer_message = kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
491 if (!line6->buffer_message)
492 return -ENOMEM;
493
494 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
495 if (!line6->urb_listen)
496 return -ENOMEM;
497
498 ret = line6_start_listen(line6);
499 if (ret < 0) {
500 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
501 return ret;
502 }
503
504 return 0;
505}
506
Markus Grabner705ecec2009-02-27 19:43:04 -0800507/*
508 Probe USB device.
509*/
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100510int line6_probe(struct usb_interface *interface,
Takashi Iwaif66fd992015-01-25 18:22:58 +0100511 const struct usb_device_id *id,
Chris Rorvick12865ca2015-02-07 10:43:19 -0600512 const char *driver_name,
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100513 const struct line6_properties *properties,
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100514 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
515 size_t data_size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800516{
Chris Rorvick35ae48a2015-01-20 02:20:48 -0600517 struct usb_device *usbdev = interface_to_usbdev(interface);
Takashi Iwai85a93392015-01-19 15:54:00 +0100518 struct snd_card *card;
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100519 struct usb_line6 *line6;
Chris Rorvick7b9584f2015-01-12 12:42:52 -0800520 int interface_number;
Markus Grabner705ecec2009-02-27 19:43:04 -0800521 int ret;
522
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100523 if (WARN_ON(data_size < sizeof(*line6)))
524 return -EINVAL;
525
Takashi Iwaid6ca69d2015-01-25 18:41:26 +0100526 /* we don't handle multiple configurations */
527 if (usbdev->descriptor.bNumConfigurations != 1)
528 return -ENODEV;
529
Takashi Iwai6b562f62015-01-23 12:27:39 +0100530 ret = snd_card_new(&interface->dev,
531 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100532 THIS_MODULE, data_size, &card);
533 if (ret < 0)
Takashi Iwai6b562f62015-01-23 12:27:39 +0100534 return ret;
Markus Grabner705ecec2009-02-27 19:43:04 -0800535
Markus Grabner705ecec2009-02-27 19:43:04 -0800536 /* store basic data: */
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100537 line6 = card->private_data;
Takashi Iwai6b562f62015-01-23 12:27:39 +0100538 line6->card = card;
Markus Grabner705ecec2009-02-27 19:43:04 -0800539 line6->properties = properties;
540 line6->usbdev = usbdev;
541 line6->ifcdev = &interface->dev;
Markus Grabner705ecec2009-02-27 19:43:04 -0800542
Takashi Iwaid6ca69d2015-01-25 18:41:26 +0100543 strcpy(card->id, properties->id);
Chris Rorvick12865ca2015-02-07 10:43:19 -0600544 strcpy(card->driver, driver_name);
Takashi Iwaid6ca69d2015-01-25 18:41:26 +0100545 strcpy(card->shortname, properties->name);
546 sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
Takashi Iwai85a93392015-01-19 15:54:00 +0100547 dev_name(line6->ifcdev));
Takashi Iwai85a93392015-01-19 15:54:00 +0100548 card->private_free = line6_destruct;
549
Markus Grabner705ecec2009-02-27 19:43:04 -0800550 usb_set_intfdata(interface, line6);
551
Takashi Iwai85a93392015-01-19 15:54:00 +0100552 /* increment reference counters: */
553 usb_get_dev(usbdev);
554
Takashi Iwai6b562f62015-01-23 12:27:39 +0100555 /* initialize device info: */
556 dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
557
558 /* query interface number */
559 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
560
561 ret = usb_set_interface(usbdev, interface_number,
Takashi Iwaid6ca69d2015-01-25 18:41:26 +0100562 properties->altsetting);
Takashi Iwai6b562f62015-01-23 12:27:39 +0100563 if (ret < 0) {
564 dev_err(&interface->dev, "set_interface failed\n");
565 goto error;
566 }
567
568 line6_get_interval(line6);
569
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800570 if (properties->capabilities & LINE6_CAP_CONTROL) {
Takashi Iwai644d9082015-01-23 12:24:03 +0100571 ret = line6_init_cap_control(line6);
572 if (ret < 0)
Takashi Iwai6b562f62015-01-23 12:27:39 +0100573 goto error;
Markus Grabner705ecec2009-02-27 19:43:04 -0800574 }
575
Chris Rorvicka23a8bf2015-01-12 12:42:42 -0800576 /* initialize device data based on device: */
Takashi Iwaif66fd992015-01-25 18:22:58 +0100577 ret = private_init(line6, id);
Kulikov Vasiliyab366c12010-07-13 15:22:46 +0400578 if (ret < 0)
Takashi Iwai6b562f62015-01-23 12:27:39 +0100579 goto error;
Markus Grabner705ecec2009-02-27 19:43:04 -0800580
Markus Grabner1027f472010-08-12 01:35:30 +0200581 /* creation of additional special files should go here */
582
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600583 dev_info(&interface->dev, "Line 6 %s now attached\n",
Takashi Iwaid6ca69d2015-01-25 18:41:26 +0100584 properties->name);
Markus Grabner1027f472010-08-12 01:35:30 +0200585
Kulikov Vasiliyab366c12010-07-13 15:22:46 +0400586 return 0;
587
Takashi Iwai6b562f62015-01-23 12:27:39 +0100588 error:
Takashi Iwaieedd0e92015-01-20 09:09:27 +0100589 if (line6->disconnect)
Takashi Iwaif66fd992015-01-25 18:22:58 +0100590 line6->disconnect(line6);
Takashi Iwai85a93392015-01-19 15:54:00 +0100591 snd_card_free(card);
Markus Grabner705ecec2009-02-27 19:43:04 -0800592 return ret;
593}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100594EXPORT_SYMBOL_GPL(line6_probe);
Markus Grabner705ecec2009-02-27 19:43:04 -0800595
596/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600597 Line 6 device disconnected.
Markus Grabner705ecec2009-02-27 19:43:04 -0800598*/
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100599void line6_disconnect(struct usb_interface *interface)
Markus Grabner705ecec2009-02-27 19:43:04 -0800600{
Takashi Iwai270fd9c2015-01-20 08:40:51 +0100601 struct usb_line6 *line6 = usb_get_intfdata(interface);
602 struct usb_device *usbdev = interface_to_usbdev(interface);
Markus Grabner705ecec2009-02-27 19:43:04 -0800603
Takashi Iwai85a93392015-01-19 15:54:00 +0100604 if (!line6)
605 return;
Markus Grabner705ecec2009-02-27 19:43:04 -0800606
Takashi Iwai2a324fc2015-01-20 09:58:54 +0100607 if (WARN_ON(usbdev != line6->usbdev))
608 return;
609
Takashi Iwai85a93392015-01-19 15:54:00 +0100610 if (line6->urb_listen != NULL)
611 line6_stop_listen(line6);
Markus Grabner705ecec2009-02-27 19:43:04 -0800612
Takashi Iwai85a93392015-01-19 15:54:00 +0100613 snd_card_disconnect(line6->card);
Takashi Iwai5a475312015-01-19 16:15:54 +0100614 if (line6->line6pcm)
615 line6_pcm_disconnect(line6->line6pcm);
Takashi Iwai85a93392015-01-19 15:54:00 +0100616 if (line6->disconnect)
Takashi Iwaif66fd992015-01-25 18:22:58 +0100617 line6->disconnect(line6);
Markus Grabner705ecec2009-02-27 19:43:04 -0800618
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600619 dev_info(&interface->dev, "Line 6 %s now disconnected\n",
Takashi Iwai85a93392015-01-19 15:54:00 +0100620 line6->properties->name);
Markus Grabner705ecec2009-02-27 19:43:04 -0800621
Takashi Iwai85a93392015-01-19 15:54:00 +0100622 /* make sure the device isn't destructed twice: */
623 usb_set_intfdata(interface, NULL);
Markus Grabner705ecec2009-02-27 19:43:04 -0800624
Takashi Iwai85a93392015-01-19 15:54:00 +0100625 snd_card_free_when_closed(line6->card);
Markus Grabner705ecec2009-02-27 19:43:04 -0800626}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100627EXPORT_SYMBOL_GPL(line6_disconnect);
Markus Grabner705ecec2009-02-27 19:43:04 -0800628
Markus Grabner1027f472010-08-12 01:35:30 +0200629#ifdef CONFIG_PM
630
631/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600632 Suspend Line 6 device.
Markus Grabner1027f472010-08-12 01:35:30 +0200633*/
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100634int line6_suspend(struct usb_interface *interface, pm_message_t message)
Markus Grabner1027f472010-08-12 01:35:30 +0200635{
636 struct usb_line6 *line6 = usb_get_intfdata(interface);
637 struct snd_line6_pcm *line6pcm = line6->line6pcm;
638
639 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
640
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800641 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
Markus Grabner1027f472010-08-12 01:35:30 +0200642 line6_stop_listen(line6);
643
644 if (line6pcm != NULL) {
645 snd_pcm_suspend_all(line6pcm->pcm);
Markus Grabner1027f472010-08-12 01:35:30 +0200646 line6pcm->flags = 0;
647 }
648
649 return 0;
650}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100651EXPORT_SYMBOL_GPL(line6_suspend);
Markus Grabner1027f472010-08-12 01:35:30 +0200652
653/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600654 Resume Line 6 device.
Markus Grabner1027f472010-08-12 01:35:30 +0200655*/
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100656int line6_resume(struct usb_interface *interface)
Markus Grabner1027f472010-08-12 01:35:30 +0200657{
658 struct usb_line6 *line6 = usb_get_intfdata(interface);
659
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800660 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
Markus Grabner1027f472010-08-12 01:35:30 +0200661 line6_start_listen(line6);
662
663 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
664 return 0;
665}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100666EXPORT_SYMBOL_GPL(line6_resume);
Markus Grabner1027f472010-08-12 01:35:30 +0200667
Markus Grabnere1a164d2010-08-23 01:08:25 +0200668#endif /* CONFIG_PM */
Markus Grabner1027f472010-08-12 01:35:30 +0200669
Markus Grabner705ecec2009-02-27 19:43:04 -0800670MODULE_AUTHOR(DRIVER_AUTHOR);
671MODULE_DESCRIPTION(DRIVER_DESC);
672MODULE_LICENSE("GPL");