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