1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2012 Ezequiel Garcia
6 * <elezegarcia--a.t--gmail.com>
8 * Based on Easycap driver by R.M. Thomas
9 * Copyright (C) 2010 R.M. Thomas
10 * <rmthomas--a.t--sciolus.org>
13 #include <linux/module.h>
14 #include <linux/usb.h>
16 #include <linux/slab.h>
18 #include <linux/videodev2.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-fh.h>
23 #include <media/v4l2-event.h>
24 #include <media/videobuf2-vmalloc.h>
26 #include <media/i2c/saa7115.h>
29 #include "stk1160-reg.h"
31 static bool keep_buffers;
32 module_param(keep_buffers, bool, 0644);
33 MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming");
35 enum stk1160_decimate_mode {
36 STK1160_DECIMATE_MORE_THAN_HALF,
37 STK1160_DECIMATE_LESS_THAN_HALF,
40 struct stk1160_decimate_ctrl {
42 enum stk1160_decimate_mode col_mode, row_mode;
43 unsigned int col_n, row_n;
46 /* supported video standards */
47 static struct stk1160_fmt format[] = {
49 .name = "16 bpp YUY2, 4:2:2, packed",
50 .fourcc = V4L2_PIX_FMT_UYVY,
56 * Helper to find the next divisor that results in modulo being zero.
57 * This is required to guarantee valid decimation unit counts.
60 div_round_integer(unsigned int x, unsigned int y)
68 static void stk1160_set_std(struct stk1160 *dev)
72 static struct regval std525[] = {
77 {STK116_CFSPO_STX_L, 0x0000},
78 {STK116_CFSPO_STX_H, 0x0000},
79 {STK116_CFSPO_STY_L, 0x0003},
80 {STK116_CFSPO_STY_H, 0x0000},
83 {STK116_CFEPO_ENX_L, 0x05a0},
84 {STK116_CFEPO_ENX_H, 0x0005},
85 {STK116_CFEPO_ENY_L, 0x00f3},
86 {STK116_CFEPO_ENY_H, 0x0000},
91 static struct regval std625[] = {
95 /* TODO: Each line of frame has some junk at the end */
97 {STK116_CFSPO, 0x0000},
98 {STK116_CFSPO+1, 0x0000},
99 {STK116_CFSPO+2, 0x0001},
100 {STK116_CFSPO+3, 0x0000},
103 {STK116_CFEPO, 0x05a0},
104 {STK116_CFEPO+1, 0x0005},
105 {STK116_CFEPO+2, 0x0121},
106 {STK116_CFEPO+3, 0x0001},
111 if (dev->norm & V4L2_STD_525_60) {
112 stk1160_dbg("registers to NTSC like standard\n");
113 for (i = 0; std525[i].reg != 0xffff; i++)
114 stk1160_write_reg(dev, std525[i].reg, std525[i].val);
116 stk1160_dbg("registers to PAL like standard\n");
117 for (i = 0; std625[i].reg != 0xffff; i++)
118 stk1160_write_reg(dev, std625[i].reg, std625[i].val);
123 static void stk1160_set_fmt(struct stk1160 *dev,
124 struct stk1160_decimate_ctrl *ctrl)
130 * Since the format is UYVY, the device must skip or send
131 * a number of rows/columns multiple of four. This way, the
132 * colour format is preserved. The STK1160_DEC_UNIT_SIZE bit
135 val |= STK1160_DEC_UNIT_SIZE;
136 val |= ctrl->col_en ? STK1160_H_DEC_EN : 0;
137 val |= ctrl->row_en ? STK1160_V_DEC_EN : 0;
138 val |= ctrl->col_mode ==
139 STK1160_DECIMATE_MORE_THAN_HALF ?
140 STK1160_H_DEC_MODE : 0;
141 val |= ctrl->row_mode ==
142 STK1160_DECIMATE_MORE_THAN_HALF ?
143 STK1160_V_DEC_MODE : 0;
145 /* Horizontal count units */
146 stk1160_write_reg(dev, STK1160_DMCTRL_H_UNITS, ctrl->col_n);
147 /* Vertical count units */
148 stk1160_write_reg(dev, STK1160_DMCTRL_V_UNITS, ctrl->row_n);
150 stk1160_dbg("decimate 0x%x, column units %d, row units %d\n",
151 val, ctrl->col_n, ctrl->row_n);
154 /* Decimation control */
155 stk1160_write_reg(dev, STK1160_DMCTRL, val);
159 * Set a new alternate setting.
160 * Returns true is dev->max_pkt_size has changed, false otherwise.
162 static bool stk1160_set_alternate(struct stk1160 *dev)
164 int i, prev_alt = dev->alt;
165 unsigned int min_pkt_size;
169 * If we don't set right alternate,
170 * then we will get a green screen with junk.
172 min_pkt_size = STK1160_MIN_PKT_SIZE;
174 for (i = 0; i < dev->num_alt; i++) {
175 /* stop when the selected alt setting offers enough bandwidth */
176 if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
180 * otherwise make sure that we end up with the maximum bandwidth
181 * because the min_pkt_size equation might be wrong...
183 } else if (dev->alt_max_pkt_size[i] >
184 dev->alt_max_pkt_size[dev->alt])
188 stk1160_dbg("setting alternate %d\n", dev->alt);
190 if (dev->alt != prev_alt) {
191 stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n",
192 min_pkt_size, dev->alt);
193 stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n",
194 dev->alt, dev->alt_max_pkt_size[dev->alt]);
195 usb_set_interface(dev->udev, 0, dev->alt);
198 new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt];
199 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
204 static int stk1160_start_streaming(struct stk1160 *dev)
210 /* Check device presence */
214 if (mutex_lock_interruptible(&dev->v4l_lock))
217 * For some reason it is mandatory to set alternate *first*
218 * and only *then* initialize isoc urbs.
219 * Someone please explain me why ;)
221 new_pkt_size = stk1160_set_alternate(dev);
224 * We (re)allocate isoc urbs if:
225 * there is no allocated isoc urbs, OR
226 * a new dev->max_pkt_size is detected
228 if (!dev->isoc_ctl.num_bufs || new_pkt_size) {
229 rc = stk1160_alloc_isoc(dev);
234 /* submit urbs and enables IRQ */
235 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
236 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL);
238 stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
244 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
249 stk1160_write_reg(dev, STK1160_DCTRL, 0xb3);
250 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
252 stk1160_dbg("streaming started\n");
254 mutex_unlock(&dev->v4l_lock);
259 stk1160_uninit_isoc(dev);
261 usb_set_interface(dev->udev, 0, 0);
262 stk1160_clear_queue(dev);
264 mutex_unlock(&dev->v4l_lock);
269 /* Must be called with v4l_lock hold */
270 static void stk1160_stop_hw(struct stk1160 *dev)
272 /* If the device is not physically present, there is nothing to do */
276 /* set alternate 0 */
278 stk1160_dbg("setting alternate %d\n", dev->alt);
279 usb_set_interface(dev->udev, 0, 0);
282 stk1160_write_reg(dev, STK1160_DCTRL, 0x00);
283 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
286 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
289 static int stk1160_stop_streaming(struct stk1160 *dev)
291 if (mutex_lock_interruptible(&dev->v4l_lock))
295 * Once URBs are cancelled, the URB complete handler
296 * won't be running. This is required to safely release the
297 * current buffer (dev->isoc_ctl.buf).
299 stk1160_cancel_isoc(dev);
302 * It is possible to keep buffers around using a module parameter.
303 * This is intended to avoid memory fragmentation.
306 stk1160_free_isoc(dev);
308 stk1160_stop_hw(dev);
310 stk1160_clear_queue(dev);
312 stk1160_dbg("streaming stopped\n");
314 mutex_unlock(&dev->v4l_lock);
319 static const struct v4l2_file_operations stk1160_fops = {
320 .owner = THIS_MODULE,
321 .open = v4l2_fh_open,
322 .release = vb2_fop_release,
323 .read = vb2_fop_read,
324 .poll = vb2_fop_poll,
325 .mmap = vb2_fop_mmap,
326 .unlocked_ioctl = video_ioctl2,
332 static int vidioc_querycap(struct file *file,
333 void *priv, struct v4l2_capability *cap)
335 struct stk1160 *dev = video_drvdata(file);
337 strscpy(cap->driver, "stk1160", sizeof(cap->driver));
338 strscpy(cap->card, "stk1160", sizeof(cap->card));
339 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
343 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
344 struct v4l2_fmtdesc *f)
349 strscpy(f->description, format[f->index].name, sizeof(f->description));
350 f->pixelformat = format[f->index].fourcc;
354 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
355 struct v4l2_format *f)
357 struct stk1160 *dev = video_drvdata(file);
359 f->fmt.pix.width = dev->width;
360 f->fmt.pix.height = dev->height;
361 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
362 f->fmt.pix.pixelformat = dev->fmt->fourcc;
363 f->fmt.pix.bytesperline = dev->width * 2;
364 f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
365 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
370 static int stk1160_try_fmt(struct stk1160 *dev, struct v4l2_format *f,
371 struct stk1160_decimate_ctrl *ctrl)
373 unsigned int width, height;
374 unsigned int base_width, base_height;
375 unsigned int col_n, row_n;
376 enum stk1160_decimate_mode col_mode, row_mode;
380 base_height = (dev->norm & V4L2_STD_525_60) ? 480 : 576;
382 /* Minimum width and height is 5% the frame size */
383 width = clamp_t(unsigned int, f->fmt.pix.width,
384 base_width / 20, base_width);
385 height = clamp_t(unsigned int, f->fmt.pix.height,
386 base_height / 20, base_height);
388 /* Let's set default no decimation values */
393 f->fmt.pix.width = base_width;
394 f->fmt.pix.height = base_height;
395 row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
396 col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
398 if (width < base_width && width > base_width / 2) {
400 * The device will send count units for each
401 * unit skipped. This means count unit is:
403 * n = width / (frame width - width)
407 * width = (n / n + 1) * frame width
409 col_n = div_round_integer(width, base_width - width);
410 if (col_n > 0 && col_n <= 255) {
412 col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
413 f->fmt.pix.width = (base_width * col_n) / (col_n + 1);
416 } else if (width <= base_width / 2) {
419 * The device will skip count units for each
420 * unit sent. This means count is:
422 * n = (frame width / width) - 1
426 * width = frame width / (n + 1)
428 col_n = div_round_integer(base_width, width) - 1;
429 if (col_n > 0 && col_n <= 255) {
431 col_mode = STK1160_DECIMATE_MORE_THAN_HALF;
432 f->fmt.pix.width = base_width / (col_n + 1);
436 if (height < base_height && height > base_height / 2) {
437 row_n = div_round_integer(height, base_height - height);
438 if (row_n > 0 && row_n <= 255) {
440 row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
441 f->fmt.pix.height = (base_height * row_n) / (row_n + 1);
444 } else if (height <= base_height / 2) {
445 row_n = div_round_integer(base_height, height) - 1;
446 if (row_n > 0 && row_n <= 255) {
448 row_mode = STK1160_DECIMATE_MORE_THAN_HALF;
449 f->fmt.pix.height = base_height / (row_n + 1);
453 f->fmt.pix.pixelformat = dev->fmt->fourcc;
454 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
455 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
456 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
457 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
460 ctrl->col_en = col_en;
462 ctrl->col_mode = col_mode;
463 ctrl->row_en = row_en;
465 ctrl->row_mode = row_mode;
468 stk1160_dbg("width %d, height %d\n",
469 f->fmt.pix.width, f->fmt.pix.height);
473 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
474 struct v4l2_format *f)
476 struct stk1160 *dev = video_drvdata(file);
478 return stk1160_try_fmt(dev, f, NULL);
481 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
482 struct v4l2_format *f)
484 struct stk1160 *dev = video_drvdata(file);
485 struct vb2_queue *q = &dev->vb_vidq;
486 struct stk1160_decimate_ctrl ctrl;
492 rc = stk1160_try_fmt(dev, f, &ctrl);
495 dev->width = f->fmt.pix.width;
496 dev->height = f->fmt.pix.height;
497 stk1160_set_fmt(dev, &ctrl);
502 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
504 struct stk1160 *dev = video_drvdata(file);
505 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
509 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
511 struct stk1160 *dev = video_drvdata(file);
517 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
519 struct stk1160 *dev = video_drvdata(file);
520 struct vb2_queue *q = &dev->vb_vidq;
522 if (dev->norm == norm)
528 /* Check device presence */
532 /* We need to set this now, before we call stk1160_set_std */
534 dev->height = (norm & V4L2_STD_525_60) ? 480 : 576;
537 stk1160_set_std(dev);
539 /* Calling with NULL disables frame decimation */
540 stk1160_set_fmt(dev, NULL);
542 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
549 static int vidioc_enum_input(struct file *file, void *priv,
550 struct v4l2_input *i)
552 struct stk1160 *dev = video_drvdata(file);
554 if (i->index > STK1160_MAX_INPUT)
557 /* S-Video special handling */
558 if (i->index == STK1160_SVIDEO_INPUT)
559 sprintf(i->name, "S-Video");
561 sprintf(i->name, "Composite%d", i->index);
563 i->type = V4L2_INPUT_TYPE_CAMERA;
564 i->std = dev->vdev.tvnorms;
568 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
570 struct stk1160 *dev = video_drvdata(file);
575 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
577 struct stk1160 *dev = video_drvdata(file);
579 if (i > STK1160_MAX_INPUT)
584 stk1160_select_input(dev);
589 #ifdef CONFIG_VIDEO_ADV_DEBUG
590 static int vidioc_g_register(struct file *file, void *priv,
591 struct v4l2_dbg_register *reg)
593 struct stk1160 *dev = video_drvdata(file);
598 rc = stk1160_read_reg(dev, reg->reg, &val);
605 static int vidioc_s_register(struct file *file, void *priv,
606 const struct v4l2_dbg_register *reg)
608 struct stk1160 *dev = video_drvdata(file);
611 return stk1160_write_reg(dev, reg->reg, reg->val);
615 static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
616 .vidioc_querycap = vidioc_querycap,
617 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
618 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
619 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
620 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
621 .vidioc_querystd = vidioc_querystd,
622 .vidioc_g_std = vidioc_g_std,
623 .vidioc_s_std = vidioc_s_std,
624 .vidioc_enum_input = vidioc_enum_input,
625 .vidioc_g_input = vidioc_g_input,
626 .vidioc_s_input = vidioc_s_input,
628 /* vb2 takes care of these */
629 .vidioc_reqbufs = vb2_ioctl_reqbufs,
630 .vidioc_querybuf = vb2_ioctl_querybuf,
631 .vidioc_qbuf = vb2_ioctl_qbuf,
632 .vidioc_dqbuf = vb2_ioctl_dqbuf,
633 .vidioc_streamon = vb2_ioctl_streamon,
634 .vidioc_streamoff = vb2_ioctl_streamoff,
635 .vidioc_expbuf = vb2_ioctl_expbuf,
637 .vidioc_log_status = v4l2_ctrl_log_status,
638 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
639 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
641 #ifdef CONFIG_VIDEO_ADV_DEBUG
642 .vidioc_g_register = vidioc_g_register,
643 .vidioc_s_register = vidioc_s_register,
647 /********************************************************************/
650 * Videobuf2 operations
652 static int queue_setup(struct vb2_queue *vq,
653 unsigned int *nbuffers, unsigned int *nplanes,
654 unsigned int sizes[], struct device *alloc_devs[])
656 struct stk1160 *dev = vb2_get_drv_priv(vq);
659 size = dev->width * dev->height * 2;
662 * Here we can change the number of buffers being requested.
663 * So, we set a minimum and a maximum like this:
665 *nbuffers = clamp_t(unsigned int, *nbuffers,
666 STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
669 return sizes[0] < size ? -EINVAL : 0;
671 /* This means a packed colorformat */
676 stk1160_dbg("%s: buffer count %d, each %ld bytes\n",
677 __func__, *nbuffers, size);
682 static void buffer_queue(struct vb2_buffer *vb)
685 struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
686 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
687 struct stk1160_buffer *buf =
688 container_of(vbuf, struct stk1160_buffer, vb);
690 spin_lock_irqsave(&dev->buf_lock, flags);
693 * If the device is disconnected return the buffer to userspace
694 * directly. The next QBUF call will fail with -ENODEV.
696 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
699 buf->mem = vb2_plane_vaddr(vb, 0);
700 buf->length = vb2_plane_size(vb, 0);
705 * If buffer length is less from expected then we return
706 * the buffer to userspace directly.
708 if (buf->length < dev->width * dev->height * 2)
709 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
711 list_add_tail(&buf->list, &dev->avail_bufs);
714 spin_unlock_irqrestore(&dev->buf_lock, flags);
717 static int start_streaming(struct vb2_queue *vq, unsigned int count)
719 struct stk1160 *dev = vb2_get_drv_priv(vq);
720 return stk1160_start_streaming(dev);
723 /* abort streaming and wait for last buffer */
724 static void stop_streaming(struct vb2_queue *vq)
726 struct stk1160 *dev = vb2_get_drv_priv(vq);
727 stk1160_stop_streaming(dev);
730 static const struct vb2_ops stk1160_video_qops = {
731 .queue_setup = queue_setup,
732 .buf_queue = buffer_queue,
733 .start_streaming = start_streaming,
734 .stop_streaming = stop_streaming,
735 .wait_prepare = vb2_ops_wait_prepare,
736 .wait_finish = vb2_ops_wait_finish,
739 static const struct video_device v4l_template = {
741 .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
742 .fops = &stk1160_fops,
743 .ioctl_ops = &stk1160_ioctl_ops,
744 .release = video_device_release_empty,
747 /********************************************************************/
749 /* Must be called with both v4l_lock and vb_queue_lock hold */
750 void stk1160_clear_queue(struct stk1160 *dev)
752 struct stk1160_buffer *buf;
755 /* Release all active buffers */
756 spin_lock_irqsave(&dev->buf_lock, flags);
757 while (!list_empty(&dev->avail_bufs)) {
758 buf = list_first_entry(&dev->avail_bufs,
759 struct stk1160_buffer, list);
760 list_del(&buf->list);
761 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
762 stk1160_dbg("buffer [%p/%d] aborted\n",
763 buf, buf->vb.vb2_buf.index);
766 /* It's important to release the current buffer */
767 if (dev->isoc_ctl.buf) {
768 buf = dev->isoc_ctl.buf;
769 dev->isoc_ctl.buf = NULL;
771 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
772 stk1160_dbg("buffer [%p/%d] aborted\n",
773 buf, buf->vb.vb2_buf.index);
775 spin_unlock_irqrestore(&dev->buf_lock, flags);
778 int stk1160_vb2_setup(struct stk1160 *dev)
784 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
785 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
787 q->buf_struct_size = sizeof(struct stk1160_buffer);
788 q->ops = &stk1160_video_qops;
789 q->mem_ops = &vb2_vmalloc_memops;
790 q->lock = &dev->vb_queue_lock;
791 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
793 rc = vb2_queue_init(q);
797 /* initialize video dma queue */
798 INIT_LIST_HEAD(&dev->avail_bufs);
803 int stk1160_video_register(struct stk1160 *dev)
807 /* Initialize video_device with a template structure */
808 dev->vdev = v4l_template;
809 dev->vdev.queue = &dev->vb_vidq;
812 * Provide mutexes for v4l2 core and for videobuf2 queue.
813 * It will be used to protect *only* v4l2 ioctls.
815 dev->vdev.lock = &dev->v4l_lock;
817 /* This will be used to set video_device parent */
818 dev->vdev.v4l2_dev = &dev->v4l2_dev;
819 dev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
822 /* NTSC is default */
823 dev->norm = V4L2_STD_NTSC_M;
827 /* set default format */
828 dev->fmt = &format[0];
829 stk1160_set_std(dev);
831 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
834 video_set_drvdata(&dev->vdev, dev);
835 rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
837 stk1160_err("video_register_device failed (%d)\n", rc);
841 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
842 video_device_node_name(&dev->vdev));