]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/media/video/cx18/cx18-ioctl.c
V4L/DVB (8084): ivtv/cx18: remove unnecessary memsets & KERNEL_VERSION tests
[mv-sheeva.git] / drivers / media / video / 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  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  *  02111-1307  USA
22  */
23
24 #include "cx18-driver.h"
25 #include "cx18-version.h"
26 #include "cx18-mailbox.h"
27 #include "cx18-i2c.h"
28 #include "cx18-queue.h"
29 #include "cx18-fileops.h"
30 #include "cx18-vbi.h"
31 #include "cx18-audio.h"
32 #include "cx18-video.h"
33 #include "cx18-streams.h"
34 #include "cx18-ioctl.h"
35 #include "cx18-gpio.h"
36 #include "cx18-controls.h"
37 #include "cx18-cards.h"
38 #include "cx18-av-core.h"
39 #include <media/tveeprom.h>
40 #include <media/v4l2-chip-ident.h>
41 #include <linux/i2c-id.h>
42
43 u16 cx18_service2vbi(int type)
44 {
45         switch (type) {
46         case V4L2_SLICED_TELETEXT_B:
47                 return CX18_SLICED_TYPE_TELETEXT_B;
48         case V4L2_SLICED_CAPTION_525:
49                 return CX18_SLICED_TYPE_CAPTION_525;
50         case V4L2_SLICED_WSS_625:
51                 return CX18_SLICED_TYPE_WSS_625;
52         case V4L2_SLICED_VPS:
53                 return CX18_SLICED_TYPE_VPS;
54         default:
55                 return 0;
56         }
57 }
58
59 static int valid_service_line(int field, int line, int is_pal)
60 {
61         return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
62                (!is_pal && line >= 10 && line < 22);
63 }
64
65 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
66 {
67         u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
68         int i;
69
70         set = set & valid_set;
71         if (set == 0 || !valid_service_line(field, line, is_pal))
72                 return 0;
73         if (!is_pal) {
74                 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
75                         return V4L2_SLICED_CAPTION_525;
76         } else {
77                 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
78                         return V4L2_SLICED_VPS;
79                 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
80                         return V4L2_SLICED_WSS_625;
81                 if (line == 23)
82                         return 0;
83         }
84         for (i = 0; i < 32; i++) {
85                 if ((1 << i) & set)
86                         return 1 << i;
87         }
88         return 0;
89 }
90
91 void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
92 {
93         u16 set = fmt->service_set;
94         int f, l;
95
96         fmt->service_set = 0;
97         for (f = 0; f < 2; f++) {
98                 for (l = 0; l < 24; l++)
99                         fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
100         }
101 }
102
103
104 u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
105 {
106         int f, l;
107         u16 set = 0;
108
109         for (f = 0; f < 2; f++) {
110                 for (l = 0; l < 24; l++)
111                         set |= fmt->service_lines[f][l];
112         }
113         return set;
114 }
115
116 static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
117 {
118         struct v4l2_register *regs = arg;
119         unsigned long flags;
120
121         if (!capable(CAP_SYS_ADMIN))
122                 return -EPERM;
123         if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
124                 return -EINVAL;
125
126         spin_lock_irqsave(&cx18_cards_lock, flags);
127         if (cmd == VIDIOC_DBG_G_REGISTER)
128                 regs->val = read_enc(regs->reg);
129         else
130                 write_enc(regs->val, regs->reg);
131         spin_unlock_irqrestore(&cx18_cards_lock, flags);
132         return 0;
133 }
134
135 static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
136                                 struct v4l2_format *fmt)
137 {
138         struct cx18_open_id *id = fh;
139         struct cx18 *cx = id->cx;
140
141         CX18_DEBUG_IOCTL("VIDIOC_G_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
142
143         fmt->fmt.pix.width = cx->params.width;
144         fmt->fmt.pix.height = cx->params.height;
145         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
146         fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
147         if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
148                 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
149                 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
150                 fmt->fmt.pix.sizeimage =
151                         fmt->fmt.pix.height * fmt->fmt.pix.width +
152                         fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
153         } else {
154                 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
155                 fmt->fmt.pix.sizeimage = 128 * 1024;
156         }
157         return 0;
158 }
159
160 static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
161                                 struct v4l2_format *fmt)
162 {
163         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
164
165         CX18_DEBUG_IOCTL("VIDIOC_G_FMT: V4L2_BUF_TYPE_VBI_CAPTURE\n");
166
167         fmt->fmt.vbi.sampling_rate = 27000000;
168         fmt->fmt.vbi.offset = 248;
169         fmt->fmt.vbi.samples_per_line = cx->vbi.raw_decoder_line_size - 4;
170         fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
171         fmt->fmt.vbi.start[0] = cx->vbi.start[0];
172         fmt->fmt.vbi.start[1] = cx->vbi.start[1];
173         fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = cx->vbi.count;
174         return 0;
175 }
176
177 static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
178                                         struct v4l2_format *fmt)
179 {
180         return -EINVAL;
181 }
182
183 static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
184                                 struct v4l2_format *fmt)
185 {
186         struct cx18_open_id *id = fh;
187         struct cx18 *cx = id->cx;
188
189         int w = fmt->fmt.pix.width;
190         int h = fmt->fmt.pix.height;
191
192         CX18_DEBUG_IOCTL("VIDIOC_TRY_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
193
194         w = min(w, 720);
195         w = max(w, 1);
196         h = min(h, cx->is_50hz ? 576 : 480);
197         h = max(h, 2);
198         cx18_g_fmt_vid_cap(file, fh, fmt);
199         fmt->fmt.pix.width = w;
200         fmt->fmt.pix.height = h;
201         return 0;
202 }
203
204 static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
205                                 struct v4l2_format *fmt)
206 {
207         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
208
209         CX18_DEBUG_IOCTL("VIDIOC_TRY_FMT: V4L2_BUF_TYPE_VBI_CAPTURE\n");
210
211         return cx18_g_fmt_vbi_cap(file, fh, fmt);
212 }
213
214 static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
215                                         struct v4l2_format *fmt)
216 {
217         return -EINVAL;
218 }
219
220 static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
221                                 struct v4l2_format *fmt)
222 {
223         struct cx18_open_id *id = fh;
224         struct cx18 *cx = id->cx;
225         int ret;
226         int w = fmt->fmt.pix.width;
227         int h = fmt->fmt.pix.height;
228
229         ret = v4l2_prio_check(&cx->prio, &id->prio);
230         if (ret)
231                 return ret;
232
233         CX18_DEBUG_IOCTL("VIDIOC_S_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
234
235         ret = cx18_try_fmt_vid_cap(file, fh, fmt);
236         if (ret)
237                 return ret;
238
239         if (cx->params.width == w && cx->params.height == h)
240                 return 0;
241
242         if (atomic_read(&cx->ana_capturing) > 0)
243                 return -EBUSY;
244
245         cx->params.width = w;
246         cx->params.height = h;
247         if (w != 720 || h != (cx->is_50hz ? 576 : 480))
248                 cx->params.video_temporal_filter = 0;
249         else
250                 cx->params.video_temporal_filter = 8;
251         cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
252         return cx18_g_fmt_vid_cap(file, fh, fmt);
253 }
254
255 static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
256                                 struct v4l2_format *fmt)
257 {
258         struct cx18_open_id *id = fh;
259         struct cx18 *cx = id->cx;
260         int ret;
261
262         ret = v4l2_prio_check(&cx->prio, &id->prio);
263         if (ret)
264                 return ret;
265
266         CX18_DEBUG_IOCTL("VIDIOC_S_FMT: V4L2_BUF_TYPE_VBI_CAPTURE\n");
267
268         if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
269                         cx->vbi.sliced_in->service_set &&
270                         atomic_read(&cx->ana_capturing) > 0)
271                 return -EBUSY;
272
273         cx->vbi.sliced_in->service_set = 0;
274         cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
275         return cx18_g_fmt_vbi_cap(file, fh, fmt);
276 }
277
278 static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
279                                         struct v4l2_format *fmt)
280 {
281         return -EINVAL;
282 }
283
284 static int cx18_g_chip_ident(struct file *file, void *fh,
285                                 struct v4l2_chip_ident *chip)
286 {
287         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
288
289         CX18_DEBUG_IOCTL("VIDIOC_G_CHIP_IDENT\n");
290
291         chip->ident = V4L2_IDENT_NONE;
292         chip->revision = 0;
293         if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
294                 if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
295                         chip->ident = V4L2_IDENT_CX23418;
296                 return 0;
297         }
298         if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
299                 return cx18_i2c_id(cx, chip->match_chip, VIDIOC_G_CHIP_IDENT,
300                                         chip);
301         if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
302                 return cx18_call_i2c_client(cx, chip->match_chip,
303                                                 VIDIOC_G_CHIP_IDENT, chip);
304         return -EINVAL;
305 }
306
307 static int cx18_g_register(struct file *file, void *fh,
308                                 struct v4l2_register *reg)
309 {
310         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
311
312         CX18_DEBUG_IOCTL("VIDIOC_DBG_G_REGISTER\n");
313
314         if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
315                 return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
316         if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
317                 return cx18_i2c_id(cx, reg->match_chip, VIDIOC_DBG_G_REGISTER,
318                                         reg);
319         return cx18_call_i2c_client(cx, reg->match_chip, VIDIOC_DBG_G_REGISTER,
320                                         reg);
321 }
322
323 static int cx18_s_register(struct file *file, void *fh,
324                                 struct v4l2_register *reg)
325 {
326         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
327
328         CX18_DEBUG_IOCTL("VIDIOC_DBG_S_REGISTER\n");
329
330         if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
331                 return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
332         if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
333                 return cx18_i2c_id(cx, reg->match_chip, VIDIOC_DBG_S_REGISTER,
334                                         reg);
335         return cx18_call_i2c_client(cx, reg->match_chip, VIDIOC_DBG_S_REGISTER,
336                                         reg);
337 }
338
339 static int cx18_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
340 {
341         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
342
343         CX18_DEBUG_IOCTL("VIDIOC_G_PRIORITY\n");
344
345         *p = v4l2_prio_max(&cx->prio);
346         return 0;
347 }
348
349 static int cx18_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
350 {
351         struct cx18_open_id *id = fh;
352         struct cx18 *cx = id->cx;
353
354         CX18_DEBUG_IOCTL("VIDIOC_S_PRIORITY\n");
355
356         return v4l2_prio_change(&cx->prio, &id->prio, prio);
357 }
358
359 static int cx18_querycap(struct file *file, void *fh,
360                                 struct v4l2_capability *vcap)
361 {
362         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
363
364         CX18_DEBUG_IOCTL("VIDIOC_QUERYCAP\n");
365
366         strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
367         strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
368         strlcpy(vcap->bus_info, pci_name(cx->dev), sizeof(vcap->bus_info));
369         vcap->version = CX18_DRIVER_VERSION;        /* version */
370         vcap->capabilities = cx->v4l2_cap;          /* capabilities */
371         return 0;
372 }
373
374 static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
375 {
376         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
377
378         CX18_DEBUG_IOCTL("VIDIOC_ENUMAUDIO\n");
379
380         return cx18_get_audio_input(cx, vin->index, vin);
381 }
382
383 static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
384 {
385         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
386
387         CX18_DEBUG_IOCTL("VIDIOC_G_AUDIO\n");
388
389         vin->index = cx->audio_input;
390         return cx18_get_audio_input(cx, vin->index, vin);
391 }
392
393 static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
394 {
395         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
396
397         CX18_DEBUG_IOCTL("VIDIOC_S_AUDIO\n");
398
399         if (vout->index >= cx->nof_audio_inputs)
400                 return -EINVAL;
401         cx->audio_input = vout->index;
402         cx18_audio_set_io(cx);
403         return 0;
404 }
405
406 static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
407 {
408         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
409
410         CX18_DEBUG_IOCTL("VIDIOC_ENUMINPUT\n");
411
412         /* set it to defaults from our table */
413         return cx18_get_input(cx, vin->index, vin);
414 }
415
416 static int cx18_cropcap(struct file *file, void *fh,
417                         struct v4l2_cropcap *cropcap)
418 {
419         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
420
421         CX18_DEBUG_IOCTL("VIDIOC_CROPCAP\n");
422
423         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
424                 return -EINVAL;
425         cropcap->bounds.top = cropcap->bounds.left = 0;
426         cropcap->bounds.width = 720;
427         cropcap->bounds.height = cx->is_50hz ? 576 : 480;
428         cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
429         cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
430         cropcap->defrect = cropcap->bounds;
431         return 0;
432 }
433
434 static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
435 {
436         struct cx18_open_id *id = fh;
437         struct cx18 *cx = id->cx;
438         int ret;
439
440         ret = v4l2_prio_check(&cx->prio, &id->prio);
441         if (ret)
442                 return ret;
443
444         CX18_DEBUG_IOCTL("VIDIOC_S_CROP\n");
445
446         if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
447                 return -EINVAL;
448         return cx18_av_cmd(cx, VIDIOC_S_CROP, crop);
449 }
450
451 static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
452 {
453         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
454
455         CX18_DEBUG_IOCTL("VIDIOC_G_CROP\n");
456
457         if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
458                 return -EINVAL;
459         return cx18_av_cmd(cx, VIDIOC_G_CROP, crop);
460 }
461
462 static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
463                                         struct v4l2_fmtdesc *fmt)
464 {
465         static struct v4l2_fmtdesc formats[] = {
466                 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
467                   "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
468                 },
469                 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
470                   "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
471                 }
472         };
473
474         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
475
476         CX18_DEBUG_IOCTL("VIDIOC_ENUM_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
477
478         if (fmt->index > 1)
479                 return -EINVAL;
480         *fmt = formats[fmt->index];
481         return 0;
482 }
483
484 static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
485 {
486         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
487
488         CX18_DEBUG_IOCTL("VIDIOC_G_INPUT\n");
489
490         *i = cx->active_input;
491         return 0;
492 }
493
494 int cx18_s_input(struct file *file, void *fh, unsigned int inp)
495 {
496         struct cx18_open_id *id = fh;
497         struct cx18 *cx = id->cx;
498         int ret;
499
500         ret = v4l2_prio_check(&cx->prio, &id->prio);
501         if (ret)
502                 return ret;
503
504         CX18_DEBUG_IOCTL("VIDIOC_S_INPUT\n");
505
506         if (inp < 0 || inp >= cx->nof_inputs)
507                 return -EINVAL;
508
509         if (inp == cx->active_input) {
510                 CX18_DEBUG_INFO("Input unchanged\n");
511                 return 0;
512         }
513
514         CX18_DEBUG_INFO("Changing input from %d to %d\n",
515                         cx->active_input, inp);
516
517         cx->active_input = inp;
518         /* Set the audio input to whatever is appropriate for the input type. */
519         cx->audio_input = cx->card->video_inputs[inp].audio_index;
520
521         /* prevent others from messing with the streams until
522            we're finished changing inputs. */
523         cx18_mute(cx);
524         cx18_video_set_io(cx);
525         cx18_audio_set_io(cx);
526         cx18_unmute(cx);
527         return 0;
528 }
529
530 static int cx18_g_frequency(struct file *file, void *fh,
531                                 struct v4l2_frequency *vf)
532 {
533         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
534
535         CX18_DEBUG_IOCTL("VIDIOC_G_FREQUENCY\n");
536
537         if (vf->tuner != 0)
538                 return -EINVAL;
539
540         cx18_call_i2c_clients(cx, VIDIOC_G_FREQUENCY, vf);
541         return 0;
542 }
543
544 int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
545 {
546         struct cx18_open_id *id = fh;
547         struct cx18 *cx = id->cx;
548         int ret;
549
550         ret = v4l2_prio_check(&cx->prio, &id->prio);
551         if (ret)
552                 return ret;
553
554         CX18_DEBUG_IOCTL("VIDIOC_S_FREQUENCY\n");
555
556         if (vf->tuner != 0)
557                 return -EINVAL;
558
559         cx18_mute(cx);
560         CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
561         cx18_call_i2c_clients(cx, VIDIOC_S_FREQUENCY, vf);
562         cx18_unmute(cx);
563         return 0;
564 }
565
566 static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
567 {
568         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
569
570         CX18_DEBUG_IOCTL("VIDIOC_G_STD\n");
571
572         *std = cx->std;
573         return 0;
574 }
575
576 int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
577 {
578         struct cx18_open_id *id = fh;
579         struct cx18 *cx = id->cx;
580         int ret;
581
582         ret = v4l2_prio_check(&cx->prio, &id->prio);
583         if (ret)
584                 return ret;
585
586         CX18_DEBUG_IOCTL("VIDIOC_S_STD\n");
587
588         if ((*std & V4L2_STD_ALL) == 0)
589                 return -EINVAL;
590
591         if (*std == cx->std)
592                 return 0;
593
594         if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
595             atomic_read(&cx->ana_capturing) > 0) {
596                 /* Switching standard would turn off the radio or mess
597                    with already running streams, prevent that by
598                    returning EBUSY. */
599                 return -EBUSY;
600         }
601
602         cx->std = *std;
603         cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
604         cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
605         cx->params.width = 720;
606         cx->params.height = cx->is_50hz ? 576 : 480;
607         cx->vbi.count = cx->is_50hz ? 18 : 12;
608         cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
609         cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
610         cx->vbi.sliced_decoder_line_size = cx->is_60hz ? 272 : 284;
611         CX18_DEBUG_INFO("Switching standard to %llx.\n",
612                         (unsigned long long) cx->std);
613
614         /* Tuner */
615         cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
616         return 0;
617 }
618
619 static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
620 {
621         struct cx18_open_id *id = fh;
622         struct cx18 *cx = id->cx;
623         int ret;
624
625         ret = v4l2_prio_check(&cx->prio, &id->prio);
626         if (ret)
627                 return ret;
628
629         CX18_DEBUG_IOCTL("VIDIOC_S_TUNER\n");
630
631         if (vt->index != 0)
632                 return -EINVAL;
633
634         /* Setting tuner can only set audio mode */
635         cx18_call_i2c_clients(cx, VIDIOC_S_TUNER, vt);
636
637         return 0;
638 }
639
640 static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
641 {
642         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
643
644         CX18_DEBUG_IOCTL("VIDIOC_G_TUNER\n");
645
646         if (vt->index != 0)
647                 return -EINVAL;
648
649         cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, vt);
650
651         if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
652                 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
653                 vt->type = V4L2_TUNER_RADIO;
654         } else {
655                 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
656                 vt->type = V4L2_TUNER_ANALOG_TV;
657         }
658
659         return 0;
660 }
661
662 static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
663                                         struct v4l2_sliced_vbi_cap *cap)
664 {
665         return -EINVAL;
666 }
667
668 static int cx18_g_enc_index(struct file *file, void *fh,
669                                 struct v4l2_enc_idx *idx)
670 {
671         return -EINVAL;
672 }
673
674 static int cx18_encoder_cmd(struct file *file, void *fh,
675                                 struct v4l2_encoder_cmd *enc)
676 {
677         struct cx18_open_id *id = fh;
678         struct cx18 *cx = id->cx;
679
680         CX18_DEBUG_IOCTL("VIDIOC_ENCODER_CMD:\n");
681
682         switch (enc->cmd) {
683         case V4L2_ENC_CMD_START:
684                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
685                 enc->flags = 0;
686                 return cx18_start_capture(id);
687
688         case V4L2_ENC_CMD_STOP:
689                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
690                 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
691                 cx18_stop_capture(id,
692                                   enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
693                 break;
694
695         case V4L2_ENC_CMD_PAUSE:
696                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
697                 enc->flags = 0;
698                 if (!atomic_read(&cx->ana_capturing))
699                         return -EPERM;
700                 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
701                         return 0;
702                 cx18_mute(cx);
703                 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, cx18_find_handle(cx));
704                 break;
705
706         case V4L2_ENC_CMD_RESUME:
707                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
708                 enc->flags = 0;
709                 if (!atomic_read(&cx->ana_capturing))
710                         return -EPERM;
711                 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
712                         return 0;
713                 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, cx18_find_handle(cx));
714                 cx18_unmute(cx);
715                 break;
716
717         default:
718                 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
719                 return -EINVAL;
720         }
721         return 0;
722 }
723
724 static int cx18_try_encoder_cmd(struct file *file, void *fh,
725                                 struct v4l2_encoder_cmd *enc)
726 {
727         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
728
729         CX18_DEBUG_IOCTL("VIDIOC_TRY_ENCDOER_CMD:\n");
730
731         switch (enc->cmd) {
732         case V4L2_ENC_CMD_START:
733                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
734                 enc->flags = 0;
735                 break;
736
737         case V4L2_ENC_CMD_STOP:
738                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
739                 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
740                 break;
741
742         case V4L2_ENC_CMD_PAUSE:
743                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
744                 enc->flags = 0;
745                 break;
746
747         case V4L2_ENC_CMD_RESUME:
748                 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
749                 enc->flags = 0;
750                 break;
751
752         default:
753                 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
754                 return -EINVAL;
755         }
756         return 0;
757 }
758
759 static int cx18_log_status(struct file *file, void *fh)
760 {
761         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
762         struct v4l2_input vidin;
763         struct v4l2_audio audin;
764         int i;
765
766         CX18_DEBUG_IOCTL("VIDIOC_LOG_STATUS\n");
767         CX18_INFO("=================  START STATUS CARD #%d  =================\n", cx->num);
768         if (cx->hw_flags & CX18_HW_TVEEPROM) {
769                 struct tveeprom tv;
770
771                 cx18_read_eeprom(cx, &tv);
772         }
773         cx18_call_i2c_clients(cx, VIDIOC_LOG_STATUS, NULL);
774         cx18_get_input(cx, cx->active_input, &vidin);
775         cx18_get_audio_input(cx, cx->audio_input, &audin);
776         CX18_INFO("Video Input: %s\n", vidin.name);
777         CX18_INFO("Audio Input: %s\n", audin.name);
778         CX18_INFO("Tuner: %s\n",
779                 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ?  "Radio" : "TV");
780         cx2341x_log_status(&cx->params, cx->name);
781         CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
782         for (i = 0; i < CX18_MAX_STREAMS; i++) {
783                 struct cx18_stream *s = &cx->streams[i];
784
785                 if (s->v4l2dev == NULL || s->buffers == 0)
786                         continue;
787                 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
788                           s->name, s->s_flags,
789                           (s->buffers - s->q_free.buffers) * 100 / s->buffers,
790                           (s->buffers * s->buf_size) / 1024, s->buffers);
791         }
792         CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
793                         (long long)cx->mpg_data_received,
794                         (long long)cx->vbi_data_inserted);
795         CX18_INFO("==================  END STATUS CARD #%d  ==================\n", cx->num);
796         return 0;
797 }
798
799 static int cx18_default(struct file *file, void *fh, int cmd, void *arg)
800 {
801         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
802
803         switch (cmd) {
804         case VIDIOC_INT_S_AUDIO_ROUTING: {
805                 struct v4l2_routing *route = arg;
806                 CX18_DEBUG_IOCTL("VIDIOC_INT_S_AUDIO_ROUTING (%d, %d)\n",
807                                         route->input, route->output);
808                 cx18_audio_set_route(cx, route);
809                 break;
810         }
811         case VIDIOC_INT_RESET: {
812                 u32 val = *(u32 *)arg;
813                 CX18_DEBUG_IOCTL("VIDIOC_INT_RESET (%#10x)\n", val);
814                 /* No op right now */
815                 /* cx18_av_cmd(cx, cmd, arg) */
816                 /* cx18_call_i2c_clients(cx, cmd, arg) */
817                 break;
818         }
819         default:
820                 if (cx18_debug & CX18_DBGFLG_IOCTL) {
821                         printk(KERN_INFO "cx18%d ioctl: unsupported cmd: ",
822                                  cx->num);
823                         v4l_printk_ioctl(cmd);
824                         printk("\n");
825                 }
826                 return -EINVAL;
827         }
828         return 0;
829 }
830
831 int cx18_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
832                     unsigned long arg)
833 {
834         struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
835         struct cx18 *cx = id->cx;
836         int res;
837
838         mutex_lock(&cx->serialize_lock);
839         res = video_ioctl2(inode, filp, cmd, arg);
840         mutex_unlock(&cx->serialize_lock);
841         return res;
842 }
843
844 void cx18_set_funcs(struct video_device *vdev)
845 {
846         vdev->vidioc_querycap                = cx18_querycap;
847         vdev->vidioc_g_priority              = cx18_g_priority;
848         vdev->vidioc_s_priority              = cx18_s_priority;
849         vdev->vidioc_s_audio                 = cx18_s_audio;
850         vdev->vidioc_g_audio                 = cx18_g_audio;
851         vdev->vidioc_enumaudio               = cx18_enumaudio;
852         vdev->vidioc_enum_input              = cx18_enum_input;
853         vdev->vidioc_cropcap                 = cx18_cropcap;
854         vdev->vidioc_s_crop                  = cx18_s_crop;
855         vdev->vidioc_g_crop                  = cx18_g_crop;
856         vdev->vidioc_g_input                 = cx18_g_input;
857         vdev->vidioc_s_input                 = cx18_s_input;
858         vdev->vidioc_g_frequency             = cx18_g_frequency;
859         vdev->vidioc_s_frequency             = cx18_s_frequency;
860         vdev->vidioc_s_tuner                 = cx18_s_tuner;
861         vdev->vidioc_g_tuner                 = cx18_g_tuner;
862         vdev->vidioc_g_enc_index             = cx18_g_enc_index;
863         vdev->vidioc_g_std                   = cx18_g_std;
864         vdev->vidioc_s_std                   = cx18_s_std;
865         vdev->vidioc_log_status              = cx18_log_status;
866         vdev->vidioc_enum_fmt_vid_cap        = cx18_enum_fmt_vid_cap;
867         vdev->vidioc_encoder_cmd             = cx18_encoder_cmd;
868         vdev->vidioc_try_encoder_cmd         = cx18_try_encoder_cmd;
869         vdev->vidioc_g_fmt_vid_cap           = cx18_g_fmt_vid_cap;
870         vdev->vidioc_g_fmt_vbi_cap           = cx18_g_fmt_vbi_cap;
871         vdev->vidioc_g_fmt_sliced_vbi_cap    = cx18_g_fmt_sliced_vbi_cap;
872         vdev->vidioc_s_fmt_vid_cap           = cx18_s_fmt_vid_cap;
873         vdev->vidioc_s_fmt_vbi_cap           = cx18_s_fmt_vbi_cap;
874         vdev->vidioc_s_fmt_sliced_vbi_cap    = cx18_s_fmt_sliced_vbi_cap;
875         vdev->vidioc_try_fmt_vid_cap         = cx18_try_fmt_vid_cap;
876         vdev->vidioc_try_fmt_vbi_cap         = cx18_try_fmt_vbi_cap;
877         vdev->vidioc_try_fmt_sliced_vbi_cap  = cx18_try_fmt_sliced_vbi_cap;
878         vdev->vidioc_g_sliced_vbi_cap        = cx18_g_sliced_vbi_cap;
879         vdev->vidioc_g_chip_ident            = cx18_g_chip_ident;
880         vdev->vidioc_g_register              = cx18_g_register;
881         vdev->vidioc_s_register              = cx18_s_register;
882         vdev->vidioc_default                 = cx18_default;
883         vdev->vidioc_queryctrl               = cx18_queryctrl;
884         vdev->vidioc_querymenu               = cx18_querymenu;
885         vdev->vidioc_g_ctrl                  = cx18_g_ctrl;
886         vdev->vidioc_s_ctrl                  = cx18_s_ctrl;
887         vdev->vidioc_g_ext_ctrls             = cx18_g_ext_ctrls;
888         vdev->vidioc_s_ext_ctrls             = cx18_s_ext_ctrls;
889         vdev->vidioc_try_ext_ctrls           = cx18_try_ext_ctrls;
890 }