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