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