1/*
2 * Copyright 2004-2008, François Revol, <revol@free.fr>.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _SONIX_CAM_DEVICE_H
6#define _SONIX_CAM_DEVICE_H
7
8#include "CamDevice.h"
9
10#define SN9C102_REG_COUNT	0x20
11/* SN9c102 registers */
12#define SN9C102_ASIC_ID		0x00
13#define SN9C102_CHIP_CTRL	0x01
14#define SN9C102_GPIO			0x02
15#define SN9C103_G_GAIN		0x04	/* chip version dependant! */
16#define SN9C102_R_GAIN		0x05
17#define SN9C102_B_GAIN		0x06
18#define SN9C102_I2C_SETUP	0x08
19#define SN9C102_I2C_SLAVE_ID	0x09
20#define SN9C102_I2C_DATA0	0x0a
21#define SN9C102_I2C_DATA1	0x0b
22#define SN9C102_I2C_DATA2	0x0c
23#define SN9C102_I2C_DATA3	0x0d
24#define SN9C102_I2C_DATA4	0x0e
25#define SN9C102_CONTROL_STAT	0x0f /*I2C ??*/
26#define SN9C102_R_B_GAIN		0x10	/* datasheet says so but it's WRONG */
27#define SN9C102_G_GAIN		0x11 /* Green channel gain control. -> Gain = (1+G_GAIN/8)
28							    Note: It is sync with VSYNC */
29#define SN9C102_H_START		0x12 /* Start active pixel number after H­sync of sensor
30							    Note:
31							    The 1st line sequence of image data is BGBGBG
32							    The 2nd line sequence of image data is GRGRGR */
33#define SN9C102_V_START		0x13 /* Start active line number after V­sync of sensor */
34#define SN9C102_OFFSET		0x14 /* Offset adjustment for sensor image data. */
35#define SN9C102_H_SIZE		0x15 /* Horizontal pixel number for sensor. */
36#define SN9C102_V_SIZE		0x16 /* Vertical pixel number for sensor. */
37#define SN9C102_CLOCK_SEL	0x17
38#define SN9C102_SYNC_N_SCALE	0x18
39#define SN9C102_PIX_CLK		0x19
40#define SN9C102_HO_SIZE		0x1a /* /32 */
41#define SN9C102_VO_SIZE		0x1b /* /32 */
42#define SN9C102_AE_STRX		0x1c
43#define SN9C102_AE_STRY		0x1d
44#define SN9C102_AE_ENDX		0x1e
45#define SN9C102_AE_ENDY		0x1f
46
47// extra regs ? maybe 103 or later only ? used by gspcav1
48#define SN9C10x_CONTRAST	0x84
49#define SN9C10x_BRIGHTNESS	0x96
50#undef SN9C102_REG_COUNT
51#define SN9C102_REG_COUNT	0x100
52
53
54#define SN9C102_RGB_GAIN_MAX	0x7f
55
56// This class represents each webcam
57class SonixCamDevice : public CamDevice {
58	public:
59						SonixCamDevice(CamDeviceAddon &_addon, BUSBDevice* _device);
60						~SonixCamDevice();
61	virtual bool		SupportsBulk();
62	virtual bool		SupportsIsochronous();
63	virtual status_t	StartTransfer();
64	virtual status_t	StopTransfer();
65
66	// sensor chip handling
67	virtual status_t	PowerOnSensor(bool on);
68
69	// generic register-like access
70	virtual ssize_t		WriteReg(uint16 address, uint8 *data, size_t count=1);
71	virtual ssize_t		ReadReg(uint16 address, uint8 *data, size_t count=1, bool cached=false);
72
73	// I2C-like access
74	virtual status_t	GetStatusIIC();
75	virtual status_t	WaitReadyIIC();
76	virtual ssize_t		WriteIIC(uint8 address, uint8 *data, size_t count=1);
77	virtual ssize_t		ReadIIC(uint8 address, uint8 *data);
78
79	virtual status_t	SetVideoFrame(BRect rect);
80	virtual status_t	SetScale(float scale);
81	virtual status_t	SetVideoParams(float brightness, float contrast, float hue, float red, float green, float blue);
82
83	virtual void		AddParameters(BParameterGroup *group, int32 &index);
84	virtual status_t	GetParameterValue(int32 id, bigtime_t *last_change, void *value, size_t *size);
85	virtual status_t	SetParameterValue(int32 id, bigtime_t when, const void *value, size_t size);
86
87
88	// for use by deframer
89	virtual size_t		MinRawFrameSize();
90	virtual size_t		MaxRawFrameSize();
91	virtual bool		ValidateStartOfFrameTag(const uint8 *tag, size_t taglen);
92	virtual bool		ValidateEndOfFrameTag(const uint8 *tag, size_t taglen, size_t datalen);
93
94	virtual status_t	GetFrameBitmap(BBitmap **bm, bigtime_t *stamp=NULL);
95	virtual status_t	FillFrameBuffer(BBuffer *buffer, bigtime_t *stamp=NULL);
96
97
98	void				DumpRegs();
99
100	private:
101//	status_t			SendCommand(uint8 dir, uint8 request, uint16 value,
102//									uint16 index, uint16 length, void* data);
103	uint8				fCachedRegs[SN9C102_REG_COUNT];
104	int					fChipVersion;
105
106	int					fFrameTagState;
107
108	uint8				fRGain;
109	uint8				fGGain;
110	uint8				fBGain;
111	float				fContrast;
112	float				fBrightness;
113};
114
115// the addon itself, that instanciate
116
117class SonixCamDeviceAddon : public CamDeviceAddon {
118	public:
119						SonixCamDeviceAddon(WebCamMediaAddOn* webcam);
120	virtual 			~SonixCamDeviceAddon();
121
122	virtual const char	*BrandName();
123	virtual SonixCamDevice	*Instantiate(CamRoster &roster, BUSBDevice *from);
124
125};
126
127
128
129#endif /* _SONIX_CAM_DEVICE_H */
130