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