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 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 
74 enum 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 
98 enum 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 
106 enum 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 
113 enum 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 
120 enum 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 
132 enum gpcs_type {
133 	GPCS_JPEG_RESET = 0,
134 	GPCS_JPEG_START,
135 	GPCS_MAX,
136 };
137 
138 struct 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 */
153 struct 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 */
161 struct 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 */
167 struct 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 
175 struct zoran_mapping {
176 	struct file *file;
177 	int count;
178 };
179 
180 struct zoran_buffer {
181 	struct zoran_mapping *map;
182 	enum zoran_buffer_state state;	/* state: unused/pending/dma/done */
183 	struct zoran_sync bs;		/* DONE: info to return to application */
184 	union {
185 		struct {
186 			__le32 *frag_tab;	/* addresses of frag table */
187 			u32 frag_tab_bus;	/* same value cached to save time in ISR */
188 		} jpg;
189 		struct {
190 			char *fbuffer;		/* virtual address of frame buffer */
191 			unsigned long fbuffer_phys;/* physical address of frame buffer */
192 			unsigned long fbuffer_bus;/* bus address of frame buffer */
193 		} v4l;
194 	};
195 };
196 
197 enum zoran_lock_activity {
198 	ZORAN_FREE,		/* free for use */
199 	ZORAN_ACTIVE,		/* active but unlocked */
200 	ZORAN_LOCKED,		/* locked */
201 };
202 
203 /* buffer collections */
204 struct zoran_buffer_col {
205 	enum zoran_lock_activity active;	/* feature currently in use? */
206 	unsigned int num_buffers, buffer_size;
207 	struct zoran_buffer buffer[MAX_FRAME];	/* buffers */
208 	u8 allocated;		/* Flag if buffers are allocated  */
209 	u8 need_contiguous;	/* Flag if contiguous buffers are needed */
210 	/* only applies to jpg buffers, raw buffers are always contiguous */
211 };
212 
213 struct zoran;
214 
215 /* zoran_fh contains per-open() settings */
216 struct zoran_fh {
217 	struct zoran *zr;
218 
219 	enum zoran_map_mode map_mode;		/* Flag which bufferset will map by next mmap() */
220 
221 	struct zoran_overlay_settings overlay_settings;
222 	u32 *overlay_mask;			/* overlay mask */
223 	enum zoran_lock_activity overlay_active;/* feature currently in use? */
224 
225 	struct zoran_buffer_col buffers;	/* buffers' info */
226 
227 	struct zoran_v4l_settings v4l_settings;	/* structure with a lot of things to play with */
228 	struct zoran_jpg_settings jpg_settings;	/* structure with a lot of things to play with */
229 };
230 
231 struct card_info {
232 	enum card_type type;
233 	char name[32];
234 	const char *i2c_decoder;	/* i2c decoder device */
235 	const unsigned short *addrs_decoder;
236 	const char *i2c_encoder;	/* i2c encoder device */
237 	const unsigned short *addrs_encoder;
238 	u16 video_vfe, video_codec;			/* videocodec types */
239 	u16 audio_chip;					/* audio type */
240 
241 	int inputs;		/* number of video inputs */
242 	struct input {
243 		int muxsel;
244 		char name[32];
245 	} input[BUZ_MAX_INPUT];
246 
247 	v4l2_std_id norms;
248 	struct tvnorm *tvn[3];	/* supported TV norms */
249 
250 	u32 jpeg_int;		/* JPEG interrupt */
251 	u32 vsync_int;		/* VSYNC interrupt */
252 	s8 gpio[ZR_GPIO_MAX];
253 	u8 gpcs[GPCS_MAX];
254 
255 	struct vfe_polarity vfe_pol;
256 	u8 gpio_pol[ZR_GPIO_MAX];
257 
258 	/* is the /GWS line connected? */
259 	u8 gws_not_connected;
260 
261 	/* avs6eyes mux setting */
262 	u8 input_mux;
263 
264 	void (*init) (struct zoran * zr);
265 };
266 
267 struct zoran {
268 	struct v4l2_device v4l2_dev;
269 	struct video_device *video_dev;
270 
271 	struct i2c_adapter i2c_adapter;	/* */
272 	struct i2c_algo_bit_data i2c_algo;	/* */
273 	u32 i2cbr;
274 
275 	struct v4l2_subdev *decoder;	/* video decoder sub-device */
276 	struct v4l2_subdev *encoder;	/* video encoder sub-device */
277 
278 	struct videocodec *codec;	/* video codec */
279 	struct videocodec *vfe;	/* video front end */
280 
281 	struct mutex resource_lock;	/* prevent evil stuff */
282 	struct mutex other_lock;	/* please merge with above */
283 
284 	u8 initialized;		/* flag if zoran has been correctly initialized */
285 	int user;		/* number of current users */
286 	struct card_info card;
287 	struct tvnorm *timing;
288 
289 	unsigned short id;	/* number of this device */
290 	char name[32];		/* name of this device */
291 	struct pci_dev *pci_dev;	/* PCI device */
292 	unsigned char revision;	/* revision of zr36057 */
293 	unsigned char __iomem *zr36057_mem;/* pointer to mapped IO memory */
294 
295 	spinlock_t spinlock;	/* Spinlock */
296 
297 	/* Video for Linux parameters */
298 	int input;	/* card's norm and input */
299 	v4l2_std_id norm;
300 
301 	/* Current buffer params */
302 	void    *vbuf_base;
303 	int     vbuf_height, vbuf_width;
304 	int     vbuf_depth;
305 	int     vbuf_bytesperline;
306 
307 	struct zoran_overlay_settings overlay_settings;
308 	u32 *overlay_mask;	/* overlay mask */
309 	enum zoran_lock_activity overlay_active;	/* feature currently in use? */
310 
311 	wait_queue_head_t v4l_capq;
312 
313 	int v4l_overlay_active;	/* Overlay grab is activated */
314 	int v4l_memgrab_active;	/* Memory grab is activated */
315 
316 	int v4l_grab_frame;	/* Frame number being currently grabbed */
317 #define NO_GRAB_ACTIVE (-1)
318 	unsigned long v4l_grab_seq;	/* Number of frames grabbed */
319 	struct zoran_v4l_settings v4l_settings;	/* structure with a lot of things to play with */
320 
321 	/* V4L grab queue of frames pending */
322 	unsigned long v4l_pend_head;
323 	unsigned long v4l_pend_tail;
324 	unsigned long v4l_sync_tail;
325 	int v4l_pend[V4L_MAX_FRAME];
326 	struct zoran_buffer_col v4l_buffers;	/* V4L buffers' info */
327 
328 	/* Buz MJPEG parameters */
329 	enum zoran_codec_mode codec_mode;	/* status of codec */
330 	struct zoran_jpg_settings jpg_settings;	/* structure with a lot of things to play with */
331 
332 	wait_queue_head_t jpg_capq;	/* wait here for grab to finish */
333 
334 	/* grab queue counts/indices, mask with BUZ_MASK_STAT_COM before using as index */
335 	/* (dma_head - dma_tail) is number active in DMA, must be <= BUZ_NUM_STAT_COM */
336 	/* (value & BUZ_MASK_STAT_COM) corresponds to index in stat_com table */
337 	unsigned long jpg_que_head;	/* Index where to put next buffer which is queued */
338 	unsigned long jpg_dma_head;	/* Index of next buffer which goes into stat_com  */
339 	unsigned long jpg_dma_tail;	/* Index of last buffer in stat_com               */
340 	unsigned long jpg_que_tail;	/* Index of last buffer in queue                  */
341 	unsigned long jpg_seq_num;	/* count of frames since grab/play started        */
342 	unsigned long jpg_err_seq;	/* last seq_num before error                      */
343 	unsigned long jpg_err_shift;
344 	unsigned long jpg_queued_num;	/* count of frames queued since grab/play started */
345 
346 	/* zr36057's code buffer table */
347 	__le32 *stat_com;		/* stat_com[i] is indexed by dma_head/tail & BUZ_MASK_STAT_COM */
348 
349 	/* (value & BUZ_MASK_FRAME) corresponds to index in pend[] queue */
350 	int jpg_pend[BUZ_MAX_FRAME];
351 
352 	/* array indexed by frame number */
353 	struct zoran_buffer_col jpg_buffers;	/* MJPEG buffers' info */
354 
355 	/* Additional stuff for testing */
356 #ifdef CONFIG_PROC_FS
357 	struct proc_dir_entry *zoran_proc;
358 #else
359 	void *zoran_proc;
360 #endif
361 	int testing;
362 	int jpeg_error;
363 	int intr_counter_GIRQ1;
364 	int intr_counter_GIRQ0;
365 	int intr_counter_CodRepIRQ;
366 	int intr_counter_JPEGRepIRQ;
367 	int field_counter;
368 	int IRQ1_in;
369 	int IRQ1_out;
370 	int JPEG_in;
371 	int JPEG_out;
372 	int JPEG_0;
373 	int JPEG_1;
374 	int END_event_missed;
375 	int JPEG_missed;
376 	int JPEG_error;
377 	int num_errors;
378 	int JPEG_max_missed;
379 	int JPEG_min_missed;
380 
381 	u32 last_isr;
382 	unsigned long frame_num;
383 
384 	wait_queue_head_t test_q;
385 };
386 
to_zoran(struct v4l2_device * v4l2_dev)387 static inline struct zoran *to_zoran(struct v4l2_device *v4l2_dev)
388 {
389 	return container_of(v4l2_dev, struct zoran, v4l2_dev);
390 }
391 
392 /* There was something called _ALPHA_BUZ that used the PCI address instead of
393  * the kernel iomapped address for btread/btwrite.  */
394 #define btwrite(dat,adr)    writel((dat), zr->zr36057_mem+(adr))
395 #define btread(adr)         readl(zr->zr36057_mem+(adr))
396 
397 #define btand(dat,adr)      btwrite((dat) & btread(adr), adr)
398 #define btor(dat,adr)       btwrite((dat) | btread(adr), adr)
399 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
400 
401 #endif
402