Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / drivers / media / video / zoran / zoran.h
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 #define ZORAN_VIDMODE_PAL       0
37 #define ZORAN_VIDMODE_NTSC      1
38 #define ZORAN_VIDMODE_SECAM     2
39
40 struct zoran_requestbuffers {
41         unsigned long count;    /* Number of buffers for MJPEG grabbing */
42         unsigned long size;     /* Size PER BUFFER in bytes */
43 };
44
45 struct zoran_sync {
46         unsigned long frame;    /* number of buffer that has been free'd */
47         unsigned long length;   /* number of code bytes in buffer (capture only) */
48         unsigned long seq;      /* frame sequence number */
49         struct timeval timestamp;       /* timestamp */
50 };
51
52 struct zoran_status {
53         int input;              /* Input channel, has to be set prior to BUZIOC_G_STATUS */
54         int signal;             /* Returned: 1 if valid video signal detected */
55         int norm;               /* Returned: ZORAN_VIDMODE_PAL or ZORAN_VIDMODE_NTSC */
56         int color;              /* Returned: 1 if color signal detected */
57 };
58
59 struct zoran_params {
60
61         /* The following parameters can only be queried */
62
63         int major_version;      /* Major version number of driver */
64         int minor_version;      /* Minor version number of driver */
65
66         /* Main control parameters */
67
68         int input;              /* Input channel: 0 = Composite, 1 = S-VHS */
69         int norm;               /* Norm: ZORAN_VIDMODE_PAL or ZORAN_VIDMODE_NTSC */
70         int decimation;         /* decimation of captured video,
71                                  * enlargement of video played back.
72                                  * Valid values are 1, 2, 4 or 0.
73                                  * 0 is a special value where the user
74                                  * has full control over video scaling */
75
76         /* The following parameters only have to be set if decimation==0,
77          * for other values of decimation they provide the data how the image is captured */
78
79         int HorDcm;             /* Horizontal decimation: 1, 2 or 4 */
80         int VerDcm;             /* Vertical decimation: 1 or 2 */
81         int TmpDcm;             /* Temporal decimation: 1 or 2,
82                                  * if TmpDcm==2 in capture every second frame is dropped,
83                                  * in playback every frame is played twice */
84         int field_per_buff;     /* Number of fields per buffer: 1 or 2 */
85         int img_x;              /* start of image in x direction */
86         int img_y;              /* start of image in y direction */
87         int img_width;          /* image width BEFORE decimation,
88                                  * must be a multiple of HorDcm*16 */
89         int img_height;         /* image height BEFORE decimation,
90                                  * must be a multiple of VerDcm*8 */
91
92         /* --- End of parameters for decimation==0 only --- */
93
94         /* JPEG control parameters */
95
96         int quality;            /* Measure for quality of compressed images.
97                                  * Scales linearly with the size of the compressed images.
98                                  * Must be beetween 0 and 100, 100 is a compression
99                                  * ratio of 1:4 */
100
101         int odd_even;           /* Which field should come first ??? */
102
103         int APPn;               /* Number of APP segment to be written, must be 0..15 */
104         int APP_len;            /* Length of data in JPEG APPn segment */
105         char APP_data[60];      /* Data in the JPEG APPn segment. */
106
107         int COM_len;            /* Length of data in JPEG COM segment */
108         char COM_data[60];      /* Data in JPEG COM segment */
109
110         unsigned long jpeg_markers;     /* Which markers should go into the JPEG output.
111                                          * Unless you exactly know what you do, leave them untouched.
112                                          * Inluding less markers will make the resulting code
113                                          * smaller, but there will be fewer applications
114                                          * which can read it.
115                                          * The presence of the APP and COM marker is
116                                          * influenced by APP0_len and COM_len ONLY! */
117 #define JPEG_MARKER_DHT (1<<3)  /* Define Huffman Tables */
118 #define JPEG_MARKER_DQT (1<<4)  /* Define Quantization Tables */
119 #define JPEG_MARKER_DRI (1<<5)  /* Define Restart Interval */
120 #define JPEG_MARKER_COM (1<<6)  /* Comment segment */
121 #define JPEG_MARKER_APP (1<<7)  /* App segment, driver will allways use APP0 */
122
123         int VFIFO_FB;           /* Flag for enabling Video Fifo Feedback.
124                                  * If this flag is turned on and JPEG decompressing
125                                  * is going to the screen, the decompress process
126                                  * is stopped every time the Video Fifo is full.
127                                  * This enables a smooth decompress to the screen
128                                  * but the video output signal will get scrambled */
129
130         /* Misc */
131
132         char reserved[312];     /* Makes 512 bytes for this structure */
133 };
134
135 /*
136 Private IOCTL to set up for displaying MJPEG
137 */
138 #define BUZIOC_G_PARAMS       _IOR ('v', BASE_VIDIOC_PRIVATE+0,  struct zoran_params)
139 #define BUZIOC_S_PARAMS       _IOWR('v', BASE_VIDIOC_PRIVATE+1,  struct zoran_params)
140 #define BUZIOC_REQBUFS        _IOWR('v', BASE_VIDIOC_PRIVATE+2,  struct zoran_requestbuffers)
141 #define BUZIOC_QBUF_CAPT      _IOW ('v', BASE_VIDIOC_PRIVATE+3,  int)
142 #define BUZIOC_QBUF_PLAY      _IOW ('v', BASE_VIDIOC_PRIVATE+4,  int)
143 #define BUZIOC_SYNC           _IOR ('v', BASE_VIDIOC_PRIVATE+5,  struct zoran_sync)
144 #define BUZIOC_G_STATUS       _IOWR('v', BASE_VIDIOC_PRIVATE+6,  struct zoran_status)
145
146
147 #ifdef __KERNEL__
148
149 #define MAJOR_VERSION 0         /* driver major version */
150 #define MINOR_VERSION 10        /* driver minor version */
151 #define RELEASE_VERSION 0       /* release version */
152
153 #define ZORAN_NAME    "ZORAN"   /* name of the device */
154
155 #define ZR_DEVNAME(zr) ((zr)->name)
156
157 #define   BUZ_MAX_WIDTH   (zr->timing->Wa)
158 #define   BUZ_MAX_HEIGHT  (zr->timing->Ha)
159 #define   BUZ_MIN_WIDTH    32   /* never display less than 32 pixels */
160 #define   BUZ_MIN_HEIGHT   24   /* never display less than 24 rows */
161
162 #define BUZ_NUM_STAT_COM    4
163 #define BUZ_MASK_STAT_COM   3
164
165 #define BUZ_MAX_FRAME     256   /* Must be a power of 2 */
166 #define BUZ_MASK_FRAME    255   /* Must be BUZ_MAX_FRAME-1 */
167
168 #define BUZ_MAX_INPUT       16
169
170 #if VIDEO_MAX_FRAME <= 32
171 #   define   V4L_MAX_FRAME   32
172 #elif VIDEO_MAX_FRAME <= 64
173 #   define   V4L_MAX_FRAME   64
174 #else
175 #   error   "Too many video frame buffers to handle"
176 #endif
177 #define   V4L_MASK_FRAME   (V4L_MAX_FRAME - 1)
178
179 #define MAX_FRAME (BUZ_MAX_FRAME > VIDEO_MAX_FRAME ? BUZ_MAX_FRAME : VIDEO_MAX_FRAME)
180
181 #include "zr36057.h"
182
183 enum card_type {
184         UNKNOWN = -1,
185
186         /* Pinnacle/Miro */
187         DC10_old,               /* DC30 like */
188         DC10_new,               /* DC10plus like */
189         DC10plus,
190         DC30,
191         DC30plus,
192
193         /* Linux Media Labs */
194         LML33,
195         LML33R10,
196
197         /* Iomega */
198         BUZ,
199
200         /* AverMedia */
201         AVS6EYES,
202
203         /* total number of cards */
204         NUM_CARDS
205 };
206
207 enum zoran_codec_mode {
208         BUZ_MODE_IDLE,          /* nothing going on */
209         BUZ_MODE_MOTION_COMPRESS,       /* grabbing frames */
210         BUZ_MODE_MOTION_DECOMPRESS,     /* playing frames */
211         BUZ_MODE_STILL_COMPRESS,        /* still frame conversion */
212         BUZ_MODE_STILL_DECOMPRESS       /* still frame conversion */
213 };
214
215 enum zoran_buffer_state {
216         BUZ_STATE_USER,         /* buffer is owned by application */
217         BUZ_STATE_PEND,         /* buffer is queued in pend[] ready to feed to I/O */
218         BUZ_STATE_DMA,          /* buffer is queued in dma[] for I/O */
219         BUZ_STATE_DONE          /* buffer is ready to return to application */
220 };
221
222 enum zoran_map_mode {
223         ZORAN_MAP_MODE_RAW,
224         ZORAN_MAP_MODE_JPG_REC,
225 #define ZORAN_MAP_MODE_JPG ZORAN_MAP_MODE_JPG_REC
226         ZORAN_MAP_MODE_JPG_PLAY,
227 };
228
229 enum gpio_type {
230         ZR_GPIO_JPEG_SLEEP = 0,
231         ZR_GPIO_JPEG_RESET,
232         ZR_GPIO_JPEG_FRAME,
233         ZR_GPIO_VID_DIR,
234         ZR_GPIO_VID_EN,
235         ZR_GPIO_VID_RESET,
236         ZR_GPIO_CLK_SEL1,
237         ZR_GPIO_CLK_SEL2,
238         ZR_GPIO_MAX,
239 };
240
241 enum gpcs_type {
242         GPCS_JPEG_RESET = 0,
243         GPCS_JPEG_START,
244         GPCS_MAX,
245 };
246
247 struct zoran_format {
248         char *name;
249         __u32 fourcc;
250         int colorspace;
251         int depth;
252         __u32 flags;
253         __u32 vfespfr;
254 };
255 /* flags */
256 #define ZORAN_FORMAT_COMPRESSED 1<<0
257 #define ZORAN_FORMAT_OVERLAY    1<<1
258 #define ZORAN_FORMAT_CAPTURE    1<<2
259 #define ZORAN_FORMAT_PLAYBACK   1<<3
260
261 /* overlay-settings */
262 struct zoran_overlay_settings {
263         int is_set;
264         int x, y, width, height;        /* position */
265         int clipcount;          /* position and number of clips */
266         const struct zoran_format *format;      /* overlay format */
267 };
268
269 /* v4l-capture settings */
270 struct zoran_v4l_settings {
271         int width, height, bytesperline;        /* capture size */
272         const struct zoran_format *format;      /* capture format */
273 };
274
275 /* jpg-capture/-playback settings */
276 struct zoran_jpg_settings {
277         int decimation;         /* this bit is used to set everything to default */
278         int HorDcm, VerDcm, TmpDcm;     /* capture decimation settings (TmpDcm=1 means both fields) */
279         int field_per_buff, odd_even;   /* field-settings (odd_even=1 (+TmpDcm=1) means top-field-first) */
280         int img_x, img_y, img_width, img_height;        /* crop settings (subframe capture) */
281         struct v4l2_jpegcompression jpg_comp;   /* JPEG-specific capture settings */
282 };
283
284 struct zoran_mapping {
285         struct file *file;
286         int count;
287 };
288
289 struct zoran_buffer {
290         struct zoran_mapping *map;
291         enum zoran_buffer_state state;  /* state: unused/pending/dma/done */
292         struct zoran_sync bs;           /* DONE: info to return to application */
293         union {
294                 struct {
295                         __le32 *frag_tab;       /* addresses of frag table */
296                         u32 frag_tab_bus;       /* same value cached to save time in ISR */
297                 } jpg;
298                 struct {
299                         char *fbuffer;          /* virtual address of frame buffer */
300                         unsigned long fbuffer_phys;/* physical address of frame buffer */
301                         unsigned long fbuffer_bus;/* bus address of frame buffer */
302                 } v4l;
303         };
304 };
305
306 enum zoran_lock_activity {
307         ZORAN_FREE,             /* free for use */
308         ZORAN_ACTIVE,           /* active but unlocked */
309         ZORAN_LOCKED,           /* locked */
310 };
311
312 /* buffer collections */
313 struct zoran_buffer_col {
314         enum zoran_lock_activity active;        /* feature currently in use? */
315         unsigned int num_buffers, buffer_size;
316         struct zoran_buffer buffer[MAX_FRAME];  /* buffers */
317         u8 allocated;           /* Flag if buffers are allocated  */
318         u8 need_contiguous;     /* Flag if contiguous buffers are needed */
319         /* only applies to jpg buffers, raw buffers are always contiguous */
320 };
321
322 struct zoran;
323
324 /* zoran_fh contains per-open() settings */
325 struct zoran_fh {
326         struct zoran *zr;
327
328         enum zoran_map_mode map_mode;           /* Flag which bufferset will map by next mmap() */
329
330         struct zoran_overlay_settings overlay_settings;
331         u32 *overlay_mask;                      /* overlay mask */
332         enum zoran_lock_activity overlay_active;/* feature currently in use? */
333
334         struct zoran_buffer_col buffers;        /* buffers' info */
335
336         struct zoran_v4l_settings v4l_settings; /* structure with a lot of things to play with */
337         struct zoran_jpg_settings jpg_settings; /* structure with a lot of things to play with */
338 };
339
340 struct card_info {
341         enum card_type type;
342         char name[32];
343         const char *i2c_decoder;        /* i2c decoder device */
344         const unsigned short *addrs_decoder;
345         const char *i2c_encoder;        /* i2c encoder device */
346         const unsigned short *addrs_encoder;
347         u16 video_vfe, video_codec;                     /* videocodec types */
348         u16 audio_chip;                                 /* audio type */
349
350         int inputs;             /* number of video inputs */
351         struct input {
352                 int muxsel;
353                 char name[32];
354         } input[BUZ_MAX_INPUT];
355
356         v4l2_std_id norms;
357         struct tvnorm *tvn[3];  /* supported TV norms */
358
359         u32 jpeg_int;           /* JPEG interrupt */
360         u32 vsync_int;          /* VSYNC interrupt */
361         s8 gpio[ZR_GPIO_MAX];
362         u8 gpcs[GPCS_MAX];
363
364         struct vfe_polarity vfe_pol;
365         u8 gpio_pol[ZR_GPIO_MAX];
366
367         /* is the /GWS line conected? */
368         u8 gws_not_connected;
369
370         /* avs6eyes mux setting */
371         u8 input_mux;
372
373         void (*init) (struct zoran * zr);
374 };
375
376 struct zoran {
377         struct v4l2_device v4l2_dev;
378         struct video_device *video_dev;
379
380         struct i2c_adapter i2c_adapter; /* */
381         struct i2c_algo_bit_data i2c_algo;      /* */
382         u32 i2cbr;
383
384         struct v4l2_subdev *decoder;    /* video decoder sub-device */
385         struct v4l2_subdev *encoder;    /* video encoder sub-device */
386
387         struct videocodec *codec;       /* video codec */
388         struct videocodec *vfe; /* video front end */
389
390         struct mutex resource_lock;     /* prevent evil stuff */
391
392         u8 initialized;         /* flag if zoran has been correctly initialized */
393         int user;               /* number of current users */
394         struct card_info card;
395         struct tvnorm *timing;
396
397         unsigned short id;      /* number of this device */
398         char name[32];          /* name of this device */
399         struct pci_dev *pci_dev;        /* PCI device */
400         unsigned char revision; /* revision of zr36057 */
401         unsigned char __iomem *zr36057_mem;/* pointer to mapped IO memory */
402
403         spinlock_t spinlock;    /* Spinlock */
404
405         /* Video for Linux parameters */
406         int input;      /* card's norm and input */
407         v4l2_std_id norm;
408
409         /* Current buffer params */
410         void    *vbuf_base;
411         int     vbuf_height, vbuf_width;
412         int     vbuf_depth;
413         int     vbuf_bytesperline;
414
415         struct zoran_overlay_settings overlay_settings;
416         u32 *overlay_mask;      /* overlay mask */
417         enum zoran_lock_activity overlay_active;        /* feature currently in use? */
418
419         wait_queue_head_t v4l_capq;
420
421         int v4l_overlay_active; /* Overlay grab is activated */
422         int v4l_memgrab_active; /* Memory grab is activated */
423
424         int v4l_grab_frame;     /* Frame number being currently grabbed */
425 #define NO_GRAB_ACTIVE (-1)
426         unsigned long v4l_grab_seq;     /* Number of frames grabbed */
427         struct zoran_v4l_settings v4l_settings; /* structure with a lot of things to play with */
428
429         /* V4L grab queue of frames pending */
430         unsigned long v4l_pend_head;
431         unsigned long v4l_pend_tail;
432         unsigned long v4l_sync_tail;
433         int v4l_pend[V4L_MAX_FRAME];
434         struct zoran_buffer_col v4l_buffers;    /* V4L buffers' info */
435
436         /* Buz MJPEG parameters */
437         enum zoran_codec_mode codec_mode;       /* status of codec */
438         struct zoran_jpg_settings jpg_settings; /* structure with a lot of things to play with */
439
440         wait_queue_head_t jpg_capq;     /* wait here for grab to finish */
441
442         /* grab queue counts/indices, mask with BUZ_MASK_STAT_COM before using as index */
443         /* (dma_head - dma_tail) is number active in DMA, must be <= BUZ_NUM_STAT_COM */
444         /* (value & BUZ_MASK_STAT_COM) corresponds to index in stat_com table */
445         unsigned long jpg_que_head;     /* Index where to put next buffer which is queued */
446         unsigned long jpg_dma_head;     /* Index of next buffer which goes into stat_com  */
447         unsigned long jpg_dma_tail;     /* Index of last buffer in stat_com               */
448         unsigned long jpg_que_tail;     /* Index of last buffer in queue                  */
449         unsigned long jpg_seq_num;      /* count of frames since grab/play started        */
450         unsigned long jpg_err_seq;      /* last seq_num before error                      */
451         unsigned long jpg_err_shift;
452         unsigned long jpg_queued_num;   /* count of frames queued since grab/play started */
453
454         /* zr36057's code buffer table */
455         __le32 *stat_com;               /* stat_com[i] is indexed by dma_head/tail & BUZ_MASK_STAT_COM */
456
457         /* (value & BUZ_MASK_FRAME) corresponds to index in pend[] queue */
458         int jpg_pend[BUZ_MAX_FRAME];
459
460         /* array indexed by frame number */
461         struct zoran_buffer_col jpg_buffers;    /* MJPEG buffers' info */
462
463         /* Additional stuff for testing */
464 #ifdef CONFIG_PROC_FS
465         struct proc_dir_entry *zoran_proc;
466 #else
467         void *zoran_proc;
468 #endif
469         int testing;
470         int jpeg_error;
471         int intr_counter_GIRQ1;
472         int intr_counter_GIRQ0;
473         int intr_counter_CodRepIRQ;
474         int intr_counter_JPEGRepIRQ;
475         int field_counter;
476         int IRQ1_in;
477         int IRQ1_out;
478         int JPEG_in;
479         int JPEG_out;
480         int JPEG_0;
481         int JPEG_1;
482         int END_event_missed;
483         int JPEG_missed;
484         int JPEG_error;
485         int num_errors;
486         int JPEG_max_missed;
487         int JPEG_min_missed;
488
489         u32 last_isr;
490         unsigned long frame_num;
491
492         wait_queue_head_t test_q;
493 };
494
495 static inline struct zoran *to_zoran(struct v4l2_device *v4l2_dev)
496 {
497         return container_of(v4l2_dev, struct zoran, v4l2_dev);
498 }
499
500 /* There was something called _ALPHA_BUZ that used the PCI address instead of
501  * the kernel iomapped address for btread/btwrite.  */
502 #define btwrite(dat,adr)    writel((dat), zr->zr36057_mem+(adr))
503 #define btread(adr)         readl(zr->zr36057_mem+(adr))
504
505 #define btand(dat,adr)      btwrite((dat) & btread(adr), adr)
506 #define btor(dat,adr)       btwrite((dat) | btread(adr), adr)
507 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
508
509 #endif                          /* __kernel__ */
510
511 #endif