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