sunplus.c 37.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 *		Sunplus spca504(abc) spca533 spca536 library
 *		Copyright (C) 2005 Michel Xhaard mxhaard@magic.fr
 *
 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#define MODULE_NAME "sunplus"

#include "gspca.h"
#include "jpeg.h"

MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
MODULE_DESCRIPTION("GSPCA/SPCA5xx USB Camera Driver");
MODULE_LICENSE("GPL");

/* specific webcam descriptor */
struct sd {
	struct gspca_dev gspca_dev;	/* !! must be the first item */

35
	s8 brightness;
36 37 38
	u8 contrast;
	u8 colors;
	u8 autogain;
39
	u8 quality;
40 41 42
#define QUALITY_MIN 70
#define QUALITY_MAX 95
#define QUALITY_DEF 85
43

44
	u8 bridge;
45 46 47 48 49
#define BRIDGE_SPCA504 0
#define BRIDGE_SPCA504B 1
#define BRIDGE_SPCA504C 2
#define BRIDGE_SPCA533 3
#define BRIDGE_SPCA536 4
50
	u8 subtype;
51 52 53 54
#define AiptekMiniPenCam13 1
#define LogitechClickSmart420 2
#define LogitechClickSmart820 3
#define MegapixV4 4
55
#define MegaImageVI 5
56 57

	u8 *jpeg_hdr;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
};

/* V4L2 controls supported by the driver */
static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);

static struct ctrl sd_ctrls[] = {
	{
	    {
		.id      = V4L2_CID_BRIGHTNESS,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Brightness",
76 77
		.minimum = -128,
		.maximum = 127,
78
		.step    = 1,
79 80
#define BRIGHTNESS_DEF 0
		.default_value = BRIGHTNESS_DEF,
81 82 83 84 85 86 87 88 89 90 91 92
	    },
	    .set = sd_setbrightness,
	    .get = sd_getbrightness,
	},
	{
	    {
		.id      = V4L2_CID_CONTRAST,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Contrast",
		.minimum = 0,
		.maximum = 0xff,
		.step    = 1,
93 94
#define CONTRAST_DEF 0x20
		.default_value = CONTRAST_DEF,
95 96 97 98 99 100 101 102 103 104 105 106
	    },
	    .set = sd_setcontrast,
	    .get = sd_getcontrast,
	},
	{
	    {
		.id      = V4L2_CID_SATURATION,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Color",
		.minimum = 0,
		.maximum = 0xff,
		.step    = 1,
107 108
#define COLOR_DEF 0x1a
		.default_value = COLOR_DEF,
109 110 111 112 113 114 115 116 117 118 119 120
	    },
	    .set = sd_setcolors,
	    .get = sd_getcolors,
	},
	{
	    {
		.id      = V4L2_CID_AUTOGAIN,
		.type    = V4L2_CTRL_TYPE_BOOLEAN,
		.name    = "Auto Gain",
		.minimum = 0,
		.maximum = 1,
		.step    = 1,
121 122
#define AUTOGAIN_DEF 1
		.default_value = AUTOGAIN_DEF,
123 124 125 126 127 128
	    },
	    .set = sd_setautogain,
	    .get = sd_getautogain,
	},
};

129
static const struct v4l2_pix_format vga_mode[] = {
130 131 132 133 134 135 136 137 138 139
	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 320,
		.sizeimage = 320 * 240 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 2},
	{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 640,
		.sizeimage = 640 * 480 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 1},
140 141
};

142
static const struct v4l2_pix_format custom_mode[] = {
143 144 145 146 147 148 149 150 151 152
	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 320,
		.sizeimage = 320 * 240 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 2},
	{464, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 464,
		.sizeimage = 464 * 480 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 1},
153 154
};

155
static const struct v4l2_pix_format vga_mode2[] = {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	{176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 176,
		.sizeimage = 176 * 144 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 4},
	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 320,
		.sizeimage = 320 * 240 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 3},
	{352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 352,
		.sizeimage = 352 * 288 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 2},
	{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 640,
		.sizeimage = 640 * 480 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 1},
176 177 178 179 180 181 182 183
};

#define SPCA50X_OFFSET_DATA 10
#define SPCA504_PCCAM600_OFFSET_SNAPSHOT 3
#define SPCA504_PCCAM600_OFFSET_COMPRESS 4
#define SPCA504_PCCAM600_OFFSET_MODE	 5
#define SPCA504_PCCAM600_OFFSET_DATA	 14
 /* Frame packet header offsets for the spca533 */
184
#define SPCA533_OFFSET_DATA	16
185 186
#define SPCA533_OFFSET_FRAMSEQ	15
/* Frame packet header offsets for the spca536 */
187 188 189 190 191 192 193 194
#define SPCA536_OFFSET_DATA	4
#define SPCA536_OFFSET_FRAMSEQ	1

struct cmd {
	u8 req;
	u16 val;
	u16 idx;
};
195 196

/* Initialisation data for the Creative PC-CAM 600 */
197
static const struct cmd spca504_pccam600_init_data[] = {
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/*	{0xa0, 0x0000, 0x0503},  * capture mode */
	{0x00, 0x0000, 0x2000},
	{0x00, 0x0013, 0x2301},
	{0x00, 0x0003, 0x2000},
	{0x00, 0x0001, 0x21ac},
	{0x00, 0x0001, 0x21a6},
	{0x00, 0x0000, 0x21a7},	/* brightness */
	{0x00, 0x0020, 0x21a8},	/* contrast */
	{0x00, 0x0001, 0x21ac},	/* sat/hue */
	{0x00, 0x0000, 0x21ad},	/* hue */
	{0x00, 0x001a, 0x21ae},	/* saturation */
	{0x00, 0x0002, 0x21a3},	/* gamma */
	{0x30, 0x0154, 0x0008},
	{0x30, 0x0004, 0x0006},
	{0x30, 0x0258, 0x0009},
	{0x30, 0x0004, 0x0000},
	{0x30, 0x0093, 0x0004},
	{0x30, 0x0066, 0x0005},
	{0x00, 0x0000, 0x2000},
	{0x00, 0x0013, 0x2301},
	{0x00, 0x0003, 0x2000},
	{0x00, 0x0013, 0x2301},
	{0x00, 0x0003, 0x2000},
};

/* Creative PC-CAM 600 specific open data, sent before using the
 * generic initialisation data from spca504_open_data.
 */
226
static const struct cmd spca504_pccam600_open_data[] = {
227 228 229 230 231 232 233
	{0x00, 0x0001, 0x2501},
	{0x20, 0x0500, 0x0001},	/* snapshot mode */
	{0x00, 0x0003, 0x2880},
	{0x00, 0x0001, 0x2881},
};

