Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / usb / stk1160 / stk1160-v4l.c
blob77b759a0bcd9cbf2d7042f06ceb57b9299fcacac
1 /*
2 * STK1160 driver
4 * Copyright (C) 2012 Ezequiel Garcia
5 * <elezegarcia--a.t--gmail.com>
7 * Based on Easycap driver by R.M. Thomas
8 * Copyright (C) 2010 R.M. Thomas
9 * <rmthomas--a.t--sciolus.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/mm.h>
26 #include <linux/slab.h>
28 #include <linux/videodev2.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-fh.h>
33 #include <media/v4l2-event.h>
34 #include <media/videobuf2-vmalloc.h>
36 #include <media/i2c/saa7115.h>
38 #include "stk1160.h"
39 #include "stk1160-reg.h"
41 static bool keep_buffers;
42 module_param(keep_buffers, bool, 0644);
43 MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming");
45 enum stk1160_decimate_mode {
46 STK1160_DECIMATE_MORE_THAN_HALF,
47 STK1160_DECIMATE_LESS_THAN_HALF,
50 struct stk1160_decimate_ctrl {
51 bool col_en, row_en;
52 enum stk1160_decimate_mode col_mode, row_mode;
53 unsigned int col_n, row_n;
56 /* supported video standards */
57 static struct stk1160_fmt format[] = {
59 .name = "16 bpp YUY2, 4:2:2, packed",
60 .fourcc = V4L2_PIX_FMT_UYVY,
61 .depth = 16,
66 * Helper to find the next divisor that results in modulo being zero.
67 * This is required to guarantee valid decimation unit counts.
69 static unsigned int
70 div_round_integer(unsigned int x, unsigned int y)
72 for (;; y++) {
73 if (x % y == 0)
74 return x / y;
78 static void stk1160_set_std(struct stk1160 *dev)
80 int i;
82 static struct regval std525[] = {
84 /* 720x480 */
86 /* Frame start */
87 {STK116_CFSPO_STX_L, 0x0000},
88 {STK116_CFSPO_STX_H, 0x0000},
89 {STK116_CFSPO_STY_L, 0x0003},
90 {STK116_CFSPO_STY_H, 0x0000},
92 /* Frame end */
93 {STK116_CFEPO_ENX_L, 0x05a0},
94 {STK116_CFEPO_ENX_H, 0x0005},
95 {STK116_CFEPO_ENY_L, 0x00f3},
96 {STK116_CFEPO_ENY_H, 0x0000},
98 {0xffff, 0xffff}
101 static struct regval std625[] = {
103 /* 720x576 */
105 /* TODO: Each line of frame has some junk at the end */
106 /* Frame start */
107 {STK116_CFSPO, 0x0000},
108 {STK116_CFSPO+1, 0x0000},
109 {STK116_CFSPO+2, 0x0001},
110 {STK116_CFSPO+3, 0x0000},
112 /* Frame end */
113 {STK116_CFEPO, 0x05a0},
114 {STK116_CFEPO+1, 0x0005},
115 {STK116_CFEPO+2, 0x0121},
116 {STK116_CFEPO+3, 0x0001},
118 {0xffff, 0xffff}
121 if (dev->norm & V4L2_STD_525_60) {
122 stk1160_dbg("registers to NTSC like standard\n");
123 for (i = 0; std525[i].reg != 0xffff; i++)
124 stk1160_write_reg(dev, std525[i].reg, std525[i].val);
125 } else {
126 stk1160_dbg("registers to PAL like standard\n");
127 for (i = 0; std625[i].reg != 0xffff; i++)
128 stk1160_write_reg(dev, std625[i].reg, std625[i].val);
133 static void stk1160_set_fmt(struct stk1160 *dev,
134 struct stk1160_decimate_ctrl *ctrl)
136 u32 val = 0;
138 if (ctrl) {
140 * Since the format is UYVY, the device must skip or send
141 * a number of rows/columns multiple of four. This way, the
142 * colour format is preserved. The STK1160_DEC_UNIT_SIZE bit
143 * does exactly this.
145 val |= STK1160_DEC_UNIT_SIZE;
146 val |= ctrl->col_en ? STK1160_H_DEC_EN : 0;
147 val |= ctrl->row_en ? STK1160_V_DEC_EN : 0;
148 val |= ctrl->col_mode ==
149 STK1160_DECIMATE_MORE_THAN_HALF ?
150 STK1160_H_DEC_MODE : 0;
151 val |= ctrl->row_mode ==
152 STK1160_DECIMATE_MORE_THAN_HALF ?
153 STK1160_V_DEC_MODE : 0;
155 /* Horizontal count units */
156 stk1160_write_reg(dev, STK1160_DMCTRL_H_UNITS, ctrl->col_n);
157 /* Vertical count units */
158 stk1160_write_reg(dev, STK1160_DMCTRL_V_UNITS, ctrl->row_n);
160 stk1160_dbg("decimate 0x%x, column units %d, row units %d\n",
161 val, ctrl->col_n, ctrl->row_n);
164 /* Decimation control */
165 stk1160_write_reg(dev, STK1160_DMCTRL, val);
169 * Set a new alternate setting.
170 * Returns true is dev->max_pkt_size has changed, false otherwise.
172 static bool stk1160_set_alternate(struct stk1160 *dev)
174 int i, prev_alt = dev->alt;
175 unsigned int min_pkt_size;
176 bool new_pkt_size;
179 * If we don't set right alternate,
180 * then we will get a green screen with junk.
182 min_pkt_size = STK1160_MIN_PKT_SIZE;
184 for (i = 0; i < dev->num_alt; i++) {
185 /* stop when the selected alt setting offers enough bandwidth */
186 if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
187 dev->alt = i;
188 break;
190 * otherwise make sure that we end up with the maximum bandwidth
191 * because the min_pkt_size equation might be wrong...
193 } else if (dev->alt_max_pkt_size[i] >
194 dev->alt_max_pkt_size[dev->alt])
195 dev->alt = i;
198 stk1160_dbg("setting alternate %d\n", dev->alt);
200 if (dev->alt != prev_alt) {
201 stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n",
202 min_pkt_size, dev->alt);
203 stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n",
204 dev->alt, dev->alt_max_pkt_size[dev->alt]);
205 usb_set_interface(dev->udev, 0, dev->alt);
208 new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt];
209 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
211 return new_pkt_size;
214 static int stk1160_start_streaming(struct stk1160 *dev)
216 bool new_pkt_size;
217 int rc = 0;
218 int i;
220 /* Check device presence */
221 if (!dev->udev)
222 return -ENODEV;
224 if (mutex_lock_interruptible(&dev->v4l_lock))
225 return -ERESTARTSYS;
227 * For some reason it is mandatory to set alternate *first*
228 * and only *then* initialize isoc urbs.
229 * Someone please explain me why ;)
231 new_pkt_size = stk1160_set_alternate(dev);
234 * We (re)allocate isoc urbs if:
235 * there is no allocated isoc urbs, OR
236 * a new dev->max_pkt_size is detected
238 if (!dev->isoc_ctl.num_bufs || new_pkt_size) {
239 rc = stk1160_alloc_isoc(dev);
240 if (rc < 0)
241 goto out_stop_hw;
244 /* submit urbs and enables IRQ */
245 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
246 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL);
247 if (rc) {
248 stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
249 goto out_uninit;
253 /* Start saa711x */
254 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
256 dev->sequence = 0;
258 /* Start stk1160 */
259 stk1160_write_reg(dev, STK1160_DCTRL, 0xb3);
260 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
262 stk1160_dbg("streaming started\n");
264 mutex_unlock(&dev->v4l_lock);
266 return 0;
268 out_uninit:
269 stk1160_uninit_isoc(dev);
270 out_stop_hw:
271 usb_set_interface(dev->udev, 0, 0);
272 stk1160_clear_queue(dev);
274 mutex_unlock(&dev->v4l_lock);
276 return rc;
279 /* Must be called with v4l_lock hold */
280 static void stk1160_stop_hw(struct stk1160 *dev)
282 /* If the device is not physically present, there is nothing to do */
283 if (!dev->udev)
284 return;
286 /* set alternate 0 */
287 dev->alt = 0;
288 stk1160_dbg("setting alternate %d\n", dev->alt);
289 usb_set_interface(dev->udev, 0, 0);
291 /* Stop stk1160 */
292 stk1160_write_reg(dev, STK1160_DCTRL, 0x00);
293 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
295 /* Stop saa711x */
296 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
299 static int stk1160_stop_streaming(struct stk1160 *dev)
301 if (mutex_lock_interruptible(&dev->v4l_lock))
302 return -ERESTARTSYS;
305 * Once URBs are cancelled, the URB complete handler
306 * won't be running. This is required to safely release the
307 * current buffer (dev->isoc_ctl.buf).
309 stk1160_cancel_isoc(dev);
312 * It is possible to keep buffers around using a module parameter.
313 * This is intended to avoid memory fragmentation.
315 if (!keep_buffers)
316 stk1160_free_isoc(dev);
318 stk1160_stop_hw(dev);
320 stk1160_clear_queue(dev);
322 stk1160_dbg("streaming stopped\n");
324 mutex_unlock(&dev->v4l_lock);
326 return 0;
329 static const struct v4l2_file_operations stk1160_fops = {
330 .owner = THIS_MODULE,
331 .open = v4l2_fh_open,
332 .release = vb2_fop_release,
333 .read = vb2_fop_read,
334 .poll = vb2_fop_poll,
335 .mmap = vb2_fop_mmap,
336 .unlocked_ioctl = video_ioctl2,
340 * vidioc ioctls
342 static int vidioc_querycap(struct file *file,
343 void *priv, struct v4l2_capability *cap)
345 struct stk1160 *dev = video_drvdata(file);
347 strcpy(cap->driver, "stk1160");
348 strcpy(cap->card, "stk1160");
349 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
350 cap->device_caps =
351 V4L2_CAP_VIDEO_CAPTURE |
352 V4L2_CAP_STREAMING |
353 V4L2_CAP_READWRITE;
354 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
355 return 0;
358 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
359 struct v4l2_fmtdesc *f)
361 if (f->index != 0)
362 return -EINVAL;
364 strlcpy(f->description, format[f->index].name, sizeof(f->description));
365 f->pixelformat = format[f->index].fourcc;
366 return 0;
369 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
370 struct v4l2_format *f)
372 struct stk1160 *dev = video_drvdata(file);
374 f->fmt.pix.width = dev->width;
375 f->fmt.pix.height = dev->height;
376 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
377 f->fmt.pix.pixelformat = dev->fmt->fourcc;
378 f->fmt.pix.bytesperline = dev->width * 2;
379 f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
380 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
382 return 0;
385 static int stk1160_try_fmt(struct stk1160 *dev, struct v4l2_format *f,
386 struct stk1160_decimate_ctrl *ctrl)
388 unsigned int width, height;
389 unsigned int base_width, base_height;
390 unsigned int col_n, row_n;
391 enum stk1160_decimate_mode col_mode, row_mode;
392 bool col_en, row_en;
394 base_width = 720;
395 base_height = (dev->norm & V4L2_STD_525_60) ? 480 : 576;
397 /* Minimum width and height is 5% the frame size */
398 width = clamp_t(unsigned int, f->fmt.pix.width,
399 base_width / 20, base_width);
400 height = clamp_t(unsigned int, f->fmt.pix.height,
401 base_height / 20, base_height);
403 /* Let's set default no decimation values */
404 col_n = 0;
405 row_n = 0;
406 col_en = false;
407 row_en = false;
408 f->fmt.pix.width = base_width;
409 f->fmt.pix.height = base_height;
410 row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
411 col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
413 if (width < base_width && width > base_width / 2) {
415 * The device will send count units for each
416 * unit skipped. This means count unit is:
418 * n = width / (frame width - width)
420 * And the width is:
422 * width = (n / n + 1) * frame width
424 col_n = div_round_integer(width, base_width - width);
425 if (col_n > 0 && col_n <= 255) {
426 col_en = true;
427 col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
428 f->fmt.pix.width = (base_width * col_n) / (col_n + 1);
431 } else if (width <= base_width / 2) {
434 * The device will skip count units for each
435 * unit sent. This means count is:
437 * n = (frame width / width) - 1
439 * And the width is:
441 * width = frame width / (n + 1)
443 col_n = div_round_integer(base_width, width) - 1;
444 if (col_n > 0 && col_n <= 255) {
445 col_en = true;
446 col_mode = STK1160_DECIMATE_MORE_THAN_HALF;
447 f->fmt.pix.width = base_width / (col_n + 1);
451 if (height < base_height && height > base_height / 2) {
452 row_n = div_round_integer(height, base_height - height);
453 if (row_n > 0 && row_n <= 255) {
454 row_en = true;
455 row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
456 f->fmt.pix.height = (base_height * row_n) / (row_n + 1);
459 } else if (height <= base_height / 2) {
460 row_n = div_round_integer(base_height, height) - 1;
461 if (row_n > 0 && row_n <= 255) {
462 row_en = true;
463 row_mode = STK1160_DECIMATE_MORE_THAN_HALF;
464 f->fmt.pix.height = base_height / (row_n + 1);
468 f->fmt.pix.pixelformat = dev->fmt->fourcc;
469 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
470 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
471 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
472 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
474 if (ctrl) {
475 ctrl->col_en = col_en;
476 ctrl->col_n = col_n;
477 ctrl->col_mode = col_mode;
478 ctrl->row_en = row_en;
479 ctrl->row_n = row_n;
480 ctrl->row_mode = row_mode;
483 stk1160_dbg("width %d, height %d\n",
484 f->fmt.pix.width, f->fmt.pix.height);
485 return 0;
488 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
489 struct v4l2_format *f)
491 struct stk1160 *dev = video_drvdata(file);
493 return stk1160_try_fmt(dev, f, NULL);
496 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
497 struct v4l2_format *f)
499 struct stk1160 *dev = video_drvdata(file);
500 struct vb2_queue *q = &dev->vb_vidq;
501 struct stk1160_decimate_ctrl ctrl;
502 int rc;
504 if (vb2_is_busy(q))
505 return -EBUSY;
507 rc = stk1160_try_fmt(dev, f, &ctrl);
508 if (rc < 0)
509 return rc;
510 dev->width = f->fmt.pix.width;
511 dev->height = f->fmt.pix.height;
512 stk1160_set_fmt(dev, &ctrl);
514 return 0;
517 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
519 struct stk1160 *dev = video_drvdata(file);
520 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
521 return 0;
524 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
526 struct stk1160 *dev = video_drvdata(file);
528 *norm = dev->norm;
529 return 0;
532 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
534 struct stk1160 *dev = video_drvdata(file);
535 struct vb2_queue *q = &dev->vb_vidq;
537 if (dev->norm == norm)
538 return 0;
540 if (vb2_is_busy(q))
541 return -EBUSY;
543 /* Check device presence */
544 if (!dev->udev)
545 return -ENODEV;
547 /* We need to set this now, before we call stk1160_set_std */
548 dev->width = 720;
549 dev->height = (norm & V4L2_STD_525_60) ? 480 : 576;
550 dev->norm = norm;
552 stk1160_set_std(dev);
554 /* Calling with NULL disables frame decimation */
555 stk1160_set_fmt(dev, NULL);
557 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
558 dev->norm);
560 return 0;
564 static int vidioc_enum_input(struct file *file, void *priv,
565 struct v4l2_input *i)
567 struct stk1160 *dev = video_drvdata(file);
569 if (i->index > STK1160_MAX_INPUT)
570 return -EINVAL;
572 /* S-Video special handling */
573 if (i->index == STK1160_SVIDEO_INPUT)
574 sprintf(i->name, "S-Video");
575 else
576 sprintf(i->name, "Composite%d", i->index);
578 i->type = V4L2_INPUT_TYPE_CAMERA;
579 i->std = dev->vdev.tvnorms;
580 return 0;
583 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
585 struct stk1160 *dev = video_drvdata(file);
586 *i = dev->ctl_input;
587 return 0;
590 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
592 struct stk1160 *dev = video_drvdata(file);
594 if (i > STK1160_MAX_INPUT)
595 return -EINVAL;
597 dev->ctl_input = i;
599 stk1160_select_input(dev);
601 return 0;
604 #ifdef CONFIG_VIDEO_ADV_DEBUG
605 static int vidioc_g_register(struct file *file, void *priv,
606 struct v4l2_dbg_register *reg)
608 struct stk1160 *dev = video_drvdata(file);
609 int rc;
610 u8 val;
612 /* Match host */
613 rc = stk1160_read_reg(dev, reg->reg, &val);
614 reg->val = val;
615 reg->size = 1;
617 return rc;
620 static int vidioc_s_register(struct file *file, void *priv,
621 const struct v4l2_dbg_register *reg)
623 struct stk1160 *dev = video_drvdata(file);
625 /* Match host */
626 return stk1160_write_reg(dev, reg->reg, reg->val);
628 #endif
630 static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
631 .vidioc_querycap = vidioc_querycap,
632 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
633 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
634 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
635 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
636 .vidioc_querystd = vidioc_querystd,
637 .vidioc_g_std = vidioc_g_std,
638 .vidioc_s_std = vidioc_s_std,
639 .vidioc_enum_input = vidioc_enum_input,
640 .vidioc_g_input = vidioc_g_input,
641 .vidioc_s_input = vidioc_s_input,
643 /* vb2 takes care of these */
644 .vidioc_reqbufs = vb2_ioctl_reqbufs,
645 .vidioc_querybuf = vb2_ioctl_querybuf,
646 .vidioc_qbuf = vb2_ioctl_qbuf,
647 .vidioc_dqbuf = vb2_ioctl_dqbuf,
648 .vidioc_streamon = vb2_ioctl_streamon,
649 .vidioc_streamoff = vb2_ioctl_streamoff,
650 .vidioc_expbuf = vb2_ioctl_expbuf,
652 .vidioc_log_status = v4l2_ctrl_log_status,
653 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
654 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
656 #ifdef CONFIG_VIDEO_ADV_DEBUG
657 .vidioc_g_register = vidioc_g_register,
658 .vidioc_s_register = vidioc_s_register,
659 #endif
662 /********************************************************************/
665 * Videobuf2 operations
667 static int queue_setup(struct vb2_queue *vq,
668 unsigned int *nbuffers, unsigned int *nplanes,
669 unsigned int sizes[], struct device *alloc_devs[])
671 struct stk1160 *dev = vb2_get_drv_priv(vq);
672 unsigned long size;
674 size = dev->width * dev->height * 2;
677 * Here we can change the number of buffers being requested.
678 * So, we set a minimum and a maximum like this:
680 *nbuffers = clamp_t(unsigned int, *nbuffers,
681 STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
683 if (*nplanes)
684 return sizes[0] < size ? -EINVAL : 0;
686 /* This means a packed colorformat */
687 *nplanes = 1;
689 sizes[0] = size;
691 stk1160_dbg("%s: buffer count %d, each %ld bytes\n",
692 __func__, *nbuffers, size);
694 return 0;
697 static void buffer_queue(struct vb2_buffer *vb)
699 unsigned long flags;
700 struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
701 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
702 struct stk1160_buffer *buf =
703 container_of(vbuf, struct stk1160_buffer, vb);
705 spin_lock_irqsave(&dev->buf_lock, flags);
706 if (!dev->udev) {
708 * If the device is disconnected return the buffer to userspace
709 * directly. The next QBUF call will fail with -ENODEV.
711 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
712 } else {
714 buf->mem = vb2_plane_vaddr(vb, 0);
715 buf->length = vb2_plane_size(vb, 0);
716 buf->bytesused = 0;
717 buf->pos = 0;
720 * If buffer length is less from expected then we return
721 * the buffer to userspace directly.
723 if (buf->length < dev->width * dev->height * 2)
724 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
725 else
726 list_add_tail(&buf->list, &dev->avail_bufs);
729 spin_unlock_irqrestore(&dev->buf_lock, flags);
732 static int start_streaming(struct vb2_queue *vq, unsigned int count)
734 struct stk1160 *dev = vb2_get_drv_priv(vq);
735 return stk1160_start_streaming(dev);
738 /* abort streaming and wait for last buffer */
739 static void stop_streaming(struct vb2_queue *vq)
741 struct stk1160 *dev = vb2_get_drv_priv(vq);
742 stk1160_stop_streaming(dev);
745 static const struct vb2_ops stk1160_video_qops = {
746 .queue_setup = queue_setup,
747 .buf_queue = buffer_queue,
748 .start_streaming = start_streaming,
749 .stop_streaming = stop_streaming,
750 .wait_prepare = vb2_ops_wait_prepare,
751 .wait_finish = vb2_ops_wait_finish,
754 static const struct video_device v4l_template = {
755 .name = "stk1160",
756 .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
757 .fops = &stk1160_fops,
758 .ioctl_ops = &stk1160_ioctl_ops,
759 .release = video_device_release_empty,
762 /********************************************************************/
764 /* Must be called with both v4l_lock and vb_queue_lock hold */
765 void stk1160_clear_queue(struct stk1160 *dev)
767 struct stk1160_buffer *buf;
768 unsigned long flags;
770 /* Release all active buffers */
771 spin_lock_irqsave(&dev->buf_lock, flags);
772 while (!list_empty(&dev->avail_bufs)) {
773 buf = list_first_entry(&dev->avail_bufs,
774 struct stk1160_buffer, list);
775 list_del(&buf->list);
776 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
777 stk1160_dbg("buffer [%p/%d] aborted\n",
778 buf, buf->vb.vb2_buf.index);
781 /* It's important to release the current buffer */
782 if (dev->isoc_ctl.buf) {
783 buf = dev->isoc_ctl.buf;
784 dev->isoc_ctl.buf = NULL;
786 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
787 stk1160_dbg("buffer [%p/%d] aborted\n",
788 buf, buf->vb.vb2_buf.index);
790 spin_unlock_irqrestore(&dev->buf_lock, flags);
793 int stk1160_vb2_setup(struct stk1160 *dev)
795 int rc;
796 struct vb2_queue *q;
798 q = &dev->vb_vidq;
799 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
800 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
801 q->drv_priv = dev;
802 q->buf_struct_size = sizeof(struct stk1160_buffer);
803 q->ops = &stk1160_video_qops;
804 q->mem_ops = &vb2_vmalloc_memops;
805 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
807 rc = vb2_queue_init(q);
808 if (rc < 0)
809 return rc;
811 /* initialize video dma queue */
812 INIT_LIST_HEAD(&dev->avail_bufs);
814 return 0;
817 int stk1160_video_register(struct stk1160 *dev)
819 int rc;
821 /* Initialize video_device with a template structure */
822 dev->vdev = v4l_template;
823 dev->vdev.queue = &dev->vb_vidq;
826 * Provide mutexes for v4l2 core and for videobuf2 queue.
827 * It will be used to protect *only* v4l2 ioctls.
829 dev->vdev.lock = &dev->v4l_lock;
830 dev->vdev.queue->lock = &dev->vb_queue_lock;
832 /* This will be used to set video_device parent */
833 dev->vdev.v4l2_dev = &dev->v4l2_dev;
835 /* NTSC is default */
836 dev->norm = V4L2_STD_NTSC_M;
837 dev->width = 720;
838 dev->height = 480;
840 /* set default format */
841 dev->fmt = &format[0];
842 stk1160_set_std(dev);
844 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
845 dev->norm);
847 video_set_drvdata(&dev->vdev, dev);
848 rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
849 if (rc < 0) {
850 stk1160_err("video_register_device failed (%d)\n", rc);
851 return rc;
854 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
855 video_device_node_name(&dev->vdev));
857 return 0;