xref: /linux/drivers/media/usb/em28xx/em28xx-video.c (revision 8e8cd9a4c6dc006e9bd51b8ad0079fb4576b48d3)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
4 //		    video capture devices
5 //
6 // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
7 //		      Markus Rechberger <mrechberger@gmail.com>
8 //		      Mauro Carvalho Chehab <mchehab@kernel.org>
9 //		      Sascha Sommer <saschasommer@freenet.de>
10 // Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
11 //
12 //	Some parts based on SN9C10x PC Camera Controllers GPL driver made
13 //		by Luca Risolia <luca.risolia@studio.unibo.it>
14 
15 #include "em28xx.h"
16 
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/bitmap.h>
22 #include <linux/usb.h>
23 #include <linux/i2c.h>
24 #include <linux/mm.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
27 
28 #include "em28xx-v4l.h"
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-event.h>
32 #include <media/drv-intf/msp3400.h>
33 #include <media/tuner.h>
34 
35 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
36 		      "Markus Rechberger <mrechberger@gmail.com>, " \
37 		      "Mauro Carvalho Chehab <mchehab@kernel.org>, " \
38 		      "Sascha Sommer <saschasommer@freenet.de>"
39 
40 static unsigned int isoc_debug;
41 module_param(isoc_debug, int, 0644);
42 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
43 
44 static unsigned int disable_vbi;
45 module_param(disable_vbi, int, 0644);
46 MODULE_PARM_DESC(disable_vbi, "disable vbi support");
47 
48 static int alt;
49 module_param(alt, int, 0644);
50 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
51 
52 #define em28xx_videodbg(fmt, arg...) do {				\
53 	if (video_debug)						\
54 		dev_printk(KERN_DEBUG, &dev->intf->dev,			\
55 			   "video: %s: " fmt, __func__, ## arg);	\
56 } while (0)
57 
58 #define em28xx_isocdbg(fmt, arg...) do {\
59 	if (isoc_debug) \
60 		dev_printk(KERN_DEBUG, &dev->intf->dev,			\
61 			   "isoc: %s: " fmt, __func__, ## arg);		\
62 } while (0)
63 
64 MODULE_AUTHOR(DRIVER_AUTHOR);
65 MODULE_DESCRIPTION(DRIVER_DESC " - v4l2 interface");
66 MODULE_LICENSE("GPL v2");
67 MODULE_VERSION(EM28XX_VERSION);
68 
69 #define EM25XX_FRMDATAHDR_BYTE1			0x02
70 #define EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE	0x20
71 #define EM25XX_FRMDATAHDR_BYTE2_FRAME_END	0x02
72 #define EM25XX_FRMDATAHDR_BYTE2_FRAME_ID	0x01
73 #define EM25XX_FRMDATAHDR_BYTE2_MASK	(EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE | \
74 					 EM25XX_FRMDATAHDR_BYTE2_FRAME_END |   \
75 					 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID)
76 
77 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
78 static unsigned int vbi_nr[]   = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
79 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
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 		.fourcc   = V4L2_PIX_FMT_YUYV,
96 		.depth    = 16,
97 		.reg	  = EM28XX_OUTFMT_YUV422_Y0UY1V,
98 	}, {
99 		.fourcc   = V4L2_PIX_FMT_RGB565,
100 		.depth    = 16,
101 		.reg      = EM28XX_OUTFMT_RGB_16_656,
102 	}, {
103 		.fourcc   = V4L2_PIX_FMT_SRGGB8,
104 		.depth    = 8,
105 		.reg      = EM28XX_OUTFMT_RGB_8_RGRG,
106 	}, {
107 		.fourcc   = V4L2_PIX_FMT_SBGGR8,
108 		.depth    = 8,
109 		.reg      = EM28XX_OUTFMT_RGB_8_BGBG,
110 	}, {
111 		.fourcc   = V4L2_PIX_FMT_SGRBG8,
112 		.depth    = 8,
113 		.reg      = EM28XX_OUTFMT_RGB_8_GRGR,
114 	}, {
115 		.fourcc   = V4L2_PIX_FMT_SGBRG8,
116 		.depth    = 8,
117 		.reg      = EM28XX_OUTFMT_RGB_8_GBGB,
118 	}, {
119 		.fourcc   = V4L2_PIX_FMT_YUV411P,
120 		.depth    = 12,
121 		.reg      = EM28XX_OUTFMT_YUV411,
122 	},
123 };
124 
125 /*FIXME: maxw should be dependent of alt mode */
norm_maxw(struct em28xx * dev)126 static inline unsigned int norm_maxw(struct em28xx *dev)
127 {
128 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
129 
130 	if (dev->is_webcam)
131 		return v4l2->sensor_xres;
132 
133 	if (dev->board.max_range_640_480)
134 		return 640;
135 
136 	return 720;
137 }
138 
norm_maxh(struct em28xx * dev)139 static inline unsigned int norm_maxh(struct em28xx *dev)
140 {
141 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
142 
143 	if (dev->is_webcam)
144 		return v4l2->sensor_yres;
145 
146 	if (dev->board.max_range_640_480)
147 		return 480;
148 
149 	return (v4l2->norm & V4L2_STD_625_50) ? 576 : 480;
150 }
151 
em28xx_vbi_supported(struct em28xx * dev)152 static int em28xx_vbi_supported(struct em28xx *dev)
153 {
154 	/* Modprobe option to manually disable */
155 	if (disable_vbi == 1)
156 		return 0;
157 
158 	if (dev->is_webcam)
159 		return 0;
160 
161 	/* FIXME: check subdevices for VBI support */
162 
163 	if (dev->chip_id == CHIP_ID_EM2860 ||
164 	    dev->chip_id == CHIP_ID_EM2883 ||
165 	    dev->board.decoder == EM28XX_BUILTIN)
166 		return 1;
167 
168 	/* Version of em28xx that does not support VBI */
169 	return 0;
170 }
171 
em28xx_analogtv_supported(struct em28xx * dev)172 static int em28xx_analogtv_supported(struct em28xx *dev)
173 {
174 	switch (dev->model) {
175 	case EM2828X_BOARD_HAUPPAUGE_935_V2:
176 	case EM2828X_BOARD_HAUPPAUGE_955_V2:
177 	case EM2828X_BOARD_HAUPPAUGE_975_V2:
178 		return 1;
179 	default:
180 		return 0;
181 	};
182 }
183 
184 /*
185  * em28xx_wake_i2c()
186  * configure i2c attached devices
187  */
em28xx_wake_i2c(struct em28xx * dev)188 static void em28xx_wake_i2c(struct em28xx *dev)
189 {
190 	struct v4l2_device *v4l2_dev = &dev->v4l2->v4l2_dev;
191 
192 	v4l2_device_call_all(v4l2_dev, 0, core,  reset, 0);
193 	v4l2_device_call_all(v4l2_dev, 0, video, s_routing,
194 			     INPUT(dev->ctl_input)->vmux, 0, 0);
195 }
196 
em28xx_colorlevels_set_default(struct em28xx * dev)197 static int em28xx_colorlevels_set_default(struct em28xx *dev)
198 {
199 	em28xx_write_reg(dev, EM28XX_R20_YGAIN, CONTRAST_DEFAULT);
200 	em28xx_write_reg(dev, EM28XX_R21_YOFFSET, BRIGHTNESS_DEFAULT);
201 	em28xx_write_reg(dev, EM28XX_R22_UVGAIN, SATURATION_DEFAULT);
202 	em28xx_write_reg(dev, EM28XX_R23_UOFFSET, BLUE_BALANCE_DEFAULT);
203 	em28xx_write_reg(dev, EM28XX_R24_VOFFSET, RED_BALANCE_DEFAULT);
204 	em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, SHARPNESS_DEFAULT);
205 
206 	em28xx_write_reg(dev, EM28XX_R14_GAMMA, 0x20);
207 	em28xx_write_reg(dev, EM28XX_R15_RGAIN, 0x20);
208 	em28xx_write_reg(dev, EM28XX_R16_GGAIN, 0x20);
209 	em28xx_write_reg(dev, EM28XX_R17_BGAIN, 0x20);
210 	em28xx_write_reg(dev, EM28XX_R18_ROFFSET, 0x00);
211 	em28xx_write_reg(dev, EM28XX_R19_GOFFSET, 0x00);
212 	return em28xx_write_reg(dev, EM28XX_R1A_BOFFSET, 0x00);
213 }
214 
em28xx_set_outfmt(struct em28xx * dev)215 static int em28xx_set_outfmt(struct em28xx *dev)
216 {
217 	int ret;
218 	u8 fmt, vinctrl;
219 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
220 
221 	fmt = v4l2->format->reg;
222 	if (!dev->is_em25xx)
223 		fmt |= 0x20;
224 	/*
225 	 * NOTE: it's not clear if this is really needed !
226 	 * The datasheets say bit 5 is a reserved bit and devices seem to work
227 	 * fine without it. But the Windows driver sets it for em2710/50+em28xx
228 	 * devices and we've always been setting it, too.
229 	 *
230 	 * em2765 (em25xx, em276x/7x/8x) devices do NOT work with this bit set,
231 	 * it's likely used for an additional (compressed ?) format there.
232 	 */
233 	ret = em28xx_write_reg(dev, EM28XX_R27_OUTFMT, fmt);
234 	if (ret < 0)
235 		return ret;
236 
237 	ret = em28xx_write_reg(dev, EM28XX_R10_VINMODE, v4l2->vinmode);
238 	if (ret < 0)
239 		return ret;
240 
241 	vinctrl = v4l2->vinctl;
242 	if (em28xx_vbi_supported(dev) == 1) {
243 		vinctrl |= EM28XX_VINCTRL_VBI_RAW;
244 		em28xx_write_reg(dev, EM28XX_R34_VBI_START_H, 0x00);
245 		em28xx_write_reg(dev, EM28XX_R36_VBI_WIDTH,
246 				 v4l2->vbi_width / 4);
247 		em28xx_write_reg(dev, EM28XX_R37_VBI_HEIGHT, v4l2->vbi_height);
248 		if (v4l2->norm & V4L2_STD_525_60) {
249 			/* NTSC */
250 			em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x09);
251 		} else if (v4l2->norm & V4L2_STD_625_50) {
252 			/* PAL */
253 			em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x07);
254 		}
255 	}
256 
257 	return em28xx_write_reg(dev, EM28XX_R11_VINCTRL, vinctrl);
258 }
259 
em28xx_accumulator_set(struct em28xx * dev,u8 xmin,u8 xmax,u8 ymin,u8 ymax)260 static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
261 				  u8 ymin, u8 ymax)
262 {
263 	em28xx_videodbg("em28xx Scale: (%d,%d)-(%d,%d)\n",
264 			xmin, ymin, xmax, ymax);
265 
266 	em28xx_write_regs(dev, EM28XX_R28_XMIN, &xmin, 1);
267 	em28xx_write_regs(dev, EM28XX_R29_XMAX, &xmax, 1);
268 	em28xx_write_regs(dev, EM28XX_R2A_YMIN, &ymin, 1);
269 	return em28xx_write_regs(dev, EM28XX_R2B_YMAX, &ymax, 1);
270 }
271 
em28xx_capture_area_set(struct em28xx * dev,u8 hstart,u8 vstart,u16 width,u16 height)272 static void em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
273 				    u16 width, u16 height)
274 {
275 	u8 cwidth = width >> 2;
276 	u8 cheight = height >> 2;
277 	u8 overflow = (height >> 9 & 0x02) | (width >> 10 & 0x01);
278 	/* NOTE: size limit: 2047x1023 = 2MPix */
279 
280 	em28xx_videodbg("capture area set to (%u,%u)/%ux%u\n",
281 			hstart, vstart,
282 		       ((overflow & 2) << 9 | cwidth << 2),
283 		       ((overflow & 1) << 10 | cheight << 2));
284 
285 	em28xx_write_regs(dev, EM28XX_R1C_HSTART, &hstart, 1);
286 	em28xx_write_regs(dev, EM28XX_R1D_VSTART, &vstart, 1);
287 	em28xx_write_regs(dev, EM28XX_R1E_CWIDTH, &cwidth, 1);
288 	em28xx_write_regs(dev, EM28XX_R1F_CHEIGHT, &cheight, 1);
289 	em28xx_write_regs(dev, EM28XX_R1B_OFLOW, &overflow, 1);
290 
291 	/* FIXME: function/meaning of these registers ? */
292 	/* FIXME: align width+height to multiples of 4 ?! */
293 	if (dev->is_em25xx) {
294 		em28xx_write_reg(dev, 0x34, width >> 4);
295 		em28xx_write_reg(dev, 0x35, height >> 4);
296 	}
297 }
298 
em28xx_scaler_set(struct em28xx * dev,u16 h,u16 v)299 static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
300 {
301 	u8 mode = 0x00;
302 	/* the em2800 scaler only supports scaling down to 50% */
303 
304 	if (dev->board.is_em2800) {
305 		mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
306 	} else {
307 		u8 buf[2];
308 
309 		buf[0] = h;
310 		buf[1] = h >> 8;
311 		em28xx_write_regs(dev, EM28XX_R30_HSCALELOW, (char *)buf, 2);
312 
313 		buf[0] = v;
314 		buf[1] = v >> 8;
315 		em28xx_write_regs(dev, EM28XX_R32_VSCALELOW, (char *)buf, 2);
316 		/*
317 		 * it seems that both H and V scalers must be active
318 		 * to work correctly
319 		 */
320 		mode = (h || v) ? 0x30 : 0x00;
321 	}
322 	return em28xx_write_reg(dev, EM28XX_R26_COMPR, mode);
323 }
324 
325 /* FIXME: this only function read values from dev */
em28xx_resolution_set(struct em28xx * dev)326 static int em28xx_resolution_set(struct em28xx *dev)
327 {
328 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
329 	int width = norm_maxw(dev);
330 	int height = norm_maxh(dev);
331 
332 	/* Properly setup VBI */
333 	v4l2->vbi_width = 720;
334 	if (v4l2->norm & V4L2_STD_525_60)
335 		v4l2->vbi_height = 12;
336 	else
337 		v4l2->vbi_height = 18;
338 
339 	em28xx_set_outfmt(dev);
340 
341 	em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
342 
343 	/*
344 	 * If we don't set the start position to 2 in VBI mode, we end up
345 	 * with line 20/21 being YUYV encoded instead of being in 8-bit
346 	 * greyscale.  The core of the issue is that line 21 (and line 23 for
347 	 * PAL WSS) are inside of active video region, and as a result they
348 	 * get the pixelformatting associated with that area.  So by cropping
349 	 * it out, we end up with the same format as the rest of the VBI
350 	 * region
351 	 */
352 	if (em28xx_vbi_supported(dev) == 1)
353 		em28xx_capture_area_set(dev, 0, 2, width, height);
354 	else
355 		em28xx_capture_area_set(dev, 0, 0, width, height);
356 
357 	return em28xx_scaler_set(dev, v4l2->hscale, v4l2->vscale);
358 }
359 
em2828X_decoder_set_std(struct em28xx * dev,v4l2_std_id norm)360 static void em2828X_decoder_set_std(struct em28xx *dev, v4l2_std_id norm)
361 {
362 	if (norm & V4L2_STD_525_60) {
363 		dev_dbg(&dev->intf->dev, "V4L2_STD_525_60");
364 		em28xx_write_reg(dev, 0x7A01, 0x0d);	// 0x05
365 		em28xx_write_reg(dev, 0x7A04, 0xDD);
366 		em28xx_write_reg(dev, 0x7A07, 0x60);
367 		em28xx_write_reg(dev, 0x7A08, 0x7A);
368 		em28xx_write_reg(dev, 0x7A09, 0x02);
369 		em28xx_write_reg(dev, 0x7A0A, 0x7C);
370 		em28xx_write_reg(dev, 0x7A0C, 0x8A);
371 		em28xx_write_reg(dev, 0x7A0F, 0x1C);
372 		em28xx_write_reg(dev, 0x7A18, 0x20);
373 		em28xx_write_reg(dev, 0x7A19, 0x74);
374 		em28xx_write_reg(dev, 0x7A1A, 0x5D);
375 		em28xx_write_reg(dev, 0x7A1B, 0x17);
376 		em28xx_write_reg(dev, 0x7A2E, 0x85);
377 		em28xx_write_reg(dev, 0x7A31, 0x63);
378 		em28xx_write_reg(dev, 0x7A82, 0x42);
379 		em28xx_write_reg(dev, 0x7AC0, 0xD4);
380 
381 		if (INPUT(dev->ctl_input)->vmux == EM2828X_COMPOSITE) {
382 			em28xx_write_reg(dev, 0x7A00, 0x00);
383 			em28xx_write_reg(dev, 0x7A03, 0x00);
384 			em28xx_write_reg(dev, 0x7A30, 0x22);
385 			em28xx_write_reg(dev, 0x7A80, 0x03);
386 		} else if (INPUT(dev->ctl_input)->vmux == EM2828X_TELEVISION) {
387 			em28xx_write_reg(dev, 0x7A17, 0xc3);
388 			em28xx_write_reg(dev, 0x7A31, 0x62);	// BRL 0x63
389 			em28xx_write_reg(dev, 0x7A82, 0x42);
390 			em28xx_write_reg(dev, 0x7AC0, 0xD4);
391 			em28xx_write_reg(dev, 0x7A00, 0x00);
392 			em28xx_write_reg(dev, 0x7A03, 0x00);
393 			em28xx_write_reg(dev, 0x7A30, 0x20);
394 			em28xx_write_reg(dev, 0x7A80, 0x00);
395 
396 			em28xx_write_reg(dev, 0x7A50, 0xdd);
397 			em28xx_write_reg(dev, 0x7A5d, 0x0e);
398 			em28xx_write_reg(dev, 0x7A5e, 0xea);
399 			em28xx_write_reg(dev, 0x7A60, 0x64);
400 			em28xx_write_reg(dev, 0x7A67, 0x5a);
401 		} else {
402 			em28xx_write_reg(dev, 0x7A00, 0x01);
403 			em28xx_write_reg(dev, 0x7A03, 0x03);
404 			em28xx_write_reg(dev, 0x7A30, 0x20);
405 			em28xx_write_reg(dev, 0x7A80, 0x04);
406 		}
407 	} else if (norm & V4L2_STD_625_50) {
408 		dev_dbg(&dev->intf->dev, "V4L2_STD_625_50");
409 		em28xx_write_reg(dev, 0x7A04, 0xDC);
410 		em28xx_write_reg(dev, 0x7A0C, 0x67);
411 		em28xx_write_reg(dev, 0x7A0F, 0x1C);
412 		em28xx_write_reg(dev, 0x7A18, 0x28);
413 		em28xx_write_reg(dev, 0x7A19, 0x32);
414 		em28xx_write_reg(dev, 0x7A1A, 0xB9);
415 		em28xx_write_reg(dev, 0x7A1B, 0x86);
416 		em28xx_write_reg(dev, 0x7A31, 0xC3);
417 		em28xx_write_reg(dev, 0x7A82, 0x52);
418 
419 		if (INPUT(dev->ctl_input)->vmux == EM2828X_COMPOSITE) {
420 			em28xx_write_reg(dev, 0x7A00, 0x32);
421 			em28xx_write_reg(dev, 0x7A01, 0x10);
422 			em28xx_write_reg(dev, 0x7A03, 0x06);
423 			em28xx_write_reg(dev, 0x7A07, 0x2f);
424 			em28xx_write_reg(dev, 0x7A08, 0x77);
425 			em28xx_write_reg(dev, 0x7A09, 0x0f);
426 			em28xx_write_reg(dev, 0x7A0A, 0x8c);
427 			em28xx_write_reg(dev, 0x7A20, 0x3d);
428 			em28xx_write_reg(dev, 0x7A2E, 0x88);
429 			em28xx_write_reg(dev, 0x7A30, 0x2c);
430 			em28xx_write_reg(dev, 0x7A80, 0x07);
431 		} else if (INPUT(dev->ctl_input)->vmux == EM2828X_TELEVISION) {
432 			em28xx_write_reg(dev, 0x7A00, 0x32);
433 			em28xx_write_reg(dev, 0x7A03, 0x09);
434 			em28xx_write_reg(dev, 0x7A30, 0x2a);
435 			em28xx_write_reg(dev, 0x7A80, 0x03);
436 			em28xx_write_reg(dev, 0x7A20, 0x35);
437 			em28xx_write_reg(dev, 0x7A2e, 0x88);
438 			em28xx_write_reg(dev, 0x7A53, 0xcc);
439 			em28xx_write_reg(dev, 0x7A5d, 0x16);
440 			em28xx_write_reg(dev, 0x7A5e, 0x50);
441 			em28xx_write_reg(dev, 0x7A60, 0xb4);
442 			em28xx_write_reg(dev, 0x7A67, 0x64);
443 		} else {
444 			em28xx_write_reg(dev, 0x7A00, 0x33);
445 			em28xx_write_reg(dev, 0x7A01, 0x04);
446 			em28xx_write_reg(dev, 0x7A03, 0x04);
447 			em28xx_write_reg(dev, 0x7A07, 0x20);
448 			em28xx_write_reg(dev, 0x7A08, 0x6a);
449 			em28xx_write_reg(dev, 0x7A09, 0x16);
450 			em28xx_write_reg(dev, 0x7A0A, 0x80);
451 			em28xx_write_reg(dev, 0x7A2E, 0x8a);
452 			em28xx_write_reg(dev, 0x7A30, 0x26);
453 			em28xx_write_reg(dev, 0x7A80, 0x08);
454 		}
455 	} else {
456 		dev_err(&dev->intf->dev, "%s() Unsupported STD: %X", __func__, (unsigned int)norm);
457 	}
458 
459 	em28xx_write_reg(dev, 0x7A3F, 0x01);
460 	em28xx_write_reg(dev, 0x7A3F, 0x00);
461 }
462 
463 /* Set USB alternate setting for analog video */
em28xx_set_alternate(struct em28xx * dev)464 static int em28xx_set_alternate(struct em28xx *dev)
465 {
466 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
467 	struct usb_device *udev = interface_to_usbdev(dev->intf);
468 	int err;
469 	int i;
470 	unsigned int min_pkt_size = v4l2->width * 2 + 4;
471 
472 	/*
473 	 * NOTE: for isoc transfers, only alt settings > 0 are allowed
474 	 * bulk transfers seem to work only with alt=0 !
475 	 */
476 	dev->alt = 0;
477 	if (alt > 0 && alt < dev->num_alt) {
478 		em28xx_videodbg("alternate forced to %d\n", dev->alt);
479 		dev->alt = alt;
480 		goto set_alt;
481 	}
482 	if (dev->analog_xfer_bulk)
483 		goto set_alt;
484 
485 	/*
486 	 * When image size is bigger than a certain value,
487 	 * the frame size should be increased, otherwise, only
488 	 * green screen will be received.
489 	 */
490 	if (v4l2->width * 2 * v4l2->height > 720 * 240 * 2)
491 		min_pkt_size *= 2;
492 
493 	for (i = 0; i < dev->num_alt; i++) {
494 		/* stop when the selected alt setting offers enough bandwidth */
495 		if (dev->alt_max_pkt_size_isoc[i] >= min_pkt_size) {
496 			dev->alt = i;
497 			break;
498 		/*
499 		 * otherwise make sure that we end up with the maximum
500 		 * bandwidth because the min_pkt_size equation might be wrong.
501 		 *
502 		 */
503 		} else if (dev->alt_max_pkt_size_isoc[i] >
504 			   dev->alt_max_pkt_size_isoc[dev->alt])
505 			dev->alt = i;
506 	}
507 
508 set_alt:
509 	/*
510 	 * NOTE: for bulk transfers, we need to call usb_set_interface()
511 	 * even if the previous settings were the same. Otherwise streaming
512 	 * fails with all urbs having status = -EOVERFLOW !
513 	 */
514 	if (dev->analog_xfer_bulk) {
515 		dev->max_pkt_size = 512; /* USB 2.0 spec */
516 		dev->packet_multiplier = EM28XX_BULK_PACKET_MULTIPLIER;
517 	} else { /* isoc */
518 		em28xx_videodbg("minimum isoc packet size: %u (alt=%d)\n",
519 				min_pkt_size, dev->alt);
520 		dev->max_pkt_size =
521 				  dev->alt_max_pkt_size_isoc[dev->alt];
522 		dev->packet_multiplier = EM28XX_NUM_ISOC_PACKETS;
523 	}
524 	em28xx_videodbg("setting alternate %d with wMaxPacketSize=%u\n",
525 			dev->alt, dev->max_pkt_size);
526 	err = usb_set_interface(udev, dev->ifnum, dev->alt);
527 	if (err < 0) {
528 		dev_err(&dev->intf->dev,
529 			"cannot change alternate number to %d (error=%i)\n",
530 			dev->alt, err);
531 		return err;
532 	}
533 	return 0;
534 }
535 
536 /*
537  * DMA and thread functions
538  */
539 
540 /*
541  * Finish the current buffer
542  */
finish_buffer(struct em28xx * dev,struct em28xx_buffer * buf)543 static inline void finish_buffer(struct em28xx *dev,
544 				 struct em28xx_buffer *buf)
545 {
546 	em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
547 
548 	buf->vb.sequence = dev->v4l2->field_count++;
549 	if (dev->v4l2->progressive)
550 		buf->vb.field = V4L2_FIELD_NONE;
551 	else
552 		buf->vb.field = V4L2_FIELD_INTERLACED;
553 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
554 
555 	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
556 }
557 
558 /*
559  * Copy picture data from USB buffer to video buffer
560  */
em28xx_copy_video(struct em28xx * dev,struct em28xx_buffer * buf,unsigned char * usb_buf,unsigned long len)561 static void em28xx_copy_video(struct em28xx *dev,
562 			      struct em28xx_buffer *buf,
563 			      unsigned char *usb_buf,
564 			      unsigned long len)
565 {
566 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
567 	void *fieldstart, *startwrite, *startread;
568 	int  linesdone, currlinedone, offset, lencopy, remain;
569 	int bytesperline = v4l2->width << 1;
570 
571 	if (buf->pos + len > buf->length)
572 		len = buf->length - buf->pos;
573 
574 	startread = usb_buf;
575 	remain = len;
576 
577 	if (v4l2->progressive || buf->top_field)
578 		fieldstart = buf->vb_buf;
579 	else /* interlaced mode, even nr. of lines */
580 		fieldstart = buf->vb_buf + bytesperline;
581 
582 	linesdone = buf->pos / bytesperline;
583 	currlinedone = buf->pos % bytesperline;
584 
585 	if (v4l2->progressive)
586 		offset = linesdone * bytesperline + currlinedone;
587 	else
588 		offset = linesdone * bytesperline * 2 + currlinedone;
589 
590 	startwrite = fieldstart + offset;
591 	lencopy = bytesperline - currlinedone;
592 	lencopy = lencopy > remain ? remain : lencopy;
593 
594 	if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
595 		em28xx_isocdbg("Overflow of %zu bytes past buffer end (1)\n",
596 			       ((char *)startwrite + lencopy) -
597 			      ((char *)buf->vb_buf + buf->length));
598 		remain = (char *)buf->vb_buf + buf->length -
599 			 (char *)startwrite;
600 		lencopy = remain;
601 	}
602 	if (lencopy <= 0)
603 		return;
604 	memcpy(startwrite, startread, lencopy);
605 
606 	remain -= lencopy;
607 
608 	while (remain > 0) {
609 		if (v4l2->progressive)
610 			startwrite += lencopy;
611 		else
612 			startwrite += lencopy + bytesperline;
613 		startread += lencopy;
614 		if (bytesperline > remain)
615 			lencopy = remain;
616 		else
617 			lencopy = bytesperline;
618 
619 		if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
620 		    buf->length) {
621 			em28xx_isocdbg("Overflow of %zu bytes past buffer end(2)\n",
622 				       ((char *)startwrite + lencopy) -
623 				       ((char *)buf->vb_buf + buf->length));
624 			remain = (char *)buf->vb_buf + buf->length -
625 				 (char *)startwrite;
626 			lencopy = remain;
627 		}
628 		if (lencopy <= 0)
629 			break;
630 
631 		memcpy(startwrite, startread, lencopy);
632 
633 		remain -= lencopy;
634 	}
635 
636 	buf->pos += len;
637 }
638 
639 /*
640  * Copy VBI data from USB buffer to video buffer
641  */
em28xx_copy_vbi(struct em28xx * dev,struct em28xx_buffer * buf,unsigned char * usb_buf,unsigned long len)642 static void em28xx_copy_vbi(struct em28xx *dev,
643 			    struct em28xx_buffer *buf,
644 			    unsigned char *usb_buf,
645 			    unsigned long len)
646 {
647 	unsigned int offset;
648 
649 	if (buf->pos + len > buf->length)
650 		len = buf->length - buf->pos;
651 
652 	offset = buf->pos;
653 	/* Make sure the bottom field populates the second half of the frame */
654 	if (buf->top_field == 0)
655 		offset += dev->v4l2->vbi_width * dev->v4l2->vbi_height;
656 
657 	memcpy(buf->vb_buf + offset, usb_buf, len);
658 	buf->pos += len;
659 }
660 
print_err_status(struct em28xx * dev,int packet,int status)661 static inline void print_err_status(struct em28xx *dev,
662 				    int packet, int status)
663 {
664 	char *errmsg = "Unknown";
665 
666 	switch (status) {
667 	case -ENOENT:
668 		errmsg = "unlinked synchronously";
669 		break;
670 	case -ECONNRESET:
671 		errmsg = "unlinked asynchronously";
672 		break;
673 	case -ENOSR:
674 		errmsg = "Buffer error (overrun)";
675 		break;
676 	case -EPIPE:
677 		errmsg = "Stalled (device not responding)";
678 		break;
679 	case -EOVERFLOW:
680 		errmsg = "Babble (bad cable?)";
681 		break;
682 	case -EPROTO:
683 		errmsg = "Bit-stuff error (bad cable?)";
684 		break;
685 	case -EILSEQ:
686 		errmsg = "CRC/Timeout (could be anything)";
687 		break;
688 	case -ETIME:
689 		errmsg = "Device does not respond";
690 		break;
691 	}
692 	if (packet < 0) {
693 		em28xx_isocdbg("URB status %d [%s].\n",	status, errmsg);
694 	} else {
695 		em28xx_isocdbg("URB packet %d, status %d [%s].\n",
696 			       packet, status, errmsg);
697 	}
698 }
699 
700 /*
701  * get the next available buffer from dma queue
702  */
get_next_buf(struct em28xx * dev,struct em28xx_dmaqueue * dma_q)703 static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
704 						 struct em28xx_dmaqueue *dma_q)
705 {
706 	struct em28xx_buffer *buf;
707 
708 	if (list_empty(&dma_q->active)) {
709 		em28xx_isocdbg("No active queue to serve\n");
710 		return NULL;
711 	}
712 
713 	/* Get the next buffer */
714 	buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
715 	/* Cleans up buffer - Useful for testing for frame/URB loss */
716 	list_del(&buf->list);
717 	buf->pos = 0;
718 	buf->vb_buf = buf->mem;
719 
720 	return buf;
721 }
722 
723 /*
724  * Finish the current buffer if completed and prepare for the next field
725  */
726 static struct em28xx_buffer *
finish_field_prepare_next(struct em28xx * dev,struct em28xx_buffer * buf,struct em28xx_dmaqueue * dma_q)727 finish_field_prepare_next(struct em28xx *dev,
728 			  struct em28xx_buffer *buf,
729 			  struct em28xx_dmaqueue *dma_q)
730 {
731 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
732 
733 	if (v4l2->progressive || v4l2->top_field) { /* Brand new frame */
734 		if (buf)
735 			finish_buffer(dev, buf);
736 		buf = get_next_buf(dev, dma_q);
737 	}
738 	if (buf) {
739 		buf->top_field = v4l2->top_field;
740 		buf->pos = 0;
741 	}
742 
743 	return buf;
744 }
745 
746 /*
747  * Process data packet according to the em2710/em2750/em28xx frame data format
748  */
process_frame_data_em28xx(struct em28xx * dev,unsigned char * data_pkt,unsigned int data_len)749 static inline void process_frame_data_em28xx(struct em28xx *dev,
750 					     unsigned char *data_pkt,
751 					     unsigned int  data_len)
752 {
753 	struct em28xx_v4l2      *v4l2 = dev->v4l2;
754 	struct em28xx_buffer    *buf = dev->usb_ctl.vid_buf;
755 	struct em28xx_buffer    *vbi_buf = dev->usb_ctl.vbi_buf;
756 	struct em28xx_dmaqueue  *dma_q = &dev->vidq;
757 	struct em28xx_dmaqueue  *vbi_dma_q = &dev->vbiq;
758 
759 	/*
760 	 * capture type 0 = vbi start
761 	 * capture type 1 = vbi in progress
762 	 * capture type 2 = video start
763 	 * capture type 3 = video in progress
764 	 */
765 	if (data_len >= 4) {
766 		/*
767 		 * NOTE: Headers are always 4 bytes and
768 		 * never split across packets
769 		 */
770 		if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
771 		    data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
772 			/* Continuation */
773 			data_pkt += 4;
774 			data_len -= 4;
775 		} else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
776 			/* Field start (VBI mode) */
777 			v4l2->capture_type = 0;
778 			v4l2->vbi_read = 0;
779 			em28xx_isocdbg("VBI START HEADER !!!\n");
780 			v4l2->top_field = !(data_pkt[2] & 1);
781 			data_pkt += 4;
782 			data_len -= 4;
783 		} else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
784 			/* Field start (VBI disabled) */
785 			v4l2->capture_type = 2;
786 			em28xx_isocdbg("VIDEO START HEADER !!!\n");
787 			v4l2->top_field = !(data_pkt[2] & 1);
788 			data_pkt += 4;
789 			data_len -= 4;
790 		}
791 	}
792 	/*
793 	 * NOTE: With bulk transfers, intermediate data packets
794 	 * have no continuation header
795 	 */
796 
797 	if (v4l2->capture_type == 0) {
798 		vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
799 		dev->usb_ctl.vbi_buf = vbi_buf;
800 		v4l2->capture_type = 1;
801 	}
802 
803 	if (v4l2->capture_type == 1) {
804 		int vbi_size = v4l2->vbi_width * v4l2->vbi_height;
805 		int vbi_data_len = ((v4l2->vbi_read + data_len) > vbi_size) ?
806 				   (vbi_size - v4l2->vbi_read) : data_len;
807 
808 		/* Copy VBI data */
809 		if (vbi_buf)
810 			em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
811 		v4l2->vbi_read += vbi_data_len;
812 
813 		if (vbi_data_len < data_len) {
814 			/* Continue with copying video data */
815 			v4l2->capture_type = 2;
816 			data_pkt += vbi_data_len;
817 			data_len -= vbi_data_len;
818 		}
819 	}
820 
821 	if (v4l2->capture_type == 2) {
822 		buf = finish_field_prepare_next(dev, buf, dma_q);
823 		dev->usb_ctl.vid_buf = buf;
824 		v4l2->capture_type = 3;
825 	}
826 
827 	if (v4l2->capture_type == 3 && buf && data_len > 0)
828 		em28xx_copy_video(dev, buf, data_pkt, data_len);
829 }
830 
831 /*
832  * Process data packet according to the em25xx/em276x/7x/8x frame data format
833  */
process_frame_data_em25xx(struct em28xx * dev,unsigned char * data_pkt,unsigned int data_len)834 static inline void process_frame_data_em25xx(struct em28xx *dev,
835 					     unsigned char *data_pkt,
836 					     unsigned int  data_len)
837 {
838 	struct em28xx_buffer    *buf = dev->usb_ctl.vid_buf;
839 	struct em28xx_dmaqueue  *dmaq = &dev->vidq;
840 	struct em28xx_v4l2      *v4l2 = dev->v4l2;
841 	bool frame_end = false;
842 
843 	/* Check for header */
844 	/*
845 	 * NOTE: at least with bulk transfers, only the first packet
846 	 * has a header and has always set the FRAME_END bit
847 	 */
848 	if (data_len >= 2) {	/* em25xx header is only 2 bytes long */
849 		if ((data_pkt[0] == EM25XX_FRMDATAHDR_BYTE1) &&
850 		    ((data_pkt[1] & ~EM25XX_FRMDATAHDR_BYTE2_MASK) == 0x00)) {
851 			v4l2->top_field = !(data_pkt[1] &
852 					   EM25XX_FRMDATAHDR_BYTE2_FRAME_ID);
853 			frame_end = data_pkt[1] &
854 				    EM25XX_FRMDATAHDR_BYTE2_FRAME_END;
855 			data_pkt += 2;
856 			data_len -= 2;
857 		}
858 
859 		/* Finish field and prepare next (BULK only) */
860 		if (dev->analog_xfer_bulk && frame_end) {
861 			buf = finish_field_prepare_next(dev, buf, dmaq);
862 			dev->usb_ctl.vid_buf = buf;
863 		}
864 		/*
865 		 * NOTE: in ISOC mode when a new frame starts and buf==NULL,
866 		 * we COULD already prepare a buffer here to avoid skipping the
867 		 * first frame.
868 		 */
869 	}
870 
871 	/* Copy data */
872 	if (buf && data_len > 0)
873 		em28xx_copy_video(dev, buf, data_pkt, data_len);
874 
875 	/* Finish frame (ISOC only) => avoids lag of 1 frame */
876 	if (!dev->analog_xfer_bulk && frame_end) {
877 		buf = finish_field_prepare_next(dev, buf, dmaq);
878 		dev->usb_ctl.vid_buf = buf;
879 	}
880 
881 	/*
882 	 * NOTES:
883 	 *
884 	 * 1) Tested with USB bulk transfers only !
885 	 * The wording in the datasheet suggests that isoc might work different.
886 	 * The current code assumes that with isoc transfers each packet has a
887 	 * header like with the other em28xx devices.
888 	 *
889 	 * 2) Support for interlaced mode is pure theory. It has not been
890 	 * tested and it is unknown if these devices actually support it.
891 	 */
892 }
893 
894 /* Processes and copies the URB data content (video and VBI data) */
em28xx_urb_data_copy(struct em28xx * dev,struct urb * urb)895 static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
896 {
897 	int xfer_bulk, num_packets, i;
898 	unsigned char *usb_data_pkt;
899 	unsigned int usb_data_len;
900 
901 	if (!dev)
902 		return 0;
903 
904 	if (dev->disconnected)
905 		return 0;
906 
907 	if (urb->status < 0)
908 		print_err_status(dev, -1, urb->status);
909 
910 	xfer_bulk = usb_pipebulk(urb->pipe);
911 
912 	if (xfer_bulk) /* bulk */
913 		num_packets = 1;
914 	else /* isoc */
915 		num_packets = urb->number_of_packets;
916 
917 	for (i = 0; i < num_packets; i++) {
918 		if (xfer_bulk) { /* bulk */
919 			usb_data_len = urb->actual_length;
920 
921 			usb_data_pkt = urb->transfer_buffer;
922 		} else { /* isoc */
923 			if (urb->iso_frame_desc[i].status < 0) {
924 				print_err_status(dev, i,
925 						 urb->iso_frame_desc[i].status);
926 				if (urb->iso_frame_desc[i].status != -EPROTO)
927 					continue;
928 			}
929 
930 			usb_data_len = urb->iso_frame_desc[i].actual_length;
931 			if (usb_data_len > dev->max_pkt_size) {
932 				em28xx_isocdbg("packet bigger than packet size");
933 				continue;
934 			}
935 
936 			usb_data_pkt = urb->transfer_buffer +
937 				       urb->iso_frame_desc[i].offset;
938 		}
939 
940 		if (usb_data_len == 0) {
941 			/* NOTE: happens very often with isoc transfers */
942 			/* em28xx_usbdbg("packet %d is empty",i); - spammy */
943 			continue;
944 		}
945 
946 		if (dev->is_em25xx)
947 			process_frame_data_em25xx(dev,
948 						  usb_data_pkt, usb_data_len);
949 		else
950 			process_frame_data_em28xx(dev,
951 						  usb_data_pkt, usb_data_len);
952 	}
953 	return 1;
954 }
955 
get_resource(enum v4l2_buf_type f_type)956 static int get_resource(enum v4l2_buf_type f_type)
957 {
958 	switch (f_type) {
959 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
960 		return EM28XX_RESOURCE_VIDEO;
961 	case V4L2_BUF_TYPE_VBI_CAPTURE:
962 		return EM28XX_RESOURCE_VBI;
963 	default:
964 		WARN_ON(1);
965 		return -1; /* Indicate that device is busy */
966 	}
967 }
968 
969 /* Usage lock check functions */
res_get(struct em28xx * dev,enum v4l2_buf_type f_type)970 static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
971 {
972 	int res_type = get_resource(f_type);
973 
974 	/* is it free? */
975 	if (dev->resources & res_type) {
976 		/* no, someone else uses it */
977 		return -EBUSY;
978 	}
979 
980 	/* it's free, grab it */
981 	dev->resources |= res_type;
982 	em28xx_videodbg("res: get %d\n", res_type);
983 	return 0;
984 }
985 
res_free(struct em28xx * dev,enum v4l2_buf_type f_type)986 static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
987 {
988 	int res_type = get_resource(f_type);
989 
990 	dev->resources &= ~res_type;
991 	em28xx_videodbg("res: put %d\n", res_type);
992 }
993 
em28xx_v4l2_media_release(struct em28xx * dev)994 static void em28xx_v4l2_media_release(struct em28xx *dev)
995 {
996 #ifdef CONFIG_MEDIA_CONTROLLER
997 	int i;
998 
999 	if (dev->board.decoder == EM28XX_BUILTIN) {
1000 		media_device_unregister_entity(dev->v4l2->decoder);
1001 		kfree(dev->v4l2->decoder);
1002 		dev->v4l2->decoder = NULL;
1003 	}
1004 
1005 	for (i = 0; i < MAX_EM28XX_INPUT; i++) {
1006 		if (!INPUT(i)->type)
1007 			return;
1008 		media_device_unregister_entity(&dev->input_ent[i]);
1009 	}
1010 #endif
1011 }
1012 
1013 /*
1014  * Media Controller helper functions
1015  */
1016 
em28xx_enable_analog_tuner(struct em28xx * dev)1017 static int em28xx_enable_analog_tuner(struct em28xx *dev)
1018 {
1019 #ifdef CONFIG_MEDIA_CONTROLLER
1020 	struct media_device *mdev = dev->media_dev;
1021 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1022 	struct media_entity *source;
1023 	struct media_link *link, *found_link = NULL;
1024 	int ret, active_links = 0;
1025 
1026 	if (!mdev || !v4l2->decoder)
1027 		return 0;
1028 
1029 	/*
1030 	 * This will find the tuner that is connected into the decoder.
1031 	 * Technically, this is not 100% correct, as the device may be
1032 	 * using an analog input instead of the tuner. However, as we can't
1033 	 * do DVB streaming while the DMA engine is being used for V4L2,
1034 	 * this should be enough for the actual needs.
1035 	 */
1036 	list_for_each_entry(link, &v4l2->decoder->links, list) {
1037 		if (link->sink->entity == v4l2->decoder) {
1038 			found_link = link;
1039 			if (link->flags & MEDIA_LNK_FL_ENABLED)
1040 				active_links++;
1041 			break;
1042 		}
1043 	}
1044 
1045 	if (active_links == 1 || !found_link)
1046 		return 0;
1047 
1048 	source = found_link->source->entity;
1049 	list_for_each_entry(link, &source->links, list) {
1050 		struct media_entity *sink;
1051 		int flags = 0;
1052 
1053 		sink = link->sink->entity;
1054 
1055 		if (sink == v4l2->decoder)
1056 			flags = MEDIA_LNK_FL_ENABLED;
1057 
1058 		ret = media_entity_setup_link(link, flags);
1059 		if (ret) {
1060 			dev_err(&dev->intf->dev,
1061 				"Couldn't change link %s->%s to %s. Error %d\n",
1062 				source->name, sink->name,
1063 				flags ? "enabled" : "disabled",
1064 				ret);
1065 			return ret;
1066 		}
1067 
1068 		em28xx_videodbg("link %s->%s was %s\n",
1069 				source->name, sink->name,
1070 				flags ? "ENABLED" : "disabled");
1071 	}
1072 #endif
1073 	return 0;
1074 }
1075 
1076 static const char * const iname[] = {
1077 	[EM28XX_VMUX_COMPOSITE]  = "Composite",
1078 	[EM28XX_VMUX_SVIDEO]     = "S-Video",
1079 	[EM28XX_VMUX_TELEVISION] = "Television",
1080 	[EM28XX_RADIO]           = "Radio",
1081 };
1082 
em28xx_v4l2_create_entities(struct em28xx * dev)1083 static void em28xx_v4l2_create_entities(struct em28xx *dev)
1084 {
1085 #if defined(CONFIG_MEDIA_CONTROLLER)
1086 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1087 	int ret, i;
1088 
1089 	/* Initialize Video, VBI and Radio pads */
1090 	v4l2->video_pad.flags = MEDIA_PAD_FL_SINK;
1091 	ret = media_entity_pads_init(&v4l2->vdev.entity, 1, &v4l2->video_pad);
1092 	if (ret < 0)
1093 		dev_err(&dev->intf->dev,
1094 			"failed to initialize video media entity!\n");
1095 
1096 	if (em28xx_vbi_supported(dev)) {
1097 		v4l2->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1098 		ret = media_entity_pads_init(&v4l2->vbi_dev.entity, 1,
1099 					     &v4l2->vbi_pad);
1100 		if (ret < 0)
1101 			dev_err(&dev->intf->dev,
1102 				"failed to initialize vbi media entity!\n");
1103 	}
1104 
1105 	/* Webcams don't have input connectors */
1106 	if (dev->is_webcam)
1107 		return;
1108 
1109 	/* Create entities for each input connector */
1110 	for (i = 0; i < MAX_EM28XX_INPUT; i++) {
1111 		struct media_entity *ent = &dev->input_ent[i];
1112 
1113 		if (!INPUT(i)->type)
1114 			break;
1115 
1116 		ent->name = iname[INPUT(i)->type];
1117 		ent->flags = MEDIA_ENT_FL_CONNECTOR;
1118 		dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1119 
1120 		switch (INPUT(i)->type) {
1121 		case EM28XX_VMUX_COMPOSITE:
1122 			ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1123 			break;
1124 		case EM28XX_VMUX_SVIDEO:
1125 			ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1126 			break;
1127 		default: /* EM28XX_VMUX_TELEVISION or EM28XX_RADIO */
1128 			if (dev->tuner_type != TUNER_ABSENT || em28xx_analogtv_supported(dev))
1129 				ent->function = MEDIA_ENT_F_CONN_RF;
1130 			break;
1131 		}
1132 
1133 		ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1134 		if (ret < 0)
1135 			dev_err(&dev->intf->dev,
1136 				"failed to initialize input pad[%d]!\n", i);
1137 
1138 		ret = media_device_register_entity(dev->media_dev, ent);
1139 		if (ret < 0)
1140 			dev_err(&dev->intf->dev,
1141 				"failed to register input entity %d!\n", i);
1142 	}
1143 
1144 	if (dev->board.decoder == EM28XX_BUILTIN) {
1145 		v4l2->decoder_pads[EM2828X_PAD_INPUT].flags = MEDIA_PAD_FL_SINK;
1146 		v4l2->decoder_pads[EM2828X_PAD_INPUT].sig_type = PAD_SIGNAL_ANALOG;
1147 		v4l2->decoder_pads[EM2828X_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
1148 		v4l2->decoder_pads[EM2828X_PAD_VID_OUT].sig_type = PAD_SIGNAL_DV;
1149 
1150 		v4l2->decoder = kzalloc_obj(*v4l2->decoder);
1151 		v4l2->decoder->name = "em2828x_builtin";
1152 		v4l2->decoder->function = MEDIA_ENT_F_ATV_DECODER;
1153 
1154 		ret = media_entity_pads_init(v4l2->decoder, EM2828X_NUM_PADS,
1155 					     &v4l2->decoder_pads[0]);
1156 		if (ret < 0)
1157 			dev_err(&dev->intf->dev, "failed to initialize decoder pads %d!\n", ret);
1158 
1159 		ret = media_device_register_entity(dev->media_dev, v4l2->decoder);
1160 		if (ret < 0)
1161 			dev_err(&dev->intf->dev, "failed to register decoder entity %d!\n", ret);
1162 	}
1163 
1164 #endif
1165 }
1166 
1167 /*
1168  * Videobuf2 operations
1169  */
1170 
queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])1171 static int queue_setup(struct vb2_queue *vq,
1172 		       unsigned int *nbuffers, unsigned int *nplanes,
1173 		       unsigned int sizes[], struct device *alloc_devs[])
1174 {
1175 	struct em28xx *dev = vb2_get_drv_priv(vq);
1176 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1177 	unsigned long size =
1178 		    (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3;
1179 
1180 	if (*nplanes)
1181 		return sizes[0] < size ? -EINVAL : 0;
1182 	*nplanes = 1;
1183 	sizes[0] = size;
1184 
1185 	em28xx_enable_analog_tuner(dev);
1186 
1187 	return 0;
1188 }
1189 
1190 static int
buffer_prepare(struct vb2_buffer * vb)1191 buffer_prepare(struct vb2_buffer *vb)
1192 {
1193 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1194 	struct em28xx        *dev = vb2_get_drv_priv(vb->vb2_queue);
1195 	struct em28xx_v4l2   *v4l2 = dev->v4l2;
1196 	unsigned long size;
1197 
1198 	em28xx_videodbg("%s, field=%d\n", __func__, vbuf->field);
1199 
1200 	size = (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3;
1201 
1202 	if (vb2_plane_size(vb, 0) < size) {
1203 		em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
1204 				__func__, vb2_plane_size(vb, 0), size);
1205 		return -EINVAL;
1206 	}
1207 	vb2_set_plane_payload(vb, 0, size);
1208 
1209 	return 0;
1210 }
1211 
em28xx_start_analog_streaming(struct vb2_queue * vq,unsigned int count)1212 int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
1213 {
1214 	struct em28xx *dev = vb2_get_drv_priv(vq);
1215 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1216 	struct em28xx_dmaqueue *dmaq = vq->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
1217 		&dev->vbiq : &dev->vidq;
1218 	unsigned long flags = 0;
1219 	struct v4l2_frequency f;
1220 	struct v4l2_fh *owner;
1221 	int rc = 0;
1222 
1223 	em28xx_videodbg("%s\n", __func__);
1224 
1225 	dev->v4l2->field_count = 0;
1226 
1227 	/*
1228 	 * Make sure streaming is not already in progress for this type
1229 	 * of filehandle (e.g. video, vbi)
1230 	 */
1231 	rc = res_get(dev, vq->type);
1232 	if (rc)
1233 		goto exit;
1234 
1235 	if (v4l2->streaming_users == 0) {
1236 		/* First active streaming user, so allocate all the URBs */
1237 
1238 		/* Allocate the USB bandwidth */
1239 		em28xx_set_alternate(dev);
1240 
1241 		/*
1242 		 * Needed, since GPIO might have disabled power of
1243 		 * some i2c device
1244 		 */
1245 		em28xx_wake_i2c(dev);
1246 
1247 		v4l2->capture_type = -1;
1248 		rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
1249 					  dev->analog_xfer_bulk,
1250 					  EM28XX_NUM_BUFS,
1251 					  dev->max_pkt_size,
1252 					  dev->packet_multiplier,
1253 					  em28xx_urb_data_copy);
1254 		if (rc < 0) {
1255 			res_free(dev, vq->type);
1256 			goto exit;
1257 		}
1258 
1259 		/*
1260 		 * djh: it's not clear whether this code is still needed.  I'm
1261 		 * leaving it in here for now entirely out of concern for
1262 		 * backward compatibility (the old code did it)
1263 		 */
1264 
1265 		/* Ask tuner to go to analog or radio mode */
1266 		memset(&f, 0, sizeof(f));
1267 		f.frequency = v4l2->frequency;
1268 		owner = (struct v4l2_fh *)vq->owner;
1269 		if (owner && owner->vdev->vfl_type == VFL_TYPE_RADIO)
1270 			f.type = V4L2_TUNER_RADIO;
1271 		else
1272 			f.type = V4L2_TUNER_ANALOG_TV;
1273 		v4l2_device_call_all(&v4l2->v4l2_dev,
1274 				     0, tuner, s_frequency, &f);
1275 
1276 		/* Enable video stream at TV decoder */
1277 		v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 1);
1278 	}
1279 
1280 	v4l2->streaming_users++;
1281 	return 0;
1282 
1283 exit:
1284 	spin_lock_irqsave(&dev->slock, flags);
1285 	while (!list_empty(&dmaq->active)) {
1286 		struct em28xx_buffer *buf;
1287 
1288 		buf = list_entry(dmaq->active.next, struct em28xx_buffer, list);
1289 		list_del(&buf->list);
1290 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
1291 	}
1292 	spin_unlock_irqrestore(&dev->slock, flags);
1293 	return rc;
1294 }
1295 
em28xx_stop_streaming(struct vb2_queue * vq)1296 static void em28xx_stop_streaming(struct vb2_queue *vq)
1297 {
1298 	struct em28xx *dev = vb2_get_drv_priv(vq);
1299 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1300 	struct em28xx_dmaqueue *vidq = &dev->vidq;
1301 	unsigned long flags = 0;
1302 
1303 	em28xx_videodbg("%s\n", __func__);
1304 
1305 	res_free(dev, vq->type);
1306 
1307 	if (v4l2->streaming_users-- == 1) {
1308 		/* Disable video stream at TV decoder */
1309 		v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 0);
1310 
1311 		/* Last active user, so shutdown all the URBS */
1312 		em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
1313 	}
1314 
1315 	spin_lock_irqsave(&dev->slock, flags);
1316 	if (dev->usb_ctl.vid_buf) {
1317 		vb2_buffer_done(&dev->usb_ctl.vid_buf->vb.vb2_buf,
1318 				VB2_BUF_STATE_ERROR);
1319 		dev->usb_ctl.vid_buf = NULL;
1320 	}
1321 	while (!list_empty(&vidq->active)) {
1322 		struct em28xx_buffer *buf;
1323 
1324 		buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
1325 		list_del(&buf->list);
1326 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1327 	}
1328 	spin_unlock_irqrestore(&dev->slock, flags);
1329 }
1330 
em28xx_stop_vbi_streaming(struct vb2_queue * vq)1331 void em28xx_stop_vbi_streaming(struct vb2_queue *vq)
1332 {
1333 	struct em28xx *dev = vb2_get_drv_priv(vq);
1334 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1335 	struct em28xx_dmaqueue *vbiq = &dev->vbiq;
1336 	unsigned long flags = 0;
1337 
1338 	em28xx_videodbg("%s\n", __func__);
1339 
1340 	res_free(dev, vq->type);
1341 
1342 	if (v4l2->streaming_users-- == 1) {
1343 		/* Disable video stream at TV decoder */
1344 		v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 0);
1345 
1346 		/* Last active user, so shutdown all the URBS */
1347 		em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
1348 	}
1349 
1350 	spin_lock_irqsave(&dev->slock, flags);
1351 	if (dev->usb_ctl.vbi_buf) {
1352 		vb2_buffer_done(&dev->usb_ctl.vbi_buf->vb.vb2_buf,
1353 				VB2_BUF_STATE_ERROR);
1354 		dev->usb_ctl.vbi_buf = NULL;
1355 	}
1356 	while (!list_empty(&vbiq->active)) {
1357 		struct em28xx_buffer *buf;
1358 
1359 		buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
1360 		list_del(&buf->list);
1361 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1362 	}
1363 	spin_unlock_irqrestore(&dev->slock, flags);
1364 }
1365 
1366 static void
buffer_queue(struct vb2_buffer * vb)1367 buffer_queue(struct vb2_buffer *vb)
1368 {
1369 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1370 	struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
1371 	struct em28xx_buffer *buf =
1372 		container_of(vbuf, struct em28xx_buffer, vb);
1373 	struct em28xx_dmaqueue *vidq = &dev->vidq;
1374 	unsigned long flags = 0;
1375 
1376 	em28xx_videodbg("%s\n", __func__);
1377 	buf->mem = vb2_plane_vaddr(vb, 0);
1378 	buf->length = vb2_plane_size(vb, 0);
1379 
1380 	spin_lock_irqsave(&dev->slock, flags);
1381 	list_add_tail(&buf->list, &vidq->active);
1382 	spin_unlock_irqrestore(&dev->slock, flags);
1383 }
1384 
1385 static const struct vb2_ops em28xx_video_qops = {
1386 	.queue_setup    = queue_setup,
1387 	.buf_prepare    = buffer_prepare,
1388 	.buf_queue      = buffer_queue,
1389 	.start_streaming = em28xx_start_analog_streaming,
1390 	.stop_streaming = em28xx_stop_streaming,
1391 };
1392 
em28xx_vb2_setup(struct em28xx * dev)1393 static int em28xx_vb2_setup(struct em28xx *dev)
1394 {
1395 	int rc;
1396 	struct vb2_queue *q;
1397 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1398 
1399 	/* Setup Videobuf2 for Video capture */
1400 	q = &v4l2->vb_vidq;
1401 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1402 	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1403 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1404 	q->drv_priv = dev;
1405 	q->buf_struct_size = sizeof(struct em28xx_buffer);
1406 	q->ops = &em28xx_video_qops;
1407 	q->mem_ops = &vb2_vmalloc_memops;
1408 
1409 	rc = vb2_queue_init(q);
1410 	if (rc < 0)
1411 		return rc;
1412 
1413 	/* Setup Videobuf2 for VBI capture */
1414 	q = &v4l2->vb_vbiq;
1415 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1416 	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
1417 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1418 	q->drv_priv = dev;
1419 	q->buf_struct_size = sizeof(struct em28xx_buffer);
1420 	q->ops = &em28xx_vbi_qops;
1421 	q->mem_ops = &vb2_vmalloc_memops;
1422 
1423 	rc = vb2_queue_init(q);
1424 	if (rc < 0)
1425 		return rc;
1426 
1427 	return 0;
1428 }
1429 
1430 /*
1431  * v4l2 interface
1432  */
1433 
video_mux(struct em28xx * dev,int index)1434 static void video_mux(struct em28xx *dev, int index)
1435 {
1436 	struct v4l2_device *v4l2_dev = &dev->v4l2->v4l2_dev;
1437 
1438 	dev->ctl_input = index;
1439 	dev->ctl_ainput = INPUT(index)->amux;
1440 	dev->ctl_aoutput = INPUT(index)->aout;
1441 
1442 	if (!dev->ctl_aoutput)
1443 		dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1444 
1445 	v4l2_device_call_all(v4l2_dev, 0, video, s_routing,
1446 			     INPUT(index)->vmux, 0, 0);
1447 
1448 	if (dev->has_msp34xx) {
1449 		if (dev->i2s_speed) {
1450 			v4l2_device_call_all(v4l2_dev, 0, audio,
1451 					     s_i2s_clock_freq, dev->i2s_speed);
1452 		}
1453 		/* Note: this is msp3400 specific */
1454 		v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
1455 				     dev->ctl_ainput,
1456 				     MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
1457 	}
1458 
1459 	if (dev->board.decoder == EM28XX_BUILTIN) {
1460 		em2828X_decoder_vmux(dev, INPUT(index)->vmux);
1461 		em2828X_decoder_set_std(dev, dev->v4l2->norm);
1462 
1463 		em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
1464 	}
1465 
1466 	if (dev->board.adecoder != EM28XX_NOADECODER) {
1467 		v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
1468 				     dev->ctl_ainput, dev->ctl_aoutput, 0);
1469 	}
1470 
1471 	em28xx_audio_analog_set(dev);
1472 }
1473 
em28xx_ctrl_notify(struct v4l2_ctrl * ctrl,void * priv)1474 static void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
1475 {
1476 	struct em28xx *dev = priv;
1477 
1478 	/*
1479 	 * In the case of non-AC97 volume controls, we still need
1480 	 * to do some setups at em28xx, in order to mute/unmute
1481 	 * and to adjust audio volume. However, the value ranges
1482 	 * should be checked by the corresponding V4L subdriver.
1483 	 */
1484 	switch (ctrl->id) {
1485 	case V4L2_CID_AUDIO_MUTE:
1486 		dev->mute = ctrl->val;
1487 		em28xx_audio_analog_set(dev);
1488 		break;
1489 	case V4L2_CID_AUDIO_VOLUME:
1490 		dev->volume = ctrl->val;
1491 		em28xx_audio_analog_set(dev);
1492 		break;
1493 	}
1494 }
1495 
em28xx_s_ctrl(struct v4l2_ctrl * ctrl)1496 static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
1497 {
1498 	struct em28xx_v4l2 *v4l2 =
1499 		  container_of(ctrl->handler, struct em28xx_v4l2, ctrl_handler);
1500 	struct em28xx *dev = v4l2->dev;
1501 	int ret = -EINVAL;
1502 
1503 	switch (ctrl->id) {
1504 	case V4L2_CID_AUDIO_MUTE:
1505 		dev->mute = ctrl->val;
1506 		ret = em28xx_audio_analog_set(dev);
1507 		break;
1508 	case V4L2_CID_AUDIO_VOLUME:
1509 		dev->volume = ctrl->val;
1510 		ret = em28xx_audio_analog_set(dev);
1511 		break;
1512 	case V4L2_CID_CONTRAST:
1513 		ret = em28xx_write_reg(dev, EM28XX_R20_YGAIN, ctrl->val);
1514 		break;
1515 	case V4L2_CID_BRIGHTNESS:
1516 		ret = em28xx_write_reg(dev, EM28XX_R21_YOFFSET, ctrl->val);
1517 		break;
1518 	case V4L2_CID_SATURATION:
1519 		ret = em28xx_write_reg(dev, EM28XX_R22_UVGAIN, ctrl->val);
1520 		break;
1521 	case V4L2_CID_BLUE_BALANCE:
1522 		ret = em28xx_write_reg(dev, EM28XX_R23_UOFFSET, ctrl->val);
1523 		break;
1524 	case V4L2_CID_RED_BALANCE:
1525 		ret = em28xx_write_reg(dev, EM28XX_R24_VOFFSET, ctrl->val);
1526 		break;
1527 	case V4L2_CID_SHARPNESS:
1528 		ret = em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, ctrl->val);
1529 		break;
1530 	}
1531 
1532 	return (ret < 0) ? ret : 0;
1533 }
1534 
1535 static const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
1536 	.s_ctrl = em28xx_s_ctrl,
1537 };
1538 
size_to_scale(struct em28xx * dev,unsigned int width,unsigned int height,unsigned int * hscale,unsigned int * vscale)1539 static void size_to_scale(struct em28xx *dev,
1540 			  unsigned int width, unsigned int height,
1541 			unsigned int *hscale, unsigned int *vscale)
1542 {
1543 	unsigned int          maxw = norm_maxw(dev);
1544 	unsigned int          maxh = norm_maxh(dev);
1545 
1546 	*hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1547 	if (*hscale > EM28XX_HVSCALE_MAX)
1548 		*hscale = EM28XX_HVSCALE_MAX;
1549 
1550 	*vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1551 	if (*vscale > EM28XX_HVSCALE_MAX)
1552 		*vscale = EM28XX_HVSCALE_MAX;
1553 }
1554 
scale_to_size(struct em28xx * dev,unsigned int hscale,unsigned int vscale,unsigned int * width,unsigned int * height)1555 static void scale_to_size(struct em28xx *dev,
1556 			  unsigned int hscale, unsigned int vscale,
1557 			  unsigned int *width, unsigned int *height)
1558 {
1559 	unsigned int          maxw = norm_maxw(dev);
1560 	unsigned int          maxh = norm_maxh(dev);
1561 
1562 	*width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1563 	*height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1564 
1565 	/* Don't let width or height to be zero */
1566 	if (*width < 1)
1567 		*width = 1;
1568 	if (*height < 1)
1569 		*height = 1;
1570 }
1571 
1572 /*
1573  * IOCTL vidioc handling
1574  */
1575 
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1576 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1577 				struct v4l2_format *f)
1578 {
1579 	struct em28xx         *dev = video_drvdata(file);
1580 	struct em28xx_v4l2    *v4l2 = dev->v4l2;
1581 
1582 	f->fmt.pix.width = v4l2->width;
1583 	f->fmt.pix.height = v4l2->height;
1584 	f->fmt.pix.pixelformat = v4l2->format->fourcc;
1585 	f->fmt.pix.bytesperline = (v4l2->width * v4l2->format->depth + 7) >> 3;
1586 	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * v4l2->height;
1587 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1588 
1589 	/* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1590 	if (v4l2->progressive)
1591 		f->fmt.pix.field = V4L2_FIELD_NONE;
1592 	else
1593 		f->fmt.pix.field = v4l2->interlaced_fieldmode ?
1594 			   V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1595 	return 0;
1596 }
1597 
format_by_fourcc(unsigned int fourcc)1598 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1599 {
1600 	unsigned int i;
1601 
1602 	for (i = 0; i < ARRAY_SIZE(format); i++)
1603 		if (format[i].fourcc == fourcc)
1604 			return &format[i];
1605 
1606 	return NULL;
1607 }
1608 
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1609 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1610 				  struct v4l2_format *f)
1611 {
1612 	struct em28xx         *dev   = video_drvdata(file);
1613 	struct em28xx_v4l2    *v4l2  = dev->v4l2;
1614 	unsigned int          width  = f->fmt.pix.width;
1615 	unsigned int          height = f->fmt.pix.height;
1616 	unsigned int          maxw   = norm_maxw(dev);
1617 	unsigned int          maxh   = norm_maxh(dev);
1618 	unsigned int          hscale, vscale;
1619 	struct em28xx_fmt     *fmt;
1620 
1621 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1622 	if (!fmt) {
1623 		fmt = &format[0];
1624 		em28xx_videodbg("Fourcc format (%08x) invalid. Using default (%08x).\n",
1625 				f->fmt.pix.pixelformat, fmt->fourcc);
1626 	}
1627 
1628 	if (dev->board.is_em2800) {
1629 		/* the em2800 can only scale down to 50% */
1630 		height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1631 		width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1632 		/*
1633 		 * MaxPacketSize for em2800 is too small to capture at full
1634 		 * resolution use half of maxw as the scaler can only scale
1635 		 * to 50%
1636 		 */
1637 		if (width == maxw && height == maxh)
1638 			width /= 2;
1639 	} else {
1640 		/*
1641 		 * width must even because of the YUYV format
1642 		 * height must be even because of interlacing
1643 		 */
1644 		v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1645 				      1, 0);
1646 	}
1647 	/* Avoid division by zero at size_to_scale */
1648 	if (width < 1)
1649 		width = 1;
1650 	if (height < 1)
1651 		height = 1;
1652 
1653 	size_to_scale(dev, width, height, &hscale, &vscale);
1654 	scale_to_size(dev, hscale, vscale, &width, &height);
1655 
1656 	f->fmt.pix.width = width;
1657 	f->fmt.pix.height = height;
1658 	f->fmt.pix.pixelformat = fmt->fourcc;
1659 	f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
1660 	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1661 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1662 	if (v4l2->progressive)
1663 		f->fmt.pix.field = V4L2_FIELD_NONE;
1664 	else
1665 		f->fmt.pix.field = v4l2->interlaced_fieldmode ?
1666 			   V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1667 
1668 	return 0;
1669 }
1670 
em28xx_set_video_format(struct em28xx * dev,unsigned int fourcc,unsigned int width,unsigned int height)1671 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1672 				   unsigned int width, unsigned int height)
1673 {
1674 	struct em28xx_fmt     *fmt;
1675 	struct em28xx_v4l2    *v4l2 = dev->v4l2;
1676 
1677 	fmt = format_by_fourcc(fourcc);
1678 	if (!fmt)
1679 		return -EINVAL;
1680 
1681 	v4l2->format = fmt;
1682 	v4l2->width  = width;
1683 	v4l2->height = height;
1684 
1685 	/* set new image size */
1686 	size_to_scale(dev, v4l2->width, v4l2->height,
1687 		      &v4l2->hscale, &v4l2->vscale);
1688 
1689 	em28xx_resolution_set(dev);
1690 
1691 	return 0;
1692 }
1693 
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1694 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1695 				struct v4l2_format *f)
1696 {
1697 	struct em28xx *dev = video_drvdata(file);
1698 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1699 
1700 	if (vb2_is_busy(&v4l2->vb_vidq))
1701 		return -EBUSY;
1702 
1703 	vidioc_try_fmt_vid_cap(file, priv, f);
1704 
1705 	return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1706 				f->fmt.pix.width, f->fmt.pix.height);
1707 }
1708 
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * norm)1709 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1710 {
1711 	struct em28xx *dev = video_drvdata(file);
1712 
1713 	*norm = dev->v4l2->norm;
1714 
1715 	return 0;
1716 }
1717 
vidioc_querystd(struct file * file,void * priv,v4l2_std_id * norm)1718 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1719 {
1720 	struct em28xx *dev = video_drvdata(file);
1721 
1722 	v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, video, querystd, norm);
1723 
1724 	return 0;
1725 }
1726 
vidioc_s_std(struct file * file,void * priv,v4l2_std_id norm)1727 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1728 {
1729 	struct em28xx      *dev  = video_drvdata(file);
1730 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1731 	struct v4l2_format f;
1732 
1733 	if (norm == v4l2->norm)
1734 		return 0;
1735 
1736 	if (v4l2->streaming_users > 0)
1737 		return -EBUSY;
1738 
1739 	v4l2->norm = norm;
1740 
1741 	/* Adjusts width/height, if needed */
1742 	f.fmt.pix.width = 720;
1743 	f.fmt.pix.height = (norm & V4L2_STD_525_60) ? 480 : 576;
1744 	vidioc_try_fmt_vid_cap(file, priv, &f);
1745 
1746 	/* set new image size */
1747 	v4l2->width = f.fmt.pix.width;
1748 	v4l2->height = f.fmt.pix.height;
1749 	size_to_scale(dev, v4l2->width, v4l2->height,
1750 		      &v4l2->hscale, &v4l2->vscale);
1751 
1752 	em28xx_resolution_set(dev);
1753 	v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
1754 
1755 	if (dev->board.decoder == EM28XX_BUILTIN)
1756 		em2828X_decoder_set_std(dev, v4l2->norm);
1757 
1758 	return 0;
1759 }
1760 
vidioc_g_parm(struct file * file,void * priv,struct v4l2_streamparm * p)1761 static int vidioc_g_parm(struct file *file, void *priv,
1762 			 struct v4l2_streamparm *p)
1763 {
1764 	struct v4l2_subdev_frame_interval ival = { 0 };
1765 	struct em28xx      *dev  = video_drvdata(file);
1766 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
1767 	int rc = 0;
1768 
1769 	if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1770 	    p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1771 		return -EINVAL;
1772 
1773 	p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1774 	p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1775 	if (dev->is_webcam) {
1776 		rc = v4l2_device_call_until_err(&v4l2->v4l2_dev, 0,
1777 						pad, get_frame_interval, NULL,
1778 						&ival);
1779 		if (!rc)
1780 			p->parm.capture.timeperframe = ival.interval;
1781 	} else {
1782 		v4l2_video_std_frame_period(v4l2->norm,
1783 					    &p->parm.capture.timeperframe);
1784 	}
1785 
1786 	return rc;
1787 }
1788 
vidioc_s_parm(struct file * file,void * priv,struct v4l2_streamparm * p)1789 static int vidioc_s_parm(struct file *file, void *priv,
1790 			 struct v4l2_streamparm *p)
1791 {
1792 	struct em28xx *dev = video_drvdata(file);
1793 	struct v4l2_subdev_frame_interval ival = {
1794 		0,
1795 		p->parm.capture.timeperframe
1796 	};
1797 	int rc = 0;
1798 
1799 	if (!dev->is_webcam)
1800 		return -ENOTTY;
1801 
1802 	if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1803 	    p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1804 		return -EINVAL;
1805 
1806 	memset(&p->parm, 0, sizeof(p->parm));
1807 	p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1808 	p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1809 	rc = v4l2_device_call_until_err(&dev->v4l2->v4l2_dev, 0,
1810 					pad, set_frame_interval, NULL,
1811 					&ival);
1812 	if (!rc)
1813 		p->parm.capture.timeperframe = ival.interval;
1814 	return rc;
1815 }
1816 
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)1817 static int vidioc_enum_input(struct file *file, void *priv,
1818 			     struct v4l2_input *i)
1819 {
1820 	struct em28xx *dev = video_drvdata(file);
1821 	unsigned int       n;
1822 	int j;
1823 
1824 	n = i->index;
1825 	if (n >= MAX_EM28XX_INPUT)
1826 		return -EINVAL;
1827 	if (!INPUT(n)->type)
1828 		return -EINVAL;
1829 
1830 	i->type = V4L2_INPUT_TYPE_CAMERA;
1831 
1832 	strscpy(i->name, iname[INPUT(n)->type], sizeof(i->name));
1833 
1834 	if (INPUT(n)->type == EM28XX_VMUX_TELEVISION)
1835 		i->type = V4L2_INPUT_TYPE_TUNER;
1836 
1837 	i->std = dev->v4l2->vdev.tvnorms;
1838 	/* webcams do not have the STD API */
1839 	if (dev->is_webcam)
1840 		i->capabilities = 0;
1841 
1842 	/* Dynamically generates an audioset bitmask */
1843 	i->audioset = 0;
1844 	for (j = 0; j < MAX_EM28XX_INPUT; j++)
1845 		if (dev->amux_map[j] != EM28XX_AMUX_UNUSED)
1846 			i->audioset |= 1 << j;
1847 
1848 	return 0;
1849 }
1850 
vidioc_g_input(struct file * file,void * priv,unsigned int * i)1851 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1852 {
1853 	struct em28xx *dev = video_drvdata(file);
1854 
1855 	*i = dev->ctl_input;
1856 
1857 	return 0;
1858 }
1859 
vidioc_s_input(struct file * file,void * priv,unsigned int i)1860 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1861 {
1862 	struct em28xx *dev = video_drvdata(file);
1863 
1864 	if (i >= MAX_EM28XX_INPUT)
1865 		return -EINVAL;
1866 	if (!INPUT(i)->type)
1867 		return -EINVAL;
1868 
1869 	video_mux(dev, i);
1870 	return 0;
1871 }
1872 
em28xx_fill_audio_input(struct em28xx * dev,const char * s,struct v4l2_audio * a,unsigned int index)1873 static int em28xx_fill_audio_input(struct em28xx *dev,
1874 				   const char *s,
1875 				   struct v4l2_audio *a,
1876 				   unsigned int index)
1877 {
1878 	unsigned int idx = dev->amux_map[index];
1879 
1880 	/*
1881 	 * With msp3400, almost all mappings use the default (amux = 0).
1882 	 * The only one may use a different value is WinTV USB2, where it
1883 	 * can also be SCART1 input.
1884 	 * As it is very doubtful that we would see new boards with msp3400,
1885 	 * let's just reuse the existing switch.
1886 	 */
1887 	if (dev->has_msp34xx && idx != EM28XX_AMUX_UNUSED)
1888 		idx = EM28XX_AMUX_LINE_IN;
1889 
1890 	switch (idx) {
1891 	case EM28XX_AMUX_VIDEO:
1892 		strscpy(a->name, "Television", sizeof(a->name));
1893 		break;
1894 	case EM28XX_AMUX_LINE_IN:
1895 		strscpy(a->name, "Line In", sizeof(a->name));
1896 		break;
1897 	case EM28XX_AMUX_VIDEO2:
1898 		strscpy(a->name, "Television alt", sizeof(a->name));
1899 		break;
1900 	case EM28XX_AMUX_PHONE:
1901 		strscpy(a->name, "Phone", sizeof(a->name));
1902 		break;
1903 	case EM28XX_AMUX_MIC:
1904 		strscpy(a->name, "Mic", sizeof(a->name));
1905 		break;
1906 	case EM28XX_AMUX_CD:
1907 		strscpy(a->name, "CD", sizeof(a->name));
1908 		break;
1909 	case EM28XX_AMUX_AUX:
1910 		strscpy(a->name, "Aux", sizeof(a->name));
1911 		break;
1912 	case EM28XX_AMUX_PCM_OUT:
1913 		strscpy(a->name, "PCM", sizeof(a->name));
1914 		break;
1915 	case EM28XX_AMUX_UNUSED:
1916 	default:
1917 		return -EINVAL;
1918 	}
1919 	a->index = index;
1920 	a->capability = V4L2_AUDCAP_STEREO;
1921 
1922 	em28xx_videodbg("%s: audio input index %d is '%s'\n",
1923 			s, a->index, a->name);
1924 
1925 	return 0;
1926 }
1927 
vidioc_enumaudio(struct file * file,void * fh,struct v4l2_audio * a)1928 static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
1929 {
1930 	struct em28xx *dev = video_drvdata(file);
1931 
1932 	if (a->index >= MAX_EM28XX_INPUT)
1933 		return -EINVAL;
1934 
1935 	return em28xx_fill_audio_input(dev, __func__, a, a->index);
1936 }
1937 
vidioc_g_audio(struct file * file,void * priv,struct v4l2_audio * a)1938 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1939 {
1940 	struct em28xx *dev = video_drvdata(file);
1941 	int i;
1942 
1943 	for (i = 0; i < MAX_EM28XX_INPUT; i++)
1944 		if (dev->ctl_ainput == dev->amux_map[i])
1945 			return em28xx_fill_audio_input(dev, __func__, a, i);
1946 
1947 	/* Should never happen! */
1948 	return -EINVAL;
1949 }
1950 
vidioc_s_audio(struct file * file,void * priv,const struct v4l2_audio * a)1951 static int vidioc_s_audio(struct file *file, void *priv,
1952 			  const struct v4l2_audio *a)
1953 {
1954 	struct em28xx *dev = video_drvdata(file);
1955 	int idx, i;
1956 
1957 	if (a->index >= MAX_EM28XX_INPUT)
1958 		return -EINVAL;
1959 
1960 	idx = dev->amux_map[a->index];
1961 
1962 	if (idx == EM28XX_AMUX_UNUSED)
1963 		return -EINVAL;
1964 
1965 	dev->ctl_ainput = idx;
1966 
1967 	/*
1968 	 * FIXME: This is wrong, as different inputs at em28xx_cards
1969 	 * may have different audio outputs. So, the right thing
1970 	 * to do is to implement VIDIOC_G_AUDOUT/VIDIOC_S_AUDOUT.
1971 	 * With the current board definitions, this would work fine,
1972 	 * as, currently, all boards fit.
1973 	 */
1974 	for (i = 0; i < MAX_EM28XX_INPUT; i++)
1975 		if (idx == dev->amux_map[i])
1976 			break;
1977 	if (i == MAX_EM28XX_INPUT)
1978 		return -EINVAL;
1979 
1980 	dev->ctl_aoutput = INPUT(i)->aout;
1981 
1982 	if (!dev->ctl_aoutput)
1983 		dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1984 
1985 	em28xx_videodbg("%s: set audio input to %d\n", __func__,
1986 			dev->ctl_ainput);
1987 
1988 	return 0;
1989 }
1990 
vidioc_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)1991 static int vidioc_g_tuner(struct file *file, void *priv,
1992 			  struct v4l2_tuner *t)
1993 {
1994 	struct em28xx *dev = video_drvdata(file);
1995 
1996 	if (t->index != 0)
1997 		return -EINVAL;
1998 
1999 	strscpy(t->name, "Tuner", sizeof(t->name));
2000 
2001 	t->type = V4L2_TUNER_ANALOG_TV;
2002 	t->capability = V4L2_TUNER_CAP_NORM;
2003 	t->rangehigh = 0xffffffffUL;
2004 	t->signal = 0xffff;     /* LOCKED */
2005 
2006 	v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
2007 	return 0;
2008 }
2009 
vidioc_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)2010 static int vidioc_s_tuner(struct file *file, void *priv,
2011 			  const struct v4l2_tuner *t)
2012 {
2013 	struct em28xx *dev = video_drvdata(file);
2014 
2015 	if (t->index != 0)
2016 		return -EINVAL;
2017 
2018 	v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
2019 	return 0;
2020 }
2021 
vidioc_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)2022 static int vidioc_g_frequency(struct file *file, void *priv,
2023 			      struct v4l2_frequency *f)
2024 {
2025 	struct em28xx         *dev = video_drvdata(file);
2026 	struct em28xx_v4l2    *v4l2 = dev->v4l2;
2027 
2028 	if (f->tuner != 0)
2029 		return -EINVAL;
2030 
2031 	f->frequency = v4l2->frequency;
2032 	return 0;
2033 }
2034 
vidioc_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)2035 static int vidioc_s_frequency(struct file *file, void *priv,
2036 			      const struct v4l2_frequency *f)
2037 {
2038 	struct v4l2_frequency  new_freq = *f;
2039 	struct em28xx             *dev  = video_drvdata(file);
2040 	struct em28xx_v4l2        *v4l2 = dev->v4l2;
2041 
2042 	if (f->tuner != 0)
2043 		return -EINVAL;
2044 
2045 	switch (dev->model) {
2046 	case EM2828X_BOARD_HAUPPAUGE_935_V2:
2047 	case EM2828X_BOARD_HAUPPAUGE_955_V2:
2048 	case EM2828X_BOARD_HAUPPAUGE_975_V2:
2049 		if (dev->em28xx_set_analog_freq)
2050 			dev->em28xx_set_analog_freq(dev, f->frequency);
2051 		break;
2052 	default:
2053 		v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_frequency, f);
2054 		break;
2055 	}
2056 
2057 	v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, g_frequency, &new_freq);
2058 	v4l2->frequency = new_freq.frequency;
2059 
2060 	return 0;
2061 }
2062 
2063 #ifdef CONFIG_VIDEO_ADV_DEBUG
vidioc_g_chip_info(struct file * file,void * priv,struct v4l2_dbg_chip_info * chip)2064 static int vidioc_g_chip_info(struct file *file, void *priv,
2065 			      struct v4l2_dbg_chip_info *chip)
2066 {
2067 	struct em28xx *dev = video_drvdata(file);
2068 
2069 	if (chip->match.addr > 1)
2070 		return -EINVAL;
2071 	if (chip->match.addr == 1)
2072 		strscpy(chip->name, "ac97", sizeof(chip->name));
2073 	else
2074 		strscpy(chip->name,
2075 			dev->v4l2->v4l2_dev.name, sizeof(chip->name));
2076 	return 0;
2077 }
2078 
em28xx_reg_len(int reg)2079 static int em28xx_reg_len(int reg)
2080 {
2081 	switch (reg) {
2082 	case EM28XX_R40_AC97LSB:
2083 	case EM28XX_R30_HSCALELOW:
2084 	case EM28XX_R32_VSCALELOW:
2085 		return 2;
2086 	default:
2087 		return 1;
2088 	}
2089 }
2090 
vidioc_g_register(struct file * file,void * priv,struct v4l2_dbg_register * reg)2091 static int vidioc_g_register(struct file *file, void *priv,
2092 			     struct v4l2_dbg_register *reg)
2093 {
2094 	struct em28xx *dev = video_drvdata(file);
2095 	int ret;
2096 
2097 	if (reg->match.addr > 1)
2098 		return -EINVAL;
2099 	if (reg->match.addr) {
2100 		ret = em28xx_read_ac97(dev, reg->reg);
2101 		if (ret < 0)
2102 			return ret;
2103 
2104 		reg->val = ret;
2105 		reg->size = 1;
2106 		return 0;
2107 	}
2108 
2109 	/* Match host */
2110 	reg->size = em28xx_reg_len(reg->reg);
2111 	if (reg->size == 1) {
2112 		ret = em28xx_read_reg(dev, reg->reg);
2113 
2114 		if (ret < 0)
2115 			return ret;
2116 
2117 		reg->val = ret;
2118 	} else {
2119 		__le16 val = 0;
2120 
2121 		ret = dev->em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
2122 						   reg->reg, (char *)&val, 2);
2123 		if (ret < 0)
2124 			return ret;
2125 
2126 		reg->val = le16_to_cpu(val);
2127 	}
2128 
2129 	return 0;
2130 }
2131 
vidioc_s_register(struct file * file,void * priv,const struct v4l2_dbg_register * reg)2132 static int vidioc_s_register(struct file *file, void *priv,
2133 			     const struct v4l2_dbg_register *reg)
2134 {
2135 	struct em28xx *dev = video_drvdata(file);
2136 	__le16 buf;
2137 
2138 	if (reg->match.addr > 1)
2139 		return -EINVAL;
2140 	if (reg->match.addr)
2141 		return em28xx_write_ac97(dev, reg->reg, reg->val);
2142 
2143 	/* Match host */
2144 	buf = cpu_to_le16(reg->val);
2145 
2146 	return em28xx_write_regs(dev, reg->reg, (char *)&buf,
2147 			       em28xx_reg_len(reg->reg));
2148 }
2149 #endif
2150 
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)2151 static int vidioc_querycap(struct file *file, void  *priv,
2152 			   struct v4l2_capability *cap)
2153 {
2154 	struct em28xx         *dev  = video_drvdata(file);
2155 	struct em28xx_v4l2    *v4l2 = dev->v4l2;
2156 	struct usb_device *udev = interface_to_usbdev(dev->intf);
2157 
2158 	strscpy(cap->driver, "em28xx", sizeof(cap->driver));
2159 	strscpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
2160 	usb_make_path(udev, cap->bus_info, sizeof(cap->bus_info));
2161 
2162 	cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_READWRITE |
2163 			    V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
2164 	if (dev->int_audio_type != EM28XX_INT_AUDIO_NONE)
2165 		cap->capabilities |= V4L2_CAP_AUDIO;
2166 	if (dev->tuner_type != TUNER_ABSENT || em28xx_analogtv_supported(dev))
2167 		cap->capabilities |= V4L2_CAP_TUNER;
2168 	if (video_is_registered(&v4l2->vbi_dev))
2169 		cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2170 	if (video_is_registered(&v4l2->radio_dev))
2171 		cap->capabilities |= V4L2_CAP_RADIO;
2172 	return 0;
2173 }
2174 
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)2175 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
2176 				   struct v4l2_fmtdesc *f)
2177 {
2178 	if (unlikely(f->index >= ARRAY_SIZE(format)))
2179 		return -EINVAL;
2180 
2181 	f->pixelformat = format[f->index].fourcc;
2182 
2183 	return 0;
2184 }
2185 
vidioc_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)2186 static int vidioc_enum_framesizes(struct file *file, void *priv,
2187 				  struct v4l2_frmsizeenum *fsize)
2188 {
2189 	struct em28xx         *dev = video_drvdata(file);
2190 	struct em28xx_fmt     *fmt;
2191 	unsigned int	      maxw = norm_maxw(dev);
2192 	unsigned int	      maxh = norm_maxh(dev);
2193 
2194 	fmt = format_by_fourcc(fsize->pixel_format);
2195 	if (!fmt) {
2196 		em28xx_videodbg("Fourcc format (%08x) invalid.\n",
2197 				fsize->pixel_format);
2198 		return -EINVAL;
2199 	}
2200 
2201 	if (dev->board.is_em2800) {
2202 		if (fsize->index > 1)
2203 			return -EINVAL;
2204 		fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
2205 		fsize->discrete.width = maxw / (1 + fsize->index);
2206 		fsize->discrete.height = maxh / (1 + fsize->index);
2207 		return 0;
2208 	}
2209 
2210 	if (fsize->index != 0)
2211 		return -EINVAL;
2212 
2213 	/* Report a continuous range */
2214 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
2215 	scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX,
2216 		      &fsize->stepwise.min_width, &fsize->stepwise.min_height);
2217 	if (fsize->stepwise.min_width < 48)
2218 		fsize->stepwise.min_width = 48;
2219 	if (fsize->stepwise.min_height < 38)
2220 		fsize->stepwise.min_height = 38;
2221 	fsize->stepwise.max_width = maxw;
2222 	fsize->stepwise.max_height = maxh;
2223 	fsize->stepwise.step_width = 1;
2224 	fsize->stepwise.step_height = 1;
2225 	return 0;
2226 }
2227 
2228 /* RAW VBI ioctls */
2229 
vidioc_g_fmt_vbi_cap(struct file * file,void * priv,struct v4l2_format * format)2230 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
2231 				struct v4l2_format *format)
2232 {
2233 	struct em28xx         *dev  = video_drvdata(file);
2234 	struct em28xx_v4l2    *v4l2 = dev->v4l2;
2235 
2236 	format->fmt.vbi.samples_per_line = v4l2->vbi_width;
2237 	format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
2238 	format->fmt.vbi.offset = 0;
2239 	format->fmt.vbi.flags = 0;
2240 	format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
2241 	format->fmt.vbi.count[0] = v4l2->vbi_height;
2242 	format->fmt.vbi.count[1] = v4l2->vbi_height;
2243 	memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
2244 
2245 	/* Varies by video standard (NTSC, PAL, etc.) */
2246 	if (v4l2->norm & V4L2_STD_525_60) {
2247 		/* NTSC */
2248 		format->fmt.vbi.start[0] = 10;
2249 		format->fmt.vbi.start[1] = 273;
2250 	} else if (v4l2->norm & V4L2_STD_625_50) {
2251 		/* PAL */
2252 		format->fmt.vbi.start[0] = 6;
2253 		format->fmt.vbi.start[1] = 318;
2254 	}
2255 
2256 	return 0;
2257 }
2258 
2259 /*
2260  * RADIO ESPECIFIC IOCTLS
2261  */
2262 
radio_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)2263 static int radio_g_tuner(struct file *file, void *priv,
2264 			 struct v4l2_tuner *t)
2265 {
2266 	struct em28xx *dev = video_drvdata(file);
2267 
2268 	if (unlikely(t->index > 0))
2269 		return -EINVAL;
2270 
2271 	strscpy(t->name, "Radio", sizeof(t->name));
2272 
2273 	v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
2274 
2275 	return 0;
2276 }
2277 
radio_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)2278 static int radio_s_tuner(struct file *file, void *priv,
2279 			 const struct v4l2_tuner *t)
2280 {
2281 	struct em28xx *dev = video_drvdata(file);
2282 
2283 	if (t->index != 0)
2284 		return -EINVAL;
2285 
2286 	v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
2287 
2288 	return 0;
2289 }
2290 
2291 /*
2292  * em28xx_free_v4l2() - v4l2_device release callback
2293  *
2294  * @v4l2_dev: pointer to struct v4l2_device embedded in struct em28xx_v4l2
2295  *
2296  * Called by the v4l2 core when the last reference to the v4l2_device is
2297  * released. At this point no userspace file handle nor video_device node
2298  * keeps the v4l2 instance alive anymore, so it is safe to release all
2299  * v4l2-related resources and drop the em28xx device reference taken when
2300  * the v4l2 extension was initialized.
2301  */
em28xx_free_v4l2(struct v4l2_device * v4l2_dev)2302 static void em28xx_free_v4l2(struct v4l2_device *v4l2_dev)
2303 {
2304 	struct em28xx_v4l2 *v4l2 =
2305 		container_of(v4l2_dev, struct em28xx_v4l2, v4l2_dev);
2306 	struct em28xx *dev = v4l2->dev;
2307 
2308 	v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
2309 	v4l2_device_unregister(v4l2_dev);
2310 	dev->v4l2 = NULL;
2311 	kfree(v4l2);
2312 	kref_put(&dev->ref, em28xx_free_device);
2313 }
2314 
2315 /*
2316  * em28xx_v4l2_open()
2317  * inits the device and starts isoc transfer
2318  */
em28xx_v4l2_open(struct file * filp)2319 static int em28xx_v4l2_open(struct file *filp)
2320 {
2321 	struct video_device *vdev = video_devdata(filp);
2322 	struct em28xx *dev = video_drvdata(filp);
2323 	struct em28xx_v4l2 *v4l2;
2324 	enum v4l2_buf_type fh_type = 0;
2325 	int ret;
2326 
2327 	switch (vdev->vfl_type) {
2328 	case VFL_TYPE_VIDEO:
2329 		fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2330 		break;
2331 	case VFL_TYPE_VBI:
2332 		fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2333 		break;
2334 	case VFL_TYPE_RADIO:
2335 		break;
2336 	default:
2337 		return -EINVAL;
2338 	}
2339 
2340 	if (mutex_lock_interruptible(&dev->lock))
2341 		return -ERESTARTSYS;
2342 
2343 	v4l2 = dev->v4l2;
2344 	if (!v4l2) {
2345 		mutex_unlock(&dev->lock);
2346 		return -ENODEV;
2347 	}
2348 
2349 	em28xx_videodbg("open dev=%s type=%s\n",
2350 			video_device_node_name(vdev), v4l2_type_names[fh_type]);
2351 
2352 	ret = v4l2_fh_open(filp);
2353 	if (ret) {
2354 		dev_err(&dev->intf->dev,
2355 			"%s: v4l2_fh_open() returned error %d\n",
2356 		       __func__, ret);
2357 		mutex_unlock(&dev->lock);
2358 		return ret;
2359 	}
2360 
2361 	if (v4l2_fh_is_singular_file(filp)) {
2362 		em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
2363 
2364 		if (vdev->vfl_type != VFL_TYPE_RADIO)
2365 			em28xx_resolution_set(dev);
2366 
2367 		/*
2368 		 * Needed, since GPIO might have disabled power
2369 		 * of some i2c devices
2370 		 */
2371 		em28xx_wake_i2c(dev);
2372 	}
2373 
2374 	if (vdev->vfl_type == VFL_TYPE_RADIO) {
2375 		em28xx_videodbg("video_open: setting radio device\n");
2376 		v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_radio);
2377 	}
2378 
2379 	mutex_unlock(&dev->lock);
2380 
2381 	return 0;
2382 }
2383 
2384 /*
2385  * em28xx_v4l2_fini()
2386  * unregisters the v4l2,i2c and usb devices
2387  * called when the device gets disconnected or at module unload
2388  */
em28xx_v4l2_fini(struct em28xx * dev)2389 static int em28xx_v4l2_fini(struct em28xx *dev)
2390 {
2391 	struct em28xx_v4l2 *v4l2 = dev->v4l2;
2392 
2393 	if (dev->is_audio_only) {
2394 		/* Shouldn't initialize IR for this interface */
2395 		return 0;
2396 	}
2397 
2398 	if (!dev->has_video) {
2399 		/* This device does not support the v4l2 extension */
2400 		return 0;
2401 	}
2402 
2403 	if (!v4l2)
2404 		return 0;
2405 
2406 	dev_info(&dev->intf->dev, "Closing video extension\n");
2407 
2408 	mutex_lock(&dev->lock);
2409 
2410 	v4l2_device_disconnect(&v4l2->v4l2_dev);
2411 
2412 	em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
2413 
2414 	em28xx_v4l2_media_release(dev);
2415 
2416 	if (video_is_registered(&v4l2->radio_dev)) {
2417 		dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
2418 			 video_device_node_name(&v4l2->radio_dev));
2419 		video_unregister_device(&v4l2->radio_dev);
2420 	}
2421 	if (video_is_registered(&v4l2->vbi_dev)) {
2422 		dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
2423 			 video_device_node_name(&v4l2->vbi_dev));
2424 		vb2_video_unregister_device(&v4l2->vbi_dev);
2425 	}
2426 	if (video_is_registered(&v4l2->vdev)) {
2427 		dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
2428 			 video_device_node_name(&v4l2->vdev));
2429 		vb2_video_unregister_device(&v4l2->vdev);
2430 	}
2431 
2432 	mutex_unlock(&dev->lock);
2433 
2434 	/*
2435 	 * Drop the initial reference taken at v4l2_device_register() time.
2436 	 * The em28xx_free_v4l2() release callback will be invoked once all
2437 	 * userspace file handles to the video device nodes are closed.
2438 	 */
2439 	v4l2_device_put(&v4l2->v4l2_dev);
2440 
2441 	return 0;
2442 }
2443 
em28xx_v4l2_suspend(struct em28xx * dev)2444 static int em28xx_v4l2_suspend(struct em28xx *dev)
2445 {
2446 	if (dev->is_audio_only)
2447 		return 0;
2448 
2449 	if (!dev->has_video)
2450 		return 0;
2451 
2452 	dev_info(&dev->intf->dev, "Suspending video extension\n");
2453 	em28xx_stop_urbs(dev);
2454 	return 0;
2455 }
2456 
em28xx_v4l2_resume(struct em28xx * dev)2457 static int em28xx_v4l2_resume(struct em28xx *dev)
2458 {
2459 	if (dev->is_audio_only)
2460 		return 0;
2461 
2462 	if (!dev->has_video)
2463 		return 0;
2464 
2465 	dev_info(&dev->intf->dev, "Resuming video extension\n");
2466 	/* what do we do here */
2467 	return 0;
2468 }
2469 
2470 /*
2471  * em28xx_v4l2_close()
2472  * stops streaming and deallocates all resources allocated by the v4l2
2473  * calls and ioctls
2474  */
em28xx_v4l2_close(struct file * filp)2475 static int em28xx_v4l2_close(struct file *filp)
2476 {
2477 	struct em28xx         *dev  = video_drvdata(filp);
2478 	struct em28xx_v4l2    *v4l2 = dev->v4l2;
2479 	struct usb_device *udev = interface_to_usbdev(dev->intf);
2480 	int              err;
2481 	bool last_user;
2482 
2483 	mutex_lock(&dev->lock);
2484 	last_user = v4l2_fh_is_singular_file(filp);
2485 	_vb2_fop_release(filp, NULL);
2486 
2487 	if (last_user) {
2488 		/* No sense to try to write to the device */
2489 		if (dev->disconnected)
2490 			goto exit;
2491 
2492 		/* Save some power by putting tuner to sleep */
2493 		v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, standby);
2494 
2495 		/* do this before setting alternate! */
2496 		em28xx_set_mode(dev, EM28XX_SUSPEND);
2497 
2498 		/* set alternate 0 */
2499 		dev->alt = 0;
2500 		em28xx_videodbg("setting alternate 0\n");
2501 		err = usb_set_interface(udev, 0, 0);
2502 		if (err < 0) {
2503 			dev_err(&dev->intf->dev,
2504 				"cannot change alternate number to 0 (error=%i)\n",
2505 				err);
2506 		}
2507 	}
2508 
2509 exit:
2510 	mutex_unlock(&dev->lock);
2511 
2512 	return 0;
2513 }
2514 
2515 static const struct v4l2_file_operations em28xx_v4l_fops = {
2516 	.owner         = THIS_MODULE,
2517 	.open          = em28xx_v4l2_open,
2518 	.release       = em28xx_v4l2_close,
2519 	.read          = vb2_fop_read,
2520 	.poll          = vb2_fop_poll,
2521 	.mmap          = vb2_fop_mmap,
2522 	.unlocked_ioctl = video_ioctl2,
2523 };
2524 
2525 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2526 	.vidioc_querycap            = vidioc_querycap,
2527 	.vidioc_enum_fmt_vid_cap    = vidioc_enum_fmt_vid_cap,
2528 	.vidioc_g_fmt_vid_cap       = vidioc_g_fmt_vid_cap,
2529 	.vidioc_try_fmt_vid_cap     = vidioc_try_fmt_vid_cap,
2530 	.vidioc_s_fmt_vid_cap       = vidioc_s_fmt_vid_cap,
2531 	.vidioc_g_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
2532 	.vidioc_try_fmt_vbi_cap     = vidioc_g_fmt_vbi_cap,
2533 	.vidioc_s_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
2534 	.vidioc_enum_framesizes     = vidioc_enum_framesizes,
2535 	.vidioc_enumaudio           = vidioc_enumaudio,
2536 	.vidioc_g_audio             = vidioc_g_audio,
2537 	.vidioc_s_audio             = vidioc_s_audio,
2538 
2539 	.vidioc_reqbufs             = vb2_ioctl_reqbufs,
2540 	.vidioc_create_bufs         = vb2_ioctl_create_bufs,
2541 	.vidioc_prepare_buf         = vb2_ioctl_prepare_buf,
2542 	.vidioc_querybuf            = vb2_ioctl_querybuf,
2543 	.vidioc_qbuf                = vb2_ioctl_qbuf,
2544 	.vidioc_dqbuf               = vb2_ioctl_dqbuf,
2545 
2546 	.vidioc_g_std               = vidioc_g_std,
2547 	.vidioc_querystd            = vidioc_querystd,
2548 	.vidioc_s_std               = vidioc_s_std,
2549 	.vidioc_g_parm		    = vidioc_g_parm,
2550 	.vidioc_s_parm		    = vidioc_s_parm,
2551 	.vidioc_enum_input          = vidioc_enum_input,
2552 	.vidioc_g_input             = vidioc_g_input,
2553 	.vidioc_s_input             = vidioc_s_input,
2554 	.vidioc_streamon            = vb2_ioctl_streamon,
2555 	.vidioc_streamoff           = vb2_ioctl_streamoff,
2556 	.vidioc_g_tuner             = vidioc_g_tuner,
2557 	.vidioc_s_tuner             = vidioc_s_tuner,
2558 	.vidioc_g_frequency         = vidioc_g_frequency,
2559 	.vidioc_s_frequency         = vidioc_s_frequency,
2560 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2561 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2562 #ifdef CONFIG_VIDEO_ADV_DEBUG
2563 	.vidioc_g_chip_info         = vidioc_g_chip_info,
2564 	.vidioc_g_register          = vidioc_g_register,
2565 	.vidioc_s_register          = vidioc_s_register,
2566 #endif
2567 };
2568 
2569 static const struct video_device em28xx_video_template = {
2570 	.fops		= &em28xx_v4l_fops,
2571 	.ioctl_ops	= &video_ioctl_ops,
2572 	.release	= video_device_release_empty,
2573 	.tvnorms	= V4L2_STD_ALL,
2574 };
2575 
2576 static const struct v4l2_file_operations radio_fops = {
2577 	.owner         = THIS_MODULE,
2578 	.open          = em28xx_v4l2_open,
2579 	.release       = em28xx_v4l2_close,
2580 	.unlocked_ioctl = video_ioctl2,
2581 };
2582 
2583 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2584 	.vidioc_querycap      = vidioc_querycap,
2585 	.vidioc_g_tuner       = radio_g_tuner,
2586 	.vidioc_s_tuner       = radio_s_tuner,
2587 	.vidioc_g_frequency   = vidioc_g_frequency,
2588 	.vidioc_s_frequency   = vidioc_s_frequency,
2589 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2590 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2591 #ifdef CONFIG_VIDEO_ADV_DEBUG
2592 	.vidioc_g_chip_info   = vidioc_g_chip_info,
2593 	.vidioc_g_register    = vidioc_g_register,
2594 	.vidioc_s_register    = vidioc_s_register,
2595 #endif
2596 };
2597 
2598 static struct video_device em28xx_radio_template = {
2599 	.fops		= &radio_fops,
2600 	.ioctl_ops	= &radio_ioctl_ops,
2601 	.release	= video_device_release_empty,
2602 };
2603 
2604 /* I2C possible address to saa7115, tvp5150, msp3400, tvaudio */
2605 static unsigned short saa711x_addrs[] = {
2606 	0x4a >> 1, 0x48 >> 1,   /* SAA7111, SAA7111A and SAA7113 */
2607 	0x42 >> 1, 0x40 >> 1,   /* SAA7114, SAA7115 and SAA7118 */
2608 	I2C_CLIENT_END };
2609 
2610 static unsigned short tvp5150_addrs[] = {
2611 	0xb8 >> 1,
2612 	0xba >> 1,
2613 	I2C_CLIENT_END
2614 };
2615 
2616 static unsigned short msp3400_addrs[] = {
2617 	0x80 >> 1,
2618 	0x88 >> 1,
2619 	I2C_CLIENT_END
2620 };
2621 
2622 /******************************** usb interface ******************************/
2623 
em28xx_vdev_init(struct em28xx * dev,struct video_device * vfd,const struct video_device * template,const char * type_name)2624 static void em28xx_vdev_init(struct em28xx *dev,
2625 			     struct video_device *vfd,
2626 			     const struct video_device *template,
2627 			     const char *type_name)
2628 {
2629 	*vfd		= *template;
2630 	vfd->v4l2_dev	= &dev->v4l2->v4l2_dev;
2631 	vfd->lock	= &dev->lock;
2632 	if (dev->is_webcam)
2633 		vfd->tvnorms = 0;
2634 
2635 	snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2636 		 dev_name(&dev->intf->dev), type_name);
2637 
2638 	video_set_drvdata(vfd, dev);
2639 }
2640 
em28xx_tuner_setup(struct em28xx * dev,unsigned short tuner_addr)2641 static void em28xx_tuner_setup(struct em28xx *dev, unsigned short tuner_addr)
2642 {
2643 	struct em28xx_v4l2      *v4l2 = dev->v4l2;
2644 	struct v4l2_device      *v4l2_dev = &v4l2->v4l2_dev;
2645 	struct tuner_setup      tun_setup;
2646 	struct v4l2_frequency   f;
2647 
2648 	memset(&tun_setup, 0, sizeof(tun_setup));
2649 
2650 	tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
2651 	tun_setup.tuner_callback = em28xx_tuner_callback;
2652 
2653 	if (dev->board.radio.type) {
2654 		tun_setup.type = dev->board.radio.type;
2655 		tun_setup.addr = dev->board.radio_addr;
2656 
2657 		v4l2_device_call_all(v4l2_dev,
2658 				     0, tuner, s_type_addr, &tun_setup);
2659 	}
2660 
2661 	if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type) {
2662 		tun_setup.type   = dev->tuner_type;
2663 		tun_setup.addr   = tuner_addr;
2664 
2665 		v4l2_device_call_all(v4l2_dev,
2666 				     0, tuner, s_type_addr, &tun_setup);
2667 	}
2668 
2669 	if (dev->board.tda9887_conf) {
2670 		struct v4l2_priv_tun_config tda9887_cfg;
2671 
2672 		tda9887_cfg.tuner = TUNER_TDA9887;
2673 		tda9887_cfg.priv = &dev->board.tda9887_conf;
2674 
2675 		v4l2_device_call_all(v4l2_dev,
2676 				     0, tuner, s_config, &tda9887_cfg);
2677 	}
2678 
2679 	if (dev->tuner_type == TUNER_XC2028) {
2680 		struct v4l2_priv_tun_config  xc2028_cfg;
2681 		struct xc2028_ctrl           ctl;
2682 
2683 		memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
2684 		memset(&ctl, 0, sizeof(ctl));
2685 
2686 		em28xx_setup_xc3028(dev, &ctl);
2687 
2688 		xc2028_cfg.tuner = TUNER_XC2028;
2689 		xc2028_cfg.priv  = &ctl;
2690 
2691 		v4l2_device_call_all(v4l2_dev, 0, tuner, s_config, &xc2028_cfg);
2692 	}
2693 
2694 	/* configure tuner */
2695 	f.tuner = 0;
2696 	f.type = V4L2_TUNER_ANALOG_TV;
2697 	f.frequency = 9076;     /* just a magic number */
2698 	v4l2->frequency = f.frequency;
2699 	v4l2_device_call_all(v4l2_dev, 0, tuner, s_frequency, &f);
2700 }
2701 
em28xx_v4l2_init(struct em28xx * dev)2702 static int em28xx_v4l2_init(struct em28xx *dev)
2703 {
2704 	u8 val;
2705 	int ret;
2706 	unsigned int maxw;
2707 	struct v4l2_ctrl_handler *hdl;
2708 	struct em28xx_v4l2 *v4l2;
2709 
2710 	if (dev->is_audio_only) {
2711 		/* Shouldn't initialize IR for this interface */
2712 		return 0;
2713 	}
2714 
2715 	if (!dev->has_video) {
2716 		/* This device does not support the v4l2 extension */
2717 		return 0;
2718 	}
2719 
2720 	dev_info(&dev->intf->dev, "Registering V4L2 extension\n");
2721 
2722 	mutex_lock(&dev->lock);
2723 
2724 	v4l2 = kzalloc_obj(*v4l2);
2725 	if (!v4l2) {
2726 		mutex_unlock(&dev->lock);
2727 		return -ENOMEM;
2728 	}
2729 	v4l2->dev = dev;
2730 	dev->v4l2 = v4l2;
2731 
2732 #ifdef CONFIG_MEDIA_CONTROLLER
2733 	v4l2->v4l2_dev.mdev = dev->media_dev;
2734 #endif
2735 	ret = v4l2_device_register(&dev->intf->dev, &v4l2->v4l2_dev);
2736 	if (ret < 0) {
2737 		dev_err(&dev->intf->dev,
2738 			"Call to v4l2_device_register() failed!\n");
2739 		dev->v4l2 = NULL;
2740 		kfree(v4l2);
2741 		mutex_unlock(&dev->lock);
2742 		return ret;
2743 	}
2744 
2745 	/*
2746 	 * From this point on, em28xx_free_v4l2() will be used to release
2747 	 * v4l2-related resources when the v4l2_device refcount reaches
2748 	 * zero. Take a reference to the em28xx device so that it cannot
2749 	 * be freed before the v4l2 instance is released.
2750 	 */
2751 	v4l2->v4l2_dev.release = em28xx_free_v4l2;
2752 	kref_get(&dev->ref);
2753 
2754 	hdl = &v4l2->ctrl_handler;
2755 	v4l2_ctrl_handler_init(hdl, 9);
2756 	v4l2->v4l2_dev.ctrl_handler = hdl;
2757 
2758 	if (dev->is_webcam)
2759 		v4l2->progressive = true;
2760 
2761 	/*
2762 	 * Default format, used for tvp5150 or saa711x output formats
2763 	 */
2764 	v4l2->vinmode = EM28XX_VINMODE_YUV422_CbYCrY;
2765 	v4l2->vinctl  = EM28XX_VINCTRL_INTERLACED |
2766 			EM28XX_VINCTRL_CCIR656_ENABLE;
2767 
2768 	/* request some modules */
2769 
2770 	if (dev->has_msp34xx)
2771 		v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2772 				    &dev->i2c_adap[dev->def_i2c_bus],
2773 				    "msp3400", 0, msp3400_addrs);
2774 
2775 	if (dev->board.decoder == EM28XX_SAA711X)
2776 		v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2777 				    &dev->i2c_adap[dev->def_i2c_bus],
2778 				    "saa7115_auto", 0, saa711x_addrs);
2779 
2780 	if (dev->board.decoder == EM28XX_TVP5150)
2781 		v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2782 				    &dev->i2c_adap[dev->def_i2c_bus],
2783 				    "tvp5150", 0, tvp5150_addrs);
2784 
2785 	if (dev->board.adecoder == EM28XX_TVAUDIO)
2786 		v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2787 				    &dev->i2c_adap[dev->def_i2c_bus],
2788 				    "tvaudio", dev->board.tvaudio_addr, NULL);
2789 
2790 	/* Initialize tuner and camera */
2791 
2792 	if (dev->board.tuner_type != TUNER_ABSENT) {
2793 		unsigned short tuner_addr = dev->board.tuner_addr;
2794 		int has_demod = (dev->board.tda9887_conf & TDA9887_PRESENT);
2795 
2796 		if (dev->board.radio.type)
2797 			v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2798 					    &dev->i2c_adap[dev->def_i2c_bus],
2799 					    "tuner", dev->board.radio_addr,
2800 					    NULL);
2801 
2802 		if (has_demod)
2803 			v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2804 					    &dev->i2c_adap[dev->def_i2c_bus],
2805 					    "tuner", 0,
2806 					    v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
2807 		if (tuner_addr == 0) {
2808 			enum v4l2_i2c_tuner_type type =
2809 				has_demod ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
2810 			struct v4l2_subdev *sd;
2811 
2812 			sd = v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2813 						 &dev->i2c_adap[dev->def_i2c_bus],
2814 						 "tuner", 0,
2815 						 v4l2_i2c_tuner_addrs(type));
2816 
2817 			if (sd)
2818 				tuner_addr = v4l2_i2c_subdev_addr(sd);
2819 		} else {
2820 			v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2821 					    &dev->i2c_adap[dev->def_i2c_bus],
2822 					    "tuner", tuner_addr, NULL);
2823 		}
2824 
2825 		em28xx_tuner_setup(dev, tuner_addr);
2826 	}
2827 
2828 	if (dev->em28xx_sensor != EM28XX_NOSENSOR)
2829 		em28xx_init_camera(dev);
2830 
2831 	/* Configure audio */
2832 	ret = em28xx_audio_setup(dev);
2833 	if (ret < 0) {
2834 		dev_err(&dev->intf->dev,
2835 			"%s: Error while setting audio - error [%d]!\n",
2836 			__func__, ret);
2837 		goto unregister_dev;
2838 	}
2839 	if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
2840 		v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2841 				  V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2842 		v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2843 				  V4L2_CID_AUDIO_VOLUME, 0, 0x1f, 1, 0x1f);
2844 	} else {
2845 		/* install the em28xx notify callback */
2846 		v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_MUTE),
2847 				 em28xx_ctrl_notify, dev);
2848 		v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_VOLUME),
2849 				 em28xx_ctrl_notify, dev);
2850 	}
2851 
2852 	/* wake i2c devices */
2853 	em28xx_wake_i2c(dev);
2854 
2855 	/* init video dma queues */
2856 	INIT_LIST_HEAD(&dev->vidq.active);
2857 	INIT_LIST_HEAD(&dev->vbiq.active);
2858 
2859 	if (dev->has_msp34xx) {
2860 		/* Send a reset to other chips via gpio */
2861 		ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xf7);
2862 		if (ret < 0) {
2863 			dev_err(&dev->intf->dev,
2864 				"%s: em28xx_write_reg - msp34xx(1) failed! error [%d]\n",
2865 				__func__, ret);
2866 			goto unregister_dev;
2867 		}
2868 		usleep_range(10000, 11000);
2869 
2870 		ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xff);
2871 		if (ret < 0) {
2872 			dev_err(&dev->intf->dev,
2873 				"%s: em28xx_write_reg - msp34xx(2) failed! error [%d]\n",
2874 				__func__, ret);
2875 			goto unregister_dev;
2876 		}
2877 		usleep_range(10000, 11000);
2878 	}
2879 
2880 	/* set default norm */
2881 	v4l2->norm = -1;
2882 	v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
2883 	v4l2->interlaced_fieldmode = EM28XX_INTERLACED_DEFAULT;
2884 
2885 	/* Analog specific initialization */
2886 	v4l2->format = &format[0];
2887 
2888 	maxw = norm_maxw(dev);
2889 	/*
2890 	 * MaxPacketSize for em2800 is too small to capture at full resolution
2891 	 * use half of maxw as the scaler can only scale to 50%
2892 	 */
2893 	if (dev->board.is_em2800)
2894 		maxw /= 2;
2895 
2896 	em28xx_set_video_format(dev, format[0].fourcc,
2897 				maxw, norm_maxh(dev));
2898 
2899 	video_mux(dev, 0);
2900 
2901 	/* Audio defaults */
2902 	dev->mute = 1;
2903 	dev->volume = 0x1f;
2904 
2905 /*	em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2906 	val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2907 	em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2908 			 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2909 
2910 	em28xx_set_outfmt(dev);
2911 
2912 	/* Add image controls */
2913 
2914 	/*
2915 	 * NOTE: at this point, the subdevices are already registered, so
2916 	 * bridge controls are only added/enabled when no subdevice provides
2917 	 * them
2918 	 */
2919 	if (!v4l2_ctrl_find(hdl, V4L2_CID_CONTRAST))
2920 		v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2921 				  V4L2_CID_CONTRAST,
2922 				  0, 0x1f, 1, CONTRAST_DEFAULT);
2923 	if (!v4l2_ctrl_find(hdl, V4L2_CID_BRIGHTNESS))
2924 		v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2925 				  V4L2_CID_BRIGHTNESS,
2926 				  -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT);
2927 	if (!v4l2_ctrl_find(hdl, V4L2_CID_SATURATION))
2928 		v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2929 				  V4L2_CID_SATURATION,
2930 				  0, 0x1f, 1, SATURATION_DEFAULT);
2931 	if (!v4l2_ctrl_find(hdl, V4L2_CID_BLUE_BALANCE))
2932 		v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2933 				  V4L2_CID_BLUE_BALANCE,
2934 				  -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT);
2935 	if (!v4l2_ctrl_find(hdl, V4L2_CID_RED_BALANCE))
2936 		v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2937 				  V4L2_CID_RED_BALANCE,
2938 				  -0x30, 0x30, 1, RED_BALANCE_DEFAULT);
2939 	if (!v4l2_ctrl_find(hdl, V4L2_CID_SHARPNESS))
2940 		v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2941 				  V4L2_CID_SHARPNESS,
2942 				  0, 0x0f, 1, SHARPNESS_DEFAULT);
2943 
2944 	/* Reset image controls */
2945 	em28xx_colorlevels_set_default(dev);
2946 	v4l2_ctrl_handler_setup(hdl);
2947 	ret = hdl->error;
2948 	if (ret)
2949 		goto unregister_dev;
2950 
2951 	/* allocate and fill video video_device struct */
2952 	em28xx_vdev_init(dev, &v4l2->vdev, &em28xx_video_template, "video");
2953 	mutex_init(&v4l2->vb_queue_lock);
2954 	mutex_init(&v4l2->vb_vbi_queue_lock);
2955 	v4l2->vdev.queue = &v4l2->vb_vidq;
2956 	v4l2->vdev.queue->lock = &v4l2->vb_queue_lock;
2957 	v4l2->vdev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE |
2958 				 V4L2_CAP_STREAMING;
2959 	if (dev->int_audio_type != EM28XX_INT_AUDIO_NONE)
2960 		v4l2->vdev.device_caps |= V4L2_CAP_AUDIO;
2961 	if (dev->tuner_type != TUNER_ABSENT || em28xx_analogtv_supported(dev))
2962 		v4l2->vdev.device_caps |= V4L2_CAP_TUNER;
2963 
2964 	/* disable inapplicable ioctls */
2965 	if (dev->is_webcam) {
2966 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_QUERYSTD);
2967 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_STD);
2968 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_STD);
2969 	} else {
2970 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_PARM);
2971 	}
2972 	if ((v4l2->vdev.device_caps & V4L2_CAP_TUNER) == 0) {
2973 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_TUNER);
2974 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_TUNER);
2975 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_FREQUENCY);
2976 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_FREQUENCY);
2977 	}
2978 	if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
2979 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_AUDIO);
2980 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_AUDIO);
2981 	}
2982 
2983 	if (dev->chip_id == CHIP_ID_EM2828X || dev->board.decoder == EM28XX_BUILTIN)
2984 		v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_ENUM_FRAMESIZES);
2985 
2986 	/* register v4l2 video video_device */
2987 	ret = video_register_device(&v4l2->vdev, VFL_TYPE_VIDEO,
2988 				    video_nr[dev->devno]);
2989 	if (ret) {
2990 		dev_err(&dev->intf->dev,
2991 			"unable to register video device (error=%i).\n", ret);
2992 		goto unregister_dev;
2993 	}
2994 
2995 	/* Allocate and fill vbi video_device struct */
2996 	if (em28xx_vbi_supported(dev) == 1) {
2997 		em28xx_vdev_init(dev, &v4l2->vbi_dev, &em28xx_video_template,
2998 				 "vbi");
2999 
3000 		v4l2->vbi_dev.queue = &v4l2->vb_vbiq;
3001 		v4l2->vbi_dev.queue->lock = &v4l2->vb_vbi_queue_lock;
3002 		v4l2->vbi_dev.device_caps = V4L2_CAP_STREAMING |
3003 			V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
3004 		if ((v4l2->vdev.device_caps & V4L2_CAP_TUNER) == 0)
3005 			v4l2->vbi_dev.device_caps |= V4L2_CAP_TUNER;
3006 
3007 		/* disable inapplicable ioctls */
3008 		v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_PARM);
3009 		if ((v4l2->vbi_dev.device_caps & V4L2_CAP_TUNER) == 0) {
3010 			v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_TUNER);
3011 			v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_TUNER);
3012 			v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_FREQUENCY);
3013 			v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_FREQUENCY);
3014 		}
3015 		if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
3016 			v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_AUDIO);
3017 			v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_AUDIO);
3018 		}
3019 
3020 		/* register v4l2 vbi video_device */
3021 		ret = video_register_device(&v4l2->vbi_dev, VFL_TYPE_VBI,
3022 					    vbi_nr[dev->devno]);
3023 		if (ret < 0) {
3024 			dev_err(&dev->intf->dev,
3025 				"unable to register vbi device\n");
3026 			goto unregister_dev;
3027 		}
3028 	}
3029 
3030 	if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
3031 		em28xx_vdev_init(dev, &v4l2->radio_dev, &em28xx_radio_template,
3032 				 "radio");
3033 		v4l2->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
3034 		ret = video_register_device(&v4l2->radio_dev, VFL_TYPE_RADIO,
3035 					    radio_nr[dev->devno]);
3036 		if (ret < 0) {
3037 			dev_err(&dev->intf->dev,
3038 				"can't register radio device\n");
3039 			goto unregister_dev;
3040 		}
3041 		dev_info(&dev->intf->dev,
3042 			 "Registered radio device as %s\n",
3043 			 video_device_node_name(&v4l2->radio_dev));
3044 	}
3045 
3046 	/* Init entities at the Media Controller */
3047 	em28xx_v4l2_create_entities(dev);
3048 
3049 #ifdef CONFIG_MEDIA_CONTROLLER
3050 	ret = v4l2_mc_create_media_graph(dev->media_dev);
3051 	if (ret) {
3052 		dev_err(&dev->intf->dev,
3053 			"failed to create media graph\n");
3054 		em28xx_v4l2_media_release(dev);
3055 		goto unregister_dev;
3056 	}
3057 #endif
3058 
3059 	dev_info(&dev->intf->dev,
3060 		 "V4L2 video device registered as %s\n",
3061 		 video_device_node_name(&v4l2->vdev));
3062 
3063 	if (video_is_registered(&v4l2->vbi_dev))
3064 		dev_info(&dev->intf->dev,
3065 			 "V4L2 VBI device registered as %s\n",
3066 			 video_device_node_name(&v4l2->vbi_dev));
3067 
3068 	/* Save some power by putting tuner to sleep */
3069 	v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, standby);
3070 
3071 	/* initialize videobuf2 stuff */
3072 	em28xx_vb2_setup(dev);
3073 
3074 	dev_info(&dev->intf->dev,
3075 		 "V4L2 extension successfully initialized\n");
3076 
3077 	mutex_unlock(&dev->lock);
3078 	return 0;
3079 
3080 unregister_dev:
3081 	if (video_is_registered(&v4l2->radio_dev)) {
3082 		dev_info(&dev->intf->dev,
3083 			 "V4L2 device %s deregistered\n",
3084 			 video_device_node_name(&v4l2->radio_dev));
3085 		video_unregister_device(&v4l2->radio_dev);
3086 	}
3087 	if (video_is_registered(&v4l2->vbi_dev)) {
3088 		dev_info(&dev->intf->dev,
3089 			 "V4L2 device %s deregistered\n",
3090 			 video_device_node_name(&v4l2->vbi_dev));
3091 		vb2_video_unregister_device(&v4l2->vbi_dev);
3092 	}
3093 	if (video_is_registered(&v4l2->vdev)) {
3094 		dev_info(&dev->intf->dev,
3095 			 "V4L2 device %s deregistered\n",
3096 			 video_device_node_name(&v4l2->vdev));
3097 		vb2_video_unregister_device(&v4l2->vdev);
3098 	}
3099 
3100 	mutex_unlock(&dev->lock);
3101 
3102 	/*
3103 	 * Drop the initial reference. em28xx_free_v4l2() will be called
3104 	 * once the last video_device node release has decremented the
3105 	 * v4l2_device refcount to zero.
3106 	 */
3107 	v4l2_device_put(&v4l2->v4l2_dev);
3108 	return ret;
3109 }
3110 
3111 static struct em28xx_ops v4l2_ops = {
3112 	.id   = EM28XX_V4L2,
3113 	.name = "Em28xx v4l2 Extension",
3114 	.init = em28xx_v4l2_init,
3115 	.fini = em28xx_v4l2_fini,
3116 	.suspend = em28xx_v4l2_suspend,
3117 	.resume = em28xx_v4l2_resume,
3118 };
3119 
em28xx_video_register(void)3120 static int __init em28xx_video_register(void)
3121 {
3122 	return em28xx_register_extension(&v4l2_ops);
3123 }
3124 
em28xx_video_unregister(void)3125 static void __exit em28xx_video_unregister(void)
3126 {
3127 	em28xx_unregister_extension(&v4l2_ops);
3128 }
3129 
3130 module_init(em28xx_video_register);
3131 module_exit(em28xx_video_unregister);
3132