/* Initialisation data for the logitech clicksmart 420 */
234
static const struct cmd spca504A_clicksmart420_init_data[] = {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
/*	{0xa0, 0x0000, 0x0503},  * capture mode */
	{0x00, 0x0000, 0x2000},
	{0x00, 0x0013, 0x2301},
	{0x00, 0x0003, 0x2000},
	{0x00, 0x0001, 0x21ac},
	{0x00, 0x0001, 0x21a6},
	{0x00, 0x0000, 0x21a7},	/* brightness */
	{0x00, 0x0020, 0x21a8},	/* contrast */
	{0x00, 0x0001, 0x21ac},	/* sat/hue */
	{0x00, 0x0000, 0x21ad},	/* hue */
	{0x00, 0x001a, 0x21ae},	/* saturation */
	{0x00, 0x0002, 0x21a3},	/* gamma */
	{0x30, 0x0004, 0x000a},
	{0xb0, 0x0001, 0x0000},


251
	{0xa1, 0x0080, 0x0001},
252 253 254 255 256 257 258 259 260 261 262 263
	{0x30, 0x0049, 0x0000},
	{0x30, 0x0060, 0x0005},
	{0x0c, 0x0004, 0x0000},
	{0x00, 0x0000, 0x0000},
	{0x00, 0x0000, 0x2000},
	{0x00, 0x0013, 0x2301},
	{0x00, 0x0003, 0x2000},
	{0x00, 0x0000, 0x2000},

};

/* clicksmart 420 open data ? */
264
static const struct cmd spca504A_clicksmart420_open_data[] = {
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	{0x00, 0x0001, 0x2501},
	{0x20, 0x0502, 0x0000},
	{0x06, 0x0000, 0x0000},
	{0x00, 0x0004, 0x2880},
	{0x00, 0x0001, 0x2881},
/* look like setting a qTable */
	{0x00, 0x0006, 0x2800},
	{0x00, 0x0004, 0x2801},
	{0x00, 0x0004, 0x2802},
	{0x00, 0x0006, 0x2803},
	{0x00, 0x000a, 0x2804},
	{0x00, 0x0010, 0x2805},
	{0x00, 0x0014, 0x2806},
	{0x00, 0x0018, 0x2807},
	{0x00, 0x0005, 0x2808},
	{0x00, 0x0005, 0x2809},
	{0x00, 0x0006, 0x280a},
	{0x00, 0x0008, 0x280b},
	{0x00, 0x000a, 0x280c},
	{0x00, 0x0017, 0x280d},
	{0x00, 0x0018, 0x280e},
	{0x00, 0x0016, 0x280f},

	{0x00, 0x0006, 0x2810},
	{0x00, 0x0005, 0x2811},
	{0x00, 0x0006, 0x2812},
	{0x00, 0x000a, 0x2813},
	{0x00, 0x0010, 0x2814},
	{0x00, 0x0017, 0x2815},
	{0x00, 0x001c, 0x2816},
	{0x00, 0x0016, 0x2817},
	{0x00, 0x0006, 0x2818},
	{0x00, 0x0007, 0x2819},
	{0x00, 0x0009, 0x281a},
	{0x00, 0x000c, 0x281b},
	{0x00, 0x0014, 0x281c},
	{0x00, 0x0023, 0x281d},
	{0x00, 0x0020, 0x281e},
	{0x00, 0x0019, 0x281f},

	{0x00, 0x0007, 0x2820},
	{0x00, 0x0009, 0x2821},
	{0x00, 0x000f, 0x2822},
	{0x00, 0x0016, 0x2823},
	{0x00, 0x001b, 0x2824},
	{0x00, 0x002c, 0x2825},
	{0x00, 0x0029, 0x2826},
	{0x00, 0x001f, 0x2827},
	{0x00, 0x000a, 0x2828},
	{0x00, 0x000e, 0x2829},
	{0x00, 0x0016, 0x282a},
	{0x00, 0x001a, 0x282b},
	{0x00, 0x0020, 0x282c},
	{0x00, 0x002a, 0x282d},
	{0x00, 0x002d, 0x282e},
	{0x00, 0x0025, 0x282f},

	{0x00, 0x0014, 0x2830},
	{0x00, 0x001a, 0x2831},
	{0x00, 0x001f, 0x2832},
	{0x00, 0x0023, 0x2833},
	{0x00, 0x0029, 0x2834},
	{0x00, 0x0030, 0x2835},
	{0x00, 0x0030, 0x2836},
	{0x00, 0x0028, 0x2837},
	{0x00, 0x001d, 0x2838},
	{0x00, 0x0025, 0x2839},
	{0x00, 0x0026, 0x283a},
	{0x00, 0x0027, 0x283b},
	{0x00, 0x002d, 0x283c},
	{0x00, 0x0028, 0x283d},
	{0x00, 0x0029, 0x283e},
	{0x00, 0x0028, 0x283f},

	{0x00, 0x0007, 0x2840},
	{0x00, 0x0007, 0x2841},
	{0x00, 0x000a, 0x2842},
	{0x00, 0x0013, 0x2843},
	{0x00, 0x0028, 0x2844},
	{0x00, 0x0028, 0x2845},
	{0x00, 0x0028, 0x2846},
	{0x00, 0x0028, 0x2847},
	{0x00, 0x0007, 0x2848},
	{0x00, 0x0008, 0x2849},
	{0x00, 0x000a, 0x284a},
	{0x00, 0x001a, 0x284b},
	{0x00, 0x0028, 0x284c},
	{0x00, 0x0028, 0x284d},
	{0x00, 0x0028, 0x284e},
	{0x00, 0x0028, 0x284f},

	{0x00, 0x000a, 0x2850},
	{0x00, 0x000a, 0x2851},
	{0x00, 0x0016, 0x2852},
	{0x00, 0x0028, 0x2853},
	{0x00, 0x0028, 0x2854},
	{0x00, 0x0028, 0x2855},
	{0x00, 0x0028, 0x2856},
	{0x00, 0x0028, 0x2857},
	{0x00, 0x0013, 0x2858},
	{0x00, 0x001a, 0x2859},
	{0x00, 0x0028, 0x285a},
	{0x00, 0x0028, 0x285b},
	{0x00, 0x0028, 0x285c},
	{0x00, 0x0028, 0x285d},
	{0x00, 0x0028, 0x285e},
	{0x00, 0x0028, 0x285f},

	{0x00, 0x0028, 0x2860},
	{0x00, 0x0028, 0x2861},
	{0x00, 0x0028, 0x2862},
	{0x00, 0x0028, 0x2863},
	{0x00, 0x0028, 0x2864},
	{0x00, 0x0028, 0x2865},
	{0x00, 0x0028, 0x2866},
	{0x00, 0x0028, 0x2867},
	{0x00, 0x0028, 0x2868},
	{0x00, 0x0028, 0x2869},
	{0x00, 0x0028, 0x286a},
	{0x00, 0x0028, 0x286b},
	{0x00, 0x0028, 0x286c},
	{0x00, 0x0028, 0x286d},
	{0x00, 0x0028, 0x286e},
	{0x00, 0x0028, 0x286f},

	{0x00, 0x0028, 0x2870},
	{0x00, 0x0028, 0x2871},
	{0x00, 0x0028, 0x2872},
	{0x00, 0x0028, 0x2873},
	{0x00, 0x0028, 0x2874},
	{0x00, 0x0028, 0x2875},
	{0x00, 0x0028, 0x2876},
	{0x00, 0x0028, 0x2877},
	{0x00, 0x0028, 0x2878},
	{0x00, 0x0028, 0x2879},
	{0x00, 0x0028, 0x287a},
	{0x00, 0x0028, 0x287b},
	{0x00, 0x0028, 0x287c},
	{0x00, 0x0028, 0x287d},
	{0x00, 0x0028, 0x287e},
	{0x00, 0x0028, 0x287f},

	{0xa0, 0x0000, 0x0503},
};

