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");
35 /* specific webcam descriptor */
37 struct gspca_dev gspca_dev
; /* !! must be the first item */
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
;
47 u8 jpeg_hdr
[JPEG_HDR_SZ
];
50 /* V4L2 controls supported by the driver */
51 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
);
52 static void setcolors(struct gspca_dev
*gspca_dev
, s32 val
);
53 static void setgamma(struct gspca_dev
*gspca_dev
, s32 val
);
54 static void setsharpness(struct gspca_dev
*gspca_dev
, s32 val
);
56 static const struct v4l2_pix_format vga_mode
[] = {
57 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
59 .sizeimage
= 320 * 240 * 3 / 8 + 590,
60 .colorspace
= V4L2_COLORSPACE_JPEG
,
62 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
64 .sizeimage
= 640 * 480 * 3 / 8 + 590,
65 .colorspace
= V4L2_COLORSPACE_JPEG
,
69 static 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
80 /* write <len> bytes from gspca_dev->usb_buf */
81 static void reg_w(struct gspca_dev
*gspca_dev
,
86 if (gspca_dev
->usb_err
< 0)
89 ret
= usb_bulk_msg(gspca_dev
->dev
,
90 usb_sndbulkpipe(gspca_dev
->dev
, 4),
94 500); /* timeout in milliseconds */
96 pr_err("reg write [%02x] error %d\n",
97 gspca_dev
->usb_buf
[0], ret
);
98 gspca_dev
->usb_err
= ret
;
102 static void mi_w(struct gspca_dev
*gspca_dev
,
106 gspca_dev
->usb_buf
[0] = 0x1f;
107 gspca_dev
->usb_buf
[1] = 0; /* control byte */
108 gspca_dev
->usb_buf
[2] = addr
;
109 gspca_dev
->usb_buf
[3] = value
;
114 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
116 gspca_dev
->usb_buf
[0] = 0x61;
117 gspca_dev
->usb_buf
[1] = val
;
121 static void setcolors(struct gspca_dev
*gspca_dev
, s32 val
)
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;
129 static void setgamma(struct gspca_dev
*gspca_dev
, s32 val
)
131 gspca_dev
->usb_buf
[0] = 0x06;
132 gspca_dev
->usb_buf
[1] = val
* 0x40;
136 static void setsharpness(struct gspca_dev
*gspca_dev
, s32 val
)
138 gspca_dev
->usb_buf
[0] = 0x67;
139 gspca_dev
->usb_buf
[1] = val
* 4 + 3;
143 static void setilluminators(struct gspca_dev
*gspca_dev
, bool top
, bool bottom
)
145 /* both are off if not streaming */
146 gspca_dev
->usb_buf
[0] = 0x22;
148 gspca_dev
->usb_buf
[1] = 0x76;
150 gspca_dev
->usb_buf
[1] = 0x7a;
152 gspca_dev
->usb_buf
[1] = 0x7e;
156 static int mars_s_ctrl(struct v4l2_ctrl
*ctrl
)
158 struct gspca_dev
*gspca_dev
=
159 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
160 struct sd
*sd
= (struct sd
*)gspca_dev
;
162 gspca_dev
->usb_err
= 0;
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;
172 if (!gspca_dev
->streaming
)
176 case V4L2_CID_BRIGHTNESS
:
177 setbrightness(gspca_dev
, ctrl
->val
);
179 case V4L2_CID_SATURATION
:
180 setcolors(gspca_dev
, ctrl
->val
);
183 setgamma(gspca_dev
, ctrl
->val
);
185 case V4L2_CID_ILLUMINATORS_1
:
186 setilluminators(gspca_dev
, sd
->illum_top
->val
,
187 sd
->illum_bottom
->val
);
189 case V4L2_CID_SHARPNESS
:
190 setsharpness(gspca_dev
, ctrl
->val
);
195 return gspca_dev
->usb_err
;
198 static const struct v4l2_ctrl_ops mars_ctrl_ops
= {
199 .s_ctrl
= mars_s_ctrl
,
202 /* this function is called at probe time */
203 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
205 struct sd
*sd
= (struct sd
*) gspca_dev
;
206 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
208 gspca_dev
->vdev
.ctrl_handler
= hdl
;
209 v4l2_ctrl_handler_init(hdl
, 6);
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
;
225 pr_err("Could not initialize controls\n");
228 v4l2_ctrl_cluster(2, &sd
->illum_top
);
232 /* this function is called at probe time */
233 static int sd_config(struct gspca_dev
*gspca_dev
,
234 const struct usb_device_id
*id
)
238 cam
= &gspca_dev
->cam
;
239 cam
->cam_mode
= vga_mode
;
240 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
244 /* this function is called at probe and resume time */
245 static int sd_init(struct gspca_dev
*gspca_dev
)
250 static int sd_start(struct gspca_dev
*gspca_dev
)
252 struct sd
*sd
= (struct sd
*) gspca_dev
;
256 /* create the JPEG header */
257 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->height
, gspca_dev
->width
,
258 0x21); /* JPEG 422 */
259 jpeg_set_qual(sd
->jpeg_hdr
, QUALITY
);
261 data
= gspca_dev
->usb_buf
;
263 data
[0] = 0x01; /* address */
268 Initialize the MR97113 chip register
270 data
[0] = 0x00; /* address */
271 data
[1] = 0x0c | 0x01; /* reg 0 */
272 data
[2] = 0x01; /* reg 1 */
273 data
[3] = gspca_dev
->width
/ 8; /* h_size , reg 2 */
274 data
[4] = gspca_dev
->height
/ 8; /* v_size , reg 3 */
275 data
[5] = 0x30; /* reg 4, MI, PAS5101 :
276 * 0x30 for 24mhz , 0x28 for 12mhz */
277 data
[6] = 0x02; /* reg 5, H start - was 0x04 */
278 data
[7] = v4l2_ctrl_g_ctrl(sd
->gamma
) * 0x40; /* reg 0x06: gamma */
279 data
[8] = 0x01; /* reg 7, V start - was 0x03 */
280 /* if (h_size == 320 ) */
281 /* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
283 data
[9] = 0x52; /* reg 8, 24MHz, no scale down */
284 /*jfm: from win trace*/
287 reg_w(gspca_dev
, 11);
289 data
[0] = 0x23; /* address */
290 data
[1] = 0x09; /* reg 35, append frame header */
294 data
[0] = 0x3c; /* address */
295 /* if (gspca_dev->width == 1280) */
296 /* data[1] = 200; * reg 60, pc-cam frame size
297 * (unit: 4KB) 800KB */
299 data
[1] = 50; /* 50 reg 60, pc-cam frame size
300 * (unit: 4KB) 200KB */
304 data
[0] = 0x5e; /* address */
305 data
[1] = 0; /* reg 94, Y Gain (auto) */
306 /*jfm: from win trace*/
307 /* reg 0x5f/0x60 (LE) = saturation */
309 * l (5f): xxxx x000 */
310 data
[2] = v4l2_ctrl_g_ctrl(sd
->saturation
) << 3;
311 data
[3] = ((v4l2_ctrl_g_ctrl(sd
->saturation
) >> 2) & 0xf8) | 0x04;
312 data
[4] = v4l2_ctrl_g_ctrl(sd
->brightness
); /* reg 0x61 = brightness */
318 /*jfm: from win trace*/
319 data
[1] = v4l2_ctrl_g_ctrl(sd
->sharpness
) * 4 + 3;
332 /*jfm: win trace - many writes here to reg 0x64*/
334 /* initialize the MI sensor */
335 for (i
= 0; i
< sizeof mi_data
; i
++)
336 mi_w(gspca_dev
, i
+ 1, mi_data
[i
]);
339 data
[1] = 0x4d; /* ISOC transferring enable... */
342 setilluminators(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->illum_top
),
343 v4l2_ctrl_g_ctrl(sd
->illum_bottom
));
345 return gspca_dev
->usb_err
;
348 static void sd_stopN(struct gspca_dev
*gspca_dev
)
350 struct sd
*sd
= (struct sd
*) gspca_dev
;
352 if (v4l2_ctrl_g_ctrl(sd
->illum_top
) ||
353 v4l2_ctrl_g_ctrl(sd
->illum_bottom
)) {
354 setilluminators(gspca_dev
, false, false);
358 gspca_dev
->usb_buf
[0] = 1;
359 gspca_dev
->usb_buf
[1] = 0;
363 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
364 u8
*data
, /* isoc packet */
365 int len
) /* iso packet length */
367 struct sd
*sd
= (struct sd
*) gspca_dev
;
371 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
374 for (p
= 0; p
< len
- 6; p
++) {
375 if (data
[0 + p
] == 0xff
376 && data
[1 + p
] == 0xff
377 && data
[2 + p
] == 0x00
378 && data
[3 + p
] == 0xff
379 && data
[4 + p
] == 0x96) {
380 if (data
[5 + p
] == 0x64
381 || data
[5 + p
] == 0x65
382 || data
[5 + p
] == 0x66
383 || data
[5 + p
] == 0x67) {
384 PDEBUG(D_PACK
, "sof offset: %d len: %d",
386 gspca_frame_add(gspca_dev
, LAST_PACKET
,
389 /* put the JPEG header */
390 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
391 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
398 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
401 /* sub-driver description */
402 static const struct sd_desc sd_desc
= {
406 .init_controls
= sd_init_controls
,
409 .pkt_scan
= sd_pkt_scan
,
412 /* -- module initialisation -- */
413 static const struct usb_device_id device_table
[] = {
414 {USB_DEVICE(0x093a, 0x050f)},
417 MODULE_DEVICE_TABLE(usb
, device_table
);
419 /* -- device connect -- */
420 static int sd_probe(struct usb_interface
*intf
,
421 const struct usb_device_id
*id
)
423 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
427 static struct usb_driver sd_driver
= {
429 .id_table
= device_table
,
431 .disconnect
= gspca_disconnect
,
433 .suspend
= gspca_suspend
,
434 .resume
= gspca_resume
,
435 .reset_resume
= gspca_resume
,
439 module_usb_driver(sd_driver
);