1 /*
2    em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 		    video capture devices
4 
5    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 		      Markus Rechberger <mrechberger@gmail.com>
7 		      Mauro Carvalho Chehab <mchehab@infradead.org>
8 		      Sascha Sommer <saschasommer@freenet.de>
9 
10 	Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 		by Luca Risolia <luca.risolia@studio.unibo.it>
12 
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 2 of the License, or
16    (at your option) any later version.
17 
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22 
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27 
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/bitmap.h>
33 #include <linux/usb.h>
34 #include <linux/i2c.h>
35 #include <linux/mm.h>
36 #include <linux/mutex.h>
37 #include <linux/slab.h>
38 
39 #include "em28xx.h"
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 #include <media/v4l2-chip-ident.h>
43 #include <media/msp3400.h>
44 #include <media/tuner.h>
45 
46 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 		      "Markus Rechberger <mrechberger@gmail.com>, " \
48 		      "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
49 		      "Sascha Sommer <saschasommer@freenet.de>"
50 
51 #define DRIVER_DESC         "Empia em28xx based USB video device driver"
52 
53 #define EM28XX_VERSION "0.1.3"
54 
55 #define em28xx_videodbg(fmt, arg...) do {\
56 	if (video_debug) \
57 		printk(KERN_INFO "%s %s :"fmt, \
58 			 dev->name, __func__ , ##arg); } while (0)
59 
60 static unsigned int isoc_debug;
61 module_param(isoc_debug, int, 0644);
62 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
63 
64 #define em28xx_isocdbg(fmt, arg...) \
65 do {\
66 	if (isoc_debug) { \
67 		printk(KERN_INFO "%s %s :"fmt, \
68 			 dev->name, __func__ , ##arg); \
69 	} \
70   } while (0)
71 
72 MODULE_AUTHOR(DRIVER_AUTHOR);
73 MODULE_DESCRIPTION(DRIVER_DESC);
74 MODULE_LICENSE("GPL");
75 MODULE_VERSION(EM28XX_VERSION);
76 
77 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78 static unsigned int vbi_nr[]   = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80 
81 module_param_array(video_nr, int, NULL, 0444);
82 module_param_array(vbi_nr, int, NULL, 0444);
83 module_param_array(radio_nr, int, NULL, 0444);
84 MODULE_PARM_DESC(video_nr, "video device numbers");
85 MODULE_PARM_DESC(vbi_nr,   "vbi device numbers");
86 MODULE_PARM_DESC(radio_nr, "radio device numbers");
87 
88 static unsigned int video_debug;
89 module_param(video_debug, int, 0644);
90 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
91 
92 /* supported video standards */
93 static struct em28xx_fmt format[] = {
94 	{
95 		.name     = "16 bpp YUY2, 4:2:2, packed",
96 		.fourcc   = V4L2_PIX_FMT_YUYV,
97 		.depth    = 16,
98 		.reg	  = EM28XX_OUTFMT_YUV422_Y0UY1V,
99 	}, {
100 		.name     = "16 bpp RGB 565, LE",
101 		.fourcc   = V4L2_PIX_FMT_RGB565,
102 		.depth    = 16,
103 		.reg      = EM28XX_OUTFMT_RGB_16_656,
104 	}, {
105 		.name     = "8 bpp Bayer BGBG..GRGR",
106 		.fourcc   = V4L2_PIX_FMT_SBGGR8,
107 		.depth    = 8,
108 		.reg      = EM28XX_OUTFMT_RGB_8_BGBG,
109 	}, {
110 		.name     = "8 bpp Bayer GRGR..BGBG",
111 		.fourcc   = V4L2_PIX_FMT_SGRBG8,
112 		.depth    = 8,
113 		.reg      = EM28XX_OUTFMT_RGB_8_GRGR,
114 	}, {
115 		.name     = "8 bpp Bayer GBGB..RGRG",
116 		.fourcc   = V4L2_PIX_FMT_SGBRG8,
117 		.depth    = 8,
118 		.reg      = EM28XX_OUTFMT_RGB_8_GBGB,
119 	}, {
120 		.name     = "12 bpp YUV411",
121 		.fourcc   = V4L2_PIX_FMT_YUV411P,
122 		.depth    = 12,
123 		.reg      = EM28XX_OUTFMT_YUV411,
124 	},
125 };
126 
127 /* supported controls */
128 /* Common to all boards */
129 static struct v4l2_queryctrl ac97_qctrl[] = {
130 	{
131 		.id = V4L2_CID_AUDIO_VOLUME,
132 		.type = V4L2_CTRL_TYPE_INTEGER,
133 		.name = "Volume",
134 		.minimum = 0x0,
135 		.maximum = 0x1f,
136 		.step = 0x1,
137 		.default_value = 0x1f,
138 		.flags = V4L2_CTRL_FLAG_SLIDER,
139 	}, {
140 		.id = V4L2_CID_AUDIO_MUTE,
141 		.type = V4L2_CTRL_TYPE_BOOLEAN,
142 		.name = "Mute",
143 		.minimum = 0,
144 		.maximum = 1,
145 		.step = 1,
146 		.default_value = 1,
147 		.flags = 0,
148 	}
149 };
150 
151 /* ------------------------------------------------------------------
152 	DMA and thread functions
153    ------------------------------------------------------------------*/
154 
155 /*
156  * Announces that a buffer were filled and request the next
157  */
158 static inline void buffer_filled(struct em28xx *dev,
159 				  struct em28xx_dmaqueue *dma_q,
160 				  struct em28xx_buffer *buf)
161 {
162 	/* Advice that buffer was filled */
163 	em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
164 	buf->vb.state = VIDEOBUF_DONE;
165 	buf->vb.field_count++;
166 	v4l2_get_timestamp(&buf->vb.ts);
167 
168 	dev->usb_ctl.vid_buf = NULL;
169 
170 	list_del(&buf->vb.queue);
171 	wake_up(&buf->vb.done);
172 }
173 
174 static inline void vbi_buffer_filled(struct em28xx *dev,
175 				     struct em28xx_dmaqueue *dma_q,
176 				     struct em28xx_buffer *buf)
177 {
178 	/* Advice that buffer was filled */
179 	em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
180 
181 	buf->vb.state = VIDEOBUF_DONE;
182 	buf->vb.field_count++;
183 	v4l2_get_timestamp(&buf->vb.ts);
184 
185 	dev->usb_ctl.vbi_buf = NULL;
186 
187 	list_del(&buf->vb.queue);
188 	wake_up(&buf->vb.done);
189 }
190 
191 /*
192  * Identify the buffer header type and properly handles
193  */
194 static void em28xx_copy_video(struct em28xx *dev,
195 			      struct em28xx_dmaqueue  *dma_q,
196 			      struct em28xx_buffer *buf,
197 			      unsigned char *p,
198 			      unsigned char *outp, unsigned long len)
199 {
200 	void *fieldstart, *startwrite, *startread;
201 	int  linesdone, currlinedone, offset, lencopy, remain;
202 	int bytesperline = dev->width << 1;
203 
204 	if (dma_q->pos + len > buf->vb.size)
205 		len = buf->vb.size - dma_q->pos;
206 
207 	startread = p;
208 	remain = len;
209 
210 	if (dev->progressive || buf->top_field)
211 		fieldstart = outp;
212 	else /* interlaced mode, even nr. of lines */
213 		fieldstart = outp + bytesperline;
214 
215 	linesdone = dma_q->pos / bytesperline;
216 	currlinedone = dma_q->pos % bytesperline;
217 
218 	if (dev->progressive)
219 		offset = linesdone * bytesperline + currlinedone;
220 	else
221 		offset = linesdone * bytesperline * 2 + currlinedone;
222 
223 	startwrite = fieldstart + offset;
224 	lencopy = bytesperline - currlinedone;
225 	lencopy = lencopy > remain ? remain : lencopy;
226 
227 	if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
228 		em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
229 			       ((char *)startwrite + lencopy) -
230 			       ((char *)outp + buf->vb.size));
231 		remain = (char *)outp + buf->vb.size - (char *)startwrite;
232 		lencopy = remain;
233 	}
234 	if (lencopy <= 0)
235 		return;
236 	memcpy(startwrite, startread, lencopy);
237 
238 	remain -= lencopy;
239 
240 	while (remain > 0) {
241 		if (dev->progressive)
242 			startwrite += lencopy;
243 		else
244 			startwrite += lencopy + bytesperline;
245 		startread += lencopy;
246 		if (bytesperline > remain)
247 			lencopy = remain;
248 		else
249 			lencopy = bytesperline;
250 
251 		if ((char *)startwrite + lencopy > (char *)outp +
252 		    buf->vb.size) {
253 			em28xx_isocdbg("Overflow of %zi bytes past buffer end"
254 				       "(2)\n",
255 				       ((char *)startwrite + lencopy) -
256 				       ((char *)outp + buf->vb.size));
257 			lencopy = remain = (char *)outp + buf->vb.size -
258 					   (char *)startwrite;
259 		}
260 		if (lencopy <= 0)
261 			break;
262 
263 		memcpy(startwrite, startread, lencopy);
264 
265 		remain -= lencopy;
266 	}
267 
268 	dma_q->pos += len;
269 }
270 
271 static void em28xx_copy_vbi(struct em28xx *dev,
272 			      struct em28xx_dmaqueue  *dma_q,
273 			      struct em28xx_buffer *buf,
274 			      unsigned char *p,
275 			      unsigned char *outp, unsigned long len)
276 {
277 	void *startwrite, *startread;
278 	int  offset;
279 	int bytesperline;
280 
281 	if (dev == NULL) {
282 		em28xx_isocdbg("dev is null\n");
283 		return;
284 	}
285 	bytesperline = dev->vbi_width;
286 
287 	if (dma_q == NULL) {
288 		em28xx_isocdbg("dma_q is null\n");
289 		return;
290 	}
291 	if (buf == NULL) {
292 		return;
293 	}
294 	if (p == NULL) {
295 		em28xx_isocdbg("p is null\n");
296 		return;
297 	}
298 	if (outp == NULL) {
299 		em28xx_isocdbg("outp is null\n");
300 		return;
301 	}
302 
303 	if (dma_q->pos + len > buf->vb.size)
304 		len = buf->vb.size - dma_q->pos;
305 
306 	startread = p;
307 
308 	startwrite = outp + dma_q->pos;
309 	offset = dma_q->pos;
310 
311 	/* Make sure the bottom field populates the second half of the frame */
312 	if (buf->top_field == 0) {
313 		startwrite += bytesperline * dev->vbi_height;
314 		offset += bytesperline * dev->vbi_height;
315 	}
316 
317 	memcpy(startwrite, startread, len);
318 	dma_q->pos += len;
319 }
320 
321 static inline void print_err_status(struct em28xx *dev,
322 				     int packet, int status)
323 {
324 	char *errmsg = "Unknown";
325 
326 	switch (status) {
327 	case -ENOENT:
328 		errmsg = "unlinked synchronuously";
329 		break;
330 	case -ECONNRESET:
331 		errmsg = "unlinked asynchronuously";
332 		break;
333 	case -ENOSR:
334 		errmsg = "Buffer error (overrun)";
335 		break;
336 	case -EPIPE:
337 		errmsg = "Stalled (device not responding)";
338 		break;
339 	case -EOVERFLOW:
340 		errmsg = "Babble (bad cable?)";
341 		break;
342 	case -EPROTO:
343 		errmsg = "Bit-stuff error (bad cable?)";
344 		break;
345 	case -EILSEQ:
346 		errmsg = "CRC/Timeout (could be anything)";
347 		break;
348 	case -ETIME:
349 		errmsg = "Device does not respond";
350 		break;
351 	}
352 	if (packet < 0) {
353 		em28xx_isocdbg("URB status %d [%s].\n",	status, errmsg);
354 	} else {
355 		em28xx_isocdbg("URB packet %d, status %d [%s].\n",
356 			       packet, status, errmsg);
357 	}
358 }
359 
360 /*
361  * video-buf generic routine to get the next available buffer
362  */
363 static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
364 					  struct em28xx_buffer **buf)
365 {
366 	struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
367 	char *outp;
368 
369 	if (list_empty(&dma_q->active)) {
370 		em28xx_isocdbg("No active queue to serve\n");
371 		dev->usb_ctl.vid_buf = NULL;
372 		*buf = NULL;
373 		return;
374 	}
375 
376 	/* Get the next buffer */
377 	*buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
378 
379 	/* Cleans up buffer - Useful for testing for frame/URB loss */
380 	outp = videobuf_to_vmalloc(&(*buf)->vb);
381 	memset(outp, 0, (*buf)->vb.size);
382 
383 	dev->usb_ctl.vid_buf = *buf;
384 
385 	return;
386 }
387 
388 /*
389  * video-buf generic routine to get the next available VBI buffer
390  */
391 static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
392 				    struct em28xx_buffer **buf)
393 {
394 	struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
395 	char *outp;
396 
397 	if (list_empty(&dma_q->active)) {
398 		em28xx_isocdbg("No active queue to serve\n");
399 		dev->usb_ctl.vbi_buf = NULL;
400 		*buf = NULL;
401 		return;
402 	}
403 
404 	/* Get the next buffer */
405 	*buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
406 	/* Cleans up buffer - Useful for testing for frame/URB loss */
407 	outp = videobuf_to_vmalloc(&(*buf)->vb);
408 	memset(outp, 0x00, (*buf)->vb.size);
409 
410 	dev->usb_ctl.vbi_buf = *buf;
411 
412 	return;
413 }
414 
415 /*
416  * Controls the isoc copy of each urb packet
417  */
418 static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
419 {
420 	struct em28xx_buffer    *buf;
421 	struct em28xx_dmaqueue  *dma_q = &dev->vidq;
422 	unsigned char *outp = NULL;
423 	int i, len = 0, rc = 1;
424 	unsigned char *p;
425 
426 	if (!dev)
427 		return 0;
428 
429 	if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
430 		return 0;
431 
432 	if (urb->status < 0)
433 		print_err_status(dev, -1, urb->status);
434 
435 	buf = dev->usb_ctl.vid_buf;
436 	if (buf != NULL)
437 		outp = videobuf_to_vmalloc(&buf->vb);
438 
439 	for (i = 0; i < urb->number_of_packets; i++) {
440 		int status = urb->iso_frame_desc[i].status;
441 
442 		if (status < 0) {
443 			print_err_status(dev, i, status);
444 			if (urb->iso_frame_desc[i].status != -EPROTO)
445 				continue;
446 		}
447 
448 		len = urb->iso_frame_desc[i].actual_length - 4;
449 
450 		if (urb->iso_frame_desc[i].actual_length <= 0) {
451 			/* em28xx_isocdbg("packet %d is empty",i); - spammy */
452 			continue;
453 		}
454 		if (urb->iso_frame_desc[i].actual_length >
455 						dev->max_pkt_size) {
456 			em28xx_isocdbg("packet bigger than packet size");
457 			continue;
458 		}
459 
460 		p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
461 
462 		/* FIXME: incomplete buffer checks where removed to make
463 		   logic simpler. Impacts of those changes should be evaluated
464 		 */
465 		if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
466 			em28xx_isocdbg("VBI HEADER!!!\n");
467 			/* FIXME: Should add vbi copy */
468 			continue;
469 		}
470 		if (p[0] == 0x22 && p[1] == 0x5a) {
471 			em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
472 				       len, (p[2] & 1) ? "odd" : "even");
473 
474 			if (dev->progressive || !(p[2] & 1)) {
475 				if (buf != NULL)
476 					buffer_filled(dev, dma_q, buf);
477 				get_next_buf(dma_q, &buf);
478 				if (buf == NULL)
479 					outp = NULL;
480 				else
481 					outp = videobuf_to_vmalloc(&buf->vb);
482 			}
483 
484 			if (buf != NULL) {
485 				if (p[2] & 1)
486 					buf->top_field = 0;
487 				else
488 					buf->top_field = 1;
489 			}
490 
491 			dma_q->pos = 0;
492 		}
493 		if (buf != NULL) {
494 			if (p[0] != 0x88 && p[0] != 0x22) {
495 				em28xx_isocdbg("frame is not complete\n");
496 				len += 4;
497 			} else {
498 				p += 4;
499 			}
500 			em28xx_copy_video(dev, dma_q, buf, p, outp, len);
501 		}
502 	}
503 	return rc;
504 }
505 
506 /* Version of isoc handler that takes into account a mixture of video and
507    VBI data */
508 static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
509 {
510 	struct em28xx_buffer    *buf, *vbi_buf;
511 	struct em28xx_dmaqueue  *dma_q = &dev->vidq;
512 	struct em28xx_dmaqueue  *vbi_dma_q = &dev->vbiq;
513 	unsigned char *outp = NULL;
514 	unsigned char *vbioutp = NULL;
515 	int i, len = 0, rc = 1;
516 	unsigned char *p;
517 	int vbi_size;
518 
519 	if (!dev)
520 		return 0;
521 
522 	if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
523 		return 0;
524 
525 	if (urb->status < 0)
526 		print_err_status(dev, -1, urb->status);
527 
528 	buf = dev->usb_ctl.vid_buf;
529 	if (buf != NULL)
530 		outp = videobuf_to_vmalloc(&buf->vb);
531 
532 	vbi_buf = dev->usb_ctl.vbi_buf;
533 	if (vbi_buf != NULL)
534 		vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
535 
536 	for (i = 0; i < urb->number_of_packets; i++) {
537 		int status = urb->iso_frame_desc[i].status;
538 
539 		if (status < 0) {
540 			print_err_status(dev, i, status);
541 			if (urb->iso_frame_desc[i].status != -EPROTO)
542 				continue;
543 		}
544 
545 		len = urb->iso_frame_desc[i].actual_length;
546 		if (urb->iso_frame_desc[i].actual_length <= 0) {
547 			/* em28xx_isocdbg("packet %d is empty",i); - spammy */
548 			continue;
549 		}
550 		if (urb->iso_frame_desc[i].actual_length >
551 						dev->max_pkt_size) {
552 			em28xx_isocdbg("packet bigger than packet size");
553 			continue;
554 		}
555 
556 		p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
557 
558 		/* capture type 0 = vbi start
559 		   capture type 1 = video start
560 		   capture type 2 = video in progress */
561 		if (p[0] == 0x33 && p[1] == 0x95) {
562 			dev->capture_type = 0;
563 			dev->vbi_read = 0;
564 			em28xx_isocdbg("VBI START HEADER!!!\n");
565 			dev->cur_field = p[2];
566 			p += 4;
567 			len -= 4;
568 		} else if (p[0] == 0x88 && p[1] == 0x88 &&
569 			   p[2] == 0x88 && p[3] == 0x88) {
570 			/* continuation */
571 			p += 4;
572 			len -= 4;
573 		} else if (p[0] == 0x22 && p[1] == 0x5a) {
574 			/* start video */
575 			p += 4;
576 			len -= 4;
577 		}
578 
579 		vbi_size = dev->vbi_width * dev->vbi_height;
580 
581 		if (dev->capture_type == 0) {
582 			if (dev->vbi_read >= vbi_size) {
583 				/* We've already read all the VBI data, so
584 				   treat the rest as video */
585 				em28xx_isocdbg("dev->vbi_read > vbi_size\n");
586 			} else if ((dev->vbi_read + len) < vbi_size) {
587 				/* This entire frame is VBI data */
588 				if (dev->vbi_read == 0 &&
589 				    (!(dev->cur_field & 1))) {
590 					/* Brand new frame */
591 					if (vbi_buf != NULL)
592 						vbi_buffer_filled(dev,
593 								  vbi_dma_q,
594 								  vbi_buf);
595 					vbi_get_next_buf(vbi_dma_q, &vbi_buf);
596 					if (vbi_buf == NULL)
597 						vbioutp = NULL;
598 					else
599 						vbioutp = videobuf_to_vmalloc(
600 							&vbi_buf->vb);
601 				}
602 
603 				if (dev->vbi_read == 0) {
604 					vbi_dma_q->pos = 0;
605 					if (vbi_buf != NULL) {
606 						if (dev->cur_field & 1)
607 							vbi_buf->top_field = 0;
608 						else
609 							vbi_buf->top_field = 1;
610 					}
611 				}
612 
613 				dev->vbi_read += len;
614 				em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
615 						vbioutp, len);
616 			} else {
617 				/* Some of this frame is VBI data and some is
618 				   video data */
619 				int vbi_data_len = vbi_size - dev->vbi_read;
620 				dev->vbi_read += vbi_data_len;
621 				em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
622 						vbioutp, vbi_data_len);
623 				dev->capture_type = 1;
624 				p += vbi_data_len;
625 				len -= vbi_data_len;
626 			}
627 		}
628 
629 		if (dev->capture_type == 1) {
630 			dev->capture_type = 2;
631 			if (dev->progressive || !(dev->cur_field & 1)) {
632 				if (buf != NULL)
633 					buffer_filled(dev, dma_q, buf);
634 				get_next_buf(dma_q, &buf);
635 				if (buf == NULL)
636 					outp = NULL;
637 				else
638 					outp = videobuf_to_vmalloc(&buf->vb);
639 			}
640 			if (buf != NULL) {
641 				if (dev->cur_field & 1)
642 					buf->top_field = 0;
643 				else
644 					buf->top_field = 1;
645 			}
646 
647 			dma_q->pos = 0;
648 		}
649 
650 		if (buf != NULL && dev->capture_type == 2) {
651 			if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
652 			    p[2] == 0x88 && p[3] == 0x88) {
653 				p += 4;
654 				len -= 4;
655 			}
656 			if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
657 				em28xx_isocdbg("Video frame %d, len=%i, %s\n",
658 					       p[2], len, (p[2] & 1) ?
659 					       "odd" : "even");
660 				p += 4;
661 				len -= 4;
662 			}
663 
664 			if (len > 0)
665 				em28xx_copy_video(dev, dma_q, buf, p, outp,
666 						  len);
667 		}
668 	}
669 	return rc;
670 }
671 
672 
673 /* ------------------------------------------------------------------
674 	Videobuf operations
675    ------------------------------------------------------------------*/
676 
677 static int
678 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
679 {
680 	struct em28xx_fh *fh = vq->priv_data;
681 	struct em28xx        *dev = fh->dev;
682 	struct v4l2_frequency f;
683 
684 	*size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
685 		>> 3;
686 
687 	if (0 == *count)
688 		*count = EM28XX_DEF_BUF;
689 
690 	if (*count < EM28XX_MIN_BUF)
691 		*count = EM28XX_MIN_BUF;
692 
693 	/* Ask tuner to go to analog or radio mode */
694 	memset(&f, 0, sizeof(f));
695 	f.frequency = dev->ctl_freq;
696 	f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
697 
698 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
699 
700 	return 0;
701 }
702 
703 /* This is called *without* dev->slock held; please keep it that way */
704 static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
705 {
706 	struct em28xx_fh     *fh  = vq->priv_data;
707 	struct em28xx        *dev = fh->dev;
708 	unsigned long flags = 0;
709 	if (in_interrupt())
710 		BUG();
711 
712 	/* We used to wait for the buffer to finish here, but this didn't work
713 	   because, as we were keeping the state as VIDEOBUF_QUEUED,
714 	   videobuf_queue_cancel marked it as finished for us.
715 	   (Also, it could wedge forever if the hardware was misconfigured.)
716 
717 	   This should be safe; by the time we get here, the buffer isn't
718 	   queued anymore. If we ever start marking the buffers as
719 	   VIDEOBUF_ACTIVE, it won't be, though.
720 	*/
721 	spin_lock_irqsave(&dev->slock, flags);
722 	if (dev->usb_ctl.vid_buf == buf)
723 		dev->usb_ctl.vid_buf = NULL;
724 	spin_unlock_irqrestore(&dev->slock, flags);
725 
726 	videobuf_vmalloc_free(&buf->vb);
727 	buf->vb.state = VIDEOBUF_NEEDS_INIT;
728 }
729 
730 static int
731 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
732 						enum v4l2_field field)
733 {
734 	struct em28xx_fh     *fh  = vq->priv_data;
735 	struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
736 	struct em28xx        *dev = fh->dev;
737 	int                  rc = 0, urb_init = 0;
738 
739 	buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
740 			+ 7) >> 3;
741 
742 	if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
743 		return -EINVAL;
744 
745 	buf->vb.width  = dev->width;
746 	buf->vb.height = dev->height;
747 	buf->vb.field  = field;
748 
749 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
750 		rc = videobuf_iolock(vq, &buf->vb, NULL);
751 		if (rc < 0)
752 			goto fail;
753 	}
754 
755 	if (!dev->usb_ctl.analog_bufs.num_bufs)
756 		urb_init = 1;
757 
758 	if (urb_init) {
759 		if (em28xx_vbi_supported(dev) == 1)
760 			rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
761 						  EM28XX_NUM_BUFS,
762 						  dev->max_pkt_size,
763 						  EM28XX_NUM_ISOC_PACKETS,
764 						  em28xx_isoc_copy_vbi);
765 		else
766 			rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
767 						  EM28XX_NUM_BUFS,
768 						  dev->max_pkt_size,
769 						  EM28XX_NUM_ISOC_PACKETS,
770 						  em28xx_isoc_copy);
771 		if (rc < 0)
772 			goto fail;
773 	}
774 
775 	buf->vb.state = VIDEOBUF_PREPARED;
776 	return 0;
777 
778 fail:
779 	free_buffer(vq, buf);
780 	return rc;
781 }
782 
783 static void
784 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
785 {
786 	struct em28xx_buffer    *buf     = container_of(vb,
787 							struct em28xx_buffer,
788 							vb);
789 	struct em28xx_fh        *fh      = vq->priv_data;
790 	struct em28xx           *dev     = fh->dev;
791 	struct em28xx_dmaqueue  *vidq    = &dev->vidq;
792 
793 	buf->vb.state = VIDEOBUF_QUEUED;
794 	list_add_tail(&buf->vb.queue, &vidq->active);
795 
796 }
797 
798 static void buffer_release(struct videobuf_queue *vq,
799 				struct videobuf_buffer *vb)
800 {
801 	struct em28xx_buffer   *buf  = container_of(vb,
802 						    struct em28xx_buffer,
803 						    vb);
804 	struct em28xx_fh       *fh   = vq->priv_data;
805 	struct em28xx          *dev  = (struct em28xx *)fh->dev;
806 
807 	em28xx_isocdbg("em28xx: called buffer_release\n");
808 
809 	free_buffer(vq, buf);
810 }
811 
812 static struct videobuf_queue_ops em28xx_video_qops = {
813 	.buf_setup      = buffer_setup,
814 	.buf_prepare    = buffer_prepare,
815 	.buf_queue      = buffer_queue,
816 	.buf_release    = buffer_release,
817 };
818 
819 /*********************  v4l2 interface  **************************************/
820 
821 static void video_mux(struct em28xx *dev, int index)
822 {
823 	dev->ctl_input = index;
824 	dev->ctl_ainput = INPUT(index)->amux;
825 	dev->ctl_aoutput = INPUT(index)->aout;
826 
827 	if (!dev->ctl_aoutput)
828 		dev->ctl_aoutput = EM28XX_AOUT_MASTER;
829 
830 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
831 			INPUT(index)->vmux, 0, 0);
832 
833 	if (dev->board.has_msp34xx) {
834 		if (dev->i2s_speed) {
835 			v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
836 				s_i2s_clock_freq, dev->i2s_speed);
837 		}
838 		/* Note: this is msp3400 specific */
839 		v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
840 			 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
841 	}
842 
843 	if (dev->board.adecoder != EM28XX_NOADECODER) {
844 		v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
845 			dev->ctl_ainput, dev->ctl_aoutput, 0);
846 	}
847 
848 	em28xx_audio_analog_set(dev);
849 }
850 
851 /* Usage lock check functions */
852 static int res_get(struct em28xx_fh *fh, unsigned int bit)
853 {
854 	struct em28xx    *dev = fh->dev;
855 
856 	if (fh->resources & bit)
857 		/* have it already allocated */
858 		return 1;
859 
860 	/* is it free? */
861 	if (dev->resources & bit) {
862 		/* no, someone else uses it */
863 		return 0;
864 	}
865 	/* it's free, grab it */
866 	fh->resources  |= bit;
867 	dev->resources |= bit;
868 	em28xx_videodbg("res: get %d\n", bit);
869 	return 1;
870 }
871 
872 static int res_check(struct em28xx_fh *fh, unsigned int bit)
873 {
874 	return fh->resources & bit;
875 }
876 
877 static int res_locked(struct em28xx *dev, unsigned int bit)
878 {
879 	return dev->resources & bit;
880 }
881 
882 static void res_free(struct em28xx_fh *fh, unsigned int bits)
883 {
884 	struct em28xx    *dev = fh->dev;
885 
886 	BUG_ON((fh->resources & bits) != bits);
887 
888 	fh->resources  &= ~bits;
889 	dev->resources &= ~bits;
890 	em28xx_videodbg("res: put %d\n", bits);
891 }
892 
893 static int get_ressource(struct em28xx_fh *fh)
894 {
895 	switch (fh->type) {
896 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
897 		return EM28XX_RESOURCE_VIDEO;
898 	case V4L2_BUF_TYPE_VBI_CAPTURE:
899 		return EM28XX_RESOURCE_VBI;
900 	default:
901 		BUG();
902 		return 0;
903 	}
904 }
905 
906 /*
907  * ac97_queryctrl()
908  * return the ac97 supported controls
909  */
910 static int ac97_queryctrl(struct v4l2_queryctrl *qc)
911 {
912 	int i;
913 
914 	for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
915 		if (qc->id && qc->id == ac97_qctrl[i].id) {
916 			memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
917 			return 0;
918 		}
919 	}
920 
921 	/* Control is not ac97 related */
922 	return 1;
923 }
924 
925 /*
926  * ac97_get_ctrl()
927  * return the current values for ac97 mute and volume
928  */
929 static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
930 {
931 	switch (ctrl->id) {
932 	case V4L2_CID_AUDIO_MUTE:
933 		ctrl->value = dev->mute;
934 		return 0;
935 	case V4L2_CID_AUDIO_VOLUME:
936 		ctrl->value = dev->volume;
937 		return 0;
938 	default:
939 		/* Control is not ac97 related */
940 		return 1;
941 	}
942 }
943 
944 /*
945  * ac97_set_ctrl()
946  * set values for ac97 mute and volume
947  */
948 static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
949 {
950 	int i;
951 
952 	for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
953 		if (ctrl->id == ac97_qctrl[i].id)
954 			goto handle;
955 
956 	/* Announce that hasn't handle it */
957 	return 1;
958 
959 handle:
960 	if (ctrl->value < ac97_qctrl[i].minimum ||
961 	    ctrl->value > ac97_qctrl[i].maximum)
962 		return -ERANGE;
963 
964 	switch (ctrl->id) {
965 	case V4L2_CID_AUDIO_MUTE:
966 		dev->mute = ctrl->value;
967 		break;
968 	case V4L2_CID_AUDIO_VOLUME:
969 		dev->volume = ctrl->value;
970 		break;
971 	}
972 
973 	return em28xx_audio_analog_set(dev);
974 }
975 
976 static int check_dev(struct em28xx *dev)
977 {
978 	if (dev->state & DEV_DISCONNECTED) {
979 		em28xx_errdev("v4l2 ioctl: device not present\n");
980 		return -ENODEV;
981 	}
982 
983 	if (dev->state & DEV_MISCONFIGURED) {
984 		em28xx_errdev("v4l2 ioctl: device is misconfigured; "
985 			      "close and open it again\n");
986 		return -EIO;
987 	}
988 	return 0;
989 }
990 
991 static void get_scale(struct em28xx *dev,
992 			unsigned int width, unsigned int height,
993 			unsigned int *hscale, unsigned int *vscale)
994 {
995 	unsigned int          maxw = norm_maxw(dev);
996 	unsigned int          maxh = norm_maxh(dev);
997 
998 	*hscale = (((unsigned long)maxw) << 12) / width - 4096L;
999 	if (*hscale >= 0x4000)
1000 		*hscale = 0x3fff;
1001 
1002 	*vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1003 	if (*vscale >= 0x4000)
1004 		*vscale = 0x3fff;
1005 }
1006 
1007 /* ------------------------------------------------------------------
1008 	IOCTL vidioc handling
1009    ------------------------------------------------------------------*/
1010 
1011 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1012 					struct v4l2_format *f)
1013 {
1014 	struct em28xx_fh      *fh  = priv;
1015 	struct em28xx         *dev = fh->dev;
1016 
1017 	f->fmt.pix.width = dev->width;
1018 	f->fmt.pix.height = dev->height;
1019 	f->fmt.pix.pixelformat = dev->format->fourcc;
1020 	f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
1021 	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline  * dev->height;
1022 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1023 
1024 	/* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1025 	if (dev->progressive)
1026 		f->fmt.pix.field = V4L2_FIELD_NONE;
1027 	else
1028 		f->fmt.pix.field = dev->interlaced ?
1029 			   V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1030 	return 0;
1031 }
1032 
1033 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1034 {
1035 	unsigned int i;
1036 
1037 	for (i = 0; i < ARRAY_SIZE(format); i++)
1038 		if (format[i].fourcc == fourcc)
1039 			return &format[i];
1040 
1041 	return NULL;
1042 }
1043 
1044 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1045 			struct v4l2_format *f)
1046 {
1047 	struct em28xx_fh      *fh    = priv;
1048 	struct em28xx         *dev   = fh->dev;
1049 	unsigned int          width  = f->fmt.pix.width;
1050 	unsigned int          height = f->fmt.pix.height;
1051 	unsigned int          maxw   = norm_maxw(dev);
1052 	unsigned int          maxh   = norm_maxh(dev);
1053 	unsigned int          hscale, vscale;
1054 	struct em28xx_fmt     *fmt;
1055 
1056 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1057 	if (!fmt) {
1058 		em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1059 				f->fmt.pix.pixelformat);
1060 		return -EINVAL;
1061 	}
1062 
1063 	if (dev->board.is_em2800) {
1064 		/* the em2800 can only scale down to 50% */
1065 		height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1066 		width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1067                 /* MaxPacketSize for em2800 is too small to capture at full resolution
1068                  * use half of maxw as the scaler can only scale to 50% */
1069 		if (width == maxw && height == maxh)
1070 			width /= 2;
1071 	} else {
1072 		/* width must even because of the YUYV format
1073 		   height must be even because of interlacing */
1074 		v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1075 				      1, 0);
1076 	}
1077 
1078 	get_scale(dev, width, height, &hscale, &vscale);
1079 
1080 	width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1081 	height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1082 
1083 	f->fmt.pix.width = width;
1084 	f->fmt.pix.height = height;
1085 	f->fmt.pix.pixelformat = fmt->fourcc;
1086 	f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1087 	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1088 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1089 	if (dev->progressive)
1090 		f->fmt.pix.field = V4L2_FIELD_NONE;
1091 	else
1092 		f->fmt.pix.field = dev->interlaced ?
1093 			   V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1094 
1095 	return 0;
1096 }
1097 
1098 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1099 				   unsigned width, unsigned height)
1100 {
1101 	struct em28xx_fmt     *fmt;
1102 
1103 	fmt = format_by_fourcc(fourcc);
1104 	if (!fmt)
1105 		return -EINVAL;
1106 
1107 	dev->format = fmt;
1108 	dev->width  = width;
1109 	dev->height = height;
1110 
1111 	/* set new image size */
1112 	get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1113 
1114 	em28xx_set_alternate(dev);
1115 	em28xx_resolution_set(dev);
1116 
1117 	return 0;
1118 }
1119 
1120 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1121 			struct v4l2_format *f)
1122 {
1123 	struct em28xx_fh      *fh  = priv;
1124 	struct em28xx         *dev = fh->dev;
1125 	int                   rc;
1126 
1127 	rc = check_dev(dev);
1128 	if (rc < 0)
1129 		return rc;
1130 
1131 	vidioc_try_fmt_vid_cap(file, priv, f);
1132 
1133 	if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1134 		em28xx_errdev("%s queue busy\n", __func__);
1135 		return -EBUSY;
1136 	}
1137 
1138 	return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1139 				f->fmt.pix.width, f->fmt.pix.height);
1140 }
1141 
1142 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1143 {
1144 	struct em28xx_fh   *fh  = priv;
1145 	struct em28xx      *dev = fh->dev;
1146 	int                rc;
1147 
1148 	rc = check_dev(dev);
1149 	if (rc < 0)
1150 		return rc;
1151 
1152 	*norm = dev->norm;
1153 
1154 	return 0;
1155 }
1156 
1157 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1158 {
1159 	struct em28xx_fh   *fh  = priv;
1160 	struct em28xx      *dev = fh->dev;
1161 	int                rc;
1162 
1163 	rc = check_dev(dev);
1164 	if (rc < 0)
1165 		return rc;
1166 
1167 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1168 
1169 	return 0;
1170 }
1171 
1172 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1173 {
1174 	struct em28xx_fh   *fh  = priv;
1175 	struct em28xx      *dev = fh->dev;
1176 	struct v4l2_format f;
1177 	int                rc;
1178 
1179 	rc = check_dev(dev);
1180 	if (rc < 0)
1181 		return rc;
1182 
1183 	dev->norm = *norm;
1184 
1185 	/* Adjusts width/height, if needed */
1186 	f.fmt.pix.width = dev->width;
1187 	f.fmt.pix.height = dev->height;
1188 	vidioc_try_fmt_vid_cap(file, priv, &f);
1189 
1190 	/* set new image size */
1191 	dev->width = f.fmt.pix.width;
1192 	dev->height = f.fmt.pix.height;
1193 	get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1194 
1195 	em28xx_resolution_set(dev);
1196 	v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
1197 
1198 	return 0;
1199 }
1200 
1201 static int vidioc_g_parm(struct file *file, void *priv,
1202 			 struct v4l2_streamparm *p)
1203 {
1204 	struct em28xx_fh   *fh  = priv;
1205 	struct em28xx      *dev = fh->dev;
1206 	int rc = 0;
1207 
1208 	if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1209 		return -EINVAL;
1210 
1211 	if (dev->board.is_webcam)
1212 		rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1213 						video, g_parm, p);
1214 	else
1215 		v4l2_video_std_frame_period(dev->norm,
1216 						 &p->parm.capture.timeperframe);
1217 
1218 	return rc;
1219 }
1220 
1221 static int vidioc_s_parm(struct file *file, void *priv,
1222 			 struct v4l2_streamparm *p)
1223 {
1224 	struct em28xx_fh   *fh  = priv;
1225 	struct em28xx      *dev = fh->dev;
1226 
1227 	if (!dev->board.is_webcam)
1228 		return -EINVAL;
1229 
1230 	if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1231 		return -EINVAL;
1232 
1233 	return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1234 }
1235 
1236 static const char *iname[] = {
1237 	[EM28XX_VMUX_COMPOSITE1] = "Composite1",
1238 	[EM28XX_VMUX_COMPOSITE2] = "Composite2",
1239 	[EM28XX_VMUX_COMPOSITE3] = "Composite3",
1240 	[EM28XX_VMUX_COMPOSITE4] = "Composite4",
1241 	[EM28XX_VMUX_SVIDEO]     = "S-Video",
1242 	[EM28XX_VMUX_TELEVISION] = "Television",
1243 	[EM28XX_VMUX_CABLE]      = "Cable TV",
1244 	[EM28XX_VMUX_DVB]        = "DVB",
1245 	[EM28XX_VMUX_DEBUG]      = "for debug only",
1246 };
1247 
1248 static int vidioc_enum_input(struct file *file, void *priv,
1249 				struct v4l2_input *i)
1250 {
1251 	struct em28xx_fh   *fh  = priv;
1252 	struct em28xx      *dev = fh->dev;
1253 	unsigned int       n;
1254 
1255 	n = i->index;
1256 	if (n >= MAX_EM28XX_INPUT)
1257 		return -EINVAL;
1258 	if (0 == INPUT(n)->type)
1259 		return -EINVAL;
1260 
1261 	i->index = n;
1262 	i->type = V4L2_INPUT_TYPE_CAMERA;
1263 
1264 	strcpy(i->name, iname[INPUT(n)->type]);
1265 
1266 	if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1267 		(EM28XX_VMUX_CABLE == INPUT(n)->type))
1268 		i->type = V4L2_INPUT_TYPE_TUNER;
1269 
1270 	i->std = dev->vdev->tvnorms;
1271 
1272 	return 0;
1273 }
1274 
1275 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1276 {
1277 	struct em28xx_fh   *fh  = priv;
1278 	struct em28xx      *dev = fh->dev;
1279 
1280 	*i = dev->ctl_input;
1281 
1282 	return 0;
1283 }
1284 
1285 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1286 {
1287 	struct em28xx_fh   *fh  = priv;
1288 	struct em28xx      *dev = fh->dev;
1289 	int                rc;
1290 
1291 	rc = check_dev(dev);
1292 	if (rc < 0)
1293 		return rc;
1294 
1295 	if (i >= MAX_EM28XX_INPUT)
1296 		return -EINVAL;
1297 	if (0 == INPUT(i)->type)
1298 		return -EINVAL;
1299 
1300 	video_mux(dev, i);
1301 	return 0;
1302 }
1303 
1304 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1305 {
1306 	struct em28xx_fh   *fh    = priv;
1307 	struct em28xx      *dev   = fh->dev;
1308 
1309 	if (!dev->audio_mode.has_audio)
1310 		return -EINVAL;
1311 
1312 	switch (a->index) {
1313 	case EM28XX_AMUX_VIDEO:
1314 		strcpy(a->name, "Television");
1315 		break;
1316 	case EM28XX_AMUX_LINE_IN:
1317 		strcpy(a->name, "Line In");
1318 		break;
1319 	case EM28XX_AMUX_VIDEO2:
1320 		strcpy(a->name, "Television alt");
1321 		break;
1322 	case EM28XX_AMUX_PHONE:
1323 		strcpy(a->name, "Phone");
1324 		break;
1325 	case EM28XX_AMUX_MIC:
1326 		strcpy(a->name, "Mic");
1327 		break;
1328 	case EM28XX_AMUX_CD:
1329 		strcpy(a->name, "CD");
1330 		break;
1331 	case EM28XX_AMUX_AUX:
1332 		strcpy(a->name, "Aux");
1333 		break;
1334 	case EM28XX_AMUX_PCM_OUT:
1335 		strcpy(a->name, "PCM");
1336 		break;
1337 	default:
1338 		return -EINVAL;
1339 	}
1340 
1341 	a->index = dev->ctl_ainput;
1342 	a->capability = V4L2_AUDCAP_STEREO;
1343 
1344 	return 0;
1345 }
1346 
1347 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1348 {
1349 	struct em28xx_fh   *fh  = priv;
1350 	struct em28xx      *dev = fh->dev;
1351 
1352 
1353 	if (!dev->audio_mode.has_audio)
1354 		return -EINVAL;
1355 
1356 	if (a->index >= MAX_EM28XX_INPUT)
1357 		return -EINVAL;
1358 	if (0 == INPUT(a->index)->type)
1359 		return -EINVAL;
1360 
1361 	dev->ctl_ainput = INPUT(a->index)->amux;
1362 	dev->ctl_aoutput = INPUT(a->index)->aout;
1363 
1364 	if (!dev->ctl_aoutput)
1365 		dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1366 
1367 	return 0;
1368 }
1369 
1370 static int vidioc_queryctrl(struct file *file, void *priv,
1371 				struct v4l2_queryctrl *qc)
1372 {
1373 	struct em28xx_fh      *fh  = priv;
1374 	struct em28xx         *dev = fh->dev;
1375 	int                   id  = qc->id;
1376 	int                   rc;
1377 
1378 	rc = check_dev(dev);
1379 	if (rc < 0)
1380 		return rc;
1381 
1382 	memset(qc, 0, sizeof(*qc));
1383 
1384 	qc->id = id;
1385 
1386 	/* enumerate AC97 controls */
1387 	if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1388 		rc = ac97_queryctrl(qc);
1389 		if (!rc)
1390 			return 0;
1391 	}
1392 
1393 	/* enumerate V4L2 device controls */
1394 	v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1395 
1396 	if (qc->type)
1397 		return 0;
1398 	else
1399 		return -EINVAL;
1400 }
1401 
1402 /*
1403  * FIXME: This is an indirect way to check if a control exists at a
1404  * subdev. Instead of that hack, maybe the better would be to change all
1405  * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1406  */
1407 static int check_subdev_ctrl(struct em28xx *dev, int id)
1408 {
1409 	struct v4l2_queryctrl qc;
1410 
1411 	memset(&qc, 0, sizeof(qc));
1412 	qc.id = id;
1413 
1414 	/* enumerate V4L2 device controls */
1415 	v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1416 
1417 	if (qc.type)
1418 		return 0;
1419 	else
1420 		return -EINVAL;
1421 }
1422 
1423 static int vidioc_g_ctrl(struct file *file, void *priv,
1424 				struct v4l2_control *ctrl)
1425 {
1426 	struct em28xx_fh      *fh  = priv;
1427 	struct em28xx         *dev = fh->dev;
1428 	int                   rc;
1429 
1430 	rc = check_dev(dev);
1431 	if (rc < 0)
1432 		return rc;
1433 	rc = 0;
1434 
1435 	/* Set an AC97 control */
1436 	if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1437 		rc = ac97_get_ctrl(dev, ctrl);
1438 	else
1439 		rc = 1;
1440 
1441 	/* It were not an AC97 control. Sends it to the v4l2 dev interface */
1442 	if (rc == 1) {
1443 		if (check_subdev_ctrl(dev, ctrl->id))
1444 			return -EINVAL;
1445 
1446 		v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1447 		rc = 0;
1448 	}
1449 
1450 	return rc;
1451 }
1452 
1453 static int vidioc_s_ctrl(struct file *file, void *priv,
1454 				struct v4l2_control *ctrl)
1455 {
1456 	struct em28xx_fh      *fh  = priv;
1457 	struct em28xx         *dev = fh->dev;
1458 	int                   rc;
1459 
1460 	rc = check_dev(dev);
1461 	if (rc < 0)
1462 		return rc;
1463 
1464 	/* Set an AC97 control */
1465 	if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1466 		rc = ac97_set_ctrl(dev, ctrl);
1467 	else
1468 		rc = 1;
1469 
1470 	/* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1471 	if (rc == 1) {
1472 		rc = check_subdev_ctrl(dev, ctrl->id);
1473 		if (!rc)
1474 			v4l2_device_call_all(&dev->v4l2_dev, 0,
1475 					     core, s_ctrl, ctrl);
1476 		/*
1477 		 * In the case of non-AC97 volume controls, we still need
1478 		 * to do some setups at em28xx, in order to mute/unmute
1479 		 * and to adjust audio volume. However, the value ranges
1480 		 * should be checked by the corresponding V4L subdriver.
1481 		 */
1482 		switch (ctrl->id) {
1483 		case V4L2_CID_AUDIO_MUTE:
1484 			dev->mute = ctrl->value;
1485 			rc = em28xx_audio_analog_set(dev);
1486 			break;
1487 		case V4L2_CID_AUDIO_VOLUME:
1488 			dev->volume = ctrl->value;
1489 			rc = em28xx_audio_analog_set(dev);
1490 		}
1491 	}
1492 	return (rc < 0) ? rc : 0;
1493 }
1494 
1495 static int vidioc_g_tuner(struct file *file, void *priv,
1496 				struct v4l2_tuner *t)
1497 {
1498 	struct em28xx_fh      *fh  = priv;
1499 	struct em28xx         *dev = fh->dev;
1500 	int                   rc;
1501 
1502 	rc = check_dev(dev);
1503 	if (rc < 0)
1504 		return rc;
1505 
1506 	if (0 != t->index)
1507 		return -EINVAL;
1508 
1509 	strcpy(t->name, "Tuner");
1510 	t->type = V4L2_TUNER_ANALOG_TV;
1511 
1512 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1513 	return 0;
1514 }
1515 
1516 static int vidioc_s_tuner(struct file *file, void *priv,
1517 				struct v4l2_tuner *t)
1518 {
1519 	struct em28xx_fh      *fh  = priv;
1520 	struct em28xx         *dev = fh->dev;
1521 	int                   rc;
1522 
1523 	rc = check_dev(dev);
1524 	if (rc < 0)
1525 		return rc;
1526 
1527 	if (0 != t->index)
1528 		return -EINVAL;
1529 
1530 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1531 	return 0;
1532 }
1533 
1534 static int vidioc_g_frequency(struct file *file, void *priv,
1535 				struct v4l2_frequency *f)
1536 {
1537 	struct em28xx_fh      *fh  = priv;
1538 	struct em28xx         *dev = fh->dev;
1539 
1540 	f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1541 	f->frequency = dev->ctl_freq;
1542 	return 0;
1543 }
1544 
1545 static int vidioc_s_frequency(struct file *file, void *priv,
1546 				struct v4l2_frequency *f)
1547 {
1548 	struct em28xx_fh      *fh  = priv;
1549 	struct em28xx         *dev = fh->dev;
1550 	int                   rc;
1551 
1552 	rc = check_dev(dev);
1553 	if (rc < 0)
1554 		return rc;
1555 
1556 	if (0 != f->tuner)
1557 		return -EINVAL;
1558 
1559 	if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1560 		return -EINVAL;
1561 	if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1562 		return -EINVAL;
1563 
1564 	dev->ctl_freq = f->frequency;
1565 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1566 
1567 	return 0;
1568 }
1569 
1570 #ifdef CONFIG_VIDEO_ADV_DEBUG
1571 static int em28xx_reg_len(int reg)
1572 {
1573 	switch (reg) {
1574 	case EM28XX_R40_AC97LSB:
1575 	case EM28XX_R30_HSCALELOW:
1576 	case EM28XX_R32_VSCALELOW:
1577 		return 2;
1578 	default:
1579 		return 1;
1580 	}
1581 }
1582 
1583 static int vidioc_g_chip_ident(struct file *file, void *priv,
1584 	       struct v4l2_dbg_chip_ident *chip)
1585 {
1586 	struct em28xx_fh      *fh  = priv;
1587 	struct em28xx         *dev = fh->dev;
1588 
1589 	chip->ident = V4L2_IDENT_NONE;
1590 	chip->revision = 0;
1591 
1592 	v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1593 
1594 	return 0;
1595 }
1596 
1597 
1598 static int vidioc_g_register(struct file *file, void *priv,
1599 			     struct v4l2_dbg_register *reg)
1600 {
1601 	struct em28xx_fh      *fh  = priv;
1602 	struct em28xx         *dev = fh->dev;
1603 	int ret;
1604 
1605 	switch (reg->match.type) {
1606 	case V4L2_CHIP_MATCH_AC97:
1607 		ret = em28xx_read_ac97(dev, reg->reg);
1608 		if (ret < 0)
1609 			return ret;
1610 
1611 		reg->val = ret;
1612 		reg->size = 1;
1613 		return 0;
1614 	case V4L2_CHIP_MATCH_I2C_DRIVER:
1615 		v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1616 		return 0;
1617 	case V4L2_CHIP_MATCH_I2C_ADDR:
1618 		/* TODO: is this correct? */
1619 		v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1620 		return 0;
1621 	default:
1622 		if (!v4l2_chip_match_host(&reg->match))
1623 			return -EINVAL;
1624 	}
1625 
1626 	/* Match host */
1627 	reg->size = em28xx_reg_len(reg->reg);
1628 	if (reg->size == 1) {
1629 		ret = em28xx_read_reg(dev, reg->reg);
1630 
1631 		if (ret < 0)
1632 			return ret;
1633 
1634 		reg->val = ret;
1635 	} else {
1636 		__le16 val = 0;
1637 		ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1638 						   reg->reg, (char *)&val, 2);
1639 		if (ret < 0)
1640 			return ret;
1641 
1642 		reg->val = le16_to_cpu(val);
1643 	}
1644 
1645 	return 0;
1646 }
1647 
1648 static int vidioc_s_register(struct file *file, void *priv,
1649 			     struct v4l2_dbg_register *reg)
1650 {
1651 	struct em28xx_fh      *fh  = priv;
1652 	struct em28xx         *dev = fh->dev;
1653 	__le16 buf;
1654 
1655 	switch (reg->match.type) {
1656 	case V4L2_CHIP_MATCH_AC97:
1657 		return em28xx_write_ac97(dev, reg->reg, reg->val);
1658 	case V4L2_CHIP_MATCH_I2C_DRIVER:
1659 		v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1660 		return 0;
1661 	case V4L2_CHIP_MATCH_I2C_ADDR:
1662 		/* TODO: is this correct? */
1663 		v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1664 		return 0;
1665 	default:
1666 		if (!v4l2_chip_match_host(&reg->match))
1667 			return -EINVAL;
1668 	}
1669 
1670 	/* Match host */
1671 	buf = cpu_to_le16(reg->val);
1672 
1673 	return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1674 			       em28xx_reg_len(reg->reg));
1675 }
1676 #endif
1677 
1678 
1679 static int vidioc_cropcap(struct file *file, void *priv,
1680 					struct v4l2_cropcap *cc)
1681 {
1682 	struct em28xx_fh      *fh  = priv;
1683 	struct em28xx         *dev = fh->dev;
1684 
1685 	if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1686 		return -EINVAL;
1687 
1688 	cc->bounds.left = 0;
1689 	cc->bounds.top = 0;
1690 	cc->bounds.width = dev->width;
1691 	cc->bounds.height = dev->height;
1692 	cc->defrect = cc->bounds;
1693 	cc->pixelaspect.numerator = 54;	/* 4:3 FIXME: remove magic numbers */
1694 	cc->pixelaspect.denominator = 59;
1695 
1696 	return 0;
1697 }
1698 
1699 static int vidioc_streamon(struct file *file, void *priv,
1700 					enum v4l2_buf_type type)
1701 {
1702 	struct em28xx_fh      *fh  = priv;
1703 	struct em28xx         *dev = fh->dev;
1704 	int                   rc = -EINVAL;
1705 
1706 	rc = check_dev(dev);
1707 	if (rc < 0)
1708 		return rc;
1709 
1710 	if (unlikely(type != fh->type))
1711 		return -EINVAL;
1712 
1713 	em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1714 			fh, type, fh->resources, dev->resources);
1715 
1716 	if (unlikely(!res_get(fh, get_ressource(fh))))
1717 		return -EBUSY;
1718 
1719 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1720 		rc = videobuf_streamon(&fh->vb_vidq);
1721 	else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1722 		rc = videobuf_streamon(&fh->vb_vbiq);
1723 
1724 	return rc;
1725 }
1726 
1727 static int vidioc_streamoff(struct file *file, void *priv,
1728 					enum v4l2_buf_type type)
1729 {
1730 	struct em28xx_fh      *fh  = priv;
1731 	struct em28xx         *dev = fh->dev;
1732 	int                   rc;
1733 
1734 	rc = check_dev(dev);
1735 	if (rc < 0)
1736 		return rc;
1737 
1738 	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1739 	    fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1740 		return -EINVAL;
1741 	if (type != fh->type)
1742 		return -EINVAL;
1743 
1744 	em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1745 			fh, type, fh->resources, dev->resources);
1746 
1747 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1748 		if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1749 			videobuf_streamoff(&fh->vb_vidq);
1750 			res_free(fh, EM28XX_RESOURCE_VIDEO);
1751 		}
1752 	} else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1753 		if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1754 			videobuf_streamoff(&fh->vb_vbiq);
1755 			res_free(fh, EM28XX_RESOURCE_VBI);
1756 		}
1757 	}
1758 
1759 	return 0;
1760 }
1761 
1762 static int vidioc_querycap(struct file *file, void  *priv,
1763 					struct v4l2_capability *cap)
1764 {
1765 	struct em28xx_fh      *fh  = priv;
1766 	struct em28xx         *dev = fh->dev;
1767 
1768 	strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1769 	strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1770 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1771 
1772 	cap->capabilities =
1773 			V4L2_CAP_SLICED_VBI_CAPTURE |
1774 			V4L2_CAP_VIDEO_CAPTURE |
1775 			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1776 
1777 	if (dev->vbi_dev)
1778 		cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1779 
1780 	if (dev->audio_mode.has_audio)
1781 		cap->capabilities |= V4L2_CAP_AUDIO;
1782 
1783 	if (dev->tuner_type != TUNER_ABSENT)
1784 		cap->capabilities |= V4L2_CAP_TUNER;
1785 
1786 	return 0;
1787 }
1788 
1789 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1790 					struct v4l2_fmtdesc *f)
1791 {
1792 	if (unlikely(f->index >= ARRAY_SIZE(format)))
1793 		return -EINVAL;
1794 
1795 	strlcpy(f->description, format[f->index].name, sizeof(f->description));
1796 	f->pixelformat = format[f->index].fourcc;
1797 
1798 	return 0;
1799 }
1800 
1801 static int vidioc_enum_framesizes(struct file *file, void *priv,
1802 				  struct v4l2_frmsizeenum *fsize)
1803 {
1804 	struct em28xx_fh      *fh  = priv;
1805 	struct em28xx         *dev = fh->dev;
1806 	struct em28xx_fmt     *fmt;
1807 	unsigned int	      maxw = norm_maxw(dev);
1808 	unsigned int	      maxh = norm_maxh(dev);
1809 
1810 	fmt = format_by_fourcc(fsize->pixel_format);
1811 	if (!fmt) {
1812 		em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1813 				fsize->pixel_format);
1814 		return -EINVAL;
1815 	}
1816 
1817 	if (dev->board.is_em2800) {
1818 		if (fsize->index > 1)
1819 			return -EINVAL;
1820 		fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1821 		fsize->discrete.width = maxw / (1 + fsize->index);
1822 		fsize->discrete.height = maxh / (1 + fsize->index);
1823 		return 0;
1824 	}
1825 
1826 	if (fsize->index != 0)
1827 		return -EINVAL;
1828 
1829 	/* Report a continuous range */
1830 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1831 	fsize->stepwise.min_width = 48;
1832 	fsize->stepwise.min_height = 32;
1833 	fsize->stepwise.max_width = maxw;
1834 	fsize->stepwise.max_height = maxh;
1835 	fsize->stepwise.step_width = 1;
1836 	fsize->stepwise.step_height = 1;
1837 	return 0;
1838 }
1839 
1840 /* Sliced VBI ioctls */
1841 static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1842 					struct v4l2_format *f)
1843 {
1844 	struct em28xx_fh      *fh  = priv;
1845 	struct em28xx         *dev = fh->dev;
1846 	int                   rc;
1847 
1848 	rc = check_dev(dev);
1849 	if (rc < 0)
1850 		return rc;
1851 
1852 	f->fmt.sliced.service_set = 0;
1853 	v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1854 
1855 	if (f->fmt.sliced.service_set == 0)
1856 		rc = -EINVAL;
1857 
1858 	return rc;
1859 }
1860 
1861 static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1862 			struct v4l2_format *f)
1863 {
1864 	struct em28xx_fh      *fh  = priv;
1865 	struct em28xx         *dev = fh->dev;
1866 	int                   rc;
1867 
1868 	rc = check_dev(dev);
1869 	if (rc < 0)
1870 		return rc;
1871 
1872 	v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1873 
1874 	if (f->fmt.sliced.service_set == 0)
1875 		return -EINVAL;
1876 
1877 	return 0;
1878 }
1879 
1880 /* RAW VBI ioctls */
1881 
1882 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1883 				struct v4l2_format *format)
1884 {
1885 	struct em28xx_fh      *fh  = priv;
1886 	struct em28xx         *dev = fh->dev;
1887 
1888 	format->fmt.vbi.samples_per_line = dev->vbi_width;
1889 	format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1890 	format->fmt.vbi.offset = 0;
1891 	format->fmt.vbi.flags = 0;
1892 	format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1893 	format->fmt.vbi.count[0] = dev->vbi_height;
1894 	format->fmt.vbi.count[1] = dev->vbi_height;
1895 
1896 	/* Varies by video standard (NTSC, PAL, etc.) */
1897 	if (dev->norm & V4L2_STD_525_60) {
1898 		/* NTSC */
1899 		format->fmt.vbi.start[0] = 10;
1900 		format->fmt.vbi.start[1] = 273;
1901 	} else if (dev->norm & V4L2_STD_625_50) {
1902 		/* PAL */
1903 		format->fmt.vbi.start[0] = 6;
1904 		format->fmt.vbi.start[1] = 318;
1905 	}
1906 
1907 	return 0;
1908 }
1909 
1910 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1911 				struct v4l2_format *format)
1912 {
1913 	struct em28xx_fh      *fh  = priv;
1914 	struct em28xx         *dev = fh->dev;
1915 
1916 	format->fmt.vbi.samples_per_line = dev->vbi_width;
1917 	format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1918 	format->fmt.vbi.offset = 0;
1919 	format->fmt.vbi.flags = 0;
1920 	format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1921 	format->fmt.vbi.count[0] = dev->vbi_height;
1922 	format->fmt.vbi.count[1] = dev->vbi_height;
1923 
1924 	/* Varies by video standard (NTSC, PAL, etc.) */
1925 	if (dev->norm & V4L2_STD_525_60) {
1926 		/* NTSC */
1927 		format->fmt.vbi.start[0] = 10;
1928 		format->fmt.vbi.start[1] = 273;
1929 	} else if (dev->norm & V4L2_STD_625_50) {
1930 		/* PAL */
1931 		format->fmt.vbi.start[0] = 6;
1932 		format->fmt.vbi.start[1] = 318;
1933 	}
1934 
1935 	return 0;
1936 }
1937 
1938 static int vidioc_reqbufs(struct file *file, void *priv,
1939 			  struct v4l2_requestbuffers *rb)
1940 {
1941 	struct em28xx_fh      *fh  = priv;
1942 	struct em28xx         *dev = fh->dev;
1943 	int                   rc;
1944 
1945 	rc = check_dev(dev);
1946 	if (rc < 0)
1947 		return rc;
1948 
1949 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1950 		return videobuf_reqbufs(&fh->vb_vidq, rb);
1951 	else
1952 		return videobuf_reqbufs(&fh->vb_vbiq, rb);
1953 }
1954 
1955 static int vidioc_querybuf(struct file *file, void *priv,
1956 			   struct v4l2_buffer *b)
1957 {
1958 	struct em28xx_fh      *fh  = priv;
1959 	struct em28xx         *dev = fh->dev;
1960 	int                   rc;
1961 
1962 	rc = check_dev(dev);
1963 	if (rc < 0)
1964 		return rc;
1965 
1966 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1967 		return videobuf_querybuf(&fh->vb_vidq, b);
1968 	else {
1969 		/* FIXME: I'm not sure yet whether this is a bug in zvbi or
1970 		   the videobuf framework, but we probably shouldn't be
1971 		   returning a buffer larger than that which was asked for.
1972 		   At a minimum, it causes a crash in zvbi since it does
1973 		   a memcpy based on the source buffer length */
1974 		int result = videobuf_querybuf(&fh->vb_vbiq, b);
1975 		b->length = dev->vbi_width * dev->vbi_height * 2;
1976 
1977 		return result;
1978 	}
1979 }
1980 
1981 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1982 {
1983 	struct em28xx_fh      *fh  = priv;
1984 	struct em28xx         *dev = fh->dev;
1985 	int                   rc;
1986 
1987 	rc = check_dev(dev);
1988 	if (rc < 0)
1989 		return rc;
1990 
1991 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1992 		return videobuf_qbuf(&fh->vb_vidq, b);
1993 	else
1994 		return videobuf_qbuf(&fh->vb_vbiq, b);
1995 }
1996 
1997 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1998 {
1999 	struct em28xx_fh      *fh  = priv;
2000 	struct em28xx         *dev = fh->dev;
2001 	int                   rc;
2002 
2003 	rc = check_dev(dev);
2004 	if (rc < 0)
2005 		return rc;
2006 
2007 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2008 		return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2009 				      O_NONBLOCK);
2010 	else
2011 		return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2012 				      O_NONBLOCK);
2013 }
2014 
2015 /* ----------------------------------------------------------- */
2016 /* RADIO ESPECIFIC IOCTLS                                      */
2017 /* ----------------------------------------------------------- */
2018 
2019 static int radio_querycap(struct file *file, void  *priv,
2020 			  struct v4l2_capability *cap)
2021 {
2022 	struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2023 
2024 	strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2025 	strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
2026 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
2027 
2028 	cap->capabilities = V4L2_CAP_TUNER;
2029 	return 0;
2030 }
2031 
2032 static int radio_g_tuner(struct file *file, void *priv,
2033 			 struct v4l2_tuner *t)
2034 {
2035 	struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2036 
2037 	if (unlikely(t->index > 0))
2038 		return -EINVAL;
2039 
2040 	strcpy(t->name, "Radio");
2041 	t->type = V4L2_TUNER_RADIO;
2042 
2043 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
2044 
2045 	return 0;
2046 }
2047 
2048 static int radio_enum_input(struct file *file, void *priv,
2049 			    struct v4l2_input *i)
2050 {
2051 	if (i->index != 0)
2052 		return -EINVAL;
2053 	strcpy(i->name, "Radio");
2054 	i->type = V4L2_INPUT_TYPE_TUNER;
2055 
2056 	return 0;
2057 }
2058 
2059 static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2060 {
2061 	if (unlikely(a->index))
2062 		return -EINVAL;
2063 
2064 	strcpy(a->name, "Radio");
2065 	return 0;
2066 }
2067 
2068 static int radio_s_tuner(struct file *file, void *priv,
2069 			 struct v4l2_tuner *t)
2070 {
2071 	struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2072 
2073 	if (0 != t->index)
2074 		return -EINVAL;
2075 
2076 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
2077 
2078 	return 0;
2079 }
2080 
2081 static int radio_s_audio(struct file *file, void *fh,
2082 			 const struct v4l2_audio *a)
2083 {
2084 	return 0;
2085 }
2086 
2087 static int radio_s_input(struct file *file, void *fh, unsigned int i)
2088 {
2089 	return 0;
2090 }
2091 
2092 static int radio_queryctrl(struct file *file, void *priv,
2093 			   struct v4l2_queryctrl *qc)
2094 {
2095 	int i;
2096 
2097 	if (qc->id <  V4L2_CID_BASE ||
2098 		qc->id >= V4L2_CID_LASTP1)
2099 		return -EINVAL;
2100 
2101 	for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2102 		if (qc->id && qc->id == ac97_qctrl[i].id) {
2103 			memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
2104 			return 0;
2105 		}
2106 	}
2107 
2108 	return -EINVAL;
2109 }
2110 
2111 /*
2112  * em28xx_v4l2_open()
2113  * inits the device and starts isoc transfer
2114  */
2115 static int em28xx_v4l2_open(struct file *filp)
2116 {
2117 	int errCode = 0, radio = 0;
2118 	struct video_device *vdev = video_devdata(filp);
2119 	struct em28xx *dev = video_drvdata(filp);
2120 	enum v4l2_buf_type fh_type = 0;
2121 	struct em28xx_fh *fh;
2122 	enum v4l2_field field;
2123 
2124 	switch (vdev->vfl_type) {
2125 	case VFL_TYPE_GRABBER:
2126 		fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2127 		break;
2128 	case VFL_TYPE_VBI:
2129 		fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2130 		break;
2131 	case VFL_TYPE_RADIO:
2132 		radio = 1;
2133 		break;
2134 	}
2135 
2136 	em28xx_videodbg("open dev=%s type=%s users=%d\n",
2137 			video_device_node_name(vdev), v4l2_type_names[fh_type],
2138 			dev->users);
2139 
2140 
2141 	if (mutex_lock_interruptible(&dev->lock))
2142 		return -ERESTARTSYS;
2143 	fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
2144 	if (!fh) {
2145 		em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2146 		mutex_unlock(&dev->lock);
2147 		return -ENOMEM;
2148 	}
2149 	fh->dev = dev;
2150 	fh->radio = radio;
2151 	fh->type = fh_type;
2152 	filp->private_data = fh;
2153 
2154 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2155 		em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
2156 		em28xx_set_alternate(dev);
2157 		em28xx_resolution_set(dev);
2158 
2159 		/* Needed, since GPIO might have disabled power of
2160 		   some i2c device
2161 		 */
2162 		em28xx_wake_i2c(dev);
2163 
2164 	}
2165 	if (fh->radio) {
2166 		em28xx_videodbg("video_open: setting radio device\n");
2167 		v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
2168 	}
2169 
2170 	dev->users++;
2171 
2172 	if (dev->progressive)
2173 		field = V4L2_FIELD_NONE;
2174 	else
2175 		field = V4L2_FIELD_INTERLACED;
2176 
2177 	videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2178 				    NULL, &dev->slock,
2179 				    V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2180 				    sizeof(struct em28xx_buffer), fh, &dev->lock);
2181 
2182 	videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2183 				    NULL, &dev->slock,
2184 				    V4L2_BUF_TYPE_VBI_CAPTURE,
2185 				    V4L2_FIELD_SEQ_TB,
2186 				    sizeof(struct em28xx_buffer), fh, &dev->lock);
2187 	mutex_unlock(&dev->lock);
2188 
2189 	return errCode;
2190 }
2191 
2192 /*
2193  * em28xx_realease_resources()
2194  * unregisters the v4l2,i2c and usb devices
2195  * called when the device gets disconected or at module unload
2196 */
2197 void em28xx_release_analog_resources(struct em28xx *dev)
2198 {
2199 
2200 	/*FIXME: I2C IR should be disconnected */
2201 
2202 	if (dev->radio_dev) {
2203 		if (video_is_registered(dev->radio_dev))
2204 			video_unregister_device(dev->radio_dev);
2205 		else
2206 			video_device_release(dev->radio_dev);
2207 		dev->radio_dev = NULL;
2208 	}
2209 	if (dev->vbi_dev) {
2210 		em28xx_info("V4L2 device %s deregistered\n",
2211 			    video_device_node_name(dev->vbi_dev));
2212 		if (video_is_registered(dev->vbi_dev))
2213 			video_unregister_device(dev->vbi_dev);
2214 		else
2215 			video_device_release(dev->vbi_dev);
2216 		dev->vbi_dev = NULL;
2217 	}
2218 	if (dev->vdev) {
2219 		em28xx_info("V4L2 device %s deregistered\n",
2220 			    video_device_node_name(dev->vdev));
2221 		if (video_is_registered(dev->vdev))
2222 			video_unregister_device(dev->vdev);
2223 		else
2224 			video_device_release(dev->vdev);
2225 		dev->vdev = NULL;
2226 	}
2227 }
2228 
2229 /*
2230  * em28xx_v4l2_close()
2231  * stops streaming and deallocates all resources allocated by the v4l2
2232  * calls and ioctls
2233  */
2234 static int em28xx_v4l2_close(struct file *filp)
2235 {
2236 	struct em28xx_fh *fh  = filp->private_data;
2237 	struct em28xx    *dev = fh->dev;
2238 	int              errCode;
2239 
2240 	em28xx_videodbg("users=%d\n", dev->users);
2241 
2242 	mutex_lock(&dev->lock);
2243 	if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
2244 		videobuf_stop(&fh->vb_vidq);
2245 		res_free(fh, EM28XX_RESOURCE_VIDEO);
2246 	}
2247 
2248 	if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2249 		videobuf_stop(&fh->vb_vbiq);
2250 		res_free(fh, EM28XX_RESOURCE_VBI);
2251 	}
2252 
2253 	if (dev->users == 1) {
2254 		/* the device is already disconnect,
2255 		   free the remaining resources */
2256 		if (dev->state & DEV_DISCONNECTED) {
2257 			em28xx_release_resources(dev);
2258 			kfree(dev->alt_max_pkt_size);
2259 			mutex_unlock(&dev->lock);
2260 			kfree(dev);
2261 			kfree(fh);
2262 			return 0;
2263 		}
2264 
2265 		/* Save some power by putting tuner to sleep */
2266 		v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
2267 
2268 		/* do this before setting alternate! */
2269 		em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
2270 		em28xx_set_mode(dev, EM28XX_SUSPEND);
2271 
2272 		/* set alternate 0 */
2273 		dev->alt = 0;
2274 		em28xx_videodbg("setting alternate 0\n");
2275 		errCode = usb_set_interface(dev->udev, 0, 0);
2276 		if (errCode < 0) {
2277 			em28xx_errdev("cannot change alternate number to "
2278 					"0 (error=%i)\n", errCode);
2279 		}
2280 	}
2281 
2282 	videobuf_mmap_free(&fh->vb_vidq);
2283 	videobuf_mmap_free(&fh->vb_vbiq);
2284 	kfree(fh);
2285 	dev->users--;
2286 	mutex_unlock(&dev->lock);
2287 	return 0;
2288 }
2289 
2290 /*
2291  * em28xx_v4l2_read()
2292  * will allocate buffers when called for the first time
2293  */
2294 static ssize_t
2295 em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2296 		 loff_t *pos)
2297 {
2298 	struct em28xx_fh *fh = filp->private_data;
2299 	struct em28xx *dev = fh->dev;
2300 	int rc;
2301 
2302 	rc = check_dev(dev);
2303 	if (rc < 0)
2304 		return rc;
2305 
2306 	if (mutex_lock_interruptible(&dev->lock))
2307 		return -ERESTARTSYS;
2308 	/* FIXME: read() is not prepared to allow changing the video
2309 	   resolution while streaming. Seems a bug at em28xx_set_fmt
2310 	 */
2311 
2312 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2313 		if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2314 			rc = -EBUSY;
2315 		else
2316 			rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2317 					filp->f_flags & O_NONBLOCK);
2318 	} else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2319 		if (!res_get(fh, EM28XX_RESOURCE_VBI))
2320 			rc = -EBUSY;
2321 		else
2322 			rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2323 					filp->f_flags & O_NONBLOCK);
2324 	}
2325 	mutex_unlock(&dev->lock);
2326 
2327 	return rc;
2328 }
2329 
2330 /*
2331  * em28xx_poll()
2332  * will allocate buffers when called for the first time
2333  */
2334 static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
2335 {
2336 	struct em28xx_fh *fh = filp->private_data;
2337 	struct em28xx *dev = fh->dev;
2338 	int rc;
2339 
2340 	rc = check_dev(dev);
2341 	if (rc < 0)
2342 		return rc;
2343 
2344 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2345 		if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2346 			return POLLERR;
2347 		return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2348 	} else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2349 		if (!res_get(fh, EM28XX_RESOURCE_VBI))
2350 			return POLLERR;
2351 		return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
2352 	} else {
2353 		return POLLERR;
2354 	}
2355 }
2356 
2357 static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2358 {
2359 	struct em28xx_fh *fh = filp->private_data;
2360 	struct em28xx *dev = fh->dev;
2361 	unsigned int res;
2362 
2363 	mutex_lock(&dev->lock);
2364 	res = em28xx_poll(filp, wait);
2365 	mutex_unlock(&dev->lock);
2366 	return res;
2367 }
2368 
2369 /*
2370  * em28xx_v4l2_mmap()
2371  */
2372 static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2373 {
2374 	struct em28xx_fh *fh    = filp->private_data;
2375 	struct em28xx	 *dev   = fh->dev;
2376 	int		 rc;
2377 
2378 	rc = check_dev(dev);
2379 	if (rc < 0)
2380 		return rc;
2381 
2382 	if (mutex_lock_interruptible(&dev->lock))
2383 		return -ERESTARTSYS;
2384 	if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2385 		rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2386 	else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2387 		rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
2388 	mutex_unlock(&dev->lock);
2389 
2390 	em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2391 		(unsigned long)vma->vm_start,
2392 		(unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2393 		rc);
2394 
2395 	return rc;
2396 }
2397 
2398 static const struct v4l2_file_operations em28xx_v4l_fops = {
2399 	.owner         = THIS_MODULE,
2400 	.open          = em28xx_v4l2_open,
2401 	.release       = em28xx_v4l2_close,
2402 	.read          = em28xx_v4l2_read,
2403 	.poll          = em28xx_v4l2_poll,
2404 	.mmap          = em28xx_v4l2_mmap,
2405 	.unlocked_ioctl = video_ioctl2,
2406 };
2407 
2408 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2409 	.vidioc_querycap            = vidioc_querycap,
2410 	.vidioc_enum_fmt_vid_cap    = vidioc_enum_fmt_vid_cap,
2411 	.vidioc_g_fmt_vid_cap       = vidioc_g_fmt_vid_cap,
2412 	.vidioc_try_fmt_vid_cap     = vidioc_try_fmt_vid_cap,
2413 	.vidioc_s_fmt_vid_cap       = vidioc_s_fmt_vid_cap,
2414 	.vidioc_g_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
2415 	.vidioc_s_fmt_vbi_cap       = vidioc_s_fmt_vbi_cap,
2416 	.vidioc_enum_framesizes     = vidioc_enum_framesizes,
2417 	.vidioc_g_audio             = vidioc_g_audio,
2418 	.vidioc_s_audio             = vidioc_s_audio,
2419 	.vidioc_cropcap             = vidioc_cropcap,
2420 	.vidioc_g_fmt_sliced_vbi_cap   = vidioc_g_fmt_sliced_vbi_cap,
2421 	.vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2422 	.vidioc_s_fmt_sliced_vbi_cap   = vidioc_try_set_sliced_vbi_cap,
2423 
2424 	.vidioc_reqbufs             = vidioc_reqbufs,
2425 	.vidioc_querybuf            = vidioc_querybuf,
2426 	.vidioc_qbuf                = vidioc_qbuf,
2427 	.vidioc_dqbuf               = vidioc_dqbuf,
2428 	.vidioc_g_std               = vidioc_g_std,
2429 	.vidioc_querystd            = vidioc_querystd,
2430 	.vidioc_s_std               = vidioc_s_std,
2431 	.vidioc_g_parm		    = vidioc_g_parm,
2432 	.vidioc_s_parm		    = vidioc_s_parm,
2433 	.vidioc_enum_input          = vidioc_enum_input,
2434 	.vidioc_g_input             = vidioc_g_input,
2435 	.vidioc_s_input             = vidioc_s_input,
2436 	.vidioc_queryctrl           = vidioc_queryctrl,
2437 	.vidioc_g_ctrl              = vidioc_g_ctrl,
2438 	.vidioc_s_ctrl              = vidioc_s_ctrl,
2439 	.vidioc_streamon            = vidioc_streamon,
2440 	.vidioc_streamoff           = vidioc_streamoff,
2441 	.vidioc_g_tuner             = vidioc_g_tuner,
2442 	.vidioc_s_tuner             = vidioc_s_tuner,
2443 	.vidioc_g_frequency         = vidioc_g_frequency,
2444 	.vidioc_s_frequency         = vidioc_s_frequency,
2445 #ifdef CONFIG_VIDEO_ADV_DEBUG
2446 	.vidioc_g_register          = vidioc_g_register,
2447 	.vidioc_s_register          = vidioc_s_register,
2448 	.vidioc_g_chip_ident        = vidioc_g_chip_ident,
2449 #endif
2450 };
2451 
2452 static const struct video_device em28xx_video_template = {
2453 	.fops                       = &em28xx_v4l_fops,
2454 	.release                    = video_device_release,
2455 	.ioctl_ops 		    = &video_ioctl_ops,
2456 
2457 	.tvnorms                    = V4L2_STD_ALL,
2458 	.current_norm               = V4L2_STD_PAL,
2459 };
2460 
2461 static const struct v4l2_file_operations radio_fops = {
2462 	.owner         = THIS_MODULE,
2463 	.open          = em28xx_v4l2_open,
2464 	.release       = em28xx_v4l2_close,
2465 	.unlocked_ioctl = video_ioctl2,
2466 };
2467 
2468 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2469 	.vidioc_querycap      = radio_querycap,
2470 	.vidioc_g_tuner       = radio_g_tuner,
2471 	.vidioc_enum_input    = radio_enum_input,
2472 	.vidioc_g_audio       = radio_g_audio,
2473 	.vidioc_s_tuner       = radio_s_tuner,
2474 	.vidioc_s_audio       = radio_s_audio,
2475 	.vidioc_s_input       = radio_s_input,
2476 	.vidioc_queryctrl     = radio_queryctrl,
2477 	.vidioc_g_ctrl        = vidioc_g_ctrl,
2478 	.vidioc_s_ctrl        = vidioc_s_ctrl,
2479 	.vidioc_g_frequency   = vidioc_g_frequency,
2480 	.vidioc_s_frequency   = vidioc_s_frequency,
2481 #ifdef CONFIG_VIDEO_ADV_DEBUG
2482 	.vidioc_g_register    = vidioc_g_register,
2483 	.vidioc_s_register    = vidioc_s_register,
2484 #endif
2485 };
2486 
2487 static struct video_device em28xx_radio_template = {
2488 	.name                 = "em28xx-radio",
2489 	.fops                 = &radio_fops,
2490 	.ioctl_ops 	      = &radio_ioctl_ops,
2491 };
2492 
2493 /******************************** usb interface ******************************/
2494 
2495 
2496 
2497 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2498 					const struct video_device *template,
2499 					const char *type_name)
2500 {
2501 	struct video_device *vfd;
2502 
2503 	vfd = video_device_alloc();
2504 	if (NULL == vfd)
2505 		return NULL;
2506 
2507 	*vfd		= *template;
2508 	vfd->v4l2_dev	= &dev->v4l2_dev;
2509 	vfd->release	= video_device_release;
2510 	vfd->debug	= video_debug;
2511 	vfd->lock	= &dev->lock;
2512 
2513 	snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2514 		 dev->name, type_name);
2515 
2516 	video_set_drvdata(vfd, dev);
2517 	return vfd;
2518 }
2519 
2520 int em28xx_register_analog_devices(struct em28xx *dev)
2521 {
2522       u8 val;
2523 	int ret;
2524 	unsigned int maxw;
2525 
2526 	printk(KERN_INFO "%s: v4l2 driver version %s\n",
2527 		dev->name, EM28XX_VERSION);
2528 
2529 	/* set default norm */
2530 	dev->norm = em28xx_video_template.current_norm;
2531 	v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
2532 	dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2533 
2534 	/* Analog specific initialization */
2535 	dev->format = &format[0];
2536 
2537 	maxw = norm_maxw(dev);
2538         /* MaxPacketSize for em2800 is too small to capture at full resolution
2539          * use half of maxw as the scaler can only scale to 50% */
2540         if (dev->board.is_em2800)
2541             maxw /= 2;
2542 
2543 	em28xx_set_video_format(dev, format[0].fourcc,
2544 				maxw, norm_maxh(dev));
2545 
2546 	video_mux(dev, 0);
2547 
2548 	/* Audio defaults */
2549 	dev->mute = 1;
2550 	dev->volume = 0x1f;
2551 
2552 /*	em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2553 	val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2554 	em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2555 			 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2556 
2557 	em28xx_set_outfmt(dev);
2558 	em28xx_colorlevels_set_default(dev);
2559 	em28xx_compression_disable(dev);
2560 
2561 	/* allocate and fill video video_device struct */
2562 	dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2563 	if (!dev->vdev) {
2564 		em28xx_errdev("cannot allocate video_device.\n");
2565 		return -ENODEV;
2566 	}
2567 
2568 	/* register v4l2 video video_device */
2569 	ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2570 				       video_nr[dev->devno]);
2571 	if (ret) {
2572 		em28xx_errdev("unable to register video device (error=%i).\n",
2573 			      ret);
2574 		return ret;
2575 	}
2576 
2577 	/* Allocate and fill vbi video_device struct */
2578 	if (em28xx_vbi_supported(dev) == 1) {
2579 		dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2580 						"vbi");
2581 
2582 		/* register v4l2 vbi video_device */
2583 		ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2584 					    vbi_nr[dev->devno]);
2585 		if (ret < 0) {
2586 			em28xx_errdev("unable to register vbi device\n");
2587 			return ret;
2588 		}
2589 	}
2590 
2591 	if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2592 		dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2593 						  "radio");
2594 		if (!dev->radio_dev) {
2595 			em28xx_errdev("cannot allocate video_device.\n");
2596 			return -ENODEV;
2597 		}
2598 		ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2599 					    radio_nr[dev->devno]);
2600 		if (ret < 0) {
2601 			em28xx_errdev("can't register radio device\n");
2602 			return ret;
2603 		}
2604 		em28xx_info("Registered radio device as %s\n",
2605 			    video_device_node_name(dev->radio_dev));
2606 	}
2607 
2608 	em28xx_info("V4L2 video device registered as %s\n",
2609 		    video_device_node_name(dev->vdev));
2610 
2611 	if (dev->vbi_dev)
2612 		em28xx_info("V4L2 VBI device registered as %s\n",
2613 			    video_device_node_name(dev->vbi_dev));
2614 
2615 	return 0;
2616 }
2617