410
static const u8 qtable_creative_pccam[2][64] = {
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	{				/* Q-table Y-components */
	 0x05, 0x03, 0x03, 0x05, 0x07, 0x0c, 0x0f, 0x12,
	 0x04, 0x04, 0x04, 0x06, 0x08, 0x11, 0x12, 0x11,
	 0x04, 0x04, 0x05, 0x07, 0x0c, 0x11, 0x15, 0x11,
	 0x04, 0x05, 0x07, 0x09, 0x0f, 0x1a, 0x18, 0x13,
	 0x05, 0x07, 0x0b, 0x11, 0x14, 0x21, 0x1f, 0x17,
	 0x07, 0x0b, 0x11, 0x13, 0x18, 0x1f, 0x22, 0x1c,
	 0x0f, 0x13, 0x17, 0x1a, 0x1f, 0x24, 0x24, 0x1e,
	 0x16, 0x1c, 0x1d, 0x1d, 0x22, 0x1e, 0x1f, 0x1e},
	{				/* Q-table C-components */
	 0x05, 0x05, 0x07, 0x0e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x05, 0x06, 0x08, 0x14, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x07, 0x08, 0x11, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x0e, 0x14, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e}
};

/* FIXME: This Q-table is identical to the Creative PC-CAM one,
 *		except for one byte. Possibly a typo?
 *		NWG: 18/05/2003.
 */
435
static const u8 qtable_spca504_default[2][64] = {
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
	{				/* Q-table Y-components */
	 0x05, 0x03, 0x03, 0x05, 0x07, 0x0c, 0x0f, 0x12,
	 0x04, 0x04, 0x04, 0x06, 0x08, 0x11, 0x12, 0x11,
	 0x04, 0x04, 0x05, 0x07, 0x0c, 0x11, 0x15, 0x11,
	 0x04, 0x05, 0x07, 0x09, 0x0f, 0x1a, 0x18, 0x13,
	 0x05, 0x07, 0x0b, 0x11, 0x14, 0x21, 0x1f, 0x17,
	 0x07, 0x0b, 0x11, 0x13, 0x18, 0x1f, 0x22, 0x1c,
	 0x0f, 0x13, 0x17, 0x1a, 0x1f, 0x24, 0x24, 0x1e,
	 0x16, 0x1c, 0x1d, 0x1d, 0x1d /* 0x22 */ , 0x1e, 0x1f, 0x1e,
	 },
	{				/* Q-table C-components */
	 0x05, 0x05, 0x07, 0x0e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x05, 0x06, 0x08, 0x14, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x07, 0x08, 0x11, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x0e, 0x14, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e,
	 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e}
};

457 458
/* read <len> bytes to gspca_dev->usb_buf */
static void reg_r(struct gspca_dev *gspca_dev,
459 460 461
		  u8 req,
		  u16 index,
		  u16 len)
462
{
463 464 465 466 467 468 469 470
#ifdef GSPCA_DEBUG
	if (len > USB_BUF_SZ) {
		err("reg_r: buffer overflow");
		return;
	}
#endif
	usb_control_msg(gspca_dev->dev,
			usb_rcvctrlpipe(gspca_dev->dev, 0),
471 472 473
			req,
			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			0,		/* value */
474 475
			index,
			len ? gspca_dev->usb_buf : NULL, len,
476 477 478
			500);
}

479 480 481 482 483 484
/* write one byte */
static void reg_w_1(struct gspca_dev *gspca_dev,
		   u8 req,
		   u16 value,
		   u16 index,
		   u16 byte)
485
{
486
	gspca_dev->usb_buf[0] = byte;
487 488
	usb_control_msg(gspca_dev->dev,
			usb_sndctrlpipe(gspca_dev->dev, 0),
489 490
			req,
			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
491
			value, index,
492
			gspca_dev->usb_buf, 1,
493 494 495
			500);
}

496 497
/* write req / index / value */
static int reg_w_riv(struct usb_device *dev,
498
		     u8 req, u16 index, u16 value)
499 500 501 502 503 504
{
	int ret;

	ret = usb_control_msg(dev,
			usb_sndctrlpipe(dev, 0),
			req,
505
			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
506
			value, index, NULL, 0, 500);
507
	PDEBUG(D_USBO, "reg write: 0x%02x,0x%02x:0x%02x, %d",
508
		req, index, value, ret);
509 510 511 512 513
	if (ret < 0)
		PDEBUG(D_ERR, "reg write: error %d", ret);
	return ret;
}

514 515
/* read 1 byte */
static int reg_r_1(struct gspca_dev *gspca_dev,
516
			u16 value)	/* wValue */
517 518 519
{
	int ret;

520 521
	ret = usb_control_msg(gspca_dev->dev,
			usb_rcvctrlpipe(gspca_dev->dev, 0),
522 523 524 525
			0x20,			/* request */
			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			value,
			0,			/* index */
526
			gspca_dev->usb_buf, 1,
527 528
			500);			/* timeout */
	if (ret < 0) {
529
		PDEBUG(D_ERR, "reg_r_1 err %d", ret);
530 531
		return 0;
	}
532
	return gspca_dev->usb_buf[0];
533 534
}

535 536
/* read 1 or 2 bytes - returns < 0 if error */
static int reg_r_12(struct gspca_dev *gspca_dev,
537 538 539
			u8 req,		/* bRequest */
			u16 index,	/* wIndex */
			u16 length)	/* wLength (1 or 2 only) */
540 541 542
{
	int ret;

543 544 545
	gspca_dev->usb_buf[1] = 0;
	ret = usb_control_msg(gspca_dev->dev,
			usb_rcvctrlpipe(gspca_dev->dev, 0),
546 547 548 549
			req,
			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			0,		/* value */
			index,
550
			gspca_dev->usb_buf, length,
551 552 553 554 555
			500);
	if (ret < 0) {
		PDEBUG(D_ERR, "reg_read err %d", ret);
		return -1;
	}
556
	return (gspca_dev->usb_buf[1] << 8) + gspca_dev->usb_buf[0];
557 558 559
}

static int write_vector(struct gspca_dev *gspca_dev,
560
			const struct cmd *data, int ncmds)
