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
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
94 { 0, }
95};
96MODULE_DEVICE_TABLE(pci, skeleton_pci_tbl);
97
98
99
100
101
102
103static const struct v4l2_dv_timings_cap skel_timings_cap = {
104 .type = V4L2_DV_BT_656_1120,
105
106 .reserved = { 0 },
107 V4L2_INIT_BT_TIMINGS(
108 720, 1920,
109 480, 1080,
110 27000000, 74250000,
111 V4L2_DV_BT_STD_CEA861,
112
113 V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE
114 )
115};
116
117
118
119
120
121#define SKEL_TVNORMS V4L2_STD_ALL
122
123
124
125
126
127
128
129static irqreturn_t skeleton_irq(int irq, void *dev_id)
130{
131#ifdef TODO
132 struct skeleton *skel = dev_id;
133
134
135
136
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
159
160
161
162
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
174
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
193
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
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
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
243
244
245
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
255
256 if (ret) {
257
258
259
260
261 return_all_buffers(skel, VB2_BUF_STATE_QUEUED);
262 }
263 return ret;
264}
265
266
267
268
269
270static void stop_streaming(struct vb2_queue *vq)
271{
272 struct skeleton *skel = vb2_get_drv_priv(vq);
273
274
275
276
277 return_all_buffers(skel, VB2_BUF_STATE_ERROR);
278}
279
280
281
282
283
284
285static 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
297
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
313
314
315
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
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
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
342
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
357
358
359
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
379
380
381 if (vb2_is_busy(&skel->queue))
382 return -EBUSY;
383
384
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
413 if (skel->input)
414 return -ENODATA;
415
416
417
418
419
420
421 if (std == skel->std)
422 return 0;
423
424
425
426
427
428 if (vb2_is_busy(&skel->queue))
429 return -EBUSY;
430
431
432
433 skel->std = std;
434
435
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
445 if (skel->input)
446 return -ENODATA;
447
448 *std = skel->std;
449 return 0;
450}
451
452
453
454
455
456
457
458
459static int skeleton_querystd(struct file *file, void *priv, v4l2_std_id *std)
460{
461 struct skeleton *skel = video_drvdata(file);
462
463
464 if (skel->input)
465 return -ENODATA;
466
467#ifdef TODO
468
469
470
471
472 get_signal_info();
473 if (no_signal) {
474 *std = 0;
475 return 0;
476 }
477
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
492 if (skel->input == 0)
493 return -ENODATA;
494
495
496 if (!v4l2_valid_dv_timings(timings, &skel_timings_cap, NULL, NULL))
497 return -EINVAL;
498
499
500 if (!v4l2_find_dv_timings_cap(timings, &skel_timings_cap,
501 0, NULL, NULL))
502 return -EINVAL;
503
504
505 if (v4l2_match_dv_timings(timings, &skel->timings, 0, false))
506 return 0;
507
508
509
510
511
512 if (vb2_is_busy(&skel->queue))
513 return -EBUSY;
514
515
516
517
518 skel->timings = *timings;
519
520
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
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
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
553
554
555
556
557
558
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
566 if (skel->input == 0)
567 return -ENODATA;
568
569#ifdef TODO
570
571
572
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
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
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
629
630
631 if (vb2_is_busy(&skel->queue))
632 return -EBUSY;
633
634 skel->input = i;
635
636
637
638
639
640 skel->vdev.tvnorms = i ? 0 : SKEL_TVNORMS;
641
642
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
656static int skeleton_s_ctrl(struct v4l2_ctrl *ctrl)
657{
658
659
660
661 switch (ctrl->id) {
662 case V4L2_CID_BRIGHTNESS:
663
664 break;
665 case V4L2_CID_CONTRAST:
666
667 break;
668 case V4L2_CID_SATURATION:
669
670 break;
671 case V4L2_CID_HUE:
672
673 break;
674 default:
675 return -EINVAL;
676 }
677 return 0;
678}
679
680
681
682
683
684static const struct v4l2_ctrl_ops skel_ctrl_ops = {
685 .s_ctrl = skeleton_s_ctrl,
686};
687
688
689
690
691
692
693
694
695
696
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
735
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
749
750
751
752static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
753{
754
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
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
774 skel = devm_kzalloc(&pdev->dev, sizeof(struct skeleton), GFP_KERNEL);
775 if (!skel)
776 return -ENOMEM;
777
778
779 ret = devm_request_irq(&pdev->dev, pdev->irq,
780 skeleton_irq, 0, KBUILD_MODNAME, skel);
781 if (ret) {
782 dev_err(&pdev->dev, "request_irq failed\n");
783 goto disable_pci;
784 }
785 skel->pdev = pdev;
786
787
788 skel->timings = timings_def;
789 skel->std = V4L2_STD_625_50;
790 skeleton_fill_pix_format(skel, &skel->format);
791
792
793 ret = v4l2_device_register(&pdev->dev, &skel->v4l2_dev);
794 if (ret)
795 goto disable_pci;
796
797 mutex_init(&skel->lock);
798
799
800 hdl = &skel->ctrl_handler;
801 v4l2_ctrl_handler_init(hdl, 4);
802 v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
803 V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
804 v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
805 V4L2_CID_CONTRAST, 0, 255, 1, 16);
806 v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
807 V4L2_CID_SATURATION, 0, 255, 1, 127);
808 v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
809 V4L2_CID_HUE, -128, 127, 1, 0);
810 if (hdl->error) {
811 ret = hdl->error;
812 goto free_hdl;
813 }
814 skel->v4l2_dev.ctrl_handler = hdl;
815
816
817 q = &skel->queue;
818 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
819 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
820 q->dev = &pdev->dev;
821 q->drv_priv = skel;
822 q->buf_struct_size = sizeof(struct skel_buffer);
823 q->ops = &skel_qops;
824 q->mem_ops = &vb2_dma_contig_memops;
825 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
826
827
828
829
830
831 q->min_buffers_needed = 2;
832
833
834
835
836
837
838
839
840
841 q->lock = &skel->lock;
842
843
844
845
846 q->gfp_flags = GFP_DMA32;
847 ret = vb2_queue_init(q);
848 if (ret)
849 goto free_hdl;
850
851 INIT_LIST_HEAD(&skel->buf_list);
852 spin_lock_init(&skel->qlock);
853
854
855 vdev = &skel->vdev;
856 strlcpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
857
858
859
860
861 vdev->release = video_device_release_empty;
862 vdev->fops = &skel_fops,
863 vdev->ioctl_ops = &skel_ioctl_ops,
864 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
865 V4L2_CAP_STREAMING;
866
867
868
869
870
871 vdev->lock = &skel->lock;
872 vdev->queue = q;
873 vdev->v4l2_dev = &skel->v4l2_dev;
874
875 vdev->tvnorms = SKEL_TVNORMS;
876 video_set_drvdata(vdev, skel);
877
878 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
879 if (ret)
880 goto free_hdl;
881
882 dev_info(&pdev->dev, "V4L2 PCI Skeleton Driver loaded\n");
883 return 0;
884
885free_hdl:
886 v4l2_ctrl_handler_free(&skel->ctrl_handler);
887 v4l2_device_unregister(&skel->v4l2_dev);
888disable_pci:
889 pci_disable_device(pdev);
890 return ret;
891}
892
893static void skeleton_remove(struct pci_dev *pdev)
894{
895 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
896 struct skeleton *skel = container_of(v4l2_dev, struct skeleton, v4l2_dev);
897
898 video_unregister_device(&skel->vdev);
899 v4l2_ctrl_handler_free(&skel->ctrl_handler);
900 v4l2_device_unregister(&skel->v4l2_dev);
901 pci_disable_device(skel->pdev);
902}
903
904static struct pci_driver skeleton_driver = {
905 .name = KBUILD_MODNAME,
906 .probe = skeleton_probe,
907 .remove = skeleton_remove,
908 .id_table = skeleton_pci_tbl,
909};
910
911module_pci_driver(skeleton_driver);
912