Mention branches and keyring.
[releases.git] / usb / line6 / podhd.c
1 /*
2  * Line 6 Pod HD
3  *
4  * Copyright (C) 2011 Stefan Hajnoczi <stefanha@gmail.com>
5  * Copyright (C) 2015 Andrej Krutak <dev@andree.sk>
6  * Copyright (C) 2017 Hans P. Moller <hmoller@uc.cl>
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License as
10  *      published by the Free Software Foundation, version 2.
11  *
12  */
13
14 #include <linux/usb.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19
20 #include "driver.h"
21 #include "pcm.h"
22
23 #define PODHD_STARTUP_DELAY 500
24
25 /*
26  * Stages of POD startup procedure
27  */
28 enum {
29         PODHD_STARTUP_INIT = 1,
30         PODHD_STARTUP_SCHEDULE_WORKQUEUE,
31         PODHD_STARTUP_SETUP,
32         PODHD_STARTUP_LAST = PODHD_STARTUP_SETUP - 1
33 };
34
35 enum {
36         LINE6_PODHD300,
37         LINE6_PODHD400,
38         LINE6_PODHD500_0,
39         LINE6_PODHD500_1,
40         LINE6_PODX3,
41         LINE6_PODX3LIVE,
42         LINE6_PODHD500X,
43         LINE6_PODHDDESKTOP
44 };
45
46 struct usb_line6_podhd {
47         /* Generic Line 6 USB data */
48         struct usb_line6 line6;
49
50         /* Timer for device initialization */
51         struct timer_list startup_timer;
52
53         /* Work handler for device initialization */
54         struct work_struct startup_work;
55
56         /* Current progress in startup procedure */
57         int startup_progress;
58
59         /* Serial number of device */
60         u32 serial_number;
61
62         /* Firmware version */
63         int firmware_version;
64 };
65
66 static struct snd_ratden podhd_ratden = {
67         .num_min = 48000,
68         .num_max = 48000,
69         .num_step = 1,
70         .den = 1,
71 };
72
73 static struct line6_pcm_properties podhd_pcm_properties = {
74         .playback_hw = {
75                                   .info = (SNDRV_PCM_INFO_MMAP |
76                                            SNDRV_PCM_INFO_INTERLEAVED |
77                                            SNDRV_PCM_INFO_BLOCK_TRANSFER |
78                                            SNDRV_PCM_INFO_MMAP_VALID |
79                                            SNDRV_PCM_INFO_PAUSE |
80                                            SNDRV_PCM_INFO_SYNC_START),
81                                   .formats = SNDRV_PCM_FMTBIT_S24_3LE,
82                                   .rates = SNDRV_PCM_RATE_48000,
83                                   .rate_min = 48000,
84                                   .rate_max = 48000,
85                                   .channels_min = 2,
86                                   .channels_max = 2,
87                                   .buffer_bytes_max = 60000,
88                                   .period_bytes_min = 64,
89                                   .period_bytes_max = 8192,
90                                   .periods_min = 1,
91                                   .periods_max = 1024},
92         .capture_hw = {
93                                  .info = (SNDRV_PCM_INFO_MMAP |
94                                           SNDRV_PCM_INFO_INTERLEAVED |
95                                           SNDRV_PCM_INFO_BLOCK_TRANSFER |
96                                           SNDRV_PCM_INFO_MMAP_VALID |
97                                           SNDRV_PCM_INFO_SYNC_START),
98                                  .formats = SNDRV_PCM_FMTBIT_S24_3LE,
99                                  .rates = SNDRV_PCM_RATE_48000,
100                                  .rate_min = 48000,
101                                  .rate_max = 48000,
102                                  .channels_min = 2,
103                                  .channels_max = 2,
104                                  .buffer_bytes_max = 60000,
105                                  .period_bytes_min = 64,
106                                  .period_bytes_max = 8192,
107                                  .periods_min = 1,
108                                  .periods_max = 1024},
109         .rates = {
110                             .nrats = 1,
111                             .rats = &podhd_ratden},
112         .bytes_per_channel = 3 /* SNDRV_PCM_FMTBIT_S24_3LE */
113 };
114
115 static struct line6_pcm_properties podx3_pcm_properties = {
116         .playback_hw = {
117                                   .info = (SNDRV_PCM_INFO_MMAP |
118                                            SNDRV_PCM_INFO_INTERLEAVED |
119                                            SNDRV_PCM_INFO_BLOCK_TRANSFER |
120                                            SNDRV_PCM_INFO_MMAP_VALID |
121                                            SNDRV_PCM_INFO_PAUSE |
122                                            SNDRV_PCM_INFO_SYNC_START),
123                                   .formats = SNDRV_PCM_FMTBIT_S24_3LE,
124                                   .rates = SNDRV_PCM_RATE_48000,
125                                   .rate_min = 48000,
126                                   .rate_max = 48000,
127                                   .channels_min = 2,
128                                   .channels_max = 2,
129                                   .buffer_bytes_max = 60000,
130                                   .period_bytes_min = 64,
131                                   .period_bytes_max = 8192,
132                                   .periods_min = 1,
133                                   .periods_max = 1024},
134         .capture_hw = {
135                                  .info = (SNDRV_PCM_INFO_MMAP |
136                                           SNDRV_PCM_INFO_INTERLEAVED |
137                                           SNDRV_PCM_INFO_BLOCK_TRANSFER |
138                                           SNDRV_PCM_INFO_MMAP_VALID |
139                                           SNDRV_PCM_INFO_SYNC_START),
140                                  .formats = SNDRV_PCM_FMTBIT_S24_3LE,
141                                  .rates = SNDRV_PCM_RATE_48000,
142                                  .rate_min = 48000,
143                                  .rate_max = 48000,
144                                  /* 1+2: Main signal (out), 3+4: Tone 1,
145                                   * 5+6: Tone 2, 7+8: raw
146                                   */
147                                  .channels_min = 8,
148                                  .channels_max = 8,
149                                  .buffer_bytes_max = 60000,
150                                  .period_bytes_min = 64,
151                                  .period_bytes_max = 8192,
152                                  .periods_min = 1,
153                                  .periods_max = 1024},
154         .rates = {
155                             .nrats = 1,
156                             .rats = &podhd_ratden},
157         .bytes_per_channel = 3 /* SNDRV_PCM_FMTBIT_S24_3LE */
158 };
159 static struct usb_driver podhd_driver;
160
161 static void podhd_startup_start_workqueue(struct timer_list *t);
162 static void podhd_startup_workqueue(struct work_struct *work);
163 static int podhd_startup_finalize(struct usb_line6_podhd *pod);
164
165 static ssize_t serial_number_show(struct device *dev,
166                                   struct device_attribute *attr, char *buf)
167 {
168         struct snd_card *card = dev_to_snd_card(dev);
169         struct usb_line6_podhd *pod = card->private_data;
170
171         return sprintf(buf, "%u\n", pod->serial_number);
172 }
173
174 static ssize_t firmware_version_show(struct device *dev,
175                                      struct device_attribute *attr, char *buf)
176 {
177         struct snd_card *card = dev_to_snd_card(dev);
178         struct usb_line6_podhd *pod = card->private_data;
179
180         return sprintf(buf, "%06x\n", pod->firmware_version);
181 }
182
183 static DEVICE_ATTR_RO(firmware_version);
184 static DEVICE_ATTR_RO(serial_number);
185
186 static struct attribute *podhd_dev_attrs[] = {
187         &dev_attr_firmware_version.attr,
188         &dev_attr_serial_number.attr,
189         NULL
190 };
191
192 static const struct attribute_group podhd_dev_attr_group = {
193         .name = "podhd",
194         .attrs = podhd_dev_attrs,
195 };
196
197 /*
198  * POD X3 startup procedure.
199  *
200  * May be compatible with other POD HD's, since it's also similar to the
201  * previous POD setup. In any case, it doesn't seem to be required for the
202  * audio nor bulk interfaces to work.
203  */
204
205 static void podhd_startup(struct usb_line6_podhd *pod)
206 {
207         CHECK_STARTUP_PROGRESS(pod->startup_progress, PODHD_STARTUP_INIT);
208
209         /* delay startup procedure: */
210         line6_start_timer(&pod->startup_timer, PODHD_STARTUP_DELAY,
211                 podhd_startup_start_workqueue);
212 }
213
214 static void podhd_startup_start_workqueue(struct timer_list *t)
215 {
216         struct usb_line6_podhd *pod = from_timer(pod, t, startup_timer);
217
218         CHECK_STARTUP_PROGRESS(pod->startup_progress,
219                 PODHD_STARTUP_SCHEDULE_WORKQUEUE);
220
221         /* schedule work for global work queue: */
222         schedule_work(&pod->startup_work);
223 }
224
225 static int podhd_dev_start(struct usb_line6_podhd *pod)
226 {
227         int ret;
228         u8 *init_bytes;
229         int i;
230         struct usb_device *usbdev = pod->line6.usbdev;
231
232         init_bytes = kmalloc(8, GFP_KERNEL);
233         if (!init_bytes)
234                 return -ENOMEM;
235
236         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0),
237                                         0x67, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
238                                         0x11, 0,
239                                         NULL, 0, LINE6_TIMEOUT);
240         if (ret < 0) {
241                 dev_err(pod->line6.ifcdev, "read request failed (error %d)\n", ret);
242                 goto exit;
243         }
244
245         /* NOTE: looks like some kind of ping message */
246         ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
247                                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
248                                         0x11, 0x0,
249                                         init_bytes, 3, LINE6_TIMEOUT);
250         if (ret < 0) {
251                 dev_err(pod->line6.ifcdev,
252                         "receive length failed (error %d)\n", ret);
253                 goto exit;
254         }
255
256         pod->firmware_version =
257                 (init_bytes[0] << 16) | (init_bytes[1] << 8) | (init_bytes[2] << 0);
258
259         for (i = 0; i <= 16; i++) {
260                 ret = line6_read_data(&pod->line6, 0xf000 + 0x08 * i, init_bytes, 8);
261                 if (ret < 0)
262                         goto exit;
263         }
264
265         ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0),
266                                         USB_REQ_SET_FEATURE,
267                                         USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_DIR_OUT,
268                                         1, 0,
269                                         NULL, 0, LINE6_TIMEOUT);
270 exit:
271         kfree(init_bytes);
272         return ret;
273 }
274
275 static void podhd_startup_workqueue(struct work_struct *work)
276 {
277         struct usb_line6_podhd *pod =
278             container_of(work, struct usb_line6_podhd, startup_work);
279
280         CHECK_STARTUP_PROGRESS(pod->startup_progress, PODHD_STARTUP_SETUP);
281
282         podhd_dev_start(pod);
283         line6_read_serial_number(&pod->line6, &pod->serial_number);
284
285         podhd_startup_finalize(pod);
286 }
287
288 static int podhd_startup_finalize(struct usb_line6_podhd *pod)
289 {
290         struct usb_line6 *line6 = &pod->line6;
291
292         /* ALSA audio interface: */
293         return snd_card_register(line6->card);
294 }
295
296 static void podhd_disconnect(struct usb_line6 *line6)
297 {
298         struct usb_line6_podhd *pod = (struct usb_line6_podhd *)line6;
299
300         if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL_INFO) {
301                 struct usb_interface *intf;
302
303                 del_timer_sync(&pod->startup_timer);
304                 cancel_work_sync(&pod->startup_work);
305
306                 intf = usb_ifnum_to_if(line6->usbdev,
307                                         pod->line6.properties->ctrl_if);
308                 if (intf)
309                         usb_driver_release_interface(&podhd_driver, intf);
310         }
311 }
312
313 /*
314         Try to init POD HD device.
315 */
316 static int podhd_init(struct usb_line6 *line6,
317                       const struct usb_device_id *id)
318 {
319         int err;
320         struct usb_line6_podhd *pod = (struct usb_line6_podhd *) line6;
321         struct usb_interface *intf;
322
323         line6->disconnect = podhd_disconnect;
324
325         timer_setup(&pod->startup_timer, NULL, 0);
326         INIT_WORK(&pod->startup_work, podhd_startup_workqueue);
327
328         if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL) {
329                 /* claim the data interface */
330                 intf = usb_ifnum_to_if(line6->usbdev,
331                                         pod->line6.properties->ctrl_if);
332                 if (!intf) {
333                         dev_err(pod->line6.ifcdev, "interface %d not found\n",
334                                 pod->line6.properties->ctrl_if);
335                         return -ENODEV;
336                 }
337
338                 err = usb_driver_claim_interface(&podhd_driver, intf, NULL);
339                 if (err != 0) {
340                         dev_err(pod->line6.ifcdev, "can't claim interface %d, error %d\n",
341                                 pod->line6.properties->ctrl_if, err);
342                         return err;
343                 }
344         }
345
346         if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL_INFO) {
347                 /* create sysfs entries: */
348                 err = snd_card_add_dev_attr(line6->card, &podhd_dev_attr_group);
349                 if (err < 0)
350                         return err;
351         }
352
353         if (pod->line6.properties->capabilities & LINE6_CAP_PCM) {
354                 /* initialize PCM subsystem: */
355                 err = line6_init_pcm(line6,
356                         (id->driver_info == LINE6_PODX3 ||
357                         id->driver_info == LINE6_PODX3LIVE) ? &podx3_pcm_properties :
358                         &podhd_pcm_properties);
359                 if (err < 0)
360                         return err;
361         }
362
363         if (!(pod->line6.properties->capabilities & LINE6_CAP_CONTROL_INFO)) {
364                 /* register USB audio system directly */
365                 return podhd_startup_finalize(pod);
366         }
367
368         /* init device and delay registering */
369         podhd_startup(pod);
370         return 0;
371 }
372
373 #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
374 #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
375
376 /* table of devices that work with this driver */
377 static const struct usb_device_id podhd_id_table[] = {
378         /* TODO: no need to alloc data interfaces when only audio is used */
379         { LINE6_DEVICE(0x5057),    .driver_info = LINE6_PODHD300 },
380         { LINE6_DEVICE(0x5058),    .driver_info = LINE6_PODHD400 },
381         { LINE6_IF_NUM(0x414D, 0), .driver_info = LINE6_PODHD500_0 },
382         { LINE6_IF_NUM(0x414D, 1), .driver_info = LINE6_PODHD500_1 },
383         { LINE6_IF_NUM(0x414A, 0), .driver_info = LINE6_PODX3 },
384         { LINE6_IF_NUM(0x414B, 0), .driver_info = LINE6_PODX3LIVE },
385         { LINE6_IF_NUM(0x4159, 0), .driver_info = LINE6_PODHD500X },
386         { LINE6_IF_NUM(0x4156, 0), .driver_info = LINE6_PODHDDESKTOP },
387         {}
388 };
389
390 MODULE_DEVICE_TABLE(usb, podhd_id_table);
391
392 static const struct line6_properties podhd_properties_table[] = {
393         [LINE6_PODHD300] = {
394                 .id = "PODHD300",
395                 .name = "POD HD300",
396                 .capabilities   = LINE6_CAP_PCM
397                                 | LINE6_CAP_HWMON,
398                 .altsetting = 5,
399                 .ep_ctrl_r = 0x84,
400                 .ep_ctrl_w = 0x03,
401                 .ep_audio_r = 0x82,
402                 .ep_audio_w = 0x01,
403         },
404         [LINE6_PODHD400] = {
405                 .id = "PODHD400",
406                 .name = "POD HD400",
407                 .capabilities   = LINE6_CAP_PCM
408                                 | LINE6_CAP_HWMON,
409                 .altsetting = 5,
410                 .ep_ctrl_r = 0x84,
411                 .ep_ctrl_w = 0x03,
412                 .ep_audio_r = 0x82,
413                 .ep_audio_w = 0x01,
414         },
415         [LINE6_PODHD500_0] = {
416                 .id = "PODHD500",
417                 .name = "POD HD500",
418                 .capabilities   = LINE6_CAP_PCM
419                                 | LINE6_CAP_HWMON,
420                 .altsetting = 0,
421                 .ep_ctrl_r = 0x81,
422                 .ep_ctrl_w = 0x01,
423                 .ep_audio_r = 0x86,
424                 .ep_audio_w = 0x02,
425         },
426         [LINE6_PODHD500_1] = {
427                 .id = "PODHD500",
428                 .name = "POD HD500",
429                 .capabilities   = LINE6_CAP_PCM
430                                 | LINE6_CAP_HWMON,
431                 .altsetting = 1,
432                 .ep_ctrl_r = 0x81,
433                 .ep_ctrl_w = 0x01,
434                 .ep_audio_r = 0x86,
435                 .ep_audio_w = 0x02,
436         },
437         [LINE6_PODX3] = {
438                 .id = "PODX3",
439                 .name = "POD X3",
440                 .capabilities   = LINE6_CAP_CONTROL | LINE6_CAP_CONTROL_INFO
441                                 | LINE6_CAP_PCM | LINE6_CAP_HWMON | LINE6_CAP_IN_NEEDS_OUT,
442                 .altsetting = 1,
443                 .ep_ctrl_r = 0x81,
444                 .ep_ctrl_w = 0x01,
445                 .ctrl_if = 1,
446                 .ep_audio_r = 0x86,
447                 .ep_audio_w = 0x02,
448         },
449         [LINE6_PODX3LIVE] = {
450                 .id = "PODX3LIVE",
451                 .name = "POD X3 LIVE",
452                 .capabilities   = LINE6_CAP_CONTROL | LINE6_CAP_CONTROL_INFO
453                                 | LINE6_CAP_PCM | LINE6_CAP_HWMON | LINE6_CAP_IN_NEEDS_OUT,
454                 .altsetting = 1,
455                 .ep_ctrl_r = 0x81,
456                 .ep_ctrl_w = 0x01,
457                 .ctrl_if = 1,
458                 .ep_audio_r = 0x86,
459                 .ep_audio_w = 0x02,
460         },
461         [LINE6_PODHD500X] = {
462                 .id = "PODHD500X",
463                 .name = "POD HD500X",
464                 .capabilities   = LINE6_CAP_CONTROL
465                                 | LINE6_CAP_PCM | LINE6_CAP_HWMON,
466                 .altsetting = 1,
467                 .ep_ctrl_r = 0x81,
468                 .ep_ctrl_w = 0x01,
469                 .ctrl_if = 1,
470                 .ep_audio_r = 0x86,
471                 .ep_audio_w = 0x02,
472         },
473         [LINE6_PODHDDESKTOP] = {
474                 .id = "PODHDDESKTOP",
475                 .name = "POD HDDESKTOP",
476                 .capabilities    = LINE6_CAP_CONTROL
477                         | LINE6_CAP_PCM | LINE6_CAP_HWMON,
478                 .altsetting = 1,
479                 .ep_ctrl_r = 0x81,
480                 .ep_ctrl_w = 0x01,
481                 .ctrl_if = 1,
482                 .ep_audio_r = 0x86,
483                 .ep_audio_w = 0x02,
484         },
485 };
486
487 /*
488         Probe USB device.
489 */
490 static int podhd_probe(struct usb_interface *interface,
491                        const struct usb_device_id *id)
492 {
493         return line6_probe(interface, id, "Line6-PODHD",
494                            &podhd_properties_table[id->driver_info],
495                            podhd_init, sizeof(struct usb_line6_podhd));
496 }
497
498 static struct usb_driver podhd_driver = {
499         .name = KBUILD_MODNAME,
500         .probe = podhd_probe,
501         .disconnect = line6_disconnect,
502 #ifdef CONFIG_PM
503         .suspend = line6_suspend,
504         .resume = line6_resume,
505         .reset_resume = line6_resume,
506 #endif
507         .id_table = podhd_id_table,
508 };
509
510 module_usb_driver(podhd_driver);
511
512 MODULE_DESCRIPTION("Line 6 PODHD USB driver");
513 MODULE_LICENSE("GPL");