blob: 084af05302a0399f7e84e993437557d7433e3a3e [file] [log] [blame]
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001/*
2 * Quickcam cameras initialization data
3 *
4 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#define MODULE_NAME "tv8532"
22
23#include "gspca.h"
24
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030025MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
26MODULE_DESCRIPTION("TV8532 USB Camera Driver");
27MODULE_LICENSE("GPL");
28
29/* specific webcam descriptor */
30struct sd {
31 struct gspca_dev gspca_dev; /* !! must be the first item */
32
33 int buflen; /* current length of tmpbuf */
34 __u8 tmpbuf[352 * 288 + 10 * 288]; /* no protection... */
35 __u8 tmpbuf2[352 * 288]; /* no protection... */
36
37 unsigned short brightness;
38 unsigned short contrast;
39
40 char packet;
41 char synchro;
42};
43
44/* V4L2 controls supported by the driver */
45static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
46static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
47static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
48static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
49
50static struct ctrl sd_ctrls[] = {
51#define SD_BRIGHTNESS 0
52 {
53 {
54 .id = V4L2_CID_BRIGHTNESS,
55 .type = V4L2_CTRL_TYPE_INTEGER,
56 .name = "Brightness",
57 .minimum = 1,
58 .maximum = 0x2ff,
59 .step = 1,
60 .default_value = 0x18f,
61 },
62 .set = sd_setbrightness,
63 .get = sd_getbrightness,
64 },
65#define SD_CONTRAST 1
66 {
67 {
68 .id = V4L2_CID_CONTRAST,
69 .type = V4L2_CTRL_TYPE_INTEGER,
70 .name = "Contrast",
71 .minimum = 0,
72 .maximum = 0xffff,
73 .step = 1,
74 .default_value = 0x7fff,
75 },
76 .set = sd_setcontrast,
77 .get = sd_getcontrast,
78 },
79};
80
Jean-Francois Moinec2446b32008-07-05 11:49:20 -030081static struct v4l2_pix_format sif_mode[] = {
82 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
83 .bytesperline = 176,
84 .sizeimage = 176 * 144,
85 .colorspace = V4L2_COLORSPACE_SRGB,
86 .priv = 1},
87 {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
88 .bytesperline = 352,
89 .sizeimage = 352 * 288,
90 .colorspace = V4L2_COLORSPACE_SRGB,
91 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030092};
93
94/*
95 * Initialization data: this is the first set-up data written to the
96 * device (before the open data).
97 */
98#define TESTCLK 0x10 /* reg 0x2c -> 0x12 //10 */
99#define TESTCOMP 0x90 /* reg 0x28 -> 0x80 */
100#define TESTLINE 0x81 /* reg 0x29 -> 0x81 */
101#define QCIFLINE 0x41 /* reg 0x29 -> 0x81 */
102#define TESTPTL 0x14 /* reg 0x2D -> 0x14 */
103#define TESTPTH 0x01 /* reg 0x2E -> 0x01 */
104#define TESTPTBL 0x12 /* reg 0x2F -> 0x0a */
105#define TESTPTBH 0x01 /* reg 0x30 -> 0x01 */
106#define ADWIDTHL 0xe8 /* reg 0x0c -> 0xe8 */
107#define ADWIDTHH 0x03 /* reg 0x0d -> 0x03 */
108#define ADHEIGHL 0x90 /* reg 0x0e -> 0x91 //93 */
109#define ADHEIGHH 0x01 /* reg 0x0f -> 0x01 */
110#define EXPOL 0x8f /* reg 0x1c -> 0x8f */
111#define EXPOH 0x01 /* reg 0x1d -> 0x01 */
112#define ADCBEGINL 0x44 /* reg 0x10 -> 0x46 //47 */
113#define ADCBEGINH 0x00 /* reg 0x11 -> 0x00 */
114#define ADRBEGINL 0x0a /* reg 0x14 -> 0x0b //0x0c */
115#define ADRBEGINH 0x00 /* reg 0x15 -> 0x00 */
116#define TV8532_CMD_UPDATE 0x84
117
118#define TV8532_EEprom_Add 0x03
119#define TV8532_EEprom_DataL 0x04
120#define TV8532_EEprom_DataM 0x05
121#define TV8532_EEprom_DataH 0x06
122#define TV8532_EEprom_TableLength 0x07
123#define TV8532_EEprom_Write 0x08
124#define TV8532_PART_CTRL 0x00
125#define TV8532_CTRL 0x01
126#define TV8532_CMD_EEprom_Open 0x30
127#define TV8532_CMD_EEprom_Close 0x29
128#define TV8532_UDP_UPDATE 0x31
129#define TV8532_GPIO 0x39
130#define TV8532_GPIO_OE 0x3B
131#define TV8532_REQ_RegWrite 0x02
132#define TV8532_REQ_RegRead 0x03
133
134#define TV8532_ADWIDTH_L 0x0C
135#define TV8532_ADWIDTH_H 0x0D
136#define TV8532_ADHEIGHT_L 0x0E
137#define TV8532_ADHEIGHT_H 0x0F
138#define TV8532_EXPOSURE 0x1C
139#define TV8532_QUANT_COMP 0x28
140#define TV8532_MODE_PACKET 0x29
141#define TV8532_SETCLK 0x2C
142#define TV8532_POINT_L 0x2D
143#define TV8532_POINT_H 0x2E
144#define TV8532_POINTB_L 0x2F
145#define TV8532_POINTB_H 0x30
146#define TV8532_BUDGET_L 0x2A
147#define TV8532_BUDGET_H 0x2B
148#define TV8532_VID_L 0x34
149#define TV8532_VID_H 0x35
150#define TV8532_PID_L 0x36
151#define TV8532_PID_H 0x37
152#define TV8532_DeviceID 0x83
153#define TV8532_AD_SLOPE 0x91
154#define TV8532_AD_BITCTRL 0x94
155#define TV8532_AD_COLBEGIN_L 0x10
156#define TV8532_AD_COLBEGIN_H 0x11
157#define TV8532_AD_ROWBEGIN_L 0x14
158#define TV8532_AD_ROWBEGIN_H 0x15
159
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300160static const __u32 tv_8532_eeprom_data[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300161/* add dataL dataM dataH */
162 0x00010001, 0x01018011, 0x02050014, 0x0305001c,
163 0x040d001e, 0x0505001f, 0x06050519, 0x0705011b,
164 0x0805091e, 0x090d892e, 0x0a05892f, 0x0b050dd9,
165 0x0c0509f1, 0
166};
167
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300168static int reg_r(struct gspca_dev *gspca_dev,
169 __u16 index)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300170{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300171 usb_control_msg(gspca_dev->dev,
172 usb_rcvctrlpipe(gspca_dev->dev, 0),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300173 TV8532_REQ_RegRead,
174 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
175 0, /* value */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300176 index, gspca_dev->usb_buf, 1,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300177 500);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300178 return gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300179}
180
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300181/* write 1 byte */
182static void reg_w_1(struct gspca_dev *gspca_dev,
183 __u16 index, __u8 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300184{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300185 gspca_dev->usb_buf[0] = value;
186 usb_control_msg(gspca_dev->dev,
187 usb_sndctrlpipe(gspca_dev->dev, 0),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300188 TV8532_REQ_RegWrite,
189 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
190 0, /* value */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300191 index, gspca_dev->usb_buf, 1, 500);
192}
193
194/* write 2 bytes */
195static void reg_w_2(struct gspca_dev *gspca_dev,
196 __u16 index, __u8 val1, __u8 val2)
197{
198 gspca_dev->usb_buf[0] = val1;
199 gspca_dev->usb_buf[1] = val2;
200 usb_control_msg(gspca_dev->dev,
201 usb_sndctrlpipe(gspca_dev->dev, 0),
202 TV8532_REQ_RegWrite,
203 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
204 0, /* value */
205 index, gspca_dev->usb_buf, 2, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300206}
207
208static void tv_8532WriteEEprom(struct gspca_dev *gspca_dev)
209{
210 int i = 0;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300211 __u8 reg, data0, data1, data2;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300212
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300213 reg_w_1(gspca_dev, TV8532_GPIO, 0xb0);
214 reg_w_1(gspca_dev, TV8532_CTRL, TV8532_CMD_EEprom_Open);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300215/* msleep(1); */
216 while (tv_8532_eeprom_data[i]) {
217 reg = (tv_8532_eeprom_data[i] & 0xff000000) >> 24;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300218 reg_w_1(gspca_dev, TV8532_EEprom_Add, reg);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300219 /* msleep(1); */
220 data0 = (tv_8532_eeprom_data[i] & 0x000000ff);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300221 reg_w_1(gspca_dev, TV8532_EEprom_DataL, data0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300222 /* msleep(1); */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300223 data1 = (tv_8532_eeprom_data[i] & 0x0000ff00) >> 8;
224 reg_w_1(gspca_dev, TV8532_EEprom_DataM, data1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300225 /* msleep(1); */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300226 data2 = (tv_8532_eeprom_data[i] & 0x00ff0000) >> 16;
227 reg_w_1(gspca_dev, TV8532_EEprom_DataH, data2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300228 /* msleep(1); */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300229 reg_w_1(gspca_dev, TV8532_EEprom_Write, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300230 /* msleep(10); */
231 i++;
232 }
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300233 reg_w_1(gspca_dev, TV8532_EEprom_TableLength, i);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300234/* msleep(1); */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300235 reg_w_1(gspca_dev, TV8532_CTRL, TV8532_CMD_EEprom_Close);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300236 msleep(10);
237}
238
239/* this function is called at probe time */
240static int sd_config(struct gspca_dev *gspca_dev,
241 const struct usb_device_id *id)
242{
243 struct sd *sd = (struct sd *) gspca_dev;
244 struct cam *cam;
245
246 tv_8532WriteEEprom(gspca_dev);
247
248 cam = &gspca_dev->cam;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300249 cam->epaddr = 1;
250 cam->cam_mode = sif_mode;
251 cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
252
253 sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
254 sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
255 return 0;
256}
257
258static void tv_8532ReadRegisters(struct gspca_dev *gspca_dev)
259{
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300260 __u8 data;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300261
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300262 data = reg_r(gspca_dev, 0x0001);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300263 PDEBUG(D_USBI, "register 0x01-> %x", data);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300264 data = reg_r(gspca_dev, 0x0002);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300265 PDEBUG(D_USBI, "register 0x02-> %x", data);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300266 reg_r(gspca_dev, TV8532_ADWIDTH_L);
267 reg_r(gspca_dev, TV8532_ADWIDTH_H);
268 reg_r(gspca_dev, TV8532_QUANT_COMP);
269 reg_r(gspca_dev, TV8532_MODE_PACKET);
270 reg_r(gspca_dev, TV8532_SETCLK);
271 reg_r(gspca_dev, TV8532_POINT_L);
272 reg_r(gspca_dev, TV8532_POINT_H);
273 reg_r(gspca_dev, TV8532_POINTB_L);
274 reg_r(gspca_dev, TV8532_POINTB_H);
275 reg_r(gspca_dev, TV8532_BUDGET_L);
276 reg_r(gspca_dev, TV8532_BUDGET_H);
277 reg_r(gspca_dev, TV8532_VID_L);
278 reg_r(gspca_dev, TV8532_VID_H);
279 reg_r(gspca_dev, TV8532_PID_L);
280 reg_r(gspca_dev, TV8532_PID_H);
281 reg_r(gspca_dev, TV8532_DeviceID);
282 reg_r(gspca_dev, TV8532_AD_COLBEGIN_L);
283 reg_r(gspca_dev, TV8532_AD_COLBEGIN_H);
284 reg_r(gspca_dev, TV8532_AD_ROWBEGIN_L);
285 reg_r(gspca_dev, TV8532_AD_ROWBEGIN_H);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300286}
287
288static void tv_8532_setReg(struct gspca_dev *gspca_dev)
289{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300290 reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_L,
291 ADCBEGINL); /* 0x10 */
292 reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_H,
293 ADCBEGINH); /* also digital gain */
294 reg_w_1(gspca_dev, TV8532_PART_CTRL,
295 TV8532_CMD_UPDATE); /* 0x00<-0x84 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300296
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300297 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0a);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300298 /******************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300299 reg_w_1(gspca_dev, TV8532_ADHEIGHT_L, ADHEIGHL); /* 0e */
300 reg_w_1(gspca_dev, TV8532_ADHEIGHT_H, ADHEIGHH); /* 0f */
301 reg_w_2(gspca_dev, TV8532_EXPOSURE,
302 EXPOL, EXPOH); /* 350d 0x014c; 1c */
303 reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_L,
304 ADCBEGINL); /* 0x10 */
305 reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_H,
306 ADCBEGINH); /* also digital gain */
307 reg_w_1(gspca_dev, TV8532_AD_ROWBEGIN_L,
308 ADRBEGINL); /* 0x14 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300309
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300310 reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x00); /* 0x91 */
311 reg_w_1(gspca_dev, TV8532_AD_BITCTRL, 0x02); /* 0x94 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300312
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300313 reg_w_1(gspca_dev, TV8532_CTRL,
314 TV8532_CMD_EEprom_Close); /* 0x01 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300315
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300316 reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x00); /* 0x91 */
317 reg_w_1(gspca_dev, TV8532_PART_CTRL,
318 TV8532_CMD_UPDATE); /* 0x00<-0x84 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300319}
320
321static void tv_8532_PollReg(struct gspca_dev *gspca_dev)
322{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300323 int i;
324
325 /* strange polling from tgc */
326 for (i = 0; i < 10; i++) {
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300327 reg_w_1(gspca_dev, TV8532_SETCLK,
328 TESTCLK); /* 0x48; //0x08; 0x2c */
329 reg_w_1(gspca_dev, TV8532_PART_CTRL, TV8532_CMD_UPDATE);
330 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x01); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300331 }
332}
333
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300334/* this function is called at probe and resume time */
335static int sd_init(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300336{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300337 reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x32);
338 reg_w_1(gspca_dev, TV8532_AD_BITCTRL, 0x00);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300339 tv_8532ReadRegisters(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300340 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
341 reg_w_2(gspca_dev, TV8532_ADHEIGHT_L, ADHEIGHL,
342 ADHEIGHH); /* 401d 0x0169; 0e */
343 reg_w_2(gspca_dev, TV8532_EXPOSURE, EXPOL,
344 EXPOH); /* 350d 0x014c; 1c */
345 reg_w_1(gspca_dev, TV8532_ADWIDTH_L, ADWIDTHL); /* 0x20; 0x0c */
346 reg_w_1(gspca_dev, TV8532_ADWIDTH_H, ADWIDTHH); /* 0x0d */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300347
348 /*******************************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300349 reg_w_1(gspca_dev, TV8532_QUANT_COMP,
350 TESTCOMP); /* 0x72 compressed mode 0x28 */
351 reg_w_1(gspca_dev, TV8532_MODE_PACKET,
352 TESTLINE); /* 0x84; // CIF | 4 packet 0x29 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300353
354 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300355 reg_w_1(gspca_dev, TV8532_SETCLK,
356 TESTCLK); /* 0x48; //0x08; 0x2c */
357 reg_w_1(gspca_dev, TV8532_POINT_L,
358 TESTPTL); /* 0x38; 0x2d */
359 reg_w_1(gspca_dev, TV8532_POINT_H,
360 TESTPTH); /* 0x04; 0x2e */
361 reg_w_1(gspca_dev, TV8532_POINTB_L,
362 TESTPTBL); /* 0x04; 0x2f */
363 reg_w_1(gspca_dev, TV8532_POINTB_H,
364 TESTPTBH); /* 0x04; 0x30 */
365 reg_w_1(gspca_dev, TV8532_PART_CTRL,
366 TV8532_CMD_UPDATE); /* 0x00<-0x84 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300367 /*************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300368 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x01); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300369 msleep(200);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300370 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x00); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300371 /*************************************************/
372 tv_8532_setReg(gspca_dev);
373 /*************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300374 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300375 /*************************************************/
376 tv_8532_setReg(gspca_dev);
377 /*************************************************/
378 tv_8532_PollReg(gspca_dev);
379 return 0;
380}
381
382static void setbrightness(struct gspca_dev *gspca_dev)
383{
384 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300385 int brightness = sd->brightness;
386
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300387 reg_w_2(gspca_dev, TV8532_EXPOSURE,
388 brightness >> 8, brightness); /* 1c */
389 reg_w_1(gspca_dev, TV8532_PART_CTRL, TV8532_CMD_UPDATE);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300390}
391
392/* -- start the camera -- */
393static void sd_start(struct gspca_dev *gspca_dev)
394{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300395 reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x32);
396 reg_w_1(gspca_dev, TV8532_AD_BITCTRL, 0x00);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300397 tv_8532ReadRegisters(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300398 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
399 reg_w_2(gspca_dev, TV8532_ADHEIGHT_L,
400 ADHEIGHL, ADHEIGHH); /* 401d 0x0169; 0e */
401/* reg_w_2(gspca_dev, TV8532_EXPOSURE,
402 EXPOL, EXPOH); * 350d 0x014c; 1c */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300403 setbrightness(gspca_dev);
404
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300405 reg_w_1(gspca_dev, TV8532_ADWIDTH_L, ADWIDTHL); /* 0x20; 0x0c */
406 reg_w_1(gspca_dev, TV8532_ADWIDTH_H, ADWIDTHH); /* 0x0d */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300407
408 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300409 reg_w_1(gspca_dev, TV8532_QUANT_COMP,
410 TESTCOMP); /* 0x72 compressed mode 0x28 */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300411 if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300412 /* 176x144 */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300413 reg_w_1(gspca_dev, TV8532_MODE_PACKET,
414 QCIFLINE); /* 0x84; // CIF | 4 packet 0x29 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300415 } else {
416 /* 352x288 */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300417 reg_w_1(gspca_dev, TV8532_MODE_PACKET,
418 TESTLINE); /* 0x84; // CIF | 4 packet 0x29 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300419 }
420 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300421 reg_w_1(gspca_dev, TV8532_SETCLK,
422 TESTCLK); /* 0x48; //0x08; 0x2c */
423 reg_w_1(gspca_dev, TV8532_POINT_L,
424 TESTPTL); /* 0x38; 0x2d */
425 reg_w_1(gspca_dev, TV8532_POINT_H,
426 TESTPTH); /* 0x04; 0x2e */
427 reg_w_1(gspca_dev, TV8532_POINTB_L,
428 TESTPTBL); /* 0x04; 0x2f */
429 reg_w_1(gspca_dev, TV8532_POINTB_H,
430 TESTPTBH); /* 0x04; 0x30 */
431 reg_w_1(gspca_dev, TV8532_PART_CTRL,
432 TV8532_CMD_UPDATE); /* 0x00<-0x84 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300433 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300434 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x01); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300435 msleep(200);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300436 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x00); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300437 /************************************************/
438 tv_8532_setReg(gspca_dev);
439 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300440 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300441 /************************************************/
442 tv_8532_setReg(gspca_dev);
443 /************************************************/
444 tv_8532_PollReg(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300445 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x00); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300446}
447
448static void sd_stopN(struct gspca_dev *gspca_dev)
449{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300450 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300451}
452
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300453static void tv8532_preprocess(struct gspca_dev *gspca_dev)
454{
455 struct sd *sd = (struct sd *) gspca_dev;
456/* we should received a whole frame with header and EOL marker
457 * in gspca_dev->tmpbuf and return a GBRG pattern in gspca_dev->tmpbuf2
458 * sequence 2bytes header the Alternate pixels bayer GB 4 bytes
459 * Alternate pixels bayer RG 4 bytes EOL */
460 int width = gspca_dev->width;
461 int height = gspca_dev->height;
462 unsigned char *dst = sd->tmpbuf2;
463 unsigned char *data = sd->tmpbuf;
464 int i;
465
466 /* precompute where is the good bayer line */
467 if (((data[3] + data[width + 7]) >> 1)
468 + (data[4] >> 2)
469 + (data[width + 6] >> 1) >= ((data[2] + data[width + 6]) >> 1)
470 + (data[3] >> 2)
471 + (data[width + 5] >> 1))
472 data += 3;
473 else
474 data += 2;
475 for (i = 0; i < height / 2; i++) {
476 memcpy(dst, data, width);
477 data += width + 3;
478 dst += width;
479 memcpy(dst, data, width);
480 data += width + 7;
481 dst += width;
482 }
483}
484
485static void sd_pkt_scan(struct gspca_dev *gspca_dev,
486 struct gspca_frame *frame, /* target */
487 __u8 *data, /* isoc packet */
488 int len) /* iso packet length */
489{
490 struct sd *sd = (struct sd *) gspca_dev;
491
492 if (data[0] != 0x80) {
493 sd->packet++;
494 if (sd->buflen + len > sizeof sd->tmpbuf) {
495 if (gspca_dev->last_packet_type != DISCARD_PACKET) {
496 PDEBUG(D_PACK, "buffer overflow");
497 gspca_dev->last_packet_type = DISCARD_PACKET;
498 }
499 return;
500 }
501 memcpy(&sd->tmpbuf[sd->buflen], data, len);
502 sd->buflen += len;
503 return;
504 }
505
506 /* here we detect 0x80 */
507 /* counter is limited so we need few header for a frame :) */
508
509 /* header 0x80 0x80 0x80 0x80 0x80 */
510 /* packet 00 63 127 145 00 */
511 /* sof 0 1 1 0 0 */
512
513 /* update sequence */
514 if (sd->packet == 63 || sd->packet == 127)
515 sd->synchro = 1;
516
517 /* is there a frame start ? */
518 if (sd->packet >= (gspca_dev->height >> 1) - 1) {
519 PDEBUG(D_PACK, "SOF > %d packet %d", sd->synchro,
520 sd->packet);
521 if (!sd->synchro) { /* start of frame */
522 if (gspca_dev->last_packet_type == FIRST_PACKET) {
523 tv8532_preprocess(gspca_dev);
524 frame = gspca_frame_add(gspca_dev,
525 LAST_PACKET,
526 frame, sd->tmpbuf2,
527 gspca_dev->width *
528 gspca_dev->width);
529 }
530 gspca_frame_add(gspca_dev, FIRST_PACKET,
531 frame, data, 0);
532 memcpy(sd->tmpbuf, data, len);
533 sd->buflen = len;
534 sd->packet = 0;
535 return;
536 }
537 if (gspca_dev->last_packet_type != DISCARD_PACKET) {
538 PDEBUG(D_PACK,
539 "Warning wrong TV8532 frame detection %d",
540 sd->packet);
541 gspca_dev->last_packet_type = DISCARD_PACKET;
542 }
543 return;
544 }
545
546 if (!sd->synchro) {
547 /* Drop packet frame corrupt */
548 PDEBUG(D_PACK, "DROP SOF %d packet %d",
549 sd->synchro, sd->packet);
550 sd->packet = 0;
551 gspca_dev->last_packet_type = DISCARD_PACKET;
552 return;
553 }
554 sd->synchro = 1;
555 sd->packet++;
556 memcpy(&sd->tmpbuf[sd->buflen], data, len);
557 sd->buflen += len;
558}
559
560static void setcontrast(struct gspca_dev *gspca_dev)
561{
562}
563
564static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
565{
566 struct sd *sd = (struct sd *) gspca_dev;
567
568 sd->brightness = val;
569 if (gspca_dev->streaming)
570 setbrightness(gspca_dev);
571 return 0;
572}
573
574static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
575{
576 struct sd *sd = (struct sd *) gspca_dev;
577
578 *val = sd->brightness;
579 return 0;
580}
581
582static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
583{
584 struct sd *sd = (struct sd *) gspca_dev;
585
586 sd->contrast = val;
587 if (gspca_dev->streaming)
588 setcontrast(gspca_dev);
589 return 0;
590}
591
592static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
593{
594 struct sd *sd = (struct sd *) gspca_dev;
595
596 *val = sd->contrast;
597 return 0;
598}
599
600/* sub-driver description */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300601static const struct sd_desc sd_desc = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300602 .name = MODULE_NAME,
603 .ctrls = sd_ctrls,
604 .nctrls = ARRAY_SIZE(sd_ctrls),
605 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300606 .init = sd_init,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300607 .start = sd_start,
608 .stopN = sd_stopN,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300609 .pkt_scan = sd_pkt_scan,
610};
611
612/* -- module initialisation -- */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300613static const __devinitdata struct usb_device_id device_table[] = {
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300614 {USB_DEVICE(0x046d, 0x0920)},
615 {USB_DEVICE(0x046d, 0x0921)},
616 {USB_DEVICE(0x0545, 0x808b)},
617 {USB_DEVICE(0x0545, 0x8333)},
618 {USB_DEVICE(0x0923, 0x010f)},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300619 {}
620};
621
622MODULE_DEVICE_TABLE(usb, device_table);
623
624/* -- device connect -- */
625static int sd_probe(struct usb_interface *intf,
626 const struct usb_device_id *id)
627{
628 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
629 THIS_MODULE);
630}
631
632static struct usb_driver sd_driver = {
633 .name = MODULE_NAME,
634 .id_table = device_table,
635 .probe = sd_probe,
636 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300637#ifdef CONFIG_PM
638 .suspend = gspca_suspend,
639 .resume = gspca_resume,
640#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300641};
642
643/* -- module insert / remove -- */
644static int __init sd_mod_init(void)
645{
646 if (usb_register(&sd_driver) < 0)
647 return -1;
Jean-Francois Moine10b0e962008-07-22 05:35:10 -0300648 PDEBUG(D_PROBE, "registered");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300649 return 0;
650}
651
652static void __exit sd_mod_exit(void)
653{
654 usb_deregister(&sd_driver);
655 PDEBUG(D_PROBE, "deregistered");
656}
657
658module_init(sd_mod_init);
659module_exit(sd_mod_exit);