561 562
{
	struct usb_device *dev = gspca_dev->dev;
563
	int ret;
564

565 566
	while (--ncmds >= 0) {
		ret = reg_w_riv(dev, data->req, data->idx, data->val);
567 568
		if (ret < 0) {
			PDEBUG(D_ERR,
569 570
			   "Register write failed for 0x%02x, 0x%04x, 0x%04x",
				data->req, data->val, data->idx);
571 572
			return ret;
		}
573
		data++;
574 575 576 577 578
	}
	return 0;
}

static int spca50x_setup_qtable(struct gspca_dev *gspca_dev,
579
				const u8 qtable[2][64])
580 581 582 583 584 585
{
	struct usb_device *dev = gspca_dev->dev;
	int i, err;

	/* loop over y components */
	for (i = 0; i < 64; i++) {
586
		err = reg_w_riv(dev, 0x00, 0x2800 + i, qtable[0][i]);
587 588 589 590 591 592
		if (err < 0)
			return err;
	}

	/* loop over c components */
	for (i = 0; i < 64; i++) {
593
		err = reg_w_riv(dev, 0x00, 0x2840 + i, qtable[1][i]);
594 595 596 597 598 599 600
		if (err < 0)
			return err;
	}
	return 0;
}

static void spca504_acknowledged_command(struct gspca_dev *gspca_dev,
601
			     u8 req, u16 idx, u16 val)
602 603
{
	struct usb_device *dev = gspca_dev->dev;
604
	int notdone;
605

606 607 608
	reg_w_riv(dev, req, idx, val);
	notdone = reg_r_12(gspca_dev, 0x01, 0x0001, 1);
	reg_w_riv(dev, req, idx, val);
609

610
	PDEBUG(D_FRAM, "before wait 0x%04x", notdone);
611 612

	msleep(200);
613
	notdone = reg_r_12(gspca_dev, 0x01, 0x0001, 1);
614
	PDEBUG(D_FRAM, "after wait 0x%04x", notdone);
615 616 617
}

static void spca504A_acknowledged_command(struct gspca_dev *gspca_dev,
618 619
			u8 req,
			u16 idx, u16 val, u8 stat, u8 count)
620 621
{
	struct usb_device *dev = gspca_dev->dev;
622 623
	int status;
	u8 endcode;
624

625 626
	reg_w_riv(dev, req, idx, val);
	status = reg_r_12(gspca_dev, 0x01, 0x0001, 1);
627
	endcode = stat;
628
	PDEBUG(D_FRAM, "Status 0x%x Need 0x%04x", status, stat);
629 630 631 632 633 634
	if (!count)
		return;
	count = 200;
	while (--count > 0) {
		msleep(10);
		/* gsmart mini2 write a each wait setting 1 ms is enought */
635 636
/*		reg_w_riv(dev, req, idx, val); */
		status = reg_r_12(gspca_dev, 0x01, 0x0001, 1);
637
		if (status == endcode) {
638
			PDEBUG(D_FRAM, "status 0x%04x after wait %d",
639 640 641 642 643 644
				status, 200 - count);
				break;
		}
	}
}

645
static int spca504B_PollingDataReady(struct gspca_dev *gspca_dev)
646 647 648 649
{
	int count = 10;

	while (--count > 0) {
650
		reg_r(gspca_dev, 0x21, 0, 1);
651
		if ((gspca_dev->usb_buf[0] & 0x01) == 0)
652 653 654
			break;
		msleep(10);
	}
655
	return gspca_dev->usb_buf[0];
656 657 658 659 660 661 662
}

static void spca504B_WaitCmdStatus(struct gspca_dev *gspca_dev)
{
	int count = 50;

	while (--count > 0) {
663
		reg_r(gspca_dev, 0x21, 1, 1);
664
		if (gspca_dev->usb_buf[0] != 0) {
665
			reg_w_1(gspca_dev, 0x21, 0, 1, 0);
666
			reg_r(gspca_dev, 0x21, 1, 1);
667
			spca504B_PollingDataReady(gspca_dev);
668 669 670 671 672 673 674 675
			break;
		}
		msleep(10);
	}
}

static void spca50x_GetFirmware(struct gspca_dev *gspca_dev)
{
676
	u8 *data;
677

678 679
	data = gspca_dev->usb_buf;
	reg_r(gspca_dev, 0x20, 0, 5);
680
	PDEBUG(D_STREAM, "FirmWare : %d %d %d %d %d ",
681
		data[0], data[1], data[2], data[3], data[4]);
682 683
	reg_r(gspca_dev, 0x23, 0, 64);
	reg_r(gspca_dev, 0x23, 1, 64);
684 685 686 687 688 689
}

static void spca504B_SetSizeType(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct usb_device *dev = gspca_dev->dev;
690
	u8 Size;
691 692
	int rc;

693
	Size = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
694 695
	switch (sd->bridge) {
	case BRIDGE_SPCA533:
696
		reg_w_riv(dev, 0x31, 0, 0);
697
		spca504B_WaitCmdStatus(gspca_dev);
698
		rc = spca504B_PollingDataReady(gspca_dev);
699
		spca50x_GetFirmware(gspca_dev);
700
		reg_w_1(gspca_dev, 0x24, 0, 8, 2);		/* type */
701
		reg_r(gspca_dev, 0x24, 8, 1);
702

703
		reg_w_1(gspca_dev, 0x25, 0, 4, Size);
704
		reg_r(gspca_dev, 0x25, 4, 1);			/* size */
705
		rc = spca504B_PollingDataReady(gspca_dev);
706 707

		/* Init the cam width height with some values get on init ? */
708
		reg_w_riv(dev, 0x31, 0, 0x04);
709
		spca504B_WaitCmdStatus(gspca_dev);
710
		rc = spca504B_PollingDataReady(gspca_dev);
711 712 713 714
		break;
	default:
/* case BRIDGE_SPCA504B: */
/* case BRIDGE_SPCA536: */
715
		reg_w_1(gspca_dev, 0x25, 0, 4, Size);
716
		reg_r(gspca_dev, 0x25, 4, 1);			/* size */
717
		reg_w_1(gspca_dev, 0x27, 0, 0, 6);
718
		reg_r(gspca_dev, 0x27, 0, 1);			/* type */
719
		rc = spca504B_PollingDataReady(gspca_dev);
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
		break;
	case BRIDGE_SPCA504:
		Size += 3;
		if (sd->subtype == AiptekMiniPenCam13) {
			/* spca504a aiptek */
			spca504A_acknowledged_command(gspca_dev,
						0x08, Size, 0,
						0x80 | (Size & 0x0f), 1);
			spca504A_acknowledged_command(gspca_dev,
							1, 3, 0, 0x9f, 0);
		} else {
			spca504_acknowledged_command(gspca_dev, 0x08, Size, 0);
		}
		break;
	case BRIDGE_SPCA504C:
		/* capture mode */
736 737
		reg_w_riv(dev, 0xa0, (0x0500 | (Size & 0x0f)), 0x00);
		reg_w_riv(dev, 0x20, 0x01, 0x0500 | (Size & 0x0f));
738 739 740 741 742 743 744 745 746 747 748
		break;
	}
}

