2 * Mars-Semi MR97311A library
3 * Copyright (C) 2005 <bradlch@hotmail.com>
5 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
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
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.
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
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #define MODULE_NAME "mars"
29 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
30 MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
31 MODULE_LICENSE("GPL");
33 /* specific webcam descriptor */
35 struct gspca_dev gspca_dev; /* !! must be the first item */
37 struct v4l2_ctrl *brightness;
38 struct v4l2_ctrl *saturation;
39 struct v4l2_ctrl *sharpness;
40 struct v4l2_ctrl *gamma;
41 struct { /* illuminator control cluster */
42 struct v4l2_ctrl *illum_top;
43 struct v4l2_ctrl *illum_bottom;
45 struct v4l2_ctrl *jpegqual;
48 #define QUALITY_MIN 40
49 #define QUALITY_MAX 70
50 #define QUALITY_DEF 50
52 u8 jpeg_hdr[JPEG_HDR_SZ];
55 /* V4L2 controls supported by the driver */
56 static void setbrightness(struct gspca_dev *gspca_dev, s32 val);
57 static void setcolors(struct gspca_dev *gspca_dev, s32 val);
58 static void setgamma(struct gspca_dev *gspca_dev, s32 val);
59 static void setsharpness(struct gspca_dev *gspca_dev, s32 val);
61 static const struct v4l2_pix_format vga_mode[] = {
62 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
64 .sizeimage = 320 * 240 * 3 / 8 + 590,
65 .colorspace = V4L2_COLORSPACE_JPEG,
67 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
69 .sizeimage = 640 * 480 * 3 / 8 + 590,
70 .colorspace = V4L2_COLORSPACE_JPEG,
74 static const __u8 mi_data[0x20] = {
75 /* 01 02 03 04 05 06 07 08 */
76 0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
77 /* 09 0a 0b 0c 0d 0e 0f 10 */
78 0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
79 /* 11 12 13 14 15 16 17 18 */
80 0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
81 /* 19 1a 1b 1c 1d 1e 1f 20 */
82 0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
85 /* write <len> bytes from gspca_dev->usb_buf */
86 static void reg_w(struct gspca_dev *gspca_dev,
91 if (gspca_dev->usb_err < 0)
94 ret = usb_bulk_msg(gspca_dev->dev,
95 usb_sndbulkpipe(gspca_dev->dev, 4),
99 500); /* timeout in milliseconds */
101 pr_err("reg write [%02x] error %d\n",
102 gspca_dev->usb_buf[0], ret);
103 gspca_dev->usb_err = ret;
107 static void mi_w(struct gspca_dev *gspca_dev,
111 gspca_dev->usb_buf[0] = 0x1f;
112 gspca_dev->usb_buf[1] = 0; /* control byte */
113 gspca_dev->usb_buf[2] = addr;
114 gspca_dev->usb_buf[3] = value;
119 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
121 gspca_dev->usb_buf[0] = 0x61;
122 gspca_dev->usb_buf[1] = val;
126 static void setcolors(struct gspca_dev *gspca_dev, s32 val)
128 gspca_dev->usb_buf[0] = 0x5f;
129 gspca_dev->usb_buf[1] = val << 3;
130 gspca_dev->usb_buf[2] = ((val >> 2) & 0xf8) | 0x04;
134 static void setgamma(struct gspca_dev *gspca_dev, s32 val)
136 gspca_dev->usb_buf[0] = 0x06;
137 gspca_dev->usb_buf[1] = val * 0x40;
141 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
143 gspca_dev->usb_buf[0] = 0x67;
144 gspca_dev->usb_buf[1] = val * 4 + 3;
148 static void setilluminators(struct gspca_dev *gspca_dev, bool top, bool bottom)
150 /* both are off if not streaming */
151 gspca_dev->usb_buf[0] = 0x22;
153 gspca_dev->usb_buf[1] = 0x76;
155 gspca_dev->usb_buf[1] = 0x7a;
157 gspca_dev->usb_buf[1] = 0x7e;
161 static int mars_s_ctrl(struct v4l2_ctrl *ctrl)
163 struct gspca_dev *gspca_dev =
164 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
165 struct sd *sd = (struct sd *)gspca_dev;
167 gspca_dev->usb_err = 0;
169 if (ctrl->id == V4L2_CID_ILLUMINATORS_1) {
170 /* only one can be on at a time */
171 if (ctrl->is_new && ctrl->val)
172 sd->illum_bottom->val = 0;
173 if (sd->illum_bottom->is_new && sd->illum_bottom->val)
174 sd->illum_top->val = 0;
177 if (!gspca_dev->streaming)
181 case V4L2_CID_BRIGHTNESS:
182 setbrightness(gspca_dev, ctrl->val);
184 case V4L2_CID_SATURATION:
185 setcolors(gspca_dev, ctrl->val);
188 setgamma(gspca_dev, ctrl->val);
190 case V4L2_CID_ILLUMINATORS_1:
191 setilluminators(gspca_dev, sd->illum_top->val,
192 sd->illum_bottom->val);
194 case V4L2_CID_SHARPNESS:
195 setsharpness(gspca_dev, ctrl->val);
197 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
198 jpeg_set_qual(sd->jpeg_hdr, ctrl->val);
203 return gspca_dev->usb_err;
206 static const struct v4l2_ctrl_ops mars_ctrl_ops = {
207 .s_ctrl = mars_s_ctrl,
210 /* this function is called at probe time */
211 static int sd_init_controls(struct gspca_dev *gspca_dev)
213 struct sd *sd = (struct sd *) gspca_dev;
214 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
216 gspca_dev->vdev.ctrl_handler = hdl;
217 v4l2_ctrl_handler_init(hdl, 7);
218 sd->brightness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
219 V4L2_CID_BRIGHTNESS, 0, 30, 1, 15);
220 sd->saturation = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
221 V4L2_CID_SATURATION, 0, 255, 1, 200);
222 sd->gamma = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
223 V4L2_CID_GAMMA, 0, 3, 1, 1);
224 sd->sharpness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
225 V4L2_CID_SHARPNESS, 0, 2, 1, 1);
226 sd->illum_top = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
227 V4L2_CID_ILLUMINATORS_1, 0, 1, 1, 0);
228 sd->illum_top->flags |= V4L2_CTRL_FLAG_UPDATE;
229 sd->illum_bottom = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
230 V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
231 sd->illum_bottom->flags |= V4L2_CTRL_FLAG_UPDATE;
232 sd->jpegqual = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
233 V4L2_CID_JPEG_COMPRESSION_QUALITY,
234 QUALITY_MIN, QUALITY_MAX, 1, QUALITY_DEF);
236 pr_err("Could not initialize controls\n");
239 v4l2_ctrl_cluster(2, &sd->illum_top);
243 /* this function is called at probe time */
244 static int sd_config(struct gspca_dev *gspca_dev,
245 const struct usb_device_id *id)
247 struct sd *sd = (struct sd *) gspca_dev;
250 cam = &gspca_dev->cam;
251 cam->cam_mode = vga_mode;
252 cam->nmodes = ARRAY_SIZE(vga_mode);
253 sd->quality = QUALITY_DEF;
257 /* this function is called at probe and resume time */
258 static int sd_init(struct gspca_dev *gspca_dev)
263 static int sd_start(struct gspca_dev *gspca_dev)
265 struct sd *sd = (struct sd *) gspca_dev;
269 /* create the JPEG header */
270 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
271 0x21); /* JPEG 422 */
272 jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual));
274 data = gspca_dev->usb_buf;
276 data[0] = 0x01; /* address */
281 Initialize the MR97113 chip register
283 data[0] = 0x00; /* address */
284 data[1] = 0x0c | 0x01; /* reg 0 */
285 data[2] = 0x01; /* reg 1 */
286 data[3] = gspca_dev->width / 8; /* h_size , reg 2 */
287 data[4] = gspca_dev->height / 8; /* v_size , reg 3 */
288 data[5] = 0x30; /* reg 4, MI, PAS5101 :
289 * 0x30 for 24mhz , 0x28 for 12mhz */
290 data[6] = 0x02; /* reg 5, H start - was 0x04 */
291 data[7] = v4l2_ctrl_g_ctrl(sd->gamma) * 0x40; /* reg 0x06: gamma */
292 data[8] = 0x01; /* reg 7, V start - was 0x03 */
293 /* if (h_size == 320 ) */
294 /* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
296 data[9] = 0x52; /* reg 8, 24MHz, no scale down */
297 /*jfm: from win trace*/
300 reg_w(gspca_dev, 11);
302 data[0] = 0x23; /* address */
303 data[1] = 0x09; /* reg 35, append frame header */
307 data[0] = 0x3c; /* address */
308 /* if (gspca_dev->width == 1280) */
309 /* data[1] = 200; * reg 60, pc-cam frame size
310 * (unit: 4KB) 800KB */
312 data[1] = 50; /* 50 reg 60, pc-cam frame size
313 * (unit: 4KB) 200KB */
317 data[0] = 0x5e; /* address */
318 data[1] = 0; /* reg 94, Y Gain (auto) */
319 /*jfm: from win trace*/
320 /* reg 0x5f/0x60 (LE) = saturation */
322 * l (5f): xxxx x000 */
323 data[2] = v4l2_ctrl_g_ctrl(sd->saturation) << 3;
324 data[3] = ((v4l2_ctrl_g_ctrl(sd->saturation) >> 2) & 0xf8) | 0x04;
325 data[4] = v4l2_ctrl_g_ctrl(sd->brightness); /* reg 0x61 = brightness */
331 /*jfm: from win trace*/
332 data[1] = v4l2_ctrl_g_ctrl(sd->sharpness) * 4 + 3;
345 /*jfm: win trace - many writes here to reg 0x64*/
347 /* initialize the MI sensor */
348 for (i = 0; i < sizeof mi_data; i++)
349 mi_w(gspca_dev, i + 1, mi_data[i]);
352 data[1] = 0x4d; /* ISOC transferring enable... */
355 setilluminators(gspca_dev, v4l2_ctrl_g_ctrl(sd->illum_top),
356 v4l2_ctrl_g_ctrl(sd->illum_bottom));
358 return gspca_dev->usb_err;
361 static void sd_stopN(struct gspca_dev *gspca_dev)
363 struct sd *sd = (struct sd *) gspca_dev;
365 if (v4l2_ctrl_g_ctrl(sd->illum_top) ||
366 v4l2_ctrl_g_ctrl(sd->illum_bottom)) {
367 setilluminators(gspca_dev, false, false);
371 gspca_dev->usb_buf[0] = 1;
372 gspca_dev->usb_buf[1] = 0;
376 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
377 u8 *data, /* isoc packet */
378 int len) /* iso packet length */
380 struct sd *sd = (struct sd *) gspca_dev;
384 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
387 for (p = 0; p < len - 6; p++) {
388 if (data[0 + p] == 0xff
389 && data[1 + p] == 0xff
390 && data[2 + p] == 0x00
391 && data[3 + p] == 0xff
392 && data[4 + p] == 0x96) {
393 if (data[5 + p] == 0x64
394 || data[5 + p] == 0x65
395 || data[5 + p] == 0x66
396 || data[5 + p] == 0x67) {
397 PDEBUG(D_PACK, "sof offset: %d len: %d",
399 gspca_frame_add(gspca_dev, LAST_PACKET,
402 /* put the JPEG header */
403 gspca_frame_add(gspca_dev, FIRST_PACKET,
404 sd->jpeg_hdr, JPEG_HDR_SZ);
411 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
414 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
415 struct v4l2_jpegcompression *jcomp)
417 struct sd *sd = (struct sd *) gspca_dev;
420 ret = v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
423 jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
427 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
428 struct v4l2_jpegcompression *jcomp)
430 struct sd *sd = (struct sd *) gspca_dev;
432 memset(jcomp, 0, sizeof *jcomp);
433 jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
434 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
435 | V4L2_JPEG_MARKER_DQT;
439 /* sub-driver description */
440 static const struct sd_desc sd_desc = {
444 .init_controls = sd_init_controls,
447 .pkt_scan = sd_pkt_scan,
448 .get_jcomp = sd_get_jcomp,
449 .set_jcomp = sd_set_jcomp,
452 /* -- module initialisation -- */
453 static const struct usb_device_id device_table[] = {
454 {USB_DEVICE(0x093a, 0x050f)},
457 MODULE_DEVICE_TABLE(usb, device_table);
459 /* -- device connect -- */
460 static int sd_probe(struct usb_interface *intf,
461 const struct usb_device_id *id)
463 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
467 static struct usb_driver sd_driver = {
469 .id_table = device_table,
471 .disconnect = gspca_disconnect,
473 .suspend = gspca_suspend,
474 .resume = gspca_resume,
475 .reset_resume = gspca_resume,
479 module_usb_driver(sd_driver);