1 /****************************************************************************
5 * Copyright 2001, STMicrolectronics, Inc.
7 * Contact: steve.miller@st.com
10 * This is a USB driver for CPiA2 based video cameras.
12 * This driver is modelled on the cpia usb driver by
13 * Jochen Scharrlach and Johannes Erdfeldt.
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.
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.
25 ****************************************************************************/
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>
37 #include "cpia2_registers.h"
39 /* define for verbose debug output */
40 //#define _CPIA2_DEBUG_
47 #define ALLOW_CORRUPT 0 /* Causes collater to discard checksum */
49 /* USB Transfer mode */
54 #define USBIF_CMDONLY 0
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 */
64 #define NEVER_FLICKER 0
70 #define DEBUG_REG 0x00000001
71 #define DEBUG_DUMP_PATCH 0x00000002
72 #define DEBUG_DUMP_REGS 0x00000004
78 VIDEOSIZE_VGA
= 0, /* 640x480 */
79 VIDEOSIZE_CIF
, /* 352x288 */
80 VIDEOSIZE_QVGA
, /* 320x240 */
81 VIDEOSIZE_QCIF
, /* 176x144 */
88 #define STV_IMAGE_CIF_ROWS 288
89 #define STV_IMAGE_CIF_COLS 352
91 #define STV_IMAGE_QCIF_ROWS 144
92 #define STV_IMAGE_QCIF_COLS 176
94 #define STV_IMAGE_VGA_ROWS 480
95 #define STV_IMAGE_VGA_COLS 640
97 #define STV_IMAGE_QVGA_ROWS 240
98 #define STV_IMAGE_QVGA_COLS 320
100 #define JPEG_MARKER_COM (1<<6) /* Comment segment */
105 /* Sensor types available with cpia2 asics */
111 /* Asic types available in the CPiA2 architecture */
112 #define CPIA2_ASIC_672 0x67
114 /* Device types (stv672, stv676, etc) */
115 #define DEVICE_STV_672 0x0001
116 #define DEVICE_STV_676 0x0002
120 FRAME_READING
, /* In the process of being grabbed into */
121 FRAME_READY
, /* Ready to be read */
126 * Register access (for USB request byte)
129 CAMERAACCESS_SYSTEM
= 0,
135 #define CAMERAACCESS_TYPE_BLOCK 0x00
136 #define CAMERAACCESS_TYPE_RANDOM 0x04
137 #define CAMERAACCESS_TYPE_MASK 0x08
138 #define CAMERAACCESS_TYPE_REPEAT 0x0C
140 #define TRANSFER_READ 0
141 #define TRANSFER_WRITE 1
143 #define DEFAULT_ALT USBIF_ISO_6
144 #define DEFAULT_BRIGHTNESS 0x46
145 #define DEFAULT_CONTRAST 0x93
146 #define DEFAULT_SATURATION 0x7f
149 #define HI_POWER_MODE CPIA2_SYSTEM_CONTROL_HIGH_POWER
150 #define LO_POWER_MODE CPIA2_SYSTEM_CONTROL_LOW_POWER
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
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
222 * Some defines specific to the 676 chip
224 #define CAMACC_CIF 0x01
225 #define CAMACC_VGA 0x02
226 #define CAMACC_QCIF 0x04
227 #define CAMACC_QVGA 0x08
230 struct cpia2_register
{
235 struct cpia2_reg_mask
{
242 struct cpia2_command
{
244 u8 req_mode
; /* (Block or random) | registerBank */
249 struct cpia2_register registers
[32];
250 struct cpia2_reg_mask masks
[16];
252 u8
*patch_data
; /* points to function defined block */
256 struct camera_params
{
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) */
262 u8 vp_device_hi
; /* Video Processor set (bank 2) */
269 u32 device_type
; /* enumerated from vendor/product ids.
270 * Currently, either STV_672 or STV_676 */
277 u8 brightness
; /* CPIA2_VP_EXPOSURE_TARGET */
278 u8 contrast
; /* Note: this is CPIA2_VP_YRANGE */
279 u8 saturation
; /* CPIA2_VP_SATURATION */
284 u8 flicker_mode_req
; /* 1 if flicker on, else never flicker */
295 u8 ohsize
; /* output image size */
297 u8 hcrop
; /* cropping start_pos/4 */
299 u8 hphase
; /* scaling registers */
310 int width
; /* actual window width */
311 int height
; /* actual window height */
317 u8 video_size
; /* Not a register, just a convenience for cropped sizes */
322 u8 lowlight_boost
; /* Bool: 0 = off, 1 = on */
340 u8 stream_mode
; /* This is the current alternate for usb drivers */
353 struct timeval timestamp
;
358 volatile enum frame_status status
;
360 struct framebuf
*next
;
365 struct v4l2_device v4l2_dev
;
366 struct mutex v4l2_lock
; /* serialize file operations */
367 struct v4l2_ctrl_handler hdl
;
369 /* Lights control cluster */
370 struct v4l2_ctrl
*top_light
;
371 struct v4l2_ctrl
*bottom_light
;
373 struct v4l2_ctrl
*usb_alt
;
376 int first_image_seen
;
377 enum sensors sensor_type
;
379 struct v4l2_fh
*stream_fh
;
381 int streaming
; /* 0 = no, 1 = yes */
382 int xfer_mode
; /* XFER_BULK or XFER_ISOC */
383 struct camera_params params
; /* camera settings */
386 int video_size
; /* VIDEO_SIZE_ */
387 struct video_device vdev
; /* v4l videodev */
389 u32 height
; /* Its size */
390 __u32 pixelformat
; /* Format fourcc */
393 struct usb_device
*dev
;
395 unsigned int cur_alt
;
396 unsigned int old_alt
;
397 struct cpia2_sbuf sbuf
[NUM_SBUF
]; /* Double buffering */
399 wait_queue_head_t wq_stream
;
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
;
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. */
415 int COM_len
; /* Length of data in JPEG COM segment */
416 char COM_data
[60]; /* Data in JPEG COM segment */
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
);
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
);
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
,
468 /* ----------------------- debug functions ---------------------- */
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)
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)
480 /* No function or lineno, for shorter lines */
481 #define KINFO(fmt, args...) printk(KERN_INFO fmt,##args)