spca505.c 23.2 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
/*
 * SPCA505 chip based cameras initialization data
 *
 * V4L2 by Jean-Francis 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 "spca505"

#include "gspca.h"

26 27
#define DRIVER_VERSION_NUMBER	KERNEL_VERSION(2, 1, 7)
static const char version[] = "2.1.7";
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

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

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

	int buflen;
	unsigned char tmpbuf[640 * 480 * 3 / 2]; /* YYUV per line */
	unsigned char tmpbuf2[640 * 480 * 2];	/* YUYV */

	unsigned char brightness;

	char subtype;
#define IntelPCCameraPro 0
#define Nxultra 1
};

/* 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 struct ctrl sd_ctrls[] = {
#define SD_BRIGHTNESS 0
	{
	    {
		.id      = V4L2_CID_BRIGHTNESS,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Brightness",
		.minimum = 0,
		.maximum = 255,
		.step    = 1,
		.default_value = 127,
	    },
	    .set = sd_setbrightness,
	    .get = sd_getbrightness,
	},
};

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static struct v4l2_pix_format vga_mode[] = {
	{160, 120, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 160 * 2,
		.sizeimage = 160 * 120 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 5},
	{176, 144, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 176 * 2,
		.sizeimage = 176 * 144 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 4},
	{320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 320 * 2,
		.sizeimage = 320 * 240 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 2},
	{352, 288, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 352 * 2,
		.sizeimage = 352 * 288 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 1},
	{640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 640 * 2,
		.sizeimage = 640 * 480 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 0},
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
};

#define SPCA50X_OFFSET_DATA 10

#define SPCA50X_REG_USB 0x02	/* spca505 501 */

#define SPCA50X_USB_CTRL 0x00	/* spca505 */
#define SPCA50X_CUSB_ENABLE 0x01 /* spca505 */
#define SPCA50X_REG_GLOBAL 0x03	/* spca505 */
#define SPCA50X_GMISC0_IDSEL 0x01 /* Global control device ID select spca505 */
#define SPCA50X_GLOBAL_MISC0 0x00 /* Global control miscellaneous 0 spca505 */

#define SPCA50X_GLOBAL_MISC1 0x01 /* 505 */
#define SPCA50X_GLOBAL_MISC3 0x03 /* 505 */
#define SPCA50X_GMISC3_SAA7113RST 0x20	/* Not sure about this one spca505 */

/*
 * Data to initialize a SPCA505. Common to the CCD and external modes
 */
114
static const __u16 spca505_init_data[][3] = {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	/* line	   bmRequest,value,index */
	/* 1819 */
	{SPCA50X_REG_GLOBAL, SPCA50X_GMISC3_SAA7113RST, SPCA50X_GLOBAL_MISC3},
	/* Sensor reset */
	/* 1822 */ {SPCA50X_REG_GLOBAL, 0x00, SPCA50X_GLOBAL_MISC3},
	/* 1825 */ {SPCA50X_REG_GLOBAL, 0x00, SPCA50X_GLOBAL_MISC1},
	/* Block USB reset */
	/* 1828 */ {SPCA50X_REG_GLOBAL, SPCA50X_GMISC0_IDSEL,
		SPCA50X_GLOBAL_MISC0},

	/* 1831 */ {0x5, 0x01, 0x10},
					/* Maybe power down some stuff */
	/* 1834 */ {0x5, 0x0f, 0x11},

	/* Setup internal CCD  ? */
	/* 1837 */ {0x6, 0x10, 0x08},
	/* 1840 */ {0x6, 0x00, 0x09},
	/* 1843 */ {0x6, 0x00, 0x0a},
	/* 1846 */ {0x6, 0x00, 0x0b},
	/* 1849 */ {0x6, 0x10, 0x0c},
	/* 1852 */ {0x6, 0x00, 0x0d},
	/* 1855 */ {0x6, 0x00, 0x0e},
	/* 1858 */ {0x6, 0x00, 0x0f},
	/* 1861 */ {0x6, 0x10, 0x10},
	/* 1864 */ {0x6, 0x02, 0x11},
	/* 1867 */ {0x6, 0x00, 0x12},
	/* 1870 */ {0x6, 0x04, 0x13},
	/* 1873 */ {0x6, 0x02, 0x14},
	/* 1876 */ {0x6, 0x8a, 0x51},
	/* 1879 */ {0x6, 0x40, 0x52},
	/* 1882 */ {0x6, 0xb6, 0x53},
	/* 1885 */ {0x6, 0x3d, 0x54},
	{}
};

