cpia.h 12 KB

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