1 /***************************************************************************
2 * Video4Linux driver for W996[87]CF JPEG USB Dual Mode Camera Chip. *
4 * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * - Memory management code from bttv driver by Ralph Metzler, *
7 * Marcus Metzler and Gerd Knorr. *
8 * - I2C interface to kernel, high-level image sensor control routines and *
9 * some symbolic names from OV511 driver by Mark W. McClelland. *
10 * - Low-level I2C fast write function by Piotr Czerczak. *
11 * - Low-level I2C read function by Frederic Jouault. *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the Free Software *
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
26 ***************************************************************************/
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/kmod.h>
31 #include <linux/init.h>
33 #include <linux/vmalloc.h>
34 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/sched.h>
39 #include <linux/ioctl.h>
40 #include <linux/delay.h>
41 #include <linux/stddef.h>
43 #include <asm/uaccess.h>
44 #include <linux/page-flags.h>
45 #include <media/v4l2-ioctl.h>
48 #include "w9968cf_decoder.h"
50 static struct w9968cf_vpp_t* w9968cf_vpp;
51 static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
53 static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
54 static DEFINE_MUTEX(w9968cf_devlist_mutex); /* semaphore for list traversal */
56 static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
59 /****************************************************************************
60 * Module macros and parameters *
61 ****************************************************************************/
63 MODULE_DEVICE_TABLE(usb, winbond_id_table);
65 MODULE_AUTHOR(W9968CF_MODULE_AUTHOR" "W9968CF_AUTHOR_EMAIL);
66 MODULE_DESCRIPTION(W9968CF_MODULE_NAME);
67 MODULE_VERSION(W9968CF_MODULE_VERSION);
68 MODULE_LICENSE(W9968CF_MODULE_LICENSE);
69 MODULE_SUPPORTED_DEVICE("Video");
71 static unsigned short simcams = W9968CF_SIMCAMS;
72 static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
73 static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
75 static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
77 static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
78 W9968CF_DOUBLE_BUFFER};
79 static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
80 static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
82 static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
83 static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
84 W9968CF_DECOMPRESSION};
85 static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
86 static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
87 static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
88 static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
89 static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
90 static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
92 static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
93 W9968CF_BANDINGFILTER};
94 static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
95 static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
96 static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
97 static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
98 static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
100 static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
101 static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
102 static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
104 static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
107 static unsigned short debug = W9968CF_DEBUG_LEVEL;
108 static int specific_debug = W9968CF_SPECIFIC_DEBUG;
111 static unsigned int param_nv[24]; /* number of values per parameter */
113 module_param(simcams, ushort, 0644);
114 module_param_array(video_nr, short, ¶m_nv[0], 0444);
115 module_param_array(packet_size, uint, ¶m_nv[1], 0444);
116 module_param_array(max_buffers, ushort, ¶m_nv[2], 0444);
117 module_param_array(double_buffer, bool, ¶m_nv[3], 0444);
118 module_param_array(clamping, bool, ¶m_nv[4], 0444);
119 module_param_array(filter_type, ushort, ¶m_nv[5], 0444);
120 module_param_array(largeview, bool, ¶m_nv[6], 0444);
121 module_param_array(decompression, ushort, ¶m_nv[7], 0444);
122 module_param_array(upscaling, bool, ¶m_nv[8], 0444);
123 module_param_array(force_palette, ushort, ¶m_nv[9], 0444);
124 module_param_array(force_rgb, ushort, ¶m_nv[10], 0444);
125 module_param_array(autobright, bool, ¶m_nv[11], 0444);
126 module_param_array(autoexp, bool, ¶m_nv[12], 0444);
127 module_param_array(lightfreq, ushort, ¶m_nv[13], 0444);
128 module_param_array(bandingfilter, bool, ¶m_nv[14], 0444);
129 module_param_array(clockdiv, short, ¶m_nv[15], 0444);
130 module_param_array(backlight, bool, ¶m_nv[16], 0444);
131 module_param_array(mirror, bool, ¶m_nv[17], 0444);
132 module_param_array(monochrome, bool, ¶m_nv[18], 0444);
133 module_param_array(brightness, uint, ¶m_nv[19], 0444);
134 module_param_array(hue, uint, ¶m_nv[20], 0444);
135 module_param_array(colour, uint, ¶m_nv[21], 0444);
136 module_param_array(contrast, uint, ¶m_nv[22], 0444);
137 module_param_array(whiteness, uint, ¶m_nv[23], 0444);
139 module_param(debug, ushort, 0644);
140 module_param(specific_debug, bool, 0644);
143 MODULE_PARM_DESC(simcams,
144 "\n<n> Number of cameras allowed to stream simultaneously."
145 "\nn may vary from 0 to "
146 __MODULE_STRING(W9968CF_MAX_DEVICES)"."
147 "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
149 MODULE_PARM_DESC(video_nr,
150 "\n<-1|n[,...]> Specify V4L minor mode number."
151 "\n -1 = use next available (default)"
152 "\n n = use minor number n (integer >= 0)"
153 "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
156 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
157 "\nthe second camera and use auto for the first"
158 "\none and for every other camera."
160 MODULE_PARM_DESC(packet_size,
161 "\n<n[,...]> Specify the maximum data payload"
162 "\nsize in bytes for alternate settings, for each device."
163 "\nn is scaled between 63 and 1023 "
164 "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
166 MODULE_PARM_DESC(max_buffers,
167 "\n<n[,...]> For advanced users."
168 "\nSpecify the maximum number of video frame buffers"
169 "\nto allocate for each device, from 2 to "
170 __MODULE_STRING(W9968CF_MAX_BUFFERS)
171 ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
173 MODULE_PARM_DESC(double_buffer,
175 "Hardware double buffering: 0 disabled, 1 enabled."
176 "\nIt should be enabled if you want smooth video output: if"
177 "\nyou obtain out of sync. video, disable it, or try to"
178 "\ndecrease the 'clockdiv' module parameter value."
179 "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
182 MODULE_PARM_DESC(clamping,
183 "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
184 "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
187 MODULE_PARM_DESC(filter_type,
188 "\n<0|1|2[,...]> Video filter type."
189 "\n0 none, 1 (1-2-1) 3-tap filter, "
190 "2 (2-3-6-3-2) 5-tap filter."
191 "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
193 "\nThe filter is used to reduce noise and aliasing artifacts"
194 "\nproduced by the CCD or CMOS image sensor, and the scaling"
197 MODULE_PARM_DESC(largeview,
198 "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
199 "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
202 MODULE_PARM_DESC(upscaling,
203 "\n<0|1[,...]> Software scaling (for non-compressed video):"
204 "\n0 disabled, 1 enabled."
205 "\nDisable it if you have a slow CPU or you don't have"
207 "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
209 "\nIf 'w9968cf-vpp' is not present, this parameter is"
212 MODULE_PARM_DESC(decompression,
213 "\n<0|1|2[,...]> Software video decompression:"
214 "\n- 0 disables decompression (doesn't allow formats needing"
216 "\n- 1 forces decompression (allows formats needing"
217 " decompression only);"
218 "\n- 2 allows any permitted formats."
219 "\nFormats supporting compressed video are YUV422P and"
221 "\nin any resolutions where both width and height are "
223 "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
225 "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
226 "\nnot allowed; in this case this parameter is set to 2."
228 MODULE_PARM_DESC(force_palette,
230 "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
231 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
232 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
233 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
234 "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
235 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
236 "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
237 "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
238 "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
239 "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
240 "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
242 " Force picture palette."
244 "\n- 0 allows any of the following formats:"
245 "\n- UYVY 16 bpp - Original video, compression disabled"
246 "\n- YUV420 12 bpp - Original video, compression enabled"
247 "\n- YUV422P 16 bpp - Original video, compression enabled"
248 "\n- YUV420P 12 bpp - Original video, compression enabled"
249 "\n- YUVY 16 bpp - Software conversion from UYVY"
250 "\n- YUV422 16 bpp - Software conversion from UYVY"
251 "\n- GREY 8 bpp - Software conversion from UYVY"
252 "\n- RGB555 16 bpp - Software conversion from UYVY"
253 "\n- RGB565 16 bpp - Software conversion from UYVY"
254 "\n- RGB24 24 bpp - Software conversion from UYVY"
255 "\n- RGB32 32 bpp - Software conversion from UYVY"
256 "\nWhen not 0, this parameter will override 'decompression'."
257 "\nDefault value is 0 for every device."
258 "\nInitial palette is "
259 __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
260 "\nIf 'w9968cf-vpp' is not present, this parameter is"
263 MODULE_PARM_DESC(force_rgb,
264 "\n<0|1[,...]> Read RGB video data instead of BGR:"
265 "\n 1 = use RGB component ordering."
266 "\n 0 = use BGR component ordering."
267 "\nThis parameter has effect when using RGBX palettes only."
268 "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
271 MODULE_PARM_DESC(autobright,
272 "\n<0|1[,...]> Image sensor automatically changes brightness:"
274 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
277 MODULE_PARM_DESC(autoexp,
278 "\n<0|1[,...]> Image sensor automatically changes exposure:"
280 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
283 MODULE_PARM_DESC(lightfreq,
284 "\n<50|60[,...]> Light frequency in Hz:"
285 "\n 50 for European and Asian lighting,"
286 " 60 for American lighting."
287 "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
290 MODULE_PARM_DESC(bandingfilter,
291 "\n<0|1[,...]> Banding filter to reduce effects of"
292 " fluorescent lighting:"
293 "\n 0 disabled, 1 enabled."
294 "\nThis filter tries to reduce the pattern of horizontal"
295 "\nlight/dark bands caused by some (usually fluorescent)"
297 "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
300 MODULE_PARM_DESC(clockdiv,
302 "Force pixel clock divisor to a specific value (for experts):"
303 "\n n may vary from 0 to 127."
304 "\n -1 for automatic value."
305 "\nSee also the 'double_buffer' module parameter."
306 "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
309 MODULE_PARM_DESC(backlight,
310 "\n<0|1[,...]> Objects are lit from behind:"
312 "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
315 MODULE_PARM_DESC(mirror,
316 "\n<0|1[,...]> Reverse image horizontally:"
318 "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
321 MODULE_PARM_DESC(monochrome,
322 "\n<0|1[,...]> Use image sensor as monochrome sensor:"
324 "\nNot all the sensors support monochrome color."
325 "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
328 MODULE_PARM_DESC(brightness,
329 "\n<n[,...]> Set picture brightness (0-65535)."
330 "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
332 "\nThis parameter has no effect if 'autobright' is enabled."
334 MODULE_PARM_DESC(hue,
335 "\n<n[,...]> Set picture hue (0-65535)."
336 "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
339 MODULE_PARM_DESC(colour,
340 "\n<n[,...]> Set picture saturation (0-65535)."
341 "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
344 MODULE_PARM_DESC(contrast,
345 "\n<n[,...]> Set picture contrast (0-65535)."
346 "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
349 MODULE_PARM_DESC(whiteness,
350 "\n<n[,...]> Set picture whiteness (0-65535)."
351 "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
355 MODULE_PARM_DESC(debug,
356 "\n<n> Debugging information level, from 0 to 6:"
357 "\n0 = none (use carefully)"
358 "\n1 = critical errors"
359 "\n2 = significant informations"
360 "\n3 = configuration or general messages"
362 "\n5 = called functions"
363 "\n6 = function internals"
364 "\nLevel 5 and 6 are useful for testing only, when only "
365 "one device is used."
366 "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
368 MODULE_PARM_DESC(specific_debug,
369 "\n<0|1> Enable or disable specific debugging messages:"
370 "\n0 = print messages concerning every level"
372 "\n1 = print messages concerning the level"
373 " indicated by 'debug'."
374 "\nDefault value is "
375 __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
377 #endif /* W9968CF_DEBUG */
381 /****************************************************************************
383 ****************************************************************************/
385 /* Video4linux interface */
386 static const struct v4l2_file_operations w9968cf_fops;
387 static int w9968cf_open(struct file *);
388 static int w9968cf_release(struct file *);
389 static int w9968cf_mmap(struct file *, struct vm_area_struct *);
390 static long w9968cf_ioctl(struct file *, unsigned, unsigned long);
391 static ssize_t w9968cf_read(struct file *, char __user *, size_t, loff_t *);
392 static long w9968cf_v4l_ioctl(struct file *, unsigned int,
396 static int w9968cf_start_transfer(struct w9968cf_device*);
397 static int w9968cf_stop_transfer(struct w9968cf_device*);
398 static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
399 static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
400 static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
401 static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
402 static int w9968cf_read_sb(struct w9968cf_device*);
403 static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
404 static void w9968cf_urb_complete(struct urb *urb);
406 /* Low-level I2C (SMBus) I/O */
407 static int w9968cf_smbus_start(struct w9968cf_device*);
408 static int w9968cf_smbus_stop(struct w9968cf_device*);
409 static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
410 static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
411 static int w9968cf_smbus_write_ack(struct w9968cf_device*);
412 static int w9968cf_smbus_read_ack(struct w9968cf_device*);
413 static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
414 static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
415 u16 address, u8* value);
416 static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
417 u8 subaddress, u8* value);
418 static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
419 u16 address, u8 subaddress);
420 static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
421 u16 address, u8 subaddress,
424 /* I2C interface to kernel */
425 static int w9968cf_i2c_init(struct w9968cf_device*);
426 static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
427 unsigned short flags, char read_write,
428 u8 command, int size, union i2c_smbus_data*);
429 static u32 w9968cf_i2c_func(struct i2c_adapter*);
431 /* Memory management */
432 static void* rvmalloc(unsigned long size);
433 static void rvfree(void *mem, unsigned long size);
434 static void w9968cf_deallocate_memory(struct w9968cf_device*);
435 static int w9968cf_allocate_memory(struct w9968cf_device*);
437 /* High-level image sensor control functions */
438 static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
439 static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
440 static int w9968cf_sensor_cmd(struct w9968cf_device*,
441 unsigned int cmd, void *arg);
442 static int w9968cf_sensor_init(struct w9968cf_device*);
443 static int w9968cf_sensor_update_settings(struct w9968cf_device*);
444 static int w9968cf_sensor_get_picture(struct w9968cf_device*);
445 static int w9968cf_sensor_update_picture(struct w9968cf_device*,
446 struct video_picture pict);
448 /* Other helper functions */
449 static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
450 enum w9968cf_model_id,
451 const unsigned short dev_nr);
452 static void w9968cf_adjust_configuration(struct w9968cf_device*);
453 static int w9968cf_turn_on_led(struct w9968cf_device*);
454 static int w9968cf_init_chip(struct w9968cf_device*);
455 static inline u16 w9968cf_valid_palette(u16 palette);
456 static inline u16 w9968cf_valid_depth(u16 palette);
457 static inline u8 w9968cf_need_decompression(u16 palette);
458 static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
459 static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
460 static int w9968cf_postprocess_frame(struct w9968cf_device*,
461 struct w9968cf_frame_t*);
462 static int w9968cf_adjust_window_size(struct w9968cf_device*, u16* w, u16* h);
463 static void w9968cf_init_framelist(struct w9968cf_device*);
464 static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
465 static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
466 static void w9968cf_release_resources(struct w9968cf_device*);
470 /****************************************************************************
472 ****************************************************************************/
474 /* Used to represent a list of values and their respective symbolic names */
475 struct w9968cf_symbolic_list {
480 /*--------------------------------------------------------------------------
481 Returns the name of the matching element in the symbolic_list array. The
482 end of the list must be marked with an element that has a NULL name.
483 --------------------------------------------------------------------------*/
484 static inline const char *
485 symbolic(struct w9968cf_symbolic_list list[], const int num)
489 for (i = 0; list[i].name != NULL; i++)
490 if (list[i].num == num)
491 return (list[i].name);
496 static struct w9968cf_symbolic_list camlist[] = {
497 { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
498 { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
500 /* Other cameras (having the same descriptors as Generic W996[87]CF) */
501 { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
502 { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
503 { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
504 { W9968CF_MOD_LL, "Lebon LDC-035A" },
505 { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
506 { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
507 { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
508 { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
509 { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
514 static struct w9968cf_symbolic_list senlist[] = {
515 { CC_OV76BE, "OV76BE" },
516 { CC_OV7610, "OV7610" },
517 { CC_OV7620, "OV7620" },
518 { CC_OV7620AE, "OV7620AE" },
519 { CC_OV6620, "OV6620" },
520 { CC_OV6630, "OV6630" },
521 { CC_OV6630AE, "OV6630AE" },
522 { CC_OV6630AF, "OV6630AF" },
526 /* Video4Linux1 palettes */
527 static struct w9968cf_symbolic_list v4l1_plist[] = {
528 { VIDEO_PALETTE_GREY, "GREY" },
529 { VIDEO_PALETTE_HI240, "HI240" },
530 { VIDEO_PALETTE_RGB565, "RGB565" },
531 { VIDEO_PALETTE_RGB24, "RGB24" },
532 { VIDEO_PALETTE_RGB32, "RGB32" },
533 { VIDEO_PALETTE_RGB555, "RGB555" },
534 { VIDEO_PALETTE_YUV422, "YUV422" },
535 { VIDEO_PALETTE_YUYV, "YUYV" },
536 { VIDEO_PALETTE_UYVY, "UYVY" },
537 { VIDEO_PALETTE_YUV420, "YUV420" },
538 { VIDEO_PALETTE_YUV411, "YUV411" },
539 { VIDEO_PALETTE_RAW, "RAW" },
540 { VIDEO_PALETTE_YUV422P, "YUV422P" },
541 { VIDEO_PALETTE_YUV411P, "YUV411P" },
542 { VIDEO_PALETTE_YUV420P, "YUV420P" },
543 { VIDEO_PALETTE_YUV410P, "YUV410P" },
547 /* Decoder error codes: */
548 static struct w9968cf_symbolic_list decoder_errlist[] = {
549 { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
550 { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
551 { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
552 { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
553 { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
554 { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
558 /* URB error codes: */
559 static struct w9968cf_symbolic_list urb_errlist[] = {
560 { -ENOMEM, "No memory for allocation of internal structures" },
561 { -ENOSPC, "The host controller's bandwidth is already consumed" },
562 { -ENOENT, "URB was canceled by unlink_urb" },
563 { -EXDEV, "ISO transfer only partially completed" },
564 { -EAGAIN, "Too match scheduled for the future" },
565 { -ENXIO, "URB already queued" },
566 { -EFBIG, "Too much ISO frames requested" },
567 { -ENOSR, "Buffer error (overrun)" },
568 { -EPIPE, "Specified endpoint is stalled (device not responding)"},
569 { -EOVERFLOW, "Babble (too much data)" },
570 { -EPROTO, "Bit-stuff error (bad cable?)" },
571 { -EILSEQ, "CRC/Timeout" },
572 { -ETIME, "Device does not respond to token" },
573 { -ETIMEDOUT, "Device does not respond to command" },
577 /****************************************************************************
578 * Memory management functions *
579 ****************************************************************************/
580 static void* rvmalloc(unsigned long size)
585 size = PAGE_ALIGN(size);
586 mem = vmalloc_32(size);
590 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
591 adr = (unsigned long) mem;
593 SetPageReserved(vmalloc_to_page((void *)adr));
602 static void rvfree(void* mem, unsigned long size)
609 adr = (unsigned long) mem;
610 while ((long) size > 0) {
611 ClearPageReserved(vmalloc_to_page((void *)adr));
619 /*--------------------------------------------------------------------------
620 Deallocate previously allocated memory.
621 --------------------------------------------------------------------------*/
622 static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
626 /* Free the isochronous transfer buffers */
627 for (i = 0; i < W9968CF_URBS; i++) {
628 kfree(cam->transfer_buffer[i]);
629 cam->transfer_buffer[i] = NULL;
632 /* Free temporary frame buffer */
633 if (cam->frame_tmp.buffer) {
634 rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
635 cam->frame_tmp.buffer = NULL;
638 /* Free helper buffer */
639 if (cam->frame_vpp.buffer) {
640 rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
641 cam->frame_vpp.buffer = NULL;
644 /* Free video frame buffers */
645 if (cam->frame[0].buffer) {
646 rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
647 cam->frame[0].buffer = NULL;
652 DBG(5, "Memory successfully deallocated")
656 /*--------------------------------------------------------------------------
657 Allocate memory buffers for USB transfers and video frames.
658 This function is called by open() only.
659 Return 0 on success, a negative number otherwise.
660 --------------------------------------------------------------------------*/
661 static int w9968cf_allocate_memory(struct w9968cf_device* cam)
663 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
665 unsigned long hw_bufsize, vpp_bufsize;
668 /* NOTE: Deallocation is done elsewhere in case of error */
670 /* Calculate the max amount of raw data per frame from the device */
671 hw_bufsize = cam->maxwidth*cam->maxheight*2;
673 /* Calculate the max buf. size needed for post-processing routines */
674 bpp = (w9968cf_vpp) ? 4 : 2;
676 vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
677 cam->maxwidth*cam->maxheight*bpp);
679 vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
681 /* Allocate memory for the isochronous transfer buffers */
682 for (i = 0; i < W9968CF_URBS; i++) {
683 if (!(cam->transfer_buffer[i] =
684 kzalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
685 DBG(1, "Couldn't allocate memory for the isochronous "
686 "transfer buffers (%u bytes)",
687 p_size * W9968CF_ISO_PACKETS)
692 /* Allocate memory for the temporary frame buffer */
693 if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
694 DBG(1, "Couldn't allocate memory for the temporary "
695 "video frame buffer (%lu bytes)", hw_bufsize)
698 cam->frame_tmp.size = hw_bufsize;
699 cam->frame_tmp.number = -1;
701 /* Allocate memory for the helper buffer */
703 if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
704 DBG(1, "Couldn't allocate memory for the helper buffer"
705 " (%lu bytes)", vpp_bufsize)
708 cam->frame_vpp.size = vpp_bufsize;
710 cam->frame_vpp.buffer = NULL;
712 /* Allocate memory for video frame buffers */
713 cam->nbuffers = cam->max_buffers;
714 while (cam->nbuffers >= 2) {
715 if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
722 DBG(1, "Couldn't allocate memory for the video frame buffers")
727 if (cam->nbuffers != cam->max_buffers)
728 DBG(2, "Couldn't allocate memory for %u video frame buffers. "
729 "Only memory for %u buffers has been allocated",
730 cam->max_buffers, cam->nbuffers)
732 for (i = 0; i < cam->nbuffers; i++) {
733 cam->frame[i].buffer = buff + i*vpp_bufsize;
734 cam->frame[i].size = vpp_bufsize;
735 cam->frame[i].number = i;
737 if (i != cam->nbuffers-1)
738 cam->frame[i].next = &cam->frame[i+1];
740 cam->frame[i].next = &cam->frame[0];
741 cam->frame[i].status = F_UNUSED;
744 DBG(5, "Memory successfully allocated")
750 /****************************************************************************
751 * USB-specific functions *
752 ****************************************************************************/
754 /*--------------------------------------------------------------------------
755 This is an handler function which is called after the URBs are completed.
756 It collects multiple data packets coming from the camera by putting them
757 into frame buffers: one or more zero data length data packets are used to
758 mark the end of a video frame; the first non-zero data packet is the start
759 of the next video frame; if an error is encountered in a packet, the entire
760 video frame is discarded and grabbed again.
761 If there are no requested frames in the FIFO list, packets are collected into
763 --------------------------------------------------------------------------*/
764 static void w9968cf_urb_complete(struct urb *urb)
766 struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
767 struct w9968cf_frame_t** f;
768 unsigned int len, status;
773 if ((!cam->streaming) || cam->disconnected) {
774 DBG(4, "Got interrupt, but not streaming")
778 /* "(*f)" will be used instead of "cam->frame_current" */
779 f = &cam->frame_current;
781 /* If a frame has been requested and we are grabbing into
782 the temporary frame, we'll switch to that requested frame */
783 if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
784 if (cam->frame_tmp.status == F_GRABBING) {
785 w9968cf_pop_frame(cam, &cam->frame_current);
786 (*f)->status = F_GRABBING;
787 (*f)->length = cam->frame_tmp.length;
788 memcpy((*f)->buffer, cam->frame_tmp.buffer,
790 DBG(6, "Switched from temp. frame to frame #%d",
795 for (i = 0; i < urb->number_of_packets; i++) {
796 len = urb->iso_frame_desc[i].actual_length;
797 status = urb->iso_frame_desc[i].status;
798 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
800 if (status && len != 0) {
801 DBG(4, "URB failed, error in data packet "
803 status, symbolic(urb_errlist, status))
804 (*f)->status = F_ERROR;
808 if (len) { /* start of frame */
810 if ((*f)->status == F_UNUSED) {
811 (*f)->status = F_GRABBING;
815 /* Buffer overflows shouldn't happen, however...*/
816 if ((*f)->length + len > (*f)->size) {
817 DBG(4, "Buffer overflow: bad data packets")
818 (*f)->status = F_ERROR;
821 if ((*f)->status == F_GRABBING) {
822 memcpy((*f)->buffer + (*f)->length, pos, len);
826 } else if ((*f)->status == F_GRABBING) { /* end of frame */
828 DBG(6, "Frame #%d successfully grabbed", (*f)->number)
830 if (cam->vpp_flag & VPP_DECOMPRESSION) {
831 err = w9968cf_vpp->check_headers((*f)->buffer,
834 DBG(4, "Skip corrupted frame: %s",
835 symbolic(decoder_errlist, err))
836 (*f)->status = F_UNUSED;
837 continue; /* grab this frame again */
841 (*f)->status = F_READY;
844 /* Take a pointer to the new frame from the FIFO list.
845 If the list is empty,we'll use the temporary frame*/
846 if (*cam->requested_frame)
847 w9968cf_pop_frame(cam, &cam->frame_current);
849 cam->frame_current = &cam->frame_tmp;
850 (*f)->status = F_UNUSED;
853 } else if ((*f)->status == F_ERROR)
854 (*f)->status = F_UNUSED; /* grab it again */
856 PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
857 (unsigned long)(*f)->length, i, len, (*f)->status)
861 /* Resubmit this URB */
862 urb->dev = cam->usbdev;
864 spin_lock(&cam->urb_lock);
866 if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
867 cam->misconfigured = 1;
868 DBG(1, "Couldn't resubmit the URB: error %d, %s",
869 err, symbolic(urb_errlist, err))
871 spin_unlock(&cam->urb_lock);
873 /* Wake up the user process */
874 wake_up_interruptible(&cam->wait_queue);
878 /*---------------------------------------------------------------------------
879 Setup the URB structures for the isochronous transfer.
880 Submit the URBs so that the data transfer begins.
881 Return 0 on success, a negative number otherwise.
882 ---------------------------------------------------------------------------*/
883 static int w9968cf_start_transfer(struct w9968cf_device* cam)
885 struct usb_device *udev = cam->usbdev;
887 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
894 for (i = 0; i < W9968CF_URBS; i++) {
895 urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
897 for (j = 0; j < i; j++)
898 usb_free_urb(cam->urb[j]);
899 DBG(1, "Couldn't allocate the URB structures")
905 urb->context = (void*)cam;
906 urb->pipe = usb_rcvisocpipe(udev, 1);
907 urb->transfer_flags = URB_ISO_ASAP;
908 urb->number_of_packets = W9968CF_ISO_PACKETS;
909 urb->complete = w9968cf_urb_complete;
910 urb->transfer_buffer = cam->transfer_buffer[i];
911 urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
913 for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
914 urb->iso_frame_desc[j].offset = p_size*j;
915 urb->iso_frame_desc[j].length = p_size;
919 /* Transfer size per frame, in WORD ! */
926 err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
927 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
930 err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
931 err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
933 if (cam->vpp_flag & VPP_DECOMPRESSION)
934 err += w9968cf_upload_quantizationtables(cam);
936 vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
937 err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
939 err += usb_set_interface(udev, 0, cam->altsetting);
940 err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
942 if (err || (vidcapt < 0)) {
943 for (i = 0; i < W9968CF_URBS; i++)
944 usb_free_urb(cam->urb[i]);
945 DBG(1, "Couldn't tell the camera to start the data transfer")
949 w9968cf_init_framelist(cam);
951 /* Begin to grab into the temporary buffer */
952 cam->frame_tmp.status = F_UNUSED;
953 cam->frame_tmp.queued = 0;
954 cam->frame_current = &cam->frame_tmp;
956 if (!(cam->vpp_flag & VPP_DECOMPRESSION))
957 DBG(5, "Isochronous transfer size: %lu bytes/frame",
958 (unsigned long)t_size*2)
960 DBG(5, "Starting the isochronous transfer...")
964 /* Submit the URBs */
965 for (i = 0; i < W9968CF_URBS; i++) {
966 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
969 for (j = i-1; j >= 0; j--) {
970 usb_kill_urb(cam->urb[j]);
971 usb_free_urb(cam->urb[j]);
973 DBG(1, "Couldn't send a transfer request to the "
974 "USB core (error #%d, %s)", err,
975 symbolic(urb_errlist, err))
984 /*--------------------------------------------------------------------------
985 Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
986 Return 0 on success, a negative number otherwise.
987 --------------------------------------------------------------------------*/
988 static int w9968cf_stop_transfer(struct w9968cf_device* cam)
990 struct usb_device *udev = cam->usbdev;
991 unsigned long lock_flags;
998 /* This avoids race conditions with usb_submit_urb()
999 in the URB completition handler */
1000 spin_lock_irqsave(&cam->urb_lock, lock_flags);
1002 spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
1004 for (i = W9968CF_URBS-1; i >= 0; i--)
1006 usb_kill_urb(cam->urb[i]);
1007 usb_free_urb(cam->urb[i]);
1011 if (cam->disconnected)
1014 err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
1015 err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
1016 err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
1017 err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
1020 DBG(2, "Failed to tell the camera to stop the isochronous "
1021 "transfer. However this is not a critical error.")
1026 DBG(5, "Isochronous transfer stopped")
1031 /*--------------------------------------------------------------------------
1032 Write a W9968CF register.
1033 Return 0 on success, -1 otherwise.
1034 --------------------------------------------------------------------------*/
1035 static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
1037 struct usb_device* udev = cam->usbdev;
1040 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1041 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1042 value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
1045 DBG(4, "Failed to write a register "
1046 "(value 0x%04X, index 0x%02X, error #%d, %s)",
1047 value, index, res, symbolic(urb_errlist, res))
1049 return (res >= 0) ? 0 : -1;
1053 /*--------------------------------------------------------------------------
1054 Read a W9968CF register.
1055 Return the register value on success, -1 otherwise.
1056 --------------------------------------------------------------------------*/
1057 static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
1059 struct usb_device* udev = cam->usbdev;
1060 u16* buff = cam->control_buffer;
1063 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
1064 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1065 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
1068 DBG(4, "Failed to read a register "
1069 "(index 0x%02X, error #%d, %s)",
1070 index, res, symbolic(urb_errlist, res))
1072 return (res >= 0) ? (int)(*buff) : -1;
1076 /*--------------------------------------------------------------------------
1077 Write 64-bit data to the fast serial bus registers.
1078 Return 0 on success, -1 otherwise.
1079 --------------------------------------------------------------------------*/
1080 static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
1082 struct usb_device* udev = cam->usbdev;
1088 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
1089 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1090 value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
1093 DBG(4, "Failed to write the FSB registers "
1094 "(error #%d, %s)", res, symbolic(urb_errlist, res))
1096 return (res >= 0) ? 0 : -1;
1100 /*--------------------------------------------------------------------------
1101 Write data to the serial bus control register.
1102 Return 0 on success, a negative number otherwise.
1103 --------------------------------------------------------------------------*/
1104 static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
1108 err = w9968cf_write_reg(cam, value, 0x01);
1109 udelay(W9968CF_I2C_BUS_DELAY);
1115 /*--------------------------------------------------------------------------
1116 Read data from the serial bus control register.
1117 Return 0 on success, a negative number otherwise.
1118 --------------------------------------------------------------------------*/
1119 static int w9968cf_read_sb(struct w9968cf_device* cam)
1123 v = w9968cf_read_reg(cam, 0x01);
1124 udelay(W9968CF_I2C_BUS_DELAY);
1130 /*--------------------------------------------------------------------------
1131 Upload quantization tables for the JPEG compression.
1132 This function is called by w9968cf_start_transfer().
1133 Return 0 on success, a negative number otherwise.
1134 --------------------------------------------------------------------------*/
1135 static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
1140 err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
1142 for (i = 0, j = 0; i < 32; i++, j += 2) {
1143 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
1144 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
1145 err += w9968cf_write_reg(cam, a, 0x40+i);
1146 err += w9968cf_write_reg(cam, b, 0x60+i);
1148 err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
1155 /****************************************************************************
1156 * Low-level I2C I/O functions. *
1157 * The adapter supports the following I2C transfer functions: *
1158 * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
1159 * i2c_adap_read_byte_data() *
1160 * i2c_adap_read_byte() *
1161 ****************************************************************************/
1163 static int w9968cf_smbus_start(struct w9968cf_device* cam)
1167 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1168 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1174 static int w9968cf_smbus_stop(struct w9968cf_device* cam)
1178 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1179 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1185 static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
1190 for (bit = 0 ; bit < 8 ; bit++) {
1191 sda = (v & 0x80) ? 2 : 0;
1193 /* SDE=1, SDA=sda, SCL=0 */
1194 err += w9968cf_write_sb(cam, 0x10 | sda);
1195 /* SDE=1, SDA=sda, SCL=1 */
1196 err += w9968cf_write_sb(cam, 0x11 | sda);
1197 /* SDE=1, SDA=sda, SCL=0 */
1198 err += w9968cf_write_sb(cam, 0x10 | sda);
1205 static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
1211 for (bit = 0 ; bit < 8 ; bit++) {
1213 err += w9968cf_write_sb(cam, 0x0013);
1214 *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
1215 err += w9968cf_write_sb(cam, 0x0012);
1222 static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
1226 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1227 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1228 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1234 static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
1238 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1239 sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
1240 err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
1244 DBG(6, "Couldn't receive the ACK")
1252 /* This seems to refresh the communication through the serial bus */
1253 static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
1257 for (j = 1; j <= 10; j++) {
1258 err = w9968cf_write_reg(cam, 0x0020, 0x01);
1259 err += w9968cf_write_reg(cam, 0x0000, 0x01);
1268 /* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
1270 w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
1271 u16 address, u8 subaddress,u8 value)
1273 u16* data = cam->data_buffer;
1276 err += w9968cf_smbus_refresh_bus(cam);
1278 /* Enable SBUS outputs */
1279 err += w9968cf_write_sb(cam, 0x0020);
1281 data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
1282 data[0] |= (address & 0x40) ? 0x4000 : 0x0;
1283 data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
1284 data[1] |= (address & 0x20) ? 0x0150 : 0x0;
1285 data[1] |= (address & 0x10) ? 0x5400 : 0x0;
1286 data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
1287 data[2] |= (address & 0x04) ? 0x0540 : 0x0;
1288 data[2] |= (address & 0x02) ? 0x5000 : 0x0;
1289 data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
1290 data[3] |= (address & 0x01) ? 0x0054 : 0x0;
1292 err += w9968cf_write_fsb(cam, data);
1294 data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
1295 data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
1296 data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
1297 data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
1298 data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
1299 data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
1300 data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
1301 data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
1302 data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
1303 data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
1306 err += w9968cf_write_fsb(cam, data);
1308 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
1309 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
1310 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
1311 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
1312 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
1313 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
1314 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
1315 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
1316 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
1317 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
1320 err += w9968cf_write_fsb(cam, data);
1322 /* Disable SBUS outputs */
1323 err += w9968cf_write_sb(cam, 0x0000);
1326 DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
1327 "value 0x%02X", address, subaddress, value)
1329 DBG(5, "I2C write byte data failed, addr.0x%04X, "
1330 "subaddr.0x%02X, value 0x%02X",
1331 address, subaddress, value)
1337 /* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
1339 w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
1340 u16 address, u8 subaddress,
1345 /* Serial data enable */
1346 err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
1348 err += w9968cf_smbus_start(cam);
1349 err += w9968cf_smbus_write_byte(cam, address);
1350 err += w9968cf_smbus_read_ack(cam);
1351 err += w9968cf_smbus_write_byte(cam, subaddress);
1352 err += w9968cf_smbus_read_ack(cam);
1353 err += w9968cf_smbus_stop(cam);
1354 err += w9968cf_smbus_start(cam);
1355 err += w9968cf_smbus_write_byte(cam, address + 1);
1356 err += w9968cf_smbus_read_ack(cam);
1357 err += w9968cf_smbus_read_byte(cam, value);
1358 err += w9968cf_smbus_write_ack(cam);
1359 err += w9968cf_smbus_stop(cam);
1361 /* Serial data disable */
1362 err += w9968cf_write_sb(cam, 0x0000);
1365 DBG(5, "I2C read byte data done, addr.0x%04X, "
1366 "subaddr.0x%02X, value 0x%02X",
1367 address, subaddress, *value)
1369 DBG(5, "I2C read byte data failed, addr.0x%04X, "
1370 "subaddr.0x%02X, wrong value 0x%02X",
1371 address, subaddress, *value)
1377 /* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
1379 w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
1380 u16 address, u8* value)
1384 /* Serial data enable */
1385 err += w9968cf_write_sb(cam, 0x0013);
1387 err += w9968cf_smbus_start(cam);
1388 err += w9968cf_smbus_write_byte(cam, address + 1);
1389 err += w9968cf_smbus_read_ack(cam);
1390 err += w9968cf_smbus_read_byte(cam, value);
1391 err += w9968cf_smbus_write_ack(cam);
1392 err += w9968cf_smbus_stop(cam);
1394 /* Serial data disable */
1395 err += w9968cf_write_sb(cam, 0x0000);
1398 DBG(5, "I2C read byte done, addr.0x%04X, "
1399 "value 0x%02X", address, *value)
1401 DBG(5, "I2C read byte failed, addr.0x%04X, "
1402 "wrong value 0x%02X", address, *value)
1408 /* SMBus protocol: S Addr Wr [A] Value [A] P */
1410 w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
1411 u16 address, u8 value)
1413 DBG(4, "i2c_write_byte() is an unsupported transfer mode")
1419 /****************************************************************************
1420 * I2C interface to kernel *
1421 ****************************************************************************/
1424 w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1425 unsigned short flags, char read_write, u8 command,
1426 int size, union i2c_smbus_data *data)
1428 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
1429 struct w9968cf_device *cam = to_cam(v4l2_dev);
1433 if (size == I2C_SMBUS_BYTE) {
1434 /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
1437 if (read_write == I2C_SMBUS_WRITE)
1438 err = w9968cf_i2c_adap_write_byte(cam, addr, command);
1439 else if (read_write == I2C_SMBUS_READ)
1440 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1441 err = w9968cf_i2c_adap_read_byte(cam, addr,
1444 if (w9968cf_smbus_refresh_bus(cam)) {
1451 } else if (size == I2C_SMBUS_BYTE_DATA) {
1454 if (read_write == I2C_SMBUS_WRITE)
1455 err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
1456 command, data->byte);
1457 else if (read_write == I2C_SMBUS_READ) {
1458 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1459 err = w9968cf_i2c_adap_read_byte_data(cam,addr,
1460 command, &data->byte);
1462 if (w9968cf_smbus_refresh_bus(cam)) {
1474 DBG(4, "Unsupported I2C transfer mode (%d)", size)
1481 static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
1483 return I2C_FUNC_SMBUS_READ_BYTE |
1484 I2C_FUNC_SMBUS_READ_BYTE_DATA |
1485 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
1489 static int w9968cf_i2c_init(struct w9968cf_device* cam)
1493 static struct i2c_algorithm algo = {
1494 .smbus_xfer = w9968cf_i2c_smbus_xfer,
1495 .functionality = w9968cf_i2c_func,
1498 static struct i2c_adapter adap = {
1499 .id = I2C_HW_SMBUS_W9968CF,
1500 .owner = THIS_MODULE,
1504 memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
1505 strcpy(cam->i2c_adapter.name, "w9968cf");
1506 cam->i2c_adapter.dev.parent = &cam->usbdev->dev;
1507 i2c_set_adapdata(&cam->i2c_adapter, &cam->v4l2_dev);
1509 DBG(6, "Registering I2C adapter with kernel...")
1511 err = i2c_add_adapter(&cam->i2c_adapter);
1513 DBG(1, "Failed to register the I2C adapter")
1515 DBG(5, "I2C adapter registered")
1522 /****************************************************************************
1523 * Helper functions *
1524 ****************************************************************************/
1526 /*--------------------------------------------------------------------------
1527 Turn on the LED on some webcams. A beep should be heard too.
1528 Return 0 on success, a negative number otherwise.
1529 --------------------------------------------------------------------------*/
1530 static int w9968cf_turn_on_led(struct w9968cf_device* cam)
1534 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power-down */
1535 err += w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
1536 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
1537 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* serial bus, SDS high */
1538 err += w9968cf_write_reg(cam, 0x0000, 0x01); /* serial bus, SDS low */
1539 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* ..high 'beep-beep' */
1542 DBG(2, "Couldn't turn on the LED")
1544 DBG(5, "LED turned on")
1550 /*--------------------------------------------------------------------------
1551 Write some registers for the device initialization.
1552 This function is called once on open().
1553 Return 0 on success, a negative number otherwise.
1554 --------------------------------------------------------------------------*/
1555 static int w9968cf_init_chip(struct w9968cf_device* cam)
1557 unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
1559 u0 = y0 + hw_bufsize/2,
1560 v0 = u0 + hw_bufsize/4,
1561 y1 = v0 + hw_bufsize/4,
1562 u1 = y1 + hw_bufsize/2,
1563 v1 = u1 + hw_bufsize/4;
1566 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
1567 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
1569 err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
1570 err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
1572 err += w9968cf_write_reg(cam, y0 & 0xffff, 0x20); /* Y buf.0, low */
1573 err += w9968cf_write_reg(cam, y0 >> 16, 0x21); /* Y buf.0, high */
1574 err += w9968cf_write_reg(cam, u0 & 0xffff, 0x24); /* U buf.0, low */
1575 err += w9968cf_write_reg(cam, u0 >> 16, 0x25); /* U buf.0, high */
1576 err += w9968cf_write_reg(cam, v0 & 0xffff, 0x28); /* V buf.0, low */
1577 err += w9968cf_write_reg(cam, v0 >> 16, 0x29); /* V buf.0, high */
1579 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x22); /* Y buf.1, low */
1580 err += w9968cf_write_reg(cam, y1 >> 16, 0x23); /* Y buf.1, high */
1581 err += w9968cf_write_reg(cam, u1 & 0xffff, 0x26); /* U buf.1, low */
1582 err += w9968cf_write_reg(cam, u1 >> 16, 0x27); /* U buf.1, high */
1583 err += w9968cf_write_reg(cam, v1 & 0xffff, 0x2a); /* V buf.1, low */
1584 err += w9968cf_write_reg(cam, v1 >> 16, 0x2b); /* V buf.1, high */
1586 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x32); /* JPEG buf 0 low */
1587 err += w9968cf_write_reg(cam, y1 >> 16, 0x33); /* JPEG buf 0 high */
1589 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x34); /* JPEG buf 1 low */
1590 err += w9968cf_write_reg(cam, y1 >> 16, 0x35); /* JPEG bug 1 high */
1592 err += w9968cf_write_reg(cam, 0x0000, 0x36);/* JPEG restart interval */
1593 err += w9968cf_write_reg(cam, 0x0804, 0x37);/*JPEG VLE FIFO threshold*/
1594 err += w9968cf_write_reg(cam, 0x0000, 0x38);/* disable hw up-scaling */
1595 err += w9968cf_write_reg(cam, 0x0000, 0x3f); /* JPEG/MCTL test data */
1597 err += w9968cf_set_picture(cam, cam->picture); /* this before */
1598 err += w9968cf_set_window(cam, cam->window);
1601 DBG(1, "Chip initialization failed")
1603 DBG(5, "Chip successfully initialized")
1609 /*--------------------------------------------------------------------------
1610 Return non-zero if the palette is supported, 0 otherwise.
1611 --------------------------------------------------------------------------*/
1612 static inline u16 w9968cf_valid_palette(u16 palette)
1615 while (w9968cf_formatlist[i].palette != 0) {
1616 if (palette == w9968cf_formatlist[i].palette)
1624 /*--------------------------------------------------------------------------
1625 Return the depth corresponding to the given palette.
1626 Palette _must_ be supported !
1627 --------------------------------------------------------------------------*/
1628 static inline u16 w9968cf_valid_depth(u16 palette)
1631 while (w9968cf_formatlist[i].palette != palette)
1634 return w9968cf_formatlist[i].depth;
1638 /*--------------------------------------------------------------------------
1639 Return non-zero if the format requires decompression, 0 otherwise.
1640 --------------------------------------------------------------------------*/
1641 static inline u8 w9968cf_need_decompression(u16 palette)
1644 while (w9968cf_formatlist[i].palette != 0) {
1645 if (palette == w9968cf_formatlist[i].palette)
1646 return w9968cf_formatlist[i].compression;
1653 /*--------------------------------------------------------------------------
1654 Change the picture settings of the camera.
1655 Return 0 on success, a negative number otherwise.
1656 --------------------------------------------------------------------------*/
1658 w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
1660 u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
1663 /* Make sure we are using a valid depth */
1664 pict.depth = w9968cf_valid_depth(pict.palette);
1668 hw_depth = pict.depth; /* depth used by the winbond chip */
1669 hw_palette = pict.palette; /* palette used by the winbond chip */
1671 /* VS & HS polarities */
1672 reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
1676 case VIDEO_PALETTE_UYVY:
1678 cam->vpp_flag = VPP_NONE;
1680 case VIDEO_PALETTE_YUV422P:
1682 cam->vpp_flag = VPP_DECOMPRESSION;
1684 case VIDEO_PALETTE_YUV420:
1685 case VIDEO_PALETTE_YUV420P:
1687 cam->vpp_flag = VPP_DECOMPRESSION;
1689 case VIDEO_PALETTE_YUYV:
1690 case VIDEO_PALETTE_YUV422:
1692 cam->vpp_flag = VPP_SWAP_YUV_BYTES;
1693 hw_palette = VIDEO_PALETTE_UYVY;
1695 /* Original video is used instead of RGBX palettes.
1696 Software conversion later. */
1697 case VIDEO_PALETTE_GREY:
1698 case VIDEO_PALETTE_RGB555:
1699 case VIDEO_PALETTE_RGB565:
1700 case VIDEO_PALETTE_RGB24:
1701 case VIDEO_PALETTE_RGB32:
1702 reg_v |= 0x0000; /* UYVY 16 bit is used */
1704 hw_palette = VIDEO_PALETTE_UYVY;
1705 cam->vpp_flag = VPP_UYVY_TO_RGBX;
1709 /* NOTE: due to memory issues, it is better to disable the hardware
1710 double buffering during compression */
1711 if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
1717 if (cam->filter_type == 1)
1719 else if (cam->filter_type == 2)
1722 if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
1725 if ((err = w9968cf_sensor_update_picture(cam, pict)))
1728 /* If all went well, update the device data structure */
1729 memcpy(&cam->picture, &pict, sizeof(pict));
1730 cam->hw_depth = hw_depth;
1731 cam->hw_palette = hw_palette;
1733 /* Settings changed, so we clear the frame buffers */
1734 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1736 DBG(4, "Palette is %s, depth is %u bpp",
1737 symbolic(v4l1_plist, pict.palette), pict.depth)
1742 DBG(1, "Failed to change picture settings")
1747 /*--------------------------------------------------------------------------
1748 Change the capture area size of the camera.
1749 This function _must_ be called _after_ w9968cf_set_picture().
1750 Return 0 on success, a negative number otherwise.
1751 --------------------------------------------------------------------------*/
1753 w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
1755 u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
1756 unsigned long fw, fh;
1757 struct ovcamchip_window s_win;
1760 /* Work around to avoid FP arithmetics */
1761 #define SC(x) ((x) << 10)
1762 #define UNSC(x) ((x) >> 10)
1764 /* Make sure we are using a supported resolution */
1765 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
1766 (u16*)&win.height)))
1769 /* Scaling factors */
1770 fw = SC(win.width) / cam->maxwidth;
1771 fh = SC(win.height) / cam->maxheight;
1773 /* Set up the width and height values used by the chip */
1774 if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
1775 cam->vpp_flag |= VPP_UPSCALE;
1776 /* Calculate largest w,h mantaining the same w/h ratio */
1777 w = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1778 h = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
1779 if (w < cam->minwidth) /* just in case */
1781 if (h < cam->minheight) /* just in case */
1784 cam->vpp_flag &= ~VPP_UPSCALE;
1789 /* x,y offsets of the cropped area */
1790 scx = cam->start_cropx;
1791 scy = cam->start_cropy;
1793 /* Calculate cropped area manteining the right w/h ratio */
1794 if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
1795 cw = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1796 ch = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
1802 /* Setup the window of the sensor */
1803 s_win.format = VIDEO_PALETTE_UYVY;
1804 s_win.width = cam->maxwidth;
1805 s_win.height = cam->maxheight;
1806 s_win.quarter = 0; /* full progressive video */
1809 s_win.x = (s_win.width - cw) / 2;
1810 s_win.y = (s_win.height - ch) / 2;
1813 if (cam->clockdiv >= 0)
1814 s_win.clockdiv = cam->clockdiv; /* manual override */
1816 switch (cam->sensor) {
1829 s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
1832 /* We have to scale win.x and win.y offsets */
1833 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1834 || (cam->vpp_flag & VPP_UPSCALE) ) {
1842 if ((ax + cw) > cam->maxwidth)
1843 ax = cam->maxwidth - cw;
1845 if ((ay + ch) > cam->maxheight)
1846 ay = cam->maxheight - ch;
1848 /* Adjust win.x, win.y */
1849 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1850 || (cam->vpp_flag & VPP_UPSCALE) ) {
1851 win.x = UNSC(ax*fw);
1852 win.y = UNSC(ay*fh);
1858 /* Offsets used by the chip */
1863 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
1866 err += w9968cf_write_reg(cam, scx + x, 0x10);
1867 err += w9968cf_write_reg(cam, scy + y, 0x11);
1868 err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
1869 err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
1870 err += w9968cf_write_reg(cam, w, 0x14);
1871 err += w9968cf_write_reg(cam, h, 0x15);
1873 /* JPEG width & height */
1874 err += w9968cf_write_reg(cam, w, 0x30);
1875 err += w9968cf_write_reg(cam, h, 0x31);
1877 /* Y & UV frame buffer strides (in WORD) */
1878 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1879 err += w9968cf_write_reg(cam, w/2, 0x2c);
1880 err += w9968cf_write_reg(cam, w/4, 0x2d);
1882 err += w9968cf_write_reg(cam, w, 0x2c);
1887 /* If all went well, update the device data structure */
1888 memcpy(&cam->window, &win, sizeof(win));
1892 /* Settings changed, so we clear the frame buffers */
1893 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1895 DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
1896 win.width, win.height, win.x, win.y)
1898 PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
1899 "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
1900 x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
1901 win.width, win.height)
1906 DBG(1, "Failed to change the capture area size")
1911 /*--------------------------------------------------------------------------
1912 Adjust the asked values for window width and height.
1913 Return 0 on success, -1 otherwise.
1914 --------------------------------------------------------------------------*/
1916 w9968cf_adjust_window_size(struct w9968cf_device* cam, u16* width, u16* height)
1920 if ((*width < cam->minwidth) || (*height < cam->minheight))
1923 maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1924 w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
1926 maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1927 w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
1935 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1936 *width &= ~15L; /* multiple of 16 */
1940 PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
1946 /*--------------------------------------------------------------------------
1947 Initialize the FIFO list of requested frames.
1948 --------------------------------------------------------------------------*/
1949 static void w9968cf_init_framelist(struct w9968cf_device* cam)
1953 for (i = 0; i < cam->nbuffers; i++) {
1954 cam->requested_frame[i] = NULL;
1955 cam->frame[i].queued = 0;
1956 cam->frame[i].status = F_UNUSED;
1961 /*--------------------------------------------------------------------------
1962 Add a frame in the FIFO list of requested frames.
1963 This function is called in process context.
1964 --------------------------------------------------------------------------*/
1965 static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
1968 unsigned long lock_flags;
1970 spin_lock_irqsave(&cam->flist_lock, lock_flags);
1972 for (f=0; cam->requested_frame[f] != NULL; f++);
1973 cam->requested_frame[f] = &cam->frame[f_num];
1974 cam->frame[f_num].queued = 1;
1975 cam->frame[f_num].status = F_UNUSED; /* clear the status */
1977 spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
1979 DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
1983 /*--------------------------------------------------------------------------
1984 Read, store and remove the first pointer in the FIFO list of requested
1985 frames. This function is called in interrupt context.
1986 --------------------------------------------------------------------------*/
1988 w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
1992 spin_lock(&cam->flist_lock);
1994 *framep = cam->requested_frame[0];
1996 /* Shift the list of pointers */
1997 for (i = 0; i < cam->nbuffers-1; i++)
1998 cam->requested_frame[i] = cam->requested_frame[i+1];
1999 cam->requested_frame[i] = NULL;
2001 spin_unlock(&cam->flist_lock);
2003 DBG(6,"Popped frame #%d from the list", (*framep)->number)
2007 /*--------------------------------------------------------------------------
2008 High-level video post-processing routine on grabbed frames.
2009 Return 0 on success, a negative number otherwise.
2010 --------------------------------------------------------------------------*/
2012 w9968cf_postprocess_frame(struct w9968cf_device* cam,
2013 struct w9968cf_frame_t* fr)
2015 void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
2016 u16 w = cam->window.width,
2017 h = cam->window.height,
2018 d = cam->picture.depth,
2019 fmt = cam->picture.palette,
2020 rgb = cam->force_rgb,
2021 hw_w = cam->hw_width,
2022 hw_h = cam->hw_height,
2023 hw_d = cam->hw_depth;
2026 #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
2028 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2029 memcpy(pOut, pIn, fr->length);
2031 err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
2032 PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
2033 fr->length = (hw_w*hw_h*hw_d)/8;
2036 DBG(4, "An error occurred while decoding the frame: "
2037 "%s", symbolic(decoder_errlist, err))
2040 DBG(6, "Frame decoded")
2043 if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
2044 w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
2045 DBG(6, "Original UYVY component ordering changed")
2048 if (cam->vpp_flag & VPP_UPSCALE) {
2049 w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
2050 fr->length = (w*h*hw_d)/8;
2052 DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
2053 hw_w, hw_h, hw_d, w, h)
2056 if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
2057 w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
2058 fr->length = (w*h*d)/8;
2060 DBG(6, "UYVY-16bit to %s conversion done",
2061 symbolic(v4l1_plist, fmt))
2064 if (pOut == fr->buffer)
2065 memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
2072 /****************************************************************************
2073 * Image sensor control routines *
2074 ****************************************************************************/
2077 w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
2079 struct ovcamchip_control ctl;
2085 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
2092 w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
2094 struct ovcamchip_control ctl;
2099 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
2108 w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
2112 rc = v4l2_subdev_call(cam->sensor_sd, core, ioctl, cmd, arg);
2113 /* The I2C driver returns -EPERM on non-supported controls */
2114 return (rc < 0 && rc != -EPERM) ? rc : 0;
2118 /*--------------------------------------------------------------------------
2119 Update some settings of the image sensor.
2120 Returns: 0 on success, a negative number otherwise.
2121 --------------------------------------------------------------------------*/
2122 static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
2126 /* Auto brightness */
2127 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
2133 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
2138 /* Banding filter */
2139 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
2144 /* Light frequency */
2145 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
2151 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
2157 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
2166 /*--------------------------------------------------------------------------
2167 Get some current picture settings from the image sensor and update the
2168 internal 'picture' structure of the camera.
2169 Returns: 0 on success, a negative number otherwise.
2170 --------------------------------------------------------------------------*/
2171 static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
2175 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
2178 cam->picture.contrast = v;
2180 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
2183 cam->picture.brightness = v;
2185 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
2188 cam->picture.colour = v;
2190 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
2193 cam->picture.hue = v;
2195 DBG(5, "Got picture settings from the image sensor")
2197 PDBGG("Brightness, contrast, hue, colour, whiteness are "
2198 "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
2199 cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
2205 /*--------------------------------------------------------------------------
2206 Update picture settings of the image sensor.
2207 Returns: 0 on success, a negative number otherwise.
2208 --------------------------------------------------------------------------*/
2210 w9968cf_sensor_update_picture(struct w9968cf_device* cam,
2211 struct video_picture pict)
2215 if ((!cam->sensor_initialized)
2216 || pict.contrast != cam->picture.contrast) {
2217 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
2221 DBG(4, "Contrast changed from %u to %u",
2222 cam->picture.contrast, pict.contrast)
2223 cam->picture.contrast = pict.contrast;
2226 if (((!cam->sensor_initialized) ||
2227 pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
2228 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
2232 DBG(4, "Brightness changed from %u to %u",
2233 cam->picture.brightness, pict.brightness)
2234 cam->picture.brightness = pict.brightness;
2237 if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
2238 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
2242 DBG(4, "Colour changed from %u to %u",
2243 cam->picture.colour, pict.colour)
2244 cam->picture.colour = pict.colour;
2247 if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
2248 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
2252 DBG(4, "Hue changed from %u to %u",
2253 cam->picture.hue, pict.hue)
2254 cam->picture.hue = pict.hue;
2260 DBG(4, "Failed to change sensor picture setting")
2266 /****************************************************************************
2267 * Camera configuration *
2268 ****************************************************************************/
2270 /*--------------------------------------------------------------------------
2271 This function is called when a supported image sensor is detected.
2272 Return 0 if the initialization succeeds, a negative number otherwise.
2273 --------------------------------------------------------------------------*/
2274 static int w9968cf_sensor_init(struct w9968cf_device* cam)
2278 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
2282 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
2286 /* NOTE: Make sure width and height are a multiple of 16 */
2287 switch (v4l2_i2c_subdev_addr(cam->sensor_sd)) {
2289 cam->maxwidth = 352;
2290 cam->maxheight = 288;
2292 cam->minheight = 48;
2295 cam->maxwidth = 640;
2296 cam->maxheight = 480;
2298 cam->minheight = 48;
2301 DBG(1, "Not supported image sensor detected for %s",
2302 symbolic(camlist, cam->id))
2306 /* These values depend on the ones in the ovxxx0.c sources */
2307 switch (cam->sensor) {
2309 cam->start_cropx = 287;
2310 cam->start_cropy = 35;
2311 /* Seems to work around a bug in the image sensor */
2312 cam->vs_polarity = 1;
2313 cam->hs_polarity = 1;
2316 cam->start_cropx = 320;
2317 cam->start_cropy = 35;
2318 cam->vs_polarity = 1;
2319 cam->hs_polarity = 0;
2322 if ((err = w9968cf_sensor_update_settings(cam)))
2325 if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
2328 cam->sensor_initialized = 1;
2330 DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
2334 cam->sensor_initialized = 0;
2335 cam->sensor = CC_UNKNOWN;
2336 DBG(1, "Image sensor initialization failed for %s (/dev/video%d). "
2337 "Try to detach and attach this device again",
2338 symbolic(camlist, cam->id), cam->v4ldev->num)
2343 /*--------------------------------------------------------------------------
2344 Fill some basic fields in the main device data structure.
2345 This function is called once on w9968cf_usb_probe() for each recognized
2347 --------------------------------------------------------------------------*/
2349 w9968cf_configure_camera(struct w9968cf_device* cam,
2350 struct usb_device* udev,
2351 enum w9968cf_model_id mod_id,
2352 const unsigned short dev_nr)
2354 mutex_init(&cam->fileop_mutex);
2355 init_waitqueue_head(&cam->open);
2356 spin_lock_init(&cam->urb_lock);
2357 spin_lock_init(&cam->flist_lock);
2360 cam->disconnected = 0;
2362 cam->sensor = CC_UNKNOWN;
2363 cam->sensor_initialized = 0;
2365 /* Calculate the alternate setting number (from 1 to 16)
2366 according to the 'packet_size' module parameter */
2367 if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
2368 packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
2369 for (cam->altsetting = 1;
2370 packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
2373 cam->max_buffers = (max_buffers[dev_nr] < 2 ||
2374 max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
2375 ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
2377 cam->double_buffer = (double_buffer[dev_nr] == 0 ||
2378 double_buffer[dev_nr] == 1)
2379 ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
2381 cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
2382 ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
2384 cam->filter_type = (filter_type[dev_nr] == 0 ||
2385 filter_type[dev_nr] == 1 ||
2386 filter_type[dev_nr] == 2)
2387 ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
2391 cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
2392 ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
2394 cam->decompression = (decompression[dev_nr] == 0 ||
2395 decompression[dev_nr] == 1 ||
2396 decompression[dev_nr] == 2)
2397 ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
2399 cam->upscaling = (upscaling[dev_nr] == 0 ||
2400 upscaling[dev_nr] == 1)
2401 ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
2403 cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
2404 ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
2406 cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
2407 ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
2409 cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
2410 ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
2412 cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
2413 bandingfilter[dev_nr] == 1)
2414 ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
2416 cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
2417 ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
2419 cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
2420 ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
2422 cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
2423 ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
2425 cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
2426 ? monochrome[dev_nr] : W9968CF_MONOCHROME;
2428 cam->picture.brightness = (u16)brightness[dev_nr];
2429 cam->picture.hue = (u16)hue[dev_nr];
2430 cam->picture.colour = (u16)colour[dev_nr];
2431 cam->picture.contrast = (u16)contrast[dev_nr];
2432 cam->picture.whiteness = (u16)whiteness[dev_nr];
2433 if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
2434 cam->picture.palette = (u16)force_palette[dev_nr];
2435 cam->force_palette = 1;
2437 cam->force_palette = 0;
2438 if (cam->decompression == 0)
2439 cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
2440 else if (cam->decompression == 1)
2441 cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
2443 cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
2445 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2447 cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
2448 ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
2452 cam->window.width = W9968CF_WIDTH;
2453 cam->window.height = W9968CF_HEIGHT;
2454 cam->window.chromakey = 0;
2455 cam->window.clipcount = 0;
2456 cam->window.flags = 0;
2458 DBG(3, "%s configured with settings #%u:",
2459 symbolic(camlist, cam->id), dev_nr)
2461 DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
2462 wMaxPacketSize[cam->altsetting-1])
2464 DBG(3, "- Number of requested video frame buffers: %u",
2467 if (cam->double_buffer)
2468 DBG(3, "- Hardware double buffering enabled")
2470 DBG(3, "- Hardware double buffering disabled")
2472 if (cam->filter_type == 0)
2473 DBG(3, "- Video filtering disabled")
2474 else if (cam->filter_type == 1)
2475 DBG(3, "- Video filtering enabled: type 1-2-1")
2476 else if (cam->filter_type == 2)
2477 DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
2480 DBG(3, "- Video data clamping (CCIR-601 format) enabled")
2482 DBG(3, "- Video data clamping (CCIR-601 format) disabled")
2485 DBG(3, "- Large view enabled")
2487 DBG(3, "- Large view disabled")
2489 if ((cam->decompression) == 0 && (!cam->force_palette))
2490 DBG(3, "- Decompression disabled")
2491 else if ((cam->decompression) == 1 && (!cam->force_palette))
2492 DBG(3, "- Decompression forced")
2493 else if ((cam->decompression) == 2 && (!cam->force_palette))
2494 DBG(3, "- Decompression allowed")
2497 DBG(3, "- Software image scaling enabled")
2499 DBG(3, "- Software image scaling disabled")
2501 if (cam->force_palette)
2502 DBG(3, "- Image palette forced to %s",
2503 symbolic(v4l1_plist, cam->picture.palette))
2506 DBG(3, "- RGB component ordering will be used instead of BGR")
2509 DBG(3, "- Auto brightness enabled")
2511 DBG(3, "- Auto brightness disabled")
2514 DBG(3, "- Auto exposure enabled")
2516 DBG(3, "- Auto exposure disabled")
2519 DBG(3, "- Backlight exposure algorithm enabled")
2521 DBG(3, "- Backlight exposure algorithm disabled")
2524 DBG(3, "- Mirror enabled")
2526 DBG(3, "- Mirror disabled")
2529 DBG(3, "- Banding filter enabled")
2531 DBG(3, "- Banding filter disabled")
2533 DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
2535 if (cam->clockdiv == -1)
2536 DBG(3, "- Automatic clock divisor enabled")
2538 DBG(3, "- Clock divisor: %d", cam->clockdiv)
2540 if (cam->monochrome)
2541 DBG(3, "- Image sensor used as monochrome")
2543 DBG(3, "- Image sensor not used as monochrome")
2547 /*--------------------------------------------------------------------------
2548 If the video post-processing module is not loaded, some parameters
2550 --------------------------------------------------------------------------*/
2551 static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2554 if (cam->decompression == 1) {
2555 cam->decompression = 2;
2556 DBG(2, "Video post-processing module not found: "
2557 "'decompression' parameter forced to 2")
2559 if (cam->upscaling) {
2561 DBG(2, "Video post-processing module not found: "
2562 "'upscaling' parameter forced to 0")
2564 if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
2565 cam->force_palette = 0;
2566 DBG(2, "Video post-processing module not found: "
2567 "'force_palette' parameter forced to 0")
2569 cam->picture.palette = VIDEO_PALETTE_UYVY;
2570 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2575 /*--------------------------------------------------------------------------
2576 Release the resources used by the driver.
2577 This function is called on disconnect
2578 (or on close if deallocation has been deferred)
2579 --------------------------------------------------------------------------*/
2580 static void w9968cf_release_resources(struct w9968cf_device* cam)
2582 mutex_lock(&w9968cf_devlist_mutex);
2584 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->num)
2586 video_unregister_device(cam->v4ldev);
2587 list_del(&cam->v4llist);
2588 i2c_del_adapter(&cam->i2c_adapter);
2589 w9968cf_deallocate_memory(cam);
2590 kfree(cam->control_buffer);
2591 kfree(cam->data_buffer);
2592 v4l2_device_unregister(&cam->v4l2_dev);
2594 mutex_unlock(&w9968cf_devlist_mutex);
2599 /****************************************************************************
2600 * Video4Linux interface *
2601 ****************************************************************************/
2603 static int w9968cf_open(struct file *filp)
2605 struct w9968cf_device* cam;
2608 /* This the only safe way to prevent race conditions with disconnect */
2609 if (!down_read_trylock(&w9968cf_disconnect))
2612 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2614 mutex_lock(&cam->dev_mutex);
2616 if (cam->sensor == CC_UNKNOWN) {
2617 DBG(2, "No supported image sensor has been detected by the "
2618 "'ovcamchip' module for the %s (/dev/video%d). Make "
2619 "sure it is loaded *before* (re)connecting the camera.",
2620 symbolic(camlist, cam->id), cam->v4ldev->num)
2621 mutex_unlock(&cam->dev_mutex);
2622 up_read(&w9968cf_disconnect);
2627 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
2628 symbolic(camlist, cam->id), cam->v4ldev->num, cam->command)
2629 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
2630 mutex_unlock(&cam->dev_mutex);
2631 up_read(&w9968cf_disconnect);
2632 return -EWOULDBLOCK;
2634 mutex_unlock(&cam->dev_mutex);
2635 err = wait_event_interruptible_exclusive(cam->open,
2636 cam->disconnected ||
2639 up_read(&w9968cf_disconnect);
2642 if (cam->disconnected) {
2643 up_read(&w9968cf_disconnect);
2646 mutex_lock(&cam->dev_mutex);
2649 DBG(5, "Opening '%s', /dev/video%d ...",
2650 symbolic(camlist, cam->id), cam->v4ldev->num)
2653 cam->misconfigured = 0;
2655 w9968cf_adjust_configuration(cam);
2657 if ((err = w9968cf_allocate_memory(cam)))
2658 goto deallocate_memory;
2660 if ((err = w9968cf_init_chip(cam)))
2661 goto deallocate_memory;
2663 if ((err = w9968cf_start_transfer(cam)))
2664 goto deallocate_memory;
2666 filp->private_data = cam;
2669 strcpy(cam->command, current->comm);
2671 init_waitqueue_head(&cam->wait_queue);
2673 DBG(5, "Video device is open")
2675 mutex_unlock(&cam->dev_mutex);
2676 up_read(&w9968cf_disconnect);
2681 w9968cf_deallocate_memory(cam);
2682 DBG(2, "Failed to open the video device")
2683 mutex_unlock(&cam->dev_mutex);
2684 up_read(&w9968cf_disconnect);
2689 static int w9968cf_release(struct file *filp)
2691 struct w9968cf_device* cam;
2693 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2695 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
2697 w9968cf_stop_transfer(cam);
2699 if (cam->disconnected) {
2700 w9968cf_release_resources(cam);
2701 mutex_unlock(&cam->dev_mutex);
2707 w9968cf_deallocate_memory(cam);
2708 wake_up_interruptible_nr(&cam->open, 1);
2710 DBG(5, "Video device closed")
2711 mutex_unlock(&cam->dev_mutex);
2717 w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2719 struct w9968cf_device* cam;
2720 struct w9968cf_frame_t* fr;
2723 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2725 if (filp->f_flags & O_NONBLOCK)
2726 return -EWOULDBLOCK;
2728 if (mutex_lock_interruptible(&cam->fileop_mutex))
2729 return -ERESTARTSYS;
2731 if (cam->disconnected) {
2732 DBG(2, "Device not present")
2733 mutex_unlock(&cam->fileop_mutex);
2737 if (cam->misconfigured) {
2738 DBG(2, "The camera is misconfigured. Close and open it again.")
2739 mutex_unlock(&cam->fileop_mutex);
2743 if (!cam->frame[0].queued)
2744 w9968cf_push_frame(cam, 0);
2746 if (!cam->frame[1].queued)
2747 w9968cf_push_frame(cam, 1);
2749 err = wait_event_interruptible(cam->wait_queue,
2750 cam->frame[0].status == F_READY ||
2751 cam->frame[1].status == F_READY ||
2754 mutex_unlock(&cam->fileop_mutex);
2757 if (cam->disconnected) {
2758 mutex_unlock(&cam->fileop_mutex);
2762 fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
2765 w9968cf_postprocess_frame(cam, fr);
2767 if (count > fr->length)
2770 if (copy_to_user(buf, fr->buffer, count)) {
2771 fr->status = F_UNUSED;
2772 mutex_unlock(&cam->fileop_mutex);
2777 fr->status = F_UNUSED;
2779 DBG(5, "%zu bytes read", count)
2781 mutex_unlock(&cam->fileop_mutex);
2786 static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
2788 struct w9968cf_device* cam = (struct w9968cf_device*)
2789 video_get_drvdata(video_devdata(filp));
2790 unsigned long vsize = vma->vm_end - vma->vm_start,
2791 psize = cam->nbuffers * cam->frame[0].size,
2792 start = vma->vm_start,
2793 pos = (unsigned long)cam->frame[0].buffer,
2796 if (cam->disconnected) {
2797 DBG(2, "Device not present")
2801 if (cam->misconfigured) {
2802 DBG(2, "The camera is misconfigured. Close and open it again")
2806 PDBGG("mmapping %lu bytes...", vsize)
2808 if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
2812 page = vmalloc_to_pfn((void *)pos);
2813 if (remap_pfn_range(vma, start, page + vma->vm_pgoff,
2814 PAGE_SIZE, vma->vm_page_prot))
2821 DBG(5, "mmap method successfully called")
2827 w9968cf_ioctl(struct file *filp,
2828 unsigned int cmd, unsigned long arg)
2830 struct w9968cf_device* cam;
2833 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2835 if (mutex_lock_interruptible(&cam->fileop_mutex))
2836 return -ERESTARTSYS;
2838 if (cam->disconnected) {
2839 DBG(2, "Device not present")
2840 mutex_unlock(&cam->fileop_mutex);
2844 if (cam->misconfigured) {
2845 DBG(2, "The camera is misconfigured. Close and open it again.")
2846 mutex_unlock(&cam->fileop_mutex);
2850 err = w9968cf_v4l_ioctl(filp, cmd, (void __user *)arg);
2852 mutex_unlock(&cam->fileop_mutex);
2857 static long w9968cf_v4l_ioctl(struct file *filp,
2858 unsigned int cmd, void __user *arg)
2860 struct w9968cf_device* cam;
2861 const char* v4l1_ioctls[] = {
2862 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
2863 "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
2864 "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
2865 "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
2866 "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
2867 "GVBIFMT", "SVBIFMT"
2870 #define V4L1_IOCTL(cmd) \
2871 ((_IOC_NR((cmd)) < ARRAY_SIZE(v4l1_ioctls)) ? \
2872 v4l1_ioctls[_IOC_NR((cmd))] : "?")
2874 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2878 case VIDIOCGCAP: /* get video capability */
2880 struct video_capability cap = {
2881 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
2884 .minwidth = cam->minwidth,
2885 .minheight = cam->minheight,
2887 sprintf(cap.name, "W996[87]CF USB Camera #%d",
2889 cap.maxwidth = (cam->upscaling && w9968cf_vpp)
2890 ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2892 cap.maxheight = (cam->upscaling && w9968cf_vpp)
2893 ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2896 if (copy_to_user(arg, &cap, sizeof(cap)))
2899 DBG(5, "VIDIOCGCAP successfully called")
2903 case VIDIOCGCHAN: /* get video channel informations */
2905 struct video_channel chan;
2906 if (copy_from_user(&chan, arg, sizeof(chan)))
2909 if (chan.channel != 0)
2912 strcpy(chan.name, "Camera");
2915 chan.type = VIDEO_TYPE_CAMERA;
2916 chan.norm = VIDEO_MODE_AUTO;
2918 if (copy_to_user(arg, &chan, sizeof(chan)))
2921 DBG(5, "VIDIOCGCHAN successfully called")
2925 case VIDIOCSCHAN: /* set active channel */
2927 struct video_channel chan;
2929 if (copy_from_user(&chan, arg, sizeof(chan)))
2932 if (chan.channel != 0)
2935 DBG(5, "VIDIOCSCHAN successfully called")
2939 case VIDIOCGPICT: /* get image properties of the picture */
2941 if (w9968cf_sensor_get_picture(cam))
2944 if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
2947 DBG(5, "VIDIOCGPICT successfully called")
2951 case VIDIOCSPICT: /* change picture settings */
2953 struct video_picture pict;
2956 if (copy_from_user(&pict, arg, sizeof(pict)))
2959 if ( (cam->force_palette || !w9968cf_vpp)
2960 && pict.palette != cam->picture.palette ) {
2961 DBG(4, "Palette %s rejected: only %s is allowed",
2962 symbolic(v4l1_plist, pict.palette),
2963 symbolic(v4l1_plist, cam->picture.palette))
2967 if (!w9968cf_valid_palette(pict.palette)) {
2968 DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
2969 symbolic(v4l1_plist, pict.palette))
2973 if (!cam->force_palette) {
2974 if (cam->decompression == 0) {
2975 if (w9968cf_need_decompression(pict.palette)) {
2976 DBG(4, "Decompression disabled: palette %s is not "
2977 "allowed. VIDIOCSPICT failed",
2978 symbolic(v4l1_plist, pict.palette))
2981 } else if (cam->decompression == 1) {
2982 if (!w9968cf_need_decompression(pict.palette)) {
2983 DBG(4, "Decompression forced: palette %s is not "
2984 "allowed. VIDIOCSPICT failed",
2985 symbolic(v4l1_plist, pict.palette))
2991 if (pict.depth != w9968cf_valid_depth(pict.palette)) {
2992 DBG(4, "Requested depth %u bpp is not valid for %s "
2993 "palette: ignored and changed to %u bpp",
2994 pict.depth, symbolic(v4l1_plist, pict.palette),
2995 w9968cf_valid_depth(pict.palette))
2996 pict.depth = w9968cf_valid_depth(pict.palette);
2999 if (pict.palette != cam->picture.palette) {
3000 if(*cam->requested_frame
3001 || cam->frame_current->queued) {
3002 err = wait_event_interruptible
3004 cam->disconnected ||
3005 (!*cam->requested_frame &&
3006 !cam->frame_current->queued) );
3009 if (cam->disconnected)
3013 if (w9968cf_stop_transfer(cam))
3016 if (w9968cf_set_picture(cam, pict))
3019 if (w9968cf_start_transfer(cam))
3022 } else if (w9968cf_sensor_update_picture(cam, pict))
3026 DBG(5, "VIDIOCSPICT successfully called")
3030 case VIDIOCSWIN: /* set capture area */
3032 struct video_window win;
3035 if (copy_from_user(&win, arg, sizeof(win)))
3038 DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
3039 "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
3040 win.x, win.y, win.width, win.height)
3042 if (win.clipcount != 0 || win.flags != 0)
3045 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
3046 (u16*)&win.height))) {
3047 DBG(4, "Resolution not supported (%ux%u). "
3048 "VIDIOCSWIN failed", win.width, win.height)
3052 if (win.x != cam->window.x ||
3053 win.y != cam->window.y ||
3054 win.width != cam->window.width ||
3055 win.height != cam->window.height) {
3056 if(*cam->requested_frame
3057 || cam->frame_current->queued) {
3058 err = wait_event_interruptible
3060 cam->disconnected ||
3061 (!*cam->requested_frame &&
3062 !cam->frame_current->queued) );
3065 if (cam->disconnected)
3069 if (w9968cf_stop_transfer(cam))
3072 /* This _must_ be called before set_window() */
3073 if (w9968cf_set_picture(cam, cam->picture))
3076 if (w9968cf_set_window(cam, win))
3079 if (w9968cf_start_transfer(cam))
3083 DBG(5, "VIDIOCSWIN successfully called. ")
3087 case VIDIOCGWIN: /* get current window properties */
3089 if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
3092 DBG(5, "VIDIOCGWIN successfully called")
3096 case VIDIOCGMBUF: /* request for memory (mapped) buffer */
3098 struct video_mbuf mbuf;
3101 mbuf.size = cam->nbuffers * cam->frame[0].size;
3102 mbuf.frames = cam->nbuffers;
3103 for (i = 0; i < cam->nbuffers; i++)
3104 mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
3105 (unsigned long)cam->frame[0].buffer;
3107 if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
3110 DBG(5, "VIDIOCGMBUF successfully called")
3114 case VIDIOCMCAPTURE: /* start the capture to a frame */
3116 struct video_mmap mmap;
3117 struct w9968cf_frame_t* fr;
3120 if (copy_from_user(&mmap, arg, sizeof(mmap)))
3123 DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
3124 mmap.frame, symbolic(v4l1_plist, mmap.format),
3125 mmap.width, mmap.height)
3127 if (mmap.frame >= cam->nbuffers) {
3128 DBG(4, "Invalid frame number (%u). "
3129 "VIDIOCMCAPTURE failed", mmap.frame)
3133 if (mmap.format!=cam->picture.palette &&
3134 (cam->force_palette || !w9968cf_vpp)) {
3135 DBG(4, "Palette %s rejected: only %s is allowed",
3136 symbolic(v4l1_plist, mmap.format),
3137 symbolic(v4l1_plist, cam->picture.palette))
3141 if (!w9968cf_valid_palette(mmap.format)) {
3142 DBG(4, "Palette %s not supported. "
3143 "VIDIOCMCAPTURE failed",
3144 symbolic(v4l1_plist, mmap.format))
3148 if (!cam->force_palette) {
3149 if (cam->decompression == 0) {
3150 if (w9968cf_need_decompression(mmap.format)) {
3151 DBG(4, "Decompression disabled: palette %s is not "
3152 "allowed. VIDIOCSPICT failed",
3153 symbolic(v4l1_plist, mmap.format))
3156 } else if (cam->decompression == 1) {
3157 if (!w9968cf_need_decompression(mmap.format)) {
3158 DBG(4, "Decompression forced: palette %s is not "
3159 "allowed. VIDIOCSPICT failed",
3160 symbolic(v4l1_plist, mmap.format))
3166 if ((err = w9968cf_adjust_window_size(cam, (u16*)&mmap.width,
3167 (u16*)&mmap.height))) {
3168 DBG(4, "Resolution not supported (%dx%d). "
3169 "VIDIOCMCAPTURE failed",
3170 mmap.width, mmap.height)
3174 fr = &cam->frame[mmap.frame];
3176 if (mmap.width != cam->window.width ||
3177 mmap.height != cam->window.height ||
3178 mmap.format != cam->picture.palette) {
3180 struct video_window win;
3181 struct video_picture pict;
3183 if(*cam->requested_frame
3184 || cam->frame_current->queued) {
3185 DBG(6, "VIDIOCMCAPTURE. Change settings for "
3186 "frame #%u: %dx%d, format %s. Wait...",
3187 mmap.frame, mmap.width, mmap.height,
3188 symbolic(v4l1_plist, mmap.format))
3189 err = wait_event_interruptible
3191 cam->disconnected ||
3192 (!*cam->requested_frame &&
3193 !cam->frame_current->queued) );
3196 if (cam->disconnected)
3200 memcpy(&win, &cam->window, sizeof(win));
3201 memcpy(&pict, &cam->picture, sizeof(pict));
3202 win.width = mmap.width;
3203 win.height = mmap.height;
3204 pict.palette = mmap.format;
3206 if (w9968cf_stop_transfer(cam))
3209 /* This before set_window */
3210 if (w9968cf_set_picture(cam, pict))
3213 if (w9968cf_set_window(cam, win))
3216 if (w9968cf_start_transfer(cam))
3219 } else if (fr->queued) {
3221 DBG(6, "Wait until frame #%u is free", mmap.frame)
3223 err = wait_event_interruptible(cam->wait_queue,
3224 cam->disconnected ||
3228 if (cam->disconnected)
3232 w9968cf_push_frame(cam, mmap.frame);
3233 DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
3237 case VIDIOCSYNC: /* wait until the capture of a frame is finished */
3240 struct w9968cf_frame_t* fr;
3243 if (copy_from_user(&f_num, arg, sizeof(f_num)))
3246 if (f_num >= cam->nbuffers) {
3247 DBG(4, "Invalid frame number (%u). "
3248 "VIDIOCMCAPTURE failed", f_num)
3252 DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
3254 fr = &cam->frame[f_num];
3256 switch (fr->status) {
3259 DBG(4, "VIDIOSYNC: Frame #%u not requested!",
3265 err = wait_event_interruptible(cam->wait_queue,
3266 (fr->status == F_READY)
3267 || cam->disconnected);
3270 if (cam->disconnected)
3278 w9968cf_postprocess_frame(cam, fr);
3280 fr->status = F_UNUSED;
3282 DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
3286 case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
3288 struct video_unit unit = {
3289 .video = cam->v4ldev->minor,
3290 .vbi = VIDEO_NO_UNIT,
3291 .radio = VIDEO_NO_UNIT,
3292 .audio = VIDEO_NO_UNIT,
3293 .teletext = VIDEO_NO_UNIT,
3296 if (copy_to_user(arg, &unit, sizeof(unit)))
3299 DBG(5, "VIDIOCGUNIT successfully called")
3308 if (clear_user(arg, sizeof(struct video_buffer)))
3311 DBG(5, "VIDIOCGFBUF successfully called")
3317 struct video_tuner tuner;
3318 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3321 if (tuner.tuner != 0)
3324 strcpy(tuner.name, "no_tuner");
3326 tuner.rangehigh = 0;
3327 tuner.flags = VIDEO_TUNER_NORM;
3328 tuner.mode = VIDEO_MODE_AUTO;
3329 tuner.signal = 0xffff;
3331 if (copy_to_user(arg, &tuner, sizeof(tuner)))
3334 DBG(5, "VIDIOCGTUNER successfully called")
3340 struct video_tuner tuner;
3341 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3344 if (tuner.tuner != 0)
3347 if (tuner.mode != VIDEO_MODE_AUTO)
3350 DBG(5, "VIDIOCSTUNER successfully called")
3360 case VIDIOCSPLAYMODE:
3361 case VIDIOCSWRITEMODE:
3362 case VIDIOCGPLAYINFO:
3363 case VIDIOCSMICROCODE:
3366 DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
3372 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3377 DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
3383 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3385 return -ENOIOCTLCMD;
3387 } /* end of switch */
3390 cam->misconfigured = 1;
3391 DBG(1, "VIDIOC%s failed because of hardware problems. "
3392 "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
3397 static const struct v4l2_file_operations w9968cf_fops = {
3398 .owner = THIS_MODULE,
3399 .open = w9968cf_open,
3400 .release = w9968cf_release,
3401 .read = w9968cf_read,
3402 .ioctl = w9968cf_ioctl,
3403 .mmap = w9968cf_mmap,
3408 /****************************************************************************
3409 * USB probe and V4L registration, disconnect and id_table[] definition *
3410 ****************************************************************************/
3413 w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3415 struct usb_device *udev = interface_to_usbdev(intf);
3416 struct w9968cf_device* cam;
3418 enum w9968cf_model_id mod_id;
3419 struct list_head* ptr;
3420 u8 sc = 0; /* number of simultaneous cameras */
3421 static unsigned short dev_nr; /* 0 - we are handling device number n */
3422 static unsigned short addrs[] = {
3428 if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[0].idVendor &&
3429 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[0].idProduct)
3430 mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
3431 else if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[1].idVendor &&
3432 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[1].idProduct)
3433 mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
3437 cam = (struct w9968cf_device*)
3438 kzalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
3442 err = v4l2_device_register(&udev->dev, &cam->v4l2_dev);
3446 mutex_init(&cam->dev_mutex);
3447 mutex_lock(&cam->dev_mutex);
3451 DBG(2, "%s detected", symbolic(camlist, mod_id))
3453 if (simcams > W9968CF_MAX_DEVICES)
3454 simcams = W9968CF_SIMCAMS;
3456 /* How many cameras are connected ? */
3457 mutex_lock(&w9968cf_devlist_mutex);
3458 list_for_each(ptr, &w9968cf_dev_list)
3460 mutex_unlock(&w9968cf_devlist_mutex);
3462 if (sc >= simcams) {
3463 DBG(2, "Device rejected: too many connected cameras "
3464 "(max. %u)", simcams)
3470 /* Allocate 2 bytes of memory for camera control USB transfers */
3471 if (!(cam->control_buffer = kzalloc(2, GFP_KERNEL))) {
3472 DBG(1,"Couldn't allocate memory for camera control transfers")
3477 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
3478 if (!(cam->data_buffer = kzalloc(8, GFP_KERNEL))) {
3479 DBG(1, "Couldn't allocate memory for data "
3480 "transfers to the FSB")
3485 /* Register the V4L device */
3486 cam->v4ldev = video_device_alloc();
3488 DBG(1, "Could not allocate memory for a V4L structure")
3493 strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
3494 cam->v4ldev->fops = &w9968cf_fops;
3495 cam->v4ldev->minor = video_nr[dev_nr];
3496 cam->v4ldev->release = video_device_release;
3497 video_set_drvdata(cam->v4ldev, cam);
3498 cam->v4ldev->v4l2_dev = &cam->v4l2_dev;
3500 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
3503 DBG(1, "V4L device registration failed")
3504 if (err == -ENFILE && video_nr[dev_nr] == -1)
3505 DBG(2, "Couldn't find a free /dev/videoX node")
3506 video_nr[dev_nr] = -1;
3507 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3511 DBG(2, "V4L device registered as /dev/video%d", cam->v4ldev->num)
3513 /* Set some basic constants */
3514 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3516 /* Add a new entry into the list of V4L registered devices */
3517 mutex_lock(&w9968cf_devlist_mutex);
3518 list_add(&cam->v4llist, &w9968cf_dev_list);
3519 mutex_unlock(&w9968cf_devlist_mutex);
3520 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3522 w9968cf_turn_on_led(cam);
3524 w9968cf_i2c_init(cam);
3525 cam->sensor_sd = v4l2_i2c_new_probed_subdev(&cam->i2c_adapter,
3526 "ovcamchip", "ovcamchip", addrs);
3528 usb_set_intfdata(intf, cam);
3529 mutex_unlock(&cam->dev_mutex);
3531 err = w9968cf_sensor_init(cam);
3534 fail: /* Free unused memory */
3535 kfree(cam->control_buffer);
3536 kfree(cam->data_buffer);
3538 video_device_release(cam->v4ldev);
3539 mutex_unlock(&cam->dev_mutex);
3540 v4l2_device_unregister(&cam->v4l2_dev);
3547 static void w9968cf_usb_disconnect(struct usb_interface* intf)
3549 struct w9968cf_device* cam =
3550 (struct w9968cf_device*)usb_get_intfdata(intf);
3553 down_write(&w9968cf_disconnect);
3554 /* Prevent concurrent accesses to data */
3555 mutex_lock(&cam->dev_mutex);
3557 cam->disconnected = 1;
3559 DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id))
3561 wake_up_interruptible_all(&cam->open);
3564 DBG(2, "The device is open (/dev/video%d)! "
3565 "Process name: %s. Deregistration and memory "
3566 "deallocation are deferred on close.",
3567 cam->v4ldev->num, cam->command)
3568 cam->misconfigured = 1;
3569 w9968cf_stop_transfer(cam);
3570 wake_up_interruptible(&cam->wait_queue);
3572 w9968cf_release_resources(cam);
3574 mutex_unlock(&cam->dev_mutex);
3575 up_write(&w9968cf_disconnect);
3584 static struct usb_driver w9968cf_usb_driver = {
3586 .id_table = winbond_id_table,
3587 .probe = w9968cf_usb_probe,
3588 .disconnect = w9968cf_usb_disconnect,
3593 /****************************************************************************
3594 * Module init, exit and intermodule communication *
3595 ****************************************************************************/
3597 static int __init w9968cf_module_init(void)
3601 KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
3602 KDBG(3, W9968CF_MODULE_AUTHOR)
3604 if ((err = usb_register(&w9968cf_usb_driver)))
3611 static void __exit w9968cf_module_exit(void)
3613 /* w9968cf_usb_disconnect() will be called */
3614 usb_deregister(&w9968cf_usb_driver);
3616 KDBG(2, W9968CF_MODULE_NAME" deregistered")
3620 module_init(w9968cf_module_init);
3621 module_exit(w9968cf_module_exit);