Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  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
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#ifndef cpia_h
#define cpia_h

/*
 * CPiA Parallel Port Video4Linux driver
 *
 * Supports CPiA based parallel port Video Camera's.
 *
 * (C) Copyright 1999 Bas Huisman,
 *                    Peter Pregler,
 *                    Scott J. Bertin,
 *                    VLSI Vision Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define CPIA_MAJ_VER	0
#define CPIA_MIN_VER    7
#define CPIA_PATCH_VER	4

#define CPIA_PP_MAJ_VER       0
#define CPIA_PP_MIN_VER       7
#define CPIA_PP_PATCH_VER     4

#define CPIA_MAX_FRAME_SIZE_UNALIGNED	(352 * 288 * 4)   /* CIF at RGB32 */
#define CPIA_MAX_FRAME_SIZE	((CPIA_MAX_FRAME_SIZE_UNALIGNED + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)) /* align above to PAGE_SIZE */

#ifdef __KERNEL__

#include <asm/uaccess.h>
#include <linux/videodev.h>
#include <linux/smp_lock.h>

struct cpia_camera_ops
{
	/* open sets privdata to point to structure for this camera.
         * Returns negative value on error, otherwise 0.
	 */
	int (*open)(void *privdata);
	
	/* Registers callback function cb to be called with cbdata
	 * when an image is ready.  If cb is NULL, only single image grabs
	 * should be used.  cb should immediately call streamRead to read
	 * the data or data may be lost. Returns negative value on error,
	 * otherwise 0.
	 */
	int (*registerCallback)(void *privdata, void (*cb)(void *cbdata),
	                        void *cbdata);
	
	/* transferCmd sends commands to the camera.  command MUST point to
	 * an  8 byte buffer in kernel space. data can be NULL if no extra
	 * data is needed.  The size of the data is given by the last 2
	 * bytes of comand.  data must also point to memory in kernel space.
	 * Returns negative value on error, otherwise 0.
	 */
	int (*transferCmd)(void *privdata, u8 *command, u8 *data);

	/* streamStart initiates stream capture mode.
	 * Returns negative value on error, otherwise 0.
	 */
	int (*streamStart)(void *privdata);
	
	/* streamStop terminates stream capture mode.
	 * Returns negative value on error, otherwise 0.
	 */
	int (*streamStop)(void *privdata);
        
	/* streamRead reads a frame from the camera.  buffer points to a
         * buffer large enough to hold a complete frame in kernel space.
         * noblock indicates if this should be a non blocking read.
	 * Returns the number of bytes read, or negative value on error.
         */
	int (*streamRead)(void *privdata, u8 *buffer, int noblock);
	
	/* close disables the device until open() is called again.
	 * Returns negative value on error, otherwise 0.
	 */
	int (*close)(void *privdata);
	
	/* If wait_for_stream_ready is non-zero, wait until the streamState
	 * is STREAM_READY before calling streamRead.
	 */
	int wait_for_stream_ready;
};

struct cpia_frame {
	u8 *data;
	int count;
	int width;
	int height;
	volatile int state;
};

struct cam_params {
	struct {
		u8 firmwareVersion;
		u8 firmwareRevision;
		u8 vcVersion;
		u8 vcRevision;
	} version;
	struct {
		u16 vendor;
		u16 product;
		u16 deviceRevision;
	} pnpID;
	struct {
		u8 vpVersion;
		u8 vpRevision;
		u16 cameraHeadID;
	} vpVersion;
	struct {
		u8 systemState;
		u8 grabState;
		u8 streamState;
		u8 fatalError;
		u8 cmdError;
		u8 debugFlags;
		u8 vpStatus;
		u8 errorCode;
	} status;
	struct {
		u8 brightness;
		u8 contrast;
		u8 saturation;
	} colourParams;
	struct {
		u8 gainMode;
		u8 expMode;
		u8 compMode;
		u8 centreWeight;
		u8 gain;
		u8 fineExp;
		u8 coarseExpLo;
		u8 coarseExpHi;
		u8 redComp;
		u8 green1Comp;
		u8 green2Comp;
		u8 blueComp;
	} exposure;
	struct {
		u8 balanceModeIsAuto;
		u8 redGain;
		u8 greenGain;
		u8 blueGain;
	} colourBalance;
	struct {
		u8 divisor;
		u8 baserate;
	} sensorFps;
	struct {
		u8 gain1;
		u8 gain2;
		u8 gain4;
		u8 gain8;
	} apcor;
	struct {
		u8 flickerMode;
		u8 coarseJump;
		u8 allowableOverExposure;
	} flickerControl;
	struct {
		u8 gain1;
		u8 gain2;
		u8 gain4;
		u8 gain8;
	} vlOffset;
	struct {
		u8 mode;
		u8 decimation;
	} compression;
	struct {
		u8 frTargeting;
		u8 targetFR;
		u8 targetQ;
	} compressionTarget;
	struct {
		u8 yThreshold;
		u8 uvThreshold;
	} yuvThreshold;
	struct {
		u8 hysteresis;
		u8 threshMax;
		u8 smallStep;
		u8 largeStep;
		u8 decimationHysteresis;
		u8 frDiffStepThresh;
		u8 qDiffStepThresh;
		u8 decimationThreshMod;
	} compressionParams;
	struct {
		u8 videoSize;		/* CIF/QCIF */
		u8 subSample;
		u8 yuvOrder;
	} format;
	struct {
		u8 colStart;		/* skip first 8*colStart pixels */
		u8 colEnd;		/* finish at 8*colEnd pixels */
		u8 rowStart;		/* skip first 4*rowStart lines */
		u8 rowEnd;		/* finish at 4*rowEnd lines */
	} roi;
	u8 ecpTiming;
	u8 streamStartLine;
};

