Correct .gbs.conf settings
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / usb / 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/mm.h>
33 #include <linux/mutex.h>
34 #include <linux/slab.h>
35
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-event.h>
39 #include <media/msp3400.h>
40 #include <media/tuner.h>
41
42 #include "dvb_frontend.h"
43
44 #include "cx231xx.h"
45 #include "cx231xx-vbi.h"
46
47 #define CX231XX_VERSION "0.0.2"
48
49 #define DRIVER_AUTHOR   "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
50 #define DRIVER_DESC     "Conexant cx231xx based USB video device driver"
51
52 #define cx231xx_videodbg(fmt, arg...) do {\
53         if (video_debug) \
54                 printk(KERN_INFO "%s %s :"fmt, \
55                          dev->name, __func__ , ##arg); } while (0)
56
57 static unsigned int isoc_debug;
58 module_param(isoc_debug, int, 0644);
59 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
60
61 #define cx231xx_isocdbg(fmt, arg...) \
62 do {\
63         if (isoc_debug) { \
64                 printk(KERN_INFO "%s %s :"fmt, \
65                          dev->name, __func__ , ##arg); \
66         } \
67   } while (0)
68
69 MODULE_AUTHOR(DRIVER_AUTHOR);
70 MODULE_DESCRIPTION(DRIVER_DESC);
71 MODULE_LICENSE("GPL");
72 MODULE_VERSION(CX231XX_VERSION);
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
104 /* ------------------------------------------------------------------
105         Video buffer and parser functions
106    ------------------------------------------------------------------*/
107
108 /*
109  * Announces that a buffer were filled and request the next
110  */
111 static inline void buffer_filled(struct cx231xx *dev,
112                                  struct cx231xx_dmaqueue *dma_q,
113                                  struct cx231xx_buffer *buf)
114 {
115         /* Advice that buffer was filled */
116         cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
117         buf->vb.state = VIDEOBUF_DONE;
118         buf->vb.field_count++;
119         v4l2_get_timestamp(&buf->vb.ts);
120
121         if (dev->USE_ISO)
122                 dev->video_mode.isoc_ctl.buf = NULL;
123         else
124                 dev->video_mode.bulk_ctl.buf = NULL;
125
126         list_del(&buf->vb.queue);
127         wake_up(&buf->vb.done);
128 }
129
130 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
131 {
132         char *errmsg = "Unknown";
133
134         switch (status) {
135         case -ENOENT:
136                 errmsg = "unlinked synchronuously";
137                 break;
138         case -ECONNRESET:
139                 errmsg = "unlinked asynchronuously";
140                 break;
141         case -ENOSR:
142                 errmsg = "Buffer error (overrun)";
143                 break;
144         case -EPIPE:
145                 errmsg = "Stalled (device not responding)";
146                 break;
147         case -EOVERFLOW:
148                 errmsg = "Babble (bad cable?)";
149                 break;
150         case -EPROTO:
151                 errmsg = "Bit-stuff error (bad cable?)";
152                 break;
153         case -EILSEQ:
154                 errmsg = "CRC/Timeout (could be anything)";
155                 break;
156         case -ETIME:
157                 errmsg = "Device does not respond";
158                 break;
159         }
160         if (packet < 0) {
161                 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
162         } else {
163                 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
164                                 packet, status, errmsg);
165         }
166 }
167
168 /*
169  * video-buf generic routine to get the next available buffer
170  */
171 static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
172                                 struct cx231xx_buffer **buf)
173 {
174         struct cx231xx_video_mode *vmode =
175             container_of(dma_q, struct cx231xx_video_mode, vidq);
176         struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
177
178         char *outp;
179
180         if (list_empty(&dma_q->active)) {
181                 cx231xx_isocdbg("No active queue to serve\n");
182                 if (dev->USE_ISO)
183                         dev->video_mode.isoc_ctl.buf = NULL;
184                 else
185                         dev->video_mode.bulk_ctl.buf = NULL;
186                 *buf = NULL;
187                 return;
188         }
189
190         /* Get the next buffer */
191         *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
192
193         /* Cleans up buffer - Useful for testing for frame/URB loss */
194         outp = videobuf_to_vmalloc(&(*buf)->vb);
195         memset(outp, 0, (*buf)->vb.size);
196
197         if (dev->USE_ISO)
198                 dev->video_mode.isoc_ctl.buf = *buf;
199         else
200                 dev->video_mode.bulk_ctl.buf = *buf;
201
202         return;
203 }
204
205 /*
206  * Controls the isoc copy of each urb packet
207  */
208 static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
209 {
210         struct cx231xx_dmaqueue *dma_q = urb->context;
211         int i, rc = 1;
212         unsigned char *p_buffer;
213         u32 bytes_parsed = 0, buffer_size = 0;
214         u8 sav_eav = 0;
215
216         if (!dev)
217                 return 0;
218
219         if (dev->state & DEV_DISCONNECTED)
220                 return 0;
221
222         if (urb->status < 0) {
223                 print_err_status(dev, -1, urb->status);
224                 if (urb->status == -ENOENT)
225                         return 0;
226         }
227
228         for (i = 0; i < urb->number_of_packets; i++) {
229                 int status = urb->iso_frame_desc[i].status;
230
231                 if (status < 0) {
232                         print_err_status(dev, i, status);
233                         if (urb->iso_frame_desc[i].status != -EPROTO)
234                                 continue;
235                 }
236
237                 if (urb->iso_frame_desc[i].actual_length <= 0) {
238                         /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
239                         continue;
240                 }
241                 if (urb->iso_frame_desc[i].actual_length >
242                     dev->video_mode.max_pkt_size) {
243                         cx231xx_isocdbg("packet bigger than packet size");
244                         continue;
245                 }
246
247                 /*  get buffer pointer and length */
248                 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
249                 buffer_size = urb->iso_frame_desc[i].actual_length;
250                 bytes_parsed = 0;
251
252                 if (dma_q->is_partial_line) {
253                         /* Handle the case of a partial line */
254                         sav_eav = dma_q->last_sav;
255                 } else {
256                         /* Check for a SAV/EAV overlapping
257                                 the buffer boundary */
258                         sav_eav =
259                             cx231xx_find_boundary_SAV_EAV(p_buffer,
260                                                           dma_q->partial_buf,
261                                                           &bytes_parsed);
262                 }
263
264                 sav_eav &= 0xF0;
265                 /* Get the first line if we have some portion of an SAV/EAV from
266                    the last buffer or a partial line  */
267                 if (sav_eav) {
268                         bytes_parsed += cx231xx_get_video_line(dev, dma_q,
269                                 sav_eav,        /* SAV/EAV */
270                                 p_buffer + bytes_parsed,        /* p_buffer */
271                                 buffer_size - bytes_parsed);/* buf size */
272                 }
273
274                 /* Now parse data that is completely in this buffer */
275                 /* dma_q->is_partial_line = 0;  */
276
277                 while (bytes_parsed < buffer_size) {
278                         u32 bytes_used = 0;
279
280                         sav_eav = cx231xx_find_next_SAV_EAV(
281                                 p_buffer + bytes_parsed,        /* p_buffer */
282                                 buffer_size - bytes_parsed,     /* buf size */
283                                 &bytes_used);/* bytes used to get SAV/EAV */
284
285                         bytes_parsed += bytes_used;
286
287                         sav_eav &= 0xF0;
288                         if (sav_eav && (bytes_parsed < buffer_size)) {
289                                 bytes_parsed += cx231xx_get_video_line(dev,
290                                         dma_q, sav_eav, /* SAV/EAV */
291                                         p_buffer + bytes_parsed,/* p_buffer */
292                                         buffer_size - bytes_parsed);/*buf size*/
293                         }
294                 }
295
296                 /* Save the last four bytes of the buffer so we can check the
297                    buffer boundary condition next time */
298                 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
299                 bytes_parsed = 0;
300
301         }
302         return rc;
303 }
304
305 static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
306 {
307         struct cx231xx_dmaqueue *dma_q = urb->context;
308         int rc = 1;
309         unsigned char *p_buffer;
310         u32 bytes_parsed = 0, buffer_size = 0;
311         u8 sav_eav = 0;
312
313         if (!dev)
314                 return 0;
315
316         if (dev->state & DEV_DISCONNECTED)
317                 return 0;
318
319         if (urb->status < 0) {
320                 print_err_status(dev, -1, urb->status);
321                 if (urb->status == -ENOENT)
322                         return 0;
323         }
324
325         if (1) {
326
327                 /*  get buffer pointer and length */
328                 p_buffer = urb->transfer_buffer;
329                 buffer_size = urb->actual_length;
330                 bytes_parsed = 0;
331
332                 if (dma_q->is_partial_line) {
333                         /* Handle the case of a partial line */
334                         sav_eav = dma_q->last_sav;
335                 } else {
336                         /* Check for a SAV/EAV overlapping
337                                 the buffer boundary */
338                         sav_eav =
339                             cx231xx_find_boundary_SAV_EAV(p_buffer,
340                                                           dma_q->partial_buf,
341                                                           &bytes_parsed);
342                 }
343
344                 sav_eav &= 0xF0;
345                 /* Get the first line if we have some portion of an SAV/EAV from
346                    the last buffer or a partial line  */
347                 if (sav_eav) {
348                         bytes_parsed += cx231xx_get_video_line(dev, dma_q,
349                                 sav_eav,        /* SAV/EAV */
350                                 p_buffer + bytes_parsed,        /* p_buffer */
351                                 buffer_size - bytes_parsed);/* buf size */
352                 }
353
354                 /* Now parse data that is completely in this buffer */
355                 /* dma_q->is_partial_line = 0;  */
356
357                 while (bytes_parsed < buffer_size) {
358                         u32 bytes_used = 0;
359
360                         sav_eav = cx231xx_find_next_SAV_EAV(
361                                 p_buffer + bytes_parsed,        /* p_buffer */
362                                 buffer_size - bytes_parsed,     /* buf size */
363                                 &bytes_used);/* bytes used to get SAV/EAV */
364
365                         bytes_parsed += bytes_used;
366
367                         sav_eav &= 0xF0;
368                         if (sav_eav && (bytes_parsed < buffer_size)) {
369                                 bytes_parsed += cx231xx_get_video_line(dev,
370                                         dma_q, sav_eav, /* SAV/EAV */
371                                         p_buffer + bytes_parsed,/* p_buffer */
372                                         buffer_size - bytes_parsed);/*buf size*/
373                         }
374                 }
375
376                 /* Save the last four bytes of the buffer so we can check the
377                    buffer boundary condition next time */
378                 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
379                 bytes_parsed = 0;
380
381         }
382         return rc;
383 }
384
385
386 u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
387                                  u32 *p_bytes_used)
388 {
389         u32 bytes_used;
390         u8 boundary_bytes[8];
391         u8 sav_eav = 0;
392
393         *p_bytes_used = 0;
394
395         /* Create an array of the last 4 bytes of the last buffer and the first
396            4 bytes of the current buffer. */
397
398         memcpy(boundary_bytes, partial_buf, 4);
399         memcpy(boundary_bytes + 4, p_buffer, 4);
400
401         /* Check for the SAV/EAV in the boundary buffer */
402         sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
403                                             &bytes_used);
404
405         if (sav_eav) {
406                 /* found a boundary SAV/EAV.  Updates the bytes used to reflect
407                    only those used in the new buffer */
408                 *p_bytes_used = bytes_used - 4;
409         }
410
411         return sav_eav;
412 }
413
414 u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
415 {
416         u32 i;
417         u8 sav_eav = 0;
418
419         /*
420          * Don't search if the buffer size is less than 4.  It causes a page
421          * fault since buffer_size - 4 evaluates to a large number in that
422          * case.
423          */
424         if (buffer_size < 4) {
425                 *p_bytes_used = buffer_size;
426                 return 0;
427         }
428
429         for (i = 0; i < (buffer_size - 3); i++) {
430
431                 if ((p_buffer[i] == 0xFF) &&
432                     (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
433
434                         *p_bytes_used = i + 4;
435                         sav_eav = p_buffer[i + 3];
436                         return sav_eav;
437                 }
438         }
439
440         *p_bytes_used = buffer_size;
441         return 0;
442 }
443
444 u32 cx231xx_get_video_line(struct cx231xx *dev,
445                            struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
446                            u8 *p_buffer, u32 buffer_size)
447 {
448         u32 bytes_copied = 0;
449         int current_field = -1;
450
451         switch (sav_eav) {
452         case SAV_ACTIVE_VIDEO_FIELD1:
453                 /* looking for skipped line which occurred in PAL 720x480 mode.
454                    In this case, there will be no active data contained
455                    between the SAV and EAV */
456                 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
457                     (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
458                     ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
459                      (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
460                      (p_buffer[3] == EAV_VBLANK_FIELD1) ||
461                      (p_buffer[3] == EAV_VBLANK_FIELD2)))
462                         return bytes_copied;
463                 current_field = 1;
464                 break;
465
466         case SAV_ACTIVE_VIDEO_FIELD2:
467                 /* looking for skipped line which occurred in PAL 720x480 mode.
468                    In this case, there will be no active data contained between
469                    the SAV and EAV */
470                 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
471                     (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
472                     ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
473                      (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
474                      (p_buffer[3] == EAV_VBLANK_FIELD1)       ||
475                      (p_buffer[3] == EAV_VBLANK_FIELD2)))
476                         return bytes_copied;
477                 current_field = 2;
478                 break;
479         }
480
481         dma_q->last_sav = sav_eav;
482
483         bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
484                                                buffer_size, current_field);
485
486         return bytes_copied;
487 }
488
489 u32 cx231xx_copy_video_line(struct cx231xx *dev,
490                             struct cx231xx_dmaqueue *dma_q, u8 *p_line,
491                             u32 length, int field_number)
492 {
493         u32 bytes_to_copy;
494         struct cx231xx_buffer *buf;
495         u32 _line_size = dev->width * 2;
496
497         if (dma_q->current_field != field_number)
498                 cx231xx_reset_video_buffer(dev, dma_q);
499
500         /* get the buffer pointer */
501         if (dev->USE_ISO)
502                 buf = dev->video_mode.isoc_ctl.buf;
503         else
504                 buf = dev->video_mode.bulk_ctl.buf;
505
506         /* Remember the field number for next time */
507         dma_q->current_field = field_number;
508
509         bytes_to_copy = dma_q->bytes_left_in_line;
510         if (bytes_to_copy > length)
511                 bytes_to_copy = length;
512
513         if (dma_q->lines_completed >= dma_q->lines_per_field) {
514                 dma_q->bytes_left_in_line -= bytes_to_copy;
515                 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
516                                           0 : 1;
517                 return 0;
518         }
519
520         dma_q->is_partial_line = 1;
521
522         /* If we don't have a buffer, just return the number of bytes we would
523            have copied if we had a buffer. */
524         if (!buf) {
525                 dma_q->bytes_left_in_line -= bytes_to_copy;
526                 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
527                                          ? 0 : 1;
528                 return bytes_to_copy;
529         }
530
531         /* copy the data to video buffer */
532         cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
533
534         dma_q->pos += bytes_to_copy;
535         dma_q->bytes_left_in_line -= bytes_to_copy;
536
537         if (dma_q->bytes_left_in_line == 0) {
538                 dma_q->bytes_left_in_line = _line_size;
539                 dma_q->lines_completed++;
540                 dma_q->is_partial_line = 0;
541
542                 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
543                         buffer_filled(dev, dma_q, buf);
544
545                         dma_q->pos = 0;
546                         buf = NULL;
547                         dma_q->lines_completed = 0;
548                 }
549         }
550
551         return bytes_to_copy;
552 }
553
554 void cx231xx_reset_video_buffer(struct cx231xx *dev,
555                                 struct cx231xx_dmaqueue *dma_q)
556 {
557         struct cx231xx_buffer *buf;
558
559         /* handle the switch from field 1 to field 2 */
560         if (dma_q->current_field == 1) {
561                 if (dma_q->lines_completed >= dma_q->lines_per_field)
562                         dma_q->field1_done = 1;
563                 else
564                         dma_q->field1_done = 0;
565         }
566
567         if (dev->USE_ISO)
568                 buf = dev->video_mode.isoc_ctl.buf;
569         else
570                 buf = dev->video_mode.bulk_ctl.buf;
571
572         if (buf == NULL) {
573                 /* first try to get the buffer */
574                 get_next_buf(dma_q, &buf);
575
576                 dma_q->pos = 0;
577                 dma_q->field1_done = 0;
578                 dma_q->current_field = -1;
579         }
580
581         /* reset the counters */
582         dma_q->bytes_left_in_line = dev->width << 1;
583         dma_q->lines_completed = 0;
584 }
585
586 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
587                     u8 *p_buffer, u32 bytes_to_copy)
588 {
589         u8 *p_out_buffer = NULL;
590         u32 current_line_bytes_copied = 0;
591         struct cx231xx_buffer *buf;
592         u32 _line_size = dev->width << 1;
593         void *startwrite;
594         int offset, lencopy;
595
596         if (dev->USE_ISO)
597                 buf = dev->video_mode.isoc_ctl.buf;
598         else
599                 buf = dev->video_mode.bulk_ctl.buf;
600
601         if (buf == NULL)
602                 return -1;
603
604         p_out_buffer = videobuf_to_vmalloc(&buf->vb);
605
606         current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
607
608         /* Offset field 2 one line from the top of the buffer */
609         offset = (dma_q->current_field == 1) ? 0 : _line_size;
610
611         /* Offset for field 2 */
612         startwrite = p_out_buffer + offset;
613
614         /* lines already completed in the current field */
615         startwrite += (dma_q->lines_completed * _line_size * 2);
616
617         /* bytes already completed in the current line */
618         startwrite += current_line_bytes_copied;
619
620         lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
621                   bytes_to_copy : dma_q->bytes_left_in_line;
622
623         if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
624                 return 0;
625
626         /* The below copies the UYVY data straight into video buffer */
627         cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
628
629         return 0;
630 }
631
632 void cx231xx_swab(u16 *from, u16 *to, u16 len)
633 {
634         u16 i;
635
636         if (len <= 0)
637                 return;
638
639         for (i = 0; i < len / 2; i++)
640                 to[i] = (from[i] << 8) | (from[i] >> 8);
641 }
642
643 u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
644 {
645         u8 buffer_complete = 0;
646
647         /* Dual field stream */
648         buffer_complete = ((dma_q->current_field == 2) &&
649                            (dma_q->lines_completed >= dma_q->lines_per_field) &&
650                             dma_q->field1_done);
651
652         return buffer_complete;
653 }
654
655 /* ------------------------------------------------------------------
656         Videobuf operations
657    ------------------------------------------------------------------*/
658
659 static int
660 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
661 {
662         struct cx231xx_fh *fh = vq->priv_data;
663         struct cx231xx *dev = fh->dev;
664
665         *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
666         if (0 == *count)
667                 *count = CX231XX_DEF_BUF;
668
669         if (*count < CX231XX_MIN_BUF)
670                 *count = CX231XX_MIN_BUF;
671
672         return 0;
673 }
674
675 /* This is called *without* dev->slock held; please keep it that way */
676 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
677 {
678         struct cx231xx_fh *fh = vq->priv_data;
679         struct cx231xx *dev = fh->dev;
680         unsigned long flags = 0;
681
682         if (in_interrupt())
683                 BUG();
684
685         /* We used to wait for the buffer to finish here, but this didn't work
686            because, as we were keeping the state as VIDEOBUF_QUEUED,
687            videobuf_queue_cancel marked it as finished for us.
688            (Also, it could wedge forever if the hardware was misconfigured.)
689
690            This should be safe; by the time we get here, the buffer isn't
691            queued anymore. If we ever start marking the buffers as
692            VIDEOBUF_ACTIVE, it won't be, though.
693          */
694         spin_lock_irqsave(&dev->video_mode.slock, flags);
695         if (dev->USE_ISO) {
696                 if (dev->video_mode.isoc_ctl.buf == buf)
697                         dev->video_mode.isoc_ctl.buf = NULL;
698         } else {
699                 if (dev->video_mode.bulk_ctl.buf == buf)
700                         dev->video_mode.bulk_ctl.buf = NULL;
701         }
702         spin_unlock_irqrestore(&dev->video_mode.slock, flags);
703
704         videobuf_vmalloc_free(&buf->vb);
705         buf->vb.state = VIDEOBUF_NEEDS_INIT;
706 }
707
708 static int
709 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
710                enum v4l2_field field)
711 {
712         struct cx231xx_fh *fh = vq->priv_data;
713         struct cx231xx_buffer *buf =
714             container_of(vb, struct cx231xx_buffer, vb);
715         struct cx231xx *dev = fh->dev;
716         int rc = 0, urb_init = 0;
717
718         /* The only currently supported format is 16 bits/pixel */
719         buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
720                         + 7) >> 3;
721         if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
722                 return -EINVAL;
723
724         buf->vb.width = dev->width;
725         buf->vb.height = dev->height;
726         buf->vb.field = field;
727
728         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
729                 rc = videobuf_iolock(vq, &buf->vb, NULL);
730                 if (rc < 0)
731                         goto fail;
732         }
733
734         if (dev->USE_ISO) {
735                 if (!dev->video_mode.isoc_ctl.num_bufs)
736                         urb_init = 1;
737         } else {
738                 if (!dev->video_mode.bulk_ctl.num_bufs)
739                         urb_init = 1;
740         }
741         /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
742                 urb_init, dev->video_mode.max_pkt_size);*/
743         if (urb_init) {
744                 dev->mode_tv = 0;
745                 if (dev->USE_ISO)
746                         rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
747                                        CX231XX_NUM_BUFS,
748                                        dev->video_mode.max_pkt_size,
749                                        cx231xx_isoc_copy);
750                 else
751                         rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
752                                        CX231XX_NUM_BUFS,
753                                        dev->video_mode.max_pkt_size,
754                                        cx231xx_bulk_copy);
755                 if (rc < 0)
756                         goto fail;
757         }
758
759         buf->vb.state = VIDEOBUF_PREPARED;
760         return 0;
761
762 fail:
763         free_buffer(vq, buf);
764         return rc;
765 }
766
767 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
768 {
769         struct cx231xx_buffer *buf =
770             container_of(vb, struct cx231xx_buffer, vb);
771         struct cx231xx_fh *fh = vq->priv_data;
772         struct cx231xx *dev = fh->dev;
773         struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
774
775         buf->vb.state = VIDEOBUF_QUEUED;
776         list_add_tail(&buf->vb.queue, &vidq->active);
777
778 }
779
780 static void buffer_release(struct videobuf_queue *vq,
781                            struct videobuf_buffer *vb)
782 {
783         struct cx231xx_buffer *buf =
784             container_of(vb, struct cx231xx_buffer, vb);
785         struct cx231xx_fh *fh = vq->priv_data;
786         struct cx231xx *dev = (struct cx231xx *)fh->dev;
787
788         cx231xx_isocdbg("cx231xx: called buffer_release\n");
789
790         free_buffer(vq, buf);
791 }
792
793 static struct videobuf_queue_ops cx231xx_video_qops = {
794         .buf_setup = buffer_setup,
795         .buf_prepare = buffer_prepare,
796         .buf_queue = buffer_queue,
797         .buf_release = buffer_release,
798 };
799
800 /*********************  v4l2 interface  **************************************/
801
802 void video_mux(struct cx231xx *dev, int index)
803 {
804         dev->video_input = index;
805         dev->ctl_ainput = INPUT(index)->amux;
806
807         cx231xx_set_video_input_mux(dev, index);
808
809         cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
810
811         cx231xx_set_audio_input(dev, dev->ctl_ainput);
812
813         cx231xx_info("video_mux : %d\n", index);
814
815         /* do mode control overrides if required */
816         cx231xx_do_mode_ctrl_overrides(dev);
817 }
818
819 /* Usage lock check functions */
820 static int res_get(struct cx231xx_fh *fh)
821 {
822         struct cx231xx *dev = fh->dev;
823         int rc = 0;
824
825         /* This instance already has stream_on */
826         if (fh->stream_on)
827                 return rc;
828
829         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
830                 if (dev->stream_on)
831                         return -EBUSY;
832                 dev->stream_on = 1;
833         } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
834                 if (dev->vbi_stream_on)
835                         return -EBUSY;
836                 dev->vbi_stream_on = 1;
837         } else
838                 return -EINVAL;
839
840         fh->stream_on = 1;
841
842         return rc;
843 }
844
845 static int res_check(struct cx231xx_fh *fh)
846 {
847         return fh->stream_on;
848 }
849
850 static void res_free(struct cx231xx_fh *fh)
851 {
852         struct cx231xx *dev = fh->dev;
853
854         fh->stream_on = 0;
855
856         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
857                 dev->stream_on = 0;
858         if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
859                 dev->vbi_stream_on = 0;
860 }
861
862 static int check_dev(struct cx231xx *dev)
863 {
864         if (dev->state & DEV_DISCONNECTED) {
865                 cx231xx_errdev("v4l2 ioctl: device not present\n");
866                 return -ENODEV;
867         }
868         return 0;
869 }
870
871 /* ------------------------------------------------------------------
872         IOCTL vidioc handling
873    ------------------------------------------------------------------*/
874
875 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
876                                 struct v4l2_format *f)
877 {
878         struct cx231xx_fh *fh = priv;
879         struct cx231xx *dev = fh->dev;
880
881         f->fmt.pix.width = dev->width;
882         f->fmt.pix.height = dev->height;
883         f->fmt.pix.pixelformat = dev->format->fourcc;
884         f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
885         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
886         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
887
888         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
889
890         return 0;
891 }
892
893 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
894 {
895         unsigned int i;
896
897         for (i = 0; i < ARRAY_SIZE(format); i++)
898                 if (format[i].fourcc == fourcc)
899                         return &format[i];
900
901         return NULL;
902 }
903
904 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
905                                   struct v4l2_format *f)
906 {
907         struct cx231xx_fh *fh = priv;
908         struct cx231xx *dev = fh->dev;
909         unsigned int width = f->fmt.pix.width;
910         unsigned int height = f->fmt.pix.height;
911         unsigned int maxw = norm_maxw(dev);
912         unsigned int maxh = norm_maxh(dev);
913         struct cx231xx_fmt *fmt;
914
915         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
916         if (!fmt) {
917                 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
918                                  f->fmt.pix.pixelformat);
919                 return -EINVAL;
920         }
921
922         /* width must even because of the YUYV format
923            height must be even because of interlacing */
924         v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
925
926         f->fmt.pix.width = width;
927         f->fmt.pix.height = height;
928         f->fmt.pix.pixelformat = fmt->fourcc;
929         f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
930         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
931         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
932         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
933
934         return 0;
935 }
936
937 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
938                                 struct v4l2_format *f)
939 {
940         struct cx231xx_fh *fh = priv;
941         struct cx231xx *dev = fh->dev;
942         int rc;
943         struct cx231xx_fmt *fmt;
944         struct v4l2_mbus_framefmt mbus_fmt;
945
946         rc = check_dev(dev);
947         if (rc < 0)
948                 return rc;
949
950         vidioc_try_fmt_vid_cap(file, priv, f);
951
952         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
953         if (!fmt)
954                 return -EINVAL;
955
956         if (videobuf_queue_is_busy(&fh->vb_vidq)) {
957                 cx231xx_errdev("%s queue busy\n", __func__);
958                 return -EBUSY;
959         }
960
961         if (dev->stream_on && !fh->stream_on) {
962                 cx231xx_errdev("%s device in use by another fh\n", __func__);
963                 return -EBUSY;
964         }
965
966         /* set new image size */
967         dev->width = f->fmt.pix.width;
968         dev->height = f->fmt.pix.height;
969         dev->format = fmt;
970
971         v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
972         call_all(dev, video, s_mbus_fmt, &mbus_fmt);
973         v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
974
975         return rc;
976 }
977
978 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
979 {
980         struct cx231xx_fh *fh = priv;
981         struct cx231xx *dev = fh->dev;
982
983         *id = dev->norm;
984         return 0;
985 }
986
987 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
988 {
989         struct cx231xx_fh *fh = priv;
990         struct cx231xx *dev = fh->dev;
991         struct v4l2_mbus_framefmt mbus_fmt;
992         int rc;
993
994         rc = check_dev(dev);
995         if (rc < 0)
996                 return rc;
997
998         if (dev->norm == norm)
999                 return 0;
1000
1001         if (videobuf_queue_is_busy(&fh->vb_vidq))
1002                 return -EBUSY;
1003
1004         dev->norm = norm;
1005
1006         /* Adjusts width/height, if needed */
1007         dev->width = 720;
1008         dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
1009
1010         call_all(dev, core, s_std, dev->norm);
1011
1012         /* We need to reset basic properties in the decoder related to
1013            resolution (since a standard change effects things like the number
1014            of lines in VACT, etc) */
1015         memset(&mbus_fmt, 0, sizeof(mbus_fmt));
1016         mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
1017         mbus_fmt.width = dev->width;
1018         mbus_fmt.height = dev->height;
1019         call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1020
1021         /* do mode control overrides */
1022         cx231xx_do_mode_ctrl_overrides(dev);
1023
1024         return 0;
1025 }
1026
1027 static const char *iname[] = {
1028         [CX231XX_VMUX_COMPOSITE1] = "Composite1",
1029         [CX231XX_VMUX_SVIDEO]     = "S-Video",
1030         [CX231XX_VMUX_TELEVISION] = "Television",
1031         [CX231XX_VMUX_CABLE]      = "Cable TV",
1032         [CX231XX_VMUX_DVB]        = "DVB",
1033         [CX231XX_VMUX_DEBUG]      = "for debug only",
1034 };
1035
1036 int cx231xx_enum_input(struct file *file, void *priv,
1037                              struct v4l2_input *i)
1038 {
1039         struct cx231xx_fh *fh = priv;
1040         struct cx231xx *dev = fh->dev;
1041         u32 gen_stat;
1042         unsigned int ret, n;
1043
1044         n = i->index;
1045         if (n >= MAX_CX231XX_INPUT)
1046                 return -EINVAL;
1047         if (0 == INPUT(n)->type)
1048                 return -EINVAL;
1049
1050         i->index = n;
1051         i->type = V4L2_INPUT_TYPE_CAMERA;
1052
1053         strcpy(i->name, iname[INPUT(n)->type]);
1054
1055         if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1056             (CX231XX_VMUX_CABLE == INPUT(n)->type))
1057                 i->type = V4L2_INPUT_TYPE_TUNER;
1058
1059         i->std = dev->vdev->tvnorms;
1060
1061         /* If they are asking about the active input, read signal status */
1062         if (n == dev->video_input) {
1063                 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1064                                             GEN_STAT, 2, &gen_stat, 4);
1065                 if (ret > 0) {
1066                         if ((gen_stat & FLD_VPRES) == 0x00)
1067                                 i->status |= V4L2_IN_ST_NO_SIGNAL;
1068                         if ((gen_stat & FLD_HLOCK) == 0x00)
1069                                 i->status |= V4L2_IN_ST_NO_H_LOCK;
1070                 }
1071         }
1072
1073         return 0;
1074 }
1075
1076 int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
1077 {
1078         struct cx231xx_fh *fh = priv;
1079         struct cx231xx *dev = fh->dev;
1080
1081         *i = dev->video_input;
1082
1083         return 0;
1084 }
1085
1086 int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
1087 {
1088         struct cx231xx_fh *fh = priv;
1089         struct cx231xx *dev = fh->dev;
1090         int rc;
1091
1092         dev->mode_tv = 0;
1093         rc = check_dev(dev);
1094         if (rc < 0)
1095                 return rc;
1096
1097         if (i >= MAX_CX231XX_INPUT)
1098                 return -EINVAL;
1099         if (0 == INPUT(i)->type)
1100                 return -EINVAL;
1101
1102         video_mux(dev, i);
1103
1104         if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1105             INPUT(i)->type == CX231XX_VMUX_CABLE) {
1106                 /* There's a tuner, so reset the standard and put it on the
1107                    last known frequency (since it was probably powered down
1108                    until now */
1109                 call_all(dev, core, s_std, dev->norm);
1110         }
1111
1112         return 0;
1113 }
1114
1115 int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1116 {
1117         struct cx231xx_fh *fh = priv;
1118         struct cx231xx *dev = fh->dev;
1119         int rc;
1120
1121         rc = check_dev(dev);
1122         if (rc < 0)
1123                 return rc;
1124
1125         if (0 != t->index)
1126                 return -EINVAL;
1127
1128         strcpy(t->name, "Tuner");
1129
1130         t->type = V4L2_TUNER_ANALOG_TV;
1131         t->capability = V4L2_TUNER_CAP_NORM;
1132         t->rangehigh = 0xffffffffUL;
1133         t->signal = 0xffff;     /* LOCKED */
1134         call_all(dev, tuner, g_tuner, t);
1135
1136         return 0;
1137 }
1138
1139 int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1140 {
1141         struct cx231xx_fh *fh = priv;
1142         struct cx231xx *dev = fh->dev;
1143         int rc;
1144
1145         rc = check_dev(dev);
1146         if (rc < 0)
1147                 return rc;
1148
1149         if (0 != t->index)
1150                 return -EINVAL;
1151 #if 0
1152         call_all(dev, tuner, s_tuner, t);
1153 #endif
1154         return 0;
1155 }
1156
1157 int cx231xx_g_frequency(struct file *file, void *priv,
1158                               struct v4l2_frequency *f)
1159 {
1160         struct cx231xx_fh *fh = priv;
1161         struct cx231xx *dev = fh->dev;
1162
1163         if (f->tuner)
1164                 return -EINVAL;
1165
1166         f->frequency = dev->ctl_freq;
1167
1168         return 0;
1169 }
1170
1171 int cx231xx_s_frequency(struct file *file, void *priv,
1172                               const struct v4l2_frequency *f)
1173 {
1174         struct cx231xx_fh *fh = priv;
1175         struct cx231xx *dev = fh->dev;
1176         struct v4l2_frequency new_freq = *f;
1177         int rc;
1178         u32 if_frequency = 5400000;
1179
1180         cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1181                  f->frequency, f->type);
1182         /*cx231xx_info("f->type:  1-radio 2-analogTV 3-digitalTV\n");*/
1183
1184         rc = check_dev(dev);
1185         if (rc < 0)
1186                 return rc;
1187
1188         if (0 != f->tuner)
1189                 return -EINVAL;
1190
1191         /* set pre channel change settings in DIF first */
1192         rc = cx231xx_tuner_pre_channel_change(dev);
1193
1194         call_all(dev, tuner, s_frequency, f);
1195         call_all(dev, tuner, g_frequency, &new_freq);
1196         dev->ctl_freq = new_freq.frequency;
1197
1198         /* set post channel change settings in DIF first */
1199         rc = cx231xx_tuner_post_channel_change(dev);
1200
1201         if (dev->tuner_type == TUNER_NXP_TDA18271) {
1202                 if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1203                         if_frequency = 5400000;  /*5.4MHz       */
1204                 else if (dev->norm & V4L2_STD_B)
1205                         if_frequency = 6000000;  /*6.0MHz       */
1206                 else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1207                         if_frequency = 6900000;  /*6.9MHz       */
1208                 else if (dev->norm & V4L2_STD_GH)
1209                         if_frequency = 7100000;  /*7.1MHz       */
1210                 else if (dev->norm & V4L2_STD_PAL_I)
1211                         if_frequency = 7250000;  /*7.25MHz      */
1212                 else if (dev->norm & V4L2_STD_SECAM_L)
1213                         if_frequency = 6900000;  /*6.9MHz       */
1214                 else if (dev->norm & V4L2_STD_SECAM_LC)
1215                         if_frequency = 1250000;  /*1.25MHz      */
1216
1217                 cx231xx_info("if_frequency is set to %d\n", if_frequency);
1218                 cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1219
1220                 update_HH_register_after_set_DIF(dev);
1221         }
1222
1223         cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
1224
1225         return rc;
1226 }
1227
1228 #ifdef CONFIG_VIDEO_ADV_DEBUG
1229
1230 int cx231xx_g_chip_info(struct file *file, void *fh,
1231                         struct v4l2_dbg_chip_info *chip)
1232 {
1233         switch (chip->match.addr) {
1234         case 0: /* Cx231xx - internal registers */
1235                 return 0;
1236         case 1: /* AFE - read byte */
1237                 strlcpy(chip->name, "AFE (byte)", sizeof(chip->name));
1238                 return 0;
1239         case 2: /* Video Block - read byte */
1240                 strlcpy(chip->name, "Video (byte)", sizeof(chip->name));
1241                 return 0;
1242         case 3: /* I2S block - read byte */
1243                 strlcpy(chip->name, "I2S (byte)", sizeof(chip->name));
1244                 return 0;
1245         case 4: /* AFE - read dword */
1246                 strlcpy(chip->name, "AFE (dword)", sizeof(chip->name));
1247                 return 0;
1248         case 5: /* Video Block - read dword */
1249                 strlcpy(chip->name, "Video (dword)", sizeof(chip->name));
1250                 return 0;
1251         case 6: /* I2S Block - read dword */
1252                 strlcpy(chip->name, "I2S (dword)", sizeof(chip->name));
1253                 return 0;
1254         }
1255         return -EINVAL;
1256 }
1257
1258 int cx231xx_g_register(struct file *file, void *priv,
1259                              struct v4l2_dbg_register *reg)
1260 {
1261         struct cx231xx_fh *fh = priv;
1262         struct cx231xx *dev = fh->dev;
1263         int ret;
1264         u8 value[4] = { 0, 0, 0, 0 };
1265         u32 data = 0;
1266
1267         switch (reg->match.addr) {
1268         case 0: /* Cx231xx - internal registers */
1269                 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1270                                 (u16)reg->reg, value, 4);
1271                 reg->val = value[0] | value[1] << 8 |
1272                         value[2] << 16 | value[3] << 24;
1273                 reg->size = 4;
1274                 break;
1275         case 1: /* AFE - read byte */
1276                 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1277                                 (u16)reg->reg, 2, &data, 1);
1278                 reg->val = data;
1279                 reg->size = 1;
1280                 break;
1281         case 2: /* Video Block - read byte */
1282                 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1283                                 (u16)reg->reg, 2, &data, 1);
1284                 reg->val = data;
1285                 reg->size = 1;
1286                 break;
1287         case 3: /* I2S block - read byte */
1288                 ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1289                                 (u16)reg->reg, 1, &data, 1);
1290                 reg->val = data;
1291                 reg->size = 1;
1292                 break;
1293         case 4: /* AFE - read dword */
1294                 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1295                                 (u16)reg->reg, 2, &data, 4);
1296                 reg->val = data;
1297                 reg->size = 4;
1298                 break;
1299         case 5: /* Video Block - read dword */
1300                 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1301                                 (u16)reg->reg, 2, &data, 4);
1302                 reg->val = data;
1303                 reg->size = 4;
1304                 break;
1305         case 6: /* I2S Block - read dword */
1306                 ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1307                                 (u16)reg->reg, 1, &data, 4);
1308                 reg->val = data;
1309                 reg->size = 4;
1310                 break;
1311         default:
1312                 return -EINVAL;
1313         }
1314         return ret < 0 ? ret : 0;
1315 }
1316
1317 int cx231xx_s_register(struct file *file, void *priv,
1318                              const struct v4l2_dbg_register *reg)
1319 {
1320         struct cx231xx_fh *fh = priv;
1321         struct cx231xx *dev = fh->dev;
1322         int ret;
1323         u8 data[4] = { 0, 0, 0, 0 };
1324
1325         switch (reg->match.addr) {
1326         case 0: /* cx231xx internal registers */
1327                 data[0] = (u8) reg->val;
1328                 data[1] = (u8) (reg->val >> 8);
1329                 data[2] = (u8) (reg->val >> 16);
1330                 data[3] = (u8) (reg->val >> 24);
1331                 ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1332                                 (u16)reg->reg, data, 4);
1333                 break;
1334         case 1: /* AFE - write byte */
1335                 ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1336                                 (u16)reg->reg, 2, reg->val, 1);
1337                 break;
1338         case 2: /* Video Block - write byte */
1339                 ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1340                                 (u16)reg->reg, 2, reg->val, 1);
1341                 break;
1342         case 3: /* I2S block - write byte */
1343                 ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1344                                 (u16)reg->reg, 1, reg->val, 1);
1345                 break;
1346         case 4: /* AFE - write dword */
1347                 ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1348                                 (u16)reg->reg, 2, reg->val, 4);
1349                 break;
1350         case 5: /* Video Block - write dword */
1351                 ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1352                                 (u16)reg->reg, 2, reg->val, 4);
1353                 break;
1354         case 6: /* I2S block - write dword */
1355                 ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1356                                 (u16)reg->reg, 1, reg->val, 4);
1357                 break;
1358         default:
1359                 return -EINVAL;
1360         }
1361         return ret < 0 ? ret : 0;
1362 }
1363 #endif
1364
1365 static int vidioc_cropcap(struct file *file, void *priv,
1366                           struct v4l2_cropcap *cc)
1367 {
1368         struct cx231xx_fh *fh = priv;
1369         struct cx231xx *dev = fh->dev;
1370
1371         if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1372                 return -EINVAL;
1373
1374         cc->bounds.left = 0;
1375         cc->bounds.top = 0;
1376         cc->bounds.width = dev->width;
1377         cc->bounds.height = dev->height;
1378         cc->defrect = cc->bounds;
1379         cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1380         cc->pixelaspect.denominator = 59;
1381
1382         return 0;
1383 }
1384
1385 static int vidioc_streamon(struct file *file, void *priv,
1386                            enum v4l2_buf_type type)
1387 {
1388         struct cx231xx_fh *fh = priv;
1389         struct cx231xx *dev = fh->dev;
1390         int rc;
1391
1392         rc = check_dev(dev);
1393         if (rc < 0)
1394                 return rc;
1395
1396         rc = res_get(fh);
1397
1398         if (likely(rc >= 0))
1399                 rc = videobuf_streamon(&fh->vb_vidq);
1400
1401         call_all(dev, video, s_stream, 1);
1402
1403         return rc;
1404 }
1405
1406 static int vidioc_streamoff(struct file *file, void *priv,
1407                             enum v4l2_buf_type type)
1408 {
1409         struct cx231xx_fh *fh = priv;
1410         struct cx231xx *dev = fh->dev;
1411         int rc;
1412
1413         rc = check_dev(dev);
1414         if (rc < 0)
1415                 return rc;
1416
1417         if (type != fh->type)
1418                 return -EINVAL;
1419
1420         cx25840_call(dev, video, s_stream, 0);
1421
1422         videobuf_streamoff(&fh->vb_vidq);
1423         res_free(fh);
1424
1425         return 0;
1426 }
1427
1428 int cx231xx_querycap(struct file *file, void *priv,
1429                            struct v4l2_capability *cap)
1430 {
1431         struct video_device *vdev = video_devdata(file);
1432         struct cx231xx_fh *fh = priv;
1433         struct cx231xx *dev = fh->dev;
1434
1435         strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1436         strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1437         usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1438
1439         if (vdev->vfl_type == VFL_TYPE_RADIO)
1440                 cap->device_caps = V4L2_CAP_RADIO;
1441         else {
1442                 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1443                 if (vdev->vfl_type == VFL_TYPE_VBI)
1444                         cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1445                 else
1446                         cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1447         }
1448         if (dev->tuner_type != TUNER_ABSENT)
1449                 cap->device_caps |= V4L2_CAP_TUNER;
1450         cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
1451                 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1452                 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
1453         if (dev->radio_dev)
1454                 cap->capabilities |= V4L2_CAP_RADIO;
1455
1456         return 0;
1457 }
1458
1459 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1460                                    struct v4l2_fmtdesc *f)
1461 {
1462         if (unlikely(f->index >= ARRAY_SIZE(format)))
1463                 return -EINVAL;
1464
1465         strlcpy(f->description, format[f->index].name, sizeof(f->description));
1466         f->pixelformat = format[f->index].fourcc;
1467
1468         return 0;
1469 }
1470
1471 /* RAW VBI ioctls */
1472
1473 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1474                                 struct v4l2_format *f)
1475 {
1476         struct cx231xx_fh *fh = priv;
1477         struct cx231xx *dev = fh->dev;
1478
1479         f->fmt.vbi.sampling_rate = 6750000 * 4;
1480         f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1481         f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1482         f->fmt.vbi.offset = 0;
1483         f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1484             PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1485         f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1486             PAL_VBI_LINES : NTSC_VBI_LINES;
1487         f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1488             PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1489         f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1490         memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1491
1492         return 0;
1493
1494 }
1495
1496 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1497                                   struct v4l2_format *f)
1498 {
1499         struct cx231xx_fh *fh = priv;
1500         struct cx231xx *dev = fh->dev;
1501
1502         f->fmt.vbi.sampling_rate = 6750000 * 4;
1503         f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1504         f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1505         f->fmt.vbi.offset = 0;
1506         f->fmt.vbi.flags = 0;
1507         f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1508             PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1509         f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1510             PAL_VBI_LINES : NTSC_VBI_LINES;
1511         f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1512             PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1513         f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1514         memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1515
1516         return 0;
1517
1518 }
1519
1520 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1521                                   struct v4l2_format *f)
1522 {
1523         struct cx231xx_fh *fh = priv;
1524         struct cx231xx *dev = fh->dev;
1525
1526         if (dev->vbi_stream_on && !fh->stream_on) {
1527                 cx231xx_errdev("%s device in use by another fh\n", __func__);
1528                 return -EBUSY;
1529         }
1530         return vidioc_try_fmt_vbi_cap(file, priv, f);
1531 }
1532
1533 static int vidioc_reqbufs(struct file *file, void *priv,
1534                           struct v4l2_requestbuffers *rb)
1535 {
1536         struct cx231xx_fh *fh = priv;
1537         struct cx231xx *dev = fh->dev;
1538         int rc;
1539
1540         rc = check_dev(dev);
1541         if (rc < 0)
1542                 return rc;
1543
1544         return videobuf_reqbufs(&fh->vb_vidq, rb);
1545 }
1546
1547 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
1548 {
1549         struct cx231xx_fh *fh = priv;
1550         struct cx231xx *dev = fh->dev;
1551         int rc;
1552
1553         rc = check_dev(dev);
1554         if (rc < 0)
1555                 return rc;
1556
1557         return videobuf_querybuf(&fh->vb_vidq, b);
1558 }
1559
1560 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1561 {
1562         struct cx231xx_fh *fh = priv;
1563         struct cx231xx *dev = fh->dev;
1564         int rc;
1565
1566         rc = check_dev(dev);
1567         if (rc < 0)
1568                 return rc;
1569
1570         return videobuf_qbuf(&fh->vb_vidq, b);
1571 }
1572
1573 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1574 {
1575         struct cx231xx_fh *fh = priv;
1576         struct cx231xx *dev = fh->dev;
1577         int rc;
1578
1579         rc = check_dev(dev);
1580         if (rc < 0)
1581                 return rc;
1582
1583         return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1584 }
1585
1586 /* ----------------------------------------------------------- */
1587 /* RADIO ESPECIFIC IOCTLS                                      */
1588 /* ----------------------------------------------------------- */
1589
1590 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1591 {
1592         struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1593
1594         if (t->index)
1595                 return -EINVAL;
1596
1597         strcpy(t->name, "Radio");
1598
1599         call_all(dev, tuner, g_tuner, t);
1600
1601         return 0;
1602 }
1603 static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1604 {
1605         struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1606
1607         if (t->index)
1608                 return -EINVAL;
1609
1610         call_all(dev, tuner, s_tuner, t);
1611
1612         return 0;
1613 }
1614
1615 /*
1616  * cx231xx_v4l2_open()
1617  * inits the device and starts isoc transfer
1618  */
1619 static int cx231xx_v4l2_open(struct file *filp)
1620 {
1621         int errCode = 0, radio = 0;
1622         struct video_device *vdev = video_devdata(filp);
1623         struct cx231xx *dev = video_drvdata(filp);
1624         struct cx231xx_fh *fh;
1625         enum v4l2_buf_type fh_type = 0;
1626
1627         switch (vdev->vfl_type) {
1628         case VFL_TYPE_GRABBER:
1629                 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1630                 break;
1631         case VFL_TYPE_VBI:
1632                 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1633                 break;
1634         case VFL_TYPE_RADIO:
1635                 radio = 1;
1636                 break;
1637         }
1638
1639         cx231xx_videodbg("open dev=%s type=%s users=%d\n",
1640                          video_device_node_name(vdev), v4l2_type_names[fh_type],
1641                          dev->users);
1642
1643 #if 0
1644         errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1645         if (errCode < 0) {
1646                 cx231xx_errdev
1647                     ("Device locked on digital mode. Can't open analog\n");
1648                 return -EBUSY;
1649         }
1650 #endif
1651
1652         fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
1653         if (!fh) {
1654                 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
1655                 return -ENOMEM;
1656         }
1657         if (mutex_lock_interruptible(&dev->lock)) {
1658                 kfree(fh);
1659                 return -ERESTARTSYS;
1660         }
1661         fh->dev = dev;
1662         fh->type = fh_type;
1663         filp->private_data = fh;
1664         v4l2_fh_init(&fh->fh, vdev);
1665
1666         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1667                 /* Power up in Analog TV mode */
1668                 if (dev->board.external_av)
1669                         cx231xx_set_power_mode(dev,
1670                                  POLARIS_AVMODE_ENXTERNAL_AV);
1671                 else
1672                         cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1673
1674 #if 0
1675                 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1676 #endif
1677
1678                 /* set video alternate setting */
1679                 cx231xx_set_video_alternate(dev);
1680
1681                 /* Needed, since GPIO might have disabled power of
1682                    some i2c device */
1683                 cx231xx_config_i2c(dev);
1684
1685                 /* device needs to be initialized before isoc transfer */
1686                 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1687
1688         }
1689         if (radio) {
1690                 cx231xx_videodbg("video_open: setting radio device\n");
1691
1692                 /* cx231xx_start_radio(dev); */
1693
1694                 call_all(dev, tuner, s_radio);
1695         }
1696
1697         dev->users++;
1698
1699         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1700                 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1701                                             NULL, &dev->video_mode.slock,
1702                                             fh->type, V4L2_FIELD_INTERLACED,
1703                                             sizeof(struct cx231xx_buffer),
1704                                             fh, &dev->lock);
1705         if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1706                 /* Set the required alternate setting  VBI interface works in
1707                    Bulk mode only */
1708                 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1709
1710                 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
1711                                             NULL, &dev->vbi_mode.slock,
1712                                             fh->type, V4L2_FIELD_SEQ_TB,
1713                                             sizeof(struct cx231xx_buffer),
1714                                             fh, &dev->lock);
1715         }
1716         mutex_unlock(&dev->lock);
1717         v4l2_fh_add(&fh->fh);
1718
1719         return errCode;
1720 }
1721
1722 /*
1723  * cx231xx_realease_resources()
1724  * unregisters the v4l2,i2c and usb devices
1725  * called when the device gets disconected or at module unload
1726 */
1727 void cx231xx_release_analog_resources(struct cx231xx *dev)
1728 {
1729
1730         /*FIXME: I2C IR should be disconnected */
1731
1732         if (dev->radio_dev) {
1733                 if (video_is_registered(dev->radio_dev))
1734                         video_unregister_device(dev->radio_dev);
1735                 else
1736                         video_device_release(dev->radio_dev);
1737                 dev->radio_dev = NULL;
1738         }
1739         if (dev->vbi_dev) {
1740                 cx231xx_info("V4L2 device %s deregistered\n",
1741                              video_device_node_name(dev->vbi_dev));
1742                 if (video_is_registered(dev->vbi_dev))
1743                         video_unregister_device(dev->vbi_dev);
1744                 else
1745                         video_device_release(dev->vbi_dev);
1746                 dev->vbi_dev = NULL;
1747         }
1748         if (dev->vdev) {
1749                 cx231xx_info("V4L2 device %s deregistered\n",
1750                              video_device_node_name(dev->vdev));
1751
1752                 if (dev->board.has_417)
1753                         cx231xx_417_unregister(dev);
1754
1755                 if (video_is_registered(dev->vdev))
1756                         video_unregister_device(dev->vdev);
1757                 else
1758                         video_device_release(dev->vdev);
1759                 dev->vdev = NULL;
1760         }
1761         v4l2_ctrl_handler_free(&dev->ctrl_handler);
1762         v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1763 }
1764
1765 /*
1766  * cx231xx_close()
1767  * stops streaming and deallocates all resources allocated by the v4l2
1768  * calls and ioctls
1769  */
1770 static int cx231xx_close(struct file *filp)
1771 {
1772         struct cx231xx_fh *fh = filp->private_data;
1773         struct cx231xx *dev = fh->dev;
1774
1775         cx231xx_videodbg("users=%d\n", dev->users);
1776
1777         cx231xx_videodbg("users=%d\n", dev->users);
1778         if (res_check(fh))
1779                 res_free(fh);
1780
1781         /*
1782          * To workaround error number=-71 on EP0 for VideoGrabber,
1783          *       need exclude following.
1784          * FIXME: It is probably safe to remove most of these, as we're
1785          * now avoiding the alternate setting for INDEX_VANC
1786          */
1787         if (!dev->board.no_alt_vanc)
1788                 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1789                         videobuf_stop(&fh->vb_vidq);
1790                         videobuf_mmap_free(&fh->vb_vidq);
1791
1792                         /* the device is already disconnect,
1793                            free the remaining resources */
1794                         if (dev->state & DEV_DISCONNECTED) {
1795                                 if (atomic_read(&dev->devlist_count) > 0) {
1796                                         cx231xx_release_resources(dev);
1797                                         fh->dev = NULL;
1798                                         return 0;
1799                                 }
1800                                 return 0;
1801                         }
1802
1803                         /* do this before setting alternate! */
1804                         cx231xx_uninit_vbi_isoc(dev);
1805
1806                         /* set alternate 0 */
1807                         if (!dev->vbi_or_sliced_cc_mode)
1808                                 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1809                         else
1810                                 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1811
1812                         v4l2_fh_del(&fh->fh);
1813                         v4l2_fh_exit(&fh->fh);
1814                         kfree(fh);
1815                         dev->users--;
1816                         wake_up_interruptible_nr(&dev->open, 1);
1817                         return 0;
1818                 }
1819
1820         v4l2_fh_del(&fh->fh);
1821         dev->users--;
1822         if (!dev->users) {
1823                 videobuf_stop(&fh->vb_vidq);
1824                 videobuf_mmap_free(&fh->vb_vidq);
1825
1826                 /* the device is already disconnect,
1827                    free the remaining resources */
1828                 if (dev->state & DEV_DISCONNECTED) {
1829                         cx231xx_release_resources(dev);
1830                         fh->dev = NULL;
1831                         return 0;
1832                 }
1833
1834                 /* Save some power by putting tuner to sleep */
1835                 call_all(dev, core, s_power, 0);
1836
1837                 /* do this before setting alternate! */
1838                 if (dev->USE_ISO)
1839                         cx231xx_uninit_isoc(dev);
1840                 else
1841                         cx231xx_uninit_bulk(dev);
1842                 cx231xx_set_mode(dev, CX231XX_SUSPEND);
1843
1844                 /* set alternate 0 */
1845                 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1846         }
1847         v4l2_fh_exit(&fh->fh);
1848         kfree(fh);
1849         wake_up_interruptible_nr(&dev->open, 1);
1850         return 0;
1851 }
1852
1853 static int cx231xx_v4l2_close(struct file *filp)
1854 {
1855         struct cx231xx_fh *fh = filp->private_data;
1856         struct cx231xx *dev = fh->dev;
1857         int rc;
1858
1859         mutex_lock(&dev->lock);
1860         rc = cx231xx_close(filp);
1861         mutex_unlock(&dev->lock);
1862         return rc;
1863 }
1864
1865 /*
1866  * cx231xx_v4l2_read()
1867  * will allocate buffers when called for the first time
1868  */
1869 static ssize_t
1870 cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1871                   loff_t *pos)
1872 {
1873         struct cx231xx_fh *fh = filp->private_data;
1874         struct cx231xx *dev = fh->dev;
1875         int rc;
1876
1877         rc = check_dev(dev);
1878         if (rc < 0)
1879                 return rc;
1880
1881         if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1882             (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
1883                 rc = res_get(fh);
1884
1885                 if (unlikely(rc < 0))
1886                         return rc;
1887
1888                 if (mutex_lock_interruptible(&dev->lock))
1889                         return -ERESTARTSYS;
1890                 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1891                                             filp->f_flags & O_NONBLOCK);
1892                 mutex_unlock(&dev->lock);
1893                 return rc;
1894         }
1895         return 0;
1896 }
1897
1898 /*
1899  * cx231xx_v4l2_poll()
1900  * will allocate buffers when called for the first time
1901  */
1902 static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
1903 {
1904         unsigned long req_events = poll_requested_events(wait);
1905         struct cx231xx_fh *fh = filp->private_data;
1906         struct cx231xx *dev = fh->dev;
1907         unsigned res = 0;
1908         int rc;
1909
1910         rc = check_dev(dev);
1911         if (rc < 0)
1912                 return POLLERR;
1913
1914         rc = res_get(fh);
1915
1916         if (unlikely(rc < 0))
1917                 return POLLERR;
1918
1919         if (v4l2_event_pending(&fh->fh))
1920                 res |= POLLPRI;
1921         else
1922                 poll_wait(filp, &fh->fh.wait, wait);
1923
1924         if (!(req_events & (POLLIN | POLLRDNORM)))
1925                 return res;
1926
1927         if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
1928             (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
1929                 mutex_lock(&dev->lock);
1930                 res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1931                 mutex_unlock(&dev->lock);
1932                 return res;
1933         }
1934         return res | POLLERR;
1935 }
1936
1937 /*
1938  * cx231xx_v4l2_mmap()
1939  */
1940 static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1941 {
1942         struct cx231xx_fh *fh = filp->private_data;
1943         struct cx231xx *dev = fh->dev;
1944         int rc;
1945
1946         rc = check_dev(dev);
1947         if (rc < 0)
1948                 return rc;
1949
1950         rc = res_get(fh);
1951
1952         if (unlikely(rc < 0))
1953                 return rc;
1954
1955         if (mutex_lock_interruptible(&dev->lock))
1956                 return -ERESTARTSYS;
1957         rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1958         mutex_unlock(&dev->lock);
1959
1960         cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1961                          (unsigned long)vma->vm_start,
1962                          (unsigned long)vma->vm_end -
1963                          (unsigned long)vma->vm_start, rc);
1964
1965         return rc;
1966 }
1967
1968 static const struct v4l2_file_operations cx231xx_v4l_fops = {
1969         .owner   = THIS_MODULE,
1970         .open    = cx231xx_v4l2_open,
1971         .release = cx231xx_v4l2_close,
1972         .read    = cx231xx_v4l2_read,
1973         .poll    = cx231xx_v4l2_poll,
1974         .mmap    = cx231xx_v4l2_mmap,
1975         .unlocked_ioctl   = video_ioctl2,
1976 };
1977
1978 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1979         .vidioc_querycap               = cx231xx_querycap,
1980         .vidioc_enum_fmt_vid_cap       = vidioc_enum_fmt_vid_cap,
1981         .vidioc_g_fmt_vid_cap          = vidioc_g_fmt_vid_cap,
1982         .vidioc_try_fmt_vid_cap        = vidioc_try_fmt_vid_cap,
1983         .vidioc_s_fmt_vid_cap          = vidioc_s_fmt_vid_cap,
1984         .vidioc_g_fmt_vbi_cap          = vidioc_g_fmt_vbi_cap,
1985         .vidioc_try_fmt_vbi_cap        = vidioc_try_fmt_vbi_cap,
1986         .vidioc_s_fmt_vbi_cap          = vidioc_s_fmt_vbi_cap,
1987         .vidioc_cropcap                = vidioc_cropcap,
1988         .vidioc_reqbufs                = vidioc_reqbufs,
1989         .vidioc_querybuf               = vidioc_querybuf,
1990         .vidioc_qbuf                   = vidioc_qbuf,
1991         .vidioc_dqbuf                  = vidioc_dqbuf,
1992         .vidioc_s_std                  = vidioc_s_std,
1993         .vidioc_g_std                  = vidioc_g_std,
1994         .vidioc_enum_input             = cx231xx_enum_input,
1995         .vidioc_g_input                = cx231xx_g_input,
1996         .vidioc_s_input                = cx231xx_s_input,
1997         .vidioc_streamon               = vidioc_streamon,
1998         .vidioc_streamoff              = vidioc_streamoff,
1999         .vidioc_g_tuner                = cx231xx_g_tuner,
2000         .vidioc_s_tuner                = cx231xx_s_tuner,
2001         .vidioc_g_frequency            = cx231xx_g_frequency,
2002         .vidioc_s_frequency            = cx231xx_s_frequency,
2003 #ifdef CONFIG_VIDEO_ADV_DEBUG
2004         .vidioc_g_chip_info            = cx231xx_g_chip_info,
2005         .vidioc_g_register             = cx231xx_g_register,
2006         .vidioc_s_register             = cx231xx_s_register,
2007 #endif
2008         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2009         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2010 };
2011
2012 static struct video_device cx231xx_vbi_template;
2013
2014 static const struct video_device cx231xx_video_template = {
2015         .fops         = &cx231xx_v4l_fops,
2016         .release      = video_device_release,
2017         .ioctl_ops    = &video_ioctl_ops,
2018         .tvnorms      = V4L2_STD_ALL,
2019 };
2020
2021 static const struct v4l2_file_operations radio_fops = {
2022         .owner   = THIS_MODULE,
2023         .open   = cx231xx_v4l2_open,
2024         .release = cx231xx_v4l2_close,
2025         .poll = v4l2_ctrl_poll,
2026         .unlocked_ioctl = video_ioctl2,
2027 };
2028
2029 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2030         .vidioc_querycap    = cx231xx_querycap,
2031         .vidioc_g_tuner     = radio_g_tuner,
2032         .vidioc_s_tuner     = radio_s_tuner,
2033         .vidioc_g_frequency = cx231xx_g_frequency,
2034         .vidioc_s_frequency = cx231xx_s_frequency,
2035 #ifdef CONFIG_VIDEO_ADV_DEBUG
2036         .vidioc_g_chip_info = cx231xx_g_chip_info,
2037         .vidioc_g_register  = cx231xx_g_register,
2038         .vidioc_s_register  = cx231xx_s_register,
2039 #endif
2040         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2041         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2042 };
2043
2044 static struct video_device cx231xx_radio_template = {
2045         .name      = "cx231xx-radio",
2046         .fops      = &radio_fops,
2047         .ioctl_ops = &radio_ioctl_ops,
2048 };
2049
2050 /******************************** usb interface ******************************/
2051
2052 static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
2053                 const struct video_device
2054                 *template, const char *type_name)
2055 {
2056         struct video_device *vfd;
2057
2058         vfd = video_device_alloc();
2059         if (NULL == vfd)
2060                 return NULL;
2061
2062         *vfd = *template;
2063         vfd->v4l2_dev = &dev->v4l2_dev;
2064         vfd->release = video_device_release;
2065         vfd->debug = video_debug;
2066         vfd->lock = &dev->lock;
2067         set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
2068
2069         snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
2070
2071         video_set_drvdata(vfd, dev);
2072         if (dev->tuner_type == TUNER_ABSENT) {
2073                 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
2074                 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
2075                 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
2076                 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
2077         }
2078         return vfd;
2079 }
2080
2081 int cx231xx_register_analog_devices(struct cx231xx *dev)
2082 {
2083         int ret;
2084
2085         cx231xx_info("%s: v4l2 driver version %s\n",
2086                      dev->name, CX231XX_VERSION);
2087
2088         /* set default norm */
2089         dev->norm = V4L2_STD_PAL;
2090         dev->width = norm_maxw(dev);
2091         dev->height = norm_maxh(dev);
2092         dev->interlaced = 0;
2093
2094         /* Analog specific initialization */
2095         dev->format = &format[0];
2096
2097         /* Set the initial input */
2098         video_mux(dev, dev->video_input);
2099
2100         call_all(dev, core, s_std, dev->norm);
2101
2102         v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
2103         v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
2104
2105         if (dev->sd_cx25840) {
2106                 v4l2_ctrl_add_handler(&dev->ctrl_handler,
2107                                 dev->sd_cx25840->ctrl_handler, NULL);
2108                 v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
2109                                 dev->sd_cx25840->ctrl_handler,
2110                                 v4l2_ctrl_radio_filter);
2111         }
2112
2113         if (dev->ctrl_handler.error)
2114                 return dev->ctrl_handler.error;
2115         if (dev->radio_ctrl_handler.error)
2116                 return dev->radio_ctrl_handler.error;
2117
2118         /* enable vbi capturing */
2119         /* write code here...  */
2120
2121         /* allocate and fill video video_device struct */
2122         dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
2123         if (!dev->vdev) {
2124                 cx231xx_errdev("cannot allocate video_device.\n");
2125                 return -ENODEV;
2126         }
2127
2128         dev->vdev->ctrl_handler = &dev->ctrl_handler;
2129         /* register v4l2 video video_device */
2130         ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2131                                     video_nr[dev->devno]);
2132         if (ret) {
2133                 cx231xx_errdev("unable to register video device (error=%i).\n",
2134                                ret);
2135                 return ret;
2136         }
2137
2138         cx231xx_info("%s/0: registered device %s [v4l2]\n",
2139                      dev->name, video_device_node_name(dev->vdev));
2140
2141         /* Initialize VBI template */
2142         cx231xx_vbi_template = cx231xx_video_template;
2143         strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
2144
2145         /* Allocate and fill vbi video_device struct */
2146         dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
2147
2148         if (!dev->vbi_dev) {
2149                 cx231xx_errdev("cannot allocate video_device.\n");
2150                 return -ENODEV;
2151         }
2152         dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
2153         /* register v4l2 vbi video_device */
2154         ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2155                                     vbi_nr[dev->devno]);
2156         if (ret < 0) {
2157                 cx231xx_errdev("unable to register vbi device\n");
2158                 return ret;
2159         }
2160
2161         cx231xx_info("%s/0: registered device %s\n",
2162                      dev->name, video_device_node_name(dev->vbi_dev));
2163
2164         if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
2165                 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
2166                                                    "radio");
2167                 if (!dev->radio_dev) {
2168                         cx231xx_errdev("cannot allocate video_device.\n");
2169                         return -ENODEV;
2170                 }
2171                 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
2172                 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2173                                             radio_nr[dev->devno]);
2174                 if (ret < 0) {
2175                         cx231xx_errdev("can't register radio device\n");
2176                         return ret;
2177                 }
2178                 cx231xx_info("Registered radio device as %s\n",
2179                              video_device_node_name(dev->radio_dev));
2180         }
2181
2182         cx231xx_info("V4L2 device registered as %s and %s\n",
2183                      video_device_node_name(dev->vdev),
2184                      video_device_node_name(dev->vbi_dev));
2185
2186         return 0;
2187 }