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