blob: f6a4733d0262f939f735ff6a52ac1e44e04654e4 [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
Jean-Francois Moineba13cca2008-10-24 04:19:29 -030033 __u32 buflen; /* current length of tmpbuf */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030034 __u8 tmpbuf[352 * 288 + 10 * 288]; /* no protection... */
35 __u8 tmpbuf2[352 * 288]; /* no protection... */
36
Jean-Francois Moineba13cca2008-10-24 04:19:29 -030037 __u16 brightness;
38 __u16 contrast;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030039
Jean-Francois Moineba13cca2008-10-24 04:19:29 -030040 __u8 packet;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030041};
42
43/* V4L2 controls supported by the driver */
44static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
45static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
46static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
47static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
48
49static struct ctrl sd_ctrls[] = {
50#define SD_BRIGHTNESS 0
51 {
52 {
53 .id = V4L2_CID_BRIGHTNESS,
54 .type = V4L2_CTRL_TYPE_INTEGER,
55 .name = "Brightness",
56 .minimum = 1,
57 .maximum = 0x2ff,
58 .step = 1,
59 .default_value = 0x18f,
60 },
61 .set = sd_setbrightness,
62 .get = sd_getbrightness,
63 },
64#define SD_CONTRAST 1
65 {
66 {
67 .id = V4L2_CID_CONTRAST,
68 .type = V4L2_CTRL_TYPE_INTEGER,
69 .name = "Contrast",
70 .minimum = 0,
71 .maximum = 0xffff,
72 .step = 1,
73 .default_value = 0x7fff,
74 },
75 .set = sd_setcontrast,
76 .get = sd_getcontrast,
77 },
78};
79
Jean-Francois Moinec2446b32008-07-05 11:49:20 -030080static struct v4l2_pix_format sif_mode[] = {
81 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
82 .bytesperline = 176,
83 .sizeimage = 176 * 144,
84 .colorspace = V4L2_COLORSPACE_SRGB,
85 .priv = 1},
86 {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
87 .bytesperline = 352,
88 .sizeimage = 352 * 288,
89 .colorspace = V4L2_COLORSPACE_SRGB,
90 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030091};
92
93/*
94 * Initialization data: this is the first set-up data written to the
95 * device (before the open data).
96 */
97#define TESTCLK 0x10 /* reg 0x2c -> 0x12 //10 */
98#define TESTCOMP 0x90 /* reg 0x28 -> 0x80 */
99#define TESTLINE 0x81 /* reg 0x29 -> 0x81 */
100#define QCIFLINE 0x41 /* reg 0x29 -> 0x81 */
101#define TESTPTL 0x14 /* reg 0x2D -> 0x14 */
102#define TESTPTH 0x01 /* reg 0x2E -> 0x01 */
103#define TESTPTBL 0x12 /* reg 0x2F -> 0x0a */
104#define TESTPTBH 0x01 /* reg 0x30 -> 0x01 */
105#define ADWIDTHL 0xe8 /* reg 0x0c -> 0xe8 */
106#define ADWIDTHH 0x03 /* reg 0x0d -> 0x03 */
107#define ADHEIGHL 0x90 /* reg 0x0e -> 0x91 //93 */
108#define ADHEIGHH 0x01 /* reg 0x0f -> 0x01 */
109#define EXPOL 0x8f /* reg 0x1c -> 0x8f */
110#define EXPOH 0x01 /* reg 0x1d -> 0x01 */
111#define ADCBEGINL 0x44 /* reg 0x10 -> 0x46 //47 */
112#define ADCBEGINH 0x00 /* reg 0x11 -> 0x00 */
113#define ADRBEGINL 0x0a /* reg 0x14 -> 0x0b //0x0c */
114#define ADRBEGINH 0x00 /* reg 0x15 -> 0x00 */
115#define TV8532_CMD_UPDATE 0x84
116
117#define TV8532_EEprom_Add 0x03
118#define TV8532_EEprom_DataL 0x04
119#define TV8532_EEprom_DataM 0x05
120#define TV8532_EEprom_DataH 0x06
121#define TV8532_EEprom_TableLength 0x07
122#define TV8532_EEprom_Write 0x08
123#define TV8532_PART_CTRL 0x00
124#define TV8532_CTRL 0x01
125#define TV8532_CMD_EEprom_Open 0x30
126#define TV8532_CMD_EEprom_Close 0x29
127#define TV8532_UDP_UPDATE 0x31
128#define TV8532_GPIO 0x39
129#define TV8532_GPIO_OE 0x3B
130#define TV8532_REQ_RegWrite 0x02
131#define TV8532_REQ_RegRead 0x03
132
133#define TV8532_ADWIDTH_L 0x0C
134#define TV8532_ADWIDTH_H 0x0D
135#define TV8532_ADHEIGHT_L 0x0E
136#define TV8532_ADHEIGHT_H 0x0F
137#define TV8532_EXPOSURE 0x1C
138#define TV8532_QUANT_COMP 0x28
139#define TV8532_MODE_PACKET 0x29
140#define TV8532_SETCLK 0x2C
141#define TV8532_POINT_L 0x2D
142#define TV8532_POINT_H 0x2E
143#define TV8532_POINTB_L 0x2F
144#define TV8532_POINTB_H 0x30
145#define TV8532_BUDGET_L 0x2A
146#define TV8532_BUDGET_H 0x2B
147#define TV8532_VID_L 0x34
148#define TV8532_VID_H 0x35
149#define TV8532_PID_L 0x36
150#define TV8532_PID_H 0x37
151#define TV8532_DeviceID 0x83
152#define TV8532_AD_SLOPE 0x91
153#define TV8532_AD_BITCTRL 0x94
154#define TV8532_AD_COLBEGIN_L 0x10
155#define TV8532_AD_COLBEGIN_H 0x11
156#define TV8532_AD_ROWBEGIN_L 0x14
157#define TV8532_AD_ROWBEGIN_H 0x15
158
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300159static const __u32 tv_8532_eeprom_data[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300160/* add dataL dataM dataH */
161 0x00010001, 0x01018011, 0x02050014, 0x0305001c,
162 0x040d001e, 0x0505001f, 0x06050519, 0x0705011b,
163 0x0805091e, 0x090d892e, 0x0a05892f, 0x0b050dd9,
164 0x0c0509f1, 0
165};
166
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300167static int reg_r(struct gspca_dev *gspca_dev,
168 __u16 index)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300169{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300170 usb_control_msg(gspca_dev->dev,
171 usb_rcvctrlpipe(gspca_dev->dev, 0),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300172 TV8532_REQ_RegRead,
173 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
174 0, /* value */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300175 index, gspca_dev->usb_buf, 1,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300176 500);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300177 return gspca_dev->usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300178}
179
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300180/* write 1 byte */
181static void reg_w_1(struct gspca_dev *gspca_dev,
182 __u16 index, __u8 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300183{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300184 gspca_dev->usb_buf[0] = value;
185 usb_control_msg(gspca_dev->dev,
186 usb_sndctrlpipe(gspca_dev->dev, 0),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300187 TV8532_REQ_RegWrite,
188 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
189 0, /* value */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300190 index, gspca_dev->usb_buf, 1, 500);
191}
192
193/* write 2 bytes */
194static void reg_w_2(struct gspca_dev *gspca_dev,
195 __u16 index, __u8 val1, __u8 val2)
196{
197 gspca_dev->usb_buf[0] = val1;
198 gspca_dev->usb_buf[1] = val2;
199 usb_control_msg(gspca_dev->dev,
200 usb_sndctrlpipe(gspca_dev->dev, 0),
201 TV8532_REQ_RegWrite,
202 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
203 0, /* value */
204 index, gspca_dev->usb_buf, 2, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300205}
206
207static void tv_8532WriteEEprom(struct gspca_dev *gspca_dev)
208{
209 int i = 0;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300210 __u8 reg, data0, data1, data2;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300211
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300212 reg_w_1(gspca_dev, TV8532_GPIO, 0xb0);
213 reg_w_1(gspca_dev, TV8532_CTRL, TV8532_CMD_EEprom_Open);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300214/* msleep(1); */
215 while (tv_8532_eeprom_data[i]) {
216 reg = (tv_8532_eeprom_data[i] & 0xff000000) >> 24;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300217 reg_w_1(gspca_dev, TV8532_EEprom_Add, reg);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300218 /* msleep(1); */
219 data0 = (tv_8532_eeprom_data[i] & 0x000000ff);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300220 reg_w_1(gspca_dev, TV8532_EEprom_DataL, data0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300221 /* msleep(1); */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300222 data1 = (tv_8532_eeprom_data[i] & 0x0000ff00) >> 8;
223 reg_w_1(gspca_dev, TV8532_EEprom_DataM, data1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300224 /* msleep(1); */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300225 data2 = (tv_8532_eeprom_data[i] & 0x00ff0000) >> 16;
226 reg_w_1(gspca_dev, TV8532_EEprom_DataH, data2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300227 /* msleep(1); */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300228 reg_w_1(gspca_dev, TV8532_EEprom_Write, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300229 /* msleep(10); */
230 i++;
231 }
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300232 reg_w_1(gspca_dev, TV8532_EEprom_TableLength, i);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300233/* msleep(1); */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300234 reg_w_1(gspca_dev, TV8532_CTRL, TV8532_CMD_EEprom_Close);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300235 msleep(10);
236}
237
238/* this function is called at probe time */
239static int sd_config(struct gspca_dev *gspca_dev,
240 const struct usb_device_id *id)
241{
242 struct sd *sd = (struct sd *) gspca_dev;
243 struct cam *cam;
244
245 tv_8532WriteEEprom(gspca_dev);
246
247 cam = &gspca_dev->cam;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300248 cam->epaddr = 1;
249 cam->cam_mode = sif_mode;
250 cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
251
252 sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
253 sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
254 return 0;
255}
256
257static void tv_8532ReadRegisters(struct gspca_dev *gspca_dev)
258{
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300259 __u8 data;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300260
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300261 data = reg_r(gspca_dev, 0x0001);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300262 PDEBUG(D_USBI, "register 0x01-> %x", data);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300263 data = reg_r(gspca_dev, 0x0002);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300264 PDEBUG(D_USBI, "register 0x02-> %x", data);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300265 reg_r(gspca_dev, TV8532_ADWIDTH_L);
266 reg_r(gspca_dev, TV8532_ADWIDTH_H);
267 reg_r(gspca_dev, TV8532_QUANT_COMP);
268 reg_r(gspca_dev, TV8532_MODE_PACKET);
269 reg_r(gspca_dev, TV8532_SETCLK);
270 reg_r(gspca_dev, TV8532_POINT_L);
271 reg_r(gspca_dev, TV8532_POINT_H);
272 reg_r(gspca_dev, TV8532_POINTB_L);
273 reg_r(gspca_dev, TV8532_POINTB_H);
274 reg_r(gspca_dev, TV8532_BUDGET_L);
275 reg_r(gspca_dev, TV8532_BUDGET_H);
276 reg_r(gspca_dev, TV8532_VID_L);
277 reg_r(gspca_dev, TV8532_VID_H);
278 reg_r(gspca_dev, TV8532_PID_L);
279 reg_r(gspca_dev, TV8532_PID_H);
280 reg_r(gspca_dev, TV8532_DeviceID);
281 reg_r(gspca_dev, TV8532_AD_COLBEGIN_L);
282 reg_r(gspca_dev, TV8532_AD_COLBEGIN_H);
283 reg_r(gspca_dev, TV8532_AD_ROWBEGIN_L);
284 reg_r(gspca_dev, TV8532_AD_ROWBEGIN_H);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300285}
286
287static void tv_8532_setReg(struct gspca_dev *gspca_dev)
288{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300289 reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_L,
290 ADCBEGINL); /* 0x10 */
291 reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_H,
292 ADCBEGINH); /* also digital gain */
293 reg_w_1(gspca_dev, TV8532_PART_CTRL,
294 TV8532_CMD_UPDATE); /* 0x00<-0x84 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300295
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300296 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0a);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300297 /******************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300298 reg_w_1(gspca_dev, TV8532_ADHEIGHT_L, ADHEIGHL); /* 0e */
299 reg_w_1(gspca_dev, TV8532_ADHEIGHT_H, ADHEIGHH); /* 0f */
300 reg_w_2(gspca_dev, TV8532_EXPOSURE,
301 EXPOL, EXPOH); /* 350d 0x014c; 1c */
302 reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_L,
303 ADCBEGINL); /* 0x10 */
304 reg_w_1(gspca_dev, TV8532_AD_COLBEGIN_H,
305 ADCBEGINH); /* also digital gain */
306 reg_w_1(gspca_dev, TV8532_AD_ROWBEGIN_L,
307 ADRBEGINL); /* 0x14 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300308
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300309 reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x00); /* 0x91 */
310 reg_w_1(gspca_dev, TV8532_AD_BITCTRL, 0x02); /* 0x94 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300311
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300312 reg_w_1(gspca_dev, TV8532_CTRL,
313 TV8532_CMD_EEprom_Close); /* 0x01 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300314
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300315 reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x00); /* 0x91 */
316 reg_w_1(gspca_dev, TV8532_PART_CTRL,
317 TV8532_CMD_UPDATE); /* 0x00<-0x84 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300318}
319
320static void tv_8532_PollReg(struct gspca_dev *gspca_dev)
321{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300322 int i;
323
324 /* strange polling from tgc */
325 for (i = 0; i < 10; i++) {
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300326 reg_w_1(gspca_dev, TV8532_SETCLK,
327 TESTCLK); /* 0x48; //0x08; 0x2c */
328 reg_w_1(gspca_dev, TV8532_PART_CTRL, TV8532_CMD_UPDATE);
329 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x01); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300330 }
331}
332
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300333/* this function is called at probe and resume time */
334static int sd_init(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300335{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300336 reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x32);
337 reg_w_1(gspca_dev, TV8532_AD_BITCTRL, 0x00);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300338 tv_8532ReadRegisters(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300339 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
340 reg_w_2(gspca_dev, TV8532_ADHEIGHT_L, ADHEIGHL,
341 ADHEIGHH); /* 401d 0x0169; 0e */
342 reg_w_2(gspca_dev, TV8532_EXPOSURE, EXPOL,
343 EXPOH); /* 350d 0x014c; 1c */
344 reg_w_1(gspca_dev, TV8532_ADWIDTH_L, ADWIDTHL); /* 0x20; 0x0c */
345 reg_w_1(gspca_dev, TV8532_ADWIDTH_H, ADWIDTHH); /* 0x0d */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300346
347 /*******************************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300348 reg_w_1(gspca_dev, TV8532_QUANT_COMP,
349 TESTCOMP); /* 0x72 compressed mode 0x28 */
350 reg_w_1(gspca_dev, TV8532_MODE_PACKET,
351 TESTLINE); /* 0x84; // CIF | 4 packet 0x29 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300352
353 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300354 reg_w_1(gspca_dev, TV8532_SETCLK,
355 TESTCLK); /* 0x48; //0x08; 0x2c */
356 reg_w_1(gspca_dev, TV8532_POINT_L,
357 TESTPTL); /* 0x38; 0x2d */
358 reg_w_1(gspca_dev, TV8532_POINT_H,
359 TESTPTH); /* 0x04; 0x2e */
360 reg_w_1(gspca_dev, TV8532_POINTB_L,
361 TESTPTBL); /* 0x04; 0x2f */
362 reg_w_1(gspca_dev, TV8532_POINTB_H,
363 TESTPTBH); /* 0x04; 0x30 */
364 reg_w_1(gspca_dev, TV8532_PART_CTRL,
365 TV8532_CMD_UPDATE); /* 0x00<-0x84 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300366 /*************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300367 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x01); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300368 msleep(200);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300369 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x00); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300370 /*************************************************/
371 tv_8532_setReg(gspca_dev);
372 /*************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300373 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300374 /*************************************************/
375 tv_8532_setReg(gspca_dev);
376 /*************************************************/
377 tv_8532_PollReg(gspca_dev);
378 return 0;
379}
380
381static void setbrightness(struct gspca_dev *gspca_dev)
382{
383 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300384 int brightness = sd->brightness;
385
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300386 reg_w_2(gspca_dev, TV8532_EXPOSURE,
387 brightness >> 8, brightness); /* 1c */
388 reg_w_1(gspca_dev, TV8532_PART_CTRL, TV8532_CMD_UPDATE);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300389}
390
391/* -- start the camera -- */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300392static int sd_start(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300393{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300394 reg_w_1(gspca_dev, TV8532_AD_SLOPE, 0x32);
395 reg_w_1(gspca_dev, TV8532_AD_BITCTRL, 0x00);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300396 tv_8532ReadRegisters(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300397 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
398 reg_w_2(gspca_dev, TV8532_ADHEIGHT_L,
399 ADHEIGHL, ADHEIGHH); /* 401d 0x0169; 0e */
400/* reg_w_2(gspca_dev, TV8532_EXPOSURE,
401 EXPOL, EXPOH); * 350d 0x014c; 1c */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300402 setbrightness(gspca_dev);
403
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300404 reg_w_1(gspca_dev, TV8532_ADWIDTH_L, ADWIDTHL); /* 0x20; 0x0c */
405 reg_w_1(gspca_dev, TV8532_ADWIDTH_H, ADWIDTHH); /* 0x0d */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300406
407 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300408 reg_w_1(gspca_dev, TV8532_QUANT_COMP,
409 TESTCOMP); /* 0x72 compressed mode 0x28 */
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300410 if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300411 /* 176x144 */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300412 reg_w_1(gspca_dev, TV8532_MODE_PACKET,
413 QCIFLINE); /* 0x84; // CIF | 4 packet 0x29 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300414 } else {
415 /* 352x288 */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300416 reg_w_1(gspca_dev, TV8532_MODE_PACKET,
417 TESTLINE); /* 0x84; // CIF | 4 packet 0x29 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300418 }
419 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300420 reg_w_1(gspca_dev, TV8532_SETCLK,
421 TESTCLK); /* 0x48; //0x08; 0x2c */
422 reg_w_1(gspca_dev, TV8532_POINT_L,
423 TESTPTL); /* 0x38; 0x2d */
424 reg_w_1(gspca_dev, TV8532_POINT_H,
425 TESTPTH); /* 0x04; 0x2e */
426 reg_w_1(gspca_dev, TV8532_POINTB_L,
427 TESTPTBL); /* 0x04; 0x2f */
428 reg_w_1(gspca_dev, TV8532_POINTB_H,
429 TESTPTBH); /* 0x04; 0x30 */
430 reg_w_1(gspca_dev, TV8532_PART_CTRL,
431 TV8532_CMD_UPDATE); /* 0x00<-0x84 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300432 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300433 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x01); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300434 msleep(200);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300435 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x00); /* 0x31 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300436 /************************************************/
437 tv_8532_setReg(gspca_dev);
438 /************************************************/
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300439 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300440 /************************************************/
441 tv_8532_setReg(gspca_dev);
442 /************************************************/
443 tv_8532_PollReg(gspca_dev);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300444 reg_w_1(gspca_dev, TV8532_UDP_UPDATE, 0x00); /* 0x31 */
Jean-Francois Moineba13cca2008-10-24 04:19:29 -0300445
446 gspca_dev->empty_packet = 0; /* check the empty packets */
447
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300448 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300449}
450
451static void sd_stopN(struct gspca_dev *gspca_dev)
452{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300453 reg_w_1(gspca_dev, TV8532_GPIO_OE, 0x0b);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300454}
455
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300456static void tv8532_preprocess(struct gspca_dev *gspca_dev)
457{
458 struct sd *sd = (struct sd *) gspca_dev;
459/* we should received a whole frame with header and EOL marker
Jean-Francois Moineba13cca2008-10-24 04:19:29 -0300460 * in sd->tmpbuf and return a GBRG pattern in sd->tmpbuf2
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300461 * sequence 2bytes header the Alternate pixels bayer GB 4 bytes
462 * Alternate pixels bayer RG 4 bytes EOL */
463 int width = gspca_dev->width;
464 int height = gspca_dev->height;
465 unsigned char *dst = sd->tmpbuf2;
466 unsigned char *data = sd->tmpbuf;
467 int i;
468
469 /* precompute where is the good bayer line */
470 if (((data[3] + data[width + 7]) >> 1)
471 + (data[4] >> 2)
472 + (data[width + 6] >> 1) >= ((data[2] + data[width + 6]) >> 1)
473 + (data[3] >> 2)
474 + (data[width + 5] >> 1))
475 data += 3;
476 else
477 data += 2;
478 for (i = 0; i < height / 2; i++) {
479 memcpy(dst, data, width);
480 data += width + 3;
481 dst += width;
482 memcpy(dst, data, width);
483 data += width + 7;
484 dst += width;
485 }
486}
487
488static void sd_pkt_scan(struct gspca_dev *gspca_dev,
489 struct gspca_frame *frame, /* target */
490 __u8 *data, /* isoc packet */
491 int len) /* iso packet length */
492{
493 struct sd *sd = (struct sd *) gspca_dev;
494
495 if (data[0] != 0x80) {
496 sd->packet++;
497 if (sd->buflen + len > sizeof sd->tmpbuf) {
498 if (gspca_dev->last_packet_type != DISCARD_PACKET) {
499 PDEBUG(D_PACK, "buffer overflow");
500 gspca_dev->last_packet_type = DISCARD_PACKET;
501 }
502 return;
503 }
504 memcpy(&sd->tmpbuf[sd->buflen], data, len);
505 sd->buflen += len;
506 return;
507 }
508
509 /* here we detect 0x80 */
510 /* counter is limited so we need few header for a frame :) */
511
512 /* header 0x80 0x80 0x80 0x80 0x80 */
513 /* packet 00 63 127 145 00 */
Jean-Francois Moineba13cca2008-10-24 04:19:29 -0300514 /* empty 1 0 0 1 1 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300515
516 /* update sequence */
517 if (sd->packet == 63 || sd->packet == 127)
Jean-Francois Moineba13cca2008-10-24 04:19:29 -0300518 gspca_dev->empty_packet = 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300519
520 /* is there a frame start ? */
Jean-Francois Moineba13cca2008-10-24 04:19:29 -0300521 /* (each packet is 2 lines plus 10 bytes) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300522 if (sd->packet >= (gspca_dev->height >> 1) - 1) {
Jean-Francois Moineba13cca2008-10-24 04:19:29 -0300523 PDEBUG(D_PACK, "empty %d packet %d",
524 gspca_dev->empty_packet, sd->packet);
525 if (gspca_dev->empty_packet) { /* start of frame */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300526 if (gspca_dev->last_packet_type == FIRST_PACKET) {
527 tv8532_preprocess(gspca_dev);
528 frame = gspca_frame_add(gspca_dev,
529 LAST_PACKET,
530 frame, sd->tmpbuf2,
Jean-Francois Moineba13cca2008-10-24 04:19:29 -0300531 gspca_dev->height *
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300532 gspca_dev->width);
533 }
534 gspca_frame_add(gspca_dev, FIRST_PACKET,
535 frame, data, 0);
536 memcpy(sd->tmpbuf, data, len);
537 sd->buflen = len;
538 sd->packet = 0;
539 return;
540 }
541 if (gspca_dev->last_packet_type != DISCARD_PACKET) {
542 PDEBUG(D_PACK,
543 "Warning wrong TV8532 frame detection %d",
544 sd->packet);
545 gspca_dev->last_packet_type = DISCARD_PACKET;
546 }
547 return;
548 }
549
Jean-Francois Moineba13cca2008-10-24 04:19:29 -0300550 if (gspca_dev->empty_packet) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300551 /* Drop packet frame corrupt */
Jean-Francois Moineba13cca2008-10-24 04:19:29 -0300552 PDEBUG(D_PACK, "DROP empty %d packet %d",
553 gspca_dev->empty_packet, sd->packet);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300554 sd->packet = 0;
555 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300556 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300557}
558
559static void setcontrast(struct gspca_dev *gspca_dev)
560{
561}
562
563static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
564{
565 struct sd *sd = (struct sd *) gspca_dev;
566
567 sd->brightness = val;
568 if (gspca_dev->streaming)
569 setbrightness(gspca_dev);
570 return 0;
571}
572
573static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
574{
575 struct sd *sd = (struct sd *) gspca_dev;
576
577 *val = sd->brightness;
578 return 0;
579}
580
581static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
582{
583 struct sd *sd = (struct sd *) gspca_dev;
584
585 sd->contrast = val;
586 if (gspca_dev->streaming)
587 setcontrast(gspca_dev);
588 return 0;
589}
590
591static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
592{
593 struct sd *sd = (struct sd *) gspca_dev;
594
595 *val = sd->contrast;
596 return 0;
597}
598
599/* sub-driver description */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300600static const struct sd_desc sd_desc = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300601 .name = MODULE_NAME,
602 .ctrls = sd_ctrls,
603 .nctrls = ARRAY_SIZE(sd_ctrls),
604 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300605 .init = sd_init,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300606 .start = sd_start,
607 .stopN = sd_stopN,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300608 .pkt_scan = sd_pkt_scan,
609};
610
611/* -- module initialisation -- */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300612static const __devinitdata struct usb_device_id device_table[] = {
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300613 {USB_DEVICE(0x046d, 0x0920)},
614 {USB_DEVICE(0x046d, 0x0921)},
615 {USB_DEVICE(0x0545, 0x808b)},
616 {USB_DEVICE(0x0545, 0x8333)},
617 {USB_DEVICE(0x0923, 0x010f)},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300618 {}
619};
620
621MODULE_DEVICE_TABLE(usb, device_table);
622
623/* -- device connect -- */
624static int sd_probe(struct usb_interface *intf,
625 const struct usb_device_id *id)
626{
627 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
628 THIS_MODULE);
629}
630
631static struct usb_driver sd_driver = {
632 .name = MODULE_NAME,
633 .id_table = device_table,
634 .probe = sd_probe,
635 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300636#ifdef CONFIG_PM
637 .suspend = gspca_suspend,
638 .resume = gspca_resume,
639#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300640};
641
642/* -- module insert / remove -- */
643static int __init sd_mod_init(void)
644{
645 if (usb_register(&sd_driver) < 0)
646 return -1;
Jean-Francois Moine10b0e962008-07-22 05:35:10 -0300647 PDEBUG(D_PROBE, "registered");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300648 return 0;
649}
650
651static void __exit sd_mod_exit(void)
652{
653 usb_deregister(&sd_driver);
654 PDEBUG(D_PROBE, "deregistered");
655}
656
657module_init(sd_mod_init);
658module_exit(sd_mod_exit);