blob: b4b737b0b35a7dd3a40c8e13445722626b16e031 [file] [log] [blame]
Ezequiel García9cb21732012-08-11 14:32:57 -03001/*
2 * STK1160 driver
3 *
4 * Copyright (C) 2012 Ezequiel Garcia
5 * <elezegarcia--a.t--gmail.com>
6 *
7 * Based on Easycap driver by R.M. Thomas
8 * Copyright (C) 2010 R.M. Thomas
9 * <rmthomas--a.t--sciolus.org>
10 *
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.
15 *
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.
20 *
21 */
22
23#include <linux/module.h>
24#include <linux/usb.h>
25#include <linux/mm.h>
26#include <linux/slab.h>
27
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>
Ezequiel García9cb21732012-08-11 14:32:57 -030034#include <media/videobuf2-vmalloc.h>
35
36#include <media/saa7115.h>
37
38#include "stk1160.h"
39#include "stk1160-reg.h"
40
Ezequiel García9cb21732012-08-11 14:32:57 -030041static bool keep_buffers;
42module_param(keep_buffers, bool, 0644);
43MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming");
44
45/* supported video standards */
46static struct stk1160_fmt format[] = {
47 {
48 .name = "16 bpp YUY2, 4:2:2, packed",
49 .fourcc = V4L2_PIX_FMT_UYVY,
50 .depth = 16,
51 }
52};
53
54static void stk1160_set_std(struct stk1160 *dev)
55{
56 int i;
57
58 static struct regval std525[] = {
59
60 /* 720x480 */
61
62 /* Frame start */
63 {STK116_CFSPO_STX_L, 0x0000},
64 {STK116_CFSPO_STX_H, 0x0000},
65 {STK116_CFSPO_STY_L, 0x0003},
66 {STK116_CFSPO_STY_H, 0x0000},
67
68 /* Frame end */
69 {STK116_CFEPO_ENX_L, 0x05a0},
70 {STK116_CFEPO_ENX_H, 0x0005},
71 {STK116_CFEPO_ENY_L, 0x00f3},
72 {STK116_CFEPO_ENY_H, 0x0000},
73
74 {0xffff, 0xffff}
75 };
76
77 static struct regval std625[] = {
78
79 /* 720x576 */
80
81 /* TODO: Each line of frame has some junk at the end */
82 /* Frame start */
83 {STK116_CFSPO, 0x0000},
84 {STK116_CFSPO+1, 0x0000},
85 {STK116_CFSPO+2, 0x0001},
86 {STK116_CFSPO+3, 0x0000},
87
88 /* Frame end */
89 {STK116_CFEPO, 0x05a0},
90 {STK116_CFEPO+1, 0x0005},
91 {STK116_CFEPO+2, 0x0121},
92 {STK116_CFEPO+3, 0x0001},
93
94 {0xffff, 0xffff}
95 };
96
97 if (dev->norm & V4L2_STD_525_60) {
98 stk1160_dbg("registers to NTSC like standard\n");
99 for (i = 0; std525[i].reg != 0xffff; i++)
100 stk1160_write_reg(dev, std525[i].reg, std525[i].val);
101 } else {
102 stk1160_dbg("registers to PAL like standard\n");
103 for (i = 0; std625[i].reg != 0xffff; i++)
104 stk1160_write_reg(dev, std625[i].reg, std625[i].val);
105 }
106
107}
108
109/*
110 * Set a new alternate setting.
111 * Returns true is dev->max_pkt_size has changed, false otherwise.
112 */
113static bool stk1160_set_alternate(struct stk1160 *dev)
114{
115 int i, prev_alt = dev->alt;
116 unsigned int min_pkt_size;
117 bool new_pkt_size;
118
119 /*
120 * If we don't set right alternate,
121 * then we will get a green screen with junk.
122 */
123 min_pkt_size = STK1160_MIN_PKT_SIZE;
124
125 for (i = 0; i < dev->num_alt; i++) {
126 /* stop when the selected alt setting offers enough bandwidth */
127 if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
128 dev->alt = i;
129 break;
130 /*
131 * otherwise make sure that we end up with the maximum bandwidth
132 * because the min_pkt_size equation might be wrong...
133 */
134 } else if (dev->alt_max_pkt_size[i] >
135 dev->alt_max_pkt_size[dev->alt])
136 dev->alt = i;
137 }
138
Ezequiel Garcia890024a2015-07-03 16:11:41 -0300139 stk1160_dbg("setting alternate %d\n", dev->alt);
Ezequiel García9cb21732012-08-11 14:32:57 -0300140
141 if (dev->alt != prev_alt) {
142 stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n",
143 min_pkt_size, dev->alt);
144 stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n",
145 dev->alt, dev->alt_max_pkt_size[dev->alt]);
146 usb_set_interface(dev->udev, 0, dev->alt);
147 }
148
149 new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt];
150 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
151
152 return new_pkt_size;
153}
154
155static int stk1160_start_streaming(struct stk1160 *dev)
156{
Ezequiel García9cb21732012-08-11 14:32:57 -0300157 bool new_pkt_size;
Dan Carpenterf367cc12012-08-14 02:59:48 -0300158 int rc = 0;
159 int i;
Ezequiel García9cb21732012-08-11 14:32:57 -0300160
161 /* Check device presence */
162 if (!dev->udev)
163 return -ENODEV;
164
165 if (mutex_lock_interruptible(&dev->v4l_lock))
166 return -ERESTARTSYS;
167 /*
168 * For some reason it is mandatory to set alternate *first*
169 * and only *then* initialize isoc urbs.
170 * Someone please explain me why ;)
171 */
172 new_pkt_size = stk1160_set_alternate(dev);
173
174 /*
175 * We (re)allocate isoc urbs if:
176 * there is no allocated isoc urbs, OR
177 * a new dev->max_pkt_size is detected
178 */
179 if (!dev->isoc_ctl.num_bufs || new_pkt_size) {
180 rc = stk1160_alloc_isoc(dev);
181 if (rc < 0)
Ezequiel Garcia8ac45642012-08-19 21:23:46 -0300182 goto out_stop_hw;
Ezequiel García9cb21732012-08-11 14:32:57 -0300183 }
184
185 /* submit urbs and enables IRQ */
186 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
187 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL);
188 if (rc) {
189 stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
Ezequiel Garcia8ac45642012-08-19 21:23:46 -0300190 goto out_uninit;
Ezequiel García9cb21732012-08-11 14:32:57 -0300191 }
192 }
193
194 /* Start saa711x */
195 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
196
Hans Verkuile3e30f62015-06-08 06:53:59 -0300197 dev->sequence = 0;
198
Ezequiel García9cb21732012-08-11 14:32:57 -0300199 /* Start stk1160 */
200 stk1160_write_reg(dev, STK1160_DCTRL, 0xb3);
201 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
202
203 stk1160_dbg("streaming started\n");
204
Ezequiel Garcia8ac45642012-08-19 21:23:46 -0300205 mutex_unlock(&dev->v4l_lock);
206
207 return 0;
208
209out_uninit:
210 stk1160_uninit_isoc(dev);
211out_stop_hw:
212 usb_set_interface(dev->udev, 0, 0);
213 stk1160_clear_queue(dev);
214
Ezequiel García9cb21732012-08-11 14:32:57 -0300215 mutex_unlock(&dev->v4l_lock);
216
Dan Carpenterf367cc12012-08-14 02:59:48 -0300217 return rc;
Ezequiel García9cb21732012-08-11 14:32:57 -0300218}
219
220/* Must be called with v4l_lock hold */
221static void stk1160_stop_hw(struct stk1160 *dev)
222{
223 /* If the device is not physically present, there is nothing to do */
224 if (!dev->udev)
225 return;
226
227 /* set alternate 0 */
228 dev->alt = 0;
Ezequiel Garcia890024a2015-07-03 16:11:41 -0300229 stk1160_dbg("setting alternate %d\n", dev->alt);
Ezequiel García9cb21732012-08-11 14:32:57 -0300230 usb_set_interface(dev->udev, 0, 0);
231
232 /* Stop stk1160 */
233 stk1160_write_reg(dev, STK1160_DCTRL, 0x00);
234 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
235
236 /* Stop saa711x */
237 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
238}
239
240static int stk1160_stop_streaming(struct stk1160 *dev)
241{
242 if (mutex_lock_interruptible(&dev->v4l_lock))
243 return -ERESTARTSYS;
244
Ezequiel Garciaaeff0922015-03-10 11:37:14 -0300245 /*
246 * Once URBs are cancelled, the URB complete handler
247 * won't be running. This is required to safely release the
248 * current buffer (dev->isoc_ctl.buf).
249 */
Ezequiel García9cb21732012-08-11 14:32:57 -0300250 stk1160_cancel_isoc(dev);
251
252 /*
253 * It is possible to keep buffers around using a module parameter.
254 * This is intended to avoid memory fragmentation.
255 */
256 if (!keep_buffers)
257 stk1160_free_isoc(dev);
258
259 stk1160_stop_hw(dev);
260
261 stk1160_clear_queue(dev);
262
263 stk1160_dbg("streaming stopped\n");
264
265 mutex_unlock(&dev->v4l_lock);
266
267 return 0;
268}
269
270static struct v4l2_file_operations stk1160_fops = {
271 .owner = THIS_MODULE,
272 .open = v4l2_fh_open,
273 .release = vb2_fop_release,
274 .read = vb2_fop_read,
275 .poll = vb2_fop_poll,
276 .mmap = vb2_fop_mmap,
277 .unlocked_ioctl = video_ioctl2,
278};
279
280/*
281 * vidioc ioctls
282 */
283static int vidioc_querycap(struct file *file,
284 void *priv, struct v4l2_capability *cap)
285{
286 struct stk1160 *dev = video_drvdata(file);
287
288 strcpy(cap->driver, "stk1160");
289 strcpy(cap->card, "stk1160");
290 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
291 cap->device_caps =
292 V4L2_CAP_VIDEO_CAPTURE |
293 V4L2_CAP_STREAMING |
294 V4L2_CAP_READWRITE;
295 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
296 return 0;
297}
298
299static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
300 struct v4l2_fmtdesc *f)
301{
302 if (f->index != 0)
303 return -EINVAL;
304
305 strlcpy(f->description, format[f->index].name, sizeof(f->description));
306 f->pixelformat = format[f->index].fourcc;
307 return 0;
308}
309
310static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
311 struct v4l2_format *f)
312{
313 struct stk1160 *dev = video_drvdata(file);
314
315 f->fmt.pix.width = dev->width;
316 f->fmt.pix.height = dev->height;
317 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
318 f->fmt.pix.pixelformat = dev->fmt->fourcc;
319 f->fmt.pix.bytesperline = dev->width * 2;
320 f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
321 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
322
323 return 0;
324}
325
326static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
327 struct v4l2_format *f)
328{
329 struct stk1160 *dev = video_drvdata(file);
330
Ezequiel García9cb21732012-08-11 14:32:57 -0300331 /*
332 * User can't choose size at his own will,
333 * so we just return him the current size chosen
334 * at standard selection.
335 * TODO: Implement frame scaling?
336 */
337
Ezequiel Garciac6b69c62012-08-19 21:23:45 -0300338 f->fmt.pix.pixelformat = dev->fmt->fourcc;
Ezequiel García9cb21732012-08-11 14:32:57 -0300339 f->fmt.pix.width = dev->width;
340 f->fmt.pix.height = dev->height;
341 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
342 f->fmt.pix.bytesperline = dev->width * 2;
343 f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
344 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
345
346 return 0;
347}
348
349static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
350 struct v4l2_format *f)
351{
352 struct stk1160 *dev = video_drvdata(file);
353 struct vb2_queue *q = &dev->vb_vidq;
Ezequiel García9cb21732012-08-11 14:32:57 -0300354
355 if (vb2_is_busy(q))
356 return -EBUSY;
357
Ezequiel Garciac6b69c62012-08-19 21:23:45 -0300358 vidioc_try_fmt_vid_cap(file, priv, f);
Ezequiel García9cb21732012-08-11 14:32:57 -0300359
360 /* We don't support any format changes */
361
362 return 0;
363}
364
365static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
366{
367 struct stk1160 *dev = video_drvdata(file);
368 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
369 return 0;
370}
371
372static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
373{
374 struct stk1160 *dev = video_drvdata(file);
375
376 *norm = dev->norm;
377 return 0;
378}
379
Hans Verkuil314527a2013-03-15 06:10:40 -0300380static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Ezequiel García9cb21732012-08-11 14:32:57 -0300381{
382 struct stk1160 *dev = video_drvdata(file);
383 struct vb2_queue *q = &dev->vb_vidq;
384
Ezequiel Garciab3ba8fa2013-07-18 09:01:23 -0300385 if (dev->norm == norm)
386 return 0;
387
Ezequiel García9cb21732012-08-11 14:32:57 -0300388 if (vb2_is_busy(q))
389 return -EBUSY;
390
391 /* Check device presence */
392 if (!dev->udev)
393 return -ENODEV;
394
395 /* We need to set this now, before we call stk1160_set_std */
Hans Verkuil314527a2013-03-15 06:10:40 -0300396 dev->norm = norm;
Ezequiel García9cb21732012-08-11 14:32:57 -0300397
398 /* This is taken from saa7115 video decoder */
399 if (dev->norm & V4L2_STD_525_60) {
400 dev->width = 720;
401 dev->height = 480;
402 } else if (dev->norm & V4L2_STD_625_50) {
403 dev->width = 720;
404 dev->height = 576;
405 } else {
406 stk1160_err("invalid standard\n");
407 return -EINVAL;
408 }
409
410 stk1160_set_std(dev);
411
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300412 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
Ezequiel García9cb21732012-08-11 14:32:57 -0300413 dev->norm);
414
415 return 0;
416}
417
418
419static int vidioc_enum_input(struct file *file, void *priv,
420 struct v4l2_input *i)
421{
422 struct stk1160 *dev = video_drvdata(file);
423
424 if (i->index > STK1160_MAX_INPUT)
425 return -EINVAL;
426
Ezequiel Garcia56a960b2012-10-09 18:01:03 -0300427 /* S-Video special handling */
428 if (i->index == STK1160_SVIDEO_INPUT)
429 sprintf(i->name, "S-Video");
430 else
431 sprintf(i->name, "Composite%d", i->index);
432
Ezequiel García9cb21732012-08-11 14:32:57 -0300433 i->type = V4L2_INPUT_TYPE_CAMERA;
434 i->std = dev->vdev.tvnorms;
435 return 0;
436}
437
438static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
439{
440 struct stk1160 *dev = video_drvdata(file);
441 *i = dev->ctl_input;
442 return 0;
443}
444
445static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
446{
447 struct stk1160 *dev = video_drvdata(file);
448
Ezequiel García9cb21732012-08-11 14:32:57 -0300449 if (i > STK1160_MAX_INPUT)
450 return -EINVAL;
451
452 dev->ctl_input = i;
453
454 stk1160_select_input(dev);
455
456 return 0;
457}
458
Ezequiel García9cb21732012-08-11 14:32:57 -0300459#ifdef CONFIG_VIDEO_ADV_DEBUG
460static int vidioc_g_register(struct file *file, void *priv,
461 struct v4l2_dbg_register *reg)
462{
463 struct stk1160 *dev = video_drvdata(file);
464 int rc;
465 u8 val;
466
Ezequiel García9cb21732012-08-11 14:32:57 -0300467 /* Match host */
468 rc = stk1160_read_reg(dev, reg->reg, &val);
469 reg->val = val;
470 reg->size = 1;
471
472 return rc;
473}
474
475static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300476 const struct v4l2_dbg_register *reg)
Ezequiel García9cb21732012-08-11 14:32:57 -0300477{
478 struct stk1160 *dev = video_drvdata(file);
479
Ezequiel García9cb21732012-08-11 14:32:57 -0300480 /* Match host */
Hans Verkuil45802782014-11-05 04:54:09 -0300481 return stk1160_write_reg(dev, reg->reg, reg->val);
Ezequiel García9cb21732012-08-11 14:32:57 -0300482}
483#endif
484
485static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
486 .vidioc_querycap = vidioc_querycap,
487 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
488 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
489 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
490 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
491 .vidioc_querystd = vidioc_querystd,
492 .vidioc_g_std = vidioc_g_std,
493 .vidioc_s_std = vidioc_s_std,
494 .vidioc_enum_input = vidioc_enum_input,
495 .vidioc_g_input = vidioc_g_input,
496 .vidioc_s_input = vidioc_s_input,
497
498 /* vb2 takes care of these */
499 .vidioc_reqbufs = vb2_ioctl_reqbufs,
500 .vidioc_querybuf = vb2_ioctl_querybuf,
501 .vidioc_qbuf = vb2_ioctl_qbuf,
502 .vidioc_dqbuf = vb2_ioctl_dqbuf,
503 .vidioc_streamon = vb2_ioctl_streamon,
504 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuilebbb5632015-06-01 08:18:31 -0300505 .vidioc_expbuf = vb2_ioctl_expbuf,
Ezequiel García9cb21732012-08-11 14:32:57 -0300506
507 .vidioc_log_status = v4l2_ctrl_log_status,
508 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
509 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Ezequiel García9cb21732012-08-11 14:32:57 -0300510
511#ifdef CONFIG_VIDEO_ADV_DEBUG
512 .vidioc_g_register = vidioc_g_register,
513 .vidioc_s_register = vidioc_s_register,
514#endif
515};
516
517/********************************************************************/
518
519/*
520 * Videobuf2 operations
521 */
522static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt,
523 unsigned int *nbuffers, unsigned int *nplanes,
524 unsigned int sizes[], void *alloc_ctxs[])
525{
526 struct stk1160 *dev = vb2_get_drv_priv(vq);
527 unsigned long size;
528
529 size = dev->width * dev->height * 2;
530
531 /*
532 * Here we can change the number of buffers being requested.
533 * So, we set a minimum and a maximum like this:
534 */
535 *nbuffers = clamp_t(unsigned int, *nbuffers,
536 STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
537
538 /* This means a packed colorformat */
539 *nplanes = 1;
540
541 sizes[0] = size;
542
Ezequiel Garcia890024a2015-07-03 16:11:41 -0300543 stk1160_dbg("%s: buffer count %d, each %ld bytes\n",
544 __func__, *nbuffers, size);
Ezequiel García9cb21732012-08-11 14:32:57 -0300545
546 return 0;
547}
548
549static void buffer_queue(struct vb2_buffer *vb)
550{
551 unsigned long flags;
552 struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
553 struct stk1160_buffer *buf =
554 container_of(vb, struct stk1160_buffer, vb);
555
556 spin_lock_irqsave(&dev->buf_lock, flags);
557 if (!dev->udev) {
558 /*
559 * If the device is disconnected return the buffer to userspace
560 * directly. The next QBUF call will fail with -ENODEV.
561 */
562 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
563 } else {
564
565 buf->mem = vb2_plane_vaddr(vb, 0);
566 buf->length = vb2_plane_size(vb, 0);
567 buf->bytesused = 0;
568 buf->pos = 0;
569
570 /*
571 * If buffer length is less from expected then we return
572 * the buffer to userspace directly.
573 */
574 if (buf->length < dev->width * dev->height * 2)
575 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
576 else
577 list_add_tail(&buf->list, &dev->avail_bufs);
578
579 }
580 spin_unlock_irqrestore(&dev->buf_lock, flags);
581}
582
583static int start_streaming(struct vb2_queue *vq, unsigned int count)
584{
585 struct stk1160 *dev = vb2_get_drv_priv(vq);
586 return stk1160_start_streaming(dev);
587}
588
589/* abort streaming and wait for last buffer */
Hans Verkuile37559b2014-04-17 02:47:21 -0300590static void stop_streaming(struct vb2_queue *vq)
Ezequiel García9cb21732012-08-11 14:32:57 -0300591{
592 struct stk1160 *dev = vb2_get_drv_priv(vq);
Hans Verkuile37559b2014-04-17 02:47:21 -0300593 stk1160_stop_streaming(dev);
Ezequiel García9cb21732012-08-11 14:32:57 -0300594}
595
596static struct vb2_ops stk1160_video_qops = {
597 .queue_setup = queue_setup,
598 .buf_queue = buffer_queue,
599 .start_streaming = start_streaming,
600 .stop_streaming = stop_streaming,
601 .wait_prepare = vb2_ops_wait_prepare,
602 .wait_finish = vb2_ops_wait_finish,
603};
604
605static struct video_device v4l_template = {
606 .name = "stk1160",
607 .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
608 .fops = &stk1160_fops,
609 .ioctl_ops = &stk1160_ioctl_ops,
610 .release = video_device_release_empty,
611};
612
613/********************************************************************/
614
615/* Must be called with both v4l_lock and vb_queue_lock hold */
616void stk1160_clear_queue(struct stk1160 *dev)
617{
618 struct stk1160_buffer *buf;
619 unsigned long flags;
620
621 /* Release all active buffers */
622 spin_lock_irqsave(&dev->buf_lock, flags);
623 while (!list_empty(&dev->avail_bufs)) {
624 buf = list_first_entry(&dev->avail_bufs,
625 struct stk1160_buffer, list);
626 list_del(&buf->list);
627 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
Ezequiel Garcia890024a2015-07-03 16:11:41 -0300628 stk1160_dbg("buffer [%p/%d] aborted\n",
629 buf, buf->vb.v4l2_buf.index);
Ezequiel García9cb21732012-08-11 14:32:57 -0300630 }
Ezequiel Garciaaeff0922015-03-10 11:37:14 -0300631
632 /* It's important to release the current buffer */
633 if (dev->isoc_ctl.buf) {
634 buf = dev->isoc_ctl.buf;
635 dev->isoc_ctl.buf = NULL;
636
637 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
Ezequiel Garcia890024a2015-07-03 16:11:41 -0300638 stk1160_dbg("buffer [%p/%d] aborted\n",
639 buf, buf->vb.v4l2_buf.index);
Ezequiel Garciaaeff0922015-03-10 11:37:14 -0300640 }
Ezequiel García9cb21732012-08-11 14:32:57 -0300641 spin_unlock_irqrestore(&dev->buf_lock, flags);
642}
643
644int stk1160_vb2_setup(struct stk1160 *dev)
645{
646 int rc;
647 struct vb2_queue *q;
648
649 q = &dev->vb_vidq;
Ezequiel García9cb21732012-08-11 14:32:57 -0300650 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuilebbb5632015-06-01 08:18:31 -0300651 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Ezequiel García9cb21732012-08-11 14:32:57 -0300652 q->drv_priv = dev;
653 q->buf_struct_size = sizeof(struct stk1160_buffer);
654 q->ops = &stk1160_video_qops;
655 q->mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -0300656 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Ezequiel García9cb21732012-08-11 14:32:57 -0300657
658 rc = vb2_queue_init(q);
659 if (rc < 0)
660 return rc;
661
662 /* initialize video dma queue */
663 INIT_LIST_HEAD(&dev->avail_bufs);
664
665 return 0;
666}
667
668int stk1160_video_register(struct stk1160 *dev)
669{
670 int rc;
671
672 /* Initialize video_device with a template structure */
673 dev->vdev = v4l_template;
Ezequiel García9cb21732012-08-11 14:32:57 -0300674 dev->vdev.queue = &dev->vb_vidq;
675
676 /*
677 * Provide mutexes for v4l2 core and for videobuf2 queue.
678 * It will be used to protect *only* v4l2 ioctls.
679 */
680 dev->vdev.lock = &dev->v4l_lock;
681 dev->vdev.queue->lock = &dev->vb_queue_lock;
682
683 /* This will be used to set video_device parent */
684 dev->vdev.v4l2_dev = &dev->v4l2_dev;
Ezequiel García9cb21732012-08-11 14:32:57 -0300685
686 /* NTSC is default */
687 dev->norm = V4L2_STD_NTSC_M;
688 dev->width = 720;
689 dev->height = 480;
690
691 /* set default format */
692 dev->fmt = &format[0];
693 stk1160_set_std(dev);
694
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300695 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
Ezequiel García9cb21732012-08-11 14:32:57 -0300696 dev->norm);
697
698 video_set_drvdata(&dev->vdev, dev);
699 rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
700 if (rc < 0) {
701 stk1160_err("video_register_device failed (%d)\n", rc);
702 return rc;
703 }
704
705 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
706 video_device_node_name(&dev->vdev));
707
708 return 0;
709}