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