]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/pci/ivtv/ivtv-alsa-pcm.c
[media] tm6000: Clean up file handle in open() error path
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / pci / ivtv / ivtv-alsa-pcm.c
CommitLineData
269c11fb
AW
1/*
2 * ALSA PCM device for the
3 * ALSA interface to ivtv PCM capture streams
4 *
5 * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net>
6 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7 *
8 * Portions of this work were sponsored by ONELAN Limited for the cx18 driver
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.
269c11fb
AW
19 */
20
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/vmalloc.h>
269c11fb
AW
24
25#include <media/v4l2-device.h>
26
27#include <sound/core.h>
28#include <sound/pcm.h>
29
30#include "ivtv-driver.h"
31#include "ivtv-queue.h"
32#include "ivtv-streams.h"
33#include "ivtv-fileops.h"
34#include "ivtv-alsa.h"
2aebbf67 35#include "ivtv-alsa-pcm.h"
269c11fb
AW
36
37static unsigned int pcm_debug;
38module_param(pcm_debug, int, 0644);
39MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
40
41#define dprintk(fmt, arg...) \
42 do { \
43 if (pcm_debug) \
44 pr_info("ivtv-alsa-pcm %s: " fmt, __func__, ##arg); \
45 } while (0)
46
47static struct snd_pcm_hardware snd_ivtv_hw_capture = {
48 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
49 SNDRV_PCM_INFO_MMAP |
50 SNDRV_PCM_INFO_INTERLEAVED |
51 SNDRV_PCM_INFO_MMAP_VALID,
52
53 .formats = SNDRV_PCM_FMTBIT_S16_LE,
54
55 .rates = SNDRV_PCM_RATE_48000,
56
57 .rate_min = 48000,
58 .rate_max = 48000,
59 .channels_min = 2,
60 .channels_max = 2,
61 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
62 .period_bytes_min = 64, /* 12544/2, */
63 .period_bytes_max = 12544,
64 .periods_min = 2,
65 .periods_max = 98, /* 12544, */
66};
67
2aebbf67
MCC
68static void ivtv_alsa_announce_pcm_data(struct snd_ivtv_card *itvsc,
69 u8 *pcm_data,
70 size_t num_bytes)
269c11fb
AW
71{
72 struct snd_pcm_substream *substream;
73 struct snd_pcm_runtime *runtime;
74 unsigned int oldptr;
75 unsigned int stride;
76 int period_elapsed = 0;
77 int length;
78
339f06c5 79 dprintk("ivtv alsa announce ptr=%p data=%p num_bytes=%zu\n", itvsc,
269c11fb
AW
80 pcm_data, num_bytes);
81
82 substream = itvsc->capture_pcm_substream;
83 if (substream == NULL) {
84 dprintk("substream was NULL\n");
85 return;
86 }
87
88 runtime = substream->runtime;
89 if (runtime == NULL) {
90 dprintk("runtime was NULL\n");
91 return;
92 }
93
94 stride = runtime->frame_bits >> 3;
95 if (stride == 0) {
96 dprintk("stride is zero\n");
97 return;
98 }
99
100 length = num_bytes / stride;
101 if (length == 0) {
102 dprintk("%s: length was zero\n", __func__);
103 return;
104 }
105
106 if (runtime->dma_area == NULL) {
107 dprintk("dma area was NULL - ignoring\n");
108 return;
109 }
110
111 oldptr = itvsc->hwptr_done_capture;
112 if (oldptr + length >= runtime->buffer_size) {
113 unsigned int cnt =
114 runtime->buffer_size - oldptr;
115 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
116 cnt * stride);
117 memcpy(runtime->dma_area, pcm_data + cnt * stride,
118 length * stride - cnt * stride);
119 } else {
120 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
121 length * stride);
122 }
123 snd_pcm_stream_lock(substream);
124
125 itvsc->hwptr_done_capture += length;
126 if (itvsc->hwptr_done_capture >=
127 runtime->buffer_size)
128 itvsc->hwptr_done_capture -=
129 runtime->buffer_size;
130
131 itvsc->capture_transfer_done += length;
132 if (itvsc->capture_transfer_done >=
133 runtime->period_size) {
134 itvsc->capture_transfer_done -=
135 runtime->period_size;
136 period_elapsed = 1;
137 }
138
139 snd_pcm_stream_unlock(substream);
140
141 if (period_elapsed)
142 snd_pcm_period_elapsed(substream);
143}
144
145static int snd_ivtv_pcm_capture_open(struct snd_pcm_substream *substream)
146{
147 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
148 struct snd_pcm_runtime *runtime = substream->runtime;
149 struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
150 struct ivtv *itv = to_ivtv(v4l2_dev);
151 struct ivtv_stream *s;
152 struct ivtv_open_id item;
153 int ret;
154
155 /* Instruct the CX2341[56] to start sending packets */
156 snd_ivtv_lock(itvsc);
deb29e90
TI
157
158 if (ivtv_init_on_first_open(itv)) {
159 snd_ivtv_unlock(itvsc);
160 return -ENXIO;
161 }
162
269c11fb
AW
163 s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
164
635d62f0 165 v4l2_fh_init(&item.fh, &s->vdev);
269c11fb
AW
166 item.itv = itv;
167 item.type = s->type;
168
169 /* See if the stream is available */
170 if (ivtv_claim_stream(&item, item.type)) {
171 /* No, it's already in use */
172 snd_ivtv_unlock(itvsc);
173 return -EBUSY;
174 }
175
176 if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) ||
177 test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
178 /* We're already streaming. No additional action required */
179 snd_ivtv_unlock(itvsc);
180 return 0;
181 }
182
183
184 runtime->hw = snd_ivtv_hw_capture;
185 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
186 itvsc->capture_pcm_substream = substream;
187 runtime->private_data = itv;
188
189 itv->pcm_announce_callback = ivtv_alsa_announce_pcm_data;
190
191 /* Not currently streaming, so start it up */
192 set_bit(IVTV_F_S_STREAMING, &s->s_flags);
193 ret = ivtv_start_v4l2_encode_stream(s);
194 snd_ivtv_unlock(itvsc);
195
196 return ret;
197}
198
199static int snd_ivtv_pcm_capture_close(struct snd_pcm_substream *substream)
200{
201 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
202 struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
203 struct ivtv *itv = to_ivtv(v4l2_dev);
204 struct ivtv_stream *s;
205
206 /* Instruct the ivtv to stop sending packets */
207 snd_ivtv_lock(itvsc);
208 s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
209 ivtv_stop_v4l2_encode_stream(s, 0);
210 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
211
212 ivtv_release_stream(s);
213
214 itv->pcm_announce_callback = NULL;
215 snd_ivtv_unlock(itvsc);
216
217 return 0;
218}
219
220static int snd_ivtv_pcm_ioctl(struct snd_pcm_substream *substream,
221 unsigned int cmd, void *arg)
222{
223 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
224 int ret;
225
226 snd_ivtv_lock(itvsc);
227 ret = snd_pcm_lib_ioctl(substream, cmd, arg);
228 snd_ivtv_unlock(itvsc);
229 return ret;
230}
231
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
254static int snd_ivtv_pcm_hw_params(struct snd_pcm_substream *substream,
255 struct snd_pcm_hw_params *params)
256{
257 dprintk("%s called\n", __func__);
258
259 return snd_pcm_alloc_vmalloc_buffer(substream,
260 params_buffer_bytes(params));
261}
262
263static int snd_ivtv_pcm_hw_free(struct snd_pcm_substream *substream)
264{
265 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
266 unsigned long flags;
267
268 spin_lock_irqsave(&itvsc->slock, flags);
269 if (substream->runtime->dma_area) {
270 dprintk("freeing pcm capture region\n");
271 vfree(substream->runtime->dma_area);
272 substream->runtime->dma_area = NULL;
273 }
274 spin_unlock_irqrestore(&itvsc->slock, flags);
275
276 return 0;
277}
278
279static int snd_ivtv_pcm_prepare(struct snd_pcm_substream *substream)
280{
281 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
282
283 itvsc->hwptr_done_capture = 0;
284 itvsc->capture_transfer_done = 0;
285
286 return 0;
287}
288
289static int snd_ivtv_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
290{
291 return 0;
292}
293
294static
295snd_pcm_uframes_t snd_ivtv_pcm_pointer(struct snd_pcm_substream *substream)
296{
297 unsigned long flags;
298 snd_pcm_uframes_t hwptr_done;
299 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
300
301 spin_lock_irqsave(&itvsc->slock, flags);
302 hwptr_done = itvsc->hwptr_done_capture;
303 spin_unlock_irqrestore(&itvsc->slock, flags);
304
305 return hwptr_done;
306}
307
308static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
309 unsigned long offset)
310{
311 void *pageptr = subs->runtime->dma_area + offset;
312
313 return vmalloc_to_page(pageptr);
314}
315
5c8d8c01 316static const struct snd_pcm_ops snd_ivtv_pcm_capture_ops = {
269c11fb
AW
317 .open = snd_ivtv_pcm_capture_open,
318 .close = snd_ivtv_pcm_capture_close,
319 .ioctl = snd_ivtv_pcm_ioctl,
320 .hw_params = snd_ivtv_pcm_hw_params,
321 .hw_free = snd_ivtv_pcm_hw_free,
322 .prepare = snd_ivtv_pcm_prepare,
323 .trigger = snd_ivtv_pcm_trigger,
324 .pointer = snd_ivtv_pcm_pointer,
325 .page = snd_pcm_get_vmalloc_page,
326};
327
328int snd_ivtv_pcm_create(struct snd_ivtv_card *itvsc)
329{
330 struct snd_pcm *sp;
331 struct snd_card *sc = itvsc->sc;
332 struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
333 struct ivtv *itv = to_ivtv(v4l2_dev);
334 int ret;
335
336 ret = snd_pcm_new(sc, "CX2341[56] PCM",
337 0, /* PCM device 0, the only one for this card */
338 0, /* 0 playback substreams */
339 1, /* 1 capture substream */
340 &sp);
341 if (ret) {
342 IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
343 __func__, ret);
344 goto err_exit;
345 }
346
347 spin_lock_init(&itvsc->slock);
348
349 snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
350 &snd_ivtv_pcm_capture_ops);
351 sp->info_flags = 0;
352 sp->private_data = itvsc;
353 strlcpy(sp->name, itv->card_name, sizeof(sp->name));
354
355 return 0;
356
357err_exit:
358 return ret;
359}