]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/cx18/cx18-alsa-pcm.c
V4L/DVB: cx18: address possible passing of NULL to snd_card_free
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / cx18 / cx18-alsa-pcm.c
CommitLineData
f9b071c7
AW
1/*
2 * ALSA PCM device for the
3 * ALSA interface to cx18 PCM capture streams
4 *
5 * Copyright (C) 2009 Andy Walls <awalls@radix.net>
9972de90
DH
6 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7 *
8 * Portions of this work were sponsored by ONELAN Limited.
f9b071c7
AW
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 * 02111-1307 USA
24 */
25
26#include <linux/init.h>
27#include <linux/kernel.h>
28
29#include <media/v4l2-device.h>
30
31#include <sound/core.h>
32#include <sound/pcm.h>
33
34#include "cx18-driver.h"
9972de90
DH
35#include "cx18-queue.h"
36#include "cx18-streams.h"
37#include "cx18-fileops.h"
f9b071c7
AW
38#include "cx18-alsa.h"
39
71036ef2
DH
40static unsigned int pcm_debug;
41module_param(pcm_debug, int, 0644);
42MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
9972de90
DH
43
44#define dprintk(fmt, arg...) do { \
71036ef2
DH
45 if (pcm_debug) \
46 printk(KERN_INFO "cx18-alsa-pcm %s: " fmt, \
9972de90
DH
47 __func__, ##arg); \
48 } while (0)
49
9972de90
DH
50static struct snd_pcm_hardware snd_cx18_hw_capture = {
51 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
52 SNDRV_PCM_INFO_MMAP |
53 SNDRV_PCM_INFO_INTERLEAVED |
54 SNDRV_PCM_INFO_MMAP_VALID,
55
56 .formats = SNDRV_PCM_FMTBIT_S16_LE,
57
58 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
59
60 .rate_min = 48000,
61 .rate_max = 48000,
62 .channels_min = 2,
63 .channels_max = 2,
64 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
65 .period_bytes_min = 64, /* 12544/2, */
66 .period_bytes_max = 12544,
67 .periods_min = 2,
68 .periods_max = 98, /* 12544, */
69};
70
60433e2a
DH
71void cx18_alsa_announce_pcm_data(struct snd_cx18_card *cxsc, u8 *pcm_data,
72 size_t num_bytes)
73{
74 struct snd_pcm_substream *substream;
75 struct snd_pcm_runtime *runtime;
76 unsigned int oldptr;
77 unsigned int stride;
78 int period_elapsed = 0;
79 int length;
80
81 dprintk("cx18 alsa announce ptr=%p data=%p num_bytes=%d\n", cxsc,
82 pcm_data, num_bytes);
83
84 substream = cxsc->capture_pcm_substream;
85 if (substream == NULL) {
86 dprintk("substream was NULL\n");
87 return;
88 }
89
90 runtime = substream->runtime;
91 if (runtime == NULL) {
92 dprintk("runtime was NULL\n");
93 return;
94 }
95
96 stride = runtime->frame_bits >> 3;
97 if (stride == 0) {
98 dprintk("stride is zero\n");
99 return;
100 }
101
102 length = num_bytes / stride;
103 if (length == 0) {
104 dprintk("%s: length was zero\n", __func__);
105 return;
106 }
107
108 if (runtime->dma_area == NULL) {
109 dprintk("dma area was NULL - ignoring\n");
110 return;
111 }
112
113 oldptr = cxsc->hwptr_done_capture;
114 if (oldptr + length >= runtime->buffer_size) {
115 unsigned int cnt =
116 runtime->buffer_size - oldptr;
117 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
118 cnt * stride);
119 memcpy(runtime->dma_area, pcm_data + cnt * stride,
120 length * stride - cnt * stride);
121 } else {
122 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
123 length * stride);
124 }
125 snd_pcm_stream_lock(substream);
126
127 cxsc->hwptr_done_capture += length;
128 if (cxsc->hwptr_done_capture >=
129 runtime->buffer_size)
130 cxsc->hwptr_done_capture -=
131 runtime->buffer_size;
132
133 cxsc->capture_transfer_done += length;
134 if (cxsc->capture_transfer_done >=
135 runtime->period_size) {
136 cxsc->capture_transfer_done -=
137 runtime->period_size;
138 period_elapsed = 1;
139 }
140
141 snd_pcm_stream_unlock(substream);
142
143 if (period_elapsed)
144 snd_pcm_period_elapsed(substream);
145}
146
f9b071c7
AW
147static int snd_cx18_pcm_capture_open(struct snd_pcm_substream *substream)
148{
149 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
150 struct snd_pcm_runtime *runtime = substream->runtime;
151 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
f9b071c7 152 struct cx18 *cx = to_cx18(v4l2_dev);
9972de90
DH
153 struct cx18_stream *s;
154 struct cx18_open_id *item;
155 int ret;
156
157 /* Instruct the cx18 to start sending packets */
158 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
159
160 /* Allocate memory */
161 item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
780edb7a 162 if (NULL == item)
9972de90 163 return -ENOMEM;
780edb7a 164
9972de90
DH
165 item->cx = cx;
166 item->type = s->type;
167 item->open_id = cx->open_id++;
168
169 /* See if the stream is available */
170 if (cx18_claim_stream(item, item->type)) {
171 /* No, it's already in use */
1a8e0e33 172 kfree(item);
9972de90
DH
173 return -EBUSY;
174 }
175
176 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
177 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
178 /* We're already streaming. No additional action required */
179 return 0;
180 }
181
182
183 runtime->hw = snd_cx18_hw_capture;
184 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
185 cxsc->capture_pcm_substream = substream;
186 runtime->private_data = cx;
187
188 cx->pcm_announce_callback = cx18_alsa_announce_pcm_data;
189
190 /* Not currently streaming, so start it up */
191 set_bit(CX18_F_S_STREAMING, &s->s_flags);
192 ret = cx18_start_v4l2_encode_stream(s);
193
f9b071c7
AW
194 return 0;
195}
196
197static int snd_cx18_pcm_capture_close(struct snd_pcm_substream *substream)
198{
9972de90
DH
199 unsigned long flags;
200 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
201 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
202 struct cx18 *cx = to_cx18(v4l2_dev);
203 struct cx18_stream *s;
204 int ret;
205
206 /* Instruct the cx18 to stop sending packets */
207 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
208 ret = cx18_stop_v4l2_encode_stream(s, 0);
209 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
210
211 cx18_release_stream(s);
212
213 cx->pcm_announce_callback = NULL;
214
215 spin_lock_irqsave(&cxsc->slock, flags);
216 if (substream->runtime->dma_area) {
217 dprintk("freeing pcm capture region\n");
218 vfree(substream->runtime->dma_area);
219 substream->runtime->dma_area = NULL;
220 }
221 spin_unlock_irqrestore(&cxsc->slock, flags);
222
f9b071c7
AW
223 return 0;
224}
225
226static int snd_cx18_pcm_ioctl(struct snd_pcm_substream *substream,
227 unsigned int cmd, void *arg)
228{
229 return snd_pcm_lib_ioctl(substream, cmd, arg);
230}
231
9972de90
DH
232
233static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
234 size_t size)
235{
236 struct snd_pcm_runtime *runtime = subs->runtime;
237
238 dprintk("Allocating vbuffer\n");
239 if (runtime->dma_area) {
240 if (runtime->dma_bytes > size)
241 return 0;
242
243 vfree(runtime->dma_area);
244 }
245 runtime->dma_area = vmalloc(size);
246 if (!runtime->dma_area)
247 return -ENOMEM;
248
249 runtime->dma_bytes = size;
250
251 return 0;
252}
253
f9b071c7
AW
254static int snd_cx18_pcm_hw_params(struct snd_pcm_substream *substream,
255 struct snd_pcm_hw_params *params)
256{
9972de90
DH
257 int ret;
258
259 dprintk("%s called\n", __func__);
260
261 ret = snd_pcm_alloc_vmalloc_buffer(substream,
262 params_buffer_bytes(params));
f9b071c7
AW
263 return 0;
264}
265
266static int snd_cx18_pcm_hw_free(struct snd_pcm_substream *substream)
267{
268 return 0;
269}
270
271static int snd_cx18_pcm_prepare(struct snd_pcm_substream *substream)
272{
9972de90
DH
273 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
274
275 cxsc->hwptr_done_capture = 0;
276 cxsc->capture_transfer_done = 0;
277
f9b071c7
AW
278 return 0;
279}
280
281static int snd_cx18_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
282{
283 return 0;
284}
285
286static
287snd_pcm_uframes_t snd_cx18_pcm_pointer(struct snd_pcm_substream *substream)
288{
9972de90
DH
289 unsigned long flags;
290 snd_pcm_uframes_t hwptr_done;
291 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
292
293 spin_lock_irqsave(&cxsc->slock, flags);
294 hwptr_done = cxsc->hwptr_done_capture;
295 spin_unlock_irqrestore(&cxsc->slock, flags);
296
297 return hwptr_done;
298}
299
9972de90
DH
300static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
301 unsigned long offset)
302{
303 void *pageptr = subs->runtime->dma_area + offset;
304
305 return vmalloc_to_page(pageptr);
f9b071c7
AW
306}
307
308static struct snd_pcm_ops snd_cx18_pcm_capture_ops = {
309 .open = snd_cx18_pcm_capture_open,
310 .close = snd_cx18_pcm_capture_close,
311 .ioctl = snd_cx18_pcm_ioctl,
312 .hw_params = snd_cx18_pcm_hw_params,
313 .hw_free = snd_cx18_pcm_hw_free,
314 .prepare = snd_cx18_pcm_prepare,
315 .trigger = snd_cx18_pcm_trigger,
316 .pointer = snd_cx18_pcm_pointer,
9972de90 317 .page = snd_pcm_get_vmalloc_page,
f9b071c7
AW
318};
319
9972de90 320int snd_cx18_pcm_create(struct snd_cx18_card *cxsc)
f9b071c7
AW
321{
322 struct snd_pcm *sp;
323 struct snd_card *sc = cxsc->sc;
324 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
325 struct cx18 *cx = to_cx18(v4l2_dev);
326 int ret;
327
328 ret = snd_pcm_new(sc, "CX23418 PCM",
329 0, /* PCM device 0, the only one for this card */
330 0, /* 0 playback substreams */
331 1, /* 1 capture substream */
332 &sp);
333 if (ret) {
334 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
335 __func__, ret);
336 goto err_exit;
337 }
9972de90
DH
338
339 spin_lock_init(&cxsc->slock);
340
780edb7a
DH
341 snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
342 &snd_cx18_pcm_capture_ops);
9972de90
DH
343 sp->info_flags = 0;
344 sp->private_data = cxsc;
5eb9978f 345 strlcpy(sp->name, cx->card_name, sizeof(sp->name));
9972de90 346
f9b071c7
AW
347 return 0;
348
349err_exit:
350 return ret;
351}