/*
 * Data to initialize the camera using the internal CCD
 */
153
static const __u16 spca505_open_data_ccd[][3] = {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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
	/* line	   bmRequest,value,index */
	/* Internal CCD data set */
	/* 1891 */ {0x3, 0x04, 0x01},
	/* This could be a reset */
	/* 1894 */ {0x3, 0x00, 0x01},

	/* Setup compression and image registers. 0x6 and 0x7 seem to be
	   related to H&V hold, and are resolution mode specific */
		/* 1897 */ {0x4, 0x10, 0x01},
		/* DIFF(0x50), was (0x10) */
	/* 1900 */ {0x4, 0x00, 0x04},
	/* 1903 */ {0x4, 0x00, 0x05},
	/* 1906 */ {0x4, 0x20, 0x06},
	/* 1909 */ {0x4, 0x20, 0x07},

	/* 1912 */ {0x8, 0x0a, 0x00},
	/* DIFF (0x4a), was (0xa) */

	/* 1915 */ {0x5, 0x00, 0x10},
	/* 1918 */ {0x5, 0x00, 0x11},
	/* 1921 */ {0x5, 0x00, 0x00},
	/* DIFF not written */
	/* 1924 */ {0x5, 0x00, 0x01},
	/* DIFF not written */
	/* 1927 */ {0x5, 0x00, 0x02},
	/* DIFF not written */
	/* 1930 */ {0x5, 0x00, 0x03},
	/* DIFF not written */
	/* 1933 */ {0x5, 0x00, 0x04},
	/* DIFF not written */
		/* 1936 */ {0x5, 0x80, 0x05},
		/* DIFF not written */
		/* 1939 */ {0x5, 0xe0, 0x06},
		/* DIFF not written */
		/* 1942 */ {0x5, 0x20, 0x07},
		/* DIFF not written */
		/* 1945 */ {0x5, 0xa0, 0x08},
		/* DIFF not written */
		/* 1948 */ {0x5, 0x0, 0x12},
		/* DIFF not written */
	/* 1951 */ {0x5, 0x02, 0x0f},
	/* DIFF not written */
		/* 1954 */ {0x5, 0x10, 0x46},
		/* DIFF not written */
		/* 1957 */ {0x5, 0x8, 0x4a},
		/* DIFF not written */

	/* 1960 */ {0x3, 0x08, 0x03},
	/* DIFF (0x3,0x28,0x3) */
	/* 1963 */ {0x3, 0x08, 0x01},
	/* 1966 */ {0x3, 0x0c, 0x03},
	/* DIFF not written */
		/* 1969 */ {0x3, 0x21, 0x00},
		/* DIFF (0x39) */

/* Extra block copied from init to hopefully ensure CCD is in a sane state */
	/* 1837 */ {0x6, 0x10, 0x08},
	/* 1840 */ {0x6, 0x00, 0x09},
	/* 1843 */ {0x6, 0x00, 0x0a},
	/* 1846 */ {0x6, 0x00, 0x0b},
	/* 1849 */ {0x6, 0x10, 0x0c},
	/* 1852 */ {0x6, 0x00, 0x0d},
	/* 1855 */ {0x6, 0x00, 0x0e},
	/* 1858 */ {0x6, 0x00, 0x0f},
	/* 1861 */ {0x6, 0x10, 0x10},
	/* 1864 */ {0x6, 0x02, 0x11},
	/* 1867 */ {0x6, 0x00, 0x12},
	/* 1870 */ {0x6, 0x04, 0x13},
	/* 1873 */ {0x6, 0x02, 0x14},
	/* 1876 */ {0x6, 0x8a, 0x51},
	/* 1879 */ {0x6, 0x40, 0x52},
	/* 1882 */ {0x6, 0xb6, 0x53},
	/* 1885 */ {0x6, 0x3d, 0x54},
	/* End of extra block */

		/* 1972 */ {0x6, 0x3f, 0x1},
		/* Block skipped */
	/* 1975 */ {0x6, 0x10, 0x02},
	/* 1978 */ {0x6, 0x64, 0x07},
	/* 1981 */ {0x6, 0x10, 0x08},
	/* 1984 */ {0x6, 0x00, 0x09},
	/* 1987 */ {0x6, 0x00, 0x0a},
	/* 1990 */ {0x6, 0x00, 0x0b},
	/* 1993 */ {0x6, 0x10, 0x0c},
	/* 1996 */ {0x6, 0x00, 0x0d},
	/* 1999 */ {0x6, 0x00, 0x0e},
	/* 2002 */ {0x6, 0x00, 0x0f},
	/* 2005 */ {0x6, 0x10, 0x10},
	/* 2008 */ {0x6, 0x02, 0x11},
	/* 2011 */ {0x6, 0x00, 0x12},
	/* 2014 */ {0x6, 0x04, 0x13},
	/* 2017 */ {0x6, 0x02, 0x14},
	/* 2020 */ {0x6, 0x8a, 0x51},
	/* 2023 */ {0x6, 0x40, 0x52},
	/* 2026 */ {0x6, 0xb6, 0x53},
	/* 2029 */ {0x6, 0x3d, 0x54},
	/* 2032 */ {0x6, 0x60, 0x57},
	/* 2035 */ {0x6, 0x20, 0x58},
	/* 2038 */ {0x6, 0x15, 0x59},
	/* 2041 */ {0x6, 0x05, 0x5a},

	/* 2044 */ {0x5, 0x01, 0xc0},
	/* 2047 */ {0x5, 0x10, 0xcb},
		/* 2050 */ {0x5, 0x80, 0xc1},
		/* */
		/* 2053 */ {0x5, 0x0, 0xc2},
		/* 4 was 0 */
	/* 2056 */ {0x5, 0x00, 0xca},
		/* 2059 */ {0x5, 0x80, 0xc1},
		/*  */
	/* 2062 */ {0x5, 0x04, 0xc2},
	/* 2065 */ {0x5, 0x00, 0xca},
		/* 2068 */ {0x5, 0x0, 0xc1},
		/*  */
	/* 2071 */ {0x5, 0x00, 0xc2},
	/* 2074 */ {0x5, 0x00, 0xca},
		/* 2077 */ {0x5, 0x40, 0xc1},
		/* */
	/* 2080 */ {0x5, 0x17, 0xc2},
	/* 2083 */ {0x5, 0x00, 0xca},
		/* 2086 */ {0x5, 0x80, 0xc1},
		/* */
	/* 2089 */ {0x5, 0x06, 0xc2},
	/* 2092 */ {0x5, 0x00, 0xca},
		/* 2095 */ {0x5, 0x80, 0xc1},
		/* */
	/* 2098 */ {0x5, 0x04, 0xc2},
	/* 2101 */ {0x5, 0x00, 0xca},

	/* 2104 */ {0x3, 0x4c, 0x3},
	/* 2107 */ {0x3, 0x18, 0x1},

	/* 2110 */ {0x6, 0x70, 0x51},
	/* 2113 */ {0x6, 0xbe, 0x53},
	/* 2116 */ {0x6, 0x71, 0x57},
	/* 2119 */ {0x6, 0x20, 0x58},
	/* 2122 */ {0x6, 0x05, 0x59},
	/* 2125 */ {0x6, 0x15, 0x5a},

	/* 2128 */ {0x4, 0x00, 0x08},
	/* Compress = OFF (0x1 to turn on) */
	/* 2131 */ {0x4, 0x12, 0x09},
	/* 2134 */ {0x4, 0x21, 0x0a},
	/* 2137 */ {0x4, 0x10, 0x0b},
	/* 2140 */ {0x4, 0x21, 0x0c},
	/* 2143 */ {0x4, 0x05, 0x00},
	/* was 5 (Image Type ? ) */
	/* 2146 */ {0x4, 0x00, 0x01},

	/* 2149 */ {0x6, 0x3f, 0x01},

	/* 2152 */ {0x4, 0x00, 0x04},
	/* 2155 */ {0x4, 0x00, 0x05},
	/* 2158 */ {0x4, 0x40, 0x06},
	/* 2161 */ {0x4, 0x40, 0x07},

	/* 2164 */ {0x6, 0x1c, 0x17},
	/* 2167 */ {0x6, 0xe2, 0x19},
	/* 2170 */ {0x6, 0x1c, 0x1b},
	/* 2173 */ {0x6, 0xe2, 0x1d},
	/* 2176 */ {0x6, 0xaa, 0x1f},
	/* 2179 */ {0x6, 0x70, 0x20},

	/* 2182 */ {0x5, 0x01, 0x10},
	/* 2185 */ {0x5, 0x00, 0x11},
	/* 2188 */ {0x5, 0x01, 0x00},
	/* 2191 */ {0x5, 0x05, 0x01},
		/* 2194 */ {0x5, 0x00, 0xc1},
		/* */
	/* 2197 */ {0x5, 0x00, 0xc2},
	/* 2200 */ {0x5, 0x00, 0xca},

	/* 2203 */ {0x6, 0x70, 0x51},
	/* 2206 */ {0x6, 0xbe, 0x53},
	{}
};

