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>
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/v4l2-chip-ident.h>
35 #include <media/videobuf2-vmalloc.h>
37 #include <media/saa7115.h>
40 #include "stk1160-reg.h"
42 static unsigned int vidioc_debug;
43 module_param(vidioc_debug, int, 0644);
44 MODULE_PARM_DESC(vidioc_debug, "enable debug messages [vidioc]");
46 static bool keep_buffers;
47 module_param(keep_buffers, bool, 0644);
48 MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming");
50 /* supported video standards */
51 static struct stk1160_fmt format[] = {
53 .name = "16 bpp YUY2, 4:2:2, packed",
54 .fourcc = V4L2_PIX_FMT_UYVY,
59 static void stk1160_set_std(struct stk1160 *dev)
63 static struct regval std525[] = {
68 {STK116_CFSPO_STX_L, 0x0000},
69 {STK116_CFSPO_STX_H, 0x0000},
70 {STK116_CFSPO_STY_L, 0x0003},
71 {STK116_CFSPO_STY_H, 0x0000},
74 {STK116_CFEPO_ENX_L, 0x05a0},
75 {STK116_CFEPO_ENX_H, 0x0005},
76 {STK116_CFEPO_ENY_L, 0x00f3},
77 {STK116_CFEPO_ENY_H, 0x0000},
82 static struct regval std625[] = {
86 /* TODO: Each line of frame has some junk at the end */
88 {STK116_CFSPO, 0x0000},
89 {STK116_CFSPO+1, 0x0000},
90 {STK116_CFSPO+2, 0x0001},
91 {STK116_CFSPO+3, 0x0000},
94 {STK116_CFEPO, 0x05a0},
95 {STK116_CFEPO+1, 0x0005},
96 {STK116_CFEPO+2, 0x0121},
97 {STK116_CFEPO+3, 0x0001},
102 if (dev->norm & V4L2_STD_525_60) {
103 stk1160_dbg("registers to NTSC like standard\n");
104 for (i = 0; std525[i].reg != 0xffff; i++)
105 stk1160_write_reg(dev, std525[i].reg, std525[i].val);
107 stk1160_dbg("registers to PAL like standard\n");
108 for (i = 0; std625[i].reg != 0xffff; i++)
109 stk1160_write_reg(dev, std625[i].reg, std625[i].val);
115 * Set a new alternate setting.
116 * Returns true is dev->max_pkt_size has changed, false otherwise.
118 static bool stk1160_set_alternate(struct stk1160 *dev)
120 int i, prev_alt = dev->alt;
121 unsigned int min_pkt_size;
125 * If we don't set right alternate,
126 * then we will get a green screen with junk.
128 min_pkt_size = STK1160_MIN_PKT_SIZE;
130 for (i = 0; i < dev->num_alt; i++) {
131 /* stop when the selected alt setting offers enough bandwidth */
132 if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
136 * otherwise make sure that we end up with the maximum bandwidth
137 * because the min_pkt_size equation might be wrong...
139 } else if (dev->alt_max_pkt_size[i] >
140 dev->alt_max_pkt_size[dev->alt])
144 stk1160_info("setting alternate %d\n", dev->alt);
146 if (dev->alt != prev_alt) {
147 stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n",
148 min_pkt_size, dev->alt);
149 stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n",
150 dev->alt, dev->alt_max_pkt_size[dev->alt]);
151 usb_set_interface(dev->udev, 0, dev->alt);
154 new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt];
155 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
160 static int stk1160_start_streaming(struct stk1160 *dev)
166 /* Check device presence */
170 if (mutex_lock_interruptible(&dev->v4l_lock))
173 * For some reason it is mandatory to set alternate *first*
174 * and only *then* initialize isoc urbs.
175 * Someone please explain me why ;)
177 new_pkt_size = stk1160_set_alternate(dev);
180 * We (re)allocate isoc urbs if:
181 * there is no allocated isoc urbs, OR
182 * a new dev->max_pkt_size is detected
184 if (!dev->isoc_ctl.num_bufs || new_pkt_size) {
185 rc = stk1160_alloc_isoc(dev);
190 /* submit urbs and enables IRQ */
191 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
192 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL);
194 stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
200 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
203 stk1160_write_reg(dev, STK1160_DCTRL, 0xb3);
204 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
206 stk1160_dbg("streaming started\n");
208 mutex_unlock(&dev->v4l_lock);
213 stk1160_uninit_isoc(dev);
215 usb_set_interface(dev->udev, 0, 0);
216 stk1160_clear_queue(dev);
218 mutex_unlock(&dev->v4l_lock);
223 /* Must be called with v4l_lock hold */
224 static void stk1160_stop_hw(struct stk1160 *dev)
226 /* If the device is not physically present, there is nothing to do */
230 /* set alternate 0 */
232 stk1160_info("setting alternate %d\n", dev->alt);
233 usb_set_interface(dev->udev, 0, 0);
236 stk1160_write_reg(dev, STK1160_DCTRL, 0x00);
237 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
240 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
243 static int stk1160_stop_streaming(struct stk1160 *dev)
245 if (mutex_lock_interruptible(&dev->v4l_lock))
248 stk1160_cancel_isoc(dev);
251 * It is possible to keep buffers around using a module parameter.
252 * This is intended to avoid memory fragmentation.
255 stk1160_free_isoc(dev);
257 stk1160_stop_hw(dev);
259 stk1160_clear_queue(dev);
261 stk1160_dbg("streaming stopped\n");
263 mutex_unlock(&dev->v4l_lock);
268 static struct v4l2_file_operations stk1160_fops = {
269 .owner = THIS_MODULE,
270 .open = v4l2_fh_open,
271 .release = vb2_fop_release,
272 .read = vb2_fop_read,
273 .poll = vb2_fop_poll,
274 .mmap = vb2_fop_mmap,
275 .unlocked_ioctl = video_ioctl2,
281 static int vidioc_querycap(struct file *file,
282 void *priv, struct v4l2_capability *cap)
284 struct stk1160 *dev = video_drvdata(file);
286 strcpy(cap->driver, "stk1160");
287 strcpy(cap->card, "stk1160");
288 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
290 V4L2_CAP_VIDEO_CAPTURE |
293 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
297 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
298 struct v4l2_fmtdesc *f)
303 strlcpy(f->description, format[f->index].name, sizeof(f->description));
304 f->pixelformat = format[f->index].fourcc;
308 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
309 struct v4l2_format *f)
311 struct stk1160 *dev = video_drvdata(file);
313 f->fmt.pix.width = dev->width;
314 f->fmt.pix.height = dev->height;
315 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
316 f->fmt.pix.pixelformat = dev->fmt->fourcc;
317 f->fmt.pix.bytesperline = dev->width * 2;
318 f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
319 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
324 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
325 struct v4l2_format *f)
327 struct stk1160 *dev = video_drvdata(file);
330 * User can't choose size at his own will,
331 * so we just return him the current size chosen
332 * at standard selection.
333 * TODO: Implement frame scaling?
336 f->fmt.pix.pixelformat = dev->fmt->fourcc;
337 f->fmt.pix.width = dev->width;
338 f->fmt.pix.height = dev->height;
339 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
340 f->fmt.pix.bytesperline = dev->width * 2;
341 f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
342 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
347 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
348 struct v4l2_format *f)
350 struct stk1160 *dev = video_drvdata(file);
351 struct vb2_queue *q = &dev->vb_vidq;
356 vidioc_try_fmt_vid_cap(file, priv, f);
358 /* We don't support any format changes */
363 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
365 struct stk1160 *dev = video_drvdata(file);
366 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
370 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
372 struct stk1160 *dev = video_drvdata(file);
378 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
380 struct stk1160 *dev = video_drvdata(file);
381 struct vb2_queue *q = &dev->vb_vidq;
386 /* Check device presence */
390 /* We need to set this now, before we call stk1160_set_std */
393 /* This is taken from saa7115 video decoder */
394 if (dev->norm & V4L2_STD_525_60) {
397 } else if (dev->norm & V4L2_STD_625_50) {
401 stk1160_err("invalid standard\n");
405 stk1160_set_std(dev);
407 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std,
414 static int vidioc_enum_input(struct file *file, void *priv,
415 struct v4l2_input *i)
417 struct stk1160 *dev = video_drvdata(file);
419 if (i->index > STK1160_MAX_INPUT)
422 /* S-Video special handling */
423 if (i->index == STK1160_SVIDEO_INPUT)
424 sprintf(i->name, "S-Video");
426 sprintf(i->name, "Composite%d", i->index);
428 i->type = V4L2_INPUT_TYPE_CAMERA;
429 i->std = dev->vdev.tvnorms;
433 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
435 struct stk1160 *dev = video_drvdata(file);
440 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
442 struct stk1160 *dev = video_drvdata(file);
444 if (vb2_is_busy(&dev->vb_vidq))
447 if (i > STK1160_MAX_INPUT)
452 stk1160_select_input(dev);
457 static int vidioc_g_chip_ident(struct file *file, void *priv,
458 struct v4l2_dbg_chip_ident *chip)
460 switch (chip->match.type) {
461 case V4L2_CHIP_MATCH_BRIDGE:
462 chip->ident = V4L2_IDENT_NONE;
470 #ifdef CONFIG_VIDEO_ADV_DEBUG
471 static int vidioc_g_register(struct file *file, void *priv,
472 struct v4l2_dbg_register *reg)
474 struct stk1160 *dev = video_drvdata(file);
478 switch (reg->match.type) {
479 case V4L2_CHIP_MATCH_I2C_DRIVER:
480 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
482 case V4L2_CHIP_MATCH_I2C_ADDR:
483 /* TODO: is this correct? */
484 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
487 if (!v4l2_chip_match_host(®->match))
492 rc = stk1160_read_reg(dev, reg->reg, &val);
499 static int vidioc_s_register(struct file *file, void *priv,
500 const struct v4l2_dbg_register *reg)
502 struct stk1160 *dev = video_drvdata(file);
504 switch (reg->match.type) {
505 case V4L2_CHIP_MATCH_I2C_DRIVER:
506 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
508 case V4L2_CHIP_MATCH_I2C_ADDR:
509 /* TODO: is this correct? */
510 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
513 if (!v4l2_chip_match_host(®->match))
518 return stk1160_write_reg(dev, reg->reg, cpu_to_le16(reg->val));
522 static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
523 .vidioc_querycap = vidioc_querycap,
524 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
525 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
526 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
527 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
528 .vidioc_querystd = vidioc_querystd,
529 .vidioc_g_std = vidioc_g_std,
530 .vidioc_s_std = vidioc_s_std,
531 .vidioc_enum_input = vidioc_enum_input,
532 .vidioc_g_input = vidioc_g_input,
533 .vidioc_s_input = vidioc_s_input,
535 /* vb2 takes care of these */
536 .vidioc_reqbufs = vb2_ioctl_reqbufs,
537 .vidioc_querybuf = vb2_ioctl_querybuf,
538 .vidioc_qbuf = vb2_ioctl_qbuf,
539 .vidioc_dqbuf = vb2_ioctl_dqbuf,
540 .vidioc_streamon = vb2_ioctl_streamon,
541 .vidioc_streamoff = vb2_ioctl_streamoff,
543 .vidioc_log_status = v4l2_ctrl_log_status,
544 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
545 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
546 .vidioc_g_chip_ident = vidioc_g_chip_ident,
548 #ifdef CONFIG_VIDEO_ADV_DEBUG
549 .vidioc_g_register = vidioc_g_register,
550 .vidioc_s_register = vidioc_s_register,
554 /********************************************************************/
557 * Videobuf2 operations
559 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt,
560 unsigned int *nbuffers, unsigned int *nplanes,
561 unsigned int sizes[], void *alloc_ctxs[])
563 struct stk1160 *dev = vb2_get_drv_priv(vq);
566 size = dev->width * dev->height * 2;
569 * Here we can change the number of buffers being requested.
570 * So, we set a minimum and a maximum like this:
572 *nbuffers = clamp_t(unsigned int, *nbuffers,
573 STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
575 /* This means a packed colorformat */
580 stk1160_info("%s: buffer count %d, each %ld bytes\n",
581 __func__, *nbuffers, size);
586 static void buffer_queue(struct vb2_buffer *vb)
589 struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
590 struct stk1160_buffer *buf =
591 container_of(vb, struct stk1160_buffer, vb);
593 spin_lock_irqsave(&dev->buf_lock, flags);
596 * If the device is disconnected return the buffer to userspace
597 * directly. The next QBUF call will fail with -ENODEV.
599 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
602 buf->mem = vb2_plane_vaddr(vb, 0);
603 buf->length = vb2_plane_size(vb, 0);
608 * If buffer length is less from expected then we return
609 * the buffer to userspace directly.
611 if (buf->length < dev->width * dev->height * 2)
612 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
614 list_add_tail(&buf->list, &dev->avail_bufs);
617 spin_unlock_irqrestore(&dev->buf_lock, flags);
620 static int start_streaming(struct vb2_queue *vq, unsigned int count)
622 struct stk1160 *dev = vb2_get_drv_priv(vq);
623 return stk1160_start_streaming(dev);
626 /* abort streaming and wait for last buffer */
627 static int stop_streaming(struct vb2_queue *vq)
629 struct stk1160 *dev = vb2_get_drv_priv(vq);
630 return stk1160_stop_streaming(dev);
633 static struct vb2_ops stk1160_video_qops = {
634 .queue_setup = queue_setup,
635 .buf_queue = buffer_queue,
636 .start_streaming = start_streaming,
637 .stop_streaming = stop_streaming,
638 .wait_prepare = vb2_ops_wait_prepare,
639 .wait_finish = vb2_ops_wait_finish,
642 static struct video_device v4l_template = {
644 .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
645 .fops = &stk1160_fops,
646 .ioctl_ops = &stk1160_ioctl_ops,
647 .release = video_device_release_empty,
650 /********************************************************************/
652 /* Must be called with both v4l_lock and vb_queue_lock hold */
653 void stk1160_clear_queue(struct stk1160 *dev)
655 struct stk1160_buffer *buf;
658 /* Release all active buffers */
659 spin_lock_irqsave(&dev->buf_lock, flags);
660 while (!list_empty(&dev->avail_bufs)) {
661 buf = list_first_entry(&dev->avail_bufs,
662 struct stk1160_buffer, list);
663 list_del(&buf->list);
664 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
665 stk1160_info("buffer [%p/%d] aborted\n",
666 buf, buf->vb.v4l2_buf.index);
668 /* It's important to clear current buffer */
669 dev->isoc_ctl.buf = NULL;
670 spin_unlock_irqrestore(&dev->buf_lock, flags);
673 int stk1160_vb2_setup(struct stk1160 *dev)
679 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
680 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
682 q->buf_struct_size = sizeof(struct stk1160_buffer);
683 q->ops = &stk1160_video_qops;
684 q->mem_ops = &vb2_vmalloc_memops;
685 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
687 rc = vb2_queue_init(q);
691 /* initialize video dma queue */
692 INIT_LIST_HEAD(&dev->avail_bufs);
697 int stk1160_video_register(struct stk1160 *dev)
701 /* Initialize video_device with a template structure */
702 dev->vdev = v4l_template;
703 dev->vdev.debug = vidioc_debug;
704 dev->vdev.queue = &dev->vb_vidq;
707 * Provide mutexes for v4l2 core and for videobuf2 queue.
708 * It will be used to protect *only* v4l2 ioctls.
710 dev->vdev.lock = &dev->v4l_lock;
711 dev->vdev.queue->lock = &dev->vb_queue_lock;
713 /* This will be used to set video_device parent */
714 dev->vdev.v4l2_dev = &dev->v4l2_dev;
715 set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
717 /* NTSC is default */
718 dev->norm = V4L2_STD_NTSC_M;
722 /* set default format */
723 dev->fmt = &format[0];
724 stk1160_set_std(dev);
726 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std,
729 video_set_drvdata(&dev->vdev, dev);
730 rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
732 stk1160_err("video_register_device failed (%d)\n", rc);
736 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
737 video_device_node_name(&dev->vdev));