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