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