diff options
Diffstat (limited to 'drivers/media/usb/sn9c102')
19 files changed, 8132 insertions, 0 deletions
diff --git a/drivers/media/usb/sn9c102/Kconfig b/drivers/media/usb/sn9c102/Kconfig new file mode 100644 index 000000000000..6ebaf2940d06 --- /dev/null +++ b/drivers/media/usb/sn9c102/Kconfig | |||
@@ -0,0 +1,14 @@ | |||
1 | config USB_SN9C102 | ||
2 | tristate "USB SN9C1xx PC Camera Controller support (DEPRECATED)" | ||
3 | depends on VIDEO_V4L2 | ||
4 | ---help--- | ||
5 | This driver is DEPRECATED please use the gspca sonixb and | ||
6 | sonixj modules instead. | ||
7 | |||
8 | Say Y here if you want support for cameras based on SONiX SN9C101, | ||
9 | SN9C102, SN9C103, SN9C105 and SN9C120 PC Camera Controllers. | ||
10 | |||
11 | See <file:Documentation/video4linux/sn9c102.txt> for more info. | ||
12 | |||
13 | To compile this driver as a module, choose M here: the | ||
14 | module will be called sn9c102. | ||
diff --git a/drivers/media/usb/sn9c102/Makefile b/drivers/media/usb/sn9c102/Makefile new file mode 100644 index 000000000000..7ecd5a90c7c9 --- /dev/null +++ b/drivers/media/usb/sn9c102/Makefile | |||
@@ -0,0 +1,15 @@ | |||
1 | sn9c102-objs := sn9c102_core.o \ | ||
2 | sn9c102_hv7131d.o \ | ||
3 | sn9c102_hv7131r.o \ | ||
4 | sn9c102_mi0343.o \ | ||
5 | sn9c102_mi0360.o \ | ||
6 | sn9c102_mt9v111.o \ | ||
7 | sn9c102_ov7630.o \ | ||
8 | sn9c102_ov7660.o \ | ||
9 | sn9c102_pas106b.o \ | ||
10 | sn9c102_pas202bcb.o \ | ||
11 | sn9c102_tas5110c1b.o \ | ||
12 | sn9c102_tas5110d.o \ | ||
13 | sn9c102_tas5130d1b.o | ||
14 | |||
15 | obj-$(CONFIG_USB_SN9C102) += sn9c102.o | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102.h b/drivers/media/usb/sn9c102/sn9c102.h new file mode 100644 index 000000000000..2bc153e869be --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102.h | |||
@@ -0,0 +1,211 @@ | |||
1 | /*************************************************************************** | ||
2 | * V4L2 driver for SN9C1xx PC Camera Controllers * | ||
3 | * * | ||
4 | * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License as published by * | ||
8 | * the Free Software Foundation; either version 2 of the License, or * | ||
9 | * (at your option) any later version. * | ||
10 | * * | ||
11 | * This program is distributed in the hope that it will be useful, * | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
14 | * GNU General Public License for more details. * | ||
15 | * * | ||
16 | * You should have received a copy of the GNU General Public License * | ||
17 | * along with this program; if not, write to the Free Software * | ||
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
19 | ***************************************************************************/ | ||
20 | |||
21 | #ifndef _SN9C102_H_ | ||
22 | #define _SN9C102_H_ | ||
23 | |||
24 | #include <linux/usb.h> | ||
25 | #include <linux/videodev2.h> | ||
26 | #include <media/v4l2-common.h> | ||
27 | #include <media/v4l2-ioctl.h> | ||
28 | #include <linux/device.h> | ||
29 | #include <linux/list.h> | ||
30 | #include <linux/spinlock.h> | ||
31 | #include <linux/time.h> | ||
32 | #include <linux/wait.h> | ||
33 | #include <linux/types.h> | ||
34 | #include <linux/param.h> | ||
35 | #include <linux/rwsem.h> | ||
36 | #include <linux/mutex.h> | ||
37 | #include <linux/string.h> | ||
38 | #include <linux/stddef.h> | ||
39 | #include <linux/kref.h> | ||
40 | |||
41 | #include "sn9c102_config.h" | ||
42 | #include "sn9c102_sensor.h" | ||
43 | #include "sn9c102_devtable.h" | ||
44 | |||
45 | |||
46 | enum sn9c102_frame_state { | ||
47 | F_UNUSED, | ||
48 | F_QUEUED, | ||
49 | F_GRABBING, | ||
50 | F_DONE, | ||
51 | F_ERROR, | ||
52 | }; | ||
53 | |||
54 | struct sn9c102_frame_t { | ||
55 | void* bufmem; | ||
56 | struct v4l2_buffer buf; | ||
57 | enum sn9c102_frame_state state; | ||
58 | struct list_head frame; | ||
59 | unsigned long vma_use_count; | ||
60 | }; | ||
61 | |||
62 | enum sn9c102_dev_state { | ||
63 | DEV_INITIALIZED = 0x01, | ||
64 | DEV_DISCONNECTED = 0x02, | ||
65 | DEV_MISCONFIGURED = 0x04, | ||
66 | }; | ||
67 | |||
68 | enum sn9c102_io_method { | ||
69 | IO_NONE, | ||
70 | IO_READ, | ||
71 | IO_MMAP, | ||
72 | }; | ||
73 | |||
74 | enum sn9c102_stream_state { | ||
75 | STREAM_OFF, | ||
76 | STREAM_INTERRUPT, | ||
77 | STREAM_ON, | ||
78 | }; | ||
79 | |||
80 | typedef char sn9c102_sof_header_t[62]; | ||
81 | |||
82 | struct sn9c102_sof_t { | ||
83 | sn9c102_sof_header_t header; | ||
84 | u16 bytesread; | ||
85 | }; | ||
86 | |||
87 | struct sn9c102_sysfs_attr { | ||
88 | u16 reg, i2c_reg; | ||
89 | sn9c102_sof_header_t frame_header; | ||
90 | }; | ||
91 | |||
92 | struct sn9c102_module_param { | ||
93 | u8 force_munmap; | ||
94 | u16 frame_timeout; | ||
95 | }; | ||
96 | |||
97 | static DEFINE_MUTEX(sn9c102_sysfs_lock); | ||
98 | static DECLARE_RWSEM(sn9c102_dev_lock); | ||
99 | |||
100 | struct sn9c102_device { | ||
101 | struct video_device* v4ldev; | ||
102 | |||
103 | enum sn9c102_bridge bridge; | ||
104 | struct sn9c102_sensor sensor; | ||
105 | |||
106 | struct usb_device* usbdev; | ||
107 | struct urb* urb[SN9C102_URBS]; | ||
108 | void* transfer_buffer[SN9C102_URBS]; | ||
109 | u8* control_buffer; | ||
110 | |||
111 | struct sn9c102_frame_t *frame_current, frame[SN9C102_MAX_FRAMES]; | ||
112 | struct list_head inqueue, outqueue; | ||
113 | u32 frame_count, nbuffers, nreadbuffers; | ||
114 | |||
115 | enum sn9c102_io_method io; | ||
116 | enum sn9c102_stream_state stream; | ||
117 | |||
118 | struct v4l2_jpegcompression compression; | ||
119 | |||
120 | struct sn9c102_sysfs_attr sysfs; | ||
121 | struct sn9c102_sof_t sof; | ||
122 | u16 reg[384]; | ||
123 | |||
124 | struct sn9c102_module_param module_param; | ||
125 | |||
126 | struct kref kref; | ||
127 | enum sn9c102_dev_state state; | ||
128 | u8 users; | ||
129 | |||
130 | struct completion probe; | ||
131 | struct mutex open_mutex, fileop_mutex; | ||
132 | spinlock_t queue_lock; | ||
133 | wait_queue_head_t wait_open, wait_frame, wait_stream; | ||
134 | }; | ||
135 | |||
136 | /*****************************************************************************/ | ||
137 | |||
138 | struct sn9c102_device* | ||
139 | sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id) | ||
140 | { | ||
141 | return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL; | ||
142 | } | ||
143 | |||
144 | |||
145 | void | ||
146 | sn9c102_attach_sensor(struct sn9c102_device* cam, | ||
147 | const struct sn9c102_sensor* sensor) | ||
148 | { | ||
149 | memcpy(&cam->sensor, sensor, sizeof(struct sn9c102_sensor)); | ||
150 | } | ||
151 | |||
152 | |||
153 | enum sn9c102_bridge | ||
154 | sn9c102_get_bridge(struct sn9c102_device* cam) | ||
155 | { | ||
156 | return cam->bridge; | ||
157 | } | ||
158 | |||
159 | |||
160 | struct sn9c102_sensor* sn9c102_get_sensor(struct sn9c102_device* cam) | ||
161 | { | ||
162 | return &cam->sensor; | ||
163 | } | ||
164 | |||
165 | /*****************************************************************************/ | ||
166 | |||
167 | #undef DBG | ||
168 | #undef KDBG | ||
169 | #ifdef SN9C102_DEBUG | ||
170 | # define DBG(level, fmt, args...) \ | ||
171 | do { \ | ||
172 | if (debug >= (level)) { \ | ||
173 | if ((level) == 1) \ | ||
174 | dev_err(&cam->usbdev->dev, fmt "\n", ## args); \ | ||
175 | else if ((level) == 2) \ | ||
176 | dev_info(&cam->usbdev->dev, fmt "\n", ## args); \ | ||
177 | else if ((level) >= 3) \ | ||
178 | dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \ | ||
179 | __func__, __LINE__ , ## args); \ | ||
180 | } \ | ||
181 | } while (0) | ||
182 | # define V4LDBG(level, name, cmd) \ | ||
183 | do { \ | ||
184 | if (debug >= (level)) \ | ||
185 | v4l_printk_ioctl(name, cmd); \ | ||
186 | } while (0) | ||
187 | # define KDBG(level, fmt, args...) \ | ||
188 | do { \ | ||
189 | if (debug >= (level)) { \ | ||
190 | if ((level) == 1 || (level) == 2) \ | ||
191 | pr_info("sn9c102: " fmt "\n", ## args); \ | ||
192 | else if ((level) == 3) \ | ||
193 | pr_debug("sn9c102: [%s:%d] " fmt "\n", \ | ||
194 | __func__, __LINE__ , ## args); \ | ||
195 | } \ | ||
196 | } while (0) | ||
197 | #else | ||
198 | # define DBG(level, fmt, args...) do {;} while(0) | ||
199 | # define V4LDBG(level, name, cmd) do {;} while(0) | ||
200 | # define KDBG(level, fmt, args...) do {;} while(0) | ||
201 | #endif | ||
202 | |||
203 | #undef PDBG | ||
204 | #define PDBG(fmt, args...) \ | ||
205 | dev_info(&cam->usbdev->dev, "[%s:%s:%d] " fmt "\n", __FILE__, __func__, \ | ||
206 | __LINE__ , ## args) | ||
207 | |||
208 | #undef PDBGG | ||
209 | #define PDBGG(fmt, args...) do {;} while(0) /* placeholder */ | ||
210 | |||
211 | #endif /* _SN9C102_H_ */ | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_config.h b/drivers/media/usb/sn9c102/sn9c102_config.h new file mode 100644 index 000000000000..0f4e0378b071 --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_config.h | |||
@@ -0,0 +1,86 @@ | |||
1 | /*************************************************************************** | ||
2 | * Global parameters for the V4L2 driver for SN9C1xx PC Camera Controllers * | ||
3 | * * | ||
4 | * Copyright (C) 2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License as published by * | ||
8 | * the Free Software Foundation; either version 2 of the License, or * | ||
9 | * (at your option) any later version. * | ||
10 | * * | ||
11 | * This program is distributed in the hope that it will be useful, * | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
14 | * GNU General Public License for more details. * | ||
15 | * * | ||
16 | * You should have received a copy of the GNU General Public License * | ||
17 | * along with this program; if not, write to the Free Software * | ||
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
19 | ***************************************************************************/ | ||
20 | |||
21 | #ifndef _SN9C102_CONFIG_H_ | ||
22 | #define _SN9C102_CONFIG_H_ | ||
23 | |||
24 | #include <linux/types.h> | ||
25 | #include <linux/jiffies.h> | ||
26 | |||
27 | #define SN9C102_DEBUG | ||
28 | #define SN9C102_DEBUG_LEVEL 2 | ||
29 | #define SN9C102_MAX_DEVICES 64 | ||
30 | #define SN9C102_PRESERVE_IMGSCALE 0 | ||
31 | #define SN9C102_FORCE_MUNMAP 0 | ||
32 | #define SN9C102_MAX_FRAMES 32 | ||
33 | #define SN9C102_URBS 2 | ||
34 | #define SN9C102_ISO_PACKETS 7 | ||
35 | #define SN9C102_ALTERNATE_SETTING 8 | ||
36 | #define SN9C102_URB_TIMEOUT msecs_to_jiffies(2 * SN9C102_ISO_PACKETS) | ||
37 | #define SN9C102_CTRL_TIMEOUT 300 | ||
38 | #define SN9C102_FRAME_TIMEOUT 0 | ||
39 | |||
40 | /*****************************************************************************/ | ||
41 | |||
42 | static const u8 SN9C102_Y_QTABLE0[64] = { | ||
43 | 8, 5, 5, 8, 12, 20, 25, 30, | ||
44 | 6, 6, 7, 9, 13, 29, 30, 27, | ||
45 | 7, 6, 8, 12, 20, 28, 34, 28, | ||
46 | 7, 8, 11, 14, 25, 43, 40, 31, | ||
47 | 9, 11, 18, 28, 34, 54, 51, 38, | ||
48 | 12, 17, 27, 32, 40, 52, 56, 46, | ||
49 | 24, 32, 39, 43, 51, 60, 60, 50, | ||
50 | 36, 46, 47, 49, 56, 50, 51, 49 | ||
51 | }; | ||
52 | |||
53 | static const u8 SN9C102_UV_QTABLE0[64] = { | ||
54 | 8, 9, 12, 23, 49, 49, 49, 49, | ||
55 | 9, 10, 13, 33, 49, 49, 49, 49, | ||
56 | 12, 13, 28, 49, 49, 49, 49, 49, | ||
57 | 23, 33, 49, 49, 49, 49, 49, 49, | ||
58 | 49, 49, 49, 49, 49, 49, 49, 49, | ||
59 | 49, 49, 49, 49, 49, 49, 49, 49, | ||
60 | 49, 49, 49, 49, 49, 49, 49, 49, | ||
61 | 49, 49, 49, 49, 49, 49, 49, 49 | ||
62 | }; | ||
63 | |||
64 | static const u8 SN9C102_Y_QTABLE1[64] = { | ||
65 | 16, 11, 10, 16, 24, 40, 51, 61, | ||
66 | 12, 12, 14, 19, 26, 58, 60, 55, | ||
67 | 14, 13, 16, 24, 40, 57, 69, 56, | ||
68 | 14, 17, 22, 29, 51, 87, 80, 62, | ||
69 | 18, 22, 37, 56, 68, 109, 103, 77, | ||
70 | 24, 35, 55, 64, 81, 104, 113, 92, | ||
71 | 49, 64, 78, 87, 103, 121, 120, 101, | ||
72 | 72, 92, 95, 98, 112, 100, 103, 99 | ||
73 | }; | ||
74 | |||
75 | static const u8 SN9C102_UV_QTABLE1[64] = { | ||
76 | 17, 18, 24, 47, 99, 99, 99, 99, | ||
77 | 18, 21, 26, 66, 99, 99, 99, 99, | ||
78 | 24, 26, 56, 99, 99, 99, 99, 99, | ||
79 | 47, 66, 99, 99, 99, 99, 99, 99, | ||
80 | 99, 99, 99, 99, 99, 99, 99, 99, | ||
81 | 99, 99, 99, 99, 99, 99, 99, 99, | ||
82 | 99, 99, 99, 99, 99, 99, 99, 99, | ||
83 | 99, 99, 99, 99, 99, 99, 99, 99 | ||
84 | }; | ||
85 | |||
86 | #endif /* _SN9C102_CONFIG_H_ */ | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_core.c b/drivers/media/usb/sn9c102/sn9c102_core.c new file mode 100644 index 000000000000..19ea780b16ff --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_core.c | |||
@@ -0,0 +1,3421 @@ | |||
1 | /*************************************************************************** | ||
2 | * V4L2 driver for SN9C1xx PC Camera Controllers * | ||
3 | * * | ||
4 | * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License as published by * | ||
8 | * the Free Software Foundation; either version 2 of the License, or * | ||
9 | * (at your option) any later version. * | ||
10 | * * | ||
11 | * This program is distributed in the hope that it will be useful, * | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
14 | * GNU General Public License for more details. * | ||
15 | * * | ||
16 | * You should have received a copy of the GNU General Public License * | ||
17 | * along with this program; if not, write to the Free Software * | ||
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
19 | ***************************************************************************/ | ||
20 | |||
21 | #include <linux/module.h> | ||
22 | #include <linux/init.h> | ||
23 | #include <linux/kernel.h> | ||
24 | #include <linux/param.h> | ||
25 | #include <linux/errno.h> | ||
26 | #include <linux/slab.h> | ||
27 | #include <linux/device.h> | ||
28 | #include <linux/fs.h> | ||
29 | #include <linux/delay.h> | ||
30 | #include <linux/compiler.h> | ||
31 | #include <linux/ioctl.h> | ||
32 | #include <linux/poll.h> | ||
33 | #include <linux/stat.h> | ||
34 | #include <linux/mm.h> | ||
35 | #include <linux/vmalloc.h> | ||
36 | #include <linux/version.h> | ||
37 | #include <linux/page-flags.h> | ||
38 | #include <asm/byteorder.h> | ||
39 | #include <asm/page.h> | ||
40 | #include <asm/uaccess.h> | ||
41 | |||
42 | #include "sn9c102.h" | ||
43 | |||
44 | /*****************************************************************************/ | ||
45 | |||
46 | #define SN9C102_MODULE_NAME "V4L2 driver for SN9C1xx PC Camera Controllers" | ||
47 | #define SN9C102_MODULE_ALIAS "sn9c1xx" | ||
48 | #define SN9C102_MODULE_AUTHOR "(C) 2004-2007 Luca Risolia" | ||
49 | #define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>" | ||
50 | #define SN9C102_MODULE_LICENSE "GPL" | ||
51 | #define SN9C102_MODULE_VERSION "1:1.48" | ||
52 | |||
53 | /*****************************************************************************/ | ||
54 | |||
55 | MODULE_DEVICE_TABLE(usb, sn9c102_id_table); | ||
56 | |||
57 | MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL); | ||
58 | MODULE_DESCRIPTION(SN9C102_MODULE_NAME); | ||
59 | MODULE_ALIAS(SN9C102_MODULE_ALIAS); | ||
60 | MODULE_VERSION(SN9C102_MODULE_VERSION); | ||
61 | MODULE_LICENSE(SN9C102_MODULE_LICENSE); | ||
62 | |||
63 | static short video_nr[] = {[0 ... SN9C102_MAX_DEVICES-1] = -1}; | ||
64 | module_param_array(video_nr, short, NULL, 0444); | ||
65 | MODULE_PARM_DESC(video_nr, | ||
66 | " <-1|n[,...]>" | ||
67 | "\nSpecify V4L2 minor mode number." | ||
68 | "\n-1 = use next available (default)" | ||
69 | "\n n = use minor number n (integer >= 0)" | ||
70 | "\nYou can specify up to "__MODULE_STRING(SN9C102_MAX_DEVICES) | ||
71 | " cameras this way." | ||
72 | "\nFor example:" | ||
73 | "\nvideo_nr=-1,2,-1 would assign minor number 2 to" | ||
74 | "\nthe second camera and use auto for the first" | ||
75 | "\none and for every other camera." | ||
76 | "\n"); | ||
77 | |||
78 | static bool force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] = | ||
79 | SN9C102_FORCE_MUNMAP}; | ||
80 | module_param_array(force_munmap, bool, NULL, 0444); | ||
81 | MODULE_PARM_DESC(force_munmap, | ||
82 | " <0|1[,...]>" | ||
83 | "\nForce the application to unmap previously" | ||
84 | "\nmapped buffer memory before calling any VIDIOC_S_CROP or" | ||
85 | "\nVIDIOC_S_FMT ioctl's. Not all the applications support" | ||
86 | "\nthis feature. This parameter is specific for each" | ||
87 | "\ndetected camera." | ||
88 | "\n0 = do not force memory unmapping" | ||
89 | "\n1 = force memory unmapping (save memory)" | ||
90 | "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"." | ||
91 | "\n"); | ||
92 | |||
93 | static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] = | ||
94 | SN9C102_FRAME_TIMEOUT}; | ||
95 | module_param_array(frame_timeout, uint, NULL, 0644); | ||
96 | MODULE_PARM_DESC(frame_timeout, | ||
97 | " <0|n[,...]>" | ||
98 | "\nTimeout for a video frame in seconds before" | ||
99 | "\nreturning an I/O error; 0 for infinity." | ||
100 | "\nThis parameter is specific for each detected camera." | ||
101 | "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"." | ||
102 | "\n"); | ||
103 | |||
104 | #ifdef SN9C102_DEBUG | ||
105 | static unsigned short debug = SN9C102_DEBUG_LEVEL; | ||
106 | module_param(debug, ushort, 0644); | ||
107 | MODULE_PARM_DESC(debug, | ||
108 | " <n>" | ||
109 | "\nDebugging information level, from 0 to 3:" | ||
110 | "\n0 = none (use carefully)" | ||
111 | "\n1 = critical errors" | ||
112 | "\n2 = significant informations" | ||
113 | "\n3 = more verbose messages" | ||
114 | "\nLevel 3 is useful for testing only." | ||
115 | "\nDefault value is "__MODULE_STRING(SN9C102_DEBUG_LEVEL)"." | ||
116 | "\n"); | ||
117 | #endif | ||
118 | |||
119 | /* | ||
120 | Add the probe entries to this table. Be sure to add the entry in the right | ||
121 | place, since, on failure, the next probing routine is called according to | ||
122 | the order of the list below, from top to bottom. | ||
123 | */ | ||
124 | static int (*sn9c102_sensor_table[])(struct sn9c102_device *) = { | ||
125 | &sn9c102_probe_hv7131d, /* strong detection based on SENSOR ids */ | ||
126 | &sn9c102_probe_hv7131r, /* strong detection based on SENSOR ids */ | ||
127 | &sn9c102_probe_mi0343, /* strong detection based on SENSOR ids */ | ||
128 | &sn9c102_probe_mi0360, /* strong detection based on SENSOR ids */ | ||
129 | &sn9c102_probe_mt9v111, /* strong detection based on SENSOR ids */ | ||
130 | &sn9c102_probe_pas106b, /* strong detection based on SENSOR ids */ | ||
131 | &sn9c102_probe_pas202bcb, /* strong detection based on SENSOR ids */ | ||
132 | &sn9c102_probe_ov7630, /* strong detection based on SENSOR ids */ | ||
133 | &sn9c102_probe_ov7660, /* strong detection based on SENSOR ids */ | ||
134 | &sn9c102_probe_tas5110c1b, /* detection based on USB pid/vid */ | ||
135 | &sn9c102_probe_tas5110d, /* detection based on USB pid/vid */ | ||
136 | &sn9c102_probe_tas5130d1b, /* detection based on USB pid/vid */ | ||
137 | }; | ||
138 | |||
139 | /*****************************************************************************/ | ||
140 | |||
141 | static u32 | ||
142 | sn9c102_request_buffers(struct sn9c102_device* cam, u32 count, | ||
143 | enum sn9c102_io_method io) | ||
144 | { | ||
145 | struct v4l2_pix_format* p = &(cam->sensor.pix_format); | ||
146 | struct v4l2_rect* r = &(cam->sensor.cropcap.bounds); | ||
147 | size_t imagesize = cam->module_param.force_munmap || io == IO_READ ? | ||
148 | (p->width * p->height * p->priv) / 8 : | ||
149 | (r->width * r->height * p->priv) / 8; | ||
150 | void* buff = NULL; | ||
151 | u32 i; | ||
152 | |||
153 | if (count > SN9C102_MAX_FRAMES) | ||
154 | count = SN9C102_MAX_FRAMES; | ||
155 | |||
156 | if (cam->bridge == BRIDGE_SN9C105 || cam->bridge == BRIDGE_SN9C120) | ||
157 | imagesize += 589 + 2; /* length of JPEG header + EOI marker */ | ||
158 | |||
159 | cam->nbuffers = count; | ||
160 | while (cam->nbuffers > 0) { | ||
161 | if ((buff = vmalloc_32_user(cam->nbuffers * | ||
162 | PAGE_ALIGN(imagesize)))) | ||
163 | break; | ||
164 | cam->nbuffers--; | ||
165 | } | ||
166 | |||
167 | for (i = 0; i < cam->nbuffers; i++) { | ||
168 | cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize); | ||
169 | cam->frame[i].buf.index = i; | ||
170 | cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize); | ||
171 | cam->frame[i].buf.length = imagesize; | ||
172 | cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
173 | cam->frame[i].buf.sequence = 0; | ||
174 | cam->frame[i].buf.field = V4L2_FIELD_NONE; | ||
175 | cam->frame[i].buf.memory = V4L2_MEMORY_MMAP; | ||
176 | cam->frame[i].buf.flags = 0; | ||
177 | } | ||
178 | |||
179 | return cam->nbuffers; | ||
180 | } | ||
181 | |||
182 | |||
183 | static void sn9c102_release_buffers(struct sn9c102_device* cam) | ||
184 | { | ||
185 | if (cam->nbuffers) { | ||
186 | vfree(cam->frame[0].bufmem); | ||
187 | cam->nbuffers = 0; | ||
188 | } | ||
189 | cam->frame_current = NULL; | ||
190 | } | ||
191 | |||
192 | |||
193 | static void sn9c102_empty_framequeues(struct sn9c102_device* cam) | ||
194 | { | ||
195 | u32 i; | ||
196 | |||
197 | INIT_LIST_HEAD(&cam->inqueue); | ||
198 | INIT_LIST_HEAD(&cam->outqueue); | ||
199 | |||
200 | for (i = 0; i < SN9C102_MAX_FRAMES; i++) { | ||
201 | cam->frame[i].state = F_UNUSED; | ||
202 | cam->frame[i].buf.bytesused = 0; | ||
203 | } | ||
204 | } | ||
205 | |||
206 | |||
207 | static void sn9c102_requeue_outqueue(struct sn9c102_device* cam) | ||
208 | { | ||
209 | struct sn9c102_frame_t *i; | ||
210 | |||
211 | list_for_each_entry(i, &cam->outqueue, frame) { | ||
212 | i->state = F_QUEUED; | ||
213 | list_add(&i->frame, &cam->inqueue); | ||
214 | } | ||
215 | |||
216 | INIT_LIST_HEAD(&cam->outqueue); | ||
217 | } | ||
218 | |||
219 | |||
220 | static void sn9c102_queue_unusedframes(struct sn9c102_device* cam) | ||
221 | { | ||
222 | unsigned long lock_flags; | ||
223 | u32 i; | ||
224 | |||
225 | for (i = 0; i < cam->nbuffers; i++) | ||
226 | if (cam->frame[i].state == F_UNUSED) { | ||
227 | cam->frame[i].state = F_QUEUED; | ||
228 | spin_lock_irqsave(&cam->queue_lock, lock_flags); | ||
229 | list_add_tail(&cam->frame[i].frame, &cam->inqueue); | ||
230 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | ||
231 | } | ||
232 | } | ||
233 | |||
234 | /*****************************************************************************/ | ||
235 | |||
236 | /* | ||
237 | Write a sequence of count value/register pairs. Returns -1 after the first | ||
238 | failed write, or 0 for no errors. | ||
239 | */ | ||
240 | int sn9c102_write_regs(struct sn9c102_device* cam, const u8 valreg[][2], | ||
241 | int count) | ||
242 | { | ||
243 | struct usb_device* udev = cam->usbdev; | ||
244 | u8* buff = cam->control_buffer; | ||
245 | int i, res; | ||
246 | |||
247 | for (i = 0; i < count; i++) { | ||
248 | u8 index = valreg[i][1]; | ||
249 | |||
250 | /* | ||
251 | index is a u8, so it must be <256 and can't be out of range. | ||
252 | If we put in a check anyway, gcc annoys us with a warning | ||
253 | hat our check is useless. People get all uppity when they | ||
254 | see warnings in the kernel compile. | ||
255 | */ | ||
256 | |||
257 | *buff = valreg[i][0]; | ||
258 | |||
259 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, | ||
260 | 0x41, index, 0, buff, 1, | ||
261 | SN9C102_CTRL_TIMEOUT); | ||
262 | |||
263 | if (res < 0) { | ||
264 | DBG(3, "Failed to write a register (value 0x%02X, " | ||
265 | "index 0x%02X, error %d)", *buff, index, res); | ||
266 | return -1; | ||
267 | } | ||
268 | |||
269 | cam->reg[index] = *buff; | ||
270 | } | ||
271 | |||
272 | return 0; | ||
273 | } | ||
274 | |||
275 | |||
276 | int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index) | ||
277 | { | ||
278 | struct usb_device* udev = cam->usbdev; | ||
279 | u8* buff = cam->control_buffer; | ||
280 | int res; | ||
281 | |||
282 | if (index >= ARRAY_SIZE(cam->reg)) | ||
283 | return -1; | ||
284 | |||
285 | *buff = value; | ||
286 | |||
287 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | ||
288 | index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); | ||
289 | if (res < 0) { | ||
290 | DBG(3, "Failed to write a register (value 0x%02X, index " | ||
291 | "0x%02X, error %d)", value, index, res); | ||
292 | return -1; | ||
293 | } | ||
294 | |||
295 | cam->reg[index] = value; | ||
296 | |||
297 | return 0; | ||
298 | } | ||
299 | |||
300 | |||
301 | /* NOTE: with the SN9C10[123] reading some registers always returns 0 */ | ||
302 | int sn9c102_read_reg(struct sn9c102_device* cam, u16 index) | ||
303 | { | ||
304 | struct usb_device* udev = cam->usbdev; | ||
305 | u8* buff = cam->control_buffer; | ||
306 | int res; | ||
307 | |||
308 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1, | ||
309 | index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); | ||
310 | if (res < 0) | ||
311 | DBG(3, "Failed to read a register (index 0x%02X, error %d)", | ||
312 | index, res); | ||
313 | |||
314 | return (res >= 0) ? (int)(*buff) : -1; | ||
315 | } | ||
316 | |||
317 | |||
318 | int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index) | ||
319 | { | ||
320 | if (index >= ARRAY_SIZE(cam->reg)) | ||
321 | return -1; | ||
322 | |||
323 | return cam->reg[index]; | ||
324 | } | ||
325 | |||
326 | |||
327 | static int | ||
328 | sn9c102_i2c_wait(struct sn9c102_device* cam, | ||
329 | const struct sn9c102_sensor* sensor) | ||
330 | { | ||
331 | int i, r; | ||
332 | |||
333 | for (i = 1; i <= 5; i++) { | ||
334 | r = sn9c102_read_reg(cam, 0x08); | ||
335 | if (r < 0) | ||
336 | return -EIO; | ||
337 | if (r & 0x04) | ||
338 | return 0; | ||
339 | if (sensor->frequency & SN9C102_I2C_400KHZ) | ||
340 | udelay(5*16); | ||
341 | else | ||
342 | udelay(16*16); | ||
343 | } | ||
344 | return -EBUSY; | ||
345 | } | ||
346 | |||
347 | |||
348 | static int | ||
349 | sn9c102_i2c_detect_read_error(struct sn9c102_device* cam, | ||
350 | const struct sn9c102_sensor* sensor) | ||
351 | { | ||
352 | int r , err = 0; | ||
353 | |||
354 | r = sn9c102_read_reg(cam, 0x08); | ||
355 | if (r < 0) | ||
356 | err += r; | ||
357 | |||
358 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) { | ||
359 | if (!(r & 0x08)) | ||
360 | err += -1; | ||
361 | } else { | ||
362 | if (r & 0x08) | ||
363 | err += -1; | ||
364 | } | ||
365 | |||
366 | return err ? -EIO : 0; | ||
367 | } | ||
368 | |||
369 | |||
370 | static int | ||
371 | sn9c102_i2c_detect_write_error(struct sn9c102_device* cam, | ||
372 | const struct sn9c102_sensor* sensor) | ||
373 | { | ||
374 | int r; | ||
375 | r = sn9c102_read_reg(cam, 0x08); | ||
376 | return (r < 0 || (r >= 0 && (r & 0x08))) ? -EIO : 0; | ||
377 | } | ||
378 | |||
379 | |||
380 | int | ||
381 | sn9c102_i2c_try_raw_read(struct sn9c102_device* cam, | ||
382 | const struct sn9c102_sensor* sensor, u8 data0, | ||
383 | u8 data1, u8 n, u8 buffer[]) | ||
384 | { | ||
385 | struct usb_device* udev = cam->usbdev; | ||
386 | u8* data = cam->control_buffer; | ||
387 | int i = 0, err = 0, res; | ||
388 | |||
389 | /* Write cycle */ | ||
390 | data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) | | ||
391 | ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | 0x10; | ||
392 | data[1] = data0; /* I2C slave id */ | ||
393 | data[2] = data1; /* address */ | ||
394 | data[7] = 0x10; | ||
395 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | ||
396 | 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT); | ||
397 | if (res < 0) | ||
398 | err += res; | ||
399 | |||
400 | err += sn9c102_i2c_wait(cam, sensor); | ||
401 | |||
402 | /* Read cycle - n bytes */ | ||
403 | data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) | | ||
404 | ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | | ||
405 | (n << 4) | 0x02; | ||
406 | data[1] = data0; | ||
407 | data[7] = 0x10; | ||
408 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | ||
409 | 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT); | ||
410 | if (res < 0) | ||
411 | err += res; | ||
412 | |||
413 | err += sn9c102_i2c_wait(cam, sensor); | ||
414 | |||
415 | /* The first read byte will be placed in data[4] */ | ||
416 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1, | ||
417 | 0x0a, 0, data, 5, SN9C102_CTRL_TIMEOUT); | ||
418 | if (res < 0) | ||
419 | err += res; | ||
420 | |||
421 | err += sn9c102_i2c_detect_read_error(cam, sensor); | ||
422 | |||
423 | PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1, | ||
424 | data[4]); | ||
425 | |||
426 | if (err) { | ||
427 | DBG(3, "I2C read failed for %s image sensor", sensor->name); | ||
428 | return -1; | ||
429 | } | ||
430 | |||
431 | if (buffer) | ||
432 | for (i = 0; i < n && i < 5; i++) | ||
433 | buffer[n-i-1] = data[4-i]; | ||
434 | |||
435 | return (int)data[4]; | ||
436 | } | ||
437 | |||
438 | |||
439 | int | ||
440 | sn9c102_i2c_try_raw_write(struct sn9c102_device* cam, | ||
441 | const struct sn9c102_sensor* sensor, u8 n, u8 data0, | ||
442 | u8 data1, u8 data2, u8 data3, u8 data4, u8 data5) | ||
443 | { | ||
444 | struct usb_device* udev = cam->usbdev; | ||
445 | u8* data = cam->control_buffer; | ||
446 | int err = 0, res; | ||
447 | |||
448 | /* Write cycle. It usually is address + value */ | ||
449 | data[0] = ((sensor->interface == SN9C102_I2C_2WIRES) ? 0x80 : 0) | | ||
450 | ((sensor->frequency & SN9C102_I2C_400KHZ) ? 0x01 : 0) | ||
451 | | ((n - 1) << 4); | ||
452 | data[1] = data0; | ||
453 | data[2] = data1; | ||
454 | data[3] = data2; | ||
455 | data[4] = data3; | ||
456 | data[5] = data4; | ||
457 | data[6] = data5; | ||
458 | data[7] = 0x17; | ||
459 | res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, | ||
460 | 0x08, 0, data, 8, SN9C102_CTRL_TIMEOUT); | ||
461 | if (res < 0) | ||
462 | err += res; | ||
463 | |||
464 | err += sn9c102_i2c_wait(cam, sensor); | ||
465 | err += sn9c102_i2c_detect_write_error(cam, sensor); | ||
466 | |||
467 | if (err) | ||
468 | DBG(3, "I2C write failed for %s image sensor", sensor->name); | ||
469 | |||
470 | PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, " | ||
471 | "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X", | ||
472 | n, data0, data1, data2, data3, data4, data5); | ||
473 | |||
474 | return err ? -1 : 0; | ||
475 | } | ||
476 | |||
477 | |||
478 | int | ||
479 | sn9c102_i2c_try_read(struct sn9c102_device* cam, | ||
480 | const struct sn9c102_sensor* sensor, u8 address) | ||
481 | { | ||
482 | return sn9c102_i2c_try_raw_read(cam, sensor, sensor->i2c_slave_id, | ||
483 | address, 1, NULL); | ||
484 | } | ||
485 | |||
486 | |||
487 | static int sn9c102_i2c_try_write(struct sn9c102_device* cam, | ||
488 | const struct sn9c102_sensor* sensor, | ||
489 | u8 address, u8 value) | ||
490 | { | ||
491 | return sn9c102_i2c_try_raw_write(cam, sensor, 3, | ||
492 | sensor->i2c_slave_id, address, | ||
493 | value, 0, 0, 0); | ||
494 | } | ||
495 | |||
496 | |||
497 | int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address) | ||
498 | { | ||
499 | return sn9c102_i2c_try_read(cam, &cam->sensor, address); | ||
500 | } | ||
501 | |||
502 | |||
503 | int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value) | ||
504 | { | ||
505 | return sn9c102_i2c_try_write(cam, &cam->sensor, address, value); | ||
506 | } | ||
507 | |||
508 | /*****************************************************************************/ | ||
509 | |||
510 | static size_t sn9c102_sof_length(struct sn9c102_device* cam) | ||
511 | { | ||
512 | switch (cam->bridge) { | ||
513 | case BRIDGE_SN9C101: | ||
514 | case BRIDGE_SN9C102: | ||
515 | return 12; | ||
516 | case BRIDGE_SN9C103: | ||
517 | return 18; | ||
518 | case BRIDGE_SN9C105: | ||
519 | case BRIDGE_SN9C120: | ||
520 | return 62; | ||
521 | } | ||
522 | |||
523 | return 0; | ||
524 | } | ||
525 | |||
526 | |||
527 | static void* | ||
528 | sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len) | ||
529 | { | ||
530 | static const char marker[6] = {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96}; | ||
531 | const char *m = mem; | ||
532 | size_t soflen = 0, i, j; | ||
533 | |||
534 | soflen = sn9c102_sof_length(cam); | ||
535 | |||
536 | for (i = 0; i < len; i++) { | ||
537 | size_t b; | ||
538 | |||
539 | /* Read the variable part of the header */ | ||
540 | if (unlikely(cam->sof.bytesread >= sizeof(marker))) { | ||
541 | cam->sof.header[cam->sof.bytesread] = *(m+i); | ||
542 | if (++cam->sof.bytesread == soflen) { | ||
543 | cam->sof.bytesread = 0; | ||
544 | return mem + i; | ||
545 | } | ||
546 | continue; | ||
547 | } | ||
548 | |||
549 | /* Search for the SOF marker (fixed part) in the header */ | ||
550 | for (j = 0, b=cam->sof.bytesread; j+b < sizeof(marker); j++) { | ||
551 | if (unlikely(i+j == len)) | ||
552 | return NULL; | ||
553 | if (*(m+i+j) == marker[cam->sof.bytesread]) { | ||
554 | cam->sof.header[cam->sof.bytesread] = *(m+i+j); | ||
555 | if (++cam->sof.bytesread == sizeof(marker)) { | ||
556 | PDBGG("Bytes to analyze: %zd. SOF " | ||
557 | "starts at byte #%zd", len, i); | ||
558 | i += j+1; | ||
559 | break; | ||
560 | } | ||
561 | } else { | ||
562 | cam->sof.bytesread = 0; | ||
563 | break; | ||
564 | } | ||
565 | } | ||
566 | } | ||
567 | |||
568 | return NULL; | ||
569 | } | ||
570 | |||
571 | |||
572 | static void* | ||
573 | sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len) | ||
574 | { | ||
575 | static const u8 eof_header[4][4] = { | ||
576 | {0x00, 0x00, 0x00, 0x00}, | ||
577 | {0x40, 0x00, 0x00, 0x00}, | ||
578 | {0x80, 0x00, 0x00, 0x00}, | ||
579 | {0xc0, 0x00, 0x00, 0x00}, | ||
580 | }; | ||
581 | size_t i, j; | ||
582 | |||
583 | /* The EOF header does not exist in compressed data */ | ||
584 | if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X || | ||
585 | cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG) | ||
586 | return NULL; | ||
587 | |||
588 | /* | ||
589 | The EOF header might cross the packet boundary, but this is not a | ||
590 | problem, since the end of a frame is determined by checking its size | ||
591 | in the first place. | ||
592 | */ | ||
593 | for (i = 0; (len >= 4) && (i <= len - 4); i++) | ||
594 | for (j = 0; j < ARRAY_SIZE(eof_header); j++) | ||
595 | if (!memcmp(mem + i, eof_header[j], 4)) | ||
596 | return mem + i; | ||
597 | |||
598 | return NULL; | ||
599 | } | ||
600 | |||
601 | |||
602 | static void | ||
603 | sn9c102_write_jpegheader(struct sn9c102_device* cam, struct sn9c102_frame_t* f) | ||
604 | { | ||
605 | static const u8 jpeg_header[589] = { | ||
606 | 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05, | ||
607 | 0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06, | ||
608 | 0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e, | ||
609 | 0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16, | ||
610 | 0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16, | ||
611 | 0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29, | ||
612 | 0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29, | ||
613 | 0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a, | ||
614 | 0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28, | ||
615 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, | ||
616 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, | ||
617 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, | ||
618 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, | ||
619 | 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2, | ||
620 | 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, | ||
621 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, | ||
622 | 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01, | ||
623 | 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, | ||
624 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, | ||
625 | 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00, | ||
626 | 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, | ||
627 | 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04, | ||
628 | 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, | ||
629 | 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, | ||
630 | 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, | ||
631 | 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, | ||
632 | 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, | ||
633 | 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, | ||
634 | 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, | ||
635 | 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, | ||
636 | 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, | ||
637 | 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, | ||
638 | 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, | ||
639 | 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, | ||
640 | 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, | ||
641 | 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, | ||
642 | 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, | ||
643 | 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02, | ||
644 | 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, | ||
645 | 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, | ||
646 | 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, | ||
647 | 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, | ||
648 | 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, | ||
649 | 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, | ||
650 | 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, | ||
651 | 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, | ||
652 | 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, | ||
653 | 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, | ||
654 | 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, | ||
655 | 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, | ||
656 | 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, | ||
657 | 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, | ||
658 | 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, | ||
659 | 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, | ||
660 | 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, | ||
661 | 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11, | ||
662 | 0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02, | ||
663 | 0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03, | ||
664 | 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00 | ||
665 | }; | ||
666 | u8 *pos = f->bufmem; | ||
667 | |||
668 | memcpy(pos, jpeg_header, sizeof(jpeg_header)); | ||
669 | *(pos + 6) = 0x00; | ||
670 | *(pos + 7 + 64) = 0x01; | ||
671 | if (cam->compression.quality == 0) { | ||
672 | memcpy(pos + 7, SN9C102_Y_QTABLE0, 64); | ||
673 | memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64); | ||
674 | } else if (cam->compression.quality == 1) { | ||
675 | memcpy(pos + 7, SN9C102_Y_QTABLE1, 64); | ||
676 | memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64); | ||
677 | } | ||
678 | *(pos + 564) = cam->sensor.pix_format.width & 0xFF; | ||
679 | *(pos + 563) = (cam->sensor.pix_format.width >> 8) & 0xFF; | ||
680 | *(pos + 562) = cam->sensor.pix_format.height & 0xFF; | ||
681 | *(pos + 561) = (cam->sensor.pix_format.height >> 8) & 0xFF; | ||
682 | *(pos + 567) = 0x21; | ||
683 | |||
684 | f->buf.bytesused += sizeof(jpeg_header); | ||
685 | } | ||
686 | |||
687 | |||
688 | static void sn9c102_urb_complete(struct urb *urb) | ||
689 | { | ||
690 | struct sn9c102_device* cam = urb->context; | ||
691 | struct sn9c102_frame_t** f; | ||
692 | size_t imagesize, soflen; | ||
693 | u8 i; | ||
694 | int err = 0; | ||
695 | |||
696 | if (urb->status == -ENOENT) | ||
697 | return; | ||
698 | |||
699 | f = &cam->frame_current; | ||
700 | |||
701 | if (cam->stream == STREAM_INTERRUPT) { | ||
702 | cam->stream = STREAM_OFF; | ||
703 | if ((*f)) | ||
704 | (*f)->state = F_QUEUED; | ||
705 | cam->sof.bytesread = 0; | ||
706 | DBG(3, "Stream interrupted by application"); | ||
707 | wake_up(&cam->wait_stream); | ||
708 | } | ||
709 | |||
710 | if (cam->state & DEV_DISCONNECTED) | ||
711 | return; | ||
712 | |||
713 | if (cam->state & DEV_MISCONFIGURED) { | ||
714 | wake_up_interruptible(&cam->wait_frame); | ||
715 | return; | ||
716 | } | ||
717 | |||
718 | if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue)) | ||
719 | goto resubmit_urb; | ||
720 | |||
721 | if (!(*f)) | ||
722 | (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t, | ||
723 | frame); | ||
724 | |||
725 | imagesize = (cam->sensor.pix_format.width * | ||
726 | cam->sensor.pix_format.height * | ||
727 | cam->sensor.pix_format.priv) / 8; | ||
728 | if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG) | ||
729 | imagesize += 589; /* length of jpeg header */ | ||
730 | soflen = sn9c102_sof_length(cam); | ||
731 | |||
732 | for (i = 0; i < urb->number_of_packets; i++) { | ||
733 | unsigned int img, len, status; | ||
734 | void *pos, *sof, *eof; | ||
735 | |||
736 | len = urb->iso_frame_desc[i].actual_length; | ||
737 | status = urb->iso_frame_desc[i].status; | ||
738 | pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer; | ||
739 | |||
740 | if (status) { | ||
741 | DBG(3, "Error in isochronous frame"); | ||
742 | (*f)->state = F_ERROR; | ||
743 | cam->sof.bytesread = 0; | ||
744 | continue; | ||
745 | } | ||
746 | |||
747 | PDBGG("Isochrnous frame: length %u, #%u i", len, i); | ||
748 | |||
749 | redo: | ||
750 | sof = sn9c102_find_sof_header(cam, pos, len); | ||
751 | if (likely(!sof)) { | ||
752 | eof = sn9c102_find_eof_header(cam, pos, len); | ||
753 | if ((*f)->state == F_GRABBING) { | ||
754 | end_of_frame: | ||
755 | img = len; | ||
756 | |||
757 | if (eof) | ||
758 | img = (eof > pos) ? eof - pos - 1 : 0; | ||
759 | |||
760 | if ((*f)->buf.bytesused + img > imagesize) { | ||
761 | u32 b; | ||
762 | b = (*f)->buf.bytesused + img - | ||
763 | imagesize; | ||
764 | img = imagesize - (*f)->buf.bytesused; | ||
765 | PDBGG("Expected EOF not found: video " | ||
766 | "frame cut"); | ||
767 | if (eof) | ||
768 | DBG(3, "Exceeded limit: +%u " | ||
769 | "bytes", (unsigned)(b)); | ||
770 | } | ||
771 | |||
772 | memcpy((*f)->bufmem + (*f)->buf.bytesused, pos, | ||
773 | img); | ||
774 | |||
775 | if ((*f)->buf.bytesused == 0) | ||
776 | do_gettimeofday(&(*f)->buf.timestamp); | ||
777 | |||
778 | (*f)->buf.bytesused += img; | ||
779 | |||
780 | if ((*f)->buf.bytesused == imagesize || | ||
781 | ((cam->sensor.pix_format.pixelformat == | ||
782 | V4L2_PIX_FMT_SN9C10X || | ||
783 | cam->sensor.pix_format.pixelformat == | ||
784 | V4L2_PIX_FMT_JPEG) && eof)) { | ||
785 | u32 b; | ||
786 | |||
787 | b = (*f)->buf.bytesused; | ||
788 | (*f)->state = F_DONE; | ||
789 | (*f)->buf.sequence= ++cam->frame_count; | ||
790 | |||
791 | spin_lock(&cam->queue_lock); | ||
792 | list_move_tail(&(*f)->frame, | ||
793 | &cam->outqueue); | ||
794 | if (!list_empty(&cam->inqueue)) | ||
795 | (*f) = list_entry( | ||
796 | cam->inqueue.next, | ||
797 | struct sn9c102_frame_t, | ||
798 | frame ); | ||
799 | else | ||
800 | (*f) = NULL; | ||
801 | spin_unlock(&cam->queue_lock); | ||
802 | |||
803 | memcpy(cam->sysfs.frame_header, | ||
804 | cam->sof.header, soflen); | ||
805 | |||
806 | DBG(3, "Video frame captured: %lu " | ||
807 | "bytes", (unsigned long)(b)); | ||
808 | |||
809 | if (!(*f)) | ||
810 | goto resubmit_urb; | ||
811 | |||
812 | } else if (eof) { | ||
813 | (*f)->state = F_ERROR; | ||
814 | DBG(3, "Not expected EOF after %lu " | ||
815 | "bytes of image data", | ||
816 | (unsigned long) | ||
817 | ((*f)->buf.bytesused)); | ||
818 | } | ||
819 | |||
820 | if (sof) /* (1) */ | ||
821 | goto start_of_frame; | ||
822 | |||
823 | } else if (eof) { | ||
824 | DBG(3, "EOF without SOF"); | ||
825 | continue; | ||
826 | |||
827 | } else { | ||
828 | PDBGG("Ignoring pointless isochronous frame"); | ||
829 | continue; | ||
830 | } | ||
831 | |||
832 | } else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) { | ||
833 | start_of_frame: | ||
834 | (*f)->state = F_GRABBING; | ||
835 | (*f)->buf.bytesused = 0; | ||
836 | len -= (sof - pos); | ||
837 | pos = sof; | ||
838 | if (cam->sensor.pix_format.pixelformat == | ||
839 | V4L2_PIX_FMT_JPEG) | ||
840 | sn9c102_write_jpegheader(cam, (*f)); | ||
841 | DBG(3, "SOF detected: new video frame"); | ||
842 | if (len) | ||
843 | goto redo; | ||
844 | |||
845 | } else if ((*f)->state == F_GRABBING) { | ||
846 | eof = sn9c102_find_eof_header(cam, pos, len); | ||
847 | if (eof && eof < sof) | ||
848 | goto end_of_frame; /* (1) */ | ||
849 | else { | ||
850 | if (cam->sensor.pix_format.pixelformat == | ||
851 | V4L2_PIX_FMT_SN9C10X || | ||
852 | cam->sensor.pix_format.pixelformat == | ||
853 | V4L2_PIX_FMT_JPEG) { | ||
854 | if (sof - pos >= soflen) { | ||
855 | eof = sof - soflen; | ||
856 | } else { /* remove header */ | ||
857 | eof = pos; | ||
858 | (*f)->buf.bytesused -= | ||
859 | (soflen - (sof - pos)); | ||
860 | } | ||
861 | goto end_of_frame; | ||
862 | } else { | ||
863 | DBG(3, "SOF before expected EOF after " | ||
864 | "%lu bytes of image data", | ||
865 | (unsigned long) | ||
866 | ((*f)->buf.bytesused)); | ||
867 | goto start_of_frame; | ||
868 | } | ||
869 | } | ||
870 | } | ||
871 | } | ||
872 | |||
873 | resubmit_urb: | ||
874 | urb->dev = cam->usbdev; | ||
875 | err = usb_submit_urb(urb, GFP_ATOMIC); | ||
876 | if (err < 0 && err != -EPERM) { | ||
877 | cam->state |= DEV_MISCONFIGURED; | ||
878 | DBG(1, "usb_submit_urb() failed"); | ||
879 | } | ||
880 | |||
881 | wake_up_interruptible(&cam->wait_frame); | ||
882 | } | ||
883 | |||
884 | |||
885 | static int sn9c102_start_transfer(struct sn9c102_device* cam) | ||
886 | { | ||
887 | struct usb_device *udev = cam->usbdev; | ||
888 | struct urb* urb; | ||
889 | struct usb_host_interface* altsetting = usb_altnum_to_altsetting( | ||
890 | usb_ifnum_to_if(udev, 0), | ||
891 | SN9C102_ALTERNATE_SETTING); | ||
892 | const unsigned int psz = le16_to_cpu(altsetting-> | ||
893 | endpoint[0].desc.wMaxPacketSize); | ||
894 | s8 i, j; | ||
895 | int err = 0; | ||
896 | |||
897 | for (i = 0; i < SN9C102_URBS; i++) { | ||
898 | cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz, | ||
899 | GFP_KERNEL); | ||
900 | if (!cam->transfer_buffer[i]) { | ||
901 | err = -ENOMEM; | ||
902 | DBG(1, "Not enough memory"); | ||
903 | goto free_buffers; | ||
904 | } | ||
905 | } | ||
906 | |||
907 | for (i = 0; i < SN9C102_URBS; i++) { | ||
908 | urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL); | ||
909 | cam->urb[i] = urb; | ||
910 | if (!urb) { | ||
911 | err = -ENOMEM; | ||
912 | DBG(1, "usb_alloc_urb() failed"); | ||
913 | goto free_urbs; | ||
914 | } | ||
915 | urb->dev = udev; | ||
916 | urb->context = cam; | ||
917 | urb->pipe = usb_rcvisocpipe(udev, 1); | ||
918 | urb->transfer_flags = URB_ISO_ASAP; | ||
919 | urb->number_of_packets = SN9C102_ISO_PACKETS; | ||
920 | urb->complete = sn9c102_urb_complete; | ||
921 | urb->transfer_buffer = cam->transfer_buffer[i]; | ||
922 | urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS; | ||
923 | urb->interval = 1; | ||
924 | for (j = 0; j < SN9C102_ISO_PACKETS; j++) { | ||
925 | urb->iso_frame_desc[j].offset = psz * j; | ||
926 | urb->iso_frame_desc[j].length = psz; | ||
927 | } | ||
928 | } | ||
929 | |||
930 | /* Enable video */ | ||
931 | if (!(cam->reg[0x01] & 0x04)) { | ||
932 | err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01); | ||
933 | if (err) { | ||
934 | err = -EIO; | ||
935 | DBG(1, "I/O hardware error"); | ||
936 | goto free_urbs; | ||
937 | } | ||
938 | } | ||
939 | |||
940 | err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING); | ||
941 | if (err) { | ||
942 | DBG(1, "usb_set_interface() failed"); | ||
943 | goto free_urbs; | ||
944 | } | ||
945 | |||
946 | cam->frame_current = NULL; | ||
947 | cam->sof.bytesread = 0; | ||
948 | |||
949 | for (i = 0; i < SN9C102_URBS; i++) { | ||
950 | err = usb_submit_urb(cam->urb[i], GFP_KERNEL); | ||
951 | if (err) { | ||
952 | for (j = i-1; j >= 0; j--) | ||
953 | usb_kill_urb(cam->urb[j]); | ||
954 | DBG(1, "usb_submit_urb() failed, error %d", err); | ||
955 | goto free_urbs; | ||
956 | } | ||
957 | } | ||
958 | |||
959 | return 0; | ||
960 | |||
961 | free_urbs: | ||
962 | for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++) | ||
963 | usb_free_urb(cam->urb[i]); | ||
964 | |||
965 | free_buffers: | ||
966 | for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++) | ||
967 | kfree(cam->transfer_buffer[i]); | ||
968 | |||
969 | return err; | ||
970 | } | ||
971 | |||
972 | |||
973 | static int sn9c102_stop_transfer(struct sn9c102_device* cam) | ||
974 | { | ||
975 | struct usb_device *udev = cam->usbdev; | ||
976 | s8 i; | ||
977 | int err = 0; | ||
978 | |||
979 | if (cam->state & DEV_DISCONNECTED) | ||
980 | return 0; | ||
981 | |||
982 | for (i = SN9C102_URBS-1; i >= 0; i--) { | ||
983 | usb_kill_urb(cam->urb[i]); | ||
984 | usb_free_urb(cam->urb[i]); | ||
985 | kfree(cam->transfer_buffer[i]); | ||
986 | } | ||
987 | |||
988 | err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */ | ||
989 | if (err) | ||
990 | DBG(3, "usb_set_interface() failed"); | ||
991 | |||
992 | return err; | ||
993 | } | ||
994 | |||
995 | |||
996 | static int sn9c102_stream_interrupt(struct sn9c102_device* cam) | ||
997 | { | ||
998 | cam->stream = STREAM_INTERRUPT; | ||
999 | wait_event_timeout(cam->wait_stream, | ||
1000 | (cam->stream == STREAM_OFF) || | ||
1001 | (cam->state & DEV_DISCONNECTED), | ||
1002 | SN9C102_URB_TIMEOUT); | ||
1003 | if (cam->state & DEV_DISCONNECTED) | ||
1004 | return -ENODEV; | ||
1005 | else if (cam->stream != STREAM_OFF) { | ||
1006 | cam->state |= DEV_MISCONFIGURED; | ||
1007 | DBG(1, "URB timeout reached. The camera is misconfigured. " | ||
1008 | "To use it, close and open %s again.", | ||
1009 | video_device_node_name(cam->v4ldev)); | ||
1010 | return -EIO; | ||
1011 | } | ||
1012 | |||
1013 | return 0; | ||
1014 | } | ||
1015 | |||
1016 | /*****************************************************************************/ | ||
1017 | |||
1018 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
1019 | static u16 sn9c102_strtou16(const char* buff, size_t len, ssize_t* count) | ||
1020 | { | ||
1021 | char str[7]; | ||
1022 | char* endp; | ||
1023 | unsigned long val; | ||
1024 | |||
1025 | if (len < 6) { | ||
1026 | strncpy(str, buff, len); | ||
1027 | str[len] = '\0'; | ||
1028 | } else { | ||
1029 | strncpy(str, buff, 6); | ||
1030 | str[6] = '\0'; | ||
1031 | } | ||
1032 | |||
1033 | val = simple_strtoul(str, &endp, 0); | ||
1034 | |||
1035 | *count = 0; | ||
1036 | if (val <= 0xffff) | ||
1037 | *count = (ssize_t)(endp - str); | ||
1038 | if ((*count) && (len == *count+1) && (buff[*count] == '\n')) | ||
1039 | *count += 1; | ||
1040 | |||
1041 | return (u16)val; | ||
1042 | } | ||
1043 | |||
1044 | /* | ||
1045 | NOTE 1: being inside one of the following methods implies that the v4l | ||
1046 | device exists for sure (see kobjects and reference counters) | ||
1047 | NOTE 2: buffers are PAGE_SIZE long | ||
1048 | */ | ||
1049 | |||
1050 | static ssize_t sn9c102_show_reg(struct device* cd, | ||
1051 | struct device_attribute *attr, char* buf) | ||
1052 | { | ||
1053 | struct sn9c102_device* cam; | ||
1054 | ssize_t count; | ||
1055 | |||
1056 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) | ||
1057 | return -ERESTARTSYS; | ||
1058 | |||
1059 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1060 | if (!cam) { | ||
1061 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1062 | return -ENODEV; | ||
1063 | } | ||
1064 | |||
1065 | count = sprintf(buf, "%u\n", cam->sysfs.reg); | ||
1066 | |||
1067 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1068 | |||
1069 | return count; | ||
1070 | } | ||
1071 | |||
1072 | |||
1073 | static ssize_t | ||
1074 | sn9c102_store_reg(struct device* cd, struct device_attribute *attr, | ||
1075 | const char* buf, size_t len) | ||
1076 | { | ||
1077 | struct sn9c102_device* cam; | ||
1078 | u16 index; | ||
1079 | ssize_t count; | ||
1080 | |||
1081 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) | ||
1082 | return -ERESTARTSYS; | ||
1083 | |||
1084 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1085 | if (!cam) { | ||
1086 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1087 | return -ENODEV; | ||
1088 | } | ||
1089 | |||
1090 | index = sn9c102_strtou16(buf, len, &count); | ||
1091 | if (index >= ARRAY_SIZE(cam->reg) || !count) { | ||
1092 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1093 | return -EINVAL; | ||
1094 | } | ||
1095 | |||
1096 | cam->sysfs.reg = index; | ||
1097 | |||
1098 | DBG(2, "Moved SN9C1XX register index to 0x%02X", cam->sysfs.reg); | ||
1099 | DBG(3, "Written bytes: %zd", count); | ||
1100 | |||
1101 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1102 | |||
1103 | return count; | ||
1104 | } | ||
1105 | |||
1106 | |||
1107 | static ssize_t sn9c102_show_val(struct device* cd, | ||
1108 | struct device_attribute *attr, char* buf) | ||
1109 | { | ||
1110 | struct sn9c102_device* cam; | ||
1111 | ssize_t count; | ||
1112 | int val; | ||
1113 | |||
1114 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) | ||
1115 | return -ERESTARTSYS; | ||
1116 | |||
1117 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1118 | if (!cam) { | ||
1119 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1120 | return -ENODEV; | ||
1121 | } | ||
1122 | |||
1123 | if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) { | ||
1124 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1125 | return -EIO; | ||
1126 | } | ||
1127 | |||
1128 | count = sprintf(buf, "%d\n", val); | ||
1129 | |||
1130 | DBG(3, "Read bytes: %zd, value: %d", count, val); | ||
1131 | |||
1132 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1133 | |||
1134 | return count; | ||
1135 | } | ||
1136 | |||
1137 | |||
1138 | static ssize_t | ||
1139 | sn9c102_store_val(struct device* cd, struct device_attribute *attr, | ||
1140 | const char* buf, size_t len) | ||
1141 | { | ||
1142 | struct sn9c102_device* cam; | ||
1143 | u16 value; | ||
1144 | ssize_t count; | ||
1145 | int err; | ||
1146 | |||
1147 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) | ||
1148 | return -ERESTARTSYS; | ||
1149 | |||
1150 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1151 | if (!cam) { | ||
1152 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1153 | return -ENODEV; | ||
1154 | } | ||
1155 | |||
1156 | value = sn9c102_strtou16(buf, len, &count); | ||
1157 | if (!count) { | ||
1158 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1159 | return -EINVAL; | ||
1160 | } | ||
1161 | |||
1162 | err = sn9c102_write_reg(cam, value, cam->sysfs.reg); | ||
1163 | if (err) { | ||
1164 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1165 | return -EIO; | ||
1166 | } | ||
1167 | |||
1168 | DBG(2, "Written SN9C1XX reg. 0x%02X, val. 0x%02X", | ||
1169 | cam->sysfs.reg, value); | ||
1170 | DBG(3, "Written bytes: %zd", count); | ||
1171 | |||
1172 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1173 | |||
1174 | return count; | ||
1175 | } | ||
1176 | |||
1177 | |||
1178 | static ssize_t sn9c102_show_i2c_reg(struct device* cd, | ||
1179 | struct device_attribute *attr, char* buf) | ||
1180 | { | ||
1181 | struct sn9c102_device* cam; | ||
1182 | ssize_t count; | ||
1183 | |||
1184 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) | ||
1185 | return -ERESTARTSYS; | ||
1186 | |||
1187 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1188 | if (!cam) { | ||
1189 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1190 | return -ENODEV; | ||
1191 | } | ||
1192 | |||
1193 | count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg); | ||
1194 | |||
1195 | DBG(3, "Read bytes: %zd", count); | ||
1196 | |||
1197 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1198 | |||
1199 | return count; | ||
1200 | } | ||
1201 | |||
1202 | |||
1203 | static ssize_t | ||
1204 | sn9c102_store_i2c_reg(struct device* cd, struct device_attribute *attr, | ||
1205 | const char* buf, size_t len) | ||
1206 | { | ||
1207 | struct sn9c102_device* cam; | ||
1208 | u16 index; | ||
1209 | ssize_t count; | ||
1210 | |||
1211 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) | ||
1212 | return -ERESTARTSYS; | ||
1213 | |||
1214 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1215 | if (!cam) { | ||
1216 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1217 | return -ENODEV; | ||
1218 | } | ||
1219 | |||
1220 | index = sn9c102_strtou16(buf, len, &count); | ||
1221 | if (!count) { | ||
1222 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1223 | return -EINVAL; | ||
1224 | } | ||
1225 | |||
1226 | cam->sysfs.i2c_reg = index; | ||
1227 | |||
1228 | DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg); | ||
1229 | DBG(3, "Written bytes: %zd", count); | ||
1230 | |||
1231 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1232 | |||
1233 | return count; | ||
1234 | } | ||
1235 | |||
1236 | |||
1237 | static ssize_t sn9c102_show_i2c_val(struct device* cd, | ||
1238 | struct device_attribute *attr, char* buf) | ||
1239 | { | ||
1240 | struct sn9c102_device* cam; | ||
1241 | ssize_t count; | ||
1242 | int val; | ||
1243 | |||
1244 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) | ||
1245 | return -ERESTARTSYS; | ||
1246 | |||
1247 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1248 | if (!cam) { | ||
1249 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1250 | return -ENODEV; | ||
1251 | } | ||
1252 | |||
1253 | if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) { | ||
1254 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1255 | return -ENOSYS; | ||
1256 | } | ||
1257 | |||
1258 | if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) { | ||
1259 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1260 | return -EIO; | ||
1261 | } | ||
1262 | |||
1263 | count = sprintf(buf, "%d\n", val); | ||
1264 | |||
1265 | DBG(3, "Read bytes: %zd, value: %d", count, val); | ||
1266 | |||
1267 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1268 | |||
1269 | return count; | ||
1270 | } | ||
1271 | |||
1272 | |||
1273 | static ssize_t | ||
1274 | sn9c102_store_i2c_val(struct device* cd, struct device_attribute *attr, | ||
1275 | const char* buf, size_t len) | ||
1276 | { | ||
1277 | struct sn9c102_device* cam; | ||
1278 | u16 value; | ||
1279 | ssize_t count; | ||
1280 | int err; | ||
1281 | |||
1282 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) | ||
1283 | return -ERESTARTSYS; | ||
1284 | |||
1285 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1286 | if (!cam) { | ||
1287 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1288 | return -ENODEV; | ||
1289 | } | ||
1290 | |||
1291 | if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) { | ||
1292 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1293 | return -ENOSYS; | ||
1294 | } | ||
1295 | |||
1296 | value = sn9c102_strtou16(buf, len, &count); | ||
1297 | if (!count) { | ||
1298 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1299 | return -EINVAL; | ||
1300 | } | ||
1301 | |||
1302 | err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value); | ||
1303 | if (err) { | ||
1304 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1305 | return -EIO; | ||
1306 | } | ||
1307 | |||
1308 | DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X", | ||
1309 | cam->sysfs.i2c_reg, value); | ||
1310 | DBG(3, "Written bytes: %zd", count); | ||
1311 | |||
1312 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1313 | |||
1314 | return count; | ||
1315 | } | ||
1316 | |||
1317 | |||
1318 | static ssize_t | ||
1319 | sn9c102_store_green(struct device* cd, struct device_attribute *attr, | ||
1320 | const char* buf, size_t len) | ||
1321 | { | ||
1322 | struct sn9c102_device* cam; | ||
1323 | enum sn9c102_bridge bridge; | ||
1324 | ssize_t res = 0; | ||
1325 | u16 value; | ||
1326 | ssize_t count; | ||
1327 | |||
1328 | if (mutex_lock_interruptible(&sn9c102_sysfs_lock)) | ||
1329 | return -ERESTARTSYS; | ||
1330 | |||
1331 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1332 | if (!cam) { | ||
1333 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1334 | return -ENODEV; | ||
1335 | } | ||
1336 | |||
1337 | bridge = cam->bridge; | ||
1338 | |||
1339 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1340 | |||
1341 | value = sn9c102_strtou16(buf, len, &count); | ||
1342 | if (!count) | ||
1343 | return -EINVAL; | ||
1344 | |||
1345 | switch (bridge) { | ||
1346 | case BRIDGE_SN9C101: | ||
1347 | case BRIDGE_SN9C102: | ||
1348 | if (value > 0x0f) | ||
1349 | return -EINVAL; | ||
1350 | if ((res = sn9c102_store_reg(cd, attr, "0x11", 4)) >= 0) | ||
1351 | res = sn9c102_store_val(cd, attr, buf, len); | ||
1352 | break; | ||
1353 | case BRIDGE_SN9C103: | ||
1354 | case BRIDGE_SN9C105: | ||
1355 | case BRIDGE_SN9C120: | ||
1356 | if (value > 0x7f) | ||
1357 | return -EINVAL; | ||
1358 | if ((res = sn9c102_store_reg(cd, attr, "0x07", 4)) >= 0) | ||
1359 | res = sn9c102_store_val(cd, attr, buf, len); | ||
1360 | break; | ||
1361 | } | ||
1362 | |||
1363 | return res; | ||
1364 | } | ||
1365 | |||
1366 | |||
1367 | static ssize_t | ||
1368 | sn9c102_store_blue(struct device* cd, struct device_attribute *attr, | ||
1369 | const char* buf, size_t len) | ||
1370 | { | ||
1371 | ssize_t res = 0; | ||
1372 | u16 value; | ||
1373 | ssize_t count; | ||
1374 | |||
1375 | value = sn9c102_strtou16(buf, len, &count); | ||
1376 | if (!count || value > 0x7f) | ||
1377 | return -EINVAL; | ||
1378 | |||
1379 | if ((res = sn9c102_store_reg(cd, attr, "0x06", 4)) >= 0) | ||
1380 | res = sn9c102_store_val(cd, attr, buf, len); | ||
1381 | |||
1382 | return res; | ||
1383 | } | ||
1384 | |||
1385 | |||
1386 | static ssize_t | ||
1387 | sn9c102_store_red(struct device* cd, struct device_attribute *attr, | ||
1388 | const char* buf, size_t len) | ||
1389 | { | ||
1390 | ssize_t res = 0; | ||
1391 | u16 value; | ||
1392 | ssize_t count; | ||
1393 | |||
1394 | value = sn9c102_strtou16(buf, len, &count); | ||
1395 | if (!count || value > 0x7f) | ||
1396 | return -EINVAL; | ||
1397 | |||
1398 | if ((res = sn9c102_store_reg(cd, attr, "0x05", 4)) >= 0) | ||
1399 | res = sn9c102_store_val(cd, attr, buf, len); | ||
1400 | |||
1401 | return res; | ||
1402 | } | ||
1403 | |||
1404 | |||
1405 | static ssize_t sn9c102_show_frame_header(struct device* cd, | ||
1406 | struct device_attribute *attr, | ||
1407 | char* buf) | ||
1408 | { | ||
1409 | struct sn9c102_device* cam; | ||
1410 | ssize_t count; | ||
1411 | |||
1412 | cam = video_get_drvdata(container_of(cd, struct video_device, dev)); | ||
1413 | if (!cam) | ||
1414 | return -ENODEV; | ||
1415 | |||
1416 | count = sizeof(cam->sysfs.frame_header); | ||
1417 | memcpy(buf, cam->sysfs.frame_header, count); | ||
1418 | |||
1419 | DBG(3, "Frame header, read bytes: %zd", count); | ||
1420 | |||
1421 | return count; | ||
1422 | } | ||
1423 | |||
1424 | |||
1425 | static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR, sn9c102_show_reg, sn9c102_store_reg); | ||
1426 | static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, sn9c102_show_val, sn9c102_store_val); | ||
1427 | static DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR, | ||
1428 | sn9c102_show_i2c_reg, sn9c102_store_i2c_reg); | ||
1429 | static DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR, | ||
1430 | sn9c102_show_i2c_val, sn9c102_store_i2c_val); | ||
1431 | static DEVICE_ATTR(green, S_IWUSR, NULL, sn9c102_store_green); | ||
1432 | static DEVICE_ATTR(blue, S_IWUSR, NULL, sn9c102_store_blue); | ||
1433 | static DEVICE_ATTR(red, S_IWUSR, NULL, sn9c102_store_red); | ||
1434 | static DEVICE_ATTR(frame_header, S_IRUGO, sn9c102_show_frame_header, NULL); | ||
1435 | |||
1436 | |||
1437 | static int sn9c102_create_sysfs(struct sn9c102_device* cam) | ||
1438 | { | ||
1439 | struct device *dev = &(cam->v4ldev->dev); | ||
1440 | int err = 0; | ||
1441 | |||
1442 | if ((err = device_create_file(dev, &dev_attr_reg))) | ||
1443 | goto err_out; | ||
1444 | if ((err = device_create_file(dev, &dev_attr_val))) | ||
1445 | goto err_reg; | ||
1446 | if ((err = device_create_file(dev, &dev_attr_frame_header))) | ||
1447 | goto err_val; | ||
1448 | |||
1449 | if (cam->sensor.sysfs_ops) { | ||
1450 | if ((err = device_create_file(dev, &dev_attr_i2c_reg))) | ||
1451 | goto err_frame_header; | ||
1452 | if ((err = device_create_file(dev, &dev_attr_i2c_val))) | ||
1453 | goto err_i2c_reg; | ||
1454 | } | ||
1455 | |||
1456 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) { | ||
1457 | if ((err = device_create_file(dev, &dev_attr_green))) | ||
1458 | goto err_i2c_val; | ||
1459 | } else { | ||
1460 | if ((err = device_create_file(dev, &dev_attr_blue))) | ||
1461 | goto err_i2c_val; | ||
1462 | if ((err = device_create_file(dev, &dev_attr_red))) | ||
1463 | goto err_blue; | ||
1464 | } | ||
1465 | |||
1466 | return 0; | ||
1467 | |||
1468 | err_blue: | ||
1469 | device_remove_file(dev, &dev_attr_blue); | ||
1470 | err_i2c_val: | ||
1471 | if (cam->sensor.sysfs_ops) | ||
1472 | device_remove_file(dev, &dev_attr_i2c_val); | ||
1473 | err_i2c_reg: | ||
1474 | if (cam->sensor.sysfs_ops) | ||
1475 | device_remove_file(dev, &dev_attr_i2c_reg); | ||
1476 | err_frame_header: | ||
1477 | device_remove_file(dev, &dev_attr_frame_header); | ||
1478 | err_val: | ||
1479 | device_remove_file(dev, &dev_attr_val); | ||
1480 | err_reg: | ||
1481 | device_remove_file(dev, &dev_attr_reg); | ||
1482 | err_out: | ||
1483 | return err; | ||
1484 | } | ||
1485 | #endif /* CONFIG_VIDEO_ADV_DEBUG */ | ||
1486 | |||
1487 | /*****************************************************************************/ | ||
1488 | |||
1489 | static int | ||
1490 | sn9c102_set_pix_format(struct sn9c102_device* cam, struct v4l2_pix_format* pix) | ||
1491 | { | ||
1492 | int err = 0; | ||
1493 | |||
1494 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X || | ||
1495 | pix->pixelformat == V4L2_PIX_FMT_JPEG) { | ||
1496 | switch (cam->bridge) { | ||
1497 | case BRIDGE_SN9C101: | ||
1498 | case BRIDGE_SN9C102: | ||
1499 | case BRIDGE_SN9C103: | ||
1500 | err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, | ||
1501 | 0x18); | ||
1502 | break; | ||
1503 | case BRIDGE_SN9C105: | ||
1504 | case BRIDGE_SN9C120: | ||
1505 | err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, | ||
1506 | 0x18); | ||
1507 | break; | ||
1508 | } | ||
1509 | } else { | ||
1510 | switch (cam->bridge) { | ||
1511 | case BRIDGE_SN9C101: | ||
1512 | case BRIDGE_SN9C102: | ||
1513 | case BRIDGE_SN9C103: | ||
1514 | err += sn9c102_write_reg(cam, cam->reg[0x18] & 0x7f, | ||
1515 | 0x18); | ||
1516 | break; | ||
1517 | case BRIDGE_SN9C105: | ||
1518 | case BRIDGE_SN9C120: | ||
1519 | err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x80, | ||
1520 | 0x18); | ||
1521 | break; | ||
1522 | } | ||
1523 | } | ||
1524 | |||
1525 | return err ? -EIO : 0; | ||
1526 | } | ||
1527 | |||
1528 | |||
1529 | static int | ||
1530 | sn9c102_set_compression(struct sn9c102_device* cam, | ||
1531 | struct v4l2_jpegcompression* compression) | ||
1532 | { | ||
1533 | int i, err = 0; | ||
1534 | |||
1535 | switch (cam->bridge) { | ||
1536 | case BRIDGE_SN9C101: | ||
1537 | case BRIDGE_SN9C102: | ||
1538 | case BRIDGE_SN9C103: | ||
1539 | if (compression->quality == 0) | ||
1540 | err += sn9c102_write_reg(cam, cam->reg[0x17] | 0x01, | ||
1541 | 0x17); | ||
1542 | else if (compression->quality == 1) | ||
1543 | err += sn9c102_write_reg(cam, cam->reg[0x17] & 0xfe, | ||
1544 | 0x17); | ||
1545 | break; | ||
1546 | case BRIDGE_SN9C105: | ||
1547 | case BRIDGE_SN9C120: | ||
1548 | if (compression->quality == 0) { | ||
1549 | for (i = 0; i <= 63; i++) { | ||
1550 | err += sn9c102_write_reg(cam, | ||
1551 | SN9C102_Y_QTABLE1[i], | ||
1552 | 0x100 + i); | ||
1553 | err += sn9c102_write_reg(cam, | ||
1554 | SN9C102_UV_QTABLE1[i], | ||
1555 | 0x140 + i); | ||
1556 | } | ||
1557 | err += sn9c102_write_reg(cam, cam->reg[0x18] & 0xbf, | ||
1558 | 0x18); | ||
1559 | } else if (compression->quality == 1) { | ||
1560 | for (i = 0; i <= 63; i++) { | ||
1561 | err += sn9c102_write_reg(cam, | ||
1562 | SN9C102_Y_QTABLE1[i], | ||
1563 | 0x100 + i); | ||
1564 | err += sn9c102_write_reg(cam, | ||
1565 | SN9C102_UV_QTABLE1[i], | ||
1566 | 0x140 + i); | ||
1567 | } | ||
1568 | err += sn9c102_write_reg(cam, cam->reg[0x18] | 0x40, | ||
1569 | 0x18); | ||
1570 | } | ||
1571 | break; | ||
1572 | } | ||
1573 | |||
1574 | return err ? -EIO : 0; | ||
1575 | } | ||
1576 | |||
1577 | |||
1578 | static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale) | ||
1579 | { | ||
1580 | u8 r = 0; | ||
1581 | int err = 0; | ||
1582 | |||
1583 | if (scale == 1) | ||
1584 | r = cam->reg[0x18] & 0xcf; | ||
1585 | else if (scale == 2) { | ||
1586 | r = cam->reg[0x18] & 0xcf; | ||
1587 | r |= 0x10; | ||
1588 | } else if (scale == 4) | ||
1589 | r = cam->reg[0x18] | 0x20; | ||
1590 | |||
1591 | err += sn9c102_write_reg(cam, r, 0x18); | ||
1592 | if (err) | ||
1593 | return -EIO; | ||
1594 | |||
1595 | PDBGG("Scaling factor: %u", scale); | ||
1596 | |||
1597 | return 0; | ||
1598 | } | ||
1599 | |||
1600 | |||
1601 | static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect) | ||
1602 | { | ||
1603 | struct sn9c102_sensor* s = &cam->sensor; | ||
1604 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left), | ||
1605 | v_start = (u8)(rect->top - s->cropcap.bounds.top), | ||
1606 | h_size = (u8)(rect->width / 16), | ||
1607 | v_size = (u8)(rect->height / 16); | ||
1608 | int err = 0; | ||
1609 | |||
1610 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
1611 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
1612 | err += sn9c102_write_reg(cam, h_size, 0x15); | ||
1613 | err += sn9c102_write_reg(cam, v_size, 0x16); | ||
1614 | if (err) | ||
1615 | return -EIO; | ||
1616 | |||
1617 | PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size " | ||
1618 | "%u %u %u %u", h_start, v_start, h_size, v_size); | ||
1619 | |||
1620 | return 0; | ||
1621 | } | ||
1622 | |||
1623 | |||
1624 | static int sn9c102_init(struct sn9c102_device* cam) | ||
1625 | { | ||
1626 | struct sn9c102_sensor* s = &cam->sensor; | ||
1627 | struct v4l2_control ctrl; | ||
1628 | struct v4l2_queryctrl *qctrl; | ||
1629 | struct v4l2_rect* rect; | ||
1630 | u8 i = 0; | ||
1631 | int err = 0; | ||
1632 | |||
1633 | if (!(cam->state & DEV_INITIALIZED)) { | ||
1634 | mutex_init(&cam->open_mutex); | ||
1635 | init_waitqueue_head(&cam->wait_open); | ||
1636 | qctrl = s->qctrl; | ||
1637 | rect = &(s->cropcap.defrect); | ||
1638 | } else { /* use current values */ | ||
1639 | qctrl = s->_qctrl; | ||
1640 | rect = &(s->_rect); | ||
1641 | } | ||
1642 | |||
1643 | err += sn9c102_set_scale(cam, rect->width / s->pix_format.width); | ||
1644 | err += sn9c102_set_crop(cam, rect); | ||
1645 | if (err) | ||
1646 | return err; | ||
1647 | |||
1648 | if (s->init) { | ||
1649 | err = s->init(cam); | ||
1650 | if (err) { | ||
1651 | DBG(3, "Sensor initialization failed"); | ||
1652 | return err; | ||
1653 | } | ||
1654 | } | ||
1655 | |||
1656 | if (!(cam->state & DEV_INITIALIZED)) | ||
1657 | if (cam->bridge == BRIDGE_SN9C101 || | ||
1658 | cam->bridge == BRIDGE_SN9C102 || | ||
1659 | cam->bridge == BRIDGE_SN9C103) { | ||
1660 | if (s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG) | ||
1661 | s->pix_format.pixelformat= V4L2_PIX_FMT_SBGGR8; | ||
1662 | cam->compression.quality = cam->reg[0x17] & 0x01 ? | ||
1663 | 0 : 1; | ||
1664 | } else { | ||
1665 | if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X) | ||
1666 | s->pix_format.pixelformat = V4L2_PIX_FMT_JPEG; | ||
1667 | cam->compression.quality = cam->reg[0x18] & 0x40 ? | ||
1668 | 0 : 1; | ||
1669 | err += sn9c102_set_compression(cam, &cam->compression); | ||
1670 | } | ||
1671 | else | ||
1672 | err += sn9c102_set_compression(cam, &cam->compression); | ||
1673 | err += sn9c102_set_pix_format(cam, &s->pix_format); | ||
1674 | if (s->set_pix_format) | ||
1675 | err += s->set_pix_format(cam, &s->pix_format); | ||
1676 | if (err) | ||
1677 | return err; | ||
1678 | |||
1679 | if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X || | ||
1680 | s->pix_format.pixelformat == V4L2_PIX_FMT_JPEG) | ||
1681 | DBG(3, "Compressed video format is active, quality %d", | ||
1682 | cam->compression.quality); | ||
1683 | else | ||
1684 | DBG(3, "Uncompressed video format is active"); | ||
1685 | |||
1686 | if (s->set_crop) | ||
1687 | if ((err = s->set_crop(cam, rect))) { | ||
1688 | DBG(3, "set_crop() failed"); | ||
1689 | return err; | ||
1690 | } | ||
1691 | |||
1692 | if (s->set_ctrl) { | ||
1693 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) | ||
1694 | if (s->qctrl[i].id != 0 && | ||
1695 | !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) { | ||
1696 | ctrl.id = s->qctrl[i].id; | ||
1697 | ctrl.value = qctrl[i].default_value; | ||
1698 | err = s->set_ctrl(cam, &ctrl); | ||
1699 | if (err) { | ||
1700 | DBG(3, "Set %s control failed", | ||
1701 | s->qctrl[i].name); | ||
1702 | return err; | ||
1703 | } | ||
1704 | DBG(3, "Image sensor supports '%s' control", | ||
1705 | s->qctrl[i].name); | ||
1706 | } | ||
1707 | } | ||
1708 | |||
1709 | if (!(cam->state & DEV_INITIALIZED)) { | ||
1710 | mutex_init(&cam->fileop_mutex); | ||
1711 | spin_lock_init(&cam->queue_lock); | ||
1712 | init_waitqueue_head(&cam->wait_frame); | ||
1713 | init_waitqueue_head(&cam->wait_stream); | ||
1714 | cam->nreadbuffers = 2; | ||
1715 | memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl)); | ||
1716 | memcpy(&(s->_rect), &(s->cropcap.defrect), | ||
1717 | sizeof(struct v4l2_rect)); | ||
1718 | cam->state |= DEV_INITIALIZED; | ||
1719 | } | ||
1720 | |||
1721 | DBG(2, "Initialization succeeded"); | ||
1722 | return 0; | ||
1723 | } | ||
1724 | |||
1725 | /*****************************************************************************/ | ||
1726 | |||
1727 | static void sn9c102_release_resources(struct kref *kref) | ||
1728 | { | ||
1729 | struct sn9c102_device *cam; | ||
1730 | |||
1731 | mutex_lock(&sn9c102_sysfs_lock); | ||
1732 | |||
1733 | cam = container_of(kref, struct sn9c102_device, kref); | ||
1734 | |||
1735 | DBG(2, "V4L2 device %s deregistered", | ||
1736 | video_device_node_name(cam->v4ldev)); | ||
1737 | video_set_drvdata(cam->v4ldev, NULL); | ||
1738 | video_unregister_device(cam->v4ldev); | ||
1739 | usb_put_dev(cam->usbdev); | ||
1740 | kfree(cam->control_buffer); | ||
1741 | kfree(cam); | ||
1742 | |||
1743 | mutex_unlock(&sn9c102_sysfs_lock); | ||
1744 | |||
1745 | } | ||
1746 | |||
1747 | |||
1748 | static int sn9c102_open(struct file *filp) | ||
1749 | { | ||
1750 | struct sn9c102_device* cam; | ||
1751 | int err = 0; | ||
1752 | |||
1753 | /* | ||
1754 | A read_trylock() in open() is the only safe way to prevent race | ||
1755 | conditions with disconnect(), one close() and multiple (not | ||
1756 | necessarily simultaneous) attempts to open(). For example, it | ||
1757 | prevents from waiting for a second access, while the device | ||
1758 | structure is being deallocated, after a possible disconnect() and | ||
1759 | during a following close() holding the write lock: given that, after | ||
1760 | this deallocation, no access will be possible anymore, using the | ||
1761 | non-trylock version would have let open() gain the access to the | ||
1762 | device structure improperly. | ||
1763 | For this reason the lock must also not be per-device. | ||
1764 | */ | ||
1765 | if (!down_read_trylock(&sn9c102_dev_lock)) | ||
1766 | return -ERESTARTSYS; | ||
1767 | |||
1768 | cam = video_drvdata(filp); | ||
1769 | |||
1770 | if (wait_for_completion_interruptible(&cam->probe)) { | ||
1771 | up_read(&sn9c102_dev_lock); | ||
1772 | return -ERESTARTSYS; | ||
1773 | } | ||
1774 | |||
1775 | kref_get(&cam->kref); | ||
1776 | |||
1777 | /* | ||
1778 | Make sure to isolate all the simultaneous opens. | ||
1779 | */ | ||
1780 | if (mutex_lock_interruptible(&cam->open_mutex)) { | ||
1781 | kref_put(&cam->kref, sn9c102_release_resources); | ||
1782 | up_read(&sn9c102_dev_lock); | ||
1783 | return -ERESTARTSYS; | ||
1784 | } | ||
1785 | |||
1786 | if (cam->state & DEV_DISCONNECTED) { | ||
1787 | DBG(1, "Device not present"); | ||
1788 | err = -ENODEV; | ||
1789 | goto out; | ||
1790 | } | ||
1791 | |||
1792 | if (cam->users) { | ||
1793 | DBG(2, "Device %s is already in use", | ||
1794 | video_device_node_name(cam->v4ldev)); | ||
1795 | DBG(3, "Simultaneous opens are not supported"); | ||
1796 | /* | ||
1797 | open() must follow the open flags and should block | ||
1798 | eventually while the device is in use. | ||
1799 | */ | ||
1800 | if ((filp->f_flags & O_NONBLOCK) || | ||
1801 | (filp->f_flags & O_NDELAY)) { | ||
1802 | err = -EWOULDBLOCK; | ||
1803 | goto out; | ||
1804 | } | ||
1805 | DBG(2, "A blocking open() has been requested. Wait for the " | ||
1806 | "device to be released..."); | ||
1807 | up_read(&sn9c102_dev_lock); | ||
1808 | /* | ||
1809 | We will not release the "open_mutex" lock, so that only one | ||
1810 | process can be in the wait queue below. This way the process | ||
1811 | will be sleeping while holding the lock, without losing its | ||
1812 | priority after any wake_up(). | ||
1813 | */ | ||
1814 | err = wait_event_interruptible_exclusive(cam->wait_open, | ||
1815 | (cam->state & DEV_DISCONNECTED) | ||
1816 | || !cam->users); | ||
1817 | down_read(&sn9c102_dev_lock); | ||
1818 | if (err) | ||
1819 | goto out; | ||
1820 | if (cam->state & DEV_DISCONNECTED) { | ||
1821 | err = -ENODEV; | ||
1822 | goto out; | ||
1823 | } | ||
1824 | } | ||
1825 | |||
1826 | if (cam->state & DEV_MISCONFIGURED) { | ||
1827 | err = sn9c102_init(cam); | ||
1828 | if (err) { | ||
1829 | DBG(1, "Initialization failed again. " | ||
1830 | "I will retry on next open()."); | ||
1831 | goto out; | ||
1832 | } | ||
1833 | cam->state &= ~DEV_MISCONFIGURED; | ||
1834 | } | ||
1835 | |||
1836 | if ((err = sn9c102_start_transfer(cam))) | ||
1837 | goto out; | ||
1838 | |||
1839 | filp->private_data = cam; | ||
1840 | cam->users++; | ||
1841 | cam->io = IO_NONE; | ||
1842 | cam->stream = STREAM_OFF; | ||
1843 | cam->nbuffers = 0; | ||
1844 | cam->frame_count = 0; | ||
1845 | sn9c102_empty_framequeues(cam); | ||
1846 | |||
1847 | DBG(3, "Video device %s is open", video_device_node_name(cam->v4ldev)); | ||
1848 | |||
1849 | out: | ||
1850 | mutex_unlock(&cam->open_mutex); | ||
1851 | if (err) | ||
1852 | kref_put(&cam->kref, sn9c102_release_resources); | ||
1853 | |||
1854 | up_read(&sn9c102_dev_lock); | ||
1855 | return err; | ||
1856 | } | ||
1857 | |||
1858 | |||
1859 | static int sn9c102_release(struct file *filp) | ||
1860 | { | ||
1861 | struct sn9c102_device* cam; | ||
1862 | |||
1863 | down_write(&sn9c102_dev_lock); | ||
1864 | |||
1865 | cam = video_drvdata(filp); | ||
1866 | |||
1867 | sn9c102_stop_transfer(cam); | ||
1868 | sn9c102_release_buffers(cam); | ||
1869 | cam->users--; | ||
1870 | wake_up_interruptible_nr(&cam->wait_open, 1); | ||
1871 | |||
1872 | DBG(3, "Video device %s closed", video_device_node_name(cam->v4ldev)); | ||
1873 | |||
1874 | kref_put(&cam->kref, sn9c102_release_resources); | ||
1875 | |||
1876 | up_write(&sn9c102_dev_lock); | ||
1877 | |||
1878 | return 0; | ||
1879 | } | ||
1880 | |||
1881 | |||
1882 | static ssize_t | ||
1883 | sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos) | ||
1884 | { | ||
1885 | struct sn9c102_device *cam = video_drvdata(filp); | ||
1886 | struct sn9c102_frame_t* f, * i; | ||
1887 | unsigned long lock_flags; | ||
1888 | long timeout; | ||
1889 | int err = 0; | ||
1890 | |||
1891 | if (mutex_lock_interruptible(&cam->fileop_mutex)) | ||
1892 | return -ERESTARTSYS; | ||
1893 | |||
1894 | if (cam->state & DEV_DISCONNECTED) { | ||
1895 | DBG(1, "Device not present"); | ||
1896 | mutex_unlock(&cam->fileop_mutex); | ||
1897 | return -ENODEV; | ||
1898 | } | ||
1899 | |||
1900 | if (cam->state & DEV_MISCONFIGURED) { | ||
1901 | DBG(1, "The camera is misconfigured. Close and open it " | ||
1902 | "again."); | ||
1903 | mutex_unlock(&cam->fileop_mutex); | ||
1904 | return -EIO; | ||
1905 | } | ||
1906 | |||
1907 | if (cam->io == IO_MMAP) { | ||
1908 | DBG(3, "Close and open the device again to choose " | ||
1909 | "the read method"); | ||
1910 | mutex_unlock(&cam->fileop_mutex); | ||
1911 | return -EBUSY; | ||
1912 | } | ||
1913 | |||
1914 | if (cam->io == IO_NONE) { | ||
1915 | if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) { | ||
1916 | DBG(1, "read() failed, not enough memory"); | ||
1917 | mutex_unlock(&cam->fileop_mutex); | ||
1918 | return -ENOMEM; | ||
1919 | } | ||
1920 | cam->io = IO_READ; | ||
1921 | cam->stream = STREAM_ON; | ||
1922 | } | ||
1923 | |||
1924 | if (list_empty(&cam->inqueue)) { | ||
1925 | if (!list_empty(&cam->outqueue)) | ||
1926 | sn9c102_empty_framequeues(cam); | ||
1927 | sn9c102_queue_unusedframes(cam); | ||
1928 | } | ||
1929 | |||
1930 | if (!count) { | ||
1931 | mutex_unlock(&cam->fileop_mutex); | ||
1932 | return 0; | ||
1933 | } | ||
1934 | |||
1935 | if (list_empty(&cam->outqueue)) { | ||
1936 | if (filp->f_flags & O_NONBLOCK) { | ||
1937 | mutex_unlock(&cam->fileop_mutex); | ||
1938 | return -EAGAIN; | ||
1939 | } | ||
1940 | if (!cam->module_param.frame_timeout) { | ||
1941 | err = wait_event_interruptible | ||
1942 | ( cam->wait_frame, | ||
1943 | (!list_empty(&cam->outqueue)) || | ||
1944 | (cam->state & DEV_DISCONNECTED) || | ||
1945 | (cam->state & DEV_MISCONFIGURED) ); | ||
1946 | if (err) { | ||
1947 | mutex_unlock(&cam->fileop_mutex); | ||
1948 | return err; | ||
1949 | } | ||
1950 | } else { | ||
1951 | timeout = wait_event_interruptible_timeout | ||
1952 | ( cam->wait_frame, | ||
1953 | (!list_empty(&cam->outqueue)) || | ||
1954 | (cam->state & DEV_DISCONNECTED) || | ||
1955 | (cam->state & DEV_MISCONFIGURED), | ||
1956 | msecs_to_jiffies( | ||
1957 | cam->module_param.frame_timeout * 1000 | ||
1958 | ) | ||
1959 | ); | ||
1960 | if (timeout < 0) { | ||
1961 | mutex_unlock(&cam->fileop_mutex); | ||
1962 | return timeout; | ||
1963 | } else if (timeout == 0 && | ||
1964 | !(cam->state & DEV_DISCONNECTED)) { | ||
1965 | DBG(1, "Video frame timeout elapsed"); | ||
1966 | mutex_unlock(&cam->fileop_mutex); | ||
1967 | return -EIO; | ||
1968 | } | ||
1969 | } | ||
1970 | if (cam->state & DEV_DISCONNECTED) { | ||
1971 | mutex_unlock(&cam->fileop_mutex); | ||
1972 | return -ENODEV; | ||
1973 | } | ||
1974 | if (cam->state & DEV_MISCONFIGURED) { | ||
1975 | mutex_unlock(&cam->fileop_mutex); | ||
1976 | return -EIO; | ||
1977 | } | ||
1978 | } | ||
1979 | |||
1980 | f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame); | ||
1981 | |||
1982 | if (count > f->buf.bytesused) | ||
1983 | count = f->buf.bytesused; | ||
1984 | |||
1985 | if (copy_to_user(buf, f->bufmem, count)) { | ||
1986 | err = -EFAULT; | ||
1987 | goto exit; | ||
1988 | } | ||
1989 | *f_pos += count; | ||
1990 | |||
1991 | exit: | ||
1992 | spin_lock_irqsave(&cam->queue_lock, lock_flags); | ||
1993 | list_for_each_entry(i, &cam->outqueue, frame) | ||
1994 | i->state = F_UNUSED; | ||
1995 | INIT_LIST_HEAD(&cam->outqueue); | ||
1996 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | ||
1997 | |||
1998 | sn9c102_queue_unusedframes(cam); | ||
1999 | |||
2000 | PDBGG("Frame #%lu, bytes read: %zu", | ||
2001 | (unsigned long)f->buf.index, count); | ||
2002 | |||
2003 | mutex_unlock(&cam->fileop_mutex); | ||
2004 | |||
2005 | return count; | ||
2006 | } | ||
2007 | |||
2008 | |||
2009 | static unsigned int sn9c102_poll(struct file *filp, poll_table *wait) | ||
2010 | { | ||
2011 | struct sn9c102_device *cam = video_drvdata(filp); | ||
2012 | struct sn9c102_frame_t* f; | ||
2013 | unsigned long lock_flags; | ||
2014 | unsigned int mask = 0; | ||
2015 | |||
2016 | if (mutex_lock_interruptible(&cam->fileop_mutex)) | ||
2017 | return POLLERR; | ||
2018 | |||
2019 | if (cam->state & DEV_DISCONNECTED) { | ||
2020 | DBG(1, "Device not present"); | ||
2021 | goto error; | ||
2022 | } | ||
2023 | |||
2024 | if (cam->state & DEV_MISCONFIGURED) { | ||
2025 | DBG(1, "The camera is misconfigured. Close and open it " | ||
2026 | "again."); | ||
2027 | goto error; | ||
2028 | } | ||
2029 | |||
2030 | if (cam->io == IO_NONE) { | ||
2031 | if (!sn9c102_request_buffers(cam, cam->nreadbuffers, | ||
2032 | IO_READ)) { | ||
2033 | DBG(1, "poll() failed, not enough memory"); | ||
2034 | goto error; | ||
2035 | } | ||
2036 | cam->io = IO_READ; | ||
2037 | cam->stream = STREAM_ON; | ||
2038 | } | ||
2039 | |||
2040 | if (cam->io == IO_READ) { | ||
2041 | spin_lock_irqsave(&cam->queue_lock, lock_flags); | ||
2042 | list_for_each_entry(f, &cam->outqueue, frame) | ||
2043 | f->state = F_UNUSED; | ||
2044 | INIT_LIST_HEAD(&cam->outqueue); | ||
2045 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | ||
2046 | sn9c102_queue_unusedframes(cam); | ||
2047 | } | ||
2048 | |||
2049 | poll_wait(filp, &cam->wait_frame, wait); | ||
2050 | |||
2051 | if (!list_empty(&cam->outqueue)) | ||
2052 | mask |= POLLIN | POLLRDNORM; | ||
2053 | |||
2054 | mutex_unlock(&cam->fileop_mutex); | ||
2055 | |||
2056 | return mask; | ||
2057 | |||
2058 | error: | ||
2059 | mutex_unlock(&cam->fileop_mutex); | ||
2060 | return POLLERR; | ||
2061 | } | ||
2062 | |||
2063 | |||
2064 | static void sn9c102_vm_open(struct vm_area_struct* vma) | ||
2065 | { | ||
2066 | struct sn9c102_frame_t* f = vma->vm_private_data; | ||
2067 | f->vma_use_count++; | ||
2068 | } | ||
2069 | |||
2070 | |||
2071 | static void sn9c102_vm_close(struct vm_area_struct* vma) | ||
2072 | { | ||
2073 | /* NOTE: buffers are not freed here */ | ||
2074 | struct sn9c102_frame_t* f = vma->vm_private_data; | ||
2075 | f->vma_use_count--; | ||
2076 | } | ||
2077 | |||
2078 | |||
2079 | static const struct vm_operations_struct sn9c102_vm_ops = { | ||
2080 | .open = sn9c102_vm_open, | ||
2081 | .close = sn9c102_vm_close, | ||
2082 | }; | ||
2083 | |||
2084 | |||
2085 | static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma) | ||
2086 | { | ||
2087 | struct sn9c102_device *cam = video_drvdata(filp); | ||
2088 | unsigned long size = vma->vm_end - vma->vm_start, | ||
2089 | start = vma->vm_start; | ||
2090 | void *pos; | ||
2091 | u32 i; | ||
2092 | |||
2093 | if (mutex_lock_interruptible(&cam->fileop_mutex)) | ||
2094 | return -ERESTARTSYS; | ||
2095 | |||
2096 | if (cam->state & DEV_DISCONNECTED) { | ||
2097 | DBG(1, "Device not present"); | ||
2098 | mutex_unlock(&cam->fileop_mutex); | ||
2099 | return -ENODEV; | ||
2100 | } | ||
2101 | |||
2102 | if (cam->state & DEV_MISCONFIGURED) { | ||
2103 | DBG(1, "The camera is misconfigured. Close and open it " | ||
2104 | "again."); | ||
2105 | mutex_unlock(&cam->fileop_mutex); | ||
2106 | return -EIO; | ||
2107 | } | ||
2108 | |||
2109 | if (!(vma->vm_flags & (VM_WRITE | VM_READ))) { | ||
2110 | mutex_unlock(&cam->fileop_mutex); | ||
2111 | return -EACCES; | ||
2112 | } | ||
2113 | |||
2114 | if (cam->io != IO_MMAP || | ||
2115 | size != PAGE_ALIGN(cam->frame[0].buf.length)) { | ||
2116 | mutex_unlock(&cam->fileop_mutex); | ||
2117 | return -EINVAL; | ||
2118 | } | ||
2119 | |||
2120 | for (i = 0; i < cam->nbuffers; i++) { | ||
2121 | if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff) | ||
2122 | break; | ||
2123 | } | ||
2124 | if (i == cam->nbuffers) { | ||
2125 | mutex_unlock(&cam->fileop_mutex); | ||
2126 | return -EINVAL; | ||
2127 | } | ||
2128 | |||
2129 | vma->vm_flags |= VM_IO; | ||
2130 | vma->vm_flags |= VM_RESERVED; | ||
2131 | |||
2132 | pos = cam->frame[i].bufmem; | ||
2133 | while (size > 0) { /* size is page-aligned */ | ||
2134 | if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { | ||
2135 | mutex_unlock(&cam->fileop_mutex); | ||
2136 | return -EAGAIN; | ||
2137 | } | ||
2138 | start += PAGE_SIZE; | ||
2139 | pos += PAGE_SIZE; | ||
2140 | size -= PAGE_SIZE; | ||
2141 | } | ||
2142 | |||
2143 | vma->vm_ops = &sn9c102_vm_ops; | ||
2144 | vma->vm_private_data = &cam->frame[i]; | ||
2145 | sn9c102_vm_open(vma); | ||
2146 | |||
2147 | mutex_unlock(&cam->fileop_mutex); | ||
2148 | |||
2149 | return 0; | ||
2150 | } | ||
2151 | |||
2152 | /*****************************************************************************/ | ||
2153 | |||
2154 | static int | ||
2155 | sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg) | ||
2156 | { | ||
2157 | struct v4l2_capability cap = { | ||
2158 | .driver = "sn9c102", | ||
2159 | .version = LINUX_VERSION_CODE, | ||
2160 | .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | | ||
2161 | V4L2_CAP_STREAMING, | ||
2162 | }; | ||
2163 | |||
2164 | strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card)); | ||
2165 | if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0) | ||
2166 | strlcpy(cap.bus_info, dev_name(&cam->usbdev->dev), | ||
2167 | sizeof(cap.bus_info)); | ||
2168 | |||
2169 | if (copy_to_user(arg, &cap, sizeof(cap))) | ||
2170 | return -EFAULT; | ||
2171 | |||
2172 | return 0; | ||
2173 | } | ||
2174 | |||
2175 | |||
2176 | static int | ||
2177 | sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg) | ||
2178 | { | ||
2179 | struct v4l2_input i; | ||
2180 | |||
2181 | if (copy_from_user(&i, arg, sizeof(i))) | ||
2182 | return -EFAULT; | ||
2183 | |||
2184 | if (i.index) | ||
2185 | return -EINVAL; | ||
2186 | |||
2187 | memset(&i, 0, sizeof(i)); | ||
2188 | strcpy(i.name, "Camera"); | ||
2189 | i.type = V4L2_INPUT_TYPE_CAMERA; | ||
2190 | i.capabilities = V4L2_IN_CAP_STD; | ||
2191 | |||
2192 | if (copy_to_user(arg, &i, sizeof(i))) | ||
2193 | return -EFAULT; | ||
2194 | |||
2195 | return 0; | ||
2196 | } | ||
2197 | |||
2198 | |||
2199 | static int | ||
2200 | sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg) | ||
2201 | { | ||
2202 | int index = 0; | ||
2203 | |||
2204 | if (copy_to_user(arg, &index, sizeof(index))) | ||
2205 | return -EFAULT; | ||
2206 | |||
2207 | return 0; | ||
2208 | } | ||
2209 | |||
2210 | |||
2211 | static int | ||
2212 | sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg) | ||
2213 | { | ||
2214 | int index; | ||
2215 | |||
2216 | if (copy_from_user(&index, arg, sizeof(index))) | ||
2217 | return -EFAULT; | ||
2218 | |||
2219 | if (index != 0) | ||
2220 | return -EINVAL; | ||
2221 | |||
2222 | return 0; | ||
2223 | } | ||
2224 | |||
2225 | |||
2226 | static int | ||
2227 | sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg) | ||
2228 | { | ||
2229 | struct sn9c102_sensor* s = &cam->sensor; | ||
2230 | struct v4l2_queryctrl qc; | ||
2231 | u8 i; | ||
2232 | |||
2233 | if (copy_from_user(&qc, arg, sizeof(qc))) | ||
2234 | return -EFAULT; | ||
2235 | |||
2236 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) | ||
2237 | if (qc.id && qc.id == s->qctrl[i].id) { | ||
2238 | memcpy(&qc, &(s->qctrl[i]), sizeof(qc)); | ||
2239 | if (copy_to_user(arg, &qc, sizeof(qc))) | ||
2240 | return -EFAULT; | ||
2241 | return 0; | ||
2242 | } | ||
2243 | |||
2244 | return -EINVAL; | ||
2245 | } | ||
2246 | |||
2247 | |||
2248 | static int | ||
2249 | sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg) | ||
2250 | { | ||
2251 | struct sn9c102_sensor* s = &cam->sensor; | ||
2252 | struct v4l2_control ctrl; | ||
2253 | int err = 0; | ||
2254 | u8 i; | ||
2255 | |||
2256 | if (!s->get_ctrl && !s->set_ctrl) | ||
2257 | return -EINVAL; | ||
2258 | |||
2259 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) | ||
2260 | return -EFAULT; | ||
2261 | |||
2262 | if (!s->get_ctrl) { | ||
2263 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) | ||
2264 | if (ctrl.id && ctrl.id == s->qctrl[i].id) { | ||
2265 | ctrl.value = s->_qctrl[i].default_value; | ||
2266 | goto exit; | ||
2267 | } | ||
2268 | return -EINVAL; | ||
2269 | } else | ||
2270 | err = s->get_ctrl(cam, &ctrl); | ||
2271 | |||
2272 | exit: | ||
2273 | if (copy_to_user(arg, &ctrl, sizeof(ctrl))) | ||
2274 | return -EFAULT; | ||
2275 | |||
2276 | PDBGG("VIDIOC_G_CTRL: id %lu, value %lu", | ||
2277 | (unsigned long)ctrl.id, (unsigned long)ctrl.value); | ||
2278 | |||
2279 | return err; | ||
2280 | } | ||
2281 | |||
2282 | |||
2283 | static int | ||
2284 | sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg) | ||
2285 | { | ||
2286 | struct sn9c102_sensor* s = &cam->sensor; | ||
2287 | struct v4l2_control ctrl; | ||
2288 | u8 i; | ||
2289 | int err = 0; | ||
2290 | |||
2291 | if (!s->set_ctrl) | ||
2292 | return -EINVAL; | ||
2293 | |||
2294 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) | ||
2295 | return -EFAULT; | ||
2296 | |||
2297 | for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) { | ||
2298 | if (ctrl.id == s->qctrl[i].id) { | ||
2299 | if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED) | ||
2300 | return -EINVAL; | ||
2301 | if (ctrl.value < s->qctrl[i].minimum || | ||
2302 | ctrl.value > s->qctrl[i].maximum) | ||
2303 | return -ERANGE; | ||
2304 | ctrl.value -= ctrl.value % s->qctrl[i].step; | ||
2305 | break; | ||
2306 | } | ||
2307 | } | ||
2308 | if (i == ARRAY_SIZE(s->qctrl)) | ||
2309 | return -EINVAL; | ||
2310 | if ((err = s->set_ctrl(cam, &ctrl))) | ||
2311 | return err; | ||
2312 | |||
2313 | s->_qctrl[i].default_value = ctrl.value; | ||
2314 | |||
2315 | PDBGG("VIDIOC_S_CTRL: id %lu, value %lu", | ||
2316 | (unsigned long)ctrl.id, (unsigned long)ctrl.value); | ||
2317 | |||
2318 | return 0; | ||
2319 | } | ||
2320 | |||
2321 | |||
2322 | static int | ||
2323 | sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg) | ||
2324 | { | ||
2325 | struct v4l2_cropcap* cc = &(cam->sensor.cropcap); | ||
2326 | |||
2327 | cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
2328 | cc->pixelaspect.numerator = 1; | ||
2329 | cc->pixelaspect.denominator = 1; | ||
2330 | |||
2331 | if (copy_to_user(arg, cc, sizeof(*cc))) | ||
2332 | return -EFAULT; | ||
2333 | |||
2334 | return 0; | ||
2335 | } | ||
2336 | |||
2337 | |||
2338 | static int | ||
2339 | sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg) | ||
2340 | { | ||
2341 | struct sn9c102_sensor* s = &cam->sensor; | ||
2342 | struct v4l2_crop crop = { | ||
2343 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, | ||
2344 | }; | ||
2345 | |||
2346 | memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect)); | ||
2347 | |||
2348 | if (copy_to_user(arg, &crop, sizeof(crop))) | ||
2349 | return -EFAULT; | ||
2350 | |||
2351 | return 0; | ||
2352 | } | ||
2353 | |||
2354 | |||
2355 | static int | ||
2356 | sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg) | ||
2357 | { | ||
2358 | struct sn9c102_sensor* s = &cam->sensor; | ||
2359 | struct v4l2_crop crop; | ||
2360 | struct v4l2_rect* rect; | ||
2361 | struct v4l2_rect* bounds = &(s->cropcap.bounds); | ||
2362 | struct v4l2_pix_format* pix_format = &(s->pix_format); | ||
2363 | u8 scale; | ||
2364 | const enum sn9c102_stream_state stream = cam->stream; | ||
2365 | const u32 nbuffers = cam->nbuffers; | ||
2366 | u32 i; | ||
2367 | int err = 0; | ||
2368 | |||
2369 | if (copy_from_user(&crop, arg, sizeof(crop))) | ||
2370 | return -EFAULT; | ||
2371 | |||
2372 | rect = &(crop.c); | ||
2373 | |||
2374 | if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
2375 | return -EINVAL; | ||
2376 | |||
2377 | if (cam->module_param.force_munmap) | ||
2378 | for (i = 0; i < cam->nbuffers; i++) | ||
2379 | if (cam->frame[i].vma_use_count) { | ||
2380 | DBG(3, "VIDIOC_S_CROP failed. " | ||
2381 | "Unmap the buffers first."); | ||
2382 | return -EBUSY; | ||
2383 | } | ||
2384 | |||
2385 | /* Preserve R,G or B origin */ | ||
2386 | rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L; | ||
2387 | rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L; | ||
2388 | |||
2389 | if (rect->width < 16) | ||
2390 | rect->width = 16; | ||
2391 | if (rect->height < 16) | ||
2392 | rect->height = 16; | ||
2393 | if (rect->width > bounds->width) | ||
2394 | rect->width = bounds->width; | ||
2395 | if (rect->height > bounds->height) | ||
2396 | rect->height = bounds->height; | ||
2397 | if (rect->left < bounds->left) | ||
2398 | rect->left = bounds->left; | ||
2399 | if (rect->top < bounds->top) | ||
2400 | rect->top = bounds->top; | ||
2401 | if (rect->left + rect->width > bounds->left + bounds->width) | ||
2402 | rect->left = bounds->left+bounds->width - rect->width; | ||
2403 | if (rect->top + rect->height > bounds->top + bounds->height) | ||
2404 | rect->top = bounds->top+bounds->height - rect->height; | ||
2405 | |||
2406 | rect->width &= ~15L; | ||
2407 | rect->height &= ~15L; | ||
2408 | |||
2409 | if (SN9C102_PRESERVE_IMGSCALE) { | ||
2410 | /* Calculate the actual scaling factor */ | ||
2411 | u32 a, b; | ||
2412 | a = rect->width * rect->height; | ||
2413 | b = pix_format->width * pix_format->height; | ||
2414 | scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1; | ||
2415 | } else | ||
2416 | scale = 1; | ||
2417 | |||
2418 | if (cam->stream == STREAM_ON) | ||
2419 | if ((err = sn9c102_stream_interrupt(cam))) | ||
2420 | return err; | ||
2421 | |||
2422 | if (copy_to_user(arg, &crop, sizeof(crop))) { | ||
2423 | cam->stream = stream; | ||
2424 | return -EFAULT; | ||
2425 | } | ||
2426 | |||
2427 | if (cam->module_param.force_munmap || cam->io == IO_READ) | ||
2428 | sn9c102_release_buffers(cam); | ||
2429 | |||
2430 | err = sn9c102_set_crop(cam, rect); | ||
2431 | if (s->set_crop) | ||
2432 | err += s->set_crop(cam, rect); | ||
2433 | err += sn9c102_set_scale(cam, scale); | ||
2434 | |||
2435 | if (err) { /* atomic, no rollback in ioctl() */ | ||
2436 | cam->state |= DEV_MISCONFIGURED; | ||
2437 | DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To " | ||
2438 | "use the camera, close and open %s again.", | ||
2439 | video_device_node_name(cam->v4ldev)); | ||
2440 | return -EIO; | ||
2441 | } | ||
2442 | |||
2443 | s->pix_format.width = rect->width/scale; | ||
2444 | s->pix_format.height = rect->height/scale; | ||
2445 | memcpy(&(s->_rect), rect, sizeof(*rect)); | ||
2446 | |||
2447 | if ((cam->module_param.force_munmap || cam->io == IO_READ) && | ||
2448 | nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) { | ||
2449 | cam->state |= DEV_MISCONFIGURED; | ||
2450 | DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To " | ||
2451 | "use the camera, close and open %s again.", | ||
2452 | video_device_node_name(cam->v4ldev)); | ||
2453 | return -ENOMEM; | ||
2454 | } | ||
2455 | |||
2456 | if (cam->io == IO_READ) | ||
2457 | sn9c102_empty_framequeues(cam); | ||
2458 | else if (cam->module_param.force_munmap) | ||
2459 | sn9c102_requeue_outqueue(cam); | ||
2460 | |||
2461 | cam->stream = stream; | ||
2462 | |||
2463 | return 0; | ||
2464 | } | ||
2465 | |||
2466 | |||
2467 | static int | ||
2468 | sn9c102_vidioc_enum_framesizes(struct sn9c102_device* cam, void __user * arg) | ||
2469 | { | ||
2470 | struct v4l2_frmsizeenum frmsize; | ||
2471 | |||
2472 | if (copy_from_user(&frmsize, arg, sizeof(frmsize))) | ||
2473 | return -EFAULT; | ||
2474 | |||
2475 | if (frmsize.index != 0) | ||
2476 | return -EINVAL; | ||
2477 | |||
2478 | switch (cam->bridge) { | ||
2479 | case BRIDGE_SN9C101: | ||
2480 | case BRIDGE_SN9C102: | ||
2481 | case BRIDGE_SN9C103: | ||
2482 | if (frmsize.pixel_format != V4L2_PIX_FMT_SN9C10X && | ||
2483 | frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8) | ||
2484 | return -EINVAL; | ||
2485 | case BRIDGE_SN9C105: | ||
2486 | case BRIDGE_SN9C120: | ||
2487 | if (frmsize.pixel_format != V4L2_PIX_FMT_JPEG && | ||
2488 | frmsize.pixel_format != V4L2_PIX_FMT_SBGGR8) | ||
2489 | return -EINVAL; | ||
2490 | } | ||
2491 | |||
2492 | frmsize.type = V4L2_FRMSIZE_TYPE_STEPWISE; | ||
2493 | frmsize.stepwise.min_width = frmsize.stepwise.step_width = 16; | ||
2494 | frmsize.stepwise.min_height = frmsize.stepwise.step_height = 16; | ||
2495 | frmsize.stepwise.max_width = cam->sensor.cropcap.bounds.width; | ||
2496 | frmsize.stepwise.max_height = cam->sensor.cropcap.bounds.height; | ||
2497 | memset(&frmsize.reserved, 0, sizeof(frmsize.reserved)); | ||
2498 | |||
2499 | if (copy_to_user(arg, &frmsize, sizeof(frmsize))) | ||
2500 | return -EFAULT; | ||
2501 | |||
2502 | return 0; | ||
2503 | } | ||
2504 | |||
2505 | |||
2506 | static int | ||
2507 | sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg) | ||
2508 | { | ||
2509 | struct v4l2_fmtdesc fmtd; | ||
2510 | |||
2511 | if (copy_from_user(&fmtd, arg, sizeof(fmtd))) | ||
2512 | return -EFAULT; | ||
2513 | |||
2514 | if (fmtd.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
2515 | return -EINVAL; | ||
2516 | |||
2517 | if (fmtd.index == 0) { | ||
2518 | strcpy(fmtd.description, "bayer rgb"); | ||
2519 | fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8; | ||
2520 | } else if (fmtd.index == 1) { | ||
2521 | switch (cam->bridge) { | ||
2522 | case BRIDGE_SN9C101: | ||
2523 | case BRIDGE_SN9C102: | ||
2524 | case BRIDGE_SN9C103: | ||
2525 | strcpy(fmtd.description, "compressed"); | ||
2526 | fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X; | ||
2527 | break; | ||
2528 | case BRIDGE_SN9C105: | ||
2529 | case BRIDGE_SN9C120: | ||
2530 | strcpy(fmtd.description, "JPEG"); | ||
2531 | fmtd.pixelformat = V4L2_PIX_FMT_JPEG; | ||
2532 | break; | ||
2533 | } | ||
2534 | fmtd.flags = V4L2_FMT_FLAG_COMPRESSED; | ||
2535 | } else | ||
2536 | return -EINVAL; | ||
2537 | |||
2538 | fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
2539 | memset(&fmtd.reserved, 0, sizeof(fmtd.reserved)); | ||
2540 | |||
2541 | if (copy_to_user(arg, &fmtd, sizeof(fmtd))) | ||
2542 | return -EFAULT; | ||
2543 | |||
2544 | return 0; | ||
2545 | } | ||
2546 | |||
2547 | |||
2548 | static int | ||
2549 | sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg) | ||
2550 | { | ||
2551 | struct v4l2_format format; | ||
2552 | struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format); | ||
2553 | |||
2554 | if (copy_from_user(&format, arg, sizeof(format))) | ||
2555 | return -EFAULT; | ||
2556 | |||
2557 | if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
2558 | return -EINVAL; | ||
2559 | |||
2560 | pfmt->colorspace = (pfmt->pixelformat == V4L2_PIX_FMT_JPEG) ? | ||
2561 | V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB; | ||
2562 | pfmt->bytesperline = (pfmt->pixelformat == V4L2_PIX_FMT_SN9C10X || | ||
2563 | pfmt->pixelformat == V4L2_PIX_FMT_JPEG) | ||
2564 | ? 0 : (pfmt->width * pfmt->priv) / 8; | ||
2565 | pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8); | ||
2566 | pfmt->field = V4L2_FIELD_NONE; | ||
2567 | memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt)); | ||
2568 | |||
2569 | if (copy_to_user(arg, &format, sizeof(format))) | ||
2570 | return -EFAULT; | ||
2571 | |||
2572 | return 0; | ||
2573 | } | ||
2574 | |||
2575 | |||
2576 | static int | ||
2577 | sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd, | ||
2578 | void __user * arg) | ||
2579 | { | ||
2580 | struct sn9c102_sensor* s = &cam->sensor; | ||
2581 | struct v4l2_format format; | ||
2582 | struct v4l2_pix_format* pix; | ||
2583 | struct v4l2_pix_format* pfmt = &(s->pix_format); | ||
2584 | struct v4l2_rect* bounds = &(s->cropcap.bounds); | ||
2585 | struct v4l2_rect rect; | ||
2586 | u8 scale; | ||
2587 | const enum sn9c102_stream_state stream = cam->stream; | ||
2588 | const u32 nbuffers = cam->nbuffers; | ||
2589 | u32 i; | ||
2590 | int err = 0; | ||
2591 | |||
2592 | if (copy_from_user(&format, arg, sizeof(format))) | ||
2593 | return -EFAULT; | ||
2594 | |||
2595 | pix = &(format.fmt.pix); | ||
2596 | |||
2597 | if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
2598 | return -EINVAL; | ||
2599 | |||
2600 | memcpy(&rect, &(s->_rect), sizeof(rect)); | ||
2601 | |||
2602 | { /* calculate the actual scaling factor */ | ||
2603 | u32 a, b; | ||
2604 | a = rect.width * rect.height; | ||
2605 | b = pix->width * pix->height; | ||
2606 | scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1; | ||
2607 | } | ||
2608 | |||
2609 | rect.width = scale * pix->width; | ||
2610 | rect.height = scale * pix->height; | ||
2611 | |||
2612 | if (rect.width < 16) | ||
2613 | rect.width = 16; | ||
2614 | if (rect.height < 16) | ||
2615 | rect.height = 16; | ||
2616 | if (rect.width > bounds->left + bounds->width - rect.left) | ||
2617 | rect.width = bounds->left + bounds->width - rect.left; | ||
2618 | if (rect.height > bounds->top + bounds->height - rect.top) | ||
2619 | rect.height = bounds->top + bounds->height - rect.top; | ||
2620 | |||
2621 | rect.width &= ~15L; | ||
2622 | rect.height &= ~15L; | ||
2623 | |||
2624 | { /* adjust the scaling factor */ | ||
2625 | u32 a, b; | ||
2626 | a = rect.width * rect.height; | ||
2627 | b = pix->width * pix->height; | ||
2628 | scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1; | ||
2629 | } | ||
2630 | |||
2631 | pix->width = rect.width / scale; | ||
2632 | pix->height = rect.height / scale; | ||
2633 | |||
2634 | switch (cam->bridge) { | ||
2635 | case BRIDGE_SN9C101: | ||
2636 | case BRIDGE_SN9C102: | ||
2637 | case BRIDGE_SN9C103: | ||
2638 | if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X && | ||
2639 | pix->pixelformat != V4L2_PIX_FMT_SBGGR8) | ||
2640 | pix->pixelformat = pfmt->pixelformat; | ||
2641 | break; | ||
2642 | case BRIDGE_SN9C105: | ||
2643 | case BRIDGE_SN9C120: | ||
2644 | if (pix->pixelformat != V4L2_PIX_FMT_JPEG && | ||
2645 | pix->pixelformat != V4L2_PIX_FMT_SBGGR8) | ||
2646 | pix->pixelformat = pfmt->pixelformat; | ||
2647 | break; | ||
2648 | } | ||
2649 | pix->priv = pfmt->priv; /* bpp */ | ||
2650 | pix->colorspace = (pix->pixelformat == V4L2_PIX_FMT_JPEG) ? | ||
2651 | V4L2_COLORSPACE_JPEG : V4L2_COLORSPACE_SRGB; | ||
2652 | pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X || | ||
2653 | pix->pixelformat == V4L2_PIX_FMT_JPEG) | ||
2654 | ? 0 : (pix->width * pix->priv) / 8; | ||
2655 | pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8); | ||
2656 | pix->field = V4L2_FIELD_NONE; | ||
2657 | |||
2658 | if (cmd == VIDIOC_TRY_FMT) { | ||
2659 | if (copy_to_user(arg, &format, sizeof(format))) | ||
2660 | return -EFAULT; | ||
2661 | return 0; | ||
2662 | } | ||
2663 | |||
2664 | if (cam->module_param.force_munmap) | ||
2665 | for (i = 0; i < cam->nbuffers; i++) | ||
2666 | if (cam->frame[i].vma_use_count) { | ||
2667 | DBG(3, "VIDIOC_S_FMT failed. Unmap the " | ||
2668 | "buffers first."); | ||
2669 | return -EBUSY; | ||
2670 | } | ||
2671 | |||
2672 | if (cam->stream == STREAM_ON) | ||
2673 | if ((err = sn9c102_stream_interrupt(cam))) | ||
2674 | return err; | ||
2675 | |||
2676 | if (copy_to_user(arg, &format, sizeof(format))) { | ||
2677 | cam->stream = stream; | ||
2678 | return -EFAULT; | ||
2679 | } | ||
2680 | |||
2681 | if (cam->module_param.force_munmap || cam->io == IO_READ) | ||
2682 | sn9c102_release_buffers(cam); | ||
2683 | |||
2684 | err += sn9c102_set_pix_format(cam, pix); | ||
2685 | err += sn9c102_set_crop(cam, &rect); | ||
2686 | if (s->set_pix_format) | ||
2687 | err += s->set_pix_format(cam, pix); | ||
2688 | if (s->set_crop) | ||
2689 | err += s->set_crop(cam, &rect); | ||
2690 | err += sn9c102_set_scale(cam, scale); | ||
2691 | |||
2692 | if (err) { /* atomic, no rollback in ioctl() */ | ||
2693 | cam->state |= DEV_MISCONFIGURED; | ||
2694 | DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To " | ||
2695 | "use the camera, close and open %s again.", | ||
2696 | video_device_node_name(cam->v4ldev)); | ||
2697 | return -EIO; | ||
2698 | } | ||
2699 | |||
2700 | memcpy(pfmt, pix, sizeof(*pix)); | ||
2701 | memcpy(&(s->_rect), &rect, sizeof(rect)); | ||
2702 | |||
2703 | if ((cam->module_param.force_munmap || cam->io == IO_READ) && | ||
2704 | nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) { | ||
2705 | cam->state |= DEV_MISCONFIGURED; | ||
2706 | DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To " | ||
2707 | "use the camera, close and open %s again.", | ||
2708 | video_device_node_name(cam->v4ldev)); | ||
2709 | return -ENOMEM; | ||
2710 | } | ||
2711 | |||
2712 | if (cam->io == IO_READ) | ||
2713 | sn9c102_empty_framequeues(cam); | ||
2714 | else if (cam->module_param.force_munmap) | ||
2715 | sn9c102_requeue_outqueue(cam); | ||
2716 | |||
2717 | cam->stream = stream; | ||
2718 | |||
2719 | return 0; | ||
2720 | } | ||
2721 | |||
2722 | |||
2723 | static int | ||
2724 | sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg) | ||
2725 | { | ||
2726 | if (copy_to_user(arg, &cam->compression, sizeof(cam->compression))) | ||
2727 | return -EFAULT; | ||
2728 | |||
2729 | return 0; | ||
2730 | } | ||
2731 | |||
2732 | |||
2733 | static int | ||
2734 | sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg) | ||
2735 | { | ||
2736 | struct v4l2_jpegcompression jc; | ||
2737 | const enum sn9c102_stream_state stream = cam->stream; | ||
2738 | int err = 0; | ||
2739 | |||
2740 | if (copy_from_user(&jc, arg, sizeof(jc))) | ||
2741 | return -EFAULT; | ||
2742 | |||
2743 | if (jc.quality != 0 && jc.quality != 1) | ||
2744 | return -EINVAL; | ||
2745 | |||
2746 | if (cam->stream == STREAM_ON) | ||
2747 | if ((err = sn9c102_stream_interrupt(cam))) | ||
2748 | return err; | ||
2749 | |||
2750 | err += sn9c102_set_compression(cam, &jc); | ||
2751 | if (err) { /* atomic, no rollback in ioctl() */ | ||
2752 | cam->state |= DEV_MISCONFIGURED; | ||
2753 | DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware problems. " | ||
2754 | "To use the camera, close and open %s again.", | ||
2755 | video_device_node_name(cam->v4ldev)); | ||
2756 | return -EIO; | ||
2757 | } | ||
2758 | |||
2759 | cam->compression.quality = jc.quality; | ||
2760 | |||
2761 | cam->stream = stream; | ||
2762 | |||
2763 | return 0; | ||
2764 | } | ||
2765 | |||
2766 | |||
2767 | static int | ||
2768 | sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg) | ||
2769 | { | ||
2770 | struct v4l2_requestbuffers rb; | ||
2771 | u32 i; | ||
2772 | int err; | ||
2773 | |||
2774 | if (copy_from_user(&rb, arg, sizeof(rb))) | ||
2775 | return -EFAULT; | ||
2776 | |||
2777 | if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | ||
2778 | rb.memory != V4L2_MEMORY_MMAP) | ||
2779 | return -EINVAL; | ||
2780 | |||
2781 | if (cam->io == IO_READ) { | ||
2782 | DBG(3, "Close and open the device again to choose the mmap " | ||
2783 | "I/O method"); | ||
2784 | return -EBUSY; | ||
2785 | } | ||
2786 | |||
2787 | for (i = 0; i < cam->nbuffers; i++) | ||
2788 | if (cam->frame[i].vma_use_count) { | ||
2789 | DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are " | ||
2790 | "still mapped."); | ||
2791 | return -EBUSY; | ||
2792 | } | ||
2793 | |||
2794 | if (cam->stream == STREAM_ON) | ||
2795 | if ((err = sn9c102_stream_interrupt(cam))) | ||
2796 | return err; | ||
2797 | |||
2798 | sn9c102_empty_framequeues(cam); | ||
2799 | |||
2800 | sn9c102_release_buffers(cam); | ||
2801 | if (rb.count) | ||
2802 | rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP); | ||
2803 | |||
2804 | if (copy_to_user(arg, &rb, sizeof(rb))) { | ||
2805 | sn9c102_release_buffers(cam); | ||
2806 | cam->io = IO_NONE; | ||
2807 | return -EFAULT; | ||
2808 | } | ||
2809 | |||
2810 | cam->io = rb.count ? IO_MMAP : IO_NONE; | ||
2811 | |||
2812 | return 0; | ||
2813 | } | ||
2814 | |||
2815 | |||
2816 | static int | ||
2817 | sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg) | ||
2818 | { | ||
2819 | struct v4l2_buffer b; | ||
2820 | |||
2821 | if (copy_from_user(&b, arg, sizeof(b))) | ||
2822 | return -EFAULT; | ||
2823 | |||
2824 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | ||
2825 | b.index >= cam->nbuffers || cam->io != IO_MMAP) | ||
2826 | return -EINVAL; | ||
2827 | |||
2828 | memcpy(&b, &cam->frame[b.index].buf, sizeof(b)); | ||
2829 | |||
2830 | if (cam->frame[b.index].vma_use_count) | ||
2831 | b.flags |= V4L2_BUF_FLAG_MAPPED; | ||
2832 | |||
2833 | if (cam->frame[b.index].state == F_DONE) | ||
2834 | b.flags |= V4L2_BUF_FLAG_DONE; | ||
2835 | else if (cam->frame[b.index].state != F_UNUSED) | ||
2836 | b.flags |= V4L2_BUF_FLAG_QUEUED; | ||
2837 | |||
2838 | if (copy_to_user(arg, &b, sizeof(b))) | ||
2839 | return -EFAULT; | ||
2840 | |||
2841 | return 0; | ||
2842 | } | ||
2843 | |||
2844 | |||
2845 | static int | ||
2846 | sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg) | ||
2847 | { | ||
2848 | struct v4l2_buffer b; | ||
2849 | unsigned long lock_flags; | ||
2850 | |||
2851 | if (copy_from_user(&b, arg, sizeof(b))) | ||
2852 | return -EFAULT; | ||
2853 | |||
2854 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | ||
2855 | b.index >= cam->nbuffers || cam->io != IO_MMAP) | ||
2856 | return -EINVAL; | ||
2857 | |||
2858 | if (cam->frame[b.index].state != F_UNUSED) | ||
2859 | return -EINVAL; | ||
2860 | |||
2861 | cam->frame[b.index].state = F_QUEUED; | ||
2862 | |||
2863 | spin_lock_irqsave(&cam->queue_lock, lock_flags); | ||
2864 | list_add_tail(&cam->frame[b.index].frame, &cam->inqueue); | ||
2865 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | ||
2866 | |||
2867 | PDBGG("Frame #%lu queued", (unsigned long)b.index); | ||
2868 | |||
2869 | return 0; | ||
2870 | } | ||
2871 | |||
2872 | |||
2873 | static int | ||
2874 | sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp, | ||
2875 | void __user * arg) | ||
2876 | { | ||
2877 | struct v4l2_buffer b; | ||
2878 | struct sn9c102_frame_t *f; | ||
2879 | unsigned long lock_flags; | ||
2880 | long timeout; | ||
2881 | int err = 0; | ||
2882 | |||
2883 | if (copy_from_user(&b, arg, sizeof(b))) | ||
2884 | return -EFAULT; | ||
2885 | |||
2886 | if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) | ||
2887 | return -EINVAL; | ||
2888 | |||
2889 | if (list_empty(&cam->outqueue)) { | ||
2890 | if (cam->stream == STREAM_OFF) | ||
2891 | return -EINVAL; | ||
2892 | if (filp->f_flags & O_NONBLOCK) | ||
2893 | return -EAGAIN; | ||
2894 | if (!cam->module_param.frame_timeout) { | ||
2895 | err = wait_event_interruptible | ||
2896 | ( cam->wait_frame, | ||
2897 | (!list_empty(&cam->outqueue)) || | ||
2898 | (cam->state & DEV_DISCONNECTED) || | ||
2899 | (cam->state & DEV_MISCONFIGURED) ); | ||
2900 | if (err) | ||
2901 | return err; | ||
2902 | } else { | ||
2903 | timeout = wait_event_interruptible_timeout | ||
2904 | ( cam->wait_frame, | ||
2905 | (!list_empty(&cam->outqueue)) || | ||
2906 | (cam->state & DEV_DISCONNECTED) || | ||
2907 | (cam->state & DEV_MISCONFIGURED), | ||
2908 | cam->module_param.frame_timeout * | ||
2909 | 1000 * msecs_to_jiffies(1) ); | ||
2910 | if (timeout < 0) | ||
2911 | return timeout; | ||
2912 | else if (timeout == 0 && | ||
2913 | !(cam->state & DEV_DISCONNECTED)) { | ||
2914 | DBG(1, "Video frame timeout elapsed"); | ||
2915 | return -EIO; | ||
2916 | } | ||
2917 | } | ||
2918 | if (cam->state & DEV_DISCONNECTED) | ||
2919 | return -ENODEV; | ||
2920 | if (cam->state & DEV_MISCONFIGURED) | ||
2921 | return -EIO; | ||
2922 | } | ||
2923 | |||
2924 | spin_lock_irqsave(&cam->queue_lock, lock_flags); | ||
2925 | f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame); | ||
2926 | list_del(cam->outqueue.next); | ||
2927 | spin_unlock_irqrestore(&cam->queue_lock, lock_flags); | ||
2928 | |||
2929 | f->state = F_UNUSED; | ||
2930 | |||
2931 | memcpy(&b, &f->buf, sizeof(b)); | ||
2932 | if (f->vma_use_count) | ||
2933 | b.flags |= V4L2_BUF_FLAG_MAPPED; | ||
2934 | |||
2935 | if (copy_to_user(arg, &b, sizeof(b))) | ||
2936 | return -EFAULT; | ||
2937 | |||
2938 | PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index); | ||
2939 | |||
2940 | return 0; | ||
2941 | } | ||
2942 | |||
2943 | |||
2944 | static int | ||
2945 | sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg) | ||
2946 | { | ||
2947 | int type; | ||
2948 | |||
2949 | if (copy_from_user(&type, arg, sizeof(type))) | ||
2950 | return -EFAULT; | ||
2951 | |||
2952 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) | ||
2953 | return -EINVAL; | ||
2954 | |||
2955 | cam->stream = STREAM_ON; | ||
2956 | |||
2957 | DBG(3, "Stream on"); | ||
2958 | |||
2959 | return 0; | ||
2960 | } | ||
2961 | |||
2962 | |||
2963 | static int | ||
2964 | sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg) | ||
2965 | { | ||
2966 | int type, err; | ||
2967 | |||
2968 | if (copy_from_user(&type, arg, sizeof(type))) | ||
2969 | return -EFAULT; | ||
2970 | |||
2971 | if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) | ||
2972 | return -EINVAL; | ||
2973 | |||
2974 | if (cam->stream == STREAM_ON) | ||
2975 | if ((err = sn9c102_stream_interrupt(cam))) | ||
2976 | return err; | ||
2977 | |||
2978 | sn9c102_empty_framequeues(cam); | ||
2979 | |||
2980 | DBG(3, "Stream off"); | ||
2981 | |||
2982 | return 0; | ||
2983 | } | ||
2984 | |||
2985 | |||
2986 | static int | ||
2987 | sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg) | ||
2988 | { | ||
2989 | struct v4l2_streamparm sp; | ||
2990 | |||
2991 | if (copy_from_user(&sp, arg, sizeof(sp))) | ||
2992 | return -EFAULT; | ||
2993 | |||
2994 | if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
2995 | return -EINVAL; | ||
2996 | |||
2997 | sp.parm.capture.extendedmode = 0; | ||
2998 | sp.parm.capture.readbuffers = cam->nreadbuffers; | ||
2999 | |||
3000 | if (copy_to_user(arg, &sp, sizeof(sp))) | ||
3001 | return -EFAULT; | ||
3002 | |||
3003 | return 0; | ||
3004 | } | ||
3005 | |||
3006 | |||
3007 | static int | ||
3008 | sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg) | ||
3009 | { | ||
3010 | struct v4l2_streamparm sp; | ||
3011 | |||
3012 | if (copy_from_user(&sp, arg, sizeof(sp))) | ||
3013 | return -EFAULT; | ||
3014 | |||
3015 | if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | ||
3016 | return -EINVAL; | ||
3017 | |||
3018 | sp.parm.capture.extendedmode = 0; | ||
3019 | |||
3020 | if (sp.parm.capture.readbuffers == 0) | ||
3021 | sp.parm.capture.readbuffers = cam->nreadbuffers; | ||
3022 | |||
3023 | if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES) | ||
3024 | sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES; | ||
3025 | |||
3026 | if (copy_to_user(arg, &sp, sizeof(sp))) | ||
3027 | return -EFAULT; | ||
3028 | |||
3029 | cam->nreadbuffers = sp.parm.capture.readbuffers; | ||
3030 | |||
3031 | return 0; | ||
3032 | } | ||
3033 | |||
3034 | |||
3035 | static int | ||
3036 | sn9c102_vidioc_enumaudio(struct sn9c102_device* cam, void __user * arg) | ||
3037 | { | ||
3038 | struct v4l2_audio audio; | ||
3039 | |||
3040 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) | ||
3041 | return -EINVAL; | ||
3042 | |||
3043 | if (copy_from_user(&audio, arg, sizeof(audio))) | ||
3044 | return -EFAULT; | ||
3045 | |||
3046 | if (audio.index != 0) | ||
3047 | return -EINVAL; | ||
3048 | |||
3049 | strcpy(audio.name, "Microphone"); | ||
3050 | audio.capability = 0; | ||
3051 | audio.mode = 0; | ||
3052 | |||
3053 | if (copy_to_user(arg, &audio, sizeof(audio))) | ||
3054 | return -EFAULT; | ||
3055 | |||
3056 | return 0; | ||
3057 | } | ||
3058 | |||
3059 | |||
3060 | static int | ||
3061 | sn9c102_vidioc_g_audio(struct sn9c102_device* cam, void __user * arg) | ||
3062 | { | ||
3063 | struct v4l2_audio audio; | ||
3064 | |||
3065 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) | ||
3066 | return -EINVAL; | ||
3067 | |||
3068 | if (copy_from_user(&audio, arg, sizeof(audio))) | ||
3069 | return -EFAULT; | ||
3070 | |||
3071 | memset(&audio, 0, sizeof(audio)); | ||
3072 | strcpy(audio.name, "Microphone"); | ||
3073 | |||
3074 | if (copy_to_user(arg, &audio, sizeof(audio))) | ||
3075 | return -EFAULT; | ||
3076 | |||
3077 | return 0; | ||
3078 | } | ||
3079 | |||
3080 | |||
3081 | static int | ||
3082 | sn9c102_vidioc_s_audio(struct sn9c102_device* cam, void __user * arg) | ||
3083 | { | ||
3084 | struct v4l2_audio audio; | ||
3085 | |||
3086 | if (cam->bridge == BRIDGE_SN9C101 || cam->bridge == BRIDGE_SN9C102) | ||
3087 | return -EINVAL; | ||
3088 | |||
3089 | if (copy_from_user(&audio, arg, sizeof(audio))) | ||
3090 | return -EFAULT; | ||
3091 | |||
3092 | if (audio.index != 0) | ||
3093 | return -EINVAL; | ||
3094 | |||
3095 | return 0; | ||
3096 | } | ||
3097 | |||
3098 | |||
3099 | static long sn9c102_ioctl_v4l2(struct file *filp, | ||
3100 | unsigned int cmd, void __user *arg) | ||
3101 | { | ||
3102 | struct sn9c102_device *cam = video_drvdata(filp); | ||
3103 | |||
3104 | switch (cmd) { | ||
3105 | |||
3106 | case VIDIOC_QUERYCAP: | ||
3107 | return sn9c102_vidioc_querycap(cam, arg); | ||
3108 | |||
3109 | case VIDIOC_ENUMINPUT: | ||
3110 | return sn9c102_vidioc_enuminput(cam, arg); | ||
3111 | |||
3112 | case VIDIOC_G_INPUT: | ||
3113 | return sn9c102_vidioc_g_input(cam, arg); | ||
3114 | |||
3115 | case VIDIOC_S_INPUT: | ||
3116 | return sn9c102_vidioc_s_input(cam, arg); | ||
3117 | |||
3118 | case VIDIOC_QUERYCTRL: | ||
3119 | return sn9c102_vidioc_query_ctrl(cam, arg); | ||
3120 | |||
3121 | case VIDIOC_G_CTRL: | ||
3122 | return sn9c102_vidioc_g_ctrl(cam, arg); | ||
3123 | |||
3124 | case VIDIOC_S_CTRL: | ||
3125 | return sn9c102_vidioc_s_ctrl(cam, arg); | ||
3126 | |||
3127 | case VIDIOC_CROPCAP: | ||
3128 | return sn9c102_vidioc_cropcap(cam, arg); | ||
3129 | |||
3130 | case VIDIOC_G_CROP: | ||
3131 | return sn9c102_vidioc_g_crop(cam, arg); | ||
3132 | |||
3133 | case VIDIOC_S_CROP: | ||
3134 | return sn9c102_vidioc_s_crop(cam, arg); | ||
3135 | |||
3136 | case VIDIOC_ENUM_FRAMESIZES: | ||
3137 | return sn9c102_vidioc_enum_framesizes(cam, arg); | ||
3138 | |||
3139 | case VIDIOC_ENUM_FMT: | ||
3140 | return sn9c102_vidioc_enum_fmt(cam, arg); | ||
3141 | |||
3142 | case VIDIOC_G_FMT: | ||
3143 | return sn9c102_vidioc_g_fmt(cam, arg); | ||
3144 | |||
3145 | case VIDIOC_TRY_FMT: | ||
3146 | case VIDIOC_S_FMT: | ||
3147 | return sn9c102_vidioc_try_s_fmt(cam, cmd, arg); | ||
3148 | |||
3149 | case VIDIOC_G_JPEGCOMP: | ||
3150 | return sn9c102_vidioc_g_jpegcomp(cam, arg); | ||
3151 | |||
3152 | case VIDIOC_S_JPEGCOMP: | ||
3153 | return sn9c102_vidioc_s_jpegcomp(cam, arg); | ||
3154 | |||
3155 | case VIDIOC_REQBUFS: | ||
3156 | return sn9c102_vidioc_reqbufs(cam, arg); | ||
3157 | |||
3158 | case VIDIOC_QUERYBUF: | ||
3159 | return sn9c102_vidioc_querybuf(cam, arg); | ||
3160 | |||
3161 | case VIDIOC_QBUF: | ||
3162 | return sn9c102_vidioc_qbuf(cam, arg); | ||
3163 | |||
3164 | case VIDIOC_DQBUF: | ||
3165 | return sn9c102_vidioc_dqbuf(cam, filp, arg); | ||
3166 | |||
3167 | case VIDIOC_STREAMON: | ||
3168 | return sn9c102_vidioc_streamon(cam, arg); | ||
3169 | |||
3170 | case VIDIOC_STREAMOFF: | ||
3171 | return sn9c102_vidioc_streamoff(cam, arg); | ||
3172 | |||
3173 | case VIDIOC_G_PARM: | ||
3174 | return sn9c102_vidioc_g_parm(cam, arg); | ||
3175 | |||
3176 | case VIDIOC_S_PARM: | ||
3177 | return sn9c102_vidioc_s_parm(cam, arg); | ||
3178 | |||
3179 | case VIDIOC_ENUMAUDIO: | ||
3180 | return sn9c102_vidioc_enumaudio(cam, arg); | ||
3181 | |||
3182 | case VIDIOC_G_AUDIO: | ||
3183 | return sn9c102_vidioc_g_audio(cam, arg); | ||
3184 | |||
3185 | case VIDIOC_S_AUDIO: | ||
3186 | return sn9c102_vidioc_s_audio(cam, arg); | ||
3187 | |||
3188 | default: | ||
3189 | return -ENOTTY; | ||
3190 | |||
3191 | } | ||
3192 | } | ||
3193 | |||
3194 | |||
3195 | static long sn9c102_ioctl(struct file *filp, | ||
3196 | unsigned int cmd, unsigned long arg) | ||
3197 | { | ||
3198 | struct sn9c102_device *cam = video_drvdata(filp); | ||
3199 | int err = 0; | ||
3200 | |||
3201 | if (mutex_lock_interruptible(&cam->fileop_mutex)) | ||
3202 | return -ERESTARTSYS; | ||
3203 | |||
3204 | if (cam->state & DEV_DISCONNECTED) { | ||
3205 | DBG(1, "Device not present"); | ||
3206 | mutex_unlock(&cam->fileop_mutex); | ||
3207 | return -ENODEV; | ||
3208 | } | ||
3209 | |||
3210 | if (cam->state & DEV_MISCONFIGURED) { | ||
3211 | DBG(1, "The camera is misconfigured. Close and open it " | ||
3212 | "again."); | ||
3213 | mutex_unlock(&cam->fileop_mutex); | ||
3214 | return -EIO; | ||
3215 | } | ||
3216 | |||
3217 | V4LDBG(3, "sn9c102", cmd); | ||
3218 | |||
3219 | err = sn9c102_ioctl_v4l2(filp, cmd, (void __user *)arg); | ||
3220 | |||
3221 | mutex_unlock(&cam->fileop_mutex); | ||
3222 | |||
3223 | return err; | ||
3224 | } | ||
3225 | |||
3226 | /*****************************************************************************/ | ||
3227 | |||
3228 | static const struct v4l2_file_operations sn9c102_fops = { | ||
3229 | .owner = THIS_MODULE, | ||
3230 | .open = sn9c102_open, | ||
3231 | .release = sn9c102_release, | ||
3232 | .unlocked_ioctl = sn9c102_ioctl, | ||
3233 | .read = sn9c102_read, | ||
3234 | .poll = sn9c102_poll, | ||
3235 | .mmap = sn9c102_mmap, | ||
3236 | }; | ||
3237 | |||
3238 | /*****************************************************************************/ | ||
3239 | |||
3240 | /* It exists a single interface only. We do not need to validate anything. */ | ||
3241 | static int | ||
3242 | sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id) | ||
3243 | { | ||
3244 | struct usb_device *udev = interface_to_usbdev(intf); | ||
3245 | struct sn9c102_device* cam; | ||
3246 | static unsigned int dev_nr; | ||
3247 | unsigned int i; | ||
3248 | int err = 0, r; | ||
3249 | |||
3250 | if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL))) | ||
3251 | return -ENOMEM; | ||
3252 | |||
3253 | cam->usbdev = udev; | ||
3254 | |||
3255 | if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) { | ||
3256 | DBG(1, "kzalloc() failed"); | ||
3257 | err = -ENOMEM; | ||
3258 | goto fail; | ||
3259 | } | ||
3260 | |||
3261 | if (!(cam->v4ldev = video_device_alloc())) { | ||
3262 | DBG(1, "video_device_alloc() failed"); | ||
3263 | err = -ENOMEM; | ||
3264 | goto fail; | ||
3265 | } | ||
3266 | |||
3267 | r = sn9c102_read_reg(cam, 0x00); | ||
3268 | if (r < 0 || (r != 0x10 && r != 0x11 && r != 0x12)) { | ||
3269 | DBG(1, "Sorry, this is not a SN9C1xx-based camera " | ||
3270 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); | ||
3271 | err = -ENODEV; | ||
3272 | goto fail; | ||
3273 | } | ||
3274 | |||
3275 | cam->bridge = id->driver_info; | ||
3276 | switch (cam->bridge) { | ||
3277 | case BRIDGE_SN9C101: | ||
3278 | case BRIDGE_SN9C102: | ||
3279 | DBG(2, "SN9C10[12] PC Camera Controller detected " | ||
3280 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); | ||
3281 | break; | ||
3282 | case BRIDGE_SN9C103: | ||
3283 | DBG(2, "SN9C103 PC Camera Controller detected " | ||
3284 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); | ||
3285 | break; | ||
3286 | case BRIDGE_SN9C105: | ||
3287 | DBG(2, "SN9C105 PC Camera Controller detected " | ||
3288 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); | ||
3289 | break; | ||
3290 | case BRIDGE_SN9C120: | ||
3291 | DBG(2, "SN9C120 PC Camera Controller detected " | ||
3292 | "(vid:pid 0x%04X:0x%04X)", id->idVendor, id->idProduct); | ||
3293 | break; | ||
3294 | } | ||
3295 | |||
3296 | for (i = 0; i < ARRAY_SIZE(sn9c102_sensor_table); i++) { | ||
3297 | err = sn9c102_sensor_table[i](cam); | ||
3298 | if (!err) | ||
3299 | break; | ||
3300 | } | ||
3301 | |||
3302 | if (!err) { | ||
3303 | DBG(2, "%s image sensor detected", cam->sensor.name); | ||
3304 | DBG(3, "Support for %s maintained by %s", | ||
3305 | cam->sensor.name, cam->sensor.maintainer); | ||
3306 | } else { | ||
3307 | DBG(1, "No supported image sensor detected for this bridge"); | ||
3308 | err = -ENODEV; | ||
3309 | goto fail; | ||
3310 | } | ||
3311 | |||
3312 | if (!(cam->bridge & cam->sensor.supported_bridge)) { | ||
3313 | DBG(1, "Bridge not supported"); | ||
3314 | err = -ENODEV; | ||
3315 | goto fail; | ||
3316 | } | ||
3317 | |||
3318 | if (sn9c102_init(cam)) { | ||
3319 | DBG(1, "Initialization failed. I will retry on open()."); | ||
3320 | cam->state |= DEV_MISCONFIGURED; | ||
3321 | } | ||
3322 | |||
3323 | strcpy(cam->v4ldev->name, "SN9C1xx PC Camera"); | ||
3324 | cam->v4ldev->fops = &sn9c102_fops; | ||
3325 | cam->v4ldev->release = video_device_release; | ||
3326 | cam->v4ldev->parent = &udev->dev; | ||
3327 | |||
3328 | init_completion(&cam->probe); | ||
3329 | |||
3330 | err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, | ||
3331 | video_nr[dev_nr]); | ||
3332 | if (err) { | ||
3333 | DBG(1, "V4L2 device registration failed"); | ||
3334 | if (err == -ENFILE && video_nr[dev_nr] == -1) | ||
3335 | DBG(1, "Free /dev/videoX node not found"); | ||
3336 | video_nr[dev_nr] = -1; | ||
3337 | dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; | ||
3338 | complete_all(&cam->probe); | ||
3339 | goto fail; | ||
3340 | } | ||
3341 | |||
3342 | DBG(2, "V4L2 device registered as %s", | ||
3343 | video_device_node_name(cam->v4ldev)); | ||
3344 | |||
3345 | video_set_drvdata(cam->v4ldev, cam); | ||
3346 | cam->module_param.force_munmap = force_munmap[dev_nr]; | ||
3347 | cam->module_param.frame_timeout = frame_timeout[dev_nr]; | ||
3348 | |||
3349 | dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; | ||
3350 | |||
3351 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
3352 | err = sn9c102_create_sysfs(cam); | ||
3353 | if (!err) | ||
3354 | DBG(2, "Optional device control through 'sysfs' " | ||
3355 | "interface ready"); | ||
3356 | else | ||
3357 | DBG(2, "Failed to create optional 'sysfs' interface for " | ||
3358 | "device controlling. Error #%d", err); | ||
3359 | #else | ||
3360 | DBG(2, "Optional device control through 'sysfs' interface disabled"); | ||
3361 | DBG(3, "Compile the kernel with the 'CONFIG_VIDEO_ADV_DEBUG' " | ||
3362 | "configuration option to enable it."); | ||
3363 | #endif | ||
3364 | |||
3365 | usb_set_intfdata(intf, cam); | ||
3366 | kref_init(&cam->kref); | ||
3367 | usb_get_dev(cam->usbdev); | ||
3368 | |||
3369 | complete_all(&cam->probe); | ||
3370 | |||
3371 | return 0; | ||
3372 | |||
3373 | fail: | ||
3374 | if (cam) { | ||
3375 | kfree(cam->control_buffer); | ||
3376 | if (cam->v4ldev) | ||
3377 | video_device_release(cam->v4ldev); | ||
3378 | kfree(cam); | ||
3379 | } | ||
3380 | return err; | ||
3381 | } | ||
3382 | |||
3383 | |||
3384 | static void sn9c102_usb_disconnect(struct usb_interface* intf) | ||
3385 | { | ||
3386 | struct sn9c102_device* cam; | ||
3387 | |||
3388 | down_write(&sn9c102_dev_lock); | ||
3389 | |||
3390 | cam = usb_get_intfdata(intf); | ||
3391 | |||
3392 | DBG(2, "Disconnecting %s...", cam->v4ldev->name); | ||
3393 | |||
3394 | if (cam->users) { | ||
3395 | DBG(2, "Device %s is open! Deregistration and memory " | ||
3396 | "deallocation are deferred.", | ||
3397 | video_device_node_name(cam->v4ldev)); | ||
3398 | cam->state |= DEV_MISCONFIGURED; | ||
3399 | sn9c102_stop_transfer(cam); | ||
3400 | cam->state |= DEV_DISCONNECTED; | ||
3401 | wake_up_interruptible(&cam->wait_frame); | ||
3402 | wake_up(&cam->wait_stream); | ||
3403 | } else | ||
3404 | cam->state |= DEV_DISCONNECTED; | ||
3405 | |||
3406 | wake_up_interruptible_all(&cam->wait_open); | ||
3407 | |||
3408 | kref_put(&cam->kref, sn9c102_release_resources); | ||
3409 | |||
3410 | up_write(&sn9c102_dev_lock); | ||
3411 | } | ||
3412 | |||
3413 | |||
3414 | static struct usb_driver sn9c102_usb_driver = { | ||
3415 | .name = "sn9c102", | ||
3416 | .id_table = sn9c102_id_table, | ||
3417 | .probe = sn9c102_usb_probe, | ||
3418 | .disconnect = sn9c102_usb_disconnect, | ||
3419 | }; | ||
3420 | |||
3421 | module_usb_driver(sn9c102_usb_driver); | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_devtable.h b/drivers/media/usb/sn9c102/sn9c102_devtable.h new file mode 100644 index 000000000000..b3d2cc729657 --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_devtable.h | |||
@@ -0,0 +1,147 @@ | |||
1 | /*************************************************************************** | ||
2 | * Table of device identifiers of the SN9C1xx PC Camera Controllers * | ||
3 | * * | ||
4 | * Copyright (C) 2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License as published by * | ||
8 | * the Free Software Foundation; either version 2 of the License, or * | ||
9 | * (at your option) any later version. * | ||
10 | * * | ||
11 | * This program is distributed in the hope that it will be useful, * | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
14 | * GNU General Public License for more details. * | ||
15 | * * | ||
16 | * You should have received a copy of the GNU General Public License * | ||
17 | * along with this program; if not, write to the Free Software * | ||
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
19 | ***************************************************************************/ | ||
20 | |||
21 | #ifndef _SN9C102_DEVTABLE_H_ | ||
22 | #define _SN9C102_DEVTABLE_H_ | ||
23 | |||
24 | #include <linux/usb.h> | ||
25 | |||
26 | struct sn9c102_device; | ||
27 | |||
28 | /* | ||
29 | Each SN9C1xx camera has proper PID/VID identifiers. | ||
30 | SN9C103, SN9C105, SN9C120 support multiple interfaces, but we only have to | ||
31 | handle the video class interface. | ||
32 | */ | ||
33 | #define SN9C102_USB_DEVICE(vend, prod, bridge) \ | ||
34 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ | ||
35 | USB_DEVICE_ID_MATCH_INT_CLASS, \ | ||
36 | .idVendor = (vend), \ | ||
37 | .idProduct = (prod), \ | ||
38 | .bInterfaceClass = 0xff, \ | ||
39 | .driver_info = (bridge) | ||
40 | |||
41 | static const struct usb_device_id sn9c102_id_table[] = { | ||
42 | /* SN9C101 and SN9C102 */ | ||
43 | #if !defined CONFIG_USB_GSPCA_SONIXB && !defined CONFIG_USB_GSPCA_SONIXB_MODULE | ||
44 | { SN9C102_USB_DEVICE(0x0c45, 0x6001, BRIDGE_SN9C102), }, | ||
45 | { SN9C102_USB_DEVICE(0x0c45, 0x6005, BRIDGE_SN9C102), }, | ||
46 | { SN9C102_USB_DEVICE(0x0c45, 0x6007, BRIDGE_SN9C102), }, | ||
47 | { SN9C102_USB_DEVICE(0x0c45, 0x6009, BRIDGE_SN9C102), }, | ||
48 | { SN9C102_USB_DEVICE(0x0c45, 0x600d, BRIDGE_SN9C102), }, | ||
49 | /* { SN9C102_USB_DEVICE(0x0c45, 0x6011, BRIDGE_SN9C102), }, OV6650 */ | ||
50 | { SN9C102_USB_DEVICE(0x0c45, 0x6019, BRIDGE_SN9C102), }, | ||
51 | #endif | ||
52 | { SN9C102_USB_DEVICE(0x0c45, 0x6024, BRIDGE_SN9C102), }, | ||
53 | { SN9C102_USB_DEVICE(0x0c45, 0x6025, BRIDGE_SN9C102), }, | ||
54 | #if !defined CONFIG_USB_GSPCA_SONIXB && !defined CONFIG_USB_GSPCA_SONIXB_MODULE | ||
55 | { SN9C102_USB_DEVICE(0x0c45, 0x6028, BRIDGE_SN9C102), }, | ||
56 | { SN9C102_USB_DEVICE(0x0c45, 0x6029, BRIDGE_SN9C102), }, | ||
57 | { SN9C102_USB_DEVICE(0x0c45, 0x602a, BRIDGE_SN9C102), }, | ||
58 | #endif | ||
59 | { SN9C102_USB_DEVICE(0x0c45, 0x602b, BRIDGE_SN9C102), }, /* not in sonixb */ | ||
60 | #if !defined CONFIG_USB_GSPCA_SONIXB && !defined CONFIG_USB_GSPCA_SONIXB_MODULE | ||
61 | { SN9C102_USB_DEVICE(0x0c45, 0x602c, BRIDGE_SN9C102), }, | ||
62 | /* { SN9C102_USB_DEVICE(0x0c45, 0x602d, BRIDGE_SN9C102), }, HV7131R */ | ||
63 | { SN9C102_USB_DEVICE(0x0c45, 0x602e, BRIDGE_SN9C102), }, | ||
64 | #endif | ||
65 | { SN9C102_USB_DEVICE(0x0c45, 0x6030, BRIDGE_SN9C102), }, /* not in sonixb */ | ||
66 | /* SN9C103 */ | ||
67 | /* { SN9C102_USB_DEVICE(0x0c45, 0x6080, BRIDGE_SN9C103), }, non existent ? */ | ||
68 | { SN9C102_USB_DEVICE(0x0c45, 0x6082, BRIDGE_SN9C103), }, /* not in sonixb */ | ||
69 | #if !defined CONFIG_USB_GSPCA_SONIXB && !defined CONFIG_USB_GSPCA_SONIXB_MODULE | ||
70 | /* { SN9C102_USB_DEVICE(0x0c45, 0x6083, BRIDGE_SN9C103), }, HY7131D/E */ | ||
71 | /* { SN9C102_USB_DEVICE(0x0c45, 0x6088, BRIDGE_SN9C103), }, non existent ? */ | ||
72 | /* { SN9C102_USB_DEVICE(0x0c45, 0x608a, BRIDGE_SN9C103), }, non existent ? */ | ||
73 | /* { SN9C102_USB_DEVICE(0x0c45, 0x608b, BRIDGE_SN9C103), }, non existent ? */ | ||
74 | { SN9C102_USB_DEVICE(0x0c45, 0x608c, BRIDGE_SN9C103), }, | ||
75 | /* { SN9C102_USB_DEVICE(0x0c45, 0x608e, BRIDGE_SN9C103), }, CISVF10 */ | ||
76 | { SN9C102_USB_DEVICE(0x0c45, 0x608f, BRIDGE_SN9C103), }, | ||
77 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60a0, BRIDGE_SN9C103), }, non existent ? */ | ||
78 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60a2, BRIDGE_SN9C103), }, non existent ? */ | ||
79 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60a3, BRIDGE_SN9C103), }, non existent ? */ | ||
80 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60a8, BRIDGE_SN9C103), }, PAS106 */ | ||
81 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60aa, BRIDGE_SN9C103), }, TAS5130 */ | ||
82 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60ab, BRIDGE_SN9C103), }, TAS5110, non existent */ | ||
83 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60ac, BRIDGE_SN9C103), }, non existent ? */ | ||
84 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60ae, BRIDGE_SN9C103), }, non existent ? */ | ||
85 | { SN9C102_USB_DEVICE(0x0c45, 0x60af, BRIDGE_SN9C103), }, | ||
86 | { SN9C102_USB_DEVICE(0x0c45, 0x60b0, BRIDGE_SN9C103), }, | ||
87 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60b2, BRIDGE_SN9C103), }, non existent ? */ | ||
88 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60b3, BRIDGE_SN9C103), }, non existent ? */ | ||
89 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60b8, BRIDGE_SN9C103), }, non existent ? */ | ||
90 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60ba, BRIDGE_SN9C103), }, non existent ? */ | ||
91 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60bb, BRIDGE_SN9C103), }, non existent ? */ | ||
92 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60bc, BRIDGE_SN9C103), }, non existent ? */ | ||
93 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60be, BRIDGE_SN9C103), }, non existent ? */ | ||
94 | #endif | ||
95 | /* SN9C105 */ | ||
96 | #if !defined CONFIG_USB_GSPCA_SONIXJ && !defined CONFIG_USB_GSPCA_SONIXJ_MODULE | ||
97 | { SN9C102_USB_DEVICE(0x045e, 0x00f5, BRIDGE_SN9C105), }, | ||
98 | { SN9C102_USB_DEVICE(0x045e, 0x00f7, BRIDGE_SN9C105), }, | ||
99 | { SN9C102_USB_DEVICE(0x0471, 0x0327, BRIDGE_SN9C105), }, | ||
100 | { SN9C102_USB_DEVICE(0x0471, 0x0328, BRIDGE_SN9C105), }, | ||
101 | { SN9C102_USB_DEVICE(0x0c45, 0x60c0, BRIDGE_SN9C105), }, | ||
102 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60c2, BRIDGE_SN9C105), }, PO1030 */ | ||
103 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60c8, BRIDGE_SN9C105), }, OM6801 */ | ||
104 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60cc, BRIDGE_SN9C105), }, HV7131GP */ | ||
105 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60ea, BRIDGE_SN9C105), }, non existent ? */ | ||
106 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60ec, BRIDGE_SN9C105), }, MO4000 */ | ||
107 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60ef, BRIDGE_SN9C105), }, ICM105C */ | ||
108 | /* { SN9C102_USB_DEVICE(0x0c45, 0x60fa, BRIDGE_SN9C105), }, OV7648 */ | ||
109 | { SN9C102_USB_DEVICE(0x0c45, 0x60fb, BRIDGE_SN9C105), }, | ||
110 | { SN9C102_USB_DEVICE(0x0c45, 0x60fc, BRIDGE_SN9C105), }, | ||
111 | { SN9C102_USB_DEVICE(0x0c45, 0x60fe, BRIDGE_SN9C105), }, | ||
112 | /* SN9C120 */ | ||
113 | { SN9C102_USB_DEVICE(0x0458, 0x7025, BRIDGE_SN9C120), }, | ||
114 | /* { SN9C102_USB_DEVICE(0x0c45, 0x6102, BRIDGE_SN9C120), }, po2030 */ | ||
115 | /* { SN9C102_USB_DEVICE(0x0c45, 0x6108, BRIDGE_SN9C120), }, om6801 */ | ||
116 | /* { SN9C102_USB_DEVICE(0x0c45, 0x610f, BRIDGE_SN9C120), }, S5K53BEB */ | ||
117 | { SN9C102_USB_DEVICE(0x0c45, 0x6130, BRIDGE_SN9C120), }, | ||
118 | /* { SN9C102_USB_DEVICE(0x0c45, 0x6138, BRIDGE_SN9C120), }, MO8000 */ | ||
119 | { SN9C102_USB_DEVICE(0x0c45, 0x613a, BRIDGE_SN9C120), }, | ||
120 | { SN9C102_USB_DEVICE(0x0c45, 0x613b, BRIDGE_SN9C120), }, | ||
121 | { SN9C102_USB_DEVICE(0x0c45, 0x613c, BRIDGE_SN9C120), }, | ||
122 | { SN9C102_USB_DEVICE(0x0c45, 0x613e, BRIDGE_SN9C120), }, | ||
123 | #endif | ||
124 | { } | ||
125 | }; | ||
126 | |||
127 | /* | ||
128 | Probing functions: on success, you must attach the sensor to the camera | ||
129 | by calling sn9c102_attach_sensor(). | ||
130 | To enable the I2C communication, you might need to perform a really basic | ||
131 | initialization of the SN9C1XX chip. | ||
132 | Functions must return 0 on success, the appropriate error otherwise. | ||
133 | */ | ||
134 | extern int sn9c102_probe_hv7131d(struct sn9c102_device* cam); | ||
135 | extern int sn9c102_probe_hv7131r(struct sn9c102_device* cam); | ||
136 | extern int sn9c102_probe_mi0343(struct sn9c102_device* cam); | ||
137 | extern int sn9c102_probe_mi0360(struct sn9c102_device* cam); | ||
138 | extern int sn9c102_probe_mt9v111(struct sn9c102_device *cam); | ||
139 | extern int sn9c102_probe_ov7630(struct sn9c102_device* cam); | ||
140 | extern int sn9c102_probe_ov7660(struct sn9c102_device* cam); | ||
141 | extern int sn9c102_probe_pas106b(struct sn9c102_device* cam); | ||
142 | extern int sn9c102_probe_pas202bcb(struct sn9c102_device* cam); | ||
143 | extern int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam); | ||
144 | extern int sn9c102_probe_tas5110d(struct sn9c102_device* cam); | ||
145 | extern int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam); | ||
146 | |||
147 | #endif /* _SN9C102_DEVTABLE_H_ */ | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_hv7131d.c b/drivers/media/usb/sn9c102/sn9c102_hv7131d.c new file mode 100644 index 000000000000..2dce5c908c8e --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_hv7131d.c | |||
@@ -0,0 +1,264 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for HV7131D image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int hv7131d_init(struct sn9c102_device* cam) | ||
27 | { | ||
28 | int err; | ||
29 | |||
30 | err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11}, | ||
31 | {0x00, 0x14}, {0x60, 0x17}, | ||
32 | {0x0e, 0x18}, {0xf2, 0x19}); | ||
33 | |||
34 | err += sn9c102_i2c_write(cam, 0x01, 0x04); | ||
35 | err += sn9c102_i2c_write(cam, 0x02, 0x00); | ||
36 | err += sn9c102_i2c_write(cam, 0x28, 0x00); | ||
37 | |||
38 | return err; | ||
39 | } | ||
40 | |||
41 | |||
42 | static int hv7131d_get_ctrl(struct sn9c102_device* cam, | ||
43 | struct v4l2_control* ctrl) | ||
44 | { | ||
45 | switch (ctrl->id) { | ||
46 | case V4L2_CID_EXPOSURE: | ||
47 | { | ||
48 | int r1 = sn9c102_i2c_read(cam, 0x26), | ||
49 | r2 = sn9c102_i2c_read(cam, 0x27); | ||
50 | if (r1 < 0 || r2 < 0) | ||
51 | return -EIO; | ||
52 | ctrl->value = (r1 << 8) | (r2 & 0xff); | ||
53 | } | ||
54 | return 0; | ||
55 | case V4L2_CID_RED_BALANCE: | ||
56 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x31)) < 0) | ||
57 | return -EIO; | ||
58 | ctrl->value = 0x3f - (ctrl->value & 0x3f); | ||
59 | return 0; | ||
60 | case V4L2_CID_BLUE_BALANCE: | ||
61 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x33)) < 0) | ||
62 | return -EIO; | ||
63 | ctrl->value = 0x3f - (ctrl->value & 0x3f); | ||
64 | return 0; | ||
65 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
66 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x32)) < 0) | ||
67 | return -EIO; | ||
68 | ctrl->value = 0x3f - (ctrl->value & 0x3f); | ||
69 | return 0; | ||
70 | case SN9C102_V4L2_CID_RESET_LEVEL: | ||
71 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x30)) < 0) | ||
72 | return -EIO; | ||
73 | ctrl->value &= 0x3f; | ||
74 | return 0; | ||
75 | case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE: | ||
76 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x34)) < 0) | ||
77 | return -EIO; | ||
78 | ctrl->value &= 0x07; | ||
79 | return 0; | ||
80 | default: | ||
81 | return -EINVAL; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | |||
86 | static int hv7131d_set_ctrl(struct sn9c102_device* cam, | ||
87 | const struct v4l2_control* ctrl) | ||
88 | { | ||
89 | int err = 0; | ||
90 | |||
91 | switch (ctrl->id) { | ||
92 | case V4L2_CID_EXPOSURE: | ||
93 | err += sn9c102_i2c_write(cam, 0x26, ctrl->value >> 8); | ||
94 | err += sn9c102_i2c_write(cam, 0x27, ctrl->value & 0xff); | ||
95 | break; | ||
96 | case V4L2_CID_RED_BALANCE: | ||
97 | err += sn9c102_i2c_write(cam, 0x31, 0x3f - ctrl->value); | ||
98 | break; | ||
99 | case V4L2_CID_BLUE_BALANCE: | ||
100 | err += sn9c102_i2c_write(cam, 0x33, 0x3f - ctrl->value); | ||
101 | break; | ||
102 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
103 | err += sn9c102_i2c_write(cam, 0x32, 0x3f - ctrl->value); | ||
104 | break; | ||
105 | case SN9C102_V4L2_CID_RESET_LEVEL: | ||
106 | err += sn9c102_i2c_write(cam, 0x30, ctrl->value); | ||
107 | break; | ||
108 | case SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE: | ||
109 | err += sn9c102_i2c_write(cam, 0x34, ctrl->value); | ||
110 | break; | ||
111 | default: | ||
112 | return -EINVAL; | ||
113 | } | ||
114 | |||
115 | return err ? -EIO : 0; | ||
116 | } | ||
117 | |||
118 | |||
119 | static int hv7131d_set_crop(struct sn9c102_device* cam, | ||
120 | const struct v4l2_rect* rect) | ||
121 | { | ||
122 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
123 | int err = 0; | ||
124 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 2, | ||
125 | v_start = (u8)(rect->top - s->cropcap.bounds.top) + 2; | ||
126 | |||
127 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
128 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
129 | |||
130 | return err; | ||
131 | } | ||
132 | |||
133 | |||
134 | static int hv7131d_set_pix_format(struct sn9c102_device* cam, | ||
135 | const struct v4l2_pix_format* pix) | ||
136 | { | ||
137 | int err = 0; | ||
138 | |||
139 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) | ||
140 | err += sn9c102_write_reg(cam, 0x42, 0x19); | ||
141 | else | ||
142 | err += sn9c102_write_reg(cam, 0xf2, 0x19); | ||
143 | |||
144 | return err; | ||
145 | } | ||
146 | |||
147 | |||
148 | static const struct sn9c102_sensor hv7131d = { | ||
149 | .name = "HV7131D", | ||
150 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
151 | .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102, | ||
152 | .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE, | ||
153 | .frequency = SN9C102_I2C_100KHZ, | ||
154 | .interface = SN9C102_I2C_2WIRES, | ||
155 | .i2c_slave_id = 0x11, | ||
156 | .init = &hv7131d_init, | ||
157 | .qctrl = { | ||
158 | { | ||
159 | .id = V4L2_CID_EXPOSURE, | ||
160 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
161 | .name = "exposure", | ||
162 | .minimum = 0x0250, | ||
163 | .maximum = 0xffff, | ||
164 | .step = 0x0001, | ||
165 | .default_value = 0x0250, | ||
166 | .flags = 0, | ||
167 | }, | ||
168 | { | ||
169 | .id = V4L2_CID_RED_BALANCE, | ||
170 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
171 | .name = "red balance", | ||
172 | .minimum = 0x00, | ||
173 | .maximum = 0x3f, | ||
174 | .step = 0x01, | ||
175 | .default_value = 0x00, | ||
176 | .flags = 0, | ||
177 | }, | ||
178 | { | ||
179 | .id = V4L2_CID_BLUE_BALANCE, | ||
180 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
181 | .name = "blue balance", | ||
182 | .minimum = 0x00, | ||
183 | .maximum = 0x3f, | ||
184 | .step = 0x01, | ||
185 | .default_value = 0x20, | ||
186 | .flags = 0, | ||
187 | }, | ||
188 | { | ||
189 | .id = SN9C102_V4L2_CID_GREEN_BALANCE, | ||
190 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
191 | .name = "green balance", | ||
192 | .minimum = 0x00, | ||
193 | .maximum = 0x3f, | ||
194 | .step = 0x01, | ||
195 | .default_value = 0x1e, | ||
196 | .flags = 0, | ||
197 | }, | ||
198 | { | ||
199 | .id = SN9C102_V4L2_CID_RESET_LEVEL, | ||
200 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
201 | .name = "reset level", | ||
202 | .minimum = 0x19, | ||
203 | .maximum = 0x3f, | ||
204 | .step = 0x01, | ||
205 | .default_value = 0x30, | ||
206 | .flags = 0, | ||
207 | }, | ||
208 | { | ||
209 | .id = SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE, | ||
210 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
211 | .name = "pixel bias voltage", | ||
212 | .minimum = 0x00, | ||
213 | .maximum = 0x07, | ||
214 | .step = 0x01, | ||
215 | .default_value = 0x02, | ||
216 | .flags = 0, | ||
217 | }, | ||
218 | }, | ||
219 | .get_ctrl = &hv7131d_get_ctrl, | ||
220 | .set_ctrl = &hv7131d_set_ctrl, | ||
221 | .cropcap = { | ||
222 | .bounds = { | ||
223 | .left = 0, | ||
224 | .top = 0, | ||
225 | .width = 640, | ||
226 | .height = 480, | ||
227 | }, | ||
228 | .defrect = { | ||
229 | .left = 0, | ||
230 | .top = 0, | ||
231 | .width = 640, | ||
232 | .height = 480, | ||
233 | }, | ||
234 | }, | ||
235 | .set_crop = &hv7131d_set_crop, | ||
236 | .pix_format = { | ||
237 | .width = 640, | ||
238 | .height = 480, | ||
239 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
240 | .priv = 8, | ||
241 | }, | ||
242 | .set_pix_format = &hv7131d_set_pix_format | ||
243 | }; | ||
244 | |||
245 | |||
246 | int sn9c102_probe_hv7131d(struct sn9c102_device* cam) | ||
247 | { | ||
248 | int r0 = 0, r1 = 0, err; | ||
249 | |||
250 | err = sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01}, | ||
251 | {0x28, 0x17}); | ||
252 | |||
253 | r0 = sn9c102_i2c_try_read(cam, &hv7131d, 0x00); | ||
254 | r1 = sn9c102_i2c_try_read(cam, &hv7131d, 0x01); | ||
255 | if (err || r0 < 0 || r1 < 0) | ||
256 | return -EIO; | ||
257 | |||
258 | if ((r0 != 0x00 && r0 != 0x01) || r1 != 0x04) | ||
259 | return -ENODEV; | ||
260 | |||
261 | sn9c102_attach_sensor(cam, &hv7131d); | ||
262 | |||
263 | return 0; | ||
264 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_hv7131r.c b/drivers/media/usb/sn9c102/sn9c102_hv7131r.c new file mode 100644 index 000000000000..4295887ff609 --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_hv7131r.c | |||
@@ -0,0 +1,363 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for HV7131R image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int hv7131r_init(struct sn9c102_device* cam) | ||
27 | { | ||
28 | int err = 0; | ||
29 | |||
30 | switch (sn9c102_get_bridge(cam)) { | ||
31 | case BRIDGE_SN9C103: | ||
32 | err = sn9c102_write_const_regs(cam, {0x00, 0x03}, {0x1a, 0x04}, | ||
33 | {0x20, 0x05}, {0x20, 0x06}, | ||
34 | {0x03, 0x10}, {0x00, 0x14}, | ||
35 | {0x60, 0x17}, {0x0a, 0x18}, | ||
36 | {0xf0, 0x19}, {0x1d, 0x1a}, | ||
37 | {0x10, 0x1b}, {0x02, 0x1c}, | ||
38 | {0x03, 0x1d}, {0x0f, 0x1e}, | ||
39 | {0x0c, 0x1f}, {0x00, 0x20}, | ||
40 | {0x10, 0x21}, {0x20, 0x22}, | ||
41 | {0x30, 0x23}, {0x40, 0x24}, | ||
42 | {0x50, 0x25}, {0x60, 0x26}, | ||
43 | {0x70, 0x27}, {0x80, 0x28}, | ||
44 | {0x90, 0x29}, {0xa0, 0x2a}, | ||
45 | {0xb0, 0x2b}, {0xc0, 0x2c}, | ||
46 | {0xd0, 0x2d}, {0xe0, 0x2e}, | ||
47 | {0xf0, 0x2f}, {0xff, 0x30}); | ||
48 | break; | ||
49 | case BRIDGE_SN9C105: | ||
50 | case BRIDGE_SN9C120: | ||
51 | err = sn9c102_write_const_regs(cam, {0x44, 0x01}, {0x40, 0x02}, | ||
52 | {0x00, 0x03}, {0x1a, 0x04}, | ||
53 | {0x44, 0x05}, {0x3e, 0x06}, | ||
54 | {0x1a, 0x07}, {0x03, 0x10}, | ||
55 | {0x08, 0x14}, {0xa3, 0x17}, | ||
56 | {0x4b, 0x18}, {0x00, 0x19}, | ||
57 | {0x1d, 0x1a}, {0x10, 0x1b}, | ||
58 | {0x02, 0x1c}, {0x03, 0x1d}, | ||
59 | {0x0f, 0x1e}, {0x0c, 0x1f}, | ||
60 | {0x00, 0x20}, {0x29, 0x21}, | ||
61 | {0x40, 0x22}, {0x54, 0x23}, | ||
62 | {0x66, 0x24}, {0x76, 0x25}, | ||
63 | {0x85, 0x26}, {0x94, 0x27}, | ||
64 | {0xa1, 0x28}, {0xae, 0x29}, | ||
65 | {0xbb, 0x2a}, {0xc7, 0x2b}, | ||
66 | {0xd3, 0x2c}, {0xde, 0x2d}, | ||
67 | {0xea, 0x2e}, {0xf4, 0x2f}, | ||
68 | {0xff, 0x30}, {0x00, 0x3F}, | ||
69 | {0xC7, 0x40}, {0x01, 0x41}, | ||
70 | {0x44, 0x42}, {0x00, 0x43}, | ||
71 | {0x44, 0x44}, {0x00, 0x45}, | ||
72 | {0x44, 0x46}, {0x00, 0x47}, | ||
73 | {0xC7, 0x48}, {0x01, 0x49}, | ||
74 | {0xC7, 0x4A}, {0x01, 0x4B}, | ||
75 | {0xC7, 0x4C}, {0x01, 0x4D}, | ||
76 | {0x44, 0x4E}, {0x00, 0x4F}, | ||
77 | {0x44, 0x50}, {0x00, 0x51}, | ||
78 | {0x44, 0x52}, {0x00, 0x53}, | ||
79 | {0xC7, 0x54}, {0x01, 0x55}, | ||
80 | {0xC7, 0x56}, {0x01, 0x57}, | ||
81 | {0xC7, 0x58}, {0x01, 0x59}, | ||
82 | {0x44, 0x5A}, {0x00, 0x5B}, | ||
83 | {0x44, 0x5C}, {0x00, 0x5D}, | ||
84 | {0x44, 0x5E}, {0x00, 0x5F}, | ||
85 | {0xC7, 0x60}, {0x01, 0x61}, | ||
86 | {0xC7, 0x62}, {0x01, 0x63}, | ||
87 | {0xC7, 0x64}, {0x01, 0x65}, | ||
88 | {0x44, 0x66}, {0x00, 0x67}, | ||
89 | {0x44, 0x68}, {0x00, 0x69}, | ||
90 | {0x44, 0x6A}, {0x00, 0x6B}, | ||
91 | {0xC7, 0x6C}, {0x01, 0x6D}, | ||
92 | {0xC7, 0x6E}, {0x01, 0x6F}, | ||
93 | {0xC7, 0x70}, {0x01, 0x71}, | ||
94 | {0x44, 0x72}, {0x00, 0x73}, | ||
95 | {0x44, 0x74}, {0x00, 0x75}, | ||
96 | {0x44, 0x76}, {0x00, 0x77}, | ||
97 | {0xC7, 0x78}, {0x01, 0x79}, | ||
98 | {0xC7, 0x7A}, {0x01, 0x7B}, | ||
99 | {0xC7, 0x7C}, {0x01, 0x7D}, | ||
100 | {0x44, 0x7E}, {0x00, 0x7F}, | ||
101 | {0x14, 0x84}, {0x00, 0x85}, | ||
102 | {0x27, 0x86}, {0x00, 0x87}, | ||
103 | {0x07, 0x88}, {0x00, 0x89}, | ||
104 | {0xEC, 0x8A}, {0x0f, 0x8B}, | ||
105 | {0xD8, 0x8C}, {0x0f, 0x8D}, | ||
106 | {0x3D, 0x8E}, {0x00, 0x8F}, | ||
107 | {0x3D, 0x90}, {0x00, 0x91}, | ||
108 | {0xCD, 0x92}, {0x0f, 0x93}, | ||
109 | {0xf7, 0x94}, {0x0f, 0x95}, | ||
110 | {0x0C, 0x96}, {0x00, 0x97}, | ||
111 | {0x00, 0x98}, {0x66, 0x99}, | ||
112 | {0x05, 0x9A}, {0x00, 0x9B}, | ||
113 | {0x04, 0x9C}, {0x00, 0x9D}, | ||
114 | {0x08, 0x9E}, {0x00, 0x9F}, | ||
115 | {0x2D, 0xC0}, {0x2D, 0xC1}, | ||
116 | {0x3A, 0xC2}, {0x05, 0xC3}, | ||
117 | {0x04, 0xC4}, {0x3F, 0xC5}, | ||
118 | {0x00, 0xC6}, {0x00, 0xC7}, | ||
119 | {0x50, 0xC8}, {0x3C, 0xC9}, | ||
120 | {0x28, 0xCA}, {0xD8, 0xCB}, | ||
121 | {0x14, 0xCC}, {0xEC, 0xCD}, | ||
122 | {0x32, 0xCE}, {0xDD, 0xCF}, | ||
123 | {0x32, 0xD0}, {0xDD, 0xD1}, | ||
124 | {0x6A, 0xD2}, {0x50, 0xD3}, | ||
125 | {0x00, 0xD4}, {0x00, 0xD5}, | ||
126 | {0x00, 0xD6}); | ||
127 | break; | ||
128 | default: | ||
129 | break; | ||
130 | } | ||
131 | |||
132 | err += sn9c102_i2c_write(cam, 0x20, 0x00); | ||
133 | err += sn9c102_i2c_write(cam, 0x21, 0xd6); | ||
134 | err += sn9c102_i2c_write(cam, 0x25, 0x06); | ||
135 | |||
136 | return err; | ||
137 | } | ||
138 | |||
139 | |||
140 | static int hv7131r_get_ctrl(struct sn9c102_device* cam, | ||
141 | struct v4l2_control* ctrl) | ||
142 | { | ||
143 | switch (ctrl->id) { | ||
144 | case V4L2_CID_GAIN: | ||
145 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x30)) < 0) | ||
146 | return -EIO; | ||
147 | return 0; | ||
148 | case V4L2_CID_RED_BALANCE: | ||
149 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x31)) < 0) | ||
150 | return -EIO; | ||
151 | ctrl->value = ctrl->value & 0x3f; | ||
152 | return 0; | ||
153 | case V4L2_CID_BLUE_BALANCE: | ||
154 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x33)) < 0) | ||
155 | return -EIO; | ||
156 | ctrl->value = ctrl->value & 0x3f; | ||
157 | return 0; | ||
158 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
159 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x32)) < 0) | ||
160 | return -EIO; | ||
161 | ctrl->value = ctrl->value & 0x3f; | ||
162 | return 0; | ||
163 | case V4L2_CID_BLACK_LEVEL: | ||
164 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x01)) < 0) | ||
165 | return -EIO; | ||
166 | ctrl->value = (ctrl->value & 0x08) ? 1 : 0; | ||
167 | return 0; | ||
168 | default: | ||
169 | return -EINVAL; | ||
170 | } | ||
171 | } | ||
172 | |||
173 | |||
174 | static int hv7131r_set_ctrl(struct sn9c102_device* cam, | ||
175 | const struct v4l2_control* ctrl) | ||
176 | { | ||
177 | int err = 0; | ||
178 | |||
179 | switch (ctrl->id) { | ||
180 | case V4L2_CID_GAIN: | ||
181 | err += sn9c102_i2c_write(cam, 0x30, ctrl->value); | ||
182 | break; | ||
183 | case V4L2_CID_RED_BALANCE: | ||
184 | err += sn9c102_i2c_write(cam, 0x31, ctrl->value); | ||
185 | break; | ||
186 | case V4L2_CID_BLUE_BALANCE: | ||
187 | err += sn9c102_i2c_write(cam, 0x33, ctrl->value); | ||
188 | break; | ||
189 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
190 | err += sn9c102_i2c_write(cam, 0x32, ctrl->value); | ||
191 | break; | ||
192 | case V4L2_CID_BLACK_LEVEL: | ||
193 | { | ||
194 | int r = sn9c102_i2c_read(cam, 0x01); | ||
195 | if (r < 0) | ||
196 | return -EIO; | ||
197 | err += sn9c102_i2c_write(cam, 0x01, | ||
198 | (ctrl->value<<3) | (r&0xf7)); | ||
199 | } | ||
200 | break; | ||
201 | default: | ||
202 | return -EINVAL; | ||
203 | } | ||
204 | |||
205 | return err ? -EIO : 0; | ||
206 | } | ||
207 | |||
208 | |||
209 | static int hv7131r_set_crop(struct sn9c102_device* cam, | ||
210 | const struct v4l2_rect* rect) | ||
211 | { | ||
212 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
213 | int err = 0; | ||
214 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1, | ||
215 | v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1; | ||
216 | |||
217 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
218 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
219 | |||
220 | return err; | ||
221 | } | ||
222 | |||
223 | |||
224 | static int hv7131r_set_pix_format(struct sn9c102_device* cam, | ||
225 | const struct v4l2_pix_format* pix) | ||
226 | { | ||
227 | int err = 0; | ||
228 | |||
229 | switch (sn9c102_get_bridge(cam)) { | ||
230 | case BRIDGE_SN9C103: | ||
231 | if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) { | ||
232 | err += sn9c102_write_reg(cam, 0xa0, 0x19); | ||
233 | err += sn9c102_i2c_write(cam, 0x01, 0x04); | ||
234 | } else { | ||
235 | err += sn9c102_write_reg(cam, 0x30, 0x19); | ||
236 | err += sn9c102_i2c_write(cam, 0x01, 0x04); | ||
237 | } | ||
238 | break; | ||
239 | case BRIDGE_SN9C105: | ||
240 | case BRIDGE_SN9C120: | ||
241 | if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) { | ||
242 | err += sn9c102_write_reg(cam, 0xa5, 0x17); | ||
243 | err += sn9c102_i2c_write(cam, 0x01, 0x24); | ||
244 | } else { | ||
245 | err += sn9c102_write_reg(cam, 0xa3, 0x17); | ||
246 | err += sn9c102_i2c_write(cam, 0x01, 0x04); | ||
247 | } | ||
248 | break; | ||
249 | default: | ||
250 | break; | ||
251 | } | ||
252 | |||
253 | return err; | ||
254 | } | ||
255 | |||
256 | |||
257 | static const struct sn9c102_sensor hv7131r = { | ||
258 | .name = "HV7131R", | ||
259 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
260 | .supported_bridge = BRIDGE_SN9C103 | BRIDGE_SN9C105 | BRIDGE_SN9C120, | ||
261 | .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE, | ||
262 | .frequency = SN9C102_I2C_100KHZ, | ||
263 | .interface = SN9C102_I2C_2WIRES, | ||
264 | .i2c_slave_id = 0x11, | ||
265 | .init = &hv7131r_init, | ||
266 | .qctrl = { | ||
267 | { | ||
268 | .id = V4L2_CID_GAIN, | ||
269 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
270 | .name = "global gain", | ||
271 | .minimum = 0x00, | ||
272 | .maximum = 0xff, | ||
273 | .step = 0x01, | ||
274 | .default_value = 0x40, | ||
275 | .flags = 0, | ||
276 | }, | ||
277 | { | ||
278 | .id = V4L2_CID_RED_BALANCE, | ||
279 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
280 | .name = "red balance", | ||
281 | .minimum = 0x00, | ||
282 | .maximum = 0x3f, | ||
283 | .step = 0x01, | ||
284 | .default_value = 0x08, | ||
285 | .flags = 0, | ||
286 | }, | ||
287 | { | ||
288 | .id = V4L2_CID_BLUE_BALANCE, | ||
289 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
290 | .name = "blue balance", | ||
291 | .minimum = 0x00, | ||
292 | .maximum = 0x3f, | ||
293 | .step = 0x01, | ||
294 | .default_value = 0x1a, | ||
295 | .flags = 0, | ||
296 | }, | ||
297 | { | ||
298 | .id = SN9C102_V4L2_CID_GREEN_BALANCE, | ||
299 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
300 | .name = "green balance", | ||
301 | .minimum = 0x00, | ||
302 | .maximum = 0x3f, | ||
303 | .step = 0x01, | ||
304 | .default_value = 0x2f, | ||
305 | .flags = 0, | ||
306 | }, | ||
307 | { | ||
308 | .id = V4L2_CID_BLACK_LEVEL, | ||
309 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
310 | .name = "auto black level compensation", | ||
311 | .minimum = 0x00, | ||
312 | .maximum = 0x01, | ||
313 | .step = 0x01, | ||
314 | .default_value = 0x00, | ||
315 | .flags = 0, | ||
316 | }, | ||
317 | }, | ||
318 | .get_ctrl = &hv7131r_get_ctrl, | ||
319 | .set_ctrl = &hv7131r_set_ctrl, | ||
320 | .cropcap = { | ||
321 | .bounds = { | ||
322 | .left = 0, | ||
323 | .top = 0, | ||
324 | .width = 640, | ||
325 | .height = 480, | ||
326 | }, | ||
327 | .defrect = { | ||
328 | .left = 0, | ||
329 | .top = 0, | ||
330 | .width = 640, | ||
331 | .height = 480, | ||
332 | }, | ||
333 | }, | ||
334 | .set_crop = &hv7131r_set_crop, | ||
335 | .pix_format = { | ||
336 | .width = 640, | ||
337 | .height = 480, | ||
338 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
339 | .priv = 8, | ||
340 | }, | ||
341 | .set_pix_format = &hv7131r_set_pix_format | ||
342 | }; | ||
343 | |||
344 | |||
345 | int sn9c102_probe_hv7131r(struct sn9c102_device* cam) | ||
346 | { | ||
347 | int devid, err; | ||
348 | |||
349 | err = sn9c102_write_const_regs(cam, {0x09, 0x01}, {0x44, 0x02}, | ||
350 | {0x34, 0x01}, {0x20, 0x17}, | ||
351 | {0x34, 0x01}, {0x46, 0x01}); | ||
352 | |||
353 | devid = sn9c102_i2c_try_read(cam, &hv7131r, 0x00); | ||
354 | if (err || devid < 0) | ||
355 | return -EIO; | ||
356 | |||
357 | if (devid != 0x02) | ||
358 | return -ENODEV; | ||
359 | |||
360 | sn9c102_attach_sensor(cam, &hv7131r); | ||
361 | |||
362 | return 0; | ||
363 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_mi0343.c b/drivers/media/usb/sn9c102/sn9c102_mi0343.c new file mode 100644 index 000000000000..1f5b09bec89c --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_mi0343.c | |||
@@ -0,0 +1,352 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for MI-0343 image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int mi0343_init(struct sn9c102_device* cam) | ||
27 | { | ||
28 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
29 | int err = 0; | ||
30 | |||
31 | err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11}, | ||
32 | {0x0a, 0x14}, {0x40, 0x01}, | ||
33 | {0x20, 0x17}, {0x07, 0x18}, | ||
34 | {0xa0, 0x19}); | ||
35 | |||
36 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d, | ||
37 | 0x00, 0x01, 0, 0); | ||
38 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d, | ||
39 | 0x00, 0x00, 0, 0); | ||
40 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03, | ||
41 | 0x01, 0xe1, 0, 0); | ||
42 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04, | ||
43 | 0x02, 0x81, 0, 0); | ||
44 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05, | ||
45 | 0x00, 0x17, 0, 0); | ||
46 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06, | ||
47 | 0x00, 0x11, 0, 0); | ||
48 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x62, | ||
49 | 0x04, 0x9a, 0, 0); | ||
50 | |||
51 | return err; | ||
52 | } | ||
53 | |||
54 | |||
55 | static int mi0343_get_ctrl(struct sn9c102_device* cam, | ||
56 | struct v4l2_control* ctrl) | ||
57 | { | ||
58 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
59 | u8 data[2]; | ||
60 | |||
61 | switch (ctrl->id) { | ||
62 | case V4L2_CID_EXPOSURE: | ||
63 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x09, 2, | ||
64 | data) < 0) | ||
65 | return -EIO; | ||
66 | ctrl->value = data[0]; | ||
67 | return 0; | ||
68 | case V4L2_CID_GAIN: | ||
69 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x35, 2, | ||
70 | data) < 0) | ||
71 | return -EIO; | ||
72 | break; | ||
73 | case V4L2_CID_HFLIP: | ||
74 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2, | ||
75 | data) < 0) | ||
76 | return -EIO; | ||
77 | ctrl->value = data[1] & 0x20 ? 1 : 0; | ||
78 | return 0; | ||
79 | case V4L2_CID_VFLIP: | ||
80 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2, | ||
81 | data) < 0) | ||
82 | return -EIO; | ||
83 | ctrl->value = data[1] & 0x80 ? 1 : 0; | ||
84 | return 0; | ||
85 | case V4L2_CID_RED_BALANCE: | ||
86 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2d, 2, | ||
87 | data) < 0) | ||
88 | return -EIO; | ||
89 | break; | ||
90 | case V4L2_CID_BLUE_BALANCE: | ||
91 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2c, 2, | ||
92 | data) < 0) | ||
93 | return -EIO; | ||
94 | break; | ||
95 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
96 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2e, 2, | ||
97 | data) < 0) | ||
98 | return -EIO; | ||
99 | break; | ||
100 | default: | ||
101 | return -EINVAL; | ||
102 | } | ||
103 | |||
104 | switch (ctrl->id) { | ||
105 | case V4L2_CID_GAIN: | ||
106 | case V4L2_CID_RED_BALANCE: | ||
107 | case V4L2_CID_BLUE_BALANCE: | ||
108 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
109 | ctrl->value = data[1] | (data[0] << 8); | ||
110 | if (ctrl->value >= 0x10 && ctrl->value <= 0x3f) | ||
111 | ctrl->value -= 0x10; | ||
112 | else if (ctrl->value >= 0x60 && ctrl->value <= 0x7f) | ||
113 | ctrl->value -= 0x60; | ||
114 | else if (ctrl->value >= 0xe0 && ctrl->value <= 0xff) | ||
115 | ctrl->value -= 0xe0; | ||
116 | } | ||
117 | |||
118 | return 0; | ||
119 | } | ||
120 | |||
121 | |||
122 | static int mi0343_set_ctrl(struct sn9c102_device* cam, | ||
123 | const struct v4l2_control* ctrl) | ||
124 | { | ||
125 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
126 | u16 reg = 0; | ||
127 | int err = 0; | ||
128 | |||
129 | switch (ctrl->id) { | ||
130 | case V4L2_CID_GAIN: | ||
131 | case V4L2_CID_RED_BALANCE: | ||
132 | case V4L2_CID_BLUE_BALANCE: | ||
133 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
134 | if (ctrl->value <= (0x3f-0x10)) | ||
135 | reg = 0x10 + ctrl->value; | ||
136 | else if (ctrl->value <= ((0x3f-0x10) + (0x7f-0x60))) | ||
137 | reg = 0x60 + (ctrl->value - (0x3f-0x10)); | ||
138 | else | ||
139 | reg = 0xe0 + (ctrl->value - (0x3f-0x10) - (0x7f-0x60)); | ||
140 | break; | ||
141 | } | ||
142 | |||
143 | switch (ctrl->id) { | ||
144 | case V4L2_CID_EXPOSURE: | ||
145 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
146 | 0x09, ctrl->value, 0x00, | ||
147 | 0, 0); | ||
148 | break; | ||
149 | case V4L2_CID_GAIN: | ||
150 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
151 | 0x35, reg >> 8, reg & 0xff, | ||
152 | 0, 0); | ||
153 | break; | ||
154 | case V4L2_CID_HFLIP: | ||
155 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
156 | 0x20, ctrl->value ? 0x40:0x00, | ||
157 | ctrl->value ? 0x20:0x00, | ||
158 | 0, 0); | ||
159 | break; | ||
160 | case V4L2_CID_VFLIP: | ||
161 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
162 | 0x20, ctrl->value ? 0x80:0x00, | ||
163 | ctrl->value ? 0x80:0x00, | ||
164 | 0, 0); | ||
165 | break; | ||
166 | case V4L2_CID_RED_BALANCE: | ||
167 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
168 | 0x2d, reg >> 8, reg & 0xff, | ||
169 | 0, 0); | ||
170 | break; | ||
171 | case V4L2_CID_BLUE_BALANCE: | ||
172 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
173 | 0x2c, reg >> 8, reg & 0xff, | ||
174 | 0, 0); | ||
175 | break; | ||
176 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
177 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
178 | 0x2b, reg >> 8, reg & 0xff, | ||
179 | 0, 0); | ||
180 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
181 | 0x2e, reg >> 8, reg & 0xff, | ||
182 | 0, 0); | ||
183 | break; | ||
184 | default: | ||
185 | return -EINVAL; | ||
186 | } | ||
187 | |||
188 | return err ? -EIO : 0; | ||
189 | } | ||
190 | |||
191 | |||
192 | static int mi0343_set_crop(struct sn9c102_device* cam, | ||
193 | const struct v4l2_rect* rect) | ||
194 | { | ||
195 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
196 | int err = 0; | ||
197 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 0, | ||
198 | v_start = (u8)(rect->top - s->cropcap.bounds.top) + 2; | ||
199 | |||
200 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
201 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
202 | |||
203 | return err; | ||
204 | } | ||
205 | |||
206 | |||
207 | static int mi0343_set_pix_format(struct sn9c102_device* cam, | ||
208 | const struct v4l2_pix_format* pix) | ||
209 | { | ||
210 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
211 | int err = 0; | ||
212 | |||
213 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) { | ||
214 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
215 | 0x0a, 0x00, 0x03, 0, 0); | ||
216 | err += sn9c102_write_reg(cam, 0x20, 0x19); | ||
217 | } else { | ||
218 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
219 | 0x0a, 0x00, 0x05, 0, 0); | ||
220 | err += sn9c102_write_reg(cam, 0xa0, 0x19); | ||
221 | } | ||
222 | |||
223 | return err; | ||
224 | } | ||
225 | |||
226 | |||
227 | static const struct sn9c102_sensor mi0343 = { | ||
228 | .name = "MI-0343", | ||
229 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
230 | .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102, | ||
231 | .frequency = SN9C102_I2C_100KHZ, | ||
232 | .interface = SN9C102_I2C_2WIRES, | ||
233 | .i2c_slave_id = 0x5d, | ||
234 | .init = &mi0343_init, | ||
235 | .qctrl = { | ||
236 | { | ||
237 | .id = V4L2_CID_EXPOSURE, | ||
238 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
239 | .name = "exposure", | ||
240 | .minimum = 0x00, | ||
241 | .maximum = 0x0f, | ||
242 | .step = 0x01, | ||
243 | .default_value = 0x06, | ||
244 | .flags = 0, | ||
245 | }, | ||
246 | { | ||
247 | .id = V4L2_CID_GAIN, | ||
248 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
249 | .name = "global gain", | ||
250 | .minimum = 0x00, | ||
251 | .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),/*0x6d*/ | ||
252 | .step = 0x01, | ||
253 | .default_value = 0x00, | ||
254 | .flags = 0, | ||
255 | }, | ||
256 | { | ||
257 | .id = V4L2_CID_HFLIP, | ||
258 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
259 | .name = "horizontal mirror", | ||
260 | .minimum = 0, | ||
261 | .maximum = 1, | ||
262 | .step = 1, | ||
263 | .default_value = 0, | ||
264 | .flags = 0, | ||
265 | }, | ||
266 | { | ||
267 | .id = V4L2_CID_VFLIP, | ||
268 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
269 | .name = "vertical mirror", | ||
270 | .minimum = 0, | ||
271 | .maximum = 1, | ||
272 | .step = 1, | ||
273 | .default_value = 0, | ||
274 | .flags = 0, | ||
275 | }, | ||
276 | { | ||
277 | .id = V4L2_CID_RED_BALANCE, | ||
278 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
279 | .name = "red balance", | ||
280 | .minimum = 0x00, | ||
281 | .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0), | ||
282 | .step = 0x01, | ||
283 | .default_value = 0x00, | ||
284 | .flags = 0, | ||
285 | }, | ||
286 | { | ||
287 | .id = V4L2_CID_BLUE_BALANCE, | ||
288 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
289 | .name = "blue balance", | ||
290 | .minimum = 0x00, | ||
291 | .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0), | ||
292 | .step = 0x01, | ||
293 | .default_value = 0x00, | ||
294 | .flags = 0, | ||
295 | }, | ||
296 | { | ||
297 | .id = SN9C102_V4L2_CID_GREEN_BALANCE, | ||
298 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
299 | .name = "green balance", | ||
300 | .minimum = 0x00, | ||
301 | .maximum = ((0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0)), | ||
302 | .step = 0x01, | ||
303 | .default_value = 0x00, | ||
304 | .flags = 0, | ||
305 | }, | ||
306 | }, | ||
307 | .get_ctrl = &mi0343_get_ctrl, | ||
308 | .set_ctrl = &mi0343_set_ctrl, | ||
309 | .cropcap = { | ||
310 | .bounds = { | ||
311 | .left = 0, | ||
312 | .top = 0, | ||
313 | .width = 640, | ||
314 | .height = 480, | ||
315 | }, | ||
316 | .defrect = { | ||
317 | .left = 0, | ||
318 | .top = 0, | ||
319 | .width = 640, | ||
320 | .height = 480, | ||
321 | }, | ||
322 | }, | ||
323 | .set_crop = &mi0343_set_crop, | ||
324 | .pix_format = { | ||
325 | .width = 640, | ||
326 | .height = 480, | ||
327 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
328 | .priv = 8, | ||
329 | }, | ||
330 | .set_pix_format = &mi0343_set_pix_format | ||
331 | }; | ||
332 | |||
333 | |||
334 | int sn9c102_probe_mi0343(struct sn9c102_device* cam) | ||
335 | { | ||
336 | u8 data[2]; | ||
337 | |||
338 | if (sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01}, | ||
339 | {0x28, 0x17})) | ||
340 | return -EIO; | ||
341 | |||
342 | if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id, 0x00, | ||
343 | 2, data) < 0) | ||
344 | return -EIO; | ||
345 | |||
346 | if (data[1] != 0x42 || data[0] != 0xe3) | ||
347 | return -ENODEV; | ||
348 | |||
349 | sn9c102_attach_sensor(cam, &mi0343); | ||
350 | |||
351 | return 0; | ||
352 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_mi0360.c b/drivers/media/usb/sn9c102/sn9c102_mi0360.c new file mode 100644 index 000000000000..d973fc1973d9 --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_mi0360.c | |||
@@ -0,0 +1,453 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for MI-0360 image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int mi0360_init(struct sn9c102_device* cam) | ||
27 | { | ||
28 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
29 | int err = 0; | ||
30 | |||
31 | switch (sn9c102_get_bridge(cam)) { | ||
32 | case BRIDGE_SN9C103: | ||
33 | err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11}, | ||
34 | {0x0a, 0x14}, {0x40, 0x01}, | ||
35 | {0x20, 0x17}, {0x07, 0x18}, | ||
36 | {0xa0, 0x19}, {0x02, 0x1c}, | ||
37 | {0x03, 0x1d}, {0x0f, 0x1e}, | ||
38 | {0x0c, 0x1f}, {0x00, 0x20}, | ||
39 | {0x10, 0x21}, {0x20, 0x22}, | ||
40 | {0x30, 0x23}, {0x40, 0x24}, | ||
41 | {0x50, 0x25}, {0x60, 0x26}, | ||
42 | {0x70, 0x27}, {0x80, 0x28}, | ||
43 | {0x90, 0x29}, {0xa0, 0x2a}, | ||
44 | {0xb0, 0x2b}, {0xc0, 0x2c}, | ||
45 | {0xd0, 0x2d}, {0xe0, 0x2e}, | ||
46 | {0xf0, 0x2f}, {0xff, 0x30}); | ||
47 | break; | ||
48 | case BRIDGE_SN9C105: | ||
49 | case BRIDGE_SN9C120: | ||
50 | err = sn9c102_write_const_regs(cam, {0x44, 0x01}, {0x40, 0x02}, | ||
51 | {0x00, 0x03}, {0x1a, 0x04}, | ||
52 | {0x50, 0x05}, {0x20, 0x06}, | ||
53 | {0x10, 0x07}, {0x03, 0x10}, | ||
54 | {0x08, 0x14}, {0xa2, 0x17}, | ||
55 | {0x47, 0x18}, {0x00, 0x19}, | ||
56 | {0x1d, 0x1a}, {0x10, 0x1b}, | ||
57 | {0x02, 0x1c}, {0x03, 0x1d}, | ||
58 | {0x0f, 0x1e}, {0x0c, 0x1f}, | ||
59 | {0x00, 0x20}, {0x29, 0x21}, | ||
60 | {0x40, 0x22}, {0x54, 0x23}, | ||
61 | {0x66, 0x24}, {0x76, 0x25}, | ||
62 | {0x85, 0x26}, {0x94, 0x27}, | ||
63 | {0xa1, 0x28}, {0xae, 0x29}, | ||
64 | {0xbb, 0x2a}, {0xc7, 0x2b}, | ||
65 | {0xd3, 0x2c}, {0xde, 0x2d}, | ||
66 | {0xea, 0x2e}, {0xf4, 0x2f}, | ||
67 | {0xff, 0x30}, {0x00, 0x3F}, | ||
68 | {0xC7, 0x40}, {0x01, 0x41}, | ||
69 | {0x44, 0x42}, {0x00, 0x43}, | ||
70 | {0x44, 0x44}, {0x00, 0x45}, | ||
71 | {0x44, 0x46}, {0x00, 0x47}, | ||
72 | {0xC7, 0x48}, {0x01, 0x49}, | ||
73 | {0xC7, 0x4A}, {0x01, 0x4B}, | ||
74 | {0xC7, 0x4C}, {0x01, 0x4D}, | ||
75 | {0x44, 0x4E}, {0x00, 0x4F}, | ||
76 | {0x44, 0x50}, {0x00, 0x51}, | ||
77 | {0x44, 0x52}, {0x00, 0x53}, | ||
78 | {0xC7, 0x54}, {0x01, 0x55}, | ||
79 | {0xC7, 0x56}, {0x01, 0x57}, | ||
80 | {0xC7, 0x58}, {0x01, 0x59}, | ||
81 | {0x44, 0x5A}, {0x00, 0x5B}, | ||
82 | {0x44, 0x5C}, {0x00, 0x5D}, | ||
83 | {0x44, 0x5E}, {0x00, 0x5F}, | ||
84 | {0xC7, 0x60}, {0x01, 0x61}, | ||
85 | {0xC7, 0x62}, {0x01, 0x63}, | ||
86 | {0xC7, 0x64}, {0x01, 0x65}, | ||
87 | {0x44, 0x66}, {0x00, 0x67}, | ||
88 | {0x44, 0x68}, {0x00, 0x69}, | ||
89 | {0x44, 0x6A}, {0x00, 0x6B}, | ||
90 | {0xC7, 0x6C}, {0x01, 0x6D}, | ||
91 | {0xC7, 0x6E}, {0x01, 0x6F}, | ||
92 | {0xC7, 0x70}, {0x01, 0x71}, | ||
93 | {0x44, 0x72}, {0x00, 0x73}, | ||
94 | {0x44, 0x74}, {0x00, 0x75}, | ||
95 | {0x44, 0x76}, {0x00, 0x77}, | ||
96 | {0xC7, 0x78}, {0x01, 0x79}, | ||
97 | {0xC7, 0x7A}, {0x01, 0x7B}, | ||
98 | {0xC7, 0x7C}, {0x01, 0x7D}, | ||
99 | {0x44, 0x7E}, {0x00, 0x7F}, | ||
100 | {0x14, 0x84}, {0x00, 0x85}, | ||
101 | {0x27, 0x86}, {0x00, 0x87}, | ||
102 | {0x07, 0x88}, {0x00, 0x89}, | ||
103 | {0xEC, 0x8A}, {0x0f, 0x8B}, | ||
104 | {0xD8, 0x8C}, {0x0f, 0x8D}, | ||
105 | {0x3D, 0x8E}, {0x00, 0x8F}, | ||
106 | {0x3D, 0x90}, {0x00, 0x91}, | ||
107 | {0xCD, 0x92}, {0x0f, 0x93}, | ||
108 | {0xf7, 0x94}, {0x0f, 0x95}, | ||
109 | {0x0C, 0x96}, {0x00, 0x97}, | ||
110 | {0x00, 0x98}, {0x66, 0x99}, | ||
111 | {0x05, 0x9A}, {0x00, 0x9B}, | ||
112 | {0x04, 0x9C}, {0x00, 0x9D}, | ||
113 | {0x08, 0x9E}, {0x00, 0x9F}, | ||
114 | {0x2D, 0xC0}, {0x2D, 0xC1}, | ||
115 | {0x3A, 0xC2}, {0x05, 0xC3}, | ||
116 | {0x04, 0xC4}, {0x3F, 0xC5}, | ||
117 | {0x00, 0xC6}, {0x00, 0xC7}, | ||
118 | {0x50, 0xC8}, {0x3C, 0xC9}, | ||
119 | {0x28, 0xCA}, {0xD8, 0xCB}, | ||
120 | {0x14, 0xCC}, {0xEC, 0xCD}, | ||
121 | {0x32, 0xCE}, {0xDD, 0xCF}, | ||
122 | {0x32, 0xD0}, {0xDD, 0xD1}, | ||
123 | {0x6A, 0xD2}, {0x50, 0xD3}, | ||
124 | {0x00, 0xD4}, {0x00, 0xD5}, | ||
125 | {0x00, 0xD6}); | ||
126 | break; | ||
127 | default: | ||
128 | break; | ||
129 | } | ||
130 | |||
131 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d, | ||
132 | 0x00, 0x01, 0, 0); | ||
133 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d, | ||
134 | 0x00, 0x00, 0, 0); | ||
135 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03, | ||
136 | 0x01, 0xe1, 0, 0); | ||
137 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04, | ||
138 | 0x02, 0x81, 0, 0); | ||
139 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05, | ||
140 | 0x00, 0x17, 0, 0); | ||
141 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06, | ||
142 | 0x00, 0x11, 0, 0); | ||
143 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x62, | ||
144 | 0x04, 0x9a, 0, 0); | ||
145 | |||
146 | return err; | ||
147 | } | ||
148 | |||
149 | |||
150 | static int mi0360_get_ctrl(struct sn9c102_device* cam, | ||
151 | struct v4l2_control* ctrl) | ||
152 | { | ||
153 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
154 | u8 data[2]; | ||
155 | |||
156 | switch (ctrl->id) { | ||
157 | case V4L2_CID_EXPOSURE: | ||
158 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x09, 2, | ||
159 | data) < 0) | ||
160 | return -EIO; | ||
161 | ctrl->value = data[0]; | ||
162 | return 0; | ||
163 | case V4L2_CID_GAIN: | ||
164 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x35, 2, | ||
165 | data) < 0) | ||
166 | return -EIO; | ||
167 | ctrl->value = data[1]; | ||
168 | return 0; | ||
169 | case V4L2_CID_RED_BALANCE: | ||
170 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2c, 2, | ||
171 | data) < 0) | ||
172 | return -EIO; | ||
173 | ctrl->value = data[1]; | ||
174 | return 0; | ||
175 | case V4L2_CID_BLUE_BALANCE: | ||
176 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2d, 2, | ||
177 | data) < 0) | ||
178 | return -EIO; | ||
179 | ctrl->value = data[1]; | ||
180 | return 0; | ||
181 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
182 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2e, 2, | ||
183 | data) < 0) | ||
184 | return -EIO; | ||
185 | ctrl->value = data[1]; | ||
186 | return 0; | ||
187 | case V4L2_CID_HFLIP: | ||
188 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2, | ||
189 | data) < 0) | ||
190 | return -EIO; | ||
191 | ctrl->value = data[1] & 0x20 ? 1 : 0; | ||
192 | return 0; | ||
193 | case V4L2_CID_VFLIP: | ||
194 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2, | ||
195 | data) < 0) | ||
196 | return -EIO; | ||
197 | ctrl->value = data[1] & 0x80 ? 1 : 0; | ||
198 | return 0; | ||
199 | default: | ||
200 | return -EINVAL; | ||
201 | } | ||
202 | |||
203 | return 0; | ||
204 | } | ||
205 | |||
206 | |||
207 | static int mi0360_set_ctrl(struct sn9c102_device* cam, | ||
208 | const struct v4l2_control* ctrl) | ||
209 | { | ||
210 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
211 | int err = 0; | ||
212 | |||
213 | switch (ctrl->id) { | ||
214 | case V4L2_CID_EXPOSURE: | ||
215 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
216 | 0x09, ctrl->value, 0x00, | ||
217 | 0, 0); | ||
218 | break; | ||
219 | case V4L2_CID_GAIN: | ||
220 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
221 | 0x35, 0x03, ctrl->value, | ||
222 | 0, 0); | ||
223 | break; | ||
224 | case V4L2_CID_RED_BALANCE: | ||
225 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
226 | 0x2c, 0x03, ctrl->value, | ||
227 | 0, 0); | ||
228 | break; | ||
229 | case V4L2_CID_BLUE_BALANCE: | ||
230 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
231 | 0x2d, 0x03, ctrl->value, | ||
232 | 0, 0); | ||
233 | break; | ||
234 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
235 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
236 | 0x2b, 0x03, ctrl->value, | ||
237 | 0, 0); | ||
238 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
239 | 0x2e, 0x03, ctrl->value, | ||
240 | 0, 0); | ||
241 | break; | ||
242 | case V4L2_CID_HFLIP: | ||
243 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
244 | 0x20, ctrl->value ? 0x40:0x00, | ||
245 | ctrl->value ? 0x20:0x00, | ||
246 | 0, 0); | ||
247 | break; | ||
248 | case V4L2_CID_VFLIP: | ||
249 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
250 | 0x20, ctrl->value ? 0x80:0x00, | ||
251 | ctrl->value ? 0x80:0x00, | ||
252 | 0, 0); | ||
253 | break; | ||
254 | default: | ||
255 | return -EINVAL; | ||
256 | } | ||
257 | |||
258 | return err ? -EIO : 0; | ||
259 | } | ||
260 | |||
261 | |||
262 | static int mi0360_set_crop(struct sn9c102_device* cam, | ||
263 | const struct v4l2_rect* rect) | ||
264 | { | ||
265 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
266 | int err = 0; | ||
267 | u8 h_start = 0, v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1; | ||
268 | |||
269 | switch (sn9c102_get_bridge(cam)) { | ||
270 | case BRIDGE_SN9C103: | ||
271 | h_start = (u8)(rect->left - s->cropcap.bounds.left) + 0; | ||
272 | break; | ||
273 | case BRIDGE_SN9C105: | ||
274 | case BRIDGE_SN9C120: | ||
275 | h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1; | ||
276 | break; | ||
277 | default: | ||
278 | break; | ||
279 | } | ||
280 | |||
281 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
282 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
283 | |||
284 | return err; | ||
285 | } | ||
286 | |||
287 | |||
288 | static int mi0360_set_pix_format(struct sn9c102_device* cam, | ||
289 | const struct v4l2_pix_format* pix) | ||
290 | { | ||
291 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
292 | int err = 0; | ||
293 | |||
294 | if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) { | ||
295 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
296 | 0x0a, 0x00, 0x05, 0, 0); | ||
297 | err += sn9c102_write_reg(cam, 0x60, 0x19); | ||
298 | if (sn9c102_get_bridge(cam) == BRIDGE_SN9C105 || | ||
299 | sn9c102_get_bridge(cam) == BRIDGE_SN9C120) | ||
300 | err += sn9c102_write_reg(cam, 0xa6, 0x17); | ||
301 | } else { | ||
302 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
303 | 0x0a, 0x00, 0x02, 0, 0); | ||
304 | err += sn9c102_write_reg(cam, 0x20, 0x19); | ||
305 | if (sn9c102_get_bridge(cam) == BRIDGE_SN9C105 || | ||
306 | sn9c102_get_bridge(cam) == BRIDGE_SN9C120) | ||
307 | err += sn9c102_write_reg(cam, 0xa2, 0x17); | ||
308 | } | ||
309 | |||
310 | return err; | ||
311 | } | ||
312 | |||
313 | |||
314 | static const struct sn9c102_sensor mi0360 = { | ||
315 | .name = "MI-0360", | ||
316 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
317 | .supported_bridge = BRIDGE_SN9C103 | BRIDGE_SN9C105 | BRIDGE_SN9C120, | ||
318 | .frequency = SN9C102_I2C_100KHZ, | ||
319 | .interface = SN9C102_I2C_2WIRES, | ||
320 | .i2c_slave_id = 0x5d, | ||
321 | .init = &mi0360_init, | ||
322 | .qctrl = { | ||
323 | { | ||
324 | .id = V4L2_CID_EXPOSURE, | ||
325 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
326 | .name = "exposure", | ||
327 | .minimum = 0x00, | ||
328 | .maximum = 0x0f, | ||
329 | .step = 0x01, | ||
330 | .default_value = 0x05, | ||
331 | .flags = 0, | ||
332 | }, | ||
333 | { | ||
334 | .id = V4L2_CID_GAIN, | ||
335 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
336 | .name = "global gain", | ||
337 | .minimum = 0x00, | ||
338 | .maximum = 0x7f, | ||
339 | .step = 0x01, | ||
340 | .default_value = 0x25, | ||
341 | .flags = 0, | ||
342 | }, | ||
343 | { | ||
344 | .id = V4L2_CID_HFLIP, | ||
345 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
346 | .name = "horizontal mirror", | ||
347 | .minimum = 0, | ||
348 | .maximum = 1, | ||
349 | .step = 1, | ||
350 | .default_value = 0, | ||
351 | .flags = 0, | ||
352 | }, | ||
353 | { | ||
354 | .id = V4L2_CID_VFLIP, | ||
355 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
356 | .name = "vertical mirror", | ||
357 | .minimum = 0, | ||
358 | .maximum = 1, | ||
359 | .step = 1, | ||
360 | .default_value = 0, | ||
361 | .flags = 0, | ||
362 | }, | ||
363 | { | ||
364 | .id = V4L2_CID_BLUE_BALANCE, | ||
365 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
366 | .name = "blue balance", | ||
367 | .minimum = 0x00, | ||
368 | .maximum = 0x7f, | ||
369 | .step = 0x01, | ||
370 | .default_value = 0x0f, | ||
371 | .flags = 0, | ||
372 | }, | ||
373 | { | ||
374 | .id = V4L2_CID_RED_BALANCE, | ||
375 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
376 | .name = "red balance", | ||
377 | .minimum = 0x00, | ||
378 | .maximum = 0x7f, | ||
379 | .step = 0x01, | ||
380 | .default_value = 0x32, | ||
381 | .flags = 0, | ||
382 | }, | ||
383 | { | ||
384 | .id = SN9C102_V4L2_CID_GREEN_BALANCE, | ||
385 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
386 | .name = "green balance", | ||
387 | .minimum = 0x00, | ||
388 | .maximum = 0x7f, | ||
389 | .step = 0x01, | ||
390 | .default_value = 0x25, | ||
391 | .flags = 0, | ||
392 | }, | ||
393 | }, | ||
394 | .get_ctrl = &mi0360_get_ctrl, | ||
395 | .set_ctrl = &mi0360_set_ctrl, | ||
396 | .cropcap = { | ||
397 | .bounds = { | ||
398 | .left = 0, | ||
399 | .top = 0, | ||
400 | .width = 640, | ||
401 | .height = 480, | ||
402 | }, | ||
403 | .defrect = { | ||
404 | .left = 0, | ||
405 | .top = 0, | ||
406 | .width = 640, | ||
407 | .height = 480, | ||
408 | }, | ||
409 | }, | ||
410 | .set_crop = &mi0360_set_crop, | ||
411 | .pix_format = { | ||
412 | .width = 640, | ||
413 | .height = 480, | ||
414 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
415 | .priv = 8, | ||
416 | }, | ||
417 | .set_pix_format = &mi0360_set_pix_format | ||
418 | }; | ||
419 | |||
420 | |||
421 | int sn9c102_probe_mi0360(struct sn9c102_device* cam) | ||
422 | { | ||
423 | |||
424 | u8 data[2]; | ||
425 | |||
426 | switch (sn9c102_get_bridge(cam)) { | ||
427 | case BRIDGE_SN9C103: | ||
428 | if (sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01}, | ||
429 | {0x28, 0x17})) | ||
430 | return -EIO; | ||
431 | break; | ||
432 | case BRIDGE_SN9C105: | ||
433 | case BRIDGE_SN9C120: | ||
434 | if (sn9c102_write_const_regs(cam, {0x01, 0xf1}, {0x00, 0xf1}, | ||
435 | {0x01, 0x01}, {0x00, 0x01}, | ||
436 | {0x28, 0x17})) | ||
437 | return -EIO; | ||
438 | break; | ||
439 | default: | ||
440 | break; | ||
441 | } | ||
442 | |||
443 | if (sn9c102_i2c_try_raw_read(cam, &mi0360, mi0360.i2c_slave_id, 0x00, | ||
444 | 2, data) < 0) | ||
445 | return -EIO; | ||
446 | |||
447 | if (data[0] != 0x82 || data[1] != 0x43) | ||
448 | return -ENODEV; | ||
449 | |||
450 | sn9c102_attach_sensor(cam, &mi0360); | ||
451 | |||
452 | return 0; | ||
453 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_mt9v111.c b/drivers/media/usb/sn9c102/sn9c102_mt9v111.c new file mode 100644 index 000000000000..95986eb492e4 --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_mt9v111.c | |||
@@ -0,0 +1,260 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for MT9V111 image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int mt9v111_init(struct sn9c102_device *cam) | ||
27 | { | ||
28 | struct sn9c102_sensor *s = sn9c102_get_sensor(cam); | ||
29 | int err = 0; | ||
30 | |||
31 | err = sn9c102_write_const_regs(cam, {0x44, 0x01}, {0x40, 0x02}, | ||
32 | {0x00, 0x03}, {0x1a, 0x04}, | ||
33 | {0x1f, 0x05}, {0x20, 0x06}, | ||
34 | {0x1f, 0x07}, {0x81, 0x08}, | ||
35 | {0x5c, 0x09}, {0x00, 0x0a}, | ||
36 | {0x00, 0x0b}, {0x00, 0x0c}, | ||
37 | {0x00, 0x0d}, {0x00, 0x0e}, | ||
38 | {0x00, 0x0f}, {0x03, 0x10}, | ||
39 | {0x00, 0x11}, {0x00, 0x12}, | ||
40 | {0x02, 0x13}, {0x14, 0x14}, | ||
41 | {0x28, 0x15}, {0x1e, 0x16}, | ||
42 | {0xe2, 0x17}, {0x06, 0x18}, | ||
43 | {0x00, 0x19}, {0x00, 0x1a}, | ||
44 | {0x00, 0x1b}, {0x08, 0x20}, | ||
45 | {0x39, 0x21}, {0x51, 0x22}, | ||
46 | {0x63, 0x23}, {0x73, 0x24}, | ||
47 | {0x82, 0x25}, {0x8f, 0x26}, | ||
48 | {0x9b, 0x27}, {0xa7, 0x28}, | ||
49 | {0xb1, 0x29}, {0xbc, 0x2a}, | ||
50 | {0xc6, 0x2b}, {0xcf, 0x2c}, | ||
51 | {0xd8, 0x2d}, {0xe1, 0x2e}, | ||
52 | {0xea, 0x2f}, {0xf2, 0x30}, | ||
53 | {0x13, 0x84}, {0x00, 0x85}, | ||
54 | {0x25, 0x86}, {0x00, 0x87}, | ||
55 | {0x07, 0x88}, {0x00, 0x89}, | ||
56 | {0xee, 0x8a}, {0x0f, 0x8b}, | ||
57 | {0xe5, 0x8c}, {0x0f, 0x8d}, | ||
58 | {0x2e, 0x8e}, {0x00, 0x8f}, | ||
59 | {0x30, 0x90}, {0x00, 0x91}, | ||
60 | {0xd4, 0x92}, {0x0f, 0x93}, | ||
61 | {0xfc, 0x94}, {0x0f, 0x95}, | ||
62 | {0x14, 0x96}, {0x00, 0x97}, | ||
63 | {0x00, 0x98}, {0x60, 0x99}, | ||
64 | {0x07, 0x9a}, {0x40, 0x9b}, | ||
65 | {0x20, 0x9c}, {0x00, 0x9d}, | ||
66 | {0x00, 0x9e}, {0x00, 0x9f}, | ||
67 | {0x2d, 0xc0}, {0x2d, 0xc1}, | ||
68 | {0x3a, 0xc2}, {0x05, 0xc3}, | ||
69 | {0x04, 0xc4}, {0x3f, 0xc5}, | ||
70 | {0x00, 0xc6}, {0x00, 0xc7}, | ||
71 | {0x50, 0xc8}, {0x3c, 0xc9}, | ||
72 | {0x28, 0xca}, {0xd8, 0xcb}, | ||
73 | {0x14, 0xcc}, {0xec, 0xcd}, | ||
74 | {0x32, 0xce}, {0xdd, 0xcf}, | ||
75 | {0x2d, 0xd0}, {0xdd, 0xd1}, | ||
76 | {0x6a, 0xd2}, {0x50, 0xd3}, | ||
77 | {0x60, 0xd4}, {0x00, 0xd5}, | ||
78 | {0x00, 0xd6}); | ||
79 | |||
80 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x01, | ||
81 | 0x00, 0x01, 0, 0); | ||
82 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d, | ||
83 | 0x00, 0x01, 0, 0); | ||
84 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d, | ||
85 | 0x00, 0x00, 0, 0); | ||
86 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x08, | ||
87 | 0x04, 0x80, 0, 0); | ||
88 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x01, | ||
89 | 0x00, 0x04, 0, 0); | ||
90 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x08, | ||
91 | 0x00, 0x08, 0, 0); | ||
92 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x02, | ||
93 | 0x00, 0x16, 0, 0); | ||
94 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03, | ||
95 | 0x01, 0xe7, 0, 0); | ||
96 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04, | ||
97 | 0x02, 0x87, 0, 0); | ||
98 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06, | ||
99 | 0x00, 0x40, 0, 0); | ||
100 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05, | ||
101 | 0x00, 0x09, 0, 0); | ||
102 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x07, | ||
103 | 0x30, 0x02, 0, 0); | ||
104 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0c, | ||
105 | 0x00, 0x00, 0, 0); | ||
106 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x12, | ||
107 | 0x00, 0xb0, 0, 0); | ||
108 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x13, | ||
109 | 0x00, 0x7c, 0, 0); | ||
110 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x1e, | ||
111 | 0x00, 0x00, 0, 0); | ||
112 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x20, | ||
113 | 0x00, 0x00, 0, 0); | ||
114 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x20, | ||
115 | 0x00, 0x00, 0, 0); | ||
116 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x01, | ||
117 | 0x00, 0x04, 0, 0); | ||
118 | |||
119 | return err; | ||
120 | } | ||
121 | |||
122 | static int mt9v111_get_ctrl(struct sn9c102_device *cam, | ||
123 | struct v4l2_control *ctrl) | ||
124 | { | ||
125 | struct sn9c102_sensor *s = sn9c102_get_sensor(cam); | ||
126 | u8 data[2]; | ||
127 | int err = 0; | ||
128 | |||
129 | switch (ctrl->id) { | ||
130 | case V4L2_CID_VFLIP: | ||
131 | if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2, | ||
132 | data) < 0) | ||
133 | return -EIO; | ||
134 | ctrl->value = data[1] & 0x80 ? 1 : 0; | ||
135 | return 0; | ||
136 | default: | ||
137 | return -EINVAL; | ||
138 | } | ||
139 | |||
140 | return err ? -EIO : 0; | ||
141 | } | ||
142 | |||
143 | static int mt9v111_set_ctrl(struct sn9c102_device *cam, | ||
144 | const struct v4l2_control *ctrl) | ||
145 | { | ||
146 | struct sn9c102_sensor *s = sn9c102_get_sensor(cam); | ||
147 | int err = 0; | ||
148 | |||
149 | switch (ctrl->id) { | ||
150 | case V4L2_CID_VFLIP: | ||
151 | err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, | ||
152 | 0x20, | ||
153 | ctrl->value ? 0x80 : 0x00, | ||
154 | ctrl->value ? 0x80 : 0x00, 0, | ||
155 | 0); | ||
156 | break; | ||
157 | default: | ||
158 | return -EINVAL; | ||
159 | } | ||
160 | |||
161 | return err ? -EIO : 0; | ||
162 | } | ||
163 | |||
164 | static int mt9v111_set_crop(struct sn9c102_device *cam, | ||
165 | const struct v4l2_rect *rect) | ||
166 | { | ||
167 | struct sn9c102_sensor *s = sn9c102_get_sensor(cam); | ||
168 | int err = 0; | ||
169 | u8 v_start = (u8) (rect->top - s->cropcap.bounds.top) + 2; | ||
170 | |||
171 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
172 | |||
173 | return err; | ||
174 | } | ||
175 | |||
176 | static int mt9v111_set_pix_format(struct sn9c102_device *cam, | ||
177 | const struct v4l2_pix_format *pix) | ||
178 | { | ||
179 | int err = 0; | ||
180 | |||
181 | if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) { | ||
182 | err += sn9c102_write_reg(cam, 0xb4, 0x17); | ||
183 | } else { | ||
184 | err += sn9c102_write_reg(cam, 0xe2, 0x17); | ||
185 | } | ||
186 | |||
187 | return err; | ||
188 | } | ||
189 | |||
190 | |||
191 | static const struct sn9c102_sensor mt9v111 = { | ||
192 | .name = "MT9V111", | ||
193 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
194 | .supported_bridge = BRIDGE_SN9C105 | BRIDGE_SN9C120, | ||
195 | .frequency = SN9C102_I2C_100KHZ, | ||
196 | .interface = SN9C102_I2C_2WIRES, | ||
197 | .i2c_slave_id = 0x5c, | ||
198 | .init = &mt9v111_init, | ||
199 | .qctrl = { | ||
200 | { | ||
201 | .id = V4L2_CID_VFLIP, | ||
202 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
203 | .name = "vertical mirror", | ||
204 | .minimum = 0, | ||
205 | .maximum = 1, | ||
206 | .step = 1, | ||
207 | .default_value = 0, | ||
208 | .flags = 0, | ||
209 | }, | ||
210 | }, | ||
211 | .get_ctrl = &mt9v111_get_ctrl, | ||
212 | .set_ctrl = &mt9v111_set_ctrl, | ||
213 | .cropcap = { | ||
214 | .bounds = { | ||
215 | .left = 0, | ||
216 | .top = 0, | ||
217 | .width = 640, | ||
218 | .height = 480, | ||
219 | }, | ||
220 | .defrect = { | ||
221 | .left = 0, | ||
222 | .top = 0, | ||
223 | .width = 640, | ||
224 | .height = 480, | ||
225 | }, | ||
226 | }, | ||
227 | .set_crop = &mt9v111_set_crop, | ||
228 | .pix_format = { | ||
229 | .width = 640, | ||
230 | .height = 480, | ||
231 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
232 | .priv = 8, | ||
233 | }, | ||
234 | .set_pix_format = &mt9v111_set_pix_format | ||
235 | }; | ||
236 | |||
237 | |||
238 | int sn9c102_probe_mt9v111(struct sn9c102_device *cam) | ||
239 | { | ||
240 | u8 data[2]; | ||
241 | int err = 0; | ||
242 | |||
243 | err += sn9c102_write_const_regs(cam, {0x01, 0xf1}, {0x00, 0xf1}, | ||
244 | {0x29, 0x01}, {0x42, 0x17}, | ||
245 | {0x62, 0x17}, {0x08, 0x01}); | ||
246 | err += sn9c102_i2c_try_raw_write(cam, &mt9v111, 4, | ||
247 | mt9v111.i2c_slave_id, 0x01, 0x00, | ||
248 | 0x04, 0, 0); | ||
249 | if (err || sn9c102_i2c_try_raw_read(cam, &mt9v111, | ||
250 | mt9v111.i2c_slave_id, 0x36, 2, | ||
251 | data) < 0) | ||
252 | return -EIO; | ||
253 | |||
254 | if (data[0] != 0x82 || data[1] != 0x3a) | ||
255 | return -ENODEV; | ||
256 | |||
257 | sn9c102_attach_sensor(cam, &mt9v111); | ||
258 | |||
259 | return 0; | ||
260 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_ov7630.c b/drivers/media/usb/sn9c102/sn9c102_ov7630.c new file mode 100644 index 000000000000..803712c29f02 --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_ov7630.c | |||
@@ -0,0 +1,626 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for OV7630 image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2006-2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int ov7630_init(struct sn9c102_device* cam) | ||
27 | { | ||
28 | int err = 0; | ||
29 | |||
30 | switch (sn9c102_get_bridge(cam)) { | ||
31 | case BRIDGE_SN9C101: | ||
32 | case BRIDGE_SN9C102: | ||
33 | err = sn9c102_write_const_regs(cam, {0x00, 0x14}, {0x60, 0x17}, | ||
34 | {0x0f, 0x18}, {0x50, 0x19}); | ||
35 | |||
36 | err += sn9c102_i2c_write(cam, 0x12, 0x8d); | ||
37 | err += sn9c102_i2c_write(cam, 0x12, 0x0d); | ||
38 | err += sn9c102_i2c_write(cam, 0x11, 0x00); | ||
39 | err += sn9c102_i2c_write(cam, 0x15, 0x35); | ||
40 | err += sn9c102_i2c_write(cam, 0x16, 0x03); | ||
41 | err += sn9c102_i2c_write(cam, 0x17, 0x1c); | ||
42 | err += sn9c102_i2c_write(cam, 0x18, 0xbd); | ||
43 | err += sn9c102_i2c_write(cam, 0x19, 0x06); | ||
44 | err += sn9c102_i2c_write(cam, 0x1a, 0xf6); | ||
45 | err += sn9c102_i2c_write(cam, 0x1b, 0x04); | ||
46 | err += sn9c102_i2c_write(cam, 0x20, 0x44); | ||
47 | err += sn9c102_i2c_write(cam, 0x23, 0xee); | ||
48 | err += sn9c102_i2c_write(cam, 0x26, 0xa0); | ||
49 | err += sn9c102_i2c_write(cam, 0x27, 0x9a); | ||
50 | err += sn9c102_i2c_write(cam, 0x28, 0x20); | ||
51 | err += sn9c102_i2c_write(cam, 0x29, 0x30); | ||
52 | err += sn9c102_i2c_write(cam, 0x2f, 0x3d); | ||
53 | err += sn9c102_i2c_write(cam, 0x30, 0x24); | ||
54 | err += sn9c102_i2c_write(cam, 0x32, 0x86); | ||
55 | err += sn9c102_i2c_write(cam, 0x60, 0xa9); | ||
56 | err += sn9c102_i2c_write(cam, 0x61, 0x42); | ||
57 | err += sn9c102_i2c_write(cam, 0x65, 0x00); | ||
58 | err += sn9c102_i2c_write(cam, 0x69, 0x38); | ||
59 | err += sn9c102_i2c_write(cam, 0x6f, 0x88); | ||
60 | err += sn9c102_i2c_write(cam, 0x70, 0x0b); | ||
61 | err += sn9c102_i2c_write(cam, 0x71, 0x00); | ||
62 | err += sn9c102_i2c_write(cam, 0x74, 0x21); | ||
63 | err += sn9c102_i2c_write(cam, 0x7d, 0xf7); | ||
64 | break; | ||
65 | case BRIDGE_SN9C103: | ||
66 | err = sn9c102_write_const_regs(cam, {0x00, 0x02}, {0x00, 0x03}, | ||
67 | {0x1a, 0x04}, {0x20, 0x05}, | ||
68 | {0x20, 0x06}, {0x20, 0x07}, | ||
69 | {0x03, 0x10}, {0x0a, 0x14}, | ||
70 | {0x60, 0x17}, {0x0f, 0x18}, | ||
71 | {0x50, 0x19}, {0x1d, 0x1a}, | ||
72 | {0x10, 0x1b}, {0x02, 0x1c}, | ||
73 | {0x03, 0x1d}, {0x0f, 0x1e}, | ||
74 | {0x0c, 0x1f}, {0x00, 0x20}, | ||
75 | {0x10, 0x21}, {0x20, 0x22}, | ||
76 | {0x30, 0x23}, {0x40, 0x24}, | ||
77 | {0x50, 0x25}, {0x60, 0x26}, | ||
78 | {0x70, 0x27}, {0x80, 0x28}, | ||
79 | {0x90, 0x29}, {0xa0, 0x2a}, | ||
80 | {0xb0, 0x2b}, {0xc0, 0x2c}, | ||
81 | {0xd0, 0x2d}, {0xe0, 0x2e}, | ||
82 | {0xf0, 0x2f}, {0xff, 0x30}); | ||
83 | |||
84 | err += sn9c102_i2c_write(cam, 0x12, 0x8d); | ||
85 | err += sn9c102_i2c_write(cam, 0x12, 0x0d); | ||
86 | err += sn9c102_i2c_write(cam, 0x15, 0x34); | ||
87 | err += sn9c102_i2c_write(cam, 0x11, 0x01); | ||
88 | err += sn9c102_i2c_write(cam, 0x1b, 0x04); | ||
89 | err += sn9c102_i2c_write(cam, 0x20, 0x44); | ||
90 | err += sn9c102_i2c_write(cam, 0x23, 0xee); | ||
91 | err += sn9c102_i2c_write(cam, 0x26, 0xa0); | ||
92 | err += sn9c102_i2c_write(cam, 0x27, 0x9a); | ||
93 | err += sn9c102_i2c_write(cam, 0x28, 0x20); | ||
94 | err += sn9c102_i2c_write(cam, 0x29, 0x30); | ||
95 | err += sn9c102_i2c_write(cam, 0x2f, 0x3d); | ||
96 | err += sn9c102_i2c_write(cam, 0x30, 0x24); | ||
97 | err += sn9c102_i2c_write(cam, 0x32, 0x86); | ||
98 | err += sn9c102_i2c_write(cam, 0x60, 0xa9); | ||
99 | err += sn9c102_i2c_write(cam, 0x61, 0x42); | ||
100 | err += sn9c102_i2c_write(cam, 0x65, 0x00); | ||
101 | err += sn9c102_i2c_write(cam, 0x69, 0x38); | ||
102 | err += sn9c102_i2c_write(cam, 0x6f, 0x88); | ||
103 | err += sn9c102_i2c_write(cam, 0x70, 0x0b); | ||
104 | err += sn9c102_i2c_write(cam, 0x71, 0x00); | ||
105 | err += sn9c102_i2c_write(cam, 0x74, 0x21); | ||
106 | err += sn9c102_i2c_write(cam, 0x7d, 0xf7); | ||
107 | break; | ||
108 | case BRIDGE_SN9C105: | ||
109 | case BRIDGE_SN9C120: | ||
110 | err = sn9c102_write_const_regs(cam, {0x40, 0x02}, {0x00, 0x03}, | ||
111 | {0x1a, 0x04}, {0x03, 0x10}, | ||
112 | {0x0a, 0x14}, {0xe2, 0x17}, | ||
113 | {0x0b, 0x18}, {0x00, 0x19}, | ||
114 | {0x1d, 0x1a}, {0x10, 0x1b}, | ||
115 | {0x02, 0x1c}, {0x03, 0x1d}, | ||
116 | {0x0f, 0x1e}, {0x0c, 0x1f}, | ||
117 | {0x00, 0x20}, {0x24, 0x21}, | ||
118 | {0x3b, 0x22}, {0x47, 0x23}, | ||
119 | {0x60, 0x24}, {0x71, 0x25}, | ||
120 | {0x80, 0x26}, {0x8f, 0x27}, | ||
121 | {0x9d, 0x28}, {0xaa, 0x29}, | ||
122 | {0xb8, 0x2a}, {0xc4, 0x2b}, | ||
123 | {0xd1, 0x2c}, {0xdd, 0x2d}, | ||
124 | {0xe8, 0x2e}, {0xf4, 0x2f}, | ||
125 | {0xff, 0x30}, {0x00, 0x3f}, | ||
126 | {0xc7, 0x40}, {0x01, 0x41}, | ||
127 | {0x44, 0x42}, {0x00, 0x43}, | ||
128 | {0x44, 0x44}, {0x00, 0x45}, | ||
129 | {0x44, 0x46}, {0x00, 0x47}, | ||
130 | {0xc7, 0x48}, {0x01, 0x49}, | ||
131 | {0xc7, 0x4a}, {0x01, 0x4b}, | ||
132 | {0xc7, 0x4c}, {0x01, 0x4d}, | ||
133 | {0x44, 0x4e}, {0x00, 0x4f}, | ||
134 | {0x44, 0x50}, {0x00, 0x51}, | ||
135 | {0x44, 0x52}, {0x00, 0x53}, | ||
136 | {0xc7, 0x54}, {0x01, 0x55}, | ||
137 | {0xc7, 0x56}, {0x01, 0x57}, | ||
138 | {0xc7, 0x58}, {0x01, 0x59}, | ||
139 | {0x44, 0x5a}, {0x00, 0x5b}, | ||
140 | {0x44, 0x5c}, {0x00, 0x5d}, | ||
141 | {0x44, 0x5e}, {0x00, 0x5f}, | ||
142 | {0xc7, 0x60}, {0x01, 0x61}, | ||
143 | {0xc7, 0x62}, {0x01, 0x63}, | ||
144 | {0xc7, 0x64}, {0x01, 0x65}, | ||
145 | {0x44, 0x66}, {0x00, 0x67}, | ||
146 | {0x44, 0x68}, {0x00, 0x69}, | ||
147 | {0x44, 0x6a}, {0x00, 0x6b}, | ||
148 | {0xc7, 0x6c}, {0x01, 0x6d}, | ||
149 | {0xc7, 0x6e}, {0x01, 0x6f}, | ||
150 | {0xc7, 0x70}, {0x01, 0x71}, | ||
151 | {0x44, 0x72}, {0x00, 0x73}, | ||
152 | {0x44, 0x74}, {0x00, 0x75}, | ||
153 | {0x44, 0x76}, {0x00, 0x77}, | ||
154 | {0xc7, 0x78}, {0x01, 0x79}, | ||
155 | {0xc7, 0x7a}, {0x01, 0x7b}, | ||
156 | {0xc7, 0x7c}, {0x01, 0x7d}, | ||
157 | {0x44, 0x7e}, {0x00, 0x7f}, | ||
158 | {0x17, 0x84}, {0x00, 0x85}, | ||
159 | {0x2e, 0x86}, {0x00, 0x87}, | ||
160 | {0x09, 0x88}, {0x00, 0x89}, | ||
161 | {0xe8, 0x8a}, {0x0f, 0x8b}, | ||
162 | {0xda, 0x8c}, {0x0f, 0x8d}, | ||
163 | {0x40, 0x8e}, {0x00, 0x8f}, | ||
164 | {0x37, 0x90}, {0x00, 0x91}, | ||
165 | {0xcf, 0x92}, {0x0f, 0x93}, | ||
166 | {0xfa, 0x94}, {0x0f, 0x95}, | ||
167 | {0x00, 0x96}, {0x00, 0x97}, | ||
168 | {0x00, 0x98}, {0x66, 0x99}, | ||
169 | {0x00, 0x9a}, {0x40, 0x9b}, | ||
170 | {0x20, 0x9c}, {0x00, 0x9d}, | ||
171 | {0x00, 0x9e}, {0x00, 0x9f}, | ||
172 | {0x2d, 0xc0}, {0x2d, 0xc1}, | ||
173 | {0x3a, 0xc2}, {0x00, 0xc3}, | ||
174 | {0x04, 0xc4}, {0x3f, 0xc5}, | ||
175 | {0x00, 0xc6}, {0x00, 0xc7}, | ||
176 | {0x50, 0xc8}, {0x3c, 0xc9}, | ||
177 | {0x28, 0xca}, {0xd8, 0xcb}, | ||
178 | {0x14, 0xcc}, {0xec, 0xcd}, | ||
179 | {0x32, 0xce}, {0xdd, 0xcf}, | ||
180 | {0x32, 0xd0}, {0xdd, 0xd1}, | ||
181 | {0x6a, 0xd2}, {0x50, 0xd3}, | ||
182 | {0x60, 0xd4}, {0x00, 0xd5}, | ||
183 | {0x00, 0xd6}); | ||
184 | |||
185 | err += sn9c102_i2c_write(cam, 0x12, 0x80); | ||
186 | err += sn9c102_i2c_write(cam, 0x12, 0x48); | ||
187 | err += sn9c102_i2c_write(cam, 0x01, 0x80); | ||
188 | err += sn9c102_i2c_write(cam, 0x02, 0x80); | ||
189 | err += sn9c102_i2c_write(cam, 0x03, 0x80); | ||
190 | err += sn9c102_i2c_write(cam, 0x04, 0x10); | ||
191 | err += sn9c102_i2c_write(cam, 0x05, 0x20); | ||
192 | err += sn9c102_i2c_write(cam, 0x06, 0x80); | ||
193 | err += sn9c102_i2c_write(cam, 0x11, 0x00); | ||
194 | err += sn9c102_i2c_write(cam, 0x0c, 0x20); | ||
195 | err += sn9c102_i2c_write(cam, 0x0d, 0x20); | ||
196 | err += sn9c102_i2c_write(cam, 0x15, 0x80); | ||
197 | err += sn9c102_i2c_write(cam, 0x16, 0x03); | ||
198 | err += sn9c102_i2c_write(cam, 0x17, 0x1b); | ||
199 | err += sn9c102_i2c_write(cam, 0x18, 0xbd); | ||
200 | err += sn9c102_i2c_write(cam, 0x19, 0x05); | ||
201 | err += sn9c102_i2c_write(cam, 0x1a, 0xf6); | ||
202 | err += sn9c102_i2c_write(cam, 0x1b, 0x04); | ||
203 | err += sn9c102_i2c_write(cam, 0x21, 0x1b); | ||
204 | err += sn9c102_i2c_write(cam, 0x22, 0x00); | ||
205 | err += sn9c102_i2c_write(cam, 0x23, 0xde); | ||
206 | err += sn9c102_i2c_write(cam, 0x24, 0x10); | ||
207 | err += sn9c102_i2c_write(cam, 0x25, 0x8a); | ||
208 | err += sn9c102_i2c_write(cam, 0x26, 0xa0); | ||
209 | err += sn9c102_i2c_write(cam, 0x27, 0xca); | ||
210 | err += sn9c102_i2c_write(cam, 0x28, 0xa2); | ||
211 | err += sn9c102_i2c_write(cam, 0x29, 0x74); | ||
212 | err += sn9c102_i2c_write(cam, 0x2a, 0x88); | ||
213 | err += sn9c102_i2c_write(cam, 0x2b, 0x34); | ||
214 | err += sn9c102_i2c_write(cam, 0x2c, 0x88); | ||
215 | err += sn9c102_i2c_write(cam, 0x2e, 0x00); | ||
216 | err += sn9c102_i2c_write(cam, 0x2f, 0x00); | ||
217 | err += sn9c102_i2c_write(cam, 0x30, 0x00); | ||
218 | err += sn9c102_i2c_write(cam, 0x32, 0xc2); | ||
219 | err += sn9c102_i2c_write(cam, 0x33, 0x08); | ||
220 | err += sn9c102_i2c_write(cam, 0x4c, 0x40); | ||
221 | err += sn9c102_i2c_write(cam, 0x4d, 0xf3); | ||
222 | err += sn9c102_i2c_write(cam, 0x60, 0x05); | ||
223 | err += sn9c102_i2c_write(cam, 0x61, 0x40); | ||
224 | err += sn9c102_i2c_write(cam, 0x62, 0x12); | ||
225 | err += sn9c102_i2c_write(cam, 0x63, 0x57); | ||
226 | err += sn9c102_i2c_write(cam, 0x64, 0x73); | ||
227 | err += sn9c102_i2c_write(cam, 0x65, 0x00); | ||
228 | err += sn9c102_i2c_write(cam, 0x66, 0x55); | ||
229 | err += sn9c102_i2c_write(cam, 0x67, 0x01); | ||
230 | err += sn9c102_i2c_write(cam, 0x68, 0xac); | ||
231 | err += sn9c102_i2c_write(cam, 0x69, 0x38); | ||
232 | err += sn9c102_i2c_write(cam, 0x6f, 0x1f); | ||
233 | err += sn9c102_i2c_write(cam, 0x70, 0x01); | ||
234 | err += sn9c102_i2c_write(cam, 0x71, 0x00); | ||
235 | err += sn9c102_i2c_write(cam, 0x72, 0x10); | ||
236 | err += sn9c102_i2c_write(cam, 0x73, 0x50); | ||
237 | err += sn9c102_i2c_write(cam, 0x74, 0x20); | ||
238 | err += sn9c102_i2c_write(cam, 0x76, 0x01); | ||
239 | err += sn9c102_i2c_write(cam, 0x77, 0xf3); | ||
240 | err += sn9c102_i2c_write(cam, 0x78, 0x90); | ||
241 | err += sn9c102_i2c_write(cam, 0x79, 0x98); | ||
242 | err += sn9c102_i2c_write(cam, 0x7a, 0x98); | ||
243 | err += sn9c102_i2c_write(cam, 0x7b, 0x00); | ||
244 | err += sn9c102_i2c_write(cam, 0x7c, 0x38); | ||
245 | err += sn9c102_i2c_write(cam, 0x7d, 0xff); | ||
246 | break; | ||
247 | default: | ||
248 | break; | ||
249 | } | ||
250 | |||
251 | return err; | ||
252 | } | ||
253 | |||
254 | |||
255 | static int ov7630_get_ctrl(struct sn9c102_device* cam, | ||
256 | struct v4l2_control* ctrl) | ||
257 | { | ||
258 | enum sn9c102_bridge bridge = sn9c102_get_bridge(cam); | ||
259 | int err = 0; | ||
260 | |||
261 | switch (ctrl->id) { | ||
262 | case V4L2_CID_EXPOSURE: | ||
263 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x10)) < 0) | ||
264 | return -EIO; | ||
265 | break; | ||
266 | case V4L2_CID_RED_BALANCE: | ||
267 | if (bridge == BRIDGE_SN9C105 || bridge == BRIDGE_SN9C120) | ||
268 | ctrl->value = sn9c102_pread_reg(cam, 0x05); | ||
269 | else | ||
270 | ctrl->value = sn9c102_pread_reg(cam, 0x07); | ||
271 | break; | ||
272 | case V4L2_CID_BLUE_BALANCE: | ||
273 | ctrl->value = sn9c102_pread_reg(cam, 0x06); | ||
274 | break; | ||
275 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
276 | if (bridge == BRIDGE_SN9C105 || bridge == BRIDGE_SN9C120) | ||
277 | ctrl->value = sn9c102_pread_reg(cam, 0x07); | ||
278 | else | ||
279 | ctrl->value = sn9c102_pread_reg(cam, 0x05); | ||
280 | break; | ||
281 | break; | ||
282 | case V4L2_CID_GAIN: | ||
283 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x00)) < 0) | ||
284 | return -EIO; | ||
285 | ctrl->value &= 0x3f; | ||
286 | break; | ||
287 | case V4L2_CID_DO_WHITE_BALANCE: | ||
288 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0) | ||
289 | return -EIO; | ||
290 | ctrl->value &= 0x3f; | ||
291 | break; | ||
292 | case V4L2_CID_WHITENESS: | ||
293 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x0d)) < 0) | ||
294 | return -EIO; | ||
295 | ctrl->value &= 0x3f; | ||
296 | break; | ||
297 | case V4L2_CID_AUTOGAIN: | ||
298 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x13)) < 0) | ||
299 | return -EIO; | ||
300 | ctrl->value &= 0x01; | ||
301 | break; | ||
302 | case V4L2_CID_VFLIP: | ||
303 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x75)) < 0) | ||
304 | return -EIO; | ||
305 | ctrl->value = (ctrl->value & 0x80) ? 1 : 0; | ||
306 | break; | ||
307 | case SN9C102_V4L2_CID_GAMMA: | ||
308 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x14)) < 0) | ||
309 | return -EIO; | ||
310 | ctrl->value = (ctrl->value & 0x02) ? 1 : 0; | ||
311 | break; | ||
312 | case SN9C102_V4L2_CID_BAND_FILTER: | ||
313 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x2d)) < 0) | ||
314 | return -EIO; | ||
315 | ctrl->value = (ctrl->value & 0x02) ? 1 : 0; | ||
316 | break; | ||
317 | default: | ||
318 | return -EINVAL; | ||
319 | } | ||
320 | |||
321 | return err ? -EIO : 0; | ||
322 | } | ||
323 | |||
324 | |||
325 | static int ov7630_set_ctrl(struct sn9c102_device* cam, | ||
326 | const struct v4l2_control* ctrl) | ||
327 | { | ||
328 | enum sn9c102_bridge bridge = sn9c102_get_bridge(cam); | ||
329 | int err = 0; | ||
330 | |||
331 | switch (ctrl->id) { | ||
332 | case V4L2_CID_EXPOSURE: | ||
333 | err += sn9c102_i2c_write(cam, 0x10, ctrl->value); | ||
334 | break; | ||
335 | case V4L2_CID_RED_BALANCE: | ||
336 | if (bridge == BRIDGE_SN9C105 || bridge == BRIDGE_SN9C120) | ||
337 | err += sn9c102_write_reg(cam, ctrl->value, 0x05); | ||
338 | else | ||
339 | err += sn9c102_write_reg(cam, ctrl->value, 0x07); | ||
340 | break; | ||
341 | case V4L2_CID_BLUE_BALANCE: | ||
342 | err += sn9c102_write_reg(cam, ctrl->value, 0x06); | ||
343 | break; | ||
344 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
345 | if (bridge == BRIDGE_SN9C105 || bridge == BRIDGE_SN9C120) | ||
346 | err += sn9c102_write_reg(cam, ctrl->value, 0x07); | ||
347 | else | ||
348 | err += sn9c102_write_reg(cam, ctrl->value, 0x05); | ||
349 | break; | ||
350 | case V4L2_CID_GAIN: | ||
351 | err += sn9c102_i2c_write(cam, 0x00, ctrl->value); | ||
352 | break; | ||
353 | case V4L2_CID_DO_WHITE_BALANCE: | ||
354 | err += sn9c102_i2c_write(cam, 0x0c, ctrl->value); | ||
355 | break; | ||
356 | case V4L2_CID_WHITENESS: | ||
357 | err += sn9c102_i2c_write(cam, 0x0d, ctrl->value); | ||
358 | break; | ||
359 | case V4L2_CID_AUTOGAIN: | ||
360 | err += sn9c102_i2c_write(cam, 0x13, ctrl->value | | ||
361 | (ctrl->value << 1)); | ||
362 | break; | ||
363 | case V4L2_CID_VFLIP: | ||
364 | err += sn9c102_i2c_write(cam, 0x75, 0x0e | (ctrl->value << 7)); | ||
365 | break; | ||
366 | case SN9C102_V4L2_CID_GAMMA: | ||
367 | err += sn9c102_i2c_write(cam, 0x14, ctrl->value << 2); | ||
368 | break; | ||
369 | case SN9C102_V4L2_CID_BAND_FILTER: | ||
370 | err += sn9c102_i2c_write(cam, 0x2d, ctrl->value << 2); | ||
371 | break; | ||
372 | default: | ||
373 | return -EINVAL; | ||
374 | } | ||
375 | |||
376 | return err ? -EIO : 0; | ||
377 | } | ||
378 | |||
379 | |||
380 | static int ov7630_set_crop(struct sn9c102_device* cam, | ||
381 | const struct v4l2_rect* rect) | ||
382 | { | ||
383 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
384 | int err = 0; | ||
385 | u8 h_start = 0, v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1; | ||
386 | |||
387 | switch (sn9c102_get_bridge(cam)) { | ||
388 | case BRIDGE_SN9C101: | ||
389 | case BRIDGE_SN9C102: | ||
390 | case BRIDGE_SN9C103: | ||
391 | h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1; | ||
392 | break; | ||
393 | case BRIDGE_SN9C105: | ||
394 | case BRIDGE_SN9C120: | ||
395 | h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4; | ||
396 | break; | ||
397 | default: | ||
398 | break; | ||
399 | } | ||
400 | |||
401 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
402 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
403 | |||
404 | return err; | ||
405 | } | ||
406 | |||
407 | |||
408 | static int ov7630_set_pix_format(struct sn9c102_device* cam, | ||
409 | const struct v4l2_pix_format* pix) | ||
410 | { | ||
411 | int err = 0; | ||
412 | |||
413 | switch (sn9c102_get_bridge(cam)) { | ||
414 | case BRIDGE_SN9C101: | ||
415 | case BRIDGE_SN9C102: | ||
416 | case BRIDGE_SN9C103: | ||
417 | if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) | ||
418 | err += sn9c102_write_reg(cam, 0x50, 0x19); | ||
419 | else | ||
420 | err += sn9c102_write_reg(cam, 0x20, 0x19); | ||
421 | break; | ||
422 | case BRIDGE_SN9C105: | ||
423 | case BRIDGE_SN9C120: | ||
424 | if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) { | ||
425 | err += sn9c102_write_reg(cam, 0xe5, 0x17); | ||
426 | err += sn9c102_i2c_write(cam, 0x11, 0x04); | ||
427 | } else { | ||
428 | err += sn9c102_write_reg(cam, 0xe2, 0x17); | ||
429 | err += sn9c102_i2c_write(cam, 0x11, 0x02); | ||
430 | } | ||
431 | break; | ||
432 | default: | ||
433 | break; | ||
434 | } | ||
435 | |||
436 | return err; | ||
437 | } | ||
438 | |||
439 | |||
440 | static const struct sn9c102_sensor ov7630 = { | ||
441 | .name = "OV7630", | ||
442 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
443 | .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102 | BRIDGE_SN9C103 | | ||
444 | BRIDGE_SN9C105 | BRIDGE_SN9C120, | ||
445 | .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE, | ||
446 | .frequency = SN9C102_I2C_100KHZ, | ||
447 | .interface = SN9C102_I2C_2WIRES, | ||
448 | .i2c_slave_id = 0x21, | ||
449 | .init = &ov7630_init, | ||
450 | .qctrl = { | ||
451 | { | ||
452 | .id = V4L2_CID_GAIN, | ||
453 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
454 | .name = "global gain", | ||
455 | .minimum = 0x00, | ||
456 | .maximum = 0x3f, | ||
457 | .step = 0x01, | ||
458 | .default_value = 0x14, | ||
459 | .flags = 0, | ||
460 | }, | ||
461 | { | ||
462 | .id = V4L2_CID_EXPOSURE, | ||
463 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
464 | .name = "exposure", | ||
465 | .minimum = 0x00, | ||
466 | .maximum = 0xff, | ||
467 | .step = 0x01, | ||
468 | .default_value = 0x60, | ||
469 | .flags = 0, | ||
470 | }, | ||
471 | { | ||
472 | .id = V4L2_CID_WHITENESS, | ||
473 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
474 | .name = "white balance background: red", | ||
475 | .minimum = 0x00, | ||
476 | .maximum = 0x3f, | ||
477 | .step = 0x01, | ||
478 | .default_value = 0x20, | ||
479 | .flags = 0, | ||
480 | }, | ||
481 | { | ||
482 | .id = V4L2_CID_DO_WHITE_BALANCE, | ||
483 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
484 | .name = "white balance background: blue", | ||
485 | .minimum = 0x00, | ||
486 | .maximum = 0x3f, | ||
487 | .step = 0x01, | ||
488 | .default_value = 0x20, | ||
489 | .flags = 0, | ||
490 | }, | ||
491 | { | ||
492 | .id = V4L2_CID_RED_BALANCE, | ||
493 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
494 | .name = "red balance", | ||
495 | .minimum = 0x00, | ||
496 | .maximum = 0x7f, | ||
497 | .step = 0x01, | ||
498 | .default_value = 0x20, | ||
499 | .flags = 0, | ||
500 | }, | ||
501 | { | ||
502 | .id = V4L2_CID_BLUE_BALANCE, | ||
503 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
504 | .name = "blue balance", | ||
505 | .minimum = 0x00, | ||
506 | .maximum = 0x7f, | ||
507 | .step = 0x01, | ||
508 | .default_value = 0x20, | ||
509 | .flags = 0, | ||
510 | }, | ||
511 | { | ||
512 | .id = V4L2_CID_AUTOGAIN, | ||
513 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
514 | .name = "auto adjust", | ||
515 | .minimum = 0x00, | ||
516 | .maximum = 0x01, | ||
517 | .step = 0x01, | ||
518 | .default_value = 0x00, | ||
519 | .flags = 0, | ||
520 | }, | ||
521 | { | ||
522 | .id = V4L2_CID_VFLIP, | ||
523 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
524 | .name = "vertical flip", | ||
525 | .minimum = 0x00, | ||
526 | .maximum = 0x01, | ||
527 | .step = 0x01, | ||
528 | .default_value = 0x01, | ||
529 | .flags = 0, | ||
530 | }, | ||
531 | { | ||
532 | .id = SN9C102_V4L2_CID_GREEN_BALANCE, | ||
533 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
534 | .name = "green balance", | ||
535 | .minimum = 0x00, | ||
536 | .maximum = 0x7f, | ||
537 | .step = 0x01, | ||
538 | .default_value = 0x20, | ||
539 | .flags = 0, | ||
540 | }, | ||
541 | { | ||
542 | .id = SN9C102_V4L2_CID_BAND_FILTER, | ||
543 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
544 | .name = "band filter", | ||
545 | .minimum = 0x00, | ||
546 | .maximum = 0x01, | ||
547 | .step = 0x01, | ||
548 | .default_value = 0x00, | ||
549 | .flags = 0, | ||
550 | }, | ||
551 | { | ||
552 | .id = SN9C102_V4L2_CID_GAMMA, | ||
553 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
554 | .name = "rgb gamma", | ||
555 | .minimum = 0x00, | ||
556 | .maximum = 0x01, | ||
557 | .step = 0x01, | ||
558 | .default_value = 0x00, | ||
559 | .flags = 0, | ||
560 | }, | ||
561 | }, | ||
562 | .get_ctrl = &ov7630_get_ctrl, | ||
563 | .set_ctrl = &ov7630_set_ctrl, | ||
564 | .cropcap = { | ||
565 | .bounds = { | ||
566 | .left = 0, | ||
567 | .top = 0, | ||
568 | .width = 640, | ||
569 | .height = 480, | ||
570 | }, | ||
571 | .defrect = { | ||
572 | .left = 0, | ||
573 | .top = 0, | ||
574 | .width = 640, | ||
575 | .height = 480, | ||
576 | }, | ||
577 | }, | ||
578 | .set_crop = &ov7630_set_crop, | ||
579 | .pix_format = { | ||
580 | .width = 640, | ||
581 | .height = 480, | ||
582 | .pixelformat = V4L2_PIX_FMT_SN9C10X, | ||
583 | .priv = 8, | ||
584 | }, | ||
585 | .set_pix_format = &ov7630_set_pix_format | ||
586 | }; | ||
587 | |||
588 | |||
589 | int sn9c102_probe_ov7630(struct sn9c102_device* cam) | ||
590 | { | ||
591 | int pid, ver, err = 0; | ||
592 | |||
593 | switch (sn9c102_get_bridge(cam)) { | ||
594 | case BRIDGE_SN9C101: | ||
595 | case BRIDGE_SN9C102: | ||
596 | err = sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01}, | ||
597 | {0x28, 0x17}); | ||
598 | break; | ||
599 | case BRIDGE_SN9C103: /* do _not_ change anything! */ | ||
600 | err = sn9c102_write_const_regs(cam, {0x09, 0x01}, {0x42, 0x01}, | ||
601 | {0x28, 0x17}, {0x44, 0x02}); | ||
602 | pid = sn9c102_i2c_try_read(cam, &ov7630, 0x0a); | ||
603 | if (err || pid < 0) /* try a different initialization */ | ||
604 | err += sn9c102_write_const_regs(cam, {0x01, 0x01}, | ||
605 | {0x00, 0x01}); | ||
606 | break; | ||
607 | case BRIDGE_SN9C105: | ||
608 | case BRIDGE_SN9C120: | ||
609 | err = sn9c102_write_const_regs(cam, {0x01, 0xf1}, {0x00, 0xf1}, | ||
610 | {0x29, 0x01}, {0x74, 0x02}, | ||
611 | {0x0e, 0x01}, {0x44, 0x01}); | ||
612 | break; | ||
613 | default: | ||
614 | break; | ||
615 | } | ||
616 | |||
617 | pid = sn9c102_i2c_try_read(cam, &ov7630, 0x0a); | ||
618 | ver = sn9c102_i2c_try_read(cam, &ov7630, 0x0b); | ||
619 | if (err || pid < 0 || ver < 0) | ||
620 | return -EIO; | ||
621 | if (pid != 0x76 || ver != 0x31) | ||
622 | return -ENODEV; | ||
623 | sn9c102_attach_sensor(cam, &ov7630); | ||
624 | |||
625 | return 0; | ||
626 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_ov7660.c b/drivers/media/usb/sn9c102/sn9c102_ov7660.c new file mode 100644 index 000000000000..7977795d342b --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_ov7660.c | |||
@@ -0,0 +1,538 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for OV7660 image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int ov7660_init(struct sn9c102_device* cam) | ||
27 | { | ||
28 | int err = 0; | ||
29 | |||
30 | err = sn9c102_write_const_regs(cam, {0x40, 0x02}, {0x00, 0x03}, | ||
31 | {0x1a, 0x04}, {0x03, 0x10}, | ||
32 | {0x08, 0x14}, {0x20, 0x17}, | ||
33 | {0x8b, 0x18}, {0x00, 0x19}, | ||
34 | {0x1d, 0x1a}, {0x10, 0x1b}, | ||
35 | {0x02, 0x1c}, {0x03, 0x1d}, | ||
36 | {0x0f, 0x1e}, {0x0c, 0x1f}, | ||
37 | {0x00, 0x20}, {0x29, 0x21}, | ||
38 | {0x40, 0x22}, {0x54, 0x23}, | ||
39 | {0x66, 0x24}, {0x76, 0x25}, | ||
40 | {0x85, 0x26}, {0x94, 0x27}, | ||
41 | {0xa1, 0x28}, {0xae, 0x29}, | ||
42 | {0xbb, 0x2a}, {0xc7, 0x2b}, | ||
43 | {0xd3, 0x2c}, {0xde, 0x2d}, | ||
44 | {0xea, 0x2e}, {0xf4, 0x2f}, | ||
45 | {0xff, 0x30}, {0x00, 0x3f}, | ||
46 | {0xc7, 0x40}, {0x01, 0x41}, | ||
47 | {0x44, 0x42}, {0x00, 0x43}, | ||
48 | {0x44, 0x44}, {0x00, 0x45}, | ||
49 | {0x44, 0x46}, {0x00, 0x47}, | ||
50 | {0xc7, 0x48}, {0x01, 0x49}, | ||
51 | {0xc7, 0x4a}, {0x01, 0x4b}, | ||
52 | {0xc7, 0x4c}, {0x01, 0x4d}, | ||
53 | {0x44, 0x4e}, {0x00, 0x4f}, | ||
54 | {0x44, 0x50}, {0x00, 0x51}, | ||
55 | {0x44, 0x52}, {0x00, 0x53}, | ||
56 | {0xc7, 0x54}, {0x01, 0x55}, | ||
57 | {0xc7, 0x56}, {0x01, 0x57}, | ||
58 | {0xc7, 0x58}, {0x01, 0x59}, | ||
59 | {0x44, 0x5a}, {0x00, 0x5b}, | ||
60 | {0x44, 0x5c}, {0x00, 0x5d}, | ||
61 | {0x44, 0x5e}, {0x00, 0x5f}, | ||
62 | {0xc7, 0x60}, {0x01, 0x61}, | ||
63 | {0xc7, 0x62}, {0x01, 0x63}, | ||
64 | {0xc7, 0x64}, {0x01, 0x65}, | ||
65 | {0x44, 0x66}, {0x00, 0x67}, | ||
66 | {0x44, 0x68}, {0x00, 0x69}, | ||
67 | {0x44, 0x6a}, {0x00, 0x6b}, | ||
68 | {0xc7, 0x6c}, {0x01, 0x6d}, | ||
69 | {0xc7, 0x6e}, {0x01, 0x6f}, | ||
70 | {0xc7, 0x70}, {0x01, 0x71}, | ||
71 | {0x44, 0x72}, {0x00, 0x73}, | ||
72 | {0x44, 0x74}, {0x00, 0x75}, | ||
73 | {0x44, 0x76}, {0x00, 0x77}, | ||
74 | {0xc7, 0x78}, {0x01, 0x79}, | ||
75 | {0xc7, 0x7a}, {0x01, 0x7b}, | ||
76 | {0xc7, 0x7c}, {0x01, 0x7d}, | ||
77 | {0x44, 0x7e}, {0x00, 0x7f}, | ||
78 | {0x14, 0x84}, {0x00, 0x85}, | ||
79 | {0x27, 0x86}, {0x00, 0x87}, | ||
80 | {0x07, 0x88}, {0x00, 0x89}, | ||
81 | {0xec, 0x8a}, {0x0f, 0x8b}, | ||
82 | {0xd8, 0x8c}, {0x0f, 0x8d}, | ||
83 | {0x3d, 0x8e}, {0x00, 0x8f}, | ||
84 | {0x3d, 0x90}, {0x00, 0x91}, | ||
85 | {0xcd, 0x92}, {0x0f, 0x93}, | ||
86 | {0xf7, 0x94}, {0x0f, 0x95}, | ||
87 | {0x0c, 0x96}, {0x00, 0x97}, | ||
88 | {0x00, 0x98}, {0x66, 0x99}, | ||
89 | {0x05, 0x9a}, {0x00, 0x9b}, | ||
90 | {0x04, 0x9c}, {0x00, 0x9d}, | ||
91 | {0x08, 0x9e}, {0x00, 0x9f}, | ||
92 | {0x2d, 0xc0}, {0x2d, 0xc1}, | ||
93 | {0x3a, 0xc2}, {0x05, 0xc3}, | ||
94 | {0x04, 0xc4}, {0x3f, 0xc5}, | ||
95 | {0x00, 0xc6}, {0x00, 0xc7}, | ||
96 | {0x50, 0xc8}, {0x3C, 0xc9}, | ||
97 | {0x28, 0xca}, {0xd8, 0xcb}, | ||
98 | {0x14, 0xcc}, {0xec, 0xcd}, | ||
99 | {0x32, 0xce}, {0xdd, 0xcf}, | ||
100 | {0x32, 0xd0}, {0xdd, 0xd1}, | ||
101 | {0x6a, 0xd2}, {0x50, 0xd3}, | ||
102 | {0x00, 0xd4}, {0x00, 0xd5}, | ||
103 | {0x00, 0xd6}); | ||
104 | |||
105 | err += sn9c102_i2c_write(cam, 0x12, 0x80); | ||
106 | err += sn9c102_i2c_write(cam, 0x11, 0x09); | ||
107 | err += sn9c102_i2c_write(cam, 0x00, 0x0A); | ||
108 | err += sn9c102_i2c_write(cam, 0x01, 0x80); | ||
109 | err += sn9c102_i2c_write(cam, 0x02, 0x80); | ||
110 | err += sn9c102_i2c_write(cam, 0x03, 0x00); | ||
111 | err += sn9c102_i2c_write(cam, 0x04, 0x00); | ||
112 | err += sn9c102_i2c_write(cam, 0x05, 0x08); | ||
113 | err += sn9c102_i2c_write(cam, 0x06, 0x0B); | ||
114 | err += sn9c102_i2c_write(cam, 0x07, 0x00); | ||
115 | err += sn9c102_i2c_write(cam, 0x08, 0x1C); | ||
116 | err += sn9c102_i2c_write(cam, 0x09, 0x01); | ||
117 | err += sn9c102_i2c_write(cam, 0x0A, 0x76); | ||
118 | err += sn9c102_i2c_write(cam, 0x0B, 0x60); | ||
119 | err += sn9c102_i2c_write(cam, 0x0C, 0x00); | ||
120 | err += sn9c102_i2c_write(cam, 0x0D, 0x08); | ||
121 | err += sn9c102_i2c_write(cam, 0x0E, 0x04); | ||
122 | err += sn9c102_i2c_write(cam, 0x0F, 0x6F); | ||
123 | err += sn9c102_i2c_write(cam, 0x10, 0x20); | ||
124 | err += sn9c102_i2c_write(cam, 0x11, 0x03); | ||
125 | err += sn9c102_i2c_write(cam, 0x12, 0x05); | ||
126 | err += sn9c102_i2c_write(cam, 0x13, 0xC7); | ||
127 | err += sn9c102_i2c_write(cam, 0x14, 0x2C); | ||
128 | err += sn9c102_i2c_write(cam, 0x15, 0x00); | ||
129 | err += sn9c102_i2c_write(cam, 0x16, 0x02); | ||
130 | err += sn9c102_i2c_write(cam, 0x17, 0x10); | ||
131 | err += sn9c102_i2c_write(cam, 0x18, 0x60); | ||
132 | err += sn9c102_i2c_write(cam, 0x19, 0x02); | ||
133 | err += sn9c102_i2c_write(cam, 0x1A, 0x7B); | ||
134 | err += sn9c102_i2c_write(cam, 0x1B, 0x02); | ||
135 | err += sn9c102_i2c_write(cam, 0x1C, 0x7F); | ||
136 | err += sn9c102_i2c_write(cam, 0x1D, 0xA2); | ||
137 | err += sn9c102_i2c_write(cam, 0x1E, 0x01); | ||
138 | err += sn9c102_i2c_write(cam, 0x1F, 0x0E); | ||
139 | err += sn9c102_i2c_write(cam, 0x20, 0x05); | ||
140 | err += sn9c102_i2c_write(cam, 0x21, 0x05); | ||
141 | err += sn9c102_i2c_write(cam, 0x22, 0x05); | ||
142 | err += sn9c102_i2c_write(cam, 0x23, 0x05); | ||
143 | err += sn9c102_i2c_write(cam, 0x24, 0x68); | ||
144 | err += sn9c102_i2c_write(cam, 0x25, 0x58); | ||
145 | err += sn9c102_i2c_write(cam, 0x26, 0xD4); | ||
146 | err += sn9c102_i2c_write(cam, 0x27, 0x80); | ||
147 | err += sn9c102_i2c_write(cam, 0x28, 0x80); | ||
148 | err += sn9c102_i2c_write(cam, 0x29, 0x30); | ||
149 | err += sn9c102_i2c_write(cam, 0x2A, 0x00); | ||
150 | err += sn9c102_i2c_write(cam, 0x2B, 0x00); | ||
151 | err += sn9c102_i2c_write(cam, 0x2C, 0x80); | ||
152 | err += sn9c102_i2c_write(cam, 0x2D, 0x00); | ||
153 | err += sn9c102_i2c_write(cam, 0x2E, 0x00); | ||
154 | err += sn9c102_i2c_write(cam, 0x2F, 0x0E); | ||
155 | err += sn9c102_i2c_write(cam, 0x30, 0x08); | ||
156 | err += sn9c102_i2c_write(cam, 0x31, 0x30); | ||
157 | err += sn9c102_i2c_write(cam, 0x32, 0xB4); | ||
158 | err += sn9c102_i2c_write(cam, 0x33, 0x00); | ||
159 | err += sn9c102_i2c_write(cam, 0x34, 0x07); | ||
160 | err += sn9c102_i2c_write(cam, 0x35, 0x84); | ||
161 | err += sn9c102_i2c_write(cam, 0x36, 0x00); | ||
162 | err += sn9c102_i2c_write(cam, 0x37, 0x0C); | ||
163 | err += sn9c102_i2c_write(cam, 0x38, 0x02); | ||
164 | err += sn9c102_i2c_write(cam, 0x39, 0x43); | ||
165 | err += sn9c102_i2c_write(cam, 0x3A, 0x00); | ||
166 | err += sn9c102_i2c_write(cam, 0x3B, 0x0A); | ||
167 | err += sn9c102_i2c_write(cam, 0x3C, 0x6C); | ||
168 | err += sn9c102_i2c_write(cam, 0x3D, 0x99); | ||
169 | err += sn9c102_i2c_write(cam, 0x3E, 0x0E); | ||
170 | err += sn9c102_i2c_write(cam, 0x3F, 0x41); | ||
171 | err += sn9c102_i2c_write(cam, 0x40, 0xC1); | ||
172 | err += sn9c102_i2c_write(cam, 0x41, 0x22); | ||
173 | err += sn9c102_i2c_write(cam, 0x42, 0x08); | ||
174 | err += sn9c102_i2c_write(cam, 0x43, 0xF0); | ||
175 | err += sn9c102_i2c_write(cam, 0x44, 0x10); | ||
176 | err += sn9c102_i2c_write(cam, 0x45, 0x78); | ||
177 | err += sn9c102_i2c_write(cam, 0x46, 0xA8); | ||
178 | err += sn9c102_i2c_write(cam, 0x47, 0x60); | ||
179 | err += sn9c102_i2c_write(cam, 0x48, 0x80); | ||
180 | err += sn9c102_i2c_write(cam, 0x49, 0x00); | ||
181 | err += sn9c102_i2c_write(cam, 0x4A, 0x00); | ||
182 | err += sn9c102_i2c_write(cam, 0x4B, 0x00); | ||
183 | err += sn9c102_i2c_write(cam, 0x4C, 0x00); | ||
184 | err += sn9c102_i2c_write(cam, 0x4D, 0x00); | ||
185 | err += sn9c102_i2c_write(cam, 0x4E, 0x00); | ||
186 | err += sn9c102_i2c_write(cam, 0x4F, 0x46); | ||
187 | err += sn9c102_i2c_write(cam, 0x50, 0x36); | ||
188 | err += sn9c102_i2c_write(cam, 0x51, 0x0F); | ||
189 | err += sn9c102_i2c_write(cam, 0x52, 0x17); | ||
190 | err += sn9c102_i2c_write(cam, 0x53, 0x7F); | ||
191 | err += sn9c102_i2c_write(cam, 0x54, 0x96); | ||
192 | err += sn9c102_i2c_write(cam, 0x55, 0x40); | ||
193 | err += sn9c102_i2c_write(cam, 0x56, 0x40); | ||
194 | err += sn9c102_i2c_write(cam, 0x57, 0x40); | ||
195 | err += sn9c102_i2c_write(cam, 0x58, 0x0F); | ||
196 | err += sn9c102_i2c_write(cam, 0x59, 0xBA); | ||
197 | err += sn9c102_i2c_write(cam, 0x5A, 0x9A); | ||
198 | err += sn9c102_i2c_write(cam, 0x5B, 0x22); | ||
199 | err += sn9c102_i2c_write(cam, 0x5C, 0xB9); | ||
200 | err += sn9c102_i2c_write(cam, 0x5D, 0x9B); | ||
201 | err += sn9c102_i2c_write(cam, 0x5E, 0x10); | ||
202 | err += sn9c102_i2c_write(cam, 0x5F, 0xF0); | ||
203 | err += sn9c102_i2c_write(cam, 0x60, 0x05); | ||
204 | err += sn9c102_i2c_write(cam, 0x61, 0x60); | ||
205 | err += sn9c102_i2c_write(cam, 0x62, 0x00); | ||
206 | err += sn9c102_i2c_write(cam, 0x63, 0x00); | ||
207 | err += sn9c102_i2c_write(cam, 0x64, 0x50); | ||
208 | err += sn9c102_i2c_write(cam, 0x65, 0x30); | ||
209 | err += sn9c102_i2c_write(cam, 0x66, 0x00); | ||
210 | err += sn9c102_i2c_write(cam, 0x67, 0x80); | ||
211 | err += sn9c102_i2c_write(cam, 0x68, 0x7A); | ||
212 | err += sn9c102_i2c_write(cam, 0x69, 0x90); | ||
213 | err += sn9c102_i2c_write(cam, 0x6A, 0x80); | ||
214 | err += sn9c102_i2c_write(cam, 0x6B, 0x0A); | ||
215 | err += sn9c102_i2c_write(cam, 0x6C, 0x30); | ||
216 | err += sn9c102_i2c_write(cam, 0x6D, 0x48); | ||
217 | err += sn9c102_i2c_write(cam, 0x6E, 0x80); | ||
218 | err += sn9c102_i2c_write(cam, 0x6F, 0x74); | ||
219 | err += sn9c102_i2c_write(cam, 0x70, 0x64); | ||
220 | err += sn9c102_i2c_write(cam, 0x71, 0x60); | ||
221 | err += sn9c102_i2c_write(cam, 0x72, 0x5C); | ||
222 | err += sn9c102_i2c_write(cam, 0x73, 0x58); | ||
223 | err += sn9c102_i2c_write(cam, 0x74, 0x54); | ||
224 | err += sn9c102_i2c_write(cam, 0x75, 0x4C); | ||
225 | err += sn9c102_i2c_write(cam, 0x76, 0x40); | ||
226 | err += sn9c102_i2c_write(cam, 0x77, 0x38); | ||
227 | err += sn9c102_i2c_write(cam, 0x78, 0x34); | ||
228 | err += sn9c102_i2c_write(cam, 0x79, 0x30); | ||
229 | err += sn9c102_i2c_write(cam, 0x7A, 0x2F); | ||
230 | err += sn9c102_i2c_write(cam, 0x7B, 0x2B); | ||
231 | err += sn9c102_i2c_write(cam, 0x7C, 0x03); | ||
232 | err += sn9c102_i2c_write(cam, 0x7D, 0x07); | ||
233 | err += sn9c102_i2c_write(cam, 0x7E, 0x17); | ||
234 | err += sn9c102_i2c_write(cam, 0x7F, 0x34); | ||
235 | err += sn9c102_i2c_write(cam, 0x80, 0x41); | ||
236 | err += sn9c102_i2c_write(cam, 0x81, 0x4D); | ||
237 | err += sn9c102_i2c_write(cam, 0x82, 0x58); | ||
238 | err += sn9c102_i2c_write(cam, 0x83, 0x63); | ||
239 | err += sn9c102_i2c_write(cam, 0x84, 0x6E); | ||
240 | err += sn9c102_i2c_write(cam, 0x85, 0x77); | ||
241 | err += sn9c102_i2c_write(cam, 0x86, 0x87); | ||
242 | err += sn9c102_i2c_write(cam, 0x87, 0x95); | ||
243 | err += sn9c102_i2c_write(cam, 0x88, 0xAF); | ||
244 | err += sn9c102_i2c_write(cam, 0x89, 0xC7); | ||
245 | err += sn9c102_i2c_write(cam, 0x8A, 0xDF); | ||
246 | err += sn9c102_i2c_write(cam, 0x8B, 0x99); | ||
247 | err += sn9c102_i2c_write(cam, 0x8C, 0x99); | ||
248 | err += sn9c102_i2c_write(cam, 0x8D, 0xCF); | ||
249 | err += sn9c102_i2c_write(cam, 0x8E, 0x20); | ||
250 | err += sn9c102_i2c_write(cam, 0x8F, 0x26); | ||
251 | err += sn9c102_i2c_write(cam, 0x90, 0x10); | ||
252 | err += sn9c102_i2c_write(cam, 0x91, 0x0C); | ||
253 | err += sn9c102_i2c_write(cam, 0x92, 0x25); | ||
254 | err += sn9c102_i2c_write(cam, 0x93, 0x00); | ||
255 | err += sn9c102_i2c_write(cam, 0x94, 0x50); | ||
256 | err += sn9c102_i2c_write(cam, 0x95, 0x50); | ||
257 | err += sn9c102_i2c_write(cam, 0x96, 0x00); | ||
258 | err += sn9c102_i2c_write(cam, 0x97, 0x01); | ||
259 | err += sn9c102_i2c_write(cam, 0x98, 0x10); | ||
260 | err += sn9c102_i2c_write(cam, 0x99, 0x40); | ||
261 | err += sn9c102_i2c_write(cam, 0x9A, 0x40); | ||
262 | err += sn9c102_i2c_write(cam, 0x9B, 0x20); | ||
263 | err += sn9c102_i2c_write(cam, 0x9C, 0x00); | ||
264 | err += sn9c102_i2c_write(cam, 0x9D, 0x99); | ||
265 | err += sn9c102_i2c_write(cam, 0x9E, 0x7F); | ||
266 | err += sn9c102_i2c_write(cam, 0x9F, 0x00); | ||
267 | err += sn9c102_i2c_write(cam, 0xA0, 0x00); | ||
268 | err += sn9c102_i2c_write(cam, 0xA1, 0x00); | ||
269 | |||
270 | return err; | ||
271 | } | ||
272 | |||
273 | |||
274 | static int ov7660_get_ctrl(struct sn9c102_device* cam, | ||
275 | struct v4l2_control* ctrl) | ||
276 | { | ||
277 | int err = 0; | ||
278 | |||
279 | switch (ctrl->id) { | ||
280 | case V4L2_CID_EXPOSURE: | ||
281 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x10)) < 0) | ||
282 | return -EIO; | ||
283 | break; | ||
284 | case V4L2_CID_DO_WHITE_BALANCE: | ||
285 | if ((ctrl->value = sn9c102_read_reg(cam, 0x02)) < 0) | ||
286 | return -EIO; | ||
287 | ctrl->value = (ctrl->value & 0x04) ? 1 : 0; | ||
288 | break; | ||
289 | case V4L2_CID_RED_BALANCE: | ||
290 | if ((ctrl->value = sn9c102_read_reg(cam, 0x05)) < 0) | ||
291 | return -EIO; | ||
292 | ctrl->value &= 0x7f; | ||
293 | break; | ||
294 | case V4L2_CID_BLUE_BALANCE: | ||
295 | if ((ctrl->value = sn9c102_read_reg(cam, 0x06)) < 0) | ||
296 | return -EIO; | ||
297 | ctrl->value &= 0x7f; | ||
298 | break; | ||
299 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
300 | if ((ctrl->value = sn9c102_read_reg(cam, 0x07)) < 0) | ||
301 | return -EIO; | ||
302 | ctrl->value &= 0x7f; | ||
303 | break; | ||
304 | case SN9C102_V4L2_CID_BAND_FILTER: | ||
305 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x3b)) < 0) | ||
306 | return -EIO; | ||
307 | ctrl->value &= 0x08; | ||
308 | break; | ||
309 | case V4L2_CID_GAIN: | ||
310 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x00)) < 0) | ||
311 | return -EIO; | ||
312 | ctrl->value &= 0x1f; | ||
313 | break; | ||
314 | case V4L2_CID_AUTOGAIN: | ||
315 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x13)) < 0) | ||
316 | return -EIO; | ||
317 | ctrl->value &= 0x01; | ||
318 | break; | ||
319 | default: | ||
320 | return -EINVAL; | ||
321 | } | ||
322 | |||
323 | return err ? -EIO : 0; | ||
324 | } | ||
325 | |||
326 | |||
327 | static int ov7660_set_ctrl(struct sn9c102_device* cam, | ||
328 | const struct v4l2_control* ctrl) | ||
329 | { | ||
330 | int err = 0; | ||
331 | |||
332 | switch (ctrl->id) { | ||
333 | case V4L2_CID_EXPOSURE: | ||
334 | err += sn9c102_i2c_write(cam, 0x10, ctrl->value); | ||
335 | break; | ||
336 | case V4L2_CID_DO_WHITE_BALANCE: | ||
337 | err += sn9c102_write_reg(cam, 0x43 | (ctrl->value << 2), 0x02); | ||
338 | break; | ||
339 | case V4L2_CID_RED_BALANCE: | ||
340 | err += sn9c102_write_reg(cam, ctrl->value, 0x05); | ||
341 | break; | ||
342 | case V4L2_CID_BLUE_BALANCE: | ||
343 | err += sn9c102_write_reg(cam, ctrl->value, 0x06); | ||
344 | break; | ||
345 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
346 | err += sn9c102_write_reg(cam, ctrl->value, 0x07); | ||
347 | break; | ||
348 | case SN9C102_V4L2_CID_BAND_FILTER: | ||
349 | err += sn9c102_i2c_write(cam, ctrl->value << 3, 0x3b); | ||
350 | break; | ||
351 | case V4L2_CID_GAIN: | ||
352 | err += sn9c102_i2c_write(cam, 0x00, 0x60 + ctrl->value); | ||
353 | break; | ||
354 | case V4L2_CID_AUTOGAIN: | ||
355 | err += sn9c102_i2c_write(cam, 0x13, 0xc0 | | ||
356 | (ctrl->value * 0x07)); | ||
357 | break; | ||
358 | default: | ||
359 | return -EINVAL; | ||
360 | } | ||
361 | |||
362 | return err ? -EIO : 0; | ||
363 | } | ||
364 | |||
365 | |||
366 | static int ov7660_set_crop(struct sn9c102_device* cam, | ||
367 | const struct v4l2_rect* rect) | ||
368 | { | ||
369 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
370 | int err = 0; | ||
371 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1, | ||
372 | v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1; | ||
373 | |||
374 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
375 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
376 | |||
377 | return err; | ||
378 | } | ||
379 | |||
380 | |||
381 | static int ov7660_set_pix_format(struct sn9c102_device* cam, | ||
382 | const struct v4l2_pix_format* pix) | ||
383 | { | ||
384 | int r0, err = 0; | ||
385 | |||
386 | r0 = sn9c102_pread_reg(cam, 0x01); | ||
387 | |||
388 | if (pix->pixelformat == V4L2_PIX_FMT_JPEG) { | ||
389 | err += sn9c102_write_reg(cam, r0 | 0x40, 0x01); | ||
390 | err += sn9c102_write_reg(cam, 0xa2, 0x17); | ||
391 | err += sn9c102_i2c_write(cam, 0x11, 0x00); | ||
392 | } else { | ||
393 | err += sn9c102_write_reg(cam, r0 | 0x40, 0x01); | ||
394 | err += sn9c102_write_reg(cam, 0xa2, 0x17); | ||
395 | err += sn9c102_i2c_write(cam, 0x11, 0x0d); | ||
396 | } | ||
397 | |||
398 | return err; | ||
399 | } | ||
400 | |||
401 | |||
402 | static const struct sn9c102_sensor ov7660 = { | ||
403 | .name = "OV7660", | ||
404 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
405 | .supported_bridge = BRIDGE_SN9C105 | BRIDGE_SN9C120, | ||
406 | .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE, | ||
407 | .frequency = SN9C102_I2C_100KHZ, | ||
408 | .interface = SN9C102_I2C_2WIRES, | ||
409 | .i2c_slave_id = 0x21, | ||
410 | .init = &ov7660_init, | ||
411 | .qctrl = { | ||
412 | { | ||
413 | .id = V4L2_CID_GAIN, | ||
414 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
415 | .name = "global gain", | ||
416 | .minimum = 0x00, | ||
417 | .maximum = 0x1f, | ||
418 | .step = 0x01, | ||
419 | .default_value = 0x09, | ||
420 | .flags = 0, | ||
421 | }, | ||
422 | { | ||
423 | .id = V4L2_CID_EXPOSURE, | ||
424 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
425 | .name = "exposure", | ||
426 | .minimum = 0x00, | ||
427 | .maximum = 0xff, | ||
428 | .step = 0x01, | ||
429 | .default_value = 0x27, | ||
430 | .flags = 0, | ||
431 | }, | ||
432 | { | ||
433 | .id = V4L2_CID_DO_WHITE_BALANCE, | ||
434 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
435 | .name = "night mode", | ||
436 | .minimum = 0x00, | ||
437 | .maximum = 0x01, | ||
438 | .step = 0x01, | ||
439 | .default_value = 0x00, | ||
440 | .flags = 0, | ||
441 | }, | ||
442 | { | ||
443 | .id = V4L2_CID_RED_BALANCE, | ||
444 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
445 | .name = "red balance", | ||
446 | .minimum = 0x00, | ||
447 | .maximum = 0x7f, | ||
448 | .step = 0x01, | ||
449 | .default_value = 0x14, | ||
450 | .flags = 0, | ||
451 | }, | ||
452 | { | ||
453 | .id = V4L2_CID_BLUE_BALANCE, | ||
454 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
455 | .name = "blue balance", | ||
456 | .minimum = 0x00, | ||
457 | .maximum = 0x7f, | ||
458 | .step = 0x01, | ||
459 | .default_value = 0x14, | ||
460 | .flags = 0, | ||
461 | }, | ||
462 | { | ||
463 | .id = V4L2_CID_AUTOGAIN, | ||
464 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
465 | .name = "auto adjust", | ||
466 | .minimum = 0x00, | ||
467 | .maximum = 0x01, | ||
468 | .step = 0x01, | ||
469 | .default_value = 0x01, | ||
470 | .flags = 0, | ||
471 | }, | ||
472 | { | ||
473 | .id = SN9C102_V4L2_CID_GREEN_BALANCE, | ||
474 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
475 | .name = "green balance", | ||
476 | .minimum = 0x00, | ||
477 | .maximum = 0x7f, | ||
478 | .step = 0x01, | ||
479 | .default_value = 0x14, | ||
480 | .flags = 0, | ||
481 | }, | ||
482 | { | ||
483 | .id = SN9C102_V4L2_CID_BAND_FILTER, | ||
484 | .type = V4L2_CTRL_TYPE_BOOLEAN, | ||
485 | .name = "band filter", | ||
486 | .minimum = 0x00, | ||
487 | .maximum = 0x01, | ||
488 | .step = 0x01, | ||
489 | .default_value = 0x00, | ||
490 | .flags = 0, | ||
491 | }, | ||
492 | }, | ||
493 | .get_ctrl = &ov7660_get_ctrl, | ||
494 | .set_ctrl = &ov7660_set_ctrl, | ||
495 | .cropcap = { | ||
496 | .bounds = { | ||
497 | .left = 0, | ||
498 | .top = 0, | ||
499 | .width = 640, | ||
500 | .height = 480, | ||
501 | }, | ||
502 | .defrect = { | ||
503 | .left = 0, | ||
504 | .top = 0, | ||
505 | .width = 640, | ||
506 | .height = 480, | ||
507 | }, | ||
508 | }, | ||
509 | .set_crop = &ov7660_set_crop, | ||
510 | .pix_format = { | ||
511 | .width = 640, | ||
512 | .height = 480, | ||
513 | .pixelformat = V4L2_PIX_FMT_JPEG, | ||
514 | .priv = 8, | ||
515 | }, | ||
516 | .set_pix_format = &ov7660_set_pix_format | ||
517 | }; | ||
518 | |||
519 | |||
520 | int sn9c102_probe_ov7660(struct sn9c102_device* cam) | ||
521 | { | ||
522 | int pid, ver, err; | ||
523 | |||
524 | err = sn9c102_write_const_regs(cam, {0x01, 0xf1}, {0x00, 0xf1}, | ||
525 | {0x01, 0x01}, {0x00, 0x01}, | ||
526 | {0x28, 0x17}); | ||
527 | |||
528 | pid = sn9c102_i2c_try_read(cam, &ov7660, 0x0a); | ||
529 | ver = sn9c102_i2c_try_read(cam, &ov7660, 0x0b); | ||
530 | if (err || pid < 0 || ver < 0) | ||
531 | return -EIO; | ||
532 | if (pid != 0x76 || ver != 0x60) | ||
533 | return -ENODEV; | ||
534 | |||
535 | sn9c102_attach_sensor(cam, &ov7660); | ||
536 | |||
537 | return 0; | ||
538 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_pas106b.c b/drivers/media/usb/sn9c102/sn9c102_pas106b.c new file mode 100644 index 000000000000..81cd969c1b7b --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_pas106b.c | |||
@@ -0,0 +1,302 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for PAS106B image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include <linux/delay.h> | ||
23 | #include "sn9c102_sensor.h" | ||
24 | #include "sn9c102_devtable.h" | ||
25 | |||
26 | |||
27 | static int pas106b_init(struct sn9c102_device* cam) | ||
28 | { | ||
29 | int err = 0; | ||
30 | |||
31 | err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11}, | ||
32 | {0x00, 0x14}, {0x20, 0x17}, | ||
33 | {0x20, 0x19}, {0x09, 0x18}); | ||
34 | |||
35 | err += sn9c102_i2c_write(cam, 0x02, 0x0c); | ||
36 | err += sn9c102_i2c_write(cam, 0x05, 0x5a); | ||
37 | err += sn9c102_i2c_write(cam, 0x06, 0x88); | ||
38 | err += sn9c102_i2c_write(cam, 0x07, 0x80); | ||
39 | err += sn9c102_i2c_write(cam, 0x10, 0x06); | ||
40 | err += sn9c102_i2c_write(cam, 0x11, 0x06); | ||
41 | err += sn9c102_i2c_write(cam, 0x12, 0x00); | ||
42 | err += sn9c102_i2c_write(cam, 0x14, 0x02); | ||
43 | err += sn9c102_i2c_write(cam, 0x13, 0x01); | ||
44 | |||
45 | msleep(400); | ||
46 | |||
47 | return err; | ||
48 | } | ||
49 | |||
50 | |||
51 | static int pas106b_get_ctrl(struct sn9c102_device* cam, | ||
52 | struct v4l2_control* ctrl) | ||
53 | { | ||
54 | switch (ctrl->id) { | ||
55 | case V4L2_CID_EXPOSURE: | ||
56 | { | ||
57 | int r1 = sn9c102_i2c_read(cam, 0x03), | ||
58 | r2 = sn9c102_i2c_read(cam, 0x04); | ||
59 | if (r1 < 0 || r2 < 0) | ||
60 | return -EIO; | ||
61 | ctrl->value = (r1 << 4) | (r2 & 0x0f); | ||
62 | } | ||
63 | return 0; | ||
64 | case V4L2_CID_RED_BALANCE: | ||
65 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0) | ||
66 | return -EIO; | ||
67 | ctrl->value &= 0x1f; | ||
68 | return 0; | ||
69 | case V4L2_CID_BLUE_BALANCE: | ||
70 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x09)) < 0) | ||
71 | return -EIO; | ||
72 | ctrl->value &= 0x1f; | ||
73 | return 0; | ||
74 | case V4L2_CID_GAIN: | ||
75 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x0e)) < 0) | ||
76 | return -EIO; | ||
77 | ctrl->value &= 0x1f; | ||
78 | return 0; | ||
79 | case V4L2_CID_CONTRAST: | ||
80 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x0f)) < 0) | ||
81 | return -EIO; | ||
82 | ctrl->value &= 0x07; | ||
83 | return 0; | ||
84 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
85 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x0a)) < 0) | ||
86 | return -EIO; | ||
87 | ctrl->value = (ctrl->value & 0x1f) << 1; | ||
88 | return 0; | ||
89 | case SN9C102_V4L2_CID_DAC_MAGNITUDE: | ||
90 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x08)) < 0) | ||
91 | return -EIO; | ||
92 | ctrl->value &= 0xf8; | ||
93 | return 0; | ||
94 | default: | ||
95 | return -EINVAL; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | |||
100 | static int pas106b_set_ctrl(struct sn9c102_device* cam, | ||
101 | const struct v4l2_control* ctrl) | ||
102 | { | ||
103 | int err = 0; | ||
104 | |||
105 | switch (ctrl->id) { | ||
106 | case V4L2_CID_EXPOSURE: | ||
107 | err += sn9c102_i2c_write(cam, 0x03, ctrl->value >> 4); | ||
108 | err += sn9c102_i2c_write(cam, 0x04, ctrl->value & 0x0f); | ||
109 | break; | ||
110 | case V4L2_CID_RED_BALANCE: | ||
111 | err += sn9c102_i2c_write(cam, 0x0c, ctrl->value); | ||
112 | break; | ||
113 | case V4L2_CID_BLUE_BALANCE: | ||
114 | err += sn9c102_i2c_write(cam, 0x09, ctrl->value); | ||
115 | break; | ||
116 | case V4L2_CID_GAIN: | ||
117 | err += sn9c102_i2c_write(cam, 0x0e, ctrl->value); | ||
118 | break; | ||
119 | case V4L2_CID_CONTRAST: | ||
120 | err += sn9c102_i2c_write(cam, 0x0f, ctrl->value); | ||
121 | break; | ||
122 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
123 | err += sn9c102_i2c_write(cam, 0x0a, ctrl->value >> 1); | ||
124 | err += sn9c102_i2c_write(cam, 0x0b, ctrl->value >> 1); | ||
125 | break; | ||
126 | case SN9C102_V4L2_CID_DAC_MAGNITUDE: | ||
127 | err += sn9c102_i2c_write(cam, 0x08, ctrl->value << 3); | ||
128 | break; | ||
129 | default: | ||
130 | return -EINVAL; | ||
131 | } | ||
132 | err += sn9c102_i2c_write(cam, 0x13, 0x01); | ||
133 | |||
134 | return err ? -EIO : 0; | ||
135 | } | ||
136 | |||
137 | |||
138 | static int pas106b_set_crop(struct sn9c102_device* cam, | ||
139 | const struct v4l2_rect* rect) | ||
140 | { | ||
141 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
142 | int err = 0; | ||
143 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4, | ||
144 | v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3; | ||
145 | |||
146 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
147 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
148 | |||
149 | return err; | ||
150 | } | ||
151 | |||
152 | |||
153 | static int pas106b_set_pix_format(struct sn9c102_device* cam, | ||
154 | const struct v4l2_pix_format* pix) | ||
155 | { | ||
156 | int err = 0; | ||
157 | |||
158 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) | ||
159 | err += sn9c102_write_reg(cam, 0x2c, 0x17); | ||
160 | else | ||
161 | err += sn9c102_write_reg(cam, 0x20, 0x17); | ||
162 | |||
163 | return err; | ||
164 | } | ||
165 | |||
166 | |||
167 | static const struct sn9c102_sensor pas106b = { | ||
168 | .name = "PAS106B", | ||
169 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
170 | .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102, | ||
171 | .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE, | ||
172 | .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ, | ||
173 | .interface = SN9C102_I2C_2WIRES, | ||
174 | .i2c_slave_id = 0x40, | ||
175 | .init = &pas106b_init, | ||
176 | .qctrl = { | ||
177 | { | ||
178 | .id = V4L2_CID_EXPOSURE, | ||
179 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
180 | .name = "exposure", | ||
181 | .minimum = 0x125, | ||
182 | .maximum = 0xfff, | ||
183 | .step = 0x001, | ||
184 | .default_value = 0x140, | ||
185 | .flags = 0, | ||
186 | }, | ||
187 | { | ||
188 | .id = V4L2_CID_GAIN, | ||
189 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
190 | .name = "global gain", | ||
191 | .minimum = 0x00, | ||
192 | .maximum = 0x1f, | ||
193 | .step = 0x01, | ||
194 | .default_value = 0x0d, | ||
195 | .flags = 0, | ||
196 | }, | ||
197 | { | ||
198 | .id = V4L2_CID_CONTRAST, | ||
199 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
200 | .name = "contrast", | ||
201 | .minimum = 0x00, | ||
202 | .maximum = 0x07, | ||
203 | .step = 0x01, | ||
204 | .default_value = 0x00, /* 0x00~0x03 have same effect */ | ||
205 | .flags = 0, | ||
206 | }, | ||
207 | { | ||
208 | .id = V4L2_CID_RED_BALANCE, | ||
209 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
210 | .name = "red balance", | ||
211 | .minimum = 0x00, | ||
212 | .maximum = 0x1f, | ||
213 | .step = 0x01, | ||
214 | .default_value = 0x04, | ||
215 | .flags = 0, | ||
216 | }, | ||
217 | { | ||
218 | .id = V4L2_CID_BLUE_BALANCE, | ||
219 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
220 | .name = "blue balance", | ||
221 | .minimum = 0x00, | ||
222 | .maximum = 0x1f, | ||
223 | .step = 0x01, | ||
224 | .default_value = 0x06, | ||
225 | .flags = 0, | ||
226 | }, | ||
227 | { | ||
228 | .id = SN9C102_V4L2_CID_GREEN_BALANCE, | ||
229 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
230 | .name = "green balance", | ||
231 | .minimum = 0x00, | ||
232 | .maximum = 0x3e, | ||
233 | .step = 0x02, | ||
234 | .default_value = 0x02, | ||
235 | .flags = 0, | ||
236 | }, | ||
237 | { | ||
238 | .id = SN9C102_V4L2_CID_DAC_MAGNITUDE, | ||
239 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
240 | .name = "DAC magnitude", | ||
241 | .minimum = 0x00, | ||
242 | .maximum = 0x1f, | ||
243 | .step = 0x01, | ||
244 | .default_value = 0x01, | ||
245 | .flags = 0, | ||
246 | }, | ||
247 | }, | ||
248 | .get_ctrl = &pas106b_get_ctrl, | ||
249 | .set_ctrl = &pas106b_set_ctrl, | ||
250 | .cropcap = { | ||
251 | .bounds = { | ||
252 | .left = 0, | ||
253 | .top = 0, | ||
254 | .width = 352, | ||
255 | .height = 288, | ||
256 | }, | ||
257 | .defrect = { | ||
258 | .left = 0, | ||
259 | .top = 0, | ||
260 | .width = 352, | ||
261 | .height = 288, | ||
262 | }, | ||
263 | }, | ||
264 | .set_crop = &pas106b_set_crop, | ||
265 | .pix_format = { | ||
266 | .width = 352, | ||
267 | .height = 288, | ||
268 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
269 | .priv = 8, /* we use this field as 'bits per pixel' */ | ||
270 | }, | ||
271 | .set_pix_format = &pas106b_set_pix_format | ||
272 | }; | ||
273 | |||
274 | |||
275 | int sn9c102_probe_pas106b(struct sn9c102_device* cam) | ||
276 | { | ||
277 | int r0 = 0, r1 = 0; | ||
278 | unsigned int pid = 0; | ||
279 | |||
280 | /* | ||
281 | Minimal initialization to enable the I2C communication | ||
282 | NOTE: do NOT change the values! | ||
283 | */ | ||
284 | if (sn9c102_write_const_regs(cam, | ||
285 | {0x01, 0x01}, /* sensor power down */ | ||
286 | {0x00, 0x01}, /* sensor power on */ | ||
287 | {0x28, 0x17})) /* sensor clock at 24 MHz */ | ||
288 | return -EIO; | ||
289 | |||
290 | r0 = sn9c102_i2c_try_read(cam, &pas106b, 0x00); | ||
291 | r1 = sn9c102_i2c_try_read(cam, &pas106b, 0x01); | ||
292 | if (r0 < 0 || r1 < 0) | ||
293 | return -EIO; | ||
294 | |||
295 | pid = (r0 << 11) | ((r1 & 0xf0) >> 4); | ||
296 | if (pid != 0x007) | ||
297 | return -ENODEV; | ||
298 | |||
299 | sn9c102_attach_sensor(cam, &pas106b); | ||
300 | |||
301 | return 0; | ||
302 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_pas202bcb.c b/drivers/media/usb/sn9c102/sn9c102_pas202bcb.c new file mode 100644 index 000000000000..2e86fdc86989 --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_pas202bcb.c | |||
@@ -0,0 +1,335 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for PAS202BCB image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2004 by Carlos Eduardo Medaglia Dyonisio * | ||
6 | * <medaglia@undl.org.br> * | ||
7 | * * | ||
8 | * Support for SN9C103, DAC Magnitude, exposure and green gain controls * | ||
9 | * added by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
10 | * * | ||
11 | * This program is free software; you can redistribute it and/or modify * | ||
12 | * it under the terms of the GNU General Public License as published by * | ||
13 | * the Free Software Foundation; either version 2 of the License, or * | ||
14 | * (at your option) any later version. * | ||
15 | * * | ||
16 | * This program is distributed in the hope that it will be useful, * | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
19 | * GNU General Public License for more details. * | ||
20 | * * | ||
21 | * You should have received a copy of the GNU General Public License * | ||
22 | * along with this program; if not, write to the Free Software * | ||
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
24 | ***************************************************************************/ | ||
25 | |||
26 | #include <linux/delay.h> | ||
27 | #include "sn9c102_sensor.h" | ||
28 | #include "sn9c102_devtable.h" | ||
29 | |||
30 | |||
31 | static int pas202bcb_init(struct sn9c102_device* cam) | ||
32 | { | ||
33 | int err = 0; | ||
34 | |||
35 | switch (sn9c102_get_bridge(cam)) { | ||
36 | case BRIDGE_SN9C101: | ||
37 | case BRIDGE_SN9C102: | ||
38 | err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11}, | ||
39 | {0x00, 0x14}, {0x20, 0x17}, | ||
40 | {0x30, 0x19}, {0x09, 0x18}); | ||
41 | break; | ||
42 | case BRIDGE_SN9C103: | ||
43 | err = sn9c102_write_const_regs(cam, {0x00, 0x02}, {0x00, 0x03}, | ||
44 | {0x1a, 0x04}, {0x20, 0x05}, | ||
45 | {0x20, 0x06}, {0x20, 0x07}, | ||
46 | {0x00, 0x10}, {0x00, 0x11}, | ||
47 | {0x00, 0x14}, {0x20, 0x17}, | ||
48 | {0x30, 0x19}, {0x09, 0x18}, | ||
49 | {0x02, 0x1c}, {0x03, 0x1d}, | ||
50 | {0x0f, 0x1e}, {0x0c, 0x1f}, | ||
51 | {0x00, 0x20}, {0x10, 0x21}, | ||
52 | {0x20, 0x22}, {0x30, 0x23}, | ||
53 | {0x40, 0x24}, {0x50, 0x25}, | ||
54 | {0x60, 0x26}, {0x70, 0x27}, | ||
55 | {0x80, 0x28}, {0x90, 0x29}, | ||
56 | {0xa0, 0x2a}, {0xb0, 0x2b}, | ||
57 | {0xc0, 0x2c}, {0xd0, 0x2d}, | ||
58 | {0xe0, 0x2e}, {0xf0, 0x2f}, | ||
59 | {0xff, 0x30}); | ||
60 | break; | ||
61 | default: | ||
62 | break; | ||
63 | } | ||
64 | |||
65 | err += sn9c102_i2c_write(cam, 0x02, 0x14); | ||
66 | err += sn9c102_i2c_write(cam, 0x03, 0x40); | ||
67 | err += sn9c102_i2c_write(cam, 0x0d, 0x2c); | ||
68 | err += sn9c102_i2c_write(cam, 0x0e, 0x01); | ||
69 | err += sn9c102_i2c_write(cam, 0x0f, 0xa9); | ||
70 | err += sn9c102_i2c_write(cam, 0x10, 0x08); | ||
71 | err += sn9c102_i2c_write(cam, 0x13, 0x63); | ||
72 | err += sn9c102_i2c_write(cam, 0x15, 0x70); | ||
73 | err += sn9c102_i2c_write(cam, 0x11, 0x01); | ||
74 | |||
75 | msleep(400); | ||
76 | |||
77 | return err; | ||
78 | } | ||
79 | |||
80 | |||
81 | static int pas202bcb_get_ctrl(struct sn9c102_device* cam, | ||
82 | struct v4l2_control* ctrl) | ||
83 | { | ||
84 | switch (ctrl->id) { | ||
85 | case V4L2_CID_EXPOSURE: | ||
86 | { | ||
87 | int r1 = sn9c102_i2c_read(cam, 0x04), | ||
88 | r2 = sn9c102_i2c_read(cam, 0x05); | ||
89 | if (r1 < 0 || r2 < 0) | ||
90 | return -EIO; | ||
91 | ctrl->value = (r1 << 6) | (r2 & 0x3f); | ||
92 | } | ||
93 | return 0; | ||
94 | case V4L2_CID_RED_BALANCE: | ||
95 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x09)) < 0) | ||
96 | return -EIO; | ||
97 | ctrl->value &= 0x0f; | ||
98 | return 0; | ||
99 | case V4L2_CID_BLUE_BALANCE: | ||
100 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x07)) < 0) | ||
101 | return -EIO; | ||
102 | ctrl->value &= 0x0f; | ||
103 | return 0; | ||
104 | case V4L2_CID_GAIN: | ||
105 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x10)) < 0) | ||
106 | return -EIO; | ||
107 | ctrl->value &= 0x1f; | ||
108 | return 0; | ||
109 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
110 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x08)) < 0) | ||
111 | return -EIO; | ||
112 | ctrl->value &= 0x0f; | ||
113 | return 0; | ||
114 | case SN9C102_V4L2_CID_DAC_MAGNITUDE: | ||
115 | if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0) | ||
116 | return -EIO; | ||
117 | return 0; | ||
118 | default: | ||
119 | return -EINVAL; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | |||
124 | static int pas202bcb_set_pix_format(struct sn9c102_device* cam, | ||
125 | const struct v4l2_pix_format* pix) | ||
126 | { | ||
127 | int err = 0; | ||
128 | |||
129 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) | ||
130 | err += sn9c102_write_reg(cam, 0x28, 0x17); | ||
131 | else | ||
132 | err += sn9c102_write_reg(cam, 0x20, 0x17); | ||
133 | |||
134 | return err; | ||
135 | } | ||
136 | |||
137 | |||
138 | static int pas202bcb_set_ctrl(struct sn9c102_device* cam, | ||
139 | const struct v4l2_control* ctrl) | ||
140 | { | ||
141 | int err = 0; | ||
142 | |||
143 | switch (ctrl->id) { | ||
144 | case V4L2_CID_EXPOSURE: | ||
145 | err += sn9c102_i2c_write(cam, 0x04, ctrl->value >> 6); | ||
146 | err += sn9c102_i2c_write(cam, 0x05, ctrl->value & 0x3f); | ||
147 | break; | ||
148 | case V4L2_CID_RED_BALANCE: | ||
149 | err += sn9c102_i2c_write(cam, 0x09, ctrl->value); | ||
150 | break; | ||
151 | case V4L2_CID_BLUE_BALANCE: | ||
152 | err += sn9c102_i2c_write(cam, 0x07, ctrl->value); | ||
153 | break; | ||
154 | case V4L2_CID_GAIN: | ||
155 | err += sn9c102_i2c_write(cam, 0x10, ctrl->value); | ||
156 | break; | ||
157 | case SN9C102_V4L2_CID_GREEN_BALANCE: | ||
158 | err += sn9c102_i2c_write(cam, 0x08, ctrl->value); | ||
159 | break; | ||
160 | case SN9C102_V4L2_CID_DAC_MAGNITUDE: | ||
161 | err += sn9c102_i2c_write(cam, 0x0c, ctrl->value); | ||
162 | break; | ||
163 | default: | ||
164 | return -EINVAL; | ||
165 | } | ||
166 | err += sn9c102_i2c_write(cam, 0x11, 0x01); | ||
167 | |||
168 | return err ? -EIO : 0; | ||
169 | } | ||
170 | |||
171 | |||
172 | static int pas202bcb_set_crop(struct sn9c102_device* cam, | ||
173 | const struct v4l2_rect* rect) | ||
174 | { | ||
175 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
176 | int err = 0; | ||
177 | u8 h_start = 0, | ||
178 | v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3; | ||
179 | |||
180 | switch (sn9c102_get_bridge(cam)) { | ||
181 | case BRIDGE_SN9C101: | ||
182 | case BRIDGE_SN9C102: | ||
183 | h_start = (u8)(rect->left - s->cropcap.bounds.left) + 4; | ||
184 | break; | ||
185 | case BRIDGE_SN9C103: | ||
186 | h_start = (u8)(rect->left - s->cropcap.bounds.left) + 3; | ||
187 | break; | ||
188 | default: | ||
189 | break; | ||
190 | } | ||
191 | |||
192 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
193 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
194 | |||
195 | return err; | ||
196 | } | ||
197 | |||
198 | |||
199 | static const struct sn9c102_sensor pas202bcb = { | ||
200 | .name = "PAS202BCB", | ||
201 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
202 | .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102 | BRIDGE_SN9C103, | ||
203 | .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE, | ||
204 | .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ, | ||
205 | .interface = SN9C102_I2C_2WIRES, | ||
206 | .i2c_slave_id = 0x40, | ||
207 | .init = &pas202bcb_init, | ||
208 | .qctrl = { | ||
209 | { | ||
210 | .id = V4L2_CID_EXPOSURE, | ||
211 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
212 | .name = "exposure", | ||
213 | .minimum = 0x01e5, | ||
214 | .maximum = 0x3fff, | ||
215 | .step = 0x0001, | ||
216 | .default_value = 0x01e5, | ||
217 | .flags = 0, | ||
218 | }, | ||
219 | { | ||
220 | .id = V4L2_CID_GAIN, | ||
221 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
222 | .name = "global gain", | ||
223 | .minimum = 0x00, | ||
224 | .maximum = 0x1f, | ||
225 | .step = 0x01, | ||
226 | .default_value = 0x0b, | ||
227 | .flags = 0, | ||
228 | }, | ||
229 | { | ||
230 | .id = V4L2_CID_RED_BALANCE, | ||
231 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
232 | .name = "red balance", | ||
233 | .minimum = 0x00, | ||
234 | .maximum = 0x0f, | ||
235 | .step = 0x01, | ||
236 | .default_value = 0x00, | ||
237 | .flags = 0, | ||
238 | }, | ||
239 | { | ||
240 | .id = V4L2_CID_BLUE_BALANCE, | ||
241 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
242 | .name = "blue balance", | ||
243 | .minimum = 0x00, | ||
244 | .maximum = 0x0f, | ||
245 | .step = 0x01, | ||
246 | .default_value = 0x05, | ||
247 | .flags = 0, | ||
248 | }, | ||
249 | { | ||
250 | .id = SN9C102_V4L2_CID_GREEN_BALANCE, | ||
251 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
252 | .name = "green balance", | ||
253 | .minimum = 0x00, | ||
254 | .maximum = 0x0f, | ||
255 | .step = 0x01, | ||
256 | .default_value = 0x00, | ||
257 | .flags = 0, | ||
258 | }, | ||
259 | { | ||
260 | .id = SN9C102_V4L2_CID_DAC_MAGNITUDE, | ||
261 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
262 | .name = "DAC magnitude", | ||
263 | .minimum = 0x00, | ||
264 | .maximum = 0xff, | ||
265 | .step = 0x01, | ||
266 | .default_value = 0x04, | ||
267 | .flags = 0, | ||
268 | }, | ||
269 | }, | ||
270 | .get_ctrl = &pas202bcb_get_ctrl, | ||
271 | .set_ctrl = &pas202bcb_set_ctrl, | ||
272 | .cropcap = { | ||
273 | .bounds = { | ||
274 | .left = 0, | ||
275 | .top = 0, | ||
276 | .width = 640, | ||
277 | .height = 480, | ||
278 | }, | ||
279 | .defrect = { | ||
280 | .left = 0, | ||
281 | .top = 0, | ||
282 | .width = 640, | ||
283 | .height = 480, | ||
284 | }, | ||
285 | }, | ||
286 | .set_crop = &pas202bcb_set_crop, | ||
287 | .pix_format = { | ||
288 | .width = 640, | ||
289 | .height = 480, | ||
290 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
291 | .priv = 8, | ||
292 | }, | ||
293 | .set_pix_format = &pas202bcb_set_pix_format | ||
294 | }; | ||
295 | |||
296 | |||
297 | int sn9c102_probe_pas202bcb(struct sn9c102_device* cam) | ||
298 | { | ||
299 | int r0 = 0, r1 = 0, err = 0; | ||
300 | unsigned int pid = 0; | ||
301 | |||
302 | /* | ||
303 | * Minimal initialization to enable the I2C communication | ||
304 | * NOTE: do NOT change the values! | ||
305 | */ | ||
306 | switch (sn9c102_get_bridge(cam)) { | ||
307 | case BRIDGE_SN9C101: | ||
308 | case BRIDGE_SN9C102: | ||
309 | err = sn9c102_write_const_regs(cam, | ||
310 | {0x01, 0x01}, /* power down */ | ||
311 | {0x40, 0x01}, /* power on */ | ||
312 | {0x28, 0x17});/* clock 24 MHz */ | ||
313 | break; | ||
314 | case BRIDGE_SN9C103: /* do _not_ change anything! */ | ||
315 | err = sn9c102_write_const_regs(cam, {0x09, 0x01}, {0x44, 0x01}, | ||
316 | {0x44, 0x02}, {0x29, 0x17}); | ||
317 | break; | ||
318 | default: | ||
319 | break; | ||
320 | } | ||
321 | |||
322 | r0 = sn9c102_i2c_try_read(cam, &pas202bcb, 0x00); | ||
323 | r1 = sn9c102_i2c_try_read(cam, &pas202bcb, 0x01); | ||
324 | |||
325 | if (err || r0 < 0 || r1 < 0) | ||
326 | return -EIO; | ||
327 | |||
328 | pid = (r0 << 4) | ((r1 & 0xf0) >> 4); | ||
329 | if (pid != 0x017) | ||
330 | return -ENODEV; | ||
331 | |||
332 | sn9c102_attach_sensor(cam, &pas202bcb); | ||
333 | |||
334 | return 0; | ||
335 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_sensor.h b/drivers/media/usb/sn9c102/sn9c102_sensor.h new file mode 100644 index 000000000000..3679970dba2c --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_sensor.h | |||
@@ -0,0 +1,307 @@ | |||
1 | /*************************************************************************** | ||
2 | * API for image sensors connected to the SN9C1xx PC Camera Controllers * | ||
3 | * * | ||
4 | * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License as published by * | ||
8 | * the Free Software Foundation; either version 2 of the License, or * | ||
9 | * (at your option) any later version. * | ||
10 | * * | ||
11 | * This program is distributed in the hope that it will be useful, * | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
14 | * GNU General Public License for more details. * | ||
15 | * * | ||
16 | * You should have received a copy of the GNU General Public License * | ||
17 | * along with this program; if not, write to the Free Software * | ||
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
19 | ***************************************************************************/ | ||
20 | |||
21 | #ifndef _SN9C102_SENSOR_H_ | ||
22 | #define _SN9C102_SENSOR_H_ | ||
23 | |||
24 | #include <linux/usb.h> | ||
25 | #include <linux/videodev2.h> | ||
26 | #include <linux/device.h> | ||
27 | #include <linux/stddef.h> | ||
28 | #include <linux/errno.h> | ||
29 | #include <asm/types.h> | ||
30 | |||
31 | struct sn9c102_device; | ||
32 | struct sn9c102_sensor; | ||
33 | |||
34 | /*****************************************************************************/ | ||
35 | |||
36 | /* | ||
37 | OVERVIEW. | ||
38 | This is a small interface that allows you to add support for any CCD/CMOS | ||
39 | image sensors connected to the SN9C1XX bridges. The entire API is documented | ||
40 | below. In the most general case, to support a sensor there are three steps | ||
41 | you have to follow: | ||
42 | 1) define the main "sn9c102_sensor" structure by setting the basic fields; | ||
43 | 2) write a probing function to be called by the core module when the USB | ||
44 | camera is recognized, then add both the USB ids and the name of that | ||
45 | function to the two corresponding tables in sn9c102_devtable.h; | ||
46 | 3) implement the methods that you want/need (and fill the rest of the main | ||
47 | structure accordingly). | ||
48 | "sn9c102_pas106b.c" is an example of all this stuff. Remember that you do | ||
49 | NOT need to touch the source code of the core module for the things to work | ||
50 | properly, unless you find bugs or flaws in it. Finally, do not forget to | ||
51 | read the V4L2 API for completeness. | ||
52 | */ | ||
53 | |||
54 | /*****************************************************************************/ | ||
55 | |||
56 | enum sn9c102_bridge { | ||
57 | BRIDGE_SN9C101 = 0x01, | ||
58 | BRIDGE_SN9C102 = 0x02, | ||
59 | BRIDGE_SN9C103 = 0x04, | ||
60 | BRIDGE_SN9C105 = 0x08, | ||
61 | BRIDGE_SN9C120 = 0x10, | ||
62 | }; | ||
63 | |||
64 | /* Return the bridge name */ | ||
65 | enum sn9c102_bridge sn9c102_get_bridge(struct sn9c102_device* cam); | ||
66 | |||
67 | /* Return a pointer the sensor struct attached to the camera */ | ||
68 | struct sn9c102_sensor* sn9c102_get_sensor(struct sn9c102_device* cam); | ||
69 | |||
70 | /* Identify a device */ | ||
71 | extern struct sn9c102_device* | ||
72 | sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id); | ||
73 | |||
74 | /* Attach a probed sensor to the camera. */ | ||
75 | extern void | ||
76 | sn9c102_attach_sensor(struct sn9c102_device* cam, | ||
77 | const struct sn9c102_sensor* sensor); | ||
78 | |||
79 | /* | ||
80 | Read/write routines: they always return -1 on error, 0 or the read value | ||
81 | otherwise. NOTE that a real read operation is not supported by the SN9C1XX | ||
82 | chip for some of its registers. To work around this problem, a pseudo-read | ||
83 | call is provided instead: it returns the last successfully written value | ||
84 | on the register (0 if it has never been written), the usual -1 on error. | ||
85 | */ | ||
86 | |||
87 | /* The "try" I2C I/O versions are used when probing the sensor */ | ||
88 | extern int sn9c102_i2c_try_read(struct sn9c102_device*, | ||
89 | const struct sn9c102_sensor*, u8 address); | ||
90 | |||
91 | /* | ||
92 | These must be used if and only if the sensor doesn't implement the standard | ||
93 | I2C protocol. There are a number of good reasons why you must use the | ||
94 | single-byte versions of these functions: do not abuse. The first function | ||
95 | writes n bytes, from data0 to datan, to registers 0x09 - 0x09+n of SN9C1XX | ||
96 | chip. The second one programs the registers 0x09 and 0x10 with data0 and | ||
97 | data1, and places the n bytes read from the sensor register table in the | ||
98 | buffer pointed by 'buffer'. Both the functions return -1 on error; the write | ||
99 | version returns 0 on success, while the read version returns the first read | ||
100 | byte. | ||
101 | */ | ||
102 | extern int sn9c102_i2c_try_raw_write(struct sn9c102_device* cam, | ||
103 | const struct sn9c102_sensor* sensor, u8 n, | ||
104 | u8 data0, u8 data1, u8 data2, u8 data3, | ||
105 | u8 data4, u8 data5); | ||
106 | extern int sn9c102_i2c_try_raw_read(struct sn9c102_device* cam, | ||
107 | const struct sn9c102_sensor* sensor, | ||
108 | u8 data0, u8 data1, u8 n, u8 buffer[]); | ||
109 | |||
110 | /* To be used after the sensor struct has been attached to the camera struct */ | ||
111 | extern int sn9c102_i2c_write(struct sn9c102_device*, u8 address, u8 value); | ||
112 | extern int sn9c102_i2c_read(struct sn9c102_device*, u8 address); | ||
113 | |||
114 | /* I/O on registers in the bridge. Could be used by the sensor methods too */ | ||
115 | extern int sn9c102_read_reg(struct sn9c102_device*, u16 index); | ||
116 | extern int sn9c102_pread_reg(struct sn9c102_device*, u16 index); | ||
117 | extern int sn9c102_write_reg(struct sn9c102_device*, u8 value, u16 index); | ||
118 | extern int sn9c102_write_regs(struct sn9c102_device*, const u8 valreg[][2], | ||
119 | int count); | ||
120 | /* | ||
121 | Write multiple registers with constant values. For example: | ||
122 | sn9c102_write_const_regs(cam, {0x00, 0x14}, {0x60, 0x17}, {0x0f, 0x18}); | ||
123 | Register addresses must be < 256. | ||
124 | */ | ||
125 | #define sn9c102_write_const_regs(sn9c102_device, data...) \ | ||
126 | ({ static const u8 _valreg[][2] = {data}; \ | ||
127 | sn9c102_write_regs(sn9c102_device, _valreg, ARRAY_SIZE(_valreg)); }) | ||
128 | |||
129 | /*****************************************************************************/ | ||
130 | |||
131 | enum sn9c102_i2c_sysfs_ops { | ||
132 | SN9C102_I2C_READ = 0x01, | ||
133 | SN9C102_I2C_WRITE = 0x02, | ||
134 | }; | ||
135 | |||
136 | enum sn9c102_i2c_frequency { /* sensors may support both the frequencies */ | ||
137 | SN9C102_I2C_100KHZ = 0x01, | ||
138 | SN9C102_I2C_400KHZ = 0x02, | ||
139 | }; | ||
140 | |||
141 | enum sn9c102_i2c_interface { | ||
142 | SN9C102_I2C_2WIRES, | ||
143 | SN9C102_I2C_3WIRES, | ||
144 | }; | ||
145 | |||
146 | #define SN9C102_MAX_CTRLS (V4L2_CID_LASTP1-V4L2_CID_BASE+10) | ||
147 | |||
148 | struct sn9c102_sensor { | ||
149 | char name[32], /* sensor name */ | ||
150 | maintainer[64]; /* name of the maintainer <email> */ | ||
151 | |||
152 | enum sn9c102_bridge supported_bridge; /* supported SN9C1xx bridges */ | ||
153 | |||
154 | /* Supported operations through the 'sysfs' interface */ | ||
155 | enum sn9c102_i2c_sysfs_ops sysfs_ops; | ||
156 | |||
157 | /* | ||
158 | These sensor capabilities must be provided if the SN9C1XX controller | ||
159 | needs to communicate through the sensor serial interface by using | ||
160 | at least one of the i2c functions available. | ||
161 | */ | ||
162 | enum sn9c102_i2c_frequency frequency; | ||
163 | enum sn9c102_i2c_interface interface; | ||
164 | |||
165 | /* | ||
166 | This identifier must be provided if the image sensor implements | ||
167 | the standard I2C protocol. | ||
168 | */ | ||
169 | u8 i2c_slave_id; /* reg. 0x09 */ | ||
170 | |||
171 | /* | ||
172 | NOTE: Where not noted,most of the functions below are not mandatory. | ||
173 | Set to null if you do not implement them. If implemented, | ||
174 | they must return 0 on success, the proper error otherwise. | ||
175 | */ | ||
176 | |||
177 | int (*init)(struct sn9c102_device* cam); | ||
178 | /* | ||
179 | This function will be called after the sensor has been attached. | ||
180 | It should be used to initialize the sensor only, but may also | ||
181 | configure part of the SN9C1XX chip if necessary. You don't need to | ||
182 | setup picture settings like brightness, contrast, etc.. here, if | ||
183 | the corresponding controls are implemented (see below), since | ||
184 | they are adjusted in the core driver by calling the set_ctrl() | ||
185 | method after init(), where the arguments are the default values | ||
186 | specified in the v4l2_queryctrl list of supported controls; | ||
187 | Same suggestions apply for other settings, _if_ the corresponding | ||
188 | methods are present; if not, the initialization must configure the | ||
189 | sensor according to the default configuration structures below. | ||
190 | */ | ||
191 | |||
192 | struct v4l2_queryctrl qctrl[SN9C102_MAX_CTRLS]; | ||
193 | /* | ||
194 | Optional list of default controls, defined as indicated in the | ||
195 | V4L2 API. Menu type controls are not handled by this interface. | ||
196 | */ | ||
197 | |||
198 | int (*get_ctrl)(struct sn9c102_device* cam, struct v4l2_control* ctrl); | ||
199 | int (*set_ctrl)(struct sn9c102_device* cam, | ||
200 | const struct v4l2_control* ctrl); | ||
201 | /* | ||
202 | You must implement at least the set_ctrl method if you have defined | ||
203 | the list above. The returned value must follow the V4L2 | ||
204 | specifications for the VIDIOC_G|C_CTRL ioctls. V4L2_CID_H|VCENTER | ||
205 | are not supported by this driver, so do not implement them. Also, | ||
206 | you don't have to check whether the passed values are out of bounds, | ||
207 | given that this is done by the core module. | ||
208 | */ | ||
209 | |||
210 | struct v4l2_cropcap cropcap; | ||
211 | /* | ||
212 | Think the image sensor as a grid of R,G,B monochromatic pixels | ||
213 | disposed according to a particular Bayer pattern, which describes | ||
214 | the complete array of pixels, from (0,0) to (xmax, ymax). We will | ||
215 | use this coordinate system from now on. It is assumed the sensor | ||
216 | chip can be programmed to capture/transmit a subsection of that | ||
217 | array of pixels: we will call this subsection "active window". | ||
218 | It is not always true that the largest achievable active window can | ||
219 | cover the whole array of pixels. The V4L2 API defines another | ||
220 | area called "source rectangle", which, in turn, is a subrectangle of | ||
221 | the active window. The SN9C1XX chip is always programmed to read the | ||
222 | source rectangle. | ||
223 | The bounds of both the active window and the source rectangle are | ||
224 | specified in the cropcap substructures 'bounds' and 'defrect'. | ||
225 | By default, the source rectangle should cover the largest possible | ||
226 | area. Again, it is not always true that the largest source rectangle | ||
227 | can cover the entire active window, although it is a rare case for | ||
228 | the hardware we have. The bounds of the source rectangle _must_ be | ||
229 | multiple of 16 and must use the same coordinate system as indicated | ||
230 | before; their centers shall align initially. | ||
231 | If necessary, the sensor chip must be initialized during init() to | ||
232 | set the bounds of the active sensor window; however, by default, it | ||
233 | usually covers the largest achievable area (maxwidth x maxheight) | ||
234 | of pixels, so no particular initialization is needed, if you have | ||
235 | defined the correct default bounds in the structures. | ||
236 | See the V4L2 API for further details. | ||
237 | NOTE: once you have defined the bounds of the active window | ||
238 | (struct cropcap.bounds) you must not change them.anymore. | ||
239 | Only 'bounds' and 'defrect' fields are mandatory, other fields | ||
240 | will be ignored. | ||
241 | */ | ||
242 | |||
243 | int (*set_crop)(struct sn9c102_device* cam, | ||
244 | const struct v4l2_rect* rect); | ||
245 | /* | ||
246 | To be called on VIDIOC_C_SETCROP. The core module always calls a | ||
247 | default routine which configures the appropriate SN9C1XX regs (also | ||
248 | scaling), but you may need to override/adjust specific stuff. | ||
249 | 'rect' contains width and height values that are multiple of 16: in | ||
250 | case you override the default function, you always have to program | ||
251 | the chip to match those values; on error return the corresponding | ||
252 | error code without rolling back. | ||
253 | NOTE: in case, you must program the SN9C1XX chip to get rid of | ||
254 | blank pixels or blank lines at the _start_ of each line or | ||
255 | frame after each HSYNC or VSYNC, so that the image starts with | ||
256 | real RGB data (see regs 0x12, 0x13) (having set H_SIZE and, | ||
257 | V_SIZE you don't have to care about blank pixels or blank | ||
258 | lines at the end of each line or frame). | ||
259 | */ | ||
260 | |||
261 | struct v4l2_pix_format pix_format; | ||
262 | /* | ||
263 | What you have to define here are: 1) initial 'width' and 'height' of | ||
264 | the target rectangle 2) the initial 'pixelformat', which can be | ||
265 | either V4L2_PIX_FMT_SN9C10X, V4L2_PIX_FMT_JPEG (for ompressed video) | ||
266 | or V4L2_PIX_FMT_SBGGR8 3) 'priv', which we'll be used to indicate | ||
267 | the number of bits per pixel for uncompressed video, 8 or 9 (despite | ||
268 | the current value of 'pixelformat'). | ||
269 | NOTE 1: both 'width' and 'height' _must_ be either 1/1 or 1/2 or 1/4 | ||
270 | of cropcap.defrect.width and cropcap.defrect.height. I | ||
271 | suggest 1/1. | ||
272 | NOTE 2: The initial compression quality is defined by the first bit | ||
273 | of reg 0x17 during the initialization of the image sensor. | ||
274 | NOTE 3: as said above, you have to program the SN9C1XX chip to get | ||
275 | rid of any blank pixels, so that the output of the sensor | ||
276 | matches the RGB bayer sequence (i.e. BGBGBG...GRGRGR). | ||
277 | */ | ||
278 | |||
279 | int (*set_pix_format)(struct sn9c102_device* cam, | ||
280 | const struct v4l2_pix_format* pix); | ||
281 | /* | ||
282 | To be called on VIDIOC_S_FMT, when switching from the SBGGR8 to | ||
283 | SN9C10X pixel format or viceversa. On error return the corresponding | ||
284 | error code without rolling back. | ||
285 | */ | ||
286 | |||
287 | /* | ||
288 | Do NOT write to the data below, it's READ ONLY. It is used by the | ||
289 | core module to store successfully updated values of the above | ||
290 | settings, for rollbacks..etc..in case of errors during atomic I/O | ||
291 | */ | ||
292 | struct v4l2_queryctrl _qctrl[SN9C102_MAX_CTRLS]; | ||
293 | struct v4l2_rect _rect; | ||
294 | }; | ||
295 | |||
296 | /*****************************************************************************/ | ||
297 | |||
298 | /* Private ioctl's for control settings supported by some image sensors */ | ||
299 | #define SN9C102_V4L2_CID_DAC_MAGNITUDE (V4L2_CID_PRIVATE_BASE + 0) | ||
300 | #define SN9C102_V4L2_CID_GREEN_BALANCE (V4L2_CID_PRIVATE_BASE + 1) | ||
301 | #define SN9C102_V4L2_CID_RESET_LEVEL (V4L2_CID_PRIVATE_BASE + 2) | ||
302 | #define SN9C102_V4L2_CID_PIXEL_BIAS_VOLTAGE (V4L2_CID_PRIVATE_BASE + 3) | ||
303 | #define SN9C102_V4L2_CID_GAMMA (V4L2_CID_PRIVATE_BASE + 4) | ||
304 | #define SN9C102_V4L2_CID_BAND_FILTER (V4L2_CID_PRIVATE_BASE + 5) | ||
305 | #define SN9C102_V4L2_CID_BRIGHT_LEVEL (V4L2_CID_PRIVATE_BASE + 6) | ||
306 | |||
307 | #endif /* _SN9C102_SENSOR_H_ */ | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_tas5110c1b.c b/drivers/media/usb/sn9c102/sn9c102_tas5110c1b.c new file mode 100644 index 000000000000..04cdfdde8564 --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_tas5110c1b.c | |||
@@ -0,0 +1,154 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for TAS5110C1B image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int tas5110c1b_init(struct sn9c102_device* cam) | ||
27 | { | ||
28 | int err = 0; | ||
29 | |||
30 | err = sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x44, 0x01}, | ||
31 | {0x00, 0x10}, {0x00, 0x11}, | ||
32 | {0x0a, 0x14}, {0x60, 0x17}, | ||
33 | {0x06, 0x18}, {0xfb, 0x19}); | ||
34 | |||
35 | err += sn9c102_i2c_write(cam, 0xc0, 0x80); | ||
36 | |||
37 | return err; | ||
38 | } | ||
39 | |||
40 | |||
41 | static int tas5110c1b_set_ctrl(struct sn9c102_device* cam, | ||
42 | const struct v4l2_control* ctrl) | ||
43 | { | ||
44 | int err = 0; | ||
45 | |||
46 | switch (ctrl->id) { | ||
47 | case V4L2_CID_GAIN: | ||
48 | err += sn9c102_i2c_write(cam, 0x20, 0xf6 - ctrl->value); | ||
49 | break; | ||
50 | default: | ||
51 | return -EINVAL; | ||
52 | } | ||
53 | |||
54 | return err ? -EIO : 0; | ||
55 | } | ||
56 | |||
57 | |||
58 | static int tas5110c1b_set_crop(struct sn9c102_device* cam, | ||
59 | const struct v4l2_rect* rect) | ||
60 | { | ||
61 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
62 | int err = 0; | ||
63 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 69, | ||
64 | v_start = (u8)(rect->top - s->cropcap.bounds.top) + 9; | ||
65 | |||
66 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
67 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
68 | |||
69 | /* Don't change ! */ | ||
70 | err += sn9c102_write_reg(cam, 0x14, 0x1a); | ||
71 | err += sn9c102_write_reg(cam, 0x0a, 0x1b); | ||
72 | err += sn9c102_write_reg(cam, sn9c102_pread_reg(cam, 0x19), 0x19); | ||
73 | |||
74 | return err; | ||
75 | } | ||
76 | |||
77 | |||
78 | static int tas5110c1b_set_pix_format(struct sn9c102_device* cam, | ||
79 | const struct v4l2_pix_format* pix) | ||
80 | { | ||
81 | int err = 0; | ||
82 | |||
83 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) | ||
84 | err += sn9c102_write_reg(cam, 0x2b, 0x19); | ||
85 | else | ||
86 | err += sn9c102_write_reg(cam, 0xfb, 0x19); | ||
87 | |||
88 | return err; | ||
89 | } | ||
90 | |||
91 | |||
92 | static const struct sn9c102_sensor tas5110c1b = { | ||
93 | .name = "TAS5110C1B", | ||
94 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
95 | .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102, | ||
96 | .sysfs_ops = SN9C102_I2C_WRITE, | ||
97 | .frequency = SN9C102_I2C_100KHZ, | ||
98 | .interface = SN9C102_I2C_3WIRES, | ||
99 | .init = &tas5110c1b_init, | ||
100 | .qctrl = { | ||
101 | { | ||
102 | .id = V4L2_CID_GAIN, | ||
103 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
104 | .name = "global gain", | ||
105 | .minimum = 0x00, | ||
106 | .maximum = 0xf6, | ||
107 | .step = 0x01, | ||
108 | .default_value = 0x40, | ||
109 | .flags = 0, | ||
110 | }, | ||
111 | }, | ||
112 | .set_ctrl = &tas5110c1b_set_ctrl, | ||
113 | .cropcap = { | ||
114 | .bounds = { | ||
115 | .left = 0, | ||
116 | .top = 0, | ||
117 | .width = 352, | ||
118 | .height = 288, | ||
119 | }, | ||
120 | .defrect = { | ||
121 | .left = 0, | ||
122 | .top = 0, | ||
123 | .width = 352, | ||
124 | .height = 288, | ||
125 | }, | ||
126 | }, | ||
127 | .set_crop = &tas5110c1b_set_crop, | ||
128 | .pix_format = { | ||
129 | .width = 352, | ||
130 | .height = 288, | ||
131 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
132 | .priv = 8, | ||
133 | }, | ||
134 | .set_pix_format = &tas5110c1b_set_pix_format | ||
135 | }; | ||
136 | |||
137 | |||
138 | int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam) | ||
139 | { | ||
140 | const struct usb_device_id tas5110c1b_id_table[] = { | ||
141 | { USB_DEVICE(0x0c45, 0x6001), }, | ||
142 | { USB_DEVICE(0x0c45, 0x6005), }, | ||
143 | { USB_DEVICE(0x0c45, 0x60ab), }, | ||
144 | { } | ||
145 | }; | ||
146 | |||
147 | /* Sensor detection is based on USB pid/vid */ | ||
148 | if (!sn9c102_match_id(cam, tas5110c1b_id_table)) | ||
149 | return -ENODEV; | ||
150 | |||
151 | sn9c102_attach_sensor(cam, &tas5110c1b); | ||
152 | |||
153 | return 0; | ||
154 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_tas5110d.c b/drivers/media/usb/sn9c102/sn9c102_tas5110d.c new file mode 100644 index 000000000000..9372e6f9fcff --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_tas5110d.c | |||
@@ -0,0 +1,119 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for TAS5110D image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int tas5110d_init(struct sn9c102_device* cam) | ||
27 | { | ||
28 | int err; | ||
29 | |||
30 | err = sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x04, 0x01}, | ||
31 | {0x0a, 0x14}, {0x60, 0x17}, | ||
32 | {0x06, 0x18}, {0xfb, 0x19}); | ||
33 | |||
34 | err += sn9c102_i2c_write(cam, 0x9a, 0xca); | ||
35 | |||
36 | return err; | ||
37 | } | ||
38 | |||
39 | |||
40 | static int tas5110d_set_crop(struct sn9c102_device* cam, | ||
41 | const struct v4l2_rect* rect) | ||
42 | { | ||
43 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
44 | int err = 0; | ||
45 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 69, | ||
46 | v_start = (u8)(rect->top - s->cropcap.bounds.top) + 9; | ||
47 | |||
48 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
49 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
50 | |||
51 | err += sn9c102_write_reg(cam, 0x14, 0x1a); | ||
52 | err += sn9c102_write_reg(cam, 0x0a, 0x1b); | ||
53 | |||
54 | return err; | ||
55 | } | ||
56 | |||
57 | |||
58 | static int tas5110d_set_pix_format(struct sn9c102_device* cam, | ||
59 | const struct v4l2_pix_format* pix) | ||
60 | { | ||
61 | int err = 0; | ||
62 | |||
63 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) | ||
64 | err += sn9c102_write_reg(cam, 0x3b, 0x19); | ||
65 | else | ||
66 | err += sn9c102_write_reg(cam, 0xfb, 0x19); | ||
67 | |||
68 | return err; | ||
69 | } | ||
70 | |||
71 | |||
72 | static const struct sn9c102_sensor tas5110d = { | ||
73 | .name = "TAS5110D", | ||
74 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
75 | .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102, | ||
76 | .sysfs_ops = SN9C102_I2C_WRITE, | ||
77 | .frequency = SN9C102_I2C_100KHZ, | ||
78 | .interface = SN9C102_I2C_2WIRES, | ||
79 | .i2c_slave_id = 0x61, | ||
80 | .init = &tas5110d_init, | ||
81 | .cropcap = { | ||
82 | .bounds = { | ||
83 | .left = 0, | ||
84 | .top = 0, | ||
85 | .width = 352, | ||
86 | .height = 288, | ||
87 | }, | ||
88 | .defrect = { | ||
89 | .left = 0, | ||
90 | .top = 0, | ||
91 | .width = 352, | ||
92 | .height = 288, | ||
93 | }, | ||
94 | }, | ||
95 | .set_crop = &tas5110d_set_crop, | ||
96 | .pix_format = { | ||
97 | .width = 352, | ||
98 | .height = 288, | ||
99 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
100 | .priv = 8, | ||
101 | }, | ||
102 | .set_pix_format = &tas5110d_set_pix_format | ||
103 | }; | ||
104 | |||
105 | |||
106 | int sn9c102_probe_tas5110d(struct sn9c102_device* cam) | ||
107 | { | ||
108 | const struct usb_device_id tas5110d_id_table[] = { | ||
109 | { USB_DEVICE(0x0c45, 0x6007), }, | ||
110 | { } | ||
111 | }; | ||
112 | |||
113 | if (!sn9c102_match_id(cam, tas5110d_id_table)) | ||
114 | return -ENODEV; | ||
115 | |||
116 | sn9c102_attach_sensor(cam, &tas5110d); | ||
117 | |||
118 | return 0; | ||
119 | } | ||
diff --git a/drivers/media/usb/sn9c102/sn9c102_tas5130d1b.c b/drivers/media/usb/sn9c102/sn9c102_tas5130d1b.c new file mode 100644 index 000000000000..a30bbc4389f5 --- /dev/null +++ b/drivers/media/usb/sn9c102/sn9c102_tas5130d1b.c | |||
@@ -0,0 +1,165 @@ | |||
1 | /*************************************************************************** | ||
2 | * Plug-in for TAS5130D1B image sensor connected to the SN9C1xx PC Camera * | ||
3 | * Controllers * | ||
4 | * * | ||
5 | * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> * | ||
6 | * * | ||
7 | * This program is free software; you can redistribute it and/or modify * | ||
8 | * it under the terms of the GNU General Public License as published by * | ||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||
10 | * (at your option) any later version. * | ||
11 | * * | ||
12 | * This program is distributed in the hope that it will be useful, * | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
15 | * GNU General Public License for more details. * | ||
16 | * * | ||
17 | * You should have received a copy of the GNU General Public License * | ||
18 | * along with this program; if not, write to the Free Software * | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * | ||
20 | ***************************************************************************/ | ||
21 | |||
22 | #include "sn9c102_sensor.h" | ||
23 | #include "sn9c102_devtable.h" | ||
24 | |||
25 | |||
26 | static int tas5130d1b_init(struct sn9c102_device* cam) | ||
27 | { | ||
28 | int err; | ||
29 | |||
30 | err = sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x20, 0x17}, | ||
31 | {0x04, 0x01}, {0x01, 0x10}, | ||
32 | {0x00, 0x11}, {0x00, 0x14}, | ||
33 | {0x60, 0x17}, {0x07, 0x18}); | ||
34 | |||
35 | return err; | ||
36 | } | ||
37 | |||
38 | |||
39 | static int tas5130d1b_set_ctrl(struct sn9c102_device* cam, | ||
40 | const struct v4l2_control* ctrl) | ||
41 | { | ||
42 | int err = 0; | ||
43 | |||
44 | switch (ctrl->id) { | ||
45 | case V4L2_CID_GAIN: | ||
46 | err += sn9c102_i2c_write(cam, 0x20, 0xf6 - ctrl->value); | ||
47 | break; | ||
48 | case V4L2_CID_EXPOSURE: | ||
49 | err += sn9c102_i2c_write(cam, 0x40, 0x47 - ctrl->value); | ||
50 | break; | ||
51 | default: | ||
52 | return -EINVAL; | ||
53 | } | ||
54 | |||
55 | return err ? -EIO : 0; | ||
56 | } | ||
57 | |||
58 | |||
59 | static int tas5130d1b_set_crop(struct sn9c102_device* cam, | ||
60 | const struct v4l2_rect* rect) | ||
61 | { | ||
62 | struct sn9c102_sensor* s = sn9c102_get_sensor(cam); | ||
63 | u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 104, | ||
64 | v_start = (u8)(rect->top - s->cropcap.bounds.top) + 12; | ||
65 | int err = 0; | ||
66 | |||
67 | err += sn9c102_write_reg(cam, h_start, 0x12); | ||
68 | err += sn9c102_write_reg(cam, v_start, 0x13); | ||
69 | |||
70 | /* Do NOT change! */ | ||
71 | err += sn9c102_write_reg(cam, 0x1f, 0x1a); | ||
72 | err += sn9c102_write_reg(cam, 0x1a, 0x1b); | ||
73 | err += sn9c102_write_reg(cam, sn9c102_pread_reg(cam, 0x19), 0x19); | ||
74 | |||
75 | return err; | ||
76 | } | ||
77 | |||
78 | |||
79 | static int tas5130d1b_set_pix_format(struct sn9c102_device* cam, | ||
80 | const struct v4l2_pix_format* pix) | ||
81 | { | ||
82 | int err = 0; | ||
83 | |||
84 | if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) | ||
85 | err += sn9c102_write_reg(cam, 0x63, 0x19); | ||
86 | else | ||
87 | err += sn9c102_write_reg(cam, 0xf3, 0x19); | ||
88 | |||
89 | return err; | ||
90 | } | ||
91 | |||
92 | |||
93 | static const struct sn9c102_sensor tas5130d1b = { | ||
94 | .name = "TAS5130D1B", | ||
95 | .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>", | ||
96 | .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102, | ||
97 | .sysfs_ops = SN9C102_I2C_WRITE, | ||
98 | .frequency = SN9C102_I2C_100KHZ, | ||
99 | .interface = SN9C102_I2C_3WIRES, | ||
100 | .init = &tas5130d1b_init, | ||
101 | .qctrl = { | ||
102 | { | ||
103 | .id = V4L2_CID_GAIN, | ||
104 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
105 | .name = "global gain", | ||
106 | .minimum = 0x00, | ||
107 | .maximum = 0xf6, | ||
108 | .step = 0x02, | ||
109 | .default_value = 0x00, | ||
110 | .flags = 0, | ||
111 | }, | ||
112 | { | ||
113 | .id = V4L2_CID_EXPOSURE, | ||
114 | .type = V4L2_CTRL_TYPE_INTEGER, | ||
115 | .name = "exposure", | ||
116 | .minimum = 0x00, | ||
117 | .maximum = 0x47, | ||
118 | .step = 0x01, | ||
119 | .default_value = 0x00, | ||
120 | .flags = 0, | ||
121 | }, | ||
122 | }, | ||
123 | .set_ctrl = &tas5130d1b_set_ctrl, | ||
124 | .cropcap = { | ||
125 | .bounds = { | ||
126 | .left = 0, | ||
127 | .top = 0, | ||
128 | .width = 640, | ||
129 | .height = 480, | ||
130 | }, | ||
131 | .defrect = { | ||
132 | .left = 0, | ||
133 | .top = 0, | ||
134 | .width = 640, | ||
135 | .height = 480, | ||
136 | }, | ||
137 | }, | ||
138 | .set_crop = &tas5130d1b_set_crop, | ||
139 | .pix_format = { | ||
140 | .width = 640, | ||
141 | .height = 480, | ||
142 | .pixelformat = V4L2_PIX_FMT_SBGGR8, | ||
143 | .priv = 8, | ||
144 | }, | ||
145 | .set_pix_format = &tas5130d1b_set_pix_format | ||
146 | }; | ||
147 | |||
148 | |||
149 | int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam) | ||
150 | { | ||
151 | const struct usb_device_id tas5130d1b_id_table[] = { | ||
152 | { USB_DEVICE(0x0c45, 0x6024), }, | ||
153 | { USB_DEVICE(0x0c45, 0x6025), }, | ||
154 | { USB_DEVICE(0x0c45, 0x60aa), }, | ||
155 | { } | ||
156 | }; | ||
157 | |||
158 | /* Sensor detection is based on USB pid/vid */ | ||
159 | if (!sn9c102_match_id(cam, tas5130d1b_id_table)) | ||
160 | return -ENODEV; | ||
161 | |||
162 | sn9c102_attach_sensor(cam, &tas5130d1b); | ||
163 | |||
164 | return 0; | ||
165 | } | ||