/*
   Made by Tomasz Zablocki (skalamandra@poczta.onet.pl)
 * SPCA505b chip based cameras initialization data
 *
 */
/* jfm */
#define initial_brightness 0x7f	/* 0x0(white)-0xff(black) */
/* #define initial_brightness 0x0	//0x0(white)-0xff(black) */
/*
 * Data to initialize a SPCA505. Common to the CCD and external modes
 */
342
static const __u16 spca505b_init_data[][3] = {
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
/* start */
	{0x02, 0x00, 0x00},		/* init */
	{0x02, 0x00, 0x01},
	{0x02, 0x00, 0x02},
	{0x02, 0x00, 0x03},
	{0x02, 0x00, 0x04},
	{0x02, 0x00, 0x05},
	{0x02, 0x00, 0x06},
	{0x02, 0x00, 0x07},
	{0x02, 0x00, 0x08},
	{0x02, 0x00, 0x09},
	{0x03, 0x00, 0x00},
	{0x03, 0x00, 0x01},
	{0x03, 0x00, 0x02},
	{0x03, 0x00, 0x03},
	{0x03, 0x00, 0x04},
	{0x03, 0x00, 0x05},
	{0x03, 0x00, 0x06},
	{0x04, 0x00, 0x00},
	{0x04, 0x00, 0x02},
	{0x04, 0x00, 0x04},
	{0x04, 0x00, 0x05},
	{0x04, 0x00, 0x06},
	{0x04, 0x00, 0x07},
	{0x04, 0x00, 0x08},
	{0x04, 0x00, 0x09},
	{0x04, 0x00, 0x0a},
	{0x04, 0x00, 0x0b},
	{0x04, 0x00, 0x0c},
	{0x07, 0x00, 0x00},
	{0x07, 0x00, 0x03},
	{0x08, 0x00, 0x00},
	{0x08, 0x00, 0x01},
	{0x08, 0x00, 0x02},
	{0x00, 0x01, 0x00},
	{0x00, 0x01, 0x01},
	{0x00, 0x01, 0x34},
	{0x00, 0x01, 0x35},
	{0x06, 0x18, 0x08},
	{0x06, 0xfc, 0x09},
	{0x06, 0xfc, 0x0a},
	{0x06, 0xfc, 0x0b},
	{0x06, 0x18, 0x0c},
	{0x06, 0xfc, 0x0d},
	{0x06, 0xfc, 0x0e},
	{0x06, 0xfc, 0x0f},
	{0x06, 0x18, 0x10},
	{0x06, 0xfe, 0x12},
	{0x06, 0x00, 0x11},
	{0x06, 0x00, 0x14},
	{0x06, 0x00, 0x13},
	{0x06, 0x28, 0x51},
	{0x06, 0xff, 0x53},
	{0x02, 0x00, 0x08},

	{0x03, 0x00, 0x03},
	{0x03, 0x10, 0x03},
	{}
};

