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