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