/*
 * Data to initialize the camera using the internal CCD
 */
406
static const __u16 spca505b_open_data_ccd[][3] = {
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

/* {0x02,0x00,0x00}, */
	{0x03, 0x04, 0x01},		/* rst */
	{0x03, 0x00, 0x01},
	{0x03, 0x00, 0x00},
	{0x03, 0x21, 0x00},
	{0x03, 0x00, 0x04},
	{0x03, 0x00, 0x03},
	{0x03, 0x18, 0x03},
	{0x03, 0x08, 0x01},
	{0x03, 0x1c, 0x03},
	{0x03, 0x5c, 0x03},
	{0x03, 0x5c, 0x03},
	{0x03, 0x18, 0x01},

/* same as 505 */
	{0x04, 0x10, 0x01},
	{0x04, 0x00, 0x04},
	{0x04, 0x00, 0x05},
	{0x04, 0x20, 0x06},
	{0x04, 0x20, 0x07},

	{0x08, 0x0a, 0x00},

	{0x05, 0x00, 0x10},
	{0x05, 0x00, 0x11},
	{0x05, 0x00, 0x12},
	{0x05, 0x6f, 0x00},
	{0x05, initial_brightness >> 6, 0x00},
	{0x05, initial_brightness << 2, 0x01},
	{0x05, 0x00, 0x02},
	{0x05, 0x01, 0x03},
	{0x05, 0x00, 0x04},
	{0x05, 0x03, 0x05},
	{0x05, 0xe0, 0x06},
	{0x05, 0x20, 0x07},
	{0x05, 0xa0, 0x08},
	{0x05, 0x00, 0x12},
	{0x05, 0x02, 0x0f},
	{0x05, 128, 0x14},		/* max exposure off (0=on) */
	{0x05, 0x01, 0xb0},
	{0x05, 0x01, 0xbf},
	{0x03, 0x02, 0x06},
	{0x05, 0x10, 0x46},
	{0x05, 0x08, 0x4a},

	{0x06, 0x00, 0x01},
	{0x06, 0x10, 0x02},
	{0x06, 0x64, 0x07},
	{0x06, 0x18, 0x08},
	{0x06, 0xfc, 0x09},
	{0x06, 0xfc, 0x0a},
	{0x06, 0xfc, 0x0b},
	{0x04, 0x00, 0x01},
	{0x06, 0x18, 0x0c},
	{0x06, 0xfc, 0x0d},
	{0x06, 0xfc, 0x0e},
	{0x06, 0xfc, 0x0f},
	{0x06, 0x11, 0x10},		/* contrast */
	{0x06, 0x00, 0x11},
	{0x06, 0xfe, 0x12},
	{0x06, 0x00, 0x13},
	{0x06, 0x00, 0x14},
	{0x06, 0x9d, 0x51},
	{0x06, 0x40, 0x52},
	{0x06, 0x7c, 0x53},
	{0x06, 0x40, 0x54},
	{0x06, 0x02, 0x57},
	{0x06, 0x03, 0x58},
	{0x06, 0x15, 0x59},
	{0x06, 0x05, 0x5a},
	{0x06, 0x03, 0x56},
	{0x06, 0x02, 0x3f},
	{0x06, 0x00, 0x40},
	{0x06, 0x39, 0x41},
	{0x06, 0x69, 0x42},
	{0x06, 0x87, 0x43},
	{0x06, 0x9e, 0x44},
	{0x06, 0xb1, 0x45},
	{0x06, 0xbf, 0x46},
	{0x06, 0xcc, 0x47},
	{0x06, 0xd5, 0x48},
	{0x06, 0xdd, 0x49},
	{0x06, 0xe3, 0x4a},
	{0x06, 0xe8, 0x4b},
	{0x06, 0xed, 0x4c},
	{0x06, 0xf2, 0x4d},
	{0x06, 0xf7, 0x4e},
	{0x06, 0xfc, 0x4f},
	{0x06, 0xff, 0x50},

	{0x05, 0x01, 0xc0},
	{0x05, 0x10, 0xcb},
	{0x05, 0x40, 0xc1},
	{0x05, 0x04, 0xc2},
	{0x05, 0x00, 0xca},
	{0x05, 0x40, 0xc1},
	{0x05, 0x09, 0xc2},
	{0x05, 0x00, 0xca},
	{0x05, 0xc0, 0xc1},
	{0x05, 0x09, 0xc2},
	{0x05, 0x00, 0xca},
	{0x05, 0x40, 0xc1},
	{0x05, 0x59, 0xc2},
	{0x05, 0x00, 0xca},
	{0x04, 0x00, 0x01},
	{0x05, 0x80, 0xc1},
	{0x05, 0xec, 0xc2},
	{0x05, 0x0, 0xca},

	{0x06, 0x02, 0x57},
	{0x06, 0x01, 0x58},
	{0x06, 0x15, 0x59},
	{0x06, 0x0a, 0x5a},
	{0x06, 0x01, 0x57},
	{0x06, 0x8a, 0x03},
	{0x06, 0x0a, 0x6c},
	{0x06, 0x30, 0x01},
	{0x06, 0x20, 0x02},
	{0x06, 0x00, 0x03},

	{0x05, 0x8c, 0x25},

	{0x06, 0x4d, 0x51},		/* maybe saturation (4d) */
	{0x06, 0x84, 0x53},		/* making green (84) */
	{0x06, 0x00, 0x57},		/* sharpness (1) */
	{0x06, 0x18, 0x08},
	{0x06, 0xfc, 0x09},
	{0x06, 0xfc, 0x0a},
	{0x06, 0xfc, 0x0b},
	{0x06, 0x18, 0x0c},		/* maybe hue (18) */
	{0x06, 0xfc, 0x0d},
	{0x06, 0xfc, 0x0e},
	{0x06, 0xfc, 0x0f},
	{0x06, 0x18, 0x10},		/* maybe contrast (18) */

	{0x05, 0x01, 0x02},

	{0x04, 0x00, 0x08},		/* compression */
	{0x04, 0x12, 0x09},
	{0x04, 0x21, 0x0a},
	{0x04, 0x10, 0x0b},
	{0x04, 0x21, 0x0c},
	{0x04, 0x1d, 0x00},		/* imagetype (1d) */
	{0x04, 0x41, 0x01},		/* hardware snapcontrol */

	{0x04, 0x00, 0x04},
	{0x04, 0x00, 0x05},
	{0x04, 0x10, 0x06},
	{0x04, 0x10, 0x07},
	{0x04, 0x40, 0x06},
	{0x04, 0x40, 0x07},
	{0x04, 0x00, 0x04},
	{0x04, 0x00, 0x05},

	{0x06, 0x1c, 0x17},
	{0x06, 0xe2, 0x19},
	{0x06, 0x1c, 0x1b},
	{0x06, 0xe2, 0x1d},
	{0x06, 0x5f, 0x1f},
	{0x06, 0x32, 0x20},

	{0x05, initial_brightness >> 6, 0x00},
	{0x05, initial_brightness << 2, 0x01},
	{0x05, 0x06, 0xc1},
	{0x05, 0x58, 0xc2},
	{0x05, 0x0, 0xca},
	{0x05, 0x0, 0x11},
	{}
};

