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