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