]>
Commit | Line | Data |
---|---|---|
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 |
40 | static unsigned int pcm_debug; |
41 | module_param(pcm_debug, int, 0644); | |
42 | MODULE_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 | ||
50 | /* Forward Declaration */ | |
51 | void cx18_alsa_announce_pcm_data(struct snd_cx18_card *cxsc, u8 *pcm_data, | |
52 | size_t num_bytes); | |
53 | ||
54 | static struct snd_pcm_hardware snd_cx18_hw_capture = { | |
55 | .info = SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
56 | SNDRV_PCM_INFO_MMAP | | |
57 | SNDRV_PCM_INFO_INTERLEAVED | | |
58 | SNDRV_PCM_INFO_MMAP_VALID, | |
59 | ||
60 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | |
61 | ||
62 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT, | |
63 | ||
64 | .rate_min = 48000, | |
65 | .rate_max = 48000, | |
66 | .channels_min = 2, | |
67 | .channels_max = 2, | |
68 | .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */ | |
69 | .period_bytes_min = 64, /* 12544/2, */ | |
70 | .period_bytes_max = 12544, | |
71 | .periods_min = 2, | |
72 | .periods_max = 98, /* 12544, */ | |
73 | }; | |
74 | ||
f9b071c7 AW |
75 | static int snd_cx18_pcm_capture_open(struct snd_pcm_substream *substream) |
76 | { | |
77 | struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream); | |
78 | struct snd_pcm_runtime *runtime = substream->runtime; | |
79 | struct v4l2_device *v4l2_dev = cxsc->v4l2_dev; | |
f9b071c7 | 80 | struct cx18 *cx = to_cx18(v4l2_dev); |
9972de90 DH |
81 | struct cx18_stream *s; |
82 | struct cx18_open_id *item; | |
83 | int ret; | |
84 | ||
85 | /* Instruct the cx18 to start sending packets */ | |
86 | s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM]; | |
87 | ||
88 | /* Allocate memory */ | |
89 | item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL); | |
780edb7a | 90 | if (NULL == item) |
9972de90 | 91 | return -ENOMEM; |
780edb7a | 92 | |
9972de90 DH |
93 | item->cx = cx; |
94 | item->type = s->type; | |
95 | item->open_id = cx->open_id++; | |
96 | ||
97 | /* See if the stream is available */ | |
98 | if (cx18_claim_stream(item, item->type)) { | |
99 | /* No, it's already in use */ | |
1a8e0e33 | 100 | kfree(item); |
9972de90 DH |
101 | return -EBUSY; |
102 | } | |
103 | ||
104 | if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) || | |
105 | test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) { | |
106 | /* We're already streaming. No additional action required */ | |
107 | return 0; | |
108 | } | |
109 | ||
110 | ||
111 | runtime->hw = snd_cx18_hw_capture; | |
112 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); | |
113 | cxsc->capture_pcm_substream = substream; | |
114 | runtime->private_data = cx; | |
115 | ||
116 | cx->pcm_announce_callback = cx18_alsa_announce_pcm_data; | |
117 | ||
118 | /* Not currently streaming, so start it up */ | |
119 | set_bit(CX18_F_S_STREAMING, &s->s_flags); | |
120 | ret = cx18_start_v4l2_encode_stream(s); | |
121 | ||
f9b071c7 AW |
122 | return 0; |
123 | } | |
124 | ||
125 | static int snd_cx18_pcm_capture_close(struct snd_pcm_substream *substream) | |
126 | { | |
9972de90 DH |
127 | unsigned long flags; |
128 | struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream); | |
129 | struct v4l2_device *v4l2_dev = cxsc->v4l2_dev; | |
130 | struct cx18 *cx = to_cx18(v4l2_dev); | |
131 | struct cx18_stream *s; | |
132 | int ret; | |
133 | ||
134 | /* Instruct the cx18 to stop sending packets */ | |
135 | s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM]; | |
136 | ret = cx18_stop_v4l2_encode_stream(s, 0); | |
137 | clear_bit(CX18_F_S_STREAMING, &s->s_flags); | |
138 | ||
139 | cx18_release_stream(s); | |
140 | ||
141 | cx->pcm_announce_callback = NULL; | |
142 | ||
143 | spin_lock_irqsave(&cxsc->slock, flags); | |
144 | if (substream->runtime->dma_area) { | |
145 | dprintk("freeing pcm capture region\n"); | |
146 | vfree(substream->runtime->dma_area); | |
147 | substream->runtime->dma_area = NULL; | |
148 | } | |
149 | spin_unlock_irqrestore(&cxsc->slock, flags); | |
150 | ||
f9b071c7 AW |
151 | return 0; |
152 | } | |
153 | ||
154 | static int snd_cx18_pcm_ioctl(struct snd_pcm_substream *substream, | |
155 | unsigned int cmd, void *arg) | |
156 | { | |
157 | return snd_pcm_lib_ioctl(substream, cmd, arg); | |
158 | } | |
159 | ||
9972de90 DH |
160 | |
161 | static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, | |
162 | size_t size) | |
163 | { | |
164 | struct snd_pcm_runtime *runtime = subs->runtime; | |
165 | ||
166 | dprintk("Allocating vbuffer\n"); | |
167 | if (runtime->dma_area) { | |
168 | if (runtime->dma_bytes > size) | |
169 | return 0; | |
170 | ||
171 | vfree(runtime->dma_area); | |
172 | } | |
173 | runtime->dma_area = vmalloc(size); | |
174 | if (!runtime->dma_area) | |
175 | return -ENOMEM; | |
176 | ||
177 | runtime->dma_bytes = size; | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
f9b071c7 AW |
182 | static int snd_cx18_pcm_hw_params(struct snd_pcm_substream *substream, |
183 | struct snd_pcm_hw_params *params) | |
184 | { | |
9972de90 DH |
185 | int ret; |
186 | ||
187 | dprintk("%s called\n", __func__); | |
188 | ||
189 | ret = snd_pcm_alloc_vmalloc_buffer(substream, | |
190 | params_buffer_bytes(params)); | |
f9b071c7 AW |
191 | return 0; |
192 | } | |
193 | ||
194 | static int snd_cx18_pcm_hw_free(struct snd_pcm_substream *substream) | |
195 | { | |
196 | return 0; | |
197 | } | |
198 | ||
199 | static int snd_cx18_pcm_prepare(struct snd_pcm_substream *substream) | |
200 | { | |
9972de90 DH |
201 | struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream); |
202 | ||
203 | cxsc->hwptr_done_capture = 0; | |
204 | cxsc->capture_transfer_done = 0; | |
205 | ||
f9b071c7 AW |
206 | return 0; |
207 | } | |
208 | ||
209 | static int snd_cx18_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
210 | { | |
211 | return 0; | |
212 | } | |
213 | ||
214 | static | |
215 | snd_pcm_uframes_t snd_cx18_pcm_pointer(struct snd_pcm_substream *substream) | |
216 | { | |
9972de90 DH |
217 | unsigned long flags; |
218 | snd_pcm_uframes_t hwptr_done; | |
219 | struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream); | |
220 | ||
221 | spin_lock_irqsave(&cxsc->slock, flags); | |
222 | hwptr_done = cxsc->hwptr_done_capture; | |
223 | spin_unlock_irqrestore(&cxsc->slock, flags); | |
224 | ||
225 | return hwptr_done; | |
226 | } | |
227 | ||
9972de90 DH |
228 | static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs, |
229 | unsigned long offset) | |
230 | { | |
231 | void *pageptr = subs->runtime->dma_area + offset; | |
232 | ||
233 | return vmalloc_to_page(pageptr); | |
f9b071c7 AW |
234 | } |
235 | ||
236 | static struct snd_pcm_ops snd_cx18_pcm_capture_ops = { | |
237 | .open = snd_cx18_pcm_capture_open, | |
238 | .close = snd_cx18_pcm_capture_close, | |
239 | .ioctl = snd_cx18_pcm_ioctl, | |
240 | .hw_params = snd_cx18_pcm_hw_params, | |
241 | .hw_free = snd_cx18_pcm_hw_free, | |
242 | .prepare = snd_cx18_pcm_prepare, | |
243 | .trigger = snd_cx18_pcm_trigger, | |
244 | .pointer = snd_cx18_pcm_pointer, | |
9972de90 | 245 | .page = snd_pcm_get_vmalloc_page, |
f9b071c7 AW |
246 | }; |
247 | ||
9972de90 DH |
248 | void cx18_alsa_announce_pcm_data(struct snd_cx18_card *cxsc, u8 *pcm_data, |
249 | size_t num_bytes) | |
250 | { | |
251 | struct snd_pcm_substream *substream; | |
252 | struct snd_pcm_runtime *runtime; | |
253 | unsigned int oldptr; | |
254 | unsigned int stride; | |
255 | int period_elapsed = 0; | |
256 | int length; | |
257 | ||
258 | dprintk("cx18 alsa announce ptr=%p data=%p num_bytes=%d\n", cxsc, | |
259 | pcm_data, num_bytes); | |
260 | ||
261 | substream = cxsc->capture_pcm_substream; | |
262 | if (substream == NULL) { | |
263 | dprintk("substream was NULL\n"); | |
264 | return; | |
265 | } | |
266 | ||
267 | runtime = substream->runtime; | |
268 | if (runtime == NULL) { | |
269 | dprintk("runtime was NULL\n"); | |
270 | return; | |
271 | } | |
272 | ||
273 | stride = runtime->frame_bits >> 3; | |
274 | if (stride == 0) { | |
275 | dprintk("stride is zero\n"); | |
276 | return; | |
277 | } | |
278 | ||
279 | length = num_bytes / stride; | |
280 | if (length == 0) { | |
281 | dprintk("%s: length was zero\n", __func__); | |
282 | return; | |
283 | } | |
284 | ||
285 | if (runtime->dma_area == NULL) { | |
286 | dprintk("dma area was NULL - ignoring\n"); | |
287 | return; | |
288 | } | |
289 | ||
290 | oldptr = cxsc->hwptr_done_capture; | |
291 | if (oldptr + length >= runtime->buffer_size) { | |
292 | unsigned int cnt = | |
293 | runtime->buffer_size - oldptr; | |
294 | memcpy(runtime->dma_area + oldptr * stride, pcm_data, | |
295 | cnt * stride); | |
296 | memcpy(runtime->dma_area, pcm_data + cnt * stride, | |
297 | length * stride - cnt * stride); | |
298 | } else { | |
299 | memcpy(runtime->dma_area + oldptr * stride, pcm_data, | |
300 | length * stride); | |
301 | } | |
302 | snd_pcm_stream_lock(substream); | |
303 | ||
304 | cxsc->hwptr_done_capture += length; | |
305 | if (cxsc->hwptr_done_capture >= | |
306 | runtime->buffer_size) | |
307 | cxsc->hwptr_done_capture -= | |
308 | runtime->buffer_size; | |
309 | ||
310 | cxsc->capture_transfer_done += length; | |
311 | if (cxsc->capture_transfer_done >= | |
312 | runtime->period_size) { | |
313 | cxsc->capture_transfer_done -= | |
314 | runtime->period_size; | |
315 | period_elapsed = 1; | |
316 | } | |
317 | ||
318 | snd_pcm_stream_unlock(substream); | |
319 | ||
320 | if (period_elapsed) | |
321 | snd_pcm_period_elapsed(substream); | |
322 | } | |
323 | ||
324 | int snd_cx18_pcm_create(struct snd_cx18_card *cxsc) | |
f9b071c7 AW |
325 | { |
326 | struct snd_pcm *sp; | |
327 | struct snd_card *sc = cxsc->sc; | |
328 | struct v4l2_device *v4l2_dev = cxsc->v4l2_dev; | |
329 | struct cx18 *cx = to_cx18(v4l2_dev); | |
330 | int ret; | |
331 | ||
332 | ret = snd_pcm_new(sc, "CX23418 PCM", | |
333 | 0, /* PCM device 0, the only one for this card */ | |
334 | 0, /* 0 playback substreams */ | |
335 | 1, /* 1 capture substream */ | |
336 | &sp); | |
337 | if (ret) { | |
338 | CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n", | |
339 | __func__, ret); | |
340 | goto err_exit; | |
341 | } | |
9972de90 DH |
342 | |
343 | spin_lock_init(&cxsc->slock); | |
344 | ||
780edb7a DH |
345 | snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE, |
346 | &snd_cx18_pcm_capture_ops); | |
9972de90 DH |
347 | sp->info_flags = 0; |
348 | sp->private_data = cxsc; | |
5eb9978f | 349 | strlcpy(sp->name, cx->card_name, sizeof(sp->name)); |
9972de90 | 350 | |
f9b071c7 AW |
351 | return 0; |
352 | ||
353 | err_exit: | |
354 | return ret; | |
355 | } |