1 #ifndef cpia_h
2 #define cpia_h
3 
4 /*
5  * CPiA Parallel Port Video4Linux driver
6  *
7  * Supports CPiA based parallel port Video Camera's.
8  *
9  * (C) Copyright 1999 Bas Huisman,
10  *                    Peter Pregler,
11  *                    Scott J. Bertin,
12  *                    VLSI Vision Ltd.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28 
29 #define CPIA_MAJ_VER	0
30 #define CPIA_MIN_VER   8
31 #define CPIA_PATCH_VER	5
32 
33 #define CPIA_PP_MAJ_VER       CPIA_MAJ_VER
34 #define CPIA_PP_MIN_VER       CPIA_MIN_VER
35 #define CPIA_PP_PATCH_VER     CPIA_PATCH_VER
36 
37 #define CPIA_USB_MAJ_VER      CPIA_MAJ_VER
38 #define CPIA_USB_MIN_VER      CPIA_MIN_VER
39 #define CPIA_USB_PATCH_VER    CPIA_PATCH_VER
40 
41 #define CPIA_MAX_FRAME_SIZE_UNALIGNED	(352 * 288 * 4)   /* CIF at RGB32 */
42 #define CPIA_MAX_FRAME_SIZE	((CPIA_MAX_FRAME_SIZE_UNALIGNED + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) /* align above to PAGE_SIZE */
43 
44 #ifdef __KERNEL__
45 
46 #include <asm/uaccess.h>
47 #include <linux/videodev.h>
48 #include <linux/list.h>
49 #include <linux/smp_lock.h>
50 
51 struct cpia_camera_ops
52 {
53 	/* open sets privdata to point to structure for this camera.
54          * Returns negative value on error, otherwise 0.
55 	 */
56 	int (*open)(void *privdata);
57 
58 	/* Registers callback function cb to be called with cbdata
59 	 * when an image is ready.  If cb is NULL, only single image grabs
60 	 * should be used.  cb should immediately call streamRead to read
61 	 * the data or data may be lost. Returns negative value on error,
62 	 * otherwise 0.
63 	 */
64 	int (*registerCallback)(void *privdata, void (*cb)(void *cbdata),
65 	                        void *cbdata);
66 
67 	/* transferCmd sends commands to the camera.  command MUST point to
68 	 * an  8 byte buffer in kernel space. data can be NULL if no extra
69 	 * data is needed.  The size of the data is given by the last 2
70 	 * bytes of command.  data must also point to memory in kernel space.
71 	 * Returns negative value on error, otherwise 0.
72 	 */
73 	int (*transferCmd)(void *privdata, u8 *command, u8 *data);
74 
75 	/* streamStart initiates stream capture mode.
76 	 * Returns negative value on error, otherwise 0.
77 	 */
78 	int (*streamStart)(void *privdata);
79 
80 	/* streamStop terminates stream capture mode.
81 	 * Returns negative value on error, otherwise 0.
82 	 */
83 	int (*streamStop)(void *privdata);
84 
85 	/* streamRead reads a frame from the camera.  buffer points to a
86          * buffer large enough to hold a complete frame in kernel space.
87          * noblock indicates if this should be a non blocking read.
88 	 * Returns the number of bytes read, or negative value on error.
89          */
90 	int (*streamRead)(void *privdata, u8 *buffer, int noblock);
91 
92 	/* close disables the device until open() is called again.
93 	 * Returns negative value on error, otherwise 0.
94 	 */
95 	int (*close)(void *privdata);
96 
97 	/* If wait_for_stream_ready is non-zero, wait until the streamState
98 	 * is STREAM_READY before calling streamRead.
99 	 */
100 	int wait_for_stream_ready;
101 	/*
102 	 * Used to maintain lowlevel module usage counts
103 	 */
104 	struct module *owner;
105 };
106 
107 struct cpia_frame {
108 	u8 *data;
109 	int count;
110 	int width;
111 	int height;
112 	volatile int state;
113 };
114 
115 struct cam_params {
116 	struct {
117 		u8 firmwareVersion;
118 		u8 firmwareRevision;
119 		u8 vcVersion;
120 		u8 vcRevision;
121 	} version;
122 	struct {
123 		u16 vendor;
124 		u16 product;
125 		u16 deviceRevision;
126 	} pnpID;
127 	struct {
128 		u8 vpVersion;
129 		u8 vpRevision;
130 		u16 cameraHeadID;
131 	} vpVersion;
132 	struct {
133 		u8 systemState;
134 		u8 grabState;
135 		u8 streamState;
136 		u8 fatalError;
137 		u8 cmdError;
138 		u8 debugFlags;
139 		u8 vpStatus;
140 		u8 errorCode;
141 	} status;
142 	struct {
143 		u8 brightness;
144 		u8 contrast;
145 		u8 saturation;
146 	} colourParams;
147 	struct {
148 		u8 gainMode;
149 		u8 expMode;
150 		u8 compMode;
151 		u8 centreWeight;
152 		u8 gain;
153 		u8 fineExp;
154 		u8 coarseExpLo;
155 		u8 coarseExpHi;
156 		u8 redComp;
157 		u8 green1Comp;
158 		u8 green2Comp;
159 		u8 blueComp;
160 	} exposure;
161 	struct {
162 		u8 balanceModeIsAuto;
163 		u8 redGain;
164 		u8 greenGain;
165 		u8 blueGain;
166 	} colourBalance;
167 	struct {
168 		u8 divisor;
169 		u8 baserate;
170 	} sensorFps;
171 	struct {
172 		u8 gain1;
173 		u8 gain2;
174 		u8 gain4;
175 		u8 gain8;
176 	} apcor;
177 	struct {
178 		u8 flickerMode;
179 		u8 coarseJump;
180 		u8 allowableOverExposure;
181 	} flickerControl;
182 	struct {
183 		u8 gain1;
184 		u8 gain2;
185 		u8 gain4;
186 		u8 gain8;
187 	} vlOffset;
188 	struct {
189 		u8 mode;
190 		u8 decimation;
191 	} compression;
192 	struct {
193 		u8 frTargeting;
194 		u8 targetFR;
195 		u8 targetQ;
196 	} compressionTarget;
197 	struct {
198 		u8 yThreshold;
199 		u8 uvThreshold;
200 	} yuvThreshold;
201 	struct {
202 		u8 hysteresis;
203 		u8 threshMax;
204 		u8 smallStep;
205 		u8 largeStep;
206 		u8 decimationHysteresis;
207 		u8 frDiffStepThresh;
208 		u8 qDiffStepThresh;
209 		u8 decimationThreshMod;
210 	} compressionParams;
211 	struct {
212 		u8 videoSize;		/* CIF/QCIF */
213 		u8 subSample;
214 		u8 yuvOrder;
215 	} format;
216         struct {                        /* Intel QX3 specific data */
217                 u8 qx3_detected;        /* a QX3 is present */
218                 u8 toplight;            /* top light lit , R/W */
219                 u8 bottomlight;         /* bottom light lit, R/W */
220                 u8 button;              /* snapshot button pressed (R/O) */
221                 u8 cradled;             /* microscope is in cradle (R/O) */
222         } qx3;
223 	struct {
224 		u8 colStart;		/* skip first 8*colStart pixels */
225 		u8 colEnd;		/* finish at 8*colEnd pixels */
226 		u8 rowStart;		/* skip first 4*rowStart lines */
227 		u8 rowEnd;		/* finish at 4*rowEnd lines */
228 	} roi;
229 	u8 ecpTiming;
230 	u8 streamStartLine;
231 };
232 
233 enum v4l_camstates {
234 	CPIA_V4L_IDLE = 0,
235 	CPIA_V4L_ERROR,
236 	CPIA_V4L_COMMAND,
237 	CPIA_V4L_GRABBING,
238 	CPIA_V4L_STREAMING,
239 	CPIA_V4L_STREAMING_PAUSED,
240 };
241 
242 #define FRAME_NUM	2	/* double buffering for now */
243 
244 struct cam_data {
245 	struct list_head cam_data_list;
246 
247         struct semaphore busy_lock;     /* guard against SMP multithreading */
248 	struct cpia_camera_ops *ops;	/* lowlevel driver operations */
249 	void *lowlevel_data;		/* private data for lowlevel driver */
250 	u8 *raw_image;			/* buffer for raw image data */
251 	struct cpia_frame decompressed_frame;
252                                         /* buffer to hold decompressed frame */
253 	int image_size;		        /* sizeof last decompressed image */
254 	int open_count;			/* # of process that have camera open */
255 
256 				/* camera status */
257 	int fps;			/* actual fps reported by the camera */
258 	int transfer_rate;		/* transfer rate from camera in kB/s */
259 	u8 mainsFreq;			/* for flicker control */
260 
261 				/* proc interface */
262 	struct semaphore param_lock;	/* params lock for this camera */
263 	struct cam_params params;	/* camera settings */
264 	struct proc_dir_entry *proc_entry;	/* /proc/cpia/videoX */
265 
266 					/* v4l */
267 	int video_size;			/* VIDEO_SIZE_ */
268 	volatile enum v4l_camstates camstate;	/* v4l layer status */
269 	struct video_device vdev;	/* v4l videodev */
270 	struct video_picture vp;	/* v4l camera settings */
271 	struct video_window vw;		/* v4l capture area */
272 
273 				/* mmap interface */
274 	int curframe;			/* the current frame to grab into */
275 	u8 *frame_buf;			/* frame buffer data */
276         struct cpia_frame frame[FRAME_NUM];
277 				/* FRAME_NUM-buffering, so we need a array */
278 
279 	int first_frame;
280 	int mmap_kludge;		/* 'wrong' byte order for mmap */
281 	volatile u32 cmd_queue;		/* queued commands */
282 };
283 
284 /* cpia_register_camera is called by low level driver for each camera.
285  * A unique camera number is returned, or a negative value on error */
286 struct cam_data *cpia_register_camera(struct cpia_camera_ops *ops, void *lowlevel);
287 
288 /* cpia_unregister_camera is called by low level driver when a camera
289  * is removed.  This must not fail. */
290 void cpia_unregister_camera(struct cam_data *cam);
291 
292 /* raw CIF + 64 byte header + (2 bytes line_length + EOL) per line + 4*EOI +
293  * one byte 16bit DMA alignment
294  */
295 #define CPIA_MAX_IMAGE_SIZE ((352*288*2)+64+(288*3)+5)
296 
297 /* constant value's */
298 #define MAGIC_0		0x19
299 #define MAGIC_1		0x68
300 #define DATA_IN		0xC0
301 #define DATA_OUT	0x40
302 #define VIDEOSIZE_QCIF	0	/* 176x144 */
303 #define VIDEOSIZE_CIF	1	/* 352x288 */
304 #define VIDEOSIZE_SIF	2	/* 320x240 */
305 #define VIDEOSIZE_QSIF	3	/* 160x120 */
306 #define VIDEOSIZE_48_48		4 /* where no one has gone before, iconsize! */
307 #define VIDEOSIZE_64_48		5
308 #define VIDEOSIZE_128_96	6
309 #define VIDEOSIZE_160_120	VIDEOSIZE_QSIF
310 #define VIDEOSIZE_176_144	VIDEOSIZE_QCIF
311 #define VIDEOSIZE_192_144	7
312 #define VIDEOSIZE_224_168	8
313 #define VIDEOSIZE_256_192	9
314 #define VIDEOSIZE_288_216	10
315 #define VIDEOSIZE_320_240	VIDEOSIZE_SIF
316 #define VIDEOSIZE_352_288	VIDEOSIZE_CIF
317 #define VIDEOSIZE_88_72		11 /* quarter CIF */
318 #define SUBSAMPLE_420	0
319 #define SUBSAMPLE_422	1
320 #define YUVORDER_YUYV	0
321 #define YUVORDER_UYVY	1
322 #define NOT_COMPRESSED	0
323 #define COMPRESSED	1
324 #define NO_DECIMATION	0
325 #define DECIMATION_ENAB	1
326 #define EOI		0xff	/* End Of Image */
327 #define EOL		0xfd	/* End Of Line */
328 #define FRAME_HEADER_SIZE	64
329 
330 /* Image grab modes */
331 #define CPIA_GRAB_SINGLE	0
332 #define CPIA_GRAB_CONTINUOUS	1
333 
334 /* Compression parameters */
335 #define CPIA_COMPRESSION_NONE	0
336 #define CPIA_COMPRESSION_AUTO	1
337 #define CPIA_COMPRESSION_MANUAL	2
338 #define CPIA_COMPRESSION_TARGET_QUALITY         0
339 #define CPIA_COMPRESSION_TARGET_FRAMERATE       1
340 
341 /* Return offsets for GetCameraState */
342 #define SYSTEMSTATE	0
343 #define GRABSTATE	1
344 #define STREAMSTATE	2
345 #define FATALERROR	3
346 #define CMDERROR	4
347 #define DEBUGFLAGS	5
348 #define VPSTATUS	6
349 #define ERRORCODE	7
350 
351 /* SystemState */
352 #define UNINITIALISED_STATE	0
353 #define PASS_THROUGH_STATE	1
354 #define LO_POWER_STATE		2
355 #define HI_POWER_STATE		3
356 #define WARM_BOOT_STATE		4
357 
358 /* GrabState */
359 #define GRAB_IDLE		0
360 #define GRAB_ACTIVE		1
361 #define GRAB_DONE		2
362 
363 /* StreamState */
364 #define STREAM_NOT_READY	0
365 #define STREAM_READY		1
366 #define STREAM_OPEN		2
367 #define STREAM_PAUSED		3
368 #define STREAM_FINISHED		4
369 
370 /* Fatal Error, CmdError, and DebugFlags */
371 #define CPIA_FLAG	  1
372 #define SYSTEM_FLAG	  2
373 #define INT_CTRL_FLAG	  4
374 #define PROCESS_FLAG	  8
375 #define COM_FLAG	 16
376 #define VP_CTRL_FLAG	 32
377 #define CAPTURE_FLAG	 64
378 #define DEBUG_FLAG	128
379 
380 /* VPStatus */
381 #define VP_STATE_OK			0x00
382 
383 #define VP_STATE_FAILED_VIDEOINIT	0x01
384 #define VP_STATE_FAILED_AECACBINIT	0x02
385 #define VP_STATE_AEC_MAX		0x04
386 #define VP_STATE_ACB_BMAX		0x08
387 
388 #define VP_STATE_ACB_RMIN		0x10
389 #define VP_STATE_ACB_GMIN		0x20
390 #define VP_STATE_ACB_RMAX		0x40
391 #define VP_STATE_ACB_GMAX		0x80
392 
393 /* ErrorCode */
394 #define ERROR_FLICKER_BELOW_MIN_EXP     0x01 /*flicker exposure got below minimum exposure */
395 
396 #define ALOG(function,lineno,fmt,args...) printk(fmt, function, lineno, ##args)
397 #define LOG(fmt,args...) ALOG((__FUNCTION__), (__LINE__), \
398 			      KERN_INFO __FILE__":%s(%d):"fmt, ##args)
399 
400 #ifdef _CPIA_DEBUG_
401 #define ADBG(function,lineno,fmt,args...) printk(fmt, jiffies, function, lineno, ##args)
402 #define DBG(fmt,args...) ADBG((__FUNCTION__), (__LINE__), \
403 			      KERN_DEBUG __FILE__"(%ld):%s(%d):"fmt, ##args)
404 #else
405 #define DBG(fmn,args...) do {} while(0)
406 #endif
407 
408 #define DEB_BYTE(p)\
409   DBG("%1d %1d %1d %1d %1d %1d %1d %1d \n",\
410       (p)&0x80?1:0, (p)&0x40?1:0, (p)&0x20?1:0, (p)&0x10?1:0,\
411         (p)&0x08?1:0, (p)&0x04?1:0, (p)&0x02?1:0, (p)&0x01?1:0);
412 
413 #endif /* __KERNEL__ */
414 
415 #endif /* cpia_h */
416