1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44
45#define MODULE_NAME "mr97310a"
46
47#include "gspca.h"
48
49#define CAM_TYPE_CIF 0
50#define CAM_TYPE_VGA 1
51
52#define MR97310A_BRIGHTNESS_DEFAULT 0
53
54#define MR97310A_EXPOSURE_MIN 0
55#define MR97310A_EXPOSURE_MAX 4095
56#define MR97310A_EXPOSURE_DEFAULT 1000
57
58#define MR97310A_GAIN_MIN 0
59#define MR97310A_GAIN_MAX 31
60#define MR97310A_GAIN_DEFAULT 25
61
62#define MR97310A_CONTRAST_MIN 0
63#define MR97310A_CONTRAST_MAX 31
64#define MR97310A_CONTRAST_DEFAULT 23
65
66#define MR97310A_CS_GAIN_MIN 0
67#define MR97310A_CS_GAIN_MAX 0x7ff
68#define MR97310A_CS_GAIN_DEFAULT 0x110
69
70#define MR97310A_CID_CLOCKDIV (V4L2_CTRL_CLASS_USER + 0x1000)
71#define MR97310A_MIN_CLOCKDIV_MIN 3
72#define MR97310A_MIN_CLOCKDIV_MAX 8
73#define MR97310A_MIN_CLOCKDIV_DEFAULT 3
74
75MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,"
76 "Theodore Kilgore <kilgota@auburn.edu>");
77MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
78MODULE_LICENSE("GPL");
79
80
81static int force_sensor_type = -1;
82module_param(force_sensor_type, int, 0644);
83MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
84
85
86struct sd {
87 struct gspca_dev gspca_dev;
88 struct {
89 struct v4l2_ctrl *exposure;
90 struct v4l2_ctrl *min_clockdiv;
91 };
92 u8 sof_read;
93 u8 cam_type;
94 u8 sensor_type;
95 u8 do_lcd_stop;
96 u8 adj_colors;
97};
98
99struct sensor_w_data {
100 u8 reg;
101 u8 flags;
102 u8 data[16];
103 int len;
104};
105
106static void sd_stopN(struct gspca_dev *gspca_dev);
107
108static const struct v4l2_pix_format vga_mode[] = {
109 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
110 .bytesperline = 160,
111 .sizeimage = 160 * 120,
112 .colorspace = V4L2_COLORSPACE_SRGB,
113 .priv = 4},
114 {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
115 .bytesperline = 176,
116 .sizeimage = 176 * 144,
117 .colorspace = V4L2_COLORSPACE_SRGB,
118 .priv = 3},
119 {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
120 .bytesperline = 320,
121 .sizeimage = 320 * 240,
122 .colorspace = V4L2_COLORSPACE_SRGB,
123 .priv = 2},
124 {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
125 .bytesperline = 352,
126 .sizeimage = 352 * 288,
127 .colorspace = V4L2_COLORSPACE_SRGB,
128 .priv = 1},
129 {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
130 .bytesperline = 640,
131 .sizeimage = 640 * 480,
132 .colorspace = V4L2_COLORSPACE_SRGB,
133 .priv = 0},
134};
135
136
137static int mr_write(struct gspca_dev *gspca_dev, int len)
138{
139 int rc;
140
141 rc = usb_bulk_msg(gspca_dev->dev,
142 usb_sndbulkpipe(gspca_dev->dev, 4),
143 gspca_dev->usb_buf, len, NULL, 500);
144 if (rc < 0)
145 pr_err("reg write [%02x] error %d\n",
146 gspca_dev->usb_buf[0], rc);
147 return rc;
148}
149
150
151static int mr_read(struct gspca_dev *gspca_dev, int len)
152{
153 int rc;
154
155 rc = usb_bulk_msg(gspca_dev->dev,
156 usb_rcvbulkpipe(gspca_dev->dev, 3),
157 gspca_dev->usb_buf, len, NULL, 500);
158 if (rc < 0)
159 pr_err("reg read [%02x] error %d\n",
160 gspca_dev->usb_buf[0], rc);
161 return rc;
162}
163
164static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
165 const u8 *data, int len)
166{
167 gspca_dev->usb_buf[0] = 0x1f;
168 gspca_dev->usb_buf[1] = flags;
169 gspca_dev->usb_buf[2] = reg;
170 memcpy(gspca_dev->usb_buf + 3, data, len);
171
172 return mr_write(gspca_dev, len + 3);
173}
174
175static int sensor_write_regs(struct gspca_dev *gspca_dev,
176 const struct sensor_w_data *data, int len)
177{
178 int i, rc;
179
180 for (i = 0; i < len; i++) {
181 rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
182 data[i].data, data[i].len);
183 if (rc < 0)
184 return rc;
185 }
186
187 return 0;
188}
189
190static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
191{
192 struct sd *sd = (struct sd *) gspca_dev;
193 u8 buf, confirm_reg;
194 int rc;
195
196 buf = data;
197 if (sd->cam_type == CAM_TYPE_CIF) {
198 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
199 confirm_reg = sd->sensor_type ? 0x13 : 0x11;
200 } else {
201 rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1);
202 confirm_reg = 0x11;
203 }
204 if (rc < 0)
205 return rc;
206
207 buf = 0x01;
208 rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
209 if (rc < 0)
210 return rc;
211
212 return 0;
213}
214
215static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose)
216{
217 int err_code;
218
219 gspca_dev->usb_buf[0] = reg;
220 err_code = mr_write(gspca_dev, 1);
221 if (err_code < 0)
222 return err_code;
223
224 err_code = mr_read(gspca_dev, 16);
225 if (err_code < 0)
226 return err_code;
227
228 if (verbose)
229 PDEBUG(D_PROBE, "Register: %02x reads %02x%02x%02x", reg,
230 gspca_dev->usb_buf[0],
231 gspca_dev->usb_buf[1],
232 gspca_dev->usb_buf[2]);
233
234 return 0;
235}
236
237static int zero_the_pointer(struct gspca_dev *gspca_dev)
238{
239 __u8 *data = gspca_dev->usb_buf;
240 int err_code;
241 u8 status = 0;
242 int tries = 0;
243
244 err_code = cam_get_response16(gspca_dev, 0x21, 0);
245 if (err_code < 0)
246 return err_code;
247
248 data[0] = 0x19;
249 data[1] = 0x51;
250 err_code = mr_write(gspca_dev, 2);
251 if (err_code < 0)
252 return err_code;
253
254 err_code = cam_get_response16(gspca_dev, 0x21, 0);
255 if (err_code < 0)
256 return err_code;
257
258 data[0] = 0x19;
259 data[1] = 0xba;
260 err_code = mr_write(gspca_dev, 2);
261 if (err_code < 0)
262 return err_code;
263
264 err_code = cam_get_response16(gspca_dev, 0x21, 0);
265 if (err_code < 0)
266 return err_code;
267
268 data[0] = 0x19;
269 data[1] = 0x00;
270 err_code = mr_write(gspca_dev, 2);
271 if (err_code < 0)
272 return err_code;
273
274 err_code = cam_get_response16(gspca_dev, 0x21, 0);
275 if (err_code < 0)
276 return err_code;
277
278 data[0] = 0x19;
279 data[1] = 0x00;
280 err_code = mr_write(gspca_dev, 2);
281 if (err_code < 0)
282 return err_code;
283
284 while (status != 0x0a && tries < 256) {
285 err_code = cam_get_response16(gspca_dev, 0x21, 0);
286 status = data[0];
287 tries++;
288 if (err_code < 0)
289 return err_code;
290 }
291 if (status != 0x0a)
292 PERR("status is %02x", status);
293
294 tries = 0;
295 while (tries < 4) {
296 data[0] = 0x19;
297 data[1] = 0x00;
298 err_code = mr_write(gspca_dev, 2);
299 if (err_code < 0)
300 return err_code;
301
302 err_code = cam_get_response16(gspca_dev, 0x21, 0);
303 status = data[0];
304 tries++;
305 if (err_code < 0)
306 return err_code;
307 }
308
309 data[0] = 0x19;
310 err_code = mr_write(gspca_dev, 1);
311 if (err_code < 0)
312 return err_code;
313
314 err_code = mr_read(gspca_dev, 16);
315 if (err_code < 0)
316 return err_code;
317
318 return 0;
319}
320
321static int stream_start(struct gspca_dev *gspca_dev)
322{
323 gspca_dev->usb_buf[0] = 0x01;
324 gspca_dev->usb_buf[1] = 0x01;
325 return mr_write(gspca_dev, 2);
326}
327
328static void stream_stop(struct gspca_dev *gspca_dev)
329{
330 gspca_dev->usb_buf[0] = 0x01;
331 gspca_dev->usb_buf[1] = 0x00;
332 if (mr_write(gspca_dev, 2) < 0)
333 PERR("Stream Stop failed");
334}
335
336static void lcd_stop(struct gspca_dev *gspca_dev)
337{
338 gspca_dev->usb_buf[0] = 0x19;
339 gspca_dev->usb_buf[1] = 0x54;
340 if (mr_write(gspca_dev, 2) < 0)
341 PERR("LCD Stop failed");
342}
343
344static int isoc_enable(struct gspca_dev *gspca_dev)
345{
346 gspca_dev->usb_buf[0] = 0x00;
347 gspca_dev->usb_buf[1] = 0x4d;
348 return mr_write(gspca_dev, 2);
349}
350
351
352static int sd_config(struct gspca_dev *gspca_dev,
353 const struct usb_device_id *id)
354{
355 struct sd *sd = (struct sd *) gspca_dev;
356 struct cam *cam;
357 int err_code;
358
359 cam = &gspca_dev->cam;
360 cam->cam_mode = vga_mode;
361 cam->nmodes = ARRAY_SIZE(vga_mode);
362 sd->do_lcd_stop = 0;
363
364
365
366
367
368
369
370
371
372 err_code = zero_the_pointer(gspca_dev);
373 if (err_code < 0)
374 return err_code;
375
376 err_code = stream_start(gspca_dev);
377 if (err_code < 0)
378 return err_code;
379
380
381 err_code = cam_get_response16(gspca_dev, 0x07, 1);
382 if (err_code < 0)
383 return err_code;
384
385 if (id->idProduct == 0x0110 || id->idProduct == 0x010e) {
386 sd->cam_type = CAM_TYPE_CIF;
387 cam->nmodes--;
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409 switch (gspca_dev->usb_buf[0]) {
410 case 2:
411 sd->sensor_type = 0;
412 break;
413 case 3:
414 sd->sensor_type = 1;
415 break;
416 default:
417 pr_err("Unknown CIF Sensor id : %02x\n",
418 gspca_dev->usb_buf[1]);
419 return -ENODEV;
420 }
421 PDEBUG(D_PROBE, "MR97310A CIF camera detected, sensor: %d",
422 sd->sensor_type);
423 } else {
424 sd->cam_type = CAM_TYPE_VGA;
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445 sd->sensor_type = 1;
446 sd->do_lcd_stop = 0;
447 sd->adj_colors = 0;
448 if (gspca_dev->usb_buf[0] == 0x01) {
449 sd->sensor_type = 2;
450 } else if ((gspca_dev->usb_buf[0] != 0x03) &&
451 (gspca_dev->usb_buf[0] != 0x04)) {
452 pr_err("Unknown VGA Sensor id Byte 0: %02x\n",
453 gspca_dev->usb_buf[0]);
454 pr_err("Defaults assumed, may not work\n");
455 pr_err("Please report this\n");
456 }
457
458 if ((gspca_dev->usb_buf[0] == 0x03) &&
459 (gspca_dev->usb_buf[1] == 0x50))
460 sd->adj_colors = 1;
461 if (gspca_dev->usb_buf[0] == 0x04) {
462 sd->do_lcd_stop = 1;
463 switch (gspca_dev->usb_buf[1]) {
464 case 0x50:
465 sd->sensor_type = 0;
466 PDEBUG(D_PROBE, "sensor_type corrected to 0");
467 break;
468 case 0x20:
469
470 break;
471 default:
472 pr_err("Unknown VGA Sensor id Byte 1: %02x\n",
473 gspca_dev->usb_buf[1]);
474 pr_err("Defaults assumed, may not work\n");
475 pr_err("Please report this\n");
476 }
477 }
478 PDEBUG(D_PROBE, "MR97310A VGA camera detected, sensor: %d",
479 sd->sensor_type);
480 }
481
482 sd_stopN(gspca_dev);
483
484 if (force_sensor_type != -1) {
485 sd->sensor_type = !!force_sensor_type;
486 PDEBUG(D_PROBE, "Forcing sensor type to: %d",
487 sd->sensor_type);
488 }
489
490 return 0;
491}
492
493
494static int sd_init(struct gspca_dev *gspca_dev)
495{
496 return 0;
497}
498
499static int start_cif_cam(struct gspca_dev *gspca_dev)
500{
501 struct sd *sd = (struct sd *) gspca_dev;
502 __u8 *data = gspca_dev->usb_buf;
503 int err_code;
504 static const __u8 startup_string[] = {
505 0x00,
506 0x0d,
507 0x01,
508 0x00,
509 0x00,
510 0x13,
511 0x00,
512 0x00,
513 0x00,
514 0x50,
515 0xc0
516 };
517
518
519
520 memcpy(data, startup_string, 11);
521 if (sd->sensor_type)
522 data[5] = 0xbb;
523
524 switch (gspca_dev->pixfmt.width) {
525 case 160:
526 data[9] |= 0x04;
527
528 case 320:
529 default:
530 data[3] = 0x28;
531 data[4] = 0x3c;
532 data[6] = 0x14;
533 data[8] = 0x1a + sd->sensor_type;
534 break;
535 case 176:
536 data[9] |= 0x04;
537
538 case 352:
539 data[3] = 0x2c;
540 data[4] = 0x48;
541 data[6] = 0x06;
542 data[8] = 0x06 - sd->sensor_type;
543 break;
544 }
545 err_code = mr_write(gspca_dev, 11);
546 if (err_code < 0)
547 return err_code;
548
549 if (!sd->sensor_type) {
550 static const struct sensor_w_data cif_sensor0_init_data[] = {
551 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
552 0x0f, 0x14, 0x0f, 0x10}, 8},
553 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
554 {0x12, 0x00, {0x07}, 1},
555 {0x1f, 0x00, {0x06}, 1},
556 {0x27, 0x00, {0x04}, 1},
557 {0x29, 0x00, {0x0c}, 1},
558 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
559 {0x50, 0x00, {0x60}, 1},
560 {0x60, 0x00, {0x06}, 1},
561 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
562 {0x72, 0x00, {0x1e, 0x56}, 2},
563 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
564 0x31, 0x80, 0x00}, 9},
565 {0x11, 0x00, {0x01}, 1},
566 {0, 0, {0}, 0}
567 };
568 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
569 ARRAY_SIZE(cif_sensor0_init_data));
570 } else {
571 static const struct sensor_w_data cif_sensor1_init_data[] = {
572
573 {0x02, 0x00, {0x10}, 1},
574 {0x05, 0x01, {0x22}, 1},
575 {0x06, 0x01, {0x00}, 1},
576 {0x09, 0x02, {0x0e}, 1},
577 {0x0a, 0x02, {0x05}, 1},
578 {0x0b, 0x02, {0x05}, 1},
579 {0x0c, 0x02, {0x0f}, 1},
580 {0x0d, 0x02, {0x07}, 1},
581 {0x0e, 0x02, {0x0c}, 1},
582 {0x0f, 0x00, {0x00}, 1},
583 {0x10, 0x00, {0x06}, 1},
584 {0x11, 0x00, {0x07}, 1},
585 {0x12, 0x00, {0x00}, 1},
586 {0x13, 0x00, {0x01}, 1},
587 {0, 0, {0}, 0}
588 };
589
590 gspca_dev->usb_buf[0] = 0x0a;
591 gspca_dev->usb_buf[1] = 0x00;
592 err_code = mr_write(gspca_dev, 2);
593 if (err_code < 0)
594 return err_code;
595 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
596 ARRAY_SIZE(cif_sensor1_init_data));
597 }
598 return err_code;
599}
600
601static int start_vga_cam(struct gspca_dev *gspca_dev)
602{
603 struct sd *sd = (struct sd *) gspca_dev;
604 __u8 *data = gspca_dev->usb_buf;
605 int err_code;
606 static const __u8 startup_string[] =
607 {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b, 0x00, 0x00,
608 0x00, 0x50, 0xc0};
609
610
611 memcpy(data, startup_string, 11);
612 if (!sd->sensor_type) {
613 data[5] = 0x00;
614 data[10] = 0x91;
615 }
616 if (sd->sensor_type == 2) {
617 data[5] = 0x00;
618 data[10] = 0x18;
619 }
620
621 switch (gspca_dev->pixfmt.width) {
622 case 160:
623 data[9] |= 0x0c;
624
625 case 320:
626 data[9] |= 0x04;
627
628 case 640:
629 default:
630 data[3] = 0x50;
631 data[4] = 0x78;
632 data[6] = 0x04;
633 data[8] = 0x03;
634 if (sd->sensor_type == 2) {
635 data[6] = 2;
636 data[8] = 1;
637 }
638 if (sd->do_lcd_stop)
639 data[8] = 0x04;
640 break;
641
642 case 176:
643 data[9] |= 0x04;
644
645 case 352:
646 data[3] = 0x2c;
647 data[4] = 0x48;
648 data[6] = 0x94;
649 data[8] = 0x63;
650 if (sd->do_lcd_stop)
651 data[8] = 0x64;
652 break;
653 }
654
655 err_code = mr_write(gspca_dev, 11);
656 if (err_code < 0)
657 return err_code;
658
659 if (!sd->sensor_type) {
660 static const struct sensor_w_data vga_sensor0_init_data[] = {
661 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
662 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
663 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
664 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
665 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
666 {0, 0, {0}, 0}
667 };
668 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
669 ARRAY_SIZE(vga_sensor0_init_data));
670 } else if (sd->sensor_type == 1) {
671 static const struct sensor_w_data color_adj[] = {
672 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
673
674
675 0x05, 0x01, 0x04}, 8}
676 };
677
678 static const struct sensor_w_data color_no_adj[] = {
679 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
680
681 0x07, 0x00, 0x01}, 8}
682 };
683
684 static const struct sensor_w_data vga_sensor1_init_data[] = {
685 {0x11, 0x04, {0x01}, 1},
686 {0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01,
687
688
689 0x00, 0x0a}, 7},
690 {0x11, 0x04, {0x01}, 1},
691 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
692 {0x11, 0x04, {0x01}, 1},
693 {0, 0, {0}, 0}
694 };
695
696 if (sd->adj_colors)
697 err_code = sensor_write_regs(gspca_dev, color_adj,
698 ARRAY_SIZE(color_adj));
699 else
700 err_code = sensor_write_regs(gspca_dev, color_no_adj,
701 ARRAY_SIZE(color_no_adj));
702
703 if (err_code < 0)
704 return err_code;
705
706 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
707 ARRAY_SIZE(vga_sensor1_init_data));
708 } else {
709 static const struct sensor_w_data vga_sensor2_init_data[] = {
710
711 {0x01, 0x00, {0x48}, 1},
712 {0x02, 0x00, {0x22}, 1},
713
714 {0x05, 0x00, {0x10}, 1},
715 {0x06, 0x00, {0x00}, 1},
716 {0x07, 0x00, {0x00}, 1},
717 {0x08, 0x00, {0x00}, 1},
718 {0x09, 0x00, {0x00}, 1},
719
720
721
722
723
724
725
726
727
728
729
730
731 {0x12, 0x00, {0x00}, 1},
732 {0x13, 0x00, {0x04}, 1},
733 {0x14, 0x00, {0x00}, 1},
734 {0x15, 0x00, {0x06}, 1},
735 {0x16, 0x00, {0x01}, 1},
736 {0x17, 0x00, {0xe2}, 1},
737 {0x18, 0x00, {0x02}, 1},
738 {0x19, 0x00, {0x82}, 1},
739 {0x1a, 0x00, {0x00}, 1},
740 {0x1b, 0x00, {0x20}, 1},
741
742 {0x1d, 0x00, {0x80}, 1},
743 {0x1e, 0x00, {0x08}, 1},
744 {0x1f, 0x00, {0x0c}, 1},
745 {0x20, 0x00, {0x00}, 1},
746 {0, 0, {0}, 0}
747 };
748 err_code = sensor_write_regs(gspca_dev, vga_sensor2_init_data,
749 ARRAY_SIZE(vga_sensor2_init_data));
750 }
751 return err_code;
752}
753
754static int sd_start(struct gspca_dev *gspca_dev)
755{
756 struct sd *sd = (struct sd *) gspca_dev;
757 int err_code;
758
759 sd->sof_read = 0;
760
761
762
763
764
765 err_code = zero_the_pointer(gspca_dev);
766 if (err_code < 0)
767 return err_code;
768
769 err_code = stream_start(gspca_dev);
770 if (err_code < 0)
771 return err_code;
772
773 if (sd->cam_type == CAM_TYPE_CIF) {
774 err_code = start_cif_cam(gspca_dev);
775 } else {
776 err_code = start_vga_cam(gspca_dev);
777 }
778 if (err_code < 0)
779 return err_code;
780
781 return isoc_enable(gspca_dev);
782}
783
784static void sd_stopN(struct gspca_dev *gspca_dev)
785{
786 struct sd *sd = (struct sd *) gspca_dev;
787
788 stream_stop(gspca_dev);
789
790 zero_the_pointer(gspca_dev);
791 if (sd->do_lcd_stop)
792 lcd_stop(gspca_dev);
793}
794
795static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
796{
797 struct sd *sd = (struct sd *) gspca_dev;
798 u8 sign_reg = 7;
799 u8 value_reg = 8;
800 static const u8 quick_clix_table[] =
801
802 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
803 if (sd->cam_type == CAM_TYPE_VGA) {
804 sign_reg += 4;
805 value_reg += 4;
806 }
807
808
809 if (val > 0) {
810 sensor_write1(gspca_dev, sign_reg, 0x00);
811 } else {
812 sensor_write1(gspca_dev, sign_reg, 0x01);
813 val = 257 - val;
814 }
815
816 if (sd->do_lcd_stop)
817 val = quick_clix_table[val];
818
819 sensor_write1(gspca_dev, value_reg, val);
820}
821
822static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 min_clockdiv)
823{
824 struct sd *sd = (struct sd *) gspca_dev;
825 int exposure = MR97310A_EXPOSURE_DEFAULT;
826 u8 buf[2];
827
828 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
829
830
831 exposure = (expo * 9267) / 10000 + 300;
832 sensor_write1(gspca_dev, 3, exposure >> 4);
833 sensor_write1(gspca_dev, 4, exposure & 0x0f);
834 } else if (sd->sensor_type == 2) {
835 exposure = expo;
836 exposure >>= 3;
837 sensor_write1(gspca_dev, 3, exposure >> 8);
838 sensor_write1(gspca_dev, 4, exposure & 0xff);
839 } else {
840
841
842
843
844
845
846
847 u8 clockdiv = (60 * expo + 7999) / 8000;
848
849
850 if (clockdiv < min_clockdiv && gspca_dev->pixfmt.width >= 320)
851 clockdiv = min_clockdiv;
852 else if (clockdiv < 2)
853 clockdiv = 2;
854
855 if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4)
856 clockdiv = 4;
857
858
859
860 exposure = (60 * 511 * expo) / (8000 * clockdiv);
861 if (exposure > 511)
862 exposure = 511;
863
864
865 exposure = 511 - exposure;
866
867 buf[0] = exposure & 0xff;
868 buf[1] = exposure >> 8;
869 sensor_write_reg(gspca_dev, 0x0e, 0, buf, 2);
870 sensor_write1(gspca_dev, 0x02, clockdiv);
871 }
872}
873
874static void setgain(struct gspca_dev *gspca_dev, s32 val)
875{
876 struct sd *sd = (struct sd *) gspca_dev;
877 u8 gainreg;
878
879 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1)
880 sensor_write1(gspca_dev, 0x0e, val);
881 else if (sd->cam_type == CAM_TYPE_VGA && sd->sensor_type == 2)
882 for (gainreg = 0x0a; gainreg < 0x11; gainreg += 2) {
883 sensor_write1(gspca_dev, gainreg, val >> 8);
884 sensor_write1(gspca_dev, gainreg + 1, val & 0xff);
885 }
886 else
887 sensor_write1(gspca_dev, 0x10, val);
888}
889
890static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
891{
892 sensor_write1(gspca_dev, 0x1c, val);
893}
894
895static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
896{
897 struct gspca_dev *gspca_dev =
898 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
899 struct sd *sd = (struct sd *)gspca_dev;
900
901 gspca_dev->usb_err = 0;
902
903 if (!gspca_dev->streaming)
904 return 0;
905
906 switch (ctrl->id) {
907 case V4L2_CID_BRIGHTNESS:
908 setbrightness(gspca_dev, ctrl->val);
909 break;
910 case V4L2_CID_CONTRAST:
911 setcontrast(gspca_dev, ctrl->val);
912 break;
913 case V4L2_CID_EXPOSURE:
914 setexposure(gspca_dev, sd->exposure->val,
915 sd->min_clockdiv ? sd->min_clockdiv->val : 0);
916 break;
917 case V4L2_CID_GAIN:
918 setgain(gspca_dev, ctrl->val);
919 break;
920 }
921 return gspca_dev->usb_err;
922}
923
924static const struct v4l2_ctrl_ops sd_ctrl_ops = {
925 .s_ctrl = sd_s_ctrl,
926};
927
928static int sd_init_controls(struct gspca_dev *gspca_dev)
929{
930 struct sd *sd = (struct sd *)gspca_dev;
931 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
932 static const struct v4l2_ctrl_config clockdiv = {
933 .ops = &sd_ctrl_ops,
934 .id = MR97310A_CID_CLOCKDIV,
935 .type = V4L2_CTRL_TYPE_INTEGER,
936 .name = "Minimum Clock Divider",
937 .min = MR97310A_MIN_CLOCKDIV_MIN,
938 .max = MR97310A_MIN_CLOCKDIV_MAX,
939 .step = 1,
940 .def = MR97310A_MIN_CLOCKDIV_DEFAULT,
941 };
942 bool has_brightness = false;
943 bool has_argus_brightness = false;
944 bool has_contrast = false;
945 bool has_gain = false;
946 bool has_cs_gain = false;
947 bool has_exposure = false;
948 bool has_clockdiv = false;
949
950 gspca_dev->vdev.ctrl_handler = hdl;
951 v4l2_ctrl_handler_init(hdl, 4);
952
953
954 if (sd->cam_type == CAM_TYPE_CIF) {
955
956 if (sd->sensor_type == 0)
957 has_exposure = has_gain = has_clockdiv = true;
958 else
959 has_exposure = has_gain = has_brightness = true;
960 } else {
961
962 if (sd->sensor_type == 0)
963 ;
964 else if (sd->sensor_type == 2)
965 has_exposure = has_cs_gain = has_contrast = true;
966 else if (sd->do_lcd_stop)
967 has_exposure = has_gain = has_argus_brightness =
968 has_clockdiv = true;
969 else
970 has_exposure = has_gain = has_brightness =
971 has_clockdiv = true;
972 }
973
974
975
976
977
978
979
980
981
982
983 if (has_brightness)
984 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
985 V4L2_CID_BRIGHTNESS, -254, 255, 1,
986 MR97310A_BRIGHTNESS_DEFAULT);
987 else if (has_argus_brightness)
988 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
989 V4L2_CID_BRIGHTNESS, 0, 15, 1,
990 MR97310A_BRIGHTNESS_DEFAULT);
991 if (has_contrast)
992 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
993 V4L2_CID_CONTRAST, MR97310A_CONTRAST_MIN,
994 MR97310A_CONTRAST_MAX, 1, MR97310A_CONTRAST_DEFAULT);
995 if (has_gain)
996 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
997 V4L2_CID_GAIN, MR97310A_GAIN_MIN, MR97310A_GAIN_MAX,
998 1, MR97310A_GAIN_DEFAULT);
999 else if (has_cs_gain)
1000 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_GAIN,
1001 MR97310A_CS_GAIN_MIN, MR97310A_CS_GAIN_MAX,
1002 1, MR97310A_CS_GAIN_DEFAULT);
1003 if (has_exposure)
1004 sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1005 V4L2_CID_EXPOSURE, MR97310A_EXPOSURE_MIN,
1006 MR97310A_EXPOSURE_MAX, 1, MR97310A_EXPOSURE_DEFAULT);
1007 if (has_clockdiv)
1008 sd->min_clockdiv = v4l2_ctrl_new_custom(hdl, &clockdiv, NULL);
1009
1010 if (hdl->error) {
1011 pr_err("Could not initialize controls\n");
1012 return hdl->error;
1013 }
1014 if (has_exposure && has_clockdiv)
1015 v4l2_ctrl_cluster(2, &sd->exposure);
1016 return 0;
1017}
1018
1019
1020#include "pac_common.h"
1021
1022static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1023 u8 *data,
1024 int len)
1025{
1026 struct sd *sd = (struct sd *) gspca_dev;
1027 unsigned char *sof;
1028
1029 sof = pac_find_sof(gspca_dev, &sd->sof_read, data, len);
1030 if (sof) {
1031 int n;
1032
1033
1034 n = sof - data;
1035 if (n > sizeof pac_sof_marker)
1036 n -= sizeof pac_sof_marker;
1037 else
1038 n = 0;
1039 gspca_frame_add(gspca_dev, LAST_PACKET,
1040 data, n);
1041
1042 gspca_frame_add(gspca_dev, FIRST_PACKET,
1043 pac_sof_marker, sizeof pac_sof_marker);
1044 len -= sof - data;
1045 data = sof;
1046 }
1047 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1048}
1049
1050
1051static const struct sd_desc sd_desc = {
1052 .name = MODULE_NAME,
1053 .config = sd_config,
1054 .init = sd_init,
1055 .init_controls = sd_init_controls,
1056 .start = sd_start,
1057 .stopN = sd_stopN,
1058 .pkt_scan = sd_pkt_scan,
1059};
1060
1061
1062static const struct usb_device_id device_table[] = {
1063 {USB_DEVICE(0x08ca, 0x0110)},
1064 {USB_DEVICE(0x08ca, 0x0111)},
1065 {USB_DEVICE(0x093a, 0x010f)},
1066 {USB_DEVICE(0x093a, 0x010e)},
1067 {}
1068};
1069MODULE_DEVICE_TABLE(usb, device_table);
1070
1071
1072static int sd_probe(struct usb_interface *intf,
1073 const struct usb_device_id *id)
1074{
1075 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1076 THIS_MODULE);
1077}
1078
1079static struct usb_driver sd_driver = {
1080 .name = MODULE_NAME,
1081 .id_table = device_table,
1082 .probe = sd_probe,
1083 .disconnect = gspca_disconnect,
1084#ifdef CONFIG_PM
1085 .suspend = gspca_suspend,
1086 .resume = gspca_resume,
1087 .reset_resume = gspca_resume,
1088#endif
1089};
1090
1091module_usb_driver(sd_driver);
1092