blob: 88c2b02f380a191064b32de47454842595021d00 [file] [log] [blame]
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001/*
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
22#define MODULE_NAME "mars"
23
24#include "gspca.h"
25#include "jpeg.h"
26
Jean-Francois Moine739570b2008-07-14 09:38:29 -030027#define DRIVER_VERSION_NUMBER KERNEL_VERSION(2, 1, 7)
28static const char version[] = "2.1.7";
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030029
30MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
31MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
32MODULE_LICENSE("GPL");
33
34/* specific webcam descriptor */
35struct sd {
36 struct gspca_dev gspca_dev; /* !! must be the first item */
37
38 char qindex;
39};
40
41/* V4L2 controls supported by the driver */
42static struct ctrl sd_ctrls[] = {
43};
44
Jean-Francois Moinec2446b32008-07-05 11:49:20 -030045static struct v4l2_pix_format vga_mode[] = {
46 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
47 .bytesperline = 320,
48 .sizeimage = 320 * 240 * 3 / 8 + 589,
49 .colorspace = V4L2_COLORSPACE_JPEG,
50 .priv = 2},
51 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
52 .bytesperline = 640,
53 .sizeimage = 640 * 480 * 3 / 8 + 590,
54 .colorspace = V4L2_COLORSPACE_JPEG,
55 .priv = 1},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030056};
57
58/* MI Register table //elvis */
59enum {
60 REG_HW_MI_0,
61 REG_HW_MI_1,
62 REG_HW_MI_2,
63 REG_HW_MI_3,
64 REG_HW_MI_4,
65 REG_HW_MI_5,
66 REG_HW_MI_6,
67 REG_HW_MI_7,
68 REG_HW_MI_9 = 0x09,
69 REG_HW_MI_B = 0x0B,
70 REG_HW_MI_C,
71 REG_HW_MI_D,
72 REG_HW_MI_1E = 0x1E,
73 REG_HW_MI_20 = 0x20,
74 REG_HW_MI_2B = 0x2B,
75 REG_HW_MI_2C,
76 REG_HW_MI_2D,
77 REG_HW_MI_2E,
78 REG_HW_MI_35 = 0x35,
79 REG_HW_MI_5F = 0x5f,
80 REG_HW_MI_60,
81 REG_HW_MI_61,
82 REG_HW_MI_62,
83 REG_HW_MI_63,
84 REG_HW_MI_64,
85 REG_HW_MI_F1 = 0xf1,
Jean-Francois Moine739570b2008-07-14 09:38:29 -030086 ATTR_TOTAL_MI_REG = 0xf2
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030087};
88
Jean-Francois Moine739570b2008-07-14 09:38:29 -030089/* the bytes to write are in gspca_dev->usb_buf */
90static int reg_w(struct gspca_dev *gspca_dev,
91 __u16 index, int len)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030092{
93 int rc;
94
Jean-Francois Moine739570b2008-07-14 09:38:29 -030095 rc = usb_control_msg(gspca_dev->dev,
96 usb_sndbulkpipe(gspca_dev->dev, 4),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030097 0x12,
Jean-Francois Moine739570b2008-07-14 09:38:29 -030098 0xc8, /* ?? */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030099 0, /* value */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300100 index, gspca_dev->usb_buf, len, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300101 if (rc < 0)
Jean-Francois Moinebf7f0b92008-07-03 11:09:12 -0300102 PDEBUG(D_ERR, "reg write [%02x] error %d", index, rc);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300103 return rc;
104}
105
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300106static int reg_w_buf(struct gspca_dev *gspca_dev,
107 __u16 index, __u8 *buf, int len)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300108{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300109 int rc;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300110
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300111 rc = usb_control_msg(gspca_dev->dev,
112 usb_sndbulkpipe(gspca_dev->dev, 4),
113 0x12,
114 0xc8, /* ?? */
115 0, /* value */
116 index, buf, len, 500);
117 if (rc < 0)
118 PDEBUG(D_ERR, "reg write [%02x] error %d", index, rc);
119 return rc;
120}
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300121
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300122static void bulk_w(struct gspca_dev *gspca_dev,
123 __u16 *pch,
124 __u16 Address)
125{
126 gspca_dev->usb_buf[0] = 0x1f;
127 gspca_dev->usb_buf[1] = 0; /* control byte */
128 gspca_dev->usb_buf[2] = Address;
129 gspca_dev->usb_buf[3] = *pch >> 8; /* high byte */
130 gspca_dev->usb_buf[4] = *pch; /* low byte */
131
132 reg_w(gspca_dev, Address, 5);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300133}
134
135/* this function is called at probe time */
136static int sd_config(struct gspca_dev *gspca_dev,
137 const struct usb_device_id *id)
138{
139 struct sd *sd = (struct sd *) gspca_dev;
140 struct cam *cam;
141
142 cam = &gspca_dev->cam;
143 cam->dev_name = (char *) id->driver_info;
144 cam->epaddr = 0x01;
145 cam->cam_mode = vga_mode;
146 cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
147 sd->qindex = 1; /* set the quantization table */
148 return 0;
149}
150
151/* this function is called at open time */
152static int sd_open(struct gspca_dev *gspca_dev)
153{
154 return 0;
155}
156
157static void sd_start(struct gspca_dev *gspca_dev)
158{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300159 int err_code;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300160 __u8 *data;
161 __u16 *MI_buf;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300162 int h_size, v_size;
163 int intpipe;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300164
165 PDEBUG(D_STREAM, "camera start, iface %d, alt 8", gspca_dev->iface);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300166 if (usb_set_interface(gspca_dev->dev, gspca_dev->iface, 8) < 0) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300167 PDEBUG(D_ERR|D_STREAM, "Set packet size: set interface error");
168 return;
169 }
170
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300171 data = gspca_dev->usb_buf;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300172 data[0] = 0x01; /* address */
173 data[1] = 0x01;
174
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300175 err_code = reg_w(gspca_dev, data[0], 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300176 if (err_code < 0)
177 return;
178
179 /*
180 Initialize the MR97113 chip register
181 */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300182 data = kmalloc(16, GFP_KERNEL);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300183 data[0] = 0x00; /* address */
184 data[1] = 0x0c | 0x01; /* reg 0 */
185 data[2] = 0x01; /* reg 1 */
186 h_size = gspca_dev->width;
187 v_size = gspca_dev->height;
188 data[3] = h_size / 8; /* h_size , reg 2 */
189 data[4] = v_size / 8; /* v_size , reg 3 */
190 data[5] = 0x30; /* reg 4, MI, PAS5101 :
191 * 0x30 for 24mhz , 0x28 for 12mhz */
192 data[6] = 4; /* reg 5, H start */
193 data[7] = 0xc0; /* reg 6, gamma 1.5 */
194 data[8] = 3; /* reg 7, V start */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300195/* if (h_size == 320 ) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300196/* data[9]= 0x56; * reg 8, 24MHz, 2:1 scale down */
197/* else */
198 data[9] = 0x52; /* reg 8, 24MHz, no scale down */
199 data[10] = 0x5d; /* reg 9, I2C device address
200 * [for PAS5101 (0x40)] [for MI (0x5d)] */
201
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300202 err_code = reg_w_buf(gspca_dev, data[0], data, 11);
203 kfree(data);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300204 if (err_code < 0)
205 return;
206
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300207 data = gspca_dev->usb_buf;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300208 data[0] = 0x23; /* address */
209 data[1] = 0x09; /* reg 35, append frame header */
210
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300211 err_code = reg_w(gspca_dev, data[0], 2);
212 if (err_code < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300213 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300214
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300215 data[0] = 0x3c; /* address */
216/* if (gspca_dev->width == 1280) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300217/* data[1] = 200; * reg 60, pc-cam frame size
218 * (unit: 4KB) 800KB */
219/* else */
220 data[1] = 50; /* 50 reg 60, pc-cam frame size
221 * (unit: 4KB) 200KB */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300222 err_code = reg_w(gspca_dev, data[0], 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300223 if (err_code < 0)
224 return;
225
226 if (0) { /* fixed dark-gain */
227 data[1] = 0; /* reg 94, Y Gain (1.75) */
228 data[2] = 0; /* reg 95, UV Gain (1.75) */
Jean-Francois Moine956e42d2008-07-01 10:03:42 -0300229 data[3] = 0x3f; /* reg 96, Y Gain/UV Gain/disable
230 * auto dark-gain */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300231 data[4] = 0; /* reg 97, set fixed dark level */
232 data[5] = 0; /* reg 98, don't care */
233 } else { /* auto dark-gain */
234 data[1] = 0; /* reg 94, Y Gain (auto) */
235 data[2] = 0; /* reg 95, UV Gain (1.75) */
Jean-Francois Moine956e42d2008-07-01 10:03:42 -0300236 data[3] = 0x78; /* reg 96, Y Gain/UV Gain/disable
237 * auto dark-gain */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300238 switch (gspca_dev->width) {
239/* case 1280: */
240/* data[4] = 154;
241 * reg 97, %3 shadow point (unit: 256 pixel) */
242/* data[5] = 51;
243 * reg 98, %1 highlight point
244 * (uint: 256 pixel) */
245/* break; */
246 default:
247/* case 640: */
248 data[4] = 36; /* reg 97, %3 shadow point
249 * (unit: 256 pixel) */
250 data[5] = 12; /* reg 98, %1 highlight point
251 * (uint: 256 pixel) */
252 break;
253 case 320:
254 data[4] = 9; /* reg 97, %3 shadow point
255 * (unit: 256 pixel) */
256 data[5] = 3; /* reg 98, %1 highlight point
257 * (uint: 256 pixel) */
258 break;
259 }
260 }
261 /* auto dark-gain */
262 data[0] = 0x5e; /* address */
263
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300264 err_code = reg_w(gspca_dev, data[0], 6);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300265 if (err_code < 0)
266 return;
267
268 data[0] = 0x67;
269 data[1] = 0x13; /* reg 103, first pixel B, disable sharpness */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300270 err_code = reg_w(gspca_dev, data[0], 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300271 if (err_code < 0)
272 return;
273
274 /*
275 * initialize the value of MI sensor...
276 */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300277 MI_buf = kzalloc(ATTR_TOTAL_MI_REG * sizeof *MI_buf, GFP_KERNEL);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300278 MI_buf[REG_HW_MI_1] = 0x000a;
279 MI_buf[REG_HW_MI_2] = 0x000c;
280 MI_buf[REG_HW_MI_3] = 0x0405;
281 MI_buf[REG_HW_MI_4] = 0x0507;
282 /* mi_Attr_Reg_[REG_HW_MI_5] = 0x01ff;//13 */
283 MI_buf[REG_HW_MI_5] = 0x0013; /* 13 */
284 MI_buf[REG_HW_MI_6] = 0x001f; /* vertical blanking */
285 /* mi_Attr_Reg_[REG_HW_MI_6] = 0x0400; // vertical blanking */
286 MI_buf[REG_HW_MI_7] = 0x0002;
287 /* mi_Attr_Reg_[REG_HW_MI_9] = 0x015f; */
288 /* mi_Attr_Reg_[REG_HW_MI_9] = 0x030f; */
289 MI_buf[REG_HW_MI_9] = 0x0374;
290 MI_buf[REG_HW_MI_B] = 0x0000;
291 MI_buf[REG_HW_MI_C] = 0x0000;
292 MI_buf[REG_HW_MI_D] = 0x0000;
293 MI_buf[REG_HW_MI_1E] = 0x8000;
294/* mi_Attr_Reg_[REG_HW_MI_20] = 0x1104; */
295 MI_buf[REG_HW_MI_20] = 0x1104; /* 0x111c; */
296 MI_buf[REG_HW_MI_2B] = 0x0008;
297/* mi_Attr_Reg_[REG_HW_MI_2C] = 0x000f; */
298 MI_buf[REG_HW_MI_2C] = 0x001f; /* lita suggest */
299 MI_buf[REG_HW_MI_2D] = 0x0008;
300 MI_buf[REG_HW_MI_2E] = 0x0008;
301 MI_buf[REG_HW_MI_35] = 0x0051;
302 MI_buf[REG_HW_MI_5F] = 0x0904; /* fail to write */
303 MI_buf[REG_HW_MI_60] = 0x0000;
304 MI_buf[REG_HW_MI_61] = 0x0000;
305 MI_buf[REG_HW_MI_62] = 0x0498;
306 MI_buf[REG_HW_MI_63] = 0x0000;
307 MI_buf[REG_HW_MI_64] = 0x0000;
308 MI_buf[REG_HW_MI_F1] = 0x0001;
309 /* changing while setting up the different value of dx/dy */
310
311 if (gspca_dev->width != 1280) {
312 MI_buf[0x01] = 0x010a;
313 MI_buf[0x02] = 0x014c;
314 MI_buf[0x03] = 0x01e5;
315 MI_buf[0x04] = 0x0287;
316 }
317 MI_buf[0x20] = 0x1104;
318
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300319 bulk_w(gspca_dev, MI_buf + 1, 1);
320 bulk_w(gspca_dev, MI_buf + 2, 2);
321 bulk_w(gspca_dev, MI_buf + 3, 3);
322 bulk_w(gspca_dev, MI_buf + 4, 4);
323 bulk_w(gspca_dev, MI_buf + 5, 5);
324 bulk_w(gspca_dev, MI_buf + 6, 6);
325 bulk_w(gspca_dev, MI_buf + 7, 7);
326 bulk_w(gspca_dev, MI_buf + 9, 9);
327 bulk_w(gspca_dev, MI_buf + 0x0b, 0x0b);
328 bulk_w(gspca_dev, MI_buf + 0x0c, 0x0c);
329 bulk_w(gspca_dev, MI_buf + 0x0d, 0x0d);
330 bulk_w(gspca_dev, MI_buf + 0x1e, 0x1e);
331 bulk_w(gspca_dev, MI_buf + 0x20, 0x20);
332 bulk_w(gspca_dev, MI_buf + 0x2b, 0x2b);
333 bulk_w(gspca_dev, MI_buf + 0x2c, 0x2c);
334 bulk_w(gspca_dev, MI_buf + 0x2d, 0x2d);
335 bulk_w(gspca_dev, MI_buf + 0x2e, 0x2e);
336 bulk_w(gspca_dev, MI_buf + 0x35, 0x35);
337 bulk_w(gspca_dev, MI_buf + 0x5f, 0x5f);
338 bulk_w(gspca_dev, MI_buf + 0x60, 0x60);
339 bulk_w(gspca_dev, MI_buf + 0x61, 0x61);
340 bulk_w(gspca_dev, MI_buf + 0x62, 0x62);
341 bulk_w(gspca_dev, MI_buf + 0x63, 0x63);
342 bulk_w(gspca_dev, MI_buf + 0x64, 0x64);
343 bulk_w(gspca_dev, MI_buf + 0xf1, 0xf1);
344 kfree(MI_buf);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300345
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300346 intpipe = usb_sndintpipe(gspca_dev->dev, 0);
347 err_code = usb_clear_halt(gspca_dev->dev, intpipe);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300348
349 data[0] = 0x00;
350 data[1] = 0x4d; /* ISOC transfering enable... */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300351 reg_w(gspca_dev, data[0], 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300352}
353
354static void sd_stopN(struct gspca_dev *gspca_dev)
355{
356 int result;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300357
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300358 gspca_dev->usb_buf[0] = 1;
359 gspca_dev->usb_buf[1] = 0;
360 result = reg_w(gspca_dev, gspca_dev->usb_buf[0], 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300361 if (result < 0)
362 PDEBUG(D_ERR, "Camera Stop failed");
363}
364
365static void sd_stop0(struct gspca_dev *gspca_dev)
366{
367}
368
369static void sd_close(struct gspca_dev *gspca_dev)
370{
371}
372
373static void sd_pkt_scan(struct gspca_dev *gspca_dev,
374 struct gspca_frame *frame, /* target */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300375 __u8 *data, /* isoc packet */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300376 int len) /* iso packet length */
377{
378 struct sd *sd = (struct sd *) gspca_dev;
379 int p;
380
381 if (len < 6) {
382/* gspca_dev->last_packet_type = DISCARD_PACKET; */
383 return;
384 }
385 for (p = 0; p < len - 6; p++) {
386 if (data[0 + p] == 0xff
387 && data[1 + p] == 0xff
388 && data[2 + p] == 0x00
389 && data[3 + p] == 0xff
390 && data[4 + p] == 0x96) {
391 if (data[5 + p] == 0x64
392 || data[5 + p] == 0x65
393 || data[5 + p] == 0x66
394 || data[5 + p] == 0x67) {
395 PDEBUG(D_PACK, "sof offset: %d leng: %d",
396 p, len);
397 frame = gspca_frame_add(gspca_dev, LAST_PACKET,
398 frame, data, 0);
399
400 /* put the JPEG header */
401 jpeg_put_header(gspca_dev, frame,
402 sd->qindex, 0x21);
403 data += 16;
404 len -= 16;
405 break;
406 }
407 }
408 }
409 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
410}
411
412/* sub-driver description */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300413static const struct sd_desc sd_desc = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300414 .name = MODULE_NAME,
415 .ctrls = sd_ctrls,
416 .nctrls = ARRAY_SIZE(sd_ctrls),
417 .config = sd_config,
418 .open = sd_open,
419 .start = sd_start,
420 .stopN = sd_stopN,
421 .stop0 = sd_stop0,
422 .close = sd_close,
423 .pkt_scan = sd_pkt_scan,
424};
425
426/* -- module initialisation -- */
427#define DVNM(name) .driver_info = (kernel_ulong_t) name
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300428static const __devinitdata struct usb_device_id device_table[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300429 {USB_DEVICE(0x093a, 0x050f), DVNM("Mars-Semi Pc-Camera")},
430 {}
431};
432MODULE_DEVICE_TABLE(usb, device_table);
433
434/* -- device connect -- */
435static int sd_probe(struct usb_interface *intf,
436 const struct usb_device_id *id)
437{
438 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
439 THIS_MODULE);
440}
441
442static struct usb_driver sd_driver = {
443 .name = MODULE_NAME,
444 .id_table = device_table,
445 .probe = sd_probe,
446 .disconnect = gspca_disconnect,
447};
448
449/* -- module insert / remove -- */
450static int __init sd_mod_init(void)
451{
452 if (usb_register(&sd_driver) < 0)
453 return -1;
454 PDEBUG(D_PROBE, "v%s registered", version);
455 return 0;
456}
457static void __exit sd_mod_exit(void)
458{
459 usb_deregister(&sd_driver);
460 PDEBUG(D_PROBE, "deregistered");
461}
462
463module_init(sd_mod_init);
464module_exit(sd_mod_exit);