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