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/videobuf2-vmalloc.h>
36 #include <media/saa7115.h>
39 #include "stk1160-reg.h"
41 static unsigned int vidioc_debug
;
42 module_param(vidioc_debug
, int, 0644);
43 MODULE_PARM_DESC(vidioc_debug
, "enable debug messages [vidioc]");
45 static bool keep_buffers
;
46 module_param(keep_buffers
, bool, 0644);
47 MODULE_PARM_DESC(keep_buffers
, "don't release buffers upon stop streaming");
49 /* supported video standards */
50 static struct stk1160_fmt format
[] = {
52 .name
= "16 bpp YUY2, 4:2:2, packed",
53 .fourcc
= V4L2_PIX_FMT_UYVY
,
58 static void stk1160_set_std(struct stk1160
*dev
)
62 static struct regval std525
[] = {
67 {STK116_CFSPO_STX_L
, 0x0000},
68 {STK116_CFSPO_STX_H
, 0x0000},
69 {STK116_CFSPO_STY_L
, 0x0003},
70 {STK116_CFSPO_STY_H
, 0x0000},
73 {STK116_CFEPO_ENX_L
, 0x05a0},
74 {STK116_CFEPO_ENX_H
, 0x0005},
75 {STK116_CFEPO_ENY_L
, 0x00f3},
76 {STK116_CFEPO_ENY_H
, 0x0000},
81 static struct regval std625
[] = {
85 /* TODO: Each line of frame has some junk at the end */
87 {STK116_CFSPO
, 0x0000},
88 {STK116_CFSPO
+1, 0x0000},
89 {STK116_CFSPO
+2, 0x0001},
90 {STK116_CFSPO
+3, 0x0000},
93 {STK116_CFEPO
, 0x05a0},
94 {STK116_CFEPO
+1, 0x0005},
95 {STK116_CFEPO
+2, 0x0121},
96 {STK116_CFEPO
+3, 0x0001},
101 if (dev
->norm
& V4L2_STD_525_60
) {
102 stk1160_dbg("registers to NTSC like standard\n");
103 for (i
= 0; std525
[i
].reg
!= 0xffff; i
++)
104 stk1160_write_reg(dev
, std525
[i
].reg
, std525
[i
].val
);
106 stk1160_dbg("registers to PAL like standard\n");
107 for (i
= 0; std625
[i
].reg
!= 0xffff; i
++)
108 stk1160_write_reg(dev
, std625
[i
].reg
, std625
[i
].val
);
114 * Set a new alternate setting.
115 * Returns true is dev->max_pkt_size has changed, false otherwise.
117 static bool stk1160_set_alternate(struct stk1160
*dev
)
119 int i
, prev_alt
= dev
->alt
;
120 unsigned int min_pkt_size
;
124 * If we don't set right alternate,
125 * then we will get a green screen with junk.
127 min_pkt_size
= STK1160_MIN_PKT_SIZE
;
129 for (i
= 0; i
< dev
->num_alt
; i
++) {
130 /* stop when the selected alt setting offers enough bandwidth */
131 if (dev
->alt_max_pkt_size
[i
] >= min_pkt_size
) {
135 * otherwise make sure that we end up with the maximum bandwidth
136 * because the min_pkt_size equation might be wrong...
138 } else if (dev
->alt_max_pkt_size
[i
] >
139 dev
->alt_max_pkt_size
[dev
->alt
])
143 stk1160_info("setting alternate %d\n", dev
->alt
);
145 if (dev
->alt
!= prev_alt
) {
146 stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n",
147 min_pkt_size
, dev
->alt
);
148 stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n",
149 dev
->alt
, dev
->alt_max_pkt_size
[dev
->alt
]);
150 usb_set_interface(dev
->udev
, 0, dev
->alt
);
153 new_pkt_size
= dev
->max_pkt_size
!= dev
->alt_max_pkt_size
[dev
->alt
];
154 dev
->max_pkt_size
= dev
->alt_max_pkt_size
[dev
->alt
];
159 static int stk1160_start_streaming(struct stk1160
*dev
)
165 /* Check device presence */
169 if (mutex_lock_interruptible(&dev
->v4l_lock
))
172 * For some reason it is mandatory to set alternate *first*
173 * and only *then* initialize isoc urbs.
174 * Someone please explain me why ;)
176 new_pkt_size
= stk1160_set_alternate(dev
);
179 * We (re)allocate isoc urbs if:
180 * there is no allocated isoc urbs, OR
181 * a new dev->max_pkt_size is detected
183 if (!dev
->isoc_ctl
.num_bufs
|| new_pkt_size
) {
184 rc
= stk1160_alloc_isoc(dev
);
189 /* submit urbs and enables IRQ */
190 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
191 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_KERNEL
);
193 stk1160_err("cannot submit urb[%d] (%d)\n", i
, rc
);
199 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 1);
202 stk1160_write_reg(dev
, STK1160_DCTRL
, 0xb3);
203 stk1160_write_reg(dev
, STK1160_DCTRL
+3, 0x00);
205 stk1160_dbg("streaming started\n");
207 mutex_unlock(&dev
->v4l_lock
);
212 stk1160_uninit_isoc(dev
);
214 usb_set_interface(dev
->udev
, 0, 0);
215 stk1160_clear_queue(dev
);
217 mutex_unlock(&dev
->v4l_lock
);
222 /* Must be called with v4l_lock hold */
223 static void stk1160_stop_hw(struct stk1160
*dev
)
225 /* If the device is not physically present, there is nothing to do */
229 /* set alternate 0 */
231 stk1160_info("setting alternate %d\n", dev
->alt
);
232 usb_set_interface(dev
->udev
, 0, 0);
235 stk1160_write_reg(dev
, STK1160_DCTRL
, 0x00);
236 stk1160_write_reg(dev
, STK1160_DCTRL
+3, 0x00);
239 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 0);
242 static int stk1160_stop_streaming(struct stk1160
*dev
)
244 if (mutex_lock_interruptible(&dev
->v4l_lock
))
247 stk1160_cancel_isoc(dev
);
250 * It is possible to keep buffers around using a module parameter.
251 * This is intended to avoid memory fragmentation.
254 stk1160_free_isoc(dev
);
256 stk1160_stop_hw(dev
);
258 stk1160_clear_queue(dev
);
260 stk1160_dbg("streaming stopped\n");
262 mutex_unlock(&dev
->v4l_lock
);
267 static struct v4l2_file_operations stk1160_fops
= {
268 .owner
= THIS_MODULE
,
269 .open
= v4l2_fh_open
,
270 .release
= vb2_fop_release
,
271 .read
= vb2_fop_read
,
272 .poll
= vb2_fop_poll
,
273 .mmap
= vb2_fop_mmap
,
274 .unlocked_ioctl
= video_ioctl2
,
280 static int vidioc_querycap(struct file
*file
,
281 void *priv
, struct v4l2_capability
*cap
)
283 struct stk1160
*dev
= video_drvdata(file
);
285 strcpy(cap
->driver
, "stk1160");
286 strcpy(cap
->card
, "stk1160");
287 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
289 V4L2_CAP_VIDEO_CAPTURE
|
292 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
296 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
297 struct v4l2_fmtdesc
*f
)
302 strlcpy(f
->description
, format
[f
->index
].name
, sizeof(f
->description
));
303 f
->pixelformat
= format
[f
->index
].fourcc
;
307 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
308 struct v4l2_format
*f
)
310 struct stk1160
*dev
= video_drvdata(file
);
312 f
->fmt
.pix
.width
= dev
->width
;
313 f
->fmt
.pix
.height
= dev
->height
;
314 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
315 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
316 f
->fmt
.pix
.bytesperline
= dev
->width
* 2;
317 f
->fmt
.pix
.sizeimage
= dev
->height
* f
->fmt
.pix
.bytesperline
;
318 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
323 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
324 struct v4l2_format
*f
)
326 struct stk1160
*dev
= video_drvdata(file
);
329 * User can't choose size at his own will,
330 * so we just return him the current size chosen
331 * at standard selection.
332 * TODO: Implement frame scaling?
335 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
336 f
->fmt
.pix
.width
= dev
->width
;
337 f
->fmt
.pix
.height
= dev
->height
;
338 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
339 f
->fmt
.pix
.bytesperline
= dev
->width
* 2;
340 f
->fmt
.pix
.sizeimage
= dev
->height
* f
->fmt
.pix
.bytesperline
;
341 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
346 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
347 struct v4l2_format
*f
)
349 struct stk1160
*dev
= video_drvdata(file
);
350 struct vb2_queue
*q
= &dev
->vb_vidq
;
355 vidioc_try_fmt_vid_cap(file
, priv
, f
);
357 /* We don't support any format changes */
362 static int vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
364 struct stk1160
*dev
= video_drvdata(file
);
365 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, querystd
, norm
);
369 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
371 struct stk1160
*dev
= video_drvdata(file
);
377 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
379 struct stk1160
*dev
= video_drvdata(file
);
380 struct vb2_queue
*q
= &dev
->vb_vidq
;
385 /* Check device presence */
389 /* We need to set this now, before we call stk1160_set_std */
392 /* This is taken from saa7115 video decoder */
393 if (dev
->norm
& V4L2_STD_525_60
) {
396 } else if (dev
->norm
& V4L2_STD_625_50
) {
400 stk1160_err("invalid standard\n");
404 stk1160_set_std(dev
);
406 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
,
413 static int vidioc_enum_input(struct file
*file
, void *priv
,
414 struct v4l2_input
*i
)
416 struct stk1160
*dev
= video_drvdata(file
);
418 if (i
->index
> STK1160_MAX_INPUT
)
421 /* S-Video special handling */
422 if (i
->index
== STK1160_SVIDEO_INPUT
)
423 sprintf(i
->name
, "S-Video");
425 sprintf(i
->name
, "Composite%d", i
->index
);
427 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
428 i
->std
= dev
->vdev
.tvnorms
;
432 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
434 struct stk1160
*dev
= video_drvdata(file
);
439 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
441 struct stk1160
*dev
= video_drvdata(file
);
443 if (vb2_is_busy(&dev
->vb_vidq
))
446 if (i
> STK1160_MAX_INPUT
)
451 stk1160_select_input(dev
);
456 #ifdef CONFIG_VIDEO_ADV_DEBUG
457 static int vidioc_g_register(struct file
*file
, void *priv
,
458 struct v4l2_dbg_register
*reg
)
460 struct stk1160
*dev
= video_drvdata(file
);
465 rc
= stk1160_read_reg(dev
, reg
->reg
, &val
);
472 static int vidioc_s_register(struct file
*file
, void *priv
,
473 const struct v4l2_dbg_register
*reg
)
475 struct stk1160
*dev
= video_drvdata(file
);
478 return stk1160_write_reg(dev
, reg
->reg
, cpu_to_le16(reg
->val
));
482 static const struct v4l2_ioctl_ops stk1160_ioctl_ops
= {
483 .vidioc_querycap
= vidioc_querycap
,
484 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
485 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
486 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
487 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
488 .vidioc_querystd
= vidioc_querystd
,
489 .vidioc_g_std
= vidioc_g_std
,
490 .vidioc_s_std
= vidioc_s_std
,
491 .vidioc_enum_input
= vidioc_enum_input
,
492 .vidioc_g_input
= vidioc_g_input
,
493 .vidioc_s_input
= vidioc_s_input
,
495 /* vb2 takes care of these */
496 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
497 .vidioc_querybuf
= vb2_ioctl_querybuf
,
498 .vidioc_qbuf
= vb2_ioctl_qbuf
,
499 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
500 .vidioc_streamon
= vb2_ioctl_streamon
,
501 .vidioc_streamoff
= vb2_ioctl_streamoff
,
503 .vidioc_log_status
= v4l2_ctrl_log_status
,
504 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
505 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
507 #ifdef CONFIG_VIDEO_ADV_DEBUG
508 .vidioc_g_register
= vidioc_g_register
,
509 .vidioc_s_register
= vidioc_s_register
,
513 /********************************************************************/
516 * Videobuf2 operations
518 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*v4l_fmt
,
519 unsigned int *nbuffers
, unsigned int *nplanes
,
520 unsigned int sizes
[], void *alloc_ctxs
[])
522 struct stk1160
*dev
= vb2_get_drv_priv(vq
);
525 size
= dev
->width
* dev
->height
* 2;
528 * Here we can change the number of buffers being requested.
529 * So, we set a minimum and a maximum like this:
531 *nbuffers
= clamp_t(unsigned int, *nbuffers
,
532 STK1160_MIN_VIDEO_BUFFERS
, STK1160_MAX_VIDEO_BUFFERS
);
534 /* This means a packed colorformat */
539 stk1160_info("%s: buffer count %d, each %ld bytes\n",
540 __func__
, *nbuffers
, size
);
545 static void buffer_queue(struct vb2_buffer
*vb
)
548 struct stk1160
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
549 struct stk1160_buffer
*buf
=
550 container_of(vb
, struct stk1160_buffer
, vb
);
552 spin_lock_irqsave(&dev
->buf_lock
, flags
);
555 * If the device is disconnected return the buffer to userspace
556 * directly. The next QBUF call will fail with -ENODEV.
558 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
561 buf
->mem
= vb2_plane_vaddr(vb
, 0);
562 buf
->length
= vb2_plane_size(vb
, 0);
567 * If buffer length is less from expected then we return
568 * the buffer to userspace directly.
570 if (buf
->length
< dev
->width
* dev
->height
* 2)
571 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
573 list_add_tail(&buf
->list
, &dev
->avail_bufs
);
576 spin_unlock_irqrestore(&dev
->buf_lock
, flags
);
579 static int start_streaming(struct vb2_queue
*vq
, unsigned int count
)
581 struct stk1160
*dev
= vb2_get_drv_priv(vq
);
582 return stk1160_start_streaming(dev
);
585 /* abort streaming and wait for last buffer */
586 static int stop_streaming(struct vb2_queue
*vq
)
588 struct stk1160
*dev
= vb2_get_drv_priv(vq
);
589 return stk1160_stop_streaming(dev
);
592 static struct vb2_ops stk1160_video_qops
= {
593 .queue_setup
= queue_setup
,
594 .buf_queue
= buffer_queue
,
595 .start_streaming
= start_streaming
,
596 .stop_streaming
= stop_streaming
,
597 .wait_prepare
= vb2_ops_wait_prepare
,
598 .wait_finish
= vb2_ops_wait_finish
,
601 static struct video_device v4l_template
= {
603 .tvnorms
= V4L2_STD_525_60
| V4L2_STD_625_50
,
604 .fops
= &stk1160_fops
,
605 .ioctl_ops
= &stk1160_ioctl_ops
,
606 .release
= video_device_release_empty
,
609 /********************************************************************/
611 /* Must be called with both v4l_lock and vb_queue_lock hold */
612 void stk1160_clear_queue(struct stk1160
*dev
)
614 struct stk1160_buffer
*buf
;
617 /* Release all active buffers */
618 spin_lock_irqsave(&dev
->buf_lock
, flags
);
619 while (!list_empty(&dev
->avail_bufs
)) {
620 buf
= list_first_entry(&dev
->avail_bufs
,
621 struct stk1160_buffer
, list
);
622 list_del(&buf
->list
);
623 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
624 stk1160_info("buffer [%p/%d] aborted\n",
625 buf
, buf
->vb
.v4l2_buf
.index
);
627 /* It's important to clear current buffer */
628 dev
->isoc_ctl
.buf
= NULL
;
629 spin_unlock_irqrestore(&dev
->buf_lock
, flags
);
632 int stk1160_vb2_setup(struct stk1160
*dev
)
638 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
639 q
->io_modes
= VB2_READ
| VB2_MMAP
| VB2_USERPTR
;
641 q
->buf_struct_size
= sizeof(struct stk1160_buffer
);
642 q
->ops
= &stk1160_video_qops
;
643 q
->mem_ops
= &vb2_vmalloc_memops
;
644 q
->timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
646 rc
= vb2_queue_init(q
);
650 /* initialize video dma queue */
651 INIT_LIST_HEAD(&dev
->avail_bufs
);
656 int stk1160_video_register(struct stk1160
*dev
)
660 /* Initialize video_device with a template structure */
661 dev
->vdev
= v4l_template
;
662 dev
->vdev
.debug
= vidioc_debug
;
663 dev
->vdev
.queue
= &dev
->vb_vidq
;
666 * Provide mutexes for v4l2 core and for videobuf2 queue.
667 * It will be used to protect *only* v4l2 ioctls.
669 dev
->vdev
.lock
= &dev
->v4l_lock
;
670 dev
->vdev
.queue
->lock
= &dev
->vb_queue_lock
;
672 /* This will be used to set video_device parent */
673 dev
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
674 set_bit(V4L2_FL_USE_FH_PRIO
, &dev
->vdev
.flags
);
676 /* NTSC is default */
677 dev
->norm
= V4L2_STD_NTSC_M
;
681 /* set default format */
682 dev
->fmt
= &format
[0];
683 stk1160_set_std(dev
);
685 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
,
688 video_set_drvdata(&dev
->vdev
, dev
);
689 rc
= video_register_device(&dev
->vdev
, VFL_TYPE_GRABBER
, -1);
691 stk1160_err("video_register_device failed (%d)\n", rc
);
695 v4l2_info(&dev
->v4l2_dev
, "V4L2 device registered as %s\n",
696 video_device_node_name(&dev
->vdev
));