aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/cpia2
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@redhat.com>2012-08-13 23:13:22 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2012-08-15 15:25:07 -0400
commit0c0d06cac63ee327ceaab4b5ffe2206574ab86bd (patch)
treee759f0dc3185d97f2a0c6b5cd5e32ea6faa74d40 /drivers/media/video/cpia2
parent84cfe9e79bd5ac11c963f4841158454fefa872f6 (diff)
[media] rename most media/video usb drivers to media/usb
Rename all USB drivers with their own directory under drivers/media/video into drivers/media/usb and update the building system. Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/video/cpia2')
-rw-r--r--drivers/media/video/cpia2/Kconfig9
-rw-r--r--drivers/media/video/cpia2/Makefile3
-rw-r--r--drivers/media/video/cpia2/cpia2.h487
-rw-r--r--drivers/media/video/cpia2/cpia2_core.c2417
-rw-r--r--drivers/media/video/cpia2/cpia2_registers.h476
-rw-r--r--drivers/media/video/cpia2/cpia2_usb.c955
-rw-r--r--drivers/media/video/cpia2/cpia2_v4l.c1267
7 files changed, 0 insertions, 5614 deletions
diff --git a/drivers/media/video/cpia2/Kconfig b/drivers/media/video/cpia2/Kconfig
deleted file mode 100644
index 66e9283f5993..000000000000
--- a/drivers/media/video/cpia2/Kconfig
+++ /dev/null
@@ -1,9 +0,0 @@
1config VIDEO_CPIA2
2 tristate "CPiA2 Video For Linux"
3 depends on VIDEO_DEV && USB && VIDEO_V4L2
4 ---help---
5 This is the video4linux driver for cameras based on Vision's CPiA2
6 (Colour Processor Interface ASIC), such as the Digital Blue QX5
7 Microscope. If you have one of these cameras, say Y here
8
9 This driver is also available as a module (cpia2).
diff --git a/drivers/media/video/cpia2/Makefile b/drivers/media/video/cpia2/Makefile
deleted file mode 100644
index 828cf1b1df86..000000000000
--- a/drivers/media/video/cpia2/Makefile
+++ /dev/null
@@ -1,3 +0,0 @@
1cpia2-objs := cpia2_v4l.o cpia2_usb.o cpia2_core.o
2
3obj-$(CONFIG_VIDEO_CPIA2) += cpia2.o
diff --git a/drivers/media/video/cpia2/cpia2.h b/drivers/media/video/cpia2/cpia2.h
deleted file mode 100644
index cdef677d57ec..000000000000
--- a/drivers/media/video/cpia2/cpia2.h
+++ /dev/null
@@ -1,487 +0,0 @@
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/videodev2.h>
35#include <linux/usb.h>
36#include <linux/poll.h>
37#include <media/v4l2-common.h>
38#include <media/v4l2-device.h>
39#include <media/v4l2-ctrls.h>
40
41#include "cpia2_registers.h"
42
43/* define for verbose debug output */
44//#define _CPIA2_DEBUG_
45
46/***
47 * Image defines
48 ***/
49
50/* Misc constants */
51#define ALLOW_CORRUPT 0 /* Causes collater to discard checksum */
52
53/* USB Transfer mode */
54#define XFER_ISOC 0
55#define XFER_BULK 1
56
57/* USB Alternates */
58#define USBIF_CMDONLY 0
59#define USBIF_BULK 1
60#define USBIF_ISO_1 2 /* 128 bytes/ms */
61#define USBIF_ISO_2 3 /* 384 bytes/ms */
62#define USBIF_ISO_3 4 /* 640 bytes/ms */
63#define USBIF_ISO_4 5 /* 768 bytes/ms */
64#define USBIF_ISO_5 6 /* 896 bytes/ms */
65#define USBIF_ISO_6 7 /* 1023 bytes/ms */
66
67/* Flicker Modes */
68#define NEVER_FLICKER 0
69#define FLICKER_60 60
70#define FLICKER_50 50
71
72/* Debug flags */
73#define DEBUG_NONE 0
74#define DEBUG_REG 0x00000001
75#define DEBUG_DUMP_PATCH 0x00000002
76#define DEBUG_DUMP_REGS 0x00000004
77
78/***
79 * Video frame sizes
80 ***/
81enum {
82 VIDEOSIZE_VGA = 0, /* 640x480 */
83 VIDEOSIZE_CIF, /* 352x288 */
84 VIDEOSIZE_QVGA, /* 320x240 */
85 VIDEOSIZE_QCIF, /* 176x144 */
86 VIDEOSIZE_288_216,
87 VIDEOSIZE_256_192,
88 VIDEOSIZE_224_168,
89 VIDEOSIZE_192_144,
90};
91
92#define STV_IMAGE_CIF_ROWS 288
93#define STV_IMAGE_CIF_COLS 352
94
95#define STV_IMAGE_QCIF_ROWS 144
96#define STV_IMAGE_QCIF_COLS 176
97
98#define STV_IMAGE_VGA_ROWS 480
99#define STV_IMAGE_VGA_COLS 640
100
101#define STV_IMAGE_QVGA_ROWS 240
102#define STV_IMAGE_QVGA_COLS 320
103
104#define JPEG_MARKER_COM (1<<6) /* Comment segment */
105
106/***
107 * Enums
108 ***/
109/* Sensor types available with cpia2 asics */
110enum sensors {
111 CPIA2_SENSOR_410,
112 CPIA2_SENSOR_500
113};
114
115/* Asic types available in the CPiA2 architecture */
116#define CPIA2_ASIC_672 0x67
117
118/* Device types (stv672, stv676, etc) */
119#define DEVICE_STV_672 0x0001
120#define DEVICE_STV_676 0x0002
121
122enum frame_status {
123 FRAME_EMPTY,
124 FRAME_READING, /* In the process of being grabbed into */
125 FRAME_READY, /* Ready to be read */
126 FRAME_ERROR,
127};
128
129/***
130 * Register access (for USB request byte)
131 ***/
132enum {
133 CAMERAACCESS_SYSTEM = 0,
134 CAMERAACCESS_VC,
135 CAMERAACCESS_VP,
136 CAMERAACCESS_IDATA
137};
138
139#define CAMERAACCESS_TYPE_BLOCK 0x00
140#define CAMERAACCESS_TYPE_RANDOM 0x04
141#define CAMERAACCESS_TYPE_MASK 0x08
142#define CAMERAACCESS_TYPE_REPEAT 0x0C
143
144#define TRANSFER_READ 0
145#define TRANSFER_WRITE 1
146
147#define DEFAULT_ALT USBIF_ISO_6
148#define DEFAULT_BRIGHTNESS 0x46
149#define DEFAULT_CONTRAST 0x93
150#define DEFAULT_SATURATION 0x7f
151
152/* Power state */
153#define HI_POWER_MODE CPIA2_SYSTEM_CONTROL_HIGH_POWER
154#define LO_POWER_MODE CPIA2_SYSTEM_CONTROL_LOW_POWER
155
156
157/********
158 * Commands
159 *******/
160enum {
161 CPIA2_CMD_NONE = 0,
162 CPIA2_CMD_GET_VERSION,
163 CPIA2_CMD_GET_PNP_ID,
164 CPIA2_CMD_GET_ASIC_TYPE,
165 CPIA2_CMD_GET_SENSOR,
166 CPIA2_CMD_GET_VP_DEVICE,
167 CPIA2_CMD_GET_VP_BRIGHTNESS,
168 CPIA2_CMD_SET_VP_BRIGHTNESS,
169 CPIA2_CMD_GET_CONTRAST,
170 CPIA2_CMD_SET_CONTRAST,
171 CPIA2_CMD_GET_VP_SATURATION,
172 CPIA2_CMD_SET_VP_SATURATION,
173 CPIA2_CMD_GET_VP_GPIO_DIRECTION,
174 CPIA2_CMD_SET_VP_GPIO_DIRECTION,
175 CPIA2_CMD_GET_VP_GPIO_DATA,
176 CPIA2_CMD_SET_VP_GPIO_DATA,
177 CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION,
178 CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
179 CPIA2_CMD_GET_VC_MP_GPIO_DATA,
180 CPIA2_CMD_SET_VC_MP_GPIO_DATA,
181 CPIA2_CMD_ENABLE_PACKET_CTRL,
182 CPIA2_CMD_GET_FLICKER_MODES,
183 CPIA2_CMD_SET_FLICKER_MODES,
184 CPIA2_CMD_RESET_FIFO, /* clear fifo and enable stream block */
185 CPIA2_CMD_SET_HI_POWER,
186 CPIA2_CMD_SET_LOW_POWER,
187 CPIA2_CMD_CLEAR_V2W_ERR,
188 CPIA2_CMD_SET_USER_MODE,
189 CPIA2_CMD_GET_USER_MODE,
190 CPIA2_CMD_FRAMERATE_REQ,
191 CPIA2_CMD_SET_COMPRESSION_STATE,
192 CPIA2_CMD_GET_WAKEUP,
193 CPIA2_CMD_SET_WAKEUP,
194 CPIA2_CMD_GET_PW_CONTROL,
195 CPIA2_CMD_SET_PW_CONTROL,
196 CPIA2_CMD_GET_SYSTEM_CTRL,
197 CPIA2_CMD_SET_SYSTEM_CTRL,
198 CPIA2_CMD_GET_VP_SYSTEM_STATE,
199 CPIA2_CMD_GET_VP_SYSTEM_CTRL,
200 CPIA2_CMD_SET_VP_SYSTEM_CTRL,
201 CPIA2_CMD_GET_VP_EXP_MODES,
202 CPIA2_CMD_SET_VP_EXP_MODES,
203 CPIA2_CMD_GET_DEVICE_CONFIG,
204 CPIA2_CMD_SET_DEVICE_CONFIG,
205 CPIA2_CMD_SET_SERIAL_ADDR,
206 CPIA2_CMD_SET_SENSOR_CR1,
207 CPIA2_CMD_GET_VC_CONTROL,
208 CPIA2_CMD_SET_VC_CONTROL,
209 CPIA2_CMD_SET_TARGET_KB,
210 CPIA2_CMD_SET_DEF_JPEG_OPT,
211 CPIA2_CMD_REHASH_VP4,
212 CPIA2_CMD_GET_USER_EFFECTS,
213 CPIA2_CMD_SET_USER_EFFECTS
214};
215
216enum user_cmd {
217 COMMAND_NONE = 0x00000001,
218 COMMAND_SET_FPS = 0x00000002,
219 COMMAND_SET_COLOR_PARAMS = 0x00000004,
220 COMMAND_GET_COLOR_PARAMS = 0x00000008,
221 COMMAND_SET_FORMAT = 0x00000010, /* size, etc */
222 COMMAND_SET_FLICKER = 0x00000020
223};
224
225/***
226 * Some defines specific to the 676 chip
227 ***/
228#define CAMACC_CIF 0x01
229#define CAMACC_VGA 0x02
230#define CAMACC_QCIF 0x04
231#define CAMACC_QVGA 0x08
232
233
234struct cpia2_register {
235 u8 index;
236 u8 value;
237};
238
239struct cpia2_reg_mask {
240 u8 index;
241 u8 and_mask;
242 u8 or_mask;
243 u8 fill;
244};
245
246struct cpia2_command {
247 u32 command;
248 u8 req_mode; /* (Block or random) | registerBank */
249 u8 reg_count;
250 u8 direction;
251 u8 start;
252 union reg_types {
253 struct cpia2_register registers[32];
254 struct cpia2_reg_mask masks[16];
255 u8 block_data[64];
256 u8 *patch_data; /* points to function defined block */
257 } buffer;
258};
259
260struct camera_params {
261 struct {
262 u8 firmware_revision_hi; /* For system register set (bank 0) */
263 u8 firmware_revision_lo;
264 u8 asic_id; /* Video Compressor set (bank 1) */
265 u8 asic_rev;
266 u8 vp_device_hi; /* Video Processor set (bank 2) */
267 u8 vp_device_lo;
268 u8 sensor_flags;
269 u8 sensor_rev;
270 } version;
271
272 struct {
273 u32 device_type; /* enumerated from vendor/product ids.
274 * Currently, either STV_672 or STV_676 */
275 u16 vendor;
276 u16 product;
277 u16 device_revision;
278 } pnp_id;
279
280 struct {
281 u8 brightness; /* CPIA2_VP_EXPOSURE_TARGET */
282 u8 contrast; /* Note: this is CPIA2_VP_YRANGE */
283 u8 saturation; /* CPIA2_VP_SATURATION */
284 } color_params;
285
286 struct {
287 u8 cam_register;
288 u8 flicker_mode_req; /* 1 if flicker on, else never flicker */
289 } flicker_control;
290
291 struct {
292 u8 jpeg_options;
293 u8 creep_period;
294 u8 user_squeeze;
295 u8 inhibit_htables;
296 } compression;
297
298 struct {
299 u8 ohsize; /* output image size */
300 u8 ovsize;
301 u8 hcrop; /* cropping start_pos/4 */
302 u8 vcrop;
303 u8 hphase; /* scaling registers */
304 u8 vphase;
305 u8 hispan;
306 u8 vispan;
307 u8 hicrop;
308 u8 vicrop;
309 u8 hifraction;
310 u8 vifraction;
311 } image_size;
312
313 struct {
314 int width; /* actual window width */
315 int height; /* actual window height */
316 } roi;
317
318 struct {
319 u8 video_mode;
320 u8 frame_rate;
321 u8 video_size; /* Not a register, just a convenience for cropped sizes */
322 u8 gpio_direction;
323 u8 gpio_data;
324 u8 system_ctrl;
325 u8 system_state;
326 u8 lowlight_boost; /* Bool: 0 = off, 1 = on */
327 u8 device_config;
328 u8 exposure_modes;
329 u8 user_effects;
330 } vp_params;
331
332 struct {
333 u8 pw_control;
334 u8 wakeup;
335 u8 vc_control;
336 u8 vc_mp_direction;
337 u8 vc_mp_data;
338 u8 quality;
339 } vc_params;
340
341 struct {
342 u8 power_mode;
343 u8 system_ctrl;
344 u8 stream_mode; /* This is the current alternate for usb drivers */
345 u8 allow_corrupt;
346 } camera_state;
347};
348
349#define NUM_SBUF 2
350
351struct cpia2_sbuf {
352 char *data;
353 struct urb *urb;
354};
355
356struct framebuf {
357 struct timeval timestamp;
358 unsigned long seq;
359 int num;
360 int length;
361 int max_length;
362 volatile enum frame_status status;
363 u8 *data;
364 struct framebuf *next;
365};
366
367struct camera_data {
368 /* locks */
369 struct v4l2_device v4l2_dev;
370 struct mutex v4l2_lock; /* serialize file operations */
371 struct v4l2_ctrl_handler hdl;
372 struct {
373 /* Lights control cluster */
374 struct v4l2_ctrl *top_light;
375 struct v4l2_ctrl *bottom_light;
376 };
377 struct v4l2_ctrl *usb_alt;
378
379 /* camera status */
380 int first_image_seen;
381 enum sensors sensor_type;
382 u8 flush;
383 struct v4l2_fh *stream_fh;
384 u8 mmapped;
385 int streaming; /* 0 = no, 1 = yes */
386 int xfer_mode; /* XFER_BULK or XFER_ISOC */
387 struct camera_params params; /* camera settings */
388
389 /* v4l */
390 int video_size; /* VIDEO_SIZE_ */
391 struct video_device vdev; /* v4l videodev */
392 u32 width;
393 u32 height; /* Its size */
394 __u32 pixelformat; /* Format fourcc */
395
396 /* USB */
397 struct usb_device *dev;
398 unsigned char iface;
399 unsigned int cur_alt;
400 unsigned int old_alt;
401 struct cpia2_sbuf sbuf[NUM_SBUF]; /* Double buffering */
402
403 wait_queue_head_t wq_stream;
404
405 /* Buffering */
406 u32 frame_size;
407 int num_frames;
408 unsigned long frame_count;
409 u8 *frame_buffer; /* frame buffer data */
410 struct framebuf *buffers;
411 struct framebuf * volatile curbuff;
412 struct framebuf *workbuff;
413
414 /* MJPEG Extension */
415 int APPn; /* Number of APP segment to be written, must be 0..15 */
416 int APP_len; /* Length of data in JPEG APPn segment */
417 char APP_data[60]; /* Data in the JPEG APPn segment. */
418
419 int COM_len; /* Length of data in JPEG COM segment */
420 char COM_data[60]; /* Data in JPEG COM segment */
421};
422
423/* v4l */
424int cpia2_register_camera(struct camera_data *cam);
425void cpia2_unregister_camera(struct camera_data *cam);
426void cpia2_camera_release(struct v4l2_device *v4l2_dev);
427
428/* core */
429int cpia2_reset_camera(struct camera_data *cam);
430int cpia2_set_low_power(struct camera_data *cam);
431void cpia2_dbg_dump_registers(struct camera_data *cam);
432int cpia2_match_video_size(int width, int height);
433void cpia2_set_camera_state(struct camera_data *cam);
434void cpia2_save_camera_state(struct camera_data *cam);
435void cpia2_set_color_params(struct camera_data *cam);
436void cpia2_set_brightness(struct camera_data *cam, unsigned char value);
437void cpia2_set_contrast(struct camera_data *cam, unsigned char value);
438void cpia2_set_saturation(struct camera_data *cam, unsigned char value);
439int cpia2_set_flicker_mode(struct camera_data *cam, int mode);
440void cpia2_set_format(struct camera_data *cam);
441int cpia2_send_command(struct camera_data *cam, struct cpia2_command *cmd);
442int cpia2_do_command(struct camera_data *cam,
443 unsigned int command,
444 unsigned char direction, unsigned char param);
445struct camera_data *cpia2_init_camera_struct(struct usb_interface *intf);
446int cpia2_init_camera(struct camera_data *cam);
447int cpia2_allocate_buffers(struct camera_data *cam);
448void cpia2_free_buffers(struct camera_data *cam);
449long cpia2_read(struct camera_data *cam,
450 char __user *buf, unsigned long count, int noblock);
451unsigned int cpia2_poll(struct camera_data *cam,
452 struct file *filp, poll_table *wait);
453int cpia2_remap_buffer(struct camera_data *cam, struct vm_area_struct *vma);
454void cpia2_set_property_flip(struct camera_data *cam, int prop_val);
455void cpia2_set_property_mirror(struct camera_data *cam, int prop_val);
456int cpia2_set_gpio(struct camera_data *cam, unsigned char setting);
457int cpia2_set_fps(struct camera_data *cam, int framerate);
458
459/* usb */
460int cpia2_usb_init(void);
461void cpia2_usb_cleanup(void);
462int cpia2_usb_transfer_cmd(struct camera_data *cam, void *registers,
463 u8 request, u8 start, u8 count, u8 direction);
464int cpia2_usb_stream_start(struct camera_data *cam, unsigned int alternate);
465int cpia2_usb_stream_stop(struct camera_data *cam);
466int cpia2_usb_stream_pause(struct camera_data *cam);
467int cpia2_usb_stream_resume(struct camera_data *cam);
468int cpia2_usb_change_streaming_alternate(struct camera_data *cam,
469 unsigned int alt);
470
471
472/* ----------------------- debug functions ---------------------- */
473#ifdef _CPIA2_DEBUG_
474#define ALOG(lev, fmt, args...) printk(lev "%s:%d %s(): " fmt, __FILE__, __LINE__, __func__, ## args)
475#define LOG(fmt, args...) ALOG(KERN_INFO, fmt, ## args)
476#define ERR(fmt, args...) ALOG(KERN_ERR, fmt, ## args)
477#define DBG(fmt, args...) ALOG(KERN_DEBUG, fmt, ## args)
478#else
479#define ALOG(fmt,args...) printk(fmt,##args)
480#define LOG(fmt,args...) ALOG(KERN_INFO "cpia2: "fmt,##args)
481#define ERR(fmt,args...) ALOG(KERN_ERR "cpia2: "fmt,##args)
482#define DBG(fmn,args...) do {} while(0)
483#endif
484/* No function or lineno, for shorter lines */
485#define KINFO(fmt, args...) printk(KERN_INFO fmt,##args)
486
487#endif
diff --git a/drivers/media/video/cpia2/cpia2_core.c b/drivers/media/video/cpia2/cpia2_core.c
deleted file mode 100644
index 187012ce444b..000000000000
--- a/drivers/media/video/cpia2/cpia2_core.c
+++ /dev/null
@@ -1,2417 +0,0 @@
1/****************************************************************************
2 *
3 * Filename: cpia2_core.c
4 *
5 * Copyright 2001, STMicrolectronics, Inc.
6 * Contact: steve.miller@st.com
7 *
8 * Description:
9 * This is a USB driver for CPia2 based video cameras.
10 * The infrastructure of this driver is based on the cpia usb driver by
11 * Jochen Scharrlach and Johannes Erdfeldt.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 * Stripped of 2.4 stuff ready for main kernel submit by
28 * Alan Cox <alan@lxorguk.ukuu.org.uk>
29 *
30 ****************************************************************************/
31
32#include "cpia2.h"
33
34#include <linux/module.h>
35#include <linux/slab.h>
36#include <linux/mm.h>
37#include <linux/vmalloc.h>
38#include <linux/firmware.h>
39
40#define FIRMWARE "cpia2/stv0672_vp4.bin"
41MODULE_FIRMWARE(FIRMWARE);
42
43/* #define _CPIA2_DEBUG_ */
44
45#ifdef _CPIA2_DEBUG_
46
47static const char *block_name[] = {
48 "System",
49 "VC",
50 "VP",
51 "IDATA"
52};
53#endif
54
55static unsigned int debugs_on; /* default 0 - DEBUG_REG */
56
57
58/******************************************************************************
59 *
60 * Forward Declarations
61 *
62 *****************************************************************************/
63static int apply_vp_patch(struct camera_data *cam);
64static int set_default_user_mode(struct camera_data *cam);
65static int set_vw_size(struct camera_data *cam, int size);
66static int configure_sensor(struct camera_data *cam,
67 int reqwidth, int reqheight);
68static int config_sensor_410(struct camera_data *cam,
69 int reqwidth, int reqheight);
70static int config_sensor_500(struct camera_data *cam,
71 int reqwidth, int reqheight);
72static int set_all_properties(struct camera_data *cam);
73static void wake_system(struct camera_data *cam);
74static void set_lowlight_boost(struct camera_data *cam);
75static void reset_camera_struct(struct camera_data *cam);
76static int cpia2_set_high_power(struct camera_data *cam);
77
78/* Here we want the physical address of the memory.
79 * This is used when initializing the contents of the
80 * area and marking the pages as reserved.
81 */
82static inline unsigned long kvirt_to_pa(unsigned long adr)
83{
84 unsigned long kva, ret;
85
86 kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
87 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
88 ret = __pa(kva);
89 return ret;
90}
91
92static void *rvmalloc(unsigned long size)
93{
94 void *mem;
95 unsigned long adr;
96
97 /* Round it off to PAGE_SIZE */
98 size = PAGE_ALIGN(size);
99
100 mem = vmalloc_32(size);
101 if (!mem)
102 return NULL;
103
104 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
105 adr = (unsigned long) mem;
106
107 while ((long)size > 0) {
108 SetPageReserved(vmalloc_to_page((void *)adr));
109 adr += PAGE_SIZE;
110 size -= PAGE_SIZE;
111 }
112 return mem;
113}
114
115static void rvfree(void *mem, unsigned long size)
116{
117 unsigned long adr;
118
119 if (!mem)
120 return;
121
122 size = PAGE_ALIGN(size);
123
124 adr = (unsigned long) mem;
125 while ((long)size > 0) {
126 ClearPageReserved(vmalloc_to_page((void *)adr));
127 adr += PAGE_SIZE;
128 size -= PAGE_SIZE;
129 }
130 vfree(mem);
131}
132
133/******************************************************************************
134 *
135 * cpia2_do_command
136 *
137 * Send an arbitrary command to the camera. For commands that read from
138 * the camera, copy the buffers into the proper param structures.
139 *****************************************************************************/
140int cpia2_do_command(struct camera_data *cam,
141 u32 command, u8 direction, u8 param)
142{
143 int retval = 0;
144 struct cpia2_command cmd;
145 unsigned int device = cam->params.pnp_id.device_type;
146
147 cmd.command = command;
148 cmd.reg_count = 2; /* default */
149 cmd.direction = direction;
150
151 /***
152 * Set up the command.
153 ***/
154 switch (command) {
155 case CPIA2_CMD_GET_VERSION:
156 cmd.req_mode =
157 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
158 cmd.start = CPIA2_SYSTEM_DEVICE_HI;
159 break;
160 case CPIA2_CMD_GET_PNP_ID:
161 cmd.req_mode =
162 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
163 cmd.reg_count = 8;
164 cmd.start = CPIA2_SYSTEM_DESCRIP_VID_HI;
165 break;
166 case CPIA2_CMD_GET_ASIC_TYPE:
167 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
168 cmd.start = CPIA2_VC_ASIC_ID;
169 break;
170 case CPIA2_CMD_GET_SENSOR:
171 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
172 cmd.start = CPIA2_VP_SENSOR_FLAGS;
173 break;
174 case CPIA2_CMD_GET_VP_DEVICE:
175 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
176 cmd.start = CPIA2_VP_DEVICEH;
177 break;
178 case CPIA2_CMD_SET_VP_BRIGHTNESS:
179 cmd.buffer.block_data[0] = param; /* Then fall through */
180 case CPIA2_CMD_GET_VP_BRIGHTNESS:
181 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
182 cmd.reg_count = 1;
183 if (device == DEVICE_STV_672)
184 cmd.start = CPIA2_VP4_EXPOSURE_TARGET;
185 else
186 cmd.start = CPIA2_VP5_EXPOSURE_TARGET;
187 break;
188 case CPIA2_CMD_SET_CONTRAST:
189 cmd.buffer.block_data[0] = param; /* Then fall through */
190 case CPIA2_CMD_GET_CONTRAST:
191 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
192 cmd.reg_count = 1;
193 cmd.start = CPIA2_VP_YRANGE;
194 break;
195 case CPIA2_CMD_SET_VP_SATURATION:
196 cmd.buffer.block_data[0] = param; /* Then fall through */
197 case CPIA2_CMD_GET_VP_SATURATION:
198 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
199 cmd.reg_count = 1;
200 if (device == DEVICE_STV_672)
201 cmd.start = CPIA2_VP_SATURATION;
202 else
203 cmd.start = CPIA2_VP5_MCUVSATURATION;
204 break;
205 case CPIA2_CMD_SET_VP_GPIO_DATA:
206 cmd.buffer.block_data[0] = param; /* Then fall through */
207 case CPIA2_CMD_GET_VP_GPIO_DATA:
208 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
209 cmd.reg_count = 1;
210 cmd.start = CPIA2_VP_GPIO_DATA;
211 break;
212 case CPIA2_CMD_SET_VP_GPIO_DIRECTION:
213 cmd.buffer.block_data[0] = param; /* Then fall through */
214 case CPIA2_CMD_GET_VP_GPIO_DIRECTION:
215 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
216 cmd.reg_count = 1;
217 cmd.start = CPIA2_VP_GPIO_DIRECTION;
218 break;
219 case CPIA2_CMD_SET_VC_MP_GPIO_DATA:
220 cmd.buffer.block_data[0] = param; /* Then fall through */
221 case CPIA2_CMD_GET_VC_MP_GPIO_DATA:
222 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
223 cmd.reg_count = 1;
224 cmd.start = CPIA2_VC_MP_DATA;
225 break;
226 case CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION:
227 cmd.buffer.block_data[0] = param; /* Then fall through */
228 case CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION:
229 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
230 cmd.reg_count = 1;
231 cmd.start = CPIA2_VC_MP_DIR;
232 break;
233 case CPIA2_CMD_ENABLE_PACKET_CTRL:
234 cmd.req_mode =
235 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
236 cmd.start = CPIA2_SYSTEM_INT_PACKET_CTRL;
237 cmd.reg_count = 1;
238 cmd.buffer.block_data[0] = param;
239 break;
240 case CPIA2_CMD_SET_FLICKER_MODES:
241 cmd.buffer.block_data[0] = param; /* Then fall through */
242 case CPIA2_CMD_GET_FLICKER_MODES:
243 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
244 cmd.reg_count = 1;
245 cmd.start = CPIA2_VP_FLICKER_MODES;
246 break;
247 case CPIA2_CMD_RESET_FIFO: /* clear fifo and enable stream block */
248 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
249 cmd.reg_count = 2;
250 cmd.start = 0;
251 cmd.buffer.registers[0].index = CPIA2_VC_ST_CTRL;
252 cmd.buffer.registers[0].value = CPIA2_VC_ST_CTRL_SRC_VC |
253 CPIA2_VC_ST_CTRL_DST_USB | CPIA2_VC_ST_CTRL_EOF_DETECT;
254 cmd.buffer.registers[1].index = CPIA2_VC_ST_CTRL;
255 cmd.buffer.registers[1].value = CPIA2_VC_ST_CTRL_SRC_VC |
256 CPIA2_VC_ST_CTRL_DST_USB |
257 CPIA2_VC_ST_CTRL_EOF_DETECT |
258 CPIA2_VC_ST_CTRL_FIFO_ENABLE;
259 break;
260 case CPIA2_CMD_SET_HI_POWER:
261 cmd.req_mode =
262 CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_SYSTEM;
263 cmd.reg_count = 2;
264 cmd.buffer.registers[0].index =
265 CPIA2_SYSTEM_SYSTEM_CONTROL;
266 cmd.buffer.registers[1].index =
267 CPIA2_SYSTEM_SYSTEM_CONTROL;
268 cmd.buffer.registers[0].value = CPIA2_SYSTEM_CONTROL_CLEAR_ERR;
269 cmd.buffer.registers[1].value =
270 CPIA2_SYSTEM_CONTROL_HIGH_POWER;
271 break;
272 case CPIA2_CMD_SET_LOW_POWER:
273 cmd.req_mode =
274 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
275 cmd.reg_count = 1;
276 cmd.start = CPIA2_SYSTEM_SYSTEM_CONTROL;
277 cmd.buffer.block_data[0] = 0;
278 break;
279 case CPIA2_CMD_CLEAR_V2W_ERR:
280 cmd.req_mode =
281 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
282 cmd.reg_count = 1;
283 cmd.start = CPIA2_SYSTEM_SYSTEM_CONTROL;
284 cmd.buffer.block_data[0] = CPIA2_SYSTEM_CONTROL_CLEAR_ERR;
285 break;
286 case CPIA2_CMD_SET_USER_MODE: /* Then fall through */
287 cmd.buffer.block_data[0] = param;
288 case CPIA2_CMD_GET_USER_MODE:
289 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
290 cmd.reg_count = 1;
291 if (device == DEVICE_STV_672)
292 cmd.start = CPIA2_VP4_USER_MODE;
293 else
294 cmd.start = CPIA2_VP5_USER_MODE;
295 break;
296 case CPIA2_CMD_FRAMERATE_REQ:
297 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
298 cmd.reg_count = 1;
299 if (device == DEVICE_STV_672)
300 cmd.start = CPIA2_VP4_FRAMERATE_REQUEST;
301 else
302 cmd.start = CPIA2_VP5_FRAMERATE_REQUEST;
303 cmd.buffer.block_data[0] = param;
304 break;
305 case CPIA2_CMD_SET_WAKEUP:
306 cmd.buffer.block_data[0] = param; /* Then fall through */
307 case CPIA2_CMD_GET_WAKEUP:
308 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
309 cmd.reg_count = 1;
310 cmd.start = CPIA2_VC_WAKEUP;
311 break;
312 case CPIA2_CMD_SET_PW_CONTROL:
313 cmd.buffer.block_data[0] = param; /* Then fall through */
314 case CPIA2_CMD_GET_PW_CONTROL:
315 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
316 cmd.reg_count = 1;
317 cmd.start = CPIA2_VC_PW_CTRL;
318 break;
319 case CPIA2_CMD_GET_VP_SYSTEM_STATE:
320 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
321 cmd.reg_count = 1;
322 cmd.start = CPIA2_VP_SYSTEMSTATE;
323 break;
324 case CPIA2_CMD_SET_SYSTEM_CTRL:
325 cmd.buffer.block_data[0] = param; /* Then fall through */
326 case CPIA2_CMD_GET_SYSTEM_CTRL:
327 cmd.req_mode =
328 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
329 cmd.reg_count = 1;
330 cmd.start = CPIA2_SYSTEM_SYSTEM_CONTROL;
331 break;
332 case CPIA2_CMD_SET_VP_SYSTEM_CTRL:
333 cmd.buffer.block_data[0] = param; /* Then fall through */
334 case CPIA2_CMD_GET_VP_SYSTEM_CTRL:
335 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
336 cmd.reg_count = 1;
337 cmd.start = CPIA2_VP_SYSTEMCTRL;
338 break;
339 case CPIA2_CMD_SET_VP_EXP_MODES:
340 cmd.buffer.block_data[0] = param; /* Then fall through */
341 case CPIA2_CMD_GET_VP_EXP_MODES:
342 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
343 cmd.reg_count = 1;
344 cmd.start = CPIA2_VP_EXPOSURE_MODES;
345 break;
346 case CPIA2_CMD_SET_DEVICE_CONFIG:
347 cmd.buffer.block_data[0] = param; /* Then fall through */
348 case CPIA2_CMD_GET_DEVICE_CONFIG:
349 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
350 cmd.reg_count = 1;
351 cmd.start = CPIA2_VP_DEVICE_CONFIG;
352 break;
353 case CPIA2_CMD_SET_SERIAL_ADDR:
354 cmd.buffer.block_data[0] = param;
355 cmd.req_mode =
356 CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
357 cmd.reg_count = 1;
358 cmd.start = CPIA2_SYSTEM_VP_SERIAL_ADDR;
359 break;
360 case CPIA2_CMD_SET_SENSOR_CR1:
361 cmd.buffer.block_data[0] = param;
362 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
363 cmd.reg_count = 1;
364 cmd.start = CPIA2_SENSOR_CR1;
365 break;
366 case CPIA2_CMD_SET_VC_CONTROL:
367 cmd.buffer.block_data[0] = param; /* Then fall through */
368 case CPIA2_CMD_GET_VC_CONTROL:
369 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
370 cmd.reg_count = 1;
371 cmd.start = CPIA2_VC_VC_CTRL;
372 break;
373 case CPIA2_CMD_SET_TARGET_KB:
374 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
375 cmd.reg_count = 1;
376 cmd.buffer.registers[0].index = CPIA2_VC_VC_TARGET_KB;
377 cmd.buffer.registers[0].value = param;
378 break;
379 case CPIA2_CMD_SET_DEF_JPEG_OPT:
380 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
381 cmd.reg_count = 4;
382 cmd.buffer.registers[0].index = CPIA2_VC_VC_JPEG_OPT;
383 cmd.buffer.registers[0].value =
384 CPIA2_VC_VC_JPEG_OPT_DOUBLE_SQUEEZE;
385 cmd.buffer.registers[1].index = CPIA2_VC_VC_USER_SQUEEZE;
386 cmd.buffer.registers[1].value = 20;
387 cmd.buffer.registers[2].index = CPIA2_VC_VC_CREEP_PERIOD;
388 cmd.buffer.registers[2].value = 2;
389 cmd.buffer.registers[3].index = CPIA2_VC_VC_JPEG_OPT;
390 cmd.buffer.registers[3].value = CPIA2_VC_VC_JPEG_OPT_DEFAULT;
391 break;
392 case CPIA2_CMD_REHASH_VP4:
393 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
394 cmd.reg_count = 1;
395 cmd.start = CPIA2_VP_REHASH_VALUES;
396 cmd.buffer.block_data[0] = param;
397 break;
398 case CPIA2_CMD_SET_USER_EFFECTS: /* Note: Be careful with this as
399 this register can also affect
400 flicker modes */
401 cmd.buffer.block_data[0] = param; /* Then fall through */
402 case CPIA2_CMD_GET_USER_EFFECTS:
403 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
404 cmd.reg_count = 1;
405 if (device == DEVICE_STV_672)
406 cmd.start = CPIA2_VP4_USER_EFFECTS;
407 else
408 cmd.start = CPIA2_VP5_USER_EFFECTS;
409 break;
410 default:
411 LOG("DoCommand received invalid command\n");
412 return -EINVAL;
413 }
414
415 retval = cpia2_send_command(cam, &cmd);
416 if (retval) {
417 return retval;
418 }
419
420 /***
421 * Now copy any results from a read into the appropriate param struct.
422 ***/
423 switch (command) {
424 case CPIA2_CMD_GET_VERSION:
425 cam->params.version.firmware_revision_hi =
426 cmd.buffer.block_data[0];
427 cam->params.version.firmware_revision_lo =
428 cmd.buffer.block_data[1];
429 break;
430 case CPIA2_CMD_GET_PNP_ID:
431 cam->params.pnp_id.vendor = (cmd.buffer.block_data[0] << 8) |
432 cmd.buffer.block_data[1];
433 cam->params.pnp_id.product = (cmd.buffer.block_data[2] << 8) |
434 cmd.buffer.block_data[3];
435 cam->params.pnp_id.device_revision =
436 (cmd.buffer.block_data[4] << 8) |
437 cmd.buffer.block_data[5];
438 if (cam->params.pnp_id.vendor == 0x553) {
439 if (cam->params.pnp_id.product == 0x100) {
440 cam->params.pnp_id.device_type = DEVICE_STV_672;
441 } else if (cam->params.pnp_id.product == 0x140 ||
442 cam->params.pnp_id.product == 0x151) {
443 cam->params.pnp_id.device_type = DEVICE_STV_676;
444 }
445 }
446 break;
447 case CPIA2_CMD_GET_ASIC_TYPE:
448 cam->params.version.asic_id = cmd.buffer.block_data[0];
449 cam->params.version.asic_rev = cmd.buffer.block_data[1];
450 break;
451 case CPIA2_CMD_GET_SENSOR:
452 cam->params.version.sensor_flags = cmd.buffer.block_data[0];
453 cam->params.version.sensor_rev = cmd.buffer.block_data[1];
454 break;
455 case CPIA2_CMD_GET_VP_DEVICE:
456 cam->params.version.vp_device_hi = cmd.buffer.block_data[0];
457 cam->params.version.vp_device_lo = cmd.buffer.block_data[1];
458 break;
459 case CPIA2_CMD_GET_VP_GPIO_DATA:
460 cam->params.vp_params.gpio_data = cmd.buffer.block_data[0];
461 break;
462 case CPIA2_CMD_GET_VP_GPIO_DIRECTION:
463 cam->params.vp_params.gpio_direction = cmd.buffer.block_data[0];
464 break;
465 case CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION:
466 cam->params.vc_params.vc_mp_direction =cmd.buffer.block_data[0];
467 break;
468 case CPIA2_CMD_GET_VC_MP_GPIO_DATA:
469 cam->params.vc_params.vc_mp_data = cmd.buffer.block_data[0];
470 break;
471 case CPIA2_CMD_GET_FLICKER_MODES:
472 cam->params.flicker_control.cam_register =
473 cmd.buffer.block_data[0];
474 break;
475 case CPIA2_CMD_GET_WAKEUP:
476 cam->params.vc_params.wakeup = cmd.buffer.block_data[0];
477 break;
478 case CPIA2_CMD_GET_PW_CONTROL:
479 cam->params.vc_params.pw_control = cmd.buffer.block_data[0];
480 break;
481 case CPIA2_CMD_GET_SYSTEM_CTRL:
482 cam->params.camera_state.system_ctrl = cmd.buffer.block_data[0];
483 break;
484 case CPIA2_CMD_GET_VP_SYSTEM_STATE:
485 cam->params.vp_params.system_state = cmd.buffer.block_data[0];
486 break;
487 case CPIA2_CMD_GET_VP_SYSTEM_CTRL:
488 cam->params.vp_params.system_ctrl = cmd.buffer.block_data[0];
489 break;
490 case CPIA2_CMD_GET_VP_EXP_MODES:
491 cam->params.vp_params.exposure_modes = cmd.buffer.block_data[0];
492 break;
493 case CPIA2_CMD_GET_DEVICE_CONFIG:
494 cam->params.vp_params.device_config = cmd.buffer.block_data[0];
495 break;
496 case CPIA2_CMD_GET_VC_CONTROL:
497 cam->params.vc_params.vc_control = cmd.buffer.block_data[0];
498 break;
499 case CPIA2_CMD_GET_USER_MODE:
500 cam->params.vp_params.video_mode = cmd.buffer.block_data[0];
501 break;
502 case CPIA2_CMD_GET_USER_EFFECTS:
503 cam->params.vp_params.user_effects = cmd.buffer.block_data[0];
504 break;
505 default:
506 break;
507 }
508 return retval;
509}
510
511/******************************************************************************
512 *
513 * cpia2_send_command
514 *
515 *****************************************************************************/
516
517#define DIR(cmd) ((cmd->direction == TRANSFER_WRITE) ? "Write" : "Read")
518#define BINDEX(cmd) (cmd->req_mode & 0x03)
519
520int cpia2_send_command(struct camera_data *cam, struct cpia2_command *cmd)
521{
522 u8 count;
523 u8 start;
524 u8 *buffer;
525 int retval;
526
527 switch (cmd->req_mode & 0x0c) {
528 case CAMERAACCESS_TYPE_RANDOM:
529 count = cmd->reg_count * sizeof(struct cpia2_register);
530 start = 0;
531 buffer = (u8 *) & cmd->buffer;
532 if (debugs_on & DEBUG_REG)
533 DBG("%s Random: Register block %s\n", DIR(cmd),
534 block_name[BINDEX(cmd)]);
535 break;
536 case CAMERAACCESS_TYPE_BLOCK:
537 count = cmd->reg_count;
538 start = cmd->start;
539 buffer = cmd->buffer.block_data;
540 if (debugs_on & DEBUG_REG)
541 DBG("%s Block: Register block %s\n", DIR(cmd),
542 block_name[BINDEX(cmd)]);
543 break;
544 case CAMERAACCESS_TYPE_MASK:
545 count = cmd->reg_count * sizeof(struct cpia2_reg_mask);
546 start = 0;
547 buffer = (u8 *) & cmd->buffer;
548 if (debugs_on & DEBUG_REG)
549 DBG("%s Mask: Register block %s\n", DIR(cmd),
550 block_name[BINDEX(cmd)]);
551 break;
552 case CAMERAACCESS_TYPE_REPEAT: /* For patch blocks only */
553 count = cmd->reg_count;
554 start = cmd->start;
555 buffer = cmd->buffer.block_data;
556 if (debugs_on & DEBUG_REG)
557 DBG("%s Repeat: Register block %s\n", DIR(cmd),
558 block_name[BINDEX(cmd)]);
559 break;
560 default:
561 LOG("%s: invalid request mode\n",__func__);
562 return -EINVAL;
563 }
564
565 retval = cpia2_usb_transfer_cmd(cam,
566 buffer,
567 cmd->req_mode,
568 start, count, cmd->direction);
569#ifdef _CPIA2_DEBUG_
570 if (debugs_on & DEBUG_REG) {
571 int i;
572 for (i = 0; i < cmd->reg_count; i++) {
573 if((cmd->req_mode & 0x0c) == CAMERAACCESS_TYPE_BLOCK)
574 KINFO("%s Block: [0x%02X] = 0x%02X\n",
575 DIR(cmd), start + i, buffer[i]);
576 if((cmd->req_mode & 0x0c) == CAMERAACCESS_TYPE_RANDOM)
577 KINFO("%s Random: [0x%02X] = 0x%02X\n",
578 DIR(cmd), cmd->buffer.registers[i].index,
579 cmd->buffer.registers[i].value);
580 }
581 }
582#endif
583
584 return retval;
585};
586
587/*************
588 * Functions to implement camera functionality
589 *************/
590/******************************************************************************
591 *
592 * cpia2_get_version_info
593 *
594 *****************************************************************************/
595static void cpia2_get_version_info(struct camera_data *cam)
596{
597 cpia2_do_command(cam, CPIA2_CMD_GET_VERSION, TRANSFER_READ, 0);
598 cpia2_do_command(cam, CPIA2_CMD_GET_PNP_ID, TRANSFER_READ, 0);
599 cpia2_do_command(cam, CPIA2_CMD_GET_ASIC_TYPE, TRANSFER_READ, 0);
600 cpia2_do_command(cam, CPIA2_CMD_GET_SENSOR, TRANSFER_READ, 0);
601 cpia2_do_command(cam, CPIA2_CMD_GET_VP_DEVICE, TRANSFER_READ, 0);
602}
603
604/******************************************************************************
605 *
606 * cpia2_reset_camera
607 *
608 * Called at least during the open process, sets up initial params.
609 *****************************************************************************/
610int cpia2_reset_camera(struct camera_data *cam)
611{
612 u8 tmp_reg;
613 int retval = 0;
614 int target_kb;
615 int i;
616 struct cpia2_command cmd;
617
618 /***
619 * VC setup
620 ***/
621 retval = configure_sensor(cam,
622 cam->params.roi.width,
623 cam->params.roi.height);
624 if (retval < 0) {
625 ERR("Couldn't configure sensor, error=%d\n", retval);
626 return retval;
627 }
628
629 /* Clear FIFO and route/enable stream block */
630 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
631 cmd.direction = TRANSFER_WRITE;
632 cmd.reg_count = 2;
633 cmd.buffer.registers[0].index = CPIA2_VC_ST_CTRL;
634 cmd.buffer.registers[0].value = CPIA2_VC_ST_CTRL_SRC_VC |
635 CPIA2_VC_ST_CTRL_DST_USB | CPIA2_VC_ST_CTRL_EOF_DETECT;
636 cmd.buffer.registers[1].index = CPIA2_VC_ST_CTRL;
637 cmd.buffer.registers[1].value = CPIA2_VC_ST_CTRL_SRC_VC |
638 CPIA2_VC_ST_CTRL_DST_USB |
639 CPIA2_VC_ST_CTRL_EOF_DETECT | CPIA2_VC_ST_CTRL_FIFO_ENABLE;
640
641 cpia2_send_command(cam, &cmd);
642
643 cpia2_set_high_power(cam);
644
645 if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
646 /* Enable button notification */
647 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_SYSTEM;
648 cmd.buffer.registers[0].index = CPIA2_SYSTEM_INT_PACKET_CTRL;
649 cmd.buffer.registers[0].value =
650 CPIA2_SYSTEM_INT_PACKET_CTRL_ENABLE_SW_XX;
651 cmd.reg_count = 1;
652 cpia2_send_command(cam, &cmd);
653 }
654
655 schedule_timeout_interruptible(msecs_to_jiffies(100));
656
657 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
658 retval = apply_vp_patch(cam);
659
660 /* wait for vp to go to sleep */
661 schedule_timeout_interruptible(msecs_to_jiffies(100));
662
663 /***
664 * If this is a 676, apply VP5 fixes before we start streaming
665 ***/
666 if (cam->params.pnp_id.device_type == DEVICE_STV_676) {
667 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VP;
668
669 /* The following writes improve the picture */
670 cmd.buffer.registers[0].index = CPIA2_VP5_MYBLACK_LEVEL;
671 cmd.buffer.registers[0].value = 0; /* reduce from the default
672 * rec 601 pedestal of 16 */
673 cmd.buffer.registers[1].index = CPIA2_VP5_MCYRANGE;
674 cmd.buffer.registers[1].value = 0x92; /* increase from 100% to
675 * (256/256 - 31) to fill
676 * available range */
677 cmd.buffer.registers[2].index = CPIA2_VP5_MYCEILING;
678 cmd.buffer.registers[2].value = 0xFF; /* Increase from the
679 * default rec 601 ceiling
680 * of 240 */
681 cmd.buffer.registers[3].index = CPIA2_VP5_MCUVSATURATION;
682 cmd.buffer.registers[3].value = 0xFF; /* Increase from the rec
683 * 601 100% level (128)
684 * to 145-192 */
685 cmd.buffer.registers[4].index = CPIA2_VP5_ANTIFLKRSETUP;
686 cmd.buffer.registers[4].value = 0x80; /* Inhibit the
687 * anti-flicker */
688
689 /* The following 4 writes are a fix to allow QVGA to work at 30 fps */
690 cmd.buffer.registers[5].index = CPIA2_VP_RAM_ADDR_H;
691 cmd.buffer.registers[5].value = 0x01;
692 cmd.buffer.registers[6].index = CPIA2_VP_RAM_ADDR_L;
693 cmd.buffer.registers[6].value = 0xE3;
694 cmd.buffer.registers[7].index = CPIA2_VP_RAM_DATA;
695 cmd.buffer.registers[7].value = 0x02;
696 cmd.buffer.registers[8].index = CPIA2_VP_RAM_DATA;
697 cmd.buffer.registers[8].value = 0xFC;
698
699 cmd.direction = TRANSFER_WRITE;
700 cmd.reg_count = 9;
701
702 cpia2_send_command(cam, &cmd);
703 }
704
705 /* Activate all settings and start the data stream */
706 /* Set user mode */
707 set_default_user_mode(cam);
708
709 /* Give VP time to wake up */
710 schedule_timeout_interruptible(msecs_to_jiffies(100));
711
712 set_all_properties(cam);
713
714 cpia2_do_command(cam, CPIA2_CMD_GET_USER_MODE, TRANSFER_READ, 0);
715 DBG("After SetAllProperties(cam), user mode is 0x%0X\n",
716 cam->params.vp_params.video_mode);
717
718 /***
719 * Set audio regulator off. This and the code to set the compresison
720 * state are too complex to form a CPIA2_CMD_, and seem to be somewhat
721 * intertwined. This stuff came straight from the windows driver.
722 ***/
723 /* Turn AutoExposure off in VP and enable the serial bridge to the sensor */
724 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SYSTEM_CTRL, TRANSFER_READ, 0);
725 tmp_reg = cam->params.vp_params.system_ctrl;
726 cmd.buffer.registers[0].value = tmp_reg &
727 (tmp_reg & (CPIA2_VP_SYSTEMCTRL_HK_CONTROL ^ 0xFF));
728
729 cpia2_do_command(cam, CPIA2_CMD_GET_DEVICE_CONFIG, TRANSFER_READ, 0);
730 cmd.buffer.registers[1].value = cam->params.vp_params.device_config |
731 CPIA2_VP_DEVICE_CONFIG_SERIAL_BRIDGE;
732 cmd.buffer.registers[0].index = CPIA2_VP_SYSTEMCTRL;
733 cmd.buffer.registers[1].index = CPIA2_VP_DEVICE_CONFIG;
734 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VP;
735 cmd.reg_count = 2;
736 cmd.direction = TRANSFER_WRITE;
737 cmd.start = 0;
738 cpia2_send_command(cam, &cmd);
739
740 /* Set the correct I2C address in the CPiA-2 system register */
741 cpia2_do_command(cam,
742 CPIA2_CMD_SET_SERIAL_ADDR,
743 TRANSFER_WRITE,
744 CPIA2_SYSTEM_VP_SERIAL_ADDR_SENSOR);
745
746 /* Now have sensor access - set bit to turn the audio regulator off */
747 cpia2_do_command(cam,
748 CPIA2_CMD_SET_SENSOR_CR1,
749 TRANSFER_WRITE, CPIA2_SENSOR_CR1_DOWN_AUDIO_REGULATOR);
750
751 /* Set the correct I2C address in the CPiA-2 system register */
752 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
753 cpia2_do_command(cam,
754 CPIA2_CMD_SET_SERIAL_ADDR,
755 TRANSFER_WRITE,
756 CPIA2_SYSTEM_VP_SERIAL_ADDR_VP); // 0x88
757 else
758 cpia2_do_command(cam,
759 CPIA2_CMD_SET_SERIAL_ADDR,
760 TRANSFER_WRITE,
761 CPIA2_SYSTEM_VP_SERIAL_ADDR_676_VP); // 0x8a
762
763 /* increase signal drive strength */
764 if (cam->params.pnp_id.device_type == DEVICE_STV_676)
765 cpia2_do_command(cam,
766 CPIA2_CMD_SET_VP_EXP_MODES,
767 TRANSFER_WRITE,
768 CPIA2_VP_EXPOSURE_MODES_COMPILE_EXP);
769
770 /* Start autoexposure */
771 cpia2_do_command(cam, CPIA2_CMD_GET_DEVICE_CONFIG, TRANSFER_READ, 0);
772 cmd.buffer.registers[0].value = cam->params.vp_params.device_config &
773 (CPIA2_VP_DEVICE_CONFIG_SERIAL_BRIDGE ^ 0xFF);
774
775 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SYSTEM_CTRL, TRANSFER_READ, 0);
776 cmd.buffer.registers[1].value =
777 cam->params.vp_params.system_ctrl | CPIA2_VP_SYSTEMCTRL_HK_CONTROL;
778
779 cmd.buffer.registers[0].index = CPIA2_VP_DEVICE_CONFIG;
780 cmd.buffer.registers[1].index = CPIA2_VP_SYSTEMCTRL;
781 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VP;
782 cmd.reg_count = 2;
783 cmd.direction = TRANSFER_WRITE;
784
785 cpia2_send_command(cam, &cmd);
786
787 /* Set compression state */
788 cpia2_do_command(cam, CPIA2_CMD_GET_VC_CONTROL, TRANSFER_READ, 0);
789 if (cam->params.compression.inhibit_htables) {
790 tmp_reg = cam->params.vc_params.vc_control |
791 CPIA2_VC_VC_CTRL_INHIBIT_H_TABLES;
792 } else {
793 tmp_reg = cam->params.vc_params.vc_control &
794 ~CPIA2_VC_VC_CTRL_INHIBIT_H_TABLES;
795 }
796 cpia2_do_command(cam, CPIA2_CMD_SET_VC_CONTROL, TRANSFER_WRITE,tmp_reg);
797
798 /* Set target size (kb) on vc
799 This is a heuristic based on the quality parameter and the raw
800 framesize in kB divided by 16 (the compression factor when the
801 quality is 100%) */
802 target_kb = (cam->width * cam->height * 2 / 16384) *
803 cam->params.vc_params.quality / 100;
804 if (target_kb < 1)
805 target_kb = 1;
806 cpia2_do_command(cam, CPIA2_CMD_SET_TARGET_KB,
807 TRANSFER_WRITE, target_kb);
808
809 /* Wiggle VC Reset */
810 /***
811 * First read and wait a bit.
812 ***/
813 for (i = 0; i < 50; i++) {
814 cpia2_do_command(cam, CPIA2_CMD_GET_PW_CONTROL,
815 TRANSFER_READ, 0);
816 }
817
818 tmp_reg = cam->params.vc_params.pw_control;
819 tmp_reg &= ~CPIA2_VC_PW_CTRL_VC_RESET_N;
820
821 cpia2_do_command(cam, CPIA2_CMD_SET_PW_CONTROL, TRANSFER_WRITE,tmp_reg);
822
823 tmp_reg |= CPIA2_VC_PW_CTRL_VC_RESET_N;
824 cpia2_do_command(cam, CPIA2_CMD_SET_PW_CONTROL, TRANSFER_WRITE,tmp_reg);
825
826 cpia2_do_command(cam, CPIA2_CMD_SET_DEF_JPEG_OPT, TRANSFER_WRITE, 0);
827
828 cpia2_do_command(cam, CPIA2_CMD_GET_USER_MODE, TRANSFER_READ, 0);
829 DBG("After VC RESET, user mode is 0x%0X\n",
830 cam->params.vp_params.video_mode);
831
832 return retval;
833}
834
835/******************************************************************************
836 *
837 * cpia2_set_high_power
838 *
839 *****************************************************************************/
840static int cpia2_set_high_power(struct camera_data *cam)
841{
842 int i;
843 for (i = 0; i <= 50; i++) {
844 /* Read system status */
845 cpia2_do_command(cam,CPIA2_CMD_GET_SYSTEM_CTRL,TRANSFER_READ,0);
846
847 /* If there is an error, clear it */
848 if(cam->params.camera_state.system_ctrl &
849 CPIA2_SYSTEM_CONTROL_V2W_ERR)
850 cpia2_do_command(cam, CPIA2_CMD_CLEAR_V2W_ERR,
851 TRANSFER_WRITE, 0);
852
853 /* Try to set high power mode */
854 cpia2_do_command(cam, CPIA2_CMD_SET_SYSTEM_CTRL,
855 TRANSFER_WRITE, 1);
856
857 /* Try to read something in VP to check if everything is awake */
858 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SYSTEM_STATE,
859 TRANSFER_READ, 0);
860 if (cam->params.vp_params.system_state &
861 CPIA2_VP_SYSTEMSTATE_HK_ALIVE) {
862 break;
863 } else if (i == 50) {
864 cam->params.camera_state.power_mode = LO_POWER_MODE;
865 ERR("Camera did not wake up\n");
866 return -EIO;
867 }
868 }
869
870 DBG("System now in high power state\n");
871 cam->params.camera_state.power_mode = HI_POWER_MODE;
872 return 0;
873}
874
875/******************************************************************************
876 *
877 * cpia2_set_low_power
878 *
879 *****************************************************************************/
880int cpia2_set_low_power(struct camera_data *cam)
881{
882 cam->params.camera_state.power_mode = LO_POWER_MODE;
883 cpia2_do_command(cam, CPIA2_CMD_SET_SYSTEM_CTRL, TRANSFER_WRITE, 0);
884 return 0;
885}
886
887/******************************************************************************
888 *
889 * apply_vp_patch
890 *
891 *****************************************************************************/
892static int cpia2_send_onebyte_command(struct camera_data *cam,
893 struct cpia2_command *cmd,
894 u8 start, u8 datum)
895{
896 cmd->buffer.block_data[0] = datum;
897 cmd->start = start;
898 cmd->reg_count = 1;
899 return cpia2_send_command(cam, cmd);
900}
901
902static int apply_vp_patch(struct camera_data *cam)
903{
904 const struct firmware *fw;
905 const char fw_name[] = FIRMWARE;
906 int i, ret;
907 struct cpia2_command cmd;
908
909 ret = request_firmware(&fw, fw_name, &cam->dev->dev);
910 if (ret) {
911 printk(KERN_ERR "cpia2: failed to load VP patch \"%s\"\n",
912 fw_name);
913 return ret;
914 }
915
916 cmd.req_mode = CAMERAACCESS_TYPE_REPEAT | CAMERAACCESS_VP;
917 cmd.direction = TRANSFER_WRITE;
918
919 /* First send the start address... */
920 cpia2_send_onebyte_command(cam, &cmd, 0x0A, fw->data[0]); /* hi */
921 cpia2_send_onebyte_command(cam, &cmd, 0x0B, fw->data[1]); /* lo */
922
923 /* ... followed by the data payload */
924 for (i = 2; i < fw->size; i += 64) {
925 cmd.start = 0x0C; /* Data */
926 cmd.reg_count = min_t(int, 64, fw->size - i);
927 memcpy(cmd.buffer.block_data, &fw->data[i], cmd.reg_count);
928 cpia2_send_command(cam, &cmd);
929 }
930
931 /* Next send the start address... */
932 cpia2_send_onebyte_command(cam, &cmd, 0x0A, fw->data[0]); /* hi */
933 cpia2_send_onebyte_command(cam, &cmd, 0x0B, fw->data[1]); /* lo */
934
935 /* ... followed by the 'goto' command */
936 cpia2_send_onebyte_command(cam, &cmd, 0x0D, 1);
937
938 release_firmware(fw);
939 return 0;
940}
941
942/******************************************************************************
943 *
944 * set_default_user_mode
945 *
946 *****************************************************************************/
947static int set_default_user_mode(struct camera_data *cam)
948{
949 unsigned char user_mode;
950 unsigned char frame_rate;
951 int width = cam->params.roi.width;
952 int height = cam->params.roi.height;
953
954 switch (cam->params.version.sensor_flags) {
955 case CPIA2_VP_SENSOR_FLAGS_404:
956 case CPIA2_VP_SENSOR_FLAGS_407:
957 case CPIA2_VP_SENSOR_FLAGS_409:
958 case CPIA2_VP_SENSOR_FLAGS_410:
959 if ((width > STV_IMAGE_QCIF_COLS)
960 || (height > STV_IMAGE_QCIF_ROWS)) {
961 user_mode = CPIA2_VP_USER_MODE_CIF;
962 } else {
963 user_mode = CPIA2_VP_USER_MODE_QCIFDS;
964 }
965 frame_rate = CPIA2_VP_FRAMERATE_30;
966 break;
967 case CPIA2_VP_SENSOR_FLAGS_500:
968 if ((width > STV_IMAGE_CIF_COLS)
969 || (height > STV_IMAGE_CIF_ROWS)) {
970 user_mode = CPIA2_VP_USER_MODE_VGA;
971 } else {
972 user_mode = CPIA2_VP_USER_MODE_QVGADS;
973 }
974 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
975 frame_rate = CPIA2_VP_FRAMERATE_15;
976 else
977 frame_rate = CPIA2_VP_FRAMERATE_30;
978 break;
979 default:
980 LOG("%s: Invalid sensor flag value 0x%0X\n",__func__,
981 cam->params.version.sensor_flags);
982 return -EINVAL;
983 }
984
985 DBG("Sensor flag = 0x%0x, user mode = 0x%0x, frame rate = 0x%X\n",
986 cam->params.version.sensor_flags, user_mode, frame_rate);
987 cpia2_do_command(cam, CPIA2_CMD_SET_USER_MODE, TRANSFER_WRITE,
988 user_mode);
989 if(cam->params.vp_params.frame_rate > 0 &&
990 frame_rate > cam->params.vp_params.frame_rate)
991 frame_rate = cam->params.vp_params.frame_rate;
992
993 cpia2_set_fps(cam, frame_rate);
994
995// if (cam->params.pnp_id.device_type == DEVICE_STV_676)
996// cpia2_do_command(cam,
997// CPIA2_CMD_SET_VP_SYSTEM_CTRL,
998// TRANSFER_WRITE,
999// CPIA2_VP_SYSTEMCTRL_HK_CONTROL |
1000// CPIA2_VP_SYSTEMCTRL_POWER_CONTROL);
1001
1002 return 0;
1003}
1004
1005/******************************************************************************
1006 *
1007 * cpia2_match_video_size
1008 *
1009 * return the best match, where 'best' is as always
1010 * the largest that is not bigger than what is requested.
1011 *****************************************************************************/
1012int cpia2_match_video_size(int width, int height)
1013{
1014 if (width >= STV_IMAGE_VGA_COLS && height >= STV_IMAGE_VGA_ROWS)
1015 return VIDEOSIZE_VGA;
1016
1017 if (width >= STV_IMAGE_CIF_COLS && height >= STV_IMAGE_CIF_ROWS)
1018 return VIDEOSIZE_CIF;
1019
1020 if (width >= STV_IMAGE_QVGA_COLS && height >= STV_IMAGE_QVGA_ROWS)
1021 return VIDEOSIZE_QVGA;
1022
1023 if (width >= 288 && height >= 216)
1024 return VIDEOSIZE_288_216;
1025
1026 if (width >= 256 && height >= 192)
1027 return VIDEOSIZE_256_192;
1028
1029 if (width >= 224 && height >= 168)
1030 return VIDEOSIZE_224_168;
1031
1032 if (width >= 192 && height >= 144)
1033 return VIDEOSIZE_192_144;
1034
1035 if (width >= STV_IMAGE_QCIF_COLS && height >= STV_IMAGE_QCIF_ROWS)
1036 return VIDEOSIZE_QCIF;
1037
1038 return -1;
1039}
1040
1041/******************************************************************************
1042 *
1043 * SetVideoSize
1044 *
1045 *****************************************************************************/
1046static int set_vw_size(struct camera_data *cam, int size)
1047{
1048 int retval = 0;
1049
1050 cam->params.vp_params.video_size = size;
1051
1052 switch (size) {
1053 case VIDEOSIZE_VGA:
1054 DBG("Setting size to VGA\n");
1055 cam->params.roi.width = STV_IMAGE_VGA_COLS;
1056 cam->params.roi.height = STV_IMAGE_VGA_ROWS;
1057 cam->width = STV_IMAGE_VGA_COLS;
1058 cam->height = STV_IMAGE_VGA_ROWS;
1059 break;
1060 case VIDEOSIZE_CIF:
1061 DBG("Setting size to CIF\n");
1062 cam->params.roi.width = STV_IMAGE_CIF_COLS;
1063 cam->params.roi.height = STV_IMAGE_CIF_ROWS;
1064 cam->width = STV_IMAGE_CIF_COLS;
1065 cam->height = STV_IMAGE_CIF_ROWS;
1066 break;
1067 case VIDEOSIZE_QVGA:
1068 DBG("Setting size to QVGA\n");
1069 cam->params.roi.width = STV_IMAGE_QVGA_COLS;
1070 cam->params.roi.height = STV_IMAGE_QVGA_ROWS;
1071 cam->width = STV_IMAGE_QVGA_COLS;
1072 cam->height = STV_IMAGE_QVGA_ROWS;
1073 break;
1074 case VIDEOSIZE_288_216:
1075 cam->params.roi.width = 288;
1076 cam->params.roi.height = 216;
1077 cam->width = 288;
1078 cam->height = 216;
1079 break;
1080 case VIDEOSIZE_256_192:
1081 cam->width = 256;
1082 cam->height = 192;
1083 cam->params.roi.width = 256;
1084 cam->params.roi.height = 192;
1085 break;
1086 case VIDEOSIZE_224_168:
1087 cam->width = 224;
1088 cam->height = 168;
1089 cam->params.roi.width = 224;
1090 cam->params.roi.height = 168;
1091 break;
1092 case VIDEOSIZE_192_144:
1093 cam->width = 192;
1094 cam->height = 144;
1095 cam->params.roi.width = 192;
1096 cam->params.roi.height = 144;
1097 break;
1098 case VIDEOSIZE_QCIF:
1099 DBG("Setting size to QCIF\n");
1100 cam->params.roi.width = STV_IMAGE_QCIF_COLS;
1101 cam->params.roi.height = STV_IMAGE_QCIF_ROWS;
1102 cam->width = STV_IMAGE_QCIF_COLS;
1103 cam->height = STV_IMAGE_QCIF_ROWS;
1104 break;
1105 default:
1106 retval = -EINVAL;
1107 }
1108 return retval;
1109}
1110
1111/******************************************************************************
1112 *
1113 * configure_sensor
1114 *
1115 *****************************************************************************/
1116static int configure_sensor(struct camera_data *cam,
1117 int req_width, int req_height)
1118{
1119 int retval;
1120
1121 switch (cam->params.version.sensor_flags) {
1122 case CPIA2_VP_SENSOR_FLAGS_404:
1123 case CPIA2_VP_SENSOR_FLAGS_407:
1124 case CPIA2_VP_SENSOR_FLAGS_409:
1125 case CPIA2_VP_SENSOR_FLAGS_410:
1126 retval = config_sensor_410(cam, req_width, req_height);
1127 break;
1128 case CPIA2_VP_SENSOR_FLAGS_500:
1129 retval = config_sensor_500(cam, req_width, req_height);
1130 break;
1131 default:
1132 return -EINVAL;
1133 }
1134
1135 return retval;
1136}
1137
1138/******************************************************************************
1139 *
1140 * config_sensor_410
1141 *
1142 *****************************************************************************/
1143static int config_sensor_410(struct camera_data *cam,
1144 int req_width, int req_height)
1145{
1146 struct cpia2_command cmd;
1147 int i = 0;
1148 int image_size;
1149 int image_type;
1150 int width = req_width;
1151 int height = req_height;
1152
1153 /***
1154 * Make sure size doesn't exceed CIF.
1155 ***/
1156 if (width > STV_IMAGE_CIF_COLS)
1157 width = STV_IMAGE_CIF_COLS;
1158 if (height > STV_IMAGE_CIF_ROWS)
1159 height = STV_IMAGE_CIF_ROWS;
1160
1161 image_size = cpia2_match_video_size(width, height);
1162
1163 DBG("Config 410: width = %d, height = %d\n", width, height);
1164 DBG("Image size returned is %d\n", image_size);
1165 if (image_size >= 0) {
1166 set_vw_size(cam, image_size);
1167 width = cam->params.roi.width;
1168 height = cam->params.roi.height;
1169
1170 DBG("After set_vw_size(), width = %d, height = %d\n",
1171 width, height);
1172 if (width <= 176 && height <= 144) {
1173 DBG("image type = VIDEOSIZE_QCIF\n");
1174 image_type = VIDEOSIZE_QCIF;
1175 }
1176 else if (width <= 320 && height <= 240) {
1177 DBG("image type = VIDEOSIZE_QVGA\n");
1178 image_type = VIDEOSIZE_QVGA;
1179 }
1180 else {
1181 DBG("image type = VIDEOSIZE_CIF\n");
1182 image_type = VIDEOSIZE_CIF;
1183 }
1184 } else {
1185 ERR("ConfigSensor410 failed\n");
1186 return -EINVAL;
1187 }
1188
1189 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
1190 cmd.direction = TRANSFER_WRITE;
1191
1192 /* VC Format */
1193 cmd.buffer.registers[i].index = CPIA2_VC_VC_FORMAT;
1194 if (image_type == VIDEOSIZE_CIF) {
1195 cmd.buffer.registers[i++].value =
1196 (u8) (CPIA2_VC_VC_FORMAT_UFIRST |
1197 CPIA2_VC_VC_FORMAT_SHORTLINE);
1198 } else {
1199 cmd.buffer.registers[i++].value =
1200 (u8) CPIA2_VC_VC_FORMAT_UFIRST;
1201 }
1202
1203 /* VC Clocks */
1204 cmd.buffer.registers[i].index = CPIA2_VC_VC_CLOCKS;
1205 if (image_type == VIDEOSIZE_QCIF) {
1206 if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
1207 cmd.buffer.registers[i++].value=
1208 (u8)(CPIA2_VC_VC_672_CLOCKS_CIF_DIV_BY_3 |
1209 CPIA2_VC_VC_672_CLOCKS_SCALING |
1210 CPIA2_VC_VC_CLOCKS_LOGDIV2);
1211 DBG("VC_Clocks (0xc4) should be B\n");
1212 }
1213 else {
1214 cmd.buffer.registers[i++].value=
1215 (u8)(CPIA2_VC_VC_676_CLOCKS_CIF_DIV_BY_3 |
1216 CPIA2_VC_VC_CLOCKS_LOGDIV2);
1217 }
1218 } else {
1219 if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
1220 cmd.buffer.registers[i++].value =
1221 (u8) (CPIA2_VC_VC_672_CLOCKS_CIF_DIV_BY_3 |
1222 CPIA2_VC_VC_CLOCKS_LOGDIV0);
1223 }
1224 else {
1225 cmd.buffer.registers[i++].value =
1226 (u8) (CPIA2_VC_VC_676_CLOCKS_CIF_DIV_BY_3 |
1227 CPIA2_VC_VC_676_CLOCKS_SCALING |
1228 CPIA2_VC_VC_CLOCKS_LOGDIV0);
1229 }
1230 }
1231 DBG("VC_Clocks (0xc4) = 0x%0X\n", cmd.buffer.registers[i-1].value);
1232
1233 /* Input reqWidth from VC */
1234 cmd.buffer.registers[i].index = CPIA2_VC_VC_IHSIZE_LO;
1235 if (image_type == VIDEOSIZE_QCIF)
1236 cmd.buffer.registers[i++].value =
1237 (u8) (STV_IMAGE_QCIF_COLS / 4);
1238 else
1239 cmd.buffer.registers[i++].value =
1240 (u8) (STV_IMAGE_CIF_COLS / 4);
1241
1242 /* Timings */
1243 cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_HI;
1244 if (image_type == VIDEOSIZE_QCIF)
1245 cmd.buffer.registers[i++].value = (u8) 0;
1246 else
1247 cmd.buffer.registers[i++].value = (u8) 1;
1248
1249 cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_LO;
1250 if (image_type == VIDEOSIZE_QCIF)
1251 cmd.buffer.registers[i++].value = (u8) 208;
1252 else
1253 cmd.buffer.registers[i++].value = (u8) 160;
1254
1255 cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_HI;
1256 if (image_type == VIDEOSIZE_QCIF)
1257 cmd.buffer.registers[i++].value = (u8) 0;
1258 else
1259 cmd.buffer.registers[i++].value = (u8) 1;
1260
1261 cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_LO;
1262 if (image_type == VIDEOSIZE_QCIF)
1263 cmd.buffer.registers[i++].value = (u8) 160;
1264 else
1265 cmd.buffer.registers[i++].value = (u8) 64;
1266
1267 /* Output Image Size */
1268 cmd.buffer.registers[i].index = CPIA2_VC_VC_OHSIZE;
1269 cmd.buffer.registers[i++].value = cam->params.roi.width / 4;
1270
1271 cmd.buffer.registers[i].index = CPIA2_VC_VC_OVSIZE;
1272 cmd.buffer.registers[i++].value = cam->params.roi.height / 4;
1273
1274 /* Cropping */
1275 cmd.buffer.registers[i].index = CPIA2_VC_VC_HCROP;
1276 if (image_type == VIDEOSIZE_QCIF)
1277 cmd.buffer.registers[i++].value =
1278 (u8) (((STV_IMAGE_QCIF_COLS / 4) - (width / 4)) / 2);
1279 else
1280 cmd.buffer.registers[i++].value =
1281 (u8) (((STV_IMAGE_CIF_COLS / 4) - (width / 4)) / 2);
1282
1283 cmd.buffer.registers[i].index = CPIA2_VC_VC_VCROP;
1284 if (image_type == VIDEOSIZE_QCIF)
1285 cmd.buffer.registers[i++].value =
1286 (u8) (((STV_IMAGE_QCIF_ROWS / 4) - (height / 4)) / 2);
1287 else
1288 cmd.buffer.registers[i++].value =
1289 (u8) (((STV_IMAGE_CIF_ROWS / 4) - (height / 4)) / 2);
1290
1291 /* Scaling registers (defaults) */
1292 cmd.buffer.registers[i].index = CPIA2_VC_VC_HPHASE;
1293 cmd.buffer.registers[i++].value = (u8) 0;
1294
1295 cmd.buffer.registers[i].index = CPIA2_VC_VC_VPHASE;
1296 cmd.buffer.registers[i++].value = (u8) 0;
1297
1298 cmd.buffer.registers[i].index = CPIA2_VC_VC_HISPAN;
1299 cmd.buffer.registers[i++].value = (u8) 31;
1300
1301 cmd.buffer.registers[i].index = CPIA2_VC_VC_VISPAN;
1302 cmd.buffer.registers[i++].value = (u8) 31;
1303
1304 cmd.buffer.registers[i].index = CPIA2_VC_VC_HICROP;
1305 cmd.buffer.registers[i++].value = (u8) 0;
1306
1307 cmd.buffer.registers[i].index = CPIA2_VC_VC_VICROP;
1308 cmd.buffer.registers[i++].value = (u8) 0;
1309
1310 cmd.buffer.registers[i].index = CPIA2_VC_VC_HFRACT;
1311 cmd.buffer.registers[i++].value = (u8) 0x81; /* = 8/1 = 8 (HIBYTE/LOBYTE) */
1312
1313 cmd.buffer.registers[i].index = CPIA2_VC_VC_VFRACT;
1314 cmd.buffer.registers[i++].value = (u8) 0x81; /* = 8/1 = 8 (HIBYTE/LOBYTE) */
1315
1316 cmd.reg_count = i;
1317
1318 cpia2_send_command(cam, &cmd);
1319
1320 return i;
1321}
1322
1323
1324/******************************************************************************
1325 *
1326 * config_sensor_500(cam)
1327 *
1328 *****************************************************************************/
1329static int config_sensor_500(struct camera_data *cam,
1330 int req_width, int req_height)
1331{
1332 struct cpia2_command cmd;
1333 int i = 0;
1334 int image_size = VIDEOSIZE_CIF;
1335 int image_type = VIDEOSIZE_VGA;
1336 int width = req_width;
1337 int height = req_height;
1338 unsigned int device = cam->params.pnp_id.device_type;
1339
1340 image_size = cpia2_match_video_size(width, height);
1341
1342 if (width > STV_IMAGE_CIF_COLS || height > STV_IMAGE_CIF_ROWS)
1343 image_type = VIDEOSIZE_VGA;
1344 else if (width > STV_IMAGE_QVGA_COLS || height > STV_IMAGE_QVGA_ROWS)
1345 image_type = VIDEOSIZE_CIF;
1346 else if (width > STV_IMAGE_QCIF_COLS || height > STV_IMAGE_QCIF_ROWS)
1347 image_type = VIDEOSIZE_QVGA;
1348 else
1349 image_type = VIDEOSIZE_QCIF;
1350
1351 if (image_size >= 0) {
1352 set_vw_size(cam, image_size);
1353 width = cam->params.roi.width;
1354 height = cam->params.roi.height;
1355 } else {
1356 ERR("ConfigSensor500 failed\n");
1357 return -EINVAL;
1358 }
1359
1360 DBG("image_size = %d, width = %d, height = %d, type = %d\n",
1361 image_size, width, height, image_type);
1362
1363 cmd.req_mode = CAMERAACCESS_TYPE_RANDOM | CAMERAACCESS_VC;
1364 cmd.direction = TRANSFER_WRITE;
1365 i = 0;
1366
1367 /* VC Format */
1368 cmd.buffer.registers[i].index = CPIA2_VC_VC_FORMAT;
1369 cmd.buffer.registers[i].value = (u8) CPIA2_VC_VC_FORMAT_UFIRST;
1370 if (image_type == VIDEOSIZE_QCIF)
1371 cmd.buffer.registers[i].value |= (u8) CPIA2_VC_VC_FORMAT_DECIMATING;
1372 i++;
1373
1374 /* VC Clocks */
1375 cmd.buffer.registers[i].index = CPIA2_VC_VC_CLOCKS;
1376 if (device == DEVICE_STV_672) {
1377 if (image_type == VIDEOSIZE_VGA)
1378 cmd.buffer.registers[i].value =
1379 (u8)CPIA2_VC_VC_CLOCKS_LOGDIV1;
1380 else
1381 cmd.buffer.registers[i].value =
1382 (u8)(CPIA2_VC_VC_672_CLOCKS_SCALING |
1383 CPIA2_VC_VC_CLOCKS_LOGDIV3);
1384 } else {
1385 if (image_type == VIDEOSIZE_VGA)
1386 cmd.buffer.registers[i].value =
1387 (u8)CPIA2_VC_VC_CLOCKS_LOGDIV0;
1388 else
1389 cmd.buffer.registers[i].value =
1390 (u8)(CPIA2_VC_VC_676_CLOCKS_SCALING |
1391 CPIA2_VC_VC_CLOCKS_LOGDIV2);
1392 }
1393 i++;
1394
1395 DBG("VC_CLOCKS = 0x%X\n", cmd.buffer.registers[i-1].value);
1396
1397 /* Input width from VP */
1398 cmd.buffer.registers[i].index = CPIA2_VC_VC_IHSIZE_LO;
1399 if (image_type == VIDEOSIZE_VGA)
1400 cmd.buffer.registers[i].value =
1401 (u8) (STV_IMAGE_VGA_COLS / 4);
1402 else
1403 cmd.buffer.registers[i].value =
1404 (u8) (STV_IMAGE_QVGA_COLS / 4);
1405 i++;
1406 DBG("Input width = %d\n", cmd.buffer.registers[i-1].value);
1407
1408 /* Timings */
1409 cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_HI;
1410 if (image_type == VIDEOSIZE_VGA)
1411 cmd.buffer.registers[i++].value = (u8) 2;
1412 else
1413 cmd.buffer.registers[i++].value = (u8) 1;
1414
1415 cmd.buffer.registers[i].index = CPIA2_VC_VC_XLIM_LO;
1416 if (image_type == VIDEOSIZE_VGA)
1417 cmd.buffer.registers[i++].value = (u8) 250;
1418 else if (image_type == VIDEOSIZE_QVGA)
1419 cmd.buffer.registers[i++].value = (u8) 125;
1420 else
1421 cmd.buffer.registers[i++].value = (u8) 160;
1422
1423 cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_HI;
1424 if (image_type == VIDEOSIZE_VGA)
1425 cmd.buffer.registers[i++].value = (u8) 2;
1426 else
1427 cmd.buffer.registers[i++].value = (u8) 1;
1428
1429 cmd.buffer.registers[i].index = CPIA2_VC_VC_YLIM_LO;
1430 if (image_type == VIDEOSIZE_VGA)
1431 cmd.buffer.registers[i++].value = (u8) 12;
1432 else if (image_type == VIDEOSIZE_QVGA)
1433 cmd.buffer.registers[i++].value = (u8) 64;
1434 else
1435 cmd.buffer.registers[i++].value = (u8) 6;
1436
1437 /* Output Image Size */
1438 cmd.buffer.registers[i].index = CPIA2_VC_VC_OHSIZE;
1439 if (image_type == VIDEOSIZE_QCIF)
1440 cmd.buffer.registers[i++].value = STV_IMAGE_CIF_COLS / 4;
1441 else
1442 cmd.buffer.registers[i++].value = width / 4;
1443
1444 cmd.buffer.registers[i].index = CPIA2_VC_VC_OVSIZE;
1445 if (image_type == VIDEOSIZE_QCIF)
1446 cmd.buffer.registers[i++].value = STV_IMAGE_CIF_ROWS / 4;
1447 else
1448 cmd.buffer.registers[i++].value = height / 4;
1449
1450 /* Cropping */
1451 cmd.buffer.registers[i].index = CPIA2_VC_VC_HCROP;
1452 if (image_type == VIDEOSIZE_VGA)
1453 cmd.buffer.registers[i++].value =
1454 (u8) (((STV_IMAGE_VGA_COLS / 4) - (width / 4)) / 2);
1455 else if (image_type == VIDEOSIZE_QVGA)
1456 cmd.buffer.registers[i++].value =
1457 (u8) (((STV_IMAGE_QVGA_COLS / 4) - (width / 4)) / 2);
1458 else if (image_type == VIDEOSIZE_CIF)
1459 cmd.buffer.registers[i++].value =
1460 (u8) (((STV_IMAGE_CIF_COLS / 4) - (width / 4)) / 2);
1461 else /*if (image_type == VIDEOSIZE_QCIF)*/
1462 cmd.buffer.registers[i++].value =
1463 (u8) (((STV_IMAGE_QCIF_COLS / 4) - (width / 4)) / 2);
1464
1465 cmd.buffer.registers[i].index = CPIA2_VC_VC_VCROP;
1466 if (image_type == VIDEOSIZE_VGA)
1467 cmd.buffer.registers[i++].value =
1468 (u8) (((STV_IMAGE_VGA_ROWS / 4) - (height / 4)) / 2);
1469 else if (image_type == VIDEOSIZE_QVGA)
1470 cmd.buffer.registers[i++].value =
1471 (u8) (((STV_IMAGE_QVGA_ROWS / 4) - (height / 4)) / 2);
1472 else if (image_type == VIDEOSIZE_CIF)
1473 cmd.buffer.registers[i++].value =
1474 (u8) (((STV_IMAGE_CIF_ROWS / 4) - (height / 4)) / 2);
1475 else /*if (image_type == VIDEOSIZE_QCIF)*/
1476 cmd.buffer.registers[i++].value =
1477 (u8) (((STV_IMAGE_QCIF_ROWS / 4) - (height / 4)) / 2);
1478
1479 /* Scaling registers (defaults) */
1480 cmd.buffer.registers[i].index = CPIA2_VC_VC_HPHASE;
1481 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1482 cmd.buffer.registers[i++].value = (u8) 36;
1483 else
1484 cmd.buffer.registers[i++].value = (u8) 0;
1485
1486 cmd.buffer.registers[i].index = CPIA2_VC_VC_VPHASE;
1487 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1488 cmd.buffer.registers[i++].value = (u8) 32;
1489 else
1490 cmd.buffer.registers[i++].value = (u8) 0;
1491
1492 cmd.buffer.registers[i].index = CPIA2_VC_VC_HISPAN;
1493 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1494 cmd.buffer.registers[i++].value = (u8) 26;
1495 else
1496 cmd.buffer.registers[i++].value = (u8) 31;
1497
1498 cmd.buffer.registers[i].index = CPIA2_VC_VC_VISPAN;
1499 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1500 cmd.buffer.registers[i++].value = (u8) 21;
1501 else
1502 cmd.buffer.registers[i++].value = (u8) 31;
1503
1504 cmd.buffer.registers[i].index = CPIA2_VC_VC_HICROP;
1505 cmd.buffer.registers[i++].value = (u8) 0;
1506
1507 cmd.buffer.registers[i].index = CPIA2_VC_VC_VICROP;
1508 cmd.buffer.registers[i++].value = (u8) 0;
1509
1510 cmd.buffer.registers[i].index = CPIA2_VC_VC_HFRACT;
1511 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1512 cmd.buffer.registers[i++].value = (u8) 0x2B; /* 2/11 */
1513 else
1514 cmd.buffer.registers[i++].value = (u8) 0x81; /* 8/1 */
1515
1516 cmd.buffer.registers[i].index = CPIA2_VC_VC_VFRACT;
1517 if (image_type == VIDEOSIZE_CIF || image_type == VIDEOSIZE_QCIF)
1518 cmd.buffer.registers[i++].value = (u8) 0x13; /* 1/3 */
1519 else
1520 cmd.buffer.registers[i++].value = (u8) 0x81; /* 8/1 */
1521
1522 cmd.reg_count = i;
1523
1524 cpia2_send_command(cam, &cmd);
1525
1526 return i;
1527}
1528
1529
1530/******************************************************************************
1531 *
1532 * setallproperties
1533 *
1534 * This sets all user changeable properties to the values in cam->params.
1535 *****************************************************************************/
1536static int set_all_properties(struct camera_data *cam)
1537{
1538 /**
1539 * Don't set target_kb here, it will be set later.
1540 * framerate and user_mode were already set (set_default_user_mode).
1541 **/
1542
1543 cpia2_usb_change_streaming_alternate(cam,
1544 cam->params.camera_state.stream_mode);
1545
1546 cpia2_do_command(cam,
1547 CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
1548 TRANSFER_WRITE, cam->params.vp_params.gpio_direction);
1549 cpia2_do_command(cam, CPIA2_CMD_SET_VC_MP_GPIO_DATA, TRANSFER_WRITE,
1550 cam->params.vp_params.gpio_data);
1551
1552 v4l2_ctrl_handler_setup(&cam->hdl);
1553
1554 wake_system(cam);
1555
1556 set_lowlight_boost(cam);
1557
1558 return 0;
1559}
1560
1561/******************************************************************************
1562 *
1563 * cpia2_save_camera_state
1564 *
1565 *****************************************************************************/
1566void cpia2_save_camera_state(struct camera_data *cam)
1567{
1568 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, TRANSFER_READ, 0);
1569 cpia2_do_command(cam, CPIA2_CMD_GET_VC_MP_GPIO_DIRECTION, TRANSFER_READ,
1570 0);
1571 cpia2_do_command(cam, CPIA2_CMD_GET_VC_MP_GPIO_DATA, TRANSFER_READ, 0);
1572 /* Don't get framerate or target_kb. Trust the values we already have */
1573}
1574
1575
1576/******************************************************************************
1577 *
1578 * cpia2_set_flicker_mode
1579 *
1580 *****************************************************************************/
1581int cpia2_set_flicker_mode(struct camera_data *cam, int mode)
1582{
1583 unsigned char cam_reg;
1584 int err = 0;
1585
1586 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
1587 return -EINVAL;
1588
1589 /* Set the appropriate bits in FLICKER_MODES, preserving the rest */
1590 if((err = cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
1591 TRANSFER_READ, 0)))
1592 return err;
1593 cam_reg = cam->params.flicker_control.cam_register;
1594
1595 switch(mode) {
1596 case NEVER_FLICKER:
1597 cam_reg |= CPIA2_VP_FLICKER_MODES_NEVER_FLICKER;
1598 cam_reg &= ~CPIA2_VP_FLICKER_MODES_50HZ;
1599 break;
1600 case FLICKER_60:
1601 cam_reg &= ~CPIA2_VP_FLICKER_MODES_NEVER_FLICKER;
1602 cam_reg &= ~CPIA2_VP_FLICKER_MODES_50HZ;
1603 break;
1604 case FLICKER_50:
1605 cam_reg &= ~CPIA2_VP_FLICKER_MODES_NEVER_FLICKER;
1606 cam_reg |= CPIA2_VP_FLICKER_MODES_50HZ;
1607 break;
1608 default:
1609 return -EINVAL;
1610 }
1611
1612 if((err = cpia2_do_command(cam, CPIA2_CMD_SET_FLICKER_MODES,
1613 TRANSFER_WRITE, cam_reg)))
1614 return err;
1615
1616 /* Set the appropriate bits in EXP_MODES, preserving the rest */
1617 if((err = cpia2_do_command(cam, CPIA2_CMD_GET_VP_EXP_MODES,
1618 TRANSFER_READ, 0)))
1619 return err;
1620 cam_reg = cam->params.vp_params.exposure_modes;
1621
1622 if (mode == NEVER_FLICKER) {
1623 cam_reg |= CPIA2_VP_EXPOSURE_MODES_INHIBIT_FLICKER;
1624 } else {
1625 cam_reg &= ~CPIA2_VP_EXPOSURE_MODES_INHIBIT_FLICKER;
1626 }
1627
1628 if((err = cpia2_do_command(cam, CPIA2_CMD_SET_VP_EXP_MODES,
1629 TRANSFER_WRITE, cam_reg)))
1630 return err;
1631
1632 if((err = cpia2_do_command(cam, CPIA2_CMD_REHASH_VP4,
1633 TRANSFER_WRITE, 1)))
1634 return err;
1635
1636 switch(mode) {
1637 case NEVER_FLICKER:
1638 case FLICKER_60:
1639 case FLICKER_50:
1640 cam->params.flicker_control.flicker_mode_req = mode;
1641 break;
1642 default:
1643 err = -EINVAL;
1644 }
1645
1646 return err;
1647}
1648
1649/******************************************************************************
1650 *
1651 * cpia2_set_property_flip
1652 *
1653 *****************************************************************************/
1654void cpia2_set_property_flip(struct camera_data *cam, int prop_val)
1655{
1656 unsigned char cam_reg;
1657
1658 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, TRANSFER_READ, 0);
1659 cam_reg = cam->params.vp_params.user_effects;
1660
1661 if (prop_val)
1662 {
1663 cam_reg |= CPIA2_VP_USER_EFFECTS_FLIP;
1664 }
1665 else
1666 {
1667 cam_reg &= ~CPIA2_VP_USER_EFFECTS_FLIP;
1668 }
1669 cam->params.vp_params.user_effects = cam_reg;
1670 cpia2_do_command(cam, CPIA2_CMD_SET_USER_EFFECTS, TRANSFER_WRITE,
1671 cam_reg);
1672}
1673
1674/******************************************************************************
1675 *
1676 * cpia2_set_property_mirror
1677 *
1678 *****************************************************************************/
1679void cpia2_set_property_mirror(struct camera_data *cam, int prop_val)
1680{
1681 unsigned char cam_reg;
1682
1683 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, TRANSFER_READ, 0);
1684 cam_reg = cam->params.vp_params.user_effects;
1685
1686 if (prop_val)
1687 {
1688 cam_reg |= CPIA2_VP_USER_EFFECTS_MIRROR;
1689 }
1690 else
1691 {
1692 cam_reg &= ~CPIA2_VP_USER_EFFECTS_MIRROR;
1693 }
1694 cam->params.vp_params.user_effects = cam_reg;
1695 cpia2_do_command(cam, CPIA2_CMD_SET_USER_EFFECTS, TRANSFER_WRITE,
1696 cam_reg);
1697}
1698
1699/******************************************************************************
1700 *
1701 * cpia2_set_gpio
1702 *
1703 *****************************************************************************/
1704int cpia2_set_gpio(struct camera_data *cam, unsigned char setting)
1705{
1706 int ret;
1707
1708 /* Set the microport direction (register 0x90, should be defined
1709 * already) to 1 (user output), and set the microport data (0x91) to
1710 * the value in the ioctl argument.
1711 */
1712
1713 ret = cpia2_do_command(cam,
1714 CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
1715 CPIA2_VC_MP_DIR_OUTPUT,
1716 255);
1717 if (ret < 0)
1718 return ret;
1719 cam->params.vp_params.gpio_direction = 255;
1720
1721 ret = cpia2_do_command(cam,
1722 CPIA2_CMD_SET_VC_MP_GPIO_DATA,
1723 CPIA2_VC_MP_DIR_OUTPUT,
1724 setting);
1725 if (ret < 0)
1726 return ret;
1727 cam->params.vp_params.gpio_data = setting;
1728
1729 return 0;
1730}
1731
1732/******************************************************************************
1733 *
1734 * cpia2_set_fps
1735 *
1736 *****************************************************************************/
1737int cpia2_set_fps(struct camera_data *cam, int framerate)
1738{
1739 int retval;
1740
1741 switch(framerate) {
1742 case CPIA2_VP_FRAMERATE_30:
1743 case CPIA2_VP_FRAMERATE_25:
1744 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1745 cam->params.version.sensor_flags ==
1746 CPIA2_VP_SENSOR_FLAGS_500) {
1747 return -EINVAL;
1748 }
1749 /* Fall through */
1750 case CPIA2_VP_FRAMERATE_15:
1751 case CPIA2_VP_FRAMERATE_12_5:
1752 case CPIA2_VP_FRAMERATE_7_5:
1753 case CPIA2_VP_FRAMERATE_6_25:
1754 break;
1755 default:
1756 return -EINVAL;
1757 }
1758
1759 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1760 framerate == CPIA2_VP_FRAMERATE_15)
1761 framerate = 0; /* Work around bug in VP4 */
1762
1763 retval = cpia2_do_command(cam,
1764 CPIA2_CMD_FRAMERATE_REQ,
1765 TRANSFER_WRITE,
1766 framerate);
1767
1768 if(retval == 0)
1769 cam->params.vp_params.frame_rate = framerate;
1770
1771 return retval;
1772}
1773
1774/******************************************************************************
1775 *
1776 * cpia2_set_brightness
1777 *
1778 *****************************************************************************/
1779void cpia2_set_brightness(struct camera_data *cam, unsigned char value)
1780{
1781 /***
1782 * Don't let the register be set to zero - bug in VP4 - flash of full
1783 * brightness
1784 ***/
1785 if (cam->params.pnp_id.device_type == DEVICE_STV_672 && value == 0)
1786 value++;
1787 DBG("Setting brightness to %d (0x%0x)\n", value, value);
1788 cpia2_do_command(cam, CPIA2_CMD_SET_VP_BRIGHTNESS, TRANSFER_WRITE, value);
1789}
1790
1791/******************************************************************************
1792 *
1793 * cpia2_set_contrast
1794 *
1795 *****************************************************************************/
1796void cpia2_set_contrast(struct camera_data *cam, unsigned char value)
1797{
1798 DBG("Setting contrast to %d (0x%0x)\n", value, value);
1799 cpia2_do_command(cam, CPIA2_CMD_SET_CONTRAST, TRANSFER_WRITE, value);
1800}
1801
1802/******************************************************************************
1803 *
1804 * cpia2_set_saturation
1805 *
1806 *****************************************************************************/
1807void cpia2_set_saturation(struct camera_data *cam, unsigned char value)
1808{
1809 DBG("Setting saturation to %d (0x%0x)\n", value, value);
1810 cpia2_do_command(cam,CPIA2_CMD_SET_VP_SATURATION, TRANSFER_WRITE,value);
1811}
1812
1813/******************************************************************************
1814 *
1815 * wake_system
1816 *
1817 *****************************************************************************/
1818static void wake_system(struct camera_data *cam)
1819{
1820 cpia2_do_command(cam, CPIA2_CMD_SET_WAKEUP, TRANSFER_WRITE, 0);
1821}
1822
1823/******************************************************************************
1824 *
1825 * set_lowlight_boost
1826 *
1827 * Valid for STV500 sensor only
1828 *****************************************************************************/
1829static void set_lowlight_boost(struct camera_data *cam)
1830{
1831 struct cpia2_command cmd;
1832
1833 if (cam->params.pnp_id.device_type != DEVICE_STV_672 ||
1834 cam->params.version.sensor_flags != CPIA2_VP_SENSOR_FLAGS_500)
1835 return;
1836
1837 cmd.direction = TRANSFER_WRITE;
1838 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
1839 cmd.reg_count = 3;
1840 cmd.start = CPIA2_VP_RAM_ADDR_H;
1841
1842 cmd.buffer.block_data[0] = 0; /* High byte of address to write to */
1843 cmd.buffer.block_data[1] = 0x59; /* Low byte of address to write to */
1844 cmd.buffer.block_data[2] = 0; /* High byte of data to write */
1845
1846 cpia2_send_command(cam, &cmd);
1847
1848 if (cam->params.vp_params.lowlight_boost) {
1849 cmd.buffer.block_data[0] = 0x02; /* Low byte data to write */
1850 } else {
1851 cmd.buffer.block_data[0] = 0x06;
1852 }
1853 cmd.start = CPIA2_VP_RAM_DATA;
1854 cmd.reg_count = 1;
1855 cpia2_send_command(cam, &cmd);
1856
1857 /* Rehash the VP4 values */
1858 cpia2_do_command(cam, CPIA2_CMD_REHASH_VP4, TRANSFER_WRITE, 1);
1859}
1860
1861/******************************************************************************
1862 *
1863 * cpia2_set_format
1864 *
1865 * Assumes that new size is already set in param struct.
1866 *****************************************************************************/
1867void cpia2_set_format(struct camera_data *cam)
1868{
1869 cam->flush = true;
1870
1871 cpia2_usb_stream_pause(cam);
1872
1873 /* reset camera to new size */
1874 cpia2_set_low_power(cam);
1875 cpia2_reset_camera(cam);
1876 cam->flush = false;
1877
1878 cpia2_dbg_dump_registers(cam);
1879
1880 cpia2_usb_stream_resume(cam);
1881}
1882
1883/******************************************************************************
1884 *
1885 * cpia2_dbg_dump_registers
1886 *
1887 *****************************************************************************/
1888void cpia2_dbg_dump_registers(struct camera_data *cam)
1889{
1890#ifdef _CPIA2_DEBUG_
1891 struct cpia2_command cmd;
1892
1893 if (!(debugs_on & DEBUG_DUMP_REGS))
1894 return;
1895
1896 cmd.direction = TRANSFER_READ;
1897
1898 /* Start with bank 0 (SYSTEM) */
1899 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_SYSTEM;
1900 cmd.reg_count = 3;
1901 cmd.start = 0;
1902 cpia2_send_command(cam, &cmd);
1903 printk(KERN_DEBUG "System Device Hi = 0x%X\n",
1904 cmd.buffer.block_data[0]);
1905 printk(KERN_DEBUG "System Device Lo = 0x%X\n",
1906 cmd.buffer.block_data[1]);
1907 printk(KERN_DEBUG "System_system control = 0x%X\n",
1908 cmd.buffer.block_data[2]);
1909
1910 /* Bank 1 (VC) */
1911 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
1912 cmd.reg_count = 4;
1913 cmd.start = 0x80;
1914 cpia2_send_command(cam, &cmd);
1915 printk(KERN_DEBUG "ASIC_ID = 0x%X\n",
1916 cmd.buffer.block_data[0]);
1917 printk(KERN_DEBUG "ASIC_REV = 0x%X\n",
1918 cmd.buffer.block_data[1]);
1919 printk(KERN_DEBUG "PW_CONTRL = 0x%X\n",
1920 cmd.buffer.block_data[2]);
1921 printk(KERN_DEBUG "WAKEUP = 0x%X\n",
1922 cmd.buffer.block_data[3]);
1923
1924 cmd.start = 0xA0; /* ST_CTRL */
1925 cmd.reg_count = 1;
1926 cpia2_send_command(cam, &cmd);
1927 printk(KERN_DEBUG "Stream ctrl = 0x%X\n",
1928 cmd.buffer.block_data[0]);
1929
1930 cmd.start = 0xA4; /* Stream status */
1931 cpia2_send_command(cam, &cmd);
1932 printk(KERN_DEBUG "Stream status = 0x%X\n",
1933 cmd.buffer.block_data[0]);
1934
1935 cmd.start = 0xA8; /* USB status */
1936 cmd.reg_count = 3;
1937 cpia2_send_command(cam, &cmd);
1938 printk(KERN_DEBUG "USB_CTRL = 0x%X\n",
1939 cmd.buffer.block_data[0]);
1940 printk(KERN_DEBUG "USB_STRM = 0x%X\n",
1941 cmd.buffer.block_data[1]);
1942 printk(KERN_DEBUG "USB_STATUS = 0x%X\n",
1943 cmd.buffer.block_data[2]);
1944
1945 cmd.start = 0xAF; /* USB settings */
1946 cmd.reg_count = 1;
1947 cpia2_send_command(cam, &cmd);
1948 printk(KERN_DEBUG "USB settings = 0x%X\n",
1949 cmd.buffer.block_data[0]);
1950
1951 cmd.start = 0xC0; /* VC stuff */
1952 cmd.reg_count = 26;
1953 cpia2_send_command(cam, &cmd);
1954 printk(KERN_DEBUG "VC Control = 0x%0X\n",
1955 cmd.buffer.block_data[0]);
1956 printk(KERN_DEBUG "VC Format = 0x%0X\n",
1957 cmd.buffer.block_data[3]);
1958 printk(KERN_DEBUG "VC Clocks = 0x%0X\n",
1959 cmd.buffer.block_data[4]);
1960 printk(KERN_DEBUG "VC IHSize = 0x%0X\n",
1961 cmd.buffer.block_data[5]);
1962 printk(KERN_DEBUG "VC Xlim Hi = 0x%0X\n",
1963 cmd.buffer.block_data[6]);
1964 printk(KERN_DEBUG "VC XLim Lo = 0x%0X\n",
1965 cmd.buffer.block_data[7]);
1966 printk(KERN_DEBUG "VC YLim Hi = 0x%0X\n",
1967 cmd.buffer.block_data[8]);
1968 printk(KERN_DEBUG "VC YLim Lo = 0x%0X\n",
1969 cmd.buffer.block_data[9]);
1970 printk(KERN_DEBUG "VC OHSize = 0x%0X\n",
1971 cmd.buffer.block_data[10]);
1972 printk(KERN_DEBUG "VC OVSize = 0x%0X\n",
1973 cmd.buffer.block_data[11]);
1974 printk(KERN_DEBUG "VC HCrop = 0x%0X\n",
1975 cmd.buffer.block_data[12]);
1976 printk(KERN_DEBUG "VC VCrop = 0x%0X\n",
1977 cmd.buffer.block_data[13]);
1978 printk(KERN_DEBUG "VC HPhase = 0x%0X\n",
1979 cmd.buffer.block_data[14]);
1980 printk(KERN_DEBUG "VC VPhase = 0x%0X\n",
1981 cmd.buffer.block_data[15]);
1982 printk(KERN_DEBUG "VC HIspan = 0x%0X\n",
1983 cmd.buffer.block_data[16]);
1984 printk(KERN_DEBUG "VC VIspan = 0x%0X\n",
1985 cmd.buffer.block_data[17]);
1986 printk(KERN_DEBUG "VC HiCrop = 0x%0X\n",
1987 cmd.buffer.block_data[18]);
1988 printk(KERN_DEBUG "VC ViCrop = 0x%0X\n",
1989 cmd.buffer.block_data[19]);
1990 printk(KERN_DEBUG "VC HiFract = 0x%0X\n",
1991 cmd.buffer.block_data[20]);
1992 printk(KERN_DEBUG "VC ViFract = 0x%0X\n",
1993 cmd.buffer.block_data[21]);
1994 printk(KERN_DEBUG "VC JPeg Opt = 0x%0X\n",
1995 cmd.buffer.block_data[22]);
1996 printk(KERN_DEBUG "VC Creep Per = 0x%0X\n",
1997 cmd.buffer.block_data[23]);
1998 printk(KERN_DEBUG "VC User Sq. = 0x%0X\n",
1999 cmd.buffer.block_data[24]);
2000 printk(KERN_DEBUG "VC Target KB = 0x%0X\n",
2001 cmd.buffer.block_data[25]);
2002
2003 /*** VP ***/
2004 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VP;
2005 cmd.reg_count = 14;
2006 cmd.start = 0;
2007 cpia2_send_command(cam, &cmd);
2008
2009 printk(KERN_DEBUG "VP Dev Hi = 0x%0X\n",
2010 cmd.buffer.block_data[0]);
2011 printk(KERN_DEBUG "VP Dev Lo = 0x%0X\n",
2012 cmd.buffer.block_data[1]);
2013 printk(KERN_DEBUG "VP Sys State = 0x%0X\n",
2014 cmd.buffer.block_data[2]);
2015 printk(KERN_DEBUG "VP Sys Ctrl = 0x%0X\n",
2016 cmd.buffer.block_data[3]);
2017 printk(KERN_DEBUG "VP Sensor flg = 0x%0X\n",
2018 cmd.buffer.block_data[5]);
2019 printk(KERN_DEBUG "VP Sensor Rev = 0x%0X\n",
2020 cmd.buffer.block_data[6]);
2021 printk(KERN_DEBUG "VP Dev Config = 0x%0X\n",
2022 cmd.buffer.block_data[7]);
2023 printk(KERN_DEBUG "VP GPIO_DIR = 0x%0X\n",
2024 cmd.buffer.block_data[8]);
2025 printk(KERN_DEBUG "VP GPIO_DATA = 0x%0X\n",
2026 cmd.buffer.block_data[9]);
2027 printk(KERN_DEBUG "VP Ram ADDR H = 0x%0X\n",
2028 cmd.buffer.block_data[10]);
2029 printk(KERN_DEBUG "VP Ram ADDR L = 0x%0X\n",
2030 cmd.buffer.block_data[11]);
2031 printk(KERN_DEBUG "VP RAM Data = 0x%0X\n",
2032 cmd.buffer.block_data[12]);
2033 printk(KERN_DEBUG "Do Call = 0x%0X\n",
2034 cmd.buffer.block_data[13]);
2035
2036 if (cam->params.pnp_id.device_type == DEVICE_STV_672) {
2037 cmd.reg_count = 9;
2038 cmd.start = 0x0E;
2039 cpia2_send_command(cam, &cmd);
2040 printk(KERN_DEBUG "VP Clock Ctrl = 0x%0X\n",
2041 cmd.buffer.block_data[0]);
2042 printk(KERN_DEBUG "VP Patch Rev = 0x%0X\n",
2043 cmd.buffer.block_data[1]);
2044 printk(KERN_DEBUG "VP Vid Mode = 0x%0X\n",
2045 cmd.buffer.block_data[2]);
2046 printk(KERN_DEBUG "VP Framerate = 0x%0X\n",
2047 cmd.buffer.block_data[3]);
2048 printk(KERN_DEBUG "VP UserEffect = 0x%0X\n",
2049 cmd.buffer.block_data[4]);
2050 printk(KERN_DEBUG "VP White Bal = 0x%0X\n",
2051 cmd.buffer.block_data[5]);
2052 printk(KERN_DEBUG "VP WB thresh = 0x%0X\n",
2053 cmd.buffer.block_data[6]);
2054 printk(KERN_DEBUG "VP Exp Modes = 0x%0X\n",
2055 cmd.buffer.block_data[7]);
2056 printk(KERN_DEBUG "VP Exp Target = 0x%0X\n",
2057 cmd.buffer.block_data[8]);
2058
2059 cmd.reg_count = 1;
2060 cmd.start = 0x1B;
2061 cpia2_send_command(cam, &cmd);
2062 printk(KERN_DEBUG "VP FlickerMds = 0x%0X\n",
2063 cmd.buffer.block_data[0]);
2064 } else {
2065 cmd.reg_count = 8 ;
2066 cmd.start = 0x0E;
2067 cpia2_send_command(cam, &cmd);
2068 printk(KERN_DEBUG "VP Clock Ctrl = 0x%0X\n",
2069 cmd.buffer.block_data[0]);
2070 printk(KERN_DEBUG "VP Patch Rev = 0x%0X\n",
2071 cmd.buffer.block_data[1]);
2072 printk(KERN_DEBUG "VP Vid Mode = 0x%0X\n",
2073 cmd.buffer.block_data[5]);
2074 printk(KERN_DEBUG "VP Framerate = 0x%0X\n",
2075 cmd.buffer.block_data[6]);
2076 printk(KERN_DEBUG "VP UserEffect = 0x%0X\n",
2077 cmd.buffer.block_data[7]);
2078
2079 cmd.reg_count = 1;
2080 cmd.start = CPIA2_VP5_EXPOSURE_TARGET;
2081 cpia2_send_command(cam, &cmd);
2082 printk(KERN_DEBUG "VP5 Exp Target= 0x%0X\n",
2083 cmd.buffer.block_data[0]);
2084
2085 cmd.reg_count = 4;
2086 cmd.start = 0x3A;
2087 cpia2_send_command(cam, &cmd);
2088 printk(KERN_DEBUG "VP5 MY Black = 0x%0X\n",
2089 cmd.buffer.block_data[0]);
2090 printk(KERN_DEBUG "VP5 MCY Range = 0x%0X\n",
2091 cmd.buffer.block_data[1]);
2092 printk(KERN_DEBUG "VP5 MYCEILING = 0x%0X\n",
2093 cmd.buffer.block_data[2]);
2094 printk(KERN_DEBUG "VP5 MCUV Sat = 0x%0X\n",
2095 cmd.buffer.block_data[3]);
2096 }
2097#endif
2098}
2099
2100/******************************************************************************
2101 *
2102 * reset_camera_struct
2103 *
2104 * Sets all values to the defaults
2105 *****************************************************************************/
2106static void reset_camera_struct(struct camera_data *cam)
2107{
2108 /***
2109 * The following parameter values are the defaults from the register map.
2110 ***/
2111 cam->params.vp_params.lowlight_boost = 0;
2112
2113 /* FlickerModes */
2114 cam->params.flicker_control.flicker_mode_req = NEVER_FLICKER;
2115
2116 /* jpeg params */
2117 cam->params.compression.jpeg_options = CPIA2_VC_VC_JPEG_OPT_DEFAULT;
2118 cam->params.compression.creep_period = 2;
2119 cam->params.compression.user_squeeze = 20;
2120 cam->params.compression.inhibit_htables = false;
2121
2122 /* gpio params */
2123 cam->params.vp_params.gpio_direction = 0; /* write, the default safe mode */
2124 cam->params.vp_params.gpio_data = 0;
2125
2126 /* Target kb params */
2127 cam->params.vc_params.quality = 100;
2128
2129 /***
2130 * Set Sensor FPS as fast as possible.
2131 ***/
2132 if(cam->params.pnp_id.device_type == DEVICE_STV_672) {
2133 if(cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
2134 cam->params.vp_params.frame_rate = CPIA2_VP_FRAMERATE_15;
2135 else
2136 cam->params.vp_params.frame_rate = CPIA2_VP_FRAMERATE_30;
2137 } else {
2138 cam->params.vp_params.frame_rate = CPIA2_VP_FRAMERATE_30;
2139 }
2140
2141 /***
2142 * Set default video mode as large as possible :
2143 * for vga sensor set to vga, for cif sensor set to CIF.
2144 ***/
2145 if (cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500) {
2146 cam->sensor_type = CPIA2_SENSOR_500;
2147 cam->video_size = VIDEOSIZE_VGA;
2148 cam->params.roi.width = STV_IMAGE_VGA_COLS;
2149 cam->params.roi.height = STV_IMAGE_VGA_ROWS;
2150 } else {
2151 cam->sensor_type = CPIA2_SENSOR_410;
2152 cam->video_size = VIDEOSIZE_CIF;
2153 cam->params.roi.width = STV_IMAGE_CIF_COLS;
2154 cam->params.roi.height = STV_IMAGE_CIF_ROWS;
2155 }
2156
2157 cam->width = cam->params.roi.width;
2158 cam->height = cam->params.roi.height;
2159}
2160
2161/******************************************************************************
2162 *
2163 * cpia2_init_camera_struct
2164 *
2165 * Initializes camera struct, does not call reset to fill in defaults.
2166 *****************************************************************************/
2167struct camera_data *cpia2_init_camera_struct(struct usb_interface *intf)
2168{
2169 struct camera_data *cam;
2170
2171 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
2172
2173 if (!cam) {
2174 ERR("couldn't kmalloc cpia2 struct\n");
2175 return NULL;
2176 }
2177
2178 cam->v4l2_dev.release = cpia2_camera_release;
2179 if (v4l2_device_register(&intf->dev, &cam->v4l2_dev) < 0) {
2180 v4l2_err(&cam->v4l2_dev, "couldn't register v4l2_device\n");
2181 kfree(cam);
2182 return NULL;
2183 }
2184
2185 mutex_init(&cam->v4l2_lock);
2186 init_waitqueue_head(&cam->wq_stream);
2187
2188 return cam;
2189}
2190
2191/******************************************************************************
2192 *
2193 * cpia2_init_camera
2194 *
2195 * Initializes camera.
2196 *****************************************************************************/
2197int cpia2_init_camera(struct camera_data *cam)
2198{
2199 DBG("Start\n");
2200
2201 cam->mmapped = false;
2202
2203 /* Get sensor and asic types before reset. */
2204 cpia2_set_high_power(cam);
2205 cpia2_get_version_info(cam);
2206 if (cam->params.version.asic_id != CPIA2_ASIC_672) {
2207 ERR("Device IO error (asicID has incorrect value of 0x%X\n",
2208 cam->params.version.asic_id);
2209 return -ENODEV;
2210 }
2211
2212 /* Set GPIO direction and data to a safe state. */
2213 cpia2_do_command(cam, CPIA2_CMD_SET_VC_MP_GPIO_DIRECTION,
2214 TRANSFER_WRITE, 0);
2215 cpia2_do_command(cam, CPIA2_CMD_SET_VC_MP_GPIO_DATA,
2216 TRANSFER_WRITE, 0);
2217
2218 /* resetting struct requires version info for sensor and asic types */
2219 reset_camera_struct(cam);
2220
2221 cpia2_set_low_power(cam);
2222
2223 DBG("End\n");
2224
2225 return 0;
2226}
2227
2228/******************************************************************************
2229 *
2230 * cpia2_allocate_buffers
2231 *
2232 *****************************************************************************/
2233int cpia2_allocate_buffers(struct camera_data *cam)
2234{
2235 int i;
2236
2237 if(!cam->buffers) {
2238 u32 size = cam->num_frames*sizeof(struct framebuf);
2239 cam->buffers = kmalloc(size, GFP_KERNEL);
2240 if(!cam->buffers) {
2241 ERR("couldn't kmalloc frame buffer structures\n");
2242 return -ENOMEM;
2243 }
2244 }
2245
2246 if(!cam->frame_buffer) {
2247 cam->frame_buffer = rvmalloc(cam->frame_size*cam->num_frames);
2248 if (!cam->frame_buffer) {
2249 ERR("couldn't vmalloc frame buffer data area\n");
2250 kfree(cam->buffers);
2251 cam->buffers = NULL;
2252 return -ENOMEM;
2253 }
2254 }
2255
2256 for(i=0; i<cam->num_frames-1; ++i) {
2257 cam->buffers[i].next = &cam->buffers[i+1];
2258 cam->buffers[i].data = cam->frame_buffer +i*cam->frame_size;
2259 cam->buffers[i].status = FRAME_EMPTY;
2260 cam->buffers[i].length = 0;
2261 cam->buffers[i].max_length = 0;
2262 cam->buffers[i].num = i;
2263 }
2264 cam->buffers[i].next = cam->buffers;
2265 cam->buffers[i].data = cam->frame_buffer +i*cam->frame_size;
2266 cam->buffers[i].status = FRAME_EMPTY;
2267 cam->buffers[i].length = 0;
2268 cam->buffers[i].max_length = 0;
2269 cam->buffers[i].num = i;
2270 cam->curbuff = cam->buffers;
2271 cam->workbuff = cam->curbuff->next;
2272 DBG("buffers=%p, curbuff=%p, workbuff=%p\n", cam->buffers, cam->curbuff,
2273 cam->workbuff);
2274 return 0;
2275}
2276
2277/******************************************************************************
2278 *
2279 * cpia2_free_buffers
2280 *
2281 *****************************************************************************/
2282void cpia2_free_buffers(struct camera_data *cam)
2283{
2284 if(cam->buffers) {
2285 kfree(cam->buffers);
2286 cam->buffers = NULL;
2287 }
2288 if(cam->frame_buffer) {
2289 rvfree(cam->frame_buffer, cam->frame_size*cam->num_frames);
2290 cam->frame_buffer = NULL;
2291 }
2292}
2293
2294/******************************************************************************
2295 *
2296 * cpia2_read
2297 *
2298 *****************************************************************************/
2299long cpia2_read(struct camera_data *cam,
2300 char __user *buf, unsigned long count, int noblock)
2301{
2302 struct framebuf *frame;
2303
2304 if (!count)
2305 return 0;
2306
2307 if (!buf) {
2308 ERR("%s: buffer NULL\n",__func__);
2309 return -EINVAL;
2310 }
2311
2312 if (!cam) {
2313 ERR("%s: Internal error, camera_data NULL!\n",__func__);
2314 return -EINVAL;
2315 }
2316
2317 if (!cam->streaming) {
2318 /* Start streaming */
2319 cpia2_usb_stream_start(cam,
2320 cam->params.camera_state.stream_mode);
2321 }
2322
2323 /* Copy cam->curbuff in case it changes while we're processing */
2324 frame = cam->curbuff;
2325 if (noblock && frame->status != FRAME_READY) {
2326 return -EAGAIN;
2327 }
2328
2329 if (frame->status != FRAME_READY) {
2330 mutex_unlock(&cam->v4l2_lock);
2331 wait_event_interruptible(cam->wq_stream,
2332 !video_is_registered(&cam->vdev) ||
2333 (frame = cam->curbuff)->status == FRAME_READY);
2334 mutex_lock(&cam->v4l2_lock);
2335 if (signal_pending(current))
2336 return -ERESTARTSYS;
2337 if (!video_is_registered(&cam->vdev))
2338 return 0;
2339 }
2340
2341 /* copy data to user space */
2342 if (frame->length > count)
2343 return -EFAULT;
2344 if (copy_to_user(buf, frame->data, frame->length))
2345 return -EFAULT;
2346
2347 count = frame->length;
2348
2349 frame->status = FRAME_EMPTY;
2350
2351 return count;
2352}
2353
2354/******************************************************************************
2355 *
2356 * cpia2_poll
2357 *
2358 *****************************************************************************/
2359unsigned int cpia2_poll(struct camera_data *cam, struct file *filp,
2360 poll_table *wait)
2361{
2362 unsigned int status = v4l2_ctrl_poll(filp, wait);
2363
2364 if ((poll_requested_events(wait) & (POLLIN | POLLRDNORM)) &&
2365 !cam->streaming) {
2366 /* Start streaming */
2367 cpia2_usb_stream_start(cam,
2368 cam->params.camera_state.stream_mode);
2369 }
2370
2371 poll_wait(filp, &cam->wq_stream, wait);
2372
2373 if (cam->curbuff->status == FRAME_READY)
2374 status |= POLLIN | POLLRDNORM;
2375
2376 return status;
2377}
2378
2379/******************************************************************************
2380 *
2381 * cpia2_remap_buffer
2382 *
2383 *****************************************************************************/
2384int cpia2_remap_buffer(struct camera_data *cam, struct vm_area_struct *vma)
2385{
2386 const char *adr = (const char *)vma->vm_start;
2387 unsigned long size = vma->vm_end-vma->vm_start;
2388 unsigned long start_offset = vma->vm_pgoff << PAGE_SHIFT;
2389 unsigned long start = (unsigned long) adr;
2390 unsigned long page, pos;
2391
2392 DBG("mmap offset:%ld size:%ld\n", start_offset, size);
2393
2394 if (!video_is_registered(&cam->vdev))
2395 return -ENODEV;
2396
2397 if (size > cam->frame_size*cam->num_frames ||
2398 (start_offset % cam->frame_size) != 0 ||
2399 (start_offset+size > cam->frame_size*cam->num_frames))
2400 return -EINVAL;
2401
2402 pos = ((unsigned long) (cam->frame_buffer)) + start_offset;
2403 while (size > 0) {
2404 page = kvirt_to_pa(pos);
2405 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT, PAGE_SIZE, PAGE_SHARED))
2406 return -EAGAIN;
2407 start += PAGE_SIZE;
2408 pos += PAGE_SIZE;
2409 if (size > PAGE_SIZE)
2410 size -= PAGE_SIZE;
2411 else
2412 size = 0;
2413 }
2414
2415 cam->mmapped = true;
2416 return 0;
2417}
diff --git a/drivers/media/video/cpia2/cpia2_registers.h b/drivers/media/video/cpia2/cpia2_registers.h
deleted file mode 100644
index 3bbec514a967..000000000000
--- a/drivers/media/video/cpia2/cpia2_registers.h
+++ /dev/null
@@ -1,476 +0,0 @@
1/****************************************************************************
2 *
3 * Filename: cpia2registers.h
4 *
5 * Copyright 2001, STMicrolectronics, Inc.
6 *
7 * Description:
8 * Definitions for the CPia2 register set
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 ****************************************************************************/
25
26#ifndef CPIA2_REGISTER_HEADER
27#define CPIA2_REGISTER_HEADER
28
29/***
30 * System register set (Bank 0)
31 ***/
32#define CPIA2_SYSTEM_DEVICE_HI 0x00
33#define CPIA2_SYSTEM_DEVICE_LO 0x01
34
35#define CPIA2_SYSTEM_SYSTEM_CONTROL 0x02
36#define CPIA2_SYSTEM_CONTROL_LOW_POWER 0x00
37#define CPIA2_SYSTEM_CONTROL_HIGH_POWER 0x01
38#define CPIA2_SYSTEM_CONTROL_SUSPEND 0x02
39#define CPIA2_SYSTEM_CONTROL_V2W_ERR 0x10
40#define CPIA2_SYSTEM_CONTROL_RB_ERR 0x10
41#define CPIA2_SYSTEM_CONTROL_CLEAR_ERR 0x80
42
43#define CPIA2_SYSTEM_INT_PACKET_CTRL 0x04
44#define CPIA2_SYSTEM_INT_PACKET_CTRL_ENABLE_SW_XX 0x01
45#define CPIA2_SYSTEM_INT_PACKET_CTRL_ENABLE_EOF 0x02
46#define CPIA2_SYSTEM_INT_PACKET_CTRL_ENABLE_INT1 0x04
47
48#define CPIA2_SYSTEM_CACHE_CTRL 0x05
49#define CPIA2_SYSTEM_CACHE_CTRL_CACHE_RESET 0x01
50#define CPIA2_SYSTEM_CACHE_CTRL_CACHE_FLUSH 0x02
51
52#define CPIA2_SYSTEM_SERIAL_CTRL 0x06
53#define CPIA2_SYSTEM_SERIAL_CTRL_NULL_CMD 0x00
54#define CPIA2_SYSTEM_SERIAL_CTRL_START_CMD 0x01
55#define CPIA2_SYSTEM_SERIAL_CTRL_STOP_CMD 0x02
56#define CPIA2_SYSTEM_SERIAL_CTRL_WRITE_CMD 0x03
57#define CPIA2_SYSTEM_SERIAL_CTRL_READ_ACK_CMD 0x04
58#define CPIA2_SYSTEM_SERIAL_CTRL_READ_NACK_CMD 0x05
59
60#define CPIA2_SYSTEM_SERIAL_DATA 0x07
61
62#define CPIA2_SYSTEM_VP_SERIAL_ADDR 0x08
63
64/***
65 * I2C addresses for various devices in CPiA2
66 ***/
67#define CPIA2_SYSTEM_VP_SERIAL_ADDR_SENSOR 0x20
68#define CPIA2_SYSTEM_VP_SERIAL_ADDR_VP 0x88
69#define CPIA2_SYSTEM_VP_SERIAL_ADDR_676_VP 0x8A
70
71#define CPIA2_SYSTEM_SPARE_REG1 0x09
72#define CPIA2_SYSTEM_SPARE_REG2 0x0A
73#define CPIA2_SYSTEM_SPARE_REG3 0x0B
74
75#define CPIA2_SYSTEM_MC_PORT_0 0x0C
76#define CPIA2_SYSTEM_MC_PORT_1 0x0D
77#define CPIA2_SYSTEM_MC_PORT_2 0x0E
78#define CPIA2_SYSTEM_MC_PORT_3 0x0F
79
80#define CPIA2_SYSTEM_STATUS_PKT 0x20
81#define CPIA2_SYSTEM_STATUS_PKT_END 0x27
82
83#define CPIA2_SYSTEM_DESCRIP_VID_HI 0x30
84#define CPIA2_SYSTEM_DESCRIP_VID_LO 0x31
85#define CPIA2_SYSTEM_DESCRIP_PID_HI 0x32
86#define CPIA2_SYSTEM_DESCRIP_PID_LO 0x33
87
88#define CPIA2_SYSTEM_FW_VERSION_HI 0x34
89#define CPIA2_SYSTEM_FW_VERSION_LO 0x35
90
91#define CPIA2_SYSTEM_CACHE_START_INDEX 0x80
92#define CPIA2_SYSTEM_CACHE_MAX_WRITES 0x10
93
94/***
95 * VC register set (Bank 1)
96 ***/
97#define CPIA2_VC_ASIC_ID 0x80
98
99#define CPIA2_VC_ASIC_REV 0x81
100
101#define CPIA2_VC_PW_CTRL 0x82
102#define CPIA2_VC_PW_CTRL_COLDSTART 0x01
103#define CPIA2_VC_PW_CTRL_CP_CLK_EN 0x02
104#define CPIA2_VC_PW_CTRL_VP_RESET_N 0x04
105#define CPIA2_VC_PW_CTRL_VC_CLK_EN 0x08
106#define CPIA2_VC_PW_CTRL_VC_RESET_N 0x10
107#define CPIA2_VC_PW_CTRL_GOTO_SUSPEND 0x20
108#define CPIA2_VC_PW_CTRL_UDC_SUSPEND 0x40
109#define CPIA2_VC_PW_CTRL_PWR_DOWN 0x80
110
111#define CPIA2_VC_WAKEUP 0x83
112#define CPIA2_VC_WAKEUP_SW_ENABLE 0x01
113#define CPIA2_VC_WAKEUP_XX_ENABLE 0x02
114#define CPIA2_VC_WAKEUP_SW_ATWAKEUP 0x04
115#define CPIA2_VC_WAKEUP_XX_ATWAKEUP 0x08
116
117#define CPIA2_VC_CLOCK_CTRL 0x84
118#define CPIA2_VC_CLOCK_CTRL_TESTUP72 0x01
119
120#define CPIA2_VC_INT_ENABLE 0x88
121#define CPIA2_VC_INT_ENABLE_XX_IE 0x01
122#define CPIA2_VC_INT_ENABLE_SW_IE 0x02
123#define CPIA2_VC_INT_ENABLE_VC_IE 0x04
124#define CPIA2_VC_INT_ENABLE_USBDATA_IE 0x08
125#define CPIA2_VC_INT_ENABLE_USBSETUP_IE 0x10
126#define CPIA2_VC_INT_ENABLE_USBCFG_IE 0x20
127
128#define CPIA2_VC_INT_FLAG 0x89
129#define CPIA2_VC_INT_ENABLE_XX_FLAG 0x01
130#define CPIA2_VC_INT_ENABLE_SW_FLAG 0x02
131#define CPIA2_VC_INT_ENABLE_VC_FLAG 0x04
132#define CPIA2_VC_INT_ENABLE_USBDATA_FLAG 0x08
133#define CPIA2_VC_INT_ENABLE_USBSETUP_FLAG 0x10
134#define CPIA2_VC_INT_ENABLE_USBCFG_FLAG 0x20
135#define CPIA2_VC_INT_ENABLE_SET_RESET_BIT 0x80
136
137#define CPIA2_VC_INT_STATE 0x8A
138#define CPIA2_VC_INT_STATE_XX_STATE 0x01
139#define CPIA2_VC_INT_STATE_SW_STATE 0x02
140
141#define CPIA2_VC_MP_DIR 0x90
142#define CPIA2_VC_MP_DIR_INPUT 0x00
143#define CPIA2_VC_MP_DIR_OUTPUT 0x01
144
145#define CPIA2_VC_MP_DATA 0x91
146
147#define CPIA2_VC_DP_CTRL 0x98
148#define CPIA2_VC_DP_CTRL_MODE_0 0x00
149#define CPIA2_VC_DP_CTRL_MODE_A 0x01
150#define CPIA2_VC_DP_CTRL_MODE_B 0x02
151#define CPIA2_VC_DP_CTRL_MODE_C 0x03
152#define CPIA2_VC_DP_CTRL_FAKE_FST 0x04
153
154#define CPIA2_VC_AD_CTRL 0x99
155#define CPIA2_VC_AD_CTRL_SRC_0 0x00
156#define CPIA2_VC_AD_CTRL_SRC_DIGI_A 0x01
157#define CPIA2_VC_AD_CTRL_SRC_REG 0x02
158#define CPIA2_VC_AD_CTRL_DST_USB 0x00
159#define CPIA2_VC_AD_CTRL_DST_REG 0x04
160
161#define CPIA2_VC_AD_TEST_IN 0x9B
162
163#define CPIA2_VC_AD_TEST_OUT 0x9C
164
165#define CPIA2_VC_AD_STATUS 0x9D
166#define CPIA2_VC_AD_STATUS_EMPTY 0x01
167#define CPIA2_VC_AD_STATUS_FULL 0x02
168
169#define CPIA2_VC_DP_DATA 0x9E
170
171#define CPIA2_VC_ST_CTRL 0xA0
172#define CPIA2_VC_ST_CTRL_SRC_VC 0x00
173#define CPIA2_VC_ST_CTRL_SRC_DP 0x01
174#define CPIA2_VC_ST_CTRL_SRC_REG 0x02
175
176#define CPIA2_VC_ST_CTRL_RAW_SELECT 0x04
177
178#define CPIA2_VC_ST_CTRL_DST_USB 0x00
179#define CPIA2_VC_ST_CTRL_DST_DP 0x08
180#define CPIA2_VC_ST_CTRL_DST_REG 0x10
181
182#define CPIA2_VC_ST_CTRL_FIFO_ENABLE 0x20
183#define CPIA2_VC_ST_CTRL_EOF_DETECT 0x40
184
185#define CPIA2_VC_ST_TEST 0xA1
186#define CPIA2_VC_ST_TEST_MODE_MANUAL 0x00
187#define CPIA2_VC_ST_TEST_MODE_INCREMENT 0x02
188
189#define CPIA2_VC_ST_TEST_AUTO_FILL 0x08
190
191#define CPIA2_VC_ST_TEST_REPEAT_FIFO 0x10
192
193#define CPIA2_VC_ST_TEST_IN 0xA2
194
195#define CPIA2_VC_ST_TEST_OUT 0xA3
196
197#define CPIA2_VC_ST_STATUS 0xA4
198#define CPIA2_VC_ST_STATUS_EMPTY 0x01
199#define CPIA2_VC_ST_STATUS_FULL 0x02
200
201#define CPIA2_VC_ST_FRAME_DETECT_1 0xA5
202
203#define CPIA2_VC_ST_FRAME_DETECT_2 0xA6
204
205#define CPIA2_VC_USB_CTRL 0xA8
206#define CPIA2_VC_USB_CTRL_CMD_STALLED 0x01
207#define CPIA2_VC_USB_CTRL_CMD_READY 0x02
208#define CPIA2_VC_USB_CTRL_CMD_STATUS 0x04
209#define CPIA2_VC_USB_CTRL_CMD_STATUS_DIR 0x08
210#define CPIA2_VC_USB_CTRL_CMD_NO_CLASH 0x10
211#define CPIA2_VC_USB_CTRL_CMD_MICRO_ACCESS 0x80
212
213#define CPIA2_VC_USB_STRM 0xA9
214#define CPIA2_VC_USB_STRM_ISO_ENABLE 0x01
215#define CPIA2_VC_USB_STRM_BLK_ENABLE 0x02
216#define CPIA2_VC_USB_STRM_INT_ENABLE 0x04
217#define CPIA2_VC_USB_STRM_AUD_ENABLE 0x08
218
219#define CPIA2_VC_USB_STATUS 0xAA
220#define CPIA2_VC_USB_STATUS_CMD_IN_PROGRESS 0x01
221#define CPIA2_VC_USB_STATUS_CMD_STATUS_STALL 0x02
222#define CPIA2_VC_USB_STATUS_CMD_HANDSHAKE 0x04
223#define CPIA2_VC_USB_STATUS_CMD_OVERRIDE 0x08
224#define CPIA2_VC_USB_STATUS_CMD_FIFO_BUSY 0x10
225#define CPIA2_VC_USB_STATUS_BULK_REPEAT_TXN 0x20
226#define CPIA2_VC_USB_STATUS_CONFIG_DONE 0x40
227#define CPIA2_VC_USB_STATUS_USB_SUSPEND 0x80
228
229#define CPIA2_VC_USB_CMDW 0xAB
230
231#define CPIA2_VC_USB_DATARW 0xAC
232
233#define CPIA2_VC_USB_INFO 0xAD
234
235#define CPIA2_VC_USB_CONFIG 0xAE
236
237#define CPIA2_VC_USB_SETTINGS 0xAF
238#define CPIA2_VC_USB_SETTINGS_CONFIG_MASK 0x03
239#define CPIA2_VC_USB_SETTINGS_INTERFACE_MASK 0x0C
240#define CPIA2_VC_USB_SETTINGS_ALTERNATE_MASK 0x70
241
242#define CPIA2_VC_USB_ISOLIM 0xB0
243
244#define CPIA2_VC_USB_ISOFAILS 0xB1
245
246#define CPIA2_VC_USB_ISOMAXPKTHI 0xB2
247
248#define CPIA2_VC_USB_ISOMAXPKTLO 0xB3
249
250#define CPIA2_VC_V2W_CTRL 0xB8
251#define CPIA2_VC_V2W_SELECT 0x01
252
253#define CPIA2_VC_V2W_SCL 0xB9
254
255#define CPIA2_VC_V2W_SDA 0xBA
256
257#define CPIA2_VC_VC_CTRL 0xC0
258#define CPIA2_VC_VC_CTRL_RUN 0x01
259#define CPIA2_VC_VC_CTRL_SINGLESHOT 0x02
260#define CPIA2_VC_VC_CTRL_IDLING 0x04
261#define CPIA2_VC_VC_CTRL_INHIBIT_H_TABLES 0x10
262#define CPIA2_VC_VC_CTRL_INHIBIT_Q_TABLES 0x20
263#define CPIA2_VC_VC_CTRL_INHIBIT_PRIVATE 0x40
264
265#define CPIA2_VC_VC_RESTART_IVAL_HI 0xC1
266
267#define CPIA2_VC_VC_RESTART_IVAL_LO 0xC2
268
269#define CPIA2_VC_VC_FORMAT 0xC3
270#define CPIA2_VC_VC_FORMAT_UFIRST 0x01
271#define CPIA2_VC_VC_FORMAT_MONO 0x02
272#define CPIA2_VC_VC_FORMAT_DECIMATING 0x04
273#define CPIA2_VC_VC_FORMAT_SHORTLINE 0x08
274#define CPIA2_VC_VC_FORMAT_SELFTEST 0x10
275
276#define CPIA2_VC_VC_CLOCKS 0xC4
277#define CPIA2_VC_VC_CLOCKS_CLKDIV_MASK 0x03
278#define CPIA2_VC_VC_672_CLOCKS_CIF_DIV_BY_3 0x04
279#define CPIA2_VC_VC_672_CLOCKS_SCALING 0x08
280#define CPIA2_VC_VC_CLOCKS_LOGDIV0 0x00
281#define CPIA2_VC_VC_CLOCKS_LOGDIV1 0x01
282#define CPIA2_VC_VC_CLOCKS_LOGDIV2 0x02
283#define CPIA2_VC_VC_CLOCKS_LOGDIV3 0x03
284#define CPIA2_VC_VC_676_CLOCKS_CIF_DIV_BY_3 0x08
285#define CPIA2_VC_VC_676_CLOCKS_SCALING 0x10
286
287#define CPIA2_VC_VC_IHSIZE_LO 0xC5
288
289#define CPIA2_VC_VC_XLIM_HI 0xC6
290
291#define CPIA2_VC_VC_XLIM_LO 0xC7
292
293#define CPIA2_VC_VC_YLIM_HI 0xC8
294
295#define CPIA2_VC_VC_YLIM_LO 0xC9
296
297#define CPIA2_VC_VC_OHSIZE 0xCA
298
299#define CPIA2_VC_VC_OVSIZE 0xCB
300
301#define CPIA2_VC_VC_HCROP 0xCC
302
303#define CPIA2_VC_VC_VCROP 0xCD
304
305#define CPIA2_VC_VC_HPHASE 0xCE
306
307#define CPIA2_VC_VC_VPHASE 0xCF
308
309#define CPIA2_VC_VC_HISPAN 0xD0
310
311#define CPIA2_VC_VC_VISPAN 0xD1
312
313#define CPIA2_VC_VC_HICROP 0xD2
314
315#define CPIA2_VC_VC_VICROP 0xD3
316
317#define CPIA2_VC_VC_HFRACT 0xD4
318#define CPIA2_VC_VC_HFRACT_DEN_MASK 0x0F
319#define CPIA2_VC_VC_HFRACT_NUM_MASK 0xF0
320
321#define CPIA2_VC_VC_VFRACT 0xD5
322#define CPIA2_VC_VC_VFRACT_DEN_MASK 0x0F
323#define CPIA2_VC_VC_VFRACT_NUM_MASK 0xF0
324
325#define CPIA2_VC_VC_JPEG_OPT 0xD6
326#define CPIA2_VC_VC_JPEG_OPT_DOUBLE_SQUEEZE 0x01
327#define CPIA2_VC_VC_JPEG_OPT_NO_DC_AUTO_SQUEEZE 0x02
328#define CPIA2_VC_VC_JPEG_OPT_AUTO_SQUEEZE 0x04
329#define CPIA2_VC_VC_JPEG_OPT_DEFAULT (CPIA2_VC_VC_JPEG_OPT_DOUBLE_SQUEEZE|\
330 CPIA2_VC_VC_JPEG_OPT_AUTO_SQUEEZE)
331
332
333#define CPIA2_VC_VC_CREEP_PERIOD 0xD7
334#define CPIA2_VC_VC_USER_SQUEEZE 0xD8
335#define CPIA2_VC_VC_TARGET_KB 0xD9
336
337#define CPIA2_VC_VC_AUTO_SQUEEZE 0xE6
338
339
340/***
341 * VP register set (Bank 2)
342 ***/
343#define CPIA2_VP_DEVICEH 0
344#define CPIA2_VP_DEVICEL 1
345
346#define CPIA2_VP_SYSTEMSTATE 0x02
347#define CPIA2_VP_SYSTEMSTATE_HK_ALIVE 0x01
348
349#define CPIA2_VP_SYSTEMCTRL 0x03
350#define CPIA2_VP_SYSTEMCTRL_REQ_CLEAR_ERROR 0x80
351#define CPIA2_VP_SYSTEMCTRL_POWER_DOWN_PLL 0x20
352#define CPIA2_VP_SYSTEMCTRL_REQ_SUSPEND_STATE 0x10
353#define CPIA2_VP_SYSTEMCTRL_REQ_SERIAL_WAKEUP 0x08
354#define CPIA2_VP_SYSTEMCTRL_REQ_AUTOLOAD 0x04
355#define CPIA2_VP_SYSTEMCTRL_HK_CONTROL 0x02
356#define CPIA2_VP_SYSTEMCTRL_POWER_CONTROL 0x01
357
358#define CPIA2_VP_SENSOR_FLAGS 0x05
359#define CPIA2_VP_SENSOR_FLAGS_404 0x01
360#define CPIA2_VP_SENSOR_FLAGS_407 0x02
361#define CPIA2_VP_SENSOR_FLAGS_409 0x04
362#define CPIA2_VP_SENSOR_FLAGS_410 0x08
363#define CPIA2_VP_SENSOR_FLAGS_500 0x10
364
365#define CPIA2_VP_SENSOR_REV 0x06
366
367#define CPIA2_VP_DEVICE_CONFIG 0x07
368#define CPIA2_VP_DEVICE_CONFIG_SERIAL_BRIDGE 0x01
369
370#define CPIA2_VP_GPIO_DIRECTION 0x08
371#define CPIA2_VP_GPIO_READ 0xFF
372#define CPIA2_VP_GPIO_WRITE 0x00
373
374#define CPIA2_VP_GPIO_DATA 0x09
375
376#define CPIA2_VP_RAM_ADDR_H 0x0A
377#define CPIA2_VP_RAM_ADDR_L 0x0B
378#define CPIA2_VP_RAM_DATA 0x0C
379
380#define CPIA2_VP_PATCH_REV 0x0F
381
382#define CPIA2_VP4_USER_MODE 0x10
383#define CPIA2_VP5_USER_MODE 0x13
384#define CPIA2_VP_USER_MODE_CIF 0x01
385#define CPIA2_VP_USER_MODE_QCIFDS 0x02
386#define CPIA2_VP_USER_MODE_QCIFPTC 0x04
387#define CPIA2_VP_USER_MODE_QVGADS 0x08
388#define CPIA2_VP_USER_MODE_QVGAPTC 0x10
389#define CPIA2_VP_USER_MODE_VGA 0x20
390
391#define CPIA2_VP4_FRAMERATE_REQUEST 0x11
392#define CPIA2_VP5_FRAMERATE_REQUEST 0x14
393#define CPIA2_VP_FRAMERATE_60 0x80
394#define CPIA2_VP_FRAMERATE_50 0x40
395#define CPIA2_VP_FRAMERATE_30 0x20
396#define CPIA2_VP_FRAMERATE_25 0x10
397#define CPIA2_VP_FRAMERATE_15 0x08
398#define CPIA2_VP_FRAMERATE_12_5 0x04
399#define CPIA2_VP_FRAMERATE_7_5 0x02
400#define CPIA2_VP_FRAMERATE_6_25 0x01
401
402#define CPIA2_VP4_USER_EFFECTS 0x12
403#define CPIA2_VP5_USER_EFFECTS 0x15
404#define CPIA2_VP_USER_EFFECTS_COLBARS 0x01
405#define CPIA2_VP_USER_EFFECTS_COLBARS_GRAD 0x02
406#define CPIA2_VP_USER_EFFECTS_MIRROR 0x04
407#define CPIA2_VP_USER_EFFECTS_FLIP 0x40 // VP5 only
408
409/* NOTE: CPIA2_VP_EXPOSURE_MODES shares the same register as VP5 User
410 * Effects */
411#define CPIA2_VP_EXPOSURE_MODES 0x15
412#define CPIA2_VP_EXPOSURE_MODES_INHIBIT_FLICKER 0x20
413#define CPIA2_VP_EXPOSURE_MODES_COMPILE_EXP 0x10
414
415#define CPIA2_VP4_EXPOSURE_TARGET 0x16 // VP4
416#define CPIA2_VP5_EXPOSURE_TARGET 0x20 // VP5
417
418#define CPIA2_VP_FLICKER_MODES 0x1B
419#define CPIA2_VP_FLICKER_MODES_50HZ 0x80
420#define CPIA2_VP_FLICKER_MODES_CUSTOM_FLT_FFREQ 0x40
421#define CPIA2_VP_FLICKER_MODES_NEVER_FLICKER 0x20
422#define CPIA2_VP_FLICKER_MODES_INHIBIT_RUB 0x10
423#define CPIA2_VP_FLICKER_MODES_ADJUST_LINE_FREQ 0x08
424#define CPIA2_VP_FLICKER_MODES_CUSTOM_INT_FFREQ 0x04
425
426#define CPIA2_VP_UMISC 0x1D
427#define CPIA2_VP_UMISC_FORCE_MONO 0x80
428#define CPIA2_VP_UMISC_FORCE_ID_MASK 0x40
429#define CPIA2_VP_UMISC_INHIBIT_AUTO_FGS 0x20
430#define CPIA2_VP_UMISC_INHIBIT_AUTO_DIMS 0x08
431#define CPIA2_VP_UMISC_OPT_FOR_SENSOR_DS 0x04
432#define CPIA2_VP_UMISC_INHIBIT_AUTO_MODE_INT 0x02
433
434#define CPIA2_VP5_ANTIFLKRSETUP 0x22 //34
435
436#define CPIA2_VP_INTERPOLATION 0x24
437#define CPIA2_VP_INTERPOLATION_EVEN_FIRST 0x40
438#define CPIA2_VP_INTERPOLATION_HJOG 0x20
439#define CPIA2_VP_INTERPOLATION_VJOG 0x10
440
441#define CPIA2_VP_GAMMA 0x25
442#define CPIA2_VP_DEFAULT_GAMMA 0x10
443
444#define CPIA2_VP_YRANGE 0x26
445
446#define CPIA2_VP_SATURATION 0x27
447
448#define CPIA2_VP5_MYBLACK_LEVEL 0x3A //58
449#define CPIA2_VP5_MCYRANGE 0x3B //59
450#define CPIA2_VP5_MYCEILING 0x3C //60
451#define CPIA2_VP5_MCUVSATURATION 0x3D //61
452
453
454#define CPIA2_VP_REHASH_VALUES 0x60
455
456
457/***
458 * Common sensor registers
459 ***/
460#define CPIA2_SENSOR_DEVICE_H 0x00
461#define CPIA2_SENSOR_DEVICE_L 0x01
462
463#define CPIA2_SENSOR_DATA_FORMAT 0x16
464#define CPIA2_SENSOR_DATA_FORMAT_HMIRROR 0x08
465#define CPIA2_SENSOR_DATA_FORMAT_VMIRROR 0x10
466
467#define CPIA2_SENSOR_CR1 0x76
468#define CPIA2_SENSOR_CR1_STAND_BY 0x01
469#define CPIA2_SENSOR_CR1_DOWN_RAMP_GEN 0x02
470#define CPIA2_SENSOR_CR1_DOWN_COLUMN_ADC 0x04
471#define CPIA2_SENSOR_CR1_DOWN_CAB_REGULATOR 0x08
472#define CPIA2_SENSOR_CR1_DOWN_AUDIO_REGULATOR 0x10
473#define CPIA2_SENSOR_CR1_DOWN_VRT_AMP 0x20
474#define CPIA2_SENSOR_CR1_DOWN_BAND_GAP 0x40
475
476#endif
diff --git a/drivers/media/video/cpia2/cpia2_usb.c b/drivers/media/video/cpia2/cpia2_usb.c
deleted file mode 100644
index 95b5d6e7cdc4..000000000000
--- a/drivers/media/video/cpia2/cpia2_usb.c
+++ /dev/null
@@ -1,955 +0,0 @@
1/****************************************************************************
2 *
3 * Filename: cpia2_usb.c
4 *
5 * Copyright 2001, STMicrolectronics, Inc.
6 * Contact: steve.miller@st.com
7 *
8 * Description:
9 * This is a USB driver for CPia2 based video cameras.
10 * The infrastructure of this driver is based on the cpia usb driver by
11 * Jochen Scharrlach and Johannes Erdfeldt.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 * Stripped of 2.4 stuff ready for main kernel submit by
28 * Alan Cox <alan@lxorguk.ukuu.org.uk>
29 ****************************************************************************/
30
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/usb.h>
34#include <linux/module.h>
35
36#include "cpia2.h"
37
38static int frame_sizes[] = {
39 0, // USBIF_CMDONLY
40 0, // USBIF_BULK
41 128, // USBIF_ISO_1
42 384, // USBIF_ISO_2
43 640, // USBIF_ISO_3
44 768, // USBIF_ISO_4
45 896, // USBIF_ISO_5
46 1023, // USBIF_ISO_6
47};
48
49#define FRAMES_PER_DESC 10
50#define FRAME_SIZE_PER_DESC frame_sizes[cam->cur_alt]
51
52static void process_frame(struct camera_data *cam);
53static void cpia2_usb_complete(struct urb *urb);
54static int cpia2_usb_probe(struct usb_interface *intf,
55 const struct usb_device_id *id);
56static void cpia2_usb_disconnect(struct usb_interface *intf);
57static int cpia2_usb_suspend(struct usb_interface *intf, pm_message_t message);
58static int cpia2_usb_resume(struct usb_interface *intf);
59
60static void free_sbufs(struct camera_data *cam);
61static void add_APPn(struct camera_data *cam);
62static void add_COM(struct camera_data *cam);
63static int submit_urbs(struct camera_data *cam);
64static int set_alternate(struct camera_data *cam, unsigned int alt);
65static int configure_transfer_mode(struct camera_data *cam, unsigned int alt);
66
67static struct usb_device_id cpia2_id_table[] = {
68 {USB_DEVICE(0x0553, 0x0100)},
69 {USB_DEVICE(0x0553, 0x0140)},
70 {USB_DEVICE(0x0553, 0x0151)}, /* STV0676 */
71 {} /* Terminating entry */
72};
73MODULE_DEVICE_TABLE(usb, cpia2_id_table);
74
75static struct usb_driver cpia2_driver = {
76 .name = "cpia2",
77 .probe = cpia2_usb_probe,
78 .disconnect = cpia2_usb_disconnect,
79 .suspend = cpia2_usb_suspend,
80 .resume = cpia2_usb_resume,
81 .reset_resume = cpia2_usb_resume,
82 .id_table = cpia2_id_table
83};
84
85
86/******************************************************************************
87 *
88 * process_frame
89 *
90 *****************************************************************************/
91static void process_frame(struct camera_data *cam)
92{
93 static int frame_count;
94
95 unsigned char *inbuff = cam->workbuff->data;
96
97 DBG("Processing frame #%d, current:%d\n",
98 cam->workbuff->num, cam->curbuff->num);
99
100 if(cam->workbuff->length > cam->workbuff->max_length)
101 cam->workbuff->max_length = cam->workbuff->length;
102
103 if ((inbuff[0] == 0xFF) && (inbuff[1] == 0xD8)) {
104 frame_count++;
105 } else {
106 cam->workbuff->status = FRAME_ERROR;
107 DBG("Start of frame not found\n");
108 return;
109 }
110
111 /***
112 * Now the output buffer should have a JPEG image in it.
113 ***/
114 if(!cam->first_image_seen) {
115 /* Always skip the first image after streaming
116 * starts. It is almost certainly corrupt. */
117 cam->first_image_seen = 1;
118 cam->workbuff->status = FRAME_EMPTY;
119 return;
120 }
121 if (cam->workbuff->length > 3) {
122 if(cam->mmapped &&
123 cam->workbuff->length < cam->workbuff->max_length) {
124 /* No junk in the buffers */
125 memset(cam->workbuff->data+cam->workbuff->length,
126 0, cam->workbuff->max_length-
127 cam->workbuff->length);
128 }
129 cam->workbuff->max_length = cam->workbuff->length;
130 cam->workbuff->status = FRAME_READY;
131
132 if(!cam->mmapped && cam->num_frames > 2) {
133 /* During normal reading, the most recent
134 * frame will be read. If the current frame
135 * hasn't started reading yet, it will never
136 * be read, so mark it empty. If the buffer is
137 * mmapped, or we have few buffers, we need to
138 * wait for the user to free the buffer.
139 *
140 * NOTE: This is not entirely foolproof with 3
141 * buffers, but it would take an EXTREMELY
142 * overloaded system to cause problems (possible
143 * image data corruption). Basically, it would
144 * need to take more time to execute cpia2_read
145 * than it would for the camera to send
146 * cam->num_frames-2 frames before problems
147 * could occur.
148 */
149 cam->curbuff->status = FRAME_EMPTY;
150 }
151 cam->curbuff = cam->workbuff;
152 cam->workbuff = cam->workbuff->next;
153 DBG("Changed buffers, work:%d, current:%d\n",
154 cam->workbuff->num, cam->curbuff->num);
155 return;
156 } else {
157 DBG("Not enough data for an image.\n");
158 }
159
160 cam->workbuff->status = FRAME_ERROR;
161 return;
162}
163
164/******************************************************************************
165 *
166 * add_APPn
167 *
168 * Adds a user specified APPn record
169 *****************************************************************************/
170static void add_APPn(struct camera_data *cam)
171{
172 if(cam->APP_len > 0) {
173 cam->workbuff->data[cam->workbuff->length++] = 0xFF;
174 cam->workbuff->data[cam->workbuff->length++] = 0xE0+cam->APPn;
175 cam->workbuff->data[cam->workbuff->length++] = 0;
176 cam->workbuff->data[cam->workbuff->length++] = cam->APP_len+2;
177 memcpy(cam->workbuff->data+cam->workbuff->length,
178 cam->APP_data, cam->APP_len);
179 cam->workbuff->length += cam->APP_len;
180 }
181}
182
183/******************************************************************************
184 *
185 * add_COM
186 *
187 * Adds a user specified COM record
188 *****************************************************************************/
189static void add_COM(struct camera_data *cam)
190{
191 if(cam->COM_len > 0) {
192 cam->workbuff->data[cam->workbuff->length++] = 0xFF;
193 cam->workbuff->data[cam->workbuff->length++] = 0xFE;
194 cam->workbuff->data[cam->workbuff->length++] = 0;
195 cam->workbuff->data[cam->workbuff->length++] = cam->COM_len+2;
196 memcpy(cam->workbuff->data+cam->workbuff->length,
197 cam->COM_data, cam->COM_len);
198 cam->workbuff->length += cam->COM_len;
199 }
200}
201
202/******************************************************************************
203 *
204 * cpia2_usb_complete
205 *
206 * callback when incoming packet is received
207 *****************************************************************************/
208static void cpia2_usb_complete(struct urb *urb)
209{
210 int i;
211 unsigned char *cdata;
212 static int frame_ready = false;
213 struct camera_data *cam = (struct camera_data *) urb->context;
214
215 if (urb->status!=0) {
216 if (!(urb->status == -ENOENT ||
217 urb->status == -ECONNRESET ||
218 urb->status == -ESHUTDOWN))
219 {
220 DBG("urb->status = %d!\n", urb->status);
221 }
222 DBG("Stopping streaming\n");
223 return;
224 }
225
226 if (!cam->streaming || !video_is_registered(&cam->vdev)) {
227 LOG("Will now stop the streaming: streaming = %d, present=%d\n",
228 cam->streaming, video_is_registered(&cam->vdev));
229 return;
230 }
231
232 /***
233 * Packet collater
234 ***/
235 //DBG("Collating %d packets\n", urb->number_of_packets);
236 for (i = 0; i < urb->number_of_packets; i++) {
237 u16 checksum, iso_checksum;
238 int j;
239 int n = urb->iso_frame_desc[i].actual_length;
240 int st = urb->iso_frame_desc[i].status;
241
242 if(cam->workbuff->status == FRAME_READY) {
243 struct framebuf *ptr;
244 /* Try to find an available buffer */
245 DBG("workbuff full, searching\n");
246 for (ptr = cam->workbuff->next;
247 ptr != cam->workbuff;
248 ptr = ptr->next)
249 {
250 if (ptr->status == FRAME_EMPTY) {
251 ptr->status = FRAME_READING;
252 ptr->length = 0;
253 break;
254 }
255 }
256 if (ptr == cam->workbuff)
257 break; /* No READING or EMPTY buffers left */
258
259 cam->workbuff = ptr;
260 }
261
262 if (cam->workbuff->status == FRAME_EMPTY ||
263 cam->workbuff->status == FRAME_ERROR) {
264 cam->workbuff->status = FRAME_READING;
265 cam->workbuff->length = 0;
266 }
267
268 //DBG(" Packet %d length = %d, status = %d\n", i, n, st);
269 cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
270
271 if (st) {
272 LOG("cpia2 data error: [%d] len=%d, status = %d\n",
273 i, n, st);
274 if(!ALLOW_CORRUPT)
275 cam->workbuff->status = FRAME_ERROR;
276 continue;
277 }
278
279 if(n<=2)
280 continue;
281
282 checksum = 0;
283 for(j=0; j<n-2; ++j)
284 checksum += cdata[j];
285 iso_checksum = cdata[j] + cdata[j+1]*256;
286 if(checksum != iso_checksum) {
287 LOG("checksum mismatch: [%d] len=%d, calculated = %x, checksum = %x\n",
288 i, n, (int)checksum, (int)iso_checksum);
289 if(!ALLOW_CORRUPT) {
290 cam->workbuff->status = FRAME_ERROR;
291 continue;
292 }
293 }
294 n -= 2;
295
296 if(cam->workbuff->status != FRAME_READING) {
297 if((0xFF == cdata[0] && 0xD8 == cdata[1]) ||
298 (0xD8 == cdata[0] && 0xFF == cdata[1] &&
299 0 != cdata[2])) {
300 /* frame is skipped, but increment total
301 * frame count anyway */
302 cam->frame_count++;
303 }
304 DBG("workbuff not reading, status=%d\n",
305 cam->workbuff->status);
306 continue;
307 }
308
309 if (cam->frame_size < cam->workbuff->length + n) {
310 ERR("buffer overflow! length: %d, n: %d\n",
311 cam->workbuff->length, n);
312 cam->workbuff->status = FRAME_ERROR;
313 if(cam->workbuff->length > cam->workbuff->max_length)
314 cam->workbuff->max_length =
315 cam->workbuff->length;
316 continue;
317 }
318
319 if (cam->workbuff->length == 0) {
320 int data_offset;
321 if ((0xD8 == cdata[0]) && (0xFF == cdata[1])) {
322 data_offset = 1;
323 } else if((0xFF == cdata[0]) && (0xD8 == cdata[1])
324 && (0xFF == cdata[2])) {
325 data_offset = 2;
326 } else {
327 DBG("Ignoring packet, not beginning!\n");
328 continue;
329 }
330 DBG("Start of frame pattern found\n");
331 do_gettimeofday(&cam->workbuff->timestamp);
332 cam->workbuff->seq = cam->frame_count++;
333 cam->workbuff->data[0] = 0xFF;
334 cam->workbuff->data[1] = 0xD8;
335 cam->workbuff->length = 2;
336 add_APPn(cam);
337 add_COM(cam);
338 memcpy(cam->workbuff->data+cam->workbuff->length,
339 cdata+data_offset, n-data_offset);
340 cam->workbuff->length += n-data_offset;
341 } else if (cam->workbuff->length > 0) {
342 memcpy(cam->workbuff->data + cam->workbuff->length,
343 cdata, n);
344 cam->workbuff->length += n;
345 }
346
347 if ((cam->workbuff->length >= 3) &&
348 (cam->workbuff->data[cam->workbuff->length - 3] == 0xFF) &&
349 (cam->workbuff->data[cam->workbuff->length - 2] == 0xD9) &&
350 (cam->workbuff->data[cam->workbuff->length - 1] == 0xFF)) {
351 frame_ready = true;
352 cam->workbuff->data[cam->workbuff->length - 1] = 0;
353 cam->workbuff->length -= 1;
354 } else if ((cam->workbuff->length >= 2) &&
355 (cam->workbuff->data[cam->workbuff->length - 2] == 0xFF) &&
356 (cam->workbuff->data[cam->workbuff->length - 1] == 0xD9)) {
357 frame_ready = true;
358 }
359
360 if (frame_ready) {
361 DBG("Workbuff image size = %d\n",cam->workbuff->length);
362 process_frame(cam);
363
364 frame_ready = false;
365
366 if (waitqueue_active(&cam->wq_stream))
367 wake_up_interruptible(&cam->wq_stream);
368 }
369 }
370
371 if(cam->streaming) {
372 /* resubmit */
373 urb->dev = cam->dev;
374 if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
375 ERR("%s: usb_submit_urb ret %d!\n", __func__, i);
376 }
377}
378
379/******************************************************************************
380 *
381 * configure_transfer_mode
382 *
383 *****************************************************************************/
384static int configure_transfer_mode(struct camera_data *cam, unsigned int alt)
385{
386 static unsigned char iso_regs[8][4] = {
387 {0x00, 0x00, 0x00, 0x00},
388 {0x00, 0x00, 0x00, 0x00},
389 {0xB9, 0x00, 0x00, 0x7E},
390 {0xB9, 0x00, 0x01, 0x7E},
391 {0xB9, 0x00, 0x02, 0x7E},
392 {0xB9, 0x00, 0x02, 0xFE},
393 {0xB9, 0x00, 0x03, 0x7E},
394 {0xB9, 0x00, 0x03, 0xFD}
395 };
396 struct cpia2_command cmd;
397 unsigned char reg;
398
399 if (!video_is_registered(&cam->vdev))
400 return -ENODEV;
401
402 /***
403 * Write the isoc registers according to the alternate selected
404 ***/
405 cmd.direction = TRANSFER_WRITE;
406 cmd.buffer.block_data[0] = iso_regs[alt][0];
407 cmd.buffer.block_data[1] = iso_regs[alt][1];
408 cmd.buffer.block_data[2] = iso_regs[alt][2];
409 cmd.buffer.block_data[3] = iso_regs[alt][3];
410 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
411 cmd.start = CPIA2_VC_USB_ISOLIM;
412 cmd.reg_count = 4;
413 cpia2_send_command(cam, &cmd);
414
415 /***
416 * Enable relevant streams before starting polling.
417 * First read USB Stream Config Register.
418 ***/
419 cmd.direction = TRANSFER_READ;
420 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
421 cmd.start = CPIA2_VC_USB_STRM;
422 cmd.reg_count = 1;
423 cpia2_send_command(cam, &cmd);
424 reg = cmd.buffer.block_data[0];
425
426 /* Clear iso, bulk, and int */
427 reg &= ~(CPIA2_VC_USB_STRM_BLK_ENABLE |
428 CPIA2_VC_USB_STRM_ISO_ENABLE |
429 CPIA2_VC_USB_STRM_INT_ENABLE);
430
431 if (alt == USBIF_BULK) {
432 DBG("Enabling bulk xfer\n");
433 reg |= CPIA2_VC_USB_STRM_BLK_ENABLE; /* Enable Bulk */
434 cam->xfer_mode = XFER_BULK;
435 } else if (alt >= USBIF_ISO_1) {
436 DBG("Enabling ISOC xfer\n");
437 reg |= CPIA2_VC_USB_STRM_ISO_ENABLE;
438 cam->xfer_mode = XFER_ISOC;
439 }
440
441 cmd.buffer.block_data[0] = reg;
442 cmd.direction = TRANSFER_WRITE;
443 cmd.start = CPIA2_VC_USB_STRM;
444 cmd.reg_count = 1;
445 cmd.req_mode = CAMERAACCESS_TYPE_BLOCK | CAMERAACCESS_VC;
446 cpia2_send_command(cam, &cmd);
447
448 return 0;
449}
450
451/******************************************************************************
452 *
453 * cpia2_usb_change_streaming_alternate
454 *
455 *****************************************************************************/
456int cpia2_usb_change_streaming_alternate(struct camera_data *cam,
457 unsigned int alt)
458{
459 int ret = 0;
460
461 if(alt < USBIF_ISO_1 || alt > USBIF_ISO_6)
462 return -EINVAL;
463
464 if(alt == cam->params.camera_state.stream_mode)
465 return 0;
466
467 cpia2_usb_stream_pause(cam);
468
469 configure_transfer_mode(cam, alt);
470
471 cam->params.camera_state.stream_mode = alt;
472
473 /* Reset the camera to prevent image quality degradation */
474 cpia2_reset_camera(cam);
475
476 cpia2_usb_stream_resume(cam);
477
478 return ret;
479}
480
481/******************************************************************************
482 *
483 * set_alternate
484 *
485 *****************************************************************************/
486static int set_alternate(struct camera_data *cam, unsigned int alt)
487{
488 int ret = 0;
489
490 if(alt == cam->cur_alt)
491 return 0;
492
493 if (cam->cur_alt != USBIF_CMDONLY) {
494 DBG("Changing from alt %d to %d\n", cam->cur_alt, USBIF_CMDONLY);
495 ret = usb_set_interface(cam->dev, cam->iface, USBIF_CMDONLY);
496 if (ret != 0)
497 return ret;
498 }
499 if (alt != USBIF_CMDONLY) {
500 DBG("Changing from alt %d to %d\n", USBIF_CMDONLY, alt);
501 ret = usb_set_interface(cam->dev, cam->iface, alt);
502 if (ret != 0)
503 return ret;
504 }
505
506 cam->old_alt = cam->cur_alt;
507 cam->cur_alt = alt;
508
509 return ret;
510}
511
512/******************************************************************************
513 *
514 * free_sbufs
515 *
516 * Free all cam->sbuf[]. All non-NULL .data and .urb members that are non-NULL
517 * are assumed to be allocated. Non-NULL .urb members are also assumed to be
518 * submitted (and must therefore be killed before they are freed).
519 *****************************************************************************/
520static void free_sbufs(struct camera_data *cam)
521{
522 int i;
523
524 for (i = 0; i < NUM_SBUF; i++) {
525 if(cam->sbuf[i].urb) {
526 usb_kill_urb(cam->sbuf[i].urb);
527 usb_free_urb(cam->sbuf[i].urb);
528 cam->sbuf[i].urb = NULL;
529 }
530 if(cam->sbuf[i].data) {
531 kfree(cam->sbuf[i].data);
532 cam->sbuf[i].data = NULL;
533 }
534 }
535}
536
537/*******
538* Convenience functions
539*******/
540/****************************************************************************
541 *
542 * write_packet
543 *
544 ***************************************************************************/
545static int write_packet(struct usb_device *udev,
546 u8 request, u8 * registers, u16 start, size_t size)
547{
548 if (!registers || size <= 0)
549 return -EINVAL;
550
551 return usb_control_msg(udev,
552 usb_sndctrlpipe(udev, 0),
553 request,
554 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
555 start, /* value */
556 0, /* index */
557 registers, /* buffer */
558 size,
559 HZ);
560}
561
562/****************************************************************************
563 *
564 * read_packet
565 *
566 ***************************************************************************/
567static int read_packet(struct usb_device *udev,
568 u8 request, u8 * registers, u16 start, size_t size)
569{
570 if (!registers || size <= 0)
571 return -EINVAL;
572
573 return usb_control_msg(udev,
574 usb_rcvctrlpipe(udev, 0),
575 request,
576 USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
577 start, /* value */
578 0, /* index */
579 registers, /* buffer */
580 size,
581 HZ);
582}
583
584/******************************************************************************
585 *
586 * cpia2_usb_transfer_cmd
587 *
588 *****************************************************************************/
589int cpia2_usb_transfer_cmd(struct camera_data *cam,
590 void *registers,
591 u8 request, u8 start, u8 count, u8 direction)
592{
593 int err = 0;
594 struct usb_device *udev = cam->dev;
595
596 if (!udev) {
597 ERR("%s: Internal driver error: udev is NULL\n", __func__);
598 return -EINVAL;
599 }
600
601 if (!registers) {
602 ERR("%s: Internal driver error: register array is NULL\n", __func__);
603 return -EINVAL;
604 }
605
606 if (direction == TRANSFER_READ) {
607 err = read_packet(udev, request, (u8 *)registers, start, count);
608 if (err > 0)
609 err = 0;
610 } else if (direction == TRANSFER_WRITE) {
611 err =write_packet(udev, request, (u8 *)registers, start, count);
612 if (err < 0) {
613 LOG("Control message failed, err val = %d\n", err);
614 LOG("Message: request = 0x%0X, start = 0x%0X\n",
615 request, start);
616 LOG("Message: count = %d, register[0] = 0x%0X\n",
617 count, ((unsigned char *) registers)[0]);
618 } else
619 err=0;
620 } else {
621 LOG("Unexpected first byte of direction: %d\n",
622 direction);
623 return -EINVAL;
624 }
625
626 if(err != 0)
627 LOG("Unexpected error: %d\n", err);
628 return err;
629}
630
631
632/******************************************************************************
633 *
634 * submit_urbs
635 *
636 *****************************************************************************/
637static int submit_urbs(struct camera_data *cam)
638{
639 struct urb *urb;
640 int fx, err, i, j;
641
642 for(i=0; i<NUM_SBUF; ++i) {
643 if (cam->sbuf[i].data)
644 continue;
645 cam->sbuf[i].data =
646 kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
647 if (!cam->sbuf[i].data) {
648 while (--i >= 0) {
649 kfree(cam->sbuf[i].data);
650 cam->sbuf[i].data = NULL;
651 }
652 return -ENOMEM;
653 }
654 }
655
656 /* We double buffer the Isoc lists, and also know the polling
657 * interval is every frame (1 == (1 << (bInterval -1))).
658 */
659 for(i=0; i<NUM_SBUF; ++i) {
660 if(cam->sbuf[i].urb) {
661 continue;
662 }
663 urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
664 if (!urb) {
665 ERR("%s: usb_alloc_urb error!\n", __func__);
666 for (j = 0; j < i; j++)
667 usb_free_urb(cam->sbuf[j].urb);
668 return -ENOMEM;
669 }
670
671 cam->sbuf[i].urb = urb;
672 urb->dev = cam->dev;
673 urb->context = cam;
674 urb->pipe = usb_rcvisocpipe(cam->dev, 1 /*ISOC endpoint*/);
675 urb->transfer_flags = URB_ISO_ASAP;
676 urb->transfer_buffer = cam->sbuf[i].data;
677 urb->complete = cpia2_usb_complete;
678 urb->number_of_packets = FRAMES_PER_DESC;
679 urb->interval = 1;
680 urb->transfer_buffer_length =
681 FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
682
683 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
684 urb->iso_frame_desc[fx].offset =
685 FRAME_SIZE_PER_DESC * fx;
686 urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
687 }
688 }
689
690
691 /* Queue the ISO urbs, and resubmit in the completion handler */
692 for(i=0; i<NUM_SBUF; ++i) {
693 err = usb_submit_urb(cam->sbuf[i].urb, GFP_KERNEL);
694 if (err) {
695 ERR("usb_submit_urb[%d]() = %d\n", i, err);
696 return err;
697 }
698 }
699
700 return 0;
701}
702
703/******************************************************************************
704 *
705 * cpia2_usb_stream_start
706 *
707 *****************************************************************************/
708int cpia2_usb_stream_start(struct camera_data *cam, unsigned int alternate)
709{
710 int ret;
711 int old_alt;
712
713 if(cam->streaming)
714 return 0;
715
716 if (cam->flush) {
717 int i;
718 DBG("Flushing buffers\n");
719 for(i=0; i<cam->num_frames; ++i) {
720 cam->buffers[i].status = FRAME_EMPTY;
721 cam->buffers[i].length = 0;
722 }
723 cam->curbuff = &cam->buffers[0];
724 cam->workbuff = cam->curbuff->next;
725 cam->flush = false;
726 }
727
728 old_alt = cam->params.camera_state.stream_mode;
729 cam->params.camera_state.stream_mode = 0;
730 ret = cpia2_usb_change_streaming_alternate(cam, alternate);
731 if (ret < 0) {
732 int ret2;
733 ERR("cpia2_usb_change_streaming_alternate() = %d!\n", ret);
734 cam->params.camera_state.stream_mode = old_alt;
735 ret2 = set_alternate(cam, USBIF_CMDONLY);
736 if (ret2 < 0) {
737 ERR("cpia2_usb_change_streaming_alternate(%d) =%d has already "
738 "failed. Then tried to call "
739 "set_alternate(USBIF_CMDONLY) = %d.\n",
740 alternate, ret, ret2);
741 }
742 } else {
743 cam->frame_count = 0;
744 cam->streaming = 1;
745 ret = cpia2_usb_stream_resume(cam);
746 }
747 return ret;
748}
749
750/******************************************************************************
751 *
752 * cpia2_usb_stream_pause
753 *
754 *****************************************************************************/
755int cpia2_usb_stream_pause(struct camera_data *cam)
756{
757 int ret = 0;
758 if(cam->streaming) {
759 free_sbufs(cam);
760 ret = set_alternate(cam, USBIF_CMDONLY);
761 }
762 return ret;
763}
764
765/******************************************************************************
766 *
767 * cpia2_usb_stream_resume
768 *
769 *****************************************************************************/
770int cpia2_usb_stream_resume(struct camera_data *cam)
771{
772 int ret = 0;
773 if(cam->streaming) {
774 cam->first_image_seen = 0;
775 ret = set_alternate(cam, cam->params.camera_state.stream_mode);
776 if(ret == 0) {
777 /* for some reason the user effects need to be set
778 again when starting streaming. */
779 cpia2_do_command(cam, CPIA2_CMD_SET_USER_EFFECTS, TRANSFER_WRITE,
780 cam->params.vp_params.user_effects);
781 ret = submit_urbs(cam);
782 }
783 }
784 return ret;
785}
786
787/******************************************************************************
788 *
789 * cpia2_usb_stream_stop
790 *
791 *****************************************************************************/
792int cpia2_usb_stream_stop(struct camera_data *cam)
793{
794 int ret;
795
796 ret = cpia2_usb_stream_pause(cam);
797 cam->streaming = 0;
798 configure_transfer_mode(cam, 0);
799 return ret;
800}
801
802/******************************************************************************
803 *
804 * cpia2_usb_probe
805 *
806 * Probe and initialize.
807 *****************************************************************************/
808static int cpia2_usb_probe(struct usb_interface *intf,
809 const struct usb_device_id *id)
810{
811 struct usb_device *udev = interface_to_usbdev(intf);
812 struct usb_interface_descriptor *interface;
813 struct camera_data *cam;
814 int ret;
815
816 /* A multi-config CPiA2 camera? */
817 if (udev->descriptor.bNumConfigurations != 1)
818 return -ENODEV;
819 interface = &intf->cur_altsetting->desc;
820
821 /* If we get to this point, we found a CPiA2 camera */
822 LOG("CPiA2 USB camera found\n");
823
824 cam = cpia2_init_camera_struct(intf);
825 if (cam == NULL)
826 return -ENOMEM;
827
828 cam->dev = udev;
829 cam->iface = interface->bInterfaceNumber;
830
831 ret = set_alternate(cam, USBIF_CMDONLY);
832 if (ret < 0) {
833 ERR("%s: usb_set_interface error (ret = %d)\n", __func__, ret);
834 kfree(cam);
835 return ret;
836 }
837
838
839 if((ret = cpia2_init_camera(cam)) < 0) {
840 ERR("%s: failed to initialize cpia2 camera (ret = %d)\n", __func__, ret);
841 kfree(cam);
842 return ret;
843 }
844 LOG(" CPiA Version: %d.%02d (%d.%d)\n",
845 cam->params.version.firmware_revision_hi,
846 cam->params.version.firmware_revision_lo,
847 cam->params.version.asic_id,
848 cam->params.version.asic_rev);
849 LOG(" CPiA PnP-ID: %04x:%04x:%04x\n",
850 cam->params.pnp_id.vendor,
851 cam->params.pnp_id.product,
852 cam->params.pnp_id.device_revision);
853 LOG(" SensorID: %d.(version %d)\n",
854 cam->params.version.sensor_flags,
855 cam->params.version.sensor_rev);
856
857 usb_set_intfdata(intf, cam);
858
859 ret = cpia2_register_camera(cam);
860 if (ret < 0) {
861 ERR("%s: Failed to register cpia2 camera (ret = %d)\n", __func__, ret);
862 kfree(cam);
863 return ret;
864 }
865
866 return 0;
867}
868
869/******************************************************************************
870 *
871 * cpia2_disconnect
872 *
873 *****************************************************************************/
874static void cpia2_usb_disconnect(struct usb_interface *intf)
875{
876 struct camera_data *cam = usb_get_intfdata(intf);
877 usb_set_intfdata(intf, NULL);
878
879 DBG("Stopping stream\n");
880 cpia2_usb_stream_stop(cam);
881
882 mutex_lock(&cam->v4l2_lock);
883 DBG("Unregistering camera\n");
884 cpia2_unregister_camera(cam);
885 v4l2_device_disconnect(&cam->v4l2_dev);
886 mutex_unlock(&cam->v4l2_lock);
887 v4l2_device_put(&cam->v4l2_dev);
888
889 if(cam->buffers) {
890 DBG("Wakeup waiting processes\n");
891 cam->curbuff->status = FRAME_READY;
892 cam->curbuff->length = 0;
893 if (waitqueue_active(&cam->wq_stream))
894 wake_up_interruptible(&cam->wq_stream);
895 }
896
897 DBG("Releasing interface\n");
898 usb_driver_release_interface(&cpia2_driver, intf);
899
900 LOG("CPiA2 camera disconnected.\n");
901}
902
903static int cpia2_usb_suspend(struct usb_interface *intf, pm_message_t message)
904{
905 struct camera_data *cam = usb_get_intfdata(intf);
906
907 mutex_lock(&cam->v4l2_lock);
908 if (cam->streaming) {
909 cpia2_usb_stream_stop(cam);
910 cam->streaming = 1;
911 }
912 mutex_unlock(&cam->v4l2_lock);
913
914 dev_info(&intf->dev, "going into suspend..\n");
915 return 0;
916}
917
918/* Resume device - start device. */
919static int cpia2_usb_resume(struct usb_interface *intf)
920{
921 struct camera_data *cam = usb_get_intfdata(intf);
922
923 mutex_lock(&cam->v4l2_lock);
924 v4l2_ctrl_handler_setup(&cam->hdl);
925 if (cam->streaming) {
926 cam->streaming = 0;
927 cpia2_usb_stream_start(cam,
928 cam->params.camera_state.stream_mode);
929 }
930 mutex_unlock(&cam->v4l2_lock);
931
932 dev_info(&intf->dev, "coming out of suspend..\n");
933 return 0;
934}
935
936/******************************************************************************
937 *
938 * usb_cpia2_init
939 *
940 *****************************************************************************/
941int cpia2_usb_init(void)
942{
943 return usb_register(&cpia2_driver);
944}
945
946/******************************************************************************
947 *
948 * usb_cpia_cleanup
949 *
950 *****************************************************************************/
951void cpia2_usb_cleanup(void)
952{
953 schedule_timeout(2 * HZ);
954 usb_deregister(&cpia2_driver);
955}
diff --git a/drivers/media/video/cpia2/cpia2_v4l.c b/drivers/media/video/cpia2/cpia2_v4l.c
deleted file mode 100644
index 5ca6f44b4c63..000000000000
--- a/drivers/media/video/cpia2/cpia2_v4l.c
+++ /dev/null
@@ -1,1267 +0,0 @@
1/****************************************************************************
2 *
3 * Filename: cpia2_v4l.c
4 *
5 * Copyright 2001, STMicrolectronics, Inc.
6 * Contact: steve.miller@st.com
7 * Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com>
8 *
9 * Description:
10 * This is a USB driver for CPia2 based video cameras.
11 * The infrastructure of this driver is based on the cpia usb driver by
12 * Jochen Scharrlach and Johannes Erdfeldt.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 * Stripped of 2.4 stuff ready for main kernel submit by
29 * Alan Cox <alan@lxorguk.ukuu.org.uk>
30 ****************************************************************************/
31
32#define CPIA_VERSION "3.0.1"
33
34#include <linux/module.h>
35#include <linux/time.h>
36#include <linux/sched.h>
37#include <linux/slab.h>
38#include <linux/init.h>
39#include <linux/videodev2.h>
40#include <linux/stringify.h>
41#include <media/v4l2-ioctl.h>
42#include <media/v4l2-event.h>
43
44#include "cpia2.h"
45
46static int video_nr = -1;
47module_param(video_nr, int, 0);
48MODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)");
49
50static int buffer_size = 68 * 1024;
51module_param(buffer_size, int, 0);
52MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
53
54static int num_buffers = 3;
55module_param(num_buffers, int, 0);
56MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
57 __stringify(VIDEO_MAX_FRAME) ", default 3)");
58
59static int alternate = DEFAULT_ALT;
60module_param(alternate, int, 0);
61MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
62 __stringify(USBIF_ISO_6) ", default "
63 __stringify(DEFAULT_ALT) ")");
64
65static int flicker_mode;
66module_param(flicker_mode, int, 0);
67MODULE_PARM_DESC(flicker_mode, "Flicker frequency (0 (disabled), " __stringify(50) " or "
68 __stringify(60) ", default 0)");
69
70MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
71MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
72MODULE_SUPPORTED_DEVICE("video");
73MODULE_LICENSE("GPL");
74MODULE_VERSION(CPIA_VERSION);
75
76#define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
77#define CPIA2_CID_USB_ALT (V4L2_CID_USER_BASE | 0xf000)
78
79/******************************************************************************
80 *
81 * cpia2_open
82 *
83 *****************************************************************************/
84static int cpia2_open(struct file *file)
85{
86 struct camera_data *cam = video_drvdata(file);
87 int retval;
88
89 if (mutex_lock_interruptible(&cam->v4l2_lock))
90 return -ERESTARTSYS;
91 retval = v4l2_fh_open(file);
92 if (retval)
93 goto open_unlock;
94
95 if (v4l2_fh_is_singular_file(file)) {
96 if (cpia2_allocate_buffers(cam)) {
97 v4l2_fh_release(file);
98 retval = -ENOMEM;
99 goto open_unlock;
100 }
101
102 /* reset the camera */
103 if (cpia2_reset_camera(cam) < 0) {
104 v4l2_fh_release(file);
105 retval = -EIO;
106 goto open_unlock;
107 }
108
109 cam->APP_len = 0;
110 cam->COM_len = 0;
111 }
112
113 cpia2_dbg_dump_registers(cam);
114open_unlock:
115 mutex_unlock(&cam->v4l2_lock);
116 return retval;
117}
118
119/******************************************************************************
120 *
121 * cpia2_close
122 *
123 *****************************************************************************/
124static int cpia2_close(struct file *file)
125{
126 struct video_device *dev = video_devdata(file);
127 struct camera_data *cam = video_get_drvdata(dev);
128
129 mutex_lock(&cam->v4l2_lock);
130 if (video_is_registered(&cam->vdev) && v4l2_fh_is_singular_file(file)) {
131 cpia2_usb_stream_stop(cam);
132
133 /* save camera state for later open */
134 cpia2_save_camera_state(cam);
135
136 cpia2_set_low_power(cam);
137 cpia2_free_buffers(cam);
138 }
139
140 if (cam->stream_fh == file->private_data) {
141 cam->stream_fh = NULL;
142 cam->mmapped = 0;
143 }
144 mutex_unlock(&cam->v4l2_lock);
145 return v4l2_fh_release(file);
146}
147
148/******************************************************************************
149 *
150 * cpia2_v4l_read
151 *
152 *****************************************************************************/
153static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
154 loff_t *off)
155{
156 struct camera_data *cam = video_drvdata(file);
157 int noblock = file->f_flags&O_NONBLOCK;
158 ssize_t ret;
159
160 if(!cam)
161 return -EINVAL;
162
163 if (mutex_lock_interruptible(&cam->v4l2_lock))
164 return -ERESTARTSYS;
165 ret = cpia2_read(cam, buf, count, noblock);
166 mutex_unlock(&cam->v4l2_lock);
167 return ret;
168}
169
170
171/******************************************************************************
172 *
173 * cpia2_v4l_poll
174 *
175 *****************************************************************************/
176static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
177{
178 struct camera_data *cam = video_drvdata(filp);
179 unsigned int res;
180
181 mutex_lock(&cam->v4l2_lock);
182 res = cpia2_poll(cam, filp, wait);
183 mutex_unlock(&cam->v4l2_lock);
184 return res;
185}
186
187
188static int sync(struct camera_data *cam, int frame_nr)
189{
190 struct framebuf *frame = &cam->buffers[frame_nr];
191
192 while (1) {
193 if (frame->status == FRAME_READY)
194 return 0;
195
196 if (!cam->streaming) {
197 frame->status = FRAME_READY;
198 frame->length = 0;
199 return 0;
200 }
201
202 mutex_unlock(&cam->v4l2_lock);
203 wait_event_interruptible(cam->wq_stream,
204 !cam->streaming ||
205 frame->status == FRAME_READY);
206 mutex_lock(&cam->v4l2_lock);
207 if (signal_pending(current))
208 return -ERESTARTSYS;
209 if (!video_is_registered(&cam->vdev))
210 return -ENOTTY;
211 }
212}
213
214/******************************************************************************
215 *
216 * ioctl_querycap
217 *
218 * V4L2 device capabilities
219 *
220 *****************************************************************************/
221
222static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc)
223{
224 struct camera_data *cam = video_drvdata(file);
225
226 strcpy(vc->driver, "cpia2");
227
228 if (cam->params.pnp_id.product == 0x151)
229 strcpy(vc->card, "QX5 Microscope");
230 else
231 strcpy(vc->card, "CPiA2 Camera");
232 switch (cam->params.pnp_id.device_type) {
233 case DEVICE_STV_672:
234 strcat(vc->card, " (672/");
235 break;
236 case DEVICE_STV_676:
237 strcat(vc->card, " (676/");
238 break;
239 default:
240 strcat(vc->card, " (XXX/");
241 break;
242 }
243 switch (cam->params.version.sensor_flags) {
244 case CPIA2_VP_SENSOR_FLAGS_404:
245 strcat(vc->card, "404)");
246 break;
247 case CPIA2_VP_SENSOR_FLAGS_407:
248 strcat(vc->card, "407)");
249 break;
250 case CPIA2_VP_SENSOR_FLAGS_409:
251 strcat(vc->card, "409)");
252 break;
253 case CPIA2_VP_SENSOR_FLAGS_410:
254 strcat(vc->card, "410)");
255 break;
256 case CPIA2_VP_SENSOR_FLAGS_500:
257 strcat(vc->card, "500)");
258 break;
259 default:
260 strcat(vc->card, "XXX)");
261 break;
262 }
263
264 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
265 memset(vc->bus_info,0, sizeof(vc->bus_info));
266
267 vc->device_caps = V4L2_CAP_VIDEO_CAPTURE |
268 V4L2_CAP_READWRITE |
269 V4L2_CAP_STREAMING;
270 vc->capabilities = vc->device_caps |
271 V4L2_CAP_DEVICE_CAPS;
272
273 return 0;
274}
275
276/******************************************************************************
277 *
278 * ioctl_input
279 *
280 * V4L2 input get/set/enumerate
281 *
282 *****************************************************************************/
283
284static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i)
285{
286 if (i->index)
287 return -EINVAL;
288 strcpy(i->name, "Camera");
289 i->type = V4L2_INPUT_TYPE_CAMERA;
290 return 0;
291}
292
293static int cpia2_g_input(struct file *file, void *fh, unsigned int *i)
294{
295 *i = 0;
296 return 0;
297}
298
299static int cpia2_s_input(struct file *file, void *fh, unsigned int i)
300{
301 return i ? -EINVAL : 0;
302}
303
304/******************************************************************************
305 *
306 * ioctl_enum_fmt
307 *
308 * V4L2 format enumerate
309 *
310 *****************************************************************************/
311
312static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh,
313 struct v4l2_fmtdesc *f)
314{
315 int index = f->index;
316
317 if (index < 0 || index > 1)
318 return -EINVAL;
319
320 memset(f, 0, sizeof(*f));
321 f->index = index;
322 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
323 f->flags = V4L2_FMT_FLAG_COMPRESSED;
324 switch(index) {
325 case 0:
326 strcpy(f->description, "MJPEG");
327 f->pixelformat = V4L2_PIX_FMT_MJPEG;
328 break;
329 case 1:
330 strcpy(f->description, "JPEG");
331 f->pixelformat = V4L2_PIX_FMT_JPEG;
332 break;
333 default:
334 return -EINVAL;
335 }
336
337 return 0;
338}
339
340/******************************************************************************
341 *
342 * ioctl_try_fmt
343 *
344 * V4L2 format try
345 *
346 *****************************************************************************/
347
348static int cpia2_try_fmt_vid_cap(struct file *file, void *fh,
349 struct v4l2_format *f)
350{
351 struct camera_data *cam = video_drvdata(file);
352
353 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
354 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
355 return -EINVAL;
356
357 f->fmt.pix.field = V4L2_FIELD_NONE;
358 f->fmt.pix.bytesperline = 0;
359 f->fmt.pix.sizeimage = cam->frame_size;
360 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
361 f->fmt.pix.priv = 0;
362
363 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
364 case VIDEOSIZE_VGA:
365 f->fmt.pix.width = 640;
366 f->fmt.pix.height = 480;
367 break;
368 case VIDEOSIZE_CIF:
369 f->fmt.pix.width = 352;
370 f->fmt.pix.height = 288;
371 break;
372 case VIDEOSIZE_QVGA:
373 f->fmt.pix.width = 320;
374 f->fmt.pix.height = 240;
375 break;
376 case VIDEOSIZE_288_216:
377 f->fmt.pix.width = 288;
378 f->fmt.pix.height = 216;
379 break;
380 case VIDEOSIZE_256_192:
381 f->fmt.pix.width = 256;
382 f->fmt.pix.height = 192;
383 break;
384 case VIDEOSIZE_224_168:
385 f->fmt.pix.width = 224;
386 f->fmt.pix.height = 168;
387 break;
388 case VIDEOSIZE_192_144:
389 f->fmt.pix.width = 192;
390 f->fmt.pix.height = 144;
391 break;
392 case VIDEOSIZE_QCIF:
393 default:
394 f->fmt.pix.width = 176;
395 f->fmt.pix.height = 144;
396 break;
397 }
398
399 return 0;
400}
401
402/******************************************************************************
403 *
404 * ioctl_set_fmt
405 *
406 * V4L2 format set
407 *
408 *****************************************************************************/
409
410static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh,
411 struct v4l2_format *f)
412{
413 struct camera_data *cam = video_drvdata(file);
414 int err, frame;
415
416 err = cpia2_try_fmt_vid_cap(file, _fh, f);
417 if(err != 0)
418 return err;
419
420 cam->pixelformat = f->fmt.pix.pixelformat;
421
422 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
423 * the missing Huffman table properly. */
424 cam->params.compression.inhibit_htables = 0;
425 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
426
427 /* we set the video window to something smaller or equal to what
428 * is requested by the user???
429 */
430 DBG("Requested width = %d, height = %d\n",
431 f->fmt.pix.width, f->fmt.pix.height);
432 if (f->fmt.pix.width != cam->width ||
433 f->fmt.pix.height != cam->height) {
434 cam->width = f->fmt.pix.width;
435 cam->height = f->fmt.pix.height;
436 cam->params.roi.width = f->fmt.pix.width;
437 cam->params.roi.height = f->fmt.pix.height;
438 cpia2_set_format(cam);
439 }
440
441 for (frame = 0; frame < cam->num_frames; ++frame) {
442 if (cam->buffers[frame].status == FRAME_READING)
443 if ((err = sync(cam, frame)) < 0)
444 return err;
445
446 cam->buffers[frame].status = FRAME_EMPTY;
447 }
448
449 return 0;
450}
451
452/******************************************************************************
453 *
454 * ioctl_get_fmt
455 *
456 * V4L2 format get
457 *
458 *****************************************************************************/
459
460static int cpia2_g_fmt_vid_cap(struct file *file, void *fh,
461 struct v4l2_format *f)
462{
463 struct camera_data *cam = video_drvdata(file);
464
465 f->fmt.pix.width = cam->width;
466 f->fmt.pix.height = cam->height;
467 f->fmt.pix.pixelformat = cam->pixelformat;
468 f->fmt.pix.field = V4L2_FIELD_NONE;
469 f->fmt.pix.bytesperline = 0;
470 f->fmt.pix.sizeimage = cam->frame_size;
471 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
472 f->fmt.pix.priv = 0;
473
474 return 0;
475}
476
477/******************************************************************************
478 *
479 * ioctl_cropcap
480 *
481 * V4L2 query cropping capabilities
482 * NOTE: cropping is currently disabled
483 *
484 *****************************************************************************/
485
486static int cpia2_cropcap(struct file *file, void *fh, struct v4l2_cropcap *c)
487{
488 struct camera_data *cam = video_drvdata(file);
489
490 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
491 return -EINVAL;
492
493 c->bounds.left = 0;
494 c->bounds.top = 0;
495 c->bounds.width = cam->width;
496 c->bounds.height = cam->height;
497 c->defrect.left = 0;
498 c->defrect.top = 0;
499 c->defrect.width = cam->width;
500 c->defrect.height = cam->height;
501 c->pixelaspect.numerator = 1;
502 c->pixelaspect.denominator = 1;
503
504 return 0;
505}
506
507struct framerate_info {
508 int value;
509 struct v4l2_fract period;
510};
511
512static const struct framerate_info framerate_controls[] = {
513 { CPIA2_VP_FRAMERATE_6_25, { 4, 25 } },
514 { CPIA2_VP_FRAMERATE_7_5, { 2, 15 } },
515 { CPIA2_VP_FRAMERATE_12_5, { 2, 25 } },
516 { CPIA2_VP_FRAMERATE_15, { 1, 15 } },
517 { CPIA2_VP_FRAMERATE_25, { 1, 25 } },
518 { CPIA2_VP_FRAMERATE_30, { 1, 30 } },
519};
520
521static int cpia2_g_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
522{
523 struct camera_data *cam = video_drvdata(file);
524 struct v4l2_captureparm *cap = &p->parm.capture;
525 int i;
526
527 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
528 return -EINVAL;
529
530 cap->capability = V4L2_CAP_TIMEPERFRAME;
531 cap->readbuffers = cam->num_frames;
532 for (i = 0; i < ARRAY_SIZE(framerate_controls); i++)
533 if (cam->params.vp_params.frame_rate == framerate_controls[i].value) {
534 cap->timeperframe = framerate_controls[i].period;
535 break;
536 }
537 return 0;
538}
539
540static int cpia2_s_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
541{
542 struct camera_data *cam = video_drvdata(file);
543 struct v4l2_captureparm *cap = &p->parm.capture;
544 struct v4l2_fract tpf = cap->timeperframe;
545 int max = ARRAY_SIZE(framerate_controls) - 1;
546 int ret;
547 int i;
548
549 ret = cpia2_g_parm(file, fh, p);
550 if (ret || !tpf.denominator || !tpf.numerator)
551 return ret;
552
553 /* Maximum 15 fps for this model */
554 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
555 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
556 max -= 2;
557 for (i = 0; i <= max; i++) {
558 struct v4l2_fract f1 = tpf;
559 struct v4l2_fract f2 = framerate_controls[i].period;
560
561 f1.numerator *= f2.denominator;
562 f2.numerator *= f1.denominator;
563 if (f1.numerator >= f2.numerator)
564 break;
565 }
566 if (i > max)
567 i = max;
568 cap->timeperframe = framerate_controls[i].period;
569 return cpia2_set_fps(cam, framerate_controls[i].value);
570}
571
572static const struct {
573 u32 width;
574 u32 height;
575} cpia2_framesizes[] = {
576 { 640, 480 },
577 { 352, 288 },
578 { 320, 240 },
579 { 288, 216 },
580 { 256, 192 },
581 { 224, 168 },
582 { 192, 144 },
583 { 176, 144 },
584};
585
586static int cpia2_enum_framesizes(struct file *file, void *fh,
587 struct v4l2_frmsizeenum *fsize)
588{
589
590 if (fsize->pixel_format != V4L2_PIX_FMT_MJPEG &&
591 fsize->pixel_format != V4L2_PIX_FMT_JPEG)
592 return -EINVAL;
593 if (fsize->index >= ARRAY_SIZE(cpia2_framesizes))
594 return -EINVAL;
595 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
596 fsize->discrete.width = cpia2_framesizes[fsize->index].width;
597 fsize->discrete.height = cpia2_framesizes[fsize->index].height;
598
599 return 0;
600}
601
602static int cpia2_enum_frameintervals(struct file *file, void *fh,
603 struct v4l2_frmivalenum *fival)
604{
605 struct camera_data *cam = video_drvdata(file);
606 int max = ARRAY_SIZE(framerate_controls) - 1;
607 int i;
608
609 if (fival->pixel_format != V4L2_PIX_FMT_MJPEG &&
610 fival->pixel_format != V4L2_PIX_FMT_JPEG)
611 return -EINVAL;
612
613 /* Maximum 15 fps for this model */
614 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
615 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
616 max -= 2;
617 if (fival->index > max)
618 return -EINVAL;
619 for (i = 0; i < ARRAY_SIZE(cpia2_framesizes); i++)
620 if (fival->width == cpia2_framesizes[i].width &&
621 fival->height == cpia2_framesizes[i].height)
622 break;
623 if (i == ARRAY_SIZE(cpia2_framesizes))
624 return -EINVAL;
625 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
626 fival->discrete = framerate_controls[fival->index].period;
627 return 0;
628}
629
630/******************************************************************************
631 *
632 * ioctl_s_ctrl
633 *
634 * V4L2 set the value of a control variable
635 *
636 *****************************************************************************/
637
638static int cpia2_s_ctrl(struct v4l2_ctrl *ctrl)
639{
640 struct camera_data *cam =
641 container_of(ctrl->handler, struct camera_data, hdl);
642 static const int flicker_table[] = {
643 NEVER_FLICKER,
644 FLICKER_50,
645 FLICKER_60,
646 };
647
648 DBG("Set control id:%d, value:%d\n", ctrl->id, ctrl->val);
649
650 switch (ctrl->id) {
651 case V4L2_CID_BRIGHTNESS:
652 cpia2_set_brightness(cam, ctrl->val);
653 break;
654 case V4L2_CID_CONTRAST:
655 cpia2_set_contrast(cam, ctrl->val);
656 break;
657 case V4L2_CID_SATURATION:
658 cpia2_set_saturation(cam, ctrl->val);
659 break;
660 case V4L2_CID_HFLIP:
661 cpia2_set_property_mirror(cam, ctrl->val);
662 break;
663 case V4L2_CID_VFLIP:
664 cpia2_set_property_flip(cam, ctrl->val);
665 break;
666 case V4L2_CID_POWER_LINE_FREQUENCY:
667 return cpia2_set_flicker_mode(cam, flicker_table[ctrl->val]);
668 case V4L2_CID_ILLUMINATORS_1:
669 return cpia2_set_gpio(cam, (cam->top_light->val << 6) |
670 (cam->bottom_light->val << 7));
671 case V4L2_CID_JPEG_ACTIVE_MARKER:
672 cam->params.compression.inhibit_htables =
673 !(ctrl->val & V4L2_JPEG_ACTIVE_MARKER_DHT);
674 break;
675 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
676 cam->params.vc_params.quality = ctrl->val;
677 break;
678 case CPIA2_CID_USB_ALT:
679 cam->params.camera_state.stream_mode = ctrl->val;
680 break;
681 default:
682 return -EINVAL;
683 }
684
685 return 0;
686}
687
688/******************************************************************************
689 *
690 * ioctl_g_jpegcomp
691 *
692 * V4L2 get the JPEG compression parameters
693 *
694 *****************************************************************************/
695
696static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
697{
698 struct camera_data *cam = video_drvdata(file);
699
700 memset(parms, 0, sizeof(*parms));
701
702 parms->quality = 80; // TODO: Can this be made meaningful?
703
704 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
705 if(!cam->params.compression.inhibit_htables) {
706 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
707 }
708
709 parms->APPn = cam->APPn;
710 parms->APP_len = cam->APP_len;
711 if(cam->APP_len > 0) {
712 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
713 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
714 }
715
716 parms->COM_len = cam->COM_len;
717 if(cam->COM_len > 0) {
718 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
719 parms->jpeg_markers |= JPEG_MARKER_COM;
720 }
721
722 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
723 parms->APP_len, parms->COM_len);
724
725 return 0;
726}
727
728/******************************************************************************
729 *
730 * ioctl_s_jpegcomp
731 *
732 * V4L2 set the JPEG compression parameters
733 * NOTE: quality and some jpeg_markers are ignored.
734 *
735 *****************************************************************************/
736
737static int cpia2_s_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
738{
739 struct camera_data *cam = video_drvdata(file);
740
741 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
742 parms->APP_len, parms->COM_len);
743
744 cam->params.compression.inhibit_htables =
745 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
746 parms->jpeg_markers &= V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI |
747 V4L2_JPEG_MARKER_DHT;
748
749 if(parms->APP_len != 0) {
750 if(parms->APP_len > 0 &&
751 parms->APP_len <= sizeof(cam->APP_data) &&
752 parms->APPn >= 0 && parms->APPn <= 15) {
753 cam->APPn = parms->APPn;
754 cam->APP_len = parms->APP_len;
755 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
756 } else {
757 LOG("Bad APPn Params n=%d len=%d\n",
758 parms->APPn, parms->APP_len);
759 return -EINVAL;
760 }
761 } else {
762 cam->APP_len = 0;
763 }
764
765 if(parms->COM_len != 0) {
766 if(parms->COM_len > 0 &&
767 parms->COM_len <= sizeof(cam->COM_data)) {
768 cam->COM_len = parms->COM_len;
769 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
770 } else {
771 LOG("Bad COM_len=%d\n", parms->COM_len);
772 return -EINVAL;
773 }
774 }
775
776 return 0;
777}
778
779/******************************************************************************
780 *
781 * ioctl_reqbufs
782 *
783 * V4L2 Initiate memory mapping.
784 * NOTE: The user's request is ignored. For now the buffers are fixed.
785 *
786 *****************************************************************************/
787
788static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req)
789{
790 struct camera_data *cam = video_drvdata(file);
791
792 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
793 req->memory != V4L2_MEMORY_MMAP)
794 return -EINVAL;
795
796 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
797 req->count = cam->num_frames;
798 memset(&req->reserved, 0, sizeof(req->reserved));
799
800 return 0;
801}
802
803/******************************************************************************
804 *
805 * ioctl_querybuf
806 *
807 * V4L2 Query memory buffer status.
808 *
809 *****************************************************************************/
810
811static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
812{
813 struct camera_data *cam = video_drvdata(file);
814
815 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
816 buf->index > cam->num_frames)
817 return -EINVAL;
818
819 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
820 buf->length = cam->frame_size;
821
822 buf->memory = V4L2_MEMORY_MMAP;
823
824 if(cam->mmapped)
825 buf->flags = V4L2_BUF_FLAG_MAPPED;
826 else
827 buf->flags = 0;
828
829 switch (cam->buffers[buf->index].status) {
830 case FRAME_EMPTY:
831 case FRAME_ERROR:
832 case FRAME_READING:
833 buf->bytesused = 0;
834 buf->flags = V4L2_BUF_FLAG_QUEUED;
835 break;
836 case FRAME_READY:
837 buf->bytesused = cam->buffers[buf->index].length;
838 buf->timestamp = cam->buffers[buf->index].timestamp;
839 buf->sequence = cam->buffers[buf->index].seq;
840 buf->flags = V4L2_BUF_FLAG_DONE;
841 break;
842 }
843
844 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
845 buf->index, buf->m.offset, buf->flags, buf->sequence,
846 buf->bytesused);
847
848 return 0;
849}
850
851/******************************************************************************
852 *
853 * ioctl_qbuf
854 *
855 * V4L2 User is freeing buffer
856 *
857 *****************************************************************************/
858
859static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
860{
861 struct camera_data *cam = video_drvdata(file);
862
863 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
864 buf->memory != V4L2_MEMORY_MMAP ||
865 buf->index > cam->num_frames)
866 return -EINVAL;
867
868 DBG("QBUF #%d\n", buf->index);
869
870 if(cam->buffers[buf->index].status == FRAME_READY)
871 cam->buffers[buf->index].status = FRAME_EMPTY;
872
873 return 0;
874}
875
876/******************************************************************************
877 *
878 * find_earliest_filled_buffer
879 *
880 * Helper for ioctl_dqbuf. Find the next ready buffer.
881 *
882 *****************************************************************************/
883
884static int find_earliest_filled_buffer(struct camera_data *cam)
885{
886 int i;
887 int found = -1;
888 for (i=0; i<cam->num_frames; i++) {
889 if(cam->buffers[i].status == FRAME_READY) {
890 if(found < 0) {
891 found = i;
892 } else {
893 /* find which buffer is earlier */
894 struct timeval *tv1, *tv2;
895 tv1 = &cam->buffers[i].timestamp;
896 tv2 = &cam->buffers[found].timestamp;
897 if(tv1->tv_sec < tv2->tv_sec ||
898 (tv1->tv_sec == tv2->tv_sec &&
899 tv1->tv_usec < tv2->tv_usec))
900 found = i;
901 }
902 }
903 }
904 return found;
905}
906
907/******************************************************************************
908 *
909 * ioctl_dqbuf
910 *
911 * V4L2 User is asking for a filled buffer.
912 *
913 *****************************************************************************/
914
915static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
916{
917 struct camera_data *cam = video_drvdata(file);
918 int frame;
919
920 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
921 buf->memory != V4L2_MEMORY_MMAP)
922 return -EINVAL;
923
924 frame = find_earliest_filled_buffer(cam);
925
926 if(frame < 0 && file->f_flags&O_NONBLOCK)
927 return -EAGAIN;
928
929 if(frame < 0) {
930 /* Wait for a frame to become available */
931 struct framebuf *cb=cam->curbuff;
932 mutex_unlock(&cam->v4l2_lock);
933 wait_event_interruptible(cam->wq_stream,
934 !video_is_registered(&cam->vdev) ||
935 (cb=cam->curbuff)->status == FRAME_READY);
936 mutex_lock(&cam->v4l2_lock);
937 if (signal_pending(current))
938 return -ERESTARTSYS;
939 if (!video_is_registered(&cam->vdev))
940 return -ENOTTY;
941 frame = cb->num;
942 }
943
944
945 buf->index = frame;
946 buf->bytesused = cam->buffers[buf->index].length;
947 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
948 buf->field = V4L2_FIELD_NONE;
949 buf->timestamp = cam->buffers[buf->index].timestamp;
950 buf->sequence = cam->buffers[buf->index].seq;
951 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
952 buf->length = cam->frame_size;
953 buf->reserved2 = 0;
954 buf->reserved = 0;
955 memset(&buf->timecode, 0, sizeof(buf->timecode));
956
957 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
958 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
959
960 return 0;
961}
962
963static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
964{
965 struct camera_data *cam = video_drvdata(file);
966 int ret = -EINVAL;
967
968 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
969 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
970 return -EINVAL;
971
972 if (!cam->streaming) {
973 ret = cpia2_usb_stream_start(cam,
974 cam->params.camera_state.stream_mode);
975 if (!ret)
976 v4l2_ctrl_grab(cam->usb_alt, true);
977 }
978 return ret;
979}
980
981static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
982{
983 struct camera_data *cam = video_drvdata(file);
984 int ret = -EINVAL;
985
986 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
987 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
988 return -EINVAL;
989
990 if (cam->streaming) {
991 ret = cpia2_usb_stream_stop(cam);
992 if (!ret)
993 v4l2_ctrl_grab(cam->usb_alt, false);
994 }
995 return ret;
996}
997
998/******************************************************************************
999 *
1000 * cpia2_mmap
1001 *
1002 *****************************************************************************/
1003static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1004{
1005 struct camera_data *cam = video_drvdata(file);
1006 int retval;
1007
1008 if (mutex_lock_interruptible(&cam->v4l2_lock))
1009 return -ERESTARTSYS;
1010 retval = cpia2_remap_buffer(cam, area);
1011
1012 if(!retval)
1013 cam->stream_fh = file->private_data;
1014 mutex_unlock(&cam->v4l2_lock);
1015 return retval;
1016}
1017
1018/******************************************************************************
1019 *
1020 * reset_camera_struct_v4l
1021 *
1022 * Sets all values to the defaults
1023 *****************************************************************************/
1024static void reset_camera_struct_v4l(struct camera_data *cam)
1025{
1026 cam->width = cam->params.roi.width;
1027 cam->height = cam->params.roi.height;
1028
1029 cam->frame_size = buffer_size;
1030 cam->num_frames = num_buffers;
1031
1032 /* Flicker modes */
1033 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1034
1035 /* stream modes */
1036 cam->params.camera_state.stream_mode = alternate;
1037
1038 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1039}
1040
1041static const struct v4l2_ioctl_ops cpia2_ioctl_ops = {
1042 .vidioc_querycap = cpia2_querycap,
1043 .vidioc_enum_input = cpia2_enum_input,
1044 .vidioc_g_input = cpia2_g_input,
1045 .vidioc_s_input = cpia2_s_input,
1046 .vidioc_enum_fmt_vid_cap = cpia2_enum_fmt_vid_cap,
1047 .vidioc_g_fmt_vid_cap = cpia2_g_fmt_vid_cap,
1048 .vidioc_s_fmt_vid_cap = cpia2_s_fmt_vid_cap,
1049 .vidioc_try_fmt_vid_cap = cpia2_try_fmt_vid_cap,
1050 .vidioc_g_jpegcomp = cpia2_g_jpegcomp,
1051 .vidioc_s_jpegcomp = cpia2_s_jpegcomp,
1052 .vidioc_cropcap = cpia2_cropcap,
1053 .vidioc_reqbufs = cpia2_reqbufs,
1054 .vidioc_querybuf = cpia2_querybuf,
1055 .vidioc_qbuf = cpia2_qbuf,
1056 .vidioc_dqbuf = cpia2_dqbuf,
1057 .vidioc_streamon = cpia2_streamon,
1058 .vidioc_streamoff = cpia2_streamoff,
1059 .vidioc_s_parm = cpia2_s_parm,
1060 .vidioc_g_parm = cpia2_g_parm,
1061 .vidioc_enum_framesizes = cpia2_enum_framesizes,
1062 .vidioc_enum_frameintervals = cpia2_enum_frameintervals,
1063 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1064 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1065};
1066
1067/***
1068 * The v4l video device structure initialized for this device
1069 ***/
1070static const struct v4l2_file_operations cpia2_fops = {
1071 .owner = THIS_MODULE,
1072 .open = cpia2_open,
1073 .release = cpia2_close,
1074 .read = cpia2_v4l_read,
1075 .poll = cpia2_v4l_poll,
1076 .unlocked_ioctl = video_ioctl2,
1077 .mmap = cpia2_mmap,
1078};
1079
1080static struct video_device cpia2_template = {
1081 /* I could not find any place for the old .initialize initializer?? */
1082 .name = "CPiA2 Camera",
1083 .fops = &cpia2_fops,
1084 .ioctl_ops = &cpia2_ioctl_ops,
1085 .release = video_device_release_empty,
1086};
1087
1088void cpia2_camera_release(struct v4l2_device *v4l2_dev)
1089{
1090 struct camera_data *cam =
1091 container_of(v4l2_dev, struct camera_data, v4l2_dev);
1092
1093 v4l2_ctrl_handler_free(&cam->hdl);
1094 v4l2_device_unregister(&cam->v4l2_dev);
1095 kfree(cam);
1096}
1097
1098static const struct v4l2_ctrl_ops cpia2_ctrl_ops = {
1099 .s_ctrl = cpia2_s_ctrl,
1100};
1101
1102/******************************************************************************
1103 *
1104 * cpia2_register_camera
1105 *
1106 *****************************************************************************/
1107int cpia2_register_camera(struct camera_data *cam)
1108{
1109 struct v4l2_ctrl_handler *hdl = &cam->hdl;
1110 struct v4l2_ctrl_config cpia2_usb_alt = {
1111 .ops = &cpia2_ctrl_ops,
1112 .id = CPIA2_CID_USB_ALT,
1113 .name = "USB Alternate",
1114 .type = V4L2_CTRL_TYPE_INTEGER,
1115 .min = USBIF_ISO_1,
1116 .max = USBIF_ISO_6,
1117 .step = 1,
1118 };
1119 int ret;
1120
1121 v4l2_ctrl_handler_init(hdl, 12);
1122 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1123 V4L2_CID_BRIGHTNESS,
1124 cam->params.pnp_id.device_type == DEVICE_STV_672 ? 1 : 0,
1125 255, 1, DEFAULT_BRIGHTNESS);
1126 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1127 V4L2_CID_CONTRAST, 0, 255, 1, DEFAULT_CONTRAST);
1128 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1129 V4L2_CID_SATURATION, 0, 255, 1, DEFAULT_SATURATION);
1130 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1131 V4L2_CID_HFLIP, 0, 1, 1, 0);
1132 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1133 V4L2_CID_JPEG_ACTIVE_MARKER, 0,
1134 V4L2_JPEG_ACTIVE_MARKER_DHT, 0,
1135 V4L2_JPEG_ACTIVE_MARKER_DHT);
1136 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1137 V4L2_CID_JPEG_COMPRESSION_QUALITY, 1,
1138 100, 1, 100);
1139 cpia2_usb_alt.def = alternate;
1140 cam->usb_alt = v4l2_ctrl_new_custom(hdl, &cpia2_usb_alt, NULL);
1141 /* VP5 Only */
1142 if (cam->params.pnp_id.device_type != DEVICE_STV_672)
1143 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1144 V4L2_CID_VFLIP, 0, 1, 1, 0);
1145 /* Flicker control only valid for 672 */
1146 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1147 v4l2_ctrl_new_std_menu(hdl, &cpia2_ctrl_ops,
1148 V4L2_CID_POWER_LINE_FREQUENCY,
1149 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
1150 /* Light control only valid for the QX5 Microscope */
1151 if (cam->params.pnp_id.product == 0x151) {
1152 cam->top_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1153 V4L2_CID_ILLUMINATORS_1, 0, 1, 1, 0);
1154 cam->bottom_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1155 V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
1156 v4l2_ctrl_cluster(2, &cam->top_light);
1157 }
1158
1159 if (hdl->error) {
1160 ret = hdl->error;
1161 v4l2_ctrl_handler_free(hdl);
1162 return ret;
1163 }
1164
1165 cam->vdev = cpia2_template;
1166 video_set_drvdata(&cam->vdev, cam);
1167 cam->vdev.lock = &cam->v4l2_lock;
1168 cam->vdev.ctrl_handler = hdl;
1169 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1170 set_bit(V4L2_FL_USE_FH_PRIO, &cam->vdev.flags);
1171
1172 reset_camera_struct_v4l(cam);
1173
1174 /* register v4l device */
1175 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1176 ERR("video_register_device failed\n");
1177 return -ENODEV;
1178 }
1179
1180 return 0;
1181}
1182
1183/******************************************************************************
1184 *
1185 * cpia2_unregister_camera
1186 *
1187 *****************************************************************************/
1188void cpia2_unregister_camera(struct camera_data *cam)
1189{
1190 video_unregister_device(&cam->vdev);
1191}
1192
1193/******************************************************************************
1194 *
1195 * check_parameters
1196 *
1197 * Make sure that all user-supplied parameters are sensible
1198 *****************************************************************************/
1199static void __init check_parameters(void)
1200{
1201 if(buffer_size < PAGE_SIZE) {
1202 buffer_size = PAGE_SIZE;
1203 LOG("buffer_size too small, setting to %d\n", buffer_size);
1204 } else if(buffer_size > 1024*1024) {
1205 /* arbitrary upper limiit */
1206 buffer_size = 1024*1024;
1207 LOG("buffer_size ridiculously large, setting to %d\n",
1208 buffer_size);
1209 } else {
1210 buffer_size += PAGE_SIZE-1;
1211 buffer_size &= ~(PAGE_SIZE-1);
1212 }
1213
1214 if(num_buffers < 1) {
1215 num_buffers = 1;
1216 LOG("num_buffers too small, setting to %d\n", num_buffers);
1217 } else if(num_buffers > VIDEO_MAX_FRAME) {
1218 num_buffers = VIDEO_MAX_FRAME;
1219 LOG("num_buffers too large, setting to %d\n", num_buffers);
1220 }
1221
1222 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1223 alternate = DEFAULT_ALT;
1224 LOG("alternate specified is invalid, using %d\n", alternate);
1225 }
1226
1227 if (flicker_mode != 0 && flicker_mode != FLICKER_50 && flicker_mode != FLICKER_60) {
1228 flicker_mode = 0;
1229 LOG("Flicker mode specified is invalid, using %d\n",
1230 flicker_mode);
1231 }
1232
1233 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1234 num_buffers, buffer_size, alternate);
1235}
1236
1237/************ Module Stuff ***************/
1238
1239
1240/******************************************************************************
1241 *
1242 * cpia2_init/module_init
1243 *
1244 *****************************************************************************/
1245static int __init cpia2_init(void)
1246{
1247 LOG("%s v%s\n",
1248 ABOUT, CPIA_VERSION);
1249 check_parameters();
1250 cpia2_usb_init();
1251 return 0;
1252}
1253
1254
1255/******************************************************************************
1256 *
1257 * cpia2_exit/module_exit
1258 *
1259 *****************************************************************************/
1260static void __exit cpia2_exit(void)
1261{
1262 cpia2_usb_cleanup();
1263 schedule_timeout(2 * HZ);
1264}
1265
1266module_init(cpia2_init);
1267module_exit(cpia2_exit);