blob: 0196209a948a879846fcbb660b11a0fe91feddc0 [file] [log] [blame]
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001/*
2 * Mars-Semi MR97311A library
3 * Copyright (C) 2005 <bradlch@hotmail.com>
4 *
5 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
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#define MODULE_NAME "mars"
23
24#include "gspca.h"
25#include "jpeg.h"
26
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030027MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
28MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
29MODULE_LICENSE("GPL");
30
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030031/* controls */
32enum e_ctrl {
33 BRIGHTNESS,
34 COLORS,
35 GAMMA,
36 SHARPNESS,
Jean-François Moinefba39802010-10-16 14:12:53 -030037 ILLUM_TOP,
38 ILLUM_BOT,
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030039 NCTRLS /* number of controls */
40};
41
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030042/* specific webcam descriptor */
43struct sd {
44 struct gspca_dev gspca_dev; /* !! must be the first item */
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030045
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030046 struct gspca_ctrl ctrls[NCTRLS];
47
Jean-Francois Moine71cb2762009-03-03 05:33:41 -030048 u8 quality;
Jean-Francois Moine77ac0ba2009-03-02 06:40:52 -030049#define QUALITY_MIN 40
50#define QUALITY_MAX 70
51#define QUALITY_DEF 50
Jean-Francois Moine71cb2762009-03-03 05:33:41 -030052
Jean-François Moine9a731a32010-06-04 05:26:42 -030053 u8 jpeg_hdr[JPEG_HDR_SZ];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030054};
55
56/* V4L2 controls supported by the driver */
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030057static void setbrightness(struct gspca_dev *gspca_dev);
58static void setcolors(struct gspca_dev *gspca_dev);
59static void setgamma(struct gspca_dev *gspca_dev);
60static void setsharpness(struct gspca_dev *gspca_dev);
Jean-François Moinefba39802010-10-16 14:12:53 -030061static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val);
62static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val);
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030063
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030064static const struct ctrl sd_ctrls[NCTRLS] = {
65[BRIGHTNESS] = {
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030066 {
67 .id = V4L2_CID_BRIGHTNESS,
68 .type = V4L2_CTRL_TYPE_INTEGER,
69 .name = "Brightness",
70 .minimum = 0,
71 .maximum = 30,
72 .step = 1,
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030073 .default_value = 15,
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030074 },
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030075 .set_control = setbrightness
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030076 },
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030077[COLORS] = {
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030078 {
79 .id = V4L2_CID_SATURATION,
80 .type = V4L2_CTRL_TYPE_INTEGER,
81 .name = "Color",
Jean-Francois Moine253bae52009-02-28 08:09:24 -030082 .minimum = 1,
83 .maximum = 255,
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030084 .step = 1,
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030085 .default_value = 200,
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030086 },
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030087 .set_control = setcolors
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030088 },
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030089[GAMMA] = {
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030090 {
91 .id = V4L2_CID_GAMMA,
92 .type = V4L2_CTRL_TYPE_INTEGER,
93 .name = "Gamma",
94 .minimum = 0,
95 .maximum = 3,
96 .step = 1,
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030097 .default_value = 1,
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -030098 },
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -030099 .set_control = setgamma
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300100 },
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -0300101[SHARPNESS] = {
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300102 {
103 .id = V4L2_CID_SHARPNESS,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Sharpness",
106 .minimum = 0,
107 .maximum = 2,
108 .step = 1,
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -0300109 .default_value = 1,
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300110 },
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -0300111 .set_control = setsharpness
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300112 },
Jean-François Moinefba39802010-10-16 14:12:53 -0300113[ILLUM_TOP] = {
114 {
115 .id = V4L2_CID_ILLUMINATORS_1,
116 .type = V4L2_CTRL_TYPE_BOOLEAN,
117 .name = "Top illuminator",
118 .minimum = 0,
119 .maximum = 1,
120 .step = 1,
121 .default_value = 0,
122 .flags = V4L2_CTRL_FLAG_UPDATE,
123 },
124 .set = sd_setilluminator1
125 },
126[ILLUM_BOT] = {
127 {
128 .id = V4L2_CID_ILLUMINATORS_2,
129 .type = V4L2_CTRL_TYPE_BOOLEAN,
130 .name = "Bottom illuminator",
131 .minimum = 0,
132 .maximum = 1,
133 .step = 1,
134 .default_value = 0,
135 .flags = V4L2_CTRL_FLAG_UPDATE,
136 },
137 .set = sd_setilluminator2
138 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300139};
140
Jean-Francois Moinecc611b82008-12-29 07:49:41 -0300141static const struct v4l2_pix_format vga_mode[] = {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300142 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
143 .bytesperline = 320,
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300144 .sizeimage = 320 * 240 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300145 .colorspace = V4L2_COLORSPACE_JPEG,
146 .priv = 2},
147 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
148 .bytesperline = 640,
149 .sizeimage = 640 * 480 * 3 / 8 + 590,
150 .colorspace = V4L2_COLORSPACE_JPEG,
151 .priv = 1},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300152};
153
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300154static const __u8 mi_data[0x20] = {
155/* 01 02 03 04 05 06 07 08 */
156 0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
157/* 09 0a 0b 0c 0d 0e 0f 10 */
158 0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
159/* 11 12 13 14 15 16 17 18 */
160 0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
161/* 19 1a 1b 1c 1d 1e 1f 20 */
162 0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300163};
164
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300165/* write <len> bytes from gspca_dev->usb_buf */
Jean-François Moined4015492010-10-16 14:10:59 -0300166static void reg_w(struct gspca_dev *gspca_dev,
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300167 int len)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300168{
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300169 int alen, ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300170
Jean-François Moined4015492010-10-16 14:10:59 -0300171 if (gspca_dev->usb_err < 0)
172 return;
173
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300174 ret = usb_bulk_msg(gspca_dev->dev,
175 usb_sndbulkpipe(gspca_dev->dev, 4),
176 gspca_dev->usb_buf,
177 len,
178 &alen,
179 500); /* timeout in milliseconds */
Jean-François Moined4015492010-10-16 14:10:59 -0300180 if (ret < 0) {
Jean-François Moine0b656322010-09-13 05:19:58 -0300181 err("reg write [%02x] error %d",
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300182 gspca_dev->usb_buf[0], ret);
Jean-François Moined4015492010-10-16 14:10:59 -0300183 gspca_dev->usb_err = ret;
184 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300185}
186
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300187static void mi_w(struct gspca_dev *gspca_dev,
188 u8 addr,
189 u8 value)
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300190{
191 gspca_dev->usb_buf[0] = 0x1f;
192 gspca_dev->usb_buf[1] = 0; /* control byte */
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300193 gspca_dev->usb_buf[2] = addr;
194 gspca_dev->usb_buf[3] = value;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300195
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300196 reg_w(gspca_dev, 4);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300197}
198
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -0300199static void setbrightness(struct gspca_dev *gspca_dev)
200{
201 struct sd *sd = (struct sd *) gspca_dev;
202
203 gspca_dev->usb_buf[0] = 0x61;
204 gspca_dev->usb_buf[1] = sd->ctrls[BRIGHTNESS].val;
205 reg_w(gspca_dev, 2);
206}
207
208static void setcolors(struct gspca_dev *gspca_dev)
209{
210 struct sd *sd = (struct sd *) gspca_dev;
211 s16 val;
212
213 val = sd->ctrls[COLORS].val;
214 gspca_dev->usb_buf[0] = 0x5f;
215 gspca_dev->usb_buf[1] = val << 3;
216 gspca_dev->usb_buf[2] = ((val >> 2) & 0xf8) | 0x04;
217 reg_w(gspca_dev, 3);
218}
219
220static void setgamma(struct gspca_dev *gspca_dev)
221{
222 struct sd *sd = (struct sd *) gspca_dev;
223
224 gspca_dev->usb_buf[0] = 0x06;
225 gspca_dev->usb_buf[1] = sd->ctrls[GAMMA].val * 0x40;
226 reg_w(gspca_dev, 2);
227}
228
229static void setsharpness(struct gspca_dev *gspca_dev)
230{
231 struct sd *sd = (struct sd *) gspca_dev;
232
233 gspca_dev->usb_buf[0] = 0x67;
234 gspca_dev->usb_buf[1] = sd->ctrls[SHARPNESS].val * 4 + 3;
235 reg_w(gspca_dev, 2);
236}
237
Jean-François Moinefba39802010-10-16 14:12:53 -0300238static void setilluminators(struct gspca_dev *gspca_dev)
239{
240 struct sd *sd = (struct sd *) gspca_dev;
241
242 gspca_dev->usb_buf[0] = 0x22;
243 if (sd->ctrls[ILLUM_TOP].val)
244 gspca_dev->usb_buf[1] = 0x76;
245 else if (sd->ctrls[ILLUM_BOT].val)
246 gspca_dev->usb_buf[1] = 0x7a;
247 else
248 gspca_dev->usb_buf[1] = 0x7e;
249 reg_w(gspca_dev, 2);
250}
251
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300252/* this function is called at probe time */
253static int sd_config(struct gspca_dev *gspca_dev,
254 const struct usb_device_id *id)
255{
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300256 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300257 struct cam *cam;
258
259 cam = &gspca_dev->cam;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300260 cam->cam_mode = vga_mode;
Julia Lawall80297122008-11-12 23:18:21 -0300261 cam->nmodes = ARRAY_SIZE(vga_mode);
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -0300262 cam->ctrls = sd->ctrls;
Jean-Francois Moine77ac0ba2009-03-02 06:40:52 -0300263 sd->quality = QUALITY_DEF;
Jean-Francois Moine625deb32009-01-15 06:11:49 -0300264 gspca_dev->nbalt = 9; /* use the altsetting 08 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300265 return 0;
266}
267
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300268/* this function is called at probe and resume time */
269static int sd_init(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300270{
Jean-François Moinefba39802010-10-16 14:12:53 -0300271 gspca_dev->ctrl_inac = (1 << ILLUM_TOP) | (1 << ILLUM_BOT);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300272 return 0;
273}
274
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300275static int sd_start(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300276{
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300277 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300278 u8 *data;
Jean-Francois Moine253bae52009-02-28 08:09:24 -0300279 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300280
Jean-Francois Moine71cb2762009-03-03 05:33:41 -0300281 /* create the JPEG header */
Jean-Francois Moine71cb2762009-03-03 05:33:41 -0300282 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
283 0x21); /* JPEG 422 */
284 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
285
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300286 data = gspca_dev->usb_buf;
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300287
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300288 data[0] = 0x01; /* address */
289 data[1] = 0x01;
Jean-François Moined4015492010-10-16 14:10:59 -0300290 reg_w(gspca_dev, 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300291
292 /*
293 Initialize the MR97113 chip register
294 */
295 data[0] = 0x00; /* address */
296 data[1] = 0x0c | 0x01; /* reg 0 */
297 data[2] = 0x01; /* reg 1 */
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300298 data[3] = gspca_dev->width / 8; /* h_size , reg 2 */
299 data[4] = gspca_dev->height / 8; /* v_size , reg 3 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300300 data[5] = 0x30; /* reg 4, MI, PAS5101 :
301 * 0x30 for 24mhz , 0x28 for 12mhz */
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300302 data[6] = 0x02; /* reg 5, H start - was 0x04 */
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -0300303 data[7] = sd->ctrls[GAMMA].val * 0x40; /* reg 0x06: gamma */
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300304 data[8] = 0x01; /* reg 7, V start - was 0x03 */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300305/* if (h_size == 320 ) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300306/* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
307/* else */
308 data[9] = 0x52; /* reg 8, 24MHz, no scale down */
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300309/*jfm: from win trace*/
310 data[10] = 0x18;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300311
Jean-François Moined4015492010-10-16 14:10:59 -0300312 reg_w(gspca_dev, 11);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300313
314 data[0] = 0x23; /* address */
315 data[1] = 0x09; /* reg 35, append frame header */
316
Jean-François Moined4015492010-10-16 14:10:59 -0300317 reg_w(gspca_dev, 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300318
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300319 data[0] = 0x3c; /* address */
320/* if (gspca_dev->width == 1280) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300321/* data[1] = 200; * reg 60, pc-cam frame size
322 * (unit: 4KB) 800KB */
323/* else */
324 data[1] = 50; /* 50 reg 60, pc-cam frame size
325 * (unit: 4KB) 200KB */
Jean-François Moined4015492010-10-16 14:10:59 -0300326 reg_w(gspca_dev, 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300327
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300328 /* auto dark-gain */
329 data[0] = 0x5e; /* address */
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300330 data[1] = 0; /* reg 94, Y Gain (auto) */
331/*jfm: from win trace*/
Jean-Francois Moine253bae52009-02-28 08:09:24 -0300332 /* reg 0x5f/0x60 (LE) = saturation */
333 /* h (60): xxxx x100
334 * l (5f): xxxx x000 */
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -0300335 data[2] = sd->ctrls[COLORS].val << 3;
336 data[3] = ((sd->ctrls[COLORS].val >> 2) & 0xf8) | 0x04;
337 data[4] = sd->ctrls[BRIGHTNESS].val; /* reg 0x61 = brightness */
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300338 data[5] = 0x00;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300339
Jean-François Moined4015492010-10-16 14:10:59 -0300340 reg_w(gspca_dev, 6);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300341
342 data[0] = 0x67;
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300343/*jfm: from win trace*/
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -0300344 data[1] = sd->ctrls[SHARPNESS].val * 4 + 3;
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300345 data[2] = 0x14;
Jean-François Moined4015492010-10-16 14:10:59 -0300346 reg_w(gspca_dev, 3);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300347
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300348 data[0] = 0x69;
349 data[1] = 0x2f;
350 data[2] = 0x28;
351 data[3] = 0x42;
Jean-François Moined4015492010-10-16 14:10:59 -0300352 reg_w(gspca_dev, 4);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300353
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300354 data[0] = 0x63;
355 data[1] = 0x07;
Jean-François Moined4015492010-10-16 14:10:59 -0300356 reg_w(gspca_dev, 2);
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300357/*jfm: win trace - many writes here to reg 0x64*/
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300358
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300359 /* initialize the MI sensor */
360 for (i = 0; i < sizeof mi_data; i++)
361 mi_w(gspca_dev, i + 1, mi_data[i]);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300362
363 data[0] = 0x00;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300364 data[1] = 0x4d; /* ISOC transferring enable... */
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300365 reg_w(gspca_dev, 2);
Jean-François Moined4015492010-10-16 14:10:59 -0300366
Jean-François Moinefba39802010-10-16 14:12:53 -0300367 gspca_dev->ctrl_inac = 0; /* activate the illuminator controls */
Jean-François Moined4015492010-10-16 14:10:59 -0300368 return gspca_dev->usb_err;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300369}
370
371static void sd_stopN(struct gspca_dev *gspca_dev)
372{
Jean-François Moinefba39802010-10-16 14:12:53 -0300373 struct sd *sd = (struct sd *) gspca_dev;
374
375 gspca_dev->ctrl_inac = (1 << ILLUM_TOP) | (1 << ILLUM_BOT);
376 if (sd->ctrls[ILLUM_TOP].val || sd->ctrls[ILLUM_BOT].val) {
377 sd->ctrls[ILLUM_TOP].val = 0;
378 sd->ctrls[ILLUM_BOT].val = 0;
379 setilluminators(gspca_dev);
380 msleep(20);
381 }
382
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300383 gspca_dev->usb_buf[0] = 1;
384 gspca_dev->usb_buf[1] = 0;
Jean-François Moined4015492010-10-16 14:10:59 -0300385 reg_w(gspca_dev, 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300386}
387
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300388static void sd_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300389 u8 *data, /* isoc packet */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300390 int len) /* iso packet length */
391{
Jean-Francois Moine71cb2762009-03-03 05:33:41 -0300392 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300393 int p;
394
395 if (len < 6) {
396/* gspca_dev->last_packet_type = DISCARD_PACKET; */
397 return;
398 }
399 for (p = 0; p < len - 6; p++) {
400 if (data[0 + p] == 0xff
401 && data[1 + p] == 0xff
402 && data[2 + p] == 0x00
403 && data[3 + p] == 0xff
404 && data[4 + p] == 0x96) {
405 if (data[5 + p] == 0x64
406 || data[5 + p] == 0x65
407 || data[5 + p] == 0x66
408 || data[5 + p] == 0x67) {
Jean-Francois Moinea0306bf2009-01-08 16:29:38 -0300409 PDEBUG(D_PACK, "sof offset: %d len: %d",
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300410 p, len);
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300411 gspca_frame_add(gspca_dev, LAST_PACKET,
412 data, p);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300413
414 /* put the JPEG header */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300415 gspca_frame_add(gspca_dev, FIRST_PACKET,
Jean-Francois Moine71cb2762009-03-03 05:33:41 -0300416 sd->jpeg_hdr, JPEG_HDR_SZ);
Jean-Francois Moine0a32ef32009-01-08 16:30:58 -0300417 data += p + 16;
418 len -= p + 16;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300419 break;
420 }
421 }
422 }
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300423 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300424}
425
Jean-François Moinefba39802010-10-16 14:12:53 -0300426static int sd_setilluminator1(struct gspca_dev *gspca_dev, __s32 val)
427{
428 struct sd *sd = (struct sd *) gspca_dev;
429
430 /* only one illuminator may be on */
431 sd->ctrls[ILLUM_TOP].val = val;
432 if (val)
433 sd->ctrls[ILLUM_BOT].val = 0;
434 setilluminators(gspca_dev);
435 return gspca_dev->usb_err;
436}
437
438static int sd_setilluminator2(struct gspca_dev *gspca_dev, __s32 val)
439{
440 struct sd *sd = (struct sd *) gspca_dev;
441
442 /* only one illuminator may be on */
443 sd->ctrls[ILLUM_BOT].val = val;
444 if (val)
445 sd->ctrls[ILLUM_TOP].val = 0;
446 setilluminators(gspca_dev);
447 return gspca_dev->usb_err;
448}
449
Jean-Francois Moine77ac0ba2009-03-02 06:40:52 -0300450static int sd_set_jcomp(struct gspca_dev *gspca_dev,
451 struct v4l2_jpegcompression *jcomp)
452{
453 struct sd *sd = (struct sd *) gspca_dev;
454
455 if (jcomp->quality < QUALITY_MIN)
456 sd->quality = QUALITY_MIN;
457 else if (jcomp->quality > QUALITY_MAX)
458 sd->quality = QUALITY_MAX;
459 else
460 sd->quality = jcomp->quality;
461 if (gspca_dev->streaming)
462 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
463 return 0;
464}
465
466static int sd_get_jcomp(struct gspca_dev *gspca_dev,
467 struct v4l2_jpegcompression *jcomp)
468{
469 struct sd *sd = (struct sd *) gspca_dev;
470
471 memset(jcomp, 0, sizeof *jcomp);
472 jcomp->quality = sd->quality;
473 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
474 | V4L2_JPEG_MARKER_DQT;
475 return 0;
476}
477
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300478/* sub-driver description */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300479static const struct sd_desc sd_desc = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300480 .name = MODULE_NAME,
481 .ctrls = sd_ctrls,
Jean-François Moinee3b4d2c2010-10-16 14:07:06 -0300482 .nctrls = NCTRLS,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300483 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300484 .init = sd_init,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300485 .start = sd_start,
486 .stopN = sd_stopN,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300487 .pkt_scan = sd_pkt_scan,
Jean-Francois Moine77ac0ba2009-03-02 06:40:52 -0300488 .get_jcomp = sd_get_jcomp,
489 .set_jcomp = sd_set_jcomp,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300490};
491
492/* -- module initialisation -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300493static const struct usb_device_id device_table[] = {
Jean-Francois Moine9d64fdb2008-07-25 08:53:03 -0300494 {USB_DEVICE(0x093a, 0x050f)},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300495 {}
496};
497MODULE_DEVICE_TABLE(usb, device_table);
498
499/* -- device connect -- */
500static int sd_probe(struct usb_interface *intf,
501 const struct usb_device_id *id)
502{
503 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
504 THIS_MODULE);
505}
506
507static struct usb_driver sd_driver = {
508 .name = MODULE_NAME,
509 .id_table = device_table,
510 .probe = sd_probe,
511 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -0300512#ifdef CONFIG_PM
513 .suspend = gspca_suspend,
514 .resume = gspca_resume,
515#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300516};
517
518/* -- module insert / remove -- */
519static int __init sd_mod_init(void)
520{
Jean-François Moine54826432010-09-13 04:53:03 -0300521 return usb_register(&sd_driver);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300522}
523static void __exit sd_mod_exit(void)
524{
525 usb_deregister(&sd_driver);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300526}
527
528module_init(sd_mod_init);
529module_exit(sd_mod_exit);