static void spca504_wait_status(struct gspca_dev *gspca_dev)
{
	int cnt;

	cnt = 256;
	while (--cnt > 0) {
		/* With this we get the status, when return 0 it's all ok */
749
		if (reg_r_12(gspca_dev, 0x06, 0x00, 1) == 0)
750 751 752 753 754 755 756
			return;
		msleep(10);
	}
}

static void spca504B_setQtable(struct gspca_dev *gspca_dev)
{
757
	reg_w_1(gspca_dev, 0x26, 0, 0, 3);
758
	reg_r(gspca_dev, 0x26, 0, 1);
759
	spca504B_PollingDataReady(gspca_dev);
760 761
}

762 763 764 765 766 767 768 769 770 771 772
static void setbrightness(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct usb_device *dev = gspca_dev->dev;
	u16 reg;

	reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f0 : 0x21a7;
	reg_w_riv(dev, 0x00, reg, sd->brightness);
}

static void setcontrast(struct gspca_dev *gspca_dev)
773 774
{
	struct sd *sd = (struct sd *) gspca_dev;
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	struct usb_device *dev = gspca_dev->dev;
	u16 reg;

	reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f1 : 0x21a8;
	reg_w_riv(dev, 0x00, reg, sd->contrast);
}

static void setcolors(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct usb_device *dev = gspca_dev->dev;
	u16 reg;

	reg = sd->bridge == BRIDGE_SPCA536 ? 0x20f6 : 0x21ae;
	reg_w_riv(dev, 0x00, reg, sd->colors);
}

static void init_ctl_reg(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct usb_device *dev = gspca_dev->dev;
796 797
	int pollreg = 1;

798 799 800 801
	setbrightness(gspca_dev);
	setcontrast(gspca_dev);
	setcolors(gspca_dev);

802 803 804 805 806 807 808 809
	switch (sd->bridge) {
	case BRIDGE_SPCA504:
	case BRIDGE_SPCA504C:
		pollreg = 0;
		/* fall thru */
	default:
/*	case BRIDGE_SPCA533: */
/*	case BRIDGE_SPCA504B: */
810 811 812
		reg_w_riv(dev, 0, 0x00, 0x21ad);	/* hue */
		reg_w_riv(dev, 0, 0x01, 0x21ac);	/* sat/hue */
		reg_w_riv(dev, 0, 0x00, 0x21a3);	/* gamma */
813 814
		break;
	case BRIDGE_SPCA536:
815 816 817
		reg_w_riv(dev, 0, 0x40, 0x20f5);
		reg_w_riv(dev, 0, 0x01, 0x20f4);
		reg_w_riv(dev, 0, 0x00, 0x2089);
818 819 820
		break;
	}
	if (pollreg)
821
		spca504B_PollingDataReady(gspca_dev);
822 823 824 825 826 827 828 829
}

/* this function is called at probe time */
static int sd_config(struct gspca_dev *gspca_dev,
			const struct usb_device_id *id)
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct cam *cam;
830 831 832 833 834 835 836

	cam = &gspca_dev->cam;

	sd->bridge = id->driver_info >> 8;
	sd->subtype = id->driver_info;

	if (sd->subtype == AiptekMiniPenCam13) {
837 838
/* try to get the firmware as some cam answer 2.0.1.2.2
 * and should be a spca504b then overwrite that setting */
839
		reg_r(gspca_dev, 0x20, 0, 1);
840 841 842 843
		switch (gspca_dev->usb_buf[0]) {
		case 1:
			break;		/* (right bridge/subtype) */
		case 2:
844
			sd->bridge = BRIDGE_SPCA504B;
845
			sd->subtype = 0;
846
			break;
847 848
		default:
			return -ENODEV;
849 850 851 852 853 854 855 856 857
		}
	}

	switch (sd->bridge) {
	default:
/*	case BRIDGE_SPCA504B: */
/*	case BRIDGE_SPCA504: */
/*	case BRIDGE_SPCA536: */
		cam->cam_mode = vga_mode;
858
		cam->nmodes =ARRAY_SIZE(vga_mode);
859 860 861
		break;
	case BRIDGE_SPCA533:
		cam->cam_mode = custom_mode;
862 863 864 865
		if (sd->subtype == MegaImageVI)		/* 320x240 only */
			cam->nmodes = ARRAY_SIZE(custom_mode) - 1;
		else
			cam->nmodes = ARRAY_SIZE(custom_mode);
866 867 868
		break;
	case BRIDGE_SPCA504C:
		cam->cam_mode = vga_mode2;
869
		cam->nmodes = ARRAY_SIZE(vga_mode2);
870 871
		break;
	}
872 873 874 875
	sd->brightness = BRIGHTNESS_DEF;
	sd->contrast = CONTRAST_DEF;
	sd->colors = COLOR_DEF;
	sd->autogain = AUTOGAIN_DEF;
876
	sd->quality = QUALITY_DEF;
877 878 879
	return 0;
}

880 881
/* this function is called at probe and resume time */
static int sd_init(struct gspca_dev *gspca_dev)
882 883 884
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct usb_device *dev = gspca_dev->dev;
885 886
	int i, err_code;
	u8 info[6];
