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