Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Syntek DV4000 (STK014) subdriver |
| 3 | * |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 4 | * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #define MODULE_NAME "stk014" |
| 22 | |
| 23 | #include "gspca.h" |
| 24 | #include "jpeg.h" |
| 25 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 26 | MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>"); |
| 27 | MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver"); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | |
| 30 | /* specific webcam descriptor */ |
| 31 | struct sd { |
| 32 | struct gspca_dev gspca_dev; /* !! must be the first item */ |
| 33 | |
| 34 | unsigned char brightness; |
| 35 | unsigned char contrast; |
| 36 | unsigned char colors; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 37 | unsigned char lightfreq; |
Jean-Francois Moine | 71cb276 | 2009-03-03 05:33:41 -0300 | [diff] [blame] | 38 | u8 quality; |
Jean-Francois Moine | 77ac0ba | 2009-03-02 06:40:52 -0300 | [diff] [blame] | 39 | #define QUALITY_MIN 60 |
| 40 | #define QUALITY_MAX 95 |
| 41 | #define QUALITY_DEF 80 |
Jean-Francois Moine | 71cb276 | 2009-03-03 05:33:41 -0300 | [diff] [blame] | 42 | |
| 43 | u8 *jpeg_hdr; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 44 | }; |
| 45 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 46 | /* V4L2 controls supported by the driver */ |
| 47 | static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val); |
| 48 | static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val); |
| 49 | static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val); |
| 50 | static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val); |
| 51 | static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val); |
| 52 | static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 53 | static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val); |
| 54 | static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 55 | |
| 56 | static struct ctrl sd_ctrls[] = { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 57 | { |
| 58 | { |
| 59 | .id = V4L2_CID_BRIGHTNESS, |
| 60 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 61 | .name = "Brightness", |
| 62 | .minimum = 0, |
| 63 | .maximum = 255, |
| 64 | .step = 1, |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 65 | #define BRIGHTNESS_DEF 127 |
| 66 | .default_value = BRIGHTNESS_DEF, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 67 | }, |
| 68 | .set = sd_setbrightness, |
| 69 | .get = sd_getbrightness, |
| 70 | }, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 71 | { |
| 72 | { |
| 73 | .id = V4L2_CID_CONTRAST, |
| 74 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 75 | .name = "Contrast", |
| 76 | .minimum = 0, |
| 77 | .maximum = 255, |
| 78 | .step = 1, |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 79 | #define CONTRAST_DEF 127 |
| 80 | .default_value = CONTRAST_DEF, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 81 | }, |
| 82 | .set = sd_setcontrast, |
| 83 | .get = sd_getcontrast, |
| 84 | }, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 85 | { |
| 86 | { |
| 87 | .id = V4L2_CID_SATURATION, |
| 88 | .type = V4L2_CTRL_TYPE_INTEGER, |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 89 | .name = "Color", |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 90 | .minimum = 0, |
| 91 | .maximum = 255, |
| 92 | .step = 1, |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 93 | #define COLOR_DEF 127 |
| 94 | .default_value = COLOR_DEF, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 95 | }, |
| 96 | .set = sd_setcolors, |
| 97 | .get = sd_getcolors, |
| 98 | }, |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 99 | { |
| 100 | { |
| 101 | .id = V4L2_CID_POWER_LINE_FREQUENCY, |
| 102 | .type = V4L2_CTRL_TYPE_MENU, |
| 103 | .name = "Light frequency filter", |
| 104 | .minimum = 1, |
| 105 | .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */ |
| 106 | .step = 1, |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 107 | #define FREQ_DEF 1 |
| 108 | .default_value = FREQ_DEF, |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 109 | }, |
| 110 | .set = sd_setfreq, |
| 111 | .get = sd_getfreq, |
| 112 | }, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 113 | }; |
| 114 | |
Jean-Francois Moine | cc611b8 | 2008-12-29 07:49:41 -0300 | [diff] [blame] | 115 | static const struct v4l2_pix_format vga_mode[] = { |
Jean-Francois Moine | c2446b3 | 2008-07-05 11:49:20 -0300 | [diff] [blame] | 116 | {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, |
| 117 | .bytesperline = 320, |
| 118 | .sizeimage = 320 * 240 * 3 / 8 + 590, |
| 119 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 120 | .priv = 1}, |
| 121 | {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, |
| 122 | .bytesperline = 640, |
| 123 | .sizeimage = 640 * 480 * 3 / 8 + 590, |
| 124 | .colorspace = V4L2_COLORSPACE_JPEG, |
| 125 | .priv = 0}, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | /* -- read a register -- */ |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 129 | static int reg_r(struct gspca_dev *gspca_dev, |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 130 | __u16 index) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 131 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 132 | struct usb_device *dev = gspca_dev->dev; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 133 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 134 | |
| 135 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 136 | 0x00, |
| 137 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 138 | 0x00, |
| 139 | index, |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 140 | gspca_dev->usb_buf, 1, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 141 | 500); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 142 | if (ret < 0) { |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 143 | PDEBUG(D_ERR, "reg_r err %d", ret); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | return gspca_dev->usb_buf[0]; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* -- write a register -- */ |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 150 | static int reg_w(struct gspca_dev *gspca_dev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 151 | __u16 index, __u16 value) |
| 152 | { |
| 153 | struct usb_device *dev = gspca_dev->dev; |
| 154 | int ret; |
| 155 | |
| 156 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 157 | 0x01, |
| 158 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 159 | value, |
| 160 | index, |
| 161 | NULL, |
| 162 | 0, |
| 163 | 500); |
| 164 | if (ret < 0) |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 165 | PDEBUG(D_ERR, "reg_w err %d", ret); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 166 | return ret; |
| 167 | } |
| 168 | |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 169 | /* -- get a bulk value (4 bytes) -- */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 170 | static int rcv_val(struct gspca_dev *gspca_dev, |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 171 | int ads) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 172 | { |
| 173 | struct usb_device *dev = gspca_dev->dev; |
| 174 | int alen, ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 175 | |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 176 | reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff); |
| 177 | reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff); |
| 178 | reg_w(gspca_dev, 0x636, ads & 0xff); |
| 179 | reg_w(gspca_dev, 0x637, 0); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 180 | reg_w(gspca_dev, 0x638, 4); /* len & 0xff */ |
| 181 | reg_w(gspca_dev, 0x639, 0); /* len >> 8 */ |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 182 | reg_w(gspca_dev, 0x63a, 0); |
| 183 | reg_w(gspca_dev, 0x63b, 0); |
| 184 | reg_w(gspca_dev, 0x630, 5); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 185 | ret = usb_bulk_msg(dev, |
Jean-Francois Moine | 50e06de | 2008-12-31 08:13:46 -0300 | [diff] [blame] | 186 | usb_rcvbulkpipe(dev, 0x05), |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 187 | gspca_dev->usb_buf, |
| 188 | 4, /* length */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 189 | &alen, |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 190 | 500); /* timeout in milliseconds */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 191 | return ret; |
| 192 | } |
| 193 | |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 194 | /* -- send a bulk value -- */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 195 | static int snd_val(struct gspca_dev *gspca_dev, |
| 196 | int ads, |
| 197 | unsigned int val) |
| 198 | { |
| 199 | struct usb_device *dev = gspca_dev->dev; |
| 200 | int alen, ret; |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 201 | __u8 seq = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 202 | |
| 203 | if (ads == 0x003f08) { |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 204 | ret = reg_r(gspca_dev, 0x0704); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 205 | if (ret < 0) |
| 206 | goto ko; |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 207 | ret = reg_r(gspca_dev, 0x0705); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 208 | if (ret < 0) |
| 209 | goto ko; |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 210 | seq = ret; /* keep the sequence number */ |
| 211 | ret = reg_r(gspca_dev, 0x0650); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 212 | if (ret < 0) |
| 213 | goto ko; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 214 | reg_w(gspca_dev, 0x654, seq); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 215 | } else { |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 216 | reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 217 | } |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 218 | reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff); |
| 219 | reg_w(gspca_dev, 0x656, ads & 0xff); |
| 220 | reg_w(gspca_dev, 0x657, 0); |
| 221 | reg_w(gspca_dev, 0x658, 0x04); /* size */ |
| 222 | reg_w(gspca_dev, 0x659, 0); |
| 223 | reg_w(gspca_dev, 0x65a, 0); |
| 224 | reg_w(gspca_dev, 0x65b, 0); |
| 225 | reg_w(gspca_dev, 0x650, 5); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 226 | gspca_dev->usb_buf[0] = val >> 24; |
| 227 | gspca_dev->usb_buf[1] = val >> 16; |
| 228 | gspca_dev->usb_buf[2] = val >> 8; |
| 229 | gspca_dev->usb_buf[3] = val; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 230 | ret = usb_bulk_msg(dev, |
| 231 | usb_sndbulkpipe(dev, 6), |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 232 | gspca_dev->usb_buf, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 233 | 4, |
| 234 | &alen, |
| 235 | 500); /* timeout in milliseconds */ |
| 236 | if (ret < 0) |
| 237 | goto ko; |
| 238 | if (ads == 0x003f08) { |
| 239 | seq += 4; |
| 240 | seq &= 0x3f; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 241 | reg_w(gspca_dev, 0x705, seq); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 242 | } |
| 243 | return ret; |
| 244 | ko: |
| 245 | PDEBUG(D_ERR, "snd_val err %d", ret); |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | /* set a camera parameter */ |
| 250 | static int set_par(struct gspca_dev *gspca_dev, |
| 251 | int parval) |
| 252 | { |
| 253 | return snd_val(gspca_dev, 0x003f08, parval); |
| 254 | } |
| 255 | |
| 256 | static void setbrightness(struct gspca_dev *gspca_dev) |
| 257 | { |
| 258 | struct sd *sd = (struct sd *) gspca_dev; |
| 259 | int parval; |
| 260 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 261 | parval = 0x06000000 /* whiteness */ |
| 262 | + (sd->brightness << 16); |
| 263 | set_par(gspca_dev, parval); |
| 264 | } |
| 265 | |
| 266 | static void setcontrast(struct gspca_dev *gspca_dev) |
| 267 | { |
| 268 | struct sd *sd = (struct sd *) gspca_dev; |
| 269 | int parval; |
| 270 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 271 | parval = 0x07000000 /* contrast */ |
| 272 | + (sd->contrast << 16); |
| 273 | set_par(gspca_dev, parval); |
| 274 | } |
| 275 | |
| 276 | static void setcolors(struct gspca_dev *gspca_dev) |
| 277 | { |
| 278 | struct sd *sd = (struct sd *) gspca_dev; |
| 279 | int parval; |
| 280 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 281 | parval = 0x08000000 /* saturation */ |
| 282 | + (sd->colors << 16); |
| 283 | set_par(gspca_dev, parval); |
| 284 | } |
| 285 | |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 286 | static void setfreq(struct gspca_dev *gspca_dev) |
| 287 | { |
| 288 | struct sd *sd = (struct sd *) gspca_dev; |
| 289 | |
| 290 | set_par(gspca_dev, sd->lightfreq == 1 |
| 291 | ? 0x33640000 /* 50 Hz */ |
| 292 | : 0x33780000); /* 60 Hz */ |
| 293 | } |
| 294 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 295 | /* this function is called at probe time */ |
| 296 | static int sd_config(struct gspca_dev *gspca_dev, |
| 297 | const struct usb_device_id *id) |
| 298 | { |
| 299 | struct sd *sd = (struct sd *) gspca_dev; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 300 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 301 | gspca_dev->cam.cam_mode = vga_mode; |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 302 | gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode); |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 303 | sd->brightness = BRIGHTNESS_DEF; |
| 304 | sd->contrast = CONTRAST_DEF; |
| 305 | sd->colors = COLOR_DEF; |
| 306 | sd->lightfreq = FREQ_DEF; |
Jean-Francois Moine | 77ac0ba | 2009-03-02 06:40:52 -0300 | [diff] [blame] | 307 | sd->quality = QUALITY_DEF; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 311 | /* this function is called at probe and resume time */ |
| 312 | static int sd_init(struct gspca_dev *gspca_dev) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 313 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 314 | int ret; |
| 315 | |
| 316 | /* check if the device responds */ |
| 317 | usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 318 | ret = reg_r(gspca_dev, 0x0740); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 319 | if (ret < 0) |
| 320 | return ret; |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 321 | if (ret != 0xff) { |
| 322 | PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", ret); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 323 | return -1; |
| 324 | } |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /* -- start the camera -- */ |
Jean-Francois Moine | 72ab97c | 2008-09-20 06:39:08 -0300 | [diff] [blame] | 329 | static int sd_start(struct gspca_dev *gspca_dev) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 330 | { |
Jean-Francois Moine | 71cb276 | 2009-03-03 05:33:41 -0300 | [diff] [blame] | 331 | struct sd *sd = (struct sd *) gspca_dev; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 332 | int ret, value; |
| 333 | |
Jean-Francois Moine | 71cb276 | 2009-03-03 05:33:41 -0300 | [diff] [blame] | 334 | /* create the JPEG header */ |
| 335 | sd->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL); |
| 336 | jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width, |
| 337 | 0x22); /* JPEG 411 */ |
| 338 | jpeg_set_qual(sd->jpeg_hdr, sd->quality); |
| 339 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 340 | /* work on alternate 1 */ |
| 341 | usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1); |
| 342 | |
| 343 | set_par(gspca_dev, 0x10000000); |
| 344 | set_par(gspca_dev, 0x00000000); |
| 345 | set_par(gspca_dev, 0x8002e001); |
| 346 | set_par(gspca_dev, 0x14000000); |
| 347 | if (gspca_dev->width > 320) |
| 348 | value = 0x8002e001; /* 640x480 */ |
| 349 | else |
| 350 | value = 0x4001f000; /* 320x240 */ |
| 351 | set_par(gspca_dev, value); |
| 352 | ret = usb_set_interface(gspca_dev->dev, |
| 353 | gspca_dev->iface, |
| 354 | gspca_dev->alt); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 355 | if (ret < 0) { |
| 356 | PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed", |
| 357 | gspca_dev->iface, gspca_dev->alt); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 358 | goto out; |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 359 | } |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 360 | ret = reg_r(gspca_dev, 0x0630); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 361 | if (ret < 0) |
| 362 | goto out; |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 363 | rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */ |
| 364 | ret = reg_r(gspca_dev, 0x0650); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 365 | if (ret < 0) |
| 366 | goto out; |
| 367 | snd_val(gspca_dev, 0x000020, 0xffffffff); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 368 | reg_w(gspca_dev, 0x0620, 0); |
| 369 | reg_w(gspca_dev, 0x0630, 0); |
| 370 | reg_w(gspca_dev, 0x0640, 0); |
| 371 | reg_w(gspca_dev, 0x0650, 0); |
| 372 | reg_w(gspca_dev, 0x0660, 0); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 373 | setbrightness(gspca_dev); /* whiteness */ |
| 374 | setcontrast(gspca_dev); /* contrast */ |
| 375 | setcolors(gspca_dev); /* saturation */ |
| 376 | set_par(gspca_dev, 0x09800000); /* Red ? */ |
| 377 | set_par(gspca_dev, 0x0a800000); /* Green ? */ |
| 378 | set_par(gspca_dev, 0x0b800000); /* Blue ? */ |
| 379 | set_par(gspca_dev, 0x0d030000); /* Gamma ? */ |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 380 | setfreq(gspca_dev); /* light frequency */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 381 | |
| 382 | /* start the video flow */ |
| 383 | set_par(gspca_dev, 0x01000000); |
| 384 | set_par(gspca_dev, 0x01000000); |
| 385 | PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt); |
Jean-Francois Moine | 72ab97c | 2008-09-20 06:39:08 -0300 | [diff] [blame] | 386 | return 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 387 | out: |
| 388 | PDEBUG(D_ERR|D_STREAM, "camera start err %d", ret); |
Jean-Francois Moine | 72ab97c | 2008-09-20 06:39:08 -0300 | [diff] [blame] | 389 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static void sd_stopN(struct gspca_dev *gspca_dev) |
| 393 | { |
| 394 | struct usb_device *dev = gspca_dev->dev; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 395 | |
| 396 | set_par(gspca_dev, 0x02000000); |
| 397 | set_par(gspca_dev, 0x02000000); |
| 398 | usb_set_interface(dev, gspca_dev->iface, 1); |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 399 | reg_r(gspca_dev, 0x0630); |
| 400 | rcv_val(gspca_dev, 0x000020); /* << (value ff ff ff ff) */ |
| 401 | reg_r(gspca_dev, 0x0650); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 402 | snd_val(gspca_dev, 0x000020, 0xffffffff); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 403 | reg_w(gspca_dev, 0x0620, 0); |
| 404 | reg_w(gspca_dev, 0x0630, 0); |
| 405 | reg_w(gspca_dev, 0x0640, 0); |
| 406 | reg_w(gspca_dev, 0x0650, 0); |
| 407 | reg_w(gspca_dev, 0x0660, 0); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 408 | PDEBUG(D_STREAM, "camera stopped"); |
| 409 | } |
| 410 | |
Jean-Francois Moine | 71cb276 | 2009-03-03 05:33:41 -0300 | [diff] [blame] | 411 | static void sd_stop0(struct gspca_dev *gspca_dev) |
| 412 | { |
| 413 | struct sd *sd = (struct sd *) gspca_dev; |
| 414 | |
| 415 | kfree(sd->jpeg_hdr); |
| 416 | } |
| 417 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 418 | static void sd_pkt_scan(struct gspca_dev *gspca_dev, |
| 419 | struct gspca_frame *frame, /* target */ |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 420 | __u8 *data, /* isoc packet */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 421 | int len) /* iso packet length */ |
| 422 | { |
Jean-Francois Moine | 71cb276 | 2009-03-03 05:33:41 -0300 | [diff] [blame] | 423 | struct sd *sd = (struct sd *) gspca_dev; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame] | 424 | static unsigned char ffd9[] = {0xff, 0xd9}; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 425 | |
| 426 | /* a frame starts with: |
| 427 | * - 0xff 0xfe |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 428 | * - 0x08 0x00 - length (little endian ?!) |
| 429 | * - 4 bytes = size of whole frame (BE - including header) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 430 | * - 0x00 0x0c |
| 431 | * - 0xff 0xd8 |
| 432 | * - .. JPEG image with escape sequences (ff 00) |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 433 | * (without ending - ff d9) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 434 | */ |
| 435 | if (data[0] == 0xff && data[1] == 0xfe) { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 436 | frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame, |
| 437 | ffd9, 2); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 438 | |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 439 | /* put the JPEG 411 header */ |
Jean-Francois Moine | 71cb276 | 2009-03-03 05:33:41 -0300 | [diff] [blame] | 440 | gspca_frame_add(gspca_dev, FIRST_PACKET, frame, |
| 441 | sd->jpeg_hdr, JPEG_HDR_SZ); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 442 | |
| 443 | /* beginning of the frame */ |
| 444 | #define STKHDRSZ 12 |
Jean-Francois Moine | 8843765 | 2008-11-24 06:28:58 -0300 | [diff] [blame] | 445 | data += STKHDRSZ; |
| 446 | len -= STKHDRSZ; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 447 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 448 | gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) |
| 452 | { |
| 453 | struct sd *sd = (struct sd *) gspca_dev; |
| 454 | |
| 455 | sd->brightness = val; |
| 456 | if (gspca_dev->streaming) |
| 457 | setbrightness(gspca_dev); |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val) |
| 462 | { |
| 463 | struct sd *sd = (struct sd *) gspca_dev; |
| 464 | |
| 465 | *val = sd->brightness; |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val) |
| 470 | { |
| 471 | struct sd *sd = (struct sd *) gspca_dev; |
| 472 | |
| 473 | sd->contrast = val; |
| 474 | if (gspca_dev->streaming) |
| 475 | setcontrast(gspca_dev); |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val) |
| 480 | { |
| 481 | struct sd *sd = (struct sd *) gspca_dev; |
| 482 | |
| 483 | *val = sd->contrast; |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val) |
| 488 | { |
| 489 | struct sd *sd = (struct sd *) gspca_dev; |
| 490 | |
| 491 | sd->colors = val; |
| 492 | if (gspca_dev->streaming) |
| 493 | setcolors(gspca_dev); |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val) |
| 498 | { |
| 499 | struct sd *sd = (struct sd *) gspca_dev; |
| 500 | |
| 501 | *val = sd->colors; |
| 502 | return 0; |
| 503 | } |
| 504 | |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 505 | static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val) |
| 506 | { |
| 507 | struct sd *sd = (struct sd *) gspca_dev; |
| 508 | |
| 509 | sd->lightfreq = val; |
| 510 | if (gspca_dev->streaming) |
| 511 | setfreq(gspca_dev); |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val) |
| 516 | { |
| 517 | struct sd *sd = (struct sd *) gspca_dev; |
| 518 | |
| 519 | *val = sd->lightfreq; |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static int sd_querymenu(struct gspca_dev *gspca_dev, |
| 524 | struct v4l2_querymenu *menu) |
| 525 | { |
| 526 | switch (menu->id) { |
| 527 | case V4L2_CID_POWER_LINE_FREQUENCY: |
| 528 | switch (menu->index) { |
| 529 | case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */ |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 530 | strcpy((char *) menu->name, "50 Hz"); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 531 | return 0; |
| 532 | case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */ |
Jean-Francois Moine | a5ae206 | 2008-07-04 11:16:16 -0300 | [diff] [blame] | 533 | strcpy((char *) menu->name, "60 Hz"); |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | break; |
| 537 | } |
| 538 | return -EINVAL; |
| 539 | } |
| 540 | |
Jean-Francois Moine | 77ac0ba | 2009-03-02 06:40:52 -0300 | [diff] [blame] | 541 | static int sd_set_jcomp(struct gspca_dev *gspca_dev, |
| 542 | struct v4l2_jpegcompression *jcomp) |
| 543 | { |
| 544 | struct sd *sd = (struct sd *) gspca_dev; |
| 545 | |
| 546 | if (jcomp->quality < QUALITY_MIN) |
| 547 | sd->quality = QUALITY_MIN; |
| 548 | else if (jcomp->quality > QUALITY_MAX) |
| 549 | sd->quality = QUALITY_MAX; |
| 550 | else |
| 551 | sd->quality = jcomp->quality; |
| 552 | if (gspca_dev->streaming) |
| 553 | jpeg_set_qual(sd->jpeg_hdr, sd->quality); |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static int sd_get_jcomp(struct gspca_dev *gspca_dev, |
| 558 | struct v4l2_jpegcompression *jcomp) |
| 559 | { |
| 560 | struct sd *sd = (struct sd *) gspca_dev; |
| 561 | |
| 562 | memset(jcomp, 0, sizeof *jcomp); |
| 563 | jcomp->quality = sd->quality; |
| 564 | jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT |
| 565 | | V4L2_JPEG_MARKER_DQT; |
| 566 | return 0; |
| 567 | } |
| 568 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 569 | /* sub-driver description */ |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 570 | static const struct sd_desc sd_desc = { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 571 | .name = MODULE_NAME, |
| 572 | .ctrls = sd_ctrls, |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 573 | .nctrls = ARRAY_SIZE(sd_ctrls), |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 574 | .config = sd_config, |
Jean-Francois Moine | 012d6b0 | 2008-09-03 17:12:16 -0300 | [diff] [blame] | 575 | .init = sd_init, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 576 | .start = sd_start, |
| 577 | .stopN = sd_stopN, |
Jean-Francois Moine | 71cb276 | 2009-03-03 05:33:41 -0300 | [diff] [blame] | 578 | .stop0 = sd_stop0, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 579 | .pkt_scan = sd_pkt_scan, |
Jean-Francois Moine | 6a7eba2 | 2008-06-30 15:50:11 -0300 | [diff] [blame] | 580 | .querymenu = sd_querymenu, |
Jean-Francois Moine | 77ac0ba | 2009-03-02 06:40:52 -0300 | [diff] [blame] | 581 | .get_jcomp = sd_get_jcomp, |
| 582 | .set_jcomp = sd_set_jcomp, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | /* -- module initialisation -- */ |
Jean-Francois Moine | 739570b | 2008-07-14 09:38:29 -0300 | [diff] [blame] | 586 | static const __devinitdata struct usb_device_id device_table[] = { |
Jean-Francois Moine | 9d64fdb | 2008-07-25 08:53:03 -0300 | [diff] [blame] | 587 | {USB_DEVICE(0x05e1, 0x0893)}, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 588 | {} |
| 589 | }; |
| 590 | MODULE_DEVICE_TABLE(usb, device_table); |
| 591 | |
| 592 | /* -- device connect -- */ |
| 593 | static int sd_probe(struct usb_interface *intf, |
| 594 | const struct usb_device_id *id) |
| 595 | { |
Jean-Francois Moine | d43fa32 | 2008-06-12 10:58:58 -0300 | [diff] [blame] | 596 | return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), |
| 597 | THIS_MODULE); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static struct usb_driver sd_driver = { |
| 601 | .name = MODULE_NAME, |
| 602 | .id_table = device_table, |
| 603 | .probe = sd_probe, |
| 604 | .disconnect = gspca_disconnect, |
Jean-Francois Moine | 6a70974 | 2008-09-03 16:48:10 -0300 | [diff] [blame] | 605 | #ifdef CONFIG_PM |
| 606 | .suspend = gspca_suspend, |
| 607 | .resume = gspca_resume, |
| 608 | #endif |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 609 | }; |
| 610 | |
| 611 | /* -- module insert / remove -- */ |
| 612 | static int __init sd_mod_init(void) |
| 613 | { |
Alexey Klimov | f69e952 | 2009-01-01 13:02:07 -0300 | [diff] [blame] | 614 | int ret; |
| 615 | ret = usb_register(&sd_driver); |
| 616 | if (ret < 0) |
Alexey Klimov | e6b1484 | 2009-01-01 13:04:58 -0300 | [diff] [blame] | 617 | return ret; |
Jean-Francois Moine | 10b0e96 | 2008-07-22 05:35:10 -0300 | [diff] [blame] | 618 | info("registered"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 619 | return 0; |
| 620 | } |
| 621 | static void __exit sd_mod_exit(void) |
| 622 | { |
| 623 | usb_deregister(&sd_driver); |
| 624 | info("deregistered"); |
| 625 | } |
| 626 | |
| 627 | module_init(sd_mod_init); |
| 628 | module_exit(sd_mod_exit); |