xref: /linux/drivers/media/pci/saa7164/saa7164-encoder.c (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2010 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->capabilities =
725 		V4L2_CAP_VIDEO_CAPTURE |
726 		V4L2_CAP_READWRITE     |
727 		0;
728 
729 	cap->capabilities |= V4L2_CAP_TUNER;
730 	cap->version = 0;
731 
732 	return 0;
733 }
734 
735 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
736 	struct v4l2_fmtdesc *f)
737 {
738 	if (f->index != 0)
739 		return -EINVAL;
740 
741 	strlcpy(f->description, "MPEG", sizeof(f->description));
742 	f->pixelformat = V4L2_PIX_FMT_MPEG;
743 
744 	return 0;
745 }
746 
747 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
748 				struct v4l2_format *f)
749 {
750 	struct saa7164_encoder_fh *fh = file->private_data;
751 	struct saa7164_port *port = fh->port;
752 	struct saa7164_dev *dev = port->dev;
753 
754 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
755 	f->fmt.pix.bytesperline = 0;
756 	f->fmt.pix.sizeimage    =
757 		port->ts_packet_size * port->ts_packet_count;
758 	f->fmt.pix.colorspace   = 0;
759 	f->fmt.pix.width        = port->width;
760 	f->fmt.pix.height       = port->height;
761 
762 	dprintk(DBGLVL_ENC, "VIDIOC_G_FMT: w: %d, h: %d\n",
763 		port->width, port->height);
764 
765 	return 0;
766 }
767 
768 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
769 				struct v4l2_format *f)
770 {
771 	struct saa7164_encoder_fh *fh = file->private_data;
772 	struct saa7164_port *port = fh->port;
773 	struct saa7164_dev *dev = port->dev;
774 
775 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
776 	f->fmt.pix.bytesperline = 0;
777 	f->fmt.pix.sizeimage    =
778 		port->ts_packet_size * port->ts_packet_count;
779 	f->fmt.pix.colorspace   = 0;
780 	dprintk(DBGLVL_ENC, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
781 		port->width, port->height);
782 	return 0;
783 }
784 
785 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
786 				struct v4l2_format *f)
787 {
788 	struct saa7164_encoder_fh *fh = file->private_data;
789 	struct saa7164_port *port = fh->port;
790 	struct saa7164_dev *dev = port->dev;
791 
792 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
793 	f->fmt.pix.bytesperline = 0;
794 	f->fmt.pix.sizeimage    =
795 		port->ts_packet_size * port->ts_packet_count;
796 	f->fmt.pix.colorspace   = 0;
797 
798 	dprintk(DBGLVL_ENC, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
799 		f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
800 
801 	return 0;
802 }
803 
804 static int fill_queryctrl(struct saa7164_encoder_params *params,
805 	struct v4l2_queryctrl *c)
806 {
807 	switch (c->id) {
808 	case V4L2_CID_BRIGHTNESS:
809 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
810 	case V4L2_CID_CONTRAST:
811 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
812 	case V4L2_CID_SATURATION:
813 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
814 	case V4L2_CID_HUE:
815 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
816 	case V4L2_CID_SHARPNESS:
817 		return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
818 	case V4L2_CID_MPEG_AUDIO_MUTE:
819 		return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
820 	case V4L2_CID_AUDIO_VOLUME:
821 		return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
822 	case V4L2_CID_MPEG_VIDEO_BITRATE:
823 		return v4l2_ctrl_query_fill(c,
824 			ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
825 			100000, ENCODER_DEF_BITRATE);
826 	case V4L2_CID_MPEG_STREAM_TYPE:
827 		return v4l2_ctrl_query_fill(c,
828 			V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
829 			V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
830 			1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
831 	case V4L2_CID_MPEG_VIDEO_ASPECT:
832 		return v4l2_ctrl_query_fill(c,
833 			V4L2_MPEG_VIDEO_ASPECT_1x1,
834 			V4L2_MPEG_VIDEO_ASPECT_221x100,
835 			1, V4L2_MPEG_VIDEO_ASPECT_4x3);
836 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
837 		return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
838 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
839 		return v4l2_ctrl_query_fill(c,
840 			V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
841 			V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
842 			1, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
843 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
844 		return v4l2_ctrl_query_fill(c,
845 			1, 3, 1, 1);
846 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
847 		return v4l2_ctrl_query_fill(c,
848 			ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
849 			100000, ENCODER_DEF_BITRATE);
850 	default:
851 		return -EINVAL;
852 	}
853 }
854 
855 static int vidioc_queryctrl(struct file *file, void *priv,
856 	struct v4l2_queryctrl *c)
857 {
858 	struct saa7164_encoder_fh *fh = priv;
859 	struct saa7164_port *port = fh->port;
860 	int i, next;
861 	u32 id = c->id;
862 
863 	memset(c, 0, sizeof(*c));
864 
865 	next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
866 	c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
867 
868 	for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
869 		if (next) {
870 			if (c->id < saa7164_v4l2_ctrls[i])
871 				c->id = saa7164_v4l2_ctrls[i];
872 			else
873 				continue;
874 		}
875 
876 		if (c->id == saa7164_v4l2_ctrls[i])
877 			return fill_queryctrl(&port->encoder_params, c);
878 
879 		if (c->id < saa7164_v4l2_ctrls[i])
880 			break;
881 	}
882 
883 	return -EINVAL;
884 }
885 
886 static int saa7164_encoder_stop_port(struct saa7164_port *port)
887 {
888 	struct saa7164_dev *dev = port->dev;
889 	int ret;
890 
891 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
892 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
893 		printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
894 			__func__, ret);
895 		ret = -EIO;
896 	} else {
897 		dprintk(DBGLVL_ENC, "%s()    Stopped\n", __func__);
898 		ret = 0;
899 	}
900 
901 	return ret;
902 }
903 
904 static int saa7164_encoder_acquire_port(struct saa7164_port *port)
905 {
906 	struct saa7164_dev *dev = port->dev;
907 	int ret;
908 
909 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
910 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
911 		printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
912 			__func__, ret);
913 		ret = -EIO;
914 	} else {
915 		dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
916 		ret = 0;
917 	}
918 
919 	return ret;
920 }
921 
922 static int saa7164_encoder_pause_port(struct saa7164_port *port)
923 {
924 	struct saa7164_dev *dev = port->dev;
925 	int ret;
926 
927 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
928 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
929 		printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
930 			__func__, ret);
931 		ret = -EIO;
932 	} else {
933 		dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);
934 		ret = 0;
935 	}
936 
937 	return ret;
938 }
939 
940 /* Firmware is very windows centric, meaning you have to transition
941  * the part through AVStream / KS Windows stages, forwards or backwards.
942  * States are: stopped, acquired (h/w), paused, started.
943  * We have to leave here will all of the soft buffers on the free list,
944  * else the cfg_post() func won't have soft buffers to correctly configure.
945  */
946 static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
947 {
948 	struct saa7164_dev *dev = port->dev;
949 	struct saa7164_buffer *buf;
950 	struct saa7164_user_buffer *ubuf;
951 	struct list_head *c, *n;
952 	int ret;
953 
954 	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
955 
956 	ret = saa7164_encoder_pause_port(port);
957 	ret = saa7164_encoder_acquire_port(port);
958 	ret = saa7164_encoder_stop_port(port);
959 
960 	dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
961 		port->nr);
962 
963 	/* Reset the state of any allocated buffer resources */
964 	mutex_lock(&port->dmaqueue_lock);
965 
966 	/* Reset the hard and soft buffer state */
967 	list_for_each_safe(c, n, &port->dmaqueue.list) {
968 		buf = list_entry(c, struct saa7164_buffer, list);
969 		buf->flags = SAA7164_BUFFER_FREE;
970 		buf->pos = 0;
971 	}
972 
973 	list_for_each_safe(c, n, &port->list_buf_used.list) {
974 		ubuf = list_entry(c, struct saa7164_user_buffer, list);
975 		ubuf->pos = 0;
976 		list_move_tail(&ubuf->list, &port->list_buf_free.list);
977 	}
978 
979 	mutex_unlock(&port->dmaqueue_lock);
980 
981 	/* Free any allocated resources */
982 	saa7164_encoder_buffers_dealloc(port);
983 
984 	dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
985 
986 	return ret;
987 }
988 
989 static int saa7164_encoder_start_streaming(struct saa7164_port *port)
990 {
991 	struct saa7164_dev *dev = port->dev;
992 	int result, ret = 0;
993 
994 	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
995 
996 	port->done_first_interrupt = 0;
997 
998 	/* allocate all of the PCIe DMA buffer resources on the fly,
999 	 * allowing switching between TS and PS payloads without
1000 	 * requiring a complete driver reload.
1001 	 */
1002 	saa7164_encoder_buffers_alloc(port);
1003 
1004 	/* Configure the encoder with any cache values */
1005 	saa7164_api_set_encoder(port);
1006 	saa7164_api_get_encoder(port);
1007 
1008 	/* Place the empty buffers on the hardware */
1009 	saa7164_buffer_cfg_port(port);
1010 
1011 	/* Acquire the hardware */
1012 	result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
1013 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1014 		printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
1015 			__func__, result);
1016 
1017 		/* Stop the hardware, regardless */
1018 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1019 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1020 			printk(KERN_ERR "%s() acquire/forced stop transition "
1021 				"failed, res = 0x%x\n", __func__, result);
1022 		}
1023 		ret = -EIO;
1024 		goto out;
1025 	} else
1026 		dprintk(DBGLVL_ENC, "%s()   Acquired\n", __func__);
1027 
1028 	/* Pause the hardware */
1029 	result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
1030 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1031 		printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
1032 				__func__, result);
1033 
1034 		/* Stop the hardware, regardless */
1035 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1036 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1037 			printk(KERN_ERR "%s() pause/forced stop transition "
1038 				"failed, res = 0x%x\n", __func__, result);
1039 		}
1040 
1041 		ret = -EIO;
1042 		goto out;
1043 	} else
1044 		dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);
1045 
1046 	/* Start the hardware */
1047 	result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
1048 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1049 		printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
1050 				__func__, result);
1051 
1052 		/* Stop the hardware, regardless */
1053 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1054 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1055 			printk(KERN_ERR "%s() run/forced stop transition "
1056 				"failed, res = 0x%x\n", __func__, result);
1057 		}
1058 
1059 		ret = -EIO;
1060 	} else
1061 		dprintk(DBGLVL_ENC, "%s()   Running\n", __func__);
1062 
1063 out:
1064 	return ret;
1065 }
1066 
1067 static int fops_open(struct file *file)
1068 {
1069 	struct saa7164_dev *dev;
1070 	struct saa7164_port *port;
1071 	struct saa7164_encoder_fh *fh;
1072 
1073 	port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
1074 	if (!port)
1075 		return -ENODEV;
1076 
1077 	dev = port->dev;
1078 
1079 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1080 
1081 	/* allocate + initialize per filehandle data */
1082 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1083 	if (NULL == fh)
1084 		return -ENOMEM;
1085 
1086 	file->private_data = fh;
1087 	fh->port = port;
1088 
1089 	return 0;
1090 }
1091 
1092 static int fops_release(struct file *file)
1093 {
1094 	struct saa7164_encoder_fh *fh = file->private_data;
1095 	struct saa7164_port *port = fh->port;
1096 	struct saa7164_dev *dev = port->dev;
1097 
1098 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1099 
1100 	/* Shut device down on last close */
1101 	if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1102 		if (atomic_dec_return(&port->v4l_reader_count) == 0) {
1103 			/* stop mpeg capture then cancel buffers */
1104 			saa7164_encoder_stop_streaming(port);
1105 		}
1106 	}
1107 
1108 	file->private_data = NULL;
1109 	kfree(fh);
1110 
1111 	return 0;
1112 }
1113 
1114 static struct
1115 saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
1116 {
1117 	struct saa7164_user_buffer *ubuf = NULL;
1118 	struct saa7164_dev *dev = port->dev;
1119 	u32 crc;
1120 
1121 	mutex_lock(&port->dmaqueue_lock);
1122 	if (!list_empty(&port->list_buf_used.list)) {
1123 		ubuf = list_first_entry(&port->list_buf_used.list,
1124 			struct saa7164_user_buffer, list);
1125 
1126 		if (crc_checking) {
1127 			crc = crc32(0, ubuf->data, ubuf->actual_size);
1128 			if (crc != ubuf->crc) {
1129 				printk(KERN_ERR
1130 		"%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
1131 					__func__,
1132 					ubuf, ubuf->crc, crc);
1133 			}
1134 		}
1135 
1136 	}
1137 	mutex_unlock(&port->dmaqueue_lock);
1138 
1139 	dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
1140 
1141 	return ubuf;
1142 }
1143 
1144 static ssize_t fops_read(struct file *file, char __user *buffer,
1145 	size_t count, loff_t *pos)
1146 {
1147 	struct saa7164_encoder_fh *fh = file->private_data;
1148 	struct saa7164_port *port = fh->port;
1149 	struct saa7164_user_buffer *ubuf = NULL;
1150 	struct saa7164_dev *dev = port->dev;
1151 	int ret = 0;
1152 	int rem, cnt;
1153 	u8 *p;
1154 
1155 	port->last_read_msecs_diff = port->last_read_msecs;
1156 	port->last_read_msecs = jiffies_to_msecs(jiffies);
1157 	port->last_read_msecs_diff = port->last_read_msecs -
1158 		port->last_read_msecs_diff;
1159 
1160 	saa7164_histogram_update(&port->read_interval,
1161 		port->last_read_msecs_diff);
1162 
1163 	if (*pos) {
1164 		printk(KERN_ERR "%s() ESPIPE\n", __func__);
1165 		return -ESPIPE;
1166 	}
1167 
1168 	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1169 		if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1170 
1171 			if (saa7164_encoder_initialize(port) < 0) {
1172 				printk(KERN_ERR "%s() EINVAL\n", __func__);
1173 				return -EINVAL;
1174 			}
1175 
1176 			saa7164_encoder_start_streaming(port);
1177 			msleep(200);
1178 		}
1179 	}
1180 
1181 	/* blocking wait for buffer */
1182 	if ((file->f_flags & O_NONBLOCK) == 0) {
1183 		if (wait_event_interruptible(port->wait_read,
1184 			saa7164_enc_next_buf(port))) {
1185 				printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
1186 				return -ERESTARTSYS;
1187 		}
1188 	}
1189 
1190 	/* Pull the first buffer from the used list */
1191 	ubuf = saa7164_enc_next_buf(port);
1192 
1193 	while ((count > 0) && ubuf) {
1194 
1195 		/* set remaining bytes to copy */
1196 		rem = ubuf->actual_size - ubuf->pos;
1197 		cnt = rem > count ? count : rem;
1198 
1199 		p = ubuf->data + ubuf->pos;
1200 
1201 		dprintk(DBGLVL_ENC,
1202 			"%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1203 			__func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1204 
1205 		if (copy_to_user(buffer, p, cnt)) {
1206 			printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1207 			if (!ret) {
1208 				printk(KERN_ERR "%s() EFAULT\n", __func__);
1209 				ret = -EFAULT;
1210 			}
1211 			goto err;
1212 		}
1213 
1214 		ubuf->pos += cnt;
1215 		count -= cnt;
1216 		buffer += cnt;
1217 		ret += cnt;
1218 
1219 		if (ubuf->pos > ubuf->actual_size)
1220 			printk(KERN_ERR "read() pos > actual, huh?\n");
1221 
1222 		if (ubuf->pos == ubuf->actual_size) {
1223 
1224 			/* finished with current buffer, take next buffer */
1225 
1226 			/* Requeue the buffer on the free list */
1227 			ubuf->pos = 0;
1228 
1229 			mutex_lock(&port->dmaqueue_lock);
1230 			list_move_tail(&ubuf->list, &port->list_buf_free.list);
1231 			mutex_unlock(&port->dmaqueue_lock);
1232 
1233 			/* Dequeue next */
1234 			if ((file->f_flags & O_NONBLOCK) == 0) {
1235 				if (wait_event_interruptible(port->wait_read,
1236 					saa7164_enc_next_buf(port))) {
1237 						break;
1238 				}
1239 			}
1240 			ubuf = saa7164_enc_next_buf(port);
1241 		}
1242 	}
1243 err:
1244 	if (!ret && !ubuf)
1245 		ret = -EAGAIN;
1246 
1247 	return ret;
1248 }
1249 
1250 static unsigned int fops_poll(struct file *file, poll_table *wait)
1251 {
1252 	struct saa7164_encoder_fh *fh =
1253 		(struct saa7164_encoder_fh *)file->private_data;
1254 	struct saa7164_port *port = fh->port;
1255 	unsigned int mask = 0;
1256 
1257 	port->last_poll_msecs_diff = port->last_poll_msecs;
1258 	port->last_poll_msecs = jiffies_to_msecs(jiffies);
1259 	port->last_poll_msecs_diff = port->last_poll_msecs -
1260 		port->last_poll_msecs_diff;
1261 
1262 	saa7164_histogram_update(&port->poll_interval,
1263 		port->last_poll_msecs_diff);
1264 
1265 	if (!video_is_registered(port->v4l_device))
1266 		return -EIO;
1267 
1268 	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1269 		if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1270 			if (saa7164_encoder_initialize(port) < 0)
1271 				return -EINVAL;
1272 			saa7164_encoder_start_streaming(port);
1273 			msleep(200);
1274 		}
1275 	}
1276 
1277 	/* blocking wait for buffer */
1278 	if ((file->f_flags & O_NONBLOCK) == 0) {
1279 		if (wait_event_interruptible(port->wait_read,
1280 			saa7164_enc_next_buf(port))) {
1281 				return -ERESTARTSYS;
1282 		}
1283 	}
1284 
1285 	/* Pull the first buffer from the used list */
1286 	if (!list_empty(&port->list_buf_used.list))
1287 		mask |= POLLIN | POLLRDNORM;
1288 
1289 	return mask;
1290 }
1291 
1292 static const struct v4l2_file_operations mpeg_fops = {
1293 	.owner		= THIS_MODULE,
1294 	.open		= fops_open,
1295 	.release	= fops_release,
1296 	.read		= fops_read,
1297 	.poll		= fops_poll,
1298 	.unlocked_ioctl	= video_ioctl2,
1299 };
1300 
1301 static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1302 	.vidioc_s_std		 = vidioc_s_std,
1303 	.vidioc_g_std		 = vidioc_g_std,
1304 	.vidioc_enum_input	 = vidioc_enum_input,
1305 	.vidioc_g_input		 = vidioc_g_input,
1306 	.vidioc_s_input		 = vidioc_s_input,
1307 	.vidioc_g_tuner		 = vidioc_g_tuner,
1308 	.vidioc_s_tuner		 = vidioc_s_tuner,
1309 	.vidioc_g_frequency	 = vidioc_g_frequency,
1310 	.vidioc_s_frequency	 = vidioc_s_frequency,
1311 	.vidioc_s_ctrl		 = vidioc_s_ctrl,
1312 	.vidioc_g_ctrl		 = vidioc_g_ctrl,
1313 	.vidioc_querycap	 = vidioc_querycap,
1314 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1315 	.vidioc_g_fmt_vid_cap	 = vidioc_g_fmt_vid_cap,
1316 	.vidioc_try_fmt_vid_cap	 = vidioc_try_fmt_vid_cap,
1317 	.vidioc_s_fmt_vid_cap	 = vidioc_s_fmt_vid_cap,
1318 	.vidioc_g_ext_ctrls	 = vidioc_g_ext_ctrls,
1319 	.vidioc_s_ext_ctrls	 = vidioc_s_ext_ctrls,
1320 	.vidioc_try_ext_ctrls	 = vidioc_try_ext_ctrls,
1321 	.vidioc_queryctrl	 = vidioc_queryctrl,
1322 };
1323 
1324 static struct video_device saa7164_mpeg_template = {
1325 	.name          = "saa7164",
1326 	.fops          = &mpeg_fops,
1327 	.ioctl_ops     = &mpeg_ioctl_ops,
1328 	.minor         = -1,
1329 	.tvnorms       = SAA7164_NORMS,
1330 };
1331 
1332 static struct video_device *saa7164_encoder_alloc(
1333 	struct saa7164_port *port,
1334 	struct pci_dev *pci,
1335 	struct video_device *template,
1336 	char *type)
1337 {
1338 	struct video_device *vfd;
1339 	struct saa7164_dev *dev = port->dev;
1340 
1341 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1342 
1343 	vfd = video_device_alloc();
1344 	if (NULL == vfd)
1345 		return NULL;
1346 
1347 	*vfd = *template;
1348 	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1349 		type, saa7164_boards[dev->board].name);
1350 
1351 	vfd->v4l2_dev  = &dev->v4l2_dev;
1352 	vfd->release = video_device_release;
1353 	return vfd;
1354 }
1355 
1356 int saa7164_encoder_register(struct saa7164_port *port)
1357 {
1358 	struct saa7164_dev *dev = port->dev;
1359 	int result = -ENODEV;
1360 
1361 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1362 
1363 	if (port->type != SAA7164_MPEG_ENCODER)
1364 		BUG();
1365 
1366 	/* Sanity check that the PCI configuration space is active */
1367 	if (port->hwcfg.BARLocation == 0) {
1368 		printk(KERN_ERR "%s() failed "
1369 		       "(errno = %d), NO PCI configuration\n",
1370 			__func__, result);
1371 		result = -ENOMEM;
1372 		goto failed;
1373 	}
1374 
1375 	/* Establish encoder defaults here */
1376 	/* Set default TV standard */
1377 	port->encodernorm = saa7164_tvnorms[0];
1378 	port->width = 720;
1379 	port->mux_input = 1; /* Composite */
1380 	port->video_format = EU_VIDEO_FORMAT_MPEG_2;
1381 	port->audio_format = 0;
1382 	port->video_resolution = 0;
1383 	port->ctl_brightness = 127;
1384 	port->ctl_contrast = 66;
1385 	port->ctl_hue = 128;
1386 	port->ctl_saturation = 62;
1387 	port->ctl_sharpness = 8;
1388 	port->encoder_params.bitrate = ENCODER_DEF_BITRATE;
1389 	port->encoder_params.bitrate_peak = ENCODER_DEF_BITRATE;
1390 	port->encoder_params.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
1391 	port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1392 	port->encoder_params.ctl_mute = 0;
1393 	port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
1394 	port->encoder_params.refdist = 1;
1395 	port->encoder_params.gop_size = SAA7164_ENCODER_DEFAULT_GOP_SIZE;
1396 	port->std = V4L2_STD_NTSC_M;
1397 
1398 	if (port->encodernorm.id & V4L2_STD_525_60)
1399 		port->height = 480;
1400 	else
1401 		port->height = 576;
1402 
1403 	/* Allocate and register the video device node */
1404 	port->v4l_device = saa7164_encoder_alloc(port,
1405 		dev->pci, &saa7164_mpeg_template, "mpeg");
1406 
1407 	if (!port->v4l_device) {
1408 		printk(KERN_INFO "%s: can't allocate mpeg device\n",
1409 			dev->name);
1410 		result = -ENOMEM;
1411 		goto failed;
1412 	}
1413 
1414 	video_set_drvdata(port->v4l_device, port);
1415 	result = video_register_device(port->v4l_device,
1416 		VFL_TYPE_GRABBER, -1);
1417 	if (result < 0) {
1418 		printk(KERN_INFO "%s: can't register mpeg device\n",
1419 			dev->name);
1420 		/* TODO: We're going to leak here if we don't dealloc
1421 		 The buffers above. The unreg function can't deal wit it.
1422 		*/
1423 		goto failed;
1424 	}
1425 
1426 	printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
1427 		dev->name, port->v4l_device->num);
1428 
1429 	/* Configure the hardware defaults */
1430 	saa7164_api_set_videomux(port);
1431 	saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
1432 	saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
1433 	saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
1434 	saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
1435 	saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
1436 	saa7164_api_audio_mute(port, 0);
1437 	saa7164_api_set_audio_volume(port, 20);
1438 	saa7164_api_set_aspect_ratio(port);
1439 
1440 	/* Disable audio standard detection, it's buggy */
1441 	saa7164_api_set_audio_detection(port, 0);
1442 
1443 	saa7164_api_set_encoder(port);
1444 	saa7164_api_get_encoder(port);
1445 
1446 	result = 0;
1447 failed:
1448 	return result;
1449 }
1450 
1451 void saa7164_encoder_unregister(struct saa7164_port *port)
1452 {
1453 	struct saa7164_dev *dev = port->dev;
1454 
1455 	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1456 
1457 	if (port->type != SAA7164_MPEG_ENCODER)
1458 		BUG();
1459 
1460 	if (port->v4l_device) {
1461 		if (port->v4l_device->minor != -1)
1462 			video_unregister_device(port->v4l_device);
1463 		else
1464 			video_device_release(port->v4l_device);
1465 
1466 		port->v4l_device = NULL;
1467 	}
1468 
1469 	dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
1470 }
1471 
1472