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 
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  */
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 
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 */
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 
242 static int stk1160_stop_streaming(struct stk1160 *dev)
243 {
244 	if (mutex_lock_interruptible(&dev->v4l_lock))
245 		return -ERESTARTSYS;
246 
247 	stk1160_cancel_isoc(dev);
248 
249 	/*
250 	 * It is possible to keep buffers around using a module parameter.
251 	 * This is intended to avoid memory fragmentation.
252 	 */
253 	if (!keep_buffers)
254 		stk1160_free_isoc(dev);
255 
256 	stk1160_stop_hw(dev);
257 
258 	stk1160_clear_queue(dev);
259 
260 	stk1160_dbg("streaming stopped\n");
261 
262 	mutex_unlock(&dev->v4l_lock);
263 
264 	return 0;
265 }
266 
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,
275 };
276 
277 /*
278  * vidioc ioctls
279  */
280 static int vidioc_querycap(struct file *file,
281 		void *priv, struct v4l2_capability *cap)
282 {
283 	struct stk1160 *dev = video_drvdata(file);
284 
285 	strcpy(cap->driver, "stk1160");
286 	strcpy(cap->card, "stk1160");
287 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
288 	cap->device_caps =
289 		V4L2_CAP_VIDEO_CAPTURE |
290 		V4L2_CAP_STREAMING |
291 		V4L2_CAP_READWRITE;
292 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
293 	return 0;
294 }
295 
296 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
297 		struct v4l2_fmtdesc *f)
298 {
299 	if (f->index != 0)
300 		return -EINVAL;
301 
302 	strlcpy(f->description, format[f->index].name, sizeof(f->description));
303 	f->pixelformat = format[f->index].fourcc;
304 	return 0;
305 }
306 
307 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
308 					struct v4l2_format *f)
309 {
310 	struct stk1160 *dev = video_drvdata(file);
311 
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;
319 
320 	return 0;
321 }
322 
323 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
324 			struct v4l2_format *f)
325 {
326 	struct stk1160 *dev = video_drvdata(file);
327 
328 	/*
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?
333 	 */
334 
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;
342 
343 	return 0;
344 }
345 
346 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
347 					struct v4l2_format *f)
348 {
349 	struct stk1160 *dev = video_drvdata(file);
350 	struct vb2_queue *q = &dev->vb_vidq;
351 
352 	if (vb2_is_busy(q))
353 		return -EBUSY;
354 
355 	vidioc_try_fmt_vid_cap(file, priv, f);
356 
357 	/* We don't support any format changes */
358 
359 	return 0;
360 }
361 
362 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
363 {
364 	struct stk1160 *dev = video_drvdata(file);
365 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
366 	return 0;
367 }
368 
369 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
370 {
371 	struct stk1160 *dev = video_drvdata(file);
372 
373 	*norm = dev->norm;
374 	return 0;
375 }
376 
377 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
378 {
379 	struct stk1160 *dev = video_drvdata(file);
380 	struct vb2_queue *q = &dev->vb_vidq;
381 
382 	if (dev->norm == norm)
383 		return 0;
384 
385 	if (vb2_is_busy(q))
386 		return -EBUSY;
387 
388 	/* Check device presence */
389 	if (!dev->udev)
390 		return -ENODEV;
391 
392 	/* We need to set this now, before we call stk1160_set_std */
393 	dev->norm = norm;
394 
395 	/* This is taken from saa7115 video decoder */
396 	if (dev->norm & V4L2_STD_525_60) {
397 		dev->width = 720;
398 		dev->height = 480;
399 	} else if (dev->norm & V4L2_STD_625_50) {
400 		dev->width = 720;
401 		dev->height = 576;
402 	} else {
403 		stk1160_err("invalid standard\n");
404 		return -EINVAL;
405 	}
406 
407 	stk1160_set_std(dev);
408 
409 	v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std,
410 			dev->norm);
411 
412 	return 0;
413 }
414 
415 
416 static int vidioc_enum_input(struct file *file, void *priv,
417 				struct v4l2_input *i)
418 {
419 	struct stk1160 *dev = video_drvdata(file);
420 
421 	if (i->index > STK1160_MAX_INPUT)
422 		return -EINVAL;
423 
424 	/* S-Video special handling */
425 	if (i->index == STK1160_SVIDEO_INPUT)
426 		sprintf(i->name, "S-Video");
427 	else
428 		sprintf(i->name, "Composite%d", i->index);
429 
430 	i->type = V4L2_INPUT_TYPE_CAMERA;
431 	i->std = dev->vdev.tvnorms;
432 	return 0;
433 }
434 
435 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
436 {
437 	struct stk1160 *dev = video_drvdata(file);
438 	*i = dev->ctl_input;
439 	return 0;
440 }
441 
442 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
443 {
444 	struct stk1160 *dev = video_drvdata(file);
445 
446 	if (i > STK1160_MAX_INPUT)
447 		return -EINVAL;
448 
449 	dev->ctl_input = i;
450 
451 	stk1160_select_input(dev);
452 
453 	return 0;
454 }
455 
456 #ifdef CONFIG_VIDEO_ADV_DEBUG
457 static int vidioc_g_register(struct file *file, void *priv,
458 			     struct v4l2_dbg_register *reg)
459 {
460 	struct stk1160 *dev = video_drvdata(file);
461 	int rc;
462 	u8 val;
463 
464 	/* Match host */
465 	rc = stk1160_read_reg(dev, reg->reg, &val);
466 	reg->val = val;
467 	reg->size = 1;
468 
469 	return rc;
470 }
471 
472 static int vidioc_s_register(struct file *file, void *priv,
473 			     const struct v4l2_dbg_register *reg)
474 {
475 	struct stk1160 *dev = video_drvdata(file);
476 
477 	/* Match host */
478 	return stk1160_write_reg(dev, reg->reg, cpu_to_le16(reg->val));
479 }
480 #endif
481 
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,
494 
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,
502 
503 	.vidioc_log_status  = v4l2_ctrl_log_status,
504 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
505 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
506 
507 #ifdef CONFIG_VIDEO_ADV_DEBUG
508 	.vidioc_g_register = vidioc_g_register,
509 	.vidioc_s_register = vidioc_s_register,
510 #endif
511 };
512 
513 /********************************************************************/
514 
515 /*
516  * Videobuf2 operations
517  */
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[])
521 {
522 	struct stk1160 *dev = vb2_get_drv_priv(vq);
523 	unsigned long size;
524 
525 	size = dev->width * dev->height * 2;
526 
527 	/*
528 	 * Here we can change the number of buffers being requested.
529 	 * So, we set a minimum and a maximum like this:
530 	 */
531 	*nbuffers = clamp_t(unsigned int, *nbuffers,
532 			STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
533 
534 	/* This means a packed colorformat */
535 	*nplanes = 1;
536 
537 	sizes[0] = size;
538 
539 	stk1160_info("%s: buffer count %d, each %ld bytes\n",
540 			__func__, *nbuffers, size);
541 
542 	return 0;
543 }
544 
545 static void buffer_queue(struct vb2_buffer *vb)
546 {
547 	unsigned long flags;
548 	struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
549 	struct stk1160_buffer *buf =
550 		container_of(vb, struct stk1160_buffer, vb);
551 
552 	spin_lock_irqsave(&dev->buf_lock, flags);
553 	if (!dev->udev) {
554 		/*
555 		 * If the device is disconnected return the buffer to userspace
556 		 * directly. The next QBUF call will fail with -ENODEV.
557 		 */
558 		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
559 	} else {
560 
561 		buf->mem = vb2_plane_vaddr(vb, 0);
562 		buf->length = vb2_plane_size(vb, 0);
563 		buf->bytesused = 0;
564 		buf->pos = 0;
565 
566 		/*
567 		 * If buffer length is less from expected then we return
568 		 * the buffer to userspace directly.
569 		 */
570 		if (buf->length < dev->width * dev->height * 2)
571 			vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
572 		else
573 			list_add_tail(&buf->list, &dev->avail_bufs);
574 
575 	}
576 	spin_unlock_irqrestore(&dev->buf_lock, flags);
577 }
578 
579 static int start_streaming(struct vb2_queue *vq, unsigned int count)
580 {
581 	struct stk1160 *dev = vb2_get_drv_priv(vq);
582 	return stk1160_start_streaming(dev);
583 }
584 
585 /* abort streaming and wait for last buffer */
586 static int stop_streaming(struct vb2_queue *vq)
587 {
588 	struct stk1160 *dev = vb2_get_drv_priv(vq);
589 	return stk1160_stop_streaming(dev);
590 }
591 
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,
599 };
600 
601 static struct video_device v4l_template = {
602 	.name = "stk1160",
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,
607 };
608 
609 /********************************************************************/
610 
611 /* Must be called with both v4l_lock and vb_queue_lock hold */
612 void stk1160_clear_queue(struct stk1160 *dev)
613 {
614 	struct stk1160_buffer *buf;
615 	unsigned long flags;
616 
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);
626 	}
627 	/* It's important to clear current buffer */
628 	dev->isoc_ctl.buf = NULL;
629 	spin_unlock_irqrestore(&dev->buf_lock, flags);
630 }
631 
632 int stk1160_vb2_setup(struct stk1160 *dev)
633 {
634 	int rc;
635 	struct vb2_queue *q;
636 
637 	q = &dev->vb_vidq;
638 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
639 	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
640 	q->drv_priv = dev;
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;
645 
646 	rc = vb2_queue_init(q);
647 	if (rc < 0)
648 		return rc;
649 
650 	/* initialize video dma queue */
651 	INIT_LIST_HEAD(&dev->avail_bufs);
652 
653 	return 0;
654 }
655 
656 int stk1160_video_register(struct stk1160 *dev)
657 {
658 	int rc;
659 
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;
664 
665 	/*
666 	 * Provide mutexes for v4l2 core and for videobuf2 queue.
667 	 * It will be used to protect *only* v4l2 ioctls.
668 	 */
669 	dev->vdev.lock = &dev->v4l_lock;
670 	dev->vdev.queue->lock = &dev->vb_queue_lock;
671 
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);
675 
676 	/* NTSC is default */
677 	dev->norm = V4L2_STD_NTSC_M;
678 	dev->width = 720;
679 	dev->height = 480;
680 
681 	/* set default format */
682 	dev->fmt = &format[0];
683 	stk1160_set_std(dev);
684 
685 	v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std,
686 			dev->norm);
687 
688 	video_set_drvdata(&dev->vdev, dev);
689 	rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
690 	if (rc < 0) {
691 		stk1160_err("video_register_device failed (%d)\n", rc);
692 		return rc;
693 	}
694 
695 	v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
696 		  video_device_node_name(&dev->vdev));
697 
698 	return 0;
699 }
700