cpia2.h 12 KB

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