Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This is a V4L2 PCI Skeleton Driver. It gives an initial skeleton source
0004  * for use with other PCI drivers.
0005  *
0006  * This skeleton PCI driver assumes that the card has an S-Video connector as
0007  * input 0 and an HDMI connector as input 1.
0008  *
0009  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
0010  */
0011 
0012 #include <linux/types.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/kmod.h>
0017 #include <linux/mutex.h>
0018 #include <linux/pci.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/videodev2.h>
0021 #include <linux/v4l2-dv-timings.h>
0022 #include <media/v4l2-device.h>
0023 #include <media/v4l2-dev.h>
0024 #include <media/v4l2-ioctl.h>
0025 #include <media/v4l2-dv-timings.h>
0026 #include <media/v4l2-ctrls.h>
0027 #include <media/v4l2-event.h>
0028 #include <media/videobuf2-v4l2.h>
0029 #include <media/videobuf2-dma-contig.h>
0030 
0031 MODULE_DESCRIPTION("V4L2 PCI Skeleton Driver");
0032 MODULE_AUTHOR("Hans Verkuil");
0033 MODULE_LICENSE("GPL v2");
0034 
0035 /**
0036  * struct skeleton - All internal data for one instance of device
0037  * @pdev: PCI device
0038  * @v4l2_dev: top-level v4l2 device struct
0039  * @vdev: video node structure
0040  * @ctrl_handler: control handler structure
0041  * @lock: ioctl serialization mutex
0042  * @std: current SDTV standard
0043  * @timings: current HDTV timings
0044  * @format: current pix format
0045  * @input: current video input (0 = SDTV, 1 = HDTV)
0046  * @queue: vb2 video capture queue
0047  * @qlock: spinlock controlling access to buf_list and sequence
0048  * @buf_list: list of buffers queued for DMA
0049  * @field: the field (TOP/BOTTOM/other) of the current buffer
0050  * @sequence: frame sequence counter
0051  */
0052 struct skeleton {
0053     struct pci_dev *pdev;
0054     struct v4l2_device v4l2_dev;
0055     struct video_device vdev;
0056     struct v4l2_ctrl_handler ctrl_handler;
0057     struct mutex lock;
0058     v4l2_std_id std;
0059     struct v4l2_dv_timings timings;
0060     struct v4l2_pix_format format;
0061     unsigned input;
0062 
0063     struct vb2_queue queue;
0064 
0065     spinlock_t qlock;
0066     struct list_head buf_list;
0067     unsigned field;
0068     unsigned sequence;
0069 };
0070 
0071 struct skel_buffer {
0072     struct vb2_v4l2_buffer vb;
0073     struct list_head list;
0074 };
0075 
0076 static inline struct skel_buffer *to_skel_buffer(struct vb2_v4l2_buffer *vbuf)
0077 {
0078     return container_of(vbuf, struct skel_buffer, vb);
0079 }
0080 
0081 static const struct pci_device_id skeleton_pci_tbl[] = {
0082     /* { PCI_DEVICE(PCI_VENDOR_ID_, PCI_DEVICE_ID_) }, */
0083     { 0, }
0084 };
0085 MODULE_DEVICE_TABLE(pci, skeleton_pci_tbl);
0086 
0087 /*
0088  * HDTV: this structure has the capabilities of the HDTV receiver.
0089  * It is used to constrain the huge list of possible formats based
0090  * upon the hardware capabilities.
0091  */
0092 static const struct v4l2_dv_timings_cap skel_timings_cap = {
0093     .type = V4L2_DV_BT_656_1120,
0094     /* keep this initialization for compatibility with GCC < 4.4.6 */
0095     .reserved = { 0 },
0096     V4L2_INIT_BT_TIMINGS(
0097         720, 1920,      /* min/max width */
0098         480, 1080,      /* min/max height */
0099         27000000, 74250000, /* min/max pixelclock*/
0100         V4L2_DV_BT_STD_CEA861,  /* Supported standards */
0101         /* capabilities */
0102         V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE
0103     )
0104 };
0105 
0106 /*
0107  * Supported SDTV standards. This does the same job as skel_timings_cap, but
0108  * for standard TV formats.
0109  */
0110 #define SKEL_TVNORMS V4L2_STD_ALL
0111 
0112 /*
0113  * Interrupt handler: typically interrupts happen after a new frame has been
0114  * captured. It is the job of the handler to remove the new frame from the
0115  * internal list and give it back to the vb2 framework, updating the sequence
0116  * counter, field and timestamp at the same time.
0117  */
0118 static irqreturn_t skeleton_irq(int irq, void *dev_id)
0119 {
0120 #ifdef TODO
0121     struct skeleton *skel = dev_id;
0122 
0123     /* handle interrupt */
0124 
0125     /* Once a new frame has been captured, mark it as done like this: */
0126     if (captured_new_frame) {
0127         ...
0128         spin_lock(&skel->qlock);
0129         list_del(&new_buf->list);
0130         spin_unlock(&skel->qlock);
0131         new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
0132         new_buf->vb.sequence = skel->sequence++;
0133         new_buf->vb.field = skel->field;
0134         if (skel->format.field == V4L2_FIELD_ALTERNATE) {
0135             if (skel->field == V4L2_FIELD_BOTTOM)
0136                 skel->field = V4L2_FIELD_TOP;
0137             else if (skel->field == V4L2_FIELD_TOP)
0138                 skel->field = V4L2_FIELD_BOTTOM;
0139         }
0140         vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
0141     }
0142 #endif
0143     return IRQ_HANDLED;
0144 }
0145 
0146 /*
0147  * Setup the constraints of the queue: besides setting the number of planes
0148  * per buffer and the size and allocation context of each plane, it also
0149  * checks if sufficient buffers have been allocated. Usually 3 is a good
0150  * minimum number: many DMA engines need a minimum of 2 buffers in the
0151  * queue and you need to have another available for userspace processing.
0152  */
0153 static int queue_setup(struct vb2_queue *vq,
0154                unsigned int *nbuffers, unsigned int *nplanes,
0155                unsigned int sizes[], struct device *alloc_devs[])
0156 {
0157     struct skeleton *skel = vb2_get_drv_priv(vq);
0158 
0159     skel->field = skel->format.field;
0160     if (skel->field == V4L2_FIELD_ALTERNATE) {
0161         /*
0162          * You cannot use read() with FIELD_ALTERNATE since the field
0163          * information (TOP/BOTTOM) cannot be passed back to the user.
0164          */
0165         if (vb2_fileio_is_active(vq))
0166             return -EINVAL;
0167         skel->field = V4L2_FIELD_TOP;
0168     }
0169 
0170     if (vq->num_buffers + *nbuffers < 3)
0171         *nbuffers = 3 - vq->num_buffers;
0172 
0173     if (*nplanes)
0174         return sizes[0] < skel->format.sizeimage ? -EINVAL : 0;
0175     *nplanes = 1;
0176     sizes[0] = skel->format.sizeimage;
0177     return 0;
0178 }
0179 
0180 /*
0181  * Prepare the buffer for queueing to the DMA engine: check and set the
0182  * payload size.
0183  */
0184 static int buffer_prepare(struct vb2_buffer *vb)
0185 {
0186     struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
0187     unsigned long size = skel->format.sizeimage;
0188 
0189     if (vb2_plane_size(vb, 0) < size) {
0190         dev_err(&skel->pdev->dev, "buffer too small (%lu < %lu)\n",
0191              vb2_plane_size(vb, 0), size);
0192         return -EINVAL;
0193     }
0194 
0195     vb2_set_plane_payload(vb, 0, size);
0196     return 0;
0197 }
0198 
0199 /*
0200  * Queue this buffer to the DMA engine.
0201  */
0202 static void buffer_queue(struct vb2_buffer *vb)
0203 {
0204     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0205     struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
0206     struct skel_buffer *buf = to_skel_buffer(vbuf);
0207     unsigned long flags;
0208 
0209     spin_lock_irqsave(&skel->qlock, flags);
0210     list_add_tail(&buf->list, &skel->buf_list);
0211 
0212     /* TODO: Update any DMA pointers if necessary */
0213 
0214     spin_unlock_irqrestore(&skel->qlock, flags);
0215 }
0216 
0217 static void return_all_buffers(struct skeleton *skel,
0218                    enum vb2_buffer_state state)
0219 {
0220     struct skel_buffer *buf, *node;
0221     unsigned long flags;
0222 
0223     spin_lock_irqsave(&skel->qlock, flags);
0224     list_for_each_entry_safe(buf, node, &skel->buf_list, list) {
0225         vb2_buffer_done(&buf->vb.vb2_buf, state);
0226         list_del(&buf->list);
0227     }
0228     spin_unlock_irqrestore(&skel->qlock, flags);
0229 }
0230 
0231 /*
0232  * Start streaming. First check if the minimum number of buffers have been
0233  * queued. If not, then return -ENOBUFS and the vb2 framework will call
0234  * this function again the next time a buffer has been queued until enough
0235  * buffers are available to actually start the DMA engine.
0236  */
0237 static int start_streaming(struct vb2_queue *vq, unsigned int count)
0238 {
0239     struct skeleton *skel = vb2_get_drv_priv(vq);
0240     int ret = 0;
0241 
0242     skel->sequence = 0;
0243 
0244     /* TODO: start DMA */
0245 
0246     if (ret) {
0247         /*
0248          * In case of an error, return all active buffers to the
0249          * QUEUED state
0250          */
0251         return_all_buffers(skel, VB2_BUF_STATE_QUEUED);
0252     }
0253     return ret;
0254 }
0255 
0256 /*
0257  * Stop the DMA engine. Any remaining buffers in the DMA queue are dequeued
0258  * and passed on to the vb2 framework marked as STATE_ERROR.
0259  */
0260 static void stop_streaming(struct vb2_queue *vq)
0261 {
0262     struct skeleton *skel = vb2_get_drv_priv(vq);
0263 
0264     /* TODO: stop DMA */
0265 
0266     /* Release all active buffers */
0267     return_all_buffers(skel, VB2_BUF_STATE_ERROR);
0268 }
0269 
0270 /*
0271  * The vb2 queue ops. Note that since q->lock is set we can use the standard
0272  * vb2_ops_wait_prepare/finish helper functions. If q->lock would be NULL,
0273  * then this driver would have to provide these ops.
0274  */
0275 static const struct vb2_ops skel_qops = {
0276     .queue_setup        = queue_setup,
0277     .buf_prepare        = buffer_prepare,
0278     .buf_queue      = buffer_queue,
0279     .start_streaming    = start_streaming,
0280     .stop_streaming     = stop_streaming,
0281     .wait_prepare       = vb2_ops_wait_prepare,
0282     .wait_finish        = vb2_ops_wait_finish,
0283 };
0284 
0285 /*
0286  * Required ioctl querycap. Note that the version field is prefilled with
0287  * the version of the kernel.
0288  */
0289 static int skeleton_querycap(struct file *file, void *priv,
0290                  struct v4l2_capability *cap)
0291 {
0292     struct skeleton *skel = video_drvdata(file);
0293 
0294     strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
0295     strlcpy(cap->card, "V4L2 PCI Skeleton", sizeof(cap->card));
0296     snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
0297          pci_name(skel->pdev));
0298     return 0;
0299 }
0300 
0301 /*
0302  * Helper function to check and correct struct v4l2_pix_format. It's used
0303  * not only in VIDIOC_TRY/S_FMT, but also elsewhere if changes to the SDTV
0304  * standard, HDTV timings or the video input would require updating the
0305  * current format.
0306  */
0307 static void skeleton_fill_pix_format(struct skeleton *skel,
0308                      struct v4l2_pix_format *pix)
0309 {
0310     pix->pixelformat = V4L2_PIX_FMT_YUYV;
0311     if (skel->input == 0) {
0312         /* S-Video input */
0313         pix->width = 720;
0314         pix->height = (skel->std & V4L2_STD_525_60) ? 480 : 576;
0315         pix->field = V4L2_FIELD_INTERLACED;
0316         pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
0317     } else {
0318         /* HDMI input */
0319         pix->width = skel->timings.bt.width;
0320         pix->height = skel->timings.bt.height;
0321         if (skel->timings.bt.interlaced) {
0322             pix->field = V4L2_FIELD_ALTERNATE;
0323             pix->height /= 2;
0324         } else {
0325             pix->field = V4L2_FIELD_NONE;
0326         }
0327         pix->colorspace = V4L2_COLORSPACE_REC709;
0328     }
0329 
0330     /*
0331      * The YUYV format is four bytes for every two pixels, so bytesperline
0332      * is width * 2.
0333      */
0334     pix->bytesperline = pix->width * 2;
0335     pix->sizeimage = pix->bytesperline * pix->height;
0336     pix->priv = 0;
0337 }
0338 
0339 static int skeleton_try_fmt_vid_cap(struct file *file, void *priv,
0340                     struct v4l2_format *f)
0341 {
0342     struct skeleton *skel = video_drvdata(file);
0343     struct v4l2_pix_format *pix = &f->fmt.pix;
0344 
0345     /*
0346      * Due to historical reasons providing try_fmt with an unsupported
0347      * pixelformat will return -EINVAL for video receivers. Webcam drivers,
0348      * however, will silently correct the pixelformat. Some video capture
0349      * applications rely on this behavior...
0350      */
0351     if (pix->pixelformat != V4L2_PIX_FMT_YUYV)
0352         return -EINVAL;
0353     skeleton_fill_pix_format(skel, pix);
0354     return 0;
0355 }
0356 
0357 static int skeleton_s_fmt_vid_cap(struct file *file, void *priv,
0358                   struct v4l2_format *f)
0359 {
0360     struct skeleton *skel = video_drvdata(file);
0361     int ret;
0362 
0363     ret = skeleton_try_fmt_vid_cap(file, priv, f);
0364     if (ret)
0365         return ret;
0366 
0367     /*
0368      * It is not allowed to change the format while buffers for use with
0369      * streaming have already been allocated.
0370      */
0371     if (vb2_is_busy(&skel->queue))
0372         return -EBUSY;
0373 
0374     /* TODO: change format */
0375     skel->format = f->fmt.pix;
0376     return 0;
0377 }
0378 
0379 static int skeleton_g_fmt_vid_cap(struct file *file, void *priv,
0380                   struct v4l2_format *f)
0381 {
0382     struct skeleton *skel = video_drvdata(file);
0383 
0384     f->fmt.pix = skel->format;
0385     return 0;
0386 }
0387 
0388 static int skeleton_enum_fmt_vid_cap(struct file *file, void *priv,
0389                      struct v4l2_fmtdesc *f)
0390 {
0391     if (f->index != 0)
0392         return -EINVAL;
0393 
0394     f->pixelformat = V4L2_PIX_FMT_YUYV;
0395     return 0;
0396 }
0397 
0398 static int skeleton_s_std(struct file *file, void *priv, v4l2_std_id std)
0399 {
0400     struct skeleton *skel = video_drvdata(file);
0401 
0402     /* S_STD is not supported on the HDMI input */
0403     if (skel->input)
0404         return -ENODATA;
0405 
0406     /*
0407      * No change, so just return. Some applications call S_STD again after
0408      * the buffers for streaming have been set up, so we have to allow for
0409      * this behavior.
0410      */
0411     if (std == skel->std)
0412         return 0;
0413 
0414     /*
0415      * Changing the standard implies a format change, which is not allowed
0416      * while buffers for use with streaming have already been allocated.
0417      */
0418     if (vb2_is_busy(&skel->queue))
0419         return -EBUSY;
0420 
0421     /* TODO: handle changing std */
0422 
0423     skel->std = std;
0424 
0425     /* Update the internal format */
0426     skeleton_fill_pix_format(skel, &skel->format);
0427     return 0;
0428 }
0429 
0430 static int skeleton_g_std(struct file *file, void *priv, v4l2_std_id *std)
0431 {
0432     struct skeleton *skel = video_drvdata(file);
0433 
0434     /* G_STD is not supported on the HDMI input */
0435     if (skel->input)
0436         return -ENODATA;
0437 
0438     *std = skel->std;
0439     return 0;
0440 }
0441 
0442 /*
0443  * Query the current standard as seen by the hardware. This function shall
0444  * never actually change the standard, it just detects and reports.
0445  * The framework will initially set *std to tvnorms (i.e. the set of
0446  * supported standards by this input), and this function should just AND
0447  * this value. If there is no signal, then *std should be set to 0.
0448  */
0449 static int skeleton_querystd(struct file *file, void *priv, v4l2_std_id *std)
0450 {
0451     struct skeleton *skel = video_drvdata(file);
0452 
0453     /* QUERY_STD is not supported on the HDMI input */
0454     if (skel->input)
0455         return -ENODATA;
0456 
0457 #ifdef TODO
0458     /*
0459      * Query currently seen standard. Initial value of *std is
0460      * V4L2_STD_ALL. This function should look something like this:
0461      */
0462     get_signal_info();
0463     if (no_signal) {
0464         *std = 0;
0465         return 0;
0466     }
0467     /* Use signal information to reduce the number of possible standards */
0468     if (signal_has_525_lines)
0469         *std &= V4L2_STD_525_60;
0470     else
0471         *std &= V4L2_STD_625_50;
0472 #endif
0473     return 0;
0474 }
0475 
0476 static int skeleton_s_dv_timings(struct file *file, void *_fh,
0477                  struct v4l2_dv_timings *timings)
0478 {
0479     struct skeleton *skel = video_drvdata(file);
0480 
0481     /* S_DV_TIMINGS is not supported on the S-Video input */
0482     if (skel->input == 0)
0483         return -ENODATA;
0484 
0485     /* Quick sanity check */
0486     if (!v4l2_valid_dv_timings(timings, &skel_timings_cap, NULL, NULL))
0487         return -EINVAL;
0488 
0489     /* Check if the timings are part of the CEA-861 timings. */
0490     if (!v4l2_find_dv_timings_cap(timings, &skel_timings_cap,
0491                       0, NULL, NULL))
0492         return -EINVAL;
0493 
0494     /* Return 0 if the new timings are the same as the current timings. */
0495     if (v4l2_match_dv_timings(timings, &skel->timings, 0, false))
0496         return 0;
0497 
0498     /*
0499      * Changing the timings implies a format change, which is not allowed
0500      * while buffers for use with streaming have already been allocated.
0501      */
0502     if (vb2_is_busy(&skel->queue))
0503         return -EBUSY;
0504 
0505     /* TODO: Configure new timings */
0506 
0507     /* Save timings */
0508     skel->timings = *timings;
0509 
0510     /* Update the internal format */
0511     skeleton_fill_pix_format(skel, &skel->format);
0512     return 0;
0513 }
0514 
0515 static int skeleton_g_dv_timings(struct file *file, void *_fh,
0516                  struct v4l2_dv_timings *timings)
0517 {
0518     struct skeleton *skel = video_drvdata(file);
0519 
0520     /* G_DV_TIMINGS is not supported on the S-Video input */
0521     if (skel->input == 0)
0522         return -ENODATA;
0523 
0524     *timings = skel->timings;
0525     return 0;
0526 }
0527 
0528 static int skeleton_enum_dv_timings(struct file *file, void *_fh,
0529                     struct v4l2_enum_dv_timings *timings)
0530 {
0531     struct skeleton *skel = video_drvdata(file);
0532 
0533     /* ENUM_DV_TIMINGS is not supported on the S-Video input */
0534     if (skel->input == 0)
0535         return -ENODATA;
0536 
0537     return v4l2_enum_dv_timings_cap(timings, &skel_timings_cap,
0538                     NULL, NULL);
0539 }
0540 
0541 /*
0542  * Query the current timings as seen by the hardware. This function shall
0543  * never actually change the timings, it just detects and reports.
0544  * If no signal is detected, then return -ENOLINK. If the hardware cannot
0545  * lock to the signal, then return -ENOLCK. If the signal is out of range
0546  * of the capabilities of the system (e.g., it is possible that the receiver
0547  * can lock but that the DMA engine it is connected to cannot handle
0548  * pixelclocks above a certain frequency), then -ERANGE is returned.
0549  */
0550 static int skeleton_query_dv_timings(struct file *file, void *_fh,
0551                      struct v4l2_dv_timings *timings)
0552 {
0553     struct skeleton *skel = video_drvdata(file);
0554 
0555     /* QUERY_DV_TIMINGS is not supported on the S-Video input */
0556     if (skel->input == 0)
0557         return -ENODATA;
0558 
0559 #ifdef TODO
0560     /*
0561      * Query currently seen timings. This function should look
0562      * something like this:
0563      */
0564     detect_timings();
0565     if (no_signal)
0566         return -ENOLINK;
0567     if (cannot_lock_to_signal)
0568         return -ENOLCK;
0569     if (signal_out_of_range_of_capabilities)
0570         return -ERANGE;
0571 
0572     /* Useful for debugging */
0573     v4l2_print_dv_timings(skel->v4l2_dev.name, "query_dv_timings:",
0574             timings, true);
0575 #endif
0576     return 0;
0577 }
0578 
0579 static int skeleton_dv_timings_cap(struct file *file, void *fh,
0580                    struct v4l2_dv_timings_cap *cap)
0581 {
0582     struct skeleton *skel = video_drvdata(file);
0583 
0584     /* DV_TIMINGS_CAP is not supported on the S-Video input */
0585     if (skel->input == 0)
0586         return -ENODATA;
0587     *cap = skel_timings_cap;
0588     return 0;
0589 }
0590 
0591 static int skeleton_enum_input(struct file *file, void *priv,
0592                    struct v4l2_input *i)
0593 {
0594     if (i->index > 1)
0595         return -EINVAL;
0596 
0597     i->type = V4L2_INPUT_TYPE_CAMERA;
0598     if (i->index == 0) {
0599         i->std = SKEL_TVNORMS;
0600         strlcpy(i->name, "S-Video", sizeof(i->name));
0601         i->capabilities = V4L2_IN_CAP_STD;
0602     } else {
0603         i->std = 0;
0604         strlcpy(i->name, "HDMI", sizeof(i->name));
0605         i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
0606     }
0607     return 0;
0608 }
0609 
0610 static int skeleton_s_input(struct file *file, void *priv, unsigned int i)
0611 {
0612     struct skeleton *skel = video_drvdata(file);
0613 
0614     if (i > 1)
0615         return -EINVAL;
0616 
0617     /*
0618      * Changing the input implies a format change, which is not allowed
0619      * while buffers for use with streaming have already been allocated.
0620      */
0621     if (vb2_is_busy(&skel->queue))
0622         return -EBUSY;
0623 
0624     skel->input = i;
0625     /*
0626      * Update tvnorms. The tvnorms value is used by the core to implement
0627      * VIDIOC_ENUMSTD so it has to be correct. If tvnorms == 0, then
0628      * ENUMSTD will return -ENODATA.
0629      */
0630     skel->vdev.tvnorms = i ? 0 : SKEL_TVNORMS;
0631 
0632     /* Update the internal format */
0633     skeleton_fill_pix_format(skel, &skel->format);
0634     return 0;
0635 }
0636 
0637 static int skeleton_g_input(struct file *file, void *priv, unsigned int *i)
0638 {
0639     struct skeleton *skel = video_drvdata(file);
0640 
0641     *i = skel->input;
0642     return 0;
0643 }
0644 
0645 /* The control handler. */
0646 static int skeleton_s_ctrl(struct v4l2_ctrl *ctrl)
0647 {
0648     /*struct skeleton *skel =
0649         container_of(ctrl->handler, struct skeleton, ctrl_handler);*/
0650 
0651     switch (ctrl->id) {
0652     case V4L2_CID_BRIGHTNESS:
0653         /* TODO: set brightness to ctrl->val */
0654         break;
0655     case V4L2_CID_CONTRAST:
0656         /* TODO: set contrast to ctrl->val */
0657         break;
0658     case V4L2_CID_SATURATION:
0659         /* TODO: set saturation to ctrl->val */
0660         break;
0661     case V4L2_CID_HUE:
0662         /* TODO: set hue to ctrl->val */
0663         break;
0664     default:
0665         return -EINVAL;
0666     }
0667     return 0;
0668 }
0669 
0670 /* ------------------------------------------------------------------
0671     File operations for the device
0672    ------------------------------------------------------------------*/
0673 
0674 static const struct v4l2_ctrl_ops skel_ctrl_ops = {
0675     .s_ctrl = skeleton_s_ctrl,
0676 };
0677 
0678 /*
0679  * The set of all supported ioctls. Note that all the streaming ioctls
0680  * use the vb2 helper functions that take care of all the locking and
0681  * that also do ownership tracking (i.e. only the filehandle that requested
0682  * the buffers can call the streaming ioctls, all other filehandles will
0683  * receive -EBUSY if they attempt to call the same streaming ioctls).
0684  *
0685  * The last three ioctls also use standard helper functions: these implement
0686  * standard behavior for drivers with controls.
0687  */
0688 static const struct v4l2_ioctl_ops skel_ioctl_ops = {
0689     .vidioc_querycap = skeleton_querycap,
0690     .vidioc_try_fmt_vid_cap = skeleton_try_fmt_vid_cap,
0691     .vidioc_s_fmt_vid_cap = skeleton_s_fmt_vid_cap,
0692     .vidioc_g_fmt_vid_cap = skeleton_g_fmt_vid_cap,
0693     .vidioc_enum_fmt_vid_cap = skeleton_enum_fmt_vid_cap,
0694 
0695     .vidioc_g_std = skeleton_g_std,
0696     .vidioc_s_std = skeleton_s_std,
0697     .vidioc_querystd = skeleton_querystd,
0698 
0699     .vidioc_s_dv_timings = skeleton_s_dv_timings,
0700     .vidioc_g_dv_timings = skeleton_g_dv_timings,
0701     .vidioc_enum_dv_timings = skeleton_enum_dv_timings,
0702     .vidioc_query_dv_timings = skeleton_query_dv_timings,
0703     .vidioc_dv_timings_cap = skeleton_dv_timings_cap,
0704 
0705     .vidioc_enum_input = skeleton_enum_input,
0706     .vidioc_g_input = skeleton_g_input,
0707     .vidioc_s_input = skeleton_s_input,
0708 
0709     .vidioc_reqbufs = vb2_ioctl_reqbufs,
0710     .vidioc_create_bufs = vb2_ioctl_create_bufs,
0711     .vidioc_querybuf = vb2_ioctl_querybuf,
0712     .vidioc_qbuf = vb2_ioctl_qbuf,
0713     .vidioc_dqbuf = vb2_ioctl_dqbuf,
0714     .vidioc_expbuf = vb2_ioctl_expbuf,
0715     .vidioc_streamon = vb2_ioctl_streamon,
0716     .vidioc_streamoff = vb2_ioctl_streamoff,
0717 
0718     .vidioc_log_status = v4l2_ctrl_log_status,
0719     .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
0720     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
0721 };
0722 
0723 /*
0724  * The set of file operations. Note that all these ops are standard core
0725  * helper functions.
0726  */
0727 static const struct v4l2_file_operations skel_fops = {
0728     .owner = THIS_MODULE,
0729     .open = v4l2_fh_open,
0730     .release = vb2_fop_release,
0731     .unlocked_ioctl = video_ioctl2,
0732     .read = vb2_fop_read,
0733     .mmap = vb2_fop_mmap,
0734     .poll = vb2_fop_poll,
0735 };
0736 
0737 /*
0738  * The initial setup of this device instance. Note that the initial state of
0739  * the driver should be complete. So the initial format, standard, timings
0740  * and video input should all be initialized to some reasonable value.
0741  */
0742 static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
0743 {
0744     /* The initial timings are chosen to be 720p60. */
0745     static const struct v4l2_dv_timings timings_def =
0746         V4L2_DV_BT_CEA_1280X720P60;
0747     struct skeleton *skel;
0748     struct video_device *vdev;
0749     struct v4l2_ctrl_handler *hdl;
0750     struct vb2_queue *q;
0751     int ret;
0752 
0753     /* Enable PCI */
0754     ret = pci_enable_device(pdev);
0755     if (ret)
0756         return ret;
0757     ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
0758     if (ret) {
0759         dev_err(&pdev->dev, "no suitable DMA available.\n");
0760         goto disable_pci;
0761     }
0762 
0763     /* Allocate a new instance */
0764     skel = devm_kzalloc(&pdev->dev, sizeof(struct skeleton), GFP_KERNEL);
0765     if (!skel) {
0766         ret = -ENOMEM;
0767         goto disable_pci;
0768     }
0769 
0770     /* Allocate the interrupt */
0771     ret = devm_request_irq(&pdev->dev, pdev->irq,
0772                    skeleton_irq, 0, KBUILD_MODNAME, skel);
0773     if (ret) {
0774         dev_err(&pdev->dev, "request_irq failed\n");
0775         goto disable_pci;
0776     }
0777     skel->pdev = pdev;
0778 
0779     /* Fill in the initial format-related settings */
0780     skel->timings = timings_def;
0781     skel->std = V4L2_STD_625_50;
0782     skeleton_fill_pix_format(skel, &skel->format);
0783 
0784     /* Initialize the top-level structure */
0785     ret = v4l2_device_register(&pdev->dev, &skel->v4l2_dev);
0786     if (ret)
0787         goto disable_pci;
0788 
0789     mutex_init(&skel->lock);
0790 
0791     /* Add the controls */
0792     hdl = &skel->ctrl_handler;
0793     v4l2_ctrl_handler_init(hdl, 4);
0794     v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
0795               V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
0796     v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
0797               V4L2_CID_CONTRAST, 0, 255, 1, 16);
0798     v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
0799               V4L2_CID_SATURATION, 0, 255, 1, 127);
0800     v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
0801               V4L2_CID_HUE, -128, 127, 1, 0);
0802     if (hdl->error) {
0803         ret = hdl->error;
0804         goto free_hdl;
0805     }
0806     skel->v4l2_dev.ctrl_handler = hdl;
0807 
0808     /* Initialize the vb2 queue */
0809     q = &skel->queue;
0810     q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
0811     q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
0812     q->dev = &pdev->dev;
0813     q->drv_priv = skel;
0814     q->buf_struct_size = sizeof(struct skel_buffer);
0815     q->ops = &skel_qops;
0816     q->mem_ops = &vb2_dma_contig_memops;
0817     q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
0818     /*
0819      * Assume that this DMA engine needs to have at least two buffers
0820      * available before it can be started. The start_streaming() op
0821      * won't be called until at least this many buffers are queued up.
0822      */
0823     q->min_buffers_needed = 2;
0824     /*
0825      * The serialization lock for the streaming ioctls. This is the same
0826      * as the main serialization lock, but if some of the non-streaming
0827      * ioctls could take a long time to execute, then you might want to
0828      * have a different lock here to prevent VIDIOC_DQBUF from being
0829      * blocked while waiting for another action to finish. This is
0830      * generally not needed for PCI devices, but USB devices usually do
0831      * want a separate lock here.
0832      */
0833     q->lock = &skel->lock;
0834     /*
0835      * Since this driver can only do 32-bit DMA we must make sure that
0836      * the vb2 core will allocate the buffers in 32-bit DMA memory.
0837      */
0838     q->gfp_flags = GFP_DMA32;
0839     ret = vb2_queue_init(q);
0840     if (ret)
0841         goto free_hdl;
0842 
0843     INIT_LIST_HEAD(&skel->buf_list);
0844     spin_lock_init(&skel->qlock);
0845 
0846     /* Initialize the video_device structure */
0847     vdev = &skel->vdev;
0848     strlcpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
0849     /*
0850      * There is nothing to clean up, so release is set to an empty release
0851      * function. The release callback must be non-NULL.
0852      */
0853     vdev->release = video_device_release_empty;
0854     vdev->fops = &skel_fops,
0855     vdev->ioctl_ops = &skel_ioctl_ops,
0856     vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
0857                 V4L2_CAP_STREAMING;
0858     /*
0859      * The main serialization lock. All ioctls are serialized by this
0860      * lock. Exception: if q->lock is set, then the streaming ioctls
0861      * are serialized by that separate lock.
0862      */
0863     vdev->lock = &skel->lock;
0864     vdev->queue = q;
0865     vdev->v4l2_dev = &skel->v4l2_dev;
0866     /* Supported SDTV standards, if any */
0867     vdev->tvnorms = SKEL_TVNORMS;
0868     video_set_drvdata(vdev, skel);
0869 
0870     ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
0871     if (ret)
0872         goto free_hdl;
0873 
0874     dev_info(&pdev->dev, "V4L2 PCI Skeleton Driver loaded\n");
0875     return 0;
0876 
0877 free_hdl:
0878     v4l2_ctrl_handler_free(&skel->ctrl_handler);
0879     v4l2_device_unregister(&skel->v4l2_dev);
0880 disable_pci:
0881     pci_disable_device(pdev);
0882     return ret;
0883 }
0884 
0885 static void skeleton_remove(struct pci_dev *pdev)
0886 {
0887     struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
0888     struct skeleton *skel = container_of(v4l2_dev, struct skeleton, v4l2_dev);
0889 
0890     video_unregister_device(&skel->vdev);
0891     v4l2_ctrl_handler_free(&skel->ctrl_handler);
0892     v4l2_device_unregister(&skel->v4l2_dev);
0893     pci_disable_device(skel->pdev);
0894 }
0895 
0896 static struct pci_driver skeleton_driver = {
0897     .name = KBUILD_MODNAME,
0898     .probe = skeleton_probe,
0899     .remove = skeleton_remove,
0900     .id_table = skeleton_pci_tbl,
0901 };
0902 
0903 module_pci_driver(skeleton_driver);