4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 buz - Iomega Buz driver
25 Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
29 buz.0.0.3 Copyright (C) 1998 Dave Perks <dperks@ibm.net>
33 bttv - Bt848 frame grabber driver
34 Copyright (C) 1996,97 Ralph Metzler (rjkm@thp.uni-koeln.de)
36 This program is free software; you can redistribute it and/or modify
37 it under the terms of the GNU General Public License as published by
38 the Free Software Foundation; either version 2 of the License, or
39 (at your option) any later version.
41 This program is distributed in the hope that it will be useful,
42 but WITHOUT ANY WARRANTY; without even the implied warranty of
43 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 GNU General Public License for more details.
46 You should have received a copy of the GNU General Public License
47 along with this program; if not, write to the Free Software
48 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
54 /* The Buz only supports a maximum width of 720, but some V4L
55 applications (e.g. xawtv are more happy with 768).
56 If XAWTV_HACK is defined, we try to fake a device with bigger width */
61 //#define BUZ_MAX_WIDTH 768 /* never display more than 768 pixels */
62 #define BUZ_MAX_WIDTH (tvnorms[btv->params.norm].Wa)
64 //#define BUZ_MAX_WIDTH 720 /* never display more than 720 pixels */
66 //#define BUZ_MAX_HEIGHT 576 /* never display more than 576 rows */
67 #define BUZ_MAX_HEIGHT (tvnorms[btv->params.norm].Ha)
68 #define BUZ_MIN_WIDTH 32 /* never display less than 32 pixels */
69 #define BUZ_MIN_HEIGHT 24 /* never display less than 24 rows */
71 struct buz_requestbuffers
73 unsigned long count; /* Number of buffers for MJPEG grabbing */
74 unsigned long size; /* Size PER BUFFER in bytes */
79 unsigned long frame; /* number of buffer that has been free'd */
80 unsigned long length; /* number of code bytes in buffer (capture only) */
81 unsigned long seq; /* frame sequence number */
82 struct timeval timestamp; /* timestamp */
87 int input; /* Input channel, has to be set prior to BUZIOC_G_STATUS */
88 int signal; /* Returned: 1 if valid video signal detected */
89 int norm; /* Returned: VIDEO_MODE_PAL or VIDEO_MODE_NTSC */
90 int color; /* Returned: 1 if color signal detected */
96 /* The following parameters can only be queried */
98 int major_version; /* Major version number of driver */
99 int minor_version; /* Minor version number of driver */
101 /* Main control parameters */
103 int input; /* Input channel: 0 = Composite, 1 = S-VHS */
104 int norm; /* Norm: VIDEO_MODE_PAL or VIDEO_MODE_NTSC */
105 int decimation; /* decimation of captured video,
106 enlargement of video played back.
107 Valid values are 1, 2, 4 or 0.
108 0 is a special value where the user
109 has full control over video scaling */
111 /* The following parameters only have to be set if decimation==0,
112 for other values of decimation they provide the data how the image is captured */
114 int HorDcm; /* Horizontal decimation: 1, 2 or 4 */
115 int VerDcm; /* Vertical decimation: 1 or 2 */
116 int TmpDcm; /* Temporal decimation: 1 or 2,
117 if TmpDcm==2 in capture every second frame is dropped,
118 in playback every frame is played twice */
119 int field_per_buff; /* Number of fields per buffer: 1 or 2 */
120 int img_x; /* start of image in x direction */
121 int img_y; /* start of image in y direction */
122 int img_width; /* image width BEFORE decimation,
123 must be a multiple of HorDcm*16 */
124 int img_height; /* image height BEFORE decimation,
125 must be a multiple of VerDcm*8 */
127 /* --- End of parameters for decimation==0 only --- */
129 /* JPEG control parameters */
131 int quality; /* Measure for quality of compressed images.
132 Scales linearly with the size of the compressed images.
133 Must be beetween 0 and 100, 100 is a compression
136 int odd_even; /* Which field should come first ??? */
138 int APPn; /* Number of APP segment to be written, must be 0..15 */
139 int APP_len; /* Length of data in JPEG APPn segment */
140 char APP_data[60]; /* Data in the JPEG APPn segment. */
142 int COM_len; /* Length of data in JPEG COM segment */
143 char COM_data[60]; /* Data in JPEG COM segment */
145 unsigned long jpeg_markers; /* Which markers should go into the JPEG output.
146 Unless you exactly know what you do, leave them untouched.
147 Inluding less markers will make the resulting code
148 smaller, but there will be fewer aplications
150 The presence of the APP and COM marker is
151 influenced by APP0_len and COM_len ONLY! */
152 #define JPEG_MARKER_DHT (1<<3) /* Define Huffman Tables */
153 #define JPEG_MARKER_DQT (1<<4) /* Define Quantization Tables */
154 #define JPEG_MARKER_DRI (1<<5) /* Define Restart Interval */
155 #define JPEG_MARKER_COM (1<<6) /* Comment segment */
156 #define JPEG_MARKER_APP (1<<7) /* App segment, driver will allways use APP0 */
158 int VFIFO_FB; /* Flag for enabling Video Fifo Feedback.
159 If this flag is turned on and JPEG decompressing
160 is going to the screen, the decompress process
161 is stopped every time the Video Fifo is full.
162 This enables a smooth decompress to the screen
163 but the video output signal will get scrambled */
167 char reserved[312]; /* Makes 512 bytes for this structure */
171 Private IOCTL to set up for displaying MJPEG
173 #define BUZIOC_G_PARAMS _IOR ('v', BASE_VIDIOCPRIVATE+0, struct buz_params)
174 #define BUZIOC_S_PARAMS _IOWR('v', BASE_VIDIOCPRIVATE+1, struct buz_params)
175 #define BUZIOC_REQBUFS _IOWR('v', BASE_VIDIOCPRIVATE+2, struct buz_requestbuffers)
176 #define BUZIOC_QBUF_CAPT _IOW ('v', BASE_VIDIOCPRIVATE+3, int)
177 #define BUZIOC_QBUF_PLAY _IOW ('v', BASE_VIDIOCPRIVATE+4, int)
178 #define BUZIOC_SYNC _IOR ('v', BASE_VIDIOCPRIVATE+5, struct buz_sync)
179 #define BUZIOC_G_STATUS _IOWR('v', BASE_VIDIOCPRIVATE+6, struct buz_status)
184 #define BUZ_NUM_STAT_COM 4
185 #define BUZ_MASK_STAT_COM 3
187 #define BUZ_MAX_FRAME 256 /* Must be a power of 2 */
188 #define BUZ_MASK_FRAME 255 /* Must be BUZ_MAX_FRAME-1 */
190 #if VIDEO_MAX_FRAME <= 32
191 # define V4L_MAX_FRAME 32
192 #elif VIDEO_MAX_FRAME <= 64
193 # define V4L_MAX_FRAME 64
195 # error "Too many video frame buffers to handle"
197 #define V4L_MASK_FRAME (V4L_MAX_FRAME - 1)
204 BUZ_MODE_IDLE, /* nothing going on */
205 BUZ_MODE_MOTION_COMPRESS, /* grabbing frames */
206 BUZ_MODE_MOTION_DECOMPRESS, /* playing frames */
207 BUZ_MODE_STILL_COMPRESS, /* still frame conversion */
208 BUZ_MODE_STILL_DECOMPRESS /* still frame conversion */
211 enum buz_buffer_state
213 BUZ_STATE_USER, /* buffer is owned by application */
214 BUZ_STATE_PEND, /* buffer is queued in pend[] ready to feed to I/O */
215 BUZ_STATE_DMA, /* buffer is queued in dma[] for I/O */
216 BUZ_STATE_DONE /* buffer is ready to return to application */
221 u32 * frag_tab; /* addresses of frag table */
222 u32 frag_tab_bus; /* same value cached to save time in ISR */
223 enum buz_buffer_state state; /* non-zero if corresponding buffer is in use in grab queue */
224 struct buz_sync bs; /* DONE: info to return to application */
229 char * fbuffer; /* virtual address of frame buffer */
230 unsigned long fbuffer_phys; /* physical address of frame buffer */
231 unsigned long fbuffer_bus; /* bus address of frame buffer */
232 enum buz_buffer_state state; /* state: unused/pending/done */
237 struct video_device video_dev;
240 int initialized; /* flag if buz has been correctly initalized */
241 int user; /* number of current users (0 or 1) */
243 unsigned short id; /* number of this device */
244 char name[32]; /* name of this device */
245 struct pci_dev *pci_dev; /* PCI device */
246 unsigned char revision; /* revision of zr36057 */
247 unsigned int zr36057_adr; /* bus address of IO mem returned by PCI BIOS */
248 unsigned char *zr36057_mem; /* pointer to mapped IO memory */
250 int map_mjpeg_buffers; /* Flag which bufferset will map by next mmap() */
252 spinlock_t lock; /* Spinlock */
254 /* Video for Linux parameters */
256 struct video_picture picture; /* Current picture params */
257 struct video_buffer buffer; /* Current buffer params */
258 struct video_window window; /* Current window params */
259 int buffer_set, window_set; /* Flags if the above structures are set */
260 int video_interlace; /* Image on screen is interlaced */
264 struct wait_queue * v4l_capq; /* wait here for grab to finish */
266 int v4l_overlay_active; /* Overlay grab is activated */
267 int v4l_memgrab_active; /* Memory grab is activated */
269 int v4l_grab_frame; /* Frame number being currently grabbed */
270 #define NO_GRAB_ACTIVE (-1)
271 int v4l_grab_seq; /* Number of frames grabbed */
272 int gwidth; /* Width of current memory capture */
273 int gheight; /* Height of current memory capture */
274 int gformat; /* Format of ... */
275 int gbpl; /* byte per line of ... */
277 /* V4L grab queue of frames pending */
279 unsigned v4l_pend_head;
280 unsigned v4l_pend_tail;
281 int v4l_pend[V4L_MAX_FRAME];
283 struct v4l_gbuffer v4l_gbuf[VIDEO_MAX_FRAME]; /* V4L buffers' info */
285 /* Buz MJPEG parameters */
287 unsigned long jpg_nbufs; /* Number of buffers */
288 unsigned long jpg_bufsize; /* Size of mjpeg buffers in bytes */
289 int jpg_buffers_allocated; /* Flag if buffers are allocated */
290 int need_contiguous; /* Flag if contiguous buffers are needed */
292 enum buz_codec_mode codec_mode; /* status of codec */
293 struct buz_params params; /* structure with a lot of things to play with */
295 struct wait_queue * jpg_capq; /* wait here for grab to finish */
297 /* grab queue counts/indices, mask with BUZ_MASK_STAT_COM before using as index */
298 /* (dma_head - dma_tail) is number active in DMA, must be <= BUZ_NUM_STAT_COM */
299 /* (value & BUZ_MASK_STAT_COM) corresponds to index in stat_com table */
300 unsigned long jpg_que_head; /* Index where to put next buffer which is queued */
301 unsigned long jpg_dma_head; /* Index of next buffer which goes into stat_com */
302 unsigned long jpg_dma_tail; /* Index of last buffer in stat_com */
303 unsigned long jpg_que_tail; /* Index of last buffer in queue */
304 unsigned long jpg_seq_num; /* count of frames since grab/play started */
306 /* zr36057's code buffer table */
307 u32 * stat_com; /* stat_com[i] is indexed by dma_head/tail & BUZ_MASK_STAT_COM */
309 /* (value & BUZ_MASK_FRAME) corresponds to index in pend[] queue */
310 int jpg_pend[BUZ_MAX_FRAME];
312 /* array indexed by frame number */
313 struct buz_gbuffer jpg_gbuf[BUZ_MAX_FRAME]; /* MJPEG buffers' info */
315 /* Additional stuff for testing */
316 #ifdef CONFIG_PROC_FS
318 struct proc_dir_entry *buz_proc;
323 int intr_counter_GIRQ1;
324 int intr_counter_GIRQ0;
325 int intr_counter_CodRepIRQ;
326 int intr_counter_JPEGRepIRQ;
339 unsigned long frame_num;
341 struct wait_queue *test_q;
346 /*The following should be done in more portable way. It depends on define
347 of _ALPHA_BUZ in the Makefile.*/
350 #define btwrite(dat,adr) writel((dat),(char *) (btv->zr36057_adr+(adr)))
351 #define btread(adr) readl(btv->zr36057_adr+(adr))
353 #define btwrite(dat,adr) writel((dat), (char *) (btv->zr36057_mem+(adr)))
354 #define btread(adr) readl(btv->zr36057_mem+(adr))
357 #define btand(dat,adr) btwrite((dat) & btread(adr), adr)
358 #define btor(dat,adr) btwrite((dat) | btread(adr), adr)
359 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
361 #define I2C_TSA5522 0xc2
362 #define I2C_TDA9850 0xb6
363 #define I2C_HAUPEE 0xa0
364 #define I2C_STBEE 0xae
365 #define I2C_SAA7111 0x48
366 #define I2C_SAA7110 0x9c
367 #define I2C_SAA7185 0x88
368 //#define I2C_ADV7175 0xd4
369 #define I2C_ADV7175 0x54
371 #define TDA9850_CON1 0x04
372 #define TDA9850_CON2 0x05
373 #define TDA9850_CON3 0x06
374 #define TDA9850_CON4 0x07
375 #define TDA9850_ALI1 0x08
376 #define TDA9850_ALI2 0x09
377 #define TDA9850_ALI3 0x0a