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