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