Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]

Diff markup

Differences between /linux/drivers/media/video/cx23885/cx23885-video.c (Version 2.6.25) and /linux/drivers/media/video/cx23885/cx23885-video.c (Version 2.6.25.8)


  1 /*                                                  1 /*
  2  *  Driver for the Conexant CX23885 PCIe bridg      2  *  Driver for the Conexant CX23885 PCIe bridge
  3  *                                                  3  *
  4  *  Copyright (c) 2007 Steven Toth <stoth@haup      4  *  Copyright (c) 2007 Steven Toth <stoth@hauppauge.com>
  5  *                                                  5  *
  6  *  This program is free software; you can red      6  *  This program is free software; you can redistribute it and/or modify
  7  *  it under the terms of the GNU General Publ      7  *  it under the terms of the GNU General Public License as published by
  8  *  the Free Software Foundation; either versi      8  *  the Free Software Foundation; either version 2 of the License, or
  9  *  (at your option) any later version.             9  *  (at your option) any later version.
 10  *                                                 10  *
 11  *  This program is distributed in the hope th     11  *  This program is distributed in the hope that it will be useful,
 12  *  but WITHOUT ANY WARRANTY; without even the     12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  *  MERCHANTABILITY or FITNESS FOR A PARTICULA     13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  *                                                 14  *
 15  *  GNU General Public License for more detail     15  *  GNU General Public License for more details.
 16  *                                                 16  *
 17  *  You should have received a copy of the GNU     17  *  You should have received a copy of the GNU General Public License
 18  *  along with this program; if not, write to      18  *  along with this program; if not, write to the Free Software
 19  *  Foundation, Inc., 675 Mass Ave, Cambridge,     19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 20  */                                                20  */
 21                                                    21 
 22 #include <linux/init.h>                            22 #include <linux/init.h>
 23 #include <linux/list.h>                            23 #include <linux/list.h>
 24 #include <linux/module.h>                          24 #include <linux/module.h>
 25 #include <linux/moduleparam.h>                     25 #include <linux/moduleparam.h>
 26 #include <linux/kmod.h>                            26 #include <linux/kmod.h>
 27 #include <linux/kernel.h>                          27 #include <linux/kernel.h>
 28 #include <linux/slab.h>                            28 #include <linux/slab.h>
 29 #include <linux/interrupt.h>                       29 #include <linux/interrupt.h>
 30 #include <linux/delay.h>                           30 #include <linux/delay.h>
 31 #include <linux/kthread.h>                         31 #include <linux/kthread.h>
 32 #include <asm/div64.h>                             32 #include <asm/div64.h>
 33                                                    33 
 34 #include "cx23885.h"                               34 #include "cx23885.h"
 35 #include <media/v4l2-common.h>                     35 #include <media/v4l2-common.h>
 36                                                    36 
 37 #ifdef CONFIG_VIDEO_V4L1_COMPAT                    37 #ifdef CONFIG_VIDEO_V4L1_COMPAT
 38 /* Include V4L1 specific functions. Should be      38 /* Include V4L1 specific functions. Should be removed soon */
 39 #include <linux/videodev.h>                        39 #include <linux/videodev.h>
 40 #endif                                             40 #endif
 41                                                    41 
 42 MODULE_DESCRIPTION("v4l2 driver module for cx2     42 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
 43 MODULE_AUTHOR("Steven Toth <stoth@hauppauge.co     43 MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
 44 MODULE_LICENSE("GPL");                             44 MODULE_LICENSE("GPL");
 45                                                    45 
 46 /* -------------------------------------------     46 /* ------------------------------------------------------------------ */
 47                                                    47 
 48 static unsigned int video_nr[] = {[0 ... (CX23     48 static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
 49 static unsigned int vbi_nr[]   = {[0 ... (CX23     49 static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
 50 static unsigned int radio_nr[] = {[0 ... (CX23     50 static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
 51                                                    51 
 52 module_param_array(video_nr, int, NULL, 0444);     52 module_param_array(video_nr, int, NULL, 0444);
 53 module_param_array(vbi_nr,   int, NULL, 0444);     53 module_param_array(vbi_nr,   int, NULL, 0444);
 54 module_param_array(radio_nr, int, NULL, 0444);     54 module_param_array(radio_nr, int, NULL, 0444);
 55                                                    55 
 56 MODULE_PARM_DESC(video_nr, "video device numbe     56 MODULE_PARM_DESC(video_nr, "video device numbers");
 57 MODULE_PARM_DESC(vbi_nr, "vbi device numbers")     57 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
 58 MODULE_PARM_DESC(radio_nr, "radio device numbe     58 MODULE_PARM_DESC(radio_nr, "radio device numbers");
 59                                                    59 
 60 static unsigned int video_debug;                   60 static unsigned int video_debug;
 61 module_param(video_debug, int, 0644);              61 module_param(video_debug, int, 0644);
 62 MODULE_PARM_DESC(video_debug, "enable debug me     62 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
 63                                                    63 
 64 static unsigned int irq_debug;                     64 static unsigned int irq_debug;
 65 module_param(irq_debug, int, 0644);                65 module_param(irq_debug, int, 0644);
 66 MODULE_PARM_DESC(irq_debug, "enable debug mess     66 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
 67                                                    67 
 68 static unsigned int vid_limit = 16;                68 static unsigned int vid_limit = 16;
 69 module_param(vid_limit, int, 0644);                69 module_param(vid_limit, int, 0644);
 70 MODULE_PARM_DESC(vid_limit, "capture memory li     70 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
 71                                                    71 
 72 #define dprintk(level, fmt, arg...)\               72 #define dprintk(level, fmt, arg...)\
 73         do { if (video_debug >= level)\            73         do { if (video_debug >= level)\
 74                 printk(KERN_DEBUG "%s/0: " fmt     74                 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
 75         } while (0)                                75         } while (0)
 76                                                    76 
 77 /* -------------------------------------------     77 /* ------------------------------------------------------------------- */
 78 /* static data                                     78 /* static data                                                         */
 79                                                    79 
 80 #define FORMAT_FLAGS_PACKED       0x01             80 #define FORMAT_FLAGS_PACKED       0x01
 81                                                    81 
 82 static struct cx23885_fmt formats[] = {            82 static struct cx23885_fmt formats[] = {
 83         {                                          83         {
 84                 .name     = "8 bpp, gray",         84                 .name     = "8 bpp, gray",
 85                 .fourcc   = V4L2_PIX_FMT_GREY,     85                 .fourcc   = V4L2_PIX_FMT_GREY,
 86                 .depth    = 8,                     86                 .depth    = 8,
 87                 .flags    = FORMAT_FLAGS_PACKE     87                 .flags    = FORMAT_FLAGS_PACKED,
 88         }, {                                       88         }, {
 89                 .name     = "15 bpp RGB, le",      89                 .name     = "15 bpp RGB, le",
 90                 .fourcc   = V4L2_PIX_FMT_RGB55     90                 .fourcc   = V4L2_PIX_FMT_RGB555,
 91                 .depth    = 16,                    91                 .depth    = 16,
 92                 .flags    = FORMAT_FLAGS_PACKE     92                 .flags    = FORMAT_FLAGS_PACKED,
 93         }, {                                       93         }, {
 94                 .name     = "15 bpp RGB, be",      94                 .name     = "15 bpp RGB, be",
 95                 .fourcc   = V4L2_PIX_FMT_RGB55     95                 .fourcc   = V4L2_PIX_FMT_RGB555X,
 96                 .depth    = 16,                    96                 .depth    = 16,
 97                 .flags    = FORMAT_FLAGS_PACKE     97                 .flags    = FORMAT_FLAGS_PACKED,
 98         }, {                                       98         }, {
 99                 .name     = "16 bpp RGB, le",      99                 .name     = "16 bpp RGB, le",
100                 .fourcc   = V4L2_PIX_FMT_RGB56    100                 .fourcc   = V4L2_PIX_FMT_RGB565,
101                 .depth    = 16,                   101                 .depth    = 16,
102                 .flags    = FORMAT_FLAGS_PACKE    102                 .flags    = FORMAT_FLAGS_PACKED,
103         }, {                                      103         }, {
104                 .name     = "16 bpp RGB, be",     104                 .name     = "16 bpp RGB, be",
105                 .fourcc   = V4L2_PIX_FMT_RGB56    105                 .fourcc   = V4L2_PIX_FMT_RGB565X,
106                 .depth    = 16,                   106                 .depth    = 16,
107                 .flags    = FORMAT_FLAGS_PACKE    107                 .flags    = FORMAT_FLAGS_PACKED,
108         }, {                                      108         }, {
109                 .name     = "24 bpp RGB, le",     109                 .name     = "24 bpp RGB, le",
110                 .fourcc   = V4L2_PIX_FMT_BGR24    110                 .fourcc   = V4L2_PIX_FMT_BGR24,
111                 .depth    = 24,                   111                 .depth    = 24,
112                 .flags    = FORMAT_FLAGS_PACKE    112                 .flags    = FORMAT_FLAGS_PACKED,
113         }, {                                      113         }, {
114                 .name     = "32 bpp RGB, le",     114                 .name     = "32 bpp RGB, le",
115                 .fourcc   = V4L2_PIX_FMT_BGR32    115                 .fourcc   = V4L2_PIX_FMT_BGR32,
116                 .depth    = 32,                   116                 .depth    = 32,
117                 .flags    = FORMAT_FLAGS_PACKE    117                 .flags    = FORMAT_FLAGS_PACKED,
118         }, {                                      118         }, {
119                 .name     = "32 bpp RGB, be",     119                 .name     = "32 bpp RGB, be",
120                 .fourcc   = V4L2_PIX_FMT_RGB32    120                 .fourcc   = V4L2_PIX_FMT_RGB32,
121                 .depth    = 32,                   121                 .depth    = 32,
122                 .flags    = FORMAT_FLAGS_PACKE    122                 .flags    = FORMAT_FLAGS_PACKED,
123         }, {                                      123         }, {
124                 .name     = "4:2:2, packed, YU    124                 .name     = "4:2:2, packed, YUYV",
125                 .fourcc   = V4L2_PIX_FMT_YUYV,    125                 .fourcc   = V4L2_PIX_FMT_YUYV,
126                 .depth    = 16,                   126                 .depth    = 16,
127                 .flags    = FORMAT_FLAGS_PACKE    127                 .flags    = FORMAT_FLAGS_PACKED,
128         }, {                                      128         }, {
129                 .name     = "4:2:2, packed, UY    129                 .name     = "4:2:2, packed, UYVY",
130                 .fourcc   = V4L2_PIX_FMT_UYVY,    130                 .fourcc   = V4L2_PIX_FMT_UYVY,
131                 .depth    = 16,                   131                 .depth    = 16,
132                 .flags    = FORMAT_FLAGS_PACKE    132                 .flags    = FORMAT_FLAGS_PACKED,
133         },                                        133         },
134 };                                                134 };
135                                                   135 
136 static struct cx23885_fmt *format_by_fourcc(un    136 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
137 {                                                 137 {
138         unsigned int i;                           138         unsigned int i;
139                                                   139 
140         for (i = 0; i < ARRAY_SIZE(formats); i    140         for (i = 0; i < ARRAY_SIZE(formats); i++)
141                 if (formats[i].fourcc == fourc    141                 if (formats[i].fourcc == fourcc)
142                         return formats+i;         142                         return formats+i;
143                                                   143 
144         printk(KERN_ERR "%s(0x%08x) NOT FOUND\    144         printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __FUNCTION__, fourcc);
145         return NULL;                              145         return NULL;
146 }                                                 146 }
147                                                   147 
148 /* -------------------------------------------    148 /* ------------------------------------------------------------------- */
149                                                   149 
150 static const struct v4l2_queryctrl no_ctl = {     150 static const struct v4l2_queryctrl no_ctl = {
151         .name  = "42",                            151         .name  = "42",
152         .flags = V4L2_CTRL_FLAG_DISABLED,         152         .flags = V4L2_CTRL_FLAG_DISABLED,
153 };                                                153 };
154                                                   154 
155 static struct cx23885_ctrl cx23885_ctls[] = {     155 static struct cx23885_ctrl cx23885_ctls[] = {
156         /* --- video --- */                       156         /* --- video --- */
157         {                                         157         {
158                 .v = {                            158                 .v = {
159                         .id            = V4L2_    159                         .id            = V4L2_CID_BRIGHTNESS,
160                         .name          = "Brig    160                         .name          = "Brightness",
161                         .minimum       = 0x00,    161                         .minimum       = 0x00,
162                         .maximum       = 0xff,    162                         .maximum       = 0xff,
163                         .step          = 1,       163                         .step          = 1,
164                         .default_value = 0x7f,    164                         .default_value = 0x7f,
165                         .type          = V4L2_    165                         .type          = V4L2_CTRL_TYPE_INTEGER,
166                 },                                166                 },
167                 .off                   = 128,     167                 .off                   = 128,
168                 .reg                   = LUMA_    168                 .reg                   = LUMA_CTRL,
169                 .mask                  = 0x00f    169                 .mask                  = 0x00ff,
170                 .shift                 = 0,       170                 .shift                 = 0,
171         }, {                                      171         }, {
172                 .v = {                            172                 .v = {
173                         .id            = V4L2_    173                         .id            = V4L2_CID_CONTRAST,
174                         .name          = "Cont    174                         .name          = "Contrast",
175                         .minimum       = 0,       175                         .minimum       = 0,
176                         .maximum       = 0xff,    176                         .maximum       = 0xff,
177                         .step          = 1,       177                         .step          = 1,
178                         .default_value = 0x3f,    178                         .default_value = 0x3f,
179                         .type          = V4L2_    179                         .type          = V4L2_CTRL_TYPE_INTEGER,
180                 },                                180                 },
181                 .off                   = 0,       181                 .off                   = 0,
182                 .reg                   = LUMA_    182                 .reg                   = LUMA_CTRL,
183                 .mask                  = 0xff0    183                 .mask                  = 0xff00,
184                 .shift                 = 8,       184                 .shift                 = 8,
185         }, {                                      185         }, {
186                 .v = {                            186                 .v = {
187                         .id            = V4L2_    187                         .id            = V4L2_CID_HUE,
188                         .name          = "Hue"    188                         .name          = "Hue",
189                         .minimum       = 0,       189                         .minimum       = 0,
190                         .maximum       = 0xff,    190                         .maximum       = 0xff,
191                         .step          = 1,       191                         .step          = 1,
192                         .default_value = 0x7f,    192                         .default_value = 0x7f,
193                         .type          = V4L2_    193                         .type          = V4L2_CTRL_TYPE_INTEGER,
194                 },                                194                 },
195                 .off                   = 128,     195                 .off                   = 128,
196                 .reg                   = CHROM    196                 .reg                   = CHROMA_CTRL,
197                 .mask                  = 0xff0    197                 .mask                  = 0xff0000,
198                 .shift                 = 16,      198                 .shift                 = 16,
199         }, {                                      199         }, {
200                 /* strictly, this only describ    200                 /* strictly, this only describes only U saturation.
201                  * V saturation is handled spe    201                  * V saturation is handled specially through code.
202                  */                               202                  */
203                 .v = {                            203                 .v = {
204                         .id            = V4L2_    204                         .id            = V4L2_CID_SATURATION,
205                         .name          = "Satu    205                         .name          = "Saturation",
206                         .minimum       = 0,       206                         .minimum       = 0,
207                         .maximum       = 0xff,    207                         .maximum       = 0xff,
208                         .step          = 1,       208                         .step          = 1,
209                         .default_value = 0x7f,    209                         .default_value = 0x7f,
210                         .type          = V4L2_    210                         .type          = V4L2_CTRL_TYPE_INTEGER,
211                 },                                211                 },
212                 .off                   = 0,       212                 .off                   = 0,
213                 .reg                   = CHROM    213                 .reg                   = CHROMA_CTRL,
214                 .mask                  = 0x00f    214                 .mask                  = 0x00ff,
215                 .shift                 = 0,       215                 .shift                 = 0,
216         }, {                                      216         }, {
217         /* --- audio --- */                       217         /* --- audio --- */
218                 .v = {                            218                 .v = {
219                         .id            = V4L2_    219                         .id            = V4L2_CID_AUDIO_MUTE,
220                         .name          = "Mute    220                         .name          = "Mute",
221                         .minimum       = 0,       221                         .minimum       = 0,
222                         .maximum       = 1,       222                         .maximum       = 1,
223                         .default_value = 1,       223                         .default_value = 1,
224                         .type          = V4L2_    224                         .type          = V4L2_CTRL_TYPE_BOOLEAN,
225                 },                                225                 },
226                 .reg                   = PATH1    226                 .reg                   = PATH1_CTL1,
227                 .mask                  = (0x1f    227                 .mask                  = (0x1f << 24),
228                 .shift                 = 24,      228                 .shift                 = 24,
229         }, {                                      229         }, {
230                 .v = {                            230                 .v = {
231                         .id            = V4L2_    231                         .id            = V4L2_CID_AUDIO_VOLUME,
232                         .name          = "Volu    232                         .name          = "Volume",
233                         .minimum       = 0,       233                         .minimum       = 0,
234                         .maximum       = 0x3f,    234                         .maximum       = 0x3f,
235                         .step          = 1,       235                         .step          = 1,
236                         .default_value = 0x3f,    236                         .default_value = 0x3f,
237                         .type          = V4L2_    237                         .type          = V4L2_CTRL_TYPE_INTEGER,
238                 },                                238                 },
239                 .reg                   = PATH1    239                 .reg                   = PATH1_VOL_CTL,
240                 .mask                  = 0xff,    240                 .mask                  = 0xff,
241                 .shift                 = 0,       241                 .shift                 = 0,
242         }                                         242         }
243 };                                                243 };
244 static const int CX23885_CTLS = ARRAY_SIZE(cx2    244 static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
245                                                   245 
246 const u32 cx23885_user_ctrls[] = {                246 const u32 cx23885_user_ctrls[] = {
247         V4L2_CID_USER_CLASS,                      247         V4L2_CID_USER_CLASS,
248         V4L2_CID_BRIGHTNESS,                      248         V4L2_CID_BRIGHTNESS,
249         V4L2_CID_CONTRAST,                        249         V4L2_CID_CONTRAST,
250         V4L2_CID_SATURATION,                      250         V4L2_CID_SATURATION,
251         V4L2_CID_HUE,                             251         V4L2_CID_HUE,
252         V4L2_CID_AUDIO_VOLUME,                    252         V4L2_CID_AUDIO_VOLUME,
253         V4L2_CID_AUDIO_MUTE,                      253         V4L2_CID_AUDIO_MUTE,
254         0                                         254         0
255 };                                                255 };
256 EXPORT_SYMBOL(cx23885_user_ctrls);                256 EXPORT_SYMBOL(cx23885_user_ctrls);
257                                                   257 
258 static const u32 *ctrl_classes[] = {              258 static const u32 *ctrl_classes[] = {
259         cx23885_user_ctrls,                       259         cx23885_user_ctrls,
260         NULL                                      260         NULL
261 };                                                261 };
262                                                   262 
263 void cx23885_video_wakeup(struct cx23885_dev *    263 void cx23885_video_wakeup(struct cx23885_dev *dev,
264                  struct cx23885_dmaqueue *q, u    264                  struct cx23885_dmaqueue *q, u32 count)
265 {                                                 265 {
266         struct cx23885_buffer *buf;               266         struct cx23885_buffer *buf;
267         int bc;                                   267         int bc;
268                                                   268 
269         for (bc = 0;; bc++) {                     269         for (bc = 0;; bc++) {
270                 if (list_empty(&q->active))       270                 if (list_empty(&q->active))
271                         break;                    271                         break;
272                 buf = list_entry(q->active.nex    272                 buf = list_entry(q->active.next,
273                                  struct cx2388    273                                  struct cx23885_buffer, vb.queue);
274                                                   274 
275                 /* count comes from the hw and    275                 /* count comes from the hw and is is 16bit wide --
276                  * this trick handles wrap-aro    276                  * this trick handles wrap-arounds correctly for
277                  * up to 32767 buffers in flig    277                  * up to 32767 buffers in flight... */
278                 if ((s16) (count - buf->count)    278                 if ((s16) (count - buf->count) < 0)
279                         break;                    279                         break;
280                                                   280 
281                 do_gettimeofday(&buf->vb.ts);     281                 do_gettimeofday(&buf->vb.ts);
282                 dprintk(2, "[%p/%d] wakeup reg    282                 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
283                         count, buf->count);       283                         count, buf->count);
284                 buf->vb.state = VIDEOBUF_DONE;    284                 buf->vb.state = VIDEOBUF_DONE;
285                 list_del(&buf->vb.queue);         285                 list_del(&buf->vb.queue);
286                 wake_up(&buf->vb.done);           286                 wake_up(&buf->vb.done);
287         }                                         287         }
288         if (list_empty(&q->active)) {             288         if (list_empty(&q->active)) {
289                 del_timer(&q->timeout);           289                 del_timer(&q->timeout);
290         } else {                                  290         } else {
291                 mod_timer(&q->timeout, jiffies    291                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
292         }                                         292         }
293         if (bc != 1)                              293         if (bc != 1)
294                 printk(KERN_ERR "%s: %d buffer    294                 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
295                         __FUNCTION__, bc);        295                         __FUNCTION__, bc);
296 }                                                 296 }
297                                                   297 
298 int cx23885_set_tvnorm(struct cx23885_dev *dev    298 int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
299 {                                                 299 {
300         dprintk(1, "%s(norm = 0x%08x) name: [%    300         dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
301                 __FUNCTION__,                     301                 __FUNCTION__,
302                 (unsigned int)norm,               302                 (unsigned int)norm,
303                 v4l2_norm_to_name(norm));         303                 v4l2_norm_to_name(norm));
304                                                   304 
305         dev->tvnorm = norm;                       305         dev->tvnorm = norm;
306                                                   306 
307         /* Tell the analog tuner/demods */        307         /* Tell the analog tuner/demods */
308         cx23885_call_i2c_clients(&dev->i2c_bus    308         cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
309                                                   309 
310         /* Tell the internal A/V decoder */       310         /* Tell the internal A/V decoder */
311         cx23885_call_i2c_clients(&dev->i2c_bus    311         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
312                                                   312 
313         return 0;                                 313         return 0;
314 }                                                 314 }
315                                                   315 
316 struct video_device *cx23885_vdev_init(struct     316 struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
317                                     struct pci    317                                     struct pci_dev *pci,
318                                     struct vid    318                                     struct video_device *template,
319                                     char *type    319                                     char *type)
320 {                                                 320 {
321         struct video_device *vfd;                 321         struct video_device *vfd;
322         dprintk(1, "%s()\n", __FUNCTION__);       322         dprintk(1, "%s()\n", __FUNCTION__);
323                                                   323 
324         vfd = video_device_alloc();               324         vfd = video_device_alloc();
325         if (NULL == vfd)                          325         if (NULL == vfd)
326                 return NULL;                      326                 return NULL;
327         *vfd = *template;                         327         *vfd = *template;
328         vfd->minor   = -1;                        328         vfd->minor   = -1;
329         vfd->dev     = &pci->dev;                 329         vfd->dev     = &pci->dev;
330         vfd->release = video_device_release;      330         vfd->release = video_device_release;
331         snprintf(vfd->name, sizeof(vfd->name),    331         snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
332                  dev->name, type, cx23885_boar    332                  dev->name, type, cx23885_boards[dev->board].name);
333         return vfd;                               333         return vfd;
334 }                                                 334 }
335                                                   335 
336 int cx23885_ctrl_query(struct v4l2_queryctrl *    336 int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
337 {                                                 337 {
338         int i;                                    338         int i;
339                                                   339 
340         if (qctrl->id < V4L2_CID_BASE ||          340         if (qctrl->id < V4L2_CID_BASE ||
341             qctrl->id >= V4L2_CID_LASTP1)         341             qctrl->id >= V4L2_CID_LASTP1)
342                 return -EINVAL;                   342                 return -EINVAL;
343         for (i = 0; i < CX23885_CTLS; i++)        343         for (i = 0; i < CX23885_CTLS; i++)
344                 if (cx23885_ctls[i].v.id == qc    344                 if (cx23885_ctls[i].v.id == qctrl->id)
345                         break;                    345                         break;
346         if (i == CX23885_CTLS) {                  346         if (i == CX23885_CTLS) {
347                 *qctrl = no_ctl;                  347                 *qctrl = no_ctl;
348                 return 0;                         348                 return 0;
349         }                                         349         }
350         *qctrl = cx23885_ctls[i].v;               350         *qctrl = cx23885_ctls[i].v;
351         return 0;                                 351         return 0;
352 }                                                 352 }
353 EXPORT_SYMBOL(cx23885_ctrl_query);                353 EXPORT_SYMBOL(cx23885_ctrl_query);
354                                                   354 
355 /* -------------------------------------------    355 /* ------------------------------------------------------------------- */
356 /* resource management                            356 /* resource management                                                 */
357                                                   357 
358 static int res_get(struct cx23885_dev *dev, st    358 static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
359         unsigned int bit)                         359         unsigned int bit)
360 {                                                 360 {
361         dprintk(1, "%s()\n", __FUNCTION__);       361         dprintk(1, "%s()\n", __FUNCTION__);
362         if (fh->resources & bit)                  362         if (fh->resources & bit)
363                 /* have it already allocated *    363                 /* have it already allocated */
364                 return 1;                         364                 return 1;
365                                                   365 
366         /* is it free? */                         366         /* is it free? */
367         mutex_lock(&dev->lock);                   367         mutex_lock(&dev->lock);
368         if (dev->resources & bit) {               368         if (dev->resources & bit) {
369                 /* no, someone else uses it */    369                 /* no, someone else uses it */
370                 mutex_unlock(&dev->lock);         370                 mutex_unlock(&dev->lock);
371                 return 0;                         371                 return 0;
372         }                                         372         }
373         /* it's free, grab it */                  373         /* it's free, grab it */
374         fh->resources  |= bit;                    374         fh->resources  |= bit;
375         dev->resources |= bit;                    375         dev->resources |= bit;
376         dprintk(1, "res: get %d\n", bit);         376         dprintk(1, "res: get %d\n", bit);
377         mutex_unlock(&dev->lock);                 377         mutex_unlock(&dev->lock);
378         return 1;                                 378         return 1;
379 }                                                 379 }
380                                                   380 
381 static int res_check(struct cx23885_fh *fh, un    381 static int res_check(struct cx23885_fh *fh, unsigned int bit)
382 {                                                 382 {
383         return (fh->resources & bit);             383         return (fh->resources & bit);
384 }                                                 384 }
385                                                   385 
386 static int res_locked(struct cx23885_dev *dev,    386 static int res_locked(struct cx23885_dev *dev, unsigned int bit)
387 {                                                 387 {
388         return (dev->resources & bit);            388         return (dev->resources & bit);
389 }                                                 389 }
390                                                   390 
391 static void res_free(struct cx23885_dev *dev,     391 static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
392         unsigned int bits)                        392         unsigned int bits)
393 {                                                 393 {
394         BUG_ON((fh->resources & bits) != bits)    394         BUG_ON((fh->resources & bits) != bits);
395         dprintk(1, "%s()\n", __FUNCTION__);       395         dprintk(1, "%s()\n", __FUNCTION__);
396                                                   396 
397         mutex_lock(&dev->lock);                   397         mutex_lock(&dev->lock);
398         fh->resources  &= ~bits;                  398         fh->resources  &= ~bits;
399         dev->resources &= ~bits;                  399         dev->resources &= ~bits;
400         dprintk(1, "res: put %d\n", bits);        400         dprintk(1, "res: put %d\n", bits);
401         mutex_unlock(&dev->lock);                 401         mutex_unlock(&dev->lock);
402 }                                                 402 }
403                                                   403 
404 int cx23885_video_mux(struct cx23885_dev *dev,    404 int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
405 {                                                 405 {
406         struct v4l2_routing route;                406         struct v4l2_routing route;
407         memset(&route, 0, sizeof(route));         407         memset(&route, 0, sizeof(route));
408                                                   408 
409         dprintk(1, "%s() video_mux: %d [vmux=%    409         dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
410                 __FUNCTION__,                     410                 __FUNCTION__,
411                 input, INPUT(input)->vmux,        411                 input, INPUT(input)->vmux,
412                 INPUT(input)->gpio0, INPUT(inp    412                 INPUT(input)->gpio0, INPUT(input)->gpio1,
413                 INPUT(input)->gpio2, INPUT(inp    413                 INPUT(input)->gpio2, INPUT(input)->gpio3);
414         dev->input = input;                       414         dev->input = input;
415                                                   415 
416         route.input = INPUT(input)->vmux;         416         route.input = INPUT(input)->vmux;
417                                                   417 
418         /* Tell the internal A/V decoder */       418         /* Tell the internal A/V decoder */
419         cx23885_call_i2c_clients(&dev->i2c_bus    419         cx23885_call_i2c_clients(&dev->i2c_bus[2],
420                 VIDIOC_INT_S_VIDEO_ROUTING, &r    420                 VIDIOC_INT_S_VIDEO_ROUTING, &route);
421                                                   421 
422         return 0;                                 422         return 0;
423 }                                                 423 }
424 EXPORT_SYMBOL(cx23885_video_mux);                 424 EXPORT_SYMBOL(cx23885_video_mux);
425                                                   425 
426 /* -------------------------------------------    426 /* ------------------------------------------------------------------ */
427 int cx23885_set_scale(struct cx23885_dev *dev,    427 int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
428         unsigned int height, enum v4l2_field f    428         unsigned int height, enum v4l2_field field)
429 {                                                 429 {
430         dprintk(1, "%s()\n", __FUNCTION__);       430         dprintk(1, "%s()\n", __FUNCTION__);
431         return 0;                                 431         return 0;
432 }                                                 432 }
433                                                   433 
434 static int cx23885_start_video_dma(struct cx23    434 static int cx23885_start_video_dma(struct cx23885_dev *dev,
435                            struct cx23885_dmaq    435                            struct cx23885_dmaqueue *q,
436                            struct cx23885_buff    436                            struct cx23885_buffer *buf)
437 {                                                 437 {
438         dprintk(1, "%s()\n", __FUNCTION__);       438         dprintk(1, "%s()\n", __FUNCTION__);
439                                                   439 
440         /* setup fifo + format */                 440         /* setup fifo + format */
441         cx23885_sram_channel_setup(dev, &dev->    441         cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
442                                 buf->bpl, buf-    442                                 buf->bpl, buf->risc.dma);
443         cx23885_set_scale(dev, buf->vb.width,     443         cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
444                                                   444 
445         /* reset counter */                       445         /* reset counter */
446         cx_write(VID_A_GPCNT_CTL, 3);             446         cx_write(VID_A_GPCNT_CTL, 3);
447         q->count = 1;                             447         q->count = 1;
448                                                   448 
449         /* enable irq */                          449         /* enable irq */
450         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MS    450         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
451         cx_set(VID_A_INT_MSK, 0x000011);          451         cx_set(VID_A_INT_MSK, 0x000011);
452                                                   452 
453         /* start dma */                           453         /* start dma */
454         cx_set(DEV_CNTRL2, (1<<5));               454         cx_set(DEV_CNTRL2, (1<<5));
455         cx_set(VID_A_DMA_CTL, 0x11); /* FIFO a    455         cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
456                                                   456 
457         return 0;                                 457         return 0;
458 }                                                 458 }
459                                                   459 
460                                                   460 
461 static int cx23885_restart_video_queue(struct     461 static int cx23885_restart_video_queue(struct cx23885_dev *dev,
462                                struct cx23885_    462                                struct cx23885_dmaqueue *q)
463 {                                                 463 {
464         struct cx23885_buffer *buf, *prev;        464         struct cx23885_buffer *buf, *prev;
465         struct list_head *item;                   465         struct list_head *item;
466         dprintk(1, "%s()\n", __FUNCTION__);       466         dprintk(1, "%s()\n", __FUNCTION__);
467                                                   467 
468         if (!list_empty(&q->active)) {            468         if (!list_empty(&q->active)) {
469                 buf = list_entry(q->active.nex    469                 buf = list_entry(q->active.next, struct cx23885_buffer,
470                         vb.queue);                470                         vb.queue);
471                 dprintk(2, "restart_queue [%p/    471                 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
472                         buf, buf->vb.i);          472                         buf, buf->vb.i);
473                 cx23885_start_video_dma(dev, q    473                 cx23885_start_video_dma(dev, q, buf);
474                 list_for_each(item, &q->active    474                 list_for_each(item, &q->active) {
475                         buf = list_entry(item,    475                         buf = list_entry(item, struct cx23885_buffer,
476                                 vb.queue);        476                                 vb.queue);
477                         buf->count    = q->cou    477                         buf->count    = q->count++;
478                 }                                 478                 }
479                 mod_timer(&q->timeout, jiffies    479                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
480                 return 0;                         480                 return 0;
481         }                                         481         }
482                                                   482 
483         prev = NULL;                              483         prev = NULL;
484         for (;;) {                                484         for (;;) {
485                 if (list_empty(&q->queued))       485                 if (list_empty(&q->queued))
486                         return 0;                 486                         return 0;
487                 buf = list_entry(q->queued.nex    487                 buf = list_entry(q->queued.next, struct cx23885_buffer,
488                         vb.queue);                488                         vb.queue);
489                 if (NULL == prev) {               489                 if (NULL == prev) {
490                         list_move_tail(&buf->v    490                         list_move_tail(&buf->vb.queue, &q->active);
491                         cx23885_start_video_dm    491                         cx23885_start_video_dma(dev, q, buf);
492                         buf->vb.state = VIDEOB    492                         buf->vb.state = VIDEOBUF_ACTIVE;
493                         buf->count    = q->cou    493                         buf->count    = q->count++;
494                         mod_timer(&q->timeout,    494                         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
495                         dprintk(2, "[%p/%d] re    495                         dprintk(2, "[%p/%d] restart_queue - first active\n",
496                                 buf, buf->vb.i    496                                 buf, buf->vb.i);
497                                                   497 
498                 } else if (prev->vb.width  ==     498                 } else if (prev->vb.width  == buf->vb.width  &&
499                            prev->vb.height ==     499                            prev->vb.height == buf->vb.height &&
500                            prev->fmt       ==     500                            prev->fmt       == buf->fmt) {
501                         list_move_tail(&buf->v    501                         list_move_tail(&buf->vb.queue, &q->active);
502                         buf->vb.state = VIDEOB    502                         buf->vb.state = VIDEOBUF_ACTIVE;
503                         buf->count    = q->cou    503                         buf->count    = q->count++;
504                         prev->risc.jmp[1] = cp    504                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
505                         prev->risc.jmp[2] = cp    505                         prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
506                         dprintk(2, "[%p/%d] re    506                         dprintk(2, "[%p/%d] restart_queue - move to active\n",
507                                 buf, buf->vb.i    507                                 buf, buf->vb.i);
508                 } else {                          508                 } else {
509                         return 0;                 509                         return 0;
510                 }                                 510                 }
511                 prev = buf;                       511                 prev = buf;
512         }                                         512         }
513 }                                                 513 }
514                                                   514 
515 static int buffer_setup(struct videobuf_queue     515 static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
516         unsigned int *size)                       516         unsigned int *size)
517 {                                                 517 {
518         struct cx23885_fh *fh = q->priv_data;     518         struct cx23885_fh *fh = q->priv_data;
519                                                   519 
520         *size = fh->fmt->depth*fh->width*fh->h    520         *size = fh->fmt->depth*fh->width*fh->height >> 3;
521         if (0 == *count)                          521         if (0 == *count)
522                 *count = 32;                      522                 *count = 32;
523         while (*size * *count > vid_limit * 10    523         while (*size * *count > vid_limit * 1024 * 1024)
524                 (*count)--;                       524                 (*count)--;
525         return 0;                                 525         return 0;
526 }                                                 526 }
527                                                   527 
528 static int buffer_prepare(struct videobuf_queu    528 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
529                enum v4l2_field field)             529                enum v4l2_field field)
530 {                                                 530 {
531         struct cx23885_fh *fh  = q->priv_data;    531         struct cx23885_fh *fh  = q->priv_data;
532         struct cx23885_dev *dev = fh->dev;        532         struct cx23885_dev *dev = fh->dev;
533         struct cx23885_buffer *buf =              533         struct cx23885_buffer *buf =
534                 container_of(vb, struct cx2388    534                 container_of(vb, struct cx23885_buffer, vb);
535         int rc, init_buffer = 0;                  535         int rc, init_buffer = 0;
536         u32 line0_offset, line1_offset;           536         u32 line0_offset, line1_offset;
537         struct videobuf_dmabuf *dma = videobuf    537         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
538                                                   538 
539         BUG_ON(NULL == fh->fmt);                  539         BUG_ON(NULL == fh->fmt);
540         if (fh->width  < 48 || fh->width  > no    540         if (fh->width  < 48 || fh->width  > norm_maxw(dev->tvnorm) ||
541             fh->height < 32 || fh->height > no    541             fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
542                 return -EINVAL;                   542                 return -EINVAL;
543         buf->vb.size = (fh->width * fh->height    543         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
544         if (0 != buf->vb.baddr  &&  buf->vb.bs    544         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
545                 return -EINVAL;                   545                 return -EINVAL;
546                                                   546 
547         if (buf->fmt       != fh->fmt    ||       547         if (buf->fmt       != fh->fmt    ||
548             buf->vb.width  != fh->width  ||       548             buf->vb.width  != fh->width  ||
549             buf->vb.height != fh->height ||       549             buf->vb.height != fh->height ||
550             buf->vb.field  != field) {            550             buf->vb.field  != field) {
551                 buf->fmt       = fh->fmt;         551                 buf->fmt       = fh->fmt;
552                 buf->vb.width  = fh->width;       552                 buf->vb.width  = fh->width;
553                 buf->vb.height = fh->height;      553                 buf->vb.height = fh->height;
554                 buf->vb.field  = field;           554                 buf->vb.field  = field;
555                 init_buffer = 1;                  555                 init_buffer = 1;
556         }                                         556         }
557                                                   557 
558         if (VIDEOBUF_NEEDS_INIT == buf->vb.sta    558         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
559                 init_buffer = 1;                  559                 init_buffer = 1;
560                 rc = videobuf_iolock(q, &buf->    560                 rc = videobuf_iolock(q, &buf->vb, NULL);
561                 if (0 != rc)                      561                 if (0 != rc)
562                         goto fail;                562                         goto fail;
563         }                                         563         }
564                                                   564 
565         if (init_buffer) {                        565         if (init_buffer) {
566                 buf->bpl = buf->vb.width * buf    566                 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
567                 switch (buf->vb.field) {          567                 switch (buf->vb.field) {
568                 case V4L2_FIELD_TOP:              568                 case V4L2_FIELD_TOP:
569                         cx23885_risc_buffer(de    569                         cx23885_risc_buffer(dev->pci, &buf->risc,
570                                          dma->    570                                          dma->sglist, 0, UNSET,
571                                          buf->    571                                          buf->bpl, 0, buf->vb.height);
572                         break;                    572                         break;
573                 case V4L2_FIELD_BOTTOM:           573                 case V4L2_FIELD_BOTTOM:
574                         cx23885_risc_buffer(de    574                         cx23885_risc_buffer(dev->pci, &buf->risc,
575                                          dma->    575                                          dma->sglist, UNSET, 0,
576                                          buf->    576                                          buf->bpl, 0, buf->vb.height);
577                         break;                    577                         break;
578                 case V4L2_FIELD_INTERLACED:       578                 case V4L2_FIELD_INTERLACED:
579                         if (dev->tvnorm & V4L2    579                         if (dev->tvnorm & V4L2_STD_NTSC) {
580                                 /* cx25840 tra    580                                 /* cx25840 transmits NTSC bottom field first */
581                                 dprintk(1, "%s    581                                 dprintk(1, "%s() Creating NTSC risc\n",
582                                         __FUNC    582                                         __FUNCTION__);
583                                 line0_offset =    583                                 line0_offset = buf->bpl;
584                                 line1_offset =    584                                 line1_offset = 0;
585                         } else {                  585                         } else {
586                                 /* All other f    586                                 /* All other formats are top field first */
587                                 dprintk(1, "%s    587                                 dprintk(1, "%s() Creating PAL/SECAM risc\n",
588                                         __FUNC    588                                         __FUNCTION__);
589                                 line0_offset =    589                                 line0_offset = 0;
590                                 line1_offset =    590                                 line1_offset = buf->bpl;
591                         }                         591                         }
592                         cx23885_risc_buffer(de    592                         cx23885_risc_buffer(dev->pci, &buf->risc,
593                                         dma->s    593                                         dma->sglist, line0_offset,
594                                         line1_    594                                         line1_offset,
595                                         buf->b    595                                         buf->bpl, buf->bpl,
596                                         buf->v    596                                         buf->vb.height >> 1);
597                         break;                    597                         break;
598                 case V4L2_FIELD_SEQ_TB:           598                 case V4L2_FIELD_SEQ_TB:
599                         cx23885_risc_buffer(de    599                         cx23885_risc_buffer(dev->pci, &buf->risc,
600                                          dma->    600                                          dma->sglist,
601                                          0, bu    601                                          0, buf->bpl * (buf->vb.height >> 1),
602                                          buf->    602                                          buf->bpl, 0,
603                                          buf->    603                                          buf->vb.height >> 1);
604                         break;                    604                         break;
605                 case V4L2_FIELD_SEQ_BT:           605                 case V4L2_FIELD_SEQ_BT:
606                         cx23885_risc_buffer(de    606                         cx23885_risc_buffer(dev->pci, &buf->risc,
607                                          dma->    607                                          dma->sglist,
608                                          buf->    608                                          buf->bpl * (buf->vb.height >> 1), 0,
609                                          buf->    609                                          buf->bpl, 0,
610                                          buf->    610                                          buf->vb.height >> 1);
611                         break;                    611                         break;
612                 default:                          612                 default:
613                         BUG();                    613                         BUG();
614                 }                                 614                 }
615         }                                         615         }
616         dprintk(2, "[%p/%d] buffer_prep - %dx%    616         dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
617                 buf, buf->vb.i,                   617                 buf, buf->vb.i,
618                 fh->width, fh->height, fh->fmt    618                 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
619                 (unsigned long)buf->risc.dma);    619                 (unsigned long)buf->risc.dma);
620                                                   620 
621         buf->vb.state = VIDEOBUF_PREPARED;        621         buf->vb.state = VIDEOBUF_PREPARED;
622         return 0;                                 622         return 0;
623                                                   623 
624  fail:                                            624  fail:
625         cx23885_free_buffer(q, buf);              625         cx23885_free_buffer(q, buf);
626         return rc;                                626         return rc;
627 }                                                 627 }
628                                                   628 
629 static void buffer_queue(struct videobuf_queue    629 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
630 {                                                 630 {
631         struct cx23885_buffer   *buf = contain    631         struct cx23885_buffer   *buf = container_of(vb,
632                 struct cx23885_buffer, vb);       632                 struct cx23885_buffer, vb);
633         struct cx23885_buffer   *prev;            633         struct cx23885_buffer   *prev;
634         struct cx23885_fh       *fh   = vq->pr    634         struct cx23885_fh       *fh   = vq->priv_data;
635         struct cx23885_dev      *dev  = fh->de    635         struct cx23885_dev      *dev  = fh->dev;
636         struct cx23885_dmaqueue *q    = &dev->    636         struct cx23885_dmaqueue *q    = &dev->vidq;
637                                                   637 
638         /* add jump to stopper */                 638         /* add jump to stopper */
639         buf->risc.jmp[0] = cpu_to_le32(RISC_JU    639         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
640         buf->risc.jmp[1] = cpu_to_le32(q->stop    640         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
641         buf->risc.jmp[2] = cpu_to_le32(0); /*     641         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
642                                                   642 
643         if (!list_empty(&q->queued)) {            643         if (!list_empty(&q->queued)) {
644                 list_add_tail(&buf->vb.queue,     644                 list_add_tail(&buf->vb.queue, &q->queued);
645                 buf->vb.state = VIDEOBUF_QUEUE    645                 buf->vb.state = VIDEOBUF_QUEUED;
646                 dprintk(2, "[%p/%d] buffer_que    646                 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
647                         buf, buf->vb.i);          647                         buf, buf->vb.i);
648                                                   648 
649         } else if (list_empty(&q->active)) {      649         } else if (list_empty(&q->active)) {
650                 list_add_tail(&buf->vb.queue,     650                 list_add_tail(&buf->vb.queue, &q->active);
651                 cx23885_start_video_dma(dev, q    651                 cx23885_start_video_dma(dev, q, buf);
652                 buf->vb.state = VIDEOBUF_ACTIV    652                 buf->vb.state = VIDEOBUF_ACTIVE;
653                 buf->count    = q->count++;       653                 buf->count    = q->count++;
654                 mod_timer(&q->timeout, jiffies    654                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
655                 dprintk(2, "[%p/%d] buffer_que    655                 dprintk(2, "[%p/%d] buffer_queue - first active\n",
656                         buf, buf->vb.i);          656                         buf, buf->vb.i);
657                                                   657 
658         } else {                                  658         } else {
659                 prev = list_entry(q->active.pr    659                 prev = list_entry(q->active.prev, struct cx23885_buffer,
660                         vb.queue);                660                         vb.queue);
661                 if (prev->vb.width  == buf->vb    661                 if (prev->vb.width  == buf->vb.width  &&
662                     prev->vb.height == buf->vb    662                     prev->vb.height == buf->vb.height &&
663                     prev->fmt       == buf->fm    663                     prev->fmt       == buf->fmt) {
664                         list_add_tail(&buf->vb    664                         list_add_tail(&buf->vb.queue, &q->active);
665                         buf->vb.state = VIDEOB    665                         buf->vb.state = VIDEOBUF_ACTIVE;
666                         buf->count    = q->cou    666                         buf->count    = q->count++;
667                         prev->risc.jmp[1] = cp    667                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
668                         /* 64 bit bits 63-32 *    668                         /* 64 bit bits 63-32 */
669                         prev->risc.jmp[2] = cp    669                         prev->risc.jmp[2] = cpu_to_le32(0);
670                         dprintk(2, "[%p/%d] bu    670                         dprintk(2, "[%p/%d] buffer_queue - append to active\n",
671                                 buf, buf->vb.i    671                                 buf, buf->vb.i);
672                                                   672 
673                 } else {                          673                 } else {
674                         list_add_tail(&buf->vb    674                         list_add_tail(&buf->vb.queue, &q->queued);
675                         buf->vb.state = VIDEOB    675                         buf->vb.state = VIDEOBUF_QUEUED;
676                         dprintk(2, "[%p/%d] bu    676                         dprintk(2, "[%p/%d] buffer_queue - first queued\n",
677                                 buf, buf->vb.i    677                                 buf, buf->vb.i);
678                 }                                 678                 }
679         }                                         679         }
680 }                                                 680 }
681                                                   681 
682 static void buffer_release(struct videobuf_que    682 static void buffer_release(struct videobuf_queue *q,
683         struct videobuf_buffer *vb)               683         struct videobuf_buffer *vb)
684 {                                                 684 {
685         struct cx23885_buffer *buf = container    685         struct cx23885_buffer *buf = container_of(vb,
686                 struct cx23885_buffer, vb);       686                 struct cx23885_buffer, vb);
687                                                   687 
688         cx23885_free_buffer(q, buf);              688         cx23885_free_buffer(q, buf);
689 }                                                 689 }
690                                                   690 
691 static struct videobuf_queue_ops cx23885_video    691 static struct videobuf_queue_ops cx23885_video_qops = {
692         .buf_setup    = buffer_setup,             692         .buf_setup    = buffer_setup,
693         .buf_prepare  = buffer_prepare,           693         .buf_prepare  = buffer_prepare,
694         .buf_queue    = buffer_queue,             694         .buf_queue    = buffer_queue,
695         .buf_release  = buffer_release,           695         .buf_release  = buffer_release,
696 };                                                696 };
697                                                   697 
698 static struct videobuf_queue *get_queue(struct    698 static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
699 {                                                 699 {
700         switch (fh->type) {                       700         switch (fh->type) {
701         case V4L2_BUF_TYPE_VIDEO_CAPTURE:         701         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
702                 return &fh->vidq;                 702                 return &fh->vidq;
703         case V4L2_BUF_TYPE_VBI_CAPTURE:           703         case V4L2_BUF_TYPE_VBI_CAPTURE:
704                 return &fh->vbiq;                 704                 return &fh->vbiq;
705         default:                                  705         default:
706                 BUG();                            706                 BUG();
707                 return NULL;                      707                 return NULL;
708         }                                         708         }
709 }                                                 709 }
710                                                   710 
711 static int get_resource(struct cx23885_fh *fh)    711 static int get_resource(struct cx23885_fh *fh)
712 {                                                 712 {
713         switch (fh->type) {                       713         switch (fh->type) {
714         case V4L2_BUF_TYPE_VIDEO_CAPTURE:         714         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
715                 return RESOURCE_VIDEO;            715                 return RESOURCE_VIDEO;
716         case V4L2_BUF_TYPE_VBI_CAPTURE:           716         case V4L2_BUF_TYPE_VBI_CAPTURE:
717                 return RESOURCE_VBI;              717                 return RESOURCE_VBI;
718         default:                                  718         default:
719                 BUG();                            719                 BUG();
720                 return 0;                         720                 return 0;
721         }                                         721         }
722 }                                                 722 }
723                                                   723 
724 static int video_open(struct inode *inode, str    724 static int video_open(struct inode *inode, struct file *file)
725 {                                                 725 {
726         int minor = iminor(inode);                726         int minor = iminor(inode);
727         struct cx23885_dev *h, *dev = NULL;       727         struct cx23885_dev *h, *dev = NULL;
728         struct cx23885_fh *fh;                    728         struct cx23885_fh *fh;
729         struct list_head *list;                   729         struct list_head *list;
730         enum v4l2_buf_type type = 0;              730         enum v4l2_buf_type type = 0;
731         int radio = 0;                            731         int radio = 0;
732                                                   732 
733         list_for_each(list, &cx23885_devlist)     733         list_for_each(list, &cx23885_devlist) {
734                 h = list_entry(list, struct cx    734                 h = list_entry(list, struct cx23885_dev, devlist);
735                 if (h->video_dev->minor == min    735                 if (h->video_dev->minor == minor) {
736                         dev  = h;                 736                         dev  = h;
737                         type = V4L2_BUF_TYPE_V    737                         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
738                 }                                 738                 }
739                 if (h->vbi_dev &&                 739                 if (h->vbi_dev &&
740                    h->vbi_dev->minor == minor)    740                    h->vbi_dev->minor == minor) {
741                         dev  = h;                 741                         dev  = h;
742                         type = V4L2_BUF_TYPE_V    742                         type = V4L2_BUF_TYPE_VBI_CAPTURE;
743                 }                                 743                 }
744                 if (h->radio_dev &&               744                 if (h->radio_dev &&
745                     h->radio_dev->minor == min    745                     h->radio_dev->minor == minor) {
746                         radio = 1;                746                         radio = 1;
747                         dev   = h;                747                         dev   = h;
748                 }                                 748                 }
749         }                                         749         }
750         if (NULL == dev)                          750         if (NULL == dev)
751                 return -ENODEV;                   751                 return -ENODEV;
752                                                   752 
753         dprintk(1, "open minor=%d radio=%d typ    753         dprintk(1, "open minor=%d radio=%d type=%s\n",
754                 minor, radio, v4l2_type_names[    754                 minor, radio, v4l2_type_names[type]);
755                                                   755 
756         /* allocate + initialize per filehandl    756         /* allocate + initialize per filehandle data */
757         fh = kzalloc(sizeof(*fh), GFP_KERNEL);    757         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
758         if (NULL == fh)                           758         if (NULL == fh)
759                 return -ENOMEM;                   759                 return -ENOMEM;
760         file->private_data = fh;                  760         file->private_data = fh;
761         fh->dev      = dev;                       761         fh->dev      = dev;
762         fh->radio    = radio;                     762         fh->radio    = radio;
763         fh->type     = type;                      763         fh->type     = type;
764         fh->width    = 320;                       764         fh->width    = 320;
765         fh->height   = 240;                       765         fh->height   = 240;
766         fh->fmt      = format_by_fourcc(V4L2_P    766         fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
767                                                   767 
768         videobuf_queue_pci_init(&fh->vidq, &cx    768         videobuf_queue_pci_init(&fh->vidq, &cx23885_video_qops,
769                             dev->pci, &dev->sl    769                             dev->pci, &dev->slock,
770                             V4L2_BUF_TYPE_VIDE    770                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
771                             V4L2_FIELD_INTERLA    771                             V4L2_FIELD_INTERLACED,
772                             sizeof(struct cx23    772                             sizeof(struct cx23885_buffer),
773                             fh);                  773                             fh);
774                                                   774 
775         dprintk(1, "post videobuf_queue_init()    775         dprintk(1, "post videobuf_queue_init()\n");
776                                                   776 
777                                                   777 
778         return 0;                                 778         return 0;
779 }                                                 779 }
780                                                   780 
781 static ssize_t video_read(struct file *file, c    781 static ssize_t video_read(struct file *file, char __user *data,
782         size_t count, loff_t *ppos)               782         size_t count, loff_t *ppos)
783 {                                                 783 {
784         struct cx23885_fh *fh = file->private_    784         struct cx23885_fh *fh = file->private_data;
785                                                   785 
786         switch (fh->type) {                       786         switch (fh->type) {
787         case V4L2_BUF_TYPE_VIDEO_CAPTURE:         787         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
788                 if (res_locked(fh->dev, RESOUR    788                 if (res_locked(fh->dev, RESOURCE_VIDEO))
789                         return -EBUSY;            789                         return -EBUSY;
790                 return videobuf_read_one(&fh->    790                 return videobuf_read_one(&fh->vidq, data, count, ppos,
791                                          file-    791                                          file->f_flags & O_NONBLOCK);
792         case V4L2_BUF_TYPE_VBI_CAPTURE:           792         case V4L2_BUF_TYPE_VBI_CAPTURE:
793                 if (!res_get(fh->dev, fh, RESO    793                 if (!res_get(fh->dev, fh, RESOURCE_VBI))
794                         return -EBUSY;            794                         return -EBUSY;
795                 return videobuf_read_stream(&f    795                 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
796                                             fi    796                                             file->f_flags & O_NONBLOCK);
797         default:                                  797         default:
798                 BUG();                            798                 BUG();
799                 return 0;                         799                 return 0;
800         }                                         800         }
801 }                                                 801 }
802                                                   802 
803 static unsigned int video_poll(struct file *fi    803 static unsigned int video_poll(struct file *file,
804         struct poll_table_struct *wait)           804         struct poll_table_struct *wait)
805 {                                                 805 {
806         struct cx23885_fh *fh = file->private_    806         struct cx23885_fh *fh = file->private_data;
807         struct cx23885_buffer *buf;               807         struct cx23885_buffer *buf;
808                                                   808 
809         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->t    809         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
810                 if (!res_get(fh->dev, fh, RESO    810                 if (!res_get(fh->dev, fh, RESOURCE_VBI))
811                         return POLLERR;           811                         return POLLERR;
812                 return videobuf_poll_stream(fi    812                 return videobuf_poll_stream(file, &fh->vbiq, wait);
813         }                                         813         }
814                                                   814 
815         if (res_check(fh, RESOURCE_VIDEO)) {      815         if (res_check(fh, RESOURCE_VIDEO)) {
816                 /* streaming capture */           816                 /* streaming capture */
817                 if (list_empty(&fh->vidq.strea    817                 if (list_empty(&fh->vidq.stream))
818                         return POLLERR;           818                         return POLLERR;
819                 buf = list_entry(fh->vidq.stre    819                 buf = list_entry(fh->vidq.stream.next,
820                         struct cx23885_buffer,    820                         struct cx23885_buffer, vb.stream);
821         } else {                                  821         } else {
822                 /* read() capture */              822                 /* read() capture */
823                 buf = (struct cx23885_buffer *    823                 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
824                 if (NULL == buf)                  824                 if (NULL == buf)
825                         return POLLERR;           825                         return POLLERR;
826         }                                         826         }
827         poll_wait(file, &buf->vb.done, wait);     827         poll_wait(file, &buf->vb.done, wait);
828         if (buf->vb.state == VIDEOBUF_DONE ||     828         if (buf->vb.state == VIDEOBUF_DONE ||
829             buf->vb.state == VIDEOBUF_ERROR)      829             buf->vb.state == VIDEOBUF_ERROR)
830                 return POLLIN|POLLRDNORM;         830                 return POLLIN|POLLRDNORM;
831         return 0;                                 831         return 0;
832 }                                                 832 }
833                                                   833 
834 static int video_release(struct inode *inode,     834 static int video_release(struct inode *inode, struct file *file)
835 {                                                 835 {
836         struct cx23885_fh *fh = file->private_    836         struct cx23885_fh *fh = file->private_data;
837         struct cx23885_dev *dev = fh->dev;        837         struct cx23885_dev *dev = fh->dev;
838                                                   838 
839         /* turn off overlay */                    839         /* turn off overlay */
840         if (res_check(fh, RESOURCE_OVERLAY)) {    840         if (res_check(fh, RESOURCE_OVERLAY)) {
841                 /* FIXME */                       841                 /* FIXME */
842                 res_free(dev, fh, RESOURCE_OVE    842                 res_free(dev, fh, RESOURCE_OVERLAY);
843         }                                         843         }
844                                                   844 
845         /* stop video capture */                  845         /* stop video capture */
846         if (res_check(fh, RESOURCE_VIDEO)) {      846         if (res_check(fh, RESOURCE_VIDEO)) {
847                 videobuf_queue_cancel(&fh->vid    847                 videobuf_queue_cancel(&fh->vidq);
848                 res_free(dev, fh, RESOURCE_VID    848                 res_free(dev, fh, RESOURCE_VIDEO);
849         }                                         849         }
850         if (fh->vidq.read_buf) {                  850         if (fh->vidq.read_buf) {
851                 buffer_release(&fh->vidq, fh->    851                 buffer_release(&fh->vidq, fh->vidq.read_buf);
852                 kfree(fh->vidq.read_buf);         852                 kfree(fh->vidq.read_buf);
853         }                                         853         }
854                                                   854 
855         /* stop vbi capture */                    855         /* stop vbi capture */
856         if (res_check(fh, RESOURCE_VBI)) {        856         if (res_check(fh, RESOURCE_VBI)) {
857                 if (fh->vbiq.streaming)           857                 if (fh->vbiq.streaming)
858                         videobuf_streamoff(&fh    858                         videobuf_streamoff(&fh->vbiq);
859                 if (fh->vbiq.reading)             859                 if (fh->vbiq.reading)
860                         videobuf_read_stop(&fh    860                         videobuf_read_stop(&fh->vbiq);
861                 res_free(dev, fh, RESOURCE_VBI    861                 res_free(dev, fh, RESOURCE_VBI);
862         }                                         862         }
863                                                   863 
864         videobuf_mmap_free(&fh->vidq);            864         videobuf_mmap_free(&fh->vidq);
865         file->private_data = NULL;                865         file->private_data = NULL;
866         kfree(fh);                                866         kfree(fh);
867                                                   867 
868         /* We are not putting the tuner to sle    868         /* We are not putting the tuner to sleep here on exit, because
869          * we want to use the mpeg encoder in     869          * we want to use the mpeg encoder in another session to capture
870          * tuner video. Closing this will resu    870          * tuner video. Closing this will result in no video to the encoder.
871          */                                       871          */
872                                                   872 
873         return 0;                                 873         return 0;
874 }                                                 874 }
875                                                   875 
876 static int video_mmap(struct file *file, struc    876 static int video_mmap(struct file *file, struct vm_area_struct *vma)
877 {                                                 877 {
878         struct cx23885_fh *fh = file->private_    878         struct cx23885_fh *fh = file->private_data;
879                                                   879 
880         return videobuf_mmap_mapper(get_queue(    880         return videobuf_mmap_mapper(get_queue(fh), vma);
881 }                                                 881 }
882                                                   882 
883 /* -------------------------------------------    883 /* ------------------------------------------------------------------ */
884 /* VIDEO CTRL IOCTLS                              884 /* VIDEO CTRL IOCTLS                                                  */
885                                                   885 
886 int cx23885_get_control(struct cx23885_dev *de    886 int cx23885_get_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
887 {                                                 887 {
888         dprintk(1, "%s() calling cx25840(VIDIO    888         dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __FUNCTION__);
889         cx23885_call_i2c_clients(&dev->i2c_bus    889         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
890         return 0;                                 890         return 0;
891 }                                                 891 }
892 EXPORT_SYMBOL(cx23885_get_control);               892 EXPORT_SYMBOL(cx23885_get_control);
893                                                   893 
894 int cx23885_set_control(struct cx23885_dev *de    894 int cx23885_set_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
895 {                                                 895 {
896         dprintk(1, "%s() calling cx25840(VIDIO    896         dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
897                 " (disabled - no action)\n", _    897                 " (disabled - no action)\n", __FUNCTION__);
898         return 0;                                 898         return 0;
899 }                                                 899 }
900 EXPORT_SYMBOL(cx23885_set_control);               900 EXPORT_SYMBOL(cx23885_set_control);
901                                                   901 
902 static void init_controls(struct cx23885_dev *    902 static void init_controls(struct cx23885_dev *dev)
903 {                                                 903 {
904         struct v4l2_control ctrl;                 904         struct v4l2_control ctrl;
905         int i;                                    905         int i;
906                                                   906 
907         for (i = 0; i < CX23885_CTLS; i++) {      907         for (i = 0; i < CX23885_CTLS; i++) {
908                 ctrl.id = cx23885_ctls[i].v.id    908                 ctrl.id = cx23885_ctls[i].v.id;
909                 ctrl.value = cx23885_ctls[i].v    909                 ctrl.value = cx23885_ctls[i].v.default_value;
910                                                   910 
911                 cx23885_set_control(dev, &ctrl    911                 cx23885_set_control(dev, &ctrl);
912         }                                         912         }
913 }                                                 913 }
914                                                   914 
915 /* -------------------------------------------    915 /* ------------------------------------------------------------------ */
916 /* VIDEO IOCTLS                                   916 /* VIDEO IOCTLS                                                       */
917                                                   917 
918 static int vidioc_g_fmt_cap(struct file *file,    918 static int vidioc_g_fmt_cap(struct file *file, void *priv,
919         struct v4l2_format *f)                    919         struct v4l2_format *f)
920 {                                                 920 {
921         struct cx23885_fh *fh   = priv;           921         struct cx23885_fh *fh   = priv;
922                                                   922 
923         f->fmt.pix.width        = fh->width;      923         f->fmt.pix.width        = fh->width;
924         f->fmt.pix.height       = fh->height;     924         f->fmt.pix.height       = fh->height;
925         f->fmt.pix.field        = fh->vidq.fie    925         f->fmt.pix.field        = fh->vidq.field;
926         f->fmt.pix.pixelformat  = fh->fmt->fou    926         f->fmt.pix.pixelformat  = fh->fmt->fourcc;
927         f->fmt.pix.bytesperline =                 927         f->fmt.pix.bytesperline =
928                 (f->fmt.pix.width * fh->fmt->d    928                 (f->fmt.pix.width * fh->fmt->depth) >> 3;
929         f->fmt.pix.sizeimage =                    929         f->fmt.pix.sizeimage =
930                 f->fmt.pix.height * f->fmt.pix    930                 f->fmt.pix.height * f->fmt.pix.bytesperline;
931                                                   931 
932         return 0;                                 932         return 0;
933 }                                                 933 }
934                                                   934 
935 static int vidioc_try_fmt_cap(struct file *fil    935 static int vidioc_try_fmt_cap(struct file *file, void *priv,
936         struct v4l2_format *f)                    936         struct v4l2_format *f)
937 {                                                 937 {
938         struct cx23885_dev *dev = ((struct cx2    938         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
939         struct cx23885_fmt *fmt;                  939         struct cx23885_fmt *fmt;
940         enum v4l2_field   field;                  940         enum v4l2_field   field;
941         unsigned int      maxw, maxh;             941         unsigned int      maxw, maxh;
942                                                   942 
943         fmt = format_by_fourcc(f->fmt.pix.pixe    943         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
944         if (NULL == fmt)                          944         if (NULL == fmt)
945                 return -EINVAL;                   945                 return -EINVAL;
946                                                   946 
947         field = f->fmt.pix.field;                 947         field = f->fmt.pix.field;
948         maxw  = norm_maxw(dev->tvnorm);           948         maxw  = norm_maxw(dev->tvnorm);
949         maxh  = norm_maxh(dev->tvnorm);           949         maxh  = norm_maxh(dev->tvnorm);
950                                                   950 
951         if (V4L2_FIELD_ANY == field) {            951         if (V4L2_FIELD_ANY == field) {
952                 field = (f->fmt.pix.height > m    952                 field = (f->fmt.pix.height > maxh/2)
953                         ? V4L2_FIELD_INTERLACE    953                         ? V4L2_FIELD_INTERLACED
954                         : V4L2_FIELD_BOTTOM;      954                         : V4L2_FIELD_BOTTOM;
955         }                                         955         }
956                                                   956 
957         switch (field) {                          957         switch (field) {
958         case V4L2_FIELD_TOP:                      958         case V4L2_FIELD_TOP:
959         case V4L2_FIELD_BOTTOM:                   959         case V4L2_FIELD_BOTTOM:
960                 maxh = maxh / 2;                  960                 maxh = maxh / 2;
961                 break;                            961                 break;
962         case V4L2_FIELD_INTERLACED:               962         case V4L2_FIELD_INTERLACED:
963                 break;                            963                 break;
964         default:                                  964         default:
965                 return -EINVAL;                   965                 return -EINVAL;
966         }                                         966         }
967                                                   967 
968         f->fmt.pix.field = field;                 968         f->fmt.pix.field = field;
969         if (f->fmt.pix.height < 32)               969         if (f->fmt.pix.height < 32)
970                 f->fmt.pix.height = 32;           970                 f->fmt.pix.height = 32;
971         if (f->fmt.pix.height > maxh)             971         if (f->fmt.pix.height > maxh)
972                 f->fmt.pix.height = maxh;         972                 f->fmt.pix.height = maxh;
973         if (f->fmt.pix.width < 48)                973         if (f->fmt.pix.width < 48)
974                 f->fmt.pix.width = 48;            974                 f->fmt.pix.width = 48;
975         if (f->fmt.pix.width > maxw)              975         if (f->fmt.pix.width > maxw)
976                 f->fmt.pix.width = maxw;          976                 f->fmt.pix.width = maxw;
977         f->fmt.pix.width &= ~0x03;                977         f->fmt.pix.width &= ~0x03;
978         f->fmt.pix.bytesperline =                 978         f->fmt.pix.bytesperline =
979                 (f->fmt.pix.width * fmt->depth    979                 (f->fmt.pix.width * fmt->depth) >> 3;
980         f->fmt.pix.sizeimage =                    980         f->fmt.pix.sizeimage =
981                 f->fmt.pix.height * f->fmt.pix    981                 f->fmt.pix.height * f->fmt.pix.bytesperline;
982                                                   982 
983         return 0;                                 983         return 0;
984 }                                                 984 }
985                                                   985 
986 static int vidioc_s_fmt_cap(struct file *file,    986 static int vidioc_s_fmt_cap(struct file *file, void *priv,
987         struct v4l2_format *f)                    987         struct v4l2_format *f)
988 {                                                 988 {
989         struct cx23885_fh *fh = priv;             989         struct cx23885_fh *fh = priv;
990         struct cx23885_dev *dev  = ((struct cx    990         struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
991         int err;                                  991         int err;
992                                                   992 
993         dprintk(2, "%s()\n", __FUNCTION__);       993         dprintk(2, "%s()\n", __FUNCTION__);
994         err = vidioc_try_fmt_cap(file, priv, f    994         err = vidioc_try_fmt_cap(file, priv, f);
995                                                   995 
996         if (0 != err)                             996         if (0 != err)
997                 return err;                       997                 return err;
998         fh->fmt        = format_by_fourcc(f->f    998         fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
999         fh->width      = f->fmt.pix.width;        999         fh->width      = f->fmt.pix.width;
1000         fh->height     = f->fmt.pix.height;      1000         fh->height     = f->fmt.pix.height;
1001         fh->vidq.field = f->fmt.pix.field;       1001         fh->vidq.field = f->fmt.pix.field;
1002         dprintk(2, "%s() width=%d height=%d f    1002         dprintk(2, "%s() width=%d height=%d field=%d\n", __FUNCTION__,
1003                 fh->width, fh->height, fh->vi    1003                 fh->width, fh->height, fh->vidq.field);
1004         cx23885_call_i2c_clients(&dev->i2c_bu    1004         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
1005         return 0;                                1005         return 0;
1006 }                                                1006 }
1007                                                  1007 
1008 static int vidioc_querycap(struct file *file,    1008 static int vidioc_querycap(struct file *file, void  *priv,
1009         struct v4l2_capability *cap)             1009         struct v4l2_capability *cap)
1010 {                                                1010 {
1011         struct cx23885_dev *dev  = ((struct c    1011         struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1012                                                  1012 
1013         strcpy(cap->driver, "cx23885");          1013         strcpy(cap->driver, "cx23885");
1014         strlcpy(cap->card, cx23885_boards[dev    1014         strlcpy(cap->card, cx23885_boards[dev->board].name,
1015                 sizeof(cap->card));              1015                 sizeof(cap->card));
1016         sprintf(cap->bus_info, "PCIe:%s", pci    1016         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1017         cap->version = CX23885_VERSION_CODE;     1017         cap->version = CX23885_VERSION_CODE;
1018         cap->capabilities =                      1018         cap->capabilities =
1019                 V4L2_CAP_VIDEO_CAPTURE |         1019                 V4L2_CAP_VIDEO_CAPTURE |
1020                 V4L2_CAP_READWRITE     |         1020                 V4L2_CAP_READWRITE     |
1021                 V4L2_CAP_STREAMING     |         1021                 V4L2_CAP_STREAMING     |
1022                 V4L2_CAP_VBI_CAPTURE;            1022                 V4L2_CAP_VBI_CAPTURE;
1023         if (UNSET != dev->tuner_type)            1023         if (UNSET != dev->tuner_type)
1024                 cap->capabilities |= V4L2_CAP    1024                 cap->capabilities |= V4L2_CAP_TUNER;
1025         return 0;                                1025         return 0;
1026 }                                                1026 }
1027                                                  1027 
1028 static int vidioc_enum_fmt_cap(struct file *f    1028 static int vidioc_enum_fmt_cap(struct file *file, void  *priv,
1029         struct v4l2_fmtdesc *f)                  1029         struct v4l2_fmtdesc *f)
1030 {                                                1030 {
1031         if (unlikely(f->index >= ARRAY_SIZE(f    1031         if (unlikely(f->index >= ARRAY_SIZE(formats)))
1032                 return -EINVAL;                  1032                 return -EINVAL;
1033                                                  1033 
1034         strlcpy(f->description, formats[f->in    1034         strlcpy(f->description, formats[f->index].name,
1035                 sizeof(f->description));         1035                 sizeof(f->description));
1036         f->pixelformat = formats[f->index].fo    1036         f->pixelformat = formats[f->index].fourcc;
1037                                                  1037 
1038         return 0;                                1038         return 0;
1039 }                                                1039 }
1040                                                  1040 
1041 #ifdef CONFIG_VIDEO_V4L1_COMPAT                  1041 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1042 static int vidiocgmbuf(struct file *file, voi    1042 static int vidiocgmbuf(struct file *file, void *priv,
1043         struct video_mbuf *mbuf)                 1043         struct video_mbuf *mbuf)
1044 {                                                1044 {
1045         struct cx23885_fh *fh = priv;            1045         struct cx23885_fh *fh = priv;
1046         struct videobuf_queue *q;                1046         struct videobuf_queue *q;
1047         struct v4l2_requestbuffers req;          1047         struct v4l2_requestbuffers req;
1048         unsigned int i;                          1048         unsigned int i;
1049         int err;                                 1049         int err;
1050                                                  1050 
1051         q = get_queue(fh);                       1051         q = get_queue(fh);
1052         memset(&req, 0, sizeof(req));            1052         memset(&req, 0, sizeof(req));
1053         req.type   = q->type;                    1053         req.type   = q->type;
1054         req.count  = 8;                          1054         req.count  = 8;
1055         req.memory = V4L2_MEMORY_MMAP;           1055         req.memory = V4L2_MEMORY_MMAP;
1056         err = videobuf_reqbufs(q, &req);         1056         err = videobuf_reqbufs(q, &req);
1057         if (err < 0)                             1057         if (err < 0)
1058                 return err;                      1058                 return err;
1059                                                  1059 
1060         mbuf->frames = req.count;                1060         mbuf->frames = req.count;
1061         mbuf->size   = 0;                        1061         mbuf->size   = 0;
1062         for (i = 0; i < mbuf->frames; i++) {     1062         for (i = 0; i < mbuf->frames; i++) {
1063                 mbuf->offsets[i]  = q->bufs[i    1063                 mbuf->offsets[i]  = q->bufs[i]->boff;
1064                 mbuf->size       += q->bufs[i    1064                 mbuf->size       += q->bufs[i]->bsize;
1065         }                                        1065         }
1066         return 0;                                1066         return 0;
1067 }                                                1067 }
1068 #endif                                           1068 #endif
1069                                                  1069 
1070 static int vidioc_reqbufs(struct file *file,     1070 static int vidioc_reqbufs(struct file *file, void *priv,
1071         struct v4l2_requestbuffers *p)           1071         struct v4l2_requestbuffers *p)
1072 {                                                1072 {
1073         struct cx23885_fh *fh = priv;            1073         struct cx23885_fh *fh = priv;
1074         return (videobuf_reqbufs(get_queue(fh    1074         return (videobuf_reqbufs(get_queue(fh), p));
1075 }                                                1075 }
1076                                                  1076 
1077 static int vidioc_querybuf(struct file *file,    1077 static int vidioc_querybuf(struct file *file, void *priv,
1078         struct v4l2_buffer *p)                   1078         struct v4l2_buffer *p)
1079 {                                                1079 {
1080         struct cx23885_fh *fh = priv;            1080         struct cx23885_fh *fh = priv;
1081         return (videobuf_querybuf(get_queue(f    1081         return (videobuf_querybuf(get_queue(fh), p));
1082 }                                                1082 }
1083                                                  1083 
1084 static int vidioc_qbuf(struct file *file, voi    1084 static int vidioc_qbuf(struct file *file, void *priv,
1085         struct v4l2_buffer *p)                   1085         struct v4l2_buffer *p)
1086 {                                                1086 {
1087         struct cx23885_fh *fh = priv;            1087         struct cx23885_fh *fh = priv;
1088         return (videobuf_qbuf(get_queue(fh),     1088         return (videobuf_qbuf(get_queue(fh), p));
1089 }                                                1089 }
1090                                                  1090 
1091 static int vidioc_dqbuf(struct file *file, vo    1091 static int vidioc_dqbuf(struct file *file, void *priv,
1092         struct v4l2_buffer *p)                   1092         struct v4l2_buffer *p)
1093 {                                                1093 {
1094         struct cx23885_fh *fh = priv;            1094         struct cx23885_fh *fh = priv;
1095         return (videobuf_dqbuf(get_queue(fh),    1095         return (videobuf_dqbuf(get_queue(fh), p,
1096                                 file->f_flags    1096                                 file->f_flags & O_NONBLOCK));
1097 }                                                1097 }
1098                                                  1098 
1099 static int vidioc_streamon(struct file *file,    1099 static int vidioc_streamon(struct file *file, void *priv,
1100         enum v4l2_buf_type i)                    1100         enum v4l2_buf_type i)
1101 {                                                1101 {
1102         struct cx23885_fh *fh = priv;            1102         struct cx23885_fh *fh = priv;
1103         struct cx23885_dev *dev = fh->dev;       1103         struct cx23885_dev *dev = fh->dev;
1104         dprintk(1, "%s()\n", __FUNCTION__);      1104         dprintk(1, "%s()\n", __FUNCTION__);
1105                                                  1105 
1106         if (unlikely(fh->type != V4L2_BUF_TYP    1106         if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1107                 return -EINVAL;                  1107                 return -EINVAL;
1108         if (unlikely(i != fh->type))             1108         if (unlikely(i != fh->type))
1109                 return -EINVAL;                  1109                 return -EINVAL;
1110                                                  1110 
1111         if (unlikely(!res_get(dev, fh, get_re    1111         if (unlikely(!res_get(dev, fh, get_resource(fh))))
1112                 return -EBUSY;                   1112                 return -EBUSY;
1113         return videobuf_streamon(get_queue(fh    1113         return videobuf_streamon(get_queue(fh));
1114 }                                                1114 }
1115                                                  1115 
1116 static int vidioc_streamoff(struct file *file    1116 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1117 {                                                1117 {
1118         struct cx23885_fh *fh = priv;            1118         struct cx23885_fh *fh = priv;
1119         struct cx23885_dev *dev = fh->dev;       1119         struct cx23885_dev *dev = fh->dev;
1120         int err, res;                            1120         int err, res;
1121         dprintk(1, "%s()\n", __FUNCTION__);      1121         dprintk(1, "%s()\n", __FUNCTION__);
1122                                                  1122 
1123         if (fh->type != V4L2_BUF_TYPE_VIDEO_C    1123         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1124                 return -EINVAL;                  1124                 return -EINVAL;
1125         if (i != fh->type)                       1125         if (i != fh->type)
1126                 return -EINVAL;                  1126                 return -EINVAL;
1127                                                  1127 
1128         res = get_resource(fh);                  1128         res = get_resource(fh);
1129         err = videobuf_streamoff(get_queue(fh    1129         err = videobuf_streamoff(get_queue(fh));
1130         if (err < 0)                             1130         if (err < 0)
1131                 return err;                      1131                 return err;
1132         res_free(dev, fh, res);                  1132         res_free(dev, fh, res);
1133         return 0;                                1133         return 0;
1134 }                                                1134 }
1135                                                  1135 
1136 static int vidioc_s_std(struct file *file, vo    1136 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1137 {                                                1137 {
1138         struct cx23885_dev *dev = ((struct cx    1138         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1139         dprintk(1, "%s()\n", __FUNCTION__);      1139         dprintk(1, "%s()\n", __FUNCTION__);
1140                                                  1140 
1141         mutex_lock(&dev->lock);                  1141         mutex_lock(&dev->lock);
1142         cx23885_set_tvnorm(dev, *tvnorms);       1142         cx23885_set_tvnorm(dev, *tvnorms);
1143         mutex_unlock(&dev->lock);                1143         mutex_unlock(&dev->lock);
1144                                                  1144 
1145         return 0;                                1145         return 0;
1146 }                                                1146 }
1147                                                  1147 
1148 int cx23885_enum_input(struct cx23885_dev *de    1148 int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1149 {                                                1149 {
1150         static const char *iname[] = {           1150         static const char *iname[] = {
1151                 [CX23885_VMUX_COMPOSITE1] = "    1151                 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1152                 [CX23885_VMUX_COMPOSITE2] = "    1152                 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1153                 [CX23885_VMUX_COMPOSITE3] = "    1153                 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1154                 [CX23885_VMUX_COMPOSITE4] = "    1154                 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1155                 [CX23885_VMUX_SVIDEO]     = "    1155                 [CX23885_VMUX_SVIDEO]     = "S-Video",
1156                 [CX23885_VMUX_TELEVISION] = "    1156                 [CX23885_VMUX_TELEVISION] = "Television",
1157                 [CX23885_VMUX_CABLE]      = "    1157                 [CX23885_VMUX_CABLE]      = "Cable TV",
1158                 [CX23885_VMUX_DVB]        = "    1158                 [CX23885_VMUX_DVB]        = "DVB",
1159                 [CX23885_VMUX_DEBUG]      = "    1159                 [CX23885_VMUX_DEBUG]      = "for debug only",
1160         };                                       1160         };
1161         unsigned int n;                          1161         unsigned int n;
1162         dprintk(1, "%s()\n", __FUNCTION__);      1162         dprintk(1, "%s()\n", __FUNCTION__);
1163                                                  1163 
1164         n = i->index;                            1164         n = i->index;
1165         if (n >= 4)                              1165         if (n >= 4)
1166                 return -EINVAL;                  1166                 return -EINVAL;
1167                                                  1167 
1168         if (0 == INPUT(n)->type)                 1168         if (0 == INPUT(n)->type)
1169                 return -EINVAL;                  1169                 return -EINVAL;
1170                                                  1170 
1171         memset(i, 0, sizeof(*i));                1171         memset(i, 0, sizeof(*i));
1172         i->index = n;                            1172         i->index = n;
1173         i->type  = V4L2_INPUT_TYPE_CAMERA;       1173         i->type  = V4L2_INPUT_TYPE_CAMERA;
1174         strcpy(i->name, iname[INPUT(n)->type]    1174         strcpy(i->name, iname[INPUT(n)->type]);
1175         if ((CX23885_VMUX_TELEVISION == INPUT    1175         if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1176                 (CX23885_VMUX_CABLE == INPUT(    1176                 (CX23885_VMUX_CABLE == INPUT(n)->type))
1177                 i->type = V4L2_INPUT_TYPE_TUN    1177                 i->type = V4L2_INPUT_TYPE_TUNER;
1178                 i->std = CX23885_NORMS;          1178                 i->std = CX23885_NORMS;
1179         return 0;                                1179         return 0;
1180 }                                                1180 }
1181 EXPORT_SYMBOL(cx23885_enum_input);               1181 EXPORT_SYMBOL(cx23885_enum_input);
1182                                                  1182 
1183 static int vidioc_enum_input(struct file *fil    1183 static int vidioc_enum_input(struct file *file, void *priv,
1184                                 struct v4l2_i    1184                                 struct v4l2_input *i)
1185 {                                                1185 {
1186         struct cx23885_dev *dev = ((struct cx    1186         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1187         dprintk(1, "%s()\n", __FUNCTION__);      1187         dprintk(1, "%s()\n", __FUNCTION__);
1188         return cx23885_enum_input(dev, i);       1188         return cx23885_enum_input(dev, i);
1189 }                                                1189 }
1190                                                  1190 
1191 static int vidioc_g_input(struct file *file,     1191 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1192 {                                                1192 {
1193         struct cx23885_dev *dev = ((struct cx    1193         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1194                                                  1194 
1195         *i = dev->input;                         1195         *i = dev->input;
1196         dprintk(1, "%s() returns %d\n", __FUN    1196         dprintk(1, "%s() returns %d\n", __FUNCTION__, *i);
1197         return 0;                                1197         return 0;
1198 }                                                1198 }
1199                                                  1199 
1200 static int vidioc_s_input(struct file *file,     1200 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1201 {                                                1201 {
1202         struct cx23885_dev *dev = ((struct cx    1202         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1203                                                  1203 
1204         dprintk(1, "%s(%d)\n", __FUNCTION__,     1204         dprintk(1, "%s(%d)\n", __FUNCTION__, i);
1205                                                  1205 
1206         if (i >= 4) {                            1206         if (i >= 4) {
1207                 dprintk(1, "%s() -EINVAL\n",     1207                 dprintk(1, "%s() -EINVAL\n", __FUNCTION__);
1208                 return -EINVAL;                  1208                 return -EINVAL;
1209         }                                        1209         }
1210                                                  1210 
1211         mutex_lock(&dev->lock);                  1211         mutex_lock(&dev->lock);
1212         cx23885_video_mux(dev, i);               1212         cx23885_video_mux(dev, i);
1213         mutex_unlock(&dev->lock);                1213         mutex_unlock(&dev->lock);
1214         return 0;                                1214         return 0;
1215 }                                                1215 }
1216                                                  1216 
1217 static int vidioc_queryctrl(struct file *file    1217 static int vidioc_queryctrl(struct file *file, void *priv,
1218                                 struct v4l2_q    1218                                 struct v4l2_queryctrl *qctrl)
1219 {                                                1219 {
1220         qctrl->id = v4l2_ctrl_next(ctrl_class    1220         qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1221         if (unlikely(qctrl->id == 0))            1221         if (unlikely(qctrl->id == 0))
1222                 return -EINVAL;                  1222                 return -EINVAL;
1223         return cx23885_ctrl_query(qctrl);        1223         return cx23885_ctrl_query(qctrl);
1224 }                                                1224 }
1225                                                  1225 
1226 static int vidioc_g_ctrl(struct file *file, v    1226 static int vidioc_g_ctrl(struct file *file, void *priv,
1227                                 struct v4l2_c    1227                                 struct v4l2_control *ctl)
1228 {                                                1228 {
1229         struct cx23885_dev *dev = ((struct cx    1229         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1230                                                  1230 
1231         return cx23885_get_control(dev, ctl);    1231         return cx23885_get_control(dev, ctl);
1232 }                                                1232 }
1233                                                  1233 
1234 static int vidioc_s_ctrl(struct file *file, v    1234 static int vidioc_s_ctrl(struct file *file, void *priv,
1235                                 struct v4l2_c    1235                                 struct v4l2_control *ctl)
1236 {                                                1236 {
1237         struct cx23885_dev *dev = ((struct cx    1237         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1238                                                  1238 
1239         return cx23885_set_control(dev, ctl);    1239         return cx23885_set_control(dev, ctl);
1240 }                                                1240 }
1241                                                  1241 
1242 static int vidioc_g_tuner(struct file *file,     1242 static int vidioc_g_tuner(struct file *file, void *priv,
1243                                 struct v4l2_t    1243                                 struct v4l2_tuner *t)
1244 {                                                1244 {
1245         struct cx23885_dev *dev = ((struct cx    1245         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1246                                                  1246 
1247         if (unlikely(UNSET == dev->tuner_type    1247         if (unlikely(UNSET == dev->tuner_type))
1248                 return -EINVAL;                  1248                 return -EINVAL;
1249         if (0 != t->index)                       1249         if (0 != t->index)
1250                 return -EINVAL;                  1250                 return -EINVAL;
1251                                                  1251 
1252         strcpy(t->name, "Television");           1252         strcpy(t->name, "Television");
1253         t->type       = V4L2_TUNER_ANALOG_TV;    1253         t->type       = V4L2_TUNER_ANALOG_TV;
1254         t->capability = V4L2_TUNER_CAP_NORM;     1254         t->capability = V4L2_TUNER_CAP_NORM;
1255         t->rangehigh  = 0xffffffffUL;            1255         t->rangehigh  = 0xffffffffUL;
1256         t->signal = 0xffff ; /* LOCKED */        1256         t->signal = 0xffff ; /* LOCKED */
1257         return 0;                                1257         return 0;
1258 }                                                1258 }
1259                                                  1259 
1260 static int vidioc_s_tuner(struct file *file,     1260 static int vidioc_s_tuner(struct file *file, void *priv,
1261                                 struct v4l2_t    1261                                 struct v4l2_tuner *t)
1262 {                                                1262 {
1263         struct cx23885_dev *dev = ((struct cx    1263         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1264                                                  1264 
1265         if (UNSET == dev->tuner_type)            1265         if (UNSET == dev->tuner_type)
1266                 return -EINVAL;                  1266                 return -EINVAL;
1267         if (0 != t->index)                       1267         if (0 != t->index)
1268                 return -EINVAL;                  1268                 return -EINVAL;
1269         return 0;                                1269         return 0;
1270 }                                                1270 }
1271                                                  1271 
1272 static int vidioc_g_frequency(struct file *fi    1272 static int vidioc_g_frequency(struct file *file, void *priv,
1273                                 struct v4l2_f    1273                                 struct v4l2_frequency *f)
1274 {                                                1274 {
1275         struct cx23885_fh *fh = priv;            1275         struct cx23885_fh *fh = priv;
1276         struct cx23885_dev *dev = fh->dev;       1276         struct cx23885_dev *dev = fh->dev;
1277                                                  1277 
1278         if (unlikely(UNSET == dev->tuner_type    1278         if (unlikely(UNSET == dev->tuner_type))
1279                 return -EINVAL;                  1279                 return -EINVAL;
1280                                                  1280 
1281         /* f->type = fh->radio ? V4L2_TUNER_R    1281         /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1282         f->type = fh->radio ? V4L2_TUNER_RADI    1282         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1283         f->frequency = dev->freq;                1283         f->frequency = dev->freq;
1284                                                  1284 
1285         cx23885_call_i2c_clients(&dev->i2c_bu    1285         cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1286                                                  1286 
1287         return 0;                                1287         return 0;
1288 }                                                1288 }
1289                                                  1289 
1290 int cx23885_set_freq(struct cx23885_dev *dev,    1290 int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1291 {                                                1291 {
1292         if (unlikely(UNSET == dev->tuner_type    1292         if (unlikely(UNSET == dev->tuner_type))
1293                 return -EINVAL;                  1293                 return -EINVAL;
1294         if (unlikely(f->tuner != 0))             1294         if (unlikely(f->tuner != 0))
1295                 return -EINVAL;                  1295                 return -EINVAL;
1296                                                  1296 
1297         mutex_lock(&dev->lock);                  1297         mutex_lock(&dev->lock);
1298         dev->freq = f->frequency;                1298         dev->freq = f->frequency;
1299                                                  1299 
1300         cx23885_call_i2c_clients(&dev->i2c_bu    1300         cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1301                                                  1301 
1302         /* When changing channels it is requi    1302         /* When changing channels it is required to reset TVAUDIO */
1303         msleep(10);                              1303         msleep(10);
1304                                                  1304 
1305         mutex_unlock(&dev->lock);                1305         mutex_unlock(&dev->lock);
1306                                                  1306 
1307         return 0;                                1307         return 0;
1308 }                                                1308 }
1309 EXPORT_SYMBOL(cx23885_set_freq);                 1309 EXPORT_SYMBOL(cx23885_set_freq);
1310                                                  1310 
1311 static int vidioc_s_frequency(struct file *fi    1311 static int vidioc_s_frequency(struct file *file, void *priv,
1312                                 struct v4l2_f    1312                                 struct v4l2_frequency *f)
1313 {                                                1313 {
1314         struct cx23885_fh *fh = priv;            1314         struct cx23885_fh *fh = priv;
1315         struct cx23885_dev *dev = fh->dev;       1315         struct cx23885_dev *dev = fh->dev;
1316                                                  1316 
1317         if (unlikely(0 == fh->radio && f->typ    1317         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1318                 return -EINVAL;                  1318                 return -EINVAL;
1319         if (unlikely(1 == fh->radio && f->typ    1319         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1320                 return -EINVAL;                  1320                 return -EINVAL;
1321                                                  1321 
1322         return                                   1322         return
1323                 cx23885_set_freq(dev, f);        1323                 cx23885_set_freq(dev, f);
1324 }                                                1324 }
1325                                                  1325 
1326 #ifdef CONFIG_VIDEO_ADV_DEBUG                    1326 #ifdef CONFIG_VIDEO_ADV_DEBUG
1327 static int vidioc_g_register(struct file *fil    1327 static int vidioc_g_register(struct file *file, void *fh,
1328                                 struct v4l2_r    1328                                 struct v4l2_register *reg)
1329 {                                                1329 {
1330         struct cx23885_dev *dev = ((struct cx    1330         struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1331                                                  1331 
1332         if (!v4l2_chip_match_host(reg->match_    1332         if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1333                 return -EINVAL;                  1333                 return -EINVAL;
1334                                                  1334 
1335         cx23885_call_i2c_clients(&dev->i2c_bu    1335         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
1336                                                  1336 
1337         return 0;                                1337         return 0;
1338 }                                                1338 }
1339                                                  1339 
1340 static int vidioc_s_register(struct file *fil    1340 static int vidioc_s_register(struct file *file, void *fh,
1341                                 struct v4l2_r    1341                                 struct v4l2_register *reg)
1342 {                                                1342 {
1343         struct cx23885_dev *dev = ((struct cx    1343         struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1344                                                  1344 
1345         if (!v4l2_chip_match_host(reg->match_    1345         if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1346                 return -EINVAL;                  1346                 return -EINVAL;
1347                                                  1347 
1348         cx23885_call_i2c_clients(&dev->i2c_bu    1348         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
1349                                                  1349 
1350         return 0;                                1350         return 0;
1351 }                                                1351 }
1352 #endif                                           1352 #endif
1353                                                  1353 
1354 /* ------------------------------------------    1354 /* ----------------------------------------------------------- */
1355                                                  1355 
1356 static void cx23885_vid_timeout(unsigned long    1356 static void cx23885_vid_timeout(unsigned long data)
1357 {                                                1357 {
1358         struct cx23885_dev *dev = (struct cx2    1358         struct cx23885_dev *dev = (struct cx23885_dev *)data;
1359         struct cx23885_dmaqueue *q = &dev->vi    1359         struct cx23885_dmaqueue *q = &dev->vidq;
1360         struct cx23885_buffer *buf;              1360         struct cx23885_buffer *buf;
1361         unsigned long flags;                     1361         unsigned long flags;
1362                                                  1362 
1363         cx23885_sram_channel_dump(dev, &dev->    1363         cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1364                                                  1364 
1365         cx_clear(VID_A_DMA_CTL, 0x11);           1365         cx_clear(VID_A_DMA_CTL, 0x11);
1366                                                  1366 
1367         spin_lock_irqsave(&dev->slock, flags)    1367         spin_lock_irqsave(&dev->slock, flags);
1368         while (!list_empty(&q->active)) {        1368         while (!list_empty(&q->active)) {
1369                 buf = list_entry(q->active.ne    1369                 buf = list_entry(q->active.next,
1370                         struct cx23885_buffer    1370                         struct cx23885_buffer, vb.queue);
1371                 list_del(&buf->vb.queue);        1371                 list_del(&buf->vb.queue);
1372                 buf->vb.state = VIDEOBUF_ERRO    1372                 buf->vb.state = VIDEOBUF_ERROR;
1373                 wake_up(&buf->vb.done);          1373                 wake_up(&buf->vb.done);
1374                 printk(KERN_ERR "%s/0: [%p/%d    1374                 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1375                         dev->name, buf, buf->    1375                         dev->name, buf, buf->vb.i,
1376                         (unsigned long)buf->r    1376                         (unsigned long)buf->risc.dma);
1377         }                                        1377         }
1378         cx23885_restart_video_queue(dev, q);     1378         cx23885_restart_video_queue(dev, q);
1379         spin_unlock_irqrestore(&dev->slock, f    1379         spin_unlock_irqrestore(&dev->slock, flags);
1380 }                                                1380 }
1381                                                  1381 
1382 int cx23885_video_irq(struct cx23885_dev *dev    1382 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1383 {                                                1383 {
1384         u32 mask, count;                         1384         u32 mask, count;
1385         int handled = 0;                         1385         int handled = 0;
1386                                                  1386 
1387         mask   = cx_read(VID_A_INT_MSK);         1387         mask   = cx_read(VID_A_INT_MSK);
1388         if (0 == (status & mask))                1388         if (0 == (status & mask))
1389                 return handled;                  1389                 return handled;
1390         cx_write(VID_A_INT_STAT, status);        1390         cx_write(VID_A_INT_STAT, status);
1391                                                  1391 
1392         dprintk(2, "%s() status = 0x%08x\n",     1392         dprintk(2, "%s() status = 0x%08x\n", __FUNCTION__, status);
1393         /* risc op code error */                 1393         /* risc op code error */
1394         if (status & (1 << 16)) {                1394         if (status & (1 << 16)) {
1395                 printk(KERN_WARNING "%s/0: vi    1395                 printk(KERN_WARNING "%s/0: video risc op code error\n",
1396                         dev->name);              1396                         dev->name);
1397                 cx_clear(VID_A_DMA_CTL, 0x11)    1397                 cx_clear(VID_A_DMA_CTL, 0x11);
1398                 cx23885_sram_channel_dump(dev    1398                 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1399         }                                        1399         }
1400                                                  1400 
1401         /* risc1 y */                            1401         /* risc1 y */
1402         if (status & 0x01) {                     1402         if (status & 0x01) {
1403                 spin_lock(&dev->slock);          1403                 spin_lock(&dev->slock);
1404                 count = cx_read(VID_A_GPCNT);    1404                 count = cx_read(VID_A_GPCNT);
1405                 cx23885_video_wakeup(dev, &de    1405                 cx23885_video_wakeup(dev, &dev->vidq, count);
1406                 spin_unlock(&dev->slock);        1406                 spin_unlock(&dev->slock);
1407                 handled++;                       1407                 handled++;
1408         }                                        1408         }
1409         /* risc2 y */                            1409         /* risc2 y */
1410         if (status & 0x10) {                     1410         if (status & 0x10) {
1411                 dprintk(2, "stopper video\n")    1411                 dprintk(2, "stopper video\n");
1412                 spin_lock(&dev->slock);          1412                 spin_lock(&dev->slock);
1413                 cx23885_restart_video_queue(d    1413                 cx23885_restart_video_queue(dev, &dev->vidq);
1414                 spin_unlock(&dev->slock);        1414                 spin_unlock(&dev->slock);
1415                 handled++;                       1415                 handled++;
1416         }                                        1416         }
1417                                                  1417 
1418         return handled;                          1418         return handled;
1419 }                                                1419 }
1420                                                  1420 
1421 /* ------------------------------------------    1421 /* ----------------------------------------------------------- */
1422 /* exported stuff                                1422 /* exported stuff                                              */
1423                                                  1423 
1424 static const struct file_operations video_fop    1424 static const struct file_operations video_fops = {
1425         .owner         = THIS_MODULE,            1425         .owner         = THIS_MODULE,
1426         .open          = video_open,             1426         .open          = video_open,
1427         .release       = video_release,          1427         .release       = video_release,
1428         .read          = video_read,             1428         .read          = video_read,
1429         .poll          = video_poll,             1429         .poll          = video_poll,
1430         .mmap          = video_mmap,             1430         .mmap          = video_mmap,
1431         .ioctl         = video_ioctl2,           1431         .ioctl         = video_ioctl2,
1432         .compat_ioctl  = v4l_compat_ioctl32,     1432         .compat_ioctl  = v4l_compat_ioctl32,
1433         .llseek        = no_llseek,              1433         .llseek        = no_llseek,
1434 };                                               1434 };
1435                                                  1435 
1436 static struct video_device cx23885_vbi_templa    1436 static struct video_device cx23885_vbi_template;
1437 static struct video_device cx23885_video_temp    1437 static struct video_device cx23885_video_template = {
1438         .name                 = "cx23885-vide    1438         .name                 = "cx23885-video",
1439         .type                 = VID_TYPE_CAPT    1439         .type                 = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1440         .fops                 = &video_fops,     1440         .fops                 = &video_fops,
1441         .minor                = -1,              1441         .minor                = -1,
1442         .vidioc_querycap      = vidioc_queryc    1442         .vidioc_querycap      = vidioc_querycap,
1443         .vidioc_enum_fmt_cap  = vidioc_enum_f    1443         .vidioc_enum_fmt_cap  = vidioc_enum_fmt_cap,
1444         .vidioc_g_fmt_cap     = vidioc_g_fmt_    1444         .vidioc_g_fmt_cap     = vidioc_g_fmt_cap,
1445         .vidioc_try_fmt_cap   = vidioc_try_fm    1445         .vidioc_try_fmt_cap   = vidioc_try_fmt_cap,
1446         .vidioc_s_fmt_cap     = vidioc_s_fmt_    1446         .vidioc_s_fmt_cap     = vidioc_s_fmt_cap,
1447         .vidioc_g_fmt_vbi     = cx23885_vbi_f    1447         .vidioc_g_fmt_vbi     = cx23885_vbi_fmt,
1448         .vidioc_try_fmt_vbi   = cx23885_vbi_f    1448         .vidioc_try_fmt_vbi   = cx23885_vbi_fmt,
1449         .vidioc_s_fmt_vbi     = cx23885_vbi_f    1449         .vidioc_s_fmt_vbi     = cx23885_vbi_fmt,
1450         .vidioc_reqbufs       = vidioc_reqbuf    1450         .vidioc_reqbufs       = vidioc_reqbufs,
1451         .vidioc_querybuf      = vidioc_queryb    1451         .vidioc_querybuf      = vidioc_querybuf,
1452         .vidioc_qbuf          = vidioc_qbuf,     1452         .vidioc_qbuf          = vidioc_qbuf,
1453         .vidioc_dqbuf         = vidioc_dqbuf,    1453         .vidioc_dqbuf         = vidioc_dqbuf,
1454         .vidioc_s_std         = vidioc_s_std,    1454         .vidioc_s_std         = vidioc_s_std,
1455         .vidioc_enum_input    = vidioc_enum_i    1455         .vidioc_enum_input    = vidioc_enum_input,
1456         .vidioc_g_input       = vidioc_g_inpu    1456         .vidioc_g_input       = vidioc_g_input,
1457         .vidioc_s_input       = vidioc_s_inpu    1457         .vidioc_s_input       = vidioc_s_input,
1458         .vidioc_queryctrl     = vidioc_queryc    1458         .vidioc_queryctrl     = vidioc_queryctrl,
1459         .vidioc_g_ctrl        = vidioc_g_ctrl    1459         .vidioc_g_ctrl        = vidioc_g_ctrl,
1460         .vidioc_s_ctrl        = vidioc_s_ctrl    1460         .vidioc_s_ctrl        = vidioc_s_ctrl,
1461         .vidioc_streamon      = vidioc_stream    1461         .vidioc_streamon      = vidioc_streamon,
1462         .vidioc_streamoff     = vidioc_stream    1462         .vidioc_streamoff     = vidioc_streamoff,
1463 #ifdef CONFIG_VIDEO_V4L1_COMPAT                  1463 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1464         .vidiocgmbuf          = vidiocgmbuf,     1464         .vidiocgmbuf          = vidiocgmbuf,
1465 #endif                                           1465 #endif
1466         .vidioc_g_tuner       = vidioc_g_tune    1466         .vidioc_g_tuner       = vidioc_g_tuner,
1467         .vidioc_s_tuner       = vidioc_s_tune    1467         .vidioc_s_tuner       = vidioc_s_tuner,
1468         .vidioc_g_frequency   = vidioc_g_freq    1468         .vidioc_g_frequency   = vidioc_g_frequency,
1469         .vidioc_s_frequency   = vidioc_s_freq    1469         .vidioc_s_frequency   = vidioc_s_frequency,
1470 #ifdef CONFIG_VIDEO_ADV_DEBUG                    1470 #ifdef CONFIG_VIDEO_ADV_DEBUG
1471         .vidioc_g_register    = vidioc_g_regi    1471         .vidioc_g_register    = vidioc_g_register,
1472         .vidioc_s_register    = vidioc_s_regi    1472         .vidioc_s_register    = vidioc_s_register,
1473 #endif                                           1473 #endif
1474         .tvnorms              = CX23885_NORMS    1474         .tvnorms              = CX23885_NORMS,
1475         .current_norm         = V4L2_STD_NTSC    1475         .current_norm         = V4L2_STD_NTSC_M,
1476 };                                               1476 };
1477                                                  1477 
1478 static const struct file_operations radio_fop    1478 static const struct file_operations radio_fops = {
1479         .owner         = THIS_MODULE,            1479         .owner         = THIS_MODULE,
1480         .open          = video_open,             1480         .open          = video_open,
1481         .release       = video_release,          1481         .release       = video_release,
1482         .ioctl         = video_ioctl2,           1482         .ioctl         = video_ioctl2,
1483         .compat_ioctl  = v4l_compat_ioctl32,     1483         .compat_ioctl  = v4l_compat_ioctl32,
1484         .llseek        = no_llseek,              1484         .llseek        = no_llseek,
1485 };                                               1485 };
1486                                                  1486 
1487                                                  1487 
1488 void cx23885_video_unregister(struct cx23885_    1488 void cx23885_video_unregister(struct cx23885_dev *dev)
1489 {                                                1489 {
1490         dprintk(1, "%s()\n", __FUNCTION__);      1490         dprintk(1, "%s()\n", __FUNCTION__);
1491         cx_clear(PCI_INT_MSK, 1);                1491         cx_clear(PCI_INT_MSK, 1);
1492                                                  1492 
1493         if (dev->video_dev) {                    1493         if (dev->video_dev) {
1494                 if (-1 != dev->video_dev->min    1494                 if (-1 != dev->video_dev->minor)
1495                         video_unregister_devi    1495                         video_unregister_device(dev->video_dev);
1496                 else                             1496                 else
1497                         video_device_release(    1497                         video_device_release(dev->video_dev);
1498                 dev->video_dev = NULL;           1498                 dev->video_dev = NULL;
1499                                                  1499 
1500                 btcx_riscmem_free(dev->pci, &    1500                 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1501         }                                        1501         }
1502 }                                                1502 }
1503                                                  1503 
1504 int cx23885_video_register(struct cx23885_dev    1504 int cx23885_video_register(struct cx23885_dev *dev)
1505 {                                                1505 {
1506         int err;                                 1506         int err;
1507                                                  1507 
1508         dprintk(1, "%s()\n", __FUNCTION__);      1508         dprintk(1, "%s()\n", __FUNCTION__);
1509         spin_lock_init(&dev->slock);             1509         spin_lock_init(&dev->slock);
1510                                                  1510 
1511         /* Initialize VBI template */            1511         /* Initialize VBI template */
1512         memcpy(&cx23885_vbi_template, &cx2388    1512         memcpy(&cx23885_vbi_template, &cx23885_video_template,
1513                 sizeof(cx23885_vbi_template))    1513                 sizeof(cx23885_vbi_template));
1514         strcpy(cx23885_vbi_template.name, "cx    1514         strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1515         cx23885_vbi_template.type = VID_TYPE_    1515         cx23885_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1516                                                  1516 
1517         dev->tvnorm = cx23885_video_template.    1517         dev->tvnorm = cx23885_video_template.current_norm;
1518                                                  1518 
1519         /* init video dma queues */              1519         /* init video dma queues */
1520         INIT_LIST_HEAD(&dev->vidq.active);       1520         INIT_LIST_HEAD(&dev->vidq.active);
1521         INIT_LIST_HEAD(&dev->vidq.queued);       1521         INIT_LIST_HEAD(&dev->vidq.queued);
1522         dev->vidq.timeout.function = cx23885_    1522         dev->vidq.timeout.function = cx23885_vid_timeout;
1523         dev->vidq.timeout.data = (unsigned lo    1523         dev->vidq.timeout.data = (unsigned long)dev;
1524         init_timer(&dev->vidq.timeout);          1524         init_timer(&dev->vidq.timeout);
1525         cx23885_risc_stopper(dev->pci, &dev->    1525         cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1526                 VID_A_DMA_CTL, 0x11, 0x00);      1526                 VID_A_DMA_CTL, 0x11, 0x00);
1527                                                  1527 
1528         /* Don't enable VBI yet */               1528         /* Don't enable VBI yet */
1529         cx_set(PCI_INT_MSK, 1);                  1529         cx_set(PCI_INT_MSK, 1);
1530                                                  1530 
1531                                                  1531 
1532         /* register v4l devices */               1532         /* register v4l devices */
1533         dev->video_dev = cx23885_vdev_init(de    1533         dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1534                 &cx23885_video_template, "vid    1534                 &cx23885_video_template, "video");
1535         err = video_register_device(dev->vide    1535         err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1536                                     video_nr[    1536                                     video_nr[dev->nr]);
1537         if (err < 0) {                           1537         if (err < 0) {
1538                 printk(KERN_INFO "%s: can't r    1538                 printk(KERN_INFO "%s: can't register video device\n",
1539                         dev->name);              1539                         dev->name);
1540                 goto fail_unreg;                 1540                 goto fail_unreg;
1541         }                                        1541         }
1542         printk(KERN_INFO "%s/0: registered de    1542         printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1543                dev->name, dev->video_dev->min    1543                dev->name, dev->video_dev->minor & 0x1f);
1544         /* initial device configuration */       1544         /* initial device configuration */
1545         mutex_lock(&dev->lock);                  1545         mutex_lock(&dev->lock);
1546         cx23885_set_tvnorm(dev, dev->tvnorm);    1546         cx23885_set_tvnorm(dev, dev->tvnorm);
1547         init_controls(dev);                      1547         init_controls(dev);
1548         cx23885_video_mux(dev, 0);               1548         cx23885_video_mux(dev, 0);
1549         mutex_unlock(&dev->lock);                1549         mutex_unlock(&dev->lock);
1550                                                  1550 
1551         return 0;                                1551         return 0;
1552                                                  1552 
1553 fail_unreg:                                      1553 fail_unreg:
1554         cx23885_video_unregister(dev);           1554         cx23885_video_unregister(dev);
1555         return err;                              1555         return err;
1556 }                                                1556 }
1557                                                  1557 
1558                                                  1558 
  This page was automatically generated by the LXR engine.