887 888 889

	switch (sd->bridge) {
	case BRIDGE_SPCA504B:
890 891 892 893 894 895
		reg_w_riv(dev, 0x1d, 0x00, 0);
		reg_w_riv(dev, 0, 0x01, 0x2306);
		reg_w_riv(dev, 0, 0x00, 0x0d04);
		reg_w_riv(dev, 0, 0x00, 0x2000);
		reg_w_riv(dev, 0, 0x13, 0x2301);
		reg_w_riv(dev, 0, 0x00, 0x2306);
896 897
		/* fall thru */
	case BRIDGE_SPCA533:
898
		spca504B_PollingDataReady(gspca_dev);
899 900 901 902
		spca50x_GetFirmware(gspca_dev);
		break;
	case BRIDGE_SPCA536:
		spca50x_GetFirmware(gspca_dev);
903
		reg_r(gspca_dev, 0x00, 0x5002, 1);
904
		reg_w_1(gspca_dev, 0x24, 0, 0, 0);
905
		reg_r(gspca_dev, 0x24, 0, 1);
906 907
		spca504B_PollingDataReady(gspca_dev);
		reg_w_riv(dev, 0x34, 0, 0);
908 909 910 911
		spca504B_WaitCmdStatus(gspca_dev);
		break;
	case BRIDGE_SPCA504C:	/* pccam600 */
		PDEBUG(D_STREAM, "Opening SPCA504 (PC-CAM 600)");
912 913
		reg_w_riv(dev, 0xe0, 0x0000, 0x0000);
		reg_w_riv(dev, 0xe0, 0x0000, 0x0001);	/* reset */
914 915 916
		spca504_wait_status(gspca_dev);
		if (sd->subtype == LogitechClickSmart420)
			write_vector(gspca_dev,
917 918
				spca504A_clicksmart420_open_data,
				ARRAY_SIZE(spca504A_clicksmart420_open_data));
919
		else
920 921
			write_vector(gspca_dev, spca504_pccam600_open_data,
				ARRAY_SIZE(spca504_pccam600_open_data));
922
		err_code = spca50x_setup_qtable(gspca_dev,
923
						qtable_creative_pccam);
924 925 926 927 928 929 930 931 932 933 934
		if (err_code < 0) {
			PDEBUG(D_ERR|D_STREAM, "spca50x_setup_qtable failed");
			return err_code;
		}
		break;
	default:
/*	case BRIDGE_SPCA504: */
		PDEBUG(D_STREAM, "Opening SPCA504");
		if (sd->subtype == AiptekMiniPenCam13) {
			/*****************************/
			for (i = 0; i < 6; i++)
935
				info[i] = reg_r_1(gspca_dev, i);
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
			PDEBUG(D_STREAM,
				"Read info: %d %d %d %d %d %d."
				" Should be 1,0,2,2,0,0",
				info[0], info[1], info[2],
				info[3], info[4], info[5]);
			/* spca504a aiptek */
			/* Set AE AWB Banding Type 3-> 50Hz 2-> 60Hz */
			spca504A_acknowledged_command(gspca_dev, 0x24,
							8, 3, 0x9e, 1);
			/* Twice sequencial need status 0xff->0x9e->0x9d */
			spca504A_acknowledged_command(gspca_dev, 0x24,
							8, 3, 0x9e, 0);

			spca504A_acknowledged_command(gspca_dev, 0x24,
							0, 0, 0x9d, 1);
			/******************************/
			/* spca504a aiptek */
			spca504A_acknowledged_command(gspca_dev, 0x08,
							6, 0, 0x86, 1);
/*			reg_write (dev, 0, 0x2000, 0); */
/*			reg_write (dev, 0, 0x2883, 1); */
/*			spca504A_acknowledged_command (gspca_dev, 0x08,
							6, 0, 0x86, 1); */
/*			spca504A_acknowledged_command (gspca_dev, 0x24,
							0, 0, 0x9D, 1); */
961 962
			reg_w_riv(dev, 0x00, 0x270c, 0x05); /* L92 sno1t.txt */
			reg_w_riv(dev, 0x00, 0x2310, 0x05);
963 964 965 966
			spca504A_acknowledged_command(gspca_dev, 0x01,
							0x0f, 0, 0xff, 0);
		}
		/* setup qtable */
967 968
		reg_w_riv(dev, 0, 0x2000, 0);
		reg_w_riv(dev, 0, 0x2883, 1);
969 970 971 972 973 974 975 976 977 978 979
		err_code = spca50x_setup_qtable(gspca_dev,
						qtable_spca504_default);
		if (err_code < 0) {
			PDEBUG(D_ERR, "spca50x_setup_qtable failed");
			return err_code;
		}
		break;
	}
	return 0;
}

980
static int sd_start(struct gspca_dev *gspca_dev)
981 982 983 984
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct usb_device *dev = gspca_dev->dev;
	int enable;
985 986
	int i;
	u8 info[6];
987

988 989
	/* create the JPEG header */
	sd->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
990 991
	if (!sd->jpeg_hdr)
		return -ENOMEM;
992 993 994 995
	jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
			0x22);		/* JPEG 411 */
	jpeg_set_qual(sd->jpeg_hdr, sd->quality);

996 997 998 999 1000 1001 1002 1003
	if (sd->bridge == BRIDGE_SPCA504B)
		spca504B_setQtable(gspca_dev);
	spca504B_SetSizeType(gspca_dev);
	switch (sd->bridge) {
	default:
/*	case BRIDGE_SPCA504B: */
/*	case BRIDGE_SPCA533: */
/*	case BRIDGE_SPCA536: */
1004 1005 1006 1007 1008
		switch (sd->subtype) {
		case MegapixV4:
		case LogitechClickSmart820:
		case MegaImageVI:
			reg_w_riv(dev, 0xf0, 0, 0);
1009
			spca504B_WaitCmdStatus(gspca_dev);
1010
			reg_r(gspca_dev, 0xf0, 4, 0);
1011
			spca504B_WaitCmdStatus(gspca_dev);
1012 1013 1014
			break;
		default:
			reg_w_riv(dev, 0x31, 0, 0x04);
1015
			spca504B_WaitCmdStatus(gspca_dev);
1016 1017
			spca504B_PollingDataReady(gspca_dev);
			break;
1018 1019 1020 1021 1022
		}
		break;
	case BRIDGE_SPCA504:
		if (sd->subtype == AiptekMiniPenCam13) {
			for (i = 0; i < 6; i++)
1023
				info[i] = reg_r_1(gspca_dev, i);
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
			PDEBUG(D_STREAM,
				"Read info: %d %d %d %d %d %d."
				" Should be 1,0,2,2,0,0",
				info[0], info[1], info[2],
				info[3], info[4], info[5]);
			/* spca504a aiptek */
			/* Set AE AWB Banding Type 3-> 50Hz 2-> 60Hz */
			spca504A_acknowledged_command(gspca_dev, 0x24,
							8, 3, 0x9e, 1);
			/* Twice sequencial need status 0xff->0x9e->0x9d */
			spca504A_acknowledged_command(gspca_dev, 0x24,
							8, 3, 0x9e, 0);
			spca504A_acknowledged_command(gspca_dev, 0x24,
							0, 0, 0x9d, 1);
		} else {
			spca504_acknowledged_command(gspca_dev, 0x24, 8, 3);
			for (i = 0; i < 6; i++)
1041
				info[i] = reg_r_1(gspca_dev, i);
1042 1043 1044 1045 1046 1047 1048 1049 1050
			PDEBUG(D_STREAM,
				"Read info: %d %d %d %d %d %d."
				" Should be 1,0,2,2,0,0",
				info[0], info[1], info[2],
				info[3], info[4], info[5]);
			spca504_acknowledged_command(gspca_dev, 0x24, 8, 3);
			spca504_acknowledged_command(gspca_dev, 0x24, 0, 0);
		}
		spca504B_SetSizeType(gspca_dev);
1051 1052
		reg_w_riv(dev, 0x00, 0x270c, 0x05);	/* L92 sno1t.txt */
		reg_w_riv(dev, 0x00, 0x2310, 0x05);
1053 1054 1055 1056
		break;
	case BRIDGE_SPCA504C:
		if (sd->subtype == LogitechClickSmart420) {
			write_vector(gspca_dev,
1057 1058
				spca504A_clicksmart420_init_data,
				ARRAY_SIZE(spca504A_clicksmart420_init_data));
1059
		} else {
1060 1061
			write_vector(gspca_dev, spca504_pccam600_init_data,
				ARRAY_SIZE(spca504_pccam600_init_data));
1062
		}
1063 1064 1065
		enable = (sd->autogain ? 0x04 : 0x01);
		reg_w_riv(dev, 0x0c, 0x0000, enable);	/* auto exposure */
		reg_w_riv(dev, 0xb0, 0x0000, enable);	/* auto whiteness */
1066 1067

		/* set default exposure compensation and whiteness balance */
1068 1069
		reg_w_riv(dev, 0x30, 0x0001, 800);	/* ~ 20 fps */
		reg_w_riv(dev, 0x30, 0x0002, 1600);
1070 1071 1072
		spca504B_SetSizeType(gspca_dev);
		break;
	}