static int reg_write(struct usb_device *dev,
		     __u16 reg, __u16 index, __u16 value)
{
	int ret;

	ret = usb_control_msg(dev,
			usb_sndctrlpipe(dev, 0),
			reg,
			USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			value, index, NULL, 0, 500);
	PDEBUG(D_PACK, "reg write: 0x%02x,0x%02x:0x%02x, 0x%x",
		reg, index, value, ret);
	if (ret < 0)
		PDEBUG(D_ERR, "reg write: error %d", ret);
	return ret;
}

/* returns: negative is error, pos or zero is data */
596
static int reg_read(struct gspca_dev *gspca_dev,
597 598 599 600 601 602
			__u16 reg,	/* bRequest */
			__u16 index,	/* wIndex */
			__u16 length)	/* wLength (1 or 2 only) */
{
	int ret;

603 604 605
	gspca_dev->usb_buf[1] = 0;
	ret = usb_control_msg(gspca_dev->dev,
			usb_rcvctrlpipe(gspca_dev->dev, 0),
606 607 608 609
			reg,
			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			(__u16) 0,		/* value */
			(__u16) index,
610
			gspca_dev->usb_buf, length,
611 612 613 614 615
			500);			/* timeout */
	if (ret < 0) {
		PDEBUG(D_ERR, "reg_read err %d", ret);
		return -1;
	}
616
	return (gspca_dev->usb_buf[1] << 8) + gspca_dev->usb_buf[0];
617 618 619
}

