1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB 4 // video capture devices 5 // 6 // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> 7 // Markus Rechberger <mrechberger@gmail.com> 8 // Mauro Carvalho Chehab <mchehab@infradead.org> 9 // Sascha Sommer <saschasommer@freenet.de> 10 // Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com> 11 // 12 // Some parts based on SN9C10x PC Camera Controllers GPL driver made 13 // by Luca Risolia <luca.risolia@studio.unibo.it> 14 // 15 // This program is free software; you can redistribute it and/or modify 16 // it under the terms of the GNU General Public License as published by 17 // the Free Software Foundation; either version 2 of the License, or 18 // (at your option) any later version. 19 // 20 // This program is distributed in the hope that it will be useful, 21 // but WITHOUT ANY WARRANTY; without even the implied warranty of 22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 // GNU General Public License for more details. 24 25 #include "em28xx.h" 26 27 #include <linux/init.h> 28 #include <linux/list.h> 29 #include <linux/module.h> 30 #include <linux/kernel.h> 31 #include <linux/bitmap.h> 32 #include <linux/usb.h> 33 #include <linux/i2c.h> 34 #include <linux/mm.h> 35 #include <linux/mutex.h> 36 #include <linux/slab.h> 37 38 #include "em28xx-v4l.h" 39 #include <media/v4l2-common.h> 40 #include <media/v4l2-ioctl.h> 41 #include <media/v4l2-event.h> 42 #include <media/drv-intf/msp3400.h> 43 #include <media/tuner.h> 44 45 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \ 46 "Markus Rechberger <mrechberger@gmail.com>, " \ 47 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \ 48 "Sascha Sommer <saschasommer@freenet.de>" 49 50 static unsigned int isoc_debug; 51 module_param(isoc_debug, int, 0644); 52 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); 53 54 static unsigned int disable_vbi; 55 module_param(disable_vbi, int, 0644); 56 MODULE_PARM_DESC(disable_vbi, "disable vbi support"); 57 58 static int alt; 59 module_param(alt, int, 0644); 60 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint"); 61 62 #define em28xx_videodbg(fmt, arg...) do { \ 63 if (video_debug) \ 64 dev_printk(KERN_DEBUG, &dev->intf->dev, \ 65 "video: %s: " fmt, __func__, ## arg); \ 66 } while (0) 67 68 #define em28xx_isocdbg(fmt, arg...) do {\ 69 if (isoc_debug) \ 70 dev_printk(KERN_DEBUG, &dev->intf->dev, \ 71 "isoc: %s: " fmt, __func__, ## arg); \ 72 } while (0) 73 74 MODULE_AUTHOR(DRIVER_AUTHOR); 75 MODULE_DESCRIPTION(DRIVER_DESC " - v4l2 interface"); 76 MODULE_LICENSE("GPL v2"); 77 MODULE_VERSION(EM28XX_VERSION); 78 79 #define EM25XX_FRMDATAHDR_BYTE1 0x02 80 #define EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE 0x20 81 #define EM25XX_FRMDATAHDR_BYTE2_FRAME_END 0x02 82 #define EM25XX_FRMDATAHDR_BYTE2_FRAME_ID 0x01 83 #define EM25XX_FRMDATAHDR_BYTE2_MASK (EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE | \ 84 EM25XX_FRMDATAHDR_BYTE2_FRAME_END | \ 85 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID) 86 87 static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U }; 88 static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U }; 89 static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U }; 90 91 module_param_array(video_nr, int, NULL, 0444); 92 module_param_array(vbi_nr, int, NULL, 0444); 93 module_param_array(radio_nr, int, NULL, 0444); 94 MODULE_PARM_DESC(video_nr, "video device numbers"); 95 MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); 96 MODULE_PARM_DESC(radio_nr, "radio device numbers"); 97 98 static unsigned int video_debug; 99 module_param(video_debug, int, 0644); 100 MODULE_PARM_DESC(video_debug, "enable debug messages [video]"); 101 102 /* supported video standards */ 103 static struct em28xx_fmt format[] = { 104 { 105 .name = "16 bpp YUY2, 4:2:2, packed", 106 .fourcc = V4L2_PIX_FMT_YUYV, 107 .depth = 16, 108 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V, 109 }, { 110 .name = "16 bpp RGB 565, LE", 111 .fourcc = V4L2_PIX_FMT_RGB565, 112 .depth = 16, 113 .reg = EM28XX_OUTFMT_RGB_16_656, 114 }, { 115 .name = "8 bpp Bayer RGRG..GBGB", 116 .fourcc = V4L2_PIX_FMT_SRGGB8, 117 .depth = 8, 118 .reg = EM28XX_OUTFMT_RGB_8_RGRG, 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->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->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->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 synchronously"; 557 break; 558 case -ECONNRESET: 559 errmsg = "unlinked asynchronously"; 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->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->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 v4l2_subdev_frame_interval ival = { 0 }; 1582 struct em28xx *dev = video_drvdata(file); 1583 struct em28xx_v4l2 *v4l2 = dev->v4l2; 1584 int rc = 0; 1585 1586 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 1587 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 1588 return -EINVAL; 1589 1590 p->parm.capture.readbuffers = EM28XX_MIN_BUF; 1591 p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 1592 if (dev->is_webcam) { 1593 rc = v4l2_device_call_until_err(&v4l2->v4l2_dev, 0, 1594 video, g_frame_interval, &ival); 1595 if (!rc) 1596 p->parm.capture.timeperframe = ival.interval; 1597 } else { 1598 v4l2_video_std_frame_period(v4l2->norm, 1599 &p->parm.capture.timeperframe); 1600 } 1601 1602 return rc; 1603 } 1604 1605 static int vidioc_s_parm(struct file *file, void *priv, 1606 struct v4l2_streamparm *p) 1607 { 1608 struct em28xx *dev = video_drvdata(file); 1609 struct v4l2_subdev_frame_interval ival = { 1610 0, 1611 p->parm.capture.timeperframe 1612 }; 1613 int rc = 0; 1614 1615 if (!dev->is_webcam) 1616 return -ENOTTY; 1617 1618 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 1619 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 1620 return -EINVAL; 1621 1622 memset(&p->parm, 0, sizeof(p->parm)); 1623 p->parm.capture.readbuffers = EM28XX_MIN_BUF; 1624 p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 1625 rc = v4l2_device_call_until_err(&dev->v4l2->v4l2_dev, 0, 1626 video, s_frame_interval, &ival); 1627 if (!rc) 1628 p->parm.capture.timeperframe = ival.interval; 1629 return rc; 1630 } 1631 1632 static int vidioc_enum_input(struct file *file, void *priv, 1633 struct v4l2_input *i) 1634 { 1635 struct em28xx *dev = video_drvdata(file); 1636 unsigned int n; 1637 1638 n = i->index; 1639 if (n >= MAX_EM28XX_INPUT) 1640 return -EINVAL; 1641 if (0 == INPUT(n)->type) 1642 return -EINVAL; 1643 1644 i->index = n; 1645 i->type = V4L2_INPUT_TYPE_CAMERA; 1646 1647 strcpy(i->name, iname[INPUT(n)->type]); 1648 1649 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type)) 1650 i->type = V4L2_INPUT_TYPE_TUNER; 1651 1652 i->std = dev->v4l2->vdev.tvnorms; 1653 /* webcams do not have the STD API */ 1654 if (dev->is_webcam) 1655 i->capabilities = 0; 1656 1657 return 0; 1658 } 1659 1660 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 1661 { 1662 struct em28xx *dev = video_drvdata(file); 1663 1664 *i = dev->ctl_input; 1665 1666 return 0; 1667 } 1668 1669 static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 1670 { 1671 struct em28xx *dev = video_drvdata(file); 1672 1673 if (i >= MAX_EM28XX_INPUT) 1674 return -EINVAL; 1675 if (0 == INPUT(i)->type) 1676 return -EINVAL; 1677 1678 video_mux(dev, i); 1679 return 0; 1680 } 1681 1682 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a) 1683 { 1684 struct em28xx *dev = video_drvdata(file); 1685 1686 switch (a->index) { 1687 case EM28XX_AMUX_VIDEO: 1688 strcpy(a->name, "Television"); 1689 break; 1690 case EM28XX_AMUX_LINE_IN: 1691 strcpy(a->name, "Line In"); 1692 break; 1693 case EM28XX_AMUX_VIDEO2: 1694 strcpy(a->name, "Television alt"); 1695 break; 1696 case EM28XX_AMUX_PHONE: 1697 strcpy(a->name, "Phone"); 1698 break; 1699 case EM28XX_AMUX_MIC: 1700 strcpy(a->name, "Mic"); 1701 break; 1702 case EM28XX_AMUX_CD: 1703 strcpy(a->name, "CD"); 1704 break; 1705 case EM28XX_AMUX_AUX: 1706 strcpy(a->name, "Aux"); 1707 break; 1708 case EM28XX_AMUX_PCM_OUT: 1709 strcpy(a->name, "PCM"); 1710 break; 1711 default: 1712 return -EINVAL; 1713 } 1714 1715 a->index = dev->ctl_ainput; 1716 a->capability = V4L2_AUDCAP_STEREO; 1717 1718 return 0; 1719 } 1720 1721 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a) 1722 { 1723 struct em28xx *dev = video_drvdata(file); 1724 1725 if (a->index >= MAX_EM28XX_INPUT) 1726 return -EINVAL; 1727 if (0 == INPUT(a->index)->type) 1728 return -EINVAL; 1729 1730 dev->ctl_ainput = INPUT(a->index)->amux; 1731 dev->ctl_aoutput = INPUT(a->index)->aout; 1732 1733 if (!dev->ctl_aoutput) 1734 dev->ctl_aoutput = EM28XX_AOUT_MASTER; 1735 1736 return 0; 1737 } 1738 1739 static int vidioc_g_tuner(struct file *file, void *priv, 1740 struct v4l2_tuner *t) 1741 { 1742 struct em28xx *dev = video_drvdata(file); 1743 1744 if (0 != t->index) 1745 return -EINVAL; 1746 1747 strcpy(t->name, "Tuner"); 1748 1749 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t); 1750 return 0; 1751 } 1752 1753 static int vidioc_s_tuner(struct file *file, void *priv, 1754 const struct v4l2_tuner *t) 1755 { 1756 struct em28xx *dev = video_drvdata(file); 1757 1758 if (0 != t->index) 1759 return -EINVAL; 1760 1761 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t); 1762 return 0; 1763 } 1764 1765 static int vidioc_g_frequency(struct file *file, void *priv, 1766 struct v4l2_frequency *f) 1767 { 1768 struct em28xx *dev = video_drvdata(file); 1769 struct em28xx_v4l2 *v4l2 = dev->v4l2; 1770 1771 if (0 != f->tuner) 1772 return -EINVAL; 1773 1774 f->frequency = v4l2->frequency; 1775 return 0; 1776 } 1777 1778 static int vidioc_s_frequency(struct file *file, void *priv, 1779 const struct v4l2_frequency *f) 1780 { 1781 struct v4l2_frequency new_freq = *f; 1782 struct em28xx *dev = video_drvdata(file); 1783 struct em28xx_v4l2 *v4l2 = dev->v4l2; 1784 1785 if (0 != f->tuner) 1786 return -EINVAL; 1787 1788 v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_frequency, f); 1789 v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, g_frequency, &new_freq); 1790 v4l2->frequency = new_freq.frequency; 1791 1792 return 0; 1793 } 1794 1795 #ifdef CONFIG_VIDEO_ADV_DEBUG 1796 static int vidioc_g_chip_info(struct file *file, void *priv, 1797 struct v4l2_dbg_chip_info *chip) 1798 { 1799 struct em28xx *dev = video_drvdata(file); 1800 1801 if (chip->match.addr > 1) 1802 return -EINVAL; 1803 if (chip->match.addr == 1) 1804 strlcpy(chip->name, "ac97", sizeof(chip->name)); 1805 else 1806 strlcpy(chip->name, 1807 dev->v4l2->v4l2_dev.name, sizeof(chip->name)); 1808 return 0; 1809 } 1810 1811 static int em28xx_reg_len(int reg) 1812 { 1813 switch (reg) { 1814 case EM28XX_R40_AC97LSB: 1815 case EM28XX_R30_HSCALELOW: 1816 case EM28XX_R32_VSCALELOW: 1817 return 2; 1818 default: 1819 return 1; 1820 } 1821 } 1822 1823 static int vidioc_g_register(struct file *file, void *priv, 1824 struct v4l2_dbg_register *reg) 1825 { 1826 struct em28xx *dev = video_drvdata(file); 1827 int ret; 1828 1829 if (reg->match.addr > 1) 1830 return -EINVAL; 1831 if (reg->match.addr) { 1832 ret = em28xx_read_ac97(dev, reg->reg); 1833 if (ret < 0) 1834 return ret; 1835 1836 reg->val = ret; 1837 reg->size = 1; 1838 return 0; 1839 } 1840 1841 /* Match host */ 1842 reg->size = em28xx_reg_len(reg->reg); 1843 if (reg->size == 1) { 1844 ret = em28xx_read_reg(dev, reg->reg); 1845 1846 if (ret < 0) 1847 return ret; 1848 1849 reg->val = ret; 1850 } else { 1851 __le16 val = 0; 1852 1853 ret = dev->em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS, 1854 reg->reg, (char *)&val, 2); 1855 if (ret < 0) 1856 return ret; 1857 1858 reg->val = le16_to_cpu(val); 1859 } 1860 1861 return 0; 1862 } 1863 1864 static int vidioc_s_register(struct file *file, void *priv, 1865 const struct v4l2_dbg_register *reg) 1866 { 1867 struct em28xx *dev = video_drvdata(file); 1868 __le16 buf; 1869 1870 if (reg->match.addr > 1) 1871 return -EINVAL; 1872 if (reg->match.addr) 1873 return em28xx_write_ac97(dev, reg->reg, reg->val); 1874 1875 /* Match host */ 1876 buf = cpu_to_le16(reg->val); 1877 1878 return em28xx_write_regs(dev, reg->reg, (char *)&buf, 1879 em28xx_reg_len(reg->reg)); 1880 } 1881 #endif 1882 1883 static int vidioc_querycap(struct file *file, void *priv, 1884 struct v4l2_capability *cap) 1885 { 1886 struct video_device *vdev = video_devdata(file); 1887 struct em28xx *dev = video_drvdata(file); 1888 struct em28xx_v4l2 *v4l2 = dev->v4l2; 1889 struct usb_device *udev = interface_to_usbdev(dev->intf); 1890 1891 strlcpy(cap->driver, "em28xx", sizeof(cap->driver)); 1892 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card)); 1893 usb_make_path(udev, cap->bus_info, sizeof(cap->bus_info)); 1894 1895 if (vdev->vfl_type == VFL_TYPE_GRABBER) 1896 cap->device_caps = V4L2_CAP_READWRITE | 1897 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; 1898 else if (vdev->vfl_type == VFL_TYPE_RADIO) 1899 cap->device_caps = V4L2_CAP_RADIO; 1900 else 1901 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE; 1902 1903 if (dev->int_audio_type != EM28XX_INT_AUDIO_NONE) 1904 cap->device_caps |= V4L2_CAP_AUDIO; 1905 1906 if (dev->tuner_type != TUNER_ABSENT) 1907 cap->device_caps |= V4L2_CAP_TUNER; 1908 1909 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS | 1910 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; 1911 if (video_is_registered(&v4l2->vbi_dev)) 1912 cap->capabilities |= V4L2_CAP_VBI_CAPTURE; 1913 if (video_is_registered(&v4l2->radio_dev)) 1914 cap->capabilities |= V4L2_CAP_RADIO; 1915 return 0; 1916 } 1917 1918 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 1919 struct v4l2_fmtdesc *f) 1920 { 1921 if (unlikely(f->index >= ARRAY_SIZE(format))) 1922 return -EINVAL; 1923 1924 strlcpy(f->description, format[f->index].name, sizeof(f->description)); 1925 f->pixelformat = format[f->index].fourcc; 1926 1927 return 0; 1928 } 1929 1930 static int vidioc_enum_framesizes(struct file *file, void *priv, 1931 struct v4l2_frmsizeenum *fsize) 1932 { 1933 struct em28xx *dev = video_drvdata(file); 1934 struct em28xx_fmt *fmt; 1935 unsigned int maxw = norm_maxw(dev); 1936 unsigned int maxh = norm_maxh(dev); 1937 1938 fmt = format_by_fourcc(fsize->pixel_format); 1939 if (!fmt) { 1940 em28xx_videodbg("Fourcc format (%08x) invalid.\n", 1941 fsize->pixel_format); 1942 return -EINVAL; 1943 } 1944 1945 if (dev->board.is_em2800) { 1946 if (fsize->index > 1) 1947 return -EINVAL; 1948 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 1949 fsize->discrete.width = maxw / (1 + fsize->index); 1950 fsize->discrete.height = maxh / (1 + fsize->index); 1951 return 0; 1952 } 1953 1954 if (fsize->index != 0) 1955 return -EINVAL; 1956 1957 /* Report a continuous range */ 1958 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 1959 scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX, 1960 &fsize->stepwise.min_width, &fsize->stepwise.min_height); 1961 if (fsize->stepwise.min_width < 48) 1962 fsize->stepwise.min_width = 48; 1963 if (fsize->stepwise.min_height < 38) 1964 fsize->stepwise.min_height = 38; 1965 fsize->stepwise.max_width = maxw; 1966 fsize->stepwise.max_height = maxh; 1967 fsize->stepwise.step_width = 1; 1968 fsize->stepwise.step_height = 1; 1969 return 0; 1970 } 1971 1972 /* RAW VBI ioctls */ 1973 1974 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv, 1975 struct v4l2_format *format) 1976 { 1977 struct em28xx *dev = video_drvdata(file); 1978 struct em28xx_v4l2 *v4l2 = dev->v4l2; 1979 1980 format->fmt.vbi.samples_per_line = v4l2->vbi_width; 1981 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; 1982 format->fmt.vbi.offset = 0; 1983 format->fmt.vbi.flags = 0; 1984 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; 1985 format->fmt.vbi.count[0] = v4l2->vbi_height; 1986 format->fmt.vbi.count[1] = v4l2->vbi_height; 1987 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved)); 1988 1989 /* Varies by video standard (NTSC, PAL, etc.) */ 1990 if (v4l2->norm & V4L2_STD_525_60) { 1991 /* NTSC */ 1992 format->fmt.vbi.start[0] = 10; 1993 format->fmt.vbi.start[1] = 273; 1994 } else if (v4l2->norm & V4L2_STD_625_50) { 1995 /* PAL */ 1996 format->fmt.vbi.start[0] = 6; 1997 format->fmt.vbi.start[1] = 318; 1998 } 1999 2000 return 0; 2001 } 2002 2003 /* ----------------------------------------------------------- */ 2004 /* RADIO ESPECIFIC IOCTLS */ 2005 /* ----------------------------------------------------------- */ 2006 2007 static int radio_g_tuner(struct file *file, void *priv, 2008 struct v4l2_tuner *t) 2009 { 2010 struct em28xx *dev = video_drvdata(file); 2011 2012 if (unlikely(t->index > 0)) 2013 return -EINVAL; 2014 2015 strcpy(t->name, "Radio"); 2016 2017 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t); 2018 2019 return 0; 2020 } 2021 2022 static int radio_s_tuner(struct file *file, void *priv, 2023 const struct v4l2_tuner *t) 2024 { 2025 struct em28xx *dev = video_drvdata(file); 2026 2027 if (0 != t->index) 2028 return -EINVAL; 2029 2030 v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t); 2031 2032 return 0; 2033 } 2034 2035 /* 2036 * em28xx_free_v4l2() - Free struct em28xx_v4l2 2037 * 2038 * @ref: struct kref for struct em28xx_v4l2 2039 * 2040 * Called when all users of struct em28xx_v4l2 are gone 2041 */ 2042 static void em28xx_free_v4l2(struct kref *ref) 2043 { 2044 struct em28xx_v4l2 *v4l2 = container_of(ref, struct em28xx_v4l2, ref); 2045 2046 v4l2->dev->v4l2 = NULL; 2047 kfree(v4l2); 2048 } 2049 2050 /* 2051 * em28xx_v4l2_open() 2052 * inits the device and starts isoc transfer 2053 */ 2054 static int em28xx_v4l2_open(struct file *filp) 2055 { 2056 struct video_device *vdev = video_devdata(filp); 2057 struct em28xx *dev = video_drvdata(filp); 2058 struct em28xx_v4l2 *v4l2 = dev->v4l2; 2059 enum v4l2_buf_type fh_type = 0; 2060 int ret; 2061 2062 switch (vdev->vfl_type) { 2063 case VFL_TYPE_GRABBER: 2064 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2065 break; 2066 case VFL_TYPE_VBI: 2067 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE; 2068 break; 2069 case VFL_TYPE_RADIO: 2070 break; 2071 default: 2072 return -EINVAL; 2073 } 2074 2075 em28xx_videodbg("open dev=%s type=%s users=%d\n", 2076 video_device_node_name(vdev), v4l2_type_names[fh_type], 2077 v4l2->users); 2078 2079 if (mutex_lock_interruptible(&dev->lock)) 2080 return -ERESTARTSYS; 2081 2082 ret = v4l2_fh_open(filp); 2083 if (ret) { 2084 dev_err(&dev->intf->dev, 2085 "%s: v4l2_fh_open() returned error %d\n", 2086 __func__, ret); 2087 mutex_unlock(&dev->lock); 2088 return ret; 2089 } 2090 2091 if (v4l2->users == 0) { 2092 em28xx_set_mode(dev, EM28XX_ANALOG_MODE); 2093 2094 if (vdev->vfl_type != VFL_TYPE_RADIO) 2095 em28xx_resolution_set(dev); 2096 2097 /* 2098 * Needed, since GPIO might have disabled power 2099 * of some i2c devices 2100 */ 2101 em28xx_wake_i2c(dev); 2102 } 2103 2104 if (vdev->vfl_type == VFL_TYPE_RADIO) { 2105 em28xx_videodbg("video_open: setting radio device\n"); 2106 v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_radio); 2107 } 2108 2109 kref_get(&dev->ref); 2110 kref_get(&v4l2->ref); 2111 v4l2->users++; 2112 2113 mutex_unlock(&dev->lock); 2114 2115 return 0; 2116 } 2117 2118 /* 2119 * em28xx_v4l2_fini() 2120 * unregisters the v4l2,i2c and usb devices 2121 * called when the device gets disconected or at module unload 2122 */ 2123 static int em28xx_v4l2_fini(struct em28xx *dev) 2124 { 2125 struct em28xx_v4l2 *v4l2 = dev->v4l2; 2126 2127 if (dev->is_audio_only) { 2128 /* Shouldn't initialize IR for this interface */ 2129 return 0; 2130 } 2131 2132 if (!dev->has_video) { 2133 /* This device does not support the v4l2 extension */ 2134 return 0; 2135 } 2136 2137 if (v4l2 == NULL) 2138 return 0; 2139 2140 dev_info(&dev->intf->dev, "Closing video extension\n"); 2141 2142 mutex_lock(&dev->lock); 2143 2144 v4l2_device_disconnect(&v4l2->v4l2_dev); 2145 2146 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE); 2147 2148 em28xx_v4l2_media_release(dev); 2149 2150 if (video_is_registered(&v4l2->radio_dev)) { 2151 dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n", 2152 video_device_node_name(&v4l2->radio_dev)); 2153 video_unregister_device(&v4l2->radio_dev); 2154 } 2155 if (video_is_registered(&v4l2->vbi_dev)) { 2156 dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n", 2157 video_device_node_name(&v4l2->vbi_dev)); 2158 video_unregister_device(&v4l2->vbi_dev); 2159 } 2160 if (video_is_registered(&v4l2->vdev)) { 2161 dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n", 2162 video_device_node_name(&v4l2->vdev)); 2163 video_unregister_device(&v4l2->vdev); 2164 } 2165 2166 v4l2_ctrl_handler_free(&v4l2->ctrl_handler); 2167 v4l2_device_unregister(&v4l2->v4l2_dev); 2168 2169 kref_put(&v4l2->ref, em28xx_free_v4l2); 2170 2171 mutex_unlock(&dev->lock); 2172 2173 kref_put(&dev->ref, em28xx_free_device); 2174 2175 return 0; 2176 } 2177 2178 static int em28xx_v4l2_suspend(struct em28xx *dev) 2179 { 2180 if (dev->is_audio_only) 2181 return 0; 2182 2183 if (!dev->has_video) 2184 return 0; 2185 2186 dev_info(&dev->intf->dev, "Suspending video extension\n"); 2187 em28xx_stop_urbs(dev); 2188 return 0; 2189 } 2190 2191 static int em28xx_v4l2_resume(struct em28xx *dev) 2192 { 2193 if (dev->is_audio_only) 2194 return 0; 2195 2196 if (!dev->has_video) 2197 return 0; 2198 2199 dev_info(&dev->intf->dev, "Resuming video extension\n"); 2200 /* what do we do here */ 2201 return 0; 2202 } 2203 2204 /* 2205 * em28xx_v4l2_close() 2206 * stops streaming and deallocates all resources allocated by the v4l2 2207 * calls and ioctls 2208 */ 2209 static int em28xx_v4l2_close(struct file *filp) 2210 { 2211 struct em28xx *dev = video_drvdata(filp); 2212 struct em28xx_v4l2 *v4l2 = dev->v4l2; 2213 struct usb_device *udev = interface_to_usbdev(dev->intf); 2214 int errCode; 2215 2216 em28xx_videodbg("users=%d\n", v4l2->users); 2217 2218 vb2_fop_release(filp); 2219 mutex_lock(&dev->lock); 2220 2221 if (v4l2->users == 1) { 2222 /* No sense to try to write to the device */ 2223 if (dev->disconnected) 2224 goto exit; 2225 2226 /* Save some power by putting tuner to sleep */ 2227 v4l2_device_call_all(&v4l2->v4l2_dev, 0, core, s_power, 0); 2228 2229 /* do this before setting alternate! */ 2230 em28xx_set_mode(dev, EM28XX_SUSPEND); 2231 2232 /* set alternate 0 */ 2233 dev->alt = 0; 2234 em28xx_videodbg("setting alternate 0\n"); 2235 errCode = usb_set_interface(udev, 0, 0); 2236 if (errCode < 0) { 2237 dev_err(&dev->intf->dev, 2238 "cannot change alternate number to 0 (error=%i)\n", 2239 errCode); 2240 } 2241 } 2242 2243 exit: 2244 v4l2->users--; 2245 kref_put(&v4l2->ref, em28xx_free_v4l2); 2246 mutex_unlock(&dev->lock); 2247 kref_put(&dev->ref, em28xx_free_device); 2248 2249 return 0; 2250 } 2251 2252 static const struct v4l2_file_operations em28xx_v4l_fops = { 2253 .owner = THIS_MODULE, 2254 .open = em28xx_v4l2_open, 2255 .release = em28xx_v4l2_close, 2256 .read = vb2_fop_read, 2257 .poll = vb2_fop_poll, 2258 .mmap = vb2_fop_mmap, 2259 .unlocked_ioctl = video_ioctl2, 2260 }; 2261 2262 static const struct v4l2_ioctl_ops video_ioctl_ops = { 2263 .vidioc_querycap = vidioc_querycap, 2264 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 2265 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 2266 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 2267 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 2268 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 2269 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 2270 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 2271 .vidioc_enum_framesizes = vidioc_enum_framesizes, 2272 .vidioc_g_audio = vidioc_g_audio, 2273 .vidioc_s_audio = vidioc_s_audio, 2274 2275 .vidioc_reqbufs = vb2_ioctl_reqbufs, 2276 .vidioc_create_bufs = vb2_ioctl_create_bufs, 2277 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 2278 .vidioc_querybuf = vb2_ioctl_querybuf, 2279 .vidioc_qbuf = vb2_ioctl_qbuf, 2280 .vidioc_dqbuf = vb2_ioctl_dqbuf, 2281 2282 .vidioc_g_std = vidioc_g_std, 2283 .vidioc_querystd = vidioc_querystd, 2284 .vidioc_s_std = vidioc_s_std, 2285 .vidioc_g_parm = vidioc_g_parm, 2286 .vidioc_s_parm = vidioc_s_parm, 2287 .vidioc_enum_input = vidioc_enum_input, 2288 .vidioc_g_input = vidioc_g_input, 2289 .vidioc_s_input = vidioc_s_input, 2290 .vidioc_streamon = vb2_ioctl_streamon, 2291 .vidioc_streamoff = vb2_ioctl_streamoff, 2292 .vidioc_g_tuner = vidioc_g_tuner, 2293 .vidioc_s_tuner = vidioc_s_tuner, 2294 .vidioc_g_frequency = vidioc_g_frequency, 2295 .vidioc_s_frequency = vidioc_s_frequency, 2296 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 2297 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 2298 #ifdef CONFIG_VIDEO_ADV_DEBUG 2299 .vidioc_g_chip_info = vidioc_g_chip_info, 2300 .vidioc_g_register = vidioc_g_register, 2301 .vidioc_s_register = vidioc_s_register, 2302 #endif 2303 }; 2304 2305 static const struct video_device em28xx_video_template = { 2306 .fops = &em28xx_v4l_fops, 2307 .ioctl_ops = &video_ioctl_ops, 2308 .release = video_device_release_empty, 2309 .tvnorms = V4L2_STD_ALL, 2310 }; 2311 2312 static const struct v4l2_file_operations radio_fops = { 2313 .owner = THIS_MODULE, 2314 .open = em28xx_v4l2_open, 2315 .release = em28xx_v4l2_close, 2316 .unlocked_ioctl = video_ioctl2, 2317 }; 2318 2319 static const struct v4l2_ioctl_ops radio_ioctl_ops = { 2320 .vidioc_querycap = vidioc_querycap, 2321 .vidioc_g_tuner = radio_g_tuner, 2322 .vidioc_s_tuner = radio_s_tuner, 2323 .vidioc_g_frequency = vidioc_g_frequency, 2324 .vidioc_s_frequency = vidioc_s_frequency, 2325 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 2326 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 2327 #ifdef CONFIG_VIDEO_ADV_DEBUG 2328 .vidioc_g_chip_info = vidioc_g_chip_info, 2329 .vidioc_g_register = vidioc_g_register, 2330 .vidioc_s_register = vidioc_s_register, 2331 #endif 2332 }; 2333 2334 static struct video_device em28xx_radio_template = { 2335 .fops = &radio_fops, 2336 .ioctl_ops = &radio_ioctl_ops, 2337 .release = video_device_release_empty, 2338 }; 2339 2340 /* I2C possible address to saa7115, tvp5150, msp3400, tvaudio */ 2341 static unsigned short saa711x_addrs[] = { 2342 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */ 2343 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */ 2344 I2C_CLIENT_END }; 2345 2346 static unsigned short tvp5150_addrs[] = { 2347 0xb8 >> 1, 2348 0xba >> 1, 2349 I2C_CLIENT_END 2350 }; 2351 2352 static unsigned short msp3400_addrs[] = { 2353 0x80 >> 1, 2354 0x88 >> 1, 2355 I2C_CLIENT_END 2356 }; 2357 2358 /******************************** usb interface ******************************/ 2359 2360 static void em28xx_vdev_init(struct em28xx *dev, 2361 struct video_device *vfd, 2362 const struct video_device *template, 2363 const char *type_name) 2364 { 2365 *vfd = *template; 2366 vfd->v4l2_dev = &dev->v4l2->v4l2_dev; 2367 vfd->lock = &dev->lock; 2368 if (dev->is_webcam) 2369 vfd->tvnorms = 0; 2370 2371 snprintf(vfd->name, sizeof(vfd->name), "%s %s", 2372 dev_name(&dev->intf->dev), type_name); 2373 2374 video_set_drvdata(vfd, dev); 2375 } 2376 2377 static void em28xx_tuner_setup(struct em28xx *dev, unsigned short tuner_addr) 2378 { 2379 struct em28xx_v4l2 *v4l2 = dev->v4l2; 2380 struct v4l2_device *v4l2_dev = &v4l2->v4l2_dev; 2381 struct tuner_setup tun_setup; 2382 struct v4l2_frequency f; 2383 2384 memset(&tun_setup, 0, sizeof(tun_setup)); 2385 2386 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO; 2387 tun_setup.tuner_callback = em28xx_tuner_callback; 2388 2389 if (dev->board.radio.type) { 2390 tun_setup.type = dev->board.radio.type; 2391 tun_setup.addr = dev->board.radio_addr; 2392 2393 v4l2_device_call_all(v4l2_dev, 2394 0, tuner, s_type_addr, &tun_setup); 2395 } 2396 2397 if ((dev->tuner_type != TUNER_ABSENT) && (dev->tuner_type)) { 2398 tun_setup.type = dev->tuner_type; 2399 tun_setup.addr = tuner_addr; 2400 2401 v4l2_device_call_all(v4l2_dev, 2402 0, tuner, s_type_addr, &tun_setup); 2403 } 2404 2405 if (dev->board.tda9887_conf) { 2406 struct v4l2_priv_tun_config tda9887_cfg; 2407 2408 tda9887_cfg.tuner = TUNER_TDA9887; 2409 tda9887_cfg.priv = &dev->board.tda9887_conf; 2410 2411 v4l2_device_call_all(v4l2_dev, 2412 0, tuner, s_config, &tda9887_cfg); 2413 } 2414 2415 if (dev->tuner_type == TUNER_XC2028) { 2416 struct v4l2_priv_tun_config xc2028_cfg; 2417 struct xc2028_ctrl ctl; 2418 2419 memset(&xc2028_cfg, 0, sizeof(xc2028_cfg)); 2420 memset(&ctl, 0, sizeof(ctl)); 2421 2422 em28xx_setup_xc3028(dev, &ctl); 2423 2424 xc2028_cfg.tuner = TUNER_XC2028; 2425 xc2028_cfg.priv = &ctl; 2426 2427 v4l2_device_call_all(v4l2_dev, 0, tuner, s_config, &xc2028_cfg); 2428 } 2429 2430 /* configure tuner */ 2431 f.tuner = 0; 2432 f.type = V4L2_TUNER_ANALOG_TV; 2433 f.frequency = 9076; /* just a magic number */ 2434 v4l2->frequency = f.frequency; 2435 v4l2_device_call_all(v4l2_dev, 0, tuner, s_frequency, &f); 2436 } 2437 2438 static int em28xx_v4l2_init(struct em28xx *dev) 2439 { 2440 u8 val; 2441 int ret; 2442 unsigned int maxw; 2443 struct v4l2_ctrl_handler *hdl; 2444 struct em28xx_v4l2 *v4l2; 2445 2446 if (dev->is_audio_only) { 2447 /* Shouldn't initialize IR for this interface */ 2448 return 0; 2449 } 2450 2451 if (!dev->has_video) { 2452 /* This device does not support the v4l2 extension */ 2453 return 0; 2454 } 2455 2456 dev_info(&dev->intf->dev, "Registering V4L2 extension\n"); 2457 2458 mutex_lock(&dev->lock); 2459 2460 v4l2 = kzalloc(sizeof(struct em28xx_v4l2), GFP_KERNEL); 2461 if (!v4l2) { 2462 mutex_unlock(&dev->lock); 2463 return -ENOMEM; 2464 } 2465 kref_init(&v4l2->ref); 2466 v4l2->dev = dev; 2467 dev->v4l2 = v4l2; 2468 2469 #ifdef CONFIG_MEDIA_CONTROLLER 2470 v4l2->v4l2_dev.mdev = dev->media_dev; 2471 #endif 2472 ret = v4l2_device_register(&dev->intf->dev, &v4l2->v4l2_dev); 2473 if (ret < 0) { 2474 dev_err(&dev->intf->dev, 2475 "Call to v4l2_device_register() failed!\n"); 2476 goto err; 2477 } 2478 2479 hdl = &v4l2->ctrl_handler; 2480 v4l2_ctrl_handler_init(hdl, 8); 2481 v4l2->v4l2_dev.ctrl_handler = hdl; 2482 2483 if (dev->is_webcam) 2484 v4l2->progressive = true; 2485 2486 /* 2487 * Default format, used for tvp5150 or saa711x output formats 2488 */ 2489 v4l2->vinmode = EM28XX_VINMODE_YUV422_CbYCrY; 2490 v4l2->vinctl = EM28XX_VINCTRL_INTERLACED | 2491 EM28XX_VINCTRL_CCIR656_ENABLE; 2492 2493 /* request some modules */ 2494 2495 if (dev->has_msp34xx) 2496 v4l2_i2c_new_subdev(&v4l2->v4l2_dev, 2497 &dev->i2c_adap[dev->def_i2c_bus], 2498 "msp3400", 0, msp3400_addrs); 2499 2500 if (dev->board.decoder == EM28XX_SAA711X) 2501 v4l2_i2c_new_subdev(&v4l2->v4l2_dev, 2502 &dev->i2c_adap[dev->def_i2c_bus], 2503 "saa7115_auto", 0, saa711x_addrs); 2504 2505 if (dev->board.decoder == EM28XX_TVP5150) 2506 v4l2_i2c_new_subdev(&v4l2->v4l2_dev, 2507 &dev->i2c_adap[dev->def_i2c_bus], 2508 "tvp5150", 0, tvp5150_addrs); 2509 2510 if (dev->board.adecoder == EM28XX_TVAUDIO) 2511 v4l2_i2c_new_subdev(&v4l2->v4l2_dev, 2512 &dev->i2c_adap[dev->def_i2c_bus], 2513 "tvaudio", dev->board.tvaudio_addr, NULL); 2514 2515 /* Initialize tuner and camera */ 2516 2517 if (dev->board.tuner_type != TUNER_ABSENT) { 2518 unsigned short tuner_addr = dev->board.tuner_addr; 2519 int has_demod = (dev->board.tda9887_conf & TDA9887_PRESENT); 2520 2521 if (dev->board.radio.type) 2522 v4l2_i2c_new_subdev(&v4l2->v4l2_dev, 2523 &dev->i2c_adap[dev->def_i2c_bus], 2524 "tuner", dev->board.radio_addr, 2525 NULL); 2526 2527 if (has_demod) 2528 v4l2_i2c_new_subdev(&v4l2->v4l2_dev, 2529 &dev->i2c_adap[dev->def_i2c_bus], 2530 "tuner", 0, 2531 v4l2_i2c_tuner_addrs(ADDRS_DEMOD)); 2532 if (tuner_addr == 0) { 2533 enum v4l2_i2c_tuner_type type = 2534 has_demod ? ADDRS_TV_WITH_DEMOD : ADDRS_TV; 2535 struct v4l2_subdev *sd; 2536 2537 sd = v4l2_i2c_new_subdev(&v4l2->v4l2_dev, 2538 &dev->i2c_adap[dev->def_i2c_bus], 2539 "tuner", 0, 2540 v4l2_i2c_tuner_addrs(type)); 2541 2542 if (sd) 2543 tuner_addr = v4l2_i2c_subdev_addr(sd); 2544 } else { 2545 v4l2_i2c_new_subdev(&v4l2->v4l2_dev, 2546 &dev->i2c_adap[dev->def_i2c_bus], 2547 "tuner", tuner_addr, NULL); 2548 } 2549 2550 em28xx_tuner_setup(dev, tuner_addr); 2551 } 2552 2553 if (dev->em28xx_sensor != EM28XX_NOSENSOR) 2554 em28xx_init_camera(dev); 2555 2556 /* Configure audio */ 2557 ret = em28xx_audio_setup(dev); 2558 if (ret < 0) { 2559 dev_err(&dev->intf->dev, 2560 "%s: Error while setting audio - error [%d]!\n", 2561 __func__, ret); 2562 goto unregister_dev; 2563 } 2564 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) { 2565 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops, 2566 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); 2567 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops, 2568 V4L2_CID_AUDIO_VOLUME, 0, 0x1f, 1, 0x1f); 2569 } else { 2570 /* install the em28xx notify callback */ 2571 v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_MUTE), 2572 em28xx_ctrl_notify, dev); 2573 v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_VOLUME), 2574 em28xx_ctrl_notify, dev); 2575 } 2576 2577 /* wake i2c devices */ 2578 em28xx_wake_i2c(dev); 2579 2580 /* init video dma queues */ 2581 INIT_LIST_HEAD(&dev->vidq.active); 2582 INIT_LIST_HEAD(&dev->vbiq.active); 2583 2584 if (dev->has_msp34xx) { 2585 /* Send a reset to other chips via gpio */ 2586 ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xf7); 2587 if (ret < 0) { 2588 dev_err(&dev->intf->dev, 2589 "%s: em28xx_write_reg - msp34xx(1) failed! error [%d]\n", 2590 __func__, ret); 2591 goto unregister_dev; 2592 } 2593 msleep(3); 2594 2595 ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xff); 2596 if (ret < 0) { 2597 dev_err(&dev->intf->dev, 2598 "%s: em28xx_write_reg - msp34xx(2) failed! error [%d]\n", 2599 __func__, ret); 2600 goto unregister_dev; 2601 } 2602 msleep(3); 2603 } 2604 2605 /* set default norm */ 2606 v4l2->norm = V4L2_STD_PAL; 2607 v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm); 2608 v4l2->interlaced_fieldmode = EM28XX_INTERLACED_DEFAULT; 2609 2610 /* Analog specific initialization */ 2611 v4l2->format = &format[0]; 2612 2613 maxw = norm_maxw(dev); 2614 /* MaxPacketSize for em2800 is too small to capture at full resolution 2615 * use half of maxw as the scaler can only scale to 50% */ 2616 if (dev->board.is_em2800) 2617 maxw /= 2; 2618 2619 em28xx_set_video_format(dev, format[0].fourcc, 2620 maxw, norm_maxh(dev)); 2621 2622 video_mux(dev, 0); 2623 2624 /* Audio defaults */ 2625 dev->mute = 1; 2626 dev->volume = 0x1f; 2627 2628 /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */ 2629 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK); 2630 em28xx_write_reg(dev, EM28XX_R0F_XCLK, 2631 (EM28XX_XCLK_AUDIO_UNMUTE | val)); 2632 2633 em28xx_set_outfmt(dev); 2634 2635 /* Add image controls */ 2636 /* NOTE: at this point, the subdevices are already registered, so bridge 2637 * controls are only added/enabled when no subdevice provides them */ 2638 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_CONTRAST)) 2639 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops, 2640 V4L2_CID_CONTRAST, 2641 0, 0x1f, 1, CONTRAST_DEFAULT); 2642 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_BRIGHTNESS)) 2643 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops, 2644 V4L2_CID_BRIGHTNESS, 2645 -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT); 2646 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_SATURATION)) 2647 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops, 2648 V4L2_CID_SATURATION, 2649 0, 0x1f, 1, SATURATION_DEFAULT); 2650 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_BLUE_BALANCE)) 2651 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops, 2652 V4L2_CID_BLUE_BALANCE, 2653 -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT); 2654 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_RED_BALANCE)) 2655 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops, 2656 V4L2_CID_RED_BALANCE, 2657 -0x30, 0x30, 1, RED_BALANCE_DEFAULT); 2658 if (NULL == v4l2_ctrl_find(hdl, V4L2_CID_SHARPNESS)) 2659 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops, 2660 V4L2_CID_SHARPNESS, 2661 0, 0x0f, 1, SHARPNESS_DEFAULT); 2662 2663 /* Reset image controls */ 2664 em28xx_colorlevels_set_default(dev); 2665 v4l2_ctrl_handler_setup(hdl); 2666 ret = hdl->error; 2667 if (ret) 2668 goto unregister_dev; 2669 2670 /* allocate and fill video video_device struct */ 2671 em28xx_vdev_init(dev, &v4l2->vdev, &em28xx_video_template, "video"); 2672 mutex_init(&v4l2->vb_queue_lock); 2673 mutex_init(&v4l2->vb_vbi_queue_lock); 2674 v4l2->vdev.queue = &v4l2->vb_vidq; 2675 v4l2->vdev.queue->lock = &v4l2->vb_queue_lock; 2676 2677 /* disable inapplicable ioctls */ 2678 if (dev->is_webcam) { 2679 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_QUERYSTD); 2680 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_STD); 2681 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_STD); 2682 } else { 2683 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_PARM); 2684 } 2685 if (dev->tuner_type == TUNER_ABSENT) { 2686 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_TUNER); 2687 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_TUNER); 2688 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_FREQUENCY); 2689 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_FREQUENCY); 2690 } 2691 if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) { 2692 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_AUDIO); 2693 v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_AUDIO); 2694 } 2695 2696 /* register v4l2 video video_device */ 2697 ret = video_register_device(&v4l2->vdev, VFL_TYPE_GRABBER, 2698 video_nr[dev->devno]); 2699 if (ret) { 2700 dev_err(&dev->intf->dev, 2701 "unable to register video device (error=%i).\n", ret); 2702 goto unregister_dev; 2703 } 2704 2705 /* Allocate and fill vbi video_device struct */ 2706 if (em28xx_vbi_supported(dev) == 1) { 2707 em28xx_vdev_init(dev, &v4l2->vbi_dev, &em28xx_video_template, 2708 "vbi"); 2709 2710 v4l2->vbi_dev.queue = &v4l2->vb_vbiq; 2711 v4l2->vbi_dev.queue->lock = &v4l2->vb_vbi_queue_lock; 2712 2713 /* disable inapplicable ioctls */ 2714 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_PARM); 2715 if (dev->tuner_type == TUNER_ABSENT) { 2716 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_TUNER); 2717 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_TUNER); 2718 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_FREQUENCY); 2719 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_FREQUENCY); 2720 } 2721 if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) { 2722 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_AUDIO); 2723 v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_AUDIO); 2724 } 2725 2726 /* register v4l2 vbi video_device */ 2727 ret = video_register_device(&v4l2->vbi_dev, VFL_TYPE_VBI, 2728 vbi_nr[dev->devno]); 2729 if (ret < 0) { 2730 dev_err(&dev->intf->dev, 2731 "unable to register vbi device\n"); 2732 goto unregister_dev; 2733 } 2734 } 2735 2736 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) { 2737 em28xx_vdev_init(dev, &v4l2->radio_dev, &em28xx_radio_template, 2738 "radio"); 2739 ret = video_register_device(&v4l2->radio_dev, VFL_TYPE_RADIO, 2740 radio_nr[dev->devno]); 2741 if (ret < 0) { 2742 dev_err(&dev->intf->dev, 2743 "can't register radio device\n"); 2744 goto unregister_dev; 2745 } 2746 dev_info(&dev->intf->dev, 2747 "Registered radio device as %s\n", 2748 video_device_node_name(&v4l2->radio_dev)); 2749 } 2750 2751 /* Init entities at the Media Controller */ 2752 em28xx_v4l2_create_entities(dev); 2753 2754 #ifdef CONFIG_MEDIA_CONTROLLER 2755 ret = v4l2_mc_create_media_graph(dev->media_dev); 2756 if (ret) { 2757 dev_err(&dev->intf->dev, 2758 "failed to create media graph\n"); 2759 em28xx_v4l2_media_release(dev); 2760 goto unregister_dev; 2761 } 2762 #endif 2763 2764 dev_info(&dev->intf->dev, 2765 "V4L2 video device registered as %s\n", 2766 video_device_node_name(&v4l2->vdev)); 2767 2768 if (video_is_registered(&v4l2->vbi_dev)) 2769 dev_info(&dev->intf->dev, 2770 "V4L2 VBI device registered as %s\n", 2771 video_device_node_name(&v4l2->vbi_dev)); 2772 2773 /* Save some power by putting tuner to sleep */ 2774 v4l2_device_call_all(&v4l2->v4l2_dev, 0, core, s_power, 0); 2775 2776 /* initialize videobuf2 stuff */ 2777 em28xx_vb2_setup(dev); 2778 2779 dev_info(&dev->intf->dev, 2780 "V4L2 extension successfully initialized\n"); 2781 2782 kref_get(&dev->ref); 2783 2784 mutex_unlock(&dev->lock); 2785 return 0; 2786 2787 unregister_dev: 2788 if (video_is_registered(&v4l2->radio_dev)) { 2789 dev_info(&dev->intf->dev, 2790 "V4L2 device %s deregistered\n", 2791 video_device_node_name(&v4l2->radio_dev)); 2792 video_unregister_device(&v4l2->radio_dev); 2793 } 2794 if (video_is_registered(&v4l2->vbi_dev)) { 2795 dev_info(&dev->intf->dev, 2796 "V4L2 device %s deregistered\n", 2797 video_device_node_name(&v4l2->vbi_dev)); 2798 video_unregister_device(&v4l2->vbi_dev); 2799 } 2800 if (video_is_registered(&v4l2->vdev)) { 2801 dev_info(&dev->intf->dev, 2802 "V4L2 device %s deregistered\n", 2803 video_device_node_name(&v4l2->vdev)); 2804 video_unregister_device(&v4l2->vdev); 2805 } 2806 2807 v4l2_ctrl_handler_free(&v4l2->ctrl_handler); 2808 v4l2_device_unregister(&v4l2->v4l2_dev); 2809 err: 2810 dev->v4l2 = NULL; 2811 kref_put(&v4l2->ref, em28xx_free_v4l2); 2812 mutex_unlock(&dev->lock); 2813 return ret; 2814 } 2815 2816 static struct em28xx_ops v4l2_ops = { 2817 .id = EM28XX_V4L2, 2818 .name = "Em28xx v4l2 Extension", 2819 .init = em28xx_v4l2_init, 2820 .fini = em28xx_v4l2_fini, 2821 .suspend = em28xx_v4l2_suspend, 2822 .resume = em28xx_v4l2_resume, 2823 }; 2824 2825 static int __init em28xx_video_register(void) 2826 { 2827 return em28xx_register_extension(&v4l2_ops); 2828 } 2829 2830 static void __exit em28xx_video_unregister(void) 2831 { 2832 em28xx_unregister_extension(&v4l2_ops); 2833 } 2834 2835 module_init(em28xx_video_register); 2836 module_exit(em28xx_video_unregister); 2837