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-chip-ident.h>
43 #include <linux/i2c-id.h>
45 u16 cx18_service2vbi(int type)
48 case V4L2_SLICED_TELETEXT_B:
49 return CX18_SLICED_TYPE_TELETEXT_B;
50 case V4L2_SLICED_CAPTION_525:
51 return CX18_SLICED_TYPE_CAPTION_525;
52 case V4L2_SLICED_WSS_625:
53 return CX18_SLICED_TYPE_WSS_625;
55 return CX18_SLICED_TYPE_VPS;
61 /* Check if VBI services are allowed on the (field, line) for the video std */
62 static int valid_service_line(int field, int line, int is_pal)
64 return (is_pal && line >= 6 &&
65 ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
66 (!is_pal && line >= 10 && line < 22);
70 * For a (field, line, std) and inbound potential set of services for that line,
71 * return the first valid service of those passed in the incoming set for that
72 * line in priority order:
73 * CC, VPS, or WSS over TELETEXT for well known lines
74 * TELETEXT, before VPS, before CC, before WSS, for other lines
76 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
78 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
81 set = set & valid_set;
82 if (set == 0 || !valid_service_line(field, line, is_pal))
85 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
86 return V4L2_SLICED_CAPTION_525;
88 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
89 return V4L2_SLICED_VPS;
90 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
91 return V4L2_SLICED_WSS_625;
95 for (i = 0; i < 32; i++) {
103 * Expand the service_set of *fmt into valid service_lines for the std,
104 * and clear the passed in fmt->service_set
106 void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
108 u16 set = fmt->service_set;
111 fmt->service_set = 0;
112 for (f = 0; f < 2; f++) {
113 for (l = 0; l < 24; l++)
114 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
119 * Sanitize the service_lines in *fmt per the video std, and return 1
120 * if any service_line is left as valid after santization
122 static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
127 for (f = 0; f < 2; f++) {
128 for (l = 0; l < 24; l++) {
129 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
130 set |= fmt->service_lines[f][l];
136 /* Compute the service_set from the assumed valid service_lines of *fmt */
137 u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
142 for (f = 0; f < 2; f++) {
143 for (l = 0; l < 24; l++)
144 set |= fmt->service_lines[f][l];
149 static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
150 struct v4l2_format *fmt)
152 struct cx18_open_id *id = fh;
153 struct cx18 *cx = id->cx;
154 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
156 pixfmt->width = cx->params.width;
157 pixfmt->height = cx->params.height;
158 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
159 pixfmt->field = V4L2_FIELD_INTERLACED;
161 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
162 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
163 /* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
164 pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
165 pixfmt->bytesperline = 720;
167 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
168 pixfmt->sizeimage = 128 * 1024;
169 pixfmt->bytesperline = 0;
174 static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
175 struct v4l2_format *fmt)
177 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
178 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
180 vbifmt->sampling_rate = 27000000;
181 vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
182 vbifmt->samples_per_line = vbi_active_samples - 4;
183 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
184 vbifmt->start[0] = cx->vbi.start[0];
185 vbifmt->start[1] = cx->vbi.start[1];
186 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
188 vbifmt->reserved[0] = 0;
189 vbifmt->reserved[1] = 0;
193 static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
194 struct v4l2_format *fmt)
196 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
197 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
199 /* sane, V4L2 spec compliant, defaults */
200 vbifmt->reserved[0] = 0;
201 vbifmt->reserved[1] = 0;
202 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
203 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
204 vbifmt->service_set = 0;
207 * Fetch the configured service_lines and total service_set from the
208 * digitizer/slicer. Note, cx18_av_vbi() wipes the passed in
209 * fmt->fmt.sliced under valid calling conditions
211 if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
214 /* Ensure V4L2 spec compliant output */
215 vbifmt->reserved[0] = 0;
216 vbifmt->reserved[1] = 0;
217 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
218 vbifmt->service_set = cx18_get_service_set(vbifmt);
222 static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
223 struct v4l2_format *fmt)
225 struct cx18_open_id *id = fh;
226 struct cx18 *cx = id->cx;
227 int w = fmt->fmt.pix.width;
228 int h = fmt->fmt.pix.height;
233 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
234 /* YUV height must be a multiple of 32 */
238 h = min(h, cx->is_50hz ? 576 : 480);
241 cx18_g_fmt_vid_cap(file, fh, fmt);
242 fmt->fmt.pix.width = w;
243 fmt->fmt.pix.height = h;
247 static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
248 struct v4l2_format *fmt)
250 return cx18_g_fmt_vbi_cap(file, fh, fmt);
253 static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
254 struct v4l2_format *fmt)
256 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
257 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
259 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
260 vbifmt->reserved[0] = 0;
261 vbifmt->reserved[1] = 0;
263 /* If given a service set, expand it validly & clear passed in set */
264 if (vbifmt->service_set)
265 cx18_expand_service_set(vbifmt, cx->is_50hz);
266 /* Sanitize the service_lines, and compute the new set if any valid */
267 if (check_service_set(vbifmt, cx->is_50hz))
268 vbifmt->service_set = cx18_get_service_set(vbifmt);
272 static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
273 struct v4l2_format *fmt)
275 struct cx18_open_id *id = fh;
276 struct cx18 *cx = id->cx;
277 struct v4l2_mbus_framefmt mbus_fmt;
281 ret = v4l2_prio_check(&cx->prio, id->prio);
285 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
288 w = fmt->fmt.pix.width;
289 h = fmt->fmt.pix.height;
291 if (cx->params.width == w && cx->params.height == h)
294 if (atomic_read(&cx->ana_capturing) > 0)
297 mbus_fmt.width = cx->params.width = w;
298 mbus_fmt.height = cx->params.height = h;
299 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
300 v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &mbus_fmt);
301 return cx18_g_fmt_vid_cap(file, fh, fmt);
304 static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
305 struct v4l2_format *fmt)
307 struct cx18_open_id *id = fh;
308 struct cx18 *cx = id->cx;
311 ret = v4l2_prio_check(&cx->prio, id->prio);
316 * Changing the Encoder's Raw VBI parameters won't have any effect
317 * if any analog capture is ongoing
319 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
323 * Set the digitizer registers for raw active VBI.
324 * Note cx18_av_vbi_wipes out alot of the passed in fmt under valid
327 ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
331 /* Store our new v4l2 (non-)sliced VBI state */
332 cx->vbi.sliced_in->service_set = 0;
333 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
335 return cx18_g_fmt_vbi_cap(file, fh, fmt);
338 static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
339 struct v4l2_format *fmt)
341 struct cx18_open_id *id = fh;
342 struct cx18 *cx = id->cx;
344 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
346 ret = v4l2_prio_check(&cx->prio, id->prio);
350 cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
353 * Changing the Encoder's Raw VBI parameters won't have any effect
354 * if any analog capture is ongoing
356 if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
360 * Set the service_lines requested in the digitizer/slicer registers.
361 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
362 * passed in fmt->fmt.sliced under valid calling conditions
364 ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
367 /* Store our current v4l2 sliced VBI settings */
368 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
369 memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
373 static int cx18_g_chip_ident(struct file *file, void *fh,
374 struct v4l2_dbg_chip_ident *chip)
376 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
379 chip->ident = V4L2_IDENT_NONE;
381 switch (chip->match.type) {
382 case V4L2_CHIP_MATCH_HOST:
383 switch (chip->match.addr) {
385 chip->ident = V4L2_IDENT_CX23418;
386 chip->revision = cx18_read_reg(cx, 0xC72028);
390 * The A/V decoder is always present, but in the rare
391 * case that the card doesn't have analog, we don't
392 * use it. We find it w/o using the cx->sd_av pointer
394 cx18_call_hw(cx, CX18_HW_418_AV,
395 core, g_chip_ident, chip);
399 * Could return ident = V4L2_IDENT_UNKNOWN if we had
400 * other host chips at higher addresses, but we don't
402 err = -EINVAL; /* per V4L2 spec */
406 case V4L2_CHIP_MATCH_I2C_DRIVER:
407 /* If needed, returns V4L2_IDENT_AMBIGUOUS without extra work */
408 cx18_call_all(cx, core, g_chip_ident, chip);
410 case V4L2_CHIP_MATCH_I2C_ADDR:
412 * We could return V4L2_IDENT_UNKNOWN, but we don't do the work
413 * to look if a chip is at the address with no driver. That's a
414 * dangerous thing to do with EEPROMs anyway.
416 cx18_call_all(cx, core, g_chip_ident, chip);
425 #ifdef CONFIG_VIDEO_ADV_DEBUG
426 static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
428 struct v4l2_dbg_register *regs = arg;
430 if (!capable(CAP_SYS_ADMIN))
432 if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
436 if (cmd == VIDIOC_DBG_S_REGISTER)
437 cx18_write_enc(cx, regs->val, regs->reg);
439 regs->val = cx18_read_enc(cx, regs->reg);
443 static int cx18_g_register(struct file *file, void *fh,
444 struct v4l2_dbg_register *reg)
446 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
448 if (v4l2_chip_match_host(®->match))
449 return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
450 /* FIXME - errors shouldn't be ignored */
451 cx18_call_all(cx, core, g_register, reg);
455 static int cx18_s_register(struct file *file, void *fh,
456 struct v4l2_dbg_register *reg)
458 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
460 if (v4l2_chip_match_host(®->match))
461 return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
462 /* FIXME - errors shouldn't be ignored */
463 cx18_call_all(cx, core, s_register, reg);
468 static int cx18_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
470 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
472 *p = v4l2_prio_max(&cx->prio);
476 static int cx18_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
478 struct cx18_open_id *id = fh;
479 struct cx18 *cx = id->cx;
481 return v4l2_prio_change(&cx->prio, &id->prio, prio);
484 static int cx18_querycap(struct file *file, void *fh,
485 struct v4l2_capability *vcap)
487 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
489 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
490 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
491 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
492 "PCI:%s", pci_name(cx->pci_dev));
493 vcap->version = CX18_DRIVER_VERSION; /* version */
494 vcap->capabilities = cx->v4l2_cap; /* capabilities */
498 static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
500 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
502 return cx18_get_audio_input(cx, vin->index, vin);
505 static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
507 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
509 vin->index = cx->audio_input;
510 return cx18_get_audio_input(cx, vin->index, vin);
513 static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
515 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
517 if (vout->index >= cx->nof_audio_inputs)
519 cx->audio_input = vout->index;
520 cx18_audio_set_io(cx);
524 static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
526 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
528 /* set it to defaults from our table */
529 return cx18_get_input(cx, vin->index, vin);
532 static int cx18_cropcap(struct file *file, void *fh,
533 struct v4l2_cropcap *cropcap)
535 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
537 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
539 cropcap->bounds.top = cropcap->bounds.left = 0;
540 cropcap->bounds.width = 720;
541 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
542 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
543 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
544 cropcap->defrect = cropcap->bounds;
548 static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
550 struct cx18_open_id *id = fh;
551 struct cx18 *cx = id->cx;
554 ret = v4l2_prio_check(&cx->prio, id->prio);
558 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
560 CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
564 static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
566 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
568 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
570 CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
574 static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
575 struct v4l2_fmtdesc *fmt)
577 static struct v4l2_fmtdesc formats[] = {
578 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
579 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
581 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
582 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
588 *fmt = formats[fmt->index];
592 static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
594 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
596 *i = cx->active_input;
600 int cx18_s_input(struct file *file, void *fh, unsigned int inp)
602 struct cx18_open_id *id = fh;
603 struct cx18 *cx = id->cx;
606 ret = v4l2_prio_check(&cx->prio, id->prio);
610 if (inp >= cx->nof_inputs)
613 if (inp == cx->active_input) {
614 CX18_DEBUG_INFO("Input unchanged\n");
618 CX18_DEBUG_INFO("Changing input from %d to %d\n",
619 cx->active_input, inp);
621 cx->active_input = inp;
622 /* Set the audio input to whatever is appropriate for the input type. */
623 cx->audio_input = cx->card->video_inputs[inp].audio_index;
625 /* prevent others from messing with the streams until
626 we're finished changing inputs. */
628 cx18_video_set_io(cx);
629 cx18_audio_set_io(cx);
634 static int cx18_g_frequency(struct file *file, void *fh,
635 struct v4l2_frequency *vf)
637 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
642 cx18_call_all(cx, tuner, g_frequency, vf);
646 int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
648 struct cx18_open_id *id = fh;
649 struct cx18 *cx = id->cx;
652 ret = v4l2_prio_check(&cx->prio, id->prio);
660 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
661 cx18_call_all(cx, tuner, s_frequency, vf);
666 static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
668 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
674 int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
676 struct cx18_open_id *id = fh;
677 struct cx18 *cx = id->cx;
680 ret = v4l2_prio_check(&cx->prio, id->prio);
684 if ((*std & V4L2_STD_ALL) == 0)
690 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
691 atomic_read(&cx->ana_capturing) > 0) {
692 /* Switching standard would turn off the radio or mess
693 with already running streams, prevent that by
699 cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
700 cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
701 cx->params.width = 720;
702 cx->params.height = cx->is_50hz ? 576 : 480;
703 cx->vbi.count = cx->is_50hz ? 18 : 12;
704 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
705 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
706 CX18_DEBUG_INFO("Switching standard to %llx.\n",
707 (unsigned long long) cx->std);
710 cx18_call_all(cx, core, s_std, cx->std);
714 static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
716 struct cx18_open_id *id = fh;
717 struct cx18 *cx = id->cx;
720 ret = v4l2_prio_check(&cx->prio, id->prio);
727 cx18_call_all(cx, tuner, s_tuner, vt);
731 static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
733 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
738 cx18_call_all(cx, tuner, g_tuner, vt);
740 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
741 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
742 vt->type = V4L2_TUNER_RADIO;
744 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
745 vt->type = V4L2_TUNER_ANALOG_TV;
751 static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
752 struct v4l2_sliced_vbi_cap *cap)
754 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
755 int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
758 if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
761 cap->service_set = 0;
762 for (f = 0; f < 2; f++) {
763 for (l = 0; l < 24; l++) {
764 if (valid_service_line(f, l, cx->is_50hz)) {
766 * We can find all v4l2 supported vbi services
767 * for the standard, on a valid line for the std
769 cap->service_lines[f][l] = set;
770 cap->service_set |= set;
772 cap->service_lines[f][l] = 0;
775 for (f = 0; f < 3; f++)
776 cap->reserved[f] = 0;
780 static int _cx18_process_idx_data(struct cx18_buffer *buf,
781 struct v4l2_enc_idx *idx)
783 int consumed, remaining;
784 struct v4l2_enc_idx_entry *e_idx;
785 struct cx18_enc_idx_entry *e_buf;
787 /* Frame type lookup: 1=I, 2=P, 4=B */
788 const int mapping[8] = {
789 -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
790 -1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
794 * Assumption here is that a buf holds an integral number of
795 * struct cx18_enc_idx_entry objects and is properly aligned.
796 * This is enforced by the module options on IDX buffer sizes.
798 remaining = buf->bytesused - buf->readpos;
800 e_idx = &idx->entry[idx->entries];
801 e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
803 while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
804 idx->entries < V4L2_ENC_IDX_ENTRIES) {
806 e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
807 | le32_to_cpu(e_buf->offset_low);
809 e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
810 | le32_to_cpu(e_buf->pts_low);
812 e_idx->length = le32_to_cpu(e_buf->length);
814 e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
816 e_idx->reserved[0] = 0;
817 e_idx->reserved[1] = 0;
820 e_idx = &idx->entry[idx->entries];
823 remaining -= sizeof(struct cx18_enc_idx_entry);
824 consumed += sizeof(struct cx18_enc_idx_entry);
827 /* Swallow any partial entries at the end, if there are any */
828 if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
829 consumed += remaining;
831 buf->readpos += consumed;
835 static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
836 struct v4l2_enc_idx *idx)
838 if (s->type != CX18_ENC_STREAM_TYPE_IDX)
841 if (mdl->curr_buf == NULL)
842 mdl->curr_buf = list_first_entry(&mdl->buf_list,
843 struct cx18_buffer, list);
845 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
847 * For some reason we've exhausted the buffers, but the MDL
848 * object still said some data was unread.
849 * Fix that and bail out.
851 mdl->readpos = mdl->bytesused;
855 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
857 /* Skip any empty buffers in the MDL */
858 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
861 mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
863 /* exit when MDL drained or request satisfied */
864 if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
865 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
866 mdl->readpos >= mdl->bytesused)
872 static int cx18_g_enc_index(struct file *file, void *fh,
873 struct v4l2_enc_idx *idx)
875 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
876 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
878 struct cx18_mdl *mdl;
880 if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
883 /* Compute the best case number of entries we can buffer */
885 s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
888 tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
890 /* Fill out the header of the return structure */
892 idx->entries_cap = tmp;
893 memset(idx->reserved, 0, sizeof(idx->reserved));
895 /* Pull IDX MDLs and buffers from q_full and populate the entries */
897 mdl = cx18_dequeue(s, &s->q_full);
898 if (mdl == NULL) /* No more IDX data right now */
901 /* Extract the Index entry data from the MDL and buffers */
902 cx18_process_idx_data(s, mdl, idx);
903 if (mdl->readpos < mdl->bytesused) {
904 /* We finished with data remaining, push the MDL back */
905 cx18_push(s, mdl, &s->q_full);
909 /* We drained this MDL, schedule it to go to the firmware */
910 cx18_enqueue(s, mdl, &s->q_free);
912 } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
914 /* Tell the work handler to send free IDX MDLs to the firmware */
915 cx18_stream_load_fw_queue(s);
919 static int cx18_encoder_cmd(struct file *file, void *fh,
920 struct v4l2_encoder_cmd *enc)
922 struct cx18_open_id *id = fh;
923 struct cx18 *cx = id->cx;
927 case V4L2_ENC_CMD_START:
928 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
930 return cx18_start_capture(id);
932 case V4L2_ENC_CMD_STOP:
933 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
934 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
935 cx18_stop_capture(id,
936 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
939 case V4L2_ENC_CMD_PAUSE:
940 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
942 if (!atomic_read(&cx->ana_capturing))
944 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
946 h = cx18_find_handle(cx);
947 if (h == CX18_INVALID_TASK_HANDLE) {
948 CX18_ERR("Can't find valid task handle for "
949 "V4L2_ENC_CMD_PAUSE\n");
953 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
956 case V4L2_ENC_CMD_RESUME:
957 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
959 if (!atomic_read(&cx->ana_capturing))
961 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
963 h = cx18_find_handle(cx);
964 if (h == CX18_INVALID_TASK_HANDLE) {
965 CX18_ERR("Can't find valid task handle for "
966 "V4L2_ENC_CMD_RESUME\n");
969 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
974 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
980 static int cx18_try_encoder_cmd(struct file *file, void *fh,
981 struct v4l2_encoder_cmd *enc)
983 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
986 case V4L2_ENC_CMD_START:
987 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
991 case V4L2_ENC_CMD_STOP:
992 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
993 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
996 case V4L2_ENC_CMD_PAUSE:
997 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1001 case V4L2_ENC_CMD_RESUME:
1002 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1007 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1013 static int cx18_log_status(struct file *file, void *fh)
1015 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1016 struct v4l2_input vidin;
1017 struct v4l2_audio audin;
1020 CX18_INFO("================= START STATUS CARD #%d "
1021 "=================\n", cx->instance);
1022 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
1023 if (cx->hw_flags & CX18_HW_TVEEPROM) {
1026 cx18_read_eeprom(cx, &tv);
1028 cx18_call_all(cx, core, log_status);
1029 cx18_get_input(cx, cx->active_input, &vidin);
1030 cx18_get_audio_input(cx, cx->audio_input, &audin);
1031 CX18_INFO("Video Input: %s\n", vidin.name);
1032 CX18_INFO("Audio Input: %s\n", audin.name);
1033 mutex_lock(&cx->gpio_lock);
1034 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
1035 cx->gpio_dir, cx->gpio_val);
1036 mutex_unlock(&cx->gpio_lock);
1037 CX18_INFO("Tuner: %s\n",
1038 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
1039 cx2341x_log_status(&cx->params, cx->v4l2_dev.name);
1040 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
1041 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1042 struct cx18_stream *s = &cx->streams[i];
1044 if (s->video_dev == NULL || s->buffers == 0)
1046 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1047 s->name, s->s_flags,
1048 atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
1050 (s->buffers * s->buf_size) / 1024, s->buffers);
1052 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1053 (long long)cx->mpg_data_received,
1054 (long long)cx->vbi_data_inserted);
1055 CX18_INFO("================== END STATUS CARD #%d "
1056 "==================\n", cx->instance);
1060 static long cx18_default(struct file *file, void *fh, int cmd, void *arg)
1062 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1065 case VIDIOC_INT_RESET: {
1066 u32 val = *(u32 *)arg;
1068 if ((val == 0) || (val & 0x01))
1069 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1070 (u32) CX18_GPIO_RESET_Z8F0811);
1080 long cx18_v4l2_ioctl(struct file *filp, unsigned int cmd,
1083 struct video_device *vfd = video_devdata(filp);
1084 struct cx18_open_id *id = filp->private_data;
1085 struct cx18 *cx = id->cx;
1088 mutex_lock(&cx->serialize_lock);
1090 /* FIXME - consolidate v4l2_prio_check()'s here */
1092 if (cx18_debug & CX18_DBGFLG_IOCTL)
1093 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
1094 res = video_ioctl2(filp, cmd, arg);
1096 mutex_unlock(&cx->serialize_lock);
1100 static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1101 .vidioc_querycap = cx18_querycap,
1102 .vidioc_g_priority = cx18_g_priority,
1103 .vidioc_s_priority = cx18_s_priority,
1104 .vidioc_s_audio = cx18_s_audio,
1105 .vidioc_g_audio = cx18_g_audio,
1106 .vidioc_enumaudio = cx18_enumaudio,
1107 .vidioc_enum_input = cx18_enum_input,
1108 .vidioc_cropcap = cx18_cropcap,
1109 .vidioc_s_crop = cx18_s_crop,
1110 .vidioc_g_crop = cx18_g_crop,
1111 .vidioc_g_input = cx18_g_input,
1112 .vidioc_s_input = cx18_s_input,
1113 .vidioc_g_frequency = cx18_g_frequency,
1114 .vidioc_s_frequency = cx18_s_frequency,
1115 .vidioc_s_tuner = cx18_s_tuner,
1116 .vidioc_g_tuner = cx18_g_tuner,
1117 .vidioc_g_enc_index = cx18_g_enc_index,
1118 .vidioc_g_std = cx18_g_std,
1119 .vidioc_s_std = cx18_s_std,
1120 .vidioc_log_status = cx18_log_status,
1121 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
1122 .vidioc_encoder_cmd = cx18_encoder_cmd,
1123 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
1124 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
1125 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
1126 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
1127 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
1128 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
1129 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
1130 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
1131 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
1132 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
1133 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
1134 .vidioc_g_chip_ident = cx18_g_chip_ident,
1135 #ifdef CONFIG_VIDEO_ADV_DEBUG
1136 .vidioc_g_register = cx18_g_register,
1137 .vidioc_s_register = cx18_s_register,
1139 .vidioc_default = cx18_default,
1140 .vidioc_queryctrl = cx18_queryctrl,
1141 .vidioc_querymenu = cx18_querymenu,
1142 .vidioc_g_ext_ctrls = cx18_g_ext_ctrls,
1143 .vidioc_s_ext_ctrls = cx18_s_ext_ctrls,
1144 .vidioc_try_ext_ctrls = cx18_try_ext_ctrls,
1147 void cx18_set_funcs(struct video_device *vdev)
1149 vdev->ioctl_ops = &cx18_ioctl_ops;