net: ethernet: sun: initialize variables directly
[linux-2.6.git] / drivers / staging / line6 / pcm.c
blob02f77d74809f3d15572c82ad44b79ebac2dc33b0
1 /*
2 * Line6 Linux USB driver - 0.9.1beta
4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
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.
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>
18 #include "audio.h"
19 #include "capture.h"
20 #include "driver.h"
21 #include "playback.h"
22 #include "pod.h"
24 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
26 static struct snd_line6_pcm *dev2pcm(struct device *dev)
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;
35 "read" request on "impulse_volume" special file.
37 static ssize_t pcm_get_impulse_volume(struct device *dev,
38 struct device_attribute *attr, char *buf)
40 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_volume);
44 "write" request on "impulse_volume" special file.
46 static ssize_t pcm_set_impulse_volume(struct device *dev,
47 struct device_attribute *attr,
48 const char *buf, size_t count)
50 struct snd_line6_pcm *line6pcm = dev2pcm(dev);
51 int value;
52 int ret;
54 ret = kstrtoint(buf, 10, &value);
55 if (ret < 0)
56 return ret;
58 line6pcm->impulse_volume = value;
60 if (value > 0)
61 line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
62 else
63 line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
65 return count;
69 "read" request on "impulse_period" special file.
71 static ssize_t pcm_get_impulse_period(struct device *dev,
72 struct device_attribute *attr, char *buf)
74 return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_period);
78 "write" request on "impulse_period" special file.
80 static ssize_t pcm_set_impulse_period(struct device *dev,
81 struct device_attribute *attr,
82 const char *buf, size_t count)
84 int value;
85 int ret;
87 ret = kstrtoint(buf, 10, &value);
88 if (ret < 0)
89 return ret;
91 dev2pcm(dev)->impulse_period = value;
92 return count;
95 static DEVICE_ATTR(impulse_volume, S_IWUSR | S_IRUGO, pcm_get_impulse_volume,
96 pcm_set_impulse_volume);
97 static DEVICE_ATTR(impulse_period, S_IWUSR | S_IRUGO, pcm_get_impulse_period,
98 pcm_set_impulse_period);
100 #endif
102 static bool test_flags(unsigned long flags0, unsigned long flags1,
103 unsigned long mask)
105 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
108 int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
110 unsigned long flags_old =
111 __sync_fetch_and_or(&line6pcm->flags, channels);
112 unsigned long flags_new = flags_old | channels;
113 unsigned long flags_final = flags_old;
114 int err = 0;
116 line6pcm->prev_fbuf = NULL;
118 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
119 /* Invoked multiple times in a row so allocate once only */
120 if (!line6pcm->buffer_in) {
121 line6pcm->buffer_in =
122 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
123 line6pcm->max_packet_size, GFP_KERNEL);
124 if (!line6pcm->buffer_in) {
125 err = -ENOMEM;
126 goto pcm_acquire_error;
129 flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
133 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
135 Waiting for completion of active URBs in the stop handler is
136 a bug, we therefore report an error if capturing is restarted
137 too soon.
139 if (line6pcm->active_urb_in | line6pcm->unlink_urb_in) {
140 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
141 return -EBUSY;
144 line6pcm->count_in = 0;
145 line6pcm->prev_fsize = 0;
146 err = line6_submit_audio_in_all_urbs(line6pcm);
148 if (err < 0)
149 goto pcm_acquire_error;
151 flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
154 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
155 /* Invoked multiple times in a row so allocate once only */
156 if (!line6pcm->buffer_out) {
157 line6pcm->buffer_out =
158 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
159 line6pcm->max_packet_size, GFP_KERNEL);
160 if (!line6pcm->buffer_out) {
161 err = -ENOMEM;
162 goto pcm_acquire_error;
165 flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
169 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
171 See comment above regarding PCM restart.
173 if (line6pcm->active_urb_out | line6pcm->unlink_urb_out) {
174 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
175 return -EBUSY;
178 line6pcm->count_out = 0;
179 err = line6_submit_audio_out_all_urbs(line6pcm);
181 if (err < 0)
182 goto pcm_acquire_error;
184 flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
187 return 0;
189 pcm_acquire_error:
191 If not all requested resources/streams could be obtained, release
192 those which were successfully obtained (if any).
194 line6_pcm_release(line6pcm, flags_final & channels);
195 return err;
198 int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
200 unsigned long flags_old =
201 __sync_fetch_and_and(&line6pcm->flags, ~channels);
202 unsigned long flags_new = flags_old & ~channels;
204 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM))
205 line6_unlink_audio_in_urbs(line6pcm);
207 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
208 line6_wait_clear_audio_in_urbs(line6pcm);
209 line6_free_capture_buffer(line6pcm);
212 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
213 line6_unlink_audio_out_urbs(line6pcm);
215 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
216 line6_wait_clear_audio_out_urbs(line6pcm);
217 line6_free_playback_buffer(line6pcm);
220 return 0;
223 /* trigger callback */
224 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
226 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
227 struct snd_pcm_substream *s;
228 int err;
229 unsigned long flags;
231 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
232 clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
234 snd_pcm_group_for_each_entry(s, substream) {
235 switch (s->stream) {
236 case SNDRV_PCM_STREAM_PLAYBACK:
237 err = snd_line6_playback_trigger(line6pcm, cmd);
239 if (err < 0) {
240 spin_unlock_irqrestore(&line6pcm->lock_trigger,
241 flags);
242 return err;
245 break;
247 case SNDRV_PCM_STREAM_CAPTURE:
248 err = snd_line6_capture_trigger(line6pcm, cmd);
250 if (err < 0) {
251 spin_unlock_irqrestore(&line6pcm->lock_trigger,
252 flags);
253 return err;
256 break;
258 default:
259 dev_err(line6pcm->line6->ifcdev,
260 "Unknown stream direction %d\n", s->stream);
264 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
265 return 0;
268 /* control info callback */
269 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
270 struct snd_ctl_elem_info *uinfo)
272 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
273 uinfo->count = 2;
274 uinfo->value.integer.min = 0;
275 uinfo->value.integer.max = 256;
276 return 0;
279 /* control get callback */
280 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
281 struct snd_ctl_elem_value *ucontrol)
283 int i;
284 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
286 for (i = 2; i--;)
287 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
289 return 0;
292 /* control put callback */
293 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
294 struct snd_ctl_elem_value *ucontrol)
296 int i, changed = 0;
297 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
299 for (i = 2; i--;)
300 if (line6pcm->volume_playback[i] !=
301 ucontrol->value.integer.value[i]) {
302 line6pcm->volume_playback[i] =
303 ucontrol->value.integer.value[i];
304 changed = 1;
307 return changed;
310 /* control definition */
311 static struct snd_kcontrol_new line6_control_playback = {
312 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
313 .name = "PCM Playback Volume",
314 .index = 0,
315 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
316 .info = snd_line6_control_playback_info,
317 .get = snd_line6_control_playback_get,
318 .put = snd_line6_control_playback_put
322 Cleanup the PCM device.
324 static void line6_cleanup_pcm(struct snd_pcm *pcm)
326 int i;
327 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
329 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
330 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
331 device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
332 #endif
334 for (i = LINE6_ISO_BUFFERS; i--;) {
335 if (line6pcm->urb_audio_out[i]) {
336 usb_kill_urb(line6pcm->urb_audio_out[i]);
337 usb_free_urb(line6pcm->urb_audio_out[i]);
339 if (line6pcm->urb_audio_in[i]) {
340 usb_kill_urb(line6pcm->urb_audio_in[i]);
341 usb_free_urb(line6pcm->urb_audio_in[i]);
346 /* create a PCM device */
347 static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
349 struct snd_pcm *pcm;
350 int err;
352 err = snd_pcm_new(line6pcm->line6->card,
353 (char *)line6pcm->line6->properties->name,
354 0, 1, 1, &pcm);
355 if (err < 0)
356 return err;
358 pcm->private_data = line6pcm;
359 pcm->private_free = line6_cleanup_pcm;
360 line6pcm->pcm = pcm;
361 strcpy(pcm->name, line6pcm->line6->properties->name);
363 /* set operators */
364 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
365 &snd_line6_playback_ops);
366 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
368 /* pre-allocation of buffers */
369 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
370 snd_dma_continuous_data
371 (GFP_KERNEL), 64 * 1024,
372 128 * 1024);
374 return 0;
377 /* PCM device destructor */
378 static int snd_line6_pcm_free(struct snd_device *device)
380 return 0;
384 Stop substream if still running.
386 static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
388 if (substream->runtime && snd_pcm_running(substream))
389 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
393 Stop PCM stream.
395 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
397 pcm_disconnect_substream(get_substream
398 (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
399 pcm_disconnect_substream(get_substream
400 (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
401 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
402 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
406 Create and register the PCM device and mixer entries.
407 Create URBs for playback and capture.
409 int line6_init_pcm(struct usb_line6 *line6,
410 struct line6_pcm_properties *properties)
412 static struct snd_device_ops pcm_ops = {
413 .dev_free = snd_line6_pcm_free,
416 int err;
417 int ep_read = 0, ep_write = 0;
418 struct snd_line6_pcm *line6pcm;
420 if (!(line6->properties->capabilities & LINE6_BIT_PCM))
421 return 0; /* skip PCM initialization and report success */
423 /* initialize PCM subsystem based on product id: */
424 switch (line6->product) {
425 case LINE6_DEVID_BASSPODXT:
426 case LINE6_DEVID_BASSPODXTLIVE:
427 case LINE6_DEVID_BASSPODXTPRO:
428 case LINE6_DEVID_PODXT:
429 case LINE6_DEVID_PODXTLIVE:
430 case LINE6_DEVID_PODXTPRO:
431 case LINE6_DEVID_PODHD300:
432 ep_read = 0x82;
433 ep_write = 0x01;
434 break;
436 case LINE6_DEVID_PODHD500:
437 case LINE6_DEVID_PODX3:
438 case LINE6_DEVID_PODX3LIVE:
439 ep_read = 0x86;
440 ep_write = 0x02;
441 break;
443 case LINE6_DEVID_POCKETPOD:
444 ep_read = 0x82;
445 ep_write = 0x02;
446 break;
448 case LINE6_DEVID_GUITARPORT:
449 case LINE6_DEVID_PODSTUDIO_GX:
450 case LINE6_DEVID_PODSTUDIO_UX1:
451 case LINE6_DEVID_PODSTUDIO_UX2:
452 case LINE6_DEVID_TONEPORT_GX:
453 case LINE6_DEVID_TONEPORT_UX1:
454 case LINE6_DEVID_TONEPORT_UX2:
455 ep_read = 0x82;
456 ep_write = 0x01;
457 break;
459 /* this is for interface_number == 1:
460 case LINE6_DEVID_TONEPORT_UX2:
461 case LINE6_DEVID_PODSTUDIO_UX2:
462 ep_read = 0x87;
463 ep_write = 0x00;
464 break; */
466 default:
467 MISSING_CASE;
470 line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
472 if (line6pcm == NULL)
473 return -ENOMEM;
475 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
476 line6pcm->volume_monitor = 255;
477 line6pcm->line6 = line6;
478 line6pcm->ep_audio_read = ep_read;
479 line6pcm->ep_audio_write = ep_write;
481 /* Read and write buffers are sized identically, so choose minimum */
482 line6pcm->max_packet_size = min(
483 usb_maxpacket(line6->usbdev,
484 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
485 usb_maxpacket(line6->usbdev,
486 usb_sndisocpipe(line6->usbdev, ep_write), 1));
488 line6pcm->properties = properties;
489 line6->line6pcm = line6pcm;
491 /* PCM device: */
492 err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
493 if (err < 0)
494 return err;
496 snd_card_set_dev(line6->card, line6->ifcdev);
498 err = snd_line6_new_pcm(line6pcm);
499 if (err < 0)
500 return err;
502 spin_lock_init(&line6pcm->lock_audio_out);
503 spin_lock_init(&line6pcm->lock_audio_in);
504 spin_lock_init(&line6pcm->lock_trigger);
506 err = line6_create_audio_out_urbs(line6pcm);
507 if (err < 0)
508 return err;
510 err = line6_create_audio_in_urbs(line6pcm);
511 if (err < 0)
512 return err;
514 /* mixer: */
515 err =
516 snd_ctl_add(line6->card,
517 snd_ctl_new1(&line6_control_playback, line6pcm));
518 if (err < 0)
519 return err;
521 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
522 /* impulse response test: */
523 err = device_create_file(line6->ifcdev, &dev_attr_impulse_volume);
524 if (err < 0)
525 return err;
527 err = device_create_file(line6->ifcdev, &dev_attr_impulse_period);
528 if (err < 0)
529 return err;
531 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
532 #endif
534 return 0;
537 /* prepare pcm callback */
538 int snd_line6_prepare(struct snd_pcm_substream *substream)
540 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
542 switch (substream->stream) {
543 case SNDRV_PCM_STREAM_PLAYBACK:
544 if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
545 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
547 break;
549 case SNDRV_PCM_STREAM_CAPTURE:
550 if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
551 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
553 break;
555 default:
556 MISSING_CASE;
559 if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
560 line6pcm->count_out = 0;
561 line6pcm->pos_out = 0;
562 line6pcm->pos_out_done = 0;
563 line6pcm->bytes_out = 0;
564 line6pcm->count_in = 0;
565 line6pcm->pos_in_done = 0;
566 line6pcm->bytes_in = 0;
569 return 0;