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