]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/line6/toneport.c
Merge tag 'jfs-3.12' of git://github.com/kleikamp/linux-shaggy
[karo-tx-linux.git] / drivers / staging / line6 / toneport.c
1 /*
2  * Line6 Linux USB driver - 0.9.1beta
3  *
4  * Copyright (C) 2004-2010 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 <linux/wait.h>
14 #include <sound/control.h>
15
16 #include "audio.h"
17 #include "capture.h"
18 #include "driver.h"
19 #include "playback.h"
20 #include "toneport.h"
21
22 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
23
24 #define TONEPORT_PCM_DELAY 1
25
26 static struct snd_ratden toneport_ratden = {
27         .num_min = 44100,
28         .num_max = 44100,
29         .num_step = 1,
30         .den = 1
31 };
32
33 static struct line6_pcm_properties toneport_pcm_properties = {
34         .snd_line6_playback_hw = {
35                                   .info = (SNDRV_PCM_INFO_MMAP |
36                                            SNDRV_PCM_INFO_INTERLEAVED |
37                                            SNDRV_PCM_INFO_BLOCK_TRANSFER |
38                                            SNDRV_PCM_INFO_MMAP_VALID |
39                                            SNDRV_PCM_INFO_PAUSE |
40 #ifdef CONFIG_PM
41                                            SNDRV_PCM_INFO_RESUME |
42 #endif
43                                            SNDRV_PCM_INFO_SYNC_START),
44                                   .formats = SNDRV_PCM_FMTBIT_S16_LE,
45                                   .rates = SNDRV_PCM_RATE_KNOT,
46                                   .rate_min = 44100,
47                                   .rate_max = 44100,
48                                   .channels_min = 2,
49                                   .channels_max = 2,
50                                   .buffer_bytes_max = 60000,
51                                   .period_bytes_min = 64,
52                                   .period_bytes_max = 8192,
53                                   .periods_min = 1,
54                                   .periods_max = 1024},
55         .snd_line6_capture_hw = {
56                                  .info = (SNDRV_PCM_INFO_MMAP |
57                                           SNDRV_PCM_INFO_INTERLEAVED |
58                                           SNDRV_PCM_INFO_BLOCK_TRANSFER |
59                                           SNDRV_PCM_INFO_MMAP_VALID |
60 #ifdef CONFIG_PM
61                                           SNDRV_PCM_INFO_RESUME |
62 #endif
63                                           SNDRV_PCM_INFO_SYNC_START),
64                                  .formats = SNDRV_PCM_FMTBIT_S16_LE,
65                                  .rates = SNDRV_PCM_RATE_KNOT,
66                                  .rate_min = 44100,
67                                  .rate_max = 44100,
68                                  .channels_min = 2,
69                                  .channels_max = 2,
70                                  .buffer_bytes_max = 60000,
71                                  .period_bytes_min = 64,
72                                  .period_bytes_max = 8192,
73                                  .periods_min = 1,
74                                  .periods_max = 1024},
75         .snd_line6_rates = {
76                             .nrats = 1,
77                             .rats = &toneport_ratden},
78         .bytes_per_frame = 4
79 };
80
81 /*
82         For the led on Guitarport.
83         Brightness goes from 0x00 to 0x26. Set a value above this to have led
84         blink.
85         (void cmd_0x02(byte red, byte green)
86 */
87 static int led_red = 0x00;
88 static int led_green = 0x26;
89
90 static const struct {
91         const char *name;
92         int code;
93 } toneport_source_info[] = {
94         {"Microphone", 0x0a01},
95         {"Line", 0x0801},
96         {"Instrument", 0x0b01},
97         {"Inst & Mic", 0x0901}
98 };
99
100 static bool toneport_has_led(short product)
101 {
102         return
103             (product == LINE6_DEVID_GUITARPORT) ||
104             (product == LINE6_DEVID_TONEPORT_GX);
105         /* add your device here if you are missing support for the LEDs */
106 }
107
108 static void toneport_update_led(struct device *dev)
109 {
110         struct usb_interface *interface = to_usb_interface(dev);
111         struct usb_line6_toneport *tp = usb_get_intfdata(interface);
112         struct usb_line6 *line6;
113
114         if (!tp)
115                 return;
116
117         line6 = &tp->line6;
118         if (line6)
119                 toneport_send_cmd(line6->usbdev, (led_red << 8) | 0x0002,
120                                   led_green);
121 }
122
123 static ssize_t toneport_set_led_red(struct device *dev,
124                                     struct device_attribute *attr,
125                                     const char *buf, size_t count)
126 {
127         int retval;
128
129         retval = kstrtoint(buf, 10, &led_red);
130         if (retval)
131                 return retval;
132
133         toneport_update_led(dev);
134         return count;
135 }
136
137 static ssize_t toneport_set_led_green(struct device *dev,
138                                       struct device_attribute *attr,
139                                       const char *buf, size_t count)
140 {
141         int retval;
142
143         retval = kstrtoint(buf, 10, &led_green);
144         if (retval)
145                 return retval;
146
147         toneport_update_led(dev);
148         return count;
149 }
150
151 static DEVICE_ATTR(led_red, S_IWUSR | S_IRUGO, line6_nop_read,
152                    toneport_set_led_red);
153 static DEVICE_ATTR(led_green, S_IWUSR | S_IRUGO, line6_nop_read,
154                    toneport_set_led_green);
155
156 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
157 {
158         int ret;
159
160         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
161                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
162                               cmd1, cmd2, NULL, 0, LINE6_TIMEOUT * HZ);
163
164         if (ret < 0) {
165                 dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
166                 return ret;
167         }
168
169         return 0;
170 }
171
172 /* monitor info callback */
173 static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
174                                      struct snd_ctl_elem_info *uinfo)
175 {
176         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
177         uinfo->count = 1;
178         uinfo->value.integer.min = 0;
179         uinfo->value.integer.max = 256;
180         return 0;
181 }
182
183 /* monitor get callback */
184 static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
185                                     struct snd_ctl_elem_value *ucontrol)
186 {
187         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
188         ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
189         return 0;
190 }
191
192 /* monitor put callback */
193 static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
194                                     struct snd_ctl_elem_value *ucontrol)
195 {
196         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
197
198         if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
199                 return 0;
200
201         line6pcm->volume_monitor = ucontrol->value.integer.value[0];
202
203         if (line6pcm->volume_monitor > 0)
204                 line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_MONITOR);
205         else
206                 line6_pcm_release(line6pcm, LINE6_BITS_PCM_MONITOR);
207
208         return 1;
209 }
210
211 /* source info callback */
212 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
213                                     struct snd_ctl_elem_info *uinfo)
214 {
215         const int size = ARRAY_SIZE(toneport_source_info);
216         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
217         uinfo->count = 1;
218         uinfo->value.enumerated.items = size;
219
220         if (uinfo->value.enumerated.item >= size)
221                 uinfo->value.enumerated.item = size - 1;
222
223         strcpy(uinfo->value.enumerated.name,
224                toneport_source_info[uinfo->value.enumerated.item].name);
225
226         return 0;
227 }
228
229 /* source get callback */
230 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
231                                    struct snd_ctl_elem_value *ucontrol)
232 {
233         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
234         struct usb_line6_toneport *toneport =
235             (struct usb_line6_toneport *)line6pcm->line6;
236         ucontrol->value.enumerated.item[0] = toneport->source;
237         return 0;
238 }
239
240 /* source put callback */
241 static int snd_toneport_source_put(struct snd_kcontrol *kcontrol,
242                                    struct snd_ctl_elem_value *ucontrol)
243 {
244         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
245         struct usb_line6_toneport *toneport =
246             (struct usb_line6_toneport *)line6pcm->line6;
247         unsigned int source;
248
249         source = ucontrol->value.enumerated.item[0];
250         if (source >= ARRAY_SIZE(toneport_source_info))
251                 return -EINVAL;
252         if (source == toneport->source)
253                 return 0;
254
255         toneport->source = source;
256         toneport_send_cmd(toneport->line6.usbdev,
257                           toneport_source_info[source].code, 0x0000);
258         return 1;
259 }
260
261 static void toneport_start_pcm(unsigned long arg)
262 {
263         struct usb_line6_toneport *toneport = (struct usb_line6_toneport *)arg;
264         struct usb_line6 *line6 = &toneport->line6;
265         line6_pcm_acquire(line6->line6pcm, LINE6_BITS_PCM_MONITOR);
266 }
267
268 /* control definition */
269 static struct snd_kcontrol_new toneport_control_monitor = {
270         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
271         .name = "Monitor Playback Volume",
272         .index = 0,
273         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
274         .info = snd_toneport_monitor_info,
275         .get = snd_toneport_monitor_get,
276         .put = snd_toneport_monitor_put
277 };
278
279 /* source selector definition */
280 static struct snd_kcontrol_new toneport_control_source = {
281         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
282         .name = "PCM Capture Source",
283         .index = 0,
284         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
285         .info = snd_toneport_source_info,
286         .get = snd_toneport_source_get,
287         .put = snd_toneport_source_put
288 };
289
290 /*
291         Toneport destructor.
292 */
293 static void toneport_destruct(struct usb_interface *interface)
294 {
295         struct usb_line6_toneport *toneport = usb_get_intfdata(interface);
296
297         if (toneport == NULL)
298                 return;
299         line6_cleanup_audio(&toneport->line6);
300 }
301
302 /*
303         Setup Toneport device.
304 */
305 static void toneport_setup(struct usb_line6_toneport *toneport)
306 {
307         int ticks;
308         struct usb_line6 *line6 = &toneport->line6;
309         struct usb_device *usbdev = line6->usbdev;
310
311         /* sync time on device with host: */
312         ticks = (int)get_seconds();
313         line6_write_data(line6, 0x80c6, &ticks, 4);
314
315         /* enable device: */
316         toneport_send_cmd(usbdev, 0x0301, 0x0000);
317
318         /* initialize source select: */
319         switch (usbdev->descriptor.idProduct) {
320         case LINE6_DEVID_TONEPORT_UX1:
321         case LINE6_DEVID_TONEPORT_UX2:
322         case LINE6_DEVID_PODSTUDIO_UX1:
323         case LINE6_DEVID_PODSTUDIO_UX2:
324                 toneport_send_cmd(usbdev,
325                                   toneport_source_info[toneport->source].code,
326                                   0x0000);
327         }
328
329         if (toneport_has_led(usbdev->descriptor.idProduct))
330                 toneport_update_led(&usbdev->dev);
331 }
332
333 /*
334          Try to init Toneport device.
335 */
336 static int toneport_try_init(struct usb_interface *interface,
337                              struct usb_line6_toneport *toneport)
338 {
339         int err;
340         struct usb_line6 *line6 = &toneport->line6;
341         struct usb_device *usbdev = line6->usbdev;
342
343         if ((interface == NULL) || (toneport == NULL))
344                 return -ENODEV;
345
346         /* initialize audio system: */
347         err = line6_init_audio(line6);
348         if (err < 0)
349                 return err;
350
351         /* initialize PCM subsystem: */
352         err = line6_init_pcm(line6, &toneport_pcm_properties);
353         if (err < 0)
354                 return err;
355
356         /* register monitor control: */
357         err = snd_ctl_add(line6->card,
358                           snd_ctl_new1(&toneport_control_monitor,
359                                        line6->line6pcm));
360         if (err < 0)
361                 return err;
362
363         /* register source select control: */
364         switch (usbdev->descriptor.idProduct) {
365         case LINE6_DEVID_TONEPORT_UX1:
366         case LINE6_DEVID_TONEPORT_UX2:
367         case LINE6_DEVID_PODSTUDIO_UX1:
368         case LINE6_DEVID_PODSTUDIO_UX2:
369                 err =
370                     snd_ctl_add(line6->card,
371                                 snd_ctl_new1(&toneport_control_source,
372                                              line6->line6pcm));
373                 if (err < 0)
374                         return err;
375         }
376
377         /* register audio system: */
378         err = line6_register_audio(line6);
379         if (err < 0)
380                 return err;
381
382         line6_read_serial_number(line6, &toneport->serial_number);
383         line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
384
385         if (toneport_has_led(usbdev->descriptor.idProduct)) {
386                 CHECK_RETURN(device_create_file
387                              (&interface->dev, &dev_attr_led_red));
388                 CHECK_RETURN(device_create_file
389                              (&interface->dev, &dev_attr_led_green));
390         }
391
392         toneport_setup(toneport);
393
394         init_timer(&toneport->timer);
395         toneport->timer.expires = jiffies + TONEPORT_PCM_DELAY * HZ;
396         toneport->timer.function = toneport_start_pcm;
397         toneport->timer.data = (unsigned long)toneport;
398         add_timer(&toneport->timer);
399
400         return 0;
401 }
402
403 /*
404          Init Toneport device (and clean up in case of failure).
405 */
406 int line6_toneport_init(struct usb_interface *interface,
407                         struct usb_line6_toneport *toneport)
408 {
409         int err = toneport_try_init(interface, toneport);
410
411         if (err < 0)
412                 toneport_destruct(interface);
413
414         return err;
415 }
416
417 /*
418         Resume Toneport device after reset.
419 */
420 void line6_toneport_reset_resume(struct usb_line6_toneport *toneport)
421 {
422         toneport_setup(toneport);
423 }
424
425 /*
426         Toneport device disconnected.
427 */
428 void line6_toneport_disconnect(struct usb_interface *interface)
429 {
430         struct usb_line6_toneport *toneport;
431
432         if (interface == NULL)
433                 return;
434
435         toneport = usb_get_intfdata(interface);
436         del_timer_sync(&toneport->timer);
437
438         if (toneport_has_led(toneport->line6.usbdev->descriptor.idProduct)) {
439                 device_remove_file(&interface->dev, &dev_attr_led_red);
440                 device_remove_file(&interface->dev, &dev_attr_led_green);
441         }
442
443         if (toneport != NULL) {
444                 struct snd_line6_pcm *line6pcm = toneport->line6.line6pcm;
445
446                 if (line6pcm != NULL) {
447                         line6_pcm_release(line6pcm, LINE6_BITS_PCM_MONITOR);
448                         line6_pcm_disconnect(line6pcm);
449                 }
450         }
451
452         toneport_destruct(interface);
453 }