cpia.h 12 KB

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