1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Mars-Semi MR97311A library
4 * Copyright (C) 2005 <bradlch@hotmail.com>
6 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define MODULE_NAME "mars"
16 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
17 MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
18 MODULE_LICENSE("GPL");
22 /* specific webcam descriptor */
24 struct gspca_dev gspca_dev
; /* !! must be the first item */
26 struct v4l2_ctrl
*brightness
;
27 struct v4l2_ctrl
*saturation
;
28 struct v4l2_ctrl
*sharpness
;
29 struct v4l2_ctrl
*gamma
;
30 struct { /* illuminator control cluster */
31 struct v4l2_ctrl
*illum_top
;
32 struct v4l2_ctrl
*illum_bottom
;
34 u8 jpeg_hdr
[JPEG_HDR_SZ
];
37 /* V4L2 controls supported by the driver */
38 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
);
39 static void setcolors(struct gspca_dev
*gspca_dev
, s32 val
);
40 static void setgamma(struct gspca_dev
*gspca_dev
, s32 val
);
41 static void setsharpness(struct gspca_dev
*gspca_dev
, s32 val
);
43 static const struct v4l2_pix_format vga_mode
[] = {
44 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
46 .sizeimage
= 320 * 240 * 3 / 8 + 590,
47 .colorspace
= V4L2_COLORSPACE_JPEG
,
49 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
51 .sizeimage
= 640 * 480 * 3 / 8 + 590,
52 .colorspace
= V4L2_COLORSPACE_JPEG
,
56 static const __u8 mi_data
[0x20] = {
57 /* 01 02 03 04 05 06 07 08 */
58 0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
59 /* 09 0a 0b 0c 0d 0e 0f 10 */
60 0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
61 /* 11 12 13 14 15 16 17 18 */
62 0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
63 /* 19 1a 1b 1c 1d 1e 1f 20 */
64 0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
67 /* write <len> bytes from gspca_dev->usb_buf */
68 static void reg_w(struct gspca_dev
*gspca_dev
,
73 if (gspca_dev
->usb_err
< 0)
76 ret
= usb_bulk_msg(gspca_dev
->dev
,
77 usb_sndbulkpipe(gspca_dev
->dev
, 4),
81 500); /* timeout in milliseconds */
83 pr_err("reg write [%02x] error %d\n",
84 gspca_dev
->usb_buf
[0], ret
);
85 gspca_dev
->usb_err
= ret
;
89 static void mi_w(struct gspca_dev
*gspca_dev
,
93 gspca_dev
->usb_buf
[0] = 0x1f;
94 gspca_dev
->usb_buf
[1] = 0; /* control byte */
95 gspca_dev
->usb_buf
[2] = addr
;
96 gspca_dev
->usb_buf
[3] = value
;
101 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
103 gspca_dev
->usb_buf
[0] = 0x61;
104 gspca_dev
->usb_buf
[1] = val
;
108 static void setcolors(struct gspca_dev
*gspca_dev
, s32 val
)
110 gspca_dev
->usb_buf
[0] = 0x5f;
111 gspca_dev
->usb_buf
[1] = val
<< 3;
112 gspca_dev
->usb_buf
[2] = ((val
>> 2) & 0xf8) | 0x04;
116 static void setgamma(struct gspca_dev
*gspca_dev
, s32 val
)
118 gspca_dev
->usb_buf
[0] = 0x06;
119 gspca_dev
->usb_buf
[1] = val
* 0x40;
123 static void setsharpness(struct gspca_dev
*gspca_dev
, s32 val
)
125 gspca_dev
->usb_buf
[0] = 0x67;
126 gspca_dev
->usb_buf
[1] = val
* 4 + 3;
130 static void setilluminators(struct gspca_dev
*gspca_dev
, bool top
, bool bottom
)
132 /* both are off if not streaming */
133 gspca_dev
->usb_buf
[0] = 0x22;
135 gspca_dev
->usb_buf
[1] = 0x76;
137 gspca_dev
->usb_buf
[1] = 0x7a;
139 gspca_dev
->usb_buf
[1] = 0x7e;
143 static int mars_s_ctrl(struct v4l2_ctrl
*ctrl
)
145 struct gspca_dev
*gspca_dev
=
146 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
147 struct sd
*sd
= (struct sd
*)gspca_dev
;
149 gspca_dev
->usb_err
= 0;
151 if (ctrl
->id
== V4L2_CID_ILLUMINATORS_1
) {
152 /* only one can be on at a time */
153 if (ctrl
->is_new
&& ctrl
->val
)
154 sd
->illum_bottom
->val
= 0;
155 if (sd
->illum_bottom
->is_new
&& sd
->illum_bottom
->val
)
156 sd
->illum_top
->val
= 0;
159 if (!gspca_dev
->streaming
)
163 case V4L2_CID_BRIGHTNESS
:
164 setbrightness(gspca_dev
, ctrl
->val
);
166 case V4L2_CID_SATURATION
:
167 setcolors(gspca_dev
, ctrl
->val
);
170 setgamma(gspca_dev
, ctrl
->val
);
172 case V4L2_CID_ILLUMINATORS_1
:
173 setilluminators(gspca_dev
, sd
->illum_top
->val
,
174 sd
->illum_bottom
->val
);
176 case V4L2_CID_SHARPNESS
:
177 setsharpness(gspca_dev
, ctrl
->val
);
182 return gspca_dev
->usb_err
;
185 static const struct v4l2_ctrl_ops mars_ctrl_ops
= {
186 .s_ctrl
= mars_s_ctrl
,
189 /* this function is called at probe time */
190 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
192 struct sd
*sd
= (struct sd
*) gspca_dev
;
193 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
195 gspca_dev
->vdev
.ctrl_handler
= hdl
;
196 v4l2_ctrl_handler_init(hdl
, 6);
197 sd
->brightness
= v4l2_ctrl_new_std(hdl
, &mars_ctrl_ops
,
198 V4L2_CID_BRIGHTNESS
, 0, 30, 1, 15);
199 sd
->saturation
= v4l2_ctrl_new_std(hdl
, &mars_ctrl_ops
,
200 V4L2_CID_SATURATION
, 0, 255, 1, 200);
201 sd
->gamma
= v4l2_ctrl_new_std(hdl
, &mars_ctrl_ops
,
202 V4L2_CID_GAMMA
, 0, 3, 1, 1);
203 sd
->sharpness
= v4l2_ctrl_new_std(hdl
, &mars_ctrl_ops
,
204 V4L2_CID_SHARPNESS
, 0, 2, 1, 1);
205 sd
->illum_top
= v4l2_ctrl_new_std(hdl
, &mars_ctrl_ops
,
206 V4L2_CID_ILLUMINATORS_1
, 0, 1, 1, 0);
207 sd
->illum_top
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
208 sd
->illum_bottom
= v4l2_ctrl_new_std(hdl
, &mars_ctrl_ops
,
209 V4L2_CID_ILLUMINATORS_2
, 0, 1, 1, 0);
210 sd
->illum_bottom
->flags
|= V4L2_CTRL_FLAG_UPDATE
;
212 pr_err("Could not initialize controls\n");
215 v4l2_ctrl_cluster(2, &sd
->illum_top
);
219 /* this function is called at probe time */
220 static int sd_config(struct gspca_dev
*gspca_dev
,
221 const struct usb_device_id
*id
)
225 cam
= &gspca_dev
->cam
;
226 cam
->cam_mode
= vga_mode
;
227 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
231 /* this function is called at probe and resume time */
232 static int sd_init(struct gspca_dev
*gspca_dev
)
237 static int sd_start(struct gspca_dev
*gspca_dev
)
239 struct sd
*sd
= (struct sd
*) gspca_dev
;
243 /* create the JPEG header */
244 jpeg_define(sd
->jpeg_hdr
, gspca_dev
->pixfmt
.height
,
245 gspca_dev
->pixfmt
.width
,
246 0x21); /* JPEG 422 */
247 jpeg_set_qual(sd
->jpeg_hdr
, QUALITY
);
249 data
= gspca_dev
->usb_buf
;
251 data
[0] = 0x01; /* address */
256 Initialize the MR97113 chip register
258 data
[0] = 0x00; /* address */
259 data
[1] = 0x0c | 0x01; /* reg 0 */
260 data
[2] = 0x01; /* reg 1 */
261 data
[3] = gspca_dev
->pixfmt
.width
/ 8; /* h_size , reg 2 */
262 data
[4] = gspca_dev
->pixfmt
.height
/ 8; /* v_size , reg 3 */
263 data
[5] = 0x30; /* reg 4, MI, PAS5101 :
264 * 0x30 for 24mhz , 0x28 for 12mhz */
265 data
[6] = 0x02; /* reg 5, H start - was 0x04 */
266 data
[7] = v4l2_ctrl_g_ctrl(sd
->gamma
) * 0x40; /* reg 0x06: gamma */
267 data
[8] = 0x01; /* reg 7, V start - was 0x03 */
268 /* if (h_size == 320 ) */
269 /* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
271 data
[9] = 0x52; /* reg 8, 24MHz, no scale down */
272 /*jfm: from win trace*/
275 reg_w(gspca_dev
, 11);
277 data
[0] = 0x23; /* address */
278 data
[1] = 0x09; /* reg 35, append frame header */
282 data
[0] = 0x3c; /* address */
283 /* if (gspca_dev->width == 1280) */
284 /* data[1] = 200; * reg 60, pc-cam frame size
285 * (unit: 4KB) 800KB */
287 data
[1] = 50; /* 50 reg 60, pc-cam frame size
288 * (unit: 4KB) 200KB */
292 data
[0] = 0x5e; /* address */
293 data
[1] = 0; /* reg 94, Y Gain (auto) */
294 /*jfm: from win trace*/
295 /* reg 0x5f/0x60 (LE) = saturation */
297 * l (5f): xxxx x000 */
298 data
[2] = v4l2_ctrl_g_ctrl(sd
->saturation
) << 3;
299 data
[3] = ((v4l2_ctrl_g_ctrl(sd
->saturation
) >> 2) & 0xf8) | 0x04;
300 data
[4] = v4l2_ctrl_g_ctrl(sd
->brightness
); /* reg 0x61 = brightness */
306 /*jfm: from win trace*/
307 data
[1] = v4l2_ctrl_g_ctrl(sd
->sharpness
) * 4 + 3;
320 /*jfm: win trace - many writes here to reg 0x64*/
322 /* initialize the MI sensor */
323 for (i
= 0; i
< sizeof mi_data
; i
++)
324 mi_w(gspca_dev
, i
+ 1, mi_data
[i
]);
327 data
[1] = 0x4d; /* ISOC transferring enable... */
330 setilluminators(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->illum_top
),
331 v4l2_ctrl_g_ctrl(sd
->illum_bottom
));
333 return gspca_dev
->usb_err
;
336 static void sd_stopN(struct gspca_dev
*gspca_dev
)
338 struct sd
*sd
= (struct sd
*) gspca_dev
;
340 if (v4l2_ctrl_g_ctrl(sd
->illum_top
) ||
341 v4l2_ctrl_g_ctrl(sd
->illum_bottom
)) {
342 setilluminators(gspca_dev
, false, false);
346 gspca_dev
->usb_buf
[0] = 1;
347 gspca_dev
->usb_buf
[1] = 0;
351 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
352 u8
*data
, /* isoc packet */
353 int len
) /* iso packet length */
355 struct sd
*sd
= (struct sd
*) gspca_dev
;
359 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
362 for (p
= 0; p
< len
- 6; p
++) {
363 if (data
[0 + p
] == 0xff
364 && data
[1 + p
] == 0xff
365 && data
[2 + p
] == 0x00
366 && data
[3 + p
] == 0xff
367 && data
[4 + p
] == 0x96) {
368 if (data
[5 + p
] == 0x64
369 || data
[5 + p
] == 0x65
370 || data
[5 + p
] == 0x66
371 || data
[5 + p
] == 0x67) {
372 gspca_dbg(gspca_dev
, D_PACK
, "sof offset: %d len: %d\n",
374 gspca_frame_add(gspca_dev
, LAST_PACKET
,
377 /* put the JPEG header */
378 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
379 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
386 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
389 /* sub-driver description */
390 static const struct sd_desc sd_desc
= {
394 .init_controls
= sd_init_controls
,
397 .pkt_scan
= sd_pkt_scan
,
400 /* -- module initialisation -- */
401 static const struct usb_device_id device_table
[] = {
402 {USB_DEVICE(0x093a, 0x050f)},
405 MODULE_DEVICE_TABLE(usb
, device_table
);
407 /* -- device connect -- */
408 static int sd_probe(struct usb_interface
*intf
,
409 const struct usb_device_id
*id
)
411 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
415 static struct usb_driver sd_driver
= {
417 .id_table
= device_table
,
419 .disconnect
= gspca_disconnect
,
421 .suspend
= gspca_suspend
,
422 .resume
= gspca_resume
,
423 .reset_resume
= gspca_resume
,
427 module_usb_driver(sd_driver
);