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/moduleparam.h>
48 #include "w9968cf_decoder.h"
52 /****************************************************************************
53 * Module macros and parameters *
54 ****************************************************************************/
56 MODULE_DEVICE_TABLE(usb, winbond_id_table);
58 MODULE_AUTHOR(W9968CF_MODULE_AUTHOR" "W9968CF_AUTHOR_EMAIL);
59 MODULE_DESCRIPTION(W9968CF_MODULE_NAME);
60 MODULE_VERSION(W9968CF_MODULE_VERSION);
61 MODULE_LICENSE(W9968CF_MODULE_LICENSE);
62 MODULE_SUPPORTED_DEVICE("Video");
64 static int ovmod_load = W9968CF_OVMOD_LOAD;
65 static int vppmod_load = W9968CF_VPPMOD_LOAD;
66 static unsigned short simcams = W9968CF_SIMCAMS;
67 static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
68 static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
70 static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
72 static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
73 W9968CF_DOUBLE_BUFFER};
74 static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
75 static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
77 static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
78 static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
79 W9968CF_DECOMPRESSION};
80 static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
81 static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
82 static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
83 static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
84 static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
85 static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
87 static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
88 W9968CF_BANDINGFILTER};
89 static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
90 static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
91 static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
92 static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
93 static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
95 static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
96 static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
97 static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
99 static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
102 static unsigned short debug = W9968CF_DEBUG_LEVEL;
103 static int specific_debug = W9968CF_SPECIFIC_DEBUG;
106 static unsigned int param_nv[24]; /* number of values per parameter */
109 module_param(ovmod_load, bool, 0644);
110 module_param(vppmod_load, bool, 0444);
112 module_param(simcams, ushort, 0644);
113 module_param_array(video_nr, short, ¶m_nv[0], 0444);
114 module_param_array(packet_size, uint, ¶m_nv[1], 0444);
115 module_param_array(max_buffers, ushort, ¶m_nv[2], 0444);
116 module_param_array(double_buffer, bool, ¶m_nv[3], 0444);
117 module_param_array(clamping, bool, ¶m_nv[4], 0444);
118 module_param_array(filter_type, ushort, ¶m_nv[5], 0444);
119 module_param_array(largeview, bool, ¶m_nv[6], 0444);
120 module_param_array(decompression, ushort, ¶m_nv[7], 0444);
121 module_param_array(upscaling, bool, ¶m_nv[8], 0444);
122 module_param_array(force_palette, ushort, ¶m_nv[9], 0444);
123 module_param_array(force_rgb, ushort, ¶m_nv[10], 0444);
124 module_param_array(autobright, bool, ¶m_nv[11], 0444);
125 module_param_array(autoexp, bool, ¶m_nv[12], 0444);
126 module_param_array(lightfreq, ushort, ¶m_nv[13], 0444);
127 module_param_array(bandingfilter, bool, ¶m_nv[14], 0444);
128 module_param_array(clockdiv, short, ¶m_nv[15], 0444);
129 module_param_array(backlight, bool, ¶m_nv[16], 0444);
130 module_param_array(mirror, bool, ¶m_nv[17], 0444);
131 module_param_array(monochrome, bool, ¶m_nv[18], 0444);
132 module_param_array(brightness, uint, ¶m_nv[19], 0444);
133 module_param_array(hue, uint, ¶m_nv[20], 0444);
134 module_param_array(colour, uint, ¶m_nv[21], 0444);
135 module_param_array(contrast, uint, ¶m_nv[22], 0444);
136 module_param_array(whiteness, uint, ¶m_nv[23], 0444);
138 module_param(debug, ushort, 0644);
139 module_param(specific_debug, bool, 0644);
143 MODULE_PARM_DESC(ovmod_load,
144 "\n<0|1> Automatic 'ovcamchip' module loading."
145 "\n0 disabled, 1 enabled."
146 "\nIf enabled,'insmod' searches for the required 'ovcamchip'"
147 "\nmodule in the system, according to its configuration, and"
148 "\nattempts to load that module automatically. This action is"
149 "\nperformed once as soon as the 'w9968cf' module is loaded"
151 "\nDefault value is "__MODULE_STRING(W9968CF_OVMOD_LOAD)"."
153 MODULE_PARM_DESC(vppmod_load,
154 "\n<0|1> Automatic 'w9968cf-vpp' module loading."
155 "\n0 disabled, 1 enabled."
156 "\nIf enabled, every time an application attempts to open a"
157 "\ncamera, 'insmod' searches for the video post-processing"
158 "\nmodule in the system and loads it automatically (if"
159 "\npresent). The optional 'w9968cf-vpp' module adds extra"
160 "\n image manipulation functions to the 'w9968cf' module,like"
161 "\nsoftware up-scaling,colour conversions and video decoding"
162 "\nfor very high frame rates."
163 "\nDefault value is "__MODULE_STRING(W9968CF_VPPMOD_LOAD)"."
166 MODULE_PARM_DESC(simcams,
167 "\n<n> Number of cameras allowed to stream simultaneously."
168 "\nn may vary from 0 to "
169 __MODULE_STRING(W9968CF_MAX_DEVICES)"."
170 "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
172 MODULE_PARM_DESC(video_nr,
173 "\n<-1|n[,...]> Specify V4L minor mode number."
174 "\n -1 = use next available (default)"
175 "\n n = use minor number n (integer >= 0)"
176 "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
179 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
180 "\nthe second camera and use auto for the first"
181 "\none and for every other camera."
183 MODULE_PARM_DESC(packet_size,
184 "\n<n[,...]> Specify the maximum data payload"
185 "\nsize in bytes for alternate settings, for each device."
186 "\nn is scaled between 63 and 1023 "
187 "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
189 MODULE_PARM_DESC(max_buffers,
190 "\n<n[,...]> For advanced users."
191 "\nSpecify the maximum number of video frame buffers"
192 "\nto allocate for each device, from 2 to "
193 __MODULE_STRING(W9968CF_MAX_BUFFERS)
194 ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
196 MODULE_PARM_DESC(double_buffer,
198 "Hardware double buffering: 0 disabled, 1 enabled."
199 "\nIt should be enabled if you want smooth video output: if"
200 "\nyou obtain out of sync. video, disable it, or try to"
201 "\ndecrease the 'clockdiv' module parameter value."
202 "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
205 MODULE_PARM_DESC(clamping,
206 "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
207 "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
210 MODULE_PARM_DESC(filter_type,
211 "\n<0|1|2[,...]> Video filter type."
212 "\n0 none, 1 (1-2-1) 3-tap filter, "
213 "2 (2-3-6-3-2) 5-tap filter."
214 "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
216 "\nThe filter is used to reduce noise and aliasing artifacts"
217 "\nproduced by the CCD or CMOS image sensor, and the scaling"
220 MODULE_PARM_DESC(largeview,
221 "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
222 "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
225 MODULE_PARM_DESC(upscaling,
226 "\n<0|1[,...]> Software scaling (for non-compressed video):"
227 "\n0 disabled, 1 enabled."
228 "\nDisable it if you have a slow CPU or you don't have"
230 "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
232 "\nIf 'w9968cf-vpp' is not present, this parameter is"
235 MODULE_PARM_DESC(decompression,
236 "\n<0|1|2[,...]> Software video decompression:"
237 "\n- 0 disables decompression (doesn't allow formats needing"
239 "\n- 1 forces decompression (allows formats needing"
240 " decompression only);"
241 "\n- 2 allows any permitted formats."
242 "\nFormats supporting compressed video are YUV422P and"
244 "\nin any resolutions where both width and height are "
246 "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
248 "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
249 "\nnot allowed; in this case this parameter is set to 2."
251 MODULE_PARM_DESC(force_palette,
253 "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
254 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
255 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
256 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
257 "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
258 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
259 "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
260 "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
261 "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
262 "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
263 "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
265 " Force picture palette."
267 "\n- 0 allows any of the following formats:"
268 "\n- UYVY 16 bpp - Original video, compression disabled"
269 "\n- YUV420 12 bpp - Original video, compression enabled"
270 "\n- YUV422P 16 bpp - Original video, compression enabled"
271 "\n- YUV420P 12 bpp - Original video, compression enabled"
272 "\n- YUVY 16 bpp - Software conversion from UYVY"
273 "\n- YUV422 16 bpp - Software conversion from UYVY"
274 "\n- GREY 8 bpp - Software conversion from UYVY"
275 "\n- RGB555 16 bpp - Software conversion from UYVY"
276 "\n- RGB565 16 bpp - Software conversion from UYVY"
277 "\n- RGB24 24 bpp - Software conversion from UYVY"
278 "\n- RGB32 32 bpp - Software conversion from UYVY"
279 "\nWhen not 0, this parameter will override 'decompression'."
280 "\nDefault value is 0 for every device."
281 "\nInitial palette is "
282 __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
283 "\nIf 'w9968cf-vpp' is not present, this parameter is"
286 MODULE_PARM_DESC(force_rgb,
287 "\n<0|1[,...]> Read RGB video data instead of BGR:"
288 "\n 1 = use RGB component ordering."
289 "\n 0 = use BGR component ordering."
290 "\nThis parameter has effect when using RGBX palettes only."
291 "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
294 MODULE_PARM_DESC(autobright,
295 "\n<0|1[,...]> Image sensor automatically changes brightness:"
297 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
300 MODULE_PARM_DESC(autoexp,
301 "\n<0|1[,...]> Image sensor automatically changes exposure:"
303 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
306 MODULE_PARM_DESC(lightfreq,
307 "\n<50|60[,...]> Light frequency in Hz:"
308 "\n 50 for European and Asian lighting,"
309 " 60 for American lighting."
310 "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
313 MODULE_PARM_DESC(bandingfilter,
314 "\n<0|1[,...]> Banding filter to reduce effects of"
315 " fluorescent lighting:"
316 "\n 0 disabled, 1 enabled."
317 "\nThis filter tries to reduce the pattern of horizontal"
318 "\nlight/dark bands caused by some (usually fluorescent)"
320 "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
323 MODULE_PARM_DESC(clockdiv,
325 "Force pixel clock divisor to a specific value (for experts):"
326 "\n n may vary from 0 to 127."
327 "\n -1 for automatic value."
328 "\nSee also the 'double_buffer' module parameter."
329 "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
332 MODULE_PARM_DESC(backlight,
333 "\n<0|1[,...]> Objects are lit from behind:"
335 "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
338 MODULE_PARM_DESC(mirror,
339 "\n<0|1[,...]> Reverse image horizontally:"
341 "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
344 MODULE_PARM_DESC(monochrome,
345 "\n<0|1[,...]> Use image sensor as monochrome sensor:"
347 "\nNot all the sensors support monochrome color."
348 "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
351 MODULE_PARM_DESC(brightness,
352 "\n<n[,...]> Set picture brightness (0-65535)."
353 "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
355 "\nThis parameter has no effect if 'autobright' is enabled."
357 MODULE_PARM_DESC(hue,
358 "\n<n[,...]> Set picture hue (0-65535)."
359 "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
362 MODULE_PARM_DESC(colour,
363 "\n<n[,...]> Set picture saturation (0-65535)."
364 "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
367 MODULE_PARM_DESC(contrast,
368 "\n<n[,...]> Set picture contrast (0-65535)."
369 "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
372 MODULE_PARM_DESC(whiteness,
373 "\n<n[,...]> Set picture whiteness (0-65535)."
374 "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
378 MODULE_PARM_DESC(debug,
379 "\n<n> Debugging information level, from 0 to 6:"
380 "\n0 = none (use carefully)"
381 "\n1 = critical errors"
382 "\n2 = significant informations"
383 "\n3 = configuration or general messages"
385 "\n5 = called functions"
386 "\n6 = function internals"
387 "\nLevel 5 and 6 are useful for testing only, when only "
388 "one device is used."
389 "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
391 MODULE_PARM_DESC(specific_debug,
392 "\n<0|1> Enable or disable specific debugging messages:"
393 "\n0 = print messages concerning every level"
395 "\n1 = print messages concerning the level"
396 " indicated by 'debug'."
397 "\nDefault value is "
398 __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
400 #endif /* W9968CF_DEBUG */
404 /****************************************************************************
406 ****************************************************************************/
408 /* Video4linux interface */
409 static struct file_operations w9968cf_fops;
410 static int w9968cf_open(struct inode*, struct file*);
411 static int w9968cf_release(struct inode*, struct file*);
412 static int w9968cf_mmap(struct file*, struct vm_area_struct*);
413 static int w9968cf_ioctl(struct inode*, struct file*, unsigned, unsigned long);
414 static ssize_t w9968cf_read(struct file*, char __user *, size_t, loff_t*);
415 static int w9968cf_v4l_ioctl(struct inode*, struct file*, unsigned int,
419 static int w9968cf_start_transfer(struct w9968cf_device*);
420 static int w9968cf_stop_transfer(struct w9968cf_device*);
421 static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
422 static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
423 static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
424 static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
425 static int w9968cf_read_sb(struct w9968cf_device*);
426 static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
427 static void w9968cf_urb_complete(struct urb *urb, struct pt_regs *regs);
429 /* Low-level I2C (SMBus) I/O */
430 static int w9968cf_smbus_start(struct w9968cf_device*);
431 static int w9968cf_smbus_stop(struct w9968cf_device*);
432 static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
433 static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
434 static int w9968cf_smbus_write_ack(struct w9968cf_device*);
435 static int w9968cf_smbus_read_ack(struct w9968cf_device*);
436 static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
437 static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
438 u16 address, u8* value);
439 static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
440 u8 subaddress, u8* value);
441 static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
442 u16 address, u8 subaddress);
443 static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
444 u16 address, u8 subaddress,
447 /* I2C interface to kernel */
448 static int w9968cf_i2c_init(struct w9968cf_device*);
449 static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
450 unsigned short flags, char read_write,
451 u8 command, int size, union i2c_smbus_data*);
452 static u32 w9968cf_i2c_func(struct i2c_adapter*);
453 static int w9968cf_i2c_attach_inform(struct i2c_client*);
454 static int w9968cf_i2c_detach_inform(struct i2c_client*);
455 static int w9968cf_i2c_control(struct i2c_adapter*, unsigned int cmd,
458 /* Memory management */
459 static void* rvmalloc(unsigned long size);
460 static void rvfree(void *mem, unsigned long size);
461 static void w9968cf_deallocate_memory(struct w9968cf_device*);
462 static int w9968cf_allocate_memory(struct w9968cf_device*);
464 /* High-level image sensor control functions */
465 static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
466 static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
467 static int w9968cf_sensor_cmd(struct w9968cf_device*,
468 unsigned int cmd, void *arg);
469 static int w9968cf_sensor_init(struct w9968cf_device*);
470 static int w9968cf_sensor_update_settings(struct w9968cf_device*);
471 static int w9968cf_sensor_get_picture(struct w9968cf_device*);
472 static int w9968cf_sensor_update_picture(struct w9968cf_device*,
473 struct video_picture pict);
475 /* Other helper functions */
476 static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
477 enum w9968cf_model_id,
478 const unsigned short dev_nr);
479 static void w9968cf_adjust_configuration(struct w9968cf_device*);
480 static int w9968cf_turn_on_led(struct w9968cf_device*);
481 static int w9968cf_init_chip(struct w9968cf_device*);
482 static inline u16 w9968cf_valid_palette(u16 palette);
483 static inline u16 w9968cf_valid_depth(u16 palette);
484 static inline u8 w9968cf_need_decompression(u16 palette);
485 static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
486 static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
487 static int w9968cf_postprocess_frame(struct w9968cf_device*,
488 struct w9968cf_frame_t*);
489 static int w9968cf_adjust_window_size(struct w9968cf_device*, u16* w, u16* h);
490 static void w9968cf_init_framelist(struct w9968cf_device*);
491 static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
492 static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
493 static void w9968cf_release_resources(struct w9968cf_device*);
495 /* Intermodule communication */
496 static int w9968cf_vppmod_detect(struct w9968cf_device*);
497 static void w9968cf_vppmod_release(struct w9968cf_device*);
501 /****************************************************************************
503 ****************************************************************************/
505 /* Used to represent a list of values and their respective symbolic names */
506 struct w9968cf_symbolic_list {
511 /*--------------------------------------------------------------------------
512 Returns the name of the matching element in the symbolic_list array. The
513 end of the list must be marked with an element that has a NULL name.
514 --------------------------------------------------------------------------*/
515 static inline const char *
516 symbolic(struct w9968cf_symbolic_list list[], const int num)
520 for (i = 0; list[i].name != NULL; i++)
521 if (list[i].num == num)
522 return (list[i].name);
527 static struct w9968cf_symbolic_list camlist[] = {
528 { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
529 { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
531 /* Other cameras (having the same descriptors as Generic W996[87]CF) */
532 { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
533 { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
534 { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
535 { W9968CF_MOD_LL, "Lebon LDC-035A" },
536 { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
537 { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
538 { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
539 { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
540 { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
545 static struct w9968cf_symbolic_list senlist[] = {
546 { CC_OV76BE, "OV76BE" },
547 { CC_OV7610, "OV7610" },
548 { CC_OV7620, "OV7620" },
549 { CC_OV7620AE, "OV7620AE" },
550 { CC_OV6620, "OV6620" },
551 { CC_OV6630, "OV6630" },
552 { CC_OV6630AE, "OV6630AE" },
553 { CC_OV6630AF, "OV6630AF" },
557 /* Video4Linux1 palettes */
558 static struct w9968cf_symbolic_list v4l1_plist[] = {
559 { VIDEO_PALETTE_GREY, "GREY" },
560 { VIDEO_PALETTE_HI240, "HI240" },
561 { VIDEO_PALETTE_RGB565, "RGB565" },
562 { VIDEO_PALETTE_RGB24, "RGB24" },
563 { VIDEO_PALETTE_RGB32, "RGB32" },
564 { VIDEO_PALETTE_RGB555, "RGB555" },
565 { VIDEO_PALETTE_YUV422, "YUV422" },
566 { VIDEO_PALETTE_YUYV, "YUYV" },
567 { VIDEO_PALETTE_UYVY, "UYVY" },
568 { VIDEO_PALETTE_YUV420, "YUV420" },
569 { VIDEO_PALETTE_YUV411, "YUV411" },
570 { VIDEO_PALETTE_RAW, "RAW" },
571 { VIDEO_PALETTE_YUV422P, "YUV422P" },
572 { VIDEO_PALETTE_YUV411P, "YUV411P" },
573 { VIDEO_PALETTE_YUV420P, "YUV420P" },
574 { VIDEO_PALETTE_YUV410P, "YUV410P" },
578 /* Decoder error codes: */
579 static struct w9968cf_symbolic_list decoder_errlist[] = {
580 { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
581 { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
582 { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
583 { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
584 { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
585 { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
589 /* URB error codes: */
590 static struct w9968cf_symbolic_list urb_errlist[] = {
591 { -ENOMEM, "No memory for allocation of internal structures" },
592 { -ENOSPC, "The host controller's bandwidth is already consumed" },
593 { -ENOENT, "URB was canceled by unlink_urb" },
594 { -EXDEV, "ISO transfer only partially completed" },
595 { -EAGAIN, "Too match scheduled for the future" },
596 { -ENXIO, "URB already queued" },
597 { -EFBIG, "Too much ISO frames requested" },
598 { -ENOSR, "Buffer error (overrun)" },
599 { -EPIPE, "Specified endpoint is stalled (device not responding)"},
600 { -EOVERFLOW, "Babble (bad cable?)" },
601 { -EPROTO, "Bit-stuff error (bad cable?)" },
602 { -EILSEQ, "CRC/Timeout" },
603 { -ETIMEDOUT, "NAK (device does not respond)" },
609 /****************************************************************************
610 * Memory management functions *
611 ****************************************************************************/
612 static void* rvmalloc(unsigned long size)
617 size = PAGE_ALIGN(size);
618 mem = vmalloc_32(size);
622 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
623 adr = (unsigned long) mem;
625 SetPageReserved(vmalloc_to_page((void *)adr));
634 static void rvfree(void* mem, unsigned long size)
641 adr = (unsigned long) mem;
642 while ((long) size > 0) {
643 ClearPageReserved(vmalloc_to_page((void *)adr));
651 /*--------------------------------------------------------------------------
652 Deallocate previously allocated memory.
653 --------------------------------------------------------------------------*/
654 static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
658 /* Free the isochronous transfer buffers */
659 for (i = 0; i < W9968CF_URBS; i++) {
660 kfree(cam->transfer_buffer[i]);
661 cam->transfer_buffer[i] = NULL;
664 /* Free temporary frame buffer */
665 if (cam->frame_tmp.buffer) {
666 rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
667 cam->frame_tmp.buffer = NULL;
670 /* Free helper buffer */
671 if (cam->frame_vpp.buffer) {
672 rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
673 cam->frame_vpp.buffer = NULL;
676 /* Free video frame buffers */
677 if (cam->frame[0].buffer) {
678 rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
679 cam->frame[0].buffer = NULL;
684 DBG(5, "Memory successfully deallocated")
688 /*--------------------------------------------------------------------------
689 Allocate memory buffers for USB transfers and video frames.
690 This function is called by open() only.
691 Return 0 on success, a negative number otherwise.
692 --------------------------------------------------------------------------*/
693 static int w9968cf_allocate_memory(struct w9968cf_device* cam)
695 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
697 unsigned long hw_bufsize, vpp_bufsize;
700 /* NOTE: Deallocation is done elsewhere in case of error */
702 /* Calculate the max amount of raw data per frame from the device */
703 hw_bufsize = cam->maxwidth*cam->maxheight*2;
705 /* Calculate the max buf. size needed for post-processing routines */
706 bpp = (w9968cf_vpp) ? 4 : 2;
708 vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
709 cam->maxwidth*cam->maxheight*bpp);
711 vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
713 /* Allocate memory for the isochronous transfer buffers */
714 for (i = 0; i < W9968CF_URBS; i++) {
715 if (!(cam->transfer_buffer[i] =
716 kmalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
717 DBG(1, "Couldn't allocate memory for the isochronous "
718 "transfer buffers (%u bytes)",
719 p_size * W9968CF_ISO_PACKETS)
722 memset(cam->transfer_buffer[i], 0, W9968CF_ISO_PACKETS*p_size);
725 /* Allocate memory for the temporary frame buffer */
726 if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
727 DBG(1, "Couldn't allocate memory for the temporary "
728 "video frame buffer (%lu bytes)", hw_bufsize)
731 cam->frame_tmp.size = hw_bufsize;
732 cam->frame_tmp.number = -1;
734 /* Allocate memory for the helper buffer */
736 if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
737 DBG(1, "Couldn't allocate memory for the helper buffer"
738 " (%lu bytes)", vpp_bufsize)
741 cam->frame_vpp.size = vpp_bufsize;
743 cam->frame_vpp.buffer = NULL;
745 /* Allocate memory for video frame buffers */
746 cam->nbuffers = cam->max_buffers;
747 while (cam->nbuffers >= 2) {
748 if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
755 DBG(1, "Couldn't allocate memory for the video frame buffers")
760 if (cam->nbuffers != cam->max_buffers)
761 DBG(2, "Couldn't allocate memory for %u video frame buffers. "
762 "Only memory for %u buffers has been allocated",
763 cam->max_buffers, cam->nbuffers)
765 for (i = 0; i < cam->nbuffers; i++) {
766 cam->frame[i].buffer = buff + i*vpp_bufsize;
767 cam->frame[i].size = vpp_bufsize;
768 cam->frame[i].number = i;
770 if (i != cam->nbuffers-1)
771 cam->frame[i].next = &cam->frame[i+1];
773 cam->frame[i].next = &cam->frame[0];
774 cam->frame[i].status = F_UNUSED;
777 DBG(5, "Memory successfully allocated")
783 /****************************************************************************
784 * USB-specific functions *
785 ****************************************************************************/
787 /*--------------------------------------------------------------------------
788 This is an handler function which is called after the URBs are completed.
789 It collects multiple data packets coming from the camera by putting them
790 into frame buffers: one or more zero data length data packets are used to
791 mark the end of a video frame; the first non-zero data packet is the start
792 of the next video frame; if an error is encountered in a packet, the entire
793 video frame is discarded and grabbed again.
794 If there are no requested frames in the FIFO list, packets are collected into
796 --------------------------------------------------------------------------*/
797 static void w9968cf_urb_complete(struct urb *urb, struct pt_regs *regs)
799 struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
800 struct w9968cf_frame_t** f;
801 unsigned int len, status;
806 if ((!cam->streaming) || cam->disconnected) {
807 DBG(4, "Got interrupt, but not streaming")
811 /* "(*f)" will be used instead of "cam->frame_current" */
812 f = &cam->frame_current;
814 /* If a frame has been requested and we are grabbing into
815 the temporary frame, we'll switch to that requested frame */
816 if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
817 if (cam->frame_tmp.status == F_GRABBING) {
818 w9968cf_pop_frame(cam, &cam->frame_current);
819 (*f)->status = F_GRABBING;
820 (*f)->length = cam->frame_tmp.length;
821 memcpy((*f)->buffer, cam->frame_tmp.buffer,
823 DBG(6, "Switched from temp. frame to frame #%d",
828 for (i = 0; i < urb->number_of_packets; i++) {
829 len = urb->iso_frame_desc[i].actual_length;
830 status = urb->iso_frame_desc[i].status;
831 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
833 if (status && len != 0) {
834 DBG(4, "URB failed, error in data packet "
836 status, symbolic(urb_errlist, status))
837 (*f)->status = F_ERROR;
841 if (len) { /* start of frame */
843 if ((*f)->status == F_UNUSED) {
844 (*f)->status = F_GRABBING;
848 /* Buffer overflows shouldn't happen, however...*/
849 if ((*f)->length + len > (*f)->size) {
850 DBG(4, "Buffer overflow: bad data packets")
851 (*f)->status = F_ERROR;
854 if ((*f)->status == F_GRABBING) {
855 memcpy((*f)->buffer + (*f)->length, pos, len);
859 } else if ((*f)->status == F_GRABBING) { /* end of frame */
861 DBG(6, "Frame #%d successfully grabbed", (*f)->number)
863 if (cam->vpp_flag & VPP_DECOMPRESSION) {
864 err = w9968cf_vpp->check_headers((*f)->buffer,
867 DBG(4, "Skip corrupted frame: %s",
868 symbolic(decoder_errlist, err))
869 (*f)->status = F_UNUSED;
870 continue; /* grab this frame again */
874 (*f)->status = F_READY;
877 /* Take a pointer to the new frame from the FIFO list.
878 If the list is empty,we'll use the temporary frame*/
879 if (*cam->requested_frame)
880 w9968cf_pop_frame(cam, &cam->frame_current);
882 cam->frame_current = &cam->frame_tmp;
883 (*f)->status = F_UNUSED;
886 } else if ((*f)->status == F_ERROR)
887 (*f)->status = F_UNUSED; /* grab it again */
889 PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
890 (unsigned long)(*f)->length, i, len, (*f)->status)
894 /* Resubmit this URB */
895 urb->dev = cam->usbdev;
897 spin_lock(&cam->urb_lock);
899 if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
900 cam->misconfigured = 1;
901 DBG(1, "Couldn't resubmit the URB: error %d, %s",
902 err, symbolic(urb_errlist, err))
904 spin_unlock(&cam->urb_lock);
906 /* Wake up the user process */
907 wake_up_interruptible(&cam->wait_queue);
911 /*---------------------------------------------------------------------------
912 Setup the URB structures for the isochronous transfer.
913 Submit the URBs so that the data transfer begins.
914 Return 0 on success, a negative number otherwise.
915 ---------------------------------------------------------------------------*/
916 static int w9968cf_start_transfer(struct w9968cf_device* cam)
918 struct usb_device *udev = cam->usbdev;
920 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
927 for (i = 0; i < W9968CF_URBS; i++) {
928 urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
931 for (j = 0; j < i; j++)
932 usb_free_urb(cam->urb[j]);
933 DBG(1, "Couldn't allocate the URB structures")
938 urb->context = (void*)cam;
939 urb->pipe = usb_rcvisocpipe(udev, 1);
940 urb->transfer_flags = URB_ISO_ASAP;
941 urb->number_of_packets = W9968CF_ISO_PACKETS;
942 urb->complete = w9968cf_urb_complete;
943 urb->transfer_buffer = cam->transfer_buffer[i];
944 urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
946 for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
947 urb->iso_frame_desc[j].offset = p_size*j;
948 urb->iso_frame_desc[j].length = p_size;
952 /* Transfer size per frame, in WORD ! */
959 err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
960 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
963 err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
964 err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
966 if (cam->vpp_flag & VPP_DECOMPRESSION)
967 err += w9968cf_upload_quantizationtables(cam);
969 vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
970 err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
972 err += usb_set_interface(udev, 0, cam->altsetting);
973 err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
975 if (err || (vidcapt < 0)) {
976 for (i = 0; i < W9968CF_URBS; i++)
977 usb_free_urb(cam->urb[i]);
978 DBG(1, "Couldn't tell the camera to start the data transfer")
982 w9968cf_init_framelist(cam);
984 /* Begin to grab into the temporary buffer */
985 cam->frame_tmp.status = F_UNUSED;
986 cam->frame_tmp.queued = 0;
987 cam->frame_current = &cam->frame_tmp;
989 if (!(cam->vpp_flag & VPP_DECOMPRESSION))
990 DBG(5, "Isochronous transfer size: %lu bytes/frame",
991 (unsigned long)t_size*2)
993 DBG(5, "Starting the isochronous transfer...")
997 /* Submit the URBs */
998 for (i = 0; i < W9968CF_URBS; i++) {
999 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
1002 for (j = i-1; j >= 0; j--) {
1003 usb_kill_urb(cam->urb[j]);
1004 usb_free_urb(cam->urb[j]);
1006 DBG(1, "Couldn't send a transfer request to the "
1007 "USB core (error #%d, %s)", err,
1008 symbolic(urb_errlist, err))
1017 /*--------------------------------------------------------------------------
1018 Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
1019 Return 0 on success, a negative number otherwise.
1020 --------------------------------------------------------------------------*/
1021 static int w9968cf_stop_transfer(struct w9968cf_device* cam)
1023 struct usb_device *udev = cam->usbdev;
1024 unsigned long lock_flags;
1028 if (!cam->streaming)
1031 /* This avoids race conditions with usb_submit_urb()
1032 in the URB completition handler */
1033 spin_lock_irqsave(&cam->urb_lock, lock_flags);
1035 spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
1037 for (i = W9968CF_URBS-1; i >= 0; i--)
1039 usb_kill_urb(cam->urb[i]);
1040 usb_free_urb(cam->urb[i]);
1044 if (cam->disconnected)
1047 err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
1048 err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
1049 err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
1050 err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
1053 DBG(2, "Failed to tell the camera to stop the isochronous "
1054 "transfer. However this is not a critical error.")
1059 DBG(5, "Isochronous transfer stopped")
1064 /*--------------------------------------------------------------------------
1065 Write a W9968CF register.
1066 Return 0 on success, -1 otherwise.
1067 --------------------------------------------------------------------------*/
1068 static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
1070 struct usb_device* udev = cam->usbdev;
1073 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1074 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1075 value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
1078 DBG(4, "Failed to write a register "
1079 "(value 0x%04X, index 0x%02X, error #%d, %s)",
1080 value, index, res, symbolic(urb_errlist, res))
1082 return (res >= 0) ? 0 : -1;
1086 /*--------------------------------------------------------------------------
1087 Read a W9968CF register.
1088 Return the register value on success, -1 otherwise.
1089 --------------------------------------------------------------------------*/
1090 static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
1092 struct usb_device* udev = cam->usbdev;
1093 u16* buff = cam->control_buffer;
1096 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
1097 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1098 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
1101 DBG(4, "Failed to read a register "
1102 "(index 0x%02X, error #%d, %s)",
1103 index, res, symbolic(urb_errlist, res))
1105 return (res >= 0) ? (int)(*buff) : -1;
1109 /*--------------------------------------------------------------------------
1110 Write 64-bit data to the fast serial bus registers.
1111 Return 0 on success, -1 otherwise.
1112 --------------------------------------------------------------------------*/
1113 static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
1115 struct usb_device* udev = cam->usbdev;
1121 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1122 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1123 value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
1126 DBG(4, "Failed to write the FSB registers "
1127 "(error #%d, %s)", res, symbolic(urb_errlist, res))
1129 return (res >= 0) ? 0 : -1;
1133 /*--------------------------------------------------------------------------
1134 Write data to the serial bus control register.
1135 Return 0 on success, a negative number otherwise.
1136 --------------------------------------------------------------------------*/
1137 static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
1141 err = w9968cf_write_reg(cam, value, 0x01);
1142 udelay(W9968CF_I2C_BUS_DELAY);
1148 /*--------------------------------------------------------------------------
1149 Read data from the serial bus control register.
1150 Return 0 on success, a negative number otherwise.
1151 --------------------------------------------------------------------------*/
1152 static int w9968cf_read_sb(struct w9968cf_device* cam)
1156 v = w9968cf_read_reg(cam, 0x01);
1157 udelay(W9968CF_I2C_BUS_DELAY);
1163 /*--------------------------------------------------------------------------
1164 Upload quantization tables for the JPEG compression.
1165 This function is called by w9968cf_start_transfer().
1166 Return 0 on success, a negative number otherwise.
1167 --------------------------------------------------------------------------*/
1168 static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
1173 err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
1175 for (i = 0, j = 0; i < 32; i++, j += 2) {
1176 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
1177 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
1178 err += w9968cf_write_reg(cam, a, 0x40+i);
1179 err += w9968cf_write_reg(cam, b, 0x60+i);
1181 err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
1188 /****************************************************************************
1189 * Low-level I2C I/O functions. *
1190 * The adapter supports the following I2C transfer functions: *
1191 * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
1192 * i2c_adap_read_byte_data() *
1193 * i2c_adap_read_byte() *
1194 ****************************************************************************/
1196 static int w9968cf_smbus_start(struct w9968cf_device* cam)
1200 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1201 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1207 static int w9968cf_smbus_stop(struct w9968cf_device* cam)
1211 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1212 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1218 static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
1223 for (bit = 0 ; bit < 8 ; bit++) {
1224 sda = (v & 0x80) ? 2 : 0;
1226 /* SDE=1, SDA=sda, SCL=0 */
1227 err += w9968cf_write_sb(cam, 0x10 | sda);
1228 /* SDE=1, SDA=sda, SCL=1 */
1229 err += w9968cf_write_sb(cam, 0x11 | sda);
1230 /* SDE=1, SDA=sda, SCL=0 */
1231 err += w9968cf_write_sb(cam, 0x10 | sda);
1238 static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
1244 for (bit = 0 ; bit < 8 ; bit++) {
1246 err += w9968cf_write_sb(cam, 0x0013);
1247 *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
1248 err += w9968cf_write_sb(cam, 0x0012);
1255 static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
1259 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1260 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1261 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1267 static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
1271 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1272 sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
1273 err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
1277 DBG(6, "Couldn't receive the ACK")
1285 /* This seems to refresh the communication through the serial bus */
1286 static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
1290 for (j = 1; j <= 10; j++) {
1291 err = w9968cf_write_reg(cam, 0x0020, 0x01);
1292 err += w9968cf_write_reg(cam, 0x0000, 0x01);
1301 /* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
1303 w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
1304 u16 address, u8 subaddress,u8 value)
1306 u16* data = cam->data_buffer;
1309 err += w9968cf_smbus_refresh_bus(cam);
1311 /* Enable SBUS outputs */
1312 err += w9968cf_write_sb(cam, 0x0020);
1314 data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
1315 data[0] |= (address & 0x40) ? 0x4000 : 0x0;
1316 data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
1317 data[1] |= (address & 0x20) ? 0x0150 : 0x0;
1318 data[1] |= (address & 0x10) ? 0x5400 : 0x0;
1319 data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
1320 data[2] |= (address & 0x04) ? 0x0540 : 0x0;
1321 data[2] |= (address & 0x02) ? 0x5000 : 0x0;
1322 data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
1323 data[3] |= (address & 0x01) ? 0x0054 : 0x0;
1325 err += w9968cf_write_fsb(cam, data);
1327 data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
1328 data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
1329 data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
1330 data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
1331 data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
1332 data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
1333 data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
1334 data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
1335 data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
1336 data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
1339 err += w9968cf_write_fsb(cam, data);
1341 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
1342 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
1343 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
1344 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
1345 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
1346 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
1347 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
1348 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
1349 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
1350 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
1353 err += w9968cf_write_fsb(cam, data);
1355 /* Disable SBUS outputs */
1356 err += w9968cf_write_sb(cam, 0x0000);
1359 DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
1360 "value 0x%02X", address, subaddress, value)
1362 DBG(5, "I2C write byte data failed, addr.0x%04X, "
1363 "subaddr.0x%02X, value 0x%02X",
1364 address, subaddress, value)
1370 /* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
1372 w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
1373 u16 address, u8 subaddress,
1378 /* Serial data enable */
1379 err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
1381 err += w9968cf_smbus_start(cam);
1382 err += w9968cf_smbus_write_byte(cam, address);
1383 err += w9968cf_smbus_read_ack(cam);
1384 err += w9968cf_smbus_write_byte(cam, subaddress);
1385 err += w9968cf_smbus_read_ack(cam);
1386 err += w9968cf_smbus_stop(cam);
1387 err += w9968cf_smbus_start(cam);
1388 err += w9968cf_smbus_write_byte(cam, address + 1);
1389 err += w9968cf_smbus_read_ack(cam);
1390 err += w9968cf_smbus_read_byte(cam, value);
1391 err += w9968cf_smbus_write_ack(cam);
1392 err += w9968cf_smbus_stop(cam);
1394 /* Serial data disable */
1395 err += w9968cf_write_sb(cam, 0x0000);
1398 DBG(5, "I2C read byte data done, addr.0x%04X, "
1399 "subaddr.0x%02X, value 0x%02X",
1400 address, subaddress, *value)
1402 DBG(5, "I2C read byte data failed, addr.0x%04X, "
1403 "subaddr.0x%02X, wrong value 0x%02X",
1404 address, subaddress, *value)
1410 /* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
1412 w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
1413 u16 address, u8* value)
1417 /* Serial data enable */
1418 err += w9968cf_write_sb(cam, 0x0013);
1420 err += w9968cf_smbus_start(cam);
1421 err += w9968cf_smbus_write_byte(cam, address + 1);
1422 err += w9968cf_smbus_read_ack(cam);
1423 err += w9968cf_smbus_read_byte(cam, value);
1424 err += w9968cf_smbus_write_ack(cam);
1425 err += w9968cf_smbus_stop(cam);
1427 /* Serial data disable */
1428 err += w9968cf_write_sb(cam, 0x0000);
1431 DBG(5, "I2C read byte done, addr.0x%04X, "
1432 "value 0x%02X", address, *value)
1434 DBG(5, "I2C read byte failed, addr.0x%04X, "
1435 "wrong value 0x%02X", address, *value)
1441 /* SMBus protocol: S Addr Wr [A] Value [A] P */
1443 w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
1444 u16 address, u8 value)
1446 DBG(4, "i2c_write_byte() is an unsupported transfer mode")
1452 /****************************************************************************
1453 * I2C interface to kernel *
1454 ****************************************************************************/
1457 w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1458 unsigned short flags, char read_write, u8 command,
1459 int size, union i2c_smbus_data *data)
1461 struct w9968cf_device* cam = i2c_get_adapdata(adapter);
1470 DBG(4, "Rejected slave ID 0x%04X", addr)
1474 if (size == I2C_SMBUS_BYTE) {
1475 /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
1478 if (read_write == I2C_SMBUS_WRITE)
1479 err = w9968cf_i2c_adap_write_byte(cam, addr, command);
1480 else if (read_write == I2C_SMBUS_READ)
1481 err = w9968cf_i2c_adap_read_byte(cam,addr,&data->byte);
1483 } else if (size == I2C_SMBUS_BYTE_DATA) {
1486 if (read_write == I2C_SMBUS_WRITE)
1487 err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
1488 command, data->byte);
1489 else if (read_write == I2C_SMBUS_READ) {
1490 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1491 err = w9968cf_i2c_adap_read_byte_data(cam,addr,
1492 command, &data->byte);
1494 if (w9968cf_smbus_refresh_bus(cam)) {
1506 DBG(4, "Unsupported I2C transfer mode (%d)", size)
1514 static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
1516 return I2C_FUNC_SMBUS_READ_BYTE |
1517 I2C_FUNC_SMBUS_READ_BYTE_DATA |
1518 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
1522 static int w9968cf_i2c_attach_inform(struct i2c_client* client)
1524 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
1525 int id = client->driver->id, err = 0;
1527 if (id == I2C_DRIVERID_OVCAMCHIP) {
1528 cam->sensor_client = client;
1529 err = w9968cf_sensor_init(cam);
1531 cam->sensor_client = NULL;
1535 DBG(4, "Rejected client [%s] with driver [%s]",
1536 client->name, client->driver->name)
1540 DBG(5, "I2C attach client [%s] with driver [%s]",
1541 client->name, client->driver->name)
1547 static int w9968cf_i2c_detach_inform(struct i2c_client* client)
1549 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
1551 if (cam->sensor_client == client)
1552 cam->sensor_client = NULL;
1554 DBG(5, "I2C detach client [%s]", client->name)
1561 w9968cf_i2c_control(struct i2c_adapter* adapter, unsigned int cmd,
1568 static int w9968cf_i2c_init(struct w9968cf_device* cam)
1572 static struct i2c_algorithm algo = {
1573 .smbus_xfer = w9968cf_i2c_smbus_xfer,
1574 .algo_control = w9968cf_i2c_control,
1575 .functionality = w9968cf_i2c_func,
1578 static struct i2c_adapter adap = {
1579 .id = I2C_HW_SMBUS_W9968CF,
1580 .class = I2C_CLASS_CAM_DIGITAL,
1581 .owner = THIS_MODULE,
1582 .client_register = w9968cf_i2c_attach_inform,
1583 .client_unregister = w9968cf_i2c_detach_inform,
1587 memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
1588 strcpy(cam->i2c_adapter.name, "w9968cf");
1589 i2c_set_adapdata(&cam->i2c_adapter, cam);
1591 DBG(6, "Registering I2C adapter with kernel...")
1593 err = i2c_add_adapter(&cam->i2c_adapter);
1595 DBG(1, "Failed to register the I2C adapter")
1597 DBG(5, "I2C adapter registered")
1604 /****************************************************************************
1605 * Helper functions *
1606 ****************************************************************************/
1608 /*--------------------------------------------------------------------------
1609 Turn on the LED on some webcams. A beep should be heard too.
1610 Return 0 on success, a negative number otherwise.
1611 --------------------------------------------------------------------------*/
1612 static int w9968cf_turn_on_led(struct w9968cf_device* cam)
1616 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power-down */
1617 err += w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
1618 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
1619 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* serial bus, SDS high */
1620 err += w9968cf_write_reg(cam, 0x0000, 0x01); /* serial bus, SDS low */
1621 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* ..high 'beep-beep' */
1624 DBG(2, "Couldn't turn on the LED")
1626 DBG(5, "LED turned on")
1632 /*--------------------------------------------------------------------------
1633 Write some registers for the device initialization.
1634 This function is called once on open().
1635 Return 0 on success, a negative number otherwise.
1636 --------------------------------------------------------------------------*/
1637 static int w9968cf_init_chip(struct w9968cf_device* cam)
1639 unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
1641 u0 = y0 + hw_bufsize/2,
1642 v0 = u0 + hw_bufsize/4,
1643 y1 = v0 + hw_bufsize/4,
1644 u1 = y1 + hw_bufsize/2,
1645 v1 = u1 + hw_bufsize/4;
1648 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
1649 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
1651 err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
1652 err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
1654 err += w9968cf_write_reg(cam, y0 & 0xffff, 0x20); /* Y buf.0, low */
1655 err += w9968cf_write_reg(cam, y0 >> 16, 0x21); /* Y buf.0, high */
1656 err += w9968cf_write_reg(cam, u0 & 0xffff, 0x24); /* U buf.0, low */
1657 err += w9968cf_write_reg(cam, u0 >> 16, 0x25); /* U buf.0, high */
1658 err += w9968cf_write_reg(cam, v0 & 0xffff, 0x28); /* V buf.0, low */
1659 err += w9968cf_write_reg(cam, v0 >> 16, 0x29); /* V buf.0, high */
1661 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x22); /* Y buf.1, low */
1662 err += w9968cf_write_reg(cam, y1 >> 16, 0x23); /* Y buf.1, high */
1663 err += w9968cf_write_reg(cam, u1 & 0xffff, 0x26); /* U buf.1, low */
1664 err += w9968cf_write_reg(cam, u1 >> 16, 0x27); /* U buf.1, high */
1665 err += w9968cf_write_reg(cam, v1 & 0xffff, 0x2a); /* V buf.1, low */
1666 err += w9968cf_write_reg(cam, v1 >> 16, 0x2b); /* V buf.1, high */
1668 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x32); /* JPEG buf 0 low */
1669 err += w9968cf_write_reg(cam, y1 >> 16, 0x33); /* JPEG buf 0 high */
1671 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x34); /* JPEG buf 1 low */
1672 err += w9968cf_write_reg(cam, y1 >> 16, 0x35); /* JPEG bug 1 high */
1674 err += w9968cf_write_reg(cam, 0x0000, 0x36);/* JPEG restart interval */
1675 err += w9968cf_write_reg(cam, 0x0804, 0x37);/*JPEG VLE FIFO threshold*/
1676 err += w9968cf_write_reg(cam, 0x0000, 0x38);/* disable hw up-scaling */
1677 err += w9968cf_write_reg(cam, 0x0000, 0x3f); /* JPEG/MCTL test data */
1679 err += w9968cf_set_picture(cam, cam->picture); /* this before */
1680 err += w9968cf_set_window(cam, cam->window);
1683 DBG(1, "Chip initialization failed")
1685 DBG(5, "Chip successfully initialized")
1691 /*--------------------------------------------------------------------------
1692 Return non-zero if the palette is supported, 0 otherwise.
1693 --------------------------------------------------------------------------*/
1694 static inline u16 w9968cf_valid_palette(u16 palette)
1697 while (w9968cf_formatlist[i].palette != 0) {
1698 if (palette == w9968cf_formatlist[i].palette)
1706 /*--------------------------------------------------------------------------
1707 Return the depth corresponding to the given palette.
1708 Palette _must_ be supported !
1709 --------------------------------------------------------------------------*/
1710 static inline u16 w9968cf_valid_depth(u16 palette)
1713 while (w9968cf_formatlist[i].palette != palette)
1716 return w9968cf_formatlist[i].depth;
1720 /*--------------------------------------------------------------------------
1721 Return non-zero if the format requires decompression, 0 otherwise.
1722 --------------------------------------------------------------------------*/
1723 static inline u8 w9968cf_need_decompression(u16 palette)
1726 while (w9968cf_formatlist[i].palette != 0) {
1727 if (palette == w9968cf_formatlist[i].palette)
1728 return w9968cf_formatlist[i].compression;
1735 /*--------------------------------------------------------------------------
1736 Change the picture settings of the camera.
1737 Return 0 on success, a negative number otherwise.
1738 --------------------------------------------------------------------------*/
1740 w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
1742 u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
1745 /* Make sure we are using a valid depth */
1746 pict.depth = w9968cf_valid_depth(pict.palette);
1750 hw_depth = pict.depth; /* depth used by the winbond chip */
1751 hw_palette = pict.palette; /* palette used by the winbond chip */
1753 /* VS & HS polarities */
1754 reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
1758 case VIDEO_PALETTE_UYVY:
1760 cam->vpp_flag = VPP_NONE;
1762 case VIDEO_PALETTE_YUV422P:
1764 cam->vpp_flag = VPP_DECOMPRESSION;
1766 case VIDEO_PALETTE_YUV420:
1767 case VIDEO_PALETTE_YUV420P:
1769 cam->vpp_flag = VPP_DECOMPRESSION;
1771 case VIDEO_PALETTE_YUYV:
1772 case VIDEO_PALETTE_YUV422:
1774 cam->vpp_flag = VPP_SWAP_YUV_BYTES;
1775 hw_palette = VIDEO_PALETTE_UYVY;
1777 /* Original video is used instead of RGBX palettes.
1778 Software conversion later. */
1779 case VIDEO_PALETTE_GREY:
1780 case VIDEO_PALETTE_RGB555:
1781 case VIDEO_PALETTE_RGB565:
1782 case VIDEO_PALETTE_RGB24:
1783 case VIDEO_PALETTE_RGB32:
1784 reg_v |= 0x0000; /* UYVY 16 bit is used */
1786 hw_palette = VIDEO_PALETTE_UYVY;
1787 cam->vpp_flag = VPP_UYVY_TO_RGBX;
1791 /* NOTE: due to memory issues, it is better to disable the hardware
1792 double buffering during compression */
1793 if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
1799 if (cam->filter_type == 1)
1801 else if (cam->filter_type == 2)
1804 if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
1807 if ((err = w9968cf_sensor_update_picture(cam, pict)))
1810 /* If all went well, update the device data structure */
1811 memcpy(&cam->picture, &pict, sizeof(pict));
1812 cam->hw_depth = hw_depth;
1813 cam->hw_palette = hw_palette;
1815 /* Settings changed, so we clear the frame buffers */
1816 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1818 DBG(4, "Palette is %s, depth is %u bpp",
1819 symbolic(v4l1_plist, pict.palette), pict.depth)
1824 DBG(1, "Failed to change picture settings")
1829 /*--------------------------------------------------------------------------
1830 Change the capture area size of the camera.
1831 This function _must_ be called _after_ w9968cf_set_picture().
1832 Return 0 on success, a negative number otherwise.
1833 --------------------------------------------------------------------------*/
1835 w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
1837 u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
1838 unsigned long fw, fh;
1839 struct ovcamchip_window s_win;
1842 /* Work around to avoid FP arithmetics */
1843 #define __SC(x) ((x) << 10)
1844 #define __UNSC(x) ((x) >> 10)
1846 /* Make sure we are using a supported resolution */
1847 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
1848 (u16*)&win.height)))
1851 /* Scaling factors */
1852 fw = __SC(win.width) / cam->maxwidth;
1853 fh = __SC(win.height) / cam->maxheight;
1855 /* Set up the width and height values used by the chip */
1856 if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
1857 cam->vpp_flag |= VPP_UPSCALE;
1858 /* Calculate largest w,h mantaining the same w/h ratio */
1859 w = (fw >= fh) ? cam->maxwidth : __SC(win.width)/fh;
1860 h = (fw >= fh) ? __SC(win.height)/fw : cam->maxheight;
1861 if (w < cam->minwidth) /* just in case */
1863 if (h < cam->minheight) /* just in case */
1866 cam->vpp_flag &= ~VPP_UPSCALE;
1871 /* x,y offsets of the cropped area */
1872 scx = cam->start_cropx;
1873 scy = cam->start_cropy;
1875 /* Calculate cropped area manteining the right w/h ratio */
1876 if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
1877 cw = (fw >= fh) ? cam->maxwidth : __SC(win.width)/fh;
1878 ch = (fw >= fh) ? __SC(win.height)/fw : cam->maxheight;
1884 /* Setup the window of the sensor */
1885 s_win.format = VIDEO_PALETTE_UYVY;
1886 s_win.width = cam->maxwidth;
1887 s_win.height = cam->maxheight;
1888 s_win.quarter = 0; /* full progressive video */
1891 s_win.x = (s_win.width - cw) / 2;
1892 s_win.y = (s_win.height - ch) / 2;
1895 if (cam->clockdiv >= 0)
1896 s_win.clockdiv = cam->clockdiv; /* manual override */
1898 switch (cam->sensor) {
1911 s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
1914 /* We have to scale win.x and win.y offsets */
1915 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1916 || (cam->vpp_flag & VPP_UPSCALE) ) {
1917 ax = __SC(win.x)/fw;
1918 ay = __SC(win.y)/fh;
1924 if ((ax + cw) > cam->maxwidth)
1925 ax = cam->maxwidth - cw;
1927 if ((ay + ch) > cam->maxheight)
1928 ay = cam->maxheight - ch;
1930 /* Adjust win.x, win.y */
1931 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1932 || (cam->vpp_flag & VPP_UPSCALE) ) {
1933 win.x = __UNSC(ax*fw);
1934 win.y = __UNSC(ay*fh);
1940 /* Offsets used by the chip */
1945 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
1948 err += w9968cf_write_reg(cam, scx + x, 0x10);
1949 err += w9968cf_write_reg(cam, scy + y, 0x11);
1950 err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
1951 err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
1952 err += w9968cf_write_reg(cam, w, 0x14);
1953 err += w9968cf_write_reg(cam, h, 0x15);
1955 /* JPEG width & height */
1956 err += w9968cf_write_reg(cam, w, 0x30);
1957 err += w9968cf_write_reg(cam, h, 0x31);
1959 /* Y & UV frame buffer strides (in WORD) */
1960 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1961 err += w9968cf_write_reg(cam, w/2, 0x2c);
1962 err += w9968cf_write_reg(cam, w/4, 0x2d);
1964 err += w9968cf_write_reg(cam, w, 0x2c);
1969 /* If all went well, update the device data structure */
1970 memcpy(&cam->window, &win, sizeof(win));
1974 /* Settings changed, so we clear the frame buffers */
1975 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1977 DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
1978 win.width, win.height, win.x, win.y)
1980 PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
1981 "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
1982 x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
1983 win.width, win.height)
1988 DBG(1, "Failed to change the capture area size")
1993 /*--------------------------------------------------------------------------
1994 Adjust the asked values for window width and height.
1995 Return 0 on success, -1 otherwise.
1996 --------------------------------------------------------------------------*/
1998 w9968cf_adjust_window_size(struct w9968cf_device* cam, u16* width, u16* height)
2002 if ((*width < cam->minwidth) || (*height < cam->minheight))
2005 maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
2006 w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2008 maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
2009 w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2017 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2018 *width &= ~15L; /* multiple of 16 */
2022 PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
2028 /*--------------------------------------------------------------------------
2029 Initialize the FIFO list of requested frames.
2030 --------------------------------------------------------------------------*/
2031 static void w9968cf_init_framelist(struct w9968cf_device* cam)
2035 for (i = 0; i < cam->nbuffers; i++) {
2036 cam->requested_frame[i] = NULL;
2037 cam->frame[i].queued = 0;
2038 cam->frame[i].status = F_UNUSED;
2043 /*--------------------------------------------------------------------------
2044 Add a frame in the FIFO list of requested frames.
2045 This function is called in process context.
2046 --------------------------------------------------------------------------*/
2047 static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
2050 unsigned long lock_flags;
2052 spin_lock_irqsave(&cam->flist_lock, lock_flags);
2054 for (f=0; cam->requested_frame[f] != NULL; f++);
2055 cam->requested_frame[f] = &cam->frame[f_num];
2056 cam->frame[f_num].queued = 1;
2057 cam->frame[f_num].status = F_UNUSED; /* clear the status */
2059 spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
2061 DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
2065 /*--------------------------------------------------------------------------
2066 Read, store and remove the first pointer in the FIFO list of requested
2067 frames. This function is called in interrupt context.
2068 --------------------------------------------------------------------------*/
2070 w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
2074 spin_lock(&cam->flist_lock);
2076 *framep = cam->requested_frame[0];
2078 /* Shift the list of pointers */
2079 for (i = 0; i < cam->nbuffers-1; i++)
2080 cam->requested_frame[i] = cam->requested_frame[i+1];
2081 cam->requested_frame[i] = NULL;
2083 spin_unlock(&cam->flist_lock);
2085 DBG(6,"Popped frame #%d from the list", (*framep)->number)
2089 /*--------------------------------------------------------------------------
2090 High-level video post-processing routine on grabbed frames.
2091 Return 0 on success, a negative number otherwise.
2092 --------------------------------------------------------------------------*/
2094 w9968cf_postprocess_frame(struct w9968cf_device* cam,
2095 struct w9968cf_frame_t* fr)
2097 void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
2098 u16 w = cam->window.width,
2099 h = cam->window.height,
2100 d = cam->picture.depth,
2101 fmt = cam->picture.palette,
2102 rgb = cam->force_rgb,
2103 hw_w = cam->hw_width,
2104 hw_h = cam->hw_height,
2105 hw_d = cam->hw_depth;
2108 #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
2110 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2111 memcpy(pOut, pIn, fr->length);
2113 err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
2114 PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
2115 fr->length = (hw_w*hw_h*hw_d)/8;
2118 DBG(4, "An error occurred while decoding the frame: "
2119 "%s", symbolic(decoder_errlist, err))
2122 DBG(6, "Frame decoded")
2125 if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
2126 w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
2127 DBG(6, "Original UYVY component ordering changed")
2130 if (cam->vpp_flag & VPP_UPSCALE) {
2131 w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
2132 fr->length = (w*h*hw_d)/8;
2134 DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
2135 hw_w, hw_h, hw_d, w, h)
2138 if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
2139 w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
2140 fr->length = (w*h*d)/8;
2142 DBG(6, "UYVY-16bit to %s conversion done",
2143 symbolic(v4l1_plist, fmt))
2146 if (pOut == fr->buffer)
2147 memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
2154 /****************************************************************************
2155 * Image sensor control routines *
2156 ****************************************************************************/
2159 w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
2161 struct ovcamchip_control ctl;
2167 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
2174 w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
2176 struct ovcamchip_control ctl;
2181 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
2190 w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
2192 struct i2c_client* c = cam->sensor_client;
2195 if (!c || !c->driver || !c->driver->command)
2198 rc = c->driver->command(c, cmd, arg);
2199 /* The I2C driver returns -EPERM on non-supported controls */
2200 return (rc < 0 && rc != -EPERM) ? rc : 0;
2204 /*--------------------------------------------------------------------------
2205 Update some settings of the image sensor.
2206 Returns: 0 on success, a negative number otherwise.
2207 --------------------------------------------------------------------------*/
2208 static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
2212 /* Auto brightness */
2213 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
2219 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
2224 /* Banding filter */
2225 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
2230 /* Light frequency */
2231 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
2237 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
2243 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
2252 /*--------------------------------------------------------------------------
2253 Get some current picture settings from the image sensor and update the
2254 internal 'picture' structure of the camera.
2255 Returns: 0 on success, a negative number otherwise.
2256 --------------------------------------------------------------------------*/
2257 static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
2261 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
2264 cam->picture.contrast = v;
2266 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
2269 cam->picture.brightness = v;
2271 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
2274 cam->picture.colour = v;
2276 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
2279 cam->picture.hue = v;
2281 DBG(5, "Got picture settings from the image sensor")
2283 PDBGG("Brightness, contrast, hue, colour, whiteness are "
2284 "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
2285 cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
2291 /*--------------------------------------------------------------------------
2292 Update picture settings of the image sensor.
2293 Returns: 0 on success, a negative number otherwise.
2294 --------------------------------------------------------------------------*/
2296 w9968cf_sensor_update_picture(struct w9968cf_device* cam,
2297 struct video_picture pict)
2301 if ((!cam->sensor_initialized)
2302 || pict.contrast != cam->picture.contrast) {
2303 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
2307 DBG(4, "Contrast changed from %u to %u",
2308 cam->picture.contrast, pict.contrast)
2309 cam->picture.contrast = pict.contrast;
2312 if (((!cam->sensor_initialized) ||
2313 pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
2314 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
2318 DBG(4, "Brightness changed from %u to %u",
2319 cam->picture.brightness, pict.brightness)
2320 cam->picture.brightness = pict.brightness;
2323 if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
2324 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
2328 DBG(4, "Colour changed from %u to %u",
2329 cam->picture.colour, pict.colour)
2330 cam->picture.colour = pict.colour;
2333 if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
2334 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
2338 DBG(4, "Hue changed from %u to %u",
2339 cam->picture.hue, pict.hue)
2340 cam->picture.hue = pict.hue;
2346 DBG(4, "Failed to change sensor picture setting")
2352 /****************************************************************************
2353 * Camera configuration *
2354 ****************************************************************************/
2356 /*--------------------------------------------------------------------------
2357 This function is called when a supported image sensor is detected.
2358 Return 0 if the initialization succeeds, a negative number otherwise.
2359 --------------------------------------------------------------------------*/
2360 static int w9968cf_sensor_init(struct w9968cf_device* cam)
2364 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
2368 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
2372 /* NOTE: Make sure width and height are a multiple of 16 */
2373 switch (cam->sensor_client->addr) {
2375 cam->maxwidth = 352;
2376 cam->maxheight = 288;
2378 cam->minheight = 48;
2381 cam->maxwidth = 640;
2382 cam->maxheight = 480;
2384 cam->minheight = 48;
2387 DBG(1, "Not supported image sensor detected for %s",
2388 symbolic(camlist, cam->id))
2392 /* These values depend on the ones in the ovxxx0.c sources */
2393 switch (cam->sensor) {
2395 cam->start_cropx = 287;
2396 cam->start_cropy = 35;
2397 /* Seems to work around a bug in the image sensor */
2398 cam->vs_polarity = 1;
2399 cam->hs_polarity = 1;
2402 cam->start_cropx = 320;
2403 cam->start_cropy = 35;
2404 cam->vs_polarity = 1;
2405 cam->hs_polarity = 0;
2408 if ((err = w9968cf_sensor_update_settings(cam)))
2411 if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
2414 cam->sensor_initialized = 1;
2416 DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
2420 cam->sensor_initialized = 0;
2421 cam->sensor = CC_UNKNOWN;
2422 DBG(1, "Image sensor initialization failed for %s (/dev/video%d). "
2423 "Try to detach and attach this device again",
2424 symbolic(camlist, cam->id), cam->v4ldev->minor)
2429 /*--------------------------------------------------------------------------
2430 Fill some basic fields in the main device data structure.
2431 This function is called once on w9968cf_usb_probe() for each recognized
2433 --------------------------------------------------------------------------*/
2435 w9968cf_configure_camera(struct w9968cf_device* cam,
2436 struct usb_device* udev,
2437 enum w9968cf_model_id mod_id,
2438 const unsigned short dev_nr)
2440 init_MUTEX(&cam->fileop_sem);
2441 init_waitqueue_head(&cam->open);
2442 spin_lock_init(&cam->urb_lock);
2443 spin_lock_init(&cam->flist_lock);
2446 cam->disconnected = 0;
2448 cam->sensor = CC_UNKNOWN;
2449 cam->sensor_initialized = 0;
2451 /* Calculate the alternate setting number (from 1 to 16)
2452 according to the 'packet_size' module parameter */
2453 if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
2454 packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
2455 for (cam->altsetting = 1;
2456 packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
2459 cam->max_buffers = (max_buffers[dev_nr] < 2 ||
2460 max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
2461 ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
2463 cam->double_buffer = (double_buffer[dev_nr] == 0 ||
2464 double_buffer[dev_nr] == 1)
2465 ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
2467 cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
2468 ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
2470 cam->filter_type = (filter_type[dev_nr] == 0 ||
2471 filter_type[dev_nr] == 1 ||
2472 filter_type[dev_nr] == 2)
2473 ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
2477 cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
2478 ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
2480 cam->decompression = (decompression[dev_nr] == 0 ||
2481 decompression[dev_nr] == 1 ||
2482 decompression[dev_nr] == 2)
2483 ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
2485 cam->upscaling = (upscaling[dev_nr] == 0 ||
2486 upscaling[dev_nr] == 1)
2487 ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
2489 cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
2490 ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
2492 cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
2493 ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
2495 cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
2496 ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
2498 cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
2499 bandingfilter[dev_nr] == 1)
2500 ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
2502 cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
2503 ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
2505 cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
2506 ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
2508 cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
2509 ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
2511 cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
2512 ? monochrome[dev_nr] : W9968CF_MONOCHROME;
2514 cam->picture.brightness = (u16)brightness[dev_nr];
2515 cam->picture.hue = (u16)hue[dev_nr];
2516 cam->picture.colour = (u16)colour[dev_nr];
2517 cam->picture.contrast = (u16)contrast[dev_nr];
2518 cam->picture.whiteness = (u16)whiteness[dev_nr];
2519 if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
2520 cam->picture.palette = (u16)force_palette[dev_nr];
2521 cam->force_palette = 1;
2523 cam->force_palette = 0;
2524 if (cam->decompression == 0)
2525 cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
2526 else if (cam->decompression == 1)
2527 cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
2529 cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
2531 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2533 cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
2534 ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
2538 cam->window.width = W9968CF_WIDTH;
2539 cam->window.height = W9968CF_HEIGHT;
2540 cam->window.chromakey = 0;
2541 cam->window.clipcount = 0;
2542 cam->window.flags = 0;
2544 DBG(3, "%s configured with settings #%u:",
2545 symbolic(camlist, cam->id), dev_nr)
2547 DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
2548 wMaxPacketSize[cam->altsetting-1])
2550 DBG(3, "- Number of requested video frame buffers: %u",
2553 if (cam->double_buffer)
2554 DBG(3, "- Hardware double buffering enabled")
2556 DBG(3, "- Hardware double buffering disabled")
2558 if (cam->filter_type == 0)
2559 DBG(3, "- Video filtering disabled")
2560 else if (cam->filter_type == 1)
2561 DBG(3, "- Video filtering enabled: type 1-2-1")
2562 else if (cam->filter_type == 2)
2563 DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
2566 DBG(3, "- Video data clamping (CCIR-601 format) enabled")
2568 DBG(3, "- Video data clamping (CCIR-601 format) disabled")
2571 DBG(3, "- Large view enabled")
2573 DBG(3, "- Large view disabled")
2575 if ((cam->decompression) == 0 && (!cam->force_palette))
2576 DBG(3, "- Decompression disabled")
2577 else if ((cam->decompression) == 1 && (!cam->force_palette))
2578 DBG(3, "- Decompression forced")
2579 else if ((cam->decompression) == 2 && (!cam->force_palette))
2580 DBG(3, "- Decompression allowed")
2583 DBG(3, "- Software image scaling enabled")
2585 DBG(3, "- Software image scaling disabled")
2587 if (cam->force_palette)
2588 DBG(3, "- Image palette forced to %s",
2589 symbolic(v4l1_plist, cam->picture.palette))
2592 DBG(3, "- RGB component ordering will be used instead of BGR")
2595 DBG(3, "- Auto brightness enabled")
2597 DBG(3, "- Auto brightness disabled")
2600 DBG(3, "- Auto exposure enabled")
2602 DBG(3, "- Auto exposure disabled")
2605 DBG(3, "- Backlight exposure algorithm enabled")
2607 DBG(3, "- Backlight exposure algorithm disabled")
2610 DBG(3, "- Mirror enabled")
2612 DBG(3, "- Mirror disabled")
2615 DBG(3, "- Banding filter enabled")
2617 DBG(3, "- Banding filter disabled")
2619 DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
2621 if (cam->clockdiv == -1)
2622 DBG(3, "- Automatic clock divisor enabled")
2624 DBG(3, "- Clock divisor: %d", cam->clockdiv)
2626 if (cam->monochrome)
2627 DBG(3, "- Image sensor used as monochrome")
2629 DBG(3, "- Image sensor not used as monochrome")
2633 /*--------------------------------------------------------------------------
2634 If the video post-processing module is not loaded, some parameters
2636 --------------------------------------------------------------------------*/
2637 static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2640 if (cam->decompression == 1) {
2641 cam->decompression = 2;
2642 DBG(2, "Video post-processing module not found: "
2643 "'decompression' parameter forced to 2")
2645 if (cam->upscaling) {
2647 DBG(2, "Video post-processing module not found: "
2648 "'upscaling' parameter forced to 0")
2650 if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
2651 cam->force_palette = 0;
2652 DBG(2, "Video post-processing module not found: "
2653 "'force_palette' parameter forced to 0")
2655 cam->picture.palette = VIDEO_PALETTE_UYVY;
2656 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2661 /*--------------------------------------------------------------------------
2662 Release the resources used by the driver.
2663 This function is called on disconnect
2664 (or on close if deallocation has been deferred)
2665 --------------------------------------------------------------------------*/
2666 static void w9968cf_release_resources(struct w9968cf_device* cam)
2668 down(&w9968cf_devlist_sem);
2670 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->minor)
2672 video_unregister_device(cam->v4ldev);
2673 list_del(&cam->v4llist);
2674 i2c_del_adapter(&cam->i2c_adapter);
2675 w9968cf_deallocate_memory(cam);
2676 kfree(cam->control_buffer);
2677 kfree(cam->data_buffer);
2679 up(&w9968cf_devlist_sem);
2684 /****************************************************************************
2685 * Video4Linux interface *
2686 ****************************************************************************/
2688 static int w9968cf_open(struct inode* inode, struct file* filp)
2690 struct w9968cf_device* cam;
2693 /* This the only safe way to prevent race conditions with disconnect */
2694 if (!down_read_trylock(&w9968cf_disconnect))
2695 return -ERESTARTSYS;
2697 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2699 down(&cam->dev_sem);
2701 if (cam->sensor == CC_UNKNOWN) {
2702 DBG(2, "No supported image sensor has been detected by the "
2703 "'ovcamchip' module for the %s (/dev/video%d). Make "
2704 "sure it is loaded *before* (re)connecting the camera.",
2705 symbolic(camlist, cam->id), cam->v4ldev->minor)
2707 up_read(&w9968cf_disconnect);
2712 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
2713 symbolic(camlist, cam->id),cam->v4ldev->minor,cam->command)
2714 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
2716 up_read(&w9968cf_disconnect);
2717 return -EWOULDBLOCK;
2720 err = wait_event_interruptible_exclusive(cam->open,
2721 cam->disconnected ||
2724 up_read(&w9968cf_disconnect);
2727 if (cam->disconnected) {
2728 up_read(&w9968cf_disconnect);
2731 down(&cam->dev_sem);
2734 DBG(5, "Opening '%s', /dev/video%d ...",
2735 symbolic(camlist, cam->id), cam->v4ldev->minor)
2738 cam->misconfigured = 0;
2741 if ((err = w9968cf_vppmod_detect(cam)))
2744 if ((err = w9968cf_allocate_memory(cam)))
2745 goto deallocate_memory;
2747 if ((err = w9968cf_init_chip(cam)))
2748 goto deallocate_memory;
2750 if ((err = w9968cf_start_transfer(cam)))
2751 goto deallocate_memory;
2753 filp->private_data = cam;
2756 strcpy(cam->command, current->comm);
2758 init_waitqueue_head(&cam->wait_queue);
2760 DBG(5, "Video device is open")
2763 up_read(&w9968cf_disconnect);
2768 w9968cf_deallocate_memory(cam);
2770 DBG(2, "Failed to open the video device")
2772 up_read(&w9968cf_disconnect);
2777 static int w9968cf_release(struct inode* inode, struct file* filp)
2779 struct w9968cf_device* cam;
2781 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2783 down(&cam->dev_sem); /* prevent disconnect() to be called */
2785 w9968cf_stop_transfer(cam);
2787 w9968cf_vppmod_release(cam);
2789 if (cam->disconnected) {
2790 w9968cf_release_resources(cam);
2797 w9968cf_deallocate_memory(cam);
2798 wake_up_interruptible_nr(&cam->open, 1);
2800 DBG(5, "Video device closed")
2807 w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2809 struct w9968cf_device* cam;
2810 struct w9968cf_frame_t* fr;
2813 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2815 if (filp->f_flags & O_NONBLOCK)
2816 return -EWOULDBLOCK;
2818 if (down_interruptible(&cam->fileop_sem))
2819 return -ERESTARTSYS;
2821 if (cam->disconnected) {
2822 DBG(2, "Device not present")
2823 up(&cam->fileop_sem);
2827 if (cam->misconfigured) {
2828 DBG(2, "The camera is misconfigured. Close and open it again.")
2829 up(&cam->fileop_sem);
2833 if (!cam->frame[0].queued)
2834 w9968cf_push_frame(cam, 0);
2836 if (!cam->frame[1].queued)
2837 w9968cf_push_frame(cam, 1);
2839 err = wait_event_interruptible(cam->wait_queue,
2840 cam->frame[0].status == F_READY ||
2841 cam->frame[1].status == F_READY ||
2844 up(&cam->fileop_sem);
2847 if (cam->disconnected) {
2848 up(&cam->fileop_sem);
2852 fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
2855 w9968cf_postprocess_frame(cam, fr);
2857 if (count > fr->length)
2860 if (copy_to_user(buf, fr->buffer, count)) {
2861 fr->status = F_UNUSED;
2862 up(&cam->fileop_sem);
2867 fr->status = F_UNUSED;
2869 DBG(5, "%zu bytes read", count)
2871 up(&cam->fileop_sem);
2876 static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
2878 struct w9968cf_device* cam = (struct w9968cf_device*)
2879 video_get_drvdata(video_devdata(filp));
2880 unsigned long vsize = vma->vm_end - vma->vm_start,
2881 psize = cam->nbuffers * cam->frame[0].size,
2882 start = vma->vm_start,
2883 pos = (unsigned long)cam->frame[0].buffer,
2886 if (cam->disconnected) {
2887 DBG(2, "Device not present")
2891 if (cam->misconfigured) {
2892 DBG(2, "The camera is misconfigured. Close and open it again")
2896 PDBGG("mmapping %lu bytes...", vsize)
2898 if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
2902 page = vmalloc_to_pfn((void *)pos);
2903 if (remap_pfn_range(vma, start, page + vma->vm_pgoff,
2904 PAGE_SIZE, vma->vm_page_prot))
2911 DBG(5, "mmap method successfully called")
2917 w9968cf_ioctl(struct inode* inode, struct file* filp,
2918 unsigned int cmd, unsigned long arg)
2920 struct w9968cf_device* cam;
2923 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2925 if (down_interruptible(&cam->fileop_sem))
2926 return -ERESTARTSYS;
2928 if (cam->disconnected) {
2929 DBG(2, "Device not present")
2930 up(&cam->fileop_sem);
2934 if (cam->misconfigured) {
2935 DBG(2, "The camera is misconfigured. Close and open it again.")
2936 up(&cam->fileop_sem);
2940 err = w9968cf_v4l_ioctl(inode, filp, cmd, (void __user *)arg);
2942 up(&cam->fileop_sem);
2947 static int w9968cf_v4l_ioctl(struct inode* inode, struct file* filp,
2948 unsigned int cmd, void __user * arg)
2950 struct w9968cf_device* cam;
2951 const char* v4l1_ioctls[] = {
2952 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
2953 "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
2954 "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
2955 "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
2956 "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
2957 "GVBIFMT", "SVBIFMT"
2960 #define V4L1_IOCTL(cmd) \
2961 ((_IOC_NR((cmd)) < sizeof(v4l1_ioctls)/sizeof(char*)) ? \
2962 v4l1_ioctls[_IOC_NR((cmd))] : "?")
2964 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2968 case VIDIOCGCAP: /* get video capability */
2970 struct video_capability cap = {
2971 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
2974 .minwidth = cam->minwidth,
2975 .minheight = cam->minheight,
2977 sprintf(cap.name, "W996[87]CF USB Camera #%d",
2978 cam->v4ldev->minor);
2979 cap.maxwidth = (cam->upscaling && w9968cf_vpp)
2980 ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2982 cap.maxheight = (cam->upscaling && w9968cf_vpp)
2983 ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2986 if (copy_to_user(arg, &cap, sizeof(cap)))
2989 DBG(5, "VIDIOCGCAP successfully called")
2993 case VIDIOCGCHAN: /* get video channel informations */
2995 struct video_channel chan;
2996 if (copy_from_user(&chan, arg, sizeof(chan)))
2999 if (chan.channel != 0)
3002 strcpy(chan.name, "Camera");
3005 chan.type = VIDEO_TYPE_CAMERA;
3006 chan.norm = VIDEO_MODE_AUTO;
3008 if (copy_to_user(arg, &chan, sizeof(chan)))
3011 DBG(5, "VIDIOCGCHAN successfully called")
3015 case VIDIOCSCHAN: /* set active channel */
3017 struct video_channel chan;
3019 if (copy_from_user(&chan, arg, sizeof(chan)))
3022 if (chan.channel != 0)
3025 DBG(5, "VIDIOCSCHAN successfully called")
3029 case VIDIOCGPICT: /* get image properties of the picture */
3031 if (w9968cf_sensor_get_picture(cam))
3034 if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
3037 DBG(5, "VIDIOCGPICT successfully called")
3041 case VIDIOCSPICT: /* change picture settings */
3043 struct video_picture pict;
3046 if (copy_from_user(&pict, arg, sizeof(pict)))
3049 if ( (cam->force_palette || !w9968cf_vpp)
3050 && pict.palette != cam->picture.palette ) {
3051 DBG(4, "Palette %s rejected: only %s is allowed",
3052 symbolic(v4l1_plist, pict.palette),
3053 symbolic(v4l1_plist, cam->picture.palette))
3057 if (!w9968cf_valid_palette(pict.palette)) {
3058 DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
3059 symbolic(v4l1_plist, pict.palette))
3063 if (!cam->force_palette) {
3064 if (cam->decompression == 0) {
3065 if (w9968cf_need_decompression(pict.palette)) {
3066 DBG(4, "Decompression disabled: palette %s is not "
3067 "allowed. VIDIOCSPICT failed",
3068 symbolic(v4l1_plist, pict.palette))
3071 } else if (cam->decompression == 1) {
3072 if (!w9968cf_need_decompression(pict.palette)) {
3073 DBG(4, "Decompression forced: palette %s is not "
3074 "allowed. VIDIOCSPICT failed",
3075 symbolic(v4l1_plist, pict.palette))
3081 if (pict.depth != w9968cf_valid_depth(pict.palette)) {
3082 DBG(4, "Requested depth %u bpp is not valid for %s "
3083 "palette: ignored and changed to %u bpp",
3084 pict.depth, symbolic(v4l1_plist, pict.palette),
3085 w9968cf_valid_depth(pict.palette))
3086 pict.depth = w9968cf_valid_depth(pict.palette);
3089 if (pict.palette != cam->picture.palette) {
3090 if(*cam->requested_frame
3091 || cam->frame_current->queued) {
3092 err = wait_event_interruptible
3094 cam->disconnected ||
3095 (!*cam->requested_frame &&
3096 !cam->frame_current->queued) );
3099 if (cam->disconnected)
3103 if (w9968cf_stop_transfer(cam))
3106 if (w9968cf_set_picture(cam, pict))
3109 if (w9968cf_start_transfer(cam))
3112 } else if (w9968cf_sensor_update_picture(cam, pict))
3116 DBG(5, "VIDIOCSPICT successfully called")
3120 case VIDIOCSWIN: /* set capture area */
3122 struct video_window win;
3125 if (copy_from_user(&win, arg, sizeof(win)))
3128 DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
3129 "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
3130 win.x, win.y, win.width, win.height)
3132 if (win.clipcount != 0 || win.flags != 0)
3135 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
3136 (u16*)&win.height))) {
3137 DBG(4, "Resolution not supported (%ux%u). "
3138 "VIDIOCSWIN failed", win.width, win.height)
3142 if (win.x != cam->window.x ||
3143 win.y != cam->window.y ||
3144 win.width != cam->window.width ||
3145 win.height != cam->window.height) {
3146 if(*cam->requested_frame
3147 || cam->frame_current->queued) {
3148 err = wait_event_interruptible
3150 cam->disconnected ||
3151 (!*cam->requested_frame &&
3152 !cam->frame_current->queued) );
3155 if (cam->disconnected)
3159 if (w9968cf_stop_transfer(cam))
3162 /* This _must_ be called before set_window() */
3163 if (w9968cf_set_picture(cam, cam->picture))
3166 if (w9968cf_set_window(cam, win))
3169 if (w9968cf_start_transfer(cam))
3173 DBG(5, "VIDIOCSWIN successfully called. ")
3177 case VIDIOCGWIN: /* get current window properties */
3179 if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
3182 DBG(5, "VIDIOCGWIN successfully called")
3186 case VIDIOCGMBUF: /* request for memory (mapped) buffer */
3188 struct video_mbuf mbuf;
3191 mbuf.size = cam->nbuffers * cam->frame[0].size;
3192 mbuf.frames = cam->nbuffers;
3193 for (i = 0; i < cam->nbuffers; i++)
3194 mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
3195 (unsigned long)cam->frame[0].buffer;
3197 if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
3200 DBG(5, "VIDIOCGMBUF successfully called")
3204 case VIDIOCMCAPTURE: /* start the capture to a frame */
3206 struct video_mmap mmap;
3207 struct w9968cf_frame_t* fr;
3210 if (copy_from_user(&mmap, arg, sizeof(mmap)))
3213 DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
3214 mmap.frame, symbolic(v4l1_plist, mmap.format),
3215 mmap.width, mmap.height)
3217 if (mmap.frame >= cam->nbuffers) {
3218 DBG(4, "Invalid frame number (%u). "
3219 "VIDIOCMCAPTURE failed", mmap.frame)
3223 if (mmap.format!=cam->picture.palette &&
3224 (cam->force_palette || !w9968cf_vpp)) {
3225 DBG(4, "Palette %s rejected: only %s is allowed",
3226 symbolic(v4l1_plist, mmap.format),
3227 symbolic(v4l1_plist, cam->picture.palette))
3231 if (!w9968cf_valid_palette(mmap.format)) {
3232 DBG(4, "Palette %s not supported. "
3233 "VIDIOCMCAPTURE failed",
3234 symbolic(v4l1_plist, mmap.format))
3238 if (!cam->force_palette) {
3239 if (cam->decompression == 0) {
3240 if (w9968cf_need_decompression(mmap.format)) {
3241 DBG(4, "Decompression disabled: palette %s is not "
3242 "allowed. VIDIOCSPICT failed",
3243 symbolic(v4l1_plist, mmap.format))
3246 } else if (cam->decompression == 1) {
3247 if (!w9968cf_need_decompression(mmap.format)) {
3248 DBG(4, "Decompression forced: palette %s is not "
3249 "allowed. VIDIOCSPICT failed",
3250 symbolic(v4l1_plist, mmap.format))
3256 if ((err = w9968cf_adjust_window_size(cam, (u16*)&mmap.width,
3257 (u16*)&mmap.height))) {
3258 DBG(4, "Resolution not supported (%dx%d). "
3259 "VIDIOCMCAPTURE failed",
3260 mmap.width, mmap.height)
3264 fr = &cam->frame[mmap.frame];
3266 if (mmap.width != cam->window.width ||
3267 mmap.height != cam->window.height ||
3268 mmap.format != cam->picture.palette) {
3270 struct video_window win;
3271 struct video_picture pict;
3273 if(*cam->requested_frame
3274 || cam->frame_current->queued) {
3275 DBG(6, "VIDIOCMCAPTURE. Change settings for "
3276 "frame #%u: %dx%d, format %s. Wait...",
3277 mmap.frame, mmap.width, mmap.height,
3278 symbolic(v4l1_plist, mmap.format))
3279 err = wait_event_interruptible
3281 cam->disconnected ||
3282 (!*cam->requested_frame &&
3283 !cam->frame_current->queued) );
3286 if (cam->disconnected)
3290 memcpy(&win, &cam->window, sizeof(win));
3291 memcpy(&pict, &cam->picture, sizeof(pict));
3292 win.width = mmap.width;
3293 win.height = mmap.height;
3294 pict.palette = mmap.format;
3296 if (w9968cf_stop_transfer(cam))
3299 /* This before set_window */
3300 if (w9968cf_set_picture(cam, pict))
3303 if (w9968cf_set_window(cam, win))
3306 if (w9968cf_start_transfer(cam))
3309 } else if (fr->queued) {
3311 DBG(6, "Wait until frame #%u is free", mmap.frame)
3313 err = wait_event_interruptible(cam->wait_queue,
3314 cam->disconnected ||
3318 if (cam->disconnected)
3322 w9968cf_push_frame(cam, mmap.frame);
3323 DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
3327 case VIDIOCSYNC: /* wait until the capture of a frame is finished */
3330 struct w9968cf_frame_t* fr;
3333 if (copy_from_user(&f_num, arg, sizeof(f_num)))
3336 if (f_num >= cam->nbuffers) {
3337 DBG(4, "Invalid frame number (%u). "
3338 "VIDIOCMCAPTURE failed", f_num)
3342 DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
3344 fr = &cam->frame[f_num];
3346 switch (fr->status) {
3349 DBG(4, "VIDIOSYNC: Frame #%u not requested!",
3355 err = wait_event_interruptible(cam->wait_queue,
3356 (fr->status == F_READY)
3357 || cam->disconnected);
3360 if (cam->disconnected)
3368 w9968cf_postprocess_frame(cam, fr);
3370 fr->status = F_UNUSED;
3372 DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
3376 case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
3378 struct video_unit unit = {
3379 .video = cam->v4ldev->minor,
3380 .vbi = VIDEO_NO_UNIT,
3381 .radio = VIDEO_NO_UNIT,
3382 .audio = VIDEO_NO_UNIT,
3383 .teletext = VIDEO_NO_UNIT,
3386 if (copy_to_user(arg, &unit, sizeof(unit)))
3389 DBG(5, "VIDIOCGUNIT successfully called")
3398 if (clear_user(arg, sizeof(struct video_buffer)))
3401 DBG(5, "VIDIOCGFBUF successfully called")
3407 struct video_tuner tuner;
3408 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3411 if (tuner.tuner != 0)
3414 strcpy(tuner.name, "no_tuner");
3416 tuner.rangehigh = 0;
3417 tuner.flags = VIDEO_TUNER_NORM;
3418 tuner.mode = VIDEO_MODE_AUTO;
3419 tuner.signal = 0xffff;
3421 if (copy_to_user(arg, &tuner, sizeof(tuner)))
3424 DBG(5, "VIDIOCGTUNER successfully called")
3430 struct video_tuner tuner;
3431 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3434 if (tuner.tuner != 0)
3437 if (tuner.mode != VIDEO_MODE_AUTO)
3440 DBG(5, "VIDIOCSTUNER successfully called")
3450 case VIDIOCSPLAYMODE:
3451 case VIDIOCSWRITEMODE:
3452 case VIDIOCGPLAYINFO:
3453 case VIDIOCSMICROCODE:
3456 DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
3462 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3467 DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
3473 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3475 return -ENOIOCTLCMD;
3477 } /* end of switch */
3480 cam->misconfigured = 1;
3481 DBG(1, "VIDIOC%s failed because of hardware problems. "
3482 "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
3487 static struct file_operations w9968cf_fops = {
3488 .owner = THIS_MODULE,
3489 .open = w9968cf_open,
3490 .release = w9968cf_release,
3491 .read = w9968cf_read,
3492 .ioctl = w9968cf_ioctl,
3493 .mmap = w9968cf_mmap,
3494 .llseek = no_llseek,
3499 /****************************************************************************
3500 * USB probe and V4L registration, disconnect and id_table[] definition *
3501 ****************************************************************************/
3504 w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3506 struct usb_device *udev = interface_to_usbdev(intf);
3507 struct w9968cf_device* cam;
3509 enum w9968cf_model_id mod_id;
3510 struct list_head* ptr;
3511 u8 sc = 0; /* number of simultaneous cameras */
3512 static unsigned short dev_nr = 0; /* we are handling device number n */
3514 if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[0].idVendor &&
3515 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[0].idProduct)
3516 mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
3517 else if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[1].idVendor &&
3518 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[1].idProduct)
3519 mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
3523 cam = (struct w9968cf_device*)
3524 kmalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
3528 memset(cam, 0, sizeof(*cam));
3530 init_MUTEX(&cam->dev_sem);
3531 down(&cam->dev_sem);
3534 /* NOTE: a local copy is used to avoid possible race conditions */
3535 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
3537 DBG(2, "%s detected", symbolic(camlist, mod_id))
3539 if (simcams > W9968CF_MAX_DEVICES)
3540 simcams = W9968CF_SIMCAMS;
3542 /* How many cameras are connected ? */
3543 down(&w9968cf_devlist_sem);
3544 list_for_each(ptr, &w9968cf_dev_list)
3546 up(&w9968cf_devlist_sem);
3548 if (sc >= simcams) {
3549 DBG(2, "Device rejected: too many connected cameras "
3550 "(max. %u)", simcams)
3556 /* Allocate 2 bytes of memory for camera control USB transfers */
3557 if (!(cam->control_buffer = (u16*)kmalloc(2, GFP_KERNEL))) {
3558 DBG(1,"Couldn't allocate memory for camera control transfers")
3562 memset(cam->control_buffer, 0, 2);
3564 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
3565 if (!(cam->data_buffer = (u16*)kmalloc(8, GFP_KERNEL))) {
3566 DBG(1, "Couldn't allocate memory for data "
3567 "transfers to the FSB")
3571 memset(cam->data_buffer, 0, 8);
3573 /* Register the V4L device */
3574 cam->v4ldev = video_device_alloc();
3576 DBG(1, "Could not allocate memory for a V4L structure")
3581 strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
3582 cam->v4ldev->owner = THIS_MODULE;
3583 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
3584 cam->v4ldev->hardware = VID_HARDWARE_W9968CF;
3585 cam->v4ldev->fops = &w9968cf_fops;
3586 cam->v4ldev->minor = video_nr[dev_nr];
3587 cam->v4ldev->release = video_device_release;
3588 video_set_drvdata(cam->v4ldev, cam);
3589 cam->v4ldev->dev = &cam->dev;
3591 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3594 DBG(1, "V4L device registration failed")
3595 if (err == -ENFILE && video_nr[dev_nr] == -1)
3596 DBG(2, "Couldn't find a free /dev/videoX node")
3597 video_nr[dev_nr] = -1;
3598 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3602 DBG(2, "V4L device registered as /dev/video%d", cam->v4ldev->minor)
3604 /* Set some basic constants */
3605 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3607 /* Add a new entry into the list of V4L registered devices */
3608 down(&w9968cf_devlist_sem);
3609 list_add(&cam->v4llist, &w9968cf_dev_list);
3610 up(&w9968cf_devlist_sem);
3611 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3613 w9968cf_turn_on_led(cam);
3615 w9968cf_i2c_init(cam);
3617 usb_set_intfdata(intf, cam);
3621 fail: /* Free unused memory */
3622 kfree(cam->control_buffer);
3623 kfree(cam->data_buffer);
3625 video_device_release(cam->v4ldev);
3632 static void w9968cf_usb_disconnect(struct usb_interface* intf)
3634 struct w9968cf_device* cam =
3635 (struct w9968cf_device*)usb_get_intfdata(intf);
3637 down_write(&w9968cf_disconnect);
3640 /* Prevent concurrent accesses to data */
3641 down(&cam->dev_sem);
3643 cam->disconnected = 1;
3645 DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id))
3647 wake_up_interruptible_all(&cam->open);
3650 DBG(2, "The device is open (/dev/video%d)! "
3651 "Process name: %s. Deregistration and memory "
3652 "deallocation are deferred on close.",
3653 cam->v4ldev->minor, cam->command)
3654 cam->misconfigured = 1;
3655 w9968cf_stop_transfer(cam);
3656 wake_up_interruptible(&cam->wait_queue);
3658 w9968cf_release_resources(cam);
3666 up_write(&w9968cf_disconnect);
3670 static struct usb_driver w9968cf_usb_driver = {
3672 .id_table = winbond_id_table,
3673 .probe = w9968cf_usb_probe,
3674 .disconnect = w9968cf_usb_disconnect,
3679 /****************************************************************************
3680 * Module init, exit and intermodule communication *
3681 ****************************************************************************/
3683 static int w9968cf_vppmod_detect(struct w9968cf_device* cam)
3687 request_module("w9968cf-vpp");
3689 down(&w9968cf_vppmod_lock);
3692 DBG(4, "Video post-processing module not detected")
3693 w9968cf_adjust_configuration(cam);
3697 if (!try_module_get(w9968cf_vpp->owner)) {
3698 DBG(1, "Couldn't increment the reference count of "
3699 "the video post-processing module")
3700 up(&w9968cf_vppmod_lock);
3704 w9968cf_vpp->busy++;
3706 DBG(5, "Video post-processing module detected")
3709 up(&w9968cf_vppmod_lock);
3714 static void w9968cf_vppmod_release(struct w9968cf_device* cam)
3716 down(&w9968cf_vppmod_lock);
3718 if (w9968cf_vpp && w9968cf_vpp->busy) {
3719 module_put(w9968cf_vpp->owner);
3720 w9968cf_vpp->busy--;
3721 wake_up(&w9968cf_vppmod_wait);
3722 DBG(5, "Video post-processing module released")
3725 up(&w9968cf_vppmod_lock);
3729 int w9968cf_vppmod_register(struct w9968cf_vpp_t* vpp)
3731 down(&w9968cf_vppmod_lock);
3734 KDBG(1, "Video post-processing module already registered")
3735 up(&w9968cf_vppmod_lock);
3740 w9968cf_vpp->busy = 0;
3742 KDBG(2, "Video post-processing module registered")
3743 up(&w9968cf_vppmod_lock);
3748 int w9968cf_vppmod_deregister(struct w9968cf_vpp_t* vpp)
3750 down(&w9968cf_vppmod_lock);
3753 up(&w9968cf_vppmod_lock);
3757 if (w9968cf_vpp != vpp) {
3758 KDBG(1, "Only the owner can unregister the video "
3759 "post-processing module")
3760 up(&w9968cf_vppmod_lock);
3764 if (w9968cf_vpp->busy) {
3765 KDBG(2, "Video post-processing module busy. Wait for it to be "
3767 up(&w9968cf_vppmod_lock);
3768 wait_event(w9968cf_vppmod_wait, !w9968cf_vpp->busy);
3775 up(&w9968cf_vppmod_lock);
3778 KDBG(2, "Video post-processing module unregistered")
3783 static int __init w9968cf_module_init(void)
3787 KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
3788 KDBG(3, W9968CF_MODULE_AUTHOR)
3791 request_module("ovcamchip");
3793 if ((err = usb_register(&w9968cf_usb_driver)))
3800 static void __exit w9968cf_module_exit(void)
3802 /* w9968cf_usb_disconnect() will be called */
3803 usb_deregister(&w9968cf_usb_driver);
3805 KDBG(2, W9968CF_MODULE_NAME" deregistered")
3809 module_init(w9968cf_module_init);
3810 module_exit(w9968cf_module_exit);
3813 EXPORT_SYMBOL(w9968cf_vppmod_register);
3814 EXPORT_SYMBOL(w9968cf_vppmod_deregister);