blob: db2a6003a1c3990e032f9fd2a5a9ff40bbb595ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 Winbond w9966cf Webcam parport driver.
3
Hans Verkuil626e2ac2010-04-06 11:36:39 -03004 Version 0.33
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 Copyright (C) 2001 Jakob Kemi <jakob.kemi@post.utfors.se>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22/*
23 Supported devices:
24 *Lifeview FlyCam Supra (using the Philips saa7111a chip)
25
26 Does any other model using the w9966 interface chip exist ?
27
28 Todo:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *Add a working EPP mode, since DMA ECP read isn't implemented
31 in the parport drivers. (That's why it's so sloow)
32
33 *Add support for other ccd-control chips than the saa7111
34 please send me feedback on what kind of chips you have.
35
36 *Add proper probing. I don't know what's wrong with the IEEE1284
37 parport drivers but (IEEE1284_MODE_NIBBLE|IEEE1284_DEVICE_ID)
38 and nibble read seems to be broken for some peripherals.
39
40 *Add probing for onboard SRAM, port directions etc. (if possible)
41
42 *Add support for the hardware compressed modes (maybe using v4l2)
43
44 *Fix better support for the capture window (no skewed images, v4l
45 interface to capt. window)
46
47 *Probably some bugs that I don't know of
48
49 Please support me by sending feedback!
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 Changes:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 Alan Cox: Removed RGB mode for kernel merge, added THIS_MODULE
54 and owner support for newer module locks
55*/
56
57#include <linux/module.h>
58#include <linux/init.h>
59#include <linux/delay.h>
Hans Verkuil626e2ac2010-04-06 11:36:39 -030060#include <linux/videodev2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030062#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030063#include <media/v4l2-ioctl.h>
Hans Verkuil626e2ac2010-04-06 11:36:39 -030064#include <media/v4l2-device.h>
Hans Verkuil9d593442012-05-08 17:00:43 -030065#include <media/v4l2-fh.h>
66#include <media/v4l2-ctrls.h>
67#include <media/v4l2-event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/parport.h>
69
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030070/*#define DEBUG*/ /* Undef me for production */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#ifdef DEBUG
Harvey Harrison7e28adb2008-04-08 23:20:00 -030073#define DPRINTF(x, a...) printk(KERN_DEBUG "W9966: %s(): "x, __func__ , ##a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#else
75#define DPRINTF(x...)
76#endif
77
78/*
79 * Defines, simple typedefs etc.
80 */
81
82#define W9966_DRIVERNAME "W9966CF Webcam"
Hans Verkuil4bfdd582010-03-22 04:47:27 -030083#define W9966_MAXCAMS 4 /* Maximum number of cameras */
84#define W9966_RBUFFER 2048 /* Read buffer (must be an even number) */
85#define W9966_SRAMSIZE 131072 /* 128kb */
86#define W9966_SRAMID 0x02 /* check w9966cf.pdf */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Hans Verkuil4bfdd582010-03-22 04:47:27 -030088/* Empirically determined window limits */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define W9966_WND_MIN_X 16
90#define W9966_WND_MIN_Y 14
91#define W9966_WND_MAX_X 705
92#define W9966_WND_MAX_Y 253
93#define W9966_WND_MAX_W (W9966_WND_MAX_X - W9966_WND_MIN_X)
94#define W9966_WND_MAX_H (W9966_WND_MAX_Y - W9966_WND_MIN_Y)
95
Hans Verkuil4bfdd582010-03-22 04:47:27 -030096/* Keep track of our current state */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define W9966_STATE_PDEV 0x01
98#define W9966_STATE_CLAIMED 0x02
99#define W9966_STATE_VDEV 0x04
100
101#define W9966_I2C_W_ID 0x48
102#define W9966_I2C_R_ID 0x49
103#define W9966_I2C_R_DATA 0x08
104#define W9966_I2C_R_CLOCK 0x04
105#define W9966_I2C_W_DATA 0x02
106#define W9966_I2C_W_CLOCK 0x01
107
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300108struct w9966 {
109 struct v4l2_device v4l2_dev;
Hans Verkuil9d593442012-05-08 17:00:43 -0300110 struct v4l2_ctrl_handler hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned char dev_state;
112 unsigned char i2c_state;
113 unsigned short ppmode;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300114 struct parport *pport;
115 struct pardevice *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct video_device vdev;
117 unsigned short width;
118 unsigned short height;
119 unsigned char brightness;
120 signed char contrast;
121 signed char color;
122 signed char hue;
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300123 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126/*
127 * Module specific properties
128 */
129
130MODULE_AUTHOR("Jakob Kemi <jakob.kemi@post.utfors.se>");
131MODULE_DESCRIPTION("Winbond w9966cf WebCam driver (0.32)");
132MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -0300133MODULE_VERSION("0.33.1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135#ifdef MODULE
Mauro Carvalho Chehabc9e72522012-03-19 15:37:59 -0300136static char *pardev[] = {[0 ... W9966_MAXCAMS] = ""};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#else
Mauro Carvalho Chehabc9e72522012-03-19 15:37:59 -0300138static char *pardev[] = {[0 ... W9966_MAXCAMS] = "aggressive"};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#endif
140module_param_array(pardev, charp, NULL, 0);
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300141MODULE_PARM_DESC(pardev, "pardev: where to search for\n"
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300142 "\teach camera. 'aggressive' means brute-force search.\n"
143 "\tEg: >pardev=parport3,aggressive,parport2,parport1< would assign\n"
144 "\tcam 1 to parport3 and search every parport for cam 2 etc...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300146static int parmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147module_param(parmode, int, 0);
148MODULE_PARM_DESC(parmode, "parmode: transfer mode (0=auto, 1=ecp, 2=epp");
149
150static int video_nr = -1;
151module_param(video_nr, int, 0);
152
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300153static struct w9966 w9966_cams[W9966_MAXCAMS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * Private function defines
157 */
158
159
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300160/* Set camera phase flags, so we know what to uninit when terminating */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300161static inline void w9966_set_state(struct w9966 *cam, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 cam->dev_state = (cam->dev_state & ~mask) ^ val;
164}
165
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300166/* Get camera phase flags */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300167static inline int w9966_get_state(struct w9966 *cam, int mask, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 return ((cam->dev_state & mask) == val);
170}
171
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300172/* Claim parport for ourself */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300173static void w9966_pdev_claim(struct w9966 *cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300175 if (w9966_get_state(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return;
177 parport_claim_or_block(cam->pdev);
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300178 w9966_set_state(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300181/* Release parport for others to use */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300182static void w9966_pdev_release(struct w9966 *cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300184 if (w9966_get_state(cam, W9966_STATE_CLAIMED, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return;
186 parport_release(cam->pdev);
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300187 w9966_set_state(cam, W9966_STATE_CLAIMED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300189
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300190/* Read register from W9966 interface-chip
191 Expects a claimed pdev
192 -1 on error, else register data (byte) */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300193static int w9966_read_reg(struct w9966 *cam, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300195 /* ECP, read, regtransfer, REG, REG, REG, REG, REG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 const unsigned char addr = 0x80 | (reg & 0x1f);
197 unsigned char val;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
200 return -1;
201 if (parport_write(cam->pport, &addr, 1) != 1)
202 return -1;
203 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
204 return -1;
205 if (parport_read(cam->pport, &val, 1) != 1)
206 return -1;
207
208 return val;
209}
210
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300211/* Write register to W9966 interface-chip
212 Expects a claimed pdev
213 -1 on error */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300214static int w9966_write_reg(struct w9966 *cam, int reg, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300216 /* ECP, write, regtransfer, REG, REG, REG, REG, REG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 const unsigned char addr = 0xc0 | (reg & 0x1f);
218 const unsigned char val = data;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
221 return -1;
222 if (parport_write(cam->pport, &addr, 1) != 1)
223 return -1;
224 if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
225 return -1;
226 if (parport_write(cam->pport, &val, 1) != 1)
227 return -1;
228
229 return 0;
230}
231
Hans Verkuil271922c2010-03-22 05:13:17 -0300232/*
233 * Ugly and primitive i2c protocol functions
234 */
235
236/* Sets the data line on the i2c bus.
237 Expects a claimed pdev. */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300238static void w9966_i2c_setsda(struct w9966 *cam, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Hans Verkuil271922c2010-03-22 05:13:17 -0300240 if (state)
241 cam->i2c_state |= W9966_I2C_W_DATA;
242 else
243 cam->i2c_state &= ~W9966_I2C_W_DATA;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300244
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300245 w9966_write_reg(cam, 0x18, cam->i2c_state);
Hans Verkuil271922c2010-03-22 05:13:17 -0300246 udelay(5);
247}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Hans Verkuil271922c2010-03-22 05:13:17 -0300249/* Get peripheral clock line
250 Expects a claimed pdev. */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300251static int w9966_i2c_getscl(struct w9966 *cam)
Hans Verkuil271922c2010-03-22 05:13:17 -0300252{
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300253 const unsigned char state = w9966_read_reg(cam, 0x18);
Hans Verkuil271922c2010-03-22 05:13:17 -0300254 return ((state & W9966_I2C_R_CLOCK) > 0);
255}
256
257/* Sets the clock line on the i2c bus.
258 Expects a claimed pdev. -1 on error */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300259static int w9966_i2c_setscl(struct w9966 *cam, int state)
Hans Verkuil271922c2010-03-22 05:13:17 -0300260{
261 unsigned long timeout;
262
263 if (state)
264 cam->i2c_state |= W9966_I2C_W_CLOCK;
265 else
266 cam->i2c_state &= ~W9966_I2C_W_CLOCK;
267
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300268 w9966_write_reg(cam, 0x18, cam->i2c_state);
Hans Verkuil271922c2010-03-22 05:13:17 -0300269 udelay(5);
270
271 /* we go to high, we also expect the peripheral to ack. */
272 if (state) {
273 timeout = jiffies + 100;
274 while (!w9966_i2c_getscl(cam)) {
275 if (time_after(jiffies, timeout))
276 return -1;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
280}
281
Hans Verkuil271922c2010-03-22 05:13:17 -0300282#if 0
283/* Get peripheral data line
284 Expects a claimed pdev. */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300285static int w9966_i2c_getsda(struct w9966 *cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300287 const unsigned char state = w9966_read_reg(cam, 0x18);
Hans Verkuil271922c2010-03-22 05:13:17 -0300288 return ((state & W9966_I2C_R_DATA) > 0);
289}
290#endif
291
292/* Write a byte with ack to the i2c bus.
293 Expects a claimed pdev. -1 on error */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300294static int w9966_i2c_wbyte(struct w9966 *cam, int data)
Hans Verkuil271922c2010-03-22 05:13:17 -0300295{
296 int i;
297
298 for (i = 7; i >= 0; i--) {
299 w9966_i2c_setsda(cam, (data >> i) & 0x01);
300
301 if (w9966_i2c_setscl(cam, 1) == -1)
302 return -1;
303 w9966_i2c_setscl(cam, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Hans Verkuil271922c2010-03-22 05:13:17 -0300306 w9966_i2c_setsda(cam, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Hans Verkuil271922c2010-03-22 05:13:17 -0300308 if (w9966_i2c_setscl(cam, 1) == -1)
309 return -1;
310 w9966_i2c_setscl(cam, 0);
311
312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Hans Verkuil271922c2010-03-22 05:13:17 -0300315/* Read a data byte with ack from the i2c-bus
316 Expects a claimed pdev. -1 on error */
317#if 0
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300318static int w9966_i2c_rbyte(struct w9966 *cam)
Hans Verkuil271922c2010-03-22 05:13:17 -0300319{
320 unsigned char data = 0x00;
321 int i;
322
323 w9966_i2c_setsda(cam, 1);
324
325 for (i = 0; i < 8; i++) {
326 if (w9966_i2c_setscl(cam, 1) == -1)
327 return -1;
328 data = data << 1;
329 if (w9966_i2c_getsda(cam))
330 data |= 0x01;
331
332 w9966_i2c_setscl(cam, 0);
333 }
334 return data;
335}
336#endif
337
338/* Read a register from the i2c device.
339 Expects claimed pdev. -1 on error */
340#if 0
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300341static int w9966_read_reg_i2c(struct w9966 *cam, int reg)
Hans Verkuil271922c2010-03-22 05:13:17 -0300342{
343 int data;
344
345 w9966_i2c_setsda(cam, 0);
346 w9966_i2c_setscl(cam, 0);
347
348 if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
349 w9966_i2c_wbyte(cam, reg) == -1)
350 return -1;
351
352 w9966_i2c_setsda(cam, 1);
353 if (w9966_i2c_setscl(cam, 1) == -1)
354 return -1;
355 w9966_i2c_setsda(cam, 0);
356 w9966_i2c_setscl(cam, 0);
357
358 if (w9966_i2c_wbyte(cam, W9966_I2C_R_ID) == -1)
359 return -1;
360 data = w9966_i2c_rbyte(cam);
361 if (data == -1)
362 return -1;
363
364 w9966_i2c_setsda(cam, 0);
365
366 if (w9966_i2c_setscl(cam, 1) == -1)
367 return -1;
368 w9966_i2c_setsda(cam, 1);
369
370 return data;
371}
372#endif
373
374/* Write a register to the i2c device.
375 Expects claimed pdev. -1 on error */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300376static int w9966_write_reg_i2c(struct w9966 *cam, int reg, int data)
Hans Verkuil271922c2010-03-22 05:13:17 -0300377{
378 w9966_i2c_setsda(cam, 0);
379 w9966_i2c_setscl(cam, 0);
380
381 if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300382 w9966_i2c_wbyte(cam, reg) == -1 ||
383 w9966_i2c_wbyte(cam, data) == -1)
Hans Verkuil271922c2010-03-22 05:13:17 -0300384 return -1;
385
386 w9966_i2c_setsda(cam, 0);
387 if (w9966_i2c_setscl(cam, 1) == -1)
388 return -1;
389
390 w9966_i2c_setsda(cam, 1);
391
392 return 0;
393}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300395/* Find a good length for capture window (used both for W and H)
396 A bit ugly but pretty functional. The capture length
397 have to match the downscale */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static int w9966_findlen(int near, int size, int maxlen)
399{
400 int bestlen = size;
401 int besterr = abs(near - bestlen);
402 int len;
403
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300404 for (len = size + 1; len < maxlen; len++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int err;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300406 if (((64 * size) % len) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 continue;
408
409 err = abs(near - len);
410
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300411 /* Only continue as long as we keep getting better values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (err > besterr)
413 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 besterr = err;
416 bestlen = len;
417 }
418
419 return bestlen;
420}
421
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300422/* Modify capture window (if necessary)
423 and calculate downscaling
424 Return -1 on error */
425static int w9966_calcscale(int size, int min, int max, int *beg, int *end, unsigned char *factor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 int maxlen = max - min;
428 int len = *end - *beg + 1;
429 int newlen = w9966_findlen(len, size, maxlen);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300430 int err = newlen - len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300432 /* Check for bad format */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (newlen > maxlen || newlen < size)
434 return -1;
435
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300436 /* Set factor (6 bit fixed) */
437 *factor = (64 * size) / newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (*factor == 64)
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300439 *factor = 0x00; /* downscale is disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 else
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300441 *factor |= 0x80; /* set downscale-enable bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300443 /* Modify old beginning and end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 *beg -= err / 2;
445 *end += err - (err / 2);
446
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300447 /* Move window if outside borders */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (*beg < min) {
449 *end += min - *beg;
450 *beg += min - *beg;
451 }
452 if (*end > max) {
453 *beg -= *end - max;
454 *end -= *end - max;
455 }
456
457 return 0;
458}
459
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300460/* Setup the cameras capture window etc.
461 Expects a claimed pdev
462 return -1 on error */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300463static int w9966_setup(struct w9966 *cam, int x1, int y1, int x2, int y2, int w, int h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 unsigned int i;
466 unsigned int enh_s, enh_e;
467 unsigned char scale_x, scale_y;
468 unsigned char regs[0x1c];
469 unsigned char saa7111_regs[] = {
470 0x21, 0x00, 0xd8, 0x23, 0x00, 0x80, 0x80, 0x00,
471 0x88, 0x10, 0x80, 0x40, 0x40, 0x00, 0x01, 0x00,
472 0x48, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
473 0x00, 0x00, 0x00, 0x71, 0xe7, 0x00, 0x00, 0xc0
474 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300475
476
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300477 if (w * h * 2 > W9966_SRAMSIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 DPRINTF("capture window exceeds SRAM size!.\n");
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300479 w = 200; h = 160; /* Pick default values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 w &= ~0x1;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300483 if (w < 2)
484 w = 2;
485 if (h < 1)
486 h = 1;
487 if (w > W9966_WND_MAX_W)
488 w = W9966_WND_MAX_W;
489 if (h > W9966_WND_MAX_H)
490 h = W9966_WND_MAX_H;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 cam->width = w;
493 cam->height = h;
494
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300495 enh_s = 0;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300496 enh_e = w * h * 2;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300497
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300498 /* Modify capture window if necessary and calculate downscaling */
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300499 if (w9966_calcscale(w, W9966_WND_MIN_X, W9966_WND_MAX_X, &x1, &x2, &scale_x) != 0 ||
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300500 w9966_calcscale(h, W9966_WND_MIN_Y, W9966_WND_MAX_Y, &y1, &y2, &scale_y) != 0)
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300501 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300503 DPRINTF("%dx%d, x: %d<->%d, y: %d<->%d, sx: %d/64, sy: %d/64.\n",
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300504 w, h, x1, x2, y1, y2, scale_x & ~0x80, scale_y & ~0x80);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300505
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300506 /* Setup registers */
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300507 regs[0x00] = 0x00; /* Set normal operation */
508 regs[0x01] = 0x18; /* Capture mode */
509 regs[0x02] = scale_y; /* V-scaling */
510 regs[0x03] = scale_x; /* H-scaling */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300511
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300512 /* Capture window */
513 regs[0x04] = (x1 & 0x0ff); /* X-start (8 low bits) */
514 regs[0x05] = (x1 & 0x300)>>8; /* X-start (2 high bits) */
515 regs[0x06] = (y1 & 0x0ff); /* Y-start (8 low bits) */
516 regs[0x07] = (y1 & 0x300)>>8; /* Y-start (2 high bits) */
517 regs[0x08] = (x2 & 0x0ff); /* X-end (8 low bits) */
518 regs[0x09] = (x2 & 0x300)>>8; /* X-end (2 high bits) */
519 regs[0x0a] = (y2 & 0x0ff); /* Y-end (8 low bits) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300521 regs[0x0c] = W9966_SRAMID; /* SRAM-banks (1x 128kb) */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300522
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300523 /* Enhancement layer */
524 regs[0x0d] = (enh_s & 0x000ff); /* Enh. start (0-7) */
525 regs[0x0e] = (enh_s & 0x0ff00) >> 8; /* Enh. start (8-15) */
526 regs[0x0f] = (enh_s & 0x70000) >> 16; /* Enh. start (16-17/18??) */
527 regs[0x10] = (enh_e & 0x000ff); /* Enh. end (0-7) */
528 regs[0x11] = (enh_e & 0x0ff00) >> 8; /* Enh. end (8-15) */
529 regs[0x12] = (enh_e & 0x70000) >> 16; /* Enh. end (16-17/18??) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300531 /* Misc */
532 regs[0x13] = 0x40; /* VEE control (raw 4:2:2) */
533 regs[0x17] = 0x00; /* ??? */
534 regs[0x18] = cam->i2c_state = 0x00; /* Serial bus */
535 regs[0x19] = 0xff; /* I/O port direction control */
536 regs[0x1a] = 0xff; /* I/O port data register */
537 regs[0x1b] = 0x10; /* ??? */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300538
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300539 /* SAA7111 chip settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 saa7111_regs[0x0a] = cam->brightness;
541 saa7111_regs[0x0b] = cam->contrast;
542 saa7111_regs[0x0c] = cam->color;
543 saa7111_regs[0x0d] = cam->hue;
544
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300545 /* Reset (ECP-fifo & serial-bus) */
546 if (w9966_write_reg(cam, 0x00, 0x03) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -1;
548
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300549 /* Write regs to w9966cf chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 for (i = 0; i < 0x1c; i++)
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300551 if (w9966_write_reg(cam, i, regs[i]) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -1;
553
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300554 /* Write regs to saa7111 chip */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 for (i = 0; i < 0x20; i++)
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300556 if (w9966_write_reg_i2c(cam, i, saa7111_regs[i]) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -1;
558
559 return 0;
560}
561
562/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * Video4linux interfacing
564 */
565
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300566static int cam_querycap(struct file *file, void *priv,
567 struct v4l2_capability *vcap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300569 struct w9966 *cam = video_drvdata(file);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300570
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300571 strlcpy(vcap->driver, cam->v4l2_dev.name, sizeof(vcap->driver));
572 strlcpy(vcap->card, W9966_DRIVERNAME, sizeof(vcap->card));
573 strlcpy(vcap->bus_info, "parport", sizeof(vcap->bus_info));
Hans Verkuil9d593442012-05-08 17:00:43 -0300574 vcap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
575 vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
577}
578
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300579static int cam_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300581 if (vin->index > 0)
582 return -EINVAL;
583 strlcpy(vin->name, "Camera", sizeof(vin->name));
584 vin->type = V4L2_INPUT_TYPE_CAMERA;
585 vin->audioset = 0;
586 vin->tuner = 0;
587 vin->std = 0;
588 vin->status = 0;
589 return 0;
590}
591
592static int cam_g_input(struct file *file, void *fh, unsigned int *inp)
593{
594 *inp = 0;
595 return 0;
596}
597
598static int cam_s_input(struct file *file, void *fh, unsigned int inp)
599{
600 return (inp > 0) ? -EINVAL : 0;
601}
602
Hans Verkuil9d593442012-05-08 17:00:43 -0300603static int cam_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300604{
Hans Verkuil9d593442012-05-08 17:00:43 -0300605 struct w9966 *cam =
606 container_of(ctrl->handler, struct w9966, hdl);
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300607 int ret = 0;
608
609 mutex_lock(&cam->lock);
610 switch (ctrl->id) {
611 case V4L2_CID_BRIGHTNESS:
Hans Verkuil9d593442012-05-08 17:00:43 -0300612 cam->brightness = ctrl->val;
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300613 break;
614 case V4L2_CID_CONTRAST:
Hans Verkuil9d593442012-05-08 17:00:43 -0300615 cam->contrast = ctrl->val;
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300616 break;
617 case V4L2_CID_SATURATION:
Hans Verkuil9d593442012-05-08 17:00:43 -0300618 cam->color = ctrl->val;
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300619 break;
620 case V4L2_CID_HUE:
Hans Verkuil9d593442012-05-08 17:00:43 -0300621 cam->hue = ctrl->val;
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300622 break;
623 default:
624 ret = -EINVAL;
625 break;
626 }
627
628 if (ret == 0) {
629 w9966_pdev_claim(cam);
630
631 if (w9966_write_reg_i2c(cam, 0x0a, cam->brightness) == -1 ||
632 w9966_write_reg_i2c(cam, 0x0b, cam->contrast) == -1 ||
633 w9966_write_reg_i2c(cam, 0x0c, cam->color) == -1 ||
634 w9966_write_reg_i2c(cam, 0x0d, cam->hue) == -1) {
635 ret = -EIO;
636 }
637
638 w9966_pdev_release(cam);
639 }
640 mutex_unlock(&cam->lock);
641 return ret;
642}
643
644static int cam_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
645{
646 struct w9966 *cam = video_drvdata(file);
647 struct v4l2_pix_format *pix = &fmt->fmt.pix;
648
649 pix->width = cam->width;
650 pix->height = cam->height;
651 pix->pixelformat = V4L2_PIX_FMT_YUYV;
652 pix->field = V4L2_FIELD_NONE;
653 pix->bytesperline = 2 * cam->width;
654 pix->sizeimage = 2 * cam->width * cam->height;
655 /* Just a guess */
656 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
657 return 0;
658}
659
660static int cam_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
661{
662 struct v4l2_pix_format *pix = &fmt->fmt.pix;
663
664 if (pix->width < 2)
665 pix->width = 2;
666 if (pix->height < 1)
667 pix->height = 1;
668 if (pix->width > W9966_WND_MAX_W)
669 pix->width = W9966_WND_MAX_W;
670 if (pix->height > W9966_WND_MAX_H)
671 pix->height = W9966_WND_MAX_H;
672 pix->pixelformat = V4L2_PIX_FMT_YUYV;
673 pix->field = V4L2_FIELD_NONE;
674 pix->bytesperline = 2 * pix->width;
675 pix->sizeimage = 2 * pix->width * pix->height;
676 /* Just a guess */
677 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
678 return 0;
679}
680
681static int cam_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
682{
683 struct w9966 *cam = video_drvdata(file);
684 struct v4l2_pix_format *pix = &fmt->fmt.pix;
685 int ret = cam_try_fmt_vid_cap(file, fh, fmt);
686
687 if (ret)
688 return ret;
689
690 mutex_lock(&cam->lock);
691 /* Update camera regs */
692 w9966_pdev_claim(cam);
693 ret = w9966_setup(cam, 0, 0, 1023, 1023, pix->width, pix->height);
694 w9966_pdev_release(cam);
695 mutex_unlock(&cam->lock);
696 return ret;
697}
698
699static int cam_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
700{
701 static struct v4l2_fmtdesc formats[] = {
702 { 0, 0, 0,
703 "YUV 4:2:2", V4L2_PIX_FMT_YUYV,
704 { 0, 0, 0, 0 }
705 },
706 };
707 enum v4l2_buf_type type = fmt->type;
708
709 if (fmt->index > 0)
710 return -EINVAL;
711
712 *fmt = formats[fmt->index];
713 fmt->type = type;
714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300717/* Capture data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static ssize_t w9966_v4l_read(struct file *file, char __user *buf,
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300719 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300721 struct w9966 *cam = video_drvdata(file);
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300722 unsigned char addr = 0xa0; /* ECP, read, CCD-transfer, 00000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned char __user *dest = (unsigned char __user *)buf;
724 unsigned long dleft = count;
725 unsigned char *tbuf;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300726
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300727 /* Why would anyone want more than this?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (count > cam->width * cam->height * 2)
729 return -EINVAL;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300730
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300731 mutex_lock(&cam->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 w9966_pdev_claim(cam);
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300733 w9966_write_reg(cam, 0x00, 0x02); /* Reset ECP-FIFO buffer */
734 w9966_write_reg(cam, 0x00, 0x00); /* Return to normal operation */
735 w9966_write_reg(cam, 0x01, 0x98); /* Enable capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300737 /* write special capture-addr and negotiate into data transfer */
738 if ((parport_negotiate(cam->pport, cam->ppmode|IEEE1284_ADDR) != 0) ||
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300739 (parport_write(cam->pport, &addr, 1) != 1) ||
740 (parport_negotiate(cam->pport, cam->ppmode|IEEE1284_DATA) != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 w9966_pdev_release(cam);
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300742 mutex_unlock(&cam->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -EFAULT;
744 }
745
746 tbuf = kmalloc(W9966_RBUFFER, GFP_KERNEL);
747 if (tbuf == NULL) {
748 count = -ENOMEM;
749 goto out;
750 }
751
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300752 while (dleft > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 unsigned long tsize = (dleft > W9966_RBUFFER) ? W9966_RBUFFER : dleft;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (parport_read(cam->pport, tbuf, tsize) < tsize) {
756 count = -EFAULT;
757 goto out;
758 }
759 if (copy_to_user(dest, tbuf, tsize) != 0) {
760 count = -EFAULT;
761 goto out;
762 }
763 dest += tsize;
764 dleft -= tsize;
765 }
766
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300767 w9966_write_reg(cam, 0x01, 0x18); /* Disable capture */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769out:
770 kfree(tbuf);
771 w9966_pdev_release(cam);
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300772 mutex_unlock(&cam->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 return count;
775}
776
Hans Verkuil271922c2010-03-22 05:13:17 -0300777static const struct v4l2_file_operations w9966_fops = {
778 .owner = THIS_MODULE,
Hans Verkuil9d593442012-05-08 17:00:43 -0300779 .open = v4l2_fh_open,
780 .release = v4l2_fh_release,
781 .poll = v4l2_ctrl_poll,
Hans Verkuil61df3c92010-11-14 10:09:38 -0300782 .unlocked_ioctl = video_ioctl2,
Hans Verkuil271922c2010-03-22 05:13:17 -0300783 .read = w9966_v4l_read,
784};
785
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300786static const struct v4l2_ioctl_ops w9966_ioctl_ops = {
787 .vidioc_querycap = cam_querycap,
788 .vidioc_g_input = cam_g_input,
789 .vidioc_s_input = cam_s_input,
790 .vidioc_enum_input = cam_enum_input,
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300791 .vidioc_enum_fmt_vid_cap = cam_enum_fmt_vid_cap,
792 .vidioc_g_fmt_vid_cap = cam_g_fmt_vid_cap,
793 .vidioc_s_fmt_vid_cap = cam_s_fmt_vid_cap,
794 .vidioc_try_fmt_vid_cap = cam_try_fmt_vid_cap,
Hans Verkuil9d593442012-05-08 17:00:43 -0300795 .vidioc_log_status = v4l2_ctrl_log_status,
796 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
797 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
798};
799
800static const struct v4l2_ctrl_ops cam_ctrl_ops = {
801 .s_ctrl = cam_s_ctrl,
Hans Verkuil271922c2010-03-22 05:13:17 -0300802};
803
804
805/* Initialize camera device. Setup all internal flags, set a
806 default video mode, setup ccd-chip, register v4l device etc..
807 Also used for 'probing' of hardware.
808 -1 on error */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300809static int w9966_init(struct w9966 *cam, struct parport *port)
Hans Verkuil271922c2010-03-22 05:13:17 -0300810{
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300811 struct v4l2_device *v4l2_dev = &cam->v4l2_dev;
812
Hans Verkuil271922c2010-03-22 05:13:17 -0300813 if (cam->dev_state != 0)
814 return -1;
815
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300816 strlcpy(v4l2_dev->name, "w9966", sizeof(v4l2_dev->name));
817
818 if (v4l2_device_register(NULL, v4l2_dev) < 0) {
819 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
820 return -1;
821 }
Hans Verkuil9d593442012-05-08 17:00:43 -0300822
823 v4l2_ctrl_handler_init(&cam->hdl, 4);
824 v4l2_ctrl_new_std(&cam->hdl, &cam_ctrl_ops,
825 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
826 v4l2_ctrl_new_std(&cam->hdl, &cam_ctrl_ops,
827 V4L2_CID_CONTRAST, -64, 64, 1, 64);
828 v4l2_ctrl_new_std(&cam->hdl, &cam_ctrl_ops,
829 V4L2_CID_SATURATION, -64, 64, 1, 64);
830 v4l2_ctrl_new_std(&cam->hdl, &cam_ctrl_ops,
831 V4L2_CID_HUE, -128, 127, 1, 0);
832 if (cam->hdl.error) {
833 v4l2_err(v4l2_dev, "couldn't register controls\n");
834 return -1;
835 }
Hans Verkuil271922c2010-03-22 05:13:17 -0300836 cam->pport = port;
837 cam->brightness = 128;
838 cam->contrast = 64;
839 cam->color = 64;
840 cam->hue = 0;
841
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300842 /* Select requested transfer mode */
Hans Verkuil271922c2010-03-22 05:13:17 -0300843 switch (parmode) {
844 default: /* Auto-detect (priority: hw-ecp, hw-epp, sw-ecp) */
845 case 0:
846 if (port->modes & PARPORT_MODE_ECP)
847 cam->ppmode = IEEE1284_MODE_ECP;
848 else if (port->modes & PARPORT_MODE_EPP)
849 cam->ppmode = IEEE1284_MODE_EPP;
850 else
851 cam->ppmode = IEEE1284_MODE_ECP;
852 break;
853 case 1: /* hw- or sw-ecp */
854 cam->ppmode = IEEE1284_MODE_ECP;
855 break;
856 case 2: /* hw- or sw-epp */
857 cam->ppmode = IEEE1284_MODE_EPP;
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300858 break;
Hans Verkuil271922c2010-03-22 05:13:17 -0300859 }
860
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300861 /* Tell the parport driver that we exists */
Hans Verkuil271922c2010-03-22 05:13:17 -0300862 cam->pdev = parport_register_device(port, "w9966", NULL, NULL, NULL, 0, NULL);
863 if (cam->pdev == NULL) {
864 DPRINTF("parport_register_device() failed\n");
865 return -1;
866 }
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300867 w9966_set_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV);
Hans Verkuil271922c2010-03-22 05:13:17 -0300868
869 w9966_pdev_claim(cam);
870
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300871 /* Setup a default capture mode */
Hans Verkuil271922c2010-03-22 05:13:17 -0300872 if (w9966_setup(cam, 0, 0, 1023, 1023, 200, 160) != 0) {
873 DPRINTF("w9966_setup() failed.\n");
874 return -1;
875 }
876
877 w9966_pdev_release(cam);
878
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300879 /* Fill in the video_device struct and register us to v4l */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300880 strlcpy(cam->vdev.name, W9966_DRIVERNAME, sizeof(cam->vdev.name));
881 cam->vdev.v4l2_dev = v4l2_dev;
882 cam->vdev.fops = &w9966_fops;
883 cam->vdev.ioctl_ops = &w9966_ioctl_ops;
884 cam->vdev.release = video_device_release_empty;
Hans Verkuil9d593442012-05-08 17:00:43 -0300885 cam->vdev.ctrl_handler = &cam->hdl;
886 set_bit(V4L2_FL_USE_FH_PRIO, &cam->vdev.flags);
Hans Verkuil271922c2010-03-22 05:13:17 -0300887 video_set_drvdata(&cam->vdev, cam);
888
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300889 mutex_init(&cam->lock);
890
Hans Verkuil271922c2010-03-22 05:13:17 -0300891 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
892 return -1;
893
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300894 w9966_set_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV);
Hans Verkuil271922c2010-03-22 05:13:17 -0300895
896 /* All ok */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300897 v4l2_info(v4l2_dev, "Found and initialized a webcam on %s.\n",
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300898 cam->pport->name);
Hans Verkuil271922c2010-03-22 05:13:17 -0300899 return 0;
900}
901
902
903/* Terminate everything gracefully */
Hans Verkuil626e2ac2010-04-06 11:36:39 -0300904static void w9966_term(struct w9966 *cam)
Hans Verkuil271922c2010-03-22 05:13:17 -0300905{
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300906 /* Unregister from v4l */
907 if (w9966_get_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV)) {
Hans Verkuil271922c2010-03-22 05:13:17 -0300908 video_unregister_device(&cam->vdev);
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300909 w9966_set_state(cam, W9966_STATE_VDEV, 0);
Hans Verkuil271922c2010-03-22 05:13:17 -0300910 }
911
Hans Verkuil9d593442012-05-08 17:00:43 -0300912 v4l2_ctrl_handler_free(&cam->hdl);
913
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300914 /* Terminate from IEEE1284 mode and release pdev block */
915 if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
Hans Verkuil271922c2010-03-22 05:13:17 -0300916 w9966_pdev_claim(cam);
917 parport_negotiate(cam->pport, IEEE1284_MODE_COMPAT);
918 w9966_pdev_release(cam);
919 }
920
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300921 /* Unregister from parport */
922 if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
Hans Verkuil271922c2010-03-22 05:13:17 -0300923 parport_unregister_device(cam->pdev);
Hans Verkuilc1f2b0f2010-03-22 05:15:14 -0300924 w9966_set_state(cam, W9966_STATE_PDEV, 0);
Hans Verkuil271922c2010-03-22 05:13:17 -0300925 }
Hans Verkuilbd5ba3b2010-12-31 11:27:38 -0300926 memset(cam, 0, sizeof(*cam));
Hans Verkuil271922c2010-03-22 05:13:17 -0300927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300930/* Called once for every parport on init */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931static void w9966_attach(struct parport *port)
932{
933 int i;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300934
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300935 for (i = 0; i < W9966_MAXCAMS; i++) {
936 if (w9966_cams[i].dev_state != 0) /* Cam is already assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 continue;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300938 if (strcmp(pardev[i], "aggressive") == 0 || strcmp(pardev[i], port->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (w9966_init(&w9966_cams[i], port) != 0)
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300940 w9966_term(&w9966_cams[i]);
941 break; /* return */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 }
944}
945
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300946/* Called once for every parport on termination */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947static void w9966_detach(struct parport *port)
948{
949 int i;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 for (i = 0; i < W9966_MAXCAMS; i++)
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300952 if (w9966_cams[i].dev_state != 0 && w9966_cams[i].pport == port)
953 w9966_term(&w9966_cams[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956
957static struct parport_driver w9966_ppd = {
958 .name = W9966_DRIVERNAME,
959 .attach = w9966_attach,
960 .detach = w9966_detach,
961};
962
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300963/* Module entry point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964static int __init w9966_mod_init(void)
965{
966 int i;
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 for (i = 0; i < W9966_MAXCAMS; i++)
969 w9966_cams[i].dev_state = 0;
970
971 return parport_register_driver(&w9966_ppd);
972}
973
Hans Verkuil4bfdd582010-03-22 04:47:27 -0300974/* Module cleanup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975static void __exit w9966_mod_term(void)
976{
977 parport_unregister_driver(&w9966_ppd);
978}
979
980module_init(w9966_mod_init);
981module_exit(w9966_mod_term);