blob: 5f73ceded31b09c5a1679b7d29054349315567ec [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
Steven Toth58acca12010-07-31 15:10:52 -03001033 port->last_read_msecs_diff = port->last_read_msecs;
1034 port->last_read_msecs = jiffies_to_msecs(jiffies);
1035 port->last_read_msecs_diff = port->last_read_msecs -
1036 port->last_read_msecs_diff;
1037
1038 saa7164_histogram_update(&port->read_interval,
1039 port->last_read_msecs_diff);
1040
Steven Toth7615e432010-07-31 14:44:53 -03001041 if (*pos)
1042 return -ESPIPE;
1043
1044 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1045 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1046
1047 if (saa7164_encoder_initialize(port) < 0)
1048 return -EINVAL;
1049
1050 saa7164_encoder_start_streaming(port);
1051 msleep(200);
1052 }
1053 }
1054
1055 /* blocking wait for buffer */
1056 if ((file->f_flags & O_NONBLOCK) == 0) {
1057 if (wait_event_interruptible(port->wait_read,
1058 saa7164_enc_next_buf(port))) {
1059 return -ERESTARTSYS;
1060 }
1061 }
1062
1063 /* Pull the first buffer from the used list */
1064 ubuf = saa7164_enc_next_buf(port);
1065
1066 while ((count > 0) && ubuf) {
1067
1068 /* set remaining bytes to copy */
1069 rem = ubuf->actual_size - ubuf->pos;
1070 cnt = rem > count ? count : rem;
1071
1072 p = ubuf->data + ubuf->pos;
1073
1074 dprintk(DBGLVL_ENC,
1075 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1076 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1077
1078 if (copy_to_user(buffer, p, cnt)) {
1079 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1080 if (!ret)
1081 ret = -EFAULT;
1082 goto err;
1083 }
1084
1085 ubuf->pos += cnt;
1086 count -= cnt;
1087 buffer += cnt;
1088 ret += cnt;
1089
1090 if (ubuf->pos == ubuf->actual_size) {
1091
1092 /* finished with current buffer, take next buffer */
1093
1094 /* Requeue the buffer on the free list */
1095 ubuf->pos = 0;
1096
1097 mutex_lock(&port->dmaqueue_lock);
1098 list_move_tail(&ubuf->list, &port->list_buf_free.list);
1099 mutex_unlock(&port->dmaqueue_lock);
1100
1101 /* Dequeue next */
1102 if ((file->f_flags & O_NONBLOCK) == 0) {
1103 if (wait_event_interruptible(port->wait_read,
1104 saa7164_enc_next_buf(port))) {
1105 break;
1106 }
1107 }
1108 ubuf = saa7164_enc_next_buf(port);
1109 }
1110 }
1111err:
1112 if (!ret && !ubuf)
1113 ret = -EAGAIN;
1114
1115 return ret;
1116}
1117
1118static unsigned int fops_poll(struct file *file, poll_table *wait)
1119{
1120 struct saa7164_fh *fh = (struct saa7164_fh *)file->private_data;
1121 struct saa7164_port *port = fh->port;
1122 struct saa7164_user_buffer *ubuf;
1123 unsigned int mask = 0;
1124
Steven Toth58acca12010-07-31 15:10:52 -03001125 port->last_poll_msecs_diff = port->last_poll_msecs;
1126 port->last_poll_msecs = jiffies_to_msecs(jiffies);
1127 port->last_poll_msecs_diff = port->last_poll_msecs -
1128 port->last_poll_msecs_diff;
1129
1130 saa7164_histogram_update(&port->poll_interval,
1131 port->last_poll_msecs_diff);
1132
Steven Toth7615e432010-07-31 14:44:53 -03001133 if (!video_is_registered(port->v4l_device)) {
1134 return -EIO;
1135 }
1136
1137 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1138 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1139 if (saa7164_encoder_initialize(port) < 0)
1140 return -EINVAL;
1141 saa7164_encoder_start_streaming(port);
1142 msleep(200);
1143 }
1144 }
1145
1146 /* blocking wait for buffer */
1147 if ((file->f_flags & O_NONBLOCK) == 0) {
1148 if (wait_event_interruptible(port->wait_read,
1149 saa7164_enc_next_buf(port))) {
1150 return -ERESTARTSYS;
1151 }
1152 }
1153
1154 /* Pull the first buffer from the used list */
1155 ubuf = list_first_entry(&port->list_buf_used.list,
1156 struct saa7164_user_buffer, list);
1157
1158 if (ubuf)
1159 mask |= POLLIN | POLLRDNORM;
1160
1161 return mask;
1162}
1163
1164static const struct v4l2_file_operations mpeg_fops = {
1165 .owner = THIS_MODULE,
1166 .open = fops_open,
1167 .release = fops_release,
1168 .read = fops_read,
1169 .poll = fops_poll,
1170 .unlocked_ioctl = video_ioctl2,
1171};
1172
1173int saa7164_g_chip_ident(struct file *file, void *fh,
1174 struct v4l2_dbg_chip_ident *chip)
1175{
1176 struct saa7164_port *port = ((struct saa7164_fh *)fh)->port;
1177 struct saa7164_dev *dev = port->dev;
1178 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1179
1180 return 0;
1181}
1182
1183int saa7164_g_register(struct file *file, void *fh,
1184 struct v4l2_dbg_register *reg)
1185{
1186 struct saa7164_port *port = ((struct saa7164_fh *)fh)->port;
1187 struct saa7164_dev *dev = port->dev;
1188 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1189
1190 if (!capable(CAP_SYS_ADMIN))
1191 return -EPERM;
1192
1193 return 0;
1194}
1195
1196int saa7164_s_register(struct file *file, void *fh,
1197 struct v4l2_dbg_register *reg)
1198{
1199 struct saa7164_port *port = ((struct saa7164_fh *)fh)->port;
1200 struct saa7164_dev *dev = port->dev;
1201 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1202
1203 if (!capable(CAP_SYS_ADMIN))
1204 return -EPERM;
1205
1206 return 0;
1207}
1208
1209static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1210 .vidioc_s_std = vidioc_s_std,
1211 .vidioc_enum_input = vidioc_enum_input,
1212 .vidioc_g_input = vidioc_g_input,
1213 .vidioc_s_input = vidioc_s_input,
1214 .vidioc_g_tuner = vidioc_g_tuner,
1215 .vidioc_s_tuner = vidioc_s_tuner,
1216 .vidioc_g_frequency = vidioc_g_frequency,
1217 .vidioc_s_frequency = vidioc_s_frequency,
1218 .vidioc_s_ctrl = vidioc_s_ctrl,
1219 .vidioc_g_ctrl = vidioc_g_ctrl,
1220 .vidioc_querycap = vidioc_querycap,
1221 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1222 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1223 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1224 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1225 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1226 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1227 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1228 .vidioc_log_status = vidioc_log_status,
1229 .vidioc_queryctrl = vidioc_queryctrl,
1230 .vidioc_g_chip_ident = saa7164_g_chip_ident,
1231#ifdef CONFIG_VIDEO_ADV_DEBUG
1232 .vidioc_g_register = saa7164_g_register,
1233 .vidioc_s_register = saa7164_s_register,
1234#endif
1235};
1236
1237static struct video_device saa7164_mpeg_template = {
1238 .name = "saa7164",
1239 .fops = &mpeg_fops,
1240 .ioctl_ops = &mpeg_ioctl_ops,
1241 .minor = -1,
1242 .tvnorms = SAA7164_NORMS,
1243 .current_norm = V4L2_STD_NTSC_M,
1244};
1245
1246static struct video_device *saa7164_encoder_alloc(
1247 struct saa7164_port *port,
1248 struct pci_dev *pci,
1249 struct video_device *template,
1250 char *type)
1251{
1252 struct video_device *vfd;
1253 struct saa7164_dev *dev = port->dev;
1254
1255 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1256
1257 vfd = video_device_alloc();
1258 if (NULL == vfd)
1259 return NULL;
1260
1261 *vfd = *template;
1262 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1263 type, saa7164_boards[dev->board].name);
1264
1265 vfd->parent = &pci->dev;
1266 vfd->release = video_device_release;
1267 return vfd;
1268}
1269
1270int saa7164_encoder_register(struct saa7164_port *port)
1271{
1272 struct saa7164_dev *dev = port->dev;
1273 struct saa7164_buffer *buf;
1274 struct saa7164_user_buffer *ubuf;
1275 int result = -ENODEV, i;
1276 int len = 0;
1277
1278 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1279
1280 if (port->type != SAA7164_MPEG_ENCODER)
1281 BUG();
1282
1283 /* Sanity check that the PCI configuration space is active */
1284 if (port->hwcfg.BARLocation == 0) {
1285 printk(KERN_ERR "%s() failed "
1286 "(errno = %d), NO PCI configuration\n",
1287 __func__, result);
1288 result = -ENOMEM;
1289 goto failed;
1290 }
1291
1292 /* Init and establish defaults */
1293 /* TODO: Check the umber of lines for PS */
1294 port->hw_streamingparams.bitspersample = 8;
1295 port->hw_streamingparams.samplesperline = 188;
1296 port->hw_streamingparams.numberoflines =
1297 (SAA7164_TS_NUMBER_OF_LINES * 188) / 188;
1298
1299 port->hw_streamingparams.pitch = 188;
1300 port->hw_streamingparams.linethreshold = 0;
1301 port->hw_streamingparams.pagetablelistvirt = 0;
1302 port->hw_streamingparams.pagetablelistphys = 0;
1303 port->hw_streamingparams.numpagetables = 2 +
1304 ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
1305
1306 port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount;
1307
1308 /* Allocate the PCI resources, buffers (hard) */
1309 for (i = 0; i < port->hwcfg.buffercount; i++) {
1310 buf = saa7164_buffer_alloc(port,
1311 port->hw_streamingparams.numberoflines *
1312 port->hw_streamingparams.pitch);
1313
1314 if (!buf) {
1315 printk(KERN_ERR "%s() failed "
1316 "(errno = %d), unable to allocate buffer\n",
1317 __func__, result);
1318 result = -ENOMEM;
1319 goto failed;
1320 } else {
1321
1322 mutex_lock(&port->dmaqueue_lock);
1323 list_add_tail(&buf->list, &port->dmaqueue.list);
1324 mutex_unlock(&port->dmaqueue_lock);
1325
1326 }
1327 }
1328
1329 /* Allocate some kenrel kernel buffers for copying
1330 * to userpsace.
1331 */
1332 len = port->hw_streamingparams.numberoflines *
1333 port->hw_streamingparams.pitch;
1334
Steven Toth66e1d372010-07-31 15:09:25 -03001335 if (encoder_buffers < 16)
1336 encoder_buffers = 16;
1337 if (encoder_buffers > 512)
1338 encoder_buffers = 512;
1339
1340 for (i = 0; i < encoder_buffers; i++) {
Steven Toth7615e432010-07-31 14:44:53 -03001341
1342 ubuf = saa7164_buffer_alloc_user(dev, len);
1343 if (ubuf) {
1344 mutex_lock(&port->dmaqueue_lock);
1345 list_add_tail(&ubuf->list, &port->list_buf_free.list);
1346 mutex_unlock(&port->dmaqueue_lock);
1347 }
1348
1349 }
1350
1351 /* Establish encoder defaults here */
1352 /* Set default TV standard */
1353 port->encodernorm = saa7164_tvnorms[0];
1354 port->width = 720;
1355 port->mux_input = 1; /* Composite */
Steven Toth7615e432010-07-31 14:44:53 -03001356 port->video_format = EU_VIDEO_FORMAT_MPEG_2;
1357 port->audio_format = 0;
1358 port->video_resolution = 0;
1359 port->ctl_brightness = 127;
1360 port->ctl_contrast = 66;
1361 port->ctl_hue = 128;
1362 port->ctl_saturation = 62;
1363 port->ctl_sharpness = 8;
1364 port->encoder_params.bitrate = ENCODER_DEF_BITRATE;
Steven Toth968b11b2010-07-31 14:59:38 -03001365 port->encoder_params.bitrate_peak = ENCODER_DEF_BITRATE;
Steven Toth5fa56cc2010-07-31 15:05:35 -03001366 port->encoder_params.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
Steven Toth7615e432010-07-31 14:44:53 -03001367 port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1368 port->encoder_params.ctl_mute = 0;
1369 port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
Steven Toth3ed43cf2010-07-31 14:58:35 -03001370 port->encoder_params.refdist = 1;
Steven Toth5fa56cc2010-07-31 15:05:35 -03001371 port->encoder_params.gop_size = SAA7164_ENCODER_DEFAULT_GOP_SIZE;
Steven Toth7615e432010-07-31 14:44:53 -03001372
1373 if (port->encodernorm.id & V4L2_STD_525_60)
1374 port->height = 480;
1375 else
1376 port->height = 576;
1377
1378 /* Allocate and register the video device node */
1379 port->v4l_device = saa7164_encoder_alloc(port,
1380 dev->pci, &saa7164_mpeg_template, "mpeg");
1381
1382 if (port->v4l_device == NULL) {
1383 printk(KERN_INFO "%s: can't allocate mpeg device\n",
1384 dev->name);
1385 result = -ENOMEM;
1386 goto failed;
1387 }
1388
1389 result = video_register_device(port->v4l_device,
1390 VFL_TYPE_GRABBER, -1);
1391 if (result < 0) {
1392 printk(KERN_INFO "%s: can't register mpeg device\n",
1393 dev->name);
1394 /* TODO: We're going to leak here if we don't dealloc
1395 The buffers above. The unreg function can't deal wit it.
1396 */
1397 goto failed;
1398 }
1399
1400 printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
1401 dev->name, port->v4l_device->num);
1402
1403 /* Configure the hardware defaults */
1404 saa7164_api_set_videomux(port);
1405 saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
1406 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
1407 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
1408 saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
1409 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
1410 saa7164_api_audio_mute(port, 0);
1411 saa7164_api_set_audio_volume(port, 20);
1412 saa7164_api_set_aspect_ratio(port);
1413
1414 /* Disable audio standard detection, it's buggy */
1415 saa7164_api_set_audio_detection(port, 0);
1416
1417 saa7164_api_set_encoder(port);
1418 saa7164_api_get_encoder(port);
1419
1420 result = 0;
1421failed:
1422 return result;
1423}
1424
1425void saa7164_encoder_unregister(struct saa7164_port *port)
1426{
1427 struct saa7164_dev *dev = port->dev;
1428 struct saa7164_buffer *buf;
1429 struct saa7164_user_buffer *ubuf;
1430 struct list_head *c, *n, *p, *q, *l, *v;
1431
1432 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1433
1434 if (port->type != SAA7164_MPEG_ENCODER)
1435 BUG();
1436
1437 if (port->v4l_device) {
1438 if (port->v4l_device->minor != -1)
1439 video_unregister_device(port->v4l_device);
1440 else
1441 video_device_release(port->v4l_device);
1442
1443 port->v4l_device = NULL;
1444 }
1445
1446 /* Remove any allocated buffers */
1447 mutex_lock(&port->dmaqueue_lock);
1448
1449 dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
1450 list_for_each_safe(c, n, &port->dmaqueue.list) {
1451 buf = list_entry(c, struct saa7164_buffer, list);
1452 list_del(c);
1453 saa7164_buffer_dealloc(buf);
1454 }
1455
1456 dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
1457 list_for_each_safe(p, q, &port->list_buf_used.list) {
1458 ubuf = list_entry(p, struct saa7164_user_buffer, list);
1459 list_del(p);
1460 saa7164_buffer_dealloc_user(ubuf);
1461 }
1462
1463 dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
1464 list_for_each_safe(l, v, &port->list_buf_free.list) {
1465 ubuf = list_entry(l, struct saa7164_user_buffer, list);
1466 list_del(l);
1467 saa7164_buffer_dealloc_user(ubuf);
1468 }
1469
1470 mutex_unlock(&port->dmaqueue_lock);
1471 dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
1472}
1473