1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "cx18-driver.h"
21#include "cx18-io.h"
22#include "cx18-version.h"
23#include "cx18-mailbox.h"
24#include "cx18-i2c.h"
25#include "cx18-queue.h"
26#include "cx18-fileops.h"
27#include "cx18-vbi.h"
28#include "cx18-audio.h"
29#include "cx18-video.h"
30#include "cx18-streams.h"
31#include "cx18-ioctl.h"
32#include "cx18-gpio.h"
33#include "cx18-controls.h"
34#include "cx18-cards.h"
35#include "cx18-av-core.h"
36#include <media/tveeprom.h>
37#include <media/v4l2-event.h>
38
39u16 cx18_service2vbi(int type)
40{
41 switch (type) {
42 case V4L2_SLICED_TELETEXT_B:
43 return CX18_SLICED_TYPE_TELETEXT_B;
44 case V4L2_SLICED_CAPTION_525:
45 return CX18_SLICED_TYPE_CAPTION_525;
46 case V4L2_SLICED_WSS_625:
47 return CX18_SLICED_TYPE_WSS_625;
48 case V4L2_SLICED_VPS:
49 return CX18_SLICED_TYPE_VPS;
50 default:
51 return 0;
52 }
53}
54
55
56static int valid_service_line(int field, int line, int is_pal)
57{
58 return (is_pal && line >= 6 &&
59 ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
60 (!is_pal && line >= 10 && line < 22);
61}
62
63
64
65
66
67
68
69
70static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
71{
72 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
73 int i;
74
75 set = set & valid_set;
76 if (set == 0 || !valid_service_line(field, line, is_pal))
77 return 0;
78 if (!is_pal) {
79 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
80 return V4L2_SLICED_CAPTION_525;
81 } else {
82 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
83 return V4L2_SLICED_VPS;
84 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
85 return V4L2_SLICED_WSS_625;
86 if (line == 23)
87 return 0;
88 }
89 for (i = 0; i < 32; i++) {
90 if ((1 << i) & set)
91 return 1 << i;
92 }
93 return 0;
94}
95
96
97
98
99
100void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
101{
102 u16 set = fmt->service_set;
103 int f, l;
104
105 fmt->service_set = 0;
106 for (f = 0; f < 2; f++) {
107 for (l = 0; l < 24; l++)
108 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
109 }
110}
111
112
113
114
115
116static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
117{
118 int f, l;
119 u16 set = 0;
120
121 for (f = 0; f < 2; f++) {
122 for (l = 0; l < 24; l++) {
123 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
124 set |= fmt->service_lines[f][l];
125 }
126 }
127 return set != 0;
128}
129
130
131u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
132{
133 int f, l;
134 u16 set = 0;
135
136 for (f = 0; f < 2; f++) {
137 for (l = 0; l < 24; l++)
138 set |= fmt->service_lines[f][l];
139 }
140 return set;
141}
142
143static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
144 struct v4l2_format *fmt)
145{
146 struct cx18_open_id *id = fh2id(fh);
147 struct cx18 *cx = id->cx;
148 struct cx18_stream *s = &cx->streams[id->type];
149 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
150
151 pixfmt->width = cx->cxhdl.width;
152 pixfmt->height = cx->cxhdl.height;
153 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
154 pixfmt->field = V4L2_FIELD_INTERLACED;
155 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
156 pixfmt->pixelformat = s->pixelformat;
157 pixfmt->sizeimage = s->vb_bytes_per_frame;
158 pixfmt->bytesperline = s->vb_bytes_per_line;
159 } else {
160 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
161 pixfmt->sizeimage = 128 * 1024;
162 pixfmt->bytesperline = 0;
163 }
164 return 0;
165}
166
167static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
168 struct v4l2_format *fmt)
169{
170 struct cx18 *cx = fh2id(fh)->cx;
171 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
172
173 vbifmt->sampling_rate = 27000000;
174 vbifmt->offset = 248;
175 vbifmt->samples_per_line = VBI_ACTIVE_SAMPLES - 4;
176 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
177 vbifmt->start[0] = cx->vbi.start[0];
178 vbifmt->start[1] = cx->vbi.start[1];
179 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
180 vbifmt->flags = 0;
181 vbifmt->reserved[0] = 0;
182 vbifmt->reserved[1] = 0;
183 return 0;
184}
185
186static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
187 struct v4l2_format *fmt)
188{
189 struct cx18 *cx = fh2id(fh)->cx;
190 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
191
192
193 vbifmt->reserved[0] = 0;
194 vbifmt->reserved[1] = 0;
195 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
196 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
197 vbifmt->service_set = 0;
198
199
200
201
202
203
204 if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
205 return -EINVAL;
206
207 vbifmt->service_set = cx18_get_service_set(vbifmt);
208 return 0;
209}
210
211static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
212 struct v4l2_format *fmt)
213{
214 struct cx18_open_id *id = fh2id(fh);
215 struct cx18 *cx = id->cx;
216 int w = fmt->fmt.pix.width;
217 int h = fmt->fmt.pix.height;
218 int min_h = 2;
219
220 w = min(w, 720);
221 w = max(w, 2);
222 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
223
224 h &= ~0x1f;
225 min_h = 32;
226 }
227 h = min(h, cx->is_50hz ? 576 : 480);
228 h = max(h, min_h);
229
230 fmt->fmt.pix.width = w;
231 fmt->fmt.pix.height = h;
232 return 0;
233}
234
235static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
236 struct v4l2_format *fmt)
237{
238 return cx18_g_fmt_vbi_cap(file, fh, fmt);
239}
240
241static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
242 struct v4l2_format *fmt)
243{
244 struct cx18 *cx = fh2id(fh)->cx;
245 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
246
247 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
248 vbifmt->reserved[0] = 0;
249 vbifmt->reserved[1] = 0;
250
251
252 if (vbifmt->service_set)
253 cx18_expand_service_set(vbifmt, cx->is_50hz);
254
255 if (check_service_set(vbifmt, cx->is_50hz))
256 vbifmt->service_set = cx18_get_service_set(vbifmt);
257 return 0;
258}
259
260static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
261 struct v4l2_format *fmt)
262{
263 struct cx18_open_id *id = fh2id(fh);
264 struct cx18 *cx = id->cx;
265 struct v4l2_subdev_format format = {
266 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
267 };
268 struct cx18_stream *s = &cx->streams[id->type];
269 int ret;
270 int w, h;
271
272 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
273 if (ret)
274 return ret;
275 w = fmt->fmt.pix.width;
276 h = fmt->fmt.pix.height;
277
278 if (cx->cxhdl.width == w && cx->cxhdl.height == h &&
279 s->pixelformat == fmt->fmt.pix.pixelformat)
280 return 0;
281
282 if (atomic_read(&cx->ana_capturing) > 0)
283 return -EBUSY;
284
285 s->pixelformat = fmt->fmt.pix.pixelformat;
286
287
288 if (s->pixelformat == V4L2_PIX_FMT_HM12) {
289 s->vb_bytes_per_frame = h * 720 * 3 / 2;
290 s->vb_bytes_per_line = 720;
291 } else {
292 s->vb_bytes_per_frame = h * 720 * 2;
293 s->vb_bytes_per_line = 1440;
294 }
295
296 format.format.width = cx->cxhdl.width = w;
297 format.format.height = cx->cxhdl.height = h;
298 format.format.code = MEDIA_BUS_FMT_FIXED;
299 v4l2_subdev_call(cx->sd_av, pad, set_fmt, NULL, &format);
300 return cx18_g_fmt_vid_cap(file, fh, fmt);
301}
302
303static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
304 struct v4l2_format *fmt)
305{
306 struct cx18_open_id *id = fh2id(fh);
307 struct cx18 *cx = id->cx;
308 int ret;
309
310
311
312
313
314 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
315 return -EBUSY;
316
317
318
319
320
321
322 ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
323 if (ret)
324 return ret;
325
326
327 cx->vbi.sliced_in->service_set = 0;
328 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
329
330 return cx18_g_fmt_vbi_cap(file, fh, fmt);
331}
332
333static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
334 struct v4l2_format *fmt)
335{
336 struct cx18_open_id *id = fh2id(fh);
337 struct cx18 *cx = id->cx;
338 int ret;
339 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
340
341 cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
342
343
344
345
346
347 if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
348 return -EBUSY;
349
350
351
352
353
354
355 ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
356 if (ret)
357 return ret;
358
359 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
360 memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
361 return 0;
362}
363
364#ifdef CONFIG_VIDEO_ADV_DEBUG
365static int cx18_g_register(struct file *file, void *fh,
366 struct v4l2_dbg_register *reg)
367{
368 struct cx18 *cx = fh2id(fh)->cx;
369
370 if (reg->reg & 0x3)
371 return -EINVAL;
372 if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
373 return -EINVAL;
374 reg->size = 4;
375 reg->val = cx18_read_enc(cx, reg->reg);
376 return 0;
377}
378
379static int cx18_s_register(struct file *file, void *fh,
380 const struct v4l2_dbg_register *reg)
381{
382 struct cx18 *cx = fh2id(fh)->cx;
383
384 if (reg->reg & 0x3)
385 return -EINVAL;
386 if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
387 return -EINVAL;
388 cx18_write_enc(cx, reg->val, reg->reg);
389 return 0;
390}
391#endif
392
393static int cx18_querycap(struct file *file, void *fh,
394 struct v4l2_capability *vcap)
395{
396 struct cx18_open_id *id = fh2id(fh);
397 struct cx18_stream *s = video_drvdata(file);
398 struct cx18 *cx = id->cx;
399
400 strscpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
401 strscpy(vcap->card, cx->card_name, sizeof(vcap->card));
402 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
403 "PCI:%s", pci_name(cx->pci_dev));
404 vcap->capabilities = cx->v4l2_cap;
405 vcap->device_caps = s->v4l2_dev_caps;
406 vcap->capabilities |= V4L2_CAP_DEVICE_CAPS;
407 return 0;
408}
409
410static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
411{
412 struct cx18 *cx = fh2id(fh)->cx;
413
414 return cx18_get_audio_input(cx, vin->index, vin);
415}
416
417static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
418{
419 struct cx18 *cx = fh2id(fh)->cx;
420
421 vin->index = cx->audio_input;
422 return cx18_get_audio_input(cx, vin->index, vin);
423}
424
425static int cx18_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
426{
427 struct cx18 *cx = fh2id(fh)->cx;
428
429 if (vout->index >= cx->nof_audio_inputs)
430 return -EINVAL;
431 cx->audio_input = vout->index;
432 cx18_audio_set_io(cx);
433 return 0;
434}
435
436static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
437{
438 struct cx18 *cx = fh2id(fh)->cx;
439
440
441 return cx18_get_input(cx, vin->index, vin);
442}
443
444static int cx18_g_pixelaspect(struct file *file, void *fh,
445 int type, struct v4l2_fract *f)
446{
447 struct cx18 *cx = fh2id(fh)->cx;
448
449 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
450 return -EINVAL;
451
452 f->numerator = cx->is_50hz ? 54 : 11;
453 f->denominator = cx->is_50hz ? 59 : 10;
454 return 0;
455}
456
457static int cx18_g_selection(struct file *file, void *fh,
458 struct v4l2_selection *sel)
459{
460 struct cx18 *cx = fh2id(fh)->cx;
461
462 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
463 return -EINVAL;
464 switch (sel->target) {
465 case V4L2_SEL_TGT_CROP_BOUNDS:
466 case V4L2_SEL_TGT_CROP_DEFAULT:
467 sel->r.top = sel->r.left = 0;
468 sel->r.width = 720;
469 sel->r.height = cx->is_50hz ? 576 : 480;
470 break;
471 default:
472 return -EINVAL;
473 }
474 return 0;
475}
476
477static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
478 struct v4l2_fmtdesc *fmt)
479{
480 static const struct v4l2_fmtdesc formats[] = {
481 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
482 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
483 },
484 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
485 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
486 },
487 { 2, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
488 "UYVY 4:2:2", V4L2_PIX_FMT_UYVY, { 0, 0, 0, 0 }
489 },
490 };
491
492 if (fmt->index > ARRAY_SIZE(formats) - 1)
493 return -EINVAL;
494 *fmt = formats[fmt->index];
495 return 0;
496}
497
498static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
499{
500 struct cx18 *cx = fh2id(fh)->cx;
501
502 *i = cx->active_input;
503 return 0;
504}
505
506int cx18_s_input(struct file *file, void *fh, unsigned int inp)
507{
508 struct cx18_open_id *id = fh2id(fh);
509 struct cx18 *cx = id->cx;
510 v4l2_std_id std = V4L2_STD_ALL;
511 const struct cx18_card_video_input *card_input =
512 cx->card->video_inputs + inp;
513
514 if (inp >= cx->nof_inputs)
515 return -EINVAL;
516
517 if (inp == cx->active_input) {
518 CX18_DEBUG_INFO("Input unchanged\n");
519 return 0;
520 }
521
522 CX18_DEBUG_INFO("Changing input from %d to %d\n",
523 cx->active_input, inp);
524
525 cx->active_input = inp;
526
527 cx->audio_input = cx->card->video_inputs[inp].audio_index;
528 if (card_input->video_type == V4L2_INPUT_TYPE_TUNER)
529 std = cx->tuner_std;
530 cx->streams[CX18_ENC_STREAM_TYPE_MPG].video_dev.tvnorms = std;
531 cx->streams[CX18_ENC_STREAM_TYPE_YUV].video_dev.tvnorms = std;
532 cx->streams[CX18_ENC_STREAM_TYPE_VBI].video_dev.tvnorms = std;
533
534
535
536 cx18_mute(cx);
537 cx18_video_set_io(cx);
538 cx18_audio_set_io(cx);
539 cx18_unmute(cx);
540 return 0;
541}
542
543static int cx18_g_frequency(struct file *file, void *fh,
544 struct v4l2_frequency *vf)
545{
546 struct cx18 *cx = fh2id(fh)->cx;
547
548 if (vf->tuner != 0)
549 return -EINVAL;
550
551 cx18_call_all(cx, tuner, g_frequency, vf);
552 return 0;
553}
554
555int cx18_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
556{
557 struct cx18_open_id *id = fh2id(fh);
558 struct cx18 *cx = id->cx;
559
560 if (vf->tuner != 0)
561 return -EINVAL;
562
563 cx18_mute(cx);
564 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
565 cx18_call_all(cx, tuner, s_frequency, vf);
566 cx18_unmute(cx);
567 return 0;
568}
569
570static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
571{
572 struct cx18 *cx = fh2id(fh)->cx;
573
574 *std = cx->std;
575 return 0;
576}
577
578int cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
579{
580 struct cx18_open_id *id = fh2id(fh);
581 struct cx18 *cx = id->cx;
582
583 if ((std & V4L2_STD_ALL) == 0)
584 return -EINVAL;
585
586 if (std == cx->std)
587 return 0;
588
589 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
590 atomic_read(&cx->ana_capturing) > 0) {
591
592
593
594 return -EBUSY;
595 }
596
597 cx->std = std;
598 cx->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
599 cx->is_50hz = !cx->is_60hz;
600 cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
601 cx->cxhdl.width = 720;
602 cx->cxhdl.height = cx->is_50hz ? 576 : 480;
603 cx->vbi.count = cx->is_50hz ? 18 : 12;
604 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
605 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
606 CX18_DEBUG_INFO("Switching standard to %llx.\n",
607 (unsigned long long) cx->std);
608
609
610 cx18_call_all(cx, video, s_std, cx->std);
611 return 0;
612}
613
614static int cx18_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
615{
616 struct cx18_open_id *id = fh2id(fh);
617 struct cx18 *cx = id->cx;
618
619 if (vt->index != 0)
620 return -EINVAL;
621
622 cx18_call_all(cx, tuner, s_tuner, vt);
623 return 0;
624}
625
626static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
627{
628 struct cx18 *cx = fh2id(fh)->cx;
629
630 if (vt->index != 0)
631 return -EINVAL;
632
633 cx18_call_all(cx, tuner, g_tuner, vt);
634
635 if (vt->type == V4L2_TUNER_RADIO)
636 strscpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
637 else
638 strscpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
639 return 0;
640}
641
642static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
643 struct v4l2_sliced_vbi_cap *cap)
644{
645 struct cx18 *cx = fh2id(fh)->cx;
646 int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
647 int f, l;
648
649 if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
650 return -EINVAL;
651
652 cap->service_set = 0;
653 for (f = 0; f < 2; f++) {
654 for (l = 0; l < 24; l++) {
655 if (valid_service_line(f, l, cx->is_50hz)) {
656
657
658
659
660 cap->service_lines[f][l] = set;
661 cap->service_set |= set;
662 } else
663 cap->service_lines[f][l] = 0;
664 }
665 }
666 for (f = 0; f < 3; f++)
667 cap->reserved[f] = 0;
668 return 0;
669}
670
671static int _cx18_process_idx_data(struct cx18_buffer *buf,
672 struct v4l2_enc_idx *idx)
673{
674 int consumed, remaining;
675 struct v4l2_enc_idx_entry *e_idx;
676 struct cx18_enc_idx_entry *e_buf;
677
678
679 const int mapping[8] = {
680 -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
681 -1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
682 };
683
684
685
686
687
688
689 remaining = buf->bytesused - buf->readpos;
690 consumed = 0;
691 e_idx = &idx->entry[idx->entries];
692 e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
693
694 while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
695 idx->entries < V4L2_ENC_IDX_ENTRIES) {
696
697 e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
698 | le32_to_cpu(e_buf->offset_low);
699
700 e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
701 | le32_to_cpu(e_buf->pts_low);
702
703 e_idx->length = le32_to_cpu(e_buf->length);
704
705 e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
706
707 e_idx->reserved[0] = 0;
708 e_idx->reserved[1] = 0;
709
710 idx->entries++;
711 e_idx = &idx->entry[idx->entries];
712 e_buf++;
713
714 remaining -= sizeof(struct cx18_enc_idx_entry);
715 consumed += sizeof(struct cx18_enc_idx_entry);
716 }
717
718
719 if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
720 consumed += remaining;
721
722 buf->readpos += consumed;
723 return consumed;
724}
725
726static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
727 struct v4l2_enc_idx *idx)
728{
729 if (s->type != CX18_ENC_STREAM_TYPE_IDX)
730 return -EINVAL;
731
732 if (mdl->curr_buf == NULL)
733 mdl->curr_buf = list_first_entry(&mdl->buf_list,
734 struct cx18_buffer, list);
735
736 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
737
738
739
740
741
742 mdl->readpos = mdl->bytesused;
743 return 0;
744 }
745
746 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
747
748
749 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
750 continue;
751
752 mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
753
754
755 if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
756 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
757 mdl->readpos >= mdl->bytesused)
758 break;
759 }
760 return 0;
761}
762
763static int cx18_g_enc_index(struct file *file, void *fh,
764 struct v4l2_enc_idx *idx)
765{
766 struct cx18 *cx = fh2id(fh)->cx;
767 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
768 s32 tmp;
769 struct cx18_mdl *mdl;
770
771 if (!cx18_stream_enabled(s))
772 return -EINVAL;
773
774
775 tmp = s->buffers -
776 s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
777 if (tmp <= 0)
778 tmp = 1;
779 tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
780
781
782 idx->entries = 0;
783 idx->entries_cap = tmp;
784 memset(idx->reserved, 0, sizeof(idx->reserved));
785
786
787 do {
788 mdl = cx18_dequeue(s, &s->q_full);
789 if (mdl == NULL)
790 break;
791
792
793 cx18_process_idx_data(s, mdl, idx);
794 if (mdl->readpos < mdl->bytesused) {
795
796 cx18_push(s, mdl, &s->q_full);
797 break;
798 }
799
800
801 cx18_enqueue(s, mdl, &s->q_free);
802
803 } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
804
805
806 cx18_stream_load_fw_queue(s);
807 return 0;
808}
809
810static struct videobuf_queue *cx18_vb_queue(struct cx18_open_id *id)
811{
812 struct videobuf_queue *q = NULL;
813 struct cx18 *cx = id->cx;
814 struct cx18_stream *s = &cx->streams[id->type];
815
816 switch (s->vb_type) {
817 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
818 q = &s->vbuf_q;
819 break;
820 case V4L2_BUF_TYPE_VBI_CAPTURE:
821 break;
822 default:
823 break;
824 }
825 return q;
826}
827
828static int cx18_streamon(struct file *file, void *priv,
829 enum v4l2_buf_type type)
830{
831 struct cx18_open_id *id = file->private_data;
832 struct cx18 *cx = id->cx;
833 struct cx18_stream *s = &cx->streams[id->type];
834
835
836 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
837 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
838 return -EINVAL;
839
840 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
841 return -EINVAL;
842
843
844 mod_timer(&s->vb_timeout, msecs_to_jiffies(2000) + jiffies);
845
846 return videobuf_streamon(cx18_vb_queue(id));
847}
848
849static int cx18_streamoff(struct file *file, void *priv,
850 enum v4l2_buf_type type)
851{
852 struct cx18_open_id *id = file->private_data;
853 struct cx18 *cx = id->cx;
854 struct cx18_stream *s = &cx->streams[id->type];
855
856
857 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
858 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
859 return -EINVAL;
860
861 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
862 return -EINVAL;
863
864 return videobuf_streamoff(cx18_vb_queue(id));
865}
866
867static int cx18_reqbufs(struct file *file, void *priv,
868 struct v4l2_requestbuffers *rb)
869{
870 struct cx18_open_id *id = file->private_data;
871 struct cx18 *cx = id->cx;
872 struct cx18_stream *s = &cx->streams[id->type];
873
874 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
875 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
876 return -EINVAL;
877
878 return videobuf_reqbufs(cx18_vb_queue(id), rb);
879}
880
881static int cx18_querybuf(struct file *file, void *priv,
882 struct v4l2_buffer *b)
883{
884 struct cx18_open_id *id = file->private_data;
885 struct cx18 *cx = id->cx;
886 struct cx18_stream *s = &cx->streams[id->type];
887
888 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
889 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
890 return -EINVAL;
891
892 return videobuf_querybuf(cx18_vb_queue(id), b);
893}
894
895static int cx18_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
896{
897 struct cx18_open_id *id = file->private_data;
898 struct cx18 *cx = id->cx;
899 struct cx18_stream *s = &cx->streams[id->type];
900
901 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
902 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
903 return -EINVAL;
904
905 return videobuf_qbuf(cx18_vb_queue(id), b);
906}
907
908static int cx18_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
909{
910 struct cx18_open_id *id = file->private_data;
911 struct cx18 *cx = id->cx;
912 struct cx18_stream *s = &cx->streams[id->type];
913
914 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
915 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
916 return -EINVAL;
917
918 return videobuf_dqbuf(cx18_vb_queue(id), b, file->f_flags & O_NONBLOCK);
919}
920
921static int cx18_encoder_cmd(struct file *file, void *fh,
922 struct v4l2_encoder_cmd *enc)
923{
924 struct cx18_open_id *id = fh2id(fh);
925 struct cx18 *cx = id->cx;
926 u32 h;
927
928 switch (enc->cmd) {
929 case V4L2_ENC_CMD_START:
930 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
931 enc->flags = 0;
932 return cx18_start_capture(id);
933
934 case V4L2_ENC_CMD_STOP:
935 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
936 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
937 cx18_stop_capture(id,
938 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
939 break;
940
941 case V4L2_ENC_CMD_PAUSE:
942 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
943 enc->flags = 0;
944 if (!atomic_read(&cx->ana_capturing))
945 return -EPERM;
946 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
947 return 0;
948 h = cx18_find_handle(cx);
949 if (h == CX18_INVALID_TASK_HANDLE) {
950 CX18_ERR("Can't find valid task handle for V4L2_ENC_CMD_PAUSE\n");
951 return -EBADFD;
952 }
953 cx18_mute(cx);
954 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
955 break;
956
957 case V4L2_ENC_CMD_RESUME:
958 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
959 enc->flags = 0;
960 if (!atomic_read(&cx->ana_capturing))
961 return -EPERM;
962 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
963 return 0;
964 h = cx18_find_handle(cx);
965 if (h == CX18_INVALID_TASK_HANDLE) {
966 CX18_ERR("Can't find valid task handle for V4L2_ENC_CMD_RESUME\n");
967 return -EBADFD;
968 }
969 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
970 cx18_unmute(cx);
971 break;
972
973 default:
974 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
975 return -EINVAL;
976 }
977 return 0;
978}
979
980static int cx18_try_encoder_cmd(struct file *file, void *fh,
981 struct v4l2_encoder_cmd *enc)
982{
983 struct cx18 *cx = fh2id(fh)->cx;
984
985 switch (enc->cmd) {
986 case V4L2_ENC_CMD_START:
987 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
988 enc->flags = 0;
989 break;
990
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;
994 break;
995
996 case V4L2_ENC_CMD_PAUSE:
997 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
998 enc->flags = 0;
999 break;
1000
1001 case V4L2_ENC_CMD_RESUME:
1002 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1003 enc->flags = 0;
1004 break;
1005
1006 default:
1007 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1008 return -EINVAL;
1009 }
1010 return 0;
1011}
1012
1013static int cx18_log_status(struct file *file, void *fh)
1014{
1015 struct cx18 *cx = fh2id(fh)->cx;
1016 struct v4l2_input vidin;
1017 struct v4l2_audio audin;
1018 int i;
1019
1020 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
1021 if (cx->hw_flags & CX18_HW_TVEEPROM) {
1022 struct tveeprom tv;
1023
1024 cx18_read_eeprom(cx, &tv);
1025 }
1026 cx18_call_all(cx, core, log_status);
1027 cx18_get_input(cx, cx->active_input, &vidin);
1028 cx18_get_audio_input(cx, cx->audio_input, &audin);
1029 CX18_INFO("Video Input: %s\n", vidin.name);
1030 CX18_INFO("Audio Input: %s\n", audin.name);
1031 mutex_lock(&cx->gpio_lock);
1032 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
1033 cx->gpio_dir, cx->gpio_val);
1034 mutex_unlock(&cx->gpio_lock);
1035 CX18_INFO("Tuner: %s\n",
1036 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
1037 v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
1038 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
1039 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1040 struct cx18_stream *s = &cx->streams[i];
1041
1042 if (s->video_dev.v4l2_dev == NULL || s->buffers == 0)
1043 continue;
1044 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1045 s->name, s->s_flags,
1046 atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
1047 / s->buffers,
1048 (s->buffers * s->buf_size) / 1024, s->buffers);
1049 }
1050 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1051 (long long)cx->mpg_data_received,
1052 (long long)cx->vbi_data_inserted);
1053 return 0;
1054}
1055
1056static long cx18_default(struct file *file, void *fh, bool valid_prio,
1057 unsigned int cmd, void *arg)
1058{
1059 struct cx18 *cx = fh2id(fh)->cx;
1060
1061 switch (cmd) {
1062 case VIDIOC_INT_RESET: {
1063 u32 val = *(u32 *)arg;
1064
1065 if ((val == 0) || (val & 0x01))
1066 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1067 (u32) CX18_GPIO_RESET_Z8F0811);
1068 break;
1069 }
1070
1071 default:
1072 return -ENOTTY;
1073 }
1074 return 0;
1075}
1076
1077static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1078 .vidioc_querycap = cx18_querycap,
1079 .vidioc_s_audio = cx18_s_audio,
1080 .vidioc_g_audio = cx18_g_audio,
1081 .vidioc_enumaudio = cx18_enumaudio,
1082 .vidioc_enum_input = cx18_enum_input,
1083 .vidioc_g_pixelaspect = cx18_g_pixelaspect,
1084 .vidioc_g_selection = cx18_g_selection,
1085 .vidioc_g_input = cx18_g_input,
1086 .vidioc_s_input = cx18_s_input,
1087 .vidioc_g_frequency = cx18_g_frequency,
1088 .vidioc_s_frequency = cx18_s_frequency,
1089 .vidioc_s_tuner = cx18_s_tuner,
1090 .vidioc_g_tuner = cx18_g_tuner,
1091 .vidioc_g_enc_index = cx18_g_enc_index,
1092 .vidioc_g_std = cx18_g_std,
1093 .vidioc_s_std = cx18_s_std,
1094 .vidioc_log_status = cx18_log_status,
1095 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
1096 .vidioc_encoder_cmd = cx18_encoder_cmd,
1097 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
1098 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
1099 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
1100 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
1101 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
1102 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
1103 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
1104 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
1105 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
1106 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
1107 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
1108#ifdef CONFIG_VIDEO_ADV_DEBUG
1109 .vidioc_g_register = cx18_g_register,
1110 .vidioc_s_register = cx18_s_register,
1111#endif
1112 .vidioc_default = cx18_default,
1113 .vidioc_streamon = cx18_streamon,
1114 .vidioc_streamoff = cx18_streamoff,
1115 .vidioc_reqbufs = cx18_reqbufs,
1116 .vidioc_querybuf = cx18_querybuf,
1117 .vidioc_qbuf = cx18_qbuf,
1118 .vidioc_dqbuf = cx18_dqbuf,
1119 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1120 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1121};
1122
1123void cx18_set_funcs(struct video_device *vdev)
1124{
1125 vdev->ioctl_ops = &cx18_ioctl_ops;
1126}
1127