2 * ALSA PCM device for the
3 * ALSA interface to cx18 PCM capture streams
5 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
6 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
8 * Portions of this work were sponsored by ONELAN Limited.
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.
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.
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
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/vmalloc.h>
30 #include <media/v4l2-device.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
35 #include "cx18-driver.h"
36 #include "cx18-queue.h"
37 #include "cx18-streams.h"
38 #include "cx18-fileops.h"
39 #include "cx18-alsa.h"
41 static unsigned int pcm_debug
;
42 module_param(pcm_debug
, int, 0644);
43 MODULE_PARM_DESC(pcm_debug
, "enable debug messages for pcm");
45 #define dprintk(fmt, arg...) do { \
47 printk(KERN_INFO "cx18-alsa-pcm %s: " fmt, \
51 static struct snd_pcm_hardware snd_cx18_hw_capture
= {
52 .info
= SNDRV_PCM_INFO_BLOCK_TRANSFER
|
54 SNDRV_PCM_INFO_INTERLEAVED
|
55 SNDRV_PCM_INFO_MMAP_VALID
,
57 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
59 .rates
= SNDRV_PCM_RATE_48000
,
65 .buffer_bytes_max
= 62720 * 8, /* just about the value in usbaudio.c */
66 .period_bytes_min
= 64, /* 12544/2, */
67 .period_bytes_max
= 12544,
69 .periods_max
= 98, /* 12544, */
72 void cx18_alsa_announce_pcm_data(struct snd_cx18_card
*cxsc
, u8
*pcm_data
,
75 struct snd_pcm_substream
*substream
;
76 struct snd_pcm_runtime
*runtime
;
79 int period_elapsed
= 0;
82 dprintk("cx18 alsa announce ptr=%p data=%p num_bytes=%zd\n", cxsc
,
85 substream
= cxsc
->capture_pcm_substream
;
86 if (substream
== NULL
) {
87 dprintk("substream was NULL\n");
91 runtime
= substream
->runtime
;
92 if (runtime
== NULL
) {
93 dprintk("runtime was NULL\n");
97 stride
= runtime
->frame_bits
>> 3;
99 dprintk("stride is zero\n");
103 length
= num_bytes
/ stride
;
105 dprintk("%s: length was zero\n", __func__
);
109 if (runtime
->dma_area
== NULL
) {
110 dprintk("dma area was NULL - ignoring\n");
114 oldptr
= cxsc
->hwptr_done_capture
;
115 if (oldptr
+ length
>= runtime
->buffer_size
) {
117 runtime
->buffer_size
- oldptr
;
118 memcpy(runtime
->dma_area
+ oldptr
* stride
, pcm_data
,
120 memcpy(runtime
->dma_area
, pcm_data
+ cnt
* stride
,
121 length
* stride
- cnt
* stride
);
123 memcpy(runtime
->dma_area
+ oldptr
* stride
, pcm_data
,
126 snd_pcm_stream_lock(substream
);
128 cxsc
->hwptr_done_capture
+= length
;
129 if (cxsc
->hwptr_done_capture
>=
130 runtime
->buffer_size
)
131 cxsc
->hwptr_done_capture
-=
132 runtime
->buffer_size
;
134 cxsc
->capture_transfer_done
+= length
;
135 if (cxsc
->capture_transfer_done
>=
136 runtime
->period_size
) {
137 cxsc
->capture_transfer_done
-=
138 runtime
->period_size
;
142 snd_pcm_stream_unlock(substream
);
145 snd_pcm_period_elapsed(substream
);
148 static int snd_cx18_pcm_capture_open(struct snd_pcm_substream
*substream
)
150 struct snd_cx18_card
*cxsc
= snd_pcm_substream_chip(substream
);
151 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
152 struct v4l2_device
*v4l2_dev
= cxsc
->v4l2_dev
;
153 struct cx18
*cx
= to_cx18(v4l2_dev
);
154 struct cx18_stream
*s
;
155 struct cx18_open_id item
;
158 /* Instruct the cx18 to start sending packets */
160 s
= &cx
->streams
[CX18_ENC_STREAM_TYPE_PCM
];
164 item
.open_id
= cx
->open_id
++;
166 /* See if the stream is available */
167 if (cx18_claim_stream(&item
, item
.type
)) {
168 /* No, it's already in use */
169 snd_cx18_unlock(cxsc
);
173 if (test_bit(CX18_F_S_STREAMOFF
, &s
->s_flags
) ||
174 test_and_set_bit(CX18_F_S_STREAMING
, &s
->s_flags
)) {
175 /* We're already streaming. No additional action required */
176 snd_cx18_unlock(cxsc
);
181 runtime
->hw
= snd_cx18_hw_capture
;
182 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
183 cxsc
->capture_pcm_substream
= substream
;
184 runtime
->private_data
= cx
;
186 cx
->pcm_announce_callback
= cx18_alsa_announce_pcm_data
;
188 /* Not currently streaming, so start it up */
189 set_bit(CX18_F_S_STREAMING
, &s
->s_flags
);
190 ret
= cx18_start_v4l2_encode_stream(s
);
191 snd_cx18_unlock(cxsc
);
196 static int snd_cx18_pcm_capture_close(struct snd_pcm_substream
*substream
)
198 struct snd_cx18_card
*cxsc
= snd_pcm_substream_chip(substream
);
199 struct v4l2_device
*v4l2_dev
= cxsc
->v4l2_dev
;
200 struct cx18
*cx
= to_cx18(v4l2_dev
);
201 struct cx18_stream
*s
;
204 /* Instruct the cx18 to stop sending packets */
206 s
= &cx
->streams
[CX18_ENC_STREAM_TYPE_PCM
];
207 ret
= cx18_stop_v4l2_encode_stream(s
, 0);
208 clear_bit(CX18_F_S_STREAMING
, &s
->s_flags
);
210 cx18_release_stream(s
);
212 cx
->pcm_announce_callback
= NULL
;
213 snd_cx18_unlock(cxsc
);
218 static int snd_cx18_pcm_ioctl(struct snd_pcm_substream
*substream
,
219 unsigned int cmd
, void *arg
)
221 struct snd_cx18_card
*cxsc
= snd_pcm_substream_chip(substream
);
225 ret
= snd_pcm_lib_ioctl(substream
, cmd
, arg
);
226 snd_cx18_unlock(cxsc
);
231 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream
*subs
,
234 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
236 dprintk("Allocating vbuffer\n");
237 if (runtime
->dma_area
) {
238 if (runtime
->dma_bytes
> size
)
241 vfree(runtime
->dma_area
);
243 runtime
->dma_area
= vmalloc(size
);
244 if (!runtime
->dma_area
)
247 runtime
->dma_bytes
= size
;
252 static int snd_cx18_pcm_hw_params(struct snd_pcm_substream
*substream
,
253 struct snd_pcm_hw_params
*params
)
257 dprintk("%s called\n", __func__
);
259 ret
= snd_pcm_alloc_vmalloc_buffer(substream
,
260 params_buffer_bytes(params
));
264 static int snd_cx18_pcm_hw_free(struct snd_pcm_substream
*substream
)
266 struct snd_cx18_card
*cxsc
= snd_pcm_substream_chip(substream
);
269 spin_lock_irqsave(&cxsc
->slock
, flags
);
270 if (substream
->runtime
->dma_area
) {
271 dprintk("freeing pcm capture region\n");
272 vfree(substream
->runtime
->dma_area
);
273 substream
->runtime
->dma_area
= NULL
;
275 spin_unlock_irqrestore(&cxsc
->slock
, flags
);
280 static int snd_cx18_pcm_prepare(struct snd_pcm_substream
*substream
)
282 struct snd_cx18_card
*cxsc
= snd_pcm_substream_chip(substream
);
284 cxsc
->hwptr_done_capture
= 0;
285 cxsc
->capture_transfer_done
= 0;
290 static int snd_cx18_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
296 snd_pcm_uframes_t
snd_cx18_pcm_pointer(struct snd_pcm_substream
*substream
)
299 snd_pcm_uframes_t hwptr_done
;
300 struct snd_cx18_card
*cxsc
= snd_pcm_substream_chip(substream
);
302 spin_lock_irqsave(&cxsc
->slock
, flags
);
303 hwptr_done
= cxsc
->hwptr_done_capture
;
304 spin_unlock_irqrestore(&cxsc
->slock
, flags
);
309 static struct page
*snd_pcm_get_vmalloc_page(struct snd_pcm_substream
*subs
,
310 unsigned long offset
)
312 void *pageptr
= subs
->runtime
->dma_area
+ offset
;
314 return vmalloc_to_page(pageptr
);
317 static struct snd_pcm_ops snd_cx18_pcm_capture_ops
= {
318 .open
= snd_cx18_pcm_capture_open
,
319 .close
= snd_cx18_pcm_capture_close
,
320 .ioctl
= snd_cx18_pcm_ioctl
,
321 .hw_params
= snd_cx18_pcm_hw_params
,
322 .hw_free
= snd_cx18_pcm_hw_free
,
323 .prepare
= snd_cx18_pcm_prepare
,
324 .trigger
= snd_cx18_pcm_trigger
,
325 .pointer
= snd_cx18_pcm_pointer
,
326 .page
= snd_pcm_get_vmalloc_page
,
329 int snd_cx18_pcm_create(struct snd_cx18_card
*cxsc
)
332 struct snd_card
*sc
= cxsc
->sc
;
333 struct v4l2_device
*v4l2_dev
= cxsc
->v4l2_dev
;
334 struct cx18
*cx
= to_cx18(v4l2_dev
);
337 ret
= snd_pcm_new(sc
, "CX23418 PCM",
338 0, /* PCM device 0, the only one for this card */
339 0, /* 0 playback substreams */
340 1, /* 1 capture substream */
343 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
348 spin_lock_init(&cxsc
->slock
);
350 snd_pcm_set_ops(sp
, SNDRV_PCM_STREAM_CAPTURE
,
351 &snd_cx18_pcm_capture_ops
);
353 sp
->private_data
= cxsc
;
354 strlcpy(sp
->name
, cx
->card_name
, sizeof(sp
->name
));