xref: /linux/drivers/media/pci/saa7134/saa7134-empress.c (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
5  */
6 
7 #include "saa7134.h"
8 #include "saa7134-reg.h"
9 
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/delay.h>
15 
16 #include <media/v4l2-common.h>
17 #include <media/v4l2-event.h>
18 
19 /* ------------------------------------------------------------------ */
20 
21 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
22 MODULE_LICENSE("GPL");
23 
24 static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
25 
26 module_param_array(empress_nr, int, NULL, 0444);
27 MODULE_PARM_DESC(empress_nr,"ts device number");
28 
29 /* ------------------------------------------------------------------ */
30 
31 static int start_streaming(struct vb2_queue *vq, unsigned int count)
32 {
33 	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
34 	struct saa7134_dev *dev = dmaq->dev;
35 	u32 leading_null_bytes = 0;
36 	int err;
37 
38 	err = saa7134_ts_start_streaming(vq, count);
39 	if (err)
40 		return err;
41 
42 	/* If more cards start to need this, then this
43 	   should probably be added to the card definitions. */
44 	switch (dev->board) {
45 	case SAA7134_BOARD_BEHOLD_M6:
46 	case SAA7134_BOARD_BEHOLD_M63:
47 	case SAA7134_BOARD_BEHOLD_M6_EXTRA:
48 		leading_null_bytes = 1;
49 		break;
50 	}
51 	saa_call_all(dev, core, init, leading_null_bytes);
52 	/* Unmute audio */
53 	saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
54 			saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
55 	dev->empress_started = 1;
56 	return 0;
57 }
58 
59 static void stop_streaming(struct vb2_queue *vq)
60 {
61 	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
62 	struct saa7134_dev *dev = dmaq->dev;
63 
64 	saa7134_ts_stop_streaming(vq);
65 	saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
66 	msleep(20);
67 	saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
68 	msleep(100);
69 	/* Mute audio */
70 	saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
71 			saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
72 	dev->empress_started = 0;
73 }
74 
75 static const struct vb2_ops saa7134_empress_qops = {
76 	.queue_setup	= saa7134_ts_queue_setup,
77 	.buf_init	= saa7134_ts_buffer_init,
78 	.buf_prepare	= saa7134_ts_buffer_prepare,
79 	.buf_queue	= saa7134_vb2_buffer_queue,
80 	.wait_prepare	= vb2_ops_wait_prepare,
81 	.wait_finish	= vb2_ops_wait_finish,
82 	.start_streaming = start_streaming,
83 	.stop_streaming = stop_streaming,
84 };
85 
86 /* ------------------------------------------------------------------ */
87 
88 static int empress_enum_fmt_vid_cap(struct file *file, void  *priv,
89 					struct v4l2_fmtdesc *f)
90 {
91 	if (f->index != 0)
92 		return -EINVAL;
93 
94 	f->pixelformat = V4L2_PIX_FMT_MPEG;
95 	return 0;
96 }
97 
98 static int empress_g_fmt_vid_cap(struct file *file, void *priv,
99 				struct v4l2_format *f)
100 {
101 	struct saa7134_dev *dev = video_drvdata(file);
102 	struct v4l2_subdev_format fmt = {
103 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
104 	};
105 	struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
106 
107 	saa_call_all(dev, pad, get_fmt, NULL, &fmt);
108 
109 	v4l2_fill_pix_format(&f->fmt.pix, mbus_fmt);
110 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
111 	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
112 	f->fmt.pix.bytesperline = 0;
113 
114 	return 0;
115 }
116 
117 static int empress_s_fmt_vid_cap(struct file *file, void *priv,
118 				struct v4l2_format *f)
119 {
120 	struct saa7134_dev *dev = video_drvdata(file);
121 	struct v4l2_subdev_format format = {
122 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
123 	};
124 
125 	v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
126 	saa_call_all(dev, pad, set_fmt, NULL, &format);
127 	v4l2_fill_pix_format(&f->fmt.pix, &format.format);
128 
129 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
130 	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
131 	f->fmt.pix.bytesperline = 0;
132 
133 	return 0;
134 }
135 
136 static int empress_try_fmt_vid_cap(struct file *file, void *priv,
137 				struct v4l2_format *f)
138 {
139 	struct saa7134_dev *dev = video_drvdata(file);
140 	struct v4l2_subdev_pad_config pad_cfg;
141 	struct v4l2_subdev_state pad_state = {
142 		.pads = &pad_cfg,
143 	};
144 	struct v4l2_subdev_format format = {
145 		.which = V4L2_SUBDEV_FORMAT_TRY,
146 	};
147 
148 	v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
149 	saa_call_all(dev, pad, set_fmt, &pad_state, &format);
150 	v4l2_fill_pix_format(&f->fmt.pix, &format.format);
151 
152 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
153 	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
154 	f->fmt.pix.bytesperline = 0;
155 
156 	return 0;
157 }
158 
159 static const struct v4l2_file_operations ts_fops =
160 {
161 	.owner	  = THIS_MODULE,
162 	.open	  = v4l2_fh_open,
163 	.release  = vb2_fop_release,
164 	.read	  = vb2_fop_read,
165 	.poll	  = vb2_fop_poll,
166 	.mmap	  = vb2_fop_mmap,
167 	.unlocked_ioctl = video_ioctl2,
168 };
169 
170 static const struct v4l2_ioctl_ops ts_ioctl_ops = {
171 	.vidioc_querycap		= saa7134_querycap,
172 	.vidioc_enum_fmt_vid_cap	= empress_enum_fmt_vid_cap,
173 	.vidioc_try_fmt_vid_cap		= empress_try_fmt_vid_cap,
174 	.vidioc_s_fmt_vid_cap		= empress_s_fmt_vid_cap,
175 	.vidioc_g_fmt_vid_cap		= empress_g_fmt_vid_cap,
176 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
177 	.vidioc_querybuf		= vb2_ioctl_querybuf,
178 	.vidioc_qbuf			= vb2_ioctl_qbuf,
179 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
180 	.vidioc_expbuf			= vb2_ioctl_expbuf,
181 	.vidioc_streamon		= vb2_ioctl_streamon,
182 	.vidioc_streamoff		= vb2_ioctl_streamoff,
183 	.vidioc_g_frequency		= saa7134_g_frequency,
184 	.vidioc_s_frequency		= saa7134_s_frequency,
185 	.vidioc_g_tuner			= saa7134_g_tuner,
186 	.vidioc_s_tuner			= saa7134_s_tuner,
187 	.vidioc_enum_input		= saa7134_enum_input,
188 	.vidioc_g_input			= saa7134_g_input,
189 	.vidioc_s_input			= saa7134_s_input,
190 	.vidioc_s_std			= saa7134_s_std,
191 	.vidioc_g_std			= saa7134_g_std,
192 	.vidioc_querystd		= saa7134_querystd,
193 	.vidioc_log_status		= v4l2_ctrl_log_status,
194 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
195 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
196 };
197 
198 /* ----------------------------------------------------------- */
199 
200 static const struct video_device saa7134_empress_template = {
201 	.name          = "saa7134-empress",
202 	.fops          = &ts_fops,
203 	.ioctl_ops     = &ts_ioctl_ops,
204 
205 	.tvnorms			= SAA7134_NORMS,
206 };
207 
208 static void empress_signal_update(struct work_struct *work)
209 {
210 	struct saa7134_dev* dev =
211 		container_of(work, struct saa7134_dev, empress_workqueue);
212 
213 	if (dev->nosignal) {
214 		pr_debug("no video signal\n");
215 	} else {
216 		pr_debug("video signal acquired\n");
217 	}
218 }
219 
220 static void empress_signal_change(struct saa7134_dev *dev)
221 {
222 	schedule_work(&dev->empress_workqueue);
223 }
224 
225 static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl)
226 {
227 	switch (ctrl->id) {
228 	case V4L2_CID_BRIGHTNESS:
229 	case V4L2_CID_HUE:
230 	case V4L2_CID_CONTRAST:
231 	case V4L2_CID_SATURATION:
232 	case V4L2_CID_AUDIO_MUTE:
233 	case V4L2_CID_AUDIO_VOLUME:
234 	case V4L2_CID_PRIVATE_INVERT:
235 	case V4L2_CID_PRIVATE_AUTOMUTE:
236 		return true;
237 	default:
238 		return false;
239 	}
240 }
241 
242 static int empress_init(struct saa7134_dev *dev)
243 {
244 	struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler;
245 	struct vb2_queue *q;
246 	int err;
247 
248 	pr_debug("%s: %s\n", dev->name, __func__);
249 	dev->empress_dev = video_device_alloc();
250 	if (NULL == dev->empress_dev)
251 		return -ENOMEM;
252 	*(dev->empress_dev) = saa7134_empress_template;
253 	dev->empress_dev->v4l2_dev  = &dev->v4l2_dev;
254 	dev->empress_dev->release = video_device_release;
255 	dev->empress_dev->lock = &dev->lock;
256 	snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
257 		 "%s empress (%s)", dev->name,
258 		 saa7134_boards[dev->board].name);
259 	v4l2_ctrl_handler_init(hdl, 21);
260 	v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter, false);
261 	if (dev->empress_sd)
262 		v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL, true);
263 	if (hdl->error) {
264 		video_device_release(dev->empress_dev);
265 		return hdl->error;
266 	}
267 	dev->empress_dev->ctrl_handler = hdl;
268 
269 	INIT_WORK(&dev->empress_workqueue, empress_signal_update);
270 
271 	q = &dev->empress_vbq;
272 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
273 	/*
274 	 * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle
275 	 * transfers that do not start at the beginning of a page. A USERPTR
276 	 * can start anywhere in a page, so USERPTR support is a no-go.
277 	 */
278 	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
279 	q->drv_priv = &dev->ts_q;
280 	q->ops = &saa7134_empress_qops;
281 	q->gfp_flags = GFP_DMA32;
282 	q->mem_ops = &vb2_dma_sg_memops;
283 	q->buf_struct_size = sizeof(struct saa7134_buf);
284 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
285 	q->lock = &dev->lock;
286 	q->dev = &dev->pci->dev;
287 	err = vb2_queue_init(q);
288 	if (err) {
289 		video_device_release(dev->empress_dev);
290 		dev->empress_dev = NULL;
291 		return err;
292 	}
293 	dev->empress_dev->queue = q;
294 	dev->empress_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
295 					V4L2_CAP_VIDEO_CAPTURE;
296 	if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type != UNSET)
297 		dev->empress_dev->device_caps |= V4L2_CAP_TUNER;
298 
299 	video_set_drvdata(dev->empress_dev, dev);
300 	err = video_register_device(dev->empress_dev,VFL_TYPE_VIDEO,
301 				    empress_nr[dev->nr]);
302 	if (err < 0) {
303 		pr_info("%s: can't register video device\n",
304 		       dev->name);
305 		video_device_release(dev->empress_dev);
306 		dev->empress_dev = NULL;
307 		return err;
308 	}
309 	pr_info("%s: registered device %s [mpeg]\n",
310 	       dev->name, video_device_node_name(dev->empress_dev));
311 
312 	empress_signal_update(&dev->empress_workqueue);
313 	return 0;
314 }
315 
316 static int empress_fini(struct saa7134_dev *dev)
317 {
318 	pr_debug("%s: %s\n", dev->name, __func__);
319 
320 	if (NULL == dev->empress_dev)
321 		return 0;
322 	flush_work(&dev->empress_workqueue);
323 	vb2_video_unregister_device(dev->empress_dev);
324 	v4l2_ctrl_handler_free(&dev->empress_ctrl_handler);
325 	dev->empress_dev = NULL;
326 	return 0;
327 }
328 
329 static struct saa7134_mpeg_ops empress_ops = {
330 	.type          = SAA7134_MPEG_EMPRESS,
331 	.init          = empress_init,
332 	.fini          = empress_fini,
333 	.signal_change = empress_signal_change,
334 };
335 
336 static int __init empress_register(void)
337 {
338 	return saa7134_ts_register(&empress_ops);
339 }
340 
341 static void __exit empress_unregister(void)
342 {
343 	saa7134_ts_unregister(&empress_ops);
344 }
345 
346 module_init(empress_register);
347 module_exit(empress_unregister);
348