blob: fd5c4c87a73b4b30204acb54c75390f40e3a1b05 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static unsigned short simcams = W9968CF_SIMCAMS;
72static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030073static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
74 W9968CF_PACKET_SIZE};
75static unsigned short max_buffers[] = {[0 ... W9968CF_MAX_DEVICES-1] =
76 W9968CF_BUFFERS};
77static int double_buffer[] = {[0 ... W9968CF_MAX_DEVICES-1] =
78 W9968CF_DOUBLE_BUFFER};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int clamping[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLAMPING};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030080static unsigned short filter_type[]= {[0 ... W9968CF_MAX_DEVICES-1] =
81 W9968CF_FILTER_TYPE};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static int largeview[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_LARGEVIEW};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030083static unsigned short decompression[] = {[0 ... W9968CF_MAX_DEVICES-1] =
84 W9968CF_DECOMPRESSION};
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int upscaling[]= {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_UPSCALING};
86static unsigned short force_palette[] = {[0 ... W9968CF_MAX_DEVICES-1] = 0};
87static int force_rgb[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_FORCE_RGB};
88static int autobright[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOBRIGHT};
89static int autoexp[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_AUTOEXP};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030090static unsigned short lightfreq[] = {[0 ... W9968CF_MAX_DEVICES-1] =
91 W9968CF_LIGHTFREQ};
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int bandingfilter[] = {[0 ... W9968CF_MAX_DEVICES-1]=
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030093 W9968CF_BANDINGFILTER};
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static short clockdiv[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_CLOCKDIV};
95static int backlight[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_BACKLIGHT};
96static int mirror[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_MIRROR};
97static int monochrome[] = {[0 ... W9968CF_MAX_DEVICES-1]=W9968CF_MONOCHROME};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030098static unsigned int brightness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
99 W9968CF_BRIGHTNESS};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static unsigned int hue[] = {[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_HUE};
101static unsigned int colour[]={[0 ... W9968CF_MAX_DEVICES-1] = W9968CF_COLOUR};
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300102static unsigned int contrast[] = {[0 ... W9968CF_MAX_DEVICES-1] =
103 W9968CF_CONTRAST};
104static unsigned int whiteness[] = {[0 ... W9968CF_MAX_DEVICES-1] =
105 W9968CF_WHITENESS};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#ifdef W9968CF_DEBUG
107static unsigned short debug = W9968CF_DEBUG_LEVEL;
108static int specific_debug = W9968CF_SPECIFIC_DEBUG;
109#endif
110
111static unsigned int param_nv[24]; /* number of values per parameter */
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113module_param(simcams, ushort, 0644);
114module_param_array(video_nr, short, &param_nv[0], 0444);
115module_param_array(packet_size, uint, &param_nv[1], 0444);
116module_param_array(max_buffers, ushort, &param_nv[2], 0444);
117module_param_array(double_buffer, bool, &param_nv[3], 0444);
118module_param_array(clamping, bool, &param_nv[4], 0444);
119module_param_array(filter_type, ushort, &param_nv[5], 0444);
120module_param_array(largeview, bool, &param_nv[6], 0444);
121module_param_array(decompression, ushort, &param_nv[7], 0444);
122module_param_array(upscaling, bool, &param_nv[8], 0444);
123module_param_array(force_palette, ushort, &param_nv[9], 0444);
124module_param_array(force_rgb, ushort, &param_nv[10], 0444);
125module_param_array(autobright, bool, &param_nv[11], 0444);
126module_param_array(autoexp, bool, &param_nv[12], 0444);
127module_param_array(lightfreq, ushort, &param_nv[13], 0444);
128module_param_array(bandingfilter, bool, &param_nv[14], 0444);
129module_param_array(clockdiv, short, &param_nv[15], 0444);
130module_param_array(backlight, bool, &param_nv[16], 0444);
131module_param_array(mirror, bool, &param_nv[17], 0444);
132module_param_array(monochrome, bool, &param_nv[18], 0444);
133module_param_array(brightness, uint, &param_nv[19], 0444);
134module_param_array(hue, uint, &param_nv[20], 0444);
135module_param_array(colour, uint, &param_nv[21], 0444);
136module_param_array(contrast, uint, &param_nv[22], 0444);
137module_param_array(whiteness, uint, &param_nv[23], 0444);
138#ifdef W9968CF_DEBUG
139module_param(debug, ushort, 0644);
140module_param(specific_debug, bool, 0644);
141#endif
142
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300143MODULE_PARM_DESC(simcams,
144 "\n<n> Number of cameras allowed to stream simultaneously."
145 "\nn may vary from 0 to "
146 __MODULE_STRING(W9968CF_MAX_DEVICES)"."
147 "\nDefault value is "__MODULE_STRING(W9968CF_SIMCAMS)"."
148 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149MODULE_PARM_DESC(video_nr,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300150 "\n<-1|n[,...]> Specify V4L minor mode number."
151 "\n -1 = use next available (default)"
152 "\n n = use minor number n (integer >= 0)"
153 "\nYou can specify up to "__MODULE_STRING(W9968CF_MAX_DEVICES)
154 " cameras this way."
155 "\nFor example:"
156 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
157 "\nthe second camera and use auto for the first"
158 "\none and for every other camera."
159 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160MODULE_PARM_DESC(packet_size,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300161 "\n<n[,...]> Specify the maximum data payload"
162 "\nsize in bytes for alternate settings, for each device."
163 "\nn is scaled between 63 and 1023 "
164 "(default is "__MODULE_STRING(W9968CF_PACKET_SIZE)")."
165 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166MODULE_PARM_DESC(max_buffers,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300167 "\n<n[,...]> For advanced users."
168 "\nSpecify the maximum number of video frame buffers"
169 "\nto allocate for each device, from 2 to "
170 __MODULE_STRING(W9968CF_MAX_BUFFERS)
171 ". (default is "__MODULE_STRING(W9968CF_BUFFERS)")."
172 "\n");
173MODULE_PARM_DESC(double_buffer,
174 "\n<0|1[,...]> "
175 "Hardware double buffering: 0 disabled, 1 enabled."
176 "\nIt should be enabled if you want smooth video output: if"
177 "\nyou obtain out of sync. video, disable it, or try to"
178 "\ndecrease the 'clockdiv' module parameter value."
179 "\nDefault value is "__MODULE_STRING(W9968CF_DOUBLE_BUFFER)
180 " for every device."
181 "\n");
182MODULE_PARM_DESC(clamping,
183 "\n<0|1[,...]> Video data clamping: 0 disabled, 1 enabled."
184 "\nDefault value is "__MODULE_STRING(W9968CF_CLAMPING)
185 " for every device."
186 "\n");
187MODULE_PARM_DESC(filter_type,
188 "\n<0|1|2[,...]> Video filter type."
189 "\n0 none, 1 (1-2-1) 3-tap filter, "
190 "2 (2-3-6-3-2) 5-tap filter."
191 "\nDefault value is "__MODULE_STRING(W9968CF_FILTER_TYPE)
192 " for every device."
193 "\nThe filter is used to reduce noise and aliasing artifacts"
194 "\nproduced by the CCD or CMOS image sensor, and the scaling"
195 " process."
196 "\n");
197MODULE_PARM_DESC(largeview,
198 "\n<0|1[,...]> Large view: 0 disabled, 1 enabled."
199 "\nDefault value is "__MODULE_STRING(W9968CF_LARGEVIEW)
200 " for every device."
201 "\n");
202MODULE_PARM_DESC(upscaling,
203 "\n<0|1[,...]> Software scaling (for non-compressed video):"
204 "\n0 disabled, 1 enabled."
205 "\nDisable it if you have a slow CPU or you don't have"
206 " enough memory."
207 "\nDefault value is "__MODULE_STRING(W9968CF_UPSCALING)
208 " for every device."
209 "\nIf 'w9968cf-vpp' is not present, this parameter is"
210 " set to 0."
211 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212MODULE_PARM_DESC(decompression,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300213 "\n<0|1|2[,...]> Software video decompression:"
214 "\n- 0 disables decompression (doesn't allow formats needing"
215 " decompression)"
216 "\n- 1 forces decompression (allows formats needing"
217 " decompression only);"
218 "\n- 2 allows any permitted formats."
219 "\nFormats supporting compressed video are YUV422P and"
220 " YUV420P/YUV420 "
221 "\nin any resolutions where both width and height are "
222 "a multiple of 16."
223 "\nDefault value is "__MODULE_STRING(W9968CF_DECOMPRESSION)
224 " for every device."
225 "\nIf 'w9968cf-vpp' is not present, forcing decompression is "
226 "\nnot allowed; in this case this parameter is set to 2."
227 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228MODULE_PARM_DESC(force_palette,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300229 "\n<0"
230 "|" __MODULE_STRING(VIDEO_PALETTE_UYVY)
231 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420)
232 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422P)
233 "|" __MODULE_STRING(VIDEO_PALETTE_YUV420P)
234 "|" __MODULE_STRING(VIDEO_PALETTE_YUYV)
235 "|" __MODULE_STRING(VIDEO_PALETTE_YUV422)
236 "|" __MODULE_STRING(VIDEO_PALETTE_GREY)
237 "|" __MODULE_STRING(VIDEO_PALETTE_RGB555)
238 "|" __MODULE_STRING(VIDEO_PALETTE_RGB565)
239 "|" __MODULE_STRING(VIDEO_PALETTE_RGB24)
240 "|" __MODULE_STRING(VIDEO_PALETTE_RGB32)
241 "[,...]>"
242 " Force picture palette."
243 "\nIn order:"
244 "\n- 0 allows any of the following formats:"
245 "\n- UYVY 16 bpp - Original video, compression disabled"
246 "\n- YUV420 12 bpp - Original video, compression enabled"
247 "\n- YUV422P 16 bpp - Original video, compression enabled"
248 "\n- YUV420P 12 bpp - Original video, compression enabled"
249 "\n- YUVY 16 bpp - Software conversion from UYVY"
250 "\n- YUV422 16 bpp - Software conversion from UYVY"
251 "\n- GREY 8 bpp - Software conversion from UYVY"
252 "\n- RGB555 16 bpp - Software conversion from UYVY"
253 "\n- RGB565 16 bpp - Software conversion from UYVY"
254 "\n- RGB24 24 bpp - Software conversion from UYVY"
255 "\n- RGB32 32 bpp - Software conversion from UYVY"
256 "\nWhen not 0, this parameter will override 'decompression'."
257 "\nDefault value is 0 for every device."
258 "\nInitial palette is "
259 __MODULE_STRING(W9968CF_PALETTE_DECOMP_ON)"."
260 "\nIf 'w9968cf-vpp' is not present, this parameter is"
261 " set to 9 (UYVY)."
262 "\n");
263MODULE_PARM_DESC(force_rgb,
264 "\n<0|1[,...]> Read RGB video data instead of BGR:"
265 "\n 1 = use RGB component ordering."
266 "\n 0 = use BGR component ordering."
267 "\nThis parameter has effect when using RGBX palettes only."
268 "\nDefault value is "__MODULE_STRING(W9968CF_FORCE_RGB)
269 " for every device."
270 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271MODULE_PARM_DESC(autobright,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300272 "\n<0|1[,...]> Image sensor automatically changes brightness:"
273 "\n 0 = no, 1 = yes"
274 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOBRIGHT)
275 " for every device."
276 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277MODULE_PARM_DESC(autoexp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300278 "\n<0|1[,...]> Image sensor automatically changes exposure:"
279 "\n 0 = no, 1 = yes"
280 "\nDefault value is "__MODULE_STRING(W9968CF_AUTOEXP)
281 " for every device."
282 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283MODULE_PARM_DESC(lightfreq,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300284 "\n<50|60[,...]> Light frequency in Hz:"
285 "\n 50 for European and Asian lighting,"
286 " 60 for American lighting."
287 "\nDefault value is "__MODULE_STRING(W9968CF_LIGHTFREQ)
288 " for every device."
289 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290MODULE_PARM_DESC(bandingfilter,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300291 "\n<0|1[,...]> Banding filter to reduce effects of"
292 " fluorescent lighting:"
293 "\n 0 disabled, 1 enabled."
294 "\nThis filter tries to reduce the pattern of horizontal"
295 "\nlight/dark bands caused by some (usually fluorescent)"
296 " lighting."
297 "\nDefault value is "__MODULE_STRING(W9968CF_BANDINGFILTER)
298 " for every device."
299 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300MODULE_PARM_DESC(clockdiv,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300301 "\n<-1|n[,...]> "
302 "Force pixel clock divisor to a specific value (for experts):"
303 "\n n may vary from 0 to 127."
304 "\n -1 for automatic value."
305 "\nSee also the 'double_buffer' module parameter."
306 "\nDefault value is "__MODULE_STRING(W9968CF_CLOCKDIV)
307 " for every device."
308 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309MODULE_PARM_DESC(backlight,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300310 "\n<0|1[,...]> Objects are lit from behind:"
311 "\n 0 = no, 1 = yes"
312 "\nDefault value is "__MODULE_STRING(W9968CF_BACKLIGHT)
313 " for every device."
314 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315MODULE_PARM_DESC(mirror,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300316 "\n<0|1[,...]> Reverse image horizontally:"
317 "\n 0 = no, 1 = yes"
318 "\nDefault value is "__MODULE_STRING(W9968CF_MIRROR)
319 " for every device."
320 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321MODULE_PARM_DESC(monochrome,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300322 "\n<0|1[,...]> Use image sensor as monochrome sensor:"
323 "\n 0 = no, 1 = yes"
324 "\nNot all the sensors support monochrome color."
325 "\nDefault value is "__MODULE_STRING(W9968CF_MONOCHROME)
326 " for every device."
327 "\n");
328MODULE_PARM_DESC(brightness,
329 "\n<n[,...]> Set picture brightness (0-65535)."
330 "\nDefault value is "__MODULE_STRING(W9968CF_BRIGHTNESS)
331 " for every device."
332 "\nThis parameter has no effect if 'autobright' is enabled."
333 "\n");
334MODULE_PARM_DESC(hue,
335 "\n<n[,...]> Set picture hue (0-65535)."
336 "\nDefault value is "__MODULE_STRING(W9968CF_HUE)
337 " for every device."
338 "\n");
339MODULE_PARM_DESC(colour,
340 "\n<n[,...]> Set picture saturation (0-65535)."
341 "\nDefault value is "__MODULE_STRING(W9968CF_COLOUR)
342 " for every device."
343 "\n");
344MODULE_PARM_DESC(contrast,
345 "\n<n[,...]> Set picture contrast (0-65535)."
346 "\nDefault value is "__MODULE_STRING(W9968CF_CONTRAST)
347 " for every device."
348 "\n");
349MODULE_PARM_DESC(whiteness,
350 "\n<n[,...]> Set picture whiteness (0-65535)."
351 "\nDefault value is "__MODULE_STRING(W9968CF_WHITENESS)
352 " for every device."
353 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#ifdef W9968CF_DEBUG
355MODULE_PARM_DESC(debug,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300356 "\n<n> Debugging information level, from 0 to 6:"
357 "\n0 = none (use carefully)"
358 "\n1 = critical errors"
359 "\n2 = significant informations"
360 "\n3 = configuration or general messages"
361 "\n4 = warnings"
362 "\n5 = called functions"
363 "\n6 = function internals"
364 "\nLevel 5 and 6 are useful for testing only, when only "
365 "one device is used."
366 "\nDefault value is "__MODULE_STRING(W9968CF_DEBUG_LEVEL)"."
367 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368MODULE_PARM_DESC(specific_debug,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300369 "\n<0|1> Enable or disable specific debugging messages:"
370 "\n0 = print messages concerning every level"
371 " <= 'debug' level."
372 "\n1 = print messages concerning the level"
373 " indicated by 'debug'."
374 "\nDefault value is "
375 __MODULE_STRING(W9968CF_SPECIFIC_DEBUG)"."
376 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#endif /* W9968CF_DEBUG */
378
379
380
381/****************************************************************************
382 * Some prototypes *
383 ****************************************************************************/
384
385/* Video4linux interface */
Hans Verkuilbec43662008-12-30 06:58:20 -0300386static const struct v4l2_file_operations w9968cf_fops;
387static int w9968cf_open(struct file *);
388static int w9968cf_release(struct file *);
389static int w9968cf_mmap(struct file *, struct vm_area_struct *);
Hans Verkuil069b7472008-12-30 07:04:34 -0300390static long w9968cf_ioctl(struct file *, unsigned, unsigned long);
Hans Verkuilbec43662008-12-30 06:58:20 -0300391static ssize_t w9968cf_read(struct file *, char __user *, size_t, loff_t *);
Hans Verkuil069b7472008-12-30 07:04:34 -0300392static long w9968cf_v4l_ioctl(struct file *, unsigned int,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300393 void __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395/* USB-specific */
396static int w9968cf_start_transfer(struct w9968cf_device*);
397static int w9968cf_stop_transfer(struct w9968cf_device*);
398static int w9968cf_write_reg(struct w9968cf_device*, u16 value, u16 index);
399static int w9968cf_read_reg(struct w9968cf_device*, u16 index);
400static int w9968cf_write_fsb(struct w9968cf_device*, u16* data);
401static int w9968cf_write_sb(struct w9968cf_device*, u16 value);
402static int w9968cf_read_sb(struct w9968cf_device*);
403static int w9968cf_upload_quantizationtables(struct w9968cf_device*);
David Howells7d12e782006-10-05 14:55:46 +0100404static void w9968cf_urb_complete(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/* Low-level I2C (SMBus) I/O */
407static int w9968cf_smbus_start(struct w9968cf_device*);
408static int w9968cf_smbus_stop(struct w9968cf_device*);
409static int w9968cf_smbus_write_byte(struct w9968cf_device*, u8 v);
410static int w9968cf_smbus_read_byte(struct w9968cf_device*, u8* v);
411static int w9968cf_smbus_write_ack(struct w9968cf_device*);
412static int w9968cf_smbus_read_ack(struct w9968cf_device*);
413static int w9968cf_smbus_refresh_bus(struct w9968cf_device*);
414static int w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300415 u16 address, u8* value);
416static int w9968cf_i2c_adap_read_byte_data(struct w9968cf_device*, u16 address,
417 u8 subaddress, u8* value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static int w9968cf_i2c_adap_write_byte(struct w9968cf_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300419 u16 address, u8 subaddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static int w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300421 u16 address, u8 subaddress,
422 u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424/* I2C interface to kernel */
425static int w9968cf_i2c_init(struct w9968cf_device*);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300426static int w9968cf_i2c_smbus_xfer(struct i2c_adapter*, u16 addr,
427 unsigned short flags, char read_write,
428 u8 command, int size, union i2c_smbus_data*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static u32 w9968cf_i2c_func(struct i2c_adapter*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/* Memory management */
432static void* rvmalloc(unsigned long size);
433static void rvfree(void *mem, unsigned long size);
434static void w9968cf_deallocate_memory(struct w9968cf_device*);
435static int w9968cf_allocate_memory(struct w9968cf_device*);
436
437/* High-level image sensor control functions */
438static int w9968cf_sensor_set_control(struct w9968cf_device*,int cid,int val);
439static int w9968cf_sensor_get_control(struct w9968cf_device*,int cid,int *val);
440static int w9968cf_sensor_cmd(struct w9968cf_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300441 unsigned int cmd, void *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static int w9968cf_sensor_init(struct w9968cf_device*);
443static int w9968cf_sensor_update_settings(struct w9968cf_device*);
444static int w9968cf_sensor_get_picture(struct w9968cf_device*);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300445static int w9968cf_sensor_update_picture(struct w9968cf_device*,
446 struct video_picture pict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448/* Other helper functions */
449static void w9968cf_configure_camera(struct w9968cf_device*,struct usb_device*,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300450 enum w9968cf_model_id,
451 const unsigned short dev_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static void w9968cf_adjust_configuration(struct w9968cf_device*);
453static int w9968cf_turn_on_led(struct w9968cf_device*);
454static int w9968cf_init_chip(struct w9968cf_device*);
455static inline u16 w9968cf_valid_palette(u16 palette);
456static inline u16 w9968cf_valid_depth(u16 palette);
457static inline u8 w9968cf_need_decompression(u16 palette);
458static int w9968cf_set_picture(struct w9968cf_device*, struct video_picture);
459static int w9968cf_set_window(struct w9968cf_device*, struct video_window);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300460static int w9968cf_postprocess_frame(struct w9968cf_device*,
461 struct w9968cf_frame_t*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462static int w9968cf_adjust_window_size(struct w9968cf_device*, u16* w, u16* h);
463static void w9968cf_init_framelist(struct w9968cf_device*);
464static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
465static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
466static void w9968cf_release_resources(struct w9968cf_device*);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469
470/****************************************************************************
471 * Symbolic names *
472 ****************************************************************************/
473
474/* Used to represent a list of values and their respective symbolic names */
475struct w9968cf_symbolic_list {
476 const int num;
477 const char *name;
478};
479
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300480/*--------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 Returns the name of the matching element in the symbolic_list array. The
482 end of the list must be marked with an element that has a NULL name.
483 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300484static inline const char *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485symbolic(struct w9968cf_symbolic_list list[], const int num)
486{
487 int i;
488
489 for (i = 0; list[i].name != NULL; i++)
490 if (list[i].num == num)
491 return (list[i].name);
492
493 return "Unknown";
494}
495
496static struct w9968cf_symbolic_list camlist[] = {
497 { W9968CF_MOD_GENERIC, "W996[87]CF JPEG USB Dual Mode Camera" },
498 { W9968CF_MOD_CLVBWGP, "Creative Labs Video Blaster WebCam Go Plus" },
499
500 /* Other cameras (having the same descriptors as Generic W996[87]CF) */
501 { W9968CF_MOD_ADPVDMA, "Aroma Digi Pen VGA Dual Mode ADG-5000" },
502 { W9986CF_MOD_AAU, "AVerMedia AVerTV USB" },
503 { W9968CF_MOD_CLVBWG, "Creative Labs Video Blaster WebCam Go" },
504 { W9968CF_MOD_LL, "Lebon LDC-035A" },
505 { W9968CF_MOD_EEEMC, "Ezonics EZ-802 EZMega Cam" },
506 { W9968CF_MOD_OOE, "OmniVision OV8610-EDE" },
507 { W9968CF_MOD_ODPVDMPC, "OPCOM Digi Pen VGA Dual Mode Pen Camera" },
508 { W9968CF_MOD_PDPII, "Pretec Digi Pen-II" },
509 { W9968CF_MOD_PDP480, "Pretec DigiPen-480" },
510
511 { -1, NULL }
512};
513
514static struct w9968cf_symbolic_list senlist[] = {
515 { CC_OV76BE, "OV76BE" },
516 { CC_OV7610, "OV7610" },
517 { CC_OV7620, "OV7620" },
518 { CC_OV7620AE, "OV7620AE" },
519 { CC_OV6620, "OV6620" },
520 { CC_OV6630, "OV6630" },
521 { CC_OV6630AE, "OV6630AE" },
522 { CC_OV6630AF, "OV6630AF" },
523 { -1, NULL }
524};
525
526/* Video4Linux1 palettes */
527static struct w9968cf_symbolic_list v4l1_plist[] = {
528 { VIDEO_PALETTE_GREY, "GREY" },
529 { VIDEO_PALETTE_HI240, "HI240" },
530 { VIDEO_PALETTE_RGB565, "RGB565" },
531 { VIDEO_PALETTE_RGB24, "RGB24" },
532 { VIDEO_PALETTE_RGB32, "RGB32" },
533 { VIDEO_PALETTE_RGB555, "RGB555" },
534 { VIDEO_PALETTE_YUV422, "YUV422" },
535 { VIDEO_PALETTE_YUYV, "YUYV" },
536 { VIDEO_PALETTE_UYVY, "UYVY" },
537 { VIDEO_PALETTE_YUV420, "YUV420" },
538 { VIDEO_PALETTE_YUV411, "YUV411" },
539 { VIDEO_PALETTE_RAW, "RAW" },
540 { VIDEO_PALETTE_YUV422P, "YUV422P" },
541 { VIDEO_PALETTE_YUV411P, "YUV411P" },
542 { VIDEO_PALETTE_YUV420P, "YUV420P" },
543 { VIDEO_PALETTE_YUV410P, "YUV410P" },
544 { -1, NULL }
545};
546
547/* Decoder error codes: */
548static struct w9968cf_symbolic_list decoder_errlist[] = {
549 { W9968CF_DEC_ERR_CORRUPTED_DATA, "Corrupted data" },
550 { W9968CF_DEC_ERR_BUF_OVERFLOW, "Buffer overflow" },
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300551 { W9968CF_DEC_ERR_NO_SOI, "SOI marker not found" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 { W9968CF_DEC_ERR_NO_SOF0, "SOF0 marker not found" },
553 { W9968CF_DEC_ERR_NO_SOS, "SOS marker not found" },
554 { W9968CF_DEC_ERR_NO_EOI, "EOI marker not found" },
555 { -1, NULL }
556};
557
558/* URB error codes: */
559static struct w9968cf_symbolic_list urb_errlist[] = {
560 { -ENOMEM, "No memory for allocation of internal structures" },
561 { -ENOSPC, "The host controller's bandwidth is already consumed" },
562 { -ENOENT, "URB was canceled by unlink_urb" },
563 { -EXDEV, "ISO transfer only partially completed" },
564 { -EAGAIN, "Too match scheduled for the future" },
565 { -ENXIO, "URB already queued" },
566 { -EFBIG, "Too much ISO frames requested" },
567 { -ENOSR, "Buffer error (overrun)" },
568 { -EPIPE, "Specified endpoint is stalled (device not responding)"},
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700569 { -EOVERFLOW, "Babble (too much data)" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 { -EPROTO, "Bit-stuff error (bad cable?)" },
571 { -EILSEQ, "CRC/Timeout" },
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700572 { -ETIME, "Device does not respond to token" },
573 { -ETIMEDOUT, "Device does not respond to command" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 { -1, NULL }
575};
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/****************************************************************************
578 * Memory management functions *
579 ****************************************************************************/
580static void* rvmalloc(unsigned long size)
581{
582 void* mem;
583 unsigned long adr;
584
585 size = PAGE_ALIGN(size);
586 mem = vmalloc_32(size);
587 if (!mem)
588 return NULL;
589
590 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
591 adr = (unsigned long) mem;
592 while (size > 0) {
593 SetPageReserved(vmalloc_to_page((void *)adr));
594 adr += PAGE_SIZE;
595 size -= PAGE_SIZE;
596 }
597
598 return mem;
599}
600
601
602static void rvfree(void* mem, unsigned long size)
603{
604 unsigned long adr;
605
606 if (!mem)
607 return;
608
609 adr = (unsigned long) mem;
610 while ((long) size > 0) {
611 ClearPageReserved(vmalloc_to_page((void *)adr));
612 adr += PAGE_SIZE;
613 size -= PAGE_SIZE;
614 }
615 vfree(mem);
616}
617
618
619/*--------------------------------------------------------------------------
620 Deallocate previously allocated memory.
621 --------------------------------------------------------------------------*/
622static void w9968cf_deallocate_memory(struct w9968cf_device* cam)
623{
624 u8 i;
625
626 /* Free the isochronous transfer buffers */
627 for (i = 0; i < W9968CF_URBS; i++) {
628 kfree(cam->transfer_buffer[i]);
629 cam->transfer_buffer[i] = NULL;
630 }
631
632 /* Free temporary frame buffer */
633 if (cam->frame_tmp.buffer) {
634 rvfree(cam->frame_tmp.buffer, cam->frame_tmp.size);
635 cam->frame_tmp.buffer = NULL;
636 }
637
638 /* Free helper buffer */
639 if (cam->frame_vpp.buffer) {
640 rvfree(cam->frame_vpp.buffer, cam->frame_vpp.size);
641 cam->frame_vpp.buffer = NULL;
642 }
643
644 /* Free video frame buffers */
645 if (cam->frame[0].buffer) {
646 rvfree(cam->frame[0].buffer, cam->nbuffers*cam->frame[0].size);
647 cam->frame[0].buffer = NULL;
648 }
649
650 cam->nbuffers = 0;
651
652 DBG(5, "Memory successfully deallocated")
653}
654
655
656/*--------------------------------------------------------------------------
657 Allocate memory buffers for USB transfers and video frames.
658 This function is called by open() only.
659 Return 0 on success, a negative number otherwise.
660 --------------------------------------------------------------------------*/
661static int w9968cf_allocate_memory(struct w9968cf_device* cam)
662{
663 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
664 void* buff = NULL;
665 unsigned long hw_bufsize, vpp_bufsize;
666 u8 i, bpp;
667
668 /* NOTE: Deallocation is done elsewhere in case of error */
669
670 /* Calculate the max amount of raw data per frame from the device */
671 hw_bufsize = cam->maxwidth*cam->maxheight*2;
672
673 /* Calculate the max buf. size needed for post-processing routines */
674 bpp = (w9968cf_vpp) ? 4 : 2;
675 if (cam->upscaling)
676 vpp_bufsize = max(W9968CF_MAX_WIDTH*W9968CF_MAX_HEIGHT*bpp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300677 cam->maxwidth*cam->maxheight*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 else
679 vpp_bufsize = cam->maxwidth*cam->maxheight*bpp;
680
681 /* Allocate memory for the isochronous transfer buffers */
682 for (i = 0; i < W9968CF_URBS; i++) {
683 if (!(cam->transfer_buffer[i] =
Oliver Neukumb10b4172006-01-06 21:28:40 +0100684 kzalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 DBG(1, "Couldn't allocate memory for the isochronous "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300686 "transfer buffers (%u bytes)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 p_size * W9968CF_ISO_PACKETS)
688 return -ENOMEM;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
692 /* Allocate memory for the temporary frame buffer */
693 if (!(cam->frame_tmp.buffer = rvmalloc(hw_bufsize))) {
694 DBG(1, "Couldn't allocate memory for the temporary "
695 "video frame buffer (%lu bytes)", hw_bufsize)
696 return -ENOMEM;
697 }
698 cam->frame_tmp.size = hw_bufsize;
699 cam->frame_tmp.number = -1;
700
701 /* Allocate memory for the helper buffer */
702 if (w9968cf_vpp) {
703 if (!(cam->frame_vpp.buffer = rvmalloc(vpp_bufsize))) {
704 DBG(1, "Couldn't allocate memory for the helper buffer"
705 " (%lu bytes)", vpp_bufsize)
706 return -ENOMEM;
707 }
708 cam->frame_vpp.size = vpp_bufsize;
709 } else
710 cam->frame_vpp.buffer = NULL;
711
712 /* Allocate memory for video frame buffers */
713 cam->nbuffers = cam->max_buffers;
714 while (cam->nbuffers >= 2) {
715 if ((buff = rvmalloc(cam->nbuffers * vpp_bufsize)))
716 break;
717 else
718 cam->nbuffers--;
719 }
720
721 if (!buff) {
722 DBG(1, "Couldn't allocate memory for the video frame buffers")
723 cam->nbuffers = 0;
724 return -ENOMEM;
725 }
726
727 if (cam->nbuffers != cam->max_buffers)
728 DBG(2, "Couldn't allocate memory for %u video frame buffers. "
729 "Only memory for %u buffers has been allocated",
730 cam->max_buffers, cam->nbuffers)
731
732 for (i = 0; i < cam->nbuffers; i++) {
733 cam->frame[i].buffer = buff + i*vpp_bufsize;
734 cam->frame[i].size = vpp_bufsize;
735 cam->frame[i].number = i;
736 /* Circular list */
737 if (i != cam->nbuffers-1)
738 cam->frame[i].next = &cam->frame[i+1];
739 else
740 cam->frame[i].next = &cam->frame[0];
741 cam->frame[i].status = F_UNUSED;
742 }
743
744 DBG(5, "Memory successfully allocated")
745 return 0;
746}
747
748
749
750/****************************************************************************
751 * USB-specific functions *
752 ****************************************************************************/
753
754/*--------------------------------------------------------------------------
755 This is an handler function which is called after the URBs are completed.
756 It collects multiple data packets coming from the camera by putting them
757 into frame buffers: one or more zero data length data packets are used to
758 mark the end of a video frame; the first non-zero data packet is the start
759 of the next video frame; if an error is encountered in a packet, the entire
760 video frame is discarded and grabbed again.
761 If there are no requested frames in the FIFO list, packets are collected into
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300762 a temporary buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 --------------------------------------------------------------------------*/
David Howells7d12e782006-10-05 14:55:46 +0100764static void w9968cf_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct w9968cf_device* cam = (struct w9968cf_device*)urb->context;
767 struct w9968cf_frame_t** f;
768 unsigned int len, status;
769 void* pos;
770 u8 i;
771 int err = 0;
772
773 if ((!cam->streaming) || cam->disconnected) {
774 DBG(4, "Got interrupt, but not streaming")
775 return;
776 }
777
778 /* "(*f)" will be used instead of "cam->frame_current" */
779 f = &cam->frame_current;
780
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300781 /* If a frame has been requested and we are grabbing into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 the temporary frame, we'll switch to that requested frame */
783 if ((*f) == &cam->frame_tmp && *cam->requested_frame) {
784 if (cam->frame_tmp.status == F_GRABBING) {
785 w9968cf_pop_frame(cam, &cam->frame_current);
786 (*f)->status = F_GRABBING;
787 (*f)->length = cam->frame_tmp.length;
788 memcpy((*f)->buffer, cam->frame_tmp.buffer,
789 (*f)->length);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300790 DBG(6, "Switched from temp. frame to frame #%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 (*f)->number)
792 }
793 }
794
795 for (i = 0; i < urb->number_of_packets; i++) {
796 len = urb->iso_frame_desc[i].actual_length;
797 status = urb->iso_frame_desc[i].status;
798 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
799
800 if (status && len != 0) {
801 DBG(4, "URB failed, error in data packet "
802 "(error #%u, %s)",
803 status, symbolic(urb_errlist, status))
804 (*f)->status = F_ERROR;
805 continue;
806 }
807
808 if (len) { /* start of frame */
809
810 if ((*f)->status == F_UNUSED) {
811 (*f)->status = F_GRABBING;
812 (*f)->length = 0;
813 }
814
815 /* Buffer overflows shouldn't happen, however...*/
816 if ((*f)->length + len > (*f)->size) {
817 DBG(4, "Buffer overflow: bad data packets")
818 (*f)->status = F_ERROR;
819 }
820
821 if ((*f)->status == F_GRABBING) {
822 memcpy((*f)->buffer + (*f)->length, pos, len);
823 (*f)->length += len;
824 }
825
826 } else if ((*f)->status == F_GRABBING) { /* end of frame */
827
828 DBG(6, "Frame #%d successfully grabbed", (*f)->number)
829
830 if (cam->vpp_flag & VPP_DECOMPRESSION) {
831 err = w9968cf_vpp->check_headers((*f)->buffer,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300832 (*f)->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (err) {
834 DBG(4, "Skip corrupted frame: %s",
835 symbolic(decoder_errlist, err))
836 (*f)->status = F_UNUSED;
837 continue; /* grab this frame again */
838 }
839 }
840
841 (*f)->status = F_READY;
842 (*f)->queued = 0;
843
844 /* Take a pointer to the new frame from the FIFO list.
845 If the list is empty,we'll use the temporary frame*/
846 if (*cam->requested_frame)
847 w9968cf_pop_frame(cam, &cam->frame_current);
848 else {
849 cam->frame_current = &cam->frame_tmp;
850 (*f)->status = F_UNUSED;
851 }
852
853 } else if ((*f)->status == F_ERROR)
854 (*f)->status = F_UNUSED; /* grab it again */
855
856 PDBGG("Frame length %lu | pack.#%u | pack.len. %u | state %d",
857 (unsigned long)(*f)->length, i, len, (*f)->status)
858
859 } /* end for */
860
861 /* Resubmit this URB */
862 urb->dev = cam->usbdev;
863 urb->status = 0;
864 spin_lock(&cam->urb_lock);
865 if (cam->streaming)
866 if ((err = usb_submit_urb(urb, GFP_ATOMIC))) {
867 cam->misconfigured = 1;
868 DBG(1, "Couldn't resubmit the URB: error %d, %s",
869 err, symbolic(urb_errlist, err))
870 }
871 spin_unlock(&cam->urb_lock);
872
873 /* Wake up the user process */
874 wake_up_interruptible(&cam->wait_queue);
875}
876
877
878/*---------------------------------------------------------------------------
879 Setup the URB structures for the isochronous transfer.
880 Submit the URBs so that the data transfer begins.
881 Return 0 on success, a negative number otherwise.
882 ---------------------------------------------------------------------------*/
883static int w9968cf_start_transfer(struct w9968cf_device* cam)
884{
885 struct usb_device *udev = cam->usbdev;
886 struct urb* urb;
887 const u16 p_size = wMaxPacketSize[cam->altsetting-1];
888 u16 w, h, d;
889 int vidcapt;
890 u32 t_size;
891 int err = 0;
892 s8 i, j;
893
894 for (i = 0; i < W9968CF_URBS; i++) {
895 urb = usb_alloc_urb(W9968CF_ISO_PACKETS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (!urb) {
897 for (j = 0; j < i; j++)
898 usb_free_urb(cam->urb[j]);
899 DBG(1, "Couldn't allocate the URB structures")
900 return -ENOMEM;
901 }
902
Douglas Schilling Landgrafff41efc2008-09-04 11:19:27 -0300903 cam->urb[i] = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 urb->dev = udev;
905 urb->context = (void*)cam;
906 urb->pipe = usb_rcvisocpipe(udev, 1);
907 urb->transfer_flags = URB_ISO_ASAP;
908 urb->number_of_packets = W9968CF_ISO_PACKETS;
909 urb->complete = w9968cf_urb_complete;
910 urb->transfer_buffer = cam->transfer_buffer[i];
911 urb->transfer_buffer_length = p_size*W9968CF_ISO_PACKETS;
912 urb->interval = 1;
913 for (j = 0; j < W9968CF_ISO_PACKETS; j++) {
914 urb->iso_frame_desc[j].offset = p_size*j;
915 urb->iso_frame_desc[j].length = p_size;
916 }
917 }
918
919 /* Transfer size per frame, in WORD ! */
920 d = cam->hw_depth;
921 w = cam->hw_width;
922 h = cam->hw_height;
923
924 t_size = (w*h*d)/16;
925
926 err = w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
927 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
928
929 /* Transfer size */
930 err += w9968cf_write_reg(cam, t_size & 0xffff, 0x3d); /* low bits */
931 err += w9968cf_write_reg(cam, t_size >> 16, 0x3e); /* high bits */
932
933 if (cam->vpp_flag & VPP_DECOMPRESSION)
934 err += w9968cf_upload_quantizationtables(cam);
935
936 vidcapt = w9968cf_read_reg(cam, 0x16); /* read picture settings */
937 err += w9968cf_write_reg(cam, vidcapt|0x8000, 0x16); /* capt. enable */
938
939 err += usb_set_interface(udev, 0, cam->altsetting);
940 err += w9968cf_write_reg(cam, 0x8a05, 0x3c); /* USB FIFO enable */
941
942 if (err || (vidcapt < 0)) {
943 for (i = 0; i < W9968CF_URBS; i++)
944 usb_free_urb(cam->urb[i]);
945 DBG(1, "Couldn't tell the camera to start the data transfer")
946 return err;
947 }
948
949 w9968cf_init_framelist(cam);
950
951 /* Begin to grab into the temporary buffer */
952 cam->frame_tmp.status = F_UNUSED;
953 cam->frame_tmp.queued = 0;
954 cam->frame_current = &cam->frame_tmp;
955
956 if (!(cam->vpp_flag & VPP_DECOMPRESSION))
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300957 DBG(5, "Isochronous transfer size: %lu bytes/frame",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 (unsigned long)t_size*2)
959
960 DBG(5, "Starting the isochronous transfer...")
961
962 cam->streaming = 1;
963
964 /* Submit the URBs */
965 for (i = 0; i < W9968CF_URBS; i++) {
966 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
967 if (err) {
968 cam->streaming = 0;
969 for (j = i-1; j >= 0; j--) {
970 usb_kill_urb(cam->urb[j]);
971 usb_free_urb(cam->urb[j]);
972 }
973 DBG(1, "Couldn't send a transfer request to the "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300974 "USB core (error #%d, %s)", err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 symbolic(urb_errlist, err))
976 return err;
977 }
978 }
979
980 return 0;
981}
982
983
984/*--------------------------------------------------------------------------
985 Stop the isochronous transfer and set alternate setting to 0 (0Mb/s).
986 Return 0 on success, a negative number otherwise.
987 --------------------------------------------------------------------------*/
988static int w9968cf_stop_transfer(struct w9968cf_device* cam)
989{
990 struct usb_device *udev = cam->usbdev;
991 unsigned long lock_flags;
992 int err = 0;
993 s8 i;
994
995 if (!cam->streaming)
996 return 0;
997
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300998 /* This avoids race conditions with usb_submit_urb()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 in the URB completition handler */
1000 spin_lock_irqsave(&cam->urb_lock, lock_flags);
1001 cam->streaming = 0;
1002 spin_unlock_irqrestore(&cam->urb_lock, lock_flags);
1003
1004 for (i = W9968CF_URBS-1; i >= 0; i--)
1005 if (cam->urb[i]) {
1006 usb_kill_urb(cam->urb[i]);
1007 usb_free_urb(cam->urb[i]);
1008 cam->urb[i] = NULL;
1009 }
1010
1011 if (cam->disconnected)
1012 goto exit;
1013
1014 err = w9968cf_write_reg(cam, 0x0a05, 0x3c); /* stop USB transfer */
1015 err += usb_set_interface(udev, 0, 0); /* 0 Mb/s */
1016 err += w9968cf_write_reg(cam, 0x0000, 0x39); /* disable JPEG encoder */
1017 err += w9968cf_write_reg(cam, 0x0000, 0x16); /* stop video capture */
1018
1019 if (err) {
1020 DBG(2, "Failed to tell the camera to stop the isochronous "
1021 "transfer. However this is not a critical error.")
1022 return -EIO;
1023 }
1024
1025exit:
1026 DBG(5, "Isochronous transfer stopped")
1027 return 0;
1028}
1029
1030
1031/*--------------------------------------------------------------------------
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001032 Write a W9968CF register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 Return 0 on success, -1 otherwise.
1034 --------------------------------------------------------------------------*/
1035static int w9968cf_write_reg(struct w9968cf_device* cam, u16 value, u16 index)
1036{
1037 struct usb_device* udev = cam->usbdev;
1038 int res;
1039
1040 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001041 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1042 value, index, NULL, 0, W9968CF_USB_CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (res < 0)
1045 DBG(4, "Failed to write a register "
1046 "(value 0x%04X, index 0x%02X, error #%d, %s)",
1047 value, index, res, symbolic(urb_errlist, res))
1048
1049 return (res >= 0) ? 0 : -1;
1050}
1051
1052
1053/*--------------------------------------------------------------------------
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001054 Read a W9968CF register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 Return the register value on success, -1 otherwise.
1056 --------------------------------------------------------------------------*/
1057static int w9968cf_read_reg(struct w9968cf_device* cam, u16 index)
1058{
1059 struct usb_device* udev = cam->usbdev;
1060 u16* buff = cam->control_buffer;
1061 int res;
1062
1063 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 1,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001064 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1065 0, index, buff, 2, W9968CF_USB_CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (res < 0)
1068 DBG(4, "Failed to read a register "
1069 "(index 0x%02X, error #%d, %s)",
1070 index, res, symbolic(urb_errlist, res))
1071
1072 return (res >= 0) ? (int)(*buff) : -1;
1073}
1074
1075
1076/*--------------------------------------------------------------------------
1077 Write 64-bit data to the fast serial bus registers.
1078 Return 0 on success, -1 otherwise.
1079 --------------------------------------------------------------------------*/
1080static int w9968cf_write_fsb(struct w9968cf_device* cam, u16* data)
1081{
1082 struct usb_device* udev = cam->usbdev;
1083 u16 value;
1084 int res;
1085
1086 value = *data++;
1087
1088 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001089 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
1090 value, 0x06, data, 6, W9968CF_USB_CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (res < 0)
1093 DBG(4, "Failed to write the FSB registers "
1094 "(error #%d, %s)", res, symbolic(urb_errlist, res))
1095
1096 return (res >= 0) ? 0 : -1;
1097}
1098
1099
1100/*--------------------------------------------------------------------------
1101 Write data to the serial bus control register.
1102 Return 0 on success, a negative number otherwise.
1103 --------------------------------------------------------------------------*/
1104static int w9968cf_write_sb(struct w9968cf_device* cam, u16 value)
1105{
1106 int err = 0;
1107
1108 err = w9968cf_write_reg(cam, value, 0x01);
1109 udelay(W9968CF_I2C_BUS_DELAY);
1110
1111 return err;
1112}
1113
1114
1115/*--------------------------------------------------------------------------
1116 Read data from the serial bus control register.
1117 Return 0 on success, a negative number otherwise.
1118 --------------------------------------------------------------------------*/
1119static int w9968cf_read_sb(struct w9968cf_device* cam)
1120{
1121 int v = 0;
1122
1123 v = w9968cf_read_reg(cam, 0x01);
1124 udelay(W9968CF_I2C_BUS_DELAY);
1125
1126 return v;
1127}
1128
1129
1130/*--------------------------------------------------------------------------
1131 Upload quantization tables for the JPEG compression.
1132 This function is called by w9968cf_start_transfer().
1133 Return 0 on success, a negative number otherwise.
1134 --------------------------------------------------------------------------*/
1135static int w9968cf_upload_quantizationtables(struct w9968cf_device* cam)
1136{
1137 u16 a, b;
1138 int err = 0, i, j;
1139
1140 err += w9968cf_write_reg(cam, 0x0010, 0x39); /* JPEG clock enable */
1141
1142 for (i = 0, j = 0; i < 32; i++, j += 2) {
1143 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j+1]) << 8);
1144 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j+1]) << 8);
1145 err += w9968cf_write_reg(cam, a, 0x40+i);
1146 err += w9968cf_write_reg(cam, b, 0x60+i);
1147 }
1148 err += w9968cf_write_reg(cam, 0x0012, 0x39); /* JPEG encoder enable */
1149
1150 return err;
1151}
1152
1153
1154
1155/****************************************************************************
1156 * Low-level I2C I/O functions. *
1157 * The adapter supports the following I2C transfer functions: *
1158 * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) *
1159 * i2c_adap_read_byte_data() *
1160 * i2c_adap_read_byte() *
1161 ****************************************************************************/
1162
1163static int w9968cf_smbus_start(struct w9968cf_device* cam)
1164{
1165 int err = 0;
1166
1167 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1168 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1169
1170 return err;
1171}
1172
1173
1174static int w9968cf_smbus_stop(struct w9968cf_device* cam)
1175{
1176 int err = 0;
1177
1178 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1179 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1180
1181 return err;
1182}
1183
1184
1185static int w9968cf_smbus_write_byte(struct w9968cf_device* cam, u8 v)
1186{
1187 u8 bit;
1188 int err = 0, sda;
1189
1190 for (bit = 0 ; bit < 8 ; bit++) {
1191 sda = (v & 0x80) ? 2 : 0;
1192 v <<= 1;
1193 /* SDE=1, SDA=sda, SCL=0 */
1194 err += w9968cf_write_sb(cam, 0x10 | sda);
1195 /* SDE=1, SDA=sda, SCL=1 */
1196 err += w9968cf_write_sb(cam, 0x11 | sda);
1197 /* SDE=1, SDA=sda, SCL=0 */
1198 err += w9968cf_write_sb(cam, 0x10 | sda);
1199 }
1200
1201 return err;
1202}
1203
1204
1205static int w9968cf_smbus_read_byte(struct w9968cf_device* cam, u8* v)
1206{
1207 u8 bit;
1208 int err = 0;
1209
1210 *v = 0;
1211 for (bit = 0 ; bit < 8 ; bit++) {
1212 *v <<= 1;
1213 err += w9968cf_write_sb(cam, 0x0013);
1214 *v |= (w9968cf_read_sb(cam) & 0x0008) ? 1 : 0;
1215 err += w9968cf_write_sb(cam, 0x0012);
1216 }
1217
1218 return err;
1219}
1220
1221
1222static int w9968cf_smbus_write_ack(struct w9968cf_device* cam)
1223{
1224 int err = 0;
1225
1226 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1227 err += w9968cf_write_sb(cam, 0x0011); /* SDE=1, SDA=0, SCL=1 */
1228 err += w9968cf_write_sb(cam, 0x0010); /* SDE=1, SDA=0, SCL=0 */
1229
1230 return err;
1231}
1232
1233
1234static int w9968cf_smbus_read_ack(struct w9968cf_device* cam)
1235{
1236 int err = 0, sda;
1237
1238 err += w9968cf_write_sb(cam, 0x0013); /* SDE=1, SDA=1, SCL=1 */
1239 sda = (w9968cf_read_sb(cam) & 0x08) ? 1 : 0; /* sda = SDA */
1240 err += w9968cf_write_sb(cam, 0x0012); /* SDE=1, SDA=1, SCL=0 */
1241 if (sda < 0)
1242 err += sda;
1243 if (sda == 1) {
1244 DBG(6, "Couldn't receive the ACK")
1245 err += -1;
1246 }
1247
1248 return err;
1249}
1250
1251
1252/* This seems to refresh the communication through the serial bus */
1253static int w9968cf_smbus_refresh_bus(struct w9968cf_device* cam)
1254{
1255 int err = 0, j;
1256
1257 for (j = 1; j <= 10; j++) {
1258 err = w9968cf_write_reg(cam, 0x0020, 0x01);
1259 err += w9968cf_write_reg(cam, 0x0000, 0x01);
1260 if (err)
1261 break;
1262 }
1263
1264 return err;
1265}
1266
1267
1268/* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001269static int
1270w9968cf_i2c_adap_fastwrite_byte_data(struct w9968cf_device* cam,
1271 u16 address, u8 subaddress,u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 u16* data = cam->data_buffer;
1274 int err = 0;
1275
1276 err += w9968cf_smbus_refresh_bus(cam);
1277
1278 /* Enable SBUS outputs */
1279 err += w9968cf_write_sb(cam, 0x0020);
1280
1281 data[0] = 0x082f | ((address & 0x80) ? 0x1500 : 0x0);
1282 data[0] |= (address & 0x40) ? 0x4000 : 0x0;
1283 data[1] = 0x2082 | ((address & 0x40) ? 0x0005 : 0x0);
1284 data[1] |= (address & 0x20) ? 0x0150 : 0x0;
1285 data[1] |= (address & 0x10) ? 0x5400 : 0x0;
1286 data[2] = 0x8208 | ((address & 0x08) ? 0x0015 : 0x0);
1287 data[2] |= (address & 0x04) ? 0x0540 : 0x0;
1288 data[2] |= (address & 0x02) ? 0x5000 : 0x0;
1289 data[3] = 0x1d20 | ((address & 0x02) ? 0x0001 : 0x0);
1290 data[3] |= (address & 0x01) ? 0x0054 : 0x0;
1291
1292 err += w9968cf_write_fsb(cam, data);
1293
1294 data[0] = 0x8208 | ((subaddress & 0x80) ? 0x0015 : 0x0);
1295 data[0] |= (subaddress & 0x40) ? 0x0540 : 0x0;
1296 data[0] |= (subaddress & 0x20) ? 0x5000 : 0x0;
1297 data[1] = 0x0820 | ((subaddress & 0x20) ? 0x0001 : 0x0);
1298 data[1] |= (subaddress & 0x10) ? 0x0054 : 0x0;
1299 data[1] |= (subaddress & 0x08) ? 0x1500 : 0x0;
1300 data[1] |= (subaddress & 0x04) ? 0x4000 : 0x0;
1301 data[2] = 0x2082 | ((subaddress & 0x04) ? 0x0005 : 0x0);
1302 data[2] |= (subaddress & 0x02) ? 0x0150 : 0x0;
1303 data[2] |= (subaddress & 0x01) ? 0x5400 : 0x0;
1304 data[3] = 0x001d;
1305
1306 err += w9968cf_write_fsb(cam, data);
1307
1308 data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
1309 data[0] |= (value & 0x40) ? 0x0540 : 0x0;
1310 data[0] |= (value & 0x20) ? 0x5000 : 0x0;
1311 data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
1312 data[1] |= (value & 0x10) ? 0x0054 : 0x0;
1313 data[1] |= (value & 0x08) ? 0x1500 : 0x0;
1314 data[1] |= (value & 0x04) ? 0x4000 : 0x0;
1315 data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
1316 data[2] |= (value & 0x02) ? 0x0150 : 0x0;
1317 data[2] |= (value & 0x01) ? 0x5400 : 0x0;
1318 data[3] = 0xfe1d;
1319
1320 err += w9968cf_write_fsb(cam, data);
1321
1322 /* Disable SBUS outputs */
1323 err += w9968cf_write_sb(cam, 0x0000);
1324
1325 if (!err)
1326 DBG(5, "I2C write byte data done, addr.0x%04X, subaddr.0x%02X "
1327 "value 0x%02X", address, subaddress, value)
1328 else
1329 DBG(5, "I2C write byte data failed, addr.0x%04X, "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001330 "subaddr.0x%02X, value 0x%02X",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 address, subaddress, value)
1332
1333 return err;
1334}
1335
1336
1337/* 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 -03001338static int
1339w9968cf_i2c_adap_read_byte_data(struct w9968cf_device* cam,
1340 u16 address, u8 subaddress,
1341 u8* value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 int err = 0;
1344
1345 /* Serial data enable */
1346 err += w9968cf_write_sb(cam, 0x0013); /* don't change ! */
1347
1348 err += w9968cf_smbus_start(cam);
1349 err += w9968cf_smbus_write_byte(cam, address);
1350 err += w9968cf_smbus_read_ack(cam);
1351 err += w9968cf_smbus_write_byte(cam, subaddress);
1352 err += w9968cf_smbus_read_ack(cam);
1353 err += w9968cf_smbus_stop(cam);
1354 err += w9968cf_smbus_start(cam);
1355 err += w9968cf_smbus_write_byte(cam, address + 1);
1356 err += w9968cf_smbus_read_ack(cam);
1357 err += w9968cf_smbus_read_byte(cam, value);
1358 err += w9968cf_smbus_write_ack(cam);
1359 err += w9968cf_smbus_stop(cam);
1360
1361 /* Serial data disable */
1362 err += w9968cf_write_sb(cam, 0x0000);
1363
1364 if (!err)
1365 DBG(5, "I2C read byte data done, addr.0x%04X, "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001366 "subaddr.0x%02X, value 0x%02X",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 address, subaddress, *value)
1368 else
1369 DBG(5, "I2C read byte data failed, addr.0x%04X, "
1370 "subaddr.0x%02X, wrong value 0x%02X",
1371 address, subaddress, *value)
1372
1373 return err;
1374}
1375
1376
1377/* SMBus protocol: S Addr+1 Rd [A] [Value] NA P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001378static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379w9968cf_i2c_adap_read_byte(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001380 u16 address, u8* value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 int err = 0;
1383
1384 /* Serial data enable */
1385 err += w9968cf_write_sb(cam, 0x0013);
1386
1387 err += w9968cf_smbus_start(cam);
1388 err += w9968cf_smbus_write_byte(cam, address + 1);
1389 err += w9968cf_smbus_read_ack(cam);
1390 err += w9968cf_smbus_read_byte(cam, value);
1391 err += w9968cf_smbus_write_ack(cam);
1392 err += w9968cf_smbus_stop(cam);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* Serial data disable */
1395 err += w9968cf_write_sb(cam, 0x0000);
1396
1397 if (!err)
1398 DBG(5, "I2C read byte done, addr.0x%04X, "
1399 "value 0x%02X", address, *value)
1400 else
1401 DBG(5, "I2C read byte failed, addr.0x%04X, "
1402 "wrong value 0x%02X", address, *value)
1403
1404 return err;
1405}
1406
1407
1408/* SMBus protocol: S Addr Wr [A] Value [A] P */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001409static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410w9968cf_i2c_adap_write_byte(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001411 u16 address, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 DBG(4, "i2c_write_byte() is an unsupported transfer mode")
1414 return -EINVAL;
1415}
1416
1417
1418
1419/****************************************************************************
1420 * I2C interface to kernel *
1421 ****************************************************************************/
1422
1423static int
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001424w9968cf_i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
1425 unsigned short flags, char read_write, u8 command,
1426 int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Hans Verkuil3160fbc2009-03-08 10:19:44 -03001428 struct v4l2_device *v4l2_dev = i2c_get_adapdata(adapter);
1429 struct w9968cf_device *cam = to_cam(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 u8 i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001431 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (size == I2C_SMBUS_BYTE) {
1434 /* Why addr <<= 1? See OVXXX0_SID defines in ovcamchip.h */
1435 addr <<= 1;
1436
1437 if (read_write == I2C_SMBUS_WRITE)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001438 err = w9968cf_i2c_adap_write_byte(cam, addr, command);
1439 else if (read_write == I2C_SMBUS_READ)
Hans Verkuil3160fbc2009-03-08 10:19:44 -03001440 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1441 err = w9968cf_i2c_adap_read_byte(cam, addr,
1442 &data->byte);
1443 if (err) {
1444 if (w9968cf_smbus_refresh_bus(cam)) {
1445 err = -EIO;
1446 break;
1447 }
1448 } else
1449 break;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 } else if (size == I2C_SMBUS_BYTE_DATA) {
1452 addr <<= 1;
1453
1454 if (read_write == I2C_SMBUS_WRITE)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001455 err = w9968cf_i2c_adap_fastwrite_byte_data(cam, addr,
1456 command, data->byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 else if (read_write == I2C_SMBUS_READ) {
1458 for (i = 1; i <= W9968CF_I2C_RW_RETRIES; i++) {
1459 err = w9968cf_i2c_adap_read_byte_data(cam,addr,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001460 command, &data->byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (err) {
1462 if (w9968cf_smbus_refresh_bus(cam)) {
1463 err = -EIO;
1464 break;
1465 }
1466 } else
1467 break;
1468 }
1469
1470 } else
1471 return -EINVAL;
1472
1473 } else {
1474 DBG(4, "Unsupported I2C transfer mode (%d)", size)
1475 return -EINVAL;
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return err;
1478}
1479
1480
1481static u32 w9968cf_i2c_func(struct i2c_adapter* adap)
1482{
1483 return I2C_FUNC_SMBUS_READ_BYTE |
1484 I2C_FUNC_SMBUS_READ_BYTE_DATA |
1485 I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
1486}
1487
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489static int w9968cf_i2c_init(struct w9968cf_device* cam)
1490{
1491 int err = 0;
1492
1493 static struct i2c_algorithm algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 .smbus_xfer = w9968cf_i2c_smbus_xfer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .functionality = w9968cf_i2c_func,
1496 };
1497
1498 static struct i2c_adapter adap = {
Jean Delvarec7a46532005-08-11 23:41:56 +02001499 .id = I2C_HW_SMBUS_W9968CF,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 .algo = &algo,
1502 };
1503
1504 memcpy(&cam->i2c_adapter, &adap, sizeof(struct i2c_adapter));
1505 strcpy(cam->i2c_adapter.name, "w9968cf");
Jean Delvare12a917f2007-02-13 22:09:03 +01001506 cam->i2c_adapter.dev.parent = &cam->usbdev->dev;
Hans Verkuil3160fbc2009-03-08 10:19:44 -03001507 i2c_set_adapdata(&cam->i2c_adapter, &cam->v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 DBG(6, "Registering I2C adapter with kernel...")
1510
1511 err = i2c_add_adapter(&cam->i2c_adapter);
1512 if (err)
1513 DBG(1, "Failed to register the I2C adapter")
1514 else
1515 DBG(5, "I2C adapter registered")
1516
1517 return err;
1518}
1519
1520
1521
1522/****************************************************************************
1523 * Helper functions *
1524 ****************************************************************************/
1525
1526/*--------------------------------------------------------------------------
1527 Turn on the LED on some webcams. A beep should be heard too.
1528 Return 0 on success, a negative number otherwise.
1529 --------------------------------------------------------------------------*/
1530static int w9968cf_turn_on_led(struct w9968cf_device* cam)
1531{
1532 int err = 0;
1533
1534 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power-down */
1535 err += w9968cf_write_reg(cam, 0xbf17, 0x00); /* reset everything */
1536 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* normal operation */
1537 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* serial bus, SDS high */
1538 err += w9968cf_write_reg(cam, 0x0000, 0x01); /* serial bus, SDS low */
1539 err += w9968cf_write_reg(cam, 0x0010, 0x01); /* ..high 'beep-beep' */
1540
1541 if (err)
1542 DBG(2, "Couldn't turn on the LED")
1543
1544 DBG(5, "LED turned on")
1545
1546 return err;
1547}
1548
1549
1550/*--------------------------------------------------------------------------
1551 Write some registers for the device initialization.
1552 This function is called once on open().
1553 Return 0 on success, a negative number otherwise.
1554 --------------------------------------------------------------------------*/
1555static int w9968cf_init_chip(struct w9968cf_device* cam)
1556{
1557 unsigned long hw_bufsize = cam->maxwidth*cam->maxheight*2,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001558 y0 = 0x0000,
1559 u0 = y0 + hw_bufsize/2,
1560 v0 = u0 + hw_bufsize/4,
1561 y1 = v0 + hw_bufsize/4,
1562 u1 = y1 + hw_bufsize/2,
1563 v1 = u1 + hw_bufsize/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int err = 0;
1565
1566 err += w9968cf_write_reg(cam, 0xff00, 0x00); /* power off */
1567 err += w9968cf_write_reg(cam, 0xbf10, 0x00); /* power on */
1568
1569 err += w9968cf_write_reg(cam, 0x405d, 0x03); /* DRAM timings */
1570 err += w9968cf_write_reg(cam, 0x0030, 0x04); /* SDRAM timings */
1571
1572 err += w9968cf_write_reg(cam, y0 & 0xffff, 0x20); /* Y buf.0, low */
1573 err += w9968cf_write_reg(cam, y0 >> 16, 0x21); /* Y buf.0, high */
1574 err += w9968cf_write_reg(cam, u0 & 0xffff, 0x24); /* U buf.0, low */
1575 err += w9968cf_write_reg(cam, u0 >> 16, 0x25); /* U buf.0, high */
1576 err += w9968cf_write_reg(cam, v0 & 0xffff, 0x28); /* V buf.0, low */
1577 err += w9968cf_write_reg(cam, v0 >> 16, 0x29); /* V buf.0, high */
1578
1579 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x22); /* Y buf.1, low */
1580 err += w9968cf_write_reg(cam, y1 >> 16, 0x23); /* Y buf.1, high */
1581 err += w9968cf_write_reg(cam, u1 & 0xffff, 0x26); /* U buf.1, low */
1582 err += w9968cf_write_reg(cam, u1 >> 16, 0x27); /* U buf.1, high */
1583 err += w9968cf_write_reg(cam, v1 & 0xffff, 0x2a); /* V buf.1, low */
1584 err += w9968cf_write_reg(cam, v1 >> 16, 0x2b); /* V buf.1, high */
1585
1586 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x32); /* JPEG buf 0 low */
1587 err += w9968cf_write_reg(cam, y1 >> 16, 0x33); /* JPEG buf 0 high */
1588
1589 err += w9968cf_write_reg(cam, y1 & 0xffff, 0x34); /* JPEG buf 1 low */
1590 err += w9968cf_write_reg(cam, y1 >> 16, 0x35); /* JPEG bug 1 high */
1591
1592 err += w9968cf_write_reg(cam, 0x0000, 0x36);/* JPEG restart interval */
1593 err += w9968cf_write_reg(cam, 0x0804, 0x37);/*JPEG VLE FIFO threshold*/
1594 err += w9968cf_write_reg(cam, 0x0000, 0x38);/* disable hw up-scaling */
1595 err += w9968cf_write_reg(cam, 0x0000, 0x3f); /* JPEG/MCTL test data */
1596
1597 err += w9968cf_set_picture(cam, cam->picture); /* this before */
1598 err += w9968cf_set_window(cam, cam->window);
1599
1600 if (err)
1601 DBG(1, "Chip initialization failed")
1602 else
1603 DBG(5, "Chip successfully initialized")
1604
1605 return err;
1606}
1607
1608
1609/*--------------------------------------------------------------------------
1610 Return non-zero if the palette is supported, 0 otherwise.
1611 --------------------------------------------------------------------------*/
1612static inline u16 w9968cf_valid_palette(u16 palette)
1613{
1614 u8 i = 0;
1615 while (w9968cf_formatlist[i].palette != 0) {
1616 if (palette == w9968cf_formatlist[i].palette)
1617 return palette;
1618 i++;
1619 }
1620 return 0;
1621}
1622
1623
1624/*--------------------------------------------------------------------------
1625 Return the depth corresponding to the given palette.
1626 Palette _must_ be supported !
1627 --------------------------------------------------------------------------*/
1628static inline u16 w9968cf_valid_depth(u16 palette)
1629{
1630 u8 i=0;
1631 while (w9968cf_formatlist[i].palette != palette)
1632 i++;
1633
1634 return w9968cf_formatlist[i].depth;
1635}
1636
1637
1638/*--------------------------------------------------------------------------
1639 Return non-zero if the format requires decompression, 0 otherwise.
1640 --------------------------------------------------------------------------*/
1641static inline u8 w9968cf_need_decompression(u16 palette)
1642{
1643 u8 i = 0;
1644 while (w9968cf_formatlist[i].palette != 0) {
1645 if (palette == w9968cf_formatlist[i].palette)
1646 return w9968cf_formatlist[i].compression;
1647 i++;
1648 }
1649 return 0;
1650}
1651
1652
1653/*--------------------------------------------------------------------------
1654 Change the picture settings of the camera.
1655 Return 0 on success, a negative number otherwise.
1656 --------------------------------------------------------------------------*/
1657static int
1658w9968cf_set_picture(struct w9968cf_device* cam, struct video_picture pict)
1659{
1660 u16 fmt, hw_depth, hw_palette, reg_v = 0x0000;
1661 int err = 0;
1662
1663 /* Make sure we are using a valid depth */
1664 pict.depth = w9968cf_valid_depth(pict.palette);
1665
1666 fmt = pict.palette;
1667
1668 hw_depth = pict.depth; /* depth used by the winbond chip */
1669 hw_palette = pict.palette; /* palette used by the winbond chip */
1670
1671 /* VS & HS polarities */
1672 reg_v = (cam->vs_polarity << 12) | (cam->hs_polarity << 11);
1673
1674 switch (fmt)
1675 {
1676 case VIDEO_PALETTE_UYVY:
1677 reg_v |= 0x0000;
1678 cam->vpp_flag = VPP_NONE;
1679 break;
1680 case VIDEO_PALETTE_YUV422P:
1681 reg_v |= 0x0002;
1682 cam->vpp_flag = VPP_DECOMPRESSION;
1683 break;
1684 case VIDEO_PALETTE_YUV420:
1685 case VIDEO_PALETTE_YUV420P:
1686 reg_v |= 0x0003;
1687 cam->vpp_flag = VPP_DECOMPRESSION;
1688 break;
1689 case VIDEO_PALETTE_YUYV:
1690 case VIDEO_PALETTE_YUV422:
1691 reg_v |= 0x0000;
1692 cam->vpp_flag = VPP_SWAP_YUV_BYTES;
1693 hw_palette = VIDEO_PALETTE_UYVY;
1694 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001695 /* Original video is used instead of RGBX palettes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 Software conversion later. */
1697 case VIDEO_PALETTE_GREY:
1698 case VIDEO_PALETTE_RGB555:
1699 case VIDEO_PALETTE_RGB565:
1700 case VIDEO_PALETTE_RGB24:
1701 case VIDEO_PALETTE_RGB32:
1702 reg_v |= 0x0000; /* UYVY 16 bit is used */
1703 hw_depth = 16;
1704 hw_palette = VIDEO_PALETTE_UYVY;
1705 cam->vpp_flag = VPP_UYVY_TO_RGBX;
1706 break;
1707 }
1708
1709 /* NOTE: due to memory issues, it is better to disable the hardware
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001710 double buffering during compression */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (cam->double_buffer && !(cam->vpp_flag & VPP_DECOMPRESSION))
1712 reg_v |= 0x0080;
1713
1714 if (cam->clamping)
1715 reg_v |= 0x0020;
1716
1717 if (cam->filter_type == 1)
1718 reg_v |= 0x0008;
1719 else if (cam->filter_type == 2)
1720 reg_v |= 0x000c;
1721
1722 if ((err = w9968cf_write_reg(cam, reg_v, 0x16)))
1723 goto error;
1724
1725 if ((err = w9968cf_sensor_update_picture(cam, pict)))
1726 goto error;
1727
1728 /* If all went well, update the device data structure */
1729 memcpy(&cam->picture, &pict, sizeof(pict));
1730 cam->hw_depth = hw_depth;
1731 cam->hw_palette = hw_palette;
1732
1733 /* Settings changed, so we clear the frame buffers */
1734 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1735
1736 DBG(4, "Palette is %s, depth is %u bpp",
1737 symbolic(v4l1_plist, pict.palette), pict.depth)
1738
1739 return 0;
1740
1741error:
1742 DBG(1, "Failed to change picture settings")
1743 return err;
1744}
1745
1746
1747/*--------------------------------------------------------------------------
1748 Change the capture area size of the camera.
1749 This function _must_ be called _after_ w9968cf_set_picture().
1750 Return 0 on success, a negative number otherwise.
1751 --------------------------------------------------------------------------*/
1752static int
1753w9968cf_set_window(struct w9968cf_device* cam, struct video_window win)
1754{
1755 u16 x, y, w, h, scx, scy, cw, ch, ax, ay;
1756 unsigned long fw, fh;
1757 struct ovcamchip_window s_win;
1758 int err = 0;
1759
1760 /* Work around to avoid FP arithmetics */
Ralf Baechlebee8a442006-12-11 11:14:54 -03001761 #define SC(x) ((x) << 10)
1762 #define UNSC(x) ((x) >> 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 /* Make sure we are using a supported resolution */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001765 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
1766 (u16*)&win.height)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto error;
1768
1769 /* Scaling factors */
Ralf Baechlebee8a442006-12-11 11:14:54 -03001770 fw = SC(win.width) / cam->maxwidth;
1771 fh = SC(win.height) / cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 /* Set up the width and height values used by the chip */
1774 if ((win.width > cam->maxwidth) || (win.height > cam->maxheight)) {
1775 cam->vpp_flag |= VPP_UPSCALE;
1776 /* Calculate largest w,h mantaining the same w/h ratio */
Ralf Baechlebee8a442006-12-11 11:14:54 -03001777 w = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1778 h = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (w < cam->minwidth) /* just in case */
1780 w = cam->minwidth;
1781 if (h < cam->minheight) /* just in case */
1782 h = cam->minheight;
1783 } else {
1784 cam->vpp_flag &= ~VPP_UPSCALE;
1785 w = win.width;
1786 h = win.height;
1787 }
1788
1789 /* x,y offsets of the cropped area */
1790 scx = cam->start_cropx;
1791 scy = cam->start_cropy;
1792
1793 /* Calculate cropped area manteining the right w/h ratio */
1794 if (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE)) {
Ralf Baechlebee8a442006-12-11 11:14:54 -03001795 cw = (fw >= fh) ? cam->maxwidth : SC(win.width)/fh;
1796 ch = (fw >= fh) ? SC(win.height)/fw : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 } else {
1798 cw = w;
1799 ch = h;
1800 }
1801
1802 /* Setup the window of the sensor */
1803 s_win.format = VIDEO_PALETTE_UYVY;
1804 s_win.width = cam->maxwidth;
1805 s_win.height = cam->maxheight;
1806 s_win.quarter = 0; /* full progressive video */
1807
1808 /* Center it */
1809 s_win.x = (s_win.width - cw) / 2;
1810 s_win.y = (s_win.height - ch) / 2;
1811
1812 /* Clock divisor */
1813 if (cam->clockdiv >= 0)
1814 s_win.clockdiv = cam->clockdiv; /* manual override */
1815 else
1816 switch (cam->sensor) {
1817 case CC_OV6620:
1818 s_win.clockdiv = 0;
1819 break;
1820 case CC_OV6630:
1821 s_win.clockdiv = 0;
1822 break;
1823 case CC_OV76BE:
1824 case CC_OV7610:
1825 case CC_OV7620:
1826 s_win.clockdiv = 0;
1827 break;
1828 default:
1829 s_win.clockdiv = W9968CF_DEF_CLOCKDIVISOR;
1830 }
1831
1832 /* We have to scale win.x and win.y offsets */
1833 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1834 || (cam->vpp_flag & VPP_UPSCALE) ) {
Ralf Baechlebee8a442006-12-11 11:14:54 -03001835 ax = SC(win.x)/fw;
1836 ay = SC(win.y)/fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 } else {
1838 ax = win.x;
1839 ay = win.y;
1840 }
1841
1842 if ((ax + cw) > cam->maxwidth)
1843 ax = cam->maxwidth - cw;
1844
1845 if ((ay + ch) > cam->maxheight)
1846 ay = cam->maxheight - ch;
1847
1848 /* Adjust win.x, win.y */
1849 if ( (cam->largeview && !(cam->vpp_flag & VPP_UPSCALE))
1850 || (cam->vpp_flag & VPP_UPSCALE) ) {
Ralf Baechlebee8a442006-12-11 11:14:54 -03001851 win.x = UNSC(ax*fw);
1852 win.y = UNSC(ay*fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 } else {
1854 win.x = ax;
1855 win.y = ay;
1856 }
1857
1858 /* Offsets used by the chip */
1859 x = ax + s_win.x;
1860 y = ay + s_win.y;
1861
1862 /* Go ! */
1863 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_MODE, &s_win)))
1864 goto error;
1865
1866 err += w9968cf_write_reg(cam, scx + x, 0x10);
1867 err += w9968cf_write_reg(cam, scy + y, 0x11);
1868 err += w9968cf_write_reg(cam, scx + x + cw, 0x12);
1869 err += w9968cf_write_reg(cam, scy + y + ch, 0x13);
1870 err += w9968cf_write_reg(cam, w, 0x14);
1871 err += w9968cf_write_reg(cam, h, 0x15);
1872
1873 /* JPEG width & height */
1874 err += w9968cf_write_reg(cam, w, 0x30);
1875 err += w9968cf_write_reg(cam, h, 0x31);
1876
1877 /* Y & UV frame buffer strides (in WORD) */
1878 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1879 err += w9968cf_write_reg(cam, w/2, 0x2c);
1880 err += w9968cf_write_reg(cam, w/4, 0x2d);
1881 } else
1882 err += w9968cf_write_reg(cam, w, 0x2c);
1883
1884 if (err)
1885 goto error;
1886
1887 /* If all went well, update the device data structure */
1888 memcpy(&cam->window, &win, sizeof(win));
1889 cam->hw_width = w;
1890 cam->hw_height = h;
1891
1892 /* Settings changed, so we clear the frame buffers */
1893 memset(cam->frame[0].buffer, 0, cam->nbuffers*cam->frame[0].size);
1894
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001895 DBG(4, "The capture area is %dx%d, Offset (x,y)=(%u,%u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 win.width, win.height, win.x, win.y)
1897
1898 PDBGG("x=%u ,y=%u, w=%u, h=%u, ax=%u, ay=%u, s_win.x=%u, s_win.y=%u, "
1899 "cw=%u, ch=%u, win.x=%u, win.y=%u, win.width=%u, win.height=%u",
1900 x, y, w, h, ax, ay, s_win.x, s_win.y, cw, ch, win.x, win.y,
1901 win.width, win.height)
1902
1903 return 0;
1904
1905error:
1906 DBG(1, "Failed to change the capture area size")
1907 return err;
1908}
1909
1910
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001911/*--------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 Adjust the asked values for window width and height.
1913 Return 0 on success, -1 otherwise.
1914 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001915static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916w9968cf_adjust_window_size(struct w9968cf_device* cam, u16* width, u16* height)
1917{
1918 u16 maxw, maxh;
1919
1920 if ((*width < cam->minwidth) || (*height < cam->minheight))
1921 return -ERANGE;
1922
1923 maxw = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1924 w9968cf_vpp ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001925 : cam->maxwidth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 maxh = cam->upscaling && !(cam->vpp_flag & VPP_DECOMPRESSION) &&
1927 w9968cf_vpp ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001928 : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 if (*width > maxw)
1931 *width = maxw;
1932 if (*height > maxh)
1933 *height = maxh;
1934
1935 if (cam->vpp_flag & VPP_DECOMPRESSION) {
1936 *width &= ~15L; /* multiple of 16 */
1937 *height &= ~15L;
1938 }
1939
1940 PDBGG("Window size adjusted w=%u, h=%u ", *width, *height)
1941
1942 return 0;
1943}
1944
1945
1946/*--------------------------------------------------------------------------
1947 Initialize the FIFO list of requested frames.
1948 --------------------------------------------------------------------------*/
1949static void w9968cf_init_framelist(struct w9968cf_device* cam)
1950{
1951 u8 i;
1952
1953 for (i = 0; i < cam->nbuffers; i++) {
1954 cam->requested_frame[i] = NULL;
1955 cam->frame[i].queued = 0;
1956 cam->frame[i].status = F_UNUSED;
1957 }
1958}
1959
1960
1961/*--------------------------------------------------------------------------
1962 Add a frame in the FIFO list of requested frames.
1963 This function is called in process context.
1964 --------------------------------------------------------------------------*/
1965static void w9968cf_push_frame(struct w9968cf_device* cam, u8 f_num)
1966{
1967 u8 f;
1968 unsigned long lock_flags;
1969
1970 spin_lock_irqsave(&cam->flist_lock, lock_flags);
1971
1972 for (f=0; cam->requested_frame[f] != NULL; f++);
1973 cam->requested_frame[f] = &cam->frame[f_num];
1974 cam->frame[f_num].queued = 1;
1975 cam->frame[f_num].status = F_UNUSED; /* clear the status */
1976
1977 spin_unlock_irqrestore(&cam->flist_lock, lock_flags);
1978
1979 DBG(6, "Frame #%u pushed into the FIFO list. Position %u", f_num, f)
1980}
1981
1982
1983/*--------------------------------------------------------------------------
1984 Read, store and remove the first pointer in the FIFO list of requested
1985 frames. This function is called in interrupt context.
1986 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001987static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988w9968cf_pop_frame(struct w9968cf_device* cam, struct w9968cf_frame_t** framep)
1989{
1990 u8 i;
1991
1992 spin_lock(&cam->flist_lock);
1993
1994 *framep = cam->requested_frame[0];
1995
1996 /* Shift the list of pointers */
1997 for (i = 0; i < cam->nbuffers-1; i++)
1998 cam->requested_frame[i] = cam->requested_frame[i+1];
1999 cam->requested_frame[i] = NULL;
2000
2001 spin_unlock(&cam->flist_lock);
2002
2003 DBG(6,"Popped frame #%d from the list", (*framep)->number)
2004}
2005
2006
2007/*--------------------------------------------------------------------------
2008 High-level video post-processing routine on grabbed frames.
2009 Return 0 on success, a negative number otherwise.
2010 --------------------------------------------------------------------------*/
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002011static int
2012w9968cf_postprocess_frame(struct w9968cf_device* cam,
2013 struct w9968cf_frame_t* fr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 void *pIn = fr->buffer, *pOut = cam->frame_vpp.buffer, *tmp;
2016 u16 w = cam->window.width,
2017 h = cam->window.height,
2018 d = cam->picture.depth,
2019 fmt = cam->picture.palette,
2020 rgb = cam->force_rgb,
2021 hw_w = cam->hw_width,
2022 hw_h = cam->hw_height,
2023 hw_d = cam->hw_depth;
2024 int err = 0;
2025
2026 #define _PSWAP(pIn, pOut) {tmp = (pIn); (pIn) = (pOut); (pOut) = tmp;}
2027
2028 if (cam->vpp_flag & VPP_DECOMPRESSION) {
2029 memcpy(pOut, pIn, fr->length);
2030 _PSWAP(pIn, pOut)
2031 err = w9968cf_vpp->decode(pIn, fr->length, hw_w, hw_h, pOut);
2032 PDBGG("Compressed frame length: %lu",(unsigned long)fr->length)
2033 fr->length = (hw_w*hw_h*hw_d)/8;
2034 _PSWAP(pIn, pOut)
2035 if (err) {
2036 DBG(4, "An error occurred while decoding the frame: "
2037 "%s", symbolic(decoder_errlist, err))
2038 return err;
2039 } else
2040 DBG(6, "Frame decoded")
2041 }
2042
2043 if (cam->vpp_flag & VPP_SWAP_YUV_BYTES) {
2044 w9968cf_vpp->swap_yuvbytes(pIn, fr->length);
2045 DBG(6, "Original UYVY component ordering changed")
2046 }
2047
2048 if (cam->vpp_flag & VPP_UPSCALE) {
2049 w9968cf_vpp->scale_up(pIn, pOut, hw_w, hw_h, hw_d, w, h);
2050 fr->length = (w*h*hw_d)/8;
2051 _PSWAP(pIn, pOut)
2052 DBG(6, "Vertical up-scaling done: %u,%u,%ubpp->%u,%u",
2053 hw_w, hw_h, hw_d, w, h)
2054 }
2055
2056 if (cam->vpp_flag & VPP_UYVY_TO_RGBX) {
2057 w9968cf_vpp->uyvy_to_rgbx(pIn, fr->length, pOut, fmt, rgb);
2058 fr->length = (w*h*d)/8;
2059 _PSWAP(pIn, pOut)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002060 DBG(6, "UYVY-16bit to %s conversion done",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 symbolic(v4l1_plist, fmt))
2062 }
2063
2064 if (pOut == fr->buffer)
2065 memcpy(fr->buffer, cam->frame_vpp.buffer, fr->length);
2066
2067 return 0;
2068}
2069
2070
2071
2072/****************************************************************************
2073 * Image sensor control routines *
2074 ****************************************************************************/
2075
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002076static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077w9968cf_sensor_set_control(struct w9968cf_device* cam, int cid, int val)
2078{
2079 struct ovcamchip_control ctl;
2080 int err;
2081
2082 ctl.id = cid;
2083 ctl.value = val;
2084
2085 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_S_CTRL, &ctl);
2086
2087 return err;
2088}
2089
2090
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002091static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092w9968cf_sensor_get_control(struct w9968cf_device* cam, int cid, int* val)
2093{
2094 struct ovcamchip_control ctl;
2095 int err;
2096
2097 ctl.id = cid;
2098
2099 err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_G_CTRL, &ctl);
2100 if (!err)
2101 *val = ctl.value;
2102
2103 return err;
2104}
2105
2106
2107static int
2108w9968cf_sensor_cmd(struct w9968cf_device* cam, unsigned int cmd, void* arg)
2109{
Hans Verkuil3160fbc2009-03-08 10:19:44 -03002110 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Hans Verkuil3160fbc2009-03-08 10:19:44 -03002112 rc = v4l2_subdev_call(cam->sensor_sd, core, ioctl, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 /* The I2C driver returns -EPERM on non-supported controls */
2114 return (rc < 0 && rc != -EPERM) ? rc : 0;
2115}
2116
2117
2118/*--------------------------------------------------------------------------
2119 Update some settings of the image sensor.
2120 Returns: 0 on success, a negative number otherwise.
2121 --------------------------------------------------------------------------*/
2122static int w9968cf_sensor_update_settings(struct w9968cf_device* cam)
2123{
2124 int err = 0;
2125
2126 /* Auto brightness */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002127 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOBRIGHT,
2128 cam->auto_brt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (err)
2130 return err;
2131
2132 /* Auto exposure */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002133 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_AUTOEXP,
2134 cam->auto_exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (err)
2136 return err;
2137
2138 /* Banding filter */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002139 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BANDFILT,
2140 cam->bandfilt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (err)
2142 return err;
2143
2144 /* Light frequency */
2145 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_FREQ,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002146 cam->lightfreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (err)
2148 return err;
2149
2150 /* Back light */
2151 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BACKLIGHT,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002152 cam->backlight);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (err)
2154 return err;
2155
2156 /* Mirror */
2157 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_MIRROR,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002158 cam->mirror);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (err)
2160 return err;
2161
2162 return 0;
2163}
2164
2165
2166/*--------------------------------------------------------------------------
2167 Get some current picture settings from the image sensor and update the
2168 internal 'picture' structure of the camera.
2169 Returns: 0 on success, a negative number otherwise.
2170 --------------------------------------------------------------------------*/
2171static int w9968cf_sensor_get_picture(struct w9968cf_device* cam)
2172{
2173 int err, v;
2174
2175 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_CONT, &v);
2176 if (err)
2177 return err;
2178 cam->picture.contrast = v;
2179
2180 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_BRIGHT, &v);
2181 if (err)
2182 return err;
2183 cam->picture.brightness = v;
2184
2185 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_SAT, &v);
2186 if (err)
2187 return err;
2188 cam->picture.colour = v;
2189
2190 err = w9968cf_sensor_get_control(cam, OVCAMCHIP_CID_HUE, &v);
2191 if (err)
2192 return err;
2193 cam->picture.hue = v;
2194
2195 DBG(5, "Got picture settings from the image sensor")
2196
2197 PDBGG("Brightness, contrast, hue, colour, whiteness are "
2198 "%u,%u,%u,%u,%u", cam->picture.brightness,cam->picture.contrast,
2199 cam->picture.hue, cam->picture.colour, cam->picture.whiteness)
2200
2201 return 0;
2202}
2203
2204
2205/*--------------------------------------------------------------------------
2206 Update picture settings of the image sensor.
2207 Returns: 0 on success, a negative number otherwise.
2208 --------------------------------------------------------------------------*/
2209static int
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002210w9968cf_sensor_update_picture(struct w9968cf_device* cam,
2211 struct video_picture pict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 int err = 0;
2214
2215 if ((!cam->sensor_initialized)
2216 || pict.contrast != cam->picture.contrast) {
2217 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_CONT,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002218 pict.contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (err)
2220 goto fail;
2221 DBG(4, "Contrast changed from %u to %u",
2222 cam->picture.contrast, pict.contrast)
2223 cam->picture.contrast = pict.contrast;
2224 }
2225
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002226 if (((!cam->sensor_initialized) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 pict.brightness != cam->picture.brightness) && (!cam->auto_brt)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002228 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_BRIGHT,
2229 pict.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (err)
2231 goto fail;
2232 DBG(4, "Brightness changed from %u to %u",
2233 cam->picture.brightness, pict.brightness)
2234 cam->picture.brightness = pict.brightness;
2235 }
2236
2237 if ((!cam->sensor_initialized) || pict.colour != cam->picture.colour) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002238 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_SAT,
2239 pict.colour);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (err)
2241 goto fail;
2242 DBG(4, "Colour changed from %u to %u",
2243 cam->picture.colour, pict.colour)
2244 cam->picture.colour = pict.colour;
2245 }
2246
2247 if ((!cam->sensor_initialized) || pict.hue != cam->picture.hue) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002248 err = w9968cf_sensor_set_control(cam, OVCAMCHIP_CID_HUE,
2249 pict.hue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (err)
2251 goto fail;
2252 DBG(4, "Hue changed from %u to %u",
2253 cam->picture.hue, pict.hue)
2254 cam->picture.hue = pict.hue;
2255 }
2256
2257 return 0;
2258
2259fail:
2260 DBG(4, "Failed to change sensor picture setting")
2261 return err;
2262}
2263
2264
2265
2266/****************************************************************************
2267 * Camera configuration *
2268 ****************************************************************************/
2269
2270/*--------------------------------------------------------------------------
2271 This function is called when a supported image sensor is detected.
2272 Return 0 if the initialization succeeds, a negative number otherwise.
2273 --------------------------------------------------------------------------*/
2274static int w9968cf_sensor_init(struct w9968cf_device* cam)
2275{
2276 int err = 0;
2277
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002278 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_INITIALIZE,
2279 &cam->monochrome)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 goto error;
2281
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002282 if ((err = w9968cf_sensor_cmd(cam, OVCAMCHIP_CMD_Q_SUBTYPE,
2283 &cam->sensor)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto error;
2285
2286 /* NOTE: Make sure width and height are a multiple of 16 */
Hans Verkuil3160fbc2009-03-08 10:19:44 -03002287 switch (v4l2_i2c_subdev_addr(cam->sensor_sd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 case OV6xx0_SID:
2289 cam->maxwidth = 352;
2290 cam->maxheight = 288;
2291 cam->minwidth = 64;
2292 cam->minheight = 48;
2293 break;
2294 case OV7xx0_SID:
2295 cam->maxwidth = 640;
2296 cam->maxheight = 480;
2297 cam->minwidth = 64;
2298 cam->minheight = 48;
2299 break;
2300 default:
2301 DBG(1, "Not supported image sensor detected for %s",
2302 symbolic(camlist, cam->id))
2303 return -EINVAL;
2304 }
2305
2306 /* These values depend on the ones in the ovxxx0.c sources */
2307 switch (cam->sensor) {
2308 case CC_OV7620:
2309 cam->start_cropx = 287;
2310 cam->start_cropy = 35;
2311 /* Seems to work around a bug in the image sensor */
2312 cam->vs_polarity = 1;
2313 cam->hs_polarity = 1;
2314 break;
2315 default:
2316 cam->start_cropx = 320;
2317 cam->start_cropy = 35;
2318 cam->vs_polarity = 1;
2319 cam->hs_polarity = 0;
2320 }
2321
2322 if ((err = w9968cf_sensor_update_settings(cam)))
2323 goto error;
2324
2325 if ((err = w9968cf_sensor_update_picture(cam, cam->picture)))
2326 goto error;
2327
2328 cam->sensor_initialized = 1;
2329
2330 DBG(2, "%s image sensor initialized", symbolic(senlist, cam->sensor))
2331 return 0;
2332
2333error:
2334 cam->sensor_initialized = 0;
2335 cam->sensor = CC_UNKNOWN;
2336 DBG(1, "Image sensor initialization failed for %s (/dev/video%d). "
2337 "Try to detach and attach this device again",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002338 symbolic(camlist, cam->id), cam->v4ldev->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return err;
2340}
2341
2342
2343/*--------------------------------------------------------------------------
2344 Fill some basic fields in the main device data structure.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002345 This function is called once on w9968cf_usb_probe() for each recognized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 camera.
2347 --------------------------------------------------------------------------*/
2348static void
2349w9968cf_configure_camera(struct w9968cf_device* cam,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002350 struct usb_device* udev,
2351 enum w9968cf_model_id mod_id,
2352 const unsigned short dev_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002354 mutex_init(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 init_waitqueue_head(&cam->open);
2356 spin_lock_init(&cam->urb_lock);
2357 spin_lock_init(&cam->flist_lock);
2358
2359 cam->users = 0;
2360 cam->disconnected = 0;
2361 cam->id = mod_id;
2362 cam->sensor = CC_UNKNOWN;
2363 cam->sensor_initialized = 0;
2364
2365 /* Calculate the alternate setting number (from 1 to 16)
2366 according to the 'packet_size' module parameter */
2367 if (packet_size[dev_nr] < W9968CF_MIN_PACKET_SIZE)
2368 packet_size[dev_nr] = W9968CF_MIN_PACKET_SIZE;
2369 for (cam->altsetting = 1;
2370 packet_size[dev_nr] < wMaxPacketSize[cam->altsetting-1];
2371 cam->altsetting++);
2372
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002373 cam->max_buffers = (max_buffers[dev_nr] < 2 ||
2374 max_buffers[dev_nr] > W9968CF_MAX_BUFFERS)
2375 ? W9968CF_BUFFERS : (u8)max_buffers[dev_nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002377 cam->double_buffer = (double_buffer[dev_nr] == 0 ||
2378 double_buffer[dev_nr] == 1)
2379 ? (u8)double_buffer[dev_nr]:W9968CF_DOUBLE_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 cam->clamping = (clamping[dev_nr] == 0 || clamping[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002382 ? (u8)clamping[dev_nr] : W9968CF_CLAMPING;
2383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 cam->filter_type = (filter_type[dev_nr] == 0 ||
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002385 filter_type[dev_nr] == 1 ||
2386 filter_type[dev_nr] == 2)
2387 ? (u8)filter_type[dev_nr] : W9968CF_FILTER_TYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 cam->capture = 1;
2390
2391 cam->largeview = (largeview[dev_nr] == 0 || largeview[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002392 ? (u8)largeview[dev_nr] : W9968CF_LARGEVIEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002394 cam->decompression = (decompression[dev_nr] == 0 ||
2395 decompression[dev_nr] == 1 ||
2396 decompression[dev_nr] == 2)
2397 ? (u8)decompression[dev_nr]:W9968CF_DECOMPRESSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002399 cam->upscaling = (upscaling[dev_nr] == 0 ||
2400 upscaling[dev_nr] == 1)
2401 ? (u8)upscaling[dev_nr] : W9968CF_UPSCALING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 cam->auto_brt = (autobright[dev_nr] == 0 || autobright[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002404 ? (u8)autobright[dev_nr] : W9968CF_AUTOBRIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 cam->auto_exp = (autoexp[dev_nr] == 0 || autoexp[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002407 ? (u8)autoexp[dev_nr] : W9968CF_AUTOEXP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 cam->lightfreq = (lightfreq[dev_nr] == 50 || lightfreq[dev_nr] == 60)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002410 ? (u8)lightfreq[dev_nr] : W9968CF_LIGHTFREQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002412 cam->bandfilt = (bandingfilter[dev_nr] == 0 ||
2413 bandingfilter[dev_nr] == 1)
2414 ? (u8)bandingfilter[dev_nr] : W9968CF_BANDINGFILTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 cam->backlight = (backlight[dev_nr] == 0 || backlight[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002417 ? (u8)backlight[dev_nr] : W9968CF_BACKLIGHT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 cam->clockdiv = (clockdiv[dev_nr] == -1 || clockdiv[dev_nr] >= 0)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002420 ? (s8)clockdiv[dev_nr] : W9968CF_CLOCKDIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 cam->mirror = (mirror[dev_nr] == 0 || mirror[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002423 ? (u8)mirror[dev_nr] : W9968CF_MIRROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 cam->monochrome = (monochrome[dev_nr] == 0 || monochrome[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002426 ? monochrome[dev_nr] : W9968CF_MONOCHROME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 cam->picture.brightness = (u16)brightness[dev_nr];
2429 cam->picture.hue = (u16)hue[dev_nr];
2430 cam->picture.colour = (u16)colour[dev_nr];
2431 cam->picture.contrast = (u16)contrast[dev_nr];
2432 cam->picture.whiteness = (u16)whiteness[dev_nr];
2433 if (w9968cf_valid_palette((u16)force_palette[dev_nr])) {
2434 cam->picture.palette = (u16)force_palette[dev_nr];
2435 cam->force_palette = 1;
2436 } else {
2437 cam->force_palette = 0;
2438 if (cam->decompression == 0)
2439 cam->picture.palette = W9968CF_PALETTE_DECOMP_OFF;
2440 else if (cam->decompression == 1)
2441 cam->picture.palette = W9968CF_PALETTE_DECOMP_FORCE;
2442 else
2443 cam->picture.palette = W9968CF_PALETTE_DECOMP_ON;
2444 }
2445 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2446
2447 cam->force_rgb = (force_rgb[dev_nr] == 0 || force_rgb[dev_nr] == 1)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002448 ? (u8)force_rgb[dev_nr] : W9968CF_FORCE_RGB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 cam->window.x = 0;
2451 cam->window.y = 0;
2452 cam->window.width = W9968CF_WIDTH;
2453 cam->window.height = W9968CF_HEIGHT;
2454 cam->window.chromakey = 0;
2455 cam->window.clipcount = 0;
2456 cam->window.flags = 0;
2457
2458 DBG(3, "%s configured with settings #%u:",
2459 symbolic(camlist, cam->id), dev_nr)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 DBG(3, "- Data packet size for USB isochrnous transfer: %u bytes",
2462 wMaxPacketSize[cam->altsetting-1])
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 DBG(3, "- Number of requested video frame buffers: %u",
2465 cam->max_buffers)
2466
2467 if (cam->double_buffer)
2468 DBG(3, "- Hardware double buffering enabled")
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002469 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 DBG(3, "- Hardware double buffering disabled")
2471
2472 if (cam->filter_type == 0)
2473 DBG(3, "- Video filtering disabled")
2474 else if (cam->filter_type == 1)
2475 DBG(3, "- Video filtering enabled: type 1-2-1")
2476 else if (cam->filter_type == 2)
2477 DBG(3, "- Video filtering enabled: type 2-3-6-3-2")
2478
2479 if (cam->clamping)
2480 DBG(3, "- Video data clamping (CCIR-601 format) enabled")
2481 else
2482 DBG(3, "- Video data clamping (CCIR-601 format) disabled")
2483
2484 if (cam->largeview)
2485 DBG(3, "- Large view enabled")
2486 else
2487 DBG(3, "- Large view disabled")
2488
2489 if ((cam->decompression) == 0 && (!cam->force_palette))
2490 DBG(3, "- Decompression disabled")
2491 else if ((cam->decompression) == 1 && (!cam->force_palette))
2492 DBG(3, "- Decompression forced")
2493 else if ((cam->decompression) == 2 && (!cam->force_palette))
2494 DBG(3, "- Decompression allowed")
2495
2496 if (cam->upscaling)
2497 DBG(3, "- Software image scaling enabled")
2498 else
2499 DBG(3, "- Software image scaling disabled")
2500
2501 if (cam->force_palette)
2502 DBG(3, "- Image palette forced to %s",
2503 symbolic(v4l1_plist, cam->picture.palette))
2504
2505 if (cam->force_rgb)
2506 DBG(3, "- RGB component ordering will be used instead of BGR")
2507
2508 if (cam->auto_brt)
2509 DBG(3, "- Auto brightness enabled")
2510 else
2511 DBG(3, "- Auto brightness disabled")
2512
2513 if (cam->auto_exp)
2514 DBG(3, "- Auto exposure enabled")
2515 else
2516 DBG(3, "- Auto exposure disabled")
2517
2518 if (cam->backlight)
2519 DBG(3, "- Backlight exposure algorithm enabled")
2520 else
2521 DBG(3, "- Backlight exposure algorithm disabled")
2522
2523 if (cam->mirror)
2524 DBG(3, "- Mirror enabled")
2525 else
2526 DBG(3, "- Mirror disabled")
2527
2528 if (cam->bandfilt)
2529 DBG(3, "- Banding filter enabled")
2530 else
2531 DBG(3, "- Banding filter disabled")
2532
2533 DBG(3, "- Power lighting frequency: %u", cam->lightfreq)
2534
2535 if (cam->clockdiv == -1)
2536 DBG(3, "- Automatic clock divisor enabled")
2537 else
2538 DBG(3, "- Clock divisor: %d", cam->clockdiv)
2539
2540 if (cam->monochrome)
2541 DBG(3, "- Image sensor used as monochrome")
2542 else
2543 DBG(3, "- Image sensor not used as monochrome")
2544}
2545
2546
2547/*--------------------------------------------------------------------------
2548 If the video post-processing module is not loaded, some parameters
2549 must be overridden.
2550 --------------------------------------------------------------------------*/
2551static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2552{
2553 if (!w9968cf_vpp) {
2554 if (cam->decompression == 1) {
2555 cam->decompression = 2;
2556 DBG(2, "Video post-processing module not found: "
2557 "'decompression' parameter forced to 2")
2558 }
2559 if (cam->upscaling) {
2560 cam->upscaling = 0;
2561 DBG(2, "Video post-processing module not found: "
2562 "'upscaling' parameter forced to 0")
2563 }
2564 if (cam->picture.palette != VIDEO_PALETTE_UYVY) {
2565 cam->force_palette = 0;
2566 DBG(2, "Video post-processing module not found: "
2567 "'force_palette' parameter forced to 0")
2568 }
2569 cam->picture.palette = VIDEO_PALETTE_UYVY;
2570 cam->picture.depth = w9968cf_valid_depth(cam->picture.palette);
2571 }
2572}
2573
2574
2575/*--------------------------------------------------------------------------
2576 Release the resources used by the driver.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002577 This function is called on disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 (or on close if deallocation has been deferred)
2579 --------------------------------------------------------------------------*/
2580static void w9968cf_release_resources(struct w9968cf_device* cam)
2581{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002582 mutex_lock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002584 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 video_unregister_device(cam->v4ldev);
2587 list_del(&cam->v4llist);
2588 i2c_del_adapter(&cam->i2c_adapter);
2589 w9968cf_deallocate_memory(cam);
2590 kfree(cam->control_buffer);
2591 kfree(cam->data_buffer);
Hans Verkuil3160fbc2009-03-08 10:19:44 -03002592 v4l2_device_unregister(&cam->v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002594 mutex_unlock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597
2598
2599/****************************************************************************
2600 * Video4Linux interface *
2601 ****************************************************************************/
2602
Hans Verkuilbec43662008-12-30 06:58:20 -03002603static int w9968cf_open(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 struct w9968cf_device* cam;
2606 int err;
2607
2608 /* This the only safe way to prevent race conditions with disconnect */
2609 if (!down_read_trylock(&w9968cf_disconnect))
Jiri Slaby3abff552007-10-09 22:03:12 -03002610 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2613
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002614 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 if (cam->sensor == CC_UNKNOWN) {
2617 DBG(2, "No supported image sensor has been detected by the "
2618 "'ovcamchip' module for the %s (/dev/video%d). Make "
2619 "sure it is loaded *before* (re)connecting the camera.",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002620 symbolic(camlist, cam->id), cam->v4ldev->num)
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002621 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 up_read(&w9968cf_disconnect);
2623 return -ENODEV;
2624 }
2625
2626 if (cam->users) {
2627 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002628 symbolic(camlist, cam->id), cam->v4ldev->num, cam->command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002630 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 up_read(&w9968cf_disconnect);
2632 return -EWOULDBLOCK;
2633 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002634 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 err = wait_event_interruptible_exclusive(cam->open,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002636 cam->disconnected ||
2637 !cam->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (err) {
2639 up_read(&w9968cf_disconnect);
2640 return err;
2641 }
2642 if (cam->disconnected) {
2643 up_read(&w9968cf_disconnect);
2644 return -ENODEV;
2645 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002646 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648
2649 DBG(5, "Opening '%s', /dev/video%d ...",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002650 symbolic(camlist, cam->id), cam->v4ldev->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 cam->streaming = 0;
2653 cam->misconfigured = 0;
2654
Adrian Bunk7f2c01a2006-01-09 00:43:39 +01002655 w9968cf_adjust_configuration(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 if ((err = w9968cf_allocate_memory(cam)))
2658 goto deallocate_memory;
2659
2660 if ((err = w9968cf_init_chip(cam)))
2661 goto deallocate_memory;
2662
2663 if ((err = w9968cf_start_transfer(cam)))
2664 goto deallocate_memory;
2665
2666 filp->private_data = cam;
2667
2668 cam->users++;
2669 strcpy(cam->command, current->comm);
2670
2671 init_waitqueue_head(&cam->wait_queue);
2672
2673 DBG(5, "Video device is open")
2674
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002675 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 up_read(&w9968cf_disconnect);
2677
2678 return 0;
2679
2680deallocate_memory:
2681 w9968cf_deallocate_memory(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 DBG(2, "Failed to open the video device")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002683 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 up_read(&w9968cf_disconnect);
2685 return err;
2686}
2687
2688
Hans Verkuilbec43662008-12-30 06:58:20 -03002689static int w9968cf_release(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 struct w9968cf_device* cam;
2692
2693 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2694
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002695 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 w9968cf_stop_transfer(cam);
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (cam->disconnected) {
2700 w9968cf_release_resources(cam);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002701 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 kfree(cam);
2703 return 0;
2704 }
2705
2706 cam->users--;
2707 w9968cf_deallocate_memory(cam);
2708 wake_up_interruptible_nr(&cam->open, 1);
2709
2710 DBG(5, "Video device closed")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002711 mutex_unlock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return 0;
2713}
2714
2715
2716static ssize_t
2717w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2718{
2719 struct w9968cf_device* cam;
2720 struct w9968cf_frame_t* fr;
2721 int err = 0;
2722
2723 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2724
2725 if (filp->f_flags & O_NONBLOCK)
2726 return -EWOULDBLOCK;
2727
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002728 if (mutex_lock_interruptible(&cam->fileop_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return -ERESTARTSYS;
2730
2731 if (cam->disconnected) {
2732 DBG(2, "Device not present")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002733 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return -ENODEV;
2735 }
2736
2737 if (cam->misconfigured) {
2738 DBG(2, "The camera is misconfigured. Close and open it again.")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002739 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 return -EIO;
2741 }
2742
2743 if (!cam->frame[0].queued)
2744 w9968cf_push_frame(cam, 0);
2745
2746 if (!cam->frame[1].queued)
2747 w9968cf_push_frame(cam, 1);
2748
2749 err = wait_event_interruptible(cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002750 cam->frame[0].status == F_READY ||
2751 cam->frame[1].status == F_READY ||
2752 cam->disconnected);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (err) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002754 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return err;
2756 }
2757 if (cam->disconnected) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002758 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return -ENODEV;
2760 }
2761
2762 fr = (cam->frame[0].status == F_READY) ? &cam->frame[0]:&cam->frame[1];
2763
2764 if (w9968cf_vpp)
2765 w9968cf_postprocess_frame(cam, fr);
2766
2767 if (count > fr->length)
2768 count = fr->length;
2769
2770 if (copy_to_user(buf, fr->buffer, count)) {
2771 fr->status = F_UNUSED;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002772 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return -EFAULT;
2774 }
2775 *f_pos += count;
2776
2777 fr->status = F_UNUSED;
2778
2779 DBG(5, "%zu bytes read", count)
2780
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002781 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return count;
2783}
2784
2785
2786static int w9968cf_mmap(struct file* filp, struct vm_area_struct *vma)
2787{
2788 struct w9968cf_device* cam = (struct w9968cf_device*)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002789 video_get_drvdata(video_devdata(filp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 unsigned long vsize = vma->vm_end - vma->vm_start,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002791 psize = cam->nbuffers * cam->frame[0].size,
2792 start = vma->vm_start,
2793 pos = (unsigned long)cam->frame[0].buffer,
2794 page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 if (cam->disconnected) {
2797 DBG(2, "Device not present")
2798 return -ENODEV;
2799 }
2800
2801 if (cam->misconfigured) {
2802 DBG(2, "The camera is misconfigured. Close and open it again")
2803 return -EIO;
2804 }
2805
2806 PDBGG("mmapping %lu bytes...", vsize)
2807
2808 if (vsize > psize - (vma->vm_pgoff << PAGE_SHIFT))
2809 return -EINVAL;
2810
2811 while (vsize > 0) {
2812 page = vmalloc_to_pfn((void *)pos);
2813 if (remap_pfn_range(vma, start, page + vma->vm_pgoff,
2814 PAGE_SIZE, vma->vm_page_prot))
2815 return -EAGAIN;
2816 start += PAGE_SIZE;
2817 pos += PAGE_SIZE;
2818 vsize -= PAGE_SIZE;
2819 }
2820
2821 DBG(5, "mmap method successfully called")
2822 return 0;
2823}
2824
2825
Hans Verkuil069b7472008-12-30 07:04:34 -03002826static long
Hans Verkuilbec43662008-12-30 06:58:20 -03002827w9968cf_ioctl(struct file *filp,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002828 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 struct w9968cf_device* cam;
Hans Verkuil069b7472008-12-30 07:04:34 -03002831 long err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2834
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002835 if (mutex_lock_interruptible(&cam->fileop_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return -ERESTARTSYS;
2837
2838 if (cam->disconnected) {
2839 DBG(2, "Device not present")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002840 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 return -ENODEV;
2842 }
2843
2844 if (cam->misconfigured) {
2845 DBG(2, "The camera is misconfigured. Close and open it again.")
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002846 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return -EIO;
2848 }
2849
Hans Verkuilbec43662008-12-30 06:58:20 -03002850 err = w9968cf_v4l_ioctl(filp, cmd, (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002852 mutex_unlock(&cam->fileop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return err;
2854}
2855
2856
Hans Verkuil069b7472008-12-30 07:04:34 -03002857static long w9968cf_v4l_ioctl(struct file *filp,
Hans Verkuilbec43662008-12-30 06:58:20 -03002858 unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
2860 struct w9968cf_device* cam;
2861 const char* v4l1_ioctls[] = {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002862 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 "GPICT", "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF",
2864 "SFBUF", "KEY", "GFREQ", "SFREQ", "GAUDIO", "SAUDIO",
2865 "SYNC", "MCAPTURE", "GMBUF", "GUNIT", "GCAPTURE", "SCAPTURE",
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002866 "SPLAYMODE", "SWRITEMODE", "GPLAYINFO", "SMICROCODE",
2867 "GVBIFMT", "SVBIFMT"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 };
2869
2870 #define V4L1_IOCTL(cmd) \
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002871 ((_IOC_NR((cmd)) < ARRAY_SIZE(v4l1_ioctls)) ? \
2872 v4l1_ioctls[_IOC_NR((cmd))] : "?")
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2875
2876 switch (cmd) {
2877
2878 case VIDIOCGCAP: /* get video capability */
2879 {
2880 struct video_capability cap = {
2881 .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES,
2882 .channels = 1,
2883 .audios = 0,
2884 .minwidth = cam->minwidth,
2885 .minheight = cam->minheight,
2886 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002887 sprintf(cap.name, "W996[87]CF USB Camera #%d",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03002888 cam->v4ldev->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 cap.maxwidth = (cam->upscaling && w9968cf_vpp)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002890 ? max((u16)W9968CF_MAX_WIDTH, cam->maxwidth)
2891 : cam->maxwidth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 cap.maxheight = (cam->upscaling && w9968cf_vpp)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002893 ? max((u16)W9968CF_MAX_HEIGHT, cam->maxheight)
2894 : cam->maxheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 if (copy_to_user(arg, &cap, sizeof(cap)))
2897 return -EFAULT;
2898
2899 DBG(5, "VIDIOCGCAP successfully called")
2900 return 0;
2901 }
2902
2903 case VIDIOCGCHAN: /* get video channel informations */
2904 {
2905 struct video_channel chan;
2906 if (copy_from_user(&chan, arg, sizeof(chan)))
2907 return -EFAULT;
2908
2909 if (chan.channel != 0)
2910 return -EINVAL;
2911
2912 strcpy(chan.name, "Camera");
2913 chan.tuners = 0;
2914 chan.flags = 0;
2915 chan.type = VIDEO_TYPE_CAMERA;
2916 chan.norm = VIDEO_MODE_AUTO;
2917
2918 if (copy_to_user(arg, &chan, sizeof(chan)))
2919 return -EFAULT;
2920
2921 DBG(5, "VIDIOCGCHAN successfully called")
2922 return 0;
2923 }
2924
2925 case VIDIOCSCHAN: /* set active channel */
2926 {
2927 struct video_channel chan;
2928
2929 if (copy_from_user(&chan, arg, sizeof(chan)))
2930 return -EFAULT;
2931
2932 if (chan.channel != 0)
2933 return -EINVAL;
2934
2935 DBG(5, "VIDIOCSCHAN successfully called")
2936 return 0;
2937 }
2938
2939 case VIDIOCGPICT: /* get image properties of the picture */
2940 {
2941 if (w9968cf_sensor_get_picture(cam))
2942 return -EIO;
2943
2944 if (copy_to_user(arg, &cam->picture, sizeof(cam->picture)))
2945 return -EFAULT;
2946
2947 DBG(5, "VIDIOCGPICT successfully called")
2948 return 0;
2949 }
2950
2951 case VIDIOCSPICT: /* change picture settings */
2952 {
2953 struct video_picture pict;
2954 int err = 0;
2955
2956 if (copy_from_user(&pict, arg, sizeof(pict)))
2957 return -EFAULT;
2958
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002959 if ( (cam->force_palette || !w9968cf_vpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 && pict.palette != cam->picture.palette ) {
2961 DBG(4, "Palette %s rejected: only %s is allowed",
2962 symbolic(v4l1_plist, pict.palette),
2963 symbolic(v4l1_plist, cam->picture.palette))
2964 return -EINVAL;
2965 }
2966
2967 if (!w9968cf_valid_palette(pict.palette)) {
2968 DBG(4, "Palette %s not supported. VIDIOCSPICT failed",
2969 symbolic(v4l1_plist, pict.palette))
2970 return -EINVAL;
2971 }
2972
2973 if (!cam->force_palette) {
2974 if (cam->decompression == 0) {
2975 if (w9968cf_need_decompression(pict.palette)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002976 DBG(4, "Decompression disabled: palette %s is not "
2977 "allowed. VIDIOCSPICT failed",
2978 symbolic(v4l1_plist, pict.palette))
2979 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
2981 } else if (cam->decompression == 1) {
2982 if (!w9968cf_need_decompression(pict.palette)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002983 DBG(4, "Decompression forced: palette %s is not "
2984 "allowed. VIDIOCSPICT failed",
2985 symbolic(v4l1_plist, pict.palette))
2986 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
2988 }
2989 }
2990
2991 if (pict.depth != w9968cf_valid_depth(pict.palette)) {
2992 DBG(4, "Requested depth %u bpp is not valid for %s "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002993 "palette: ignored and changed to %u bpp",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 pict.depth, symbolic(v4l1_plist, pict.palette),
2995 w9968cf_valid_depth(pict.palette))
2996 pict.depth = w9968cf_valid_depth(pict.palette);
2997 }
2998
2999 if (pict.palette != cam->picture.palette) {
3000 if(*cam->requested_frame
3001 || cam->frame_current->queued) {
3002 err = wait_event_interruptible
3003 ( cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003004 cam->disconnected ||
3005 (!*cam->requested_frame &&
3006 !cam->frame_current->queued) );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 if (err)
3008 return err;
3009 if (cam->disconnected)
3010 return -ENODEV;
3011 }
3012
3013 if (w9968cf_stop_transfer(cam))
3014 goto ioctl_fail;
3015
3016 if (w9968cf_set_picture(cam, pict))
3017 goto ioctl_fail;
3018
3019 if (w9968cf_start_transfer(cam))
3020 goto ioctl_fail;
3021
3022 } else if (w9968cf_sensor_update_picture(cam, pict))
3023 return -EIO;
3024
3025
3026 DBG(5, "VIDIOCSPICT successfully called")
3027 return 0;
3028 }
3029
3030 case VIDIOCSWIN: /* set capture area */
3031 {
3032 struct video_window win;
3033 int err = 0;
3034
3035 if (copy_from_user(&win, arg, sizeof(win)))
3036 return -EFAULT;
3037
3038 DBG(6, "VIDIOCSWIN called: clipcount=%d, flags=%u, "
3039 "x=%u, y=%u, %ux%u", win.clipcount, win.flags,
3040 win.x, win.y, win.width, win.height)
3041
3042 if (win.clipcount != 0 || win.flags != 0)
3043 return -EINVAL;
3044
3045 if ((err = w9968cf_adjust_window_size(cam, (u16*)&win.width,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003046 (u16*)&win.height))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 DBG(4, "Resolution not supported (%ux%u). "
3048 "VIDIOCSWIN failed", win.width, win.height)
3049 return err;
3050 }
3051
3052 if (win.x != cam->window.x ||
3053 win.y != cam->window.y ||
3054 win.width != cam->window.width ||
3055 win.height != cam->window.height) {
3056 if(*cam->requested_frame
3057 || cam->frame_current->queued) {
3058 err = wait_event_interruptible
3059 ( cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003060 cam->disconnected ||
3061 (!*cam->requested_frame &&
3062 !cam->frame_current->queued) );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (err)
3064 return err;
3065 if (cam->disconnected)
3066 return -ENODEV;
3067 }
3068
3069 if (w9968cf_stop_transfer(cam))
3070 goto ioctl_fail;
3071
3072 /* This _must_ be called before set_window() */
3073 if (w9968cf_set_picture(cam, cam->picture))
3074 goto ioctl_fail;
3075
3076 if (w9968cf_set_window(cam, win))
3077 goto ioctl_fail;
3078
3079 if (w9968cf_start_transfer(cam))
3080 goto ioctl_fail;
3081 }
3082
3083 DBG(5, "VIDIOCSWIN successfully called. ")
3084 return 0;
3085 }
3086
3087 case VIDIOCGWIN: /* get current window properties */
3088 {
3089 if (copy_to_user(arg,&cam->window,sizeof(struct video_window)))
3090 return -EFAULT;
3091
3092 DBG(5, "VIDIOCGWIN successfully called")
3093 return 0;
3094 }
3095
3096 case VIDIOCGMBUF: /* request for memory (mapped) buffer */
3097 {
3098 struct video_mbuf mbuf;
3099 u8 i;
3100
3101 mbuf.size = cam->nbuffers * cam->frame[0].size;
3102 mbuf.frames = cam->nbuffers;
3103 for (i = 0; i < cam->nbuffers; i++)
3104 mbuf.offsets[i] = (unsigned long)cam->frame[i].buffer -
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003105 (unsigned long)cam->frame[0].buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 if (copy_to_user(arg, &mbuf, sizeof(mbuf)))
3108 return -EFAULT;
3109
3110 DBG(5, "VIDIOCGMBUF successfully called")
3111 return 0;
3112 }
3113
3114 case VIDIOCMCAPTURE: /* start the capture to a frame */
3115 {
3116 struct video_mmap mmap;
3117 struct w9968cf_frame_t* fr;
3118 int err = 0;
3119
3120 if (copy_from_user(&mmap, arg, sizeof(mmap)))
3121 return -EFAULT;
3122
3123 DBG(6, "VIDIOCMCAPTURE called: frame #%u, format=%s, %dx%d",
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003124 mmap.frame, symbolic(v4l1_plist, mmap.format),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 mmap.width, mmap.height)
3126
3127 if (mmap.frame >= cam->nbuffers) {
3128 DBG(4, "Invalid frame number (%u). "
3129 "VIDIOCMCAPTURE failed", mmap.frame)
3130 return -EINVAL;
3131 }
3132
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003133 if (mmap.format!=cam->picture.palette &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 (cam->force_palette || !w9968cf_vpp)) {
3135 DBG(4, "Palette %s rejected: only %s is allowed",
3136 symbolic(v4l1_plist, mmap.format),
3137 symbolic(v4l1_plist, cam->picture.palette))
3138 return -EINVAL;
3139 }
3140
3141 if (!w9968cf_valid_palette(mmap.format)) {
3142 DBG(4, "Palette %s not supported. "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003143 "VIDIOCMCAPTURE failed",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 symbolic(v4l1_plist, mmap.format))
3145 return -EINVAL;
3146 }
3147
3148 if (!cam->force_palette) {
3149 if (cam->decompression == 0) {
3150 if (w9968cf_need_decompression(mmap.format)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003151 DBG(4, "Decompression disabled: palette %s is not "
3152 "allowed. VIDIOCSPICT failed",
3153 symbolic(v4l1_plist, mmap.format))
3154 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
3156 } else if (cam->decompression == 1) {
3157 if (!w9968cf_need_decompression(mmap.format)) {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003158 DBG(4, "Decompression forced: palette %s is not "
3159 "allowed. VIDIOCSPICT failed",
3160 symbolic(v4l1_plist, mmap.format))
3161 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 }
3163 }
3164 }
3165
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003166 if ((err = w9968cf_adjust_window_size(cam, (u16*)&mmap.width,
3167 (u16*)&mmap.height))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 DBG(4, "Resolution not supported (%dx%d). "
3169 "VIDIOCMCAPTURE failed",
3170 mmap.width, mmap.height)
3171 return err;
3172 }
3173
3174 fr = &cam->frame[mmap.frame];
3175
3176 if (mmap.width != cam->window.width ||
3177 mmap.height != cam->window.height ||
3178 mmap.format != cam->picture.palette) {
3179
3180 struct video_window win;
3181 struct video_picture pict;
3182
3183 if(*cam->requested_frame
3184 || cam->frame_current->queued) {
3185 DBG(6, "VIDIOCMCAPTURE. Change settings for "
3186 "frame #%u: %dx%d, format %s. Wait...",
3187 mmap.frame, mmap.width, mmap.height,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003188 symbolic(v4l1_plist, mmap.format))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 err = wait_event_interruptible
3190 ( cam->wait_queue,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003191 cam->disconnected ||
3192 (!*cam->requested_frame &&
3193 !cam->frame_current->queued) );
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 if (err)
3195 return err;
3196 if (cam->disconnected)
3197 return -ENODEV;
3198 }
3199
3200 memcpy(&win, &cam->window, sizeof(win));
3201 memcpy(&pict, &cam->picture, sizeof(pict));
3202 win.width = mmap.width;
3203 win.height = mmap.height;
3204 pict.palette = mmap.format;
3205
3206 if (w9968cf_stop_transfer(cam))
3207 goto ioctl_fail;
3208
3209 /* This before set_window */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003210 if (w9968cf_set_picture(cam, pict))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 goto ioctl_fail;
3212
3213 if (w9968cf_set_window(cam, win))
3214 goto ioctl_fail;
3215
3216 if (w9968cf_start_transfer(cam))
3217 goto ioctl_fail;
3218
3219 } else if (fr->queued) {
3220
3221 DBG(6, "Wait until frame #%u is free", mmap.frame)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003222
3223 err = wait_event_interruptible(cam->wait_queue,
3224 cam->disconnected ||
3225 (!fr->queued));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (err)
3227 return err;
3228 if (cam->disconnected)
3229 return -ENODEV;
3230 }
3231
3232 w9968cf_push_frame(cam, mmap.frame);
3233 DBG(5, "VIDIOCMCAPTURE(%u): successfully called", mmap.frame)
3234 return 0;
3235 }
3236
3237 case VIDIOCSYNC: /* wait until the capture of a frame is finished */
3238 {
3239 unsigned int f_num;
3240 struct w9968cf_frame_t* fr;
3241 int err = 0;
3242
3243 if (copy_from_user(&f_num, arg, sizeof(f_num)))
3244 return -EFAULT;
3245
3246 if (f_num >= cam->nbuffers) {
3247 DBG(4, "Invalid frame number (%u). "
3248 "VIDIOCMCAPTURE failed", f_num)
3249 return -EINVAL;
3250 }
3251
3252 DBG(6, "VIDIOCSYNC called for frame #%u", f_num)
3253
3254 fr = &cam->frame[f_num];
3255
3256 switch (fr->status) {
3257 case F_UNUSED:
3258 if (!fr->queued) {
3259 DBG(4, "VIDIOSYNC: Frame #%u not requested!",
3260 f_num)
3261 return -EFAULT;
3262 }
3263 case F_ERROR:
3264 case F_GRABBING:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003265 err = wait_event_interruptible(cam->wait_queue,
3266 (fr->status == F_READY)
3267 || cam->disconnected);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (err)
3269 return err;
3270 if (cam->disconnected)
3271 return -ENODEV;
3272 break;
3273 case F_READY:
3274 break;
3275 }
3276
3277 if (w9968cf_vpp)
3278 w9968cf_postprocess_frame(cam, fr);
3279
3280 fr->status = F_UNUSED;
3281
3282 DBG(5, "VIDIOCSYNC(%u) successfully called", f_num)
3283 return 0;
3284 }
3285
3286 case VIDIOCGUNIT:/* report the unit numbers of the associated devices*/
3287 {
3288 struct video_unit unit = {
3289 .video = cam->v4ldev->minor,
3290 .vbi = VIDEO_NO_UNIT,
3291 .radio = VIDEO_NO_UNIT,
3292 .audio = VIDEO_NO_UNIT,
3293 .teletext = VIDEO_NO_UNIT,
3294 };
3295
3296 if (copy_to_user(arg, &unit, sizeof(unit)))
3297 return -EFAULT;
3298
3299 DBG(5, "VIDIOCGUNIT successfully called")
3300 return 0;
3301 }
3302
3303 case VIDIOCKEY:
3304 return 0;
3305
3306 case VIDIOCGFBUF:
3307 {
3308 if (clear_user(arg, sizeof(struct video_buffer)))
3309 return -EFAULT;
3310
3311 DBG(5, "VIDIOCGFBUF successfully called")
3312 return 0;
3313 }
3314
3315 case VIDIOCGTUNER:
3316 {
3317 struct video_tuner tuner;
3318 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3319 return -EFAULT;
3320
3321 if (tuner.tuner != 0)
3322 return -EINVAL;
3323
3324 strcpy(tuner.name, "no_tuner");
3325 tuner.rangelow = 0;
3326 tuner.rangehigh = 0;
3327 tuner.flags = VIDEO_TUNER_NORM;
3328 tuner.mode = VIDEO_MODE_AUTO;
3329 tuner.signal = 0xffff;
3330
3331 if (copy_to_user(arg, &tuner, sizeof(tuner)))
3332 return -EFAULT;
3333
3334 DBG(5, "VIDIOCGTUNER successfully called")
3335 return 0;
3336 }
3337
3338 case VIDIOCSTUNER:
3339 {
3340 struct video_tuner tuner;
3341 if (copy_from_user(&tuner, arg, sizeof(tuner)))
3342 return -EFAULT;
3343
3344 if (tuner.tuner != 0)
3345 return -EINVAL;
3346
3347 if (tuner.mode != VIDEO_MODE_AUTO)
3348 return -EINVAL;
3349
3350 DBG(5, "VIDIOCSTUNER successfully called")
3351 return 0;
3352 }
3353
3354 case VIDIOCSFBUF:
3355 case VIDIOCCAPTURE:
3356 case VIDIOCGFREQ:
3357 case VIDIOCSFREQ:
3358 case VIDIOCGAUDIO:
3359 case VIDIOCSAUDIO:
3360 case VIDIOCSPLAYMODE:
3361 case VIDIOCSWRITEMODE:
3362 case VIDIOCGPLAYINFO:
3363 case VIDIOCSMICROCODE:
3364 case VIDIOCGVBIFMT:
3365 case VIDIOCSVBIFMT:
3366 DBG(4, "Unsupported V4L1 IOCtl: VIDIOC%s "
3367 "(type 0x%01X, "
3368 "n. 0x%01X, "
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003369 "dir. 0x%01X, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 "size 0x%02X)",
3371 V4L1_IOCTL(cmd),
3372 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3373
3374 return -EINVAL;
3375
3376 default:
3377 DBG(4, "Invalid V4L1 IOCtl: VIDIOC%s "
3378 "type 0x%01X, "
3379 "n. 0x%01X, "
3380 "dir. 0x%01X, "
3381 "size 0x%02X",
3382 V4L1_IOCTL(cmd),
3383 _IOC_TYPE(cmd),_IOC_NR(cmd),_IOC_DIR(cmd),_IOC_SIZE(cmd))
3384
3385 return -ENOIOCTLCMD;
3386
3387 } /* end of switch */
3388
3389ioctl_fail:
3390 cam->misconfigured = 1;
3391 DBG(1, "VIDIOC%s failed because of hardware problems. "
3392 "To use the camera, close and open it again.", V4L1_IOCTL(cmd))
3393 return -EFAULT;
3394}
3395
3396
Hans Verkuilbec43662008-12-30 06:58:20 -03003397static const struct v4l2_file_operations w9968cf_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 .owner = THIS_MODULE,
3399 .open = w9968cf_open,
3400 .release = w9968cf_release,
3401 .read = w9968cf_read,
3402 .ioctl = w9968cf_ioctl,
3403 .mmap = w9968cf_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404};
3405
3406
3407
3408/****************************************************************************
3409 * USB probe and V4L registration, disconnect and id_table[] definition *
3410 ****************************************************************************/
3411
3412static int
3413w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3414{
3415 struct usb_device *udev = interface_to_usbdev(intf);
3416 struct w9968cf_device* cam;
3417 int err = 0;
3418 enum w9968cf_model_id mod_id;
3419 struct list_head* ptr;
3420 u8 sc = 0; /* number of simultaneous cameras */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -03003421 static unsigned short dev_nr; /* 0 - we are handling device number n */
Hans Verkuil3160fbc2009-03-08 10:19:44 -03003422 static unsigned short addrs[] = {
3423 OV7xx0_SID,
3424 OV6xx0_SID,
3425 I2C_CLIENT_END
3426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
3428 if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[0].idVendor &&
3429 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[0].idProduct)
3430 mod_id = W9968CF_MOD_CLVBWGP; /* see camlist[] table */
3431 else if (le16_to_cpu(udev->descriptor.idVendor) == winbond_id_table[1].idVendor &&
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003432 le16_to_cpu(udev->descriptor.idProduct) == winbond_id_table[1].idProduct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 mod_id = W9968CF_MOD_GENERIC; /* see camlist[] table */
3434 else
3435 return -ENODEV;
3436
3437 cam = (struct w9968cf_device*)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003438 kzalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 if (!cam)
3440 return -ENOMEM;
3441
Hans Verkuil4e068392009-03-08 06:56:19 -03003442 err = v4l2_device_register(&udev->dev, &cam->v4l2_dev);
3443 if (err)
3444 goto fail0;
3445
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003446 mutex_init(&cam->dev_mutex);
3447 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
3449 cam->usbdev = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 DBG(2, "%s detected", symbolic(camlist, mod_id))
3452
3453 if (simcams > W9968CF_MAX_DEVICES)
3454 simcams = W9968CF_SIMCAMS;
3455
3456 /* How many cameras are connected ? */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003457 mutex_lock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 list_for_each(ptr, &w9968cf_dev_list)
3459 sc++;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003460 mutex_unlock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 if (sc >= simcams) {
3463 DBG(2, "Device rejected: too many connected cameras "
3464 "(max. %u)", simcams)
3465 err = -EPERM;
3466 goto fail;
3467 }
3468
3469
3470 /* Allocate 2 bytes of memory for camera control USB transfers */
Oliver Neukumb10b4172006-01-06 21:28:40 +01003471 if (!(cam->control_buffer = kzalloc(2, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 DBG(1,"Couldn't allocate memory for camera control transfers")
3473 err = -ENOMEM;
3474 goto fail;
3475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
Oliver Neukumb10b4172006-01-06 21:28:40 +01003478 if (!(cam->data_buffer = kzalloc(8, GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 DBG(1, "Couldn't allocate memory for data "
3480 "transfers to the FSB")
3481 err = -ENOMEM;
3482 goto fail;
3483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485 /* Register the V4L device */
3486 cam->v4ldev = video_device_alloc();
3487 if (!cam->v4ldev) {
3488 DBG(1, "Could not allocate memory for a V4L structure")
3489 err = -ENOMEM;
3490 goto fail;
3491 }
3492
3493 strcpy(cam->v4ldev->name, symbolic(camlist, mod_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 cam->v4ldev->fops = &w9968cf_fops;
3495 cam->v4ldev->minor = video_nr[dev_nr];
3496 cam->v4ldev->release = video_device_release;
3497 video_set_drvdata(cam->v4ldev, cam);
Hans Verkuil4e068392009-03-08 06:56:19 -03003498 cam->v4ldev->v4l2_dev = &cam->v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003501 video_nr[dev_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 if (err) {
3503 DBG(1, "V4L device registration failed")
3504 if (err == -ENFILE && video_nr[dev_nr] == -1)
3505 DBG(2, "Couldn't find a free /dev/videoX node")
3506 video_nr[dev_nr] = -1;
3507 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3508 goto fail;
3509 }
3510
Hans Verkuilc6330fb2008-10-19 18:54:26 -03003511 DBG(2, "V4L device registered as /dev/video%d", cam->v4ldev->num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 /* Set some basic constants */
3514 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3515
3516 /* Add a new entry into the list of V4L registered devices */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003517 mutex_lock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 list_add(&cam->v4llist, &w9968cf_dev_list);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003519 mutex_unlock(&w9968cf_devlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3521
3522 w9968cf_turn_on_led(cam);
3523
3524 w9968cf_i2c_init(cam);
Hans Verkuil3160fbc2009-03-08 10:19:44 -03003525 cam->sensor_sd = v4l2_i2c_new_probed_subdev(&cam->i2c_adapter,
3526 "ovcamchip", "ovcamchip", addrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
3528 usb_set_intfdata(intf, cam);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003529 mutex_unlock(&cam->dev_mutex);
Hans Verkuil4e068392009-03-08 06:56:19 -03003530
Hans Verkuil3160fbc2009-03-08 10:19:44 -03003531 err = w9968cf_sensor_init(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 return 0;
3533
3534fail: /* Free unused memory */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003535 kfree(cam->control_buffer);
3536 kfree(cam->data_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 if (cam->v4ldev)
3538 video_device_release(cam->v4ldev);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003539 mutex_unlock(&cam->dev_mutex);
Hans Verkuil4e068392009-03-08 06:56:19 -03003540 v4l2_device_unregister(&cam->v4l2_dev);
3541fail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 kfree(cam);
3543 return err;
3544}
3545
3546
3547static void w9968cf_usb_disconnect(struct usb_interface* intf)
3548{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03003549 struct w9968cf_device* cam =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 (struct w9968cf_device*)usb_get_intfdata(intf);
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 if (cam) {
Hans Verkuil3160fbc2009-03-08 10:19:44 -03003553 down_write(&w9968cf_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 /* Prevent concurrent accesses to data */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003555 mutex_lock(&cam->dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557 cam->disconnected = 1;
3558
3559 DBG(2, "Disconnecting %s...", symbolic(camlist, cam->id))
3560
3561 wake_up_interruptible_all(&cam->open);
3562
3563 if (cam->users) {
3564 DBG(2, "The device is open (/dev/video%d)! "
3565 "Process name: %s. Deregistration and memory "
3566 "deallocation are deferred on close.",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03003567 cam->v4ldev->num, cam->command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 cam->misconfigured = 1;
3569 w9968cf_stop_transfer(cam);
3570 wake_up_interruptible(&cam->wait_queue);
3571 } else
3572 w9968cf_release_resources(cam);
3573
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003574 mutex_unlock(&cam->dev_mutex);
Hans Verkuil3160fbc2009-03-08 10:19:44 -03003575 up_write(&w9968cf_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Hans Verkuil4e068392009-03-08 06:56:19 -03003577 if (!cam->users) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 kfree(cam);
Hans Verkuil4e068392009-03-08 06:56:19 -03003579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
3583
3584static struct usb_driver w9968cf_usb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 .name = "w9968cf",
3586 .id_table = winbond_id_table,
3587 .probe = w9968cf_usb_probe,
3588 .disconnect = w9968cf_usb_disconnect,
3589};
3590
3591
3592
3593/****************************************************************************
3594 * Module init, exit and intermodule communication *
3595 ****************************************************************************/
3596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597static int __init w9968cf_module_init(void)
3598{
3599 int err;
3600
3601 KDBG(2, W9968CF_MODULE_NAME" "W9968CF_MODULE_VERSION)
3602 KDBG(3, W9968CF_MODULE_AUTHOR)
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 if ((err = usb_register(&w9968cf_usb_driver)))
3605 return err;
3606
3607 return 0;
3608}
3609
3610
3611static void __exit w9968cf_module_exit(void)
3612{
3613 /* w9968cf_usb_disconnect() will be called */
3614 usb_deregister(&w9968cf_usb_driver);
3615
3616 KDBG(2, W9968CF_MODULE_NAME" deregistered")
3617}
3618
3619
3620module_init(w9968cf_module_init);
3621module_exit(w9968cf_module_exit);
3622