blob: e6770ea179364e8579d7646d72fec4c0379c64bf [file] [log] [blame]
Markus Grabner705ecec2009-02-27 19:43:04 -08001/*
2 * Line6 Linux USB driver - 0.8.0
3 *
4 * Copyright (C) 2004-2009 Markus Grabner (grabner@icg.tugraz.at)
5 * Emil Myhrman (emil.myhrman@gmail.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2.
10 *
11 */
12
13#include "driver.h"
14
15#include "audio.h"
16#include "capture.h"
17#include "playback.h"
18#include "toneport.h"
19
Markus Grabner705ecec2009-02-27 19:43:04 -080020static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
21
Markus Grabner705ecec2009-02-27 19:43:04 -080022static struct snd_ratden toneport_ratden = {
23 .num_min = 44100,
24 .num_max = 44100,
25 .num_step = 1,
26 .den = 1
27};
28
29static struct line6_pcm_properties toneport_pcm_properties = {
Shawn Bohrer63a4a8b2009-11-15 22:17:58 -060030 .snd_line6_playback_hw = {
31 .info = (SNDRV_PCM_INFO_MMAP |
32 SNDRV_PCM_INFO_INTERLEAVED |
33 SNDRV_PCM_INFO_BLOCK_TRANSFER |
34 SNDRV_PCM_INFO_MMAP_VALID |
35 SNDRV_PCM_INFO_PAUSE |
36 SNDRV_PCM_INFO_SYNC_START),
37 .formats = SNDRV_PCM_FMTBIT_S16_LE,
38 .rates = SNDRV_PCM_RATE_KNOT,
39 .rate_min = 44100,
40 .rate_max = 44100,
41 .channels_min = 2,
42 .channels_max = 2,
43 .buffer_bytes_max = 60000,
44 .period_bytes_min = 180 * 4,
45 .period_bytes_max = 8192,
46 .periods_min = 1,
47 .periods_max = 1024},
48 .snd_line6_capture_hw = {
49 .info = (SNDRV_PCM_INFO_MMAP |
50 SNDRV_PCM_INFO_INTERLEAVED |
51 SNDRV_PCM_INFO_BLOCK_TRANSFER |
52 SNDRV_PCM_INFO_MMAP_VALID |
53 SNDRV_PCM_INFO_SYNC_START),
54 .formats = SNDRV_PCM_FMTBIT_S16_LE,
55 .rates = SNDRV_PCM_RATE_KNOT,
56 .rate_min = 44100,
57 .rate_max = 44100,
58 .channels_min = 2,
59 .channels_max = 2,
60 .buffer_bytes_max = 60000,
61 .period_bytes_min = 188 * 4,
62 .period_bytes_max = 8192,
63 .periods_min = 1,
64 .periods_max = 1024},
Markus Grabner705ecec2009-02-27 19:43:04 -080065 .snd_line6_rates = {
Shawn Bohrer63a4a8b2009-11-15 22:17:58 -060066 .nrats = 1,
67 .rats = &toneport_ratden},
Markus Grabner705ecec2009-02-27 19:43:04 -080068 .bytes_per_frame = 4
69};
70
71/*
72 For the led on Guitarport.
Greg Kroah-Hartman63537732009-02-27 22:43:45 -080073 Brightness goes from 0x00 to 0x26. Set a value above this to have led
74 blink.
Markus Grabner705ecec2009-02-27 19:43:04 -080075 (void cmd_0x02(byte red, byte green)
76*/
77static int led_red = 0x00;
78static int led_green = 0x26;
79
Greg Kroah-Hartman63537732009-02-27 22:43:45 -080080static void toneport_update_led(struct device *dev)
81{
82 struct usb_interface *interface = to_usb_interface(dev);
83 struct usb_line6_toneport *tp = usb_get_intfdata(interface);
84 struct usb_line6 *line6;
Markus Grabner705ecec2009-02-27 19:43:04 -080085
Greg Kroah-Hartman63537732009-02-27 22:43:45 -080086 if (!tp)
87 return;
88
89 line6 = &tp->line6;
90 if (line6)
91 toneport_send_cmd(line6->usbdev, (led_red << 8) | 0x0002,
92 led_green);
Markus Grabner705ecec2009-02-27 19:43:04 -080093}
Greg Kroah-Hartman63537732009-02-27 22:43:45 -080094
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -080095static ssize_t toneport_set_led_red(struct device *dev,
96 struct device_attribute *attr,
Greg Kroah-Hartman63537732009-02-27 22:43:45 -080097 const char *buf, size_t count)
98{
Shawn Bohrerbb950a12009-11-15 22:17:59 -060099 int retval;
100 long value;
101
102 retval = strict_strtol(buf, 10, &value);
103 if (retval)
104 return retval;
105
106 led_red = value;
Markus Grabner705ecec2009-02-27 19:43:04 -0800107 toneport_update_led(dev);
108 return count;
109}
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800110
Greg Kroah-Hartman77491e52009-02-27 20:25:43 -0800111static ssize_t toneport_set_led_green(struct device *dev,
112 struct device_attribute *attr,
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800113 const char *buf, size_t count)
114{
Shawn Bohrerbb950a12009-11-15 22:17:59 -0600115 int retval;
116 long value;
117
118 retval = strict_strtol(buf, 10, &value);
119 if (retval)
120 return retval;
121
122 led_green = value;
Markus Grabner705ecec2009-02-27 19:43:04 -0800123 toneport_update_led(dev);
124 return count;
125}
126
Shawn Bohrer63a4a8b2009-11-15 22:17:58 -0600127static DEVICE_ATTR(led_red, S_IWUGO | S_IRUGO, line6_nop_read,
128 toneport_set_led_red);
129static DEVICE_ATTR(led_green, S_IWUGO | S_IRUGO, line6_nop_read,
130 toneport_set_led_green);
Markus Grabner705ecec2009-02-27 19:43:04 -0800131
132static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
133{
134 int ret;
Markus Grabner705ecec2009-02-27 19:43:04 -0800135
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800136 ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
137 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
138 cmd1, cmd2, NULL, 0, LINE6_TIMEOUT * HZ);
139
140 if (ret < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800141 err("send failed (error %d)\n", ret);
142 return ret;
143 }
144
145 return 0;
146}
147
148/*
149 Toneport destructor.
150*/
151static void toneport_destruct(struct usb_interface *interface)
152{
153 struct usb_line6_toneport *toneport = usb_get_intfdata(interface);
154 struct usb_line6 *line6;
155
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800156 if (toneport == NULL)
157 return;
Markus Grabner705ecec2009-02-27 19:43:04 -0800158 line6 = &toneport->line6;
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800159 if (line6 == NULL)
160 return;
Markus Grabner705ecec2009-02-27 19:43:04 -0800161 line6_cleanup_audio(line6);
162}
163
164/*
165 Init Toneport device.
166*/
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800167int toneport_init(struct usb_interface *interface,
168 struct usb_line6_toneport *toneport)
Markus Grabner705ecec2009-02-27 19:43:04 -0800169{
170 int err, ticks;
171 struct usb_line6 *line6 = &toneport->line6;
172 struct usb_device *usbdev;
173
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800174 if ((interface == NULL) || (toneport == NULL))
Markus Grabner705ecec2009-02-27 19:43:04 -0800175 return -ENODEV;
176
177 /* initialize audio system: */
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800178 err = line6_init_audio(line6);
179 if (err < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800180 toneport_destruct(interface);
181 return err;
182 }
183
184 /* initialize PCM subsystem: */
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800185 err = line6_init_pcm(line6, &toneport_pcm_properties);
186 if (err < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800187 toneport_destruct(interface);
188 return err;
189 }
190
191 /* register audio system: */
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800192 err = line6_register_audio(line6);
193 if (err < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800194 toneport_destruct(interface);
195 return err;
196 }
197
198 usbdev = line6->usbdev;
199 line6_read_serial_number(line6, &toneport->serial_number);
200 line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
201
202 /* sync time on device with host: */
203 ticks = (int)get_seconds();
204 line6_write_data(line6, 0x80c6, &ticks, 4);
205
206 /*
Shawn Bohrer63a4a8b2009-11-15 22:17:58 -0600207 seems to work without the first two...
208 */
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800209 /* toneport_send_cmd(usbdev, 0x0201, 0x0002); */
210 /* toneport_send_cmd(usbdev, 0x0801, 0x0000); */
211 /* only one that works for me; on GP, TP might be different? */
212 toneport_send_cmd(usbdev, 0x0301, 0x0000);
Markus Grabner705ecec2009-02-27 19:43:04 -0800213
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800214 if (usbdev->descriptor.idProduct != LINE6_DEVID_GUITARPORT) {
Shawn Bohrer63a4a8b2009-11-15 22:17:58 -0600215 CHECK_RETURN(device_create_file
216 (&interface->dev, &dev_attr_led_red));
217 CHECK_RETURN(device_create_file
218 (&interface->dev, &dev_attr_led_green));
Markus Grabner705ecec2009-02-27 19:43:04 -0800219 toneport_update_led(&usbdev->dev);
220 }
221
222 return 0;
223}
224
225/*
226 Toneport device disconnected.
227*/
228void toneport_disconnect(struct usb_interface *interface)
229{
230 struct usb_line6_toneport *toneport;
231
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800232 if (interface == NULL)
233 return;
Markus Grabner705ecec2009-02-27 19:43:04 -0800234 toneport = usb_get_intfdata(interface);
235
Shawn Bohrer63a4a8b2009-11-15 22:17:58 -0600236 if (toneport->line6.usbdev->descriptor.idProduct !=
237 LINE6_DEVID_GUITARPORT) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800238 device_remove_file(&interface->dev, &dev_attr_led_red);
239 device_remove_file(&interface->dev, &dev_attr_led_green);
240 }
241
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800242 if (toneport != NULL) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800243 struct snd_line6_pcm *line6pcm = toneport->line6.line6pcm;
244
Greg Kroah-Hartman63537732009-02-27 22:43:45 -0800245 if (line6pcm != NULL) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800246 unlink_wait_clear_audio_out_urbs(line6pcm);
247 unlink_wait_clear_audio_in_urbs(line6pcm);
248 }
249 }
250
251 toneport_destruct(interface);
252}