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