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