1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64struct skeleton {
65 struct pci_dev *pdev;
66 struct v4l2_device v4l2_dev;
67 struct video_device vdev;
68 struct v4l2_ctrl_handler ctrl_handler;
69 struct mutex lock;
70 v4l2_std_id std;
71 struct v4l2_dv_timings timings;
72 struct v4l2_pix_format format;
73 unsigned input;
74
75 struct vb2_queue queue;
76 struct vb2_alloc_ctx *alloc_ctx;
77
78 spinlock_t qlock;
79 struct list_head buf_list;
80 unsigned field;
81 unsigned sequence;
82};
83
84struct skel_buffer {
85 struct vb2_buffer vb;
86 struct list_head list;
87};
88
89static inline struct skel_buffer *to_skel_buffer(struct vb2_buffer *vb2)
90{
91 return container_of(vb2, struct skel_buffer, vb);
92}
93
94static const struct pci_device_id skeleton_pci_tbl[] = {
95
96 { 0, }
97};
98MODULE_DEVICE_TABLE(pci, skeleton_pci_tbl);
99
100
101
102
103
104
105static const struct v4l2_dv_timings_cap skel_timings_cap = {
106 .type = V4L2_DV_BT_656_1120,
107
108 .reserved = { 0 },
109 V4L2_INIT_BT_TIMINGS(
110 720, 1920,
111 480, 1080,
112 27000000, 74250000,
113 V4L2_DV_BT_STD_CEA861,
114
115 V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE
116 )
117};
118
119
120
121
122
123#define SKEL_TVNORMS V4L2_STD_ALL
124
125
126
127
128
129
130
131static irqreturn_t skeleton_irq(int irq, void *dev_id)
132{
133#ifdef TODO
134 struct skeleton *skel = dev_id;
135
136
137
138
139 if (captured_new_frame) {
140 ...
141 spin_lock(&skel->qlock);
142 list_del(&new_buf->list);
143 spin_unlock(&skel->qlock);
144 v4l2_get_timestamp(&new_buf->vb.v4l2_buf.timestamp);
145 new_buf->vb.v4l2_buf.sequence = skel->sequence++;
146 new_buf->vb.v4l2_buf.field = skel->field;
147 if (skel->format.field == V4L2_FIELD_ALTERNATE) {
148 if (skel->field == V4L2_FIELD_BOTTOM)
149 skel->field = V4L2_FIELD_TOP;
150 else if (skel->field == V4L2_FIELD_TOP)
151 skel->field = V4L2_FIELD_BOTTOM;
152 }
153 vb2_buffer_done(&new_buf->vb, VB2_BUF_STATE_DONE);
154 }
155#endif
156 return IRQ_HANDLED;
157}
158
159
160
161
162
163
164
165
166static int queue_setup(struct vb2_queue *vq,
167 unsigned int *nbuffers, unsigned int *nplanes,
168 unsigned int sizes[], void *alloc_ctxs[])
169{
170 struct skeleton *skel = vb2_get_drv_priv(vq);
171
172 skel->field = skel->format.field;
173 if (skel->field == V4L2_FIELD_ALTERNATE) {
174
175
176
177
178 if (vb2_fileio_is_active(vq))
179 return -EINVAL;
180 skel->field = V4L2_FIELD_TOP;
181 }
182
183 if (vq->num_buffers + *nbuffers < 3)
184 *nbuffers = 3 - vq->num_buffers;
185 alloc_ctxs[0] = skel->alloc_ctx;
186
187 if (*nplanes)
188 return sizes[0] < skel->format.sizeimage ? -EINVAL : 0;
189 *nplanes = 1;
190 sizes[0] = skel->format.sizeimage;
191 return 0;
192}
193
194
195
196
197
198static int buffer_prepare(struct vb2_buffer *vb)
199{
200 struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
201 unsigned long size = skel->format.sizeimage;
202
203 if (vb2_plane_size(vb, 0) < size) {
204 dev_err(&skel->pdev->dev, "buffer too small (%lu < %lu)\n",
205 vb2_plane_size(vb, 0), size);
206 return -EINVAL;
207 }
208
209 vb2_set_plane_payload(vb, 0, size);
210 return 0;
211}
212
213
214
215
216static void buffer_queue(struct vb2_buffer *vb)
217{
218 struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
219 struct skel_buffer *buf = to_skel_buffer(vb);
220 unsigned long flags;
221
222 spin_lock_irqsave(&skel->qlock, flags);
223 list_add_tail(&buf->list, &skel->buf_list);
224
225
226
227 spin_unlock_irqrestore(&skel->qlock, flags);
228}
229
230static void return_all_buffers(struct skeleton *skel,
231 enum vb2_buffer_state state)
232{
233 struct skel_buffer *buf, *node;
234 unsigned long flags;
235
236 spin_lock_irqsave(&skel->qlock, flags);
237 list_for_each_entry_safe(buf, node, &skel->buf_list, list) {
238 vb2_buffer_done(&buf->vb, state);
239 list_del(&buf->list);
240 }
241 spin_unlock_irqrestore(&skel->qlock, flags);
242}
243
244
245
246
247
248
249
250static int start_streaming(struct vb2_queue *vq, unsigned int count)
251{
252 struct skeleton *skel = vb2_get_drv_priv(vq);
253 int ret = 0;
254
255 skel->sequence = 0;
256
257
258
259 if (ret) {
260
261
262
263
264 return_all_buffers(skel, VB2_BUF_STATE_QUEUED);
265 }
266 return ret;
267}
268
269
270
271
272
273static void stop_streaming(struct vb2_queue *vq)
274{
275 struct skeleton *skel = vb2_get_drv_priv(vq);
276
277
278
279
280 return_all_buffers(skel, VB2_BUF_STATE_ERROR);
281}
282
283
284
285
286
287
288static struct vb2_ops skel_qops = {
289 .queue_setup = queue_setup,
290 .buf_prepare = buffer_prepare,
291 .buf_queue = buffer_queue,
292 .start_streaming = start_streaming,
293 .stop_streaming = stop_streaming,
294 .wait_prepare = vb2_ops_wait_prepare,
295 .wait_finish = vb2_ops_wait_finish,
296};
297
298
299
300
301
302static int skeleton_querycap(struct file *file, void *priv,
303 struct v4l2_capability *cap)
304{
305 struct skeleton *skel = video_drvdata(file);
306
307 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
308 strlcpy(cap->card, "V4L2 PCI Skeleton", sizeof(cap->card));
309 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
310 pci_name(skel->pdev));
311 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
312 V4L2_CAP_STREAMING;
313 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
314 return 0;
315}
316
317
318
319
320
321
322
323static void skeleton_fill_pix_format(struct skeleton *skel,
324 struct v4l2_pix_format *pix)
325{
326 pix->pixelformat = V4L2_PIX_FMT_YUYV;
327 if (skel->input == 0) {
328
329 pix->width = 720;
330 pix->height = (skel->std & V4L2_STD_525_60) ? 480 : 576;
331 pix->field = V4L2_FIELD_INTERLACED;
332 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
333 } else {
334
335 pix->width = skel->timings.bt.width;
336 pix->height = skel->timings.bt.height;
337 if (skel->timings.bt.interlaced) {
338 pix->field = V4L2_FIELD_ALTERNATE;
339 pix->height /= 2;
340 } else {
341 pix->field = V4L2_FIELD_NONE;
342 }
343 pix->colorspace = V4L2_COLORSPACE_REC709;
344 }
345
346
347
348
349
350 pix->bytesperline = pix->width * 2;
351 pix->sizeimage = pix->bytesperline * pix->height;
352 pix->priv = 0;
353}
354
355static int skeleton_try_fmt_vid_cap(struct file *file, void *priv,
356 struct v4l2_format *f)
357{
358 struct skeleton *skel = video_drvdata(file);
359 struct v4l2_pix_format *pix = &f->fmt.pix;
360
361
362
363
364
365
366
367 if (pix->pixelformat != V4L2_PIX_FMT_YUYV)
368 return -EINVAL;
369 skeleton_fill_pix_format(skel, pix);
370 return 0;
371}
372
373static int skeleton_s_fmt_vid_cap(struct file *file, void *priv,
374 struct v4l2_format *f)
375{
376 struct skeleton *skel = video_drvdata(file);
377 int ret;
378
379 ret = skeleton_try_fmt_vid_cap(file, priv, f);
380 if (ret)
381 return ret;
382
383
384
385
386
387 if (vb2_is_busy(&skel->queue))
388 return -EBUSY;
389
390
391 skel->format = f->fmt.pix;
392 return 0;
393}
394
395static int skeleton_g_fmt_vid_cap(struct file *file, void *priv,
396 struct v4l2_format *f)
397{
398 struct skeleton *skel = video_drvdata(file);
399
400 f->fmt.pix = skel->format;
401 return 0;
402}
403
404static int skeleton_enum_fmt_vid_cap(struct file *file, void *priv,
405 struct v4l2_fmtdesc *f)
406{
407 if (f->index != 0)
408 return -EINVAL;
409
410 f->pixelformat = V4L2_PIX_FMT_YUYV;
411 return 0;
412}
413
414static int skeleton_s_std(struct file *file, void *priv, v4l2_std_id std)
415{
416 struct skeleton *skel = video_drvdata(file);
417
418
419 if (skel->input)
420 return -ENODATA;
421
422
423
424
425
426
427 if (std == skel->std)
428 return 0;
429
430
431
432
433
434 if (vb2_is_busy(&skel->queue))
435 return -EBUSY;
436
437
438
439 skel->std = std;
440
441
442 skeleton_fill_pix_format(skel, &skel->format);
443 return 0;
444}
445
446static int skeleton_g_std(struct file *file, void *priv, v4l2_std_id *std)
447{
448 struct skeleton *skel = video_drvdata(file);
449
450
451 if (skel->input)
452 return -ENODATA;
453
454 *std = skel->std;
455 return 0;
456}
457
458
459
460
461
462
463
464
465static int skeleton_querystd(struct file *file, void *priv, v4l2_std_id *std)
466{
467 struct skeleton *skel = video_drvdata(file);
468
469
470 if (skel->input)
471 return -ENODATA;
472
473#ifdef TODO
474
475
476
477
478 get_signal_info();
479 if (no_signal) {
480 *std = 0;
481 return 0;
482 }
483
484 if (signal_has_525_lines)
485 *std &= V4L2_STD_525_60;
486 else
487 *std &= V4L2_STD_625_50;
488#endif
489 return 0;
490}
491
492static int skeleton_s_dv_timings(struct file *file, void *_fh,
493 struct v4l2_dv_timings *timings)
494{
495 struct skeleton *skel = video_drvdata(file);
496
497
498 if (skel->input == 0)
499 return -ENODATA;
500
501
502 if (!v4l2_valid_dv_timings(timings, &skel_timings_cap, NULL, NULL))
503 return -EINVAL;
504
505
506 if (!v4l2_find_dv_timings_cap(timings, &skel_timings_cap,
507 0, NULL, NULL))
508 return -EINVAL;
509
510
511 if (v4l2_match_dv_timings(timings, &skel->timings, 0, false))
512 return 0;
513
514
515
516
517
518 if (vb2_is_busy(&skel->queue))
519 return -EBUSY;
520
521
522
523
524 skel->timings = *timings;
525
526
527 skeleton_fill_pix_format(skel, &skel->format);
528 return 0;
529}
530
531static int skeleton_g_dv_timings(struct file *file, void *_fh,
532 struct v4l2_dv_timings *timings)
533{
534 struct skeleton *skel = video_drvdata(file);
535
536
537 if (skel->input == 0)
538 return -ENODATA;
539
540 *timings = skel->timings;
541 return 0;
542}
543
544static int skeleton_enum_dv_timings(struct file *file, void *_fh,
545 struct v4l2_enum_dv_timings *timings)
546{
547 struct skeleton *skel = video_drvdata(file);
548
549
550 if (skel->input == 0)
551 return -ENODATA;
552
553 return v4l2_enum_dv_timings_cap(timings, &skel_timings_cap,
554 NULL, NULL);
555}
556
557
558
559
560
561
562
563
564
565
566static int skeleton_query_dv_timings(struct file *file, void *_fh,
567 struct v4l2_dv_timings *timings)
568{
569 struct skeleton *skel = video_drvdata(file);
570
571
572 if (skel->input == 0)
573 return -ENODATA;
574
575#ifdef TODO
576
577
578
579
580 detect_timings();
581 if (no_signal)
582 return -ENOLINK;
583 if (cannot_lock_to_signal)
584 return -ENOLCK;
585 if (signal_out_of_range_of_capabilities)
586 return -ERANGE;
587
588
589 v4l2_print_dv_timings(skel->v4l2_dev.name, "query_dv_timings:",
590 timings, true);
591#endif
592 return 0;
593}
594
595static int skeleton_dv_timings_cap(struct file *file, void *fh,
596 struct v4l2_dv_timings_cap *cap)
597{
598 struct skeleton *skel = video_drvdata(file);
599
600
601 if (skel->input == 0)
602 return -ENODATA;
603 *cap = skel_timings_cap;
604 return 0;
605}
606
607static int skeleton_enum_input(struct file *file, void *priv,
608 struct v4l2_input *i)
609{
610 if (i->index > 1)
611 return -EINVAL;
612
613 i->type = V4L2_INPUT_TYPE_CAMERA;
614 if (i->index == 0) {
615 i->std = SKEL_TVNORMS;
616 strlcpy(i->name, "S-Video", sizeof(i->name));
617 i->capabilities = V4L2_IN_CAP_STD;
618 } else {
619 i->std = 0;
620 strlcpy(i->name, "HDMI", sizeof(i->name));
621 i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
622 }
623 return 0;
624}
625
626static int skeleton_s_input(struct file *file, void *priv, unsigned int i)
627{
628 struct skeleton *skel = video_drvdata(file);
629
630 if (i > 1)
631 return -EINVAL;
632
633
634
635
636
637 if (vb2_is_busy(&skel->queue))
638 return -EBUSY;
639
640 skel->input = i;
641
642
643
644
645
646 skel->vdev.tvnorms = i ? 0 : SKEL_TVNORMS;
647
648
649 skeleton_fill_pix_format(skel, &skel->format);
650 return 0;
651}
652
653static int skeleton_g_input(struct file *file, void *priv, unsigned int *i)
654{
655 struct skeleton *skel = video_drvdata(file);
656
657 *i = skel->input;
658 return 0;
659}
660
661
662static int skeleton_s_ctrl(struct v4l2_ctrl *ctrl)
663{
664
665
666
667 switch (ctrl->id) {
668 case V4L2_CID_BRIGHTNESS:
669
670 break;
671 case V4L2_CID_CONTRAST:
672
673 break;
674 case V4L2_CID_SATURATION:
675
676 break;
677 case V4L2_CID_HUE:
678
679 break;
680 default:
681 return -EINVAL;
682 }
683 return 0;
684}
685
686
687
688
689
690static const struct v4l2_ctrl_ops skel_ctrl_ops = {
691 .s_ctrl = skeleton_s_ctrl,
692};
693
694
695
696
697
698
699
700
701
702
703
704static const struct v4l2_ioctl_ops skel_ioctl_ops = {
705 .vidioc_querycap = skeleton_querycap,
706 .vidioc_try_fmt_vid_cap = skeleton_try_fmt_vid_cap,
707 .vidioc_s_fmt_vid_cap = skeleton_s_fmt_vid_cap,
708 .vidioc_g_fmt_vid_cap = skeleton_g_fmt_vid_cap,
709 .vidioc_enum_fmt_vid_cap = skeleton_enum_fmt_vid_cap,
710
711 .vidioc_g_std = skeleton_g_std,
712 .vidioc_s_std = skeleton_s_std,
713 .vidioc_querystd = skeleton_querystd,
714
715 .vidioc_s_dv_timings = skeleton_s_dv_timings,
716 .vidioc_g_dv_timings = skeleton_g_dv_timings,
717 .vidioc_enum_dv_timings = skeleton_enum_dv_timings,
718 .vidioc_query_dv_timings = skeleton_query_dv_timings,
719 .vidioc_dv_timings_cap = skeleton_dv_timings_cap,
720
721 .vidioc_enum_input = skeleton_enum_input,
722 .vidioc_g_input = skeleton_g_input,
723 .vidioc_s_input = skeleton_s_input,
724
725 .vidioc_reqbufs = vb2_ioctl_reqbufs,
726 .vidioc_create_bufs = vb2_ioctl_create_bufs,
727 .vidioc_querybuf = vb2_ioctl_querybuf,
728 .vidioc_qbuf = vb2_ioctl_qbuf,
729 .vidioc_dqbuf = vb2_ioctl_dqbuf,
730 .vidioc_expbuf = vb2_ioctl_expbuf,
731 .vidioc_streamon = vb2_ioctl_streamon,
732 .vidioc_streamoff = vb2_ioctl_streamoff,
733
734 .vidioc_log_status = v4l2_ctrl_log_status,
735 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
736 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
737};
738
739
740
741
742
743static const struct v4l2_file_operations skel_fops = {
744 .owner = THIS_MODULE,
745 .open = v4l2_fh_open,
746 .release = vb2_fop_release,
747 .unlocked_ioctl = video_ioctl2,
748 .read = vb2_fop_read,
749 .mmap = vb2_fop_mmap,
750 .poll = vb2_fop_poll,
751};
752
753
754
755
756
757
758static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
759{
760
761 static const struct v4l2_dv_timings timings_def =
762 V4L2_DV_BT_CEA_1280X720P60;
763 struct skeleton *skel;
764 struct video_device *vdev;
765 struct v4l2_ctrl_handler *hdl;
766 struct vb2_queue *q;
767 int ret;
768
769
770 ret = pci_enable_device(pdev);
771 if (ret)
772 return ret;
773 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
774 if (ret) {
775 dev_err(&pdev->dev, "no suitable DMA available.\n");
776 goto disable_pci;
777 }
778
779
780 skel = devm_kzalloc(&pdev->dev, sizeof(struct skeleton), GFP_KERNEL);
781 if (!skel)
782 return -ENOMEM;
783
784
785 ret = devm_request_irq(&pdev->dev, pdev->irq,
786 skeleton_irq, 0, KBUILD_MODNAME, skel);
787 if (ret) {
788 dev_err(&pdev->dev, "request_irq failed\n");
789 goto disable_pci;
790 }
791 skel->pdev = pdev;
792
793
794 skel->timings = timings_def;
795 skel->std = V4L2_STD_625_50;
796 skeleton_fill_pix_format(skel, &skel->format);
797
798
799 ret = v4l2_device_register(&pdev->dev, &skel->v4l2_dev);
800 if (ret)
801 goto disable_pci;
802
803 mutex_init(&skel->lock);
804
805
806 hdl = &skel->ctrl_handler;
807 v4l2_ctrl_handler_init(hdl, 4);
808 v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
809 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
810 v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
811 V4L2_CID_CONTRAST, 0, 255, 1, 16);
812 v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
813 V4L2_CID_SATURATION, 0, 255, 1, 127);
814 v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
815 V4L2_CID_HUE, -128, 127, 1, 0);
816 if (hdl->error) {
817 ret = hdl->error;
818 goto free_hdl;
819 }
820 skel->v4l2_dev.ctrl_handler = hdl;
821
822
823 q = &skel->queue;
824 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
825 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
826 q->drv_priv = skel;
827 q->buf_struct_size = sizeof(struct skel_buffer);
828 q->ops = &skel_qops;
829 q->mem_ops = &vb2_dma_contig_memops;
830 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
831
832
833
834
835
836 q->min_buffers_needed = 2;
837
838
839
840
841
842
843
844
845
846 q->lock = &skel->lock;
847
848
849
850
851 q->gfp_flags = GFP_DMA32;
852 ret = vb2_queue_init(q);
853 if (ret)
854 goto free_hdl;
855
856 skel->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
857 if (IS_ERR(skel->alloc_ctx)) {
858 dev_err(&pdev->dev, "Can't allocate buffer context");
859 ret = PTR_ERR(skel->alloc_ctx);
860 goto free_hdl;
861 }
862 INIT_LIST_HEAD(&skel->buf_list);
863 spin_lock_init(&skel->qlock);
864
865
866 vdev = &skel->vdev;
867 strlcpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
868
869
870
871
872 vdev->release = video_device_release_empty;
873 vdev->fops = &skel_fops,
874 vdev->ioctl_ops = &skel_ioctl_ops,
875
876
877
878
879
880 vdev->lock = &skel->lock;
881 vdev->queue = q;
882 vdev->v4l2_dev = &skel->v4l2_dev;
883
884 vdev->tvnorms = SKEL_TVNORMS;
885 video_set_drvdata(vdev, skel);
886
887 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
888 if (ret)
889 goto free_ctx;
890
891 dev_info(&pdev->dev, "V4L2 PCI Skeleton Driver loaded\n");
892 return 0;
893
894free_ctx:
895 vb2_dma_contig_cleanup_ctx(skel->alloc_ctx);
896free_hdl:
897 v4l2_ctrl_handler_free(&skel->ctrl_handler);
898 v4l2_device_unregister(&skel->v4l2_dev);
899disable_pci:
900 pci_disable_device(pdev);
901 return ret;
902}
903
904static void skeleton_remove(struct pci_dev *pdev)
905{
906 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
907 struct skeleton *skel = container_of(v4l2_dev, struct skeleton, v4l2_dev);
908
909 video_unregister_device(&skel->vdev);
910 v4l2_ctrl_handler_free(&skel->ctrl_handler);
911 vb2_dma_contig_cleanup_ctx(skel->alloc_ctx);
912 v4l2_device_unregister(&skel->v4l2_dev);
913 pci_disable_device(skel->pdev);
914}
915
916static struct pci_driver skeleton_driver = {
917 .name = KBUILD_MODNAME,
918 .probe = skeleton_probe,
919 .remove = skeleton_remove,
920 .id_table = skeleton_pci_tbl,
921};
922
923module_pci_driver(skeleton_driver);
924