Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Winbond w9966cf Webcam parport driver. |
| 3 | |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 4 | Version 0.33 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 60 | #include <linux/videodev2.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> |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 64 | #include <media/v4l2-device.h> |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 65 | #include <media/v4l2-fh.h> |
| 66 | #include <media/v4l2-ctrls.h> |
| 67 | #include <media/v4l2-event.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/parport.h> |
| 69 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 70 | /*#define DEBUG*/ /* Undef me for production */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | #ifdef DEBUG |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 73 | #define DPRINTF(x, a...) printk(KERN_DEBUG "W9966: %s(): "x, __func__ , ##a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #else |
| 75 | #define DPRINTF(x...) |
| 76 | #endif |
| 77 | |
| 78 | /* |
| 79 | * Defines, simple typedefs etc. |
| 80 | */ |
| 81 | |
| 82 | #define W9966_DRIVERNAME "W9966CF Webcam" |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 83 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 88 | /* Empirically determined window limits */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #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 Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 96 | /* Keep track of our current state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | #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 Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 108 | struct w9966 { |
| 109 | struct v4l2_device v4l2_dev; |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 110 | struct v4l2_ctrl_handler hdl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | unsigned char dev_state; |
| 112 | unsigned char i2c_state; |
| 113 | unsigned short ppmode; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 114 | struct parport *pport; |
| 115 | struct pardevice *pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | 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 Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 123 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * Module specific properties |
| 128 | */ |
| 129 | |
| 130 | MODULE_AUTHOR("Jakob Kemi <jakob.kemi@post.utfors.se>"); |
| 131 | MODULE_DESCRIPTION("Winbond w9966cf WebCam driver (0.32)"); |
| 132 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 1990d50 | 2011-06-24 14:45:49 -0300 | [diff] [blame] | 133 | MODULE_VERSION("0.33.1"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | #ifdef MODULE |
Mauro Carvalho Chehab | c9e7252 | 2012-03-19 15:37:59 -0300 | [diff] [blame] | 136 | static char *pardev[] = {[0 ... W9966_MAXCAMS] = ""}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | #else |
Mauro Carvalho Chehab | c9e7252 | 2012-03-19 15:37:59 -0300 | [diff] [blame] | 138 | static char *pardev[] = {[0 ... W9966_MAXCAMS] = "aggressive"}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | #endif |
| 140 | module_param_array(pardev, charp, NULL, 0); |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 141 | MODULE_PARM_DESC(pardev, "pardev: where to search for\n" |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 142 | "\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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 146 | static int parmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | module_param(parmode, int, 0); |
| 148 | MODULE_PARM_DESC(parmode, "parmode: transfer mode (0=auto, 1=ecp, 2=epp"); |
| 149 | |
| 150 | static int video_nr = -1; |
| 151 | module_param(video_nr, int, 0); |
| 152 | |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 153 | static struct w9966 w9966_cams[W9966_MAXCAMS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | * Private function defines |
| 157 | */ |
| 158 | |
| 159 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 160 | /* Set camera phase flags, so we know what to uninit when terminating */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 161 | static inline void w9966_set_state(struct w9966 *cam, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
| 163 | cam->dev_state = (cam->dev_state & ~mask) ^ val; |
| 164 | } |
| 165 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 166 | /* Get camera phase flags */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 167 | static inline int w9966_get_state(struct w9966 *cam, int mask, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | return ((cam->dev_state & mask) == val); |
| 170 | } |
| 171 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 172 | /* Claim parport for ourself */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 173 | static void w9966_pdev_claim(struct w9966 *cam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 175 | if (w9966_get_state(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return; |
| 177 | parport_claim_or_block(cam->pdev); |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 178 | w9966_set_state(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 181 | /* Release parport for others to use */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 182 | static void w9966_pdev_release(struct w9966 *cam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 184 | if (w9966_get_state(cam, W9966_STATE_CLAIMED, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return; |
| 186 | parport_release(cam->pdev); |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 187 | w9966_set_state(cam, W9966_STATE_CLAIMED, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 189 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 190 | /* Read register from W9966 interface-chip |
| 191 | Expects a claimed pdev |
| 192 | -1 on error, else register data (byte) */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 193 | static int w9966_read_reg(struct w9966 *cam, int reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 195 | /* ECP, read, regtransfer, REG, REG, REG, REG, REG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | const unsigned char addr = 0x80 | (reg & 0x1f); |
| 197 | unsigned char val; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | 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 Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 211 | /* Write register to W9966 interface-chip |
| 212 | Expects a claimed pdev |
| 213 | -1 on error */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 214 | static int w9966_write_reg(struct w9966 *cam, int reg, int data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 216 | /* ECP, write, regtransfer, REG, REG, REG, REG, REG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | const unsigned char addr = 0xc0 | (reg & 0x1f); |
| 218 | const unsigned char val = data; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | 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 Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 232 | /* |
| 233 | * Ugly and primitive i2c protocol functions |
| 234 | */ |
| 235 | |
| 236 | /* Sets the data line on the i2c bus. |
| 237 | Expects a claimed pdev. */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 238 | static void w9966_i2c_setsda(struct w9966 *cam, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 240 | if (state) |
| 241 | cam->i2c_state |= W9966_I2C_W_DATA; |
| 242 | else |
| 243 | cam->i2c_state &= ~W9966_I2C_W_DATA; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 244 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 245 | w9966_write_reg(cam, 0x18, cam->i2c_state); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 246 | udelay(5); |
| 247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 249 | /* Get peripheral clock line |
| 250 | Expects a claimed pdev. */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 251 | static int w9966_i2c_getscl(struct w9966 *cam) |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 252 | { |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 253 | const unsigned char state = w9966_read_reg(cam, 0x18); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 254 | 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 Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 259 | static int w9966_i2c_setscl(struct w9966 *cam, int state) |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 260 | { |
| 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 Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 268 | w9966_write_reg(cam, 0x18, cam->i2c_state); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 269 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 282 | #if 0 |
| 283 | /* Get peripheral data line |
| 284 | Expects a claimed pdev. */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 285 | static int w9966_i2c_getsda(struct w9966 *cam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 287 | const unsigned char state = w9966_read_reg(cam, 0x18); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 288 | 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 Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 294 | static int w9966_i2c_wbyte(struct w9966 *cam, int data) |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 295 | { |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 306 | w9966_i2c_setsda(cam, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 308 | if (w9966_i2c_setscl(cam, 1) == -1) |
| 309 | return -1; |
| 310 | w9966_i2c_setscl(cam, 0); |
| 311 | |
| 312 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 315 | /* Read a data byte with ack from the i2c-bus |
| 316 | Expects a claimed pdev. -1 on error */ |
| 317 | #if 0 |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 318 | static int w9966_i2c_rbyte(struct w9966 *cam) |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 319 | { |
| 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 Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 341 | static int w9966_read_reg_i2c(struct w9966 *cam, int reg) |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 342 | { |
| 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 Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 376 | static int w9966_write_reg_i2c(struct w9966 *cam, int reg, int data) |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 377 | { |
| 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 Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 382 | w9966_i2c_wbyte(cam, reg) == -1 || |
| 383 | w9966_i2c_wbyte(cam, data) == -1) |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 384 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 395 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | static 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 Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 404 | for (len = size + 1; len < maxlen; len++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | int err; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 406 | if (((64 * size) % len) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | continue; |
| 408 | |
| 409 | err = abs(near - len); |
| 410 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 411 | /* Only continue as long as we keep getting better values */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (err > besterr) |
| 413 | break; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | besterr = err; |
| 416 | bestlen = len; |
| 417 | } |
| 418 | |
| 419 | return bestlen; |
| 420 | } |
| 421 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 422 | /* Modify capture window (if necessary) |
| 423 | and calculate downscaling |
| 424 | Return -1 on error */ |
| 425 | 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] | 426 | { |
| 427 | int maxlen = max - min; |
| 428 | int len = *end - *beg + 1; |
| 429 | int newlen = w9966_findlen(len, size, maxlen); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 430 | int err = newlen - len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 432 | /* Check for bad format */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | if (newlen > maxlen || newlen < size) |
| 434 | return -1; |
| 435 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 436 | /* Set factor (6 bit fixed) */ |
| 437 | *factor = (64 * size) / newlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | if (*factor == 64) |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 439 | *factor = 0x00; /* downscale is disabled */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | else |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 441 | *factor |= 0x80; /* set downscale-enable bit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 443 | /* Modify old beginning and end */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | *beg -= err / 2; |
| 445 | *end += err - (err / 2); |
| 446 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 447 | /* Move window if outside borders */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | 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 Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 460 | /* Setup the cameras capture window etc. |
| 461 | Expects a claimed pdev |
| 462 | return -1 on error */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 463 | static int w9966_setup(struct w9966 *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] | 464 | { |
| 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 Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 475 | |
| 476 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 477 | if (w * h * 2 > W9966_SRAMSIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | DPRINTF("capture window exceeds SRAM size!.\n"); |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 479 | w = 200; h = 160; /* Pick default values */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | w &= ~0x1; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 483 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | cam->width = w; |
| 493 | cam->height = h; |
| 494 | |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 495 | enh_s = 0; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 496 | enh_e = w * h * 2; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 497 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 498 | /* Modify capture window if necessary and calculate downscaling */ |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 499 | if (w9966_calcscale(w, W9966_WND_MIN_X, W9966_WND_MAX_X, &x1, &x2, &scale_x) != 0 || |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 500 | w9966_calcscale(h, W9966_WND_MIN_Y, W9966_WND_MAX_Y, &y1, &y2, &scale_y) != 0) |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 501 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 503 | DPRINTF("%dx%d, x: %d<->%d, y: %d<->%d, sx: %d/64, sy: %d/64.\n", |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 504 | 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] | 505 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 506 | /* Setup registers */ |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 507 | 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 Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 511 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 512 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 521 | regs[0x0c] = W9966_SRAMID; /* SRAM-banks (1x 128kb) */ |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 522 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 523 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 531 | /* 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 Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 538 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 539 | /* SAA7111 chip settings */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | 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 Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 545 | /* Reset (ECP-fifo & serial-bus) */ |
| 546 | if (w9966_write_reg(cam, 0x00, 0x03) == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | return -1; |
| 548 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 549 | /* Write regs to w9966cf chip */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | for (i = 0; i < 0x1c; i++) |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 551 | if (w9966_write_reg(cam, i, regs[i]) == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | return -1; |
| 553 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 554 | /* Write regs to saa7111 chip */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | for (i = 0; i < 0x20; i++) |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 556 | if (w9966_write_reg_i2c(cam, i, saa7111_regs[i]) == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return -1; |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | * Video4linux interfacing |
| 564 | */ |
| 565 | |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 566 | static int cam_querycap(struct file *file, void *priv, |
| 567 | struct v4l2_capability *vcap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 569 | struct w9966 *cam = video_drvdata(file); |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 570 | |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 571 | 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 Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 574 | vcap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE; |
| 575 | vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 579 | static int cam_enum_input(struct file *file, void *fh, struct v4l2_input *vin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 581 | 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 | |
| 592 | static int cam_g_input(struct file *file, void *fh, unsigned int *inp) |
| 593 | { |
| 594 | *inp = 0; |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static int cam_s_input(struct file *file, void *fh, unsigned int inp) |
| 599 | { |
| 600 | return (inp > 0) ? -EINVAL : 0; |
| 601 | } |
| 602 | |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 603 | static int cam_s_ctrl(struct v4l2_ctrl *ctrl) |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 604 | { |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 605 | struct w9966 *cam = |
| 606 | container_of(ctrl->handler, struct w9966, hdl); |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 607 | int ret = 0; |
| 608 | |
| 609 | mutex_lock(&cam->lock); |
| 610 | switch (ctrl->id) { |
| 611 | case V4L2_CID_BRIGHTNESS: |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 612 | cam->brightness = ctrl->val; |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 613 | break; |
| 614 | case V4L2_CID_CONTRAST: |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 615 | cam->contrast = ctrl->val; |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 616 | break; |
| 617 | case V4L2_CID_SATURATION: |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 618 | cam->color = ctrl->val; |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 619 | break; |
| 620 | case V4L2_CID_HUE: |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 621 | cam->hue = ctrl->val; |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 622 | 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 | |
| 644 | static 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 | |
| 660 | static 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 | |
| 681 | static 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 | |
| 699 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 717 | /* Capture data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | static ssize_t w9966_v4l_read(struct file *file, char __user *buf, |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 719 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | { |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 721 | struct w9966 *cam = video_drvdata(file); |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 722 | unsigned char addr = 0xa0; /* ECP, read, CCD-transfer, 00000 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | unsigned char __user *dest = (unsigned char __user *)buf; |
| 724 | unsigned long dleft = count; |
| 725 | unsigned char *tbuf; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 726 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 727 | /* Why would anyone want more than this?? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | if (count > cam->width * cam->height * 2) |
| 729 | return -EINVAL; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 730 | |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 731 | mutex_lock(&cam->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | w9966_pdev_claim(cam); |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 733 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 737 | /* write special capture-addr and negotiate into data transfer */ |
| 738 | if ((parport_negotiate(cam->pport, cam->ppmode|IEEE1284_ADDR) != 0) || |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 739 | (parport_write(cam->pport, &addr, 1) != 1) || |
| 740 | (parport_negotiate(cam->pport, cam->ppmode|IEEE1284_DATA) != 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | w9966_pdev_release(cam); |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 742 | mutex_unlock(&cam->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | return -EFAULT; |
| 744 | } |
| 745 | |
| 746 | tbuf = kmalloc(W9966_RBUFFER, GFP_KERNEL); |
| 747 | if (tbuf == NULL) { |
| 748 | count = -ENOMEM; |
| 749 | goto out; |
| 750 | } |
| 751 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 752 | while (dleft > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | unsigned long tsize = (dleft > W9966_RBUFFER) ? W9966_RBUFFER : dleft; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | 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 Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 767 | w9966_write_reg(cam, 0x01, 0x18); /* Disable capture */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | out: |
| 770 | kfree(tbuf); |
| 771 | w9966_pdev_release(cam); |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 772 | mutex_unlock(&cam->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | return count; |
| 775 | } |
| 776 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 777 | static const struct v4l2_file_operations w9966_fops = { |
| 778 | .owner = THIS_MODULE, |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 779 | .open = v4l2_fh_open, |
| 780 | .release = v4l2_fh_release, |
| 781 | .poll = v4l2_ctrl_poll, |
Hans Verkuil | 61df3c9 | 2010-11-14 10:09:38 -0300 | [diff] [blame] | 782 | .unlocked_ioctl = video_ioctl2, |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 783 | .read = w9966_v4l_read, |
| 784 | }; |
| 785 | |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 786 | static 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 Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 791 | .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 Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 795 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 796 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 797 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
| 798 | }; |
| 799 | |
| 800 | static const struct v4l2_ctrl_ops cam_ctrl_ops = { |
| 801 | .s_ctrl = cam_s_ctrl, |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 802 | }; |
| 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 Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 809 | static int w9966_init(struct w9966 *cam, struct parport *port) |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 810 | { |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 811 | struct v4l2_device *v4l2_dev = &cam->v4l2_dev; |
| 812 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 813 | if (cam->dev_state != 0) |
| 814 | return -1; |
| 815 | |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 816 | 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 Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 822 | |
| 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 Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 836 | cam->pport = port; |
| 837 | cam->brightness = 128; |
| 838 | cam->contrast = 64; |
| 839 | cam->color = 64; |
| 840 | cam->hue = 0; |
| 841 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 842 | /* Select requested transfer mode */ |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 843 | 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 Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 858 | break; |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 859 | } |
| 860 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 861 | /* Tell the parport driver that we exists */ |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 862 | 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 Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 867 | w9966_set_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 868 | |
| 869 | w9966_pdev_claim(cam); |
| 870 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 871 | /* Setup a default capture mode */ |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 872 | 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 Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 879 | /* Fill in the video_device struct and register us to v4l */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 880 | 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 Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 885 | cam->vdev.ctrl_handler = &cam->hdl; |
| 886 | set_bit(V4L2_FL_USE_FH_PRIO, &cam->vdev.flags); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 887 | video_set_drvdata(&cam->vdev, cam); |
| 888 | |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 889 | mutex_init(&cam->lock); |
| 890 | |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 891 | if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) |
| 892 | return -1; |
| 893 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 894 | w9966_set_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 895 | |
| 896 | /* All ok */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 897 | v4l2_info(v4l2_dev, "Found and initialized a webcam on %s.\n", |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 898 | cam->pport->name); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | |
| 903 | /* Terminate everything gracefully */ |
Hans Verkuil | 626e2ac | 2010-04-06 11:36:39 -0300 | [diff] [blame] | 904 | static void w9966_term(struct w9966 *cam) |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 905 | { |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 906 | /* Unregister from v4l */ |
| 907 | if (w9966_get_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV)) { |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 908 | video_unregister_device(&cam->vdev); |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 909 | w9966_set_state(cam, W9966_STATE_VDEV, 0); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 910 | } |
| 911 | |
Hans Verkuil | 9d59344 | 2012-05-08 17:00:43 -0300 | [diff] [blame] | 912 | v4l2_ctrl_handler_free(&cam->hdl); |
| 913 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 914 | /* Terminate from IEEE1284 mode and release pdev block */ |
| 915 | if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) { |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 916 | w9966_pdev_claim(cam); |
| 917 | parport_negotiate(cam->pport, IEEE1284_MODE_COMPAT); |
| 918 | w9966_pdev_release(cam); |
| 919 | } |
| 920 | |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 921 | /* Unregister from parport */ |
| 922 | if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) { |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 923 | parport_unregister_device(cam->pdev); |
Hans Verkuil | c1f2b0f | 2010-03-22 05:15:14 -0300 | [diff] [blame] | 924 | w9966_set_state(cam, W9966_STATE_PDEV, 0); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 925 | } |
Hans Verkuil | bd5ba3b | 2010-12-31 11:27:38 -0300 | [diff] [blame] | 926 | memset(cam, 0, sizeof(*cam)); |
Hans Verkuil | 271922c | 2010-03-22 05:13:17 -0300 | [diff] [blame] | 927 | } |
| 928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 930 | /* Called once for every parport on init */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | static void w9966_attach(struct parport *port) |
| 932 | { |
| 933 | int i; |
Mauro Carvalho Chehab | d56410e | 2006-03-25 09:19:53 -0300 | [diff] [blame] | 934 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 935 | for (i = 0; i < W9966_MAXCAMS; i++) { |
| 936 | if (w9966_cams[i].dev_state != 0) /* Cam is already assigned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | continue; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 938 | if (strcmp(pardev[i], "aggressive") == 0 || strcmp(pardev[i], port->name) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | if (w9966_init(&w9966_cams[i], port) != 0) |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 940 | w9966_term(&w9966_cams[i]); |
| 941 | break; /* return */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 946 | /* Called once for every parport on termination */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | static void w9966_detach(struct parport *port) |
| 948 | { |
| 949 | int i; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | for (i = 0; i < W9966_MAXCAMS; i++) |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 952 | if (w9966_cams[i].dev_state != 0 && w9966_cams[i].pport == port) |
| 953 | w9966_term(&w9966_cams[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | |
| 957 | static struct parport_driver w9966_ppd = { |
| 958 | .name = W9966_DRIVERNAME, |
| 959 | .attach = w9966_attach, |
| 960 | .detach = w9966_detach, |
| 961 | }; |
| 962 | |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 963 | /* Module entry point */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | static int __init w9966_mod_init(void) |
| 965 | { |
| 966 | int i; |
Hans Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | 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 Verkuil | 4bfdd58 | 2010-03-22 04:47:27 -0300 | [diff] [blame] | 974 | /* Module cleanup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | static void __exit w9966_mod_term(void) |
| 976 | { |
| 977 | parport_unregister_driver(&w9966_ppd); |
| 978 | } |
| 979 | |
| 980 | module_init(w9966_mod_init); |
| 981 | module_exit(w9966_mod_term); |