📄 et61x251_core.c
字号:
}static intet61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg){ int index; if (copy_from_user(&index, arg, sizeof(index))) return -EFAULT; if (index != 0) return -EINVAL; return 0;}static intet61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg){ struct et61x251_sensor* s = &cam->sensor; struct v4l2_queryctrl qc; u8 i; if (copy_from_user(&qc, arg, sizeof(qc))) return -EFAULT; for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) if (qc.id && qc.id == s->qctrl[i].id) { memcpy(&qc, &(s->qctrl[i]), sizeof(qc)); if (copy_to_user(arg, &qc, sizeof(qc))) return -EFAULT; return 0; } return -EINVAL;}static intet61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg){ struct et61x251_sensor* s = &cam->sensor; struct v4l2_control ctrl; int err = 0; u8 i; if (!s->get_ctrl && !s->set_ctrl) return -EINVAL; if (copy_from_user(&ctrl, arg, sizeof(ctrl))) return -EFAULT; if (!s->get_ctrl) { for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) if (ctrl.id == s->qctrl[i].id) { ctrl.value = s->_qctrl[i].default_value; goto exit; } return -EINVAL; } else err = s->get_ctrl(cam, &ctrl);exit: if (copy_to_user(arg, &ctrl, sizeof(ctrl))) return -EFAULT; return err;}static intet61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg){ struct et61x251_sensor* s = &cam->sensor; struct v4l2_control ctrl; u8 i; int err = 0; if (!s->set_ctrl) return -EINVAL; if (copy_from_user(&ctrl, arg, sizeof(ctrl))) return -EFAULT; for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) if (ctrl.id == s->qctrl[i].id) { if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED) return -EINVAL; if (ctrl.value < s->qctrl[i].minimum || ctrl.value > s->qctrl[i].maximum) return -ERANGE; ctrl.value -= ctrl.value % s->qctrl[i].step; break; } if ((err = s->set_ctrl(cam, &ctrl))) return err; s->_qctrl[i].default_value = ctrl.value; return 0;}static intet61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg){ struct v4l2_cropcap* cc = &(cam->sensor.cropcap); cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; cc->pixelaspect.numerator = 1; cc->pixelaspect.denominator = 1; if (copy_to_user(arg, cc, sizeof(*cc))) return -EFAULT; return 0;}static intet61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg){ struct et61x251_sensor* s = &cam->sensor; struct v4l2_crop crop = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, }; memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect)); if (copy_to_user(arg, &crop, sizeof(crop))) return -EFAULT; return 0;}static intet61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg){ struct et61x251_sensor* s = &cam->sensor; struct v4l2_crop crop; struct v4l2_rect* rect; struct v4l2_rect* bounds = &(s->cropcap.bounds); struct v4l2_pix_format* pix_format = &(s->pix_format); u8 scale; const enum et61x251_stream_state stream = cam->stream; const u32 nbuffers = cam->nbuffers; u32 i; int err = 0; if (copy_from_user(&crop, arg, sizeof(crop))) return -EFAULT; rect = &(crop.c); if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; if (cam->module_param.force_munmap) for (i = 0; i < cam->nbuffers; i++) if (cam->frame[i].vma_use_count) { DBG(3, "VIDIOC_S_CROP failed. " "Unmap the buffers first."); return -EINVAL; } /* Preserve R,G or B origin */ rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L; rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L; if (rect->width < 4) rect->width = 4; if (rect->height < 4) rect->height = 4; if (rect->width > bounds->width) rect->width = bounds->width; if (rect->height > bounds->height) rect->height = bounds->height; if (rect->left < bounds->left) rect->left = bounds->left; if (rect->top < bounds->top) rect->top = bounds->top; if (rect->left + rect->width > bounds->left + bounds->width) rect->left = bounds->left+bounds->width - rect->width; if (rect->top + rect->height > bounds->top + bounds->height) rect->top = bounds->top+bounds->height - rect->height; rect->width &= ~3L; rect->height &= ~3L; if (ET61X251_PRESERVE_IMGSCALE) { /* Calculate the actual scaling factor */ u32 a, b; a = rect->width * rect->height; b = pix_format->width * pix_format->height; scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1; } else scale = 1; if (cam->stream == STREAM_ON) if ((err = et61x251_stream_interrupt(cam))) return err; if (copy_to_user(arg, &crop, sizeof(crop))) { cam->stream = stream; return -EFAULT; } if (cam->module_param.force_munmap || cam->io == IO_READ) et61x251_release_buffers(cam); err = et61x251_set_crop(cam, rect); if (s->set_crop) err += s->set_crop(cam, rect); err += et61x251_set_scale(cam, scale); if (err) { /* atomic, no rollback in ioctl() */ cam->state |= DEV_MISCONFIGURED; DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To " "use the camera, close and open /dev/video%d again.", cam->v4ldev->minor); return -EIO; } s->pix_format.width = rect->width/scale; s->pix_format.height = rect->height/scale; memcpy(&(s->_rect), rect, sizeof(*rect)); if ((cam->module_param.force_munmap || cam->io == IO_READ) && nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) { cam->state |= DEV_MISCONFIGURED; DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To " "use the camera, close and open /dev/video%d again.", cam->v4ldev->minor); return -ENOMEM; } if (cam->io == IO_READ) et61x251_empty_framequeues(cam); else if (cam->module_param.force_munmap) et61x251_requeue_outqueue(cam); cam->stream = stream; return 0;}static intet61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg){ struct v4l2_fmtdesc fmtd; if (copy_from_user(&fmtd, arg, sizeof(fmtd))) return -EFAULT; if (fmtd.index == 0) { strcpy(fmtd.description, "bayer rgb"); fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8; } else if (fmtd.index == 1) { strcpy(fmtd.description, "compressed"); fmtd.pixelformat = V4L2_PIX_FMT_ET61X251; fmtd.flags = V4L2_FMT_FLAG_COMPRESSED; } else return -EINVAL; fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; memset(&fmtd.reserved, 0, sizeof(fmtd.reserved)); if (copy_to_user(arg, &fmtd, sizeof(fmtd))) return -EFAULT; return 0;}static intet61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg){ struct v4l2_format format; struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format); if (copy_from_user(&format, arg, sizeof(format))) return -EFAULT; if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251) ? 0 : (pfmt->width * pfmt->priv) / 8; pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8); pfmt->field = V4L2_FIELD_NONE; memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt)); if (copy_to_user(arg, &format, sizeof(format))) return -EFAULT; return 0;}static intet61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd, void __user * arg){ struct et61x251_sensor* s = &cam->sensor; struct v4l2_format format; struct v4l2_pix_format* pix; struct v4l2_pix_format* pfmt = &(s->pix_format); struct v4l2_rect* bounds = &(s->cropcap.bounds); struct v4l2_rect rect; u8 scale; const enum et61x251_stream_state stream = cam->stream; const u32 nbuffers = cam->nbuffers; u32 i; int err = 0; if (copy_from_user(&format, arg, sizeof(format))) return -EFAULT; pix = &(format.fmt.pix); if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; memcpy(&rect, &(s->_rect), sizeof(rect)); { /* calculate the actual scaling factor */ u32 a, b; a = rect.width * rect.height; b = pix->width * pix->height; scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1; } rect.width = scale * pix->width; rect.height = scale * pix->height; if (rect.width < 4) rect.width = 4; if (rect.height < 4) rect.height = 4; if (rect.width > bounds->left + bounds->width - rect.left) rect.width = bounds->left + bounds->width - rect.left; if (rect.height > bounds->top + bounds->height - rect.top) rect.height = bounds->top + bounds->height - rect.top; rect.width &= ~3L; rect.height &= ~3L; { /* adjust the scaling factor */ u32 a, b; a = rect.width * rect.height; b = pix->width * pix->height; scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1; } pix->width = rect.width / scale; pix->height = rect.height / scale; if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 && pix->pixelformat != V4L2_PIX_FMT_SBGGR8) pix->pixelformat = pfmt->pixelformat; pix->priv = pfmt->priv; /* bpp */ pix->colorspace = pfmt->colorspace; pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251) ? 0 : (pix->width * pix->priv) / 8; pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8); pix->field = V4L2_FIELD_NONE; if (cmd == VIDIOC_TRY_FMT) { if (copy_to_user(arg, &format, sizeof(format))) return -EFAULT; return 0; } if (cam->module_param.force_munmap) for (i = 0; i < cam->nbuffers; i++) if (cam->frame[i].vma_use_count) { DBG(3, "VIDIOC_S_FMT failed. " "Unmap the buffers first."); return -EINVAL; } if (cam->stream == STREAM_ON) if ((err = et61x251_stream_interrupt(cam))) return err; if (copy_to_user(arg, &format, sizeof(format))) { cam->stream = stream; return -EFAULT; } if (cam->module_param.force_munmap || cam->io == IO_READ) et61x251_release_buffers(cam); err += et61x251_set_pix_format(cam, pix); err += et61x251_set_crop(cam, &rect); if (s->set_pix_format) err += s->set_pix_format(cam, pix); if (s->set_crop) err += s->set_crop(cam, &rect); err += et61x251_set_scale(cam, scale); if (err) { /* atomic, no rollback in ioctl() */ cam->state |= DEV_MISCONFIGURED; DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To " "use the camera, close and open /dev/video%d again.", cam->v4ldev->minor); return -EIO; } memcpy(pfmt, pix, sizeof(*pix)); memcpy(&(s->_rect), &rect, sizeof(rect)); if ((cam->module_param.force_munmap || cam->io == IO_READ) && nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) { cam->state |= DEV_MISCONFIGURED; DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To " "use the camera, close and open /dev/video%d again.", cam->v4ldev->minor); return -ENOMEM; } if (cam->io == IO_READ) et61x251_empty_framequeues(cam); else if (cam->module_param.force_munmap) et61x251_requeue_outqueue(cam); cam->stream = stream; return 0;}static intet61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg){ if (copy_to_user(arg, &cam->compression, sizeof(cam->compression))) return -EFAULT; return 0;}static intet61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg){ struct v4l2_jpegcompression jc; const enum et61x251_stream_state stream = cam->stream; int err = 0; if (copy_from_user(&jc, arg, sizeof(jc))) return -EFAULT; if (jc.quality != 0 && jc.quality != 1) return -EINVAL; if (cam->stream == STREAM_ON) if ((err = et61x251_stream_interrupt(cam))) return err; err += et61x251_set_compression(cam, &jc); if (err) { /* atomic, no rollback in ioctl() */ cam->state |= DEV_MISCONFIGURED; DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware " "problems. To use the camera, close and open " "/dev/video%d again.", cam->v4ldev->minor); return -EIO; } cam->compression.quality = jc.quality; cam->stream = stream; return 0;}static intet61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg){ struct v4l2_requestbuffers rb; u32 i; int err; if (copy_from_user(&rb, arg, sizeof(rb))) return -EFAULT; if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || rb.memory != V4L2_MEMORY_MMAP) return -EINVAL; if (cam->io == IO_READ) { DBG(3, "Close and open the device again to choose the mmap " "I/O method"); return -EINVAL; } for (i = 0; i < cam->nbuffers; i++) if (cam->frame[i].vma_use_count) { DBG(3, "VIDIOC_REQBUFS failed. " "Previous buffers are still mapped."); return -EINVAL; } if (cam->stream == STREAM_ON) if ((err = et61x251_stream_interrupt(cam))) return err; et61x251_empty_framequeues(cam); et61x251_release_buffers(cam); if (rb.count) rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP); if (copy_to_user(arg, &rb, sizeof(rb))) { et61x251_release_buffers(cam); cam->io = IO_NONE; return -EFAULT; } cam->io = rb.count ? IO_MMAP : IO_NONE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -