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