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#define MODULE_NAME "konica"
32
33#include <linux/input.h>
34#include "gspca.h"
35
36MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
37MODULE_DESCRIPTION("Konica chipset USB Camera Driver");
38MODULE_LICENSE("GPL");
39
40#define WHITEBAL_REG 0x01
41#define BRIGHTNESS_REG 0x02
42#define SHARPNESS_REG 0x03
43#define CONTRAST_REG 0x04
44#define SATURATION_REG 0x05
45
46
47struct sd {
48 struct gspca_dev gspca_dev;
49 struct urb *last_data_urb;
50 u8 snapshot_pressed;
51 u8 brightness;
52 u8 contrast;
53 u8 saturation;
54 u8 whitebal;
55 u8 sharpness;
56};
57
58
59static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
60static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
61static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
62static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
63static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val);
64static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val);
65static int sd_setwhitebal(struct gspca_dev *gspca_dev, __s32 val);
66static int sd_getwhitebal(struct gspca_dev *gspca_dev, __s32 *val);
67static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
68static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
69
70static const struct ctrl sd_ctrls[] = {
71#define SD_BRIGHTNESS 0
72 {
73 {
74 .id = V4L2_CID_BRIGHTNESS,
75 .type = V4L2_CTRL_TYPE_INTEGER,
76 .name = "Brightness",
77 .minimum = 0,
78 .maximum = 9,
79 .step = 1,
80#define BRIGHTNESS_DEFAULT 4
81 .default_value = BRIGHTNESS_DEFAULT,
82 .flags = 0,
83 },
84 .set = sd_setbrightness,
85 .get = sd_getbrightness,
86 },
87#define SD_CONTRAST 1
88 {
89 {
90 .id = V4L2_CID_CONTRAST,
91 .type = V4L2_CTRL_TYPE_INTEGER,
92 .name = "Contrast",
93 .minimum = 0,
94 .maximum = 9,
95 .step = 4,
96#define CONTRAST_DEFAULT 10
97 .default_value = CONTRAST_DEFAULT,
98 .flags = 0,
99 },
100 .set = sd_setcontrast,
101 .get = sd_getcontrast,
102 },
103#define SD_SATURATION 2
104 {
105 {
106 .id = V4L2_CID_SATURATION,
107 .type = V4L2_CTRL_TYPE_INTEGER,
108 .name = "Saturation",
109 .minimum = 0,
110 .maximum = 9,
111 .step = 1,
112#define SATURATION_DEFAULT 4
113 .default_value = SATURATION_DEFAULT,
114 .flags = 0,
115 },
116 .set = sd_setsaturation,
117 .get = sd_getsaturation,
118 },
119#define SD_WHITEBAL 3
120 {
121 {
122 .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
123 .type = V4L2_CTRL_TYPE_INTEGER,
124 .name = "White Balance",
125 .minimum = 0,
126 .maximum = 33,
127 .step = 1,
128#define WHITEBAL_DEFAULT 25
129 .default_value = WHITEBAL_DEFAULT,
130 .flags = 0,
131 },
132 .set = sd_setwhitebal,
133 .get = sd_getwhitebal,
134 },
135#define SD_SHARPNESS 4
136 {
137 {
138 .id = V4L2_CID_SHARPNESS,
139 .type = V4L2_CTRL_TYPE_INTEGER,
140 .name = "Sharpness",
141 .minimum = 0,
142 .maximum = 9,
143 .step = 1,
144#define SHARPNESS_DEFAULT 4
145 .default_value = SHARPNESS_DEFAULT,
146 .flags = 0,
147 },
148 .set = sd_setsharpness,
149 .get = sd_getsharpness,
150 },
151};
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167static const struct v4l2_pix_format vga_mode[] = {
168 {160, 120, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
169 .bytesperline = 160,
170 .sizeimage = 160 * 136 * 3 / 2 + 960,
171 .colorspace = V4L2_COLORSPACE_SRGB,
172 .priv = 0x0a},
173 {176, 144, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
174 .bytesperline = 176,
175 .sizeimage = 176 * 144 * 3 / 2 + 960,
176 .colorspace = V4L2_COLORSPACE_SRGB,
177 .priv = 0x04},
178 {320, 240, V4L2_PIX_FMT_KONICA420, V4L2_FIELD_NONE,
179 .bytesperline = 320,
180 .sizeimage = 320 * 240 * 3 / 2 + 960,
181 .colorspace = V4L2_COLORSPACE_SRGB,
182 .priv = 0x05},
183};
184
185static void sd_isoc_irq(struct urb *urb);
186
187static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
188{
189 struct usb_device *dev = gspca_dev->dev;
190 int ret;
191
192 if (gspca_dev->usb_err < 0)
193 return;
194 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
195 0x02,
196 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
197 value,
198 index,
199 NULL,
200 0,
201 1000);
202 if (ret < 0) {
203 err("reg_w err %d", ret);
204 gspca_dev->usb_err = ret;
205 }
206}
207
208static void reg_r(struct gspca_dev *gspca_dev, u16 value, u16 index)
209{
210 struct usb_device *dev = gspca_dev->dev;
211 int ret;
212
213 if (gspca_dev->usb_err < 0)
214 return;
215 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
216 0x03,
217 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
218 value,
219 index,
220 gspca_dev->usb_buf,
221 2,
222 1000);
223 if (ret < 0) {
224 err("reg_w err %d", ret);
225 gspca_dev->usb_err = ret;
226 }
227}
228
229static void konica_stream_on(struct gspca_dev *gspca_dev)
230{
231 reg_w(gspca_dev, 1, 0x0b);
232}
233
234static void konica_stream_off(struct gspca_dev *gspca_dev)
235{
236 reg_w(gspca_dev, 0, 0x0b);
237}
238
239
240static int sd_config(struct gspca_dev *gspca_dev,
241 const struct usb_device_id *id)
242{
243 struct sd *sd = (struct sd *) gspca_dev;
244
245 gspca_dev->cam.cam_mode = vga_mode;
246 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
247 gspca_dev->cam.no_urb_create = 1;
248
249
250 gspca_dev->nbalt--;
251
252 sd->brightness = BRIGHTNESS_DEFAULT;
253 sd->contrast = CONTRAST_DEFAULT;
254 sd->saturation = SATURATION_DEFAULT;
255 sd->whitebal = WHITEBAL_DEFAULT;
256 sd->sharpness = SHARPNESS_DEFAULT;
257
258 return 0;
259}
260
261
262static int sd_init(struct gspca_dev *gspca_dev)
263{
264
265 reg_r(gspca_dev, 0, 0x10);
266 PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
267 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
268 reg_r(gspca_dev, 0, 0x10);
269 PDEBUG(D_PROBE, "Reg 0x10 reads: %02x %02x",
270 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
271 reg_w(gspca_dev, 0, 0x0d);
272
273 return 0;
274}
275
276static int sd_start(struct gspca_dev *gspca_dev)
277{
278 struct sd *sd = (struct sd *) gspca_dev;
279 struct urb *urb;
280 int i, n, packet_size;
281 struct usb_host_interface *alt;
282 struct usb_interface *intf;
283
284 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
285 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
286 if (!alt) {
287 err("Couldn't get altsetting");
288 return -EIO;
289 }
290
291 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
292
293 reg_w(gspca_dev, sd->brightness, BRIGHTNESS_REG);
294 reg_w(gspca_dev, sd->whitebal, WHITEBAL_REG);
295 reg_w(gspca_dev, sd->contrast, CONTRAST_REG);
296 reg_w(gspca_dev, sd->saturation, SATURATION_REG);
297 reg_w(gspca_dev, sd->sharpness, SHARPNESS_REG);
298
299 n = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
300 reg_w(gspca_dev, n, 0x08);
301
302 konica_stream_on(gspca_dev);
303
304 if (gspca_dev->usb_err)
305 return gspca_dev->usb_err;
306
307
308#if MAX_NURBS < 4
309#error "Not enough URBs in the gspca table"
310#endif
311#define SD_NPKT 32
312 for (n = 0; n < 4; n++) {
313 i = n & 1 ? 0 : 1;
314 packet_size =
315 le16_to_cpu(alt->endpoint[i].desc.wMaxPacketSize);
316 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
317 if (!urb) {
318 err("usb_alloc_urb failed");
319 return -ENOMEM;
320 }
321 gspca_dev->urb[n] = urb;
322 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
323 packet_size * SD_NPKT,
324 GFP_KERNEL,
325 &urb->transfer_dma);
326 if (urb->transfer_buffer == NULL) {
327 err("usb_buffer_alloc failed");
328 return -ENOMEM;
329 }
330
331 urb->dev = gspca_dev->dev;
332 urb->context = gspca_dev;
333 urb->transfer_buffer_length = packet_size * SD_NPKT;
334 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
335 n & 1 ? 0x81 : 0x82);
336 urb->transfer_flags = URB_ISO_ASAP
337 | URB_NO_TRANSFER_DMA_MAP;
338 urb->interval = 1;
339 urb->complete = sd_isoc_irq;
340 urb->number_of_packets = SD_NPKT;
341 for (i = 0; i < SD_NPKT; i++) {
342 urb->iso_frame_desc[i].length = packet_size;
343 urb->iso_frame_desc[i].offset = packet_size * i;
344 }
345 }
346
347 return 0;
348}
349
350static void sd_stopN(struct gspca_dev *gspca_dev)
351{
352 struct sd *sd = (struct sd *) gspca_dev;
353
354 konica_stream_off(gspca_dev);
355#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
356
357
358 if (sd->snapshot_pressed) {
359 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
360 input_sync(gspca_dev->input_dev);
361 sd->snapshot_pressed = 0;
362 }
363#endif
364}
365
366
367static void sd_isoc_irq(struct urb *urb)
368{
369 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
370 struct sd *sd = (struct sd *) gspca_dev;
371 struct urb *data_urb, *status_urb;
372 u8 *data;
373 int i, st;
374
375 PDEBUG(D_PACK, "sd isoc irq");
376 if (!gspca_dev->streaming)
377 return;
378
379 if (urb->status != 0) {
380 if (urb->status == -ESHUTDOWN)
381 return;
382#ifdef CONFIG_PM
383 if (gspca_dev->frozen)
384 return;
385#endif
386 PDEBUG(D_ERR, "urb status: %d", urb->status);
387 st = usb_submit_urb(urb, GFP_ATOMIC);
388 if (st < 0)
389 err("resubmit urb error %d", st);
390 return;
391 }
392
393
394 if (urb->transfer_buffer_length > 32) {
395 sd->last_data_urb = urb;
396 return;
397 }
398
399 status_urb = urb;
400 data_urb = sd->last_data_urb;
401 sd->last_data_urb = NULL;
402
403 if (!data_urb || data_urb->start_frame != status_urb->start_frame) {
404 PDEBUG(D_ERR|D_PACK, "lost sync on frames");
405 goto resubmit;
406 }
407
408 if (data_urb->number_of_packets != status_urb->number_of_packets) {
409 PDEBUG(D_ERR|D_PACK,
410 "no packets does not match, data: %d, status: %d",
411 data_urb->number_of_packets,
412 status_urb->number_of_packets);
413 goto resubmit;
414 }
415
416 for (i = 0; i < status_urb->number_of_packets; i++) {
417 if (data_urb->iso_frame_desc[i].status ||
418 status_urb->iso_frame_desc[i].status) {
419 PDEBUG(D_ERR|D_PACK,
420 "pkt %d data-status %d, status-status %d", i,
421 data_urb->iso_frame_desc[i].status,
422 status_urb->iso_frame_desc[i].status);
423 gspca_dev->last_packet_type = DISCARD_PACKET;
424 continue;
425 }
426
427 if (status_urb->iso_frame_desc[i].actual_length != 1) {
428 PDEBUG(D_ERR|D_PACK,
429 "bad status packet length %d",
430 status_urb->iso_frame_desc[i].actual_length);
431 gspca_dev->last_packet_type = DISCARD_PACKET;
432 continue;
433 }
434
435 st = *((u8 *)status_urb->transfer_buffer
436 + status_urb->iso_frame_desc[i].offset);
437
438 data = (u8 *)data_urb->transfer_buffer
439 + data_urb->iso_frame_desc[i].offset;
440
441
442
443
444
445
446
447
448
449
450 if (st & 0x80) {
451 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
452 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
453 } else {
454#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
455 u8 button_state = st & 0x40 ? 1 : 0;
456 if (sd->snapshot_pressed != button_state) {
457 input_report_key(gspca_dev->input_dev,
458 KEY_CAMERA,
459 button_state);
460 input_sync(gspca_dev->input_dev);
461 sd->snapshot_pressed = button_state;
462 }
463#endif
464 if (st & 0x01)
465 continue;
466 }
467 gspca_frame_add(gspca_dev, INTER_PACKET, data,
468 data_urb->iso_frame_desc[i].actual_length);
469 }
470
471resubmit:
472 if (data_urb) {
473 st = usb_submit_urb(data_urb, GFP_ATOMIC);
474 if (st < 0)
475 PDEBUG(D_ERR|D_PACK,
476 "usb_submit_urb(data_urb) ret %d", st);
477 }
478 st = usb_submit_urb(status_urb, GFP_ATOMIC);
479 if (st < 0)
480 err("usb_submit_urb(status_urb) ret %d", st);
481}
482
483static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
484{
485 struct sd *sd = (struct sd *) gspca_dev;
486
487 sd->brightness = val;
488 if (gspca_dev->streaming) {
489 konica_stream_off(gspca_dev);
490 reg_w(gspca_dev, sd->brightness, BRIGHTNESS_REG);
491 konica_stream_on(gspca_dev);
492 }
493
494 return 0;
495}
496
497static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
498{
499 struct sd *sd = (struct sd *) gspca_dev;
500
501 *val = sd->brightness;
502
503 return 0;
504}
505
506static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
507{
508 struct sd *sd = (struct sd *) gspca_dev;
509
510 sd->contrast = val;
511 if (gspca_dev->streaming) {
512 konica_stream_off(gspca_dev);
513 reg_w(gspca_dev, sd->contrast, CONTRAST_REG);
514 konica_stream_on(gspca_dev);
515 }
516
517 return 0;
518}
519
520static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
521{
522 struct sd *sd = (struct sd *) gspca_dev;
523
524 *val = sd->contrast;
525
526 return 0;
527}
528
529static int sd_setsaturation(struct gspca_dev *gspca_dev, __s32 val)
530{
531 struct sd *sd = (struct sd *) gspca_dev;
532
533 sd->saturation = val;
534 if (gspca_dev->streaming) {
535 konica_stream_off(gspca_dev);
536 reg_w(gspca_dev, sd->saturation, SATURATION_REG);
537 konica_stream_on(gspca_dev);
538 }
539 return 0;
540}
541
542static int sd_getsaturation(struct gspca_dev *gspca_dev, __s32 *val)
543{
544 struct sd *sd = (struct sd *) gspca_dev;
545
546 *val = sd->saturation;
547
548 return 0;
549}
550
551static int sd_setwhitebal(struct gspca_dev *gspca_dev, __s32 val)
552{
553 struct sd *sd = (struct sd *) gspca_dev;
554
555 sd->whitebal = val;
556 if (gspca_dev->streaming) {
557 konica_stream_off(gspca_dev);
558 reg_w(gspca_dev, sd->whitebal, WHITEBAL_REG);
559 konica_stream_on(gspca_dev);
560 }
561 return 0;
562}
563
564static int sd_getwhitebal(struct gspca_dev *gspca_dev, __s32 *val)
565{
566 struct sd *sd = (struct sd *) gspca_dev;
567
568 *val = sd->whitebal;
569
570 return 0;
571}
572
573static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
574{
575 struct sd *sd = (struct sd *) gspca_dev;
576
577 sd->sharpness = val;
578 if (gspca_dev->streaming) {
579 konica_stream_off(gspca_dev);
580 reg_w(gspca_dev, sd->sharpness, SHARPNESS_REG);
581 konica_stream_on(gspca_dev);
582 }
583 return 0;
584}
585
586static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
587{
588 struct sd *sd = (struct sd *) gspca_dev;
589
590 *val = sd->sharpness;
591
592 return 0;
593}
594
595
596static const struct sd_desc sd_desc = {
597 .name = MODULE_NAME,
598 .ctrls = sd_ctrls,
599 .nctrls = ARRAY_SIZE(sd_ctrls),
600 .config = sd_config,
601 .init = sd_init,
602 .start = sd_start,
603 .stopN = sd_stopN,
604#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
605 .other_input = 1,
606#endif
607};
608
609
610static const struct usb_device_id device_table[] = {
611 {USB_DEVICE(0x04c8, 0x0720)},
612 {}
613};
614MODULE_DEVICE_TABLE(usb, device_table);
615
616
617static int sd_probe(struct usb_interface *intf,
618 const struct usb_device_id *id)
619{
620 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
621 THIS_MODULE);
622}
623
624static struct usb_driver sd_driver = {
625 .name = MODULE_NAME,
626 .id_table = device_table,
627 .probe = sd_probe,
628 .disconnect = gspca_disconnect,
629#ifdef CONFIG_PM
630 .suspend = gspca_suspend,
631 .resume = gspca_resume,
632#endif
633};
634
635
636static int __init sd_mod_init(void)
637{
638 return usb_register(&sd_driver);
639}
640static void __exit sd_mod_exit(void)
641{
642 usb_deregister(&sd_driver);
643}
644
645module_init(sd_mod_init);
646module_exit(sd_mod_exit);
647