V4L/DVB (12540): v4l: simplify v4l2_i2c_new_subdev and friends
[deliverable/linux.git] / drivers / media / video / w9968cf.c
CommitLineData
1da177e4
LT
1/***************************************************************************
2 * Video4Linux driver for W996[87]CF JPEG USB Dual Mode Camera Chip. *
3 * *
4 * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
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. *
12 * *
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. *
17 * *
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. *
22 * *
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 ***************************************************************************/
27
1da177e4
LT
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/kmod.h>
31#include <linux/init.h>
32#include <linux/fs.h>
33#include <linux/vmalloc.h>
34#include <linux/slab.h>
35#include <linux/mm.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>
42#include <asm/page.h>
43#include <asm/uaccess.h>
44#include <linux/page-flags.h>
7e0a16f6 45#include <linux/videodev.h>
2864462e 46#include <media/v4l2-ioctl.h>
1da177e4
LT
47
48#include "w9968cf.h"
49#include "w9968cf_decoder.h"
50
4186ecf8
AV
51static struct w9968cf_vpp_t* w9968cf_vpp;
52static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
53
54static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
55static DEFINE_MUTEX(w9968cf_devlist_mutex); /* semaphore for list traversal */
56
57static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
1da177e4
LT
58
59
60/****************************************************************************
61 * Module macros and parameters *
62 ****************************************************************************/
63
64MODULE_DEVICE_TABLE(usb, winbond_id_table);
65
66MODULE_AUTHOR(W9968CF_MODULE_AUTHOR" "W9968CF_AUTHOR_EMAIL);
67MODULE_DESCRIPTION(W9968CF_MODULE_NAME);
68MODULE_VERSION(W9968CF_MODULE_VERSION);
69MODULE_LICENSE(W9968CF_MODULE_LICENSE);
70MODULE_SUPPORTED_DEVICE("Video");
71
1da177e4
LT
72static unsigned short simcams = W9968CF_SIMCAMS;
73static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
d56410e0
MCC
74static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
75 W9968CF_PACKET_SIZE};
76static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
77 W9968CF_BUFFERS};
78static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
79 W9968CF_DOUBLE_BUFFER};
1da177e4 80static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
d56410e0
MCC
81static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
82 W9968CF_FILTER_TYPE};
1da177e4 83static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
d56410e0
MCC
84static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
85 W9968CF_DECOMPRESSION};
1da177e4
LT
86static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
87static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
88static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
89static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
90static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
d56410e0
MCC
91static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
92 W9968CF_LIGHTFREQ};
1da177e4 93static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
d56410e0 94 W9968CF_BANDINGFILTER};
1da177e4
LT
95static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
96static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
97static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
98static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
d56410e0
MCC
99static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
100 W9968CF_BRIGHTNESS};
1da177e4
LT
101static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
102static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
d56410e0
MCC
103static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
104 W9968CF_CONTRAST};
105static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
106 W9968CF_WHITENESS};
1da177e4
LT
107#ifdef W9968CF_DEBUG
108static unsigned short debug = W9968CF_DEBUG_LEVEL;
109static int specific_debug = W9968CF_SPECIFIC_DEBUG;
110#endif
111
112static unsigned int param_nv[24]; /* number of values per parameter */
113
1da177e4
LT
114module_param(simcams, ushort, 0644);
115module_param_array(video_nr, short, &param_nv[0], 0444);
116module_param_array(packet_size, uint, &param_nv[1], 0444);
117module_param_array(max_buffers, ushort, &param_nv[2], 0444);
118module_param_array(double_buffer, bool, &param_nv[3], 0444);
119module_param_array(clamping, bool, &param_nv[4], 0444);
120module_param_array(filter_type, ushort, &param_nv[5], 0444);
121module_param_array(largeview, bool, &param_nv[6], 0444);
122module_param_array(decompression, ushort, &param_nv[7], 0444);
123module_param_array(upscaling, bool, &param_nv[8], 0444);
124module_param_array(force_palette, ushort, &param_nv[9], 0444);
125module_param_array(force_rgb, ushort, &param_nv[10], 0444);
126module_param_array(autobright, bool, &param_nv[11], 0444);
127module_param_array(autoexp, bool, &param_nv[12], 0444);
128module_param_array(lightfreq, ushort, &param_nv[13], 0444);
129module_param_array(bandingfilter, bool, &param_nv[14], 0444);
130module_param_array(clockdiv, short, &param_nv[15], 0444);
131module_param_array(backlight, bool, &param_nv[16], 0444);
132module_param_array(mirror, bool, &param_nv[17], 0444);
133module_param_array(monochrome, bool, &param_nv[18], 0444);
134module_param_array(brightness, uint, &param_nv[19], 0444);
135module_param_array(hue, uint, &param_nv[20], 0444);
136module_param_array(colour, uint, &param_nv[21], 0444);
137module_param_array(contrast, uint, &param_nv[22], 0444);
138module_param_array(whiteness, uint, &param_nv[23], 0444);
139#ifdef W9968CF_DEBUG
140module_param(debug, ushort, 0644);
141module_param(specific_debug, bool, 0644);
142#endif
143
d56410e0
MCC
144MODULE_PARM_DESC(simcams,
145 "\n<n> Number of cameras allowed to stream simultaneously."
146 "\nn may vary from 0 to "
147 __MODULE_STRING(W9968CF_MAX_DEVICES)"."
148 "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
149 "\n");
1da177e4 150MODULE_PARM_DESC(video_nr,
d56410e0
MCC
151 "\n<-1|n[,...]> Specify V4L minor mode number."
152 "\n -1 = use next available (default)"
153 "\n n = use minor number n (integer >= 0)"
154 "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
155 " cameras this way."
156 "\nFor example:"
157 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
158 "\nthe second camera and use auto for the first"
159 "\none and for every other camera."
160 "\n");
1da177e4 161MODULE_PARM_DESC(packet_size,
d56410e0
MCC
162 "\n<n[,...]> Specify the maximum data payload"
163 "\nsize in bytes for alternate settings, for each device."
164 "\nn is scaled between 63 and 1023 "
165 "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
166 "\n");
1da177e4 167MODULE_PARM_DESC(max_buffers,
d56410e0
MCC
168 "\n<n[,...]> For advanced users."
169 "\nSpecify the maximum number of video frame buffers"
170 "\nto allocate for each device, from 2 to "
171 __MODULE_STRING(W9968CF_MAX_BUFFERS)
172 ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
173 "\n");
174MODULE_PARM_DESC(double_buffer,
175 "\n<0|1[,...]> "
176 "Hardware double buffering: 0 disabled, 1 enabled."
177 "\nIt should be enabled if you want smooth video output: if"
178 "\nyou obtain out of sync. video, disable it, or try to"
179 "\ndecrease the 'clockdiv' module parameter value."
180 "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
181 " for every device."
182 "\n");
183MODULE_PARM_DESC(clamping,
184 "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
185 "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
186 " for every device."
187 "\n");
188MODULE_PARM_DESC(filter_type,
189 "\n<0|1|2[,...]> Video filter type."
190 "\n0 none, 1 (1-2-1) 3-tap filter, "
191 "2 (2-3-6-3-2) 5-tap filter."
192 "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
193 " for every device."
194 "\nThe filter is used to reduce noise and aliasing artifacts"
195 "\nproduced by the CCD or CMOS image sensor, and the scaling"
196 " process."
197 "\n");
198MODULE_PARM_DESC(largeview,
199 "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
200 "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
201 " for every device."
202 "\n");
203MODULE_PARM_DESC(upscaling,
204 "\n<0|1[,...]> Software scaling (for non-compressed video):"
205 "\n0 disabled, 1 enabled."
206 "\nDisable it if you have a slow CPU or you don't have"
207 " enough memory."
208 "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
209 " for every device."
210 "\nIf 'w9968cf-vpp' is not present, this parameter is"
211 " set to 0."
212 "\n");
1da177e4 213MODULE_PARM_DESC(decompression,
d56410e0
MCC
214 "\n<0|1|2[,...]> Software video decompression:"
215 "\n- 0 disables decompression (doesn't allow formats needing"
216 " decompression)"
217 "\n- 1 forces decompression (allows formats needing"
218 " decompression only);"
219 "\n- 2 allows any permitted formats."
220 "\nFormats supporting compressed video are YUV422P and"
221 " YUV420P/YUV420 "
222 "\nin any resolutions where both width and height are "
223 "a multiple of 16."
224 "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
225 " for every device."
226 "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
227 "\nnot allowed; in this case this parameter is set to 2."
228 "\n");
1da177e4 229MODULE_PARM_DESC(force_palette,
d56410e0
MCC
230 "\n<0"
231 "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
232 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
233 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
234 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
235 "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
236 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
237 "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
238 "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
239 "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
240 "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
241 "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
242 "[,...]>"
243 " Force picture palette."
244 "\nIn order:"
245 "\n- 0 allows any of the following formats:"
246 "\n- UYVY 16 bpp - Original video, compression disabled"
247 "\n- YUV420 12 bpp - Original video, compression enabled"
248 "\n- YUV422P 16 bpp - Original video, compression enabled"
249 "\n- YUV420P 12 bpp - Original video, compression enabled"
250 "\n- YUVY 16 bpp - Software conversion from UYVY"
251 "\n- YUV422 16 bpp - Software conversion from UYVY"
252 "\n- GREY 8 bpp - Software conversion from UYVY"
253 "\n- RGB555 16 bpp - Software conversion from UYVY"
254 "\n- RGB565 16 bpp - Software conversion from UYVY"
255 "\n- RGB24 24 bpp - Software conversion from UYVY"
256 "\n- RGB32 32 bpp - Software conversion from UYVY"
257 "\nWhen not 0, this parameter will override 'decompression'."
258 "\nDefault value is 0 for every device."
259 "\nInitial palette is "
260 __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
261 "\nIf 'w9968cf-vpp' is not present, this parameter is"
262 " set to 9 (UYVY)."
263 "\n");
264MODULE_PARM_DESC(force_rgb,
265 "\n<0|1[,...]> Read RGB video data instead of BGR:"
266 "\n 1 = use RGB component ordering."
267 "\n 0 = use BGR component ordering."
268 "\nThis parameter has effect when using RGBX palettes only."
269 "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
270 " for every device."
271 "\n");
1da177e4 272MODULE_PARM_DESC(autobright,
d56410e0
MCC
273 "\n<0|1[,...]> Image sensor automatically changes brightness:"
274 "\n 0 = no, 1 = yes"
275 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
276 " for every device."
277 "\n");
1da177e4 278MODULE_PARM_DESC(autoexp,
d56410e0
MCC
279 "\n<0|1[,...]> Image sensor automatically changes exposure:"
280 "\n 0 = no, 1 = yes"
281 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
282 " for every device."
283 "\n");
1da177e4 284MODULE_PARM_DESC(lightfreq,
d56410e0
MCC
285 "\n<50|60[,...]> Light frequency in Hz:"
286 "\n 50 for European and Asian lighting,"
287 " 60 for American lighting."
288 "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
289 " for every device."
290 "\n");
1da177e4 291MODULE_PARM_DESC(bandingfilter,
d56410e0
MCC
292 "\n<0|1[,...]> Banding filter to reduce effects of"
293 " fluorescent lighting:"
294 "\n 0 disabled, 1 enabled."
295 "\nThis filter tries to reduce the pattern of horizontal"
296 "\nlight/dark bands caused by some (usually fluorescent)"
297 " lighting."
298 "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
299 " for every device."
300 "\n");
1da177e4 301MODULE_PARM_DESC(clockdiv,
d56410e0
MCC
302 "\n<-1|n[,...]> "
303 "Force pixel clock divisor to a specific value (for experts):"
304 "\n n may vary from 0 to 127."
305 "\n -1 for automatic value."
306 "\nSee also the 'double_buffer' module parameter."
307 "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
308 " for every device."
309 "\n");
1da177e4 310MODULE_PARM_DESC(backlight,
d56410e0
MCC
311 "\n<0|1[,...]> Objects are lit from behind:"
312 "\n 0 = no, 1 = yes"
313 "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
314 " for every device."
315 "\n");
1da177e4 316MODULE_PARM_DESC(mirror,
d56410e0
MCC
317 "\n<0|1[,...]> Reverse image horizontally:"
318 "\n 0 = no, 1 = yes"
319 "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
320 " for every device."
321 "\n");
1da177e4 322MODULE_PARM_DESC(monochrome,
d56410e0
MCC
323 "\n<0|1[,...]> Use image sensor as monochrome sensor:"
324 "\n 0 = no, 1 = yes"
325 "\nNot all the sensors support monochrome color."
326 "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
327 " for every device."
328 "\n");
329MODULE_PARM_DESC(brightness,
330 "\n<n[,...]> Set picture brightness (0-65535)."
331 "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
332 " for every device."
333 "\nThis parameter has no effect if 'autobright' is enabled."
334 "\n");
335MODULE_PARM_DESC(hue,
336 "\n<n[,...]> Set picture hue (0-65535)."
337 "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
338 " for every device."
339 "\n");
340MODULE_PARM_DESC(colour,
341 "\n<n[,...]> Set picture saturation (0-65535)."
342 "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
343 " for every device."
344 "\n");
345MODULE_PARM_DESC(contrast,
346 "\n<n[,...]> Set picture contrast (0-65535)."
347 "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
348 " for every device."
349 "\n");
350MODULE_PARM_DESC(whiteness,
351 "\n<n[,...]> Set picture whiteness (0-65535)."
352 "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
353 " for every device."
354 "\n");
1da177e4
LT
355#ifdef W9968CF_DEBUG
356MODULE_PARM_DESC(debug,
d56410e0
MCC
357 "\n<n> Debugging information level, from 0 to 6:"
358 "\n0 = none (use carefully)"
359 "\n1 = critical errors"
360 "\n2 = significant informations"
361 "\n3 = configuration or general messages"
362 "\n4 = warnings"
363 "\n5 = called functions"
364 "\n6 = function internals"
365 "\nLevel 5 and 6 are useful for testing only, when only "
366 "one device is used."
367 "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
368 "\n");
1da177e4 369MODULE_PARM_DESC(specific_debug,
d56410e0
MCC
370 "\n<0|1> Enable or disable specific debugging messages:"
371 "\n0 = print messages concerning every level"
372 " <= 'debug' level."
373 "\n1 = print messages concerning the level"
374 " indicated by 'debug'."
375 "\nDefault value is "
376 __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
377 "\n");
1da177e4
LT
378#endif /* W9968CF_DEBUG */
379
380
381
382/****************************************************************************
383 * Some prototypes *
384 ****************************************************************************/
385
386/* Video4linux interface */
bec43661
HV
387static const struct v4l2_file_operations w9968cf_fops;
388static int w9968cf_open(struct file *);
389static int w9968cf_release(struct file *);
390static int w9968cf_mmap(struct file *, struct vm_area_struct *);
069b7479 391static long w9968cf_ioctl(struct file *, unsigned, unsigned long);
bec43661 392static ssize_t w9968cf_read(struct file *, char __user *, size_t, loff_t *);
069b7479 393static long w9968cf_v4l_ioctl(struct file *, unsigned int,
d56410e0 394 void __user *);
1da177e4
LT
395
396/* USB-specific */
397static int w9968cf_start_transfer(struct w9968cf_device*);
398static int w9968cf_stop_transfer(struct w9968cf_device*);
399static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
400static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
401static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
402static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
403static int w9968cf_read_sb(struct w9968cf_device*);
404static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
7d12e780 405static void w9968cf_urb_complete(struct urb *urb);
1da177e4
LT
406
407/* Low-level I2C (SMBus) I/O */
408static int w9968cf_smbus_start(struct w9968cf_device*);
409static int w9968cf_smbus_stop(struct w9968cf_device*);
410static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
411static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
412static int w9968cf_smbus_write_ack(struct w9968cf_device*);
413static int w9968cf_smbus_read_ack(struct w9968cf_device*);
414static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
415static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
d56410e0
MCC
416 u16 address, u8* value);
417static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
418 u8 subaddress, u8* value);
1da177e4 419static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
d56410e0 420 u16 address, u8 subaddress);
1da177e4 421static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
d56410e0
MCC
422 u16 address, u8 subaddress,
423 u8 value);
1da177e4
LT
424
425/* I2C interface to kernel */
426static int w9968cf_i2c_init(struct w9968cf_device*);
d56410e0
MCC
427static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
428 unsigned short flags, char read_write,
429 u8 command, int size, union i2c_smbus_data*);
1da177e4 430static u32 w9968cf_i2c_func(struct i2c_adapter*);
1da177e4
LT
431
432/* Memory management */
433static void* rvmalloc(unsigned long size);
434static void rvfree(void *mem, unsigned long size);
435static void w9968cf_deallocate_memory(struct w9968cf_device*);
436static int w9968cf_allocate_memory(struct w9968cf_device*);
437
438/* High-level image sensor control functions */
439static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
440static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
441static int w9968cf_sensor_cmd(struct w9968cf_device*,
d56410e0 442 unsigned int cmd, void *arg);
1da177e4
LT
443static int w9968cf_sensor_init(struct w9968cf_device*);
444static int w9968cf_sensor_update_settings(struct w9968cf_device*);
445static int w9968cf_sensor_get_picture(struct w9968cf_device*);
d56410e0
MCC
446static int w9968cf_sensor_update_picture(struct w9968cf_device*,
447 struct video_picture pict);
1da177e4
LT
448
449/* Other helper functions */
450static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
d56410e0
MCC
451 enum w9968cf_model_id,
452 const unsigned short dev_nr);
1da177e4
LT
453static void w9968cf_adjust_configuration(struct w9968cf_device*);
454static int w9968cf_turn_on_led(struct w9968cf_device*);
455static int w9968cf_init_chip(struct w9968cf_device*);
456static inline u16 w9968cf_valid_palette(u16 palette);
457static inline u16 w9968cf_valid_depth(u16 palette);
458static inline u8 w9968cf_need_decompression(u16 palette);
459static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
460static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
d56410e0
MCC
461static int w9968cf_postprocess_frame(struct w9968cf_device*,
462 struct w9968cf_frame_t*);
1c657a99 463static int w9968cf_adjust_window_size(struct w9968cf_device*, u32 *w, u32 *h);
1da177e4
LT
464static void w9968cf_init_framelist(struct w9968cf_device*);
465static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
466static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
467static void w9968cf_release_resources(struct w9968cf_device*);
468
1da177e4
LT
469
470
471/****************************************************************************
472 * Symbolic names *
473 ****************************************************************************/
474
475/* Used to represent a list of values and their respective symbolic names */
476struct w9968cf_symbolic_list {
477 const int num;
478 const char *name;
479};
480
d56410e0 481/*--------------------------------------------------------------------------
1da177e4
LT
482 Returns the name of the matching element in the symbolic_list array. The
483 end of the list must be marked with an element that has a NULL name.
484 --------------------------------------------------------------------------*/
d56410e0 485static inline const char *
1da177e4
LT
486symbolic(struct w9968cf_symbolic_list list[], const int num)
487{
488 int i;
489
490 for (i = 0; list[i].name != NULL; i++)
491 if (list[i].num == num)
492 return (list[i].name);
493
494 return "Unknown";
495}
496
497static struct w9968cf_symbolic_list camlist[] = {
498 { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
499 { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
500
501 /* Other cameras (having the same descriptors as Generic W996[87]CF) */
502 { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
503 { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
504 { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
505 { W9968CF_MOD_LL, "Lebon LDC-035A" },
506 { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
507 { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
508 { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
509 { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
510 { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
511
512 { -1, NULL }
513};
514
515static struct w9968cf_symbolic_list senlist[] = {
516 { CC_OV76BE, "OV76BE" },
517 { CC_OV7610, "OV7610" },
518 { CC_OV7620, "OV7620" },
519 { CC_OV7620AE, "OV7620AE" },
520 { CC_OV6620, "OV6620" },
521 { CC_OV6630, "OV6630" },
522 { CC_OV6630AE, "OV6630AE" },
523 { CC_OV6630AF, "OV6630AF" },
524 { -1, NULL }
525};
526
527/* Video4Linux1 palettes */
528static struct w9968cf_symbolic_list v4l1_plist[] = {
529 { VIDEO_PALETTE_GREY, "GREY" },
530 { VIDEO_PALETTE_HI240, "HI240" },
531 { VIDEO_PALETTE_RGB565, "RGB565" },
532 { VIDEO_PALETTE_RGB24, "RGB24" },
533 { VIDEO_PALETTE_RGB32, "RGB32" },
534 { VIDEO_PALETTE_RGB555, "RGB555" },
535 { VIDEO_PALETTE_YUV422, "YUV422" },
536 { VIDEO_PALETTE_YUYV, "YUYV" },
537 { VIDEO_PALETTE_UYVY, "UYVY" },
538 { VIDEO_PALETTE_YUV420, "YUV420" },
539 { VIDEO_PALETTE_YUV411, "YUV411" },
540 { VIDEO_PALETTE_RAW, "RAW" },
541 { VIDEO_PALETTE_YUV422P, "YUV422P" },
542 { VIDEO_PALETTE_YUV411P, "YUV411P" },
543 { VIDEO_PALETTE_YUV420P, "YUV420P" },
544 { VIDEO_PALETTE_YUV410P, "YUV410P" },
545 { -1, NULL }
546};
547
548/* Decoder error codes: */
549static struct w9968cf_symbolic_list decoder_errlist[] = {
550 { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
551 { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
d56410e0 552 { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
1da177e4
LT
553 { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
554 { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
555 { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
556 { -1, NULL }
557};
558
559/* URB error codes: */
560static struct w9968cf_symbolic_list urb_errlist[] = {
561 { -ENOMEM, "No memory for allocation of internal structures" },
562 { -ENOSPC, "The host controller's bandwidth is already consumed" },
563 { -ENOENT, "URB was canceled by unlink_urb" },
564 { -EXDEV, "ISO transfer only partially completed" },
565 { -EAGAIN, "Too match scheduled for the future" },
566 { -ENXIO, "URB already queued" },
567 { -EFBIG, "Too much ISO frames requested" },
568 { -ENOSR, "Buffer error (overrun)" },
569 { -EPIPE, "Specified endpoint is stalled (device not responding)"},
38e2bfc9 570 { -EOVERFLOW, "Babble (too much data)" },
1da177e4
LT
571 { -EPROTO, "Bit-stuff error (bad cable?)" },
572 { -EILSEQ, "CRC/Timeout" },
38e2bfc9
PZ
573 { -ETIME, "Device does not respond to token" },
574 { -ETIMEDOUT, "Device does not respond to command" },
1da177e4
LT
575 { -1, NULL }
576};
577
1da177e4
LT
578/****************************************************************************
579 * Memory management functions *
580 ****************************************************************************/
581static void* rvmalloc(unsigned long size)
582{
583 void* mem;
584 unsigned long adr;
585
586 size = PAGE_ALIGN(size);
587 mem = vmalloc_32(size);
588 if (!mem)
589 return NULL;
590
591 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
592 adr = (unsigned long) mem;
593 while (size > 0) {
594 SetPageReserved(vmalloc_to_page((void *)adr));
595 adr += PAGE_SIZE;
596 size -= PAGE_SIZE;
597 }
598
599 return mem;
600}
601
602
603static void rvfree(void* mem, unsigned long size)
604{
605 unsigned long adr;
606
607 if (!mem)
608 return;
609
610 adr = (unsigned long) mem;
611 while ((long) size > 0) {
612 ClearPageReserved(vmalloc_to_page((void *)adr));
613 adr += PAGE_SIZE;
614 size -= PAGE_SIZE;
615 }
616 vfree(mem);
617}
618
619
620/*--------------------------------------------------------------------------
621 Deallocate previously allocated memory.
622 --------------------------------------------------------------------------*/
623static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
624{
625 u8 i;
626
627 /* Free the isochronous transfer buffers */
628 for (i = 0; i < W9968CF_URBS; i++) {
629 kfree(cam->transfer_buffer[i]);
630 cam->transfer_buffer[i] = NULL;
631 }
632
633 /* Free temporary frame buffer */
634 if (cam->frame_tmp.buffer) {
635 rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
636 cam->frame_tmp.buffer = NULL;
637 }
638
639 /* Free helper buffer */
640 if (cam->frame_vpp.buffer) {
641 rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
642 cam->frame_vpp.buffer = NULL;
643 }
644
645 /* Free video frame buffers */
646 if (cam->frame[0].buffer) {
647 rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
648 cam->frame[0].buffer = NULL;
649 }
650
651 cam->nbuffers = 0;
652
653 DBG(5, "Memory successfully deallocated")
654}
655
656
657/*--------------------------------------------------------------------------
658 Allocate memory buffers for USB transfers and video frames.
659 This function is called by open() only.
660 Return 0 on success, a negative number otherwise.
661 --------------------------------------------------------------------------*/
662static int w9968cf_allocate_memory(struct w9968cf_device* cam)
663{
664 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
665 void* buff = NULL;
666 unsigned long hw_bufsize, vpp_bufsize;
667 u8 i, bpp;
668
669 /* NOTE: Deallocation is done elsewhere in case of error */
670
671 /* Calculate the max amount of raw data per frame from the device */
672 hw_bufsize = cam->maxwidth*cam->maxheight*2;
673
674 /* Calculate the max buf. size needed for post-processing routines */
675 bpp = (w9968cf_vpp) ? 4 : 2;
676 if (cam->upscaling)
677 vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
d56410e0 678 cam->maxwidth*cam->maxheight*bpp);
1da177e4
LT
679 else
680 vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
681
682 /* Allocate memory for the isochronous transfer buffers */
683 for (i = 0; i < W9968CF_URBS; i++) {
684 if (!(cam->transfer_buffer[i] =
b10b4177 685 kzalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
1da177e4 686 DBG(1, "Couldn't allocate memory for the isochronous "
d56410e0 687 "transfer buffers (%u bytes)",
1da177e4
LT
688 p_size * W9968CF_ISO_PACKETS)
689 return -ENOMEM;
690 }
1da177e4
LT
691 }
692
693 /* Allocate memory for the temporary frame buffer */
694 if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
695 DBG(1, "Couldn't allocate memory for the temporary "
696 "video frame buffer (%lu bytes)", hw_bufsize)
697 return -ENOMEM;
698 }
699 cam->frame_tmp.size = hw_bufsize;
700 cam->frame_tmp.number = -1;
701
702 /* Allocate memory for the helper buffer */
703 if (w9968cf_vpp) {
704 if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
705 DBG(1, "Couldn't allocate memory for the helper buffer"
706 " (%lu bytes)", vpp_bufsize)
707 return -ENOMEM;
708 }
709 cam->frame_vpp.size = vpp_bufsize;
710 } else
711 cam->frame_vpp.buffer = NULL;
712
713 /* Allocate memory for video frame buffers */
714 cam->nbuffers = cam->max_buffers;
715 while (cam->nbuffers >= 2) {
716 if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
717 break;
718 else
719 cam->nbuffers--;
720 }
721
722 if (!buff) {
723 DBG(1, "Couldn't allocate memory for the video frame buffers")
724 cam->nbuffers = 0;
725 return -ENOMEM;
726 }
727
728 if (cam->nbuffers != cam->max_buffers)
729 DBG(2, "Couldn't allocate memory for %u video frame buffers. "
730 "Only memory for %u buffers has been allocated",
731 cam->max_buffers, cam->nbuffers)
732
733 for (i = 0; i < cam->nbuffers; i++) {
734 cam->frame[i].buffer = buff + i*vpp_bufsize;
735 cam->frame[i].size = vpp_bufsize;
736 cam->frame[i].number = i;
737 /* Circular list */
738 if (i != cam->nbuffers-1)
739 cam->frame[i].next = &cam->frame[i+1];
740 else
741 cam->frame[i].next = &cam->frame[0];
742 cam->frame[i].status = F_UNUSED;
743 }
744
745 DBG(5, "Memory successfully allocated")
746 return 0;
747}
748
749
750
751/****************************************************************************
752 * USB-specific functions *
753 ****************************************************************************/
754
755/*--------------------------------------------------------------------------
756 This is an handler function which is called after the URBs are completed.
757 It collects multiple data packets coming from the camera by putting them
758 into frame buffers: one or more zero data length data packets are used to
759 mark the end of a video frame; the first non-zero data packet is the start
760 of the next video frame; if an error is encountered in a packet, the entire
761 video frame is discarded and grabbed again.
762 If there are no requested frames in the FIFO list, packets are collected into
d56410e0 763 a temporary buffer.
1da177e4 764 --------------------------------------------------------------------------*/
7d12e780 765static void w9968cf_urb_complete(struct urb *urb)
1da177e4
LT
766{
767 struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
768 struct w9968cf_frame_t** f;
769 unsigned int len, status;
770 void* pos;
771 u8 i;
772 int err = 0;
773
774 if ((!cam->streaming) || cam->disconnected) {
775 DBG(4, "Got interrupt, but not streaming")
776 return;
777 }
778
779 /* "(*f)" will be used instead of "cam->frame_current" */
780 f = &cam->frame_current;
781
d56410e0 782 /* If a frame has been requested and we are grabbing into
1da177e4
LT
783 the temporary frame, we'll switch to that requested frame */
784 if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
785 if (cam->frame_tmp.status == F_GRABBING) {
786 w9968cf_pop_frame(cam, &cam->frame_current);
787 (*f)->status = F_GRABBING;
788 (*f)->length = cam->frame_tmp.length;
789 memcpy((*f)->buffer, cam->frame_tmp.buffer,
790 (*f)->length);
d56410e0 791 DBG(6, "Switched from temp. frame to frame #%d",
1da177e4
LT
792 (*f)->number)
793 }
794 }
795
796 for (i = 0; i < urb->number_of_packets; i++) {
797 len = urb->iso_frame_desc[i].actual_length;
798 status = urb->iso_frame_desc[i].status;
799 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
800
801 if (status && len != 0) {
802 DBG(4, "URB failed, error in data packet "
803 "(error #%u, %s)",
804 status, symbolic(urb_errlist, status))
805 (*f)->status = F_ERROR;
806 continue;
807 }
808
809 if (len) { /* start of frame */
810
811 if ((*f)->status == F_UNUSED) {
812 (*f)->status = F_GRABBING;
813 (*f)->length = 0;
814 }
815
816 /* Buffer overflows shouldn't happen, however...*/
817 if ((*f)->length + len > (*f)->size) {
818 DBG(4, "Buffer overflow: bad data packets")
819 (*f)->status = F_ERROR;
820 }
821
822 if ((*f)->status == F_GRABBING) {
823 memcpy((*f)->buffer + (*f)->length, pos, len);
824 (*f)->length += len;
825 }
826
827 } else if ((*f)->status == F_GRABBING) { /* end of frame */
828
829 DBG(6, "Frame #%d successfully grabbed", (*f)->number)
830
831 if (cam->vpp_flag & VPP_DECOMPRESSION) {
832 err = w9968cf_vpp->check_headers((*f)->buffer,
d56410e0 833 (*f)->length);
1da177e4
LT
834 if (err) {
835 DBG(4, "Skip corrupted frame: %s",
836 symbolic(decoder_errlist, err))
837 (*f)->status = F_UNUSED;
838 continue; /* grab this frame again */
839 }
840 }
841
842 (*f)->status = F_READY;
843 (*f)->queued = 0;
844
845 /* Take a pointer to the new frame from the FIFO list.
846 If the list is empty,we'll use the temporary frame*/
847 if (*cam->requested_frame)
848 w9968cf_pop_frame(cam, &cam->frame_current);
849 else {
850 cam->frame_current = &cam->frame_tmp;
851 (*f)->status = F_UNUSED;
852 }
853
854 } else if ((*f)->status == F_ERROR)
855 (*f)->status = F_UNUSED; /* grab it again */
856
857 PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
858 (unsigned long)(*f)->length, i, len, (*f)->status)
859
860 } /* end for */
861
862 /* Resubmit this URB */
863 urb->dev = cam->usbdev;
864 urb->status = 0;
865 spin_lock(&cam->urb_lock);
866 if (cam->streaming)
867 if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
868 cam->misconfigured = 1;
869 DBG(1, "Couldn't resubmit the URB: error %d, %s",
870 err, symbolic(urb_errlist, err))
871 }
872 spin_unlock(&cam->urb_lock);
873
874 /* Wake up the user process */
875 wake_up_interruptible(&cam->wait_queue);
876}
877
878
879/*---------------------------------------------------------------------------
880 Setup the URB structures for the isochronous transfer.
881 Submit the URBs so that the data transfer begins.
882 Return 0 on success, a negative number otherwise.
883 ---------------------------------------------------------------------------*/
884static int w9968cf_start_transfer(struct w9968cf_device* cam)
885{
886 struct usb_device *udev = cam->usbdev;
887 struct urb* urb;
888 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
889 u16 w, h, d;
890 int vidcapt;
891 u32 t_size;
892 int err = 0;
893 s8 i, j;
894
895 for (i = 0; i < W9968CF_URBS; i++) {
896 urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
1da177e4
LT
897 if (!urb) {
898 for (j = 0; j < i; j++)
899 usb_free_urb(cam->urb[j]);
900 DBG(1, "Couldn't allocate the URB structures")
901 return -ENOMEM;
902 }
903
ff41efcf 904 cam->urb[i] = urb;
1da177e4
LT
905 urb->dev = udev;
906 urb->context = (void*)cam;
907 urb->pipe = usb_rcvisocpipe(udev, 1);
908 urb->transfer_flags = URB_ISO_ASAP;
909 urb->number_of_packets = W9968CF_ISO_PACKETS;
910 urb->complete = w9968cf_urb_complete;
911 urb->transfer_buffer = cam->transfer_buffer[i];
912 urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
913 urb->interval = 1;
914 for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
915 urb->iso_frame_desc[j].offset = p_size*j;
916 urb->iso_frame_desc[j].length = p_size;
917 }
918 }
919
920 /* Transfer size per frame, in WORD ! */
921 d = cam->hw_depth;
922 w = cam->hw_width;
923 h = cam->hw_height;
924
925 t_size = (w*h*d)/16;
926
927 err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
928 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
929
930 /* Transfer size */
931 err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
932 err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
933
934 if (cam->vpp_flag & VPP_DECOMPRESSION)
935 err += w9968cf_upload_quantizationtables(cam);
936
937 vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
938 err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
939
940 err += usb_set_interface(udev, 0, cam->altsetting);
941 err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
942
943 if (err || (vidcapt < 0)) {
944 for (i = 0; i < W9968CF_URBS; i++)
945 usb_free_urb(cam->urb[i]);
946 DBG(1, "Couldn't tell the camera to start the data transfer")
947 return err;
948 }
949
950 w9968cf_init_framelist(cam);
951
952 /* Begin to grab into the temporary buffer */
953 cam->frame_tmp.status = F_UNUSED;
954 cam->frame_tmp.queued = 0;
955 cam->frame_current = &cam->frame_tmp;
956
957 if (!(cam->vpp_flag & VPP_DECOMPRESSION))
d56410e0 958 DBG(5, "Isochronous transfer size: %lu bytes/frame",
1da177e4
LT
959 (unsigned long)t_size*2)
960
961 DBG(5, "Starting the isochronous transfer...")
962
963 cam->streaming = 1;
964
965 /* Submit the URBs */
966 for (i = 0; i < W9968CF_URBS; i++) {
967 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
968 if (err) {
969 cam->streaming = 0;
970 for (j = i-1; j >= 0; j--) {
971 usb_kill_urb(cam->urb[j]);
972 usb_free_urb(cam->urb[j]);
973 }
974 DBG(1, "Couldn't send a transfer request to the "
d56410e0 975 "USB core (error #%d, %s)", err,
1da177e4
LT
976 symbolic(urb_errlist, err))
977 return err;
978 }
979 }
980
981 return 0;
982}
983
984
985/*--------------------------------------------------------------------------
986 Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
987 Return 0 on success, a negative number otherwise.
988 --------------------------------------------------------------------------*/
989static int w9968cf_stop_transfer(struct w9968cf_device* cam)
990{
991 struct usb_device *udev = cam->usbdev;
992 unsigned long lock_flags;
993 int err = 0;
994 s8 i;
995
996 if (!cam->streaming)
997 return 0;
998
d56410e0 999 /* This avoids race conditions with usb_submit_urb()
1da177e4
LT
1000 in the URB completition handler */
1001 spin_lock_irqsave(&cam->urb_lock, lock_flags);
1002 cam->streaming = 0;
1003 spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
1004
1005 for (i = W9968CF_URBS-1; i >= 0; i--)
1006 if (cam->urb[i]) {
1007 usb_kill_urb(cam->urb[i]);
1008 usb_free_urb(cam->urb[i]);
1009 cam->urb[i] = NULL;
1010 }
1011
1012 if (cam->disconnected)
1013 goto exit;
1014
1015 err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
1016 err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
1017 err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
1018 err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
1019
1020 if (err) {
1021 DBG(2, "Failed to tell the camera to stop the isochronous "
1022 "transfer. However this is not a critical error.")
1023 return -EIO;
1024 }
1025
1026exit:
1027 DBG(5, "Isochronous transfer stopped")
1028 return 0;
1029}
1030
1031
1032/*--------------------------------------------------------------------------
d56410e0 1033 Write a W9968CF register.
1da177e4
LT
1034 Return 0 on success, -1 otherwise.
1035 --------------------------------------------------------------------------*/
1036static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
1037{
1038 struct usb_device* udev = cam->usbdev;
1039 int res;
1040
1041 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
d56410e0
MCC
1042 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1043 value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
1da177e4
LT
1044
1045 if (res < 0)
1046 DBG(4, "Failed to write a register "
1047 "(value 0x%04X, index 0x%02X, error #%d, %s)",
1048 value, index, res, symbolic(urb_errlist, res))
1049
1050 return (res >= 0) ? 0 : -1;
1051}
1052
1053
1054/*--------------------------------------------------------------------------
d56410e0 1055 Read a W9968CF register.
1da177e4
LT
1056 Return the register value on success, -1 otherwise.
1057 --------------------------------------------------------------------------*/
1058static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
1059{
1060 struct usb_device* udev = cam->usbdev;
1061 u16* buff = cam->control_buffer;
1062 int res;
1063
1064 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
d56410e0
MCC
1065 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1066 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
1da177e4
LT
1067
1068 if (res < 0)
1069 DBG(4, "Failed to read a register "
1070 "(index 0x%02X, error #%d, %s)",
1071 index, res, symbolic(urb_errlist, res))
1072
1073 return (res >= 0) ? (int)(*buff) : -1;
1074}
1075
1076
1077/*--------------------------------------------------------------------------
1078 Write 64-bit data to the fast serial bus registers.
1079 Return 0 on success, -1 otherwise.
1080 --------------------------------------------------------------------------*/
1081static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
1082{
1083 struct usb_device* udev = cam->usbdev;
1084 u16 value;
1085 int res;
1086
1087 value = *data++;
1088
1089 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
d56410e0
MCC
1090 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1091 value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
1da177e4
LT
1092
1093 if (res < 0)
1094 DBG(4, "Failed to write the FSB registers "
1095 "(error #%d, %s)", res, symbolic(urb_errlist, res))
1096
1097 return (res >= 0) ? 0 : -1;
1098}
1099
1100
1101/*--------------------------------------------------------------------------
1102 Write data to the serial bus control register.
1103 Return 0 on success, a negative number otherwise.
1104 --------------------------------------------------------------------------*/
1105static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
1106{
1107 int err = 0;
1108
1109 err = w9968cf_write_reg(cam, value, 0x01);
1110 udelay(W9968CF_I2C_BUS_DELAY);
1111
1112 return err;
1113}
1114
1115
1116/*--------------------------------------------------------------------------
1117 Read data from the serial bus control register.
1118 Return 0 on success, a negative number otherwise.
1119 --------------------------------------------------------------------------*/
1120static int w9968cf_read_sb(struct w9968cf_device* cam)
1121{
1122 int v = 0;
1123
1124 v = w9968cf_read_reg(cam, 0x01);
1125 udelay(W9968CF_I2C_BUS_DELAY);
1126
1127 return v;
1128}
1129
1130
1131/*--------------------------------------------------------------------------
1132 Upload quantization tables for the JPEG compression.
1133 This function is called by w9968cf_start_transfer().
1134 Return 0 on success, a negative number otherwise.
1135 --------------------------------------------------------------------------*/
1136static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
1137{
1138 u16 a, b;
1139 int err = 0, i, j;
1140
1141 err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
1142
1143 for (i = 0, j = 0; i < 32; i++, j += 2) {
1144 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
1145 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
1146 err += w9968cf_write_reg(cam, a, 0x40+i);
1147 err += w9968cf_write_reg(cam, b, 0x60+i);
1148 }
1149 err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
1150
1151 return err;
1152}
1153
1154
1155
1156/****************************************************************************
1157 * Low-level I2C I/O functions. *
1158 * The adapter supports the following I2C transfer functions: *
1159 * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
1160 * i2c_adap_read_byte_data() *
1161 * i2c_adap_read_byte() *
1162 ****************************************************************************/
1163
1164static int w9968cf_smbus_start(struct w9968cf_device* cam)
1165{
1166 int err = 0;
1167
1168 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1169 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1170
1171 return err;
1172}
1173
1174
1175static int w9968cf_smbus_stop(struct w9968cf_device* cam)
1176{
1177 int err = 0;
1178
1179 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1180 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1181
1182 return err;
1183}
1184
1185
1186static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
1187{
1188 u8 bit;
1189 int err = 0, sda;
1190
1191 for (bit = 0 ; bit < 8 ; bit++) {
1192 sda = (v & 0x80) ? 2 : 0;
1193 v <<= 1;
1194 /* SDE=1, SDA=sda, SCL=0 */
1195 err += w9968cf_write_sb(cam, 0x10 | sda);
1196 /* SDE=1, SDA=sda, SCL=1 */
1197 err += w9968cf_write_sb(cam, 0x11 | sda);
1198 /* SDE=1, SDA=sda, SCL=0 */
1199 err += w9968cf_write_sb(cam, 0x10 | sda);
1200 }
1201
1202 return err;
1203}
1204
1205
1206static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
1207{
1208 u8 bit;
1209 int err = 0;
1210
1211 *v = 0;
1212 for (bit = 0 ; bit < 8 ; bit++) {
1213 *v <<= 1;
1214 err += w9968cf_write_sb(cam, 0x0013);
1215 *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
1216 err += w9968cf_write_sb(cam, 0x0012);
1217 }
1218
1219 return err;
1220}
1221
1222
1223static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
1224{
1225 int err = 0;
1226
1227 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1228 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1229 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1230
1231 return err;
1232}
1233
1234
1235static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
1236{
1237 int err = 0, sda;
1238
1239 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1240 sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
1241 err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
1242 if (sda < 0)
1243 err += sda;
1244 if (sda == 1) {
1245 DBG(6, "Couldn't receive the ACK")
1246 err += -1;
1247 }
1248
1249 return err;
1250}
1251
1252
1253/* This seems to refresh the communication through the serial bus */
1254static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
1255{
1256 int err = 0, j;
1257
1258 for (j = 1; j <= 10; j++) {
1259 err = w9968cf_write_reg(cam, 0x0020, 0x01);
1260 err += w9968cf_write_reg(cam, 0x0000, 0x01);
1261 if (err)
1262 break;
1263 }
1264
1265 return err;
1266}
1267
1268
1269/* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
d56410e0
MCC
1270static int
1271w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
1272 u16 address, u8 subaddress,u8 value)
1da177e4
LT
1273{
1274 u16* data = cam->data_buffer;
1275 int err = 0;
1276
1277 err += w9968cf_smbus_refresh_bus(cam);
1278
1279 /* Enable SBUS outputs */
1280 err += w9968cf_write_sb(cam, 0x0020);
1281
1282 data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
1283 data[0] |= (address & 0x40) ? 0x4000 : 0x0;
1284 data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
1285 data[1] |= (address & 0x20) ? 0x0150 : 0x0;
1286 data[1] |= (address & 0x10) ? 0x5400 : 0x0;
1287 data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
1288 data[2] |= (address & 0x04) ? 0x0540 : 0x0;
1289 data[2] |= (address & 0x02) ? 0x5000 : 0x0;
1290 data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
1291 data[3] |= (address & 0x01) ? 0x0054 : 0x0;
1292
1293 err += w9968cf_write_fsb(cam, data);
1294
1295 data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
1296 data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
1297 data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
1298 data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
1299 data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
1300 data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
1301 data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
1302 data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
1303 data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
1304 data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
1305 data[3] = 0x001d;
1306
1307 err += w9968cf_write_fsb(cam, data);
1308
1309 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
1310 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
1311 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
1312 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
1313 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
1314 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
1315 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
1316 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
1317 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
1318 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
1319 data[3] = 0xfe1d;
1320
1321 err += w9968cf_write_fsb(cam, data);
1322
1323 /* Disable SBUS outputs */
1324 err += w9968cf_write_sb(cam, 0x0000);
1325
1326 if (!err)
1327 DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
1328 "value 0x%02X", address, subaddress, value)
1329 else
1330 DBG(5, "I2C write byte data failed, addr.0x%04X, "
d56410e0 1331 "subaddr.0x%02X, value 0x%02X",
1da177e4
LT
1332 address, subaddress, value)
1333
1334 return err;
1335}
1336
1337
1338/* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
d56410e0
MCC
1339static int
1340w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
1341 u16 address, u8 subaddress,
1342 u8* value)
1da177e4
LT
1343{
1344 int err = 0;
1345
1346 /* Serial data enable */
1347 err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
1348
1349 err += w9968cf_smbus_start(cam);
1350 err += w9968cf_smbus_write_byte(cam, address);
1351 err += w9968cf_smbus_read_ack(cam);
1352 err += w9968cf_smbus_write_byte(cam, subaddress);
1353 err += w9968cf_smbus_read_ack(cam);
1354 err += w9968cf_smbus_stop(cam);
1355 err += w9968cf_smbus_start(cam);
1356 err += w9968cf_smbus_write_byte(cam, address + 1);
1357 err += w9968cf_smbus_read_ack(cam);
1358 err += w9968cf_smbus_read_byte(cam, value);
1359 err += w9968cf_smbus_write_ack(cam);
1360 err += w9968cf_smbus_stop(cam);
1361
1362 /* Serial data disable */
1363 err += w9968cf_write_sb(cam, 0x0000);
1364
1365 if (!err)
1366 DBG(5, "I2C read byte data done, addr.0x%04X, "
d56410e0 1367 "subaddr.0x%02X, value 0x%02X",
1da177e4
LT
1368 address, subaddress, *value)
1369 else
1370 DBG(5, "I2C read byte data failed, addr.0x%04X, "
1371 "subaddr.0x%02X, wrong value 0x%02X",
1372 address, subaddress, *value)
1373
1374 return err;
1375}
1376
1377
1378/* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
d56410e0 1379static int
1da177e4 1380w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
d56410e0 1381 u16 address, u8* value)
1da177e4
LT
1382{
1383 int err = 0;
1384
1385 /* Serial data enable */
1386 err += w9968cf_write_sb(cam, 0x0013);
1387
1388 err += w9968cf_smbus_start(cam);
1389 err += w9968cf_smbus_write_byte(cam, address + 1);
1390 err += w9968cf_smbus_read_ack(cam);
1391 err += w9968cf_smbus_read_byte(cam, value);
1392 err += w9968cf_smbus_write_ack(cam);
1393 err += w9968cf_smbus_stop(cam);
d56410e0 1394
1da177e4
LT
1395 /* Serial data disable */
1396 err += w9968cf_write_sb(cam, 0x0000);
1397
1398 if (!err)
1399 DBG(5, "I2C read byte done, addr.0x%04X, "
1400 "value 0x%02X", address, *value)
1401 else
1402 DBG(5, "I2C read byte failed, addr.0x%04X, "
1403 "wrong value 0x%02X", address, *value)
1404
1405 return err;
1406}
1407
1408
1409/* SMBus protocol: S Addr Wr [A] Value [A] P */
d56410e0 1410static int
1da177e4 1411w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
d56410e0 1412 u16 address, u8 value)
1da177e4
LT
1413{
1414 DBG(4, "i2c_write_byte() is an unsupported transfer mode")
1415 return -EINVAL;
1416}
1417
1418
1419
1420/****************************************************************************
1421 * I2C interface to kernel *
1422 ****************************************************************************/
1423
1424static int
d56410e0
MCC
1425w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1426 unsigned short flags, char read_write, u8 command,
1427 int size, union i2c_smbus_data *data)
1da177e4 1428{
3160fbc5
HV
1429 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
1430 struct w9968cf_device *cam = to_cam(v4l2_dev);
1da177e4 1431 u8 i;
d56410e0 1432 int err = 0;
1da177e4 1433
1da177e4
LT
1434 if (size == I2C_SMBUS_BYTE) {
1435 /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
1436 addr <<= 1;
1437
1438 if (read_write == I2C_SMBUS_WRITE)
d56410e0
MCC
1439 err = w9968cf_i2c_adap_write_byte(cam, addr, command);
1440 else if (read_write == I2C_SMBUS_READ)
3160fbc5
HV
1441 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1442 err = w9968cf_i2c_adap_read_byte(cam, addr,
1443 &data->byte);
1444 if (err) {
1445 if (w9968cf_smbus_refresh_bus(cam)) {
1446 err = -EIO;
1447 break;
1448 }
1449 } else
1450 break;
1451 }
1da177e4
LT
1452 } else if (size == I2C_SMBUS_BYTE_DATA) {
1453 addr <<= 1;
1454
1455 if (read_write == I2C_SMBUS_WRITE)
d56410e0
MCC
1456 err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
1457 command, data->byte);
1da177e4
LT
1458 else if (read_write == I2C_SMBUS_READ) {
1459 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1460 err = w9968cf_i2c_adap_read_byte_data(cam,addr,
d56410e0 1461 command, &data->byte);
1da177e4
LT
1462 if (err) {
1463 if (w9968cf_smbus_refresh_bus(cam)) {
1464 err = -EIO;
1465 break;
1466 }
1467 } else
1468 break;
1469 }
1470
1471 } else
1472 return -EINVAL;
1473
1474 } else {
1475 DBG(4, "Unsupported I2C transfer mode (%d)", size)
1476 return -EINVAL;
1477 }
1da177e4
LT
1478 return err;
1479}
1480
1481
1482static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
1483{
1484 return I2C_FUNC_SMBUS_READ_BYTE |
1485 I2C_FUNC_SMBUS_READ_BYTE_DATA |
1486 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
1487}
1488
1489
1da177e4
LT
1490static int w9968cf_i2c_init(struct w9968cf_device* cam)
1491{
1492 int err = 0;
1493
1494 static struct i2c_algorithm algo = {
1da177e4 1495 .smbus_xfer = w9968cf_i2c_smbus_xfer,
1da177e4
LT
1496 .functionality = w9968cf_i2c_func,
1497 };
1498
1499 static struct i2c_adapter adap = {
1da177e4 1500 .owner = THIS_MODULE,
1da177e4
LT
1501 .algo = &algo,
1502 };
1503
1504 memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
1505 strcpy(cam->i2c_adapter.name, "w9968cf");
12a917f6 1506 cam->i2c_adapter.dev.parent = &cam->usbdev->dev;
3160fbc5 1507 i2c_set_adapdata(&cam->i2c_adapter, &cam->v4l2_dev);
1da177e4
LT
1508
1509 DBG(6, "Registering I2C adapter with kernel...")
1510
1511 err = i2c_add_adapter(&cam->i2c_adapter);
1512 if (err)
1513 DBG(1, "Failed to register the I2C adapter")
1514 else
1515 DBG(5, "I2C adapter registered")
1516
1517 return err;
1518}
1519
1520
1521
1522/****************************************************************************
1523 * Helper functions *
1524 ****************************************************************************/
1525
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 --------------------------------------------------------------------------*/
1530static int w9968cf_turn_on_led(struct w9968cf_device* cam)
1531{
1532 int err = 0;
1533
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' */
1540
1541 if (err)
1542 DBG(2, "Couldn't turn on the LED")
1543
1544 DBG(5, "LED turned on")
1545
1546 return err;
1547}
1548
1549
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 --------------------------------------------------------------------------*/
1555static int w9968cf_init_chip(struct w9968cf_device* cam)
1556{
1557 unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
d56410e0
MCC
1558 y0 = 0x0000,
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;
1da177e4
LT
1564 int err = 0;
1565
1566 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
1567 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
1568
1569 err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
1570 err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
1571
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 */
1578
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 */
1585
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 */
1588
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 */
1591
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 */
1596
1597 err += w9968cf_set_picture(cam, cam->picture); /* this before */
1598 err += w9968cf_set_window(cam, cam->window);
1599
1600 if (err)
1601 DBG(1, "Chip initialization failed")
1602 else
1603 DBG(5, "Chip successfully initialized")
1604
1605 return err;
1606}
1607
1608
1609/*--------------------------------------------------------------------------
1610 Return non-zero if the palette is supported, 0 otherwise.
1611 --------------------------------------------------------------------------*/
1612static inline u16 w9968cf_valid_palette(u16 palette)
1613{
1614 u8 i = 0;
1615 while (w9968cf_formatlist[i].palette != 0) {
1616 if (palette == w9968cf_formatlist[i].palette)
1617 return palette;
1618 i++;
1619 }
1620 return 0;
1621}
1622
1623
1624/*--------------------------------------------------------------------------
1625 Return the depth corresponding to the given palette.
1626 Palette _must_ be supported !
1627 --------------------------------------------------------------------------*/
1628static inline u16 w9968cf_valid_depth(u16 palette)
1629{
1630 u8 i=0;
1631 while (w9968cf_formatlist[i].palette != palette)
1632 i++;
1633
1634 return w9968cf_formatlist[i].depth;
1635}
1636
1637
1638/*--------------------------------------------------------------------------
1639 Return non-zero if the format requires decompression, 0 otherwise.
1640 --------------------------------------------------------------------------*/
1641static inline u8 w9968cf_need_decompression(u16 palette)
1642{
1643 u8 i = 0;
1644 while (w9968cf_formatlist[i].palette != 0) {
1645 if (palette == w9968cf_formatlist[i].palette)
1646 return w9968cf_formatlist[i].compression;
1647 i++;
1648 }
1649 return 0;
1650}
1651
1652
1653/*--------------------------------------------------------------------------
1654 Change the picture settings of the camera.
1655 Return 0 on success, a negative number otherwise.
1656 --------------------------------------------------------------------------*/
1657static int
1658w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
1659{
1660 u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
1661 int err = 0;
1662
1663 /* Make sure we are using a valid depth */
1664 pict.depth = w9968cf_valid_depth(pict.palette);
1665
1666 fmt = pict.palette;
1667
1668 hw_depth = pict.depth; /* depth used by the winbond chip */
1669 hw_palette = pict.palette; /* palette used by the winbond chip */
1670
1671 /* VS & HS polarities */
1672 reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
1673
1674 switch (fmt)
1675 {
1676 case VIDEO_PALETTE_UYVY:
1677 reg_v |= 0x0000;
1678 cam->vpp_flag = VPP_NONE;
1679 break;
1680 case VIDEO_PALETTE_YUV422P:
1681 reg_v |= 0x0002;
1682 cam->vpp_flag = VPP_DECOMPRESSION;
1683 break;
1684 case VIDEO_PALETTE_YUV420:
1685 case VIDEO_PALETTE_YUV420P:
1686 reg_v |= 0x0003;
1687 cam->vpp_flag = VPP_DECOMPRESSION;
1688 break;
1689 case VIDEO_PALETTE_YUYV:
1690 case VIDEO_PALETTE_YUV422:
1691 reg_v |= 0x0000;
1692 cam->vpp_flag = VPP_SWAP_YUV_BYTES;
1693 hw_palette = VIDEO_PALETTE_UYVY;
1694 break;
d56410e0 1695 /* Original video is used instead of RGBX palettes.
1da177e4
LT
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 */
1703 hw_depth = 16;
1704 hw_palette = VIDEO_PALETTE_UYVY;
1705 cam->vpp_flag = VPP_UYVY_TO_RGBX;
1706 break;
1707 }
1708
1709 /* NOTE: due to memory issues, it is better to disable the hardware
d56410e0 1710 double buffering during compression */
1da177e4
LT
1711 if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
1712 reg_v |= 0x0080;
1713
1714 if (cam->clamping)
1715 reg_v |= 0x0020;
1716
1717 if (cam->filter_type == 1)
1718 reg_v |= 0x0008;
1719 else if (cam->filter_type == 2)
1720 reg_v |= 0x000c;
1721
1722 if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
1723 goto error;
1724
1725 if ((err = w9968cf_sensor_update_picture(cam, pict)))
1726 goto error;
1727
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;
1732
1733 /* Settings changed, so we clear the frame buffers */
1734 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1735
1736 DBG(4, "Palette is %s, depth is %u bpp",
1737 symbolic(v4l1_plist, pict.palette), pict.depth)
1738
1739 return 0;
1740
1741error:
1742 DBG(1, "Failed to change picture settings")
1743 return err;
1744}
1745
1746
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 --------------------------------------------------------------------------*/
1752static int
1753w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
1754{
1755 u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
1756 unsigned long fw, fh;
1757 struct ovcamchip_window s_win;
1758 int err = 0;
1759
1760 /* Work around to avoid FP arithmetics */
bee8a44e
RB
1761 #define SC(x) ((x) << 10)
1762 #define UNSC(x) ((x) >> 10)
1da177e4
LT
1763
1764 /* Make sure we are using a supported resolution */
1c657a99 1765 if ((err = w9968cf_adjust_window_size(cam, &win.width, &win.height)))
1da177e4
LT
1766 goto error;
1767
1768 /* Scaling factors */
bee8a44e
RB
1769 fw = SC(win.width) / cam->maxwidth;
1770 fh = SC(win.height) / cam->maxheight;
1da177e4
LT
1771
1772 /* Set up the width and height values used by the chip */
1773 if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
1774 cam->vpp_flag |= VPP_UPSCALE;
1775 /* Calculate largest w,h mantaining the same w/h ratio */
bee8a44e
RB
1776 w = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1777 h = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
1da177e4
LT
1778 if (w < cam->minwidth) /* just in case */
1779 w = cam->minwidth;
1780 if (h < cam->minheight) /* just in case */
1781 h = cam->minheight;
1782 } else {
1783 cam->vpp_flag &= ~VPP_UPSCALE;
1784 w = win.width;
1785 h = win.height;
1786 }
1787
1788 /* x,y offsets of the cropped area */
1789 scx = cam->start_cropx;
1790 scy = cam->start_cropy;
1791
1792 /* Calculate cropped area manteining the right w/h ratio */
1793 if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
bee8a44e
RB
1794 cw = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1795 ch = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
1da177e4
LT
1796 } else {
1797 cw = w;
1798 ch = h;
1799 }
1800
1801 /* Setup the window of the sensor */
1802 s_win.format = VIDEO_PALETTE_UYVY;
1803 s_win.width = cam->maxwidth;
1804 s_win.height = cam->maxheight;
1805 s_win.quarter = 0; /* full progressive video */
1806
1807 /* Center it */
1808 s_win.x = (s_win.width - cw) / 2;
1809 s_win.y = (s_win.height - ch) / 2;
1810
1811 /* Clock divisor */
1812 if (cam->clockdiv >= 0)
1813 s_win.clockdiv = cam->clockdiv; /* manual override */
1814 else
1815 switch (cam->sensor) {
1816 case CC_OV6620:
1817 s_win.clockdiv = 0;
1818 break;
1819 case CC_OV6630:
1820 s_win.clockdiv = 0;
1821 break;
1822 case CC_OV76BE:
1823 case CC_OV7610:
1824 case CC_OV7620:
1825 s_win.clockdiv = 0;
1826 break;
1827 default:
1828 s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
1829 }
1830
1831 /* We have to scale win.x and win.y offsets */
1832 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1833 || (cam->vpp_flag & VPP_UPSCALE) ) {
bee8a44e
RB
1834 ax = SC(win.x)/fw;
1835 ay = SC(win.y)/fh;
1da177e4
LT
1836 } else {
1837 ax = win.x;
1838 ay = win.y;
1839 }
1840
1841 if ((ax + cw) > cam->maxwidth)
1842 ax = cam->maxwidth - cw;
1843
1844 if ((ay + ch) > cam->maxheight)
1845 ay = cam->maxheight - ch;
1846
1847 /* Adjust win.x, win.y */
1848 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1849 || (cam->vpp_flag & VPP_UPSCALE) ) {
bee8a44e
RB
1850 win.x = UNSC(ax*fw);
1851 win.y = UNSC(ay*fh);
1da177e4
LT
1852 } else {
1853 win.x = ax;
1854 win.y = ay;
1855 }
1856
1857 /* Offsets used by the chip */
1858 x = ax + s_win.x;
1859 y = ay + s_win.y;
1860
1861 /* Go ! */
1862 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
1863 goto error;
1864
1865 err += w9968cf_write_reg(cam, scx + x, 0x10);
1866 err += w9968cf_write_reg(cam, scy + y, 0x11);
1867 err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
1868 err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
1869 err += w9968cf_write_reg(cam, w, 0x14);
1870 err += w9968cf_write_reg(cam, h, 0x15);
1871
1872 /* JPEG width & height */
1873 err += w9968cf_write_reg(cam, w, 0x30);
1874 err += w9968cf_write_reg(cam, h, 0x31);
1875
1876 /* Y & UV frame buffer strides (in WORD) */
1877 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1878 err += w9968cf_write_reg(cam, w/2, 0x2c);
1879 err += w9968cf_write_reg(cam, w/4, 0x2d);
1880 } else
1881 err += w9968cf_write_reg(cam, w, 0x2c);
1882
1883 if (err)
1884 goto error;
1885
1886 /* If all went well, update the device data structure */
1887 memcpy(&cam->window, &win, sizeof(win));
1888 cam->hw_width = w;
1889 cam->hw_height = h;
1890
1891 /* Settings changed, so we clear the frame buffers */
1892 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1893
d56410e0 1894 DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
1da177e4
LT
1895 win.width, win.height, win.x, win.y)
1896
1897 PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
1898 "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
1899 x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
1900 win.width, win.height)
1901
1902 return 0;
1903
1904error:
1905 DBG(1, "Failed to change the capture area size")
1906 return err;
1907}
1908
1909
d56410e0 1910/*--------------------------------------------------------------------------
1da177e4
LT
1911 Adjust the asked values for window width and height.
1912 Return 0 on success, -1 otherwise.
1913 --------------------------------------------------------------------------*/
d56410e0 1914static int
1c657a99 1915w9968cf_adjust_window_size(struct w9968cf_device *cam, u32 *width, u32 *height)
1da177e4 1916{
1c657a99 1917 unsigned int maxw, maxh, align;
1da177e4
LT
1918
1919 maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1920 w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
d56410e0 1921 : cam->maxwidth;
1da177e4
LT
1922 maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1923 w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
d56410e0 1924 : cam->maxheight;
1c657a99 1925 align = (cam->vpp_flag & VPP_DECOMPRESSION) ? 4 : 0;
1da177e4 1926
1c657a99
TP
1927 v4l_bound_align_image(width, cam->minwidth, maxw, align,
1928 height, cam->minheight, maxh, align, 0);
1da177e4
LT
1929
1930 PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
1931
1932 return 0;
1933}
1934
1935
1936/*--------------------------------------------------------------------------
1937 Initialize the FIFO list of requested frames.
1938 --------------------------------------------------------------------------*/
1939static void w9968cf_init_framelist(struct w9968cf_device* cam)
1940{
1941 u8 i;
1942
1943 for (i = 0; i < cam->nbuffers; i++) {
1944 cam->requested_frame[i] = NULL;
1945 cam->frame[i].queued = 0;
1946 cam->frame[i].status = F_UNUSED;
1947 }
1948}
1949
1950
1951/*--------------------------------------------------------------------------
1952 Add a frame in the FIFO list of requested frames.
1953 This function is called in process context.
1954 --------------------------------------------------------------------------*/
1955static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
1956{
1957 u8 f;
1958 unsigned long lock_flags;
1959
1960 spin_lock_irqsave(&cam->flist_lock, lock_flags);
1961
1962 for (f=0; cam->requested_frame[f] != NULL; f++);
1963 cam->requested_frame[f] = &cam->frame[f_num];
1964 cam->frame[f_num].queued = 1;
1965 cam->frame[f_num].status = F_UNUSED; /* clear the status */
1966
1967 spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
1968
1969 DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
1970}
1971
1972
1973/*--------------------------------------------------------------------------
1974 Read, store and remove the first pointer in the FIFO list of requested
1975 frames. This function is called in interrupt context.
1976 --------------------------------------------------------------------------*/
d56410e0 1977static void
1da177e4
LT
1978w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
1979{
1980 u8 i;
1981
1982 spin_lock(&cam->flist_lock);
1983
1984 *framep = cam->requested_frame[0];
1985
1986 /* Shift the list of pointers */
1987 for (i = 0; i < cam->nbuffers-1; i++)
1988 cam->requested_frame[i] = cam->requested_frame[i+1];
1989 cam->requested_frame[i] = NULL;
1990
1991 spin_unlock(&cam->flist_lock);
1992
1993 DBG(6,"Popped frame #%d from the list", (*framep)->number)
1994}
1995
1996
1997/*--------------------------------------------------------------------------
1998 High-level video post-processing routine on grabbed frames.
1999 Return 0 on success, a negative number otherwise.
2000 --------------------------------------------------------------------------*/
d56410e0
MCC
2001static int
2002w9968cf_postprocess_frame(struct w9968cf_device* cam,
2003 struct w9968cf_frame_t* fr)
1da177e4
LT
2004{
2005 void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
2006 u16 w = cam->window.width,
2007 h = cam->window.height,
2008 d = cam->picture.depth,
2009 fmt = cam->picture.palette,
2010 rgb = cam->force_rgb,
2011 hw_w = cam->hw_width,
2012 hw_h = cam->hw_height,
2013 hw_d = cam->hw_depth;
2014 int err = 0;
2015
2016 #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
2017
2018 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2019 memcpy(pOut, pIn, fr->length);
2020 _PSWAP(pIn, pOut)
2021 err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
2022 PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
2023 fr->length = (hw_w*hw_h*hw_d)/8;
2024 _PSWAP(pIn, pOut)
2025 if (err) {
2026 DBG(4, "An error occurred while decoding the frame: "
2027 "%s", symbolic(decoder_errlist, err))
2028 return err;
2029 } else
2030 DBG(6, "Frame decoded")
2031 }
2032
2033 if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
2034 w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
2035 DBG(6, "Original UYVY component ordering changed")
2036 }
2037
2038 if (cam->vpp_flag & VPP_UPSCALE) {
2039 w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
2040 fr->length = (w*h*hw_d)/8;
2041 _PSWAP(pIn, pOut)
2042 DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
2043 hw_w, hw_h, hw_d, w, h)
2044 }
2045
2046 if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
2047 w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
2048 fr->length = (w*h*d)/8;
2049 _PSWAP(pIn, pOut)
d56410e0 2050 DBG(6, "UYVY-16bit to %s conversion done",
1da177e4
LT
2051 symbolic(v4l1_plist, fmt))
2052 }
2053
2054 if (pOut == fr->buffer)
2055 memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
2056
2057 return 0;
2058}
2059
2060
2061
2062/****************************************************************************
2063 * Image sensor control routines *
2064 ****************************************************************************/
2065
d56410e0 2066static int
1da177e4
LT
2067w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
2068{
2069 struct ovcamchip_control ctl;
2070 int err;
2071
2072 ctl.id = cid;
2073 ctl.value = val;
2074
2075 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
2076
2077 return err;
2078}
2079
2080
d56410e0 2081static int
1da177e4
LT
2082w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
2083{
2084 struct ovcamchip_control ctl;
2085 int err;
2086
2087 ctl.id = cid;
2088
2089 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
2090 if (!err)
2091 *val = ctl.value;
2092
2093 return err;
2094}
2095
2096
2097static int
2098w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
2099{
3160fbc5 2100 int rc;
1da177e4 2101
3160fbc5 2102 rc = v4l2_subdev_call(cam->sensor_sd, core, ioctl, cmd, arg);
1da177e4
LT
2103 /* The I2C driver returns -EPERM on non-supported controls */
2104 return (rc < 0 && rc != -EPERM) ? rc : 0;
2105}
2106
2107
2108/*--------------------------------------------------------------------------
2109 Update some settings of the image sensor.
2110 Returns: 0 on success, a negative number otherwise.
2111 --------------------------------------------------------------------------*/
2112static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
2113{
2114 int err = 0;
2115
2116 /* Auto brightness */
d56410e0
MCC
2117 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
2118 cam->auto_brt);
1da177e4
LT
2119 if (err)
2120 return err;
2121
2122 /* Auto exposure */
d56410e0
MCC
2123 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
2124 cam->auto_exp);
1da177e4
LT
2125 if (err)
2126 return err;
2127
2128 /* Banding filter */
d56410e0
MCC
2129 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
2130 cam->bandfilt);
1da177e4
LT
2131 if (err)
2132 return err;
2133
2134 /* Light frequency */
2135 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
d56410e0 2136 cam->lightfreq);
1da177e4
LT
2137 if (err)
2138 return err;
2139
2140 /* Back light */
2141 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
d56410e0 2142 cam->backlight);
1da177e4
LT
2143 if (err)
2144 return err;
2145
2146 /* Mirror */
2147 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
d56410e0 2148 cam->mirror);
1da177e4
LT
2149 if (err)
2150 return err;
2151
2152 return 0;
2153}
2154
2155
2156/*--------------------------------------------------------------------------
2157 Get some current picture settings from the image sensor and update the
2158 internal 'picture' structure of the camera.
2159 Returns: 0 on success, a negative number otherwise.
2160 --------------------------------------------------------------------------*/
2161static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
2162{
2163 int err, v;
2164
2165 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
2166 if (err)
2167 return err;
2168 cam->picture.contrast = v;
2169
2170 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
2171 if (err)
2172 return err;
2173 cam->picture.brightness = v;
2174
2175 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
2176 if (err)
2177 return err;
2178 cam->picture.colour = v;
2179
2180 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
2181 if (err)
2182 return err;
2183 cam->picture.hue = v;
2184
2185 DBG(5, "Got picture settings from the image sensor")
2186
2187 PDBGG("Brightness, contrast, hue, colour, whiteness are "
2188 "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
2189 cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
2190
2191 return 0;
2192}
2193
2194
2195/*--------------------------------------------------------------------------
2196 Update picture settings of the image sensor.
2197 Returns: 0 on success, a negative number otherwise.
2198 --------------------------------------------------------------------------*/
2199static int
d56410e0
MCC
2200w9968cf_sensor_update_picture(struct w9968cf_device* cam,
2201 struct video_picture pict)
1da177e4
LT
2202{
2203 int err = 0;
2204
2205 if ((!cam->sensor_initialized)
2206 || pict.contrast != cam->picture.contrast) {
2207 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
d56410e0 2208 pict.contrast);
1da177e4
LT
2209 if (err)
2210 goto fail;
2211 DBG(4, "Contrast changed from %u to %u",
2212 cam->picture.contrast, pict.contrast)
2213 cam->picture.contrast = pict.contrast;
2214 }
2215
d56410e0 2216 if (((!cam->sensor_initialized) ||
1da177e4 2217 pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
d56410e0
MCC
2218 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
2219 pict.brightness);
1da177e4
LT
2220 if (err)
2221 goto fail;
2222 DBG(4, "Brightness changed from %u to %u",
2223 cam->picture.brightness, pict.brightness)
2224 cam->picture.brightness = pict.brightness;
2225 }
2226
2227 if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
d56410e0
MCC
2228 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
2229 pict.colour);
1da177e4
LT
2230 if (err)
2231 goto fail;
2232 DBG(4, "Colour changed from %u to %u",
2233 cam->picture.colour, pict.colour)
2234 cam->picture.colour = pict.colour;
2235 }
2236
2237 if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
d56410e0
MCC
2238 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
2239 pict.hue);
1da177e4
LT
2240 if (err)
2241 goto fail;
2242 DBG(4, "Hue changed from %u to %u",
2243 cam->picture.hue, pict.hue)
2244 cam->picture.hue = pict.hue;
2245 }
2246
2247 return 0;
2248
2249fail:
2250 DBG(4, "Failed to change sensor picture setting")
2251 return err;
2252}
2253
2254
2255
2256/****************************************************************************
2257 * Camera configuration *
2258 ****************************************************************************/
2259
2260/*--------------------------------------------------------------------------
2261 This function is called when a supported image sensor is detected.
2262 Return 0 if the initialization succeeds, a negative number otherwise.
2263 --------------------------------------------------------------------------*/
2264static int w9968cf_sensor_init(struct w9968cf_device* cam)
2265{
2266 int err = 0;
2267
d56410e0
MCC
2268 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
2269 &cam->monochrome)))
1da177e4
LT
2270 goto error;
2271
d56410e0
MCC
2272 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
2273 &cam->sensor)))
1da177e4
LT
2274 goto error;
2275
2276 /* NOTE: Make sure width and height are a multiple of 16 */
3160fbc5 2277 switch (v4l2_i2c_subdev_addr(cam->sensor_sd)) {
1da177e4
LT
2278 case OV6xx0_SID:
2279 cam->maxwidth = 352;
2280 cam->maxheight = 288;
2281 cam->minwidth = 64;
2282 cam->minheight = 48;
2283 break;
2284 case OV7xx0_SID:
2285 cam->maxwidth = 640;
2286 cam->maxheight = 480;
2287 cam->minwidth = 64;
2288 cam->minheight = 48;
2289 break;
2290 default:
2291 DBG(1, "Not supported image sensor detected for %s",
2292 symbolic(camlist, cam->id))
2293 return -EINVAL;
2294 }
2295
2296 /* These values depend on the ones in the ovxxx0.c sources */
2297 switch (cam->sensor) {
2298 case CC_OV7620:
2299 cam->start_cropx = 287;
2300 cam->start_cropy = 35;
2301 /* Seems to work around a bug in the image sensor */
2302 cam->vs_polarity = 1;
2303 cam->hs_polarity = 1;
2304 break;
2305 default:
2306 cam->start_cropx = 320;
2307 cam->start_cropy = 35;
2308 cam->vs_polarity = 1;
2309 cam->hs_polarity = 0;
2310 }
2311
2312 if ((err = w9968cf_sensor_update_settings(cam)))
2313 goto error;
2314
2315 if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
2316 goto error;
2317
2318 cam->sensor_initialized = 1;
2319
2320 DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
2321 return 0;
2322
2323error:
2324 cam->sensor_initialized = 0;
2325 cam->sensor = CC_UNKNOWN;
2326 DBG(1, "Image sensor initialization failed for %s (/dev/video%d). "
2327 "Try to detach and attach this device again",
c6330fb8 2328 symbolic(camlist, cam->id), cam->v4ldev->num)
1da177e4
LT
2329 return err;
2330}
2331
2332
2333/*--------------------------------------------------------------------------
2334 Fill some basic fields in the main device data structure.
d56410e0 2335 This function is called once on w9968cf_usb_probe() for each recognized
1da177e4
LT
2336 camera.
2337 --------------------------------------------------------------------------*/
2338static void
2339w9968cf_configure_camera(struct w9968cf_device* cam,
d56410e0
MCC
2340 struct usb_device* udev,
2341 enum w9968cf_model_id mod_id,
2342 const unsigned short dev_nr)
1da177e4 2343{
4186ecf8 2344 mutex_init(&cam->fileop_mutex);
1da177e4
LT
2345 init_waitqueue_head(&cam->open);
2346 spin_lock_init(&cam->urb_lock);
2347 spin_lock_init(&cam->flist_lock);
2348
2349 cam->users = 0;
2350 cam->disconnected = 0;
2351 cam->id = mod_id;
2352 cam->sensor = CC_UNKNOWN;
2353 cam->sensor_initialized = 0;
2354
2355 /* Calculate the alternate setting number (from 1 to 16)
2356 according to the 'packet_size' module parameter */
2357 if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
2358 packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
2359 for (cam->altsetting = 1;
2360 packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
2361 cam->altsetting++);
2362
d56410e0
MCC
2363 cam->max_buffers = (max_buffers[dev_nr] < 2 ||
2364 max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
2365 ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
1da177e4 2366
d56410e0
MCC
2367 cam->double_buffer = (double_buffer[dev_nr] == 0 ||
2368 double_buffer[dev_nr] == 1)
2369 ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
1da177e4
LT
2370
2371 cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
d56410e0
MCC
2372 ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
2373
1da177e4 2374 cam->filter_type = (filter_type[dev_nr] == 0 ||
d56410e0
MCC
2375 filter_type[dev_nr] == 1 ||
2376 filter_type[dev_nr] == 2)
2377 ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
1da177e4
LT
2378
2379 cam->capture = 1;
2380
2381 cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
d56410e0 2382 ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
1da177e4 2383
d56410e0
MCC
2384 cam->decompression = (decompression[dev_nr] == 0 ||
2385 decompression[dev_nr] == 1 ||
2386 decompression[dev_nr] == 2)
2387 ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
1da177e4 2388
d56410e0
MCC
2389 cam->upscaling = (upscaling[dev_nr] == 0 ||
2390 upscaling[dev_nr] == 1)
2391 ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
1da177e4
LT
2392
2393 cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
d56410e0 2394 ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
1da177e4
LT
2395
2396 cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
d56410e0 2397 ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
1da177e4
LT
2398
2399 cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
d56410e0 2400 ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
1da177e4 2401
d56410e0
MCC
2402 cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
2403 bandingfilter[dev_nr] == 1)
2404 ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
1da177e4
LT
2405
2406 cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
d56410e0 2407 ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
1da177e4
LT
2408
2409 cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
d56410e0 2410 ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
1da177e4
LT
2411
2412 cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
d56410e0 2413 ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
1da177e4
LT
2414
2415 cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
d56410e0 2416 ? monochrome[dev_nr] : W9968CF_MONOCHROME;
1da177e4
LT
2417
2418 cam->picture.brightness = (u16)brightness[dev_nr];
2419 cam->picture.hue = (u16)hue[dev_nr];
2420 cam->picture.colour = (u16)colour[dev_nr];
2421 cam->picture.contrast = (u16)contrast[dev_nr];
2422 cam->picture.whiteness = (u16)whiteness[dev_nr];
2423 if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
2424 cam->picture.palette = (u16)force_palette[dev_nr];
2425 cam->force_palette = 1;
2426 } else {
2427 cam->force_palette = 0;
2428 if (cam->decompression == 0)
2429 cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
2430 else if (cam->decompression == 1)
2431 cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
2432 else
2433 cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
2434 }
2435 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2436
2437 cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
d56410e0 2438 ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
1da177e4
LT
2439
2440 cam->window.x = 0;
2441 cam->window.y = 0;
2442 cam->window.width = W9968CF_WIDTH;
2443 cam->window.height = W9968CF_HEIGHT;
2444 cam->window.chromakey = 0;
2445 cam->window.clipcount = 0;
2446 cam->window.flags = 0;
2447
2448 DBG(3, "%s configured with settings #%u:",
2449 symbolic(camlist, cam->id), dev_nr)
d56410e0 2450
1da177e4
LT
2451 DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
2452 wMaxPacketSize[cam->altsetting-1])
d56410e0 2453
1da177e4
LT
2454 DBG(3, "- Number of requested video frame buffers: %u",
2455 cam->max_buffers)
2456
2457 if (cam->double_buffer)
2458 DBG(3, "- Hardware double buffering enabled")
d56410e0 2459 else
1da177e4
LT
2460 DBG(3, "- Hardware double buffering disabled")
2461
2462 if (cam->filter_type == 0)
2463 DBG(3, "- Video filtering disabled")
2464 else if (cam->filter_type == 1)
2465 DBG(3, "- Video filtering enabled: type 1-2-1")
2466 else if (cam->filter_type == 2)
2467 DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
2468
2469 if (cam->clamping)
2470 DBG(3, "- Video data clamping (CCIR-601 format) enabled")
2471 else
2472 DBG(3, "- Video data clamping (CCIR-601 format) disabled")
2473
2474 if (cam->largeview)
2475 DBG(3, "- Large view enabled")
2476 else
2477 DBG(3, "- Large view disabled")
2478
2479 if ((cam->decompression) == 0 && (!cam->force_palette))
2480 DBG(3, "- Decompression disabled")
2481 else if ((cam->decompression) == 1 && (!cam->force_palette))
2482 DBG(3, "- Decompression forced")
2483 else if ((cam->decompression) == 2 && (!cam->force_palette))
2484 DBG(3, "- Decompression allowed")
2485
2486 if (cam->upscaling)
2487 DBG(3, "- Software image scaling enabled")
2488 else
2489 DBG(3, "- Software image scaling disabled")
2490
2491 if (cam->force_palette)
2492 DBG(3, "- Image palette forced to %s",
2493 symbolic(v4l1_plist, cam->picture.palette))
2494
2495 if (cam->force_rgb)
2496 DBG(3, "- RGB component ordering will be used instead of BGR")
2497
2498 if (cam->auto_brt)
2499 DBG(3, "- Auto brightness enabled")
2500 else
2501 DBG(3, "- Auto brightness disabled")
2502
2503 if (cam->auto_exp)
2504 DBG(3, "- Auto exposure enabled")
2505 else
2506 DBG(3, "- Auto exposure disabled")
2507
2508 if (cam->backlight)
2509 DBG(3, "- Backlight exposure algorithm enabled")
2510 else
2511 DBG(3, "- Backlight exposure algorithm disabled")
2512
2513 if (cam->mirror)
2514 DBG(3, "- Mirror enabled")
2515 else
2516 DBG(3, "- Mirror disabled")
2517
2518 if (cam->bandfilt)
2519 DBG(3, "- Banding filter enabled")
2520 else
2521 DBG(3, "- Banding filter disabled")
2522
2523 DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
2524
2525 if (cam->clockdiv == -1)
2526 DBG(3, "- Automatic clock divisor enabled")
2527 else
2528 DBG(3, "- Clock divisor: %d", cam->clockdiv)
2529
2530 if (cam->monochrome)
2531 DBG(3, "- Image sensor used as monochrome")
2532 else
2533 DBG(3, "- Image sensor not used as monochrome")
2534}
2535
2536
2537/*--------------------------------------------------------------------------
2538 If the video post-processing module is not loaded, some parameters
2539 must be overridden.
2540 --------------------------------------------------------------------------*/
2541static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2542{
2543 if (!w9968cf_vpp) {
2544 if (cam->decompression == 1) {
2545 cam->decompression = 2;
2546 DBG(2, "Video post-processing module not found: "
2547 "'decompression' parameter forced to 2")
2548 }
2549 if (cam->upscaling) {
2550 cam->upscaling = 0;
2551 DBG(2, "Video post-processing module not found: "
2552 "'upscaling' parameter forced to 0")
2553 }
2554 if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
2555 cam->force_palette = 0;
2556 DBG(2, "Video post-processing module not found: "
2557 "'force_palette' parameter forced to 0")
2558 }
2559 cam->picture.palette = VIDEO_PALETTE_UYVY;
2560 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2561 }
2562}
2563
2564
2565/*--------------------------------------------------------------------------
2566 Release the resources used by the driver.
d56410e0 2567 This function is called on disconnect
1da177e4
LT
2568 (or on close if deallocation has been deferred)
2569 --------------------------------------------------------------------------*/
2570static void w9968cf_release_resources(struct w9968cf_device* cam)
2571{
4186ecf8 2572 mutex_lock(&w9968cf_devlist_mutex);
1da177e4 2573
c6330fb8 2574 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->num)
1da177e4
LT
2575
2576 video_unregister_device(cam->v4ldev);
2577 list_del(&cam->v4llist);
2578 i2c_del_adapter(&cam->i2c_adapter);
2579 w9968cf_deallocate_memory(cam);
2580 kfree(cam->control_buffer);
2581 kfree(cam->data_buffer);
3160fbc5 2582 v4l2_device_unregister(&cam->v4l2_dev);
1da177e4 2583
4186ecf8 2584 mutex_unlock(&w9968cf_devlist_mutex);
1da177e4
LT
2585}
2586
2587
2588
2589/****************************************************************************
2590 * Video4Linux interface *
2591 ****************************************************************************/
2592
bec43661 2593static int w9968cf_open(struct file *filp)
1da177e4
LT
2594{
2595 struct w9968cf_device* cam;
2596 int err;
2597
2598 /* This the only safe way to prevent race conditions with disconnect */
2599 if (!down_read_trylock(&w9968cf_disconnect))
3abff557 2600 return -EAGAIN;
1da177e4
LT
2601
2602 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2603
4186ecf8 2604 mutex_lock(&cam->dev_mutex);
1da177e4
LT
2605
2606 if (cam->sensor == CC_UNKNOWN) {
2607 DBG(2, "No supported image sensor has been detected by the "
2608 "'ovcamchip' module for the %s (/dev/video%d). Make "
2609 "sure it is loaded *before* (re)connecting the camera.",
c6330fb8 2610 symbolic(camlist, cam->id), cam->v4ldev->num)
4186ecf8 2611 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
2612 up_read(&w9968cf_disconnect);
2613 return -ENODEV;
2614 }
2615
2616 if (cam->users) {
2617 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
c6330fb8 2618 symbolic(camlist, cam->id), cam->v4ldev->num, cam->command)
1da177e4 2619 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
4186ecf8 2620 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
2621 up_read(&w9968cf_disconnect);
2622 return -EWOULDBLOCK;
2623 }
4186ecf8 2624 mutex_unlock(&cam->dev_mutex);
1da177e4 2625 err = wait_event_interruptible_exclusive(cam->open,
d56410e0
MCC
2626 cam->disconnected ||
2627 !cam->users);
1da177e4
LT
2628 if (err) {
2629 up_read(&w9968cf_disconnect);
2630 return err;
2631 }
2632 if (cam->disconnected) {
2633 up_read(&w9968cf_disconnect);
2634 return -ENODEV;
2635 }
4186ecf8 2636 mutex_lock(&cam->dev_mutex);
1da177e4
LT
2637 }
2638
2639 DBG(5, "Opening '%s', /dev/video%d ...",
c6330fb8 2640 symbolic(camlist, cam->id), cam->v4ldev->num)
1da177e4
LT
2641
2642 cam->streaming = 0;
2643 cam->misconfigured = 0;
2644
7f2c01ab 2645 w9968cf_adjust_configuration(cam);
1da177e4
LT
2646
2647 if ((err = w9968cf_allocate_memory(cam)))
2648 goto deallocate_memory;
2649
2650 if ((err = w9968cf_init_chip(cam)))
2651 goto deallocate_memory;
2652
2653 if ((err = w9968cf_start_transfer(cam)))
2654 goto deallocate_memory;
2655
2656 filp->private_data = cam;
2657
2658 cam->users++;
2659 strcpy(cam->command, current->comm);
2660
2661 init_waitqueue_head(&cam->wait_queue);
2662
2663 DBG(5, "Video device is open")
2664
4186ecf8 2665 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
2666 up_read(&w9968cf_disconnect);
2667
2668 return 0;
2669
2670deallocate_memory:
2671 w9968cf_deallocate_memory(cam);
1da177e4 2672 DBG(2, "Failed to open the video device")
4186ecf8 2673 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
2674 up_read(&w9968cf_disconnect);
2675 return err;
2676}
2677
2678
bec43661 2679static int w9968cf_release(struct file *filp)
1da177e4
LT
2680{
2681 struct w9968cf_device* cam;
2682
2683 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2684
4186ecf8 2685 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1da177e4
LT
2686
2687 w9968cf_stop_transfer(cam);
2688
1da177e4
LT
2689 if (cam->disconnected) {
2690 w9968cf_release_resources(cam);
4186ecf8 2691 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
2692 kfree(cam);
2693 return 0;
2694 }
2695
2696 cam->users--;
2697 w9968cf_deallocate_memory(cam);
2698 wake_up_interruptible_nr(&cam->open, 1);
2699
2700 DBG(5, "Video device closed")
4186ecf8 2701 mutex_unlock(&cam->dev_mutex);
1da177e4
LT
2702 return 0;
2703}
2704
2705
2706static ssize_t
2707w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2708{
2709 struct w9968cf_device* cam;
2710 struct w9968cf_frame_t* fr;
2711 int err = 0;
2712
2713 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2714
2715 if (filp->f_flags & O_NONBLOCK)
2716 return -EWOULDBLOCK;
2717
4186ecf8 2718 if (mutex_lock_interruptible(&cam->fileop_mutex))
1da177e4
LT
2719 return -ERESTARTSYS;
2720
2721 if (cam->disconnected) {
2722 DBG(2, "Device not present")
4186ecf8 2723 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2724 return -ENODEV;
2725 }
2726
2727 if (cam->misconfigured) {
2728 DBG(2, "The camera is misconfigured. Close and open it again.")
4186ecf8 2729 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2730 return -EIO;
2731 }
2732
2733 if (!cam->frame[0].queued)
2734 w9968cf_push_frame(cam, 0);
2735
2736 if (!cam->frame[1].queued)
2737 w9968cf_push_frame(cam, 1);
2738
2739 err = wait_event_interruptible(cam->wait_queue,
d56410e0
MCC
2740 cam->frame[0].status == F_READY ||
2741 cam->frame[1].status == F_READY ||
2742 cam->disconnected);
1da177e4 2743 if (err) {
4186ecf8 2744 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2745 return err;
2746 }
2747 if (cam->disconnected) {
4186ecf8 2748 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2749 return -ENODEV;
2750 }
2751
2752 fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
2753
2754 if (w9968cf_vpp)
2755 w9968cf_postprocess_frame(cam, fr);
2756
2757 if (count > fr->length)
2758 count = fr->length;
2759
2760 if (copy_to_user(buf, fr->buffer, count)) {
2761 fr->status = F_UNUSED;
4186ecf8 2762 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2763 return -EFAULT;
2764 }
2765 *f_pos += count;
2766
2767 fr->status = F_UNUSED;
2768
2769 DBG(5, "%zu bytes read", count)
2770
4186ecf8 2771 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2772 return count;
2773}
2774
2775
2776static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
2777{
2778 struct w9968cf_device* cam = (struct w9968cf_device*)
d56410e0 2779 video_get_drvdata(video_devdata(filp));
1da177e4 2780 unsigned long vsize = vma->vm_end - vma->vm_start,
d56410e0
MCC
2781 psize = cam->nbuffers * cam->frame[0].size,
2782 start = vma->vm_start,
2783 pos = (unsigned long)cam->frame[0].buffer,
2784 page;
1da177e4
LT
2785
2786 if (cam->disconnected) {
2787 DBG(2, "Device not present")
2788 return -ENODEV;
2789 }
2790
2791 if (cam->misconfigured) {
2792 DBG(2, "The camera is misconfigured. Close and open it again")
2793 return -EIO;
2794 }
2795
2796 PDBGG("mmapping %lu bytes...", vsize)
2797
2798 if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
2799 return -EINVAL;
2800
2801 while (vsize > 0) {
2802 page = vmalloc_to_pfn((void *)pos);
2803 if (remap_pfn_range(vma, start, page + vma->vm_pgoff,
2804 PAGE_SIZE, vma->vm_page_prot))
2805 return -EAGAIN;
2806 start += PAGE_SIZE;
2807 pos += PAGE_SIZE;
2808 vsize -= PAGE_SIZE;
2809 }
2810
2811 DBG(5, "mmap method successfully called")
2812 return 0;
2813}
2814
2815
069b7479 2816static long
bec43661 2817w9968cf_ioctl(struct file *filp,
d56410e0 2818 unsigned int cmd, unsigned long arg)
1da177e4
LT
2819{
2820 struct w9968cf_device* cam;
069b7479 2821 long err;
1da177e4
LT
2822
2823 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2824
4186ecf8 2825 if (mutex_lock_interruptible(&cam->fileop_mutex))
1da177e4
LT
2826 return -ERESTARTSYS;
2827
2828 if (cam->disconnected) {
2829 DBG(2, "Device not present")
4186ecf8 2830 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2831 return -ENODEV;
2832 }
2833
2834 if (cam->misconfigured) {
2835 DBG(2, "The camera is misconfigured. Close and open it again.")
4186ecf8 2836 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2837 return -EIO;
2838 }
2839
bec43661 2840 err = w9968cf_v4l_ioctl(filp, cmd, (void __user *)arg);
1da177e4 2841
4186ecf8 2842 mutex_unlock(&cam->fileop_mutex);
1da177e4
LT
2843 return err;
2844}
2845
2846
069b7479 2847static long w9968cf_v4l_ioctl(struct file *filp,
bec43661 2848 unsigned int cmd, void __user *arg)
1da177e4
LT
2849{
2850 struct w9968cf_device* cam;
2851 const char* v4l1_ioctls[] = {
d56410e0 2852 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
1da177e4
LT
2853 "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
2854 "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
2855 "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
d56410e0
MCC
2856 "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
2857 "GVBIFMT", "SVBIFMT"
1da177e4
LT
2858 };
2859
2860 #define V4L1_IOCTL(cmd) \
d56410e0
MCC
2861 ((_IOC_NR((cmd)) < ARRAY_SIZE(v4l1_ioctls)) ? \
2862 v4l1_ioctls[_IOC_NR((cmd))] : "?")
1da177e4
LT
2863
2864 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2865
2866 switch (cmd) {
2867
2868 case VIDIOCGCAP: /* get video capability */
2869 {
2870 struct video_capability cap = {
2871 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
2872 .channels = 1,
2873 .audios = 0,
2874 .minwidth = cam->minwidth,
2875 .minheight = cam->minheight,
2876 };
d56410e0 2877 sprintf(cap.name, "W996[87]CF USB Camera #%d",
c6330fb8 2878 cam->v4ldev->num);
1da177e4 2879 cap.maxwidth = (cam->upscaling && w9968cf_vpp)
d56410e0
MCC
2880 ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2881 : cam->maxwidth;
1da177e4 2882 cap.maxheight = (cam->upscaling && w9968cf_vpp)
d56410e0
MCC
2883 ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2884 : cam->maxheight;
1da177e4
LT
2885
2886 if (copy_to_user(arg, &cap, sizeof(cap)))
2887 return -EFAULT;
2888
2889 DBG(5, "VIDIOCGCAP successfully called")
2890 return 0;
2891 }
2892
2893 case VIDIOCGCHAN: /* get video channel informations */
2894 {
2895 struct video_channel chan;
2896 if (copy_from_user(&chan, arg, sizeof(chan)))
2897 return -EFAULT;
2898
2899 if (chan.channel != 0)
2900 return -EINVAL;
2901
2902 strcpy(chan.name, "Camera");
2903 chan.tuners = 0;
2904 chan.flags = 0;
2905 chan.type = VIDEO_TYPE_CAMERA;
2906 chan.norm = VIDEO_MODE_AUTO;
2907
2908 if (copy_to_user(arg, &chan, sizeof(chan)))
2909 return -EFAULT;
2910
2911 DBG(5, "VIDIOCGCHAN successfully called")
2912 return 0;
2913 }
2914
2915 case VIDIOCSCHAN: /* set active channel */
2916 {
2917 struct video_channel chan;
2918
2919 if (copy_from_user(&chan, arg, sizeof(chan)))
2920 return -EFAULT;
2921
2922 if (chan.channel != 0)
2923 return -EINVAL;
2924
2925 DBG(5, "VIDIOCSCHAN successfully called")
2926 return 0;
2927 }
2928
2929 case VIDIOCGPICT: /* get image properties of the picture */
2930 {
2931 if (w9968cf_sensor_get_picture(cam))
2932 return -EIO;
2933
2934 if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
2935 return -EFAULT;
2936
2937 DBG(5, "VIDIOCGPICT successfully called")
2938 return 0;
2939 }
2940
2941 case VIDIOCSPICT: /* change picture settings */
2942 {
2943 struct video_picture pict;
2944 int err = 0;
2945
2946 if (copy_from_user(&pict, arg, sizeof(pict)))
2947 return -EFAULT;
2948
d56410e0 2949 if ( (cam->force_palette || !w9968cf_vpp)
1da177e4
LT
2950 && pict.palette != cam->picture.palette ) {
2951 DBG(4, "Palette %s rejected: only %s is allowed",
2952 symbolic(v4l1_plist, pict.palette),
2953 symbolic(v4l1_plist, cam->picture.palette))
2954 return -EINVAL;
2955 }
2956
2957 if (!w9968cf_valid_palette(pict.palette)) {
2958 DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
2959 symbolic(v4l1_plist, pict.palette))
2960 return -EINVAL;
2961 }
2962
2963 if (!cam->force_palette) {
2964 if (cam->decompression == 0) {
2965 if (w9968cf_need_decompression(pict.palette)) {
d56410e0
MCC
2966 DBG(4, "Decompression disabled: palette %s is not "
2967 "allowed. VIDIOCSPICT failed",
2968 symbolic(v4l1_plist, pict.palette))
2969 return -EINVAL;
1da177e4
LT
2970 }
2971 } else if (cam->decompression == 1) {
2972 if (!w9968cf_need_decompression(pict.palette)) {
d56410e0
MCC
2973 DBG(4, "Decompression forced: palette %s is not "
2974 "allowed. VIDIOCSPICT failed",
2975 symbolic(v4l1_plist, pict.palette))
2976 return -EINVAL;
1da177e4
LT
2977 }
2978 }
2979 }
2980
2981 if (pict.depth != w9968cf_valid_depth(pict.palette)) {
2982 DBG(4, "Requested depth %u bpp is not valid for %s "
d56410e0 2983 "palette: ignored and changed to %u bpp",
1da177e4
LT
2984 pict.depth, symbolic(v4l1_plist, pict.palette),
2985 w9968cf_valid_depth(pict.palette))
2986 pict.depth = w9968cf_valid_depth(pict.palette);
2987 }
2988
2989 if (pict.palette != cam->picture.palette) {
2990 if(*cam->requested_frame
2991 || cam->frame_current->queued) {
2992 err = wait_event_interruptible
2993 ( cam->wait_queue,
d56410e0
MCC
2994 cam->disconnected ||
2995 (!*cam->requested_frame &&
2996 !cam->frame_current->queued) );
1da177e4
LT
2997 if (err)
2998 return err;
2999 if (cam->disconnected)
3000 return -ENODEV;
3001 }
3002
3003 if (w9968cf_stop_transfer(cam))
3004 goto ioctl_fail;
3005
3006 if (w9968cf_set_picture(cam, pict))
3007 goto ioctl_fail;
3008
3009 if (w9968cf_start_transfer(cam))
3010 goto ioctl_fail;
3011
3012 } else if (w9968cf_sensor_update_picture(cam, pict))
3013 return -EIO;
3014
3015
3016 DBG(5, "VIDIOCSPICT successfully called")
3017 return 0;
3018 }
3019
3020 case VIDIOCSWIN: /* set capture area */
3021 {
3022 struct video_window win;
3023 int err = 0;
3024
3025 if (copy_from_user(&win, arg, sizeof(win)))
3026 return -EFAULT;
3027
3028 DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
3029 "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
3030 win.x, win.y, win.width, win.height)
3031
3032 if (win.clipcount != 0 || win.flags != 0)
3033 return -EINVAL;
3034
1c657a99
TP
3035 if ((err = w9968cf_adjust_window_size(cam, &win.width,
3036 &win.height))) {
1da177e4
LT
3037 DBG(4, "Resolution not supported (%ux%u). "
3038 "VIDIOCSWIN failed", win.width, win.height)
3039 return err;
3040 }
3041
3042 if (win.x != cam->window.x ||
3043 win.y != cam->window.y ||
3044 win.width != cam->window.width ||
3045 win.height != cam->window.height) {
3046 if(*cam->requested_frame
3047 || cam->frame_current->queued) {
3048 err = wait_event_interruptible
3049 ( cam->wait_queue,
d56410e0
MCC
3050 cam->disconnected ||
3051 (!*cam->requested_frame &&
3052 !cam->frame_current->queued) );
1da177e4
LT
3053 if (err)
3054 return err;
3055 if (cam->disconnected)
3056 return -ENODEV;
3057 }
3058
3059 if (w9968cf_stop_transfer(cam))
3060 goto ioctl_fail;
3061
3062 /* This _must_ be called before set_window() */
3063 if (w9968cf_set_picture(cam, cam->picture))
3064 goto ioctl_fail;
3065
3066 if (w9968cf_set_window(cam, win))
3067 goto ioctl_fail;
3068
3069 if (w9968cf_start_transfer(cam))
3070 goto ioctl_fail;
3071 }
3072
3073 DBG(5, "VIDIOCSWIN successfully called. ")
3074 return 0;
3075 }
3076
3077 case VIDIOCGWIN: /* get current window properties */
3078 {
3079 if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
3080 return -EFAULT;
3081
3082 DBG(5, "VIDIOCGWIN successfully called")
3083 return 0;
3084 }
3085
3086 case VIDIOCGMBUF: /* request for memory (mapped) buffer */
3087 {
3088 struct video_mbuf mbuf;
3089 u8 i;
3090
3091 mbuf.size = cam->nbuffers * cam->frame[0].size;
3092 mbuf.frames = cam->nbuffers;
3093 for (i = 0; i < cam->nbuffers; i++)
3094 mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
d56410e0 3095 (unsigned long)cam->frame[0].buffer;
1da177e4
LT
3096
3097 if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
3098 return -EFAULT;
3099
3100 DBG(5, "VIDIOCGMBUF successfully called")
3101 return 0;
3102 }
3103
3104 case VIDIOCMCAPTURE: /* start the capture to a frame */
3105 {
3106 struct video_mmap mmap;
3107 struct w9968cf_frame_t* fr;
1c657a99 3108 u32 w, h;
1da177e4
LT
3109 int err = 0;
3110
3111 if (copy_from_user(&mmap, arg, sizeof(mmap)))
3112 return -EFAULT;
3113
3114 DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
d56410e0 3115 mmap.frame, symbolic(v4l1_plist, mmap.format),
1da177e4
LT
3116 mmap.width, mmap.height)
3117
3118 if (mmap.frame >= cam->nbuffers) {
3119 DBG(4, "Invalid frame number (%u). "
3120 "VIDIOCMCAPTURE failed", mmap.frame)
3121 return -EINVAL;
3122 }
3123
d56410e0 3124 if (mmap.format!=cam->picture.palette &&
1da177e4
LT
3125 (cam->force_palette || !w9968cf_vpp)) {
3126 DBG(4, "Palette %s rejected: only %s is allowed",
3127 symbolic(v4l1_plist, mmap.format),
3128 symbolic(v4l1_plist, cam->picture.palette))
3129 return -EINVAL;
3130 }
3131
3132 if (!w9968cf_valid_palette(mmap.format)) {
3133 DBG(4, "Palette %s not supported. "
d56410e0 3134 "VIDIOCMCAPTURE failed",
1da177e4
LT
3135 symbolic(v4l1_plist, mmap.format))
3136 return -EINVAL;
3137 }
3138
3139 if (!cam->force_palette) {
3140 if (cam->decompression == 0) {
3141 if (w9968cf_need_decompression(mmap.format)) {
d56410e0
MCC
3142 DBG(4, "Decompression disabled: palette %s is not "
3143 "allowed. VIDIOCSPICT failed",
3144 symbolic(v4l1_plist, mmap.format))
3145 return -EINVAL;
1da177e4
LT
3146 }
3147 } else if (cam->decompression == 1) {
3148 if (!w9968cf_need_decompression(mmap.format)) {
d56410e0
MCC
3149 DBG(4, "Decompression forced: palette %s is not "
3150 "allowed. VIDIOCSPICT failed",
3151 symbolic(v4l1_plist, mmap.format))
3152 return -EINVAL;
1da177e4
LT
3153 }
3154 }
3155 }
3156
1c657a99
TP
3157 w = mmap.width; h = mmap.height;
3158 err = w9968cf_adjust_window_size(cam, &w, &h);
3159 mmap.width = w; mmap.height = h;
3160 if (err) {
1da177e4
LT
3161 DBG(4, "Resolution not supported (%dx%d). "
3162 "VIDIOCMCAPTURE failed",
3163 mmap.width, mmap.height)
3164 return err;
3165 }
3166
3167 fr = &cam->frame[mmap.frame];
3168
3169 if (mmap.width != cam->window.width ||
3170 mmap.height != cam->window.height ||
3171 mmap.format != cam->picture.palette) {
3172
3173 struct video_window win;
3174 struct video_picture pict;
3175
3176 if(*cam->requested_frame
3177 || cam->frame_current->queued) {
3178 DBG(6, "VIDIOCMCAPTURE. Change settings for "
3179 "frame #%u: %dx%d, format %s. Wait...",
3180 mmap.frame, mmap.width, mmap.height,
d56410e0 3181 symbolic(v4l1_plist, mmap.format))
1da177e4
LT
3182 err = wait_event_interruptible
3183 ( cam->wait_queue,
d56410e0
MCC
3184 cam->disconnected ||
3185 (!*cam->requested_frame &&
3186 !cam->frame_current->queued) );
1da177e4
LT
3187 if (err)
3188 return err;
3189 if (cam->disconnected)
3190 return -ENODEV;
3191 }
3192
3193 memcpy(&win, &cam->window, sizeof(win));
3194 memcpy(&pict, &cam->picture, sizeof(pict));
3195 win.width = mmap.width;
3196 win.height = mmap.height;
3197 pict.palette = mmap.format;
3198
3199 if (w9968cf_stop_transfer(cam))
3200 goto ioctl_fail;
3201
3202 /* This before set_window */
d56410e0 3203 if (w9968cf_set_picture(cam, pict))
1da177e4
LT
3204 goto ioctl_fail;
3205
3206 if (w9968cf_set_window(cam, win))
3207 goto ioctl_fail;
3208
3209 if (w9968cf_start_transfer(cam))
3210 goto ioctl_fail;
3211
3212 } else if (fr->queued) {
3213
3214 DBG(6, "Wait until frame #%u is free", mmap.frame)
d56410e0
MCC
3215
3216 err = wait_event_interruptible(cam->wait_queue,
3217 cam->disconnected ||
3218 (!fr->queued));
1da177e4
LT
3219 if (err)
3220 return err;
3221 if (cam->disconnected)
3222 return -ENODEV;
3223 }
3224
3225 w9968cf_push_frame(cam, mmap.frame);
3226 DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
3227 return 0;
3228 }
3229
3230 case VIDIOCSYNC: /* wait until the capture of a frame is finished */
3231 {
3232 unsigned int f_num;
3233 struct w9968cf_frame_t* fr;
3234 int err = 0;
3235
3236 if (copy_from_user(&f_num, arg, sizeof(f_num)))
3237 return -EFAULT;
3238
3239 if (f_num >= cam->nbuffers) {
3240 DBG(4, "Invalid frame number (%u). "
3241 "VIDIOCMCAPTURE failed", f_num)
3242 return -EINVAL;
3243 }
3244
3245 DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
3246
3247 fr = &cam->frame[f_num];
3248
3249 switch (fr->status) {
3250 case F_UNUSED:
3251 if (!fr->queued) {
3252 DBG(4, "VIDIOSYNC: Frame #%u not requested!",
3253 f_num)
3254 return -EFAULT;
3255 }
3256 case F_ERROR:
3257 case F_GRABBING:
d56410e0
MCC
3258 err = wait_event_interruptible(cam->wait_queue,
3259 (fr->status == F_READY)
3260 || cam->disconnected);
1da177e4
LT
3261 if (err)
3262 return err;
3263 if (cam->disconnected)
3264 return -ENODEV;
3265 break;
3266 case F_READY:
3267 break;
3268 }
3269
3270 if (w9968cf_vpp)
3271 w9968cf_postprocess_frame(cam, fr);
3272
3273 fr->status = F_UNUSED;
3274
3275 DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
3276 return 0;
3277 }
3278
3279 case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
3280 {
3281 struct video_unit unit = {
3282 .video = cam->v4ldev->minor,
3283 .vbi = VIDEO_NO_UNIT,
3284 .radio = VIDEO_NO_UNIT,
3285 .audio = VIDEO_NO_UNIT,
3286 .teletext = VIDEO_NO_UNIT,
3287 };
3288
3289 if (copy_to_user(arg, &unit, sizeof(unit)))
3290 return -EFAULT;
3291
3292 DBG(5, "VIDIOCGUNIT successfully called")
3293 return 0;
3294 }
3295
3296 case VIDIOCKEY:
3297 return 0;
3298
3299 case VIDIOCGFBUF:
3300 {
3301 if (clear_user(arg, sizeof(struct video_buffer)))
3302 return -EFAULT;
3303
3304 DBG(5, "VIDIOCGFBUF successfully called")
3305 return 0;
3306 }
3307
3308 case VIDIOCGTUNER:
3309 {
3310 struct video_tuner tuner;
3311 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3312 return -EFAULT;
3313
3314 if (tuner.tuner != 0)
3315 return -EINVAL;
3316
3317 strcpy(tuner.name, "no_tuner");
3318 tuner.rangelow = 0;
3319 tuner.rangehigh = 0;
3320 tuner.flags = VIDEO_TUNER_NORM;
3321 tuner.mode = VIDEO_MODE_AUTO;
3322 tuner.signal = 0xffff;
3323
3324 if (copy_to_user(arg, &tuner, sizeof(tuner)))
3325 return -EFAULT;
3326
3327 DBG(5, "VIDIOCGTUNER successfully called")
3328 return 0;
3329 }
3330
3331 case VIDIOCSTUNER:
3332 {
3333 struct video_tuner tuner;
3334 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3335 return -EFAULT;
3336
3337 if (tuner.tuner != 0)
3338 return -EINVAL;
3339
3340 if (tuner.mode != VIDEO_MODE_AUTO)
3341 return -EINVAL;
3342
3343 DBG(5, "VIDIOCSTUNER successfully called")
3344 return 0;
3345 }
3346
3347 case VIDIOCSFBUF:
3348 case VIDIOCCAPTURE:
3349 case VIDIOCGFREQ:
3350 case VIDIOCSFREQ:
3351 case VIDIOCGAUDIO:
3352 case VIDIOCSAUDIO:
3353 case VIDIOCSPLAYMODE:
3354 case VIDIOCSWRITEMODE:
3355 case VIDIOCGPLAYINFO:
3356 case VIDIOCSMICROCODE:
3357 case VIDIOCGVBIFMT:
3358 case VIDIOCSVBIFMT:
3359 DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
3360 "(type 0x%01X, "
3361 "n. 0x%01X, "
d56410e0 3362 "dir. 0x%01X, "
1da177e4
LT
3363 "size 0x%02X)",
3364 V4L1_IOCTL(cmd),
3365 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3366
3367 return -EINVAL;
3368
3369 default:
3370 DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
3371 "type 0x%01X, "
3372 "n. 0x%01X, "
3373 "dir. 0x%01X, "
3374 "size 0x%02X",
3375 V4L1_IOCTL(cmd),
3376 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3377
3378 return -ENOIOCTLCMD;
3379
3380 } /* end of switch */
3381
3382ioctl_fail:
3383 cam->misconfigured = 1;
3384 DBG(1, "VIDIOC%s failed because of hardware problems. "
3385 "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
3386 return -EFAULT;
3387}
3388
3389
bec43661 3390static const struct v4l2_file_operations w9968cf_fops = {
1da177e4
LT
3391 .owner = THIS_MODULE,
3392 .open = w9968cf_open,
3393 .release = w9968cf_release,
3394 .read = w9968cf_read,
3395 .ioctl = w9968cf_ioctl,
3396 .mmap = w9968cf_mmap,
1da177e4
LT
3397};
3398
3399
3400
3401/****************************************************************************
3402 * USB probe and V4L registration, disconnect and id_table[] definition *
3403 ****************************************************************************/
3404
3405static int
3406w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3407{
3408 struct usb_device *udev = interface_to_usbdev(intf);
3409 struct w9968cf_device* cam;
3410 int err = 0;
3411 enum w9968cf_model_id mod_id;
3412 struct list_head* ptr;
3413 u8 sc = 0; /* number of simultaneous cameras */
ff699e6b 3414 static unsigned short dev_nr; /* 0 - we are handling device number n */
3160fbc5
HV
3415 static unsigned short addrs[] = {
3416 OV7xx0_SID,
3417 OV6xx0_SID,
3418 I2C_CLIENT_END
3419 };
1da177e4
LT
3420
3421 if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[0].idVendor &&
3422 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[0].idProduct)
3423 mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
3424 else if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[1].idVendor &&
d56410e0 3425 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[1].idProduct)
1da177e4
LT
3426 mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
3427 else
3428 return -ENODEV;
3429
3430 cam = (struct w9968cf_device*)
d56410e0 3431 kzalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
1da177e4
LT
3432 if (!cam)
3433 return -ENOMEM;
3434
119753a4 3435 err = v4l2_device_register(&intf->dev, &cam->v4l2_dev);
4e06839f
HV
3436 if (err)
3437 goto fail0;
3438
4186ecf8
AV
3439 mutex_init(&cam->dev_mutex);
3440 mutex_lock(&cam->dev_mutex);
1da177e4
LT
3441
3442 cam->usbdev = udev;
1da177e4
LT
3443
3444 DBG(2, "%s detected", symbolic(camlist, mod_id))
3445
3446 if (simcams > W9968CF_MAX_DEVICES)
3447 simcams = W9968CF_SIMCAMS;
3448
3449 /* How many cameras are connected ? */
4186ecf8 3450 mutex_lock(&w9968cf_devlist_mutex);
1da177e4
LT
3451 list_for_each(ptr, &w9968cf_dev_list)
3452 sc++;
4186ecf8 3453 mutex_unlock(&w9968cf_devlist_mutex);
1da177e4
LT
3454
3455 if (sc >= simcams) {
3456 DBG(2, "Device rejected: too many connected cameras "
3457 "(max. %u)", simcams)
3458 err = -EPERM;
3459 goto fail;
3460 }
3461
3462
3463 /* Allocate 2 bytes of memory for camera control USB transfers */
b10b4177 3464 if (!(cam->control_buffer = kzalloc(2, GFP_KERNEL))) {
1da177e4
LT
3465 DBG(1,"Couldn't allocate memory for camera control transfers")
3466 err = -ENOMEM;
3467 goto fail;
3468 }
1da177e4
LT
3469
3470 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
b10b4177 3471 if (!(cam->data_buffer = kzalloc(8, GFP_KERNEL))) {
1da177e4
LT
3472 DBG(1, "Couldn't allocate memory for data "
3473 "transfers to the FSB")
3474 err = -ENOMEM;
3475 goto fail;
3476 }
1da177e4
LT
3477
3478 /* Register the V4L device */
3479 cam->v4ldev = video_device_alloc();
3480 if (!cam->v4ldev) {
3481 DBG(1, "Could not allocate memory for a V4L structure")
3482 err = -ENOMEM;
3483 goto fail;
3484 }
3485
3486 strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
1da177e4
LT
3487 cam->v4ldev->fops = &w9968cf_fops;
3488 cam->v4ldev->minor = video_nr[dev_nr];
3489 cam->v4ldev->release = video_device_release;
3490 video_set_drvdata(cam->v4ldev, cam);
4e06839f 3491 cam->v4ldev->v4l2_dev = &cam->v4l2_dev;
1da177e4
LT
3492
3493 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
d56410e0 3494 video_nr[dev_nr]);
1da177e4
LT
3495 if (err) {
3496 DBG(1, "V4L device registration failed")
3497 if (err == -ENFILE && video_nr[dev_nr] == -1)
3498 DBG(2, "Couldn't find a free /dev/videoX node")
3499 video_nr[dev_nr] = -1;
3500 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3501 goto fail;
3502 }
3503
c6330fb8 3504 DBG(2, "V4L device registered as /dev/video%d", cam->v4ldev->num)
1da177e4
LT
3505
3506 /* Set some basic constants */
3507 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3508
3509 /* Add a new entry into the list of V4L registered devices */
4186ecf8 3510 mutex_lock(&w9968cf_devlist_mutex);
1da177e4 3511 list_add(&cam->v4llist, &w9968cf_dev_list);
4186ecf8 3512 mutex_unlock(&w9968cf_devlist_mutex);
1da177e4
LT
3513 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3514
3515 w9968cf_turn_on_led(cam);
3516
3517 w9968cf_i2c_init(cam);
53dacb15 3518 cam->sensor_sd = v4l2_i2c_new_subdev(&cam->v4l2_dev,
e6574f2f 3519 &cam->i2c_adapter,
53dacb15 3520 "ovcamchip", "ovcamchip", 0, addrs);
1da177e4
LT
3521
3522 usb_set_intfdata(intf, cam);
4186ecf8 3523 mutex_unlock(&cam->dev_mutex);
4e06839f 3524
3160fbc5 3525 err = w9968cf_sensor_init(cam);
1da177e4
LT
3526 return 0;
3527
3528fail: /* Free unused memory */
1bc3c9e1
JJ
3529 kfree(cam->control_buffer);
3530 kfree(cam->data_buffer);
1da177e4
LT
3531 if (cam->v4ldev)
3532 video_device_release(cam->v4ldev);
4186ecf8 3533 mutex_unlock(&cam->dev_mutex);
4e06839f
HV
3534 v4l2_device_unregister(&cam->v4l2_dev);
3535fail0:
1da177e4
LT
3536 kfree(cam);
3537 return err;
3538}
3539
3540
3541static void w9968cf_usb_disconnect(struct usb_interface* intf)
3542{
d56410e0 3543 struct w9968cf_device* cam =
1da177e4
LT
3544 (struct w9968cf_device*)usb_get_intfdata(intf);
3545
1da177e4 3546 if (cam) {
3160fbc5 3547 down_write(&w9968cf_disconnect);
1da177e4 3548 /* Prevent concurrent accesses to data */
4186ecf8 3549 mutex_lock(&cam->dev_mutex);
1da177e4
LT
3550
3551 cam->disconnected = 1;
3552
f1ba28c3
HV
3553 DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id));
3554
3555 v4l2_device_disconnect(&cam->v4l2_dev);
1da177e4
LT
3556
3557 wake_up_interruptible_all(&cam->open);
3558
3559 if (cam->users) {
3560 DBG(2, "The device is open (/dev/video%d)! "
3561 "Process name: %s. Deregistration and memory "
3562 "deallocation are deferred on close.",
c6330fb8 3563 cam->v4ldev->num, cam->command)
1da177e4
LT
3564 cam->misconfigured = 1;
3565 w9968cf_stop_transfer(cam);
3566 wake_up_interruptible(&cam->wait_queue);
3567 } else
3568 w9968cf_release_resources(cam);
3569
4186ecf8 3570 mutex_unlock(&cam->dev_mutex);
3160fbc5 3571 up_write(&w9968cf_disconnect);
1da177e4 3572
4e06839f 3573 if (!cam->users) {
1da177e4 3574 kfree(cam);
4e06839f 3575 }
1da177e4 3576 }
1da177e4
LT
3577}
3578
3579
3580static struct usb_driver w9968cf_usb_driver = {
1da177e4
LT
3581 .name = "w9968cf",
3582 .id_table = winbond_id_table,
3583 .probe = w9968cf_usb_probe,
3584 .disconnect = w9968cf_usb_disconnect,
3585};
3586
3587
3588
3589/****************************************************************************
3590 * Module init, exit and intermodule communication *
3591 ****************************************************************************/
3592
1da177e4
LT
3593static int __init w9968cf_module_init(void)
3594{
3595 int err;
3596
3597 KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
3598 KDBG(3, W9968CF_MODULE_AUTHOR)
3599
1da177e4
LT
3600 if ((err = usb_register(&w9968cf_usb_driver)))
3601 return err;
3602
3603 return 0;
3604}
3605
3606
3607static void __exit w9968cf_module_exit(void)
3608{
3609 /* w9968cf_usb_disconnect() will be called */
3610 usb_deregister(&w9968cf_usb_driver);
3611
3612 KDBG(2, W9968CF_MODULE_NAME" deregistered")
3613}
3614
3615
3616module_init(w9968cf_module_init);
3617module_exit(w9968cf_module_exit);
3618
This page took 0.752161 seconds and 5 git commands to generate.