usbvideo.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 2,283 行 · 第 1/5 页
C
2,283 行
/* * usbvideo_Disconnect() * * This procedure stops all driver activity. Deallocation of * the interface-private structure (pointed by 'ptr') is done now * (if we don't have any open files) or later, when those files * are closed. After that driver should be removable. * * This code handles surprise removal. The uvd->user is a counter which * increments on open() and decrements on close(). If we see here that * this counter is not 0 then we have a client who still has us opened. * We set uvd->remove_pending flag as early as possible, and after that * all access to the camera will gracefully fail. These failures should * prompt client to (eventually) close the video device, and then - in * usbvideo_v4l_close() - we decrement uvd->uvd_used and usage counter. * * History: * 22-Jan-2000 Added polling of MOD_IN_USE to delay removal until all users gone. * 27-Jan-2000 Reworked to allow pending disconnects; see xxx_close() * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT). * 19-Oct-2000 Moved to usbvideo module. */static void usbvideo_Disconnect(struct usb_interface *intf){ struct uvd *uvd = usb_get_intfdata (intf); int i; if (uvd == NULL) { err("%s($%p): Illegal call.", __FUNCTION__, intf); return; } usb_set_intfdata (intf, NULL); usbvideo_ClientIncModCount(uvd); if (uvd->debug > 0) info("%s(%p.)", __FUNCTION__, intf); down(&uvd->lock); uvd->remove_pending = 1; /* Now all ISO data will be ignored */ /* At this time we ask to cancel outstanding URBs */ GET_CALLBACK(uvd, stopDataPump)(uvd); for (i=0; i < USBVIDEO_NUMSBUF; i++) usb_free_urb(uvd->sbuf[i].urb); usb_put_dev(uvd->dev); uvd->dev = NULL; /* USB device is no more */ video_unregister_device(&uvd->vdev); if (uvd->debug > 0) info("%s: Video unregistered.", __FUNCTION__); if (uvd->user) info("%s: In use, disconnect pending.", __FUNCTION__); else usbvideo_CameraRelease(uvd); up(&uvd->lock); info("USB camera disconnected."); usbvideo_ClientDecModCount(uvd);}/* * usbvideo_CameraRelease() * * This code does final release of uvd. This happens * after the device is disconnected -and- all clients * closed their files. * * History: * 27-Jan-2000 Created. */static void usbvideo_CameraRelease(struct uvd *uvd){ if (uvd == NULL) { err("%s: Illegal call", __FUNCTION__); return; } RingQueue_Free(&uvd->dp); if (VALID_CALLBACK(uvd, userFree)) GET_CALLBACK(uvd, userFree)(uvd); uvd->uvd_used = 0; /* This is atomic, no need to take mutex */}/* * usbvideo_find_struct() * * This code searches the array of preallocated (static) structures * and returns index of the first one that isn't in use. Returns -1 * if there are no free structures. * * History: * 27-Jan-2000 Created. */static int usbvideo_find_struct(struct usbvideo *cams){ int u, rv = -1; if (cams == NULL) { err("No usbvideo handle?"); return -1; } down(&cams->lock); for (u = 0; u < cams->num_cameras; u++) { struct uvd *uvd = &cams->cam[u]; if (!uvd->uvd_used) /* This one is free */ { uvd->uvd_used = 1; /* In use now */ init_MUTEX(&uvd->lock); /* to 1 == available */ uvd->dev = NULL; rv = u; break; } } up(&cams->lock); return rv;}static struct file_operations usbvideo_fops = { .owner = THIS_MODULE, .open = usbvideo_v4l_open, .release =usbvideo_v4l_close, .read = usbvideo_v4l_read, .mmap = usbvideo_v4l_mmap, .ioctl = usbvideo_v4l_ioctl, .llseek = no_llseek,};static struct video_device usbvideo_template = { .owner = THIS_MODULE, .type = VID_TYPE_CAPTURE, .hardware = VID_HARDWARE_CPIA, .fops = &usbvideo_fops,};struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams){ int i, devnum; struct uvd *uvd = NULL; if (cams == NULL) { err("No usbvideo handle?"); return NULL; } devnum = usbvideo_find_struct(cams); if (devnum == -1) { err("IBM USB camera driver: Too many devices!"); return NULL; } uvd = &cams->cam[devnum]; dbg("Device entry #%d. at $%p", devnum, uvd); /* Not relying upon caller we increase module counter ourselves */ usbvideo_ClientIncModCount(uvd); down(&uvd->lock); for (i=0; i < USBVIDEO_NUMSBUF; i++) { uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL); if (uvd->sbuf[i].urb == NULL) { err("usb_alloc_urb(%d.) failed.", FRAMES_PER_DESC); uvd->uvd_used = 0; uvd = NULL; goto allocate_done; } } uvd->user=0; uvd->remove_pending = 0; uvd->last_error = 0; RingQueue_Initialize(&uvd->dp); /* Initialize video device structure */ uvd->vdev = usbvideo_template; sprintf(uvd->vdev.name, "%.20s USB Camera", cams->drvName); /* * The client is free to overwrite those because we * return control to the client's probe function right now. */allocate_done: up (&uvd->lock); usbvideo_ClientDecModCount(uvd); return uvd;}EXPORT_SYMBOL(usbvideo_AllocateDevice);int usbvideo_RegisterVideoDevice(struct uvd *uvd){ char tmp1[20], tmp2[20]; /* Buffers for printing */ if (uvd == NULL) { err("%s: Illegal call.", __FUNCTION__); return -EINVAL; } if (uvd->video_endp == 0) { info("%s: No video endpoint specified; data pump disabled.", __FUNCTION__); } if (uvd->paletteBits == 0) { err("%s: No palettes specified!", __FUNCTION__); return -EINVAL; } if (uvd->defaultPalette == 0) { info("%s: No default palette!", __FUNCTION__); } uvd->max_frame_size = VIDEOSIZE_X(uvd->canvas) * VIDEOSIZE_Y(uvd->canvas) * V4L_BYTES_PER_PIXEL; usbvideo_VideosizeToString(tmp1, sizeof(tmp1), uvd->videosize); usbvideo_VideosizeToString(tmp2, sizeof(tmp2), uvd->canvas); if (uvd->debug > 0) { info("%s: iface=%d. endpoint=$%02x paletteBits=$%08lx", __FUNCTION__, uvd->iface, uvd->video_endp, uvd->paletteBits); } if (video_register_device(&uvd->vdev, VFL_TYPE_GRABBER, video_nr) == -1) { err("%s: video_register_device failed", __FUNCTION__); return -EPIPE; } if (uvd->debug > 1) { info("%s: video_register_device() successful", __FUNCTION__); } if (uvd->dev == NULL) { err("%s: uvd->dev == NULL", __FUNCTION__); return -EINVAL; } info("%s on /dev/video%d: canvas=%s videosize=%s", (uvd->handle != NULL) ? uvd->handle->drvName : "???", uvd->vdev.minor, tmp2, tmp1); usb_get_dev(uvd->dev); return 0;}EXPORT_SYMBOL(usbvideo_RegisterVideoDevice);/* ******************************************************************** */static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma){ struct uvd *uvd = file->private_data; unsigned long start = vma->vm_start; unsigned long size = vma->vm_end-vma->vm_start; unsigned long page, pos; if (!CAMERA_IS_OPERATIONAL(uvd)) return -EFAULT; if (size > (((USBVIDEO_NUMFRAMES * uvd->max_frame_size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))) return -EINVAL; pos = (unsigned long) uvd->fbuf; while (size > 0) { page = usbvideo_kvirt_to_pa(pos); if (remap_page_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) return -EAGAIN; start += PAGE_SIZE; pos += PAGE_SIZE; if (size > PAGE_SIZE) size -= PAGE_SIZE; else size = 0; } return 0;}/* * usbvideo_v4l_open() * * This is part of Video 4 Linux API. The driver can be opened by one * client only (checks internal counter 'uvdser'). The procedure * then allocates buffers needed for video processing. * * History: * 22-Jan-2000 Rewrote, moved scratch buffer allocation here. Now the * camera is also initialized here (once per connect), at * expense of V4L client (it waits on open() call). * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers. * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT). */static int usbvideo_v4l_open(struct inode *inode, struct file *file){ struct video_device *dev = video_devdata(file); struct uvd *uvd = (struct uvd *) dev; const int sb_size = FRAMES_PER_DESC * uvd->iso_packet_len; int i, errCode = 0; if (uvd->debug > 1) info("%s($%p)", __FUNCTION__, dev); usbvideo_ClientIncModCount(uvd); down(&uvd->lock); if (uvd->user) { err("%s: Someone tried to open an already opened device!", __FUNCTION__); errCode = -EBUSY; } else { /* Clear statistics */ memset(&uvd->stats, 0, sizeof(uvd->stats)); /* Clean pointers so we know if we allocated something */ for (i=0; i < USBVIDEO_NUMSBUF; i++) uvd->sbuf[i].data = NULL; /* Allocate memory for the frame buffers */ uvd->fbuf_size = USBVIDEO_NUMFRAMES * uvd->max_frame_size; uvd->fbuf = usbvideo_rvmalloc(uvd->fbuf_size); RingQueue_Allocate(&uvd->dp, RING_QUEUE_SIZE); if ((uvd->fbuf == NULL) || (!RingQueue_IsAllocated(&uvd->dp))) { err("%s: Failed to allocate fbuf or dp", __FUNCTION__); errCode = -ENOMEM; } else { /* Allocate all buffers */ for (i=0; i < USBVIDEO_NUMFRAMES; i++) { uvd->frame[i].frameState = FrameState_Unused; uvd->frame[i].data = uvd->fbuf + i*(uvd->max_frame_size); /* * Set default sizes in case IOCTL (VIDIOCMCAPTURE) * is not used (using read() instead). */ uvd->frame[i].canvas = uvd->canvas; uvd->frame[i].seqRead_Index = 0; } for (i=0; i < USBVIDEO_NUMSBUF; i++) { uvd->sbuf[i].data = kmalloc(sb_size, GFP_KERNEL); if (uvd->sbuf[i].data == NULL) { errCode = -ENOMEM; break; } } } if (errCode != 0) { /* Have to free all that memory */ if (uvd->fbuf != NULL) { usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size); uvd->fbuf = NULL; } RingQueue_Free(&uvd->dp); for (i=0; i < USBVIDEO_NUMSBUF; i++) { if (uvd->sbuf[i].data != NULL) { kfree (uvd->sbuf[i].data); uvd->sbuf[i].data = NULL; } } } } /* If so far no errors then we shall start the camera */ if (errCode == 0) { /* Start data pump if we have valid endpoint */ if (uvd->video_endp != 0) errCode = GET_CALLBACK(uvd, startDataPump)(uvd); if (errCode == 0) { if (VALID_CALLBACK(uvd, setupOnOpen)) { if (uvd->debug > 1) info("%s: setupOnOpen callback", __FUNCTION__); errCode = GET_CALLBACK(uvd, setupOnOpen)(uvd); if (errCode < 0) { err("%s: setupOnOpen callback failed (%d.).", __FUNCTION__, errCode); } else if (uvd->debug > 1) { info("%s: setupOnOpen callback successful", __FUNCTION__); } } if (errCode == 0) { uvd->settingsAdjusted = 0; if (uvd->debug > 1) info("%s: Open succeeded.", __FUNCTION__); uvd->user++; file->private_data = uvd; } } } up(&uvd->lock); if (errCode != 0) usbvideo_ClientDecModCount(uvd); if (uvd->debug > 0) info("%s: Returning %d.", __FUNCTION__, errCode); return errCode;}/* * usbvideo_v4l_close() * * This is part of Video 4 Linux API. The procedure * stops streaming and deallocates all buffers that were earlier * allocated in usbvideo_v4l_open(). * * History: * 22-Jan-2000 Moved scratch buffer deallocation here. * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers. * 24-May-2000 Moved MOD_DEC_USE_COUNT outside of code that can sleep. */static int usbvideo_v4l_close(struct inode *inode, struct file *file){ struct video_device *dev = file->private_data; struct uvd *uvd = (struct uvd *) dev; int i; if (uvd->debug > 1) info("%s($%p)", __FUNCTION__, dev); down(&uvd->lock); GET_CALLBACK(uvd, stopDataPump)(uvd); usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size); uvd->fbuf = NULL; RingQueue_Free(&uvd->dp); for (i=0; i < USBVIDEO_NUMSBUF; i++) { kfree(uvd->sbuf[i].data); uvd->sbuf[i].data = NULL; }#if USBVIDEO_REPORT_STATS usbvideo_ReportStatistics(uvd);#endif uvd->user--; if (uvd->remove_pending) { if (uvd->debug > 0) info("usbvideo_v4l_close: Final disconnect."); usbvideo_CameraRelease(uvd); } up(&uvd->lock); usbvideo_ClientDecModCount(uvd); if (uvd->debug > 1) info("%s: Completed.", __FUNCTION__); file->private_data = NULL; return 0;}/* * usbvideo_v4l_ioctl() * * This is part of Video 4 Linux API. The procedure handles ioctl() calls. * * History: * 22-Jan-2000 Corrected VIDIOCSPICT to reject unsupported settings. */static int usbvideo_v4l_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, void *arg){ struct uvd *uvd = file->private_data; if (!CAMERA_IS_OPERATIONAL(uvd)) return -EIO; switch (cmd) { case VIDIOCGCAP: {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?