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