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