1073
	init_ctl_reg(gspca_dev);
1074
	return 0;
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
}

static void sd_stopN(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct usb_device *dev = gspca_dev->dev;

	switch (sd->bridge) {
	default:
/*	case BRIDGE_SPCA533: */
/*	case BRIDGE_SPCA536: */
/*	case BRIDGE_SPCA504B: */
1087
		reg_w_riv(dev, 0x31, 0, 0);
1088
		spca504B_WaitCmdStatus(gspca_dev);
1089
		spca504B_PollingDataReady(gspca_dev);
1090 1091 1092
		break;
	case BRIDGE_SPCA504:
	case BRIDGE_SPCA504C:
1093
		reg_w_riv(dev, 0x00, 0x2000, 0x0000);
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104

		if (sd->subtype == AiptekMiniPenCam13) {
			/* spca504a aiptek */
/*			spca504A_acknowledged_command(gspca_dev, 0x08,
							 6, 0, 0x86, 1); */
			spca504A_acknowledged_command(gspca_dev, 0x24,
							0x00, 0x00, 0x9d, 1);
			spca504A_acknowledged_command(gspca_dev, 0x01,
							0x0f, 0x00, 0xff, 1);
		} else {
			spca504_acknowledged_command(gspca_dev, 0x24, 0, 0);
1105
			reg_w_riv(dev, 0x01, 0x000f, 0x0000);
1106 1107 1108 1109 1110
		}
		break;
	}
}

1111 1112 1113 1114 1115 1116 1117
static void sd_stop0(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	kfree(sd->jpeg_hdr);
}

1118 1119
static void sd_pkt_scan(struct gspca_dev *gspca_dev,
			struct gspca_frame *frame,	/* target */
1120
			u8 *data,			/* isoc packet */
1121 1122 1123 1124
			int len)			/* iso packet length */
{
	struct sd *sd = (struct sd *) gspca_dev;
	int i, sof = 0;
1125
	static u8 ffd9[] = {0xff, 0xd9};
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192

/* frames are jpeg 4.1.1 without 0xff escape */
	switch (sd->bridge) {
	case BRIDGE_SPCA533:
		if (data[0] == 0xff) {
			if (data[1] != 0x01) {	/* drop packet */
/*				gspca_dev->last_packet_type = DISCARD_PACKET; */
				return;
			}
			sof = 1;
			data += SPCA533_OFFSET_DATA;
			len -= SPCA533_OFFSET_DATA;
		} else {
			data += 1;
			len -= 1;
		}
		break;
	case BRIDGE_SPCA536:
		if (data[0] == 0xff) {
			sof = 1;
			data += SPCA536_OFFSET_DATA;
			len -= SPCA536_OFFSET_DATA;
		} else {
			data += 2;
			len -= 2;
		}
		break;
	default:
/*	case BRIDGE_SPCA504: */
/*	case BRIDGE_SPCA504B: */
		switch (data[0]) {
		case 0xfe:			/* start of frame */
			sof = 1;
			data += SPCA50X_OFFSET_DATA;
			len -= SPCA50X_OFFSET_DATA;
			break;
		case 0xff:			/* drop packet */
/*			gspca_dev->last_packet_type = DISCARD_PACKET; */
			return;
		default:
			data += 1;
			len -= 1;
			break;
		}
		break;
	case BRIDGE_SPCA504C:
		switch (data[0]) {
		case 0xfe:			/* start of frame */
			sof = 1;
			data += SPCA504_PCCAM600_OFFSET_DATA;
			len -= SPCA504_PCCAM600_OFFSET_DATA;
			break;
		case 0xff:			/* drop packet */
/*			gspca_dev->last_packet_type = DISCARD_PACKET; */
			return;
		default:
			data += 1;
			len -= 1;
			break;
		}
		break;
	}
	if (sof) {		/* start of frame */
		frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
					ffd9, 2);

		/* put the JPEG header in the new frame */
1193 1194
		gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
			sd->jpeg_hdr, JPEG_HDR_SZ);
1195 1196 1197
	}

	/* add 0x00 after 0xff */
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	i = 0;
	do {
		if (data[i] == 0xff) {
			gspca_frame_add(gspca_dev, INTER_PACKET, frame,
					data, i + 1);
			len -= i;
			data += i;
			*data = 0x00;
			i = 0;
		}
		i++;
	} while (i < len);
	gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
}

static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->brightness = val;
	if (gspca_dev->streaming)
		setbrightness(gspca_dev);
	return 0;
}

static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	*val = sd->brightness;
	return 0;
}

static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->contrast = val;
	if (gspca_dev->streaming)
		setcontrast(gspca_dev);
	return 0;
}

static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	*val = sd->contrast;
	return 0;
}

static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->colors = val;
	if (gspca_dev->streaming)
		setcolors(gspca_dev);
	return 0;
}

static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	*val = sd->colors;
	return 0;
}

static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->autogain = val;
	return 0;
}

static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	*val = sd->autogain;
	return 0;
}

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
static int sd_set_jcomp(struct gspca_dev *gspca_dev,
			struct v4l2_jpegcompression *jcomp)
{
	struct sd *sd = (struct sd *) gspca_dev;

	if (jcomp->quality < QUALITY_MIN)
		sd->quality = QUALITY_MIN;
	else if (jcomp->quality > QUALITY_MAX)
		sd->quality = QUALITY_MAX;
	else
		sd->quality = jcomp->quality;
	if (gspca_dev->streaming)
		jpeg_set_qual(sd->jpeg_hdr, sd->quality);
	return 0;
}

static int sd_get_jcomp(struct gspca_dev *gspca_dev,
			struct v4l2_jpegcompression *jcomp)
{
	struct sd *sd = (struct sd *) gspca_dev;

	memset(jcomp, 0, sizeof *jcomp);
	jcomp->quality = sd->quality;
	jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
			| V4L2_JPEG_MARKER_DQT;
	return 0;
}

1311
/* sub-driver description */
1312
static const struct sd_desc sd_desc = {
1313 1314 1315 1316
	.name = MODULE_NAME,
	.ctrls = sd_ctrls,
	.nctrls = ARRAY_SIZE(sd_ctrls),
	.config = sd_config,
1317
	.init = sd_init,
1318 1319
	.start = sd_start,
	.stopN = sd_stopN,
1320
	.stop0 = sd_stop0,
1321
	.pkt_scan = sd_pkt_scan,
1322 1323
	.get_jcomp = sd_get_jcomp,
	.set_jcomp = sd_set_jcomp,
1324 1325 1326
};

