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