1 /***************************************************************************
2 * Video4Linux driver for W996[87]CF JPEG USB Dual Mode Camera Chip. *
4 * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * - Memory management code from bttv driver by Ralph Metzler, *
7 * Marcus Metzler and Gerd Knorr. *
8 * - I2C interface to kernel, high-level image sensor control routines and *
9 * some symbolic names from OV511 driver by Mark W. McClelland. *
10 * - Low-level I2C fast write function by Piotr Czerczak. *
11 * - Low-level I2C read function by Frederic Jouault. *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the Free Software *
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
26 ***************************************************************************/
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/kmod.h>
31 #include <linux/init.h>
33 #include <linux/vmalloc.h>
34 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/sched.h>
39 #include <linux/ioctl.h>
40 #include <linux/delay.h>
41 #include <linux/stddef.h>
43 #include <asm/uaccess.h>
44 #include <linux/page-flags.h>
45 #include <linux/videodev.h>
46 #include <media/v4l2-ioctl.h>
49 #include "w9968cf_decoder.h"
51 static struct w9968cf_vpp_t* w9968cf_vpp;
52 static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
54 static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
55 static DEFINE_MUTEX(w9968cf_devlist_mutex); /* semaphore for list traversal */
57 static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
60 /****************************************************************************
61 * Module macros and parameters *
62 ****************************************************************************/
64 MODULE_DEVICE_TABLE(usb, winbond_id_table);
66 MODULE_AUTHOR(W9968CF_MODULE_AUTHOR" "W9968CF_AUTHOR_EMAIL);
67 MODULE_DESCRIPTION(W9968CF_MODULE_NAME);
68 MODULE_VERSION(W9968CF_MODULE_VERSION);
69 MODULE_LICENSE(W9968CF_MODULE_LICENSE);
70 MODULE_SUPPORTED_DEVICE("Video");
72 static unsigned short simcams = W9968CF_SIMCAMS;
73 static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
74 static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
76 static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
78 static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
79 W9968CF_DOUBLE_BUFFER};
80 static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
81 static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
83 static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
84 static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
85 W9968CF_DECOMPRESSION};
86 static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
87 static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
88 static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
89 static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
90 static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
91 static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
93 static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
94 W9968CF_BANDINGFILTER};
95 static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
96 static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
97 static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
98 static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
99 static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
101 static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
102 static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
103 static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
105 static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
108 static unsigned short debug = W9968CF_DEBUG_LEVEL;
109 static int specific_debug = W9968CF_SPECIFIC_DEBUG;
112 static unsigned int param_nv[24]; /* number of values per parameter */
114 module_param(simcams, ushort, 0644);
115 module_param_array(video_nr, short, ¶m_nv[0], 0444);
116 module_param_array(packet_size, uint, ¶m_nv[1], 0444);
117 module_param_array(max_buffers, ushort, ¶m_nv[2], 0444);
118 module_param_array(double_buffer, bool, ¶m_nv[3], 0444);
119 module_param_array(clamping, bool, ¶m_nv[4], 0444);
120 module_param_array(filter_type, ushort, ¶m_nv[5], 0444);
121 module_param_array(largeview, bool, ¶m_nv[6], 0444);
122 module_param_array(decompression, ushort, ¶m_nv[7], 0444);
123 module_param_array(upscaling, bool, ¶m_nv[8], 0444);
124 module_param_array(force_palette, ushort, ¶m_nv[9], 0444);
125 module_param_array(force_rgb, ushort, ¶m_nv[10], 0444);
126 module_param_array(autobright, bool, ¶m_nv[11], 0444);
127 module_param_array(autoexp, bool, ¶m_nv[12], 0444);
128 module_param_array(lightfreq, ushort, ¶m_nv[13], 0444);
129 module_param_array(bandingfilter, bool, ¶m_nv[14], 0444);
130 module_param_array(clockdiv, short, ¶m_nv[15], 0444);
131 module_param_array(backlight, bool, ¶m_nv[16], 0444);
132 module_param_array(mirror, bool, ¶m_nv[17], 0444);
133 module_param_array(monochrome, bool, ¶m_nv[18], 0444);
134 module_param_array(brightness, uint, ¶m_nv[19], 0444);
135 module_param_array(hue, uint, ¶m_nv[20], 0444);
136 module_param_array(colour, uint, ¶m_nv[21], 0444);
137 module_param_array(contrast, uint, ¶m_nv[22], 0444);
138 module_param_array(whiteness, uint, ¶m_nv[23], 0444);
140 module_param(debug, ushort, 0644);
141 module_param(specific_debug, bool, 0644);
144 MODULE_PARM_DESC(simcams,
145 "\n<n> Number of cameras allowed to stream simultaneously."
146 "\nn may vary from 0 to "
147 __MODULE_STRING(W9968CF_MAX_DEVICES)"."
148 "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
150 MODULE_PARM_DESC(video_nr,
151 "\n<-1|n[,...]> Specify V4L minor mode number."
152 "\n -1 = use next available (default)"
153 "\n n = use minor number n (integer >= 0)"
154 "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
157 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
158 "\nthe second camera and use auto for the first"
159 "\none and for every other camera."
161 MODULE_PARM_DESC(packet_size,
162 "\n<n[,...]> Specify the maximum data payload"
163 "\nsize in bytes for alternate settings, for each device."
164 "\nn is scaled between 63 and 1023 "
165 "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
167 MODULE_PARM_DESC(max_buffers,
168 "\n<n[,...]> For advanced users."
169 "\nSpecify the maximum number of video frame buffers"
170 "\nto allocate for each device, from 2 to "
171 __MODULE_STRING(W9968CF_MAX_BUFFERS)
172 ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
174 MODULE_PARM_DESC(double_buffer,
176 "Hardware double buffering: 0 disabled, 1 enabled."
177 "\nIt should be enabled if you want smooth video output: if"
178 "\nyou obtain out of sync. video, disable it, or try to"
179 "\ndecrease the 'clockdiv' module parameter value."
180 "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
183 MODULE_PARM_DESC(clamping,
184 "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
185 "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
188 MODULE_PARM_DESC(filter_type,
189 "\n<0|1|2[,...]> Video filter type."
190 "\n0 none, 1 (1-2-1) 3-tap filter, "
191 "2 (2-3-6-3-2) 5-tap filter."
192 "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
194 "\nThe filter is used to reduce noise and aliasing artifacts"
195 "\nproduced by the CCD or CMOS image sensor, and the scaling"
198 MODULE_PARM_DESC(largeview,
199 "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
200 "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
203 MODULE_PARM_DESC(upscaling,
204 "\n<0|1[,...]> Software scaling (for non-compressed video):"
205 "\n0 disabled, 1 enabled."
206 "\nDisable it if you have a slow CPU or you don't have"
208 "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
210 "\nIf 'w9968cf-vpp' is not present, this parameter is"
213 MODULE_PARM_DESC(decompression,
214 "\n<0|1|2[,...]> Software video decompression:"
215 "\n- 0 disables decompression (doesn't allow formats needing"
217 "\n- 1 forces decompression (allows formats needing"
218 " decompression only);"
219 "\n- 2 allows any permitted formats."
220 "\nFormats supporting compressed video are YUV422P and"
222 "\nin any resolutions where both width and height are "
224 "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
226 "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
227 "\nnot allowed; in this case this parameter is set to 2."
229 MODULE_PARM_DESC(force_palette,
231 "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
232 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
233 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
234 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
235 "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
236 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
237 "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
238 "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
239 "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
240 "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
241 "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
243 " Force picture palette."
245 "\n- 0 allows any of the following formats:"
246 "\n- UYVY 16 bpp - Original video, compression disabled"
247 "\n- YUV420 12 bpp - Original video, compression enabled"
248 "\n- YUV422P 16 bpp - Original video, compression enabled"
249 "\n- YUV420P 12 bpp - Original video, compression enabled"
250 "\n- YUVY 16 bpp - Software conversion from UYVY"
251 "\n- YUV422 16 bpp - Software conversion from UYVY"
252 "\n- GREY 8 bpp - Software conversion from UYVY"
253 "\n- RGB555 16 bpp - Software conversion from UYVY"
254 "\n- RGB565 16 bpp - Software conversion from UYVY"
255 "\n- RGB24 24 bpp - Software conversion from UYVY"
256 "\n- RGB32 32 bpp - Software conversion from UYVY"
257 "\nWhen not 0, this parameter will override 'decompression'."
258 "\nDefault value is 0 for every device."
259 "\nInitial palette is "
260 __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
261 "\nIf 'w9968cf-vpp' is not present, this parameter is"
264 MODULE_PARM_DESC(force_rgb,
265 "\n<0|1[,...]> Read RGB video data instead of BGR:"
266 "\n 1 = use RGB component ordering."
267 "\n 0 = use BGR component ordering."
268 "\nThis parameter has effect when using RGBX palettes only."
269 "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
272 MODULE_PARM_DESC(autobright,
273 "\n<0|1[,...]> Image sensor automatically changes brightness:"
275 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
278 MODULE_PARM_DESC(autoexp,
279 "\n<0|1[,...]> Image sensor automatically changes exposure:"
281 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
284 MODULE_PARM_DESC(lightfreq,
285 "\n<50|60[,...]> Light frequency in Hz:"
286 "\n 50 for European and Asian lighting,"
287 " 60 for American lighting."
288 "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
291 MODULE_PARM_DESC(bandingfilter,
292 "\n<0|1[,...]> Banding filter to reduce effects of"
293 " fluorescent lighting:"
294 "\n 0 disabled, 1 enabled."
295 "\nThis filter tries to reduce the pattern of horizontal"
296 "\nlight/dark bands caused by some (usually fluorescent)"
298 "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
301 MODULE_PARM_DESC(clockdiv,
303 "Force pixel clock divisor to a specific value (for experts):"
304 "\n n may vary from 0 to 127."
305 "\n -1 for automatic value."
306 "\nSee also the 'double_buffer' module parameter."
307 "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
310 MODULE_PARM_DESC(backlight,
311 "\n<0|1[,...]> Objects are lit from behind:"
313 "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
316 MODULE_PARM_DESC(mirror,
317 "\n<0|1[,...]> Reverse image horizontally:"
319 "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
322 MODULE_PARM_DESC(monochrome,
323 "\n<0|1[,...]> Use image sensor as monochrome sensor:"
325 "\nNot all the sensors support monochrome color."
326 "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
329 MODULE_PARM_DESC(brightness,
330 "\n<n[,...]> Set picture brightness (0-65535)."
331 "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
333 "\nThis parameter has no effect if 'autobright' is enabled."
335 MODULE_PARM_DESC(hue,
336 "\n<n[,...]> Set picture hue (0-65535)."
337 "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
340 MODULE_PARM_DESC(colour,
341 "\n<n[,...]> Set picture saturation (0-65535)."
342 "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
345 MODULE_PARM_DESC(contrast,
346 "\n<n[,...]> Set picture contrast (0-65535)."
347 "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
350 MODULE_PARM_DESC(whiteness,
351 "\n<n[,...]> Set picture whiteness (0-65535)."
352 "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
356 MODULE_PARM_DESC(debug,
357 "\n<n> Debugging information level, from 0 to 6:"
358 "\n0 = none (use carefully)"
359 "\n1 = critical errors"
360 "\n2 = significant informations"
361 "\n3 = configuration or general messages"
363 "\n5 = called functions"
364 "\n6 = function internals"
365 "\nLevel 5 and 6 are useful for testing only, when only "
366 "one device is used."
367 "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
369 MODULE_PARM_DESC(specific_debug,
370 "\n<0|1> Enable or disable specific debugging messages:"
371 "\n0 = print messages concerning every level"
373 "\n1 = print messages concerning the level"
374 " indicated by 'debug'."
375 "\nDefault value is "
376 __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
378 #endif /* W9968CF_DEBUG */
382 /****************************************************************************
384 ****************************************************************************/
386 /* Video4linux interface */
387 static const struct v4l2_file_operations w9968cf_fops;
388 static int w9968cf_open(struct file *);
389 static int w9968cf_release(struct file *);
390 static int w9968cf_mmap(struct file *, struct vm_area_struct *);
391 static long w9968cf_ioctl(struct file *, unsigned, unsigned long);
392 static ssize_t w9968cf_read(struct file *, char __user *, size_t, loff_t *);
393 static long w9968cf_v4l_ioctl(struct file *, unsigned int,
397 static int w9968cf_start_transfer(struct w9968cf_device*);
398 static int w9968cf_stop_transfer(struct w9968cf_device*);
399 static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
400 static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
401 static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
402 static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
403 static int w9968cf_read_sb(struct w9968cf_device*);
404 static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
405 static void w9968cf_urb_complete(struct urb *urb);
407 /* Low-level I2C (SMBus) I/O */
408 static int w9968cf_smbus_start(struct w9968cf_device*);
409 static int w9968cf_smbus_stop(struct w9968cf_device*);
410 static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
411 static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
412 static int w9968cf_smbus_write_ack(struct w9968cf_device*);
413 static int w9968cf_smbus_read_ack(struct w9968cf_device*);
414 static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
415 static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
416 u16 address, u8* value);
417 static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
418 u8 subaddress, u8* value);
419 static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
420 u16 address, u8 subaddress);
421 static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
422 u16 address, u8 subaddress,
425 /* I2C interface to kernel */
426 static int w9968cf_i2c_init(struct w9968cf_device*);
427 static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
428 unsigned short flags, char read_write,
429 u8 command, int size, union i2c_smbus_data*);
430 static u32 w9968cf_i2c_func(struct i2c_adapter*);
432 /* Memory management */
433 static void* rvmalloc(unsigned long size);
434 static void rvfree(void *mem, unsigned long size);
435 static void w9968cf_deallocate_memory(struct w9968cf_device*);
436 static int w9968cf_allocate_memory(struct w9968cf_device*);
438 /* High-level image sensor control functions */
439 static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
440 static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
441 static int w9968cf_sensor_cmd(struct w9968cf_device*,
442 unsigned int cmd, void *arg);
443 static int w9968cf_sensor_init(struct w9968cf_device*);
444 static int w9968cf_sensor_update_settings(struct w9968cf_device*);
445 static int w9968cf_sensor_get_picture(struct w9968cf_device*);
446 static int w9968cf_sensor_update_picture(struct w9968cf_device*,
447 struct video_picture pict);
449 /* Other helper functions */
450 static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
451 enum w9968cf_model_id,
452 const unsigned short dev_nr);
453 static void w9968cf_adjust_configuration(struct w9968cf_device*);
454 static int w9968cf_turn_on_led(struct w9968cf_device*);
455 static int w9968cf_init_chip(struct w9968cf_device*);
456 static inline u16 w9968cf_valid_palette(u16 palette);
457 static inline u16 w9968cf_valid_depth(u16 palette);
458 static inline u8 w9968cf_need_decompression(u16 palette);
459 static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
460 static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
461 static int w9968cf_postprocess_frame(struct w9968cf_device*,
462 struct w9968cf_frame_t*);
463 static int w9968cf_adjust_window_size(struct w9968cf_device*, u32 *w, u32 *h);
464 static void w9968cf_init_framelist(struct w9968cf_device*);
465 static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
466 static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
467 static void w9968cf_release_resources(struct w9968cf_device*);
471 /****************************************************************************
473 ****************************************************************************/
475 /* Used to represent a list of values and their respective symbolic names */
476 struct w9968cf_symbolic_list {
481 /*--------------------------------------------------------------------------
482 Returns the name of the matching element in the symbolic_list array. The
483 end of the list must be marked with an element that has a NULL name.
484 --------------------------------------------------------------------------*/
485 static inline const char *
486 symbolic(struct w9968cf_symbolic_list list[], const int num)
490 for (i = 0; list[i].name != NULL; i++)
491 if (list[i].num == num)
492 return (list[i].name);
497 static struct w9968cf_symbolic_list camlist[] = {
498 { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
499 { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
501 /* Other cameras (having the same descriptors as Generic W996[87]CF) */
502 { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
503 { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
504 { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
505 { W9968CF_MOD_LL, "Lebon LDC-035A" },
506 { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
507 { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
508 { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
509 { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
510 { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
515 static struct w9968cf_symbolic_list senlist[] = {
516 { CC_OV76BE, "OV76BE" },
517 { CC_OV7610, "OV7610" },
518 { CC_OV7620, "OV7620" },
519 { CC_OV7620AE, "OV7620AE" },
520 { CC_OV6620, "OV6620" },
521 { CC_OV6630, "OV6630" },
522 { CC_OV6630AE, "OV6630AE" },
523 { CC_OV6630AF, "OV6630AF" },
527 /* Video4Linux1 palettes */
528 static struct w9968cf_symbolic_list v4l1_plist[] = {
529 { VIDEO_PALETTE_GREY, "GREY" },
530 { VIDEO_PALETTE_HI240, "HI240" },
531 { VIDEO_PALETTE_RGB565, "RGB565" },
532 { VIDEO_PALETTE_RGB24, "RGB24" },
533 { VIDEO_PALETTE_RGB32, "RGB32" },
534 { VIDEO_PALETTE_RGB555, "RGB555" },
535 { VIDEO_PALETTE_YUV422, "YUV422" },
536 { VIDEO_PALETTE_YUYV, "YUYV" },
537 { VIDEO_PALETTE_UYVY, "UYVY" },
538 { VIDEO_PALETTE_YUV420, "YUV420" },
539 { VIDEO_PALETTE_YUV411, "YUV411" },
540 { VIDEO_PALETTE_RAW, "RAW" },
541 { VIDEO_PALETTE_YUV422P, "YUV422P" },
542 { VIDEO_PALETTE_YUV411P, "YUV411P" },
543 { VIDEO_PALETTE_YUV420P, "YUV420P" },
544 { VIDEO_PALETTE_YUV410P, "YUV410P" },
548 /* Decoder error codes: */
549 static struct w9968cf_symbolic_list decoder_errlist[] = {
550 { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
551 { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
552 { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
553 { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
554 { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
555 { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
559 /* URB error codes: */
560 static struct w9968cf_symbolic_list urb_errlist[] = {
561 { -ENOMEM, "No memory for allocation of internal structures" },
562 { -ENOSPC, "The host controller's bandwidth is already consumed" },
563 { -ENOENT, "URB was canceled by unlink_urb" },
564 { -EXDEV, "ISO transfer only partially completed" },
565 { -EAGAIN, "Too match scheduled for the future" },
566 { -ENXIO, "URB already queued" },
567 { -EFBIG, "Too much ISO frames requested" },
568 { -ENOSR, "Buffer error (overrun)" },
569 { -EPIPE, "Specified endpoint is stalled (device not responding)"},
570 { -EOVERFLOW, "Babble (too much data)" },
571 { -EPROTO, "Bit-stuff error (bad cable?)" },
572 { -EILSEQ, "CRC/Timeout" },
573 { -ETIME, "Device does not respond to token" },
574 { -ETIMEDOUT, "Device does not respond to command" },
578 /****************************************************************************
579 * Memory management functions *
580 ****************************************************************************/
581 static void* rvmalloc(unsigned long size)
586 size = PAGE_ALIGN(size);
587 mem = vmalloc_32(size);
591 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
592 adr = (unsigned long) mem;
594 SetPageReserved(vmalloc_to_page((void *)adr));
603 static void rvfree(void* mem, unsigned long size)
610 adr = (unsigned long) mem;
611 while ((long) size > 0) {
612 ClearPageReserved(vmalloc_to_page((void *)adr));
620 /*--------------------------------------------------------------------------
621 Deallocate previously allocated memory.
622 --------------------------------------------------------------------------*/
623 static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
627 /* Free the isochronous transfer buffers */
628 for (i = 0; i < W9968CF_URBS; i++) {
629 kfree(cam->transfer_buffer[i]);
630 cam->transfer_buffer[i] = NULL;
633 /* Free temporary frame buffer */
634 if (cam->frame_tmp.buffer) {
635 rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
636 cam->frame_tmp.buffer = NULL;
639 /* Free helper buffer */
640 if (cam->frame_vpp.buffer) {
641 rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
642 cam->frame_vpp.buffer = NULL;
645 /* Free video frame buffers */
646 if (cam->frame[0].buffer) {
647 rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
648 cam->frame[0].buffer = NULL;
653 DBG(5, "Memory successfully deallocated")
657 /*--------------------------------------------------------------------------
658 Allocate memory buffers for USB transfers and video frames.
659 This function is called by open() only.
660 Return 0 on success, a negative number otherwise.
661 --------------------------------------------------------------------------*/
662 static int w9968cf_allocate_memory(struct w9968cf_device* cam)
664 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
666 unsigned long hw_bufsize, vpp_bufsize;
669 /* NOTE: Deallocation is done elsewhere in case of error */
671 /* Calculate the max amount of raw data per frame from the device */
672 hw_bufsize = cam->maxwidth*cam->maxheight*2;
674 /* Calculate the max buf. size needed for post-processing routines */
675 bpp = (w9968cf_vpp) ? 4 : 2;
677 vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
678 cam->maxwidth*cam->maxheight*bpp);
680 vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
682 /* Allocate memory for the isochronous transfer buffers */
683 for (i = 0; i < W9968CF_URBS; i++) {
684 if (!(cam->transfer_buffer[i] =
685 kzalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
686 DBG(1, "Couldn't allocate memory for the isochronous "
687 "transfer buffers (%u bytes)",
688 p_size * W9968CF_ISO_PACKETS)
693 /* Allocate memory for the temporary frame buffer */
694 if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
695 DBG(1, "Couldn't allocate memory for the temporary "
696 "video frame buffer (%lu bytes)", hw_bufsize)
699 cam->frame_tmp.size = hw_bufsize;
700 cam->frame_tmp.number = -1;
702 /* Allocate memory for the helper buffer */
704 if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
705 DBG(1, "Couldn't allocate memory for the helper buffer"
706 " (%lu bytes)", vpp_bufsize)
709 cam->frame_vpp.size = vpp_bufsize;
711 cam->frame_vpp.buffer = NULL;
713 /* Allocate memory for video frame buffers */
714 cam->nbuffers = cam->max_buffers;
715 while (cam->nbuffers >= 2) {
716 if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
723 DBG(1, "Couldn't allocate memory for the video frame buffers")
728 if (cam->nbuffers != cam->max_buffers)
729 DBG(2, "Couldn't allocate memory for %u video frame buffers. "
730 "Only memory for %u buffers has been allocated",
731 cam->max_buffers, cam->nbuffers)
733 for (i = 0; i < cam->nbuffers; i++) {
734 cam->frame[i].buffer = buff + i*vpp_bufsize;
735 cam->frame[i].size = vpp_bufsize;
736 cam->frame[i].number = i;
738 if (i != cam->nbuffers-1)
739 cam->frame[i].next = &cam->frame[i+1];
741 cam->frame[i].next = &cam->frame[0];
742 cam->frame[i].status = F_UNUSED;
745 DBG(5, "Memory successfully allocated")
751 /****************************************************************************
752 * USB-specific functions *
753 ****************************************************************************/
755 /*--------------------------------------------------------------------------
756 This is an handler function which is called after the URBs are completed.
757 It collects multiple data packets coming from the camera by putting them
758 into frame buffers: one or more zero data length data packets are used to
759 mark the end of a video frame; the first non-zero data packet is the start
760 of the next video frame; if an error is encountered in a packet, the entire
761 video frame is discarded and grabbed again.
762 If there are no requested frames in the FIFO list, packets are collected into
764 --------------------------------------------------------------------------*/
765 static void w9968cf_urb_complete(struct urb *urb)
767 struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
768 struct w9968cf_frame_t** f;
769 unsigned int len, status;
774 if ((!cam->streaming) || cam->disconnected) {
775 DBG(4, "Got interrupt, but not streaming")
779 /* "(*f)" will be used instead of "cam->frame_current" */
780 f = &cam->frame_current;
782 /* If a frame has been requested and we are grabbing into
783 the temporary frame, we'll switch to that requested frame */
784 if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
785 if (cam->frame_tmp.status == F_GRABBING) {
786 w9968cf_pop_frame(cam, &cam->frame_current);
787 (*f)->status = F_GRABBING;
788 (*f)->length = cam->frame_tmp.length;
789 memcpy((*f)->buffer, cam->frame_tmp.buffer,
791 DBG(6, "Switched from temp. frame to frame #%d",
796 for (i = 0; i < urb->number_of_packets; i++) {
797 len = urb->iso_frame_desc[i].actual_length;
798 status = urb->iso_frame_desc[i].status;
799 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
801 if (status && len != 0) {
802 DBG(4, "URB failed, error in data packet "
804 status, symbolic(urb_errlist, status))
805 (*f)->status = F_ERROR;
809 if (len) { /* start of frame */
811 if ((*f)->status == F_UNUSED) {
812 (*f)->status = F_GRABBING;
816 /* Buffer overflows shouldn't happen, however...*/
817 if ((*f)->length + len > (*f)->size) {
818 DBG(4, "Buffer overflow: bad data packets")
819 (*f)->status = F_ERROR;
822 if ((*f)->status == F_GRABBING) {
823 memcpy((*f)->buffer + (*f)->length, pos, len);
827 } else if ((*f)->status == F_GRABBING) { /* end of frame */
829 DBG(6, "Frame #%d successfully grabbed", (*f)->number)
831 if (cam->vpp_flag & VPP_DECOMPRESSION) {
832 err = w9968cf_vpp->check_headers((*f)->buffer,
835 DBG(4, "Skip corrupted frame: %s",
836 symbolic(decoder_errlist, err))
837 (*f)->status = F_UNUSED;
838 continue; /* grab this frame again */
842 (*f)->status = F_READY;
845 /* Take a pointer to the new frame from the FIFO list.
846 If the list is empty,we'll use the temporary frame*/
847 if (*cam->requested_frame)
848 w9968cf_pop_frame(cam, &cam->frame_current);
850 cam->frame_current = &cam->frame_tmp;
851 (*f)->status = F_UNUSED;
854 } else if ((*f)->status == F_ERROR)
855 (*f)->status = F_UNUSED; /* grab it again */
857 PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
858 (unsigned long)(*f)->length, i, len, (*f)->status)
862 /* Resubmit this URB */
863 urb->dev = cam->usbdev;
865 spin_lock(&cam->urb_lock);
867 if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
868 cam->misconfigured = 1;
869 DBG(1, "Couldn't resubmit the URB: error %d, %s",
870 err, symbolic(urb_errlist, err))
872 spin_unlock(&cam->urb_lock);
874 /* Wake up the user process */
875 wake_up_interruptible(&cam->wait_queue);
879 /*---------------------------------------------------------------------------
880 Setup the URB structures for the isochronous transfer.
881 Submit the URBs so that the data transfer begins.
882 Return 0 on success, a negative number otherwise.
883 ---------------------------------------------------------------------------*/
884 static int w9968cf_start_transfer(struct w9968cf_device* cam)
886 struct usb_device *udev = cam->usbdev;
888 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
895 for (i = 0; i < W9968CF_URBS; i++) {
896 urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
898 for (j = 0; j < i; j++)
899 usb_free_urb(cam->urb[j]);
900 DBG(1, "Couldn't allocate the URB structures")
906 urb->context = (void*)cam;
907 urb->pipe = usb_rcvisocpipe(udev, 1);
908 urb->transfer_flags = URB_ISO_ASAP;
909 urb->number_of_packets = W9968CF_ISO_PACKETS;
910 urb->complete = w9968cf_urb_complete;
911 urb->transfer_buffer = cam->transfer_buffer[i];
912 urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
914 for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
915 urb->iso_frame_desc[j].offset = p_size*j;
916 urb->iso_frame_desc[j].length = p_size;
920 /* Transfer size per frame, in WORD ! */
927 err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
928 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
931 err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
932 err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
934 if (cam->vpp_flag & VPP_DECOMPRESSION)
935 err += w9968cf_upload_quantizationtables(cam);
937 vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
938 err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
940 err += usb_set_interface(udev, 0, cam->altsetting);
941 err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
943 if (err || (vidcapt < 0)) {
944 for (i = 0; i < W9968CF_URBS; i++)
945 usb_free_urb(cam->urb[i]);
946 DBG(1, "Couldn't tell the camera to start the data transfer")
950 w9968cf_init_framelist(cam);
952 /* Begin to grab into the temporary buffer */
953 cam->frame_tmp.status = F_UNUSED;
954 cam->frame_tmp.queued = 0;
955 cam->frame_current = &cam->frame_tmp;
957 if (!(cam->vpp_flag & VPP_DECOMPRESSION))
958 DBG(5, "Isochronous transfer size: %lu bytes/frame",
959 (unsigned long)t_size*2)
961 DBG(5, "Starting the isochronous transfer...")
965 /* Submit the URBs */
966 for (i = 0; i < W9968CF_URBS; i++) {
967 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
970 for (j = i-1; j >= 0; j--) {
971 usb_kill_urb(cam->urb[j]);
972 usb_free_urb(cam->urb[j]);
974 DBG(1, "Couldn't send a transfer request to the "
975 "USB core (error #%d, %s)", err,
976 symbolic(urb_errlist, err))
985 /*--------------------------------------------------------------------------
986 Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
987 Return 0 on success, a negative number otherwise.
988 --------------------------------------------------------------------------*/
989 static int w9968cf_stop_transfer(struct w9968cf_device* cam)
991 struct usb_device *udev = cam->usbdev;
992 unsigned long lock_flags;
999 /* This avoids race conditions with usb_submit_urb()
1000 in the URB completition handler */
1001 spin_lock_irqsave(&cam->urb_lock, lock_flags);
1003 spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
1005 for (i = W9968CF_URBS-1; i >= 0; i--)
1007 usb_kill_urb(cam->urb[i]);
1008 usb_free_urb(cam->urb[i]);
1012 if (cam->disconnected)
1015 err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
1016 err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
1017 err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
1018 err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
1021 DBG(2, "Failed to tell the camera to stop the isochronous "
1022 "transfer. However this is not a critical error.")
1027 DBG(5, "Isochronous transfer stopped")
1032 /*--------------------------------------------------------------------------
1033 Write a W9968CF register.
1034 Return 0 on success, -1 otherwise.
1035 --------------------------------------------------------------------------*/
1036 static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
1038 struct usb_device* udev = cam->usbdev;
1041 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1042 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1043 value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
1046 DBG(4, "Failed to write a register "
1047 "(value 0x%04X, index 0x%02X, error #%d, %s)",
1048 value, index, res, symbolic(urb_errlist, res))
1050 return (res >= 0) ? 0 : -1;
1054 /*--------------------------------------------------------------------------
1055 Read a W9968CF register.
1056 Return the register value on success, -1 otherwise.
1057 --------------------------------------------------------------------------*/
1058 static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
1060 struct usb_device* udev = cam->usbdev;
1061 u16* buff = cam->control_buffer;
1064 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
1065 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1066 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
1069 DBG(4, "Failed to read a register "
1070 "(index 0x%02X, error #%d, %s)",
1071 index, res, symbolic(urb_errlist, res))
1073 return (res >= 0) ? (int)(*buff) : -1;
1077 /*--------------------------------------------------------------------------
1078 Write 64-bit data to the fast serial bus registers.
1079 Return 0 on success, -1 otherwise.
1080 --------------------------------------------------------------------------*/
1081 static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
1083 struct usb_device* udev = cam->usbdev;
1089 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1090 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1091 value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
1094 DBG(4, "Failed to write the FSB registers "
1095 "(error #%d, %s)", res, symbolic(urb_errlist, res))
1097 return (res >= 0) ? 0 : -1;
1101 /*--------------------------------------------------------------------------
1102 Write data to the serial bus control register.
1103 Return 0 on success, a negative number otherwise.
1104 --------------------------------------------------------------------------*/
1105 static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
1109 err = w9968cf_write_reg(cam, value, 0x01);
1110 udelay(W9968CF_I2C_BUS_DELAY);
1116 /*--------------------------------------------------------------------------
1117 Read data from the serial bus control register.
1118 Return 0 on success, a negative number otherwise.
1119 --------------------------------------------------------------------------*/
1120 static int w9968cf_read_sb(struct w9968cf_device* cam)
1124 v = w9968cf_read_reg(cam, 0x01);
1125 udelay(W9968CF_I2C_BUS_DELAY);
1131 /*--------------------------------------------------------------------------
1132 Upload quantization tables for the JPEG compression.
1133 This function is called by w9968cf_start_transfer().
1134 Return 0 on success, a negative number otherwise.
1135 --------------------------------------------------------------------------*/
1136 static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
1141 err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
1143 for (i = 0, j = 0; i < 32; i++, j += 2) {
1144 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
1145 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
1146 err += w9968cf_write_reg(cam, a, 0x40+i);
1147 err += w9968cf_write_reg(cam, b, 0x60+i);
1149 err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
1156 /****************************************************************************
1157 * Low-level I2C I/O functions. *
1158 * The adapter supports the following I2C transfer functions: *
1159 * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
1160 * i2c_adap_read_byte_data() *
1161 * i2c_adap_read_byte() *
1162 ****************************************************************************/
1164 static int w9968cf_smbus_start(struct w9968cf_device* cam)
1168 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1169 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1175 static int w9968cf_smbus_stop(struct w9968cf_device* cam)
1179 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1180 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1186 static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
1191 for (bit = 0 ; bit < 8 ; bit++) {
1192 sda = (v & 0x80) ? 2 : 0;
1194 /* SDE=1, SDA=sda, SCL=0 */
1195 err += w9968cf_write_sb(cam, 0x10 | sda);
1196 /* SDE=1, SDA=sda, SCL=1 */
1197 err += w9968cf_write_sb(cam, 0x11 | sda);
1198 /* SDE=1, SDA=sda, SCL=0 */
1199 err += w9968cf_write_sb(cam, 0x10 | sda);
1206 static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
1212 for (bit = 0 ; bit < 8 ; bit++) {
1214 err += w9968cf_write_sb(cam, 0x0013);
1215 *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
1216 err += w9968cf_write_sb(cam, 0x0012);
1223 static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
1227 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1228 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1229 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1235 static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
1239 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1240 sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
1241 err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
1245 DBG(6, "Couldn't receive the ACK")
1253 /* This seems to refresh the communication through the serial bus */
1254 static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
1258 for (j = 1; j <= 10; j++) {
1259 err = w9968cf_write_reg(cam, 0x0020, 0x01);
1260 err += w9968cf_write_reg(cam, 0x0000, 0x01);
1269 /* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
1271 w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
1272 u16 address, u8 subaddress,u8 value)
1274 u16* data = cam->data_buffer;
1277 err += w9968cf_smbus_refresh_bus(cam);
1279 /* Enable SBUS outputs */
1280 err += w9968cf_write_sb(cam, 0x0020);
1282 data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
1283 data[0] |= (address & 0x40) ? 0x4000 : 0x0;
1284 data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
1285 data[1] |= (address & 0x20) ? 0x0150 : 0x0;
1286 data[1] |= (address & 0x10) ? 0x5400 : 0x0;
1287 data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
1288 data[2] |= (address & 0x04) ? 0x0540 : 0x0;
1289 data[2] |= (address & 0x02) ? 0x5000 : 0x0;
1290 data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
1291 data[3] |= (address & 0x01) ? 0x0054 : 0x0;
1293 err += w9968cf_write_fsb(cam, data);
1295 data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
1296 data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
1297 data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
1298 data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
1299 data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
1300 data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
1301 data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
1302 data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
1303 data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
1304 data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
1307 err += w9968cf_write_fsb(cam, data);
1309 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
1310 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
1311 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
1312 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
1313 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
1314 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
1315 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
1316 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
1317 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
1318 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
1321 err += w9968cf_write_fsb(cam, data);
1323 /* Disable SBUS outputs */
1324 err += w9968cf_write_sb(cam, 0x0000);
1327 DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
1328 "value 0x%02X", address, subaddress, value)
1330 DBG(5, "I2C write byte data failed, addr.0x%04X, "
1331 "subaddr.0x%02X, value 0x%02X",
1332 address, subaddress, value)
1338 /* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
1340 w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
1341 u16 address, u8 subaddress,
1346 /* Serial data enable */
1347 err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
1349 err += w9968cf_smbus_start(cam);
1350 err += w9968cf_smbus_write_byte(cam, address);
1351 err += w9968cf_smbus_read_ack(cam);
1352 err += w9968cf_smbus_write_byte(cam, subaddress);
1353 err += w9968cf_smbus_read_ack(cam);
1354 err += w9968cf_smbus_stop(cam);
1355 err += w9968cf_smbus_start(cam);
1356 err += w9968cf_smbus_write_byte(cam, address + 1);
1357 err += w9968cf_smbus_read_ack(cam);
1358 err += w9968cf_smbus_read_byte(cam, value);
1359 err += w9968cf_smbus_write_ack(cam);
1360 err += w9968cf_smbus_stop(cam);
1362 /* Serial data disable */
1363 err += w9968cf_write_sb(cam, 0x0000);
1366 DBG(5, "I2C read byte data done, addr.0x%04X, "
1367 "subaddr.0x%02X, value 0x%02X",
1368 address, subaddress, *value)
1370 DBG(5, "I2C read byte data failed, addr.0x%04X, "
1371 "subaddr.0x%02X, wrong value 0x%02X",
1372 address, subaddress, *value)
1378 /* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
1380 w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
1381 u16 address, u8* value)
1385 /* Serial data enable */
1386 err += w9968cf_write_sb(cam, 0x0013);
1388 err += w9968cf_smbus_start(cam);
1389 err += w9968cf_smbus_write_byte(cam, address + 1);
1390 err += w9968cf_smbus_read_ack(cam);
1391 err += w9968cf_smbus_read_byte(cam, value);
1392 err += w9968cf_smbus_write_ack(cam);
1393 err += w9968cf_smbus_stop(cam);
1395 /* Serial data disable */
1396 err += w9968cf_write_sb(cam, 0x0000);
1399 DBG(5, "I2C read byte done, addr.0x%04X, "
1400 "value 0x%02X", address, *value)
1402 DBG(5, "I2C read byte failed, addr.0x%04X, "
1403 "wrong value 0x%02X", address, *value)
1409 /* SMBus protocol: S Addr Wr [A] Value [A] P */
1411 w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
1412 u16 address, u8 value)
1414 DBG(4, "i2c_write_byte() is an unsupported transfer mode")
1420 /****************************************************************************
1421 * I2C interface to kernel *
1422 ****************************************************************************/
1425 w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1426 unsigned short flags, char read_write, u8 command,
1427 int size, union i2c_smbus_data *data)
1429 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
1430 struct w9968cf_device *cam = to_cam(v4l2_dev);
1434 if (size == I2C_SMBUS_BYTE) {
1435 /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
1438 if (read_write == I2C_SMBUS_WRITE)
1439 err = w9968cf_i2c_adap_write_byte(cam, addr, command);
1440 else if (read_write == I2C_SMBUS_READ)
1441 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1442 err = w9968cf_i2c_adap_read_byte(cam, addr,
1445 if (w9968cf_smbus_refresh_bus(cam)) {
1452 } else if (size == I2C_SMBUS_BYTE_DATA) {
1455 if (read_write == I2C_SMBUS_WRITE)
1456 err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
1457 command, data->byte);
1458 else if (read_write == I2C_SMBUS_READ) {
1459 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1460 err = w9968cf_i2c_adap_read_byte_data(cam,addr,
1461 command, &data->byte);
1463 if (w9968cf_smbus_refresh_bus(cam)) {
1475 DBG(4, "Unsupported I2C transfer mode (%d)", size)
1482 static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
1484 return I2C_FUNC_SMBUS_READ_BYTE |
1485 I2C_FUNC_SMBUS_READ_BYTE_DATA |
1486 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
1490 static int w9968cf_i2c_init(struct w9968cf_device* cam)
1494 static struct i2c_algorithm algo = {
1495 .smbus_xfer = w9968cf_i2c_smbus_xfer,
1496 .functionality = w9968cf_i2c_func,
1499 static struct i2c_adapter adap = {
1500 .id = I2C_HW_SMBUS_W9968CF,
1501 .owner = THIS_MODULE,
1505 memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
1506 strcpy(cam->i2c_adapter.name, "w9968cf");
1507 cam->i2c_adapter.dev.parent = &cam->usbdev->dev;
1508 i2c_set_adapdata(&cam->i2c_adapter, &cam->v4l2_dev);
1510 DBG(6, "Registering I2C adapter with kernel...")
1512 err = i2c_add_adapter(&cam->i2c_adapter);
1514 DBG(1, "Failed to register the I2C adapter")
1516 DBG(5, "I2C adapter registered")
1523 /****************************************************************************
1524 * Helper functions *
1525 ****************************************************************************/
1527 /*--------------------------------------------------------------------------
1528 Turn on the LED on some webcams. A beep should be heard too.
1529 Return 0 on success, a negative number otherwise.
1530 --------------------------------------------------------------------------*/
1531 static int w9968cf_turn_on_led(struct w9968cf_device* cam)
1535 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power-down */
1536 err += w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
1537 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
1538 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* serial bus, SDS high */
1539 err += w9968cf_write_reg(cam, 0x0000, 0x01); /* serial bus, SDS low */
1540 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* ..high 'beep-beep' */
1543 DBG(2, "Couldn't turn on the LED")
1545 DBG(5, "LED turned on")
1551 /*--------------------------------------------------------------------------
1552 Write some registers for the device initialization.
1553 This function is called once on open().
1554 Return 0 on success, a negative number otherwise.
1555 --------------------------------------------------------------------------*/
1556 static int w9968cf_init_chip(struct w9968cf_device* cam)
1558 unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
1560 u0 = y0 + hw_bufsize/2,
1561 v0 = u0 + hw_bufsize/4,
1562 y1 = v0 + hw_bufsize/4,
1563 u1 = y1 + hw_bufsize/2,
1564 v1 = u1 + hw_bufsize/4;
1567 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
1568 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
1570 err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
1571 err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
1573 err += w9968cf_write_reg(cam, y0 & 0xffff, 0x20); /* Y buf.0, low */
1574 err += w9968cf_write_reg(cam, y0 >> 16, 0x21); /* Y buf.0, high */
1575 err += w9968cf_write_reg(cam, u0 & 0xffff, 0x24); /* U buf.0, low */
1576 err += w9968cf_write_reg(cam, u0 >> 16, 0x25); /* U buf.0, high */
1577 err += w9968cf_write_reg(cam, v0 & 0xffff, 0x28); /* V buf.0, low */
1578 err += w9968cf_write_reg(cam, v0 >> 16, 0x29); /* V buf.0, high */
1580 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x22); /* Y buf.1, low */
1581 err += w9968cf_write_reg(cam, y1 >> 16, 0x23); /* Y buf.1, high */
1582 err += w9968cf_write_reg(cam, u1 & 0xffff, 0x26); /* U buf.1, low */
1583 err += w9968cf_write_reg(cam, u1 >> 16, 0x27); /* U buf.1, high */
1584 err += w9968cf_write_reg(cam, v1 & 0xffff, 0x2a); /* V buf.1, low */
1585 err += w9968cf_write_reg(cam, v1 >> 16, 0x2b); /* V buf.1, high */
1587 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x32); /* JPEG buf 0 low */
1588 err += w9968cf_write_reg(cam, y1 >> 16, 0x33); /* JPEG buf 0 high */
1590 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x34); /* JPEG buf 1 low */
1591 err += w9968cf_write_reg(cam, y1 >> 16, 0x35); /* JPEG bug 1 high */
1593 err += w9968cf_write_reg(cam, 0x0000, 0x36);/* JPEG restart interval */
1594 err += w9968cf_write_reg(cam, 0x0804, 0x37);/*JPEG VLE FIFO threshold*/
1595 err += w9968cf_write_reg(cam, 0x0000, 0x38);/* disable hw up-scaling */
1596 err += w9968cf_write_reg(cam, 0x0000, 0x3f); /* JPEG/MCTL test data */
1598 err += w9968cf_set_picture(cam, cam->picture); /* this before */
1599 err += w9968cf_set_window(cam, cam->window);
1602 DBG(1, "Chip initialization failed")
1604 DBG(5, "Chip successfully initialized")
1610 /*--------------------------------------------------------------------------
1611 Return non-zero if the palette is supported, 0 otherwise.
1612 --------------------------------------------------------------------------*/
1613 static inline u16 w9968cf_valid_palette(u16 palette)
1616 while (w9968cf_formatlist[i].palette != 0) {
1617 if (palette == w9968cf_formatlist[i].palette)
1625 /*--------------------------------------------------------------------------
1626 Return the depth corresponding to the given palette.
1627 Palette _must_ be supported !
1628 --------------------------------------------------------------------------*/
1629 static inline u16 w9968cf_valid_depth(u16 palette)
1632 while (w9968cf_formatlist[i].palette != palette)
1635 return w9968cf_formatlist[i].depth;
1639 /*--------------------------------------------------------------------------
1640 Return non-zero if the format requires decompression, 0 otherwise.
1641 --------------------------------------------------------------------------*/
1642 static inline u8 w9968cf_need_decompression(u16 palette)
1645 while (w9968cf_formatlist[i].palette != 0) {
1646 if (palette == w9968cf_formatlist[i].palette)
1647 return w9968cf_formatlist[i].compression;
1654 /*--------------------------------------------------------------------------
1655 Change the picture settings of the camera.
1656 Return 0 on success, a negative number otherwise.
1657 --------------------------------------------------------------------------*/
1659 w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
1661 u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
1664 /* Make sure we are using a valid depth */
1665 pict.depth = w9968cf_valid_depth(pict.palette);
1669 hw_depth = pict.depth; /* depth used by the winbond chip */
1670 hw_palette = pict.palette; /* palette used by the winbond chip */
1672 /* VS & HS polarities */
1673 reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
1677 case VIDEO_PALETTE_UYVY:
1679 cam->vpp_flag = VPP_NONE;
1681 case VIDEO_PALETTE_YUV422P:
1683 cam->vpp_flag = VPP_DECOMPRESSION;
1685 case VIDEO_PALETTE_YUV420:
1686 case VIDEO_PALETTE_YUV420P:
1688 cam->vpp_flag = VPP_DECOMPRESSION;
1690 case VIDEO_PALETTE_YUYV:
1691 case VIDEO_PALETTE_YUV422:
1693 cam->vpp_flag = VPP_SWAP_YUV_BYTES;
1694 hw_palette = VIDEO_PALETTE_UYVY;
1696 /* Original video is used instead of RGBX palettes.
1697 Software conversion later. */
1698 case VIDEO_PALETTE_GREY:
1699 case VIDEO_PALETTE_RGB555:
1700 case VIDEO_PALETTE_RGB565:
1701 case VIDEO_PALETTE_RGB24:
1702 case VIDEO_PALETTE_RGB32:
1703 reg_v |= 0x0000; /* UYVY 16 bit is used */
1705 hw_palette = VIDEO_PALETTE_UYVY;
1706 cam->vpp_flag = VPP_UYVY_TO_RGBX;
1710 /* NOTE: due to memory issues, it is better to disable the hardware
1711 double buffering during compression */
1712 if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
1718 if (cam->filter_type == 1)
1720 else if (cam->filter_type == 2)
1723 if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
1726 if ((err = w9968cf_sensor_update_picture(cam, pict)))
1729 /* If all went well, update the device data structure */
1730 memcpy(&cam->picture, &pict, sizeof(pict));
1731 cam->hw_depth = hw_depth;
1732 cam->hw_palette = hw_palette;
1734 /* Settings changed, so we clear the frame buffers */
1735 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1737 DBG(4, "Palette is %s, depth is %u bpp",
1738 symbolic(v4l1_plist, pict.palette), pict.depth)
1743 DBG(1, "Failed to change picture settings")
1748 /*--------------------------------------------------------------------------
1749 Change the capture area size of the camera.
1750 This function _must_ be called _after_ w9968cf_set_picture().
1751 Return 0 on success, a negative number otherwise.
1752 --------------------------------------------------------------------------*/
1754 w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
1756 u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
1757 unsigned long fw, fh;
1758 struct ovcamchip_window s_win;
1761 /* Work around to avoid FP arithmetics */
1762 #define SC(x) ((x) << 10)
1763 #define UNSC(x) ((x) >> 10)
1765 /* Make sure we are using a supported resolution */
1766 if ((err = w9968cf_adjust_window_size(cam, &win.width, &win.height)))
1769 /* Scaling factors */
1770 fw = SC(win.width) / cam->maxwidth;
1771 fh = SC(win.height) / cam->maxheight;
1773 /* Set up the width and height values used by the chip */
1774 if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
1775 cam->vpp_flag |= VPP_UPSCALE;
1776 /* Calculate largest w,h mantaining the same w/h ratio */
1777 w = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1778 h = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
1779 if (w < cam->minwidth) /* just in case */
1781 if (h < cam->minheight) /* just in case */
1784 cam->vpp_flag &= ~VPP_UPSCALE;
1789 /* x,y offsets of the cropped area */
1790 scx = cam->start_cropx;
1791 scy = cam->start_cropy;
1793 /* Calculate cropped area manteining the right w/h ratio */
1794 if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
1795 cw = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1796 ch = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
1802 /* Setup the window of the sensor */
1803 s_win.format = VIDEO_PALETTE_UYVY;
1804 s_win.width = cam->maxwidth;
1805 s_win.height = cam->maxheight;
1806 s_win.quarter = 0; /* full progressive video */
1809 s_win.x = (s_win.width - cw) / 2;
1810 s_win.y = (s_win.height - ch) / 2;
1813 if (cam->clockdiv >= 0)
1814 s_win.clockdiv = cam->clockdiv; /* manual override */
1816 switch (cam->sensor) {
1829 s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
1832 /* We have to scale win.x and win.y offsets */
1833 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1834 || (cam->vpp_flag & VPP_UPSCALE) ) {
1842 if ((ax + cw) > cam->maxwidth)
1843 ax = cam->maxwidth - cw;
1845 if ((ay + ch) > cam->maxheight)
1846 ay = cam->maxheight - ch;
1848 /* Adjust win.x, win.y */
1849 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1850 || (cam->vpp_flag & VPP_UPSCALE) ) {
1851 win.x = UNSC(ax*fw);
1852 win.y = UNSC(ay*fh);
1858 /* Offsets used by the chip */
1863 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
1866 err += w9968cf_write_reg(cam, scx + x, 0x10);
1867 err += w9968cf_write_reg(cam, scy + y, 0x11);
1868 err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
1869 err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
1870 err += w9968cf_write_reg(cam, w, 0x14);
1871 err += w9968cf_write_reg(cam, h, 0x15);
1873 /* JPEG width & height */
1874 err += w9968cf_write_reg(cam, w, 0x30);
1875 err += w9968cf_write_reg(cam, h, 0x31);
1877 /* Y & UV frame buffer strides (in WORD) */
1878 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1879 err += w9968cf_write_reg(cam, w/2, 0x2c);
1880 err += w9968cf_write_reg(cam, w/4, 0x2d);
1882 err += w9968cf_write_reg(cam, w, 0x2c);
1887 /* If all went well, update the device data structure */
1888 memcpy(&cam->window, &win, sizeof(win));
1892 /* Settings changed, so we clear the frame buffers */
1893 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1895 DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
1896 win.width, win.height, win.x, win.y)
1898 PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
1899 "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
1900 x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
1901 win.width, win.height)
1906 DBG(1, "Failed to change the capture area size")
1911 /*--------------------------------------------------------------------------
1912 Adjust the asked values for window width and height.
1913 Return 0 on success, -1 otherwise.
1914 --------------------------------------------------------------------------*/
1916 w9968cf_adjust_window_size(struct w9968cf_device *cam, u32 *width, u32 *height)
1918 unsigned int maxw, maxh, align;
1920 maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1921 w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
1923 maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1924 w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
1926 align = (cam->vpp_flag & VPP_DECOMPRESSION) ? 4 : 0;
1928 v4l_bound_align_image(width, cam->minwidth, maxw, align,
1929 height, cam->minheight, maxh, align, 0);
1931 PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
1937 /*--------------------------------------------------------------------------
1938 Initialize the FIFO list of requested frames.
1939 --------------------------------------------------------------------------*/
1940 static void w9968cf_init_framelist(struct w9968cf_device* cam)
1944 for (i = 0; i < cam->nbuffers; i++) {
1945 cam->requested_frame[i] = NULL;
1946 cam->frame[i].queued = 0;
1947 cam->frame[i].status = F_UNUSED;
1952 /*--------------------------------------------------------------------------
1953 Add a frame in the FIFO list of requested frames.
1954 This function is called in process context.
1955 --------------------------------------------------------------------------*/
1956 static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
1959 unsigned long lock_flags;
1961 spin_lock_irqsave(&cam->flist_lock, lock_flags);
1963 for (f=0; cam->requested_frame[f] != NULL; f++);
1964 cam->requested_frame[f] = &cam->frame[f_num];
1965 cam->frame[f_num].queued = 1;
1966 cam->frame[f_num].status = F_UNUSED; /* clear the status */
1968 spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
1970 DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
1974 /*--------------------------------------------------------------------------
1975 Read, store and remove the first pointer in the FIFO list of requested
1976 frames. This function is called in interrupt context.
1977 --------------------------------------------------------------------------*/
1979 w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
1983 spin_lock(&cam->flist_lock);
1985 *framep = cam->requested_frame[0];
1987 /* Shift the list of pointers */
1988 for (i = 0; i < cam->nbuffers-1; i++)
1989 cam->requested_frame[i] = cam->requested_frame[i+1];
1990 cam->requested_frame[i] = NULL;
1992 spin_unlock(&cam->flist_lock);
1994 DBG(6,"Popped frame #%d from the list", (*framep)->number)
1998 /*--------------------------------------------------------------------------
1999 High-level video post-processing routine on grabbed frames.
2000 Return 0 on success, a negative number otherwise.
2001 --------------------------------------------------------------------------*/
2003 w9968cf_postprocess_frame(struct w9968cf_device* cam,
2004 struct w9968cf_frame_t* fr)
2006 void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
2007 u16 w = cam->window.width,
2008 h = cam->window.height,
2009 d = cam->picture.depth,
2010 fmt = cam->picture.palette,
2011 rgb = cam->force_rgb,
2012 hw_w = cam->hw_width,
2013 hw_h = cam->hw_height,
2014 hw_d = cam->hw_depth;
2017 #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
2019 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2020 memcpy(pOut, pIn, fr->length);
2022 err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
2023 PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
2024 fr->length = (hw_w*hw_h*hw_d)/8;
2027 DBG(4, "An error occurred while decoding the frame: "
2028 "%s", symbolic(decoder_errlist, err))
2031 DBG(6, "Frame decoded")
2034 if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
2035 w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
2036 DBG(6, "Original UYVY component ordering changed")
2039 if (cam->vpp_flag & VPP_UPSCALE) {
2040 w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
2041 fr->length = (w*h*hw_d)/8;
2043 DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
2044 hw_w, hw_h, hw_d, w, h)
2047 if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
2048 w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
2049 fr->length = (w*h*d)/8;
2051 DBG(6, "UYVY-16bit to %s conversion done",
2052 symbolic(v4l1_plist, fmt))
2055 if (pOut == fr->buffer)
2056 memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
2063 /****************************************************************************
2064 * Image sensor control routines *
2065 ****************************************************************************/
2068 w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
2070 struct ovcamchip_control ctl;
2076 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
2083 w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
2085 struct ovcamchip_control ctl;
2090 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
2099 w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
2103 rc = v4l2_subdev_call(cam->sensor_sd, core, ioctl, cmd, arg);
2104 /* The I2C driver returns -EPERM on non-supported controls */
2105 return (rc < 0 && rc != -EPERM) ? rc : 0;
2109 /*--------------------------------------------------------------------------
2110 Update some settings of the image sensor.
2111 Returns: 0 on success, a negative number otherwise.
2112 --------------------------------------------------------------------------*/
2113 static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
2117 /* Auto brightness */
2118 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
2124 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
2129 /* Banding filter */
2130 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
2135 /* Light frequency */
2136 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
2142 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
2148 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
2157 /*--------------------------------------------------------------------------
2158 Get some current picture settings from the image sensor and update the
2159 internal 'picture' structure of the camera.
2160 Returns: 0 on success, a negative number otherwise.
2161 --------------------------------------------------------------------------*/
2162 static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
2166 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
2169 cam->picture.contrast = v;
2171 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
2174 cam->picture.brightness = v;
2176 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
2179 cam->picture.colour = v;
2181 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
2184 cam->picture.hue = v;
2186 DBG(5, "Got picture settings from the image sensor")
2188 PDBGG("Brightness, contrast, hue, colour, whiteness are "
2189 "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
2190 cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
2196 /*--------------------------------------------------------------------------
2197 Update picture settings of the image sensor.
2198 Returns: 0 on success, a negative number otherwise.
2199 --------------------------------------------------------------------------*/
2201 w9968cf_sensor_update_picture(struct w9968cf_device* cam,
2202 struct video_picture pict)
2206 if ((!cam->sensor_initialized)
2207 || pict.contrast != cam->picture.contrast) {
2208 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
2212 DBG(4, "Contrast changed from %u to %u",
2213 cam->picture.contrast, pict.contrast)
2214 cam->picture.contrast = pict.contrast;
2217 if (((!cam->sensor_initialized) ||
2218 pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
2219 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
2223 DBG(4, "Brightness changed from %u to %u",
2224 cam->picture.brightness, pict.brightness)
2225 cam->picture.brightness = pict.brightness;
2228 if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
2229 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
2233 DBG(4, "Colour changed from %u to %u",
2234 cam->picture.colour, pict.colour)
2235 cam->picture.colour = pict.colour;
2238 if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
2239 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
2243 DBG(4, "Hue changed from %u to %u",
2244 cam->picture.hue, pict.hue)
2245 cam->picture.hue = pict.hue;
2251 DBG(4, "Failed to change sensor picture setting")
2257 /****************************************************************************
2258 * Camera configuration *
2259 ****************************************************************************/
2261 /*--------------------------------------------------------------------------
2262 This function is called when a supported image sensor is detected.
2263 Return 0 if the initialization succeeds, a negative number otherwise.
2264 --------------------------------------------------------------------------*/
2265 static int w9968cf_sensor_init(struct w9968cf_device* cam)
2269 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
2273 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
2277 /* NOTE: Make sure width and height are a multiple of 16 */
2278 switch (v4l2_i2c_subdev_addr(cam->sensor_sd)) {
2280 cam->maxwidth = 352;
2281 cam->maxheight = 288;
2283 cam->minheight = 48;
2286 cam->maxwidth = 640;
2287 cam->maxheight = 480;
2289 cam->minheight = 48;
2292 DBG(1, "Not supported image sensor detected for %s",
2293 symbolic(camlist, cam->id))
2297 /* These values depend on the ones in the ovxxx0.c sources */
2298 switch (cam->sensor) {
2300 cam->start_cropx = 287;
2301 cam->start_cropy = 35;
2302 /* Seems to work around a bug in the image sensor */
2303 cam->vs_polarity = 1;
2304 cam->hs_polarity = 1;
2307 cam->start_cropx = 320;
2308 cam->start_cropy = 35;
2309 cam->vs_polarity = 1;
2310 cam->hs_polarity = 0;
2313 if ((err = w9968cf_sensor_update_settings(cam)))
2316 if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
2319 cam->sensor_initialized = 1;
2321 DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
2325 cam->sensor_initialized = 0;
2326 cam->sensor = CC_UNKNOWN;
2327 DBG(1, "Image sensor initialization failed for %s (/dev/video%d). "
2328 "Try to detach and attach this device again",
2329 symbolic(camlist, cam->id), cam->v4ldev->num)
2334 /*--------------------------------------------------------------------------
2335 Fill some basic fields in the main device data structure.
2336 This function is called once on w9968cf_usb_probe() for each recognized
2338 --------------------------------------------------------------------------*/
2340 w9968cf_configure_camera(struct w9968cf_device* cam,
2341 struct usb_device* udev,
2342 enum w9968cf_model_id mod_id,
2343 const unsigned short dev_nr)
2345 mutex_init(&cam->fileop_mutex);
2346 init_waitqueue_head(&cam->open);
2347 spin_lock_init(&cam->urb_lock);
2348 spin_lock_init(&cam->flist_lock);
2351 cam->disconnected = 0;
2353 cam->sensor = CC_UNKNOWN;
2354 cam->sensor_initialized = 0;
2356 /* Calculate the alternate setting number (from 1 to 16)
2357 according to the 'packet_size' module parameter */
2358 if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
2359 packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
2360 for (cam->altsetting = 1;
2361 packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
2364 cam->max_buffers = (max_buffers[dev_nr] < 2 ||
2365 max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
2366 ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
2368 cam->double_buffer = (double_buffer[dev_nr] == 0 ||
2369 double_buffer[dev_nr] == 1)
2370 ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
2372 cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
2373 ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
2375 cam->filter_type = (filter_type[dev_nr] == 0 ||
2376 filter_type[dev_nr] == 1 ||
2377 filter_type[dev_nr] == 2)
2378 ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
2382 cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
2383 ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
2385 cam->decompression = (decompression[dev_nr] == 0 ||
2386 decompression[dev_nr] == 1 ||
2387 decompression[dev_nr] == 2)
2388 ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
2390 cam->upscaling = (upscaling[dev_nr] == 0 ||
2391 upscaling[dev_nr] == 1)
2392 ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
2394 cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
2395 ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
2397 cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
2398 ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
2400 cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
2401 ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
2403 cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
2404 bandingfilter[dev_nr] == 1)
2405 ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
2407 cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
2408 ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
2410 cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
2411 ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
2413 cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
2414 ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
2416 cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
2417 ? monochrome[dev_nr] : W9968CF_MONOCHROME;
2419 cam->picture.brightness = (u16)brightness[dev_nr];
2420 cam->picture.hue = (u16)hue[dev_nr];
2421 cam->picture.colour = (u16)colour[dev_nr];
2422 cam->picture.contrast = (u16)contrast[dev_nr];
2423 cam->picture.whiteness = (u16)whiteness[dev_nr];
2424 if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
2425 cam->picture.palette = (u16)force_palette[dev_nr];
2426 cam->force_palette = 1;
2428 cam->force_palette = 0;
2429 if (cam->decompression == 0)
2430 cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
2431 else if (cam->decompression == 1)
2432 cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
2434 cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
2436 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2438 cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
2439 ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
2443 cam->window.width = W9968CF_WIDTH;
2444 cam->window.height = W9968CF_HEIGHT;
2445 cam->window.chromakey = 0;
2446 cam->window.clipcount = 0;
2447 cam->window.flags = 0;
2449 DBG(3, "%s configured with settings #%u:",
2450 symbolic(camlist, cam->id), dev_nr)
2452 DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
2453 wMaxPacketSize[cam->altsetting-1])
2455 DBG(3, "- Number of requested video frame buffers: %u",
2458 if (cam->double_buffer)
2459 DBG(3, "- Hardware double buffering enabled")
2461 DBG(3, "- Hardware double buffering disabled")
2463 if (cam->filter_type == 0)
2464 DBG(3, "- Video filtering disabled")
2465 else if (cam->filter_type == 1)
2466 DBG(3, "- Video filtering enabled: type 1-2-1")
2467 else if (cam->filter_type == 2)
2468 DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
2471 DBG(3, "- Video data clamping (CCIR-601 format) enabled")
2473 DBG(3, "- Video data clamping (CCIR-601 format) disabled")
2476 DBG(3, "- Large view enabled")
2478 DBG(3, "- Large view disabled")
2480 if ((cam->decompression) == 0 && (!cam->force_palette))
2481 DBG(3, "- Decompression disabled")
2482 else if ((cam->decompression) == 1 && (!cam->force_palette))
2483 DBG(3, "- Decompression forced")
2484 else if ((cam->decompression) == 2 && (!cam->force_palette))
2485 DBG(3, "- Decompression allowed")
2488 DBG(3, "- Software image scaling enabled")
2490 DBG(3, "- Software image scaling disabled")
2492 if (cam->force_palette)
2493 DBG(3, "- Image palette forced to %s",
2494 symbolic(v4l1_plist, cam->picture.palette))
2497 DBG(3, "- RGB component ordering will be used instead of BGR")
2500 DBG(3, "- Auto brightness enabled")
2502 DBG(3, "- Auto brightness disabled")
2505 DBG(3, "- Auto exposure enabled")
2507 DBG(3, "- Auto exposure disabled")
2510 DBG(3, "- Backlight exposure algorithm enabled")
2512 DBG(3, "- Backlight exposure algorithm disabled")
2515 DBG(3, "- Mirror enabled")
2517 DBG(3, "- Mirror disabled")
2520 DBG(3, "- Banding filter enabled")
2522 DBG(3, "- Banding filter disabled")
2524 DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
2526 if (cam->clockdiv == -1)
2527 DBG(3, "- Automatic clock divisor enabled")
2529 DBG(3, "- Clock divisor: %d", cam->clockdiv)
2531 if (cam->monochrome)
2532 DBG(3, "- Image sensor used as monochrome")
2534 DBG(3, "- Image sensor not used as monochrome")
2538 /*--------------------------------------------------------------------------
2539 If the video post-processing module is not loaded, some parameters
2541 --------------------------------------------------------------------------*/
2542 static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2545 if (cam->decompression == 1) {
2546 cam->decompression = 2;
2547 DBG(2, "Video post-processing module not found: "
2548 "'decompression' parameter forced to 2")
2550 if (cam->upscaling) {
2552 DBG(2, "Video post-processing module not found: "
2553 "'upscaling' parameter forced to 0")
2555 if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
2556 cam->force_palette = 0;
2557 DBG(2, "Video post-processing module not found: "
2558 "'force_palette' parameter forced to 0")
2560 cam->picture.palette = VIDEO_PALETTE_UYVY;
2561 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2566 /*--------------------------------------------------------------------------
2567 Release the resources used by the driver.
2568 This function is called on disconnect
2569 (or on close if deallocation has been deferred)
2570 --------------------------------------------------------------------------*/
2571 static void w9968cf_release_resources(struct w9968cf_device* cam)
2573 mutex_lock(&w9968cf_devlist_mutex);
2575 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->num)
2577 video_unregister_device(cam->v4ldev);
2578 list_del(&cam->v4llist);
2579 i2c_del_adapter(&cam->i2c_adapter);
2580 w9968cf_deallocate_memory(cam);
2581 kfree(cam->control_buffer);
2582 kfree(cam->data_buffer);
2583 v4l2_device_unregister(&cam->v4l2_dev);
2585 mutex_unlock(&w9968cf_devlist_mutex);
2590 /****************************************************************************
2591 * Video4Linux interface *
2592 ****************************************************************************/
2594 static int w9968cf_open(struct file *filp)
2596 struct w9968cf_device* cam;
2599 /* This the only safe way to prevent race conditions with disconnect */
2600 if (!down_read_trylock(&w9968cf_disconnect))
2603 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2605 mutex_lock(&cam->dev_mutex);
2607 if (cam->sensor == CC_UNKNOWN) {
2608 DBG(2, "No supported image sensor has been detected by the "
2609 "'ovcamchip' module for the %s (/dev/video%d). Make "
2610 "sure it is loaded *before* (re)connecting the camera.",
2611 symbolic(camlist, cam->id), cam->v4ldev->num)
2612 mutex_unlock(&cam->dev_mutex);
2613 up_read(&w9968cf_disconnect);
2618 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
2619 symbolic(camlist, cam->id), cam->v4ldev->num, cam->command)
2620 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
2621 mutex_unlock(&cam->dev_mutex);
2622 up_read(&w9968cf_disconnect);
2623 return -EWOULDBLOCK;
2625 mutex_unlock(&cam->dev_mutex);
2626 err = wait_event_interruptible_exclusive(cam->open,
2627 cam->disconnected ||
2630 up_read(&w9968cf_disconnect);
2633 if (cam->disconnected) {
2634 up_read(&w9968cf_disconnect);
2637 mutex_lock(&cam->dev_mutex);
2640 DBG(5, "Opening '%s', /dev/video%d ...",
2641 symbolic(camlist, cam->id), cam->v4ldev->num)
2644 cam->misconfigured = 0;
2646 w9968cf_adjust_configuration(cam);
2648 if ((err = w9968cf_allocate_memory(cam)))
2649 goto deallocate_memory;
2651 if ((err = w9968cf_init_chip(cam)))
2652 goto deallocate_memory;
2654 if ((err = w9968cf_start_transfer(cam)))
2655 goto deallocate_memory;
2657 filp->private_data = cam;
2660 strcpy(cam->command, current->comm);
2662 init_waitqueue_head(&cam->wait_queue);
2664 DBG(5, "Video device is open")
2666 mutex_unlock(&cam->dev_mutex);
2667 up_read(&w9968cf_disconnect);
2672 w9968cf_deallocate_memory(cam);
2673 DBG(2, "Failed to open the video device")
2674 mutex_unlock(&cam->dev_mutex);
2675 up_read(&w9968cf_disconnect);
2680 static int w9968cf_release(struct file *filp)
2682 struct w9968cf_device* cam;
2684 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2686 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
2688 w9968cf_stop_transfer(cam);
2690 if (cam->disconnected) {
2691 w9968cf_release_resources(cam);
2692 mutex_unlock(&cam->dev_mutex);
2698 w9968cf_deallocate_memory(cam);
2699 wake_up_interruptible_nr(&cam->open, 1);
2701 DBG(5, "Video device closed")
2702 mutex_unlock(&cam->dev_mutex);
2708 w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2710 struct w9968cf_device* cam;
2711 struct w9968cf_frame_t* fr;
2714 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2716 if (filp->f_flags & O_NONBLOCK)
2717 return -EWOULDBLOCK;
2719 if (mutex_lock_interruptible(&cam->fileop_mutex))
2720 return -ERESTARTSYS;
2722 if (cam->disconnected) {
2723 DBG(2, "Device not present")
2724 mutex_unlock(&cam->fileop_mutex);
2728 if (cam->misconfigured) {
2729 DBG(2, "The camera is misconfigured. Close and open it again.")
2730 mutex_unlock(&cam->fileop_mutex);
2734 if (!cam->frame[0].queued)
2735 w9968cf_push_frame(cam, 0);
2737 if (!cam->frame[1].queued)
2738 w9968cf_push_frame(cam, 1);
2740 err = wait_event_interruptible(cam->wait_queue,
2741 cam->frame[0].status == F_READY ||
2742 cam->frame[1].status == F_READY ||
2745 mutex_unlock(&cam->fileop_mutex);
2748 if (cam->disconnected) {
2749 mutex_unlock(&cam->fileop_mutex);
2753 fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
2756 w9968cf_postprocess_frame(cam, fr);
2758 if (count > fr->length)
2761 if (copy_to_user(buf, fr->buffer, count)) {
2762 fr->status = F_UNUSED;
2763 mutex_unlock(&cam->fileop_mutex);
2768 fr->status = F_UNUSED;
2770 DBG(5, "%zu bytes read", count)
2772 mutex_unlock(&cam->fileop_mutex);
2777 static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
2779 struct w9968cf_device* cam = (struct w9968cf_device*)
2780 video_get_drvdata(video_devdata(filp));
2781 unsigned long vsize = vma->vm_end - vma->vm_start,
2782 psize = cam->nbuffers * cam->frame[0].size,
2783 start = vma->vm_start,
2784 pos = (unsigned long)cam->frame[0].buffer,
2787 if (cam->disconnected) {
2788 DBG(2, "Device not present")
2792 if (cam->misconfigured) {
2793 DBG(2, "The camera is misconfigured. Close and open it again")
2797 PDBGG("mmapping %lu bytes...", vsize)
2799 if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
2803 page = vmalloc_to_pfn((void *)pos);
2804 if (remap_pfn_range(vma, start, page + vma->vm_pgoff,
2805 PAGE_SIZE, vma->vm_page_prot))
2812 DBG(5, "mmap method successfully called")
2818 w9968cf_ioctl(struct file *filp,
2819 unsigned int cmd, unsigned long arg)
2821 struct w9968cf_device* cam;
2824 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2826 if (mutex_lock_interruptible(&cam->fileop_mutex))
2827 return -ERESTARTSYS;
2829 if (cam->disconnected) {
2830 DBG(2, "Device not present")
2831 mutex_unlock(&cam->fileop_mutex);
2835 if (cam->misconfigured) {
2836 DBG(2, "The camera is misconfigured. Close and open it again.")
2837 mutex_unlock(&cam->fileop_mutex);
2841 err = w9968cf_v4l_ioctl(filp, cmd, (void __user *)arg);
2843 mutex_unlock(&cam->fileop_mutex);
2848 static long w9968cf_v4l_ioctl(struct file *filp,
2849 unsigned int cmd, void __user *arg)
2851 struct w9968cf_device* cam;
2852 const char* v4l1_ioctls[] = {
2853 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
2854 "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
2855 "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
2856 "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
2857 "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
2858 "GVBIFMT", "SVBIFMT"
2861 #define V4L1_IOCTL(cmd) \
2862 ((_IOC_NR((cmd)) < ARRAY_SIZE(v4l1_ioctls)) ? \
2863 v4l1_ioctls[_IOC_NR((cmd))] : "?")
2865 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2869 case VIDIOCGCAP: /* get video capability */
2871 struct video_capability cap = {
2872 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
2875 .minwidth = cam->minwidth,
2876 .minheight = cam->minheight,
2878 sprintf(cap.name, "W996[87]CF USB Camera #%d",
2880 cap.maxwidth = (cam->upscaling && w9968cf_vpp)
2881 ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2883 cap.maxheight = (cam->upscaling && w9968cf_vpp)
2884 ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2887 if (copy_to_user(arg, &cap, sizeof(cap)))
2890 DBG(5, "VIDIOCGCAP successfully called")
2894 case VIDIOCGCHAN: /* get video channel informations */
2896 struct video_channel chan;
2897 if (copy_from_user(&chan, arg, sizeof(chan)))
2900 if (chan.channel != 0)
2903 strcpy(chan.name, "Camera");
2906 chan.type = VIDEO_TYPE_CAMERA;
2907 chan.norm = VIDEO_MODE_AUTO;
2909 if (copy_to_user(arg, &chan, sizeof(chan)))
2912 DBG(5, "VIDIOCGCHAN successfully called")
2916 case VIDIOCSCHAN: /* set active channel */
2918 struct video_channel chan;
2920 if (copy_from_user(&chan, arg, sizeof(chan)))
2923 if (chan.channel != 0)
2926 DBG(5, "VIDIOCSCHAN successfully called")
2930 case VIDIOCGPICT: /* get image properties of the picture */
2932 if (w9968cf_sensor_get_picture(cam))
2935 if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
2938 DBG(5, "VIDIOCGPICT successfully called")
2942 case VIDIOCSPICT: /* change picture settings */
2944 struct video_picture pict;
2947 if (copy_from_user(&pict, arg, sizeof(pict)))
2950 if ( (cam->force_palette || !w9968cf_vpp)
2951 && pict.palette != cam->picture.palette ) {
2952 DBG(4, "Palette %s rejected: only %s is allowed",
2953 symbolic(v4l1_plist, pict.palette),
2954 symbolic(v4l1_plist, cam->picture.palette))
2958 if (!w9968cf_valid_palette(pict.palette)) {
2959 DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
2960 symbolic(v4l1_plist, pict.palette))
2964 if (!cam->force_palette) {
2965 if (cam->decompression == 0) {
2966 if (w9968cf_need_decompression(pict.palette)) {
2967 DBG(4, "Decompression disabled: palette %s is not "
2968 "allowed. VIDIOCSPICT failed",
2969 symbolic(v4l1_plist, pict.palette))
2972 } else if (cam->decompression == 1) {
2973 if (!w9968cf_need_decompression(pict.palette)) {
2974 DBG(4, "Decompression forced: palette %s is not "
2975 "allowed. VIDIOCSPICT failed",
2976 symbolic(v4l1_plist, pict.palette))
2982 if (pict.depth != w9968cf_valid_depth(pict.palette)) {
2983 DBG(4, "Requested depth %u bpp is not valid for %s "
2984 "palette: ignored and changed to %u bpp",
2985 pict.depth, symbolic(v4l1_plist, pict.palette),
2986 w9968cf_valid_depth(pict.palette))
2987 pict.depth = w9968cf_valid_depth(pict.palette);
2990 if (pict.palette != cam->picture.palette) {
2991 if(*cam->requested_frame
2992 || cam->frame_current->queued) {
2993 err = wait_event_interruptible
2995 cam->disconnected ||
2996 (!*cam->requested_frame &&
2997 !cam->frame_current->queued) );
3000 if (cam->disconnected)
3004 if (w9968cf_stop_transfer(cam))
3007 if (w9968cf_set_picture(cam, pict))
3010 if (w9968cf_start_transfer(cam))
3013 } else if (w9968cf_sensor_update_picture(cam, pict))
3017 DBG(5, "VIDIOCSPICT successfully called")
3021 case VIDIOCSWIN: /* set capture area */
3023 struct video_window win;
3026 if (copy_from_user(&win, arg, sizeof(win)))
3029 DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
3030 "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
3031 win.x, win.y, win.width, win.height)
3033 if (win.clipcount != 0 || win.flags != 0)
3036 if ((err = w9968cf_adjust_window_size(cam, &win.width,
3038 DBG(4, "Resolution not supported (%ux%u). "
3039 "VIDIOCSWIN failed", win.width, win.height)
3043 if (win.x != cam->window.x ||
3044 win.y != cam->window.y ||
3045 win.width != cam->window.width ||
3046 win.height != cam->window.height) {
3047 if(*cam->requested_frame
3048 || cam->frame_current->queued) {
3049 err = wait_event_interruptible
3051 cam->disconnected ||
3052 (!*cam->requested_frame &&
3053 !cam->frame_current->queued) );
3056 if (cam->disconnected)
3060 if (w9968cf_stop_transfer(cam))
3063 /* This _must_ be called before set_window() */
3064 if (w9968cf_set_picture(cam, cam->picture))
3067 if (w9968cf_set_window(cam, win))
3070 if (w9968cf_start_transfer(cam))
3074 DBG(5, "VIDIOCSWIN successfully called. ")
3078 case VIDIOCGWIN: /* get current window properties */
3080 if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
3083 DBG(5, "VIDIOCGWIN successfully called")
3087 case VIDIOCGMBUF: /* request for memory (mapped) buffer */
3089 struct video_mbuf mbuf;
3092 mbuf.size = cam->nbuffers * cam->frame[0].size;
3093 mbuf.frames = cam->nbuffers;
3094 for (i = 0; i < cam->nbuffers; i++)
3095 mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
3096 (unsigned long)cam->frame[0].buffer;
3098 if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
3101 DBG(5, "VIDIOCGMBUF successfully called")
3105 case VIDIOCMCAPTURE: /* start the capture to a frame */
3107 struct video_mmap mmap;
3108 struct w9968cf_frame_t* fr;
3112 if (copy_from_user(&mmap, arg, sizeof(mmap)))
3115 DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
3116 mmap.frame, symbolic(v4l1_plist, mmap.format),
3117 mmap.width, mmap.height)
3119 if (mmap.frame >= cam->nbuffers) {
3120 DBG(4, "Invalid frame number (%u). "
3121 "VIDIOCMCAPTURE failed", mmap.frame)
3125 if (mmap.format!=cam->picture.palette &&
3126 (cam->force_palette || !w9968cf_vpp)) {
3127 DBG(4, "Palette %s rejected: only %s is allowed",
3128 symbolic(v4l1_plist, mmap.format),
3129 symbolic(v4l1_plist, cam->picture.palette))
3133 if (!w9968cf_valid_palette(mmap.format)) {
3134 DBG(4, "Palette %s not supported. "
3135 "VIDIOCMCAPTURE failed",
3136 symbolic(v4l1_plist, mmap.format))
3140 if (!cam->force_palette) {
3141 if (cam->decompression == 0) {
3142 if (w9968cf_need_decompression(mmap.format)) {
3143 DBG(4, "Decompression disabled: palette %s is not "
3144 "allowed. VIDIOCSPICT failed",
3145 symbolic(v4l1_plist, mmap.format))
3148 } else if (cam->decompression == 1) {
3149 if (!w9968cf_need_decompression(mmap.format)) {
3150 DBG(4, "Decompression forced: palette %s is not "
3151 "allowed. VIDIOCSPICT failed",
3152 symbolic(v4l1_plist, mmap.format))
3158 w = mmap.width; h = mmap.height;
3159 err = w9968cf_adjust_window_size(cam, &w, &h);
3160 mmap.width = w; mmap.height = h;
3162 DBG(4, "Resolution not supported (%dx%d). "
3163 "VIDIOCMCAPTURE failed",
3164 mmap.width, mmap.height)
3168 fr = &cam->frame[mmap.frame];
3170 if (mmap.width != cam->window.width ||
3171 mmap.height != cam->window.height ||
3172 mmap.format != cam->picture.palette) {
3174 struct video_window win;
3175 struct video_picture pict;
3177 if(*cam->requested_frame
3178 || cam->frame_current->queued) {
3179 DBG(6, "VIDIOCMCAPTURE. Change settings for "
3180 "frame #%u: %dx%d, format %s. Wait...",
3181 mmap.frame, mmap.width, mmap.height,
3182 symbolic(v4l1_plist, mmap.format))
3183 err = wait_event_interruptible
3185 cam->disconnected ||
3186 (!*cam->requested_frame &&
3187 !cam->frame_current->queued) );
3190 if (cam->disconnected)
3194 memcpy(&win, &cam->window, sizeof(win));
3195 memcpy(&pict, &cam->picture, sizeof(pict));
3196 win.width = mmap.width;
3197 win.height = mmap.height;
3198 pict.palette = mmap.format;
3200 if (w9968cf_stop_transfer(cam))
3203 /* This before set_window */
3204 if (w9968cf_set_picture(cam, pict))
3207 if (w9968cf_set_window(cam, win))
3210 if (w9968cf_start_transfer(cam))
3213 } else if (fr->queued) {
3215 DBG(6, "Wait until frame #%u is free", mmap.frame)
3217 err = wait_event_interruptible(cam->wait_queue,
3218 cam->disconnected ||
3222 if (cam->disconnected)
3226 w9968cf_push_frame(cam, mmap.frame);
3227 DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
3231 case VIDIOCSYNC: /* wait until the capture of a frame is finished */
3234 struct w9968cf_frame_t* fr;
3237 if (copy_from_user(&f_num, arg, sizeof(f_num)))
3240 if (f_num >= cam->nbuffers) {
3241 DBG(4, "Invalid frame number (%u). "
3242 "VIDIOCMCAPTURE failed", f_num)
3246 DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
3248 fr = &cam->frame[f_num];
3250 switch (fr->status) {
3253 DBG(4, "VIDIOSYNC: Frame #%u not requested!",
3259 err = wait_event_interruptible(cam->wait_queue,
3260 (fr->status == F_READY)
3261 || cam->disconnected);
3264 if (cam->disconnected)
3272 w9968cf_postprocess_frame(cam, fr);
3274 fr->status = F_UNUSED;
3276 DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
3280 case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
3282 struct video_unit unit = {
3283 .video = cam->v4ldev->minor,
3284 .vbi = VIDEO_NO_UNIT,
3285 .radio = VIDEO_NO_UNIT,
3286 .audio = VIDEO_NO_UNIT,
3287 .teletext = VIDEO_NO_UNIT,
3290 if (copy_to_user(arg, &unit, sizeof(unit)))
3293 DBG(5, "VIDIOCGUNIT successfully called")
3302 if (clear_user(arg, sizeof(struct video_buffer)))
3305 DBG(5, "VIDIOCGFBUF successfully called")
3311 struct video_tuner tuner;
3312 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3315 if (tuner.tuner != 0)
3318 strcpy(tuner.name, "no_tuner");
3320 tuner.rangehigh = 0;
3321 tuner.flags = VIDEO_TUNER_NORM;
3322 tuner.mode = VIDEO_MODE_AUTO;
3323 tuner.signal = 0xffff;
3325 if (copy_to_user(arg, &tuner, sizeof(tuner)))
3328 DBG(5, "VIDIOCGTUNER successfully called")
3334 struct video_tuner tuner;
3335 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3338 if (tuner.tuner != 0)
3341 if (tuner.mode != VIDEO_MODE_AUTO)
3344 DBG(5, "VIDIOCSTUNER successfully called")
3354 case VIDIOCSPLAYMODE:
3355 case VIDIOCSWRITEMODE:
3356 case VIDIOCGPLAYINFO:
3357 case VIDIOCSMICROCODE:
3360 DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
3366 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3371 DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
3377 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3379 return -ENOIOCTLCMD;
3381 } /* end of switch */
3384 cam->misconfigured = 1;
3385 DBG(1, "VIDIOC%s failed because of hardware problems. "
3386 "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
3391 static const struct v4l2_file_operations w9968cf_fops = {
3392 .owner = THIS_MODULE,
3393 .open = w9968cf_open,
3394 .release = w9968cf_release,
3395 .read = w9968cf_read,
3396 .ioctl = w9968cf_ioctl,
3397 .mmap = w9968cf_mmap,
3402 /****************************************************************************
3403 * USB probe and V4L registration, disconnect and id_table[] definition *
3404 ****************************************************************************/
3407 w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3409 struct usb_device *udev = interface_to_usbdev(intf);
3410 struct w9968cf_device* cam;
3412 enum w9968cf_model_id mod_id;
3413 struct list_head* ptr;
3414 u8 sc = 0; /* number of simultaneous cameras */
3415 static unsigned short dev_nr; /* 0 - we are handling device number n */
3416 static unsigned short addrs[] = {
3422 if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[0].idVendor &&
3423 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[0].idProduct)
3424 mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
3425 else if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[1].idVendor &&
3426 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[1].idProduct)
3427 mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
3431 cam = (struct w9968cf_device*)
3432 kzalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
3436 err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
3440 mutex_init(&cam->dev_mutex);
3441 mutex_lock(&cam->dev_mutex);
3445 DBG(2, "%s detected", symbolic(camlist, mod_id))
3447 if (simcams > W9968CF_MAX_DEVICES)
3448 simcams = W9968CF_SIMCAMS;
3450 /* How many cameras are connected ? */
3451 mutex_lock(&w9968cf_devlist_mutex);
3452 list_for_each(ptr, &w9968cf_dev_list)
3454 mutex_unlock(&w9968cf_devlist_mutex);
3456 if (sc >= simcams) {
3457 DBG(2, "Device rejected: too many connected cameras "
3458 "(max. %u)", simcams)
3464 /* Allocate 2 bytes of memory for camera control USB transfers */
3465 if (!(cam->control_buffer = kzalloc(2, GFP_KERNEL))) {
3466 DBG(1,"Couldn't allocate memory for camera control transfers")
3471 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
3472 if (!(cam->data_buffer = kzalloc(8, GFP_KERNEL))) {
3473 DBG(1, "Couldn't allocate memory for data "
3474 "transfers to the FSB")
3479 /* Register the V4L device */
3480 cam->v4ldev = video_device_alloc();
3482 DBG(1, "Could not allocate memory for a V4L structure")
3487 strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
3488 cam->v4ldev->fops = &w9968cf_fops;
3489 cam->v4ldev->minor = video_nr[dev_nr];
3490 cam->v4ldev->release = video_device_release;
3491 video_set_drvdata(cam->v4ldev, cam);
3492 cam->v4ldev->v4l2_dev = &cam->v4l2_dev;
3494 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3497 DBG(1, "V4L device registration failed")
3498 if (err == -ENFILE && video_nr[dev_nr] == -1)
3499 DBG(2, "Couldn't find a free /dev/videoX node")
3500 video_nr[dev_nr] = -1;
3501 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3505 DBG(2, "V4L device registered as /dev/video%d", cam->v4ldev->num)
3507 /* Set some basic constants */
3508 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3510 /* Add a new entry into the list of V4L registered devices */
3511 mutex_lock(&w9968cf_devlist_mutex);
3512 list_add(&cam->v4llist, &w9968cf_dev_list);
3513 mutex_unlock(&w9968cf_devlist_mutex);
3514 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3516 w9968cf_turn_on_led(cam);
3518 w9968cf_i2c_init(cam);
3519 cam->sensor_sd = v4l2_i2c_new_probed_subdev(&cam->v4l2_dev,
3521 "ovcamchip", "ovcamchip", addrs);
3523 usb_set_intfdata(intf, cam);
3524 mutex_unlock(&cam->dev_mutex);
3526 err = w9968cf_sensor_init(cam);
3529 fail: /* Free unused memory */
3530 kfree(cam->control_buffer);
3531 kfree(cam->data_buffer);
3533 video_device_release(cam->v4ldev);
3534 mutex_unlock(&cam->dev_mutex);
3535 v4l2_device_unregister(&cam->v4l2_dev);
3542 static void w9968cf_usb_disconnect(struct usb_interface* intf)
3544 struct w9968cf_device* cam =
3545 (struct w9968cf_device*)usb_get_intfdata(intf);
3548 down_write(&w9968cf_disconnect);
3549 /* Prevent concurrent accesses to data */
3550 mutex_lock(&cam->dev_mutex);
3552 cam->disconnected = 1;
3554 DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id));
3556 v4l2_device_disconnect(&cam->v4l2_dev);
3558 wake_up_interruptible_all(&cam->open);
3561 DBG(2, "The device is open (/dev/video%d)! "
3562 "Process name: %s. Deregistration and memory "
3563 "deallocation are deferred on close.",
3564 cam->v4ldev->num, cam->command)
3565 cam->misconfigured = 1;
3566 w9968cf_stop_transfer(cam);
3567 wake_up_interruptible(&cam->wait_queue);
3569 w9968cf_release_resources(cam);
3571 mutex_unlock(&cam->dev_mutex);
3572 up_write(&w9968cf_disconnect);
3581 static struct usb_driver w9968cf_usb_driver = {
3583 .id_table = winbond_id_table,
3584 .probe = w9968cf_usb_probe,
3585 .disconnect = w9968cf_usb_disconnect,
3590 /****************************************************************************
3591 * Module init, exit and intermodule communication *
3592 ****************************************************************************/
3594 static int __init w9968cf_module_init(void)
3598 KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
3599 KDBG(3, W9968CF_MODULE_AUTHOR)
3601 if ((err = usb_register(&w9968cf_usb_driver)))
3608 static void __exit w9968cf_module_exit(void)
3610 /* w9968cf_usb_disconnect() will be called */
3611 usb_deregister(&w9968cf_usb_driver);
3613 KDBG(2, W9968CF_MODULE_NAME" deregistered")
3617 module_init(w9968cf_module_init);
3618 module_exit(w9968cf_module_exit);