]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/pci/cx18/cx18-alsa-pcm.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.git] / drivers / media / pci / cx18 / cx18-alsa-pcm.c
CommitLineData
f9b071c7
AW
1/*
2 * ALSA PCM device for the
3 * ALSA interface to cx18 PCM capture streams
4 *
6afdeaf8 5 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.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.
f9b071c7
AW
19 */
20
21#include <linux/init.h>
22#include <linux/kernel.h>
6aeb0905 23#include <linux/vmalloc.h>
f9b071c7
AW
24
25#include <media/v4l2-device.h>
26
27#include <sound/core.h>
28#include <sound/pcm.h>
29
30#include "cx18-driver.h"
9972de90
DH
31#include "cx18-queue.h"
32#include "cx18-streams.h"
33#include "cx18-fileops.h"
f9b071c7 34#include "cx18-alsa.h"
5e6e81b2 35#include "cx18-alsa-pcm.h"
f9b071c7 36
71036ef2
DH
37static unsigned int pcm_debug;
38module_param(pcm_debug, int, 0644);
39MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
9972de90
DH
40
41#define dprintk(fmt, arg...) do { \
71036ef2
DH
42 if (pcm_debug) \
43 printk(KERN_INFO "cx18-alsa-pcm %s: " fmt, \
9972de90
DH
44 __func__, ##arg); \
45 } while (0)
46
9972de90
DH
47static struct snd_pcm_hardware snd_cx18_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
94b12d9e 55 .rates = SNDRV_PCM_RATE_48000,
9972de90
DH
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
60433e2a
DH
68void cx18_alsa_announce_pcm_data(struct snd_cx18_card *cxsc, u8 *pcm_data,
69 size_t num_bytes)
70{
71 struct snd_pcm_substream *substream;
72 struct snd_pcm_runtime *runtime;
73 unsigned int oldptr;
74 unsigned int stride;
75 int period_elapsed = 0;
76 int length;
77
339f06c5 78 dprintk("cx18 alsa announce ptr=%p data=%p num_bytes=%zu\n", cxsc,
60433e2a
DH
79 pcm_data, num_bytes);
80
81 substream = cxsc->capture_pcm_substream;
82 if (substream == NULL) {
83 dprintk("substream was NULL\n");
84 return;
85 }
86
87 runtime = substream->runtime;
88 if (runtime == NULL) {
89 dprintk("runtime was NULL\n");
90 return;
91 }
92
93 stride = runtime->frame_bits >> 3;
94 if (stride == 0) {
95 dprintk("stride is zero\n");
96 return;
97 }
98
99 length = num_bytes / stride;
100 if (length == 0) {
101 dprintk("%s: length was zero\n", __func__);
102 return;
103 }
104
105 if (runtime->dma_area == NULL) {
106 dprintk("dma area was NULL - ignoring\n");
107 return;
108 }
109
110 oldptr = cxsc->hwptr_done_capture;
111 if (oldptr + length >= runtime->buffer_size) {
112 unsigned int cnt =
113 runtime->buffer_size - oldptr;
114 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
115 cnt * stride);
116 memcpy(runtime->dma_area, pcm_data + cnt * stride,
117 length * stride - cnt * stride);
118 } else {
119 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
120 length * stride);
121 }
122 snd_pcm_stream_lock(substream);
123
124 cxsc->hwptr_done_capture += length;
125 if (cxsc->hwptr_done_capture >=
126 runtime->buffer_size)
127 cxsc->hwptr_done_capture -=
128 runtime->buffer_size;
129
130 cxsc->capture_transfer_done += length;
131 if (cxsc->capture_transfer_done >=
132 runtime->period_size) {
133 cxsc->capture_transfer_done -=
134 runtime->period_size;
135 period_elapsed = 1;
136 }
137
138 snd_pcm_stream_unlock(substream);
139
140 if (period_elapsed)
141 snd_pcm_period_elapsed(substream);
142}
143
f9b071c7
AW
144static int snd_cx18_pcm_capture_open(struct snd_pcm_substream *substream)
145{
146 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
147 struct snd_pcm_runtime *runtime = substream->runtime;
148 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
f9b071c7 149 struct cx18 *cx = to_cx18(v4l2_dev);
9972de90 150 struct cx18_stream *s;
831f476c 151 struct cx18_open_id item;
9972de90
DH
152 int ret;
153
154 /* Instruct the cx18 to start sending packets */
b4729dcb 155 snd_cx18_lock(cxsc);
9972de90
DH
156 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
157
831f476c
AW
158 item.cx = cx;
159 item.type = s->type;
160 item.open_id = cx->open_id++;
9972de90
DH
161
162 /* See if the stream is available */
831f476c 163 if (cx18_claim_stream(&item, item.type)) {
9972de90 164 /* No, it's already in use */
b4729dcb 165 snd_cx18_unlock(cxsc);
9972de90
DH
166 return -EBUSY;
167 }
168
169 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
170 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
171 /* We're already streaming. No additional action required */
b4729dcb 172 snd_cx18_unlock(cxsc);
9972de90
DH
173 return 0;
174 }
175
176
177 runtime->hw = snd_cx18_hw_capture;
178 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
179 cxsc->capture_pcm_substream = substream;
180 runtime->private_data = cx;
181
182 cx->pcm_announce_callback = cx18_alsa_announce_pcm_data;
183
184 /* Not currently streaming, so start it up */
185 set_bit(CX18_F_S_STREAMING, &s->s_flags);
186 ret = cx18_start_v4l2_encode_stream(s);
b4729dcb 187 snd_cx18_unlock(cxsc);
9972de90 188
932205a7 189 return ret;
f9b071c7
AW
190}
191
192static int snd_cx18_pcm_capture_close(struct snd_pcm_substream *substream)
193{
9972de90
DH
194 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
195 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
196 struct cx18 *cx = to_cx18(v4l2_dev);
197 struct cx18_stream *s;
9972de90
DH
198
199 /* Instruct the cx18 to stop sending packets */
b4729dcb 200 snd_cx18_lock(cxsc);
9972de90 201 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
932205a7 202 cx18_stop_v4l2_encode_stream(s, 0);
9972de90
DH
203 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
204
205 cx18_release_stream(s);
206
207 cx->pcm_announce_callback = NULL;
b4729dcb 208 snd_cx18_unlock(cxsc);
9972de90 209
f9b071c7
AW
210 return 0;
211}
212
213static int snd_cx18_pcm_ioctl(struct snd_pcm_substream *substream,
214 unsigned int cmd, void *arg)
215{
78b055be
HV
216 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
217 int ret;
218
219 snd_cx18_lock(cxsc);
220 ret = snd_pcm_lib_ioctl(substream, cmd, arg);
221 snd_cx18_unlock(cxsc);
222 return ret;
f9b071c7
AW
223}
224
9972de90
DH
225
226static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
227 size_t size)
228{
229 struct snd_pcm_runtime *runtime = subs->runtime;
230
231 dprintk("Allocating vbuffer\n");
232 if (runtime->dma_area) {
233 if (runtime->dma_bytes > size)
234 return 0;
235
236 vfree(runtime->dma_area);
237 }
238 runtime->dma_area = vmalloc(size);
239 if (!runtime->dma_area)
240 return -ENOMEM;
241
242 runtime->dma_bytes = size;
243
244 return 0;
245}
246
f9b071c7
AW
247static int snd_cx18_pcm_hw_params(struct snd_pcm_substream *substream,
248 struct snd_pcm_hw_params *params)
249{
9972de90
DH
250 dprintk("%s called\n", __func__);
251
932205a7 252 return snd_pcm_alloc_vmalloc_buffer(substream,
9972de90 253 params_buffer_bytes(params));
f9b071c7
AW
254}
255
256static int snd_cx18_pcm_hw_free(struct snd_pcm_substream *substream)
257{
94b12d9e
DH
258 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
259 unsigned long flags;
260
261 spin_lock_irqsave(&cxsc->slock, flags);
262 if (substream->runtime->dma_area) {
263 dprintk("freeing pcm capture region\n");
264 vfree(substream->runtime->dma_area);
265 substream->runtime->dma_area = NULL;
266 }
267 spin_unlock_irqrestore(&cxsc->slock, flags);
268
f9b071c7
AW
269 return 0;
270}
271
272static int snd_cx18_pcm_prepare(struct snd_pcm_substream *substream)
273{
9972de90
DH
274 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
275
276 cxsc->hwptr_done_capture = 0;
277 cxsc->capture_transfer_done = 0;
278
f9b071c7
AW
279 return 0;
280}
281
282static int snd_cx18_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
283{
284 return 0;
285}
286
287static
288snd_pcm_uframes_t snd_cx18_pcm_pointer(struct snd_pcm_substream *substream)
289{
9972de90
DH
290 unsigned long flags;
291 snd_pcm_uframes_t hwptr_done;
292 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
293
294 spin_lock_irqsave(&cxsc->slock, flags);
295 hwptr_done = cxsc->hwptr_done_capture;
296 spin_unlock_irqrestore(&cxsc->slock, flags);
297
298 return hwptr_done;
299}
300
9972de90
DH
301static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
302 unsigned long offset)
303{
304 void *pageptr = subs->runtime->dma_area + offset;
305
306 return vmalloc_to_page(pageptr);
f9b071c7
AW
307}
308
5c8d8c01 309static const struct snd_pcm_ops snd_cx18_pcm_capture_ops = {
f9b071c7
AW
310 .open = snd_cx18_pcm_capture_open,
311 .close = snd_cx18_pcm_capture_close,
312 .ioctl = snd_cx18_pcm_ioctl,
313 .hw_params = snd_cx18_pcm_hw_params,
314 .hw_free = snd_cx18_pcm_hw_free,
315 .prepare = snd_cx18_pcm_prepare,
316 .trigger = snd_cx18_pcm_trigger,
317 .pointer = snd_cx18_pcm_pointer,
9972de90 318 .page = snd_pcm_get_vmalloc_page,
f9b071c7
AW
319};
320
9972de90 321int snd_cx18_pcm_create(struct snd_cx18_card *cxsc)
f9b071c7
AW
322{
323 struct snd_pcm *sp;
324 struct snd_card *sc = cxsc->sc;
325 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
326 struct cx18 *cx = to_cx18(v4l2_dev);
327 int ret;
328
329 ret = snd_pcm_new(sc, "CX23418 PCM",
330 0, /* PCM device 0, the only one for this card */
331 0, /* 0 playback substreams */
332 1, /* 1 capture substream */
333 &sp);
334 if (ret) {
335 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
336 __func__, ret);
337 goto err_exit;
338 }
9972de90
DH
339
340 spin_lock_init(&cxsc->slock);
341
780edb7a
DH
342 snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
343 &snd_cx18_pcm_capture_ops);
9972de90
DH
344 sp->info_flags = 0;
345 sp->private_data = cxsc;
5eb9978f 346 strlcpy(sp->name, cx->card_name, sizeof(sp->name));
9972de90 347
f9b071c7
AW
348 return 0;
349
350err_exit:
351 return ret;
352}