1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24#define MODULE_NAME "se401"
25
26#define BULK_SIZE 4096
27#define PACKET_SIZE 1024
28#define READ_REQ_SIZE 64
29#define MAX_MODES ((READ_REQ_SIZE - 6) / 4)
30
31
32
33#define SE401_QUANT_FACT 8
34
35#include <linux/input.h>
36#include <linux/slab.h>
37#include "gspca.h"
38#include "se401.h"
39
40MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
41MODULE_DESCRIPTION("Endpoints se401");
42MODULE_LICENSE("GPL");
43
44
45enum {
46 EXPO_CHANGED,
47 EXPO_DROP_FRAME,
48 EXPO_NO_CHANGE,
49};
50
51
52struct sd {
53 struct gspca_dev gspca_dev;
54 struct {
55 struct v4l2_ctrl *exposure;
56 struct v4l2_ctrl *freq;
57 };
58 bool has_brightness;
59 struct v4l2_pix_format fmts[MAX_MODES];
60 int pixels_read;
61 int packet_read;
62 u8 packet[PACKET_SIZE];
63 u8 restart_stream;
64 u8 button_state;
65 u8 resetlevel;
66 u8 resetlevel_frame_count;
67 int resetlevel_adjust_dir;
68 int expo_change_state;
69};
70
71
72static void se401_write_req(struct gspca_dev *gspca_dev, u16 req, u16 value,
73 int silent)
74{
75 int err;
76
77 if (gspca_dev->usb_err < 0)
78 return;
79
80 err = usb_control_msg(gspca_dev->dev,
81 usb_sndctrlpipe(gspca_dev->dev, 0), req,
82 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
83 value, 0, NULL, 0, 1000);
84 if (err < 0) {
85 if (!silent)
86 pr_err("write req failed req %#04x val %#04x error %d\n",
87 req, value, err);
88 gspca_dev->usb_err = err;
89 }
90}
91
92static void se401_read_req(struct gspca_dev *gspca_dev, u16 req, int silent)
93{
94 int err;
95
96 if (gspca_dev->usb_err < 0)
97 return;
98
99 if (USB_BUF_SZ < READ_REQ_SIZE) {
100 pr_err("USB_BUF_SZ too small!!\n");
101 gspca_dev->usb_err = -ENOBUFS;
102 return;
103 }
104
105 err = usb_control_msg(gspca_dev->dev,
106 usb_rcvctrlpipe(gspca_dev->dev, 0), req,
107 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
108 0, 0, gspca_dev->usb_buf, READ_REQ_SIZE, 1000);
109 if (err < 0) {
110 if (!silent)
111 pr_err("read req failed req %#04x error %d\n",
112 req, err);
113 gspca_dev->usb_err = err;
114 }
115}
116
117static void se401_set_feature(struct gspca_dev *gspca_dev,
118 u16 selector, u16 param)
119{
120 int err;
121
122 if (gspca_dev->usb_err < 0)
123 return;
124
125 err = usb_control_msg(gspca_dev->dev,
126 usb_sndctrlpipe(gspca_dev->dev, 0),
127 SE401_REQ_SET_EXT_FEATURE,
128 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
129 param, selector, NULL, 0, 1000);
130 if (err < 0) {
131 pr_err("set feature failed sel %#04x param %#04x error %d\n",
132 selector, param, err);
133 gspca_dev->usb_err = err;
134 }
135}
136
137static int se401_get_feature(struct gspca_dev *gspca_dev, u16 selector)
138{
139 int err;
140
141 if (gspca_dev->usb_err < 0)
142 return gspca_dev->usb_err;
143
144 if (USB_BUF_SZ < 2) {
145 pr_err("USB_BUF_SZ too small!!\n");
146 gspca_dev->usb_err = -ENOBUFS;
147 return gspca_dev->usb_err;
148 }
149
150 err = usb_control_msg(gspca_dev->dev,
151 usb_rcvctrlpipe(gspca_dev->dev, 0),
152 SE401_REQ_GET_EXT_FEATURE,
153 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
154 0, selector, gspca_dev->usb_buf, 2, 1000);
155 if (err < 0) {
156 pr_err("get feature failed sel %#04x error %d\n",
157 selector, err);
158 gspca_dev->usb_err = err;
159 return err;
160 }
161 return gspca_dev->usb_buf[0] | (gspca_dev->usb_buf[1] << 8);
162}
163
164static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
165{
166
167 se401_write_req(gspca_dev, SE401_REQ_SET_BRT, val, 0);
168}
169
170static void setgain(struct gspca_dev *gspca_dev, s32 val)
171{
172 u16 gain = 63 - val;
173
174
175 se401_set_feature(gspca_dev, HV7131_REG_ARCG, gain);
176
177 se401_set_feature(gspca_dev, HV7131_REG_AGCG, gain);
178
179 se401_set_feature(gspca_dev, HV7131_REG_ABCG, gain);
180}
181
182static void setexposure(struct gspca_dev *gspca_dev, s32 val, s32 freq)
183{
184 struct sd *sd = (struct sd *) gspca_dev;
185 int integration = val << 6;
186 u8 expose_h, expose_m, expose_l;
187
188
189
190
191
192
193 sd->expo_change_state = EXPO_CHANGED;
194
195 if (freq == V4L2_CID_POWER_LINE_FREQUENCY_50HZ)
196 integration = integration - integration % 106667;
197 if (freq == V4L2_CID_POWER_LINE_FREQUENCY_60HZ)
198 integration = integration - integration % 88889;
199
200 expose_h = (integration >> 16);
201 expose_m = (integration >> 8);
202 expose_l = integration;
203
204
205 se401_set_feature(gspca_dev, HV7131_REG_TITL, expose_l);
206
207 se401_set_feature(gspca_dev, HV7131_REG_TITM, expose_m);
208
209 se401_set_feature(gspca_dev, HV7131_REG_TITU, expose_h);
210}
211
212static int sd_config(struct gspca_dev *gspca_dev,
213 const struct usb_device_id *id)
214{
215 struct sd *sd = (struct sd *)gspca_dev;
216 struct cam *cam = &gspca_dev->cam;
217 u8 *cd = gspca_dev->usb_buf;
218 int i, j, n;
219 int widths[MAX_MODES], heights[MAX_MODES];
220
221
222 se401_read_req(gspca_dev, SE401_REQ_GET_CAMERA_DESCRIPTOR, 1);
223 if (gspca_dev->usb_err) {
224
225
226 usb_reset_device(gspca_dev->dev);
227 gspca_dev->usb_err = 0;
228 se401_read_req(gspca_dev, SE401_REQ_GET_CAMERA_DESCRIPTOR, 0);
229 }
230
231
232 se401_write_req(gspca_dev, SE401_REQ_LED_CONTROL, 0, 0);
233 if (gspca_dev->usb_err)
234 return gspca_dev->usb_err;
235
236 if (cd[1] != 0x41) {
237 pr_err("Wrong descriptor type\n");
238 return -ENODEV;
239 }
240
241 if (!(cd[2] & SE401_FORMAT_BAYER)) {
242 pr_err("Bayer format not supported!\n");
243 return -ENODEV;
244 }
245
246 if (cd[3])
247 pr_info("ExtraFeatures: %d\n", cd[3]);
248
249 n = cd[4] | (cd[5] << 8);
250 if (n > MAX_MODES) {
251 pr_err("Too many frame sizes\n");
252 return -ENODEV;
253 }
254
255 for (i = 0; i < n ; i++) {
256 widths[i] = cd[6 + i * 4 + 0] | (cd[6 + i * 4 + 1] << 8);
257 heights[i] = cd[6 + i * 4 + 2] | (cd[6 + i * 4 + 3] << 8);
258 }
259
260 for (i = 0; i < n ; i++) {
261 sd->fmts[i].width = widths[i];
262 sd->fmts[i].height = heights[i];
263 sd->fmts[i].field = V4L2_FIELD_NONE;
264 sd->fmts[i].colorspace = V4L2_COLORSPACE_SRGB;
265 sd->fmts[i].priv = 1;
266
267
268 for (j = 0; j < n; j++) {
269 if (widths[j] / 2 == widths[i] &&
270 heights[j] / 2 == heights[i]) {
271 sd->fmts[i].priv = 2;
272 break;
273 }
274 }
275
276
277 for (j = 0; j < n; j++) {
278 if (widths[j] / 4 == widths[i] &&
279 heights[j] / 4 == heights[i]) {
280 sd->fmts[i].priv = 4;
281 break;
282 }
283 }
284
285 if (sd->fmts[i].priv == 1) {
286
287 sd->fmts[i].pixelformat = V4L2_PIX_FMT_SBGGR8;
288 sd->fmts[i].bytesperline = widths[i];
289 sd->fmts[i].sizeimage = widths[i] * heights[i];
290 pr_info("Frame size: %dx%d bayer\n",
291 widths[i], heights[i]);
292 } else {
293
294 sd->fmts[i].pixelformat = V4L2_PIX_FMT_SE401;
295 sd->fmts[i].bytesperline = 0;
296 sd->fmts[i].sizeimage = widths[i] * heights[i] * 3;
297 pr_info("Frame size: %dx%d 1/%dth janggu\n",
298 widths[i], heights[i],
299 sd->fmts[i].priv * sd->fmts[i].priv);
300 }
301 }
302
303 cam->cam_mode = sd->fmts;
304 cam->nmodes = n;
305 cam->bulk = 1;
306 cam->bulk_size = BULK_SIZE;
307 cam->bulk_nurbs = 4;
308 sd->resetlevel = 0x2d;
309
310
311 se401_read_req(gspca_dev, SE401_REQ_GET_BRT, 1);
312 sd->has_brightness = !!gspca_dev->usb_err;
313 gspca_dev->usb_err = 0;
314
315 return 0;
316}
317
318
319static int sd_init(struct gspca_dev *gspca_dev)
320{
321 return 0;
322}
323
324
325static int sd_isoc_init(struct gspca_dev *gspca_dev)
326{
327 gspca_dev->alt = 1;
328
329 return gspca_dev->usb_err;
330}
331
332
333static int sd_start(struct gspca_dev *gspca_dev)
334{
335 struct sd *sd = (struct sd *)gspca_dev;
336 int mult = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
337 int mode = 0;
338
339 se401_write_req(gspca_dev, SE401_REQ_CAMERA_POWER, 1, 1);
340 if (gspca_dev->usb_err) {
341
342
343 usb_reset_device(gspca_dev->dev);
344 gspca_dev->usb_err = 0;
345 se401_write_req(gspca_dev, SE401_REQ_CAMERA_POWER, 1, 0);
346 }
347 se401_write_req(gspca_dev, SE401_REQ_LED_CONTROL, 1, 0);
348
349 se401_set_feature(gspca_dev, HV7131_REG_MODE_B, 0x05);
350
351
352 se401_write_req(gspca_dev, SE401_REQ_SET_WIDTH,
353 gspca_dev->pixfmt.width * mult, 0);
354 se401_write_req(gspca_dev, SE401_REQ_SET_HEIGHT,
355 gspca_dev->pixfmt.height * mult, 0);
356
357
358
359
360
361
362 switch (mult) {
363 case 1:
364 mode = 0x03; break;
365 case 2:
366 mode = SE401_QUANT_FACT << 4; break;
367 case 4:
368 mode = (SE401_QUANT_FACT << 4) | 0x02; break;
369 }
370 se401_set_feature(gspca_dev, SE401_OPERATINGMODE, mode);
371
372 se401_set_feature(gspca_dev, HV7131_REG_ARLV, sd->resetlevel);
373
374 sd->packet_read = 0;
375 sd->pixels_read = 0;
376 sd->restart_stream = 0;
377 sd->resetlevel_frame_count = 0;
378 sd->resetlevel_adjust_dir = 0;
379 sd->expo_change_state = EXPO_NO_CHANGE;
380
381 se401_write_req(gspca_dev, SE401_REQ_START_CONTINUOUS_CAPTURE, 0, 0);
382
383 return gspca_dev->usb_err;
384}
385
386static void sd_stopN(struct gspca_dev *gspca_dev)
387{
388 se401_write_req(gspca_dev, SE401_REQ_STOP_CONTINUOUS_CAPTURE, 0, 0);
389 se401_write_req(gspca_dev, SE401_REQ_LED_CONTROL, 0, 0);
390 se401_write_req(gspca_dev, SE401_REQ_CAMERA_POWER, 0, 0);
391}
392
393static void sd_dq_callback(struct gspca_dev *gspca_dev)
394{
395 struct sd *sd = (struct sd *)gspca_dev;
396 unsigned int ahrc, alrc;
397 int oldreset, adjust_dir;
398
399
400 if (sd->restart_stream) {
401 sd_stopN(gspca_dev);
402 sd_start(gspca_dev);
403 sd->restart_stream = 0;
404 }
405
406
407
408
409 sd->resetlevel_frame_count++;
410 if (sd->resetlevel_frame_count < 20)
411 return;
412
413
414
415 se401_get_feature(gspca_dev, HV7131_REG_HIREFNOH);
416 se401_get_feature(gspca_dev, HV7131_REG_HIREFNOL);
417 se401_get_feature(gspca_dev, HV7131_REG_LOREFNOH);
418 se401_get_feature(gspca_dev, HV7131_REG_LOREFNOL);
419 ahrc = 256*se401_get_feature(gspca_dev, HV7131_REG_HIREFNOH) +
420 se401_get_feature(gspca_dev, HV7131_REG_HIREFNOL);
421 alrc = 256*se401_get_feature(gspca_dev, HV7131_REG_LOREFNOH) +
422 se401_get_feature(gspca_dev, HV7131_REG_LOREFNOL);
423
424
425 oldreset = sd->resetlevel;
426 if (alrc > 10) {
427 while (alrc >= 10 && sd->resetlevel < 63) {
428 sd->resetlevel++;
429 alrc /= 2;
430 }
431 } else if (ahrc > 20) {
432 while (ahrc >= 20 && sd->resetlevel > 0) {
433 sd->resetlevel--;
434 ahrc /= 2;
435 }
436 }
437
438 if (sd->resetlevel > oldreset)
439 adjust_dir = 1;
440 else
441 adjust_dir = -1;
442 if (sd->resetlevel_adjust_dir &&
443 sd->resetlevel_adjust_dir != adjust_dir)
444 sd->resetlevel = oldreset + (sd->resetlevel - oldreset) / 2;
445
446 if (sd->resetlevel != oldreset) {
447 sd->resetlevel_adjust_dir = adjust_dir;
448 se401_set_feature(gspca_dev, HV7131_REG_ARLV, sd->resetlevel);
449 }
450
451 sd->resetlevel_frame_count = 0;
452}
453
454static void sd_complete_frame(struct gspca_dev *gspca_dev, u8 *data, int len)
455{
456 struct sd *sd = (struct sd *)gspca_dev;
457
458 switch (sd->expo_change_state) {
459 case EXPO_CHANGED:
460
461
462 sd->expo_change_state = EXPO_DROP_FRAME;
463 break;
464 case EXPO_DROP_FRAME:
465
466
467 gspca_dev->last_packet_type = DISCARD_PACKET;
468 sd->expo_change_state = EXPO_NO_CHANGE;
469 break;
470 case EXPO_NO_CHANGE:
471 break;
472 }
473 gspca_frame_add(gspca_dev, LAST_PACKET, data, len);
474}
475
476static void sd_pkt_scan_janggu(struct gspca_dev *gspca_dev, u8 *data, int len)
477{
478 struct sd *sd = (struct sd *)gspca_dev;
479 int imagesize = gspca_dev->pixfmt.width * gspca_dev->pixfmt.height;
480 int i, plen, bits, pixels, info, count;
481
482 if (sd->restart_stream)
483 return;
484
485
486 if (gspca_dev->last_packet_type == LAST_PACKET && len == 1024) {
487 gspca_dev->last_packet_type = DISCARD_PACKET;
488 return;
489 }
490
491 i = 0;
492 while (i < len) {
493
494 if (sd->packet_read < 4) {
495 count = 4 - sd->packet_read;
496 if (count > len - i)
497 count = len - i;
498 memcpy(&sd->packet[sd->packet_read], &data[i], count);
499 sd->packet_read += count;
500 i += count;
501 if (sd->packet_read < 4)
502 break;
503 }
504 bits = sd->packet[3] + (sd->packet[2] << 8);
505 pixels = sd->packet[1] + ((sd->packet[0] & 0x3f) << 8);
506 info = (sd->packet[0] & 0xc0) >> 6;
507 plen = ((bits + 47) >> 4) << 1;
508
509 if (plen > 1024) {
510 pr_err("invalid packet len %d restarting stream\n",
511 plen);
512 goto error;
513 }
514 if (info == 3) {
515 pr_err("unknown frame info value restarting stream\n");
516 goto error;
517 }
518
519
520 count = plen - sd->packet_read;
521 if (count > len - i)
522 count = len - i;
523 memcpy(&sd->packet[sd->packet_read], &data[i], count);
524 sd->packet_read += count;
525 i += count;
526 if (sd->packet_read < plen)
527 break;
528
529 sd->pixels_read += pixels;
530 sd->packet_read = 0;
531
532 switch (info) {
533 case 0:
534 gspca_frame_add(gspca_dev, INTER_PACKET, sd->packet,
535 plen);
536 break;
537 case 1:
538 if (sd->pixels_read != imagesize) {
539 pr_err("frame size %d expected %d\n",
540 sd->pixels_read, imagesize);
541 goto error;
542 }
543 sd_complete_frame(gspca_dev, sd->packet, plen);
544 return;
545 case 2:
546 gspca_frame_add(gspca_dev, FIRST_PACKET, sd->packet,
547 plen);
548 sd->pixels_read = pixels;
549 break;
550 }
551 }
552 return;
553
554error:
555 sd->restart_stream = 1;
556
557
558 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
559 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
560}
561
562static void sd_pkt_scan_bayer(struct gspca_dev *gspca_dev, u8 *data, int len)
563{
564 struct cam *cam = &gspca_dev->cam;
565 int imagesize = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
566
567 if (gspca_dev->image_len == 0) {
568 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
569 return;
570 }
571
572 if (gspca_dev->image_len + len >= imagesize) {
573 sd_complete_frame(gspca_dev, data, len);
574 return;
575 }
576
577 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
578}
579
580static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len)
581{
582 int mult = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
583
584 if (len == 0)
585 return;
586
587 if (mult == 1)
588 sd_pkt_scan_bayer(gspca_dev, data, len);
589 else
590 sd_pkt_scan_janggu(gspca_dev, data, len);
591}
592
593#if IS_ENABLED(CONFIG_INPUT)
594static int sd_int_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len)
595{
596 struct sd *sd = (struct sd *)gspca_dev;
597 u8 state;
598
599 if (len != 2)
600 return -EINVAL;
601
602 switch (data[0]) {
603 case 0:
604 case 1:
605 state = data[0];
606 break;
607 default:
608 return -EINVAL;
609 }
610 if (sd->button_state != state) {
611 input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);
612 input_sync(gspca_dev->input_dev);
613 sd->button_state = state;
614 }
615
616 return 0;
617}
618#endif
619
620static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
621{
622 struct gspca_dev *gspca_dev =
623 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
624 struct sd *sd = (struct sd *)gspca_dev;
625
626 gspca_dev->usb_err = 0;
627
628 if (!gspca_dev->streaming)
629 return 0;
630
631 switch (ctrl->id) {
632 case V4L2_CID_BRIGHTNESS:
633 setbrightness(gspca_dev, ctrl->val);
634 break;
635 case V4L2_CID_GAIN:
636 setgain(gspca_dev, ctrl->val);
637 break;
638 case V4L2_CID_EXPOSURE:
639 setexposure(gspca_dev, ctrl->val, sd->freq->val);
640 break;
641 }
642 return gspca_dev->usb_err;
643}
644
645static const struct v4l2_ctrl_ops sd_ctrl_ops = {
646 .s_ctrl = sd_s_ctrl,
647};
648
649static int sd_init_controls(struct gspca_dev *gspca_dev)
650{
651 struct sd *sd = (struct sd *)gspca_dev;
652 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
653
654 gspca_dev->vdev.ctrl_handler = hdl;
655 v4l2_ctrl_handler_init(hdl, 4);
656 if (sd->has_brightness)
657 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
658 V4L2_CID_BRIGHTNESS, 0, 255, 1, 15);
659
660 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
661 V4L2_CID_GAIN, 0, 50, 1, 25);
662 sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
663 V4L2_CID_EXPOSURE, 0, 32767, 1, 15000);
664 sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
665 V4L2_CID_POWER_LINE_FREQUENCY,
666 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
667
668 if (hdl->error) {
669 pr_err("Could not initialize controls\n");
670 return hdl->error;
671 }
672 v4l2_ctrl_cluster(2, &sd->exposure);
673 return 0;
674}
675
676
677static const struct sd_desc sd_desc = {
678 .name = MODULE_NAME,
679 .config = sd_config,
680 .init = sd_init,
681 .init_controls = sd_init_controls,
682 .isoc_init = sd_isoc_init,
683 .start = sd_start,
684 .stopN = sd_stopN,
685 .dq_callback = sd_dq_callback,
686 .pkt_scan = sd_pkt_scan,
687#if IS_ENABLED(CONFIG_INPUT)
688 .int_pkt_scan = sd_int_pkt_scan,
689#endif
690};
691
692
693static const struct usb_device_id device_table[] = {
694 {USB_DEVICE(0x03e8, 0x0004)},
695 {USB_DEVICE(0x0471, 0x030b)},
696 {USB_DEVICE(0x047d, 0x5001)},
697 {USB_DEVICE(0x047d, 0x5002)},
698 {USB_DEVICE(0x047d, 0x5003)},
699 {}
700};
701MODULE_DEVICE_TABLE(usb, device_table);
702
703
704static int sd_probe(struct usb_interface *intf,
705 const struct usb_device_id *id)
706{
707 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
708 THIS_MODULE);
709}
710
711static int sd_pre_reset(struct usb_interface *intf)
712{
713 return 0;
714}
715
716static int sd_post_reset(struct usb_interface *intf)
717{
718 return 0;
719}
720
721static struct usb_driver sd_driver = {
722 .name = MODULE_NAME,
723 .id_table = device_table,
724 .probe = sd_probe,
725 .disconnect = gspca_disconnect,
726#ifdef CONFIG_PM
727 .suspend = gspca_suspend,
728 .resume = gspca_resume,
729 .reset_resume = gspca_resume,
730#endif
731 .pre_reset = sd_pre_reset,
732 .post_reset = sd_post_reset,
733};
734
735module_usb_driver(sd_driver);
736