iwlwifi: fix skb usage after free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / line6 / pcm.c
blobdd98121eb80bb582bf515b8e4a9b740bd6f5a545
1 /*
2 * Line6 Linux USB driver - 0.8.0
4 * Copyright (C) 2004-2009 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 "driver.h"
14 #include <sound/core.h>
15 #include <sound/control.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
19 #include "audio.h"
20 #include "capture.h"
21 #include "playback.h"
22 #include "pod.h"
25 /* trigger callback */
26 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
28 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
29 struct snd_pcm_substream *s;
30 int err;
31 unsigned long flags;
33 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
34 clear_bit(BIT_PREPARED, &line6pcm->flags);
36 snd_pcm_group_for_each_entry(s, substream) {
37 switch (s->stream) {
38 case SNDRV_PCM_STREAM_PLAYBACK:
39 err = snd_line6_playback_trigger(s, cmd);
41 if (err < 0) {
42 spin_unlock_irqrestore(&line6pcm->lock_trigger,
43 flags);
44 return err;
47 break;
49 case SNDRV_PCM_STREAM_CAPTURE:
50 err = snd_line6_capture_trigger(s, cmd);
52 if (err < 0) {
53 spin_unlock_irqrestore(&line6pcm->lock_trigger,
54 flags);
55 return err;
58 break;
60 default:
61 dev_err(s2m(substream), "Unknown stream direction %d\n",
62 s->stream);
66 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
67 return 0;
70 /* control info callback */
71 static int snd_line6_control_info(struct snd_kcontrol *kcontrol,
72 struct snd_ctl_elem_info *uinfo)
74 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
75 uinfo->count = 2;
76 uinfo->value.integer.min = 0;
77 uinfo->value.integer.max = 256;
78 return 0;
81 /* control get callback */
82 static int snd_line6_control_get(struct snd_kcontrol *kcontrol,
83 struct snd_ctl_elem_value *ucontrol)
85 int i;
86 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
88 for (i = 2; i--;)
89 ucontrol->value.integer.value[i] = line6pcm->volume[i];
91 return 0;
94 /* control put callback */
95 static int snd_line6_control_put(struct snd_kcontrol *kcontrol,
96 struct snd_ctl_elem_value *ucontrol)
98 int i, changed = 0;
99 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
101 for (i = 2; i--;)
102 if (line6pcm->volume[i] != ucontrol->value.integer.value[i]) {
103 line6pcm->volume[i] = ucontrol->value.integer.value[i];
104 changed = 1;
107 return changed;
110 /* control definition */
111 static struct snd_kcontrol_new line6_control = {
112 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
113 .name = "PCM Playback Volume",
114 .index = 0,
115 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
116 .info = snd_line6_control_info,
117 .get = snd_line6_control_get,
118 .put = snd_line6_control_put
122 Cleanup the PCM device.
124 static void line6_cleanup_pcm(struct snd_pcm *pcm)
126 int i;
127 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
129 for (i = LINE6_ISO_BUFFERS; i--;) {
130 if (line6pcm->urb_audio_out[i]) {
131 usb_kill_urb(line6pcm->urb_audio_out[i]);
132 usb_free_urb(line6pcm->urb_audio_out[i]);
134 if (line6pcm->urb_audio_in[i]) {
135 usb_kill_urb(line6pcm->urb_audio_in[i]);
136 usb_free_urb(line6pcm->urb_audio_in[i]);
141 /* create a PCM device */
142 static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
144 struct snd_pcm *pcm;
145 int err;
147 err = snd_pcm_new(line6pcm->line6->card,
148 (char *)line6pcm->line6->properties->name,
149 0, 1, 1, &pcm);
150 if (err < 0)
151 return err;
153 pcm->private_data = line6pcm;
154 pcm->private_free = line6_cleanup_pcm;
155 line6pcm->pcm = pcm;
156 strcpy(pcm->name, line6pcm->line6->properties->name);
158 /* set operators */
159 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
160 &snd_line6_playback_ops);
161 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
163 /* pre-allocation of buffers */
164 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
165 snd_dma_continuous_data(GFP_KERNEL),
166 64 * 1024, 128 * 1024);
168 return 0;
171 /* PCM device destructor */
172 static int snd_line6_pcm_free(struct snd_device *device)
174 return 0;
178 Create and register the PCM device and mixer entries.
179 Create URBs for playback and capture.
181 int line6_init_pcm(struct usb_line6 *line6,
182 struct line6_pcm_properties *properties)
184 static struct snd_device_ops pcm_ops = {
185 .dev_free = snd_line6_pcm_free,
188 int err;
189 int ep_read = 0, ep_write = 0;
190 struct snd_line6_pcm *line6pcm;
192 if (!(line6->properties->capabilities & LINE6_BIT_PCM))
193 return 0; /* skip PCM initialization and report success */
195 /* initialize PCM subsystem based on product id: */
196 switch (line6->product) {
197 case LINE6_DEVID_BASSPODXT:
198 case LINE6_DEVID_BASSPODXTLIVE:
199 case LINE6_DEVID_BASSPODXTPRO:
200 case LINE6_DEVID_PODXT:
201 case LINE6_DEVID_PODXTLIVE:
202 case LINE6_DEVID_PODXTPRO:
203 ep_read = 0x82;
204 ep_write = 0x01;
205 break;
207 case LINE6_DEVID_PODX3:
208 case LINE6_DEVID_PODX3LIVE:
209 ep_read = 0x86;
210 ep_write = 0x02;
211 break;
213 case LINE6_DEVID_POCKETPOD:
214 ep_read = 0x82;
215 ep_write = 0x02;
216 break;
218 case LINE6_DEVID_GUITARPORT:
219 case LINE6_DEVID_TONEPORT_GX:
220 ep_read = 0x82;
221 ep_write = 0x01;
222 break;
224 case LINE6_DEVID_TONEPORT_UX1:
225 ep_read = 0x00;
226 ep_write = 0x00;
227 break;
229 case LINE6_DEVID_TONEPORT_UX2:
230 ep_read = 0x87;
231 ep_write = 0x00;
232 break;
234 default:
235 MISSING_CASE;
238 line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
240 if (line6pcm == NULL)
241 return -ENOMEM;
243 line6pcm->volume[0] = line6pcm->volume[1] = 128;
244 line6pcm->line6 = line6;
245 line6pcm->ep_audio_read = ep_read;
246 line6pcm->ep_audio_write = ep_write;
247 line6pcm->max_packet_size = usb_maxpacket(line6->usbdev,
248 usb_rcvintpipe(line6->usbdev,
249 ep_read),
251 line6pcm->properties = properties;
252 line6->line6pcm = line6pcm;
254 /* PCM device: */
255 err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
256 if (err < 0)
257 return err;
259 snd_card_set_dev(line6->card, line6->ifcdev);
261 err = snd_line6_new_pcm(line6pcm);
262 if (err < 0)
263 return err;
265 spin_lock_init(&line6pcm->lock_audio_out);
266 spin_lock_init(&line6pcm->lock_audio_in);
267 spin_lock_init(&line6pcm->lock_trigger);
269 err = create_audio_out_urbs(line6pcm);
270 if (err < 0)
271 return err;
273 err = create_audio_in_urbs(line6pcm);
274 if (err < 0)
275 return err;
277 /* mixer: */
278 err = snd_ctl_add(line6->card, snd_ctl_new1(&line6_control, line6pcm));
279 if (err < 0)
280 return err;
282 return 0;
285 /* prepare pcm callback */
286 int snd_line6_prepare(struct snd_pcm_substream *substream)
288 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
290 if (!test_and_set_bit(BIT_PREPARED, &line6pcm->flags)) {
291 unlink_wait_clear_audio_out_urbs(line6pcm);
292 line6pcm->pos_out = 0;
293 line6pcm->pos_out_done = 0;
295 unlink_wait_clear_audio_in_urbs(line6pcm);
296 line6pcm->bytes_out = 0;
297 line6pcm->pos_in_done = 0;
298 line6pcm->bytes_in = 0;
301 return 0;