1 /*
2 * konicawc.c - konica webcam driver
3 *
4 * Author: Simon Evans <spse@secret.org.uk>
5 *
6 * Copyright (C) 2002 Simon Evans
7 *
8 * Licence: GPL
9 *
10 * Driver for USB webcams based on Konica chipset. This
11 * chipset is used in Intel YC76 camera.
12 *
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/usb/input.h>
19
20 #include "usbvideo.h"
21
22 #define MAX_BRIGHTNESS 108
23 #define MAX_CONTRAST 108
24 #define MAX_SATURATION 108
25 #define MAX_SHARPNESS 108
26 #define MAX_WHITEBAL 372
27 #define MAX_SPEED 6
28
29
30 #define MAX_CAMERAS 1
31
32 #define DRIVER_VERSION "v1.4"
33 #define DRIVER_DESC "Konica Webcam driver"
34
35 enum ctrl_req {
36 SetWhitebal = 0x01,
37 SetBrightness = 0x02,
38 SetSharpness = 0x03,
39 SetContrast = 0x04,
40 SetSaturation = 0x05,
41 };
42
43
44 enum frame_sizes {
45 SIZE_160X120 = 0,
46 SIZE_160X136 = 1,
47 SIZE_176X144 = 2,
48 SIZE_320X240 = 3,
49
50 };
51
52 #define MAX_FRAME_SIZE SIZE_320X240
53
54 static struct usbvideo *cams;
55
56 #ifdef CONFIG_USB_DEBUG
57 static int debug;
58 #define DEBUG(n, format, arg...) \
59 if (n <= debug) { \
60 printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __func__ , ## arg); \
61 }
62 #else
63 #define DEBUG(n, arg...)
64 static const int debug;
65 #endif
66
67
68 /* Some default values for initial camera settings,
69 can be set by modprobe */
70
71 static int size;
72 static int speed = 6; /* Speed (fps) 0 (slowest) to 6 (fastest) */
73 static int brightness = MAX_BRIGHTNESS/2;
74 static int contrast = MAX_CONTRAST/2;
75 static int saturation = MAX_SATURATION/2;
76 static int sharpness = MAX_SHARPNESS/2;
77 static int whitebal = 3*(MAX_WHITEBAL/4);
78
79 static const int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
80
81 /* These FPS speeds are from the windows config box. They are
82 * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
83 * real fps.
84 */
85
86 static const int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
87 { 24, 40, 48, 60, 72, 80, 100 },
88 { 18, 30, 36, 45, 54, 60, 75 },
89 { 6, 10, 12, 15, 18, 21, 25 } };
90
91 struct cam_size {
92 u16 width;
93 u16 height;
94 u8 cmd;
95 };
96
97 static const struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
98 { 160, 136, 0xa },
99 { 176, 144, 0x4 },
100 { 320, 240, 0x5 } };
101
102 struct konicawc {
103 u8 brightness; /* camera uses 0 - 9, x11 for real value */
104 u8 contrast; /* as above */
105 u8 saturation; /* as above */
106 u8 sharpness; /* as above */
107 u8 white_bal; /* 0 - 33, x11 for real value */
108 u8 speed; /* Stored as 0 - 6, used as index in spd_to_* (above) */
109 u8 size; /* Frame Size */
110 int height;
111 int width;
112 struct urb *sts_urb[USBVIDEO_NUMSBUF];
113 u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
114 struct urb *last_data_urb;
115 int lastframe;
116 int cur_frame_size; /* number of bytes in current frame size */
117 int maxline; /* number of lines per frame */
118 int yplanesz; /* Number of bytes in the Y plane */
119 unsigned int buttonsts:1;
120 #ifdef CONFIG_INPUT
121 struct input_dev *input;
122 char input_physname[64];
123 #endif
124 };
125
126
127 #define konicawc_set_misc(uvd, req, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
128 #define konicawc_get_misc(uvd, req, value, index, buf, sz) konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
129 #define konicawc_set_value(uvd, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
130
131
konicawc_ctrl_msg(struct uvd * uvd,u8 dir,u8 request,u16 value,u16 index,void * buf,int len)132 static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
133 {
134 int retval = usb_control_msg(uvd->dev,
135 dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
136 request, 0x40 | dir, value, index, buf, len, 1000);
137 return retval < 0 ? retval : 0;
138 }
139
140
konicawc_camera_on(struct uvd * uvd)141 static inline void konicawc_camera_on(struct uvd *uvd)
142 {
143 DEBUG(0, "camera on");
144 konicawc_set_misc(uvd, 0x2, 1, 0x0b);
145 }
146
147
konicawc_camera_off(struct uvd * uvd)148 static inline void konicawc_camera_off(struct uvd *uvd)
149 {
150 DEBUG(0, "camera off");
151 konicawc_set_misc(uvd, 0x2, 0, 0x0b);
152 }
153
154
konicawc_set_camera_size(struct uvd * uvd)155 static void konicawc_set_camera_size(struct uvd *uvd)
156 {
157 struct konicawc *cam = (struct konicawc *)uvd->user_data;
158
159 konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
160 cam->width = camera_sizes[cam->size].width;
161 cam->height = camera_sizes[cam->size].height;
162 cam->yplanesz = cam->height * cam->width;
163 cam->cur_frame_size = (cam->yplanesz * 3) / 2;
164 cam->maxline = cam->yplanesz / 256;
165 uvd->videosize = VIDEOSIZE(cam->width, cam->height);
166 }
167
168
konicawc_setup_on_open(struct uvd * uvd)169 static int konicawc_setup_on_open(struct uvd *uvd)
170 {
171 struct konicawc *cam = (struct konicawc *)uvd->user_data;
172
173 DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
174 cam->brightness * 11);
175 konicawc_set_value(uvd, cam->brightness, SetBrightness);
176 DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
177 cam->white_bal * 11);
178 konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
179 DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
180 cam->contrast * 11);
181 konicawc_set_value(uvd, cam->contrast, SetContrast);
182 DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
183 cam->saturation * 11);
184 konicawc_set_value(uvd, cam->saturation, SetSaturation);
185 DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
186 cam->sharpness * 11);
187 konicawc_set_value(uvd, cam->sharpness, SetSharpness);
188 konicawc_set_camera_size(uvd);
189 cam->lastframe = -2;
190 cam->buttonsts = 0;
191 return 0;
192 }
193
194
konicawc_adjust_picture(struct uvd * uvd)195 static void konicawc_adjust_picture(struct uvd *uvd)
196 {
197 struct konicawc *cam = (struct konicawc *)uvd->user_data;
198
199 konicawc_camera_off(uvd);
200 DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
201 uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
202 if(cam->brightness != uvd->vpic.brightness / 11) {
203 cam->brightness = uvd->vpic.brightness / 11;
204 DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
205 cam->brightness * 11);
206 konicawc_set_value(uvd, cam->brightness, SetBrightness);
207 }
208
209 DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
210 uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
211 if(cam->contrast != uvd->vpic.contrast / 11) {
212 cam->contrast = uvd->vpic.contrast / 11;
213 DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
214 cam->contrast * 11);
215 konicawc_set_value(uvd, cam->contrast, SetContrast);
216 }
217 konicawc_camera_on(uvd);
218 }
219
220 #ifdef CONFIG_INPUT
221
konicawc_register_input(struct konicawc * cam,struct usb_device * dev)222 static void konicawc_register_input(struct konicawc *cam, struct usb_device *dev)
223 {
224 struct input_dev *input_dev;
225 int error;
226
227 usb_make_path(dev, cam->input_physname, sizeof(cam->input_physname));
228 strncat(cam->input_physname, "/input0", sizeof(cam->input_physname));
229
230 cam->input = input_dev = input_allocate_device();
231 if (!input_dev) {
232 dev_warn(&dev->dev,
233 "Not enough memory for camera's input device\n");
234 return;
235 }
236
237 input_dev->name = "Konicawc snapshot button";
238 input_dev->phys = cam->input_physname;
239 usb_to_input_id(dev, &input_dev->id);
240 input_dev->dev.parent = &dev->dev;
241
242 input_dev->evbit[0] = BIT_MASK(EV_KEY);
243 input_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
244
245 error = input_register_device(cam->input);
246 if (error) {
247 dev_warn(&dev->dev,
248 "Failed to register camera's input device, err: %d\n",
249 error);
250 input_free_device(cam->input);
251 cam->input = NULL;
252 }
253 }
254
konicawc_unregister_input(struct konicawc * cam)255 static void konicawc_unregister_input(struct konicawc *cam)
256 {
257 if (cam->input) {
258 input_unregister_device(cam->input);
259 cam->input = NULL;
260 }
261 }
262
konicawc_report_buttonstat(struct konicawc * cam)263 static void konicawc_report_buttonstat(struct konicawc *cam)
264 {
265 if (cam->input) {
266 input_report_key(cam->input, BTN_0, cam->buttonsts);
267 input_sync(cam->input);
268 }
269 }
270
271 #else
272
konicawc_register_input(struct konicawc * cam,struct usb_device * dev)273 static inline void konicawc_register_input(struct konicawc *cam, struct usb_device *dev) { }
konicawc_unregister_input(struct konicawc * cam)274 static inline void konicawc_unregister_input(struct konicawc *cam) { }
konicawc_report_buttonstat(struct konicawc * cam)275 static inline void konicawc_report_buttonstat(struct konicawc *cam) { }
276
277 #endif /* CONFIG_INPUT */
278
konicawc_compress_iso(struct uvd * uvd,struct urb * dataurb,struct urb * stsurb)279 static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
280 {
281 char *cdata;
282 int i, totlen = 0;
283 unsigned char *status = stsurb->transfer_buffer;
284 int keep = 0, discard = 0, bad = 0;
285 struct konicawc *cam = (struct konicawc *)uvd->user_data;
286
287 for (i = 0; i < dataurb->number_of_packets; i++) {
288 int button = cam->buttonsts;
289 unsigned char sts;
290 int n = dataurb->iso_frame_desc[i].actual_length;
291 int st = dataurb->iso_frame_desc[i].status;
292 cdata = dataurb->transfer_buffer +
293 dataurb->iso_frame_desc[i].offset;
294
295 /* Detect and ignore errored packets */
296 if (st < 0) {
297 DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
298 i, n, st);
299 uvd->stats.iso_err_count++;
300 continue;
301 }
302
303 /* Detect and ignore empty packets */
304 if (n <= 0) {
305 uvd->stats.iso_skip_count++;
306 continue;
307 }
308
309 /* See what the status data said about the packet */
310 sts = *(status+stsurb->iso_frame_desc[i].offset);
311
312 /* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
313 * otherwise:
314 * bit 0 0: keep packet
315 * 1: drop packet (padding data)
316 *
317 * bit 4 0 button not clicked
318 * 1 button clicked
319 * button is used to `take a picture' (in software)
320 */
321
322 if(sts < 0x80) {
323 button = !!(sts & 0x40);
324 sts &= ~0x40;
325 }
326
327 /* work out the button status, but don't do
328 anything with it for now */
329
330 if(button != cam->buttonsts) {
331 DEBUG(2, "button: %sclicked", button ? "" : "un");
332 cam->buttonsts = button;
333 konicawc_report_buttonstat(cam);
334 }
335
336 if(sts == 0x01) { /* drop frame */
337 discard++;
338 continue;
339 }
340
341 if((sts > 0x01) && (sts < 0x80)) {
342 dev_info(&uvd->dev->dev, "unknown status %2.2x\n",
343 sts);
344 bad++;
345 continue;
346 }
347 if(!sts && cam->lastframe == -2) {
348 DEBUG(2, "dropping frame looking for image start");
349 continue;
350 }
351
352 keep++;
353 if(sts & 0x80) { /* frame start */
354 unsigned char marker[] = { 0, 0xff, 0, 0x00 };
355
356 if(cam->lastframe == -2) {
357 DEBUG(2, "found initial image");
358 cam->lastframe = -1;
359 }
360
361 marker[3] = sts & 0x7F;
362 RingQueue_Enqueue(&uvd->dp, marker, 4);
363 totlen += 4;
364 }
365
366 totlen += n; /* Little local accounting */
367 RingQueue_Enqueue(&uvd->dp, cdata, n);
368 }
369 DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
370 keep, discard, bad, totlen);
371 return totlen;
372 }
373
374
resubmit_urb(struct uvd * uvd,struct urb * urb)375 static void resubmit_urb(struct uvd *uvd, struct urb *urb)
376 {
377 int i, ret;
378 for (i = 0; i < FRAMES_PER_DESC; i++) {
379 urb->iso_frame_desc[i].status = 0;
380 }
381 urb->dev = uvd->dev;
382 urb->status = 0;
383 ret = usb_submit_urb(urb, GFP_ATOMIC);
384 DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
385 if(ret)
386 err("usb_submit_urb error (%d)", ret);
387
388 }
389
390
konicawc_isoc_irq(struct urb * urb)391 static void konicawc_isoc_irq(struct urb *urb)
392 {
393 struct uvd *uvd = urb->context;
394 struct konicawc *cam = (struct konicawc *)uvd->user_data;
395
396 /* We don't want to do anything if we are about to be removed! */
397 if (!CAMERA_IS_OPERATIONAL(uvd))
398 return;
399
400 if (!uvd->streaming) {
401 DEBUG(1, "Not streaming, but interrupt!");
402 return;
403 }
404
405 DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
406
407 uvd->stats.urb_count++;
408
409 if (urb->transfer_buffer_length > 32) {
410 cam->last_data_urb = urb;
411 return;
412 }
413 /* Copy the data received into ring queue */
414 if(cam->last_data_urb) {
415 int len = 0;
416 if(urb->start_frame != cam->last_data_urb->start_frame)
417 err("Lost sync on frames");
418 else if (!urb->status && !cam->last_data_urb->status)
419 len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
420
421 resubmit_urb(uvd, cam->last_data_urb);
422 resubmit_urb(uvd, urb);
423 cam->last_data_urb = NULL;
424 uvd->stats.urb_length = len;
425 uvd->stats.data_count += len;
426 if(len)
427 RingQueue_WakeUpInterruptible(&uvd->dp);
428 return;
429 }
430 return;
431 }
432
433
konicawc_start_data(struct uvd * uvd)434 static int konicawc_start_data(struct uvd *uvd)
435 {
436 struct usb_device *dev = uvd->dev;
437 int i, errFlag;
438 struct konicawc *cam = (struct konicawc *)uvd->user_data;
439 int pktsz;
440 struct usb_interface *intf;
441 struct usb_host_interface *interface = NULL;
442
443 intf = usb_ifnum_to_if(dev, uvd->iface);
444 if (intf)
445 interface = usb_altnum_to_altsetting(intf,
446 spd_to_iface[cam->speed]);
447 if (!interface)
448 return -ENXIO;
449 pktsz = le16_to_cpu(interface->endpoint[1].desc.wMaxPacketSize);
450 DEBUG(1, "pktsz = %d", pktsz);
451 if (!CAMERA_IS_OPERATIONAL(uvd)) {
452 err("Camera is not operational");
453 return -EFAULT;
454 }
455 uvd->curframe = -1;
456 konicawc_camera_on(uvd);
457 /* Alternate interface 1 is is the biggest frame size */
458 i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
459 if (i < 0) {
460 err("usb_set_interface error");
461 uvd->last_error = i;
462 return -EBUSY;
463 }
464
465 /* We double buffer the Iso lists */
466 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
467 int j, k;
468 struct urb *urb = uvd->sbuf[i].urb;
469 urb->dev = dev;
470 urb->context = uvd;
471 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
472 urb->interval = 1;
473 urb->transfer_flags = URB_ISO_ASAP;
474 urb->transfer_buffer = uvd->sbuf[i].data;
475 urb->complete = konicawc_isoc_irq;
476 urb->number_of_packets = FRAMES_PER_DESC;
477 urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
478 for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
479 urb->iso_frame_desc[j].offset = k;
480 urb->iso_frame_desc[j].length = pktsz;
481 }
482
483 urb = cam->sts_urb[i];
484 urb->dev = dev;
485 urb->context = uvd;
486 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
487 urb->interval = 1;
488 urb->transfer_flags = URB_ISO_ASAP;
489 urb->transfer_buffer = cam->sts_buf[i];
490 urb->complete = konicawc_isoc_irq;
491 urb->number_of_packets = FRAMES_PER_DESC;
492 urb->transfer_buffer_length = FRAMES_PER_DESC;
493 for (j=0; j < FRAMES_PER_DESC; j++) {
494 urb->iso_frame_desc[j].offset = j;
495 urb->iso_frame_desc[j].length = 1;
496 }
497 }
498
499 cam->last_data_urb = NULL;
500
501 /* Submit all URBs */
502 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
503 errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
504 if (errFlag)
505 err("usb_submit_isoc(%d) ret %d", i, errFlag);
506
507 errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
508 if (errFlag)
509 err ("usb_submit_isoc(%d) ret %d", i, errFlag);
510 }
511
512 uvd->streaming = 1;
513 DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
514 return 0;
515 }
516
517
konicawc_stop_data(struct uvd * uvd)518 static void konicawc_stop_data(struct uvd *uvd)
519 {
520 int i, j;
521 struct konicawc *cam;
522
523 if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
524 return;
525
526 konicawc_camera_off(uvd);
527 uvd->streaming = 0;
528 cam = (struct konicawc *)uvd->user_data;
529 cam->last_data_urb = NULL;
530
531 /* Unschedule all of the iso td's */
532 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
533 usb_kill_urb(uvd->sbuf[i].urb);
534 usb_kill_urb(cam->sts_urb[i]);
535 }
536
537 if (!uvd->remove_pending) {
538 /* Set packet size to 0 */
539 j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
540 if (j < 0) {
541 err("usb_set_interface() error %d.", j);
542 uvd->last_error = j;
543 }
544 }
545 }
546
547
konicawc_process_isoc(struct uvd * uvd,struct usbvideo_frame * frame)548 static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
549 {
550 struct konicawc *cam = (struct konicawc *)uvd->user_data;
551 int maxline = cam->maxline;
552 int yplanesz = cam->yplanesz;
553
554 assert(frame != NULL);
555
556 DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
557 DEBUG(3, "Frame state = %d", frame->scanstate);
558
559 if(frame->scanstate == ScanState_Scanning) {
560 int drop = 0;
561 int curframe;
562 int fdrops = 0;
563 DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
564 while(RingQueue_GetLength(&uvd->dp) >= 4) {
565 if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
566 (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
567 (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
568 (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
569 curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
570 if(cam->lastframe >= 0) {
571 fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
572 fdrops--;
573 if(fdrops) {
574 dev_info(&uvd->dev->dev,
575 "Dropped %d frames "
576 "(%d -> %d)\n",
577 fdrops,
578 cam->lastframe,
579 curframe);
580 }
581 }
582 cam->lastframe = curframe;
583 frame->curline = 0;
584 frame->scanstate = ScanState_Lines;
585 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
586 break;
587 }
588 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
589 drop++;
590 }
591 if(drop)
592 DEBUG(2, "dropped %d bytes looking for new frame", drop);
593 }
594
595 if(frame->scanstate == ScanState_Scanning)
596 return;
597
598 /* Try to move data from queue into frame buffer
599 * We get data in blocks of 384 bytes made up of:
600 * 256 Y, 64 U, 64 V.
601 * This needs to be written out as a Y plane, a U plane and a V plane.
602 */
603
604 while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
605 /* Y */
606 RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
607 /* U */
608 RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
609 /* V */
610 RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
611 frame->seqRead_Length += 384;
612 frame->curline++;
613 }
614 /* See if we filled the frame */
615 if (frame->curline == maxline) {
616 DEBUG(5, "got whole frame");
617
618 frame->frameState = FrameState_Done_Hold;
619 frame->curline = 0;
620 uvd->curframe = -1;
621 uvd->stats.frame_num++;
622 }
623 }
624
625
konicawc_find_fps(int size,int fps)626 static int konicawc_find_fps(int size, int fps)
627 {
628 int i;
629
630 fps *= 3;
631 DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
632 if(fps <= spd_to_fps[size][0])
633 return 0;
634
635 if(fps >= spd_to_fps[size][MAX_SPEED])
636 return MAX_SPEED;
637
638 for(i = 0; i < MAX_SPEED; i++) {
639 if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
640 DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
641 if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
642 return i;
643 else
644 return i+1;
645 }
646 }
647 return MAX_SPEED+1;
648 }
649
650
konicawc_set_video_mode(struct uvd * uvd,struct video_window * vw)651 static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
652 {
653 struct konicawc *cam = (struct konicawc *)uvd->user_data;
654 int newspeed = cam->speed;
655 int newsize;
656 int x = vw->width;
657 int y = vw->height;
658 int fps = vw->flags;
659
660 if(x > 0 && y > 0) {
661 DEBUG(2, "trying to find size %d,%d", x, y);
662 for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
663 if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
664 break;
665 }
666 } else {
667 newsize = cam->size;
668 }
669
670 if(newsize > MAX_FRAME_SIZE) {
671 DEBUG(1, "couldn't find size %d,%d", x, y);
672 return -EINVAL;
673 }
674
675 if(fps > 0) {
676 DEBUG(1, "trying to set fps to %d", fps);
677 newspeed = konicawc_find_fps(newsize, fps);
678 DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
679 }
680
681 if(newspeed > MAX_SPEED)
682 return -EINVAL;
683
684 DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
685 if((newsize == cam->size) && (newspeed == cam->speed)) {
686 DEBUG(1, "Nothing to do");
687 return 0;
688 }
689 DEBUG(0, "setting to %dx%d @ %d fps", camera_sizes[newsize].width,
690 camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
691
692 konicawc_stop_data(uvd);
693 uvd->ifaceAltActive = spd_to_iface[newspeed];
694 DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
695 cam->speed = newspeed;
696
697 if(cam->size != newsize) {
698 cam->size = newsize;
699 konicawc_set_camera_size(uvd);
700 }
701
702 /* Flush the input queue and clear any current frame in progress */
703
704 RingQueue_Flush(&uvd->dp);
705 cam->lastframe = -2;
706 if(uvd->curframe != -1) {
707 uvd->frame[uvd->curframe].curline = 0;
708 uvd->frame[uvd->curframe].seqRead_Length = 0;
709 uvd->frame[uvd->curframe].seqRead_Index = 0;
710 }
711
712 konicawc_start_data(uvd);
713 return 0;
714 }
715
716
konicawc_calculate_fps(struct uvd * uvd)717 static int konicawc_calculate_fps(struct uvd *uvd)
718 {
719 struct konicawc *cam = uvd->user_data;
720 return spd_to_fps[cam->size][cam->speed]/3;
721 }
722
723
konicawc_configure_video(struct uvd * uvd)724 static void konicawc_configure_video(struct uvd *uvd)
725 {
726 struct konicawc *cam = (struct konicawc *)uvd->user_data;
727 u8 buf[2];
728
729 memset(&uvd->vpic, 0, sizeof(uvd->vpic));
730 memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
731
732 RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
733 RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
734 RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
735 RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
736 RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
737
738 cam->brightness = brightness / 11;
739 cam->contrast = contrast / 11;
740 cam->saturation = saturation / 11;
741 cam->sharpness = sharpness / 11;
742 cam->white_bal = whitebal / 11;
743
744 uvd->vpic.colour = 108;
745 uvd->vpic.hue = 108;
746 uvd->vpic.brightness = brightness;
747 uvd->vpic.contrast = contrast;
748 uvd->vpic.whiteness = whitebal;
749 uvd->vpic.depth = 6;
750 uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
751
752 memset(&uvd->vcap, 0, sizeof(uvd->vcap));
753 strcpy(uvd->vcap.name, "Konica Webcam");
754 uvd->vcap.type = VID_TYPE_CAPTURE;
755 uvd->vcap.channels = 1;
756 uvd->vcap.audios = 0;
757 uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
758 uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
759 uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
760 uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
761
762 memset(&uvd->vchan, 0, sizeof(uvd->vchan));
763 uvd->vchan.flags = 0 ;
764 uvd->vchan.tuners = 0;
765 uvd->vchan.channel = 0;
766 uvd->vchan.type = VIDEO_TYPE_CAMERA;
767 strcpy(uvd->vchan.name, "Camera");
768
769 /* Talk to device */
770 DEBUG(1, "device init");
771 if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
772 DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
773 if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
774 DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
775 if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
776 DEBUG(2, "2,0,d failed");
777 DEBUG(1, "setting initial values");
778 }
779
konicawc_probe(struct usb_interface * intf,const struct usb_device_id * devid)780 static int konicawc_probe(struct usb_interface *intf, const struct usb_device_id *devid)
781 {
782 struct usb_device *dev = interface_to_usbdev(intf);
783 struct uvd *uvd = NULL;
784 int ix, i, nas;
785 int actInterface=-1, inactInterface=-1, maxPS=0;
786 unsigned char video_ep = 0;
787
788 DEBUG(1, "konicawc_probe(%p)", intf);
789
790 /* We don't handle multi-config cameras */
791 if (dev->descriptor.bNumConfigurations != 1)
792 return -ENODEV;
793
794 dev_info(&intf->dev, "Konica Webcam (rev. 0x%04x)\n",
795 le16_to_cpu(dev->descriptor.bcdDevice));
796 RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
797
798 /* Validate found interface: must have one ISO endpoint */
799 nas = intf->num_altsetting;
800 if (nas != 8) {
801 err("Incorrect number of alternate settings (%d) for this camera!", nas);
802 return -ENODEV;
803 }
804 /* Validate all alternate settings */
805 for (ix=0; ix < nas; ix++) {
806 const struct usb_host_interface *interface;
807 const struct usb_endpoint_descriptor *endpoint;
808
809 interface = &intf->altsetting[ix];
810 i = interface->desc.bAlternateSetting;
811 if (interface->desc.bNumEndpoints != 2) {
812 err("Interface %d. has %u. endpoints!",
813 interface->desc.bInterfaceNumber,
814 (unsigned)(interface->desc.bNumEndpoints));
815 return -ENODEV;
816 }
817 endpoint = &interface->endpoint[1].desc;
818 DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
819 endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize));
820 if (video_ep == 0)
821 video_ep = endpoint->bEndpointAddress;
822 else if (video_ep != endpoint->bEndpointAddress) {
823 err("Alternate settings have different endpoint addresses!");
824 return -ENODEV;
825 }
826 if (!usb_endpoint_xfer_isoc(endpoint)) {
827 err("Interface %d. has non-ISO endpoint!",
828 interface->desc.bInterfaceNumber);
829 return -ENODEV;
830 }
831 if (usb_endpoint_dir_out(endpoint)) {
832 err("Interface %d. has ISO OUT endpoint!",
833 interface->desc.bInterfaceNumber);
834 return -ENODEV;
835 }
836 if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
837 if (inactInterface < 0)
838 inactInterface = i;
839 else {
840 err("More than one inactive alt. setting!");
841 return -ENODEV;
842 }
843 } else {
844 if (i == spd_to_iface[speed]) {
845 /* This one is the requested one */
846 actInterface = i;
847 }
848 }
849 if (le16_to_cpu(endpoint->wMaxPacketSize) > maxPS)
850 maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
851 }
852 if(actInterface == -1) {
853 err("Cant find required endpoint");
854 return -ENODEV;
855 }
856
857 DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
858
859 uvd = usbvideo_AllocateDevice(cams);
860 if (uvd != NULL) {
861 struct konicawc *cam = (struct konicawc *)(uvd->user_data);
862 /* Here uvd is a fully allocated uvd object */
863 for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
864 cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
865 if(cam->sts_urb[i] == NULL) {
866 while(i--) {
867 usb_free_urb(cam->sts_urb[i]);
868 }
869 err("can't allocate urbs");
870 return -ENOMEM;
871 }
872 }
873 cam->speed = speed;
874 RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
875 cam->width = camera_sizes[size].width;
876 cam->height = camera_sizes[size].height;
877 cam->size = size;
878
879 uvd->flags = 0;
880 uvd->debug = debug;
881 uvd->dev = dev;
882 uvd->iface = intf->altsetting->desc.bInterfaceNumber;
883 uvd->ifaceAltInactive = inactInterface;
884 uvd->ifaceAltActive = actInterface;
885 uvd->video_endp = video_ep;
886 uvd->iso_packet_len = maxPS;
887 uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
888 uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
889 uvd->canvas = VIDEOSIZE(320, 240);
890 uvd->videosize = VIDEOSIZE(cam->width, cam->height);
891
892 /* Initialize konicawc specific data */
893 konicawc_configure_video(uvd);
894
895 i = usbvideo_RegisterVideoDevice(uvd);
896 uvd->max_frame_size = (320 * 240 * 3)/2;
897 if (i != 0) {
898 err("usbvideo_RegisterVideoDevice() failed.");
899 uvd = NULL;
900 }
901
902 konicawc_register_input(cam, dev);
903 }
904
905 if (uvd) {
906 usb_set_intfdata (intf, uvd);
907 return 0;
908 }
909 return -EIO;
910 }
911
912
konicawc_free_uvd(struct uvd * uvd)913 static void konicawc_free_uvd(struct uvd *uvd)
914 {
915 int i;
916 struct konicawc *cam = (struct konicawc *)uvd->user_data;
917
918 konicawc_unregister_input(cam);
919
920 for (i = 0; i < USBVIDEO_NUMSBUF; i++) {
921 usb_free_urb(cam->sts_urb[i]);
922 cam->sts_urb[i] = NULL;
923 }
924 }
925
926
927 static struct usb_device_id id_table[] = {
928 { USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
929 { } /* Terminating entry */
930 };
931
932
konicawc_init(void)933 static int __init konicawc_init(void)
934 {
935 struct usbvideo_cb cbTbl;
936 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
937 DRIVER_DESC "\n");
938 memset(&cbTbl, 0, sizeof(cbTbl));
939 cbTbl.probe = konicawc_probe;
940 cbTbl.setupOnOpen = konicawc_setup_on_open;
941 cbTbl.processData = konicawc_process_isoc;
942 cbTbl.getFPS = konicawc_calculate_fps;
943 cbTbl.setVideoMode = konicawc_set_video_mode;
944 cbTbl.startDataPump = konicawc_start_data;
945 cbTbl.stopDataPump = konicawc_stop_data;
946 cbTbl.adjustPicture = konicawc_adjust_picture;
947 cbTbl.userFree = konicawc_free_uvd;
948 return usbvideo_register(
949 &cams,
950 MAX_CAMERAS,
951 sizeof(struct konicawc),
952 "konicawc",
953 &cbTbl,
954 THIS_MODULE,
955 id_table);
956 }
957
958
konicawc_cleanup(void)959 static void __exit konicawc_cleanup(void)
960 {
961 usbvideo_Deregister(&cams);
962 }
963
964
965 MODULE_DEVICE_TABLE(usb, id_table);
966
967 MODULE_LICENSE("GPL");
968 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
969 MODULE_DESCRIPTION(DRIVER_DESC);
970 module_param(speed, int, 0);
971 MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
972 module_param(size, int, 0);
973 MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
974 module_param(brightness, int, 0);
975 MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
976 module_param(contrast, int, 0);
977 MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
978 module_param(saturation, int, 0);
979 MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
980 module_param(sharpness, int, 0);
981 MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
982 module_param(whitebal, int, 0);
983 MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
984
985 #ifdef CONFIG_USB_DEBUG
986 module_param(debug, int, S_IRUGO | S_IWUSR);
987 MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
988 #endif
989
990 module_init(konicawc_init);
991 module_exit(konicawc_cleanup);
992