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