Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Winbond w9966cf Webcam parport driver. |
| 3 | |
| 4 | Version 0.32 |
| 5 | |
| 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 Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | *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 Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | Changes: |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | 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> |
Mauro Carvalho Chehab | 7e0a16f | 2009-03-10 05:31:34 -0300 | [diff] [blame] | 60 | #include <linux/videodev.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 61 | #include <linux/slab.h> |
Mauro Carvalho Chehab | 5e87efa | 2006-06-05 10:26:32 -0300 | [diff] [blame] | 62 | #include <media/v4l2-common.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 63 | #include <media/v4l2-ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <linux/parport.h> |
| 65 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 66 | /*#define DEBUG*/ /* Undef me for production */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | #ifdef DEBUG |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 69 | #define DPRINTF(x, a...) printk(KERN_DEBUG "W9966: %s(): "x, __func__ , ##a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #else |
| 71 | #define DPRINTF(x...) |
| 72 | #endif |
| 73 | |
| 74 | /* |
| 75 | * Defines, simple typedefs etc. |
| 76 | */ |
| 77 | |
| 78 | #define W9966_DRIVERNAME "W9966CF Webcam" |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 79 | #define W9966_MAXCAMS 4 /* Maximum number of cameras */ |
| 80 | #define W9966_RBUFFER 2048 /* Read buffer (must be an even number) */ |
| 81 | #define W9966_SRAMSIZE 131072 /* 128kb */ |
| 82 | #define W9966_SRAMID 0x02 /* check w9966cf.pdf */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 84 | /* Empirically determined window limits */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #define W9966_WND_MIN_X 16 |
| 86 | #define W9966_WND_MIN_Y 14 |
| 87 | #define W9966_WND_MAX_X 705 |
| 88 | #define W9966_WND_MAX_Y 253 |
| 89 | #define W9966_WND_MAX_W (W9966_WND_MAX_X - W9966_WND_MIN_X) |
| 90 | #define W9966_WND_MAX_H (W9966_WND_MAX_Y - W9966_WND_MIN_Y) |
| 91 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 92 | /* Keep track of our current state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #define W9966_STATE_PDEV 0x01 |
| 94 | #define W9966_STATE_CLAIMED 0x02 |
| 95 | #define W9966_STATE_VDEV 0x04 |
| 96 | |
| 97 | #define W9966_I2C_W_ID 0x48 |
| 98 | #define W9966_I2C_R_ID 0x49 |
| 99 | #define W9966_I2C_R_DATA 0x08 |
| 100 | #define W9966_I2C_R_CLOCK 0x04 |
| 101 | #define W9966_I2C_W_DATA 0x02 |
| 102 | #define W9966_I2C_W_CLOCK 0x01 |
| 103 | |
| 104 | struct w9966_dev { |
| 105 | unsigned char dev_state; |
| 106 | unsigned char i2c_state; |
| 107 | unsigned short ppmode; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 108 | struct parport *pport; |
| 109 | struct pardevice *pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | struct video_device vdev; |
| 111 | unsigned short width; |
| 112 | unsigned short height; |
| 113 | unsigned char brightness; |
| 114 | signed char contrast; |
| 115 | signed char color; |
| 116 | signed char hue; |
Hans Verkuil | 7d43cd5 | 2008-08-23 05:31:47 -0300 | [diff] [blame] | 117 | unsigned long in_use; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* |
| 121 | * Module specific properties |
| 122 | */ |
| 123 | |
| 124 | MODULE_AUTHOR("Jakob Kemi <jakob.kemi@post.utfors.se>"); |
| 125 | MODULE_DESCRIPTION("Winbond w9966cf WebCam driver (0.32)"); |
| 126 | MODULE_LICENSE("GPL"); |
| 127 | |
| 128 | |
| 129 | #ifdef MODULE |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 130 | static const char *pardev[] = {[0 ... W9966_MAXCAMS] = ""}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | #else |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 132 | static const char *pardev[] = {[0 ... W9966_MAXCAMS] = "aggressive"}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | #endif |
| 134 | module_param_array(pardev, charp, NULL, 0); |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 135 | MODULE_PARM_DESC(pardev, "pardev: where to search for\n" |
| 136 | "\teach camera. 'aggressive' means brute-force search.\n" |
| 137 | "\tEg: >pardev=parport3,aggressive,parport2,parport1< would assign\n" |
| 138 | "\tcam 1 to parport3 and search every parport for cam 2 etc..."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 140 | static int parmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | module_param(parmode, int, 0); |
| 142 | MODULE_PARM_DESC(parmode, "parmode: transfer mode (0=auto, 1=ecp, 2=epp"); |
| 143 | |
| 144 | static int video_nr = -1; |
| 145 | module_param(video_nr, int, 0); |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | static struct w9966_dev w9966_cams[W9966_MAXCAMS]; |
| 148 | |
| 149 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | * Private function defines |
| 151 | */ |
| 152 | |
| 153 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 154 | /* Set camera phase flags, so we know what to uninit when terminating */ |
| 155 | static inline void w9966_setState(struct w9966_dev *cam, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | cam->dev_state = (cam->dev_state & ~mask) ^ val; |
| 158 | } |
| 159 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 160 | /* Get camera phase flags */ |
| 161 | static inline int w9966_getState(struct w9966_dev *cam, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
| 163 | return ((cam->dev_state & mask) == val); |
| 164 | } |
| 165 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 166 | /* Claim parport for ourself */ |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 167 | static void w9966_pdev_claim(struct w9966_dev *cam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | if (w9966_getState(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED)) |
| 170 | return; |
| 171 | parport_claim_or_block(cam->pdev); |
| 172 | w9966_setState(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED); |
| 173 | } |
| 174 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 175 | /* Release parport for others to use */ |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 176 | static void w9966_pdev_release(struct w9966_dev *cam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
| 178 | if (w9966_getState(cam, W9966_STATE_CLAIMED, 0)) |
| 179 | return; |
| 180 | parport_release(cam->pdev); |
| 181 | w9966_setState(cam, W9966_STATE_CLAIMED, 0); |
| 182 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 183 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 184 | /* Read register from W9966 interface-chip |
| 185 | Expects a claimed pdev |
| 186 | -1 on error, else register data (byte) */ |
| 187 | static int w9966_rReg(struct w9966_dev *cam, int reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 189 | /* ECP, read, regtransfer, REG, REG, REG, REG, REG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | const unsigned char addr = 0x80 | (reg & 0x1f); |
| 191 | unsigned char val; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0) |
| 194 | return -1; |
| 195 | if (parport_write(cam->pport, &addr, 1) != 1) |
| 196 | return -1; |
| 197 | if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0) |
| 198 | return -1; |
| 199 | if (parport_read(cam->pport, &val, 1) != 1) |
| 200 | return -1; |
| 201 | |
| 202 | return val; |
| 203 | } |
| 204 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 205 | /* Write register to W9966 interface-chip |
| 206 | Expects a claimed pdev |
| 207 | -1 on error */ |
| 208 | static int w9966_wReg(struct w9966_dev *cam, int reg, int data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 210 | /* ECP, write, regtransfer, REG, REG, REG, REG, REG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | const unsigned char addr = 0xc0 | (reg & 0x1f); |
| 212 | const unsigned char val = data; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0) |
| 215 | return -1; |
| 216 | if (parport_write(cam->pport, &addr, 1) != 1) |
| 217 | return -1; |
| 218 | if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0) |
| 219 | return -1; |
| 220 | if (parport_write(cam->pport, &val, 1) != 1) |
| 221 | return -1; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 226 | /* |
| 227 | * Ugly and primitive i2c protocol functions |
| 228 | */ |
| 229 | |
| 230 | /* Sets the data line on the i2c bus. |
| 231 | Expects a claimed pdev. */ |
| 232 | static void w9966_i2c_setsda(struct w9966_dev *cam, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 234 | if (state) |
| 235 | cam->i2c_state |= W9966_I2C_W_DATA; |
| 236 | else |
| 237 | cam->i2c_state &= ~W9966_I2C_W_DATA; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 238 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 239 | w9966_wReg(cam, 0x18, cam->i2c_state); |
| 240 | udelay(5); |
| 241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 243 | /* Get peripheral clock line |
| 244 | Expects a claimed pdev. */ |
| 245 | static int w9966_i2c_getscl(struct w9966_dev *cam) |
| 246 | { |
| 247 | const unsigned char state = w9966_rReg(cam, 0x18); |
| 248 | return ((state & W9966_I2C_R_CLOCK) > 0); |
| 249 | } |
| 250 | |
| 251 | /* Sets the clock line on the i2c bus. |
| 252 | Expects a claimed pdev. -1 on error */ |
| 253 | static int w9966_i2c_setscl(struct w9966_dev *cam, int state) |
| 254 | { |
| 255 | unsigned long timeout; |
| 256 | |
| 257 | if (state) |
| 258 | cam->i2c_state |= W9966_I2C_W_CLOCK; |
| 259 | else |
| 260 | cam->i2c_state &= ~W9966_I2C_W_CLOCK; |
| 261 | |
| 262 | w9966_wReg(cam, 0x18, cam->i2c_state); |
| 263 | udelay(5); |
| 264 | |
| 265 | /* we go to high, we also expect the peripheral to ack. */ |
| 266 | if (state) { |
| 267 | timeout = jiffies + 100; |
| 268 | while (!w9966_i2c_getscl(cam)) { |
| 269 | if (time_after(jiffies, timeout)) |
| 270 | return -1; |
| 271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 276 | #if 0 |
| 277 | /* Get peripheral data line |
| 278 | Expects a claimed pdev. */ |
| 279 | static int w9966_i2c_getsda(struct w9966_dev *cam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 281 | const unsigned char state = w9966_rReg(cam, 0x18); |
| 282 | return ((state & W9966_I2C_R_DATA) > 0); |
| 283 | } |
| 284 | #endif |
| 285 | |
| 286 | /* Write a byte with ack to the i2c bus. |
| 287 | Expects a claimed pdev. -1 on error */ |
| 288 | static int w9966_i2c_wbyte(struct w9966_dev *cam, int data) |
| 289 | { |
| 290 | int i; |
| 291 | |
| 292 | for (i = 7; i >= 0; i--) { |
| 293 | w9966_i2c_setsda(cam, (data >> i) & 0x01); |
| 294 | |
| 295 | if (w9966_i2c_setscl(cam, 1) == -1) |
| 296 | return -1; |
| 297 | w9966_i2c_setscl(cam, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 300 | w9966_i2c_setsda(cam, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 302 | if (w9966_i2c_setscl(cam, 1) == -1) |
| 303 | return -1; |
| 304 | w9966_i2c_setscl(cam, 0); |
| 305 | |
| 306 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 309 | /* Read a data byte with ack from the i2c-bus |
| 310 | Expects a claimed pdev. -1 on error */ |
| 311 | #if 0 |
| 312 | static int w9966_i2c_rbyte(struct w9966_dev *cam) |
| 313 | { |
| 314 | unsigned char data = 0x00; |
| 315 | int i; |
| 316 | |
| 317 | w9966_i2c_setsda(cam, 1); |
| 318 | |
| 319 | for (i = 0; i < 8; i++) { |
| 320 | if (w9966_i2c_setscl(cam, 1) == -1) |
| 321 | return -1; |
| 322 | data = data << 1; |
| 323 | if (w9966_i2c_getsda(cam)) |
| 324 | data |= 0x01; |
| 325 | |
| 326 | w9966_i2c_setscl(cam, 0); |
| 327 | } |
| 328 | return data; |
| 329 | } |
| 330 | #endif |
| 331 | |
| 332 | /* Read a register from the i2c device. |
| 333 | Expects claimed pdev. -1 on error */ |
| 334 | #if 0 |
| 335 | static int w9966_rReg_i2c(struct w9966_dev *cam, int reg) |
| 336 | { |
| 337 | int data; |
| 338 | |
| 339 | w9966_i2c_setsda(cam, 0); |
| 340 | w9966_i2c_setscl(cam, 0); |
| 341 | |
| 342 | if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 || |
| 343 | w9966_i2c_wbyte(cam, reg) == -1) |
| 344 | return -1; |
| 345 | |
| 346 | w9966_i2c_setsda(cam, 1); |
| 347 | if (w9966_i2c_setscl(cam, 1) == -1) |
| 348 | return -1; |
| 349 | w9966_i2c_setsda(cam, 0); |
| 350 | w9966_i2c_setscl(cam, 0); |
| 351 | |
| 352 | if (w9966_i2c_wbyte(cam, W9966_I2C_R_ID) == -1) |
| 353 | return -1; |
| 354 | data = w9966_i2c_rbyte(cam); |
| 355 | if (data == -1) |
| 356 | return -1; |
| 357 | |
| 358 | w9966_i2c_setsda(cam, 0); |
| 359 | |
| 360 | if (w9966_i2c_setscl(cam, 1) == -1) |
| 361 | return -1; |
| 362 | w9966_i2c_setsda(cam, 1); |
| 363 | |
| 364 | return data; |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | /* Write a register to the i2c device. |
| 369 | Expects claimed pdev. -1 on error */ |
| 370 | static int w9966_wReg_i2c(struct w9966_dev *cam, int reg, int data) |
| 371 | { |
| 372 | w9966_i2c_setsda(cam, 0); |
| 373 | w9966_i2c_setscl(cam, 0); |
| 374 | |
| 375 | if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 || |
| 376 | w9966_i2c_wbyte(cam, reg) == -1 || |
| 377 | w9966_i2c_wbyte(cam, data) == -1) |
| 378 | return -1; |
| 379 | |
| 380 | w9966_i2c_setsda(cam, 0); |
| 381 | if (w9966_i2c_setscl(cam, 1) == -1) |
| 382 | return -1; |
| 383 | |
| 384 | w9966_i2c_setsda(cam, 1); |
| 385 | |
| 386 | return 0; |
| 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 389 | /* Find a good length for capture window (used both for W and H) |
| 390 | A bit ugly but pretty functional. The capture length |
| 391 | have to match the downscale */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | static int w9966_findlen(int near, int size, int maxlen) |
| 393 | { |
| 394 | int bestlen = size; |
| 395 | int besterr = abs(near - bestlen); |
| 396 | int len; |
| 397 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 398 | for (len = size + 1; len < maxlen; len++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | int err; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 400 | if (((64 * size) % len) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | continue; |
| 402 | |
| 403 | err = abs(near - len); |
| 404 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 405 | /* Only continue as long as we keep getting better values */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | if (err > besterr) |
| 407 | break; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | besterr = err; |
| 410 | bestlen = len; |
| 411 | } |
| 412 | |
| 413 | return bestlen; |
| 414 | } |
| 415 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 416 | /* Modify capture window (if necessary) |
| 417 | and calculate downscaling |
| 418 | Return -1 on error */ |
| 419 | static int w9966_calcscale(int size, int min, int max, int *beg, int *end, unsigned char *factor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
| 421 | int maxlen = max - min; |
| 422 | int len = *end - *beg + 1; |
| 423 | int newlen = w9966_findlen(len, size, maxlen); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 424 | int err = newlen - len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 426 | /* Check for bad format */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (newlen > maxlen || newlen < size) |
| 428 | return -1; |
| 429 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 430 | /* Set factor (6 bit fixed) */ |
| 431 | *factor = (64 * size) / newlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | if (*factor == 64) |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 433 | *factor = 0x00; /* downscale is disabled */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | else |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 435 | *factor |= 0x80; /* set downscale-enable bit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 437 | /* Modify old beginning and end */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | *beg -= err / 2; |
| 439 | *end += err - (err / 2); |
| 440 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 441 | /* Move window if outside borders */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (*beg < min) { |
| 443 | *end += min - *beg; |
| 444 | *beg += min - *beg; |
| 445 | } |
| 446 | if (*end > max) { |
| 447 | *beg -= *end - max; |
| 448 | *end -= *end - max; |
| 449 | } |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 454 | /* Setup the cameras capture window etc. |
| 455 | Expects a claimed pdev |
| 456 | return -1 on error */ |
| 457 | static int w9966_setup(struct w9966_dev *cam, int x1, int y1, int x2, int y2, int w, int h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
| 459 | unsigned int i; |
| 460 | unsigned int enh_s, enh_e; |
| 461 | unsigned char scale_x, scale_y; |
| 462 | unsigned char regs[0x1c]; |
| 463 | unsigned char saa7111_regs[] = { |
| 464 | 0x21, 0x00, 0xd8, 0x23, 0x00, 0x80, 0x80, 0x00, |
| 465 | 0x88, 0x10, 0x80, 0x40, 0x40, 0x00, 0x01, 0x00, |
| 466 | 0x48, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 467 | 0x00, 0x00, 0x00, 0x71, 0xe7, 0x00, 0x00, 0xc0 |
| 468 | }; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 469 | |
| 470 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 471 | if (w * h * 2 > W9966_SRAMSIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | DPRINTF("capture window exceeds SRAM size!.\n"); |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 473 | w = 200; h = 160; /* Pick default values */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | w &= ~0x1; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 477 | if (w < 2) |
| 478 | w = 2; |
| 479 | if (h < 1) |
| 480 | h = 1; |
| 481 | if (w > W9966_WND_MAX_W) |
| 482 | w = W9966_WND_MAX_W; |
| 483 | if (h > W9966_WND_MAX_H) |
| 484 | h = W9966_WND_MAX_H; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
| 486 | cam->width = w; |
| 487 | cam->height = h; |
| 488 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 489 | enh_s = 0; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 490 | enh_e = w * h * 2; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 491 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 492 | /* Modify capture window if necessary and calculate downscaling */ |
| 493 | if (w9966_calcscale(w, W9966_WND_MIN_X, W9966_WND_MAX_X, &x1, &x2, &scale_x) != 0 || |
| 494 | w9966_calcscale(h, W9966_WND_MIN_Y, W9966_WND_MAX_Y, &y1, &y2, &scale_y) != 0) |
| 495 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 497 | DPRINTF("%dx%d, x: %d<->%d, y: %d<->%d, sx: %d/64, sy: %d/64.\n", |
| 498 | w, h, x1, x2, y1, y2, scale_x & ~0x80, scale_y & ~0x80); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 499 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 500 | /* Setup registers */ |
| 501 | regs[0x00] = 0x00; /* Set normal operation */ |
| 502 | regs[0x01] = 0x18; /* Capture mode */ |
| 503 | regs[0x02] = scale_y; /* V-scaling */ |
| 504 | regs[0x03] = scale_x; /* H-scaling */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 505 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 506 | /* Capture window */ |
| 507 | regs[0x04] = (x1 & 0x0ff); /* X-start (8 low bits) */ |
| 508 | regs[0x05] = (x1 & 0x300)>>8; /* X-start (2 high bits) */ |
| 509 | regs[0x06] = (y1 & 0x0ff); /* Y-start (8 low bits) */ |
| 510 | regs[0x07] = (y1 & 0x300)>>8; /* Y-start (2 high bits) */ |
| 511 | regs[0x08] = (x2 & 0x0ff); /* X-end (8 low bits) */ |
| 512 | regs[0x09] = (x2 & 0x300)>>8; /* X-end (2 high bits) */ |
| 513 | regs[0x0a] = (y2 & 0x0ff); /* Y-end (8 low bits) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 515 | regs[0x0c] = W9966_SRAMID; /* SRAM-banks (1x 128kb) */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 516 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 517 | /* Enhancement layer */ |
| 518 | regs[0x0d] = (enh_s & 0x000ff); /* Enh. start (0-7) */ |
| 519 | regs[0x0e] = (enh_s & 0x0ff00) >> 8; /* Enh. start (8-15) */ |
| 520 | regs[0x0f] = (enh_s & 0x70000) >> 16; /* Enh. start (16-17/18??) */ |
| 521 | regs[0x10] = (enh_e & 0x000ff); /* Enh. end (0-7) */ |
| 522 | regs[0x11] = (enh_e & 0x0ff00) >> 8; /* Enh. end (8-15) */ |
| 523 | regs[0x12] = (enh_e & 0x70000) >> 16; /* Enh. end (16-17/18??) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 525 | /* Misc */ |
| 526 | regs[0x13] = 0x40; /* VEE control (raw 4:2:2) */ |
| 527 | regs[0x17] = 0x00; /* ??? */ |
| 528 | regs[0x18] = cam->i2c_state = 0x00; /* Serial bus */ |
| 529 | regs[0x19] = 0xff; /* I/O port direction control */ |
| 530 | regs[0x1a] = 0xff; /* I/O port data register */ |
| 531 | regs[0x1b] = 0x10; /* ??? */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 532 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 533 | /* SAA7111 chip settings */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | saa7111_regs[0x0a] = cam->brightness; |
| 535 | saa7111_regs[0x0b] = cam->contrast; |
| 536 | saa7111_regs[0x0c] = cam->color; |
| 537 | saa7111_regs[0x0d] = cam->hue; |
| 538 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 539 | /* Reset (ECP-fifo & serial-bus) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | if (w9966_wReg(cam, 0x00, 0x03) == -1) |
| 541 | return -1; |
| 542 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 543 | /* Write regs to w9966cf chip */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | for (i = 0; i < 0x1c; i++) |
| 545 | if (w9966_wReg(cam, i, regs[i]) == -1) |
| 546 | return -1; |
| 547 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 548 | /* Write regs to saa7111 chip */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | for (i = 0; i < 0x20; i++) |
| 550 | if (w9966_wReg_i2c(cam, i, saa7111_regs[i]) == -1) |
| 551 | return -1; |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | * Video4linux interfacing |
| 558 | */ |
| 559 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 560 | static long w9966_v4l_do_ioctl(struct file *file, unsigned int cmd, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 562 | struct w9966_dev *cam = video_drvdata(file); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 563 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 564 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | case VIDIOCGCAP: |
| 566 | { |
| 567 | static struct video_capability vcap = { |
| 568 | .name = W9966_DRIVERNAME, |
| 569 | .type = VID_TYPE_CAPTURE | VID_TYPE_SCALES, |
| 570 | .channels = 1, |
| 571 | .maxwidth = W9966_WND_MAX_W, |
| 572 | .maxheight = W9966_WND_MAX_H, |
| 573 | .minwidth = 2, |
| 574 | .minheight = 1, |
| 575 | }; |
| 576 | struct video_capability *cap = arg; |
| 577 | *cap = vcap; |
| 578 | return 0; |
| 579 | } |
| 580 | case VIDIOCGCHAN: |
| 581 | { |
| 582 | struct video_channel *vch = arg; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 583 | if (vch->channel != 0) /* We only support one channel (#0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | return -EINVAL; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 585 | memset(vch, 0, sizeof(*vch)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | strcpy(vch->name, "CCD-input"); |
| 587 | vch->type = VIDEO_TYPE_CAMERA; |
| 588 | return 0; |
| 589 | } |
| 590 | case VIDIOCSCHAN: |
| 591 | { |
| 592 | struct video_channel *vch = arg; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 593 | if (vch->channel != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | return -EINVAL; |
| 595 | return 0; |
| 596 | } |
| 597 | case VIDIOCGTUNER: |
| 598 | { |
| 599 | struct video_tuner *vtune = arg; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 600 | if (vtune->tuner != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return -EINVAL; |
| 602 | strcpy(vtune->name, "no tuner"); |
| 603 | vtune->rangelow = 0; |
| 604 | vtune->rangehigh = 0; |
| 605 | vtune->flags = VIDEO_TUNER_NORM; |
| 606 | vtune->mode = VIDEO_MODE_AUTO; |
| 607 | vtune->signal = 0xffff; |
| 608 | return 0; |
| 609 | } |
| 610 | case VIDIOCSTUNER: |
| 611 | { |
| 612 | struct video_tuner *vtune = arg; |
| 613 | if (vtune->tuner != 0) |
| 614 | return -EINVAL; |
| 615 | if (vtune->mode != VIDEO_MODE_AUTO) |
| 616 | return -EINVAL; |
| 617 | return 0; |
| 618 | } |
| 619 | case VIDIOCGPICT: |
| 620 | { |
| 621 | struct video_picture vpic = { |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 622 | cam->brightness << 8, /* brightness */ |
| 623 | (cam->hue + 128) << 8, /* hue */ |
| 624 | cam->color << 9, /* color */ |
| 625 | cam->contrast << 9, /* contrast */ |
| 626 | 0x8000, /* whiteness */ |
| 627 | 16, VIDEO_PALETTE_YUV422/* bpp, palette format */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | }; |
| 629 | struct video_picture *pic = arg; |
| 630 | *pic = vpic; |
| 631 | return 0; |
| 632 | } |
| 633 | case VIDIOCSPICT: |
| 634 | { |
| 635 | struct video_picture *vpic = arg; |
audetto@tiscali.it | 2485eb0 | 2006-12-12 10:35:57 -0300 | [diff] [blame] | 636 | if (vpic->depth != 16 || (vpic->palette != VIDEO_PALETTE_YUV422 && vpic->palette != VIDEO_PALETTE_YUYV)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | return -EINVAL; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | cam->brightness = vpic->brightness >> 8; |
| 640 | cam->hue = (vpic->hue >> 8) - 128; |
| 641 | cam->color = vpic->colour >> 9; |
| 642 | cam->contrast = vpic->contrast >> 9; |
| 643 | |
| 644 | w9966_pdev_claim(cam); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if ( |
| 647 | w9966_wReg_i2c(cam, 0x0a, cam->brightness) == -1 || |
| 648 | w9966_wReg_i2c(cam, 0x0b, cam->contrast) == -1 || |
| 649 | w9966_wReg_i2c(cam, 0x0c, cam->color) == -1 || |
| 650 | w9966_wReg_i2c(cam, 0x0d, cam->hue) == -1 |
| 651 | ) { |
| 652 | w9966_pdev_release(cam); |
| 653 | return -EIO; |
| 654 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | w9966_pdev_release(cam); |
| 657 | return 0; |
| 658 | } |
| 659 | case VIDIOCSWIN: |
| 660 | { |
| 661 | int ret; |
| 662 | struct video_window *vwin = arg; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | if (vwin->flags != 0) |
| 665 | return -EINVAL; |
| 666 | if (vwin->clipcount != 0) |
| 667 | return -EINVAL; |
| 668 | if (vwin->width < 2 || vwin->width > W9966_WND_MAX_W) |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 669 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (vwin->height < 1 || vwin->height > W9966_WND_MAX_H) |
| 671 | return -EINVAL; |
| 672 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 673 | /* Update camera regs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | w9966_pdev_claim(cam); |
| 675 | ret = w9966_setup(cam, 0, 0, 1023, 1023, vwin->width, vwin->height); |
| 676 | w9966_pdev_release(cam); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (ret != 0) { |
| 679 | DPRINTF("VIDIOCSWIN: w9966_setup() failed.\n"); |
| 680 | return -EIO; |
| 681 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | return 0; |
| 684 | } |
| 685 | case VIDIOCGWIN: |
| 686 | { |
| 687 | struct video_window *vwin = arg; |
| 688 | memset(vwin, 0, sizeof(*vwin)); |
| 689 | vwin->width = cam->width; |
| 690 | vwin->height = cam->height; |
| 691 | return 0; |
| 692 | } |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 693 | /* Unimplemented */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 694 | case VIDIOCCAPTURE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | case VIDIOCGFBUF: |
| 696 | case VIDIOCSFBUF: |
| 697 | case VIDIOCKEY: |
| 698 | case VIDIOCGFREQ: |
| 699 | case VIDIOCSFREQ: |
| 700 | case VIDIOCGAUDIO: |
| 701 | case VIDIOCSAUDIO: |
| 702 | return -EINVAL; |
| 703 | default: |
| 704 | return -ENOIOCTLCMD; |
| 705 | } |
| 706 | return 0; |
| 707 | } |
| 708 | |
Hans Verkuil | 069b747 | 2008-12-30 07:04:34 -0300 | [diff] [blame] | 709 | static long w9966_v4l_ioctl(struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | unsigned int cmd, unsigned long arg) |
| 711 | { |
Hans Verkuil | f473bf7 | 2008-11-01 08:25:11 -0300 | [diff] [blame] | 712 | return video_usercopy(file, cmd, arg, w9966_v4l_do_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 715 | /* Capture data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | static ssize_t w9966_v4l_read(struct file *file, char __user *buf, |
| 717 | size_t count, loff_t *ppos) |
| 718 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 719 | struct w9966_dev *cam = video_drvdata(file); |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 720 | unsigned char addr = 0xa0; /* ECP, read, CCD-transfer, 00000 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | unsigned char __user *dest = (unsigned char __user *)buf; |
| 722 | unsigned long dleft = count; |
| 723 | unsigned char *tbuf; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 724 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 725 | /* Why would anyone want more than this?? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | if (count > cam->width * cam->height * 2) |
| 727 | return -EINVAL; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | w9966_pdev_claim(cam); |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 730 | w9966_wReg(cam, 0x00, 0x02); /* Reset ECP-FIFO buffer */ |
| 731 | w9966_wReg(cam, 0x00, 0x00); /* Return to normal operation */ |
| 732 | w9966_wReg(cam, 0x01, 0x98); /* Enable capture */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 734 | /* write special capture-addr and negotiate into data transfer */ |
| 735 | if ((parport_negotiate(cam->pport, cam->ppmode|IEEE1284_ADDR) != 0) || |
| 736 | (parport_write(cam->pport, &addr, 1) != 1) || |
| 737 | (parport_negotiate(cam->pport, cam->ppmode|IEEE1284_DATA) != 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | w9966_pdev_release(cam); |
| 739 | return -EFAULT; |
| 740 | } |
| 741 | |
| 742 | tbuf = kmalloc(W9966_RBUFFER, GFP_KERNEL); |
| 743 | if (tbuf == NULL) { |
| 744 | count = -ENOMEM; |
| 745 | goto out; |
| 746 | } |
| 747 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 748 | while (dleft > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | unsigned long tsize = (dleft > W9966_RBUFFER) ? W9966_RBUFFER : dleft; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | if (parport_read(cam->pport, tbuf, tsize) < tsize) { |
| 752 | count = -EFAULT; |
| 753 | goto out; |
| 754 | } |
| 755 | if (copy_to_user(dest, tbuf, tsize) != 0) { |
| 756 | count = -EFAULT; |
| 757 | goto out; |
| 758 | } |
| 759 | dest += tsize; |
| 760 | dleft -= tsize; |
| 761 | } |
| 762 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 763 | w9966_wReg(cam, 0x01, 0x18); /* Disable capture */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | out: |
| 766 | kfree(tbuf); |
| 767 | w9966_pdev_release(cam); |
| 768 | |
| 769 | return count; |
| 770 | } |
| 771 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame^] | 772 | static int w9966_exclusive_open(struct file *file) |
| 773 | { |
| 774 | struct w9966_dev *cam = video_drvdata(file); |
| 775 | |
| 776 | return test_and_set_bit(0, &cam->in_use) ? -EBUSY : 0; |
| 777 | } |
| 778 | |
| 779 | static int w9966_exclusive_release(struct file *file) |
| 780 | { |
| 781 | struct w9966_dev *cam = video_drvdata(file); |
| 782 | |
| 783 | clear_bit(0, &cam->in_use); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static const struct v4l2_file_operations w9966_fops = { |
| 788 | .owner = THIS_MODULE, |
| 789 | .open = w9966_exclusive_open, |
| 790 | .release = w9966_exclusive_release, |
| 791 | .ioctl = w9966_v4l_ioctl, |
| 792 | .read = w9966_v4l_read, |
| 793 | }; |
| 794 | |
| 795 | static struct video_device w9966_template = { |
| 796 | .name = W9966_DRIVERNAME, |
| 797 | .fops = &w9966_fops, |
| 798 | .release = video_device_release_empty, |
| 799 | }; |
| 800 | |
| 801 | |
| 802 | /* Initialize camera device. Setup all internal flags, set a |
| 803 | default video mode, setup ccd-chip, register v4l device etc.. |
| 804 | Also used for 'probing' of hardware. |
| 805 | -1 on error */ |
| 806 | static int w9966_init(struct w9966_dev *cam, struct parport* port) |
| 807 | { |
| 808 | if (cam->dev_state != 0) |
| 809 | return -1; |
| 810 | |
| 811 | cam->pport = port; |
| 812 | cam->brightness = 128; |
| 813 | cam->contrast = 64; |
| 814 | cam->color = 64; |
| 815 | cam->hue = 0; |
| 816 | |
| 817 | /* Select requested transfer mode */ |
| 818 | switch (parmode) { |
| 819 | default: /* Auto-detect (priority: hw-ecp, hw-epp, sw-ecp) */ |
| 820 | case 0: |
| 821 | if (port->modes & PARPORT_MODE_ECP) |
| 822 | cam->ppmode = IEEE1284_MODE_ECP; |
| 823 | else if (port->modes & PARPORT_MODE_EPP) |
| 824 | cam->ppmode = IEEE1284_MODE_EPP; |
| 825 | else |
| 826 | cam->ppmode = IEEE1284_MODE_ECP; |
| 827 | break; |
| 828 | case 1: /* hw- or sw-ecp */ |
| 829 | cam->ppmode = IEEE1284_MODE_ECP; |
| 830 | break; |
| 831 | case 2: /* hw- or sw-epp */ |
| 832 | cam->ppmode = IEEE1284_MODE_EPP; |
| 833 | break; |
| 834 | } |
| 835 | |
| 836 | /* Tell the parport driver that we exists */ |
| 837 | cam->pdev = parport_register_device(port, "w9966", NULL, NULL, NULL, 0, NULL); |
| 838 | if (cam->pdev == NULL) { |
| 839 | DPRINTF("parport_register_device() failed\n"); |
| 840 | return -1; |
| 841 | } |
| 842 | w9966_setState(cam, W9966_STATE_PDEV, W9966_STATE_PDEV); |
| 843 | |
| 844 | w9966_pdev_claim(cam); |
| 845 | |
| 846 | /* Setup a default capture mode */ |
| 847 | if (w9966_setup(cam, 0, 0, 1023, 1023, 200, 160) != 0) { |
| 848 | DPRINTF("w9966_setup() failed.\n"); |
| 849 | return -1; |
| 850 | } |
| 851 | |
| 852 | w9966_pdev_release(cam); |
| 853 | |
| 854 | /* Fill in the video_device struct and register us to v4l */ |
| 855 | memcpy(&cam->vdev, &w9966_template, sizeof(struct video_device)); |
| 856 | video_set_drvdata(&cam->vdev, cam); |
| 857 | |
| 858 | if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) |
| 859 | return -1; |
| 860 | |
| 861 | w9966_setState(cam, W9966_STATE_VDEV, W9966_STATE_VDEV); |
| 862 | |
| 863 | /* All ok */ |
| 864 | printk(KERN_INFO "w9966cf: Found and initialized a webcam on %s.\n", |
| 865 | cam->pport->name); |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | |
| 870 | /* Terminate everything gracefully */ |
| 871 | static void w9966_term(struct w9966_dev *cam) |
| 872 | { |
| 873 | /* Unregister from v4l */ |
| 874 | if (w9966_getState(cam, W9966_STATE_VDEV, W9966_STATE_VDEV)) { |
| 875 | video_unregister_device(&cam->vdev); |
| 876 | w9966_setState(cam, W9966_STATE_VDEV, 0); |
| 877 | } |
| 878 | |
| 879 | /* Terminate from IEEE1284 mode and release pdev block */ |
| 880 | if (w9966_getState(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) { |
| 881 | w9966_pdev_claim(cam); |
| 882 | parport_negotiate(cam->pport, IEEE1284_MODE_COMPAT); |
| 883 | w9966_pdev_release(cam); |
| 884 | } |
| 885 | |
| 886 | /* Unregister from parport */ |
| 887 | if (w9966_getState(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) { |
| 888 | parport_unregister_device(cam->pdev); |
| 889 | w9966_setState(cam, W9966_STATE_PDEV, 0); |
| 890 | } |
| 891 | } |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 894 | /* Called once for every parport on init */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | static void w9966_attach(struct parport *port) |
| 896 | { |
| 897 | int i; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 898 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 899 | for (i = 0; i < W9966_MAXCAMS; i++) { |
| 900 | if (w9966_cams[i].dev_state != 0) /* Cam is already assigned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | continue; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 902 | if (strcmp(pardev[i], "aggressive") == 0 || strcmp(pardev[i], port->name) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | if (w9966_init(&w9966_cams[i], port) != 0) |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 904 | w9966_term(&w9966_cams[i]); |
| 905 | break; /* return */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 910 | /* Called once for every parport on termination */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | static void w9966_detach(struct parport *port) |
| 912 | { |
| 913 | int i; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | for (i = 0; i < W9966_MAXCAMS; i++) |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 916 | if (w9966_cams[i].dev_state != 0 && w9966_cams[i].pport == port) |
| 917 | w9966_term(&w9966_cams[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | |
| 921 | static struct parport_driver w9966_ppd = { |
| 922 | .name = W9966_DRIVERNAME, |
| 923 | .attach = w9966_attach, |
| 924 | .detach = w9966_detach, |
| 925 | }; |
| 926 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 927 | /* Module entry point */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | static int __init w9966_mod_init(void) |
| 929 | { |
| 930 | int i; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | for (i = 0; i < W9966_MAXCAMS; i++) |
| 933 | w9966_cams[i].dev_state = 0; |
| 934 | |
| 935 | return parport_register_driver(&w9966_ppd); |
| 936 | } |
| 937 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 938 | /* Module cleanup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | static void __exit w9966_mod_term(void) |
| 940 | { |
| 941 | parport_unregister_driver(&w9966_ppd); |
| 942 | } |
| 943 | |
| 944 | module_init(w9966_mod_init); |
| 945 | module_exit(w9966_mod_term); |