1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /****************************************************************************
6 * Copyright 2001, STMicrolectronics, Inc.
8 * Contact: steve.miller@st.com
11 * This is a USB driver for CPiA2 based video cameras.
13 * This driver is modelled on the cpia usb driver by
14 * Jochen Scharrlach and Johannes Erdfeldt.
16 ****************************************************************************/
21 #include <linux/videodev2.h>
22 #include <linux/usb.h>
23 #include <linux/poll.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ctrls.h>
28 #include "cpia2_registers.h"
30 /* define for verbose debug output */
31 //#define _CPIA2_DEBUG_
38 #define ALLOW_CORRUPT 0 /* Causes collater to discard checksum */
40 /* USB Transfer mode */
45 #define USBIF_CMDONLY 0
47 #define USBIF_ISO_1 2 /* 128 bytes/ms */
48 #define USBIF_ISO_2 3 /* 384 bytes/ms */
49 #define USBIF_ISO_3 4 /* 640 bytes/ms */
50 #define USBIF_ISO_4 5 /* 768 bytes/ms */
51 #define USBIF_ISO_5 6 /* 896 bytes/ms */
52 #define USBIF_ISO_6 7 /* 1023 bytes/ms */
55 #define NEVER_FLICKER 0
61 #define DEBUG_REG 0x00000001
62 #define DEBUG_DUMP_PATCH 0x00000002
63 #define DEBUG_DUMP_REGS 0x00000004
69 VIDEOSIZE_VGA
= 0, /* 640x480 */
70 VIDEOSIZE_CIF
, /* 352x288 */
71 VIDEOSIZE_QVGA
, /* 320x240 */
72 VIDEOSIZE_QCIF
, /* 176x144 */
79 #define STV_IMAGE_CIF_ROWS 288
80 #define STV_IMAGE_CIF_COLS 352
82 #define STV_IMAGE_QCIF_ROWS 144
83 #define STV_IMAGE_QCIF_COLS 176
85 #define STV_IMAGE_VGA_ROWS 480
86 #define STV_IMAGE_VGA_COLS 640
88 #define STV_IMAGE_QVGA_ROWS 240
89 #define STV_IMAGE_QVGA_COLS 320
91 #define JPEG_MARKER_COM (1<<6) /* Comment segment */
96 /* Sensor types available with cpia2 asics */
102 /* Asic types available in the CPiA2 architecture */
103 #define CPIA2_ASIC_672 0x67
105 /* Device types (stv672, stv676, etc) */
106 #define DEVICE_STV_672 0x0001
107 #define DEVICE_STV_676 0x0002
111 FRAME_READING
, /* In the process of being grabbed into */
112 FRAME_READY
, /* Ready to be read */
117 * Register access (for USB request byte)
120 CAMERAACCESS_SYSTEM
= 0,
126 #define CAMERAACCESS_TYPE_BLOCK 0x00
127 #define CAMERAACCESS_TYPE_RANDOM 0x04
128 #define CAMERAACCESS_TYPE_MASK 0x08
129 #define CAMERAACCESS_TYPE_REPEAT 0x0C
131 #define TRANSFER_READ 0
132 #define TRANSFER_WRITE 1
134 #define DEFAULT_ALT USBIF_ISO_6
135 #define DEFAULT_BRIGHTNESS 0x46
136 #define DEFAULT_CONTRAST 0x93
137 #define DEFAULT_SATURATION 0x7f
140 #define HI_POWER_MODE CPIA2_SYSTEM_CONTROL_HIGH_POWER
141 #define LO_POWER_MODE CPIA2_SYSTEM_CONTROL_LOW_POWER
149 CPIA2_CMD_GET_VERSION
,
150 CPIA2_CMD_GET_PNP_ID
,
151 CPIA2_CMD_GET_ASIC_TYPE
,
152 CPIA2_CMD_GET_SENSOR
,
153 CPIA2_CMD_GET_VP_DEVICE
,
154 CPIA2_CMD_GET_VP_BRIGHTNESS
,
155 CPIA2_CMD_SET_VP_BRIGHTNESS
,
156 CPIA2_CMD_GET_CONTRAST
,
157 CPIA2_CMD_SET_CONTRAST
,
158 CPIA2_CMD_GET_VP_SATURATION
,
159 CPIA2_CMD_SET_VP_SATURATION
,
160 CPIA2_CMD_GET_VP_GPIO_DIRECTION
,
161 CPIA2_CMD_SET_VP_GPIO_DIRECTION
,
162 CPIA2_CMD_GET_VP_GPIO_DATA
,
163 CPIA2_CMD_SET_VP_GPIO_DATA
,
164 CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION
,
165 CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION
,
166 CPIA2_CMD_GET_VC_MP_GPIO_DATA
,
167 CPIA2_CMD_SET_VC_MP_GPIO_DATA
,
168 CPIA2_CMD_ENABLE_PACKET_CTRL
,
169 CPIA2_CMD_GET_FLICKER_MODES
,
170 CPIA2_CMD_SET_FLICKER_MODES
,
171 CPIA2_CMD_RESET_FIFO
, /* clear fifo and enable stream block */
172 CPIA2_CMD_SET_HI_POWER
,
173 CPIA2_CMD_SET_LOW_POWER
,
174 CPIA2_CMD_CLEAR_V2W_ERR
,
175 CPIA2_CMD_SET_USER_MODE
,
176 CPIA2_CMD_GET_USER_MODE
,
177 CPIA2_CMD_FRAMERATE_REQ
,
178 CPIA2_CMD_SET_COMPRESSION_STATE
,
179 CPIA2_CMD_GET_WAKEUP
,
180 CPIA2_CMD_SET_WAKEUP
,
181 CPIA2_CMD_GET_PW_CONTROL
,
182 CPIA2_CMD_SET_PW_CONTROL
,
183 CPIA2_CMD_GET_SYSTEM_CTRL
,
184 CPIA2_CMD_SET_SYSTEM_CTRL
,
185 CPIA2_CMD_GET_VP_SYSTEM_STATE
,
186 CPIA2_CMD_GET_VP_SYSTEM_CTRL
,
187 CPIA2_CMD_SET_VP_SYSTEM_CTRL
,
188 CPIA2_CMD_GET_VP_EXP_MODES
,
189 CPIA2_CMD_SET_VP_EXP_MODES
,
190 CPIA2_CMD_GET_DEVICE_CONFIG
,
191 CPIA2_CMD_SET_DEVICE_CONFIG
,
192 CPIA2_CMD_SET_SERIAL_ADDR
,
193 CPIA2_CMD_SET_SENSOR_CR1
,
194 CPIA2_CMD_GET_VC_CONTROL
,
195 CPIA2_CMD_SET_VC_CONTROL
,
196 CPIA2_CMD_SET_TARGET_KB
,
197 CPIA2_CMD_SET_DEF_JPEG_OPT
,
198 CPIA2_CMD_REHASH_VP4
,
199 CPIA2_CMD_GET_USER_EFFECTS
,
200 CPIA2_CMD_SET_USER_EFFECTS
204 COMMAND_NONE
= 0x00000001,
205 COMMAND_SET_FPS
= 0x00000002,
206 COMMAND_SET_COLOR_PARAMS
= 0x00000004,
207 COMMAND_GET_COLOR_PARAMS
= 0x00000008,
208 COMMAND_SET_FORMAT
= 0x00000010, /* size, etc */
209 COMMAND_SET_FLICKER
= 0x00000020
213 * Some defines specific to the 676 chip
215 #define CAMACC_CIF 0x01
216 #define CAMACC_VGA 0x02
217 #define CAMACC_QCIF 0x04
218 #define CAMACC_QVGA 0x08
221 struct cpia2_register
{
226 struct cpia2_reg_mask
{
233 struct cpia2_command
{
235 u8 req_mode
; /* (Block or random) | registerBank */
240 struct cpia2_register registers
[32];
241 struct cpia2_reg_mask masks
[16];
243 u8
*patch_data
; /* points to function defined block */
247 struct camera_params
{
249 u8 firmware_revision_hi
; /* For system register set (bank 0) */
250 u8 firmware_revision_lo
;
251 u8 asic_id
; /* Video Compressor set (bank 1) */
253 u8 vp_device_hi
; /* Video Processor set (bank 2) */
260 u32 device_type
; /* enumerated from vendor/product ids.
261 * Currently, either STV_672 or STV_676 */
268 u8 brightness
; /* CPIA2_VP_EXPOSURE_TARGET */
269 u8 contrast
; /* Note: this is CPIA2_VP_YRANGE */
270 u8 saturation
; /* CPIA2_VP_SATURATION */
275 u8 flicker_mode_req
; /* 1 if flicker on, else never flicker */
286 u8 ohsize
; /* output image size */
288 u8 hcrop
; /* cropping start_pos/4 */
290 u8 hphase
; /* scaling registers */
301 int width
; /* actual window width */
302 int height
; /* actual window height */
308 u8 video_size
; /* Not a register, just a convenience for cropped sizes */
313 u8 lowlight_boost
; /* Bool: 0 = off, 1 = on */
331 u8 stream_mode
; /* This is the current alternate for usb drivers */
349 volatile enum frame_status status
;
351 struct framebuf
*next
;
356 struct v4l2_device v4l2_dev
;
357 struct mutex v4l2_lock
; /* serialize file operations */
358 struct v4l2_ctrl_handler hdl
;
360 /* Lights control cluster */
361 struct v4l2_ctrl
*top_light
;
362 struct v4l2_ctrl
*bottom_light
;
364 struct v4l2_ctrl
*usb_alt
;
367 int first_image_seen
;
368 enum sensors sensor_type
;
370 struct v4l2_fh
*stream_fh
;
372 int streaming
; /* 0 = no, 1 = yes */
373 int xfer_mode
; /* XFER_BULK or XFER_ISOC */
374 struct camera_params params
; /* camera settings */
377 int video_size
; /* VIDEO_SIZE_ */
378 struct video_device vdev
; /* v4l videodev */
380 u32 height
; /* Its size */
381 __u32 pixelformat
; /* Format fourcc */
384 struct usb_device
*dev
;
386 unsigned int cur_alt
;
387 unsigned int old_alt
;
388 struct cpia2_sbuf sbuf
[NUM_SBUF
]; /* Double buffering */
390 wait_queue_head_t wq_stream
;
395 unsigned long frame_count
;
396 u8
*frame_buffer
; /* frame buffer data */
397 struct framebuf
*buffers
;
398 struct framebuf
* volatile curbuff
;
399 struct framebuf
*workbuff
;
401 /* MJPEG Extension */
402 int APPn
; /* Number of APP segment to be written, must be 0..15 */
403 int APP_len
; /* Length of data in JPEG APPn segment */
404 char APP_data
[60]; /* Data in the JPEG APPn segment. */
406 int COM_len
; /* Length of data in JPEG COM segment */
407 char COM_data
[60]; /* Data in JPEG COM segment */
411 int cpia2_register_camera(struct camera_data
*cam
);
412 void cpia2_unregister_camera(struct camera_data
*cam
);
413 void cpia2_camera_release(struct v4l2_device
*v4l2_dev
);
416 int cpia2_reset_camera(struct camera_data
*cam
);
417 int cpia2_set_low_power(struct camera_data
*cam
);
418 void cpia2_dbg_dump_registers(struct camera_data
*cam
);
419 int cpia2_match_video_size(int width
, int height
);
420 void cpia2_set_camera_state(struct camera_data
*cam
);
421 void cpia2_save_camera_state(struct camera_data
*cam
);
422 void cpia2_set_color_params(struct camera_data
*cam
);
423 void cpia2_set_brightness(struct camera_data
*cam
, unsigned char value
);
424 void cpia2_set_contrast(struct camera_data
*cam
, unsigned char value
);
425 void cpia2_set_saturation(struct camera_data
*cam
, unsigned char value
);
426 int cpia2_set_flicker_mode(struct camera_data
*cam
, int mode
);
427 void cpia2_set_format(struct camera_data
*cam
);
428 int cpia2_send_command(struct camera_data
*cam
, struct cpia2_command
*cmd
);
429 int cpia2_do_command(struct camera_data
*cam
,
430 unsigned int command
,
431 unsigned char direction
, unsigned char param
);
432 struct camera_data
*cpia2_init_camera_struct(struct usb_interface
*intf
);
433 int cpia2_init_camera(struct camera_data
*cam
);
434 int cpia2_allocate_buffers(struct camera_data
*cam
);
435 void cpia2_free_buffers(struct camera_data
*cam
);
436 long cpia2_read(struct camera_data
*cam
,
437 char __user
*buf
, unsigned long count
, int noblock
);
438 __poll_t
cpia2_poll(struct camera_data
*cam
,
439 struct file
*filp
, poll_table
*wait
);
440 int cpia2_remap_buffer(struct camera_data
*cam
, struct vm_area_struct
*vma
);
441 void cpia2_set_property_flip(struct camera_data
*cam
, int prop_val
);
442 void cpia2_set_property_mirror(struct camera_data
*cam
, int prop_val
);
443 int cpia2_set_gpio(struct camera_data
*cam
, unsigned char setting
);
444 int cpia2_set_fps(struct camera_data
*cam
, int framerate
);
447 int cpia2_usb_init(void);
448 void cpia2_usb_cleanup(void);
449 int cpia2_usb_transfer_cmd(struct camera_data
*cam
, void *registers
,
450 u8 request
, u8 start
, u8 count
, u8 direction
);
451 int cpia2_usb_stream_start(struct camera_data
*cam
, unsigned int alternate
);
452 int cpia2_usb_stream_stop(struct camera_data
*cam
);
453 int cpia2_usb_stream_pause(struct camera_data
*cam
);
454 int cpia2_usb_stream_resume(struct camera_data
*cam
);
455 int cpia2_usb_change_streaming_alternate(struct camera_data
*cam
,
459 /* ----------------------- debug functions ---------------------- */
461 #define ALOG(lev, fmt, args...) printk(lev "%s:%d %s(): " fmt, __FILE__, __LINE__, __func__, ## args)
462 #define LOG(fmt, args...) ALOG(KERN_INFO, fmt, ## args)
463 #define ERR(fmt, args...) ALOG(KERN_ERR, fmt, ## args)
464 #define DBG(fmt, args...) ALOG(KERN_DEBUG, fmt, ## args)
466 #define ALOG(fmt,args...) printk(fmt,##args)
467 #define LOG(fmt,args...) ALOG(KERN_INFO "cpia2: "fmt,##args)
468 #define ERR(fmt,args...) ALOG(KERN_ERR "cpia2: "fmt,##args)
469 #define DBG(fmn,args...) do {} while(0)
471 /* No function or lineno, for shorter lines */
472 #define KINFO(fmt, args...) printk(KERN_INFO fmt,##args)