03a94e6a30ef060b75cb550952b12baf6d66d685
[pandora-kernel.git] / drivers / media / video / cx231xx / cx231xx-video.c
1 /*
2    cx231xx-video.c - driver for Conexant Cx23100/101/102
3                      USB video capture devices
4
5    Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6         Based on em28xx driver
7         Based on cx23885 driver
8         Based on cx88 driver
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/bitmap.h>
30 #include <linux/usb.h>
31 #include <linux/i2c.h>
32 #include <linux/version.h>
33 #include <linux/mm.h>
34 #include <linux/mutex.h>
35 #include <linux/slab.h>
36
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-chip-ident.h>
40 #include <media/msp3400.h>
41 #include <media/tuner.h>
42
43 #include "dvb_frontend.h"
44
45 #include "cx231xx.h"
46 #include "cx231xx-vbi.h"
47
48 #define CX231XX_VERSION_CODE            KERNEL_VERSION(0, 0, 1)
49
50 #define DRIVER_AUTHOR   "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
51 #define DRIVER_DESC     "Conexant cx231xx based USB video device driver"
52
53 #define cx231xx_videodbg(fmt, arg...) do {\
54         if (video_debug) \
55                 printk(KERN_INFO "%s %s :"fmt, \
56                          dev->name, __func__ , ##arg); } while (0)
57
58 static unsigned int isoc_debug;
59 module_param(isoc_debug, int, 0644);
60 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
61
62 #define cx231xx_isocdbg(fmt, arg...) \
63 do {\
64         if (isoc_debug) { \
65                 printk(KERN_INFO "%s %s :"fmt, \
66                          dev->name, __func__ , ##arg); \
67         } \
68   } while (0)
69
70 MODULE_AUTHOR(DRIVER_AUTHOR);
71 MODULE_DESCRIPTION(DRIVER_DESC);
72 MODULE_LICENSE("GPL");
73
74 static unsigned int card[]     = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
75 static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76 static unsigned int vbi_nr[]   = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
77 static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
78
79 module_param_array(card, int, NULL, 0444);
80 module_param_array(video_nr, int, NULL, 0444);
81 module_param_array(vbi_nr, int, NULL, 0444);
82 module_param_array(radio_nr, int, NULL, 0444);
83
84 MODULE_PARM_DESC(card, "card type");
85 MODULE_PARM_DESC(video_nr, "video device numbers");
86 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
87 MODULE_PARM_DESC(radio_nr, "radio device numbers");
88
89 static unsigned int video_debug;
90 module_param(video_debug, int, 0644);
91 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
92
93 /* supported video standards */
94 static struct cx231xx_fmt format[] = {
95         {
96          .name = "16bpp YUY2, 4:2:2, packed",
97          .fourcc = V4L2_PIX_FMT_YUYV,
98          .depth = 16,
99          .reg = 0,
100          },
101 };
102
103 /* supported controls */
104 /* Common to all boards */
105
106 /* ------------------------------------------------------------------- */
107
108 static const struct v4l2_queryctrl no_ctl = {
109         .name = "42",
110         .flags = V4L2_CTRL_FLAG_DISABLED,
111 };
112
113 static struct cx231xx_ctrl cx231xx_ctls[] = {
114         /* --- video --- */
115         {
116                 .v = {
117                         .id = V4L2_CID_BRIGHTNESS,
118                         .name = "Brightness",
119                         .minimum = 0x00,
120                         .maximum = 0xff,
121                         .step = 1,
122                         .default_value = 0x7f,
123                         .type = V4L2_CTRL_TYPE_INTEGER,
124                 },
125                 .off = 128,
126                 .reg = LUMA_CTRL,
127                 .mask = 0x00ff,
128                 .shift = 0,
129         }, {
130                 .v = {
131                         .id = V4L2_CID_CONTRAST,
132                         .name = "Contrast",
133                         .minimum = 0,
134                         .maximum = 0xff,
135                         .step = 1,
136                         .default_value = 0x3f,
137                         .type = V4L2_CTRL_TYPE_INTEGER,
138                 },
139                 .off = 0,
140                 .reg = LUMA_CTRL,
141                 .mask = 0xff00,
142                 .shift = 8,
143         }, {
144                 .v = {
145                         .id = V4L2_CID_HUE,
146                         .name = "Hue",
147                         .minimum = 0,
148                         .maximum = 0xff,
149                         .step = 1,
150                         .default_value = 0x7f,
151                         .type = V4L2_CTRL_TYPE_INTEGER,
152                 },
153                 .off = 128,
154                 .reg = CHROMA_CTRL,
155                 .mask = 0xff0000,
156                 .shift = 16,
157         }, {
158         /* strictly, this only describes only U saturation.
159         * V saturation is handled specially through code.
160         */
161                 .v = {
162                         .id = V4L2_CID_SATURATION,
163                         .name = "Saturation",
164                         .minimum = 0,
165                         .maximum = 0xff,
166                         .step = 1,
167                         .default_value = 0x7f,
168                         .type = V4L2_CTRL_TYPE_INTEGER,
169                 },
170                 .off = 0,
171                 .reg = CHROMA_CTRL,
172                 .mask = 0x00ff,
173                 .shift = 0,
174         }, {
175                 /* --- audio --- */
176                 .v = {
177                         .id = V4L2_CID_AUDIO_MUTE,
178                         .name = "Mute",
179                         .minimum = 0,
180                         .maximum = 1,
181                         .default_value = 1,
182                         .type = V4L2_CTRL_TYPE_BOOLEAN,
183                 },
184                 .reg = PATH1_CTL1,
185                 .mask = (0x1f << 24),
186                 .shift = 24,
187         }, {
188                 .v = {
189                         .id = V4L2_CID_AUDIO_VOLUME,
190                         .name = "Volume",
191                         .minimum = 0,
192                         .maximum = 0x3f,
193                         .step = 1,
194                         .default_value = 0x3f,
195                         .type = V4L2_CTRL_TYPE_INTEGER,
196                 },
197                 .reg = PATH1_VOL_CTL,
198                 .mask = 0xff,
199                 .shift = 0,
200         }
201 };
202 static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
203
204 static const u32 cx231xx_user_ctrls[] = {
205         V4L2_CID_USER_CLASS,
206         V4L2_CID_BRIGHTNESS,
207         V4L2_CID_CONTRAST,
208         V4L2_CID_SATURATION,
209         V4L2_CID_HUE,
210         V4L2_CID_AUDIO_VOLUME,
211 #if 0
212         V4L2_CID_AUDIO_BALANCE,
213 #endif
214         V4L2_CID_AUDIO_MUTE,
215         0
216 };
217
218 static const u32 *ctrl_classes[] = {
219         cx231xx_user_ctrls,
220         NULL
221 };
222
223 /* ------------------------------------------------------------------
224         Video buffer and parser functions
225    ------------------------------------------------------------------*/
226
227 /*
228  * Announces that a buffer were filled and request the next
229  */
230 static inline void buffer_filled(struct cx231xx *dev,
231                                  struct cx231xx_dmaqueue *dma_q,
232                                  struct cx231xx_buffer *buf)
233 {
234         /* Advice that buffer was filled */
235         cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
236         buf->vb.state = VIDEOBUF_DONE;
237         buf->vb.field_count++;
238         do_gettimeofday(&buf->vb.ts);
239
240         if (dev->USE_ISO)
241                 dev->video_mode.isoc_ctl.buf = NULL;
242         else
243                 dev->video_mode.bulk_ctl.buf = NULL;
244
245         list_del(&buf->vb.queue);
246         wake_up(&buf->vb.done);
247 }
248
249 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
250 {
251         char *errmsg = "Unknown";
252
253         switch (status) {
254         case -ENOENT:
255                 errmsg = "unlinked synchronuously";
256                 break;
257         case -ECONNRESET:
258                 errmsg = "unlinked asynchronuously";
259                 break;
260         case -ENOSR:
261                 errmsg = "Buffer error (overrun)";
262                 break;
263         case -EPIPE:
264                 errmsg = "Stalled (device not responding)";
265                 break;
266         case -EOVERFLOW:
267                 errmsg = "Babble (bad cable?)";
268                 break;
269         case -EPROTO:
270                 errmsg = "Bit-stuff error (bad cable?)";
271                 break;
272         case -EILSEQ:
273                 errmsg = "CRC/Timeout (could be anything)";
274                 break;
275         case -ETIME:
276                 errmsg = "Device does not respond";
277                 break;
278         }
279         if (packet < 0) {
280                 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
281         } else {
282                 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
283                                 packet, status, errmsg);
284         }
285 }
286
287 /*
288  * video-buf generic routine to get the next available buffer
289  */
290 static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
291                                 struct cx231xx_buffer **buf)
292 {
293         struct cx231xx_video_mode *vmode =
294             container_of(dma_q, struct cx231xx_video_mode, vidq);
295         struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
296
297         char *outp;
298
299         if (list_empty(&dma_q->active)) {
300                 cx231xx_isocdbg("No active queue to serve\n");
301                 if (dev->USE_ISO)
302                         dev->video_mode.isoc_ctl.buf = NULL;
303                 else
304                         dev->video_mode.bulk_ctl.buf = NULL;
305                 *buf = NULL;
306                 return;
307         }
308
309         /* Get the next buffer */
310         *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
311
312         /* Cleans up buffer - Usefull for testing for frame/URB loss */
313         outp = videobuf_to_vmalloc(&(*buf)->vb);
314         memset(outp, 0, (*buf)->vb.size);
315
316         if (dev->USE_ISO)
317                 dev->video_mode.isoc_ctl.buf = *buf;
318         else
319                 dev->video_mode.bulk_ctl.buf = *buf;
320
321         return;
322 }
323
324 /*
325  * Controls the isoc copy of each urb packet
326  */
327 static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
328 {
329         struct cx231xx_buffer *buf;
330         struct cx231xx_dmaqueue *dma_q = urb->context;
331         unsigned char *outp = NULL;
332         int i, rc = 1;
333         unsigned char *p_buffer;
334         u32 bytes_parsed = 0, buffer_size = 0;
335         u8 sav_eav = 0;
336
337         if (!dev)
338                 return 0;
339
340         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
341                 return 0;
342
343         if (urb->status < 0) {
344                 print_err_status(dev, -1, urb->status);
345                 if (urb->status == -ENOENT)
346                         return 0;
347         }
348
349         buf = dev->video_mode.isoc_ctl.buf;
350         if (buf != NULL)
351                 outp = videobuf_to_vmalloc(&buf->vb);
352
353         for (i = 0; i < urb->number_of_packets; i++) {
354                 int status = urb->iso_frame_desc[i].status;
355
356                 if (status < 0) {
357                         print_err_status(dev, i, status);
358                         if (urb->iso_frame_desc[i].status != -EPROTO)
359                                 continue;
360                 }
361
362                 if (urb->iso_frame_desc[i].actual_length <= 0) {
363                         /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
364                         continue;
365                 }
366                 if (urb->iso_frame_desc[i].actual_length >
367                     dev->video_mode.max_pkt_size) {
368                         cx231xx_isocdbg("packet bigger than packet size");
369                         continue;
370                 }
371
372                 /*  get buffer pointer and length */
373                 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
374                 buffer_size = urb->iso_frame_desc[i].actual_length;
375                 bytes_parsed = 0;
376
377                 if (dma_q->is_partial_line) {
378                         /* Handle the case of a partial line */
379                         sav_eav = dma_q->last_sav;
380                 } else {
381                         /* Check for a SAV/EAV overlapping
382                                 the buffer boundary */
383                         sav_eav =
384                             cx231xx_find_boundary_SAV_EAV(p_buffer,
385                                                           dma_q->partial_buf,
386                                                           &bytes_parsed);
387                 }
388
389                 sav_eav &= 0xF0;
390                 /* Get the first line if we have some portion of an SAV/EAV from
391                    the last buffer or a partial line  */
392                 if (sav_eav) {
393                         bytes_parsed += cx231xx_get_video_line(dev, dma_q,
394                                 sav_eav,        /* SAV/EAV */
395                                 p_buffer + bytes_parsed,        /* p_buffer */
396                                 buffer_size - bytes_parsed);/* buf size */
397                 }
398
399                 /* Now parse data that is completely in this buffer */
400                 /* dma_q->is_partial_line = 0;  */
401
402                 while (bytes_parsed < buffer_size) {
403                         u32 bytes_used = 0;
404
405                         sav_eav = cx231xx_find_next_SAV_EAV(
406                                 p_buffer + bytes_parsed,        /* p_buffer */
407                                 buffer_size - bytes_parsed,     /* buf size */
408                                 &bytes_used);/* bytes used to get SAV/EAV */
409
410                         bytes_parsed += bytes_used;
411
412                         sav_eav &= 0xF0;
413                         if (sav_eav && (bytes_parsed < buffer_size)) {
414                                 bytes_parsed += cx231xx_get_video_line(dev,
415                                         dma_q, sav_eav, /* SAV/EAV */
416                                         p_buffer + bytes_parsed,/* p_buffer */
417                                         buffer_size - bytes_parsed);/*buf size*/
418                         }
419                 }
420
421                 /* Save the last four bytes of the buffer so we can check the
422                    buffer boundary condition next time */
423                 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
424                 bytes_parsed = 0;
425
426         }
427         return rc;
428 }
429
430 static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
431 {
432         struct cx231xx_buffer *buf;
433         struct cx231xx_dmaqueue *dma_q = urb->context;
434         unsigned char *outp = NULL;
435         int rc = 1;
436         unsigned char *p_buffer;
437         u32 bytes_parsed = 0, buffer_size = 0;
438         u8 sav_eav = 0;
439
440         if (!dev)
441                 return 0;
442
443         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
444                 return 0;
445
446         if (urb->status < 0) {
447                 print_err_status(dev, -1, urb->status);
448                 if (urb->status == -ENOENT)
449                         return 0;
450         }
451
452         buf = dev->video_mode.bulk_ctl.buf;
453         if (buf != NULL)
454                 outp = videobuf_to_vmalloc(&buf->vb);
455
456         if (1) {
457
458                 /*  get buffer pointer and length */
459                 p_buffer = urb->transfer_buffer;
460                 buffer_size = urb->actual_length;
461                 bytes_parsed = 0;
462
463                 if (dma_q->is_partial_line) {
464                         /* Handle the case of a partial line */
465                         sav_eav = dma_q->last_sav;
466                 } else {
467                         /* Check for a SAV/EAV overlapping
468                                 the buffer boundary */
469                         sav_eav =
470                             cx231xx_find_boundary_SAV_EAV(p_buffer,
471                                                           dma_q->partial_buf,
472                                                           &bytes_parsed);
473                 }
474
475                 sav_eav &= 0xF0;
476                 /* Get the first line if we have some portion of an SAV/EAV from
477                    the last buffer or a partial line  */
478                 if (sav_eav) {
479                         bytes_parsed += cx231xx_get_video_line(dev, dma_q,
480                                 sav_eav,        /* SAV/EAV */
481                                 p_buffer + bytes_parsed,        /* p_buffer */
482                                 buffer_size - bytes_parsed);/* buf size */
483                 }
484
485                 /* Now parse data that is completely in this buffer */
486                 /* dma_q->is_partial_line = 0;  */
487
488                 while (bytes_parsed < buffer_size) {
489                         u32 bytes_used = 0;
490
491                         sav_eav = cx231xx_find_next_SAV_EAV(
492                                 p_buffer + bytes_parsed,        /* p_buffer */
493                                 buffer_size - bytes_parsed,     /* buf size */
494                                 &bytes_used);/* bytes used to get SAV/EAV */
495
496                         bytes_parsed += bytes_used;
497
498                         sav_eav &= 0xF0;
499                         if (sav_eav && (bytes_parsed < buffer_size)) {
500                                 bytes_parsed += cx231xx_get_video_line(dev,
501                                         dma_q, sav_eav, /* SAV/EAV */
502                                         p_buffer + bytes_parsed,/* p_buffer */
503                                         buffer_size - bytes_parsed);/*buf size*/
504                         }
505                 }
506
507                 /* Save the last four bytes of the buffer so we can check the
508                    buffer boundary condition next time */
509                 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
510                 bytes_parsed = 0;
511
512         }
513         return rc;
514 }
515
516
517 u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
518                                  u32 *p_bytes_used)
519 {
520         u32 bytes_used;
521         u8 boundary_bytes[8];
522         u8 sav_eav = 0;
523
524         *p_bytes_used = 0;
525
526         /* Create an array of the last 4 bytes of the last buffer and the first
527            4 bytes of the current buffer. */
528
529         memcpy(boundary_bytes, partial_buf, 4);
530         memcpy(boundary_bytes + 4, p_buffer, 4);
531
532         /* Check for the SAV/EAV in the boundary buffer */
533         sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
534                                             &bytes_used);
535
536         if (sav_eav) {
537                 /* found a boundary SAV/EAV.  Updates the bytes used to reflect
538                    only those used in the new buffer */
539                 *p_bytes_used = bytes_used - 4;
540         }
541
542         return sav_eav;
543 }
544
545 u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
546 {
547         u32 i;
548         u8 sav_eav = 0;
549
550         /*
551          * Don't search if the buffer size is less than 4.  It causes a page
552          * fault since buffer_size - 4 evaluates to a large number in that
553          * case.
554          */
555         if (buffer_size < 4) {
556                 *p_bytes_used = buffer_size;
557                 return 0;
558         }
559
560         for (i = 0; i < (buffer_size - 3); i++) {
561
562                 if ((p_buffer[i] == 0xFF) &&
563                     (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
564
565                         *p_bytes_used = i + 4;
566                         sav_eav = p_buffer[i + 3];
567                         return sav_eav;
568                 }
569         }
570
571         *p_bytes_used = buffer_size;
572         return 0;
573 }
574
575 u32 cx231xx_get_video_line(struct cx231xx *dev,
576                            struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
577                            u8 *p_buffer, u32 buffer_size)
578 {
579         u32 bytes_copied = 0;
580         int current_field = -1;
581
582         switch (sav_eav) {
583         case SAV_ACTIVE_VIDEO_FIELD1:
584                 /* looking for skipped line which occurred in PAL 720x480 mode.
585                    In this case, there will be no active data contained
586                    between the SAV and EAV */
587                 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
588                     (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
589                     ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
590                      (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
591                      (p_buffer[3] == EAV_VBLANK_FIELD1) ||
592                      (p_buffer[3] == EAV_VBLANK_FIELD2)))
593                         return bytes_copied;
594                 current_field = 1;
595                 break;
596
597         case SAV_ACTIVE_VIDEO_FIELD2:
598                 /* looking for skipped line which occurred in PAL 720x480 mode.
599                    In this case, there will be no active data contained between
600                    the SAV and EAV */
601                 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
602                     (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
603                     ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
604                      (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
605                      (p_buffer[3] == EAV_VBLANK_FIELD1)       ||
606                      (p_buffer[3] == EAV_VBLANK_FIELD2)))
607                         return bytes_copied;
608                 current_field = 2;
609                 break;
610         }
611
612         dma_q->last_sav = sav_eav;
613
614         bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
615                                                buffer_size, current_field);
616
617         return bytes_copied;
618 }
619
620 u32 cx231xx_copy_video_line(struct cx231xx *dev,
621                             struct cx231xx_dmaqueue *dma_q, u8 *p_line,
622                             u32 length, int field_number)
623 {
624         u32 bytes_to_copy;
625         struct cx231xx_buffer *buf;
626         u32 _line_size = dev->width * 2;
627
628         if (dma_q->current_field != field_number)
629                 cx231xx_reset_video_buffer(dev, dma_q);
630
631         /* get the buffer pointer */
632         if (dev->USE_ISO)
633                 buf = dev->video_mode.isoc_ctl.buf;
634         else
635                 buf = dev->video_mode.bulk_ctl.buf;
636
637         /* Remember the field number for next time */
638         dma_q->current_field = field_number;
639
640         bytes_to_copy = dma_q->bytes_left_in_line;
641         if (bytes_to_copy > length)
642                 bytes_to_copy = length;
643
644         if (dma_q->lines_completed >= dma_q->lines_per_field) {
645                 dma_q->bytes_left_in_line -= bytes_to_copy;
646                 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
647                                           0 : 1;
648                 return 0;
649         }
650
651         dma_q->is_partial_line = 1;
652
653         /* If we don't have a buffer, just return the number of bytes we would
654            have copied if we had a buffer. */
655         if (!buf) {
656                 dma_q->bytes_left_in_line -= bytes_to_copy;
657                 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
658                                          ? 0 : 1;
659                 return bytes_to_copy;
660         }
661
662         /* copy the data to video buffer */
663         cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
664
665         dma_q->pos += bytes_to_copy;
666         dma_q->bytes_left_in_line -= bytes_to_copy;
667
668         if (dma_q->bytes_left_in_line == 0) {
669                 dma_q->bytes_left_in_line = _line_size;
670                 dma_q->lines_completed++;
671                 dma_q->is_partial_line = 0;
672
673                 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
674                         buffer_filled(dev, dma_q, buf);
675
676                         dma_q->pos = 0;
677                         buf = NULL;
678                         dma_q->lines_completed = 0;
679                 }
680         }
681
682         return bytes_to_copy;
683 }
684
685 void cx231xx_reset_video_buffer(struct cx231xx *dev,
686                                 struct cx231xx_dmaqueue *dma_q)
687 {
688         struct cx231xx_buffer *buf;
689
690         /* handle the switch from field 1 to field 2 */
691         if (dma_q->current_field == 1) {
692                 if (dma_q->lines_completed >= dma_q->lines_per_field)
693                         dma_q->field1_done = 1;
694                 else
695                         dma_q->field1_done = 0;
696         }
697
698         if (dev->USE_ISO)
699                 buf = dev->video_mode.isoc_ctl.buf;
700         else
701                 buf = dev->video_mode.bulk_ctl.buf;
702
703         if (buf == NULL) {
704                 u8 *outp = NULL;
705                 /* first try to get the buffer */
706                 get_next_buf(dma_q, &buf);
707
708                 if (buf)
709                         outp = videobuf_to_vmalloc(&buf->vb);
710
711                 dma_q->pos = 0;
712                 dma_q->field1_done = 0;
713                 dma_q->current_field = -1;
714         }
715
716         /* reset the counters */
717         dma_q->bytes_left_in_line = dev->width << 1;
718         dma_q->lines_completed = 0;
719 }
720
721 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
722                     u8 *p_buffer, u32 bytes_to_copy)
723 {
724         u8 *p_out_buffer = NULL;
725         u32 current_line_bytes_copied = 0;
726         struct cx231xx_buffer *buf;
727         u32 _line_size = dev->width << 1;
728         void *startwrite;
729         int offset, lencopy;
730
731         if (dev->USE_ISO)
732                 buf = dev->video_mode.isoc_ctl.buf;
733         else
734                 buf = dev->video_mode.bulk_ctl.buf;
735
736         if (buf == NULL)
737                 return -1;
738
739         p_out_buffer = videobuf_to_vmalloc(&buf->vb);
740
741         current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
742
743         /* Offset field 2 one line from the top of the buffer */
744         offset = (dma_q->current_field == 1) ? 0 : _line_size;
745
746         /* Offset for field 2 */
747         startwrite = p_out_buffer + offset;
748
749         /* lines already completed in the current field */
750         startwrite += (dma_q->lines_completed * _line_size * 2);
751
752         /* bytes already completed in the current line */
753         startwrite += current_line_bytes_copied;
754
755         lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
756                   bytes_to_copy : dma_q->bytes_left_in_line;
757
758         if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
759                 return 0;
760
761         /* The below copies the UYVY data straight into video buffer */
762         cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
763
764         return 0;
765 }
766
767 void cx231xx_swab(u16 *from, u16 *to, u16 len)
768 {
769         u16 i;
770
771         if (len <= 0)
772                 return;
773
774         for (i = 0; i < len / 2; i++)
775                 to[i] = (from[i] << 8) | (from[i] >> 8);
776 }
777
778 u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
779 {
780         u8 buffer_complete = 0;
781
782         /* Dual field stream */
783         buffer_complete = ((dma_q->current_field == 2) &&
784                            (dma_q->lines_completed >= dma_q->lines_per_field) &&
785                             dma_q->field1_done);
786
787         return buffer_complete;
788 }
789
790 /* ------------------------------------------------------------------
791         Videobuf operations
792    ------------------------------------------------------------------*/
793
794 static int
795 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
796 {
797         struct cx231xx_fh *fh = vq->priv_data;
798         struct cx231xx *dev = fh->dev;
799
800         *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
801         if (0 == *count)
802                 *count = CX231XX_DEF_BUF;
803
804         if (*count < CX231XX_MIN_BUF)
805                 *count = CX231XX_MIN_BUF;
806
807         return 0;
808 }
809
810 /* This is called *without* dev->slock held; please keep it that way */
811 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
812 {
813         struct cx231xx_fh *fh = vq->priv_data;
814         struct cx231xx *dev = fh->dev;
815         unsigned long flags = 0;
816
817         if (in_interrupt())
818                 BUG();
819
820         /* We used to wait for the buffer to finish here, but this didn't work
821            because, as we were keeping the state as VIDEOBUF_QUEUED,
822            videobuf_queue_cancel marked it as finished for us.
823            (Also, it could wedge forever if the hardware was misconfigured.)
824
825            This should be safe; by the time we get here, the buffer isn't
826            queued anymore. If we ever start marking the buffers as
827            VIDEOBUF_ACTIVE, it won't be, though.
828          */
829         spin_lock_irqsave(&dev->video_mode.slock, flags);
830         if (dev->USE_ISO) {
831                 if (dev->video_mode.isoc_ctl.buf == buf)
832                         dev->video_mode.isoc_ctl.buf = NULL;
833         } else {
834                 if (dev->video_mode.bulk_ctl.buf == buf)
835                         dev->video_mode.bulk_ctl.buf = NULL;
836         }
837         spin_unlock_irqrestore(&dev->video_mode.slock, flags);
838
839         videobuf_vmalloc_free(&buf->vb);
840         buf->vb.state = VIDEOBUF_NEEDS_INIT;
841 }
842
843 static int
844 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
845                enum v4l2_field field)
846 {
847         struct cx231xx_fh *fh = vq->priv_data;
848         struct cx231xx_buffer *buf =
849             container_of(vb, struct cx231xx_buffer, vb);
850         struct cx231xx *dev = fh->dev;
851         int rc = 0, urb_init = 0;
852
853         /* The only currently supported format is 16 bits/pixel */
854         buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
855                         + 7) >> 3;
856         if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
857                 return -EINVAL;
858
859         buf->vb.width = dev->width;
860         buf->vb.height = dev->height;
861         buf->vb.field = field;
862
863         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
864                 rc = videobuf_iolock(vq, &buf->vb, NULL);
865                 if (rc < 0)
866                         goto fail;
867         }
868
869         if (dev->USE_ISO) {
870                 if (!dev->video_mode.isoc_ctl.num_bufs)
871                         urb_init = 1;
872         } else {
873                 if (!dev->video_mode.bulk_ctl.num_bufs)
874                         urb_init = 1;
875         }
876         /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
877                 urb_init, dev->video_mode.max_pkt_size);*/
878         if (urb_init) {
879                 dev->mode_tv = 0;
880                 if (dev->USE_ISO)
881                         rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
882                                        CX231XX_NUM_BUFS,
883                                        dev->video_mode.max_pkt_size,
884                                        cx231xx_isoc_copy);
885                 else
886                         rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
887                                        CX231XX_NUM_BUFS,
888                                        dev->video_mode.max_pkt_size,
889                                        cx231xx_bulk_copy);
890                 if (rc < 0)
891                         goto fail;
892         }
893
894         buf->vb.state = VIDEOBUF_PREPARED;
895         return 0;
896
897 fail:
898         free_buffer(vq, buf);
899         return rc;
900 }
901
902 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
903 {
904         struct cx231xx_buffer *buf =
905             container_of(vb, struct cx231xx_buffer, vb);
906         struct cx231xx_fh *fh = vq->priv_data;
907         struct cx231xx *dev = fh->dev;
908         struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
909
910         buf->vb.state = VIDEOBUF_QUEUED;
911         list_add_tail(&buf->vb.queue, &vidq->active);
912
913 }
914
915 static void buffer_release(struct videobuf_queue *vq,
916                            struct videobuf_buffer *vb)
917 {
918         struct cx231xx_buffer *buf =
919             container_of(vb, struct cx231xx_buffer, vb);
920         struct cx231xx_fh *fh = vq->priv_data;
921         struct cx231xx *dev = (struct cx231xx *)fh->dev;
922
923         cx231xx_isocdbg("cx231xx: called buffer_release\n");
924
925         free_buffer(vq, buf);
926 }
927
928 static struct videobuf_queue_ops cx231xx_video_qops = {
929         .buf_setup = buffer_setup,
930         .buf_prepare = buffer_prepare,
931         .buf_queue = buffer_queue,
932         .buf_release = buffer_release,
933 };
934
935 /*********************  v4l2 interface  **************************************/
936
937 void video_mux(struct cx231xx *dev, int index)
938 {
939         dev->video_input = index;
940         dev->ctl_ainput = INPUT(index)->amux;
941
942         cx231xx_set_video_input_mux(dev, index);
943
944         cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
945
946         cx231xx_set_audio_input(dev, dev->ctl_ainput);
947
948         cx231xx_info("video_mux : %d\n", index);
949
950         /* do mode control overrides if required */
951         cx231xx_do_mode_ctrl_overrides(dev);
952 }
953
954 /* Usage lock check functions */
955 static int res_get(struct cx231xx_fh *fh)
956 {
957         struct cx231xx *dev = fh->dev;
958         int rc = 0;
959
960         /* This instance already has stream_on */
961         if (fh->stream_on)
962                 return rc;
963
964         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
965                 if (dev->stream_on)
966                         return -EBUSY;
967                 dev->stream_on = 1;
968         } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
969                 if (dev->vbi_stream_on)
970                         return -EBUSY;
971                 dev->vbi_stream_on = 1;
972         } else
973                 return -EINVAL;
974
975         fh->stream_on = 1;
976
977         return rc;
978 }
979
980 static int res_check(struct cx231xx_fh *fh)
981 {
982         return fh->stream_on;
983 }
984
985 static void res_free(struct cx231xx_fh *fh)
986 {
987         struct cx231xx *dev = fh->dev;
988
989         fh->stream_on = 0;
990
991         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
992                 dev->stream_on = 0;
993         if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
994                 dev->vbi_stream_on = 0;
995 }
996
997 static int check_dev(struct cx231xx *dev)
998 {
999         if (dev->state & DEV_DISCONNECTED) {
1000                 cx231xx_errdev("v4l2 ioctl: device not present\n");
1001                 return -ENODEV;
1002         }
1003
1004         if (dev->state & DEV_MISCONFIGURED) {
1005                 cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
1006                                "close and open it again\n");
1007                 return -EIO;
1008         }
1009         return 0;
1010 }
1011
1012 /* ------------------------------------------------------------------
1013         IOCTL vidioc handling
1014    ------------------------------------------------------------------*/
1015
1016 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1017                                 struct v4l2_format *f)
1018 {
1019         struct cx231xx_fh *fh = priv;
1020         struct cx231xx *dev = fh->dev;
1021
1022         mutex_lock(&dev->lock);
1023
1024         f->fmt.pix.width = dev->width;
1025         f->fmt.pix.height = dev->height;
1026         f->fmt.pix.pixelformat = dev->format->fourcc;
1027         f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
1028         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
1029         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1030
1031         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1032
1033         mutex_unlock(&dev->lock);
1034
1035         return 0;
1036 }
1037
1038 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
1039 {
1040         unsigned int i;
1041
1042         for (i = 0; i < ARRAY_SIZE(format); i++)
1043                 if (format[i].fourcc == fourcc)
1044                         return &format[i];
1045
1046         return NULL;
1047 }
1048
1049 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1050                                   struct v4l2_format *f)
1051 {
1052         struct cx231xx_fh *fh = priv;
1053         struct cx231xx *dev = fh->dev;
1054         unsigned int width = f->fmt.pix.width;
1055         unsigned int height = f->fmt.pix.height;
1056         unsigned int maxw = norm_maxw(dev);
1057         unsigned int maxh = norm_maxh(dev);
1058         struct cx231xx_fmt *fmt;
1059
1060         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1061         if (!fmt) {
1062                 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
1063                                  f->fmt.pix.pixelformat);
1064                 return -EINVAL;
1065         }
1066
1067         /* width must even because of the YUYV format
1068            height must be even because of interlacing */
1069         v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
1070
1071         f->fmt.pix.width = width;
1072         f->fmt.pix.height = height;
1073         f->fmt.pix.pixelformat = fmt->fourcc;
1074         f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1075         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1076         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1077         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1078
1079         return 0;
1080 }
1081
1082 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1083                                 struct v4l2_format *f)
1084 {
1085         struct cx231xx_fh *fh = priv;
1086         struct cx231xx *dev = fh->dev;
1087         int rc;
1088         struct cx231xx_fmt *fmt;
1089         struct v4l2_mbus_framefmt mbus_fmt;
1090
1091         rc = check_dev(dev);
1092         if (rc < 0)
1093                 return rc;
1094
1095         mutex_lock(&dev->lock);
1096
1097         vidioc_try_fmt_vid_cap(file, priv, f);
1098
1099         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1100         if (!fmt) {
1101                 rc = -EINVAL;
1102                 goto out;
1103         }
1104
1105         if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1106                 cx231xx_errdev("%s queue busy\n", __func__);
1107                 rc = -EBUSY;
1108                 goto out;
1109         }
1110
1111         if (dev->stream_on && !fh->stream_on) {
1112                 cx231xx_errdev("%s device in use by another fh\n", __func__);
1113                 rc = -EBUSY;
1114                 goto out;
1115         }
1116
1117         /* set new image size */
1118         dev->width = f->fmt.pix.width;
1119         dev->height = f->fmt.pix.height;
1120         dev->format = fmt;
1121
1122         v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1123         call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1124         v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
1125
1126 out:
1127         mutex_unlock(&dev->lock);
1128         return rc;
1129 }
1130
1131 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1132 {
1133         struct cx231xx_fh *fh = priv;
1134         struct cx231xx *dev = fh->dev;
1135
1136         *id = dev->norm;
1137         return 0;
1138 }
1139
1140 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1141 {
1142         struct cx231xx_fh *fh = priv;
1143         struct cx231xx *dev = fh->dev;
1144         struct v4l2_mbus_framefmt mbus_fmt;
1145         struct v4l2_format f;
1146         int rc;
1147
1148         rc = check_dev(dev);
1149         if (rc < 0)
1150                 return rc;
1151
1152         cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
1153
1154         mutex_lock(&dev->lock);
1155         dev->norm = *norm;
1156
1157         /* Adjusts width/height, if needed */
1158         f.fmt.pix.width = dev->width;
1159         f.fmt.pix.height = dev->height;
1160         vidioc_try_fmt_vid_cap(file, priv, &f);
1161
1162         call_all(dev, core, s_std, dev->norm);
1163
1164         /* We need to reset basic properties in the decoder related to
1165            resolution (since a standard change effects things like the number
1166            of lines in VACT, etc) */
1167         v4l2_fill_mbus_format(&mbus_fmt, &f.fmt.pix, V4L2_MBUS_FMT_FIXED);
1168         call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1169         v4l2_fill_pix_format(&f.fmt.pix, &mbus_fmt);
1170
1171         /* set new image size */
1172         dev->width = f.fmt.pix.width;
1173         dev->height = f.fmt.pix.height;
1174
1175         mutex_unlock(&dev->lock);
1176
1177         /* do mode control overrides */
1178         cx231xx_do_mode_ctrl_overrides(dev);
1179
1180         return 0;
1181 }
1182
1183 static const char *iname[] = {
1184         [CX231XX_VMUX_COMPOSITE1] = "Composite1",
1185         [CX231XX_VMUX_SVIDEO]     = "S-Video",
1186         [CX231XX_VMUX_TELEVISION] = "Television",
1187         [CX231XX_VMUX_CABLE]      = "Cable TV",
1188         [CX231XX_VMUX_DVB]        = "DVB",
1189         [CX231XX_VMUX_DEBUG]      = "for debug only",
1190 };
1191
1192 static int vidioc_enum_input(struct file *file, void *priv,
1193                              struct v4l2_input *i)
1194 {
1195         struct cx231xx_fh *fh = priv;
1196         struct cx231xx *dev = fh->dev;
1197         unsigned int n;
1198
1199         n = i->index;
1200         if (n >= MAX_CX231XX_INPUT)
1201                 return -EINVAL;
1202         if (0 == INPUT(n)->type)
1203                 return -EINVAL;
1204
1205         i->index = n;
1206         i->type = V4L2_INPUT_TYPE_CAMERA;
1207
1208         strcpy(i->name, iname[INPUT(n)->type]);
1209
1210         if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1211             (CX231XX_VMUX_CABLE == INPUT(n)->type))
1212                 i->type = V4L2_INPUT_TYPE_TUNER;
1213
1214         i->std = dev->vdev->tvnorms;
1215
1216         return 0;
1217 }
1218
1219 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1220 {
1221         struct cx231xx_fh *fh = priv;
1222         struct cx231xx *dev = fh->dev;
1223
1224         *i = dev->video_input;
1225
1226         return 0;
1227 }
1228
1229 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1230 {
1231         struct cx231xx_fh *fh = priv;
1232         struct cx231xx *dev = fh->dev;
1233         int rc;
1234
1235         if (i == 10) {
1236                 dev->USE_ISO = 0;
1237                 cx231xx_info("trans-mode=BULK. USE_ISO = %d\n", dev->USE_ISO);
1238                 return 0;
1239         }
1240
1241         if (i == 11) {
1242                 dev->USE_ISO = 1;
1243                 cx231xx_info("trans-mode=ISOC. USE_ISO = %d\n", dev->USE_ISO);
1244                 return 0;
1245         }
1246
1247         dev->mode_tv = 0;
1248         rc = check_dev(dev);
1249         if (rc < 0)
1250                 return rc;
1251
1252         if (i >= MAX_CX231XX_INPUT)
1253                 return -EINVAL;
1254         if (0 == INPUT(i)->type)
1255                 return -EINVAL;
1256
1257         mutex_lock(&dev->lock);
1258
1259         video_mux(dev, i);
1260
1261         mutex_unlock(&dev->lock);
1262         return 0;
1263 }
1264
1265 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1266 {
1267         struct cx231xx_fh *fh = priv;
1268         struct cx231xx *dev = fh->dev;
1269
1270         switch (a->index) {
1271         case CX231XX_AMUX_VIDEO:
1272                 strcpy(a->name, "Television");
1273                 break;
1274         case CX231XX_AMUX_LINE_IN:
1275                 strcpy(a->name, "Line In");
1276                 break;
1277         default:
1278                 return -EINVAL;
1279         }
1280
1281         a->index = dev->ctl_ainput;
1282         a->capability = V4L2_AUDCAP_STEREO;
1283
1284         return 0;
1285 }
1286
1287 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1288 {
1289         struct cx231xx_fh *fh = priv;
1290         struct cx231xx *dev = fh->dev;
1291         int status = 0;
1292
1293         /* Doesn't allow manual routing */
1294         if (a->index != dev->ctl_ainput)
1295                 return -EINVAL;
1296
1297         dev->ctl_ainput = INPUT(a->index)->amux;
1298         status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
1299
1300         return status;
1301 }
1302
1303 static int vidioc_queryctrl(struct file *file, void *priv,
1304                             struct v4l2_queryctrl *qc)
1305 {
1306         struct cx231xx_fh *fh = priv;
1307         struct cx231xx *dev = fh->dev;
1308         int id = qc->id;
1309         int i;
1310         int rc;
1311
1312         rc = check_dev(dev);
1313         if (rc < 0)
1314                 return rc;
1315
1316         qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
1317         if (unlikely(qc->id == 0))
1318                 return -EINVAL;
1319
1320         memset(qc, 0, sizeof(*qc));
1321
1322         qc->id = id;
1323
1324         if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
1325                 return -EINVAL;
1326
1327         for (i = 0; i < CX231XX_CTLS; i++)
1328                 if (cx231xx_ctls[i].v.id == qc->id)
1329                         break;
1330
1331         if (i == CX231XX_CTLS) {
1332                 *qc = no_ctl;
1333                 return 0;
1334         }
1335         *qc = cx231xx_ctls[i].v;
1336
1337         mutex_lock(&dev->lock);
1338         call_all(dev, core, queryctrl, qc);
1339         mutex_unlock(&dev->lock);
1340
1341         if (qc->type)
1342                 return 0;
1343         else
1344                 return -EINVAL;
1345 }
1346
1347 static int vidioc_g_ctrl(struct file *file, void *priv,
1348                          struct v4l2_control *ctrl)
1349 {
1350         struct cx231xx_fh *fh = priv;
1351         struct cx231xx *dev = fh->dev;
1352         int rc;
1353
1354         rc = check_dev(dev);
1355         if (rc < 0)
1356                 return rc;
1357
1358         mutex_lock(&dev->lock);
1359         call_all(dev, core, g_ctrl, ctrl);
1360         mutex_unlock(&dev->lock);
1361         return rc;
1362 }
1363
1364 static int vidioc_s_ctrl(struct file *file, void *priv,
1365                          struct v4l2_control *ctrl)
1366 {
1367         struct cx231xx_fh *fh = priv;
1368         struct cx231xx *dev = fh->dev;
1369         int rc;
1370
1371         rc = check_dev(dev);
1372         if (rc < 0)
1373                 return rc;
1374
1375         mutex_lock(&dev->lock);
1376         call_all(dev, core, s_ctrl, ctrl);
1377         mutex_unlock(&dev->lock);
1378         return rc;
1379 }
1380
1381 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1382 {
1383         struct cx231xx_fh *fh = priv;
1384         struct cx231xx *dev = fh->dev;
1385         int rc;
1386
1387         rc = check_dev(dev);
1388         if (rc < 0)
1389                 return rc;
1390
1391         if (0 != t->index)
1392                 return -EINVAL;
1393
1394         strcpy(t->name, "Tuner");
1395
1396         t->type = V4L2_TUNER_ANALOG_TV;
1397         t->capability = V4L2_TUNER_CAP_NORM;
1398         t->rangehigh = 0xffffffffUL;
1399         t->signal = 0xffff;     /* LOCKED */
1400
1401         return 0;
1402 }
1403
1404 static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1405 {
1406         struct cx231xx_fh *fh = priv;
1407         struct cx231xx *dev = fh->dev;
1408         int rc;
1409
1410         rc = check_dev(dev);
1411         if (rc < 0)
1412                 return rc;
1413
1414         if (0 != t->index)
1415                 return -EINVAL;
1416 #if 0
1417         mutex_lock(&dev->lock);
1418         call_all(dev, tuner, s_tuner, t);
1419         mutex_unlock(&dev->lock);
1420 #endif
1421         return 0;
1422 }
1423
1424 static int vidioc_g_frequency(struct file *file, void *priv,
1425                               struct v4l2_frequency *f)
1426 {
1427         struct cx231xx_fh *fh = priv;
1428         struct cx231xx *dev = fh->dev;
1429
1430         mutex_lock(&dev->lock);
1431         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1432         f->frequency = dev->ctl_freq;
1433
1434         call_all(dev, tuner, g_frequency, f);
1435
1436         mutex_unlock(&dev->lock);
1437
1438         return 0;
1439 }
1440
1441 static int vidioc_s_frequency(struct file *file, void *priv,
1442                               struct v4l2_frequency *f)
1443 {
1444         struct cx231xx_fh *fh = priv;
1445         struct cx231xx *dev = fh->dev;
1446         int rc;
1447         u32 if_frequency = 5400000;
1448
1449         cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1450                  f->frequency, f->type);
1451         /*cx231xx_info("f->type:  1-radio 2-analogTV 3-digitalTV\n");*/
1452
1453         rc = check_dev(dev);
1454         if (rc < 0)
1455                 return rc;
1456
1457         if (0 != f->tuner)
1458                 return -EINVAL;
1459
1460         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1461                 return -EINVAL;
1462         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1463                 return -EINVAL;
1464
1465         /* set pre channel change settings in DIF first */
1466         rc = cx231xx_tuner_pre_channel_change(dev);
1467
1468         mutex_lock(&dev->lock);
1469
1470         dev->ctl_freq = f->frequency;
1471         call_all(dev, tuner, s_frequency, f);
1472
1473         mutex_unlock(&dev->lock);
1474
1475         /* set post channel change settings in DIF first */
1476         rc = cx231xx_tuner_post_channel_change(dev);
1477
1478         if (dev->tuner_type == TUNER_NXP_TDA18271) {
1479                 if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1480                         if_frequency = 5400000;  /*5.4MHz       */
1481                 else if (dev->norm & V4L2_STD_B)
1482                         if_frequency = 6000000;  /*6.0MHz       */
1483                 else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1484                         if_frequency = 6900000;  /*6.9MHz       */
1485                 else if (dev->norm & V4L2_STD_GH)
1486                         if_frequency = 7100000;  /*7.1MHz       */
1487                 else if (dev->norm & V4L2_STD_PAL_I)
1488                         if_frequency = 7250000;  /*7.25MHz      */
1489                 else if (dev->norm & V4L2_STD_SECAM_L)
1490                         if_frequency = 6900000;  /*6.9MHz       */
1491                 else if (dev->norm & V4L2_STD_SECAM_LC)
1492                         if_frequency = 1250000;  /*1.25MHz      */
1493
1494                 cx231xx_info("if_frequency is set to %d\n", if_frequency);
1495                 cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1496
1497                 update_HH_register_after_set_DIF(dev);
1498         }
1499
1500         cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
1501
1502         return rc;
1503 }
1504
1505 #ifdef CONFIG_VIDEO_ADV_DEBUG
1506
1507 /*
1508   -R, --list-registers=type=<host/i2cdrv/i2caddr>,
1509                                 chip=<chip>[,min=<addr>,max=<addr>]
1510                      dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
1511   -r, --set-register=type=<host/i2cdrv/i2caddr>,
1512                                 chip=<chip>,reg=<addr>,val=<val>
1513                      set the register [VIDIOC_DBG_S_REGISTER]
1514
1515   if type == host, then <chip> is the hosts chip ID (default 0)
1516   if type == i2cdrv (default), then <chip> is the I2C driver name or ID
1517   if type == i2caddr, then <chip> is the 7-bit I2C address
1518 */
1519
1520 static int vidioc_g_register(struct file *file, void *priv,
1521                              struct v4l2_dbg_register *reg)
1522 {
1523         struct cx231xx_fh *fh = priv;
1524         struct cx231xx *dev = fh->dev;
1525         int ret = 0;
1526         u8 value[4] = { 0, 0, 0, 0 };
1527         u32 data = 0;
1528
1529         switch (reg->match.type) {
1530         case V4L2_CHIP_MATCH_HOST:
1531                 switch (reg->match.addr) {
1532                 case 0: /* Cx231xx - internal registers */
1533                         ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1534                                                   (u16)reg->reg, value, 4);
1535                         reg->val = value[0] | value[1] << 8 |
1536                                    value[2] << 16 | value[3] << 24;
1537                         break;
1538                 case 1: /* AFE - read byte */
1539                         ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1540                                                   (u16)reg->reg, 2, &data, 1);
1541                         reg->val = le32_to_cpu(data & 0xff);
1542                         break;
1543                 case 14: /* AFE - read dword */
1544                         ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1545                                                   (u16)reg->reg, 2, &data, 4);
1546                         reg->val = le32_to_cpu(data);
1547                         break;
1548                 case 2: /* Video Block - read byte */
1549                         ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1550                                                   (u16)reg->reg, 2, &data, 1);
1551                         reg->val = le32_to_cpu(data & 0xff);
1552                         break;
1553                 case 24: /* Video Block - read dword */
1554                         ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1555                                                   (u16)reg->reg, 2, &data, 4);
1556                         reg->val = le32_to_cpu(data);
1557                         break;
1558                 case 3: /* I2S block - read byte */
1559                         ret = cx231xx_read_i2c_data(dev,
1560                                                     I2S_BLK_DEVICE_ADDRESS,
1561                                                     (u16)reg->reg, 1,
1562                                                     &data, 1);
1563                         reg->val = le32_to_cpu(data & 0xff);
1564                         break;
1565                 case 34: /* I2S Block - read dword */
1566                         ret =
1567                             cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1568                                                   (u16)reg->reg, 1, &data, 4);
1569                         reg->val = le32_to_cpu(data);
1570                         break;
1571                 }
1572                 return ret < 0 ? ret : 0;
1573
1574         case V4L2_CHIP_MATCH_I2C_DRIVER:
1575                 call_all(dev, core, g_register, reg);
1576                 return 0;
1577         case V4L2_CHIP_MATCH_I2C_ADDR:/*for register debug*/
1578                 switch (reg->match.addr) {
1579                 case 0: /* Cx231xx - internal registers */
1580                         ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1581                                                   (u16)reg->reg, value, 4);
1582                         reg->val = value[0] | value[1] << 8 |
1583                                    value[2] << 16 | value[3] << 24;
1584
1585                         break;
1586                 case 0x600:/* AFE - read byte */
1587                         ret = cx231xx_read_i2c_master(dev, AFE_DEVICE_ADDRESS,
1588                                                  (u16)reg->reg, 2,
1589                                                  &data, 1 , 0);
1590                         reg->val = le32_to_cpu(data & 0xff);
1591                         break;
1592
1593                 case 0x880:/* Video Block - read byte */
1594                         if (reg->reg < 0x0b) {
1595                                 ret = cx231xx_read_i2c_master(dev,
1596                                                 VID_BLK_I2C_ADDRESS,
1597                                                  (u16)reg->reg, 2,
1598                                                  &data, 1 , 0);
1599                                 reg->val = le32_to_cpu(data & 0xff);
1600                         } else {
1601                                 ret = cx231xx_read_i2c_master(dev,
1602                                                 VID_BLK_I2C_ADDRESS,
1603                                                  (u16)reg->reg, 2,
1604                                                  &data, 4 , 0);
1605                                 reg->val = le32_to_cpu(data);
1606                         }
1607                         break;
1608                 case 0x980:
1609                         ret = cx231xx_read_i2c_master(dev,
1610                                                 I2S_BLK_DEVICE_ADDRESS,
1611                                                 (u16)reg->reg, 1,
1612                                                 &data, 1 , 0);
1613                         reg->val = le32_to_cpu(data & 0xff);
1614                         break;
1615                 case 0x400:
1616                         ret =
1617                             cx231xx_read_i2c_master(dev, 0x40,
1618                                                   (u16)reg->reg, 1,
1619                                                  &data, 1 , 0);
1620                         reg->val = le32_to_cpu(data & 0xff);
1621                         break;
1622                 case 0xc01:
1623                         ret =
1624                                 cx231xx_read_i2c_master(dev, 0xc0,
1625                                                 (u16)reg->reg, 2,
1626                                                  &data, 38, 1);
1627                         reg->val = le32_to_cpu(data);
1628                         break;
1629                 case 0x022:
1630                         ret =
1631                                 cx231xx_read_i2c_master(dev, 0x02,
1632                                                 (u16)reg->reg, 1,
1633                                                  &data, 1, 2);
1634                         reg->val = le32_to_cpu(data & 0xff);
1635                         break;
1636                 case 0x322:
1637                         ret = cx231xx_read_i2c_master(dev,
1638                                                 0x32,
1639                                                  (u16)reg->reg, 1,
1640                                                  &data, 4 , 2);
1641                                 reg->val = le32_to_cpu(data);
1642                         break;
1643                 case 0x342:
1644                         ret = cx231xx_read_i2c_master(dev,
1645                                                 0x34,
1646                                                  (u16)reg->reg, 1,
1647                                                  &data, 4 , 2);
1648                                 reg->val = le32_to_cpu(data);
1649                         break;
1650
1651                 default:
1652                         cx231xx_info("no match device address!!\n");
1653                         break;
1654                         }
1655                 return ret < 0 ? ret : 0;
1656                 /*return -EINVAL;*/
1657         default:
1658                 if (!v4l2_chip_match_host(&reg->match))
1659                         return -EINVAL;
1660         }
1661
1662         mutex_lock(&dev->lock);
1663         call_all(dev, core, g_register, reg);
1664         mutex_unlock(&dev->lock);
1665
1666         return ret;
1667 }
1668
1669 static int vidioc_s_register(struct file *file, void *priv,
1670                              struct v4l2_dbg_register *reg)
1671 {
1672         struct cx231xx_fh *fh = priv;
1673         struct cx231xx *dev = fh->dev;
1674         int ret = 0;
1675         __le64 buf;
1676         u32 value;
1677         u8 data[4] = { 0, 0, 0, 0 };
1678
1679         buf = cpu_to_le64(reg->val);
1680
1681         switch (reg->match.type) {
1682         case V4L2_CHIP_MATCH_HOST:
1683                 {
1684                         value = (u32) buf & 0xffffffff;
1685
1686                         switch (reg->match.addr) {
1687                         case 0: /* cx231xx internal registers */
1688                                 data[0] = (u8) value;
1689                                 data[1] = (u8) (value >> 8);
1690                                 data[2] = (u8) (value >> 16);
1691                                 data[3] = (u8) (value >> 24);
1692                                 ret = cx231xx_write_ctrl_reg(dev,
1693                                                            VRT_SET_REGISTER,
1694                                                            (u16)reg->reg, data,
1695                                                            4);
1696                                 break;
1697                         case 1: /* AFE - read byte */
1698                                 ret = cx231xx_write_i2c_data(dev,
1699                                                         AFE_DEVICE_ADDRESS,
1700                                                         (u16)reg->reg, 2,
1701                                                         value, 1);
1702                                 break;
1703                         case 14: /* AFE - read dword */
1704                                 ret = cx231xx_write_i2c_data(dev,
1705                                                         AFE_DEVICE_ADDRESS,
1706                                                         (u16)reg->reg, 2,
1707                                                         value, 4);
1708                                 break;
1709                         case 2: /* Video Block - read byte */
1710                                 ret =
1711                                     cx231xx_write_i2c_data(dev,
1712                                                         VID_BLK_I2C_ADDRESS,
1713                                                         (u16)reg->reg, 2,
1714                                                         value, 1);
1715                                 break;
1716                         case 24: /* Video Block - read dword */
1717                                 ret =
1718                                     cx231xx_write_i2c_data(dev,
1719                                                         VID_BLK_I2C_ADDRESS,
1720                                                         (u16)reg->reg, 2,
1721                                                         value, 4);
1722                                 break;
1723                         case 3: /* I2S block - read byte */
1724                                 ret =
1725                                     cx231xx_write_i2c_data(dev,
1726                                                         I2S_BLK_DEVICE_ADDRESS,
1727                                                         (u16)reg->reg, 1,
1728                                                         value, 1);
1729                                 break;
1730                         case 34: /* I2S block - read dword */
1731                                 ret =
1732                                     cx231xx_write_i2c_data(dev,
1733                                                         I2S_BLK_DEVICE_ADDRESS,
1734                                                         (u16)reg->reg, 1,
1735                                                         value, 4);
1736                                 break;
1737                         }
1738                 }
1739                 return ret < 0 ? ret : 0;
1740         case V4L2_CHIP_MATCH_I2C_ADDR:
1741                 {
1742                         value = (u32) buf & 0xffffffff;
1743
1744                         switch (reg->match.addr) {
1745                         case 0:/*cx231xx internal registers*/
1746                                         data[0] = (u8) value;
1747                                         data[1] = (u8) (value >> 8);
1748                                         data[2] = (u8) (value >> 16);
1749                                         data[3] = (u8) (value >> 24);
1750                                         ret = cx231xx_write_ctrl_reg(dev,
1751                                                            VRT_SET_REGISTER,
1752                                                            (u16)reg->reg, data,
1753                                                            4);
1754                                         break;
1755                         case 0x600:/* AFE - read byte */
1756                                         ret = cx231xx_write_i2c_master(dev,
1757                                                         AFE_DEVICE_ADDRESS,
1758                                                         (u16)reg->reg, 2,
1759                                                         value, 1 , 0);
1760                                         break;
1761
1762                         case 0x880:/* Video Block - read byte */
1763                                         if (reg->reg < 0x0b)
1764                                                 cx231xx_write_i2c_master(dev,
1765                                                         VID_BLK_I2C_ADDRESS,
1766                                                         (u16)reg->reg, 2,
1767                                                         value, 1, 0);
1768                                         else
1769                                                 cx231xx_write_i2c_master(dev,
1770                                                         VID_BLK_I2C_ADDRESS,
1771                                                         (u16)reg->reg, 2,
1772                                                         value, 4, 0);
1773                                         break;
1774                         case 0x980:
1775                                         ret =
1776                                                 cx231xx_write_i2c_master(dev,
1777                                                         I2S_BLK_DEVICE_ADDRESS,
1778                                                         (u16)reg->reg, 1,
1779                                                         value, 1, 0);
1780                                         break;
1781                         case 0x400:
1782                                         ret =
1783                                                 cx231xx_write_i2c_master(dev,
1784                                                         0x40,
1785                                                         (u16)reg->reg, 1,
1786                                                         value, 1, 0);
1787                                         break;
1788                         case 0xc01:
1789                                         ret =
1790                                                 cx231xx_write_i2c_master(dev,
1791                                                          0xc0,
1792                                                          (u16)reg->reg, 1,
1793                                                          value, 1, 1);
1794                                         break;
1795
1796                         case 0x022:
1797                                         ret =
1798                                                 cx231xx_write_i2c_master(dev,
1799                                                         0x02,
1800                                                         (u16)reg->reg, 1,
1801                                                         value, 1, 2);
1802                         case 0x322:
1803                                         ret =
1804                                                 cx231xx_write_i2c_master(dev,
1805                                                         0x32,
1806                                                         (u16)reg->reg, 1,
1807                                                         value, 4, 2);
1808                                         break;
1809
1810                         case 0x342:
1811                                         ret =
1812                                                 cx231xx_write_i2c_master(dev,
1813                                                         0x34,
1814                                                         (u16)reg->reg, 1,
1815                                                         value, 4, 2);
1816                                         break;
1817                         default:
1818                                 cx231xx_info("no match device address, "
1819                                         "the value is %x\n", reg->match.addr);
1820                                         break;
1821
1822                                         }
1823
1824                 }
1825         default:
1826                 break;
1827         }
1828
1829         mutex_lock(&dev->lock);
1830         call_all(dev, core, s_register, reg);
1831         mutex_unlock(&dev->lock);
1832
1833         return ret;
1834 }
1835 #endif
1836
1837 static int vidioc_cropcap(struct file *file, void *priv,
1838                           struct v4l2_cropcap *cc)
1839 {
1840         struct cx231xx_fh *fh = priv;
1841         struct cx231xx *dev = fh->dev;
1842
1843         if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1844                 return -EINVAL;
1845
1846         cc->bounds.left = 0;
1847         cc->bounds.top = 0;
1848         cc->bounds.width = dev->width;
1849         cc->bounds.height = dev->height;
1850         cc->defrect = cc->bounds;
1851         cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1852         cc->pixelaspect.denominator = 59;
1853
1854         return 0;
1855 }
1856
1857 static int vidioc_streamon(struct file *file, void *priv,
1858                            enum v4l2_buf_type type)
1859 {
1860         struct cx231xx_fh *fh = priv;
1861         struct cx231xx *dev = fh->dev;
1862         int rc;
1863
1864         rc = check_dev(dev);
1865         if (rc < 0)
1866                 return rc;
1867
1868         mutex_lock(&dev->lock);
1869         rc = res_get(fh);
1870
1871         if (likely(rc >= 0))
1872                 rc = videobuf_streamon(&fh->vb_vidq);
1873
1874         call_all(dev, video, s_stream, 1);
1875
1876         mutex_unlock(&dev->lock);
1877
1878         return rc;
1879 }
1880
1881 static int vidioc_streamoff(struct file *file, void *priv,
1882                             enum v4l2_buf_type type)
1883 {
1884         struct cx231xx_fh *fh = priv;
1885         struct cx231xx *dev = fh->dev;
1886         int rc;
1887
1888         rc = check_dev(dev);
1889         if (rc < 0)
1890                 return rc;
1891
1892         if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1893             (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1894                 return -EINVAL;
1895         if (type != fh->type)
1896                 return -EINVAL;
1897
1898         mutex_lock(&dev->lock);
1899
1900         cx25840_call(dev, video, s_stream, 0);
1901
1902         videobuf_streamoff(&fh->vb_vidq);
1903         res_free(fh);
1904
1905         mutex_unlock(&dev->lock);
1906
1907         return 0;
1908 }
1909
1910 static int vidioc_querycap(struct file *file, void *priv,
1911                            struct v4l2_capability *cap)
1912 {
1913         struct cx231xx_fh *fh = priv;
1914         struct cx231xx *dev = fh->dev;
1915
1916         strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1917         strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1918         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1919
1920         cap->version = CX231XX_VERSION_CODE;
1921
1922         cap->capabilities = V4L2_CAP_VBI_CAPTURE |
1923 #if 0
1924                 V4L2_CAP_SLICED_VBI_CAPTURE |
1925 #endif
1926                 V4L2_CAP_VIDEO_CAPTURE  |
1927                 V4L2_CAP_AUDIO          |
1928                 V4L2_CAP_READWRITE      |
1929                 V4L2_CAP_STREAMING;
1930
1931         if (dev->tuner_type != TUNER_ABSENT)
1932                 cap->capabilities |= V4L2_CAP_TUNER;
1933
1934         return 0;
1935 }
1936
1937 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1938                                    struct v4l2_fmtdesc *f)
1939 {
1940         if (unlikely(f->index >= ARRAY_SIZE(format)))
1941                 return -EINVAL;
1942
1943         strlcpy(f->description, format[f->index].name, sizeof(f->description));
1944         f->pixelformat = format[f->index].fourcc;
1945
1946         return 0;
1947 }
1948
1949 /* Sliced VBI ioctls */
1950 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1951                                        struct v4l2_format *f)
1952 {
1953         struct cx231xx_fh *fh = priv;
1954         struct cx231xx *dev = fh->dev;
1955         int rc;
1956
1957         rc = check_dev(dev);
1958         if (rc < 0)
1959                 return rc;
1960
1961         mutex_lock(&dev->lock);
1962
1963         f->fmt.sliced.service_set = 0;
1964
1965         call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
1966
1967         if (f->fmt.sliced.service_set == 0)
1968                 rc = -EINVAL;
1969
1970         mutex_unlock(&dev->lock);
1971         return rc;
1972 }
1973
1974 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1975                                          struct v4l2_format *f)
1976 {
1977         struct cx231xx_fh *fh = priv;
1978         struct cx231xx *dev = fh->dev;
1979         int rc;
1980
1981         rc = check_dev(dev);
1982         if (rc < 0)
1983                 return rc;
1984
1985         mutex_lock(&dev->lock);
1986         call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
1987         mutex_unlock(&dev->lock);
1988
1989         if (f->fmt.sliced.service_set == 0)
1990                 return -EINVAL;
1991
1992         return 0;
1993 }
1994
1995 /* RAW VBI ioctls */
1996
1997 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1998                                 struct v4l2_format *f)
1999 {
2000         struct cx231xx_fh *fh = priv;
2001         struct cx231xx *dev = fh->dev;
2002         f->fmt.vbi.sampling_rate = 6750000 * 4;
2003         f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
2004         f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
2005         f->fmt.vbi.offset = 0;
2006         f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
2007             PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
2008         f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
2009             PAL_VBI_LINES : NTSC_VBI_LINES;
2010         f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
2011             PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
2012         f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
2013
2014         return 0;
2015
2016 }
2017
2018 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
2019                                   struct v4l2_format *f)
2020 {
2021         struct cx231xx_fh *fh = priv;
2022         struct cx231xx *dev = fh->dev;
2023
2024         if (dev->vbi_stream_on && !fh->stream_on) {
2025                 cx231xx_errdev("%s device in use by another fh\n", __func__);
2026                 return -EBUSY;
2027         }
2028
2029         f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
2030         f->fmt.vbi.sampling_rate = 6750000 * 4;
2031         f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
2032         f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
2033         f->fmt.vbi.offset = 0;
2034         f->fmt.vbi.flags = 0;
2035         f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
2036             PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
2037         f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
2038             PAL_VBI_LINES : NTSC_VBI_LINES;
2039         f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
2040             PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
2041         f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
2042
2043         return 0;
2044
2045 }
2046
2047 static int vidioc_reqbufs(struct file *file, void *priv,
2048                           struct v4l2_requestbuffers *rb)
2049 {
2050         struct cx231xx_fh *fh = priv;
2051         struct cx231xx *dev = fh->dev;
2052         int rc;
2053
2054         rc = check_dev(dev);
2055         if (rc < 0)
2056                 return rc;
2057
2058         return videobuf_reqbufs(&fh->vb_vidq, rb);
2059 }
2060
2061 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
2062 {
2063         struct cx231xx_fh *fh = priv;
2064         struct cx231xx *dev = fh->dev;
2065         int rc;
2066
2067         rc = check_dev(dev);
2068         if (rc < 0)
2069                 return rc;
2070
2071         return videobuf_querybuf(&fh->vb_vidq, b);
2072 }
2073
2074 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2075 {
2076         struct cx231xx_fh *fh = priv;
2077         struct cx231xx *dev = fh->dev;
2078         int rc;
2079
2080         rc = check_dev(dev);
2081         if (rc < 0)
2082                 return rc;
2083
2084         return videobuf_qbuf(&fh->vb_vidq, b);
2085 }
2086
2087 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2088 {
2089         struct cx231xx_fh *fh = priv;
2090         struct cx231xx *dev = fh->dev;
2091         int rc;
2092
2093         rc = check_dev(dev);
2094         if (rc < 0)
2095                 return rc;
2096
2097         return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
2098 }
2099
2100 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2101 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2102 {
2103         struct cx231xx_fh *fh = priv;
2104
2105         return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
2106 }
2107 #endif
2108
2109 /* ----------------------------------------------------------- */
2110 /* RADIO ESPECIFIC IOCTLS                                      */
2111 /* ----------------------------------------------------------- */
2112
2113 static int radio_querycap(struct file *file, void *priv,
2114                           struct v4l2_capability *cap)
2115 {
2116         struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
2117
2118         strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
2119         strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
2120         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
2121
2122         cap->version = CX231XX_VERSION_CODE;
2123         cap->capabilities = V4L2_CAP_TUNER;
2124         return 0;
2125 }
2126
2127 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
2128 {
2129         struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
2130
2131         if (unlikely(t->index > 0))
2132                 return -EINVAL;
2133
2134         strcpy(t->name, "Radio");
2135         t->type = V4L2_TUNER_RADIO;
2136
2137         mutex_lock(&dev->lock);
2138         call_all(dev, tuner, s_tuner, t);
2139         mutex_unlock(&dev->lock);
2140
2141         return 0;
2142 }
2143
2144 static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
2145 {
2146         if (i->index != 0)
2147                 return -EINVAL;
2148         strcpy(i->name, "Radio");
2149         i->type = V4L2_INPUT_TYPE_TUNER;
2150
2151         return 0;
2152 }
2153
2154 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2155 {
2156         if (unlikely(a->index))
2157                 return -EINVAL;
2158
2159         strcpy(a->name, "Radio");
2160         return 0;
2161 }
2162
2163 static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
2164 {
2165         struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
2166
2167         if (0 != t->index)
2168                 return -EINVAL;
2169
2170         mutex_lock(&dev->lock);
2171         call_all(dev, tuner, s_tuner, t);
2172         mutex_unlock(&dev->lock);
2173
2174         return 0;
2175 }
2176
2177 static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
2178 {
2179         return 0;
2180 }
2181
2182 static int radio_s_input(struct file *file, void *fh, unsigned int i)
2183 {
2184         return 0;
2185 }
2186
2187 static int radio_queryctrl(struct file *file, void *priv,
2188                            struct v4l2_queryctrl *c)
2189 {
2190         int i;
2191
2192         if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
2193                 return -EINVAL;
2194         if (c->id == V4L2_CID_AUDIO_MUTE) {
2195                 for (i = 0; i < CX231XX_CTLS; i++) {
2196                         if (cx231xx_ctls[i].v.id == c->id)
2197                                 break;
2198                 }
2199                 if (i == CX231XX_CTLS)
2200                         return -EINVAL;
2201                 *c = cx231xx_ctls[i].v;
2202         } else
2203                 *c = no_ctl;
2204         return 0;
2205 }
2206
2207 /*
2208  * cx231xx_v4l2_open()
2209  * inits the device and starts isoc transfer
2210  */
2211 static int cx231xx_v4l2_open(struct file *filp)
2212 {
2213         int errCode = 0, radio = 0;
2214         struct video_device *vdev = video_devdata(filp);
2215         struct cx231xx *dev = video_drvdata(filp);
2216         struct cx231xx_fh *fh;
2217         enum v4l2_buf_type fh_type = 0;
2218
2219         switch (vdev->vfl_type) {
2220         case VFL_TYPE_GRABBER:
2221                 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2222                 break;
2223         case VFL_TYPE_VBI:
2224                 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2225                 break;
2226         case VFL_TYPE_RADIO:
2227                 radio = 1;
2228                 break;
2229         }
2230
2231         mutex_lock(&dev->lock);
2232
2233         cx231xx_videodbg("open dev=%s type=%s users=%d\n",
2234                          video_device_node_name(vdev), v4l2_type_names[fh_type],
2235                          dev->users);
2236
2237 #if 0
2238         errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
2239         if (errCode < 0) {
2240                 cx231xx_errdev
2241                     ("Device locked on digital mode. Can't open analog\n");
2242                 mutex_unlock(&dev->lock);
2243                 return -EBUSY;
2244         }
2245 #endif
2246
2247         fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
2248         if (!fh) {
2249                 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
2250                 mutex_unlock(&dev->lock);
2251                 return -ENOMEM;
2252         }
2253         fh->dev = dev;
2254         fh->radio = radio;
2255         fh->type = fh_type;
2256         filp->private_data = fh;
2257
2258         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2259                 dev->width = norm_maxw(dev);
2260                 dev->height = norm_maxh(dev);
2261
2262                 /* Power up in Analog TV mode */
2263                 if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER)
2264                         cx231xx_set_power_mode(dev,
2265                                  POLARIS_AVMODE_ENXTERNAL_AV);
2266                 else
2267                         cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
2268
2269 #if 0
2270                 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
2271 #endif
2272
2273                 /* set video alternate setting */
2274                 cx231xx_set_video_alternate(dev);
2275
2276                 /* Needed, since GPIO might have disabled power of
2277                    some i2c device */
2278                 cx231xx_config_i2c(dev);
2279
2280                 /* device needs to be initialized before isoc transfer */
2281                 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
2282
2283         }
2284         if (fh->radio) {
2285                 cx231xx_videodbg("video_open: setting radio device\n");
2286
2287                 /* cx231xx_start_radio(dev); */
2288
2289                 call_all(dev, tuner, s_radio);
2290         }
2291
2292         dev->users++;
2293
2294         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2295                 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
2296                                             NULL, &dev->video_mode.slock,
2297                                             fh->type, V4L2_FIELD_INTERLACED,
2298                                             sizeof(struct cx231xx_buffer),
2299                                             fh, NULL);
2300         if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2301                 /* Set the required alternate setting  VBI interface works in
2302                    Bulk mode only */
2303                 if (dev->model != CX231XX_BOARD_CNXT_VIDEO_GRABBER)
2304                         cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2305
2306                 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
2307                                             NULL, &dev->vbi_mode.slock,
2308                                             fh->type, V4L2_FIELD_SEQ_TB,
2309                                             sizeof(struct cx231xx_buffer),
2310                                             fh, NULL);
2311         }
2312
2313         mutex_unlock(&dev->lock);
2314
2315         return errCode;
2316 }
2317
2318 /*
2319  * cx231xx_realease_resources()
2320  * unregisters the v4l2,i2c and usb devices
2321  * called when the device gets disconected or at module unload
2322 */
2323 void cx231xx_release_analog_resources(struct cx231xx *dev)
2324 {
2325
2326         /*FIXME: I2C IR should be disconnected */
2327
2328         if (dev->radio_dev) {
2329                 if (video_is_registered(dev->radio_dev))
2330                         video_unregister_device(dev->radio_dev);
2331                 else
2332                         video_device_release(dev->radio_dev);
2333                 dev->radio_dev = NULL;
2334         }
2335         if (dev->vbi_dev) {
2336                 cx231xx_info("V4L2 device %s deregistered\n",
2337                              video_device_node_name(dev->vbi_dev));
2338                 if (video_is_registered(dev->vbi_dev))
2339                         video_unregister_device(dev->vbi_dev);
2340                 else
2341                         video_device_release(dev->vbi_dev);
2342                 dev->vbi_dev = NULL;
2343         }
2344         if (dev->vdev) {
2345                 cx231xx_info("V4L2 device %s deregistered\n",
2346                              video_device_node_name(dev->vdev));
2347
2348                 if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER)
2349                         cx231xx_417_unregister(dev);
2350
2351                 if (video_is_registered(dev->vdev))
2352                         video_unregister_device(dev->vdev);
2353                 else
2354                         video_device_release(dev->vdev);
2355                 dev->vdev = NULL;
2356         }
2357 }
2358
2359 /*
2360  * cx231xx_v4l2_close()
2361  * stops streaming and deallocates all resources allocated by the v4l2
2362  * calls and ioctls
2363  */
2364 static int cx231xx_v4l2_close(struct file *filp)
2365 {
2366         struct cx231xx_fh *fh = filp->private_data;
2367         struct cx231xx *dev = fh->dev;
2368
2369         cx231xx_videodbg("users=%d\n", dev->users);
2370
2371         mutex_lock(&dev->lock);
2372         cx231xx_videodbg("users=%d\n", dev->users);
2373         if (res_check(fh))
2374                 res_free(fh);
2375
2376         /*To workaround error number=-71 on EP0 for VideoGrabber,
2377                  need exclude following.*/
2378         if (dev->model != CX231XX_BOARD_CNXT_VIDEO_GRABBER)
2379                 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2380                         videobuf_stop(&fh->vb_vidq);
2381                         videobuf_mmap_free(&fh->vb_vidq);
2382
2383                         /* the device is already disconnect,
2384                            free the remaining resources */
2385                         if (dev->state & DEV_DISCONNECTED) {
2386                                 if (atomic_read(&dev->devlist_count) > 0) {
2387                                         cx231xx_release_resources(dev);
2388                                         mutex_unlock(&dev->lock);
2389                                         kfree(dev);
2390                                         dev = NULL;
2391                                         return 0;
2392                                 }
2393                                 mutex_unlock(&dev->lock);
2394                                 return 0;
2395                         }
2396
2397                         /* do this before setting alternate! */
2398                         cx231xx_uninit_vbi_isoc(dev);
2399
2400                         /* set alternate 0 */
2401                         if (!dev->vbi_or_sliced_cc_mode)
2402                                 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
2403                         else
2404                                 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
2405
2406                         kfree(fh);
2407                         dev->users--;
2408                         wake_up_interruptible_nr(&dev->open, 1);
2409                         mutex_unlock(&dev->lock);
2410                         return 0;
2411                 }
2412
2413         if (dev->users == 1) {
2414                 videobuf_stop(&fh->vb_vidq);
2415                 videobuf_mmap_free(&fh->vb_vidq);
2416
2417                 /* the device is already disconnect,
2418                    free the remaining resources */
2419                 if (dev->state & DEV_DISCONNECTED) {
2420                         cx231xx_release_resources(dev);
2421                         mutex_unlock(&dev->lock);
2422                         kfree(dev);
2423                         dev = NULL;
2424                         return 0;
2425                 }
2426
2427                 /* Save some power by putting tuner to sleep */
2428                 call_all(dev, core, s_power, 0);
2429
2430                 /* do this before setting alternate! */
2431                 if (dev->USE_ISO)
2432                         cx231xx_uninit_isoc(dev);
2433                 else
2434                         cx231xx_uninit_bulk(dev);
2435                 cx231xx_set_mode(dev, CX231XX_SUSPEND);
2436
2437                 /* set alternate 0 */
2438                 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
2439         }
2440         kfree(fh);
2441         dev->users--;
2442         wake_up_interruptible_nr(&dev->open, 1);
2443         mutex_unlock(&dev->lock);
2444         return 0;
2445 }
2446
2447 /*
2448  * cx231xx_v4l2_read()
2449  * will allocate buffers when called for the first time
2450  */
2451 static ssize_t
2452 cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2453                   loff_t *pos)
2454 {
2455         struct cx231xx_fh *fh = filp->private_data;
2456         struct cx231xx *dev = fh->dev;
2457         int rc;
2458
2459         rc = check_dev(dev);
2460         if (rc < 0)
2461                 return rc;
2462
2463         if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
2464             (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
2465                 mutex_lock(&dev->lock);
2466                 rc = res_get(fh);
2467                 mutex_unlock(&dev->lock);
2468
2469                 if (unlikely(rc < 0))
2470                         return rc;
2471
2472                 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2473                                             filp->f_flags & O_NONBLOCK);
2474         }
2475         return 0;
2476 }
2477
2478 /*
2479  * cx231xx_v4l2_poll()
2480  * will allocate buffers when called for the first time
2481  */
2482 static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
2483 {
2484         struct cx231xx_fh *fh = filp->private_data;
2485         struct cx231xx *dev = fh->dev;
2486         int rc;
2487
2488         rc = check_dev(dev);
2489         if (rc < 0)
2490                 return rc;
2491
2492         mutex_lock(&dev->lock);
2493         rc = res_get(fh);
2494         mutex_unlock(&dev->lock);
2495
2496         if (unlikely(rc < 0))
2497                 return POLLERR;
2498
2499         if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
2500             (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
2501                 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2502         else
2503                 return POLLERR;
2504 }
2505
2506 /*
2507  * cx231xx_v4l2_mmap()
2508  */
2509 static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2510 {
2511         struct cx231xx_fh *fh = filp->private_data;
2512         struct cx231xx *dev = fh->dev;
2513         int rc;
2514
2515         rc = check_dev(dev);
2516         if (rc < 0)
2517                 return rc;
2518
2519         mutex_lock(&dev->lock);
2520         rc = res_get(fh);
2521         mutex_unlock(&dev->lock);
2522
2523         if (unlikely(rc < 0))
2524                 return rc;
2525
2526         rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2527
2528         cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2529                          (unsigned long)vma->vm_start,
2530                          (unsigned long)vma->vm_end -
2531                          (unsigned long)vma->vm_start, rc);
2532
2533         return rc;
2534 }
2535
2536 static const struct v4l2_file_operations cx231xx_v4l_fops = {
2537         .owner   = THIS_MODULE,
2538         .open    = cx231xx_v4l2_open,
2539         .release = cx231xx_v4l2_close,
2540         .read    = cx231xx_v4l2_read,
2541         .poll    = cx231xx_v4l2_poll,
2542         .mmap    = cx231xx_v4l2_mmap,
2543         .ioctl   = video_ioctl2,
2544 };
2545
2546 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2547         .vidioc_querycap               = vidioc_querycap,
2548         .vidioc_enum_fmt_vid_cap       = vidioc_enum_fmt_vid_cap,
2549         .vidioc_g_fmt_vid_cap          = vidioc_g_fmt_vid_cap,
2550         .vidioc_try_fmt_vid_cap        = vidioc_try_fmt_vid_cap,
2551         .vidioc_s_fmt_vid_cap          = vidioc_s_fmt_vid_cap,
2552         .vidioc_g_fmt_vbi_cap          = vidioc_g_fmt_vbi_cap,
2553         .vidioc_try_fmt_vbi_cap        = vidioc_try_fmt_vbi_cap,
2554         .vidioc_s_fmt_vbi_cap          = vidioc_try_fmt_vbi_cap,
2555         .vidioc_g_audio                =  vidioc_g_audio,
2556         .vidioc_s_audio                = vidioc_s_audio,
2557         .vidioc_cropcap                = vidioc_cropcap,
2558         .vidioc_g_fmt_sliced_vbi_cap   = vidioc_g_fmt_sliced_vbi_cap,
2559         .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2560         .vidioc_reqbufs                = vidioc_reqbufs,
2561         .vidioc_querybuf               = vidioc_querybuf,
2562         .vidioc_qbuf                   = vidioc_qbuf,
2563         .vidioc_dqbuf                  = vidioc_dqbuf,
2564         .vidioc_s_std                  = vidioc_s_std,
2565         .vidioc_g_std                  = vidioc_g_std,
2566         .vidioc_enum_input             = vidioc_enum_input,
2567         .vidioc_g_input                = vidioc_g_input,
2568         .vidioc_s_input                = vidioc_s_input,
2569         .vidioc_queryctrl              = vidioc_queryctrl,
2570         .vidioc_g_ctrl                 = vidioc_g_ctrl,
2571         .vidioc_s_ctrl                 = vidioc_s_ctrl,
2572         .vidioc_streamon               = vidioc_streamon,
2573         .vidioc_streamoff              = vidioc_streamoff,
2574         .vidioc_g_tuner                = vidioc_g_tuner,
2575         .vidioc_s_tuner                = vidioc_s_tuner,
2576         .vidioc_g_frequency            = vidioc_g_frequency,
2577         .vidioc_s_frequency            = vidioc_s_frequency,
2578 #ifdef CONFIG_VIDEO_ADV_DEBUG
2579         .vidioc_g_register             = vidioc_g_register,
2580         .vidioc_s_register             = vidioc_s_register,
2581 #endif
2582 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2583         .vidiocgmbuf                   = vidiocgmbuf,
2584 #endif
2585 };
2586
2587 static struct video_device cx231xx_vbi_template;
2588
2589 static const struct video_device cx231xx_video_template = {
2590         .fops         = &cx231xx_v4l_fops,
2591         .release      = video_device_release,
2592         .ioctl_ops    = &video_ioctl_ops,
2593         .tvnorms      = V4L2_STD_ALL,
2594         .current_norm = V4L2_STD_PAL,
2595 };
2596
2597 static const struct v4l2_file_operations radio_fops = {
2598         .owner   = THIS_MODULE,
2599         .open   = cx231xx_v4l2_open,
2600         .release = cx231xx_v4l2_close,
2601         .ioctl   = video_ioctl2,
2602 };
2603
2604 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2605         .vidioc_querycap    = radio_querycap,
2606         .vidioc_g_tuner     = radio_g_tuner,
2607         .vidioc_enum_input  = radio_enum_input,
2608         .vidioc_g_audio     = radio_g_audio,
2609         .vidioc_s_tuner     = radio_s_tuner,
2610         .vidioc_s_audio     = radio_s_audio,
2611         .vidioc_s_input     = radio_s_input,
2612         .vidioc_queryctrl   = radio_queryctrl,
2613         .vidioc_g_ctrl      = vidioc_g_ctrl,
2614         .vidioc_s_ctrl      = vidioc_s_ctrl,
2615         .vidioc_g_frequency = vidioc_g_frequency,
2616         .vidioc_s_frequency = vidioc_s_frequency,
2617 #ifdef CONFIG_VIDEO_ADV_DEBUG
2618         .vidioc_g_register  = vidioc_g_register,
2619         .vidioc_s_register  = vidioc_s_register,
2620 #endif
2621 };
2622
2623 static struct video_device cx231xx_radio_template = {
2624         .name      = "cx231xx-radio",
2625         .fops      = &radio_fops,
2626         .ioctl_ops = &radio_ioctl_ops,
2627 };
2628
2629 /******************************** usb interface ******************************/
2630
2631 static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2632                 const struct video_device
2633                 *template, const char *type_name)
2634 {
2635         struct video_device *vfd;
2636
2637         vfd = video_device_alloc();
2638         if (NULL == vfd)
2639                 return NULL;
2640
2641         *vfd = *template;
2642         vfd->v4l2_dev = &dev->v4l2_dev;
2643         vfd->release = video_device_release;
2644         vfd->debug = video_debug;
2645
2646         snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
2647
2648         video_set_drvdata(vfd, dev);
2649         return vfd;
2650 }
2651
2652 int cx231xx_register_analog_devices(struct cx231xx *dev)
2653 {
2654         int ret;
2655
2656         cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
2657                      dev->name,
2658                      (CX231XX_VERSION_CODE >> 16) & 0xff,
2659                      (CX231XX_VERSION_CODE >> 8) & 0xff,
2660                      CX231XX_VERSION_CODE & 0xff);
2661
2662         /* set default norm */
2663         /*dev->norm = cx231xx_video_template.current_norm; */
2664         dev->width = norm_maxw(dev);
2665         dev->height = norm_maxh(dev);
2666         dev->interlaced = 0;
2667
2668         /* Analog specific initialization */
2669         dev->format = &format[0];
2670         /* video_mux(dev, dev->video_input); */
2671
2672         /* Audio defaults */
2673         dev->mute = 1;
2674         dev->volume = 0x1f;
2675
2676         /* enable vbi capturing */
2677         /* write code here...  */
2678
2679         /* allocate and fill video video_device struct */
2680         dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2681         if (!dev->vdev) {
2682                 cx231xx_errdev("cannot allocate video_device.\n");
2683                 return -ENODEV;
2684         }
2685
2686         /* register v4l2 video video_device */
2687         ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2688                                     video_nr[dev->devno]);
2689         if (ret) {
2690                 cx231xx_errdev("unable to register video device (error=%i).\n",
2691                                ret);
2692                 return ret;
2693         }
2694
2695         cx231xx_info("%s/0: registered device %s [v4l2]\n",
2696                      dev->name, video_device_node_name(dev->vdev));
2697
2698         /* Initialize VBI template */
2699         memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
2700                sizeof(cx231xx_vbi_template));
2701         strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
2702
2703         /* Allocate and fill vbi video_device struct */
2704         dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2705
2706         /* register v4l2 vbi video_device */
2707         ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2708                                     vbi_nr[dev->devno]);
2709         if (ret < 0) {
2710                 cx231xx_errdev("unable to register vbi device\n");
2711                 return ret;
2712         }
2713
2714         cx231xx_info("%s/0: registered device %s\n",
2715                      dev->name, video_device_node_name(dev->vbi_dev));
2716
2717         if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2718                 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2719                                                    "radio");
2720                 if (!dev->radio_dev) {
2721                         cx231xx_errdev("cannot allocate video_device.\n");
2722                         return -ENODEV;
2723                 }
2724                 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2725                                             radio_nr[dev->devno]);
2726                 if (ret < 0) {
2727                         cx231xx_errdev("can't register radio device\n");
2728                         return ret;
2729                 }
2730                 cx231xx_info("Registered radio device as %s\n",
2731                              video_device_node_name(dev->radio_dev));
2732         }
2733
2734         cx231xx_info("V4L2 device registered as %s and %s\n",
2735                      video_device_node_name(dev->vdev),
2736                      video_device_node_name(dev->vbi_dev));
2737
2738         return 0;
2739 }