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