Merge remote branch 'common/android-2.6.36' into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / drivers / staging / line6 / toneport.c
1 /*
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
20 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
21
22 static struct snd_ratden toneport_ratden = {
23         .num_min = 44100,
24         .num_max = 44100,
25         .num_step = 1,
26         .den = 1
27 };
28
29 static struct line6_pcm_properties toneport_pcm_properties = {
30         .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},
65         .snd_line6_rates = {
66                             .nrats = 1,
67                             .rats = &toneport_ratden},
68         .bytes_per_frame = 4
69 };
70
71 /*
72         For the led on Guitarport.
73         Brightness goes from 0x00 to 0x26. Set a value above this to have led
74         blink.
75         (void cmd_0x02(byte red, byte green)
76 */
77 static int led_red = 0x00;
78 static int led_green = 0x26;
79
80 static 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;
85
86         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);
93 }
94
95 static ssize_t toneport_set_led_red(struct device *dev,
96                                     struct device_attribute *attr,
97                                     const char *buf, size_t count)
98 {
99         int retval;
100         long value;
101
102         retval = strict_strtol(buf, 10, &value);
103         if (retval)
104                 return retval;
105
106         led_red = value;
107         toneport_update_led(dev);
108         return count;
109 }
110
111 static ssize_t toneport_set_led_green(struct device *dev,
112                                       struct device_attribute *attr,
113                                       const char *buf, size_t count)
114 {
115         int retval;
116         long value;
117
118         retval = strict_strtol(buf, 10, &value);
119         if (retval)
120                 return retval;
121
122         led_green = value;
123         toneport_update_led(dev);
124         return count;
125 }
126
127 static DEVICE_ATTR(led_red, S_IWUSR | S_IRUGO, line6_nop_read,
128                    toneport_set_led_red);
129 static DEVICE_ATTR(led_green, S_IWUSR | S_IRUGO, line6_nop_read,
130                    toneport_set_led_green);
131
132 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
133 {
134         int ret;
135
136         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) {
141                 err("send failed (error %d)\n", ret);
142                 return ret;
143         }
144
145         return 0;
146 }
147
148 /*
149         Toneport destructor.
150 */
151 static void toneport_destruct(struct usb_interface *interface)
152 {
153         struct usb_line6_toneport *toneport = usb_get_intfdata(interface);
154         struct usb_line6 *line6;
155
156         if (toneport == NULL)
157                 return;
158         line6 = &toneport->line6;
159         if (line6 == NULL)
160                 return;
161         line6_cleanup_audio(line6);
162 }
163
164 /*
165          Init Toneport device.
166 */
167 int toneport_init(struct usb_interface *interface,
168                   struct usb_line6_toneport *toneport)
169 {
170         int err, ticks;
171         struct usb_line6 *line6 = &toneport->line6;
172         struct usb_device *usbdev;
173
174         if ((interface == NULL) || (toneport == NULL))
175                 return -ENODEV;
176
177         /* initialize audio system: */
178         err = line6_init_audio(line6);
179         if (err < 0) {
180                 toneport_destruct(interface);
181                 return err;
182         }
183
184         /* initialize PCM subsystem: */
185         err = line6_init_pcm(line6, &toneport_pcm_properties);
186         if (err < 0) {
187                 toneport_destruct(interface);
188                 return err;
189         }
190
191         /* register audio system: */
192         err = line6_register_audio(line6);
193         if (err < 0) {
194                 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         /*
207            seems to work without the first two...
208          */
209         /* 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);
213
214         if (usbdev->descriptor.idProduct != LINE6_DEVID_GUITARPORT) {
215                 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));
219                 toneport_update_led(&usbdev->dev);
220         }
221
222         return 0;
223 }
224
225 /*
226         Toneport device disconnected.
227 */
228 void toneport_disconnect(struct usb_interface *interface)
229 {
230         struct usb_line6_toneport *toneport;
231
232         if (interface == NULL)
233                 return;
234         toneport = usb_get_intfdata(interface);
235
236         if (toneport->line6.usbdev->descriptor.idProduct !=
237             LINE6_DEVID_GUITARPORT) {
238                 device_remove_file(&interface->dev, &dev_attr_led_red);
239                 device_remove_file(&interface->dev, &dev_attr_led_green);
240         }
241
242         if (toneport != NULL) {
243                 struct snd_line6_pcm *line6pcm = toneport->line6.line6pcm;
244
245                 if (line6pcm != NULL) {
246                         unlink_wait_clear_audio_out_urbs(line6pcm);
247                         unlink_wait_clear_audio_in_urbs(line6pcm);
248                 }
249         }
250
251         toneport_destruct(interface);
252 }