cpia2.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /****************************************************************************
  2. *
  3. * Filename: cpia2.h
  4. *
  5. * Copyright 2001, STMicrolectronics, Inc.
  6. *
  7. * Contact: steve.miller@st.com
  8. *
  9. * Description:
  10. * This is a USB driver for CPiA2 based video cameras.
  11. *
  12. * This driver is modelled on the cpia usb driver by
  13. * Jochen Scharrlach and Johannes Erdfeldt.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. ****************************************************************************/
  30. #ifndef __CPIA2_H__
  31. #define __CPIA2_H__
  32. #include <linux/version.h>
  33. #include <linux/videodev.h>
  34. #include <linux/usb.h>
  35. #include <linux/poll.h>
  36. #include "cpia2dev.h"
  37. #include "cpia2_registers.h"
  38. /* define for verbose debug output */
  39. //#define _CPIA2_DEBUG_
  40. #define CPIA2_MAJ_VER 2
  41. #define CPIA2_MIN_VER 0
  42. #define CPIA2_PATCH_VER 0
  43. /***
  44. * Image defines
  45. ***/
  46. #ifndef true
  47. #define true 1
  48. #define false 0
  49. #endif
  50. /* Misc constants */
  51. #define ALLOW_CORRUPT 0 /* Causes collater to discard checksum */
  52. /* USB Transfer mode */
  53. #define XFER_ISOC 0
  54. #define XFER_BULK 1
  55. /* USB Alternates */
  56. #define USBIF_CMDONLY 0
  57. #define USBIF_BULK 1
  58. #define USBIF_ISO_1 2 /* 128 bytes/ms */
  59. #define USBIF_ISO_2 3 /* 384 bytes/ms */
  60. #define USBIF_ISO_3 4 /* 640 bytes/ms */
  61. #define USBIF_ISO_4 5 /* 768 bytes/ms */
  62. #define USBIF_ISO_5 6 /* 896 bytes/ms */
  63. #define USBIF_ISO_6 7 /* 1023 bytes/ms */
  64. /* Flicker Modes */
  65. #define NEVER_FLICKER 0
  66. #define ANTI_FLICKER_ON 1
  67. #define FLICKER_60 60
  68. #define FLICKER_50 50
  69. /* Debug flags */
  70. #define DEBUG_NONE 0
  71. #define DEBUG_REG 0x00000001
  72. #define DEBUG_DUMP_PATCH 0x00000002
  73. #define DEBUG_DUMP_REGS 0x00000004
  74. /***
  75. * Video frame sizes
  76. ***/
  77. enum {
  78. VIDEOSIZE_VGA = 0, /* 640x480 */
  79. VIDEOSIZE_CIF, /* 352x288 */
  80. VIDEOSIZE_QVGA, /* 320x240 */
  81. VIDEOSIZE_QCIF, /* 176x144 */
  82. VIDEOSIZE_288_216,
  83. VIDEOSIZE_256_192,
  84. VIDEOSIZE_224_168,
  85. VIDEOSIZE_192_144,
  86. };
  87. #define STV_IMAGE_CIF_ROWS 288
  88. #define STV_IMAGE_CIF_COLS 352
  89. #define STV_IMAGE_QCIF_ROWS 144
  90. #define STV_IMAGE_QCIF_COLS 176
  91. #define STV_IMAGE_VGA_ROWS 480
  92. #define STV_IMAGE_VGA_COLS 640
  93. #define STV_IMAGE_QVGA_ROWS 240
  94. #define STV_IMAGE_QVGA_COLS 320
  95. #define JPEG_MARKER_COM (1<<6) /* Comment segment */
  96. /***
  97. * Enums
  98. ***/
  99. /* Sensor types available with cpia2 asics */
  100. enum sensors {
  101. CPIA2_SENSOR_410,
  102. CPIA2_SENSOR_500
  103. };
  104. /* Asic types available in the CPiA2 architecture */
  105. #define CPIA2_ASIC_672 0x67
  106. /* Device types (stv672, stv676, etc) */
  107. #define DEVICE_STV_672 0x0001
  108. #define DEVICE_STV_676 0x0002
  109. enum frame_status {
  110. FRAME_EMPTY,
  111. FRAME_READING, /* In the process of being grabbed into */
  112. FRAME_READY, /* Ready to be read */
  113. FRAME_ERROR,
  114. };
  115. /***
  116. * Register access (for USB request byte)
  117. ***/
  118. enum {
  119. CAMERAACCESS_SYSTEM = 0,
  120. CAMERAACCESS_VC,
  121. CAMERAACCESS_VP,
  122. CAMERAACCESS_IDATA
  123. };
  124. #define CAMERAACCESS_TYPE_BLOCK 0x00
  125. #define CAMERAACCESS_TYPE_RANDOM 0x04
  126. #define CAMERAACCESS_TYPE_MASK 0x08
  127. #define CAMERAACCESS_TYPE_REPEAT 0x0C
  128. #define TRANSFER_READ 0
  129. #define TRANSFER_WRITE 1
  130. #define DEFAULT_ALT USBIF_ISO_6
  131. #define DEFAULT_BRIGHTNESS 0x46
  132. #define DEFAULT_CONTRAST 0x93
  133. #define DEFAULT_SATURATION 0x7f
  134. #define DEFAULT_TARGET_KB 0x30
  135. /* Power state */
  136. #define HI_POWER_MODE CPIA2_SYSTEM_CONTROL_HIGH_POWER
  137. #define LO_POWER_MODE CPIA2_SYSTEM_CONTROL_LOW_POWER
  138. /********
  139. * Commands
  140. *******/
  141. enum {
  142. CPIA2_CMD_NONE = 0,
  143. CPIA2_CMD_GET_VERSION,
  144. CPIA2_CMD_GET_PNP_ID,
  145. CPIA2_CMD_GET_ASIC_TYPE,
  146. CPIA2_CMD_GET_SENSOR,
  147. CPIA2_CMD_GET_VP_DEVICE,
  148. CPIA2_CMD_GET_VP_BRIGHTNESS,
  149. CPIA2_CMD_SET_VP_BRIGHTNESS,
  150. CPIA2_CMD_GET_CONTRAST,
  151. CPIA2_CMD_SET_CONTRAST,
  152. CPIA2_CMD_GET_VP_SATURATION,
  153. CPIA2_CMD_SET_VP_SATURATION,
  154. CPIA2_CMD_GET_VP_GPIO_DIRECTION,
  155. CPIA2_CMD_SET_VP_GPIO_DIRECTION,
  156. CPIA2_CMD_GET_VP_GPIO_DATA,
  157. CPIA2_CMD_SET_VP_GPIO_DATA,
  158. CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION,
  159. CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
  160. CPIA2_CMD_GET_VC_MP_GPIO_DATA,
  161. CPIA2_CMD_SET_VC_MP_GPIO_DATA,
  162. CPIA2_CMD_ENABLE_PACKET_CTRL,
  163. CPIA2_CMD_GET_FLICKER_MODES,
  164. CPIA2_CMD_SET_FLICKER_MODES,
  165. CPIA2_CMD_RESET_FIFO, /* clear fifo and enable stream block */
  166. CPIA2_CMD_SET_HI_POWER,
  167. CPIA2_CMD_SET_LOW_POWER,
  168. CPIA2_CMD_CLEAR_V2W_ERR,
  169. CPIA2_CMD_SET_USER_MODE,
  170. CPIA2_CMD_GET_USER_MODE,
  171. CPIA2_CMD_FRAMERATE_REQ,
  172. CPIA2_CMD_SET_COMPRESSION_STATE,
  173. CPIA2_CMD_GET_WAKEUP,
  174. CPIA2_CMD_SET_WAKEUP,
  175. CPIA2_CMD_GET_PW_CONTROL,
  176. CPIA2_CMD_SET_PW_CONTROL,
  177. CPIA2_CMD_GET_SYSTEM_CTRL,
  178. CPIA2_CMD_SET_SYSTEM_CTRL,
  179. CPIA2_CMD_GET_VP_SYSTEM_STATE,
  180. CPIA2_CMD_GET_VP_SYSTEM_CTRL,
  181. CPIA2_CMD_SET_VP_SYSTEM_CTRL,
  182. CPIA2_CMD_GET_VP_EXP_MODES,
  183. CPIA2_CMD_SET_VP_EXP_MODES,
  184. CPIA2_CMD_GET_DEVICE_CONFIG,
  185. CPIA2_CMD_SET_DEVICE_CONFIG,
  186. CPIA2_CMD_SET_SERIAL_ADDR,
  187. CPIA2_CMD_SET_SENSOR_CR1,
  188. CPIA2_CMD_GET_VC_CONTROL,
  189. CPIA2_CMD_SET_VC_CONTROL,
  190. CPIA2_CMD_SET_TARGET_KB,
  191. CPIA2_CMD_SET_DEF_JPEG_OPT,
  192. CPIA2_CMD_REHASH_VP4,
  193. CPIA2_CMD_GET_USER_EFFECTS,
  194. CPIA2_CMD_SET_USER_EFFECTS
  195. };
  196. enum user_cmd {
  197. COMMAND_NONE = 0x00000001,
  198. COMMAND_SET_FPS = 0x00000002,
  199. COMMAND_SET_COLOR_PARAMS = 0x00000004,
  200. COMMAND_GET_COLOR_PARAMS = 0x00000008,
  201. COMMAND_SET_FORMAT = 0x00000010, /* size, etc */
  202. COMMAND_SET_FLICKER = 0x00000020
  203. };
  204. /***
  205. * Some defines specific to the 676 chip
  206. ***/
  207. #define CAMACC_CIF 0x01
  208. #define CAMACC_VGA 0x02
  209. #define CAMACC_QCIF 0x04
  210. #define CAMACC_QVGA 0x08
  211. struct cpia2_register {
  212. u8 index;
  213. u8 value;
  214. };
  215. struct cpia2_reg_mask {
  216. u8 index;
  217. u8 and_mask;
  218. u8 or_mask;
  219. u8 fill;
  220. };
  221. struct cpia2_command {
  222. u32 command;
  223. u8 req_mode; /* (Block or random) | registerBank */
  224. u8 reg_count;
  225. u8 direction;
  226. u8 start;
  227. union reg_types {
  228. struct cpia2_register registers[32];
  229. struct cpia2_reg_mask masks[16];
  230. u8 block_data[64];
  231. u8 *patch_data; /* points to function defined block */
  232. } buffer;
  233. };
  234. struct camera_params {
  235. struct {
  236. u8 firmware_revision_hi; /* For system register set (bank 0) */
  237. u8 firmware_revision_lo;
  238. u8 asic_id; /* Video Compressor set (bank 1) */
  239. u8 asic_rev;
  240. u8 vp_device_hi; /* Video Processor set (bank 2) */
  241. u8 vp_device_lo;
  242. u8 sensor_flags;
  243. u8 sensor_rev;
  244. } version;
  245. struct {
  246. u32 device_type; /* enumerated from vendor/product ids.
  247. * Currently, either STV_672 or STV_676 */
  248. u16 vendor;
  249. u16 product;
  250. u16 device_revision;
  251. } pnp_id;
  252. struct {
  253. u8 brightness; /* CPIA2_VP_EXPOSURE_TARGET */
  254. u8 contrast; /* Note: this is CPIA2_VP_YRANGE */
  255. u8 saturation; /* CPIA2_VP_SATURATION */
  256. } color_params;
  257. struct {
  258. u8 cam_register;
  259. u8 flicker_mode_req; /* 1 if flicker on, else never flicker */
  260. int mains_frequency;
  261. } flicker_control;
  262. struct {
  263. u8 jpeg_options;
  264. u8 creep_period;
  265. u8 user_squeeze;
  266. u8 inhibit_htables;
  267. } compression;
  268. struct {
  269. u8 ohsize; /* output image size */
  270. u8 ovsize;
  271. u8 hcrop; /* cropping start_pos/4 */
  272. u8 vcrop;
  273. u8 hphase; /* scaling registers */
  274. u8 vphase;
  275. u8 hispan;
  276. u8 vispan;
  277. u8 hicrop;
  278. u8 vicrop;
  279. u8 hifraction;
  280. u8 vifraction;
  281. } image_size;
  282. struct {
  283. int width; /* actual window width */
  284. int height; /* actual window height */
  285. } roi;
  286. struct {
  287. u8 video_mode;
  288. u8 frame_rate;
  289. u8 video_size; /* Not a register, just a convenience for cropped sizes */
  290. u8 gpio_direction;
  291. u8 gpio_data;
  292. u8 system_ctrl;
  293. u8 system_state;
  294. u8 lowlight_boost; /* Bool: 0 = off, 1 = on */
  295. u8 device_config;
  296. u8 exposure_modes;
  297. u8 user_effects;
  298. } vp_params;
  299. struct {
  300. u8 pw_control;
  301. u8 wakeup;
  302. u8 vc_control;
  303. u8 vc_mp_direction;
  304. u8 vc_mp_data;
  305. u8 target_kb;
  306. } vc_params;
  307. struct {
  308. u8 power_mode;
  309. u8 system_ctrl;
  310. u8 stream_mode; /* This is the current alternate for usb drivers */
  311. u8 allow_corrupt;
  312. } camera_state;
  313. };
  314. #define NUM_SBUF 2
  315. struct cpia2_sbuf {
  316. char *data;
  317. struct urb *urb;
  318. };
  319. struct framebuf {
  320. struct timeval timestamp;
  321. unsigned long seq;
  322. int num;
  323. int length;
  324. int max_length;
  325. volatile enum frame_status status;
  326. u8 *data;
  327. struct framebuf *next;
  328. };
  329. struct cpia2_fh {
  330. enum v4l2_priority prio;
  331. u8 mmapped;
  332. };
  333. struct camera_data {
  334. /* locks */
  335. struct mutex busy_lock; /* guard against SMP multithreading */
  336. struct v4l2_prio_state prio;
  337. /* camera status */
  338. volatile int present; /* Is the camera still present? */
  339. int open_count; /* # of process that have camera open */
  340. int first_image_seen;
  341. u8 mains_freq; /* for flicker control */
  342. enum sensors sensor_type;
  343. u8 flush;
  344. u8 mmapped;
  345. int streaming; /* 0 = no, 1 = yes */
  346. int xfer_mode; /* XFER_BULK or XFER_ISOC */
  347. struct camera_params params; /* camera settings */
  348. /* v4l */
  349. int video_size; /* VIDEO_SIZE_ */
  350. struct video_device *vdev; /* v4l videodev */
  351. struct video_picture vp; /* v4l camera settings */
  352. struct video_window vw; /* v4l capture area */
  353. __u32 pixelformat; /* Format fourcc */
  354. /* USB */
  355. struct usb_device *dev;
  356. unsigned char iface;
  357. unsigned int cur_alt;
  358. unsigned int old_alt;
  359. struct cpia2_sbuf sbuf[NUM_SBUF]; /* Double buffering */
  360. wait_queue_head_t wq_stream;
  361. /* Buffering */
  362. u32 frame_size;
  363. int num_frames;
  364. unsigned long frame_count;
  365. u8 *frame_buffer; /* frame buffer data */
  366. struct framebuf *buffers;
  367. struct framebuf * volatile curbuff;
  368. struct framebuf *workbuff;
  369. /* MJPEG Extension */
  370. int APPn; /* Number of APP segment to be written, must be 0..15 */
  371. int APP_len; /* Length of data in JPEG APPn segment */
  372. char APP_data[60]; /* Data in the JPEG APPn segment. */
  373. int COM_len; /* Length of data in JPEG COM segment */
  374. char COM_data[60]; /* Data in JPEG COM segment */
  375. };
  376. /* v4l */
  377. int cpia2_register_camera(struct camera_data *cam);
  378. void cpia2_unregister_camera(struct camera_data *cam);
  379. /* core */
  380. int cpia2_reset_camera(struct camera_data *cam);
  381. int cpia2_set_low_power(struct camera_data *cam);
  382. void cpia2_dbg_dump_registers(struct camera_data *cam);
  383. int cpia2_match_video_size(int width, int height);
  384. void cpia2_set_camera_state(struct camera_data *cam);
  385. void cpia2_save_camera_state(struct camera_data *cam);
  386. void cpia2_set_color_params(struct camera_data *cam);
  387. void cpia2_set_brightness(struct camera_data *cam, unsigned char value);
  388. void cpia2_set_contrast(struct camera_data *cam, unsigned char value);
  389. void cpia2_set_saturation(struct camera_data *cam, unsigned char value);
  390. int cpia2_set_flicker_mode(struct camera_data *cam, int mode);
  391. void cpia2_set_format(struct camera_data *cam);
  392. int cpia2_send_command(struct camera_data *cam, struct cpia2_command *cmd);
  393. int cpia2_do_command(struct camera_data *cam,
  394. unsigned int command,
  395. unsigned char direction, unsigned char param);
  396. struct camera_data *cpia2_init_camera_struct(void);
  397. int cpia2_init_camera(struct camera_data *cam);
  398. int cpia2_allocate_buffers(struct camera_data *cam);
  399. void cpia2_free_buffers(struct camera_data *cam);
  400. long cpia2_read(struct camera_data *cam,
  401. char __user *buf, unsigned long count, int noblock);
  402. unsigned int cpia2_poll(struct camera_data *cam,
  403. struct file *filp, poll_table *wait);
  404. int cpia2_remap_buffer(struct camera_data *cam, struct vm_area_struct *vma);
  405. void cpia2_set_property_flip(struct camera_data *cam, int prop_val);
  406. void cpia2_set_property_mirror(struct camera_data *cam, int prop_val);
  407. int cpia2_set_target_kb(struct camera_data *cam, unsigned char value);
  408. int cpia2_set_gpio(struct camera_data *cam, unsigned char setting);
  409. int cpia2_set_fps(struct camera_data *cam, int framerate);
  410. /* usb */
  411. int cpia2_usb_init(void);
  412. void cpia2_usb_cleanup(void);
  413. int cpia2_usb_transfer_cmd(struct camera_data *cam, void *registers,
  414. u8 request, u8 start, u8 count, u8 direction);
  415. int cpia2_usb_stream_start(struct camera_data *cam, unsigned int alternate);
  416. int cpia2_usb_stream_stop(struct camera_data *cam);
  417. int cpia2_usb_stream_pause(struct camera_data *cam);
  418. int cpia2_usb_stream_resume(struct camera_data *cam);
  419. int cpia2_usb_change_streaming_alternate(struct camera_data *cam,
  420. unsigned int alt);
  421. /* ----------------------- debug functions ---------------------- */
  422. #ifdef _CPIA2_DEBUG_
  423. #define ALOG(lev, fmt, args...) printk(lev "%s:%d %s(): " fmt, __FILE__, __LINE__, __func__, ## args)
  424. #define LOG(fmt, args...) ALOG(KERN_INFO, fmt, ## args)
  425. #define ERR(fmt, args...) ALOG(KERN_ERR, fmt, ## args)
  426. #define DBG(fmt, args...) ALOG(KERN_DEBUG, fmt, ## args)
  427. #else
  428. #define ALOG(fmt,args...) printk(fmt,##args)
  429. #define LOG(fmt,args...) ALOG(KERN_INFO "cpia2: "fmt,##args)
  430. #define ERR(fmt,args...) ALOG(KERN_ERR "cpia2: "fmt,##args)
  431. #define DBG(fmn,args...) do {} while(0)
  432. #endif
  433. /* No function or lineno, for shorter lines */
  434. #define KINFO(fmt, args...) printk(KERN_INFO fmt,##args)
  435. #endif