blob: 3318be5688c9aa65cdca3686ae4b8c9ff1f9ff1a [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>
Mauro Carvalho Chehab28644622008-07-20 20:26:54 -030045#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include "w9968cf.h"
48#include "w9968cf_decoder.h"
49
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010050static struct w9968cf_vpp_t* w9968cf_vpp;
51static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
52
53static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
54static DEFINE_MUTEX(w9968cf_devlist_mutex); /* semaphore for list traversal */
55
56static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58
59/****************************************************************************
60 * Module macros and parameters *
61 ****************************************************************************/
62
63MODULE_DEVICE_TABLE(usb, winbond_id_table);
64
65MODULE_AUTHOR(W9968CF_MODULE_AUTHOR" "W9968CF_AUTHOR_EMAIL);
66MODULE_DESCRIPTION(W9968CF_MODULE_NAME);
67MODULE_VERSION(W9968CF_MODULE_VERSION);
68MODULE_LICENSE(W9968CF_MODULE_LICENSE);
69MODULE_SUPPORTED_DEVICE("Video");
70
71static int ovmod_load = W9968CF_OVMOD_LOAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static unsigned short simcams = W9968CF_SIMCAMS;
73static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030074static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
75 W9968CF_PACKET_SIZE};
76static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
77 W9968CF_BUFFERS};
78static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
79 W9968CF_DOUBLE_BUFFER};
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030081static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
82 W9968CF_FILTER_TYPE};
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030084static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
85 W9968CF_DECOMPRESSION};
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
87static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
88static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
89static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
90static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030091static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
92 W9968CF_LIGHTFREQ};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030094 W9968CF_BANDINGFILTER};
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
96static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
97static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
98static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030099static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
100 W9968CF_BRIGHTNESS};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
102static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300103static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
104 W9968CF_CONTRAST};
105static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
106 W9968CF_WHITENESS};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#ifdef W9968CF_DEBUG
108static unsigned short debug = W9968CF_DEBUG_LEVEL;
109static int specific_debug = W9968CF_SPECIFIC_DEBUG;
110#endif
111
112static unsigned int param_nv[24]; /* number of values per parameter */
113
Johannes Berga65e5d72008-07-09 10:28:38 +0200114#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115module_param(ovmod_load, bool, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#endif
117module_param(simcams, ushort, 0644);
118module_param_array(video_nr, short, &param_nv[0], 0444);
119module_param_array(packet_size, uint, &param_nv[1], 0444);
120module_param_array(max_buffers, ushort, &param_nv[2], 0444);
121module_param_array(double_buffer, bool, &param_nv[3], 0444);
122module_param_array(clamping, bool, &param_nv[4], 0444);
123module_param_array(filter_type, ushort, &param_nv[5], 0444);
124module_param_array(largeview, bool, &param_nv[6], 0444);
125module_param_array(decompression, ushort, &param_nv[7], 0444);
126module_param_array(upscaling, bool, &param_nv[8], 0444);
127module_param_array(force_palette, ushort, &param_nv[9], 0444);
128module_param_array(force_rgb, ushort, &param_nv[10], 0444);
129module_param_array(autobright, bool, &param_nv[11], 0444);
130module_param_array(autoexp, bool, &param_nv[12], 0444);
131module_param_array(lightfreq, ushort, &param_nv[13], 0444);
132module_param_array(bandingfilter, bool, &param_nv[14], 0444);
133module_param_array(clockdiv, short, &param_nv[15], 0444);
134module_param_array(backlight, bool, &param_nv[16], 0444);
135module_param_array(mirror, bool, &param_nv[17], 0444);
136module_param_array(monochrome, bool, &param_nv[18], 0444);
137module_param_array(brightness, uint, &param_nv[19], 0444);
138module_param_array(hue, uint, &param_nv[20], 0444);
139module_param_array(colour, uint, &param_nv[21], 0444);
140module_param_array(contrast, uint, &param_nv[22], 0444);
141module_param_array(whiteness, uint, &param_nv[23], 0444);
142#ifdef W9968CF_DEBUG
143module_param(debug, ushort, 0644);
144module_param(specific_debug, bool, 0644);
145#endif
146
Johannes Berga65e5d72008-07-09 10:28:38 +0200147#ifdef CONFIG_MODULES
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300148MODULE_PARM_DESC(ovmod_load,
149 "\n<0|1> Automatic 'ovcamchip' module loading."
150 "\n0 disabled, 1 enabled."
151 "\nIf enabled,'insmod' searches for the required 'ovcamchip'"
152 "\nmodule in the system, according to its configuration, and"
153 "\nattempts to load that module automatically. This action is"
154 "\nperformed once as soon as the 'w9968cf' module is loaded"
155 "\ninto memory."
156 "\nDefault value is "__MODULE_STRING(W9968CF_OVMOD_LOAD)"."
157 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#endif
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300159MODULE_PARM_DESC(simcams,
160 "\n<n> Number of cameras allowed to stream simultaneously."
161 "\nn may vary from 0 to "
162 __MODULE_STRING(W9968CF_MAX_DEVICES)"."
163 "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
164 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165MODULE_PARM_DESC(video_nr,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300166 "\n<-1|n[,...]> Specify V4L minor mode number."
167 "\n -1 = use next available (default)"
168 "\n n = use minor number n (integer >= 0)"
169 "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
170 " cameras this way."
171 "\nFor example:"
172 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
173 "\nthe second camera and use auto for the first"
174 "\none and for every other camera."
175 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176MODULE_PARM_DESC(packet_size,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300177 "\n<n[,...]> Specify the maximum data payload"
178 "\nsize in bytes for alternate settings, for each device."
179 "\nn is scaled between 63 and 1023 "
180 "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
181 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182MODULE_PARM_DESC(max_buffers,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300183 "\n<n[,...]> For advanced users."
184 "\nSpecify the maximum number of video frame buffers"
185 "\nto allocate for each device, from 2 to "
186 __MODULE_STRING(W9968CF_MAX_BUFFERS)
187 ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
188 "\n");
189MODULE_PARM_DESC(double_buffer,
190 "\n<0|1[,...]> "
191 "Hardware double buffering: 0 disabled, 1 enabled."
192 "\nIt should be enabled if you want smooth video output: if"
193 "\nyou obtain out of sync. video, disable it, or try to"
194 "\ndecrease the 'clockdiv' module parameter value."
195 "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
196 " for every device."
197 "\n");
198MODULE_PARM_DESC(clamping,
199 "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
200 "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
201 " for every device."
202 "\n");
203MODULE_PARM_DESC(filter_type,
204 "\n<0|1|2[,...]> Video filter type."
205 "\n0 none, 1 (1-2-1) 3-tap filter, "
206 "2 (2-3-6-3-2) 5-tap filter."
207 "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
208 " for every device."
209 "\nThe filter is used to reduce noise and aliasing artifacts"
210 "\nproduced by the CCD or CMOS image sensor, and the scaling"
211 " process."
212 "\n");
213MODULE_PARM_DESC(largeview,
214 "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
215 "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
216 " for every device."
217 "\n");
218MODULE_PARM_DESC(upscaling,
219 "\n<0|1[,...]> Software scaling (for non-compressed video):"
220 "\n0 disabled, 1 enabled."
221 "\nDisable it if you have a slow CPU or you don't have"
222 " enough memory."
223 "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
224 " for every device."
225 "\nIf 'w9968cf-vpp' is not present, this parameter is"
226 " set to 0."
227 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228MODULE_PARM_DESC(decompression,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300229 "\n<0|1|2[,...]> Software video decompression:"
230 "\n- 0 disables decompression (doesn't allow formats needing"
231 " decompression)"
232 "\n- 1 forces decompression (allows formats needing"
233 " decompression only);"
234 "\n- 2 allows any permitted formats."
235 "\nFormats supporting compressed video are YUV422P and"
236 " YUV420P/YUV420 "
237 "\nin any resolutions where both width and height are "
238 "a multiple of 16."
239 "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
240 " for every device."
241 "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
242 "\nnot allowed; in this case this parameter is set to 2."
243 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244MODULE_PARM_DESC(force_palette,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300245 "\n<0"
246 "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
247 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
248 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
249 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
250 "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
251 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
252 "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
253 "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
254 "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
255 "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
256 "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
257 "[,...]>"
258 " Force picture palette."
259 "\nIn order:"
260 "\n- 0 allows any of the following formats:"
261 "\n- UYVY 16 bpp - Original video, compression disabled"
262 "\n- YUV420 12 bpp - Original video, compression enabled"
263 "\n- YUV422P 16 bpp - Original video, compression enabled"
264 "\n- YUV420P 12 bpp - Original video, compression enabled"
265 "\n- YUVY 16 bpp - Software conversion from UYVY"
266 "\n- YUV422 16 bpp - Software conversion from UYVY"
267 "\n- GREY 8 bpp - Software conversion from UYVY"
268 "\n- RGB555 16 bpp - Software conversion from UYVY"
269 "\n- RGB565 16 bpp - Software conversion from UYVY"
270 "\n- RGB24 24 bpp - Software conversion from UYVY"
271 "\n- RGB32 32 bpp - Software conversion from UYVY"
272 "\nWhen not 0, this parameter will override 'decompression'."
273 "\nDefault value is 0 for every device."
274 "\nInitial palette is "
275 __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
276 "\nIf 'w9968cf-vpp' is not present, this parameter is"
277 " set to 9 (UYVY)."
278 "\n");
279MODULE_PARM_DESC(force_rgb,
280 "\n<0|1[,...]> Read RGB video data instead of BGR:"
281 "\n 1 = use RGB component ordering."
282 "\n 0 = use BGR component ordering."
283 "\nThis parameter has effect when using RGBX palettes only."
284 "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
285 " for every device."
286 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287MODULE_PARM_DESC(autobright,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300288 "\n<0|1[,...]> Image sensor automatically changes brightness:"
289 "\n 0 = no, 1 = yes"
290 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
291 " for every device."
292 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293MODULE_PARM_DESC(autoexp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300294 "\n<0|1[,...]> Image sensor automatically changes exposure:"
295 "\n 0 = no, 1 = yes"
296 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
297 " for every device."
298 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299MODULE_PARM_DESC(lightfreq,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300300 "\n<50|60[,...]> Light frequency in Hz:"
301 "\n 50 for European and Asian lighting,"
302 " 60 for American lighting."
303 "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
304 " for every device."
305 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306MODULE_PARM_DESC(bandingfilter,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300307 "\n<0|1[,...]> Banding filter to reduce effects of"
308 " fluorescent lighting:"
309 "\n 0 disabled, 1 enabled."
310 "\nThis filter tries to reduce the pattern of horizontal"
311 "\nlight/dark bands caused by some (usually fluorescent)"
312 " lighting."
313 "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
314 " for every device."
315 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316MODULE_PARM_DESC(clockdiv,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300317 "\n<-1|n[,...]> "
318 "Force pixel clock divisor to a specific value (for experts):"
319 "\n n may vary from 0 to 127."
320 "\n -1 for automatic value."
321 "\nSee also the 'double_buffer' module parameter."
322 "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
323 " for every device."
324 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325MODULE_PARM_DESC(backlight,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300326 "\n<0|1[,...]> Objects are lit from behind:"
327 "\n 0 = no, 1 = yes"
328 "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
329 " for every device."
330 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331MODULE_PARM_DESC(mirror,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300332 "\n<0|1[,...]> Reverse image horizontally:"
333 "\n 0 = no, 1 = yes"
334 "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
335 " for every device."
336 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337MODULE_PARM_DESC(monochrome,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300338 "\n<0|1[,...]> Use image sensor as monochrome sensor:"
339 "\n 0 = no, 1 = yes"
340 "\nNot all the sensors support monochrome color."
341 "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
342 " for every device."
343 "\n");
344MODULE_PARM_DESC(brightness,
345 "\n<n[,...]> Set picture brightness (0-65535)."
346 "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
347 " for every device."
348 "\nThis parameter has no effect if 'autobright' is enabled."
349 "\n");
350MODULE_PARM_DESC(hue,
351 "\n<n[,...]> Set picture hue (0-65535)."
352 "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
353 " for every device."
354 "\n");
355MODULE_PARM_DESC(colour,
356 "\n<n[,...]> Set picture saturation (0-65535)."
357 "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
358 " for every device."
359 "\n");
360MODULE_PARM_DESC(contrast,
361 "\n<n[,...]> Set picture contrast (0-65535)."
362 "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
363 " for every device."
364 "\n");
365MODULE_PARM_DESC(whiteness,
366 "\n<n[,...]> Set picture whiteness (0-65535)."
367 "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
368 " for every device."
369 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#ifdef W9968CF_DEBUG
371MODULE_PARM_DESC(debug,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300372 "\n<n> Debugging information level, from 0 to 6:"
373 "\n0 = none (use carefully)"
374 "\n1 = critical errors"
375 "\n2 = significant informations"
376 "\n3 = configuration or general messages"
377 "\n4 = warnings"
378 "\n5 = called functions"
379 "\n6 = function internals"
380 "\nLevel 5 and 6 are useful for testing only, when only "
381 "one device is used."
382 "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
383 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384MODULE_PARM_DESC(specific_debug,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300385 "\n<0|1> Enable or disable specific debugging messages:"
386 "\n0 = print messages concerning every level"
387 " <= 'debug' level."
388 "\n1 = print messages concerning the level"
389 " indicated by 'debug'."
390 "\nDefault value is "
391 __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
392 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#endif /* W9968CF_DEBUG */
394
395
396
397/****************************************************************************
398 * Some prototypes *
399 ****************************************************************************/
400
401/* Video4linux interface */
Hans Verkuilbec43662008-12-30 06:58:20 -0300402static const struct v4l2_file_operations w9968cf_fops;
403static int w9968cf_open(struct file *);
404static int w9968cf_release(struct file *);
405static int w9968cf_mmap(struct file *, struct vm_area_struct *);
Hans Verkuil069b7472008-12-30 07:04:34 -0300406static long w9968cf_ioctl(struct file *, unsigned, unsigned long);
Hans Verkuilbec43662008-12-30 06:58:20 -0300407static ssize_t w9968cf_read(struct file *, char __user *, size_t, loff_t *);
Hans Verkuil069b7472008-12-30 07:04:34 -0300408static long w9968cf_v4l_ioctl(struct file *, unsigned int,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300409 void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411/* USB-specific */
412static int w9968cf_start_transfer(struct w9968cf_device*);
413static int w9968cf_stop_transfer(struct w9968cf_device*);
414static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
415static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
416static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
417static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
418static int w9968cf_read_sb(struct w9968cf_device*);
419static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
David Howells7d12e782006-10-05 14:55:46 +0100420static void w9968cf_urb_complete(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422/* Low-level I2C (SMBus) I/O */
423static int w9968cf_smbus_start(struct w9968cf_device*);
424static int w9968cf_smbus_stop(struct w9968cf_device*);
425static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
426static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
427static int w9968cf_smbus_write_ack(struct w9968cf_device*);
428static int w9968cf_smbus_read_ack(struct w9968cf_device*);
429static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
430static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300431 u16 address, u8* value);
432static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
433 u8 subaddress, u8* value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300435 u16 address, u8 subaddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300437 u16 address, u8 subaddress,
438 u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440/* I2C interface to kernel */
441static int w9968cf_i2c_init(struct w9968cf_device*);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300442static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
443 unsigned short flags, char read_write,
444 u8 command, int size, union i2c_smbus_data*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static u32 w9968cf_i2c_func(struct i2c_adapter*);
446static int w9968cf_i2c_attach_inform(struct i2c_client*);
447static int w9968cf_i2c_detach_inform(struct i2c_client*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449/* Memory management */
450static void* rvmalloc(unsigned long size);
451static void rvfree(void *mem, unsigned long size);
452static void w9968cf_deallocate_memory(struct w9968cf_device*);
453static int w9968cf_allocate_memory(struct w9968cf_device*);
454
455/* High-level image sensor control functions */
456static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
457static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
458static int w9968cf_sensor_cmd(struct w9968cf_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300459 unsigned int cmd, void *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static int w9968cf_sensor_init(struct w9968cf_device*);
461static int w9968cf_sensor_update_settings(struct w9968cf_device*);
462static int w9968cf_sensor_get_picture(struct w9968cf_device*);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300463static int w9968cf_sensor_update_picture(struct w9968cf_device*,
464 struct video_picture pict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466/* Other helper functions */
467static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300468 enum w9968cf_model_id,
469 const unsigned short dev_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470static void w9968cf_adjust_configuration(struct w9968cf_device*);
471static int w9968cf_turn_on_led(struct w9968cf_device*);
472static int w9968cf_init_chip(struct w9968cf_device*);
473static inline u16 w9968cf_valid_palette(u16 palette);
474static inline u16 w9968cf_valid_depth(u16 palette);
475static inline u8 w9968cf_need_decompression(u16 palette);
476static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
477static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300478static int w9968cf_postprocess_frame(struct w9968cf_device*,
479 struct w9968cf_frame_t*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480static int w9968cf_adjust_window_size(struct w9968cf_device*, u16* w, u16* h);
481static void w9968cf_init_framelist(struct w9968cf_device*);
482static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
483static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
484static void w9968cf_release_resources(struct w9968cf_device*);
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487
488/****************************************************************************
489 * Symbolic names *
490 ****************************************************************************/
491
492/* Used to represent a list of values and their respective symbolic names */
493struct w9968cf_symbolic_list {
494 const int num;
495 const char *name;
496};
497
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300498/*--------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 Returns the name of the matching element in the symbolic_list array. The
500 end of the list must be marked with an element that has a NULL name.
501 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300502static inline const char *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503symbolic(struct w9968cf_symbolic_list list[], const int num)
504{
505 int i;
506
507 for (i = 0; list[i].name != NULL; i++)
508 if (list[i].num == num)
509 return (list[i].name);
510
511 return "Unknown";
512}
513
514static struct w9968cf_symbolic_list camlist[] = {
515 { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
516 { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
517
518 /* Other cameras (having the same descriptors as Generic W996[87]CF) */
519 { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
520 { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
521 { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
522 { W9968CF_MOD_LL, "Lebon LDC-035A" },
523 { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
524 { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
525 { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
526 { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
527 { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
528
529 { -1, NULL }
530};
531
532static struct w9968cf_symbolic_list senlist[] = {
533 { CC_OV76BE, "OV76BE" },
534 { CC_OV7610, "OV7610" },
535 { CC_OV7620, "OV7620" },
536 { CC_OV7620AE, "OV7620AE" },
537 { CC_OV6620, "OV6620" },
538 { CC_OV6630, "OV6630" },
539 { CC_OV6630AE, "OV6630AE" },
540 { CC_OV6630AF, "OV6630AF" },
541 { -1, NULL }
542};
543
544/* Video4Linux1 palettes */
545static struct w9968cf_symbolic_list v4l1_plist[] = {
546 { VIDEO_PALETTE_GREY, "GREY" },
547 { VIDEO_PALETTE_HI240, "HI240" },
548 { VIDEO_PALETTE_RGB565, "RGB565" },
549 { VIDEO_PALETTE_RGB24, "RGB24" },
550 { VIDEO_PALETTE_RGB32, "RGB32" },
551 { VIDEO_PALETTE_RGB555, "RGB555" },
552 { VIDEO_PALETTE_YUV422, "YUV422" },
553 { VIDEO_PALETTE_YUYV, "YUYV" },
554 { VIDEO_PALETTE_UYVY, "UYVY" },
555 { VIDEO_PALETTE_YUV420, "YUV420" },
556 { VIDEO_PALETTE_YUV411, "YUV411" },
557 { VIDEO_PALETTE_RAW, "RAW" },
558 { VIDEO_PALETTE_YUV422P, "YUV422P" },
559 { VIDEO_PALETTE_YUV411P, "YUV411P" },
560 { VIDEO_PALETTE_YUV420P, "YUV420P" },
561 { VIDEO_PALETTE_YUV410P, "YUV410P" },
562 { -1, NULL }
563};
564
565/* Decoder error codes: */
566static struct w9968cf_symbolic_list decoder_errlist[] = {
567 { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
568 { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300569 { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
571 { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
572 { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
573 { -1, NULL }
574};
575
576/* URB error codes: */
577static struct w9968cf_symbolic_list urb_errlist[] = {
578 { -ENOMEM, "No memory for allocation of internal structures" },
579 { -ENOSPC, "The host controller's bandwidth is already consumed" },
580 { -ENOENT, "URB was canceled by unlink_urb" },
581 { -EXDEV, "ISO transfer only partially completed" },
582 { -EAGAIN, "Too match scheduled for the future" },
583 { -ENXIO, "URB already queued" },
584 { -EFBIG, "Too much ISO frames requested" },
585 { -ENOSR, "Buffer error (overrun)" },
586 { -EPIPE, "Specified endpoint is stalled (device not responding)"},
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700587 { -EOVERFLOW, "Babble (too much data)" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 { -EPROTO, "Bit-stuff error (bad cable?)" },
589 { -EILSEQ, "CRC/Timeout" },
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700590 { -ETIME, "Device does not respond to token" },
591 { -ETIMEDOUT, "Device does not respond to command" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 { -1, NULL }
593};
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/****************************************************************************
596 * Memory management functions *
597 ****************************************************************************/
598static void* rvmalloc(unsigned long size)
599{
600 void* mem;
601 unsigned long adr;
602
603 size = PAGE_ALIGN(size);
604 mem = vmalloc_32(size);
605 if (!mem)
606 return NULL;
607
608 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
609 adr = (unsigned long) mem;
610 while (size > 0) {
611 SetPageReserved(vmalloc_to_page((void *)adr));
612 adr += PAGE_SIZE;
613 size -= PAGE_SIZE;
614 }
615
616 return mem;
617}
618
619
620static void rvfree(void* mem, unsigned long size)
621{
622 unsigned long adr;
623
624 if (!mem)
625 return;
626
627 adr = (unsigned long) mem;
628 while ((long) size > 0) {
629 ClearPageReserved(vmalloc_to_page((void *)adr));
630 adr += PAGE_SIZE;
631 size -= PAGE_SIZE;
632 }
633 vfree(mem);
634}
635
636
637/*--------------------------------------------------------------------------
638 Deallocate previously allocated memory.
639 --------------------------------------------------------------------------*/
640static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
641{
642 u8 i;
643
644 /* Free the isochronous transfer buffers */
645 for (i = 0; i < W9968CF_URBS; i++) {
646 kfree(cam->transfer_buffer[i]);
647 cam->transfer_buffer[i] = NULL;
648 }
649
650 /* Free temporary frame buffer */
651 if (cam->frame_tmp.buffer) {
652 rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
653 cam->frame_tmp.buffer = NULL;
654 }
655
656 /* Free helper buffer */
657 if (cam->frame_vpp.buffer) {
658 rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
659 cam->frame_vpp.buffer = NULL;
660 }
661
662 /* Free video frame buffers */
663 if (cam->frame[0].buffer) {
664 rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
665 cam->frame[0].buffer = NULL;
666 }
667
668 cam->nbuffers = 0;
669
670 DBG(5, "Memory successfully deallocated")
671}
672
673
674/*--------------------------------------------------------------------------
675 Allocate memory buffers for USB transfers and video frames.
676 This function is called by open() only.
677 Return 0 on success, a negative number otherwise.
678 --------------------------------------------------------------------------*/
679static int w9968cf_allocate_memory(struct w9968cf_device* cam)
680{
681 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
682 void* buff = NULL;
683 unsigned long hw_bufsize, vpp_bufsize;
684 u8 i, bpp;
685
686 /* NOTE: Deallocation is done elsewhere in case of error */
687
688 /* Calculate the max amount of raw data per frame from the device */
689 hw_bufsize = cam->maxwidth*cam->maxheight*2;
690
691 /* Calculate the max buf. size needed for post-processing routines */
692 bpp = (w9968cf_vpp) ? 4 : 2;
693 if (cam->upscaling)
694 vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300695 cam->maxwidth*cam->maxheight*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 else
697 vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
698
699 /* Allocate memory for the isochronous transfer buffers */
700 for (i = 0; i < W9968CF_URBS; i++) {
701 if (!(cam->transfer_buffer[i] =
Oliver Neukumb10b4172006-01-06 21:28:40 +0100702 kzalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 DBG(1, "Couldn't allocate memory for the isochronous "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300704 "transfer buffers (%u bytes)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 p_size * W9968CF_ISO_PACKETS)
706 return -ENOMEM;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 /* Allocate memory for the temporary frame buffer */
711 if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
712 DBG(1, "Couldn't allocate memory for the temporary "
713 "video frame buffer (%lu bytes)", hw_bufsize)
714 return -ENOMEM;
715 }
716 cam->frame_tmp.size = hw_bufsize;
717 cam->frame_tmp.number = -1;
718
719 /* Allocate memory for the helper buffer */
720 if (w9968cf_vpp) {
721 if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
722 DBG(1, "Couldn't allocate memory for the helper buffer"
723 " (%lu bytes)", vpp_bufsize)
724 return -ENOMEM;
725 }
726 cam->frame_vpp.size = vpp_bufsize;
727 } else
728 cam->frame_vpp.buffer = NULL;
729
730 /* Allocate memory for video frame buffers */
731 cam->nbuffers = cam->max_buffers;
732 while (cam->nbuffers >= 2) {
733 if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
734 break;
735 else
736 cam->nbuffers--;
737 }
738
739 if (!buff) {
740 DBG(1, "Couldn't allocate memory for the video frame buffers")
741 cam->nbuffers = 0;
742 return -ENOMEM;
743 }
744
745 if (cam->nbuffers != cam->max_buffers)
746 DBG(2, "Couldn't allocate memory for %u video frame buffers. "
747 "Only memory for %u buffers has been allocated",
748 cam->max_buffers, cam->nbuffers)
749
750 for (i = 0; i < cam->nbuffers; i++) {
751 cam->frame[i].buffer = buff + i*vpp_bufsize;
752 cam->frame[i].size = vpp_bufsize;
753 cam->frame[i].number = i;
754 /* Circular list */
755 if (i != cam->nbuffers-1)
756 cam->frame[i].next = &cam->frame[i+1];
757 else
758 cam->frame[i].next = &cam->frame[0];
759 cam->frame[i].status = F_UNUSED;
760 }
761
762 DBG(5, "Memory successfully allocated")
763 return 0;
764}
765
766
767
768/****************************************************************************
769 * USB-specific functions *
770 ****************************************************************************/
771
772/*--------------------------------------------------------------------------
773 This is an handler function which is called after the URBs are completed.
774 It collects multiple data packets coming from the camera by putting them
775 into frame buffers: one or more zero data length data packets are used to
776 mark the end of a video frame; the first non-zero data packet is the start
777 of the next video frame; if an error is encountered in a packet, the entire
778 video frame is discarded and grabbed again.
779 If there are no requested frames in the FIFO list, packets are collected into
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300780 a temporary buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 --------------------------------------------------------------------------*/
David Howells7d12e782006-10-05 14:55:46 +0100782static void w9968cf_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
785 struct w9968cf_frame_t** f;
786 unsigned int len, status;
787 void* pos;
788 u8 i;
789 int err = 0;
790
791 if ((!cam->streaming) || cam->disconnected) {
792 DBG(4, "Got interrupt, but not streaming")
793 return;
794 }
795
796 /* "(*f)" will be used instead of "cam->frame_current" */
797 f = &cam->frame_current;
798
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300799 /* If a frame has been requested and we are grabbing into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 the temporary frame, we'll switch to that requested frame */
801 if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
802 if (cam->frame_tmp.status == F_GRABBING) {
803 w9968cf_pop_frame(cam, &cam->frame_current);
804 (*f)->status = F_GRABBING;
805 (*f)->length = cam->frame_tmp.length;
806 memcpy((*f)->buffer, cam->frame_tmp.buffer,
807 (*f)->length);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300808 DBG(6, "Switched from temp. frame to frame #%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 (*f)->number)
810 }
811 }
812
813 for (i = 0; i < urb->number_of_packets; i++) {
814 len = urb->iso_frame_desc[i].actual_length;
815 status = urb->iso_frame_desc[i].status;
816 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
817
818 if (status && len != 0) {
819 DBG(4, "URB failed, error in data packet "
820 "(error #%u, %s)",
821 status, symbolic(urb_errlist, status))
822 (*f)->status = F_ERROR;
823 continue;
824 }
825
826 if (len) { /* start of frame */
827
828 if ((*f)->status == F_UNUSED) {
829 (*f)->status = F_GRABBING;
830 (*f)->length = 0;
831 }
832
833 /* Buffer overflows shouldn't happen, however...*/
834 if ((*f)->length + len > (*f)->size) {
835 DBG(4, "Buffer overflow: bad data packets")
836 (*f)->status = F_ERROR;
837 }
838
839 if ((*f)->status == F_GRABBING) {
840 memcpy((*f)->buffer + (*f)->length, pos, len);
841 (*f)->length += len;
842 }
843
844 } else if ((*f)->status == F_GRABBING) { /* end of frame */
845
846 DBG(6, "Frame #%d successfully grabbed", (*f)->number)
847
848 if (cam->vpp_flag & VPP_DECOMPRESSION) {
849 err = w9968cf_vpp->check_headers((*f)->buffer,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300850 (*f)->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (err) {
852 DBG(4, "Skip corrupted frame: %s",
853 symbolic(decoder_errlist, err))
854 (*f)->status = F_UNUSED;
855 continue; /* grab this frame again */
856 }
857 }
858
859 (*f)->status = F_READY;
860 (*f)->queued = 0;
861
862 /* Take a pointer to the new frame from the FIFO list.
863 If the list is empty,we'll use the temporary frame*/
864 if (*cam->requested_frame)
865 w9968cf_pop_frame(cam, &cam->frame_current);
866 else {
867 cam->frame_current = &cam->frame_tmp;
868 (*f)->status = F_UNUSED;
869 }
870
871 } else if ((*f)->status == F_ERROR)
872 (*f)->status = F_UNUSED; /* grab it again */
873
874 PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
875 (unsigned long)(*f)->length, i, len, (*f)->status)
876
877 } /* end for */
878
879 /* Resubmit this URB */
880 urb->dev = cam->usbdev;
881 urb->status = 0;
882 spin_lock(&cam->urb_lock);
883 if (cam->streaming)
884 if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
885 cam->misconfigured = 1;
886 DBG(1, "Couldn't resubmit the URB: error %d, %s",
887 err, symbolic(urb_errlist, err))
888 }
889 spin_unlock(&cam->urb_lock);
890
891 /* Wake up the user process */
892 wake_up_interruptible(&cam->wait_queue);
893}
894
895
896/*---------------------------------------------------------------------------
897 Setup the URB structures for the isochronous transfer.
898 Submit the URBs so that the data transfer begins.
899 Return 0 on success, a negative number otherwise.
900 ---------------------------------------------------------------------------*/
901static int w9968cf_start_transfer(struct w9968cf_device* cam)
902{
903 struct usb_device *udev = cam->usbdev;
904 struct urb* urb;
905 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
906 u16 w, h, d;
907 int vidcapt;
908 u32 t_size;
909 int err = 0;
910 s8 i, j;
911
912 for (i = 0; i < W9968CF_URBS; i++) {
913 urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (!urb) {
915 for (j = 0; j < i; j++)
916 usb_free_urb(cam->urb[j]);
917 DBG(1, "Couldn't allocate the URB structures")
918 return -ENOMEM;
919 }
920
Douglas Schilling Landgrafff41efc2008-09-04 11:19:27 -0300921 cam->urb[i] = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 urb->dev = udev;
923 urb->context = (void*)cam;
924 urb->pipe = usb_rcvisocpipe(udev, 1);
925 urb->transfer_flags = URB_ISO_ASAP;
926 urb->number_of_packets = W9968CF_ISO_PACKETS;
927 urb->complete = w9968cf_urb_complete;
928 urb->transfer_buffer = cam->transfer_buffer[i];
929 urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
930 urb->interval = 1;
931 for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
932 urb->iso_frame_desc[j].offset = p_size*j;
933 urb->iso_frame_desc[j].length = p_size;
934 }
935 }
936
937 /* Transfer size per frame, in WORD ! */
938 d = cam->hw_depth;
939 w = cam->hw_width;
940 h = cam->hw_height;
941
942 t_size = (w*h*d)/16;
943
944 err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
945 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
946
947 /* Transfer size */
948 err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
949 err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
950
951 if (cam->vpp_flag & VPP_DECOMPRESSION)
952 err += w9968cf_upload_quantizationtables(cam);
953
954 vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
955 err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
956
957 err += usb_set_interface(udev, 0, cam->altsetting);
958 err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
959
960 if (err || (vidcapt < 0)) {
961 for (i = 0; i < W9968CF_URBS; i++)
962 usb_free_urb(cam->urb[i]);
963 DBG(1, "Couldn't tell the camera to start the data transfer")
964 return err;
965 }
966
967 w9968cf_init_framelist(cam);
968
969 /* Begin to grab into the temporary buffer */
970 cam->frame_tmp.status = F_UNUSED;
971 cam->frame_tmp.queued = 0;
972 cam->frame_current = &cam->frame_tmp;
973
974 if (!(cam->vpp_flag & VPP_DECOMPRESSION))
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300975 DBG(5, "Isochronous transfer size: %lu bytes/frame",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 (unsigned long)t_size*2)
977
978 DBG(5, "Starting the isochronous transfer...")
979
980 cam->streaming = 1;
981
982 /* Submit the URBs */
983 for (i = 0; i < W9968CF_URBS; i++) {
984 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
985 if (err) {
986 cam->streaming = 0;
987 for (j = i-1; j >= 0; j--) {
988 usb_kill_urb(cam->urb[j]);
989 usb_free_urb(cam->urb[j]);
990 }
991 DBG(1, "Couldn't send a transfer request to the "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300992 "USB core (error #%d, %s)", err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 symbolic(urb_errlist, err))
994 return err;
995 }
996 }
997
998 return 0;
999}
1000
1001
1002/*--------------------------------------------------------------------------
1003 Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
1004 Return 0 on success, a negative number otherwise.
1005 --------------------------------------------------------------------------*/
1006static int w9968cf_stop_transfer(struct w9968cf_device* cam)
1007{
1008 struct usb_device *udev = cam->usbdev;
1009 unsigned long lock_flags;
1010 int err = 0;
1011 s8 i;
1012
1013 if (!cam->streaming)
1014 return 0;
1015
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001016 /* This avoids race conditions with usb_submit_urb()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 in the URB completition handler */
1018 spin_lock_irqsave(&cam->urb_lock, lock_flags);
1019 cam->streaming = 0;
1020 spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
1021
1022 for (i = W9968CF_URBS-1; i >= 0; i--)
1023 if (cam->urb[i]) {
1024 usb_kill_urb(cam->urb[i]);
1025 usb_free_urb(cam->urb[i]);
1026 cam->urb[i] = NULL;
1027 }
1028
1029 if (cam->disconnected)
1030 goto exit;
1031
1032 err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
1033 err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
1034 err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
1035 err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
1036
1037 if (err) {
1038 DBG(2, "Failed to tell the camera to stop the isochronous "
1039 "transfer. However this is not a critical error.")
1040 return -EIO;
1041 }
1042
1043exit:
1044 DBG(5, "Isochronous transfer stopped")
1045 return 0;
1046}
1047
1048
1049/*--------------------------------------------------------------------------
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001050 Write a W9968CF register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 Return 0 on success, -1 otherwise.
1052 --------------------------------------------------------------------------*/
1053static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
1054{
1055 struct usb_device* udev = cam->usbdev;
1056 int res;
1057
1058 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001059 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1060 value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (res < 0)
1063 DBG(4, "Failed to write a register "
1064 "(value 0x%04X, index 0x%02X, error #%d, %s)",
1065 value, index, res, symbolic(urb_errlist, res))
1066
1067 return (res >= 0) ? 0 : -1;
1068}
1069
1070
1071/*--------------------------------------------------------------------------
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001072 Read a W9968CF register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 Return the register value on success, -1 otherwise.
1074 --------------------------------------------------------------------------*/
1075static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
1076{
1077 struct usb_device* udev = cam->usbdev;
1078 u16* buff = cam->control_buffer;
1079 int res;
1080
1081 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001082 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1083 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (res < 0)
1086 DBG(4, "Failed to read a register "
1087 "(index 0x%02X, error #%d, %s)",
1088 index, res, symbolic(urb_errlist, res))
1089
1090 return (res >= 0) ? (int)(*buff) : -1;
1091}
1092
1093
1094/*--------------------------------------------------------------------------
1095 Write 64-bit data to the fast serial bus registers.
1096 Return 0 on success, -1 otherwise.
1097 --------------------------------------------------------------------------*/
1098static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
1099{
1100 struct usb_device* udev = cam->usbdev;
1101 u16 value;
1102 int res;
1103
1104 value = *data++;
1105
1106 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001107 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1108 value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (res < 0)
1111 DBG(4, "Failed to write the FSB registers "
1112 "(error #%d, %s)", res, symbolic(urb_errlist, res))
1113
1114 return (res >= 0) ? 0 : -1;
1115}
1116
1117
1118/*--------------------------------------------------------------------------
1119 Write data to the serial bus control register.
1120 Return 0 on success, a negative number otherwise.
1121 --------------------------------------------------------------------------*/
1122static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
1123{
1124 int err = 0;
1125
1126 err = w9968cf_write_reg(cam, value, 0x01);
1127 udelay(W9968CF_I2C_BUS_DELAY);
1128
1129 return err;
1130}
1131
1132
1133/*--------------------------------------------------------------------------
1134 Read data from the serial bus control register.
1135 Return 0 on success, a negative number otherwise.
1136 --------------------------------------------------------------------------*/
1137static int w9968cf_read_sb(struct w9968cf_device* cam)
1138{
1139 int v = 0;
1140
1141 v = w9968cf_read_reg(cam, 0x01);
1142 udelay(W9968CF_I2C_BUS_DELAY);
1143
1144 return v;
1145}
1146
1147
1148/*--------------------------------------------------------------------------
1149 Upload quantization tables for the JPEG compression.
1150 This function is called by w9968cf_start_transfer().
1151 Return 0 on success, a negative number otherwise.
1152 --------------------------------------------------------------------------*/
1153static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
1154{
1155 u16 a, b;
1156 int err = 0, i, j;
1157
1158 err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
1159
1160 for (i = 0, j = 0; i < 32; i++, j += 2) {
1161 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
1162 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
1163 err += w9968cf_write_reg(cam, a, 0x40+i);
1164 err += w9968cf_write_reg(cam, b, 0x60+i);
1165 }
1166 err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
1167
1168 return err;
1169}
1170
1171
1172
1173/****************************************************************************
1174 * Low-level I2C I/O functions. *
1175 * The adapter supports the following I2C transfer functions: *
1176 * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
1177 * i2c_adap_read_byte_data() *
1178 * i2c_adap_read_byte() *
1179 ****************************************************************************/
1180
1181static int w9968cf_smbus_start(struct w9968cf_device* cam)
1182{
1183 int err = 0;
1184
1185 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1186 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1187
1188 return err;
1189}
1190
1191
1192static int w9968cf_smbus_stop(struct w9968cf_device* cam)
1193{
1194 int err = 0;
1195
1196 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1197 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1198
1199 return err;
1200}
1201
1202
1203static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
1204{
1205 u8 bit;
1206 int err = 0, sda;
1207
1208 for (bit = 0 ; bit < 8 ; bit++) {
1209 sda = (v & 0x80) ? 2 : 0;
1210 v <<= 1;
1211 /* SDE=1, SDA=sda, SCL=0 */
1212 err += w9968cf_write_sb(cam, 0x10 | sda);
1213 /* SDE=1, SDA=sda, SCL=1 */
1214 err += w9968cf_write_sb(cam, 0x11 | sda);
1215 /* SDE=1, SDA=sda, SCL=0 */
1216 err += w9968cf_write_sb(cam, 0x10 | sda);
1217 }
1218
1219 return err;
1220}
1221
1222
1223static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
1224{
1225 u8 bit;
1226 int err = 0;
1227
1228 *v = 0;
1229 for (bit = 0 ; bit < 8 ; bit++) {
1230 *v <<= 1;
1231 err += w9968cf_write_sb(cam, 0x0013);
1232 *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
1233 err += w9968cf_write_sb(cam, 0x0012);
1234 }
1235
1236 return err;
1237}
1238
1239
1240static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
1241{
1242 int err = 0;
1243
1244 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1245 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1246 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1247
1248 return err;
1249}
1250
1251
1252static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
1253{
1254 int err = 0, sda;
1255
1256 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1257 sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
1258 err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
1259 if (sda < 0)
1260 err += sda;
1261 if (sda == 1) {
1262 DBG(6, "Couldn't receive the ACK")
1263 err += -1;
1264 }
1265
1266 return err;
1267}
1268
1269
1270/* This seems to refresh the communication through the serial bus */
1271static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
1272{
1273 int err = 0, j;
1274
1275 for (j = 1; j <= 10; j++) {
1276 err = w9968cf_write_reg(cam, 0x0020, 0x01);
1277 err += w9968cf_write_reg(cam, 0x0000, 0x01);
1278 if (err)
1279 break;
1280 }
1281
1282 return err;
1283}
1284
1285
1286/* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001287static int
1288w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
1289 u16 address, u8 subaddress,u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 u16* data = cam->data_buffer;
1292 int err = 0;
1293
1294 err += w9968cf_smbus_refresh_bus(cam);
1295
1296 /* Enable SBUS outputs */
1297 err += w9968cf_write_sb(cam, 0x0020);
1298
1299 data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
1300 data[0] |= (address & 0x40) ? 0x4000 : 0x0;
1301 data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
1302 data[1] |= (address & 0x20) ? 0x0150 : 0x0;
1303 data[1] |= (address & 0x10) ? 0x5400 : 0x0;
1304 data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
1305 data[2] |= (address & 0x04) ? 0x0540 : 0x0;
1306 data[2] |= (address & 0x02) ? 0x5000 : 0x0;
1307 data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
1308 data[3] |= (address & 0x01) ? 0x0054 : 0x0;
1309
1310 err += w9968cf_write_fsb(cam, data);
1311
1312 data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
1313 data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
1314 data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
1315 data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
1316 data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
1317 data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
1318 data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
1319 data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
1320 data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
1321 data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
1322 data[3] = 0x001d;
1323
1324 err += w9968cf_write_fsb(cam, data);
1325
1326 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
1327 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
1328 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
1329 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
1330 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
1331 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
1332 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
1333 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
1334 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
1335 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
1336 data[3] = 0xfe1d;
1337
1338 err += w9968cf_write_fsb(cam, data);
1339
1340 /* Disable SBUS outputs */
1341 err += w9968cf_write_sb(cam, 0x0000);
1342
1343 if (!err)
1344 DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
1345 "value 0x%02X", address, subaddress, value)
1346 else
1347 DBG(5, "I2C write byte data failed, addr.0x%04X, "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001348 "subaddr.0x%02X, value 0x%02X",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 address, subaddress, value)
1350
1351 return err;
1352}
1353
1354
1355/* 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 -03001356static int
1357w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
1358 u16 address, u8 subaddress,
1359 u8* value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 int err = 0;
1362
1363 /* Serial data enable */
1364 err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
1365
1366 err += w9968cf_smbus_start(cam);
1367 err += w9968cf_smbus_write_byte(cam, address);
1368 err += w9968cf_smbus_read_ack(cam);
1369 err += w9968cf_smbus_write_byte(cam, subaddress);
1370 err += w9968cf_smbus_read_ack(cam);
1371 err += w9968cf_smbus_stop(cam);
1372 err += w9968cf_smbus_start(cam);
1373 err += w9968cf_smbus_write_byte(cam, address + 1);
1374 err += w9968cf_smbus_read_ack(cam);
1375 err += w9968cf_smbus_read_byte(cam, value);
1376 err += w9968cf_smbus_write_ack(cam);
1377 err += w9968cf_smbus_stop(cam);
1378
1379 /* Serial data disable */
1380 err += w9968cf_write_sb(cam, 0x0000);
1381
1382 if (!err)
1383 DBG(5, "I2C read byte data done, addr.0x%04X, "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001384 "subaddr.0x%02X, value 0x%02X",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 address, subaddress, *value)
1386 else
1387 DBG(5, "I2C read byte data failed, addr.0x%04X, "
1388 "subaddr.0x%02X, wrong value 0x%02X",
1389 address, subaddress, *value)
1390
1391 return err;
1392}
1393
1394
1395/* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001396static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001398 u16 address, u8* value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 int err = 0;
1401
1402 /* Serial data enable */
1403 err += w9968cf_write_sb(cam, 0x0013);
1404
1405 err += w9968cf_smbus_start(cam);
1406 err += w9968cf_smbus_write_byte(cam, address + 1);
1407 err += w9968cf_smbus_read_ack(cam);
1408 err += w9968cf_smbus_read_byte(cam, value);
1409 err += w9968cf_smbus_write_ack(cam);
1410 err += w9968cf_smbus_stop(cam);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /* Serial data disable */
1413 err += w9968cf_write_sb(cam, 0x0000);
1414
1415 if (!err)
1416 DBG(5, "I2C read byte done, addr.0x%04X, "
1417 "value 0x%02X", address, *value)
1418 else
1419 DBG(5, "I2C read byte failed, addr.0x%04X, "
1420 "wrong value 0x%02X", address, *value)
1421
1422 return err;
1423}
1424
1425
1426/* SMBus protocol: S Addr Wr [A] Value [A] P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001427static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001429 u16 address, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431 DBG(4, "i2c_write_byte() is an unsupported transfer mode")
1432 return -EINVAL;
1433}
1434
1435
1436
1437/****************************************************************************
1438 * I2C interface to kernel *
1439 ****************************************************************************/
1440
1441static int
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001442w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1443 unsigned short flags, char read_write, u8 command,
1444 int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct w9968cf_device* cam = i2c_get_adapdata(adapter);
1447 u8 i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001448 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 switch (addr) {
1451 case OV6xx0_SID:
1452 case OV7xx0_SID:
1453 break;
1454 default:
1455 DBG(4, "Rejected slave ID 0x%04X", addr)
1456 return -EINVAL;
1457 }
1458
1459 if (size == I2C_SMBUS_BYTE) {
1460 /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
1461 addr <<= 1;
1462
1463 if (read_write == I2C_SMBUS_WRITE)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001464 err = w9968cf_i2c_adap_write_byte(cam, addr, command);
1465 else if (read_write == I2C_SMBUS_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 err = w9968cf_i2c_adap_read_byte(cam,addr,&data->byte);
1467
1468 } else if (size == I2C_SMBUS_BYTE_DATA) {
1469 addr <<= 1;
1470
1471 if (read_write == I2C_SMBUS_WRITE)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001472 err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
1473 command, data->byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 else if (read_write == I2C_SMBUS_READ) {
1475 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1476 err = w9968cf_i2c_adap_read_byte_data(cam,addr,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001477 command, &data->byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (err) {
1479 if (w9968cf_smbus_refresh_bus(cam)) {
1480 err = -EIO;
1481 break;
1482 }
1483 } else
1484 break;
1485 }
1486
1487 } else
1488 return -EINVAL;
1489
1490 } else {
1491 DBG(4, "Unsupported I2C transfer mode (%d)", size)
1492 return -EINVAL;
1493 }
1494
1495 return err;
1496}
1497
1498
1499static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
1500{
1501 return I2C_FUNC_SMBUS_READ_BYTE |
1502 I2C_FUNC_SMBUS_READ_BYTE_DATA |
1503 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
1504}
1505
1506
1507static int w9968cf_i2c_attach_inform(struct i2c_client* client)
1508{
1509 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int id = client->driver->id, err = 0;
1511
1512 if (id == I2C_DRIVERID_OVCAMCHIP) {
1513 cam->sensor_client = client;
1514 err = w9968cf_sensor_init(cam);
1515 if (err) {
1516 cam->sensor_client = NULL;
1517 return err;
1518 }
1519 } else {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001520 DBG(4, "Rejected client [%s] with driver [%s]",
Laurent Riffard604f28e2005-11-26 20:43:39 +01001521 client->name, client->driver->driver.name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return -EINVAL;
1523 }
1524
1525 DBG(5, "I2C attach client [%s] with driver [%s]",
Laurent Riffard604f28e2005-11-26 20:43:39 +01001526 client->name, client->driver->driver.name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 return 0;
1529}
1530
1531
1532static int w9968cf_i2c_detach_inform(struct i2c_client* client)
1533{
1534 struct w9968cf_device* cam = i2c_get_adapdata(client->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (cam->sensor_client == client)
1537 cam->sensor_client = NULL;
1538
Jean Delvarefae91e72005-08-15 19:57:04 +02001539 DBG(5, "I2C detach client [%s]", client->name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 return 0;
1542}
1543
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545static int w9968cf_i2c_init(struct w9968cf_device* cam)
1546{
1547 int err = 0;
1548
1549 static struct i2c_algorithm algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 .smbus_xfer = w9968cf_i2c_smbus_xfer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 .functionality = w9968cf_i2c_func,
1552 };
1553
1554 static struct i2c_adapter adap = {
Jean Delvarec7a46532005-08-11 23:41:56 +02001555 .id = I2C_HW_SMBUS_W9968CF,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 .owner = THIS_MODULE,
1557 .client_register = w9968cf_i2c_attach_inform,
1558 .client_unregister = w9968cf_i2c_detach_inform,
1559 .algo = &algo,
1560 };
1561
1562 memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
1563 strcpy(cam->i2c_adapter.name, "w9968cf");
Jean Delvare12a917f2007-02-13 22:09:03 +01001564 cam->i2c_adapter.dev.parent = &cam->usbdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 i2c_set_adapdata(&cam->i2c_adapter, cam);
1566
1567 DBG(6, "Registering I2C adapter with kernel...")
1568
1569 err = i2c_add_adapter(&cam->i2c_adapter);
1570 if (err)
1571 DBG(1, "Failed to register the I2C adapter")
1572 else
1573 DBG(5, "I2C adapter registered")
1574
1575 return err;
1576}
1577
1578
1579
1580/****************************************************************************
1581 * Helper functions *
1582 ****************************************************************************/
1583
1584/*--------------------------------------------------------------------------
1585 Turn on the LED on some webcams. A beep should be heard too.
1586 Return 0 on success, a negative number otherwise.
1587 --------------------------------------------------------------------------*/
1588static int w9968cf_turn_on_led(struct w9968cf_device* cam)
1589{
1590 int err = 0;
1591
1592 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power-down */
1593 err += w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
1594 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
1595 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* serial bus, SDS high */
1596 err += w9968cf_write_reg(cam, 0x0000, 0x01); /* serial bus, SDS low */
1597 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* ..high 'beep-beep' */
1598
1599 if (err)
1600 DBG(2, "Couldn't turn on the LED")
1601
1602 DBG(5, "LED turned on")
1603
1604 return err;
1605}
1606
1607
1608/*--------------------------------------------------------------------------
1609 Write some registers for the device initialization.
1610 This function is called once on open().
1611 Return 0 on success, a negative number otherwise.
1612 --------------------------------------------------------------------------*/
1613static int w9968cf_init_chip(struct w9968cf_device* cam)
1614{
1615 unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001616 y0 = 0x0000,
1617 u0 = y0 + hw_bufsize/2,
1618 v0 = u0 + hw_bufsize/4,
1619 y1 = v0 + hw_bufsize/4,
1620 u1 = y1 + hw_bufsize/2,
1621 v1 = u1 + hw_bufsize/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 int err = 0;
1623
1624 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
1625 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
1626
1627 err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
1628 err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
1629
1630 err += w9968cf_write_reg(cam, y0 & 0xffff, 0x20); /* Y buf.0, low */
1631 err += w9968cf_write_reg(cam, y0 >> 16, 0x21); /* Y buf.0, high */
1632 err += w9968cf_write_reg(cam, u0 & 0xffff, 0x24); /* U buf.0, low */
1633 err += w9968cf_write_reg(cam, u0 >> 16, 0x25); /* U buf.0, high */
1634 err += w9968cf_write_reg(cam, v0 & 0xffff, 0x28); /* V buf.0, low */
1635 err += w9968cf_write_reg(cam, v0 >> 16, 0x29); /* V buf.0, high */
1636
1637 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x22); /* Y buf.1, low */
1638 err += w9968cf_write_reg(cam, y1 >> 16, 0x23); /* Y buf.1, high */
1639 err += w9968cf_write_reg(cam, u1 & 0xffff, 0x26); /* U buf.1, low */
1640 err += w9968cf_write_reg(cam, u1 >> 16, 0x27); /* U buf.1, high */
1641 err += w9968cf_write_reg(cam, v1 & 0xffff, 0x2a); /* V buf.1, low */
1642 err += w9968cf_write_reg(cam, v1 >> 16, 0x2b); /* V buf.1, high */
1643
1644 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x32); /* JPEG buf 0 low */
1645 err += w9968cf_write_reg(cam, y1 >> 16, 0x33); /* JPEG buf 0 high */
1646
1647 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x34); /* JPEG buf 1 low */
1648 err += w9968cf_write_reg(cam, y1 >> 16, 0x35); /* JPEG bug 1 high */
1649
1650 err += w9968cf_write_reg(cam, 0x0000, 0x36);/* JPEG restart interval */
1651 err += w9968cf_write_reg(cam, 0x0804, 0x37);/*JPEG VLE FIFO threshold*/
1652 err += w9968cf_write_reg(cam, 0x0000, 0x38);/* disable hw up-scaling */
1653 err += w9968cf_write_reg(cam, 0x0000, 0x3f); /* JPEG/MCTL test data */
1654
1655 err += w9968cf_set_picture(cam, cam->picture); /* this before */
1656 err += w9968cf_set_window(cam, cam->window);
1657
1658 if (err)
1659 DBG(1, "Chip initialization failed")
1660 else
1661 DBG(5, "Chip successfully initialized")
1662
1663 return err;
1664}
1665
1666
1667/*--------------------------------------------------------------------------
1668 Return non-zero if the palette is supported, 0 otherwise.
1669 --------------------------------------------------------------------------*/
1670static inline u16 w9968cf_valid_palette(u16 palette)
1671{
1672 u8 i = 0;
1673 while (w9968cf_formatlist[i].palette != 0) {
1674 if (palette == w9968cf_formatlist[i].palette)
1675 return palette;
1676 i++;
1677 }
1678 return 0;
1679}
1680
1681
1682/*--------------------------------------------------------------------------
1683 Return the depth corresponding to the given palette.
1684 Palette _must_ be supported !
1685 --------------------------------------------------------------------------*/
1686static inline u16 w9968cf_valid_depth(u16 palette)
1687{
1688 u8 i=0;
1689 while (w9968cf_formatlist[i].palette != palette)
1690 i++;
1691
1692 return w9968cf_formatlist[i].depth;
1693}
1694
1695
1696/*--------------------------------------------------------------------------
1697 Return non-zero if the format requires decompression, 0 otherwise.
1698 --------------------------------------------------------------------------*/
1699static inline u8 w9968cf_need_decompression(u16 palette)
1700{
1701 u8 i = 0;
1702 while (w9968cf_formatlist[i].palette != 0) {
1703 if (palette == w9968cf_formatlist[i].palette)
1704 return w9968cf_formatlist[i].compression;
1705 i++;
1706 }
1707 return 0;
1708}
1709
1710
1711/*--------------------------------------------------------------------------
1712 Change the picture settings of the camera.
1713 Return 0 on success, a negative number otherwise.
1714 --------------------------------------------------------------------------*/
1715static int
1716w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
1717{
1718 u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
1719 int err = 0;
1720
1721 /* Make sure we are using a valid depth */
1722 pict.depth = w9968cf_valid_depth(pict.palette);
1723
1724 fmt = pict.palette;
1725
1726 hw_depth = pict.depth; /* depth used by the winbond chip */
1727 hw_palette = pict.palette; /* palette used by the winbond chip */
1728
1729 /* VS & HS polarities */
1730 reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
1731
1732 switch (fmt)
1733 {
1734 case VIDEO_PALETTE_UYVY:
1735 reg_v |= 0x0000;
1736 cam->vpp_flag = VPP_NONE;
1737 break;
1738 case VIDEO_PALETTE_YUV422P:
1739 reg_v |= 0x0002;
1740 cam->vpp_flag = VPP_DECOMPRESSION;
1741 break;
1742 case VIDEO_PALETTE_YUV420:
1743 case VIDEO_PALETTE_YUV420P:
1744 reg_v |= 0x0003;
1745 cam->vpp_flag = VPP_DECOMPRESSION;
1746 break;
1747 case VIDEO_PALETTE_YUYV:
1748 case VIDEO_PALETTE_YUV422:
1749 reg_v |= 0x0000;
1750 cam->vpp_flag = VPP_SWAP_YUV_BYTES;
1751 hw_palette = VIDEO_PALETTE_UYVY;
1752 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001753 /* Original video is used instead of RGBX palettes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 Software conversion later. */
1755 case VIDEO_PALETTE_GREY:
1756 case VIDEO_PALETTE_RGB555:
1757 case VIDEO_PALETTE_RGB565:
1758 case VIDEO_PALETTE_RGB24:
1759 case VIDEO_PALETTE_RGB32:
1760 reg_v |= 0x0000; /* UYVY 16 bit is used */
1761 hw_depth = 16;
1762 hw_palette = VIDEO_PALETTE_UYVY;
1763 cam->vpp_flag = VPP_UYVY_TO_RGBX;
1764 break;
1765 }
1766
1767 /* NOTE: due to memory issues, it is better to disable the hardware
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001768 double buffering during compression */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
1770 reg_v |= 0x0080;
1771
1772 if (cam->clamping)
1773 reg_v |= 0x0020;
1774
1775 if (cam->filter_type == 1)
1776 reg_v |= 0x0008;
1777 else if (cam->filter_type == 2)
1778 reg_v |= 0x000c;
1779
1780 if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
1781 goto error;
1782
1783 if ((err = w9968cf_sensor_update_picture(cam, pict)))
1784 goto error;
1785
1786 /* If all went well, update the device data structure */
1787 memcpy(&cam->picture, &pict, sizeof(pict));
1788 cam->hw_depth = hw_depth;
1789 cam->hw_palette = hw_palette;
1790
1791 /* Settings changed, so we clear the frame buffers */
1792 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1793
1794 DBG(4, "Palette is %s, depth is %u bpp",
1795 symbolic(v4l1_plist, pict.palette), pict.depth)
1796
1797 return 0;
1798
1799error:
1800 DBG(1, "Failed to change picture settings")
1801 return err;
1802}
1803
1804
1805/*--------------------------------------------------------------------------
1806 Change the capture area size of the camera.
1807 This function _must_ be called _after_ w9968cf_set_picture().
1808 Return 0 on success, a negative number otherwise.
1809 --------------------------------------------------------------------------*/
1810static int
1811w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
1812{
1813 u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
1814 unsigned long fw, fh;
1815 struct ovcamchip_window s_win;
1816 int err = 0;
1817
1818 /* Work around to avoid FP arithmetics */
Ralf Baechlebee8a442006-12-11 11:14:54 -03001819 #define SC(x) ((x) << 10)
1820 #define UNSC(x) ((x) >> 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 /* Make sure we are using a supported resolution */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001823 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
1824 (u16*)&win.height)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 goto error;
1826
1827 /* Scaling factors */
Ralf Baechlebee8a442006-12-11 11:14:54 -03001828 fw = SC(win.width) / cam->maxwidth;
1829 fh = SC(win.height) / cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 /* Set up the width and height values used by the chip */
1832 if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
1833 cam->vpp_flag |= VPP_UPSCALE;
1834 /* Calculate largest w,h mantaining the same w/h ratio */
Ralf Baechlebee8a442006-12-11 11:14:54 -03001835 w = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1836 h = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (w < cam->minwidth) /* just in case */
1838 w = cam->minwidth;
1839 if (h < cam->minheight) /* just in case */
1840 h = cam->minheight;
1841 } else {
1842 cam->vpp_flag &= ~VPP_UPSCALE;
1843 w = win.width;
1844 h = win.height;
1845 }
1846
1847 /* x,y offsets of the cropped area */
1848 scx = cam->start_cropx;
1849 scy = cam->start_cropy;
1850
1851 /* Calculate cropped area manteining the right w/h ratio */
1852 if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
Ralf Baechlebee8a442006-12-11 11:14:54 -03001853 cw = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1854 ch = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 } else {
1856 cw = w;
1857 ch = h;
1858 }
1859
1860 /* Setup the window of the sensor */
1861 s_win.format = VIDEO_PALETTE_UYVY;
1862 s_win.width = cam->maxwidth;
1863 s_win.height = cam->maxheight;
1864 s_win.quarter = 0; /* full progressive video */
1865
1866 /* Center it */
1867 s_win.x = (s_win.width - cw) / 2;
1868 s_win.y = (s_win.height - ch) / 2;
1869
1870 /* Clock divisor */
1871 if (cam->clockdiv >= 0)
1872 s_win.clockdiv = cam->clockdiv; /* manual override */
1873 else
1874 switch (cam->sensor) {
1875 case CC_OV6620:
1876 s_win.clockdiv = 0;
1877 break;
1878 case CC_OV6630:
1879 s_win.clockdiv = 0;
1880 break;
1881 case CC_OV76BE:
1882 case CC_OV7610:
1883 case CC_OV7620:
1884 s_win.clockdiv = 0;
1885 break;
1886 default:
1887 s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
1888 }
1889
1890 /* We have to scale win.x and win.y offsets */
1891 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1892 || (cam->vpp_flag & VPP_UPSCALE) ) {
Ralf Baechlebee8a442006-12-11 11:14:54 -03001893 ax = SC(win.x)/fw;
1894 ay = SC(win.y)/fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 } else {
1896 ax = win.x;
1897 ay = win.y;
1898 }
1899
1900 if ((ax + cw) > cam->maxwidth)
1901 ax = cam->maxwidth - cw;
1902
1903 if ((ay + ch) > cam->maxheight)
1904 ay = cam->maxheight - ch;
1905
1906 /* Adjust win.x, win.y */
1907 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1908 || (cam->vpp_flag & VPP_UPSCALE) ) {
Ralf Baechlebee8a442006-12-11 11:14:54 -03001909 win.x = UNSC(ax*fw);
1910 win.y = UNSC(ay*fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 } else {
1912 win.x = ax;
1913 win.y = ay;
1914 }
1915
1916 /* Offsets used by the chip */
1917 x = ax + s_win.x;
1918 y = ay + s_win.y;
1919
1920 /* Go ! */
1921 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
1922 goto error;
1923
1924 err += w9968cf_write_reg(cam, scx + x, 0x10);
1925 err += w9968cf_write_reg(cam, scy + y, 0x11);
1926 err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
1927 err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
1928 err += w9968cf_write_reg(cam, w, 0x14);
1929 err += w9968cf_write_reg(cam, h, 0x15);
1930
1931 /* JPEG width & height */
1932 err += w9968cf_write_reg(cam, w, 0x30);
1933 err += w9968cf_write_reg(cam, h, 0x31);
1934
1935 /* Y & UV frame buffer strides (in WORD) */
1936 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1937 err += w9968cf_write_reg(cam, w/2, 0x2c);
1938 err += w9968cf_write_reg(cam, w/4, 0x2d);
1939 } else
1940 err += w9968cf_write_reg(cam, w, 0x2c);
1941
1942 if (err)
1943 goto error;
1944
1945 /* If all went well, update the device data structure */
1946 memcpy(&cam->window, &win, sizeof(win));
1947 cam->hw_width = w;
1948 cam->hw_height = h;
1949
1950 /* Settings changed, so we clear the frame buffers */
1951 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1952
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001953 DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 win.width, win.height, win.x, win.y)
1955
1956 PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
1957 "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
1958 x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
1959 win.width, win.height)
1960
1961 return 0;
1962
1963error:
1964 DBG(1, "Failed to change the capture area size")
1965 return err;
1966}
1967
1968
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001969/*--------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 Adjust the asked values for window width and height.
1971 Return 0 on success, -1 otherwise.
1972 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001973static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974w9968cf_adjust_window_size(struct w9968cf_device* cam, u16* width, u16* height)
1975{
1976 u16 maxw, maxh;
1977
1978 if ((*width < cam->minwidth) || (*height < cam->minheight))
1979 return -ERANGE;
1980
1981 maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1982 w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001983 : cam->maxwidth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1985 w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001986 : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 if (*width > maxw)
1989 *width = maxw;
1990 if (*height > maxh)
1991 *height = maxh;
1992
1993 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1994 *width &= ~15L; /* multiple of 16 */
1995 *height &= ~15L;
1996 }
1997
1998 PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
1999
2000 return 0;
2001}
2002
2003
2004/*--------------------------------------------------------------------------
2005 Initialize the FIFO list of requested frames.
2006 --------------------------------------------------------------------------*/
2007static void w9968cf_init_framelist(struct w9968cf_device* cam)
2008{
2009 u8 i;
2010
2011 for (i = 0; i < cam->nbuffers; i++) {
2012 cam->requested_frame[i] = NULL;
2013 cam->frame[i].queued = 0;
2014 cam->frame[i].status = F_UNUSED;
2015 }
2016}
2017
2018
2019/*--------------------------------------------------------------------------
2020 Add a frame in the FIFO list of requested frames.
2021 This function is called in process context.
2022 --------------------------------------------------------------------------*/
2023static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
2024{
2025 u8 f;
2026 unsigned long lock_flags;
2027
2028 spin_lock_irqsave(&cam->flist_lock, lock_flags);
2029
2030 for (f=0; cam->requested_frame[f] != NULL; f++);
2031 cam->requested_frame[f] = &cam->frame[f_num];
2032 cam->frame[f_num].queued = 1;
2033 cam->frame[f_num].status = F_UNUSED; /* clear the status */
2034
2035 spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
2036
2037 DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
2038}
2039
2040
2041/*--------------------------------------------------------------------------
2042 Read, store and remove the first pointer in the FIFO list of requested
2043 frames. This function is called in interrupt context.
2044 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002045static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
2047{
2048 u8 i;
2049
2050 spin_lock(&cam->flist_lock);
2051
2052 *framep = cam->requested_frame[0];
2053
2054 /* Shift the list of pointers */
2055 for (i = 0; i < cam->nbuffers-1; i++)
2056 cam->requested_frame[i] = cam->requested_frame[i+1];
2057 cam->requested_frame[i] = NULL;
2058
2059 spin_unlock(&cam->flist_lock);
2060
2061 DBG(6,"Popped frame #%d from the list", (*framep)->number)
2062}
2063
2064
2065/*--------------------------------------------------------------------------
2066 High-level video post-processing routine on grabbed frames.
2067 Return 0 on success, a negative number otherwise.
2068 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002069static int
2070w9968cf_postprocess_frame(struct w9968cf_device* cam,
2071 struct w9968cf_frame_t* fr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
2074 u16 w = cam->window.width,
2075 h = cam->window.height,
2076 d = cam->picture.depth,
2077 fmt = cam->picture.palette,
2078 rgb = cam->force_rgb,
2079 hw_w = cam->hw_width,
2080 hw_h = cam->hw_height,
2081 hw_d = cam->hw_depth;
2082 int err = 0;
2083
2084 #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
2085
2086 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2087 memcpy(pOut, pIn, fr->length);
2088 _PSWAP(pIn, pOut)
2089 err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
2090 PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
2091 fr->length = (hw_w*hw_h*hw_d)/8;
2092 _PSWAP(pIn, pOut)
2093 if (err) {
2094 DBG(4, "An error occurred while decoding the frame: "
2095 "%s", symbolic(decoder_errlist, err))
2096 return err;
2097 } else
2098 DBG(6, "Frame decoded")
2099 }
2100
2101 if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
2102 w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
2103 DBG(6, "Original UYVY component ordering changed")
2104 }
2105
2106 if (cam->vpp_flag & VPP_UPSCALE) {
2107 w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
2108 fr->length = (w*h*hw_d)/8;
2109 _PSWAP(pIn, pOut)
2110 DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
2111 hw_w, hw_h, hw_d, w, h)
2112 }
2113
2114 if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
2115 w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
2116 fr->length = (w*h*d)/8;
2117 _PSWAP(pIn, pOut)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002118 DBG(6, "UYVY-16bit to %s conversion done",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 symbolic(v4l1_plist, fmt))
2120 }
2121
2122 if (pOut == fr->buffer)
2123 memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
2124
2125 return 0;
2126}
2127
2128
2129
2130/****************************************************************************
2131 * Image sensor control routines *
2132 ****************************************************************************/
2133
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002134static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
2136{
2137 struct ovcamchip_control ctl;
2138 int err;
2139
2140 ctl.id = cid;
2141 ctl.value = val;
2142
2143 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
2144
2145 return err;
2146}
2147
2148
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002149static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
2151{
2152 struct ovcamchip_control ctl;
2153 int err;
2154
2155 ctl.id = cid;
2156
2157 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
2158 if (!err)
2159 *val = ctl.value;
2160
2161 return err;
2162}
2163
2164
2165static int
2166w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
2167{
2168 struct i2c_client* c = cam->sensor_client;
2169 int rc = 0;
2170
2171 if (!c || !c->driver || !c->driver->command)
2172 return -EINVAL;
2173
2174 rc = c->driver->command(c, cmd, arg);
2175 /* The I2C driver returns -EPERM on non-supported controls */
2176 return (rc < 0 && rc != -EPERM) ? rc : 0;
2177}
2178
2179
2180/*--------------------------------------------------------------------------
2181 Update some settings of the image sensor.
2182 Returns: 0 on success, a negative number otherwise.
2183 --------------------------------------------------------------------------*/
2184static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
2185{
2186 int err = 0;
2187
2188 /* Auto brightness */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002189 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
2190 cam->auto_brt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (err)
2192 return err;
2193
2194 /* Auto exposure */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002195 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
2196 cam->auto_exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (err)
2198 return err;
2199
2200 /* Banding filter */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002201 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
2202 cam->bandfilt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (err)
2204 return err;
2205
2206 /* Light frequency */
2207 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002208 cam->lightfreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (err)
2210 return err;
2211
2212 /* Back light */
2213 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002214 cam->backlight);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (err)
2216 return err;
2217
2218 /* Mirror */
2219 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002220 cam->mirror);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (err)
2222 return err;
2223
2224 return 0;
2225}
2226
2227
2228/*--------------------------------------------------------------------------
2229 Get some current picture settings from the image sensor and update the
2230 internal 'picture' structure of the camera.
2231 Returns: 0 on success, a negative number otherwise.
2232 --------------------------------------------------------------------------*/
2233static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
2234{
2235 int err, v;
2236
2237 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
2238 if (err)
2239 return err;
2240 cam->picture.contrast = v;
2241
2242 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
2243 if (err)
2244 return err;
2245 cam->picture.brightness = v;
2246
2247 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
2248 if (err)
2249 return err;
2250 cam->picture.colour = v;
2251
2252 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
2253 if (err)
2254 return err;
2255 cam->picture.hue = v;
2256
2257 DBG(5, "Got picture settings from the image sensor")
2258
2259 PDBGG("Brightness, contrast, hue, colour, whiteness are "
2260 "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
2261 cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
2262
2263 return 0;
2264}
2265
2266
2267/*--------------------------------------------------------------------------
2268 Update picture settings of the image sensor.
2269 Returns: 0 on success, a negative number otherwise.
2270 --------------------------------------------------------------------------*/
2271static int
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002272w9968cf_sensor_update_picture(struct w9968cf_device* cam,
2273 struct video_picture pict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 int err = 0;
2276
2277 if ((!cam->sensor_initialized)
2278 || pict.contrast != cam->picture.contrast) {
2279 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002280 pict.contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (err)
2282 goto fail;
2283 DBG(4, "Contrast changed from %u to %u",
2284 cam->picture.contrast, pict.contrast)
2285 cam->picture.contrast = pict.contrast;
2286 }
2287
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002288 if (((!cam->sensor_initialized) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002290 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
2291 pict.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (err)
2293 goto fail;
2294 DBG(4, "Brightness changed from %u to %u",
2295 cam->picture.brightness, pict.brightness)
2296 cam->picture.brightness = pict.brightness;
2297 }
2298
2299 if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002300 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
2301 pict.colour);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (err)
2303 goto fail;
2304 DBG(4, "Colour changed from %u to %u",
2305 cam->picture.colour, pict.colour)
2306 cam->picture.colour = pict.colour;
2307 }
2308
2309 if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002310 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
2311 pict.hue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (err)
2313 goto fail;
2314 DBG(4, "Hue changed from %u to %u",
2315 cam->picture.hue, pict.hue)
2316 cam->picture.hue = pict.hue;
2317 }
2318
2319 return 0;
2320
2321fail:
2322 DBG(4, "Failed to change sensor picture setting")
2323 return err;
2324}
2325
2326
2327
2328/****************************************************************************
2329 * Camera configuration *
2330 ****************************************************************************/
2331
2332/*--------------------------------------------------------------------------
2333 This function is called when a supported image sensor is detected.
2334 Return 0 if the initialization succeeds, a negative number otherwise.
2335 --------------------------------------------------------------------------*/
2336static int w9968cf_sensor_init(struct w9968cf_device* cam)
2337{
2338 int err = 0;
2339
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002340 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
2341 &cam->monochrome)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 goto error;
2343
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002344 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
2345 &cam->sensor)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 goto error;
2347
2348 /* NOTE: Make sure width and height are a multiple of 16 */
2349 switch (cam->sensor_client->addr) {
2350 case OV6xx0_SID:
2351 cam->maxwidth = 352;
2352 cam->maxheight = 288;
2353 cam->minwidth = 64;
2354 cam->minheight = 48;
2355 break;
2356 case OV7xx0_SID:
2357 cam->maxwidth = 640;
2358 cam->maxheight = 480;
2359 cam->minwidth = 64;
2360 cam->minheight = 48;
2361 break;
2362 default:
2363 DBG(1, "Not supported image sensor detected for %s",
2364 symbolic(camlist, cam->id))
2365 return -EINVAL;
2366 }
2367
2368 /* These values depend on the ones in the ovxxx0.c sources */
2369 switch (cam->sensor) {
2370 case CC_OV7620:
2371 cam->start_cropx = 287;
2372 cam->start_cropy = 35;
2373 /* Seems to work around a bug in the image sensor */
2374 cam->vs_polarity = 1;
2375 cam->hs_polarity = 1;
2376 break;
2377 default:
2378 cam->start_cropx = 320;
2379 cam->start_cropy = 35;
2380 cam->vs_polarity = 1;
2381 cam->hs_polarity = 0;
2382 }
2383
2384 if ((err = w9968cf_sensor_update_settings(cam)))
2385 goto error;
2386
2387 if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
2388 goto error;
2389
2390 cam->sensor_initialized = 1;
2391
2392 DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
2393 return 0;
2394
2395error:
2396 cam->sensor_initialized = 0;
2397 cam->sensor = CC_UNKNOWN;
2398 DBG(1, "Image sensor initialization failed for %s (/dev/video%d). "
2399 "Try to detach and attach this device again",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002400 symbolic(camlist, cam->id), cam->v4ldev->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return err;
2402}
2403
2404
2405/*--------------------------------------------------------------------------
2406 Fill some basic fields in the main device data structure.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002407 This function is called once on w9968cf_usb_probe() for each recognized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 camera.
2409 --------------------------------------------------------------------------*/
2410static void
2411w9968cf_configure_camera(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002412 struct usb_device* udev,
2413 enum w9968cf_model_id mod_id,
2414 const unsigned short dev_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002416 mutex_init(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 init_waitqueue_head(&cam->open);
2418 spin_lock_init(&cam->urb_lock);
2419 spin_lock_init(&cam->flist_lock);
2420
2421 cam->users = 0;
2422 cam->disconnected = 0;
2423 cam->id = mod_id;
2424 cam->sensor = CC_UNKNOWN;
2425 cam->sensor_initialized = 0;
2426
2427 /* Calculate the alternate setting number (from 1 to 16)
2428 according to the 'packet_size' module parameter */
2429 if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
2430 packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
2431 for (cam->altsetting = 1;
2432 packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
2433 cam->altsetting++);
2434
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002435 cam->max_buffers = (max_buffers[dev_nr] < 2 ||
2436 max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
2437 ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002439 cam->double_buffer = (double_buffer[dev_nr] == 0 ||
2440 double_buffer[dev_nr] == 1)
2441 ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002444 ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 cam->filter_type = (filter_type[dev_nr] == 0 ||
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002447 filter_type[dev_nr] == 1 ||
2448 filter_type[dev_nr] == 2)
2449 ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 cam->capture = 1;
2452
2453 cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002454 ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002456 cam->decompression = (decompression[dev_nr] == 0 ||
2457 decompression[dev_nr] == 1 ||
2458 decompression[dev_nr] == 2)
2459 ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002461 cam->upscaling = (upscaling[dev_nr] == 0 ||
2462 upscaling[dev_nr] == 1)
2463 ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002466 ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002469 ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002472 ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002474 cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
2475 bandingfilter[dev_nr] == 1)
2476 ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002479 ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002482 ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002485 ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002488 ? monochrome[dev_nr] : W9968CF_MONOCHROME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 cam->picture.brightness = (u16)brightness[dev_nr];
2491 cam->picture.hue = (u16)hue[dev_nr];
2492 cam->picture.colour = (u16)colour[dev_nr];
2493 cam->picture.contrast = (u16)contrast[dev_nr];
2494 cam->picture.whiteness = (u16)whiteness[dev_nr];
2495 if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
2496 cam->picture.palette = (u16)force_palette[dev_nr];
2497 cam->force_palette = 1;
2498 } else {
2499 cam->force_palette = 0;
2500 if (cam->decompression == 0)
2501 cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
2502 else if (cam->decompression == 1)
2503 cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
2504 else
2505 cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
2506 }
2507 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2508
2509 cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002510 ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 cam->window.x = 0;
2513 cam->window.y = 0;
2514 cam->window.width = W9968CF_WIDTH;
2515 cam->window.height = W9968CF_HEIGHT;
2516 cam->window.chromakey = 0;
2517 cam->window.clipcount = 0;
2518 cam->window.flags = 0;
2519
2520 DBG(3, "%s configured with settings #%u:",
2521 symbolic(camlist, cam->id), dev_nr)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
2524 wMaxPacketSize[cam->altsetting-1])
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 DBG(3, "- Number of requested video frame buffers: %u",
2527 cam->max_buffers)
2528
2529 if (cam->double_buffer)
2530 DBG(3, "- Hardware double buffering enabled")
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002531 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 DBG(3, "- Hardware double buffering disabled")
2533
2534 if (cam->filter_type == 0)
2535 DBG(3, "- Video filtering disabled")
2536 else if (cam->filter_type == 1)
2537 DBG(3, "- Video filtering enabled: type 1-2-1")
2538 else if (cam->filter_type == 2)
2539 DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
2540
2541 if (cam->clamping)
2542 DBG(3, "- Video data clamping (CCIR-601 format) enabled")
2543 else
2544 DBG(3, "- Video data clamping (CCIR-601 format) disabled")
2545
2546 if (cam->largeview)
2547 DBG(3, "- Large view enabled")
2548 else
2549 DBG(3, "- Large view disabled")
2550
2551 if ((cam->decompression) == 0 && (!cam->force_palette))
2552 DBG(3, "- Decompression disabled")
2553 else if ((cam->decompression) == 1 && (!cam->force_palette))
2554 DBG(3, "- Decompression forced")
2555 else if ((cam->decompression) == 2 && (!cam->force_palette))
2556 DBG(3, "- Decompression allowed")
2557
2558 if (cam->upscaling)
2559 DBG(3, "- Software image scaling enabled")
2560 else
2561 DBG(3, "- Software image scaling disabled")
2562
2563 if (cam->force_palette)
2564 DBG(3, "- Image palette forced to %s",
2565 symbolic(v4l1_plist, cam->picture.palette))
2566
2567 if (cam->force_rgb)
2568 DBG(3, "- RGB component ordering will be used instead of BGR")
2569
2570 if (cam->auto_brt)
2571 DBG(3, "- Auto brightness enabled")
2572 else
2573 DBG(3, "- Auto brightness disabled")
2574
2575 if (cam->auto_exp)
2576 DBG(3, "- Auto exposure enabled")
2577 else
2578 DBG(3, "- Auto exposure disabled")
2579
2580 if (cam->backlight)
2581 DBG(3, "- Backlight exposure algorithm enabled")
2582 else
2583 DBG(3, "- Backlight exposure algorithm disabled")
2584
2585 if (cam->mirror)
2586 DBG(3, "- Mirror enabled")
2587 else
2588 DBG(3, "- Mirror disabled")
2589
2590 if (cam->bandfilt)
2591 DBG(3, "- Banding filter enabled")
2592 else
2593 DBG(3, "- Banding filter disabled")
2594
2595 DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
2596
2597 if (cam->clockdiv == -1)
2598 DBG(3, "- Automatic clock divisor enabled")
2599 else
2600 DBG(3, "- Clock divisor: %d", cam->clockdiv)
2601
2602 if (cam->monochrome)
2603 DBG(3, "- Image sensor used as monochrome")
2604 else
2605 DBG(3, "- Image sensor not used as monochrome")
2606}
2607
2608
2609/*--------------------------------------------------------------------------
2610 If the video post-processing module is not loaded, some parameters
2611 must be overridden.
2612 --------------------------------------------------------------------------*/
2613static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2614{
2615 if (!w9968cf_vpp) {
2616 if (cam->decompression == 1) {
2617 cam->decompression = 2;
2618 DBG(2, "Video post-processing module not found: "
2619 "'decompression' parameter forced to 2")
2620 }
2621 if (cam->upscaling) {
2622 cam->upscaling = 0;
2623 DBG(2, "Video post-processing module not found: "
2624 "'upscaling' parameter forced to 0")
2625 }
2626 if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
2627 cam->force_palette = 0;
2628 DBG(2, "Video post-processing module not found: "
2629 "'force_palette' parameter forced to 0")
2630 }
2631 cam->picture.palette = VIDEO_PALETTE_UYVY;
2632 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2633 }
2634}
2635
2636
2637/*--------------------------------------------------------------------------
2638 Release the resources used by the driver.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002639 This function is called on disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 (or on close if deallocation has been deferred)
2641 --------------------------------------------------------------------------*/
2642static void w9968cf_release_resources(struct w9968cf_device* cam)
2643{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002644 mutex_lock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002646 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 video_unregister_device(cam->v4ldev);
2649 list_del(&cam->v4llist);
2650 i2c_del_adapter(&cam->i2c_adapter);
2651 w9968cf_deallocate_memory(cam);
2652 kfree(cam->control_buffer);
2653 kfree(cam->data_buffer);
2654
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002655 mutex_unlock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658
2659
2660/****************************************************************************
2661 * Video4Linux interface *
2662 ****************************************************************************/
2663
Hans Verkuilbec43662008-12-30 06:58:20 -03002664static int w9968cf_open(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 struct w9968cf_device* cam;
2667 int err;
2668
2669 /* This the only safe way to prevent race conditions with disconnect */
2670 if (!down_read_trylock(&w9968cf_disconnect))
Jiri Slaby3abff552007-10-09 22:03:12 -03002671 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2674
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002675 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 if (cam->sensor == CC_UNKNOWN) {
2678 DBG(2, "No supported image sensor has been detected by the "
2679 "'ovcamchip' module for the %s (/dev/video%d). Make "
2680 "sure it is loaded *before* (re)connecting the camera.",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002681 symbolic(camlist, cam->id), cam->v4ldev->num)
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002682 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 up_read(&w9968cf_disconnect);
2684 return -ENODEV;
2685 }
2686
2687 if (cam->users) {
2688 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002689 symbolic(camlist, cam->id), cam->v4ldev->num, cam->command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002691 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 up_read(&w9968cf_disconnect);
2693 return -EWOULDBLOCK;
2694 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002695 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 err = wait_event_interruptible_exclusive(cam->open,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002697 cam->disconnected ||
2698 !cam->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (err) {
2700 up_read(&w9968cf_disconnect);
2701 return err;
2702 }
2703 if (cam->disconnected) {
2704 up_read(&w9968cf_disconnect);
2705 return -ENODEV;
2706 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002707 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709
2710 DBG(5, "Opening '%s', /dev/video%d ...",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002711 symbolic(camlist, cam->id), cam->v4ldev->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 cam->streaming = 0;
2714 cam->misconfigured = 0;
2715
Adrian Bunk7f2c01a2006-01-09 00:43:39 +01002716 w9968cf_adjust_configuration(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 if ((err = w9968cf_allocate_memory(cam)))
2719 goto deallocate_memory;
2720
2721 if ((err = w9968cf_init_chip(cam)))
2722 goto deallocate_memory;
2723
2724 if ((err = w9968cf_start_transfer(cam)))
2725 goto deallocate_memory;
2726
2727 filp->private_data = cam;
2728
2729 cam->users++;
2730 strcpy(cam->command, current->comm);
2731
2732 init_waitqueue_head(&cam->wait_queue);
2733
2734 DBG(5, "Video device is open")
2735
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002736 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 up_read(&w9968cf_disconnect);
2738
2739 return 0;
2740
2741deallocate_memory:
2742 w9968cf_deallocate_memory(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 DBG(2, "Failed to open the video device")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002744 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 up_read(&w9968cf_disconnect);
2746 return err;
2747}
2748
2749
Hans Verkuilbec43662008-12-30 06:58:20 -03002750static int w9968cf_release(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 struct w9968cf_device* cam;
2753
2754 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2755
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002756 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 w9968cf_stop_transfer(cam);
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (cam->disconnected) {
2761 w9968cf_release_resources(cam);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002762 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 kfree(cam);
2764 return 0;
2765 }
2766
2767 cam->users--;
2768 w9968cf_deallocate_memory(cam);
2769 wake_up_interruptible_nr(&cam->open, 1);
2770
2771 DBG(5, "Video device closed")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002772 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return 0;
2774}
2775
2776
2777static ssize_t
2778w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2779{
2780 struct w9968cf_device* cam;
2781 struct w9968cf_frame_t* fr;
2782 int err = 0;
2783
2784 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2785
2786 if (filp->f_flags & O_NONBLOCK)
2787 return -EWOULDBLOCK;
2788
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002789 if (mutex_lock_interruptible(&cam->fileop_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return -ERESTARTSYS;
2791
2792 if (cam->disconnected) {
2793 DBG(2, "Device not present")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002794 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return -ENODEV;
2796 }
2797
2798 if (cam->misconfigured) {
2799 DBG(2, "The camera is misconfigured. Close and open it again.")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002800 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return -EIO;
2802 }
2803
2804 if (!cam->frame[0].queued)
2805 w9968cf_push_frame(cam, 0);
2806
2807 if (!cam->frame[1].queued)
2808 w9968cf_push_frame(cam, 1);
2809
2810 err = wait_event_interruptible(cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002811 cam->frame[0].status == F_READY ||
2812 cam->frame[1].status == F_READY ||
2813 cam->disconnected);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (err) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002815 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return err;
2817 }
2818 if (cam->disconnected) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002819 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return -ENODEV;
2821 }
2822
2823 fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
2824
2825 if (w9968cf_vpp)
2826 w9968cf_postprocess_frame(cam, fr);
2827
2828 if (count > fr->length)
2829 count = fr->length;
2830
2831 if (copy_to_user(buf, fr->buffer, count)) {
2832 fr->status = F_UNUSED;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002833 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return -EFAULT;
2835 }
2836 *f_pos += count;
2837
2838 fr->status = F_UNUSED;
2839
2840 DBG(5, "%zu bytes read", count)
2841
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002842 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return count;
2844}
2845
2846
2847static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
2848{
2849 struct w9968cf_device* cam = (struct w9968cf_device*)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002850 video_get_drvdata(video_devdata(filp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 unsigned long vsize = vma->vm_end - vma->vm_start,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002852 psize = cam->nbuffers * cam->frame[0].size,
2853 start = vma->vm_start,
2854 pos = (unsigned long)cam->frame[0].buffer,
2855 page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857 if (cam->disconnected) {
2858 DBG(2, "Device not present")
2859 return -ENODEV;
2860 }
2861
2862 if (cam->misconfigured) {
2863 DBG(2, "The camera is misconfigured. Close and open it again")
2864 return -EIO;
2865 }
2866
2867 PDBGG("mmapping %lu bytes...", vsize)
2868
2869 if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
2870 return -EINVAL;
2871
2872 while (vsize > 0) {
2873 page = vmalloc_to_pfn((void *)pos);
2874 if (remap_pfn_range(vma, start, page + vma->vm_pgoff,
2875 PAGE_SIZE, vma->vm_page_prot))
2876 return -EAGAIN;
2877 start += PAGE_SIZE;
2878 pos += PAGE_SIZE;
2879 vsize -= PAGE_SIZE;
2880 }
2881
2882 DBG(5, "mmap method successfully called")
2883 return 0;
2884}
2885
2886
Hans Verkuil069b7472008-12-30 07:04:34 -03002887static long
Hans Verkuilbec43662008-12-30 06:58:20 -03002888w9968cf_ioctl(struct file *filp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002889 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
2891 struct w9968cf_device* cam;
Hans Verkuil069b7472008-12-30 07:04:34 -03002892 long err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2895
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002896 if (mutex_lock_interruptible(&cam->fileop_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return -ERESTARTSYS;
2898
2899 if (cam->disconnected) {
2900 DBG(2, "Device not present")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002901 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return -ENODEV;
2903 }
2904
2905 if (cam->misconfigured) {
2906 DBG(2, "The camera is misconfigured. Close and open it again.")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002907 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return -EIO;
2909 }
2910
Hans Verkuilbec43662008-12-30 06:58:20 -03002911 err = w9968cf_v4l_ioctl(filp, cmd, (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002913 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return err;
2915}
2916
2917
Hans Verkuil069b7472008-12-30 07:04:34 -03002918static long w9968cf_v4l_ioctl(struct file *filp,
Hans Verkuilbec43662008-12-30 06:58:20 -03002919 unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 struct w9968cf_device* cam;
2922 const char* v4l1_ioctls[] = {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002923 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
2925 "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
2926 "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002927 "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
2928 "GVBIFMT", "SVBIFMT"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 };
2930
2931 #define V4L1_IOCTL(cmd) \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002932 ((_IOC_NR((cmd)) < ARRAY_SIZE(v4l1_ioctls)) ? \
2933 v4l1_ioctls[_IOC_NR((cmd))] : "?")
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2936
2937 switch (cmd) {
2938
2939 case VIDIOCGCAP: /* get video capability */
2940 {
2941 struct video_capability cap = {
2942 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
2943 .channels = 1,
2944 .audios = 0,
2945 .minwidth = cam->minwidth,
2946 .minheight = cam->minheight,
2947 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002948 sprintf(cap.name, "W996[87]CF USB Camera #%d",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002949 cam->v4ldev->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 cap.maxwidth = (cam->upscaling && w9968cf_vpp)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002951 ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2952 : cam->maxwidth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 cap.maxheight = (cam->upscaling && w9968cf_vpp)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002954 ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2955 : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 if (copy_to_user(arg, &cap, sizeof(cap)))
2958 return -EFAULT;
2959
2960 DBG(5, "VIDIOCGCAP successfully called")
2961 return 0;
2962 }
2963
2964 case VIDIOCGCHAN: /* get video channel informations */
2965 {
2966 struct video_channel chan;
2967 if (copy_from_user(&chan, arg, sizeof(chan)))
2968 return -EFAULT;
2969
2970 if (chan.channel != 0)
2971 return -EINVAL;
2972
2973 strcpy(chan.name, "Camera");
2974 chan.tuners = 0;
2975 chan.flags = 0;
2976 chan.type = VIDEO_TYPE_CAMERA;
2977 chan.norm = VIDEO_MODE_AUTO;
2978
2979 if (copy_to_user(arg, &chan, sizeof(chan)))
2980 return -EFAULT;
2981
2982 DBG(5, "VIDIOCGCHAN successfully called")
2983 return 0;
2984 }
2985
2986 case VIDIOCSCHAN: /* set active channel */
2987 {
2988 struct video_channel chan;
2989
2990 if (copy_from_user(&chan, arg, sizeof(chan)))
2991 return -EFAULT;
2992
2993 if (chan.channel != 0)
2994 return -EINVAL;
2995
2996 DBG(5, "VIDIOCSCHAN successfully called")
2997 return 0;
2998 }
2999
3000 case VIDIOCGPICT: /* get image properties of the picture */
3001 {
3002 if (w9968cf_sensor_get_picture(cam))
3003 return -EIO;
3004
3005 if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
3006 return -EFAULT;
3007
3008 DBG(5, "VIDIOCGPICT successfully called")
3009 return 0;
3010 }
3011
3012 case VIDIOCSPICT: /* change picture settings */
3013 {
3014 struct video_picture pict;
3015 int err = 0;
3016
3017 if (copy_from_user(&pict, arg, sizeof(pict)))
3018 return -EFAULT;
3019
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003020 if ( (cam->force_palette || !w9968cf_vpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 && pict.palette != cam->picture.palette ) {
3022 DBG(4, "Palette %s rejected: only %s is allowed",
3023 symbolic(v4l1_plist, pict.palette),
3024 symbolic(v4l1_plist, cam->picture.palette))
3025 return -EINVAL;
3026 }
3027
3028 if (!w9968cf_valid_palette(pict.palette)) {
3029 DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
3030 symbolic(v4l1_plist, pict.palette))
3031 return -EINVAL;
3032 }
3033
3034 if (!cam->force_palette) {
3035 if (cam->decompression == 0) {
3036 if (w9968cf_need_decompression(pict.palette)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003037 DBG(4, "Decompression disabled: palette %s is not "
3038 "allowed. VIDIOCSPICT failed",
3039 symbolic(v4l1_plist, pict.palette))
3040 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042 } else if (cam->decompression == 1) {
3043 if (!w9968cf_need_decompression(pict.palette)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003044 DBG(4, "Decompression forced: palette %s is not "
3045 "allowed. VIDIOCSPICT failed",
3046 symbolic(v4l1_plist, pict.palette))
3047 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049 }
3050 }
3051
3052 if (pict.depth != w9968cf_valid_depth(pict.palette)) {
3053 DBG(4, "Requested depth %u bpp is not valid for %s "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003054 "palette: ignored and changed to %u bpp",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 pict.depth, symbolic(v4l1_plist, pict.palette),
3056 w9968cf_valid_depth(pict.palette))
3057 pict.depth = w9968cf_valid_depth(pict.palette);
3058 }
3059
3060 if (pict.palette != cam->picture.palette) {
3061 if(*cam->requested_frame
3062 || cam->frame_current->queued) {
3063 err = wait_event_interruptible
3064 ( cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003065 cam->disconnected ||
3066 (!*cam->requested_frame &&
3067 !cam->frame_current->queued) );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 if (err)
3069 return err;
3070 if (cam->disconnected)
3071 return -ENODEV;
3072 }
3073
3074 if (w9968cf_stop_transfer(cam))
3075 goto ioctl_fail;
3076
3077 if (w9968cf_set_picture(cam, pict))
3078 goto ioctl_fail;
3079
3080 if (w9968cf_start_transfer(cam))
3081 goto ioctl_fail;
3082
3083 } else if (w9968cf_sensor_update_picture(cam, pict))
3084 return -EIO;
3085
3086
3087 DBG(5, "VIDIOCSPICT successfully called")
3088 return 0;
3089 }
3090
3091 case VIDIOCSWIN: /* set capture area */
3092 {
3093 struct video_window win;
3094 int err = 0;
3095
3096 if (copy_from_user(&win, arg, sizeof(win)))
3097 return -EFAULT;
3098
3099 DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
3100 "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
3101 win.x, win.y, win.width, win.height)
3102
3103 if (win.clipcount != 0 || win.flags != 0)
3104 return -EINVAL;
3105
3106 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003107 (u16*)&win.height))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 DBG(4, "Resolution not supported (%ux%u). "
3109 "VIDIOCSWIN failed", win.width, win.height)
3110 return err;
3111 }
3112
3113 if (win.x != cam->window.x ||
3114 win.y != cam->window.y ||
3115 win.width != cam->window.width ||
3116 win.height != cam->window.height) {
3117 if(*cam->requested_frame
3118 || cam->frame_current->queued) {
3119 err = wait_event_interruptible
3120 ( cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003121 cam->disconnected ||
3122 (!*cam->requested_frame &&
3123 !cam->frame_current->queued) );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (err)
3125 return err;
3126 if (cam->disconnected)
3127 return -ENODEV;
3128 }
3129
3130 if (w9968cf_stop_transfer(cam))
3131 goto ioctl_fail;
3132
3133 /* This _must_ be called before set_window() */
3134 if (w9968cf_set_picture(cam, cam->picture))
3135 goto ioctl_fail;
3136
3137 if (w9968cf_set_window(cam, win))
3138 goto ioctl_fail;
3139
3140 if (w9968cf_start_transfer(cam))
3141 goto ioctl_fail;
3142 }
3143
3144 DBG(5, "VIDIOCSWIN successfully called. ")
3145 return 0;
3146 }
3147
3148 case VIDIOCGWIN: /* get current window properties */
3149 {
3150 if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
3151 return -EFAULT;
3152
3153 DBG(5, "VIDIOCGWIN successfully called")
3154 return 0;
3155 }
3156
3157 case VIDIOCGMBUF: /* request for memory (mapped) buffer */
3158 {
3159 struct video_mbuf mbuf;
3160 u8 i;
3161
3162 mbuf.size = cam->nbuffers * cam->frame[0].size;
3163 mbuf.frames = cam->nbuffers;
3164 for (i = 0; i < cam->nbuffers; i++)
3165 mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003166 (unsigned long)cam->frame[0].buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
3169 return -EFAULT;
3170
3171 DBG(5, "VIDIOCGMBUF successfully called")
3172 return 0;
3173 }
3174
3175 case VIDIOCMCAPTURE: /* start the capture to a frame */
3176 {
3177 struct video_mmap mmap;
3178 struct w9968cf_frame_t* fr;
3179 int err = 0;
3180
3181 if (copy_from_user(&mmap, arg, sizeof(mmap)))
3182 return -EFAULT;
3183
3184 DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003185 mmap.frame, symbolic(v4l1_plist, mmap.format),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 mmap.width, mmap.height)
3187
3188 if (mmap.frame >= cam->nbuffers) {
3189 DBG(4, "Invalid frame number (%u). "
3190 "VIDIOCMCAPTURE failed", mmap.frame)
3191 return -EINVAL;
3192 }
3193
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003194 if (mmap.format!=cam->picture.palette &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 (cam->force_palette || !w9968cf_vpp)) {
3196 DBG(4, "Palette %s rejected: only %s is allowed",
3197 symbolic(v4l1_plist, mmap.format),
3198 symbolic(v4l1_plist, cam->picture.palette))
3199 return -EINVAL;
3200 }
3201
3202 if (!w9968cf_valid_palette(mmap.format)) {
3203 DBG(4, "Palette %s not supported. "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003204 "VIDIOCMCAPTURE failed",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 symbolic(v4l1_plist, mmap.format))
3206 return -EINVAL;
3207 }
3208
3209 if (!cam->force_palette) {
3210 if (cam->decompression == 0) {
3211 if (w9968cf_need_decompression(mmap.format)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003212 DBG(4, "Decompression disabled: palette %s is not "
3213 "allowed. VIDIOCSPICT failed",
3214 symbolic(v4l1_plist, mmap.format))
3215 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
3217 } else if (cam->decompression == 1) {
3218 if (!w9968cf_need_decompression(mmap.format)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003219 DBG(4, "Decompression forced: palette %s is not "
3220 "allowed. VIDIOCSPICT failed",
3221 symbolic(v4l1_plist, mmap.format))
3222 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
3224 }
3225 }
3226
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003227 if ((err = w9968cf_adjust_window_size(cam, (u16*)&mmap.width,
3228 (u16*)&mmap.height))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 DBG(4, "Resolution not supported (%dx%d). "
3230 "VIDIOCMCAPTURE failed",
3231 mmap.width, mmap.height)
3232 return err;
3233 }
3234
3235 fr = &cam->frame[mmap.frame];
3236
3237 if (mmap.width != cam->window.width ||
3238 mmap.height != cam->window.height ||
3239 mmap.format != cam->picture.palette) {
3240
3241 struct video_window win;
3242 struct video_picture pict;
3243
3244 if(*cam->requested_frame
3245 || cam->frame_current->queued) {
3246 DBG(6, "VIDIOCMCAPTURE. Change settings for "
3247 "frame #%u: %dx%d, format %s. Wait...",
3248 mmap.frame, mmap.width, mmap.height,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003249 symbolic(v4l1_plist, mmap.format))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 err = wait_event_interruptible
3251 ( cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003252 cam->disconnected ||
3253 (!*cam->requested_frame &&
3254 !cam->frame_current->queued) );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (err)
3256 return err;
3257 if (cam->disconnected)
3258 return -ENODEV;
3259 }
3260
3261 memcpy(&win, &cam->window, sizeof(win));
3262 memcpy(&pict, &cam->picture, sizeof(pict));
3263 win.width = mmap.width;
3264 win.height = mmap.height;
3265 pict.palette = mmap.format;
3266
3267 if (w9968cf_stop_transfer(cam))
3268 goto ioctl_fail;
3269
3270 /* This before set_window */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003271 if (w9968cf_set_picture(cam, pict))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 goto ioctl_fail;
3273
3274 if (w9968cf_set_window(cam, win))
3275 goto ioctl_fail;
3276
3277 if (w9968cf_start_transfer(cam))
3278 goto ioctl_fail;
3279
3280 } else if (fr->queued) {
3281
3282 DBG(6, "Wait until frame #%u is free", mmap.frame)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003283
3284 err = wait_event_interruptible(cam->wait_queue,
3285 cam->disconnected ||
3286 (!fr->queued));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 if (err)
3288 return err;
3289 if (cam->disconnected)
3290 return -ENODEV;
3291 }
3292
3293 w9968cf_push_frame(cam, mmap.frame);
3294 DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
3295 return 0;
3296 }
3297
3298 case VIDIOCSYNC: /* wait until the capture of a frame is finished */
3299 {
3300 unsigned int f_num;
3301 struct w9968cf_frame_t* fr;
3302 int err = 0;
3303
3304 if (copy_from_user(&f_num, arg, sizeof(f_num)))
3305 return -EFAULT;
3306
3307 if (f_num >= cam->nbuffers) {
3308 DBG(4, "Invalid frame number (%u). "
3309 "VIDIOCMCAPTURE failed", f_num)
3310 return -EINVAL;
3311 }
3312
3313 DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
3314
3315 fr = &cam->frame[f_num];
3316
3317 switch (fr->status) {
3318 case F_UNUSED:
3319 if (!fr->queued) {
3320 DBG(4, "VIDIOSYNC: Frame #%u not requested!",
3321 f_num)
3322 return -EFAULT;
3323 }
3324 case F_ERROR:
3325 case F_GRABBING:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003326 err = wait_event_interruptible(cam->wait_queue,
3327 (fr->status == F_READY)
3328 || cam->disconnected);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (err)
3330 return err;
3331 if (cam->disconnected)
3332 return -ENODEV;
3333 break;
3334 case F_READY:
3335 break;
3336 }
3337
3338 if (w9968cf_vpp)
3339 w9968cf_postprocess_frame(cam, fr);
3340
3341 fr->status = F_UNUSED;
3342
3343 DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
3344 return 0;
3345 }
3346
3347 case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
3348 {
3349 struct video_unit unit = {
3350 .video = cam->v4ldev->minor,
3351 .vbi = VIDEO_NO_UNIT,
3352 .radio = VIDEO_NO_UNIT,
3353 .audio = VIDEO_NO_UNIT,
3354 .teletext = VIDEO_NO_UNIT,
3355 };
3356
3357 if (copy_to_user(arg, &unit, sizeof(unit)))
3358 return -EFAULT;
3359
3360 DBG(5, "VIDIOCGUNIT successfully called")
3361 return 0;
3362 }
3363
3364 case VIDIOCKEY:
3365 return 0;
3366
3367 case VIDIOCGFBUF:
3368 {
3369 if (clear_user(arg, sizeof(struct video_buffer)))
3370 return -EFAULT;
3371
3372 DBG(5, "VIDIOCGFBUF successfully called")
3373 return 0;
3374 }
3375
3376 case VIDIOCGTUNER:
3377 {
3378 struct video_tuner tuner;
3379 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3380 return -EFAULT;
3381
3382 if (tuner.tuner != 0)
3383 return -EINVAL;
3384
3385 strcpy(tuner.name, "no_tuner");
3386 tuner.rangelow = 0;
3387 tuner.rangehigh = 0;
3388 tuner.flags = VIDEO_TUNER_NORM;
3389 tuner.mode = VIDEO_MODE_AUTO;
3390 tuner.signal = 0xffff;
3391
3392 if (copy_to_user(arg, &tuner, sizeof(tuner)))
3393 return -EFAULT;
3394
3395 DBG(5, "VIDIOCGTUNER successfully called")
3396 return 0;
3397 }
3398
3399 case VIDIOCSTUNER:
3400 {
3401 struct video_tuner tuner;
3402 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3403 return -EFAULT;
3404
3405 if (tuner.tuner != 0)
3406 return -EINVAL;
3407
3408 if (tuner.mode != VIDEO_MODE_AUTO)
3409 return -EINVAL;
3410
3411 DBG(5, "VIDIOCSTUNER successfully called")
3412 return 0;
3413 }
3414
3415 case VIDIOCSFBUF:
3416 case VIDIOCCAPTURE:
3417 case VIDIOCGFREQ:
3418 case VIDIOCSFREQ:
3419 case VIDIOCGAUDIO:
3420 case VIDIOCSAUDIO:
3421 case VIDIOCSPLAYMODE:
3422 case VIDIOCSWRITEMODE:
3423 case VIDIOCGPLAYINFO:
3424 case VIDIOCSMICROCODE:
3425 case VIDIOCGVBIFMT:
3426 case VIDIOCSVBIFMT:
3427 DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
3428 "(type 0x%01X, "
3429 "n. 0x%01X, "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003430 "dir. 0x%01X, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 "size 0x%02X)",
3432 V4L1_IOCTL(cmd),
3433 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3434
3435 return -EINVAL;
3436
3437 default:
3438 DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
3439 "type 0x%01X, "
3440 "n. 0x%01X, "
3441 "dir. 0x%01X, "
3442 "size 0x%02X",
3443 V4L1_IOCTL(cmd),
3444 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3445
3446 return -ENOIOCTLCMD;
3447
3448 } /* end of switch */
3449
3450ioctl_fail:
3451 cam->misconfigured = 1;
3452 DBG(1, "VIDIOC%s failed because of hardware problems. "
3453 "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
3454 return -EFAULT;
3455}
3456
3457
Hans Verkuilbec43662008-12-30 06:58:20 -03003458static const struct v4l2_file_operations w9968cf_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 .owner = THIS_MODULE,
3460 .open = w9968cf_open,
3461 .release = w9968cf_release,
3462 .read = w9968cf_read,
3463 .ioctl = w9968cf_ioctl,
3464 .mmap = w9968cf_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465};
3466
3467
3468
3469/****************************************************************************
3470 * USB probe and V4L registration, disconnect and id_table[] definition *
3471 ****************************************************************************/
3472
3473static int
3474w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3475{
3476 struct usb_device *udev = interface_to_usbdev(intf);
3477 struct w9968cf_device* cam;
3478 int err = 0;
3479 enum w9968cf_model_id mod_id;
3480 struct list_head* ptr;
3481 u8 sc = 0; /* number of simultaneous cameras */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -03003482 static unsigned short dev_nr; /* 0 - we are handling device number n */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
3484 if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[0].idVendor &&
3485 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[0].idProduct)
3486 mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
3487 else if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[1].idVendor &&
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003488 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[1].idProduct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
3490 else
3491 return -ENODEV;
3492
3493 cam = (struct w9968cf_device*)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003494 kzalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 if (!cam)
3496 return -ENOMEM;
3497
Hans Verkuil4e068392009-03-08 06:56:19 -03003498 err = v4l2_device_register(&udev->dev, &cam->v4l2_dev);
3499 if (err)
3500 goto fail0;
3501
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003502 mutex_init(&cam->dev_mutex);
3503 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 cam->usbdev = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
3507 DBG(2, "%s detected", symbolic(camlist, mod_id))
3508
3509 if (simcams > W9968CF_MAX_DEVICES)
3510 simcams = W9968CF_SIMCAMS;
3511
3512 /* How many cameras are connected ? */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003513 mutex_lock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 list_for_each(ptr, &w9968cf_dev_list)
3515 sc++;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003516 mutex_unlock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
3518 if (sc >= simcams) {
3519 DBG(2, "Device rejected: too many connected cameras "
3520 "(max. %u)", simcams)
3521 err = -EPERM;
3522 goto fail;
3523 }
3524
3525
3526 /* Allocate 2 bytes of memory for camera control USB transfers */
Oliver Neukumb10b4172006-01-06 21:28:40 +01003527 if (!(cam->control_buffer = kzalloc(2, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 DBG(1,"Couldn't allocate memory for camera control transfers")
3529 err = -ENOMEM;
3530 goto fail;
3531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
Oliver Neukumb10b4172006-01-06 21:28:40 +01003534 if (!(cam->data_buffer = kzalloc(8, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 DBG(1, "Couldn't allocate memory for data "
3536 "transfers to the FSB")
3537 err = -ENOMEM;
3538 goto fail;
3539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 /* Register the V4L device */
3542 cam->v4ldev = video_device_alloc();
3543 if (!cam->v4ldev) {
3544 DBG(1, "Could not allocate memory for a V4L structure")
3545 err = -ENOMEM;
3546 goto fail;
3547 }
3548
3549 strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 cam->v4ldev->fops = &w9968cf_fops;
3551 cam->v4ldev->minor = video_nr[dev_nr];
3552 cam->v4ldev->release = video_device_release;
3553 video_set_drvdata(cam->v4ldev, cam);
Hans Verkuil4e068392009-03-08 06:56:19 -03003554 cam->v4ldev->v4l2_dev = &cam->v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003557 video_nr[dev_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 if (err) {
3559 DBG(1, "V4L device registration failed")
3560 if (err == -ENFILE && video_nr[dev_nr] == -1)
3561 DBG(2, "Couldn't find a free /dev/videoX node")
3562 video_nr[dev_nr] = -1;
3563 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3564 goto fail;
3565 }
3566
Hans Verkuilc6330fb2008-10-19 18:54:26 -03003567 DBG(2, "V4L device registered as /dev/video%d", cam->v4ldev->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
3569 /* Set some basic constants */
3570 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3571
3572 /* Add a new entry into the list of V4L registered devices */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003573 mutex_lock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 list_add(&cam->v4llist, &w9968cf_dev_list);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003575 mutex_unlock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3577
3578 w9968cf_turn_on_led(cam);
3579
3580 w9968cf_i2c_init(cam);
3581
3582 usb_set_intfdata(intf, cam);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003583 mutex_unlock(&cam->dev_mutex);
Hans Verkuil4e068392009-03-08 06:56:19 -03003584
3585 if (ovmod_load)
3586 request_module("ovcamchip");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 return 0;
3588
3589fail: /* Free unused memory */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003590 kfree(cam->control_buffer);
3591 kfree(cam->data_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 if (cam->v4ldev)
3593 video_device_release(cam->v4ldev);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003594 mutex_unlock(&cam->dev_mutex);
Hans Verkuil4e068392009-03-08 06:56:19 -03003595 v4l2_device_unregister(&cam->v4l2_dev);
3596fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 kfree(cam);
3598 return err;
3599}
3600
3601
3602static void w9968cf_usb_disconnect(struct usb_interface* intf)
3603{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003604 struct w9968cf_device* cam =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 (struct w9968cf_device*)usb_get_intfdata(intf);
3606
3607 down_write(&w9968cf_disconnect);
3608
3609 if (cam) {
3610 /* Prevent concurrent accesses to data */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003611 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
3613 cam->disconnected = 1;
3614
3615 DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id))
3616
3617 wake_up_interruptible_all(&cam->open);
3618
3619 if (cam->users) {
3620 DBG(2, "The device is open (/dev/video%d)! "
3621 "Process name: %s. Deregistration and memory "
3622 "deallocation are deferred on close.",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03003623 cam->v4ldev->num, cam->command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 cam->misconfigured = 1;
3625 w9968cf_stop_transfer(cam);
3626 wake_up_interruptible(&cam->wait_queue);
3627 } else
3628 w9968cf_release_resources(cam);
3629
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003630 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Hans Verkuil4e068392009-03-08 06:56:19 -03003632 if (!cam->users) {
3633 v4l2_device_unregister(&cam->v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 kfree(cam);
Hans Verkuil4e068392009-03-08 06:56:19 -03003635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 }
3637
3638 up_write(&w9968cf_disconnect);
3639}
3640
3641
3642static struct usb_driver w9968cf_usb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 .name = "w9968cf",
3644 .id_table = winbond_id_table,
3645 .probe = w9968cf_usb_probe,
3646 .disconnect = w9968cf_usb_disconnect,
3647};
3648
3649
3650
3651/****************************************************************************
3652 * Module init, exit and intermodule communication *
3653 ****************************************************************************/
3654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655static int __init w9968cf_module_init(void)
3656{
3657 int err;
3658
3659 KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
3660 KDBG(3, W9968CF_MODULE_AUTHOR)
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 if ((err = usb_register(&w9968cf_usb_driver)))
3663 return err;
3664
3665 return 0;
3666}
3667
3668
3669static void __exit w9968cf_module_exit(void)
3670{
3671 /* w9968cf_usb_disconnect() will be called */
3672 usb_deregister(&w9968cf_usb_driver);
3673
3674 KDBG(2, W9968CF_MODULE_NAME" deregistered")
3675}
3676
3677
3678module_init(w9968cf_module_init);
3679module_exit(w9968cf_module_exit);
3680