2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
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.
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
15 * GNU General Public License for more details.
20 #define ENCODER_MAX_BITRATE 6500000
21 #define ENCODER_MIN_BITRATE 1000000
22 #define ENCODER_DEF_BITRATE 5000000
25 * This is a dummy non-zero value for the sizeimage field of v4l2_pix_format.
26 * It is not actually used for anything since this driver does not support
27 * stream I/O, only read(), and because this driver produces an MPEG stream
28 * and not discrete frames. But the V4L2 spec doesn't allow for this value
29 * to be 0, so set it to 0x10000 instead.
31 * If we ever change this driver to support stream I/O, then this field
32 * will be the size of the streaming buffers.
34 #define SAA7164_SIZEIMAGE (0x10000)
36 static struct saa7164_tvnorm saa7164_tvnorms
[] = {
39 .id
= V4L2_STD_NTSC_M
,
42 .id
= V4L2_STD_NTSC_M_JP
,
46 /* Take the encoder configuration form the port struct and
47 * flush it to the hardware.
49 static void saa7164_encoder_configure(struct saa7164_port
*port
)
51 struct saa7164_dev
*dev
= port
->dev
;
52 dprintk(DBGLVL_ENC
, "%s()\n", __func__
);
54 port
->encoder_params
.width
= port
->width
;
55 port
->encoder_params
.height
= port
->height
;
56 port
->encoder_params
.is_50hz
=
57 (port
->encodernorm
.id
& V4L2_STD_625_50
) != 0;
59 /* Set up the DIF (enable it) for analog mode by default */
60 saa7164_api_initialize_dif(port
);
62 /* Configure the correct video standard */
63 saa7164_api_configure_dif(port
, port
->encodernorm
.id
);
65 /* Ensure the audio decoder is correct configured */
66 saa7164_api_set_audio_std(port
);
69 static int saa7164_encoder_buffers_dealloc(struct saa7164_port
*port
)
71 struct list_head
*c
, *n
, *p
, *q
, *l
, *v
;
72 struct saa7164_dev
*dev
= port
->dev
;
73 struct saa7164_buffer
*buf
;
74 struct saa7164_user_buffer
*ubuf
;
76 /* Remove any allocated buffers */
77 mutex_lock(&port
->dmaqueue_lock
);
79 dprintk(DBGLVL_ENC
, "%s(port=%d) dmaqueue\n", __func__
, port
->nr
);
80 list_for_each_safe(c
, n
, &port
->dmaqueue
.list
) {
81 buf
= list_entry(c
, struct saa7164_buffer
, list
);
83 saa7164_buffer_dealloc(buf
);
86 dprintk(DBGLVL_ENC
, "%s(port=%d) used\n", __func__
, port
->nr
);
87 list_for_each_safe(p
, q
, &port
->list_buf_used
.list
) {
88 ubuf
= list_entry(p
, struct saa7164_user_buffer
, list
);
90 saa7164_buffer_dealloc_user(ubuf
);
93 dprintk(DBGLVL_ENC
, "%s(port=%d) free\n", __func__
, port
->nr
);
94 list_for_each_safe(l
, v
, &port
->list_buf_free
.list
) {
95 ubuf
= list_entry(l
, struct saa7164_user_buffer
, list
);
97 saa7164_buffer_dealloc_user(ubuf
);
100 mutex_unlock(&port
->dmaqueue_lock
);
101 dprintk(DBGLVL_ENC
, "%s(port=%d) done\n", __func__
, port
->nr
);
106 /* Dynamic buffer switch at encoder start time */
107 static int saa7164_encoder_buffers_alloc(struct saa7164_port
*port
)
109 struct saa7164_dev
*dev
= port
->dev
;
110 struct saa7164_buffer
*buf
;
111 struct saa7164_user_buffer
*ubuf
;
112 struct tmHWStreamParameters
*params
= &port
->hw_streamingparams
;
113 int result
= -ENODEV
, i
;
116 dprintk(DBGLVL_ENC
, "%s()\n", __func__
);
118 if (port
->encoder_params
.stream_type
==
119 V4L2_MPEG_STREAM_TYPE_MPEG2_PS
) {
121 "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n",
123 params
->samplesperline
= 128;
124 params
->numberoflines
= 256;
126 params
->numpagetables
= 2 +
127 ((SAA7164_PS_NUMBER_OF_LINES
* 128) / PAGE_SIZE
);
129 if (port
->encoder_params
.stream_type
==
130 V4L2_MPEG_STREAM_TYPE_MPEG2_TS
) {
132 "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n",
134 params
->samplesperline
= 188;
135 params
->numberoflines
= 312;
137 params
->numpagetables
= 2 +
138 ((SAA7164_TS_NUMBER_OF_LINES
* 188) / PAGE_SIZE
);
142 /* Init and establish defaults */
143 params
->bitspersample
= 8;
144 params
->linethreshold
= 0;
145 params
->pagetablelistvirt
= NULL
;
146 params
->pagetablelistphys
= NULL
;
147 params
->numpagetableentries
= port
->hwcfg
.buffercount
;
149 /* Allocate the PCI resources, buffers (hard) */
150 for (i
= 0; i
< port
->hwcfg
.buffercount
; i
++) {
151 buf
= saa7164_buffer_alloc(port
,
152 params
->numberoflines
*
156 printk(KERN_ERR
"%s() failed (errno = %d), unable to allocate buffer\n",
162 mutex_lock(&port
->dmaqueue_lock
);
163 list_add_tail(&buf
->list
, &port
->dmaqueue
.list
);
164 mutex_unlock(&port
->dmaqueue_lock
);
169 /* Allocate some kernel buffers for copying
172 len
= params
->numberoflines
* params
->pitch
;
174 if (encoder_buffers
< 16)
175 encoder_buffers
= 16;
176 if (encoder_buffers
> 512)
177 encoder_buffers
= 512;
179 for (i
= 0; i
< encoder_buffers
; i
++) {
181 ubuf
= saa7164_buffer_alloc_user(dev
, len
);
183 mutex_lock(&port
->dmaqueue_lock
);
184 list_add_tail(&ubuf
->list
, &port
->list_buf_free
.list
);
185 mutex_unlock(&port
->dmaqueue_lock
);
196 static int saa7164_encoder_initialize(struct saa7164_port
*port
)
198 saa7164_encoder_configure(port
);
202 /* -- V4L2 --------------------------------------------------------- */
203 int saa7164_s_std(struct saa7164_port
*port
, v4l2_std_id id
)
205 struct saa7164_dev
*dev
= port
->dev
;
208 dprintk(DBGLVL_ENC
, "%s(id=0x%x)\n", __func__
, (u32
)id
);
210 for (i
= 0; i
< ARRAY_SIZE(saa7164_tvnorms
); i
++) {
211 if (id
& saa7164_tvnorms
[i
].id
)
214 if (i
== ARRAY_SIZE(saa7164_tvnorms
))
217 port
->encodernorm
= saa7164_tvnorms
[i
];
220 /* Update the audio decoder while is not running in
223 saa7164_api_set_audio_std(port
);
225 dprintk(DBGLVL_ENC
, "%s(id=0x%x) OK\n", __func__
, (u32
)id
);
230 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
232 struct saa7164_encoder_fh
*fh
= file
->private_data
;
234 return saa7164_s_std(fh
->port
, id
);
237 int saa7164_g_std(struct saa7164_port
*port
, v4l2_std_id
*id
)
243 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
245 struct saa7164_encoder_fh
*fh
= file
->private_data
;
247 return saa7164_g_std(fh
->port
, id
);
250 int saa7164_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*i
)
252 static const char * const inputs
[] = {
253 "tuner", "composite", "svideo", "aux",
254 "composite 2", "svideo 2", "aux 2"
261 strcpy(i
->name
, inputs
[i
->index
]);
264 i
->type
= V4L2_INPUT_TYPE_TUNER
;
266 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
268 for (n
= 0; n
< ARRAY_SIZE(saa7164_tvnorms
); n
++)
269 i
->std
|= saa7164_tvnorms
[n
].id
;
274 int saa7164_g_input(struct saa7164_port
*port
, unsigned int *i
)
276 struct saa7164_dev
*dev
= port
->dev
;
278 if (saa7164_api_get_videomux(port
) != SAA_OK
)
281 *i
= (port
->mux_input
- 1);
283 dprintk(DBGLVL_ENC
, "%s() input=%d\n", __func__
, *i
);
288 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
290 struct saa7164_encoder_fh
*fh
= file
->private_data
;
292 return saa7164_g_input(fh
->port
, i
);
295 int saa7164_s_input(struct saa7164_port
*port
, unsigned int i
)
297 struct saa7164_dev
*dev
= port
->dev
;
299 dprintk(DBGLVL_ENC
, "%s() input=%d\n", __func__
, i
);
304 port
->mux_input
= i
+ 1;
306 if (saa7164_api_set_videomux(port
) != SAA_OK
)
312 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
314 struct saa7164_encoder_fh
*fh
= file
->private_data
;
316 return saa7164_s_input(fh
->port
, i
);
319 int saa7164_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
321 struct saa7164_encoder_fh
*fh
= file
->private_data
;
322 struct saa7164_port
*port
= fh
->port
;
323 struct saa7164_dev
*dev
= port
->dev
;
328 strcpy(t
->name
, "tuner");
329 t
->capability
= V4L2_TUNER_CAP_NORM
| V4L2_TUNER_CAP_STEREO
;
330 t
->rangelow
= SAA7164_TV_MIN_FREQ
;
331 t
->rangehigh
= SAA7164_TV_MAX_FREQ
;
333 dprintk(DBGLVL_ENC
, "VIDIOC_G_TUNER: tuner type %d\n", t
->type
);
338 int saa7164_s_tuner(struct file
*file
, void *priv
,
339 const struct v4l2_tuner
*t
)
344 /* Update the A/V core */
348 int saa7164_g_frequency(struct saa7164_port
*port
, struct v4l2_frequency
*f
)
353 f
->frequency
= port
->freq
;
357 static int vidioc_g_frequency(struct file
*file
, void *priv
,
358 struct v4l2_frequency
*f
)
360 struct saa7164_encoder_fh
*fh
= file
->private_data
;
362 return saa7164_g_frequency(fh
->port
, f
);
365 int saa7164_s_frequency(struct saa7164_port
*port
,
366 const struct v4l2_frequency
*f
)
368 struct saa7164_dev
*dev
= port
->dev
;
369 struct saa7164_port
*tsport
;
370 struct dvb_frontend
*fe
;
372 /* TODO: Pull this for the std */
373 struct analog_parameters params
= {
374 .mode
= V4L2_TUNER_ANALOG_TV
,
375 .audmode
= V4L2_TUNER_MODE_STEREO
,
376 .std
= port
->encodernorm
.id
,
377 .frequency
= f
->frequency
380 /* Stop the encoder */
381 dprintk(DBGLVL_ENC
, "%s() frequency=%d tuner=%d\n", __func__
,
382 f
->frequency
, f
->tuner
);
387 port
->freq
= clamp(f
->frequency
,
388 SAA7164_TV_MIN_FREQ
, SAA7164_TV_MAX_FREQ
);
390 /* Update the hardware */
391 if (port
->nr
== SAA7164_PORT_ENC1
)
392 tsport
= &dev
->ports
[SAA7164_PORT_TS1
];
393 else if (port
->nr
== SAA7164_PORT_ENC2
)
394 tsport
= &dev
->ports
[SAA7164_PORT_TS2
];
398 fe
= tsport
->dvb
.frontend
;
400 if (fe
&& fe
->ops
.tuner_ops
.set_analog_params
)
401 fe
->ops
.tuner_ops
.set_analog_params(fe
, ¶ms
);
403 printk(KERN_ERR
"%s() No analog tuner, aborting\n", __func__
);
405 saa7164_encoder_initialize(port
);
410 static int vidioc_s_frequency(struct file
*file
, void *priv
,
411 const struct v4l2_frequency
*f
)
413 struct saa7164_encoder_fh
*fh
= file
->private_data
;
415 return saa7164_s_frequency(fh
->port
, f
);
418 static int saa7164_s_ctrl(struct v4l2_ctrl
*ctrl
)
420 struct saa7164_port
*port
=
421 container_of(ctrl
->handler
, struct saa7164_port
, ctrl_handler
);
422 struct saa7164_encoder_params
*params
= &port
->encoder_params
;
426 case V4L2_CID_BRIGHTNESS
:
427 port
->ctl_brightness
= ctrl
->val
;
428 saa7164_api_set_usercontrol(port
, PU_BRIGHTNESS_CONTROL
);
430 case V4L2_CID_CONTRAST
:
431 port
->ctl_contrast
= ctrl
->val
;
432 saa7164_api_set_usercontrol(port
, PU_CONTRAST_CONTROL
);
434 case V4L2_CID_SATURATION
:
435 port
->ctl_saturation
= ctrl
->val
;
436 saa7164_api_set_usercontrol(port
, PU_SATURATION_CONTROL
);
439 port
->ctl_hue
= ctrl
->val
;
440 saa7164_api_set_usercontrol(port
, PU_HUE_CONTROL
);
442 case V4L2_CID_SHARPNESS
:
443 port
->ctl_sharpness
= ctrl
->val
;
444 saa7164_api_set_usercontrol(port
, PU_SHARPNESS_CONTROL
);
446 case V4L2_CID_AUDIO_VOLUME
:
447 port
->ctl_volume
= ctrl
->val
;
448 saa7164_api_set_audio_volume(port
, port
->ctl_volume
);
450 case V4L2_CID_MPEG_VIDEO_BITRATE
:
451 params
->bitrate
= ctrl
->val
;
453 case V4L2_CID_MPEG_STREAM_TYPE
:
454 params
->stream_type
= ctrl
->val
;
456 case V4L2_CID_MPEG_AUDIO_MUTE
:
457 params
->ctl_mute
= ctrl
->val
;
458 ret
= saa7164_api_audio_mute(port
, params
->ctl_mute
);
460 printk(KERN_ERR
"%s() error, ret = 0x%x\n", __func__
,
465 case V4L2_CID_MPEG_VIDEO_ASPECT
:
466 params
->ctl_aspect
= ctrl
->val
;
467 ret
= saa7164_api_set_aspect_ratio(port
);
469 printk(KERN_ERR
"%s() error, ret = 0x%x\n", __func__
,
474 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
475 params
->bitrate_mode
= ctrl
->val
;
477 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
478 params
->refdist
= ctrl
->val
;
480 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
481 params
->bitrate_peak
= ctrl
->val
;
483 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
484 params
->gop_size
= ctrl
->val
;
493 static int vidioc_querycap(struct file
*file
, void *priv
,
494 struct v4l2_capability
*cap
)
496 struct saa7164_encoder_fh
*fh
= file
->private_data
;
497 struct saa7164_port
*port
= fh
->port
;
498 struct saa7164_dev
*dev
= port
->dev
;
500 strcpy(cap
->driver
, dev
->name
);
501 strlcpy(cap
->card
, saa7164_boards
[dev
->board
].name
,
503 sprintf(cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
506 V4L2_CAP_VIDEO_CAPTURE
|
510 cap
->capabilities
= cap
->device_caps
|
511 V4L2_CAP_VBI_CAPTURE
|
512 V4L2_CAP_DEVICE_CAPS
;
517 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
518 struct v4l2_fmtdesc
*f
)
523 strlcpy(f
->description
, "MPEG", sizeof(f
->description
));
524 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
529 static int vidioc_fmt_vid_cap(struct file
*file
, void *priv
,
530 struct v4l2_format
*f
)
532 struct saa7164_encoder_fh
*fh
= file
->private_data
;
533 struct saa7164_port
*port
= fh
->port
;
535 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
536 f
->fmt
.pix
.bytesperline
= 0;
537 f
->fmt
.pix
.sizeimage
= SAA7164_SIZEIMAGE
;
538 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
539 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
540 f
->fmt
.pix
.width
= port
->width
;
541 f
->fmt
.pix
.height
= port
->height
;
545 static int saa7164_encoder_stop_port(struct saa7164_port
*port
)
547 struct saa7164_dev
*dev
= port
->dev
;
550 ret
= saa7164_api_transition_port(port
, SAA_DMASTATE_STOP
);
551 if ((ret
!= SAA_OK
) && (ret
!= SAA_ERR_ALREADY_STOPPED
)) {
552 printk(KERN_ERR
"%s() stop transition failed, ret = 0x%x\n",
556 dprintk(DBGLVL_ENC
, "%s() Stopped\n", __func__
);
563 static int saa7164_encoder_acquire_port(struct saa7164_port
*port
)
565 struct saa7164_dev
*dev
= port
->dev
;
568 ret
= saa7164_api_transition_port(port
, SAA_DMASTATE_ACQUIRE
);
569 if ((ret
!= SAA_OK
) && (ret
!= SAA_ERR_ALREADY_STOPPED
)) {
570 printk(KERN_ERR
"%s() acquire transition failed, ret = 0x%x\n",
574 dprintk(DBGLVL_ENC
, "%s() Acquired\n", __func__
);
581 static int saa7164_encoder_pause_port(struct saa7164_port
*port
)
583 struct saa7164_dev
*dev
= port
->dev
;
586 ret
= saa7164_api_transition_port(port
, SAA_DMASTATE_PAUSE
);
587 if ((ret
!= SAA_OK
) && (ret
!= SAA_ERR_ALREADY_STOPPED
)) {
588 printk(KERN_ERR
"%s() pause transition failed, ret = 0x%x\n",
592 dprintk(DBGLVL_ENC
, "%s() Paused\n", __func__
);
599 /* Firmware is very windows centric, meaning you have to transition
600 * the part through AVStream / KS Windows stages, forwards or backwards.
601 * States are: stopped, acquired (h/w), paused, started.
602 * We have to leave here will all of the soft buffers on the free list,
603 * else the cfg_post() func won't have soft buffers to correctly configure.
605 static int saa7164_encoder_stop_streaming(struct saa7164_port
*port
)
607 struct saa7164_dev
*dev
= port
->dev
;
608 struct saa7164_buffer
*buf
;
609 struct saa7164_user_buffer
*ubuf
;
610 struct list_head
*c
, *n
;
613 dprintk(DBGLVL_ENC
, "%s(port=%d)\n", __func__
, port
->nr
);
615 ret
= saa7164_encoder_pause_port(port
);
616 ret
= saa7164_encoder_acquire_port(port
);
617 ret
= saa7164_encoder_stop_port(port
);
619 dprintk(DBGLVL_ENC
, "%s(port=%d) Hardware stopped\n", __func__
,
622 /* Reset the state of any allocated buffer resources */
623 mutex_lock(&port
->dmaqueue_lock
);
625 /* Reset the hard and soft buffer state */
626 list_for_each_safe(c
, n
, &port
->dmaqueue
.list
) {
627 buf
= list_entry(c
, struct saa7164_buffer
, list
);
628 buf
->flags
= SAA7164_BUFFER_FREE
;
632 list_for_each_safe(c
, n
, &port
->list_buf_used
.list
) {
633 ubuf
= list_entry(c
, struct saa7164_user_buffer
, list
);
635 list_move_tail(&ubuf
->list
, &port
->list_buf_free
.list
);
638 mutex_unlock(&port
->dmaqueue_lock
);
640 /* Free any allocated resources */
641 saa7164_encoder_buffers_dealloc(port
);
643 dprintk(DBGLVL_ENC
, "%s(port=%d) Released\n", __func__
, port
->nr
);
648 static int saa7164_encoder_start_streaming(struct saa7164_port
*port
)
650 struct saa7164_dev
*dev
= port
->dev
;
653 dprintk(DBGLVL_ENC
, "%s(port=%d)\n", __func__
, port
->nr
);
655 port
->done_first_interrupt
= 0;
657 /* allocate all of the PCIe DMA buffer resources on the fly,
658 * allowing switching between TS and PS payloads without
659 * requiring a complete driver reload.
661 saa7164_encoder_buffers_alloc(port
);
663 /* Configure the encoder with any cache values */
664 saa7164_api_set_encoder(port
);
665 saa7164_api_get_encoder(port
);
667 /* Place the empty buffers on the hardware */
668 saa7164_buffer_cfg_port(port
);
670 /* Acquire the hardware */
671 result
= saa7164_api_transition_port(port
, SAA_DMASTATE_ACQUIRE
);
672 if ((result
!= SAA_OK
) && (result
!= SAA_ERR_ALREADY_STOPPED
)) {
673 printk(KERN_ERR
"%s() acquire transition failed, res = 0x%x\n",
676 /* Stop the hardware, regardless */
677 result
= saa7164_api_transition_port(port
, SAA_DMASTATE_STOP
);
678 if ((result
!= SAA_OK
) && (result
!= SAA_ERR_ALREADY_STOPPED
)) {
679 printk(KERN_ERR
"%s() acquire/forced stop transition failed, res = 0x%x\n",
685 dprintk(DBGLVL_ENC
, "%s() Acquired\n", __func__
);
687 /* Pause the hardware */
688 result
= saa7164_api_transition_port(port
, SAA_DMASTATE_PAUSE
);
689 if ((result
!= SAA_OK
) && (result
!= SAA_ERR_ALREADY_STOPPED
)) {
690 printk(KERN_ERR
"%s() pause transition failed, res = 0x%x\n",
693 /* Stop the hardware, regardless */
694 result
= saa7164_api_transition_port(port
, SAA_DMASTATE_STOP
);
695 if ((result
!= SAA_OK
) && (result
!= SAA_ERR_ALREADY_STOPPED
)) {
696 printk(KERN_ERR
"%s() pause/forced stop transition failed, res = 0x%x\n",
703 dprintk(DBGLVL_ENC
, "%s() Paused\n", __func__
);
705 /* Start the hardware */
706 result
= saa7164_api_transition_port(port
, SAA_DMASTATE_RUN
);
707 if ((result
!= SAA_OK
) && (result
!= SAA_ERR_ALREADY_STOPPED
)) {
708 printk(KERN_ERR
"%s() run transition failed, result = 0x%x\n",
711 /* Stop the hardware, regardless */
712 result
= saa7164_api_transition_port(port
, SAA_DMASTATE_STOP
);
713 if ((result
!= SAA_OK
) && (result
!= SAA_ERR_ALREADY_STOPPED
)) {
714 printk(KERN_ERR
"%s() run/forced stop transition failed, res = 0x%x\n",
720 dprintk(DBGLVL_ENC
, "%s() Running\n", __func__
);
726 static int fops_open(struct file
*file
)
728 struct saa7164_dev
*dev
;
729 struct saa7164_port
*port
;
730 struct saa7164_encoder_fh
*fh
;
732 port
= (struct saa7164_port
*)video_get_drvdata(video_devdata(file
));
738 dprintk(DBGLVL_ENC
, "%s()\n", __func__
);
740 /* allocate + initialize per filehandle data */
741 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
746 v4l2_fh_init(&fh
->fh
, video_devdata(file
));
747 v4l2_fh_add(&fh
->fh
);
748 file
->private_data
= fh
;
753 static int fops_release(struct file
*file
)
755 struct saa7164_encoder_fh
*fh
= file
->private_data
;
756 struct saa7164_port
*port
= fh
->port
;
757 struct saa7164_dev
*dev
= port
->dev
;
759 dprintk(DBGLVL_ENC
, "%s()\n", __func__
);
761 /* Shut device down on last close */
762 if (atomic_cmpxchg(&fh
->v4l_reading
, 1, 0) == 1) {
763 if (atomic_dec_return(&port
->v4l_reader_count
) == 0) {
764 /* stop mpeg capture then cancel buffers */
765 saa7164_encoder_stop_streaming(port
);
769 v4l2_fh_del(&fh
->fh
);
770 v4l2_fh_exit(&fh
->fh
);
777 saa7164_user_buffer
*saa7164_enc_next_buf(struct saa7164_port
*port
)
779 struct saa7164_user_buffer
*ubuf
= NULL
;
780 struct saa7164_dev
*dev
= port
->dev
;
783 mutex_lock(&port
->dmaqueue_lock
);
784 if (!list_empty(&port
->list_buf_used
.list
)) {
785 ubuf
= list_first_entry(&port
->list_buf_used
.list
,
786 struct saa7164_user_buffer
, list
);
789 crc
= crc32(0, ubuf
->data
, ubuf
->actual_size
);
790 if (crc
!= ubuf
->crc
) {
792 "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
794 ubuf
, ubuf
->crc
, crc
);
799 mutex_unlock(&port
->dmaqueue_lock
);
801 dprintk(DBGLVL_ENC
, "%s() returns %p\n", __func__
, ubuf
);
806 static ssize_t
fops_read(struct file
*file
, char __user
*buffer
,
807 size_t count
, loff_t
*pos
)
809 struct saa7164_encoder_fh
*fh
= file
->private_data
;
810 struct saa7164_port
*port
= fh
->port
;
811 struct saa7164_user_buffer
*ubuf
= NULL
;
812 struct saa7164_dev
*dev
= port
->dev
;
817 port
->last_read_msecs_diff
= port
->last_read_msecs
;
818 port
->last_read_msecs
= jiffies_to_msecs(jiffies
);
819 port
->last_read_msecs_diff
= port
->last_read_msecs
-
820 port
->last_read_msecs_diff
;
822 saa7164_histogram_update(&port
->read_interval
,
823 port
->last_read_msecs_diff
);
826 printk(KERN_ERR
"%s() ESPIPE\n", __func__
);
830 if (atomic_cmpxchg(&fh
->v4l_reading
, 0, 1) == 0) {
831 if (atomic_inc_return(&port
->v4l_reader_count
) == 1) {
833 if (saa7164_encoder_initialize(port
) < 0) {
834 printk(KERN_ERR
"%s() EINVAL\n", __func__
);
838 saa7164_encoder_start_streaming(port
);
843 /* blocking wait for buffer */
844 if ((file
->f_flags
& O_NONBLOCK
) == 0) {
845 if (wait_event_interruptible(port
->wait_read
,
846 saa7164_enc_next_buf(port
))) {
847 printk(KERN_ERR
"%s() ERESTARTSYS\n", __func__
);
852 /* Pull the first buffer from the used list */
853 ubuf
= saa7164_enc_next_buf(port
);
855 while ((count
> 0) && ubuf
) {
857 /* set remaining bytes to copy */
858 rem
= ubuf
->actual_size
- ubuf
->pos
;
859 cnt
= rem
> count
? count
: rem
;
861 p
= ubuf
->data
+ ubuf
->pos
;
864 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
865 __func__
, (int)count
, cnt
, rem
, ubuf
, ubuf
->pos
);
867 if (copy_to_user(buffer
, p
, cnt
)) {
868 printk(KERN_ERR
"%s() copy_to_user failed\n", __func__
);
870 printk(KERN_ERR
"%s() EFAULT\n", __func__
);
881 if (ubuf
->pos
> ubuf
->actual_size
)
882 printk(KERN_ERR
"read() pos > actual, huh?\n");
884 if (ubuf
->pos
== ubuf
->actual_size
) {
886 /* finished with current buffer, take next buffer */
888 /* Requeue the buffer on the free list */
891 mutex_lock(&port
->dmaqueue_lock
);
892 list_move_tail(&ubuf
->list
, &port
->list_buf_free
.list
);
893 mutex_unlock(&port
->dmaqueue_lock
);
896 if ((file
->f_flags
& O_NONBLOCK
) == 0) {
897 if (wait_event_interruptible(port
->wait_read
,
898 saa7164_enc_next_buf(port
))) {
902 ubuf
= saa7164_enc_next_buf(port
);
912 static __poll_t
fops_poll(struct file
*file
, poll_table
*wait
)
914 __poll_t req_events
= poll_requested_events(wait
);
915 struct saa7164_encoder_fh
*fh
=
916 (struct saa7164_encoder_fh
*)file
->private_data
;
917 struct saa7164_port
*port
= fh
->port
;
918 __poll_t mask
= v4l2_ctrl_poll(file
, wait
);
920 port
->last_poll_msecs_diff
= port
->last_poll_msecs
;
921 port
->last_poll_msecs
= jiffies_to_msecs(jiffies
);
922 port
->last_poll_msecs_diff
= port
->last_poll_msecs
-
923 port
->last_poll_msecs_diff
;
925 saa7164_histogram_update(&port
->poll_interval
,
926 port
->last_poll_msecs_diff
);
928 if (!(req_events
& (EPOLLIN
| EPOLLRDNORM
)))
931 if (atomic_cmpxchg(&fh
->v4l_reading
, 0, 1) == 0) {
932 if (atomic_inc_return(&port
->v4l_reader_count
) == 1) {
933 if (saa7164_encoder_initialize(port
) < 0)
934 return mask
| EPOLLERR
;
935 saa7164_encoder_start_streaming(port
);
940 /* Pull the first buffer from the used list */
941 if (!list_empty(&port
->list_buf_used
.list
))
942 mask
|= EPOLLIN
| EPOLLRDNORM
;
947 static const struct v4l2_ctrl_ops saa7164_ctrl_ops
= {
948 .s_ctrl
= saa7164_s_ctrl
,
951 static const struct v4l2_file_operations mpeg_fops
= {
952 .owner
= THIS_MODULE
,
954 .release
= fops_release
,
957 .unlocked_ioctl
= video_ioctl2
,
960 static const struct v4l2_ioctl_ops mpeg_ioctl_ops
= {
961 .vidioc_s_std
= vidioc_s_std
,
962 .vidioc_g_std
= vidioc_g_std
,
963 .vidioc_enum_input
= saa7164_enum_input
,
964 .vidioc_g_input
= vidioc_g_input
,
965 .vidioc_s_input
= vidioc_s_input
,
966 .vidioc_g_tuner
= saa7164_g_tuner
,
967 .vidioc_s_tuner
= saa7164_s_tuner
,
968 .vidioc_g_frequency
= vidioc_g_frequency
,
969 .vidioc_s_frequency
= vidioc_s_frequency
,
970 .vidioc_querycap
= vidioc_querycap
,
971 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
972 .vidioc_g_fmt_vid_cap
= vidioc_fmt_vid_cap
,
973 .vidioc_try_fmt_vid_cap
= vidioc_fmt_vid_cap
,
974 .vidioc_s_fmt_vid_cap
= vidioc_fmt_vid_cap
,
975 .vidioc_log_status
= v4l2_ctrl_log_status
,
976 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
977 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
980 static struct video_device saa7164_mpeg_template
= {
983 .ioctl_ops
= &mpeg_ioctl_ops
,
985 .tvnorms
= SAA7164_NORMS
,
988 static struct video_device
*saa7164_encoder_alloc(
989 struct saa7164_port
*port
,
991 struct video_device
*template,
994 struct video_device
*vfd
;
995 struct saa7164_dev
*dev
= port
->dev
;
997 dprintk(DBGLVL_ENC
, "%s()\n", __func__
);
999 vfd
= video_device_alloc();
1004 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)", dev
->name
,
1005 type
, saa7164_boards
[dev
->board
].name
);
1007 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1008 vfd
->release
= video_device_release
;
1012 int saa7164_encoder_register(struct saa7164_port
*port
)
1014 struct saa7164_dev
*dev
= port
->dev
;
1015 struct v4l2_ctrl_handler
*hdl
= &port
->ctrl_handler
;
1016 int result
= -ENODEV
;
1018 dprintk(DBGLVL_ENC
, "%s()\n", __func__
);
1020 BUG_ON(port
->type
!= SAA7164_MPEG_ENCODER
);
1022 /* Sanity check that the PCI configuration space is active */
1023 if (port
->hwcfg
.BARLocation
== 0) {
1024 printk(KERN_ERR
"%s() failed (errno = %d), NO PCI configuration\n",
1030 /* Establish encoder defaults here */
1031 /* Set default TV standard */
1032 port
->encodernorm
= saa7164_tvnorms
[0];
1034 port
->mux_input
= 1; /* Composite */
1035 port
->video_format
= EU_VIDEO_FORMAT_MPEG_2
;
1036 port
->audio_format
= 0;
1037 port
->video_resolution
= 0;
1038 port
->freq
= SAA7164_TV_MIN_FREQ
;
1040 v4l2_ctrl_handler_init(hdl
, 14);
1041 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1042 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 127);
1043 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1044 V4L2_CID_CONTRAST
, 0, 255, 1, 66);
1045 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1046 V4L2_CID_SATURATION
, 0, 255, 1, 62);
1047 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1048 V4L2_CID_HUE
, 0, 255, 1, 128);
1049 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1050 V4L2_CID_SHARPNESS
, 0x0, 0x0f, 1, 8);
1051 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1052 V4L2_CID_MPEG_AUDIO_MUTE
, 0x0, 0x01, 1, 0);
1053 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1054 V4L2_CID_AUDIO_VOLUME
, -83, 24, 1, 20);
1055 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1056 V4L2_CID_MPEG_VIDEO_BITRATE
,
1057 ENCODER_MIN_BITRATE
, ENCODER_MAX_BITRATE
,
1058 100000, ENCODER_DEF_BITRATE
);
1059 v4l2_ctrl_new_std_menu(hdl
, &saa7164_ctrl_ops
,
1060 V4L2_CID_MPEG_STREAM_TYPE
,
1061 V4L2_MPEG_STREAM_TYPE_MPEG2_TS
, 0,
1062 V4L2_MPEG_STREAM_TYPE_MPEG2_PS
);
1063 v4l2_ctrl_new_std_menu(hdl
, &saa7164_ctrl_ops
,
1064 V4L2_CID_MPEG_VIDEO_ASPECT
,
1065 V4L2_MPEG_VIDEO_ASPECT_221x100
, 0,
1066 V4L2_MPEG_VIDEO_ASPECT_4x3
);
1067 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1068 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 1, 255, 1, 15);
1069 v4l2_ctrl_new_std_menu(hdl
, &saa7164_ctrl_ops
,
1070 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
1071 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
, 0,
1072 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
);
1073 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1074 V4L2_CID_MPEG_VIDEO_B_FRAMES
, 1, 3, 1, 1);
1075 v4l2_ctrl_new_std(hdl
, &saa7164_ctrl_ops
,
1076 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
1077 ENCODER_MIN_BITRATE
, ENCODER_MAX_BITRATE
,
1078 100000, ENCODER_DEF_BITRATE
);
1080 result
= hdl
->error
;
1084 port
->std
= V4L2_STD_NTSC_M
;
1086 if (port
->encodernorm
.id
& V4L2_STD_525_60
)
1091 /* Allocate and register the video device node */
1092 port
->v4l_device
= saa7164_encoder_alloc(port
,
1093 dev
->pci
, &saa7164_mpeg_template
, "mpeg");
1095 if (!port
->v4l_device
) {
1096 printk(KERN_INFO
"%s: can't allocate mpeg device\n",
1102 port
->v4l_device
->ctrl_handler
= hdl
;
1103 v4l2_ctrl_handler_setup(hdl
);
1104 video_set_drvdata(port
->v4l_device
, port
);
1105 result
= video_register_device(port
->v4l_device
,
1106 VFL_TYPE_GRABBER
, -1);
1108 printk(KERN_INFO
"%s: can't register mpeg device\n",
1110 /* TODO: We're going to leak here if we don't dealloc
1111 The buffers above. The unreg function can't deal wit it.
1116 printk(KERN_INFO
"%s: registered device video%d [mpeg]\n",
1117 dev
->name
, port
->v4l_device
->num
);
1119 /* Configure the hardware defaults */
1120 saa7164_api_set_videomux(port
);
1121 saa7164_api_set_usercontrol(port
, PU_BRIGHTNESS_CONTROL
);
1122 saa7164_api_set_usercontrol(port
, PU_CONTRAST_CONTROL
);
1123 saa7164_api_set_usercontrol(port
, PU_HUE_CONTROL
);
1124 saa7164_api_set_usercontrol(port
, PU_SATURATION_CONTROL
);
1125 saa7164_api_set_usercontrol(port
, PU_SHARPNESS_CONTROL
);
1126 saa7164_api_audio_mute(port
, 0);
1127 saa7164_api_set_audio_volume(port
, 20);
1128 saa7164_api_set_aspect_ratio(port
);
1130 /* Disable audio standard detection, it's buggy */
1131 saa7164_api_set_audio_detection(port
, 0);
1133 saa7164_api_set_encoder(port
);
1134 saa7164_api_get_encoder(port
);
1141 void saa7164_encoder_unregister(struct saa7164_port
*port
)
1143 struct saa7164_dev
*dev
= port
->dev
;
1145 dprintk(DBGLVL_ENC
, "%s(port=%d)\n", __func__
, port
->nr
);
1147 BUG_ON(port
->type
!= SAA7164_MPEG_ENCODER
);
1149 if (port
->v4l_device
) {
1150 if (port
->v4l_device
->minor
!= -1)
1151 video_unregister_device(port
->v4l_device
);
1153 video_device_release(port
->v4l_device
);
1155 port
->v4l_device
= NULL
;
1157 v4l2_ctrl_handler_free(&port
->ctrl_handler
);
1159 dprintk(DBGLVL_ENC
, "%s(port=%d) done\n", __func__
, port
->nr
);