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