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