1#ifndef cpia_h
2#define cpia_h
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)
42#define CPIA_MAX_FRAME_SIZE ((CPIA_MAX_FRAME_SIZE_UNALIGNED + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
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
53struct cpia_camera_ops
54{
55
56
57
58 int (*open)(void *privdata);
59
60
61
62
63
64
65
66 int (*registerCallback)(void *privdata, void (*cb)(void *cbdata),
67 void *cbdata);
68
69
70
71
72
73
74
75 int (*transferCmd)(void *privdata, u8 *command, u8 *data);
76
77
78
79
80 int (*streamStart)(void *privdata);
81
82
83
84
85 int (*streamStop)(void *privdata);
86
87
88
89
90
91
92 int (*streamRead)(void *privdata, u8 *buffer, int noblock);
93
94
95
96
97 int (*close)(void *privdata);
98
99
100
101
102 int wait_for_stream_ready;
103
104
105
106
107 struct module *owner;
108};
109
110struct cpia_frame {
111 u8 *data;
112 int count;
113 int width;
114 int height;
115 volatile int state;
116};
117
118struct 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;
217 u8 subSample;
218 u8 yuvOrder;
219 } format;
220 struct {
221 u8 qx3_detected;
222 u8 toplight;
223 u8 bottomlight;
224 u8 button;
225 u8 cradled;
226 } qx3;
227 struct {
228 u8 colStart;
229 u8 colEnd;
230 u8 rowStart;
231 u8 rowEnd;
232 } roi;
233 u8 ecpTiming;
234 u8 streamStartLine;
235};
236
237enum 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
247
248struct cam_data {
249 struct list_head cam_data_list;
250
251 struct mutex busy_lock;
252 struct cpia_camera_ops *ops;
253 void *lowlevel_data;
254 u8 *raw_image;
255 struct cpia_frame decompressed_frame;
256
257 int image_size;
258 int open_count;
259
260
261 int fps;
262 int transfer_rate;
263 u8 mainsFreq;
264
265
266 struct mutex param_lock;
267 struct cam_params params;
268 struct proc_dir_entry *proc_entry;
269
270
271 int video_size;
272 volatile enum v4l_camstates camstate;
273 struct video_device vdev;
274 struct video_picture vp;
275 struct video_window vw;
276 struct video_capture vc;
277
278
279 int curframe;
280 u8 *frame_buf;
281 struct cpia_frame frame[FRAME_NUM];
282
283
284 int first_frame;
285 int mmap_kludge;
286 volatile u32 cmd_queue;
287 int exposure_status;
288 int exposure_count;
289};
290
291
292
293struct cam_data *cpia_register_camera(struct cpia_camera_ops *ops, void *lowlevel);
294
295
296
297void cpia_unregister_camera(struct cam_data *cam);
298
299
300
301
302#define CPIA_MAX_IMAGE_SIZE ((352*288*2)+64+(288*3)+5)
303
304
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
310#define VIDEOSIZE_CIF 1
311#define VIDEOSIZE_SIF 2
312#define VIDEOSIZE_QSIF 3
313#define VIDEOSIZE_48_48 4
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
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
334#define EOL 0xfd
335#define FRAME_HEADER_SIZE 64
336
337
338#define CPIA_GRAB_SINGLE 0
339#define CPIA_GRAB_CONTINUOUS 1
340
341
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
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
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
366#define GRAB_IDLE 0
367#define GRAB_ACTIVE 1
368#define GRAB_DONE 2
369
370
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
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
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
401#define COMP_RED 220
402#define COMP_GREEN1 214
403#define COMP_GREEN2 COMP_GREEN1
404#define COMP_BLUE 230
405
406
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
414#define ERROR_FLICKER_BELOW_MIN_EXP 0x01
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
431
432#endif
433