staging: line6: sync with upstream
[firefly-linux-kernel-4.4.55.git] / drivers / staging / line6 / pcm.c
1 /*
2  * Line6 Linux USB driver - 0.9.0
3  *
4  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License as
8  *      published by the Free Software Foundation, version 2.
9  *
10  */
11
12 #include <linux/slab.h>
13 #include <sound/core.h>
14 #include <sound/control.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17
18 #include "audio.h"
19 #include "capture.h"
20 #include "driver.h"
21 #include "playback.h"
22 #include "pod.h"
23
24
25 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
26
27 static struct snd_line6_pcm* dev2pcm(struct device *dev)
28 {
29         struct usb_interface *interface = to_usb_interface(dev);
30         struct usb_line6 *line6 = usb_get_intfdata(interface);
31         struct snd_line6_pcm *line6pcm = line6->line6pcm;
32         return line6pcm;
33 }
34
35 /*
36         "read" request on "impulse_volume" special file.
37 */
38 static ssize_t pcm_get_impulse_volume(struct device *dev,
39                                       struct device_attribute *attr,
40                                       char *buf)
41 {
42         return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_volume);
43 }
44
45 /*
46         "write" request on "impulse_volume" special file.
47 */
48 static ssize_t pcm_set_impulse_volume(struct device *dev,
49                                       struct device_attribute *attr,
50                                       const char *buf, size_t count)
51 {
52         struct snd_line6_pcm *line6pcm = dev2pcm(dev);
53         int value = simple_strtoul(buf, NULL, 10);
54         line6pcm->impulse_volume = value;
55
56         if(value > 0)
57                 line6_pcm_start(line6pcm, MASK_PCM_IMPULSE);
58         else
59                 line6_pcm_stop(line6pcm, MASK_PCM_IMPULSE);
60
61         return count;
62 }
63
64 /*
65         "read" request on "impulse_period" special file.
66 */
67 static ssize_t pcm_get_impulse_period(struct device *dev,
68                                       struct device_attribute *attr,
69                                       char *buf)
70 {
71         return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_period);
72 }
73
74 /*
75         "write" request on "impulse_period" special file.
76 */
77 static ssize_t pcm_set_impulse_period(struct device *dev,
78                                       struct device_attribute *attr,
79                                       const char *buf, size_t count)
80 {
81         dev2pcm(dev)->impulse_period = simple_strtoul(buf, NULL, 10);
82         return count;
83 }
84
85 static DEVICE_ATTR(impulse_volume, S_IWUGO | S_IRUGO, pcm_get_impulse_volume, pcm_set_impulse_volume);
86 static DEVICE_ATTR(impulse_period, S_IWUGO | S_IRUGO, pcm_get_impulse_period, pcm_set_impulse_period);
87
88 #endif
89
90 int line6_pcm_start(struct snd_line6_pcm *line6pcm, int channels)
91 {
92         unsigned long flags_old = __sync_fetch_and_or(&line6pcm->flags, channels);
93         unsigned long flags_new = flags_old | channels;
94         int err = 0;
95
96 #if LINE6_BACKUP_MONITOR_SIGNAL
97         if (!(line6pcm->line6->properties->capabilities & LINE6_BIT_HWMON)) {
98                 line6pcm->prev_fbuf = kmalloc(LINE6_ISO_PACKETS * line6pcm->max_packet_size, GFP_KERNEL);
99
100                 if (!line6pcm->prev_fbuf) {
101                         dev_err(line6pcm->line6->ifcdev, "cannot malloc monitor buffer\n");
102                         return -ENOMEM;
103                 }
104         }
105 #else
106         line6pcm->prev_fbuf = NULL;
107 #endif
108                 
109         if (((flags_old & MASK_CAPTURE) == 0) &&
110             ((flags_new & MASK_CAPTURE) != 0)) {
111                 /*
112                   Waiting for completion of active URBs in the stop handler is
113                   a bug, we therefore report an error if capturing is restarted
114                   too soon.
115                 */
116                 if(line6pcm->active_urb_in | line6pcm->unlink_urb_in)
117                         return -EBUSY;
118
119                 line6pcm->buffer_in = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS * line6pcm->max_packet_size, GFP_KERNEL);
120
121                 if (!line6pcm->buffer_in) {
122                         dev_err(line6pcm->line6->ifcdev, "cannot malloc capture buffer\n");
123                         return -ENOMEM;
124                 }
125
126                 line6pcm->count_in = 0;
127                 line6pcm->prev_fsize = 0;
128                 err = line6_submit_audio_in_all_urbs(line6pcm);
129                 
130                 if (err < 0) {
131                         __sync_fetch_and_and(&line6pcm->flags, ~channels);
132                         return err;
133                 }
134         }
135         
136         if (((flags_old & MASK_PLAYBACK) == 0) &&
137             ((flags_new & MASK_PLAYBACK) != 0)) {
138                 /*
139                   See comment above regarding PCM restart.
140                 */
141                 if(line6pcm->active_urb_out | line6pcm->unlink_urb_out)
142                         return -EBUSY;
143
144                 line6pcm->buffer_out = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS * line6pcm->max_packet_size, GFP_KERNEL);
145
146                 if (!line6pcm->buffer_out) {
147                         dev_err(line6pcm->line6->ifcdev, "cannot malloc playback buffer\n");
148                         return -ENOMEM;
149                 }
150
151                 line6pcm->count_out = 0;
152                 err = line6_submit_audio_out_all_urbs(line6pcm);
153                 
154                 if (err < 0) {
155                         __sync_fetch_and_and(&line6pcm->flags, ~channels);
156                         return err;
157                 }
158         }
159         
160         return 0;
161 }
162
163 int line6_pcm_stop(struct snd_line6_pcm *line6pcm, int channels)
164 {
165         unsigned long flags_old = __sync_fetch_and_and(&line6pcm->flags, ~channels);
166         unsigned long flags_new = flags_old & ~channels;
167
168         if (((flags_old & MASK_CAPTURE) != 0) &&
169             ((flags_new & MASK_CAPTURE) == 0)) {
170                 line6_unlink_audio_in_urbs(line6pcm);
171                 kfree(line6pcm->buffer_in);
172                 line6pcm->buffer_in = NULL;
173         }
174
175         if (((flags_old & MASK_PLAYBACK) != 0) &&
176             ((flags_new & MASK_PLAYBACK) == 0)) {
177                 line6_unlink_audio_out_urbs(line6pcm);
178                 kfree(line6pcm->buffer_out);
179                 line6pcm->buffer_out = NULL;
180         }
181
182 #if LINE6_BACKUP_MONITOR_SIGNAL
183         if (line6pcm->prev_fbuf != NULL)
184                 kfree(line6pcm->prev_fbuf);
185 #endif
186
187         return 0;
188 }
189
190 /* trigger callback */
191 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
192 {
193         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
194         struct snd_pcm_substream *s;
195         int err;
196         unsigned long flags;
197
198         spin_lock_irqsave(&line6pcm->lock_trigger, flags);
199         clear_bit(BIT_PREPARED, &line6pcm->flags);
200
201         snd_pcm_group_for_each_entry(s, substream) {
202                 switch (s->stream) {
203                 case SNDRV_PCM_STREAM_PLAYBACK:
204                         err = snd_line6_playback_trigger(line6pcm, cmd);
205
206                         if (err < 0) {
207                                 spin_unlock_irqrestore(&line6pcm->lock_trigger,
208                                                        flags);
209                                 return err;
210                         }
211
212                         break;
213
214                 case SNDRV_PCM_STREAM_CAPTURE:
215                         err = snd_line6_capture_trigger(line6pcm, cmd);
216
217                         if (err < 0) {
218                                 spin_unlock_irqrestore(&line6pcm->lock_trigger,
219                                                        flags);
220                                 return err;
221                         }
222
223                         break;
224
225                 default:
226                         dev_err(line6pcm->line6->ifcdev, "Unknown stream direction %d\n",
227                                 s->stream);
228                 }
229         }
230
231         spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
232         return 0;
233 }
234
235 /* control info callback */
236 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
237                                            struct snd_ctl_elem_info *uinfo)
238 {
239         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
240         uinfo->count = 2;
241         uinfo->value.integer.min = 0;
242         uinfo->value.integer.max = 256;
243         return 0;
244 }
245
246 /* control get callback */
247 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
248                                           struct snd_ctl_elem_value *ucontrol)
249 {
250         int i;
251         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
252
253         for (i = 2; i--;)
254                 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
255
256         return 0;
257 }
258
259 /* control put callback */
260 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
261                                           struct snd_ctl_elem_value *ucontrol)
262 {
263         int i, changed = 0;
264         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
265
266         for (i = 2; i--;)
267                 if (line6pcm->volume_playback[i] != ucontrol->value.integer.value[i]) {
268                         line6pcm->volume_playback[i] = ucontrol->value.integer.value[i];
269                         changed = 1;
270                 }
271
272         return changed;
273 }
274
275 /* control definition */
276 static struct snd_kcontrol_new line6_control_playback = {
277         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
278         .name = "PCM Playback Volume",
279         .index = 0,
280         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
281         .info = snd_line6_control_playback_info,
282         .get = snd_line6_control_playback_get,
283         .put = snd_line6_control_playback_put
284 };
285
286 /*
287         Cleanup the PCM device.
288 */
289 static void line6_cleanup_pcm(struct snd_pcm *pcm)
290 {
291         int i;
292         struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
293
294 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
295         device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
296         device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
297 #endif
298
299         for (i = LINE6_ISO_BUFFERS; i--;) {
300                 if (line6pcm->urb_audio_out[i]) {
301                         usb_kill_urb(line6pcm->urb_audio_out[i]);
302                         usb_free_urb(line6pcm->urb_audio_out[i]);
303                 }
304                 if (line6pcm->urb_audio_in[i]) {
305                         usb_kill_urb(line6pcm->urb_audio_in[i]);
306                         usb_free_urb(line6pcm->urb_audio_in[i]);
307                 }
308         }
309 }
310
311 /* create a PCM device */
312 static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
313 {
314         struct snd_pcm *pcm;
315         int err;
316
317         err = snd_pcm_new(line6pcm->line6->card,
318                          (char *)line6pcm->line6->properties->name,
319                          0, 1, 1, &pcm);
320         if (err < 0)
321                 return err;
322
323         pcm->private_data = line6pcm;
324         pcm->private_free = line6_cleanup_pcm;
325         line6pcm->pcm = pcm;
326         strcpy(pcm->name, line6pcm->line6->properties->name);
327
328         /* set operators */
329         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
330                         &snd_line6_playback_ops);
331         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
332                         &snd_line6_capture_ops);
333
334         /* pre-allocation of buffers */
335         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
336                                         snd_dma_continuous_data(GFP_KERNEL),
337                                         64 * 1024, 128 * 1024);
338
339         return 0;
340 }
341
342 /* PCM device destructor */
343 static int snd_line6_pcm_free(struct snd_device *device)
344 {
345         return 0;
346 }
347
348 /*
349         Stop substream if still running.
350 */
351 static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
352 {
353         if(substream->runtime && snd_pcm_running(substream)) {
354                 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
355         }
356 }
357
358 /*
359         Stop PCM stream.
360 */
361 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
362 {
363         pcm_disconnect_substream(get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE));
364         pcm_disconnect_substream(get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
365         line6_unlink_wait_clear_audio_out_urbs(line6pcm);
366         line6_unlink_wait_clear_audio_in_urbs(line6pcm);
367 }
368
369 /*
370         Create and register the PCM device and mixer entries.
371         Create URBs for playback and capture.
372 */
373 int line6_init_pcm(struct usb_line6 *line6,
374                    struct line6_pcm_properties *properties)
375 {
376         static struct snd_device_ops pcm_ops = {
377                 .dev_free = snd_line6_pcm_free,
378         };
379
380         int err;
381         int ep_read = 0, ep_write = 0;
382         struct snd_line6_pcm *line6pcm;
383
384         if (!(line6->properties->capabilities & LINE6_BIT_PCM))
385                 return 0;  /* skip PCM initialization and report success */
386
387         /* initialize PCM subsystem based on product id: */
388         switch (line6->product) {
389         case LINE6_DEVID_BASSPODXT:
390         case LINE6_DEVID_BASSPODXTLIVE:
391         case LINE6_DEVID_BASSPODXTPRO:
392         case LINE6_DEVID_PODXT:
393         case LINE6_DEVID_PODXTLIVE:
394         case LINE6_DEVID_PODXTPRO:
395                 ep_read  = 0x82;
396                 ep_write = 0x01;
397                 break;
398
399         case LINE6_DEVID_PODX3:
400         case LINE6_DEVID_PODX3LIVE:
401                 ep_read  = 0x86;
402                 ep_write = 0x02;
403                 break;
404
405         case LINE6_DEVID_POCKETPOD:
406                 ep_read  = 0x82;
407                 ep_write = 0x02;
408                 break;
409
410         case LINE6_DEVID_GUITARPORT:
411         case LINE6_DEVID_PODSTUDIO_GX:
412         case LINE6_DEVID_PODSTUDIO_UX1:
413         case LINE6_DEVID_PODSTUDIO_UX2:
414         case LINE6_DEVID_TONEPORT_GX:
415         case LINE6_DEVID_TONEPORT_UX1:
416         case LINE6_DEVID_TONEPORT_UX2:
417                 ep_read  = 0x82;
418                 ep_write = 0x01;
419                 break;
420
421                 /* this is for interface_number == 1:
422         case LINE6_DEVID_TONEPORT_UX2:
423         case LINE6_DEVID_PODSTUDIO_UX2:
424                 ep_read  = 0x87;
425                 ep_write = 0x00;
426                 break;
427                 */
428
429         default:
430                 MISSING_CASE;
431         }
432
433         line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
434
435         if (line6pcm == NULL)
436                 return -ENOMEM;
437
438         line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
439         line6pcm->volume_monitor = 255;
440         line6pcm->line6 = line6;
441         line6pcm->ep_audio_read = ep_read;
442         line6pcm->ep_audio_write = ep_write;
443         line6pcm->max_packet_size = usb_maxpacket(line6->usbdev,
444                                                   usb_rcvintpipe(line6->usbdev,
445                                                                 ep_read),
446                                                   0);
447         line6pcm->properties = properties;
448         line6->line6pcm = line6pcm;
449
450         /* PCM device: */
451         err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
452         if (err < 0)
453                 return err;
454
455         snd_card_set_dev(line6->card, line6->ifcdev);
456
457         err = snd_line6_new_pcm(line6pcm);
458         if (err < 0)
459                 return err;
460
461         spin_lock_init(&line6pcm->lock_audio_out);
462         spin_lock_init(&line6pcm->lock_audio_in);
463         spin_lock_init(&line6pcm->lock_trigger);
464
465         err = line6_create_audio_out_urbs(line6pcm);
466         if (err < 0)
467                 return err;
468
469         err = line6_create_audio_in_urbs(line6pcm);
470         if (err < 0)
471                 return err;
472
473         /* mixer: */
474         err = snd_ctl_add(line6->card, snd_ctl_new1(&line6_control_playback, line6pcm));
475         if (err < 0)
476                 return err;
477
478 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
479         /* impulse response test: */
480         err = device_create_file(line6->ifcdev, &dev_attr_impulse_volume);
481         if (err < 0)
482                 return err;
483
484         err = device_create_file(line6->ifcdev, &dev_attr_impulse_period);
485         if (err < 0)
486                 return err;
487
488         line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
489 #endif
490
491         return 0;
492 }
493
494 /* prepare pcm callback */
495 int snd_line6_prepare(struct snd_pcm_substream *substream)
496 {
497         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
498
499         if (!test_and_set_bit(BIT_PREPARED, &line6pcm->flags)) {
500                 line6pcm->count_out = 0;
501                 line6pcm->pos_out = 0;
502                 line6pcm->pos_out_done = 0;
503                 line6pcm->bytes_out = 0;
504                 line6pcm->count_in = 0;
505                 line6pcm->pos_in_done = 0;
506                 line6pcm->bytes_in = 0;
507         }
508
509         return 0;
510 }