Merge remote-tracking branch 'asoc/topic/ac97' into asoc-fsl
[deliverable/linux.git] / drivers / staging / line6 / pcm.c
CommitLineData
705ececd 1/*
e1a164d7 2 * Line6 Linux USB driver - 0.9.1beta
705ececd 3 *
1027f476 4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
705ececd
MG
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
5a0e3ad6 12#include <linux/slab.h>
705ececd
MG
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"
1027f476 20#include "driver.h"
705ececd
MG
21#include "playback.h"
22#include "pod.h"
23
1027f476
MG
24#ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
25
e1a164d7 26static struct snd_line6_pcm *dev2pcm(struct device *dev)
1027f476
MG
27{
28 struct usb_interface *interface = to_usb_interface(dev);
29 struct usb_line6 *line6 = usb_get_intfdata(interface);
30 struct snd_line6_pcm *line6pcm = line6->line6pcm;
31 return line6pcm;
32}
33
34/*
35 "read" request on "impulse_volume" special file.
36*/
37static ssize_t pcm_get_impulse_volume(struct device *dev,
e1a164d7 38 struct device_attribute *attr, char *buf)
1027f476
MG
39{
40 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_volume);
41}
42
43/*
44 "write" request on "impulse_volume" special file.
45*/
46static ssize_t pcm_set_impulse_volume(struct device *dev,
47 struct device_attribute *attr,
48 const char *buf, size_t count)
49{
50 struct snd_line6_pcm *line6pcm = dev2pcm(dev);
cdf5e551 51 int value;
a3762902 52 int ret;
cdf5e551 53
a3762902
LN
54 ret = kstrtoint(buf, 10, &value);
55 if (ret < 0)
56 return ret;
cdf5e551 57
1027f476
MG
58 line6pcm->impulse_volume = value;
59
e1a164d7 60 if (value > 0)
0ca54888 61 line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
1027f476 62 else
0ca54888 63 line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
1027f476
MG
64
65 return count;
66}
67
68/*
69 "read" request on "impulse_period" special file.
70*/
71static ssize_t pcm_get_impulse_period(struct device *dev,
e1a164d7 72 struct device_attribute *attr, char *buf)
1027f476
MG
73{
74 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_period);
75}
76
77/*
78 "write" request on "impulse_period" special file.
79*/
80static ssize_t pcm_set_impulse_period(struct device *dev,
81 struct device_attribute *attr,
82 const char *buf, size_t count)
83{
a3762902
LN
84 int value;
85 int ret;
86
87 ret = kstrtoint(buf, 10, &value);
88 if (ret < 0)
89 return ret;
90
91 dev2pcm(dev)->impulse_period = value;
1027f476
MG
92 return count;
93}
94
a3a972a0 95static DEVICE_ATTR(impulse_volume, S_IWUSR | S_IRUGO, pcm_get_impulse_volume,
e1a164d7 96 pcm_set_impulse_volume);
a3a972a0 97static DEVICE_ATTR(impulse_period, S_IWUSR | S_IRUGO, pcm_get_impulse_period,
e1a164d7 98 pcm_set_impulse_period);
1027f476
MG
99
100#endif
101
6b02a17e
MG
102static bool test_flags(unsigned long flags0, unsigned long flags1,
103 unsigned long mask)
104{
105 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
106}
107
0ca54888 108int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
1027f476 109{
9f613601
AB
110 unsigned long flags_old, flags_new, flags_final;
111 int err;
112
113 do {
114 flags_old = ACCESS_ONCE(line6pcm->flags);
115 flags_new = flags_old | channels;
116 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
117
118 flags_final = flags_old;
82a74d4a 119
1027f476 120 line6pcm->prev_fbuf = NULL;
e1a164d7 121
0ca54888 122 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
928f25ee 123 /* Invoked multiple times in a row so allocate once only */
0ca54888
MG
124 if (!line6pcm->buffer_in) {
125 line6pcm->buffer_in =
126 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
127 line6pcm->max_packet_size, GFP_KERNEL);
0ca54888 128 if (!line6pcm->buffer_in) {
0ca54888
MG
129 err = -ENOMEM;
130 goto pcm_acquire_error;
131 }
132
133 flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
134 }
135 }
136
137 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
1027f476 138 /*
e1a164d7
MG
139 Waiting for completion of active URBs in the stop handler is
140 a bug, we therefore report an error if capturing is restarted
141 too soon.
142 */
0ca54888
MG
143 if (line6pcm->active_urb_in | line6pcm->unlink_urb_in) {
144 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
1027f476 145 return -EBUSY;
6b02a17e
MG
146 }
147
1027f476
MG
148 line6pcm->count_in = 0;
149 line6pcm->prev_fsize = 0;
150 err = line6_submit_audio_in_all_urbs(line6pcm);
e1a164d7 151
6b02a17e 152 if (err < 0)
0ca54888
MG
153 goto pcm_acquire_error;
154
155 flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
1027f476 156 }
e1a164d7 157
0ca54888 158 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
928f25ee 159 /* Invoked multiple times in a row so allocate once only */
0ca54888
MG
160 if (!line6pcm->buffer_out) {
161 line6pcm->buffer_out =
162 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
163 line6pcm->max_packet_size, GFP_KERNEL);
0ca54888 164 if (!line6pcm->buffer_out) {
0ca54888
MG
165 err = -ENOMEM;
166 goto pcm_acquire_error;
167 }
1027f476 168
0ca54888
MG
169 flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
170 }
171 }
6b02a17e 172
0ca54888
MG
173 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
174 /*
175 See comment above regarding PCM restart.
176 */
177 if (line6pcm->active_urb_out | line6pcm->unlink_urb_out) {
178 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
179 return -EBUSY;
6b02a17e
MG
180 }
181
1027f476
MG
182 line6pcm->count_out = 0;
183 err = line6_submit_audio_out_all_urbs(line6pcm);
e1a164d7 184
6b02a17e 185 if (err < 0)
0ca54888
MG
186 goto pcm_acquire_error;
187
188 flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
1027f476 189 }
e1a164d7 190
1027f476 191 return 0;
6b02a17e 192
0ca54888
MG
193pcm_acquire_error:
194 /*
195 If not all requested resources/streams could be obtained, release
196 those which were successfully obtained (if any).
197 */
198 line6_pcm_release(line6pcm, flags_final & channels);
6b02a17e 199 return err;
1027f476
MG
200}
201
0ca54888 202int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
1027f476 203{
9f613601
AB
204 unsigned long flags_old, flags_new;
205
206 do {
207 flags_old = ACCESS_ONCE(line6pcm->flags);
208 flags_new = flags_old & ~channels;
209 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
1027f476 210
0ca54888 211 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM))
1027f476 212 line6_unlink_audio_in_urbs(line6pcm);
6b02a17e 213
0ca54888
MG
214 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
215 line6_wait_clear_audio_in_urbs(line6pcm);
216 line6_free_capture_buffer(line6pcm);
1027f476
MG
217 }
218
0ca54888 219 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
1027f476 220 line6_unlink_audio_out_urbs(line6pcm);
6b02a17e 221
0ca54888
MG
222 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
223 line6_wait_clear_audio_out_urbs(line6pcm);
224 line6_free_playback_buffer(line6pcm);
1027f476 225 }
1027f476
MG
226
227 return 0;
228}
229
705ececd
MG
230/* trigger callback */
231int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
232{
233 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
705ececd
MG
234 struct snd_pcm_substream *s;
235 int err;
236 unsigned long flags;
237
238 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
0ca54888 239 clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
705ececd 240
705ececd 241 snd_pcm_group_for_each_entry(s, substream) {
68dc3dde 242 switch (s->stream) {
705ececd 243 case SNDRV_PCM_STREAM_PLAYBACK:
1027f476 244 err = snd_line6_playback_trigger(line6pcm, cmd);
705ececd 245
68dc3dde
GKH
246 if (err < 0) {
247 spin_unlock_irqrestore(&line6pcm->lock_trigger,
248 flags);
705ececd
MG
249 return err;
250 }
251
252 break;
253
254 case SNDRV_PCM_STREAM_CAPTURE:
1027f476 255 err = snd_line6_capture_trigger(line6pcm, cmd);
705ececd 256
68dc3dde
GKH
257 if (err < 0) {
258 spin_unlock_irqrestore(&line6pcm->lock_trigger,
259 flags);
705ececd
MG
260 return err;
261 }
262
263 break;
264
265 default:
e1a164d7
MG
266 dev_err(line6pcm->line6->ifcdev,
267 "Unknown stream direction %d\n", s->stream);
705ececd
MG
268 }
269 }
270
271 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
272 return 0;
273}
274
275/* control info callback */
1027f476
MG
276static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
277 struct snd_ctl_elem_info *uinfo)
68dc3dde 278{
705ececd
MG
279 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
280 uinfo->count = 2;
281 uinfo->value.integer.min = 0;
282 uinfo->value.integer.max = 256;
283 return 0;
284}
285
286/* control get callback */
1027f476
MG
287static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
288 struct snd_ctl_elem_value *ucontrol)
68dc3dde 289{
705ececd
MG
290 int i;
291 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
292
68dc3dde 293 for (i = 2; i--;)
1027f476 294 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
705ececd
MG
295
296 return 0;
297}
298
299/* control put callback */
1027f476
MG
300static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
301 struct snd_ctl_elem_value *ucontrol)
68dc3dde 302{
705ececd
MG
303 int i, changed = 0;
304 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
305
68dc3dde 306 for (i = 2; i--;)
e1a164d7
MG
307 if (line6pcm->volume_playback[i] !=
308 ucontrol->value.integer.value[i]) {
309 line6pcm->volume_playback[i] =
310 ucontrol->value.integer.value[i];
705ececd
MG
311 changed = 1;
312 }
313
314 return changed;
315}
316
317/* control definition */
1027f476 318static struct snd_kcontrol_new line6_control_playback = {
705ececd
MG
319 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
320 .name = "PCM Playback Volume",
321 .index = 0,
322 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1027f476
MG
323 .info = snd_line6_control_playback_info,
324 .get = snd_line6_control_playback_get,
325 .put = snd_line6_control_playback_put
705ececd
MG
326};
327
328/*
329 Cleanup the PCM device.
330*/
331static void line6_cleanup_pcm(struct snd_pcm *pcm)
332{
333 int i;
334 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
335
1027f476
MG
336#ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
337 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
338 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
339#endif
340
68dc3dde
GKH
341 for (i = LINE6_ISO_BUFFERS; i--;) {
342 if (line6pcm->urb_audio_out[i]) {
705ececd
MG
343 usb_kill_urb(line6pcm->urb_audio_out[i]);
344 usb_free_urb(line6pcm->urb_audio_out[i]);
345 }
68dc3dde 346 if (line6pcm->urb_audio_in[i]) {
705ececd
MG
347 usb_kill_urb(line6pcm->urb_audio_in[i]);
348 usb_free_urb(line6pcm->urb_audio_in[i]);
349 }
350 }
351}
352
353/* create a PCM device */
354static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
355{
356 struct snd_pcm *pcm;
357 int err;
358
68dc3dde 359 err = snd_pcm_new(line6pcm->line6->card,
e1a164d7
MG
360 (char *)line6pcm->line6->properties->name,
361 0, 1, 1, &pcm);
68dc3dde 362 if (err < 0)
705ececd
MG
363 return err;
364
365 pcm->private_data = line6pcm;
366 pcm->private_free = line6_cleanup_pcm;
367 line6pcm->pcm = pcm;
368 strcpy(pcm->name, line6pcm->line6->properties->name);
369
370 /* set operators */
afb9091d
SB
371 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
372 &snd_line6_playback_ops);
e1a164d7 373 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
705ececd
MG
374
375 /* pre-allocation of buffers */
68dc3dde 376 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
e1a164d7
MG
377 snd_dma_continuous_data
378 (GFP_KERNEL), 64 * 1024,
379 128 * 1024);
705ececd
MG
380
381 return 0;
382}
383
384/* PCM device destructor */
385static int snd_line6_pcm_free(struct snd_device *device)
386{
387 return 0;
388}
389
1027f476
MG
390/*
391 Stop substream if still running.
392*/
393static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
394{
86f0b5b8
TI
395 if (substream->runtime && snd_pcm_running(substream)) {
396 snd_pcm_stream_lock_irq(substream);
1027f476 397 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
86f0b5b8
TI
398 snd_pcm_stream_unlock_irq(substream);
399 }
1027f476
MG
400}
401
402/*
403 Stop PCM stream.
404*/
405void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
406{
e1a164d7
MG
407 pcm_disconnect_substream(get_substream
408 (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
409 pcm_disconnect_substream(get_substream
410 (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
1027f476
MG
411 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
412 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
413}
414
705ececd
MG
415/*
416 Create and register the PCM device and mixer entries.
417 Create URBs for playback and capture.
418*/
68dc3dde
GKH
419int line6_init_pcm(struct usb_line6 *line6,
420 struct line6_pcm_properties *properties)
705ececd
MG
421{
422 static struct snd_device_ops pcm_ops = {
423 .dev_free = snd_line6_pcm_free,
424 };
425
426 int err;
427 int ep_read = 0, ep_write = 0;
428 struct snd_line6_pcm *line6pcm;
429
68dc3dde 430 if (!(line6->properties->capabilities & LINE6_BIT_PCM))
e1a164d7 431 return 0; /* skip PCM initialization and report success */
705ececd
MG
432
433 /* initialize PCM subsystem based on product id: */
68dc3dde 434 switch (line6->product) {
705ececd 435 case LINE6_DEVID_BASSPODXT:
68dc3dde
GKH
436 case LINE6_DEVID_BASSPODXTLIVE:
437 case LINE6_DEVID_BASSPODXTPRO:
438 case LINE6_DEVID_PODXT:
439 case LINE6_DEVID_PODXTLIVE:
440 case LINE6_DEVID_PODXTPRO:
16dc1040 441 case LINE6_DEVID_PODHD300:
e1a164d7 442 ep_read = 0x82;
705ececd
MG
443 ep_write = 0x01;
444 break;
445
4c6fb5fc 446 case LINE6_DEVID_PODHD500:
68dc3dde
GKH
447 case LINE6_DEVID_PODX3:
448 case LINE6_DEVID_PODX3LIVE:
e1a164d7 449 ep_read = 0x86;
705ececd
MG
450 ep_write = 0x02;
451 break;
452
68dc3dde 453 case LINE6_DEVID_POCKETPOD:
e1a164d7 454 ep_read = 0x82;
705ececd
MG
455 ep_write = 0x02;
456 break;
457
68dc3dde 458 case LINE6_DEVID_GUITARPORT:
1027f476
MG
459 case LINE6_DEVID_PODSTUDIO_GX:
460 case LINE6_DEVID_PODSTUDIO_UX1:
461 case LINE6_DEVID_PODSTUDIO_UX2:
705ececd 462 case LINE6_DEVID_TONEPORT_GX:
1027f476
MG
463 case LINE6_DEVID_TONEPORT_UX1:
464 case LINE6_DEVID_TONEPORT_UX2:
e1a164d7 465 ep_read = 0x82;
705ececd
MG
466 ep_write = 0x01;
467 break;
468
a3762902
LN
469 /* this is for interface_number == 1:
470 case LINE6_DEVID_TONEPORT_UX2:
471 case LINE6_DEVID_PODSTUDIO_UX2:
472 ep_read = 0x87;
473 ep_write = 0x00;
474 break; */
705ececd
MG
475
476 default:
477 MISSING_CASE;
478 }
479
480 line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
481
68dc3dde 482 if (line6pcm == NULL)
705ececd
MG
483 return -ENOMEM;
484
1027f476
MG
485 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
486 line6pcm->volume_monitor = 255;
705ececd
MG
487 line6pcm->line6 = line6;
488 line6pcm->ep_audio_read = ep_read;
489 line6pcm->ep_audio_write = ep_write;
3b08db37
SH
490
491 /* Read and write buffers are sized identically, so choose minimum */
492 line6pcm->max_packet_size = min(
493 usb_maxpacket(line6->usbdev,
494 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
495 usb_maxpacket(line6->usbdev,
496 usb_sndisocpipe(line6->usbdev, ep_write), 1));
497
705ececd
MG
498 line6pcm->properties = properties;
499 line6->line6pcm = line6pcm;
500
501 /* PCM device: */
68dc3dde
GKH
502 err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
503 if (err < 0)
705ececd
MG
504 return err;
505
506 snd_card_set_dev(line6->card, line6->ifcdev);
507
68dc3dde
GKH
508 err = snd_line6_new_pcm(line6pcm);
509 if (err < 0)
705ececd
MG
510 return err;
511
512 spin_lock_init(&line6pcm->lock_audio_out);
513 spin_lock_init(&line6pcm->lock_audio_in);
514 spin_lock_init(&line6pcm->lock_trigger);
515
1027f476 516 err = line6_create_audio_out_urbs(line6pcm);
68dc3dde 517 if (err < 0)
705ececd
MG
518 return err;
519
1027f476 520 err = line6_create_audio_in_urbs(line6pcm);
68dc3dde 521 if (err < 0)
705ececd
MG
522 return err;
523
524 /* mixer: */
e1a164d7
MG
525 err =
526 snd_ctl_add(line6->card,
527 snd_ctl_new1(&line6_control_playback, line6pcm));
1027f476
MG
528 if (err < 0)
529 return err;
530
531#ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
532 /* impulse response test: */
533 err = device_create_file(line6->ifcdev, &dev_attr_impulse_volume);
68dc3dde 534 if (err < 0)
705ececd
MG
535 return err;
536
1027f476
MG
537 err = device_create_file(line6->ifcdev, &dev_attr_impulse_period);
538 if (err < 0)
539 return err;
540
541 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
542#endif
543
705ececd
MG
544 return 0;
545}
546
547/* prepare pcm callback */
548int snd_line6_prepare(struct snd_pcm_substream *substream)
549{
550 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
551
665f3f50
SH
552 switch (substream->stream) {
553 case SNDRV_PCM_STREAM_PLAYBACK:
0ca54888 554 if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
6b02a17e
MG
555 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
556
665f3f50
SH
557 break;
558
559 case SNDRV_PCM_STREAM_CAPTURE:
0ca54888 560 if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
6b02a17e
MG
561 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
562
665f3f50
SH
563 break;
564
565 default:
566 MISSING_CASE;
567 }
568
0ca54888 569 if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
1027f476 570 line6pcm->count_out = 0;
705ececd
MG
571 line6pcm->pos_out = 0;
572 line6pcm->pos_out_done = 0;
705ececd 573 line6pcm->bytes_out = 0;
1027f476 574 line6pcm->count_in = 0;
705ececd
MG
575 line6pcm->pos_in_done = 0;
576 line6pcm->bytes_in = 0;
577 }
578
579 return 0;
580}
This page took 0.436608 seconds and 5 git commands to generate.