static int write_vector(struct gspca_dev *gspca_dev,
620
			const __u16 data[][3])
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
{
	struct usb_device *dev = gspca_dev->dev;
	int ret, i = 0;

	while (data[i][0] != 0 || data[i][1] != 0 || data[i][2] != 0) {
		ret = reg_write(dev, data[i][0], data[i][2], data[i][1]);
		if (ret < 0) {
			PDEBUG(D_ERR,
				"Register write failed for 0x%x,0x%x,0x%x",
				data[i][0], data[i][1], data[i][2]);
			return ret;
		}
		i++;
	}
	return 0;
}

/* 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;
	__u16 vendor;
	__u16 product;

	vendor = id->idVendor;
	product = id->idProduct;
	switch (vendor) {
	case 0x041e:		/* Creative cameras */
/*		switch (product) { */
/*		case 0x401d:	 * here505b */
			sd->subtype = Nxultra;
/*			break; */
/*		} */
		break;
	case 0x0733:	/* Rebadged ViewQuest (Intel) and ViewQuest cameras */
/*		switch (product) { */
/*		case 0x0430: */
/*		fixme: may be UsbGrabberPV321 BRIDGE_SPCA506 SENSOR_SAA7113 */
			sd->subtype = IntelPCCameraPro;
/*			break; */
/*		} */
		break;
	}

	cam = &gspca_dev->cam;
	cam->dev_name = (char *) id->driver_info;
	cam->epaddr = 0x01;
	cam->cam_mode = vga_mode;
	if (sd->subtype != IntelPCCameraPro)
		cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
	else			/* no 640x480 for IntelPCCameraPro */
		cam->nmodes = sizeof vga_mode / sizeof vga_mode[0] - 1;
	sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;

	if (sd->subtype == Nxultra) {
		if (write_vector(gspca_dev, spca505b_init_data))
			return -EIO;
	} else {
		if (write_vector(gspca_dev, spca505_init_data))
			return -EIO;
	}
	return 0;
}