enum v4l_camstates {
	CPIA_V4L_IDLE = 0,
	CPIA_V4L_ERROR,
	CPIA_V4L_COMMAND,
	CPIA_V4L_GRABBING,
	CPIA_V4L_STREAMING,
	CPIA_V4L_STREAMING_PAUSED,
};

#define FRAME_NUM	2	/* double buffering for now */

struct cam_data {
	struct cam_data **previous;
	struct cam_data *next;

        struct semaphore busy_lock;     /* guard against SMP multithreading */
	struct cpia_camera_ops *ops;	/* lowlevel driver operations */
	void *lowlevel_data;		/* private data for lowlevel driver */
	u8 *raw_image;			/* buffer for raw image data */
	struct cpia_frame decompressed_frame;
                                        /* buffer to hold decompressed frame */
	int image_size;		        /* sizeof last decompressed image */
	int open_count;			/* # of process that have camera open */

				/* camera status */
	int fps;			/* actual fps reported by the camera */
	int transfer_rate;		/* transfer rate from camera in kB/s */
	u8 mainsFreq;			/* for flicker control */

				/* proc interface */
	struct semaphore param_lock;	/* params lock for this camera */
	struct cam_params params;	/* camera settings */
	struct proc_dir_entry *proc_entry;	/* /proc/cpia/videoX */
	
					/* v4l */
	int video_size;			/* VIDEO_SIZE_ */
	volatile enum v4l_camstates camstate;	/* v4l layer status */
	struct video_device vdev;	/* v4l videodev */
	struct video_picture vp;	/* v4l camera settings */
	struct video_window vw;		/* v4l capture area */

				/* mmap interface */
	int curframe;			/* the current frame to grab into */
	u8 *frame_buf;			/* frame buffer data */
        struct cpia_frame frame[FRAME_NUM];
				/* FRAME_NUM-buffering, so we need a array */

	int first_frame;
	int mmap_kludge;		/* 'wrong' byte order for mmap */
	volatile u32 cmd_queue;		/* queued commands */
};

/* cpia_register_camera is called by low level driver for each camera.
 * A unique camera number is returned, or a negative value on error */
struct cam_data *cpia_register_camera(struct cpia_camera_ops *ops, void *lowlevel);

/* cpia_unregister_camera is called by low level driver when a camera
 * is removed.  This must not fail. */
void cpia_unregister_camera(struct cam_data *cam);

/* raw CIF + 64 byte header + (2 bytes line_length + EOL) per line + 4*EOI +
 * one byte 16bit DMA alignment
 */
#define CPIA_MAX_IMAGE_SIZE ((352*288*2)+64+(288*3)+5)

/* constant value's */
#define MAGIC_0		0x19
#define MAGIC_1		0x68
#define DATA_IN		0xC0
#define DATA_OUT	0x40
#define VIDEOSIZE_QCIF	0	/* 176x144 */
#define VIDEOSIZE_CIF	1	/* 352x288 */
#define VIDEOSIZE_SIF	2	/* 320x240 */
#define VIDEOSIZE_QSIF	3	/* 160x120 */
#define VIDEOSIZE_48_48		4 /* where no one has gone before, iconsize! */
#define VIDEOSIZE_64_48		5
#define VIDEOSIZE_128_96	6
#define VIDEOSIZE_160_120	VIDEOSIZE_QSIF
#define VIDEOSIZE_176_144	VIDEOSIZE_QCIF
#define VIDEOSIZE_192_144	7
#define VIDEOSIZE_224_168	8
#define VIDEOSIZE_256_192	9
#define VIDEOSIZE_288_216	10
#define VIDEOSIZE_320_240	VIDEOSIZE_SIF
#define VIDEOSIZE_352_288	VIDEOSIZE_CIF
#define VIDEOSIZE_88_72		11 /* quarter CIF */
#define SUBSAMPLE_420	0
#define SUBSAMPLE_422	1
#define YUVORDER_YUYV	0
#define YUVORDER_UYVY	1
#define NOT_COMPRESSED	0
#define COMPRESSED	1
#define NO_DECIMATION	0
#define DECIMATION_ENAB	1
#define EOI		0xff	/* End Of Image */
#define EOL		0xfd	/* End Of Line */
#define FRAME_HEADER_SIZE	64

