GNU Linux-libre 4.19.286-gnu1
[releases.git] / sound / usb / line6 / toneport.c
1 /*
2  * Line 6 Linux USB driver
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 <linux/usb.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/leds.h>
18 #include <sound/core.h>
19 #include <sound/control.h>
20
21 #include "capture.h"
22 #include "driver.h"
23 #include "playback.h"
24
25 enum line6_device_type {
26         LINE6_GUITARPORT,
27         LINE6_PODSTUDIO_GX,
28         LINE6_PODSTUDIO_UX1,
29         LINE6_PODSTUDIO_UX2,
30         LINE6_TONEPORT_GX,
31         LINE6_TONEPORT_UX1,
32         LINE6_TONEPORT_UX2,
33 };
34
35 struct usb_line6_toneport;
36
37 struct toneport_led {
38         struct led_classdev dev;
39         char name[64];
40         struct usb_line6_toneport *toneport;
41         bool registered;
42 };
43
44 struct usb_line6_toneport {
45         /* Generic Line 6 USB data */
46         struct usb_line6 line6;
47
48         /* Source selector */
49         int source;
50
51         /* Serial number of device */
52         u32 serial_number;
53
54         /* Firmware version (x 100) */
55         u8 firmware_version;
56
57         /* Device type */
58         enum line6_device_type type;
59
60         /* LED instances */
61         struct toneport_led leds[2];
62 };
63
64 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
65
66 #define TONEPORT_PCM_DELAY 1
67
68 static struct snd_ratden toneport_ratden = {
69         .num_min = 44100,
70         .num_max = 44100,
71         .num_step = 1,
72         .den = 1
73 };
74
75 static struct line6_pcm_properties toneport_pcm_properties = {
76         .playback_hw = {
77                                   .info = (SNDRV_PCM_INFO_MMAP |
78                                            SNDRV_PCM_INFO_INTERLEAVED |
79                                            SNDRV_PCM_INFO_BLOCK_TRANSFER |
80                                            SNDRV_PCM_INFO_MMAP_VALID |
81                                            SNDRV_PCM_INFO_PAUSE |
82                                            SNDRV_PCM_INFO_SYNC_START),
83                                   .formats = SNDRV_PCM_FMTBIT_S16_LE,
84                                   .rates = SNDRV_PCM_RATE_KNOT,
85                                   .rate_min = 44100,
86                                   .rate_max = 44100,
87                                   .channels_min = 2,
88                                   .channels_max = 2,
89                                   .buffer_bytes_max = 60000,
90                                   .period_bytes_min = 64,
91                                   .period_bytes_max = 8192,
92                                   .periods_min = 1,
93                                   .periods_max = 1024},
94         .capture_hw = {
95                                  .info = (SNDRV_PCM_INFO_MMAP |
96                                           SNDRV_PCM_INFO_INTERLEAVED |
97                                           SNDRV_PCM_INFO_BLOCK_TRANSFER |
98                                           SNDRV_PCM_INFO_MMAP_VALID |
99                                           SNDRV_PCM_INFO_SYNC_START),
100                                  .formats = SNDRV_PCM_FMTBIT_S16_LE,
101                                  .rates = SNDRV_PCM_RATE_KNOT,
102                                  .rate_min = 44100,
103                                  .rate_max = 44100,
104                                  .channels_min = 2,
105                                  .channels_max = 2,
106                                  .buffer_bytes_max = 60000,
107                                  .period_bytes_min = 64,
108                                  .period_bytes_max = 8192,
109                                  .periods_min = 1,
110                                  .periods_max = 1024},
111         .rates = {
112                             .nrats = 1,
113                             .rats = &toneport_ratden},
114         .bytes_per_channel = 2
115 };
116
117 static const struct {
118         const char *name;
119         int code;
120 } toneport_source_info[] = {
121         {"Microphone", 0x0a01},
122         {"Line", 0x0801},
123         {"Instrument", 0x0b01},
124         {"Inst & Mic", 0x0901}
125 };
126
127 static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
128 {
129         int ret;
130
131         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
132                               USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
133                               cmd1, cmd2, NULL, 0, LINE6_TIMEOUT);
134
135         if (ret < 0) {
136                 dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
137                 return ret;
138         }
139
140         return 0;
141 }
142
143 /* monitor info callback */
144 static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
145                                      struct snd_ctl_elem_info *uinfo)
146 {
147         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
148         uinfo->count = 1;
149         uinfo->value.integer.min = 0;
150         uinfo->value.integer.max = 256;
151         return 0;
152 }
153
154 /* monitor get callback */
155 static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
156                                     struct snd_ctl_elem_value *ucontrol)
157 {
158         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
159
160         ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
161         return 0;
162 }
163
164 /* monitor put callback */
165 static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
166                                     struct snd_ctl_elem_value *ucontrol)
167 {
168         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
169         int err;
170
171         if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
172                 return 0;
173
174         line6pcm->volume_monitor = ucontrol->value.integer.value[0];
175
176         if (line6pcm->volume_monitor > 0) {
177                 err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR, true);
178                 if (err < 0) {
179                         line6pcm->volume_monitor = 0;
180                         line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
181                         return err;
182                 }
183         } else {
184                 line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
185         }
186
187         return 1;
188 }
189
190 /* source info callback */
191 static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
192                                     struct snd_ctl_elem_info *uinfo)
193 {
194         const int size = ARRAY_SIZE(toneport_source_info);
195
196         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
197         uinfo->count = 1;
198         uinfo->value.enumerated.items = size;
199
200         if (uinfo->value.enumerated.item >= size)
201                 uinfo->value.enumerated.item = size - 1;
202
203         strcpy(uinfo->value.enumerated.name,
204                toneport_source_info[uinfo->value.enumerated.item].name);
205
206         return 0;
207 }
208
209 /* source get callback */
210 static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
211                                    struct snd_ctl_elem_value *ucontrol)
212 {
213         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
214         struct usb_line6_toneport *toneport =
215             (struct usb_line6_toneport *)line6pcm->line6;
216         ucontrol->value.enumerated.item[0] = toneport->source;
217         return 0;
218 }
219
220 /* source put callback */
221 static int snd_toneport_source_put(struct snd_kcontrol *kcontrol,
222                                    struct snd_ctl_elem_value *ucontrol)
223 {
224         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
225         struct usb_line6_toneport *toneport =
226             (struct usb_line6_toneport *)line6pcm->line6;
227         unsigned int source;
228
229         source = ucontrol->value.enumerated.item[0];
230         if (source >= ARRAY_SIZE(toneport_source_info))
231                 return -EINVAL;
232         if (source == toneport->source)
233                 return 0;
234
235         toneport->source = source;
236         toneport_send_cmd(toneport->line6.usbdev,
237                           toneport_source_info[source].code, 0x0000);
238         return 1;
239 }
240
241 static void toneport_startup(struct usb_line6 *line6)
242 {
243         line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR, true);
244 }
245
246 /* control definition */
247 static const struct snd_kcontrol_new toneport_control_monitor = {
248         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
249         .name = "Monitor Playback Volume",
250         .index = 0,
251         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
252         .info = snd_toneport_monitor_info,
253         .get = snd_toneport_monitor_get,
254         .put = snd_toneport_monitor_put
255 };
256
257 /* source selector definition */
258 static const struct snd_kcontrol_new toneport_control_source = {
259         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
260         .name = "PCM Capture Source",
261         .index = 0,
262         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
263         .info = snd_toneport_source_info,
264         .get = snd_toneport_source_get,
265         .put = snd_toneport_source_put
266 };
267
268 /*
269         For the led on Guitarport.
270         Brightness goes from 0x00 to 0x26. Set a value above this to have led
271         blink.
272         (void cmd_0x02(byte red, byte green)
273 */
274
275 static bool toneport_has_led(struct usb_line6_toneport *toneport)
276 {
277         switch (toneport->type) {
278         case LINE6_GUITARPORT:
279         case LINE6_TONEPORT_GX:
280         /* add your device here if you are missing support for the LEDs */
281                 return true;
282
283         default:
284                 return false;
285         }
286 }
287
288 static const char * const led_colors[2] = { "red", "green" };
289 static const int led_init_vals[2] = { 0x00, 0x26 };
290
291 static void toneport_update_led(struct usb_line6_toneport *toneport)
292 {
293         toneport_send_cmd(toneport->line6.usbdev,
294                           (toneport->leds[0].dev.brightness << 8) | 0x0002,
295                           toneport->leds[1].dev.brightness);
296 }
297
298 static void toneport_led_brightness_set(struct led_classdev *led_cdev,
299                                         enum led_brightness brightness)
300 {
301         struct toneport_led *leds =
302                 container_of(led_cdev, struct toneport_led, dev);
303         toneport_update_led(leds->toneport);
304 }
305
306 static int toneport_init_leds(struct usb_line6_toneport *toneport)
307 {
308         struct device *dev = &toneport->line6.usbdev->dev;
309         int i, err;
310
311         for (i = 0; i < 2; i++) {
312                 struct toneport_led *led = &toneport->leds[i];
313                 struct led_classdev *leddev = &led->dev;
314
315                 led->toneport = toneport;
316                 snprintf(led->name, sizeof(led->name), "%s::%s",
317                          dev_name(dev), led_colors[i]);
318                 leddev->name = led->name;
319                 leddev->brightness = led_init_vals[i];
320                 leddev->max_brightness = 0x26;
321                 leddev->brightness_set = toneport_led_brightness_set;
322                 err = led_classdev_register(dev, leddev);
323                 if (err)
324                         return err;
325                 led->registered = true;
326         }
327
328         return 0;
329 }
330
331 static void toneport_remove_leds(struct usb_line6_toneport *toneport)
332 {
333         struct toneport_led *led;
334         int i;
335
336         for (i = 0; i < 2; i++) {
337                 led = &toneport->leds[i];
338                 if (!led->registered)
339                         break;
340                 led_classdev_unregister(&led->dev);
341                 led->registered = false;
342         }
343 }
344
345 static bool toneport_has_source_select(struct usb_line6_toneport *toneport)
346 {
347         switch (toneport->type) {
348         case LINE6_TONEPORT_UX1:
349         case LINE6_TONEPORT_UX2:
350         case LINE6_PODSTUDIO_UX1:
351         case LINE6_PODSTUDIO_UX2:
352                 return true;
353
354         default:
355                 return false;
356         }
357 }
358
359 /*
360         Setup Toneport device.
361 */
362 static int toneport_setup(struct usb_line6_toneport *toneport)
363 {
364         u32 *ticks;
365         struct usb_line6 *line6 = &toneport->line6;
366         struct usb_device *usbdev = line6->usbdev;
367
368         ticks = kmalloc(sizeof(*ticks), GFP_KERNEL);
369         if (!ticks)
370                 return -ENOMEM;
371
372         /* sync time on device with host: */
373         /* note: 32-bit timestamps overflow in year 2106 */
374         *ticks = (u32)ktime_get_real_seconds();
375         line6_write_data(line6, 0x80c6, ticks, 4);
376         kfree(ticks);
377
378         /* enable device: */
379         toneport_send_cmd(usbdev, 0x0301, 0x0000);
380
381         /* initialize source select: */
382         if (toneport_has_source_select(toneport))
383                 toneport_send_cmd(usbdev,
384                                   toneport_source_info[toneport->source].code,
385                                   0x0000);
386
387         if (toneport_has_led(toneport))
388                 toneport_update_led(toneport);
389
390         schedule_delayed_work(&toneport->line6.startup_work,
391                               msecs_to_jiffies(TONEPORT_PCM_DELAY * 1000));
392         return 0;
393 }
394
395 /*
396         Toneport device disconnected.
397 */
398 static void line6_toneport_disconnect(struct usb_line6 *line6)
399 {
400         struct usb_line6_toneport *toneport =
401                 (struct usb_line6_toneport *)line6;
402
403         if (toneport_has_led(toneport))
404                 toneport_remove_leds(toneport);
405 }
406
407
408 /*
409          Try to init Toneport device.
410 */
411 static int toneport_init(struct usb_line6 *line6,
412                          const struct usb_device_id *id)
413 {
414         int err;
415         struct usb_line6_toneport *toneport =  (struct usb_line6_toneport *) line6;
416
417         toneport->type = id->driver_info;
418
419         line6->disconnect = line6_toneport_disconnect;
420         line6->startup = toneport_startup;
421
422         /* initialize PCM subsystem: */
423         err = line6_init_pcm(line6, &toneport_pcm_properties);
424         if (err < 0)
425                 return err;
426
427         /* register monitor control: */
428         err = snd_ctl_add(line6->card,
429                           snd_ctl_new1(&toneport_control_monitor,
430                                        line6->line6pcm));
431         if (err < 0)
432                 return err;
433
434         /* register source select control: */
435         if (toneport_has_source_select(toneport)) {
436                 err =
437                     snd_ctl_add(line6->card,
438                                 snd_ctl_new1(&toneport_control_source,
439                                              line6->line6pcm));
440                 if (err < 0)
441                         return err;
442         }
443
444         line6_read_serial_number(line6, &toneport->serial_number);
445         line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
446
447         if (toneport_has_led(toneport)) {
448                 err = toneport_init_leds(toneport);
449                 if (err < 0)
450                         return err;
451         }
452
453         err = toneport_setup(toneport);
454         if (err)
455                 return err;
456
457         /* register audio system: */
458         return snd_card_register(line6->card);
459 }
460
461 #ifdef CONFIG_PM
462 /*
463         Resume Toneport device after reset.
464 */
465 static int toneport_reset_resume(struct usb_interface *interface)
466 {
467         int err;
468
469         err = toneport_setup(usb_get_intfdata(interface));
470         if (err)
471                 return err;
472         return line6_resume(interface);
473 }
474 #endif
475
476 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
477 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
478
479 /* table of devices that work with this driver */
480 static const struct usb_device_id toneport_id_table[] = {
481         { LINE6_DEVICE(0x4750),    .driver_info = LINE6_GUITARPORT },
482         { LINE6_DEVICE(0x4153),    .driver_info = LINE6_PODSTUDIO_GX },
483         { LINE6_DEVICE(0x4150),    .driver_info = LINE6_PODSTUDIO_UX1 },
484         { LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 },
485         { LINE6_DEVICE(0x4147),    .driver_info = LINE6_TONEPORT_GX },
486         { LINE6_DEVICE(0x4141),    .driver_info = LINE6_TONEPORT_UX1 },
487         { LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 },
488         {}
489 };
490
491 MODULE_DEVICE_TABLE(usb, toneport_id_table);
492
493 static const struct line6_properties toneport_properties_table[] = {
494         [LINE6_GUITARPORT] = {
495                 .id = "GuitarPort",
496                 .name = "GuitarPort",
497                 .capabilities   = LINE6_CAP_PCM,
498                 .altsetting = 2,  /* 1..4 seem to be ok */
499                 /* no control channel */
500                 .ep_audio_r = 0x82,
501                 .ep_audio_w = 0x01,
502         },
503         [LINE6_PODSTUDIO_GX] = {
504                 .id = "PODStudioGX",
505                 .name = "POD Studio GX",
506                 .capabilities   = LINE6_CAP_PCM,
507                 .altsetting = 2,  /* 1..4 seem to be ok */
508                 /* no control channel */
509                 .ep_audio_r = 0x82,
510                 .ep_audio_w = 0x01,
511         },
512         [LINE6_PODSTUDIO_UX1] = {
513                 .id = "PODStudioUX1",
514                 .name = "POD Studio UX1",
515                 .capabilities   = LINE6_CAP_PCM,
516                 .altsetting = 2,  /* 1..4 seem to be ok */
517                 /* no control channel */
518                 .ep_audio_r = 0x82,
519                 .ep_audio_w = 0x01,
520         },
521         [LINE6_PODSTUDIO_UX2] = {
522                 .id = "PODStudioUX2",
523                 .name = "POD Studio UX2",
524                 .capabilities   = LINE6_CAP_PCM,
525                 .altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
526                 /* no control channel */
527                 .ep_audio_r = 0x82,
528                 .ep_audio_w = 0x01,
529         },
530         [LINE6_TONEPORT_GX] = {
531                 .id = "TonePortGX",
532                 .name = "TonePort GX",
533                 .capabilities   = LINE6_CAP_PCM,
534                 .altsetting = 2,  /* 1..4 seem to be ok */
535                 /* no control channel */
536                 .ep_audio_r = 0x82,
537                 .ep_audio_w = 0x01,
538         },
539         [LINE6_TONEPORT_UX1] = {
540                 .id = "TonePortUX1",
541                 .name = "TonePort UX1",
542                 .capabilities   = LINE6_CAP_PCM,
543                 .altsetting = 2,  /* 1..4 seem to be ok */
544                 /* no control channel */
545                 .ep_audio_r = 0x82,
546                 .ep_audio_w = 0x01,
547         },
548         [LINE6_TONEPORT_UX2] = {
549                 .id = "TonePortUX2",
550                 .name = "TonePort UX2",
551                 .capabilities   = LINE6_CAP_PCM,
552                 .altsetting = 2,  /* defaults to 44.1kHz, 16-bit */
553                 /* no control channel */
554                 .ep_audio_r = 0x82,
555                 .ep_audio_w = 0x01,
556         },
557 };
558
559 /*
560         Probe USB device.
561 */
562 static int toneport_probe(struct usb_interface *interface,
563                           const struct usb_device_id *id)
564 {
565         return line6_probe(interface, id, "Line6-TonePort",
566                            &toneport_properties_table[id->driver_info],
567                            toneport_init, sizeof(struct usb_line6_toneport));
568 }
569
570 static struct usb_driver toneport_driver = {
571         .name = KBUILD_MODNAME,
572         .probe = toneport_probe,
573         .disconnect = line6_disconnect,
574 #ifdef CONFIG_PM
575         .suspend = line6_suspend,
576         .resume = line6_resume,
577         .reset_resume = toneport_reset_resume,
578 #endif
579         .id_table = toneport_id_table,
580 };
581
582 module_usb_driver(toneport_driver);
583
584 MODULE_DESCRIPTION("TonePort USB driver");
585 MODULE_LICENSE("GPL");