blob: c696f9773cbbefdfa37c35c4ad3446a13c905da3 [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#include "revision.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080026#include "usbdefs.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080027
Markus Grabner705ecec2009-02-27 19:43:04 -080028#define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
Chris Rorvickc6fffce2015-01-20 02:20:49 -060029#define DRIVER_DESC "Line 6 USB Driver"
Markus Grabner705ecec2009-02-27 19:43:04 -080030
Markus Grabner705ecec2009-02-27 19:43:04 -080031/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -060032 This is Line 6's MIDI manufacturer ID.
Markus Grabner705ecec2009-02-27 19:43:04 -080033*/
Markus Grabner1027f472010-08-12 01:35:30 +020034const unsigned char line6_midi_id[] = {
35 0x00, 0x01, 0x0c
36};
Takashi Iwaiccddbe42015-01-15 08:22:31 +010037EXPORT_SYMBOL_GPL(line6_midi_id);
Markus Grabner1027f472010-08-12 01:35:30 +020038
39/*
40 Code to request version of POD, Variax interface
41 (and maybe other devices).
42*/
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -070043static const char line6_request_version[] = {
Markus Grabner1027f472010-08-12 01:35:30 +020044 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
45};
46
Markus Grabner705ecec2009-02-27 19:43:04 -080047/**
48 Class for asynchronous messages.
49*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -080050struct message {
Markus Grabner705ecec2009-02-27 19:43:04 -080051 struct usb_line6 *line6;
52 const char *buffer;
53 int size;
54 int done;
55};
56
Markus Grabner705ecec2009-02-27 19:43:04 -080057/*
58 Forward declarations.
59*/
Greg Kroah-Hartman0c7ab152009-02-27 20:28:04 -080060static void line6_data_received(struct urb *urb);
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -080061static int line6_send_raw_message_async_part(struct message *msg,
62 struct urb *urb);
Markus Grabner705ecec2009-02-27 19:43:04 -080063
Markus Grabner705ecec2009-02-27 19:43:04 -080064/*
65 Start to listen on endpoint.
66*/
67static int line6_start_listen(struct usb_line6 *line6)
68{
Markus Grabner1027f472010-08-12 01:35:30 +020069 int err;
Fabian Mewesa6b46992014-03-24 23:46:31 +010070
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -080071 usb_fill_int_urb(line6->urb_listen, line6->usbdev,
Chris Rorvick9e165be2015-01-12 12:42:53 -080072 usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
73 line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
74 line6_data_received, line6, line6->interval);
Markus Grabner705ecec2009-02-27 19:43:04 -080075 line6->urb_listen->actual_length = 0;
Markus Grabnere1a164d2010-08-23 01:08:25 +020076 err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
Markus Grabner1027f472010-08-12 01:35:30 +020077 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -080078}
79
Markus Grabner1027f472010-08-12 01:35:30 +020080/*
81 Stop listening on endpoint.
82*/
83static void line6_stop_listen(struct usb_line6 *line6)
84{
85 usb_kill_urb(line6->urb_listen);
86}
87
Markus Grabner705ecec2009-02-27 19:43:04 -080088/*
Markus Grabner1027f472010-08-12 01:35:30 +020089 Send raw message in pieces of wMaxPacketSize bytes.
Markus Grabner705ecec2009-02-27 19:43:04 -080090*/
Takashi Iwai73723192015-01-19 16:36:21 +010091static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
92 int size)
Markus Grabner705ecec2009-02-27 19:43:04 -080093{
94 int i, done = 0;
95
Markus Grabner1027f472010-08-12 01:35:30 +020096 for (i = 0; i < size; i += line6->max_packet_size) {
97 int partial;
Markus Grabner705ecec2009-02-27 19:43:04 -080098 const char *frag_buf = buffer + i;
99 int frag_size = min(line6->max_packet_size, size - i);
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800100 int retval;
Markus Grabner705ecec2009-02-27 19:43:04 -0800101
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800102 retval = usb_interrupt_msg(line6->usbdev,
Davide Berardiba79e522014-03-17 03:34:41 +0100103 usb_sndintpipe(line6->usbdev,
Chris Rorvick9e165be2015-01-12 12:42:53 -0800104 line6->properties->ep_ctrl_w),
Davide Berardiba79e522014-03-17 03:34:41 +0100105 (char *)frag_buf, frag_size,
106 &partial, LINE6_TIMEOUT * HZ);
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800107
108 if (retval) {
109 dev_err(line6->ifcdev,
110 "usb_interrupt_msg failed (%d)\n", retval);
Markus Grabner705ecec2009-02-27 19:43:04 -0800111 break;
112 }
113
Markus Grabner1027f472010-08-12 01:35:30 +0200114 done += frag_size;
Markus Grabner705ecec2009-02-27 19:43:04 -0800115 }
116
117 return done;
118}
119
120/*
121 Notification of completion of asynchronous request transmission.
122*/
Greg Kroah-Hartman0c7ab152009-02-27 20:28:04 -0800123static void line6_async_request_sent(struct urb *urb)
Markus Grabner705ecec2009-02-27 19:43:04 -0800124{
125 struct message *msg = (struct message *)urb->context;
126
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800127 if (msg->done >= msg->size) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800128 usb_free_urb(urb);
129 kfree(msg);
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800130 } else
Markus Grabner705ecec2009-02-27 19:43:04 -0800131 line6_send_raw_message_async_part(msg, urb);
132}
133
134/*
135 Asynchronously send part of a raw message.
136*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800137static int line6_send_raw_message_async_part(struct message *msg,
138 struct urb *urb)
Markus Grabner705ecec2009-02-27 19:43:04 -0800139{
140 int retval;
141 struct usb_line6 *line6 = msg->line6;
142 int done = msg->done;
143 int bytes = min(msg->size - done, line6->max_packet_size);
144
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800145 usb_fill_int_urb(urb, line6->usbdev,
Chris Rorvick9e165be2015-01-12 12:42:53 -0800146 usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
147 (char *)msg->buffer + done, bytes,
148 line6_async_request_sent, msg, line6->interval);
Markus Grabner705ecec2009-02-27 19:43:04 -0800149
Markus Grabner705ecec2009-02-27 19:43:04 -0800150 msg->done += bytes;
151 retval = usb_submit_urb(urb, GFP_ATOMIC);
152
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800153 if (retval < 0) {
154 dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
155 __func__, retval);
Markus Grabner705ecec2009-02-27 19:43:04 -0800156 usb_free_urb(urb);
157 kfree(msg);
Ashvini Varatharaj056e0af2013-10-19 22:01:22 +0530158 return retval;
Markus Grabner705ecec2009-02-27 19:43:04 -0800159 }
160
161 return 0;
162}
163
164/*
Markus Grabner1027f472010-08-12 01:35:30 +0200165 Setup and start timer.
166*/
167void line6_start_timer(struct timer_list *timer, unsigned int msecs,
Monam Agarwal1cad6082014-02-27 21:05:18 +0530168 void (*function)(unsigned long), unsigned long data)
Markus Grabner1027f472010-08-12 01:35:30 +0200169{
170 setup_timer(timer, function, data);
Takashi Iwai0f2524b2015-01-19 11:44:40 +0100171 mod_timer(timer, jiffies + msecs * HZ / 1000);
Markus Grabner1027f472010-08-12 01:35:30 +0200172}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100173EXPORT_SYMBOL_GPL(line6_start_timer);
Markus Grabner1027f472010-08-12 01:35:30 +0200174
175/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800176 Asynchronously send raw message.
177*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800178int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
179 int size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800180{
181 struct message *msg;
182 struct urb *urb;
183
184 /* create message: */
185 msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
Joe Perches78110bb2013-02-11 09:41:29 -0800186 if (msg == NULL)
Markus Grabner705ecec2009-02-27 19:43:04 -0800187 return -ENOMEM;
Markus Grabner705ecec2009-02-27 19:43:04 -0800188
189 /* create URB: */
190 urb = usb_alloc_urb(0, GFP_ATOMIC);
191
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800192 if (urb == NULL) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800193 kfree(msg);
Markus Grabner705ecec2009-02-27 19:43:04 -0800194 return -ENOMEM;
195 }
196
197 /* set message data: */
198 msg->line6 = line6;
199 msg->buffer = buffer;
200 msg->size = size;
201 msg->done = 0;
202
203 /* start sending: */
204 return line6_send_raw_message_async_part(msg, urb);
205}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100206EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
Markus Grabner705ecec2009-02-27 19:43:04 -0800207
208/*
Markus Grabner1027f472010-08-12 01:35:30 +0200209 Send asynchronous device version request.
210*/
211int line6_version_request_async(struct usb_line6 *line6)
212{
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -0700213 char *buffer;
214 int retval;
215
Laurent Navet77ecb6f2012-12-03 14:20:25 +0100216 buffer = kmemdup(line6_request_version,
217 sizeof(line6_request_version), GFP_ATOMIC);
Takashi Iwaia019f5e2015-01-19 15:05:10 +0100218 if (buffer == NULL)
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -0700219 return -ENOMEM;
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -0700220
Greg Kroah-Hartmanc46b8a62012-04-23 16:09:56 -0700221 retval = line6_send_raw_message_async(line6, buffer,
222 sizeof(line6_request_version));
223 kfree(buffer);
224 return retval;
Markus Grabner1027f472010-08-12 01:35:30 +0200225}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100226EXPORT_SYMBOL_GPL(line6_version_request_async);
Markus Grabner1027f472010-08-12 01:35:30 +0200227
228/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800229 Send sysex message in pieces of wMaxPacketSize bytes.
230*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800231int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
232 int size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800233{
Markus Grabnere1a164d2010-08-23 01:08:25 +0200234 return line6_send_raw_message(line6, buffer,
235 size + SYSEX_EXTRA_SIZE) -
236 SYSEX_EXTRA_SIZE;
Markus Grabner705ecec2009-02-27 19:43:04 -0800237}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100238EXPORT_SYMBOL_GPL(line6_send_sysex_message);
Markus Grabner705ecec2009-02-27 19:43:04 -0800239
240/*
241 Allocate buffer for sysex message and prepare header.
242 @param code sysex message code
243 @param size number of bytes between code and sysex end
244*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800245char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
246 int size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800247{
Markus Grabner1027f472010-08-12 01:35:30 +0200248 char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
Markus Grabner705ecec2009-02-27 19:43:04 -0800249
Joe Perches78110bb2013-02-11 09:41:29 -0800250 if (!buffer)
Greg Kroah-Hartman536165d2009-02-27 20:49:46 -0800251 return NULL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800252
253 buffer[0] = LINE6_SYSEX_BEGIN;
254 memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
255 buffer[sizeof(line6_midi_id) + 1] = code1;
256 buffer[sizeof(line6_midi_id) + 2] = code2;
257 buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
258 return buffer;
259}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100260EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
Markus Grabner705ecec2009-02-27 19:43:04 -0800261
262/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600263 Notification of data received from the Line 6 device.
Markus Grabner705ecec2009-02-27 19:43:04 -0800264*/
Greg Kroah-Hartman0c7ab152009-02-27 20:28:04 -0800265static void line6_data_received(struct urb *urb)
Markus Grabner705ecec2009-02-27 19:43:04 -0800266{
267 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100268 struct midi_buffer *mb = &line6->line6midi->midibuf_in;
Markus Grabner705ecec2009-02-27 19:43:04 -0800269 int done;
270
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800271 if (urb->status == -ESHUTDOWN)
Markus Grabner705ecec2009-02-27 19:43:04 -0800272 return;
273
Markus Grabnere1a164d2010-08-23 01:08:25 +0200274 done =
275 line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
Markus Grabner705ecec2009-02-27 19:43:04 -0800276
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800277 if (done < urb->actual_length) {
Markus Grabner1027f472010-08-12 01:35:30 +0200278 line6_midibuf_ignore(mb, done);
Stefan Hajnoczie00d33c2012-11-11 13:52:24 +0100279 dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
280 done, urb->actual_length);
Markus Grabner705ecec2009-02-27 19:43:04 -0800281 }
282
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800283 for (;;) {
Markus Grabnere1a164d2010-08-23 01:08:25 +0200284 done =
285 line6_midibuf_read(mb, line6->buffer_message,
286 LINE6_MESSAGE_MAXLEN);
Markus Grabner705ecec2009-02-27 19:43:04 -0800287
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800288 if (done == 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800289 break;
290
Markus Grabner705ecec2009-02-27 19:43:04 -0800291 line6->message_length = done;
Markus Grabner705ecec2009-02-27 19:43:04 -0800292 line6_midi_receive(line6, line6->buffer_message, done);
293
Chris Rorvick01f6b2b2015-01-12 12:42:58 -0800294 if (line6->process_message)
295 line6->process_message(line6);
Markus Grabner705ecec2009-02-27 19:43:04 -0800296 }
297
298 line6_start_listen(line6);
299}
300
301/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800302 Read data from device.
303*/
Markus Grabnere1a164d2010-08-23 01:08:25 +0200304int line6_read_data(struct usb_line6 *line6, int address, void *data,
305 size_t datalen)
Markus Grabner705ecec2009-02-27 19:43:04 -0800306{
307 struct usb_device *usbdev = line6->usbdev;
308 int ret;
309 unsigned char len;
310
311 /* query the serial number: */
312 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
Markus Grabner1027f472010-08-12 01:35:30 +0200313 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
314 (datalen << 8) | 0x21, address,
315 NULL, 0, LINE6_TIMEOUT * HZ);
Markus Grabner705ecec2009-02-27 19:43:04 -0800316
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800317 if (ret < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800318 dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
319 return ret;
320 }
321
Stefan Hajnoczibc776f22013-01-11 23:08:10 +0100322 /* Wait for data length. We'll get 0xff until length arrives. */
Markus Grabner705ecec2009-02-27 19:43:04 -0800323 do {
324 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800325 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
326 USB_DIR_IN,
327 0x0012, 0x0000, &len, 1,
328 LINE6_TIMEOUT * HZ);
329 if (ret < 0) {
330 dev_err(line6->ifcdev,
331 "receive length failed (error %d)\n", ret);
Markus Grabner705ecec2009-02-27 19:43:04 -0800332 return ret;
333 }
Frederik Deweerdtd722a512009-09-15 09:29:43 +0000334 } while (len == 0xff);
Markus Grabner705ecec2009-02-27 19:43:04 -0800335
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800336 if (len != datalen) {
337 /* should be equal or something went wrong */
338 dev_err(line6->ifcdev,
339 "length mismatch (expected %d, got %d)\n",
340 (int)datalen, (int)len);
Markus Grabner705ecec2009-02-27 19:43:04 -0800341 return -EINVAL;
342 }
343
344 /* receive the result: */
345 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800346 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
347 0x0013, 0x0000, data, datalen,
348 LINE6_TIMEOUT * HZ);
Markus Grabner705ecec2009-02-27 19:43:04 -0800349
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800350 if (ret < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800351 dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
352 return ret;
353 }
354
355 return 0;
356}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100357EXPORT_SYMBOL_GPL(line6_read_data);
Markus Grabner705ecec2009-02-27 19:43:04 -0800358
359/*
360 Write data to device.
361*/
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800362int line6_write_data(struct usb_line6 *line6, int address, void *data,
363 size_t datalen)
Markus Grabner705ecec2009-02-27 19:43:04 -0800364{
365 struct usb_device *usbdev = line6->usbdev;
366 int ret;
367 unsigned char status;
368
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800369 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
370 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
371 0x0022, address, data, datalen,
372 LINE6_TIMEOUT * HZ);
Markus Grabner705ecec2009-02-27 19:43:04 -0800373
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800374 if (ret < 0) {
375 dev_err(line6->ifcdev,
376 "write request failed (error %d)\n", ret);
Markus Grabner705ecec2009-02-27 19:43:04 -0800377 return ret;
378 }
379
380 do {
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800381 ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
382 0x67,
383 USB_TYPE_VENDOR | USB_RECIP_DEVICE |
384 USB_DIR_IN,
385 0x0012, 0x0000,
386 &status, 1, LINE6_TIMEOUT * HZ);
Markus Grabner705ecec2009-02-27 19:43:04 -0800387
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800388 if (ret < 0) {
389 dev_err(line6->ifcdev,
390 "receiving status failed (error %d)\n", ret);
Markus Grabner705ecec2009-02-27 19:43:04 -0800391 return ret;
392 }
Greg Kroah-Hartman027360c2010-09-21 16:58:00 -0700393 } while (status == 0xff);
Markus Grabner705ecec2009-02-27 19:43:04 -0800394
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800395 if (status != 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800396 dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
397 return -EINVAL;
398 }
399
400 return 0;
401}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100402EXPORT_SYMBOL_GPL(line6_write_data);
Markus Grabner705ecec2009-02-27 19:43:04 -0800403
404/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600405 Read Line 6 device serial number.
Markus Grabner705ecec2009-02-27 19:43:04 -0800406 (POD, TonePort, GuitarPort)
407*/
408int line6_read_serial_number(struct usb_line6 *line6, int *serial_number)
409{
Markus Grabnere1a164d2010-08-23 01:08:25 +0200410 return line6_read_data(line6, 0x80d0, serial_number,
411 sizeof(*serial_number));
Markus Grabner705ecec2009-02-27 19:43:04 -0800412}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100413EXPORT_SYMBOL_GPL(line6_read_serial_number);
Markus Grabner705ecec2009-02-27 19:43:04 -0800414
415/*
Takashi Iwai85a93392015-01-19 15:54:00 +0100416 Card destructor.
Markus Grabner705ecec2009-02-27 19:43:04 -0800417*/
Takashi Iwai85a93392015-01-19 15:54:00 +0100418static void line6_destruct(struct snd_card *card)
Markus Grabner705ecec2009-02-27 19:43:04 -0800419{
Takashi Iwai85a93392015-01-19 15:54:00 +0100420 struct usb_line6 *line6 = card->private_data;
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100421 struct usb_device *usbdev = line6->usbdev;
Markus Grabner705ecec2009-02-27 19:43:04 -0800422
423 /* free buffer memory first: */
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800424 kfree(line6->buffer_message);
425 kfree(line6->buffer_listen);
Markus Grabner705ecec2009-02-27 19:43:04 -0800426
427 /* then free URBs: */
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800428 usb_free_urb(line6->urb_listen);
Markus Grabner705ecec2009-02-27 19:43:04 -0800429
Takashi Iwai85a93392015-01-19 15:54:00 +0100430 /* decrement reference counters: */
431 usb_put_dev(usbdev);
Markus Grabner705ecec2009-02-27 19:43:04 -0800432}
433
Takashi Iwai644d9082015-01-23 12:24:03 +0100434/* get data from endpoint descriptor (see usb_maxpacket): */
435static void line6_get_interval(struct usb_line6 *line6)
436{
437 struct usb_device *usbdev = line6->usbdev;
438 struct usb_host_endpoint *ep;
439 unsigned pipe = usb_rcvintpipe(usbdev, line6->properties->ep_ctrl_r);
440 unsigned epnum = usb_pipeendpoint(pipe);
441
442 ep = usbdev->ep_in[epnum];
443 if (ep) {
444 line6->interval = ep->desc.bInterval;
445 line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
446 } else {
447 dev_err(line6->ifcdev,
448 "endpoint not available, using fallback values");
449 line6->interval = LINE6_FALLBACK_INTERVAL;
450 line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
451 }
452}
453
454static int line6_init_cap_control(struct usb_line6 *line6)
455{
456 int ret;
457
458 /* initialize USB buffers: */
459 line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
460 if (!line6->buffer_listen)
461 return -ENOMEM;
462
463 line6->buffer_message = kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
464 if (!line6->buffer_message)
465 return -ENOMEM;
466
467 line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
468 if (!line6->urb_listen)
469 return -ENOMEM;
470
471 ret = line6_start_listen(line6);
472 if (ret < 0) {
473 dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
474 return ret;
475 }
476
477 return 0;
478}
479
Markus Grabner705ecec2009-02-27 19:43:04 -0800480/*
481 Probe USB device.
482*/
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100483int line6_probe(struct usb_interface *interface,
Takashi Iwaif66fd992015-01-25 18:22:58 +0100484 const struct usb_device_id *id,
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100485 const struct line6_properties *properties,
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100486 int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
487 size_t data_size)
Markus Grabner705ecec2009-02-27 19:43:04 -0800488{
Chris Rorvick35ae48a2015-01-20 02:20:48 -0600489 struct usb_device *usbdev = interface_to_usbdev(interface);
Takashi Iwai85a93392015-01-19 15:54:00 +0100490 struct snd_card *card;
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100491 struct usb_line6 *line6;
Chris Rorvick7b9584f2015-01-12 12:42:52 -0800492 int interface_number;
Markus Grabner705ecec2009-02-27 19:43:04 -0800493 int ret;
494
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100495 if (WARN_ON(data_size < sizeof(*line6)))
496 return -EINVAL;
497
Takashi Iwai6b562f62015-01-23 12:27:39 +0100498 ret = snd_card_new(&interface->dev,
499 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100500 THIS_MODULE, data_size, &card);
501 if (ret < 0)
Takashi Iwai6b562f62015-01-23 12:27:39 +0100502 return ret;
Markus Grabner705ecec2009-02-27 19:43:04 -0800503
Markus Grabner705ecec2009-02-27 19:43:04 -0800504 /* store basic data: */
Takashi Iwaiaca514b2015-01-25 18:36:29 +0100505 line6 = card->private_data;
Takashi Iwai6b562f62015-01-23 12:27:39 +0100506 line6->card = card;
Markus Grabner705ecec2009-02-27 19:43:04 -0800507 line6->properties = properties;
508 line6->usbdev = usbdev;
509 line6->ifcdev = &interface->dev;
Markus Grabner705ecec2009-02-27 19:43:04 -0800510
Takashi Iwai85a93392015-01-19 15:54:00 +0100511 strcpy(card->id, line6->properties->id);
512 strcpy(card->driver, DRIVER_NAME);
513 strcpy(card->shortname, line6->properties->name);
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600514 sprintf(card->longname, "Line 6 %s at USB %s", line6->properties->name,
Takashi Iwai85a93392015-01-19 15:54:00 +0100515 dev_name(line6->ifcdev));
Takashi Iwai85a93392015-01-19 15:54:00 +0100516 card->private_free = line6_destruct;
517
Markus Grabner705ecec2009-02-27 19:43:04 -0800518 usb_set_intfdata(interface, line6);
519
Takashi Iwai85a93392015-01-19 15:54:00 +0100520 /* increment reference counters: */
521 usb_get_dev(usbdev);
522
Takashi Iwai6b562f62015-01-23 12:27:39 +0100523 /* we don't handle multiple configurations */
524 if (usbdev->descriptor.bNumConfigurations != 1) {
525 ret = -ENODEV;
526 goto error;
527 }
528
529 /* initialize device info: */
530 dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
531
532 /* query interface number */
533 interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
534
535 ret = usb_set_interface(usbdev, interface_number,
536 properties->altsetting);
537 if (ret < 0) {
538 dev_err(&interface->dev, "set_interface failed\n");
539 goto error;
540 }
541
542 line6_get_interval(line6);
543
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800544 if (properties->capabilities & LINE6_CAP_CONTROL) {
Takashi Iwai644d9082015-01-23 12:24:03 +0100545 ret = line6_init_cap_control(line6);
546 if (ret < 0)
Takashi Iwai6b562f62015-01-23 12:27:39 +0100547 goto error;
Markus Grabner705ecec2009-02-27 19:43:04 -0800548 }
549
Chris Rorvicka23a8bf2015-01-12 12:42:42 -0800550 /* initialize device data based on device: */
Takashi Iwaif66fd992015-01-25 18:22:58 +0100551 ret = private_init(line6, id);
Kulikov Vasiliyab366c12010-07-13 15:22:46 +0400552 if (ret < 0)
Takashi Iwai6b562f62015-01-23 12:27:39 +0100553 goto error;
Markus Grabner705ecec2009-02-27 19:43:04 -0800554
Markus Grabner1027f472010-08-12 01:35:30 +0200555 /* creation of additional special files should go here */
556
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600557 dev_info(&interface->dev, "Line 6 %s now attached\n",
Greg Kroah-Hartman36445bc2009-02-27 22:42:18 -0800558 line6->properties->name);
Markus Grabner1027f472010-08-12 01:35:30 +0200559
Kulikov Vasiliyab366c12010-07-13 15:22:46 +0400560 return 0;
561
Takashi Iwai6b562f62015-01-23 12:27:39 +0100562 error:
Takashi Iwaieedd0e92015-01-20 09:09:27 +0100563 if (line6->disconnect)
Takashi Iwaif66fd992015-01-25 18:22:58 +0100564 line6->disconnect(line6);
Takashi Iwai85a93392015-01-19 15:54:00 +0100565 snd_card_free(card);
Markus Grabner705ecec2009-02-27 19:43:04 -0800566 return ret;
567}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100568EXPORT_SYMBOL_GPL(line6_probe);
Markus Grabner705ecec2009-02-27 19:43:04 -0800569
570/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600571 Line 6 device disconnected.
Markus Grabner705ecec2009-02-27 19:43:04 -0800572*/
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100573void line6_disconnect(struct usb_interface *interface)
Markus Grabner705ecec2009-02-27 19:43:04 -0800574{
Takashi Iwai270fd9c2015-01-20 08:40:51 +0100575 struct usb_line6 *line6 = usb_get_intfdata(interface);
576 struct usb_device *usbdev = interface_to_usbdev(interface);
Markus Grabner705ecec2009-02-27 19:43:04 -0800577
Takashi Iwai85a93392015-01-19 15:54:00 +0100578 if (!line6)
579 return;
Markus Grabner705ecec2009-02-27 19:43:04 -0800580
Takashi Iwai2a324fc2015-01-20 09:58:54 +0100581 if (WARN_ON(usbdev != line6->usbdev))
582 return;
583
Takashi Iwai85a93392015-01-19 15:54:00 +0100584 if (line6->urb_listen != NULL)
585 line6_stop_listen(line6);
Markus Grabner705ecec2009-02-27 19:43:04 -0800586
Takashi Iwai85a93392015-01-19 15:54:00 +0100587 snd_card_disconnect(line6->card);
Takashi Iwai5a475312015-01-19 16:15:54 +0100588 if (line6->line6pcm)
589 line6_pcm_disconnect(line6->line6pcm);
Takashi Iwai85a93392015-01-19 15:54:00 +0100590 if (line6->disconnect)
Takashi Iwaif66fd992015-01-25 18:22:58 +0100591 line6->disconnect(line6);
Markus Grabner705ecec2009-02-27 19:43:04 -0800592
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600593 dev_info(&interface->dev, "Line 6 %s now disconnected\n",
Takashi Iwai85a93392015-01-19 15:54:00 +0100594 line6->properties->name);
Markus Grabner705ecec2009-02-27 19:43:04 -0800595
Takashi Iwai85a93392015-01-19 15:54:00 +0100596 /* make sure the device isn't destructed twice: */
597 usb_set_intfdata(interface, NULL);
Markus Grabner705ecec2009-02-27 19:43:04 -0800598
Takashi Iwai85a93392015-01-19 15:54:00 +0100599 snd_card_free_when_closed(line6->card);
Markus Grabner705ecec2009-02-27 19:43:04 -0800600}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100601EXPORT_SYMBOL_GPL(line6_disconnect);
Markus Grabner705ecec2009-02-27 19:43:04 -0800602
Markus Grabner1027f472010-08-12 01:35:30 +0200603#ifdef CONFIG_PM
604
605/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600606 Suspend Line 6 device.
Markus Grabner1027f472010-08-12 01:35:30 +0200607*/
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100608int line6_suspend(struct usb_interface *interface, pm_message_t message)
Markus Grabner1027f472010-08-12 01:35:30 +0200609{
610 struct usb_line6 *line6 = usb_get_intfdata(interface);
611 struct snd_line6_pcm *line6pcm = line6->line6pcm;
612
613 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
614
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800615 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
Markus Grabner1027f472010-08-12 01:35:30 +0200616 line6_stop_listen(line6);
617
618 if (line6pcm != NULL) {
619 snd_pcm_suspend_all(line6pcm->pcm);
Markus Grabner1027f472010-08-12 01:35:30 +0200620 line6pcm->flags = 0;
621 }
622
623 return 0;
624}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100625EXPORT_SYMBOL_GPL(line6_suspend);
Markus Grabner1027f472010-08-12 01:35:30 +0200626
627/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -0600628 Resume Line 6 device.
Markus Grabner1027f472010-08-12 01:35:30 +0200629*/
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100630int line6_resume(struct usb_interface *interface)
Markus Grabner1027f472010-08-12 01:35:30 +0200631{
632 struct usb_line6 *line6 = usb_get_intfdata(interface);
633
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800634 if (line6->properties->capabilities & LINE6_CAP_CONTROL)
Markus Grabner1027f472010-08-12 01:35:30 +0200635 line6_start_listen(line6);
636
637 snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
638 return 0;
639}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100640EXPORT_SYMBOL_GPL(line6_resume);
Markus Grabner1027f472010-08-12 01:35:30 +0200641
Markus Grabnere1a164d2010-08-23 01:08:25 +0200642#endif /* CONFIG_PM */
Markus Grabner1027f472010-08-12 01:35:30 +0200643
Markus Grabner705ecec2009-02-27 19:43:04 -0800644MODULE_AUTHOR(DRIVER_AUTHOR);
645MODULE_DESCRIPTION(DRIVER_DESC);
646MODULE_LICENSE("GPL");