/* Image grab modes */
#define CPIA_GRAB_SINGLE	0
#define CPIA_GRAB_CONTINUOUS	1

/* Compression parameters */
#define CPIA_COMPRESSION_NONE	0
#define CPIA_COMPRESSION_AUTO	1
#define CPIA_COMPRESSION_MANUAL	2
#define CPIA_COMPRESSION_TARGET_QUALITY         0
#define CPIA_COMPRESSION_TARGET_FRAMERATE       1

/* Return offsets for GetCameraState */
#define SYSTEMSTATE	0
#define GRABSTATE	1
#define STREAMSTATE	2
#define FATALERROR	3
#define CMDERROR	4
#define DEBUGFLAGS	5
#define VPSTATUS	6
#define ERRORCODE	7

/* SystemState */
#define UNINITIALISED_STATE	0
#define PASS_THROUGH_STATE	1
#define LO_POWER_STATE		2
#define HI_POWER_STATE		3
#define WARM_BOOT_STATE		4

/* GrabState */
#define GRAB_IDLE		0
#define GRAB_ACTIVE		1
#define GRAB_DONE		2

/* StreamState */
#define STREAM_NOT_READY	0
#define STREAM_READY		1
#define STREAM_OPEN		2
#define STREAM_PAUSED		3
#define STREAM_FINISHED		4

/* Fatal Error, CmdError, and DebugFlags */
#define CPIA_FLAG	  1
#define SYSTEM_FLAG	  2
#define INT_CTRL_FLAG	  4
#define PROCESS_FLAG	  8
#define COM_FLAG	 16
#define VP_CTRL_FLAG	 32
#define CAPTURE_FLAG	 64
#define DEBUG_FLAG	128

/* VPStatus */
#define VP_STATE_OK			0x00

#define VP_STATE_FAILED_VIDEOINIT	0x01
#define VP_STATE_FAILED_AECACBINIT	0x02
#define VP_STATE_AEC_MAX		0x04
#define VP_STATE_ACB_BMAX		0x08

#define VP_STATE_ACB_RMIN		0x10
#define VP_STATE_ACB_GMIN		0x20
#define VP_STATE_ACB_RMAX		0x40
#define VP_STATE_ACB_GMAX		0x80

/* ErrorCode */
#define ERROR_FLICKER_BELOW_MIN_EXP     0x01 /*flicker exposure got below minimum exposure */

#define ALOG(lineno,fmt,args...) printk(fmt,lineno,##args)
#define LOG(fmt,args...) ALOG((__LINE__),KERN_INFO __FILE__":"__FUNCTION__"(%d):"fmt,##args)

#ifdef _CPIA_DEBUG_
#define ADBG(lineno,fmt,args...) printk(fmt, jiffies, lineno, ##args)
#define DBG(fmt,args...) ADBG((__LINE__),KERN_DEBUG __FILE__"(%ld):"__FUNCTION__"(%d):"fmt,##args)
#else
#define DBG(fmn,args...) do {} while(0)
#endif

#define DEB_BYTE(p)\
  DBG("%1d %1d %1d %1d %1d %1d %1d %1d \n",\
      (p)&0x80?1:0, (p)&0x40?1:0, (p)&0x20?1:0, (p)&0x10?1:0,\
        (p)&0x08?1:0, (p)&0x04?1:0, (p)&0x02?1:0, (p)&0x01?1:0);

#define ADD_TO_LIST(l, drv) \
  {\
    lock_kernel();\
    (drv)->next = l;\
    (drv)->previous = &(l);\
    (l) = drv;\
    unlock_kernel();\
  } while(0)

#define REMOVE_FROM_LIST(drv) \
  {\
    if ((drv)->previous != NULL) {\
      lock_kernel();\
      if ((drv)->next != NULL)\
        (drv)->next->previous = (drv)->previous;\
      *((drv)->previous) = (drv)->next;\
      (drv)->previous = NULL;\
      (drv)->next = NULL;\
      unlock_kernel();\
    }\
  } while (0)


#endif /* __KERNEL__ */

#endif /* cpia_h */