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