[media] DocBook improvement for U8 and U16 control types
[deliverable/linux.git] / drivers / media / usb / gspca / mars.c
CommitLineData
6a7eba24
JFM
1/*
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
133a9fe9
JP
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
6a7eba24
JFM
24#define MODULE_NAME "mars"
25
26#include "gspca.h"
27#include "jpeg.h"
28
6a7eba24
JFM
29MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
30MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
31MODULE_LICENSE("GPL");
32
b56ab4ca
HG
33#define QUALITY 50
34
6a7eba24
JFM
35/* specific webcam descriptor */
36struct sd {
37 struct gspca_dev gspca_dev; /* !! must be the first item */
a0306bfa 38
98684298
HV
39 struct v4l2_ctrl *brightness;
40 struct v4l2_ctrl *saturation;
41 struct v4l2_ctrl *sharpness;
42 struct v4l2_ctrl *gamma;
43 struct { /* illuminator control cluster */
44 struct v4l2_ctrl *illum_top;
45 struct v4l2_ctrl *illum_bottom;
46 };
9a731a32 47 u8 jpeg_hdr[JPEG_HDR_SZ];
6a7eba24
JFM
48};
49
50/* V4L2 controls supported by the driver */
98684298
HV
51static void setbrightness(struct gspca_dev *gspca_dev, s32 val);
52static void setcolors(struct gspca_dev *gspca_dev, s32 val);
53static void setgamma(struct gspca_dev *gspca_dev, s32 val);
54static void setsharpness(struct gspca_dev *gspca_dev, s32 val);
6a7eba24 55
cc611b8a 56static const struct v4l2_pix_format vga_mode[] = {
c2446b3e
JFM
57 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
58 .bytesperline = 320,
a0306bfa 59 .sizeimage = 320 * 240 * 3 / 8 + 590,
c2446b3e
JFM
60 .colorspace = V4L2_COLORSPACE_JPEG,
61 .priv = 2},
62 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
63 .bytesperline = 640,
64 .sizeimage = 640 * 480 * 3 / 8 + 590,
65 .colorspace = V4L2_COLORSPACE_JPEG,
66 .priv = 1},
6a7eba24
JFM
67};
68
a0306bfa
JFM
69static const __u8 mi_data[0x20] = {
70/* 01 02 03 04 05 06 07 08 */
71 0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
72/* 09 0a 0b 0c 0d 0e 0f 10 */
73 0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
74/* 11 12 13 14 15 16 17 18 */
75 0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
76/* 19 1a 1b 1c 1d 1e 1f 20 */
77 0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
6a7eba24
JFM
78};
79
a0306bfa 80/* write <len> bytes from gspca_dev->usb_buf */
d4015493 81static void reg_w(struct gspca_dev *gspca_dev,
a0306bfa 82 int len)
6a7eba24 83{
a0306bfa
JFM
84 int alen, ret;
85
d4015493
JFM
86 if (gspca_dev->usb_err < 0)
87 return;
88
a0306bfa
JFM
89 ret = usb_bulk_msg(gspca_dev->dev,
90 usb_sndbulkpipe(gspca_dev->dev, 4),
91 gspca_dev->usb_buf,
92 len,
93 &alen,
94 500); /* timeout in milliseconds */
d4015493 95 if (ret < 0) {
133a9fe9
JP
96 pr_err("reg write [%02x] error %d\n",
97 gspca_dev->usb_buf[0], ret);
d4015493
JFM
98 gspca_dev->usb_err = ret;
99 }
6a7eba24
JFM
100}
101
a0306bfa
JFM
102static void mi_w(struct gspca_dev *gspca_dev,
103 u8 addr,
104 u8 value)
739570bb
JFM
105{
106 gspca_dev->usb_buf[0] = 0x1f;
107 gspca_dev->usb_buf[1] = 0; /* control byte */
a0306bfa
JFM
108 gspca_dev->usb_buf[2] = addr;
109 gspca_dev->usb_buf[3] = value;
6a7eba24 110
a0306bfa 111 reg_w(gspca_dev, 4);
6a7eba24
JFM
112}
113
98684298 114static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
e3b4d2c6 115{
e3b4d2c6 116 gspca_dev->usb_buf[0] = 0x61;
98684298 117 gspca_dev->usb_buf[1] = val;
e3b4d2c6
JFM
118 reg_w(gspca_dev, 2);
119}
120
98684298 121static void setcolors(struct gspca_dev *gspca_dev, s32 val)
e3b4d2c6 122{
e3b4d2c6
JFM
123 gspca_dev->usb_buf[0] = 0x5f;
124 gspca_dev->usb_buf[1] = val << 3;
125 gspca_dev->usb_buf[2] = ((val >> 2) & 0xf8) | 0x04;
126 reg_w(gspca_dev, 3);
127}
128
98684298 129static void setgamma(struct gspca_dev *gspca_dev, s32 val)
e3b4d2c6 130{
e3b4d2c6 131 gspca_dev->usb_buf[0] = 0x06;
98684298 132 gspca_dev->usb_buf[1] = val * 0x40;
e3b4d2c6
JFM
133 reg_w(gspca_dev, 2);
134}
135
98684298 136static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
e3b4d2c6 137{
e3b4d2c6 138 gspca_dev->usb_buf[0] = 0x67;
98684298 139 gspca_dev->usb_buf[1] = val * 4 + 3;
e3b4d2c6
JFM
140 reg_w(gspca_dev, 2);
141}
142
98684298 143static void setilluminators(struct gspca_dev *gspca_dev, bool top, bool bottom)
fba39807 144{
98684298 145 /* both are off if not streaming */
fba39807 146 gspca_dev->usb_buf[0] = 0x22;
98684298 147 if (top)
fba39807 148 gspca_dev->usb_buf[1] = 0x76;
98684298 149 else if (bottom)
fba39807
JFM
150 gspca_dev->usb_buf[1] = 0x7a;
151 else
152 gspca_dev->usb_buf[1] = 0x7e;
153 reg_w(gspca_dev, 2);
154}
155
98684298
HV
156static int mars_s_ctrl(struct v4l2_ctrl *ctrl)
157{
a8a47860
HG
158 struct gspca_dev *gspca_dev =
159 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
160 struct sd *sd = (struct sd *)gspca_dev;
98684298
HV
161
162 gspca_dev->usb_err = 0;
163
164 if (ctrl->id == V4L2_CID_ILLUMINATORS_1) {
165 /* only one can be on at a time */
166 if (ctrl->is_new && ctrl->val)
167 sd->illum_bottom->val = 0;
168 if (sd->illum_bottom->is_new && sd->illum_bottom->val)
169 sd->illum_top->val = 0;
170 }
171
172 if (!gspca_dev->streaming)
173 return 0;
174
175 switch (ctrl->id) {
176 case V4L2_CID_BRIGHTNESS:
a8a47860 177 setbrightness(gspca_dev, ctrl->val);
98684298
HV
178 break;
179 case V4L2_CID_SATURATION:
a8a47860 180 setcolors(gspca_dev, ctrl->val);
98684298
HV
181 break;
182 case V4L2_CID_GAMMA:
a8a47860 183 setgamma(gspca_dev, ctrl->val);
98684298
HV
184 break;
185 case V4L2_CID_ILLUMINATORS_1:
a8a47860
HG
186 setilluminators(gspca_dev, sd->illum_top->val,
187 sd->illum_bottom->val);
98684298
HV
188 break;
189 case V4L2_CID_SHARPNESS:
a8a47860 190 setsharpness(gspca_dev, ctrl->val);
98684298 191 break;
98684298
HV
192 default:
193 return -EINVAL;
194 }
195 return gspca_dev->usb_err;
196}
197
198static const struct v4l2_ctrl_ops mars_ctrl_ops = {
199 .s_ctrl = mars_s_ctrl,
200};
201
202/* this function is called at probe time */
203static int sd_init_controls(struct gspca_dev *gspca_dev)
204{
205 struct sd *sd = (struct sd *) gspca_dev;
a8a47860 206 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
98684298
HV
207
208 gspca_dev->vdev.ctrl_handler = hdl;
b56ab4ca 209 v4l2_ctrl_handler_init(hdl, 6);
98684298
HV
210 sd->brightness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
211 V4L2_CID_BRIGHTNESS, 0, 30, 1, 15);
212 sd->saturation = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
213 V4L2_CID_SATURATION, 0, 255, 1, 200);
214 sd->gamma = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
215 V4L2_CID_GAMMA, 0, 3, 1, 1);
216 sd->sharpness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
217 V4L2_CID_SHARPNESS, 0, 2, 1, 1);
218 sd->illum_top = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
219 V4L2_CID_ILLUMINATORS_1, 0, 1, 1, 0);
220 sd->illum_top->flags |= V4L2_CTRL_FLAG_UPDATE;
221 sd->illum_bottom = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
222 V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
223 sd->illum_bottom->flags |= V4L2_CTRL_FLAG_UPDATE;
98684298
HV
224 if (hdl->error) {
225 pr_err("Could not initialize controls\n");
226 return hdl->error;
227 }
228 v4l2_ctrl_cluster(2, &sd->illum_top);
229 return 0;
230}
231
6a7eba24
JFM
232/* this function is called at probe time */
233static int sd_config(struct gspca_dev *gspca_dev,
234 const struct usb_device_id *id)
235{
6a7eba24
JFM
236 struct cam *cam;
237
238 cam = &gspca_dev->cam;
6a7eba24 239 cam->cam_mode = vga_mode;
80297125 240 cam->nmodes = ARRAY_SIZE(vga_mode);
6a7eba24
JFM
241 return 0;
242}
243
012d6b02
JFM
244/* this function is called at probe and resume time */
245static int sd_init(struct gspca_dev *gspca_dev)
6a7eba24
JFM
246{
247 return 0;
248}
249
72ab97ce 250static int sd_start(struct gspca_dev *gspca_dev)
6a7eba24 251{
a0306bfa 252 struct sd *sd = (struct sd *) gspca_dev;
a0306bfa 253 u8 *data;
253bae57 254 int i;
6a7eba24 255
71cb2764 256 /* create the JPEG header */
1966bc2a
OZ
257 jpeg_define(sd->jpeg_hdr, gspca_dev->pixfmt.height,
258 gspca_dev->pixfmt.width,
71cb2764 259 0x21); /* JPEG 422 */
b56ab4ca 260 jpeg_set_qual(sd->jpeg_hdr, QUALITY);
71cb2764 261
739570bb 262 data = gspca_dev->usb_buf;
a0306bfa 263
6a7eba24
JFM
264 data[0] = 0x01; /* address */
265 data[1] = 0x01;
d4015493 266 reg_w(gspca_dev, 2);
6a7eba24
JFM
267
268 /*
269 Initialize the MR97113 chip register
270 */
271 data[0] = 0x00; /* address */
272 data[1] = 0x0c | 0x01; /* reg 0 */
273 data[2] = 0x01; /* reg 1 */
1966bc2a
OZ
274 data[3] = gspca_dev->pixfmt.width / 8; /* h_size , reg 2 */
275 data[4] = gspca_dev->pixfmt.height / 8; /* v_size , reg 3 */
6a7eba24
JFM
276 data[5] = 0x30; /* reg 4, MI, PAS5101 :
277 * 0x30 for 24mhz , 0x28 for 12mhz */
a0306bfa 278 data[6] = 0x02; /* reg 5, H start - was 0x04 */
98684298 279 data[7] = v4l2_ctrl_g_ctrl(sd->gamma) * 0x40; /* reg 0x06: gamma */
a0306bfa 280 data[8] = 0x01; /* reg 7, V start - was 0x03 */
739570bb 281/* if (h_size == 320 ) */
6a7eba24
JFM
282/* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
283/* else */
284 data[9] = 0x52; /* reg 8, 24MHz, no scale down */
a0306bfa
JFM
285/*jfm: from win trace*/
286 data[10] = 0x18;
6a7eba24 287
d4015493 288 reg_w(gspca_dev, 11);
6a7eba24
JFM
289
290 data[0] = 0x23; /* address */
291 data[1] = 0x09; /* reg 35, append frame header */
292
d4015493 293 reg_w(gspca_dev, 2);
6a7eba24 294
739570bb
JFM
295 data[0] = 0x3c; /* address */
296/* if (gspca_dev->width == 1280) */
6a7eba24
JFM
297/* data[1] = 200; * reg 60, pc-cam frame size
298 * (unit: 4KB) 800KB */
299/* else */
300 data[1] = 50; /* 50 reg 60, pc-cam frame size
301 * (unit: 4KB) 200KB */
d4015493 302 reg_w(gspca_dev, 2);
6a7eba24 303
6a7eba24
JFM
304 /* auto dark-gain */
305 data[0] = 0x5e; /* address */
a0306bfa
JFM
306 data[1] = 0; /* reg 94, Y Gain (auto) */
307/*jfm: from win trace*/
253bae57
JFM
308 /* reg 0x5f/0x60 (LE) = saturation */
309 /* h (60): xxxx x100
310 * l (5f): xxxx x000 */
98684298
HV
311 data[2] = v4l2_ctrl_g_ctrl(sd->saturation) << 3;
312 data[3] = ((v4l2_ctrl_g_ctrl(sd->saturation) >> 2) & 0xf8) | 0x04;
313 data[4] = v4l2_ctrl_g_ctrl(sd->brightness); /* reg 0x61 = brightness */
a0306bfa
JFM
314 data[5] = 0x00;
315
d4015493 316 reg_w(gspca_dev, 6);
6a7eba24
JFM
317
318 data[0] = 0x67;
a0306bfa 319/*jfm: from win trace*/
98684298 320 data[1] = v4l2_ctrl_g_ctrl(sd->sharpness) * 4 + 3;
a0306bfa 321 data[2] = 0x14;
d4015493 322 reg_w(gspca_dev, 3);
6a7eba24 323
a0306bfa
JFM
324 data[0] = 0x69;
325 data[1] = 0x2f;
326 data[2] = 0x28;
327 data[3] = 0x42;
d4015493 328 reg_w(gspca_dev, 4);
a0306bfa
JFM
329
330 data[0] = 0x63;
331 data[1] = 0x07;
d4015493 332 reg_w(gspca_dev, 2);
a0306bfa 333/*jfm: win trace - many writes here to reg 0x64*/
a0306bfa
JFM
334
335 /* initialize the MI sensor */
336 for (i = 0; i < sizeof mi_data; i++)
337 mi_w(gspca_dev, i + 1, mi_data[i]);
6a7eba24
JFM
338
339 data[0] = 0x00;
25985edc 340 data[1] = 0x4d; /* ISOC transferring enable... */
a0306bfa 341 reg_w(gspca_dev, 2);
d4015493 342
98684298
HV
343 setilluminators(gspca_dev, v4l2_ctrl_g_ctrl(sd->illum_top),
344 v4l2_ctrl_g_ctrl(sd->illum_bottom));
345
d4015493 346 return gspca_dev->usb_err;
6a7eba24
JFM
347}
348
349static void sd_stopN(struct gspca_dev *gspca_dev)
350{
fba39807
JFM
351 struct sd *sd = (struct sd *) gspca_dev;
352
98684298
HV
353 if (v4l2_ctrl_g_ctrl(sd->illum_top) ||
354 v4l2_ctrl_g_ctrl(sd->illum_bottom)) {
355 setilluminators(gspca_dev, false, false);
fba39807
JFM
356 msleep(20);
357 }
358
739570bb
JFM
359 gspca_dev->usb_buf[0] = 1;
360 gspca_dev->usb_buf[1] = 0;
d4015493 361 reg_w(gspca_dev, 2);
6a7eba24
JFM
362}
363
6a7eba24 364static void sd_pkt_scan(struct gspca_dev *gspca_dev,
76dd272b 365 u8 *data, /* isoc packet */
6a7eba24
JFM
366 int len) /* iso packet length */
367{
71cb2764 368 struct sd *sd = (struct sd *) gspca_dev;
6a7eba24
JFM
369 int p;
370
371 if (len < 6) {
372/* gspca_dev->last_packet_type = DISCARD_PACKET; */
373 return;
374 }
375 for (p = 0; p < len - 6; p++) {
376 if (data[0 + p] == 0xff
377 && data[1 + p] == 0xff
378 && data[2 + p] == 0x00
379 && data[3 + p] == 0xff
380 && data[4 + p] == 0x96) {
381 if (data[5 + p] == 0x64
382 || data[5 + p] == 0x65
383 || data[5 + p] == 0x66
384 || data[5 + p] == 0x67) {
a0306bfa 385 PDEBUG(D_PACK, "sof offset: %d len: %d",
6a7eba24 386 p, len);
76dd272b
JFM
387 gspca_frame_add(gspca_dev, LAST_PACKET,
388 data, p);
6a7eba24
JFM
389
390 /* put the JPEG header */
76dd272b 391 gspca_frame_add(gspca_dev, FIRST_PACKET,
71cb2764 392 sd->jpeg_hdr, JPEG_HDR_SZ);
0a32ef3f
JFM
393 data += p + 16;
394 len -= p + 16;
6a7eba24
JFM
395 break;
396 }
397 }
398 }
76dd272b 399 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
6a7eba24
JFM
400}
401
402/* sub-driver description */
a5ae2062 403static const struct sd_desc sd_desc = {
6a7eba24 404 .name = MODULE_NAME,
6a7eba24 405 .config = sd_config,
012d6b02 406 .init = sd_init,
98684298 407 .init_controls = sd_init_controls,
6a7eba24
JFM
408 .start = sd_start,
409 .stopN = sd_stopN,
6a7eba24
JFM
410 .pkt_scan = sd_pkt_scan,
411};
412
413/* -- module initialisation -- */
95c967c1 414static const struct usb_device_id device_table[] = {
9d64fdb1 415 {USB_DEVICE(0x093a, 0x050f)},
6a7eba24
JFM
416 {}
417};
418MODULE_DEVICE_TABLE(usb, device_table);
419
420/* -- device connect -- */
421static int sd_probe(struct usb_interface *intf,
422 const struct usb_device_id *id)
423{
424 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
425 THIS_MODULE);
426}
427
428static struct usb_driver sd_driver = {
429 .name = MODULE_NAME,
430 .id_table = device_table,
431 .probe = sd_probe,
432 .disconnect = gspca_disconnect,
6a709749
JFM
433#ifdef CONFIG_PM
434 .suspend = gspca_suspend,
435 .resume = gspca_resume,
98684298 436 .reset_resume = gspca_resume,
6a709749 437#endif
6a7eba24
JFM
438};
439
ecb3b2b3 440module_usb_driver(sd_driver);
This page took 0.815692 seconds and 5 git commands to generate.