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