xref: /linux/drivers/media/pci/saa7164/saa7164-encoder.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include "saa7164.h"
23 
24 #define ENCODER_MAX_BITRATE 6500000
25 #define ENCODER_MIN_BITRATE 1000000
26 #define ENCODER_DEF_BITRATE 5000000
27 
28 static struct saa7164_tvnorm saa7164_tvnorms[] = {
29 	{
30 		.name      = "NTSC-M",
31 		.id        = V4L2_STD_NTSC_M,
32 	}, {
33 		.name      = "NTSC-JP",
34 		.id        = V4L2_STD_NTSC_M_JP,
35 	}
36 };
37 
38 static const u32 saa7164_v4l2_ctrls[] = {
39 	V4L2_CID_BRIGHTNESS,
40 	V4L2_CID_CONTRAST,
41 	V4L2_CID_SATURATION,
42 	V4L2_CID_HUE,
43 	V4L2_CID_AUDIO_VOLUME,
44 	V4L2_CID_SHARPNESS,
45 	V4L2_CID_MPEG_STREAM_TYPE,
46 	V4L2_CID_MPEG_VIDEO_ASPECT,
47 	V4L2_CID_MPEG_VIDEO_B_FRAMES,
48 	V4L2_CID_MPEG_VIDEO_GOP_SIZE,
49 	V4L2_CID_MPEG_AUDIO_MUTE,
50 	V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
51 	V4L2_CID_MPEG_VIDEO_BITRATE,
52 	V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
53 	0
54 };
55 
56 /* Take the encoder configuration form the port struct and
57  * flush it to the hardware.
58  */
59 static void saa7164_encoder_configure(struct saa7164_port *port)
60 {
61 	struct saa7164_dev *dev = port->dev;
62 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
63 
64 	port->encoder_params.width = port->width;
65 	port->encoder_params.height = port->height;
66 	port->encoder_params.is_50hz =
67 		(port->encodernorm.id & V4L2_STD_625_50) != 0;
68 
69 	/* Set up the DIF (enable it) for analog mode by default */
70 	saa7164_api_initialize_dif(port);
71 
72 	/* Configure the correct video standard */
73 	saa7164_api_configure_dif(port, port->encodernorm.id);
74 
75 	/* Ensure the audio decoder is correct configured */
76 	saa7164_api_set_audio_std(port);
77 }
78 
79 static int saa7164_encoder_buffers_dealloc(struct saa7164_port *port)
80 {
81 	struct list_head *c, *n, *p, *q, *l, *v;
82 	struct saa7164_dev *dev = port->dev;
83 	struct saa7164_buffer *buf;
84 	struct saa7164_user_buffer *ubuf;
85 
86 	/* Remove any allocated buffers */
87 	mutex_lock(&port->dmaqueue_lock);
88 
89 	dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
90 	list_for_each_safe(c, n, &port->dmaqueue.list) {
91 		buf = list_entry(c, struct saa7164_buffer, list);
92 		list_del(c);
93 		saa7164_buffer_dealloc(buf);
94 	}
95 
96 	dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
97 	list_for_each_safe(p, q, &port->list_buf_used.list) {
98 		ubuf = list_entry(p, struct saa7164_user_buffer, list);
99 		list_del(p);
100 		saa7164_buffer_dealloc_user(ubuf);
101 	}
102 
103 	dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
104 	list_for_each_safe(l, v, &port->list_buf_free.list) {
105 		ubuf = list_entry(l, struct saa7164_user_buffer, list);
106 		list_del(l);
107 		saa7164_buffer_dealloc_user(ubuf);
108 	}
109 
110 	mutex_unlock(&port->dmaqueue_lock);
111 	dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
112 
113 	return 0;
114 }
115 
116 /* Dynamic buffer switch at encoder start time */
117 static int saa7164_encoder_buffers_alloc(struct saa7164_port *port)
118 {
119 	struct saa7164_dev *dev = port->dev;
120 	struct saa7164_buffer *buf;
121 	struct saa7164_user_buffer *ubuf;
122 	struct tmHWStreamParameters *params = &port->hw_streamingparams;
123 	int result = -ENODEV, i;
124 	int len = 0;
125 
126 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
127 
128 	if (port->encoder_params.stream_type ==
129 		V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
130 		dprintk(DBGLVL_ENC,
131 			"%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n",
132 			__func__);
133 		params->samplesperline = 128;
134 		params->numberoflines = 256;
135 		params->pitch = 128;
136 		params->numpagetables = 2 +
137 			((SAA7164_PS_NUMBER_OF_LINES * 128) / PAGE_SIZE);
138 	} else
139 	if (port->encoder_params.stream_type ==
140 		V4L2_MPEG_STREAM_TYPE_MPEG2_TS) {
141 		dprintk(DBGLVL_ENC,
142 			"%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n",
143 			__func__);
144 		params->samplesperline = 188;
145 		params->numberoflines = 312;
146 		params->pitch = 188;
147 		params->numpagetables = 2 +
148 			((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
149 	} else
150 		BUG();
151 
152 	/* Init and establish defaults */
153 	params->bitspersample = 8;
154 	params->linethreshold = 0;
155 	params->pagetablelistvirt = NULL;
156 	params->pagetablelistphys = NULL;
157 	params->numpagetableentries = port->hwcfg.buffercount;
158 
159 	/* Allocate the PCI resources, buffers (hard) */
160 	for (i = 0; i < port->hwcfg.buffercount; i++) {
161 		buf = saa7164_buffer_alloc(port,
162 			params->numberoflines *
163 			params->pitch);
164 
165 		if (!buf) {
166 			printk(KERN_ERR "%s() failed "
167 			       "(errno = %d), unable to allocate buffer\n",
168 				__func__, result);
169 			result = -ENOMEM;
170 			goto failed;
171 		} else {
172 
173 			mutex_lock(&port->dmaqueue_lock);
174 			list_add_tail(&buf->list, &port->dmaqueue.list);
175 			mutex_unlock(&port->dmaqueue_lock);
176 
177 		}
178 	}
179 
180 	/* Allocate some kernel buffers for copying
181 	 * to userpsace.
182 	 */
183 	len = params->numberoflines * params->pitch;
184 
185 	if (encoder_buffers < 16)
186 		encoder_buffers = 16;
187 	if (encoder_buffers > 512)
188 		encoder_buffers = 512;
189 
190 	for (i = 0; i < encoder_buffers; i++) {
191 
192 		ubuf = saa7164_buffer_alloc_user(dev, len);
193 		if (ubuf) {
194 			mutex_lock(&port->dmaqueue_lock);
195 			list_add_tail(&ubuf->list, &port->list_buf_free.list);
196 			mutex_unlock(&port->dmaqueue_lock);
197 		}
198 
199 	}
200 
201 	result = 0;
202 
203 failed:
204 	return result;
205 }
206 
207 static int saa7164_encoder_initialize(struct saa7164_port *port)
208 {
209 	saa7164_encoder_configure(port);
210 	return 0;
211 }
212 
213 /* -- V4L2 --------------------------------------------------------- */
214 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
215 {
216 	struct saa7164_encoder_fh *fh = file->private_data;
217 	struct saa7164_port *port = fh->port;
218 	struct saa7164_dev *dev = port->dev;
219 	unsigned int i;
220 
221 	dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)id);
222 
223 	for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
224 		if (id & saa7164_tvnorms[i].id)
225 			break;
226 	}
227 	if (i == ARRAY_SIZE(saa7164_tvnorms))
228 		return -EINVAL;
229 
230 	port->encodernorm = saa7164_tvnorms[i];
231 	port->std = id;
232 
233 	/* Update the audio decoder while is not running in
234 	 * auto detect mode.
235 	 */
236 	saa7164_api_set_audio_std(port);
237 
238 	dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)id);
239 
240 	return 0;
241 }
242 
243 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
244 {
245 	struct saa7164_encoder_fh *fh = file->private_data;
246 	struct saa7164_port *port = fh->port;
247 
248 	*id = port->std;
249 	return 0;
250 }
251 
252 static int vidioc_enum_input(struct file *file, void *priv,
253 	struct v4l2_input *i)
254 {
255 	int n;
256 
257 	char *inputs[] = { "tuner", "composite", "svideo", "aux",
258 		"composite 2", "svideo 2", "aux 2" };
259 
260 	if (i->index >= 7)
261 		return -EINVAL;
262 
263 	strcpy(i->name, inputs[i->index]);
264 
265 	if (i->index == 0)
266 		i->type = V4L2_INPUT_TYPE_TUNER;
267 	else
268 		i->type  = V4L2_INPUT_TYPE_CAMERA;
269 
270 	for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
271 		i->std |= saa7164_tvnorms[n].id;
272 
273 	return 0;
274 }
275 
276 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
277 {
278 	struct saa7164_encoder_fh *fh = file->private_data;
279 	struct saa7164_port *port = fh->port;
280 	struct saa7164_dev *dev = port->dev;
281 
282 	if (saa7164_api_get_videomux(port) != SAA_OK)
283 		return -EIO;
284 
285 	*i = (port->mux_input - 1);
286 
287 	dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);
288 
289 	return 0;
290 }
291 
292 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
293 {
294 	struct saa7164_encoder_fh *fh = file->private_data;
295 	struct saa7164_port *port = fh->port;
296 	struct saa7164_dev *dev = port->dev;
297 
298 	dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);
299 
300 	if (i >= 7)
301 		return -EINVAL;
302 
303 	port->mux_input = i + 1;
304 
305 	if (saa7164_api_set_videomux(port) != SAA_OK)
306 		return -EIO;
307 
308 	return 0;
309 }
310 
311 static int vidioc_g_tuner(struct file *file, void *priv,
312 	struct v4l2_tuner *t)
313 {
314 	struct saa7164_encoder_fh *fh = file->private_data;
315 	struct saa7164_port *port = fh->port;
316 	struct saa7164_dev *dev = port->dev;
317 
318 	if (0 != t->index)
319 		return -EINVAL;
320 
321 	strcpy(t->name, "tuner");
322 	t->type = V4L2_TUNER_ANALOG_TV;
323 	t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
324 
325 	dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
326 
327 	return 0;
328 }
329 
330 static int vidioc_s_tuner(struct file *file, void *priv,
331 	const struct v4l2_tuner *t)
332 {
333 	/* Update the A/V core */
334 	return 0;
335 }
336 
337 static int vidioc_g_frequency(struct file *file, void *priv,
338 	struct v4l2_frequency *f)
339 {
340 	struct saa7164_encoder_fh *fh = file->private_data;
341 	struct saa7164_port *port = fh->port;
342 
343 	f->type = V4L2_TUNER_ANALOG_TV;
344 	f->frequency = port->freq;
345 
346 	return 0;
347 }
348 
349 static int vidioc_s_frequency(struct file *file, void *priv,
350 	const struct v4l2_frequency *f)
351 {
352 	struct saa7164_encoder_fh *fh = file->private_data;
353 	struct saa7164_port *port = fh->port;
354 	struct saa7164_dev *dev = port->dev;
355 	struct saa7164_port *tsport;
356 	struct dvb_frontend *fe;
357 
358 	/* TODO: Pull this for the std */
359 	struct analog_parameters params = {
360 		.mode      = V4L2_TUNER_ANALOG_TV,
361 		.audmode   = V4L2_TUNER_MODE_STEREO,
362 		.std       = port->encodernorm.id,
363 		.frequency = f->frequency
364 	};
365 
366 	/* Stop the encoder */
367 	dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
368 		f->frequency, f->tuner);
369 
370 	if (f->tuner != 0)
371 		return -EINVAL;
372 
373 	if (f->type != V4L2_TUNER_ANALOG_TV)
374 		return -EINVAL;
375 
376 	port->freq = f->frequency;
377 
378 	/* Update the hardware */
379 	if (port->nr == SAA7164_PORT_ENC1)
380 		tsport = &dev->ports[SAA7164_PORT_TS1];
381 	else
382 	if (port->nr == SAA7164_PORT_ENC2)
383 		tsport = &dev->ports[SAA7164_PORT_TS2];
384 	else
385 		BUG();
386 
387 	fe = tsport->dvb.frontend;
388 
389 	if (fe && fe->ops.tuner_ops.set_analog_params)
390 		fe->ops.tuner_ops.set_analog_params(fe, &params);
391 	else
392 		printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
393 
394 	saa7164_encoder_initialize(port);
395 
396 	return 0;
397 }
398 
399 static int vidioc_g_ctrl(struct file *file, void *priv,
400 	struct v4l2_control *ctl)
401 {
402 	struct saa7164_encoder_fh *fh = file->private_data;
403 	struct saa7164_port *port = fh->port;
404 	struct saa7164_dev *dev = port->dev;
405 
406 	dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
407 		ctl->id, ctl->value);
408 
409 	switch (ctl->id) {
410 	case V4L2_CID_BRIGHTNESS:
411 		ctl->value = port->ctl_brightness;
412 		break;
413 	case V4L2_CID_CONTRAST:
414 		ctl->value = port->ctl_contrast;
415 		break;
416 	case V4L2_CID_SATURATION:
417 		ctl->value = port->ctl_saturation;
418 		break;
419 	case V4L2_CID_HUE:
420 		ctl->value = port->ctl_hue;
421 		break;
422 	case V4L2_CID_SHARPNESS:
423 		ctl->value = port->ctl_sharpness;
424 		break;
425 	case V4L2_CID_AUDIO_VOLUME:
426 		ctl->value = port->ctl_volume;
427 		break;
428 	default:
429 		return -EINVAL;
430 	}
431 
432 	return 0;
433 }
434 
435 static int vidioc_s_ctrl(struct file *file, void *priv,
436 	struct v4l2_control *ctl)
437 {
438 	struct saa7164_encoder_fh *fh = file->private_data;
439 	struct saa7164_port *port = fh->port;
440 	struct saa7164_dev *dev = port->dev;
441 	int ret = 0;
442 
443 	dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
444 		ctl->id, ctl->value);
445 
446 	switch (ctl->id) {
447 	case V4L2_CID_BRIGHTNESS:
448 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
449 			port->ctl_brightness = ctl->value;
450 			saa7164_api_set_usercontrol(port,
451 				PU_BRIGHTNESS_CONTROL);
452 		} else
453 			ret = -EINVAL;
454 		break;
455 	case V4L2_CID_CONTRAST:
456 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
457 			port->ctl_contrast = ctl->value;
458 			saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
459 		} else
460 			ret = -EINVAL;
461 		break;
462 	case V4L2_CID_SATURATION:
463 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
464 			port->ctl_saturation = ctl->value;
465 			saa7164_api_set_usercontrol(port,
466 				PU_SATURATION_CONTROL);
467 		} else
468 			ret = -EINVAL;
469 		break;
470 	case V4L2_CID_HUE:
471 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
472 			port->ctl_hue = ctl->value;
473 			saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
474 		} else
475 			ret = -EINVAL;
476 		break;
477 	case V4L2_CID_SHARPNESS:
478 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
479 			port->ctl_sharpness = ctl->value;
480 			saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
481 		} else
482 			ret = -EINVAL;
483 		break;
484 	case V4L2_CID_AUDIO_VOLUME:
485 		if ((ctl->value >= -83) && (ctl->value <= 24)) {
486 			port->ctl_volume = ctl->value;
487 			saa7164_api_set_audio_volume(port, port->ctl_volume);
488 		} else
489 			ret = -EINVAL;
490 		break;
491 	default:
492 		ret = -EINVAL;
493 	}
494 
495 	return ret;
496 }
497 
498 static int saa7164_get_ctrl(struct saa7164_port *port,
499 	struct v4l2_ext_control *ctrl)
500 {
501 	struct saa7164_encoder_params *params = &port->encoder_params;
502 
503 	switch (ctrl->id) {
504 	case V4L2_CID_MPEG_VIDEO_BITRATE:
505 		ctrl->value = params->bitrate;
506 		break;
507 	case V4L2_CID_MPEG_STREAM_TYPE:
508 		ctrl->value = params->stream_type;
509 		break;
510 	case V4L2_CID_MPEG_AUDIO_MUTE:
511 		ctrl->value = params->ctl_mute;
512 		break;
513 	case V4L2_CID_MPEG_VIDEO_ASPECT:
514 		ctrl->value = params->ctl_aspect;
515 		break;
516 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
517 		ctrl->value = params->bitrate_mode;
518 		break;
519 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
520 		ctrl->value = params->refdist;
521 		break;
522 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
523 		ctrl->value = params->bitrate_peak;
524 		break;
525 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
526 		ctrl->value = params->gop_size;
527 		break;
528 	default:
529 		return -EINVAL;
530 	}
531 	return 0;
532 }
533 
534 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
535 	struct v4l2_ext_controls *ctrls)
536 {
537 	struct saa7164_encoder_fh *fh = file->private_data;
538 	struct saa7164_port *port = fh->port;
539 	int i, err = 0;
540 
541 	if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
542 		for (i = 0; i < ctrls->count; i++) {
543 			struct v4l2_ext_control *ctrl = ctrls->controls + i;
544 
545 			err = saa7164_get_ctrl(port, ctrl);
546 			if (err) {
547 				ctrls->error_idx = i;
548 				break;
549 			}
550 		}
551 		return err;
552 
553 	}
554 
555 	return -EINVAL;
556 }
557 
558 static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
559 {
560 	int ret = -EINVAL;
561 
562 	switch (ctrl->id) {
563 	case V4L2_CID_MPEG_VIDEO_BITRATE:
564 		if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
565 			(ctrl->value <= ENCODER_MAX_BITRATE))
566 			ret = 0;
567 		break;
568 	case V4L2_CID_MPEG_STREAM_TYPE:
569 		if ((ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) ||
570 			(ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS))
571 			ret = 0;
572 		break;
573 	case V4L2_CID_MPEG_AUDIO_MUTE:
574 		if ((ctrl->value >= 0) &&
575 			(ctrl->value <= 1))
576 			ret = 0;
577 		break;
578 	case V4L2_CID_MPEG_VIDEO_ASPECT:
579 		if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
580 			(ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
581 			ret = 0;
582 		break;
583 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
584 		if ((ctrl->value >= 0) &&
585 			(ctrl->value <= 255))
586 			ret = 0;
587 		break;
588 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
589 		if ((ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ||
590 			(ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR))
591 			ret = 0;
592 		break;
593 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
594 		if ((ctrl->value >= 1) &&
595 			(ctrl->value <= 3))
596 			ret = 0;
597 		break;
598 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
599 		if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
600 			(ctrl->value <= ENCODER_MAX_BITRATE))
601 			ret = 0;
602 		break;
603 	default:
604 		ret = -EINVAL;
605 	}
606 
607 	return ret;
608 }
609 
610 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
611 	struct v4l2_ext_controls *ctrls)
612 {
613 	int i, err = 0;
614 
615 	if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
616 		for (i = 0; i < ctrls->count; i++) {
617 			struct v4l2_ext_control *ctrl = ctrls->controls + i;
618 
619 			err = saa7164_try_ctrl(ctrl, 0);
620 			if (err) {
621 				ctrls->error_idx = i;
622 				break;
623 			}
624 		}
625 		return err;
626 	}
627 
628 	return -EINVAL;
629 }
630 
631 static int saa7164_set_ctrl(struct saa7164_port *port,
632 	struct v4l2_ext_control *ctrl)
633 {
634 	struct saa7164_encoder_params *params = &port->encoder_params;
635 	int ret = 0;
636 
637 	switch (ctrl->id) {
638 	case V4L2_CID_MPEG_VIDEO_BITRATE:
639 		params->bitrate = ctrl->value;
640 		break;
641 	case V4L2_CID_MPEG_STREAM_TYPE:
642 		params->stream_type = ctrl->value;
643 		break;
644 	case V4L2_CID_MPEG_AUDIO_MUTE:
645 		params->ctl_mute = ctrl->value;
646 		ret = saa7164_api_audio_mute(port, params->ctl_mute);
647 		if (ret != SAA_OK) {
648 			printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
649 				ret);
650 			ret = -EIO;
651 		}
652 		break;
653 	case V4L2_CID_MPEG_VIDEO_ASPECT:
654 		params->ctl_aspect = ctrl->value;
655 		ret = saa7164_api_set_aspect_ratio(port);
656 		if (ret != SAA_OK) {
657 			printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
658 				ret);
659 			ret = -EIO;
660 		}
661 		break;
662 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
663 		params->bitrate_mode = ctrl->value;
664 		break;
665 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
666 		params->refdist = ctrl->value;
667 		break;
668 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
669 		params->bitrate_peak = ctrl->value;
670 		break;
671 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
672 		params->gop_size = ctrl->value;
673 		break;
674 	default:
675 		return -EINVAL;
676 	}
677 
678 	/* TODO: Update the hardware */
679 
680 	return ret;
681 }
682 
683 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
684 	struct v4l2_ext_controls *ctrls)
685 {
686 	struct saa7164_encoder_fh *fh = file->private_data;
687 	struct saa7164_port *port = fh->port;
688 	int i, err = 0;
689 
690 	if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
691 		for (i = 0; i < ctrls->count; i++) {
692 			struct v4l2_ext_control *ctrl = ctrls->controls + i;
693 
694 			err = saa7164_try_ctrl(ctrl, 0);
695 			if (err) {
696 				ctrls->error_idx = i;
697 				break;
698 			}
699 			err = saa7164_set_ctrl(port, ctrl);
700 			if (err) {
701 				ctrls->error_idx = i;
702 				break;
703 			}
704 		}
705 		return err;
706 
707 	}
708 
709 	return -EINVAL;
710 }
711 
712 static int vidioc_querycap(struct file *file, void  *priv,
713 	struct v4l2_capability *cap)
714 {
715 	struct saa7164_encoder_fh *fh = file->private_data;
716 	struct saa7164_port *port = fh->port;
717 	struct saa7164_dev *dev = port->dev;
718 
719 	strcpy(cap->driver, dev->name);
720 	strlcpy(cap->card, saa7164_boards[dev->board].name,
721 		sizeof(cap->card));
722 	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
723 
724 	cap->device_caps =
725 		V4L2_CAP_VIDEO_CAPTURE |
726 		V4L2_CAP_READWRITE |
727 		V4L2_CAP_TUNER;
728 
729 	cap->capabilities = cap->device_caps |
730 		V4L2_CAP_VBI_CAPTURE |
731 		V4L2_CAP_DEVICE_CAPS;
732 
733 	return 0;
734 }
735 
736 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
737 	struct v4l2_fmtdesc *f)
738 {
739 	if (f->index != 0)
740 		return -EINVAL;
741 
742 	strlcpy(f->description, "MPEG", sizeof(f->description));
743 	f->pixelformat = V4L2_PIX_FMT_MPEG;
744 
745 	return 0;
746 }
747 
748 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
749 				struct v4l2_format *f)
750 {
751 	struct saa7164_encoder_fh *fh = file->private_data;
752 	struct saa7164_port *port = fh->port;
753 	struct saa7164_dev *dev = port->dev;
754 
755 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
756 	f->fmt.pix.bytesperline = 0;
757 	f->fmt.pix.sizeimage    =
758 		port->ts_packet_size * port->ts_packet_count;
759 	f->fmt.pix.colorspace   = 0;
760 	f->fmt.pix.width        = port->width;
761 	f->fmt.pix.height       = port->height;
762 
763 	dprintk(DBGLVL_ENC, "VIDIOC_G_FMT: w: %d, h: %d\n",
764 		port->width, port->height);
765 
766 	return 0;
767 }
768 
769 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
770 				struct v4l2_format *f)
771 {
772 	struct saa7164_encoder_fh *fh = file->private_data;
773 	struct saa7164_port *port = fh->port;
774 	struct saa7164_dev *dev = port->dev;
775 
776 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
777 	f->fmt.pix.bytesperline = 0;
778 	f->fmt.pix.sizeimage    =
779 		port->ts_packet_size * port->ts_packet_count;
780 	f->fmt.pix.colorspace   = 0;
781 	dprintk(DBGLVL_ENC, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
782 		port->width, port->height);
783 	return 0;
784 }
785 
786 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
787 				struct v4l2_format *f)
788 {
789 	struct saa7164_encoder_fh *fh = file->private_data;
790 	struct saa7164_port *port = fh->port;
791 	struct saa7164_dev *dev = port->dev;
792 
793 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
794 	f->fmt.pix.bytesperline = 0;
795 	f->fmt.pix.sizeimage    =
796 		port->ts_packet_size * port->ts_packet_count;
797 	f->fmt.pix.colorspace   = 0;
798 
799 	dprintk(DBGLVL_ENC, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
800 		f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
801 
802 	return 0;
803 }
804 
805 static int fill_queryctrl(struct saa7164_encoder_params *params,
806 	struct v4l2_queryctrl *c)
807 {
808 	switch (c->id) {
809 	case V4L2_CID_BRIGHTNESS:
810 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
811 	case V4L2_CID_CONTRAST:
812 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
813 	case V4L2_CID_SATURATION:
814 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
815 	case V4L2_CID_HUE:
816 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
817 	case V4L2_CID_SHARPNESS:
818 		return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
819 	case V4L2_CID_MPEG_AUDIO_MUTE:
820 		return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
821 	case V4L2_CID_AUDIO_VOLUME:
822 		return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
823 	case V4L2_CID_MPEG_VIDEO_BITRATE:
824 		return v4l2_ctrl_query_fill(c,
825 			ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
826 			100000, ENCODER_DEF_BITRATE);
827 	case V4L2_CID_MPEG_STREAM_TYPE:
828 		return v4l2_ctrl_query_fill(c,
829 			V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
830 			V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
831 			1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
832 	case V4L2_CID_MPEG_VIDEO_ASPECT:
833 		return v4l2_ctrl_query_fill(c,
834 			V4L2_MPEG_VIDEO_ASPECT_1x1,
835 			V4L2_MPEG_VIDEO_ASPECT_221x100,
836 			1, V4L2_MPEG_VIDEO_ASPECT_4x3);
837 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
838 		return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
839 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
840 		return v4l2_ctrl_query_fill(c,
841 			V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
842 			V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
843 			1, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
844 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
845 		return v4l2_ctrl_query_fill(c,
846 			1, 3, 1, 1);
847 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
848 		return v4l2_ctrl_query_fill(c,
849 			ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
850 			100000, ENCODER_DEF_BITRATE);
851 	default:
852 		return -EINVAL;
853 	}
854 }
855 
856 static int vidioc_queryctrl(struct file *file, void *priv,
857 	struct v4l2_queryctrl *c)
858 {
859 	struct saa7164_encoder_fh *fh = priv;
860 	struct saa7164_port *port = fh->port;
861 	int i, next;
862 	u32 id = c->id;
863 
864 	memset(c, 0, sizeof(*c));
865 
866 	next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
867 	c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
868 
869 	for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
870 		if (next) {
871 			if (c->id < saa7164_v4l2_ctrls[i])
872 				c->id = saa7164_v4l2_ctrls[i];
873 			else
874 				continue;
875 		}
876 
877 		if (c->id == saa7164_v4l2_ctrls[i])
878 			return fill_queryctrl(&port->encoder_params, c);
879 
880 		if (c->id < saa7164_v4l2_ctrls[i])
881 			break;
882 	}
883 
884 	return -EINVAL;
885 }
886 
887 static int saa7164_encoder_stop_port(struct saa7164_port *port)
888 {
889 	struct saa7164_dev *dev = port->dev;
890 	int ret;
891 
892 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
893 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
894 		printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
895 			__func__, ret);
896 		ret = -EIO;
897 	} else {
898 		dprintk(DBGLVL_ENC, "%s()    Stopped\n", __func__);
899 		ret = 0;
900 	}
901 
902 	return ret;
903 }
904 
905 static int saa7164_encoder_acquire_port(struct saa7164_port *port)
906 {
907 	struct saa7164_dev *dev = port->dev;
908 	int ret;
909 
910 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
911 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
912 		printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
913 			__func__, ret);
914 		ret = -EIO;
915 	} else {
916 		dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
917 		ret = 0;
918 	}
919 
920 	return ret;
921 }
922 
923 static int saa7164_encoder_pause_port(struct saa7164_port *port)
924 {
925 	struct saa7164_dev *dev = port->dev;
926 	int ret;
927 
928 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
929 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
930 		printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
931 			__func__, ret);
932 		ret = -EIO;
933 	} else {
934 		dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);
935 		ret = 0;
936 	}
937 
938 	return ret;
939 }
940 
941 /* Firmware is very windows centric, meaning you have to transition
942  * the part through AVStream / KS Windows stages, forwards or backwards.
943  * States are: stopped, acquired (h/w), paused, started.
944  * We have to leave here will all of the soft buffers on the free list,
945  * else the cfg_post() func won't have soft buffers to correctly configure.
946  */
947 static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
948 {
949 	struct saa7164_dev *dev = port->dev;
950 	struct saa7164_buffer *buf;
951 	struct saa7164_user_buffer *ubuf;
952 	struct list_head *c, *n;
953 	int ret;
954 
955 	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
956 
957 	ret = saa7164_encoder_pause_port(port);
958 	ret = saa7164_encoder_acquire_port(port);
959 	ret = saa7164_encoder_stop_port(port);
960 
961 	dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
962 		port->nr);
963 
964 	/* Reset the state of any allocated buffer resources */
965 	mutex_lock(&port->dmaqueue_lock);
966 
967 	/* Reset the hard and soft buffer state */
968 	list_for_each_safe(c, n, &port->dmaqueue.list) {
969 		buf = list_entry(c, struct saa7164_buffer, list);
970 		buf->flags = SAA7164_BUFFER_FREE;
971 		buf->pos = 0;
972 	}
973 
974 	list_for_each_safe(c, n, &port->list_buf_used.list) {
975 		ubuf = list_entry(c, struct saa7164_user_buffer, list);
976 		ubuf->pos = 0;
977 		list_move_tail(&ubuf->list, &port->list_buf_free.list);
978 	}
979 
980 	mutex_unlock(&port->dmaqueue_lock);
981 
982 	/* Free any allocated resources */
983 	saa7164_encoder_buffers_dealloc(port);
984 
985 	dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
986 
987 	return ret;
988 }
989 
990 static int saa7164_encoder_start_streaming(struct saa7164_port *port)
991 {
992 	struct saa7164_dev *dev = port->dev;
993 	int result, ret = 0;
994 
995 	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
996 
997 	port->done_first_interrupt = 0;
998 
999 	/* allocate all of the PCIe DMA buffer resources on the fly,
1000 	 * allowing switching between TS and PS payloads without
1001 	 * requiring a complete driver reload.
1002 	 */
1003 	saa7164_encoder_buffers_alloc(port);
1004 
1005 	/* Configure the encoder with any cache values */
1006 	saa7164_api_set_encoder(port);
1007 	saa7164_api_get_encoder(port);
1008 
1009 	/* Place the empty buffers on the hardware */
1010 	saa7164_buffer_cfg_port(port);
1011 
1012 	/* Acquire the hardware */
1013 	result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
1014 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1015 		printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
1016 			__func__, result);
1017 
1018 		/* Stop the hardware, regardless */
1019 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1020 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1021 			printk(KERN_ERR "%s() acquire/forced stop transition "
1022 				"failed, res = 0x%x\n", __func__, result);
1023 		}
1024 		ret = -EIO;
1025 		goto out;
1026 	} else
1027 		dprintk(DBGLVL_ENC, "%s()   Acquired\n", __func__);
1028 
1029 	/* Pause the hardware */
1030 	result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
1031 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1032 		printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
1033 				__func__, result);
1034 
1035 		/* Stop the hardware, regardless */
1036 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1037 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1038 			printk(KERN_ERR "%s() pause/forced stop transition "
1039 				"failed, res = 0x%x\n", __func__, result);
1040 		}
1041 
1042 		ret = -EIO;
1043 		goto out;
1044 	} else
1045 		dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);
1046 
1047 	/* Start the hardware */
1048 	result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
1049 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1050 		printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
1051 				__func__, result);
1052 
1053 		/* Stop the hardware, regardless */
1054 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1055 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1056 			printk(KERN_ERR "%s() run/forced stop transition "
1057 				"failed, res = 0x%x\n", __func__, result);
1058 		}
1059 
1060 		ret = -EIO;
1061 	} else
1062 		dprintk(DBGLVL_ENC, "%s()   Running\n", __func__);
1063 
1064 out:
1065 	return ret;
1066 }
1067 
1068 static int fops_open(struct file *file)
1069 {
1070 	struct saa7164_dev *dev;
1071 	struct saa7164_port *port;
1072 	struct saa7164_encoder_fh *fh;
1073 
1074 	port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
1075 	if (!port)
1076 		return -ENODEV;
1077 
1078 	dev = port->dev;
1079 
1080 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1081 
1082 	/* allocate + initialize per filehandle data */
1083 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1084 	if (NULL == fh)
1085 		return -ENOMEM;
1086 
1087 	file->private_data = fh;
1088 	fh->port = port;
1089 
1090 	return 0;
1091 }
1092 
1093 static int fops_release(struct file *file)
1094 {
1095 	struct saa7164_encoder_fh *fh = file->private_data;
1096 	struct saa7164_port *port = fh->port;
1097 	struct saa7164_dev *dev = port->dev;
1098 
1099 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1100 
1101 	/* Shut device down on last close */
1102 	if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1103 		if (atomic_dec_return(&port->v4l_reader_count) == 0) {
1104 			/* stop mpeg capture then cancel buffers */
1105 			saa7164_encoder_stop_streaming(port);
1106 		}
1107 	}
1108 
1109 	file->private_data = NULL;
1110 	kfree(fh);
1111 
1112 	return 0;
1113 }
1114 
1115 static struct
1116 saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
1117 {
1118 	struct saa7164_user_buffer *ubuf = NULL;
1119 	struct saa7164_dev *dev = port->dev;
1120 	u32 crc;
1121 
1122 	mutex_lock(&port->dmaqueue_lock);
1123 	if (!list_empty(&port->list_buf_used.list)) {
1124 		ubuf = list_first_entry(&port->list_buf_used.list,
1125 			struct saa7164_user_buffer, list);
1126 
1127 		if (crc_checking) {
1128 			crc = crc32(0, ubuf->data, ubuf->actual_size);
1129 			if (crc != ubuf->crc) {
1130 				printk(KERN_ERR
1131 		"%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
1132 					__func__,
1133 					ubuf, ubuf->crc, crc);
1134 			}
1135 		}
1136 
1137 	}
1138 	mutex_unlock(&port->dmaqueue_lock);
1139 
1140 	dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
1141 
1142 	return ubuf;
1143 }
1144 
1145 static ssize_t fops_read(struct file *file, char __user *buffer,
1146 	size_t count, loff_t *pos)
1147 {
1148 	struct saa7164_encoder_fh *fh = file->private_data;
1149 	struct saa7164_port *port = fh->port;
1150 	struct saa7164_user_buffer *ubuf = NULL;
1151 	struct saa7164_dev *dev = port->dev;
1152 	int ret = 0;
1153 	int rem, cnt;
1154 	u8 *p;
1155 
1156 	port->last_read_msecs_diff = port->last_read_msecs;
1157 	port->last_read_msecs = jiffies_to_msecs(jiffies);
1158 	port->last_read_msecs_diff = port->last_read_msecs -
1159 		port->last_read_msecs_diff;
1160 
1161 	saa7164_histogram_update(&port->read_interval,
1162 		port->last_read_msecs_diff);
1163 
1164 	if (*pos) {
1165 		printk(KERN_ERR "%s() ESPIPE\n", __func__);
1166 		return -ESPIPE;
1167 	}
1168 
1169 	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1170 		if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1171 
1172 			if (saa7164_encoder_initialize(port) < 0) {
1173 				printk(KERN_ERR "%s() EINVAL\n", __func__);
1174 				return -EINVAL;
1175 			}
1176 
1177 			saa7164_encoder_start_streaming(port);
1178 			msleep(200);
1179 		}
1180 	}
1181 
1182 	/* blocking wait for buffer */
1183 	if ((file->f_flags & O_NONBLOCK) == 0) {
1184 		if (wait_event_interruptible(port->wait_read,
1185 			saa7164_enc_next_buf(port))) {
1186 				printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
1187 				return -ERESTARTSYS;
1188 		}
1189 	}
1190 
1191 	/* Pull the first buffer from the used list */
1192 	ubuf = saa7164_enc_next_buf(port);
1193 
1194 	while ((count > 0) && ubuf) {
1195 
1196 		/* set remaining bytes to copy */
1197 		rem = ubuf->actual_size - ubuf->pos;
1198 		cnt = rem > count ? count : rem;
1199 
1200 		p = ubuf->data + ubuf->pos;
1201 
1202 		dprintk(DBGLVL_ENC,
1203 			"%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1204 			__func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1205 
1206 		if (copy_to_user(buffer, p, cnt)) {
1207 			printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1208 			if (!ret) {
1209 				printk(KERN_ERR "%s() EFAULT\n", __func__);
1210 				ret = -EFAULT;
1211 			}
1212 			goto err;
1213 		}
1214 
1215 		ubuf->pos += cnt;
1216 		count -= cnt;
1217 		buffer += cnt;
1218 		ret += cnt;
1219 
1220 		if (ubuf->pos > ubuf->actual_size)
1221 			printk(KERN_ERR "read() pos > actual, huh?\n");
1222 
1223 		if (ubuf->pos == ubuf->actual_size) {
1224 
1225 			/* finished with current buffer, take next buffer */
1226 
1227 			/* Requeue the buffer on the free list */
1228 			ubuf->pos = 0;
1229 
1230 			mutex_lock(&port->dmaqueue_lock);
1231 			list_move_tail(&ubuf->list, &port->list_buf_free.list);
1232 			mutex_unlock(&port->dmaqueue_lock);
1233 
1234 			/* Dequeue next */
1235 			if ((file->f_flags & O_NONBLOCK) == 0) {
1236 				if (wait_event_interruptible(port->wait_read,
1237 					saa7164_enc_next_buf(port))) {
1238 						break;
1239 				}
1240 			}
1241 			ubuf = saa7164_enc_next_buf(port);
1242 		}
1243 	}
1244 err:
1245 	if (!ret && !ubuf)
1246 		ret = -EAGAIN;
1247 
1248 	return ret;
1249 }
1250 
1251 static unsigned int fops_poll(struct file *file, poll_table *wait)
1252 {
1253 	struct saa7164_encoder_fh *fh =
1254 		(struct saa7164_encoder_fh *)file->private_data;
1255 	struct saa7164_port *port = fh->port;
1256 	unsigned int mask = 0;
1257 
1258 	port->last_poll_msecs_diff = port->last_poll_msecs;
1259 	port->last_poll_msecs = jiffies_to_msecs(jiffies);
1260 	port->last_poll_msecs_diff = port->last_poll_msecs -
1261 		port->last_poll_msecs_diff;
1262 
1263 	saa7164_histogram_update(&port->poll_interval,
1264 		port->last_poll_msecs_diff);
1265 
1266 	if (!video_is_registered(port->v4l_device))
1267 		return -EIO;
1268 
1269 	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1270 		if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1271 			if (saa7164_encoder_initialize(port) < 0)
1272 				return -EINVAL;
1273 			saa7164_encoder_start_streaming(port);
1274 			msleep(200);
1275 		}
1276 	}
1277 
1278 	/* blocking wait for buffer */
1279 	if ((file->f_flags & O_NONBLOCK) == 0) {
1280 		if (wait_event_interruptible(port->wait_read,
1281 			saa7164_enc_next_buf(port))) {
1282 				return -ERESTARTSYS;
1283 		}
1284 	}
1285 
1286 	/* Pull the first buffer from the used list */
1287 	if (!list_empty(&port->list_buf_used.list))
1288 		mask |= POLLIN | POLLRDNORM;
1289 
1290 	return mask;
1291 }
1292 
1293 static const struct v4l2_file_operations mpeg_fops = {
1294 	.owner		= THIS_MODULE,
1295 	.open		= fops_open,
1296 	.release	= fops_release,
1297 	.read		= fops_read,
1298 	.poll		= fops_poll,
1299 	.unlocked_ioctl	= video_ioctl2,
1300 };
1301 
1302 static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1303 	.vidioc_s_std		 = vidioc_s_std,
1304 	.vidioc_g_std		 = vidioc_g_std,
1305 	.vidioc_enum_input	 = vidioc_enum_input,
1306 	.vidioc_g_input		 = vidioc_g_input,
1307 	.vidioc_s_input		 = vidioc_s_input,
1308 	.vidioc_g_tuner		 = vidioc_g_tuner,
1309 	.vidioc_s_tuner		 = vidioc_s_tuner,
1310 	.vidioc_g_frequency	 = vidioc_g_frequency,
1311 	.vidioc_s_frequency	 = vidioc_s_frequency,
1312 	.vidioc_s_ctrl		 = vidioc_s_ctrl,
1313 	.vidioc_g_ctrl		 = vidioc_g_ctrl,
1314 	.vidioc_querycap	 = vidioc_querycap,
1315 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1316 	.vidioc_g_fmt_vid_cap	 = vidioc_g_fmt_vid_cap,
1317 	.vidioc_try_fmt_vid_cap	 = vidioc_try_fmt_vid_cap,
1318 	.vidioc_s_fmt_vid_cap	 = vidioc_s_fmt_vid_cap,
1319 	.vidioc_g_ext_ctrls	 = vidioc_g_ext_ctrls,
1320 	.vidioc_s_ext_ctrls	 = vidioc_s_ext_ctrls,
1321 	.vidioc_try_ext_ctrls	 = vidioc_try_ext_ctrls,
1322 	.vidioc_queryctrl	 = vidioc_queryctrl,
1323 };
1324 
1325 static struct video_device saa7164_mpeg_template = {
1326 	.name          = "saa7164",
1327 	.fops          = &mpeg_fops,
1328 	.ioctl_ops     = &mpeg_ioctl_ops,
1329 	.minor         = -1,
1330 	.tvnorms       = SAA7164_NORMS,
1331 };
1332 
1333 static struct video_device *saa7164_encoder_alloc(
1334 	struct saa7164_port *port,
1335 	struct pci_dev *pci,
1336 	struct video_device *template,
1337 	char *type)
1338 {
1339 	struct video_device *vfd;
1340 	struct saa7164_dev *dev = port->dev;
1341 
1342 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1343 
1344 	vfd = video_device_alloc();
1345 	if (NULL == vfd)
1346 		return NULL;
1347 
1348 	*vfd = *template;
1349 	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1350 		type, saa7164_boards[dev->board].name);
1351 
1352 	vfd->v4l2_dev  = &dev->v4l2_dev;
1353 	vfd->release = video_device_release;
1354 	return vfd;
1355 }
1356 
1357 int saa7164_encoder_register(struct saa7164_port *port)
1358 {
1359 	struct saa7164_dev *dev = port->dev;
1360 	int result = -ENODEV;
1361 
1362 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1363 
1364 	if (port->type != SAA7164_MPEG_ENCODER)
1365 		BUG();
1366 
1367 	/* Sanity check that the PCI configuration space is active */
1368 	if (port->hwcfg.BARLocation == 0) {
1369 		printk(KERN_ERR "%s() failed "
1370 		       "(errno = %d), NO PCI configuration\n",
1371 			__func__, result);
1372 		result = -ENOMEM;
1373 		goto failed;
1374 	}
1375 
1376 	/* Establish encoder defaults here */
1377 	/* Set default TV standard */
1378 	port->encodernorm = saa7164_tvnorms[0];
1379 	port->width = 720;
1380 	port->mux_input = 1; /* Composite */
1381 	port->video_format = EU_VIDEO_FORMAT_MPEG_2;
1382 	port->audio_format = 0;
1383 	port->video_resolution = 0;
1384 	port->ctl_brightness = 127;
1385 	port->ctl_contrast = 66;
1386 	port->ctl_hue = 128;
1387 	port->ctl_saturation = 62;
1388 	port->ctl_sharpness = 8;
1389 	port->encoder_params.bitrate = ENCODER_DEF_BITRATE;
1390 	port->encoder_params.bitrate_peak = ENCODER_DEF_BITRATE;
1391 	port->encoder_params.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
1392 	port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1393 	port->encoder_params.ctl_mute = 0;
1394 	port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
1395 	port->encoder_params.refdist = 1;
1396 	port->encoder_params.gop_size = SAA7164_ENCODER_DEFAULT_GOP_SIZE;
1397 	port->std = V4L2_STD_NTSC_M;
1398 
1399 	if (port->encodernorm.id & V4L2_STD_525_60)
1400 		port->height = 480;
1401 	else
1402 		port->height = 576;
1403 
1404 	/* Allocate and register the video device node */
1405 	port->v4l_device = saa7164_encoder_alloc(port,
1406 		dev->pci, &saa7164_mpeg_template, "mpeg");
1407 
1408 	if (!port->v4l_device) {
1409 		printk(KERN_INFO "%s: can't allocate mpeg device\n",
1410 			dev->name);
1411 		result = -ENOMEM;
1412 		goto failed;
1413 	}
1414 
1415 	video_set_drvdata(port->v4l_device, port);
1416 	result = video_register_device(port->v4l_device,
1417 		VFL_TYPE_GRABBER, -1);
1418 	if (result < 0) {
1419 		printk(KERN_INFO "%s: can't register mpeg device\n",
1420 			dev->name);
1421 		/* TODO: We're going to leak here if we don't dealloc
1422 		 The buffers above. The unreg function can't deal wit it.
1423 		*/
1424 		goto failed;
1425 	}
1426 
1427 	printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
1428 		dev->name, port->v4l_device->num);
1429 
1430 	/* Configure the hardware defaults */
1431 	saa7164_api_set_videomux(port);
1432 	saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
1433 	saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
1434 	saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
1435 	saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
1436 	saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
1437 	saa7164_api_audio_mute(port, 0);
1438 	saa7164_api_set_audio_volume(port, 20);
1439 	saa7164_api_set_aspect_ratio(port);
1440 
1441 	/* Disable audio standard detection, it's buggy */
1442 	saa7164_api_set_audio_detection(port, 0);
1443 
1444 	saa7164_api_set_encoder(port);
1445 	saa7164_api_get_encoder(port);
1446 
1447 	result = 0;
1448 failed:
1449 	return result;
1450 }
1451 
1452 void saa7164_encoder_unregister(struct saa7164_port *port)
1453 {
1454 	struct saa7164_dev *dev = port->dev;
1455 
1456 	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1457 
1458 	if (port->type != SAA7164_MPEG_ENCODER)
1459 		BUG();
1460 
1461 	if (port->v4l_device) {
1462 		if (port->v4l_device->minor != -1)
1463 			video_unregister_device(port->v4l_device);
1464 		else
1465 			video_device_release(port->v4l_device);
1466 
1467 		port->v4l_device = NULL;
1468 	}
1469 
1470 	dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
1471 }
1472 
1473