xref: /linux/drivers/media/usb/gspca/pac7302.c (revision 52173c5f3f6476eb9100691ec47b10d6740eed4a)
10c0d06caSMauro Carvalho Chehab /*
20c0d06caSMauro Carvalho Chehab  * Pixart PAC7302 driver
30c0d06caSMauro Carvalho Chehab  *
40c0d06caSMauro Carvalho Chehab  * Copyright (C) 2008-2012 Jean-Francois Moine <http://moinejf.free.fr>
50c0d06caSMauro Carvalho Chehab  * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
60c0d06caSMauro Carvalho Chehab  *
70c0d06caSMauro Carvalho Chehab  * Separated from Pixart PAC7311 library by Márton Németh
80c0d06caSMauro Carvalho Chehab  * Camera button input handling by Márton Németh <nm127@freemail.hu>
90c0d06caSMauro Carvalho Chehab  * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
100c0d06caSMauro Carvalho Chehab  *
110c0d06caSMauro Carvalho Chehab  * This program is free software; you can redistribute it and/or modify
120c0d06caSMauro Carvalho Chehab  * it under the terms of the GNU General Public License as published by
130c0d06caSMauro Carvalho Chehab  * the Free Software Foundation; either version 2 of the License, or
140c0d06caSMauro Carvalho Chehab  * any later version.
150c0d06caSMauro Carvalho Chehab  *
160c0d06caSMauro Carvalho Chehab  * This program is distributed in the hope that it will be useful,
170c0d06caSMauro Carvalho Chehab  * but WITHOUT ANY WARRANTY; without even the implied warranty of
180c0d06caSMauro Carvalho Chehab  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
190c0d06caSMauro Carvalho Chehab  * GNU General Public License for more details.
200c0d06caSMauro Carvalho Chehab  */
210c0d06caSMauro Carvalho Chehab 
220c0d06caSMauro Carvalho Chehab /*
230c0d06caSMauro Carvalho Chehab  * Some documentation about various registers as determined by trial and error.
240c0d06caSMauro Carvalho Chehab  *
25b1a19c01SFrank Schäfer  * Register page 0:
26b1a19c01SFrank Schäfer  *
27b1a19c01SFrank Schäfer  * Address	Description
286f9b3312SFrank Schäfer  * 0x01		Red balance control
296f9b3312SFrank Schäfer  * 0x02		Green balance control
306f9b3312SFrank Schäfer  * 0x03		Blue balance control
31780d6170SFrank Schäfer  *		     The Windows driver uses a quadratic approach to map
32780d6170SFrank Schäfer  *		     the settable values (0-200) on register values:
336f9b3312SFrank Schäfer  *		     min=0x20, default=0x40, max=0x80
346f9b3312SFrank Schäfer  * 0x0f-0x20	Color and saturation control
35780d6170SFrank Schäfer  * 0xa2-0xab	Brightness, contrast and gamma control
36b1a19c01SFrank Schäfer  * 0xb6		Sharpness control (bits 0-4)
37b1a19c01SFrank Schäfer  *
380c0d06caSMauro Carvalho Chehab  * Register page 1:
390c0d06caSMauro Carvalho Chehab  *
400c0d06caSMauro Carvalho Chehab  * Address	Description
410c0d06caSMauro Carvalho Chehab  * 0x78		Global control, bit 6 controls the LED (inverted)
420c0d06caSMauro Carvalho Chehab  * 0x80		Compression balance, 2 interesting settings:
430c0d06caSMauro Carvalho Chehab  *		0x0f Default
440c0d06caSMauro Carvalho Chehab  *		0x50 Values >= this switch the camera to a lower compression,
450c0d06caSMauro Carvalho Chehab  *		     using the same table for both luminance and chrominance.
460c0d06caSMauro Carvalho Chehab  *		     This gives a sharper picture. Only usable when running
470c0d06caSMauro Carvalho Chehab  *		     at < 15 fps! Note currently the driver does not use this
480c0d06caSMauro Carvalho Chehab  *		     as the quality gain is small and the generated JPG-s are
490c0d06caSMauro Carvalho Chehab  *		     only understood by v4l-utils >= 0.8.9
500c0d06caSMauro Carvalho Chehab  *
510c0d06caSMauro Carvalho Chehab  * Register page 3:
520c0d06caSMauro Carvalho Chehab  *
530c0d06caSMauro Carvalho Chehab  * Address	Description
540c0d06caSMauro Carvalho Chehab  * 0x02		Clock divider 3-63, fps = 90 / val. Must be a multiple of 3 on
550c0d06caSMauro Carvalho Chehab  *		the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
560c0d06caSMauro Carvalho Chehab  * 0x03		Variable framerate ctrl reg2==3: 0 -> ~30 fps, 255 -> ~22fps
570c0d06caSMauro Carvalho Chehab  * 0x04		Another var framerate ctrl reg2==3, reg3==0: 0 -> ~30 fps,
580c0d06caSMauro Carvalho Chehab  *		63 -> ~27 fps, the 2 msb's must always be 1 !!
590c0d06caSMauro Carvalho Chehab  * 0x05		Another var framerate ctrl reg2==3, reg3==0, reg4==0xc0:
600c0d06caSMauro Carvalho Chehab  *		1 -> ~30 fps, 2 -> ~20 fps
610c0d06caSMauro Carvalho Chehab  * 0x0e		Exposure bits 0-7, 0-448, 0 = use full frame time
620c0d06caSMauro Carvalho Chehab  * 0x0f		Exposure bit 8, 0-448, 448 = no exposure at all
630c0d06caSMauro Carvalho Chehab  * 0x10		Gain 0-31
640c0d06caSMauro Carvalho Chehab  * 0x12		Another gain 0-31, unlike 0x10 this one seems to start with an
650c0d06caSMauro Carvalho Chehab  *		amplification value of 1 rather then 0 at its lowest setting
660c0d06caSMauro Carvalho Chehab  * 0x21		Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
670c0d06caSMauro Carvalho Chehab  * 0x80		Another framerate control, best left at 1, moving it from 1 to
680c0d06caSMauro Carvalho Chehab  *		2 causes the framerate to become 3/4th of what it was, and
690c0d06caSMauro Carvalho Chehab  *		also seems to cause pixel averaging, resulting in an effective
700c0d06caSMauro Carvalho Chehab  *		resolution of 320x240 and thus a much blockier image
710c0d06caSMauro Carvalho Chehab  *
720c0d06caSMauro Carvalho Chehab  * The registers are accessed in the following functions:
730c0d06caSMauro Carvalho Chehab  *
740c0d06caSMauro Carvalho Chehab  * Page | Register   | Function
750c0d06caSMauro Carvalho Chehab  * -----+------------+---------------------------------------------------
762b34e9d1SFrank Schäfer  *  0   | 0x01       | setredbalance()
772b34e9d1SFrank Schäfer  *  0   | 0x03       | setbluebalance()
780c0d06caSMauro Carvalho Chehab  *  0   | 0x0f..0x20 | setcolors()
790c0d06caSMauro Carvalho Chehab  *  0   | 0xa2..0xab | setbrightcont()
80b1a19c01SFrank Schäfer  *  0   | 0xb6       | setsharpness()
810c0d06caSMauro Carvalho Chehab  *  0   | 0xc6       | setwhitebalance()
820c0d06caSMauro Carvalho Chehab  *  0   | 0xdc       | setbrightcont(), setcolors()
830c0d06caSMauro Carvalho Chehab  *  3   | 0x02       | setexposure()
840c0d06caSMauro Carvalho Chehab  *  3   | 0x10, 0x12 | setgain()
850c0d06caSMauro Carvalho Chehab  *  3   | 0x11       | setcolors(), setgain(), setexposure(), sethvflip()
860c0d06caSMauro Carvalho Chehab  *  3   | 0x21       | sethvflip()
870c0d06caSMauro Carvalho Chehab  */
880c0d06caSMauro Carvalho Chehab 
890c0d06caSMauro Carvalho Chehab #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
900c0d06caSMauro Carvalho Chehab 
910c0d06caSMauro Carvalho Chehab #include <linux/input.h>
920c0d06caSMauro Carvalho Chehab #include "gspca.h"
930c0d06caSMauro Carvalho Chehab /* Include pac common sof detection functions */
940c0d06caSMauro Carvalho Chehab #include "pac_common.h"
950c0d06caSMauro Carvalho Chehab 
962b34e9d1SFrank Schäfer #define PAC7302_RGB_BALANCE_MIN		  0
972b34e9d1SFrank Schäfer #define PAC7302_RGB_BALANCE_MAX		200
982b34e9d1SFrank Schäfer #define PAC7302_RGB_BALANCE_DEFAULT	100
990c0d06caSMauro Carvalho Chehab #define PAC7302_GAIN_DEFAULT		 15
1000c0d06caSMauro Carvalho Chehab #define PAC7302_GAIN_KNEE		 42
1010c0d06caSMauro Carvalho Chehab #define PAC7302_EXPOSURE_DEFAULT	 66 /* 33 ms / 30 fps */
1020c0d06caSMauro Carvalho Chehab #define PAC7302_EXPOSURE_KNEE		133 /* 66 ms / 15 fps */
1030c0d06caSMauro Carvalho Chehab 
1041ddc9f75SMauro Carvalho Chehab MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>, Thomas Kaiser thomas@kaiser-linux.li");
1050c0d06caSMauro Carvalho Chehab MODULE_DESCRIPTION("Pixart PAC7302");
1060c0d06caSMauro Carvalho Chehab MODULE_LICENSE("GPL");
1070c0d06caSMauro Carvalho Chehab 
1080c0d06caSMauro Carvalho Chehab struct sd {
1090c0d06caSMauro Carvalho Chehab 	struct gspca_dev gspca_dev;		/* !! must be the first item */
1100c0d06caSMauro Carvalho Chehab 
1110c0d06caSMauro Carvalho Chehab 	struct { /* brightness / contrast cluster */
1120c0d06caSMauro Carvalho Chehab 		struct v4l2_ctrl *brightness;
1130c0d06caSMauro Carvalho Chehab 		struct v4l2_ctrl *contrast;
1140c0d06caSMauro Carvalho Chehab 	};
1150c0d06caSMauro Carvalho Chehab 	struct v4l2_ctrl *saturation;
1160c0d06caSMauro Carvalho Chehab 	struct v4l2_ctrl *white_balance;
1170c0d06caSMauro Carvalho Chehab 	struct v4l2_ctrl *red_balance;
1180c0d06caSMauro Carvalho Chehab 	struct v4l2_ctrl *blue_balance;
1190c0d06caSMauro Carvalho Chehab 	struct { /* flip cluster */
1200c0d06caSMauro Carvalho Chehab 		struct v4l2_ctrl *hflip;
1210c0d06caSMauro Carvalho Chehab 		struct v4l2_ctrl *vflip;
1220c0d06caSMauro Carvalho Chehab 	};
123b1a19c01SFrank Schäfer 	struct v4l2_ctrl *sharpness;
1240c0d06caSMauro Carvalho Chehab 	u8 flags;
1250c0d06caSMauro Carvalho Chehab #define FL_HFLIP 0x01		/* mirrored by default */
1260c0d06caSMauro Carvalho Chehab #define FL_VFLIP 0x02		/* vertical flipped by default */
1270c0d06caSMauro Carvalho Chehab 
1280c0d06caSMauro Carvalho Chehab 	u8 sof_read;
1290c0d06caSMauro Carvalho Chehab 	s8 autogain_ignore_frames;
1300c0d06caSMauro Carvalho Chehab 
1310c0d06caSMauro Carvalho Chehab 	atomic_t avg_lum;
1320c0d06caSMauro Carvalho Chehab };
1330c0d06caSMauro Carvalho Chehab 
1340c0d06caSMauro Carvalho Chehab static const struct v4l2_pix_format vga_mode[] = {
1350c0d06caSMauro Carvalho Chehab 	{640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
1360c0d06caSMauro Carvalho Chehab 		.bytesperline = 640,
1370c0d06caSMauro Carvalho Chehab 		.sizeimage = 640 * 480 * 3 / 8 + 590,
1380c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_JPEG,
1390c0d06caSMauro Carvalho Chehab 	},
1400c0d06caSMauro Carvalho Chehab };
1410c0d06caSMauro Carvalho Chehab 
1420c0d06caSMauro Carvalho Chehab #define LOAD_PAGE3		255
1430c0d06caSMauro Carvalho Chehab #define END_OF_SEQUENCE		0
1440c0d06caSMauro Carvalho Chehab 
1450c0d06caSMauro Carvalho Chehab static const u8 init_7302[] = {
1460c0d06caSMauro Carvalho Chehab /*	index,value */
1470c0d06caSMauro Carvalho Chehab 	0xff, 0x01,		/* page 1 */
1480c0d06caSMauro Carvalho Chehab 	0x78, 0x00,		/* deactivate */
1490c0d06caSMauro Carvalho Chehab 	0xff, 0x01,
1500c0d06caSMauro Carvalho Chehab 	0x78, 0x40,		/* led off */
1510c0d06caSMauro Carvalho Chehab };
1520c0d06caSMauro Carvalho Chehab static const u8 start_7302[] = {
1530c0d06caSMauro Carvalho Chehab /*	index, len, [value]* */
1540c0d06caSMauro Carvalho Chehab 	0xff, 1,	0x00,		/* page 0 */
1550c0d06caSMauro Carvalho Chehab 	0x00, 12,	0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
1560c0d06caSMauro Carvalho Chehab 			0x00, 0x00, 0x00, 0x00,
1570c0d06caSMauro Carvalho Chehab 	0x0d, 24,	0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
1580c0d06caSMauro Carvalho Chehab 			0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
1590c0d06caSMauro Carvalho Chehab 			0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
1600c0d06caSMauro Carvalho Chehab 	0x26, 2,	0xaa, 0xaa,
1610c0d06caSMauro Carvalho Chehab 	0x2e, 1,	0x31,
1620c0d06caSMauro Carvalho Chehab 	0x38, 1,	0x01,
1630c0d06caSMauro Carvalho Chehab 	0x3a, 3,	0x14, 0xff, 0x5a,
1640c0d06caSMauro Carvalho Chehab 	0x43, 11,	0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
1650c0d06caSMauro Carvalho Chehab 			0x00, 0x54, 0x11,
1660c0d06caSMauro Carvalho Chehab 	0x55, 1,	0x00,
1670c0d06caSMauro Carvalho Chehab 	0x62, 4,	0x10, 0x1e, 0x1e, 0x18,
1680c0d06caSMauro Carvalho Chehab 	0x6b, 1,	0x00,
1690c0d06caSMauro Carvalho Chehab 	0x6e, 3,	0x08, 0x06, 0x00,
1700c0d06caSMauro Carvalho Chehab 	0x72, 3,	0x00, 0xff, 0x00,
1710c0d06caSMauro Carvalho Chehab 	0x7d, 23,	0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
1720c0d06caSMauro Carvalho Chehab 			0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
1730c0d06caSMauro Carvalho Chehab 			0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
1740c0d06caSMauro Carvalho Chehab 	0xa2, 10,	0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
1750c0d06caSMauro Carvalho Chehab 			0xd2, 0xeb,
1760c0d06caSMauro Carvalho Chehab 	0xaf, 1,	0x02,
1770c0d06caSMauro Carvalho Chehab 	0xb5, 2,	0x08, 0x08,
1780c0d06caSMauro Carvalho Chehab 	0xb8, 2,	0x08, 0x88,
1790c0d06caSMauro Carvalho Chehab 	0xc4, 4,	0xae, 0x01, 0x04, 0x01,
1800c0d06caSMauro Carvalho Chehab 	0xcc, 1,	0x00,
1810c0d06caSMauro Carvalho Chehab 	0xd1, 11,	0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
1820c0d06caSMauro Carvalho Chehab 			0xc1, 0xd7, 0xec,
1830c0d06caSMauro Carvalho Chehab 	0xdc, 1,	0x01,
1840c0d06caSMauro Carvalho Chehab 	0xff, 1,	0x01,		/* page 1 */
1850c0d06caSMauro Carvalho Chehab 	0x12, 3,	0x02, 0x00, 0x01,
1860c0d06caSMauro Carvalho Chehab 	0x3e, 2,	0x00, 0x00,
1870c0d06caSMauro Carvalho Chehab 	0x76, 5,	0x01, 0x20, 0x40, 0x00, 0xf2,
1880c0d06caSMauro Carvalho Chehab 	0x7c, 1,	0x00,
1890c0d06caSMauro Carvalho Chehab 	0x7f, 10,	0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
1900c0d06caSMauro Carvalho Chehab 			0x02, 0x00,
1910c0d06caSMauro Carvalho Chehab 	0x96, 5,	0x01, 0x10, 0x04, 0x01, 0x04,
1920c0d06caSMauro Carvalho Chehab 	0xc8, 14,	0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
1930c0d06caSMauro Carvalho Chehab 			0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
1940c0d06caSMauro Carvalho Chehab 	0xd8, 1,	0x01,
1950c0d06caSMauro Carvalho Chehab 	0xdb, 2,	0x00, 0x01,
1960c0d06caSMauro Carvalho Chehab 	0xde, 7,	0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
1970c0d06caSMauro Carvalho Chehab 	0xe6, 4,	0x00, 0x00, 0x00, 0x01,
1980c0d06caSMauro Carvalho Chehab 	0xeb, 1,	0x00,
1990c0d06caSMauro Carvalho Chehab 	0xff, 1,	0x02,		/* page 2 */
2000c0d06caSMauro Carvalho Chehab 	0x22, 1,	0x00,
2010c0d06caSMauro Carvalho Chehab 	0xff, 1,	0x03,		/* page 3 */
2020c0d06caSMauro Carvalho Chehab 	0, LOAD_PAGE3,			/* load the page 3 */
2030c0d06caSMauro Carvalho Chehab 	0x11, 1,	0x01,
2040c0d06caSMauro Carvalho Chehab 	0xff, 1,	0x02,		/* page 2 */
2050c0d06caSMauro Carvalho Chehab 	0x13, 1,	0x00,
2060c0d06caSMauro Carvalho Chehab 	0x22, 4,	0x1f, 0xa4, 0xf0, 0x96,
2070c0d06caSMauro Carvalho Chehab 	0x27, 2,	0x14, 0x0c,
2080c0d06caSMauro Carvalho Chehab 	0x2a, 5,	0xc8, 0x00, 0x18, 0x12, 0x22,
2090c0d06caSMauro Carvalho Chehab 	0x64, 8,	0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
2100c0d06caSMauro Carvalho Chehab 	0x6e, 1,	0x08,
2110c0d06caSMauro Carvalho Chehab 	0xff, 1,	0x01,		/* page 1 */
2120c0d06caSMauro Carvalho Chehab 	0x78, 1,	0x00,
2130c0d06caSMauro Carvalho Chehab 	0, END_OF_SEQUENCE		/* end of sequence */
2140c0d06caSMauro Carvalho Chehab };
2150c0d06caSMauro Carvalho Chehab 
2160c0d06caSMauro Carvalho Chehab #define SKIP		0xaa
2170c0d06caSMauro Carvalho Chehab /* page 3 - the value SKIP says skip the index - see reg_w_page() */
2180c0d06caSMauro Carvalho Chehab static const u8 page3_7302[] = {
2190c0d06caSMauro Carvalho Chehab 	0x90, 0x40, 0x03, 0x00, 0xc0, 0x01, 0x14, 0x16,
2200c0d06caSMauro Carvalho Chehab 	0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
2210c0d06caSMauro Carvalho Chehab 	0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2220c0d06caSMauro Carvalho Chehab 	0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
2230c0d06caSMauro Carvalho Chehab 	0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
2240c0d06caSMauro Carvalho Chehab 	0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
2250c0d06caSMauro Carvalho Chehab 	0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
2260c0d06caSMauro Carvalho Chehab 	0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2270c0d06caSMauro Carvalho Chehab 	0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
2280c0d06caSMauro Carvalho Chehab 	SKIP, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
2290c0d06caSMauro Carvalho Chehab 	0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2300c0d06caSMauro Carvalho Chehab 	0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
2310c0d06caSMauro Carvalho Chehab 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2320c0d06caSMauro Carvalho Chehab 	0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
2330c0d06caSMauro Carvalho Chehab 	0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
2340c0d06caSMauro Carvalho Chehab 	0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
2350c0d06caSMauro Carvalho Chehab 	0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
2360c0d06caSMauro Carvalho Chehab 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2370c0d06caSMauro Carvalho Chehab 	0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
2380c0d06caSMauro Carvalho Chehab 	0x00
2390c0d06caSMauro Carvalho Chehab };
2400c0d06caSMauro Carvalho Chehab 
2410c0d06caSMauro Carvalho Chehab static void reg_w_buf(struct gspca_dev *gspca_dev,
2420c0d06caSMauro Carvalho Chehab 		u8 index,
2430c0d06caSMauro Carvalho Chehab 		  const u8 *buffer, int len)
2440c0d06caSMauro Carvalho Chehab {
2450c0d06caSMauro Carvalho Chehab 	int ret;
2460c0d06caSMauro Carvalho Chehab 
2470c0d06caSMauro Carvalho Chehab 	if (gspca_dev->usb_err < 0)
2480c0d06caSMauro Carvalho Chehab 		return;
2490c0d06caSMauro Carvalho Chehab 	memcpy(gspca_dev->usb_buf, buffer, len);
2500c0d06caSMauro Carvalho Chehab 	ret = usb_control_msg(gspca_dev->dev,
2510c0d06caSMauro Carvalho Chehab 			usb_sndctrlpipe(gspca_dev->dev, 0),
2520c0d06caSMauro Carvalho Chehab 			0,		/* request */
2530c0d06caSMauro Carvalho Chehab 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2540c0d06caSMauro Carvalho Chehab 			0,		/* value */
2550c0d06caSMauro Carvalho Chehab 			index, gspca_dev->usb_buf, len,
2560c0d06caSMauro Carvalho Chehab 			500);
2570c0d06caSMauro Carvalho Chehab 	if (ret < 0) {
2580c0d06caSMauro Carvalho Chehab 		pr_err("reg_w_buf failed i: %02x error %d\n",
2590c0d06caSMauro Carvalho Chehab 		       index, ret);
2600c0d06caSMauro Carvalho Chehab 		gspca_dev->usb_err = ret;
2610c0d06caSMauro Carvalho Chehab 	}
2620c0d06caSMauro Carvalho Chehab }
2630c0d06caSMauro Carvalho Chehab 
2640c0d06caSMauro Carvalho Chehab 
2650c0d06caSMauro Carvalho Chehab static void reg_w(struct gspca_dev *gspca_dev,
2660c0d06caSMauro Carvalho Chehab 		u8 index,
2670c0d06caSMauro Carvalho Chehab 		u8 value)
2680c0d06caSMauro Carvalho Chehab {
2690c0d06caSMauro Carvalho Chehab 	int ret;
2700c0d06caSMauro Carvalho Chehab 
2710c0d06caSMauro Carvalho Chehab 	if (gspca_dev->usb_err < 0)
2720c0d06caSMauro Carvalho Chehab 		return;
2730c0d06caSMauro Carvalho Chehab 	gspca_dev->usb_buf[0] = value;
2740c0d06caSMauro Carvalho Chehab 	ret = usb_control_msg(gspca_dev->dev,
2750c0d06caSMauro Carvalho Chehab 			usb_sndctrlpipe(gspca_dev->dev, 0),
2760c0d06caSMauro Carvalho Chehab 			0,			/* request */
2770c0d06caSMauro Carvalho Chehab 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2780c0d06caSMauro Carvalho Chehab 			0, index, gspca_dev->usb_buf, 1,
2790c0d06caSMauro Carvalho Chehab 			500);
2800c0d06caSMauro Carvalho Chehab 	if (ret < 0) {
2810c0d06caSMauro Carvalho Chehab 		pr_err("reg_w() failed i: %02x v: %02x error %d\n",
2820c0d06caSMauro Carvalho Chehab 		       index, value, ret);
2830c0d06caSMauro Carvalho Chehab 		gspca_dev->usb_err = ret;
2840c0d06caSMauro Carvalho Chehab 	}
2850c0d06caSMauro Carvalho Chehab }
2860c0d06caSMauro Carvalho Chehab 
2870c0d06caSMauro Carvalho Chehab static void reg_w_seq(struct gspca_dev *gspca_dev,
2880c0d06caSMauro Carvalho Chehab 		const u8 *seq, int len)
2890c0d06caSMauro Carvalho Chehab {
2900c0d06caSMauro Carvalho Chehab 	while (--len >= 0) {
2910c0d06caSMauro Carvalho Chehab 		reg_w(gspca_dev, seq[0], seq[1]);
2920c0d06caSMauro Carvalho Chehab 		seq += 2;
2930c0d06caSMauro Carvalho Chehab 	}
2940c0d06caSMauro Carvalho Chehab }
2950c0d06caSMauro Carvalho Chehab 
2960c0d06caSMauro Carvalho Chehab /* load the beginning of a page */
2970c0d06caSMauro Carvalho Chehab static void reg_w_page(struct gspca_dev *gspca_dev,
2980c0d06caSMauro Carvalho Chehab 			const u8 *page, int len)
2990c0d06caSMauro Carvalho Chehab {
3000c0d06caSMauro Carvalho Chehab 	int index;
3010c0d06caSMauro Carvalho Chehab 	int ret = 0;
3020c0d06caSMauro Carvalho Chehab 
3030c0d06caSMauro Carvalho Chehab 	if (gspca_dev->usb_err < 0)
3040c0d06caSMauro Carvalho Chehab 		return;
3050c0d06caSMauro Carvalho Chehab 	for (index = 0; index < len; index++) {
3060c0d06caSMauro Carvalho Chehab 		if (page[index] == SKIP)		/* skip this index */
3070c0d06caSMauro Carvalho Chehab 			continue;
3080c0d06caSMauro Carvalho Chehab 		gspca_dev->usb_buf[0] = page[index];
3090c0d06caSMauro Carvalho Chehab 		ret = usb_control_msg(gspca_dev->dev,
3100c0d06caSMauro Carvalho Chehab 				usb_sndctrlpipe(gspca_dev->dev, 0),
3110c0d06caSMauro Carvalho Chehab 				0,			/* request */
3120c0d06caSMauro Carvalho Chehab 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3130c0d06caSMauro Carvalho Chehab 				0, index, gspca_dev->usb_buf, 1,
3140c0d06caSMauro Carvalho Chehab 				500);
3150c0d06caSMauro Carvalho Chehab 		if (ret < 0) {
3160c0d06caSMauro Carvalho Chehab 			pr_err("reg_w_page() failed i: %02x v: %02x error %d\n",
3170c0d06caSMauro Carvalho Chehab 			       index, page[index], ret);
3180c0d06caSMauro Carvalho Chehab 			gspca_dev->usb_err = ret;
3190c0d06caSMauro Carvalho Chehab 			break;
3200c0d06caSMauro Carvalho Chehab 		}
3210c0d06caSMauro Carvalho Chehab 	}
3220c0d06caSMauro Carvalho Chehab }
3230c0d06caSMauro Carvalho Chehab 
3240c0d06caSMauro Carvalho Chehab /* output a variable sequence */
3250c0d06caSMauro Carvalho Chehab static void reg_w_var(struct gspca_dev *gspca_dev,
3260c0d06caSMauro Carvalho Chehab 			const u8 *seq,
3270c0d06caSMauro Carvalho Chehab 			const u8 *page3, unsigned int page3_len)
3280c0d06caSMauro Carvalho Chehab {
3290c0d06caSMauro Carvalho Chehab 	int index, len;
3300c0d06caSMauro Carvalho Chehab 
3310c0d06caSMauro Carvalho Chehab 	for (;;) {
3320c0d06caSMauro Carvalho Chehab 		index = *seq++;
3330c0d06caSMauro Carvalho Chehab 		len = *seq++;
3340c0d06caSMauro Carvalho Chehab 		switch (len) {
3350c0d06caSMauro Carvalho Chehab 		case END_OF_SEQUENCE:
3360c0d06caSMauro Carvalho Chehab 			return;
3370c0d06caSMauro Carvalho Chehab 		case LOAD_PAGE3:
3380c0d06caSMauro Carvalho Chehab 			reg_w_page(gspca_dev, page3, page3_len);
3390c0d06caSMauro Carvalho Chehab 			break;
3400c0d06caSMauro Carvalho Chehab 		default:
3410c0d06caSMauro Carvalho Chehab 			if (len > USB_BUF_SZ) {
342*52173c5fSJoe Perches 				gspca_err(gspca_dev, "Incorrect variable sequence\n");
3430c0d06caSMauro Carvalho Chehab 				return;
3440c0d06caSMauro Carvalho Chehab 			}
3450c0d06caSMauro Carvalho Chehab 			while (len > 0) {
3460c0d06caSMauro Carvalho Chehab 				if (len < 8) {
3470c0d06caSMauro Carvalho Chehab 					reg_w_buf(gspca_dev,
3480c0d06caSMauro Carvalho Chehab 						index, seq, len);
3490c0d06caSMauro Carvalho Chehab 					seq += len;
3500c0d06caSMauro Carvalho Chehab 					break;
3510c0d06caSMauro Carvalho Chehab 				}
3520c0d06caSMauro Carvalho Chehab 				reg_w_buf(gspca_dev, index, seq, 8);
3530c0d06caSMauro Carvalho Chehab 				seq += 8;
3540c0d06caSMauro Carvalho Chehab 				index += 8;
3550c0d06caSMauro Carvalho Chehab 				len -= 8;
3560c0d06caSMauro Carvalho Chehab 			}
3570c0d06caSMauro Carvalho Chehab 		}
3580c0d06caSMauro Carvalho Chehab 	}
3590c0d06caSMauro Carvalho Chehab 	/* not reached */
3600c0d06caSMauro Carvalho Chehab }
3610c0d06caSMauro Carvalho Chehab 
3620c0d06caSMauro Carvalho Chehab /* this function is called at probe time for pac7302 */
3630c0d06caSMauro Carvalho Chehab static int sd_config(struct gspca_dev *gspca_dev,
3640c0d06caSMauro Carvalho Chehab 			const struct usb_device_id *id)
3650c0d06caSMauro Carvalho Chehab {
3660c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
3670c0d06caSMauro Carvalho Chehab 	struct cam *cam;
3680c0d06caSMauro Carvalho Chehab 
3690c0d06caSMauro Carvalho Chehab 	cam = &gspca_dev->cam;
3700c0d06caSMauro Carvalho Chehab 
3710c0d06caSMauro Carvalho Chehab 	cam->cam_mode = vga_mode;	/* only 640x480 */
3720c0d06caSMauro Carvalho Chehab 	cam->nmodes = ARRAY_SIZE(vga_mode);
3730c0d06caSMauro Carvalho Chehab 
3740c0d06caSMauro Carvalho Chehab 	sd->flags = id->driver_info;
3750c0d06caSMauro Carvalho Chehab 	return 0;
3760c0d06caSMauro Carvalho Chehab }
3770c0d06caSMauro Carvalho Chehab 
3780c0d06caSMauro Carvalho Chehab static void setbrightcont(struct gspca_dev *gspca_dev)
3790c0d06caSMauro Carvalho Chehab {
3800c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
3810c0d06caSMauro Carvalho Chehab 	int i, v;
3820c0d06caSMauro Carvalho Chehab 	static const u8 max[10] =
3830c0d06caSMauro Carvalho Chehab 		{0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,
3840c0d06caSMauro Carvalho Chehab 		 0xd4, 0xec};
3850c0d06caSMauro Carvalho Chehab 	static const u8 delta[10] =
3860c0d06caSMauro Carvalho Chehab 		{0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,
3870c0d06caSMauro Carvalho Chehab 		 0x11, 0x0b};
3880c0d06caSMauro Carvalho Chehab 
3890c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x00);		/* page 0 */
3900c0d06caSMauro Carvalho Chehab 	for (i = 0; i < 10; i++) {
3910c0d06caSMauro Carvalho Chehab 		v = max[i];
3920d5e8c43SHans Verkuil 		v += (sd->brightness->val - (s32)sd->brightness->maximum)
3930d5e8c43SHans Verkuil 			* 150 / (s32)sd->brightness->maximum; /* 200 ? */
3940d5e8c43SHans Verkuil 		v -= delta[i] * sd->contrast->val / (s32)sd->contrast->maximum;
3950c0d06caSMauro Carvalho Chehab 		if (v < 0)
3960c0d06caSMauro Carvalho Chehab 			v = 0;
3970c0d06caSMauro Carvalho Chehab 		else if (v > 0xff)
3980c0d06caSMauro Carvalho Chehab 			v = 0xff;
3990c0d06caSMauro Carvalho Chehab 		reg_w(gspca_dev, 0xa2 + i, v);
4000c0d06caSMauro Carvalho Chehab 	}
4010c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xdc, 0x01);
4020c0d06caSMauro Carvalho Chehab }
4030c0d06caSMauro Carvalho Chehab 
4040c0d06caSMauro Carvalho Chehab static void setcolors(struct gspca_dev *gspca_dev)
4050c0d06caSMauro Carvalho Chehab {
4060c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4070c0d06caSMauro Carvalho Chehab 	int i, v;
4080c0d06caSMauro Carvalho Chehab 	static const int a[9] =
4090c0d06caSMauro Carvalho Chehab 		{217, -212, 0, -101, 170, -67, -38, -315, 355};
4100c0d06caSMauro Carvalho Chehab 	static const int b[9] =
4110c0d06caSMauro Carvalho Chehab 		{19, 106, 0, 19, 106, 1, 19, 106, 1};
4120c0d06caSMauro Carvalho Chehab 
4130c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x03);			/* page 3 */
4140c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x11, 0x01);
4150c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x00);			/* page 0 */
4160c0d06caSMauro Carvalho Chehab 	for (i = 0; i < 9; i++) {
4170d5e8c43SHans Verkuil 		v = a[i] * sd->saturation->val / (s32)sd->saturation->maximum;
4180c0d06caSMauro Carvalho Chehab 		v += b[i];
4190c0d06caSMauro Carvalho Chehab 		reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);
4200c0d06caSMauro Carvalho Chehab 		reg_w(gspca_dev, 0x0f + 2 * i + 1, v);
4210c0d06caSMauro Carvalho Chehab 	}
4220c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xdc, 0x01);
4230c0d06caSMauro Carvalho Chehab }
4240c0d06caSMauro Carvalho Chehab 
4250c0d06caSMauro Carvalho Chehab static void setwhitebalance(struct gspca_dev *gspca_dev)
4260c0d06caSMauro Carvalho Chehab {
4270c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4280c0d06caSMauro Carvalho Chehab 
4290c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x00);		/* page 0 */
4300c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xc6, sd->white_balance->val);
4310c0d06caSMauro Carvalho Chehab 
4320c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xdc, 0x01);
4330c0d06caSMauro Carvalho Chehab }
4340c0d06caSMauro Carvalho Chehab 
4352b34e9d1SFrank Schäfer static u8 rgbbalance_ctrl_to_reg_value(s32 rgb_ctrl_val)
4362b34e9d1SFrank Schäfer {
4372b34e9d1SFrank Schäfer 	const unsigned int k = 1000;	/* precision factor */
4382b34e9d1SFrank Schäfer 	unsigned int norm;
4392b34e9d1SFrank Schäfer 
4402b34e9d1SFrank Schäfer 	/* Normed value [0...k] */
4412b34e9d1SFrank Schäfer 	norm = k * (rgb_ctrl_val - PAC7302_RGB_BALANCE_MIN)
4422b34e9d1SFrank Schäfer 		    / (PAC7302_RGB_BALANCE_MAX - PAC7302_RGB_BALANCE_MIN);
4432b34e9d1SFrank Schäfer 	/* Qudratic apporach improves control at small (register) values: */
4442b34e9d1SFrank Schäfer 	return 64 * norm * norm / (k*k)  +  32 * norm / k  +  32;
4452b34e9d1SFrank Schäfer 	/* Y = 64*X*X + 32*X + 32
4462b34e9d1SFrank Schäfer 	 * => register values 0x20-0x80; Windows driver uses these limits */
4472b34e9d1SFrank Schäfer 
4482b34e9d1SFrank Schäfer 	/* NOTE: for full value range (0x00-0xff) use
4492b34e9d1SFrank Schäfer 	 *         Y = 254*X*X + X
4502b34e9d1SFrank Schäfer 	 *         => 254 * norm * norm / (k*k)  +  1 * norm / k	*/
4512b34e9d1SFrank Schäfer }
4522b34e9d1SFrank Schäfer 
4530c0d06caSMauro Carvalho Chehab static void setredbalance(struct gspca_dev *gspca_dev)
4540c0d06caSMauro Carvalho Chehab {
4550c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4560c0d06caSMauro Carvalho Chehab 
4570c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x00);			/* page 0 */
4582b34e9d1SFrank Schäfer 	reg_w(gspca_dev, 0x01,
4592b34e9d1SFrank Schäfer 	      rgbbalance_ctrl_to_reg_value(sd->red_balance->val));
4600c0d06caSMauro Carvalho Chehab 
4610c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xdc, 0x01);
4620c0d06caSMauro Carvalho Chehab }
4630c0d06caSMauro Carvalho Chehab 
4640c0d06caSMauro Carvalho Chehab static void setbluebalance(struct gspca_dev *gspca_dev)
4650c0d06caSMauro Carvalho Chehab {
4660c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4670c0d06caSMauro Carvalho Chehab 
4680c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x00);			/* page 0 */
4692b34e9d1SFrank Schäfer 	reg_w(gspca_dev, 0x03,
4702b34e9d1SFrank Schäfer 	      rgbbalance_ctrl_to_reg_value(sd->blue_balance->val));
4710c0d06caSMauro Carvalho Chehab 
4720c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xdc, 0x01);
4730c0d06caSMauro Carvalho Chehab }
4740c0d06caSMauro Carvalho Chehab 
4750c0d06caSMauro Carvalho Chehab static void setgain(struct gspca_dev *gspca_dev)
4760c0d06caSMauro Carvalho Chehab {
4770c0d06caSMauro Carvalho Chehab 	u8 reg10, reg12;
4780c0d06caSMauro Carvalho Chehab 
4790c0d06caSMauro Carvalho Chehab 	if (gspca_dev->gain->val < 32) {
4800c0d06caSMauro Carvalho Chehab 		reg10 = gspca_dev->gain->val;
4810c0d06caSMauro Carvalho Chehab 		reg12 = 0;
4820c0d06caSMauro Carvalho Chehab 	} else {
4830c0d06caSMauro Carvalho Chehab 		reg10 = 31;
4840c0d06caSMauro Carvalho Chehab 		reg12 = gspca_dev->gain->val - 31;
4850c0d06caSMauro Carvalho Chehab 	}
4860c0d06caSMauro Carvalho Chehab 
4870c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x03);			/* page 3 */
4880c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x10, reg10);
4890c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x12, reg12);
4900c0d06caSMauro Carvalho Chehab 
4910c0d06caSMauro Carvalho Chehab 	/* load registers to sensor (Bit 0, auto clear) */
4920c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x11, 0x01);
4930c0d06caSMauro Carvalho Chehab }
4940c0d06caSMauro Carvalho Chehab 
4950c0d06caSMauro Carvalho Chehab static void setexposure(struct gspca_dev *gspca_dev)
4960c0d06caSMauro Carvalho Chehab {
4970c0d06caSMauro Carvalho Chehab 	u8 clockdiv;
4980c0d06caSMauro Carvalho Chehab 	u16 exposure;
4990c0d06caSMauro Carvalho Chehab 
5000c0d06caSMauro Carvalho Chehab 	/*
5010c0d06caSMauro Carvalho Chehab 	 * Register 2 of frame 3 contains the clock divider configuring the
5020c0d06caSMauro Carvalho Chehab 	 * no fps according to the formula: 90 / reg. sd->exposure is the
5030c0d06caSMauro Carvalho Chehab 	 * desired exposure time in 0.5 ms.
5040c0d06caSMauro Carvalho Chehab 	 */
5050c0d06caSMauro Carvalho Chehab 	clockdiv = (90 * gspca_dev->exposure->val + 1999) / 2000;
5060c0d06caSMauro Carvalho Chehab 
5070c0d06caSMauro Carvalho Chehab 	/*
5080c0d06caSMauro Carvalho Chehab 	 * Note clockdiv = 3 also works, but when running at 30 fps, depending
5090c0d06caSMauro Carvalho Chehab 	 * on the scene being recorded, the camera switches to another
5100c0d06caSMauro Carvalho Chehab 	 * quantization table for certain JPEG blocks, and we don't know how
5110c0d06caSMauro Carvalho Chehab 	 * to decompress these blocks. So we cap the framerate at 15 fps.
5120c0d06caSMauro Carvalho Chehab 	 */
5130c0d06caSMauro Carvalho Chehab 	if (clockdiv < 6)
5140c0d06caSMauro Carvalho Chehab 		clockdiv = 6;
5150c0d06caSMauro Carvalho Chehab 	else if (clockdiv > 63)
5160c0d06caSMauro Carvalho Chehab 		clockdiv = 63;
5170c0d06caSMauro Carvalho Chehab 
5180c0d06caSMauro Carvalho Chehab 	/*
5190c0d06caSMauro Carvalho Chehab 	 * Register 2 MUST be a multiple of 3, except when between 6 and 12?
5200c0d06caSMauro Carvalho Chehab 	 * Always round up, otherwise we cannot get the desired frametime
5210c0d06caSMauro Carvalho Chehab 	 * using the partial frame time exposure control.
5220c0d06caSMauro Carvalho Chehab 	 */
5230c0d06caSMauro Carvalho Chehab 	if (clockdiv < 6 || clockdiv > 12)
5240c0d06caSMauro Carvalho Chehab 		clockdiv = ((clockdiv + 2) / 3) * 3;
5250c0d06caSMauro Carvalho Chehab 
5260c0d06caSMauro Carvalho Chehab 	/*
5270c0d06caSMauro Carvalho Chehab 	 * frame exposure time in ms = 1000 * clockdiv / 90    ->
5280c0d06caSMauro Carvalho Chehab 	 * exposure = (sd->exposure / 2) * 448 / (1000 * clockdiv / 90)
5290c0d06caSMauro Carvalho Chehab 	 */
5300c0d06caSMauro Carvalho Chehab 	exposure = (gspca_dev->exposure->val * 45 * 448) / (1000 * clockdiv);
5310c0d06caSMauro Carvalho Chehab 	/* 0 = use full frametime, 448 = no exposure, reverse it */
5320c0d06caSMauro Carvalho Chehab 	exposure = 448 - exposure;
5330c0d06caSMauro Carvalho Chehab 
5340c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x03);			/* page 3 */
5350c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x02, clockdiv);
5360c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x0e, exposure & 0xff);
5370c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x0f, exposure >> 8);
5380c0d06caSMauro Carvalho Chehab 
5390c0d06caSMauro Carvalho Chehab 	/* load registers to sensor (Bit 0, auto clear) */
5400c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x11, 0x01);
5410c0d06caSMauro Carvalho Chehab }
5420c0d06caSMauro Carvalho Chehab 
5430c0d06caSMauro Carvalho Chehab static void sethvflip(struct gspca_dev *gspca_dev)
5440c0d06caSMauro Carvalho Chehab {
5450c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
5460c0d06caSMauro Carvalho Chehab 	u8 data, hflip, vflip;
5470c0d06caSMauro Carvalho Chehab 
5480c0d06caSMauro Carvalho Chehab 	hflip = sd->hflip->val;
5490c0d06caSMauro Carvalho Chehab 	if (sd->flags & FL_HFLIP)
5500c0d06caSMauro Carvalho Chehab 		hflip = !hflip;
5510c0d06caSMauro Carvalho Chehab 	vflip = sd->vflip->val;
5520c0d06caSMauro Carvalho Chehab 	if (sd->flags & FL_VFLIP)
5530c0d06caSMauro Carvalho Chehab 		vflip = !vflip;
5540c0d06caSMauro Carvalho Chehab 
5550c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x03);			/* page 3 */
5560c0d06caSMauro Carvalho Chehab 	data = (hflip ? 0x08 : 0x00) | (vflip ? 0x04 : 0x00);
5570c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x21, data);
5580c0d06caSMauro Carvalho Chehab 
5590c0d06caSMauro Carvalho Chehab 	/* load registers to sensor (Bit 0, auto clear) */
5600c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x11, 0x01);
5610c0d06caSMauro Carvalho Chehab }
5620c0d06caSMauro Carvalho Chehab 
563b1a19c01SFrank Schäfer static void setsharpness(struct gspca_dev *gspca_dev)
564b1a19c01SFrank Schäfer {
565b1a19c01SFrank Schäfer 	struct sd *sd = (struct sd *) gspca_dev;
566b1a19c01SFrank Schäfer 
567b1a19c01SFrank Schäfer 	reg_w(gspca_dev, 0xff, 0x00);		/* page 0 */
568b1a19c01SFrank Schäfer 	reg_w(gspca_dev, 0xb6, sd->sharpness->val);
569b1a19c01SFrank Schäfer 
570b1a19c01SFrank Schäfer 	reg_w(gspca_dev, 0xdc, 0x01);
571b1a19c01SFrank Schäfer }
572b1a19c01SFrank Schäfer 
5730c0d06caSMauro Carvalho Chehab /* this function is called at probe and resume time for pac7302 */
5740c0d06caSMauro Carvalho Chehab static int sd_init(struct gspca_dev *gspca_dev)
5750c0d06caSMauro Carvalho Chehab {
5760c0d06caSMauro Carvalho Chehab 	reg_w_seq(gspca_dev, init_7302, sizeof(init_7302)/2);
5770c0d06caSMauro Carvalho Chehab 	return gspca_dev->usb_err;
5780c0d06caSMauro Carvalho Chehab }
5790c0d06caSMauro Carvalho Chehab 
5800c0d06caSMauro Carvalho Chehab static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
5810c0d06caSMauro Carvalho Chehab {
5820c0d06caSMauro Carvalho Chehab 	struct gspca_dev *gspca_dev =
5830c0d06caSMauro Carvalho Chehab 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
5840c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *)gspca_dev;
5850c0d06caSMauro Carvalho Chehab 
5860c0d06caSMauro Carvalho Chehab 	gspca_dev->usb_err = 0;
5870c0d06caSMauro Carvalho Chehab 
5880c0d06caSMauro Carvalho Chehab 	if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
5890c0d06caSMauro Carvalho Chehab 		/* when switching to autogain set defaults to make sure
5900c0d06caSMauro Carvalho Chehab 		   we are on a valid point of the autogain gain /
5910c0d06caSMauro Carvalho Chehab 		   exposure knee graph, and give this change time to
5920c0d06caSMauro Carvalho Chehab 		   take effect before doing autogain. */
5930c0d06caSMauro Carvalho Chehab 		gspca_dev->exposure->val    = PAC7302_EXPOSURE_DEFAULT;
5940c0d06caSMauro Carvalho Chehab 		gspca_dev->gain->val        = PAC7302_GAIN_DEFAULT;
5950c0d06caSMauro Carvalho Chehab 		sd->autogain_ignore_frames  = PAC_AUTOGAIN_IGNORE_FRAMES;
5960c0d06caSMauro Carvalho Chehab 	}
5970c0d06caSMauro Carvalho Chehab 
5980c0d06caSMauro Carvalho Chehab 	if (!gspca_dev->streaming)
5990c0d06caSMauro Carvalho Chehab 		return 0;
6000c0d06caSMauro Carvalho Chehab 
6010c0d06caSMauro Carvalho Chehab 	switch (ctrl->id) {
6020c0d06caSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
6030c0d06caSMauro Carvalho Chehab 		setbrightcont(gspca_dev);
6040c0d06caSMauro Carvalho Chehab 		break;
6050c0d06caSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
6060c0d06caSMauro Carvalho Chehab 		setcolors(gspca_dev);
6070c0d06caSMauro Carvalho Chehab 		break;
6080c0d06caSMauro Carvalho Chehab 	case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
6090c0d06caSMauro Carvalho Chehab 		setwhitebalance(gspca_dev);
6100c0d06caSMauro Carvalho Chehab 		break;
6110c0d06caSMauro Carvalho Chehab 	case V4L2_CID_RED_BALANCE:
6120c0d06caSMauro Carvalho Chehab 		setredbalance(gspca_dev);
6130c0d06caSMauro Carvalho Chehab 		break;
6140c0d06caSMauro Carvalho Chehab 	case V4L2_CID_BLUE_BALANCE:
6150c0d06caSMauro Carvalho Chehab 		setbluebalance(gspca_dev);
6160c0d06caSMauro Carvalho Chehab 		break;
6170c0d06caSMauro Carvalho Chehab 	case V4L2_CID_AUTOGAIN:
6180c0d06caSMauro Carvalho Chehab 		if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
6190c0d06caSMauro Carvalho Chehab 			setexposure(gspca_dev);
6200c0d06caSMauro Carvalho Chehab 		if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
6210c0d06caSMauro Carvalho Chehab 			setgain(gspca_dev);
6220c0d06caSMauro Carvalho Chehab 		break;
6230c0d06caSMauro Carvalho Chehab 	case V4L2_CID_HFLIP:
6240c0d06caSMauro Carvalho Chehab 		sethvflip(gspca_dev);
6250c0d06caSMauro Carvalho Chehab 		break;
626b1a19c01SFrank Schäfer 	case V4L2_CID_SHARPNESS:
627b1a19c01SFrank Schäfer 		setsharpness(gspca_dev);
628b1a19c01SFrank Schäfer 		break;
6290c0d06caSMauro Carvalho Chehab 	default:
6300c0d06caSMauro Carvalho Chehab 		return -EINVAL;
6310c0d06caSMauro Carvalho Chehab 	}
6320c0d06caSMauro Carvalho Chehab 	return gspca_dev->usb_err;
6330c0d06caSMauro Carvalho Chehab }
6340c0d06caSMauro Carvalho Chehab 
6350c0d06caSMauro Carvalho Chehab static const struct v4l2_ctrl_ops sd_ctrl_ops = {
6360c0d06caSMauro Carvalho Chehab 	.s_ctrl = sd_s_ctrl,
6370c0d06caSMauro Carvalho Chehab };
6380c0d06caSMauro Carvalho Chehab 
6390c0d06caSMauro Carvalho Chehab /* this function is called at probe time */
6400c0d06caSMauro Carvalho Chehab static int sd_init_controls(struct gspca_dev *gspca_dev)
6410c0d06caSMauro Carvalho Chehab {
6420c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
6430c0d06caSMauro Carvalho Chehab 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
6440c0d06caSMauro Carvalho Chehab 
6450c0d06caSMauro Carvalho Chehab 	gspca_dev->vdev.ctrl_handler = hdl;
646b1a19c01SFrank Schäfer 	v4l2_ctrl_handler_init(hdl, 12);
6470c0d06caSMauro Carvalho Chehab 
6480c0d06caSMauro Carvalho Chehab 	sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6490c0d06caSMauro Carvalho Chehab 					V4L2_CID_BRIGHTNESS, 0, 32, 1, 16);
6500c0d06caSMauro Carvalho Chehab 	sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6510c0d06caSMauro Carvalho Chehab 					V4L2_CID_CONTRAST, 0, 255, 1, 127);
6520c0d06caSMauro Carvalho Chehab 
6530c0d06caSMauro Carvalho Chehab 	sd->saturation = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6540c0d06caSMauro Carvalho Chehab 					V4L2_CID_SATURATION, 0, 255, 1, 127);
6550c0d06caSMauro Carvalho Chehab 	sd->white_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6560c0d06caSMauro Carvalho Chehab 					V4L2_CID_WHITE_BALANCE_TEMPERATURE,
657f58e5cdfSFrank Schäfer 					0, 255, 1, 55);
6580c0d06caSMauro Carvalho Chehab 	sd->red_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6592b34e9d1SFrank Schäfer 					V4L2_CID_RED_BALANCE,
6602b34e9d1SFrank Schäfer 					PAC7302_RGB_BALANCE_MIN,
6612b34e9d1SFrank Schäfer 					PAC7302_RGB_BALANCE_MAX,
6622b34e9d1SFrank Schäfer 					1, PAC7302_RGB_BALANCE_DEFAULT);
6630c0d06caSMauro Carvalho Chehab 	sd->blue_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6642b34e9d1SFrank Schäfer 					V4L2_CID_BLUE_BALANCE,
6652b34e9d1SFrank Schäfer 					PAC7302_RGB_BALANCE_MIN,
6662b34e9d1SFrank Schäfer 					PAC7302_RGB_BALANCE_MAX,
6672b34e9d1SFrank Schäfer 					1, PAC7302_RGB_BALANCE_DEFAULT);
6680c0d06caSMauro Carvalho Chehab 
6690c0d06caSMauro Carvalho Chehab 	gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6700c0d06caSMauro Carvalho Chehab 					V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
6710c0d06caSMauro Carvalho Chehab 	gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6720c0d06caSMauro Carvalho Chehab 					V4L2_CID_EXPOSURE, 0, 1023, 1,
6730c0d06caSMauro Carvalho Chehab 					PAC7302_EXPOSURE_DEFAULT);
6740c0d06caSMauro Carvalho Chehab 	gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6750c0d06caSMauro Carvalho Chehab 					V4L2_CID_GAIN, 0, 62, 1,
6760c0d06caSMauro Carvalho Chehab 					PAC7302_GAIN_DEFAULT);
6770c0d06caSMauro Carvalho Chehab 
6780c0d06caSMauro Carvalho Chehab 	sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6790c0d06caSMauro Carvalho Chehab 		V4L2_CID_HFLIP, 0, 1, 1, 0);
6800c0d06caSMauro Carvalho Chehab 	sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
6810c0d06caSMauro Carvalho Chehab 		V4L2_CID_VFLIP, 0, 1, 1, 0);
6820c0d06caSMauro Carvalho Chehab 
683b1a19c01SFrank Schäfer 	sd->sharpness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
684b1a19c01SFrank Schäfer 					V4L2_CID_SHARPNESS, 0, 15, 1, 8);
685b1a19c01SFrank Schäfer 
6860c0d06caSMauro Carvalho Chehab 	if (hdl->error) {
6870c0d06caSMauro Carvalho Chehab 		pr_err("Could not initialize controls\n");
6880c0d06caSMauro Carvalho Chehab 		return hdl->error;
6890c0d06caSMauro Carvalho Chehab 	}
6900c0d06caSMauro Carvalho Chehab 
6910c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_cluster(2, &sd->brightness);
6920c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
6930c0d06caSMauro Carvalho Chehab 	v4l2_ctrl_cluster(2, &sd->hflip);
6940c0d06caSMauro Carvalho Chehab 	return 0;
6950c0d06caSMauro Carvalho Chehab }
6960c0d06caSMauro Carvalho Chehab 
6970c0d06caSMauro Carvalho Chehab /* -- start the camera -- */
6980c0d06caSMauro Carvalho Chehab static int sd_start(struct gspca_dev *gspca_dev)
6990c0d06caSMauro Carvalho Chehab {
7000c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
7010c0d06caSMauro Carvalho Chehab 
7020c0d06caSMauro Carvalho Chehab 	reg_w_var(gspca_dev, start_7302,
7030c0d06caSMauro Carvalho Chehab 		page3_7302, sizeof(page3_7302));
7040c0d06caSMauro Carvalho Chehab 
7050c0d06caSMauro Carvalho Chehab 	sd->sof_read = 0;
7060c0d06caSMauro Carvalho Chehab 	sd->autogain_ignore_frames = 0;
7070c0d06caSMauro Carvalho Chehab 	atomic_set(&sd->avg_lum, 270 + sd->brightness->val);
7080c0d06caSMauro Carvalho Chehab 
7090c0d06caSMauro Carvalho Chehab 	/* start stream */
7100c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x01);
7110c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x78, 0x01);
7120c0d06caSMauro Carvalho Chehab 
7130c0d06caSMauro Carvalho Chehab 	return gspca_dev->usb_err;
7140c0d06caSMauro Carvalho Chehab }
7150c0d06caSMauro Carvalho Chehab 
7160c0d06caSMauro Carvalho Chehab static void sd_stopN(struct gspca_dev *gspca_dev)
7170c0d06caSMauro Carvalho Chehab {
7180c0d06caSMauro Carvalho Chehab 
7190c0d06caSMauro Carvalho Chehab 	/* stop stream */
7200c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x01);
7210c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x78, 0x00);
7220c0d06caSMauro Carvalho Chehab }
7230c0d06caSMauro Carvalho Chehab 
7240c0d06caSMauro Carvalho Chehab /* called on streamoff with alt 0 and on disconnect for pac7302 */
7250c0d06caSMauro Carvalho Chehab static void sd_stop0(struct gspca_dev *gspca_dev)
7260c0d06caSMauro Carvalho Chehab {
7270c0d06caSMauro Carvalho Chehab 	if (!gspca_dev->present)
7280c0d06caSMauro Carvalho Chehab 		return;
7290c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0xff, 0x01);
7300c0d06caSMauro Carvalho Chehab 	reg_w(gspca_dev, 0x78, 0x40);
7310c0d06caSMauro Carvalho Chehab }
7320c0d06caSMauro Carvalho Chehab 
7330c0d06caSMauro Carvalho Chehab static void do_autogain(struct gspca_dev *gspca_dev)
7340c0d06caSMauro Carvalho Chehab {
7350c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
7360c0d06caSMauro Carvalho Chehab 	int avg_lum = atomic_read(&sd->avg_lum);
7370c0d06caSMauro Carvalho Chehab 	int desired_lum;
7380c0d06caSMauro Carvalho Chehab 	const int deadzone = 30;
7390c0d06caSMauro Carvalho Chehab 
7400c0d06caSMauro Carvalho Chehab 	if (sd->autogain_ignore_frames < 0)
7410c0d06caSMauro Carvalho Chehab 		return;
7420c0d06caSMauro Carvalho Chehab 
7430c0d06caSMauro Carvalho Chehab 	if (sd->autogain_ignore_frames > 0) {
7440c0d06caSMauro Carvalho Chehab 		sd->autogain_ignore_frames--;
7450c0d06caSMauro Carvalho Chehab 	} else {
7460c0d06caSMauro Carvalho Chehab 		desired_lum = 270 + sd->brightness->val;
7470c0d06caSMauro Carvalho Chehab 
7480c0d06caSMauro Carvalho Chehab 		if (gspca_expo_autogain(gspca_dev, avg_lum, desired_lum,
7490c0d06caSMauro Carvalho Chehab 					deadzone, PAC7302_GAIN_KNEE,
7500c0d06caSMauro Carvalho Chehab 					PAC7302_EXPOSURE_KNEE))
7510c0d06caSMauro Carvalho Chehab 			sd->autogain_ignore_frames =
7520c0d06caSMauro Carvalho Chehab 						PAC_AUTOGAIN_IGNORE_FRAMES;
7530c0d06caSMauro Carvalho Chehab 	}
7540c0d06caSMauro Carvalho Chehab }
7550c0d06caSMauro Carvalho Chehab 
7560c0d06caSMauro Carvalho Chehab /* JPEG header */
7570c0d06caSMauro Carvalho Chehab static const u8 jpeg_header[] = {
7580c0d06caSMauro Carvalho Chehab 	0xff, 0xd8,	/* SOI: Start of Image */
7590c0d06caSMauro Carvalho Chehab 
7600c0d06caSMauro Carvalho Chehab 	0xff, 0xc0,	/* SOF0: Start of Frame (Baseline DCT) */
7610c0d06caSMauro Carvalho Chehab 	0x00, 0x11,	/* length = 17 bytes (including this length field) */
7620c0d06caSMauro Carvalho Chehab 	0x08,		/* Precision: 8 */
7630c0d06caSMauro Carvalho Chehab 	0x02, 0x80,	/* height = 640 (image rotated) */
7640c0d06caSMauro Carvalho Chehab 	0x01, 0xe0,	/* width = 480 */
7650c0d06caSMauro Carvalho Chehab 	0x03,		/* Number of image components: 3 */
7660c0d06caSMauro Carvalho Chehab 	0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
7670c0d06caSMauro Carvalho Chehab 	0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
7680c0d06caSMauro Carvalho Chehab 	0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
7690c0d06caSMauro Carvalho Chehab 
7700c0d06caSMauro Carvalho Chehab 	0xff, 0xda,	/* SOS: Start Of Scan */
7710c0d06caSMauro Carvalho Chehab 	0x00, 0x0c,	/* length = 12 bytes (including this length field) */
7720c0d06caSMauro Carvalho Chehab 	0x03,		/* number of components: 3 */
7730c0d06caSMauro Carvalho Chehab 	0x01, 0x00,	/* selector 1, table 0x00 */
7740c0d06caSMauro Carvalho Chehab 	0x02, 0x11,	/* selector 2, table 0x11 */
7750c0d06caSMauro Carvalho Chehab 	0x03, 0x11,	/* selector 3, table 0x11 */
7760c0d06caSMauro Carvalho Chehab 	0x00, 0x3f,	/* Spectral selection: 0 .. 63 */
7770c0d06caSMauro Carvalho Chehab 	0x00		/* Successive approximation: 0 */
7780c0d06caSMauro Carvalho Chehab };
7790c0d06caSMauro Carvalho Chehab 
7800c0d06caSMauro Carvalho Chehab /* this function is run at interrupt level */
7810c0d06caSMauro Carvalho Chehab static void sd_pkt_scan(struct gspca_dev *gspca_dev,
7820c0d06caSMauro Carvalho Chehab 			u8 *data,			/* isoc packet */
7830c0d06caSMauro Carvalho Chehab 			int len)			/* iso packet length */
7840c0d06caSMauro Carvalho Chehab {
7850c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
7860c0d06caSMauro Carvalho Chehab 	u8 *image;
7870c0d06caSMauro Carvalho Chehab 	u8 *sof;
7880c0d06caSMauro Carvalho Chehab 
789c93396e1STheodore Kilgore 	sof = pac_find_sof(gspca_dev, &sd->sof_read, data, len);
7900c0d06caSMauro Carvalho Chehab 	if (sof) {
7910c0d06caSMauro Carvalho Chehab 		int n, lum_offset, footer_length;
7920c0d06caSMauro Carvalho Chehab 
7930c0d06caSMauro Carvalho Chehab 		/*
7940c0d06caSMauro Carvalho Chehab 		 * 6 bytes after the FF D9 EOF marker a number of lumination
7950c0d06caSMauro Carvalho Chehab 		 * bytes are send corresponding to different parts of the
7960c0d06caSMauro Carvalho Chehab 		 * image, the 14th and 15th byte after the EOF seem to
7970c0d06caSMauro Carvalho Chehab 		 * correspond to the center of the image.
7980c0d06caSMauro Carvalho Chehab 		 */
7990c0d06caSMauro Carvalho Chehab 		lum_offset = 61 + sizeof pac_sof_marker;
8000c0d06caSMauro Carvalho Chehab 		footer_length = 74;
8010c0d06caSMauro Carvalho Chehab 
8020c0d06caSMauro Carvalho Chehab 		/* Finish decoding current frame */
8030c0d06caSMauro Carvalho Chehab 		n = (sof - data) - (footer_length + sizeof pac_sof_marker);
8040c0d06caSMauro Carvalho Chehab 		if (n < 0) {
8050c0d06caSMauro Carvalho Chehab 			gspca_dev->image_len += n;
8060c0d06caSMauro Carvalho Chehab 			n = 0;
8070c0d06caSMauro Carvalho Chehab 		} else {
8080c0d06caSMauro Carvalho Chehab 			gspca_frame_add(gspca_dev, INTER_PACKET, data, n);
8090c0d06caSMauro Carvalho Chehab 		}
8100c0d06caSMauro Carvalho Chehab 
8110c0d06caSMauro Carvalho Chehab 		image = gspca_dev->image;
8120c0d06caSMauro Carvalho Chehab 		if (image != NULL
8130c0d06caSMauro Carvalho Chehab 		 && image[gspca_dev->image_len - 2] == 0xff
8140c0d06caSMauro Carvalho Chehab 		 && image[gspca_dev->image_len - 1] == 0xd9)
8150c0d06caSMauro Carvalho Chehab 			gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
8160c0d06caSMauro Carvalho Chehab 
8170c0d06caSMauro Carvalho Chehab 		n = sof - data;
8180c0d06caSMauro Carvalho Chehab 		len -= n;
8190c0d06caSMauro Carvalho Chehab 		data = sof;
8200c0d06caSMauro Carvalho Chehab 
8210c0d06caSMauro Carvalho Chehab 		/* Get average lumination */
8220c0d06caSMauro Carvalho Chehab 		if (gspca_dev->last_packet_type == LAST_PACKET &&
8230c0d06caSMauro Carvalho Chehab 				n >= lum_offset)
8240c0d06caSMauro Carvalho Chehab 			atomic_set(&sd->avg_lum, data[-lum_offset] +
8250c0d06caSMauro Carvalho Chehab 						data[-lum_offset + 1]);
8260c0d06caSMauro Carvalho Chehab 
8270c0d06caSMauro Carvalho Chehab 		/* Start the new frame with the jpeg header */
8280c0d06caSMauro Carvalho Chehab 		/* The PAC7302 has the image rotated 90 degrees */
8290c0d06caSMauro Carvalho Chehab 		gspca_frame_add(gspca_dev, FIRST_PACKET,
8300c0d06caSMauro Carvalho Chehab 				jpeg_header, sizeof jpeg_header);
8310c0d06caSMauro Carvalho Chehab 	}
8320c0d06caSMauro Carvalho Chehab 	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
8330c0d06caSMauro Carvalho Chehab }
8340c0d06caSMauro Carvalho Chehab 
8350c0d06caSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
8360c0d06caSMauro Carvalho Chehab static int sd_dbg_s_register(struct gspca_dev *gspca_dev,
837977ba3b1SHans Verkuil 			const struct v4l2_dbg_register *reg)
8380c0d06caSMauro Carvalho Chehab {
8390c0d06caSMauro Carvalho Chehab 	u8 index;
8400c0d06caSMauro Carvalho Chehab 	u8 value;
8410c0d06caSMauro Carvalho Chehab 
8420c0d06caSMauro Carvalho Chehab 	/*
8430c0d06caSMauro Carvalho Chehab 	 * reg->reg: bit0..15: reserved for register index (wIndex is 16bit
8440c0d06caSMauro Carvalho Chehab 	 *		       long on the USB bus)
8450c0d06caSMauro Carvalho Chehab 	 */
846b1c85cc0SHans Verkuil 	if (reg->match.addr == 0 &&
8470c0d06caSMauro Carvalho Chehab 	    (reg->reg < 0x000000ff) &&
8480c0d06caSMauro Carvalho Chehab 	    (reg->val <= 0x000000ff)
8490c0d06caSMauro Carvalho Chehab 	) {
8500c0d06caSMauro Carvalho Chehab 		/* Currently writing to page 0 is only supported. */
8510c0d06caSMauro Carvalho Chehab 		/* reg_w() only supports 8bit index */
8520c0d06caSMauro Carvalho Chehab 		index = reg->reg;
8530c0d06caSMauro Carvalho Chehab 		value = reg->val;
8540c0d06caSMauro Carvalho Chehab 
8550c0d06caSMauro Carvalho Chehab 		/*
8560c0d06caSMauro Carvalho Chehab 		 * Note that there shall be no access to other page
8570c0d06caSMauro Carvalho Chehab 		 * by any other function between the page switch and
8580c0d06caSMauro Carvalho Chehab 		 * the actual register write.
8590c0d06caSMauro Carvalho Chehab 		 */
8600c0d06caSMauro Carvalho Chehab 		reg_w(gspca_dev, 0xff, 0x00);		/* page 0 */
8610c0d06caSMauro Carvalho Chehab 		reg_w(gspca_dev, index, value);
8620c0d06caSMauro Carvalho Chehab 
8630c0d06caSMauro Carvalho Chehab 		reg_w(gspca_dev, 0xdc, 0x01);
8640c0d06caSMauro Carvalho Chehab 	}
8650c0d06caSMauro Carvalho Chehab 	return gspca_dev->usb_err;
8660c0d06caSMauro Carvalho Chehab }
8670c0d06caSMauro Carvalho Chehab #endif
8680c0d06caSMauro Carvalho Chehab 
869ae814c08SPeter Senna Tschudin #if IS_ENABLED(CONFIG_INPUT)
8700c0d06caSMauro Carvalho Chehab static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
8710c0d06caSMauro Carvalho Chehab 			u8 *data,		/* interrupt packet data */
87239c1cb2bSJonathan McCrohan 			int len)		/* interrupt packet length */
8730c0d06caSMauro Carvalho Chehab {
8740c0d06caSMauro Carvalho Chehab 	int ret = -EINVAL;
8750c0d06caSMauro Carvalho Chehab 	u8 data0, data1;
8760c0d06caSMauro Carvalho Chehab 
8770c0d06caSMauro Carvalho Chehab 	if (len == 2) {
8780c0d06caSMauro Carvalho Chehab 		data0 = data[0];
8790c0d06caSMauro Carvalho Chehab 		data1 = data[1];
8800c0d06caSMauro Carvalho Chehab 		if ((data0 == 0x00 && data1 == 0x11) ||
8810c0d06caSMauro Carvalho Chehab 		    (data0 == 0x22 && data1 == 0x33) ||
8820c0d06caSMauro Carvalho Chehab 		    (data0 == 0x44 && data1 == 0x55) ||
8830c0d06caSMauro Carvalho Chehab 		    (data0 == 0x66 && data1 == 0x77) ||
8840c0d06caSMauro Carvalho Chehab 		    (data0 == 0x88 && data1 == 0x99) ||
8850c0d06caSMauro Carvalho Chehab 		    (data0 == 0xaa && data1 == 0xbb) ||
8860c0d06caSMauro Carvalho Chehab 		    (data0 == 0xcc && data1 == 0xdd) ||
8870c0d06caSMauro Carvalho Chehab 		    (data0 == 0xee && data1 == 0xff)) {
8880c0d06caSMauro Carvalho Chehab 			input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
8890c0d06caSMauro Carvalho Chehab 			input_sync(gspca_dev->input_dev);
8900c0d06caSMauro Carvalho Chehab 			input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
8910c0d06caSMauro Carvalho Chehab 			input_sync(gspca_dev->input_dev);
8920c0d06caSMauro Carvalho Chehab 			ret = 0;
8930c0d06caSMauro Carvalho Chehab 		}
8940c0d06caSMauro Carvalho Chehab 	}
8950c0d06caSMauro Carvalho Chehab 
8960c0d06caSMauro Carvalho Chehab 	return ret;
8970c0d06caSMauro Carvalho Chehab }
8980c0d06caSMauro Carvalho Chehab #endif
8990c0d06caSMauro Carvalho Chehab 
9000c0d06caSMauro Carvalho Chehab /* sub-driver description for pac7302 */
9010c0d06caSMauro Carvalho Chehab static const struct sd_desc sd_desc = {
9020c0d06caSMauro Carvalho Chehab 	.name = KBUILD_MODNAME,
9030c0d06caSMauro Carvalho Chehab 	.config = sd_config,
9040c0d06caSMauro Carvalho Chehab 	.init = sd_init,
9050c0d06caSMauro Carvalho Chehab 	.init_controls = sd_init_controls,
9060c0d06caSMauro Carvalho Chehab 	.start = sd_start,
9070c0d06caSMauro Carvalho Chehab 	.stopN = sd_stopN,
9080c0d06caSMauro Carvalho Chehab 	.stop0 = sd_stop0,
9090c0d06caSMauro Carvalho Chehab 	.pkt_scan = sd_pkt_scan,
9100c0d06caSMauro Carvalho Chehab 	.dq_callback = do_autogain,
9110c0d06caSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
9120c0d06caSMauro Carvalho Chehab 	.set_register = sd_dbg_s_register,
9130c0d06caSMauro Carvalho Chehab #endif
914ae814c08SPeter Senna Tschudin #if IS_ENABLED(CONFIG_INPUT)
9150c0d06caSMauro Carvalho Chehab 	.int_pkt_scan = sd_int_pkt_scan,
9160c0d06caSMauro Carvalho Chehab #endif
9170c0d06caSMauro Carvalho Chehab };
9180c0d06caSMauro Carvalho Chehab 
9190c0d06caSMauro Carvalho Chehab /* -- module initialisation -- */
9200c0d06caSMauro Carvalho Chehab static const struct usb_device_id device_table[] = {
9210c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x06f8, 0x3009)},
9220c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x06f8, 0x301b)},
9230c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x2620)},
9240c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x2621)},
9250c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x2622), .driver_info = FL_VFLIP},
926242841d3SHans de Goede 	{USB_DEVICE(0x093a, 0x2623), .driver_info = FL_VFLIP},
9270c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x2624), .driver_info = FL_VFLIP},
9280c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x2625)},
9290c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x2626)},
9300c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x2627), .driver_info = FL_VFLIP},
9310c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x2628)},
9320c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x2629), .driver_info = FL_VFLIP},
9330c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x262a)},
9340c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x093a, 0x262c)},
9350c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x145f, 0x013c)},
93697d2fbf5SFrank Schäfer 	{USB_DEVICE(0x1ae7, 0x2001)}, /* SpeedLink Snappy Mic SL-6825-SBK */
9370c0d06caSMauro Carvalho Chehab 	{}
9380c0d06caSMauro Carvalho Chehab };
9390c0d06caSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, device_table);
9400c0d06caSMauro Carvalho Chehab 
9410c0d06caSMauro Carvalho Chehab /* -- device connect -- */
9420c0d06caSMauro Carvalho Chehab static int sd_probe(struct usb_interface *intf,
9430c0d06caSMauro Carvalho Chehab 			const struct usb_device_id *id)
9440c0d06caSMauro Carvalho Chehab {
9450c0d06caSMauro Carvalho Chehab 	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
9460c0d06caSMauro Carvalho Chehab 				THIS_MODULE);
9470c0d06caSMauro Carvalho Chehab }
9480c0d06caSMauro Carvalho Chehab 
9490c0d06caSMauro Carvalho Chehab static struct usb_driver sd_driver = {
9500c0d06caSMauro Carvalho Chehab 	.name = KBUILD_MODNAME,
9510c0d06caSMauro Carvalho Chehab 	.id_table = device_table,
9520c0d06caSMauro Carvalho Chehab 	.probe = sd_probe,
9530c0d06caSMauro Carvalho Chehab 	.disconnect = gspca_disconnect,
9540c0d06caSMauro Carvalho Chehab #ifdef CONFIG_PM
9550c0d06caSMauro Carvalho Chehab 	.suspend = gspca_suspend,
9560c0d06caSMauro Carvalho Chehab 	.resume = gspca_resume,
9570c0d06caSMauro Carvalho Chehab 	.reset_resume = gspca_resume,
9580c0d06caSMauro Carvalho Chehab #endif
9590c0d06caSMauro Carvalho Chehab };
9600c0d06caSMauro Carvalho Chehab 
9610c0d06caSMauro Carvalho Chehab module_usb_driver(sd_driver);
962