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