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/version.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/kmod.h>
32 #include <linux/init.h>
34 #include <linux/vmalloc.h>
35 #include <linux/slab.h>
37 #include <linux/string.h>
38 #include <linux/errno.h>
39 #include <linux/sched.h>
40 #include <linux/ioctl.h>
41 #include <linux/delay.h>
42 #include <linux/stddef.h>
44 #include <asm/uaccess.h>
45 #include <linux/page-flags.h>
46 #include <linux/moduleparam.h>
49 #include "w9968cf_decoder.h"
53 /****************************************************************************
54 * Module macros and parameters *
55 ****************************************************************************/
57 MODULE_DEVICE_TABLE(usb, winbond_id_table);
59 MODULE_AUTHOR(W9968CF_MODULE_AUTHOR" "W9968CF_AUTHOR_EMAIL);
60 MODULE_DESCRIPTION(W9968CF_MODULE_NAME);
61 MODULE_VERSION(W9968CF_MODULE_VERSION);
62 MODULE_LICENSE(W9968CF_MODULE_LICENSE);
63 MODULE_SUPPORTED_DEVICE("Video");
65 static int ovmod_load = W9968CF_OVMOD_LOAD;
66 static int vppmod_load = W9968CF_VPPMOD_LOAD;
67 static unsigned short simcams = W9968CF_SIMCAMS;
68 static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
69 static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
71 static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
73 static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
74 W9968CF_DOUBLE_BUFFER};
75 static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
76 static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
78 static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
79 static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
80 W9968CF_DECOMPRESSION};
81 static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
82 static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
83 static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
84 static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
85 static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
86 static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
88 static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
89 W9968CF_BANDINGFILTER};
90 static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
91 static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
92 static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
93 static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
94 static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
96 static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
97 static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
98 static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
100 static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
103 static unsigned short debug = W9968CF_DEBUG_LEVEL;
104 static int specific_debug = W9968CF_SPECIFIC_DEBUG;
107 static unsigned int param_nv[24]; /* number of values per parameter */
110 module_param(ovmod_load, bool, 0644);
111 module_param(vppmod_load, bool, 0444);
113 module_param(simcams, ushort, 0644);
114 module_param_array(video_nr, short, param_nv[0], 0444);
115 module_param_array(packet_size, uint, param_nv[1], 0444);
116 module_param_array(max_buffers, ushort, param_nv[2], 0444);
117 module_param_array(double_buffer, bool, param_nv[3], 0444);
118 module_param_array(clamping, bool, param_nv[4], 0444);
119 module_param_array(filter_type, ushort, param_nv[5], 0444);
120 module_param_array(largeview, bool, param_nv[6], 0444);
121 module_param_array(decompression, ushort, param_nv[7], 0444);
122 module_param_array(upscaling, bool, param_nv[8], 0444);
123 module_param_array(force_palette, ushort, param_nv[9], 0444);
124 module_param_array(force_rgb, ushort, param_nv[10], 0444);
125 module_param_array(autobright, bool, param_nv[11], 0444);
126 module_param_array(autoexp, bool, param_nv[12], 0444);
127 module_param_array(lightfreq, ushort, param_nv[13], 0444);
128 module_param_array(bandingfilter, bool, param_nv[14], 0444);
129 module_param_array(clockdiv, short, param_nv[15], 0444);
130 module_param_array(backlight, bool, param_nv[16], 0444);
131 module_param_array(mirror, bool, param_nv[17], 0444);
132 module_param_array(monochrome, bool, param_nv[18], 0444);
133 module_param_array(brightness, uint, param_nv[19], 0444);
134 module_param_array(hue, uint, param_nv[20], 0444);
135 module_param_array(colour, uint, param_nv[21], 0444);
136 module_param_array(contrast, uint, param_nv[22], 0444);
137 module_param_array(whiteness, uint, param_nv[23], 0444);
139 module_param(debug, ushort, 0644);
140 module_param(specific_debug, bool, 0644);
144 MODULE_PARM_DESC(ovmod_load,
145 "\n<0|1> Automatic 'ovcamchip' module loading."
146 "\n0 disabled, 1 enabled."
147 "\nIf enabled,'insmod' searches for the required 'ovcamchip'"
148 "\nmodule in the system, according to its configuration, and"
149 "\nattempts to load that module automatically. This action is"
150 "\nperformed once as soon as the 'w9968cf' module is loaded"
152 "\nDefault value is "__MODULE_STRING(W9968CF_OVMOD_LOAD)"."
154 MODULE_PARM_DESC(vppmod_load,
155 "\n<0|1> Automatic 'w9968cf-vpp' module loading."
156 "\n0 disabled, 1 enabled."
157 "\nIf enabled, every time an application attempts to open a"
158 "\ncamera, 'insmod' searches for the video post-processing"
159 "\nmodule in the system and loads it automatically (if"
160 "\npresent). The optional 'w9968cf-vpp' module adds extra"
161 "\n image manipulation functions to the 'w9968cf' module,like"
162 "\nsoftware up-scaling,colour conversions and video decoding"
163 "\nfor very high frame rates."
164 "\nDefault value is "__MODULE_STRING(W9968CF_VPPMOD_LOAD)"."
167 MODULE_PARM_DESC(simcams,
168 "\n<n> Number of cameras allowed to stream simultaneously."
169 "\nn may vary from 0 to "
170 __MODULE_STRING(W9968CF_MAX_DEVICES)"."
171 "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
173 MODULE_PARM_DESC(video_nr,
174 "\n<-1|n[,...]> Specify V4L minor mode number."
175 "\n -1 = use next available (default)"
176 "\n n = use minor number n (integer >= 0)"
177 "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
180 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
181 "\nthe second camera and use auto for the first"
182 "\none and for every other camera."
184 MODULE_PARM_DESC(packet_size,
185 "\n<n[,...]> Specify the maximum data payload"
186 "\nsize in bytes for alternate settings, for each device."
187 "\nn is scaled between 63 and 1023 "
188 "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
190 MODULE_PARM_DESC(max_buffers,
191 "\n<n[,...]> For advanced users."
192 "\nSpecify the maximum number of video frame buffers"
193 "\nto allocate for each device, from 2 to "
194 __MODULE_STRING(W9968CF_MAX_BUFFERS)
195 ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
197 MODULE_PARM_DESC(double_buffer,
199 "Hardware double buffering: 0 disabled, 1 enabled."
200 "\nIt should be enabled if you want smooth video output: if"
201 "\nyou obtain out of sync. video, disable it, or try to"
202 "\ndecrease the 'clockdiv' module parameter value."
203 "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
206 MODULE_PARM_DESC(clamping,
207 "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
208 "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
211 MODULE_PARM_DESC(filter_type,
212 "\n<0|1|2[,...]> Video filter type."
213 "\n0 none, 1 (1-2-1) 3-tap filter, "
214 "2 (2-3-6-3-2) 5-tap filter."
215 "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
217 "\nThe filter is used to reduce noise and aliasing artifacts"
218 "\nproduced by the CCD or CMOS image sensor, and the scaling"
221 MODULE_PARM_DESC(largeview,
222 "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
223 "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
226 MODULE_PARM_DESC(upscaling,
227 "\n<0|1[,...]> Software scaling (for non-compressed video):"
228 "\n0 disabled, 1 enabled."
229 "\nDisable it if you have a slow CPU or you don't have"
231 "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
233 "\nIf 'w9968cf-vpp' is not present, this parameter is"
236 MODULE_PARM_DESC(decompression,
237 "\n<0|1|2[,...]> Software video decompression:"
238 "\n- 0 disables decompression (doesn't allow formats needing"
240 "\n- 1 forces decompression (allows formats needing"
241 " decompression only);"
242 "\n- 2 allows any permitted formats."
243 "\nFormats supporting compressed video are YUV422P and"
245 "\nin any resolutions where both width and height are "
247 "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
249 "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
250 "\nnot allowed; in this case this parameter is set to 2."
252 MODULE_PARM_DESC(force_palette,
254 "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
255 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
256 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
257 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
258 "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
259 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
260 "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
261 "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
262 "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
263 "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
264 "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
266 " Force picture palette."
268 "\n- 0 allows any of the following formats:"
269 "\n- UYVY 16 bpp - Original video, compression disabled"
270 "\n- YUV420 12 bpp - Original video, compression enabled"
271 "\n- YUV422P 16 bpp - Original video, compression enabled"
272 "\n- YUV420P 12 bpp - Original video, compression enabled"
273 "\n- YUVY 16 bpp - Software conversion from UYVY"
274 "\n- YUV422 16 bpp - Software conversion from UYVY"
275 "\n- GREY 8 bpp - Software conversion from UYVY"
276 "\n- RGB555 16 bpp - Software conversion from UYVY"
277 "\n- RGB565 16 bpp - Software conversion from UYVY"
278 "\n- RGB24 24 bpp - Software conversion from UYVY"
279 "\n- RGB32 32 bpp - Software conversion from UYVY"
280 "\nWhen not 0, this parameter will override 'decompression'."
281 "\nDefault value is 0 for every device."
282 "\nInitial palette is "
283 __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
284 "\nIf 'w9968cf-vpp' is not present, this parameter is"
287 MODULE_PARM_DESC(force_rgb,
288 "\n<0|1[,...]> Read RGB video data instead of BGR:"
289 "\n 1 = use RGB component ordering."
290 "\n 0 = use BGR component ordering."
291 "\nThis parameter has effect when using RGBX palettes only."
292 "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
295 MODULE_PARM_DESC(autobright,
296 "\n<0|1[,...]> Image sensor automatically changes brightness:"
298 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
301 MODULE_PARM_DESC(autoexp,
302 "\n<0|1[,...]> Image sensor automatically changes exposure:"
304 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
307 MODULE_PARM_DESC(lightfreq,
308 "\n<50|60[,...]> Light frequency in Hz:"
309 "\n 50 for European and Asian lighting,"
310 " 60 for American lighting."
311 "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
314 MODULE_PARM_DESC(bandingfilter,
315 "\n<0|1[,...]> Banding filter to reduce effects of"
316 " fluorescent lighting:"
317 "\n 0 disabled, 1 enabled."
318 "\nThis filter tries to reduce the pattern of horizontal"
319 "\nlight/dark bands caused by some (usually fluorescent)"
321 "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
324 MODULE_PARM_DESC(clockdiv,
326 "Force pixel clock divisor to a specific value (for experts):"
327 "\n n may vary from 0 to 127."
328 "\n -1 for automatic value."
329 "\nSee also the 'double_buffer' module parameter."
330 "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
333 MODULE_PARM_DESC(backlight,
334 "\n<0|1[,...]> Objects are lit from behind:"
336 "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
339 MODULE_PARM_DESC(mirror,
340 "\n<0|1[,...]> Reverse image horizontally:"
342 "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
345 MODULE_PARM_DESC(monochrome,
346 "\n<0|1[,...]> Use image sensor as monochrome sensor:"
348 "\nNot all the sensors support monochrome color."
349 "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
352 MODULE_PARM_DESC(brightness,
353 "\n<n[,...]> Set picture brightness (0-65535)."
354 "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
356 "\nThis parameter has no effect if 'autobright' is enabled."
358 MODULE_PARM_DESC(hue,
359 "\n<n[,...]> Set picture hue (0-65535)."
360 "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
363 MODULE_PARM_DESC(colour,
364 "\n<n[,...]> Set picture saturation (0-65535)."
365 "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
368 MODULE_PARM_DESC(contrast,
369 "\n<n[,...]> Set picture contrast (0-65535)."
370 "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
373 MODULE_PARM_DESC(whiteness,
374 "\n<n[,...]> Set picture whiteness (0-65535)."
375 "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
379 MODULE_PARM_DESC(debug,
380 "\n<n> Debugging information level, from 0 to 6:"
381 "\n0 = none (use carefully)"
382 "\n1 = critical errors"
383 "\n2 = significant informations"
384 "\n3 = configuration or general messages"
386 "\n5 = called functions"
387 "\n6 = function internals"
388 "\nLevel 5 and 6 are useful for testing only, when only "
389 "one device is used."
390 "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
392 MODULE_PARM_DESC(specific_debug,
393 "\n<0|1> Enable or disable specific debugging messages:"
394 "\n0 = print messages concerning every level"
396 "\n1 = print messages concerning the level"
397 " indicated by 'debug'."
398 "\nDefault value is "
399 __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
401 #endif /* W9968CF_DEBUG */
405 /****************************************************************************
407 ****************************************************************************/
409 /* Video4linux interface */
410 static struct file_operations w9968cf_fops;
411 static int w9968cf_open(struct inode*, struct file*);
412 static int w9968cf_release(struct inode*, struct file*);
413 static int w9968cf_mmap(struct file*, struct vm_area_struct*);
414 static int w9968cf_ioctl(struct inode*, struct file*, unsigned, unsigned long);
415 static ssize_t w9968cf_read(struct file*, char __user *, size_t, loff_t*);
416 static int w9968cf_v4l_ioctl(struct inode*, struct file*, unsigned int,
420 static int w9968cf_start_transfer(struct w9968cf_device*);
421 static int w9968cf_stop_transfer(struct w9968cf_device*);
422 static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
423 static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
424 static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
425 static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
426 static int w9968cf_read_sb(struct w9968cf_device*);
427 static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
428 static void w9968cf_urb_complete(struct urb *urb, struct pt_regs *regs);
430 /* Low-level I2C (SMBus) I/O */
431 static int w9968cf_smbus_start(struct w9968cf_device*);
432 static int w9968cf_smbus_stop(struct w9968cf_device*);
433 static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
434 static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
435 static int w9968cf_smbus_write_ack(struct w9968cf_device*);
436 static int w9968cf_smbus_read_ack(struct w9968cf_device*);
437 static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
438 static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
439 u16 address, u8* value);
440 static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
441 u8 subaddress, u8* value);
442 static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
443 u16 address, u8 subaddress);
444 static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
445 u16 address, u8 subaddress,
448 /* I2C interface to kernel */
449 static int w9968cf_i2c_init(struct w9968cf_device*);
450 static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
451 unsigned short flags, char read_write,
452 u8 command, int size, union i2c_smbus_data*);
453 static u32 w9968cf_i2c_func(struct i2c_adapter*);
454 static int w9968cf_i2c_attach_inform(struct i2c_client*);
455 static int w9968cf_i2c_detach_inform(struct i2c_client*);
456 static int w9968cf_i2c_control(struct i2c_adapter*, unsigned int cmd,
459 /* Memory management */
460 static inline unsigned long kvirt_to_pa(unsigned long adr);
461 static void* rvmalloc(unsigned long size);
462 static void rvfree(void *mem, unsigned long size);
463 static void w9968cf_deallocate_memory(struct w9968cf_device*);
464 static int w9968cf_allocate_memory(struct w9968cf_device*);
466 /* High-level image sensor control functions */
467 static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
468 static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
469 static int w9968cf_sensor_cmd(struct w9968cf_device*,
470 unsigned int cmd, void *arg);
471 static int w9968cf_sensor_init(struct w9968cf_device*);
472 static int w9968cf_sensor_update_settings(struct w9968cf_device*);
473 static int w9968cf_sensor_get_picture(struct w9968cf_device*);
474 static int w9968cf_sensor_update_picture(struct w9968cf_device*,
475 struct video_picture pict);
477 /* Other helper functions */
478 static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
479 enum w9968cf_model_id,
480 const unsigned short dev_nr);
481 static void w9968cf_adjust_configuration(struct w9968cf_device*);
482 static int w9968cf_turn_on_led(struct w9968cf_device*);
483 static int w9968cf_init_chip(struct w9968cf_device*);
484 static inline u16 w9968cf_valid_palette(u16 palette);
485 static inline u16 w9968cf_valid_depth(u16 palette);
486 static inline u8 w9968cf_need_decompression(u16 palette);
487 static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
488 static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
489 static int w9968cf_postprocess_frame(struct w9968cf_device*,
490 struct w9968cf_frame_t*);
491 static int w9968cf_adjust_window_size(struct w9968cf_device*, u16* w, u16* h);
492 static void w9968cf_init_framelist(struct w9968cf_device*);
493 static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
494 static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
495 static void w9968cf_release_resources(struct w9968cf_device*);
497 /* Intermodule communication */
498 static int w9968cf_vppmod_detect(struct w9968cf_device*);
499 static void w9968cf_vppmod_release(struct w9968cf_device*);
503 /****************************************************************************
505 ****************************************************************************/
507 /* Used to represent a list of values and their respective symbolic names */
508 struct w9968cf_symbolic_list {
513 /*--------------------------------------------------------------------------
514 Returns the name of the matching element in the symbolic_list array. The
515 end of the list must be marked with an element that has a NULL name.
516 --------------------------------------------------------------------------*/
517 static inline const char *
518 symbolic(struct w9968cf_symbolic_list list[], const int num)
522 for (i = 0; list[i].name != NULL; i++)
523 if (list[i].num == num)
524 return (list[i].name);
529 static struct w9968cf_symbolic_list camlist[] = {
530 { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
531 { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
533 /* Other cameras (having the same descriptors as Generic W996[87]CF) */
534 { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
535 { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
536 { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
537 { W9968CF_MOD_LL, "Lebon LDC-035A" },
538 { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
539 { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
540 { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
541 { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
542 { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
547 static struct w9968cf_symbolic_list senlist[] = {
548 { CC_OV76BE, "OV76BE" },
549 { CC_OV7610, "OV7610" },
550 { CC_OV7620, "OV7620" },
551 { CC_OV7620AE, "OV7620AE" },
552 { CC_OV6620, "OV6620" },
553 { CC_OV6630, "OV6630" },
554 { CC_OV6630AE, "OV6630AE" },
555 { CC_OV6630AF, "OV6630AF" },
559 /* Video4Linux1 palettes */
560 static struct w9968cf_symbolic_list v4l1_plist[] = {
561 { VIDEO_PALETTE_GREY, "GREY" },
562 { VIDEO_PALETTE_HI240, "HI240" },
563 { VIDEO_PALETTE_RGB565, "RGB565" },
564 { VIDEO_PALETTE_RGB24, "RGB24" },
565 { VIDEO_PALETTE_RGB32, "RGB32" },
566 { VIDEO_PALETTE_RGB555, "RGB555" },
567 { VIDEO_PALETTE_YUV422, "YUV422" },
568 { VIDEO_PALETTE_YUYV, "YUYV" },
569 { VIDEO_PALETTE_UYVY, "UYVY" },
570 { VIDEO_PALETTE_YUV420, "YUV420" },
571 { VIDEO_PALETTE_YUV411, "YUV411" },
572 { VIDEO_PALETTE_RAW, "RAW" },
573 { VIDEO_PALETTE_YUV422P, "YUV422P" },
574 { VIDEO_PALETTE_YUV411P, "YUV411P" },
575 { VIDEO_PALETTE_YUV420P, "YUV420P" },
576 { VIDEO_PALETTE_YUV410P, "YUV410P" },
580 /* Decoder error codes: */
581 static struct w9968cf_symbolic_list decoder_errlist[] = {
582 { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
583 { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
584 { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
585 { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
586 { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
587 { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
591 /* URB error codes: */
592 static struct w9968cf_symbolic_list urb_errlist[] = {
593 { -ENOMEM, "No memory for allocation of internal structures" },
594 { -ENOSPC, "The host controller's bandwidth is already consumed" },
595 { -ENOENT, "URB was canceled by unlink_urb" },
596 { -EXDEV, "ISO transfer only partially completed" },
597 { -EAGAIN, "Too match scheduled for the future" },
598 { -ENXIO, "URB already queued" },
599 { -EFBIG, "Too much ISO frames requested" },
600 { -ENOSR, "Buffer error (overrun)" },
601 { -EPIPE, "Specified endpoint is stalled (device not responding)"},
602 { -EOVERFLOW, "Babble (bad cable?)" },
603 { -EPROTO, "Bit-stuff error (bad cable?)" },
604 { -EILSEQ, "CRC/Timeout" },
605 { -ETIMEDOUT, "NAK (device does not respond)" },
611 /****************************************************************************
612 * Memory management functions *
613 ****************************************************************************/
615 /* Here we want the physical address of the memory.
616 This is used when initializing the contents of the area. */
617 static inline unsigned long kvirt_to_pa(unsigned long adr)
619 unsigned long kva, ret;
621 kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
622 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
628 static void* rvmalloc(unsigned long size)
633 size = PAGE_ALIGN(size);
634 mem = vmalloc_32(size);
638 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
639 adr = (unsigned long) mem;
641 SetPageReserved(vmalloc_to_page((void *)adr));
650 static void rvfree(void* mem, unsigned long size)
657 adr = (unsigned long) mem;
658 while ((long) size > 0) {
659 ClearPageReserved(vmalloc_to_page((void *)adr));
667 /*--------------------------------------------------------------------------
668 Deallocate previously allocated memory.
669 --------------------------------------------------------------------------*/
670 static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
674 /* Free the isochronous transfer buffers */
675 for (i = 0; i < W9968CF_URBS; i++) {
676 kfree(cam->transfer_buffer[i]);
677 cam->transfer_buffer[i] = NULL;
680 /* Free temporary frame buffer */
681 if (cam->frame_tmp.buffer) {
682 rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
683 cam->frame_tmp.buffer = NULL;
686 /* Free helper buffer */
687 if (cam->frame_vpp.buffer) {
688 rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
689 cam->frame_vpp.buffer = NULL;
692 /* Free video frame buffers */
693 if (cam->frame[0].buffer) {
694 rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
695 cam->frame[0].buffer = NULL;
700 DBG(5, "Memory successfully deallocated")
704 /*--------------------------------------------------------------------------
705 Allocate memory buffers for USB transfers and video frames.
706 This function is called by open() only.
707 Return 0 on success, a negative number otherwise.
708 --------------------------------------------------------------------------*/
709 static int w9968cf_allocate_memory(struct w9968cf_device* cam)
711 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
713 unsigned long hw_bufsize, vpp_bufsize;
716 /* NOTE: Deallocation is done elsewhere in case of error */
718 /* Calculate the max amount of raw data per frame from the device */
719 hw_bufsize = cam->maxwidth*cam->maxheight*2;
721 /* Calculate the max buf. size needed for post-processing routines */
722 bpp = (w9968cf_vpp) ? 4 : 2;
724 vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
725 cam->maxwidth*cam->maxheight*bpp);
727 vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
729 /* Allocate memory for the isochronous transfer buffers */
730 for (i = 0; i < W9968CF_URBS; i++) {
731 if (!(cam->transfer_buffer[i] =
732 kmalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
733 DBG(1, "Couldn't allocate memory for the isochronous "
734 "transfer buffers (%u bytes)",
735 p_size * W9968CF_ISO_PACKETS)
738 memset(cam->transfer_buffer[i], 0, W9968CF_ISO_PACKETS*p_size);
741 /* Allocate memory for the temporary frame buffer */
742 if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
743 DBG(1, "Couldn't allocate memory for the temporary "
744 "video frame buffer (%lu bytes)", hw_bufsize)
747 cam->frame_tmp.size = hw_bufsize;
748 cam->frame_tmp.number = -1;
750 /* Allocate memory for the helper buffer */
752 if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
753 DBG(1, "Couldn't allocate memory for the helper buffer"
754 " (%lu bytes)", vpp_bufsize)
757 cam->frame_vpp.size = vpp_bufsize;
759 cam->frame_vpp.buffer = NULL;
761 /* Allocate memory for video frame buffers */
762 cam->nbuffers = cam->max_buffers;
763 while (cam->nbuffers >= 2) {
764 if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
771 DBG(1, "Couldn't allocate memory for the video frame buffers")
776 if (cam->nbuffers != cam->max_buffers)
777 DBG(2, "Couldn't allocate memory for %u video frame buffers. "
778 "Only memory for %u buffers has been allocated",
779 cam->max_buffers, cam->nbuffers)
781 for (i = 0; i < cam->nbuffers; i++) {
782 cam->frame[i].buffer = buff + i*vpp_bufsize;
783 cam->frame[i].size = vpp_bufsize;
784 cam->frame[i].number = i;
786 if (i != cam->nbuffers-1)
787 cam->frame[i].next = &cam->frame[i+1];
789 cam->frame[i].next = &cam->frame[0];
790 cam->frame[i].status = F_UNUSED;
793 DBG(5, "Memory successfully allocated")
799 /****************************************************************************
800 * USB-specific functions *
801 ****************************************************************************/
803 /*--------------------------------------------------------------------------
804 This is an handler function which is called after the URBs are completed.
805 It collects multiple data packets coming from the camera by putting them
806 into frame buffers: one or more zero data length data packets are used to
807 mark the end of a video frame; the first non-zero data packet is the start
808 of the next video frame; if an error is encountered in a packet, the entire
809 video frame is discarded and grabbed again.
810 If there are no requested frames in the FIFO list, packets are collected into
812 --------------------------------------------------------------------------*/
813 static void w9968cf_urb_complete(struct urb *urb, struct pt_regs *regs)
815 struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
816 struct w9968cf_frame_t** f;
817 unsigned int len, status;
822 if ((!cam->streaming) || cam->disconnected) {
823 DBG(4, "Got interrupt, but not streaming")
827 /* "(*f)" will be used instead of "cam->frame_current" */
828 f = &cam->frame_current;
830 /* If a frame has been requested and we are grabbing into
831 the temporary frame, we'll switch to that requested frame */
832 if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
833 if (cam->frame_tmp.status == F_GRABBING) {
834 w9968cf_pop_frame(cam, &cam->frame_current);
835 (*f)->status = F_GRABBING;
836 (*f)->length = cam->frame_tmp.length;
837 memcpy((*f)->buffer, cam->frame_tmp.buffer,
839 DBG(6, "Switched from temp. frame to frame #%d",
844 for (i = 0; i < urb->number_of_packets; i++) {
845 len = urb->iso_frame_desc[i].actual_length;
846 status = urb->iso_frame_desc[i].status;
847 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
849 if (status && len != 0) {
850 DBG(4, "URB failed, error in data packet "
852 status, symbolic(urb_errlist, status))
853 (*f)->status = F_ERROR;
857 if (len) { /* start of frame */
859 if ((*f)->status == F_UNUSED) {
860 (*f)->status = F_GRABBING;
864 /* Buffer overflows shouldn't happen, however...*/
865 if ((*f)->length + len > (*f)->size) {
866 DBG(4, "Buffer overflow: bad data packets")
867 (*f)->status = F_ERROR;
870 if ((*f)->status == F_GRABBING) {
871 memcpy((*f)->buffer + (*f)->length, pos, len);
875 } else if ((*f)->status == F_GRABBING) { /* end of frame */
877 DBG(6, "Frame #%d successfully grabbed", (*f)->number)
879 if (cam->vpp_flag & VPP_DECOMPRESSION) {
880 err = w9968cf_vpp->check_headers((*f)->buffer,
883 DBG(4, "Skip corrupted frame: %s",
884 symbolic(decoder_errlist, err))
885 (*f)->status = F_UNUSED;
886 continue; /* grab this frame again */
890 (*f)->status = F_READY;
893 /* Take a pointer to the new frame from the FIFO list.
894 If the list is empty,we'll use the temporary frame*/
895 if (*cam->requested_frame)
896 w9968cf_pop_frame(cam, &cam->frame_current);
898 cam->frame_current = &cam->frame_tmp;
899 (*f)->status = F_UNUSED;
902 } else if ((*f)->status == F_ERROR)
903 (*f)->status = F_UNUSED; /* grab it again */
905 PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
906 (unsigned long)(*f)->length, i, len, (*f)->status)
910 /* Resubmit this URB */
911 urb->dev = cam->usbdev;
913 spin_lock(&cam->urb_lock);
915 if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
916 cam->misconfigured = 1;
917 DBG(1, "Couldn't resubmit the URB: error %d, %s",
918 err, symbolic(urb_errlist, err))
920 spin_unlock(&cam->urb_lock);
922 /* Wake up the user process */
923 wake_up_interruptible(&cam->wait_queue);
927 /*---------------------------------------------------------------------------
928 Setup the URB structures for the isochronous transfer.
929 Submit the URBs so that the data transfer begins.
930 Return 0 on success, a negative number otherwise.
931 ---------------------------------------------------------------------------*/
932 static int w9968cf_start_transfer(struct w9968cf_device* cam)
934 struct usb_device *udev = cam->usbdev;
936 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
943 for (i = 0; i < W9968CF_URBS; i++) {
944 urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
947 for (j = 0; j < i; j++)
948 usb_free_urb(cam->urb[j]);
949 DBG(1, "Couldn't allocate the URB structures")
954 urb->context = (void*)cam;
955 urb->pipe = usb_rcvisocpipe(udev, 1);
956 urb->transfer_flags = URB_ISO_ASAP;
957 urb->number_of_packets = W9968CF_ISO_PACKETS;
958 urb->complete = w9968cf_urb_complete;
959 urb->transfer_buffer = cam->transfer_buffer[i];
960 urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
962 for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
963 urb->iso_frame_desc[j].offset = p_size*j;
964 urb->iso_frame_desc[j].length = p_size;
968 /* Transfer size per frame, in WORD ! */
975 err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
976 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
979 err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
980 err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
982 if (cam->vpp_flag & VPP_DECOMPRESSION)
983 err += w9968cf_upload_quantizationtables(cam);
985 vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
986 err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
988 err += usb_set_interface(udev, 0, cam->altsetting);
989 err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
991 if (err || (vidcapt < 0)) {
992 for (i = 0; i < W9968CF_URBS; i++)
993 usb_free_urb(cam->urb[i]);
994 DBG(1, "Couldn't tell the camera to start the data transfer")
998 w9968cf_init_framelist(cam);
1000 /* Begin to grab into the temporary buffer */
1001 cam->frame_tmp.status = F_UNUSED;
1002 cam->frame_tmp.queued = 0;
1003 cam->frame_current = &cam->frame_tmp;
1005 if (!(cam->vpp_flag & VPP_DECOMPRESSION))
1006 DBG(5, "Isochronous transfer size: %lu bytes/frame",
1007 (unsigned long)t_size*2)
1009 DBG(5, "Starting the isochronous transfer...")
1013 /* Submit the URBs */
1014 for (i = 0; i < W9968CF_URBS; i++) {
1015 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
1018 for (j = i-1; j >= 0; j--) {
1019 usb_kill_urb(cam->urb[j]);
1020 usb_free_urb(cam->urb[j]);
1022 DBG(1, "Couldn't send a transfer request to the "
1023 "USB core (error #%d, %s)", err,
1024 symbolic(urb_errlist, err))
1033 /*--------------------------------------------------------------------------
1034 Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
1035 Return 0 on success, a negative number otherwise.
1036 --------------------------------------------------------------------------*/
1037 static int w9968cf_stop_transfer(struct w9968cf_device* cam)
1039 struct usb_device *udev = cam->usbdev;
1040 unsigned long lock_flags;
1044 if (!cam->streaming)
1047 /* This avoids race conditions with usb_submit_urb()
1048 in the URB completition handler */
1049 spin_lock_irqsave(&cam->urb_lock, lock_flags);
1051 spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
1053 for (i = W9968CF_URBS-1; i >= 0; i--)
1055 usb_kill_urb(cam->urb[i]);
1056 usb_free_urb(cam->urb[i]);
1060 if (cam->disconnected)
1063 err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
1064 err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
1065 err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
1066 err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
1069 DBG(2, "Failed to tell the camera to stop the isochronous "
1070 "transfer. However this is not a critical error.")
1075 DBG(5, "Isochronous transfer stopped")
1080 /*--------------------------------------------------------------------------
1081 Write a W9968CF register.
1082 Return 0 on success, -1 otherwise.
1083 --------------------------------------------------------------------------*/
1084 static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
1086 struct usb_device* udev = cam->usbdev;
1089 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1090 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1091 value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
1094 DBG(4, "Failed to write a register "
1095 "(value 0x%04X, index 0x%02X, error #%d, %s)",
1096 value, index, res, symbolic(urb_errlist, res))
1098 return (res >= 0) ? 0 : -1;
1102 /*--------------------------------------------------------------------------
1103 Read a W9968CF register.
1104 Return the register value on success, -1 otherwise.
1105 --------------------------------------------------------------------------*/
1106 static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
1108 struct usb_device* udev = cam->usbdev;
1109 u16* buff = cam->control_buffer;
1112 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
1113 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1114 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
1117 DBG(4, "Failed to read a register "
1118 "(index 0x%02X, error #%d, %s)",
1119 index, res, symbolic(urb_errlist, res))
1121 return (res >= 0) ? (int)(*buff) : -1;
1125 /*--------------------------------------------------------------------------
1126 Write 64-bit data to the fast serial bus registers.
1127 Return 0 on success, -1 otherwise.
1128 --------------------------------------------------------------------------*/
1129 static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
1131 struct usb_device* udev = cam->usbdev;
1137 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1138 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1139 value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
1142 DBG(4, "Failed to write the FSB registers "
1143 "(error #%d, %s)", res, symbolic(urb_errlist, res))
1145 return (res >= 0) ? 0 : -1;
1149 /*--------------------------------------------------------------------------
1150 Write data to the serial bus control register.
1151 Return 0 on success, a negative number otherwise.
1152 --------------------------------------------------------------------------*/
1153 static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
1157 err = w9968cf_write_reg(cam, value, 0x01);
1158 udelay(W9968CF_I2C_BUS_DELAY);
1164 /*--------------------------------------------------------------------------
1165 Read data from the serial bus control register.
1166 Return 0 on success, a negative number otherwise.
1167 --------------------------------------------------------------------------*/
1168 static int w9968cf_read_sb(struct w9968cf_device* cam)
1172 v = w9968cf_read_reg(cam, 0x01);
1173 udelay(W9968CF_I2C_BUS_DELAY);
1179 /*--------------------------------------------------------------------------
1180 Upload quantization tables for the JPEG compression.
1181 This function is called by w9968cf_start_transfer().
1182 Return 0 on success, a negative number otherwise.
1183 --------------------------------------------------------------------------*/
1184 static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
1189 err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
1191 for (i = 0, j = 0; i < 32; i++, j += 2) {
1192 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
1193 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
1194 err += w9968cf_write_reg(cam, a, 0x40+i);
1195 err += w9968cf_write_reg(cam, b, 0x60+i);
1197 err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
1204 /****************************************************************************
1205 * Low-level I2C I/O functions. *
1206 * The adapter supports the following I2C transfer functions: *
1207 * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
1208 * i2c_adap_read_byte_data() *
1209 * i2c_adap_read_byte() *
1210 ****************************************************************************/
1212 static int w9968cf_smbus_start(struct w9968cf_device* cam)
1216 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1217 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1223 static int w9968cf_smbus_stop(struct w9968cf_device* cam)
1227 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1228 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1234 static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
1239 for (bit = 0 ; bit < 8 ; bit++) {
1240 sda = (v & 0x80) ? 2 : 0;
1242 /* SDE=1, SDA=sda, SCL=0 */
1243 err += w9968cf_write_sb(cam, 0x10 | sda);
1244 /* SDE=1, SDA=sda, SCL=1 */
1245 err += w9968cf_write_sb(cam, 0x11 | sda);
1246 /* SDE=1, SDA=sda, SCL=0 */
1247 err += w9968cf_write_sb(cam, 0x10 | sda);
1254 static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
1260 for (bit = 0 ; bit < 8 ; bit++) {
1262 err += w9968cf_write_sb(cam, 0x0013);
1263 *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
1264 err += w9968cf_write_sb(cam, 0x0012);
1271 static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
1275 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1276 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1277 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1283 static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
1287 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1288 sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
1289 err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
1293 DBG(6, "Couldn't receive the ACK")
1301 /* This seems to refresh the communication through the serial bus */
1302 static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
1306 for (j = 1; j <= 10; j++) {
1307 err = w9968cf_write_reg(cam, 0x0020, 0x01);
1308 err += w9968cf_write_reg(cam, 0x0000, 0x01);
1317 /* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
1319 w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
1320 u16 address, u8 subaddress,u8 value)
1322 u16* data = cam->data_buffer;
1325 err += w9968cf_smbus_refresh_bus(cam);
1327 /* Enable SBUS outputs */
1328 err += w9968cf_write_sb(cam, 0x0020);
1330 data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
1331 data[0] |= (address & 0x40) ? 0x4000 : 0x0;
1332 data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
1333 data[1] |= (address & 0x20) ? 0x0150 : 0x0;
1334 data[1] |= (address & 0x10) ? 0x5400 : 0x0;
1335 data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
1336 data[2] |= (address & 0x04) ? 0x0540 : 0x0;
1337 data[2] |= (address & 0x02) ? 0x5000 : 0x0;
1338 data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
1339 data[3] |= (address & 0x01) ? 0x0054 : 0x0;
1341 err += w9968cf_write_fsb(cam, data);
1343 data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
1344 data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
1345 data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
1346 data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
1347 data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
1348 data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
1349 data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
1350 data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
1351 data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
1352 data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
1355 err += w9968cf_write_fsb(cam, data);
1357 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
1358 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
1359 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
1360 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
1361 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
1362 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
1363 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
1364 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
1365 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
1366 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
1369 err += w9968cf_write_fsb(cam, data);
1371 /* Disable SBUS outputs */
1372 err += w9968cf_write_sb(cam, 0x0000);
1375 DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
1376 "value 0x%02X", address, subaddress, value)
1378 DBG(5, "I2C write byte data failed, addr.0x%04X, "
1379 "subaddr.0x%02X, value 0x%02X",
1380 address, subaddress, value)
1386 /* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
1388 w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
1389 u16 address, u8 subaddress,
1394 /* Serial data enable */
1395 err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
1397 err += w9968cf_smbus_start(cam);
1398 err += w9968cf_smbus_write_byte(cam, address);
1399 err += w9968cf_smbus_read_ack(cam);
1400 err += w9968cf_smbus_write_byte(cam, subaddress);
1401 err += w9968cf_smbus_read_ack(cam);
1402 err += w9968cf_smbus_stop(cam);
1403 err += w9968cf_smbus_start(cam);
1404 err += w9968cf_smbus_write_byte(cam, address + 1);
1405 err += w9968cf_smbus_read_ack(cam);
1406 err += w9968cf_smbus_read_byte(cam, value);
1407 err += w9968cf_smbus_write_ack(cam);
1408 err += w9968cf_smbus_stop(cam);
1410 /* Serial data disable */
1411 err += w9968cf_write_sb(cam, 0x0000);
1414 DBG(5, "I2C read byte data done, addr.0x%04X, "
1415 "subaddr.0x%02X, value 0x%02X",
1416 address, subaddress, *value)
1418 DBG(5, "I2C read byte data failed, addr.0x%04X, "
1419 "subaddr.0x%02X, wrong value 0x%02X",
1420 address, subaddress, *value)
1426 /* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
1428 w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
1429 u16 address, u8* value)
1433 /* Serial data enable */
1434 err += w9968cf_write_sb(cam, 0x0013);
1436 err += w9968cf_smbus_start(cam);
1437 err += w9968cf_smbus_write_byte(cam, address + 1);
1438 err += w9968cf_smbus_read_ack(cam);
1439 err += w9968cf_smbus_read_byte(cam, value);
1440 err += w9968cf_smbus_write_ack(cam);
1441 err += w9968cf_smbus_stop(cam);
1443 /* Serial data disable */
1444 err += w9968cf_write_sb(cam, 0x0000);
1447 DBG(5, "I2C read byte done, addr.0x%04X, "
1448 "value 0x%02X", address, *value)
1450 DBG(5, "I2C read byte failed, addr.0x%04X, "
1451 "wrong value 0x%02X", address, *value)
1457 /* SMBus protocol: S Addr Wr [A] Value [A] P */
1459 w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
1460 u16 address, u8 value)
1462 DBG(4, "i2c_write_byte() is an unsupported transfer mode")
1468 /****************************************************************************
1469 * I2C interface to kernel *
1470 ****************************************************************************/
1473 w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1474 unsigned short flags, char read_write, u8 command,
1475 int size, union i2c_smbus_data *data)
1477 struct w9968cf_device* cam = i2c_get_adapdata(adapter);
1486 DBG(4, "Rejected slave ID 0x%04X", addr)
1490 if (size == I2C_SMBUS_BYTE) {
1491 /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
1494 if (read_write == I2C_SMBUS_WRITE)
1495 err = w9968cf_i2c_adap_write_byte(cam, addr, command);
1496 else if (read_write == I2C_SMBUS_READ)
1497 err = w9968cf_i2c_adap_read_byte(cam,addr,&data->byte);
1499 } else if (size == I2C_SMBUS_BYTE_DATA) {
1502 if (read_write == I2C_SMBUS_WRITE)
1503 err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
1504 command, data->byte);
1505 else if (read_write == I2C_SMBUS_READ) {
1506 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1507 err = w9968cf_i2c_adap_read_byte_data(cam,addr,
1508 command, &data->byte);
1510 if (w9968cf_smbus_refresh_bus(cam)) {
1522 DBG(4, "Unsupported I2C transfer mode (%d)", size)
1530 static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
1532 return I2C_FUNC_SMBUS_READ_BYTE |
1533 I2C_FUNC_SMBUS_READ_BYTE_DATA |
1534 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
1538 static int w9968cf_i2c_attach_inform(struct i2c_client* client)
1540 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
1541 const char* clientname = i2c_clientname(client);
1542 int id = client->driver->id, err = 0;
1544 if (id == I2C_DRIVERID_OVCAMCHIP) {
1545 cam->sensor_client = client;
1546 err = w9968cf_sensor_init(cam);
1548 cam->sensor_client = NULL;
1552 DBG(4, "Rejected client [%s] with driver [%s]",
1553 clientname, client->driver->name)
1557 DBG(5, "I2C attach client [%s] with driver [%s]",
1558 clientname, client->driver->name)
1564 static int w9968cf_i2c_detach_inform(struct i2c_client* client)
1566 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
1567 const char* clientname = i2c_clientname(client);
1569 if (cam->sensor_client == client)
1570 cam->sensor_client = NULL;
1572 DBG(5, "I2C detach client [%s]", clientname)
1579 w9968cf_i2c_control(struct i2c_adapter* adapter, unsigned int cmd,
1586 static int w9968cf_i2c_init(struct w9968cf_device* cam)
1590 static struct i2c_algorithm algo = {
1591 .name = "W996[87]CF algorithm",
1592 .id = I2C_ALGO_SMBUS,
1593 .smbus_xfer = w9968cf_i2c_smbus_xfer,
1594 .algo_control = w9968cf_i2c_control,
1595 .functionality = w9968cf_i2c_func,
1598 static struct i2c_adapter adap = {
1599 .id = I2C_ALGO_SMBUS | I2C_HW_SMBUS_W9968CF,
1600 .class = I2C_CLASS_CAM_DIGITAL,
1601 .owner = THIS_MODULE,
1602 .client_register = w9968cf_i2c_attach_inform,
1603 .client_unregister = w9968cf_i2c_detach_inform,
1607 memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
1608 strcpy(cam->i2c_adapter.name, "w9968cf");
1609 i2c_set_adapdata(&cam->i2c_adapter, cam);
1611 DBG(6, "Registering I2C adapter with kernel...")
1613 err = i2c_add_adapter(&cam->i2c_adapter);
1615 DBG(1, "Failed to register the I2C adapter")
1617 DBG(5, "I2C adapter registered")
1624 /****************************************************************************
1625 * Helper functions *
1626 ****************************************************************************/
1628 /*--------------------------------------------------------------------------
1629 Turn on the LED on some webcams. A beep should be heard too.
1630 Return 0 on success, a negative number otherwise.
1631 --------------------------------------------------------------------------*/
1632 static int w9968cf_turn_on_led(struct w9968cf_device* cam)
1636 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power-down */
1637 err += w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
1638 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
1639 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* serial bus, SDS high */
1640 err += w9968cf_write_reg(cam, 0x0000, 0x01); /* serial bus, SDS low */
1641 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* ..high 'beep-beep' */
1644 DBG(2, "Couldn't turn on the LED")
1646 DBG(5, "LED turned on")
1652 /*--------------------------------------------------------------------------
1653 Write some registers for the device initialization.
1654 This function is called once on open().
1655 Return 0 on success, a negative number otherwise.
1656 --------------------------------------------------------------------------*/
1657 static int w9968cf_init_chip(struct w9968cf_device* cam)
1659 unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
1661 u0 = y0 + hw_bufsize/2,
1662 v0 = u0 + hw_bufsize/4,
1663 y1 = v0 + hw_bufsize/4,
1664 u1 = y1 + hw_bufsize/2,
1665 v1 = u1 + hw_bufsize/4;
1668 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
1669 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
1671 err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
1672 err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
1674 err += w9968cf_write_reg(cam, y0 & 0xffff, 0x20); /* Y buf.0, low */
1675 err += w9968cf_write_reg(cam, y0 >> 16, 0x21); /* Y buf.0, high */
1676 err += w9968cf_write_reg(cam, u0 & 0xffff, 0x24); /* U buf.0, low */
1677 err += w9968cf_write_reg(cam, u0 >> 16, 0x25); /* U buf.0, high */
1678 err += w9968cf_write_reg(cam, v0 & 0xffff, 0x28); /* V buf.0, low */
1679 err += w9968cf_write_reg(cam, v0 >> 16, 0x29); /* V buf.0, high */
1681 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x22); /* Y buf.1, low */
1682 err += w9968cf_write_reg(cam, y1 >> 16, 0x23); /* Y buf.1, high */
1683 err += w9968cf_write_reg(cam, u1 & 0xffff, 0x26); /* U buf.1, low */
1684 err += w9968cf_write_reg(cam, u1 >> 16, 0x27); /* U buf.1, high */
1685 err += w9968cf_write_reg(cam, v1 & 0xffff, 0x2a); /* V buf.1, low */
1686 err += w9968cf_write_reg(cam, v1 >> 16, 0x2b); /* V buf.1, high */
1688 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x32); /* JPEG buf 0 low */
1689 err += w9968cf_write_reg(cam, y1 >> 16, 0x33); /* JPEG buf 0 high */
1691 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x34); /* JPEG buf 1 low */
1692 err += w9968cf_write_reg(cam, y1 >> 16, 0x35); /* JPEG bug 1 high */
1694 err += w9968cf_write_reg(cam, 0x0000, 0x36);/* JPEG restart interval */
1695 err += w9968cf_write_reg(cam, 0x0804, 0x37);/*JPEG VLE FIFO threshold*/
1696 err += w9968cf_write_reg(cam, 0x0000, 0x38);/* disable hw up-scaling */
1697 err += w9968cf_write_reg(cam, 0x0000, 0x3f); /* JPEG/MCTL test data */
1699 err += w9968cf_set_picture(cam, cam->picture); /* this before */
1700 err += w9968cf_set_window(cam, cam->window);
1703 DBG(1, "Chip initialization failed")
1705 DBG(5, "Chip successfully initialized")
1711 /*--------------------------------------------------------------------------
1712 Return non-zero if the palette is supported, 0 otherwise.
1713 --------------------------------------------------------------------------*/
1714 static inline u16 w9968cf_valid_palette(u16 palette)
1717 while (w9968cf_formatlist[i].palette != 0) {
1718 if (palette == w9968cf_formatlist[i].palette)
1726 /*--------------------------------------------------------------------------
1727 Return the depth corresponding to the given palette.
1728 Palette _must_ be supported !
1729 --------------------------------------------------------------------------*/
1730 static inline u16 w9968cf_valid_depth(u16 palette)
1733 while (w9968cf_formatlist[i].palette != palette)
1736 return w9968cf_formatlist[i].depth;
1740 /*--------------------------------------------------------------------------
1741 Return non-zero if the format requires decompression, 0 otherwise.
1742 --------------------------------------------------------------------------*/
1743 static inline u8 w9968cf_need_decompression(u16 palette)
1746 while (w9968cf_formatlist[i].palette != 0) {
1747 if (palette == w9968cf_formatlist[i].palette)
1748 return w9968cf_formatlist[i].compression;
1755 /*--------------------------------------------------------------------------
1756 Change the picture settings of the camera.
1757 Return 0 on success, a negative number otherwise.
1758 --------------------------------------------------------------------------*/
1760 w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
1762 u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
1765 /* Make sure we are using a valid depth */
1766 pict.depth = w9968cf_valid_depth(pict.palette);
1770 hw_depth = pict.depth; /* depth used by the winbond chip */
1771 hw_palette = pict.palette; /* palette used by the winbond chip */
1773 /* VS & HS polarities */
1774 reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
1778 case VIDEO_PALETTE_UYVY:
1780 cam->vpp_flag = VPP_NONE;
1782 case VIDEO_PALETTE_YUV422P:
1784 cam->vpp_flag = VPP_DECOMPRESSION;
1786 case VIDEO_PALETTE_YUV420:
1787 case VIDEO_PALETTE_YUV420P:
1789 cam->vpp_flag = VPP_DECOMPRESSION;
1791 case VIDEO_PALETTE_YUYV:
1792 case VIDEO_PALETTE_YUV422:
1794 cam->vpp_flag = VPP_SWAP_YUV_BYTES;
1795 hw_palette = VIDEO_PALETTE_UYVY;
1797 /* Original video is used instead of RGBX palettes.
1798 Software conversion later. */
1799 case VIDEO_PALETTE_GREY:
1800 case VIDEO_PALETTE_RGB555:
1801 case VIDEO_PALETTE_RGB565:
1802 case VIDEO_PALETTE_RGB24:
1803 case VIDEO_PALETTE_RGB32:
1804 reg_v |= 0x0000; /* UYVY 16 bit is used */
1806 hw_palette = VIDEO_PALETTE_UYVY;
1807 cam->vpp_flag = VPP_UYVY_TO_RGBX;
1811 /* NOTE: due to memory issues, it is better to disable the hardware
1812 double buffering during compression */
1813 if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
1819 if (cam->filter_type == 1)
1821 else if (cam->filter_type == 2)
1824 if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
1827 if ((err = w9968cf_sensor_update_picture(cam, pict)))
1830 /* If all went well, update the device data structure */
1831 memcpy(&cam->picture, &pict, sizeof(pict));
1832 cam->hw_depth = hw_depth;
1833 cam->hw_palette = hw_palette;
1835 /* Settings changed, so we clear the frame buffers */
1836 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1838 DBG(4, "Palette is %s, depth is %u bpp",
1839 symbolic(v4l1_plist, pict.palette), pict.depth)
1844 DBG(1, "Failed to change picture settings")
1849 /*--------------------------------------------------------------------------
1850 Change the capture area size of the camera.
1851 This function _must_ be called _after_ w9968cf_set_picture().
1852 Return 0 on success, a negative number otherwise.
1853 --------------------------------------------------------------------------*/
1855 w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
1857 u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
1858 unsigned long fw, fh;
1859 struct ovcamchip_window s_win;
1862 /* Work around to avoid FP arithmetics */
1863 #define __SC(x) ((x) << 10)
1864 #define __UNSC(x) ((x) >> 10)
1866 /* Make sure we are using a supported resolution */
1867 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
1868 (u16*)&win.height)))
1871 /* Scaling factors */
1872 fw = __SC(win.width) / cam->maxwidth;
1873 fh = __SC(win.height) / cam->maxheight;
1875 /* Set up the width and height values used by the chip */
1876 if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
1877 cam->vpp_flag |= VPP_UPSCALE;
1878 /* Calculate largest w,h mantaining the same w/h ratio */
1879 w = (fw >= fh) ? cam->maxwidth : __SC(win.width)/fh;
1880 h = (fw >= fh) ? __SC(win.height)/fw : cam->maxheight;
1881 if (w < cam->minwidth) /* just in case */
1883 if (h < cam->minheight) /* just in case */
1886 cam->vpp_flag &= ~VPP_UPSCALE;
1891 /* x,y offsets of the cropped area */
1892 scx = cam->start_cropx;
1893 scy = cam->start_cropy;
1895 /* Calculate cropped area manteining the right w/h ratio */
1896 if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
1897 cw = (fw >= fh) ? cam->maxwidth : __SC(win.width)/fh;
1898 ch = (fw >= fh) ? __SC(win.height)/fw : cam->maxheight;
1904 /* Setup the window of the sensor */
1905 s_win.format = VIDEO_PALETTE_UYVY;
1906 s_win.width = cam->maxwidth;
1907 s_win.height = cam->maxheight;
1908 s_win.quarter = 0; /* full progressive video */
1911 s_win.x = (s_win.width - cw) / 2;
1912 s_win.y = (s_win.height - ch) / 2;
1915 if (cam->clockdiv >= 0)
1916 s_win.clockdiv = cam->clockdiv; /* manual override */
1918 switch (cam->sensor) {
1931 s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
1934 /* We have to scale win.x and win.y offsets */
1935 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1936 || (cam->vpp_flag & VPP_UPSCALE) ) {
1937 ax = __SC(win.x)/fw;
1938 ay = __SC(win.y)/fh;
1944 if ((ax + cw) > cam->maxwidth)
1945 ax = cam->maxwidth - cw;
1947 if ((ay + ch) > cam->maxheight)
1948 ay = cam->maxheight - ch;
1950 /* Adjust win.x, win.y */
1951 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1952 || (cam->vpp_flag & VPP_UPSCALE) ) {
1953 win.x = __UNSC(ax*fw);
1954 win.y = __UNSC(ay*fh);
1960 /* Offsets used by the chip */
1965 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
1968 err += w9968cf_write_reg(cam, scx + x, 0x10);
1969 err += w9968cf_write_reg(cam, scy + y, 0x11);
1970 err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
1971 err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
1972 err += w9968cf_write_reg(cam, w, 0x14);
1973 err += w9968cf_write_reg(cam, h, 0x15);
1975 /* JPEG width & height */
1976 err += w9968cf_write_reg(cam, w, 0x30);
1977 err += w9968cf_write_reg(cam, h, 0x31);
1979 /* Y & UV frame buffer strides (in WORD) */
1980 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1981 err += w9968cf_write_reg(cam, w/2, 0x2c);
1982 err += w9968cf_write_reg(cam, w/4, 0x2d);
1984 err += w9968cf_write_reg(cam, w, 0x2c);
1989 /* If all went well, update the device data structure */
1990 memcpy(&cam->window, &win, sizeof(win));
1994 /* Settings changed, so we clear the frame buffers */
1995 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1997 DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
1998 win.width, win.height, win.x, win.y)
2000 PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
2001 "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
2002 x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
2003 win.width, win.height)
2008 DBG(1, "Failed to change the capture area size")
2013 /*--------------------------------------------------------------------------
2014 Adjust the asked values for window width and height.
2015 Return 0 on success, -1 otherwise.
2016 --------------------------------------------------------------------------*/
2018 w9968cf_adjust_window_size(struct w9968cf_device* cam, u16* width, u16* height)
2022 if ((*width < cam->minwidth) || (*height < cam->minheight))
2025 maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
2026 w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2028 maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
2029 w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2037 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2038 *width &= ~15L; /* multiple of 16 */
2042 PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
2048 /*--------------------------------------------------------------------------
2049 Initialize the FIFO list of requested frames.
2050 --------------------------------------------------------------------------*/
2051 static void w9968cf_init_framelist(struct w9968cf_device* cam)
2055 for (i = 0; i < cam->nbuffers; i++) {
2056 cam->requested_frame[i] = NULL;
2057 cam->frame[i].queued = 0;
2058 cam->frame[i].status = F_UNUSED;
2063 /*--------------------------------------------------------------------------
2064 Add a frame in the FIFO list of requested frames.
2065 This function is called in process context.
2066 --------------------------------------------------------------------------*/
2067 static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
2070 unsigned long lock_flags;
2072 spin_lock_irqsave(&cam->flist_lock, lock_flags);
2074 for (f=0; cam->requested_frame[f] != NULL; f++);
2075 cam->requested_frame[f] = &cam->frame[f_num];
2076 cam->frame[f_num].queued = 1;
2077 cam->frame[f_num].status = F_UNUSED; /* clear the status */
2079 spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
2081 DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
2085 /*--------------------------------------------------------------------------
2086 Read, store and remove the first pointer in the FIFO list of requested
2087 frames. This function is called in interrupt context.
2088 --------------------------------------------------------------------------*/
2090 w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
2094 spin_lock(&cam->flist_lock);
2096 *framep = cam->requested_frame[0];
2098 /* Shift the list of pointers */
2099 for (i = 0; i < cam->nbuffers-1; i++)
2100 cam->requested_frame[i] = cam->requested_frame[i+1];
2101 cam->requested_frame[i] = NULL;
2103 spin_unlock(&cam->flist_lock);
2105 DBG(6,"Popped frame #%d from the list", (*framep)->number)
2109 /*--------------------------------------------------------------------------
2110 High-level video post-processing routine on grabbed frames.
2111 Return 0 on success, a negative number otherwise.
2112 --------------------------------------------------------------------------*/
2114 w9968cf_postprocess_frame(struct w9968cf_device* cam,
2115 struct w9968cf_frame_t* fr)
2117 void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
2118 u16 w = cam->window.width,
2119 h = cam->window.height,
2120 d = cam->picture.depth,
2121 fmt = cam->picture.palette,
2122 rgb = cam->force_rgb,
2123 hw_w = cam->hw_width,
2124 hw_h = cam->hw_height,
2125 hw_d = cam->hw_depth;
2128 #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
2130 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2131 memcpy(pOut, pIn, fr->length);
2133 err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
2134 PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
2135 fr->length = (hw_w*hw_h*hw_d)/8;
2138 DBG(4, "An error occurred while decoding the frame: "
2139 "%s", symbolic(decoder_errlist, err))
2142 DBG(6, "Frame decoded")
2145 if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
2146 w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
2147 DBG(6, "Original UYVY component ordering changed")
2150 if (cam->vpp_flag & VPP_UPSCALE) {
2151 w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
2152 fr->length = (w*h*hw_d)/8;
2154 DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
2155 hw_w, hw_h, hw_d, w, h)
2158 if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
2159 w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
2160 fr->length = (w*h*d)/8;
2162 DBG(6, "UYVY-16bit to %s conversion done",
2163 symbolic(v4l1_plist, fmt))
2166 if (pOut == fr->buffer)
2167 memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
2174 /****************************************************************************
2175 * Image sensor control routines *
2176 ****************************************************************************/
2179 w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
2181 struct ovcamchip_control ctl;
2187 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
2194 w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
2196 struct ovcamchip_control ctl;
2201 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
2210 w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
2212 struct i2c_client* c = cam->sensor_client;
2215 if (!c || !c->driver || !c->driver->command)
2218 rc = c->driver->command(c, cmd, arg);
2219 /* The I2C driver returns -EPERM on non-supported controls */
2220 return (rc < 0 && rc != -EPERM) ? rc : 0;
2224 /*--------------------------------------------------------------------------
2225 Update some settings of the image sensor.
2226 Returns: 0 on success, a negative number otherwise.
2227 --------------------------------------------------------------------------*/
2228 static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
2232 /* Auto brightness */
2233 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
2239 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
2244 /* Banding filter */
2245 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
2250 /* Light frequency */
2251 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
2257 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
2263 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
2272 /*--------------------------------------------------------------------------
2273 Get some current picture settings from the image sensor and update the
2274 internal 'picture' structure of the camera.
2275 Returns: 0 on success, a negative number otherwise.
2276 --------------------------------------------------------------------------*/
2277 static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
2281 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
2284 cam->picture.contrast = v;
2286 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
2289 cam->picture.brightness = v;
2291 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
2294 cam->picture.colour = v;
2296 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
2299 cam->picture.hue = v;
2301 DBG(5, "Got picture settings from the image sensor")
2303 PDBGG("Brightness, contrast, hue, colour, whiteness are "
2304 "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
2305 cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
2311 /*--------------------------------------------------------------------------
2312 Update picture settings of the image sensor.
2313 Returns: 0 on success, a negative number otherwise.
2314 --------------------------------------------------------------------------*/
2316 w9968cf_sensor_update_picture(struct w9968cf_device* cam,
2317 struct video_picture pict)
2321 if ((!cam->sensor_initialized)
2322 || pict.contrast != cam->picture.contrast) {
2323 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
2327 DBG(4, "Contrast changed from %u to %u",
2328 cam->picture.contrast, pict.contrast)
2329 cam->picture.contrast = pict.contrast;
2332 if (((!cam->sensor_initialized) ||
2333 pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
2334 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
2338 DBG(4, "Brightness changed from %u to %u",
2339 cam->picture.brightness, pict.brightness)
2340 cam->picture.brightness = pict.brightness;
2343 if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
2344 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
2348 DBG(4, "Colour changed from %u to %u",
2349 cam->picture.colour, pict.colour)
2350 cam->picture.colour = pict.colour;
2353 if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
2354 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
2358 DBG(4, "Hue changed from %u to %u",
2359 cam->picture.hue, pict.hue)
2360 cam->picture.hue = pict.hue;
2366 DBG(4, "Failed to change sensor picture setting")
2372 /****************************************************************************
2373 * Camera configuration *
2374 ****************************************************************************/
2376 /*--------------------------------------------------------------------------
2377 This function is called when a supported image sensor is detected.
2378 Return 0 if the initialization succeeds, a negative number otherwise.
2379 --------------------------------------------------------------------------*/
2380 static int w9968cf_sensor_init(struct w9968cf_device* cam)
2384 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
2388 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
2392 /* NOTE: Make sure width and height are a multiple of 16 */
2393 switch (cam->sensor_client->addr) {
2395 cam->maxwidth = 352;
2396 cam->maxheight = 288;
2398 cam->minheight = 48;
2401 cam->maxwidth = 640;
2402 cam->maxheight = 480;
2404 cam->minheight = 48;
2407 DBG(1, "Not supported image sensor detected for %s",
2408 symbolic(camlist, cam->id))
2412 /* These values depend on the ones in the ovxxx0.c sources */
2413 switch (cam->sensor) {
2415 cam->start_cropx = 287;
2416 cam->start_cropy = 35;
2417 /* Seems to work around a bug in the image sensor */
2418 cam->vs_polarity = 1;
2419 cam->hs_polarity = 1;
2422 cam->start_cropx = 320;
2423 cam->start_cropy = 35;
2424 cam->vs_polarity = 1;
2425 cam->hs_polarity = 0;
2428 if ((err = w9968cf_sensor_update_settings(cam)))
2431 if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
2434 cam->sensor_initialized = 1;
2436 DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
2440 cam->sensor_initialized = 0;
2441 cam->sensor = CC_UNKNOWN;
2442 DBG(1, "Image sensor initialization failed for %s (/dev/video%d). "
2443 "Try to detach and attach this device again",
2444 symbolic(camlist, cam->id), cam->v4ldev->minor)
2449 /*--------------------------------------------------------------------------
2450 Fill some basic fields in the main device data structure.
2451 This function is called once on w9968cf_usb_probe() for each recognized
2453 --------------------------------------------------------------------------*/
2455 w9968cf_configure_camera(struct w9968cf_device* cam,
2456 struct usb_device* udev,
2457 enum w9968cf_model_id mod_id,
2458 const unsigned short dev_nr)
2460 init_MUTEX(&cam->fileop_sem);
2461 init_waitqueue_head(&cam->open);
2462 spin_lock_init(&cam->urb_lock);
2463 spin_lock_init(&cam->flist_lock);
2466 cam->disconnected = 0;
2468 cam->sensor = CC_UNKNOWN;
2469 cam->sensor_initialized = 0;
2471 /* Calculate the alternate setting number (from 1 to 16)
2472 according to the 'packet_size' module parameter */
2473 if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
2474 packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
2475 for (cam->altsetting = 1;
2476 packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
2479 cam->max_buffers = (max_buffers[dev_nr] < 2 ||
2480 max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
2481 ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
2483 cam->double_buffer = (double_buffer[dev_nr] == 0 ||
2484 double_buffer[dev_nr] == 1)
2485 ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
2487 cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
2488 ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
2490 cam->filter_type = (filter_type[dev_nr] == 0 ||
2491 filter_type[dev_nr] == 1 ||
2492 filter_type[dev_nr] == 2)
2493 ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
2497 cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
2498 ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
2500 cam->decompression = (decompression[dev_nr] == 0 ||
2501 decompression[dev_nr] == 1 ||
2502 decompression[dev_nr] == 2)
2503 ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
2505 cam->upscaling = (upscaling[dev_nr] == 0 ||
2506 upscaling[dev_nr] == 1)
2507 ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
2509 cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
2510 ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
2512 cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
2513 ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
2515 cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
2516 ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
2518 cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
2519 bandingfilter[dev_nr] == 1)
2520 ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
2522 cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
2523 ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
2525 cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
2526 ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
2528 cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
2529 ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
2531 cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
2532 ? monochrome[dev_nr] : W9968CF_MONOCHROME;
2534 cam->picture.brightness = (u16)brightness[dev_nr];
2535 cam->picture.hue = (u16)hue[dev_nr];
2536 cam->picture.colour = (u16)colour[dev_nr];
2537 cam->picture.contrast = (u16)contrast[dev_nr];
2538 cam->picture.whiteness = (u16)whiteness[dev_nr];
2539 if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
2540 cam->picture.palette = (u16)force_palette[dev_nr];
2541 cam->force_palette = 1;
2543 cam->force_palette = 0;
2544 if (cam->decompression == 0)
2545 cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
2546 else if (cam->decompression == 1)
2547 cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
2549 cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
2551 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2553 cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
2554 ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
2558 cam->window.width = W9968CF_WIDTH;
2559 cam->window.height = W9968CF_HEIGHT;
2560 cam->window.chromakey = 0;
2561 cam->window.clipcount = 0;
2562 cam->window.flags = 0;
2564 DBG(3, "%s configured with settings #%u:",
2565 symbolic(camlist, cam->id), dev_nr)
2567 DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
2568 wMaxPacketSize[cam->altsetting-1])
2570 DBG(3, "- Number of requested video frame buffers: %u",
2573 if (cam->double_buffer)
2574 DBG(3, "- Hardware double buffering enabled")
2576 DBG(3, "- Hardware double buffering disabled")
2578 if (cam->filter_type == 0)
2579 DBG(3, "- Video filtering disabled")
2580 else if (cam->filter_type == 1)
2581 DBG(3, "- Video filtering enabled: type 1-2-1")
2582 else if (cam->filter_type == 2)
2583 DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
2586 DBG(3, "- Video data clamping (CCIR-601 format) enabled")
2588 DBG(3, "- Video data clamping (CCIR-601 format) disabled")
2591 DBG(3, "- Large view enabled")
2593 DBG(3, "- Large view disabled")
2595 if ((cam->decompression) == 0 && (!cam->force_palette))
2596 DBG(3, "- Decompression disabled")
2597 else if ((cam->decompression) == 1 && (!cam->force_palette))
2598 DBG(3, "- Decompression forced")
2599 else if ((cam->decompression) == 2 && (!cam->force_palette))
2600 DBG(3, "- Decompression allowed")
2603 DBG(3, "- Software image scaling enabled")
2605 DBG(3, "- Software image scaling disabled")
2607 if (cam->force_palette)
2608 DBG(3, "- Image palette forced to %s",
2609 symbolic(v4l1_plist, cam->picture.palette))
2612 DBG(3, "- RGB component ordering will be used instead of BGR")
2615 DBG(3, "- Auto brightness enabled")
2617 DBG(3, "- Auto brightness disabled")
2620 DBG(3, "- Auto exposure enabled")
2622 DBG(3, "- Auto exposure disabled")
2625 DBG(3, "- Backlight exposure algorithm enabled")
2627 DBG(3, "- Backlight exposure algorithm disabled")
2630 DBG(3, "- Mirror enabled")
2632 DBG(3, "- Mirror disabled")
2635 DBG(3, "- Banding filter enabled")
2637 DBG(3, "- Banding filter disabled")
2639 DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
2641 if (cam->clockdiv == -1)
2642 DBG(3, "- Automatic clock divisor enabled")
2644 DBG(3, "- Clock divisor: %d", cam->clockdiv)
2646 if (cam->monochrome)
2647 DBG(3, "- Image sensor used as monochrome")
2649 DBG(3, "- Image sensor not used as monochrome")
2653 /*--------------------------------------------------------------------------
2654 If the video post-processing module is not loaded, some parameters
2656 --------------------------------------------------------------------------*/
2657 static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2660 if (cam->decompression == 1) {
2661 cam->decompression = 2;
2662 DBG(2, "Video post-processing module not found: "
2663 "'decompression' parameter forced to 2")
2665 if (cam->upscaling) {
2667 DBG(2, "Video post-processing module not found: "
2668 "'upscaling' parameter forced to 0")
2670 if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
2671 cam->force_palette = 0;
2672 DBG(2, "Video post-processing module not found: "
2673 "'force_palette' parameter forced to 0")
2675 cam->picture.palette = VIDEO_PALETTE_UYVY;
2676 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2681 /*--------------------------------------------------------------------------
2682 Release the resources used by the driver.
2683 This function is called on disconnect
2684 (or on close if deallocation has been deferred)
2685 --------------------------------------------------------------------------*/
2686 static void w9968cf_release_resources(struct w9968cf_device* cam)
2688 down(&w9968cf_devlist_sem);
2690 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->minor)
2692 video_unregister_device(cam->v4ldev);
2693 list_del(&cam->v4llist);
2694 i2c_del_adapter(&cam->i2c_adapter);
2695 w9968cf_deallocate_memory(cam);
2696 kfree(cam->control_buffer);
2697 kfree(cam->data_buffer);
2699 up(&w9968cf_devlist_sem);
2704 /****************************************************************************
2705 * Video4Linux interface *
2706 ****************************************************************************/
2708 static int w9968cf_open(struct inode* inode, struct file* filp)
2710 struct w9968cf_device* cam;
2713 /* This the only safe way to prevent race conditions with disconnect */
2714 if (!down_read_trylock(&w9968cf_disconnect))
2715 return -ERESTARTSYS;
2717 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2719 down(&cam->dev_sem);
2721 if (cam->sensor == CC_UNKNOWN) {
2722 DBG(2, "No supported image sensor has been detected by the "
2723 "'ovcamchip' module for the %s (/dev/video%d). Make "
2724 "sure it is loaded *before* (re)connecting the camera.",
2725 symbolic(camlist, cam->id), cam->v4ldev->minor)
2727 up_read(&w9968cf_disconnect);
2732 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
2733 symbolic(camlist, cam->id),cam->v4ldev->minor,cam->command)
2734 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
2736 up_read(&w9968cf_disconnect);
2737 return -EWOULDBLOCK;
2740 err = wait_event_interruptible_exclusive(cam->open,
2741 cam->disconnected ||
2744 up_read(&w9968cf_disconnect);
2747 if (cam->disconnected) {
2748 up_read(&w9968cf_disconnect);
2751 down(&cam->dev_sem);
2754 DBG(5, "Opening '%s', /dev/video%d ...",
2755 symbolic(camlist, cam->id), cam->v4ldev->minor)
2758 cam->misconfigured = 0;
2761 if ((err = w9968cf_vppmod_detect(cam)))
2764 if ((err = w9968cf_allocate_memory(cam)))
2765 goto deallocate_memory;
2767 if ((err = w9968cf_init_chip(cam)))
2768 goto deallocate_memory;
2770 if ((err = w9968cf_start_transfer(cam)))
2771 goto deallocate_memory;
2773 filp->private_data = cam;
2776 strcpy(cam->command, current->comm);
2778 init_waitqueue_head(&cam->wait_queue);
2780 DBG(5, "Video device is open")
2783 up_read(&w9968cf_disconnect);
2788 w9968cf_deallocate_memory(cam);
2790 DBG(2, "Failed to open the video device")
2792 up_read(&w9968cf_disconnect);
2797 static int w9968cf_release(struct inode* inode, struct file* filp)
2799 struct w9968cf_device* cam;
2801 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2803 down(&cam->dev_sem); /* prevent disconnect() to be called */
2805 w9968cf_stop_transfer(cam);
2807 w9968cf_vppmod_release(cam);
2809 if (cam->disconnected) {
2810 w9968cf_release_resources(cam);
2817 w9968cf_deallocate_memory(cam);
2818 wake_up_interruptible_nr(&cam->open, 1);
2820 DBG(5, "Video device closed")
2827 w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2829 struct w9968cf_device* cam;
2830 struct w9968cf_frame_t* fr;
2833 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2835 if (filp->f_flags & O_NONBLOCK)
2836 return -EWOULDBLOCK;
2838 if (down_interruptible(&cam->fileop_sem))
2839 return -ERESTARTSYS;
2841 if (cam->disconnected) {
2842 DBG(2, "Device not present")
2843 up(&cam->fileop_sem);
2847 if (cam->misconfigured) {
2848 DBG(2, "The camera is misconfigured. Close and open it again.")
2849 up(&cam->fileop_sem);
2853 if (!cam->frame[0].queued)
2854 w9968cf_push_frame(cam, 0);
2856 if (!cam->frame[1].queued)
2857 w9968cf_push_frame(cam, 1);
2859 err = wait_event_interruptible(cam->wait_queue,
2860 cam->frame[0].status == F_READY ||
2861 cam->frame[1].status == F_READY ||
2864 up(&cam->fileop_sem);
2867 if (cam->disconnected) {
2868 up(&cam->fileop_sem);
2872 fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
2875 w9968cf_postprocess_frame(cam, fr);
2877 if (count > fr->length)
2880 if (copy_to_user(buf, fr->buffer, count)) {
2881 fr->status = F_UNUSED;
2882 up(&cam->fileop_sem);
2887 fr->status = F_UNUSED;
2889 DBG(5, "%zu bytes read", count)
2891 up(&cam->fileop_sem);
2896 static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
2898 struct w9968cf_device* cam = (struct w9968cf_device*)
2899 video_get_drvdata(video_devdata(filp));
2900 unsigned long vsize = vma->vm_end - vma->vm_start,
2901 psize = cam->nbuffers * cam->frame[0].size,
2902 start = vma->vm_start,
2903 pos = (unsigned long)cam->frame[0].buffer,
2906 if (cam->disconnected) {
2907 DBG(2, "Device not present")
2911 if (cam->misconfigured) {
2912 DBG(2, "The camera is misconfigured. Close and open it again")
2916 PDBGG("mmapping %lu bytes...", vsize)
2918 if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
2922 page = kvirt_to_pa(pos) + vma->vm_pgoff;
2923 if (remap_page_range(vma, start, page, PAGE_SIZE,
2931 DBG(5, "mmap method successfully called")
2937 w9968cf_ioctl(struct inode* inode, struct file* filp,
2938 unsigned int cmd, unsigned long arg)
2940 struct w9968cf_device* cam;
2943 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2945 if (down_interruptible(&cam->fileop_sem))
2946 return -ERESTARTSYS;
2948 if (cam->disconnected) {
2949 DBG(2, "Device not present")
2950 up(&cam->fileop_sem);
2954 if (cam->misconfigured) {
2955 DBG(2, "The camera is misconfigured. Close and open it again.")
2956 up(&cam->fileop_sem);
2960 err = w9968cf_v4l_ioctl(inode, filp, cmd, (void __user *)arg);
2962 up(&cam->fileop_sem);
2967 static int w9968cf_v4l_ioctl(struct inode* inode, struct file* filp,
2968 unsigned int cmd, void __user * arg)
2970 struct w9968cf_device* cam;
2971 const char* v4l1_ioctls[] = {
2972 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
2973 "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
2974 "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
2975 "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
2976 "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
2977 "GVBIFMT", "SVBIFMT"
2980 #define V4L1_IOCTL(cmd) \
2981 ((_IOC_NR((cmd)) < sizeof(v4l1_ioctls)/sizeof(char*)) ? \
2982 v4l1_ioctls[_IOC_NR((cmd))] : "?")
2984 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2988 case VIDIOCGCAP: /* get video capability */
2990 struct video_capability cap = {
2991 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
2994 .minwidth = cam->minwidth,
2995 .minheight = cam->minheight,
2997 sprintf(cap.name, "W996[87]CF USB Camera #%d",
2998 cam->v4ldev->minor);
2999 cap.maxwidth = (cam->upscaling && w9968cf_vpp)
3000 ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
3002 cap.maxheight = (cam->upscaling && w9968cf_vpp)
3003 ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
3006 if (copy_to_user(arg, &cap, sizeof(cap)))
3009 DBG(5, "VIDIOCGCAP successfully called")
3013 case VIDIOCGCHAN: /* get video channel informations */
3015 struct video_channel chan;
3016 if (copy_from_user(&chan, arg, sizeof(chan)))
3019 if (chan.channel != 0)
3022 strcpy(chan.name, "Camera");
3025 chan.type = VIDEO_TYPE_CAMERA;
3026 chan.norm = VIDEO_MODE_AUTO;
3028 if (copy_to_user(arg, &chan, sizeof(chan)))
3031 DBG(5, "VIDIOCGCHAN successfully called")
3035 case VIDIOCSCHAN: /* set active channel */
3037 struct video_channel chan;
3039 if (copy_from_user(&chan, arg, sizeof(chan)))
3042 if (chan.channel != 0)
3045 DBG(5, "VIDIOCSCHAN successfully called")
3049 case VIDIOCGPICT: /* get image properties of the picture */
3051 if (w9968cf_sensor_get_picture(cam))
3054 if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
3057 DBG(5, "VIDIOCGPICT successfully called")
3061 case VIDIOCSPICT: /* change picture settings */
3063 struct video_picture pict;
3066 if (copy_from_user(&pict, arg, sizeof(pict)))
3069 if ( (cam->force_palette || !w9968cf_vpp)
3070 && pict.palette != cam->picture.palette ) {
3071 DBG(4, "Palette %s rejected: only %s is allowed",
3072 symbolic(v4l1_plist, pict.palette),
3073 symbolic(v4l1_plist, cam->picture.palette))
3077 if (!w9968cf_valid_palette(pict.palette)) {
3078 DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
3079 symbolic(v4l1_plist, pict.palette))
3083 if (!cam->force_palette) {
3084 if (cam->decompression == 0) {
3085 if (w9968cf_need_decompression(pict.palette)) {
3086 DBG(4, "Decompression disabled: palette %s is not "
3087 "allowed. VIDIOCSPICT failed",
3088 symbolic(v4l1_plist, pict.palette))
3091 } else if (cam->decompression == 1) {
3092 if (!w9968cf_need_decompression(pict.palette)) {
3093 DBG(4, "Decompression forced: palette %s is not "
3094 "allowed. VIDIOCSPICT failed",
3095 symbolic(v4l1_plist, pict.palette))
3101 if (pict.depth != w9968cf_valid_depth(pict.palette)) {
3102 DBG(4, "Requested depth %u bpp is not valid for %s "
3103 "palette: ignored and changed to %u bpp",
3104 pict.depth, symbolic(v4l1_plist, pict.palette),
3105 w9968cf_valid_depth(pict.palette))
3106 pict.depth = w9968cf_valid_depth(pict.palette);
3109 if (pict.palette != cam->picture.palette) {
3110 if(*cam->requested_frame
3111 || cam->frame_current->queued) {
3112 err = wait_event_interruptible
3114 cam->disconnected ||
3115 (!*cam->requested_frame &&
3116 !cam->frame_current->queued) );
3119 if (cam->disconnected)
3123 if (w9968cf_stop_transfer(cam))
3126 if (w9968cf_set_picture(cam, pict))
3129 if (w9968cf_start_transfer(cam))
3132 } else if (w9968cf_sensor_update_picture(cam, pict))
3136 DBG(5, "VIDIOCSPICT successfully called")
3140 case VIDIOCSWIN: /* set capture area */
3142 struct video_window win;
3145 if (copy_from_user(&win, arg, sizeof(win)))
3148 DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
3149 "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
3150 win.x, win.y, win.width, win.height)
3152 if (win.clipcount != 0 || win.flags != 0)
3155 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
3156 (u16*)&win.height))) {
3157 DBG(4, "Resolution not supported (%ux%u). "
3158 "VIDIOCSWIN failed", win.width, win.height)
3162 if (win.x != cam->window.x ||
3163 win.y != cam->window.y ||
3164 win.width != cam->window.width ||
3165 win.height != cam->window.height) {
3166 if(*cam->requested_frame
3167 || cam->frame_current->queued) {
3168 err = wait_event_interruptible
3170 cam->disconnected ||
3171 (!*cam->requested_frame &&
3172 !cam->frame_current->queued) );
3175 if (cam->disconnected)
3179 if (w9968cf_stop_transfer(cam))
3182 /* This _must_ be called before set_window() */
3183 if (w9968cf_set_picture(cam, cam->picture))
3186 if (w9968cf_set_window(cam, win))
3189 if (w9968cf_start_transfer(cam))
3193 DBG(5, "VIDIOCSWIN successfully called. ")
3197 case VIDIOCGWIN: /* get current window properties */
3199 if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
3202 DBG(5, "VIDIOCGWIN successfully called")
3206 case VIDIOCGMBUF: /* request for memory (mapped) buffer */
3208 struct video_mbuf mbuf;
3211 mbuf.size = cam->nbuffers * cam->frame[0].size;
3212 mbuf.frames = cam->nbuffers;
3213 for (i = 0; i < cam->nbuffers; i++)
3214 mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
3215 (unsigned long)cam->frame[0].buffer;
3217 if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
3220 DBG(5, "VIDIOCGMBUF successfully called")
3224 case VIDIOCMCAPTURE: /* start the capture to a frame */
3226 struct video_mmap mmap;
3227 struct w9968cf_frame_t* fr;
3230 if (copy_from_user(&mmap, arg, sizeof(mmap)))
3233 DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
3234 mmap.frame, symbolic(v4l1_plist, mmap.format),
3235 mmap.width, mmap.height)
3237 if (mmap.frame >= cam->nbuffers) {
3238 DBG(4, "Invalid frame number (%u). "
3239 "VIDIOCMCAPTURE failed", mmap.frame)
3243 if (mmap.format!=cam->picture.palette &&
3244 (cam->force_palette || !w9968cf_vpp)) {
3245 DBG(4, "Palette %s rejected: only %s is allowed",
3246 symbolic(v4l1_plist, mmap.format),
3247 symbolic(v4l1_plist, cam->picture.palette))
3251 if (!w9968cf_valid_palette(mmap.format)) {
3252 DBG(4, "Palette %s not supported. "
3253 "VIDIOCMCAPTURE failed",
3254 symbolic(v4l1_plist, mmap.format))
3258 if (!cam->force_palette) {
3259 if (cam->decompression == 0) {
3260 if (w9968cf_need_decompression(mmap.format)) {
3261 DBG(4, "Decompression disabled: palette %s is not "
3262 "allowed. VIDIOCSPICT failed",
3263 symbolic(v4l1_plist, mmap.format))
3266 } else if (cam->decompression == 1) {
3267 if (!w9968cf_need_decompression(mmap.format)) {
3268 DBG(4, "Decompression forced: palette %s is not "
3269 "allowed. VIDIOCSPICT failed",
3270 symbolic(v4l1_plist, mmap.format))
3276 if ((err = w9968cf_adjust_window_size(cam, (u16*)&mmap.width,
3277 (u16*)&mmap.height))) {
3278 DBG(4, "Resolution not supported (%dx%d). "
3279 "VIDIOCMCAPTURE failed",
3280 mmap.width, mmap.height)
3284 fr = &cam->frame[mmap.frame];
3286 if (mmap.width != cam->window.width ||
3287 mmap.height != cam->window.height ||
3288 mmap.format != cam->picture.palette) {
3290 struct video_window win;
3291 struct video_picture pict;
3293 if(*cam->requested_frame
3294 || cam->frame_current->queued) {
3295 DBG(6, "VIDIOCMCAPTURE. Change settings for "
3296 "frame #%u: %dx%d, format %s. Wait...",
3297 mmap.frame, mmap.width, mmap.height,
3298 symbolic(v4l1_plist, mmap.format))
3299 err = wait_event_interruptible
3301 cam->disconnected ||
3302 (!*cam->requested_frame &&
3303 !cam->frame_current->queued) );
3306 if (cam->disconnected)
3310 memcpy(&win, &cam->window, sizeof(win));
3311 memcpy(&pict, &cam->picture, sizeof(pict));
3312 win.width = mmap.width;
3313 win.height = mmap.height;
3314 pict.palette = mmap.format;
3316 if (w9968cf_stop_transfer(cam))
3319 /* This before set_window */
3320 if (w9968cf_set_picture(cam, pict))
3323 if (w9968cf_set_window(cam, win))
3326 if (w9968cf_start_transfer(cam))
3329 } else if (fr->queued) {
3331 DBG(6, "Wait until frame #%u is free", mmap.frame)
3333 err = wait_event_interruptible(cam->wait_queue,
3334 cam->disconnected ||
3338 if (cam->disconnected)
3342 w9968cf_push_frame(cam, mmap.frame);
3343 DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
3347 case VIDIOCSYNC: /* wait until the capture of a frame is finished */
3350 struct w9968cf_frame_t* fr;
3353 if (copy_from_user(&f_num, arg, sizeof(f_num)))
3356 if (f_num >= cam->nbuffers) {
3357 DBG(4, "Invalid frame number (%u). "
3358 "VIDIOCMCAPTURE failed", f_num)
3362 DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
3364 fr = &cam->frame[f_num];
3366 switch (fr->status) {
3369 DBG(4, "VIDIOSYNC: Frame #%u not requested!",
3375 err = wait_event_interruptible(cam->wait_queue,
3376 (fr->status == F_READY)
3377 || cam->disconnected);
3380 if (cam->disconnected)
3388 w9968cf_postprocess_frame(cam, fr);
3390 fr->status = F_UNUSED;
3392 DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
3396 case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
3398 struct video_unit unit = {
3399 .video = cam->v4ldev->minor,
3400 .vbi = VIDEO_NO_UNIT,
3401 .radio = VIDEO_NO_UNIT,
3402 .audio = VIDEO_NO_UNIT,
3403 .teletext = VIDEO_NO_UNIT,
3406 if (copy_to_user(arg, &unit, sizeof(unit)))
3409 DBG(5, "VIDIOCGUNIT successfully called")
3418 if (clear_user(arg, sizeof(struct video_buffer)))
3421 DBG(5, "VIDIOCGFBUF successfully called")
3427 struct video_tuner tuner;
3428 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3431 if (tuner.tuner != 0)
3434 strcpy(tuner.name, "no_tuner");
3436 tuner.rangehigh = 0;
3437 tuner.flags = VIDEO_TUNER_NORM;
3438 tuner.mode = VIDEO_MODE_AUTO;
3439 tuner.signal = 0xffff;
3441 if (copy_to_user(arg, &tuner, sizeof(tuner)))
3444 DBG(5, "VIDIOCGTUNER successfully called")
3450 struct video_tuner tuner;
3451 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3454 if (tuner.tuner != 0)
3457 if (tuner.mode != VIDEO_MODE_AUTO)
3460 DBG(5, "VIDIOCSTUNER successfully called")
3470 case VIDIOCSPLAYMODE:
3471 case VIDIOCSWRITEMODE:
3472 case VIDIOCGPLAYINFO:
3473 case VIDIOCSMICROCODE:
3476 DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
3482 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3487 DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
3493 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3495 return -ENOIOCTLCMD;
3497 } /* end of switch */
3500 cam->misconfigured = 1;
3501 DBG(1, "VIDIOC%s failed because of hardware problems. "
3502 "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
3507 static struct file_operations w9968cf_fops = {
3508 .owner = THIS_MODULE,
3509 .open = w9968cf_open,
3510 .release = w9968cf_release,
3511 .read = w9968cf_read,
3512 .ioctl = w9968cf_ioctl,
3513 .mmap = w9968cf_mmap,
3514 .llseek = no_llseek,
3519 /****************************************************************************
3520 * USB probe and V4L registration, disconnect and id_table[] definition *
3521 ****************************************************************************/
3524 w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3526 struct usb_device *udev = interface_to_usbdev(intf);
3527 struct w9968cf_device* cam;
3529 enum w9968cf_model_id mod_id;
3530 struct list_head* ptr;
3531 u8 sc = 0; /* number of simultaneous cameras */
3532 static unsigned short dev_nr = 0; /* we are handling device number n */
3534 if (udev->descriptor.idVendor == winbond_id_table[0].idVendor &&
3535 udev->descriptor.idProduct == winbond_id_table[0].idProduct)
3536 mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
3537 else if (udev->descriptor.idVendor == winbond_id_table[1].idVendor &&
3538 udev->descriptor.idProduct == winbond_id_table[1].idProduct)
3539 mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
3543 cam = (struct w9968cf_device*)
3544 kmalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
3548 memset(cam, 0, sizeof(*cam));
3550 init_MUTEX(&cam->dev_sem);
3551 down(&cam->dev_sem);
3554 /* NOTE: a local copy is used to avoid possible race conditions */
3555 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
3557 DBG(2, "%s detected", symbolic(camlist, mod_id))
3559 if (simcams > W9968CF_MAX_DEVICES)
3560 simcams = W9968CF_SIMCAMS;
3562 /* How many cameras are connected ? */
3563 down(&w9968cf_devlist_sem);
3564 list_for_each(ptr, &w9968cf_dev_list)
3566 up(&w9968cf_devlist_sem);
3568 if (sc >= simcams) {
3569 DBG(2, "Device rejected: too many connected cameras "
3570 "(max. %u)", simcams)
3576 /* Allocate 2 bytes of memory for camera control USB transfers */
3577 if (!(cam->control_buffer = (u16*)kmalloc(2, GFP_KERNEL))) {
3578 DBG(1,"Couldn't allocate memory for camera control transfers")
3582 memset(cam->control_buffer, 0, 2);
3584 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
3585 if (!(cam->data_buffer = (u16*)kmalloc(8, GFP_KERNEL))) {
3586 DBG(1, "Couldn't allocate memory for data "
3587 "transfers to the FSB")
3591 memset(cam->data_buffer, 0, 8);
3593 /* Register the V4L device */
3594 cam->v4ldev = video_device_alloc();
3596 DBG(1, "Could not allocate memory for a V4L structure")
3601 strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
3602 cam->v4ldev->owner = THIS_MODULE;
3603 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
3604 cam->v4ldev->hardware = VID_HARDWARE_W9968CF;
3605 cam->v4ldev->fops = &w9968cf_fops;
3606 cam->v4ldev->minor = video_nr[dev_nr];
3607 cam->v4ldev->release = video_device_release;
3608 video_set_drvdata(cam->v4ldev, cam);
3609 cam->v4ldev->dev = &cam->dev;
3611 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3614 DBG(1, "V4L device registration failed")
3615 if (err == -ENFILE && video_nr[dev_nr] == -1)
3616 DBG(2, "Couldn't find a free /dev/videoX node")
3617 video_nr[dev_nr] = -1;
3618 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3622 DBG(2, "V4L device registered as /dev/video%d", cam->v4ldev->minor)
3624 /* Set some basic constants */
3625 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3627 /* Add a new entry into the list of V4L registered devices */
3628 down(&w9968cf_devlist_sem);
3629 list_add(&cam->v4llist, &w9968cf_dev_list);
3630 up(&w9968cf_devlist_sem);
3631 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3633 w9968cf_turn_on_led(cam);
3635 w9968cf_i2c_init(cam);
3637 usb_set_intfdata(intf, cam);
3641 fail: /* Free unused memory */
3642 if (cam->control_buffer)
3643 kfree(cam->control_buffer);
3644 if (cam->data_buffer)
3645 kfree(cam->data_buffer);
3647 video_device_release(cam->v4ldev);
3654 static void w9968cf_usb_disconnect(struct usb_interface* intf)
3656 struct w9968cf_device* cam =
3657 (struct w9968cf_device*)usb_get_intfdata(intf);
3659 down_write(&w9968cf_disconnect);
3662 /* Prevent concurrent accesses to data */
3663 down(&cam->dev_sem);
3665 cam->disconnected = 1;
3667 DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id))
3669 wake_up_interruptible_all(&cam->open);
3672 DBG(2, "The device is open (/dev/video%d)! "
3673 "Process name: %s. Deregistration and memory "
3674 "deallocation are deferred on close.",
3675 cam->v4ldev->minor, cam->command)
3676 cam->misconfigured = 1;
3677 w9968cf_stop_transfer(cam);
3678 wake_up_interruptible(&cam->wait_queue);
3680 w9968cf_release_resources(cam);
3688 up_write(&w9968cf_disconnect);
3692 static struct usb_driver w9968cf_usb_driver = {
3693 .owner = THIS_MODULE,
3695 .id_table = winbond_id_table,
3696 .probe = w9968cf_usb_probe,
3697 .disconnect = w9968cf_usb_disconnect,
3702 /****************************************************************************
3703 * Module init, exit and intermodule communication *
3704 ****************************************************************************/
3706 static int w9968cf_vppmod_detect(struct w9968cf_device* cam)
3710 request_module("w9968cf-vpp");
3712 down(&w9968cf_vppmod_lock);
3715 DBG(4, "Video post-processing module not detected")
3716 w9968cf_adjust_configuration(cam);
3720 if (!try_module_get(w9968cf_vpp->owner)) {
3721 DBG(1, "Couldn't increment the reference count of "
3722 "the video post-processing module")
3723 up(&w9968cf_vppmod_lock);
3727 w9968cf_vpp->busy++;
3729 DBG(5, "Video post-processing module detected")
3732 up(&w9968cf_vppmod_lock);
3737 static void w9968cf_vppmod_release(struct w9968cf_device* cam)
3739 down(&w9968cf_vppmod_lock);
3741 if (w9968cf_vpp && w9968cf_vpp->busy) {
3742 module_put(w9968cf_vpp->owner);
3743 w9968cf_vpp->busy--;
3744 wake_up(&w9968cf_vppmod_wait);
3745 DBG(5, "Video post-processing module released")
3748 up(&w9968cf_vppmod_lock);
3752 int w9968cf_vppmod_register(struct w9968cf_vpp_t* vpp)
3754 down(&w9968cf_vppmod_lock);
3757 KDBG(1, "Video post-processing module already registered")
3758 up(&w9968cf_vppmod_lock);
3763 w9968cf_vpp->busy = 0;
3765 KDBG(2, "Video post-processing module registered")
3766 up(&w9968cf_vppmod_lock);
3771 int w9968cf_vppmod_deregister(struct w9968cf_vpp_t* vpp)
3773 down(&w9968cf_vppmod_lock);
3776 up(&w9968cf_vppmod_lock);
3780 if (w9968cf_vpp != vpp) {
3781 KDBG(1, "Only the owner can unregister the video "
3782 "post-processing module")
3783 up(&w9968cf_vppmod_lock);
3787 if (w9968cf_vpp->busy) {
3788 KDBG(2, "Video post-processing module busy. Wait for it to be "
3790 up(&w9968cf_vppmod_lock);
3791 wait_event(w9968cf_vppmod_wait, !w9968cf_vpp->busy);
3798 up(&w9968cf_vppmod_lock);
3801 KDBG(2, "Video post-processing module unregistered")
3806 static int __init w9968cf_module_init(void)
3810 KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
3811 KDBG(3, W9968CF_MODULE_AUTHOR)
3814 request_module("ovcamchip");
3816 if ((err = usb_register(&w9968cf_usb_driver)))
3823 static void __exit w9968cf_module_exit(void)
3825 /* w9968cf_usb_disconnect() will be called */
3826 usb_deregister(&w9968cf_usb_driver);
3828 KDBG(2, W9968CF_MODULE_NAME" deregistered")
3832 module_init(w9968cf_module_init);
3833 module_exit(w9968cf_module_exit);
3836 EXPORT_SYMBOL(w9968cf_vppmod_register);
3837 EXPORT_SYMBOL(w9968cf_vppmod_deregister);