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