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