/* -- module initialisation -- */
1327 1328 1329
#define BS(bridge, subtype) \
	.driver_info = (BRIDGE_ ## bridge << 8) \
			| (subtype)
1330
static const __devinitdata struct usb_device_id device_table[] = {
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
	{USB_DEVICE(0x041e, 0x400b), BS(SPCA504C, 0)},
	{USB_DEVICE(0x041e, 0x4012), BS(SPCA504C, 0)},
	{USB_DEVICE(0x041e, 0x4013), BS(SPCA504C, 0)},
	{USB_DEVICE(0x0458, 0x7006), BS(SPCA504B, 0)},
	{USB_DEVICE(0x0461, 0x0821), BS(SPCA533, 0)},
	{USB_DEVICE(0x046d, 0x0905), BS(SPCA533, LogitechClickSmart820)},
	{USB_DEVICE(0x046d, 0x0960), BS(SPCA504C, LogitechClickSmart420)},
	{USB_DEVICE(0x0471, 0x0322), BS(SPCA504B, 0)},
	{USB_DEVICE(0x04a5, 0x3003), BS(SPCA504B, 0)},
	{USB_DEVICE(0x04a5, 0x3008), BS(SPCA533, 0)},
	{USB_DEVICE(0x04a5, 0x300a), BS(SPCA533, 0)},
	{USB_DEVICE(0x04f1, 0x1001), BS(SPCA504B, 0)},
	{USB_DEVICE(0x04fc, 0x500c), BS(SPCA504B, 0)},
	{USB_DEVICE(0x04fc, 0x504a), BS(SPCA504, AiptekMiniPenCam13)},
	{USB_DEVICE(0x04fc, 0x504b), BS(SPCA504B, 0)},
	{USB_DEVICE(0x04fc, 0x5330), BS(SPCA533, 0)},
	{USB_DEVICE(0x04fc, 0x5360), BS(SPCA536, 0)},
	{USB_DEVICE(0x04fc, 0xffff), BS(SPCA504B, 0)},
	{USB_DEVICE(0x052b, 0x1513), BS(SPCA533, MegapixV4)},
1350
	{USB_DEVICE(0x052b, 0x1803), BS(SPCA533, MegaImageVI)},
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
	{USB_DEVICE(0x0546, 0x3155), BS(SPCA533, 0)},
	{USB_DEVICE(0x0546, 0x3191), BS(SPCA504B, 0)},
	{USB_DEVICE(0x0546, 0x3273), BS(SPCA504B, 0)},
	{USB_DEVICE(0x055f, 0xc211), BS(SPCA536, 0)},
	{USB_DEVICE(0x055f, 0xc230), BS(SPCA533, 0)},
	{USB_DEVICE(0x055f, 0xc232), BS(SPCA533, 0)},
	{USB_DEVICE(0x055f, 0xc360), BS(SPCA536, 0)},
	{USB_DEVICE(0x055f, 0xc420), BS(SPCA504, 0)},
	{USB_DEVICE(0x055f, 0xc430), BS(SPCA533, 0)},
	{USB_DEVICE(0x055f, 0xc440), BS(SPCA533, 0)},
	{USB_DEVICE(0x055f, 0xc520), BS(SPCA504, 0)},
	{USB_DEVICE(0x055f, 0xc530), BS(SPCA533, 0)},
	{USB_DEVICE(0x055f, 0xc540), BS(SPCA533, 0)},
	{USB_DEVICE(0x055f, 0xc630), BS(SPCA533, 0)},
	{USB_DEVICE(0x055f, 0xc650), BS(SPCA533, 0)},
	{USB_DEVICE(0x05da, 0x1018), BS(SPCA504B, 0)},
	{USB_DEVICE(0x06d6, 0x0031), BS(SPCA533, 0)},
	{USB_DEVICE(0x0733, 0x1311), BS(SPCA533, 0)},
	{USB_DEVICE(0x0733, 0x1314), BS(SPCA533, 0)},
	{USB_DEVICE(0x0733, 0x2211), BS(SPCA533, 0)},
	{USB_DEVICE(0x0733, 0x2221), BS(SPCA533, 0)},
	{USB_DEVICE(0x0733, 0x3261), BS(SPCA536, 0)},
	{USB_DEVICE(0x0733, 0x3281), BS(SPCA536, 0)},
	{USB_DEVICE(0x08ca, 0x0104), BS(SPCA533, 0)},
	{USB_DEVICE(0x08ca, 0x0106), BS(SPCA533, 0)},
	{USB_DEVICE(0x08ca, 0x2008), BS(SPCA504B, 0)},
	{USB_DEVICE(0x08ca, 0x2010), BS(SPCA533, 0)},
	{USB_DEVICE(0x08ca, 0x2016), BS(SPCA504B, 0)},
	{USB_DEVICE(0x08ca, 0x2018), BS(SPCA504B, 0)},
	{USB_DEVICE(0x08ca, 0x2020), BS(SPCA533, 0)},
	{USB_DEVICE(0x08ca, 0x2022), BS(SPCA533, 0)},
	{USB_DEVICE(0x08ca, 0x2024), BS(SPCA536, 0)},
	{USB_DEVICE(0x08ca, 0x2028), BS(SPCA533, 0)},
	{USB_DEVICE(0x08ca, 0x2040), BS(SPCA536, 0)},
	{USB_DEVICE(0x08ca, 0x2042), BS(SPCA536, 0)},
	{USB_DEVICE(0x08ca, 0x2050), BS(SPCA536, 0)},
	{USB_DEVICE(0x08ca, 0x2060), BS(SPCA536, 0)},
	{USB_DEVICE(0x0d64, 0x0303), BS(SPCA536, 0)},
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
	{}
};
MODULE_DEVICE_TABLE(usb, device_table);

/* -- device connect -- */
static int sd_probe(struct usb_interface *intf,
			const struct usb_device_id *id)
{
	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
				THIS_MODULE);
}

static struct usb_driver sd_driver = {
	.name = MODULE_NAME,
	.id_table = device_table,
	.probe = sd_probe,
	.disconnect = gspca_disconnect,
1406 1407 1408 1409
#ifdef CONFIG_PM
	.suspend = gspca_suspend,
	.resume = gspca_resume,
#endif
1410 1411 1412 1413 1414
};

/* -- module insert / remove -- */
static int __init sd_mod_init(void)
{
1415 1416 1417
	int ret;
	ret = usb_register(&sd_driver);
	if (ret < 0)
1418
		return ret;
1419
	PDEBUG(D_PROBE, "registered");
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
	return 0;
}
static void __exit sd_mod_exit(void)
{
	usb_deregister(&sd_driver);
	PDEBUG(D_PROBE, "deregistered");
}

module_init(sd_mod_init);
module_exit(sd_mod_exit);