/* this function is called at open time */
static int sd_open(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	int ret;

	PDEBUG(D_STREAM, "Initializing SPCA505");
	if (sd->subtype == Nxultra)
		write_vector(gspca_dev, spca505b_open_data_ccd);
	else
		write_vector(gspca_dev, spca505_open_data_ccd);
698
	ret = reg_read(gspca_dev, 6, 0x16, 2);
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

	if (ret < 0) {
		PDEBUG(D_ERR|D_STREAM,
		       "register read failed for after vector read err = %d",
		       ret);
		return -EIO;
	}
	PDEBUG(D_STREAM,
		"After vector read returns : 0x%x should be 0x0101",
		ret & 0xffff);

	ret = reg_write(gspca_dev->dev, 6, 0x16, 0x0a);
	if (ret < 0) {
		PDEBUG(D_ERR, "register write failed for (6,0xa,0x16) err=%d",
		       ret);
		return -EIO;
	}
	reg_write(gspca_dev->dev, 5, 0xc2, 18);
	return 0;
}

static void sd_start(struct gspca_dev *gspca_dev)
{
	struct usb_device *dev = gspca_dev->dev;
	int ret;

	/* necessary because without it we can see stream
	 * only once after loading module */
	/* stopping usb registers Tomasz change */
	reg_write(dev, 0x02, 0x0, 0x0);
729
	switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	case 0:
		reg_write(dev, 0x04, 0x00, 0x00);
		reg_write(dev, 0x04, 0x06, 0x10);
		reg_write(dev, 0x04, 0x07, 0x10);
		break;
	case 1:
		reg_write(dev, 0x04, 0x00, 0x01);
		reg_write(dev, 0x04, 0x06, 0x1a);
		reg_write(dev, 0x04, 0x07, 0x1a);
		break;
	case 2:
		reg_write(dev, 0x04, 0x00, 0x02);
		reg_write(dev, 0x04, 0x06, 0x1c);
		reg_write(dev, 0x04, 0x07, 0x1d);
		break;
	case 4:
		reg_write(dev, 0x04, 0x00, 0x04);
		reg_write(dev, 0x04, 0x06, 0x34);
		reg_write(dev, 0x04, 0x07, 0x34);
		break;
	default:
/*	case 5: */
		reg_write(dev, 0x04, 0x00, 0x05);
		reg_write(dev, 0x04, 0x06, 0x40);
		reg_write(dev, 0x04, 0x07, 0x40);
		break;
	}
/* Enable ISO packet machine - should we do this here or in ISOC init ? */
	ret = reg_write(dev, SPCA50X_REG_USB,
			 SPCA50X_USB_CTRL,
			 SPCA50X_CUSB_ENABLE);

/*	reg_write(dev, 0x5, 0x0, 0x0); */
/*	reg_write(dev, 0x5, 0x0, 0x1); */
/*	reg_write(dev, 0x5, 0x11, 0x2); */
}

static void sd_stopN(struct gspca_dev *gspca_dev)
{
	/* Disable ISO packet machine */
	reg_write(gspca_dev->dev, 0x02, 0x00, 0x00);
}

static void sd_stop0(struct gspca_dev *gspca_dev)
{
}

/* this function is called at close time */
static void sd_close(struct gspca_dev *gspca_dev)
{
	/* This maybe reset or power control */
	reg_write(gspca_dev->dev, 0x03, 0x03, 0x20);
	reg_write(gspca_dev->dev, 0x03, 0x01, 0x0);
	reg_write(gspca_dev->dev, 0x03, 0x00, 0x1);
	reg_write(gspca_dev->dev, 0x05, 0x10, 0x1);
	reg_write(gspca_dev->dev, 0x05, 0x11, 0xf);
}

