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.
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/bitmap.h>
36 #include <linux/usb.h>
37 #include <linux/i2c.h>
39 #include <linux/mutex.h>
40 #include <linux/slab.h>
42 #include "em28xx-v4l.h"
43 #include <media/v4l2-common.h>
44 #include <media/v4l2-ioctl.h>
45 #include <media/v4l2-event.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 dev_printk(KERN_DEBUG, &dev->intf->dev, \
69 "video: %s: " fmt, __func__, ## arg); \
72 #define em28xx_isocdbg(fmt, arg...) do {\
74 dev_printk(KERN_DEBUG, &dev->intf->dev, \
75 "isoc: %s: " fmt, __func__, ## arg); \
78 MODULE_AUTHOR(DRIVER_AUTHOR);
79 MODULE_DESCRIPTION(DRIVER_DESC " - v4l2 interface");
80 MODULE_LICENSE("GPL");
81 MODULE_VERSION(EM28XX_VERSION);
83 #define EM25XX_FRMDATAHDR_BYTE1 0x02
84 #define EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE 0x20
85 #define EM25XX_FRMDATAHDR_BYTE2_FRAME_END 0x02
86 #define EM25XX_FRMDATAHDR_BYTE2_FRAME_ID 0x01
87 #define EM25XX_FRMDATAHDR_BYTE2_MASK (EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE | \
88 EM25XX_FRMDATAHDR_BYTE2_FRAME_END | \
89 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID)
91 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
92 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
93 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
95 module_param_array(video_nr, int, NULL, 0444);
96 module_param_array(vbi_nr, int, NULL, 0444);
97 module_param_array(radio_nr, int, NULL, 0444);
98 MODULE_PARM_DESC(video_nr, "video device numbers");
99 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
100 MODULE_PARM_DESC(radio_nr, "radio device numbers");
102 static unsigned int video_debug;
103 module_param(video_debug, int, 0644);
104 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
106 /* supported video standards */
107 static struct em28xx_fmt format[] = {
109 .name = "16 bpp YUY2, 4:2:2, packed",
110 .fourcc = V4L2_PIX_FMT_YUYV,
112 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
114 .name = "16 bpp RGB 565, LE",
115 .fourcc = V4L2_PIX_FMT_RGB565,
117 .reg = EM28XX_OUTFMT_RGB_16_656,
119 .name = "8 bpp Bayer RGRG..GBGB",
120 .fourcc = V4L2_PIX_FMT_SRGGB8,
122 .reg = EM28XX_OUTFMT_RGB_8_RGRG,
124 .name = "8 bpp Bayer BGBG..GRGR",
125 .fourcc = V4L2_PIX_FMT_SBGGR8,
127 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
129 .name = "8 bpp Bayer GRGR..BGBG",
130 .fourcc = V4L2_PIX_FMT_SGRBG8,
132 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
134 .name = "8 bpp Bayer GBGB..RGRG",
135 .fourcc = V4L2_PIX_FMT_SGBRG8,
137 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
139 .name = "12 bpp YUV411",
140 .fourcc = V4L2_PIX_FMT_YUV411P,
142 .reg = EM28XX_OUTFMT_YUV411,
146 /*FIXME: maxw should be dependent of alt mode */
147 static inline unsigned int norm_maxw(struct em28xx *dev)
149 struct em28xx_v4l2 *v4l2 = dev->v4l2;
151 if (dev->board.is_webcam)
152 return v4l2->sensor_xres;
154 if (dev->board.max_range_640_480)
160 static inline unsigned int norm_maxh(struct em28xx *dev)
162 struct em28xx_v4l2 *v4l2 = dev->v4l2;
164 if (dev->board.is_webcam)
165 return v4l2->sensor_yres;
167 if (dev->board.max_range_640_480)
170 return (v4l2->norm & V4L2_STD_625_50) ? 576 : 480;
173 static int em28xx_vbi_supported(struct em28xx *dev)
175 /* Modprobe option to manually disable */
176 if (disable_vbi == 1)
179 if (dev->board.is_webcam)
182 /* FIXME: check subdevices for VBI support */
184 if (dev->chip_id == CHIP_ID_EM2860 ||
185 dev->chip_id == CHIP_ID_EM2883)
188 /* Version of em28xx that does not support VBI */
194 * configure i2c attached devices
196 static void em28xx_wake_i2c(struct em28xx *dev)
198 struct v4l2_device *v4l2_dev = &dev->v4l2->v4l2_dev;
200 v4l2_device_call_all(v4l2_dev, 0, core, reset, 0);
201 v4l2_device_call_all(v4l2_dev, 0, video, s_routing,
202 INPUT(dev->ctl_input)->vmux, 0, 0);
205 static int em28xx_colorlevels_set_default(struct em28xx *dev)
207 em28xx_write_reg(dev, EM28XX_R20_YGAIN, CONTRAST_DEFAULT);
208 em28xx_write_reg(dev, EM28XX_R21_YOFFSET, BRIGHTNESS_DEFAULT);
209 em28xx_write_reg(dev, EM28XX_R22_UVGAIN, SATURATION_DEFAULT);
210 em28xx_write_reg(dev, EM28XX_R23_UOFFSET, BLUE_BALANCE_DEFAULT);
211 em28xx_write_reg(dev, EM28XX_R24_VOFFSET, RED_BALANCE_DEFAULT);
212 em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, SHARPNESS_DEFAULT);
214 em28xx_write_reg(dev, EM28XX_R14_GAMMA, 0x20);
215 em28xx_write_reg(dev, EM28XX_R15_RGAIN, 0x20);
216 em28xx_write_reg(dev, EM28XX_R16_GGAIN, 0x20);
217 em28xx_write_reg(dev, EM28XX_R17_BGAIN, 0x20);
218 em28xx_write_reg(dev, EM28XX_R18_ROFFSET, 0x00);
219 em28xx_write_reg(dev, EM28XX_R19_GOFFSET, 0x00);
220 return em28xx_write_reg(dev, EM28XX_R1A_BOFFSET, 0x00);
223 static int em28xx_set_outfmt(struct em28xx *dev)
227 struct em28xx_v4l2 *v4l2 = dev->v4l2;
229 fmt = v4l2->format->reg;
233 * NOTE: it's not clear if this is really needed !
234 * The datasheets say bit 5 is a reserved bit and devices seem to work
235 * fine without it. But the Windows driver sets it for em2710/50+em28xx
236 * devices and we've always been setting it, too.
238 * em2765 (em25xx, em276x/7x/8x) devices do NOT work with this bit set,
239 * it's likely used for an additional (compressed ?) format there.
241 ret = em28xx_write_reg(dev, EM28XX_R27_OUTFMT, fmt);
245 ret = em28xx_write_reg(dev, EM28XX_R10_VINMODE, v4l2->vinmode);
249 vinctrl = v4l2->vinctl;
250 if (em28xx_vbi_supported(dev) == 1) {
251 vinctrl |= EM28XX_VINCTRL_VBI_RAW;
252 em28xx_write_reg(dev, EM28XX_R34_VBI_START_H, 0x00);
253 em28xx_write_reg(dev, EM28XX_R36_VBI_WIDTH, v4l2->vbi_width/4);
254 em28xx_write_reg(dev, EM28XX_R37_VBI_HEIGHT, v4l2->vbi_height);
255 if (v4l2->norm & V4L2_STD_525_60) {
257 em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x09);
258 } else if (v4l2->norm & V4L2_STD_625_50) {
260 em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x07);
264 return em28xx_write_reg(dev, EM28XX_R11_VINCTRL, vinctrl);
267 static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
270 em28xx_videodbg("em28xx Scale: (%d,%d)-(%d,%d)\n",
271 xmin, ymin, xmax, ymax);
273 em28xx_write_regs(dev, EM28XX_R28_XMIN, &xmin, 1);
274 em28xx_write_regs(dev, EM28XX_R29_XMAX, &xmax, 1);
275 em28xx_write_regs(dev, EM28XX_R2A_YMIN, &ymin, 1);
276 return em28xx_write_regs(dev, EM28XX_R2B_YMAX, &ymax, 1);
279 static void em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
280 u16 width, u16 height)
282 u8 cwidth = width >> 2;
283 u8 cheight = height >> 2;
284 u8 overflow = (height >> 9 & 0x02) | (width >> 10 & 0x01);
285 /* NOTE: size limit: 2047x1023 = 2MPix */
287 em28xx_videodbg("capture area set to (%d,%d): %dx%d\n",
289 ((overflow & 2) << 9 | cwidth << 2),
290 ((overflow & 1) << 10 | cheight << 2));
292 em28xx_write_regs(dev, EM28XX_R1C_HSTART, &hstart, 1);
293 em28xx_write_regs(dev, EM28XX_R1D_VSTART, &vstart, 1);
294 em28xx_write_regs(dev, EM28XX_R1E_CWIDTH, &cwidth, 1);
295 em28xx_write_regs(dev, EM28XX_R1F_CHEIGHT, &cheight, 1);
296 em28xx_write_regs(dev, EM28XX_R1B_OFLOW, &overflow, 1);
298 /* FIXME: function/meaning of these registers ? */
299 /* FIXME: align width+height to multiples of 4 ?! */
300 if (dev->is_em25xx) {
301 em28xx_write_reg(dev, 0x34, width >> 4);
302 em28xx_write_reg(dev, 0x35, height >> 4);
306 static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
309 /* the em2800 scaler only supports scaling down to 50% */
311 if (dev->board.is_em2800) {
312 mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
318 em28xx_write_regs(dev, EM28XX_R30_HSCALELOW, (char *)buf, 2);
322 em28xx_write_regs(dev, EM28XX_R32_VSCALELOW, (char *)buf, 2);
323 /* it seems that both H and V scalers must be active
325 mode = (h || v) ? 0x30 : 0x00;
327 return em28xx_write_reg(dev, EM28XX_R26_COMPR, mode);
330 /* FIXME: this only function read values from dev */
331 static int em28xx_resolution_set(struct em28xx *dev)
333 struct em28xx_v4l2 *v4l2 = dev->v4l2;
334 int width = norm_maxw(dev);
335 int height = norm_maxh(dev);
337 /* Properly setup VBI */
338 v4l2->vbi_width = 720;
339 if (v4l2->norm & V4L2_STD_525_60)
340 v4l2->vbi_height = 12;
342 v4l2->vbi_height = 18;
344 em28xx_set_outfmt(dev);
346 em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
348 /* If we don't set the start position to 2 in VBI mode, we end up
349 with line 20/21 being YUYV encoded instead of being in 8-bit
350 greyscale. The core of the issue is that line 21 (and line 23 for
351 PAL WSS) are inside of active video region, and as a result they
352 get the pixelformatting associated with that area. So by cropping
353 it out, we end up with the same format as the rest of the VBI
355 if (em28xx_vbi_supported(dev) == 1)
356 em28xx_capture_area_set(dev, 0, 2, width, height);
358 em28xx_capture_area_set(dev, 0, 0, width, height);
360 return em28xx_scaler_set(dev, v4l2->hscale, v4l2->vscale);
363 /* Set USB alternate setting for analog video */
364 static int em28xx_set_alternate(struct em28xx *dev)
366 struct em28xx_v4l2 *v4l2 = dev->v4l2;
367 struct usb_device *udev = interface_to_usbdev(dev->intf);
370 unsigned int min_pkt_size = v4l2->width * 2 + 4;
372 /* NOTE: for isoc transfers, only alt settings > 0 are allowed
373 bulk transfers seem to work only with alt=0 ! */
375 if ((alt > 0) && (alt < dev->num_alt)) {
376 em28xx_videodbg("alternate forced to %d\n", dev->alt);
380 if (dev->analog_xfer_bulk)
383 /* When image size is bigger than a certain value,
384 the frame size should be increased, otherwise, only
385 green screen will be received.
387 if (v4l2->width * 2 * v4l2->height > 720 * 240 * 2)
390 for (i = 0; i < dev->num_alt; i++) {
391 /* stop when the selected alt setting offers enough bandwidth */
392 if (dev->alt_max_pkt_size_isoc[i] >= min_pkt_size) {
395 /* otherwise make sure that we end up with the maximum bandwidth
396 because the min_pkt_size equation might be wrong...
398 } else if (dev->alt_max_pkt_size_isoc[i] >
399 dev->alt_max_pkt_size_isoc[dev->alt])
404 /* NOTE: for bulk transfers, we need to call usb_set_interface()
405 * even if the previous settings were the same. Otherwise streaming
406 * fails with all urbs having status = -EOVERFLOW ! */
407 if (dev->analog_xfer_bulk) {
408 dev->max_pkt_size = 512; /* USB 2.0 spec */
409 dev->packet_multiplier = EM28XX_BULK_PACKET_MULTIPLIER;
411 em28xx_videodbg("minimum isoc packet size: %u (alt=%d)\n",
412 min_pkt_size, dev->alt);
414 dev->alt_max_pkt_size_isoc[dev->alt];
415 dev->packet_multiplier = EM28XX_NUM_ISOC_PACKETS;
417 em28xx_videodbg("setting alternate %d with wMaxPacketSize=%u\n",
418 dev->alt, dev->max_pkt_size);
419 errCode = usb_set_interface(udev, dev->ifnum, dev->alt);
421 dev_err(&dev->intf->dev,
422 "cannot change alternate number to %d (error=%i)\n",
429 /* ------------------------------------------------------------------
430 DMA and thread functions
431 ------------------------------------------------------------------*/
434 * Finish the current buffer
436 static inline void finish_buffer(struct em28xx *dev,
437 struct em28xx_buffer *buf)
439 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
441 buf->vb.sequence = dev->v4l2->field_count++;
442 if (dev->v4l2->progressive)
443 buf->vb.field = V4L2_FIELD_NONE;
445 buf->vb.field = V4L2_FIELD_INTERLACED;
446 buf->vb.vb2_buf.timestamp = ktime_get_ns();
448 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
452 * Copy picture data from USB buffer to videobuf buffer
454 static void em28xx_copy_video(struct em28xx *dev,
455 struct em28xx_buffer *buf,
456 unsigned char *usb_buf,
459 struct em28xx_v4l2 *v4l2 = dev->v4l2;
460 void *fieldstart, *startwrite, *startread;
461 int linesdone, currlinedone, offset, lencopy, remain;
462 int bytesperline = v4l2->width << 1;
464 if (buf->pos + len > buf->length)
465 len = buf->length - buf->pos;
470 if (v4l2->progressive || buf->top_field)
471 fieldstart = buf->vb_buf;
472 else /* interlaced mode, even nr. of lines */
473 fieldstart = buf->vb_buf + bytesperline;
475 linesdone = buf->pos / bytesperline;
476 currlinedone = buf->pos % bytesperline;
478 if (v4l2->progressive)
479 offset = linesdone * bytesperline + currlinedone;
481 offset = linesdone * bytesperline * 2 + currlinedone;
483 startwrite = fieldstart + offset;
484 lencopy = bytesperline - currlinedone;
485 lencopy = lencopy > remain ? remain : lencopy;
487 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
488 em28xx_isocdbg("Overflow of %zu bytes past buffer end (1)\n",
489 ((char *)startwrite + lencopy) -
490 ((char *)buf->vb_buf + buf->length));
491 remain = (char *)buf->vb_buf + buf->length -
497 memcpy(startwrite, startread, lencopy);
502 if (v4l2->progressive)
503 startwrite += lencopy;
505 startwrite += lencopy + bytesperline;
506 startread += lencopy;
507 if (bytesperline > remain)
510 lencopy = bytesperline;
512 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
514 em28xx_isocdbg("Overflow of %zu bytes past buffer end(2)\n",
515 ((char *)startwrite + lencopy) -
516 ((char *)buf->vb_buf + buf->length));
517 lencopy = remain = (char *)buf->vb_buf + buf->length -
523 memcpy(startwrite, startread, lencopy);
532 * Copy VBI data from USB buffer to videobuf buffer
534 static void em28xx_copy_vbi(struct em28xx *dev,
535 struct em28xx_buffer *buf,
536 unsigned char *usb_buf,
541 if (buf->pos + len > buf->length)
542 len = buf->length - buf->pos;
545 /* Make sure the bottom field populates the second half of the frame */
546 if (buf->top_field == 0)
547 offset += dev->v4l2->vbi_width * dev->v4l2->vbi_height;
549 memcpy(buf->vb_buf + offset, usb_buf, len);
553 static inline void print_err_status(struct em28xx *dev,
554 int packet, int status)
556 char *errmsg = "Unknown";
560 errmsg = "unlinked synchronuously";
563 errmsg = "unlinked asynchronuously";
566 errmsg = "Buffer error (overrun)";
569 errmsg = "Stalled (device not responding)";
572 errmsg = "Babble (bad cable?)";
575 errmsg = "Bit-stuff error (bad cable?)";
578 errmsg = "CRC/Timeout (could be anything)";
581 errmsg = "Device does not respond";
585 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
587 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
588 packet, status, errmsg);
593 * get the next available buffer from dma queue
595 static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
596 struct em28xx_dmaqueue *dma_q)
598 struct em28xx_buffer *buf;
600 if (list_empty(&dma_q->active)) {
601 em28xx_isocdbg("No active queue to serve\n");
605 /* Get the next buffer */
606 buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
607 /* Cleans up buffer - Useful for testing for frame/URB loss */
608 list_del(&buf->list);
610 buf->vb_buf = buf->mem;
616 * Finish the current buffer if completed and prepare for the next field
618 static struct em28xx_buffer *
619 finish_field_prepare_next(struct em28xx *dev,
620 struct em28xx_buffer *buf,
621 struct em28xx_dmaqueue *dma_q)
623 struct em28xx_v4l2 *v4l2 = dev->v4l2;
625 if (v4l2->progressive || v4l2->top_field) { /* Brand new frame */
627 finish_buffer(dev, buf);
628 buf = get_next_buf(dev, dma_q);
631 buf->top_field = v4l2->top_field;
639 * Process data packet according to the em2710/em2750/em28xx frame data format
641 static inline void process_frame_data_em28xx(struct em28xx *dev,
642 unsigned char *data_pkt,
643 unsigned int data_len)
645 struct em28xx_v4l2 *v4l2 = dev->v4l2;
646 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
647 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
648 struct em28xx_dmaqueue *dma_q = &dev->vidq;
649 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
651 /* capture type 0 = vbi start
652 capture type 1 = vbi in progress
653 capture type 2 = video start
654 capture type 3 = video in progress */
656 /* NOTE: Headers are always 4 bytes and
657 * never split across packets */
658 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
659 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
663 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
664 /* Field start (VBI mode) */
665 v4l2->capture_type = 0;
667 em28xx_isocdbg("VBI START HEADER !!!\n");
668 v4l2->top_field = !(data_pkt[2] & 1);
671 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
672 /* Field start (VBI disabled) */
673 v4l2->capture_type = 2;
674 em28xx_isocdbg("VIDEO START HEADER !!!\n");
675 v4l2->top_field = !(data_pkt[2] & 1);
680 /* NOTE: With bulk transfers, intermediate data packets
681 * have no continuation header */
683 if (v4l2->capture_type == 0) {
684 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
685 dev->usb_ctl.vbi_buf = vbi_buf;
686 v4l2->capture_type = 1;
689 if (v4l2->capture_type == 1) {
690 int vbi_size = v4l2->vbi_width * v4l2->vbi_height;
691 int vbi_data_len = ((v4l2->vbi_read + data_len) > vbi_size) ?
692 (vbi_size - v4l2->vbi_read) : data_len;
696 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
697 v4l2->vbi_read += vbi_data_len;
699 if (vbi_data_len < data_len) {
700 /* Continue with copying video data */
701 v4l2->capture_type = 2;
702 data_pkt += vbi_data_len;
703 data_len -= vbi_data_len;
707 if (v4l2->capture_type == 2) {
708 buf = finish_field_prepare_next(dev, buf, dma_q);
709 dev->usb_ctl.vid_buf = buf;
710 v4l2->capture_type = 3;
713 if (v4l2->capture_type == 3 && buf != NULL && data_len > 0)
714 em28xx_copy_video(dev, buf, data_pkt, data_len);
718 * Process data packet according to the em25xx/em276x/7x/8x frame data format
720 static inline void process_frame_data_em25xx(struct em28xx *dev,
721 unsigned char *data_pkt,
722 unsigned int data_len)
724 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
725 struct em28xx_dmaqueue *dmaq = &dev->vidq;
726 struct em28xx_v4l2 *v4l2 = dev->v4l2;
727 bool frame_end = false;
729 /* Check for header */
730 /* NOTE: at least with bulk transfers, only the first packet
731 * has a header and has always set the FRAME_END bit */
732 if (data_len >= 2) { /* em25xx header is only 2 bytes long */
733 if ((data_pkt[0] == EM25XX_FRMDATAHDR_BYTE1) &&
734 ((data_pkt[1] & ~EM25XX_FRMDATAHDR_BYTE2_MASK) == 0x00)) {
735 v4l2->top_field = !(data_pkt[1] &
736 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID);
737 frame_end = data_pkt[1] &
738 EM25XX_FRMDATAHDR_BYTE2_FRAME_END;
743 /* Finish field and prepare next (BULK only) */
744 if (dev->analog_xfer_bulk && frame_end) {
745 buf = finish_field_prepare_next(dev, buf, dmaq);
746 dev->usb_ctl.vid_buf = buf;
748 /* NOTE: in ISOC mode when a new frame starts and buf==NULL,
749 * we COULD already prepare a buffer here to avoid skipping the
755 if (buf != NULL && data_len > 0)
756 em28xx_copy_video(dev, buf, data_pkt, data_len);
758 /* Finish frame (ISOC only) => avoids lag of 1 frame */
759 if (!dev->analog_xfer_bulk && frame_end) {
760 buf = finish_field_prepare_next(dev, buf, dmaq);
761 dev->usb_ctl.vid_buf = buf;
764 /* NOTE: Tested with USB bulk transfers only !
765 * The wording in the datasheet suggests that isoc might work different.
766 * The current code assumes that with isoc transfers each packet has a
767 * header like with the other em28xx devices.
769 /* NOTE: Support for interlaced mode is pure theory. It has not been
770 * tested and it is unknown if these devices actually support it. */
771 /* NOTE: No VBI support yet (these chips likely do not support VBI). */
774 /* Processes and copies the URB data content (video and VBI data) */
775 static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
777 int xfer_bulk, num_packets, i;
778 unsigned char *usb_data_pkt;
779 unsigned int usb_data_len;
784 if (dev->disconnected)
788 print_err_status(dev, -1, urb->status);
790 xfer_bulk = usb_pipebulk(urb->pipe);
792 if (xfer_bulk) /* bulk */
795 num_packets = urb->number_of_packets;
797 for (i = 0; i < num_packets; i++) {
798 if (xfer_bulk) { /* bulk */
799 usb_data_len = urb->actual_length;
801 usb_data_pkt = urb->transfer_buffer;
803 if (urb->iso_frame_desc[i].status < 0) {
804 print_err_status(dev, i,
805 urb->iso_frame_desc[i].status);
806 if (urb->iso_frame_desc[i].status != -EPROTO)
810 usb_data_len = urb->iso_frame_desc[i].actual_length;
811 if (usb_data_len > dev->max_pkt_size) {
812 em28xx_isocdbg("packet bigger than packet size");
816 usb_data_pkt = urb->transfer_buffer +
817 urb->iso_frame_desc[i].offset;
820 if (usb_data_len == 0) {
821 /* NOTE: happens very often with isoc transfers */
822 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
827 process_frame_data_em25xx(dev,
828 usb_data_pkt, usb_data_len);
830 process_frame_data_em28xx(dev,
831 usb_data_pkt, usb_data_len);
837 static int get_ressource(enum v4l2_buf_type f_type)
840 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
841 return EM28XX_RESOURCE_VIDEO;
842 case V4L2_BUF_TYPE_VBI_CAPTURE:
843 return EM28XX_RESOURCE_VBI;
849 /* Usage lock check functions */
850 static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
852 int res_type = get_ressource(f_type);
855 if (dev->resources & res_type) {
856 /* no, someone else uses it */
860 /* it's free, grab it */
861 dev->resources |= res_type;
862 em28xx_videodbg("res: get %d\n", res_type);
866 static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
868 int res_type = get_ressource(f_type);
870 dev->resources &= ~res_type;
871 em28xx_videodbg("res: put %d\n", res_type);
874 static void em28xx_v4l2_media_release(struct em28xx *dev)
876 #ifdef CONFIG_MEDIA_CONTROLLER
879 for (i = 0; i < MAX_EM28XX_INPUT; i++) {
882 media_device_unregister_entity(&dev->input_ent[i]);
888 * Media Controller helper functions
891 static int em28xx_enable_analog_tuner(struct em28xx *dev)
893 #ifdef CONFIG_MEDIA_CONTROLLER
894 struct media_device *mdev = dev->media_dev;
895 struct em28xx_v4l2 *v4l2 = dev->v4l2;
896 struct media_entity *source;
897 struct media_link *link, *found_link = NULL;
898 int ret, active_links = 0;
900 if (!mdev || !v4l2->decoder)
904 * This will find the tuner that is connected into the decoder.
905 * Technically, this is not 100% correct, as the device may be
906 * using an analog input instead of the tuner. However, as we can't
907 * do DVB streaming while the DMA engine is being used for V4L2,
908 * this should be enough for the actual needs.
910 list_for_each_entry(link, &v4l2->decoder->links, list) {
911 if (link->sink->entity == v4l2->decoder) {
913 if (link->flags & MEDIA_LNK_FL_ENABLED)
919 if (active_links == 1 || !found_link)
922 source = found_link->source->entity;
923 list_for_each_entry(link, &source->links, list) {
924 struct media_entity *sink;
927 sink = link->sink->entity;
929 if (sink == v4l2->decoder)
930 flags = MEDIA_LNK_FL_ENABLED;
932 ret = media_entity_setup_link(link, flags);
934 dev_err(&dev->intf->dev,
935 "Couldn't change link %s->%s to %s. Error %d\n",
936 source->name, sink->name,
937 flags ? "enabled" : "disabled",
941 em28xx_videodbg("link %s->%s was %s\n",
942 source->name, sink->name,
943 flags ? "ENABLED" : "disabled");
949 static const char * const iname[] = {
950 [EM28XX_VMUX_COMPOSITE] = "Composite",
951 [EM28XX_VMUX_SVIDEO] = "S-Video",
952 [EM28XX_VMUX_TELEVISION] = "Television",
953 [EM28XX_RADIO] = "Radio",
956 static void em28xx_v4l2_create_entities(struct em28xx *dev)
958 #if defined(CONFIG_MEDIA_CONTROLLER)
959 struct em28xx_v4l2 *v4l2 = dev->v4l2;
962 /* Initialize Video, VBI and Radio pads */
963 v4l2->video_pad.flags = MEDIA_PAD_FL_SINK;
964 ret = media_entity_pads_init(&v4l2->vdev.entity, 1, &v4l2->video_pad);
966 dev_err(&dev->intf->dev,
967 "failed to initialize video media entity!\n");
969 if (em28xx_vbi_supported(dev)) {
970 v4l2->vbi_pad.flags = MEDIA_PAD_FL_SINK;
971 ret = media_entity_pads_init(&v4l2->vbi_dev.entity, 1,
974 dev_err(&dev->intf->dev,
975 "failed to initialize vbi media entity!\n");
978 /* Webcams don't have input connectors */
979 if (dev->board.is_webcam)
982 /* Create entities for each input connector */
983 for (i = 0; i < MAX_EM28XX_INPUT; i++) {
984 struct media_entity *ent = &dev->input_ent[i];
989 ent->name = iname[INPUT(i)->type];
990 ent->flags = MEDIA_ENT_FL_CONNECTOR;
991 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
993 switch (INPUT(i)->type) {
994 case EM28XX_VMUX_COMPOSITE:
995 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
997 case EM28XX_VMUX_SVIDEO:
998 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1000 default: /* EM28XX_VMUX_TELEVISION or EM28XX_RADIO */
1001 if (dev->tuner_type != TUNER_ABSENT)
1002 ent->function = MEDIA_ENT_F_CONN_RF;
1006 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1008 dev_err(&dev->intf->dev,
1009 "failed to initialize input pad[%d]!\n", i);
1011 ret = media_device_register_entity(dev->media_dev, ent);
1013 dev_err(&dev->intf->dev,
1014 "failed to register input entity %d!\n", i);
1020 /* ------------------------------------------------------------------
1021 Videobuf2 operations
1022 ------------------------------------------------------------------*/
1024 static int queue_setup(struct vb2_queue *vq,
1025 unsigned int *nbuffers, unsigned int *nplanes,
1026 unsigned int sizes[], struct device *alloc_devs[])
1028 struct em28xx *dev = vb2_get_drv_priv(vq);
1029 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1030 unsigned long size =
1031 (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3;
1034 return sizes[0] < size ? -EINVAL : 0;
1038 em28xx_enable_analog_tuner(dev);
1044 buffer_prepare(struct vb2_buffer *vb)
1046 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1047 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
1048 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1051 em28xx_videodbg("%s, field=%d\n", __func__, vbuf->field);
1053 size = (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3;
1055 if (vb2_plane_size(vb, 0) < size) {
1056 em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
1057 __func__, vb2_plane_size(vb, 0), size);
1060 vb2_set_plane_payload(vb, 0, size);
1065 int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
1067 struct em28xx *dev = vb2_get_drv_priv(vq);
1068 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1069 struct v4l2_frequency f;
1070 struct v4l2_fh *owner;
1073 em28xx_videodbg("%s\n", __func__);
1075 dev->v4l2->field_count = 0;
1077 /* Make sure streaming is not already in progress for this type
1078 of filehandle (e.g. video, vbi) */
1079 rc = res_get(dev, vq->type);
1083 if (v4l2->streaming_users == 0) {
1084 /* First active streaming user, so allocate all the URBs */
1086 /* Allocate the USB bandwidth */
1087 em28xx_set_alternate(dev);
1089 /* Needed, since GPIO might have disabled power of
1092 em28xx_wake_i2c(dev);
1094 v4l2->capture_type = -1;
1095 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
1096 dev->analog_xfer_bulk,
1099 dev->packet_multiplier,
1100 em28xx_urb_data_copy);
1105 * djh: it's not clear whether this code is still needed. I'm
1106 * leaving it in here for now entirely out of concern for
1107 * backward compatibility (the old code did it)
1110 /* Ask tuner to go to analog or radio mode */
1111 memset(&f, 0, sizeof(f));
1112 f.frequency = v4l2->frequency;
1113 owner = (struct v4l2_fh *)vq->owner;
1114 if (owner && owner->vdev->vfl_type == VFL_TYPE_RADIO)
1115 f.type = V4L2_TUNER_RADIO;
1117 f.type = V4L2_TUNER_ANALOG_TV;
1118 v4l2_device_call_all(&v4l2->v4l2_dev,
1119 0, tuner, s_frequency, &f);
1121 /* Enable video stream at TV decoder */
1122 v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 1);
1125 v4l2->streaming_users++;
1130 static void em28xx_stop_streaming(struct vb2_queue *vq)
1132 struct em28xx *dev = vb2_get_drv_priv(vq);
1133 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1134 struct em28xx_dmaqueue *vidq = &dev->vidq;
1135 unsigned long flags = 0;
1137 em28xx_videodbg("%s\n", __func__);
1139 res_free(dev, vq->type);
1141 if (v4l2->streaming_users-- == 1) {
1142 /* Disable video stream at TV decoder */
1143 v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 0);
1145 /* Last active user, so shutdown all the URBS */
1146 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
1149 spin_lock_irqsave(&dev->slock, flags);
1150 if (dev->usb_ctl.vid_buf != NULL) {
1151 vb2_buffer_done(&dev->usb_ctl.vid_buf->vb.vb2_buf,
1152 VB2_BUF_STATE_ERROR);
1153 dev->usb_ctl.vid_buf = NULL;
1155 while (!list_empty(&vidq->active)) {
1156 struct em28xx_buffer *buf;
1158 buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
1159 list_del(&buf->list);
1160 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1162 spin_unlock_irqrestore(&dev->slock, flags);
1165 void em28xx_stop_vbi_streaming(struct vb2_queue *vq)
1167 struct em28xx *dev = vb2_get_drv_priv(vq);
1168 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1169 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
1170 unsigned long flags = 0;
1172 em28xx_videodbg("%s\n", __func__);
1174 res_free(dev, vq->type);
1176 if (v4l2->streaming_users-- == 1) {
1177 /* Disable video stream at TV decoder */
1178 v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 0);
1180 /* Last active user, so shutdown all the URBS */
1181 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
1184 spin_lock_irqsave(&dev->slock, flags);
1185 if (dev->usb_ctl.vbi_buf != NULL) {
1186 vb2_buffer_done(&dev->usb_ctl.vbi_buf->vb.vb2_buf,
1187 VB2_BUF_STATE_ERROR);
1188 dev->usb_ctl.vbi_buf = NULL;
1190 while (!list_empty(&vbiq->active)) {
1191 struct em28xx_buffer *buf;
1193 buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
1194 list_del(&buf->list);
1195 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1197 spin_unlock_irqrestore(&dev->slock, flags);
1201 buffer_queue(struct vb2_buffer *vb)
1203 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1204 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
1205 struct em28xx_buffer *buf =
1206 container_of(vbuf, struct em28xx_buffer, vb);
1207 struct em28xx_dmaqueue *vidq = &dev->vidq;
1208 unsigned long flags = 0;
1210 em28xx_videodbg("%s\n", __func__);
1211 buf->mem = vb2_plane_vaddr(vb, 0);
1212 buf->length = vb2_plane_size(vb, 0);
1214 spin_lock_irqsave(&dev->slock, flags);
1215 list_add_tail(&buf->list, &vidq->active);
1216 spin_unlock_irqrestore(&dev->slock, flags);
1219 static const struct vb2_ops em28xx_video_qops = {
1220 .queue_setup = queue_setup,
1221 .buf_prepare = buffer_prepare,
1222 .buf_queue = buffer_queue,
1223 .start_streaming = em28xx_start_analog_streaming,
1224 .stop_streaming = em28xx_stop_streaming,
1225 .wait_prepare = vb2_ops_wait_prepare,
1226 .wait_finish = vb2_ops_wait_finish,
1229 static int em28xx_vb2_setup(struct em28xx *dev)
1232 struct vb2_queue *q;
1233 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1235 /* Setup Videobuf2 for Video capture */
1237 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1238 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1239 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1241 q->buf_struct_size = sizeof(struct em28xx_buffer);
1242 q->ops = &em28xx_video_qops;
1243 q->mem_ops = &vb2_vmalloc_memops;
1245 rc = vb2_queue_init(q);
1249 /* Setup Videobuf2 for VBI capture */
1251 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1252 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
1253 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1255 q->buf_struct_size = sizeof(struct em28xx_buffer);
1256 q->ops = &em28xx_vbi_qops;
1257 q->mem_ops = &vb2_vmalloc_memops;
1259 rc = vb2_queue_init(q);
1266 /********************* v4l2 interface **************************************/
1268 static void video_mux(struct em28xx *dev, int index)
1270 struct v4l2_device *v4l2_dev = &dev->v4l2->v4l2_dev;
1272 dev->ctl_input = index;
1273 dev->ctl_ainput = INPUT(index)->amux;
1274 dev->ctl_aoutput = INPUT(index)->aout;
1276 if (!dev->ctl_aoutput)
1277 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1279 v4l2_device_call_all(v4l2_dev, 0, video, s_routing,
1280 INPUT(index)->vmux, 0, 0);
1282 if (dev->board.has_msp34xx) {
1283 if (dev->i2s_speed) {
1284 v4l2_device_call_all(v4l2_dev, 0, audio,
1285 s_i2s_clock_freq, dev->i2s_speed);
1287 /* Note: this is msp3400 specific */
1288 v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
1290 MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
1293 if (dev->board.adecoder != EM28XX_NOADECODER) {
1294 v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
1295 dev->ctl_ainput, dev->ctl_aoutput, 0);
1298 em28xx_audio_analog_set(dev);
1301 static void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
1303 struct em28xx *dev = priv;
1306 * In the case of non-AC97 volume controls, we still need
1307 * to do some setups at em28xx, in order to mute/unmute
1308 * and to adjust audio volume. However, the value ranges
1309 * should be checked by the corresponding V4L subdriver.
1312 case V4L2_CID_AUDIO_MUTE:
1313 dev->mute = ctrl->val;
1314 em28xx_audio_analog_set(dev);
1316 case V4L2_CID_AUDIO_VOLUME:
1317 dev->volume = ctrl->val;
1318 em28xx_audio_analog_set(dev);
1323 static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
1325 struct em28xx_v4l2 *v4l2 =
1326 container_of(ctrl->handler, struct em28xx_v4l2, ctrl_handler);
1327 struct em28xx *dev = v4l2->dev;
1331 case V4L2_CID_AUDIO_MUTE:
1332 dev->mute = ctrl->val;
1333 ret = em28xx_audio_analog_set(dev);
1335 case V4L2_CID_AUDIO_VOLUME:
1336 dev->volume = ctrl->val;
1337 ret = em28xx_audio_analog_set(dev);
1339 case V4L2_CID_CONTRAST:
1340 ret = em28xx_write_reg(dev, EM28XX_R20_YGAIN, ctrl->val);
1342 case V4L2_CID_BRIGHTNESS:
1343 ret = em28xx_write_reg(dev, EM28XX_R21_YOFFSET, ctrl->val);
1345 case V4L2_CID_SATURATION:
1346 ret = em28xx_write_reg(dev, EM28XX_R22_UVGAIN, ctrl->val);
1348 case V4L2_CID_BLUE_BALANCE:
1349 ret = em28xx_write_reg(dev, EM28XX_R23_UOFFSET, ctrl->val);
1351 case V4L2_CID_RED_BALANCE:
1352 ret = em28xx_write_reg(dev, EM28XX_R24_VOFFSET, ctrl->val);
1354 case V4L2_CID_SHARPNESS:
1355 ret = em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, ctrl->val);
1359 return (ret < 0) ? ret : 0;
1362 static const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
1363 .s_ctrl = em28xx_s_ctrl,
1366 static void size_to_scale(struct em28xx *dev,
1367 unsigned int width, unsigned int height,
1368 unsigned int *hscale, unsigned int *vscale)
1370 unsigned int maxw = norm_maxw(dev);
1371 unsigned int maxh = norm_maxh(dev);
1373 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1374 if (*hscale > EM28XX_HVSCALE_MAX)
1375 *hscale = EM28XX_HVSCALE_MAX;
1377 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1378 if (*vscale > EM28XX_HVSCALE_MAX)
1379 *vscale = EM28XX_HVSCALE_MAX;
1382 static void scale_to_size(struct em28xx *dev,
1383 unsigned int hscale, unsigned int vscale,
1384 unsigned int *width, unsigned int *height)
1386 unsigned int maxw = norm_maxw(dev);
1387 unsigned int maxh = norm_maxh(dev);
1389 *width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1390 *height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1392 /* Don't let width or height to be zero */
1399 /* ------------------------------------------------------------------
1400 IOCTL vidioc handling
1401 ------------------------------------------------------------------*/
1403 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1404 struct v4l2_format *f)
1406 struct em28xx *dev = video_drvdata(file);
1407 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1409 f->fmt.pix.width = v4l2->width;
1410 f->fmt.pix.height = v4l2->height;
1411 f->fmt.pix.pixelformat = v4l2->format->fourcc;
1412 f->fmt.pix.bytesperline = (v4l2->width * v4l2->format->depth + 7) >> 3;
1413 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * v4l2->height;
1414 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1416 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1417 if (v4l2->progressive)
1418 f->fmt.pix.field = V4L2_FIELD_NONE;
1420 f->fmt.pix.field = v4l2->interlaced_fieldmode ?
1421 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1425 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1429 for (i = 0; i < ARRAY_SIZE(format); i++)
1430 if (format[i].fourcc == fourcc)
1436 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1437 struct v4l2_format *f)
1439 struct em28xx *dev = video_drvdata(file);
1440 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1441 unsigned int width = f->fmt.pix.width;
1442 unsigned int height = f->fmt.pix.height;
1443 unsigned int maxw = norm_maxw(dev);
1444 unsigned int maxh = norm_maxh(dev);
1445 unsigned int hscale, vscale;
1446 struct em28xx_fmt *fmt;
1448 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1451 em28xx_videodbg("Fourcc format (%08x) invalid. Using default (%08x).\n",
1452 f->fmt.pix.pixelformat, fmt->fourcc);
1455 if (dev->board.is_em2800) {
1456 /* the em2800 can only scale down to 50% */
1457 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1458 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1460 * MaxPacketSize for em2800 is too small to capture at full
1461 * resolution use half of maxw as the scaler can only scale
1464 if (width == maxw && height == maxh)
1467 /* width must even because of the YUYV format
1468 height must be even because of interlacing */
1469 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1472 /* Avoid division by zero at size_to_scale */
1478 size_to_scale(dev, width, height, &hscale, &vscale);
1479 scale_to_size(dev, hscale, vscale, &width, &height);
1481 f->fmt.pix.width = width;
1482 f->fmt.pix.height = height;
1483 f->fmt.pix.pixelformat = fmt->fourcc;
1484 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
1485 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1486 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1487 if (v4l2->progressive)
1488 f->fmt.pix.field = V4L2_FIELD_NONE;
1490 f->fmt.pix.field = v4l2->interlaced_fieldmode ?
1491 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1492 f->fmt.pix.priv = 0;
1497 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1498 unsigned width, unsigned height)
1500 struct em28xx_fmt *fmt;
1501 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1503 fmt = format_by_fourcc(fourcc);
1508 v4l2->width = width;
1509 v4l2->height = height;
1511 /* set new image size */
1512 size_to_scale(dev, v4l2->width, v4l2->height,
1513 &v4l2->hscale, &v4l2->vscale);
1515 em28xx_resolution_set(dev);
1520 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1521 struct v4l2_format *f)
1523 struct em28xx *dev = video_drvdata(file);
1524 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1526 if (vb2_is_busy(&v4l2->vb_vidq))
1529 vidioc_try_fmt_vid_cap(file, priv, f);
1531 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1532 f->fmt.pix.width, f->fmt.pix.height);
1535 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1537 struct em28xx *dev = video_drvdata(file);
1539 *norm = dev->v4l2->norm;
1544 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1546 struct em28xx *dev = video_drvdata(file);
1548 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, video, querystd, norm);
1553 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1555 struct em28xx *dev = video_drvdata(file);
1556 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1557 struct v4l2_format f;
1559 if (norm == v4l2->norm)
1562 if (v4l2->streaming_users > 0)
1567 /* Adjusts width/height, if needed */
1568 f.fmt.pix.width = 720;
1569 f.fmt.pix.height = (norm & V4L2_STD_525_60) ? 480 : 576;
1570 vidioc_try_fmt_vid_cap(file, priv, &f);
1572 /* set new image size */
1573 v4l2->width = f.fmt.pix.width;
1574 v4l2->height = f.fmt.pix.height;
1575 size_to_scale(dev, v4l2->width, v4l2->height,
1576 &v4l2->hscale, &v4l2->vscale);
1578 em28xx_resolution_set(dev);
1579 v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
1584 static int vidioc_g_parm(struct file *file, void *priv,
1585 struct v4l2_streamparm *p)
1587 struct em28xx *dev = video_drvdata(file);
1588 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1591 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1592 if (dev->board.is_webcam)
1593 rc = v4l2_device_call_until_err(&v4l2->v4l2_dev, 0,
1596 v4l2_video_std_frame_period(v4l2->norm,
1597 &p->parm.capture.timeperframe);
1602 static int vidioc_s_parm(struct file *file, void *priv,
1603 struct v4l2_streamparm *p)
1605 struct em28xx *dev = video_drvdata(file);
1607 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1608 return v4l2_device_call_until_err(&dev->v4l2->v4l2_dev,
1609 0, video, s_parm, p);
1612 static int vidioc_enum_input(struct file *file, void *priv,
1613 struct v4l2_input *i)
1615 struct em28xx *dev = video_drvdata(file);
1619 if (n >= MAX_EM28XX_INPUT)
1621 if (0 == INPUT(n)->type)
1625 i->type = V4L2_INPUT_TYPE_CAMERA;
1627 strcpy(i->name, iname[INPUT(n)->type]);
1629 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type))
1630 i->type = V4L2_INPUT_TYPE_TUNER;
1632 i->std = dev->v4l2->vdev.tvnorms;
1633 /* webcams do not have the STD API */
1634 if (dev->board.is_webcam)
1635 i->capabilities = 0;
1640 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1642 struct em28xx *dev = video_drvdata(file);
1644 *i = dev->ctl_input;
1649 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1651 struct em28xx *dev = video_drvdata(file);
1653 if (i >= MAX_EM28XX_INPUT)
1655 if (0 == INPUT(i)->type)
1662 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1664 struct em28xx *dev = video_drvdata(file);
1667 case EM28XX_AMUX_VIDEO:
1668 strcpy(a->name, "Television");
1670 case EM28XX_AMUX_LINE_IN:
1671 strcpy(a->name, "Line In");
1673 case EM28XX_AMUX_VIDEO2:
1674 strcpy(a->name, "Television alt");
1676 case EM28XX_AMUX_PHONE:
1677 strcpy(a->name, "Phone");
1679 case EM28XX_AMUX_MIC:
1680 strcpy(a->name, "Mic");
1682 case EM28XX_AMUX_CD:
1683 strcpy(a->name, "CD");
1685 case EM28XX_AMUX_AUX:
1686 strcpy(a->name, "Aux");
1688 case EM28XX_AMUX_PCM_OUT:
1689 strcpy(a->name, "PCM");
1695 a->index = dev->ctl_ainput;
1696 a->capability = V4L2_AUDCAP_STEREO;
1701 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1703 struct em28xx *dev = video_drvdata(file);
1705 if (a->index >= MAX_EM28XX_INPUT)
1707 if (0 == INPUT(a->index)->type)
1710 dev->ctl_ainput = INPUT(a->index)->amux;
1711 dev->ctl_aoutput = INPUT(a->index)->aout;
1713 if (!dev->ctl_aoutput)
1714 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1719 static int vidioc_g_tuner(struct file *file, void *priv,
1720 struct v4l2_tuner *t)
1722 struct em28xx *dev = video_drvdata(file);
1727 strcpy(t->name, "Tuner");
1729 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
1733 static int vidioc_s_tuner(struct file *file, void *priv,
1734 const struct v4l2_tuner *t)
1736 struct em28xx *dev = video_drvdata(file);
1741 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
1745 static int vidioc_g_frequency(struct file *file, void *priv,
1746 struct v4l2_frequency *f)
1748 struct em28xx *dev = video_drvdata(file);
1749 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1754 f->frequency = v4l2->frequency;
1758 static int vidioc_s_frequency(struct file *file, void *priv,
1759 const struct v4l2_frequency *f)
1761 struct v4l2_frequency new_freq = *f;
1762 struct em28xx *dev = video_drvdata(file);
1763 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1768 v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_frequency, f);
1769 v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1770 v4l2->frequency = new_freq.frequency;
1775 #ifdef CONFIG_VIDEO_ADV_DEBUG
1776 static int vidioc_g_chip_info(struct file *file, void *priv,
1777 struct v4l2_dbg_chip_info *chip)
1779 struct em28xx *dev = video_drvdata(file);
1781 if (chip->match.addr > 1)
1783 if (chip->match.addr == 1)
1784 strlcpy(chip->name, "ac97", sizeof(chip->name));
1787 dev->v4l2->v4l2_dev.name, sizeof(chip->name));
1791 static int em28xx_reg_len(int reg)
1794 case EM28XX_R40_AC97LSB:
1795 case EM28XX_R30_HSCALELOW:
1796 case EM28XX_R32_VSCALELOW:
1803 static int vidioc_g_register(struct file *file, void *priv,
1804 struct v4l2_dbg_register *reg)
1806 struct em28xx *dev = video_drvdata(file);
1809 if (reg->match.addr > 1)
1811 if (reg->match.addr) {
1812 ret = em28xx_read_ac97(dev, reg->reg);
1822 reg->size = em28xx_reg_len(reg->reg);
1823 if (reg->size == 1) {
1824 ret = em28xx_read_reg(dev, reg->reg);
1833 ret = dev->em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1834 reg->reg, (char *)&val, 2);
1838 reg->val = le16_to_cpu(val);
1844 static int vidioc_s_register(struct file *file, void *priv,
1845 const struct v4l2_dbg_register *reg)
1847 struct em28xx *dev = video_drvdata(file);
1850 if (reg->match.addr > 1)
1852 if (reg->match.addr)
1853 return em28xx_write_ac97(dev, reg->reg, reg->val);
1856 buf = cpu_to_le16(reg->val);
1858 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1859 em28xx_reg_len(reg->reg));
1863 static int vidioc_querycap(struct file *file, void *priv,
1864 struct v4l2_capability *cap)
1866 struct video_device *vdev = video_devdata(file);
1867 struct em28xx *dev = video_drvdata(file);
1868 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1869 struct usb_device *udev = interface_to_usbdev(dev->intf);
1871 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1872 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1873 usb_make_path(udev, cap->bus_info, sizeof(cap->bus_info));
1875 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1876 cap->device_caps = V4L2_CAP_READWRITE |
1877 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1878 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1879 cap->device_caps = V4L2_CAP_RADIO;
1881 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
1883 if (dev->int_audio_type != EM28XX_INT_AUDIO_NONE)
1884 cap->device_caps |= V4L2_CAP_AUDIO;
1886 if (dev->tuner_type != TUNER_ABSENT)
1887 cap->device_caps |= V4L2_CAP_TUNER;
1889 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1890 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1891 if (video_is_registered(&v4l2->vbi_dev))
1892 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1893 if (video_is_registered(&v4l2->radio_dev))
1894 cap->capabilities |= V4L2_CAP_RADIO;
1898 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1899 struct v4l2_fmtdesc *f)
1901 if (unlikely(f->index >= ARRAY_SIZE(format)))
1904 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1905 f->pixelformat = format[f->index].fourcc;
1910 static int vidioc_enum_framesizes(struct file *file, void *priv,
1911 struct v4l2_frmsizeenum *fsize)
1913 struct em28xx *dev = video_drvdata(file);
1914 struct em28xx_fmt *fmt;
1915 unsigned int maxw = norm_maxw(dev);
1916 unsigned int maxh = norm_maxh(dev);
1918 fmt = format_by_fourcc(fsize->pixel_format);
1920 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1921 fsize->pixel_format);
1925 if (dev->board.is_em2800) {
1926 if (fsize->index > 1)
1928 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1929 fsize->discrete.width = maxw / (1 + fsize->index);
1930 fsize->discrete.height = maxh / (1 + fsize->index);
1934 if (fsize->index != 0)
1937 /* Report a continuous range */
1938 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1939 scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX,
1940 &fsize->stepwise.min_width, &fsize->stepwise.min_height);
1941 if (fsize->stepwise.min_width < 48)
1942 fsize->stepwise.min_width = 48;
1943 if (fsize->stepwise.min_height < 38)
1944 fsize->stepwise.min_height = 38;
1945 fsize->stepwise.max_width = maxw;
1946 fsize->stepwise.max_height = maxh;
1947 fsize->stepwise.step_width = 1;
1948 fsize->stepwise.step_height = 1;
1952 /* RAW VBI ioctls */
1954 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1955 struct v4l2_format *format)
1957 struct em28xx *dev = video_drvdata(file);
1958 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1960 format->fmt.vbi.samples_per_line = v4l2->vbi_width;
1961 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1962 format->fmt.vbi.offset = 0;
1963 format->fmt.vbi.flags = 0;
1964 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1965 format->fmt.vbi.count[0] = v4l2->vbi_height;
1966 format->fmt.vbi.count[1] = v4l2->vbi_height;
1967 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1969 /* Varies by video standard (NTSC, PAL, etc.) */
1970 if (v4l2->norm & V4L2_STD_525_60) {
1972 format->fmt.vbi.start[0] = 10;
1973 format->fmt.vbi.start[1] = 273;
1974 } else if (v4l2->norm & V4L2_STD_625_50) {
1976 format->fmt.vbi.start[0] = 6;
1977 format->fmt.vbi.start[1] = 318;
1983 /* ----------------------------------------------------------- */
1984 /* RADIO ESPECIFIC IOCTLS */
1985 /* ----------------------------------------------------------- */
1987 static int radio_g_tuner(struct file *file, void *priv,
1988 struct v4l2_tuner *t)
1990 struct em28xx *dev = video_drvdata(file);
1992 if (unlikely(t->index > 0))
1995 strcpy(t->name, "Radio");
1997 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
2002 static int radio_s_tuner(struct file *file, void *priv,
2003 const struct v4l2_tuner *t)
2005 struct em28xx *dev = video_drvdata(file);
2010 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
2016 * em28xx_free_v4l2() - Free struct em28xx_v4l2
2018 * @ref: struct kref for struct em28xx_v4l2
2020 * Called when all users of struct em28xx_v4l2 are gone
2022 static void em28xx_free_v4l2(struct kref *ref)
2024 struct em28xx_v4l2 *v4l2 = container_of(ref, struct em28xx_v4l2, ref);
2026 v4l2->dev->v4l2 = NULL;
2031 * em28xx_v4l2_open()
2032 * inits the device and starts isoc transfer
2034 static int em28xx_v4l2_open(struct file *filp)
2036 struct video_device *vdev = video_devdata(filp);
2037 struct em28xx *dev = video_drvdata(filp);
2038 struct em28xx_v4l2 *v4l2 = dev->v4l2;
2039 enum v4l2_buf_type fh_type = 0;
2042 switch (vdev->vfl_type) {
2043 case VFL_TYPE_GRABBER:
2044 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2047 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2049 case VFL_TYPE_RADIO:
2055 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2056 video_device_node_name(vdev), v4l2_type_names[fh_type],
2059 if (mutex_lock_interruptible(&dev->lock))
2060 return -ERESTARTSYS;
2062 ret = v4l2_fh_open(filp);
2064 dev_err(&dev->intf->dev,
2065 "%s: v4l2_fh_open() returned error %d\n",
2067 mutex_unlock(&dev->lock);
2071 if (v4l2->users == 0) {
2072 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
2074 if (vdev->vfl_type != VFL_TYPE_RADIO)
2075 em28xx_resolution_set(dev);
2078 * Needed, since GPIO might have disabled power
2079 * of some i2c devices
2081 em28xx_wake_i2c(dev);
2084 if (vdev->vfl_type == VFL_TYPE_RADIO) {
2085 em28xx_videodbg("video_open: setting radio device\n");
2086 v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_radio);
2089 kref_get(&dev->ref);
2090 kref_get(&v4l2->ref);
2093 mutex_unlock(&dev->lock);
2099 * em28xx_v4l2_fini()
2100 * unregisters the v4l2,i2c and usb devices
2101 * called when the device gets disconected or at module unload
2103 static int em28xx_v4l2_fini(struct em28xx *dev)
2105 struct em28xx_v4l2 *v4l2 = dev->v4l2;
2107 if (dev->is_audio_only) {
2108 /* Shouldn't initialize IR for this interface */
2112 if (!dev->has_video) {
2113 /* This device does not support the v4l2 extension */
2120 dev_info(&dev->intf->dev, "Closing video extension\n");
2122 mutex_lock(&dev->lock);
2124 v4l2_device_disconnect(&v4l2->v4l2_dev);
2126 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
2128 em28xx_v4l2_media_release(dev);
2130 if (video_is_registered(&v4l2->radio_dev)) {
2131 dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
2132 video_device_node_name(&v4l2->radio_dev));
2133 video_unregister_device(&v4l2->radio_dev);
2135 if (video_is_registered(&v4l2->vbi_dev)) {
2136 dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
2137 video_device_node_name(&v4l2->vbi_dev));
2138 video_unregister_device(&v4l2->vbi_dev);
2140 if (video_is_registered(&v4l2->vdev)) {
2141 dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
2142 video_device_node_name(&v4l2->vdev));
2143 video_unregister_device(&v4l2->vdev);
2146 v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
2147 v4l2_device_unregister(&v4l2->v4l2_dev);
2149 kref_put(&v4l2->ref, em28xx_free_v4l2);
2151 mutex_unlock(&dev->lock);
2153 kref_put(&dev->ref, em28xx_free_device);
2158 static int em28xx_v4l2_suspend(struct em28xx *dev)
2160 if (dev->is_audio_only)
2163 if (!dev->has_video)
2166 dev_info(&dev->intf->dev, "Suspending video extension\n");
2167 em28xx_stop_urbs(dev);
2171 static int em28xx_v4l2_resume(struct em28xx *dev)
2173 if (dev->is_audio_only)
2176 if (!dev->has_video)
2179 dev_info(&dev->intf->dev, "Resuming video extension\n");
2180 /* what do we do here */
2185 * em28xx_v4l2_close()
2186 * stops streaming and deallocates all resources allocated by the v4l2
2189 static int em28xx_v4l2_close(struct file *filp)
2191 struct em28xx *dev = video_drvdata(filp);
2192 struct em28xx_v4l2 *v4l2 = dev->v4l2;
2193 struct usb_device *udev = interface_to_usbdev(dev->intf);
2196 em28xx_videodbg("users=%d\n", v4l2->users);
2198 vb2_fop_release(filp);
2199 mutex_lock(&dev->lock);
2201 if (v4l2->users == 1) {
2202 /* No sense to try to write to the device */
2203 if (dev->disconnected)
2206 /* Save some power by putting tuner to sleep */
2207 v4l2_device_call_all(&v4l2->v4l2_dev, 0, core, s_power, 0);
2209 /* do this before setting alternate! */
2210 em28xx_set_mode(dev, EM28XX_SUSPEND);
2212 /* set alternate 0 */
2214 em28xx_videodbg("setting alternate 0\n");
2215 errCode = usb_set_interface(udev, 0, 0);
2217 dev_err(&dev->intf->dev,
2218 "cannot change alternate number to 0 (error=%i)\n",
2225 kref_put(&v4l2->ref, em28xx_free_v4l2);
2226 mutex_unlock(&dev->lock);
2227 kref_put(&dev->ref, em28xx_free_device);
2232 static const struct v4l2_file_operations em28xx_v4l_fops = {
2233 .owner = THIS_MODULE,
2234 .open = em28xx_v4l2_open,
2235 .release = em28xx_v4l2_close,
2236 .read = vb2_fop_read,
2237 .poll = vb2_fop_poll,
2238 .mmap = vb2_fop_mmap,
2239 .unlocked_ioctl = video_ioctl2,
2242 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2243 .vidioc_querycap = vidioc_querycap,
2244 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2245 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2246 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2247 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2248 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2249 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2250 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2251 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2252 .vidioc_g_audio = vidioc_g_audio,
2253 .vidioc_s_audio = vidioc_s_audio,
2255 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2256 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2257 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2258 .vidioc_querybuf = vb2_ioctl_querybuf,
2259 .vidioc_qbuf = vb2_ioctl_qbuf,
2260 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2262 .vidioc_g_std = vidioc_g_std,
2263 .vidioc_querystd = vidioc_querystd,
2264 .vidioc_s_std = vidioc_s_std,
2265 .vidioc_g_parm = vidioc_g_parm,
2266 .vidioc_s_parm = vidioc_s_parm,
2267 .vidioc_enum_input = vidioc_enum_input,
2268 .vidioc_g_input = vidioc_g_input,
2269 .vidioc_s_input = vidioc_s_input,
2270 .vidioc_streamon = vb2_ioctl_streamon,
2271 .vidioc_streamoff = vb2_ioctl_streamoff,
2272 .vidioc_g_tuner = vidioc_g_tuner,
2273 .vidioc_s_tuner = vidioc_s_tuner,
2274 .vidioc_g_frequency = vidioc_g_frequency,
2275 .vidioc_s_frequency = vidioc_s_frequency,
2276 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2277 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2278 #ifdef CONFIG_VIDEO_ADV_DEBUG
2279 .vidioc_g_chip_info = vidioc_g_chip_info,
2280 .vidioc_g_register = vidioc_g_register,
2281 .vidioc_s_register = vidioc_s_register,
2285 static const struct video_device em28xx_video_template = {
2286 .fops = &em28xx_v4l_fops,
2287 .ioctl_ops = &video_ioctl_ops,
2288 .release = video_device_release_empty,
2289 .tvnorms = V4L2_STD_ALL,
2292 static const struct v4l2_file_operations radio_fops = {
2293 .owner = THIS_MODULE,
2294 .open = em28xx_v4l2_open,
2295 .release = em28xx_v4l2_close,
2296 .unlocked_ioctl = video_ioctl2,
2299 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2300 .vidioc_querycap = vidioc_querycap,
2301 .vidioc_g_tuner = radio_g_tuner,
2302 .vidioc_s_tuner = radio_s_tuner,
2303 .vidioc_g_frequency = vidioc_g_frequency,
2304 .vidioc_s_frequency = vidioc_s_frequency,
2305 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2306 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2307 #ifdef CONFIG_VIDEO_ADV_DEBUG
2308 .vidioc_g_chip_info = vidioc_g_chip_info,
2309 .vidioc_g_register = vidioc_g_register,
2310 .vidioc_s_register = vidioc_s_register,
2314 static struct video_device em28xx_radio_template = {
2315 .fops = &radio_fops,
2316 .ioctl_ops = &radio_ioctl_ops,
2317 .release = video_device_release_empty,
2320 /* I2C possible address to saa7115, tvp5150, msp3400, tvaudio */
2321 static unsigned short saa711x_addrs[] = {
2322 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
2323 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
2326 static unsigned short tvp5150_addrs[] = {
2332 static unsigned short msp3400_addrs[] = {
2338 /******************************** usb interface ******************************/
2340 static void em28xx_vdev_init(struct em28xx *dev,
2341 struct video_device *vfd,
2342 const struct video_device *template,
2343 const char *type_name)
2346 vfd->v4l2_dev = &dev->v4l2->v4l2_dev;
2347 vfd->lock = &dev->lock;
2348 if (dev->board.is_webcam)
2351 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2352 dev_name(&dev->intf->dev), type_name);
2354 video_set_drvdata(vfd, dev);
2357 static void em28xx_tuner_setup(struct em28xx *dev, unsigned short tuner_addr)
2359 struct em28xx_v4l2 *v4l2 = dev->v4l2;
2360 struct v4l2_device *v4l2_dev = &v4l2->v4l2_dev;
2361 struct tuner_setup tun_setup;
2362 struct v4l2_frequency f;
2364 memset(&tun_setup, 0, sizeof(tun_setup));
2366 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
2367 tun_setup.tuner_callback = em28xx_tuner_callback;
2369 if (dev->board.radio.type) {
2370 tun_setup.type = dev->board.radio.type;
2371 tun_setup.addr = dev->board.radio_addr;
2373 v4l2_device_call_all(v4l2_dev,
2374 0, tuner, s_type_addr, &tun_setup);
2377 if ((dev->tuner_type != TUNER_ABSENT) && (dev->tuner_type)) {
2378 tun_setup.type = dev->tuner_type;
2379 tun_setup.addr = tuner_addr;
2381 v4l2_device_call_all(v4l2_dev,
2382 0, tuner, s_type_addr, &tun_setup);
2385 if (dev->board.tda9887_conf) {
2386 struct v4l2_priv_tun_config tda9887_cfg;
2388 tda9887_cfg.tuner = TUNER_TDA9887;
2389 tda9887_cfg.priv = &dev->board.tda9887_conf;
2391 v4l2_device_call_all(v4l2_dev,
2392 0, tuner, s_config, &tda9887_cfg);
2395 if (dev->tuner_type == TUNER_XC2028) {
2396 struct v4l2_priv_tun_config xc2028_cfg;
2397 struct xc2028_ctrl ctl;
2399 memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
2400 memset(&ctl, 0, sizeof(ctl));
2402 em28xx_setup_xc3028(dev, &ctl);
2404 xc2028_cfg.tuner = TUNER_XC2028;
2405 xc2028_cfg.priv = &ctl;
2407 v4l2_device_call_all(v4l2_dev, 0, tuner, s_config, &xc2028_cfg);
2410 /* configure tuner */
2412 f.type = V4L2_TUNER_ANALOG_TV;
2413 f.frequency = 9076; /* just a magic number */
2414 v4l2->frequency = f.frequency;
2415 v4l2_device_call_all(v4l2_dev, 0, tuner, s_frequency, &f);
2418 static int em28xx_v4l2_init(struct em28xx *dev)
2423 struct v4l2_ctrl_handler *hdl;
2424 struct em28xx_v4l2 *v4l2;
2426 if (dev->is_audio_only) {
2427 /* Shouldn't initialize IR for this interface */
2431 if (!dev->has_video) {
2432 /* This device does not support the v4l2 extension */
2436 dev_info(&dev->intf->dev, "Registering V4L2 extension\n");
2438 mutex_lock(&dev->lock);
2440 v4l2 = kzalloc(sizeof(struct em28xx_v4l2), GFP_KERNEL);
2442 mutex_unlock(&dev->lock);
2445 kref_init(&v4l2->ref);
2449 #ifdef CONFIG_MEDIA_CONTROLLER
2450 v4l2->v4l2_dev.mdev = dev->media_dev;
2452 ret = v4l2_device_register(&dev->intf->dev, &v4l2->v4l2_dev);
2454 dev_err(&dev->intf->dev,
2455 "Call to v4l2_device_register() failed!\n");
2459 hdl = &v4l2->ctrl_handler;
2460 v4l2_ctrl_handler_init(hdl, 8);
2461 v4l2->v4l2_dev.ctrl_handler = hdl;
2463 if (dev->board.is_webcam)
2464 v4l2->progressive = true;
2467 * Default format, used for tvp5150 or saa711x output formats
2469 v4l2->vinmode = EM28XX_VINMODE_YUV422_CbYCrY;
2470 v4l2->vinctl = EM28XX_VINCTRL_INTERLACED |
2471 EM28XX_VINCTRL_CCIR656_ENABLE;
2473 /* request some modules */
2475 if (dev->board.has_msp34xx)
2476 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2477 &dev->i2c_adap[dev->def_i2c_bus],
2478 "msp3400", 0, msp3400_addrs);
2480 if (dev->board.decoder == EM28XX_SAA711X)
2481 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2482 &dev->i2c_adap[dev->def_i2c_bus],
2483 "saa7115_auto", 0, saa711x_addrs);
2485 if (dev->board.decoder == EM28XX_TVP5150)
2486 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2487 &dev->i2c_adap[dev->def_i2c_bus],
2488 "tvp5150", 0, tvp5150_addrs);
2490 if (dev->board.adecoder == EM28XX_TVAUDIO)
2491 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2492 &dev->i2c_adap[dev->def_i2c_bus],
2493 "tvaudio", dev->board.tvaudio_addr, NULL);
2495 /* Initialize tuner and camera */
2497 if (dev->board.tuner_type != TUNER_ABSENT) {
2498 unsigned short tuner_addr = dev->board.tuner_addr;
2499 int has_demod = (dev->board.tda9887_conf & TDA9887_PRESENT);
2501 if (dev->board.radio.type)
2502 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2503 &dev->i2c_adap[dev->def_i2c_bus],
2504 "tuner", dev->board.radio_addr,
2508 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2509 &dev->i2c_adap[dev->def_i2c_bus],
2511 v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
2512 if (tuner_addr == 0) {
2513 enum v4l2_i2c_tuner_type type =
2514 has_demod ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
2515 struct v4l2_subdev *sd;
2517 sd = v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2518 &dev->i2c_adap[dev->def_i2c_bus],
2520 v4l2_i2c_tuner_addrs(type));
2523 tuner_addr = v4l2_i2c_subdev_addr(sd);
2525 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2526 &dev->i2c_adap[dev->def_i2c_bus],
2527 "tuner", tuner_addr, NULL);
2530 em28xx_tuner_setup(dev, tuner_addr);
2533 if (dev->em28xx_sensor != EM28XX_NOSENSOR)
2534 em28xx_init_camera(dev);
2536 /* Configure audio */
2537 ret = em28xx_audio_setup(dev);
2539 dev_err(&dev->intf->dev,
2540 "%s: Error while setting audio - error [%d]!\n",
2542 goto unregister_dev;
2544 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
2545 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2546 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2547 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2548 V4L2_CID_AUDIO_VOLUME, 0, 0x1f, 1, 0x1f);
2550 /* install the em28xx notify callback */
2551 v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_MUTE),
2552 em28xx_ctrl_notify, dev);
2553 v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_VOLUME),
2554 em28xx_ctrl_notify, dev);
2557 /* wake i2c devices */
2558 em28xx_wake_i2c(dev);
2560 /* init video dma queues */
2561 INIT_LIST_HEAD(&dev->vidq.active);
2562 INIT_LIST_HEAD(&dev->vbiq.active);
2564 if (dev->board.has_msp34xx) {
2565 /* Send a reset to other chips via gpio */
2566 ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xf7);
2568 dev_err(&dev->intf->dev,
2569 "%s: em28xx_write_reg - msp34xx(1) failed! error [%d]\n",
2571 goto unregister_dev;
2575 ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xff);
2577 dev_err(&dev->intf->dev,
2578 "%s: em28xx_write_reg - msp34xx(2) failed! error [%d]\n",
2580 goto unregister_dev;
2585 /* set default norm */
2586 v4l2->norm = V4L2_STD_PAL;
2587 v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
2588 v4l2->interlaced_fieldmode = EM28XX_INTERLACED_DEFAULT;
2590 /* Analog specific initialization */
2591 v4l2->format = &format[0];
2593 maxw = norm_maxw(dev);
2594 /* MaxPacketSize for em2800 is too small to capture at full resolution
2595 * use half of maxw as the scaler can only scale to 50% */
2596 if (dev->board.is_em2800)
2599 em28xx_set_video_format(dev, format[0].fourcc,
2600 maxw, norm_maxh(dev));
2604 /* Audio defaults */
2608 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2609 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2610 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2611 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2613 em28xx_set_outfmt(dev);
2615 /* Add image controls */
2616 /* NOTE: at this point, the subdevices are already registered, so bridge
2617 * controls are only added/enabled when no subdevice provides them */
2618 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_CONTRAST))
2619 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2621 0, 0x1f, 1, CONTRAST_DEFAULT);
2622 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_BRIGHTNESS))
2623 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2624 V4L2_CID_BRIGHTNESS,
2625 -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT);
2626 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_SATURATION))
2627 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2628 V4L2_CID_SATURATION,
2629 0, 0x1f, 1, SATURATION_DEFAULT);
2630 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_BLUE_BALANCE))
2631 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2632 V4L2_CID_BLUE_BALANCE,
2633 -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT);
2634 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_RED_BALANCE))
2635 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2636 V4L2_CID_RED_BALANCE,
2637 -0x30, 0x30, 1, RED_BALANCE_DEFAULT);
2638 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_SHARPNESS))
2639 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2641 0, 0x0f, 1, SHARPNESS_DEFAULT);
2643 /* Reset image controls */
2644 em28xx_colorlevels_set_default(dev);
2645 v4l2_ctrl_handler_setup(hdl);
2648 goto unregister_dev;
2650 /* allocate and fill video video_device struct */
2651 em28xx_vdev_init(dev, &v4l2->vdev, &em28xx_video_template, "video");
2652 mutex_init(&v4l2->vb_queue_lock);
2653 mutex_init(&v4l2->vb_vbi_queue_lock);
2654 v4l2->vdev.queue = &v4l2->vb_vidq;
2655 v4l2->vdev.queue->lock = &v4l2->vb_queue_lock;
2657 /* disable inapplicable ioctls */
2658 if (dev->board.is_webcam) {
2659 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_QUERYSTD);
2660 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_STD);
2661 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_STD);
2663 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_PARM);
2665 if (dev->tuner_type == TUNER_ABSENT) {
2666 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_TUNER);
2667 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_TUNER);
2668 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_FREQUENCY);
2669 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_FREQUENCY);
2671 if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
2672 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_AUDIO);
2673 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_AUDIO);
2676 /* register v4l2 video video_device */
2677 ret = video_register_device(&v4l2->vdev, VFL_TYPE_GRABBER,
2678 video_nr[dev->devno]);
2680 dev_err(&dev->intf->dev,
2681 "unable to register video device (error=%i).\n", ret);
2682 goto unregister_dev;
2685 /* Allocate and fill vbi video_device struct */
2686 if (em28xx_vbi_supported(dev) == 1) {
2687 em28xx_vdev_init(dev, &v4l2->vbi_dev, &em28xx_video_template,
2690 v4l2->vbi_dev.queue = &v4l2->vb_vbiq;
2691 v4l2->vbi_dev.queue->lock = &v4l2->vb_vbi_queue_lock;
2693 /* disable inapplicable ioctls */
2694 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_PARM);
2695 if (dev->tuner_type == TUNER_ABSENT) {
2696 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_TUNER);
2697 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_TUNER);
2698 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_FREQUENCY);
2699 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_FREQUENCY);
2701 if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
2702 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_AUDIO);
2703 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_AUDIO);
2706 /* register v4l2 vbi video_device */
2707 ret = video_register_device(&v4l2->vbi_dev, VFL_TYPE_VBI,
2708 vbi_nr[dev->devno]);
2710 dev_err(&dev->intf->dev,
2711 "unable to register vbi device\n");
2712 goto unregister_dev;
2716 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2717 em28xx_vdev_init(dev, &v4l2->radio_dev, &em28xx_radio_template,
2719 ret = video_register_device(&v4l2->radio_dev, VFL_TYPE_RADIO,
2720 radio_nr[dev->devno]);
2722 dev_err(&dev->intf->dev,
2723 "can't register radio device\n");
2724 goto unregister_dev;
2726 dev_info(&dev->intf->dev,
2727 "Registered radio device as %s\n",
2728 video_device_node_name(&v4l2->radio_dev));
2731 /* Init entities at the Media Controller */
2732 em28xx_v4l2_create_entities(dev);
2734 #ifdef CONFIG_MEDIA_CONTROLLER
2735 ret = v4l2_mc_create_media_graph(dev->media_dev);
2737 dev_err(&dev->intf->dev,
2738 "failed to create media graph\n");
2739 em28xx_v4l2_media_release(dev);
2740 goto unregister_dev;
2744 dev_info(&dev->intf->dev,
2745 "V4L2 video device registered as %s\n",
2746 video_device_node_name(&v4l2->vdev));
2748 if (video_is_registered(&v4l2->vbi_dev))
2749 dev_info(&dev->intf->dev,
2750 "V4L2 VBI device registered as %s\n",
2751 video_device_node_name(&v4l2->vbi_dev));
2753 /* Save some power by putting tuner to sleep */
2754 v4l2_device_call_all(&v4l2->v4l2_dev, 0, core, s_power, 0);
2756 /* initialize videobuf2 stuff */
2757 em28xx_vb2_setup(dev);
2759 dev_info(&dev->intf->dev,
2760 "V4L2 extension successfully initialized\n");
2762 kref_get(&dev->ref);
2764 mutex_unlock(&dev->lock);
2768 if (video_is_registered(&v4l2->radio_dev)) {
2769 dev_info(&dev->intf->dev,
2770 "V4L2 device %s deregistered\n",
2771 video_device_node_name(&v4l2->radio_dev));
2772 video_unregister_device(&v4l2->radio_dev);
2774 if (video_is_registered(&v4l2->vbi_dev)) {
2775 dev_info(&dev->intf->dev,
2776 "V4L2 device %s deregistered\n",
2777 video_device_node_name(&v4l2->vbi_dev));
2778 video_unregister_device(&v4l2->vbi_dev);
2780 if (video_is_registered(&v4l2->vdev)) {
2781 dev_info(&dev->intf->dev,
2782 "V4L2 device %s deregistered\n",
2783 video_device_node_name(&v4l2->vdev));
2784 video_unregister_device(&v4l2->vdev);
2787 v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
2788 v4l2_device_unregister(&v4l2->v4l2_dev);
2791 kref_put(&v4l2->ref, em28xx_free_v4l2);
2792 mutex_unlock(&dev->lock);
2796 static struct em28xx_ops v4l2_ops = {
2798 .name = "Em28xx v4l2 Extension",
2799 .init = em28xx_v4l2_init,
2800 .fini = em28xx_v4l2_fini,
2801 .suspend = em28xx_v4l2_suspend,
2802 .resume = em28xx_v4l2_resume,
2805 static int __init em28xx_video_register(void)
2807 return em28xx_register_extension(&v4l2_ops);
2810 static void __exit em28xx_video_unregister(void)
2812 em28xx_unregister_extension(&v4l2_ops);
2815 module_init(em28xx_video_register);
2816 module_exit(em28xx_video_unregister);