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/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);
85 #define EM25XX_FRMDATAHDR_BYTE1 0x02
86 #define EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE 0x20
87 #define EM25XX_FRMDATAHDR_BYTE2_FRAME_END 0x02
88 #define EM25XX_FRMDATAHDR_BYTE2_FRAME_ID 0x01
89 #define EM25XX_FRMDATAHDR_BYTE2_MASK (EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE | \
90 EM25XX_FRMDATAHDR_BYTE2_FRAME_END | \
91 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID)
94 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
95 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
96 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
98 module_param_array(video_nr, int, NULL, 0444);
99 module_param_array(vbi_nr, int, NULL, 0444);
100 module_param_array(radio_nr, int, NULL, 0444);
101 MODULE_PARM_DESC(video_nr, "video device numbers");
102 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
103 MODULE_PARM_DESC(radio_nr, "radio device numbers");
105 static unsigned int video_debug;
106 module_param(video_debug, int, 0644);
107 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
109 /* supported video standards */
110 static struct em28xx_fmt format[] = {
112 .name = "16 bpp YUY2, 4:2:2, packed",
113 .fourcc = V4L2_PIX_FMT_YUYV,
115 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
117 .name = "16 bpp RGB 565, LE",
118 .fourcc = V4L2_PIX_FMT_RGB565,
120 .reg = EM28XX_OUTFMT_RGB_16_656,
122 .name = "8 bpp Bayer BGBG..GRGR",
123 .fourcc = V4L2_PIX_FMT_SBGGR8,
125 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
127 .name = "8 bpp Bayer GRGR..BGBG",
128 .fourcc = V4L2_PIX_FMT_SGRBG8,
130 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
132 .name = "8 bpp Bayer GBGB..RGRG",
133 .fourcc = V4L2_PIX_FMT_SGBRG8,
135 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
137 .name = "12 bpp YUV411",
138 .fourcc = V4L2_PIX_FMT_YUV411P,
140 .reg = EM28XX_OUTFMT_YUV411,
144 /*FIXME: maxw should be dependent of alt mode */
145 static inline unsigned int norm_maxw(struct em28xx *dev)
147 struct em28xx_v4l2 *v4l2 = dev->v4l2;
149 if (dev->board.is_webcam)
150 return v4l2->sensor_xres;
152 if (dev->board.max_range_640_480)
158 static inline unsigned int norm_maxh(struct em28xx *dev)
160 struct em28xx_v4l2 *v4l2 = dev->v4l2;
162 if (dev->board.is_webcam)
163 return v4l2->sensor_yres;
165 if (dev->board.max_range_640_480)
168 return (v4l2->norm & V4L2_STD_625_50) ? 576 : 480;
171 static int em28xx_vbi_supported(struct em28xx *dev)
173 /* Modprobe option to manually disable */
174 if (disable_vbi == 1)
177 if (dev->board.is_webcam)
180 /* FIXME: check subdevices for VBI support */
182 if (dev->chip_id == CHIP_ID_EM2860 ||
183 dev->chip_id == CHIP_ID_EM2883)
186 /* Version of em28xx that does not support VBI */
192 * configure i2c attached devices
194 static void em28xx_wake_i2c(struct em28xx *dev)
196 struct v4l2_device *v4l2_dev = &dev->v4l2->v4l2_dev;
197 v4l2_device_call_all(v4l2_dev, 0, core, reset, 0);
198 v4l2_device_call_all(v4l2_dev, 0, video, s_routing,
199 INPUT(dev->ctl_input)->vmux, 0, 0);
200 v4l2_device_call_all(v4l2_dev, 0, video, s_stream, 0);
203 static int em28xx_colorlevels_set_default(struct em28xx *dev)
205 em28xx_write_reg(dev, EM28XX_R20_YGAIN, CONTRAST_DEFAULT);
206 em28xx_write_reg(dev, EM28XX_R21_YOFFSET, BRIGHTNESS_DEFAULT);
207 em28xx_write_reg(dev, EM28XX_R22_UVGAIN, SATURATION_DEFAULT);
208 em28xx_write_reg(dev, EM28XX_R23_UOFFSET, BLUE_BALANCE_DEFAULT);
209 em28xx_write_reg(dev, EM28XX_R24_VOFFSET, RED_BALANCE_DEFAULT);
210 em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, SHARPNESS_DEFAULT);
212 em28xx_write_reg(dev, EM28XX_R14_GAMMA, 0x20);
213 em28xx_write_reg(dev, EM28XX_R15_RGAIN, 0x20);
214 em28xx_write_reg(dev, EM28XX_R16_GGAIN, 0x20);
215 em28xx_write_reg(dev, EM28XX_R17_BGAIN, 0x20);
216 em28xx_write_reg(dev, EM28XX_R18_ROFFSET, 0x00);
217 em28xx_write_reg(dev, EM28XX_R19_GOFFSET, 0x00);
218 return em28xx_write_reg(dev, EM28XX_R1A_BOFFSET, 0x00);
221 static int em28xx_set_outfmt(struct em28xx *dev)
225 struct em28xx_v4l2 *v4l2 = dev->v4l2;
227 fmt = v4l2->format->reg;
231 * NOTE: it's not clear if this is really needed !
232 * The datasheets say bit 5 is a reserved bit and devices seem to work
233 * fine without it. But the Windows driver sets it for em2710/50+em28xx
234 * devices and we've always been setting it, too.
236 * em2765 (em25xx, em276x/7x/8x) devices do NOT work with this bit set,
237 * it's likely used for an additional (compressed ?) format there.
239 ret = em28xx_write_reg(dev, EM28XX_R27_OUTFMT, fmt);
243 ret = em28xx_write_reg(dev, EM28XX_R10_VINMODE, v4l2->vinmode);
247 vinctrl = v4l2->vinctl;
248 if (em28xx_vbi_supported(dev) == 1) {
249 vinctrl |= EM28XX_VINCTRL_VBI_RAW;
250 em28xx_write_reg(dev, EM28XX_R34_VBI_START_H, 0x00);
251 em28xx_write_reg(dev, EM28XX_R36_VBI_WIDTH, v4l2->vbi_width/4);
252 em28xx_write_reg(dev, EM28XX_R37_VBI_HEIGHT, v4l2->vbi_height);
253 if (v4l2->norm & V4L2_STD_525_60) {
255 em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x09);
256 } else if (v4l2->norm & V4L2_STD_625_50) {
258 em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x07);
262 return em28xx_write_reg(dev, EM28XX_R11_VINCTRL, vinctrl);
265 static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
268 em28xx_videodbg("em28xx Scale: (%d,%d)-(%d,%d)\n",
269 xmin, ymin, xmax, ymax);
271 em28xx_write_regs(dev, EM28XX_R28_XMIN, &xmin, 1);
272 em28xx_write_regs(dev, EM28XX_R29_XMAX, &xmax, 1);
273 em28xx_write_regs(dev, EM28XX_R2A_YMIN, &ymin, 1);
274 return em28xx_write_regs(dev, EM28XX_R2B_YMAX, &ymax, 1);
277 static void em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
278 u16 width, u16 height)
280 u8 cwidth = width >> 2;
281 u8 cheight = height >> 2;
282 u8 overflow = (height >> 9 & 0x02) | (width >> 10 & 0x01);
283 /* NOTE: size limit: 2047x1023 = 2MPix */
285 em28xx_videodbg("capture area set to (%d,%d): %dx%d\n",
287 ((overflow & 2) << 9 | cwidth << 2),
288 ((overflow & 1) << 10 | cheight << 2));
290 em28xx_write_regs(dev, EM28XX_R1C_HSTART, &hstart, 1);
291 em28xx_write_regs(dev, EM28XX_R1D_VSTART, &vstart, 1);
292 em28xx_write_regs(dev, EM28XX_R1E_CWIDTH, &cwidth, 1);
293 em28xx_write_regs(dev, EM28XX_R1F_CHEIGHT, &cheight, 1);
294 em28xx_write_regs(dev, EM28XX_R1B_OFLOW, &overflow, 1);
296 /* FIXME: function/meaning of these registers ? */
297 /* FIXME: align width+height to multiples of 4 ?! */
298 if (dev->is_em25xx) {
299 em28xx_write_reg(dev, 0x34, width >> 4);
300 em28xx_write_reg(dev, 0x35, height >> 4);
304 static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
307 /* the em2800 scaler only supports scaling down to 50% */
309 if (dev->board.is_em2800) {
310 mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
316 em28xx_write_regs(dev, EM28XX_R30_HSCALELOW, (char *)buf, 2);
320 em28xx_write_regs(dev, EM28XX_R32_VSCALELOW, (char *)buf, 2);
321 /* it seems that both H and V scalers must be active
323 mode = (h || v) ? 0x30 : 0x00;
325 return em28xx_write_reg(dev, EM28XX_R26_COMPR, mode);
328 /* FIXME: this only function read values from dev */
329 static int em28xx_resolution_set(struct em28xx *dev)
331 struct em28xx_v4l2 *v4l2 = dev->v4l2;
332 int width = norm_maxw(dev);
333 int height = norm_maxh(dev);
335 /* Properly setup VBI */
336 v4l2->vbi_width = 720;
337 if (v4l2->norm & V4L2_STD_525_60)
338 v4l2->vbi_height = 12;
340 v4l2->vbi_height = 18;
342 em28xx_set_outfmt(dev);
344 em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
346 /* If we don't set the start position to 2 in VBI mode, we end up
347 with line 20/21 being YUYV encoded instead of being in 8-bit
348 greyscale. The core of the issue is that line 21 (and line 23 for
349 PAL WSS) are inside of active video region, and as a result they
350 get the pixelformatting associated with that area. So by cropping
351 it out, we end up with the same format as the rest of the VBI
353 if (em28xx_vbi_supported(dev) == 1)
354 em28xx_capture_area_set(dev, 0, 2, width, height);
356 em28xx_capture_area_set(dev, 0, 0, width, height);
358 return em28xx_scaler_set(dev, v4l2->hscale, v4l2->vscale);
361 /* Set USB alternate setting for analog video */
362 static int em28xx_set_alternate(struct em28xx *dev)
364 struct em28xx_v4l2 *v4l2 = dev->v4l2;
367 unsigned int min_pkt_size = v4l2->width * 2 + 4;
369 /* NOTE: for isoc transfers, only alt settings > 0 are allowed
370 bulk transfers seem to work only with alt=0 ! */
372 if ((alt > 0) && (alt < dev->num_alt)) {
373 em28xx_videodbg("alternate forced to %d\n", dev->alt);
377 if (dev->analog_xfer_bulk)
380 /* When image size is bigger than a certain value,
381 the frame size should be increased, otherwise, only
382 green screen will be received.
384 if (v4l2->width * 2 * v4l2->height > 720 * 240 * 2)
387 for (i = 0; i < dev->num_alt; i++) {
388 /* stop when the selected alt setting offers enough bandwidth */
389 if (dev->alt_max_pkt_size_isoc[i] >= min_pkt_size) {
392 /* otherwise make sure that we end up with the maximum bandwidth
393 because the min_pkt_size equation might be wrong...
395 } else if (dev->alt_max_pkt_size_isoc[i] >
396 dev->alt_max_pkt_size_isoc[dev->alt])
401 /* NOTE: for bulk transfers, we need to call usb_set_interface()
402 * even if the previous settings were the same. Otherwise streaming
403 * fails with all urbs having status = -EOVERFLOW ! */
404 if (dev->analog_xfer_bulk) {
405 dev->max_pkt_size = 512; /* USB 2.0 spec */
406 dev->packet_multiplier = EM28XX_BULK_PACKET_MULTIPLIER;
408 em28xx_videodbg("minimum isoc packet size: %u (alt=%d)\n",
409 min_pkt_size, dev->alt);
411 dev->alt_max_pkt_size_isoc[dev->alt];
412 dev->packet_multiplier = EM28XX_NUM_ISOC_PACKETS;
414 em28xx_videodbg("setting alternate %d with wMaxPacketSize=%u\n",
415 dev->alt, dev->max_pkt_size);
416 errCode = usb_set_interface(dev->udev, dev->ifnum, dev->alt);
418 em28xx_errdev("cannot change alternate number to %d (error=%i)\n",
425 /* ------------------------------------------------------------------
426 DMA and thread functions
427 ------------------------------------------------------------------*/
430 * Finish the current buffer
432 static inline void finish_buffer(struct em28xx *dev,
433 struct em28xx_buffer *buf)
435 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
437 buf->vb.v4l2_buf.sequence = dev->v4l2->field_count++;
438 if (dev->v4l2->progressive)
439 buf->vb.v4l2_buf.field = V4L2_FIELD_NONE;
441 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
442 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
444 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
448 * Copy picture data from USB buffer to videobuf buffer
450 static void em28xx_copy_video(struct em28xx *dev,
451 struct em28xx_buffer *buf,
452 unsigned char *usb_buf,
455 struct em28xx_v4l2 *v4l2 = dev->v4l2;
456 void *fieldstart, *startwrite, *startread;
457 int linesdone, currlinedone, offset, lencopy, remain;
458 int bytesperline = v4l2->width << 1;
460 if (buf->pos + len > buf->length)
461 len = buf->length - buf->pos;
466 if (v4l2->progressive || buf->top_field)
467 fieldstart = buf->vb_buf;
468 else /* interlaced mode, even nr. of lines */
469 fieldstart = buf->vb_buf + bytesperline;
471 linesdone = buf->pos / bytesperline;
472 currlinedone = buf->pos % bytesperline;
474 if (v4l2->progressive)
475 offset = linesdone * bytesperline + currlinedone;
477 offset = linesdone * bytesperline * 2 + currlinedone;
479 startwrite = fieldstart + offset;
480 lencopy = bytesperline - currlinedone;
481 lencopy = lencopy > remain ? remain : lencopy;
483 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
484 em28xx_isocdbg("Overflow of %zu bytes past buffer end (1)\n",
485 ((char *)startwrite + lencopy) -
486 ((char *)buf->vb_buf + buf->length));
487 remain = (char *)buf->vb_buf + buf->length -
493 memcpy(startwrite, startread, lencopy);
498 if (v4l2->progressive)
499 startwrite += lencopy;
501 startwrite += lencopy + bytesperline;
502 startread += lencopy;
503 if (bytesperline > remain)
506 lencopy = bytesperline;
508 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
510 em28xx_isocdbg("Overflow of %zu bytes past buffer end"
512 ((char *)startwrite + lencopy) -
513 ((char *)buf->vb_buf + buf->length));
514 lencopy = remain = (char *)buf->vb_buf + buf->length -
520 memcpy(startwrite, startread, lencopy);
529 * Copy VBI data from USB buffer to videobuf buffer
531 static void em28xx_copy_vbi(struct em28xx *dev,
532 struct em28xx_buffer *buf,
533 unsigned char *usb_buf,
538 if (buf->pos + len > buf->length)
539 len = buf->length - buf->pos;
542 /* Make sure the bottom field populates the second half of the frame */
543 if (buf->top_field == 0)
544 offset += dev->v4l2->vbi_width * dev->v4l2->vbi_height;
546 memcpy(buf->vb_buf + offset, usb_buf, len);
550 static inline void print_err_status(struct em28xx *dev,
551 int packet, int status)
553 char *errmsg = "Unknown";
557 errmsg = "unlinked synchronuously";
560 errmsg = "unlinked asynchronuously";
563 errmsg = "Buffer error (overrun)";
566 errmsg = "Stalled (device not responding)";
569 errmsg = "Babble (bad cable?)";
572 errmsg = "Bit-stuff error (bad cable?)";
575 errmsg = "CRC/Timeout (could be anything)";
578 errmsg = "Device does not respond";
582 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
584 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
585 packet, status, errmsg);
590 * get the next available buffer from dma queue
592 static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
593 struct em28xx_dmaqueue *dma_q)
595 struct em28xx_buffer *buf;
597 if (list_empty(&dma_q->active)) {
598 em28xx_isocdbg("No active queue to serve\n");
602 /* Get the next buffer */
603 buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
604 /* Cleans up buffer - Useful for testing for frame/URB loss */
605 list_del(&buf->list);
607 buf->vb_buf = buf->mem;
613 * Finish the current buffer if completed and prepare for the next field
615 static struct em28xx_buffer *
616 finish_field_prepare_next(struct em28xx *dev,
617 struct em28xx_buffer *buf,
618 struct em28xx_dmaqueue *dma_q)
620 struct em28xx_v4l2 *v4l2 = dev->v4l2;
622 if (v4l2->progressive || v4l2->top_field) { /* Brand new frame */
624 finish_buffer(dev, buf);
625 buf = get_next_buf(dev, dma_q);
628 buf->top_field = v4l2->top_field;
636 * Process data packet according to the em2710/em2750/em28xx frame data format
638 static inline void process_frame_data_em28xx(struct em28xx *dev,
639 unsigned char *data_pkt,
640 unsigned int data_len)
642 struct em28xx_v4l2 *v4l2 = dev->v4l2;
643 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
644 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
645 struct em28xx_dmaqueue *dma_q = &dev->vidq;
646 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
648 /* capture type 0 = vbi start
649 capture type 1 = vbi in progress
650 capture type 2 = video start
651 capture type 3 = video in progress */
653 /* NOTE: Headers are always 4 bytes and
654 * never split across packets */
655 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
656 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
660 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
661 /* Field start (VBI mode) */
662 v4l2->capture_type = 0;
664 em28xx_isocdbg("VBI START HEADER !!!\n");
665 v4l2->top_field = !(data_pkt[2] & 1);
668 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
669 /* Field start (VBI disabled) */
670 v4l2->capture_type = 2;
671 em28xx_isocdbg("VIDEO START HEADER !!!\n");
672 v4l2->top_field = !(data_pkt[2] & 1);
677 /* NOTE: With bulk transfers, intermediate data packets
678 * have no continuation header */
680 if (v4l2->capture_type == 0) {
681 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
682 dev->usb_ctl.vbi_buf = vbi_buf;
683 v4l2->capture_type = 1;
686 if (v4l2->capture_type == 1) {
687 int vbi_size = v4l2->vbi_width * v4l2->vbi_height;
688 int vbi_data_len = ((v4l2->vbi_read + data_len) > vbi_size) ?
689 (vbi_size - v4l2->vbi_read) : data_len;
693 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
694 v4l2->vbi_read += vbi_data_len;
696 if (vbi_data_len < data_len) {
697 /* Continue with copying video data */
698 v4l2->capture_type = 2;
699 data_pkt += vbi_data_len;
700 data_len -= vbi_data_len;
704 if (v4l2->capture_type == 2) {
705 buf = finish_field_prepare_next(dev, buf, dma_q);
706 dev->usb_ctl.vid_buf = buf;
707 v4l2->capture_type = 3;
710 if (v4l2->capture_type == 3 && buf != NULL && data_len > 0)
711 em28xx_copy_video(dev, buf, data_pkt, data_len);
715 * Process data packet according to the em25xx/em276x/7x/8x frame data format
717 static inline void process_frame_data_em25xx(struct em28xx *dev,
718 unsigned char *data_pkt,
719 unsigned int data_len)
721 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
722 struct em28xx_dmaqueue *dmaq = &dev->vidq;
723 struct em28xx_v4l2 *v4l2 = dev->v4l2;
724 bool frame_end = false;
726 /* Check for header */
727 /* NOTE: at least with bulk transfers, only the first packet
728 * has a header and has always set the FRAME_END bit */
729 if (data_len >= 2) { /* em25xx header is only 2 bytes long */
730 if ((data_pkt[0] == EM25XX_FRMDATAHDR_BYTE1) &&
731 ((data_pkt[1] & ~EM25XX_FRMDATAHDR_BYTE2_MASK) == 0x00)) {
732 v4l2->top_field = !(data_pkt[1] &
733 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID);
734 frame_end = data_pkt[1] &
735 EM25XX_FRMDATAHDR_BYTE2_FRAME_END;
740 /* Finish field and prepare next (BULK only) */
741 if (dev->analog_xfer_bulk && frame_end) {
742 buf = finish_field_prepare_next(dev, buf, dmaq);
743 dev->usb_ctl.vid_buf = buf;
745 /* NOTE: in ISOC mode when a new frame starts and buf==NULL,
746 * we COULD already prepare a buffer here to avoid skipping the
752 if (buf != NULL && data_len > 0)
753 em28xx_copy_video(dev, buf, data_pkt, data_len);
755 /* Finish frame (ISOC only) => avoids lag of 1 frame */
756 if (!dev->analog_xfer_bulk && frame_end) {
757 buf = finish_field_prepare_next(dev, buf, dmaq);
758 dev->usb_ctl.vid_buf = buf;
761 /* NOTE: Tested with USB bulk transfers only !
762 * The wording in the datasheet suggests that isoc might work different.
763 * The current code assumes that with isoc transfers each packet has a
764 * header like with the other em28xx devices.
766 /* NOTE: Support for interlaced mode is pure theory. It has not been
767 * tested and it is unknown if these devices actually support it. */
768 /* NOTE: No VBI support yet (these chips likely do not support VBI). */
771 /* Processes and copies the URB data content (video and VBI data) */
772 static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
774 int xfer_bulk, num_packets, i;
775 unsigned char *usb_data_pkt;
776 unsigned int usb_data_len;
781 if (dev->disconnected)
785 print_err_status(dev, -1, urb->status);
787 xfer_bulk = usb_pipebulk(urb->pipe);
789 if (xfer_bulk) /* bulk */
792 num_packets = urb->number_of_packets;
794 for (i = 0; i < num_packets; i++) {
795 if (xfer_bulk) { /* bulk */
796 usb_data_len = urb->actual_length;
798 usb_data_pkt = urb->transfer_buffer;
800 if (urb->iso_frame_desc[i].status < 0) {
801 print_err_status(dev, i,
802 urb->iso_frame_desc[i].status);
803 if (urb->iso_frame_desc[i].status != -EPROTO)
807 usb_data_len = urb->iso_frame_desc[i].actual_length;
808 if (usb_data_len > dev->max_pkt_size) {
809 em28xx_isocdbg("packet bigger than packet size");
813 usb_data_pkt = urb->transfer_buffer +
814 urb->iso_frame_desc[i].offset;
817 if (usb_data_len == 0) {
818 /* NOTE: happens very often with isoc transfers */
819 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
824 process_frame_data_em25xx(dev,
825 usb_data_pkt, usb_data_len);
827 process_frame_data_em28xx(dev,
828 usb_data_pkt, usb_data_len);
835 static int get_ressource(enum v4l2_buf_type f_type)
838 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
839 return EM28XX_RESOURCE_VIDEO;
840 case V4L2_BUF_TYPE_VBI_CAPTURE:
841 return EM28XX_RESOURCE_VBI;
848 /* Usage lock check functions */
849 static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
851 int res_type = get_ressource(f_type);
854 if (dev->resources & res_type) {
855 /* no, someone else uses it */
859 /* it's free, grab it */
860 dev->resources |= res_type;
861 em28xx_videodbg("res: get %d\n", res_type);
865 static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
867 int res_type = get_ressource(f_type);
869 dev->resources &= ~res_type;
870 em28xx_videodbg("res: put %d\n", res_type);
873 /* ------------------------------------------------------------------
875 ------------------------------------------------------------------*/
877 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
878 unsigned int *nbuffers, unsigned int *nplanes,
879 unsigned int sizes[], void *alloc_ctxs[])
881 struct em28xx *dev = vb2_get_drv_priv(vq);
882 struct em28xx_v4l2 *v4l2 = dev->v4l2;
886 size = fmt->fmt.pix.sizeimage;
889 (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3;
904 buffer_prepare(struct vb2_buffer *vb)
906 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
907 struct em28xx_v4l2 *v4l2 = dev->v4l2;
908 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
911 em28xx_videodbg("%s, field=%d\n", __func__, vb->v4l2_buf.field);
913 size = (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3;
915 if (vb2_plane_size(vb, 0) < size) {
916 em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
917 __func__, vb2_plane_size(vb, 0), size);
920 vb2_set_plane_payload(&buf->vb, 0, size);
925 int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
927 struct em28xx *dev = vb2_get_drv_priv(vq);
928 struct em28xx_v4l2 *v4l2 = dev->v4l2;
929 struct v4l2_frequency f;
932 em28xx_videodbg("%s\n", __func__);
934 /* Make sure streaming is not already in progress for this type
935 of filehandle (e.g. video, vbi) */
936 rc = res_get(dev, vq->type);
940 if (v4l2->streaming_users == 0) {
941 /* First active streaming user, so allocate all the URBs */
943 /* Allocate the USB bandwidth */
944 em28xx_set_alternate(dev);
946 /* Needed, since GPIO might have disabled power of
949 em28xx_wake_i2c(dev);
951 v4l2->capture_type = -1;
952 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
953 dev->analog_xfer_bulk,
956 dev->packet_multiplier,
957 em28xx_urb_data_copy);
962 * djh: it's not clear whether this code is still needed. I'm
963 * leaving it in here for now entirely out of concern for
964 * backward compatibility (the old code did it)
967 /* Ask tuner to go to analog or radio mode */
968 memset(&f, 0, sizeof(f));
969 f.frequency = v4l2->frequency;
970 if (vq->owner && vq->owner->vdev->vfl_type == VFL_TYPE_RADIO)
971 f.type = V4L2_TUNER_RADIO;
973 f.type = V4L2_TUNER_ANALOG_TV;
974 v4l2_device_call_all(&v4l2->v4l2_dev,
975 0, tuner, s_frequency, &f);
978 v4l2->streaming_users++;
983 static void em28xx_stop_streaming(struct vb2_queue *vq)
985 struct em28xx *dev = vb2_get_drv_priv(vq);
986 struct em28xx_v4l2 *v4l2 = dev->v4l2;
987 struct em28xx_dmaqueue *vidq = &dev->vidq;
988 unsigned long flags = 0;
990 em28xx_videodbg("%s\n", __func__);
992 res_free(dev, vq->type);
994 if (v4l2->streaming_users-- == 1) {
995 /* Last active user, so shutdown all the URBS */
996 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
999 spin_lock_irqsave(&dev->slock, flags);
1000 if (dev->usb_ctl.vid_buf != NULL) {
1001 vb2_buffer_done(&dev->usb_ctl.vid_buf->vb, VB2_BUF_STATE_ERROR);
1002 dev->usb_ctl.vid_buf = NULL;
1004 while (!list_empty(&vidq->active)) {
1005 struct em28xx_buffer *buf;
1006 buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
1007 list_del(&buf->list);
1008 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1010 spin_unlock_irqrestore(&dev->slock, flags);
1013 void em28xx_stop_vbi_streaming(struct vb2_queue *vq)
1015 struct em28xx *dev = vb2_get_drv_priv(vq);
1016 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1017 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
1018 unsigned long flags = 0;
1020 em28xx_videodbg("%s\n", __func__);
1022 res_free(dev, vq->type);
1024 if (v4l2->streaming_users-- == 1) {
1025 /* Last active user, so shutdown all the URBS */
1026 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
1029 spin_lock_irqsave(&dev->slock, flags);
1030 if (dev->usb_ctl.vbi_buf != NULL) {
1031 vb2_buffer_done(&dev->usb_ctl.vbi_buf->vb, VB2_BUF_STATE_ERROR);
1032 dev->usb_ctl.vbi_buf = NULL;
1034 while (!list_empty(&vbiq->active)) {
1035 struct em28xx_buffer *buf;
1036 buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
1037 list_del(&buf->list);
1038 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1040 spin_unlock_irqrestore(&dev->slock, flags);
1044 buffer_queue(struct vb2_buffer *vb)
1046 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
1047 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
1048 struct em28xx_dmaqueue *vidq = &dev->vidq;
1049 unsigned long flags = 0;
1051 em28xx_videodbg("%s\n", __func__);
1052 buf->mem = vb2_plane_vaddr(vb, 0);
1053 buf->length = vb2_plane_size(vb, 0);
1055 spin_lock_irqsave(&dev->slock, flags);
1056 list_add_tail(&buf->list, &vidq->active);
1057 spin_unlock_irqrestore(&dev->slock, flags);
1060 static struct vb2_ops em28xx_video_qops = {
1061 .queue_setup = queue_setup,
1062 .buf_prepare = buffer_prepare,
1063 .buf_queue = buffer_queue,
1064 .start_streaming = em28xx_start_analog_streaming,
1065 .stop_streaming = em28xx_stop_streaming,
1066 .wait_prepare = vb2_ops_wait_prepare,
1067 .wait_finish = vb2_ops_wait_finish,
1070 static int em28xx_vb2_setup(struct em28xx *dev)
1073 struct vb2_queue *q;
1074 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1076 /* Setup Videobuf2 for Video capture */
1078 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1079 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1080 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1082 q->buf_struct_size = sizeof(struct em28xx_buffer);
1083 q->ops = &em28xx_video_qops;
1084 q->mem_ops = &vb2_vmalloc_memops;
1086 rc = vb2_queue_init(q);
1090 /* Setup Videobuf2 for VBI capture */
1092 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1093 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
1094 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1096 q->buf_struct_size = sizeof(struct em28xx_buffer);
1097 q->ops = &em28xx_vbi_qops;
1098 q->mem_ops = &vb2_vmalloc_memops;
1100 rc = vb2_queue_init(q);
1107 /********************* v4l2 interface **************************************/
1109 static void video_mux(struct em28xx *dev, int index)
1111 struct v4l2_device *v4l2_dev = &dev->v4l2->v4l2_dev;
1112 dev->ctl_input = index;
1113 dev->ctl_ainput = INPUT(index)->amux;
1114 dev->ctl_aoutput = INPUT(index)->aout;
1116 if (!dev->ctl_aoutput)
1117 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1119 v4l2_device_call_all(v4l2_dev, 0, video, s_routing,
1120 INPUT(index)->vmux, 0, 0);
1122 if (dev->board.has_msp34xx) {
1123 if (dev->i2s_speed) {
1124 v4l2_device_call_all(v4l2_dev, 0, audio,
1125 s_i2s_clock_freq, dev->i2s_speed);
1127 /* Note: this is msp3400 specific */
1128 v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
1129 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
1132 if (dev->board.adecoder != EM28XX_NOADECODER) {
1133 v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
1134 dev->ctl_ainput, dev->ctl_aoutput, 0);
1137 em28xx_audio_analog_set(dev);
1140 static void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
1142 struct em28xx *dev = priv;
1145 * In the case of non-AC97 volume controls, we still need
1146 * to do some setups at em28xx, in order to mute/unmute
1147 * and to adjust audio volume. However, the value ranges
1148 * should be checked by the corresponding V4L subdriver.
1151 case V4L2_CID_AUDIO_MUTE:
1152 dev->mute = ctrl->val;
1153 em28xx_audio_analog_set(dev);
1155 case V4L2_CID_AUDIO_VOLUME:
1156 dev->volume = ctrl->val;
1157 em28xx_audio_analog_set(dev);
1162 static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
1164 struct em28xx_v4l2 *v4l2 =
1165 container_of(ctrl->handler, struct em28xx_v4l2, ctrl_handler);
1166 struct em28xx *dev = v4l2->dev;
1170 case V4L2_CID_AUDIO_MUTE:
1171 dev->mute = ctrl->val;
1172 ret = em28xx_audio_analog_set(dev);
1174 case V4L2_CID_AUDIO_VOLUME:
1175 dev->volume = ctrl->val;
1176 ret = em28xx_audio_analog_set(dev);
1178 case V4L2_CID_CONTRAST:
1179 ret = em28xx_write_reg(dev, EM28XX_R20_YGAIN, ctrl->val);
1181 case V4L2_CID_BRIGHTNESS:
1182 ret = em28xx_write_reg(dev, EM28XX_R21_YOFFSET, ctrl->val);
1184 case V4L2_CID_SATURATION:
1185 ret = em28xx_write_reg(dev, EM28XX_R22_UVGAIN, ctrl->val);
1187 case V4L2_CID_BLUE_BALANCE:
1188 ret = em28xx_write_reg(dev, EM28XX_R23_UOFFSET, ctrl->val);
1190 case V4L2_CID_RED_BALANCE:
1191 ret = em28xx_write_reg(dev, EM28XX_R24_VOFFSET, ctrl->val);
1193 case V4L2_CID_SHARPNESS:
1194 ret = em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, ctrl->val);
1198 return (ret < 0) ? ret : 0;
1201 static const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
1202 .s_ctrl = em28xx_s_ctrl,
1205 static void size_to_scale(struct em28xx *dev,
1206 unsigned int width, unsigned int height,
1207 unsigned int *hscale, unsigned int *vscale)
1209 unsigned int maxw = norm_maxw(dev);
1210 unsigned int maxh = norm_maxh(dev);
1212 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1213 if (*hscale > EM28XX_HVSCALE_MAX)
1214 *hscale = EM28XX_HVSCALE_MAX;
1216 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1217 if (*vscale > EM28XX_HVSCALE_MAX)
1218 *vscale = EM28XX_HVSCALE_MAX;
1221 static void scale_to_size(struct em28xx *dev,
1222 unsigned int hscale, unsigned int vscale,
1223 unsigned int *width, unsigned int *height)
1225 unsigned int maxw = norm_maxw(dev);
1226 unsigned int maxh = norm_maxh(dev);
1228 *width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1229 *height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1232 /* ------------------------------------------------------------------
1233 IOCTL vidioc handling
1234 ------------------------------------------------------------------*/
1236 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1237 struct v4l2_format *f)
1239 struct em28xx *dev = video_drvdata(file);
1240 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1242 f->fmt.pix.width = v4l2->width;
1243 f->fmt.pix.height = v4l2->height;
1244 f->fmt.pix.pixelformat = v4l2->format->fourcc;
1245 f->fmt.pix.bytesperline = (v4l2->width * v4l2->format->depth + 7) >> 3;
1246 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * v4l2->height;
1247 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1249 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1250 if (v4l2->progressive)
1251 f->fmt.pix.field = V4L2_FIELD_NONE;
1253 f->fmt.pix.field = v4l2->interlaced_fieldmode ?
1254 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1258 static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1262 for (i = 0; i < ARRAY_SIZE(format); i++)
1263 if (format[i].fourcc == fourcc)
1269 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1270 struct v4l2_format *f)
1272 struct em28xx *dev = video_drvdata(file);
1273 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1274 unsigned int width = f->fmt.pix.width;
1275 unsigned int height = f->fmt.pix.height;
1276 unsigned int maxw = norm_maxw(dev);
1277 unsigned int maxh = norm_maxh(dev);
1278 unsigned int hscale, vscale;
1279 struct em28xx_fmt *fmt;
1281 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1283 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1284 f->fmt.pix.pixelformat);
1288 if (dev->board.is_em2800) {
1289 /* the em2800 can only scale down to 50% */
1290 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1291 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1293 * MaxPacketSize for em2800 is too small to capture at full
1294 * resolution use half of maxw as the scaler can only scale
1297 if (width == maxw && height == maxh)
1300 /* width must even because of the YUYV format
1301 height must be even because of interlacing */
1302 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1306 size_to_scale(dev, width, height, &hscale, &vscale);
1307 scale_to_size(dev, hscale, vscale, &width, &height);
1309 f->fmt.pix.width = width;
1310 f->fmt.pix.height = height;
1311 f->fmt.pix.pixelformat = fmt->fourcc;
1312 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
1313 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1314 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1315 if (v4l2->progressive)
1316 f->fmt.pix.field = V4L2_FIELD_NONE;
1318 f->fmt.pix.field = v4l2->interlaced_fieldmode ?
1319 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1320 f->fmt.pix.priv = 0;
1325 static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1326 unsigned width, unsigned height)
1328 struct em28xx_fmt *fmt;
1329 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1331 fmt = format_by_fourcc(fourcc);
1336 v4l2->width = width;
1337 v4l2->height = height;
1339 /* set new image size */
1340 size_to_scale(dev, v4l2->width, v4l2->height,
1341 &v4l2->hscale, &v4l2->vscale);
1343 em28xx_resolution_set(dev);
1348 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1349 struct v4l2_format *f)
1351 struct em28xx *dev = video_drvdata(file);
1352 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1354 if (vb2_is_busy(&v4l2->vb_vidq))
1357 vidioc_try_fmt_vid_cap(file, priv, f);
1359 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1360 f->fmt.pix.width, f->fmt.pix.height);
1363 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1365 struct em28xx *dev = video_drvdata(file);
1367 *norm = dev->v4l2->norm;
1372 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1374 struct em28xx *dev = video_drvdata(file);
1376 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, video, querystd, norm);
1381 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1383 struct em28xx *dev = video_drvdata(file);
1384 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1385 struct v4l2_format f;
1387 if (norm == v4l2->norm)
1390 if (v4l2->streaming_users > 0)
1395 /* Adjusts width/height, if needed */
1396 f.fmt.pix.width = 720;
1397 f.fmt.pix.height = (norm & V4L2_STD_525_60) ? 480 : 576;
1398 vidioc_try_fmt_vid_cap(file, priv, &f);
1400 /* set new image size */
1401 v4l2->width = f.fmt.pix.width;
1402 v4l2->height = f.fmt.pix.height;
1403 size_to_scale(dev, v4l2->width, v4l2->height,
1404 &v4l2->hscale, &v4l2->vscale);
1406 em28xx_resolution_set(dev);
1407 v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
1412 static int vidioc_g_parm(struct file *file, void *priv,
1413 struct v4l2_streamparm *p)
1415 struct em28xx *dev = video_drvdata(file);
1416 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1419 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1420 if (dev->board.is_webcam)
1421 rc = v4l2_device_call_until_err(&v4l2->v4l2_dev, 0,
1424 v4l2_video_std_frame_period(v4l2->norm,
1425 &p->parm.capture.timeperframe);
1430 static int vidioc_s_parm(struct file *file, void *priv,
1431 struct v4l2_streamparm *p)
1433 struct em28xx *dev = video_drvdata(file);
1435 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1436 return v4l2_device_call_until_err(&dev->v4l2->v4l2_dev,
1437 0, video, s_parm, p);
1440 static const char *iname[] = {
1441 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1442 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1443 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1444 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1445 [EM28XX_VMUX_SVIDEO] = "S-Video",
1446 [EM28XX_VMUX_TELEVISION] = "Television",
1447 [EM28XX_VMUX_CABLE] = "Cable TV",
1448 [EM28XX_VMUX_DVB] = "DVB",
1449 [EM28XX_VMUX_DEBUG] = "for debug only",
1452 static int vidioc_enum_input(struct file *file, void *priv,
1453 struct v4l2_input *i)
1455 struct em28xx *dev = video_drvdata(file);
1459 if (n >= MAX_EM28XX_INPUT)
1461 if (0 == INPUT(n)->type)
1465 i->type = V4L2_INPUT_TYPE_CAMERA;
1467 strcpy(i->name, iname[INPUT(n)->type]);
1469 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1470 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1471 i->type = V4L2_INPUT_TYPE_TUNER;
1473 i->std = dev->v4l2->vdev->tvnorms;
1474 /* webcams do not have the STD API */
1475 if (dev->board.is_webcam)
1476 i->capabilities = 0;
1481 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1483 struct em28xx *dev = video_drvdata(file);
1485 *i = dev->ctl_input;
1490 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1492 struct em28xx *dev = video_drvdata(file);
1494 if (i >= MAX_EM28XX_INPUT)
1496 if (0 == INPUT(i)->type)
1503 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1505 struct em28xx *dev = video_drvdata(file);
1508 case EM28XX_AMUX_VIDEO:
1509 strcpy(a->name, "Television");
1511 case EM28XX_AMUX_LINE_IN:
1512 strcpy(a->name, "Line In");
1514 case EM28XX_AMUX_VIDEO2:
1515 strcpy(a->name, "Television alt");
1517 case EM28XX_AMUX_PHONE:
1518 strcpy(a->name, "Phone");
1520 case EM28XX_AMUX_MIC:
1521 strcpy(a->name, "Mic");
1523 case EM28XX_AMUX_CD:
1524 strcpy(a->name, "CD");
1526 case EM28XX_AMUX_AUX:
1527 strcpy(a->name, "Aux");
1529 case EM28XX_AMUX_PCM_OUT:
1530 strcpy(a->name, "PCM");
1536 a->index = dev->ctl_ainput;
1537 a->capability = V4L2_AUDCAP_STEREO;
1542 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1544 struct em28xx *dev = video_drvdata(file);
1546 if (a->index >= MAX_EM28XX_INPUT)
1548 if (0 == INPUT(a->index)->type)
1551 dev->ctl_ainput = INPUT(a->index)->amux;
1552 dev->ctl_aoutput = INPUT(a->index)->aout;
1554 if (!dev->ctl_aoutput)
1555 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
1560 static int vidioc_g_tuner(struct file *file, void *priv,
1561 struct v4l2_tuner *t)
1563 struct em28xx *dev = video_drvdata(file);
1568 strcpy(t->name, "Tuner");
1570 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
1574 static int vidioc_s_tuner(struct file *file, void *priv,
1575 const struct v4l2_tuner *t)
1577 struct em28xx *dev = video_drvdata(file);
1582 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
1586 static int vidioc_g_frequency(struct file *file, void *priv,
1587 struct v4l2_frequency *f)
1589 struct em28xx *dev = video_drvdata(file);
1590 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1595 f->frequency = v4l2->frequency;
1599 static int vidioc_s_frequency(struct file *file, void *priv,
1600 const struct v4l2_frequency *f)
1602 struct v4l2_frequency new_freq = *f;
1603 struct em28xx *dev = video_drvdata(file);
1604 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1609 v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_frequency, f);
1610 v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1611 v4l2->frequency = new_freq.frequency;
1616 #ifdef CONFIG_VIDEO_ADV_DEBUG
1617 static int vidioc_g_chip_info(struct file *file, void *priv,
1618 struct v4l2_dbg_chip_info *chip)
1620 struct em28xx *dev = video_drvdata(file);
1622 if (chip->match.addr > 1)
1624 if (chip->match.addr == 1)
1625 strlcpy(chip->name, "ac97", sizeof(chip->name));
1628 dev->v4l2->v4l2_dev.name, sizeof(chip->name));
1632 static int em28xx_reg_len(int reg)
1635 case EM28XX_R40_AC97LSB:
1636 case EM28XX_R30_HSCALELOW:
1637 case EM28XX_R32_VSCALELOW:
1644 static int vidioc_g_register(struct file *file, void *priv,
1645 struct v4l2_dbg_register *reg)
1647 struct em28xx *dev = video_drvdata(file);
1650 if (reg->match.addr > 1)
1652 if (reg->match.addr) {
1653 ret = em28xx_read_ac97(dev, reg->reg);
1663 reg->size = em28xx_reg_len(reg->reg);
1664 if (reg->size == 1) {
1665 ret = em28xx_read_reg(dev, reg->reg);
1673 ret = dev->em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1674 reg->reg, (char *)&val, 2);
1678 reg->val = le16_to_cpu(val);
1684 static int vidioc_s_register(struct file *file, void *priv,
1685 const struct v4l2_dbg_register *reg)
1687 struct em28xx *dev = video_drvdata(file);
1690 if (reg->match.addr > 1)
1692 if (reg->match.addr)
1693 return em28xx_write_ac97(dev, reg->reg, reg->val);
1696 buf = cpu_to_le16(reg->val);
1698 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1699 em28xx_reg_len(reg->reg));
1704 static int vidioc_querycap(struct file *file, void *priv,
1705 struct v4l2_capability *cap)
1707 struct video_device *vdev = video_devdata(file);
1708 struct em28xx *dev = video_drvdata(file);
1709 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1711 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1712 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1713 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1715 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1716 cap->device_caps = V4L2_CAP_READWRITE |
1717 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1718 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1719 cap->device_caps = V4L2_CAP_RADIO;
1721 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
1723 if (dev->int_audio_type != EM28XX_INT_AUDIO_NONE)
1724 cap->device_caps |= V4L2_CAP_AUDIO;
1726 if (dev->tuner_type != TUNER_ABSENT)
1727 cap->device_caps |= V4L2_CAP_TUNER;
1729 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1730 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1732 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1733 if (v4l2->radio_dev)
1734 cap->capabilities |= V4L2_CAP_RADIO;
1738 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1739 struct v4l2_fmtdesc *f)
1741 if (unlikely(f->index >= ARRAY_SIZE(format)))
1744 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1745 f->pixelformat = format[f->index].fourcc;
1750 static int vidioc_enum_framesizes(struct file *file, void *priv,
1751 struct v4l2_frmsizeenum *fsize)
1753 struct em28xx *dev = video_drvdata(file);
1754 struct em28xx_fmt *fmt;
1755 unsigned int maxw = norm_maxw(dev);
1756 unsigned int maxh = norm_maxh(dev);
1758 fmt = format_by_fourcc(fsize->pixel_format);
1760 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1761 fsize->pixel_format);
1765 if (dev->board.is_em2800) {
1766 if (fsize->index > 1)
1768 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1769 fsize->discrete.width = maxw / (1 + fsize->index);
1770 fsize->discrete.height = maxh / (1 + fsize->index);
1774 if (fsize->index != 0)
1777 /* Report a continuous range */
1778 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1779 scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX,
1780 &fsize->stepwise.min_width, &fsize->stepwise.min_height);
1781 if (fsize->stepwise.min_width < 48)
1782 fsize->stepwise.min_width = 48;
1783 if (fsize->stepwise.min_height < 38)
1784 fsize->stepwise.min_height = 38;
1785 fsize->stepwise.max_width = maxw;
1786 fsize->stepwise.max_height = maxh;
1787 fsize->stepwise.step_width = 1;
1788 fsize->stepwise.step_height = 1;
1792 /* RAW VBI ioctls */
1794 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1795 struct v4l2_format *format)
1797 struct em28xx *dev = video_drvdata(file);
1798 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1800 format->fmt.vbi.samples_per_line = v4l2->vbi_width;
1801 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1802 format->fmt.vbi.offset = 0;
1803 format->fmt.vbi.flags = 0;
1804 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1805 format->fmt.vbi.count[0] = v4l2->vbi_height;
1806 format->fmt.vbi.count[1] = v4l2->vbi_height;
1807 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1809 /* Varies by video standard (NTSC, PAL, etc.) */
1810 if (v4l2->norm & V4L2_STD_525_60) {
1812 format->fmt.vbi.start[0] = 10;
1813 format->fmt.vbi.start[1] = 273;
1814 } else if (v4l2->norm & V4L2_STD_625_50) {
1816 format->fmt.vbi.start[0] = 6;
1817 format->fmt.vbi.start[1] = 318;
1823 /* ----------------------------------------------------------- */
1824 /* RADIO ESPECIFIC IOCTLS */
1825 /* ----------------------------------------------------------- */
1827 static int radio_g_tuner(struct file *file, void *priv,
1828 struct v4l2_tuner *t)
1830 struct em28xx *dev = video_drvdata(file);
1832 if (unlikely(t->index > 0))
1835 strcpy(t->name, "Radio");
1837 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
1842 static int radio_s_tuner(struct file *file, void *priv,
1843 const struct v4l2_tuner *t)
1845 struct em28xx *dev = video_drvdata(file);
1850 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
1856 * em28xx_free_v4l2() - Free struct em28xx_v4l2
1858 * @ref: struct kref for struct em28xx_v4l2
1860 * Called when all users of struct em28xx_v4l2 are gone
1862 static void em28xx_free_v4l2(struct kref *ref)
1864 struct em28xx_v4l2 *v4l2 = container_of(ref, struct em28xx_v4l2, ref);
1866 v4l2->dev->v4l2 = NULL;
1871 * em28xx_v4l2_open()
1872 * inits the device and starts isoc transfer
1874 static int em28xx_v4l2_open(struct file *filp)
1876 struct video_device *vdev = video_devdata(filp);
1877 struct em28xx *dev = video_drvdata(filp);
1878 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1879 enum v4l2_buf_type fh_type = 0;
1882 switch (vdev->vfl_type) {
1883 case VFL_TYPE_GRABBER:
1884 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1887 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1889 case VFL_TYPE_RADIO:
1895 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1896 video_device_node_name(vdev), v4l2_type_names[fh_type],
1899 if (mutex_lock_interruptible(&dev->lock))
1900 return -ERESTARTSYS;
1902 ret = v4l2_fh_open(filp);
1904 em28xx_errdev("%s: v4l2_fh_open() returned error %d\n",
1906 mutex_unlock(&dev->lock);
1910 if (v4l2->users == 0) {
1911 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1913 if (vdev->vfl_type != VFL_TYPE_RADIO)
1914 em28xx_resolution_set(dev);
1917 * Needed, since GPIO might have disabled power
1918 * of some i2c devices
1920 em28xx_wake_i2c(dev);
1923 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1924 em28xx_videodbg("video_open: setting radio device\n");
1925 v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_radio);
1928 kref_get(&dev->ref);
1929 kref_get(&v4l2->ref);
1932 mutex_unlock(&dev->lock);
1938 * em28xx_v4l2_fini()
1939 * unregisters the v4l2,i2c and usb devices
1940 * called when the device gets disconected or at module unload
1942 static int em28xx_v4l2_fini(struct em28xx *dev)
1944 struct em28xx_v4l2 *v4l2 = dev->v4l2;
1946 if (dev->is_audio_only) {
1947 /* Shouldn't initialize IR for this interface */
1951 if (!dev->has_video) {
1952 /* This device does not support the v4l2 extension */
1959 em28xx_info("Closing video extension");
1961 mutex_lock(&dev->lock);
1963 v4l2_device_disconnect(&v4l2->v4l2_dev);
1965 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
1967 if (v4l2->radio_dev) {
1968 em28xx_info("V4L2 device %s deregistered\n",
1969 video_device_node_name(v4l2->radio_dev));
1970 video_unregister_device(v4l2->radio_dev);
1972 if (v4l2->vbi_dev) {
1973 em28xx_info("V4L2 device %s deregistered\n",
1974 video_device_node_name(v4l2->vbi_dev));
1975 video_unregister_device(v4l2->vbi_dev);
1978 em28xx_info("V4L2 device %s deregistered\n",
1979 video_device_node_name(v4l2->vdev));
1980 video_unregister_device(v4l2->vdev);
1983 v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
1984 v4l2_device_unregister(&v4l2->v4l2_dev);
1987 v4l2_clk_unregister_fixed(v4l2->clk);
1991 kref_put(&v4l2->ref, em28xx_free_v4l2);
1993 mutex_unlock(&dev->lock);
1995 kref_put(&dev->ref, em28xx_free_device);
2000 static int em28xx_v4l2_suspend(struct em28xx *dev)
2002 if (dev->is_audio_only)
2005 if (!dev->has_video)
2008 em28xx_info("Suspending video extension");
2009 em28xx_stop_urbs(dev);
2013 static int em28xx_v4l2_resume(struct em28xx *dev)
2015 if (dev->is_audio_only)
2018 if (!dev->has_video)
2021 em28xx_info("Resuming video extension");
2022 /* what do we do here */
2027 * em28xx_v4l2_close()
2028 * stops streaming and deallocates all resources allocated by the v4l2
2031 static int em28xx_v4l2_close(struct file *filp)
2033 struct em28xx *dev = video_drvdata(filp);
2034 struct em28xx_v4l2 *v4l2 = dev->v4l2;
2037 em28xx_videodbg("users=%d\n", v4l2->users);
2039 vb2_fop_release(filp);
2040 mutex_lock(&dev->lock);
2042 if (v4l2->users == 1) {
2043 /* No sense to try to write to the device */
2044 if (dev->disconnected)
2047 /* Save some power by putting tuner to sleep */
2048 v4l2_device_call_all(&v4l2->v4l2_dev, 0, core, s_power, 0);
2050 /* do this before setting alternate! */
2051 em28xx_set_mode(dev, EM28XX_SUSPEND);
2053 /* set alternate 0 */
2055 em28xx_videodbg("setting alternate 0\n");
2056 errCode = usb_set_interface(dev->udev, 0, 0);
2058 em28xx_errdev("cannot change alternate number to "
2059 "0 (error=%i)\n", errCode);
2065 kref_put(&v4l2->ref, em28xx_free_v4l2);
2066 mutex_unlock(&dev->lock);
2067 kref_put(&dev->ref, em28xx_free_device);
2072 static const struct v4l2_file_operations em28xx_v4l_fops = {
2073 .owner = THIS_MODULE,
2074 .open = em28xx_v4l2_open,
2075 .release = em28xx_v4l2_close,
2076 .read = vb2_fop_read,
2077 .poll = vb2_fop_poll,
2078 .mmap = vb2_fop_mmap,
2079 .unlocked_ioctl = video_ioctl2,
2082 static const struct v4l2_ioctl_ops video_ioctl_ops = {
2083 .vidioc_querycap = vidioc_querycap,
2084 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2085 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2086 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2087 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2088 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2089 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2090 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2091 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2092 .vidioc_g_audio = vidioc_g_audio,
2093 .vidioc_s_audio = vidioc_s_audio,
2095 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2096 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2097 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2098 .vidioc_querybuf = vb2_ioctl_querybuf,
2099 .vidioc_qbuf = vb2_ioctl_qbuf,
2100 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2102 .vidioc_g_std = vidioc_g_std,
2103 .vidioc_querystd = vidioc_querystd,
2104 .vidioc_s_std = vidioc_s_std,
2105 .vidioc_g_parm = vidioc_g_parm,
2106 .vidioc_s_parm = vidioc_s_parm,
2107 .vidioc_enum_input = vidioc_enum_input,
2108 .vidioc_g_input = vidioc_g_input,
2109 .vidioc_s_input = vidioc_s_input,
2110 .vidioc_streamon = vb2_ioctl_streamon,
2111 .vidioc_streamoff = vb2_ioctl_streamoff,
2112 .vidioc_g_tuner = vidioc_g_tuner,
2113 .vidioc_s_tuner = vidioc_s_tuner,
2114 .vidioc_g_frequency = vidioc_g_frequency,
2115 .vidioc_s_frequency = vidioc_s_frequency,
2116 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2117 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2118 #ifdef CONFIG_VIDEO_ADV_DEBUG
2119 .vidioc_g_chip_info = vidioc_g_chip_info,
2120 .vidioc_g_register = vidioc_g_register,
2121 .vidioc_s_register = vidioc_s_register,
2125 static const struct video_device em28xx_video_template = {
2126 .fops = &em28xx_v4l_fops,
2127 .ioctl_ops = &video_ioctl_ops,
2128 .release = video_device_release,
2129 .tvnorms = V4L2_STD_ALL,
2132 static const struct v4l2_file_operations radio_fops = {
2133 .owner = THIS_MODULE,
2134 .open = em28xx_v4l2_open,
2135 .release = em28xx_v4l2_close,
2136 .unlocked_ioctl = video_ioctl2,
2139 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2140 .vidioc_querycap = vidioc_querycap,
2141 .vidioc_g_tuner = radio_g_tuner,
2142 .vidioc_s_tuner = radio_s_tuner,
2143 .vidioc_g_frequency = vidioc_g_frequency,
2144 .vidioc_s_frequency = vidioc_s_frequency,
2145 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2146 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2147 #ifdef CONFIG_VIDEO_ADV_DEBUG
2148 .vidioc_g_chip_info = vidioc_g_chip_info,
2149 .vidioc_g_register = vidioc_g_register,
2150 .vidioc_s_register = vidioc_s_register,
2154 static struct video_device em28xx_radio_template = {
2155 .fops = &radio_fops,
2156 .ioctl_ops = &radio_ioctl_ops,
2157 .release = video_device_release,
2160 /* I2C possible address to saa7115, tvp5150, msp3400, tvaudio */
2161 static unsigned short saa711x_addrs[] = {
2162 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
2163 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
2166 static unsigned short tvp5150_addrs[] = {
2172 static unsigned short msp3400_addrs[] = {
2178 /******************************** usb interface ******************************/
2180 static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2181 const struct video_device *template,
2182 const char *type_name)
2184 struct video_device *vfd;
2186 vfd = video_device_alloc();
2191 vfd->v4l2_dev = &dev->v4l2->v4l2_dev;
2192 vfd->debug = video_debug;
2193 vfd->lock = &dev->lock;
2194 if (dev->board.is_webcam)
2197 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2198 dev->name, type_name);
2200 video_set_drvdata(vfd, dev);
2204 static void em28xx_tuner_setup(struct em28xx *dev, unsigned short tuner_addr)
2206 struct em28xx_v4l2 *v4l2 = dev->v4l2;
2207 struct v4l2_device *v4l2_dev = &v4l2->v4l2_dev;
2208 struct tuner_setup tun_setup;
2209 struct v4l2_frequency f;
2211 memset(&tun_setup, 0, sizeof(tun_setup));
2213 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
2214 tun_setup.tuner_callback = em28xx_tuner_callback;
2216 if (dev->board.radio.type) {
2217 tun_setup.type = dev->board.radio.type;
2218 tun_setup.addr = dev->board.radio_addr;
2220 v4l2_device_call_all(v4l2_dev,
2221 0, tuner, s_type_addr, &tun_setup);
2224 if ((dev->tuner_type != TUNER_ABSENT) && (dev->tuner_type)) {
2225 tun_setup.type = dev->tuner_type;
2226 tun_setup.addr = tuner_addr;
2228 v4l2_device_call_all(v4l2_dev,
2229 0, tuner, s_type_addr, &tun_setup);
2232 if (dev->board.tda9887_conf) {
2233 struct v4l2_priv_tun_config tda9887_cfg;
2235 tda9887_cfg.tuner = TUNER_TDA9887;
2236 tda9887_cfg.priv = &dev->board.tda9887_conf;
2238 v4l2_device_call_all(v4l2_dev,
2239 0, tuner, s_config, &tda9887_cfg);
2242 if (dev->tuner_type == TUNER_XC2028) {
2243 struct v4l2_priv_tun_config xc2028_cfg;
2244 struct xc2028_ctrl ctl;
2246 memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
2247 memset(&ctl, 0, sizeof(ctl));
2249 em28xx_setup_xc3028(dev, &ctl);
2251 xc2028_cfg.tuner = TUNER_XC2028;
2252 xc2028_cfg.priv = &ctl;
2254 v4l2_device_call_all(v4l2_dev, 0, tuner, s_config, &xc2028_cfg);
2257 /* configure tuner */
2259 f.type = V4L2_TUNER_ANALOG_TV;
2260 f.frequency = 9076; /* just a magic number */
2261 v4l2->frequency = f.frequency;
2262 v4l2_device_call_all(v4l2_dev, 0, tuner, s_frequency, &f);
2265 static int em28xx_v4l2_init(struct em28xx *dev)
2270 struct v4l2_ctrl_handler *hdl;
2271 struct em28xx_v4l2 *v4l2;
2273 if (dev->is_audio_only) {
2274 /* Shouldn't initialize IR for this interface */
2278 if (!dev->has_video) {
2279 /* This device does not support the v4l2 extension */
2283 em28xx_info("Registering V4L2 extension\n");
2285 mutex_lock(&dev->lock);
2287 v4l2 = kzalloc(sizeof(struct em28xx_v4l2), GFP_KERNEL);
2289 em28xx_info("em28xx_v4l: memory allocation failed\n");
2290 mutex_unlock(&dev->lock);
2293 kref_init(&v4l2->ref);
2297 ret = v4l2_device_register(&dev->udev->dev, &v4l2->v4l2_dev);
2299 em28xx_errdev("Call to v4l2_device_register() failed!\n");
2303 hdl = &v4l2->ctrl_handler;
2304 v4l2_ctrl_handler_init(hdl, 8);
2305 v4l2->v4l2_dev.ctrl_handler = hdl;
2307 if (dev->board.is_webcam)
2308 v4l2->progressive = true;
2311 * Default format, used for tvp5150 or saa711x output formats
2313 v4l2->vinmode = 0x10;
2314 v4l2->vinctl = EM28XX_VINCTRL_INTERLACED |
2315 EM28XX_VINCTRL_CCIR656_ENABLE;
2317 /* request some modules */
2319 if (dev->board.has_msp34xx)
2320 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2321 &dev->i2c_adap[dev->def_i2c_bus],
2322 "msp3400", 0, msp3400_addrs);
2324 if (dev->board.decoder == EM28XX_SAA711X)
2325 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2326 &dev->i2c_adap[dev->def_i2c_bus],
2327 "saa7115_auto", 0, saa711x_addrs);
2329 if (dev->board.decoder == EM28XX_TVP5150)
2330 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2331 &dev->i2c_adap[dev->def_i2c_bus],
2332 "tvp5150", 0, tvp5150_addrs);
2334 if (dev->board.adecoder == EM28XX_TVAUDIO)
2335 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2336 &dev->i2c_adap[dev->def_i2c_bus],
2337 "tvaudio", dev->board.tvaudio_addr, NULL);
2339 /* Initialize tuner and camera */
2341 if (dev->board.tuner_type != TUNER_ABSENT) {
2342 unsigned short tuner_addr = dev->board.tuner_addr;
2343 int has_demod = (dev->board.tda9887_conf & TDA9887_PRESENT);
2345 if (dev->board.radio.type)
2346 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2347 &dev->i2c_adap[dev->def_i2c_bus],
2348 "tuner", dev->board.radio_addr, NULL);
2351 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2352 &dev->i2c_adap[dev->def_i2c_bus], "tuner",
2353 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
2354 if (tuner_addr == 0) {
2355 enum v4l2_i2c_tuner_type type =
2356 has_demod ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
2357 struct v4l2_subdev *sd;
2359 sd = v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2360 &dev->i2c_adap[dev->def_i2c_bus], "tuner",
2361 0, v4l2_i2c_tuner_addrs(type));
2364 tuner_addr = v4l2_i2c_subdev_addr(sd);
2366 v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
2367 &dev->i2c_adap[dev->def_i2c_bus],
2368 "tuner", tuner_addr, NULL);
2371 em28xx_tuner_setup(dev, tuner_addr);
2374 if (dev->em28xx_sensor != EM28XX_NOSENSOR)
2375 em28xx_init_camera(dev);
2377 /* Configure audio */
2378 ret = em28xx_audio_setup(dev);
2380 em28xx_errdev("%s: Error while setting audio - error [%d]!\n",
2382 goto unregister_dev;
2384 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
2385 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2386 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
2387 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2388 V4L2_CID_AUDIO_VOLUME, 0, 0x1f, 1, 0x1f);
2390 /* install the em28xx notify callback */
2391 v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_MUTE),
2392 em28xx_ctrl_notify, dev);
2393 v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_VOLUME),
2394 em28xx_ctrl_notify, dev);
2397 /* wake i2c devices */
2398 em28xx_wake_i2c(dev);
2400 /* init video dma queues */
2401 INIT_LIST_HEAD(&dev->vidq.active);
2402 INIT_LIST_HEAD(&dev->vbiq.active);
2404 if (dev->board.has_msp34xx) {
2405 /* Send a reset to other chips via gpio */
2406 ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xf7);
2408 em28xx_errdev("%s: em28xx_write_reg - msp34xx(1) failed! error [%d]\n",
2410 goto unregister_dev;
2414 ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xff);
2416 em28xx_errdev("%s: em28xx_write_reg - msp34xx(2) failed! error [%d]\n",
2418 goto unregister_dev;
2423 /* set default norm */
2424 v4l2->norm = V4L2_STD_PAL;
2425 v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
2426 v4l2->interlaced_fieldmode = EM28XX_INTERLACED_DEFAULT;
2428 /* Analog specific initialization */
2429 v4l2->format = &format[0];
2431 maxw = norm_maxw(dev);
2432 /* MaxPacketSize for em2800 is too small to capture at full resolution
2433 * use half of maxw as the scaler can only scale to 50% */
2434 if (dev->board.is_em2800)
2437 em28xx_set_video_format(dev, format[0].fourcc,
2438 maxw, norm_maxh(dev));
2442 /* Audio defaults */
2446 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
2447 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2448 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2449 (EM28XX_XCLK_AUDIO_UNMUTE | val));
2451 em28xx_set_outfmt(dev);
2453 /* Add image controls */
2454 /* NOTE: at this point, the subdevices are already registered, so bridge
2455 * controls are only added/enabled when no subdevice provides them */
2456 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_CONTRAST))
2457 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2459 0, 0x1f, 1, CONTRAST_DEFAULT);
2460 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_BRIGHTNESS))
2461 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2462 V4L2_CID_BRIGHTNESS,
2463 -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT);
2464 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_SATURATION))
2465 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2466 V4L2_CID_SATURATION,
2467 0, 0x1f, 1, SATURATION_DEFAULT);
2468 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_BLUE_BALANCE))
2469 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2470 V4L2_CID_BLUE_BALANCE,
2471 -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT);
2472 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_RED_BALANCE))
2473 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2474 V4L2_CID_RED_BALANCE,
2475 -0x30, 0x30, 1, RED_BALANCE_DEFAULT);
2476 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_SHARPNESS))
2477 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
2479 0, 0x0f, 1, SHARPNESS_DEFAULT);
2481 /* Reset image controls */
2482 em28xx_colorlevels_set_default(dev);
2483 v4l2_ctrl_handler_setup(hdl);
2486 goto unregister_dev;
2488 /* allocate and fill video video_device struct */
2489 v4l2->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2491 em28xx_errdev("cannot allocate video_device.\n");
2493 goto unregister_dev;
2495 mutex_init(&v4l2->vb_queue_lock);
2496 mutex_init(&v4l2->vb_vbi_queue_lock);
2497 v4l2->vdev->queue = &v4l2->vb_vidq;
2498 v4l2->vdev->queue->lock = &v4l2->vb_queue_lock;
2500 /* disable inapplicable ioctls */
2501 if (dev->board.is_webcam) {
2502 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_QUERYSTD);
2503 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_G_STD);
2504 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_S_STD);
2506 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_S_PARM);
2508 if (dev->tuner_type == TUNER_ABSENT) {
2509 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_G_TUNER);
2510 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_S_TUNER);
2511 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_G_FREQUENCY);
2512 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_S_FREQUENCY);
2514 if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
2515 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_G_AUDIO);
2516 v4l2_disable_ioctl(v4l2->vdev, VIDIOC_S_AUDIO);
2519 /* register v4l2 video video_device */
2520 ret = video_register_device(v4l2->vdev, VFL_TYPE_GRABBER,
2521 video_nr[dev->devno]);
2523 em28xx_errdev("unable to register video device (error=%i).\n",
2525 goto unregister_dev;
2528 /* Allocate and fill vbi video_device struct */
2529 if (em28xx_vbi_supported(dev) == 1) {
2530 v4l2->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2533 v4l2->vbi_dev->queue = &v4l2->vb_vbiq;
2534 v4l2->vbi_dev->queue->lock = &v4l2->vb_vbi_queue_lock;
2536 /* disable inapplicable ioctls */
2537 v4l2_disable_ioctl(v4l2->vbi_dev, VIDIOC_S_PARM);
2538 if (dev->tuner_type == TUNER_ABSENT) {
2539 v4l2_disable_ioctl(v4l2->vbi_dev, VIDIOC_G_TUNER);
2540 v4l2_disable_ioctl(v4l2->vbi_dev, VIDIOC_S_TUNER);
2541 v4l2_disable_ioctl(v4l2->vbi_dev, VIDIOC_G_FREQUENCY);
2542 v4l2_disable_ioctl(v4l2->vbi_dev, VIDIOC_S_FREQUENCY);
2544 if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
2545 v4l2_disable_ioctl(v4l2->vbi_dev, VIDIOC_G_AUDIO);
2546 v4l2_disable_ioctl(v4l2->vbi_dev, VIDIOC_S_AUDIO);
2549 /* register v4l2 vbi video_device */
2550 ret = video_register_device(v4l2->vbi_dev, VFL_TYPE_VBI,
2551 vbi_nr[dev->devno]);
2553 em28xx_errdev("unable to register vbi device\n");
2554 goto unregister_dev;
2558 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2559 v4l2->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2561 if (!v4l2->radio_dev) {
2562 em28xx_errdev("cannot allocate video_device.\n");
2564 goto unregister_dev;
2566 ret = video_register_device(v4l2->radio_dev, VFL_TYPE_RADIO,
2567 radio_nr[dev->devno]);
2569 em28xx_errdev("can't register radio device\n");
2570 goto unregister_dev;
2572 em28xx_info("Registered radio device as %s\n",
2573 video_device_node_name(v4l2->radio_dev));
2576 em28xx_info("V4L2 video device registered as %s\n",
2577 video_device_node_name(v4l2->vdev));
2580 em28xx_info("V4L2 VBI device registered as %s\n",
2581 video_device_node_name(v4l2->vbi_dev));
2583 /* Save some power by putting tuner to sleep */
2584 v4l2_device_call_all(&v4l2->v4l2_dev, 0, core, s_power, 0);
2586 /* initialize videobuf2 stuff */
2587 em28xx_vb2_setup(dev);
2589 em28xx_info("V4L2 extension successfully initialized\n");
2591 kref_get(&dev->ref);
2593 mutex_unlock(&dev->lock);
2597 v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
2598 v4l2_device_unregister(&v4l2->v4l2_dev);
2601 kref_put(&v4l2->ref, em28xx_free_v4l2);
2602 mutex_unlock(&dev->lock);
2606 static struct em28xx_ops v4l2_ops = {
2608 .name = "Em28xx v4l2 Extension",
2609 .init = em28xx_v4l2_init,
2610 .fini = em28xx_v4l2_fini,
2611 .suspend = em28xx_v4l2_suspend,
2612 .resume = em28xx_v4l2_resume,
2615 static int __init em28xx_video_register(void)
2617 return em28xx_register_extension(&v4l2_ops);
2620 static void __exit em28xx_video_unregister(void)
2622 em28xx_unregister_extension(&v4l2_ops);
2625 module_init(em28xx_video_register);
2626 module_exit(em28xx_video_unregister);