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