]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - Documentation/video4linux/v4l2-pci-skeleton.c
Merge branch 'jdelvare-hwmon/master'
[karo-tx-linux.git] / Documentation / video4linux / v4l2-pci-skeleton.c
1 /*
2  * This is a V4L2 PCI Skeleton Driver. It gives an initial skeleton source
3  * for use with other PCI drivers.
4  *
5  * This skeleton PCI driver assumes that the card has an S-Video connector as
6  * input 0 and an HDMI connector as input 1.
7  *
8  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
9  *
10  * This program is free software; you may redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; version 2 of the License.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/kmod.h>
29 #include <linux/mutex.h>
30 #include <linux/pci.h>
31 #include <linux/interrupt.h>
32 #include <linux/videodev2.h>
33 #include <linux/v4l2-dv-timings.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-dev.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-dv-timings.h>
38 #include <media/v4l2-ctrls.h>
39 #include <media/v4l2-event.h>
40 #include <media/videobuf2-dma-contig.h>
41
42 MODULE_DESCRIPTION("V4L2 PCI Skeleton Driver");
43 MODULE_AUTHOR("Hans Verkuil");
44 MODULE_LICENSE("GPL v2");
45
46 /**
47  * struct skeleton - All internal data for one instance of device
48  * @pdev: PCI device
49  * @v4l2_dev: top-level v4l2 device struct
50  * @vdev: video node structure
51  * @ctrl_handler: control handler structure
52  * @lock: ioctl serialization mutex
53  * @std: current SDTV standard
54  * @timings: current HDTV timings
55  * @format: current pix format
56  * @input: current video input (0 = SDTV, 1 = HDTV)
57  * @queue: vb2 video capture queue
58  * @alloc_ctx: vb2 contiguous DMA context
59  * @qlock: spinlock controlling access to buf_list and sequence
60  * @buf_list: list of buffers queued for DMA
61  * @sequence: frame sequence counter
62  */
63 struct skeleton {
64         struct pci_dev *pdev;
65         struct v4l2_device v4l2_dev;
66         struct video_device vdev;
67         struct v4l2_ctrl_handler ctrl_handler;
68         struct mutex lock;
69         v4l2_std_id std;
70         struct v4l2_dv_timings timings;
71         struct v4l2_pix_format format;
72         unsigned input;
73
74         struct vb2_queue queue;
75         struct vb2_alloc_ctx *alloc_ctx;
76
77         spinlock_t qlock;
78         struct list_head buf_list;
79         unsigned field;
80         unsigned sequence;
81 };
82
83 struct skel_buffer {
84         struct vb2_buffer vb;
85         struct list_head list;
86 };
87
88 static inline struct skel_buffer *to_skel_buffer(struct vb2_buffer *vb2)
89 {
90         return container_of(vb2, struct skel_buffer, vb);
91 }
92
93 static const struct pci_device_id skeleton_pci_tbl[] = {
94         /* { PCI_DEVICE(PCI_VENDOR_ID_, PCI_DEVICE_ID_) }, */
95         { 0, }
96 };
97 MODULE_DEVICE_TABLE(pci, skeleton_pci_tbl);
98
99 /*
100  * HDTV: this structure has the capabilities of the HDTV receiver.
101  * It is used to constrain the huge list of possible formats based
102  * upon the hardware capabilities.
103  */
104 static const struct v4l2_dv_timings_cap skel_timings_cap = {
105         .type = V4L2_DV_BT_656_1120,
106         /* keep this initialization for compatibility with GCC < 4.4.6 */
107         .reserved = { 0 },
108         V4L2_INIT_BT_TIMINGS(
109                 720, 1920,              /* min/max width */
110                 480, 1080,              /* min/max height */
111                 27000000, 74250000,     /* min/max pixelclock*/
112                 V4L2_DV_BT_STD_CEA861,  /* Supported standards */
113                 /* capabilities */
114                 V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE
115         )
116 };
117
118 /*
119  * Supported SDTV standards. This does the same job as skel_timings_cap, but
120  * for standard TV formats.
121  */
122 #define SKEL_TVNORMS V4L2_STD_ALL
123
124 /*
125  * Interrupt handler: typically interrupts happen after a new frame has been
126  * captured. It is the job of the handler to remove the new frame from the
127  * internal list and give it back to the vb2 framework, updating the sequence
128  * counter, field and timestamp at the same time.
129  */
130 static irqreturn_t skeleton_irq(int irq, void *dev_id)
131 {
132 #ifdef TODO
133         struct skeleton *skel = dev_id;
134
135         /* handle interrupt */
136
137         /* Once a new frame has been captured, mark it as done like this: */
138         if (captured_new_frame) {
139                 ...
140                 spin_lock(&skel->qlock);
141                 list_del(&new_buf->list);
142                 spin_unlock(&skel->qlock);
143                 v4l2_get_timestamp(&new_buf->vb.v4l2_buf.timestamp);
144                 new_buf->vb.v4l2_buf.sequence = skel->sequence++;
145                 new_buf->vb.v4l2_buf.field = skel->field;
146                 if (skel->format.field == V4L2_FIELD_ALTERNATE) {
147                         if (skel->field == V4L2_FIELD_BOTTOM)
148                                 skel->field = V4L2_FIELD_TOP;
149                         else if (skel->field == V4L2_FIELD_TOP)
150                                 skel->field = V4L2_FIELD_BOTTOM;
151                 }
152                 vb2_buffer_done(&new_buf->vb, VB2_BUF_STATE_DONE);
153         }
154 #endif
155         return IRQ_HANDLED;
156 }
157
158 /*
159  * Setup the constraints of the queue: besides setting the number of planes
160  * per buffer and the size and allocation context of each plane, it also
161  * checks if sufficient buffers have been allocated. Usually 3 is a good
162  * minimum number: many DMA engines need a minimum of 2 buffers in the
163  * queue and you need to have another available for userspace processing.
164  */
165 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
166                        unsigned int *nbuffers, unsigned int *nplanes,
167                        unsigned int sizes[], void *alloc_ctxs[])
168 {
169         struct skeleton *skel = vb2_get_drv_priv(vq);
170
171         skel->field = skel->format.field;
172         if (skel->field == V4L2_FIELD_ALTERNATE) {
173                 /*
174                  * You cannot use read() with FIELD_ALTERNATE since the field
175                  * information (TOP/BOTTOM) cannot be passed back to the user.
176                  */
177                 if (vb2_fileio_is_active(vq))
178                         return -EINVAL;
179                 skel->field = V4L2_FIELD_TOP;
180         }
181
182         if (vq->num_buffers + *nbuffers < 3)
183                 *nbuffers = 3 - vq->num_buffers;
184
185         if (fmt && fmt->fmt.pix.sizeimage < skel->format.sizeimage)
186                 return -EINVAL;
187         *nplanes = 1;
188         sizes[0] = fmt ? fmt->fmt.pix.sizeimage : skel->format.sizeimage;
189         alloc_ctxs[0] = skel->alloc_ctx;
190         return 0;
191 }
192
193 /*
194  * Prepare the buffer for queueing to the DMA engine: check and set the
195  * payload size.
196  */
197 static int buffer_prepare(struct vb2_buffer *vb)
198 {
199         struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
200         unsigned long size = skel->format.sizeimage;
201
202         if (vb2_plane_size(vb, 0) < size) {
203                 dev_err(&skel->pdev->dev, "buffer too small (%lu < %lu)\n",
204                          vb2_plane_size(vb, 0), size);
205                 return -EINVAL;
206         }
207
208         vb2_set_plane_payload(vb, 0, size);
209         return 0;
210 }
211
212 /*
213  * Queue this buffer to the DMA engine.
214  */
215 static void buffer_queue(struct vb2_buffer *vb)
216 {
217         struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
218         struct skel_buffer *buf = to_skel_buffer(vb);
219         unsigned long flags;
220
221         spin_lock_irqsave(&skel->qlock, flags);
222         list_add_tail(&buf->list, &skel->buf_list);
223
224         /* TODO: Update any DMA pointers if necessary */
225
226         spin_unlock_irqrestore(&skel->qlock, flags);
227 }
228
229 static void return_all_buffers(struct skeleton *skel,
230                                enum vb2_buffer_state state)
231 {
232         struct skel_buffer *buf, *node;
233         unsigned long flags;
234
235         spin_lock_irqsave(&skel->qlock, flags);
236         list_for_each_entry_safe(buf, node, &skel->buf_list, list) {
237                 vb2_buffer_done(&buf->vb, state);
238                 list_del(&buf->list);
239         }
240         spin_unlock_irqrestore(&skel->qlock, flags);
241 }
242
243 /*
244  * Start streaming. First check if the minimum number of buffers have been
245  * queued. If not, then return -ENOBUFS and the vb2 framework will call
246  * this function again the next time a buffer has been queued until enough
247  * buffers are available to actually start the DMA engine.
248  */
249 static int start_streaming(struct vb2_queue *vq, unsigned int count)
250 {
251         struct skeleton *skel = vb2_get_drv_priv(vq);
252         int ret = 0;
253
254         skel->sequence = 0;
255
256         /* TODO: start DMA */
257
258         if (ret) {
259                 /*
260                  * In case of an error, return all active buffers to the
261                  * QUEUED state
262                  */
263                 return_all_buffers(skel, VB2_BUF_STATE_QUEUED);
264         }
265         return ret;
266 }
267
268 /*
269  * Stop the DMA engine. Any remaining buffers in the DMA queue are dequeued
270  * and passed on to the vb2 framework marked as STATE_ERROR.
271  */
272 static void stop_streaming(struct vb2_queue *vq)
273 {
274         struct skeleton *skel = vb2_get_drv_priv(vq);
275
276         /* TODO: stop DMA */
277
278         /* Release all active buffers */
279         return_all_buffers(skel, VB2_BUF_STATE_ERROR);
280 }
281
282 /*
283  * The vb2 queue ops. Note that since q->lock is set we can use the standard
284  * vb2_ops_wait_prepare/finish helper functions. If q->lock would be NULL,
285  * then this driver would have to provide these ops.
286  */
287 static struct vb2_ops skel_qops = {
288         .queue_setup            = queue_setup,
289         .buf_prepare            = buffer_prepare,
290         .buf_queue              = buffer_queue,
291         .start_streaming        = start_streaming,
292         .stop_streaming         = stop_streaming,
293         .wait_prepare           = vb2_ops_wait_prepare,
294         .wait_finish            = vb2_ops_wait_finish,
295 };
296
297 /*
298  * Required ioctl querycap. Note that the version field is prefilled with
299  * the version of the kernel.
300  */
301 static int skeleton_querycap(struct file *file, void *priv,
302                              struct v4l2_capability *cap)
303 {
304         struct skeleton *skel = video_drvdata(file);
305
306         strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
307         strlcpy(cap->card, "V4L2 PCI Skeleton", sizeof(cap->card));
308         snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
309                  pci_name(skel->pdev));
310         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
311                            V4L2_CAP_STREAMING;
312         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
313         return 0;
314 }
315
316 /*
317  * Helper function to check and correct struct v4l2_pix_format. It's used
318  * not only in VIDIOC_TRY/S_FMT, but also elsewhere if changes to the SDTV
319  * standard, HDTV timings or the video input would require updating the
320  * current format.
321  */
322 static void skeleton_fill_pix_format(struct skeleton *skel,
323                                      struct v4l2_pix_format *pix)
324 {
325         pix->pixelformat = V4L2_PIX_FMT_YUYV;
326         if (skel->input == 0) {
327                 /* S-Video input */
328                 pix->width = 720;
329                 pix->height = (skel->std & V4L2_STD_525_60) ? 480 : 576;
330                 pix->field = V4L2_FIELD_INTERLACED;
331                 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
332         } else {
333                 /* HDMI input */
334                 pix->width = skel->timings.bt.width;
335                 pix->height = skel->timings.bt.height;
336                 if (skel->timings.bt.interlaced) {
337                         pix->field = V4L2_FIELD_ALTERNATE;
338                         pix->height /= 2;
339                 } else {
340                         pix->field = V4L2_FIELD_NONE;
341                 }
342                 pix->colorspace = V4L2_COLORSPACE_REC709;
343         }
344
345         /*
346          * The YUYV format is four bytes for every two pixels, so bytesperline
347          * is width * 2.
348          */
349         pix->bytesperline = pix->width * 2;
350         pix->sizeimage = pix->bytesperline * pix->height;
351         pix->priv = 0;
352 }
353
354 static int skeleton_try_fmt_vid_cap(struct file *file, void *priv,
355                                     struct v4l2_format *f)
356 {
357         struct skeleton *skel = video_drvdata(file);
358         struct v4l2_pix_format *pix = &f->fmt.pix;
359
360         /*
361          * Due to historical reasons providing try_fmt with an unsupported
362          * pixelformat will return -EINVAL for video receivers. Webcam drivers,
363          * however, will silently correct the pixelformat. Some video capture
364          * applications rely on this behavior...
365          */
366         if (pix->pixelformat != V4L2_PIX_FMT_YUYV)
367                 return -EINVAL;
368         skeleton_fill_pix_format(skel, pix);
369         return 0;
370 }
371
372 static int skeleton_s_fmt_vid_cap(struct file *file, void *priv,
373                                   struct v4l2_format *f)
374 {
375         struct skeleton *skel = video_drvdata(file);
376         int ret;
377
378         ret = skeleton_try_fmt_vid_cap(file, priv, f);
379         if (ret)
380                 return ret;
381
382         /*
383          * It is not allowed to change the format while buffers for use with
384          * streaming have already been allocated.
385          */
386         if (vb2_is_busy(&skel->queue))
387                 return -EBUSY;
388
389         /* TODO: change format */
390         skel->format = f->fmt.pix;
391         return 0;
392 }
393
394 static int skeleton_g_fmt_vid_cap(struct file *file, void *priv,
395                                   struct v4l2_format *f)
396 {
397         struct skeleton *skel = video_drvdata(file);
398
399         f->fmt.pix = skel->format;
400         return 0;
401 }
402
403 static int skeleton_enum_fmt_vid_cap(struct file *file, void *priv,
404                                      struct v4l2_fmtdesc *f)
405 {
406         if (f->index != 0)
407                 return -EINVAL;
408
409         f->pixelformat = V4L2_PIX_FMT_YUYV;
410         return 0;
411 }
412
413 static int skeleton_s_std(struct file *file, void *priv, v4l2_std_id std)
414 {
415         struct skeleton *skel = video_drvdata(file);
416
417         /* S_STD is not supported on the HDMI input */
418         if (skel->input)
419                 return -ENODATA;
420
421         /*
422          * No change, so just return. Some applications call S_STD again after
423          * the buffers for streaming have been set up, so we have to allow for
424          * this behavior.
425          */
426         if (std == skel->std)
427                 return 0;
428
429         /*
430          * Changing the standard implies a format change, which is not allowed
431          * while buffers for use with streaming have already been allocated.
432          */
433         if (vb2_is_busy(&skel->queue))
434                 return -EBUSY;
435
436         /* TODO: handle changing std */
437
438         skel->std = std;
439
440         /* Update the internal format */
441         skeleton_fill_pix_format(skel, &skel->format);
442         return 0;
443 }
444
445 static int skeleton_g_std(struct file *file, void *priv, v4l2_std_id *std)
446 {
447         struct skeleton *skel = video_drvdata(file);
448
449         /* G_STD is not supported on the HDMI input */
450         if (skel->input)
451                 return -ENODATA;
452
453         *std = skel->std;
454         return 0;
455 }
456
457 /*
458  * Query the current standard as seen by the hardware. This function shall
459  * never actually change the standard, it just detects and reports.
460  * The framework will initially set *std to tvnorms (i.e. the set of
461  * supported standards by this input), and this function should just AND
462  * this value. If there is no signal, then *std should be set to 0.
463  */
464 static int skeleton_querystd(struct file *file, void *priv, v4l2_std_id *std)
465 {
466         struct skeleton *skel = video_drvdata(file);
467
468         /* QUERY_STD is not supported on the HDMI input */
469         if (skel->input)
470                 return -ENODATA;
471
472 #ifdef TODO
473         /*
474          * Query currently seen standard. Initial value of *std is
475          * V4L2_STD_ALL. This function should look something like this:
476          */
477         get_signal_info();
478         if (no_signal) {
479                 *std = 0;
480                 return 0;
481         }
482         /* Use signal information to reduce the number of possible standards */
483         if (signal_has_525_lines)
484                 *std &= V4L2_STD_525_60;
485         else
486                 *std &= V4L2_STD_625_50;
487 #endif
488         return 0;
489 }
490
491 static int skeleton_s_dv_timings(struct file *file, void *_fh,
492                                  struct v4l2_dv_timings *timings)
493 {
494         struct skeleton *skel = video_drvdata(file);
495
496         /* S_DV_TIMINGS is not supported on the S-Video input */
497         if (skel->input == 0)
498                 return -ENODATA;
499
500         /* Quick sanity check */
501         if (!v4l2_valid_dv_timings(timings, &skel_timings_cap, NULL, NULL))
502                 return -EINVAL;
503
504         /* Check if the timings are part of the CEA-861 timings. */
505         if (!v4l2_find_dv_timings_cap(timings, &skel_timings_cap,
506                                       0, NULL, NULL))
507                 return -EINVAL;
508
509         /* Return 0 if the new timings are the same as the current timings. */
510         if (v4l2_match_dv_timings(timings, &skel->timings, 0))
511                 return 0;
512
513         /*
514          * Changing the timings implies a format change, which is not allowed
515          * while buffers for use with streaming have already been allocated.
516          */
517         if (vb2_is_busy(&skel->queue))
518                 return -EBUSY;
519
520         /* TODO: Configure new timings */
521
522         /* Save timings */
523         skel->timings = *timings;
524
525         /* Update the internal format */
526         skeleton_fill_pix_format(skel, &skel->format);
527         return 0;
528 }
529
530 static int skeleton_g_dv_timings(struct file *file, void *_fh,
531                                  struct v4l2_dv_timings *timings)
532 {
533         struct skeleton *skel = video_drvdata(file);
534
535         /* G_DV_TIMINGS is not supported on the S-Video input */
536         if (skel->input == 0)
537                 return -ENODATA;
538
539         *timings = skel->timings;
540         return 0;
541 }
542
543 static int skeleton_enum_dv_timings(struct file *file, void *_fh,
544                                     struct v4l2_enum_dv_timings *timings)
545 {
546         struct skeleton *skel = video_drvdata(file);
547
548         /* ENUM_DV_TIMINGS is not supported on the S-Video input */
549         if (skel->input == 0)
550                 return -ENODATA;
551
552         return v4l2_enum_dv_timings_cap(timings, &skel_timings_cap,
553                                         NULL, NULL);
554 }
555
556 /*
557  * Query the current timings as seen by the hardware. This function shall
558  * never actually change the timings, it just detects and reports.
559  * If no signal is detected, then return -ENOLINK. If the hardware cannot
560  * lock to the signal, then return -ENOLCK. If the signal is out of range
561  * of the capabilities of the system (e.g., it is possible that the receiver
562  * can lock but that the DMA engine it is connected to cannot handle
563  * pixelclocks above a certain frequency), then -ERANGE is returned.
564  */
565 static int skeleton_query_dv_timings(struct file *file, void *_fh,
566                                      struct v4l2_dv_timings *timings)
567 {
568         struct skeleton *skel = video_drvdata(file);
569
570         /* QUERY_DV_TIMINGS is not supported on the S-Video input */
571         if (skel->input == 0)
572                 return -ENODATA;
573
574 #ifdef TODO
575         /*
576          * Query currently seen timings. This function should look
577          * something like this:
578          */
579         detect_timings();
580         if (no_signal)
581                 return -ENOLINK;
582         if (cannot_lock_to_signal)
583                 return -ENOLCK;
584         if (signal_out_of_range_of_capabilities)
585                 return -ERANGE;
586
587         /* Useful for debugging */
588         v4l2_print_dv_timings(skel->v4l2_dev.name, "query_dv_timings:",
589                         timings, true);
590 #endif
591         return 0;
592 }
593
594 static int skeleton_dv_timings_cap(struct file *file, void *fh,
595                                    struct v4l2_dv_timings_cap *cap)
596 {
597         struct skeleton *skel = video_drvdata(file);
598
599         /* DV_TIMINGS_CAP is not supported on the S-Video input */
600         if (skel->input == 0)
601                 return -ENODATA;
602         *cap = skel_timings_cap;
603         return 0;
604 }
605
606 static int skeleton_enum_input(struct file *file, void *priv,
607                                struct v4l2_input *i)
608 {
609         if (i->index > 1)
610                 return -EINVAL;
611
612         i->type = V4L2_INPUT_TYPE_CAMERA;
613         if (i->index == 0) {
614                 i->std = SKEL_TVNORMS;
615                 strlcpy(i->name, "S-Video", sizeof(i->name));
616                 i->capabilities = V4L2_IN_CAP_STD;
617         } else {
618                 i->std = 0;
619                 strlcpy(i->name, "HDMI", sizeof(i->name));
620                 i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
621         }
622         return 0;
623 }
624
625 static int skeleton_s_input(struct file *file, void *priv, unsigned int i)
626 {
627         struct skeleton *skel = video_drvdata(file);
628
629         if (i > 1)
630                 return -EINVAL;
631
632         /*
633          * Changing the input implies a format change, which is not allowed
634          * while buffers for use with streaming have already been allocated.
635          */
636         if (vb2_is_busy(&skel->queue))
637                 return -EBUSY;
638
639         skel->input = i;
640         /*
641          * Update tvnorms. The tvnorms value is used by the core to implement
642          * VIDIOC_ENUMSTD so it has to be correct. If tvnorms == 0, then
643          * ENUMSTD will return -ENODATA.
644          */
645         skel->vdev.tvnorms = i ? 0 : SKEL_TVNORMS;
646
647         /* Update the internal format */
648         skeleton_fill_pix_format(skel, &skel->format);
649         return 0;
650 }
651
652 static int skeleton_g_input(struct file *file, void *priv, unsigned int *i)
653 {
654         struct skeleton *skel = video_drvdata(file);
655
656         *i = skel->input;
657         return 0;
658 }
659
660 /* The control handler. */
661 static int skeleton_s_ctrl(struct v4l2_ctrl *ctrl)
662 {
663         /*struct skeleton *skel =
664                 container_of(ctrl->handler, struct skeleton, ctrl_handler);*/
665
666         switch (ctrl->id) {
667         case V4L2_CID_BRIGHTNESS:
668                 /* TODO: set brightness to ctrl->val */
669                 break;
670         case V4L2_CID_CONTRAST:
671                 /* TODO: set contrast to ctrl->val */
672                 break;
673         case V4L2_CID_SATURATION:
674                 /* TODO: set saturation to ctrl->val */
675                 break;
676         case V4L2_CID_HUE:
677                 /* TODO: set hue to ctrl->val */
678                 break;
679         default:
680                 return -EINVAL;
681         }
682         return 0;
683 }
684
685 /* ------------------------------------------------------------------
686         File operations for the device
687    ------------------------------------------------------------------*/
688
689 static const struct v4l2_ctrl_ops skel_ctrl_ops = {
690         .s_ctrl = skeleton_s_ctrl,
691 };
692
693 /*
694  * The set of all supported ioctls. Note that all the streaming ioctls
695  * use the vb2 helper functions that take care of all the locking and
696  * that also do ownership tracking (i.e. only the filehandle that requested
697  * the buffers can call the streaming ioctls, all other filehandles will
698  * receive -EBUSY if they attempt to call the same streaming ioctls).
699  *
700  * The last three ioctls also use standard helper functions: these implement
701  * standard behavior for drivers with controls.
702  */
703 static const struct v4l2_ioctl_ops skel_ioctl_ops = {
704         .vidioc_querycap = skeleton_querycap,
705         .vidioc_try_fmt_vid_cap = skeleton_try_fmt_vid_cap,
706         .vidioc_s_fmt_vid_cap = skeleton_s_fmt_vid_cap,
707         .vidioc_g_fmt_vid_cap = skeleton_g_fmt_vid_cap,
708         .vidioc_enum_fmt_vid_cap = skeleton_enum_fmt_vid_cap,
709
710         .vidioc_g_std = skeleton_g_std,
711         .vidioc_s_std = skeleton_s_std,
712         .vidioc_querystd = skeleton_querystd,
713
714         .vidioc_s_dv_timings = skeleton_s_dv_timings,
715         .vidioc_g_dv_timings = skeleton_g_dv_timings,
716         .vidioc_enum_dv_timings = skeleton_enum_dv_timings,
717         .vidioc_query_dv_timings = skeleton_query_dv_timings,
718         .vidioc_dv_timings_cap = skeleton_dv_timings_cap,
719
720         .vidioc_enum_input = skeleton_enum_input,
721         .vidioc_g_input = skeleton_g_input,
722         .vidioc_s_input = skeleton_s_input,
723
724         .vidioc_reqbufs = vb2_ioctl_reqbufs,
725         .vidioc_create_bufs = vb2_ioctl_create_bufs,
726         .vidioc_querybuf = vb2_ioctl_querybuf,
727         .vidioc_qbuf = vb2_ioctl_qbuf,
728         .vidioc_dqbuf = vb2_ioctl_dqbuf,
729         .vidioc_expbuf = vb2_ioctl_expbuf,
730         .vidioc_streamon = vb2_ioctl_streamon,
731         .vidioc_streamoff = vb2_ioctl_streamoff,
732
733         .vidioc_log_status = v4l2_ctrl_log_status,
734         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
735         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
736 };
737
738 /*
739  * The set of file operations. Note that all these ops are standard core
740  * helper functions.
741  */
742 static const struct v4l2_file_operations skel_fops = {
743         .owner = THIS_MODULE,
744         .open = v4l2_fh_open,
745         .release = vb2_fop_release,
746         .unlocked_ioctl = video_ioctl2,
747         .read = vb2_fop_read,
748         .mmap = vb2_fop_mmap,
749         .poll = vb2_fop_poll,
750 };
751
752 /*
753  * The initial setup of this device instance. Note that the initial state of
754  * the driver should be complete. So the initial format, standard, timings
755  * and video input should all be initialized to some reasonable value.
756  */
757 static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
758 {
759         /* The initial timings are chosen to be 720p60. */
760         static const struct v4l2_dv_timings timings_def =
761                 V4L2_DV_BT_CEA_1280X720P60;
762         struct skeleton *skel;
763         struct video_device *vdev;
764         struct v4l2_ctrl_handler *hdl;
765         struct vb2_queue *q;
766         int ret;
767
768         /* Enable PCI */
769         ret = pci_enable_device(pdev);
770         if (ret)
771                 return ret;
772         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
773         if (ret) {
774                 dev_err(&pdev->dev, "no suitable DMA available.\n");
775                 goto disable_pci;
776         }
777
778         /* Allocate a new instance */
779         skel = devm_kzalloc(&pdev->dev, sizeof(struct skeleton), GFP_KERNEL);
780         if (!skel)
781                 return -ENOMEM;
782
783         /* Allocate the interrupt */
784         ret = devm_request_irq(&pdev->dev, pdev->irq,
785                                skeleton_irq, 0, KBUILD_MODNAME, skel);
786         if (ret) {
787                 dev_err(&pdev->dev, "request_irq failed\n");
788                 goto disable_pci;
789         }
790         skel->pdev = pdev;
791
792         /* Fill in the initial format-related settings */
793         skel->timings = timings_def;
794         skel->std = V4L2_STD_625_50;
795         skeleton_fill_pix_format(skel, &skel->format);
796
797         /* Initialize the top-level structure */
798         ret = v4l2_device_register(&pdev->dev, &skel->v4l2_dev);
799         if (ret)
800                 goto disable_pci;
801
802         mutex_init(&skel->lock);
803
804         /* Add the controls */
805         hdl = &skel->ctrl_handler;
806         v4l2_ctrl_handler_init(hdl, 4);
807         v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
808                           V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
809         v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
810                           V4L2_CID_CONTRAST, 0, 255, 1, 16);
811         v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
812                           V4L2_CID_SATURATION, 0, 255, 1, 127);
813         v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
814                           V4L2_CID_HUE, -128, 127, 1, 0);
815         if (hdl->error) {
816                 ret = hdl->error;
817                 goto free_hdl;
818         }
819         skel->v4l2_dev.ctrl_handler = hdl;
820
821         /* Initialize the vb2 queue */
822         q = &skel->queue;
823         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
824         q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
825         q->drv_priv = skel;
826         q->buf_struct_size = sizeof(struct skel_buffer);
827         q->ops = &skel_qops;
828         q->mem_ops = &vb2_dma_contig_memops;
829         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
830         /*
831          * Assume that this DMA engine needs to have at least two buffers
832          * available before it can be started. The start_streaming() op
833          * won't be called until at least this many buffers are queued up.
834          */
835         q->min_buffers_needed = 2;
836         /*
837          * The serialization lock for the streaming ioctls. This is the same
838          * as the main serialization lock, but if some of the non-streaming
839          * ioctls could take a long time to execute, then you might want to
840          * have a different lock here to prevent VIDIOC_DQBUF from being
841          * blocked while waiting for another action to finish. This is
842          * generally not needed for PCI devices, but USB devices usually do
843          * want a separate lock here.
844          */
845         q->lock = &skel->lock;
846         /*
847          * Since this driver can only do 32-bit DMA we must make sure that
848          * the vb2 core will allocate the buffers in 32-bit DMA memory.
849          */
850         q->gfp_flags = GFP_DMA32;
851         ret = vb2_queue_init(q);
852         if (ret)
853                 goto free_hdl;
854
855         skel->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
856         if (IS_ERR(skel->alloc_ctx)) {
857                 dev_err(&pdev->dev, "Can't allocate buffer context");
858                 ret = PTR_ERR(skel->alloc_ctx);
859                 goto free_hdl;
860         }
861         INIT_LIST_HEAD(&skel->buf_list);
862         spin_lock_init(&skel->qlock);
863
864         /* Initialize the video_device structure */
865         vdev = &skel->vdev;
866         strlcpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
867         /*
868          * There is nothing to clean up, so release is set to an empty release
869          * function. The release callback must be non-NULL.
870          */
871         vdev->release = video_device_release_empty;
872         vdev->fops = &skel_fops,
873         vdev->ioctl_ops = &skel_ioctl_ops,
874         /*
875          * The main serialization lock. All ioctls are serialized by this
876          * lock. Exception: if q->lock is set, then the streaming ioctls
877          * are serialized by that separate lock.
878          */
879         vdev->lock = &skel->lock;
880         vdev->queue = q;
881         vdev->v4l2_dev = &skel->v4l2_dev;
882         /* Supported SDTV standards, if any */
883         vdev->tvnorms = SKEL_TVNORMS;
884         video_set_drvdata(vdev, skel);
885
886         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
887         if (ret)
888                 goto free_ctx;
889
890         dev_info(&pdev->dev, "V4L2 PCI Skeleton Driver loaded\n");
891         return 0;
892
893 free_ctx:
894         vb2_dma_contig_cleanup_ctx(skel->alloc_ctx);
895 free_hdl:
896         v4l2_ctrl_handler_free(&skel->ctrl_handler);
897         v4l2_device_unregister(&skel->v4l2_dev);
898 disable_pci:
899         pci_disable_device(pdev);
900         return ret;
901 }
902
903 static void skeleton_remove(struct pci_dev *pdev)
904 {
905         struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
906         struct skeleton *skel = container_of(v4l2_dev, struct skeleton, v4l2_dev);
907
908         video_unregister_device(&skel->vdev);
909         v4l2_ctrl_handler_free(&skel->ctrl_handler);
910         vb2_dma_contig_cleanup_ctx(skel->alloc_ctx);
911         v4l2_device_unregister(&skel->v4l2_dev);
912         pci_disable_device(skel->pdev);
913 }
914
915 static struct pci_driver skeleton_driver = {
916         .name = KBUILD_MODNAME,
917         .probe = skeleton_probe,
918         .remove = skeleton_remove,
919         .id_table = skeleton_pci_tbl,
920 };
921
922 module_pci_driver(skeleton_driver);