cpia2.h 13 KB

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