blob: 22c7d8ad72562de6aa6902af6940e30c20027f76 [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
Steven Toth1b0e8e42010-07-31 16:01:00 -030079static 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 */
117static int saa7164_encoder_buffers_alloc(struct saa7164_port *port)
Steven Toth7615e432010-07-31 14:44:53 -0300118{
119 struct saa7164_dev *dev = port->dev;
Steven Toth1b0e8e42010-07-31 16:01:00 -0300120 struct saa7164_buffer *buf;
121 struct saa7164_user_buffer *ubuf;
122 tmHWStreamParameters_t *params = &port->hw_streamingparams;
123 int result = -ENODEV, i;
124 int len = 0;
Steven Toth7615e432010-07-31 14:44:53 -0300125
126 dprintk(DBGLVL_ENC, "%s()\n", __func__);
127
Steven Toth1b0e8e42010-07-31 16:01:00 -0300128 if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
129 dprintk(DBGLVL_ENC, "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n", __func__);
130 params->samplesperline = 128;
131 params->numberoflines = 256;
132 params->pitch = 128;
133 params->numpagetables = 2 +
134 ((SAA7164_PS_NUMBER_OF_LINES * 128) / PAGE_SIZE);
135 } else
136 if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_TS) {
137 dprintk(DBGLVL_ENC, "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n", __func__);
138 params->samplesperline = 188;
139 params->numberoflines = 312;
140 params->pitch = 188;
141 params->numpagetables = 2 +
142 ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
143 } else
144 BUG();
Steven Toth7615e432010-07-31 14:44:53 -0300145
Steven Toth1b0e8e42010-07-31 16:01:00 -0300146 /* Init and establish defaults */
147 params->bitspersample = 8;
148 params->linethreshold = 0;
149 params->pagetablelistvirt = 0;
150 params->pagetablelistphys = 0;
151 params->numpagetableentries = port->hwcfg.buffercount;
152
153 /* Allocate the PCI resources, buffers (hard) */
154 for (i = 0; i < port->hwcfg.buffercount; i++) {
155 buf = saa7164_buffer_alloc(port,
156 params->numberoflines *
157 params->pitch);
158
159 if (!buf) {
160 printk(KERN_ERR "%s() failed "
161 "(errno = %d), unable to allocate buffer\n",
162 __func__, result);
163 result = -ENOMEM;
164 goto failed;
165 } else {
166
167 mutex_lock(&port->dmaqueue_lock);
168 list_add_tail(&buf->list, &port->dmaqueue.list);
169 mutex_unlock(&port->dmaqueue_lock);
170
171 }
172 }
173
174 /* Allocate some kenrel kernel buffers for copying
175 * to userpsace.
176 */
177 len = params->numberoflines * params->pitch;
178
179 if (encoder_buffers < 16)
180 encoder_buffers = 16;
181 if (encoder_buffers > 512)
182 encoder_buffers = 512;
183
184 for (i = 0; i < encoder_buffers; i++) {
185
186 ubuf = saa7164_buffer_alloc_user(dev, len);
187 if (ubuf) {
188 mutex_lock(&port->dmaqueue_lock);
189 list_add_tail(&ubuf->list, &port->list_buf_free.list);
190 mutex_unlock(&port->dmaqueue_lock);
191 }
192
193 }
194
195 result = 0;
196
197failed:
198 return result;
199}
200
201static int saa7164_encoder_initialize(struct saa7164_port *port)
202{
203 saa7164_encoder_configure(port);
Steven Toth7615e432010-07-31 14:44:53 -0300204 return 0;
205}
206
207/* -- V4L2 --------------------------------------------------------- */
208static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
209{
Steven Toth96d84202010-07-31 16:04:59 -0300210 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300211 struct saa7164_port *port = fh->port;
212 struct saa7164_dev *dev = port->dev;
213 unsigned int i;
214
215 dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)*id);
216
217 for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
218 if (*id & saa7164_tvnorms[i].id)
219 break;
220 }
221 if (i == ARRAY_SIZE(saa7164_tvnorms))
222 return -EINVAL;
223
224 port->encodernorm = saa7164_tvnorms[i];
225
226 /* Update the audio decoder while is not running in
227 * auto detect mode.
228 */
229 saa7164_api_set_audio_std(port);
230
231 dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)*id);
232
233 return 0;
234}
235
236static int vidioc_enum_input(struct file *file, void *priv,
237 struct v4l2_input *i)
238{
239 int n;
240
241 char *inputs[] = { "tuner", "composite", "svideo", "aux",
242 "composite", "svideo", "aux" };
243
244 if (i->index >= 7)
245 return -EINVAL;
246
247 strcpy(i->name, inputs[ i->index ]);
248
249 if (i->index == 0)
250 i->type = V4L2_INPUT_TYPE_TUNER;
251 else
252 i->type = V4L2_INPUT_TYPE_CAMERA;
253
254 for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
255 i->std |= saa7164_tvnorms[n].id;
256
257 return 0;
258}
259
260static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
261{
Steven Toth96d84202010-07-31 16:04:59 -0300262 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300263 struct saa7164_port *port = fh->port;
264 struct saa7164_dev *dev = port->dev;
265
266 if (saa7164_api_get_videomux(port) != SAA_OK)
267 return -EIO;
268
269 *i = (port->mux_input - 1);
270
271 dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);
272
273 return 0;
274}
275
276static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
277{
Steven Toth96d84202010-07-31 16:04:59 -0300278 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300279 struct saa7164_port *port = fh->port;
280 struct saa7164_dev *dev = port->dev;
281
282 dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);
283
284 if (i >= 7)
285 return -EINVAL;
286
287 port->mux_input = i + 1;
288
289 if (saa7164_api_set_videomux(port) != SAA_OK)
290 return -EIO;
291
292 return 0;
293}
294
295static int vidioc_g_tuner(struct file *file, void *priv,
296 struct v4l2_tuner *t)
297{
Steven Toth96d84202010-07-31 16:04:59 -0300298 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300299 struct saa7164_port *port = fh->port;
300 struct saa7164_dev *dev = port->dev;
301
302 if (0 != t->index)
303 return -EINVAL;
304
305 strcpy(t->name, "tuner");
306 t->type = V4L2_TUNER_ANALOG_TV;
307 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
308
309 dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
310
311 return 0;
312}
313
314static int vidioc_s_tuner(struct file *file, void *priv,
315 struct v4l2_tuner *t)
316{
317
318 /* Update the A/V core */
319
320 return 0;
321}
322
323static int vidioc_g_frequency(struct file *file, void *priv,
324 struct v4l2_frequency *f)
325{
Steven Toth96d84202010-07-31 16:04:59 -0300326 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300327 struct saa7164_port *port = fh->port;
328
329 f->type = V4L2_TUNER_ANALOG_TV;
330 f->frequency = port->freq;
331
332 return 0;
333}
334
335static int vidioc_s_frequency(struct file *file, void *priv,
336 struct v4l2_frequency *f)
337{
Steven Toth96d84202010-07-31 16:04:59 -0300338 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300339 struct saa7164_port *port = fh->port;
340 struct saa7164_dev *dev = port->dev;
341 struct saa7164_port *tsport;
342 struct dvb_frontend *fe;
343
344 /* TODO: Pull this for the std */
345 struct analog_parameters params = {
346 .mode = V4L2_TUNER_ANALOG_TV,
347 .audmode = V4L2_TUNER_MODE_STEREO,
348 .std = port->encodernorm.id,
349 .frequency = f->frequency
350 };
351
352 /* Stop the encoder */
353 dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
354 f->frequency, f->tuner);
355
356 if (f->tuner != 0)
357 return -EINVAL;
358
359 if (f->type != V4L2_TUNER_ANALOG_TV)
360 return -EINVAL;
361
362 port->freq = f->frequency;
363
364 /* Update the hardware */
365 if (port->nr == SAA7164_PORT_ENC1)
366 tsport = &dev->ports[ SAA7164_PORT_TS1 ];
367 else
368 if (port->nr == SAA7164_PORT_ENC2)
369 tsport = &dev->ports[ SAA7164_PORT_TS2 ];
370 else
371 BUG();
372
373 fe = tsport->dvb.frontend;
374
375 if (fe && fe->ops.tuner_ops.set_analog_params)
376 fe->ops.tuner_ops.set_analog_params(fe, &params);
377 else
378 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
379
380 saa7164_encoder_initialize(port);
381
382 return 0;
383}
384
385static int vidioc_g_ctrl(struct file *file, void *priv,
386 struct v4l2_control *ctl)
387{
Steven Toth96d84202010-07-31 16:04:59 -0300388 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300389 struct saa7164_port *port = fh->port;
390 struct saa7164_dev *dev = port->dev;
391
392 dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
393 ctl->id, ctl->value);
394
395 switch (ctl->id) {
396 case V4L2_CID_BRIGHTNESS:
397 ctl->value = port->ctl_brightness;
398 break;
399 case V4L2_CID_CONTRAST:
400 ctl->value = port->ctl_contrast;
401 break;
402 case V4L2_CID_SATURATION:
403 ctl->value = port->ctl_saturation;
404 break;
405 case V4L2_CID_HUE:
406 ctl->value = port->ctl_hue;
407 break;
408 case V4L2_CID_SHARPNESS:
409 ctl->value = port->ctl_sharpness;
410 break;
411 case V4L2_CID_AUDIO_VOLUME:
412 ctl->value = port->ctl_volume;
413 break;
414 default:
415 return -EINVAL;
416 }
417
418 return 0;
419}
420
421static int vidioc_s_ctrl(struct file *file, void *priv,
422 struct v4l2_control *ctl)
423{
Steven Toth96d84202010-07-31 16:04:59 -0300424 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300425 struct saa7164_port *port = fh->port;
426 struct saa7164_dev *dev = port->dev;
427 int ret = 0;
428
429 dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
430 ctl->id, ctl->value);
431
432 switch (ctl->id) {
433 case V4L2_CID_BRIGHTNESS:
434 if ((ctl->value >= 0) && (ctl->value <= 255)) {
435 port->ctl_brightness = ctl->value;
436 saa7164_api_set_usercontrol(port,
437 PU_BRIGHTNESS_CONTROL);
438 } else
439 ret = -EINVAL;
440 break;
441 case V4L2_CID_CONTRAST:
442 if ((ctl->value >= 0) && (ctl->value <= 255)) {
443 port->ctl_contrast = ctl->value;
444 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
445 } else
446 ret = -EINVAL;
447 break;
448 case V4L2_CID_SATURATION:
449 if ((ctl->value >= 0) && (ctl->value <= 255)) {
450 port->ctl_saturation = ctl->value;
451 saa7164_api_set_usercontrol(port,
452 PU_SATURATION_CONTROL);
453 } else
454 ret = -EINVAL;
455 break;
456 case V4L2_CID_HUE:
457 if ((ctl->value >= 0) && (ctl->value <= 255)) {
458 port->ctl_hue = ctl->value;
459 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
460 } else
461 ret = -EINVAL;
462 break;
463 case V4L2_CID_SHARPNESS:
464 if ((ctl->value >= 0) && (ctl->value <= 255)) {
465 port->ctl_sharpness = ctl->value;
466 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
467 } else
468 ret = -EINVAL;
469 break;
470 case V4L2_CID_AUDIO_VOLUME:
471 if ((ctl->value >= -83) && (ctl->value <= 24)) {
472 port->ctl_volume = ctl->value;
473 saa7164_api_set_audio_volume(port, port->ctl_volume);
474 } else
475 ret = -EINVAL;
476 break;
477 default:
478 ret = -EINVAL;
479 }
480
481 return ret;
482}
483
484static int saa7164_get_ctrl(struct saa7164_port *port,
485 struct v4l2_ext_control *ctrl)
486{
487 struct saa7164_encoder_params *params = &port->encoder_params;
488
489 switch (ctrl->id) {
490 case V4L2_CID_MPEG_VIDEO_BITRATE:
491 ctrl->value = params->bitrate;
492 break;
493 case V4L2_CID_MPEG_STREAM_TYPE:
494 ctrl->value = params->stream_type;
495 break;
496 case V4L2_CID_MPEG_AUDIO_MUTE:
497 ctrl->value = params->ctl_mute;
498 break;
499 case V4L2_CID_MPEG_VIDEO_ASPECT:
500 ctrl->value = params->ctl_aspect;
501 break;
Steven Toth2600d712010-07-31 14:51:30 -0300502 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
503 ctrl->value = params->bitrate_mode;
504 break;
Steven Toth3ed43cf2010-07-31 14:58:35 -0300505 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
506 ctrl->value = params->refdist;
507 break;
Steven Toth968b11b2010-07-31 14:59:38 -0300508 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
509 ctrl->value = params->bitrate_peak;
510 break;
Steven Toth5fa56cc2010-07-31 15:05:35 -0300511 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
512 ctrl->value = params->gop_size;
513 break;
Steven Toth7615e432010-07-31 14:44:53 -0300514 default:
515 return -EINVAL;
516 }
517 return 0;
518}
519
520static int vidioc_g_ext_ctrls(struct file *file, void *priv,
521 struct v4l2_ext_controls *ctrls)
522{
Steven Toth96d84202010-07-31 16:04:59 -0300523 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300524 struct saa7164_port *port = fh->port;
525 int i, err = 0;
526
527 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
528 for (i = 0; i < ctrls->count; i++) {
529 struct v4l2_ext_control *ctrl = ctrls->controls + i;
530
531 err = saa7164_get_ctrl(port, ctrl);
532 if (err) {
533 ctrls->error_idx = i;
534 break;
535 }
536 }
537 return err;
538
539 }
540
541 return -EINVAL;
542}
543
544static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
545{
546 int ret = -EINVAL;
547
548 switch (ctrl->id) {
549 case V4L2_CID_MPEG_VIDEO_BITRATE:
550 if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
551 (ctrl->value <= ENCODER_MAX_BITRATE))
552 ret = 0;
553 break;
554 case V4L2_CID_MPEG_STREAM_TYPE:
Steven Tothf91d0952010-07-31 15:03:31 -0300555 if ((ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) ||
556 (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS))
Steven Toth7615e432010-07-31 14:44:53 -0300557 ret = 0;
558 break;
559 case V4L2_CID_MPEG_AUDIO_MUTE:
560 if ((ctrl->value >= 0) &&
561 (ctrl->value <= 1))
562 ret = 0;
563 break;
564 case V4L2_CID_MPEG_VIDEO_ASPECT:
565 if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
566 (ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
567 ret = 0;
568 break;
569 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
570 if ((ctrl->value >= 0) &&
571 (ctrl->value <= 255))
572 ret = 0;
573 break;
Steven Toth2600d712010-07-31 14:51:30 -0300574 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
575 if ((ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ||
576 (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR))
577 ret = 0;
578 break;
Steven Toth3ed43cf2010-07-31 14:58:35 -0300579 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
580 if ((ctrl->value >= 1) &&
581 (ctrl->value <= 3))
582 ret = 0;
583 break;
Steven Toth968b11b2010-07-31 14:59:38 -0300584 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
585 if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
586 (ctrl->value <= ENCODER_MAX_BITRATE))
587 ret = 0;
588 break;
Steven Toth7615e432010-07-31 14:44:53 -0300589 default:
590 ret = -EINVAL;
591 }
592
593 return ret;
594}
595
596static int vidioc_try_ext_ctrls(struct file *file, void *priv,
597 struct v4l2_ext_controls *ctrls)
598{
599 int i, err = 0;
600
601 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
602 for (i = 0; i < ctrls->count; i++) {
603 struct v4l2_ext_control *ctrl = ctrls->controls + i;
604
605 err = saa7164_try_ctrl(ctrl, 0);
606 if (err) {
607 ctrls->error_idx = i;
608 break;
609 }
610 }
611 return err;
612 }
613
614 return -EINVAL;
615}
616
617static int saa7164_set_ctrl(struct saa7164_port *port,
618 struct v4l2_ext_control *ctrl)
619{
620 struct saa7164_encoder_params *params = &port->encoder_params;
621 int ret = 0;
622
623 switch (ctrl->id) {
624 case V4L2_CID_MPEG_VIDEO_BITRATE:
625 params->bitrate = ctrl->value;
626 break;
627 case V4L2_CID_MPEG_STREAM_TYPE:
628 params->stream_type = ctrl->value;
629 break;
630 case V4L2_CID_MPEG_AUDIO_MUTE:
631 params->ctl_mute = ctrl->value;
632 ret = saa7164_api_audio_mute(port, params->ctl_mute);
633 if (ret != SAA_OK) {
634 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
635 ret);
636 ret = -EIO;
637 }
638 break;
639 case V4L2_CID_MPEG_VIDEO_ASPECT:
640 params->ctl_aspect = ctrl->value;
641 ret = saa7164_api_set_aspect_ratio(port);
642 if (ret != SAA_OK) {
643 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
644 ret);
645 ret = -EIO;
646 }
647 break;
Steven Toth2600d712010-07-31 14:51:30 -0300648 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
649 params->bitrate_mode = ctrl->value;
650 break;
Steven Toth3ed43cf2010-07-31 14:58:35 -0300651 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
652 params->refdist = ctrl->value;
653 break;
Steven Toth968b11b2010-07-31 14:59:38 -0300654 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
655 params->bitrate_peak = ctrl->value;
656 break;
Steven Toth5fa56cc2010-07-31 15:05:35 -0300657 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
658 params->gop_size = ctrl->value;
659 break;
Steven Toth7615e432010-07-31 14:44:53 -0300660 default:
661 return -EINVAL;
662 }
663
664 /* TODO: Update the hardware */
665
666 return ret;
667}
668
669static int vidioc_s_ext_ctrls(struct file *file, void *priv,
670 struct v4l2_ext_controls *ctrls)
671{
Steven Toth96d84202010-07-31 16:04:59 -0300672 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300673 struct saa7164_port *port = fh->port;
674 int i, err = 0;
675
676 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
677 for (i = 0; i < ctrls->count; i++) {
678 struct v4l2_ext_control *ctrl = ctrls->controls + i;
679
680 err = saa7164_try_ctrl(ctrl, 0);
681 if (err) {
682 ctrls->error_idx = i;
683 break;
684 }
685 err = saa7164_set_ctrl(port, ctrl);
686 if (err) {
687 ctrls->error_idx = i;
688 break;
689 }
690 }
691 return err;
692
693 }
694
695 return -EINVAL;
696}
697
698static int vidioc_querycap(struct file *file, void *priv,
699 struct v4l2_capability *cap)
700{
Steven Toth96d84202010-07-31 16:04:59 -0300701 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300702 struct saa7164_port *port = fh->port;
703 struct saa7164_dev *dev = port->dev;
704
705 strcpy(cap->driver, dev->name);
706 strlcpy(cap->card, saa7164_boards[dev->board].name,
707 sizeof(cap->card));
708 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
709
710 cap->capabilities =
711 V4L2_CAP_VIDEO_CAPTURE |
712 V4L2_CAP_READWRITE |
713 V4L2_CAP_STREAMING |
714 0;
715
716 cap->capabilities |= V4L2_CAP_TUNER;
717 cap->version = 0;
718
719 return 0;
720}
721
722static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
723 struct v4l2_fmtdesc *f)
724{
725 if (f->index != 0)
726 return -EINVAL;
727
728 strlcpy(f->description, "MPEG", sizeof(f->description));
729 f->pixelformat = V4L2_PIX_FMT_MPEG;
730
731 return 0;
732}
733
734static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
735 struct v4l2_format *f)
736{
Steven Toth96d84202010-07-31 16:04:59 -0300737 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300738 struct saa7164_port *port = fh->port;
739 struct saa7164_dev *dev = port->dev;
740
741 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
742 f->fmt.pix.bytesperline = 0;
743 f->fmt.pix.sizeimage =
744 port->ts_packet_size * port->ts_packet_count;
745 f->fmt.pix.colorspace = 0;
746 f->fmt.pix.width = port->width;
747 f->fmt.pix.height = port->height;
748
749 dprintk(DBGLVL_ENC, "VIDIOC_G_FMT: w: %d, h: %d\n",
750 port->width, port->height);
751
752 return 0;
753}
754
755static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
756 struct v4l2_format *f)
757{
Steven Toth96d84202010-07-31 16:04:59 -0300758 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300759 struct saa7164_port *port = fh->port;
760 struct saa7164_dev *dev = port->dev;
761
762 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
763 f->fmt.pix.bytesperline = 0;
764 f->fmt.pix.sizeimage =
765 port->ts_packet_size * port->ts_packet_count;
766 f->fmt.pix.colorspace = 0;
767 dprintk(DBGLVL_ENC, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
768 port->width, port->height);
769 return 0;
770}
771
772static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
773 struct v4l2_format *f)
774{
Steven Toth96d84202010-07-31 16:04:59 -0300775 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -0300776 struct saa7164_port *port = fh->port;
777 struct saa7164_dev *dev = port->dev;
778
779 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
780 f->fmt.pix.bytesperline = 0;
781 f->fmt.pix.sizeimage =
782 port->ts_packet_size * port->ts_packet_count;
783 f->fmt.pix.colorspace = 0;
784
785 dprintk(DBGLVL_ENC, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
786 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
787
788 return 0;
789}
790
791static int vidioc_log_status(struct file *file, void *priv)
792{
793 return 0;
794}
795
796static int fill_queryctrl(struct saa7164_encoder_params *params,
797 struct v4l2_queryctrl *c)
798{
799 switch (c->id) {
800 case V4L2_CID_BRIGHTNESS:
801 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
802 case V4L2_CID_CONTRAST:
803 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
804 case V4L2_CID_SATURATION:
805 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
806 case V4L2_CID_HUE:
807 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
808 case V4L2_CID_SHARPNESS:
809 return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
810 case V4L2_CID_MPEG_AUDIO_MUTE:
811 return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
812 case V4L2_CID_AUDIO_VOLUME:
813 return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
814 case V4L2_CID_MPEG_VIDEO_BITRATE:
815 return v4l2_ctrl_query_fill(c,
816 ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
817 100000, ENCODER_DEF_BITRATE);
818 case V4L2_CID_MPEG_STREAM_TYPE:
819 return v4l2_ctrl_query_fill(c,
820 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
Steven Tothf91d0952010-07-31 15:03:31 -0300821 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
822 1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
Steven Toth7615e432010-07-31 14:44:53 -0300823 case V4L2_CID_MPEG_VIDEO_ASPECT:
824 return v4l2_ctrl_query_fill(c,
825 V4L2_MPEG_VIDEO_ASPECT_1x1,
826 V4L2_MPEG_VIDEO_ASPECT_221x100,
827 1, V4L2_MPEG_VIDEO_ASPECT_4x3);
828 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
829 return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
Steven Toth2600d712010-07-31 14:51:30 -0300830 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
831 return v4l2_ctrl_query_fill(c,
832 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR, V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
833 1, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
Steven Toth3ed43cf2010-07-31 14:58:35 -0300834 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
835 return v4l2_ctrl_query_fill(c,
836 1, 3, 1, 1);
Steven Toth968b11b2010-07-31 14:59:38 -0300837 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
838 return v4l2_ctrl_query_fill(c,
839 ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
840 100000, ENCODER_DEF_BITRATE);
Steven Toth7615e432010-07-31 14:44:53 -0300841 default:
842 return -EINVAL;
843 }
844}
845
846static int vidioc_queryctrl(struct file *file, void *priv,
847 struct v4l2_queryctrl *c)
848{
Steven Toth96d84202010-07-31 16:04:59 -0300849 struct saa7164_encoder_fh *fh = priv;
Steven Toth7615e432010-07-31 14:44:53 -0300850 struct saa7164_port *port = fh->port;
851 int i, next;
852 u32 id = c->id;
853
854 memset(c, 0, sizeof(*c));
855
856 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
857 c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
858
859 for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
860 if (next) {
861 if (c->id < saa7164_v4l2_ctrls[i])
862 c->id = saa7164_v4l2_ctrls[i];
863 else
864 continue;
865 }
866
867 if (c->id == saa7164_v4l2_ctrls[i])
868 return fill_queryctrl(&port->encoder_params, c);
869
870 if (c->id < saa7164_v4l2_ctrls[i])
871 break;
872 }
873
874 return -EINVAL;
875}
876
877static int saa7164_encoder_stop_port(struct saa7164_port *port)
878{
879 struct saa7164_dev *dev = port->dev;
880 int ret;
881
882 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
883 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
884 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
885 __func__, ret);
886 ret = -EIO;
887 } else {
888 dprintk(DBGLVL_ENC, "%s() Stopped\n", __func__);
889 ret = 0;
890 }
891
892 return ret;
893}
894
895static int saa7164_encoder_acquire_port(struct saa7164_port *port)
896{
897 struct saa7164_dev *dev = port->dev;
898 int ret;
899
900 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
901 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
902 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
903 __func__, ret);
904 ret = -EIO;
905 } else {
906 dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
907 ret = 0;
908 }
909
910 return ret;
911}
912
913static int saa7164_encoder_pause_port(struct saa7164_port *port)
914{
915 struct saa7164_dev *dev = port->dev;
916 int ret;
917
918 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
919 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
920 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
921 __func__, ret);
922 ret = -EIO;
923 } else {
924 dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
925 ret = 0;
926 }
927
928 return ret;
929}
930
931/* Firmware is very windows centric, meaning you have to transition
932 * the part through AVStream / KS Windows stages, forwards or backwards.
933 * States are: stopped, acquired (h/w), paused, started.
934 * We have to leave here will all of the soft buffers on the free list,
935 * else the cfg_post() func won't have soft buffers to correctly configure.
936 */
937static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
938{
939 struct saa7164_dev *dev = port->dev;
940 struct saa7164_buffer *buf;
941 struct saa7164_user_buffer *ubuf;
942 struct list_head *c, *n;
943 int ret;
944
945 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
946
947 ret = saa7164_encoder_pause_port(port);
948 ret = saa7164_encoder_acquire_port(port);
949 ret = saa7164_encoder_stop_port(port);
950
951 dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
952 port->nr);
953
Steven Toth1b0e8e42010-07-31 16:01:00 -0300954 /* Reset the state of any allocated buffer resources */
Steven Toth7615e432010-07-31 14:44:53 -0300955 mutex_lock(&port->dmaqueue_lock);
956
957 /* Reset the hard and soft buffer state */
958 list_for_each_safe(c, n, &port->dmaqueue.list) {
959 buf = list_entry(c, struct saa7164_buffer, list);
960 buf->flags = SAA7164_BUFFER_FREE;
961 buf->pos = 0;
962 }
963
964 list_for_each_safe(c, n, &port->list_buf_used.list) {
965 ubuf = list_entry(c, struct saa7164_user_buffer, list);
966 ubuf->pos = 0;
967 list_move_tail(&ubuf->list, &port->list_buf_free.list);
968 }
969
970 mutex_unlock(&port->dmaqueue_lock);
Steven Toth1b0e8e42010-07-31 16:01:00 -0300971
972 /* Free any allocated resources */
973 saa7164_encoder_buffers_dealloc(port);
974
Steven Toth7615e432010-07-31 14:44:53 -0300975 dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
976
977 return ret;
978}
979
980static int saa7164_encoder_start_streaming(struct saa7164_port *port)
981{
982 struct saa7164_dev *dev = port->dev;
983 int result, ret = 0;
984
985 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
986
Steven Toth1b0e8e42010-07-31 16:01:00 -0300987 port->done_first_interrupt = 0;
988
989 /* allocate all of the PCIe DMA buffer resources on the fly,
990 * allowing switching between TS and PS payloads without
991 * requiring a complete driver reload.
992 */
993 saa7164_encoder_buffers_alloc(port);
994
Steven Toth7615e432010-07-31 14:44:53 -0300995 /* Configure the encoder with any cache values */
996 saa7164_api_set_encoder(port);
Steven Toth12d32032010-07-31 15:13:45 -0300997 saa7164_api_get_encoder(port);
Steven Toth7615e432010-07-31 14:44:53 -0300998
Steven Toth1b0e8e42010-07-31 16:01:00 -0300999 /* Place the empty buffers on the hardware */
Steven Toth7615e432010-07-31 14:44:53 -03001000 saa7164_buffer_cfg_port(port);
1001
1002 /* Acquire the hardware */
1003 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
1004 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1005 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
1006 __func__, result);
1007
1008 /* Stop the hardware, regardless */
1009 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1010 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1011 printk(KERN_ERR "%s() acquire/forced stop transition "
1012 "failed, res = 0x%x\n", __func__, result);
1013 }
1014 ret = -EIO;
1015 goto out;
1016 } else
1017 dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
1018
1019 /* Pause the hardware */
1020 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
1021 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1022 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
1023 __func__, result);
1024
1025 /* Stop the hardware, regardless */
1026 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1027 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1028 printk(KERN_ERR "%s() pause/forced stop transition "
1029 "failed, res = 0x%x\n", __func__, result);
1030 }
1031
1032 ret = -EIO;
1033 goto out;
1034 } else
1035 dprintk(DBGLVL_ENC, "%s() Paused\n", __func__);
1036
1037 /* Start the hardware */
1038 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
1039 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1040 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
1041 __func__, result);
1042
1043 /* Stop the hardware, regardless */
1044 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1045 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1046 printk(KERN_ERR "%s() run/forced stop transition "
1047 "failed, res = 0x%x\n", __func__, result);
1048 }
1049
1050 ret = -EIO;
1051 } else
1052 dprintk(DBGLVL_ENC, "%s() Running\n", __func__);
1053
1054out:
1055 return ret;
1056}
1057
1058static int fops_open(struct file *file)
1059{
1060 struct saa7164_dev *h, *dev = NULL;
1061 struct saa7164_port *port = NULL;
1062 struct saa7164_port *portc = NULL;
1063 struct saa7164_port *portd = NULL;
Steven Toth96d84202010-07-31 16:04:59 -03001064 struct saa7164_encoder_fh *fh;
Steven Toth7615e432010-07-31 14:44:53 -03001065 struct list_head *list;
1066 int minor = video_devdata(file)->minor;
1067
1068 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1069
1070 /* TODO: Really, the BKL? - remove this */
1071 lock_kernel();
1072 list_for_each(list, &saa7164_devlist) {
1073 h = list_entry(list, struct saa7164_dev, devlist);
1074
1075 portc = &h->ports[ SAA7164_PORT_ENC1 ];
1076 portd = &h->ports[ SAA7164_PORT_ENC2 ];
1077
1078 if (portc->v4l_device &&
1079 portc->v4l_device->minor == minor) {
1080 dev = h;
1081 port = portc;
1082 break;
1083 }
1084
1085 if (portd->v4l_device &&
1086 portd->v4l_device->minor == minor) {
1087 dev = h;
1088 port = portd;
1089 break;
1090 }
1091
1092 }
1093
1094 if (port == NULL) {
1095 unlock_kernel();
1096 return -ENODEV;
1097 }
1098
1099 /* allocate + initialize per filehandle data */
1100 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1101 if (NULL == fh) {
1102 unlock_kernel();
1103 return -ENOMEM;
1104 }
1105
1106 file->private_data = fh;
1107 fh->port = port;
1108
1109 unlock_kernel();
1110
1111 return 0;
1112}
1113
1114static int fops_release(struct file *file)
1115{
Steven Toth96d84202010-07-31 16:04:59 -03001116 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -03001117 struct saa7164_port *port = fh->port;
1118 struct saa7164_dev *dev = port->dev;
1119
1120 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1121
1122 /* Shut device down on last close */
1123 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1124 if (atomic_dec_return(&port->v4l_reader_count) == 0) {
1125 /* stop mpeg capture then cancel buffers */
1126 saa7164_encoder_stop_streaming(port);
1127 }
1128 }
1129
1130 file->private_data = NULL;
1131 kfree(fh);
1132
1133 return 0;
1134}
1135
1136struct saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
1137{
Steven Toth1b0e8e42010-07-31 16:01:00 -03001138 struct saa7164_user_buffer *ubuf = 0;
Steven Toth7615e432010-07-31 14:44:53 -03001139 struct saa7164_dev *dev = port->dev;
Steven Toth12d32032010-07-31 15:13:45 -03001140 u32 crc;
Steven Toth7615e432010-07-31 14:44:53 -03001141
1142 mutex_lock(&port->dmaqueue_lock);
1143 if (!list_empty(&port->list_buf_used.list)) {
Steven Toth1b0e8e42010-07-31 16:01:00 -03001144 ubuf = list_first_entry(&port->list_buf_used.list,
Steven Toth7615e432010-07-31 14:44:53 -03001145 struct saa7164_user_buffer, list);
Steven Toth12d32032010-07-31 15:13:45 -03001146
Steven Toth1b0e8e42010-07-31 16:01:00 -03001147 if (crc_checking) {
1148 crc = crc32(0, ubuf->data, ubuf->actual_size);
1149 if (crc != ubuf->crc) {
1150 printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n", __func__,
1151 ubuf, ubuf->crc, crc);
1152 }
Steven Toth12d32032010-07-31 15:13:45 -03001153 }
1154
Steven Toth7615e432010-07-31 14:44:53 -03001155 }
1156 mutex_unlock(&port->dmaqueue_lock);
1157
Steven Toth1b0e8e42010-07-31 16:01:00 -03001158 dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
Steven Toth7615e432010-07-31 14:44:53 -03001159
Steven Toth1b0e8e42010-07-31 16:01:00 -03001160 return ubuf;
Steven Toth7615e432010-07-31 14:44:53 -03001161}
1162
1163static ssize_t fops_read(struct file *file, char __user *buffer,
1164 size_t count, loff_t *pos)
1165{
Steven Toth96d84202010-07-31 16:04:59 -03001166 struct saa7164_encoder_fh *fh = file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -03001167 struct saa7164_port *port = fh->port;
1168 struct saa7164_user_buffer *ubuf = NULL;
1169 struct saa7164_dev *dev = port->dev;
1170 unsigned int ret = 0;
1171 int rem, cnt;
1172 u8 *p;
1173
Steven Toth58acca12010-07-31 15:10:52 -03001174 port->last_read_msecs_diff = port->last_read_msecs;
1175 port->last_read_msecs = jiffies_to_msecs(jiffies);
1176 port->last_read_msecs_diff = port->last_read_msecs -
1177 port->last_read_msecs_diff;
1178
1179 saa7164_histogram_update(&port->read_interval,
1180 port->last_read_msecs_diff);
1181
Steven Toth46eeb8d2010-07-31 15:11:59 -03001182 if (*pos) {
1183 printk(KERN_ERR "%s() ESPIPE\n", __func__);
Steven Toth7615e432010-07-31 14:44:53 -03001184 return -ESPIPE;
Steven Toth46eeb8d2010-07-31 15:11:59 -03001185 }
Steven Toth7615e432010-07-31 14:44:53 -03001186
1187 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1188 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1189
Steven Toth46eeb8d2010-07-31 15:11:59 -03001190 if (saa7164_encoder_initialize(port) < 0) {
1191 printk(KERN_ERR "%s() EINVAL\n", __func__);
Steven Toth7615e432010-07-31 14:44:53 -03001192 return -EINVAL;
Steven Toth46eeb8d2010-07-31 15:11:59 -03001193 }
Steven Toth7615e432010-07-31 14:44:53 -03001194
1195 saa7164_encoder_start_streaming(port);
1196 msleep(200);
1197 }
1198 }
1199
1200 /* blocking wait for buffer */
1201 if ((file->f_flags & O_NONBLOCK) == 0) {
1202 if (wait_event_interruptible(port->wait_read,
1203 saa7164_enc_next_buf(port))) {
Steven Toth46eeb8d2010-07-31 15:11:59 -03001204 printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
Steven Toth7615e432010-07-31 14:44:53 -03001205 return -ERESTARTSYS;
1206 }
1207 }
1208
1209 /* Pull the first buffer from the used list */
1210 ubuf = saa7164_enc_next_buf(port);
1211
1212 while ((count > 0) && ubuf) {
1213
1214 /* set remaining bytes to copy */
1215 rem = ubuf->actual_size - ubuf->pos;
1216 cnt = rem > count ? count : rem;
1217
1218 p = ubuf->data + ubuf->pos;
1219
1220 dprintk(DBGLVL_ENC,
1221 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1222 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1223
1224 if (copy_to_user(buffer, p, cnt)) {
1225 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
Steven Toth46eeb8d2010-07-31 15:11:59 -03001226 if (!ret) {
1227 printk(KERN_ERR "%s() EFAULT\n", __func__);
Steven Toth7615e432010-07-31 14:44:53 -03001228 ret = -EFAULT;
Steven Toth46eeb8d2010-07-31 15:11:59 -03001229 }
Steven Toth7615e432010-07-31 14:44:53 -03001230 goto err;
1231 }
1232
1233 ubuf->pos += cnt;
1234 count -= cnt;
1235 buffer += cnt;
1236 ret += cnt;
1237
Steven Toth46eeb8d2010-07-31 15:11:59 -03001238 if (ubuf->pos > ubuf->actual_size) {
1239 printk(KERN_ERR "read() pos > actual, huh?\n");
1240 }
1241
Steven Toth7615e432010-07-31 14:44:53 -03001242 if (ubuf->pos == ubuf->actual_size) {
1243
1244 /* finished with current buffer, take next buffer */
1245
1246 /* Requeue the buffer on the free list */
1247 ubuf->pos = 0;
1248
1249 mutex_lock(&port->dmaqueue_lock);
1250 list_move_tail(&ubuf->list, &port->list_buf_free.list);
1251 mutex_unlock(&port->dmaqueue_lock);
1252
1253 /* Dequeue next */
1254 if ((file->f_flags & O_NONBLOCK) == 0) {
1255 if (wait_event_interruptible(port->wait_read,
1256 saa7164_enc_next_buf(port))) {
1257 break;
1258 }
1259 }
1260 ubuf = saa7164_enc_next_buf(port);
1261 }
1262 }
1263err:
Steven Toth46eeb8d2010-07-31 15:11:59 -03001264 if (!ret && !ubuf) {
1265 printk(KERN_ERR "%s() EAGAIN\n", __func__);
Steven Toth7615e432010-07-31 14:44:53 -03001266 ret = -EAGAIN;
Steven Toth46eeb8d2010-07-31 15:11:59 -03001267 }
Steven Toth7615e432010-07-31 14:44:53 -03001268
1269 return ret;
1270}
1271
1272static unsigned int fops_poll(struct file *file, poll_table *wait)
1273{
Steven Toth96d84202010-07-31 16:04:59 -03001274 struct saa7164_encoder_fh *fh = (struct saa7164_encoder_fh *)file->private_data;
Steven Toth7615e432010-07-31 14:44:53 -03001275 struct saa7164_port *port = fh->port;
1276 struct saa7164_user_buffer *ubuf;
1277 unsigned int mask = 0;
1278
Steven Toth58acca12010-07-31 15:10:52 -03001279 port->last_poll_msecs_diff = port->last_poll_msecs;
1280 port->last_poll_msecs = jiffies_to_msecs(jiffies);
1281 port->last_poll_msecs_diff = port->last_poll_msecs -
1282 port->last_poll_msecs_diff;
1283
1284 saa7164_histogram_update(&port->poll_interval,
1285 port->last_poll_msecs_diff);
1286
Steven Toth7615e432010-07-31 14:44:53 -03001287 if (!video_is_registered(port->v4l_device)) {
1288 return -EIO;
1289 }
1290
1291 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1292 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1293 if (saa7164_encoder_initialize(port) < 0)
1294 return -EINVAL;
1295 saa7164_encoder_start_streaming(port);
1296 msleep(200);
1297 }
1298 }
1299
1300 /* blocking wait for buffer */
1301 if ((file->f_flags & O_NONBLOCK) == 0) {
1302 if (wait_event_interruptible(port->wait_read,
1303 saa7164_enc_next_buf(port))) {
1304 return -ERESTARTSYS;
1305 }
1306 }
1307
1308 /* Pull the first buffer from the used list */
1309 ubuf = list_first_entry(&port->list_buf_used.list,
1310 struct saa7164_user_buffer, list);
1311
1312 if (ubuf)
1313 mask |= POLLIN | POLLRDNORM;
1314
1315 return mask;
1316}
1317
1318static const struct v4l2_file_operations mpeg_fops = {
1319 .owner = THIS_MODULE,
1320 .open = fops_open,
1321 .release = fops_release,
1322 .read = fops_read,
1323 .poll = fops_poll,
1324 .unlocked_ioctl = video_ioctl2,
1325};
1326
1327int saa7164_g_chip_ident(struct file *file, void *fh,
1328 struct v4l2_dbg_chip_ident *chip)
1329{
Steven Toth96d84202010-07-31 16:04:59 -03001330 struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
Steven Toth7615e432010-07-31 14:44:53 -03001331 struct saa7164_dev *dev = port->dev;
1332 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1333
1334 return 0;
1335}
1336
1337int saa7164_g_register(struct file *file, void *fh,
1338 struct v4l2_dbg_register *reg)
1339{
Steven Toth96d84202010-07-31 16:04:59 -03001340 struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
Steven Toth7615e432010-07-31 14:44:53 -03001341 struct saa7164_dev *dev = port->dev;
1342 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1343
1344 if (!capable(CAP_SYS_ADMIN))
1345 return -EPERM;
1346
1347 return 0;
1348}
1349
1350int saa7164_s_register(struct file *file, void *fh,
1351 struct v4l2_dbg_register *reg)
1352{
Steven Toth96d84202010-07-31 16:04:59 -03001353 struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
Steven Toth7615e432010-07-31 14:44:53 -03001354 struct saa7164_dev *dev = port->dev;
1355 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1356
1357 if (!capable(CAP_SYS_ADMIN))
1358 return -EPERM;
1359
1360 return 0;
1361}
1362
1363static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1364 .vidioc_s_std = vidioc_s_std,
1365 .vidioc_enum_input = vidioc_enum_input,
1366 .vidioc_g_input = vidioc_g_input,
1367 .vidioc_s_input = vidioc_s_input,
1368 .vidioc_g_tuner = vidioc_g_tuner,
1369 .vidioc_s_tuner = vidioc_s_tuner,
1370 .vidioc_g_frequency = vidioc_g_frequency,
1371 .vidioc_s_frequency = vidioc_s_frequency,
1372 .vidioc_s_ctrl = vidioc_s_ctrl,
1373 .vidioc_g_ctrl = vidioc_g_ctrl,
1374 .vidioc_querycap = vidioc_querycap,
1375 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1376 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1377 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1378 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1379 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1380 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1381 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1382 .vidioc_log_status = vidioc_log_status,
1383 .vidioc_queryctrl = vidioc_queryctrl,
1384 .vidioc_g_chip_ident = saa7164_g_chip_ident,
1385#ifdef CONFIG_VIDEO_ADV_DEBUG
1386 .vidioc_g_register = saa7164_g_register,
1387 .vidioc_s_register = saa7164_s_register,
1388#endif
1389};
1390
1391static struct video_device saa7164_mpeg_template = {
1392 .name = "saa7164",
1393 .fops = &mpeg_fops,
1394 .ioctl_ops = &mpeg_ioctl_ops,
1395 .minor = -1,
1396 .tvnorms = SAA7164_NORMS,
1397 .current_norm = V4L2_STD_NTSC_M,
1398};
1399
1400static struct video_device *saa7164_encoder_alloc(
1401 struct saa7164_port *port,
1402 struct pci_dev *pci,
1403 struct video_device *template,
1404 char *type)
1405{
1406 struct video_device *vfd;
1407 struct saa7164_dev *dev = port->dev;
1408
1409 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1410
1411 vfd = video_device_alloc();
1412 if (NULL == vfd)
1413 return NULL;
1414
1415 *vfd = *template;
1416 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1417 type, saa7164_boards[dev->board].name);
1418
1419 vfd->parent = &pci->dev;
1420 vfd->release = video_device_release;
1421 return vfd;
1422}
1423
1424int saa7164_encoder_register(struct saa7164_port *port)
1425{
1426 struct saa7164_dev *dev = port->dev;
Steven Toth1b0e8e42010-07-31 16:01:00 -03001427 int result = -ENODEV;
Steven Toth7615e432010-07-31 14:44:53 -03001428
1429 dprintk(DBGLVL_ENC, "%s()\n", __func__);
1430
1431 if (port->type != SAA7164_MPEG_ENCODER)
1432 BUG();
1433
1434 /* Sanity check that the PCI configuration space is active */
1435 if (port->hwcfg.BARLocation == 0) {
1436 printk(KERN_ERR "%s() failed "
1437 "(errno = %d), NO PCI configuration\n",
1438 __func__, result);
1439 result = -ENOMEM;
1440 goto failed;
1441 }
1442
Steven Toth7615e432010-07-31 14:44:53 -03001443 /* Establish encoder defaults here */
1444 /* Set default TV standard */
1445 port->encodernorm = saa7164_tvnorms[0];
1446 port->width = 720;
1447 port->mux_input = 1; /* Composite */
Steven Toth7615e432010-07-31 14:44:53 -03001448 port->video_format = EU_VIDEO_FORMAT_MPEG_2;
1449 port->audio_format = 0;
1450 port->video_resolution = 0;
1451 port->ctl_brightness = 127;
1452 port->ctl_contrast = 66;
1453 port->ctl_hue = 128;
1454 port->ctl_saturation = 62;
1455 port->ctl_sharpness = 8;
1456 port->encoder_params.bitrate = ENCODER_DEF_BITRATE;
Steven Toth968b11b2010-07-31 14:59:38 -03001457 port->encoder_params.bitrate_peak = ENCODER_DEF_BITRATE;
Steven Toth5fa56cc2010-07-31 15:05:35 -03001458 port->encoder_params.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
Steven Toth7615e432010-07-31 14:44:53 -03001459 port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1460 port->encoder_params.ctl_mute = 0;
1461 port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
Steven Toth3ed43cf2010-07-31 14:58:35 -03001462 port->encoder_params.refdist = 1;
Steven Toth5fa56cc2010-07-31 15:05:35 -03001463 port->encoder_params.gop_size = SAA7164_ENCODER_DEFAULT_GOP_SIZE;
Steven Toth7615e432010-07-31 14:44:53 -03001464
1465 if (port->encodernorm.id & V4L2_STD_525_60)
1466 port->height = 480;
1467 else
1468 port->height = 576;
1469
1470 /* Allocate and register the video device node */
1471 port->v4l_device = saa7164_encoder_alloc(port,
1472 dev->pci, &saa7164_mpeg_template, "mpeg");
1473
1474 if (port->v4l_device == NULL) {
1475 printk(KERN_INFO "%s: can't allocate mpeg device\n",
1476 dev->name);
1477 result = -ENOMEM;
1478 goto failed;
1479 }
1480
1481 result = video_register_device(port->v4l_device,
1482 VFL_TYPE_GRABBER, -1);
1483 if (result < 0) {
1484 printk(KERN_INFO "%s: can't register mpeg device\n",
1485 dev->name);
1486 /* TODO: We're going to leak here if we don't dealloc
1487 The buffers above. The unreg function can't deal wit it.
1488 */
1489 goto failed;
1490 }
1491
1492 printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
1493 dev->name, port->v4l_device->num);
1494
1495 /* Configure the hardware defaults */
1496 saa7164_api_set_videomux(port);
1497 saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
1498 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
1499 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
1500 saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
1501 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
1502 saa7164_api_audio_mute(port, 0);
1503 saa7164_api_set_audio_volume(port, 20);
1504 saa7164_api_set_aspect_ratio(port);
1505
1506 /* Disable audio standard detection, it's buggy */
1507 saa7164_api_set_audio_detection(port, 0);
1508
1509 saa7164_api_set_encoder(port);
1510 saa7164_api_get_encoder(port);
1511
1512 result = 0;
1513failed:
1514 return result;
1515}
1516
1517void saa7164_encoder_unregister(struct saa7164_port *port)
1518{
1519 struct saa7164_dev *dev = port->dev;
Steven Toth7615e432010-07-31 14:44:53 -03001520
1521 dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1522
1523 if (port->type != SAA7164_MPEG_ENCODER)
1524 BUG();
1525
1526 if (port->v4l_device) {
1527 if (port->v4l_device->minor != -1)
1528 video_unregister_device(port->v4l_device);
1529 else
1530 video_device_release(port->v4l_device);
1531
1532 port->v4l_device = NULL;
1533 }
1534
Steven Toth7615e432010-07-31 14:44:53 -03001535 dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
1536}
1537