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