xref: /linux/drivers/media/usb/go7007/go7007-priv.h (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
2  * Copyright (C) 2005-2006 Micronas USA Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License (Version 2) as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 /*
15  * This is the private include file for the go7007 driver.  It should not
16  * be included by anybody but the driver itself, and especially not by
17  * user-space applications.
18  */
19 
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-fh.h>
23 #include <media/videobuf2-v4l2.h>
24 
25 struct go7007;
26 
27 /* IDs to activate board-specific support code */
28 #define GO7007_BOARDID_MATRIX_II	0
29 #define GO7007_BOARDID_MATRIX_RELOAD	1
30 #define GO7007_BOARDID_STAR_TREK	2
31 #define GO7007_BOARDID_PCI_VOYAGER	3
32 #define GO7007_BOARDID_XMEN		4
33 #define GO7007_BOARDID_XMEN_II		5
34 #define GO7007_BOARDID_XMEN_III		6
35 #define GO7007_BOARDID_MATRIX_REV	7
36 #define GO7007_BOARDID_PX_M402U		8
37 #define GO7007_BOARDID_PX_TV402U	9
38 #define GO7007_BOARDID_LIFEVIEW_LR192	10 /* TV Walker Ultra */
39 #define GO7007_BOARDID_ENDURA		11
40 #define GO7007_BOARDID_ADLINK_MPG24	12
41 #define GO7007_BOARDID_SENSORAY_2250	13 /* Sensoray 2250/2251 */
42 #define GO7007_BOARDID_ADS_USBAV_709    14
43 
44 /* Various characteristics of each board */
45 #define GO7007_BOARD_HAS_AUDIO		(1<<0)
46 #define GO7007_BOARD_USE_ONBOARD_I2C	(1<<1)
47 #define GO7007_BOARD_HAS_TUNER		(1<<2)
48 
49 /* Characteristics of sensor devices */
50 #define GO7007_SENSOR_VALID_POLAR	(1<<0)
51 #define GO7007_SENSOR_HREF_POLAR	(1<<1)
52 #define GO7007_SENSOR_VREF_POLAR	(1<<2)
53 #define GO7007_SENSOR_FIELD_ID_POLAR	(1<<3)
54 #define GO7007_SENSOR_BIT_WIDTH		(1<<4)
55 #define GO7007_SENSOR_VALID_ENABLE	(1<<5)
56 #define GO7007_SENSOR_656		(1<<6)
57 #define GO7007_SENSOR_CONFIG_MASK	0x7f
58 #define GO7007_SENSOR_TV		(1<<7)
59 #define GO7007_SENSOR_VBI		(1<<8)
60 #define GO7007_SENSOR_SCALING		(1<<9)
61 #define GO7007_SENSOR_SAA7115		(1<<10)
62 
63 /* Characteristics of audio sensor devices */
64 #define GO7007_AUDIO_I2S_MODE_1		(1)
65 #define GO7007_AUDIO_I2S_MODE_2		(2)
66 #define GO7007_AUDIO_I2S_MODE_3		(3)
67 #define GO7007_AUDIO_BCLK_POLAR		(1<<2)
68 #define GO7007_AUDIO_WORD_14		(14<<4)
69 #define GO7007_AUDIO_WORD_16		(16<<4)
70 #define GO7007_AUDIO_ONE_CHANNEL	(1<<11)
71 #define GO7007_AUDIO_I2S_MASTER		(1<<16)
72 #define GO7007_AUDIO_OKI_MODE		(1<<17)
73 
74 #define GO7007_CID_CUSTOM_BASE		(V4L2_CID_DETECT_CLASS_BASE + 0x1000)
75 #define V4L2_CID_PIXEL_THRESHOLD0	(GO7007_CID_CUSTOM_BASE+1)
76 #define V4L2_CID_MOTION_THRESHOLD0	(GO7007_CID_CUSTOM_BASE+2)
77 #define V4L2_CID_MB_THRESHOLD0		(GO7007_CID_CUSTOM_BASE+3)
78 #define V4L2_CID_PIXEL_THRESHOLD1	(GO7007_CID_CUSTOM_BASE+4)
79 #define V4L2_CID_MOTION_THRESHOLD1	(GO7007_CID_CUSTOM_BASE+5)
80 #define V4L2_CID_MB_THRESHOLD1		(GO7007_CID_CUSTOM_BASE+6)
81 #define V4L2_CID_PIXEL_THRESHOLD2	(GO7007_CID_CUSTOM_BASE+7)
82 #define V4L2_CID_MOTION_THRESHOLD2	(GO7007_CID_CUSTOM_BASE+8)
83 #define V4L2_CID_MB_THRESHOLD2		(GO7007_CID_CUSTOM_BASE+9)
84 #define V4L2_CID_PIXEL_THRESHOLD3	(GO7007_CID_CUSTOM_BASE+10)
85 #define V4L2_CID_MOTION_THRESHOLD3	(GO7007_CID_CUSTOM_BASE+11)
86 #define V4L2_CID_MB_THRESHOLD3		(GO7007_CID_CUSTOM_BASE+12)
87 
88 struct go7007_board_info {
89 	unsigned int flags;
90 	int hpi_buffer_cap;
91 	unsigned int sensor_flags;
92 	int sensor_width;
93 	int sensor_height;
94 	int sensor_framerate;
95 	int sensor_h_offset;
96 	int sensor_v_offset;
97 	unsigned int audio_flags;
98 	int audio_rate;
99 	int audio_bclk_div;
100 	int audio_main_div;
101 	int num_i2c_devs;
102 	struct go_i2c {
103 		const char *type;
104 		unsigned int is_video:1;
105 		unsigned int is_audio:1;
106 		int addr;
107 		u32 flags;
108 	} i2c_devs[5];
109 	int num_inputs;
110 	struct {
111 		int video_input;
112 		int audio_index;
113 		char *name;
114 	} inputs[4];
115 	int video_config;
116 	int num_aud_inputs;
117 	struct {
118 		int audio_input;
119 		char *name;
120 	} aud_inputs[3];
121 };
122 
123 struct go7007_hpi_ops {
124 	int (*interface_reset)(struct go7007 *go);
125 	int (*write_interrupt)(struct go7007 *go, int addr, int data);
126 	int (*read_interrupt)(struct go7007 *go);
127 	int (*stream_start)(struct go7007 *go);
128 	int (*stream_stop)(struct go7007 *go);
129 	int (*send_firmware)(struct go7007 *go, u8 *data, int len);
130 	int (*send_command)(struct go7007 *go, unsigned int cmd, void *arg);
131 	void (*release)(struct go7007 *go);
132 };
133 
134 /* The video buffer size must be a multiple of PAGE_SIZE */
135 #define	GO7007_BUF_PAGES	(128 * 1024 / PAGE_SIZE)
136 #define	GO7007_BUF_SIZE		(GO7007_BUF_PAGES << PAGE_SHIFT)
137 
138 struct go7007_buffer {
139 	struct vb2_v4l2_buffer vb;
140 	struct list_head list;
141 	unsigned int frame_offset;
142 	u32 modet_active;
143 };
144 
145 #define GO7007_RATIO_1_1	0
146 #define GO7007_RATIO_4_3	1
147 #define GO7007_RATIO_16_9	2
148 
149 enum go7007_parser_state {
150 	STATE_DATA,
151 	STATE_00,
152 	STATE_00_00,
153 	STATE_00_00_01,
154 	STATE_FF,
155 	STATE_VBI_LEN_A,
156 	STATE_VBI_LEN_B,
157 	STATE_MODET_MAP,
158 	STATE_UNPARSED,
159 };
160 
161 struct go7007 {
162 	struct device *dev;
163 	u8 bus_info[32];
164 	const struct go7007_board_info *board_info;
165 	unsigned int board_id;
166 	int tuner_type;
167 	int channel_number; /* for multi-channel boards like Adlink PCI-MPG24 */
168 	char name[64];
169 	struct video_device vdev;
170 	void *boot_fw;
171 	unsigned boot_fw_len;
172 	struct v4l2_device v4l2_dev;
173 	struct v4l2_ctrl_handler hdl;
174 	struct v4l2_ctrl *mpeg_video_encoding;
175 	struct v4l2_ctrl *mpeg_video_gop_size;
176 	struct v4l2_ctrl *mpeg_video_gop_closure;
177 	struct v4l2_ctrl *mpeg_video_bitrate;
178 	struct v4l2_ctrl *mpeg_video_aspect_ratio;
179 	struct v4l2_ctrl *mpeg_video_b_frames;
180 	struct v4l2_ctrl *mpeg_video_rep_seqheader;
181 	struct v4l2_ctrl *modet_mode;
182 	enum { STATUS_INIT, STATUS_ONLINE, STATUS_SHUTDOWN } status;
183 	spinlock_t spinlock;
184 	struct mutex hw_lock;
185 	struct mutex serialize_lock;
186 	int audio_enabled;
187 	struct v4l2_subdev *sd_video;
188 	struct v4l2_subdev *sd_audio;
189 	u8 usb_buf[16];
190 
191 	/* Video input */
192 	int input;
193 	int aud_input;
194 	enum { GO7007_STD_NTSC, GO7007_STD_PAL, GO7007_STD_OTHER } standard;
195 	v4l2_std_id std;
196 	int sensor_framerate;
197 	int width;
198 	int height;
199 	int encoder_h_offset;
200 	int encoder_v_offset;
201 	unsigned int encoder_h_halve:1;
202 	unsigned int encoder_v_halve:1;
203 	unsigned int encoder_subsample:1;
204 
205 	/* Encoder config */
206 	u32 format;
207 	int bitrate;
208 	int fps_scale;
209 	int pali;
210 	int aspect_ratio;
211 	int gop_size;
212 	unsigned int ipb:1;
213 	unsigned int closed_gop:1;
214 	unsigned int repeat_seqhead:1;
215 	unsigned int seq_header_enable:1;
216 	unsigned int gop_header_enable:1;
217 	unsigned int dvd_mode:1;
218 	unsigned int interlace_coding:1;
219 
220 	/* Motion detection */
221 	unsigned int modet_enable:1;
222 	struct {
223 		unsigned int enable:1;
224 		int pixel_threshold;
225 		int motion_threshold;
226 		int mb_threshold;
227 	} modet[4];
228 	unsigned char modet_map[1624];
229 	unsigned char active_map[216];
230 	u32 modet_event_status;
231 
232 	/* Video streaming */
233 	struct mutex queue_lock;
234 	struct vb2_queue vidq;
235 	enum go7007_parser_state state;
236 	int parse_length;
237 	u16 modet_word;
238 	int seen_frame;
239 	u32 next_seq;
240 	struct list_head vidq_active;
241 	wait_queue_head_t frame_waitq;
242 	struct go7007_buffer *active_buf;
243 
244 	/* Audio streaming */
245 	void (*audio_deliver)(struct go7007 *go, u8 *buf, int length);
246 	void *snd_context;
247 
248 	/* I2C */
249 	int i2c_adapter_online;
250 	struct i2c_adapter i2c_adapter;
251 
252 	/* HPI driver */
253 	const struct go7007_hpi_ops *hpi_ops;
254 	void *hpi_context;
255 	int interrupt_available;
256 	wait_queue_head_t interrupt_waitq;
257 	unsigned short interrupt_value;
258 	unsigned short interrupt_data;
259 };
260 
261 static inline struct go7007 *to_go7007(struct v4l2_device *v4l2_dev)
262 {
263 	return container_of(v4l2_dev, struct go7007, v4l2_dev);
264 }
265 
266 /* All of these must be called with the hpi_lock mutex held! */
267 #define go7007_interface_reset(go) \
268 			((go)->hpi_ops->interface_reset(go))
269 #define	go7007_write_interrupt(go, x, y) \
270 			((go)->hpi_ops->write_interrupt)((go), (x), (y))
271 #define go7007_stream_start(go) \
272 			((go)->hpi_ops->stream_start(go))
273 #define go7007_stream_stop(go) \
274 			((go)->hpi_ops->stream_stop(go))
275 #define	go7007_send_firmware(go, x, y) \
276 			((go)->hpi_ops->send_firmware)((go), (x), (y))
277 #define go7007_write_addr(go, x, y) \
278 			((go)->hpi_ops->write_interrupt)((go), (x)|0x8000, (y))
279 
280 /* go7007-driver.c */
281 int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data);
282 int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data);
283 int go7007_boot_encoder(struct go7007 *go, int init_i2c);
284 int go7007_reset_encoder(struct go7007 *go);
285 int go7007_register_encoder(struct go7007 *go, unsigned num_i2c_devs);
286 int go7007_start_encoder(struct go7007 *go);
287 void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length);
288 struct go7007 *go7007_alloc(const struct go7007_board_info *board,
289 					struct device *dev);
290 void go7007_update_board(struct go7007 *go);
291 
292 /* go7007-fw.c */
293 int go7007_construct_fw_image(struct go7007 *go, u8 **fw, int *fwlen);
294 
295 /* go7007-i2c.c */
296 int go7007_i2c_init(struct go7007 *go);
297 int go7007_i2c_remove(struct go7007 *go);
298 
299 /* go7007-v4l2.c */
300 int go7007_v4l2_init(struct go7007 *go);
301 int go7007_v4l2_ctrl_init(struct go7007 *go);
302 void go7007_v4l2_remove(struct go7007 *go);
303 
304 /* snd-go7007.c */
305 int go7007_snd_init(struct go7007 *go);
306 int go7007_snd_remove(struct go7007 *go);
307