123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911 |
- #include <linux/types.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/kmod.h>
- #include <linux/mutex.h>
- #include <linux/pci.h>
- #include <linux/interrupt.h>
- #include <linux/videodev2.h>
- #include <linux/v4l2-dv-timings.h>
- #include <media/v4l2-device.h>
- #include <media/v4l2-dev.h>
- #include <media/v4l2-ioctl.h>
- #include <media/v4l2-dv-timings.h>
- #include <media/v4l2-ctrls.h>
- #include <media/v4l2-event.h>
- #include <media/videobuf2-v4l2.h>
- #include <media/videobuf2-dma-contig.h>
- MODULE_DESCRIPTION("V4L2 PCI Skeleton Driver");
- MODULE_AUTHOR("Hans Verkuil");
- MODULE_LICENSE("GPL v2");
- struct skeleton {
- struct pci_dev *pdev;
- struct v4l2_device v4l2_dev;
- struct video_device vdev;
- struct v4l2_ctrl_handler ctrl_handler;
- struct mutex lock;
- v4l2_std_id std;
- struct v4l2_dv_timings timings;
- struct v4l2_pix_format format;
- unsigned input;
- struct vb2_queue queue;
- spinlock_t qlock;
- struct list_head buf_list;
- unsigned field;
- unsigned sequence;
- };
- struct skel_buffer {
- struct vb2_buffer vb;
- struct list_head list;
- };
- static inline struct skel_buffer *to_skel_buffer(struct vb2_buffer *vb2)
- {
- return container_of(vb2, struct skel_buffer, vb);
- }
- static const struct pci_device_id skeleton_pci_tbl[] = {
-
- { 0, }
- };
- MODULE_DEVICE_TABLE(pci, skeleton_pci_tbl);
- static const struct v4l2_dv_timings_cap skel_timings_cap = {
- .type = V4L2_DV_BT_656_1120,
-
- .reserved = { 0 },
- V4L2_INIT_BT_TIMINGS(
- 720, 1920,
- 480, 1080,
- 27000000, 74250000,
- V4L2_DV_BT_STD_CEA861,
-
- V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE
- )
- };
- #define SKEL_TVNORMS V4L2_STD_ALL
- static irqreturn_t skeleton_irq(int irq, void *dev_id)
- {
- #ifdef TODO
- struct skeleton *skel = dev_id;
-
-
- if (captured_new_frame) {
- ...
- spin_lock(&skel->qlock);
- list_del(&new_buf->list);
- spin_unlock(&skel->qlock);
- v4l2_get_timestamp(&new_buf->vb.v4l2_buf.timestamp);
- new_buf->vb.v4l2_buf.sequence = skel->sequence++;
- new_buf->vb.v4l2_buf.field = skel->field;
- if (skel->format.field == V4L2_FIELD_ALTERNATE) {
- if (skel->field == V4L2_FIELD_BOTTOM)
- skel->field = V4L2_FIELD_TOP;
- else if (skel->field == V4L2_FIELD_TOP)
- skel->field = V4L2_FIELD_BOTTOM;
- }
- vb2_buffer_done(&new_buf->vb, VB2_BUF_STATE_DONE);
- }
- #endif
- return IRQ_HANDLED;
- }
- static int queue_setup(struct vb2_queue *vq,
- unsigned int *nbuffers, unsigned int *nplanes,
- unsigned int sizes[], struct device *alloc_devs[])
- {
- struct skeleton *skel = vb2_get_drv_priv(vq);
- skel->field = skel->format.field;
- if (skel->field == V4L2_FIELD_ALTERNATE) {
-
- if (vb2_fileio_is_active(vq))
- return -EINVAL;
- skel->field = V4L2_FIELD_TOP;
- }
- if (vq->num_buffers + *nbuffers < 3)
- *nbuffers = 3 - vq->num_buffers;
- if (*nplanes)
- return sizes[0] < skel->format.sizeimage ? -EINVAL : 0;
- *nplanes = 1;
- sizes[0] = skel->format.sizeimage;
- return 0;
- }
- static int buffer_prepare(struct vb2_buffer *vb)
- {
- struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
- unsigned long size = skel->format.sizeimage;
- if (vb2_plane_size(vb, 0) < size) {
- dev_err(&skel->pdev->dev, "buffer too small (%lu < %lu)\n",
- vb2_plane_size(vb, 0), size);
- return -EINVAL;
- }
- vb2_set_plane_payload(vb, 0, size);
- return 0;
- }
- static void buffer_queue(struct vb2_buffer *vb)
- {
- struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
- struct skel_buffer *buf = to_skel_buffer(vb);
- unsigned long flags;
- spin_lock_irqsave(&skel->qlock, flags);
- list_add_tail(&buf->list, &skel->buf_list);
-
- spin_unlock_irqrestore(&skel->qlock, flags);
- }
- static void return_all_buffers(struct skeleton *skel,
- enum vb2_buffer_state state)
- {
- struct skel_buffer *buf, *node;
- unsigned long flags;
- spin_lock_irqsave(&skel->qlock, flags);
- list_for_each_entry_safe(buf, node, &skel->buf_list, list) {
- vb2_buffer_done(&buf->vb, state);
- list_del(&buf->list);
- }
- spin_unlock_irqrestore(&skel->qlock, flags);
- }
- static int start_streaming(struct vb2_queue *vq, unsigned int count)
- {
- struct skeleton *skel = vb2_get_drv_priv(vq);
- int ret = 0;
- skel->sequence = 0;
-
- if (ret) {
-
- return_all_buffers(skel, VB2_BUF_STATE_QUEUED);
- }
- return ret;
- }
- static void stop_streaming(struct vb2_queue *vq)
- {
- struct skeleton *skel = vb2_get_drv_priv(vq);
-
-
- return_all_buffers(skel, VB2_BUF_STATE_ERROR);
- }
- static struct vb2_ops skel_qops = {
- .queue_setup = queue_setup,
- .buf_prepare = buffer_prepare,
- .buf_queue = buffer_queue,
- .start_streaming = start_streaming,
- .stop_streaming = stop_streaming,
- .wait_prepare = vb2_ops_wait_prepare,
- .wait_finish = vb2_ops_wait_finish,
- };
- static int skeleton_querycap(struct file *file, void *priv,
- struct v4l2_capability *cap)
- {
- struct skeleton *skel = video_drvdata(file);
- strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
- strlcpy(cap->card, "V4L2 PCI Skeleton", sizeof(cap->card));
- snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
- pci_name(skel->pdev));
- return 0;
- }
- static void skeleton_fill_pix_format(struct skeleton *skel,
- struct v4l2_pix_format *pix)
- {
- pix->pixelformat = V4L2_PIX_FMT_YUYV;
- if (skel->input == 0) {
-
- pix->width = 720;
- pix->height = (skel->std & V4L2_STD_525_60) ? 480 : 576;
- pix->field = V4L2_FIELD_INTERLACED;
- pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
- } else {
-
- pix->width = skel->timings.bt.width;
- pix->height = skel->timings.bt.height;
- if (skel->timings.bt.interlaced) {
- pix->field = V4L2_FIELD_ALTERNATE;
- pix->height /= 2;
- } else {
- pix->field = V4L2_FIELD_NONE;
- }
- pix->colorspace = V4L2_COLORSPACE_REC709;
- }
-
- pix->bytesperline = pix->width * 2;
- pix->sizeimage = pix->bytesperline * pix->height;
- pix->priv = 0;
- }
- static int skeleton_try_fmt_vid_cap(struct file *file, void *priv,
- struct v4l2_format *f)
- {
- struct skeleton *skel = video_drvdata(file);
- struct v4l2_pix_format *pix = &f->fmt.pix;
-
- if (pix->pixelformat != V4L2_PIX_FMT_YUYV)
- return -EINVAL;
- skeleton_fill_pix_format(skel, pix);
- return 0;
- }
- static int skeleton_s_fmt_vid_cap(struct file *file, void *priv,
- struct v4l2_format *f)
- {
- struct skeleton *skel = video_drvdata(file);
- int ret;
- ret = skeleton_try_fmt_vid_cap(file, priv, f);
- if (ret)
- return ret;
-
- if (vb2_is_busy(&skel->queue))
- return -EBUSY;
-
- skel->format = f->fmt.pix;
- return 0;
- }
- static int skeleton_g_fmt_vid_cap(struct file *file, void *priv,
- struct v4l2_format *f)
- {
- struct skeleton *skel = video_drvdata(file);
- f->fmt.pix = skel->format;
- return 0;
- }
- static int skeleton_enum_fmt_vid_cap(struct file *file, void *priv,
- struct v4l2_fmtdesc *f)
- {
- if (f->index != 0)
- return -EINVAL;
- f->pixelformat = V4L2_PIX_FMT_YUYV;
- return 0;
- }
- static int skeleton_s_std(struct file *file, void *priv, v4l2_std_id std)
- {
- struct skeleton *skel = video_drvdata(file);
-
- if (skel->input)
- return -ENODATA;
-
- if (std == skel->std)
- return 0;
-
- if (vb2_is_busy(&skel->queue))
- return -EBUSY;
-
- skel->std = std;
-
- skeleton_fill_pix_format(skel, &skel->format);
- return 0;
- }
- static int skeleton_g_std(struct file *file, void *priv, v4l2_std_id *std)
- {
- struct skeleton *skel = video_drvdata(file);
-
- if (skel->input)
- return -ENODATA;
- *std = skel->std;
- return 0;
- }
- static int skeleton_querystd(struct file *file, void *priv, v4l2_std_id *std)
- {
- struct skeleton *skel = video_drvdata(file);
-
- if (skel->input)
- return -ENODATA;
- #ifdef TODO
-
- get_signal_info();
- if (no_signal) {
- *std = 0;
- return 0;
- }
-
- if (signal_has_525_lines)
- *std &= V4L2_STD_525_60;
- else
- *std &= V4L2_STD_625_50;
- #endif
- return 0;
- }
- static int skeleton_s_dv_timings(struct file *file, void *_fh,
- struct v4l2_dv_timings *timings)
- {
- struct skeleton *skel = video_drvdata(file);
-
- if (skel->input == 0)
- return -ENODATA;
-
- if (!v4l2_valid_dv_timings(timings, &skel_timings_cap, NULL, NULL))
- return -EINVAL;
-
- if (!v4l2_find_dv_timings_cap(timings, &skel_timings_cap,
- 0, NULL, NULL))
- return -EINVAL;
-
- if (v4l2_match_dv_timings(timings, &skel->timings, 0, false))
- return 0;
-
- if (vb2_is_busy(&skel->queue))
- return -EBUSY;
-
-
- skel->timings = *timings;
-
- skeleton_fill_pix_format(skel, &skel->format);
- return 0;
- }
- static int skeleton_g_dv_timings(struct file *file, void *_fh,
- struct v4l2_dv_timings *timings)
- {
- struct skeleton *skel = video_drvdata(file);
-
- if (skel->input == 0)
- return -ENODATA;
- *timings = skel->timings;
- return 0;
- }
- static int skeleton_enum_dv_timings(struct file *file, void *_fh,
- struct v4l2_enum_dv_timings *timings)
- {
- struct skeleton *skel = video_drvdata(file);
-
- if (skel->input == 0)
- return -ENODATA;
- return v4l2_enum_dv_timings_cap(timings, &skel_timings_cap,
- NULL, NULL);
- }
- static int skeleton_query_dv_timings(struct file *file, void *_fh,
- struct v4l2_dv_timings *timings)
- {
- struct skeleton *skel = video_drvdata(file);
-
- if (skel->input == 0)
- return -ENODATA;
- #ifdef TODO
-
- detect_timings();
- if (no_signal)
- return -ENOLINK;
- if (cannot_lock_to_signal)
- return -ENOLCK;
- if (signal_out_of_range_of_capabilities)
- return -ERANGE;
-
- v4l2_print_dv_timings(skel->v4l2_dev.name, "query_dv_timings:",
- timings, true);
- #endif
- return 0;
- }
- static int skeleton_dv_timings_cap(struct file *file, void *fh,
- struct v4l2_dv_timings_cap *cap)
- {
- struct skeleton *skel = video_drvdata(file);
-
- if (skel->input == 0)
- return -ENODATA;
- *cap = skel_timings_cap;
- return 0;
- }
- static int skeleton_enum_input(struct file *file, void *priv,
- struct v4l2_input *i)
- {
- if (i->index > 1)
- return -EINVAL;
- i->type = V4L2_INPUT_TYPE_CAMERA;
- if (i->index == 0) {
- i->std = SKEL_TVNORMS;
- strlcpy(i->name, "S-Video", sizeof(i->name));
- i->capabilities = V4L2_IN_CAP_STD;
- } else {
- i->std = 0;
- strlcpy(i->name, "HDMI", sizeof(i->name));
- i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
- }
- return 0;
- }
- static int skeleton_s_input(struct file *file, void *priv, unsigned int i)
- {
- struct skeleton *skel = video_drvdata(file);
- if (i > 1)
- return -EINVAL;
-
- if (vb2_is_busy(&skel->queue))
- return -EBUSY;
- skel->input = i;
-
- skel->vdev.tvnorms = i ? 0 : SKEL_TVNORMS;
-
- skeleton_fill_pix_format(skel, &skel->format);
- return 0;
- }
- static int skeleton_g_input(struct file *file, void *priv, unsigned int *i)
- {
- struct skeleton *skel = video_drvdata(file);
- *i = skel->input;
- return 0;
- }
- static int skeleton_s_ctrl(struct v4l2_ctrl *ctrl)
- {
-
- switch (ctrl->id) {
- case V4L2_CID_BRIGHTNESS:
-
- break;
- case V4L2_CID_CONTRAST:
-
- break;
- case V4L2_CID_SATURATION:
-
- break;
- case V4L2_CID_HUE:
-
- break;
- default:
- return -EINVAL;
- }
- return 0;
- }
- static const struct v4l2_ctrl_ops skel_ctrl_ops = {
- .s_ctrl = skeleton_s_ctrl,
- };
- static const struct v4l2_ioctl_ops skel_ioctl_ops = {
- .vidioc_querycap = skeleton_querycap,
- .vidioc_try_fmt_vid_cap = skeleton_try_fmt_vid_cap,
- .vidioc_s_fmt_vid_cap = skeleton_s_fmt_vid_cap,
- .vidioc_g_fmt_vid_cap = skeleton_g_fmt_vid_cap,
- .vidioc_enum_fmt_vid_cap = skeleton_enum_fmt_vid_cap,
- .vidioc_g_std = skeleton_g_std,
- .vidioc_s_std = skeleton_s_std,
- .vidioc_querystd = skeleton_querystd,
- .vidioc_s_dv_timings = skeleton_s_dv_timings,
- .vidioc_g_dv_timings = skeleton_g_dv_timings,
- .vidioc_enum_dv_timings = skeleton_enum_dv_timings,
- .vidioc_query_dv_timings = skeleton_query_dv_timings,
- .vidioc_dv_timings_cap = skeleton_dv_timings_cap,
- .vidioc_enum_input = skeleton_enum_input,
- .vidioc_g_input = skeleton_g_input,
- .vidioc_s_input = skeleton_s_input,
- .vidioc_reqbufs = vb2_ioctl_reqbufs,
- .vidioc_create_bufs = vb2_ioctl_create_bufs,
- .vidioc_querybuf = vb2_ioctl_querybuf,
- .vidioc_qbuf = vb2_ioctl_qbuf,
- .vidioc_dqbuf = vb2_ioctl_dqbuf,
- .vidioc_expbuf = vb2_ioctl_expbuf,
- .vidioc_streamon = vb2_ioctl_streamon,
- .vidioc_streamoff = vb2_ioctl_streamoff,
- .vidioc_log_status = v4l2_ctrl_log_status,
- .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
- .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
- };
- static const struct v4l2_file_operations skel_fops = {
- .owner = THIS_MODULE,
- .open = v4l2_fh_open,
- .release = vb2_fop_release,
- .unlocked_ioctl = video_ioctl2,
- .read = vb2_fop_read,
- .mmap = vb2_fop_mmap,
- .poll = vb2_fop_poll,
- };
- static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
- {
-
- static const struct v4l2_dv_timings timings_def =
- V4L2_DV_BT_CEA_1280X720P60;
- struct skeleton *skel;
- struct video_device *vdev;
- struct v4l2_ctrl_handler *hdl;
- struct vb2_queue *q;
- int ret;
-
- ret = pci_enable_device(pdev);
- if (ret)
- return ret;
- ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
- if (ret) {
- dev_err(&pdev->dev, "no suitable DMA available.\n");
- goto disable_pci;
- }
-
- skel = devm_kzalloc(&pdev->dev, sizeof(struct skeleton), GFP_KERNEL);
- if (!skel)
- return -ENOMEM;
-
- ret = devm_request_irq(&pdev->dev, pdev->irq,
- skeleton_irq, 0, KBUILD_MODNAME, skel);
- if (ret) {
- dev_err(&pdev->dev, "request_irq failed\n");
- goto disable_pci;
- }
- skel->pdev = pdev;
-
- skel->timings = timings_def;
- skel->std = V4L2_STD_625_50;
- skeleton_fill_pix_format(skel, &skel->format);
-
- ret = v4l2_device_register(&pdev->dev, &skel->v4l2_dev);
- if (ret)
- goto disable_pci;
- mutex_init(&skel->lock);
-
- hdl = &skel->ctrl_handler;
- v4l2_ctrl_handler_init(hdl, 4);
- v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
- V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
- v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
- V4L2_CID_CONTRAST, 0, 255, 1, 16);
- v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
- V4L2_CID_SATURATION, 0, 255, 1, 127);
- v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
- V4L2_CID_HUE, -128, 127, 1, 0);
- if (hdl->error) {
- ret = hdl->error;
- goto free_hdl;
- }
- skel->v4l2_dev.ctrl_handler = hdl;
-
- q = &skel->queue;
- q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
- q->dev = &pdev->dev;
- q->drv_priv = skel;
- q->buf_struct_size = sizeof(struct skel_buffer);
- q->ops = &skel_qops;
- q->mem_ops = &vb2_dma_contig_memops;
- q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
-
- q->min_buffers_needed = 2;
-
- q->lock = &skel->lock;
-
- q->gfp_flags = GFP_DMA32;
- ret = vb2_queue_init(q);
- if (ret)
- goto free_hdl;
- INIT_LIST_HEAD(&skel->buf_list);
- spin_lock_init(&skel->qlock);
-
- vdev = &skel->vdev;
- strlcpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
-
- vdev->release = video_device_release_empty;
- vdev->fops = &skel_fops,
- vdev->ioctl_ops = &skel_ioctl_ops,
- vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
- V4L2_CAP_STREAMING;
-
- vdev->lock = &skel->lock;
- vdev->queue = q;
- vdev->v4l2_dev = &skel->v4l2_dev;
-
- vdev->tvnorms = SKEL_TVNORMS;
- video_set_drvdata(vdev, skel);
- ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
- if (ret)
- goto free_hdl;
- dev_info(&pdev->dev, "V4L2 PCI Skeleton Driver loaded\n");
- return 0;
- free_hdl:
- v4l2_ctrl_handler_free(&skel->ctrl_handler);
- v4l2_device_unregister(&skel->v4l2_dev);
- disable_pci:
- pci_disable_device(pdev);
- return ret;
- }
- static void skeleton_remove(struct pci_dev *pdev)
- {
- struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
- struct skeleton *skel = container_of(v4l2_dev, struct skeleton, v4l2_dev);
- video_unregister_device(&skel->vdev);
- v4l2_ctrl_handler_free(&skel->ctrl_handler);
- v4l2_device_unregister(&skel->v4l2_dev);
- pci_disable_device(skel->pdev);
- }
- static struct pci_driver skeleton_driver = {
- .name = KBUILD_MODNAME,
- .probe = skeleton_probe,
- .remove = skeleton_remove,
- .id_table = skeleton_pci_tbl,
- };
- module_pci_driver(skeleton_driver);
|