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