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