blob: 4f5869a98082201e2ef39d8bb3f009b0972826a4 [file] [log] [blame]
Marton Nemeth1408b842009-11-02 08:13:21 -03001/*
Jean-François Moineae251e62012-02-27 05:15:12 -03002 * Pixart PAC7302 driver
Marton Nemeth1408b842009-11-02 08:13:21 -03003 *
Jean-François Moineae251e62012-02-27 05:15:12 -03004 * Copyright (C) 2008-2012 Jean-Francois Moine <http://moinejf.free.fr>
5 * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
Marton Nemeth1408b842009-11-02 08:13:21 -03006 *
Jean-Francois Moinecc2f82c2010-01-28 16:35:40 -03007 * Separated from Pixart PAC7311 library by Márton Németh
Márton Némethaed6f1b2010-01-28 16:33:38 -03008 * Camera button input handling by Márton Németh <nm127@freemail.hu>
9 * Copyright (C) 2009-2010 Márton Németh <nm127@freemail.hu>
Marton Nemeth1408b842009-11-02 08:13:21 -030010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
Hans de Goede895d4642012-04-28 10:31:17 -030026/*
27 * Some documentation about various registers as determined by trial and error.
28 *
Frank Schäferb1a19c02012-09-09 15:02:21 -030029 * Register page 0:
30 *
31 * Address Description
Frank Schäfer6f9b3312012-09-23 10:29:42 -030032 * 0x01 Red balance control
33 * 0x02 Green balance control
34 * 0x03 Blue balance control
Frank Schäfer780d6172012-09-09 15:02:24 -030035 * The Windows driver uses a quadratic approach to map
36 * the settable values (0-200) on register values:
Frank Schäfer6f9b3312012-09-23 10:29:42 -030037 * min=0x20, default=0x40, max=0x80
38 * 0x0f-0x20 Color and saturation control
Frank Schäfer780d6172012-09-09 15:02:24 -030039 * 0xa2-0xab Brightness, contrast and gamma control
Frank Schäferb1a19c02012-09-09 15:02:21 -030040 * 0xb6 Sharpness control (bits 0-4)
41 *
Hans de Goede895d4642012-04-28 10:31:17 -030042 * Register page 1:
43 *
44 * Address Description
45 * 0x78 Global control, bit 6 controls the LED (inverted)
Hans de Goede48bb7312012-04-27 13:00:57 -030046 * 0x80 Compression balance, 2 interesting settings:
47 * 0x0f Default
48 * 0x50 Values >= this switch the camera to a lower compression,
49 * using the same table for both luminance and chrominance.
50 * This gives a sharper picture. Only usable when running
51 * at < 15 fps! Note currently the driver does not use this
52 * as the quality gain is small and the generated JPG-s are
53 * only understood by v4l-utils >= 0.8.9
Hans de Goede895d4642012-04-28 10:31:17 -030054 *
55 * Register page 3:
56 *
57 * Address Description
58 * 0x02 Clock divider 3-63, fps = 90 / val. Must be a multiple of 3 on
59 * the 7302, so one of 3, 6, 9, ..., except when between 6 and 12?
60 * 0x03 Variable framerate ctrl reg2==3: 0 -> ~30 fps, 255 -> ~22fps
61 * 0x04 Another var framerate ctrl reg2==3, reg3==0: 0 -> ~30 fps,
62 * 63 -> ~27 fps, the 2 msb's must always be 1 !!
63 * 0x05 Another var framerate ctrl reg2==3, reg3==0, reg4==0xc0:
64 * 1 -> ~30 fps, 2 -> ~20 fps
65 * 0x0e Exposure bits 0-7, 0-448, 0 = use full frame time
66 * 0x0f Exposure bit 8, 0-448, 448 = no exposure at all
Hans de Goede48bb7312012-04-27 13:00:57 -030067 * 0x10 Gain 0-31
68 * 0x12 Another gain 0-31, unlike 0x10 this one seems to start with an
69 * amplification value of 1 rather then 0 at its lowest setting
Hans de Goede895d4642012-04-28 10:31:17 -030070 * 0x21 Bitfield: 0-1 unused, 2-3 vflip/hflip, 4-5 unknown, 6-7 unused
Hans de Goede48bb7312012-04-27 13:00:57 -030071 * 0x80 Another framerate control, best left at 1, moving it from 1 to
72 * 2 causes the framerate to become 3/4th of what it was, and
73 * also seems to cause pixel averaging, resulting in an effective
74 * resolution of 320x240 and thus a much blockier image
Hans de Goede895d4642012-04-28 10:31:17 -030075 *
76 * The registers are accessed in the following functions:
77 *
78 * Page | Register | Function
79 * -----+------------+---------------------------------------------------
Frank Schäfer2b34e9d2012-09-23 10:29:43 -030080 * 0 | 0x01 | setredbalance()
81 * 0 | 0x03 | setbluebalance()
Hans de Goede895d4642012-04-28 10:31:17 -030082 * 0 | 0x0f..0x20 | setcolors()
83 * 0 | 0xa2..0xab | setbrightcont()
Frank Schäferb1a19c02012-09-09 15:02:21 -030084 * 0 | 0xb6 | setsharpness()
Hans de Goede895d4642012-04-28 10:31:17 -030085 * 0 | 0xc6 | setwhitebalance()
Hans de Goede895d4642012-04-28 10:31:17 -030086 * 0 | 0xdc | setbrightcont(), setcolors()
87 * 3 | 0x02 | setexposure()
Hans de Goededf8b9852012-04-28 10:12:28 -030088 * 3 | 0x10, 0x12 | setgain()
Hans de Goede895d4642012-04-28 10:31:17 -030089 * 3 | 0x11 | setcolors(), setgain(), setexposure(), sethvflip()
90 * 3 | 0x21 | sethvflip()
91 */
Marton Nemeth1408b842009-11-02 08:13:21 -030092
Joe Perches133a9fe2011-08-21 19:56:57 -030093#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
94
Márton Némethaed6f1b2010-01-28 16:33:38 -030095#include <linux/input.h>
Márton Németh6763cc02009-11-09 07:10:46 -030096#include <media/v4l2-chip-ident.h>
Marton Nemeth1408b842009-11-02 08:13:21 -030097#include "gspca.h"
Jean-François Moineac399cd2012-02-27 05:40:47 -030098/* Include pac common sof detection functions */
99#include "pac_common.h"
Marton Nemeth1408b842009-11-02 08:13:21 -0300100
Frank Schäfer2b34e9d2012-09-23 10:29:43 -0300101#define PAC7302_RGB_BALANCE_MIN 0
102#define PAC7302_RGB_BALANCE_MAX 200
103#define PAC7302_RGB_BALANCE_DEFAULT 100
104#define PAC7302_GAIN_DEFAULT 15
105#define PAC7302_GAIN_KNEE 42
106#define PAC7302_EXPOSURE_DEFAULT 66 /* 33 ms / 30 fps */
107#define PAC7302_EXPOSURE_KNEE 133 /* 66 ms / 15 fps */
Hans de Goede74233cd2012-05-14 11:16:09 -0300108
Jean-François Moineae251e62012-02-27 05:15:12 -0300109MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>, "
110 "Thomas Kaiser thomas@kaiser-linux.li");
Marton Nemeth1408b842009-11-02 08:13:21 -0300111MODULE_DESCRIPTION("Pixart PAC7302");
112MODULE_LICENSE("GPL");
113
Marton Nemeth1408b842009-11-02 08:13:21 -0300114struct sd {
115 struct gspca_dev gspca_dev; /* !! must be the first item */
116
Hans de Goede74233cd2012-05-14 11:16:09 -0300117 struct { /* brightness / contrast cluster */
118 struct v4l2_ctrl *brightness;
119 struct v4l2_ctrl *contrast;
120 };
121 struct v4l2_ctrl *saturation;
122 struct v4l2_ctrl *white_balance;
123 struct v4l2_ctrl *red_balance;
124 struct v4l2_ctrl *blue_balance;
125 struct { /* flip cluster */
126 struct v4l2_ctrl *hflip;
127 struct v4l2_ctrl *vflip;
128 };
Frank Schäferb1a19c02012-09-09 15:02:21 -0300129 struct v4l2_ctrl *sharpness;
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300130 u8 flags;
131#define FL_HFLIP 0x01 /* mirrored by default */
132#define FL_VFLIP 0x02 /* vertical flipped by default */
Marton Nemeth1408b842009-11-02 08:13:21 -0300133
134 u8 sof_read;
Jean-François Moineac399cd2012-02-27 05:40:47 -0300135 s8 autogain_ignore_frames;
Marton Nemeth1408b842009-11-02 08:13:21 -0300136
137 atomic_t avg_lum;
138};
139
Marton Nemeth1408b842009-11-02 08:13:21 -0300140static const struct v4l2_pix_format vga_mode[] = {
141 {640, 480, V4L2_PIX_FMT_PJPG, V4L2_FIELD_NONE,
142 .bytesperline = 640,
143 .sizeimage = 640 * 480 * 3 / 8 + 590,
144 .colorspace = V4L2_COLORSPACE_JPEG,
Jean-François Moineae251e62012-02-27 05:15:12 -0300145 },
Marton Nemeth1408b842009-11-02 08:13:21 -0300146};
147
148#define LOAD_PAGE3 255
Marton Nemeth1408b842009-11-02 08:13:21 -0300149#define END_OF_SEQUENCE 0
150
Jean-François Moineae251e62012-02-27 05:15:12 -0300151static const u8 init_7302[] = {
Marton Nemeth1408b842009-11-02 08:13:21 -0300152/* index,value */
153 0xff, 0x01, /* page 1 */
154 0x78, 0x00, /* deactivate */
155 0xff, 0x01,
156 0x78, 0x40, /* led off */
157};
Jean-François Moineae251e62012-02-27 05:15:12 -0300158static const u8 start_7302[] = {
Marton Nemeth1408b842009-11-02 08:13:21 -0300159/* index, len, [value]* */
160 0xff, 1, 0x00, /* page 0 */
161 0x00, 12, 0x01, 0x40, 0x40, 0x40, 0x01, 0xe0, 0x02, 0x80,
162 0x00, 0x00, 0x00, 0x00,
163 0x0d, 24, 0x03, 0x01, 0x00, 0xb5, 0x07, 0xcb, 0x00, 0x00,
164 0x07, 0xc8, 0x00, 0xea, 0x07, 0xcf, 0x07, 0xf7,
165 0x07, 0x7e, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x11,
166 0x26, 2, 0xaa, 0xaa,
167 0x2e, 1, 0x31,
168 0x38, 1, 0x01,
169 0x3a, 3, 0x14, 0xff, 0x5a,
170 0x43, 11, 0x00, 0x0a, 0x18, 0x11, 0x01, 0x2c, 0x88, 0x11,
171 0x00, 0x54, 0x11,
172 0x55, 1, 0x00,
Jean-François Moineae251e62012-02-27 05:15:12 -0300173 0x62, 4, 0x10, 0x1e, 0x1e, 0x18,
Marton Nemeth1408b842009-11-02 08:13:21 -0300174 0x6b, 1, 0x00,
175 0x6e, 3, 0x08, 0x06, 0x00,
176 0x72, 3, 0x00, 0xff, 0x00,
177 0x7d, 23, 0x01, 0x01, 0x58, 0x46, 0x50, 0x3c, 0x50, 0x3c,
178 0x54, 0x46, 0x54, 0x56, 0x52, 0x50, 0x52, 0x50,
179 0x56, 0x64, 0xa4, 0x00, 0xda, 0x00, 0x00,
180 0xa2, 10, 0x22, 0x2c, 0x3c, 0x54, 0x69, 0x7c, 0x9c, 0xb9,
181 0xd2, 0xeb,
182 0xaf, 1, 0x02,
183 0xb5, 2, 0x08, 0x08,
184 0xb8, 2, 0x08, 0x88,
185 0xc4, 4, 0xae, 0x01, 0x04, 0x01,
186 0xcc, 1, 0x00,
187 0xd1, 11, 0x01, 0x30, 0x49, 0x5e, 0x6f, 0x7f, 0x8e, 0xa9,
188 0xc1, 0xd7, 0xec,
189 0xdc, 1, 0x01,
190 0xff, 1, 0x01, /* page 1 */
191 0x12, 3, 0x02, 0x00, 0x01,
192 0x3e, 2, 0x00, 0x00,
193 0x76, 5, 0x01, 0x20, 0x40, 0x00, 0xf2,
194 0x7c, 1, 0x00,
195 0x7f, 10, 0x4b, 0x0f, 0x01, 0x2c, 0x02, 0x58, 0x03, 0x20,
196 0x02, 0x00,
197 0x96, 5, 0x01, 0x10, 0x04, 0x01, 0x04,
198 0xc8, 14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00,
199 0x07, 0x00, 0x01, 0x07, 0x04, 0x01,
200 0xd8, 1, 0x01,
201 0xdb, 2, 0x00, 0x01,
202 0xde, 7, 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00,
203 0xe6, 4, 0x00, 0x00, 0x00, 0x01,
204 0xeb, 1, 0x00,
205 0xff, 1, 0x02, /* page 2 */
206 0x22, 1, 0x00,
207 0xff, 1, 0x03, /* page 3 */
208 0, LOAD_PAGE3, /* load the page 3 */
209 0x11, 1, 0x01,
210 0xff, 1, 0x02, /* page 2 */
211 0x13, 1, 0x00,
212 0x22, 4, 0x1f, 0xa4, 0xf0, 0x96,
213 0x27, 2, 0x14, 0x0c,
214 0x2a, 5, 0xc8, 0x00, 0x18, 0x12, 0x22,
215 0x64, 8, 0x00, 0x00, 0xf0, 0x01, 0x14, 0x44, 0x44, 0x44,
216 0x6e, 1, 0x08,
217 0xff, 1, 0x01, /* page 1 */
218 0x78, 1, 0x00,
219 0, END_OF_SEQUENCE /* end of sequence */
220};
221
222#define SKIP 0xaa
223/* page 3 - the value SKIP says skip the index - see reg_w_page() */
Jean-François Moineae251e62012-02-27 05:15:12 -0300224static const u8 page3_7302[] = {
Hans de Goede5fb2dde2010-02-17 11:59:19 -0300225 0x90, 0x40, 0x03, 0x00, 0xc0, 0x01, 0x14, 0x16,
Marton Nemeth1408b842009-11-02 08:13:21 -0300226 0x14, 0x12, 0x00, 0x00, 0x00, 0x02, 0x33, 0x00,
227 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x47, 0x01, 0xb3, 0x01, 0x00,
229 0x00, 0x08, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x21,
230 0x00, 0x00, 0x00, 0x54, 0xf4, 0x02, 0x52, 0x54,
231 0xa4, 0xb8, 0xe0, 0x2a, 0xf6, 0x00, 0x00, 0x00,
232 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233 0x00, 0xfc, 0x00, 0xf2, 0x1f, 0x04, 0x00, 0x00,
Jean-Francois Moinecdf955c2010-01-11 15:06:12 -0300234 SKIP, 0x00, 0x00, 0xc0, 0xc0, 0x10, 0x00, 0x00,
Marton Nemeth1408b842009-11-02 08:13:21 -0300235 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 0x00, 0x40, 0xff, 0x03, 0x19, 0x00, 0x00, 0x00,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0xc8, 0xc8,
239 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
240 0x08, 0x10, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00,
241 0x01, 0x00, 0x02, 0x47, 0x00, 0x00, 0x00, 0x00,
242 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243 0x00, 0x02, 0xfa, 0x00, 0x64, 0x5a, 0x28, 0x00,
244 0x00
245};
246
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300247static void reg_w_buf(struct gspca_dev *gspca_dev,
Jean-François Moineae251e62012-02-27 05:15:12 -0300248 u8 index,
Jean-François Moine0aeb5ec2010-12-28 06:59:04 -0300249 const u8 *buffer, int len)
Marton Nemeth1408b842009-11-02 08:13:21 -0300250{
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300251 int ret;
252
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300253 if (gspca_dev->usb_err < 0)
254 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300255 memcpy(gspca_dev->usb_buf, buffer, len);
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300256 ret = usb_control_msg(gspca_dev->dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300257 usb_sndctrlpipe(gspca_dev->dev, 0),
Jean-François Moinea1317132010-06-24 04:50:26 -0300258 0, /* request */
Marton Nemeth1408b842009-11-02 08:13:21 -0300259 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
260 0, /* value */
261 index, gspca_dev->usb_buf, len,
262 500);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300263 if (ret < 0) {
Jean-François Moineae251e62012-02-27 05:15:12 -0300264 pr_err("reg_w_buf failed i: %02x error %d\n",
Joe Perches133a9fe2011-08-21 19:56:57 -0300265 index, ret);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300266 gspca_dev->usb_err = ret;
267 }
Marton Nemeth1408b842009-11-02 08:13:21 -0300268}
269
270
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300271static void reg_w(struct gspca_dev *gspca_dev,
Jean-François Moineae251e62012-02-27 05:15:12 -0300272 u8 index,
273 u8 value)
Marton Nemeth1408b842009-11-02 08:13:21 -0300274{
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300275 int ret;
276
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300277 if (gspca_dev->usb_err < 0)
278 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300279 gspca_dev->usb_buf[0] = value;
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300280 ret = usb_control_msg(gspca_dev->dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300281 usb_sndctrlpipe(gspca_dev->dev, 0),
282 0, /* request */
283 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
284 0, index, gspca_dev->usb_buf, 1,
285 500);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300286 if (ret < 0) {
Jean-François Moineae251e62012-02-27 05:15:12 -0300287 pr_err("reg_w() failed i: %02x v: %02x error %d\n",
Joe Perches133a9fe2011-08-21 19:56:57 -0300288 index, value, ret);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300289 gspca_dev->usb_err = ret;
290 }
Marton Nemeth1408b842009-11-02 08:13:21 -0300291}
292
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300293static void reg_w_seq(struct gspca_dev *gspca_dev,
Jean-François Moineae251e62012-02-27 05:15:12 -0300294 const u8 *seq, int len)
Marton Nemeth1408b842009-11-02 08:13:21 -0300295{
296 while (--len >= 0) {
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300297 reg_w(gspca_dev, seq[0], seq[1]);
Marton Nemeth1408b842009-11-02 08:13:21 -0300298 seq += 2;
299 }
300}
301
302/* load the beginning of a page */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300303static void reg_w_page(struct gspca_dev *gspca_dev,
Jean-François Moineae251e62012-02-27 05:15:12 -0300304 const u8 *page, int len)
Marton Nemeth1408b842009-11-02 08:13:21 -0300305{
306 int index;
Márton Némethb1784b32009-11-07 05:52:02 -0300307 int ret = 0;
Marton Nemeth1408b842009-11-02 08:13:21 -0300308
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300309 if (gspca_dev->usb_err < 0)
310 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300311 for (index = 0; index < len; index++) {
312 if (page[index] == SKIP) /* skip this index */
313 continue;
314 gspca_dev->usb_buf[0] = page[index];
Marton Nemeth4f7309e2009-11-05 05:35:08 -0300315 ret = usb_control_msg(gspca_dev->dev,
Marton Nemeth1408b842009-11-02 08:13:21 -0300316 usb_sndctrlpipe(gspca_dev->dev, 0),
317 0, /* request */
318 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
319 0, index, gspca_dev->usb_buf, 1,
320 500);
Márton Némethb1784b32009-11-07 05:52:02 -0300321 if (ret < 0) {
Jean-François Moineae251e62012-02-27 05:15:12 -0300322 pr_err("reg_w_page() failed i: %02x v: %02x error %d\n",
Joe Perches133a9fe2011-08-21 19:56:57 -0300323 index, page[index], ret);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300324 gspca_dev->usb_err = ret;
Márton Némethb1784b32009-11-07 05:52:02 -0300325 break;
326 }
Marton Nemeth1408b842009-11-02 08:13:21 -0300327 }
328}
329
330/* output a variable sequence */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300331static void reg_w_var(struct gspca_dev *gspca_dev,
Jean-François Moineae251e62012-02-27 05:15:12 -0300332 const u8 *seq,
333 const u8 *page3, unsigned int page3_len)
Marton Nemeth1408b842009-11-02 08:13:21 -0300334{
335 int index, len;
336
337 for (;;) {
338 index = *seq++;
339 len = *seq++;
340 switch (len) {
341 case END_OF_SEQUENCE:
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300342 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300343 case LOAD_PAGE3:
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300344 reg_w_page(gspca_dev, page3, page3_len);
Marton Nemeth1408b842009-11-02 08:13:21 -0300345 break;
346 default:
Jean-François Moineae251e62012-02-27 05:15:12 -0300347#ifdef GSPCA_DEBUG
Marton Nemeth1408b842009-11-02 08:13:21 -0300348 if (len > USB_BUF_SZ) {
349 PDEBUG(D_ERR|D_STREAM,
350 "Incorrect variable sequence");
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300351 return;
Marton Nemeth1408b842009-11-02 08:13:21 -0300352 }
Jean-François Moineae251e62012-02-27 05:15:12 -0300353#endif
Marton Nemeth1408b842009-11-02 08:13:21 -0300354 while (len > 0) {
355 if (len < 8) {
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300356 reg_w_buf(gspca_dev,
Márton Némethb1784b32009-11-07 05:52:02 -0300357 index, seq, len);
Marton Nemeth1408b842009-11-02 08:13:21 -0300358 seq += len;
359 break;
360 }
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300361 reg_w_buf(gspca_dev, index, seq, 8);
Marton Nemeth1408b842009-11-02 08:13:21 -0300362 seq += 8;
363 index += 8;
364 len -= 8;
365 }
366 }
367 }
368 /* not reached */
369}
370
371/* this function is called at probe time for pac7302 */
372static int sd_config(struct gspca_dev *gspca_dev,
373 const struct usb_device_id *id)
374{
375 struct sd *sd = (struct sd *) gspca_dev;
376 struct cam *cam;
377
378 cam = &gspca_dev->cam;
379
Marton Nemeth1408b842009-11-02 08:13:21 -0300380 cam->cam_mode = vga_mode; /* only 640x480 */
381 cam->nmodes = ARRAY_SIZE(vga_mode);
382
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300383 sd->flags = id->driver_info;
Marton Nemeth1408b842009-11-02 08:13:21 -0300384 return 0;
385}
386
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300387static void setbrightcont(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300388{
389 struct sd *sd = (struct sd *) gspca_dev;
390 int i, v;
Jean-François Moineae251e62012-02-27 05:15:12 -0300391 static const u8 max[10] =
Marton Nemeth1408b842009-11-02 08:13:21 -0300392 {0x29, 0x33, 0x42, 0x5a, 0x6e, 0x80, 0x9f, 0xbb,
393 0xd4, 0xec};
Jean-François Moineae251e62012-02-27 05:15:12 -0300394 static const u8 delta[10] =
Marton Nemeth1408b842009-11-02 08:13:21 -0300395 {0x35, 0x33, 0x33, 0x2f, 0x2a, 0x25, 0x1e, 0x17,
396 0x11, 0x0b};
397
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300398 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
Marton Nemeth1408b842009-11-02 08:13:21 -0300399 for (i = 0; i < 10; i++) {
400 v = max[i];
Hans de Goede74233cd2012-05-14 11:16:09 -0300401 v += (sd->brightness->val - sd->brightness->maximum)
402 * 150 / sd->brightness->maximum; /* 200 ? */
403 v -= delta[i] * sd->contrast->val / sd->contrast->maximum;
Marton Nemeth1408b842009-11-02 08:13:21 -0300404 if (v < 0)
405 v = 0;
406 else if (v > 0xff)
407 v = 0xff;
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300408 reg_w(gspca_dev, 0xa2 + i, v);
Marton Nemeth1408b842009-11-02 08:13:21 -0300409 }
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300410 reg_w(gspca_dev, 0xdc, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300411}
412
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300413static void setcolors(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300414{
415 struct sd *sd = (struct sd *) gspca_dev;
416 int i, v;
417 static const int a[9] =
418 {217, -212, 0, -101, 170, -67, -38, -315, 355};
419 static const int b[9] =
420 {19, 106, 0, 19, 106, 1, 19, 106, 1};
421
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300422 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
423 reg_w(gspca_dev, 0x11, 0x01);
424 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
Marton Nemeth1408b842009-11-02 08:13:21 -0300425 for (i = 0; i < 9; i++) {
Hans de Goede74233cd2012-05-14 11:16:09 -0300426 v = a[i] * sd->saturation->val / sd->saturation->maximum;
427 v += b[i];
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300428 reg_w(gspca_dev, 0x0f + 2 * i, (v >> 8) & 0x07);
429 reg_w(gspca_dev, 0x0f + 2 * i + 1, v);
Marton Nemeth1408b842009-11-02 08:13:21 -0300430 }
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300431 reg_w(gspca_dev, 0xdc, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300432}
433
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300434static void setwhitebalance(struct gspca_dev *gspca_dev)
Marton Nemeth23fbee62009-11-08 04:35:12 -0300435{
436 struct sd *sd = (struct sd *) gspca_dev;
Marton Nemeth23fbee62009-11-08 04:35:12 -0300437
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300438 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
Hans de Goede74233cd2012-05-14 11:16:09 -0300439 reg_w(gspca_dev, 0xc6, sd->white_balance->val);
Marton Nemeth23fbee62009-11-08 04:35:12 -0300440
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300441 reg_w(gspca_dev, 0xdc, 0x01);
Marton Nemeth23fbee62009-11-08 04:35:12 -0300442}
443
Frank Schäfer2b34e9d2012-09-23 10:29:43 -0300444static u8 rgbbalance_ctrl_to_reg_value(s32 rgb_ctrl_val)
445{
446 const unsigned int k = 1000; /* precision factor */
447 unsigned int norm;
448
449 /* Normed value [0...k] */
450 norm = k * (rgb_ctrl_val - PAC7302_RGB_BALANCE_MIN)
451 / (PAC7302_RGB_BALANCE_MAX - PAC7302_RGB_BALANCE_MIN);
452 /* Qudratic apporach improves control at small (register) values: */
453 return 64 * norm * norm / (k*k) + 32 * norm / k + 32;
454 /* Y = 64*X*X + 32*X + 32
455 * => register values 0x20-0x80; Windows driver uses these limits */
456
457 /* NOTE: for full value range (0x00-0xff) use
458 * Y = 254*X*X + X
459 * => 254 * norm * norm / (k*k) + 1 * norm / k */
460}
461
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300462static void setredbalance(struct gspca_dev *gspca_dev)
Márton Németh265a8092009-11-07 15:15:56 -0300463{
464 struct sd *sd = (struct sd *) gspca_dev;
Márton Németh265a8092009-11-07 15:15:56 -0300465
Frank Schäfer2b34e9d2012-09-23 10:29:43 -0300466 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
467 reg_w(gspca_dev, 0x01,
468 rgbbalance_ctrl_to_reg_value(sd->red_balance->val));
Márton Németh265a8092009-11-07 15:15:56 -0300469
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300470 reg_w(gspca_dev, 0xdc, 0x01);
Márton Németh265a8092009-11-07 15:15:56 -0300471}
472
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300473static void setbluebalance(struct gspca_dev *gspca_dev)
Márton Németh265a8092009-11-07 15:15:56 -0300474{
475 struct sd *sd = (struct sd *) gspca_dev;
Márton Németh265a8092009-11-07 15:15:56 -0300476
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300477 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
Frank Schäfer2b34e9d2012-09-23 10:29:43 -0300478 reg_w(gspca_dev, 0x03,
479 rgbbalance_ctrl_to_reg_value(sd->blue_balance->val));
Márton Németh265a8092009-11-07 15:15:56 -0300480
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300481 reg_w(gspca_dev, 0xdc, 0x01);
Márton Németh265a8092009-11-07 15:15:56 -0300482}
483
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300484static void setgain(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300485{
Hans de Goededf8b9852012-04-28 10:12:28 -0300486 u8 reg10, reg12;
487
Hans de Goede74233cd2012-05-14 11:16:09 -0300488 if (gspca_dev->gain->val < 32) {
489 reg10 = gspca_dev->gain->val;
Hans de Goededf8b9852012-04-28 10:12:28 -0300490 reg12 = 0;
491 } else {
492 reg10 = 31;
Hans de Goede74233cd2012-05-14 11:16:09 -0300493 reg12 = gspca_dev->gain->val - 31;
Hans de Goededf8b9852012-04-28 10:12:28 -0300494 }
Marton Nemeth1408b842009-11-02 08:13:21 -0300495
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300496 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
Hans de Goededf8b9852012-04-28 10:12:28 -0300497 reg_w(gspca_dev, 0x10, reg10);
498 reg_w(gspca_dev, 0x12, reg12);
Marton Nemeth1408b842009-11-02 08:13:21 -0300499
500 /* load registers to sensor (Bit 0, auto clear) */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300501 reg_w(gspca_dev, 0x11, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300502}
503
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300504static void setexposure(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300505{
Jean-François Moineae251e62012-02-27 05:15:12 -0300506 u8 clockdiv;
507 u16 exposure;
Marton Nemeth1408b842009-11-02 08:13:21 -0300508
Hans de Goede895d4642012-04-28 10:31:17 -0300509 /*
510 * Register 2 of frame 3 contains the clock divider configuring the
511 * no fps according to the formula: 90 / reg. sd->exposure is the
512 * desired exposure time in 0.5 ms.
513 */
Hans de Goede74233cd2012-05-14 11:16:09 -0300514 clockdiv = (90 * gspca_dev->exposure->val + 1999) / 2000;
Marton Nemeth1408b842009-11-02 08:13:21 -0300515
Hans de Goede895d4642012-04-28 10:31:17 -0300516 /*
517 * Note clockdiv = 3 also works, but when running at 30 fps, depending
518 * on the scene being recorded, the camera switches to another
519 * quantization table for certain JPEG blocks, and we don't know how
520 * to decompress these blocks. So we cap the framerate at 15 fps.
521 */
Hans de Goede5fb2dde2010-02-17 11:59:19 -0300522 if (clockdiv < 6)
523 clockdiv = 6;
524 else if (clockdiv > 63)
525 clockdiv = 63;
526
Hans de Goede895d4642012-04-28 10:31:17 -0300527 /*
528 * Register 2 MUST be a multiple of 3, except when between 6 and 12?
529 * Always round up, otherwise we cannot get the desired frametime
530 * using the partial frame time exposure control.
531 */
Hans de Goede5fb2dde2010-02-17 11:59:19 -0300532 if (clockdiv < 6 || clockdiv > 12)
533 clockdiv = ((clockdiv + 2) / 3) * 3;
534
Hans de Goede895d4642012-04-28 10:31:17 -0300535 /*
536 * frame exposure time in ms = 1000 * clockdiv / 90 ->
537 * exposure = (sd->exposure / 2) * 448 / (1000 * clockdiv / 90)
538 */
Hans de Goede74233cd2012-05-14 11:16:09 -0300539 exposure = (gspca_dev->exposure->val * 45 * 448) / (1000 * clockdiv);
Hans de Goede5fb2dde2010-02-17 11:59:19 -0300540 /* 0 = use full frametime, 448 = no exposure, reverse it */
541 exposure = 448 - exposure;
542
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300543 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
Hans de Goede5fb2dde2010-02-17 11:59:19 -0300544 reg_w(gspca_dev, 0x02, clockdiv);
545 reg_w(gspca_dev, 0x0e, exposure & 0xff);
546 reg_w(gspca_dev, 0x0f, exposure >> 8);
Marton Nemeth1408b842009-11-02 08:13:21 -0300547
548 /* load registers to sensor (Bit 0, auto clear) */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300549 reg_w(gspca_dev, 0x11, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300550}
551
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300552static void sethvflip(struct gspca_dev *gspca_dev)
Marton Nemeth1408b842009-11-02 08:13:21 -0300553{
554 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300555 u8 data, hflip, vflip;
556
Hans de Goede74233cd2012-05-14 11:16:09 -0300557 hflip = sd->hflip->val;
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300558 if (sd->flags & FL_HFLIP)
559 hflip = !hflip;
Hans de Goede74233cd2012-05-14 11:16:09 -0300560 vflip = sd->vflip->val;
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300561 if (sd->flags & FL_VFLIP)
562 vflip = !vflip;
Marton Nemeth1408b842009-11-02 08:13:21 -0300563
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300564 reg_w(gspca_dev, 0xff, 0x03); /* page 3 */
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300565 data = (hflip ? 0x08 : 0x00) | (vflip ? 0x04 : 0x00);
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300566 reg_w(gspca_dev, 0x21, data);
567
Marton Nemeth1408b842009-11-02 08:13:21 -0300568 /* load registers to sensor (Bit 0, auto clear) */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300569 reg_w(gspca_dev, 0x11, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300570}
571
Frank Schäferb1a19c02012-09-09 15:02:21 -0300572static void setsharpness(struct gspca_dev *gspca_dev)
573{
574 struct sd *sd = (struct sd *) gspca_dev;
575
576 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
577 reg_w(gspca_dev, 0xb6, sd->sharpness->val);
578
579 reg_w(gspca_dev, 0xdc, 0x01);
580}
581
Marton Nemeth1408b842009-11-02 08:13:21 -0300582/* this function is called at probe and resume time for pac7302 */
583static int sd_init(struct gspca_dev *gspca_dev)
584{
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300585 reg_w_seq(gspca_dev, init_7302, sizeof(init_7302)/2);
586 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -0300587}
588
Hans de Goede74233cd2012-05-14 11:16:09 -0300589static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
590{
591 struct gspca_dev *gspca_dev =
592 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
593 struct sd *sd = (struct sd *)gspca_dev;
594
595 gspca_dev->usb_err = 0;
596
597 if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
598 /* when switching to autogain set defaults to make sure
599 we are on a valid point of the autogain gain /
600 exposure knee graph, and give this change time to
601 take effect before doing autogain. */
602 gspca_dev->exposure->val = PAC7302_EXPOSURE_DEFAULT;
603 gspca_dev->gain->val = PAC7302_GAIN_DEFAULT;
604 sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
605 }
606
607 if (!gspca_dev->streaming)
608 return 0;
609
610 switch (ctrl->id) {
611 case V4L2_CID_BRIGHTNESS:
612 setbrightcont(gspca_dev);
613 break;
614 case V4L2_CID_SATURATION:
615 setcolors(gspca_dev);
616 break;
617 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
618 setwhitebalance(gspca_dev);
619 break;
620 case V4L2_CID_RED_BALANCE:
621 setredbalance(gspca_dev);
622 break;
623 case V4L2_CID_BLUE_BALANCE:
624 setbluebalance(gspca_dev);
625 break;
626 case V4L2_CID_AUTOGAIN:
627 if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
628 setexposure(gspca_dev);
629 if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
630 setgain(gspca_dev);
631 break;
632 case V4L2_CID_HFLIP:
633 sethvflip(gspca_dev);
634 break;
Frank Schäferb1a19c02012-09-09 15:02:21 -0300635 case V4L2_CID_SHARPNESS:
636 setsharpness(gspca_dev);
637 break;
Hans de Goede74233cd2012-05-14 11:16:09 -0300638 default:
639 return -EINVAL;
640 }
641 return gspca_dev->usb_err;
642}
643
644static const struct v4l2_ctrl_ops sd_ctrl_ops = {
645 .s_ctrl = sd_s_ctrl,
646};
647
648/* this function is called at probe time */
649static int sd_init_controls(struct gspca_dev *gspca_dev)
650{
651 struct sd *sd = (struct sd *) gspca_dev;
652 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
653
654 gspca_dev->vdev.ctrl_handler = hdl;
Frank Schäferb1a19c02012-09-09 15:02:21 -0300655 v4l2_ctrl_handler_init(hdl, 12);
Hans de Goede74233cd2012-05-14 11:16:09 -0300656
657 sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
658 V4L2_CID_BRIGHTNESS, 0, 32, 1, 16);
659 sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
660 V4L2_CID_CONTRAST, 0, 255, 1, 127);
661
662 sd->saturation = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
663 V4L2_CID_SATURATION, 0, 255, 1, 127);
664 sd->white_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
665 V4L2_CID_WHITE_BALANCE_TEMPERATURE,
Frank Schäferf58e5cd2012-09-09 15:02:22 -0300666 0, 255, 1, 55);
Hans de Goede74233cd2012-05-14 11:16:09 -0300667 sd->red_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
Frank Schäfer2b34e9d2012-09-23 10:29:43 -0300668 V4L2_CID_RED_BALANCE,
669 PAC7302_RGB_BALANCE_MIN,
670 PAC7302_RGB_BALANCE_MAX,
671 1, PAC7302_RGB_BALANCE_DEFAULT);
Hans de Goede74233cd2012-05-14 11:16:09 -0300672 sd->blue_balance = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
Frank Schäfer2b34e9d2012-09-23 10:29:43 -0300673 V4L2_CID_BLUE_BALANCE,
674 PAC7302_RGB_BALANCE_MIN,
675 PAC7302_RGB_BALANCE_MAX,
676 1, PAC7302_RGB_BALANCE_DEFAULT);
Hans de Goede74233cd2012-05-14 11:16:09 -0300677
678 gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
679 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
680 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
681 V4L2_CID_EXPOSURE, 0, 1023, 1,
682 PAC7302_EXPOSURE_DEFAULT);
683 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
684 V4L2_CID_GAIN, 0, 62, 1,
685 PAC7302_GAIN_DEFAULT);
686
687 sd->hflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
688 V4L2_CID_HFLIP, 0, 1, 1, 0);
689 sd->vflip = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
690 V4L2_CID_VFLIP, 0, 1, 1, 0);
691
Frank Schäferb1a19c02012-09-09 15:02:21 -0300692 sd->sharpness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
693 V4L2_CID_SHARPNESS, 0, 15, 1, 8);
694
Hans de Goede74233cd2012-05-14 11:16:09 -0300695 if (hdl->error) {
696 pr_err("Could not initialize controls\n");
697 return hdl->error;
698 }
699
700 v4l2_ctrl_cluster(2, &sd->brightness);
701 v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
702 v4l2_ctrl_cluster(2, &sd->hflip);
703 return 0;
704}
705
706/* -- start the camera -- */
Marton Nemeth1408b842009-11-02 08:13:21 -0300707static int sd_start(struct gspca_dev *gspca_dev)
708{
709 struct sd *sd = (struct sd *) gspca_dev;
710
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300711 reg_w_var(gspca_dev, start_7302,
Jean-Francois Moine23a5de22010-01-13 08:30:30 -0300712 page3_7302, sizeof(page3_7302));
Marton Nemeth1408b842009-11-02 08:13:21 -0300713
Marton Nemeth1408b842009-11-02 08:13:21 -0300714 sd->sof_read = 0;
Hans de Goede74233cd2012-05-14 11:16:09 -0300715 sd->autogain_ignore_frames = 0;
716 atomic_set(&sd->avg_lum, 270 + sd->brightness->val);
Marton Nemeth1408b842009-11-02 08:13:21 -0300717
718 /* start stream */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300719 reg_w(gspca_dev, 0xff, 0x01);
720 reg_w(gspca_dev, 0x78, 0x01);
Marton Nemeth1408b842009-11-02 08:13:21 -0300721
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300722 return gspca_dev->usb_err;
Marton Nemeth1408b842009-11-02 08:13:21 -0300723}
724
725static void sd_stopN(struct gspca_dev *gspca_dev)
726{
Márton Némethb1784b32009-11-07 05:52:02 -0300727
Márton Németh67c98f72009-11-07 05:45:33 -0300728 /* stop stream */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300729 reg_w(gspca_dev, 0xff, 0x01);
730 reg_w(gspca_dev, 0x78, 0x00);
Marton Nemeth1408b842009-11-02 08:13:21 -0300731}
732
733/* called on streamoff with alt 0 and on disconnect for pac7302 */
734static void sd_stop0(struct gspca_dev *gspca_dev)
735{
736 if (!gspca_dev->present)
737 return;
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300738 reg_w(gspca_dev, 0xff, 0x01);
739 reg_w(gspca_dev, 0x78, 0x40);
Marton Nemeth1408b842009-11-02 08:13:21 -0300740}
741
Marton Nemeth1408b842009-11-02 08:13:21 -0300742static void do_autogain(struct gspca_dev *gspca_dev)
743{
744 struct sd *sd = (struct sd *) gspca_dev;
745 int avg_lum = atomic_read(&sd->avg_lum);
Hans de Goede5fb2dde2010-02-17 11:59:19 -0300746 int desired_lum;
747 const int deadzone = 30;
Marton Nemeth1408b842009-11-02 08:13:21 -0300748
Jean-François Moineac399cd2012-02-27 05:40:47 -0300749 if (sd->autogain_ignore_frames < 0)
Marton Nemeth1408b842009-11-02 08:13:21 -0300750 return;
751
Jean-François Moineac399cd2012-02-27 05:40:47 -0300752 if (sd->autogain_ignore_frames > 0) {
Marton Nemeth1408b842009-11-02 08:13:21 -0300753 sd->autogain_ignore_frames--;
Jean-François Moineac399cd2012-02-27 05:40:47 -0300754 } else {
Hans de Goede74233cd2012-05-14 11:16:09 -0300755 desired_lum = 270 + sd->brightness->val;
Jean-François Moineac399cd2012-02-27 05:40:47 -0300756
Hans de Goede74233cd2012-05-14 11:16:09 -0300757 if (gspca_expo_autogain(gspca_dev, avg_lum, desired_lum,
758 deadzone, PAC7302_GAIN_KNEE,
759 PAC7302_EXPOSURE_KNEE))
760 sd->autogain_ignore_frames =
761 PAC_AUTOGAIN_IGNORE_FRAMES;
Jean-François Moineac399cd2012-02-27 05:40:47 -0300762 }
Marton Nemeth1408b842009-11-02 08:13:21 -0300763}
764
Jean-François Moine7532e812012-02-27 05:21:57 -0300765/* JPEG header */
766static const u8 jpeg_header[] = {
767 0xff, 0xd8, /* SOI: Start of Image */
Marton Nemeth1408b842009-11-02 08:13:21 -0300768
Jean-François Moine7532e812012-02-27 05:21:57 -0300769 0xff, 0xc0, /* SOF0: Start of Frame (Baseline DCT) */
770 0x00, 0x11, /* length = 17 bytes (including this length field) */
771 0x08, /* Precision: 8 */
772 0x02, 0x80, /* height = 640 (image rotated) */
773 0x01, 0xe0, /* width = 480 */
774 0x03, /* Number of image components: 3 */
775 0x01, 0x21, 0x00, /* ID=1, Subsampling 1x1, Quantization table: 0 */
776 0x02, 0x11, 0x01, /* ID=2, Subsampling 2x1, Quantization table: 1 */
777 0x03, 0x11, 0x01, /* ID=3, Subsampling 2x1, Quantization table: 1 */
778
779 0xff, 0xda, /* SOS: Start Of Scan */
780 0x00, 0x0c, /* length = 12 bytes (including this length field) */
781 0x03, /* number of components: 3 */
782 0x01, 0x00, /* selector 1, table 0x00 */
783 0x02, 0x11, /* selector 2, table 0x11 */
784 0x03, 0x11, /* selector 3, table 0x11 */
785 0x00, 0x3f, /* Spectral selection: 0 .. 63 */
786 0x00 /* Successive approximation: 0 */
Marton Nemeth1408b842009-11-02 08:13:21 -0300787};
788
Marton Nemeth1408b842009-11-02 08:13:21 -0300789/* this function is run at interrupt level */
790static void sd_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300791 u8 *data, /* isoc packet */
Marton Nemeth1408b842009-11-02 08:13:21 -0300792 int len) /* iso packet length */
793{
794 struct sd *sd = (struct sd *) gspca_dev;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300795 u8 *image;
Jean-François Moineae251e62012-02-27 05:15:12 -0300796 u8 *sof;
Marton Nemeth1408b842009-11-02 08:13:21 -0300797
798 sof = pac_find_sof(&sd->sof_read, data, len);
799 if (sof) {
800 int n, lum_offset, footer_length;
801
Hans de Goede895d4642012-04-28 10:31:17 -0300802 /*
803 * 6 bytes after the FF D9 EOF marker a number of lumination
804 * bytes are send corresponding to different parts of the
805 * image, the 14th and 15th byte after the EOF seem to
806 * correspond to the center of the image.
807 */
Marton Nemeth1408b842009-11-02 08:13:21 -0300808 lum_offset = 61 + sizeof pac_sof_marker;
809 footer_length = 74;
810
811 /* Finish decoding current frame */
812 n = (sof - data) - (footer_length + sizeof pac_sof_marker);
813 if (n < 0) {
Jean-François Moineb192ca92010-06-27 03:08:19 -0300814 gspca_dev->image_len += n;
Marton Nemeth1408b842009-11-02 08:13:21 -0300815 n = 0;
Jean-François Moineb192ca92010-06-27 03:08:19 -0300816 } else {
817 gspca_frame_add(gspca_dev, INTER_PACKET, data, n);
Marton Nemeth1408b842009-11-02 08:13:21 -0300818 }
Jean-François Moinef7059ea2010-07-06 04:32:27 -0300819
820 image = gspca_dev->image;
821 if (image != NULL
Jean-François Moineb192ca92010-06-27 03:08:19 -0300822 && image[gspca_dev->image_len - 2] == 0xff
823 && image[gspca_dev->image_len - 1] == 0xd9)
824 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
Marton Nemeth1408b842009-11-02 08:13:21 -0300825
826 n = sof - data;
827 len -= n;
828 data = sof;
829
830 /* Get average lumination */
831 if (gspca_dev->last_packet_type == LAST_PACKET &&
832 n >= lum_offset)
833 atomic_set(&sd->avg_lum, data[-lum_offset] +
834 data[-lum_offset + 1]);
Marton Nemeth1408b842009-11-02 08:13:21 -0300835
836 /* Start the new frame with the jpeg header */
837 /* The PAC7302 has the image rotated 90 degrees */
Jean-François Moine7532e812012-02-27 05:21:57 -0300838 gspca_frame_add(gspca_dev, FIRST_PACKET,
839 jpeg_header, sizeof jpeg_header);
Marton Nemeth1408b842009-11-02 08:13:21 -0300840 }
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300841 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
Marton Nemeth1408b842009-11-02 08:13:21 -0300842}
843
Márton Németh6763cc02009-11-09 07:10:46 -0300844#ifdef CONFIG_VIDEO_ADV_DEBUG
845static int sd_dbg_s_register(struct gspca_dev *gspca_dev,
846 struct v4l2_dbg_register *reg)
847{
Jean-François Moineae251e62012-02-27 05:15:12 -0300848 u8 index;
849 u8 value;
Márton Németh6763cc02009-11-09 07:10:46 -0300850
Hans de Goede895d4642012-04-28 10:31:17 -0300851 /*
852 * reg->reg: bit0..15: reserved for register index (wIndex is 16bit
853 * long on the USB bus)
854 */
Márton Németh6763cc02009-11-09 07:10:46 -0300855 if (reg->match.type == V4L2_CHIP_MATCH_HOST &&
856 reg->match.addr == 0 &&
857 (reg->reg < 0x000000ff) &&
858 (reg->val <= 0x000000ff)
859 ) {
860 /* Currently writing to page 0 is only supported. */
861 /* reg_w() only supports 8bit index */
Jean-François Moineae251e62012-02-27 05:15:12 -0300862 index = reg->reg;
863 value = reg->val;
Márton Németh6763cc02009-11-09 07:10:46 -0300864
Hans de Goede895d4642012-04-28 10:31:17 -0300865 /*
866 * Note that there shall be no access to other page
867 * by any other function between the page switch and
868 * the actual register write.
869 */
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300870 reg_w(gspca_dev, 0xff, 0x00); /* page 0 */
871 reg_w(gspca_dev, index, value);
Márton Németh6763cc02009-11-09 07:10:46 -0300872
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300873 reg_w(gspca_dev, 0xdc, 0x01);
Márton Németh6763cc02009-11-09 07:10:46 -0300874 }
Jean-Francois Moinebe927be2010-01-13 15:09:14 -0300875 return gspca_dev->usb_err;
Márton Németh6763cc02009-11-09 07:10:46 -0300876}
877
878static int sd_chip_ident(struct gspca_dev *gspca_dev,
879 struct v4l2_dbg_chip_ident *chip)
880{
881 int ret = -EINVAL;
882
883 if (chip->match.type == V4L2_CHIP_MATCH_HOST &&
884 chip->match.addr == 0) {
885 chip->revision = 0;
886 chip->ident = V4L2_IDENT_UNKNOWN;
887 ret = 0;
888 }
889 return ret;
890}
891#endif
892
Jean-François Moine28566432010-10-01 07:33:26 -0300893#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Némethaed6f1b2010-01-28 16:33:38 -0300894static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
895 u8 *data, /* interrupt packet data */
896 int len) /* interrput packet length */
897{
898 int ret = -EINVAL;
899 u8 data0, data1;
900
901 if (len == 2) {
902 data0 = data[0];
903 data1 = data[1];
904 if ((data0 == 0x00 && data1 == 0x11) ||
905 (data0 == 0x22 && data1 == 0x33) ||
906 (data0 == 0x44 && data1 == 0x55) ||
907 (data0 == 0x66 && data1 == 0x77) ||
908 (data0 == 0x88 && data1 == 0x99) ||
909 (data0 == 0xaa && data1 == 0xbb) ||
910 (data0 == 0xcc && data1 == 0xdd) ||
911 (data0 == 0xee && data1 == 0xff)) {
912 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
913 input_sync(gspca_dev->input_dev);
914 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
915 input_sync(gspca_dev->input_dev);
916 ret = 0;
917 }
918 }
919
920 return ret;
921}
922#endif
923
Marton Nemeth1408b842009-11-02 08:13:21 -0300924/* sub-driver description for pac7302 */
Márton Némethaabcdfb2010-01-05 12:39:02 -0300925static const struct sd_desc sd_desc = {
Jean-François Moineae251e62012-02-27 05:15:12 -0300926 .name = KBUILD_MODNAME,
Marton Nemeth1408b842009-11-02 08:13:21 -0300927 .config = sd_config,
928 .init = sd_init,
Hans de Goede74233cd2012-05-14 11:16:09 -0300929 .init_controls = sd_init_controls,
Marton Nemeth1408b842009-11-02 08:13:21 -0300930 .start = sd_start,
931 .stopN = sd_stopN,
932 .stop0 = sd_stop0,
933 .pkt_scan = sd_pkt_scan,
934 .dq_callback = do_autogain,
Márton Németh6763cc02009-11-09 07:10:46 -0300935#ifdef CONFIG_VIDEO_ADV_DEBUG
936 .set_register = sd_dbg_s_register,
937 .get_chip_ident = sd_chip_ident,
938#endif
Jean-François Moine28566432010-10-01 07:33:26 -0300939#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Márton Némethaed6f1b2010-01-28 16:33:38 -0300940 .int_pkt_scan = sd_int_pkt_scan,
941#endif
Marton Nemeth1408b842009-11-02 08:13:21 -0300942};
943
944/* -- module initialisation -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300945static const struct usb_device_id device_table[] = {
Marton Nemeth1408b842009-11-02 08:13:21 -0300946 {USB_DEVICE(0x06f8, 0x3009)},
Jean-François Moinedd32f982012-02-27 04:58:59 -0300947 {USB_DEVICE(0x06f8, 0x301b)},
Marton Nemeth1408b842009-11-02 08:13:21 -0300948 {USB_DEVICE(0x093a, 0x2620)},
949 {USB_DEVICE(0x093a, 0x2621)},
Jean-Francois Moinefe2b60322009-11-26 14:28:48 -0300950 {USB_DEVICE(0x093a, 0x2622), .driver_info = FL_VFLIP},
951 {USB_DEVICE(0x093a, 0x2624), .driver_info = FL_VFLIP},
Márton Németh4e6aeef2010-06-14 17:21:37 -0300952 {USB_DEVICE(0x093a, 0x2625)},
Marton Nemeth1408b842009-11-02 08:13:21 -0300953 {USB_DEVICE(0x093a, 0x2626)},
Jozsef Marton5b843252012-05-15 12:05:36 -0300954 {USB_DEVICE(0x093a, 0x2627), .driver_info = FL_VFLIP},
Marton Nemeth1408b842009-11-02 08:13:21 -0300955 {USB_DEVICE(0x093a, 0x2628)},
Jean-Francois Moinec4322bf2009-12-02 07:04:35 -0300956 {USB_DEVICE(0x093a, 0x2629), .driver_info = FL_VFLIP},
Marton Nemeth1408b842009-11-02 08:13:21 -0300957 {USB_DEVICE(0x093a, 0x262a)},
958 {USB_DEVICE(0x093a, 0x262c)},
Hans de Goede4d6454d2011-12-30 19:15:53 -0300959 {USB_DEVICE(0x145f, 0x013c)},
Frank Schäfer97d2fbf2012-09-09 15:02:19 -0300960 {USB_DEVICE(0x1ae7, 0x2001)}, /* SpeedLink Snappy Mic SL-6825-SBK */
Marton Nemeth1408b842009-11-02 08:13:21 -0300961 {}
962};
963MODULE_DEVICE_TABLE(usb, device_table);
964
965/* -- device connect -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300966static int sd_probe(struct usb_interface *intf,
Marton Nemeth1408b842009-11-02 08:13:21 -0300967 const struct usb_device_id *id)
968{
969 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
970 THIS_MODULE);
971}
972
973static struct usb_driver sd_driver = {
Jean-François Moineae251e62012-02-27 05:15:12 -0300974 .name = KBUILD_MODNAME,
Marton Nemeth1408b842009-11-02 08:13:21 -0300975 .id_table = device_table,
976 .probe = sd_probe,
977 .disconnect = gspca_disconnect,
978#ifdef CONFIG_PM
979 .suspend = gspca_suspend,
980 .resume = gspca_resume,
Hans de Goede8bb58962012-06-30 06:44:47 -0300981 .reset_resume = gspca_resume,
Marton Nemeth1408b842009-11-02 08:13:21 -0300982#endif
983};
984
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800985module_usb_driver(sd_driver);