Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License as published by | |
4 | * the Free Software Foundation; either version 2, or (at your option) | |
5 | * any later version. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, | |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU General Public License | |
13 | * along with this program; if not, write to the Free Software | |
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
15 | */ | |
16 | ||
17 | #include <linux/kernel.h> | |
18 | #include <linux/sched.h> | |
19 | #include <linux/list.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/mm.h> | |
23 | #include <linux/smp_lock.h> | |
24 | #include <linux/vmalloc.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/spinlock.h> | |
27 | ||
28 | #include <asm/io.h> | |
29 | ||
30 | #include "usbvideo.h" | |
31 | ||
32 | #if defined(MAP_NR) | |
33 | #define virt_to_page(v) MAP_NR(v) /* Kernels 2.2.x */ | |
34 | #endif | |
35 | ||
36 | static int video_nr = -1; | |
37 | module_param(video_nr, int, 0); | |
38 | ||
39 | /* | |
40 | * Local prototypes. | |
41 | */ | |
42 | static void usbvideo_Disconnect(struct usb_interface *intf); | |
43 | static void usbvideo_CameraRelease(struct uvd *uvd); | |
44 | ||
45 | static int usbvideo_v4l_ioctl(struct inode *inode, struct file *file, | |
46 | unsigned int cmd, unsigned long arg); | |
47 | static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma); | |
48 | static int usbvideo_v4l_open(struct inode *inode, struct file *file); | |
49 | static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf, | |
50 | size_t count, loff_t *ppos); | |
51 | static int usbvideo_v4l_close(struct inode *inode, struct file *file); | |
52 | ||
53 | static int usbvideo_StartDataPump(struct uvd *uvd); | |
54 | static void usbvideo_StopDataPump(struct uvd *uvd); | |
55 | static int usbvideo_GetFrame(struct uvd *uvd, int frameNum); | |
56 | static int usbvideo_NewFrame(struct uvd *uvd, int framenum); | |
57 | static void usbvideo_SoftwareContrastAdjustment(struct uvd *uvd, | |
58 | struct usbvideo_frame *frame); | |
59 | ||
60 | /*******************************/ | |
61 | /* Memory management functions */ | |
62 | /*******************************/ | |
63 | static void *usbvideo_rvmalloc(unsigned long size) | |
64 | { | |
65 | void *mem; | |
66 | unsigned long adr; | |
67 | ||
68 | size = PAGE_ALIGN(size); | |
69 | mem = vmalloc_32(size); | |
70 | if (!mem) | |
71 | return NULL; | |
72 | ||
73 | memset(mem, 0, size); /* Clear the ram out, no junk to the user */ | |
74 | adr = (unsigned long) mem; | |
75 | while (size > 0) { | |
76 | SetPageReserved(vmalloc_to_page((void *)adr)); | |
77 | adr += PAGE_SIZE; | |
78 | size -= PAGE_SIZE; | |
79 | } | |
80 | ||
81 | return mem; | |
82 | } | |
83 | ||
84 | static void usbvideo_rvfree(void *mem, unsigned long size) | |
85 | { | |
86 | unsigned long adr; | |
87 | ||
88 | if (!mem) | |
89 | return; | |
90 | ||
91 | adr = (unsigned long) mem; | |
92 | while ((long) size > 0) { | |
93 | ClearPageReserved(vmalloc_to_page((void *)adr)); | |
94 | adr += PAGE_SIZE; | |
95 | size -= PAGE_SIZE; | |
96 | } | |
97 | vfree(mem); | |
98 | } | |
99 | ||
100 | static void RingQueue_Initialize(struct RingQueue *rq) | |
101 | { | |
102 | assert(rq != NULL); | |
103 | init_waitqueue_head(&rq->wqh); | |
104 | } | |
105 | ||
106 | static void RingQueue_Allocate(struct RingQueue *rq, int rqLen) | |
107 | { | |
108 | /* Make sure the requested size is a power of 2 and | |
109 | round up if necessary. This allows index wrapping | |
110 | using masks rather than modulo */ | |
111 | ||
112 | int i = 1; | |
113 | assert(rq != NULL); | |
114 | assert(rqLen > 0); | |
115 | ||
116 | while(rqLen >> i) | |
117 | i++; | |
118 | if(rqLen != 1 << (i-1)) | |
119 | rqLen = 1 << i; | |
120 | ||
121 | rq->length = rqLen; | |
122 | rq->ri = rq->wi = 0; | |
123 | rq->queue = usbvideo_rvmalloc(rq->length); | |
124 | assert(rq->queue != NULL); | |
125 | } | |
126 | ||
127 | static int RingQueue_IsAllocated(const struct RingQueue *rq) | |
128 | { | |
129 | if (rq == NULL) | |
130 | return 0; | |
131 | return (rq->queue != NULL) && (rq->length > 0); | |
132 | } | |
133 | ||
134 | static void RingQueue_Free(struct RingQueue *rq) | |
135 | { | |
136 | assert(rq != NULL); | |
137 | if (RingQueue_IsAllocated(rq)) { | |
138 | usbvideo_rvfree(rq->queue, rq->length); | |
139 | rq->queue = NULL; | |
140 | rq->length = 0; | |
141 | } | |
142 | } | |
143 | ||
144 | int RingQueue_Dequeue(struct RingQueue *rq, unsigned char *dst, int len) | |
145 | { | |
146 | int rql, toread; | |
147 | ||
148 | assert(rq != NULL); | |
149 | assert(dst != NULL); | |
150 | ||
151 | rql = RingQueue_GetLength(rq); | |
152 | if(!rql) | |
153 | return 0; | |
154 | ||
155 | /* Clip requested length to available data */ | |
156 | if(len > rql) | |
157 | len = rql; | |
158 | ||
159 | toread = len; | |
160 | if(rq->ri > rq->wi) { | |
161 | /* Read data from tail */ | |
162 | int read = (toread < (rq->length - rq->ri)) ? toread : rq->length - rq->ri; | |
163 | memcpy(dst, rq->queue + rq->ri, read); | |
164 | toread -= read; | |
165 | dst += read; | |
166 | rq->ri = (rq->ri + read) & (rq->length-1); | |
167 | } | |
168 | if(toread) { | |
169 | /* Read data from head */ | |
170 | memcpy(dst, rq->queue + rq->ri, toread); | |
171 | rq->ri = (rq->ri + toread) & (rq->length-1); | |
172 | } | |
173 | return len; | |
174 | } | |
175 | ||
176 | EXPORT_SYMBOL(RingQueue_Dequeue); | |
177 | ||
178 | int RingQueue_Enqueue(struct RingQueue *rq, const unsigned char *cdata, int n) | |
179 | { | |
180 | int enqueued = 0; | |
181 | ||
182 | assert(rq != NULL); | |
183 | assert(cdata != NULL); | |
184 | assert(rq->length > 0); | |
185 | while (n > 0) { | |
186 | int m, q_avail; | |
187 | ||
188 | /* Calculate the largest chunk that fits the tail of the ring */ | |
189 | q_avail = rq->length - rq->wi; | |
190 | if (q_avail <= 0) { | |
191 | rq->wi = 0; | |
192 | q_avail = rq->length; | |
193 | } | |
194 | m = n; | |
195 | assert(q_avail > 0); | |
196 | if (m > q_avail) | |
197 | m = q_avail; | |
198 | ||
199 | memcpy(rq->queue + rq->wi, cdata, m); | |
200 | RING_QUEUE_ADVANCE_INDEX(rq, wi, m); | |
201 | cdata += m; | |
202 | enqueued += m; | |
203 | n -= m; | |
204 | } | |
205 | return enqueued; | |
206 | } | |
207 | ||
208 | EXPORT_SYMBOL(RingQueue_Enqueue); | |
209 | ||
210 | static void RingQueue_InterruptibleSleepOn(struct RingQueue *rq) | |
211 | { | |
212 | assert(rq != NULL); | |
213 | interruptible_sleep_on(&rq->wqh); | |
214 | } | |
215 | ||
216 | void RingQueue_WakeUpInterruptible(struct RingQueue *rq) | |
217 | { | |
218 | assert(rq != NULL); | |
219 | if (waitqueue_active(&rq->wqh)) | |
220 | wake_up_interruptible(&rq->wqh); | |
221 | } | |
222 | ||
223 | EXPORT_SYMBOL(RingQueue_WakeUpInterruptible); | |
224 | ||
225 | void RingQueue_Flush(struct RingQueue *rq) | |
226 | { | |
227 | assert(rq != NULL); | |
228 | rq->ri = 0; | |
229 | rq->wi = 0; | |
230 | } | |
231 | ||
232 | EXPORT_SYMBOL(RingQueue_Flush); | |
233 | ||
234 | ||
235 | /* | |
236 | * usbvideo_VideosizeToString() | |
237 | * | |
238 | * This procedure converts given videosize value to readable string. | |
239 | * | |
240 | * History: | |
241 | * 07-Aug-2000 Created. | |
242 | * 19-Oct-2000 Reworked for usbvideo module. | |
243 | */ | |
244 | static void usbvideo_VideosizeToString(char *buf, int bufLen, videosize_t vs) | |
245 | { | |
246 | char tmp[40]; | |
247 | int n; | |
248 | ||
249 | n = 1 + sprintf(tmp, "%ldx%ld", VIDEOSIZE_X(vs), VIDEOSIZE_Y(vs)); | |
250 | assert(n < sizeof(tmp)); | |
251 | if ((buf == NULL) || (bufLen < n)) | |
252 | err("usbvideo_VideosizeToString: buffer is too small."); | |
253 | else | |
254 | memmove(buf, tmp, n); | |
255 | } | |
256 | ||
257 | /* | |
258 | * usbvideo_OverlayChar() | |
259 | * | |
260 | * History: | |
261 | * 01-Feb-2000 Created. | |
262 | */ | |
263 | static void usbvideo_OverlayChar(struct uvd *uvd, struct usbvideo_frame *frame, | |
264 | int x, int y, int ch) | |
265 | { | |
266 | static const unsigned short digits[16] = { | |
267 | 0xF6DE, /* 0 */ | |
268 | 0x2492, /* 1 */ | |
269 | 0xE7CE, /* 2 */ | |
270 | 0xE79E, /* 3 */ | |
271 | 0xB792, /* 4 */ | |
272 | 0xF39E, /* 5 */ | |
273 | 0xF3DE, /* 6 */ | |
274 | 0xF492, /* 7 */ | |
275 | 0xF7DE, /* 8 */ | |
276 | 0xF79E, /* 9 */ | |
277 | 0x77DA, /* a */ | |
278 | 0xD75C, /* b */ | |
279 | 0xF24E, /* c */ | |
280 | 0xD6DC, /* d */ | |
281 | 0xF34E, /* e */ | |
282 | 0xF348 /* f */ | |
283 | }; | |
284 | unsigned short digit; | |
285 | int ix, iy; | |
286 | ||
287 | if ((uvd == NULL) || (frame == NULL)) | |
288 | return; | |
289 | ||
290 | if (ch >= '0' && ch <= '9') | |
291 | ch -= '0'; | |
292 | else if (ch >= 'A' && ch <= 'F') | |
293 | ch = 10 + (ch - 'A'); | |
294 | else if (ch >= 'a' && ch <= 'f') | |
295 | ch = 10 + (ch - 'a'); | |
296 | else | |
297 | return; | |
298 | digit = digits[ch]; | |
299 | ||
300 | for (iy=0; iy < 5; iy++) { | |
301 | for (ix=0; ix < 3; ix++) { | |
302 | if (digit & 0x8000) { | |
303 | if (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24)) { | |
304 | /* TODO */ RGB24_PUTPIXEL(frame, x+ix, y+iy, 0xFF, 0xFF, 0xFF); | |
305 | } | |
306 | } | |
307 | digit = digit << 1; | |
308 | } | |
309 | } | |
310 | } | |
311 | ||
312 | /* | |
313 | * usbvideo_OverlayString() | |
314 | * | |
315 | * History: | |
316 | * 01-Feb-2000 Created. | |
317 | */ | |
318 | static void usbvideo_OverlayString(struct uvd *uvd, struct usbvideo_frame *frame, | |
319 | int x, int y, const char *str) | |
320 | { | |
321 | while (*str) { | |
322 | usbvideo_OverlayChar(uvd, frame, x, y, *str); | |
323 | str++; | |
324 | x += 4; /* 3 pixels character + 1 space */ | |
325 | } | |
326 | } | |
327 | ||
328 | /* | |
329 | * usbvideo_OverlayStats() | |
330 | * | |
331 | * Overlays important debugging information. | |
332 | * | |
333 | * History: | |
334 | * 01-Feb-2000 Created. | |
335 | */ | |
336 | static void usbvideo_OverlayStats(struct uvd *uvd, struct usbvideo_frame *frame) | |
337 | { | |
338 | const int y_diff = 8; | |
339 | char tmp[16]; | |
340 | int x = 10, y=10; | |
341 | long i, j, barLength; | |
342 | const int qi_x1 = 60, qi_y1 = 10; | |
343 | const int qi_x2 = VIDEOSIZE_X(frame->request) - 10, qi_h = 10; | |
344 | ||
345 | /* Call the user callback, see if we may proceed after that */ | |
346 | if (VALID_CALLBACK(uvd, overlayHook)) { | |
347 | if (GET_CALLBACK(uvd, overlayHook)(uvd, frame) < 0) | |
348 | return; | |
349 | } | |
350 | ||
351 | /* | |
352 | * We draw a (mostly) hollow rectangle with qi_xxx coordinates. | |
353 | * Left edge symbolizes the queue index 0; right edge symbolizes | |
354 | * the full capacity of the queue. | |
355 | */ | |
356 | barLength = qi_x2 - qi_x1 - 2; | |
357 | if ((barLength > 10) && (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24))) { | |
358 | /* TODO */ long u_lo, u_hi, q_used; | |
359 | long m_ri, m_wi, m_lo, m_hi; | |
360 | ||
361 | /* | |
362 | * Determine fill zones (used areas of the queue): | |
363 | * 0 xxxxxxx u_lo ...... uvd->dp.ri xxxxxxxx u_hi ..... uvd->dp.length | |
364 | * | |
365 | * if u_lo < 0 then there is no first filler. | |
366 | */ | |
367 | ||
368 | q_used = RingQueue_GetLength(&uvd->dp); | |
369 | if ((uvd->dp.ri + q_used) >= uvd->dp.length) { | |
370 | u_hi = uvd->dp.length; | |
371 | u_lo = (q_used + uvd->dp.ri) & (uvd->dp.length-1); | |
372 | } else { | |
373 | u_hi = (q_used + uvd->dp.ri); | |
374 | u_lo = -1; | |
375 | } | |
376 | ||
377 | /* Convert byte indices into screen units */ | |
378 | m_ri = qi_x1 + ((barLength * uvd->dp.ri) / uvd->dp.length); | |
379 | m_wi = qi_x1 + ((barLength * uvd->dp.wi) / uvd->dp.length); | |
380 | m_lo = (u_lo > 0) ? (qi_x1 + ((barLength * u_lo) / uvd->dp.length)) : -1; | |
381 | m_hi = qi_x1 + ((barLength * u_hi) / uvd->dp.length); | |
382 | ||
383 | for (j=qi_y1; j < (qi_y1 + qi_h); j++) { | |
384 | for (i=qi_x1; i < qi_x2; i++) { | |
385 | /* Draw border lines */ | |
386 | if ((j == qi_y1) || (j == (qi_y1 + qi_h - 1)) || | |
387 | (i == qi_x1) || (i == (qi_x2 - 1))) { | |
388 | RGB24_PUTPIXEL(frame, i, j, 0xFF, 0xFF, 0xFF); | |
389 | continue; | |
390 | } | |
391 | /* For all other points the Y coordinate does not matter */ | |
392 | if ((i >= m_ri) && (i <= (m_ri + 3))) { | |
393 | RGB24_PUTPIXEL(frame, i, j, 0x00, 0xFF, 0x00); | |
394 | } else if ((i >= m_wi) && (i <= (m_wi + 3))) { | |
395 | RGB24_PUTPIXEL(frame, i, j, 0xFF, 0x00, 0x00); | |
396 | } else if ((i < m_lo) || ((i > m_ri) && (i < m_hi))) | |
397 | RGB24_PUTPIXEL(frame, i, j, 0x00, 0x00, 0xFF); | |
398 | } | |
399 | } | |
400 | } | |
401 | ||
402 | sprintf(tmp, "%8lx", uvd->stats.frame_num); | |
403 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
404 | y += y_diff; | |
405 | ||
406 | sprintf(tmp, "%8lx", uvd->stats.urb_count); | |
407 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
408 | y += y_diff; | |
409 | ||
410 | sprintf(tmp, "%8lx", uvd->stats.urb_length); | |
411 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
412 | y += y_diff; | |
413 | ||
414 | sprintf(tmp, "%8lx", uvd->stats.data_count); | |
415 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
416 | y += y_diff; | |
417 | ||
418 | sprintf(tmp, "%8lx", uvd->stats.header_count); | |
419 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
420 | y += y_diff; | |
421 | ||
422 | sprintf(tmp, "%8lx", uvd->stats.iso_skip_count); | |
423 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
424 | y += y_diff; | |
425 | ||
426 | sprintf(tmp, "%8lx", uvd->stats.iso_err_count); | |
427 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
428 | y += y_diff; | |
429 | ||
430 | sprintf(tmp, "%8x", uvd->vpic.colour); | |
431 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
432 | y += y_diff; | |
433 | ||
434 | sprintf(tmp, "%8x", uvd->vpic.hue); | |
435 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
436 | y += y_diff; | |
437 | ||
438 | sprintf(tmp, "%8x", uvd->vpic.brightness >> 8); | |
439 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
440 | y += y_diff; | |
441 | ||
442 | sprintf(tmp, "%8x", uvd->vpic.contrast >> 12); | |
443 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
444 | y += y_diff; | |
445 | ||
446 | sprintf(tmp, "%8d", uvd->vpic.whiteness >> 8); | |
447 | usbvideo_OverlayString(uvd, frame, x, y, tmp); | |
448 | y += y_diff; | |
449 | } | |
450 | ||
451 | /* | |
452 | * usbvideo_ReportStatistics() | |
453 | * | |
454 | * This procedure prints packet and transfer statistics. | |
455 | * | |
456 | * History: | |
457 | * 14-Jan-2000 Corrected default multiplier. | |
458 | */ | |
459 | static void usbvideo_ReportStatistics(const struct uvd *uvd) | |
460 | { | |
461 | if ((uvd != NULL) && (uvd->stats.urb_count > 0)) { | |
462 | unsigned long allPackets, badPackets, goodPackets, percent; | |
463 | allPackets = uvd->stats.urb_count * CAMERA_URB_FRAMES; | |
464 | badPackets = uvd->stats.iso_skip_count + uvd->stats.iso_err_count; | |
465 | goodPackets = allPackets - badPackets; | |
466 | /* Calculate percentage wisely, remember integer limits */ | |
467 | assert(allPackets != 0); | |
468 | if (goodPackets < (((unsigned long)-1)/100)) | |
469 | percent = (100 * goodPackets) / allPackets; | |
470 | else | |
471 | percent = goodPackets / (allPackets / 100); | |
472 | info("Packet Statistics: Total=%lu. Empty=%lu. Usage=%lu%%", | |
473 | allPackets, badPackets, percent); | |
474 | if (uvd->iso_packet_len > 0) { | |
475 | unsigned long allBytes, xferBytes; | |
476 | char multiplier = ' '; | |
477 | allBytes = allPackets * uvd->iso_packet_len; | |
478 | xferBytes = uvd->stats.data_count; | |
479 | assert(allBytes != 0); | |
480 | if (xferBytes < (((unsigned long)-1)/100)) | |
481 | percent = (100 * xferBytes) / allBytes; | |
482 | else | |
483 | percent = xferBytes / (allBytes / 100); | |
484 | /* Scale xferBytes for easy reading */ | |
485 | if (xferBytes > 10*1024) { | |
486 | xferBytes /= 1024; | |
487 | multiplier = 'K'; | |
488 | if (xferBytes > 10*1024) { | |
489 | xferBytes /= 1024; | |
490 | multiplier = 'M'; | |
491 | if (xferBytes > 10*1024) { | |
492 | xferBytes /= 1024; | |
493 | multiplier = 'G'; | |
494 | if (xferBytes > 10*1024) { | |
495 | xferBytes /= 1024; | |
496 | multiplier = 'T'; | |
497 | } | |
498 | } | |
499 | } | |
500 | } | |
501 | info("Transfer Statistics: Transferred=%lu%cB Usage=%lu%%", | |
502 | xferBytes, multiplier, percent); | |
503 | } | |
504 | } | |
505 | } | |
506 | ||
507 | /* | |
508 | * usbvideo_TestPattern() | |
509 | * | |
510 | * Procedure forms a test pattern (yellow grid on blue background). | |
511 | * | |
512 | * Parameters: | |
513 | * fullframe: if TRUE then entire frame is filled, otherwise the procedure | |
514 | * continues from the current scanline. | |
515 | * pmode 0: fill the frame with solid blue color (like on VCR or TV) | |
516 | * 1: Draw a colored grid | |
517 | * | |
518 | * History: | |
519 | * 01-Feb-2000 Created. | |
520 | */ | |
521 | void usbvideo_TestPattern(struct uvd *uvd, int fullframe, int pmode) | |
522 | { | |
523 | struct usbvideo_frame *frame; | |
524 | int num_cell = 0; | |
525 | int scan_length = 0; | |
526 | static int num_pass = 0; | |
527 | ||
528 | if (uvd == NULL) { | |
529 | err("%s: uvd == NULL", __FUNCTION__); | |
530 | return; | |
531 | } | |
532 | if ((uvd->curframe < 0) || (uvd->curframe >= USBVIDEO_NUMFRAMES)) { | |
533 | err("%s: uvd->curframe=%d.", __FUNCTION__, uvd->curframe); | |
534 | return; | |
535 | } | |
536 | ||
537 | /* Grab the current frame */ | |
538 | frame = &uvd->frame[uvd->curframe]; | |
539 | ||
540 | /* Optionally start at the beginning */ | |
541 | if (fullframe) { | |
542 | frame->curline = 0; | |
543 | frame->seqRead_Length = 0; | |
544 | } | |
545 | #if 0 | |
546 | { /* For debugging purposes only */ | |
547 | char tmp[20]; | |
548 | usbvideo_VideosizeToString(tmp, sizeof(tmp), frame->request); | |
549 | info("testpattern: frame=%s", tmp); | |
550 | } | |
551 | #endif | |
552 | /* Form every scan line */ | |
553 | for (; frame->curline < VIDEOSIZE_Y(frame->request); frame->curline++) { | |
554 | int i; | |
555 | unsigned char *f = frame->data + | |
556 | (VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL * frame->curline); | |
557 | for (i=0; i < VIDEOSIZE_X(frame->request); i++) { | |
558 | unsigned char cb=0x80; | |
559 | unsigned char cg = 0; | |
560 | unsigned char cr = 0; | |
561 | ||
562 | if (pmode == 1) { | |
563 | if (frame->curline % 32 == 0) | |
564 | cb = 0, cg = cr = 0xFF; | |
565 | else if (i % 32 == 0) { | |
566 | if (frame->curline % 32 == 1) | |
567 | num_cell++; | |
568 | cb = 0, cg = cr = 0xFF; | |
569 | } else { | |
570 | cb = ((num_cell*7) + num_pass) & 0xFF; | |
571 | cg = ((num_cell*5) + num_pass*2) & 0xFF; | |
572 | cr = ((num_cell*3) + num_pass*3) & 0xFF; | |
573 | } | |
574 | } else { | |
575 | /* Just the blue screen */ | |
576 | } | |
577 | ||
578 | *f++ = cb; | |
579 | *f++ = cg; | |
580 | *f++ = cr; | |
581 | scan_length += 3; | |
582 | } | |
583 | } | |
584 | ||
585 | frame->frameState = FrameState_Done; | |
586 | frame->seqRead_Length += scan_length; | |
587 | ++num_pass; | |
588 | ||
589 | /* We do this unconditionally, regardless of FLAGS_OVERLAY_STATS */ | |
590 | usbvideo_OverlayStats(uvd, frame); | |
591 | } | |
592 | ||
593 | EXPORT_SYMBOL(usbvideo_TestPattern); | |
594 | ||
595 | ||
596 | #ifdef DEBUG | |
597 | /* | |
598 | * usbvideo_HexDump() | |
599 | * | |
600 | * A debugging tool. Prints hex dumps. | |
601 | * | |
602 | * History: | |
603 | * 29-Jul-2000 Added printing of offsets. | |
604 | */ | |
605 | void usbvideo_HexDump(const unsigned char *data, int len) | |
606 | { | |
607 | const int bytes_per_line = 32; | |
608 | char tmp[128]; /* 32*3 + 5 */ | |
609 | int i, k; | |
610 | ||
611 | for (i=k=0; len > 0; i++, len--) { | |
612 | if (i > 0 && ((i % bytes_per_line) == 0)) { | |
613 | printk("%s\n", tmp); | |
614 | k=0; | |
615 | } | |
616 | if ((i % bytes_per_line) == 0) | |
617 | k += sprintf(&tmp[k], "%04x: ", i); | |
618 | k += sprintf(&tmp[k], "%02x ", data[i]); | |
619 | } | |
620 | if (k > 0) | |
621 | printk("%s\n", tmp); | |
622 | } | |
623 | ||
624 | EXPORT_SYMBOL(usbvideo_HexDump); | |
625 | ||
626 | #endif | |
627 | ||
628 | /* ******************************************************************** */ | |
629 | ||
630 | /* XXX: this piece of crap really wants some error handling.. */ | |
631 | static void usbvideo_ClientIncModCount(struct uvd *uvd) | |
632 | { | |
633 | if (uvd == NULL) { | |
634 | err("%s: uvd == NULL", __FUNCTION__); | |
635 | return; | |
636 | } | |
637 | if (uvd->handle == NULL) { | |
638 | err("%s: uvd->handle == NULL", __FUNCTION__); | |
639 | return; | |
640 | } | |
641 | if (uvd->handle->md_module == NULL) { | |
642 | err("%s: uvd->handle->md_module == NULL", __FUNCTION__); | |
643 | return; | |
644 | } | |
645 | if (!try_module_get(uvd->handle->md_module)) { | |
646 | err("%s: try_module_get() == 0", __FUNCTION__); | |
647 | return; | |
648 | } | |
649 | } | |
650 | ||
651 | static void usbvideo_ClientDecModCount(struct uvd *uvd) | |
652 | { | |
653 | if (uvd == NULL) { | |
654 | err("%s: uvd == NULL", __FUNCTION__); | |
655 | return; | |
656 | } | |
657 | if (uvd->handle == NULL) { | |
658 | err("%s: uvd->handle == NULL", __FUNCTION__); | |
659 | return; | |
660 | } | |
661 | if (uvd->handle->md_module == NULL) { | |
662 | err("%s: uvd->handle->md_module == NULL", __FUNCTION__); | |
663 | return; | |
664 | } | |
665 | module_put(uvd->handle->md_module); | |
666 | } | |
667 | ||
668 | int usbvideo_register( | |
669 | struct usbvideo **pCams, | |
670 | const int num_cams, | |
671 | const int num_extra, | |
672 | const char *driverName, | |
673 | const struct usbvideo_cb *cbTbl, | |
674 | struct module *md, | |
675 | const struct usb_device_id *id_table) | |
676 | { | |
677 | struct usbvideo *cams; | |
678 | int i, base_size, result; | |
679 | ||
680 | /* Check parameters for sanity */ | |
681 | if ((num_cams <= 0) || (pCams == NULL) || (cbTbl == NULL)) { | |
682 | err("%s: Illegal call", __FUNCTION__); | |
683 | return -EINVAL; | |
684 | } | |
685 | ||
686 | /* Check registration callback - must be set! */ | |
687 | if (cbTbl->probe == NULL) { | |
688 | err("%s: probe() is required!", __FUNCTION__); | |
689 | return -EINVAL; | |
690 | } | |
691 | ||
692 | base_size = num_cams * sizeof(struct uvd) + sizeof(struct usbvideo); | |
693 | cams = (struct usbvideo *) kmalloc(base_size, GFP_KERNEL); | |
694 | if (cams == NULL) { | |
695 | err("Failed to allocate %d. bytes for usbvideo struct", base_size); | |
696 | return -ENOMEM; | |
697 | } | |
698 | dbg("%s: Allocated $%p (%d. bytes) for %d. cameras", | |
699 | __FUNCTION__, cams, base_size, num_cams); | |
700 | memset(cams, 0, base_size); | |
701 | ||
702 | /* Copy callbacks, apply defaults for those that are not set */ | |
703 | memmove(&cams->cb, cbTbl, sizeof(cams->cb)); | |
704 | if (cams->cb.getFrame == NULL) | |
705 | cams->cb.getFrame = usbvideo_GetFrame; | |
706 | if (cams->cb.disconnect == NULL) | |
707 | cams->cb.disconnect = usbvideo_Disconnect; | |
708 | if (cams->cb.startDataPump == NULL) | |
709 | cams->cb.startDataPump = usbvideo_StartDataPump; | |
710 | if (cams->cb.stopDataPump == NULL) | |
711 | cams->cb.stopDataPump = usbvideo_StopDataPump; | |
712 | ||
713 | cams->num_cameras = num_cams; | |
714 | cams->cam = (struct uvd *) &cams[1]; | |
715 | cams->md_module = md; | |
716 | if (cams->md_module == NULL) | |
717 | warn("%s: module == NULL!", __FUNCTION__); | |
718 | init_MUTEX(&cams->lock); /* to 1 == available */ | |
719 | ||
720 | for (i = 0; i < num_cams; i++) { | |
721 | struct uvd *up = &cams->cam[i]; | |
722 | ||
723 | up->handle = cams; | |
724 | ||
725 | /* Allocate user_data separately because of kmalloc's limits */ | |
726 | if (num_extra > 0) { | |
727 | up->user_size = num_cams * num_extra; | |
0e8eb0f0 | 728 | up->user_data = kmalloc(up->user_size, GFP_KERNEL); |
1da177e4 LT |
729 | if (up->user_data == NULL) { |
730 | err("%s: Failed to allocate user_data (%d. bytes)", | |
731 | __FUNCTION__, up->user_size); | |
732 | while (i) { | |
733 | up = &cams->cam[--i]; | |
734 | kfree(up->user_data); | |
735 | } | |
736 | kfree(cams); | |
737 | return -ENOMEM; | |
738 | } | |
739 | dbg("%s: Allocated cams[%d].user_data=$%p (%d. bytes)", | |
740 | __FUNCTION__, i, up->user_data, up->user_size); | |
741 | } | |
742 | } | |
743 | ||
744 | /* | |
745 | * Register ourselves with USB stack. | |
746 | */ | |
747 | strcpy(cams->drvName, (driverName != NULL) ? driverName : "Unknown"); | |
748 | cams->usbdrv.name = cams->drvName; | |
749 | cams->usbdrv.probe = cams->cb.probe; | |
750 | cams->usbdrv.disconnect = cams->cb.disconnect; | |
751 | cams->usbdrv.id_table = id_table; | |
752 | ||
753 | /* | |
754 | * Update global handle to usbvideo. This is very important | |
755 | * because probe() can be called before usb_register() returns. | |
756 | * If the handle is not yet updated then the probe() will fail. | |
757 | */ | |
758 | *pCams = cams; | |
759 | result = usb_register(&cams->usbdrv); | |
760 | if (result) { | |
761 | for (i = 0; i < num_cams; i++) { | |
762 | struct uvd *up = &cams->cam[i]; | |
763 | kfree(up->user_data); | |
764 | } | |
765 | kfree(cams); | |
766 | } | |
767 | ||
768 | return result; | |
769 | } | |
770 | ||
771 | EXPORT_SYMBOL(usbvideo_register); | |
772 | ||
773 | /* | |
774 | * usbvideo_Deregister() | |
775 | * | |
776 | * Procedure frees all usbvideo and user data structures. Be warned that | |
777 | * if you had some dynamically allocated components in ->user field then | |
778 | * you should free them before calling here. | |
779 | */ | |
780 | void usbvideo_Deregister(struct usbvideo **pCams) | |
781 | { | |
782 | struct usbvideo *cams; | |
783 | int i; | |
784 | ||
785 | if (pCams == NULL) { | |
786 | err("%s: pCams == NULL", __FUNCTION__); | |
787 | return; | |
788 | } | |
789 | cams = *pCams; | |
790 | if (cams == NULL) { | |
791 | err("%s: cams == NULL", __FUNCTION__); | |
792 | return; | |
793 | } | |
794 | ||
795 | dbg("%s: Deregistering %s driver.", __FUNCTION__, cams->drvName); | |
796 | usb_deregister(&cams->usbdrv); | |
797 | ||
798 | dbg("%s: Deallocating cams=$%p (%d. cameras)", __FUNCTION__, cams, cams->num_cameras); | |
799 | for (i=0; i < cams->num_cameras; i++) { | |
800 | struct uvd *up = &cams->cam[i]; | |
801 | int warning = 0; | |
802 | ||
803 | if (up->user_data != NULL) { | |
804 | if (up->user_size <= 0) | |
805 | ++warning; | |
806 | } else { | |
807 | if (up->user_size > 0) | |
808 | ++warning; | |
809 | } | |
810 | if (warning) { | |
811 | err("%s: Warning: user_data=$%p user_size=%d.", | |
812 | __FUNCTION__, up->user_data, up->user_size); | |
813 | } else { | |
814 | dbg("%s: Freeing %d. $%p->user_data=$%p", | |
815 | __FUNCTION__, i, up, up->user_data); | |
816 | kfree(up->user_data); | |
817 | } | |
818 | } | |
819 | /* Whole array was allocated in one chunk */ | |
820 | dbg("%s: Freed %d uvd structures", | |
821 | __FUNCTION__, cams->num_cameras); | |
822 | kfree(cams); | |
823 | *pCams = NULL; | |
824 | } | |
825 | ||
826 | EXPORT_SYMBOL(usbvideo_Deregister); | |
827 | ||
828 | /* | |
829 | * usbvideo_Disconnect() | |
830 | * | |
831 | * This procedure stops all driver activity. Deallocation of | |
832 | * the interface-private structure (pointed by 'ptr') is done now | |
833 | * (if we don't have any open files) or later, when those files | |
834 | * are closed. After that driver should be removable. | |
835 | * | |
836 | * This code handles surprise removal. The uvd->user is a counter which | |
837 | * increments on open() and decrements on close(). If we see here that | |
838 | * this counter is not 0 then we have a client who still has us opened. | |
839 | * We set uvd->remove_pending flag as early as possible, and after that | |
840 | * all access to the camera will gracefully fail. These failures should | |
841 | * prompt client to (eventually) close the video device, and then - in | |
842 | * usbvideo_v4l_close() - we decrement uvd->uvd_used and usage counter. | |
843 | * | |
844 | * History: | |
845 | * 22-Jan-2000 Added polling of MOD_IN_USE to delay removal until all users gone. | |
846 | * 27-Jan-2000 Reworked to allow pending disconnects; see xxx_close() | |
847 | * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT). | |
848 | * 19-Oct-2000 Moved to usbvideo module. | |
849 | */ | |
850 | static void usbvideo_Disconnect(struct usb_interface *intf) | |
851 | { | |
852 | struct uvd *uvd = usb_get_intfdata (intf); | |
853 | int i; | |
854 | ||
855 | if (uvd == NULL) { | |
856 | err("%s($%p): Illegal call.", __FUNCTION__, intf); | |
857 | return; | |
858 | } | |
859 | ||
860 | usb_set_intfdata (intf, NULL); | |
861 | ||
862 | usbvideo_ClientIncModCount(uvd); | |
863 | if (uvd->debug > 0) | |
864 | info("%s(%p.)", __FUNCTION__, intf); | |
865 | ||
866 | down(&uvd->lock); | |
867 | uvd->remove_pending = 1; /* Now all ISO data will be ignored */ | |
868 | ||
869 | /* At this time we ask to cancel outstanding URBs */ | |
870 | GET_CALLBACK(uvd, stopDataPump)(uvd); | |
871 | ||
872 | for (i=0; i < USBVIDEO_NUMSBUF; i++) | |
873 | usb_free_urb(uvd->sbuf[i].urb); | |
874 | ||
875 | usb_put_dev(uvd->dev); | |
876 | uvd->dev = NULL; /* USB device is no more */ | |
877 | ||
878 | video_unregister_device(&uvd->vdev); | |
879 | if (uvd->debug > 0) | |
880 | info("%s: Video unregistered.", __FUNCTION__); | |
881 | ||
882 | if (uvd->user) | |
883 | info("%s: In use, disconnect pending.", __FUNCTION__); | |
884 | else | |
885 | usbvideo_CameraRelease(uvd); | |
886 | up(&uvd->lock); | |
887 | info("USB camera disconnected."); | |
888 | ||
889 | usbvideo_ClientDecModCount(uvd); | |
890 | } | |
891 | ||
892 | /* | |
893 | * usbvideo_CameraRelease() | |
894 | * | |
895 | * This code does final release of uvd. This happens | |
896 | * after the device is disconnected -and- all clients | |
897 | * closed their files. | |
898 | * | |
899 | * History: | |
900 | * 27-Jan-2000 Created. | |
901 | */ | |
902 | static void usbvideo_CameraRelease(struct uvd *uvd) | |
903 | { | |
904 | if (uvd == NULL) { | |
905 | err("%s: Illegal call", __FUNCTION__); | |
906 | return; | |
907 | } | |
908 | ||
909 | RingQueue_Free(&uvd->dp); | |
910 | if (VALID_CALLBACK(uvd, userFree)) | |
911 | GET_CALLBACK(uvd, userFree)(uvd); | |
912 | uvd->uvd_used = 0; /* This is atomic, no need to take mutex */ | |
913 | } | |
914 | ||
915 | /* | |
916 | * usbvideo_find_struct() | |
917 | * | |
918 | * This code searches the array of preallocated (static) structures | |
919 | * and returns index of the first one that isn't in use. Returns -1 | |
920 | * if there are no free structures. | |
921 | * | |
922 | * History: | |
923 | * 27-Jan-2000 Created. | |
924 | */ | |
925 | static int usbvideo_find_struct(struct usbvideo *cams) | |
926 | { | |
927 | int u, rv = -1; | |
928 | ||
929 | if (cams == NULL) { | |
930 | err("No usbvideo handle?"); | |
931 | return -1; | |
932 | } | |
933 | down(&cams->lock); | |
934 | for (u = 0; u < cams->num_cameras; u++) { | |
935 | struct uvd *uvd = &cams->cam[u]; | |
936 | if (!uvd->uvd_used) /* This one is free */ | |
937 | { | |
938 | uvd->uvd_used = 1; /* In use now */ | |
939 | init_MUTEX(&uvd->lock); /* to 1 == available */ | |
940 | uvd->dev = NULL; | |
941 | rv = u; | |
942 | break; | |
943 | } | |
944 | } | |
945 | up(&cams->lock); | |
946 | return rv; | |
947 | } | |
948 | ||
949 | static struct file_operations usbvideo_fops = { | |
950 | .owner = THIS_MODULE, | |
951 | .open = usbvideo_v4l_open, | |
952 | .release =usbvideo_v4l_close, | |
953 | .read = usbvideo_v4l_read, | |
954 | .mmap = usbvideo_v4l_mmap, | |
955 | .ioctl = usbvideo_v4l_ioctl, | |
0d0fbf81 | 956 | .compat_ioctl = v4l_compat_ioctl32, |
1da177e4 LT |
957 | .llseek = no_llseek, |
958 | }; | |
4c4c9432 | 959 | static const struct video_device usbvideo_template = { |
1da177e4 LT |
960 | .owner = THIS_MODULE, |
961 | .type = VID_TYPE_CAPTURE, | |
962 | .hardware = VID_HARDWARE_CPIA, | |
963 | .fops = &usbvideo_fops, | |
964 | }; | |
965 | ||
966 | struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams) | |
967 | { | |
968 | int i, devnum; | |
969 | struct uvd *uvd = NULL; | |
970 | ||
971 | if (cams == NULL) { | |
972 | err("No usbvideo handle?"); | |
973 | return NULL; | |
974 | } | |
975 | ||
976 | devnum = usbvideo_find_struct(cams); | |
977 | if (devnum == -1) { | |
978 | err("IBM USB camera driver: Too many devices!"); | |
979 | return NULL; | |
980 | } | |
981 | uvd = &cams->cam[devnum]; | |
982 | dbg("Device entry #%d. at $%p", devnum, uvd); | |
983 | ||
984 | /* Not relying upon caller we increase module counter ourselves */ | |
985 | usbvideo_ClientIncModCount(uvd); | |
986 | ||
987 | down(&uvd->lock); | |
988 | for (i=0; i < USBVIDEO_NUMSBUF; i++) { | |
989 | uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL); | |
990 | if (uvd->sbuf[i].urb == NULL) { | |
991 | err("usb_alloc_urb(%d.) failed.", FRAMES_PER_DESC); | |
992 | uvd->uvd_used = 0; | |
993 | uvd = NULL; | |
994 | goto allocate_done; | |
995 | } | |
996 | } | |
997 | uvd->user=0; | |
998 | uvd->remove_pending = 0; | |
999 | uvd->last_error = 0; | |
1000 | RingQueue_Initialize(&uvd->dp); | |
1001 | ||
1002 | /* Initialize video device structure */ | |
1003 | uvd->vdev = usbvideo_template; | |
1004 | sprintf(uvd->vdev.name, "%.20s USB Camera", cams->drvName); | |
1005 | /* | |
1006 | * The client is free to overwrite those because we | |
1007 | * return control to the client's probe function right now. | |
1008 | */ | |
1009 | allocate_done: | |
1010 | up (&uvd->lock); | |
1011 | usbvideo_ClientDecModCount(uvd); | |
1012 | return uvd; | |
1013 | } | |
1014 | ||
1015 | EXPORT_SYMBOL(usbvideo_AllocateDevice); | |
1016 | ||
1017 | int usbvideo_RegisterVideoDevice(struct uvd *uvd) | |
1018 | { | |
1019 | char tmp1[20], tmp2[20]; /* Buffers for printing */ | |
1020 | ||
1021 | if (uvd == NULL) { | |
1022 | err("%s: Illegal call.", __FUNCTION__); | |
1023 | return -EINVAL; | |
1024 | } | |
1025 | if (uvd->video_endp == 0) { | |
1026 | info("%s: No video endpoint specified; data pump disabled.", __FUNCTION__); | |
1027 | } | |
1028 | if (uvd->paletteBits == 0) { | |
1029 | err("%s: No palettes specified!", __FUNCTION__); | |
1030 | return -EINVAL; | |
1031 | } | |
1032 | if (uvd->defaultPalette == 0) { | |
1033 | info("%s: No default palette!", __FUNCTION__); | |
1034 | } | |
1035 | ||
1036 | uvd->max_frame_size = VIDEOSIZE_X(uvd->canvas) * | |
1037 | VIDEOSIZE_Y(uvd->canvas) * V4L_BYTES_PER_PIXEL; | |
1038 | usbvideo_VideosizeToString(tmp1, sizeof(tmp1), uvd->videosize); | |
1039 | usbvideo_VideosizeToString(tmp2, sizeof(tmp2), uvd->canvas); | |
1040 | ||
1041 | if (uvd->debug > 0) { | |
1042 | info("%s: iface=%d. endpoint=$%02x paletteBits=$%08lx", | |
1043 | __FUNCTION__, uvd->iface, uvd->video_endp, uvd->paletteBits); | |
1044 | } | |
1045 | if (video_register_device(&uvd->vdev, VFL_TYPE_GRABBER, video_nr) == -1) { | |
1046 | err("%s: video_register_device failed", __FUNCTION__); | |
1047 | return -EPIPE; | |
1048 | } | |
1049 | if (uvd->debug > 1) { | |
1050 | info("%s: video_register_device() successful", __FUNCTION__); | |
1051 | } | |
1052 | if (uvd->dev == NULL) { | |
1053 | err("%s: uvd->dev == NULL", __FUNCTION__); | |
1054 | return -EINVAL; | |
1055 | } | |
1056 | ||
1057 | info("%s on /dev/video%d: canvas=%s videosize=%s", | |
1058 | (uvd->handle != NULL) ? uvd->handle->drvName : "???", | |
1059 | uvd->vdev.minor, tmp2, tmp1); | |
1060 | ||
1061 | usb_get_dev(uvd->dev); | |
1062 | return 0; | |
1063 | } | |
1064 | ||
1065 | EXPORT_SYMBOL(usbvideo_RegisterVideoDevice); | |
1066 | ||
1067 | /* ******************************************************************** */ | |
1068 | ||
1069 | static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma) | |
1070 | { | |
1071 | struct uvd *uvd = file->private_data; | |
1072 | unsigned long start = vma->vm_start; | |
1073 | unsigned long size = vma->vm_end-vma->vm_start; | |
1074 | unsigned long page, pos; | |
1075 | ||
1076 | if (!CAMERA_IS_OPERATIONAL(uvd)) | |
1077 | return -EFAULT; | |
1078 | ||
1079 | if (size > (((USBVIDEO_NUMFRAMES * uvd->max_frame_size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))) | |
1080 | return -EINVAL; | |
1081 | ||
1082 | pos = (unsigned long) uvd->fbuf; | |
1083 | while (size > 0) { | |
1084 | page = vmalloc_to_pfn((void *)pos); | |
1085 | if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) | |
1086 | return -EAGAIN; | |
1087 | ||
1088 | start += PAGE_SIZE; | |
1089 | pos += PAGE_SIZE; | |
1090 | if (size > PAGE_SIZE) | |
1091 | size -= PAGE_SIZE; | |
1092 | else | |
1093 | size = 0; | |
1094 | } | |
1095 | ||
1096 | return 0; | |
1097 | } | |
1098 | ||
1099 | /* | |
1100 | * usbvideo_v4l_open() | |
1101 | * | |
1102 | * This is part of Video 4 Linux API. The driver can be opened by one | |
1103 | * client only (checks internal counter 'uvdser'). The procedure | |
1104 | * then allocates buffers needed for video processing. | |
1105 | * | |
1106 | * History: | |
1107 | * 22-Jan-2000 Rewrote, moved scratch buffer allocation here. Now the | |
1108 | * camera is also initialized here (once per connect), at | |
1109 | * expense of V4L client (it waits on open() call). | |
1110 | * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers. | |
1111 | * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT). | |
1112 | */ | |
1113 | static int usbvideo_v4l_open(struct inode *inode, struct file *file) | |
1114 | { | |
1115 | struct video_device *dev = video_devdata(file); | |
1116 | struct uvd *uvd = (struct uvd *) dev; | |
1117 | const int sb_size = FRAMES_PER_DESC * uvd->iso_packet_len; | |
1118 | int i, errCode = 0; | |
1119 | ||
1120 | if (uvd->debug > 1) | |
1121 | info("%s($%p)", __FUNCTION__, dev); | |
1122 | ||
1123 | usbvideo_ClientIncModCount(uvd); | |
1124 | down(&uvd->lock); | |
1125 | ||
1126 | if (uvd->user) { | |
1127 | err("%s: Someone tried to open an already opened device!", __FUNCTION__); | |
1128 | errCode = -EBUSY; | |
1129 | } else { | |
1130 | /* Clear statistics */ | |
1131 | memset(&uvd->stats, 0, sizeof(uvd->stats)); | |
1132 | ||
1133 | /* Clean pointers so we know if we allocated something */ | |
1134 | for (i=0; i < USBVIDEO_NUMSBUF; i++) | |
1135 | uvd->sbuf[i].data = NULL; | |
1136 | ||
1137 | /* Allocate memory for the frame buffers */ | |
1138 | uvd->fbuf_size = USBVIDEO_NUMFRAMES * uvd->max_frame_size; | |
1139 | uvd->fbuf = usbvideo_rvmalloc(uvd->fbuf_size); | |
1140 | RingQueue_Allocate(&uvd->dp, RING_QUEUE_SIZE); | |
1141 | if ((uvd->fbuf == NULL) || | |
1142 | (!RingQueue_IsAllocated(&uvd->dp))) { | |
1143 | err("%s: Failed to allocate fbuf or dp", __FUNCTION__); | |
1144 | errCode = -ENOMEM; | |
1145 | } else { | |
1146 | /* Allocate all buffers */ | |
1147 | for (i=0; i < USBVIDEO_NUMFRAMES; i++) { | |
1148 | uvd->frame[i].frameState = FrameState_Unused; | |
1149 | uvd->frame[i].data = uvd->fbuf + i*(uvd->max_frame_size); | |
1150 | /* | |
1151 | * Set default sizes in case IOCTL (VIDIOCMCAPTURE) | |
1152 | * is not used (using read() instead). | |
1153 | */ | |
1154 | uvd->frame[i].canvas = uvd->canvas; | |
1155 | uvd->frame[i].seqRead_Index = 0; | |
1156 | } | |
1157 | for (i=0; i < USBVIDEO_NUMSBUF; i++) { | |
1158 | uvd->sbuf[i].data = kmalloc(sb_size, GFP_KERNEL); | |
1159 | if (uvd->sbuf[i].data == NULL) { | |
1160 | errCode = -ENOMEM; | |
1161 | break; | |
1162 | } | |
1163 | } | |
1164 | } | |
1165 | if (errCode != 0) { | |
1166 | /* Have to free all that memory */ | |
1167 | if (uvd->fbuf != NULL) { | |
1168 | usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size); | |
1169 | uvd->fbuf = NULL; | |
1170 | } | |
1171 | RingQueue_Free(&uvd->dp); | |
1172 | for (i=0; i < USBVIDEO_NUMSBUF; i++) { | |
1bc3c9e1 JJ |
1173 | kfree(uvd->sbuf[i].data); |
1174 | uvd->sbuf[i].data = NULL; | |
1da177e4 LT |
1175 | } |
1176 | } | |
1177 | } | |
1178 | ||
1179 | /* If so far no errors then we shall start the camera */ | |
1180 | if (errCode == 0) { | |
1181 | /* Start data pump if we have valid endpoint */ | |
1182 | if (uvd->video_endp != 0) | |
1183 | errCode = GET_CALLBACK(uvd, startDataPump)(uvd); | |
1184 | if (errCode == 0) { | |
1185 | if (VALID_CALLBACK(uvd, setupOnOpen)) { | |
1186 | if (uvd->debug > 1) | |
1187 | info("%s: setupOnOpen callback", __FUNCTION__); | |
1188 | errCode = GET_CALLBACK(uvd, setupOnOpen)(uvd); | |
1189 | if (errCode < 0) { | |
1190 | err("%s: setupOnOpen callback failed (%d.).", | |
1191 | __FUNCTION__, errCode); | |
1192 | } else if (uvd->debug > 1) { | |
1193 | info("%s: setupOnOpen callback successful", __FUNCTION__); | |
1194 | } | |
1195 | } | |
1196 | if (errCode == 0) { | |
1197 | uvd->settingsAdjusted = 0; | |
1198 | if (uvd->debug > 1) | |
1199 | info("%s: Open succeeded.", __FUNCTION__); | |
1200 | uvd->user++; | |
1201 | file->private_data = uvd; | |
1202 | } | |
1203 | } | |
1204 | } | |
1205 | up(&uvd->lock); | |
1206 | if (errCode != 0) | |
1207 | usbvideo_ClientDecModCount(uvd); | |
1208 | if (uvd->debug > 0) | |
1209 | info("%s: Returning %d.", __FUNCTION__, errCode); | |
1210 | return errCode; | |
1211 | } | |
1212 | ||
1213 | /* | |
1214 | * usbvideo_v4l_close() | |
1215 | * | |
1216 | * This is part of Video 4 Linux API. The procedure | |
1217 | * stops streaming and deallocates all buffers that were earlier | |
1218 | * allocated in usbvideo_v4l_open(). | |
1219 | * | |
1220 | * History: | |
1221 | * 22-Jan-2000 Moved scratch buffer deallocation here. | |
1222 | * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers. | |
1223 | * 24-May-2000 Moved MOD_DEC_USE_COUNT outside of code that can sleep. | |
1224 | */ | |
1225 | static int usbvideo_v4l_close(struct inode *inode, struct file *file) | |
1226 | { | |
1227 | struct video_device *dev = file->private_data; | |
1228 | struct uvd *uvd = (struct uvd *) dev; | |
1229 | int i; | |
1230 | ||
1231 | if (uvd->debug > 1) | |
1232 | info("%s($%p)", __FUNCTION__, dev); | |
1233 | ||
1234 | down(&uvd->lock); | |
1235 | GET_CALLBACK(uvd, stopDataPump)(uvd); | |
1236 | usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size); | |
1237 | uvd->fbuf = NULL; | |
1238 | RingQueue_Free(&uvd->dp); | |
1239 | ||
1240 | for (i=0; i < USBVIDEO_NUMSBUF; i++) { | |
1241 | kfree(uvd->sbuf[i].data); | |
1242 | uvd->sbuf[i].data = NULL; | |
1243 | } | |
1244 | ||
1245 | #if USBVIDEO_REPORT_STATS | |
1246 | usbvideo_ReportStatistics(uvd); | |
1247 | #endif | |
1248 | ||
1249 | uvd->user--; | |
1250 | if (uvd->remove_pending) { | |
1251 | if (uvd->debug > 0) | |
1252 | info("usbvideo_v4l_close: Final disconnect."); | |
1253 | usbvideo_CameraRelease(uvd); | |
1254 | } | |
1255 | up(&uvd->lock); | |
1256 | usbvideo_ClientDecModCount(uvd); | |
1257 | ||
1258 | if (uvd->debug > 1) | |
1259 | info("%s: Completed.", __FUNCTION__); | |
1260 | file->private_data = NULL; | |
1261 | return 0; | |
1262 | } | |
1263 | ||
1264 | /* | |
1265 | * usbvideo_v4l_ioctl() | |
1266 | * | |
1267 | * This is part of Video 4 Linux API. The procedure handles ioctl() calls. | |
1268 | * | |
1269 | * History: | |
1270 | * 22-Jan-2000 Corrected VIDIOCSPICT to reject unsupported settings. | |
1271 | */ | |
1272 | static int usbvideo_v4l_do_ioctl(struct inode *inode, struct file *file, | |
1273 | unsigned int cmd, void *arg) | |
1274 | { | |
1275 | struct uvd *uvd = file->private_data; | |
1276 | ||
1277 | if (!CAMERA_IS_OPERATIONAL(uvd)) | |
1278 | return -EIO; | |
1279 | ||
1280 | switch (cmd) { | |
1281 | case VIDIOCGCAP: | |
1282 | { | |
1283 | struct video_capability *b = arg; | |
1284 | *b = uvd->vcap; | |
1285 | return 0; | |
1286 | } | |
1287 | case VIDIOCGCHAN: | |
1288 | { | |
1289 | struct video_channel *v = arg; | |
1290 | *v = uvd->vchan; | |
1291 | return 0; | |
1292 | } | |
1293 | case VIDIOCSCHAN: | |
1294 | { | |
1295 | struct video_channel *v = arg; | |
1296 | if (v->channel != 0) | |
1297 | return -EINVAL; | |
1298 | return 0; | |
1299 | } | |
1300 | case VIDIOCGPICT: | |
1301 | { | |
1302 | struct video_picture *pic = arg; | |
1303 | *pic = uvd->vpic; | |
1304 | return 0; | |
1305 | } | |
1306 | case VIDIOCSPICT: | |
1307 | { | |
1308 | struct video_picture *pic = arg; | |
1309 | /* | |
1310 | * Use temporary 'video_picture' structure to preserve our | |
1311 | * own settings (such as color depth, palette) that we | |
1312 | * aren't allowing everyone (V4L client) to change. | |
1313 | */ | |
1314 | uvd->vpic.brightness = pic->brightness; | |
1315 | uvd->vpic.hue = pic->hue; | |
1316 | uvd->vpic.colour = pic->colour; | |
1317 | uvd->vpic.contrast = pic->contrast; | |
1318 | uvd->settingsAdjusted = 0; /* Will force new settings */ | |
1319 | return 0; | |
1320 | } | |
1321 | case VIDIOCSWIN: | |
1322 | { | |
1323 | struct video_window *vw = arg; | |
1324 | ||
1325 | if(VALID_CALLBACK(uvd, setVideoMode)) { | |
1326 | return GET_CALLBACK(uvd, setVideoMode)(uvd, vw); | |
1327 | } | |
1328 | ||
1329 | if (vw->flags) | |
1330 | return -EINVAL; | |
1331 | if (vw->clipcount) | |
1332 | return -EINVAL; | |
1333 | if (vw->width != VIDEOSIZE_X(uvd->canvas)) | |
1334 | return -EINVAL; | |
1335 | if (vw->height != VIDEOSIZE_Y(uvd->canvas)) | |
1336 | return -EINVAL; | |
1337 | ||
1338 | return 0; | |
1339 | } | |
1340 | case VIDIOCGWIN: | |
1341 | { | |
1342 | struct video_window *vw = arg; | |
1343 | ||
1344 | vw->x = 0; | |
1345 | vw->y = 0; | |
1346 | vw->width = VIDEOSIZE_X(uvd->videosize); | |
1347 | vw->height = VIDEOSIZE_Y(uvd->videosize); | |
1348 | vw->chromakey = 0; | |
1349 | if (VALID_CALLBACK(uvd, getFPS)) | |
1350 | vw->flags = GET_CALLBACK(uvd, getFPS)(uvd); | |
1351 | else | |
1352 | vw->flags = 10; /* FIXME: do better! */ | |
1353 | return 0; | |
1354 | } | |
1355 | case VIDIOCGMBUF: | |
1356 | { | |
1357 | struct video_mbuf *vm = arg; | |
1358 | int i; | |
1359 | ||
1360 | memset(vm, 0, sizeof(*vm)); | |
1361 | vm->size = uvd->max_frame_size * USBVIDEO_NUMFRAMES; | |
1362 | vm->frames = USBVIDEO_NUMFRAMES; | |
1363 | for(i = 0; i < USBVIDEO_NUMFRAMES; i++) | |
1364 | vm->offsets[i] = i * uvd->max_frame_size; | |
1365 | ||
1366 | return 0; | |
1367 | } | |
1368 | case VIDIOCMCAPTURE: | |
1369 | { | |
1370 | struct video_mmap *vm = arg; | |
1371 | ||
1372 | if (uvd->debug >= 1) { | |
1373 | info("VIDIOCMCAPTURE: frame=%d. size=%dx%d, format=%d.", | |
1374 | vm->frame, vm->width, vm->height, vm->format); | |
1375 | } | |
1376 | /* | |
1377 | * Check if the requested size is supported. If the requestor | |
1378 | * requests too big a frame then we may be tricked into accessing | |
1379 | * outside of own preallocated frame buffer (in uvd->frame). | |
1380 | * This will cause oops or a security hole. Theoretically, we | |
1381 | * could only clamp the size down to acceptable bounds, but then | |
1382 | * we'd need to figure out how to insert our smaller buffer into | |
1383 | * larger caller's buffer... this is not an easy question. So we | |
1384 | * here just flatly reject too large requests, assuming that the | |
1385 | * caller will resubmit with smaller size. Callers should know | |
1386 | * what size we support (returned by VIDIOCGCAP). However vidcat, | |
1387 | * for one, does not care and allows to ask for any size. | |
1388 | */ | |
1389 | if ((vm->width > VIDEOSIZE_X(uvd->canvas)) || | |
1390 | (vm->height > VIDEOSIZE_Y(uvd->canvas))) { | |
1391 | if (uvd->debug > 0) { | |
1392 | info("VIDIOCMCAPTURE: Size=%dx%d too large; " | |
1393 | "allowed only up to %ldx%ld", vm->width, vm->height, | |
1394 | VIDEOSIZE_X(uvd->canvas), VIDEOSIZE_Y(uvd->canvas)); | |
1395 | } | |
1396 | return -EINVAL; | |
1397 | } | |
1398 | /* Check if the palette is supported */ | |
1399 | if (((1L << vm->format) & uvd->paletteBits) == 0) { | |
1400 | if (uvd->debug > 0) { | |
1401 | info("VIDIOCMCAPTURE: format=%d. not supported" | |
1402 | " (paletteBits=$%08lx)", | |
1403 | vm->format, uvd->paletteBits); | |
1404 | } | |
1405 | return -EINVAL; | |
1406 | } | |
1407 | if ((vm->frame < 0) || (vm->frame >= USBVIDEO_NUMFRAMES)) { | |
1408 | err("VIDIOCMCAPTURE: vm.frame=%d. !E [0-%d]", vm->frame, USBVIDEO_NUMFRAMES-1); | |
1409 | return -EINVAL; | |
1410 | } | |
1411 | if (uvd->frame[vm->frame].frameState == FrameState_Grabbing) { | |
1412 | /* Not an error - can happen */ | |
1413 | } | |
1414 | uvd->frame[vm->frame].request = VIDEOSIZE(vm->width, vm->height); | |
1415 | uvd->frame[vm->frame].palette = vm->format; | |
1416 | ||
1417 | /* Mark it as ready */ | |
1418 | uvd->frame[vm->frame].frameState = FrameState_Ready; | |
1419 | ||
1420 | return usbvideo_NewFrame(uvd, vm->frame); | |
1421 | } | |
1422 | case VIDIOCSYNC: | |
1423 | { | |
1424 | int *frameNum = arg; | |
1425 | int ret; | |
1426 | ||
1427 | if (*frameNum < 0 || *frameNum >= USBVIDEO_NUMFRAMES) | |
1428 | return -EINVAL; | |
1429 | ||
1430 | if (uvd->debug >= 1) | |
1431 | info("VIDIOCSYNC: syncing to frame %d.", *frameNum); | |
1432 | if (uvd->flags & FLAGS_NO_DECODING) | |
1433 | ret = usbvideo_GetFrame(uvd, *frameNum); | |
1434 | else if (VALID_CALLBACK(uvd, getFrame)) { | |
1435 | ret = GET_CALLBACK(uvd, getFrame)(uvd, *frameNum); | |
1436 | if ((ret < 0) && (uvd->debug >= 1)) { | |
1437 | err("VIDIOCSYNC: getFrame() returned %d.", ret); | |
1438 | } | |
1439 | } else { | |
1440 | err("VIDIOCSYNC: getFrame is not set"); | |
1441 | ret = -EFAULT; | |
1442 | } | |
1443 | ||
1444 | /* | |
1445 | * The frame is in FrameState_Done_Hold state. Release it | |
1446 | * right now because its data is already mapped into | |
1447 | * the user space and it's up to the application to | |
1448 | * make use of it until it asks for another frame. | |
1449 | */ | |
1450 | uvd->frame[*frameNum].frameState = FrameState_Unused; | |
1451 | return ret; | |
1452 | } | |
1453 | case VIDIOCGFBUF: | |
1454 | { | |
1455 | struct video_buffer *vb = arg; | |
1456 | ||
1457 | memset(vb, 0, sizeof(*vb)); | |
1458 | return 0; | |
1459 | } | |
1460 | case VIDIOCKEY: | |
1461 | return 0; | |
1462 | ||
1463 | case VIDIOCCAPTURE: | |
1464 | return -EINVAL; | |
1465 | ||
1466 | case VIDIOCSFBUF: | |
1467 | ||
1468 | case VIDIOCGTUNER: | |
1469 | case VIDIOCSTUNER: | |
1470 | ||
1471 | case VIDIOCGFREQ: | |
1472 | case VIDIOCSFREQ: | |
1473 | ||
1474 | case VIDIOCGAUDIO: | |
1475 | case VIDIOCSAUDIO: | |
1476 | return -EINVAL; | |
1477 | ||
1478 | default: | |
1479 | return -ENOIOCTLCMD; | |
1480 | } | |
1481 | return 0; | |
1482 | } | |
1483 | ||
1484 | static int usbvideo_v4l_ioctl(struct inode *inode, struct file *file, | |
1485 | unsigned int cmd, unsigned long arg) | |
1486 | { | |
1487 | return video_usercopy(inode, file, cmd, arg, usbvideo_v4l_do_ioctl); | |
1488 | } | |
1489 | ||
1490 | /* | |
1491 | * usbvideo_v4l_read() | |
1492 | * | |
1493 | * This is mostly boring stuff. We simply ask for a frame and when it | |
1494 | * arrives copy all the video data from it into user space. There is | |
1495 | * no obvious need to override this method. | |
1496 | * | |
1497 | * History: | |
1498 | * 20-Oct-2000 Created. | |
1499 | * 01-Nov-2000 Added mutex (uvd->lock). | |
1500 | */ | |
1501 | static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf, | |
1502 | size_t count, loff_t *ppos) | |
1503 | { | |
1504 | struct uvd *uvd = file->private_data; | |
1505 | int noblock = file->f_flags & O_NONBLOCK; | |
1506 | int frmx = -1, i; | |
1507 | struct usbvideo_frame *frame; | |
1508 | ||
1509 | if (!CAMERA_IS_OPERATIONAL(uvd) || (buf == NULL)) | |
1510 | return -EFAULT; | |
1511 | ||
1512 | if (uvd->debug >= 1) | |
1513 | info("%s: %Zd. bytes, noblock=%d.", __FUNCTION__, count, noblock); | |
1514 | ||
1515 | down(&uvd->lock); | |
1516 | ||
1517 | /* See if a frame is completed, then use it. */ | |
1518 | for(i = 0; i < USBVIDEO_NUMFRAMES; i++) { | |
1519 | if ((uvd->frame[i].frameState == FrameState_Done) || | |
1520 | (uvd->frame[i].frameState == FrameState_Done_Hold) || | |
1521 | (uvd->frame[i].frameState == FrameState_Error)) { | |
1522 | frmx = i; | |
1523 | break; | |
1524 | } | |
1525 | } | |
1526 | ||
1527 | /* FIXME: If we don't start a frame here then who ever does? */ | |
1528 | if (noblock && (frmx == -1)) { | |
1529 | count = -EAGAIN; | |
1530 | goto read_done; | |
1531 | } | |
1532 | ||
1533 | /* | |
1534 | * If no FrameState_Done, look for a FrameState_Grabbing state. | |
1535 | * See if a frame is in process (grabbing), then use it. | |
1536 | * We will need to wait until it becomes cooked, of course. | |
1537 | */ | |
1538 | if (frmx == -1) { | |
1539 | for(i = 0; i < USBVIDEO_NUMFRAMES; i++) { | |
1540 | if (uvd->frame[i].frameState == FrameState_Grabbing) { | |
1541 | frmx = i; | |
1542 | break; | |
1543 | } | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | /* | |
1548 | * If no frame is active, start one. We don't care which one | |
1549 | * it will be, so #0 is as good as any. | |
1550 | * In read access mode we don't have convenience of VIDIOCMCAPTURE | |
1551 | * to specify the requested palette (video format) on per-frame | |
1552 | * basis. This means that we have to return data in -some- format | |
1553 | * and just hope that the client knows what to do with it. | |
1554 | * The default format is configured in uvd->defaultPalette field | |
1555 | * as one of VIDEO_PALETTE_xxx values. We stuff it into the new | |
1556 | * frame and initiate the frame filling process. | |
1557 | */ | |
1558 | if (frmx == -1) { | |
1559 | if (uvd->defaultPalette == 0) { | |
1560 | err("%s: No default palette; don't know what to do!", __FUNCTION__); | |
1561 | count = -EFAULT; | |
1562 | goto read_done; | |
1563 | } | |
1564 | frmx = 0; | |
1565 | /* | |
1566 | * We have no per-frame control over video size. | |
1567 | * Therefore we only can use whatever size was | |
1568 | * specified as default. | |
1569 | */ | |
1570 | uvd->frame[frmx].request = uvd->videosize; | |
1571 | uvd->frame[frmx].palette = uvd->defaultPalette; | |
1572 | uvd->frame[frmx].frameState = FrameState_Ready; | |
1573 | usbvideo_NewFrame(uvd, frmx); | |
1574 | /* Now frame 0 is supposed to start filling... */ | |
1575 | } | |
1576 | ||
1577 | /* | |
1578 | * Get a pointer to the active frame. It is either previously | |
1579 | * completed frame or frame in progress but not completed yet. | |
1580 | */ | |
1581 | frame = &uvd->frame[frmx]; | |
1582 | ||
1583 | /* | |
1584 | * Sit back & wait until the frame gets filled and postprocessed. | |
1585 | * If we fail to get the picture [in time] then return the error. | |
1586 | * In this call we specify that we want the frame to be waited for, | |
1587 | * postprocessed and switched into FrameState_Done_Hold state. This | |
1588 | * state is used to hold the frame as "fully completed" between | |
1589 | * subsequent partial reads of the same frame. | |
1590 | */ | |
1591 | if (frame->frameState != FrameState_Done_Hold) { | |
1592 | long rv = -EFAULT; | |
1593 | if (uvd->flags & FLAGS_NO_DECODING) | |
1594 | rv = usbvideo_GetFrame(uvd, frmx); | |
1595 | else if (VALID_CALLBACK(uvd, getFrame)) | |
1596 | rv = GET_CALLBACK(uvd, getFrame)(uvd, frmx); | |
1597 | else | |
1598 | err("getFrame is not set"); | |
1599 | if ((rv != 0) || (frame->frameState != FrameState_Done_Hold)) { | |
1600 | count = rv; | |
1601 | goto read_done; | |
1602 | } | |
1603 | } | |
1604 | ||
1605 | /* | |
1606 | * Copy bytes to user space. We allow for partial reads, which | |
1607 | * means that the user application can request read less than | |
1608 | * the full frame size. It is up to the application to issue | |
1609 | * subsequent calls until entire frame is read. | |
1610 | * | |
1611 | * First things first, make sure we don't copy more than we | |
1612 | * have - even if the application wants more. That would be | |
1613 | * a big security embarassment! | |
1614 | */ | |
1615 | if ((count + frame->seqRead_Index) > frame->seqRead_Length) | |
1616 | count = frame->seqRead_Length - frame->seqRead_Index; | |
1617 | ||
1618 | /* | |
1619 | * Copy requested amount of data to user space. We start | |
1620 | * copying from the position where we last left it, which | |
1621 | * will be zero for a new frame (not read before). | |
1622 | */ | |
1623 | if (copy_to_user(buf, frame->data + frame->seqRead_Index, count)) { | |
1624 | count = -EFAULT; | |
1625 | goto read_done; | |
1626 | } | |
1627 | ||
1628 | /* Update last read position */ | |
1629 | frame->seqRead_Index += count; | |
1630 | if (uvd->debug >= 1) { | |
1631 | err("%s: {copy} count used=%Zd, new seqRead_Index=%ld", | |
1632 | __FUNCTION__, count, frame->seqRead_Index); | |
1633 | } | |
1634 | ||
1635 | /* Finally check if the frame is done with and "release" it */ | |
1636 | if (frame->seqRead_Index >= frame->seqRead_Length) { | |
1637 | /* All data has been read */ | |
1638 | frame->seqRead_Index = 0; | |
1639 | ||
1640 | /* Mark it as available to be used again. */ | |
1641 | uvd->frame[frmx].frameState = FrameState_Unused; | |
1642 | if (usbvideo_NewFrame(uvd, (frmx + 1) % USBVIDEO_NUMFRAMES)) { | |
1643 | err("%s: usbvideo_NewFrame failed.", __FUNCTION__); | |
1644 | } | |
1645 | } | |
1646 | read_done: | |
1647 | up(&uvd->lock); | |
1648 | return count; | |
1649 | } | |
1650 | ||
1651 | /* | |
1652 | * Make all of the blocks of data contiguous | |
1653 | */ | |
1654 | static int usbvideo_CompressIsochronous(struct uvd *uvd, struct urb *urb) | |
1655 | { | |
1656 | char *cdata; | |
1657 | int i, totlen = 0; | |
1658 | ||
1659 | for (i = 0; i < urb->number_of_packets; i++) { | |
1660 | int n = urb->iso_frame_desc[i].actual_length; | |
1661 | int st = urb->iso_frame_desc[i].status; | |
1662 | ||
1663 | cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset; | |
1664 | ||
1665 | /* Detect and ignore errored packets */ | |
1666 | if (st < 0) { | |
1667 | if (uvd->debug >= 1) | |
1668 | err("Data error: packet=%d. len=%d. status=%d.", i, n, st); | |
1669 | uvd->stats.iso_err_count++; | |
1670 | continue; | |
1671 | } | |
1672 | ||
1673 | /* Detect and ignore empty packets */ | |
1674 | if (n <= 0) { | |
1675 | uvd->stats.iso_skip_count++; | |
1676 | continue; | |
1677 | } | |
1678 | totlen += n; /* Little local accounting */ | |
1679 | RingQueue_Enqueue(&uvd->dp, cdata, n); | |
1680 | } | |
1681 | return totlen; | |
1682 | } | |
1683 | ||
1684 | static void usbvideo_IsocIrq(struct urb *urb, struct pt_regs *regs) | |
1685 | { | |
1686 | int i, ret, len; | |
1687 | struct uvd *uvd = urb->context; | |
1688 | ||
1689 | /* We don't want to do anything if we are about to be removed! */ | |
1690 | if (!CAMERA_IS_OPERATIONAL(uvd)) | |
1691 | return; | |
1692 | #if 0 | |
1693 | if (urb->actual_length > 0) { | |
1694 | info("urb=$%p status=%d. errcount=%d. length=%d.", | |
1695 | urb, urb->status, urb->error_count, urb->actual_length); | |
1696 | } else { | |
1697 | static int c = 0; | |
1698 | if (c++ % 100 == 0) | |
1699 | info("No Isoc data"); | |
1700 | } | |
1701 | #endif | |
1702 | ||
1703 | if (!uvd->streaming) { | |
1704 | if (uvd->debug >= 1) | |
1705 | info("Not streaming, but interrupt!"); | |
1706 | return; | |
1707 | } | |
1708 | ||
1709 | uvd->stats.urb_count++; | |
1710 | if (urb->actual_length <= 0) | |
1711 | goto urb_done_with; | |
1712 | ||
1713 | /* Copy the data received into ring queue */ | |
1714 | len = usbvideo_CompressIsochronous(uvd, urb); | |
1715 | uvd->stats.urb_length = len; | |
1716 | if (len <= 0) | |
1717 | goto urb_done_with; | |
1718 | ||
1719 | /* Here we got some data */ | |
1720 | uvd->stats.data_count += len; | |
1721 | RingQueue_WakeUpInterruptible(&uvd->dp); | |
1722 | ||
1723 | urb_done_with: | |
1724 | for (i = 0; i < FRAMES_PER_DESC; i++) { | |
1725 | urb->iso_frame_desc[i].status = 0; | |
1726 | urb->iso_frame_desc[i].actual_length = 0; | |
1727 | } | |
1728 | urb->status = 0; | |
1729 | urb->dev = uvd->dev; | |
1730 | ret = usb_submit_urb (urb, GFP_KERNEL); | |
1731 | if(ret) | |
1732 | err("usb_submit_urb error (%d)", ret); | |
1733 | return; | |
1734 | } | |
1735 | ||
1736 | /* | |
1737 | * usbvideo_StartDataPump() | |
1738 | * | |
1739 | * History: | |
1740 | * 27-Jan-2000 Used ibmcam->iface, ibmcam->ifaceAltActive instead | |
1741 | * of hardcoded values. Simplified by using for loop, | |
1742 | * allowed any number of URBs. | |
1743 | */ | |
1744 | static int usbvideo_StartDataPump(struct uvd *uvd) | |
1745 | { | |
1746 | struct usb_device *dev = uvd->dev; | |
1747 | int i, errFlag; | |
1748 | ||
1749 | if (uvd->debug > 1) | |
1750 | info("%s($%p)", __FUNCTION__, uvd); | |
1751 | ||
1752 | if (!CAMERA_IS_OPERATIONAL(uvd)) { | |
1753 | err("%s: Camera is not operational", __FUNCTION__); | |
1754 | return -EFAULT; | |
1755 | } | |
1756 | uvd->curframe = -1; | |
1757 | ||
1758 | /* Alternate interface 1 is is the biggest frame size */ | |
1759 | i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive); | |
1760 | if (i < 0) { | |
1761 | err("%s: usb_set_interface error", __FUNCTION__); | |
1762 | uvd->last_error = i; | |
1763 | return -EBUSY; | |
1764 | } | |
1765 | if (VALID_CALLBACK(uvd, videoStart)) | |
1766 | GET_CALLBACK(uvd, videoStart)(uvd); | |
1767 | else | |
1768 | err("%s: videoStart not set", __FUNCTION__); | |
1769 | ||
1770 | /* We double buffer the Iso lists */ | |
1771 | for (i=0; i < USBVIDEO_NUMSBUF; i++) { | |
1772 | int j, k; | |
1773 | struct urb *urb = uvd->sbuf[i].urb; | |
1774 | urb->dev = dev; | |
1775 | urb->context = uvd; | |
1776 | urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp); | |
1777 | urb->interval = 1; | |
1778 | urb->transfer_flags = URB_ISO_ASAP; | |
1779 | urb->transfer_buffer = uvd->sbuf[i].data; | |
1780 | urb->complete = usbvideo_IsocIrq; | |
1781 | urb->number_of_packets = FRAMES_PER_DESC; | |
1782 | urb->transfer_buffer_length = uvd->iso_packet_len * FRAMES_PER_DESC; | |
1783 | for (j=k=0; j < FRAMES_PER_DESC; j++, k += uvd->iso_packet_len) { | |
1784 | urb->iso_frame_desc[j].offset = k; | |
1785 | urb->iso_frame_desc[j].length = uvd->iso_packet_len; | |
1786 | } | |
1787 | } | |
1788 | ||
1789 | /* Submit all URBs */ | |
1790 | for (i=0; i < USBVIDEO_NUMSBUF; i++) { | |
1791 | errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL); | |
1792 | if (errFlag) | |
1793 | err("%s: usb_submit_isoc(%d) ret %d", __FUNCTION__, i, errFlag); | |
1794 | } | |
1795 | ||
1796 | uvd->streaming = 1; | |
1797 | if (uvd->debug > 1) | |
1798 | info("%s: streaming=1 video_endp=$%02x", __FUNCTION__, uvd->video_endp); | |
1799 | return 0; | |
1800 | } | |
1801 | ||
1802 | /* | |
1803 | * usbvideo_StopDataPump() | |
1804 | * | |
1805 | * This procedure stops streaming and deallocates URBs. Then it | |
1806 | * activates zero-bandwidth alt. setting of the video interface. | |
1807 | * | |
1808 | * History: | |
1809 | * 22-Jan-2000 Corrected order of actions to work after surprise removal. | |
1810 | * 27-Jan-2000 Used uvd->iface, uvd->ifaceAltInactive instead of hardcoded values. | |
1811 | */ | |
1812 | static void usbvideo_StopDataPump(struct uvd *uvd) | |
1813 | { | |
1814 | int i, j; | |
1815 | ||
1816 | if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL)) | |
1817 | return; | |
1818 | ||
1819 | if (uvd->debug > 1) | |
1820 | info("%s($%p)", __FUNCTION__, uvd); | |
1821 | ||
1822 | /* Unschedule all of the iso td's */ | |
1823 | for (i=0; i < USBVIDEO_NUMSBUF; i++) { | |
1824 | usb_kill_urb(uvd->sbuf[i].urb); | |
1825 | } | |
1826 | if (uvd->debug > 1) | |
1827 | info("%s: streaming=0", __FUNCTION__); | |
1828 | uvd->streaming = 0; | |
1829 | ||
1830 | if (!uvd->remove_pending) { | |
1831 | /* Invoke minidriver's magic to stop the camera */ | |
1832 | if (VALID_CALLBACK(uvd, videoStop)) | |
1833 | GET_CALLBACK(uvd, videoStop)(uvd); | |
1834 | else | |
1835 | err("%s: videoStop not set", __FUNCTION__); | |
1836 | ||
1837 | /* Set packet size to 0 */ | |
1838 | j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive); | |
1839 | if (j < 0) { | |
1840 | err("%s: usb_set_interface() error %d.", __FUNCTION__, j); | |
1841 | uvd->last_error = j; | |
1842 | } | |
1843 | } | |
1844 | } | |
1845 | ||
1846 | /* | |
1847 | * usbvideo_NewFrame() | |
1848 | * | |
1849 | * History: | |
1850 | * 29-Mar-00 Added copying of previous frame into the current one. | |
1851 | * 6-Aug-00 Added model 3 video sizes, removed redundant width, height. | |
1852 | */ | |
1853 | static int usbvideo_NewFrame(struct uvd *uvd, int framenum) | |
1854 | { | |
1855 | struct usbvideo_frame *frame; | |
1856 | int n; | |
1857 | ||
1858 | if (uvd->debug > 1) | |
1859 | info("usbvideo_NewFrame($%p,%d.)", uvd, framenum); | |
1860 | ||
1861 | /* If we're not grabbing a frame right now and the other frame is */ | |
1862 | /* ready to be grabbed into, then use it instead */ | |
1863 | if (uvd->curframe != -1) | |
1864 | return 0; | |
1865 | ||
1866 | /* If necessary we adjust picture settings between frames */ | |
1867 | if (!uvd->settingsAdjusted) { | |
1868 | if (VALID_CALLBACK(uvd, adjustPicture)) | |
1869 | GET_CALLBACK(uvd, adjustPicture)(uvd); | |
1870 | uvd->settingsAdjusted = 1; | |
1871 | } | |
1872 | ||
1873 | n = (framenum + 1) % USBVIDEO_NUMFRAMES; | |
1874 | if (uvd->frame[n].frameState == FrameState_Ready) | |
1875 | framenum = n; | |
1876 | ||
1877 | frame = &uvd->frame[framenum]; | |
1878 | ||
1879 | frame->frameState = FrameState_Grabbing; | |
1880 | frame->scanstate = ScanState_Scanning; | |
1881 | frame->seqRead_Length = 0; /* Accumulated in xxx_parse_data() */ | |
1882 | frame->deinterlace = Deinterlace_None; | |
1883 | frame->flags = 0; /* No flags yet, up to minidriver (or us) to set them */ | |
1884 | uvd->curframe = framenum; | |
1885 | ||
1886 | /* | |
1887 | * Normally we would want to copy previous frame into the current one | |
1888 | * before we even start filling it with data; this allows us to stop | |
1889 | * filling at any moment; top portion of the frame will be new and | |
1890 | * bottom portion will stay as it was in previous frame. If we don't | |
1891 | * do that then missing chunks of video stream will result in flickering | |
1892 | * portions of old data whatever it was before. | |
1893 | * | |
1894 | * If we choose not to copy previous frame (to, for example, save few | |
1895 | * bus cycles - the frame can be pretty large!) then we have an option | |
1896 | * to clear the frame before using. If we experience losses in this | |
1897 | * mode then missing picture will be black (no flickering). | |
1898 | * | |
1899 | * Finally, if user chooses not to clean the current frame before | |
1900 | * filling it with data then the old data will be visible if we fail | |
1901 | * to refill entire frame with new data. | |
1902 | */ | |
1903 | if (!(uvd->flags & FLAGS_SEPARATE_FRAMES)) { | |
1904 | /* This copies previous frame into this one to mask losses */ | |
1905 | int prev = (framenum - 1 + USBVIDEO_NUMFRAMES) % USBVIDEO_NUMFRAMES; | |
1906 | memmove(frame->data, uvd->frame[prev].data, uvd->max_frame_size); | |
1907 | } else { | |
1908 | if (uvd->flags & FLAGS_CLEAN_FRAMES) { | |
1909 | /* This provides a "clean" frame but slows things down */ | |
1910 | memset(frame->data, 0, uvd->max_frame_size); | |
1911 | } | |
1912 | } | |
1913 | return 0; | |
1914 | } | |
1915 | ||
1916 | /* | |
1917 | * usbvideo_CollectRawData() | |
1918 | * | |
1919 | * This procedure can be used instead of 'processData' callback if you | |
1920 | * only want to dump the raw data from the camera into the output | |
1921 | * device (frame buffer). You can look at it with V4L client, but the | |
1922 | * image will be unwatchable. The main purpose of this code and of the | |
1923 | * mode FLAGS_NO_DECODING is debugging and capturing of datastreams from | |
1924 | * new, unknown cameras. This procedure will be automatically invoked | |
1925 | * instead of the specified callback handler when uvd->flags has bit | |
1926 | * FLAGS_NO_DECODING set. Therefore, any regular build of any driver | |
1927 | * based on usbvideo can use this feature at any time. | |
1928 | */ | |
1929 | static void usbvideo_CollectRawData(struct uvd *uvd, struct usbvideo_frame *frame) | |
1930 | { | |
1931 | int n; | |
1932 | ||
1933 | assert(uvd != NULL); | |
1934 | assert(frame != NULL); | |
1935 | ||
1936 | /* Try to move data from queue into frame buffer */ | |
1937 | n = RingQueue_GetLength(&uvd->dp); | |
1938 | if (n > 0) { | |
1939 | int m; | |
1940 | /* See how much space we have left */ | |
1941 | m = uvd->max_frame_size - frame->seqRead_Length; | |
1942 | if (n > m) | |
1943 | n = m; | |
1944 | /* Now move that much data into frame buffer */ | |
1945 | RingQueue_Dequeue( | |
1946 | &uvd->dp, | |
1947 | frame->data + frame->seqRead_Length, | |
1948 | m); | |
1949 | frame->seqRead_Length += m; | |
1950 | } | |
1951 | /* See if we filled the frame */ | |
1952 | if (frame->seqRead_Length >= uvd->max_frame_size) { | |
1953 | frame->frameState = FrameState_Done; | |
1954 | uvd->curframe = -1; | |
1955 | uvd->stats.frame_num++; | |
1956 | } | |
1957 | } | |
1958 | ||
1959 | static int usbvideo_GetFrame(struct uvd *uvd, int frameNum) | |
1960 | { | |
1961 | struct usbvideo_frame *frame = &uvd->frame[frameNum]; | |
1962 | ||
1963 | if (uvd->debug >= 2) | |
1964 | info("%s($%p,%d.)", __FUNCTION__, uvd, frameNum); | |
1965 | ||
1966 | switch (frame->frameState) { | |
1967 | case FrameState_Unused: | |
1968 | if (uvd->debug >= 2) | |
1969 | info("%s: FrameState_Unused", __FUNCTION__); | |
1970 | return -EINVAL; | |
1971 | case FrameState_Ready: | |
1972 | case FrameState_Grabbing: | |
1973 | case FrameState_Error: | |
1974 | { | |
1975 | int ntries, signalPending; | |
1976 | redo: | |
1977 | if (!CAMERA_IS_OPERATIONAL(uvd)) { | |
1978 | if (uvd->debug >= 2) | |
1979 | info("%s: Camera is not operational (1)", __FUNCTION__); | |
1980 | return -EIO; | |
1981 | } | |
1982 | ntries = 0; | |
1983 | do { | |
1984 | RingQueue_InterruptibleSleepOn(&uvd->dp); | |
1985 | signalPending = signal_pending(current); | |
1986 | if (!CAMERA_IS_OPERATIONAL(uvd)) { | |
1987 | if (uvd->debug >= 2) | |
1988 | info("%s: Camera is not operational (2)", __FUNCTION__); | |
1989 | return -EIO; | |
1990 | } | |
1991 | assert(uvd->fbuf != NULL); | |
1992 | if (signalPending) { | |
1993 | if (uvd->debug >= 2) | |
1994 | info("%s: Signal=$%08x", __FUNCTION__, signalPending); | |
1995 | if (uvd->flags & FLAGS_RETRY_VIDIOCSYNC) { | |
1996 | usbvideo_TestPattern(uvd, 1, 0); | |
1997 | uvd->curframe = -1; | |
1998 | uvd->stats.frame_num++; | |
1999 | if (uvd->debug >= 2) | |
2000 | info("%s: Forced test pattern screen", __FUNCTION__); | |
2001 | return 0; | |
2002 | } else { | |
2003 | /* Standard answer: Interrupted! */ | |
2004 | if (uvd->debug >= 2) | |
2005 | info("%s: Interrupted!", __FUNCTION__); | |
2006 | return -EINTR; | |
2007 | } | |
2008 | } else { | |
2009 | /* No signals - we just got new data in dp queue */ | |
2010 | if (uvd->flags & FLAGS_NO_DECODING) | |
2011 | usbvideo_CollectRawData(uvd, frame); | |
2012 | else if (VALID_CALLBACK(uvd, processData)) | |
2013 | GET_CALLBACK(uvd, processData)(uvd, frame); | |
2014 | else | |
2015 | err("%s: processData not set", __FUNCTION__); | |
2016 | } | |
2017 | } while (frame->frameState == FrameState_Grabbing); | |
2018 | if (uvd->debug >= 2) { | |
2019 | info("%s: Grabbing done; state=%d. (%lu. bytes)", | |
2020 | __FUNCTION__, frame->frameState, frame->seqRead_Length); | |
2021 | } | |
2022 | if (frame->frameState == FrameState_Error) { | |
2023 | int ret = usbvideo_NewFrame(uvd, frameNum); | |
2024 | if (ret < 0) { | |
2025 | err("%s: usbvideo_NewFrame() failed (%d.)", __FUNCTION__, ret); | |
2026 | return ret; | |
2027 | } | |
2028 | goto redo; | |
2029 | } | |
2030 | /* Note that we fall through to meet our destiny below */ | |
2031 | } | |
2032 | case FrameState_Done: | |
2033 | /* | |
2034 | * Do all necessary postprocessing of data prepared in | |
2035 | * "interrupt" code and the collecting code above. The | |
2036 | * frame gets marked as FrameState_Done by queue parsing code. | |
2037 | * This status means that we collected enough data and | |
2038 | * most likely processed it as we went through. However | |
2039 | * the data may need postprocessing, such as deinterlacing | |
2040 | * or picture adjustments implemented in software (horror!) | |
2041 | * | |
2042 | * As soon as the frame becomes "final" it gets promoted to | |
2043 | * FrameState_Done_Hold status where it will remain until the | |
2044 | * caller consumed all the video data from the frame. Then | |
2045 | * the empty shell of ex-frame is thrown out for dogs to eat. | |
2046 | * But we, worried about pets, will recycle the frame! | |
2047 | */ | |
2048 | uvd->stats.frame_num++; | |
2049 | if ((uvd->flags & FLAGS_NO_DECODING) == 0) { | |
2050 | if (VALID_CALLBACK(uvd, postProcess)) | |
2051 | GET_CALLBACK(uvd, postProcess)(uvd, frame); | |
2052 | if (frame->flags & USBVIDEO_FRAME_FLAG_SOFTWARE_CONTRAST) | |
2053 | usbvideo_SoftwareContrastAdjustment(uvd, frame); | |
2054 | } | |
2055 | frame->frameState = FrameState_Done_Hold; | |
2056 | if (uvd->debug >= 2) | |
2057 | info("%s: Entered FrameState_Done_Hold state.", __FUNCTION__); | |
2058 | return 0; | |
2059 | ||
2060 | case FrameState_Done_Hold: | |
2061 | /* | |
2062 | * We stay in this state indefinitely until someone external, | |
2063 | * like ioctl() or read() call finishes digesting the frame | |
2064 | * data. Then it will mark the frame as FrameState_Unused and | |
2065 | * it will be released back into the wild to roam freely. | |
2066 | */ | |
2067 | if (uvd->debug >= 2) | |
2068 | info("%s: FrameState_Done_Hold state.", __FUNCTION__); | |
2069 | return 0; | |
2070 | } | |
2071 | ||
2072 | /* Catch-all for other cases. We shall not be here. */ | |
2073 | err("%s: Invalid state %d.", __FUNCTION__, frame->frameState); | |
2074 | frame->frameState = FrameState_Unused; | |
2075 | return 0; | |
2076 | } | |
2077 | ||
2078 | /* | |
2079 | * usbvideo_DeinterlaceFrame() | |
2080 | * | |
2081 | * This procedure deinterlaces the given frame. Some cameras produce | |
2082 | * only half of scanlines - sometimes only even lines, sometimes only | |
2083 | * odd lines. The deinterlacing method is stored in frame->deinterlace | |
2084 | * variable. | |
2085 | * | |
2086 | * Here we scan the frame vertically and replace missing scanlines with | |
2087 | * average between surrounding ones - before and after. If we have no | |
2088 | * line above then we just copy next line. Similarly, if we need to | |
2089 | * create a last line then preceding line is used. | |
2090 | */ | |
2091 | void usbvideo_DeinterlaceFrame(struct uvd *uvd, struct usbvideo_frame *frame) | |
2092 | { | |
2093 | if ((uvd == NULL) || (frame == NULL)) | |
2094 | return; | |
2095 | ||
2096 | if ((frame->deinterlace == Deinterlace_FillEvenLines) || | |
2097 | (frame->deinterlace == Deinterlace_FillOddLines)) | |
2098 | { | |
2099 | const int v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL; | |
2100 | int i = (frame->deinterlace == Deinterlace_FillEvenLines) ? 0 : 1; | |
2101 | ||
2102 | for (; i < VIDEOSIZE_Y(frame->request); i += 2) { | |
2103 | const unsigned char *fs1, *fs2; | |
2104 | unsigned char *fd; | |
2105 | int ip, in, j; /* Previous and next lines */ | |
2106 | ||
2107 | /* | |
2108 | * Need to average lines before and after 'i'. | |
2109 | * If we go out of bounds seeking those lines then | |
2110 | * we point back to existing line. | |
2111 | */ | |
2112 | ip = i - 1; /* First, get rough numbers */ | |
2113 | in = i + 1; | |
2114 | ||
2115 | /* Now validate */ | |
2116 | if (ip < 0) | |
2117 | ip = in; | |
2118 | if (in >= VIDEOSIZE_Y(frame->request)) | |
2119 | in = ip; | |
2120 | ||
2121 | /* Sanity check */ | |
2122 | if ((ip < 0) || (in < 0) || | |
2123 | (ip >= VIDEOSIZE_Y(frame->request)) || | |
2124 | (in >= VIDEOSIZE_Y(frame->request))) | |
2125 | { | |
2126 | err("Error: ip=%d. in=%d. req.height=%ld.", | |
2127 | ip, in, VIDEOSIZE_Y(frame->request)); | |
2128 | break; | |
2129 | } | |
2130 | ||
2131 | /* Now we need to average lines 'ip' and 'in' to produce line 'i' */ | |
2132 | fs1 = frame->data + (v4l_linesize * ip); | |
2133 | fs2 = frame->data + (v4l_linesize * in); | |
2134 | fd = frame->data + (v4l_linesize * i); | |
2135 | ||
2136 | /* Average lines around destination */ | |
2137 | for (j=0; j < v4l_linesize; j++) { | |
2138 | fd[j] = (unsigned char)((((unsigned) fs1[j]) + | |
2139 | ((unsigned)fs2[j])) >> 1); | |
2140 | } | |
2141 | } | |
2142 | } | |
2143 | ||
2144 | /* Optionally display statistics on the screen */ | |
2145 | if (uvd->flags & FLAGS_OVERLAY_STATS) | |
2146 | usbvideo_OverlayStats(uvd, frame); | |
2147 | } | |
2148 | ||
2149 | EXPORT_SYMBOL(usbvideo_DeinterlaceFrame); | |
2150 | ||
2151 | /* | |
2152 | * usbvideo_SoftwareContrastAdjustment() | |
2153 | * | |
2154 | * This code adjusts the contrast of the frame, assuming RGB24 format. | |
2155 | * As most software image processing, this job is CPU-intensive. | |
2156 | * Get a camera that supports hardware adjustment! | |
2157 | * | |
2158 | * History: | |
2159 | * 09-Feb-2001 Created. | |
2160 | */ | |
2161 | static void usbvideo_SoftwareContrastAdjustment(struct uvd *uvd, | |
2162 | struct usbvideo_frame *frame) | |
2163 | { | |
2164 | int i, j, v4l_linesize; | |
2165 | signed long adj; | |
2166 | const int ccm = 128; /* Color correction median - see below */ | |
2167 | ||
2168 | if ((uvd == NULL) || (frame == NULL)) { | |
2169 | err("%s: Illegal call.", __FUNCTION__); | |
2170 | return; | |
2171 | } | |
2172 | adj = (uvd->vpic.contrast - 0x8000) >> 8; /* -128..+127 = -ccm..+(ccm-1)*/ | |
2173 | RESTRICT_TO_RANGE(adj, -ccm, ccm+1); | |
2174 | if (adj == 0) { | |
2175 | /* In rare case of no adjustment */ | |
2176 | return; | |
2177 | } | |
2178 | v4l_linesize = VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL; | |
2179 | for (i=0; i < VIDEOSIZE_Y(frame->request); i++) { | |
2180 | unsigned char *fd = frame->data + (v4l_linesize * i); | |
2181 | for (j=0; j < v4l_linesize; j++) { | |
2182 | signed long v = (signed long) fd[j]; | |
2183 | /* Magnify up to 2 times, reduce down to zero */ | |
2184 | v = 128 + ((ccm + adj) * (v - 128)) / ccm; | |
2185 | RESTRICT_TO_RANGE(v, 0, 0xFF); /* Must flatten tails */ | |
2186 | fd[j] = (unsigned char) v; | |
2187 | } | |
2188 | } | |
2189 | } | |
2190 | ||
2191 | MODULE_LICENSE("GPL"); |