Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / media / pci / cx18 / cx18-ioctl.c
1 /*
2  *  cx18 ioctl system call
3  *
4  *  Derived from ivtv-ioctl.c
5  *
6  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
7  *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
8  *
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.
13  *
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.
18  *
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
22  *  02111-1307  USA
23  */
24
25 #include "cx18-driver.h"
26 #include "cx18-io.h"
27 #include "cx18-version.h"
28 #include "cx18-mailbox.h"
29 #include "cx18-i2c.h"
30 #include "cx18-queue.h"
31 #include "cx18-fileops.h"
32 #include "cx18-vbi.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>
43
44 u16 cx18_service2vbi(int type)
45 {
46         switch (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;
53         case V4L2_SLICED_VPS:
54                 return CX18_SLICED_TYPE_VPS;
55         default:
56                 return 0;
57         }
58 }
59
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)
62 {
63         return (is_pal && line >= 6 &&
64                 ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
65                (!is_pal && line >= 10 && line < 22);
66 }
67
68 /*
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
74  */
75 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
76 {
77         u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
78         int i;
79
80         set = set & valid_set;
81         if (set == 0 || !valid_service_line(field, line, is_pal))
82                 return 0;
83         if (!is_pal) {
84                 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
85                         return V4L2_SLICED_CAPTION_525;
86         } else {
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;
91                 if (line == 23)
92                         return 0;
93         }
94         for (i = 0; i < 32; i++) {
95                 if ((1 << i) & set)
96                         return 1 << i;
97         }
98         return 0;
99 }
100
101 /*
102  * Expand the service_set of *fmt into valid service_lines for the std,
103  * and clear the passed in fmt->service_set
104  */
105 void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
106 {
107         u16 set = fmt->service_set;
108         int f, l;
109
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);
114         }
115 }
116
117 /*
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
120  */
121 static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
122 {
123         int f, l;
124         u16 set = 0;
125
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];
130                 }
131         }
132         return set != 0;
133 }
134
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)
137 {
138         int f, l;
139         u16 set = 0;
140
141         for (f = 0; f < 2; f++) {
142                 for (l = 0; l < 24; l++)
143                         set |= fmt->service_lines[f][l];
144         }
145         return set;
146 }
147
148 static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
149                                 struct v4l2_format *fmt)
150 {
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;
155
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;
164         } else {
165                 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
166                 pixfmt->sizeimage = 128 * 1024;
167                 pixfmt->bytesperline = 0;
168         }
169         return 0;
170 }
171
172 static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
173                                 struct v4l2_format *fmt)
174 {
175         struct cx18 *cx = fh2id(fh)->cx;
176         struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
177
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;
185         vbifmt->flags = 0;
186         vbifmt->reserved[0] = 0;
187         vbifmt->reserved[1] = 0;
188         return 0;
189 }
190
191 static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
192                                         struct v4l2_format *fmt)
193 {
194         struct cx18 *cx = fh2id(fh)->cx;
195         struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
196
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;
203
204         /*
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
208          */
209         if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
210                 return -EINVAL;
211
212         vbifmt->service_set = cx18_get_service_set(vbifmt);
213         return 0;
214 }
215
216 static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
217                                 struct v4l2_format *fmt)
218 {
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;
223         int min_h = 2;
224
225         w = min(w, 720);
226         w = max(w, 2);
227         if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
228                 /* YUV height must be a multiple of 32 */
229                 h &= ~0x1f;
230                 min_h = 32;
231         }
232         h = min(h, cx->is_50hz ? 576 : 480);
233         h = max(h, min_h);
234
235         fmt->fmt.pix.width = w;
236         fmt->fmt.pix.height = h;
237         return 0;
238 }
239
240 static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
241                                 struct v4l2_format *fmt)
242 {
243         return cx18_g_fmt_vbi_cap(file, fh, fmt);
244 }
245
246 static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
247                                         struct v4l2_format *fmt)
248 {
249         struct cx18 *cx = fh2id(fh)->cx;
250         struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
251
252         vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
253         vbifmt->reserved[0] = 0;
254         vbifmt->reserved[1] = 0;
255
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);
262         return 0;
263 }
264
265 static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
266                                 struct v4l2_format *fmt)
267 {
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];
272         int ret;
273         int w, h;
274
275         ret = cx18_try_fmt_vid_cap(file, fh, fmt);
276         if (ret)
277                 return ret;
278         w = fmt->fmt.pix.width;
279         h = fmt->fmt.pix.height;
280
281         if (cx->cxhdl.width == w && cx->cxhdl.height == h &&
282             s->pixelformat == fmt->fmt.pix.pixelformat)
283                 return 0;
284
285         if (atomic_read(&cx->ana_capturing) > 0)
286                 return -EBUSY;
287
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 */
294         } else {
295                 s->vb_bytes_per_frame = h * 720 * 2;
296                 s->vb_bytes_per_line = 1440; /* Packed */
297         }
298
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);
304 }
305
306 static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
307                                 struct v4l2_format *fmt)
308 {
309         struct cx18_open_id *id = fh2id(fh);
310         struct cx18 *cx = id->cx;
311         int ret;
312
313         /*
314          * Changing the Encoder's Raw VBI parameters won't have any effect
315          * if any analog capture is ongoing
316          */
317         if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
318                 return -EBUSY;
319
320         /*
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
323          * calling conditions
324          */
325         ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
326         if (ret)
327                 return ret;
328
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;
332
333         return cx18_g_fmt_vbi_cap(file, fh, fmt);
334 }
335
336 static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
337                                         struct v4l2_format *fmt)
338 {
339         struct cx18_open_id *id = fh2id(fh);
340         struct cx18 *cx = id->cx;
341         int ret;
342         struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
343
344         cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
345
346         /*
347          * Changing the Encoder's Raw VBI parameters won't have any effect
348          * if any analog capture is ongoing
349          */
350         if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
351                 return -EBUSY;
352
353         /*
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
357          */
358         ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
359         if (ret)
360                 return ret;
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));
364         return 0;
365 }
366
367 #ifdef CONFIG_VIDEO_ADV_DEBUG
368 static int cx18_g_register(struct file *file, void *fh,
369                                 struct v4l2_dbg_register *reg)
370 {
371         struct cx18 *cx = fh2id(fh)->cx;
372
373         if (reg->reg & 0x3)
374                 return -EINVAL;
375         if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
376                 return -EINVAL;
377         reg->size = 4;
378         reg->val = cx18_read_enc(cx, reg->reg);
379         return 0;
380 }
381
382 static int cx18_s_register(struct file *file, void *fh,
383                                 const struct v4l2_dbg_register *reg)
384 {
385         struct cx18 *cx = fh2id(fh)->cx;
386
387         if (reg->reg & 0x3)
388                 return -EINVAL;
389         if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
390                 return -EINVAL;
391         cx18_write_enc(cx, reg->val, reg->reg);
392         return 0;
393 }
394 #endif
395
396 static int cx18_querycap(struct file *file, void *fh,
397                                 struct v4l2_capability *vcap)
398 {
399         struct cx18_open_id *id = fh2id(fh);
400         struct cx18_stream *s = video_drvdata(file);
401         struct cx18 *cx = id->cx;
402
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;
410         return 0;
411 }
412
413 static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
414 {
415         struct cx18 *cx = fh2id(fh)->cx;
416
417         return cx18_get_audio_input(cx, vin->index, vin);
418 }
419
420 static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
421 {
422         struct cx18 *cx = fh2id(fh)->cx;
423
424         vin->index = cx->audio_input;
425         return cx18_get_audio_input(cx, vin->index, vin);
426 }
427
428 static int cx18_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
429 {
430         struct cx18 *cx = fh2id(fh)->cx;
431
432         if (vout->index >= cx->nof_audio_inputs)
433                 return -EINVAL;
434         cx->audio_input = vout->index;
435         cx18_audio_set_io(cx);
436         return 0;
437 }
438
439 static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
440 {
441         struct cx18 *cx = fh2id(fh)->cx;
442
443         /* set it to defaults from our table */
444         return cx18_get_input(cx, vin->index, vin);
445 }
446
447 static int cx18_cropcap(struct file *file, void *fh,
448                         struct v4l2_cropcap *cropcap)
449 {
450         struct cx18 *cx = fh2id(fh)->cx;
451
452         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
453                 return -EINVAL;
454         cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
455         cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
456         return 0;
457 }
458
459 static int cx18_g_selection(struct file *file, void *fh,
460                             struct v4l2_selection *sel)
461 {
462         struct cx18 *cx = fh2id(fh)->cx;
463
464         if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
465                 return -EINVAL;
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;
470                 sel->r.width = 720;
471                 sel->r.height = cx->is_50hz ? 576 : 480;
472                 break;
473         default:
474                 return -EINVAL;
475         }
476         return 0;
477 }
478
479 static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
480                                         struct v4l2_fmtdesc *fmt)
481 {
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 }
485                 },
486                 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
487                   "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
488                 },
489                 { 2, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
490                   "UYVY 4:2:2", V4L2_PIX_FMT_UYVY, { 0, 0, 0, 0 }
491                 },
492         };
493
494         if (fmt->index > ARRAY_SIZE(formats) - 1)
495                 return -EINVAL;
496         *fmt = formats[fmt->index];
497         return 0;
498 }
499
500 static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
501 {
502         struct cx18 *cx = fh2id(fh)->cx;
503
504         *i = cx->active_input;
505         return 0;
506 }
507
508 int cx18_s_input(struct file *file, void *fh, unsigned int inp)
509 {
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;
515
516         if (inp >= cx->nof_inputs)
517                 return -EINVAL;
518
519         if (inp == cx->active_input) {
520                 CX18_DEBUG_INFO("Input unchanged\n");
521                 return 0;
522         }
523
524         CX18_DEBUG_INFO("Changing input from %d to %d\n",
525                         cx->active_input, inp);
526
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)
531                 std = cx->tuner_std;
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;
535
536         /* prevent others from messing with the streams until
537            we're finished changing inputs. */
538         cx18_mute(cx);
539         cx18_video_set_io(cx);
540         cx18_audio_set_io(cx);
541         cx18_unmute(cx);
542         return 0;
543 }
544
545 static int cx18_g_frequency(struct file *file, void *fh,
546                                 struct v4l2_frequency *vf)
547 {
548         struct cx18 *cx = fh2id(fh)->cx;
549
550         if (vf->tuner != 0)
551                 return -EINVAL;
552
553         cx18_call_all(cx, tuner, g_frequency, vf);
554         return 0;
555 }
556
557 int cx18_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
558 {
559         struct cx18_open_id *id = fh2id(fh);
560         struct cx18 *cx = id->cx;
561
562         if (vf->tuner != 0)
563                 return -EINVAL;
564
565         cx18_mute(cx);
566         CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
567         cx18_call_all(cx, tuner, s_frequency, vf);
568         cx18_unmute(cx);
569         return 0;
570 }
571
572 static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
573 {
574         struct cx18 *cx = fh2id(fh)->cx;
575
576         *std = cx->std;
577         return 0;
578 }
579
580 int cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
581 {
582         struct cx18_open_id *id = fh2id(fh);
583         struct cx18 *cx = id->cx;
584
585         if ((std & V4L2_STD_ALL) == 0)
586                 return -EINVAL;
587
588         if (std == cx->std)
589                 return 0;
590
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
595                    returning EBUSY. */
596                 return -EBUSY;
597         }
598
599         cx->std = std;
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);
610
611         /* Tuner */
612         cx18_call_all(cx, video, s_std, cx->std);
613         return 0;
614 }
615
616 static int cx18_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
617 {
618         struct cx18_open_id *id = fh2id(fh);
619         struct cx18 *cx = id->cx;
620
621         if (vt->index != 0)
622                 return -EINVAL;
623
624         cx18_call_all(cx, tuner, s_tuner, vt);
625         return 0;
626 }
627
628 static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
629 {
630         struct cx18 *cx = fh2id(fh)->cx;
631
632         if (vt->index != 0)
633                 return -EINVAL;
634
635         cx18_call_all(cx, tuner, g_tuner, vt);
636
637         if (vt->type == V4L2_TUNER_RADIO)
638                 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
639         else
640                 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
641         return 0;
642 }
643
644 static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
645                                         struct v4l2_sliced_vbi_cap *cap)
646 {
647         struct cx18 *cx = fh2id(fh)->cx;
648         int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
649         int f, l;
650
651         if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
652                 return -EINVAL;
653
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)) {
658                                 /*
659                                  * We can find all v4l2 supported vbi services
660                                  * for the standard, on a valid line for the std
661                                  */
662                                 cap->service_lines[f][l] = set;
663                                 cap->service_set |= set;
664                         } else
665                                 cap->service_lines[f][l] = 0;
666                 }
667         }
668         for (f = 0; f < 3; f++)
669                 cap->reserved[f] = 0;
670         return 0;
671 }
672
673 static int _cx18_process_idx_data(struct cx18_buffer *buf,
674                                   struct v4l2_enc_idx *idx)
675 {
676         int consumed, remaining;
677         struct v4l2_enc_idx_entry *e_idx;
678         struct cx18_enc_idx_entry *e_buf;
679
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
684         };
685
686         /*
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.
690          */
691         remaining = buf->bytesused - buf->readpos;
692         consumed = 0;
693         e_idx = &idx->entry[idx->entries];
694         e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
695
696         while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
697                idx->entries < V4L2_ENC_IDX_ENTRIES) {
698
699                 e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
700                                 | le32_to_cpu(e_buf->offset_low);
701
702                 e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
703                              | le32_to_cpu(e_buf->pts_low);
704
705                 e_idx->length = le32_to_cpu(e_buf->length);
706
707                 e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
708
709                 e_idx->reserved[0] = 0;
710                 e_idx->reserved[1] = 0;
711
712                 idx->entries++;
713                 e_idx = &idx->entry[idx->entries];
714                 e_buf++;
715
716                 remaining -= sizeof(struct cx18_enc_idx_entry);
717                 consumed += sizeof(struct cx18_enc_idx_entry);
718         }
719
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;
723
724         buf->readpos += consumed;
725         return consumed;
726 }
727
728 static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
729                                  struct v4l2_enc_idx *idx)
730 {
731         if (s->type != CX18_ENC_STREAM_TYPE_IDX)
732                 return -EINVAL;
733
734         if (mdl->curr_buf == NULL)
735                 mdl->curr_buf = list_first_entry(&mdl->buf_list,
736                                                  struct cx18_buffer, list);
737
738         if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
739                 /*
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.
743                  */
744                 mdl->readpos = mdl->bytesused;
745                 return 0;
746         }
747
748         list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
749
750                 /* Skip any empty buffers in the MDL */
751                 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
752                         continue;
753
754                 mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
755
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)
760                         break;
761         }
762         return 0;
763 }
764
765 static int cx18_g_enc_index(struct file *file, void *fh,
766                                 struct v4l2_enc_idx *idx)
767 {
768         struct cx18 *cx = fh2id(fh)->cx;
769         struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
770         s32 tmp;
771         struct cx18_mdl *mdl;
772
773         if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
774                 return -EINVAL;
775
776         /* Compute the best case number of entries we can buffer */
777         tmp = s->buffers -
778                           s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
779         if (tmp <= 0)
780                 tmp = 1;
781         tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
782
783         /* Fill out the header of the return structure */
784         idx->entries = 0;
785         idx->entries_cap = tmp;
786         memset(idx->reserved, 0, sizeof(idx->reserved));
787
788         /* Pull IDX MDLs and buffers from q_full and populate the entries */
789         do {
790                 mdl = cx18_dequeue(s, &s->q_full);
791                 if (mdl == NULL) /* No more IDX data right now */
792                         break;
793
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);
799                         break;
800                 }
801
802                 /* We drained this MDL, schedule it to go to the firmware */
803                 cx18_enqueue(s, mdl, &s->q_free);
804
805         } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
806
807         /* Tell the work handler to send free IDX MDLs to the firmware */
808         cx18_stream_load_fw_queue(s);
809         return 0;
810 }
811
812 static struct videobuf_queue *cx18_vb_queue(struct cx18_open_id *id)
813 {
814         struct videobuf_queue *q = NULL;
815         struct cx18 *cx = id->cx;
816         struct cx18_stream *s = &cx->streams[id->type];
817
818         switch (s->vb_type) {
819         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
820                 q = &s->vbuf_q;
821                 break;
822         case V4L2_BUF_TYPE_VBI_CAPTURE:
823                 break;
824         default:
825                 break;
826         }
827         return q;
828 }
829
830 static int cx18_streamon(struct file *file, void *priv,
831         enum v4l2_buf_type type)
832 {
833         struct cx18_open_id *id = file->private_data;
834         struct cx18 *cx = id->cx;
835         struct cx18_stream *s = &cx->streams[id->type];
836
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))
840                 return -EINVAL;
841
842         if (id->type != CX18_ENC_STREAM_TYPE_YUV)
843                 return -EINVAL;
844
845         /* Establish a buffer timeout */
846         mod_timer(&s->vb_timeout, msecs_to_jiffies(2000) + jiffies);
847
848         return videobuf_streamon(cx18_vb_queue(id));
849 }
850
851 static int cx18_streamoff(struct file *file, void *priv,
852         enum v4l2_buf_type type)
853 {
854         struct cx18_open_id *id = file->private_data;
855         struct cx18 *cx = id->cx;
856         struct cx18_stream *s = &cx->streams[id->type];
857
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))
861                 return -EINVAL;
862
863         if (id->type != CX18_ENC_STREAM_TYPE_YUV)
864                 return -EINVAL;
865
866         return videobuf_streamoff(cx18_vb_queue(id));
867 }
868
869 static int cx18_reqbufs(struct file *file, void *priv,
870         struct v4l2_requestbuffers *rb)
871 {
872         struct cx18_open_id *id = file->private_data;
873         struct cx18 *cx = id->cx;
874         struct cx18_stream *s = &cx->streams[id->type];
875
876         if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
877                 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
878                 return -EINVAL;
879
880         return videobuf_reqbufs(cx18_vb_queue(id), rb);
881 }
882
883 static int cx18_querybuf(struct file *file, void *priv,
884         struct v4l2_buffer *b)
885 {
886         struct cx18_open_id *id = file->private_data;
887         struct cx18 *cx = id->cx;
888         struct cx18_stream *s = &cx->streams[id->type];
889
890         if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
891                 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
892                 return -EINVAL;
893
894         return videobuf_querybuf(cx18_vb_queue(id), b);
895 }
896
897 static int cx18_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
898 {
899         struct cx18_open_id *id = file->private_data;
900         struct cx18 *cx = id->cx;
901         struct cx18_stream *s = &cx->streams[id->type];
902
903         if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
904                 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
905                 return -EINVAL;
906
907         return videobuf_qbuf(cx18_vb_queue(id), b);
908 }
909
910 static int cx18_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
911 {
912         struct cx18_open_id *id = file->private_data;
913         struct cx18 *cx = id->cx;
914         struct cx18_stream *s = &cx->streams[id->type];
915
916         if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
917                 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
918                 return -EINVAL;
919
920         return videobuf_dqbuf(cx18_vb_queue(id), b, file->f_flags & O_NONBLOCK);
921 }
922
923 static int cx18_encoder_cmd(struct file *file, void *fh,
924                                 struct v4l2_encoder_cmd *enc)
925 {
926         struct cx18_open_id *id = fh2id(fh);
927         struct cx18 *cx = id->cx;
928         u32 h;
929
930         switch (enc->cmd) {
931         case V4L2_ENC_CMD_START:
932                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
933                 enc->flags = 0;
934                 return cx18_start_capture(id);
935
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);
941                 break;
942
943         case V4L2_ENC_CMD_PAUSE:
944                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
945                 enc->flags = 0;
946                 if (!atomic_read(&cx->ana_capturing))
947                         return -EPERM;
948                 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
949                         return 0;
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");
954                         return -EBADFD;
955                 }
956                 cx18_mute(cx);
957                 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
958                 break;
959
960         case V4L2_ENC_CMD_RESUME:
961                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
962                 enc->flags = 0;
963                 if (!atomic_read(&cx->ana_capturing))
964                         return -EPERM;
965                 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
966                         return 0;
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");
971                         return -EBADFD;
972                 }
973                 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
974                 cx18_unmute(cx);
975                 break;
976
977         default:
978                 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
979                 return -EINVAL;
980         }
981         return 0;
982 }
983
984 static int cx18_try_encoder_cmd(struct file *file, void *fh,
985                                 struct v4l2_encoder_cmd *enc)
986 {
987         struct cx18 *cx = fh2id(fh)->cx;
988
989         switch (enc->cmd) {
990         case V4L2_ENC_CMD_START:
991                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
992                 enc->flags = 0;
993                 break;
994
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;
998                 break;
999
1000         case V4L2_ENC_CMD_PAUSE:
1001                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1002                 enc->flags = 0;
1003                 break;
1004
1005         case V4L2_ENC_CMD_RESUME:
1006                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1007                 enc->flags = 0;
1008                 break;
1009
1010         default:
1011                 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1012                 return -EINVAL;
1013         }
1014         return 0;
1015 }
1016
1017 static int cx18_log_status(struct file *file, void *fh)
1018 {
1019         struct cx18 *cx = fh2id(fh)->cx;
1020         struct v4l2_input vidin;
1021         struct v4l2_audio audin;
1022         int i;
1023
1024         CX18_INFO("Version: %s  Card: %s\n", CX18_VERSION, cx->card_name);
1025         if (cx->hw_flags & CX18_HW_TVEEPROM) {
1026                 struct tveeprom tv;
1027
1028                 cx18_read_eeprom(cx, &tv);
1029         }
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];
1045
1046                 if (s->video_dev.v4l2_dev == NULL || s->buffers == 0)
1047                         continue;
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
1051                            / s->buffers,
1052                           (s->buffers * s->buf_size) / 1024, s->buffers);
1053         }
1054         CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1055                         (long long)cx->mpg_data_received,
1056                         (long long)cx->vbi_data_inserted);
1057         return 0;
1058 }
1059
1060 static long cx18_default(struct file *file, void *fh, bool valid_prio,
1061                          unsigned int cmd, void *arg)
1062 {
1063         struct cx18 *cx = fh2id(fh)->cx;
1064
1065         switch (cmd) {
1066         case VIDIOC_INT_RESET: {
1067                 u32 val = *(u32 *)arg;
1068
1069                 if ((val == 0) || (val & 0x01))
1070                         cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1071                                      (u32) CX18_GPIO_RESET_Z8F0811);
1072                 break;
1073         }
1074
1075         default:
1076                 return -ENOTTY;
1077         }
1078         return 0;
1079 }
1080
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,
1115 #endif
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,
1125 };
1126
1127 void cx18_set_funcs(struct video_device *vdev)
1128 {
1129         vdev->ioctl_ops = &cx18_ioctl_ops;
1130 }