2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de>
9 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
11 Some parts based on SN9C10x PC Camera Controllers GPL driver made
12 by Luca Risolia <luca.risolia@studio.unibo.it>
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.
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.
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.
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/bitmap.h>
34 #include <linux/usb.h>
35 #include <linux/i2c.h>
37 #include <linux/mutex.h>
38 #include <linux/slab.h>
41 #include "em28xx-v4l.h"
42 #include <media/v4l2-common.h>
43 #include <media/v4l2-ioctl.h>
44 #include <media/v4l2-event.h>
45 #include <media/v4l2-clk.h>
46 #include <media/drv-intf/msp3400.h>
47 #include <media/tuner.h>
49 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
50 "Markus Rechberger <mrechberger@gmail.com>, " \
51 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
52 "Sascha Sommer <saschasommer@freenet.de>"
54 static unsigned int isoc_debug
;
55 module_param(isoc_debug
, int, 0644);
56 MODULE_PARM_DESC(isoc_debug
, "enable debug messages [isoc transfers]");
58 static unsigned int disable_vbi
;
59 module_param(disable_vbi
, int, 0644);
60 MODULE_PARM_DESC(disable_vbi
, "disable vbi support");
63 module_param(alt
, int, 0644);
64 MODULE_PARM_DESC(alt
, "alternate setting to use for video endpoint");
66 #define em28xx_videodbg(fmt, arg...) do {\
68 printk(KERN_INFO "%s %s :"fmt, \
69 dev->name, __func__ , ##arg); } while (0)
71 #define em28xx_isocdbg(fmt, arg...) \
74 printk(KERN_INFO "%s %s :"fmt, \
75 dev->name, __func__ , ##arg); \
79 MODULE_AUTHOR(DRIVER_AUTHOR
);
80 MODULE_DESCRIPTION(DRIVER_DESC
" - v4l2 interface");
81 MODULE_LICENSE("GPL");
82 MODULE_VERSION(EM28XX_VERSION
);
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)
92 static unsigned int video_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = -1U };
93 static unsigned int vbi_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = -1U };
94 static unsigned int radio_nr
[] = {[0 ... (EM28XX_MAXBOARDS
- 1)] = -1U };
96 module_param_array(video_nr
, int, NULL
, 0444);
97 module_param_array(vbi_nr
, int, NULL
, 0444);
98 module_param_array(radio_nr
, int, NULL
, 0444);
99 MODULE_PARM_DESC(video_nr
, "video device numbers");
100 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
101 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
103 static unsigned int video_debug
;
104 module_param(video_debug
, int, 0644);
105 MODULE_PARM_DESC(video_debug
, "enable debug messages [video]");
107 /* supported video standards */
108 static struct em28xx_fmt format
[] = {
110 .name
= "16 bpp YUY2, 4:2:2, packed",
111 .fourcc
= V4L2_PIX_FMT_YUYV
,
113 .reg
= EM28XX_OUTFMT_YUV422_Y0UY1V
,
115 .name
= "16 bpp RGB 565, LE",
116 .fourcc
= V4L2_PIX_FMT_RGB565
,
118 .reg
= EM28XX_OUTFMT_RGB_16_656
,
120 .name
= "8 bpp Bayer BGBG..GRGR",
121 .fourcc
= V4L2_PIX_FMT_SBGGR8
,
123 .reg
= EM28XX_OUTFMT_RGB_8_BGBG
,
125 .name
= "8 bpp Bayer GRGR..BGBG",
126 .fourcc
= V4L2_PIX_FMT_SGRBG8
,
128 .reg
= EM28XX_OUTFMT_RGB_8_GRGR
,
130 .name
= "8 bpp Bayer GBGB..RGRG",
131 .fourcc
= V4L2_PIX_FMT_SGBRG8
,
133 .reg
= EM28XX_OUTFMT_RGB_8_GBGB
,
135 .name
= "12 bpp YUV411",
136 .fourcc
= V4L2_PIX_FMT_YUV411P
,
138 .reg
= EM28XX_OUTFMT_YUV411
,
142 /*FIXME: maxw should be dependent of alt mode */
143 static inline unsigned int norm_maxw(struct em28xx
*dev
)
145 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
147 if (dev
->board
.is_webcam
)
148 return v4l2
->sensor_xres
;
150 if (dev
->board
.max_range_640_480
)
156 static inline unsigned int norm_maxh(struct em28xx
*dev
)
158 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
160 if (dev
->board
.is_webcam
)
161 return v4l2
->sensor_yres
;
163 if (dev
->board
.max_range_640_480
)
166 return (v4l2
->norm
& V4L2_STD_625_50
) ? 576 : 480;
169 static int em28xx_vbi_supported(struct em28xx
*dev
)
171 /* Modprobe option to manually disable */
172 if (disable_vbi
== 1)
175 if (dev
->board
.is_webcam
)
178 /* FIXME: check subdevices for VBI support */
180 if (dev
->chip_id
== CHIP_ID_EM2860
||
181 dev
->chip_id
== CHIP_ID_EM2883
)
184 /* Version of em28xx that does not support VBI */
190 * configure i2c attached devices
192 static void em28xx_wake_i2c(struct em28xx
*dev
)
194 struct v4l2_device
*v4l2_dev
= &dev
->v4l2
->v4l2_dev
;
196 v4l2_device_call_all(v4l2_dev
, 0, core
, reset
, 0);
197 v4l2_device_call_all(v4l2_dev
, 0, video
, s_routing
,
198 INPUT(dev
->ctl_input
)->vmux
, 0, 0);
201 static int em28xx_colorlevels_set_default(struct em28xx
*dev
)
203 em28xx_write_reg(dev
, EM28XX_R20_YGAIN
, CONTRAST_DEFAULT
);
204 em28xx_write_reg(dev
, EM28XX_R21_YOFFSET
, BRIGHTNESS_DEFAULT
);
205 em28xx_write_reg(dev
, EM28XX_R22_UVGAIN
, SATURATION_DEFAULT
);
206 em28xx_write_reg(dev
, EM28XX_R23_UOFFSET
, BLUE_BALANCE_DEFAULT
);
207 em28xx_write_reg(dev
, EM28XX_R24_VOFFSET
, RED_BALANCE_DEFAULT
);
208 em28xx_write_reg(dev
, EM28XX_R25_SHARPNESS
, SHARPNESS_DEFAULT
);
210 em28xx_write_reg(dev
, EM28XX_R14_GAMMA
, 0x20);
211 em28xx_write_reg(dev
, EM28XX_R15_RGAIN
, 0x20);
212 em28xx_write_reg(dev
, EM28XX_R16_GGAIN
, 0x20);
213 em28xx_write_reg(dev
, EM28XX_R17_BGAIN
, 0x20);
214 em28xx_write_reg(dev
, EM28XX_R18_ROFFSET
, 0x00);
215 em28xx_write_reg(dev
, EM28XX_R19_GOFFSET
, 0x00);
216 return em28xx_write_reg(dev
, EM28XX_R1A_BOFFSET
, 0x00);
219 static int em28xx_set_outfmt(struct em28xx
*dev
)
223 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
225 fmt
= v4l2
->format
->reg
;
229 * NOTE: it's not clear if this is really needed !
230 * The datasheets say bit 5 is a reserved bit and devices seem to work
231 * fine without it. But the Windows driver sets it for em2710/50+em28xx
232 * devices and we've always been setting it, too.
234 * em2765 (em25xx, em276x/7x/8x) devices do NOT work with this bit set,
235 * it's likely used for an additional (compressed ?) format there.
237 ret
= em28xx_write_reg(dev
, EM28XX_R27_OUTFMT
, fmt
);
241 ret
= em28xx_write_reg(dev
, EM28XX_R10_VINMODE
, v4l2
->vinmode
);
245 vinctrl
= v4l2
->vinctl
;
246 if (em28xx_vbi_supported(dev
) == 1) {
247 vinctrl
|= EM28XX_VINCTRL_VBI_RAW
;
248 em28xx_write_reg(dev
, EM28XX_R34_VBI_START_H
, 0x00);
249 em28xx_write_reg(dev
, EM28XX_R36_VBI_WIDTH
, v4l2
->vbi_width
/4);
250 em28xx_write_reg(dev
, EM28XX_R37_VBI_HEIGHT
, v4l2
->vbi_height
);
251 if (v4l2
->norm
& V4L2_STD_525_60
) {
253 em28xx_write_reg(dev
, EM28XX_R35_VBI_START_V
, 0x09);
254 } else if (v4l2
->norm
& V4L2_STD_625_50
) {
256 em28xx_write_reg(dev
, EM28XX_R35_VBI_START_V
, 0x07);
260 return em28xx_write_reg(dev
, EM28XX_R11_VINCTRL
, vinctrl
);
263 static int em28xx_accumulator_set(struct em28xx
*dev
, u8 xmin
, u8 xmax
,
266 em28xx_videodbg("em28xx Scale: (%d,%d)-(%d,%d)\n",
267 xmin
, ymin
, xmax
, ymax
);
269 em28xx_write_regs(dev
, EM28XX_R28_XMIN
, &xmin
, 1);
270 em28xx_write_regs(dev
, EM28XX_R29_XMAX
, &xmax
, 1);
271 em28xx_write_regs(dev
, EM28XX_R2A_YMIN
, &ymin
, 1);
272 return em28xx_write_regs(dev
, EM28XX_R2B_YMAX
, &ymax
, 1);
275 static void em28xx_capture_area_set(struct em28xx
*dev
, u8 hstart
, u8 vstart
,
276 u16 width
, u16 height
)
278 u8 cwidth
= width
>> 2;
279 u8 cheight
= height
>> 2;
280 u8 overflow
= (height
>> 9 & 0x02) | (width
>> 10 & 0x01);
281 /* NOTE: size limit: 2047x1023 = 2MPix */
283 em28xx_videodbg("capture area set to (%d,%d): %dx%d\n",
285 ((overflow
& 2) << 9 | cwidth
<< 2),
286 ((overflow
& 1) << 10 | cheight
<< 2));
288 em28xx_write_regs(dev
, EM28XX_R1C_HSTART
, &hstart
, 1);
289 em28xx_write_regs(dev
, EM28XX_R1D_VSTART
, &vstart
, 1);
290 em28xx_write_regs(dev
, EM28XX_R1E_CWIDTH
, &cwidth
, 1);
291 em28xx_write_regs(dev
, EM28XX_R1F_CHEIGHT
, &cheight
, 1);
292 em28xx_write_regs(dev
, EM28XX_R1B_OFLOW
, &overflow
, 1);
294 /* FIXME: function/meaning of these registers ? */
295 /* FIXME: align width+height to multiples of 4 ?! */
296 if (dev
->is_em25xx
) {
297 em28xx_write_reg(dev
, 0x34, width
>> 4);
298 em28xx_write_reg(dev
, 0x35, height
>> 4);
302 static int em28xx_scaler_set(struct em28xx
*dev
, u16 h
, u16 v
)
305 /* the em2800 scaler only supports scaling down to 50% */
307 if (dev
->board
.is_em2800
) {
308 mode
= (v
? 0x20 : 0x00) | (h
? 0x10 : 0x00);
314 em28xx_write_regs(dev
, EM28XX_R30_HSCALELOW
, (char *)buf
, 2);
318 em28xx_write_regs(dev
, EM28XX_R32_VSCALELOW
, (char *)buf
, 2);
319 /* it seems that both H and V scalers must be active
321 mode
= (h
|| v
) ? 0x30 : 0x00;
323 return em28xx_write_reg(dev
, EM28XX_R26_COMPR
, mode
);
326 /* FIXME: this only function read values from dev */
327 static int em28xx_resolution_set(struct em28xx
*dev
)
329 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
330 int width
= norm_maxw(dev
);
331 int height
= norm_maxh(dev
);
333 /* Properly setup VBI */
334 v4l2
->vbi_width
= 720;
335 if (v4l2
->norm
& V4L2_STD_525_60
)
336 v4l2
->vbi_height
= 12;
338 v4l2
->vbi_height
= 18;
340 em28xx_set_outfmt(dev
);
342 em28xx_accumulator_set(dev
, 1, (width
- 4) >> 2, 1, (height
- 4) >> 2);
344 /* If we don't set the start position to 2 in VBI mode, we end up
345 with line 20/21 being YUYV encoded instead of being in 8-bit
346 greyscale. The core of the issue is that line 21 (and line 23 for
347 PAL WSS) are inside of active video region, and as a result they
348 get the pixelformatting associated with that area. So by cropping
349 it out, we end up with the same format as the rest of the VBI
351 if (em28xx_vbi_supported(dev
) == 1)
352 em28xx_capture_area_set(dev
, 0, 2, width
, height
);
354 em28xx_capture_area_set(dev
, 0, 0, width
, height
);
356 return em28xx_scaler_set(dev
, v4l2
->hscale
, v4l2
->vscale
);
359 /* Set USB alternate setting for analog video */
360 static int em28xx_set_alternate(struct em28xx
*dev
)
362 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
365 unsigned int min_pkt_size
= v4l2
->width
* 2 + 4;
367 /* NOTE: for isoc transfers, only alt settings > 0 are allowed
368 bulk transfers seem to work only with alt=0 ! */
370 if ((alt
> 0) && (alt
< dev
->num_alt
)) {
371 em28xx_videodbg("alternate forced to %d\n", dev
->alt
);
375 if (dev
->analog_xfer_bulk
)
378 /* When image size is bigger than a certain value,
379 the frame size should be increased, otherwise, only
380 green screen will be received.
382 if (v4l2
->width
* 2 * v4l2
->height
> 720 * 240 * 2)
385 for (i
= 0; i
< dev
->num_alt
; i
++) {
386 /* stop when the selected alt setting offers enough bandwidth */
387 if (dev
->alt_max_pkt_size_isoc
[i
] >= min_pkt_size
) {
390 /* otherwise make sure that we end up with the maximum bandwidth
391 because the min_pkt_size equation might be wrong...
393 } else if (dev
->alt_max_pkt_size_isoc
[i
] >
394 dev
->alt_max_pkt_size_isoc
[dev
->alt
])
399 /* NOTE: for bulk transfers, we need to call usb_set_interface()
400 * even if the previous settings were the same. Otherwise streaming
401 * fails with all urbs having status = -EOVERFLOW ! */
402 if (dev
->analog_xfer_bulk
) {
403 dev
->max_pkt_size
= 512; /* USB 2.0 spec */
404 dev
->packet_multiplier
= EM28XX_BULK_PACKET_MULTIPLIER
;
406 em28xx_videodbg("minimum isoc packet size: %u (alt=%d)\n",
407 min_pkt_size
, dev
->alt
);
409 dev
->alt_max_pkt_size_isoc
[dev
->alt
];
410 dev
->packet_multiplier
= EM28XX_NUM_ISOC_PACKETS
;
412 em28xx_videodbg("setting alternate %d with wMaxPacketSize=%u\n",
413 dev
->alt
, dev
->max_pkt_size
);
414 errCode
= usb_set_interface(dev
->udev
, dev
->ifnum
, dev
->alt
);
416 em28xx_errdev("cannot change alternate number to %d (error=%i)\n",
423 /* ------------------------------------------------------------------
424 DMA and thread functions
425 ------------------------------------------------------------------*/
428 * Finish the current buffer
430 static inline void finish_buffer(struct em28xx
*dev
,
431 struct em28xx_buffer
*buf
)
433 em28xx_isocdbg("[%p/%d] wakeup\n", buf
, buf
->top_field
);
435 buf
->vb
.sequence
= dev
->v4l2
->field_count
++;
436 if (dev
->v4l2
->progressive
)
437 buf
->vb
.field
= V4L2_FIELD_NONE
;
439 buf
->vb
.field
= V4L2_FIELD_INTERLACED
;
440 buf
->vb
.vb2_buf
.timestamp
= ktime_get_ns();
442 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
446 * Copy picture data from USB buffer to videobuf buffer
448 static void em28xx_copy_video(struct em28xx
*dev
,
449 struct em28xx_buffer
*buf
,
450 unsigned char *usb_buf
,
453 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
454 void *fieldstart
, *startwrite
, *startread
;
455 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
456 int bytesperline
= v4l2
->width
<< 1;
458 if (buf
->pos
+ len
> buf
->length
)
459 len
= buf
->length
- buf
->pos
;
464 if (v4l2
->progressive
|| buf
->top_field
)
465 fieldstart
= buf
->vb_buf
;
466 else /* interlaced mode, even nr. of lines */
467 fieldstart
= buf
->vb_buf
+ bytesperline
;
469 linesdone
= buf
->pos
/ bytesperline
;
470 currlinedone
= buf
->pos
% bytesperline
;
472 if (v4l2
->progressive
)
473 offset
= linesdone
* bytesperline
+ currlinedone
;
475 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
477 startwrite
= fieldstart
+ offset
;
478 lencopy
= bytesperline
- currlinedone
;
479 lencopy
= lencopy
> remain
? remain
: lencopy
;
481 if ((char *)startwrite
+ lencopy
> (char *)buf
->vb_buf
+ buf
->length
) {
482 em28xx_isocdbg("Overflow of %zu bytes past buffer end (1)\n",
483 ((char *)startwrite
+ lencopy
) -
484 ((char *)buf
->vb_buf
+ buf
->length
));
485 remain
= (char *)buf
->vb_buf
+ buf
->length
-
491 memcpy(startwrite
, startread
, lencopy
);
496 if (v4l2
->progressive
)
497 startwrite
+= lencopy
;
499 startwrite
+= lencopy
+ bytesperline
;
500 startread
+= lencopy
;
501 if (bytesperline
> remain
)
504 lencopy
= bytesperline
;
506 if ((char *)startwrite
+ lencopy
> (char *)buf
->vb_buf
+
508 em28xx_isocdbg("Overflow of %zu bytes past buffer end"
510 ((char *)startwrite
+ lencopy
) -
511 ((char *)buf
->vb_buf
+ buf
->length
));
512 lencopy
= remain
= (char *)buf
->vb_buf
+ buf
->length
-
518 memcpy(startwrite
, startread
, lencopy
);
527 * Copy VBI data from USB buffer to videobuf buffer
529 static void em28xx_copy_vbi(struct em28xx
*dev
,
530 struct em28xx_buffer
*buf
,
531 unsigned char *usb_buf
,
536 if (buf
->pos
+ len
> buf
->length
)
537 len
= buf
->length
- buf
->pos
;
540 /* Make sure the bottom field populates the second half of the frame */
541 if (buf
->top_field
== 0)
542 offset
+= dev
->v4l2
->vbi_width
* dev
->v4l2
->vbi_height
;
544 memcpy(buf
->vb_buf
+ offset
, usb_buf
, len
);
548 static inline void print_err_status(struct em28xx
*dev
,
549 int packet
, int status
)
551 char *errmsg
= "Unknown";
555 errmsg
= "unlinked synchronuously";
558 errmsg
= "unlinked asynchronuously";
561 errmsg
= "Buffer error (overrun)";
564 errmsg
= "Stalled (device not responding)";
567 errmsg
= "Babble (bad cable?)";
570 errmsg
= "Bit-stuff error (bad cable?)";
573 errmsg
= "CRC/Timeout (could be anything)";
576 errmsg
= "Device does not respond";
580 em28xx_isocdbg("URB status %d [%s].\n", status
, errmsg
);
582 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
583 packet
, status
, errmsg
);
588 * get the next available buffer from dma queue
590 static inline struct em28xx_buffer
*get_next_buf(struct em28xx
*dev
,
591 struct em28xx_dmaqueue
*dma_q
)
593 struct em28xx_buffer
*buf
;
595 if (list_empty(&dma_q
->active
)) {
596 em28xx_isocdbg("No active queue to serve\n");
600 /* Get the next buffer */
601 buf
= list_entry(dma_q
->active
.next
, struct em28xx_buffer
, list
);
602 /* Cleans up buffer - Useful for testing for frame/URB loss */
603 list_del(&buf
->list
);
605 buf
->vb_buf
= buf
->mem
;
611 * Finish the current buffer if completed and prepare for the next field
613 static struct em28xx_buffer
*
614 finish_field_prepare_next(struct em28xx
*dev
,
615 struct em28xx_buffer
*buf
,
616 struct em28xx_dmaqueue
*dma_q
)
618 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
620 if (v4l2
->progressive
|| v4l2
->top_field
) { /* Brand new frame */
622 finish_buffer(dev
, buf
);
623 buf
= get_next_buf(dev
, dma_q
);
626 buf
->top_field
= v4l2
->top_field
;
634 * Process data packet according to the em2710/em2750/em28xx frame data format
636 static inline void process_frame_data_em28xx(struct em28xx
*dev
,
637 unsigned char *data_pkt
,
638 unsigned int data_len
)
640 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
641 struct em28xx_buffer
*buf
= dev
->usb_ctl
.vid_buf
;
642 struct em28xx_buffer
*vbi_buf
= dev
->usb_ctl
.vbi_buf
;
643 struct em28xx_dmaqueue
*dma_q
= &dev
->vidq
;
644 struct em28xx_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
646 /* capture type 0 = vbi start
647 capture type 1 = vbi in progress
648 capture type 2 = video start
649 capture type 3 = video in progress */
651 /* NOTE: Headers are always 4 bytes and
652 * never split across packets */
653 if (data_pkt
[0] == 0x88 && data_pkt
[1] == 0x88 &&
654 data_pkt
[2] == 0x88 && data_pkt
[3] == 0x88) {
658 } else if (data_pkt
[0] == 0x33 && data_pkt
[1] == 0x95) {
659 /* Field start (VBI mode) */
660 v4l2
->capture_type
= 0;
662 em28xx_isocdbg("VBI START HEADER !!!\n");
663 v4l2
->top_field
= !(data_pkt
[2] & 1);
666 } else if (data_pkt
[0] == 0x22 && data_pkt
[1] == 0x5a) {
667 /* Field start (VBI disabled) */
668 v4l2
->capture_type
= 2;
669 em28xx_isocdbg("VIDEO START HEADER !!!\n");
670 v4l2
->top_field
= !(data_pkt
[2] & 1);
675 /* NOTE: With bulk transfers, intermediate data packets
676 * have no continuation header */
678 if (v4l2
->capture_type
== 0) {
679 vbi_buf
= finish_field_prepare_next(dev
, vbi_buf
, vbi_dma_q
);
680 dev
->usb_ctl
.vbi_buf
= vbi_buf
;
681 v4l2
->capture_type
= 1;
684 if (v4l2
->capture_type
== 1) {
685 int vbi_size
= v4l2
->vbi_width
* v4l2
->vbi_height
;
686 int vbi_data_len
= ((v4l2
->vbi_read
+ data_len
) > vbi_size
) ?
687 (vbi_size
- v4l2
->vbi_read
) : data_len
;
691 em28xx_copy_vbi(dev
, vbi_buf
, data_pkt
, vbi_data_len
);
692 v4l2
->vbi_read
+= vbi_data_len
;
694 if (vbi_data_len
< data_len
) {
695 /* Continue with copying video data */
696 v4l2
->capture_type
= 2;
697 data_pkt
+= vbi_data_len
;
698 data_len
-= vbi_data_len
;
702 if (v4l2
->capture_type
== 2) {
703 buf
= finish_field_prepare_next(dev
, buf
, dma_q
);
704 dev
->usb_ctl
.vid_buf
= buf
;
705 v4l2
->capture_type
= 3;
708 if (v4l2
->capture_type
== 3 && buf
!= NULL
&& data_len
> 0)
709 em28xx_copy_video(dev
, buf
, data_pkt
, data_len
);
713 * Process data packet according to the em25xx/em276x/7x/8x frame data format
715 static inline void process_frame_data_em25xx(struct em28xx
*dev
,
716 unsigned char *data_pkt
,
717 unsigned int data_len
)
719 struct em28xx_buffer
*buf
= dev
->usb_ctl
.vid_buf
;
720 struct em28xx_dmaqueue
*dmaq
= &dev
->vidq
;
721 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
722 bool frame_end
= false;
724 /* Check for header */
725 /* NOTE: at least with bulk transfers, only the first packet
726 * has a header and has always set the FRAME_END bit */
727 if (data_len
>= 2) { /* em25xx header is only 2 bytes long */
728 if ((data_pkt
[0] == EM25XX_FRMDATAHDR_BYTE1
) &&
729 ((data_pkt
[1] & ~EM25XX_FRMDATAHDR_BYTE2_MASK
) == 0x00)) {
730 v4l2
->top_field
= !(data_pkt
[1] &
731 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID
);
732 frame_end
= data_pkt
[1] &
733 EM25XX_FRMDATAHDR_BYTE2_FRAME_END
;
738 /* Finish field and prepare next (BULK only) */
739 if (dev
->analog_xfer_bulk
&& frame_end
) {
740 buf
= finish_field_prepare_next(dev
, buf
, dmaq
);
741 dev
->usb_ctl
.vid_buf
= buf
;
743 /* NOTE: in ISOC mode when a new frame starts and buf==NULL,
744 * we COULD already prepare a buffer here to avoid skipping the
750 if (buf
!= NULL
&& data_len
> 0)
751 em28xx_copy_video(dev
, buf
, data_pkt
, data_len
);
753 /* Finish frame (ISOC only) => avoids lag of 1 frame */
754 if (!dev
->analog_xfer_bulk
&& frame_end
) {
755 buf
= finish_field_prepare_next(dev
, buf
, dmaq
);
756 dev
->usb_ctl
.vid_buf
= buf
;
759 /* NOTE: Tested with USB bulk transfers only !
760 * The wording in the datasheet suggests that isoc might work different.
761 * The current code assumes that with isoc transfers each packet has a
762 * header like with the other em28xx devices.
764 /* NOTE: Support for interlaced mode is pure theory. It has not been
765 * tested and it is unknown if these devices actually support it. */
766 /* NOTE: No VBI support yet (these chips likely do not support VBI). */
769 /* Processes and copies the URB data content (video and VBI data) */
770 static inline int em28xx_urb_data_copy(struct em28xx
*dev
, struct urb
*urb
)
772 int xfer_bulk
, num_packets
, i
;
773 unsigned char *usb_data_pkt
;
774 unsigned int usb_data_len
;
779 if (dev
->disconnected
)
783 print_err_status(dev
, -1, urb
->status
);
785 xfer_bulk
= usb_pipebulk(urb
->pipe
);
787 if (xfer_bulk
) /* bulk */
790 num_packets
= urb
->number_of_packets
;
792 for (i
= 0; i
< num_packets
; i
++) {
793 if (xfer_bulk
) { /* bulk */
794 usb_data_len
= urb
->actual_length
;
796 usb_data_pkt
= urb
->transfer_buffer
;
798 if (urb
->iso_frame_desc
[i
].status
< 0) {
799 print_err_status(dev
, i
,
800 urb
->iso_frame_desc
[i
].status
);
801 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
805 usb_data_len
= urb
->iso_frame_desc
[i
].actual_length
;
806 if (usb_data_len
> dev
->max_pkt_size
) {
807 em28xx_isocdbg("packet bigger than packet size");
811 usb_data_pkt
= urb
->transfer_buffer
+
812 urb
->iso_frame_desc
[i
].offset
;
815 if (usb_data_len
== 0) {
816 /* NOTE: happens very often with isoc transfers */
817 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
822 process_frame_data_em25xx(dev
,
823 usb_data_pkt
, usb_data_len
);
825 process_frame_data_em28xx(dev
,
826 usb_data_pkt
, usb_data_len
);
832 static int get_ressource(enum v4l2_buf_type f_type
)
835 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
836 return EM28XX_RESOURCE_VIDEO
;
837 case V4L2_BUF_TYPE_VBI_CAPTURE
:
838 return EM28XX_RESOURCE_VBI
;
844 /* Usage lock check functions */
845 static int res_get(struct em28xx
*dev
, enum v4l2_buf_type f_type
)
847 int res_type
= get_ressource(f_type
);
850 if (dev
->resources
& res_type
) {
851 /* no, someone else uses it */
855 /* it's free, grab it */
856 dev
->resources
|= res_type
;
857 em28xx_videodbg("res: get %d\n", res_type
);
861 static void res_free(struct em28xx
*dev
, enum v4l2_buf_type f_type
)
863 int res_type
= get_ressource(f_type
);
865 dev
->resources
&= ~res_type
;
866 em28xx_videodbg("res: put %d\n", res_type
);
869 static void em28xx_v4l2_media_release(struct em28xx
*dev
)
871 #ifdef CONFIG_MEDIA_CONTROLLER
874 for (i
= 0; i
< MAX_EM28XX_INPUT
; i
++) {
877 media_device_unregister_entity(&dev
->input_ent
[i
]);
883 * Media Controller helper functions
886 static int em28xx_enable_analog_tuner(struct em28xx
*dev
)
888 #ifdef CONFIG_MEDIA_CONTROLLER
889 struct media_device
*mdev
= dev
->media_dev
;
890 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
891 struct media_entity
*source
;
892 struct media_link
*link
, *found_link
= NULL
;
893 int ret
, active_links
= 0;
895 if (!mdev
|| !v4l2
->decoder
)
899 * This will find the tuner that is connected into the decoder.
900 * Technically, this is not 100% correct, as the device may be
901 * using an analog input instead of the tuner. However, as we can't
902 * do DVB streaming while the DMA engine is being used for V4L2,
903 * this should be enough for the actual needs.
905 list_for_each_entry(link
, &v4l2
->decoder
->links
, list
) {
906 if (link
->sink
->entity
== v4l2
->decoder
) {
908 if (link
->flags
& MEDIA_LNK_FL_ENABLED
)
914 if (active_links
== 1 || !found_link
)
917 source
= found_link
->source
->entity
;
918 list_for_each_entry(link
, &source
->links
, list
) {
919 struct media_entity
*sink
;
922 sink
= link
->sink
->entity
;
924 if (sink
== v4l2
->decoder
)
925 flags
= MEDIA_LNK_FL_ENABLED
;
927 ret
= media_entity_setup_link(link
, flags
);
929 pr_err("Couldn't change link %s->%s to %s. Error %d\n",
930 source
->name
, sink
->name
,
931 flags
? "enabled" : "disabled",
935 em28xx_videodbg("link %s->%s was %s\n",
936 source
->name
, sink
->name
,
937 flags
? "ENABLED" : "disabled");
943 static const char * const iname
[] = {
944 [EM28XX_VMUX_COMPOSITE
] = "Composite",
945 [EM28XX_VMUX_SVIDEO
] = "S-Video",
946 [EM28XX_VMUX_TELEVISION
] = "Television",
947 [EM28XX_RADIO
] = "Radio",
950 static void em28xx_v4l2_create_entities(struct em28xx
*dev
)
952 #if defined(CONFIG_MEDIA_CONTROLLER)
953 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
956 /* Initialize Video, VBI and Radio pads */
957 v4l2
->video_pad
.flags
= MEDIA_PAD_FL_SINK
;
958 ret
= media_entity_pads_init(&v4l2
->vdev
.entity
, 1, &v4l2
->video_pad
);
960 pr_err("failed to initialize video media entity!\n");
962 if (em28xx_vbi_supported(dev
)) {
963 v4l2
->vbi_pad
.flags
= MEDIA_PAD_FL_SINK
;
964 ret
= media_entity_pads_init(&v4l2
->vbi_dev
.entity
, 1,
967 pr_err("failed to initialize vbi media entity!\n");
970 /* Webcams don't have input connectors */
971 if (dev
->board
.is_webcam
)
974 /* Create entities for each input connector */
975 for (i
= 0; i
< MAX_EM28XX_INPUT
; i
++) {
976 struct media_entity
*ent
= &dev
->input_ent
[i
];
981 ent
->name
= iname
[INPUT(i
)->type
];
982 ent
->flags
= MEDIA_ENT_FL_CONNECTOR
;
983 dev
->input_pad
[i
].flags
= MEDIA_PAD_FL_SOURCE
;
985 switch (INPUT(i
)->type
) {
986 case EM28XX_VMUX_COMPOSITE
:
987 ent
->function
= MEDIA_ENT_F_CONN_COMPOSITE
;
989 case EM28XX_VMUX_SVIDEO
:
990 ent
->function
= MEDIA_ENT_F_CONN_SVIDEO
;
992 default: /* EM28XX_VMUX_TELEVISION or EM28XX_RADIO */
993 if (dev
->tuner_type
!= TUNER_ABSENT
)
994 ent
->function
= MEDIA_ENT_F_CONN_RF
;
998 ret
= media_entity_pads_init(ent
, 1, &dev
->input_pad
[i
]);
1000 pr_err("failed to initialize input pad[%d]!\n", i
);
1002 ret
= media_device_register_entity(dev
->media_dev
, ent
);
1004 pr_err("failed to register input entity %d!\n", i
);
1010 /* ------------------------------------------------------------------
1011 Videobuf2 operations
1012 ------------------------------------------------------------------*/
1014 static int queue_setup(struct vb2_queue
*vq
,
1015 unsigned int *nbuffers
, unsigned int *nplanes
,
1016 unsigned int sizes
[], struct device
*alloc_devs
[])
1018 struct em28xx
*dev
= vb2_get_drv_priv(vq
);
1019 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1020 unsigned long size
=
1021 (v4l2
->width
* v4l2
->height
* v4l2
->format
->depth
+ 7) >> 3;
1024 return sizes
[0] < size
? -EINVAL
: 0;
1028 em28xx_enable_analog_tuner(dev
);
1034 buffer_prepare(struct vb2_buffer
*vb
)
1036 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1037 struct em28xx
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
1038 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1041 em28xx_videodbg("%s, field=%d\n", __func__
, vbuf
->field
);
1043 size
= (v4l2
->width
* v4l2
->height
* v4l2
->format
->depth
+ 7) >> 3;
1045 if (vb2_plane_size(vb
, 0) < size
) {
1046 em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
1047 __func__
, vb2_plane_size(vb
, 0), size
);
1050 vb2_set_plane_payload(vb
, 0, size
);
1055 int em28xx_start_analog_streaming(struct vb2_queue
*vq
, unsigned int count
)
1057 struct em28xx
*dev
= vb2_get_drv_priv(vq
);
1058 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1059 struct v4l2_frequency f
;
1060 struct v4l2_fh
*owner
;
1063 em28xx_videodbg("%s\n", __func__
);
1065 /* Make sure streaming is not already in progress for this type
1066 of filehandle (e.g. video, vbi) */
1067 rc
= res_get(dev
, vq
->type
);
1071 if (v4l2
->streaming_users
== 0) {
1072 /* First active streaming user, so allocate all the URBs */
1074 /* Allocate the USB bandwidth */
1075 em28xx_set_alternate(dev
);
1077 /* Needed, since GPIO might have disabled power of
1080 em28xx_wake_i2c(dev
);
1082 v4l2
->capture_type
= -1;
1083 rc
= em28xx_init_usb_xfer(dev
, EM28XX_ANALOG_MODE
,
1084 dev
->analog_xfer_bulk
,
1087 dev
->packet_multiplier
,
1088 em28xx_urb_data_copy
);
1093 * djh: it's not clear whether this code is still needed. I'm
1094 * leaving it in here for now entirely out of concern for
1095 * backward compatibility (the old code did it)
1098 /* Ask tuner to go to analog or radio mode */
1099 memset(&f
, 0, sizeof(f
));
1100 f
.frequency
= v4l2
->frequency
;
1101 owner
= (struct v4l2_fh
*)vq
->owner
;
1102 if (owner
&& owner
->vdev
->vfl_type
== VFL_TYPE_RADIO
)
1103 f
.type
= V4L2_TUNER_RADIO
;
1105 f
.type
= V4L2_TUNER_ANALOG_TV
;
1106 v4l2_device_call_all(&v4l2
->v4l2_dev
,
1107 0, tuner
, s_frequency
, &f
);
1109 /* Enable video stream at TV decoder */
1110 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, video
, s_stream
, 1);
1113 v4l2
->streaming_users
++;
1118 static void em28xx_stop_streaming(struct vb2_queue
*vq
)
1120 struct em28xx
*dev
= vb2_get_drv_priv(vq
);
1121 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1122 struct em28xx_dmaqueue
*vidq
= &dev
->vidq
;
1123 unsigned long flags
= 0;
1125 em28xx_videodbg("%s\n", __func__
);
1127 res_free(dev
, vq
->type
);
1129 if (v4l2
->streaming_users
-- == 1) {
1130 /* Disable video stream at TV decoder */
1131 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, video
, s_stream
, 0);
1133 /* Last active user, so shutdown all the URBS */
1134 em28xx_uninit_usb_xfer(dev
, EM28XX_ANALOG_MODE
);
1137 spin_lock_irqsave(&dev
->slock
, flags
);
1138 if (dev
->usb_ctl
.vid_buf
!= NULL
) {
1139 vb2_buffer_done(&dev
->usb_ctl
.vid_buf
->vb
.vb2_buf
,
1140 VB2_BUF_STATE_ERROR
);
1141 dev
->usb_ctl
.vid_buf
= NULL
;
1143 while (!list_empty(&vidq
->active
)) {
1144 struct em28xx_buffer
*buf
;
1146 buf
= list_entry(vidq
->active
.next
, struct em28xx_buffer
, list
);
1147 list_del(&buf
->list
);
1148 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
1150 spin_unlock_irqrestore(&dev
->slock
, flags
);
1153 void em28xx_stop_vbi_streaming(struct vb2_queue
*vq
)
1155 struct em28xx
*dev
= vb2_get_drv_priv(vq
);
1156 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1157 struct em28xx_dmaqueue
*vbiq
= &dev
->vbiq
;
1158 unsigned long flags
= 0;
1160 em28xx_videodbg("%s\n", __func__
);
1162 res_free(dev
, vq
->type
);
1164 if (v4l2
->streaming_users
-- == 1) {
1165 /* Disable video stream at TV decoder */
1166 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, video
, s_stream
, 0);
1168 /* Last active user, so shutdown all the URBS */
1169 em28xx_uninit_usb_xfer(dev
, EM28XX_ANALOG_MODE
);
1172 spin_lock_irqsave(&dev
->slock
, flags
);
1173 if (dev
->usb_ctl
.vbi_buf
!= NULL
) {
1174 vb2_buffer_done(&dev
->usb_ctl
.vbi_buf
->vb
.vb2_buf
,
1175 VB2_BUF_STATE_ERROR
);
1176 dev
->usb_ctl
.vbi_buf
= NULL
;
1178 while (!list_empty(&vbiq
->active
)) {
1179 struct em28xx_buffer
*buf
;
1181 buf
= list_entry(vbiq
->active
.next
, struct em28xx_buffer
, list
);
1182 list_del(&buf
->list
);
1183 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
1185 spin_unlock_irqrestore(&dev
->slock
, flags
);
1189 buffer_queue(struct vb2_buffer
*vb
)
1191 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
1192 struct em28xx
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
1193 struct em28xx_buffer
*buf
=
1194 container_of(vbuf
, struct em28xx_buffer
, vb
);
1195 struct em28xx_dmaqueue
*vidq
= &dev
->vidq
;
1196 unsigned long flags
= 0;
1198 em28xx_videodbg("%s\n", __func__
);
1199 buf
->mem
= vb2_plane_vaddr(vb
, 0);
1200 buf
->length
= vb2_plane_size(vb
, 0);
1202 spin_lock_irqsave(&dev
->slock
, flags
);
1203 list_add_tail(&buf
->list
, &vidq
->active
);
1204 spin_unlock_irqrestore(&dev
->slock
, flags
);
1207 static struct vb2_ops em28xx_video_qops
= {
1208 .queue_setup
= queue_setup
,
1209 .buf_prepare
= buffer_prepare
,
1210 .buf_queue
= buffer_queue
,
1211 .start_streaming
= em28xx_start_analog_streaming
,
1212 .stop_streaming
= em28xx_stop_streaming
,
1213 .wait_prepare
= vb2_ops_wait_prepare
,
1214 .wait_finish
= vb2_ops_wait_finish
,
1217 static int em28xx_vb2_setup(struct em28xx
*dev
)
1220 struct vb2_queue
*q
;
1221 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1223 /* Setup Videobuf2 for Video capture */
1225 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1226 q
->io_modes
= VB2_READ
| VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
1227 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1229 q
->buf_struct_size
= sizeof(struct em28xx_buffer
);
1230 q
->ops
= &em28xx_video_qops
;
1231 q
->mem_ops
= &vb2_vmalloc_memops
;
1233 rc
= vb2_queue_init(q
);
1237 /* Setup Videobuf2 for VBI capture */
1239 q
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1240 q
->io_modes
= VB2_READ
| VB2_MMAP
| VB2_USERPTR
;
1241 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1243 q
->buf_struct_size
= sizeof(struct em28xx_buffer
);
1244 q
->ops
= &em28xx_vbi_qops
;
1245 q
->mem_ops
= &vb2_vmalloc_memops
;
1247 rc
= vb2_queue_init(q
);
1254 /********************* v4l2 interface **************************************/
1256 static void video_mux(struct em28xx
*dev
, int index
)
1258 struct v4l2_device
*v4l2_dev
= &dev
->v4l2
->v4l2_dev
;
1260 dev
->ctl_input
= index
;
1261 dev
->ctl_ainput
= INPUT(index
)->amux
;
1262 dev
->ctl_aoutput
= INPUT(index
)->aout
;
1264 if (!dev
->ctl_aoutput
)
1265 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
1267 v4l2_device_call_all(v4l2_dev
, 0, video
, s_routing
,
1268 INPUT(index
)->vmux
, 0, 0);
1270 if (dev
->board
.has_msp34xx
) {
1271 if (dev
->i2s_speed
) {
1272 v4l2_device_call_all(v4l2_dev
, 0, audio
,
1273 s_i2s_clock_freq
, dev
->i2s_speed
);
1275 /* Note: this is msp3400 specific */
1276 v4l2_device_call_all(v4l2_dev
, 0, audio
, s_routing
,
1278 MSP_OUTPUT(MSP_SC_IN_DSP_SCART1
), 0);
1281 if (dev
->board
.adecoder
!= EM28XX_NOADECODER
) {
1282 v4l2_device_call_all(v4l2_dev
, 0, audio
, s_routing
,
1283 dev
->ctl_ainput
, dev
->ctl_aoutput
, 0);
1286 em28xx_audio_analog_set(dev
);
1289 static void em28xx_ctrl_notify(struct v4l2_ctrl
*ctrl
, void *priv
)
1291 struct em28xx
*dev
= priv
;
1294 * In the case of non-AC97 volume controls, we still need
1295 * to do some setups at em28xx, in order to mute/unmute
1296 * and to adjust audio volume. However, the value ranges
1297 * should be checked by the corresponding V4L subdriver.
1300 case V4L2_CID_AUDIO_MUTE
:
1301 dev
->mute
= ctrl
->val
;
1302 em28xx_audio_analog_set(dev
);
1304 case V4L2_CID_AUDIO_VOLUME
:
1305 dev
->volume
= ctrl
->val
;
1306 em28xx_audio_analog_set(dev
);
1311 static int em28xx_s_ctrl(struct v4l2_ctrl
*ctrl
)
1313 struct em28xx_v4l2
*v4l2
=
1314 container_of(ctrl
->handler
, struct em28xx_v4l2
, ctrl_handler
);
1315 struct em28xx
*dev
= v4l2
->dev
;
1319 case V4L2_CID_AUDIO_MUTE
:
1320 dev
->mute
= ctrl
->val
;
1321 ret
= em28xx_audio_analog_set(dev
);
1323 case V4L2_CID_AUDIO_VOLUME
:
1324 dev
->volume
= ctrl
->val
;
1325 ret
= em28xx_audio_analog_set(dev
);
1327 case V4L2_CID_CONTRAST
:
1328 ret
= em28xx_write_reg(dev
, EM28XX_R20_YGAIN
, ctrl
->val
);
1330 case V4L2_CID_BRIGHTNESS
:
1331 ret
= em28xx_write_reg(dev
, EM28XX_R21_YOFFSET
, ctrl
->val
);
1333 case V4L2_CID_SATURATION
:
1334 ret
= em28xx_write_reg(dev
, EM28XX_R22_UVGAIN
, ctrl
->val
);
1336 case V4L2_CID_BLUE_BALANCE
:
1337 ret
= em28xx_write_reg(dev
, EM28XX_R23_UOFFSET
, ctrl
->val
);
1339 case V4L2_CID_RED_BALANCE
:
1340 ret
= em28xx_write_reg(dev
, EM28XX_R24_VOFFSET
, ctrl
->val
);
1342 case V4L2_CID_SHARPNESS
:
1343 ret
= em28xx_write_reg(dev
, EM28XX_R25_SHARPNESS
, ctrl
->val
);
1347 return (ret
< 0) ? ret
: 0;
1350 static const struct v4l2_ctrl_ops em28xx_ctrl_ops
= {
1351 .s_ctrl
= em28xx_s_ctrl
,
1354 static void size_to_scale(struct em28xx
*dev
,
1355 unsigned int width
, unsigned int height
,
1356 unsigned int *hscale
, unsigned int *vscale
)
1358 unsigned int maxw
= norm_maxw(dev
);
1359 unsigned int maxh
= norm_maxh(dev
);
1361 *hscale
= (((unsigned long)maxw
) << 12) / width
- 4096L;
1362 if (*hscale
> EM28XX_HVSCALE_MAX
)
1363 *hscale
= EM28XX_HVSCALE_MAX
;
1365 *vscale
= (((unsigned long)maxh
) << 12) / height
- 4096L;
1366 if (*vscale
> EM28XX_HVSCALE_MAX
)
1367 *vscale
= EM28XX_HVSCALE_MAX
;
1370 static void scale_to_size(struct em28xx
*dev
,
1371 unsigned int hscale
, unsigned int vscale
,
1372 unsigned int *width
, unsigned int *height
)
1374 unsigned int maxw
= norm_maxw(dev
);
1375 unsigned int maxh
= norm_maxh(dev
);
1377 *width
= (((unsigned long)maxw
) << 12) / (hscale
+ 4096L);
1378 *height
= (((unsigned long)maxh
) << 12) / (vscale
+ 4096L);
1380 /* Don't let width or height to be zero */
1387 /* ------------------------------------------------------------------
1388 IOCTL vidioc handling
1389 ------------------------------------------------------------------*/
1391 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1392 struct v4l2_format
*f
)
1394 struct em28xx
*dev
= video_drvdata(file
);
1395 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1397 f
->fmt
.pix
.width
= v4l2
->width
;
1398 f
->fmt
.pix
.height
= v4l2
->height
;
1399 f
->fmt
.pix
.pixelformat
= v4l2
->format
->fourcc
;
1400 f
->fmt
.pix
.bytesperline
= (v4l2
->width
* v4l2
->format
->depth
+ 7) >> 3;
1401 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* v4l2
->height
;
1402 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1404 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1405 if (v4l2
->progressive
)
1406 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1408 f
->fmt
.pix
.field
= v4l2
->interlaced_fieldmode
?
1409 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
1413 static struct em28xx_fmt
*format_by_fourcc(unsigned int fourcc
)
1417 for (i
= 0; i
< ARRAY_SIZE(format
); i
++)
1418 if (format
[i
].fourcc
== fourcc
)
1424 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1425 struct v4l2_format
*f
)
1427 struct em28xx
*dev
= video_drvdata(file
);
1428 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1429 unsigned int width
= f
->fmt
.pix
.width
;
1430 unsigned int height
= f
->fmt
.pix
.height
;
1431 unsigned int maxw
= norm_maxw(dev
);
1432 unsigned int maxh
= norm_maxh(dev
);
1433 unsigned int hscale
, vscale
;
1434 struct em28xx_fmt
*fmt
;
1436 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1438 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1439 f
->fmt
.pix
.pixelformat
);
1443 if (dev
->board
.is_em2800
) {
1444 /* the em2800 can only scale down to 50% */
1445 height
= height
> (3 * maxh
/ 4) ? maxh
: maxh
/ 2;
1446 width
= width
> (3 * maxw
/ 4) ? maxw
: maxw
/ 2;
1448 * MaxPacketSize for em2800 is too small to capture at full
1449 * resolution use half of maxw as the scaler can only scale
1452 if (width
== maxw
&& height
== maxh
)
1455 /* width must even because of the YUYV format
1456 height must be even because of interlacing */
1457 v4l_bound_align_image(&width
, 48, maxw
, 1, &height
, 32, maxh
,
1460 /* Avoid division by zero at size_to_scale */
1466 size_to_scale(dev
, width
, height
, &hscale
, &vscale
);
1467 scale_to_size(dev
, hscale
, vscale
, &width
, &height
);
1469 f
->fmt
.pix
.width
= width
;
1470 f
->fmt
.pix
.height
= height
;
1471 f
->fmt
.pix
.pixelformat
= fmt
->fourcc
;
1472 f
->fmt
.pix
.bytesperline
= (width
* fmt
->depth
+ 7) >> 3;
1473 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.bytesperline
* height
;
1474 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1475 if (v4l2
->progressive
)
1476 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1478 f
->fmt
.pix
.field
= v4l2
->interlaced_fieldmode
?
1479 V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
1480 f
->fmt
.pix
.priv
= 0;
1485 static int em28xx_set_video_format(struct em28xx
*dev
, unsigned int fourcc
,
1486 unsigned width
, unsigned height
)
1488 struct em28xx_fmt
*fmt
;
1489 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1491 fmt
= format_by_fourcc(fourcc
);
1496 v4l2
->width
= width
;
1497 v4l2
->height
= height
;
1499 /* set new image size */
1500 size_to_scale(dev
, v4l2
->width
, v4l2
->height
,
1501 &v4l2
->hscale
, &v4l2
->vscale
);
1503 em28xx_resolution_set(dev
);
1508 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1509 struct v4l2_format
*f
)
1511 struct em28xx
*dev
= video_drvdata(file
);
1512 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1514 if (vb2_is_busy(&v4l2
->vb_vidq
))
1517 vidioc_try_fmt_vid_cap(file
, priv
, f
);
1519 return em28xx_set_video_format(dev
, f
->fmt
.pix
.pixelformat
,
1520 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
1523 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1525 struct em28xx
*dev
= video_drvdata(file
);
1527 *norm
= dev
->v4l2
->norm
;
1532 static int vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1534 struct em28xx
*dev
= video_drvdata(file
);
1536 v4l2_device_call_all(&dev
->v4l2
->v4l2_dev
, 0, video
, querystd
, norm
);
1541 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
1543 struct em28xx
*dev
= video_drvdata(file
);
1544 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1545 struct v4l2_format f
;
1547 if (norm
== v4l2
->norm
)
1550 if (v4l2
->streaming_users
> 0)
1555 /* Adjusts width/height, if needed */
1556 f
.fmt
.pix
.width
= 720;
1557 f
.fmt
.pix
.height
= (norm
& V4L2_STD_525_60
) ? 480 : 576;
1558 vidioc_try_fmt_vid_cap(file
, priv
, &f
);
1560 /* set new image size */
1561 v4l2
->width
= f
.fmt
.pix
.width
;
1562 v4l2
->height
= f
.fmt
.pix
.height
;
1563 size_to_scale(dev
, v4l2
->width
, v4l2
->height
,
1564 &v4l2
->hscale
, &v4l2
->vscale
);
1566 em28xx_resolution_set(dev
);
1567 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, video
, s_std
, v4l2
->norm
);
1572 static int vidioc_g_parm(struct file
*file
, void *priv
,
1573 struct v4l2_streamparm
*p
)
1575 struct em28xx
*dev
= video_drvdata(file
);
1576 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1579 p
->parm
.capture
.readbuffers
= EM28XX_MIN_BUF
;
1580 if (dev
->board
.is_webcam
)
1581 rc
= v4l2_device_call_until_err(&v4l2
->v4l2_dev
, 0,
1584 v4l2_video_std_frame_period(v4l2
->norm
,
1585 &p
->parm
.capture
.timeperframe
);
1590 static int vidioc_s_parm(struct file
*file
, void *priv
,
1591 struct v4l2_streamparm
*p
)
1593 struct em28xx
*dev
= video_drvdata(file
);
1595 p
->parm
.capture
.readbuffers
= EM28XX_MIN_BUF
;
1596 return v4l2_device_call_until_err(&dev
->v4l2
->v4l2_dev
,
1597 0, video
, s_parm
, p
);
1600 static int vidioc_enum_input(struct file
*file
, void *priv
,
1601 struct v4l2_input
*i
)
1603 struct em28xx
*dev
= video_drvdata(file
);
1607 if (n
>= MAX_EM28XX_INPUT
)
1609 if (0 == INPUT(n
)->type
)
1613 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1615 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
1617 if ((EM28XX_VMUX_TELEVISION
== INPUT(n
)->type
))
1618 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1620 i
->std
= dev
->v4l2
->vdev
.tvnorms
;
1621 /* webcams do not have the STD API */
1622 if (dev
->board
.is_webcam
)
1623 i
->capabilities
= 0;
1628 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1630 struct em28xx
*dev
= video_drvdata(file
);
1632 *i
= dev
->ctl_input
;
1637 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1639 struct em28xx
*dev
= video_drvdata(file
);
1641 if (i
>= MAX_EM28XX_INPUT
)
1643 if (0 == INPUT(i
)->type
)
1650 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1652 struct em28xx
*dev
= video_drvdata(file
);
1655 case EM28XX_AMUX_VIDEO
:
1656 strcpy(a
->name
, "Television");
1658 case EM28XX_AMUX_LINE_IN
:
1659 strcpy(a
->name
, "Line In");
1661 case EM28XX_AMUX_VIDEO2
:
1662 strcpy(a
->name
, "Television alt");
1664 case EM28XX_AMUX_PHONE
:
1665 strcpy(a
->name
, "Phone");
1667 case EM28XX_AMUX_MIC
:
1668 strcpy(a
->name
, "Mic");
1670 case EM28XX_AMUX_CD
:
1671 strcpy(a
->name
, "CD");
1673 case EM28XX_AMUX_AUX
:
1674 strcpy(a
->name
, "Aux");
1676 case EM28XX_AMUX_PCM_OUT
:
1677 strcpy(a
->name
, "PCM");
1683 a
->index
= dev
->ctl_ainput
;
1684 a
->capability
= V4L2_AUDCAP_STEREO
;
1689 static int vidioc_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*a
)
1691 struct em28xx
*dev
= video_drvdata(file
);
1693 if (a
->index
>= MAX_EM28XX_INPUT
)
1695 if (0 == INPUT(a
->index
)->type
)
1698 dev
->ctl_ainput
= INPUT(a
->index
)->amux
;
1699 dev
->ctl_aoutput
= INPUT(a
->index
)->aout
;
1701 if (!dev
->ctl_aoutput
)
1702 dev
->ctl_aoutput
= EM28XX_AOUT_MASTER
;
1707 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1708 struct v4l2_tuner
*t
)
1710 struct em28xx
*dev
= video_drvdata(file
);
1715 strcpy(t
->name
, "Tuner");
1717 v4l2_device_call_all(&dev
->v4l2
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1721 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1722 const struct v4l2_tuner
*t
)
1724 struct em28xx
*dev
= video_drvdata(file
);
1729 v4l2_device_call_all(&dev
->v4l2
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1733 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1734 struct v4l2_frequency
*f
)
1736 struct em28xx
*dev
= video_drvdata(file
);
1737 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1742 f
->frequency
= v4l2
->frequency
;
1746 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1747 const struct v4l2_frequency
*f
)
1749 struct v4l2_frequency new_freq
= *f
;
1750 struct em28xx
*dev
= video_drvdata(file
);
1751 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1756 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, tuner
, s_frequency
, f
);
1757 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, tuner
, g_frequency
, &new_freq
);
1758 v4l2
->frequency
= new_freq
.frequency
;
1763 #ifdef CONFIG_VIDEO_ADV_DEBUG
1764 static int vidioc_g_chip_info(struct file
*file
, void *priv
,
1765 struct v4l2_dbg_chip_info
*chip
)
1767 struct em28xx
*dev
= video_drvdata(file
);
1769 if (chip
->match
.addr
> 1)
1771 if (chip
->match
.addr
== 1)
1772 strlcpy(chip
->name
, "ac97", sizeof(chip
->name
));
1775 dev
->v4l2
->v4l2_dev
.name
, sizeof(chip
->name
));
1779 static int em28xx_reg_len(int reg
)
1782 case EM28XX_R40_AC97LSB
:
1783 case EM28XX_R30_HSCALELOW
:
1784 case EM28XX_R32_VSCALELOW
:
1791 static int vidioc_g_register(struct file
*file
, void *priv
,
1792 struct v4l2_dbg_register
*reg
)
1794 struct em28xx
*dev
= video_drvdata(file
);
1797 if (reg
->match
.addr
> 1)
1799 if (reg
->match
.addr
) {
1800 ret
= em28xx_read_ac97(dev
, reg
->reg
);
1810 reg
->size
= em28xx_reg_len(reg
->reg
);
1811 if (reg
->size
== 1) {
1812 ret
= em28xx_read_reg(dev
, reg
->reg
);
1821 ret
= dev
->em28xx_read_reg_req_len(dev
, USB_REQ_GET_STATUS
,
1822 reg
->reg
, (char *)&val
, 2);
1826 reg
->val
= le16_to_cpu(val
);
1832 static int vidioc_s_register(struct file
*file
, void *priv
,
1833 const struct v4l2_dbg_register
*reg
)
1835 struct em28xx
*dev
= video_drvdata(file
);
1838 if (reg
->match
.addr
> 1)
1840 if (reg
->match
.addr
)
1841 return em28xx_write_ac97(dev
, reg
->reg
, reg
->val
);
1844 buf
= cpu_to_le16(reg
->val
);
1846 return em28xx_write_regs(dev
, reg
->reg
, (char *)&buf
,
1847 em28xx_reg_len(reg
->reg
));
1851 static int vidioc_querycap(struct file
*file
, void *priv
,
1852 struct v4l2_capability
*cap
)
1854 struct video_device
*vdev
= video_devdata(file
);
1855 struct em28xx
*dev
= video_drvdata(file
);
1856 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1858 strlcpy(cap
->driver
, "em28xx", sizeof(cap
->driver
));
1859 strlcpy(cap
->card
, em28xx_boards
[dev
->model
].name
, sizeof(cap
->card
));
1860 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1862 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
1863 cap
->device_caps
= V4L2_CAP_READWRITE
|
1864 V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1865 else if (vdev
->vfl_type
== VFL_TYPE_RADIO
)
1866 cap
->device_caps
= V4L2_CAP_RADIO
;
1868 cap
->device_caps
= V4L2_CAP_READWRITE
| V4L2_CAP_VBI_CAPTURE
;
1870 if (dev
->int_audio_type
!= EM28XX_INT_AUDIO_NONE
)
1871 cap
->device_caps
|= V4L2_CAP_AUDIO
;
1873 if (dev
->tuner_type
!= TUNER_ABSENT
)
1874 cap
->device_caps
|= V4L2_CAP_TUNER
;
1876 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
|
1877 V4L2_CAP_READWRITE
| V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1878 if (video_is_registered(&v4l2
->vbi_dev
))
1879 cap
->capabilities
|= V4L2_CAP_VBI_CAPTURE
;
1880 if (video_is_registered(&v4l2
->radio_dev
))
1881 cap
->capabilities
|= V4L2_CAP_RADIO
;
1885 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1886 struct v4l2_fmtdesc
*f
)
1888 if (unlikely(f
->index
>= ARRAY_SIZE(format
)))
1891 strlcpy(f
->description
, format
[f
->index
].name
, sizeof(f
->description
));
1892 f
->pixelformat
= format
[f
->index
].fourcc
;
1897 static int vidioc_enum_framesizes(struct file
*file
, void *priv
,
1898 struct v4l2_frmsizeenum
*fsize
)
1900 struct em28xx
*dev
= video_drvdata(file
);
1901 struct em28xx_fmt
*fmt
;
1902 unsigned int maxw
= norm_maxw(dev
);
1903 unsigned int maxh
= norm_maxh(dev
);
1905 fmt
= format_by_fourcc(fsize
->pixel_format
);
1907 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1908 fsize
->pixel_format
);
1912 if (dev
->board
.is_em2800
) {
1913 if (fsize
->index
> 1)
1915 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1916 fsize
->discrete
.width
= maxw
/ (1 + fsize
->index
);
1917 fsize
->discrete
.height
= maxh
/ (1 + fsize
->index
);
1921 if (fsize
->index
!= 0)
1924 /* Report a continuous range */
1925 fsize
->type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
1926 scale_to_size(dev
, EM28XX_HVSCALE_MAX
, EM28XX_HVSCALE_MAX
,
1927 &fsize
->stepwise
.min_width
, &fsize
->stepwise
.min_height
);
1928 if (fsize
->stepwise
.min_width
< 48)
1929 fsize
->stepwise
.min_width
= 48;
1930 if (fsize
->stepwise
.min_height
< 38)
1931 fsize
->stepwise
.min_height
= 38;
1932 fsize
->stepwise
.max_width
= maxw
;
1933 fsize
->stepwise
.max_height
= maxh
;
1934 fsize
->stepwise
.step_width
= 1;
1935 fsize
->stepwise
.step_height
= 1;
1939 /* RAW VBI ioctls */
1941 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
1942 struct v4l2_format
*format
)
1944 struct em28xx
*dev
= video_drvdata(file
);
1945 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
1947 format
->fmt
.vbi
.samples_per_line
= v4l2
->vbi_width
;
1948 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1949 format
->fmt
.vbi
.offset
= 0;
1950 format
->fmt
.vbi
.flags
= 0;
1951 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1952 format
->fmt
.vbi
.count
[0] = v4l2
->vbi_height
;
1953 format
->fmt
.vbi
.count
[1] = v4l2
->vbi_height
;
1954 memset(format
->fmt
.vbi
.reserved
, 0, sizeof(format
->fmt
.vbi
.reserved
));
1956 /* Varies by video standard (NTSC, PAL, etc.) */
1957 if (v4l2
->norm
& V4L2_STD_525_60
) {
1959 format
->fmt
.vbi
.start
[0] = 10;
1960 format
->fmt
.vbi
.start
[1] = 273;
1961 } else if (v4l2
->norm
& V4L2_STD_625_50
) {
1963 format
->fmt
.vbi
.start
[0] = 6;
1964 format
->fmt
.vbi
.start
[1] = 318;
1970 /* ----------------------------------------------------------- */
1971 /* RADIO ESPECIFIC IOCTLS */
1972 /* ----------------------------------------------------------- */
1974 static int radio_g_tuner(struct file
*file
, void *priv
,
1975 struct v4l2_tuner
*t
)
1977 struct em28xx
*dev
= video_drvdata(file
);
1979 if (unlikely(t
->index
> 0))
1982 strcpy(t
->name
, "Radio");
1984 v4l2_device_call_all(&dev
->v4l2
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1989 static int radio_s_tuner(struct file
*file
, void *priv
,
1990 const struct v4l2_tuner
*t
)
1992 struct em28xx
*dev
= video_drvdata(file
);
1997 v4l2_device_call_all(&dev
->v4l2
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
2003 * em28xx_free_v4l2() - Free struct em28xx_v4l2
2005 * @ref: struct kref for struct em28xx_v4l2
2007 * Called when all users of struct em28xx_v4l2 are gone
2009 static void em28xx_free_v4l2(struct kref
*ref
)
2011 struct em28xx_v4l2
*v4l2
= container_of(ref
, struct em28xx_v4l2
, ref
);
2013 v4l2
->dev
->v4l2
= NULL
;
2018 * em28xx_v4l2_open()
2019 * inits the device and starts isoc transfer
2021 static int em28xx_v4l2_open(struct file
*filp
)
2023 struct video_device
*vdev
= video_devdata(filp
);
2024 struct em28xx
*dev
= video_drvdata(filp
);
2025 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
2026 enum v4l2_buf_type fh_type
= 0;
2029 switch (vdev
->vfl_type
) {
2030 case VFL_TYPE_GRABBER
:
2031 fh_type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2034 fh_type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
2036 case VFL_TYPE_RADIO
:
2042 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2043 video_device_node_name(vdev
), v4l2_type_names
[fh_type
],
2046 if (mutex_lock_interruptible(&dev
->lock
))
2047 return -ERESTARTSYS
;
2049 ret
= v4l2_fh_open(filp
);
2051 em28xx_errdev("%s: v4l2_fh_open() returned error %d\n",
2053 mutex_unlock(&dev
->lock
);
2057 if (v4l2
->users
== 0) {
2058 em28xx_set_mode(dev
, EM28XX_ANALOG_MODE
);
2060 if (vdev
->vfl_type
!= VFL_TYPE_RADIO
)
2061 em28xx_resolution_set(dev
);
2064 * Needed, since GPIO might have disabled power
2065 * of some i2c devices
2067 em28xx_wake_i2c(dev
);
2070 if (vdev
->vfl_type
== VFL_TYPE_RADIO
) {
2071 em28xx_videodbg("video_open: setting radio device\n");
2072 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, tuner
, s_radio
);
2075 kref_get(&dev
->ref
);
2076 kref_get(&v4l2
->ref
);
2079 mutex_unlock(&dev
->lock
);
2085 * em28xx_v4l2_fini()
2086 * unregisters the v4l2,i2c and usb devices
2087 * called when the device gets disconected or at module unload
2089 static int em28xx_v4l2_fini(struct em28xx
*dev
)
2091 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
2093 if (dev
->is_audio_only
) {
2094 /* Shouldn't initialize IR for this interface */
2098 if (!dev
->has_video
) {
2099 /* This device does not support the v4l2 extension */
2106 em28xx_info("Closing video extension\n");
2108 mutex_lock(&dev
->lock
);
2110 v4l2_device_disconnect(&v4l2
->v4l2_dev
);
2112 em28xx_uninit_usb_xfer(dev
, EM28XX_ANALOG_MODE
);
2114 em28xx_v4l2_media_release(dev
);
2116 if (video_is_registered(&v4l2
->radio_dev
)) {
2117 em28xx_info("V4L2 device %s deregistered\n",
2118 video_device_node_name(&v4l2
->radio_dev
));
2119 video_unregister_device(&v4l2
->radio_dev
);
2121 if (video_is_registered(&v4l2
->vbi_dev
)) {
2122 em28xx_info("V4L2 device %s deregistered\n",
2123 video_device_node_name(&v4l2
->vbi_dev
));
2124 video_unregister_device(&v4l2
->vbi_dev
);
2126 if (video_is_registered(&v4l2
->vdev
)) {
2127 em28xx_info("V4L2 device %s deregistered\n",
2128 video_device_node_name(&v4l2
->vdev
));
2129 video_unregister_device(&v4l2
->vdev
);
2132 v4l2_ctrl_handler_free(&v4l2
->ctrl_handler
);
2133 v4l2_device_unregister(&v4l2
->v4l2_dev
);
2136 v4l2_clk_unregister_fixed(v4l2
->clk
);
2140 kref_put(&v4l2
->ref
, em28xx_free_v4l2
);
2142 mutex_unlock(&dev
->lock
);
2144 kref_put(&dev
->ref
, em28xx_free_device
);
2149 static int em28xx_v4l2_suspend(struct em28xx
*dev
)
2151 if (dev
->is_audio_only
)
2154 if (!dev
->has_video
)
2157 em28xx_info("Suspending video extension\n");
2158 em28xx_stop_urbs(dev
);
2162 static int em28xx_v4l2_resume(struct em28xx
*dev
)
2164 if (dev
->is_audio_only
)
2167 if (!dev
->has_video
)
2170 em28xx_info("Resuming video extension\n");
2171 /* what do we do here */
2176 * em28xx_v4l2_close()
2177 * stops streaming and deallocates all resources allocated by the v4l2
2180 static int em28xx_v4l2_close(struct file
*filp
)
2182 struct em28xx
*dev
= video_drvdata(filp
);
2183 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
2186 em28xx_videodbg("users=%d\n", v4l2
->users
);
2188 vb2_fop_release(filp
);
2189 mutex_lock(&dev
->lock
);
2191 if (v4l2
->users
== 1) {
2192 /* No sense to try to write to the device */
2193 if (dev
->disconnected
)
2196 /* Save some power by putting tuner to sleep */
2197 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, core
, s_power
, 0);
2199 /* do this before setting alternate! */
2200 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
2202 /* set alternate 0 */
2204 em28xx_videodbg("setting alternate 0\n");
2205 errCode
= usb_set_interface(dev
->udev
, 0, 0);
2207 em28xx_errdev("cannot change alternate number to "
2208 "0 (error=%i)\n", errCode
);
2214 kref_put(&v4l2
->ref
, em28xx_free_v4l2
);
2215 mutex_unlock(&dev
->lock
);
2216 kref_put(&dev
->ref
, em28xx_free_device
);
2221 static const struct v4l2_file_operations em28xx_v4l_fops
= {
2222 .owner
= THIS_MODULE
,
2223 .open
= em28xx_v4l2_open
,
2224 .release
= em28xx_v4l2_close
,
2225 .read
= vb2_fop_read
,
2226 .poll
= vb2_fop_poll
,
2227 .mmap
= vb2_fop_mmap
,
2228 .unlocked_ioctl
= video_ioctl2
,
2231 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
2232 .vidioc_querycap
= vidioc_querycap
,
2233 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
2234 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
2235 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
2236 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
2237 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
2238 .vidioc_try_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
2239 .vidioc_s_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
2240 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
2241 .vidioc_g_audio
= vidioc_g_audio
,
2242 .vidioc_s_audio
= vidioc_s_audio
,
2244 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
2245 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
2246 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
2247 .vidioc_querybuf
= vb2_ioctl_querybuf
,
2248 .vidioc_qbuf
= vb2_ioctl_qbuf
,
2249 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
2251 .vidioc_g_std
= vidioc_g_std
,
2252 .vidioc_querystd
= vidioc_querystd
,
2253 .vidioc_s_std
= vidioc_s_std
,
2254 .vidioc_g_parm
= vidioc_g_parm
,
2255 .vidioc_s_parm
= vidioc_s_parm
,
2256 .vidioc_enum_input
= vidioc_enum_input
,
2257 .vidioc_g_input
= vidioc_g_input
,
2258 .vidioc_s_input
= vidioc_s_input
,
2259 .vidioc_streamon
= vb2_ioctl_streamon
,
2260 .vidioc_streamoff
= vb2_ioctl_streamoff
,
2261 .vidioc_g_tuner
= vidioc_g_tuner
,
2262 .vidioc_s_tuner
= vidioc_s_tuner
,
2263 .vidioc_g_frequency
= vidioc_g_frequency
,
2264 .vidioc_s_frequency
= vidioc_s_frequency
,
2265 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
2266 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
2267 #ifdef CONFIG_VIDEO_ADV_DEBUG
2268 .vidioc_g_chip_info
= vidioc_g_chip_info
,
2269 .vidioc_g_register
= vidioc_g_register
,
2270 .vidioc_s_register
= vidioc_s_register
,
2274 static const struct video_device em28xx_video_template
= {
2275 .fops
= &em28xx_v4l_fops
,
2276 .ioctl_ops
= &video_ioctl_ops
,
2277 .release
= video_device_release_empty
,
2278 .tvnorms
= V4L2_STD_ALL
,
2281 static const struct v4l2_file_operations radio_fops
= {
2282 .owner
= THIS_MODULE
,
2283 .open
= em28xx_v4l2_open
,
2284 .release
= em28xx_v4l2_close
,
2285 .unlocked_ioctl
= video_ioctl2
,
2288 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
2289 .vidioc_querycap
= vidioc_querycap
,
2290 .vidioc_g_tuner
= radio_g_tuner
,
2291 .vidioc_s_tuner
= radio_s_tuner
,
2292 .vidioc_g_frequency
= vidioc_g_frequency
,
2293 .vidioc_s_frequency
= vidioc_s_frequency
,
2294 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
2295 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
2296 #ifdef CONFIG_VIDEO_ADV_DEBUG
2297 .vidioc_g_chip_info
= vidioc_g_chip_info
,
2298 .vidioc_g_register
= vidioc_g_register
,
2299 .vidioc_s_register
= vidioc_s_register
,
2303 static struct video_device em28xx_radio_template
= {
2304 .fops
= &radio_fops
,
2305 .ioctl_ops
= &radio_ioctl_ops
,
2306 .release
= video_device_release_empty
,
2309 /* I2C possible address to saa7115, tvp5150, msp3400, tvaudio */
2310 static unsigned short saa711x_addrs
[] = {
2311 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
2312 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
2315 static unsigned short tvp5150_addrs
[] = {
2321 static unsigned short msp3400_addrs
[] = {
2327 /******************************** usb interface ******************************/
2329 static void em28xx_vdev_init(struct em28xx
*dev
,
2330 struct video_device
*vfd
,
2331 const struct video_device
*template,
2332 const char *type_name
)
2335 vfd
->v4l2_dev
= &dev
->v4l2
->v4l2_dev
;
2336 vfd
->lock
= &dev
->lock
;
2337 if (dev
->board
.is_webcam
)
2340 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s",
2341 dev
->name
, type_name
);
2343 video_set_drvdata(vfd
, dev
);
2346 static void em28xx_tuner_setup(struct em28xx
*dev
, unsigned short tuner_addr
)
2348 struct em28xx_v4l2
*v4l2
= dev
->v4l2
;
2349 struct v4l2_device
*v4l2_dev
= &v4l2
->v4l2_dev
;
2350 struct tuner_setup tun_setup
;
2351 struct v4l2_frequency f
;
2353 memset(&tun_setup
, 0, sizeof(tun_setup
));
2355 tun_setup
.mode_mask
= T_ANALOG_TV
| T_RADIO
;
2356 tun_setup
.tuner_callback
= em28xx_tuner_callback
;
2358 if (dev
->board
.radio
.type
) {
2359 tun_setup
.type
= dev
->board
.radio
.type
;
2360 tun_setup
.addr
= dev
->board
.radio_addr
;
2362 v4l2_device_call_all(v4l2_dev
,
2363 0, tuner
, s_type_addr
, &tun_setup
);
2366 if ((dev
->tuner_type
!= TUNER_ABSENT
) && (dev
->tuner_type
)) {
2367 tun_setup
.type
= dev
->tuner_type
;
2368 tun_setup
.addr
= tuner_addr
;
2370 v4l2_device_call_all(v4l2_dev
,
2371 0, tuner
, s_type_addr
, &tun_setup
);
2374 if (dev
->board
.tda9887_conf
) {
2375 struct v4l2_priv_tun_config tda9887_cfg
;
2377 tda9887_cfg
.tuner
= TUNER_TDA9887
;
2378 tda9887_cfg
.priv
= &dev
->board
.tda9887_conf
;
2380 v4l2_device_call_all(v4l2_dev
,
2381 0, tuner
, s_config
, &tda9887_cfg
);
2384 if (dev
->tuner_type
== TUNER_XC2028
) {
2385 struct v4l2_priv_tun_config xc2028_cfg
;
2386 struct xc2028_ctrl ctl
;
2388 memset(&xc2028_cfg
, 0, sizeof(xc2028_cfg
));
2389 memset(&ctl
, 0, sizeof(ctl
));
2391 em28xx_setup_xc3028(dev
, &ctl
);
2393 xc2028_cfg
.tuner
= TUNER_XC2028
;
2394 xc2028_cfg
.priv
= &ctl
;
2396 v4l2_device_call_all(v4l2_dev
, 0, tuner
, s_config
, &xc2028_cfg
);
2399 /* configure tuner */
2401 f
.type
= V4L2_TUNER_ANALOG_TV
;
2402 f
.frequency
= 9076; /* just a magic number */
2403 v4l2
->frequency
= f
.frequency
;
2404 v4l2_device_call_all(v4l2_dev
, 0, tuner
, s_frequency
, &f
);
2407 static int em28xx_v4l2_init(struct em28xx
*dev
)
2412 struct v4l2_ctrl_handler
*hdl
;
2413 struct em28xx_v4l2
*v4l2
;
2415 if (dev
->is_audio_only
) {
2416 /* Shouldn't initialize IR for this interface */
2420 if (!dev
->has_video
) {
2421 /* This device does not support the v4l2 extension */
2425 em28xx_info("Registering V4L2 extension\n");
2427 mutex_lock(&dev
->lock
);
2429 v4l2
= kzalloc(sizeof(struct em28xx_v4l2
), GFP_KERNEL
);
2431 em28xx_info("em28xx_v4l: memory allocation failed\n");
2432 mutex_unlock(&dev
->lock
);
2435 kref_init(&v4l2
->ref
);
2439 #ifdef CONFIG_MEDIA_CONTROLLER
2440 v4l2
->v4l2_dev
.mdev
= dev
->media_dev
;
2442 ret
= v4l2_device_register(&dev
->udev
->dev
, &v4l2
->v4l2_dev
);
2444 em28xx_errdev("Call to v4l2_device_register() failed!\n");
2448 hdl
= &v4l2
->ctrl_handler
;
2449 v4l2_ctrl_handler_init(hdl
, 8);
2450 v4l2
->v4l2_dev
.ctrl_handler
= hdl
;
2452 if (dev
->board
.is_webcam
)
2453 v4l2
->progressive
= true;
2456 * Default format, used for tvp5150 or saa711x output formats
2458 v4l2
->vinmode
= 0x10;
2459 v4l2
->vinctl
= EM28XX_VINCTRL_INTERLACED
|
2460 EM28XX_VINCTRL_CCIR656_ENABLE
;
2462 /* request some modules */
2464 if (dev
->board
.has_msp34xx
)
2465 v4l2_i2c_new_subdev(&v4l2
->v4l2_dev
,
2466 &dev
->i2c_adap
[dev
->def_i2c_bus
],
2467 "msp3400", 0, msp3400_addrs
);
2469 if (dev
->board
.decoder
== EM28XX_SAA711X
)
2470 v4l2_i2c_new_subdev(&v4l2
->v4l2_dev
,
2471 &dev
->i2c_adap
[dev
->def_i2c_bus
],
2472 "saa7115_auto", 0, saa711x_addrs
);
2474 if (dev
->board
.decoder
== EM28XX_TVP5150
)
2475 v4l2_i2c_new_subdev(&v4l2
->v4l2_dev
,
2476 &dev
->i2c_adap
[dev
->def_i2c_bus
],
2477 "tvp5150", 0, tvp5150_addrs
);
2479 if (dev
->board
.adecoder
== EM28XX_TVAUDIO
)
2480 v4l2_i2c_new_subdev(&v4l2
->v4l2_dev
,
2481 &dev
->i2c_adap
[dev
->def_i2c_bus
],
2482 "tvaudio", dev
->board
.tvaudio_addr
, NULL
);
2484 /* Initialize tuner and camera */
2486 if (dev
->board
.tuner_type
!= TUNER_ABSENT
) {
2487 unsigned short tuner_addr
= dev
->board
.tuner_addr
;
2488 int has_demod
= (dev
->board
.tda9887_conf
& TDA9887_PRESENT
);
2490 if (dev
->board
.radio
.type
)
2491 v4l2_i2c_new_subdev(&v4l2
->v4l2_dev
,
2492 &dev
->i2c_adap
[dev
->def_i2c_bus
],
2493 "tuner", dev
->board
.radio_addr
,
2497 v4l2_i2c_new_subdev(&v4l2
->v4l2_dev
,
2498 &dev
->i2c_adap
[dev
->def_i2c_bus
],
2500 v4l2_i2c_tuner_addrs(ADDRS_DEMOD
));
2501 if (tuner_addr
== 0) {
2502 enum v4l2_i2c_tuner_type type
=
2503 has_demod
? ADDRS_TV_WITH_DEMOD
: ADDRS_TV
;
2504 struct v4l2_subdev
*sd
;
2506 sd
= v4l2_i2c_new_subdev(&v4l2
->v4l2_dev
,
2507 &dev
->i2c_adap
[dev
->def_i2c_bus
],
2509 v4l2_i2c_tuner_addrs(type
));
2512 tuner_addr
= v4l2_i2c_subdev_addr(sd
);
2514 v4l2_i2c_new_subdev(&v4l2
->v4l2_dev
,
2515 &dev
->i2c_adap
[dev
->def_i2c_bus
],
2516 "tuner", tuner_addr
, NULL
);
2519 em28xx_tuner_setup(dev
, tuner_addr
);
2522 if (dev
->em28xx_sensor
!= EM28XX_NOSENSOR
)
2523 em28xx_init_camera(dev
);
2525 /* Configure audio */
2526 ret
= em28xx_audio_setup(dev
);
2528 em28xx_errdev("%s: Error while setting audio - error [%d]!\n",
2530 goto unregister_dev
;
2532 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
2533 v4l2_ctrl_new_std(hdl
, &em28xx_ctrl_ops
,
2534 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 1);
2535 v4l2_ctrl_new_std(hdl
, &em28xx_ctrl_ops
,
2536 V4L2_CID_AUDIO_VOLUME
, 0, 0x1f, 1, 0x1f);
2538 /* install the em28xx notify callback */
2539 v4l2_ctrl_notify(v4l2_ctrl_find(hdl
, V4L2_CID_AUDIO_MUTE
),
2540 em28xx_ctrl_notify
, dev
);
2541 v4l2_ctrl_notify(v4l2_ctrl_find(hdl
, V4L2_CID_AUDIO_VOLUME
),
2542 em28xx_ctrl_notify
, dev
);
2545 /* wake i2c devices */
2546 em28xx_wake_i2c(dev
);
2548 /* init video dma queues */
2549 INIT_LIST_HEAD(&dev
->vidq
.active
);
2550 INIT_LIST_HEAD(&dev
->vbiq
.active
);
2552 if (dev
->board
.has_msp34xx
) {
2553 /* Send a reset to other chips via gpio */
2554 ret
= em28xx_write_reg(dev
, EM2820_R08_GPIO_CTRL
, 0xf7);
2556 em28xx_errdev("%s: em28xx_write_reg - msp34xx(1) failed! error [%d]\n",
2558 goto unregister_dev
;
2562 ret
= em28xx_write_reg(dev
, EM2820_R08_GPIO_CTRL
, 0xff);
2564 em28xx_errdev("%s: em28xx_write_reg - msp34xx(2) failed! error [%d]\n",
2566 goto unregister_dev
;
2571 /* set default norm */
2572 v4l2
->norm
= V4L2_STD_PAL
;
2573 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, video
, s_std
, v4l2
->norm
);
2574 v4l2
->interlaced_fieldmode
= EM28XX_INTERLACED_DEFAULT
;
2576 /* Analog specific initialization */
2577 v4l2
->format
= &format
[0];
2579 maxw
= norm_maxw(dev
);
2580 /* MaxPacketSize for em2800 is too small to capture at full resolution
2581 * use half of maxw as the scaler can only scale to 50% */
2582 if (dev
->board
.is_em2800
)
2585 em28xx_set_video_format(dev
, format
[0].fourcc
,
2586 maxw
, norm_maxh(dev
));
2590 /* Audio defaults */
2594 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2595 val
= (u8
)em28xx_read_reg(dev
, EM28XX_R0F_XCLK
);
2596 em28xx_write_reg(dev
, EM28XX_R0F_XCLK
,
2597 (EM28XX_XCLK_AUDIO_UNMUTE
| val
));
2599 em28xx_set_outfmt(dev
);
2601 /* Add image controls */
2602 /* NOTE: at this point, the subdevices are already registered, so bridge
2603 * controls are only added/enabled when no subdevice provides them */
2604 if (NULL
== v4l2_ctrl_find(hdl
, V4L2_CID_CONTRAST
))
2605 v4l2_ctrl_new_std(hdl
, &em28xx_ctrl_ops
,
2607 0, 0x1f, 1, CONTRAST_DEFAULT
);
2608 if (NULL
== v4l2_ctrl_find(hdl
, V4L2_CID_BRIGHTNESS
))
2609 v4l2_ctrl_new_std(hdl
, &em28xx_ctrl_ops
,
2610 V4L2_CID_BRIGHTNESS
,
2611 -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT
);
2612 if (NULL
== v4l2_ctrl_find(hdl
, V4L2_CID_SATURATION
))
2613 v4l2_ctrl_new_std(hdl
, &em28xx_ctrl_ops
,
2614 V4L2_CID_SATURATION
,
2615 0, 0x1f, 1, SATURATION_DEFAULT
);
2616 if (NULL
== v4l2_ctrl_find(hdl
, V4L2_CID_BLUE_BALANCE
))
2617 v4l2_ctrl_new_std(hdl
, &em28xx_ctrl_ops
,
2618 V4L2_CID_BLUE_BALANCE
,
2619 -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT
);
2620 if (NULL
== v4l2_ctrl_find(hdl
, V4L2_CID_RED_BALANCE
))
2621 v4l2_ctrl_new_std(hdl
, &em28xx_ctrl_ops
,
2622 V4L2_CID_RED_BALANCE
,
2623 -0x30, 0x30, 1, RED_BALANCE_DEFAULT
);
2624 if (NULL
== v4l2_ctrl_find(hdl
, V4L2_CID_SHARPNESS
))
2625 v4l2_ctrl_new_std(hdl
, &em28xx_ctrl_ops
,
2627 0, 0x0f, 1, SHARPNESS_DEFAULT
);
2629 /* Reset image controls */
2630 em28xx_colorlevels_set_default(dev
);
2631 v4l2_ctrl_handler_setup(hdl
);
2634 goto unregister_dev
;
2636 /* allocate and fill video video_device struct */
2637 em28xx_vdev_init(dev
, &v4l2
->vdev
, &em28xx_video_template
, "video");
2638 mutex_init(&v4l2
->vb_queue_lock
);
2639 mutex_init(&v4l2
->vb_vbi_queue_lock
);
2640 v4l2
->vdev
.queue
= &v4l2
->vb_vidq
;
2641 v4l2
->vdev
.queue
->lock
= &v4l2
->vb_queue_lock
;
2643 /* disable inapplicable ioctls */
2644 if (dev
->board
.is_webcam
) {
2645 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_QUERYSTD
);
2646 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_G_STD
);
2647 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_S_STD
);
2649 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_S_PARM
);
2651 if (dev
->tuner_type
== TUNER_ABSENT
) {
2652 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_G_TUNER
);
2653 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_S_TUNER
);
2654 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_G_FREQUENCY
);
2655 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_S_FREQUENCY
);
2657 if (dev
->int_audio_type
== EM28XX_INT_AUDIO_NONE
) {
2658 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_G_AUDIO
);
2659 v4l2_disable_ioctl(&v4l2
->vdev
, VIDIOC_S_AUDIO
);
2662 /* register v4l2 video video_device */
2663 ret
= video_register_device(&v4l2
->vdev
, VFL_TYPE_GRABBER
,
2664 video_nr
[dev
->devno
]);
2666 em28xx_errdev("unable to register video device (error=%i).\n",
2668 goto unregister_dev
;
2671 /* Allocate and fill vbi video_device struct */
2672 if (em28xx_vbi_supported(dev
) == 1) {
2673 em28xx_vdev_init(dev
, &v4l2
->vbi_dev
, &em28xx_video_template
,
2676 v4l2
->vbi_dev
.queue
= &v4l2
->vb_vbiq
;
2677 v4l2
->vbi_dev
.queue
->lock
= &v4l2
->vb_vbi_queue_lock
;
2679 /* disable inapplicable ioctls */
2680 v4l2_disable_ioctl(&v4l2
->vbi_dev
, VIDIOC_S_PARM
);
2681 if (dev
->tuner_type
== TUNER_ABSENT
) {
2682 v4l2_disable_ioctl(&v4l2
->vbi_dev
, VIDIOC_G_TUNER
);
2683 v4l2_disable_ioctl(&v4l2
->vbi_dev
, VIDIOC_S_TUNER
);
2684 v4l2_disable_ioctl(&v4l2
->vbi_dev
, VIDIOC_G_FREQUENCY
);
2685 v4l2_disable_ioctl(&v4l2
->vbi_dev
, VIDIOC_S_FREQUENCY
);
2687 if (dev
->int_audio_type
== EM28XX_INT_AUDIO_NONE
) {
2688 v4l2_disable_ioctl(&v4l2
->vbi_dev
, VIDIOC_G_AUDIO
);
2689 v4l2_disable_ioctl(&v4l2
->vbi_dev
, VIDIOC_S_AUDIO
);
2692 /* register v4l2 vbi video_device */
2693 ret
= video_register_device(&v4l2
->vbi_dev
, VFL_TYPE_VBI
,
2694 vbi_nr
[dev
->devno
]);
2696 em28xx_errdev("unable to register vbi device\n");
2697 goto unregister_dev
;
2701 if (em28xx_boards
[dev
->model
].radio
.type
== EM28XX_RADIO
) {
2702 em28xx_vdev_init(dev
, &v4l2
->radio_dev
, &em28xx_radio_template
,
2704 ret
= video_register_device(&v4l2
->radio_dev
, VFL_TYPE_RADIO
,
2705 radio_nr
[dev
->devno
]);
2707 em28xx_errdev("can't register radio device\n");
2708 goto unregister_dev
;
2710 em28xx_info("Registered radio device as %s\n",
2711 video_device_node_name(&v4l2
->radio_dev
));
2714 /* Init entities at the Media Controller */
2715 em28xx_v4l2_create_entities(dev
);
2717 #ifdef CONFIG_MEDIA_CONTROLLER
2718 ret
= v4l2_mc_create_media_graph(dev
->media_dev
);
2720 em28xx_errdev("failed to create media graph\n");
2721 em28xx_v4l2_media_release(dev
);
2722 goto unregister_dev
;
2726 em28xx_info("V4L2 video device registered as %s\n",
2727 video_device_node_name(&v4l2
->vdev
));
2729 if (video_is_registered(&v4l2
->vbi_dev
))
2730 em28xx_info("V4L2 VBI device registered as %s\n",
2731 video_device_node_name(&v4l2
->vbi_dev
));
2733 /* Save some power by putting tuner to sleep */
2734 v4l2_device_call_all(&v4l2
->v4l2_dev
, 0, core
, s_power
, 0);
2736 /* initialize videobuf2 stuff */
2737 em28xx_vb2_setup(dev
);
2739 em28xx_info("V4L2 extension successfully initialized\n");
2741 kref_get(&dev
->ref
);
2743 mutex_unlock(&dev
->lock
);
2747 if (video_is_registered(&v4l2
->radio_dev
)) {
2748 em28xx_info("V4L2 device %s deregistered\n",
2749 video_device_node_name(&v4l2
->radio_dev
));
2750 video_unregister_device(&v4l2
->radio_dev
);
2752 if (video_is_registered(&v4l2
->vbi_dev
)) {
2753 em28xx_info("V4L2 device %s deregistered\n",
2754 video_device_node_name(&v4l2
->vbi_dev
));
2755 video_unregister_device(&v4l2
->vbi_dev
);
2757 if (video_is_registered(&v4l2
->vdev
)) {
2758 em28xx_info("V4L2 device %s deregistered\n",
2759 video_device_node_name(&v4l2
->vdev
));
2760 video_unregister_device(&v4l2
->vdev
);
2763 v4l2_ctrl_handler_free(&v4l2
->ctrl_handler
);
2764 v4l2_device_unregister(&v4l2
->v4l2_dev
);
2767 kref_put(&v4l2
->ref
, em28xx_free_v4l2
);
2768 mutex_unlock(&dev
->lock
);
2772 static struct em28xx_ops v4l2_ops
= {
2774 .name
= "Em28xx v4l2 Extension",
2775 .init
= em28xx_v4l2_init
,
2776 .fini
= em28xx_v4l2_fini
,
2777 .suspend
= em28xx_v4l2_suspend
,
2778 .resume
= em28xx_v4l2_resume
,
2781 static int __init
em28xx_video_register(void)
2783 return em28xx_register_extension(&v4l2_ops
);
2786 static void __exit
em28xx_video_unregister(void)
2788 em28xx_unregister_extension(&v4l2_ops
);
2791 module_init(em28xx_video_register
);
2792 module_exit(em28xx_video_unregister
);