1 /*
2  * zoran - Iomega Buz driver
3  *
4  * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
5  *
6  * based on
7  *
8  * zoran.0.0.3 Copyright (C) 1998 Dave Perks <dperks@ibm.net>
9  *
10  * and
11  *
12  * bttv - Bt848 frame grabber driver
13  * Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
14  *                        & Marcus Metzler (mocm@thp.uni-koeln.de)
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  */
30 
31 #ifndef _BUZ_H_
32 #define _BUZ_H_
33 
34 #include <media/v4l2-device.h>
35 
36 struct zoran_sync {
37 	unsigned long frame;	/* number of buffer that has been free'd */
38 	unsigned long length;	/* number of code bytes in buffer (capture only) */
39 	unsigned long seq;	/* frame sequence number */
40 	struct timeval timestamp;	/* timestamp */
41 };
42 
43 
44 #define MAJOR_VERSION 0		/* driver major version */
45 #define MINOR_VERSION 10	/* driver minor version */
46 #define RELEASE_VERSION 0	/* release version */
47 
48 #define ZORAN_NAME    "ZORAN"	/* name of the device */
49 
50 #define ZR_DEVNAME(zr) ((zr)->name)
51 
52 #define   BUZ_MAX_WIDTH   (zr->timing->Wa)
53 #define   BUZ_MAX_HEIGHT  (zr->timing->Ha)
54 #define   BUZ_MIN_WIDTH    32	/* never display less than 32 pixels */
55 #define   BUZ_MIN_HEIGHT   24	/* never display less than 24 rows */
56 
57 #define BUZ_NUM_STAT_COM    4
58 #define BUZ_MASK_STAT_COM   3
59 
60 #define BUZ_MAX_FRAME     256	/* Must be a power of 2 */
61 #define BUZ_MASK_FRAME    255	/* Must be BUZ_MAX_FRAME-1 */
62 
63 #define BUZ_MAX_INPUT       16
64 
65 #if VIDEO_MAX_FRAME <= 32
66 #   define   V4L_MAX_FRAME   32
67 #elif VIDEO_MAX_FRAME <= 64
68 #   define   V4L_MAX_FRAME   64
69 #else
70 #   error   "Too many video frame buffers to handle"
71 #endif
72 #define   V4L_MASK_FRAME   (V4L_MAX_FRAME - 1)
73 
74 #define MAX_FRAME (BUZ_MAX_FRAME > VIDEO_MAX_FRAME ? BUZ_MAX_FRAME : VIDEO_MAX_FRAME)
75 
76 #include "zr36057.h"
77 
78 enum card_type {
79 	UNKNOWN = -1,
80 
81 	/* Pinnacle/Miro */
82 	DC10_old,		/* DC30 like */
83 	DC10_new,		/* DC10plus like */
84 	DC10plus,
85 	DC30,
86 	DC30plus,
87 
88 	/* Linux Media Labs */
89 	LML33,
90 	LML33R10,
91 
92 	/* Iomega */
93 	BUZ,
94 
95 	/* AverMedia */
96 	AVS6EYES,
97 
98 	/* total number of cards */
99 	NUM_CARDS
100 };
101 
102 enum zoran_codec_mode {
103 	BUZ_MODE_IDLE,		/* nothing going on */
104 	BUZ_MODE_MOTION_COMPRESS,	/* grabbing frames */
105 	BUZ_MODE_MOTION_DECOMPRESS,	/* playing frames */
106 	BUZ_MODE_STILL_COMPRESS,	/* still frame conversion */
107 	BUZ_MODE_STILL_DECOMPRESS	/* still frame conversion */
108 };
109 
110 enum zoran_buffer_state {
111 	BUZ_STATE_USER,		/* buffer is owned by application */
112 	BUZ_STATE_PEND,		/* buffer is queued in pend[] ready to feed to I/O */
113 	BUZ_STATE_DMA,		/* buffer is queued in dma[] for I/O */
114 	BUZ_STATE_DONE		/* buffer is ready to return to application */
115 };
116 
117 enum zoran_map_mode {
118 	ZORAN_MAP_MODE_RAW,
119 	ZORAN_MAP_MODE_JPG_REC,
120 #define ZORAN_MAP_MODE_JPG ZORAN_MAP_MODE_JPG_REC
121 	ZORAN_MAP_MODE_JPG_PLAY,
122 };
123 
124 enum gpio_type {
125 	ZR_GPIO_JPEG_SLEEP = 0,
126 	ZR_GPIO_JPEG_RESET,
127 	ZR_GPIO_JPEG_FRAME,
128 	ZR_GPIO_VID_DIR,
129 	ZR_GPIO_VID_EN,
130 	ZR_GPIO_VID_RESET,
131 	ZR_GPIO_CLK_SEL1,
132 	ZR_GPIO_CLK_SEL2,
133 	ZR_GPIO_MAX,
134 };
135 
136 enum gpcs_type {
137 	GPCS_JPEG_RESET = 0,
138 	GPCS_JPEG_START,
139 	GPCS_MAX,
140 };
141 
142 struct zoran_format {
143 	char *name;
144 	__u32 fourcc;
145 	int colorspace;
146 	int depth;
147 	__u32 flags;
148 	__u32 vfespfr;
149 };
150 /* flags */
151 #define ZORAN_FORMAT_COMPRESSED 1<<0
152 #define ZORAN_FORMAT_OVERLAY    1<<1
153 #define ZORAN_FORMAT_CAPTURE	1<<2
154 #define ZORAN_FORMAT_PLAYBACK	1<<3
155 
156 /* overlay-settings */
157 struct zoran_overlay_settings {
158 	int is_set;
159 	int x, y, width, height;	/* position */
160 	int clipcount;		/* position and number of clips */
161 	const struct zoran_format *format;	/* overlay format */
162 };
163 
164 /* v4l-capture settings */
165 struct zoran_v4l_settings {
166 	int width, height, bytesperline;	/* capture size */
167 	const struct zoran_format *format;	/* capture format */
168 };
169 
170 /* jpg-capture/-playback settings */
171 struct zoran_jpg_settings {
172 	int decimation;		/* this bit is used to set everything to default */
173 	int HorDcm, VerDcm, TmpDcm;	/* capture decimation settings (TmpDcm=1 means both fields) */
174 	int field_per_buff, odd_even;	/* field-settings (odd_even=1 (+TmpDcm=1) means top-field-first) */
175 	int img_x, img_y, img_width, img_height;	/* crop settings (subframe capture) */
176 	struct v4l2_jpegcompression jpg_comp;	/* JPEG-specific capture settings */
177 };
178 
179 struct zoran_mapping {
180 	struct file *file;
181 	int count;
182 };
183 
184 struct zoran_buffer {
185 	struct zoran_mapping *map;
186 	enum zoran_buffer_state state;	/* state: unused/pending/dma/done */
187 	struct zoran_sync bs;		/* DONE: info to return to application */
188 	union {
189 		struct {
190 			__le32 *frag_tab;	/* addresses of frag table */
191 			u32 frag_tab_bus;	/* same value cached to save time in ISR */
192 		} jpg;
193 		struct {
194 			char *fbuffer;		/* virtual address of frame buffer */
195 			unsigned long fbuffer_phys;/* physical address of frame buffer */
196 			unsigned long fbuffer_bus;/* bus address of frame buffer */
197 		} v4l;
198 	};
199 };
200 
201 enum zoran_lock_activity {
202 	ZORAN_FREE,		/* free for use */
203 	ZORAN_ACTIVE,		/* active but unlocked */
204 	ZORAN_LOCKED,		/* locked */
205 };
206 
207 /* buffer collections */
208 struct zoran_buffer_col {
209 	enum zoran_lock_activity active;	/* feature currently in use? */
210 	unsigned int num_buffers, buffer_size;
211 	struct zoran_buffer buffer[MAX_FRAME];	/* buffers */
212 	u8 allocated;		/* Flag if buffers are allocated  */
213 	u8 need_contiguous;	/* Flag if contiguous buffers are needed */
214 	/* only applies to jpg buffers, raw buffers are always contiguous */
215 };
216 
217 struct zoran;
218 
219 /* zoran_fh contains per-open() settings */
220 struct zoran_fh {
221 	struct zoran *zr;
222 
223 	enum zoran_map_mode map_mode;		/* Flag which bufferset will map by next mmap() */
224 
225 	struct zoran_overlay_settings overlay_settings;
226 	u32 *overlay_mask;			/* overlay mask */
227 	enum zoran_lock_activity overlay_active;/* feature currently in use? */
228 
229 	struct zoran_buffer_col buffers;	/* buffers' info */
230 
231 	struct zoran_v4l_settings v4l_settings;	/* structure with a lot of things to play with */
232 	struct zoran_jpg_settings jpg_settings;	/* structure with a lot of things to play with */
233 };
234 
235 struct card_info {
236 	enum card_type type;
237 	char name[32];
238 	const char *i2c_decoder;	/* i2c decoder device */
239 	const unsigned short *addrs_decoder;
240 	const char *i2c_encoder;	/* i2c encoder device */
241 	const unsigned short *addrs_encoder;
242 	u16 video_vfe, video_codec;			/* videocodec types */
243 	u16 audio_chip;					/* audio type */
244 
245 	int inputs;		/* number of video inputs */
246 	struct input {
247 		int muxsel;
248 		char name[32];
249 	} input[BUZ_MAX_INPUT];
250 
251 	v4l2_std_id norms;
252 	struct tvnorm *tvn[3];	/* supported TV norms */
253 
254 	u32 jpeg_int;		/* JPEG interrupt */
255 	u32 vsync_int;		/* VSYNC interrupt */
256 	s8 gpio[ZR_GPIO_MAX];
257 	u8 gpcs[GPCS_MAX];
258 
259 	struct vfe_polarity vfe_pol;
260 	u8 gpio_pol[ZR_GPIO_MAX];
261 
262 	/* is the /GWS line connected? */
263 	u8 gws_not_connected;
264 
265 	/* avs6eyes mux setting */
266 	u8 input_mux;
267 
268 	void (*init) (struct zoran * zr);
269 };
270 
271 struct zoran {
272 	struct v4l2_device v4l2_dev;
273 	struct video_device *video_dev;
274 
275 	struct i2c_adapter i2c_adapter;	/* */
276 	struct i2c_algo_bit_data i2c_algo;	/* */
277 	u32 i2cbr;
278 
279 	struct v4l2_subdev *decoder;	/* video decoder sub-device */
280 	struct v4l2_subdev *encoder;	/* video encoder sub-device */
281 
282 	struct videocodec *codec;	/* video codec */
283 	struct videocodec *vfe;	/* video front end */
284 
285 	struct mutex resource_lock;	/* prevent evil stuff */
286 	struct mutex other_lock;	/* please merge with above */
287 
288 	u8 initialized;		/* flag if zoran has been correctly initialized */
289 	int user;		/* number of current users */
290 	struct card_info card;
291 	struct tvnorm *timing;
292 
293 	unsigned short id;	/* number of this device */
294 	char name[32];		/* name of this device */
295 	struct pci_dev *pci_dev;	/* PCI device */
296 	unsigned char revision;	/* revision of zr36057 */
297 	unsigned char __iomem *zr36057_mem;/* pointer to mapped IO memory */
298 
299 	spinlock_t spinlock;	/* Spinlock */
300 
301 	/* Video for Linux parameters */
302 	int input;	/* card's norm and input */
303 	v4l2_std_id norm;
304 
305 	/* Current buffer params */
306 	void    *vbuf_base;
307 	int     vbuf_height, vbuf_width;
308 	int     vbuf_depth;
309 	int     vbuf_bytesperline;
310 
311 	struct zoran_overlay_settings overlay_settings;
312 	u32 *overlay_mask;	/* overlay mask */
313 	enum zoran_lock_activity overlay_active;	/* feature currently in use? */
314 
315 	wait_queue_head_t v4l_capq;
316 
317 	int v4l_overlay_active;	/* Overlay grab is activated */
318 	int v4l_memgrab_active;	/* Memory grab is activated */
319 
320 	int v4l_grab_frame;	/* Frame number being currently grabbed */
321 #define NO_GRAB_ACTIVE (-1)
322 	unsigned long v4l_grab_seq;	/* Number of frames grabbed */
323 	struct zoran_v4l_settings v4l_settings;	/* structure with a lot of things to play with */
324 
325 	/* V4L grab queue of frames pending */
326 	unsigned long v4l_pend_head;
327 	unsigned long v4l_pend_tail;
328 	unsigned long v4l_sync_tail;
329 	int v4l_pend[V4L_MAX_FRAME];
330 	struct zoran_buffer_col v4l_buffers;	/* V4L buffers' info */
331 
332 	/* Buz MJPEG parameters */
333 	enum zoran_codec_mode codec_mode;	/* status of codec */
334 	struct zoran_jpg_settings jpg_settings;	/* structure with a lot of things to play with */
335 
336 	wait_queue_head_t jpg_capq;	/* wait here for grab to finish */
337 
338 	/* grab queue counts/indices, mask with BUZ_MASK_STAT_COM before using as index */
339 	/* (dma_head - dma_tail) is number active in DMA, must be <= BUZ_NUM_STAT_COM */
340 	/* (value & BUZ_MASK_STAT_COM) corresponds to index in stat_com table */
341 	unsigned long jpg_que_head;	/* Index where to put next buffer which is queued */
342 	unsigned long jpg_dma_head;	/* Index of next buffer which goes into stat_com  */
343 	unsigned long jpg_dma_tail;	/* Index of last buffer in stat_com               */
344 	unsigned long jpg_que_tail;	/* Index of last buffer in queue                  */
345 	unsigned long jpg_seq_num;	/* count of frames since grab/play started        */
346 	unsigned long jpg_err_seq;	/* last seq_num before error                      */
347 	unsigned long jpg_err_shift;
348 	unsigned long jpg_queued_num;	/* count of frames queued since grab/play started */
349 
350 	/* zr36057's code buffer table */
351 	__le32 *stat_com;		/* stat_com[i] is indexed by dma_head/tail & BUZ_MASK_STAT_COM */
352 
353 	/* (value & BUZ_MASK_FRAME) corresponds to index in pend[] queue */
354 	int jpg_pend[BUZ_MAX_FRAME];
355 
356 	/* array indexed by frame number */
357 	struct zoran_buffer_col jpg_buffers;	/* MJPEG buffers' info */
358 
359 	/* Additional stuff for testing */
360 #ifdef CONFIG_PROC_FS
361 	struct proc_dir_entry *zoran_proc;
362 #else
363 	void *zoran_proc;
364 #endif
365 	int testing;
366 	int jpeg_error;
367 	int intr_counter_GIRQ1;
368 	int intr_counter_GIRQ0;
369 	int intr_counter_CodRepIRQ;
370 	int intr_counter_JPEGRepIRQ;
371 	int field_counter;
372 	int IRQ1_in;
373 	int IRQ1_out;
374 	int JPEG_in;
375 	int JPEG_out;
376 	int JPEG_0;
377 	int JPEG_1;
378 	int END_event_missed;
379 	int JPEG_missed;
380 	int JPEG_error;
381 	int num_errors;
382 	int JPEG_max_missed;
383 	int JPEG_min_missed;
384 
385 	u32 last_isr;
386 	unsigned long frame_num;
387 
388 	wait_queue_head_t test_q;
389 };
390 
to_zoran(struct v4l2_device * v4l2_dev)391 static inline struct zoran *to_zoran(struct v4l2_device *v4l2_dev)
392 {
393 	return container_of(v4l2_dev, struct zoran, v4l2_dev);
394 }
395 
396 /* There was something called _ALPHA_BUZ that used the PCI address instead of
397  * the kernel iomapped address for btread/btwrite.  */
398 #define btwrite(dat,adr)    writel((dat), zr->zr36057_mem+(adr))
399 #define btread(adr)         readl(zr->zr36057_mem+(adr))
400 
401 #define btand(dat,adr)      btwrite((dat) & btread(adr), adr)
402 #define btor(dat,adr)       btwrite((dat) | btread(adr), adr)
403 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
404 
405 #endif
406