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