xref: /OK3568_Linux_fs/kernel/drivers/media/pci/cx18/cx18-alsa-pcm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  ALSA PCM device for the
4*4882a593Smuzhiyun  *  ALSA interface to cx18 PCM capture streams
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  Copyright (C) 2009  Andy Walls <awalls@md.metrocast.net>
7*4882a593Smuzhiyun  *  Copyright (C) 2009  Devin Heitmueller <dheitmueller@kernellabs.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *  Portions of this work were sponsored by ONELAN Limited.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/init.h>
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <media/v4l2-device.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <sound/core.h>
18*4882a593Smuzhiyun #include <sound/pcm.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "cx18-driver.h"
21*4882a593Smuzhiyun #include "cx18-queue.h"
22*4882a593Smuzhiyun #include "cx18-streams.h"
23*4882a593Smuzhiyun #include "cx18-fileops.h"
24*4882a593Smuzhiyun #include "cx18-alsa.h"
25*4882a593Smuzhiyun #include "cx18-alsa-pcm.h"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static unsigned int pcm_debug;
28*4882a593Smuzhiyun module_param(pcm_debug, int, 0644);
29*4882a593Smuzhiyun MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define dprintk(fmt, arg...) do {					\
32*4882a593Smuzhiyun 	    if (pcm_debug)						\
33*4882a593Smuzhiyun 		printk(KERN_INFO "cx18-alsa-pcm %s: " fmt,		\
34*4882a593Smuzhiyun 				  __func__, ##arg);			\
35*4882a593Smuzhiyun 	} while (0)
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static const struct snd_pcm_hardware snd_cx18_hw_capture = {
38*4882a593Smuzhiyun 	.info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
39*4882a593Smuzhiyun 		SNDRV_PCM_INFO_MMAP           |
40*4882a593Smuzhiyun 		SNDRV_PCM_INFO_INTERLEAVED    |
41*4882a593Smuzhiyun 		SNDRV_PCM_INFO_MMAP_VALID,
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	.rates = SNDRV_PCM_RATE_48000,
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	.rate_min = 48000,
48*4882a593Smuzhiyun 	.rate_max = 48000,
49*4882a593Smuzhiyun 	.channels_min = 2,
50*4882a593Smuzhiyun 	.channels_max = 2,
51*4882a593Smuzhiyun 	.buffer_bytes_max = 62720 * 8,	/* just about the value in usbaudio.c */
52*4882a593Smuzhiyun 	.period_bytes_min = 64,		/* 12544/2, */
53*4882a593Smuzhiyun 	.period_bytes_max = 12544,
54*4882a593Smuzhiyun 	.periods_min = 2,
55*4882a593Smuzhiyun 	.periods_max = 98,		/* 12544, */
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
cx18_alsa_announce_pcm_data(struct snd_cx18_card * cxsc,u8 * pcm_data,size_t num_bytes)58*4882a593Smuzhiyun void cx18_alsa_announce_pcm_data(struct snd_cx18_card *cxsc, u8 *pcm_data,
59*4882a593Smuzhiyun 				 size_t num_bytes)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	struct snd_pcm_substream *substream;
62*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime;
63*4882a593Smuzhiyun 	unsigned int oldptr;
64*4882a593Smuzhiyun 	unsigned int stride;
65*4882a593Smuzhiyun 	int period_elapsed = 0;
66*4882a593Smuzhiyun 	int length;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	dprintk("cx18 alsa announce ptr=%p data=%p num_bytes=%zu\n", cxsc,
69*4882a593Smuzhiyun 		pcm_data, num_bytes);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	substream = cxsc->capture_pcm_substream;
72*4882a593Smuzhiyun 	if (substream == NULL) {
73*4882a593Smuzhiyun 		dprintk("substream was NULL\n");
74*4882a593Smuzhiyun 		return;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	runtime = substream->runtime;
78*4882a593Smuzhiyun 	if (runtime == NULL) {
79*4882a593Smuzhiyun 		dprintk("runtime was NULL\n");
80*4882a593Smuzhiyun 		return;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	stride = runtime->frame_bits >> 3;
84*4882a593Smuzhiyun 	if (stride == 0) {
85*4882a593Smuzhiyun 		dprintk("stride is zero\n");
86*4882a593Smuzhiyun 		return;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	length = num_bytes / stride;
90*4882a593Smuzhiyun 	if (length == 0) {
91*4882a593Smuzhiyun 		dprintk("%s: length was zero\n", __func__);
92*4882a593Smuzhiyun 		return;
93*4882a593Smuzhiyun 	}
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (runtime->dma_area == NULL) {
96*4882a593Smuzhiyun 		dprintk("dma area was NULL - ignoring\n");
97*4882a593Smuzhiyun 		return;
98*4882a593Smuzhiyun 	}
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	oldptr = cxsc->hwptr_done_capture;
101*4882a593Smuzhiyun 	if (oldptr + length >= runtime->buffer_size) {
102*4882a593Smuzhiyun 		unsigned int cnt =
103*4882a593Smuzhiyun 			runtime->buffer_size - oldptr;
104*4882a593Smuzhiyun 		memcpy(runtime->dma_area + oldptr * stride, pcm_data,
105*4882a593Smuzhiyun 		       cnt * stride);
106*4882a593Smuzhiyun 		memcpy(runtime->dma_area, pcm_data + cnt * stride,
107*4882a593Smuzhiyun 		       length * stride - cnt * stride);
108*4882a593Smuzhiyun 	} else {
109*4882a593Smuzhiyun 		memcpy(runtime->dma_area + oldptr * stride, pcm_data,
110*4882a593Smuzhiyun 		       length * stride);
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 	snd_pcm_stream_lock(substream);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	cxsc->hwptr_done_capture += length;
115*4882a593Smuzhiyun 	if (cxsc->hwptr_done_capture >=
116*4882a593Smuzhiyun 	    runtime->buffer_size)
117*4882a593Smuzhiyun 		cxsc->hwptr_done_capture -=
118*4882a593Smuzhiyun 			runtime->buffer_size;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	cxsc->capture_transfer_done += length;
121*4882a593Smuzhiyun 	if (cxsc->capture_transfer_done >=
122*4882a593Smuzhiyun 	    runtime->period_size) {
123*4882a593Smuzhiyun 		cxsc->capture_transfer_done -=
124*4882a593Smuzhiyun 			runtime->period_size;
125*4882a593Smuzhiyun 		period_elapsed = 1;
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	snd_pcm_stream_unlock(substream);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	if (period_elapsed)
131*4882a593Smuzhiyun 		snd_pcm_period_elapsed(substream);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
snd_cx18_pcm_capture_open(struct snd_pcm_substream * substream)134*4882a593Smuzhiyun static int snd_cx18_pcm_capture_open(struct snd_pcm_substream *substream)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
137*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
138*4882a593Smuzhiyun 	struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
139*4882a593Smuzhiyun 	struct cx18 *cx = to_cx18(v4l2_dev);
140*4882a593Smuzhiyun 	struct cx18_stream *s;
141*4882a593Smuzhiyun 	struct cx18_open_id item;
142*4882a593Smuzhiyun 	int ret;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	/* Instruct the cx18 to start sending packets */
145*4882a593Smuzhiyun 	snd_cx18_lock(cxsc);
146*4882a593Smuzhiyun 	s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	item.cx = cx;
149*4882a593Smuzhiyun 	item.type = s->type;
150*4882a593Smuzhiyun 	item.open_id = cx->open_id++;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	/* See if the stream is available */
153*4882a593Smuzhiyun 	if (cx18_claim_stream(&item, item.type)) {
154*4882a593Smuzhiyun 		/* No, it's already in use */
155*4882a593Smuzhiyun 		snd_cx18_unlock(cxsc);
156*4882a593Smuzhiyun 		return -EBUSY;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
160*4882a593Smuzhiyun 	    test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
161*4882a593Smuzhiyun 		/* We're already streaming.  No additional action required */
162*4882a593Smuzhiyun 		snd_cx18_unlock(cxsc);
163*4882a593Smuzhiyun 		return 0;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	runtime->hw = snd_cx18_hw_capture;
168*4882a593Smuzhiyun 	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
169*4882a593Smuzhiyun 	cxsc->capture_pcm_substream = substream;
170*4882a593Smuzhiyun 	runtime->private_data = cx;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	cx->pcm_announce_callback = cx18_alsa_announce_pcm_data;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* Not currently streaming, so start it up */
175*4882a593Smuzhiyun 	set_bit(CX18_F_S_STREAMING, &s->s_flags);
176*4882a593Smuzhiyun 	ret = cx18_start_v4l2_encode_stream(s);
177*4882a593Smuzhiyun 	snd_cx18_unlock(cxsc);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	return ret;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
snd_cx18_pcm_capture_close(struct snd_pcm_substream * substream)182*4882a593Smuzhiyun static int snd_cx18_pcm_capture_close(struct snd_pcm_substream *substream)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
185*4882a593Smuzhiyun 	struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
186*4882a593Smuzhiyun 	struct cx18 *cx = to_cx18(v4l2_dev);
187*4882a593Smuzhiyun 	struct cx18_stream *s;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/* Instruct the cx18 to stop sending packets */
190*4882a593Smuzhiyun 	snd_cx18_lock(cxsc);
191*4882a593Smuzhiyun 	s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
192*4882a593Smuzhiyun 	cx18_stop_v4l2_encode_stream(s, 0);
193*4882a593Smuzhiyun 	clear_bit(CX18_F_S_STREAMING, &s->s_flags);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	cx18_release_stream(s);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	cx->pcm_announce_callback = NULL;
198*4882a593Smuzhiyun 	snd_cx18_unlock(cxsc);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	return 0;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
snd_cx18_pcm_prepare(struct snd_pcm_substream * substream)203*4882a593Smuzhiyun static int snd_cx18_pcm_prepare(struct snd_pcm_substream *substream)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	cxsc->hwptr_done_capture = 0;
208*4882a593Smuzhiyun 	cxsc->capture_transfer_done = 0;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	return 0;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
snd_cx18_pcm_trigger(struct snd_pcm_substream * substream,int cmd)213*4882a593Smuzhiyun static int snd_cx18_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun 	return 0;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun static
snd_cx18_pcm_pointer(struct snd_pcm_substream * substream)219*4882a593Smuzhiyun snd_pcm_uframes_t snd_cx18_pcm_pointer(struct snd_pcm_substream *substream)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	unsigned long flags;
222*4882a593Smuzhiyun 	snd_pcm_uframes_t hwptr_done;
223*4882a593Smuzhiyun 	struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	spin_lock_irqsave(&cxsc->slock, flags);
226*4882a593Smuzhiyun 	hwptr_done = cxsc->hwptr_done_capture;
227*4882a593Smuzhiyun 	spin_unlock_irqrestore(&cxsc->slock, flags);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	return hwptr_done;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun static const struct snd_pcm_ops snd_cx18_pcm_capture_ops = {
233*4882a593Smuzhiyun 	.open		= snd_cx18_pcm_capture_open,
234*4882a593Smuzhiyun 	.close		= snd_cx18_pcm_capture_close,
235*4882a593Smuzhiyun 	.prepare	= snd_cx18_pcm_prepare,
236*4882a593Smuzhiyun 	.trigger	= snd_cx18_pcm_trigger,
237*4882a593Smuzhiyun 	.pointer	= snd_cx18_pcm_pointer,
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun 
snd_cx18_pcm_create(struct snd_cx18_card * cxsc)240*4882a593Smuzhiyun int snd_cx18_pcm_create(struct snd_cx18_card *cxsc)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	struct snd_pcm *sp;
243*4882a593Smuzhiyun 	struct snd_card *sc = cxsc->sc;
244*4882a593Smuzhiyun 	struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
245*4882a593Smuzhiyun 	struct cx18 *cx = to_cx18(v4l2_dev);
246*4882a593Smuzhiyun 	int ret;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	ret = snd_pcm_new(sc, "CX23418 PCM",
249*4882a593Smuzhiyun 			  0, /* PCM device 0, the only one for this card */
250*4882a593Smuzhiyun 			  0, /* 0 playback substreams */
251*4882a593Smuzhiyun 			  1, /* 1 capture substream */
252*4882a593Smuzhiyun 			  &sp);
253*4882a593Smuzhiyun 	if (ret) {
254*4882a593Smuzhiyun 		CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
255*4882a593Smuzhiyun 			      __func__, ret);
256*4882a593Smuzhiyun 		goto err_exit;
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	spin_lock_init(&cxsc->slock);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
262*4882a593Smuzhiyun 			&snd_cx18_pcm_capture_ops);
263*4882a593Smuzhiyun 	snd_pcm_set_managed_buffer_all(sp, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
264*4882a593Smuzhiyun 	sp->info_flags = 0;
265*4882a593Smuzhiyun 	sp->private_data = cxsc;
266*4882a593Smuzhiyun 	strscpy(sp->name, cx->card_name, sizeof(sp->name));
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	return 0;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun err_exit:
271*4882a593Smuzhiyun 	return ret;
272*4882a593Smuzhiyun }
273