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  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16  */
17 
18 /*
19  * This is the private include file for the go7007 driver.  It should not
20  * be included by anybody but the driver itself, and especially not by
21  * user-space applications.
22  */
23 
24 #include <media/v4l2-device.h>
25 
26 struct go7007;
27 
28 /* IDs to activate board-specific support code */
29 #define GO7007_BOARDID_MATRIX_II	0
30 #define GO7007_BOARDID_MATRIX_RELOAD	1
31 #define GO7007_BOARDID_STAR_TREK	2
32 #define GO7007_BOARDID_PCI_VOYAGER	3
33 #define GO7007_BOARDID_XMEN		4
34 #define GO7007_BOARDID_XMEN_II		5
35 #define GO7007_BOARDID_XMEN_III		6
36 #define GO7007_BOARDID_MATRIX_REV	7
37 #define GO7007_BOARDID_PX_M402U		16
38 #define GO7007_BOARDID_PX_TV402U_ANY	17 /* need to check tuner model */
39 #define GO7007_BOARDID_PX_TV402U_NA	18 /* detected NTSC tuner */
40 #define GO7007_BOARDID_PX_TV402U_EU	19 /* detected PAL tuner */
41 #define GO7007_BOARDID_PX_TV402U_JP	20 /* detected NTSC-J tuner */
42 #define GO7007_BOARDID_LIFEVIEW_LR192	21 /* TV Walker Ultra */
43 #define GO7007_BOARDID_ENDURA		22
44 #define GO7007_BOARDID_ADLINK_MPG24	23
45 #define GO7007_BOARDID_SENSORAY_2250	24 /* Sensoray 2250/2251 */
46 
47 /* Various characteristics of each board */
48 #define GO7007_BOARD_HAS_AUDIO		(1<<0)
49 #define GO7007_BOARD_USE_ONBOARD_I2C	(1<<1)
50 #define GO7007_BOARD_HAS_TUNER		(1<<2)
51 
52 /* Characteristics of sensor devices */
53 #define GO7007_SENSOR_VALID_POLAR	(1<<0)
54 #define GO7007_SENSOR_HREF_POLAR	(1<<1)
55 #define GO7007_SENSOR_VREF_POLAR	(1<<2)
56 #define GO7007_SENSOR_FIELD_ID_POLAR	(1<<3)
57 #define GO7007_SENSOR_BIT_WIDTH		(1<<4)
58 #define GO7007_SENSOR_VALID_ENABLE	(1<<5)
59 #define GO7007_SENSOR_656		(1<<6)
60 #define GO7007_SENSOR_CONFIG_MASK	0x7f
61 #define GO7007_SENSOR_TV		(1<<7)
62 #define GO7007_SENSOR_VBI		(1<<8)
63 #define GO7007_SENSOR_SCALING		(1<<9)
64 
65 /* Characteristics of audio sensor devices */
66 #define GO7007_AUDIO_I2S_MODE_1		(1)
67 #define GO7007_AUDIO_I2S_MODE_2		(2)
68 #define GO7007_AUDIO_I2S_MODE_3		(3)
69 #define GO7007_AUDIO_BCLK_POLAR		(1<<2)
70 #define GO7007_AUDIO_WORD_14		(14<<4)
71 #define GO7007_AUDIO_WORD_16		(16<<4)
72 #define GO7007_AUDIO_ONE_CHANNEL	(1<<11)
73 #define GO7007_AUDIO_I2S_MASTER		(1<<16)
74 #define GO7007_AUDIO_OKI_MODE		(1<<17)
75 
76 struct go7007_board_info {
77 	char *firmware;
78 	unsigned int flags;
79 	int hpi_buffer_cap;
80 	unsigned int sensor_flags;
81 	int sensor_width;
82 	int sensor_height;
83 	int sensor_framerate;
84 	int sensor_h_offset;
85 	int sensor_v_offset;
86 	unsigned int audio_flags;
87 	int audio_rate;
88 	int audio_bclk_div;
89 	int audio_main_div;
90 	int num_i2c_devs;
91 	struct {
92 		const char *type;
93 		int id;
94 		int addr;
95 	} i2c_devs[4];
96 	int num_inputs;
97 	struct {
98 		int video_input;
99 		int audio_input;
100 		char *name;
101 	} inputs[4];
102 };
103 
104 struct go7007_hpi_ops {
105 	int (*interface_reset)(struct go7007 *go);
106 	int (*write_interrupt)(struct go7007 *go, int addr, int data);
107 	int (*read_interrupt)(struct go7007 *go);
108 	int (*stream_start)(struct go7007 *go);
109 	int (*stream_stop)(struct go7007 *go);
110 	int (*send_firmware)(struct go7007 *go, u8 *data, int len);
111 	int (*send_command)(struct go7007 *go, unsigned int cmd, void *arg);
112 };
113 
114 /* The video buffer size must be a multiple of PAGE_SIZE */
115 #define	GO7007_BUF_PAGES	(128 * 1024 / PAGE_SIZE)
116 #define	GO7007_BUF_SIZE		(GO7007_BUF_PAGES << PAGE_SHIFT)
117 
118 struct go7007_buffer {
119 	struct go7007 *go; /* Reverse reference for VMA ops */
120 	int index; /* Reverse reference for DQBUF */
121 	enum { BUF_STATE_IDLE, BUF_STATE_QUEUED, BUF_STATE_DONE } state;
122 	u32 seq;
123 	struct timeval timestamp;
124 	struct list_head stream;
125 	struct page *pages[GO7007_BUF_PAGES + 1]; /* extra for straddling */
126 	unsigned long user_addr;
127 	unsigned int page_count;
128 	unsigned int offset;
129 	unsigned int bytesused;
130 	unsigned int frame_offset;
131 	u32 modet_active;
132 	int mapped;
133 };
134 
135 struct go7007_file {
136 	struct go7007 *go;
137 	struct mutex lock;
138 	int buf_count;
139 	struct go7007_buffer *bufs;
140 };
141 
142 #define	GO7007_FORMAT_MJPEG	0
143 #define GO7007_FORMAT_MPEG4	1
144 #define GO7007_FORMAT_MPEG1	2
145 #define GO7007_FORMAT_MPEG2	3
146 #define GO7007_FORMAT_H263	4
147 
148 #define GO7007_RATIO_1_1	0
149 #define GO7007_RATIO_4_3	1
150 #define GO7007_RATIO_16_9	2
151 
152 enum go7007_parser_state {
153 	STATE_DATA,
154 	STATE_00,
155 	STATE_00_00,
156 	STATE_00_00_01,
157 	STATE_FF,
158 	STATE_VBI_LEN_A,
159 	STATE_VBI_LEN_B,
160 	STATE_MODET_MAP,
161 	STATE_UNPARSED,
162 };
163 
164 struct go7007 {
165 	struct device *dev;
166 	struct go7007_board_info *board_info;
167 	unsigned int board_id;
168 	int tuner_type;
169 	int channel_number; /* for multi-channel boards like Adlink PCI-MPG24 */
170 	char name[64];
171 	struct video_device *video_dev;
172 	struct v4l2_device v4l2_dev;
173 	int ref_count;
174 	enum { STATUS_INIT, STATUS_ONLINE, STATUS_SHUTDOWN } status;
175 	spinlock_t spinlock;
176 	struct mutex hw_lock;
177 	int streaming;
178 	int in_use;
179 	int audio_enabled;
180 
181 	/* Video input */
182 	int input;
183 	enum { GO7007_STD_NTSC, GO7007_STD_PAL, GO7007_STD_OTHER } standard;
184 	int sensor_framerate;
185 	int width;
186 	int height;
187 	int encoder_h_offset;
188 	int encoder_v_offset;
189 	unsigned int encoder_h_halve:1;
190 	unsigned int encoder_v_halve:1;
191 	unsigned int encoder_subsample:1;
192 
193 	/* Encoder config */
194 	int format;
195 	int bitrate;
196 	int fps_scale;
197 	int pali;
198 	int aspect_ratio;
199 	int gop_size;
200 	unsigned int ipb:1;
201 	unsigned int closed_gop:1;
202 	unsigned int repeat_seqhead:1;
203 	unsigned int seq_header_enable:1;
204 	unsigned int gop_header_enable:1;
205 	unsigned int dvd_mode:1;
206 	unsigned int interlace_coding:1;
207 
208 	/* Motion detection */
209 	unsigned int modet_enable:1;
210 	struct {
211 		unsigned int enable:1;
212 		int pixel_threshold;
213 		int motion_threshold;
214 		int mb_threshold;
215 	} modet[4];
216 	unsigned char modet_map[1624];
217 	unsigned char active_map[216];
218 
219 	/* Video streaming */
220 	struct go7007_buffer *active_buf;
221 	enum go7007_parser_state state;
222 	int parse_length;
223 	u16 modet_word;
224 	int seen_frame;
225 	u32 next_seq;
226 	struct list_head stream;
227 	wait_queue_head_t frame_waitq;
228 
229 	/* Audio streaming */
230 	void (*audio_deliver)(struct go7007 *go, u8 *buf, int length);
231 	void *snd_context;
232 
233 	/* I2C */
234 	int i2c_adapter_online;
235 	struct i2c_adapter i2c_adapter;
236 
237 	/* HPI driver */
238 	struct go7007_hpi_ops *hpi_ops;
239 	void *hpi_context;
240 	int interrupt_available;
241 	wait_queue_head_t interrupt_waitq;
242 	unsigned short interrupt_value;
243 	unsigned short interrupt_data;
244 };
245 
to_go7007(struct v4l2_device * v4l2_dev)246 static inline struct go7007 *to_go7007(struct v4l2_device *v4l2_dev)
247 {
248 	return container_of(v4l2_dev, struct go7007, v4l2_dev);
249 }
250 
251 /* All of these must be called with the hpi_lock mutex held! */
252 #define go7007_interface_reset(go) \
253 			((go)->hpi_ops->interface_reset(go))
254 #define	go7007_write_interrupt(go, x, y) \
255 			((go)->hpi_ops->write_interrupt)((go), (x), (y))
256 #define go7007_stream_start(go) \
257 			((go)->hpi_ops->stream_start(go))
258 #define go7007_stream_stop(go) \
259 			((go)->hpi_ops->stream_stop(go))
260 #define	go7007_send_firmware(go, x, y) \
261 			((go)->hpi_ops->send_firmware)((go), (x), (y))
262 #define go7007_write_addr(go, x, y) \
263 			((go)->hpi_ops->write_interrupt)((go), (x)|0x8000, (y))
264 
265 /* go7007-driver.c */
266 int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data);
267 int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data);
268 int go7007_boot_encoder(struct go7007 *go, int init_i2c);
269 int go7007_reset_encoder(struct go7007 *go);
270 int go7007_register_encoder(struct go7007 *go);
271 int go7007_start_encoder(struct go7007 *go);
272 void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length);
273 struct go7007 *go7007_alloc(struct go7007_board_info *board,
274 					struct device *dev);
275 void go7007_remove(struct go7007 *go);
276 
277 /* go7007-fw.c */
278 int go7007_construct_fw_image(struct go7007 *go, u8 **fw, int *fwlen);
279 
280 /* go7007-i2c.c */
281 int go7007_i2c_init(struct go7007 *go);
282 int go7007_i2c_remove(struct go7007 *go);
283 
284 /* go7007-v4l2.c */
285 int go7007_v4l2_init(struct go7007 *go);
286 void go7007_v4l2_remove(struct go7007 *go);
287 
288 /* snd-go7007.c */
289 int go7007_snd_init(struct go7007 *go);
290 int go7007_snd_remove(struct go7007 *go);
291