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