/* convert YYUV per line to YUYV (YUV 4:2:2) */
static void yyuv_decode(unsigned char *out,
			unsigned char *in,
			int width,
			int height)
{
	unsigned char *Ui, *Vi, *yi, *yi1;
	unsigned char *out1;
	int i, j;

	yi = in;
	for (i = height / 2; --i >= 0; ) {
		out1 = out + width * 2;		/* next line */
		yi1 = yi + width;
		Ui = yi1 + width;
		Vi = Ui + width / 2;
		for (j = width / 2; --j >= 0; ) {
			*out++ = 128 + *yi++;
			*out++ = 128 + *Ui;
			*out++ = 128 + *yi++;
			*out++ = 128 + *Vi;

			*out1++ = 128 + *yi1++;
			*out1++ = 128 + *Ui++;
			*out1++ = 128 + *yi1++;
			*out1++ = 128 + *Vi++;
		}
		yi += width * 2;
		out = out1;
	}
}

static void sd_pkt_scan(struct gspca_dev *gspca_dev,
			struct gspca_frame *frame,	/* target */
822
			__u8 *data,			/* isoc packet */
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
			int len)			/* iso packet length */
{
	struct sd *sd = (struct sd *) gspca_dev;

	switch (data[0]) {
	case 0:				/* start of frame */
		if (gspca_dev->last_packet_type == FIRST_PACKET) {
			yyuv_decode(sd->tmpbuf2, sd->tmpbuf,
					gspca_dev->width,
					gspca_dev->height);
			frame = gspca_frame_add(gspca_dev,
						LAST_PACKET,
						frame,
						sd->tmpbuf2,
						gspca_dev->width
							* gspca_dev->height
							* 2);
		}
		gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
				data, 0);
		data += SPCA50X_OFFSET_DATA;
		len -= SPCA50X_OFFSET_DATA;
		if (len > 0)
			memcpy(sd->tmpbuf, data, len);
		else
			len = 0;
		sd->buflen = len;
		return;
	case 0xff:			/* drop */
/*		gspca_dev->last_packet_type = DISCARD_PACKET; */
		return;
	}
	data += 1;
	len -= 1;
	memcpy(&sd->tmpbuf[sd->buflen], data, len);
	sd->buflen += len;
}

static void setbrightness(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	__u8 brightness = sd->brightness;
	reg_write(gspca_dev->dev, 5, 0x00, (255 - brightness) >> 6);
	reg_write(gspca_dev->dev, 5, 0x01, (255 - brightness) << 2);

}
static void getbrightness(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->brightness = 255
875 876
		- ((reg_read(gspca_dev, 5, 0x01, 1) >> 2)
			+ (reg_read(gspca_dev, 5, 0x0, 1) << 6));
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
}

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;

	getbrightness(gspca_dev);
	*val = sd->brightness;
	return 0;
}

/* sub-driver description */
899
static const struct sd_desc sd_desc = {
900 901 902 903 904 905 906 907 908 909 910 911 912 913
	.name = MODULE_NAME,
	.ctrls = sd_ctrls,
	.nctrls = ARRAY_SIZE(sd_ctrls),
	.config = sd_config,
	.open = sd_open,
	.start = sd_start,
	.stopN = sd_stopN,
	.stop0 = sd_stop0,
	.close = sd_close,
	.pkt_scan = sd_pkt_scan,
};

/* -- module initialisation -- */
#define DVNM(name) .driver_info = (kernel_ulong_t) name
914
static const __devinitdata struct usb_device_id device_table[] = {
915 916 917 918 919 920 921 922 923 924
	{USB_DEVICE(0x041e, 0x401d), DVNM("Creative Webcam NX ULTRA")},
	{USB_DEVICE(0x0733, 0x0430), DVNM("Intel PC Camera Pro")},
	{}
};
MODULE_DEVICE_TABLE(usb, device_table);

/* -- device connect -- */
static int sd_probe(struct usb_interface *intf,
			const struct usb_device_id *id)
{
925
	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
				THIS_MODULE);
}

static struct usb_driver sd_driver = {
	.name = MODULE_NAME,
	.id_table = device_table,
	.probe = sd_probe,
	.disconnect = gspca_disconnect,
};

/* -- module insert / remove -- */
static int __init sd_mod_init(void)
{
	if (usb_register(&sd_driver) < 0)
		return -1;
	PDEBUG(D_PROBE, "v%s registered", version);
	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);