blob: 5a1b5f5a7d463e11cab98c6197b38646e18101f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/***************************************************************************
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "w9968cf.h"
47#include "w9968cf_decoder.h"
48
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010049static struct w9968cf_vpp_t* w9968cf_vpp;
50static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
51
52static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
53static DEFINE_MUTEX(w9968cf_devlist_mutex); /* semaphore for list traversal */
54
55static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57
58/****************************************************************************
59 * Module macros and parameters *
60 ****************************************************************************/
61
62MODULE_DEVICE_TABLE(usb, winbond_id_table);
63
64MODULE_AUTHOR(W9968CF_MODULE_AUTHOR" "W9968CF_AUTHOR_EMAIL);
65MODULE_DESCRIPTION(W9968CF_MODULE_NAME);
66MODULE_VERSION(W9968CF_MODULE_VERSION);
67MODULE_LICENSE(W9968CF_MODULE_LICENSE);
68MODULE_SUPPORTED_DEVICE("Video");
69
70static int ovmod_load = W9968CF_OVMOD_LOAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static unsigned short simcams = W9968CF_SIMCAMS;
72static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030073static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
74 W9968CF_PACKET_SIZE};
75static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
76 W9968CF_BUFFERS};
77static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
78 W9968CF_DOUBLE_BUFFER};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030080static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
81 W9968CF_FILTER_TYPE};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030083static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
84 W9968CF_DECOMPRESSION};
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
86static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
87static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
88static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
89static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030090static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
91 W9968CF_LIGHTFREQ};
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030093 W9968CF_BANDINGFILTER};
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
95static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
96static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
97static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030098static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
99 W9968CF_BRIGHTNESS};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
101static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300102static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
103 W9968CF_CONTRAST};
104static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
105 W9968CF_WHITENESS};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#ifdef W9968CF_DEBUG
107static unsigned short debug = W9968CF_DEBUG_LEVEL;
108static int specific_debug = W9968CF_SPECIFIC_DEBUG;
109#endif
110
111static unsigned int param_nv[24]; /* number of values per parameter */
112
113#ifdef CONFIG_KMOD
114module_param(ovmod_load, bool, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#endif
116module_param(simcams, ushort, 0644);
117module_param_array(video_nr, short, &param_nv[0], 0444);
118module_param_array(packet_size, uint, &param_nv[1], 0444);
119module_param_array(max_buffers, ushort, &param_nv[2], 0444);
120module_param_array(double_buffer, bool, &param_nv[3], 0444);
121module_param_array(clamping, bool, &param_nv[4], 0444);
122module_param_array(filter_type, ushort, &param_nv[5], 0444);
123module_param_array(largeview, bool, &param_nv[6], 0444);
124module_param_array(decompression, ushort, &param_nv[7], 0444);
125module_param_array(upscaling, bool, &param_nv[8], 0444);
126module_param_array(force_palette, ushort, &param_nv[9], 0444);
127module_param_array(force_rgb, ushort, &param_nv[10], 0444);
128module_param_array(autobright, bool, &param_nv[11], 0444);
129module_param_array(autoexp, bool, &param_nv[12], 0444);
130module_param_array(lightfreq, ushort, &param_nv[13], 0444);
131module_param_array(bandingfilter, bool, &param_nv[14], 0444);
132module_param_array(clockdiv, short, &param_nv[15], 0444);
133module_param_array(backlight, bool, &param_nv[16], 0444);
134module_param_array(mirror, bool, &param_nv[17], 0444);
135module_param_array(monochrome, bool, &param_nv[18], 0444);
136module_param_array(brightness, uint, &param_nv[19], 0444);
137module_param_array(hue, uint, &param_nv[20], 0444);
138module_param_array(colour, uint, &param_nv[21], 0444);
139module_param_array(contrast, uint, &param_nv[22], 0444);
140module_param_array(whiteness, uint, &param_nv[23], 0444);
141#ifdef W9968CF_DEBUG
142module_param(debug, ushort, 0644);
143module_param(specific_debug, bool, 0644);
144#endif
145
146#ifdef CONFIG_KMOD
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300147MODULE_PARM_DESC(ovmod_load,
148 "\n<0|1> Automatic 'ovcamchip' module loading."
149 "\n0 disabled, 1 enabled."
150 "\nIf enabled,'insmod' searches for the required 'ovcamchip'"
151 "\nmodule in the system, according to its configuration, and"
152 "\nattempts to load that module automatically. This action is"
153 "\nperformed once as soon as the 'w9968cf' module is loaded"
154 "\ninto memory."
155 "\nDefault value is "__MODULE_STRING(W9968CF_OVMOD_LOAD)"."
156 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#endif
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300158MODULE_PARM_DESC(simcams,
159 "\n<n> Number of cameras allowed to stream simultaneously."
160 "\nn may vary from 0 to "
161 __MODULE_STRING(W9968CF_MAX_DEVICES)"."
162 "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
163 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164MODULE_PARM_DESC(video_nr,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300165 "\n<-1|n[,...]> Specify V4L minor mode number."
166 "\n -1 = use next available (default)"
167 "\n n = use minor number n (integer >= 0)"
168 "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
169 " cameras this way."
170 "\nFor example:"
171 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
172 "\nthe second camera and use auto for the first"
173 "\none and for every other camera."
174 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175MODULE_PARM_DESC(packet_size,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300176 "\n<n[,...]> Specify the maximum data payload"
177 "\nsize in bytes for alternate settings, for each device."
178 "\nn is scaled between 63 and 1023 "
179 "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
180 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181MODULE_PARM_DESC(max_buffers,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300182 "\n<n[,...]> For advanced users."
183 "\nSpecify the maximum number of video frame buffers"
184 "\nto allocate for each device, from 2 to "
185 __MODULE_STRING(W9968CF_MAX_BUFFERS)
186 ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
187 "\n");
188MODULE_PARM_DESC(double_buffer,
189 "\n<0|1[,...]> "
190 "Hardware double buffering: 0 disabled, 1 enabled."
191 "\nIt should be enabled if you want smooth video output: if"
192 "\nyou obtain out of sync. video, disable it, or try to"
193 "\ndecrease the 'clockdiv' module parameter value."
194 "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
195 " for every device."
196 "\n");
197MODULE_PARM_DESC(clamping,
198 "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
199 "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
200 " for every device."
201 "\n");
202MODULE_PARM_DESC(filter_type,
203 "\n<0|1|2[,...]> Video filter type."
204 "\n0 none, 1 (1-2-1) 3-tap filter, "
205 "2 (2-3-6-3-2) 5-tap filter."
206 "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
207 " for every device."
208 "\nThe filter is used to reduce noise and aliasing artifacts"
209 "\nproduced by the CCD or CMOS image sensor, and the scaling"
210 " process."
211 "\n");
212MODULE_PARM_DESC(largeview,
213 "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
214 "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
215 " for every device."
216 "\n");
217MODULE_PARM_DESC(upscaling,
218 "\n<0|1[,...]> Software scaling (for non-compressed video):"
219 "\n0 disabled, 1 enabled."
220 "\nDisable it if you have a slow CPU or you don't have"
221 " enough memory."
222 "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
223 " for every device."
224 "\nIf 'w9968cf-vpp' is not present, this parameter is"
225 " set to 0."
226 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227MODULE_PARM_DESC(decompression,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300228 "\n<0|1|2[,...]> Software video decompression:"
229 "\n- 0 disables decompression (doesn't allow formats needing"
230 " decompression)"
231 "\n- 1 forces decompression (allows formats needing"
232 " decompression only);"
233 "\n- 2 allows any permitted formats."
234 "\nFormats supporting compressed video are YUV422P and"
235 " YUV420P/YUV420 "
236 "\nin any resolutions where both width and height are "
237 "a multiple of 16."
238 "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
239 " for every device."
240 "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
241 "\nnot allowed; in this case this parameter is set to 2."
242 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243MODULE_PARM_DESC(force_palette,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300244 "\n<0"
245 "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
246 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
247 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
248 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
249 "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
250 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
251 "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
252 "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
253 "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
254 "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
255 "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
256 "[,...]>"
257 " Force picture palette."
258 "\nIn order:"
259 "\n- 0 allows any of the following formats:"
260 "\n- UYVY 16 bpp - Original video, compression disabled"
261 "\n- YUV420 12 bpp - Original video, compression enabled"
262 "\n- YUV422P 16 bpp - Original video, compression enabled"
263 "\n- YUV420P 12 bpp - Original video, compression enabled"
264 "\n- YUVY 16 bpp - Software conversion from UYVY"
265 "\n- YUV422 16 bpp - Software conversion from UYVY"
266 "\n- GREY 8 bpp - Software conversion from UYVY"
267 "\n- RGB555 16 bpp - Software conversion from UYVY"
268 "\n- RGB565 16 bpp - Software conversion from UYVY"
269 "\n- RGB24 24 bpp - Software conversion from UYVY"
270 "\n- RGB32 32 bpp - Software conversion from UYVY"
271 "\nWhen not 0, this parameter will override 'decompression'."
272 "\nDefault value is 0 for every device."
273 "\nInitial palette is "
274 __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
275 "\nIf 'w9968cf-vpp' is not present, this parameter is"
276 " set to 9 (UYVY)."
277 "\n");
278MODULE_PARM_DESC(force_rgb,
279 "\n<0|1[,...]> Read RGB video data instead of BGR:"
280 "\n 1 = use RGB component ordering."
281 "\n 0 = use BGR component ordering."
282 "\nThis parameter has effect when using RGBX palettes only."
283 "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
284 " for every device."
285 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286MODULE_PARM_DESC(autobright,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300287 "\n<0|1[,...]> Image sensor automatically changes brightness:"
288 "\n 0 = no, 1 = yes"
289 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
290 " for every device."
291 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292MODULE_PARM_DESC(autoexp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300293 "\n<0|1[,...]> Image sensor automatically changes exposure:"
294 "\n 0 = no, 1 = yes"
295 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
296 " for every device."
297 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298MODULE_PARM_DESC(lightfreq,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300299 "\n<50|60[,...]> Light frequency in Hz:"
300 "\n 50 for European and Asian lighting,"
301 " 60 for American lighting."
302 "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
303 " for every device."
304 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305MODULE_PARM_DESC(bandingfilter,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300306 "\n<0|1[,...]> Banding filter to reduce effects of"
307 " fluorescent lighting:"
308 "\n 0 disabled, 1 enabled."
309 "\nThis filter tries to reduce the pattern of horizontal"
310 "\nlight/dark bands caused by some (usually fluorescent)"
311 " lighting."
312 "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
313 " for every device."
314 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315MODULE_PARM_DESC(clockdiv,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300316 "\n<-1|n[,...]> "
317 "Force pixel clock divisor to a specific value (for experts):"
318 "\n n may vary from 0 to 127."
319 "\n -1 for automatic value."
320 "\nSee also the 'double_buffer' module parameter."
321 "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
322 " for every device."
323 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324MODULE_PARM_DESC(backlight,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300325 "\n<0|1[,...]> Objects are lit from behind:"
326 "\n 0 = no, 1 = yes"
327 "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
328 " for every device."
329 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330MODULE_PARM_DESC(mirror,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300331 "\n<0|1[,...]> Reverse image horizontally:"
332 "\n 0 = no, 1 = yes"
333 "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
334 " for every device."
335 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336MODULE_PARM_DESC(monochrome,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300337 "\n<0|1[,...]> Use image sensor as monochrome sensor:"
338 "\n 0 = no, 1 = yes"
339 "\nNot all the sensors support monochrome color."
340 "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
341 " for every device."
342 "\n");
343MODULE_PARM_DESC(brightness,
344 "\n<n[,...]> Set picture brightness (0-65535)."
345 "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
346 " for every device."
347 "\nThis parameter has no effect if 'autobright' is enabled."
348 "\n");
349MODULE_PARM_DESC(hue,
350 "\n<n[,...]> Set picture hue (0-65535)."
351 "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
352 " for every device."
353 "\n");
354MODULE_PARM_DESC(colour,
355 "\n<n[,...]> Set picture saturation (0-65535)."
356 "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
357 " for every device."
358 "\n");
359MODULE_PARM_DESC(contrast,
360 "\n<n[,...]> Set picture contrast (0-65535)."
361 "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
362 " for every device."
363 "\n");
364MODULE_PARM_DESC(whiteness,
365 "\n<n[,...]> Set picture whiteness (0-65535)."
366 "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
367 " for every device."
368 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#ifdef W9968CF_DEBUG
370MODULE_PARM_DESC(debug,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300371 "\n<n> Debugging information level, from 0 to 6:"
372 "\n0 = none (use carefully)"
373 "\n1 = critical errors"
374 "\n2 = significant informations"
375 "\n3 = configuration or general messages"
376 "\n4 = warnings"
377 "\n5 = called functions"
378 "\n6 = function internals"
379 "\nLevel 5 and 6 are useful for testing only, when only "
380 "one device is used."
381 "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
382 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383MODULE_PARM_DESC(specific_debug,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300384 "\n<0|1> Enable or disable specific debugging messages:"
385 "\n0 = print messages concerning every level"
386 " <= 'debug' level."
387 "\n1 = print messages concerning the level"
388 " indicated by 'debug'."
389 "\nDefault value is "
390 __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
391 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#endif /* W9968CF_DEBUG */
393
394
395
396/****************************************************************************
397 * Some prototypes *
398 ****************************************************************************/
399
400/* Video4linux interface */
Arjan van de Venfa027c22007-02-12 00:55:33 -0800401static const struct file_operations w9968cf_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static int w9968cf_open(struct inode*, struct file*);
403static int w9968cf_release(struct inode*, struct file*);
404static int w9968cf_mmap(struct file*, struct vm_area_struct*);
405static int w9968cf_ioctl(struct inode*, struct file*, unsigned, unsigned long);
406static ssize_t w9968cf_read(struct file*, char __user *, size_t, loff_t*);
407static int w9968cf_v4l_ioctl(struct inode*, struct file*, unsigned int,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300408 void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410/* USB-specific */
411static int w9968cf_start_transfer(struct w9968cf_device*);
412static int w9968cf_stop_transfer(struct w9968cf_device*);
413static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
414static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
415static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
416static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
417static int w9968cf_read_sb(struct w9968cf_device*);
418static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
David Howells7d12e782006-10-05 14:55:46 +0100419static void w9968cf_urb_complete(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421/* Low-level I2C (SMBus) I/O */
422static int w9968cf_smbus_start(struct w9968cf_device*);
423static int w9968cf_smbus_stop(struct w9968cf_device*);
424static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
425static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
426static int w9968cf_smbus_write_ack(struct w9968cf_device*);
427static int w9968cf_smbus_read_ack(struct w9968cf_device*);
428static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
429static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300430 u16 address, u8* value);
431static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
432 u8 subaddress, u8* value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300434 u16 address, u8 subaddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300436 u16 address, u8 subaddress,
437 u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439/* I2C interface to kernel */
440static int w9968cf_i2c_init(struct w9968cf_device*);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300441static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
442 unsigned short flags, char read_write,
443 u8 command, int size, union i2c_smbus_data*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static u32 w9968cf_i2c_func(struct i2c_adapter*);
445static int w9968cf_i2c_attach_inform(struct i2c_client*);
446static int w9968cf_i2c_detach_inform(struct i2c_client*);
447static int w9968cf_i2c_control(struct i2c_adapter*, unsigned int cmd,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300448 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450/* Memory management */
451static void* rvmalloc(unsigned long size);
452static void rvfree(void *mem, unsigned long size);
453static void w9968cf_deallocate_memory(struct w9968cf_device*);
454static int w9968cf_allocate_memory(struct w9968cf_device*);
455
456/* High-level image sensor control functions */
457static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
458static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
459static int w9968cf_sensor_cmd(struct w9968cf_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300460 unsigned int cmd, void *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static int w9968cf_sensor_init(struct w9968cf_device*);
462static int w9968cf_sensor_update_settings(struct w9968cf_device*);
463static int w9968cf_sensor_get_picture(struct w9968cf_device*);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300464static int w9968cf_sensor_update_picture(struct w9968cf_device*,
465 struct video_picture pict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467/* Other helper functions */
468static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300469 enum w9968cf_model_id,
470 const unsigned short dev_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471static void w9968cf_adjust_configuration(struct w9968cf_device*);
472static int w9968cf_turn_on_led(struct w9968cf_device*);
473static int w9968cf_init_chip(struct w9968cf_device*);
474static inline u16 w9968cf_valid_palette(u16 palette);
475static inline u16 w9968cf_valid_depth(u16 palette);
476static inline u8 w9968cf_need_decompression(u16 palette);
477static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
478static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300479static int w9968cf_postprocess_frame(struct w9968cf_device*,
480 struct w9968cf_frame_t*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static int w9968cf_adjust_window_size(struct w9968cf_device*, u16* w, u16* h);
482static void w9968cf_init_framelist(struct w9968cf_device*);
483static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
484static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
485static void w9968cf_release_resources(struct w9968cf_device*);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488
489/****************************************************************************
490 * Symbolic names *
491 ****************************************************************************/
492
493/* Used to represent a list of values and their respective symbolic names */
494struct w9968cf_symbolic_list {
495 const int num;
496 const char *name;
497};
498
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300499/*--------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 Returns the name of the matching element in the symbolic_list array. The
501 end of the list must be marked with an element that has a NULL name.
502 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300503static inline const char *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504symbolic(struct w9968cf_symbolic_list list[], const int num)
505{
506 int i;
507
508 for (i = 0; list[i].name != NULL; i++)
509 if (list[i].num == num)
510 return (list[i].name);
511
512 return "Unknown";
513}
514
515static struct w9968cf_symbolic_list camlist[] = {
516 { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
517 { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
518
519 /* Other cameras (having the same descriptors as Generic W996[87]CF) */
520 { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
521 { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
522 { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
523 { W9968CF_MOD_LL, "Lebon LDC-035A" },
524 { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
525 { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
526 { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
527 { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
528 { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
529
530 { -1, NULL }
531};
532
533static struct w9968cf_symbolic_list senlist[] = {
534 { CC_OV76BE, "OV76BE" },
535 { CC_OV7610, "OV7610" },
536 { CC_OV7620, "OV7620" },
537 { CC_OV7620AE, "OV7620AE" },
538 { CC_OV6620, "OV6620" },
539 { CC_OV6630, "OV6630" },
540 { CC_OV6630AE, "OV6630AE" },
541 { CC_OV6630AF, "OV6630AF" },
542 { -1, NULL }
543};
544
545/* Video4Linux1 palettes */
546static struct w9968cf_symbolic_list v4l1_plist[] = {
547 { VIDEO_PALETTE_GREY, "GREY" },
548 { VIDEO_PALETTE_HI240, "HI240" },
549 { VIDEO_PALETTE_RGB565, "RGB565" },
550 { VIDEO_PALETTE_RGB24, "RGB24" },
551 { VIDEO_PALETTE_RGB32, "RGB32" },
552 { VIDEO_PALETTE_RGB555, "RGB555" },
553 { VIDEO_PALETTE_YUV422, "YUV422" },
554 { VIDEO_PALETTE_YUYV, "YUYV" },
555 { VIDEO_PALETTE_UYVY, "UYVY" },
556 { VIDEO_PALETTE_YUV420, "YUV420" },
557 { VIDEO_PALETTE_YUV411, "YUV411" },
558 { VIDEO_PALETTE_RAW, "RAW" },
559 { VIDEO_PALETTE_YUV422P, "YUV422P" },
560 { VIDEO_PALETTE_YUV411P, "YUV411P" },
561 { VIDEO_PALETTE_YUV420P, "YUV420P" },
562 { VIDEO_PALETTE_YUV410P, "YUV410P" },
563 { -1, NULL }
564};
565
566/* Decoder error codes: */
567static struct w9968cf_symbolic_list decoder_errlist[] = {
568 { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
569 { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300570 { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
572 { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
573 { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
574 { -1, NULL }
575};
576
577/* URB error codes: */
578static struct w9968cf_symbolic_list urb_errlist[] = {
579 { -ENOMEM, "No memory for allocation of internal structures" },
580 { -ENOSPC, "The host controller's bandwidth is already consumed" },
581 { -ENOENT, "URB was canceled by unlink_urb" },
582 { -EXDEV, "ISO transfer only partially completed" },
583 { -EAGAIN, "Too match scheduled for the future" },
584 { -ENXIO, "URB already queued" },
585 { -EFBIG, "Too much ISO frames requested" },
586 { -ENOSR, "Buffer error (overrun)" },
587 { -EPIPE, "Specified endpoint is stalled (device not responding)"},
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700588 { -EOVERFLOW, "Babble (too much data)" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 { -EPROTO, "Bit-stuff error (bad cable?)" },
590 { -EILSEQ, "CRC/Timeout" },
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700591 { -ETIME, "Device does not respond to token" },
592 { -ETIMEDOUT, "Device does not respond to command" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 { -1, NULL }
594};
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/****************************************************************************
597 * Memory management functions *
598 ****************************************************************************/
599static void* rvmalloc(unsigned long size)
600{
601 void* mem;
602 unsigned long adr;
603
604 size = PAGE_ALIGN(size);
605 mem = vmalloc_32(size);
606 if (!mem)
607 return NULL;
608
609 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
610 adr = (unsigned long) mem;
611 while (size > 0) {
612 SetPageReserved(vmalloc_to_page((void *)adr));
613 adr += PAGE_SIZE;
614 size -= PAGE_SIZE;
615 }
616
617 return mem;
618}
619
620
621static void rvfree(void* mem, unsigned long size)
622{
623 unsigned long adr;
624
625 if (!mem)
626 return;
627
628 adr = (unsigned long) mem;
629 while ((long) size > 0) {
630 ClearPageReserved(vmalloc_to_page((void *)adr));
631 adr += PAGE_SIZE;
632 size -= PAGE_SIZE;
633 }
634 vfree(mem);
635}
636
637
638/*--------------------------------------------------------------------------
639 Deallocate previously allocated memory.
640 --------------------------------------------------------------------------*/
641static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
642{
643 u8 i;
644
645 /* Free the isochronous transfer buffers */
646 for (i = 0; i < W9968CF_URBS; i++) {
647 kfree(cam->transfer_buffer[i]);
648 cam->transfer_buffer[i] = NULL;
649 }
650
651 /* Free temporary frame buffer */
652 if (cam->frame_tmp.buffer) {
653 rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
654 cam->frame_tmp.buffer = NULL;
655 }
656
657 /* Free helper buffer */
658 if (cam->frame_vpp.buffer) {
659 rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
660 cam->frame_vpp.buffer = NULL;
661 }
662
663 /* Free video frame buffers */
664 if (cam->frame[0].buffer) {
665 rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
666 cam->frame[0].buffer = NULL;
667 }
668
669 cam->nbuffers = 0;
670
671 DBG(5, "Memory successfully deallocated")
672}
673
674
675/*--------------------------------------------------------------------------
676 Allocate memory buffers for USB transfers and video frames.
677 This function is called by open() only.
678 Return 0 on success, a negative number otherwise.
679 --------------------------------------------------------------------------*/
680static int w9968cf_allocate_memory(struct w9968cf_device* cam)
681{
682 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
683 void* buff = NULL;
684 unsigned long hw_bufsize, vpp_bufsize;
685 u8 i, bpp;
686
687 /* NOTE: Deallocation is done elsewhere in case of error */
688
689 /* Calculate the max amount of raw data per frame from the device */
690 hw_bufsize = cam->maxwidth*cam->maxheight*2;
691
692 /* Calculate the max buf. size needed for post-processing routines */
693 bpp = (w9968cf_vpp) ? 4 : 2;
694 if (cam->upscaling)
695 vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300696 cam->maxwidth*cam->maxheight*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 else
698 vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
699
700 /* Allocate memory for the isochronous transfer buffers */
701 for (i = 0; i < W9968CF_URBS; i++) {
702 if (!(cam->transfer_buffer[i] =
Oliver Neukumb10b4172006-01-06 21:28:40 +0100703 kzalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 DBG(1, "Couldn't allocate memory for the isochronous "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300705 "transfer buffers (%u bytes)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 p_size * W9968CF_ISO_PACKETS)
707 return -ENOMEM;
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 /* Allocate memory for the temporary frame buffer */
712 if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
713 DBG(1, "Couldn't allocate memory for the temporary "
714 "video frame buffer (%lu bytes)", hw_bufsize)
715 return -ENOMEM;
716 }
717 cam->frame_tmp.size = hw_bufsize;
718 cam->frame_tmp.number = -1;
719
720 /* Allocate memory for the helper buffer */
721 if (w9968cf_vpp) {
722 if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
723 DBG(1, "Couldn't allocate memory for the helper buffer"
724 " (%lu bytes)", vpp_bufsize)
725 return -ENOMEM;
726 }
727 cam->frame_vpp.size = vpp_bufsize;
728 } else
729 cam->frame_vpp.buffer = NULL;
730
731 /* Allocate memory for video frame buffers */
732 cam->nbuffers = cam->max_buffers;
733 while (cam->nbuffers >= 2) {
734 if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
735 break;
736 else
737 cam->nbuffers--;
738 }
739
740 if (!buff) {
741 DBG(1, "Couldn't allocate memory for the video frame buffers")
742 cam->nbuffers = 0;
743 return -ENOMEM;
744 }
745
746 if (cam->nbuffers != cam->max_buffers)
747 DBG(2, "Couldn't allocate memory for %u video frame buffers. "
748 "Only memory for %u buffers has been allocated",
749 cam->max_buffers, cam->nbuffers)
750
751 for (i = 0; i < cam->nbuffers; i++) {
752 cam->frame[i].buffer = buff + i*vpp_bufsize;
753 cam->frame[i].size = vpp_bufsize;
754 cam->frame[i].number = i;
755 /* Circular list */
756 if (i != cam->nbuffers-1)
757 cam->frame[i].next = &cam->frame[i+1];
758 else
759 cam->frame[i].next = &cam->frame[0];
760 cam->frame[i].status = F_UNUSED;
761 }
762
763 DBG(5, "Memory successfully allocated")
764 return 0;
765}
766
767
768
769/****************************************************************************
770 * USB-specific functions *
771 ****************************************************************************/
772
773/*--------------------------------------------------------------------------
774 This is an handler function which is called after the URBs are completed.
775 It collects multiple data packets coming from the camera by putting them
776 into frame buffers: one or more zero data length data packets are used to
777 mark the end of a video frame; the first non-zero data packet is the start
778 of the next video frame; if an error is encountered in a packet, the entire
779 video frame is discarded and grabbed again.
780 If there are no requested frames in the FIFO list, packets are collected into
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300781 a temporary buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 --------------------------------------------------------------------------*/
David Howells7d12e782006-10-05 14:55:46 +0100783static void w9968cf_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
786 struct w9968cf_frame_t** f;
787 unsigned int len, status;
788 void* pos;
789 u8 i;
790 int err = 0;
791
792 if ((!cam->streaming) || cam->disconnected) {
793 DBG(4, "Got interrupt, but not streaming")
794 return;
795 }
796
797 /* "(*f)" will be used instead of "cam->frame_current" */
798 f = &cam->frame_current;
799
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300800 /* If a frame has been requested and we are grabbing into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 the temporary frame, we'll switch to that requested frame */
802 if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
803 if (cam->frame_tmp.status == F_GRABBING) {
804 w9968cf_pop_frame(cam, &cam->frame_current);
805 (*f)->status = F_GRABBING;
806 (*f)->length = cam->frame_tmp.length;
807 memcpy((*f)->buffer, cam->frame_tmp.buffer,
808 (*f)->length);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300809 DBG(6, "Switched from temp. frame to frame #%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 (*f)->number)
811 }
812 }
813
814 for (i = 0; i < urb->number_of_packets; i++) {
815 len = urb->iso_frame_desc[i].actual_length;
816 status = urb->iso_frame_desc[i].status;
817 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
818
819 if (status && len != 0) {
820 DBG(4, "URB failed, error in data packet "
821 "(error #%u, %s)",
822 status, symbolic(urb_errlist, status))
823 (*f)->status = F_ERROR;
824 continue;
825 }
826
827 if (len) { /* start of frame */
828
829 if ((*f)->status == F_UNUSED) {
830 (*f)->status = F_GRABBING;
831 (*f)->length = 0;
832 }
833
834 /* Buffer overflows shouldn't happen, however...*/
835 if ((*f)->length + len > (*f)->size) {
836 DBG(4, "Buffer overflow: bad data packets")
837 (*f)->status = F_ERROR;
838 }
839
840 if ((*f)->status == F_GRABBING) {
841 memcpy((*f)->buffer + (*f)->length, pos, len);
842 (*f)->length += len;
843 }
844
845 } else if ((*f)->status == F_GRABBING) { /* end of frame */
846
847 DBG(6, "Frame #%d successfully grabbed", (*f)->number)
848
849 if (cam->vpp_flag & VPP_DECOMPRESSION) {
850 err = w9968cf_vpp->check_headers((*f)->buffer,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300851 (*f)->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (err) {
853 DBG(4, "Skip corrupted frame: %s",
854 symbolic(decoder_errlist, err))
855 (*f)->status = F_UNUSED;
856 continue; /* grab this frame again */
857 }
858 }
859
860 (*f)->status = F_READY;
861 (*f)->queued = 0;
862
863 /* Take a pointer to the new frame from the FIFO list.
864 If the list is empty,we'll use the temporary frame*/
865 if (*cam->requested_frame)
866 w9968cf_pop_frame(cam, &cam->frame_current);
867 else {
868 cam->frame_current = &cam->frame_tmp;
869 (*f)->status = F_UNUSED;
870 }
871
872 } else if ((*f)->status == F_ERROR)
873 (*f)->status = F_UNUSED; /* grab it again */
874
875 PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
876 (unsigned long)(*f)->length, i, len, (*f)->status)
877
878 } /* end for */
879
880 /* Resubmit this URB */
881 urb->dev = cam->usbdev;
882 urb->status = 0;
883 spin_lock(&cam->urb_lock);
884 if (cam->streaming)
885 if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
886 cam->misconfigured = 1;
887 DBG(1, "Couldn't resubmit the URB: error %d, %s",
888 err, symbolic(urb_errlist, err))
889 }
890 spin_unlock(&cam->urb_lock);
891
892 /* Wake up the user process */
893 wake_up_interruptible(&cam->wait_queue);
894}
895
896
897/*---------------------------------------------------------------------------
898 Setup the URB structures for the isochronous transfer.
899 Submit the URBs so that the data transfer begins.
900 Return 0 on success, a negative number otherwise.
901 ---------------------------------------------------------------------------*/
902static int w9968cf_start_transfer(struct w9968cf_device* cam)
903{
904 struct usb_device *udev = cam->usbdev;
905 struct urb* urb;
906 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
907 u16 w, h, d;
908 int vidcapt;
909 u32 t_size;
910 int err = 0;
911 s8 i, j;
912
913 for (i = 0; i < W9968CF_URBS; i++) {
914 urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
915 cam->urb[i] = urb;
916 if (!urb) {
917 for (j = 0; j < i; j++)
918 usb_free_urb(cam->urb[j]);
919 DBG(1, "Couldn't allocate the URB structures")
920 return -ENOMEM;
921 }
922
923 urb->dev = udev;
924 urb->context = (void*)cam;
925 urb->pipe = usb_rcvisocpipe(udev, 1);
926 urb->transfer_flags = URB_ISO_ASAP;
927 urb->number_of_packets = W9968CF_ISO_PACKETS;
928 urb->complete = w9968cf_urb_complete;
929 urb->transfer_buffer = cam->transfer_buffer[i];
930 urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
931 urb->interval = 1;
932 for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
933 urb->iso_frame_desc[j].offset = p_size*j;
934 urb->iso_frame_desc[j].length = p_size;
935 }
936 }
937
938 /* Transfer size per frame, in WORD ! */
939 d = cam->hw_depth;
940 w = cam->hw_width;
941 h = cam->hw_height;
942
943 t_size = (w*h*d)/16;
944
945 err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
946 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
947
948 /* Transfer size */
949 err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
950 err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
951
952 if (cam->vpp_flag & VPP_DECOMPRESSION)
953 err += w9968cf_upload_quantizationtables(cam);
954
955 vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
956 err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
957
958 err += usb_set_interface(udev, 0, cam->altsetting);
959 err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
960
961 if (err || (vidcapt < 0)) {
962 for (i = 0; i < W9968CF_URBS; i++)
963 usb_free_urb(cam->urb[i]);
964 DBG(1, "Couldn't tell the camera to start the data transfer")
965 return err;
966 }
967
968 w9968cf_init_framelist(cam);
969
970 /* Begin to grab into the temporary buffer */
971 cam->frame_tmp.status = F_UNUSED;
972 cam->frame_tmp.queued = 0;
973 cam->frame_current = &cam->frame_tmp;
974
975 if (!(cam->vpp_flag & VPP_DECOMPRESSION))
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300976 DBG(5, "Isochronous transfer size: %lu bytes/frame",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 (unsigned long)t_size*2)
978
979 DBG(5, "Starting the isochronous transfer...")
980
981 cam->streaming = 1;
982
983 /* Submit the URBs */
984 for (i = 0; i < W9968CF_URBS; i++) {
985 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
986 if (err) {
987 cam->streaming = 0;
988 for (j = i-1; j >= 0; j--) {
989 usb_kill_urb(cam->urb[j]);
990 usb_free_urb(cam->urb[j]);
991 }
992 DBG(1, "Couldn't send a transfer request to the "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300993 "USB core (error #%d, %s)", err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 symbolic(urb_errlist, err))
995 return err;
996 }
997 }
998
999 return 0;
1000}
1001
1002
1003/*--------------------------------------------------------------------------
1004 Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
1005 Return 0 on success, a negative number otherwise.
1006 --------------------------------------------------------------------------*/
1007static int w9968cf_stop_transfer(struct w9968cf_device* cam)
1008{
1009 struct usb_device *udev = cam->usbdev;
1010 unsigned long lock_flags;
1011 int err = 0;
1012 s8 i;
1013
1014 if (!cam->streaming)
1015 return 0;
1016
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001017 /* This avoids race conditions with usb_submit_urb()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 in the URB completition handler */
1019 spin_lock_irqsave(&cam->urb_lock, lock_flags);
1020 cam->streaming = 0;
1021 spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
1022
1023 for (i = W9968CF_URBS-1; i >= 0; i--)
1024 if (cam->urb[i]) {
1025 usb_kill_urb(cam->urb[i]);
1026 usb_free_urb(cam->urb[i]);
1027 cam->urb[i] = NULL;
1028 }
1029
1030 if (cam->disconnected)
1031 goto exit;
1032
1033 err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
1034 err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
1035 err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
1036 err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
1037
1038 if (err) {
1039 DBG(2, "Failed to tell the camera to stop the isochronous "
1040 "transfer. However this is not a critical error.")
1041 return -EIO;
1042 }
1043
1044exit:
1045 DBG(5, "Isochronous transfer stopped")
1046 return 0;
1047}
1048
1049
1050/*--------------------------------------------------------------------------
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001051 Write a W9968CF register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 Return 0 on success, -1 otherwise.
1053 --------------------------------------------------------------------------*/
1054static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
1055{
1056 struct usb_device* udev = cam->usbdev;
1057 int res;
1058
1059 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001060 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1061 value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (res < 0)
1064 DBG(4, "Failed to write a register "
1065 "(value 0x%04X, index 0x%02X, error #%d, %s)",
1066 value, index, res, symbolic(urb_errlist, res))
1067
1068 return (res >= 0) ? 0 : -1;
1069}
1070
1071
1072/*--------------------------------------------------------------------------
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001073 Read a W9968CF register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 Return the register value on success, -1 otherwise.
1075 --------------------------------------------------------------------------*/
1076static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
1077{
1078 struct usb_device* udev = cam->usbdev;
1079 u16* buff = cam->control_buffer;
1080 int res;
1081
1082 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001083 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1084 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (res < 0)
1087 DBG(4, "Failed to read a register "
1088 "(index 0x%02X, error #%d, %s)",
1089 index, res, symbolic(urb_errlist, res))
1090
1091 return (res >= 0) ? (int)(*buff) : -1;
1092}
1093
1094
1095/*--------------------------------------------------------------------------
1096 Write 64-bit data to the fast serial bus registers.
1097 Return 0 on success, -1 otherwise.
1098 --------------------------------------------------------------------------*/
1099static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
1100{
1101 struct usb_device* udev = cam->usbdev;
1102 u16 value;
1103 int res;
1104
1105 value = *data++;
1106
1107 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001108 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1109 value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (res < 0)
1112 DBG(4, "Failed to write the FSB registers "
1113 "(error #%d, %s)", res, symbolic(urb_errlist, res))
1114
1115 return (res >= 0) ? 0 : -1;
1116}
1117
1118
1119/*--------------------------------------------------------------------------
1120 Write data to the serial bus control register.
1121 Return 0 on success, a negative number otherwise.
1122 --------------------------------------------------------------------------*/
1123static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
1124{
1125 int err = 0;
1126
1127 err = w9968cf_write_reg(cam, value, 0x01);
1128 udelay(W9968CF_I2C_BUS_DELAY);
1129
1130 return err;
1131}
1132
1133
1134/*--------------------------------------------------------------------------
1135 Read data from the serial bus control register.
1136 Return 0 on success, a negative number otherwise.
1137 --------------------------------------------------------------------------*/
1138static int w9968cf_read_sb(struct w9968cf_device* cam)
1139{
1140 int v = 0;
1141
1142 v = w9968cf_read_reg(cam, 0x01);
1143 udelay(W9968CF_I2C_BUS_DELAY);
1144
1145 return v;
1146}
1147
1148
1149/*--------------------------------------------------------------------------
1150 Upload quantization tables for the JPEG compression.
1151 This function is called by w9968cf_start_transfer().
1152 Return 0 on success, a negative number otherwise.
1153 --------------------------------------------------------------------------*/
1154static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
1155{
1156 u16 a, b;
1157 int err = 0, i, j;
1158
1159 err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
1160
1161 for (i = 0, j = 0; i < 32; i++, j += 2) {
1162 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
1163 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
1164 err += w9968cf_write_reg(cam, a, 0x40+i);
1165 err += w9968cf_write_reg(cam, b, 0x60+i);
1166 }
1167 err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
1168
1169 return err;
1170}
1171
1172
1173
1174/****************************************************************************
1175 * Low-level I2C I/O functions. *
1176 * The adapter supports the following I2C transfer functions: *
1177 * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
1178 * i2c_adap_read_byte_data() *
1179 * i2c_adap_read_byte() *
1180 ****************************************************************************/
1181
1182static int w9968cf_smbus_start(struct w9968cf_device* cam)
1183{
1184 int err = 0;
1185
1186 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1187 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1188
1189 return err;
1190}
1191
1192
1193static int w9968cf_smbus_stop(struct w9968cf_device* cam)
1194{
1195 int err = 0;
1196
1197 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1198 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1199
1200 return err;
1201}
1202
1203
1204static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
1205{
1206 u8 bit;
1207 int err = 0, sda;
1208
1209 for (bit = 0 ; bit < 8 ; bit++) {
1210 sda = (v & 0x80) ? 2 : 0;
1211 v <<= 1;
1212 /* SDE=1, SDA=sda, SCL=0 */
1213 err += w9968cf_write_sb(cam, 0x10 | sda);
1214 /* SDE=1, SDA=sda, SCL=1 */
1215 err += w9968cf_write_sb(cam, 0x11 | sda);
1216 /* SDE=1, SDA=sda, SCL=0 */
1217 err += w9968cf_write_sb(cam, 0x10 | sda);
1218 }
1219
1220 return err;
1221}
1222
1223
1224static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
1225{
1226 u8 bit;
1227 int err = 0;
1228
1229 *v = 0;
1230 for (bit = 0 ; bit < 8 ; bit++) {
1231 *v <<= 1;
1232 err += w9968cf_write_sb(cam, 0x0013);
1233 *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
1234 err += w9968cf_write_sb(cam, 0x0012);
1235 }
1236
1237 return err;
1238}
1239
1240
1241static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
1242{
1243 int err = 0;
1244
1245 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1246 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1247 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1248
1249 return err;
1250}
1251
1252
1253static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
1254{
1255 int err = 0, sda;
1256
1257 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1258 sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
1259 err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
1260 if (sda < 0)
1261 err += sda;
1262 if (sda == 1) {
1263 DBG(6, "Couldn't receive the ACK")
1264 err += -1;
1265 }
1266
1267 return err;
1268}
1269
1270
1271/* This seems to refresh the communication through the serial bus */
1272static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
1273{
1274 int err = 0, j;
1275
1276 for (j = 1; j <= 10; j++) {
1277 err = w9968cf_write_reg(cam, 0x0020, 0x01);
1278 err += w9968cf_write_reg(cam, 0x0000, 0x01);
1279 if (err)
1280 break;
1281 }
1282
1283 return err;
1284}
1285
1286
1287/* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001288static int
1289w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
1290 u16 address, u8 subaddress,u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 u16* data = cam->data_buffer;
1293 int err = 0;
1294
1295 err += w9968cf_smbus_refresh_bus(cam);
1296
1297 /* Enable SBUS outputs */
1298 err += w9968cf_write_sb(cam, 0x0020);
1299
1300 data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
1301 data[0] |= (address & 0x40) ? 0x4000 : 0x0;
1302 data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
1303 data[1] |= (address & 0x20) ? 0x0150 : 0x0;
1304 data[1] |= (address & 0x10) ? 0x5400 : 0x0;
1305 data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
1306 data[2] |= (address & 0x04) ? 0x0540 : 0x0;
1307 data[2] |= (address & 0x02) ? 0x5000 : 0x0;
1308 data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
1309 data[3] |= (address & 0x01) ? 0x0054 : 0x0;
1310
1311 err += w9968cf_write_fsb(cam, data);
1312
1313 data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
1314 data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
1315 data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
1316 data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
1317 data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
1318 data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
1319 data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
1320 data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
1321 data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
1322 data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
1323 data[3] = 0x001d;
1324
1325 err += w9968cf_write_fsb(cam, data);
1326
1327 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
1328 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
1329 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
1330 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
1331 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
1332 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
1333 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
1334 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
1335 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
1336 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
1337 data[3] = 0xfe1d;
1338
1339 err += w9968cf_write_fsb(cam, data);
1340
1341 /* Disable SBUS outputs */
1342 err += w9968cf_write_sb(cam, 0x0000);
1343
1344 if (!err)
1345 DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
1346 "value 0x%02X", address, subaddress, value)
1347 else
1348 DBG(5, "I2C write byte data failed, addr.0x%04X, "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001349 "subaddr.0x%02X, value 0x%02X",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 address, subaddress, value)
1351
1352 return err;
1353}
1354
1355
1356/* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001357static int
1358w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
1359 u16 address, u8 subaddress,
1360 u8* value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 int err = 0;
1363
1364 /* Serial data enable */
1365 err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
1366
1367 err += w9968cf_smbus_start(cam);
1368 err += w9968cf_smbus_write_byte(cam, address);
1369 err += w9968cf_smbus_read_ack(cam);
1370 err += w9968cf_smbus_write_byte(cam, subaddress);
1371 err += w9968cf_smbus_read_ack(cam);
1372 err += w9968cf_smbus_stop(cam);
1373 err += w9968cf_smbus_start(cam);
1374 err += w9968cf_smbus_write_byte(cam, address + 1);
1375 err += w9968cf_smbus_read_ack(cam);
1376 err += w9968cf_smbus_read_byte(cam, value);
1377 err += w9968cf_smbus_write_ack(cam);
1378 err += w9968cf_smbus_stop(cam);
1379
1380 /* Serial data disable */
1381 err += w9968cf_write_sb(cam, 0x0000);
1382
1383 if (!err)
1384 DBG(5, "I2C read byte data done, addr.0x%04X, "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001385 "subaddr.0x%02X, value 0x%02X",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 address, subaddress, *value)
1387 else
1388 DBG(5, "I2C read byte data failed, addr.0x%04X, "
1389 "subaddr.0x%02X, wrong value 0x%02X",
1390 address, subaddress, *value)
1391
1392 return err;
1393}
1394
1395
1396/* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001397static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001399 u16 address, u8* value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 int err = 0;
1402
1403 /* Serial data enable */
1404 err += w9968cf_write_sb(cam, 0x0013);
1405
1406 err += w9968cf_smbus_start(cam);
1407 err += w9968cf_smbus_write_byte(cam, address + 1);
1408 err += w9968cf_smbus_read_ack(cam);
1409 err += w9968cf_smbus_read_byte(cam, value);
1410 err += w9968cf_smbus_write_ack(cam);
1411 err += w9968cf_smbus_stop(cam);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /* Serial data disable */
1414 err += w9968cf_write_sb(cam, 0x0000);
1415
1416 if (!err)
1417 DBG(5, "I2C read byte done, addr.0x%04X, "
1418 "value 0x%02X", address, *value)
1419 else
1420 DBG(5, "I2C read byte failed, addr.0x%04X, "
1421 "wrong value 0x%02X", address, *value)
1422
1423 return err;
1424}
1425
1426
1427/* SMBus protocol: S Addr Wr [A] Value [A] P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001428static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001430 u16 address, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 DBG(4, "i2c_write_byte() is an unsupported transfer mode")
1433 return -EINVAL;
1434}
1435
1436
1437
1438/****************************************************************************
1439 * I2C interface to kernel *
1440 ****************************************************************************/
1441
1442static int
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001443w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1444 unsigned short flags, char read_write, u8 command,
1445 int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct w9968cf_device* cam = i2c_get_adapdata(adapter);
1448 u8 i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001449 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 switch (addr) {
1452 case OV6xx0_SID:
1453 case OV7xx0_SID:
1454 break;
1455 default:
1456 DBG(4, "Rejected slave ID 0x%04X", addr)
1457 return -EINVAL;
1458 }
1459
1460 if (size == I2C_SMBUS_BYTE) {
1461 /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
1462 addr <<= 1;
1463
1464 if (read_write == I2C_SMBUS_WRITE)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001465 err = w9968cf_i2c_adap_write_byte(cam, addr, command);
1466 else if (read_write == I2C_SMBUS_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 err = w9968cf_i2c_adap_read_byte(cam,addr,&data->byte);
1468
1469 } else if (size == I2C_SMBUS_BYTE_DATA) {
1470 addr <<= 1;
1471
1472 if (read_write == I2C_SMBUS_WRITE)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001473 err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
1474 command, data->byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 else if (read_write == I2C_SMBUS_READ) {
1476 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1477 err = w9968cf_i2c_adap_read_byte_data(cam,addr,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001478 command, &data->byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (err) {
1480 if (w9968cf_smbus_refresh_bus(cam)) {
1481 err = -EIO;
1482 break;
1483 }
1484 } else
1485 break;
1486 }
1487
1488 } else
1489 return -EINVAL;
1490
1491 } else {
1492 DBG(4, "Unsupported I2C transfer mode (%d)", size)
1493 return -EINVAL;
1494 }
1495
1496 return err;
1497}
1498
1499
1500static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
1501{
1502 return I2C_FUNC_SMBUS_READ_BYTE |
1503 I2C_FUNC_SMBUS_READ_BYTE_DATA |
1504 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
1505}
1506
1507
1508static int w9968cf_i2c_attach_inform(struct i2c_client* client)
1509{
1510 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 int id = client->driver->id, err = 0;
1512
1513 if (id == I2C_DRIVERID_OVCAMCHIP) {
1514 cam->sensor_client = client;
1515 err = w9968cf_sensor_init(cam);
1516 if (err) {
1517 cam->sensor_client = NULL;
1518 return err;
1519 }
1520 } else {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001521 DBG(4, "Rejected client [%s] with driver [%s]",
Laurent Riffard604f28e2005-11-26 20:43:39 +01001522 client->name, client->driver->driver.name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -EINVAL;
1524 }
1525
1526 DBG(5, "I2C attach client [%s] with driver [%s]",
Laurent Riffard604f28e2005-11-26 20:43:39 +01001527 client->name, client->driver->driver.name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 return 0;
1530}
1531
1532
1533static int w9968cf_i2c_detach_inform(struct i2c_client* client)
1534{
1535 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 if (cam->sensor_client == client)
1538 cam->sensor_client = NULL;
1539
Jean Delvarefae91e72005-08-15 19:57:04 +02001540 DBG(5, "I2C detach client [%s]", client->name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 return 0;
1543}
1544
1545
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001546static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547w9968cf_i2c_control(struct i2c_adapter* adapter, unsigned int cmd,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001548 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 return 0;
1551}
1552
1553
1554static int w9968cf_i2c_init(struct w9968cf_device* cam)
1555{
1556 int err = 0;
1557
1558 static struct i2c_algorithm algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 .smbus_xfer = w9968cf_i2c_smbus_xfer,
1560 .algo_control = w9968cf_i2c_control,
1561 .functionality = w9968cf_i2c_func,
1562 };
1563
1564 static struct i2c_adapter adap = {
Jean Delvarec7a46532005-08-11 23:41:56 +02001565 .id = I2C_HW_SMBUS_W9968CF,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 .class = I2C_CLASS_CAM_DIGITAL,
1567 .owner = THIS_MODULE,
1568 .client_register = w9968cf_i2c_attach_inform,
1569 .client_unregister = w9968cf_i2c_detach_inform,
1570 .algo = &algo,
1571 };
1572
1573 memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
1574 strcpy(cam->i2c_adapter.name, "w9968cf");
Jean Delvare12a917f2007-02-13 22:09:03 +01001575 cam->i2c_adapter.dev.parent = &cam->usbdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 i2c_set_adapdata(&cam->i2c_adapter, cam);
1577
1578 DBG(6, "Registering I2C adapter with kernel...")
1579
1580 err = i2c_add_adapter(&cam->i2c_adapter);
1581 if (err)
1582 DBG(1, "Failed to register the I2C adapter")
1583 else
1584 DBG(5, "I2C adapter registered")
1585
1586 return err;
1587}
1588
1589
1590
1591/****************************************************************************
1592 * Helper functions *
1593 ****************************************************************************/
1594
1595/*--------------------------------------------------------------------------
1596 Turn on the LED on some webcams. A beep should be heard too.
1597 Return 0 on success, a negative number otherwise.
1598 --------------------------------------------------------------------------*/
1599static int w9968cf_turn_on_led(struct w9968cf_device* cam)
1600{
1601 int err = 0;
1602
1603 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power-down */
1604 err += w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
1605 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
1606 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* serial bus, SDS high */
1607 err += w9968cf_write_reg(cam, 0x0000, 0x01); /* serial bus, SDS low */
1608 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* ..high 'beep-beep' */
1609
1610 if (err)
1611 DBG(2, "Couldn't turn on the LED")
1612
1613 DBG(5, "LED turned on")
1614
1615 return err;
1616}
1617
1618
1619/*--------------------------------------------------------------------------
1620 Write some registers for the device initialization.
1621 This function is called once on open().
1622 Return 0 on success, a negative number otherwise.
1623 --------------------------------------------------------------------------*/
1624static int w9968cf_init_chip(struct w9968cf_device* cam)
1625{
1626 unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001627 y0 = 0x0000,
1628 u0 = y0 + hw_bufsize/2,
1629 v0 = u0 + hw_bufsize/4,
1630 y1 = v0 + hw_bufsize/4,
1631 u1 = y1 + hw_bufsize/2,
1632 v1 = u1 + hw_bufsize/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 int err = 0;
1634
1635 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
1636 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
1637
1638 err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
1639 err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
1640
1641 err += w9968cf_write_reg(cam, y0 & 0xffff, 0x20); /* Y buf.0, low */
1642 err += w9968cf_write_reg(cam, y0 >> 16, 0x21); /* Y buf.0, high */
1643 err += w9968cf_write_reg(cam, u0 & 0xffff, 0x24); /* U buf.0, low */
1644 err += w9968cf_write_reg(cam, u0 >> 16, 0x25); /* U buf.0, high */
1645 err += w9968cf_write_reg(cam, v0 & 0xffff, 0x28); /* V buf.0, low */
1646 err += w9968cf_write_reg(cam, v0 >> 16, 0x29); /* V buf.0, high */
1647
1648 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x22); /* Y buf.1, low */
1649 err += w9968cf_write_reg(cam, y1 >> 16, 0x23); /* Y buf.1, high */
1650 err += w9968cf_write_reg(cam, u1 & 0xffff, 0x26); /* U buf.1, low */
1651 err += w9968cf_write_reg(cam, u1 >> 16, 0x27); /* U buf.1, high */
1652 err += w9968cf_write_reg(cam, v1 & 0xffff, 0x2a); /* V buf.1, low */
1653 err += w9968cf_write_reg(cam, v1 >> 16, 0x2b); /* V buf.1, high */
1654
1655 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x32); /* JPEG buf 0 low */
1656 err += w9968cf_write_reg(cam, y1 >> 16, 0x33); /* JPEG buf 0 high */
1657
1658 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x34); /* JPEG buf 1 low */
1659 err += w9968cf_write_reg(cam, y1 >> 16, 0x35); /* JPEG bug 1 high */
1660
1661 err += w9968cf_write_reg(cam, 0x0000, 0x36);/* JPEG restart interval */
1662 err += w9968cf_write_reg(cam, 0x0804, 0x37);/*JPEG VLE FIFO threshold*/
1663 err += w9968cf_write_reg(cam, 0x0000, 0x38);/* disable hw up-scaling */
1664 err += w9968cf_write_reg(cam, 0x0000, 0x3f); /* JPEG/MCTL test data */
1665
1666 err += w9968cf_set_picture(cam, cam->picture); /* this before */
1667 err += w9968cf_set_window(cam, cam->window);
1668
1669 if (err)
1670 DBG(1, "Chip initialization failed")
1671 else
1672 DBG(5, "Chip successfully initialized")
1673
1674 return err;
1675}
1676
1677
1678/*--------------------------------------------------------------------------
1679 Return non-zero if the palette is supported, 0 otherwise.
1680 --------------------------------------------------------------------------*/
1681static inline u16 w9968cf_valid_palette(u16 palette)
1682{
1683 u8 i = 0;
1684 while (w9968cf_formatlist[i].palette != 0) {
1685 if (palette == w9968cf_formatlist[i].palette)
1686 return palette;
1687 i++;
1688 }
1689 return 0;
1690}
1691
1692
1693/*--------------------------------------------------------------------------
1694 Return the depth corresponding to the given palette.
1695 Palette _must_ be supported !
1696 --------------------------------------------------------------------------*/
1697static inline u16 w9968cf_valid_depth(u16 palette)
1698{
1699 u8 i=0;
1700 while (w9968cf_formatlist[i].palette != palette)
1701 i++;
1702
1703 return w9968cf_formatlist[i].depth;
1704}
1705
1706
1707/*--------------------------------------------------------------------------
1708 Return non-zero if the format requires decompression, 0 otherwise.
1709 --------------------------------------------------------------------------*/
1710static inline u8 w9968cf_need_decompression(u16 palette)
1711{
1712 u8 i = 0;
1713 while (w9968cf_formatlist[i].palette != 0) {
1714 if (palette == w9968cf_formatlist[i].palette)
1715 return w9968cf_formatlist[i].compression;
1716 i++;
1717 }
1718 return 0;
1719}
1720
1721
1722/*--------------------------------------------------------------------------
1723 Change the picture settings of the camera.
1724 Return 0 on success, a negative number otherwise.
1725 --------------------------------------------------------------------------*/
1726static int
1727w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
1728{
1729 u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
1730 int err = 0;
1731
1732 /* Make sure we are using a valid depth */
1733 pict.depth = w9968cf_valid_depth(pict.palette);
1734
1735 fmt = pict.palette;
1736
1737 hw_depth = pict.depth; /* depth used by the winbond chip */
1738 hw_palette = pict.palette; /* palette used by the winbond chip */
1739
1740 /* VS & HS polarities */
1741 reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
1742
1743 switch (fmt)
1744 {
1745 case VIDEO_PALETTE_UYVY:
1746 reg_v |= 0x0000;
1747 cam->vpp_flag = VPP_NONE;
1748 break;
1749 case VIDEO_PALETTE_YUV422P:
1750 reg_v |= 0x0002;
1751 cam->vpp_flag = VPP_DECOMPRESSION;
1752 break;
1753 case VIDEO_PALETTE_YUV420:
1754 case VIDEO_PALETTE_YUV420P:
1755 reg_v |= 0x0003;
1756 cam->vpp_flag = VPP_DECOMPRESSION;
1757 break;
1758 case VIDEO_PALETTE_YUYV:
1759 case VIDEO_PALETTE_YUV422:
1760 reg_v |= 0x0000;
1761 cam->vpp_flag = VPP_SWAP_YUV_BYTES;
1762 hw_palette = VIDEO_PALETTE_UYVY;
1763 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001764 /* Original video is used instead of RGBX palettes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 Software conversion later. */
1766 case VIDEO_PALETTE_GREY:
1767 case VIDEO_PALETTE_RGB555:
1768 case VIDEO_PALETTE_RGB565:
1769 case VIDEO_PALETTE_RGB24:
1770 case VIDEO_PALETTE_RGB32:
1771 reg_v |= 0x0000; /* UYVY 16 bit is used */
1772 hw_depth = 16;
1773 hw_palette = VIDEO_PALETTE_UYVY;
1774 cam->vpp_flag = VPP_UYVY_TO_RGBX;
1775 break;
1776 }
1777
1778 /* NOTE: due to memory issues, it is better to disable the hardware
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001779 double buffering during compression */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
1781 reg_v |= 0x0080;
1782
1783 if (cam->clamping)
1784 reg_v |= 0x0020;
1785
1786 if (cam->filter_type == 1)
1787 reg_v |= 0x0008;
1788 else if (cam->filter_type == 2)
1789 reg_v |= 0x000c;
1790
1791 if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
1792 goto error;
1793
1794 if ((err = w9968cf_sensor_update_picture(cam, pict)))
1795 goto error;
1796
1797 /* If all went well, update the device data structure */
1798 memcpy(&cam->picture, &pict, sizeof(pict));
1799 cam->hw_depth = hw_depth;
1800 cam->hw_palette = hw_palette;
1801
1802 /* Settings changed, so we clear the frame buffers */
1803 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1804
1805 DBG(4, "Palette is %s, depth is %u bpp",
1806 symbolic(v4l1_plist, pict.palette), pict.depth)
1807
1808 return 0;
1809
1810error:
1811 DBG(1, "Failed to change picture settings")
1812 return err;
1813}
1814
1815
1816/*--------------------------------------------------------------------------
1817 Change the capture area size of the camera.
1818 This function _must_ be called _after_ w9968cf_set_picture().
1819 Return 0 on success, a negative number otherwise.
1820 --------------------------------------------------------------------------*/
1821static int
1822w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
1823{
1824 u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
1825 unsigned long fw, fh;
1826 struct ovcamchip_window s_win;
1827 int err = 0;
1828
1829 /* Work around to avoid FP arithmetics */
Ralf Baechlebee8a442006-12-11 11:14:54 -03001830 #define SC(x) ((x) << 10)
1831 #define UNSC(x) ((x) >> 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 /* Make sure we are using a supported resolution */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001834 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
1835 (u16*)&win.height)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto error;
1837
1838 /* Scaling factors */
Ralf Baechlebee8a442006-12-11 11:14:54 -03001839 fw = SC(win.width) / cam->maxwidth;
1840 fh = SC(win.height) / cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /* Set up the width and height values used by the chip */
1843 if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
1844 cam->vpp_flag |= VPP_UPSCALE;
1845 /* Calculate largest w,h mantaining the same w/h ratio */
Ralf Baechlebee8a442006-12-11 11:14:54 -03001846 w = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1847 h = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (w < cam->minwidth) /* just in case */
1849 w = cam->minwidth;
1850 if (h < cam->minheight) /* just in case */
1851 h = cam->minheight;
1852 } else {
1853 cam->vpp_flag &= ~VPP_UPSCALE;
1854 w = win.width;
1855 h = win.height;
1856 }
1857
1858 /* x,y offsets of the cropped area */
1859 scx = cam->start_cropx;
1860 scy = cam->start_cropy;
1861
1862 /* Calculate cropped area manteining the right w/h ratio */
1863 if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
Ralf Baechlebee8a442006-12-11 11:14:54 -03001864 cw = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1865 ch = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 } else {
1867 cw = w;
1868 ch = h;
1869 }
1870
1871 /* Setup the window of the sensor */
1872 s_win.format = VIDEO_PALETTE_UYVY;
1873 s_win.width = cam->maxwidth;
1874 s_win.height = cam->maxheight;
1875 s_win.quarter = 0; /* full progressive video */
1876
1877 /* Center it */
1878 s_win.x = (s_win.width - cw) / 2;
1879 s_win.y = (s_win.height - ch) / 2;
1880
1881 /* Clock divisor */
1882 if (cam->clockdiv >= 0)
1883 s_win.clockdiv = cam->clockdiv; /* manual override */
1884 else
1885 switch (cam->sensor) {
1886 case CC_OV6620:
1887 s_win.clockdiv = 0;
1888 break;
1889 case CC_OV6630:
1890 s_win.clockdiv = 0;
1891 break;
1892 case CC_OV76BE:
1893 case CC_OV7610:
1894 case CC_OV7620:
1895 s_win.clockdiv = 0;
1896 break;
1897 default:
1898 s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
1899 }
1900
1901 /* We have to scale win.x and win.y offsets */
1902 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1903 || (cam->vpp_flag & VPP_UPSCALE) ) {
Ralf Baechlebee8a442006-12-11 11:14:54 -03001904 ax = SC(win.x)/fw;
1905 ay = SC(win.y)/fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 } else {
1907 ax = win.x;
1908 ay = win.y;
1909 }
1910
1911 if ((ax + cw) > cam->maxwidth)
1912 ax = cam->maxwidth - cw;
1913
1914 if ((ay + ch) > cam->maxheight)
1915 ay = cam->maxheight - ch;
1916
1917 /* Adjust win.x, win.y */
1918 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1919 || (cam->vpp_flag & VPP_UPSCALE) ) {
Ralf Baechlebee8a442006-12-11 11:14:54 -03001920 win.x = UNSC(ax*fw);
1921 win.y = UNSC(ay*fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 } else {
1923 win.x = ax;
1924 win.y = ay;
1925 }
1926
1927 /* Offsets used by the chip */
1928 x = ax + s_win.x;
1929 y = ay + s_win.y;
1930
1931 /* Go ! */
1932 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
1933 goto error;
1934
1935 err += w9968cf_write_reg(cam, scx + x, 0x10);
1936 err += w9968cf_write_reg(cam, scy + y, 0x11);
1937 err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
1938 err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
1939 err += w9968cf_write_reg(cam, w, 0x14);
1940 err += w9968cf_write_reg(cam, h, 0x15);
1941
1942 /* JPEG width & height */
1943 err += w9968cf_write_reg(cam, w, 0x30);
1944 err += w9968cf_write_reg(cam, h, 0x31);
1945
1946 /* Y & UV frame buffer strides (in WORD) */
1947 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1948 err += w9968cf_write_reg(cam, w/2, 0x2c);
1949 err += w9968cf_write_reg(cam, w/4, 0x2d);
1950 } else
1951 err += w9968cf_write_reg(cam, w, 0x2c);
1952
1953 if (err)
1954 goto error;
1955
1956 /* If all went well, update the device data structure */
1957 memcpy(&cam->window, &win, sizeof(win));
1958 cam->hw_width = w;
1959 cam->hw_height = h;
1960
1961 /* Settings changed, so we clear the frame buffers */
1962 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1963
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001964 DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 win.width, win.height, win.x, win.y)
1966
1967 PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
1968 "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
1969 x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
1970 win.width, win.height)
1971
1972 return 0;
1973
1974error:
1975 DBG(1, "Failed to change the capture area size")
1976 return err;
1977}
1978
1979
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001980/*--------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 Adjust the asked values for window width and height.
1982 Return 0 on success, -1 otherwise.
1983 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001984static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985w9968cf_adjust_window_size(struct w9968cf_device* cam, u16* width, u16* height)
1986{
1987 u16 maxw, maxh;
1988
1989 if ((*width < cam->minwidth) || (*height < cam->minheight))
1990 return -ERANGE;
1991
1992 maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1993 w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001994 : cam->maxwidth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1996 w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001997 : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 if (*width > maxw)
2000 *width = maxw;
2001 if (*height > maxh)
2002 *height = maxh;
2003
2004 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2005 *width &= ~15L; /* multiple of 16 */
2006 *height &= ~15L;
2007 }
2008
2009 PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
2010
2011 return 0;
2012}
2013
2014
2015/*--------------------------------------------------------------------------
2016 Initialize the FIFO list of requested frames.
2017 --------------------------------------------------------------------------*/
2018static void w9968cf_init_framelist(struct w9968cf_device* cam)
2019{
2020 u8 i;
2021
2022 for (i = 0; i < cam->nbuffers; i++) {
2023 cam->requested_frame[i] = NULL;
2024 cam->frame[i].queued = 0;
2025 cam->frame[i].status = F_UNUSED;
2026 }
2027}
2028
2029
2030/*--------------------------------------------------------------------------
2031 Add a frame in the FIFO list of requested frames.
2032 This function is called in process context.
2033 --------------------------------------------------------------------------*/
2034static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
2035{
2036 u8 f;
2037 unsigned long lock_flags;
2038
2039 spin_lock_irqsave(&cam->flist_lock, lock_flags);
2040
2041 for (f=0; cam->requested_frame[f] != NULL; f++);
2042 cam->requested_frame[f] = &cam->frame[f_num];
2043 cam->frame[f_num].queued = 1;
2044 cam->frame[f_num].status = F_UNUSED; /* clear the status */
2045
2046 spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
2047
2048 DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
2049}
2050
2051
2052/*--------------------------------------------------------------------------
2053 Read, store and remove the first pointer in the FIFO list of requested
2054 frames. This function is called in interrupt context.
2055 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002056static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
2058{
2059 u8 i;
2060
2061 spin_lock(&cam->flist_lock);
2062
2063 *framep = cam->requested_frame[0];
2064
2065 /* Shift the list of pointers */
2066 for (i = 0; i < cam->nbuffers-1; i++)
2067 cam->requested_frame[i] = cam->requested_frame[i+1];
2068 cam->requested_frame[i] = NULL;
2069
2070 spin_unlock(&cam->flist_lock);
2071
2072 DBG(6,"Popped frame #%d from the list", (*framep)->number)
2073}
2074
2075
2076/*--------------------------------------------------------------------------
2077 High-level video post-processing routine on grabbed frames.
2078 Return 0 on success, a negative number otherwise.
2079 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002080static int
2081w9968cf_postprocess_frame(struct w9968cf_device* cam,
2082 struct w9968cf_frame_t* fr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
2085 u16 w = cam->window.width,
2086 h = cam->window.height,
2087 d = cam->picture.depth,
2088 fmt = cam->picture.palette,
2089 rgb = cam->force_rgb,
2090 hw_w = cam->hw_width,
2091 hw_h = cam->hw_height,
2092 hw_d = cam->hw_depth;
2093 int err = 0;
2094
2095 #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
2096
2097 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2098 memcpy(pOut, pIn, fr->length);
2099 _PSWAP(pIn, pOut)
2100 err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
2101 PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
2102 fr->length = (hw_w*hw_h*hw_d)/8;
2103 _PSWAP(pIn, pOut)
2104 if (err) {
2105 DBG(4, "An error occurred while decoding the frame: "
2106 "%s", symbolic(decoder_errlist, err))
2107 return err;
2108 } else
2109 DBG(6, "Frame decoded")
2110 }
2111
2112 if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
2113 w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
2114 DBG(6, "Original UYVY component ordering changed")
2115 }
2116
2117 if (cam->vpp_flag & VPP_UPSCALE) {
2118 w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
2119 fr->length = (w*h*hw_d)/8;
2120 _PSWAP(pIn, pOut)
2121 DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
2122 hw_w, hw_h, hw_d, w, h)
2123 }
2124
2125 if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
2126 w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
2127 fr->length = (w*h*d)/8;
2128 _PSWAP(pIn, pOut)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002129 DBG(6, "UYVY-16bit to %s conversion done",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 symbolic(v4l1_plist, fmt))
2131 }
2132
2133 if (pOut == fr->buffer)
2134 memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
2135
2136 return 0;
2137}
2138
2139
2140
2141/****************************************************************************
2142 * Image sensor control routines *
2143 ****************************************************************************/
2144
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002145static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
2147{
2148 struct ovcamchip_control ctl;
2149 int err;
2150
2151 ctl.id = cid;
2152 ctl.value = val;
2153
2154 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
2155
2156 return err;
2157}
2158
2159
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002160static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
2162{
2163 struct ovcamchip_control ctl;
2164 int err;
2165
2166 ctl.id = cid;
2167
2168 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
2169 if (!err)
2170 *val = ctl.value;
2171
2172 return err;
2173}
2174
2175
2176static int
2177w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
2178{
2179 struct i2c_client* c = cam->sensor_client;
2180 int rc = 0;
2181
2182 if (!c || !c->driver || !c->driver->command)
2183 return -EINVAL;
2184
2185 rc = c->driver->command(c, cmd, arg);
2186 /* The I2C driver returns -EPERM on non-supported controls */
2187 return (rc < 0 && rc != -EPERM) ? rc : 0;
2188}
2189
2190
2191/*--------------------------------------------------------------------------
2192 Update some settings of the image sensor.
2193 Returns: 0 on success, a negative number otherwise.
2194 --------------------------------------------------------------------------*/
2195static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
2196{
2197 int err = 0;
2198
2199 /* Auto brightness */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002200 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
2201 cam->auto_brt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (err)
2203 return err;
2204
2205 /* Auto exposure */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002206 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
2207 cam->auto_exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (err)
2209 return err;
2210
2211 /* Banding filter */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002212 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
2213 cam->bandfilt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (err)
2215 return err;
2216
2217 /* Light frequency */
2218 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002219 cam->lightfreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (err)
2221 return err;
2222
2223 /* Back light */
2224 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002225 cam->backlight);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (err)
2227 return err;
2228
2229 /* Mirror */
2230 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002231 cam->mirror);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (err)
2233 return err;
2234
2235 return 0;
2236}
2237
2238
2239/*--------------------------------------------------------------------------
2240 Get some current picture settings from the image sensor and update the
2241 internal 'picture' structure of the camera.
2242 Returns: 0 on success, a negative number otherwise.
2243 --------------------------------------------------------------------------*/
2244static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
2245{
2246 int err, v;
2247
2248 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
2249 if (err)
2250 return err;
2251 cam->picture.contrast = v;
2252
2253 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
2254 if (err)
2255 return err;
2256 cam->picture.brightness = v;
2257
2258 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
2259 if (err)
2260 return err;
2261 cam->picture.colour = v;
2262
2263 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
2264 if (err)
2265 return err;
2266 cam->picture.hue = v;
2267
2268 DBG(5, "Got picture settings from the image sensor")
2269
2270 PDBGG("Brightness, contrast, hue, colour, whiteness are "
2271 "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
2272 cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
2273
2274 return 0;
2275}
2276
2277
2278/*--------------------------------------------------------------------------
2279 Update picture settings of the image sensor.
2280 Returns: 0 on success, a negative number otherwise.
2281 --------------------------------------------------------------------------*/
2282static int
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002283w9968cf_sensor_update_picture(struct w9968cf_device* cam,
2284 struct video_picture pict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
2286 int err = 0;
2287
2288 if ((!cam->sensor_initialized)
2289 || pict.contrast != cam->picture.contrast) {
2290 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002291 pict.contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (err)
2293 goto fail;
2294 DBG(4, "Contrast changed from %u to %u",
2295 cam->picture.contrast, pict.contrast)
2296 cam->picture.contrast = pict.contrast;
2297 }
2298
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002299 if (((!cam->sensor_initialized) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002301 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
2302 pict.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (err)
2304 goto fail;
2305 DBG(4, "Brightness changed from %u to %u",
2306 cam->picture.brightness, pict.brightness)
2307 cam->picture.brightness = pict.brightness;
2308 }
2309
2310 if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002311 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
2312 pict.colour);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (err)
2314 goto fail;
2315 DBG(4, "Colour changed from %u to %u",
2316 cam->picture.colour, pict.colour)
2317 cam->picture.colour = pict.colour;
2318 }
2319
2320 if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002321 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
2322 pict.hue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (err)
2324 goto fail;
2325 DBG(4, "Hue changed from %u to %u",
2326 cam->picture.hue, pict.hue)
2327 cam->picture.hue = pict.hue;
2328 }
2329
2330 return 0;
2331
2332fail:
2333 DBG(4, "Failed to change sensor picture setting")
2334 return err;
2335}
2336
2337
2338
2339/****************************************************************************
2340 * Camera configuration *
2341 ****************************************************************************/
2342
2343/*--------------------------------------------------------------------------
2344 This function is called when a supported image sensor is detected.
2345 Return 0 if the initialization succeeds, a negative number otherwise.
2346 --------------------------------------------------------------------------*/
2347static int w9968cf_sensor_init(struct w9968cf_device* cam)
2348{
2349 int err = 0;
2350
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002351 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
2352 &cam->monochrome)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto error;
2354
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002355 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
2356 &cam->sensor)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 goto error;
2358
2359 /* NOTE: Make sure width and height are a multiple of 16 */
2360 switch (cam->sensor_client->addr) {
2361 case OV6xx0_SID:
2362 cam->maxwidth = 352;
2363 cam->maxheight = 288;
2364 cam->minwidth = 64;
2365 cam->minheight = 48;
2366 break;
2367 case OV7xx0_SID:
2368 cam->maxwidth = 640;
2369 cam->maxheight = 480;
2370 cam->minwidth = 64;
2371 cam->minheight = 48;
2372 break;
2373 default:
2374 DBG(1, "Not supported image sensor detected for %s",
2375 symbolic(camlist, cam->id))
2376 return -EINVAL;
2377 }
2378
2379 /* These values depend on the ones in the ovxxx0.c sources */
2380 switch (cam->sensor) {
2381 case CC_OV7620:
2382 cam->start_cropx = 287;
2383 cam->start_cropy = 35;
2384 /* Seems to work around a bug in the image sensor */
2385 cam->vs_polarity = 1;
2386 cam->hs_polarity = 1;
2387 break;
2388 default:
2389 cam->start_cropx = 320;
2390 cam->start_cropy = 35;
2391 cam->vs_polarity = 1;
2392 cam->hs_polarity = 0;
2393 }
2394
2395 if ((err = w9968cf_sensor_update_settings(cam)))
2396 goto error;
2397
2398 if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
2399 goto error;
2400
2401 cam->sensor_initialized = 1;
2402
2403 DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
2404 return 0;
2405
2406error:
2407 cam->sensor_initialized = 0;
2408 cam->sensor = CC_UNKNOWN;
2409 DBG(1, "Image sensor initialization failed for %s (/dev/video%d). "
2410 "Try to detach and attach this device again",
2411 symbolic(camlist, cam->id), cam->v4ldev->minor)
2412 return err;
2413}
2414
2415
2416/*--------------------------------------------------------------------------
2417 Fill some basic fields in the main device data structure.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002418 This function is called once on w9968cf_usb_probe() for each recognized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 camera.
2420 --------------------------------------------------------------------------*/
2421static void
2422w9968cf_configure_camera(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002423 struct usb_device* udev,
2424 enum w9968cf_model_id mod_id,
2425 const unsigned short dev_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002427 mutex_init(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 init_waitqueue_head(&cam->open);
2429 spin_lock_init(&cam->urb_lock);
2430 spin_lock_init(&cam->flist_lock);
2431
2432 cam->users = 0;
2433 cam->disconnected = 0;
2434 cam->id = mod_id;
2435 cam->sensor = CC_UNKNOWN;
2436 cam->sensor_initialized = 0;
2437
2438 /* Calculate the alternate setting number (from 1 to 16)
2439 according to the 'packet_size' module parameter */
2440 if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
2441 packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
2442 for (cam->altsetting = 1;
2443 packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
2444 cam->altsetting++);
2445
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002446 cam->max_buffers = (max_buffers[dev_nr] < 2 ||
2447 max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
2448 ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002450 cam->double_buffer = (double_buffer[dev_nr] == 0 ||
2451 double_buffer[dev_nr] == 1)
2452 ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002455 ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 cam->filter_type = (filter_type[dev_nr] == 0 ||
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002458 filter_type[dev_nr] == 1 ||
2459 filter_type[dev_nr] == 2)
2460 ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 cam->capture = 1;
2463
2464 cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002465 ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002467 cam->decompression = (decompression[dev_nr] == 0 ||
2468 decompression[dev_nr] == 1 ||
2469 decompression[dev_nr] == 2)
2470 ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002472 cam->upscaling = (upscaling[dev_nr] == 0 ||
2473 upscaling[dev_nr] == 1)
2474 ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002477 ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479 cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002480 ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002483 ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002485 cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
2486 bandingfilter[dev_nr] == 1)
2487 ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002490 ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002493 ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002496 ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002499 ? monochrome[dev_nr] : W9968CF_MONOCHROME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 cam->picture.brightness = (u16)brightness[dev_nr];
2502 cam->picture.hue = (u16)hue[dev_nr];
2503 cam->picture.colour = (u16)colour[dev_nr];
2504 cam->picture.contrast = (u16)contrast[dev_nr];
2505 cam->picture.whiteness = (u16)whiteness[dev_nr];
2506 if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
2507 cam->picture.palette = (u16)force_palette[dev_nr];
2508 cam->force_palette = 1;
2509 } else {
2510 cam->force_palette = 0;
2511 if (cam->decompression == 0)
2512 cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
2513 else if (cam->decompression == 1)
2514 cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
2515 else
2516 cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
2517 }
2518 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2519
2520 cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002521 ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 cam->window.x = 0;
2524 cam->window.y = 0;
2525 cam->window.width = W9968CF_WIDTH;
2526 cam->window.height = W9968CF_HEIGHT;
2527 cam->window.chromakey = 0;
2528 cam->window.clipcount = 0;
2529 cam->window.flags = 0;
2530
2531 DBG(3, "%s configured with settings #%u:",
2532 symbolic(camlist, cam->id), dev_nr)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
2535 wMaxPacketSize[cam->altsetting-1])
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 DBG(3, "- Number of requested video frame buffers: %u",
2538 cam->max_buffers)
2539
2540 if (cam->double_buffer)
2541 DBG(3, "- Hardware double buffering enabled")
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002542 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 DBG(3, "- Hardware double buffering disabled")
2544
2545 if (cam->filter_type == 0)
2546 DBG(3, "- Video filtering disabled")
2547 else if (cam->filter_type == 1)
2548 DBG(3, "- Video filtering enabled: type 1-2-1")
2549 else if (cam->filter_type == 2)
2550 DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
2551
2552 if (cam->clamping)
2553 DBG(3, "- Video data clamping (CCIR-601 format) enabled")
2554 else
2555 DBG(3, "- Video data clamping (CCIR-601 format) disabled")
2556
2557 if (cam->largeview)
2558 DBG(3, "- Large view enabled")
2559 else
2560 DBG(3, "- Large view disabled")
2561
2562 if ((cam->decompression) == 0 && (!cam->force_palette))
2563 DBG(3, "- Decompression disabled")
2564 else if ((cam->decompression) == 1 && (!cam->force_palette))
2565 DBG(3, "- Decompression forced")
2566 else if ((cam->decompression) == 2 && (!cam->force_palette))
2567 DBG(3, "- Decompression allowed")
2568
2569 if (cam->upscaling)
2570 DBG(3, "- Software image scaling enabled")
2571 else
2572 DBG(3, "- Software image scaling disabled")
2573
2574 if (cam->force_palette)
2575 DBG(3, "- Image palette forced to %s",
2576 symbolic(v4l1_plist, cam->picture.palette))
2577
2578 if (cam->force_rgb)
2579 DBG(3, "- RGB component ordering will be used instead of BGR")
2580
2581 if (cam->auto_brt)
2582 DBG(3, "- Auto brightness enabled")
2583 else
2584 DBG(3, "- Auto brightness disabled")
2585
2586 if (cam->auto_exp)
2587 DBG(3, "- Auto exposure enabled")
2588 else
2589 DBG(3, "- Auto exposure disabled")
2590
2591 if (cam->backlight)
2592 DBG(3, "- Backlight exposure algorithm enabled")
2593 else
2594 DBG(3, "- Backlight exposure algorithm disabled")
2595
2596 if (cam->mirror)
2597 DBG(3, "- Mirror enabled")
2598 else
2599 DBG(3, "- Mirror disabled")
2600
2601 if (cam->bandfilt)
2602 DBG(3, "- Banding filter enabled")
2603 else
2604 DBG(3, "- Banding filter disabled")
2605
2606 DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
2607
2608 if (cam->clockdiv == -1)
2609 DBG(3, "- Automatic clock divisor enabled")
2610 else
2611 DBG(3, "- Clock divisor: %d", cam->clockdiv)
2612
2613 if (cam->monochrome)
2614 DBG(3, "- Image sensor used as monochrome")
2615 else
2616 DBG(3, "- Image sensor not used as monochrome")
2617}
2618
2619
2620/*--------------------------------------------------------------------------
2621 If the video post-processing module is not loaded, some parameters
2622 must be overridden.
2623 --------------------------------------------------------------------------*/
2624static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2625{
2626 if (!w9968cf_vpp) {
2627 if (cam->decompression == 1) {
2628 cam->decompression = 2;
2629 DBG(2, "Video post-processing module not found: "
2630 "'decompression' parameter forced to 2")
2631 }
2632 if (cam->upscaling) {
2633 cam->upscaling = 0;
2634 DBG(2, "Video post-processing module not found: "
2635 "'upscaling' parameter forced to 0")
2636 }
2637 if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
2638 cam->force_palette = 0;
2639 DBG(2, "Video post-processing module not found: "
2640 "'force_palette' parameter forced to 0")
2641 }
2642 cam->picture.palette = VIDEO_PALETTE_UYVY;
2643 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2644 }
2645}
2646
2647
2648/*--------------------------------------------------------------------------
2649 Release the resources used by the driver.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002650 This function is called on disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 (or on close if deallocation has been deferred)
2652 --------------------------------------------------------------------------*/
2653static void w9968cf_release_resources(struct w9968cf_device* cam)
2654{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002655 mutex_lock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->minor)
2658
2659 video_unregister_device(cam->v4ldev);
2660 list_del(&cam->v4llist);
2661 i2c_del_adapter(&cam->i2c_adapter);
2662 w9968cf_deallocate_memory(cam);
2663 kfree(cam->control_buffer);
2664 kfree(cam->data_buffer);
2665
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002666 mutex_unlock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
2669
2670
2671/****************************************************************************
2672 * Video4Linux interface *
2673 ****************************************************************************/
2674
2675static int w9968cf_open(struct inode* inode, struct file* filp)
2676{
2677 struct w9968cf_device* cam;
2678 int err;
2679
2680 /* This the only safe way to prevent race conditions with disconnect */
2681 if (!down_read_trylock(&w9968cf_disconnect))
Jiri Slaby3abff5572007-10-09 22:03:12 -03002682 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2685
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002686 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 if (cam->sensor == CC_UNKNOWN) {
2689 DBG(2, "No supported image sensor has been detected by the "
2690 "'ovcamchip' module for the %s (/dev/video%d). Make "
2691 "sure it is loaded *before* (re)connecting the camera.",
2692 symbolic(camlist, cam->id), cam->v4ldev->minor)
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002693 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 up_read(&w9968cf_disconnect);
2695 return -ENODEV;
2696 }
2697
2698 if (cam->users) {
2699 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
2700 symbolic(camlist, cam->id),cam->v4ldev->minor,cam->command)
2701 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002702 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 up_read(&w9968cf_disconnect);
2704 return -EWOULDBLOCK;
2705 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002706 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 err = wait_event_interruptible_exclusive(cam->open,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002708 cam->disconnected ||
2709 !cam->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (err) {
2711 up_read(&w9968cf_disconnect);
2712 return err;
2713 }
2714 if (cam->disconnected) {
2715 up_read(&w9968cf_disconnect);
2716 return -ENODEV;
2717 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002718 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
2720
2721 DBG(5, "Opening '%s', /dev/video%d ...",
2722 symbolic(camlist, cam->id), cam->v4ldev->minor)
2723
2724 cam->streaming = 0;
2725 cam->misconfigured = 0;
2726
Adrian Bunk7f2c01a2006-01-09 00:43:39 +01002727 w9968cf_adjust_configuration(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 if ((err = w9968cf_allocate_memory(cam)))
2730 goto deallocate_memory;
2731
2732 if ((err = w9968cf_init_chip(cam)))
2733 goto deallocate_memory;
2734
2735 if ((err = w9968cf_start_transfer(cam)))
2736 goto deallocate_memory;
2737
2738 filp->private_data = cam;
2739
2740 cam->users++;
2741 strcpy(cam->command, current->comm);
2742
2743 init_waitqueue_head(&cam->wait_queue);
2744
2745 DBG(5, "Video device is open")
2746
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002747 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 up_read(&w9968cf_disconnect);
2749
2750 return 0;
2751
2752deallocate_memory:
2753 w9968cf_deallocate_memory(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 DBG(2, "Failed to open the video device")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002755 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 up_read(&w9968cf_disconnect);
2757 return err;
2758}
2759
2760
2761static int w9968cf_release(struct inode* inode, struct file* filp)
2762{
2763 struct w9968cf_device* cam;
2764
2765 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2766
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002767 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 w9968cf_stop_transfer(cam);
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (cam->disconnected) {
2772 w9968cf_release_resources(cam);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002773 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 kfree(cam);
2775 return 0;
2776 }
2777
2778 cam->users--;
2779 w9968cf_deallocate_memory(cam);
2780 wake_up_interruptible_nr(&cam->open, 1);
2781
2782 DBG(5, "Video device closed")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002783 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return 0;
2785}
2786
2787
2788static ssize_t
2789w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2790{
2791 struct w9968cf_device* cam;
2792 struct w9968cf_frame_t* fr;
2793 int err = 0;
2794
2795 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2796
2797 if (filp->f_flags & O_NONBLOCK)
2798 return -EWOULDBLOCK;
2799
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002800 if (mutex_lock_interruptible(&cam->fileop_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return -ERESTARTSYS;
2802
2803 if (cam->disconnected) {
2804 DBG(2, "Device not present")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002805 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return -ENODEV;
2807 }
2808
2809 if (cam->misconfigured) {
2810 DBG(2, "The camera is misconfigured. Close and open it again.")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002811 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return -EIO;
2813 }
2814
2815 if (!cam->frame[0].queued)
2816 w9968cf_push_frame(cam, 0);
2817
2818 if (!cam->frame[1].queued)
2819 w9968cf_push_frame(cam, 1);
2820
2821 err = wait_event_interruptible(cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002822 cam->frame[0].status == F_READY ||
2823 cam->frame[1].status == F_READY ||
2824 cam->disconnected);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (err) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002826 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return err;
2828 }
2829 if (cam->disconnected) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002830 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return -ENODEV;
2832 }
2833
2834 fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
2835
2836 if (w9968cf_vpp)
2837 w9968cf_postprocess_frame(cam, fr);
2838
2839 if (count > fr->length)
2840 count = fr->length;
2841
2842 if (copy_to_user(buf, fr->buffer, count)) {
2843 fr->status = F_UNUSED;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002844 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return -EFAULT;
2846 }
2847 *f_pos += count;
2848
2849 fr->status = F_UNUSED;
2850
2851 DBG(5, "%zu bytes read", count)
2852
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002853 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return count;
2855}
2856
2857
2858static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
2859{
2860 struct w9968cf_device* cam = (struct w9968cf_device*)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002861 video_get_drvdata(video_devdata(filp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 unsigned long vsize = vma->vm_end - vma->vm_start,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002863 psize = cam->nbuffers * cam->frame[0].size,
2864 start = vma->vm_start,
2865 pos = (unsigned long)cam->frame[0].buffer,
2866 page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 if (cam->disconnected) {
2869 DBG(2, "Device not present")
2870 return -ENODEV;
2871 }
2872
2873 if (cam->misconfigured) {
2874 DBG(2, "The camera is misconfigured. Close and open it again")
2875 return -EIO;
2876 }
2877
2878 PDBGG("mmapping %lu bytes...", vsize)
2879
2880 if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
2881 return -EINVAL;
2882
2883 while (vsize > 0) {
2884 page = vmalloc_to_pfn((void *)pos);
2885 if (remap_pfn_range(vma, start, page + vma->vm_pgoff,
2886 PAGE_SIZE, vma->vm_page_prot))
2887 return -EAGAIN;
2888 start += PAGE_SIZE;
2889 pos += PAGE_SIZE;
2890 vsize -= PAGE_SIZE;
2891 }
2892
2893 DBG(5, "mmap method successfully called")
2894 return 0;
2895}
2896
2897
2898static int
2899w9968cf_ioctl(struct inode* inode, struct file* filp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002900 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 struct w9968cf_device* cam;
2903 int err;
2904
2905 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2906
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002907 if (mutex_lock_interruptible(&cam->fileop_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return -ERESTARTSYS;
2909
2910 if (cam->disconnected) {
2911 DBG(2, "Device not present")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002912 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return -ENODEV;
2914 }
2915
2916 if (cam->misconfigured) {
2917 DBG(2, "The camera is misconfigured. Close and open it again.")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002918 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return -EIO;
2920 }
2921
2922 err = w9968cf_v4l_ioctl(inode, filp, cmd, (void __user *)arg);
2923
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002924 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return err;
2926}
2927
2928
2929static int w9968cf_v4l_ioctl(struct inode* inode, struct file* filp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002930 unsigned int cmd, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 struct w9968cf_device* cam;
2933 const char* v4l1_ioctls[] = {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002934 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
2936 "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
2937 "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002938 "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
2939 "GVBIFMT", "SVBIFMT"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 };
2941
2942 #define V4L1_IOCTL(cmd) \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002943 ((_IOC_NR((cmd)) < ARRAY_SIZE(v4l1_ioctls)) ? \
2944 v4l1_ioctls[_IOC_NR((cmd))] : "?")
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2947
2948 switch (cmd) {
2949
2950 case VIDIOCGCAP: /* get video capability */
2951 {
2952 struct video_capability cap = {
2953 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
2954 .channels = 1,
2955 .audios = 0,
2956 .minwidth = cam->minwidth,
2957 .minheight = cam->minheight,
2958 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002959 sprintf(cap.name, "W996[87]CF USB Camera #%d",
2960 cam->v4ldev->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 cap.maxwidth = (cam->upscaling && w9968cf_vpp)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002962 ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2963 : cam->maxwidth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 cap.maxheight = (cam->upscaling && w9968cf_vpp)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002965 ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2966 : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 if (copy_to_user(arg, &cap, sizeof(cap)))
2969 return -EFAULT;
2970
2971 DBG(5, "VIDIOCGCAP successfully called")
2972 return 0;
2973 }
2974
2975 case VIDIOCGCHAN: /* get video channel informations */
2976 {
2977 struct video_channel chan;
2978 if (copy_from_user(&chan, arg, sizeof(chan)))
2979 return -EFAULT;
2980
2981 if (chan.channel != 0)
2982 return -EINVAL;
2983
2984 strcpy(chan.name, "Camera");
2985 chan.tuners = 0;
2986 chan.flags = 0;
2987 chan.type = VIDEO_TYPE_CAMERA;
2988 chan.norm = VIDEO_MODE_AUTO;
2989
2990 if (copy_to_user(arg, &chan, sizeof(chan)))
2991 return -EFAULT;
2992
2993 DBG(5, "VIDIOCGCHAN successfully called")
2994 return 0;
2995 }
2996
2997 case VIDIOCSCHAN: /* set active channel */
2998 {
2999 struct video_channel chan;
3000
3001 if (copy_from_user(&chan, arg, sizeof(chan)))
3002 return -EFAULT;
3003
3004 if (chan.channel != 0)
3005 return -EINVAL;
3006
3007 DBG(5, "VIDIOCSCHAN successfully called")
3008 return 0;
3009 }
3010
3011 case VIDIOCGPICT: /* get image properties of the picture */
3012 {
3013 if (w9968cf_sensor_get_picture(cam))
3014 return -EIO;
3015
3016 if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
3017 return -EFAULT;
3018
3019 DBG(5, "VIDIOCGPICT successfully called")
3020 return 0;
3021 }
3022
3023 case VIDIOCSPICT: /* change picture settings */
3024 {
3025 struct video_picture pict;
3026 int err = 0;
3027
3028 if (copy_from_user(&pict, arg, sizeof(pict)))
3029 return -EFAULT;
3030
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003031 if ( (cam->force_palette || !w9968cf_vpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 && pict.palette != cam->picture.palette ) {
3033 DBG(4, "Palette %s rejected: only %s is allowed",
3034 symbolic(v4l1_plist, pict.palette),
3035 symbolic(v4l1_plist, cam->picture.palette))
3036 return -EINVAL;
3037 }
3038
3039 if (!w9968cf_valid_palette(pict.palette)) {
3040 DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
3041 symbolic(v4l1_plist, pict.palette))
3042 return -EINVAL;
3043 }
3044
3045 if (!cam->force_palette) {
3046 if (cam->decompression == 0) {
3047 if (w9968cf_need_decompression(pict.palette)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003048 DBG(4, "Decompression disabled: palette %s is not "
3049 "allowed. VIDIOCSPICT failed",
3050 symbolic(v4l1_plist, pict.palette))
3051 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
3053 } else if (cam->decompression == 1) {
3054 if (!w9968cf_need_decompression(pict.palette)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003055 DBG(4, "Decompression forced: palette %s is not "
3056 "allowed. VIDIOCSPICT failed",
3057 symbolic(v4l1_plist, pict.palette))
3058 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060 }
3061 }
3062
3063 if (pict.depth != w9968cf_valid_depth(pict.palette)) {
3064 DBG(4, "Requested depth %u bpp is not valid for %s "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003065 "palette: ignored and changed to %u bpp",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 pict.depth, symbolic(v4l1_plist, pict.palette),
3067 w9968cf_valid_depth(pict.palette))
3068 pict.depth = w9968cf_valid_depth(pict.palette);
3069 }
3070
3071 if (pict.palette != cam->picture.palette) {
3072 if(*cam->requested_frame
3073 || cam->frame_current->queued) {
3074 err = wait_event_interruptible
3075 ( cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003076 cam->disconnected ||
3077 (!*cam->requested_frame &&
3078 !cam->frame_current->queued) );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (err)
3080 return err;
3081 if (cam->disconnected)
3082 return -ENODEV;
3083 }
3084
3085 if (w9968cf_stop_transfer(cam))
3086 goto ioctl_fail;
3087
3088 if (w9968cf_set_picture(cam, pict))
3089 goto ioctl_fail;
3090
3091 if (w9968cf_start_transfer(cam))
3092 goto ioctl_fail;
3093
3094 } else if (w9968cf_sensor_update_picture(cam, pict))
3095 return -EIO;
3096
3097
3098 DBG(5, "VIDIOCSPICT successfully called")
3099 return 0;
3100 }
3101
3102 case VIDIOCSWIN: /* set capture area */
3103 {
3104 struct video_window win;
3105 int err = 0;
3106
3107 if (copy_from_user(&win, arg, sizeof(win)))
3108 return -EFAULT;
3109
3110 DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
3111 "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
3112 win.x, win.y, win.width, win.height)
3113
3114 if (win.clipcount != 0 || win.flags != 0)
3115 return -EINVAL;
3116
3117 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003118 (u16*)&win.height))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 DBG(4, "Resolution not supported (%ux%u). "
3120 "VIDIOCSWIN failed", win.width, win.height)
3121 return err;
3122 }
3123
3124 if (win.x != cam->window.x ||
3125 win.y != cam->window.y ||
3126 win.width != cam->window.width ||
3127 win.height != cam->window.height) {
3128 if(*cam->requested_frame
3129 || cam->frame_current->queued) {
3130 err = wait_event_interruptible
3131 ( cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003132 cam->disconnected ||
3133 (!*cam->requested_frame &&
3134 !cam->frame_current->queued) );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (err)
3136 return err;
3137 if (cam->disconnected)
3138 return -ENODEV;
3139 }
3140
3141 if (w9968cf_stop_transfer(cam))
3142 goto ioctl_fail;
3143
3144 /* This _must_ be called before set_window() */
3145 if (w9968cf_set_picture(cam, cam->picture))
3146 goto ioctl_fail;
3147
3148 if (w9968cf_set_window(cam, win))
3149 goto ioctl_fail;
3150
3151 if (w9968cf_start_transfer(cam))
3152 goto ioctl_fail;
3153 }
3154
3155 DBG(5, "VIDIOCSWIN successfully called. ")
3156 return 0;
3157 }
3158
3159 case VIDIOCGWIN: /* get current window properties */
3160 {
3161 if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
3162 return -EFAULT;
3163
3164 DBG(5, "VIDIOCGWIN successfully called")
3165 return 0;
3166 }
3167
3168 case VIDIOCGMBUF: /* request for memory (mapped) buffer */
3169 {
3170 struct video_mbuf mbuf;
3171 u8 i;
3172
3173 mbuf.size = cam->nbuffers * cam->frame[0].size;
3174 mbuf.frames = cam->nbuffers;
3175 for (i = 0; i < cam->nbuffers; i++)
3176 mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003177 (unsigned long)cam->frame[0].buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
3180 return -EFAULT;
3181
3182 DBG(5, "VIDIOCGMBUF successfully called")
3183 return 0;
3184 }
3185
3186 case VIDIOCMCAPTURE: /* start the capture to a frame */
3187 {
3188 struct video_mmap mmap;
3189 struct w9968cf_frame_t* fr;
3190 int err = 0;
3191
3192 if (copy_from_user(&mmap, arg, sizeof(mmap)))
3193 return -EFAULT;
3194
3195 DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003196 mmap.frame, symbolic(v4l1_plist, mmap.format),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 mmap.width, mmap.height)
3198
3199 if (mmap.frame >= cam->nbuffers) {
3200 DBG(4, "Invalid frame number (%u). "
3201 "VIDIOCMCAPTURE failed", mmap.frame)
3202 return -EINVAL;
3203 }
3204
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003205 if (mmap.format!=cam->picture.palette &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 (cam->force_palette || !w9968cf_vpp)) {
3207 DBG(4, "Palette %s rejected: only %s is allowed",
3208 symbolic(v4l1_plist, mmap.format),
3209 symbolic(v4l1_plist, cam->picture.palette))
3210 return -EINVAL;
3211 }
3212
3213 if (!w9968cf_valid_palette(mmap.format)) {
3214 DBG(4, "Palette %s not supported. "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003215 "VIDIOCMCAPTURE failed",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 symbolic(v4l1_plist, mmap.format))
3217 return -EINVAL;
3218 }
3219
3220 if (!cam->force_palette) {
3221 if (cam->decompression == 0) {
3222 if (w9968cf_need_decompression(mmap.format)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003223 DBG(4, "Decompression disabled: palette %s is not "
3224 "allowed. VIDIOCSPICT failed",
3225 symbolic(v4l1_plist, mmap.format))
3226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 }
3228 } else if (cam->decompression == 1) {
3229 if (!w9968cf_need_decompression(mmap.format)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003230 DBG(4, "Decompression forced: palette %s is not "
3231 "allowed. VIDIOCSPICT failed",
3232 symbolic(v4l1_plist, mmap.format))
3233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
3235 }
3236 }
3237
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003238 if ((err = w9968cf_adjust_window_size(cam, (u16*)&mmap.width,
3239 (u16*)&mmap.height))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 DBG(4, "Resolution not supported (%dx%d). "
3241 "VIDIOCMCAPTURE failed",
3242 mmap.width, mmap.height)
3243 return err;
3244 }
3245
3246 fr = &cam->frame[mmap.frame];
3247
3248 if (mmap.width != cam->window.width ||
3249 mmap.height != cam->window.height ||
3250 mmap.format != cam->picture.palette) {
3251
3252 struct video_window win;
3253 struct video_picture pict;
3254
3255 if(*cam->requested_frame
3256 || cam->frame_current->queued) {
3257 DBG(6, "VIDIOCMCAPTURE. Change settings for "
3258 "frame #%u: %dx%d, format %s. Wait...",
3259 mmap.frame, mmap.width, mmap.height,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003260 symbolic(v4l1_plist, mmap.format))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 err = wait_event_interruptible
3262 ( cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003263 cam->disconnected ||
3264 (!*cam->requested_frame &&
3265 !cam->frame_current->queued) );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (err)
3267 return err;
3268 if (cam->disconnected)
3269 return -ENODEV;
3270 }
3271
3272 memcpy(&win, &cam->window, sizeof(win));
3273 memcpy(&pict, &cam->picture, sizeof(pict));
3274 win.width = mmap.width;
3275 win.height = mmap.height;
3276 pict.palette = mmap.format;
3277
3278 if (w9968cf_stop_transfer(cam))
3279 goto ioctl_fail;
3280
3281 /* This before set_window */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003282 if (w9968cf_set_picture(cam, pict))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 goto ioctl_fail;
3284
3285 if (w9968cf_set_window(cam, win))
3286 goto ioctl_fail;
3287
3288 if (w9968cf_start_transfer(cam))
3289 goto ioctl_fail;
3290
3291 } else if (fr->queued) {
3292
3293 DBG(6, "Wait until frame #%u is free", mmap.frame)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003294
3295 err = wait_event_interruptible(cam->wait_queue,
3296 cam->disconnected ||
3297 (!fr->queued));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (err)
3299 return err;
3300 if (cam->disconnected)
3301 return -ENODEV;
3302 }
3303
3304 w9968cf_push_frame(cam, mmap.frame);
3305 DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
3306 return 0;
3307 }
3308
3309 case VIDIOCSYNC: /* wait until the capture of a frame is finished */
3310 {
3311 unsigned int f_num;
3312 struct w9968cf_frame_t* fr;
3313 int err = 0;
3314
3315 if (copy_from_user(&f_num, arg, sizeof(f_num)))
3316 return -EFAULT;
3317
3318 if (f_num >= cam->nbuffers) {
3319 DBG(4, "Invalid frame number (%u). "
3320 "VIDIOCMCAPTURE failed", f_num)
3321 return -EINVAL;
3322 }
3323
3324 DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
3325
3326 fr = &cam->frame[f_num];
3327
3328 switch (fr->status) {
3329 case F_UNUSED:
3330 if (!fr->queued) {
3331 DBG(4, "VIDIOSYNC: Frame #%u not requested!",
3332 f_num)
3333 return -EFAULT;
3334 }
3335 case F_ERROR:
3336 case F_GRABBING:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003337 err = wait_event_interruptible(cam->wait_queue,
3338 (fr->status == F_READY)
3339 || cam->disconnected);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (err)
3341 return err;
3342 if (cam->disconnected)
3343 return -ENODEV;
3344 break;
3345 case F_READY:
3346 break;
3347 }
3348
3349 if (w9968cf_vpp)
3350 w9968cf_postprocess_frame(cam, fr);
3351
3352 fr->status = F_UNUSED;
3353
3354 DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
3355 return 0;
3356 }
3357
3358 case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
3359 {
3360 struct video_unit unit = {
3361 .video = cam->v4ldev->minor,
3362 .vbi = VIDEO_NO_UNIT,
3363 .radio = VIDEO_NO_UNIT,
3364 .audio = VIDEO_NO_UNIT,
3365 .teletext = VIDEO_NO_UNIT,
3366 };
3367
3368 if (copy_to_user(arg, &unit, sizeof(unit)))
3369 return -EFAULT;
3370
3371 DBG(5, "VIDIOCGUNIT successfully called")
3372 return 0;
3373 }
3374
3375 case VIDIOCKEY:
3376 return 0;
3377
3378 case VIDIOCGFBUF:
3379 {
3380 if (clear_user(arg, sizeof(struct video_buffer)))
3381 return -EFAULT;
3382
3383 DBG(5, "VIDIOCGFBUF successfully called")
3384 return 0;
3385 }
3386
3387 case VIDIOCGTUNER:
3388 {
3389 struct video_tuner tuner;
3390 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3391 return -EFAULT;
3392
3393 if (tuner.tuner != 0)
3394 return -EINVAL;
3395
3396 strcpy(tuner.name, "no_tuner");
3397 tuner.rangelow = 0;
3398 tuner.rangehigh = 0;
3399 tuner.flags = VIDEO_TUNER_NORM;
3400 tuner.mode = VIDEO_MODE_AUTO;
3401 tuner.signal = 0xffff;
3402
3403 if (copy_to_user(arg, &tuner, sizeof(tuner)))
3404 return -EFAULT;
3405
3406 DBG(5, "VIDIOCGTUNER successfully called")
3407 return 0;
3408 }
3409
3410 case VIDIOCSTUNER:
3411 {
3412 struct video_tuner tuner;
3413 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3414 return -EFAULT;
3415
3416 if (tuner.tuner != 0)
3417 return -EINVAL;
3418
3419 if (tuner.mode != VIDEO_MODE_AUTO)
3420 return -EINVAL;
3421
3422 DBG(5, "VIDIOCSTUNER successfully called")
3423 return 0;
3424 }
3425
3426 case VIDIOCSFBUF:
3427 case VIDIOCCAPTURE:
3428 case VIDIOCGFREQ:
3429 case VIDIOCSFREQ:
3430 case VIDIOCGAUDIO:
3431 case VIDIOCSAUDIO:
3432 case VIDIOCSPLAYMODE:
3433 case VIDIOCSWRITEMODE:
3434 case VIDIOCGPLAYINFO:
3435 case VIDIOCSMICROCODE:
3436 case VIDIOCGVBIFMT:
3437 case VIDIOCSVBIFMT:
3438 DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
3439 "(type 0x%01X, "
3440 "n. 0x%01X, "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003441 "dir. 0x%01X, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 "size 0x%02X)",
3443 V4L1_IOCTL(cmd),
3444 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3445
3446 return -EINVAL;
3447
3448 default:
3449 DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
3450 "type 0x%01X, "
3451 "n. 0x%01X, "
3452 "dir. 0x%01X, "
3453 "size 0x%02X",
3454 V4L1_IOCTL(cmd),
3455 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3456
3457 return -ENOIOCTLCMD;
3458
3459 } /* end of switch */
3460
3461ioctl_fail:
3462 cam->misconfigured = 1;
3463 DBG(1, "VIDIOC%s failed because of hardware problems. "
3464 "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
3465 return -EFAULT;
3466}
3467
3468
Arjan van de Venfa027c22007-02-12 00:55:33 -08003469static const struct file_operations w9968cf_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 .owner = THIS_MODULE,
3471 .open = w9968cf_open,
3472 .release = w9968cf_release,
3473 .read = w9968cf_read,
3474 .ioctl = w9968cf_ioctl,
Arnd Bergmann0d0fbf82006-01-09 15:24:57 -02003475 .compat_ioctl = v4l_compat_ioctl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 .mmap = w9968cf_mmap,
3477 .llseek = no_llseek,
3478};
3479
3480
3481
3482/****************************************************************************
3483 * USB probe and V4L registration, disconnect and id_table[] definition *
3484 ****************************************************************************/
3485
3486static int
3487w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3488{
3489 struct usb_device *udev = interface_to_usbdev(intf);
3490 struct w9968cf_device* cam;
3491 int err = 0;
3492 enum w9968cf_model_id mod_id;
3493 struct list_head* ptr;
3494 u8 sc = 0; /* number of simultaneous cameras */
3495 static unsigned short dev_nr = 0; /* we are handling device number n */
3496
3497 if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[0].idVendor &&
3498 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[0].idProduct)
3499 mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
3500 else if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[1].idVendor &&
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003501 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[1].idProduct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
3503 else
3504 return -ENODEV;
3505
3506 cam = (struct w9968cf_device*)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003507 kzalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 if (!cam)
3509 return -ENOMEM;
3510
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003511 mutex_init(&cam->dev_mutex);
3512 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 cam->usbdev = udev;
3515 /* NOTE: a local copy is used to avoid possible race conditions */
3516 memcpy(&cam->dev, &udev->dev, sizeof(struct device));
3517
3518 DBG(2, "%s detected", symbolic(camlist, mod_id))
3519
3520 if (simcams > W9968CF_MAX_DEVICES)
3521 simcams = W9968CF_SIMCAMS;
3522
3523 /* How many cameras are connected ? */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003524 mutex_lock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 list_for_each(ptr, &w9968cf_dev_list)
3526 sc++;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003527 mutex_unlock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
3529 if (sc >= simcams) {
3530 DBG(2, "Device rejected: too many connected cameras "
3531 "(max. %u)", simcams)
3532 err = -EPERM;
3533 goto fail;
3534 }
3535
3536
3537 /* Allocate 2 bytes of memory for camera control USB transfers */
Oliver Neukumb10b4172006-01-06 21:28:40 +01003538 if (!(cam->control_buffer = kzalloc(2, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 DBG(1,"Couldn't allocate memory for camera control transfers")
3540 err = -ENOMEM;
3541 goto fail;
3542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
3544 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
Oliver Neukumb10b4172006-01-06 21:28:40 +01003545 if (!(cam->data_buffer = kzalloc(8, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 DBG(1, "Couldn't allocate memory for data "
3547 "transfers to the FSB")
3548 err = -ENOMEM;
3549 goto fail;
3550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552 /* Register the V4L device */
3553 cam->v4ldev = video_device_alloc();
3554 if (!cam->v4ldev) {
3555 DBG(1, "Could not allocate memory for a V4L structure")
3556 err = -ENOMEM;
3557 goto fail;
3558 }
3559
3560 strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
3561 cam->v4ldev->owner = THIS_MODULE;
3562 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
3563 cam->v4ldev->hardware = VID_HARDWARE_W9968CF;
3564 cam->v4ldev->fops = &w9968cf_fops;
3565 cam->v4ldev->minor = video_nr[dev_nr];
3566 cam->v4ldev->release = video_device_release;
3567 video_set_drvdata(cam->v4ldev, cam);
3568 cam->v4ldev->dev = &cam->dev;
3569
3570 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003571 video_nr[dev_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 if (err) {
3573 DBG(1, "V4L device registration failed")
3574 if (err == -ENFILE && video_nr[dev_nr] == -1)
3575 DBG(2, "Couldn't find a free /dev/videoX node")
3576 video_nr[dev_nr] = -1;
3577 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3578 goto fail;
3579 }
3580
3581 DBG(2, "V4L device registered as /dev/video%d", cam->v4ldev->minor)
3582
3583 /* Set some basic constants */
3584 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3585
3586 /* Add a new entry into the list of V4L registered devices */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003587 mutex_lock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 list_add(&cam->v4llist, &w9968cf_dev_list);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003589 mutex_unlock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3591
3592 w9968cf_turn_on_led(cam);
3593
3594 w9968cf_i2c_init(cam);
3595
3596 usb_set_intfdata(intf, cam);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003597 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 return 0;
3599
3600fail: /* Free unused memory */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003601 kfree(cam->control_buffer);
3602 kfree(cam->data_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 if (cam->v4ldev)
3604 video_device_release(cam->v4ldev);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003605 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 kfree(cam);
3607 return err;
3608}
3609
3610
3611static void w9968cf_usb_disconnect(struct usb_interface* intf)
3612{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003613 struct w9968cf_device* cam =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 (struct w9968cf_device*)usb_get_intfdata(intf);
3615
3616 down_write(&w9968cf_disconnect);
3617
3618 if (cam) {
3619 /* Prevent concurrent accesses to data */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003620 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
3622 cam->disconnected = 1;
3623
3624 DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id))
3625
3626 wake_up_interruptible_all(&cam->open);
3627
3628 if (cam->users) {
3629 DBG(2, "The device is open (/dev/video%d)! "
3630 "Process name: %s. Deregistration and memory "
3631 "deallocation are deferred on close.",
3632 cam->v4ldev->minor, cam->command)
3633 cam->misconfigured = 1;
3634 w9968cf_stop_transfer(cam);
3635 wake_up_interruptible(&cam->wait_queue);
3636 } else
3637 w9968cf_release_resources(cam);
3638
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003639 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 if (!cam->users)
3642 kfree(cam);
3643 }
3644
3645 up_write(&w9968cf_disconnect);
3646}
3647
3648
3649static struct usb_driver w9968cf_usb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 .name = "w9968cf",
3651 .id_table = winbond_id_table,
3652 .probe = w9968cf_usb_probe,
3653 .disconnect = w9968cf_usb_disconnect,
3654};
3655
3656
3657
3658/****************************************************************************
3659 * Module init, exit and intermodule communication *
3660 ****************************************************************************/
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662static int __init w9968cf_module_init(void)
3663{
3664 int err;
3665
3666 KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
3667 KDBG(3, W9968CF_MODULE_AUTHOR)
3668
3669 if (ovmod_load)
3670 request_module("ovcamchip");
3671
3672 if ((err = usb_register(&w9968cf_usb_driver)))
3673 return err;
3674
3675 return 0;
3676}
3677
3678
3679static void __exit w9968cf_module_exit(void)
3680{
3681 /* w9968cf_usb_disconnect() will be called */
3682 usb_deregister(&w9968cf_usb_driver);
3683
3684 KDBG(2, W9968CF_MODULE_NAME" deregistered")
3685}
3686
3687
3688module_init(w9968cf_module_init);
3689module_exit(w9968cf_module_exit);
3690