blob: 68b082bcee1dcf2c704f53d0b194a61a1365a506 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __LINUX_OV511_H
2#define __LINUX_OV511_H
3
4#include <asm/uaccess.h>
5#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -03006#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/smp_lock.h>
8#include <linux/usb.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01009#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#define OV511_DEBUG /* Turn on debug messages */
12
13#ifdef OV511_DEBUG
14 #define PDEBUG(level, fmt, args...) \
15 if (debug >= (level)) info("[%s:%d] " fmt, \
16 __FUNCTION__, __LINE__ , ## args)
17#else
18 #define PDEBUG(level, fmt, args...) do {} while(0)
19#endif
20
21/* This macro restricts an int variable to an inclusive range */
22#define RESTRICT_TO_RANGE(v,mi,ma) { \
23 if ((v) < (mi)) (v) = (mi); \
24 else if ((v) > (ma)) (v) = (ma); \
25}
26
27/* --------------------------------- */
28/* DEFINES FOR OV511 AND OTHER CHIPS */
29/* --------------------------------- */
30
31/* USB IDs */
32#define VEND_OMNIVISION 0x05A9
33#define PROD_OV511 0x0511
34#define PROD_OV511PLUS 0xA511
35#define PROD_OV518 0x0518
36#define PROD_OV518PLUS 0xA518
37
38#define VEND_MATTEL 0x0813
39#define PROD_ME2CAM 0x0002
40
41/* --------------------------------- */
42/* OV51x REGISTER MNEMONICS */
43/* --------------------------------- */
44
45/* Camera interface register numbers */
46#define R511_CAM_DELAY 0x10
47#define R511_CAM_EDGE 0x11
48#define R511_CAM_PXCNT 0x12
49#define R511_CAM_LNCNT 0x13
50#define R511_CAM_PXDIV 0x14
51#define R511_CAM_LNDIV 0x15
52#define R511_CAM_UV_EN 0x16
53#define R511_CAM_LINE_MODE 0x17
54#define R511_CAM_OPTS 0x18
55
56/* Snapshot mode camera interface register numbers */
57#define R511_SNAP_FRAME 0x19
58#define R511_SNAP_PXCNT 0x1A
59#define R511_SNAP_LNCNT 0x1B
60#define R511_SNAP_PXDIV 0x1C
61#define R511_SNAP_LNDIV 0x1D
62#define R511_SNAP_UV_EN 0x1E
63#define R511_SNAP_OPTS 0x1F
64
65/* DRAM register numbers */
66#define R511_DRAM_FLOW_CTL 0x20
67#define R511_DRAM_ARCP 0x21
68#define R511_DRAM_MRC 0x22
69#define R511_DRAM_RFC 0x23
70
71/* ISO FIFO register numbers */
72#define R51x_FIFO_PSIZE 0x30 /* 2 bytes wide w/ OV518(+) */
73#define R511_FIFO_OPTS 0x31
74
75/* Parallel IO register numbers */
76#define R511_PIO_OPTS 0x38
77#define R511_PIO_DATA 0x39
78#define R511_PIO_BIST 0x3E
79#define R518_GPIO_IN 0x55 /* OV518(+) only */
80#define R518_GPIO_OUT 0x56 /* OV518(+) only */
81#define R518_GPIO_CTL 0x57 /* OV518(+) only */
82#define R518_GPIO_PULSE_IN 0x58 /* OV518(+) only */
83#define R518_GPIO_PULSE_CLEAR 0x59 /* OV518(+) only */
84#define R518_GPIO_PULSE_POL 0x5a /* OV518(+) only */
85#define R518_GPIO_PULSE_EN 0x5b /* OV518(+) only */
86#define R518_GPIO_RESET 0x5c /* OV518(+) only */
87
88/* I2C registers */
89#define R511_I2C_CTL 0x40
90#define R518_I2C_CTL 0x47 /* OV518(+) only */
91#define R51x_I2C_W_SID 0x41
92#define R51x_I2C_SADDR_3 0x42
93#define R51x_I2C_SADDR_2 0x43
94#define R51x_I2C_R_SID 0x44
95#define R51x_I2C_DATA 0x45
96#define R51x_I2C_CLOCK 0x46
97#define R51x_I2C_TIMEOUT 0x47
98
99/* I2C snapshot registers */
100#define R511_SI2C_SADDR_3 0x48
101#define R511_SI2C_DATA 0x49
102
103/* System control registers */
104#define R51x_SYS_RESET 0x50
105 /* Reset type definitions */
106#define OV511_RESET_UDC 0x01
107#define OV511_RESET_I2C 0x02
108#define OV511_RESET_FIFO 0x04
109#define OV511_RESET_OMNICE 0x08
110#define OV511_RESET_DRAM 0x10
111#define OV511_RESET_CAM_INT 0x20
112#define OV511_RESET_OV511 0x40
113#define OV511_RESET_NOREGS 0x3F /* All but OV511 & regs */
114#define OV511_RESET_ALL 0x7F
115
116#define R511_SYS_CLOCK_DIV 0x51
117#define R51x_SYS_SNAP 0x52
118#define R51x_SYS_INIT 0x53
119#define R511_SYS_PWR_CLK 0x54 /* OV511+/OV518(+) only */
120#define R511_SYS_LED_CTL 0x55 /* OV511+ only */
121#define R511_SYS_USER 0x5E
122#define R511_SYS_CUST_ID 0x5F
123
124/* OmniCE (compression) registers */
125#define R511_COMP_PHY 0x70
126#define R511_COMP_PHUV 0x71
127#define R511_COMP_PVY 0x72
128#define R511_COMP_PVUV 0x73
129#define R511_COMP_QHY 0x74
130#define R511_COMP_QHUV 0x75
131#define R511_COMP_QVY 0x76
132#define R511_COMP_QVUV 0x77
133#define R511_COMP_EN 0x78
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300134#define R511_COMP_LUT_EN 0x79
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define R511_COMP_LUT_BEGIN 0x80
136
137/* --------------------------------- */
138/* ALTERNATE NUMBERS */
139/* --------------------------------- */
140
141/* Alternate numbers for various max packet sizes (OV511 only) */
142#define OV511_ALT_SIZE_992 0
143#define OV511_ALT_SIZE_993 1
144#define OV511_ALT_SIZE_768 2
145#define OV511_ALT_SIZE_769 3
146#define OV511_ALT_SIZE_512 4
147#define OV511_ALT_SIZE_513 5
148#define OV511_ALT_SIZE_257 6
149#define OV511_ALT_SIZE_0 7
150
151/* Alternate numbers for various max packet sizes (OV511+ only) */
152#define OV511PLUS_ALT_SIZE_0 0
153#define OV511PLUS_ALT_SIZE_33 1
154#define OV511PLUS_ALT_SIZE_129 2
155#define OV511PLUS_ALT_SIZE_257 3
156#define OV511PLUS_ALT_SIZE_385 4
157#define OV511PLUS_ALT_SIZE_513 5
158#define OV511PLUS_ALT_SIZE_769 6
159#define OV511PLUS_ALT_SIZE_961 7
160
161/* Alternate numbers for various max packet sizes (OV518(+) only) */
162#define OV518_ALT_SIZE_0 0
163#define OV518_ALT_SIZE_128 1
164#define OV518_ALT_SIZE_256 2
165#define OV518_ALT_SIZE_384 3
166#define OV518_ALT_SIZE_512 4
167#define OV518_ALT_SIZE_640 5
168#define OV518_ALT_SIZE_768 6
169#define OV518_ALT_SIZE_896 7
170
171/* --------------------------------- */
172/* OV7610 REGISTER MNEMONICS */
173/* --------------------------------- */
174
175/* OV7610 registers */
176#define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
177#define OV7610_REG_BLUE 0x01 /* blue channel balance */
178#define OV7610_REG_RED 0x02 /* red channel balance */
179#define OV7610_REG_SAT 0x03 /* saturation */
180 /* 04 reserved */
181#define OV7610_REG_CNT 0x05 /* Y contrast */
182#define OV7610_REG_BRT 0x06 /* Y brightness */
183 /* 08-0b reserved */
184#define OV7610_REG_BLUE_BIAS 0x0C /* blue channel bias (5:0) */
185#define OV7610_REG_RED_BIAS 0x0D /* read channel bias (5:0) */
186#define OV7610_REG_GAMMA_COEFF 0x0E /* gamma settings */
187#define OV7610_REG_WB_RANGE 0x0F /* AEC/ALC/S-AWB settings */
188#define OV7610_REG_EXP 0x10 /* manual exposure setting */
189#define OV7610_REG_CLOCK 0x11 /* polarity/clock prescaler */
190#define OV7610_REG_COM_A 0x12 /* misc common regs */
191#define OV7610_REG_COM_B 0x13 /* misc common regs */
192#define OV7610_REG_COM_C 0x14 /* misc common regs */
193#define OV7610_REG_COM_D 0x15 /* misc common regs */
194#define OV7610_REG_FIELD_DIVIDE 0x16 /* field interval/mode settings */
195#define OV7610_REG_HWIN_START 0x17 /* horizontal window start */
196#define OV7610_REG_HWIN_END 0x18 /* horizontal window end */
197#define OV7610_REG_VWIN_START 0x19 /* vertical window start */
198#define OV7610_REG_VWIN_END 0x1A /* vertical window end */
199#define OV7610_REG_PIXEL_SHIFT 0x1B /* pixel shift */
200#define OV7610_REG_ID_HIGH 0x1C /* manufacturer ID MSB */
201#define OV7610_REG_ID_LOW 0x1D /* manufacturer ID LSB */
202 /* 0e-0f reserved */
203#define OV7610_REG_COM_E 0x20 /* misc common regs */
204#define OV7610_REG_YOFFSET 0x21 /* Y channel offset */
205#define OV7610_REG_UOFFSET 0x22 /* U channel offset */
206 /* 23 reserved */
207#define OV7610_REG_ECW 0x24 /* Exposure white level for AEC */
208#define OV7610_REG_ECB 0x25 /* Exposure black level for AEC */
209#define OV7610_REG_COM_F 0x26 /* misc settings */
210#define OV7610_REG_COM_G 0x27 /* misc settings */
211#define OV7610_REG_COM_H 0x28 /* misc settings */
212#define OV7610_REG_COM_I 0x29 /* misc settings */
213#define OV7610_REG_FRAMERATE_H 0x2A /* frame rate MSB + misc */
214#define OV7610_REG_FRAMERATE_L 0x2B /* frame rate LSB */
215#define OV7610_REG_ALC 0x2C /* Auto Level Control settings */
216#define OV7610_REG_COM_J 0x2D /* misc settings */
217#define OV7610_REG_VOFFSET 0x2E /* V channel offset adjustment */
218#define OV7610_REG_ARRAY_BIAS 0x2F /* Array bias -- don't change */
219 /* 30-32 reserved */
220#define OV7610_REG_YGAMMA 0x33 /* misc gamma settings (7:6) */
221#define OV7610_REG_BIAS_ADJUST 0x34 /* misc bias settings */
222#define OV7610_REG_COM_L 0x35 /* misc settings */
223 /* 36-37 reserved */
224#define OV7610_REG_COM_K 0x38 /* misc registers */
225
226/* --------------------------------- */
227/* I2C ADDRESSES */
228/* --------------------------------- */
229
230#define OV7xx0_SID 0x42
231#define OV6xx0_SID 0xC0
232#define OV8xx0_SID 0xA0
233#define KS0127_SID 0xD8
234#define SAA7111A_SID 0x48
235
236/* --------------------------------- */
237/* MISCELLANEOUS DEFINES */
238/* --------------------------------- */
239
240#define I2C_CLOCK_PRESCALER 0x03
241
242#define FRAMES_PER_DESC 10 /* FIXME - What should this be? */
243#define MAX_FRAME_SIZE_PER_DESC 993 /* For statically allocated stuff */
244#define PIXELS_PER_SEG 256 /* Pixels per segment */
245
246#define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
247
248#define OV511_NUMFRAMES 2
249#if OV511_NUMFRAMES > VIDEO_MAX_FRAME
250 #error "OV511_NUMFRAMES is too high"
251#endif
252
253#define OV511_NUMSBUF 2
254
255/* Control transfers use up to 4 bytes */
256#define OV511_CBUF_SIZE 4
257
258/* Size of usb_make_path() buffer */
259#define OV511_USB_PATH_LEN 64
260
261/* Bridge types */
262enum {
263 BRG_UNKNOWN,
264 BRG_OV511,
265 BRG_OV511PLUS,
266 BRG_OV518,
267 BRG_OV518PLUS,
268};
269
270/* Bridge classes */
271enum {
272 BCL_UNKNOWN,
273 BCL_OV511,
274 BCL_OV518,
275};
276
277/* Sensor types */
278enum {
279 SEN_UNKNOWN,
280 SEN_OV76BE,
281 SEN_OV7610,
282 SEN_OV7620,
283 SEN_OV7620AE,
284 SEN_OV6620,
285 SEN_OV6630,
286 SEN_OV6630AE,
287 SEN_OV6630AF,
288 SEN_OV8600,
289 SEN_KS0127,
290 SEN_KS0127B,
291 SEN_SAA7111A,
292};
293
294enum {
295 STATE_SCANNING, /* Scanning for start */
296 STATE_HEADER, /* Parsing header */
297 STATE_LINES, /* Parsing lines */
298};
299
300/* Buffer states */
301enum {
302 BUF_NOT_ALLOCATED,
303 BUF_ALLOCATED,
304};
305
306/* --------- Definition of ioctl interface --------- */
307
308#define OV511_INTERFACE_VER 101
309
310/* LED options */
311enum {
312 LED_OFF,
313 LED_ON,
314 LED_AUTO,
315};
316
317/* Raw frame formats */
318enum {
319 RAWFMT_INVALID,
320 RAWFMT_YUV400,
321 RAWFMT_YUV420,
322 RAWFMT_YUV422,
323 RAWFMT_GBR422,
324};
325
326struct ov511_i2c_struct {
327 unsigned char slave; /* Write slave ID (read ID - 1) */
328 unsigned char reg; /* Index of register */
329 unsigned char value; /* User sets this w/ write, driver does w/ read */
330 unsigned char mask; /* Bits to be changed. Not used with read ops */
331};
332
333/* ioctls */
334#define OV511IOC_WI2C _IOW('v', BASE_VIDIOCPRIVATE + 5, \
335 struct ov511_i2c_struct)
336#define OV511IOC_RI2C _IOWR('v', BASE_VIDIOCPRIVATE + 6, \
337 struct ov511_i2c_struct)
338/* ------------- End IOCTL interface -------------- */
339
340struct usb_ov511; /* Forward declaration */
341
342struct ov511_sbuf {
343 struct usb_ov511 *ov;
344 unsigned char *data;
345 struct urb *urb;
346 spinlock_t lock;
347 int n;
348};
349
350enum {
351 FRAME_UNUSED, /* Unused (no MCAPTURE) */
352 FRAME_READY, /* Ready to start grabbing */
353 FRAME_GRABBING, /* In the process of being grabbed into */
354 FRAME_DONE, /* Finished grabbing, but not been synced yet */
355 FRAME_ERROR, /* Something bad happened while processing */
356};
357
358struct ov511_regvals {
359 enum {
360 OV511_DONE_BUS,
361 OV511_REG_BUS,
362 OV511_I2C_BUS,
363 } bus;
364 unsigned char reg;
365 unsigned char val;
366};
367
368struct ov511_frame {
369 int framenum; /* Index of this frame */
370 unsigned char *data; /* Frame buffer */
371 unsigned char *tempdata; /* Temp buffer for multi-stage conversions */
372 unsigned char *rawdata; /* Raw camera data buffer */
373 unsigned char *compbuf; /* Temp buffer for decompressor */
374
375 int depth; /* Bytes per pixel */
376 int width; /* Width application is expecting */
377 int height; /* Height application is expecting */
378
379 int rawwidth; /* Actual width of frame sent from camera */
380 int rawheight; /* Actual height of frame sent from camera */
381
382 int sub_flag; /* Sub-capture mode for this frame? */
383 unsigned int format; /* Format for this frame */
384 int compressed; /* Is frame compressed? */
385
386 volatile int grabstate; /* State of grabbing */
387 int scanstate; /* State of scanning */
388
389 int bytes_recvd; /* Number of image bytes received from camera */
390
391 long bytes_read; /* Amount that has been read() */
392
393 wait_queue_head_t wq; /* Processes waiting */
394
395 int snapshot; /* True if frame was a snapshot */
396};
397
398#define DECOMP_INTERFACE_VER 4
399
400/* Compression module operations */
401struct ov51x_decomp_ops {
402 int (*decomp_400)(unsigned char *, unsigned char *, unsigned char *,
403 int, int, int);
404 int (*decomp_420)(unsigned char *, unsigned char *, unsigned char *,
405 int, int, int);
406 int (*decomp_422)(unsigned char *, unsigned char *, unsigned char *,
407 int, int, int);
408 struct module *owner;
409};
410
411struct usb_ov511 {
412 struct video_device *vdev;
413 struct usb_device *dev;
414
415 int customid;
416 char *desc;
417 unsigned char iface;
418 char usb_path[OV511_USB_PATH_LEN];
419
420 /* Determined by sensor type */
421 int maxwidth;
422 int maxheight;
423 int minwidth;
424 int minheight;
425
426 int brightness;
427 int colour;
428 int contrast;
429 int hue;
430 int whiteness;
431 int exposure;
432 int auto_brt; /* Auto brightness enabled flag */
433 int auto_gain; /* Auto gain control enabled flag */
434 int auto_exp; /* Auto exposure enabled flag */
435 int backlight; /* Backlight exposure algorithm flag */
436 int mirror; /* Image is reversed horizontally */
437
438 int led_policy; /* LED: off|on|auto; OV511+ only */
439
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100440 struct mutex lock; /* Serializes user-accessible operations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int user; /* user count for exclusive use */
442
443 int streaming; /* Are we streaming Isochronous? */
444 int grabbing; /* Are we grabbing? */
445
446 int compress; /* Should the next frame be compressed? */
447 int compress_inited; /* Are compression params uploaded? */
448
449 int lightfreq; /* Power (lighting) frequency */
450 int bandfilt; /* Banding filter enabled flag */
451
452 unsigned char *fbuf; /* Videodev buffer area */
453 unsigned char *tempfbuf; /* Temporary (intermediate) buffer area */
454 unsigned char *rawfbuf; /* Raw camera data buffer area */
455
456 int sub_flag; /* Pix Array subcapture on flag */
457 int subx; /* Pix Array subcapture x offset */
458 int suby; /* Pix Array subcapture y offset */
459 int subw; /* Pix Array subcapture width */
460 int subh; /* Pix Array subcapture height */
461
462 int curframe; /* Current receiving sbuf */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300463 struct ov511_frame frame[OV511_NUMFRAMES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 struct ov511_sbuf sbuf[OV511_NUMSBUF];
466
467 wait_queue_head_t wq; /* Processes waiting */
468
469 int snap_enabled; /* Snapshot mode enabled */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int bridge; /* Type of bridge (BRG_*) */
472 int bclass; /* Class of bridge (BCL_*) */
473 int sensor; /* Type of image sensor chip (SEN_*) */
474
475 int packet_size; /* Frame size per isoc desc */
476 int packet_numbering; /* Is ISO frame numbering enabled? */
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* Framebuffer/sbuf management */
479 int buf_state;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100480 struct mutex buf_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 struct ov51x_decomp_ops *decomp_ops;
483
484 /* Stop streaming while changing picture settings */
485 int stop_during_set;
486
487 int stopped; /* Streaming is temporarily paused */
488
489 /* Video decoder stuff */
490 int input; /* Composite, S-VIDEO, etc... */
491 int num_inputs; /* Number of inputs */
492 int norm; /* NTSC / PAL / SECAM */
493 int has_decoder; /* Device has a video decoder */
494 int pal; /* Device is designed for PAL resolution */
495
496 /* I2C interface */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100497 struct mutex i2c_lock; /* Protect I2C controller regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 unsigned char primary_i2c_slave; /* I2C write id of sensor */
499
500 /* Control transaction stuff */
501 unsigned char *cbuf; /* Buffer for payload */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100502 struct mutex cbuf_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503};
504
505/* Used to represent a list of values and their respective symbolic names */
506struct symbolic_list {
507 int num;
508 char *name;
509};
510
511#define NOT_DEFINED_STR "Unknown"
512
513/* Returns the name of the matching element in the symbolic_list array. The
514 * end of the list must be marked with an element that has a NULL name.
515 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300516static inline char *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517symbolic(struct symbolic_list list[], int num)
518{
519 int i;
520
521 for (i = 0; list[i].name != NULL; i++)
522 if (list[i].num == num)
523 return (list[i].name);
524
525 return (NOT_DEFINED_STR);
526}
527
528/* Compression stuff */
529
530#define OV511_QUANTABLESIZE 64
531#define OV518_QUANTABLESIZE 32
532
533#define OV511_YQUANTABLE { \
534 0, 1, 1, 2, 2, 3, 3, 4, \
535 1, 1, 1, 2, 2, 3, 4, 4, \
536 1, 1, 2, 2, 3, 4, 4, 4, \
537 2, 2, 2, 3, 4, 4, 4, 4, \
538 2, 2, 3, 4, 4, 5, 5, 5, \
539 3, 3, 4, 4, 5, 5, 5, 5, \
540 3, 4, 4, 4, 5, 5, 5, 5, \
541 4, 4, 4, 4, 5, 5, 5, 5 \
542}
543
544#define OV511_UVQUANTABLE { \
545 0, 2, 2, 3, 4, 4, 4, 4, \
546 2, 2, 2, 4, 4, 4, 4, 4, \
547 2, 2, 3, 4, 4, 4, 4, 4, \
548 3, 4, 4, 4, 4, 4, 4, 4, \
549 4, 4, 4, 4, 4, 4, 4, 4, \
550 4, 4, 4, 4, 4, 4, 4, 4, \
551 4, 4, 4, 4, 4, 4, 4, 4, \
552 4, 4, 4, 4, 4, 4, 4, 4 \
553}
554
555#define OV518_YQUANTABLE { \
556 5, 4, 5, 6, 6, 7, 7, 7, \
557 5, 5, 5, 5, 6, 7, 7, 7, \
558 6, 6, 6, 6, 7, 7, 7, 8, \
559 7, 7, 6, 7, 7, 7, 8, 8 \
560}
561
562#define OV518_UVQUANTABLE { \
563 6, 6, 6, 7, 7, 7, 7, 7, \
564 6, 6, 6, 7, 7, 7, 7, 7, \
565 6, 6, 6, 7, 7, 7, 7, 8, \
566 7, 7, 7, 7, 7, 7, 8, 8 \
567}
568
569#endif