1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2269c11fbSAndy Walls /*
3269c11fbSAndy Walls * ALSA PCM device for the
4269c11fbSAndy Walls * ALSA interface to ivtv PCM capture streams
5269c11fbSAndy Walls *
6269c11fbSAndy Walls * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net>
7269c11fbSAndy Walls * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
8269c11fbSAndy Walls *
9269c11fbSAndy Walls * Portions of this work were sponsored by ONELAN Limited for the cx18 driver
10269c11fbSAndy Walls */
11269c11fbSAndy Walls
12269c11fbSAndy Walls #include "ivtv-driver.h"
13269c11fbSAndy Walls #include "ivtv-queue.h"
14269c11fbSAndy Walls #include "ivtv-streams.h"
15269c11fbSAndy Walls #include "ivtv-fileops.h"
16269c11fbSAndy Walls #include "ivtv-alsa.h"
172aebbf67SMauro Carvalho Chehab #include "ivtv-alsa-pcm.h"
18269c11fbSAndy Walls
19bbdba43fSMauro Carvalho Chehab #include <sound/core.h>
20bbdba43fSMauro Carvalho Chehab #include <sound/pcm.h>
21bbdba43fSMauro Carvalho Chehab
22bbdba43fSMauro Carvalho Chehab
23269c11fbSAndy Walls static unsigned int pcm_debug;
24269c11fbSAndy Walls module_param(pcm_debug, int, 0644);
25269c11fbSAndy Walls MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
26269c11fbSAndy Walls
27269c11fbSAndy Walls #define dprintk(fmt, arg...) \
28269c11fbSAndy Walls do { \
29269c11fbSAndy Walls if (pcm_debug) \
30269c11fbSAndy Walls pr_info("ivtv-alsa-pcm %s: " fmt, __func__, ##arg); \
31269c11fbSAndy Walls } while (0)
32269c11fbSAndy Walls
3347f14314SBhumika Goyal static const struct snd_pcm_hardware snd_ivtv_hw_capture = {
34269c11fbSAndy Walls .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
35269c11fbSAndy Walls SNDRV_PCM_INFO_MMAP |
36269c11fbSAndy Walls SNDRV_PCM_INFO_INTERLEAVED |
37269c11fbSAndy Walls SNDRV_PCM_INFO_MMAP_VALID,
38269c11fbSAndy Walls
39269c11fbSAndy Walls .formats = SNDRV_PCM_FMTBIT_S16_LE,
40269c11fbSAndy Walls
41269c11fbSAndy Walls .rates = SNDRV_PCM_RATE_48000,
42269c11fbSAndy Walls
43269c11fbSAndy Walls .rate_min = 48000,
44269c11fbSAndy Walls .rate_max = 48000,
45269c11fbSAndy Walls .channels_min = 2,
46269c11fbSAndy Walls .channels_max = 2,
47269c11fbSAndy Walls .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
48269c11fbSAndy Walls .period_bytes_min = 64, /* 12544/2, */
49269c11fbSAndy Walls .period_bytes_max = 12544,
50269c11fbSAndy Walls .periods_min = 2,
51269c11fbSAndy Walls .periods_max = 98, /* 12544, */
52269c11fbSAndy Walls };
53269c11fbSAndy Walls
ivtv_alsa_announce_pcm_data(struct snd_ivtv_card * itvsc,u8 * pcm_data,size_t num_bytes)542aebbf67SMauro Carvalho Chehab static void ivtv_alsa_announce_pcm_data(struct snd_ivtv_card *itvsc,
552aebbf67SMauro Carvalho Chehab u8 *pcm_data,
56269c11fbSAndy Walls size_t num_bytes)
57269c11fbSAndy Walls {
58269c11fbSAndy Walls struct snd_pcm_substream *substream;
59269c11fbSAndy Walls struct snd_pcm_runtime *runtime;
60269c11fbSAndy Walls unsigned int oldptr;
61269c11fbSAndy Walls unsigned int stride;
62269c11fbSAndy Walls int period_elapsed = 0;
63269c11fbSAndy Walls int length;
64269c11fbSAndy Walls
65339f06c5SMauro Carvalho Chehab dprintk("ivtv alsa announce ptr=%p data=%p num_bytes=%zu\n", itvsc,
66269c11fbSAndy Walls pcm_data, num_bytes);
67269c11fbSAndy Walls
68269c11fbSAndy Walls substream = itvsc->capture_pcm_substream;
69269c11fbSAndy Walls if (substream == NULL) {
70269c11fbSAndy Walls dprintk("substream was NULL\n");
71269c11fbSAndy Walls return;
72269c11fbSAndy Walls }
73269c11fbSAndy Walls
74269c11fbSAndy Walls runtime = substream->runtime;
75269c11fbSAndy Walls if (runtime == NULL) {
76269c11fbSAndy Walls dprintk("runtime was NULL\n");
77269c11fbSAndy Walls return;
78269c11fbSAndy Walls }
79269c11fbSAndy Walls
80269c11fbSAndy Walls stride = runtime->frame_bits >> 3;
81269c11fbSAndy Walls if (stride == 0) {
82269c11fbSAndy Walls dprintk("stride is zero\n");
83269c11fbSAndy Walls return;
84269c11fbSAndy Walls }
85269c11fbSAndy Walls
86269c11fbSAndy Walls length = num_bytes / stride;
87269c11fbSAndy Walls if (length == 0) {
88269c11fbSAndy Walls dprintk("%s: length was zero\n", __func__);
89269c11fbSAndy Walls return;
90269c11fbSAndy Walls }
91269c11fbSAndy Walls
92269c11fbSAndy Walls if (runtime->dma_area == NULL) {
93269c11fbSAndy Walls dprintk("dma area was NULL - ignoring\n");
94269c11fbSAndy Walls return;
95269c11fbSAndy Walls }
96269c11fbSAndy Walls
97269c11fbSAndy Walls oldptr = itvsc->hwptr_done_capture;
98269c11fbSAndy Walls if (oldptr + length >= runtime->buffer_size) {
99269c11fbSAndy Walls unsigned int cnt =
100269c11fbSAndy Walls runtime->buffer_size - oldptr;
101269c11fbSAndy Walls memcpy(runtime->dma_area + oldptr * stride, pcm_data,
102269c11fbSAndy Walls cnt * stride);
103269c11fbSAndy Walls memcpy(runtime->dma_area, pcm_data + cnt * stride,
104269c11fbSAndy Walls length * stride - cnt * stride);
105269c11fbSAndy Walls } else {
106269c11fbSAndy Walls memcpy(runtime->dma_area + oldptr * stride, pcm_data,
107269c11fbSAndy Walls length * stride);
108269c11fbSAndy Walls }
109269c11fbSAndy Walls snd_pcm_stream_lock(substream);
110269c11fbSAndy Walls
111269c11fbSAndy Walls itvsc->hwptr_done_capture += length;
112269c11fbSAndy Walls if (itvsc->hwptr_done_capture >=
113269c11fbSAndy Walls runtime->buffer_size)
114269c11fbSAndy Walls itvsc->hwptr_done_capture -=
115269c11fbSAndy Walls runtime->buffer_size;
116269c11fbSAndy Walls
117269c11fbSAndy Walls itvsc->capture_transfer_done += length;
118269c11fbSAndy Walls if (itvsc->capture_transfer_done >=
119269c11fbSAndy Walls runtime->period_size) {
120269c11fbSAndy Walls itvsc->capture_transfer_done -=
121269c11fbSAndy Walls runtime->period_size;
122269c11fbSAndy Walls period_elapsed = 1;
123269c11fbSAndy Walls }
124269c11fbSAndy Walls
125269c11fbSAndy Walls snd_pcm_stream_unlock(substream);
126269c11fbSAndy Walls
127269c11fbSAndy Walls if (period_elapsed)
128269c11fbSAndy Walls snd_pcm_period_elapsed(substream);
129269c11fbSAndy Walls }
130269c11fbSAndy Walls
snd_ivtv_pcm_capture_open(struct snd_pcm_substream * substream)131269c11fbSAndy Walls static int snd_ivtv_pcm_capture_open(struct snd_pcm_substream *substream)
132269c11fbSAndy Walls {
133269c11fbSAndy Walls struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
134269c11fbSAndy Walls struct snd_pcm_runtime *runtime = substream->runtime;
135269c11fbSAndy Walls struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
136269c11fbSAndy Walls struct ivtv *itv = to_ivtv(v4l2_dev);
137269c11fbSAndy Walls struct ivtv_stream *s;
138269c11fbSAndy Walls struct ivtv_open_id item;
139269c11fbSAndy Walls int ret;
140269c11fbSAndy Walls
141269c11fbSAndy Walls /* Instruct the CX2341[56] to start sending packets */
142269c11fbSAndy Walls snd_ivtv_lock(itvsc);
143deb29e90STakashi Iwai
144deb29e90STakashi Iwai if (ivtv_init_on_first_open(itv)) {
145deb29e90STakashi Iwai snd_ivtv_unlock(itvsc);
146deb29e90STakashi Iwai return -ENXIO;
147deb29e90STakashi Iwai }
148deb29e90STakashi Iwai
149269c11fbSAndy Walls s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
150269c11fbSAndy Walls
151635d62f0SHans Verkuil v4l2_fh_init(&item.fh, &s->vdev);
152269c11fbSAndy Walls item.itv = itv;
153269c11fbSAndy Walls item.type = s->type;
154269c11fbSAndy Walls
155269c11fbSAndy Walls /* See if the stream is available */
156269c11fbSAndy Walls if (ivtv_claim_stream(&item, item.type)) {
157269c11fbSAndy Walls /* No, it's already in use */
158c5b81fe3SSantosh Kumar Singh v4l2_fh_exit(&item.fh);
159269c11fbSAndy Walls snd_ivtv_unlock(itvsc);
160269c11fbSAndy Walls return -EBUSY;
161269c11fbSAndy Walls }
162269c11fbSAndy Walls
163269c11fbSAndy Walls if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) ||
164269c11fbSAndy Walls test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
165269c11fbSAndy Walls /* We're already streaming. No additional action required */
166269c11fbSAndy Walls snd_ivtv_unlock(itvsc);
167269c11fbSAndy Walls return 0;
168269c11fbSAndy Walls }
169269c11fbSAndy Walls
170269c11fbSAndy Walls
171269c11fbSAndy Walls runtime->hw = snd_ivtv_hw_capture;
172269c11fbSAndy Walls snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
173269c11fbSAndy Walls itvsc->capture_pcm_substream = substream;
174269c11fbSAndy Walls runtime->private_data = itv;
175269c11fbSAndy Walls
176269c11fbSAndy Walls itv->pcm_announce_callback = ivtv_alsa_announce_pcm_data;
177269c11fbSAndy Walls
178269c11fbSAndy Walls /* Not currently streaming, so start it up */
179269c11fbSAndy Walls set_bit(IVTV_F_S_STREAMING, &s->s_flags);
180269c11fbSAndy Walls ret = ivtv_start_v4l2_encode_stream(s);
181269c11fbSAndy Walls snd_ivtv_unlock(itvsc);
182269c11fbSAndy Walls
183269c11fbSAndy Walls return ret;
184269c11fbSAndy Walls }
185269c11fbSAndy Walls
snd_ivtv_pcm_capture_close(struct snd_pcm_substream * substream)186269c11fbSAndy Walls static int snd_ivtv_pcm_capture_close(struct snd_pcm_substream *substream)
187269c11fbSAndy Walls {
188269c11fbSAndy Walls struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
189269c11fbSAndy Walls struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
190269c11fbSAndy Walls struct ivtv *itv = to_ivtv(v4l2_dev);
191269c11fbSAndy Walls struct ivtv_stream *s;
192269c11fbSAndy Walls
193269c11fbSAndy Walls /* Instruct the ivtv to stop sending packets */
194269c11fbSAndy Walls snd_ivtv_lock(itvsc);
195269c11fbSAndy Walls s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
196269c11fbSAndy Walls ivtv_stop_v4l2_encode_stream(s, 0);
197269c11fbSAndy Walls clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
198269c11fbSAndy Walls
199269c11fbSAndy Walls ivtv_release_stream(s);
200269c11fbSAndy Walls
201269c11fbSAndy Walls itv->pcm_announce_callback = NULL;
202269c11fbSAndy Walls snd_ivtv_unlock(itvsc);
203269c11fbSAndy Walls
204269c11fbSAndy Walls return 0;
205269c11fbSAndy Walls }
206269c11fbSAndy Walls
snd_ivtv_pcm_prepare(struct snd_pcm_substream * substream)207269c11fbSAndy Walls static int snd_ivtv_pcm_prepare(struct snd_pcm_substream *substream)
208269c11fbSAndy Walls {
209269c11fbSAndy Walls struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
210269c11fbSAndy Walls
211269c11fbSAndy Walls itvsc->hwptr_done_capture = 0;
212269c11fbSAndy Walls itvsc->capture_transfer_done = 0;
213269c11fbSAndy Walls
214269c11fbSAndy Walls return 0;
215269c11fbSAndy Walls }
216269c11fbSAndy Walls
snd_ivtv_pcm_trigger(struct snd_pcm_substream * substream,int cmd)217269c11fbSAndy Walls static int snd_ivtv_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
218269c11fbSAndy Walls {
219269c11fbSAndy Walls return 0;
220269c11fbSAndy Walls }
221269c11fbSAndy Walls
222269c11fbSAndy Walls static
snd_ivtv_pcm_pointer(struct snd_pcm_substream * substream)223269c11fbSAndy Walls snd_pcm_uframes_t snd_ivtv_pcm_pointer(struct snd_pcm_substream *substream)
224269c11fbSAndy Walls {
225269c11fbSAndy Walls unsigned long flags;
226269c11fbSAndy Walls snd_pcm_uframes_t hwptr_done;
227269c11fbSAndy Walls struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
228269c11fbSAndy Walls
229269c11fbSAndy Walls spin_lock_irqsave(&itvsc->slock, flags);
230269c11fbSAndy Walls hwptr_done = itvsc->hwptr_done_capture;
231269c11fbSAndy Walls spin_unlock_irqrestore(&itvsc->slock, flags);
232269c11fbSAndy Walls
233269c11fbSAndy Walls return hwptr_done;
234269c11fbSAndy Walls }
235269c11fbSAndy Walls
2365c8d8c01SJulia Lawall static const struct snd_pcm_ops snd_ivtv_pcm_capture_ops = {
237269c11fbSAndy Walls .open = snd_ivtv_pcm_capture_open,
238269c11fbSAndy Walls .close = snd_ivtv_pcm_capture_close,
239269c11fbSAndy Walls .prepare = snd_ivtv_pcm_prepare,
240269c11fbSAndy Walls .trigger = snd_ivtv_pcm_trigger,
241269c11fbSAndy Walls .pointer = snd_ivtv_pcm_pointer,
242269c11fbSAndy Walls };
243269c11fbSAndy Walls
snd_ivtv_pcm_create(struct snd_ivtv_card * itvsc)244269c11fbSAndy Walls int snd_ivtv_pcm_create(struct snd_ivtv_card *itvsc)
245269c11fbSAndy Walls {
246269c11fbSAndy Walls struct snd_pcm *sp;
247269c11fbSAndy Walls struct snd_card *sc = itvsc->sc;
248269c11fbSAndy Walls struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
249269c11fbSAndy Walls struct ivtv *itv = to_ivtv(v4l2_dev);
250269c11fbSAndy Walls int ret;
251269c11fbSAndy Walls
252269c11fbSAndy Walls ret = snd_pcm_new(sc, "CX2341[56] PCM",
253269c11fbSAndy Walls 0, /* PCM device 0, the only one for this card */
254269c11fbSAndy Walls 0, /* 0 playback substreams */
255269c11fbSAndy Walls 1, /* 1 capture substream */
256269c11fbSAndy Walls &sp);
257269c11fbSAndy Walls if (ret) {
258269c11fbSAndy Walls IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
259269c11fbSAndy Walls __func__, ret);
260269c11fbSAndy Walls goto err_exit;
261269c11fbSAndy Walls }
262269c11fbSAndy Walls
263269c11fbSAndy Walls spin_lock_init(&itvsc->slock);
264269c11fbSAndy Walls
265269c11fbSAndy Walls snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
266269c11fbSAndy Walls &snd_ivtv_pcm_capture_ops);
267*7ffeb6ceSTakashi Iwai snd_pcm_set_managed_buffer_all(sp, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
268269c11fbSAndy Walls sp->info_flags = 0;
269269c11fbSAndy Walls sp->private_data = itvsc;
270c0decac1SMauro Carvalho Chehab strscpy(sp->name, itv->card_name, sizeof(sp->name));
271269c11fbSAndy Walls
272269c11fbSAndy Walls return 0;
273269c11fbSAndy Walls
274269c11fbSAndy Walls err_exit:
275269c11fbSAndy Walls return ret;
276269c11fbSAndy Walls }
277