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