aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video
diff options
context:
space:
mode:
authorJaime Velasco Juan <jsagarribay@gmail.com>2008-01-12 04:48:14 -0500
committerMauro Carvalho Chehab <mchehab@infradead.org>2008-01-25 16:04:54 -0500
commitec16dae5453eafd1586f35c4ec1ef854e5a808e0 (patch)
tree3811349160986ec7da781785de57cf23cf2192bb /drivers/media/video
parentb6667e5fb8c478e65768fdb18fa6576ac1634068 (diff)
V4L/DVB (7019): V4L: add support for Syntek DC1125 webcams
This driver supports cameras with USB ID 174f:a311 or 05e1:0501, and the ov965x sensors. These devices are found in some Asus laptops and probably somewhere else. It is based on the stk11xx driver written by Nicolas Vivien Signed-off-by: Jaime Velasco Juan <jsagarribay@gmail.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Diffstat (limited to 'drivers/media/video')
-rw-r--r--drivers/media/video/Kconfig13
-rw-r--r--drivers/media/video/Makefile3
-rw-r--r--drivers/media/video/stk-sensor.c578
-rw-r--r--drivers/media/video/stk-webcam.c1465
-rw-r--r--drivers/media/video/stk-webcam.h138
5 files changed, 2197 insertions, 0 deletions
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index 7431f6da5cae..a2e8987a6195 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -821,6 +821,19 @@ config USB_ZR364XX
821 To compile this driver as a module, choose M here: the 821 To compile this driver as a module, choose M here: the
822 module will be called zr364xx. 822 module will be called zr364xx.
823 823
824config USB_STKWEBCAM
825 tristate "USB Syntek DC1125 Camera support"
826 depends on VIDEO_V4L2 && EXPERIMENTAL
827 ---help---
828 Say Y here if you want to use this type of camera.
829 Supported devices are typically found in some Asus laptops,
830 with USB id 174f:a311 and 05e1:0501. Other Syntek cameras
831 may be supported by the stk11xx driver, from which this is
832 derived, see http://stk11xx.sourceforge.net
833
834 To compile this driver as a module, choose M here: the
835 module will be called stkwebcam.
836
824endif # V4L_USB_DRIVERS 837endif # V4L_USB_DRIVERS
825 838
826endif # VIDEO_CAPTURE_DRIVERS 839endif # VIDEO_CAPTURE_DRIVERS
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile
index 74a2a1c45fe3..28ddd146c1c5 100644
--- a/drivers/media/video/Makefile
+++ b/drivers/media/video/Makefile
@@ -8,6 +8,8 @@ tuner-objs := tuner-core.o tuner-types.o
8 8
9msp3400-objs := msp3400-driver.o msp3400-kthreads.o 9msp3400-objs := msp3400-driver.o msp3400-kthreads.o
10 10
11stkwebcam-objs := stk-webcam.o stk-sensor.o
12
11obj-$(CONFIG_VIDEO_DEV) += videodev.o v4l2-common.o compat_ioctl32.o \ 13obj-$(CONFIG_VIDEO_DEV) += videodev.o v4l2-common.o compat_ioctl32.o \
12 v4l2-int-device.o 14 v4l2-int-device.o
13 15
@@ -116,6 +118,7 @@ obj-$(CONFIG_USB_SE401) += se401.o
116obj-$(CONFIG_USB_STV680) += stv680.o 118obj-$(CONFIG_USB_STV680) += stv680.o
117obj-$(CONFIG_USB_W9968CF) += w9968cf.o 119obj-$(CONFIG_USB_W9968CF) += w9968cf.o
118obj-$(CONFIG_USB_ZR364XX) += zr364xx.o 120obj-$(CONFIG_USB_ZR364XX) += zr364xx.o
121obj-$(CONFIG_USB_STKWEBCAM) += stkwebcam.o
119 122
120obj-$(CONFIG_USB_SN9C102) += sn9c102/ 123obj-$(CONFIG_USB_SN9C102) += sn9c102/
121obj-$(CONFIG_USB_ET61X251) += et61x251/ 124obj-$(CONFIG_USB_ET61X251) += et61x251/
diff --git a/drivers/media/video/stk-sensor.c b/drivers/media/video/stk-sensor.c
new file mode 100644
index 000000000000..4a9a0b62efa3
--- /dev/null
+++ b/drivers/media/video/stk-sensor.c
@@ -0,0 +1,578 @@
1/* stk-sensor.c: Driver for ov96xx sensor (used in some Syntek webcams)
2 *
3 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
4 *
5 * Some parts derived from ov7670.c:
6 * Copyright 2006 One Laptop Per Child Association, Inc. Written
7 * by Jonathan Corbet with substantial inspiration from Mark
8 * McClelland's ovcamchip code.
9 *
10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
11 *
12 * This file may be distributed under the terms of the GNU General
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28/* Controlling the sensor via the STK1125 vendor specific control interface:
29 * The camera uses an OmniVision sensor and the stk1125 provides an
30 * SCCB(i2c)-USB bridge which let us program the sensor.
31 * In my case the sensor id is 0x9652, it can be read from sensor's register
32 * 0x0A and 0x0B as follows:
33 * - read register #R:
34 * output #R to index 0x0208
35 * output 0x0070 to index 0x0200
36 * input 1 byte from index 0x0201 (some kind of status register)
37 * until its value is 0x01
38 * input 1 byte from index 0x0209. This is the value of #R
39 * - write value V to register #R
40 * output #R to index 0x0204
41 * output V to index 0x0205
42 * output 0x0005 to index 0x0200
43 * input 1 byte from index 0x0201 until its value becomes 0x04
44 */
45
46/* It seems the i2c bus is controlled with these registers */
47
48#include "stk-webcam.h"
49
50#define STK_IIC_BASE (0x0200)
51# define STK_IIC_OP (STK_IIC_BASE)
52# define STK_IIC_OP_TX (0x05)
53# define STK_IIC_OP_RX (0x70)
54# define STK_IIC_STAT (STK_IIC_BASE+1)
55# define STK_IIC_STAT_TX_OK (0x04)
56# define STK_IIC_STAT_RX_OK (0x01)
57/* I don't know what does this register.
58 * when it is 0x00 or 0x01, we cannot talk to the sensor,
59 * other values work */
60# define STK_IIC_ENABLE (STK_IIC_BASE+2)
61# define STK_IIC_ENABLE_NO (0x00)
62/* This is what the driver writes in windows */
63# define STK_IIC_ENABLE_YES (0x1e)
64/*
65 * Address of the slave. Seems like the binary driver look for the
66 * sensor in multiple places, attempting a reset sequence.
67 * We only know about the ov9650
68 */
69# define STK_IIC_ADDR (STK_IIC_BASE+3)
70# define STK_IIC_TX_INDEX (STK_IIC_BASE+4)
71# define STK_IIC_TX_VALUE (STK_IIC_BASE+5)
72# define STK_IIC_RX_INDEX (STK_IIC_BASE+8)
73# define STK_IIC_RX_VALUE (STK_IIC_BASE+9)
74
75#define MAX_RETRIES (50)
76
77#define SENSOR_ADDRESS (0x60)
78
79/* From ov7670.c (These registers aren't fully accurate) */
80
81/* Registers */
82#define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
83#define REG_BLUE 0x01 /* blue gain */
84#define REG_RED 0x02 /* red gain */
85#define REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
86#define REG_COM1 0x04 /* Control 1 */
87#define COM1_CCIR656 0x40 /* CCIR656 enable */
88#define COM1_QFMT 0x20 /* QVGA/QCIF format */
89#define COM1_SKIP_0 0x00 /* Do not skip any row */
90#define COM1_SKIP_2 0x04 /* Skip 2 rows of 4 */
91#define COM1_SKIP_3 0x08 /* Skip 3 rows of 4 */
92#define REG_BAVE 0x05 /* U/B Average level */
93#define REG_GbAVE 0x06 /* Y/Gb Average level */
94#define REG_AECHH 0x07 /* AEC MS 5 bits */
95#define REG_RAVE 0x08 /* V/R Average level */
96#define REG_COM2 0x09 /* Control 2 */
97#define COM2_SSLEEP 0x10 /* Soft sleep mode */
98#define REG_PID 0x0a /* Product ID MSB */
99#define REG_VER 0x0b /* Product ID LSB */
100#define REG_COM3 0x0c /* Control 3 */
101#define COM3_SWAP 0x40 /* Byte swap */
102#define COM3_SCALEEN 0x08 /* Enable scaling */
103#define COM3_DCWEN 0x04 /* Enable downsamp/crop/window */
104#define REG_COM4 0x0d /* Control 4 */
105#define REG_COM5 0x0e /* All "reserved" */
106#define REG_COM6 0x0f /* Control 6 */
107#define REG_AECH 0x10 /* More bits of AEC value */
108#define REG_CLKRC 0x11 /* Clock control */
109#define CLK_PLL 0x80 /* Enable internal PLL */
110#define CLK_EXT 0x40 /* Use external clock directly */
111#define CLK_SCALE 0x3f /* Mask for internal clock scale */
112#define REG_COM7 0x12 /* Control 7 */
113#define COM7_RESET 0x80 /* Register reset */
114#define COM7_FMT_MASK 0x38
115#define COM7_FMT_SXGA 0x00
116#define COM7_FMT_VGA 0x40
117#define COM7_FMT_CIF 0x20 /* CIF format */
118#define COM7_FMT_QVGA 0x10 /* QVGA format */
119#define COM7_FMT_QCIF 0x08 /* QCIF format */
120#define COM7_RGB 0x04 /* bits 0 and 2 - RGB format */
121#define COM7_YUV 0x00 /* YUV */
122#define COM7_BAYER 0x01 /* Bayer format */
123#define COM7_PBAYER 0x05 /* "Processed bayer" */
124#define REG_COM8 0x13 /* Control 8 */
125#define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
126#define COM8_AECSTEP 0x40 /* Unlimited AEC step size */
127#define COM8_BFILT 0x20 /* Band filter enable */
128#define COM8_AGC 0x04 /* Auto gain enable */
129#define COM8_AWB 0x02 /* White balance enable */
130#define COM8_AEC 0x01 /* Auto exposure enable */
131#define REG_COM9 0x14 /* Control 9 - gain ceiling */
132#define REG_COM10 0x15 /* Control 10 */
133#define COM10_HSYNC 0x40 /* HSYNC instead of HREF */
134#define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */
135#define COM10_HREF_REV 0x08 /* Reverse HREF */
136#define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */
137#define COM10_VS_NEG 0x02 /* VSYNC negative */
138#define COM10_HS_NEG 0x01 /* HSYNC negative */
139#define REG_HSTART 0x17 /* Horiz start high bits */
140#define REG_HSTOP 0x18 /* Horiz stop high bits */
141#define REG_VSTART 0x19 /* Vert start high bits */
142#define REG_VSTOP 0x1a /* Vert stop high bits */
143#define REG_PSHFT 0x1b /* Pixel delay after HREF */
144#define REG_MIDH 0x1c /* Manuf. ID high */
145#define REG_MIDL 0x1d /* Manuf. ID low */
146#define REG_MVFP 0x1e /* Mirror / vflip */
147#define MVFP_MIRROR 0x20 /* Mirror image */
148#define MVFP_FLIP 0x10 /* Vertical flip */
149
150#define REG_AEW 0x24 /* AGC upper limit */
151#define REG_AEB 0x25 /* AGC lower limit */
152#define REG_VPT 0x26 /* AGC/AEC fast mode op region */
153#define REG_ADVFL 0x2d /* Insert dummy lines (LSB) */
154#define REG_ADVFH 0x2e /* Insert dummy lines (MSB) */
155#define REG_HSYST 0x30 /* HSYNC rising edge delay */
156#define REG_HSYEN 0x31 /* HSYNC falling edge delay */
157#define REG_HREF 0x32 /* HREF pieces */
158#define REG_TSLB 0x3a /* lots of stuff */
159#define TSLB_YLAST 0x04 /* UYVY or VYUY - see com13 */
160#define TSLB_BYTEORD 0x08 /* swap bytes in 16bit mode? */
161#define REG_COM11 0x3b /* Control 11 */
162#define COM11_NIGHT 0x80 /* NIght mode enable */
163#define COM11_NMFR 0x60 /* Two bit NM frame rate */
164#define COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
165#define COM11_50HZ 0x08 /* Manual 50Hz select */
166#define COM11_EXP 0x02
167#define REG_COM12 0x3c /* Control 12 */
168#define COM12_HREF 0x80 /* HREF always */
169#define REG_COM13 0x3d /* Control 13 */
170#define COM13_GAMMA 0x80 /* Gamma enable */
171#define COM13_UVSAT 0x40 /* UV saturation auto adjustment */
172#define COM13_CMATRIX 0x10 /* Enable color matrix for RGB or YUV */
173#define COM13_UVSWAP 0x01 /* V before U - w/TSLB */
174#define REG_COM14 0x3e /* Control 14 */
175#define COM14_DCWEN 0x10 /* DCW/PCLK-scale enable */
176#define REG_EDGE 0x3f /* Edge enhancement factor */
177#define REG_COM15 0x40 /* Control 15 */
178#define COM15_R10F0 0x00 /* Data range 10 to F0 */
179#define COM15_R01FE 0x80 /* 01 to FE */
180#define COM15_R00FF 0xc0 /* 00 to FF */
181#define COM15_RGB565 0x10 /* RGB565 output */
182#define COM15_RGBFIXME 0x20 /* FIXME */
183#define COM15_RGB555 0x30 /* RGB555 output */
184#define REG_COM16 0x41 /* Control 16 */
185#define COM16_AWBGAIN 0x08 /* AWB gain enable */
186#define REG_COM17 0x42 /* Control 17 */
187#define COM17_AECWIN 0xc0 /* AEC window - must match COM4 */
188#define COM17_CBAR 0x08 /* DSP Color bar */
189
190/*
191 * This matrix defines how the colors are generated, must be
192 * tweaked to adjust hue and saturation.
193 *
194 * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
195 *
196 * They are nine-bit signed quantities, with the sign bit
197 * stored in 0x58. Sign for v-red is bit 0, and up from there.
198 */
199#define REG_CMATRIX_BASE 0x4f
200#define CMATRIX_LEN 6
201#define REG_CMATRIX_SIGN 0x58
202
203
204#define REG_BRIGHT 0x55 /* Brightness */
205#define REG_CONTRAS 0x56 /* Contrast control */
206
207#define REG_GFIX 0x69 /* Fix gain control */
208
209#define REG_RGB444 0x8c /* RGB 444 control */
210#define R444_ENABLE 0x02 /* Turn on RGB444, overrides 5x5 */
211#define R444_RGBX 0x01 /* Empty nibble at end */
212
213#define REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
214#define REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
215
216#define REG_BD50MAX 0xa5 /* 50hz banding step limit */
217#define REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
218#define REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
219#define REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
220#define REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
221#define REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
222#define REG_BD60MAX 0xab /* 60hz banding step limit */
223
224
225
226
227/* Returns 0 if OK */
228int stk_sensor_outb(struct stk_camera *dev, u8 reg, u8 val)
229{
230 int i = 0;
231 int tmpval = 0;
232
233 if (stk_camera_write_reg(dev, STK_IIC_TX_INDEX, reg))
234 return 1;
235 if (stk_camera_write_reg(dev, STK_IIC_TX_VALUE, val))
236 return 1;
237 if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_TX))
238 return 1;
239 do {
240 if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
241 return 1;
242 i++;
243 } while (tmpval == 0 && i < MAX_RETRIES);
244 if (tmpval != STK_IIC_STAT_TX_OK) {
245 if (tmpval)
246 STK_ERROR("stk_sensor_outb failed, status=0x%02x\n",
247 tmpval);
248 return 1;
249 } else
250 return 0;
251}
252
253int stk_sensor_inb(struct stk_camera *dev, u8 reg, u8 *val)
254{
255 int i = 0;
256 int tmpval = 0;
257
258 if (stk_camera_write_reg(dev, STK_IIC_RX_INDEX, reg))
259 return 1;
260 if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_RX))
261 return 1;
262 do {
263 if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
264 return 1;
265 i++;
266 } while (tmpval == 0 && i < MAX_RETRIES);
267 if (tmpval != STK_IIC_STAT_RX_OK) {
268 if (tmpval)
269 STK_ERROR("stk_sensor_inb failed, status=0x%02x\n",
270 tmpval);
271 return 1;
272 }
273
274 if (stk_camera_read_reg(dev, STK_IIC_RX_VALUE, &tmpval))
275 return 1;
276
277 *val = (u8) tmpval;
278 return 0;
279}
280
281static int stk_sensor_write_regvals(struct stk_camera *dev,
282 struct regval *rv)
283{
284 int ret;
285 if (rv == NULL)
286 return 0;
287 while (rv->reg != 0xff || rv->val != 0xff) {
288 ret = stk_sensor_outb(dev, rv->reg, rv->val);
289 if (ret != 0)
290 return ret;
291 rv++;
292 }
293 return 0;
294}
295
296int stk_sensor_sleep(struct stk_camera *dev)
297{
298 u8 tmp;
299 return stk_sensor_inb(dev, REG_COM2, &tmp)
300 || stk_sensor_outb(dev, REG_COM2, tmp|COM2_SSLEEP);
301}
302
303int stk_sensor_wakeup(struct stk_camera *dev)
304{
305 u8 tmp;
306 return stk_sensor_inb(dev, REG_COM2, &tmp)
307 || stk_sensor_outb(dev, REG_COM2, tmp&~COM2_SSLEEP);
308}
309
310static struct regval ov_initvals[] = {
311 {REG_CLKRC, CLK_PLL},
312 {REG_COM11, 0x01},
313 {0x6a, 0x7d},
314 {REG_AECH, 0x40},
315 {REG_GAIN, 0x00},
316 {REG_BLUE, 0x80},
317 {REG_RED, 0x80},
318 /* Do not enable fast AEC for now */
319 /*{REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},*/
320 {REG_COM8, COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},
321 {0x39, 0x50}, {0x38, 0x93},
322 {0x37, 0x00}, {0x35, 0x81},
323 {REG_COM5, 0x20},
324 {REG_COM1, 0x00},
325 {REG_COM3, 0x00},
326 {REG_COM4, 0x00},
327 {REG_PSHFT, 0x00},
328 {0x16, 0x07},
329 {0x33, 0xe2}, {0x34, 0xbf},
330 {REG_COM16, 0x00},
331 {0x96, 0x04},
332 /* Gamma curve values */
333/* { 0x7a, 0x20 }, { 0x7b, 0x10 },
334 { 0x7c, 0x1e }, { 0x7d, 0x35 },
335 { 0x7e, 0x5a }, { 0x7f, 0x69 },
336 { 0x80, 0x76 }, { 0x81, 0x80 },
337 { 0x82, 0x88 }, { 0x83, 0x8f },
338 { 0x84, 0x96 }, { 0x85, 0xa3 },
339 { 0x86, 0xaf }, { 0x87, 0xc4 },
340 { 0x88, 0xd7 }, { 0x89, 0xe8 },
341*/
342 {REG_GFIX, 0x40},
343 {0x8e, 0x00},
344 {REG_COM12, 0x73},
345 {0x8f, 0xdf}, {0x8b, 0x06},
346 {0x8c, 0x20},
347 {0x94, 0x88}, {0x95, 0x88},
348/* {REG_COM15, 0xc1}, TODO */
349 {0x29, 0x3f},
350 {REG_COM6, 0x42},
351 {REG_BD50MAX, 0x80},
352 {REG_HAECC6, 0xb8}, {REG_HAECC7, 0x92},
353 {REG_BD60MAX, 0x0a},
354 {0x90, 0x00}, {0x91, 0x00},
355 {REG_HAECC1, 0x00}, {REG_HAECC2, 0x00},
356 {REG_AEW, 0x68}, {REG_AEB, 0x5c},
357 {REG_VPT, 0xc3},
358 {REG_COM9, 0x2e},
359 {0x2a, 0x00}, {0x2b, 0x00},
360
361 {0xff, 0xff}, /* END MARKER */
362};
363
364/* Probe the I2C bus and initialise the sensor chip */
365int stk_sensor_init(struct stk_camera *dev)
366{
367 u8 idl = 0;
368 u8 idh = 0;
369
370 if (stk_camera_write_reg(dev, STK_IIC_ENABLE, STK_IIC_ENABLE_YES)
371 || stk_camera_write_reg(dev, STK_IIC_ADDR, SENSOR_ADDRESS)
372 || stk_sensor_outb(dev, REG_COM7, COM7_RESET)) {
373 STK_ERROR("Sensor resetting failed\n");
374 return -ENODEV;
375 }
376 msleep(10);
377 /* Read the manufacturer ID: ov = 0x7FA2 */
378 if (stk_sensor_inb(dev, REG_MIDH, &idh)
379 || stk_sensor_inb(dev, REG_MIDL, &idl)) {
380 STK_ERROR("Strange error reading sensor ID\n");
381 return -ENODEV;
382 }
383 if (idh != 0x7F || idl != 0xA2) {
384 STK_ERROR("Huh? you don't have a sensor from ovt\n");
385 return -ENODEV;
386 }
387 if (stk_sensor_inb(dev, REG_PID, &idh)
388 || stk_sensor_inb(dev, REG_VER, &idl)) {
389 STK_ERROR("Could not read sensor model\n");
390 return -ENODEV;
391 }
392 stk_sensor_write_regvals(dev, ov_initvals);
393 msleep(10);
394 STK_INFO("OmniVision sensor detected, id %02X%02X"
395 " at address %x\n", idh, idl, SENSOR_ADDRESS);
396 return 0;
397}
398
399/* V4L2_PIX_FMT_UYVY */
400static struct regval ov_fmt_uyvy[] = {
401 {REG_TSLB, TSLB_YLAST|0x08 },
402 { 0x4f, 0x80 }, /* "matrix coefficient 1" */
403 { 0x50, 0x80 }, /* "matrix coefficient 2" */
404 { 0x51, 0 }, /* vb */
405 { 0x52, 0x22 }, /* "matrix coefficient 4" */
406 { 0x53, 0x5e }, /* "matrix coefficient 5" */
407 { 0x54, 0x80 }, /* "matrix coefficient 6" */
408 {REG_COM13, COM13_UVSAT|COM13_CMATRIX},
409 {REG_COM15, COM15_R00FF },
410 {0xff, 0xff}, /* END MARKER */
411};
412
413/* V4L2_PIX_FMT_RGB565X rrrrrggg gggbbbbb */
414static struct regval ov_fmt_rgbr[] = {
415 { REG_RGB444, 0 }, /* No RGB444 please */
416 {REG_TSLB, 0x00},
417 { REG_COM1, 0x0 },
418 { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
419 { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
420 { 0x50, 0xb3 }, /* "matrix coefficient 2" */
421 { 0x51, 0 }, /* vb */
422 { 0x52, 0x3d }, /* "matrix coefficient 4" */
423 { 0x53, 0xa7 }, /* "matrix coefficient 5" */
424 { 0x54, 0xe4 }, /* "matrix coefficient 6" */
425 { REG_COM13, COM13_GAMMA },
426 { REG_COM15, COM15_RGB565|COM15_R00FF },
427 { 0xff, 0xff },
428};
429
430/* V4L2_PIX_FMT_RGB565 gggbbbbb rrrrrggg */
431static struct regval ov_fmt_rgbp[] = {
432 { REG_RGB444, 0 }, /* No RGB444 please */
433 {REG_TSLB, TSLB_BYTEORD },
434 { REG_COM1, 0x0 },
435 { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
436 { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
437 { 0x50, 0xb3 }, /* "matrix coefficient 2" */
438 { 0x51, 0 }, /* vb */
439 { 0x52, 0x3d }, /* "matrix coefficient 4" */
440 { 0x53, 0xa7 }, /* "matrix coefficient 5" */
441 { 0x54, 0xe4 }, /* "matrix coefficient 6" */
442 { REG_COM13, COM13_GAMMA },
443 { REG_COM15, COM15_RGB565|COM15_R00FF },
444 { 0xff, 0xff },
445};
446
447/* V4L2_PIX_FMT_SRGGB8 */
448static struct regval ov_fmt_bayer[] = {
449 /* This changes color order */
450 {REG_TSLB, 0x40}, /* BGGR */
451 /* {REG_TSLB, 0x08}, */ /* BGGR with vertical image flipping */
452 {REG_COM15, COM15_R00FF },
453 {0xff, 0xff}, /* END MARKER */
454};
455/*
456 * Store a set of start/stop values into the camera.
457 */
458static int stk_sensor_set_hw(struct stk_camera *dev,
459 int hstart, int hstop, int vstart, int vstop)
460{
461 int ret;
462 unsigned char v;
463/*
464 * Horizontal: 11 bits, top 8 live in hstart and hstop. Bottom 3 of
465 * hstart are in href[2:0], bottom 3 of hstop in href[5:3]. There is
466 * a mystery "edge offset" value in the top two bits of href.
467 */
468 ret = stk_sensor_outb(dev, REG_HSTART, (hstart >> 3) & 0xff);
469 ret += stk_sensor_outb(dev, REG_HSTOP, (hstop >> 3) & 0xff);
470 ret += stk_sensor_inb(dev, REG_HREF, &v);
471 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
472 msleep(10);
473 ret += stk_sensor_outb(dev, REG_HREF, v);
474/*
475 * Vertical: similar arrangement (note: this is different from ov7670.c)
476 */
477 ret += stk_sensor_outb(dev, REG_VSTART, (vstart >> 3) & 0xff);
478 ret += stk_sensor_outb(dev, REG_VSTOP, (vstop >> 3) & 0xff);
479 ret += stk_sensor_inb(dev, REG_VREF, &v);
480 v = (v & 0xc0) | ((vstop & 0x7) << 3) | (vstart & 0x7);
481 msleep(10);
482 ret += stk_sensor_outb(dev, REG_VREF, v);
483 return ret;
484}
485
486
487int stk_sensor_configure(struct stk_camera *dev)
488{
489 int com7;
490 /*
491 * We setup the sensor to output dummy lines in low-res modes,
492 * so we don't get absurdly hight framerates.
493 */
494 unsigned dummylines;
495 int flip;
496 struct regval *rv;
497
498 switch (dev->vsettings.mode) {
499 case MODE_QCIF: com7 = COM7_FMT_QCIF;
500 dummylines = 604;
501 break;
502 case MODE_QVGA: com7 = COM7_FMT_QVGA;
503 dummylines = 267;
504 break;
505 case MODE_CIF: com7 = COM7_FMT_CIF;
506 dummylines = 412;
507 break;
508 case MODE_VGA: com7 = COM7_FMT_VGA;
509 dummylines = 11;
510 break;
511 case MODE_SXGA: com7 = COM7_FMT_SXGA;
512 dummylines = 0;
513 break;
514 default: STK_ERROR("Unsupported mode %d\n", dev->vsettings.mode);
515 return -EFAULT;
516 }
517 switch (dev->vsettings.palette) {
518 case V4L2_PIX_FMT_UYVY:
519 com7 |= COM7_YUV;
520 rv = ov_fmt_uyvy;
521 break;
522 case V4L2_PIX_FMT_RGB565:
523 com7 |= COM7_RGB;
524 rv = ov_fmt_rgbp;
525 break;
526 case V4L2_PIX_FMT_RGB565X:
527 com7 |= COM7_RGB;
528 rv = ov_fmt_rgbr;
529 break;
530 case V4L2_PIX_FMT_SBGGR8:
531 com7 |= COM7_PBAYER;
532 rv = ov_fmt_bayer;
533 break;
534 default: STK_ERROR("Unsupported colorspace\n");
535 return -EFAULT;
536 }
537 /*FIXME sometimes the sensor go to a bad state
538 stk_sensor_write_regvals(dev, ov_initvals); */
539 stk_sensor_outb(dev, REG_COM7, com7);
540 msleep(50);
541 stk_sensor_write_regvals(dev, rv);
542 flip = (dev->vsettings.vflip?MVFP_FLIP:0)
543 | (dev->vsettings.hflip?MVFP_MIRROR:0);
544 stk_sensor_outb(dev, REG_MVFP, flip);
545 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8
546 && !dev->vsettings.vflip)
547 stk_sensor_outb(dev, REG_TSLB, 0x08);
548 stk_sensor_outb(dev, REG_ADVFH, dummylines >> 8);
549 stk_sensor_outb(dev, REG_ADVFL, dummylines & 0xff);
550 msleep(50);
551 switch (dev->vsettings.mode) {
552 case MODE_VGA:
553 if (stk_sensor_set_hw(dev, 302, 1582, 6, 486))
554 STK_ERROR("stk_sensor_set_hw failed (VGA)\n");
555 break;
556 case MODE_SXGA:
557 case MODE_CIF:
558 case MODE_QVGA:
559 case MODE_QCIF:
560 /*FIXME These settings seem ignored by the sensor
561 if (stk_sensor_set_hw(dev, 220, 1500, 10, 1034))
562 STK_ERROR("stk_sensor_set_hw failed (SXGA)\n");
563 */
564 break;
565 }
566 msleep(10);
567 return 0;
568}
569
570int stk_sensor_set_brightness(struct stk_camera *dev, int br)
571{
572 if (br < 0 || br > 0xff)
573 return -EINVAL;
574 stk_sensor_outb(dev, REG_AEB, max(0x00, br - 6));
575 stk_sensor_outb(dev, REG_AEW, min(0xff, br + 6));
576 return 0;
577}
578
diff --git a/drivers/media/video/stk-webcam.c b/drivers/media/video/stk-webcam.c
new file mode 100644
index 000000000000..f300eca3c473
--- /dev/null
+++ b/drivers/media/video/stk-webcam.c
@@ -0,0 +1,1465 @@
1/*
2 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
3 *
4 * Copyright (C) 2006 Nicolas VIVIEN
5 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
6 *
7 * Some parts are inspired from cafe_ccic.c
8 * Copyright 2006-2007 Jonathan Corbet
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/errno.h>
29#include <linux/slab.h>
30#include <linux/kref.h>
31
32#include <linux/usb.h>
33#include <linux/videodev2.h>
34#include <media/v4l2-common.h>
35#include <linux/vmalloc.h>
36
37#include "stk-webcam.h"
38
39
40static int hflip = 1;
41module_param(hflip, bool, 0444);
42MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
43
44static int vflip = 1;
45module_param(vflip, bool, 0444);
46MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
47
48static int debug;
49module_param(debug, int, 0444);
50MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
51
52MODULE_LICENSE("GPL");
53MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
54MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
55
56
57
58/* Some cameras have audio interfaces, we aren't interested in those */
59static struct usb_device_id stkwebcam_table[] = {
60 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
61 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
62 { }
63};
64MODULE_DEVICE_TABLE(usb, stkwebcam_table);
65
66void stk_camera_cleanup(struct kref *kref)
67{
68 struct stk_camera *dev = to_stk_camera(kref);
69
70 STK_INFO("Syntek USB2.0 Camera release resources"
71 " video device /dev/video%d\n", dev->vdev.minor);
72 video_unregister_device(&dev->vdev);
73 dev->vdev.priv = NULL;
74
75 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
76 STK_ERROR("We are leaking memory\n");
77 usb_put_intf(dev->interface);
78 kfree(dev);
79}
80
81
82/*
83 * Basic stuff
84 */
85int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
86{
87 struct usb_device *udev = dev->udev;
88 int ret;
89
90 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
91 0x01,
92 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
93 value,
94 index,
95 NULL,
96 0,
97 500);
98 if (ret < 0)
99 return ret;
100 else
101 return 0;
102}
103
104int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
105{
106 struct usb_device *udev = dev->udev;
107 int ret;
108
109 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
110 0x00,
111 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
112 0x00,
113 index,
114 (u8 *) value,
115 sizeof(u8),
116 500);
117 if (ret < 0)
118 return ret;
119 else
120 return 0;
121}
122
123static int stk_start_stream(struct stk_camera *dev)
124{
125 int value;
126 int i, ret;
127 int value_116, value_117;
128
129 if (!is_present(dev))
130 return -ENODEV;
131 if (!is_memallocd(dev) || !is_initialised(dev)) {
132 STK_ERROR("FIXME: Buffers are not allocated\n");
133 return -EFAULT;
134 }
135 ret = usb_set_interface(dev->udev, 0, 5);
136
137 if (ret < 0)
138 STK_ERROR("usb_set_interface failed !\n");
139 if (stk_sensor_wakeup(dev))
140 STK_ERROR("error awaking the sensor\n");
141
142 stk_camera_read_reg(dev, 0x0116, &value_116);
143 stk_camera_read_reg(dev, 0x0117, &value_117);
144
145 stk_camera_write_reg(dev, 0x0116, 0x0000);
146 stk_camera_write_reg(dev, 0x0117, 0x0000);
147
148 stk_camera_read_reg(dev, 0x0100, &value);
149 stk_camera_write_reg(dev, 0x0100, value | 0x80);
150
151 stk_camera_write_reg(dev, 0x0116, value_116);
152 stk_camera_write_reg(dev, 0x0117, value_117);
153 for (i = 0; i < MAX_ISO_BUFS; i++) {
154 if (dev->isobufs[i].urb) {
155 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
156 atomic_inc(&dev->urbs_used);
157 if (ret)
158 return ret;
159 }
160 }
161 set_streaming(dev);
162 return 0;
163}
164
165static int stk_stop_stream(struct stk_camera *dev)
166{
167 int value;
168 int i;
169 if (is_present(dev)) {
170 stk_camera_read_reg(dev, 0x0100, &value);
171 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
172 if (dev->isobufs != NULL) {
173 for (i = 0; i < MAX_ISO_BUFS; i++) {
174 if (dev->isobufs[i].urb)
175 usb_kill_urb(dev->isobufs[i].urb);
176 }
177 }
178 unset_streaming(dev);
179
180 if (usb_set_interface(dev->udev, 0, 0))
181 STK_ERROR("usb_set_interface failed !\n");
182 if (stk_sensor_sleep(dev))
183 STK_ERROR("error suspending the sensor\n");
184 }
185 return 0;
186}
187
188/*
189 * This seems to be the shortest init sequence we
190 * must do in order to find the sensor
191 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
192 * is also reset. Maybe powers down it?
193 * Rest of values don't make a difference
194 */
195
196static struct regval stk1125_initvals[] = {
197 /*TODO: What means this sequence? */
198 {0x0000, 0x24},
199 {0x0100, 0x21},
200 {0x0002, 0x68},
201 {0x0003, 0x80},
202 {0x0005, 0x00},
203 {0x0007, 0x03},
204 {0x000d, 0x00},
205 {0x000f, 0x02},
206 {0x0300, 0x12},
207 {0x0350, 0x41},
208 {0x0351, 0x00},
209 {0x0352, 0x00},
210 {0x0353, 0x00},
211 {0x0018, 0x10},
212 {0x0019, 0x00},
213 {0x001b, 0x0e},
214 {0x001c, 0x46},
215 {0x0300, 0x80},
216 {0x001a, 0x04},
217 {0x0110, 0x00},
218 {0x0111, 0x00},
219 {0x0112, 0x00},
220 {0x0113, 0x00},
221
222 {0xffff, 0xff},
223};
224
225
226static int stk_initialise(struct stk_camera *dev)
227{
228 struct regval *rv;
229 int ret;
230 if (!is_present(dev))
231 return -ENODEV;
232 if (is_initialised(dev))
233 return 0;
234 rv = stk1125_initvals;
235 while (rv->reg != 0xffff) {
236 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
237 if (ret)
238 return ret;
239 rv++;
240 }
241 if (stk_sensor_init(dev) == 0) {
242 set_initialised(dev);
243 return 0;
244 } else
245 return -1;
246}
247
248/* sysfs functions */
249/*FIXME cleanup this */
250
251static ssize_t show_brightness(struct device *class,
252 struct device_attribute *attr, char *buf)
253{
254 struct video_device *vdev = to_video_device(class);
255 struct stk_camera *dev = vdev_to_camera(vdev);
256
257 return sprintf(buf, "%X\n", dev->vsettings.brightness);
258}
259
260static ssize_t store_brightness(struct device *class,
261 struct device_attribute *attr, const char *buf, size_t count)
262{
263 char *endp;
264 unsigned long value;
265 int ret;
266
267 struct video_device *vdev = to_video_device(class);
268 struct stk_camera *dev = vdev_to_camera(vdev);
269
270 value = simple_strtoul(buf, &endp, 16);
271
272 dev->vsettings.brightness = (int) value;
273
274 ret = stk_sensor_set_brightness(dev, value >> 8);
275 if (ret)
276 return ret;
277 else
278 return count;
279}
280
281static ssize_t show_hflip(struct device *class,
282 struct device_attribute *attr, char *buf)
283{
284 struct video_device *vdev = to_video_device(class);
285 struct stk_camera *dev = vdev_to_camera(vdev);
286
287 return sprintf(buf, "%d\n", dev->vsettings.hflip);
288}
289
290static ssize_t store_hflip(struct device *class,
291 struct device_attribute *attr, const char *buf, size_t count)
292{
293 struct video_device *vdev = to_video_device(class);
294 struct stk_camera *dev = vdev_to_camera(vdev);
295
296 if (strncmp(buf, "1", 1) == 0)
297 dev->vsettings.hflip = 1;
298 else if (strncmp(buf, "0", 1) == 0)
299 dev->vsettings.hflip = 0;
300 else
301 return -EINVAL;
302
303 return strlen(buf);
304}
305
306static ssize_t show_vflip(struct device *class,
307 struct device_attribute *attr, char *buf)
308{
309 struct video_device *vdev = to_video_device(class);
310 struct stk_camera *dev = vdev_to_camera(vdev);
311
312 return sprintf(buf, "%d\n", dev->vsettings.vflip);
313}
314
315static ssize_t store_vflip(struct device *class,
316 struct device_attribute *attr, const char *buf, size_t count)
317{
318 struct video_device *vdev = to_video_device(class);
319 struct stk_camera *dev = vdev_to_camera(vdev);
320
321 if (strncmp(buf, "1", 1) == 0)
322 dev->vsettings.vflip = 1;
323 else if (strncmp(buf, "0", 1) == 0)
324 dev->vsettings.vflip = 0;
325 else
326 return -EINVAL;
327
328 return strlen(buf);
329}
330
331static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO,
332 show_brightness, store_brightness);
333static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
334static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
335
336static int stk_create_sysfs_files(struct video_device *vdev)
337{
338 int ret;
339
340 ret = video_device_create_file(vdev, &dev_attr_brightness);
341 ret += video_device_create_file(vdev, &dev_attr_hflip);
342 ret += video_device_create_file(vdev, &dev_attr_vflip);
343 return ret;
344}
345
346static void stk_remove_sysfs_files(struct video_device *vdev)
347{
348 video_device_remove_file(vdev, &dev_attr_brightness);
349 video_device_remove_file(vdev, &dev_attr_hflip);
350 video_device_remove_file(vdev, &dev_attr_vflip);
351}
352
353
354/* *********************************************** */
355/*
356 * This function is called as an URB transfert is complete (Isochronous pipe).
357 * So, the traitement is done in interrupt time, so it has be fast, not crash,
358 * and not stall. Neat.
359 */
360static void stk_isoc_handler(struct urb *urb)
361{
362 int i;
363 int ret;
364 int framelen;
365 unsigned long flags;
366
367 unsigned char *fill = NULL;
368 unsigned char *iso_buf = NULL;
369
370 struct stk_camera *dev;
371 struct stk_sio_buffer *fb;
372
373 dev = (struct stk_camera *) urb->context;
374
375 if (dev == NULL) {
376 STK_ERROR("isoc_handler called with NULL device !\n");
377 return;
378 }
379
380 if (urb->status == -ENOENT || urb->status == -ECONNRESET
381 || urb->status == -ESHUTDOWN) {
382 atomic_dec(&dev->urbs_used);
383 return;
384 }
385
386 spin_lock_irqsave(&dev->spinlock, flags);
387
388 if (urb->status != -EINPROGRESS && urb->status != 0) {
389 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
390 goto resubmit;
391 }
392
393 if (list_empty(&dev->sio_avail)) {
394 /*FIXME Stop streaming after a while */
395 (void) (printk_ratelimit() &&
396 STK_ERROR("isoc_handler without available buffer!\n"));
397 goto resubmit;
398 }
399 fb = list_first_entry(&dev->sio_avail,
400 struct stk_sio_buffer, list);
401 fill = fb->buffer + fb->v4lbuf.bytesused;
402
403 for (i = 0; i < urb->number_of_packets; i++) {
404 if (urb->iso_frame_desc[i].status != 0) {
405 if (urb->iso_frame_desc[i].status != -EXDEV)
406 STK_ERROR("Frame %d has error %d\n", i,
407 urb->iso_frame_desc[i].status);
408 continue;
409 }
410 framelen = urb->iso_frame_desc[i].actual_length;
411 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
412
413 if (framelen <= 4)
414 continue; /* no data */
415
416 /*
417 * we found something informational from there
418 * the isoc frames have to type of headers
419 * type1: 00 xx 00 00 or 20 xx 00 00
420 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
421 * xx is a sequencer which has never been seen over 0x3f
422 * imho data written down looks like bayer, i see similarities
423 * after every 640 bytes
424 */
425 if (*iso_buf & 0x80) {
426 framelen -= 8;
427 iso_buf += 8;
428 /* This marks a new frame */
429 if (fb->v4lbuf.bytesused != 0
430 && fb->v4lbuf.bytesused != dev->frame_size) {
431 (void) (printk_ratelimit() &&
432 STK_ERROR("frame %d, "
433 "bytesused=%d, skipping\n",
434 i, fb->v4lbuf.bytesused));
435 fb->v4lbuf.bytesused = 0;
436 fill = fb->buffer;
437 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
438 list_move_tail(dev->sio_avail.next,
439 &dev->sio_full);
440 wake_up(&dev->wait_frame);
441 if (list_empty(&dev->sio_avail)) {
442 (void) (printk_ratelimit() &&
443 STK_ERROR("No buffer available\n"));
444 goto resubmit;
445 }
446 fb = list_first_entry(&dev->sio_avail,
447 struct stk_sio_buffer, list);
448 fb->v4lbuf.bytesused = 0;
449 fill = fb->buffer;
450 }
451 } else {
452 framelen -= 4;
453 iso_buf += 4;
454 }
455
456 /* Our buffer is full !!! */
457 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
458 (void) (printk_ratelimit() &&
459 STK_ERROR("Frame buffer overflow, lost sync\n"));
460 /*FIXME Do something here? */
461 continue;
462 }
463 spin_unlock_irqrestore(&dev->spinlock, flags);
464 memcpy(fill, iso_buf, framelen);
465 spin_lock_irqsave(&dev->spinlock, flags);
466 fill += framelen;
467
468 /* New size of our buffer */
469 fb->v4lbuf.bytesused += framelen;
470 }
471
472resubmit:
473 spin_unlock_irqrestore(&dev->spinlock, flags);
474 urb->dev = dev->udev;
475 ret = usb_submit_urb(urb, GFP_ATOMIC);
476 if (ret != 0) {
477 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
478 ret);
479 }
480}
481
482/* -------------------------------------------- */
483
484static int stk_prepare_iso(struct stk_camera *dev)
485{
486 void *kbuf;
487 int i, j;
488 struct urb *urb;
489 struct usb_device *udev;
490
491 if (dev == NULL)
492 return -ENXIO;
493 udev = dev->udev;
494
495 if (dev->isobufs)
496 STK_ERROR("isobufs already allocated. Bad\n");
497 else
498 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
499 GFP_KERNEL);
500 if (dev->isobufs == NULL) {
501 STK_ERROR("Unable to allocate iso buffers\n");
502 return -ENOMEM;
503 }
504 for (i = 0; i < MAX_ISO_BUFS; i++) {
505 if (dev->isobufs[i].data == NULL) {
506 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
507 if (kbuf == NULL) {
508 STK_ERROR("Failed to allocate iso buffer %d\n",
509 i);
510 goto isobufs_out;
511 }
512 dev->isobufs[i].data = kbuf;
513 } else
514 STK_ERROR("isobuf data already allocated\n");
515 if (dev->isobufs[i].urb == NULL) {
516 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
517 if (urb == NULL) {
518 STK_ERROR("Failed to allocate URB %d\n", i);
519 goto isobufs_out;
520 }
521 dev->isobufs[i].urb = urb;
522 } else {
523 STK_ERROR("Killing URB\n");
524 usb_kill_urb(dev->isobufs[i].urb);
525 urb = dev->isobufs[i].urb;
526 }
527 urb->interval = 1;
528 urb->dev = udev;
529 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
530 urb->transfer_flags = URB_ISO_ASAP;
531 urb->transfer_buffer = dev->isobufs[i].data;
532 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
533 urb->complete = stk_isoc_handler;
534 urb->context = dev;
535 urb->start_frame = 0;
536 urb->number_of_packets = ISO_FRAMES_PER_DESC;
537
538 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
539 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
540 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
541 }
542 }
543 set_memallocd(dev);
544 return 0;
545
546isobufs_out:
547 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
548 kfree(dev->isobufs[i].data);
549 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
550 usb_free_urb(dev->isobufs[i].urb);
551 kfree(dev->isobufs);
552 dev->isobufs = NULL;
553 return -ENOMEM;
554}
555
556static void stk_clean_iso(struct stk_camera *dev)
557{
558 int i;
559
560 if (dev == NULL || dev->isobufs == NULL)
561 return;
562
563 for (i = 0; i < MAX_ISO_BUFS; i++) {
564 struct urb *urb;
565
566 urb = dev->isobufs[i].urb;
567 if (urb) {
568 if (atomic_read(&dev->urbs_used))
569 usb_kill_urb(urb);
570 usb_free_urb(urb);
571 }
572 kfree(dev->isobufs[i].data);
573 }
574 kfree(dev->isobufs);
575 dev->isobufs = NULL;
576 unset_memallocd(dev);
577}
578
579static int stk_setup_siobuf(struct stk_camera *dev, int index)
580{
581 struct stk_sio_buffer *buf = dev->sio_bufs + index;
582 INIT_LIST_HEAD(&buf->list);
583 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
584 buf->buffer = vmalloc_user(buf->v4lbuf.length);
585 if (buf->buffer == NULL)
586 return -ENOMEM;
587 buf->mapcount = 0;
588 buf->dev = dev;
589 buf->v4lbuf.index = index;
590 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
591 buf->v4lbuf.field = V4L2_FIELD_NONE;
592 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
593 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
594 return 0;
595}
596
597static int stk_free_sio_buffers(struct stk_camera *dev)
598{
599 int i;
600 int nbufs;
601 unsigned long flags;
602 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
603 return 0;
604 /*
605 * If any buffers are mapped, we cannot free them at all.
606 */
607 for (i = 0; i < dev->n_sbufs; i++) {
608 if (dev->sio_bufs[i].mapcount > 0)
609 return -EBUSY;
610 }
611 /*
612 * OK, let's do it.
613 */
614 spin_lock_irqsave(&dev->spinlock, flags);
615 INIT_LIST_HEAD(&dev->sio_avail);
616 INIT_LIST_HEAD(&dev->sio_full);
617 nbufs = dev->n_sbufs;
618 dev->n_sbufs = 0;
619 spin_unlock_irqrestore(&dev->spinlock, flags);
620 for (i = 0; i < nbufs; i++) {
621 if (dev->sio_bufs[i].buffer != NULL)
622 vfree(dev->sio_bufs[i].buffer);
623 }
624 kfree(dev->sio_bufs);
625 dev->sio_bufs = NULL;
626 return 0;
627}
628
629static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
630{
631 int i;
632 if (dev->sio_bufs != NULL)
633 STK_ERROR("sio_bufs already allocated\n");
634 else {
635 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
636 GFP_KERNEL);
637 if (dev->sio_bufs == NULL)
638 return -ENOMEM;
639 for (i = 0; i < n_sbufs; i++) {
640 if (stk_setup_siobuf(dev, i))
641 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
642 dev->n_sbufs = i+1;
643 }
644 }
645 return 0;
646}
647
648static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
649{
650 int err;
651 err = stk_prepare_iso(dev);
652 if (err) {
653 stk_clean_iso(dev);
654 return err;
655 }
656 err = stk_prepare_sio_buffers(dev, n_sbufs);
657 if (err) {
658 stk_free_sio_buffers(dev);
659 return err;
660 }
661 return 0;
662}
663
664static void stk_free_buffers(struct stk_camera *dev)
665{
666 stk_clean_iso(dev);
667 stk_free_sio_buffers(dev);
668}
669/* -------------------------------------------- */
670
671/* v4l file operations */
672
673static int v4l_stk_open(struct inode *inode, struct file *fp)
674{
675 struct stk_camera *dev;
676 struct video_device *vdev;
677
678 vdev = video_devdata(fp);
679 dev = vdev_to_camera(vdev);
680
681 if (dev == NULL || !is_present(dev))
682 return -ENXIO;
683 fp->private_data = vdev;
684 kref_get(&dev->kref);
685
686 return 0;
687}
688
689static int v4l_stk_release(struct inode *inode, struct file *fp)
690{
691 struct stk_camera *dev;
692 struct video_device *vdev;
693
694 vdev = video_devdata(fp);
695 if (vdev == NULL) {
696 STK_ERROR("v4l_release called w/o video devdata\n");
697 return -EFAULT;
698 }
699 dev = vdev_to_camera(vdev);
700 if (dev == NULL) {
701 STK_ERROR("v4l_release called on removed device\n");
702 return -ENODEV;
703 }
704
705 if (dev->owner != fp) {
706 kref_put(&dev->kref, stk_camera_cleanup);
707 return 0;
708 }
709
710 stk_stop_stream(dev);
711
712 stk_free_buffers(dev);
713
714 dev->owner = NULL;
715
716 kref_put(&dev->kref, stk_camera_cleanup);
717
718 return 0;
719}
720
721static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
722 size_t count, loff_t *f_pos)
723{
724 int i;
725 int ret;
726 unsigned long flags;
727 struct stk_camera *dev;
728 struct video_device *vdev;
729 struct stk_sio_buffer *sbuf;
730
731 vdev = video_devdata(fp);
732 if (vdev == NULL)
733 return -EFAULT;
734 dev = vdev_to_camera(vdev);
735
736 if (dev == NULL)
737 return -EIO;
738
739 if (!is_present(dev))
740 return -EIO;
741 if (dev->owner && dev->owner != fp)
742 return -EBUSY;
743 dev->owner = fp;
744 if (!is_streaming(dev)) {
745 if (stk_initialise(dev)
746 || stk_allocate_buffers(dev, 3)
747 || stk_start_stream(dev))
748 return -ENOMEM;
749 spin_lock_irqsave(&dev->spinlock, flags);
750 for (i = 0; i < dev->n_sbufs; i++) {
751 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
752 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
753 }
754 spin_unlock_irqrestore(&dev->spinlock, flags);
755 }
756 if (*f_pos == 0) {
757 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
758 return -EWOULDBLOCK;
759 ret = wait_event_interruptible(dev->wait_frame,
760 !list_empty(&dev->sio_full) || !is_present(dev));
761 if (ret)
762 return ret;
763 if (!is_present(dev))
764 return -EIO;
765 }
766 if (count + *f_pos > dev->frame_size)
767 count = dev->frame_size - *f_pos;
768 spin_lock_irqsave(&dev->spinlock, flags);
769 if (list_empty(&dev->sio_full)) {
770 spin_unlock_irqrestore(&dev->spinlock, flags);
771 STK_ERROR("BUG: No siobufs ready\n");
772 return 0;
773 }
774 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
775 spin_unlock_irqrestore(&dev->spinlock, flags);
776
777 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
778 return -EFAULT;
779
780 *f_pos += count;
781
782 if (*f_pos >= dev->frame_size) {
783 *f_pos = 0;
784 spin_lock_irqsave(&dev->spinlock, flags);
785 list_move_tail(&sbuf->list, &dev->sio_avail);
786 spin_unlock_irqrestore(&dev->spinlock, flags);
787 }
788 return count;
789}
790
791static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
792{
793 struct stk_camera *dev;
794 struct video_device *vdev;
795
796 vdev = video_devdata(fp);
797
798 if (vdev == NULL)
799 return -EFAULT;
800
801 dev = vdev_to_camera(vdev);
802 if (dev == NULL)
803 return -ENODEV;
804
805 poll_wait(fp, &dev->wait_frame, wait);
806
807 if (!is_present(dev))
808 return POLLERR;
809
810 if (!list_empty(&dev->sio_full))
811 return (POLLIN | POLLRDNORM);
812
813 return 0;
814}
815
816
817static void stk_v4l_vm_open(struct vm_area_struct *vma)
818{
819 struct stk_sio_buffer *sbuf = vma->vm_private_data;
820 sbuf->mapcount++;
821}
822static void stk_v4l_vm_close(struct vm_area_struct *vma)
823{
824 struct stk_sio_buffer *sbuf = vma->vm_private_data;
825 sbuf->mapcount--;
826 if (sbuf->mapcount == 0)
827 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
828}
829static struct vm_operations_struct stk_v4l_vm_ops = {
830 .open = stk_v4l_vm_open,
831 .close = stk_v4l_vm_close
832};
833
834static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
835{
836 unsigned int i;
837 int ret;
838 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
839 struct stk_camera *dev;
840 struct video_device *vdev;
841 struct stk_sio_buffer *sbuf = NULL;
842
843 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
844 return -EINVAL;
845
846 vdev = video_devdata(fp);
847 dev = vdev_to_camera(vdev);
848
849 for (i = 0; i < dev->n_sbufs; i++) {
850 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
851 sbuf = dev->sio_bufs + i;
852 break;
853 }
854 }
855 if (sbuf == NULL)
856 return -EINVAL;
857 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
858 if (ret)
859 return ret;
860 vma->vm_flags |= VM_DONTEXPAND;
861 vma->vm_private_data = sbuf;
862 vma->vm_ops = &stk_v4l_vm_ops;
863 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
864 stk_v4l_vm_open(vma);
865 return 0;
866}
867
868/* v4l ioctl handlers */
869
870static int stk_vidioc_querycap(struct file *filp,
871 void *priv, struct v4l2_capability *cap)
872{
873 strcpy(cap->driver, "stk");
874 strcpy(cap->card, "stk");
875 cap->version = DRIVER_VERSION_NUM;
876
877 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
878 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
879 return 0;
880}
881
882static int stk_vidioc_enum_input(struct file *filp,
883 void *priv, struct v4l2_input *input)
884{
885 if (input->index != 0)
886 return -EINVAL;
887
888 strcpy(input->name, "Syntek USB Camera");
889 input->type = V4L2_INPUT_TYPE_CAMERA;
890 return 0;
891}
892
893
894static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
895{
896 *i = 0;
897 return 0;
898}
899
900static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
901{
902 if (i != 0)
903 return -EINVAL;
904 else
905 return 0;
906}
907
908/* from vivi.c */
909static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
910{
911 return 0;
912}
913
914/* List of all V4Lv2 controls supported by the driver */
915static struct v4l2_queryctrl stk_controls[] = {
916 {
917 .id = V4L2_CID_BRIGHTNESS,
918 .type = V4L2_CTRL_TYPE_INTEGER,
919 .name = "Brightness",
920 .minimum = 0,
921 .maximum = 0xffff,
922 .step = 0x0100,
923 .default_value = 0x6000,
924 },
925 /*TODO: get more controls to work */
926};
927
928static int stk_vidioc_queryctrl(struct file *filp,
929 void *priv, struct v4l2_queryctrl *c)
930{
931 int i;
932 int nbr;
933 nbr = ARRAY_SIZE(stk_controls);
934
935 for (i = 0; i < nbr; i++) {
936 if (stk_controls[i].id == c->id) {
937 memcpy(c, &stk_controls[i],
938 sizeof(struct v4l2_queryctrl));
939 return 0;
940 }
941 }
942 return -EINVAL;
943}
944
945static int stk_vidioc_g_ctrl(struct file *filp,
946 void *priv, struct v4l2_control *c)
947{
948 struct stk_camera *dev = priv;
949 switch (c->id) {
950 case V4L2_CID_BRIGHTNESS:
951 c->value = dev->vsettings.brightness;
952 break;
953 default:
954 return -EINVAL;
955 }
956 return 0;
957}
958
959static int stk_vidioc_s_ctrl(struct file *filp,
960 void *priv, struct v4l2_control *c)
961{
962 struct stk_camera *dev = priv;
963 switch (c->id) {
964 case V4L2_CID_BRIGHTNESS:
965 dev->vsettings.brightness = c->value;
966 return stk_sensor_set_brightness(dev, c->value >> 8);
967 default:
968 return -EINVAL;
969 }
970 return 0;
971}
972
973
974static int stk_vidioc_enum_fmt_cap(struct file *filp,
975 void *priv, struct v4l2_fmtdesc *fmtd)
976{
977 fmtd->flags = 0;
978
979 switch (fmtd->index) {
980 case 0:
981 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
982 strcpy(fmtd->description, "r5g6b5");
983 break;
984 case 1:
985 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
986 strcpy(fmtd->description, "r5g6b5BE");
987 break;
988 case 2:
989 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
990 strcpy(fmtd->description, "yuv4:2:2");
991 break;
992 case 3:
993 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
994 strcpy(fmtd->description, "Raw bayer");
995 break;
996 default:
997 return -EINVAL;
998 }
999 return 0;
1000}
1001
1002static struct stk_size {
1003 unsigned w;
1004 unsigned h;
1005 enum stk_mode m;
1006} stk_sizes[] = {
1007 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1008 { .w = 640, .h = 480, .m = MODE_VGA, },
1009 { .w = 352, .h = 288, .m = MODE_CIF, },
1010 { .w = 320, .h = 240, .m = MODE_QVGA, },
1011 { .w = 176, .h = 144, .m = MODE_QCIF, },
1012};
1013
1014static int stk_vidioc_g_fmt_cap(struct file *filp,
1015 void *priv, struct v4l2_format *f)
1016{
1017 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1018 struct stk_camera *dev = priv;
1019 int i;
1020
1021 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1022 && stk_sizes[i].m != dev->vsettings.mode;
1023 i++);
1024 if (i == ARRAY_SIZE(stk_sizes)) {
1025 STK_ERROR("ERROR: mode invalid\n");
1026 return -EINVAL;
1027 }
1028 pix_format->width = stk_sizes[i].w;
1029 pix_format->height = stk_sizes[i].h;
1030 pix_format->field = V4L2_FIELD_NONE;
1031 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1032 pix_format->priv = 0;
1033 pix_format->pixelformat = dev->vsettings.palette;
1034 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1035 pix_format->bytesperline = pix_format->width;
1036 else
1037 pix_format->bytesperline = 2 * pix_format->width;
1038 pix_format->sizeimage = pix_format->bytesperline
1039 * pix_format->height;
1040 return 0;
1041}
1042
1043static int stk_vidioc_try_fmt_cap(struct file *filp,
1044 void *priv, struct v4l2_format *fmtd)
1045{
1046 int i;
1047 switch (fmtd->fmt.pix.pixelformat) {
1048 case V4L2_PIX_FMT_RGB565:
1049 case V4L2_PIX_FMT_RGB565X:
1050 case V4L2_PIX_FMT_UYVY:
1051 case V4L2_PIX_FMT_SBGGR8:
1052 break;
1053 default:
1054 return -EINVAL;
1055 }
1056 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1057 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1058 break;
1059 }
1060 if (i == ARRAY_SIZE(stk_sizes)
1061 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1062 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1063 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1064 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1065 fmtd->fmt.pix.priv = i - 1;
1066 } else {
1067 fmtd->fmt.pix.height = stk_sizes[i].h;
1068 fmtd->fmt.pix.width = stk_sizes[i].w;
1069 fmtd->fmt.pix.priv = i;
1070 }
1071
1072 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1073 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1074 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1075 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1076 else
1077 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1078 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1079 * fmtd->fmt.pix.height;
1080 return 0;
1081}
1082
1083static int stk_vidioc_s_fmt_cap(struct file *filp,
1084 void *priv, struct v4l2_format *fmtd)
1085{
1086 int ret;
1087 struct stk_camera *dev = priv;
1088
1089 if (dev == NULL)
1090 return -ENODEV;
1091 if (!is_present(dev))
1092 return -ENODEV;
1093 if (is_streaming(dev))
1094 return -EBUSY;
1095 if (dev->owner && dev->owner != filp)
1096 return -EBUSY;
1097 dev->owner = filp;
1098 ret = stk_vidioc_try_fmt_cap(filp, priv, fmtd);
1099 if (ret)
1100 return ret;
1101
1102 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1103 stk_free_buffers(dev);
1104 dev->frame_size = fmtd->fmt.pix.sizeimage;
1105 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1106
1107 stk_initialise(dev);
1108 /* This registers controls some timings, not sure of what. */
1109 stk_camera_write_reg(dev, 0x001b, 0x0e);
1110 if (dev->vsettings.mode == MODE_SXGA)
1111 stk_camera_write_reg(dev, 0x001c, 0x0e);
1112 else
1113 stk_camera_write_reg(dev, 0x001c, 0x46);
1114 /*
1115 * Registers 0x0115 0x0114 are the size of each line (bytes),
1116 * regs 0x0117 0x0116 are the heigth of the image.
1117 */
1118 stk_camera_write_reg(dev, 0x0115,
1119 (fmtd->fmt.pix.bytesperline >> 8) & 0xff);
1120 stk_camera_write_reg(dev, 0x0114,
1121 fmtd->fmt.pix.bytesperline & 0xff);
1122 stk_camera_write_reg(dev, 0x0117,
1123 (fmtd->fmt.pix.height >> 8) & 0xff);
1124 stk_camera_write_reg(dev, 0x0116,
1125 fmtd->fmt.pix.height & 0xff);
1126 return stk_sensor_configure(dev);
1127}
1128
1129static int stk_vidioc_reqbufs(struct file *filp,
1130 void *priv, struct v4l2_requestbuffers *rb)
1131{
1132 struct stk_camera *dev = priv;
1133
1134 if (dev == NULL)
1135 return -ENODEV;
1136 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1137 return -EINVAL;
1138 if (rb->memory != V4L2_MEMORY_MMAP)
1139 return -EINVAL;
1140 if (is_streaming(dev)
1141 || (dev->owner && dev->owner != filp))
1142 return -EBUSY;
1143 dev->owner = filp;
1144
1145 /*FIXME If they ask for zero, we must stop streaming and free */
1146 if (rb->count < 3)
1147 rb->count = 3;
1148 /* Arbitrary limit */
1149 else if (rb->count > 5)
1150 rb->count = 5;
1151
1152 stk_allocate_buffers(dev, rb->count);
1153 rb->count = dev->n_sbufs;
1154 return 0;
1155}
1156
1157static int stk_vidioc_querybuf(struct file *filp,
1158 void *priv, struct v4l2_buffer *buf)
1159{
1160 int index;
1161 struct stk_camera *dev = priv;
1162 struct stk_sio_buffer *sbuf;
1163
1164 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1165 return -EINVAL;
1166
1167 index = buf->index;
1168
1169 if (index < 0 || index >= dev->n_sbufs)
1170 return -EINVAL;
1171 sbuf = dev->sio_bufs + buf->index;
1172 *buf = sbuf->v4lbuf;
1173 return 0;
1174}
1175
1176static int stk_vidioc_qbuf(struct file *filp,
1177 void *priv, struct v4l2_buffer *buf)
1178{
1179 struct stk_camera *dev = priv;
1180 struct stk_sio_buffer *sbuf;
1181 unsigned long flags;
1182 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1183 return -EINVAL;
1184
1185 if (buf->memory != V4L2_MEMORY_MMAP)
1186 return -EINVAL;
1187
1188 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1189 return -EINVAL;
1190 sbuf = dev->sio_bufs + buf->index;
1191 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1192 return 0;
1193 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1194 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1195 spin_lock_irqsave(&dev->spinlock, flags);
1196 list_add_tail(&sbuf->list, &dev->sio_avail);
1197 *buf = sbuf->v4lbuf;
1198 spin_unlock_irqrestore(&dev->spinlock, flags);
1199 return 0;
1200}
1201
1202static int stk_vidioc_dqbuf(struct file *filp,
1203 void *priv, struct v4l2_buffer *buf)
1204{
1205 struct stk_camera *dev = priv;
1206 struct stk_sio_buffer *sbuf;
1207 unsigned long flags;
1208 int ret;
1209
1210 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1211 || !is_streaming(dev))
1212 return -EINVAL;
1213
1214 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1215 return -EWOULDBLOCK;
1216 ret = wait_event_interruptible(dev->wait_frame,
1217 !list_empty(&dev->sio_full) || !is_present(dev));
1218 if (ret)
1219 return ret;
1220 if (!is_present(dev))
1221 return -EIO;
1222
1223 spin_lock_irqsave(&dev->spinlock, flags);
1224 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1225 list_del_init(&sbuf->list);
1226 spin_unlock_irqrestore(&dev->spinlock, flags);
1227 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1228 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1229 sbuf->v4lbuf.sequence = ++dev->sequence;
1230 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1231
1232 *buf = sbuf->v4lbuf;
1233 return 0;
1234}
1235
1236static int stk_vidioc_streamon(struct file *filp,
1237 void *priv, enum v4l2_buf_type type)
1238{
1239 struct stk_camera *dev = priv;
1240 if (is_streaming(dev))
1241 return 0;
1242 if (dev->sio_bufs == NULL)
1243 return -EINVAL;
1244 dev->sequence = 0;
1245 return stk_start_stream(dev);
1246}
1247
1248static int stk_vidioc_streamoff(struct file *filp,
1249 void *priv, enum v4l2_buf_type type)
1250{
1251 struct stk_camera *dev = priv;
1252 unsigned long flags;
1253 int i;
1254 stk_stop_stream(dev);
1255 spin_lock_irqsave(&dev->spinlock, flags);
1256 INIT_LIST_HEAD(&dev->sio_avail);
1257 INIT_LIST_HEAD(&dev->sio_full);
1258 for (i = 0; i < dev->n_sbufs; i++) {
1259 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1260 dev->sio_bufs[i].v4lbuf.flags = 0;
1261 }
1262 spin_unlock_irqrestore(&dev->spinlock, flags);
1263 return 0;
1264}
1265
1266
1267static int stk_vidioc_g_parm(struct file *filp,
1268 void *priv, struct v4l2_streamparm *sp)
1269{
1270 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1271 return -EINVAL;
1272
1273 sp->parm.capture.capability = 0;
1274 sp->parm.capture.capturemode = 0;
1275 /*FIXME This is not correct */
1276 sp->parm.capture.timeperframe.numerator = 1;
1277 sp->parm.capture.timeperframe.denominator = 30;
1278 sp->parm.capture.readbuffers = 2;
1279 sp->parm.capture.extendedmode = 0;
1280 return 0;
1281}
1282
1283static struct file_operations v4l_stk_fops = {
1284 .owner = THIS_MODULE,
1285 .open = v4l_stk_open,
1286 .release = v4l_stk_release,
1287 .read = v4l_stk_read,
1288 .poll = v4l_stk_poll,
1289 .mmap = v4l_stk_mmap,
1290 .ioctl = video_ioctl2,
1291 .llseek = no_llseek
1292};
1293
1294static void stk_v4l_dev_release(struct video_device *vd)
1295{
1296}
1297
1298static struct video_device stk_v4l_data = {
1299 .name = "stkwebcam",
1300 .type = VFL_TYPE_GRABBER,
1301 .type2 = VID_TYPE_CAPTURE,
1302 .minor = -1,
1303 .tvnorms = V4L2_STD_UNKNOWN,
1304 .current_norm = V4L2_STD_UNKNOWN,
1305 .fops = &v4l_stk_fops,
1306 .release = stk_v4l_dev_release,
1307
1308 .vidioc_querycap = stk_vidioc_querycap,
1309 .vidioc_enum_fmt_cap = stk_vidioc_enum_fmt_cap,
1310 .vidioc_try_fmt_cap = stk_vidioc_try_fmt_cap,
1311 .vidioc_s_fmt_cap = stk_vidioc_s_fmt_cap,
1312 .vidioc_g_fmt_cap = stk_vidioc_g_fmt_cap,
1313 .vidioc_enum_input = stk_vidioc_enum_input,
1314 .vidioc_s_input = stk_vidioc_s_input,
1315 .vidioc_g_input = stk_vidioc_g_input,
1316 .vidioc_s_std = stk_vidioc_s_std,
1317 .vidioc_reqbufs = stk_vidioc_reqbufs,
1318 .vidioc_querybuf = stk_vidioc_querybuf,
1319 .vidioc_qbuf = stk_vidioc_qbuf,
1320 .vidioc_dqbuf = stk_vidioc_dqbuf,
1321 .vidioc_streamon = stk_vidioc_streamon,
1322 .vidioc_streamoff = stk_vidioc_streamoff,
1323 .vidioc_queryctrl = stk_vidioc_queryctrl,
1324 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1325 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1326 .vidioc_g_parm = stk_vidioc_g_parm,
1327};
1328
1329
1330static int stk_register_video_device(struct stk_camera *dev)
1331{
1332 int err;
1333
1334 dev->vdev = stk_v4l_data;
1335 dev->vdev.debug = debug;
1336 dev->vdev.dev = &dev->interface->dev;
1337 dev->vdev.priv = dev;
1338 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1339 if (err)
1340 STK_ERROR("v4l registration failed\n");
1341 else
1342 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1343 " /dev/video%d\n", dev->vdev.minor);
1344 return err;
1345}
1346
1347
1348/* USB Stuff */
1349
1350static int stk_camera_probe(struct usb_interface *interface,
1351 const struct usb_device_id *id)
1352{
1353 int i;
1354 int err;
1355
1356 struct stk_camera *dev = NULL;
1357 struct usb_device *udev = interface_to_usbdev(interface);
1358 struct usb_host_interface *iface_desc;
1359 struct usb_endpoint_descriptor *endpoint;
1360
1361 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1362 if (dev == NULL) {
1363 STK_ERROR("Out of memory !\n");
1364 return -ENOMEM;
1365 }
1366
1367 kref_init(&dev->kref);
1368 spin_lock_init(&dev->spinlock);
1369 init_waitqueue_head(&dev->wait_frame);
1370
1371 dev->udev = udev;
1372 dev->interface = interface;
1373 usb_get_intf(interface);
1374
1375 dev->vsettings.vflip = vflip;
1376 dev->vsettings.hflip = hflip;
1377 dev->n_sbufs = 0;
1378 set_present(dev);
1379
1380 /* Set up the endpoint information
1381 * use only the first isoc-in endpoint
1382 * for the current alternate setting */
1383 iface_desc = interface->cur_altsetting;
1384
1385 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1386 endpoint = &iface_desc->endpoint[i].desc;
1387
1388 if (!dev->isoc_ep
1389 && ((endpoint->bEndpointAddress
1390 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1391 && ((endpoint->bmAttributes
1392 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1393 /* we found an isoc in endpoint */
1394 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1395 break;
1396 }
1397 }
1398 if (!dev->isoc_ep) {
1399 STK_ERROR("Could not find isoc-in endpoint");
1400 kref_put(&dev->kref, stk_camera_cleanup);
1401 return -ENODEV;
1402 }
1403 dev->vsettings.brightness = 0x7fff;
1404 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1405 dev->vsettings.mode = MODE_VGA;
1406 dev->frame_size = 640*480*2;
1407
1408 INIT_LIST_HEAD(&dev->sio_avail);
1409 INIT_LIST_HEAD(&dev->sio_full);
1410
1411 usb_set_intfdata(interface, dev);
1412
1413 err = stk_register_video_device(dev);
1414 if (err) {
1415 kref_put(&dev->kref, stk_camera_cleanup);
1416 return err;
1417 }
1418
1419 stk_create_sysfs_files(&dev->vdev);
1420
1421 return 0;
1422}
1423
1424static void stk_camera_disconnect(struct usb_interface *interface)
1425{
1426 struct stk_camera *dev = usb_get_intfdata(interface);
1427
1428 usb_set_intfdata(interface, NULL);
1429 unset_present(dev);
1430
1431 wake_up_interruptible(&dev->wait_frame);
1432 stk_remove_sysfs_files(&dev->vdev);
1433
1434 kref_put(&dev->kref, stk_camera_cleanup);
1435}
1436
1437static struct usb_driver stk_camera_driver = {
1438 .name = "stkwebcam",
1439 .probe = stk_camera_probe,
1440 .disconnect = stk_camera_disconnect,
1441 .id_table = stkwebcam_table,
1442};
1443
1444
1445static int __init stk_camera_init(void)
1446{
1447 int result;
1448
1449 result = usb_register(&stk_camera_driver);
1450 if (result)
1451 STK_ERROR("usb_register failed ! Error number %d\n", result);
1452
1453
1454 return result;
1455}
1456
1457static void __exit stk_camera_exit(void)
1458{
1459 usb_deregister(&stk_camera_driver);
1460}
1461
1462module_init(stk_camera_init);
1463module_exit(stk_camera_exit);
1464
1465
diff --git a/drivers/media/video/stk-webcam.h b/drivers/media/video/stk-webcam.h
new file mode 100644
index 000000000000..7e989d1ac1e0
--- /dev/null
+++ b/drivers/media/video/stk-webcam.h
@@ -0,0 +1,138 @@
1/*
2 * stk-webcam.h : Driver for Syntek 1125 USB webcam controller
3 *
4 * Copyright (C) 2006 Nicolas VIVIEN
5 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
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 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#ifndef STKWEBCAM_H
23#define STKWEBCAM_H
24
25#include <linux/usb.h>
26#include <media/v4l2-common.h>
27
28#define DRIVER_VERSION "v0.0.1"
29#define DRIVER_VERSION_NUM 0x000001
30
31#define MAX_ISO_BUFS 3
32#define ISO_FRAMES_PER_DESC 16
33#define ISO_MAX_FRAME_SIZE 3 * 1024
34#define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE)
35
36
37#define PREFIX "stkwebcam: "
38#define STK_INFO(str, args...) printk(KERN_INFO PREFIX str, ##args)
39#define STK_ERROR(str, args...) printk(KERN_ERR PREFIX str, ##args)
40#define STK_WARNING(str, args...) printk(KERN_WARNING PREFIX str, ##args)
41
42struct stk_iso_buf {
43 void *data;
44 int length;
45 int read;
46 struct urb *urb;
47};
48
49/* Streaming IO buffers */
50struct stk_sio_buffer {
51 struct v4l2_buffer v4lbuf;
52 char *buffer;
53 int mapcount;
54 struct stk_camera *dev;
55 struct list_head list;
56};
57
58enum stk_mode {MODE_VGA, MODE_SXGA, MODE_CIF, MODE_QVGA, MODE_QCIF};
59
60struct stk_video {
61 enum stk_mode mode;
62 int brightness;
63 __u32 palette;
64 int hflip;
65 int vflip;
66};
67
68enum stk_status {
69 S_PRESENT = 1,
70 S_INITIALISED = 2,
71 S_MEMALLOCD = 4,
72 S_STREAMING = 8,
73};
74#define is_present(dev) ((dev)->status & S_PRESENT)
75#define is_initialised(dev) ((dev)->status & S_INITIALISED)
76#define is_streaming(dev) ((dev)->status & S_STREAMING)
77#define is_memallocd(dev) ((dev)->status & S_MEMALLOCD)
78#define set_present(dev) ((dev)->status = S_PRESENT)
79#define unset_present(dev) ((dev)->status &= \
80 ~(S_PRESENT|S_INITIALISED|S_STREAMING))
81#define set_initialised(dev) ((dev)->status |= S_INITIALISED)
82#define set_memallocd(dev) ((dev)->status |= S_MEMALLOCD)
83#define unset_memallocd(dev) ((dev)->status &= ~S_MEMALLOCD)
84#define set_streaming(dev) ((dev)->status |= S_STREAMING)
85#define unset_streaming(dev) ((dev)->status &= ~S_STREAMING)
86
87struct regval {
88 unsigned reg;
89 unsigned val;
90};
91
92struct stk_camera {
93 struct video_device vdev;
94 struct usb_device *udev;
95 struct usb_interface *interface;
96 int webcam_model;
97 struct file *owner;
98
99 u8 isoc_ep;
100
101 struct kref kref;
102 /* Not sure if this is right */
103 atomic_t urbs_used;
104
105 struct stk_video vsettings;
106
107 enum stk_status status;
108
109 spinlock_t spinlock;
110 wait_queue_head_t wait_frame;
111
112 struct stk_iso_buf *isobufs;
113
114 int frame_size;
115 /* Streaming buffers */
116 unsigned int n_sbufs;
117 struct stk_sio_buffer *sio_bufs;
118 struct list_head sio_avail;
119 struct list_head sio_full;
120 unsigned sequence;
121};
122
123#define to_stk_camera(d) container_of(d, struct stk_camera, kref)
124#define vdev_to_camera(d) container_of(d, struct stk_camera, vdev)
125
126void stk_camera_delete(struct kref *);
127int stk_camera_write_reg(struct stk_camera *, u16, u8);
128int stk_camera_read_reg(struct stk_camera *, u16, int *);
129
130int stk_sensor_outb(struct stk_camera *dev, u8 reg, u8 val);
131int stk_sensor_inb(struct stk_camera *dev, u8 reg, u8 *val);
132int stk_sensor_init(struct stk_camera *);
133int stk_sensor_configure(struct stk_camera *);
134int stk_sensor_sleep(struct stk_camera *dev);
135int stk_sensor_wakeup(struct stk_camera *dev);
136int stk_sensor_set_brightness(struct stk_camera *dev, int br);
137
138#endif