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