cpia.h 11 KB

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