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