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