2 * cx18 ioctl system call
4 * Derived from ivtv-ioctl.c
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include "cx18-driver.h"
27 #include "cx18-version.h"
28 #include "cx18-mailbox.h"
30 #include "cx18-queue.h"
31 #include "cx18-fileops.h"
33 #include "cx18-audio.h"
34 #include "cx18-video.h"
35 #include "cx18-streams.h"
36 #include "cx18-ioctl.h"
37 #include "cx18-gpio.h"
38 #include "cx18-controls.h"
39 #include "cx18-cards.h"
40 #include "cx18-av-core.h"
41 #include <media/tveeprom.h>
42 #include <media/v4l2-event.h>
44 u16
cx18_service2vbi(int type
)
47 case V4L2_SLICED_TELETEXT_B
:
48 return CX18_SLICED_TYPE_TELETEXT_B
;
49 case V4L2_SLICED_CAPTION_525
:
50 return CX18_SLICED_TYPE_CAPTION_525
;
51 case V4L2_SLICED_WSS_625
:
52 return CX18_SLICED_TYPE_WSS_625
;
54 return CX18_SLICED_TYPE_VPS
;
60 /* Check if VBI services are allowed on the (field, line) for the video std */
61 static int valid_service_line(int field
, int line
, int is_pal
)
63 return (is_pal
&& line
>= 6 &&
64 ((field
== 0 && line
<= 23) || (field
== 1 && line
<= 22))) ||
65 (!is_pal
&& line
>= 10 && line
< 22);
69 * For a (field, line, std) and inbound potential set of services for that line,
70 * return the first valid service of those passed in the incoming set for that
71 * line in priority order:
72 * CC, VPS, or WSS over TELETEXT for well known lines
73 * TELETEXT, before VPS, before CC, before WSS, for other lines
75 static u16
select_service_from_set(int field
, int line
, u16 set
, int is_pal
)
77 u16 valid_set
= (is_pal
? V4L2_SLICED_VBI_625
: V4L2_SLICED_VBI_525
);
80 set
= set
& valid_set
;
81 if (set
== 0 || !valid_service_line(field
, line
, is_pal
))
84 if (line
== 21 && (set
& V4L2_SLICED_CAPTION_525
))
85 return V4L2_SLICED_CAPTION_525
;
87 if (line
== 16 && field
== 0 && (set
& V4L2_SLICED_VPS
))
88 return V4L2_SLICED_VPS
;
89 if (line
== 23 && field
== 0 && (set
& V4L2_SLICED_WSS_625
))
90 return V4L2_SLICED_WSS_625
;
94 for (i
= 0; i
< 32; i
++) {
102 * Expand the service_set of *fmt into valid service_lines for the std,
103 * and clear the passed in fmt->service_set
105 void cx18_expand_service_set(struct v4l2_sliced_vbi_format
*fmt
, int is_pal
)
107 u16 set
= fmt
->service_set
;
110 fmt
->service_set
= 0;
111 for (f
= 0; f
< 2; f
++) {
112 for (l
= 0; l
< 24; l
++)
113 fmt
->service_lines
[f
][l
] = select_service_from_set(f
, l
, set
, is_pal
);
118 * Sanitize the service_lines in *fmt per the video std, and return 1
119 * if any service_line is left as valid after santization
121 static int check_service_set(struct v4l2_sliced_vbi_format
*fmt
, int is_pal
)
126 for (f
= 0; f
< 2; f
++) {
127 for (l
= 0; l
< 24; l
++) {
128 fmt
->service_lines
[f
][l
] = select_service_from_set(f
, l
, fmt
->service_lines
[f
][l
], is_pal
);
129 set
|= fmt
->service_lines
[f
][l
];
135 /* Compute the service_set from the assumed valid service_lines of *fmt */
136 u16
cx18_get_service_set(struct v4l2_sliced_vbi_format
*fmt
)
141 for (f
= 0; f
< 2; f
++) {
142 for (l
= 0; l
< 24; l
++)
143 set
|= fmt
->service_lines
[f
][l
];
148 static int cx18_g_fmt_vid_cap(struct file
*file
, void *fh
,
149 struct v4l2_format
*fmt
)
151 struct cx18_open_id
*id
= fh2id(fh
);
152 struct cx18
*cx
= id
->cx
;
153 struct cx18_stream
*s
= &cx
->streams
[id
->type
];
154 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
156 pixfmt
->width
= cx
->cxhdl
.width
;
157 pixfmt
->height
= cx
->cxhdl
.height
;
158 pixfmt
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
159 pixfmt
->field
= V4L2_FIELD_INTERLACED
;
160 if (id
->type
== CX18_ENC_STREAM_TYPE_YUV
) {
161 pixfmt
->pixelformat
= s
->pixelformat
;
162 pixfmt
->sizeimage
= s
->vb_bytes_per_frame
;
163 pixfmt
->bytesperline
= s
->vb_bytes_per_line
;
165 pixfmt
->pixelformat
= V4L2_PIX_FMT_MPEG
;
166 pixfmt
->sizeimage
= 128 * 1024;
167 pixfmt
->bytesperline
= 0;
172 static int cx18_g_fmt_vbi_cap(struct file
*file
, void *fh
,
173 struct v4l2_format
*fmt
)
175 struct cx18
*cx
= fh2id(fh
)->cx
;
176 struct v4l2_vbi_format
*vbifmt
= &fmt
->fmt
.vbi
;
178 vbifmt
->sampling_rate
= 27000000;
179 vbifmt
->offset
= 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
180 vbifmt
->samples_per_line
= vbi_active_samples
- 4;
181 vbifmt
->sample_format
= V4L2_PIX_FMT_GREY
;
182 vbifmt
->start
[0] = cx
->vbi
.start
[0];
183 vbifmt
->start
[1] = cx
->vbi
.start
[1];
184 vbifmt
->count
[0] = vbifmt
->count
[1] = cx
->vbi
.count
;
186 vbifmt
->reserved
[0] = 0;
187 vbifmt
->reserved
[1] = 0;
191 static int cx18_g_fmt_sliced_vbi_cap(struct file
*file
, void *fh
,
192 struct v4l2_format
*fmt
)
194 struct cx18
*cx
= fh2id(fh
)->cx
;
195 struct v4l2_sliced_vbi_format
*vbifmt
= &fmt
->fmt
.sliced
;
197 /* sane, V4L2 spec compliant, defaults */
198 vbifmt
->reserved
[0] = 0;
199 vbifmt
->reserved
[1] = 0;
200 vbifmt
->io_size
= sizeof(struct v4l2_sliced_vbi_data
) * 36;
201 memset(vbifmt
->service_lines
, 0, sizeof(vbifmt
->service_lines
));
202 vbifmt
->service_set
= 0;
205 * Fetch the configured service_lines and total service_set from the
206 * digitizer/slicer. Note, cx18_av_vbi() wipes the passed in
207 * fmt->fmt.sliced under valid calling conditions
209 if (v4l2_subdev_call(cx
->sd_av
, vbi
, g_sliced_fmt
, &fmt
->fmt
.sliced
))
212 vbifmt
->service_set
= cx18_get_service_set(vbifmt
);
216 static int cx18_try_fmt_vid_cap(struct file
*file
, void *fh
,
217 struct v4l2_format
*fmt
)
219 struct cx18_open_id
*id
= fh2id(fh
);
220 struct cx18
*cx
= id
->cx
;
221 int w
= fmt
->fmt
.pix
.width
;
222 int h
= fmt
->fmt
.pix
.height
;
227 if (id
->type
== CX18_ENC_STREAM_TYPE_YUV
) {
228 /* YUV height must be a multiple of 32 */
232 h
= min(h
, cx
->is_50hz
? 576 : 480);
235 fmt
->fmt
.pix
.width
= w
;
236 fmt
->fmt
.pix
.height
= h
;
240 static int cx18_try_fmt_vbi_cap(struct file
*file
, void *fh
,
241 struct v4l2_format
*fmt
)
243 return cx18_g_fmt_vbi_cap(file
, fh
, fmt
);
246 static int cx18_try_fmt_sliced_vbi_cap(struct file
*file
, void *fh
,
247 struct v4l2_format
*fmt
)
249 struct cx18
*cx
= fh2id(fh
)->cx
;
250 struct v4l2_sliced_vbi_format
*vbifmt
= &fmt
->fmt
.sliced
;
252 vbifmt
->io_size
= sizeof(struct v4l2_sliced_vbi_data
) * 36;
253 vbifmt
->reserved
[0] = 0;
254 vbifmt
->reserved
[1] = 0;
256 /* If given a service set, expand it validly & clear passed in set */
257 if (vbifmt
->service_set
)
258 cx18_expand_service_set(vbifmt
, cx
->is_50hz
);
259 /* Sanitize the service_lines, and compute the new set if any valid */
260 if (check_service_set(vbifmt
, cx
->is_50hz
))
261 vbifmt
->service_set
= cx18_get_service_set(vbifmt
);
265 static int cx18_s_fmt_vid_cap(struct file
*file
, void *fh
,
266 struct v4l2_format
*fmt
)
268 struct cx18_open_id
*id
= fh2id(fh
);
269 struct cx18
*cx
= id
->cx
;
270 struct v4l2_mbus_framefmt mbus_fmt
;
271 struct cx18_stream
*s
= &cx
->streams
[id
->type
];
275 ret
= cx18_try_fmt_vid_cap(file
, fh
, fmt
);
278 w
= fmt
->fmt
.pix
.width
;
279 h
= fmt
->fmt
.pix
.height
;
281 if (cx
->cxhdl
.width
== w
&& cx
->cxhdl
.height
== h
&&
282 s
->pixelformat
== fmt
->fmt
.pix
.pixelformat
)
285 if (atomic_read(&cx
->ana_capturing
) > 0)
288 s
->pixelformat
= fmt
->fmt
.pix
.pixelformat
;
289 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
290 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
291 if (s
->pixelformat
== V4L2_PIX_FMT_HM12
) {
292 s
->vb_bytes_per_frame
= h
* 720 * 3 / 2;
293 s
->vb_bytes_per_line
= 720; /* First plane */
295 s
->vb_bytes_per_frame
= h
* 720 * 2;
296 s
->vb_bytes_per_line
= 1440; /* Packed */
299 mbus_fmt
.width
= cx
->cxhdl
.width
= w
;
300 mbus_fmt
.height
= cx
->cxhdl
.height
= h
;
301 mbus_fmt
.code
= MEDIA_BUS_FMT_FIXED
;
302 v4l2_subdev_call(cx
->sd_av
, video
, s_mbus_fmt
, &mbus_fmt
);
303 return cx18_g_fmt_vid_cap(file
, fh
, fmt
);
306 static int cx18_s_fmt_vbi_cap(struct file
*file
, void *fh
,
307 struct v4l2_format
*fmt
)
309 struct cx18_open_id
*id
= fh2id(fh
);
310 struct cx18
*cx
= id
->cx
;
314 * Changing the Encoder's Raw VBI parameters won't have any effect
315 * if any analog capture is ongoing
317 if (!cx18_raw_vbi(cx
) && atomic_read(&cx
->ana_capturing
) > 0)
321 * Set the digitizer registers for raw active VBI.
322 * Note cx18_av_vbi_wipes out a lot of the passed in fmt under valid
325 ret
= v4l2_subdev_call(cx
->sd_av
, vbi
, s_raw_fmt
, &fmt
->fmt
.vbi
);
329 /* Store our new v4l2 (non-)sliced VBI state */
330 cx
->vbi
.sliced_in
->service_set
= 0;
331 cx
->vbi
.in
.type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
333 return cx18_g_fmt_vbi_cap(file
, fh
, fmt
);
336 static int cx18_s_fmt_sliced_vbi_cap(struct file
*file
, void *fh
,
337 struct v4l2_format
*fmt
)
339 struct cx18_open_id
*id
= fh2id(fh
);
340 struct cx18
*cx
= id
->cx
;
342 struct v4l2_sliced_vbi_format
*vbifmt
= &fmt
->fmt
.sliced
;
344 cx18_try_fmt_sliced_vbi_cap(file
, fh
, fmt
);
347 * Changing the Encoder's Raw VBI parameters won't have any effect
348 * if any analog capture is ongoing
350 if (cx18_raw_vbi(cx
) && atomic_read(&cx
->ana_capturing
) > 0)
354 * Set the service_lines requested in the digitizer/slicer registers.
355 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
356 * passed in fmt->fmt.sliced under valid calling conditions
358 ret
= v4l2_subdev_call(cx
->sd_av
, vbi
, s_sliced_fmt
, &fmt
->fmt
.sliced
);
361 /* Store our current v4l2 sliced VBI settings */
362 cx
->vbi
.in
.type
= V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
;
363 memcpy(cx
->vbi
.sliced_in
, vbifmt
, sizeof(*cx
->vbi
.sliced_in
));
367 #ifdef CONFIG_VIDEO_ADV_DEBUG
368 static int cx18_g_register(struct file
*file
, void *fh
,
369 struct v4l2_dbg_register
*reg
)
371 struct cx18
*cx
= fh2id(fh
)->cx
;
375 if (reg
->reg
>= CX18_MEM_OFFSET
+ CX18_MEM_SIZE
)
378 reg
->val
= cx18_read_enc(cx
, reg
->reg
);
382 static int cx18_s_register(struct file
*file
, void *fh
,
383 const struct v4l2_dbg_register
*reg
)
385 struct cx18
*cx
= fh2id(fh
)->cx
;
389 if (reg
->reg
>= CX18_MEM_OFFSET
+ CX18_MEM_SIZE
)
391 cx18_write_enc(cx
, reg
->val
, reg
->reg
);
396 static int cx18_querycap(struct file
*file
, void *fh
,
397 struct v4l2_capability
*vcap
)
399 struct cx18_open_id
*id
= fh2id(fh
);
400 struct cx18_stream
*s
= video_drvdata(file
);
401 struct cx18
*cx
= id
->cx
;
403 strlcpy(vcap
->driver
, CX18_DRIVER_NAME
, sizeof(vcap
->driver
));
404 strlcpy(vcap
->card
, cx
->card_name
, sizeof(vcap
->card
));
405 snprintf(vcap
->bus_info
, sizeof(vcap
->bus_info
),
406 "PCI:%s", pci_name(cx
->pci_dev
));
407 vcap
->capabilities
= cx
->v4l2_cap
; /* capabilities */
408 vcap
->device_caps
= s
->v4l2_dev_caps
; /* device capabilities */
409 vcap
->capabilities
|= V4L2_CAP_DEVICE_CAPS
;
413 static int cx18_enumaudio(struct file
*file
, void *fh
, struct v4l2_audio
*vin
)
415 struct cx18
*cx
= fh2id(fh
)->cx
;
417 return cx18_get_audio_input(cx
, vin
->index
, vin
);
420 static int cx18_g_audio(struct file
*file
, void *fh
, struct v4l2_audio
*vin
)
422 struct cx18
*cx
= fh2id(fh
)->cx
;
424 vin
->index
= cx
->audio_input
;
425 return cx18_get_audio_input(cx
, vin
->index
, vin
);
428 static int cx18_s_audio(struct file
*file
, void *fh
, const struct v4l2_audio
*vout
)
430 struct cx18
*cx
= fh2id(fh
)->cx
;
432 if (vout
->index
>= cx
->nof_audio_inputs
)
434 cx
->audio_input
= vout
->index
;
435 cx18_audio_set_io(cx
);
439 static int cx18_enum_input(struct file
*file
, void *fh
, struct v4l2_input
*vin
)
441 struct cx18
*cx
= fh2id(fh
)->cx
;
443 /* set it to defaults from our table */
444 return cx18_get_input(cx
, vin
->index
, vin
);
447 static int cx18_cropcap(struct file
*file
, void *fh
,
448 struct v4l2_cropcap
*cropcap
)
450 struct cx18
*cx
= fh2id(fh
)->cx
;
452 if (cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
454 cropcap
->pixelaspect
.numerator
= cx
->is_50hz
? 59 : 10;
455 cropcap
->pixelaspect
.denominator
= cx
->is_50hz
? 54 : 11;
459 static int cx18_g_selection(struct file
*file
, void *fh
,
460 struct v4l2_selection
*sel
)
462 struct cx18
*cx
= fh2id(fh
)->cx
;
464 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
466 switch (sel
->target
) {
467 case V4L2_SEL_TGT_CROP_BOUNDS
:
468 case V4L2_SEL_TGT_CROP_DEFAULT
:
469 sel
->r
.top
= sel
->r
.left
= 0;
471 sel
->r
.height
= cx
->is_50hz
? 576 : 480;
479 static int cx18_enum_fmt_vid_cap(struct file
*file
, void *fh
,
480 struct v4l2_fmtdesc
*fmt
)
482 static const struct v4l2_fmtdesc formats
[] = {
483 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE
, 0,
484 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12
, { 0, 0, 0, 0 }
486 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE
, V4L2_FMT_FLAG_COMPRESSED
,
487 "MPEG", V4L2_PIX_FMT_MPEG
, { 0, 0, 0, 0 }
489 { 2, V4L2_BUF_TYPE_VIDEO_CAPTURE
, 0,
490 "UYVY 4:2:2", V4L2_PIX_FMT_UYVY
, { 0, 0, 0, 0 }
494 if (fmt
->index
> ARRAY_SIZE(formats
) - 1)
496 *fmt
= formats
[fmt
->index
];
500 static int cx18_g_input(struct file
*file
, void *fh
, unsigned int *i
)
502 struct cx18
*cx
= fh2id(fh
)->cx
;
504 *i
= cx
->active_input
;
508 int cx18_s_input(struct file
*file
, void *fh
, unsigned int inp
)
510 struct cx18_open_id
*id
= fh2id(fh
);
511 struct cx18
*cx
= id
->cx
;
512 v4l2_std_id std
= V4L2_STD_ALL
;
513 const struct cx18_card_video_input
*card_input
=
514 cx
->card
->video_inputs
+ inp
;
516 if (inp
>= cx
->nof_inputs
)
519 if (inp
== cx
->active_input
) {
520 CX18_DEBUG_INFO("Input unchanged\n");
524 CX18_DEBUG_INFO("Changing input from %d to %d\n",
525 cx
->active_input
, inp
);
527 cx
->active_input
= inp
;
528 /* Set the audio input to whatever is appropriate for the input type. */
529 cx
->audio_input
= cx
->card
->video_inputs
[inp
].audio_index
;
530 if (card_input
->video_type
== V4L2_INPUT_TYPE_TUNER
)
532 cx
->streams
[CX18_ENC_STREAM_TYPE_MPG
].video_dev
.tvnorms
= std
;
533 cx
->streams
[CX18_ENC_STREAM_TYPE_YUV
].video_dev
.tvnorms
= std
;
534 cx
->streams
[CX18_ENC_STREAM_TYPE_VBI
].video_dev
.tvnorms
= std
;
536 /* prevent others from messing with the streams until
537 we're finished changing inputs. */
539 cx18_video_set_io(cx
);
540 cx18_audio_set_io(cx
);
545 static int cx18_g_frequency(struct file
*file
, void *fh
,
546 struct v4l2_frequency
*vf
)
548 struct cx18
*cx
= fh2id(fh
)->cx
;
553 cx18_call_all(cx
, tuner
, g_frequency
, vf
);
557 int cx18_s_frequency(struct file
*file
, void *fh
, const struct v4l2_frequency
*vf
)
559 struct cx18_open_id
*id
= fh2id(fh
);
560 struct cx18
*cx
= id
->cx
;
566 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf
->frequency
);
567 cx18_call_all(cx
, tuner
, s_frequency
, vf
);
572 static int cx18_g_std(struct file
*file
, void *fh
, v4l2_std_id
*std
)
574 struct cx18
*cx
= fh2id(fh
)->cx
;
580 int cx18_s_std(struct file
*file
, void *fh
, v4l2_std_id std
)
582 struct cx18_open_id
*id
= fh2id(fh
);
583 struct cx18
*cx
= id
->cx
;
585 if ((std
& V4L2_STD_ALL
) == 0)
591 if (test_bit(CX18_F_I_RADIO_USER
, &cx
->i_flags
) ||
592 atomic_read(&cx
->ana_capturing
) > 0) {
593 /* Switching standard would turn off the radio or mess
594 with already running streams, prevent that by
600 cx
->is_60hz
= (std
& V4L2_STD_525_60
) ? 1 : 0;
601 cx
->is_50hz
= !cx
->is_60hz
;
602 cx2341x_handler_set_50hz(&cx
->cxhdl
, cx
->is_50hz
);
603 cx
->cxhdl
.width
= 720;
604 cx
->cxhdl
.height
= cx
->is_50hz
? 576 : 480;
605 cx
->vbi
.count
= cx
->is_50hz
? 18 : 12;
606 cx
->vbi
.start
[0] = cx
->is_50hz
? 6 : 10;
607 cx
->vbi
.start
[1] = cx
->is_50hz
? 318 : 273;
608 CX18_DEBUG_INFO("Switching standard to %llx.\n",
609 (unsigned long long) cx
->std
);
612 cx18_call_all(cx
, video
, s_std
, cx
->std
);
616 static int cx18_s_tuner(struct file
*file
, void *fh
, const struct v4l2_tuner
*vt
)
618 struct cx18_open_id
*id
= fh2id(fh
);
619 struct cx18
*cx
= id
->cx
;
624 cx18_call_all(cx
, tuner
, s_tuner
, vt
);
628 static int cx18_g_tuner(struct file
*file
, void *fh
, struct v4l2_tuner
*vt
)
630 struct cx18
*cx
= fh2id(fh
)->cx
;
635 cx18_call_all(cx
, tuner
, g_tuner
, vt
);
637 if (vt
->type
== V4L2_TUNER_RADIO
)
638 strlcpy(vt
->name
, "cx18 Radio Tuner", sizeof(vt
->name
));
640 strlcpy(vt
->name
, "cx18 TV Tuner", sizeof(vt
->name
));
644 static int cx18_g_sliced_vbi_cap(struct file
*file
, void *fh
,
645 struct v4l2_sliced_vbi_cap
*cap
)
647 struct cx18
*cx
= fh2id(fh
)->cx
;
648 int set
= cx
->is_50hz
? V4L2_SLICED_VBI_625
: V4L2_SLICED_VBI_525
;
651 if (cap
->type
!= V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
)
654 cap
->service_set
= 0;
655 for (f
= 0; f
< 2; f
++) {
656 for (l
= 0; l
< 24; l
++) {
657 if (valid_service_line(f
, l
, cx
->is_50hz
)) {
659 * We can find all v4l2 supported vbi services
660 * for the standard, on a valid line for the std
662 cap
->service_lines
[f
][l
] = set
;
663 cap
->service_set
|= set
;
665 cap
->service_lines
[f
][l
] = 0;
668 for (f
= 0; f
< 3; f
++)
669 cap
->reserved
[f
] = 0;
673 static int _cx18_process_idx_data(struct cx18_buffer
*buf
,
674 struct v4l2_enc_idx
*idx
)
676 int consumed
, remaining
;
677 struct v4l2_enc_idx_entry
*e_idx
;
678 struct cx18_enc_idx_entry
*e_buf
;
680 /* Frame type lookup: 1=I, 2=P, 4=B */
681 const int mapping
[8] = {
682 -1, V4L2_ENC_IDX_FRAME_I
, V4L2_ENC_IDX_FRAME_P
,
683 -1, V4L2_ENC_IDX_FRAME_B
, -1, -1, -1
687 * Assumption here is that a buf holds an integral number of
688 * struct cx18_enc_idx_entry objects and is properly aligned.
689 * This is enforced by the module options on IDX buffer sizes.
691 remaining
= buf
->bytesused
- buf
->readpos
;
693 e_idx
= &idx
->entry
[idx
->entries
];
694 e_buf
= (struct cx18_enc_idx_entry
*) &buf
->buf
[buf
->readpos
];
696 while (remaining
>= sizeof(struct cx18_enc_idx_entry
) &&
697 idx
->entries
< V4L2_ENC_IDX_ENTRIES
) {
699 e_idx
->offset
= (((u64
) le32_to_cpu(e_buf
->offset_high
)) << 32)
700 | le32_to_cpu(e_buf
->offset_low
);
702 e_idx
->pts
= (((u64
) (le32_to_cpu(e_buf
->pts_high
) & 1)) << 32)
703 | le32_to_cpu(e_buf
->pts_low
);
705 e_idx
->length
= le32_to_cpu(e_buf
->length
);
707 e_idx
->flags
= mapping
[le32_to_cpu(e_buf
->flags
) & 0x7];
709 e_idx
->reserved
[0] = 0;
710 e_idx
->reserved
[1] = 0;
713 e_idx
= &idx
->entry
[idx
->entries
];
716 remaining
-= sizeof(struct cx18_enc_idx_entry
);
717 consumed
+= sizeof(struct cx18_enc_idx_entry
);
720 /* Swallow any partial entries at the end, if there are any */
721 if (remaining
> 0 && remaining
< sizeof(struct cx18_enc_idx_entry
))
722 consumed
+= remaining
;
724 buf
->readpos
+= consumed
;
728 static int cx18_process_idx_data(struct cx18_stream
*s
, struct cx18_mdl
*mdl
,
729 struct v4l2_enc_idx
*idx
)
731 if (s
->type
!= CX18_ENC_STREAM_TYPE_IDX
)
734 if (mdl
->curr_buf
== NULL
)
735 mdl
->curr_buf
= list_first_entry(&mdl
->buf_list
,
736 struct cx18_buffer
, list
);
738 if (list_entry_is_past_end(mdl
->curr_buf
, &mdl
->buf_list
, list
)) {
740 * For some reason we've exhausted the buffers, but the MDL
741 * object still said some data was unread.
742 * Fix that and bail out.
744 mdl
->readpos
= mdl
->bytesused
;
748 list_for_each_entry_from(mdl
->curr_buf
, &mdl
->buf_list
, list
) {
750 /* Skip any empty buffers in the MDL */
751 if (mdl
->curr_buf
->readpos
>= mdl
->curr_buf
->bytesused
)
754 mdl
->readpos
+= _cx18_process_idx_data(mdl
->curr_buf
, idx
);
756 /* exit when MDL drained or request satisfied */
757 if (idx
->entries
>= V4L2_ENC_IDX_ENTRIES
||
758 mdl
->curr_buf
->readpos
< mdl
->curr_buf
->bytesused
||
759 mdl
->readpos
>= mdl
->bytesused
)
765 static int cx18_g_enc_index(struct file
*file
, void *fh
,
766 struct v4l2_enc_idx
*idx
)
768 struct cx18
*cx
= fh2id(fh
)->cx
;
769 struct cx18_stream
*s
= &cx
->streams
[CX18_ENC_STREAM_TYPE_IDX
];
771 struct cx18_mdl
*mdl
;
773 if (!cx18_stream_enabled(s
)) /* Module options inhibited IDX stream */
776 /* Compute the best case number of entries we can buffer */
778 s
->bufs_per_mdl
* CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN
;
781 tmp
= tmp
* s
->buf_size
/ sizeof(struct cx18_enc_idx_entry
);
783 /* Fill out the header of the return structure */
785 idx
->entries_cap
= tmp
;
786 memset(idx
->reserved
, 0, sizeof(idx
->reserved
));
788 /* Pull IDX MDLs and buffers from q_full and populate the entries */
790 mdl
= cx18_dequeue(s
, &s
->q_full
);
791 if (mdl
== NULL
) /* No more IDX data right now */
794 /* Extract the Index entry data from the MDL and buffers */
795 cx18_process_idx_data(s
, mdl
, idx
);
796 if (mdl
->readpos
< mdl
->bytesused
) {
797 /* We finished with data remaining, push the MDL back */
798 cx18_push(s
, mdl
, &s
->q_full
);
802 /* We drained this MDL, schedule it to go to the firmware */
803 cx18_enqueue(s
, mdl
, &s
->q_free
);
805 } while (idx
->entries
< V4L2_ENC_IDX_ENTRIES
);
807 /* Tell the work handler to send free IDX MDLs to the firmware */
808 cx18_stream_load_fw_queue(s
);
812 static struct videobuf_queue
*cx18_vb_queue(struct cx18_open_id
*id
)
814 struct videobuf_queue
*q
= NULL
;
815 struct cx18
*cx
= id
->cx
;
816 struct cx18_stream
*s
= &cx
->streams
[id
->type
];
818 switch (s
->vb_type
) {
819 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
822 case V4L2_BUF_TYPE_VBI_CAPTURE
:
830 static int cx18_streamon(struct file
*file
, void *priv
,
831 enum v4l2_buf_type type
)
833 struct cx18_open_id
*id
= file
->private_data
;
834 struct cx18
*cx
= id
->cx
;
835 struct cx18_stream
*s
= &cx
->streams
[id
->type
];
837 /* Start the hardware only if we're the video device */
838 if ((s
->vb_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
839 (s
->vb_type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
842 if (id
->type
!= CX18_ENC_STREAM_TYPE_YUV
)
845 /* Establish a buffer timeout */
846 mod_timer(&s
->vb_timeout
, msecs_to_jiffies(2000) + jiffies
);
848 return videobuf_streamon(cx18_vb_queue(id
));
851 static int cx18_streamoff(struct file
*file
, void *priv
,
852 enum v4l2_buf_type type
)
854 struct cx18_open_id
*id
= file
->private_data
;
855 struct cx18
*cx
= id
->cx
;
856 struct cx18_stream
*s
= &cx
->streams
[id
->type
];
858 /* Start the hardware only if we're the video device */
859 if ((s
->vb_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
860 (s
->vb_type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
863 if (id
->type
!= CX18_ENC_STREAM_TYPE_YUV
)
866 return videobuf_streamoff(cx18_vb_queue(id
));
869 static int cx18_reqbufs(struct file
*file
, void *priv
,
870 struct v4l2_requestbuffers
*rb
)
872 struct cx18_open_id
*id
= file
->private_data
;
873 struct cx18
*cx
= id
->cx
;
874 struct cx18_stream
*s
= &cx
->streams
[id
->type
];
876 if ((s
->vb_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
877 (s
->vb_type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
880 return videobuf_reqbufs(cx18_vb_queue(id
), rb
);
883 static int cx18_querybuf(struct file
*file
, void *priv
,
884 struct v4l2_buffer
*b
)
886 struct cx18_open_id
*id
= file
->private_data
;
887 struct cx18
*cx
= id
->cx
;
888 struct cx18_stream
*s
= &cx
->streams
[id
->type
];
890 if ((s
->vb_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
891 (s
->vb_type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
894 return videobuf_querybuf(cx18_vb_queue(id
), b
);
897 static int cx18_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
899 struct cx18_open_id
*id
= file
->private_data
;
900 struct cx18
*cx
= id
->cx
;
901 struct cx18_stream
*s
= &cx
->streams
[id
->type
];
903 if ((s
->vb_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
904 (s
->vb_type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
907 return videobuf_qbuf(cx18_vb_queue(id
), b
);
910 static int cx18_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
912 struct cx18_open_id
*id
= file
->private_data
;
913 struct cx18
*cx
= id
->cx
;
914 struct cx18_stream
*s
= &cx
->streams
[id
->type
];
916 if ((s
->vb_type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
917 (s
->vb_type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
920 return videobuf_dqbuf(cx18_vb_queue(id
), b
, file
->f_flags
& O_NONBLOCK
);
923 static int cx18_encoder_cmd(struct file
*file
, void *fh
,
924 struct v4l2_encoder_cmd
*enc
)
926 struct cx18_open_id
*id
= fh2id(fh
);
927 struct cx18
*cx
= id
->cx
;
931 case V4L2_ENC_CMD_START
:
932 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
934 return cx18_start_capture(id
);
936 case V4L2_ENC_CMD_STOP
:
937 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
938 enc
->flags
&= V4L2_ENC_CMD_STOP_AT_GOP_END
;
939 cx18_stop_capture(id
,
940 enc
->flags
& V4L2_ENC_CMD_STOP_AT_GOP_END
);
943 case V4L2_ENC_CMD_PAUSE
:
944 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
946 if (!atomic_read(&cx
->ana_capturing
))
948 if (test_and_set_bit(CX18_F_I_ENC_PAUSED
, &cx
->i_flags
))
950 h
= cx18_find_handle(cx
);
951 if (h
== CX18_INVALID_TASK_HANDLE
) {
952 CX18_ERR("Can't find valid task handle for "
953 "V4L2_ENC_CMD_PAUSE\n");
957 cx18_vapi(cx
, CX18_CPU_CAPTURE_PAUSE
, 1, h
);
960 case V4L2_ENC_CMD_RESUME
:
961 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
963 if (!atomic_read(&cx
->ana_capturing
))
965 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED
, &cx
->i_flags
))
967 h
= cx18_find_handle(cx
);
968 if (h
== CX18_INVALID_TASK_HANDLE
) {
969 CX18_ERR("Can't find valid task handle for "
970 "V4L2_ENC_CMD_RESUME\n");
973 cx18_vapi(cx
, CX18_CPU_CAPTURE_RESUME
, 1, h
);
978 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc
->cmd
);
984 static int cx18_try_encoder_cmd(struct file
*file
, void *fh
,
985 struct v4l2_encoder_cmd
*enc
)
987 struct cx18
*cx
= fh2id(fh
)->cx
;
990 case V4L2_ENC_CMD_START
:
991 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
995 case V4L2_ENC_CMD_STOP
:
996 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
997 enc
->flags
&= V4L2_ENC_CMD_STOP_AT_GOP_END
;
1000 case V4L2_ENC_CMD_PAUSE
:
1001 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1005 case V4L2_ENC_CMD_RESUME
:
1006 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1011 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc
->cmd
);
1017 static int cx18_log_status(struct file
*file
, void *fh
)
1019 struct cx18
*cx
= fh2id(fh
)->cx
;
1020 struct v4l2_input vidin
;
1021 struct v4l2_audio audin
;
1024 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION
, cx
->card_name
);
1025 if (cx
->hw_flags
& CX18_HW_TVEEPROM
) {
1028 cx18_read_eeprom(cx
, &tv
);
1030 cx18_call_all(cx
, core
, log_status
);
1031 cx18_get_input(cx
, cx
->active_input
, &vidin
);
1032 cx18_get_audio_input(cx
, cx
->audio_input
, &audin
);
1033 CX18_INFO("Video Input: %s\n", vidin
.name
);
1034 CX18_INFO("Audio Input: %s\n", audin
.name
);
1035 mutex_lock(&cx
->gpio_lock
);
1036 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
1037 cx
->gpio_dir
, cx
->gpio_val
);
1038 mutex_unlock(&cx
->gpio_lock
);
1039 CX18_INFO("Tuner: %s\n",
1040 test_bit(CX18_F_I_RADIO_USER
, &cx
->i_flags
) ? "Radio" : "TV");
1041 v4l2_ctrl_handler_log_status(&cx
->cxhdl
.hdl
, cx
->v4l2_dev
.name
);
1042 CX18_INFO("Status flags: 0x%08lx\n", cx
->i_flags
);
1043 for (i
= 0; i
< CX18_MAX_STREAMS
; i
++) {
1044 struct cx18_stream
*s
= &cx
->streams
[i
];
1046 if (s
->video_dev
.v4l2_dev
== NULL
|| s
->buffers
== 0)
1048 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1049 s
->name
, s
->s_flags
,
1050 atomic_read(&s
->q_full
.depth
) * s
->bufs_per_mdl
* 100
1052 (s
->buffers
* s
->buf_size
) / 1024, s
->buffers
);
1054 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1055 (long long)cx
->mpg_data_received
,
1056 (long long)cx
->vbi_data_inserted
);
1060 static long cx18_default(struct file
*file
, void *fh
, bool valid_prio
,
1061 unsigned int cmd
, void *arg
)
1063 struct cx18
*cx
= fh2id(fh
)->cx
;
1066 case VIDIOC_INT_RESET
: {
1067 u32 val
= *(u32
*)arg
;
1069 if ((val
== 0) || (val
& 0x01))
1070 cx18_call_hw(cx
, CX18_HW_GPIO_RESET_CTRL
, core
, reset
,
1071 (u32
) CX18_GPIO_RESET_Z8F0811
);
1081 static const struct v4l2_ioctl_ops cx18_ioctl_ops
= {
1082 .vidioc_querycap
= cx18_querycap
,
1083 .vidioc_s_audio
= cx18_s_audio
,
1084 .vidioc_g_audio
= cx18_g_audio
,
1085 .vidioc_enumaudio
= cx18_enumaudio
,
1086 .vidioc_enum_input
= cx18_enum_input
,
1087 .vidioc_cropcap
= cx18_cropcap
,
1088 .vidioc_g_selection
= cx18_g_selection
,
1089 .vidioc_g_input
= cx18_g_input
,
1090 .vidioc_s_input
= cx18_s_input
,
1091 .vidioc_g_frequency
= cx18_g_frequency
,
1092 .vidioc_s_frequency
= cx18_s_frequency
,
1093 .vidioc_s_tuner
= cx18_s_tuner
,
1094 .vidioc_g_tuner
= cx18_g_tuner
,
1095 .vidioc_g_enc_index
= cx18_g_enc_index
,
1096 .vidioc_g_std
= cx18_g_std
,
1097 .vidioc_s_std
= cx18_s_std
,
1098 .vidioc_log_status
= cx18_log_status
,
1099 .vidioc_enum_fmt_vid_cap
= cx18_enum_fmt_vid_cap
,
1100 .vidioc_encoder_cmd
= cx18_encoder_cmd
,
1101 .vidioc_try_encoder_cmd
= cx18_try_encoder_cmd
,
1102 .vidioc_g_fmt_vid_cap
= cx18_g_fmt_vid_cap
,
1103 .vidioc_g_fmt_vbi_cap
= cx18_g_fmt_vbi_cap
,
1104 .vidioc_g_fmt_sliced_vbi_cap
= cx18_g_fmt_sliced_vbi_cap
,
1105 .vidioc_s_fmt_vid_cap
= cx18_s_fmt_vid_cap
,
1106 .vidioc_s_fmt_vbi_cap
= cx18_s_fmt_vbi_cap
,
1107 .vidioc_s_fmt_sliced_vbi_cap
= cx18_s_fmt_sliced_vbi_cap
,
1108 .vidioc_try_fmt_vid_cap
= cx18_try_fmt_vid_cap
,
1109 .vidioc_try_fmt_vbi_cap
= cx18_try_fmt_vbi_cap
,
1110 .vidioc_try_fmt_sliced_vbi_cap
= cx18_try_fmt_sliced_vbi_cap
,
1111 .vidioc_g_sliced_vbi_cap
= cx18_g_sliced_vbi_cap
,
1112 #ifdef CONFIG_VIDEO_ADV_DEBUG
1113 .vidioc_g_register
= cx18_g_register
,
1114 .vidioc_s_register
= cx18_s_register
,
1116 .vidioc_default
= cx18_default
,
1117 .vidioc_streamon
= cx18_streamon
,
1118 .vidioc_streamoff
= cx18_streamoff
,
1119 .vidioc_reqbufs
= cx18_reqbufs
,
1120 .vidioc_querybuf
= cx18_querybuf
,
1121 .vidioc_qbuf
= cx18_qbuf
,
1122 .vidioc_dqbuf
= cx18_dqbuf
,
1123 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1124 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1127 void cx18_set_funcs(struct video_device
*vdev
)
1129 vdev
->ioctl_ops
= &cx18_ioctl_ops
;