1 /* 2 3 bttv - Bt848 frame grabber driver 4 5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de> 6 & Marcus Metzler <mocm@thp.uni-koeln.de> 7 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org> 8 9 some v4l2 code lines are taken from Justin's bttv2 driver which is 10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za> 11 12 V4L1 removal from: 13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru> 14 15 Fixes to be fully V4L2 compliant by 16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org> 17 18 Cropping and overscan support 19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at> 20 Sponsored by OPQ Systems AB 21 22 This program is free software; you can redistribute it and/or modify 23 it under the terms of the GNU General Public License as published by 24 the Free Software Foundation; either version 2 of the License, or 25 (at your option) any later version. 26 27 This program is distributed in the hope that it will be useful, 28 but WITHOUT ANY WARRANTY; without even the implied warranty of 29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 30 GNU General Public License for more details. 31 32 You should have received a copy of the GNU General Public License 33 along with this program; if not, write to the Free Software 34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 35 */ 36 37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 38 39 #include <linux/init.h> 40 #include <linux/module.h> 41 #include <linux/delay.h> 42 #include <linux/slab.h> 43 #include <linux/errno.h> 44 #include <linux/fs.h> 45 #include <linux/kernel.h> 46 #include <linux/sched.h> 47 #include <linux/interrupt.h> 48 #include <linux/kdev_t.h> 49 #include "bttvp.h" 50 #include <media/v4l2-common.h> 51 #include <media/v4l2-ioctl.h> 52 #include <media/tvaudio.h> 53 #include <media/msp3400.h> 54 55 #include <linux/dma-mapping.h> 56 57 #include <asm/io.h> 58 #include <asm/byteorder.h> 59 60 #include <media/saa6588.h> 61 62 #define BTTV_VERSION "0.9.19" 63 64 unsigned int bttv_num; /* number of Bt848s in use */ 65 struct bttv *bttvs[BTTV_MAX]; 66 67 unsigned int bttv_debug; 68 unsigned int bttv_verbose = 1; 69 unsigned int bttv_gpio; 70 71 /* config variables */ 72 #ifdef __BIG_ENDIAN 73 static unsigned int bigendian=1; 74 #else 75 static unsigned int bigendian; 76 #endif 77 static unsigned int radio[BTTV_MAX]; 78 static unsigned int irq_debug; 79 static unsigned int gbuffers = 8; 80 static unsigned int gbufsize = 0x208000; 81 static unsigned int reset_crop = 1; 82 83 static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 }; 84 static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 }; 85 static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 }; 86 static int debug_latency; 87 static int disable_ir; 88 89 static unsigned int fdsr; 90 91 /* options */ 92 static unsigned int combfilter; 93 static unsigned int lumafilter; 94 static unsigned int automute = 1; 95 static unsigned int chroma_agc; 96 static unsigned int adc_crush = 1; 97 static unsigned int whitecrush_upper = 0xCF; 98 static unsigned int whitecrush_lower = 0x7F; 99 static unsigned int vcr_hack; 100 static unsigned int irq_iswitch; 101 static unsigned int uv_ratio = 50; 102 static unsigned int full_luma_range; 103 static unsigned int coring; 104 105 /* API features (turn on/off stuff for testing) */ 106 static unsigned int v4l2 = 1; 107 108 /* insmod args */ 109 module_param(bttv_verbose, int, 0644); 110 module_param(bttv_gpio, int, 0644); 111 module_param(bttv_debug, int, 0644); 112 module_param(irq_debug, int, 0644); 113 module_param(debug_latency, int, 0644); 114 module_param(disable_ir, int, 0444); 115 116 module_param(fdsr, int, 0444); 117 module_param(gbuffers, int, 0444); 118 module_param(gbufsize, int, 0444); 119 module_param(reset_crop, int, 0444); 120 121 module_param(v4l2, int, 0644); 122 module_param(bigendian, int, 0644); 123 module_param(irq_iswitch, int, 0644); 124 module_param(combfilter, int, 0444); 125 module_param(lumafilter, int, 0444); 126 module_param(automute, int, 0444); 127 module_param(chroma_agc, int, 0444); 128 module_param(adc_crush, int, 0444); 129 module_param(whitecrush_upper, int, 0444); 130 module_param(whitecrush_lower, int, 0444); 131 module_param(vcr_hack, int, 0444); 132 module_param(uv_ratio, int, 0444); 133 module_param(full_luma_range, int, 0444); 134 module_param(coring, int, 0444); 135 136 module_param_array(radio, int, NULL, 0444); 137 module_param_array(video_nr, int, NULL, 0444); 138 module_param_array(radio_nr, int, NULL, 0444); 139 module_param_array(vbi_nr, int, NULL, 0444); 140 141 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)"); 142 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian"); 143 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)"); 144 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)"); 145 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)"); 146 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)"); 147 MODULE_PARM_DESC(disable_ir, "disable infrared remote support"); 148 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8"); 149 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000"); 150 MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default " 151 "is 1 (yes) for compatibility with older applications"); 152 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)"); 153 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)"); 154 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)"); 155 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207"); 156 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127"); 157 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)"); 158 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler"); 159 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50"); 160 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)"); 161 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)"); 162 MODULE_PARM_DESC(video_nr, "video device numbers"); 163 MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); 164 MODULE_PARM_DESC(radio_nr, "radio device numbers"); 165 166 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards"); 167 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr"); 168 MODULE_LICENSE("GPL"); 169 MODULE_VERSION(BTTV_VERSION); 170 171 /* ----------------------------------------------------------------------- */ 172 /* sysfs */ 173 174 static ssize_t show_card(struct device *cd, 175 struct device_attribute *attr, char *buf) 176 { 177 struct video_device *vfd = container_of(cd, struct video_device, dev); 178 struct bttv *btv = video_get_drvdata(vfd); 179 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET); 180 } 181 static DEVICE_ATTR(card, S_IRUGO, show_card, NULL); 182 183 /* ----------------------------------------------------------------------- */ 184 /* dvb auto-load setup */ 185 #if defined(CONFIG_MODULES) && defined(MODULE) 186 static void request_module_async(struct work_struct *work) 187 { 188 request_module("dvb-bt8xx"); 189 } 190 191 static void request_modules(struct bttv *dev) 192 { 193 INIT_WORK(&dev->request_module_wk, request_module_async); 194 schedule_work(&dev->request_module_wk); 195 } 196 197 static void flush_request_modules(struct bttv *dev) 198 { 199 flush_work_sync(&dev->request_module_wk); 200 } 201 #else 202 #define request_modules(dev) 203 #define flush_request_modules(dev) 204 #endif /* CONFIG_MODULES */ 205 206 207 /* ----------------------------------------------------------------------- */ 208 /* static data */ 209 210 /* special timing tables from conexant... */ 211 static u8 SRAM_Table[][60] = 212 { 213 /* PAL digital input over GPIO[7:0] */ 214 { 215 45, // 45 bytes following 216 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16, 217 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00, 218 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00, 219 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37, 220 0x37,0x00,0xAF,0x21,0x00 221 }, 222 /* NTSC digital input over GPIO[7:0] */ 223 { 224 51, // 51 bytes following 225 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06, 226 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00, 227 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07, 228 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6, 229 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21, 230 0x00, 231 }, 232 // TGB_NTSC392 // quartzsight 233 // This table has been modified to be used for Fusion Rev D 234 { 235 0x2A, // size of table = 42 236 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24, 237 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10, 238 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00, 239 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3, 240 0x20, 0x00 241 } 242 }; 243 244 /* minhdelayx1 first video pixel we can capture on a line and 245 hdelayx1 start of active video, both relative to rising edge of 246 /HRESET pulse (0H) in 1 / fCLKx1. 247 swidth width of active video and 248 totalwidth total line width, both in 1 / fCLKx1. 249 sqwidth total line width in square pixels. 250 vdelay start of active video in 2 * field lines relative to 251 trailing edge of /VRESET pulse (VDELAY register). 252 sheight height of active video in 2 * field lines. 253 videostart0 ITU-R frame line number of the line corresponding 254 to vdelay in the first field. */ 255 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \ 256 vdelay, sheight, videostart0) \ 257 .cropcap.bounds.left = minhdelayx1, \ 258 /* * 2 because vertically we count field lines times two, */ \ 259 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \ 260 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \ 261 /* 4 is a safety margin at the end of the line. */ \ 262 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \ 263 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \ 264 .cropcap.defrect.left = hdelayx1, \ 265 .cropcap.defrect.top = (videostart0) * 2, \ 266 .cropcap.defrect.width = swidth, \ 267 .cropcap.defrect.height = sheight, \ 268 .cropcap.pixelaspect.numerator = totalwidth, \ 269 .cropcap.pixelaspect.denominator = sqwidth, 270 271 const struct bttv_tvnorm bttv_tvnorms[] = { 272 /* PAL-BDGHI */ 273 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */ 274 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */ 275 { 276 .v4l2_id = V4L2_STD_PAL, 277 .name = "PAL", 278 .Fsc = 35468950, 279 .swidth = 924, 280 .sheight = 576, 281 .totalwidth = 1135, 282 .adelay = 0x7f, 283 .bdelay = 0x72, 284 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1), 285 .scaledtwidth = 1135, 286 .hdelayx1 = 186, 287 .hactivex1 = 924, 288 .vdelay = 0x20, 289 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */ 290 .sram = 0, 291 /* ITU-R frame line number of the first VBI line 292 we can capture, of the first and second field. 293 The last line is determined by cropcap.bounds. */ 294 .vbistart = { 7, 320 }, 295 CROPCAP(/* minhdelayx1 */ 68, 296 /* hdelayx1 */ 186, 297 /* Should be (768 * 1135 + 944 / 2) / 944. 298 cropcap.defrect is used for image width 299 checks, so we keep the old value 924. */ 300 /* swidth */ 924, 301 /* totalwidth */ 1135, 302 /* sqwidth */ 944, 303 /* vdelay */ 0x20, 304 /* sheight */ 576, 305 /* videostart0 */ 23) 306 /* bt878 (and bt848?) can capture another 307 line below active video. */ 308 .cropcap.bounds.height = (576 + 2) + 0x20 - 2, 309 },{ 310 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR, 311 .name = "NTSC", 312 .Fsc = 28636363, 313 .swidth = 768, 314 .sheight = 480, 315 .totalwidth = 910, 316 .adelay = 0x68, 317 .bdelay = 0x5d, 318 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0), 319 .scaledtwidth = 910, 320 .hdelayx1 = 128, 321 .hactivex1 = 910, 322 .vdelay = 0x1a, 323 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */ 324 .sram = 1, 325 .vbistart = { 10, 273 }, 326 CROPCAP(/* minhdelayx1 */ 68, 327 /* hdelayx1 */ 128, 328 /* Should be (640 * 910 + 780 / 2) / 780? */ 329 /* swidth */ 768, 330 /* totalwidth */ 910, 331 /* sqwidth */ 780, 332 /* vdelay */ 0x1a, 333 /* sheight */ 480, 334 /* videostart0 */ 23) 335 },{ 336 .v4l2_id = V4L2_STD_SECAM, 337 .name = "SECAM", 338 .Fsc = 35468950, 339 .swidth = 924, 340 .sheight = 576, 341 .totalwidth = 1135, 342 .adelay = 0x7f, 343 .bdelay = 0xb0, 344 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1), 345 .scaledtwidth = 1135, 346 .hdelayx1 = 186, 347 .hactivex1 = 922, 348 .vdelay = 0x20, 349 .vbipack = 255, 350 .sram = 0, /* like PAL, correct? */ 351 .vbistart = { 7, 320 }, 352 CROPCAP(/* minhdelayx1 */ 68, 353 /* hdelayx1 */ 186, 354 /* swidth */ 924, 355 /* totalwidth */ 1135, 356 /* sqwidth */ 944, 357 /* vdelay */ 0x20, 358 /* sheight */ 576, 359 /* videostart0 */ 23) 360 },{ 361 .v4l2_id = V4L2_STD_PAL_Nc, 362 .name = "PAL-Nc", 363 .Fsc = 28636363, 364 .swidth = 640, 365 .sheight = 576, 366 .totalwidth = 910, 367 .adelay = 0x68, 368 .bdelay = 0x5d, 369 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0), 370 .scaledtwidth = 780, 371 .hdelayx1 = 130, 372 .hactivex1 = 734, 373 .vdelay = 0x1a, 374 .vbipack = 144, 375 .sram = -1, 376 .vbistart = { 7, 320 }, 377 CROPCAP(/* minhdelayx1 */ 68, 378 /* hdelayx1 */ 130, 379 /* swidth */ (640 * 910 + 780 / 2) / 780, 380 /* totalwidth */ 910, 381 /* sqwidth */ 780, 382 /* vdelay */ 0x1a, 383 /* sheight */ 576, 384 /* videostart0 */ 23) 385 },{ 386 .v4l2_id = V4L2_STD_PAL_M, 387 .name = "PAL-M", 388 .Fsc = 28636363, 389 .swidth = 640, 390 .sheight = 480, 391 .totalwidth = 910, 392 .adelay = 0x68, 393 .bdelay = 0x5d, 394 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0), 395 .scaledtwidth = 780, 396 .hdelayx1 = 135, 397 .hactivex1 = 754, 398 .vdelay = 0x1a, 399 .vbipack = 144, 400 .sram = -1, 401 .vbistart = { 10, 273 }, 402 CROPCAP(/* minhdelayx1 */ 68, 403 /* hdelayx1 */ 135, 404 /* swidth */ (640 * 910 + 780 / 2) / 780, 405 /* totalwidth */ 910, 406 /* sqwidth */ 780, 407 /* vdelay */ 0x1a, 408 /* sheight */ 480, 409 /* videostart0 */ 23) 410 },{ 411 .v4l2_id = V4L2_STD_PAL_N, 412 .name = "PAL-N", 413 .Fsc = 35468950, 414 .swidth = 768, 415 .sheight = 576, 416 .totalwidth = 1135, 417 .adelay = 0x7f, 418 .bdelay = 0x72, 419 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1), 420 .scaledtwidth = 944, 421 .hdelayx1 = 186, 422 .hactivex1 = 922, 423 .vdelay = 0x20, 424 .vbipack = 144, 425 .sram = -1, 426 .vbistart = { 7, 320 }, 427 CROPCAP(/* minhdelayx1 */ 68, 428 /* hdelayx1 */ 186, 429 /* swidth */ (768 * 1135 + 944 / 2) / 944, 430 /* totalwidth */ 1135, 431 /* sqwidth */ 944, 432 /* vdelay */ 0x20, 433 /* sheight */ 576, 434 /* videostart0 */ 23) 435 },{ 436 .v4l2_id = V4L2_STD_NTSC_M_JP, 437 .name = "NTSC-JP", 438 .Fsc = 28636363, 439 .swidth = 640, 440 .sheight = 480, 441 .totalwidth = 910, 442 .adelay = 0x68, 443 .bdelay = 0x5d, 444 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0), 445 .scaledtwidth = 780, 446 .hdelayx1 = 135, 447 .hactivex1 = 754, 448 .vdelay = 0x16, 449 .vbipack = 144, 450 .sram = -1, 451 .vbistart = { 10, 273 }, 452 CROPCAP(/* minhdelayx1 */ 68, 453 /* hdelayx1 */ 135, 454 /* swidth */ (640 * 910 + 780 / 2) / 780, 455 /* totalwidth */ 910, 456 /* sqwidth */ 780, 457 /* vdelay */ 0x16, 458 /* sheight */ 480, 459 /* videostart0 */ 23) 460 },{ 461 /* that one hopefully works with the strange timing 462 * which video recorders produce when playing a NTSC 463 * tape on a PAL TV ... */ 464 .v4l2_id = V4L2_STD_PAL_60, 465 .name = "PAL-60", 466 .Fsc = 35468950, 467 .swidth = 924, 468 .sheight = 480, 469 .totalwidth = 1135, 470 .adelay = 0x7f, 471 .bdelay = 0x72, 472 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1), 473 .scaledtwidth = 1135, 474 .hdelayx1 = 186, 475 .hactivex1 = 924, 476 .vdelay = 0x1a, 477 .vbipack = 255, 478 .vtotal = 524, 479 .sram = -1, 480 .vbistart = { 10, 273 }, 481 CROPCAP(/* minhdelayx1 */ 68, 482 /* hdelayx1 */ 186, 483 /* swidth */ 924, 484 /* totalwidth */ 1135, 485 /* sqwidth */ 944, 486 /* vdelay */ 0x1a, 487 /* sheight */ 480, 488 /* videostart0 */ 23) 489 } 490 }; 491 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms); 492 493 /* ----------------------------------------------------------------------- */ 494 /* bttv format list 495 packed pixel formats must come first */ 496 static const struct bttv_format formats[] = { 497 { 498 .name = "8 bpp, gray", 499 .fourcc = V4L2_PIX_FMT_GREY, 500 .btformat = BT848_COLOR_FMT_Y8, 501 .depth = 8, 502 .flags = FORMAT_FLAGS_PACKED, 503 },{ 504 .name = "8 bpp, dithered color", 505 .fourcc = V4L2_PIX_FMT_HI240, 506 .btformat = BT848_COLOR_FMT_RGB8, 507 .depth = 8, 508 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER, 509 },{ 510 .name = "15 bpp RGB, le", 511 .fourcc = V4L2_PIX_FMT_RGB555, 512 .btformat = BT848_COLOR_FMT_RGB15, 513 .depth = 16, 514 .flags = FORMAT_FLAGS_PACKED, 515 },{ 516 .name = "15 bpp RGB, be", 517 .fourcc = V4L2_PIX_FMT_RGB555X, 518 .btformat = BT848_COLOR_FMT_RGB15, 519 .btswap = 0x03, /* byteswap */ 520 .depth = 16, 521 .flags = FORMAT_FLAGS_PACKED, 522 },{ 523 .name = "16 bpp RGB, le", 524 .fourcc = V4L2_PIX_FMT_RGB565, 525 .btformat = BT848_COLOR_FMT_RGB16, 526 .depth = 16, 527 .flags = FORMAT_FLAGS_PACKED, 528 },{ 529 .name = "16 bpp RGB, be", 530 .fourcc = V4L2_PIX_FMT_RGB565X, 531 .btformat = BT848_COLOR_FMT_RGB16, 532 .btswap = 0x03, /* byteswap */ 533 .depth = 16, 534 .flags = FORMAT_FLAGS_PACKED, 535 },{ 536 .name = "24 bpp RGB, le", 537 .fourcc = V4L2_PIX_FMT_BGR24, 538 .btformat = BT848_COLOR_FMT_RGB24, 539 .depth = 24, 540 .flags = FORMAT_FLAGS_PACKED, 541 },{ 542 .name = "32 bpp RGB, le", 543 .fourcc = V4L2_PIX_FMT_BGR32, 544 .btformat = BT848_COLOR_FMT_RGB32, 545 .depth = 32, 546 .flags = FORMAT_FLAGS_PACKED, 547 },{ 548 .name = "32 bpp RGB, be", 549 .fourcc = V4L2_PIX_FMT_RGB32, 550 .btformat = BT848_COLOR_FMT_RGB32, 551 .btswap = 0x0f, /* byte+word swap */ 552 .depth = 32, 553 .flags = FORMAT_FLAGS_PACKED, 554 },{ 555 .name = "4:2:2, packed, YUYV", 556 .fourcc = V4L2_PIX_FMT_YUYV, 557 .btformat = BT848_COLOR_FMT_YUY2, 558 .depth = 16, 559 .flags = FORMAT_FLAGS_PACKED, 560 },{ 561 .name = "4:2:2, packed, UYVY", 562 .fourcc = V4L2_PIX_FMT_UYVY, 563 .btformat = BT848_COLOR_FMT_YUY2, 564 .btswap = 0x03, /* byteswap */ 565 .depth = 16, 566 .flags = FORMAT_FLAGS_PACKED, 567 },{ 568 .name = "4:2:2, planar, Y-Cb-Cr", 569 .fourcc = V4L2_PIX_FMT_YUV422P, 570 .btformat = BT848_COLOR_FMT_YCrCb422, 571 .depth = 16, 572 .flags = FORMAT_FLAGS_PLANAR, 573 .hshift = 1, 574 .vshift = 0, 575 },{ 576 .name = "4:2:0, planar, Y-Cb-Cr", 577 .fourcc = V4L2_PIX_FMT_YUV420, 578 .btformat = BT848_COLOR_FMT_YCrCb422, 579 .depth = 12, 580 .flags = FORMAT_FLAGS_PLANAR, 581 .hshift = 1, 582 .vshift = 1, 583 },{ 584 .name = "4:2:0, planar, Y-Cr-Cb", 585 .fourcc = V4L2_PIX_FMT_YVU420, 586 .btformat = BT848_COLOR_FMT_YCrCb422, 587 .depth = 12, 588 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb, 589 .hshift = 1, 590 .vshift = 1, 591 },{ 592 .name = "4:1:1, planar, Y-Cb-Cr", 593 .fourcc = V4L2_PIX_FMT_YUV411P, 594 .btformat = BT848_COLOR_FMT_YCrCb411, 595 .depth = 12, 596 .flags = FORMAT_FLAGS_PLANAR, 597 .hshift = 2, 598 .vshift = 0, 599 },{ 600 .name = "4:1:0, planar, Y-Cb-Cr", 601 .fourcc = V4L2_PIX_FMT_YUV410, 602 .btformat = BT848_COLOR_FMT_YCrCb411, 603 .depth = 9, 604 .flags = FORMAT_FLAGS_PLANAR, 605 .hshift = 2, 606 .vshift = 2, 607 },{ 608 .name = "4:1:0, planar, Y-Cr-Cb", 609 .fourcc = V4L2_PIX_FMT_YVU410, 610 .btformat = BT848_COLOR_FMT_YCrCb411, 611 .depth = 9, 612 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb, 613 .hshift = 2, 614 .vshift = 2, 615 },{ 616 .name = "raw scanlines", 617 .fourcc = -1, 618 .btformat = BT848_COLOR_FMT_RAW, 619 .depth = 8, 620 .flags = FORMAT_FLAGS_RAW, 621 } 622 }; 623 static const unsigned int FORMATS = ARRAY_SIZE(formats); 624 625 /* ----------------------------------------------------------------------- */ 626 627 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0) 628 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1) 629 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2) 630 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3) 631 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4) 632 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5) 633 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6) 634 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7) 635 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8) 636 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9) 637 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10) 638 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11) 639 640 static const struct v4l2_queryctrl no_ctl = { 641 .name = "42", 642 .flags = V4L2_CTRL_FLAG_DISABLED, 643 }; 644 static const struct v4l2_queryctrl bttv_ctls[] = { 645 /* --- video --- */ 646 { 647 .id = V4L2_CID_BRIGHTNESS, 648 .name = "Brightness", 649 .minimum = 0, 650 .maximum = 65535, 651 .step = 256, 652 .default_value = 32768, 653 .type = V4L2_CTRL_TYPE_INTEGER, 654 },{ 655 .id = V4L2_CID_CONTRAST, 656 .name = "Contrast", 657 .minimum = 0, 658 .maximum = 65535, 659 .step = 128, 660 .default_value = 27648, 661 .type = V4L2_CTRL_TYPE_INTEGER, 662 },{ 663 .id = V4L2_CID_SATURATION, 664 .name = "Saturation", 665 .minimum = 0, 666 .maximum = 65535, 667 .step = 128, 668 .default_value = 32768, 669 .type = V4L2_CTRL_TYPE_INTEGER, 670 },{ 671 .id = V4L2_CID_HUE, 672 .name = "Hue", 673 .minimum = 0, 674 .maximum = 65535, 675 .step = 256, 676 .default_value = 32768, 677 .type = V4L2_CTRL_TYPE_INTEGER, 678 }, 679 /* --- audio --- */ 680 { 681 .id = V4L2_CID_AUDIO_MUTE, 682 .name = "Mute", 683 .minimum = 0, 684 .maximum = 1, 685 .type = V4L2_CTRL_TYPE_BOOLEAN, 686 },{ 687 .id = V4L2_CID_AUDIO_VOLUME, 688 .name = "Volume", 689 .minimum = 0, 690 .maximum = 65535, 691 .step = 65535/100, 692 .default_value = 65535, 693 .type = V4L2_CTRL_TYPE_INTEGER, 694 },{ 695 .id = V4L2_CID_AUDIO_BALANCE, 696 .name = "Balance", 697 .minimum = 0, 698 .maximum = 65535, 699 .step = 65535/100, 700 .default_value = 32768, 701 .type = V4L2_CTRL_TYPE_INTEGER, 702 },{ 703 .id = V4L2_CID_AUDIO_BASS, 704 .name = "Bass", 705 .minimum = 0, 706 .maximum = 65535, 707 .step = 65535/100, 708 .default_value = 32768, 709 .type = V4L2_CTRL_TYPE_INTEGER, 710 },{ 711 .id = V4L2_CID_AUDIO_TREBLE, 712 .name = "Treble", 713 .minimum = 0, 714 .maximum = 65535, 715 .step = 65535/100, 716 .default_value = 32768, 717 .type = V4L2_CTRL_TYPE_INTEGER, 718 }, 719 /* --- private --- */ 720 { 721 .id = V4L2_CID_PRIVATE_CHROMA_AGC, 722 .name = "chroma agc", 723 .minimum = 0, 724 .maximum = 1, 725 .type = V4L2_CTRL_TYPE_BOOLEAN, 726 },{ 727 .id = V4L2_CID_PRIVATE_COMBFILTER, 728 .name = "combfilter", 729 .minimum = 0, 730 .maximum = 1, 731 .type = V4L2_CTRL_TYPE_BOOLEAN, 732 },{ 733 .id = V4L2_CID_PRIVATE_AUTOMUTE, 734 .name = "automute", 735 .minimum = 0, 736 .maximum = 1, 737 .type = V4L2_CTRL_TYPE_BOOLEAN, 738 },{ 739 .id = V4L2_CID_PRIVATE_LUMAFILTER, 740 .name = "luma decimation filter", 741 .minimum = 0, 742 .maximum = 1, 743 .type = V4L2_CTRL_TYPE_BOOLEAN, 744 },{ 745 .id = V4L2_CID_PRIVATE_AGC_CRUSH, 746 .name = "agc crush", 747 .minimum = 0, 748 .maximum = 1, 749 .type = V4L2_CTRL_TYPE_BOOLEAN, 750 },{ 751 .id = V4L2_CID_PRIVATE_VCR_HACK, 752 .name = "vcr hack", 753 .minimum = 0, 754 .maximum = 1, 755 .type = V4L2_CTRL_TYPE_BOOLEAN, 756 },{ 757 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER, 758 .name = "whitecrush upper", 759 .minimum = 0, 760 .maximum = 255, 761 .step = 1, 762 .default_value = 0xCF, 763 .type = V4L2_CTRL_TYPE_INTEGER, 764 },{ 765 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER, 766 .name = "whitecrush lower", 767 .minimum = 0, 768 .maximum = 255, 769 .step = 1, 770 .default_value = 0x7F, 771 .type = V4L2_CTRL_TYPE_INTEGER, 772 },{ 773 .id = V4L2_CID_PRIVATE_UV_RATIO, 774 .name = "uv ratio", 775 .minimum = 0, 776 .maximum = 100, 777 .step = 1, 778 .default_value = 50, 779 .type = V4L2_CTRL_TYPE_INTEGER, 780 },{ 781 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE, 782 .name = "full luma range", 783 .minimum = 0, 784 .maximum = 1, 785 .type = V4L2_CTRL_TYPE_BOOLEAN, 786 },{ 787 .id = V4L2_CID_PRIVATE_CORING, 788 .name = "coring", 789 .minimum = 0, 790 .maximum = 3, 791 .step = 1, 792 .default_value = 0, 793 .type = V4L2_CTRL_TYPE_INTEGER, 794 } 795 796 797 798 }; 799 800 static const struct v4l2_queryctrl *ctrl_by_id(int id) 801 { 802 int i; 803 804 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++) 805 if (bttv_ctls[i].id == id) 806 return bttv_ctls+i; 807 808 return NULL; 809 } 810 811 /* ----------------------------------------------------------------------- */ 812 /* resource management */ 813 814 /* 815 RESOURCE_ allocated by freed by 816 817 VIDEO_READ bttv_read 1) bttv_read 2) 818 819 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF 820 VIDIOC_QBUF 1) bttv_release 821 VIDIOCMCAPTURE 1) 822 823 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off 824 VIDIOC_OVERLAY on VIDIOC_OVERLAY off 825 3) bttv_release 826 827 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF 828 VIDIOC_QBUF 1) bttv_release 829 bttv_read, bttv_poll 1) 4) 830 831 1) The resource must be allocated when we enter buffer prepare functions 832 and remain allocated while buffers are in the DMA queue. 833 2) This is a single frame read. 834 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when 835 RESOURCE_OVERLAY is allocated. 836 4) This is a continuous read, implies VIDIOC_STREAMON. 837 838 Note this driver permits video input and standard changes regardless if 839 resources are allocated. 840 */ 841 842 #define VBI_RESOURCES (RESOURCE_VBI) 843 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \ 844 RESOURCE_VIDEO_STREAM | \ 845 RESOURCE_OVERLAY) 846 847 static 848 int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit) 849 { 850 int xbits; /* mutual exclusive resources */ 851 852 if (fh->resources & bit) 853 /* have it already allocated */ 854 return 1; 855 856 xbits = bit; 857 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM)) 858 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM; 859 860 /* is it free? */ 861 if (btv->resources & xbits) { 862 /* no, someone else uses it */ 863 goto fail; 864 } 865 866 if ((bit & VIDEO_RESOURCES) 867 && 0 == (btv->resources & VIDEO_RESOURCES)) { 868 /* Do crop - use current, don't - use default parameters. */ 869 __s32 top = btv->crop[!!fh->do_crop].rect.top; 870 871 if (btv->vbi_end > top) 872 goto fail; 873 874 /* We cannot capture the same line as video and VBI data. 875 Claim scan lines crop[].rect.top to bottom. */ 876 btv->crop_start = top; 877 } else if (bit & VBI_RESOURCES) { 878 __s32 end = fh->vbi_fmt.end; 879 880 if (end > btv->crop_start) 881 goto fail; 882 883 /* Claim scan lines above fh->vbi_fmt.end. */ 884 btv->vbi_end = end; 885 } 886 887 /* it's free, grab it */ 888 fh->resources |= bit; 889 btv->resources |= bit; 890 return 1; 891 892 fail: 893 return 0; 894 } 895 896 static 897 int check_btres(struct bttv_fh *fh, int bit) 898 { 899 return (fh->resources & bit); 900 } 901 902 static 903 int locked_btres(struct bttv *btv, int bit) 904 { 905 return (btv->resources & bit); 906 } 907 908 /* Call with btv->lock down. */ 909 static void 910 disclaim_vbi_lines(struct bttv *btv) 911 { 912 btv->vbi_end = 0; 913 } 914 915 /* Call with btv->lock down. */ 916 static void 917 disclaim_video_lines(struct bttv *btv) 918 { 919 const struct bttv_tvnorm *tvnorm; 920 u8 crop; 921 922 tvnorm = &bttv_tvnorms[btv->tvnorm]; 923 btv->crop_start = tvnorm->cropcap.bounds.top 924 + tvnorm->cropcap.bounds.height; 925 926 /* VBI capturing ends at VDELAY, start of video capturing, no 927 matter how many lines the VBI RISC program expects. When video 928 capturing is off, it shall no longer "preempt" VBI capturing, 929 so we set VDELAY to maximum. */ 930 crop = btread(BT848_E_CROP) | 0xc0; 931 btwrite(crop, BT848_E_CROP); 932 btwrite(0xfe, BT848_E_VDELAY_LO); 933 btwrite(crop, BT848_O_CROP); 934 btwrite(0xfe, BT848_O_VDELAY_LO); 935 } 936 937 static 938 void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits) 939 { 940 if ((fh->resources & bits) != bits) { 941 /* trying to free resources not allocated by us ... */ 942 pr_err("BUG! (btres)\n"); 943 } 944 fh->resources &= ~bits; 945 btv->resources &= ~bits; 946 947 bits = btv->resources; 948 949 if (0 == (bits & VIDEO_RESOURCES)) 950 disclaim_video_lines(btv); 951 952 if (0 == (bits & VBI_RESOURCES)) 953 disclaim_vbi_lines(btv); 954 } 955 956 /* ----------------------------------------------------------------------- */ 957 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */ 958 959 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C 960 PLL_X = Reference pre-divider (0=1, 1=2) 961 PLL_C = Post divider (0=6, 1=4) 962 PLL_I = Integer input 963 PLL_F = Fractional input 964 965 F_input = 28.636363 MHz: 966 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0 967 */ 968 969 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout) 970 { 971 unsigned char fl, fh, fi; 972 973 /* prevent overflows */ 974 fin/=4; 975 fout/=4; 976 977 fout*=12; 978 fi=fout/fin; 979 980 fout=(fout%fin)*256; 981 fh=fout/fin; 982 983 fout=(fout%fin)*256; 984 fl=fout/fin; 985 986 btwrite(fl, BT848_PLL_F_LO); 987 btwrite(fh, BT848_PLL_F_HI); 988 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI); 989 } 990 991 static void set_pll(struct bttv *btv) 992 { 993 int i; 994 995 if (!btv->pll.pll_crystal) 996 return; 997 998 if (btv->pll.pll_ofreq == btv->pll.pll_current) { 999 dprintk("%d: PLL: no change required\n", btv->c.nr); 1000 return; 1001 } 1002 1003 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) { 1004 /* no PLL needed */ 1005 if (btv->pll.pll_current == 0) 1006 return; 1007 if (bttv_verbose) 1008 pr_info("%d: PLL can sleep, using XTAL (%d)\n", 1009 btv->c.nr, btv->pll.pll_ifreq); 1010 btwrite(0x00,BT848_TGCTRL); 1011 btwrite(0x00,BT848_PLL_XCI); 1012 btv->pll.pll_current = 0; 1013 return; 1014 } 1015 1016 if (bttv_verbose) 1017 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n", 1018 btv->c.nr, 1019 btv->pll.pll_ifreq, btv->pll.pll_ofreq); 1020 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq); 1021 1022 for (i=0; i<10; i++) { 1023 /* Let other people run while the PLL stabilizes */ 1024 msleep(10); 1025 1026 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) { 1027 btwrite(0,BT848_DSTATUS); 1028 } else { 1029 btwrite(0x08,BT848_TGCTRL); 1030 btv->pll.pll_current = btv->pll.pll_ofreq; 1031 if (bttv_verbose) 1032 pr_info("PLL set ok\n"); 1033 return; 1034 } 1035 } 1036 btv->pll.pll_current = -1; 1037 if (bttv_verbose) 1038 pr_info("Setting PLL failed\n"); 1039 return; 1040 } 1041 1042 /* used to switch between the bt848's analog/digital video capture modes */ 1043 static void bt848A_set_timing(struct bttv *btv) 1044 { 1045 int i, len; 1046 int table_idx = bttv_tvnorms[btv->tvnorm].sram; 1047 int fsc = bttv_tvnorms[btv->tvnorm].Fsc; 1048 1049 if (btv->input == btv->dig) { 1050 dprintk("%d: load digital timing table (table_idx=%d)\n", 1051 btv->c.nr,table_idx); 1052 1053 /* timing change...reset timing generator address */ 1054 btwrite(0x00, BT848_TGCTRL); 1055 btwrite(0x02, BT848_TGCTRL); 1056 btwrite(0x00, BT848_TGCTRL); 1057 1058 len=SRAM_Table[table_idx][0]; 1059 for(i = 1; i <= len; i++) 1060 btwrite(SRAM_Table[table_idx][i],BT848_TGLB); 1061 btv->pll.pll_ofreq = 27000000; 1062 1063 set_pll(btv); 1064 btwrite(0x11, BT848_TGCTRL); 1065 btwrite(0x41, BT848_DVSIF); 1066 } else { 1067 btv->pll.pll_ofreq = fsc; 1068 set_pll(btv); 1069 btwrite(0x0, BT848_DVSIF); 1070 } 1071 } 1072 1073 /* ----------------------------------------------------------------------- */ 1074 1075 static void bt848_bright(struct bttv *btv, int bright) 1076 { 1077 int value; 1078 1079 // printk("set bright: %d\n", bright); // DEBUG 1080 btv->bright = bright; 1081 1082 /* We want -128 to 127 we get 0-65535 */ 1083 value = (bright >> 8) - 128; 1084 btwrite(value & 0xff, BT848_BRIGHT); 1085 } 1086 1087 static void bt848_hue(struct bttv *btv, int hue) 1088 { 1089 int value; 1090 1091 btv->hue = hue; 1092 1093 /* -128 to 127 */ 1094 value = (hue >> 8) - 128; 1095 btwrite(value & 0xff, BT848_HUE); 1096 } 1097 1098 static void bt848_contrast(struct bttv *btv, int cont) 1099 { 1100 int value,hibit; 1101 1102 btv->contrast = cont; 1103 1104 /* 0-511 */ 1105 value = (cont >> 7); 1106 hibit = (value >> 6) & 4; 1107 btwrite(value & 0xff, BT848_CONTRAST_LO); 1108 btaor(hibit, ~4, BT848_E_CONTROL); 1109 btaor(hibit, ~4, BT848_O_CONTROL); 1110 } 1111 1112 static void bt848_sat(struct bttv *btv, int color) 1113 { 1114 int val_u,val_v,hibits; 1115 1116 btv->saturation = color; 1117 1118 /* 0-511 for the color */ 1119 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7; 1120 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254; 1121 hibits = (val_u >> 7) & 2; 1122 hibits |= (val_v >> 8) & 1; 1123 btwrite(val_u & 0xff, BT848_SAT_U_LO); 1124 btwrite(val_v & 0xff, BT848_SAT_V_LO); 1125 btaor(hibits, ~3, BT848_E_CONTROL); 1126 btaor(hibits, ~3, BT848_O_CONTROL); 1127 } 1128 1129 /* ----------------------------------------------------------------------- */ 1130 1131 static int 1132 video_mux(struct bttv *btv, unsigned int input) 1133 { 1134 int mux,mask2; 1135 1136 if (input >= bttv_tvcards[btv->c.type].video_inputs) 1137 return -EINVAL; 1138 1139 /* needed by RemoteVideo MX */ 1140 mask2 = bttv_tvcards[btv->c.type].gpiomask2; 1141 if (mask2) 1142 gpio_inout(mask2,mask2); 1143 1144 if (input == btv->svhs) { 1145 btor(BT848_CONTROL_COMP, BT848_E_CONTROL); 1146 btor(BT848_CONTROL_COMP, BT848_O_CONTROL); 1147 } else { 1148 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL); 1149 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL); 1150 } 1151 mux = bttv_muxsel(btv, input); 1152 btaor(mux<<5, ~(3<<5), BT848_IFORM); 1153 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux); 1154 1155 /* card specific hook */ 1156 if(bttv_tvcards[btv->c.type].muxsel_hook) 1157 bttv_tvcards[btv->c.type].muxsel_hook (btv, input); 1158 return 0; 1159 } 1160 1161 static char *audio_modes[] = { 1162 "audio: tuner", "audio: radio", "audio: extern", 1163 "audio: intern", "audio: mute" 1164 }; 1165 1166 static int 1167 audio_mux(struct bttv *btv, int input, int mute) 1168 { 1169 int gpio_val, signal; 1170 struct v4l2_control ctrl; 1171 1172 gpio_inout(bttv_tvcards[btv->c.type].gpiomask, 1173 bttv_tvcards[btv->c.type].gpiomask); 1174 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC; 1175 1176 btv->mute = mute; 1177 btv->audio = input; 1178 1179 /* automute */ 1180 mute = mute || (btv->opt_automute && !signal && !btv->radio_user); 1181 1182 if (mute) 1183 gpio_val = bttv_tvcards[btv->c.type].gpiomute; 1184 else 1185 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input]; 1186 1187 switch (btv->c.type) { 1188 case BTTV_BOARD_VOODOOTV_FM: 1189 case BTTV_BOARD_VOODOOTV_200: 1190 gpio_val = bttv_tda9880_setnorm(btv, gpio_val); 1191 break; 1192 1193 default: 1194 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val); 1195 } 1196 1197 if (bttv_gpio) 1198 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]); 1199 if (in_interrupt()) 1200 return 0; 1201 1202 ctrl.id = V4L2_CID_AUDIO_MUTE; 1203 ctrl.value = btv->mute; 1204 bttv_call_all(btv, core, s_ctrl, &ctrl); 1205 if (btv->sd_msp34xx) { 1206 u32 in; 1207 1208 /* Note: the inputs tuner/radio/extern/intern are translated 1209 to msp routings. This assumes common behavior for all msp3400 1210 based TV cards. When this assumption fails, then the 1211 specific MSP routing must be added to the card table. 1212 For now this is sufficient. */ 1213 switch (input) { 1214 case TVAUDIO_INPUT_RADIO: 1215 /* Some boards need the msp do to the radio demod */ 1216 if (btv->radio_uses_msp_demodulator) { 1217 in = MSP_INPUT_DEFAULT; 1218 break; 1219 } 1220 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1, 1221 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART); 1222 break; 1223 case TVAUDIO_INPUT_EXTERN: 1224 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1, 1225 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART); 1226 break; 1227 case TVAUDIO_INPUT_INTERN: 1228 /* Yes, this is the same input as for RADIO. I doubt 1229 if this is ever used. The only board with an INTERN 1230 input is the BTTV_BOARD_AVERMEDIA98. I wonder how 1231 that was tested. My guess is that the whole INTERN 1232 input does not work. */ 1233 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1, 1234 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART); 1235 break; 1236 case TVAUDIO_INPUT_TUNER: 1237 default: 1238 /* This is the only card that uses TUNER2, and afaik, 1239 is the only difference between the VOODOOTV_FM 1240 and VOODOOTV_200 */ 1241 if (btv->c.type == BTTV_BOARD_VOODOOTV_200) 1242 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \ 1243 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER); 1244 else 1245 in = MSP_INPUT_DEFAULT; 1246 break; 1247 } 1248 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing, 1249 in, MSP_OUTPUT_DEFAULT, 0); 1250 } 1251 if (btv->sd_tvaudio) { 1252 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing, 1253 input, 0, 0); 1254 } 1255 return 0; 1256 } 1257 1258 static inline int 1259 audio_mute(struct bttv *btv, int mute) 1260 { 1261 return audio_mux(btv, btv->audio, mute); 1262 } 1263 1264 static inline int 1265 audio_input(struct bttv *btv, int input) 1266 { 1267 return audio_mux(btv, input, btv->mute); 1268 } 1269 1270 static void 1271 bttv_crop_calc_limits(struct bttv_crop *c) 1272 { 1273 /* Scale factor min. 1:1, max. 16:1. Min. image size 1274 48 x 32. Scaled width must be a multiple of 4. */ 1275 1276 if (1) { 1277 /* For bug compatibility with VIDIOCGCAP and image 1278 size checks in earlier driver versions. */ 1279 c->min_scaled_width = 48; 1280 c->min_scaled_height = 32; 1281 } else { 1282 c->min_scaled_width = 1283 (max(48, c->rect.width >> 4) + 3) & ~3; 1284 c->min_scaled_height = 1285 max(32, c->rect.height >> 4); 1286 } 1287 1288 c->max_scaled_width = c->rect.width & ~3; 1289 c->max_scaled_height = c->rect.height; 1290 } 1291 1292 static void 1293 bttv_crop_reset(struct bttv_crop *c, unsigned int norm) 1294 { 1295 c->rect = bttv_tvnorms[norm].cropcap.defrect; 1296 bttv_crop_calc_limits(c); 1297 } 1298 1299 /* Call with btv->lock down. */ 1300 static int 1301 set_tvnorm(struct bttv *btv, unsigned int norm) 1302 { 1303 const struct bttv_tvnorm *tvnorm; 1304 v4l2_std_id id; 1305 1306 BUG_ON(norm >= BTTV_TVNORMS); 1307 BUG_ON(btv->tvnorm >= BTTV_TVNORMS); 1308 1309 tvnorm = &bttv_tvnorms[norm]; 1310 1311 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap, 1312 sizeof (tvnorm->cropcap))) { 1313 bttv_crop_reset(&btv->crop[0], norm); 1314 btv->crop[1] = btv->crop[0]; /* current = default */ 1315 1316 if (0 == (btv->resources & VIDEO_RESOURCES)) { 1317 btv->crop_start = tvnorm->cropcap.bounds.top 1318 + tvnorm->cropcap.bounds.height; 1319 } 1320 } 1321 1322 btv->tvnorm = norm; 1323 1324 btwrite(tvnorm->adelay, BT848_ADELAY); 1325 btwrite(tvnorm->bdelay, BT848_BDELAY); 1326 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH), 1327 BT848_IFORM); 1328 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE); 1329 btwrite(1, BT848_VBI_PACK_DEL); 1330 bt848A_set_timing(btv); 1331 1332 switch (btv->c.type) { 1333 case BTTV_BOARD_VOODOOTV_FM: 1334 case BTTV_BOARD_VOODOOTV_200: 1335 bttv_tda9880_setnorm(btv, gpio_read()); 1336 break; 1337 } 1338 id = tvnorm->v4l2_id; 1339 bttv_call_all(btv, core, s_std, id); 1340 1341 return 0; 1342 } 1343 1344 /* Call with btv->lock down. */ 1345 static void 1346 set_input(struct bttv *btv, unsigned int input, unsigned int norm) 1347 { 1348 unsigned long flags; 1349 1350 btv->input = input; 1351 if (irq_iswitch) { 1352 spin_lock_irqsave(&btv->s_lock,flags); 1353 if (btv->curr.frame_irq) { 1354 /* active capture -> delayed input switch */ 1355 btv->new_input = input; 1356 } else { 1357 video_mux(btv,input); 1358 } 1359 spin_unlock_irqrestore(&btv->s_lock,flags); 1360 } else { 1361 video_mux(btv,input); 1362 } 1363 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ? 1364 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN); 1365 set_tvnorm(btv, norm); 1366 } 1367 1368 static void init_irqreg(struct bttv *btv) 1369 { 1370 /* clear status */ 1371 btwrite(0xfffffUL, BT848_INT_STAT); 1372 1373 if (bttv_tvcards[btv->c.type].no_video) { 1374 /* i2c only */ 1375 btwrite(BT848_INT_I2CDONE, 1376 BT848_INT_MASK); 1377 } else { 1378 /* full video */ 1379 btwrite((btv->triton1) | 1380 (btv->gpioirq ? BT848_INT_GPINT : 0) | 1381 BT848_INT_SCERR | 1382 (fdsr ? BT848_INT_FDSR : 0) | 1383 BT848_INT_RISCI | BT848_INT_OCERR | 1384 BT848_INT_FMTCHG|BT848_INT_HLOCK| 1385 BT848_INT_I2CDONE, 1386 BT848_INT_MASK); 1387 } 1388 } 1389 1390 static void init_bt848(struct bttv *btv) 1391 { 1392 int val; 1393 1394 if (bttv_tvcards[btv->c.type].no_video) { 1395 /* very basic init only */ 1396 init_irqreg(btv); 1397 return; 1398 } 1399 1400 btwrite(0x00, BT848_CAP_CTL); 1401 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL); 1402 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM); 1403 1404 /* set planar and packed mode trigger points and */ 1405 /* set rising edge of inverted GPINTR pin as irq trigger */ 1406 btwrite(BT848_GPIO_DMA_CTL_PKTP_32| 1407 BT848_GPIO_DMA_CTL_PLTP1_16| 1408 BT848_GPIO_DMA_CTL_PLTP23_16| 1409 BT848_GPIO_DMA_CTL_GPINTC| 1410 BT848_GPIO_DMA_CTL_GPINTI, 1411 BT848_GPIO_DMA_CTL); 1412 1413 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0; 1414 btwrite(val, BT848_E_SCLOOP); 1415 btwrite(val, BT848_O_SCLOOP); 1416 1417 btwrite(0x20, BT848_E_VSCALE_HI); 1418 btwrite(0x20, BT848_O_VSCALE_HI); 1419 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0), 1420 BT848_ADC); 1421 1422 btwrite(whitecrush_upper, BT848_WC_UP); 1423 btwrite(whitecrush_lower, BT848_WC_DOWN); 1424 1425 if (btv->opt_lumafilter) { 1426 btwrite(0, BT848_E_CONTROL); 1427 btwrite(0, BT848_O_CONTROL); 1428 } else { 1429 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL); 1430 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL); 1431 } 1432 1433 bt848_bright(btv, btv->bright); 1434 bt848_hue(btv, btv->hue); 1435 bt848_contrast(btv, btv->contrast); 1436 bt848_sat(btv, btv->saturation); 1437 1438 /* interrupt */ 1439 init_irqreg(btv); 1440 } 1441 1442 static void bttv_reinit_bt848(struct bttv *btv) 1443 { 1444 unsigned long flags; 1445 1446 if (bttv_verbose) 1447 pr_info("%d: reset, reinitialize\n", btv->c.nr); 1448 spin_lock_irqsave(&btv->s_lock,flags); 1449 btv->errors=0; 1450 bttv_set_dma(btv,0); 1451 spin_unlock_irqrestore(&btv->s_lock,flags); 1452 1453 init_bt848(btv); 1454 btv->pll.pll_current = -1; 1455 set_input(btv, btv->input, btv->tvnorm); 1456 } 1457 1458 static int bttv_g_ctrl(struct file *file, void *priv, 1459 struct v4l2_control *c) 1460 { 1461 struct bttv_fh *fh = priv; 1462 struct bttv *btv = fh->btv; 1463 1464 switch (c->id) { 1465 case V4L2_CID_BRIGHTNESS: 1466 c->value = btv->bright; 1467 break; 1468 case V4L2_CID_HUE: 1469 c->value = btv->hue; 1470 break; 1471 case V4L2_CID_CONTRAST: 1472 c->value = btv->contrast; 1473 break; 1474 case V4L2_CID_SATURATION: 1475 c->value = btv->saturation; 1476 break; 1477 1478 case V4L2_CID_AUDIO_MUTE: 1479 case V4L2_CID_AUDIO_VOLUME: 1480 case V4L2_CID_AUDIO_BALANCE: 1481 case V4L2_CID_AUDIO_BASS: 1482 case V4L2_CID_AUDIO_TREBLE: 1483 bttv_call_all(btv, core, g_ctrl, c); 1484 break; 1485 1486 case V4L2_CID_PRIVATE_CHROMA_AGC: 1487 c->value = btv->opt_chroma_agc; 1488 break; 1489 case V4L2_CID_PRIVATE_COMBFILTER: 1490 c->value = btv->opt_combfilter; 1491 break; 1492 case V4L2_CID_PRIVATE_LUMAFILTER: 1493 c->value = btv->opt_lumafilter; 1494 break; 1495 case V4L2_CID_PRIVATE_AUTOMUTE: 1496 c->value = btv->opt_automute; 1497 break; 1498 case V4L2_CID_PRIVATE_AGC_CRUSH: 1499 c->value = btv->opt_adc_crush; 1500 break; 1501 case V4L2_CID_PRIVATE_VCR_HACK: 1502 c->value = btv->opt_vcr_hack; 1503 break; 1504 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER: 1505 c->value = btv->opt_whitecrush_upper; 1506 break; 1507 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER: 1508 c->value = btv->opt_whitecrush_lower; 1509 break; 1510 case V4L2_CID_PRIVATE_UV_RATIO: 1511 c->value = btv->opt_uv_ratio; 1512 break; 1513 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE: 1514 c->value = btv->opt_full_luma_range; 1515 break; 1516 case V4L2_CID_PRIVATE_CORING: 1517 c->value = btv->opt_coring; 1518 break; 1519 default: 1520 return -EINVAL; 1521 } 1522 return 0; 1523 } 1524 1525 static int bttv_s_ctrl(struct file *file, void *f, 1526 struct v4l2_control *c) 1527 { 1528 int err; 1529 int val; 1530 struct bttv_fh *fh = f; 1531 struct bttv *btv = fh->btv; 1532 1533 err = v4l2_prio_check(&btv->prio, fh->prio); 1534 if (0 != err) 1535 return err; 1536 1537 switch (c->id) { 1538 case V4L2_CID_BRIGHTNESS: 1539 bt848_bright(btv, c->value); 1540 break; 1541 case V4L2_CID_HUE: 1542 bt848_hue(btv, c->value); 1543 break; 1544 case V4L2_CID_CONTRAST: 1545 bt848_contrast(btv, c->value); 1546 break; 1547 case V4L2_CID_SATURATION: 1548 bt848_sat(btv, c->value); 1549 break; 1550 case V4L2_CID_AUDIO_MUTE: 1551 audio_mute(btv, c->value); 1552 /* fall through */ 1553 case V4L2_CID_AUDIO_VOLUME: 1554 if (btv->volume_gpio) 1555 btv->volume_gpio(btv, c->value); 1556 1557 bttv_call_all(btv, core, s_ctrl, c); 1558 break; 1559 case V4L2_CID_AUDIO_BALANCE: 1560 case V4L2_CID_AUDIO_BASS: 1561 case V4L2_CID_AUDIO_TREBLE: 1562 bttv_call_all(btv, core, s_ctrl, c); 1563 break; 1564 1565 case V4L2_CID_PRIVATE_CHROMA_AGC: 1566 btv->opt_chroma_agc = c->value; 1567 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0; 1568 btwrite(val, BT848_E_SCLOOP); 1569 btwrite(val, BT848_O_SCLOOP); 1570 break; 1571 case V4L2_CID_PRIVATE_COMBFILTER: 1572 btv->opt_combfilter = c->value; 1573 break; 1574 case V4L2_CID_PRIVATE_LUMAFILTER: 1575 btv->opt_lumafilter = c->value; 1576 if (btv->opt_lumafilter) { 1577 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL); 1578 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL); 1579 } else { 1580 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL); 1581 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL); 1582 } 1583 break; 1584 case V4L2_CID_PRIVATE_AUTOMUTE: 1585 btv->opt_automute = c->value; 1586 break; 1587 case V4L2_CID_PRIVATE_AGC_CRUSH: 1588 btv->opt_adc_crush = c->value; 1589 btwrite(BT848_ADC_RESERVED | 1590 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0), 1591 BT848_ADC); 1592 break; 1593 case V4L2_CID_PRIVATE_VCR_HACK: 1594 btv->opt_vcr_hack = c->value; 1595 break; 1596 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER: 1597 btv->opt_whitecrush_upper = c->value; 1598 btwrite(c->value, BT848_WC_UP); 1599 break; 1600 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER: 1601 btv->opt_whitecrush_lower = c->value; 1602 btwrite(c->value, BT848_WC_DOWN); 1603 break; 1604 case V4L2_CID_PRIVATE_UV_RATIO: 1605 btv->opt_uv_ratio = c->value; 1606 bt848_sat(btv, btv->saturation); 1607 break; 1608 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE: 1609 btv->opt_full_luma_range = c->value; 1610 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM); 1611 break; 1612 case V4L2_CID_PRIVATE_CORING: 1613 btv->opt_coring = c->value; 1614 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM); 1615 break; 1616 default: 1617 return -EINVAL; 1618 } 1619 return 0; 1620 } 1621 1622 /* ----------------------------------------------------------------------- */ 1623 1624 void bttv_gpio_tracking(struct bttv *btv, char *comment) 1625 { 1626 unsigned int outbits, data; 1627 outbits = btread(BT848_GPIO_OUT_EN); 1628 data = btread(BT848_GPIO_DATA); 1629 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n", 1630 btv->c.nr, outbits, data & outbits, data & ~outbits, comment); 1631 } 1632 1633 static void bttv_field_count(struct bttv *btv) 1634 { 1635 int need_count = 0; 1636 1637 if (btv->users) 1638 need_count++; 1639 1640 if (need_count) { 1641 /* start field counter */ 1642 btor(BT848_INT_VSYNC,BT848_INT_MASK); 1643 } else { 1644 /* stop field counter */ 1645 btand(~BT848_INT_VSYNC,BT848_INT_MASK); 1646 btv->field_count = 0; 1647 } 1648 } 1649 1650 static const struct bttv_format* 1651 format_by_fourcc(int fourcc) 1652 { 1653 unsigned int i; 1654 1655 for (i = 0; i < FORMATS; i++) { 1656 if (-1 == formats[i].fourcc) 1657 continue; 1658 if (formats[i].fourcc == fourcc) 1659 return formats+i; 1660 } 1661 return NULL; 1662 } 1663 1664 /* ----------------------------------------------------------------------- */ 1665 /* misc helpers */ 1666 1667 static int 1668 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh, 1669 struct bttv_buffer *new) 1670 { 1671 struct bttv_buffer *old; 1672 unsigned long flags; 1673 int retval = 0; 1674 1675 dprintk("switch_overlay: enter [new=%p]\n", new); 1676 if (new) 1677 new->vb.state = VIDEOBUF_DONE; 1678 spin_lock_irqsave(&btv->s_lock,flags); 1679 old = btv->screen; 1680 btv->screen = new; 1681 btv->loop_irq |= 1; 1682 bttv_set_dma(btv, 0x03); 1683 spin_unlock_irqrestore(&btv->s_lock,flags); 1684 if (NULL != old) { 1685 dprintk("switch_overlay: old=%p state is %d\n", 1686 old, old->vb.state); 1687 bttv_dma_free(&fh->cap,btv, old); 1688 kfree(old); 1689 } 1690 if (NULL == new) 1691 free_btres_lock(btv,fh,RESOURCE_OVERLAY); 1692 dprintk("switch_overlay: done\n"); 1693 return retval; 1694 } 1695 1696 /* ----------------------------------------------------------------------- */ 1697 /* video4linux (1) interface */ 1698 1699 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv, 1700 struct bttv_buffer *buf, 1701 const struct bttv_format *fmt, 1702 unsigned int width, unsigned int height, 1703 enum v4l2_field field) 1704 { 1705 struct bttv_fh *fh = q->priv_data; 1706 int redo_dma_risc = 0; 1707 struct bttv_crop c; 1708 int norm; 1709 int rc; 1710 1711 /* check settings */ 1712 if (NULL == fmt) 1713 return -EINVAL; 1714 if (fmt->btformat == BT848_COLOR_FMT_RAW) { 1715 width = RAW_BPL; 1716 height = RAW_LINES*2; 1717 if (width*height > buf->vb.bsize) 1718 return -EINVAL; 1719 buf->vb.size = buf->vb.bsize; 1720 1721 /* Make sure tvnorm and vbi_end remain consistent 1722 until we're done. */ 1723 1724 norm = btv->tvnorm; 1725 1726 /* In this mode capturing always starts at defrect.top 1727 (default VDELAY), ignoring cropping parameters. */ 1728 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) { 1729 return -EINVAL; 1730 } 1731 1732 c.rect = bttv_tvnorms[norm].cropcap.defrect; 1733 } else { 1734 norm = btv->tvnorm; 1735 c = btv->crop[!!fh->do_crop]; 1736 1737 if (width < c.min_scaled_width || 1738 width > c.max_scaled_width || 1739 height < c.min_scaled_height) 1740 return -EINVAL; 1741 1742 switch (field) { 1743 case V4L2_FIELD_TOP: 1744 case V4L2_FIELD_BOTTOM: 1745 case V4L2_FIELD_ALTERNATE: 1746 /* btv->crop counts frame lines. Max. scale 1747 factor is 16:1 for frames, 8:1 for fields. */ 1748 if (height * 2 > c.max_scaled_height) 1749 return -EINVAL; 1750 break; 1751 1752 default: 1753 if (height > c.max_scaled_height) 1754 return -EINVAL; 1755 break; 1756 } 1757 1758 buf->vb.size = (width * height * fmt->depth) >> 3; 1759 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 1760 return -EINVAL; 1761 } 1762 1763 /* alloc + fill struct bttv_buffer (if changed) */ 1764 if (buf->vb.width != width || buf->vb.height != height || 1765 buf->vb.field != field || 1766 buf->tvnorm != norm || buf->fmt != fmt || 1767 buf->crop.top != c.rect.top || 1768 buf->crop.left != c.rect.left || 1769 buf->crop.width != c.rect.width || 1770 buf->crop.height != c.rect.height) { 1771 buf->vb.width = width; 1772 buf->vb.height = height; 1773 buf->vb.field = field; 1774 buf->tvnorm = norm; 1775 buf->fmt = fmt; 1776 buf->crop = c.rect; 1777 redo_dma_risc = 1; 1778 } 1779 1780 /* alloc risc memory */ 1781 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 1782 redo_dma_risc = 1; 1783 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf))) 1784 goto fail; 1785 } 1786 1787 if (redo_dma_risc) 1788 if (0 != (rc = bttv_buffer_risc(btv,buf))) 1789 goto fail; 1790 1791 buf->vb.state = VIDEOBUF_PREPARED; 1792 return 0; 1793 1794 fail: 1795 bttv_dma_free(q,btv,buf); 1796 return rc; 1797 } 1798 1799 static int 1800 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) 1801 { 1802 struct bttv_fh *fh = q->priv_data; 1803 1804 *size = fh->fmt->depth*fh->width*fh->height >> 3; 1805 if (0 == *count) 1806 *count = gbuffers; 1807 if (*size * *count > gbuffers * gbufsize) 1808 *count = (gbuffers * gbufsize) / *size; 1809 return 0; 1810 } 1811 1812 static int 1813 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, 1814 enum v4l2_field field) 1815 { 1816 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb); 1817 struct bttv_fh *fh = q->priv_data; 1818 1819 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt, 1820 fh->width, fh->height, field); 1821 } 1822 1823 static void 1824 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) 1825 { 1826 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb); 1827 struct bttv_fh *fh = q->priv_data; 1828 struct bttv *btv = fh->btv; 1829 1830 buf->vb.state = VIDEOBUF_QUEUED; 1831 list_add_tail(&buf->vb.queue,&btv->capture); 1832 if (!btv->curr.frame_irq) { 1833 btv->loop_irq |= 1; 1834 bttv_set_dma(btv, 0x03); 1835 } 1836 } 1837 1838 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb) 1839 { 1840 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb); 1841 struct bttv_fh *fh = q->priv_data; 1842 1843 bttv_dma_free(q,fh->btv,buf); 1844 } 1845 1846 static struct videobuf_queue_ops bttv_video_qops = { 1847 .buf_setup = buffer_setup, 1848 .buf_prepare = buffer_prepare, 1849 .buf_queue = buffer_queue, 1850 .buf_release = buffer_release, 1851 }; 1852 1853 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id) 1854 { 1855 struct bttv_fh *fh = priv; 1856 struct bttv *btv = fh->btv; 1857 unsigned int i; 1858 int err; 1859 1860 err = v4l2_prio_check(&btv->prio, fh->prio); 1861 if (err) 1862 goto err; 1863 1864 for (i = 0; i < BTTV_TVNORMS; i++) 1865 if (*id & bttv_tvnorms[i].v4l2_id) 1866 break; 1867 if (i == BTTV_TVNORMS) { 1868 err = -EINVAL; 1869 goto err; 1870 } 1871 1872 set_tvnorm(btv, i); 1873 1874 err: 1875 1876 return err; 1877 } 1878 1879 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id) 1880 { 1881 struct bttv_fh *fh = f; 1882 struct bttv *btv = fh->btv; 1883 1884 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML) 1885 *id = V4L2_STD_625_50; 1886 else 1887 *id = V4L2_STD_525_60; 1888 return 0; 1889 } 1890 1891 static int bttv_enum_input(struct file *file, void *priv, 1892 struct v4l2_input *i) 1893 { 1894 struct bttv_fh *fh = priv; 1895 struct bttv *btv = fh->btv; 1896 int rc = 0; 1897 1898 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) { 1899 rc = -EINVAL; 1900 goto err; 1901 } 1902 1903 i->type = V4L2_INPUT_TYPE_CAMERA; 1904 i->audioset = 1; 1905 1906 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) { 1907 sprintf(i->name, "Television"); 1908 i->type = V4L2_INPUT_TYPE_TUNER; 1909 i->tuner = 0; 1910 } else if (i->index == btv->svhs) { 1911 sprintf(i->name, "S-Video"); 1912 } else { 1913 sprintf(i->name, "Composite%d", i->index); 1914 } 1915 1916 if (i->index == btv->input) { 1917 __u32 dstatus = btread(BT848_DSTATUS); 1918 if (0 == (dstatus & BT848_DSTATUS_PRES)) 1919 i->status |= V4L2_IN_ST_NO_SIGNAL; 1920 if (0 == (dstatus & BT848_DSTATUS_HLOC)) 1921 i->status |= V4L2_IN_ST_NO_H_LOCK; 1922 } 1923 1924 i->std = BTTV_NORMS; 1925 1926 err: 1927 1928 return rc; 1929 } 1930 1931 static int bttv_g_input(struct file *file, void *priv, unsigned int *i) 1932 { 1933 struct bttv_fh *fh = priv; 1934 struct bttv *btv = fh->btv; 1935 1936 *i = btv->input; 1937 1938 return 0; 1939 } 1940 1941 static int bttv_s_input(struct file *file, void *priv, unsigned int i) 1942 { 1943 struct bttv_fh *fh = priv; 1944 struct bttv *btv = fh->btv; 1945 1946 int err; 1947 1948 err = v4l2_prio_check(&btv->prio, fh->prio); 1949 if (unlikely(err)) 1950 goto err; 1951 1952 if (i > bttv_tvcards[btv->c.type].video_inputs) { 1953 err = -EINVAL; 1954 goto err; 1955 } 1956 1957 set_input(btv, i, btv->tvnorm); 1958 1959 err: 1960 return 0; 1961 } 1962 1963 static int bttv_s_tuner(struct file *file, void *priv, 1964 struct v4l2_tuner *t) 1965 { 1966 struct bttv_fh *fh = priv; 1967 struct bttv *btv = fh->btv; 1968 int err; 1969 1970 if (unlikely(0 != t->index)) 1971 return -EINVAL; 1972 1973 if (unlikely(btv->tuner_type == TUNER_ABSENT)) { 1974 err = -EINVAL; 1975 goto err; 1976 } 1977 1978 err = v4l2_prio_check(&btv->prio, fh->prio); 1979 if (unlikely(err)) 1980 goto err; 1981 1982 bttv_call_all(btv, tuner, s_tuner, t); 1983 1984 if (btv->audio_mode_gpio) 1985 btv->audio_mode_gpio(btv, t, 1); 1986 1987 err: 1988 1989 return 0; 1990 } 1991 1992 static int bttv_g_frequency(struct file *file, void *priv, 1993 struct v4l2_frequency *f) 1994 { 1995 struct bttv_fh *fh = priv; 1996 struct bttv *btv = fh->btv; 1997 1998 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 1999 f->frequency = btv->freq; 2000 2001 return 0; 2002 } 2003 2004 static int bttv_s_frequency(struct file *file, void *priv, 2005 struct v4l2_frequency *f) 2006 { 2007 struct bttv_fh *fh = priv; 2008 struct bttv *btv = fh->btv; 2009 int err; 2010 2011 if (unlikely(f->tuner != 0)) 2012 return -EINVAL; 2013 2014 err = v4l2_prio_check(&btv->prio, fh->prio); 2015 if (unlikely(err)) 2016 goto err; 2017 2018 if (unlikely(f->type != (btv->radio_user 2019 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) { 2020 err = -EINVAL; 2021 goto err; 2022 } 2023 btv->freq = f->frequency; 2024 bttv_call_all(btv, tuner, s_frequency, f); 2025 if (btv->has_matchbox && btv->radio_user) 2026 tea5757_set_freq(btv, btv->freq); 2027 err: 2028 2029 return 0; 2030 } 2031 2032 static int bttv_log_status(struct file *file, void *f) 2033 { 2034 struct bttv_fh *fh = f; 2035 struct bttv *btv = fh->btv; 2036 2037 bttv_call_all(btv, core, log_status); 2038 return 0; 2039 } 2040 2041 #ifdef CONFIG_VIDEO_ADV_DEBUG 2042 static int bttv_g_register(struct file *file, void *f, 2043 struct v4l2_dbg_register *reg) 2044 { 2045 struct bttv_fh *fh = f; 2046 struct bttv *btv = fh->btv; 2047 2048 if (!capable(CAP_SYS_ADMIN)) 2049 return -EPERM; 2050 2051 if (!v4l2_chip_match_host(®->match)) 2052 return -EINVAL; 2053 2054 /* bt848 has a 12-bit register space */ 2055 reg->reg &= 0xfff; 2056 reg->val = btread(reg->reg); 2057 reg->size = 1; 2058 2059 return 0; 2060 } 2061 2062 static int bttv_s_register(struct file *file, void *f, 2063 struct v4l2_dbg_register *reg) 2064 { 2065 struct bttv_fh *fh = f; 2066 struct bttv *btv = fh->btv; 2067 2068 if (!capable(CAP_SYS_ADMIN)) 2069 return -EPERM; 2070 2071 if (!v4l2_chip_match_host(®->match)) 2072 return -EINVAL; 2073 2074 /* bt848 has a 12-bit register space */ 2075 reg->reg &= 0xfff; 2076 btwrite(reg->val, reg->reg); 2077 2078 return 0; 2079 } 2080 #endif 2081 2082 /* Given cropping boundaries b and the scaled width and height of a 2083 single field or frame, which must not exceed hardware limits, this 2084 function adjusts the cropping parameters c. */ 2085 static void 2086 bttv_crop_adjust (struct bttv_crop * c, 2087 const struct v4l2_rect * b, 2088 __s32 width, 2089 __s32 height, 2090 enum v4l2_field field) 2091 { 2092 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field); 2093 __s32 max_left; 2094 __s32 max_top; 2095 2096 if (width < c->min_scaled_width) { 2097 /* Max. hor. scale factor 16:1. */ 2098 c->rect.width = width * 16; 2099 } else if (width > c->max_scaled_width) { 2100 /* Min. hor. scale factor 1:1. */ 2101 c->rect.width = width; 2102 2103 max_left = b->left + b->width - width; 2104 max_left = min(max_left, (__s32) MAX_HDELAY); 2105 if (c->rect.left > max_left) 2106 c->rect.left = max_left; 2107 } 2108 2109 if (height < c->min_scaled_height) { 2110 /* Max. vert. scale factor 16:1, single fields 8:1. */ 2111 c->rect.height = height * 16; 2112 } else if (frame_height > c->max_scaled_height) { 2113 /* Min. vert. scale factor 1:1. 2114 Top and height count field lines times two. */ 2115 c->rect.height = (frame_height + 1) & ~1; 2116 2117 max_top = b->top + b->height - c->rect.height; 2118 if (c->rect.top > max_top) 2119 c->rect.top = max_top; 2120 } 2121 2122 bttv_crop_calc_limits(c); 2123 } 2124 2125 /* Returns an error if scaling to a frame or single field with the given 2126 width and height is not possible with the current cropping parameters 2127 and width aligned according to width_mask. If adjust_size is TRUE the 2128 function may adjust the width and/or height instead, rounding width 2129 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may 2130 also adjust the current cropping parameters to get closer to the 2131 desired image size. */ 2132 static int 2133 limit_scaled_size_lock (struct bttv_fh * fh, 2134 __s32 * width, 2135 __s32 * height, 2136 enum v4l2_field field, 2137 unsigned int width_mask, 2138 unsigned int width_bias, 2139 int adjust_size, 2140 int adjust_crop) 2141 { 2142 struct bttv *btv = fh->btv; 2143 const struct v4l2_rect *b; 2144 struct bttv_crop *c; 2145 __s32 min_width; 2146 __s32 min_height; 2147 __s32 max_width; 2148 __s32 max_height; 2149 int rc; 2150 2151 BUG_ON((int) width_mask >= 0 || 2152 width_bias >= (unsigned int) -width_mask); 2153 2154 /* Make sure tvnorm, vbi_end and the current cropping parameters 2155 remain consistent until we're done. */ 2156 2157 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds; 2158 2159 /* Do crop - use current, don't - use default parameters. */ 2160 c = &btv->crop[!!fh->do_crop]; 2161 2162 if (fh->do_crop 2163 && adjust_size 2164 && adjust_crop 2165 && !locked_btres(btv, VIDEO_RESOURCES)) { 2166 min_width = 48; 2167 min_height = 32; 2168 2169 /* We cannot scale up. When the scaled image is larger 2170 than crop.rect we adjust the crop.rect as required 2171 by the V4L2 spec, hence cropcap.bounds are our limit. */ 2172 max_width = min(b->width, (__s32) MAX_HACTIVE); 2173 max_height = b->height; 2174 2175 /* We cannot capture the same line as video and VBI data. 2176 Note btv->vbi_end is really a minimum, see 2177 bttv_vbi_try_fmt(). */ 2178 if (btv->vbi_end > b->top) { 2179 max_height -= btv->vbi_end - b->top; 2180 rc = -EBUSY; 2181 if (min_height > max_height) 2182 goto fail; 2183 } 2184 } else { 2185 rc = -EBUSY; 2186 if (btv->vbi_end > c->rect.top) 2187 goto fail; 2188 2189 min_width = c->min_scaled_width; 2190 min_height = c->min_scaled_height; 2191 max_width = c->max_scaled_width; 2192 max_height = c->max_scaled_height; 2193 2194 adjust_crop = 0; 2195 } 2196 2197 min_width = (min_width - width_mask - 1) & width_mask; 2198 max_width = max_width & width_mask; 2199 2200 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */ 2201 min_height = min_height; 2202 /* Min. scale factor is 1:1. */ 2203 max_height >>= !V4L2_FIELD_HAS_BOTH(field); 2204 2205 if (adjust_size) { 2206 *width = clamp(*width, min_width, max_width); 2207 *height = clamp(*height, min_height, max_height); 2208 2209 /* Round after clamping to avoid overflow. */ 2210 *width = (*width + width_bias) & width_mask; 2211 2212 if (adjust_crop) { 2213 bttv_crop_adjust(c, b, *width, *height, field); 2214 2215 if (btv->vbi_end > c->rect.top) { 2216 /* Move the crop window out of the way. */ 2217 c->rect.top = btv->vbi_end; 2218 } 2219 } 2220 } else { 2221 rc = -EINVAL; 2222 if (*width < min_width || 2223 *height < min_height || 2224 *width > max_width || 2225 *height > max_height || 2226 0 != (*width & ~width_mask)) 2227 goto fail; 2228 } 2229 2230 rc = 0; /* success */ 2231 2232 fail: 2233 2234 return rc; 2235 } 2236 2237 /* Returns an error if the given overlay window dimensions are not 2238 possible with the current cropping parameters. If adjust_size is 2239 TRUE the function may adjust the window width and/or height 2240 instead, however it always rounds the horizontal position and 2241 width as btcx_align() does. If adjust_crop is TRUE the function 2242 may also adjust the current cropping parameters to get closer 2243 to the desired window size. */ 2244 static int 2245 verify_window_lock (struct bttv_fh * fh, 2246 struct v4l2_window * win, 2247 int adjust_size, 2248 int adjust_crop) 2249 { 2250 enum v4l2_field field; 2251 unsigned int width_mask; 2252 int rc; 2253 2254 if (win->w.width < 48 || win->w.height < 32) 2255 return -EINVAL; 2256 if (win->clipcount > 2048) 2257 return -EINVAL; 2258 2259 field = win->field; 2260 2261 if (V4L2_FIELD_ANY == field) { 2262 __s32 height2; 2263 2264 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1; 2265 field = (win->w.height > height2) 2266 ? V4L2_FIELD_INTERLACED 2267 : V4L2_FIELD_TOP; 2268 } 2269 switch (field) { 2270 case V4L2_FIELD_TOP: 2271 case V4L2_FIELD_BOTTOM: 2272 case V4L2_FIELD_INTERLACED: 2273 break; 2274 default: 2275 return -EINVAL; 2276 } 2277 2278 /* 4-byte alignment. */ 2279 if (NULL == fh->ovfmt) 2280 return -EINVAL; 2281 width_mask = ~0; 2282 switch (fh->ovfmt->depth) { 2283 case 8: 2284 case 24: 2285 width_mask = ~3; 2286 break; 2287 case 16: 2288 width_mask = ~1; 2289 break; 2290 case 32: 2291 break; 2292 default: 2293 BUG(); 2294 } 2295 2296 win->w.width -= win->w.left & ~width_mask; 2297 win->w.left = (win->w.left - width_mask - 1) & width_mask; 2298 2299 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height, 2300 field, width_mask, 2301 /* width_bias: round down */ 0, 2302 adjust_size, adjust_crop); 2303 if (0 != rc) 2304 return rc; 2305 2306 win->field = field; 2307 return 0; 2308 } 2309 2310 static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv, 2311 struct v4l2_window *win, int fixup) 2312 { 2313 struct v4l2_clip *clips = NULL; 2314 int n,size,retval = 0; 2315 2316 if (NULL == fh->ovfmt) 2317 return -EINVAL; 2318 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED)) 2319 return -EINVAL; 2320 retval = verify_window_lock(fh, win, 2321 /* adjust_size */ fixup, 2322 /* adjust_crop */ fixup); 2323 if (0 != retval) 2324 return retval; 2325 2326 /* copy clips -- luckily v4l1 + v4l2 are binary 2327 compatible here ...*/ 2328 n = win->clipcount; 2329 size = sizeof(*clips)*(n+4); 2330 clips = kmalloc(size,GFP_KERNEL); 2331 if (NULL == clips) 2332 return -ENOMEM; 2333 if (n > 0) { 2334 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) { 2335 kfree(clips); 2336 return -EFAULT; 2337 } 2338 } 2339 2340 /* clip against screen */ 2341 if (NULL != btv->fbuf.base) 2342 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height, 2343 &win->w, clips, n); 2344 btcx_sort_clips(clips,n); 2345 2346 /* 4-byte alignments */ 2347 switch (fh->ovfmt->depth) { 2348 case 8: 2349 case 24: 2350 btcx_align(&win->w, clips, n, 3); 2351 break; 2352 case 16: 2353 btcx_align(&win->w, clips, n, 1); 2354 break; 2355 case 32: 2356 /* no alignment fixups needed */ 2357 break; 2358 default: 2359 BUG(); 2360 } 2361 2362 kfree(fh->ov.clips); 2363 fh->ov.clips = clips; 2364 fh->ov.nclips = n; 2365 2366 fh->ov.w = win->w; 2367 fh->ov.field = win->field; 2368 fh->ov.setup_ok = 1; 2369 2370 btv->init.ov.w.width = win->w.width; 2371 btv->init.ov.w.height = win->w.height; 2372 btv->init.ov.field = win->field; 2373 2374 /* update overlay if needed */ 2375 retval = 0; 2376 if (check_btres(fh, RESOURCE_OVERLAY)) { 2377 struct bttv_buffer *new; 2378 2379 new = videobuf_sg_alloc(sizeof(*new)); 2380 new->crop = btv->crop[!!fh->do_crop].rect; 2381 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new); 2382 retval = bttv_switch_overlay(btv,fh,new); 2383 } 2384 return retval; 2385 } 2386 2387 /* ----------------------------------------------------------------------- */ 2388 2389 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh) 2390 { 2391 struct videobuf_queue* q = NULL; 2392 2393 switch (fh->type) { 2394 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 2395 q = &fh->cap; 2396 break; 2397 case V4L2_BUF_TYPE_VBI_CAPTURE: 2398 q = &fh->vbi; 2399 break; 2400 default: 2401 BUG(); 2402 } 2403 return q; 2404 } 2405 2406 static int bttv_resource(struct bttv_fh *fh) 2407 { 2408 int res = 0; 2409 2410 switch (fh->type) { 2411 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 2412 res = RESOURCE_VIDEO_STREAM; 2413 break; 2414 case V4L2_BUF_TYPE_VBI_CAPTURE: 2415 res = RESOURCE_VBI; 2416 break; 2417 default: 2418 BUG(); 2419 } 2420 return res; 2421 } 2422 2423 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type) 2424 { 2425 struct videobuf_queue *q = bttv_queue(fh); 2426 int res = bttv_resource(fh); 2427 2428 if (check_btres(fh,res)) 2429 return -EBUSY; 2430 if (videobuf_queue_is_busy(q)) 2431 return -EBUSY; 2432 fh->type = type; 2433 return 0; 2434 } 2435 2436 static void 2437 pix_format_set_size (struct v4l2_pix_format * f, 2438 const struct bttv_format * fmt, 2439 unsigned int width, 2440 unsigned int height) 2441 { 2442 f->width = width; 2443 f->height = height; 2444 2445 if (fmt->flags & FORMAT_FLAGS_PLANAR) { 2446 f->bytesperline = width; /* Y plane */ 2447 f->sizeimage = (width * height * fmt->depth) >> 3; 2448 } else { 2449 f->bytesperline = (width * fmt->depth) >> 3; 2450 f->sizeimage = height * f->bytesperline; 2451 } 2452 } 2453 2454 static int bttv_g_fmt_vid_cap(struct file *file, void *priv, 2455 struct v4l2_format *f) 2456 { 2457 struct bttv_fh *fh = priv; 2458 2459 pix_format_set_size(&f->fmt.pix, fh->fmt, 2460 fh->width, fh->height); 2461 f->fmt.pix.field = fh->cap.field; 2462 f->fmt.pix.pixelformat = fh->fmt->fourcc; 2463 2464 return 0; 2465 } 2466 2467 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv, 2468 struct v4l2_format *f) 2469 { 2470 struct bttv_fh *fh = priv; 2471 2472 f->fmt.win.w = fh->ov.w; 2473 f->fmt.win.field = fh->ov.field; 2474 2475 return 0; 2476 } 2477 2478 static int bttv_try_fmt_vid_cap(struct file *file, void *priv, 2479 struct v4l2_format *f) 2480 { 2481 const struct bttv_format *fmt; 2482 struct bttv_fh *fh = priv; 2483 struct bttv *btv = fh->btv; 2484 enum v4l2_field field; 2485 __s32 width, height; 2486 int rc; 2487 2488 fmt = format_by_fourcc(f->fmt.pix.pixelformat); 2489 if (NULL == fmt) 2490 return -EINVAL; 2491 2492 field = f->fmt.pix.field; 2493 2494 if (V4L2_FIELD_ANY == field) { 2495 __s32 height2; 2496 2497 height2 = btv->crop[!!fh->do_crop].rect.height >> 1; 2498 field = (f->fmt.pix.height > height2) 2499 ? V4L2_FIELD_INTERLACED 2500 : V4L2_FIELD_BOTTOM; 2501 } 2502 2503 if (V4L2_FIELD_SEQ_BT == field) 2504 field = V4L2_FIELD_SEQ_TB; 2505 2506 switch (field) { 2507 case V4L2_FIELD_TOP: 2508 case V4L2_FIELD_BOTTOM: 2509 case V4L2_FIELD_ALTERNATE: 2510 case V4L2_FIELD_INTERLACED: 2511 break; 2512 case V4L2_FIELD_SEQ_TB: 2513 if (fmt->flags & FORMAT_FLAGS_PLANAR) 2514 return -EINVAL; 2515 break; 2516 default: 2517 return -EINVAL; 2518 } 2519 2520 width = f->fmt.pix.width; 2521 height = f->fmt.pix.height; 2522 2523 rc = limit_scaled_size_lock(fh, &width, &height, field, 2524 /* width_mask: 4 pixels */ ~3, 2525 /* width_bias: nearest */ 2, 2526 /* adjust_size */ 1, 2527 /* adjust_crop */ 0); 2528 if (0 != rc) 2529 return rc; 2530 2531 /* update data for the application */ 2532 f->fmt.pix.field = field; 2533 pix_format_set_size(&f->fmt.pix, fmt, width, height); 2534 2535 return 0; 2536 } 2537 2538 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv, 2539 struct v4l2_format *f) 2540 { 2541 struct bttv_fh *fh = priv; 2542 2543 return verify_window_lock(fh, &f->fmt.win, 2544 /* adjust_size */ 1, 2545 /* adjust_crop */ 0); 2546 } 2547 2548 static int bttv_s_fmt_vid_cap(struct file *file, void *priv, 2549 struct v4l2_format *f) 2550 { 2551 int retval; 2552 const struct bttv_format *fmt; 2553 struct bttv_fh *fh = priv; 2554 struct bttv *btv = fh->btv; 2555 __s32 width, height; 2556 enum v4l2_field field; 2557 2558 retval = bttv_switch_type(fh, f->type); 2559 if (0 != retval) 2560 return retval; 2561 2562 retval = bttv_try_fmt_vid_cap(file, priv, f); 2563 if (0 != retval) 2564 return retval; 2565 2566 width = f->fmt.pix.width; 2567 height = f->fmt.pix.height; 2568 field = f->fmt.pix.field; 2569 2570 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field, 2571 /* width_mask: 4 pixels */ ~3, 2572 /* width_bias: nearest */ 2, 2573 /* adjust_size */ 1, 2574 /* adjust_crop */ 1); 2575 if (0 != retval) 2576 return retval; 2577 2578 f->fmt.pix.field = field; 2579 2580 fmt = format_by_fourcc(f->fmt.pix.pixelformat); 2581 2582 /* update our state informations */ 2583 fh->fmt = fmt; 2584 fh->cap.field = f->fmt.pix.field; 2585 fh->cap.last = V4L2_FIELD_NONE; 2586 fh->width = f->fmt.pix.width; 2587 fh->height = f->fmt.pix.height; 2588 btv->init.fmt = fmt; 2589 btv->init.width = f->fmt.pix.width; 2590 btv->init.height = f->fmt.pix.height; 2591 2592 return 0; 2593 } 2594 2595 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv, 2596 struct v4l2_format *f) 2597 { 2598 struct bttv_fh *fh = priv; 2599 struct bttv *btv = fh->btv; 2600 2601 if (no_overlay > 0) { 2602 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n"); 2603 return -EINVAL; 2604 } 2605 2606 return setup_window_lock(fh, btv, &f->fmt.win, 1); 2607 } 2608 2609 static int bttv_querycap(struct file *file, void *priv, 2610 struct v4l2_capability *cap) 2611 { 2612 struct bttv_fh *fh = priv; 2613 struct bttv *btv = fh->btv; 2614 2615 if (0 == v4l2) 2616 return -EINVAL; 2617 2618 strlcpy(cap->driver, "bttv", sizeof(cap->driver)); 2619 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card)); 2620 snprintf(cap->bus_info, sizeof(cap->bus_info), 2621 "PCI:%s", pci_name(btv->c.pci)); 2622 cap->capabilities = 2623 V4L2_CAP_VIDEO_CAPTURE | 2624 V4L2_CAP_VBI_CAPTURE | 2625 V4L2_CAP_READWRITE | 2626 V4L2_CAP_STREAMING; 2627 if (no_overlay <= 0) 2628 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY; 2629 2630 /* 2631 * No need to lock here: those vars are initialized during board 2632 * probe and remains untouched during the rest of the driver lifecycle 2633 */ 2634 if (btv->has_saa6588) 2635 cap->capabilities |= V4L2_CAP_RDS_CAPTURE; 2636 if (btv->tuner_type != TUNER_ABSENT) 2637 cap->capabilities |= V4L2_CAP_TUNER; 2638 return 0; 2639 } 2640 2641 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f) 2642 { 2643 int index = -1, i; 2644 2645 for (i = 0; i < FORMATS; i++) { 2646 if (formats[i].fourcc != -1) 2647 index++; 2648 if ((unsigned int)index == f->index) 2649 break; 2650 } 2651 if (FORMATS == i) 2652 return -EINVAL; 2653 2654 f->pixelformat = formats[i].fourcc; 2655 strlcpy(f->description, formats[i].name, sizeof(f->description)); 2656 2657 return i; 2658 } 2659 2660 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv, 2661 struct v4l2_fmtdesc *f) 2662 { 2663 int rc = bttv_enum_fmt_cap_ovr(f); 2664 2665 if (rc < 0) 2666 return rc; 2667 2668 return 0; 2669 } 2670 2671 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv, 2672 struct v4l2_fmtdesc *f) 2673 { 2674 int rc; 2675 2676 if (no_overlay > 0) { 2677 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n"); 2678 return -EINVAL; 2679 } 2680 2681 rc = bttv_enum_fmt_cap_ovr(f); 2682 2683 if (rc < 0) 2684 return rc; 2685 2686 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED)) 2687 return -EINVAL; 2688 2689 return 0; 2690 } 2691 2692 static int bttv_g_fbuf(struct file *file, void *f, 2693 struct v4l2_framebuffer *fb) 2694 { 2695 struct bttv_fh *fh = f; 2696 struct bttv *btv = fh->btv; 2697 2698 *fb = btv->fbuf; 2699 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING; 2700 if (fh->ovfmt) 2701 fb->fmt.pixelformat = fh->ovfmt->fourcc; 2702 return 0; 2703 } 2704 2705 static int bttv_overlay(struct file *file, void *f, unsigned int on) 2706 { 2707 struct bttv_fh *fh = f; 2708 struct bttv *btv = fh->btv; 2709 struct bttv_buffer *new; 2710 int retval = 0; 2711 2712 if (on) { 2713 /* verify args */ 2714 if (unlikely(!btv->fbuf.base)) { 2715 return -EINVAL; 2716 } 2717 if (unlikely(!fh->ov.setup_ok)) { 2718 dprintk("%d: overlay: !setup_ok\n", btv->c.nr); 2719 retval = -EINVAL; 2720 } 2721 if (retval) 2722 return retval; 2723 } 2724 2725 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY)) 2726 return -EBUSY; 2727 2728 if (on) { 2729 fh->ov.tvnorm = btv->tvnorm; 2730 new = videobuf_sg_alloc(sizeof(*new)); 2731 new->crop = btv->crop[!!fh->do_crop].rect; 2732 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new); 2733 } else { 2734 new = NULL; 2735 } 2736 2737 /* switch over */ 2738 retval = bttv_switch_overlay(btv, fh, new); 2739 return retval; 2740 } 2741 2742 static int bttv_s_fbuf(struct file *file, void *f, 2743 const struct v4l2_framebuffer *fb) 2744 { 2745 struct bttv_fh *fh = f; 2746 struct bttv *btv = fh->btv; 2747 const struct bttv_format *fmt; 2748 int retval; 2749 2750 if (!capable(CAP_SYS_ADMIN) && 2751 !capable(CAP_SYS_RAWIO)) 2752 return -EPERM; 2753 2754 /* check args */ 2755 fmt = format_by_fourcc(fb->fmt.pixelformat); 2756 if (NULL == fmt) 2757 return -EINVAL; 2758 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED)) 2759 return -EINVAL; 2760 2761 retval = -EINVAL; 2762 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) { 2763 __s32 width = fb->fmt.width; 2764 __s32 height = fb->fmt.height; 2765 2766 retval = limit_scaled_size_lock(fh, &width, &height, 2767 V4L2_FIELD_INTERLACED, 2768 /* width_mask */ ~3, 2769 /* width_bias */ 2, 2770 /* adjust_size */ 0, 2771 /* adjust_crop */ 0); 2772 if (0 != retval) 2773 return retval; 2774 } 2775 2776 /* ok, accept it */ 2777 btv->fbuf.base = fb->base; 2778 btv->fbuf.fmt.width = fb->fmt.width; 2779 btv->fbuf.fmt.height = fb->fmt.height; 2780 if (0 != fb->fmt.bytesperline) 2781 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline; 2782 else 2783 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8; 2784 2785 retval = 0; 2786 fh->ovfmt = fmt; 2787 btv->init.ovfmt = fmt; 2788 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) { 2789 fh->ov.w.left = 0; 2790 fh->ov.w.top = 0; 2791 fh->ov.w.width = fb->fmt.width; 2792 fh->ov.w.height = fb->fmt.height; 2793 btv->init.ov.w.width = fb->fmt.width; 2794 btv->init.ov.w.height = fb->fmt.height; 2795 kfree(fh->ov.clips); 2796 fh->ov.clips = NULL; 2797 fh->ov.nclips = 0; 2798 2799 if (check_btres(fh, RESOURCE_OVERLAY)) { 2800 struct bttv_buffer *new; 2801 2802 new = videobuf_sg_alloc(sizeof(*new)); 2803 new->crop = btv->crop[!!fh->do_crop].rect; 2804 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new); 2805 retval = bttv_switch_overlay(btv, fh, new); 2806 } 2807 } 2808 return retval; 2809 } 2810 2811 static int bttv_reqbufs(struct file *file, void *priv, 2812 struct v4l2_requestbuffers *p) 2813 { 2814 struct bttv_fh *fh = priv; 2815 return videobuf_reqbufs(bttv_queue(fh), p); 2816 } 2817 2818 static int bttv_querybuf(struct file *file, void *priv, 2819 struct v4l2_buffer *b) 2820 { 2821 struct bttv_fh *fh = priv; 2822 return videobuf_querybuf(bttv_queue(fh), b); 2823 } 2824 2825 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) 2826 { 2827 struct bttv_fh *fh = priv; 2828 struct bttv *btv = fh->btv; 2829 int res = bttv_resource(fh); 2830 2831 if (!check_alloc_btres_lock(btv, fh, res)) 2832 return -EBUSY; 2833 2834 return videobuf_qbuf(bttv_queue(fh), b); 2835 } 2836 2837 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) 2838 { 2839 struct bttv_fh *fh = priv; 2840 return videobuf_dqbuf(bttv_queue(fh), b, 2841 file->f_flags & O_NONBLOCK); 2842 } 2843 2844 static int bttv_streamon(struct file *file, void *priv, 2845 enum v4l2_buf_type type) 2846 { 2847 struct bttv_fh *fh = priv; 2848 struct bttv *btv = fh->btv; 2849 int res = bttv_resource(fh); 2850 2851 if (!check_alloc_btres_lock(btv, fh, res)) 2852 return -EBUSY; 2853 return videobuf_streamon(bttv_queue(fh)); 2854 } 2855 2856 2857 static int bttv_streamoff(struct file *file, void *priv, 2858 enum v4l2_buf_type type) 2859 { 2860 struct bttv_fh *fh = priv; 2861 struct bttv *btv = fh->btv; 2862 int retval; 2863 int res = bttv_resource(fh); 2864 2865 2866 retval = videobuf_streamoff(bttv_queue(fh)); 2867 if (retval < 0) 2868 return retval; 2869 free_btres_lock(btv, fh, res); 2870 return 0; 2871 } 2872 2873 static int bttv_queryctrl(struct file *file, void *priv, 2874 struct v4l2_queryctrl *c) 2875 { 2876 struct bttv_fh *fh = priv; 2877 struct bttv *btv = fh->btv; 2878 const struct v4l2_queryctrl *ctrl; 2879 2880 if ((c->id < V4L2_CID_BASE || 2881 c->id >= V4L2_CID_LASTP1) && 2882 (c->id < V4L2_CID_PRIVATE_BASE || 2883 c->id >= V4L2_CID_PRIVATE_LASTP1)) 2884 return -EINVAL; 2885 2886 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME)) 2887 *c = no_ctl; 2888 else { 2889 ctrl = ctrl_by_id(c->id); 2890 2891 *c = (NULL != ctrl) ? *ctrl : no_ctl; 2892 } 2893 2894 return 0; 2895 } 2896 2897 static int bttv_g_parm(struct file *file, void *f, 2898 struct v4l2_streamparm *parm) 2899 { 2900 struct bttv_fh *fh = f; 2901 struct bttv *btv = fh->btv; 2902 2903 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id, 2904 &parm->parm.capture.timeperframe); 2905 2906 return 0; 2907 } 2908 2909 static int bttv_g_tuner(struct file *file, void *priv, 2910 struct v4l2_tuner *t) 2911 { 2912 struct bttv_fh *fh = priv; 2913 struct bttv *btv = fh->btv; 2914 2915 if (btv->tuner_type == TUNER_ABSENT) 2916 return -EINVAL; 2917 if (0 != t->index) 2918 return -EINVAL; 2919 2920 t->rxsubchans = V4L2_TUNER_SUB_MONO; 2921 bttv_call_all(btv, tuner, g_tuner, t); 2922 strcpy(t->name, "Television"); 2923 t->capability = V4L2_TUNER_CAP_NORM; 2924 t->type = V4L2_TUNER_ANALOG_TV; 2925 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) 2926 t->signal = 0xffff; 2927 2928 if (btv->audio_mode_gpio) 2929 btv->audio_mode_gpio(btv, t, 0); 2930 2931 return 0; 2932 } 2933 2934 static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p) 2935 { 2936 struct bttv_fh *fh = f; 2937 struct bttv *btv = fh->btv; 2938 2939 *p = v4l2_prio_max(&btv->prio); 2940 2941 return 0; 2942 } 2943 2944 static int bttv_s_priority(struct file *file, void *f, 2945 enum v4l2_priority prio) 2946 { 2947 struct bttv_fh *fh = f; 2948 struct bttv *btv = fh->btv; 2949 int rc; 2950 2951 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio); 2952 2953 return rc; 2954 } 2955 2956 static int bttv_cropcap(struct file *file, void *priv, 2957 struct v4l2_cropcap *cap) 2958 { 2959 struct bttv_fh *fh = priv; 2960 struct bttv *btv = fh->btv; 2961 2962 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 2963 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 2964 return -EINVAL; 2965 2966 *cap = bttv_tvnorms[btv->tvnorm].cropcap; 2967 2968 return 0; 2969 } 2970 2971 static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop) 2972 { 2973 struct bttv_fh *fh = f; 2974 struct bttv *btv = fh->btv; 2975 2976 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 2977 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 2978 return -EINVAL; 2979 2980 /* No fh->do_crop = 1; because btv->crop[1] may be 2981 inconsistent with fh->width or fh->height and apps 2982 do not expect a change here. */ 2983 2984 crop->c = btv->crop[!!fh->do_crop].rect; 2985 2986 return 0; 2987 } 2988 2989 static int bttv_s_crop(struct file *file, void *f, const struct v4l2_crop *crop) 2990 { 2991 struct bttv_fh *fh = f; 2992 struct bttv *btv = fh->btv; 2993 const struct v4l2_rect *b; 2994 int retval; 2995 struct bttv_crop c; 2996 __s32 b_left; 2997 __s32 b_top; 2998 __s32 b_right; 2999 __s32 b_bottom; 3000 3001 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 3002 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 3003 return -EINVAL; 3004 3005 /* Make sure tvnorm, vbi_end and the current cropping 3006 parameters remain consistent until we're done. Note 3007 read() may change vbi_end in check_alloc_btres_lock(). */ 3008 retval = v4l2_prio_check(&btv->prio, fh->prio); 3009 if (0 != retval) { 3010 return retval; 3011 } 3012 3013 retval = -EBUSY; 3014 3015 if (locked_btres(fh->btv, VIDEO_RESOURCES)) { 3016 return retval; 3017 } 3018 3019 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds; 3020 3021 b_left = b->left; 3022 b_right = b_left + b->width; 3023 b_bottom = b->top + b->height; 3024 3025 b_top = max(b->top, btv->vbi_end); 3026 if (b_top + 32 >= b_bottom) { 3027 return retval; 3028 } 3029 3030 /* Min. scaled size 48 x 32. */ 3031 c.rect.left = clamp_t(s32, crop->c.left, b_left, b_right - 48); 3032 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY); 3033 3034 c.rect.width = clamp_t(s32, crop->c.width, 3035 48, b_right - c.rect.left); 3036 3037 c.rect.top = clamp_t(s32, crop->c.top, b_top, b_bottom - 32); 3038 /* Top and height must be a multiple of two. */ 3039 c.rect.top = (c.rect.top + 1) & ~1; 3040 3041 c.rect.height = clamp_t(s32, crop->c.height, 3042 32, b_bottom - c.rect.top); 3043 c.rect.height = (c.rect.height + 1) & ~1; 3044 3045 bttv_crop_calc_limits(&c); 3046 3047 btv->crop[1] = c; 3048 3049 fh->do_crop = 1; 3050 3051 if (fh->width < c.min_scaled_width) { 3052 fh->width = c.min_scaled_width; 3053 btv->init.width = c.min_scaled_width; 3054 } else if (fh->width > c.max_scaled_width) { 3055 fh->width = c.max_scaled_width; 3056 btv->init.width = c.max_scaled_width; 3057 } 3058 3059 if (fh->height < c.min_scaled_height) { 3060 fh->height = c.min_scaled_height; 3061 btv->init.height = c.min_scaled_height; 3062 } else if (fh->height > c.max_scaled_height) { 3063 fh->height = c.max_scaled_height; 3064 btv->init.height = c.max_scaled_height; 3065 } 3066 3067 return 0; 3068 } 3069 3070 static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a) 3071 { 3072 if (unlikely(a->index)) 3073 return -EINVAL; 3074 3075 strcpy(a->name, "audio"); 3076 return 0; 3077 } 3078 3079 static int bttv_s_audio(struct file *file, void *priv, const struct v4l2_audio *a) 3080 { 3081 if (unlikely(a->index)) 3082 return -EINVAL; 3083 3084 return 0; 3085 } 3086 3087 static ssize_t bttv_read(struct file *file, char __user *data, 3088 size_t count, loff_t *ppos) 3089 { 3090 struct bttv_fh *fh = file->private_data; 3091 int retval = 0; 3092 3093 if (fh->btv->errors) 3094 bttv_reinit_bt848(fh->btv); 3095 dprintk("%d: read count=%d type=%s\n", 3096 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]); 3097 3098 switch (fh->type) { 3099 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 3100 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) { 3101 /* VIDEO_READ in use by another fh, 3102 or VIDEO_STREAM by any fh. */ 3103 return -EBUSY; 3104 } 3105 retval = videobuf_read_one(&fh->cap, data, count, ppos, 3106 file->f_flags & O_NONBLOCK); 3107 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ); 3108 break; 3109 case V4L2_BUF_TYPE_VBI_CAPTURE: 3110 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI)) 3111 return -EBUSY; 3112 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1, 3113 file->f_flags & O_NONBLOCK); 3114 break; 3115 default: 3116 BUG(); 3117 } 3118 return retval; 3119 } 3120 3121 static unsigned int bttv_poll(struct file *file, poll_table *wait) 3122 { 3123 struct bttv_fh *fh = file->private_data; 3124 struct bttv_buffer *buf; 3125 enum v4l2_field field; 3126 unsigned int rc = POLLERR; 3127 3128 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) { 3129 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI)) 3130 return POLLERR; 3131 return videobuf_poll_stream(file, &fh->vbi, wait); 3132 } 3133 3134 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) { 3135 /* streaming capture */ 3136 if (list_empty(&fh->cap.stream)) 3137 goto err; 3138 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream); 3139 } else { 3140 /* read() capture */ 3141 if (NULL == fh->cap.read_buf) { 3142 /* need to capture a new frame */ 3143 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM)) 3144 goto err; 3145 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize); 3146 if (NULL == fh->cap.read_buf) 3147 goto err; 3148 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR; 3149 field = videobuf_next_field(&fh->cap); 3150 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) { 3151 kfree (fh->cap.read_buf); 3152 fh->cap.read_buf = NULL; 3153 goto err; 3154 } 3155 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf); 3156 fh->cap.read_off = 0; 3157 } 3158 buf = (struct bttv_buffer*)fh->cap.read_buf; 3159 } 3160 3161 poll_wait(file, &buf->vb.done, wait); 3162 if (buf->vb.state == VIDEOBUF_DONE || 3163 buf->vb.state == VIDEOBUF_ERROR) 3164 rc = POLLIN|POLLRDNORM; 3165 else 3166 rc = 0; 3167 err: 3168 return rc; 3169 } 3170 3171 static int bttv_open(struct file *file) 3172 { 3173 struct video_device *vdev = video_devdata(file); 3174 struct bttv *btv = video_drvdata(file); 3175 struct bttv_fh *fh; 3176 enum v4l2_buf_type type = 0; 3177 3178 dprintk("open dev=%s\n", video_device_node_name(vdev)); 3179 3180 if (vdev->vfl_type == VFL_TYPE_GRABBER) { 3181 type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 3182 } else if (vdev->vfl_type == VFL_TYPE_VBI) { 3183 type = V4L2_BUF_TYPE_VBI_CAPTURE; 3184 } else { 3185 WARN_ON(1); 3186 return -ENODEV; 3187 } 3188 3189 dprintk("%d: open called (type=%s)\n", 3190 btv->c.nr, v4l2_type_names[type]); 3191 3192 /* allocate per filehandle data */ 3193 fh = kmalloc(sizeof(*fh), GFP_KERNEL); 3194 if (unlikely(!fh)) 3195 return -ENOMEM; 3196 file->private_data = fh; 3197 3198 *fh = btv->init; 3199 3200 fh->type = type; 3201 fh->ov.setup_ok = 0; 3202 3203 v4l2_prio_open(&btv->prio, &fh->prio); 3204 3205 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops, 3206 &btv->c.pci->dev, &btv->s_lock, 3207 V4L2_BUF_TYPE_VIDEO_CAPTURE, 3208 V4L2_FIELD_INTERLACED, 3209 sizeof(struct bttv_buffer), 3210 fh, &btv->lock); 3211 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops, 3212 &btv->c.pci->dev, &btv->s_lock, 3213 V4L2_BUF_TYPE_VBI_CAPTURE, 3214 V4L2_FIELD_SEQ_TB, 3215 sizeof(struct bttv_buffer), 3216 fh, &btv->lock); 3217 set_tvnorm(btv,btv->tvnorm); 3218 set_input(btv, btv->input, btv->tvnorm); 3219 3220 btv->users++; 3221 3222 /* The V4L2 spec requires one global set of cropping parameters 3223 which only change on request. These are stored in btv->crop[1]. 3224 However for compatibility with V4L apps and cropping unaware 3225 V4L2 apps we now reset the cropping parameters as seen through 3226 this fh, which is to say VIDIOC_G_CROP and scaling limit checks 3227 will use btv->crop[0], the default cropping parameters for the 3228 current video standard, and VIDIOC_S_FMT will not implicitely 3229 change the cropping parameters until VIDIOC_S_CROP has been 3230 called. */ 3231 fh->do_crop = !reset_crop; /* module parameter */ 3232 3233 /* Likewise there should be one global set of VBI capture 3234 parameters, but for compatibility with V4L apps and earlier 3235 driver versions each fh has its own parameters. */ 3236 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm); 3237 3238 bttv_field_count(btv); 3239 return 0; 3240 } 3241 3242 static int bttv_release(struct file *file) 3243 { 3244 struct bttv_fh *fh = file->private_data; 3245 struct bttv *btv = fh->btv; 3246 3247 /* turn off overlay */ 3248 if (check_btres(fh, RESOURCE_OVERLAY)) 3249 bttv_switch_overlay(btv,fh,NULL); 3250 3251 /* stop video capture */ 3252 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) { 3253 videobuf_streamoff(&fh->cap); 3254 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM); 3255 } 3256 if (fh->cap.read_buf) { 3257 buffer_release(&fh->cap,fh->cap.read_buf); 3258 kfree(fh->cap.read_buf); 3259 } 3260 if (check_btres(fh, RESOURCE_VIDEO_READ)) { 3261 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ); 3262 } 3263 3264 /* stop vbi capture */ 3265 if (check_btres(fh, RESOURCE_VBI)) { 3266 videobuf_stop(&fh->vbi); 3267 free_btres_lock(btv,fh,RESOURCE_VBI); 3268 } 3269 3270 /* free stuff */ 3271 3272 videobuf_mmap_free(&fh->cap); 3273 videobuf_mmap_free(&fh->vbi); 3274 v4l2_prio_close(&btv->prio, fh->prio); 3275 file->private_data = NULL; 3276 kfree(fh); 3277 3278 btv->users--; 3279 bttv_field_count(btv); 3280 3281 if (!btv->users) 3282 audio_mute(btv, 1); 3283 3284 return 0; 3285 } 3286 3287 static int 3288 bttv_mmap(struct file *file, struct vm_area_struct *vma) 3289 { 3290 struct bttv_fh *fh = file->private_data; 3291 3292 dprintk("%d: mmap type=%s 0x%lx+%ld\n", 3293 fh->btv->c.nr, v4l2_type_names[fh->type], 3294 vma->vm_start, vma->vm_end - vma->vm_start); 3295 return videobuf_mmap_mapper(bttv_queue(fh),vma); 3296 } 3297 3298 static const struct v4l2_file_operations bttv_fops = 3299 { 3300 .owner = THIS_MODULE, 3301 .open = bttv_open, 3302 .release = bttv_release, 3303 .unlocked_ioctl = video_ioctl2, 3304 .read = bttv_read, 3305 .mmap = bttv_mmap, 3306 .poll = bttv_poll, 3307 }; 3308 3309 static const struct v4l2_ioctl_ops bttv_ioctl_ops = { 3310 .vidioc_querycap = bttv_querycap, 3311 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap, 3312 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap, 3313 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap, 3314 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap, 3315 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay, 3316 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay, 3317 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay, 3318 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay, 3319 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap, 3320 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap, 3321 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap, 3322 .vidioc_g_audio = bttv_g_audio, 3323 .vidioc_s_audio = bttv_s_audio, 3324 .vidioc_cropcap = bttv_cropcap, 3325 .vidioc_reqbufs = bttv_reqbufs, 3326 .vidioc_querybuf = bttv_querybuf, 3327 .vidioc_qbuf = bttv_qbuf, 3328 .vidioc_dqbuf = bttv_dqbuf, 3329 .vidioc_s_std = bttv_s_std, 3330 .vidioc_enum_input = bttv_enum_input, 3331 .vidioc_g_input = bttv_g_input, 3332 .vidioc_s_input = bttv_s_input, 3333 .vidioc_queryctrl = bttv_queryctrl, 3334 .vidioc_g_ctrl = bttv_g_ctrl, 3335 .vidioc_s_ctrl = bttv_s_ctrl, 3336 .vidioc_streamon = bttv_streamon, 3337 .vidioc_streamoff = bttv_streamoff, 3338 .vidioc_g_tuner = bttv_g_tuner, 3339 .vidioc_s_tuner = bttv_s_tuner, 3340 .vidioc_g_crop = bttv_g_crop, 3341 .vidioc_s_crop = bttv_s_crop, 3342 .vidioc_g_fbuf = bttv_g_fbuf, 3343 .vidioc_s_fbuf = bttv_s_fbuf, 3344 .vidioc_overlay = bttv_overlay, 3345 .vidioc_g_priority = bttv_g_priority, 3346 .vidioc_s_priority = bttv_s_priority, 3347 .vidioc_g_parm = bttv_g_parm, 3348 .vidioc_g_frequency = bttv_g_frequency, 3349 .vidioc_s_frequency = bttv_s_frequency, 3350 .vidioc_log_status = bttv_log_status, 3351 .vidioc_querystd = bttv_querystd, 3352 #ifdef CONFIG_VIDEO_ADV_DEBUG 3353 .vidioc_g_register = bttv_g_register, 3354 .vidioc_s_register = bttv_s_register, 3355 #endif 3356 }; 3357 3358 static struct video_device bttv_video_template = { 3359 .fops = &bttv_fops, 3360 .ioctl_ops = &bttv_ioctl_ops, 3361 .tvnorms = BTTV_NORMS, 3362 .current_norm = V4L2_STD_PAL, 3363 }; 3364 3365 /* ----------------------------------------------------------------------- */ 3366 /* radio interface */ 3367 3368 static int radio_open(struct file *file) 3369 { 3370 struct video_device *vdev = video_devdata(file); 3371 struct bttv *btv = video_drvdata(file); 3372 struct bttv_fh *fh; 3373 3374 dprintk("open dev=%s\n", video_device_node_name(vdev)); 3375 3376 dprintk("%d: open called (radio)\n", btv->c.nr); 3377 3378 /* allocate per filehandle data */ 3379 fh = kmalloc(sizeof(*fh), GFP_KERNEL); 3380 if (unlikely(!fh)) 3381 return -ENOMEM; 3382 file->private_data = fh; 3383 *fh = btv->init; 3384 3385 v4l2_prio_open(&btv->prio, &fh->prio); 3386 3387 btv->radio_user++; 3388 3389 bttv_call_all(btv, tuner, s_radio); 3390 audio_input(btv,TVAUDIO_INPUT_RADIO); 3391 3392 return 0; 3393 } 3394 3395 static int radio_release(struct file *file) 3396 { 3397 struct bttv_fh *fh = file->private_data; 3398 struct bttv *btv = fh->btv; 3399 struct saa6588_command cmd; 3400 3401 v4l2_prio_close(&btv->prio, fh->prio); 3402 file->private_data = NULL; 3403 kfree(fh); 3404 3405 btv->radio_user--; 3406 3407 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd); 3408 3409 return 0; 3410 } 3411 3412 static int radio_querycap(struct file *file, void *priv, 3413 struct v4l2_capability *cap) 3414 { 3415 struct bttv_fh *fh = priv; 3416 struct bttv *btv = fh->btv; 3417 3418 strcpy(cap->driver, "bttv"); 3419 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card)); 3420 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci)); 3421 cap->capabilities = V4L2_CAP_TUNER; 3422 3423 return 0; 3424 } 3425 3426 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t) 3427 { 3428 struct bttv_fh *fh = priv; 3429 struct bttv *btv = fh->btv; 3430 3431 if (btv->tuner_type == TUNER_ABSENT) 3432 return -EINVAL; 3433 if (0 != t->index) 3434 return -EINVAL; 3435 strcpy(t->name, "Radio"); 3436 t->type = V4L2_TUNER_RADIO; 3437 3438 bttv_call_all(btv, tuner, g_tuner, t); 3439 3440 if (btv->audio_mode_gpio) 3441 btv->audio_mode_gpio(btv, t, 0); 3442 3443 return 0; 3444 } 3445 3446 static int radio_enum_input(struct file *file, void *priv, 3447 struct v4l2_input *i) 3448 { 3449 if (i->index != 0) 3450 return -EINVAL; 3451 3452 strcpy(i->name, "Radio"); 3453 i->type = V4L2_INPUT_TYPE_TUNER; 3454 3455 return 0; 3456 } 3457 3458 static int radio_g_audio(struct file *file, void *priv, 3459 struct v4l2_audio *a) 3460 { 3461 if (unlikely(a->index)) 3462 return -EINVAL; 3463 3464 strcpy(a->name, "Radio"); 3465 3466 return 0; 3467 } 3468 3469 static int radio_s_tuner(struct file *file, void *priv, 3470 struct v4l2_tuner *t) 3471 { 3472 struct bttv_fh *fh = priv; 3473 struct bttv *btv = fh->btv; 3474 3475 if (0 != t->index) 3476 return -EINVAL; 3477 3478 bttv_call_all(btv, tuner, s_tuner, t); 3479 return 0; 3480 } 3481 3482 static int radio_s_audio(struct file *file, void *priv, 3483 const struct v4l2_audio *a) 3484 { 3485 if (unlikely(a->index)) 3486 return -EINVAL; 3487 3488 return 0; 3489 } 3490 3491 static int radio_s_input(struct file *filp, void *priv, unsigned int i) 3492 { 3493 if (unlikely(i)) 3494 return -EINVAL; 3495 3496 return 0; 3497 } 3498 3499 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm) 3500 { 3501 return 0; 3502 } 3503 3504 static int radio_queryctrl(struct file *file, void *priv, 3505 struct v4l2_queryctrl *c) 3506 { 3507 const struct v4l2_queryctrl *ctrl; 3508 3509 if (c->id < V4L2_CID_BASE || 3510 c->id >= V4L2_CID_LASTP1) 3511 return -EINVAL; 3512 3513 if (c->id == V4L2_CID_AUDIO_MUTE) { 3514 ctrl = ctrl_by_id(c->id); 3515 *c = *ctrl; 3516 } else 3517 *c = no_ctl; 3518 3519 return 0; 3520 } 3521 3522 static int radio_g_input(struct file *filp, void *priv, unsigned int *i) 3523 { 3524 *i = 0; 3525 return 0; 3526 } 3527 3528 static ssize_t radio_read(struct file *file, char __user *data, 3529 size_t count, loff_t *ppos) 3530 { 3531 struct bttv_fh *fh = file->private_data; 3532 struct bttv *btv = fh->btv; 3533 struct saa6588_command cmd; 3534 cmd.block_count = count/3; 3535 cmd.buffer = data; 3536 cmd.instance = file; 3537 cmd.result = -ENODEV; 3538 3539 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd); 3540 3541 return cmd.result; 3542 } 3543 3544 static unsigned int radio_poll(struct file *file, poll_table *wait) 3545 { 3546 struct bttv_fh *fh = file->private_data; 3547 struct bttv *btv = fh->btv; 3548 struct saa6588_command cmd; 3549 cmd.instance = file; 3550 cmd.event_list = wait; 3551 cmd.result = -ENODEV; 3552 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd); 3553 3554 return cmd.result; 3555 } 3556 3557 static const struct v4l2_file_operations radio_fops = 3558 { 3559 .owner = THIS_MODULE, 3560 .open = radio_open, 3561 .read = radio_read, 3562 .release = radio_release, 3563 .unlocked_ioctl = video_ioctl2, 3564 .poll = radio_poll, 3565 }; 3566 3567 static const struct v4l2_ioctl_ops radio_ioctl_ops = { 3568 .vidioc_querycap = radio_querycap, 3569 .vidioc_g_tuner = radio_g_tuner, 3570 .vidioc_enum_input = radio_enum_input, 3571 .vidioc_g_audio = radio_g_audio, 3572 .vidioc_s_tuner = radio_s_tuner, 3573 .vidioc_s_audio = radio_s_audio, 3574 .vidioc_s_input = radio_s_input, 3575 .vidioc_s_std = radio_s_std, 3576 .vidioc_queryctrl = radio_queryctrl, 3577 .vidioc_g_input = radio_g_input, 3578 .vidioc_g_ctrl = bttv_g_ctrl, 3579 .vidioc_s_ctrl = bttv_s_ctrl, 3580 .vidioc_g_frequency = bttv_g_frequency, 3581 .vidioc_s_frequency = bttv_s_frequency, 3582 }; 3583 3584 static struct video_device radio_template = { 3585 .fops = &radio_fops, 3586 .ioctl_ops = &radio_ioctl_ops, 3587 }; 3588 3589 /* ----------------------------------------------------------------------- */ 3590 /* some debug code */ 3591 3592 static int bttv_risc_decode(u32 risc) 3593 { 3594 static char *instr[16] = { 3595 [ BT848_RISC_WRITE >> 28 ] = "write", 3596 [ BT848_RISC_SKIP >> 28 ] = "skip", 3597 [ BT848_RISC_WRITEC >> 28 ] = "writec", 3598 [ BT848_RISC_JUMP >> 28 ] = "jump", 3599 [ BT848_RISC_SYNC >> 28 ] = "sync", 3600 [ BT848_RISC_WRITE123 >> 28 ] = "write123", 3601 [ BT848_RISC_SKIP123 >> 28 ] = "skip123", 3602 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23", 3603 }; 3604 static int incr[16] = { 3605 [ BT848_RISC_WRITE >> 28 ] = 2, 3606 [ BT848_RISC_JUMP >> 28 ] = 2, 3607 [ BT848_RISC_SYNC >> 28 ] = 2, 3608 [ BT848_RISC_WRITE123 >> 28 ] = 5, 3609 [ BT848_RISC_SKIP123 >> 28 ] = 2, 3610 [ BT848_RISC_WRITE1S23 >> 28 ] = 3, 3611 }; 3612 static char *bits[] = { 3613 "be0", "be1", "be2", "be3/resync", 3614 "set0", "set1", "set2", "set3", 3615 "clr0", "clr1", "clr2", "clr3", 3616 "irq", "res", "eol", "sol", 3617 }; 3618 int i; 3619 3620 pr_cont("0x%08x [ %s", risc, 3621 instr[risc >> 28] ? instr[risc >> 28] : "INVALID"); 3622 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--) 3623 if (risc & (1 << (i + 12))) 3624 pr_cont(" %s", bits[i]); 3625 pr_cont(" count=%d ]\n", risc & 0xfff); 3626 return incr[risc >> 28] ? incr[risc >> 28] : 1; 3627 } 3628 3629 static void bttv_risc_disasm(struct bttv *btv, 3630 struct btcx_riscmem *risc) 3631 { 3632 unsigned int i,j,n; 3633 3634 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n", 3635 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma); 3636 for (i = 0; i < (risc->size >> 2); i += n) { 3637 pr_info("%s: 0x%lx: ", 3638 btv->c.v4l2_dev.name, 3639 (unsigned long)(risc->dma + (i<<2))); 3640 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i])); 3641 for (j = 1; j < n; j++) 3642 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n", 3643 btv->c.v4l2_dev.name, 3644 (unsigned long)(risc->dma + ((i+j)<<2)), 3645 risc->cpu[i+j], j); 3646 if (0 == risc->cpu[i]) 3647 break; 3648 } 3649 } 3650 3651 static void bttv_print_riscaddr(struct bttv *btv) 3652 { 3653 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma); 3654 pr_info(" vbi : o=%08llx e=%08llx\n", 3655 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0, 3656 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0); 3657 pr_info(" cap : o=%08llx e=%08llx\n", 3658 btv->curr.top 3659 ? (unsigned long long)btv->curr.top->top.dma : 0, 3660 btv->curr.bottom 3661 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0); 3662 pr_info(" scr : o=%08llx e=%08llx\n", 3663 btv->screen ? (unsigned long long)btv->screen->top.dma : 0, 3664 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0); 3665 bttv_risc_disasm(btv, &btv->main); 3666 } 3667 3668 /* ----------------------------------------------------------------------- */ 3669 /* irq handler */ 3670 3671 static char *irq_name[] = { 3672 "FMTCHG", // format change detected (525 vs. 625) 3673 "VSYNC", // vertical sync (new field) 3674 "HSYNC", // horizontal sync 3675 "OFLOW", // chroma/luma AGC overflow 3676 "HLOCK", // horizontal lock changed 3677 "VPRES", // video presence changed 3678 "6", "7", 3679 "I2CDONE", // hw irc operation finished 3680 "GPINT", // gpio port triggered irq 3681 "10", 3682 "RISCI", // risc instruction triggered irq 3683 "FBUS", // pixel data fifo dropped data (high pci bus latencies) 3684 "FTRGT", // pixel data fifo overrun 3685 "FDSR", // fifo data stream resyncronisation 3686 "PPERR", // parity error (data transfer) 3687 "RIPERR", // parity error (read risc instructions) 3688 "PABORT", // pci abort 3689 "OCERR", // risc instruction error 3690 "SCERR", // syncronisation error 3691 }; 3692 3693 static void bttv_print_irqbits(u32 print, u32 mark) 3694 { 3695 unsigned int i; 3696 3697 pr_cont("bits:"); 3698 for (i = 0; i < ARRAY_SIZE(irq_name); i++) { 3699 if (print & (1 << i)) 3700 pr_cont(" %s", irq_name[i]); 3701 if (mark & (1 << i)) 3702 pr_cont("*"); 3703 } 3704 } 3705 3706 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc) 3707 { 3708 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n", 3709 btv->c.nr, 3710 (unsigned long)btv->main.dma, 3711 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]), 3712 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]), 3713 (unsigned long)rc); 3714 3715 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) { 3716 pr_notice("%d: Oh, there (temporarily?) is no input signal. " 3717 "Ok, then this is harmless, don't worry ;)\n", 3718 btv->c.nr); 3719 return; 3720 } 3721 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n", 3722 btv->c.nr); 3723 pr_notice("%d: Lets try to catch the culpit red-handed ...\n", 3724 btv->c.nr); 3725 dump_stack(); 3726 } 3727 3728 static int 3729 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set) 3730 { 3731 struct bttv_buffer *item; 3732 3733 memset(set,0,sizeof(*set)); 3734 3735 /* capture request ? */ 3736 if (!list_empty(&btv->capture)) { 3737 set->frame_irq = 1; 3738 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue); 3739 if (V4L2_FIELD_HAS_TOP(item->vb.field)) 3740 set->top = item; 3741 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field)) 3742 set->bottom = item; 3743 3744 /* capture request for other field ? */ 3745 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) && 3746 (item->vb.queue.next != &btv->capture)) { 3747 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue); 3748 /* Mike Isely <isely@pobox.com> - Only check 3749 * and set up the bottom field in the logic 3750 * below. Don't ever do the top field. This 3751 * of course means that if we set up the 3752 * bottom field in the above code that we'll 3753 * actually skip a field. But that's OK. 3754 * Having processed only a single buffer this 3755 * time, then the next time around the first 3756 * available buffer should be for a top field. 3757 * That will then cause us here to set up a 3758 * top then a bottom field in the normal way. 3759 * The alternative to this understanding is 3760 * that we set up the second available buffer 3761 * as a top field, but that's out of order 3762 * since this driver always processes the top 3763 * field first - the effect will be the two 3764 * buffers being returned in the wrong order, 3765 * with the second buffer also being delayed 3766 * by one field time (owing to the fifo nature 3767 * of videobuf). Worse still, we'll be stuck 3768 * doing fields out of order now every time 3769 * until something else causes a field to be 3770 * dropped. By effectively forcing a field to 3771 * drop this way then we always get back into 3772 * sync within a single frame time. (Out of 3773 * order fields can screw up deinterlacing 3774 * algorithms.) */ 3775 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) { 3776 if (NULL == set->bottom && 3777 V4L2_FIELD_BOTTOM == item->vb.field) { 3778 set->bottom = item; 3779 } 3780 if (NULL != set->top && NULL != set->bottom) 3781 set->top_irq = 2; 3782 } 3783 } 3784 } 3785 3786 /* screen overlay ? */ 3787 if (NULL != btv->screen) { 3788 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) { 3789 if (NULL == set->top && NULL == set->bottom) { 3790 set->top = btv->screen; 3791 set->bottom = btv->screen; 3792 } 3793 } else { 3794 if (V4L2_FIELD_TOP == btv->screen->vb.field && 3795 NULL == set->top) { 3796 set->top = btv->screen; 3797 } 3798 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field && 3799 NULL == set->bottom) { 3800 set->bottom = btv->screen; 3801 } 3802 } 3803 } 3804 3805 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n", 3806 btv->c.nr, set->top, set->bottom, 3807 btv->screen, set->frame_irq, set->top_irq); 3808 return 0; 3809 } 3810 3811 static void 3812 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup, 3813 struct bttv_buffer_set *curr, unsigned int state) 3814 { 3815 struct timeval ts; 3816 3817 do_gettimeofday(&ts); 3818 3819 if (wakeup->top == wakeup->bottom) { 3820 if (NULL != wakeup->top && curr->top != wakeup->top) { 3821 if (irq_debug > 1) 3822 pr_debug("%d: wakeup: both=%p\n", 3823 btv->c.nr, wakeup->top); 3824 wakeup->top->vb.ts = ts; 3825 wakeup->top->vb.field_count = btv->field_count; 3826 wakeup->top->vb.state = state; 3827 wake_up(&wakeup->top->vb.done); 3828 } 3829 } else { 3830 if (NULL != wakeup->top && curr->top != wakeup->top) { 3831 if (irq_debug > 1) 3832 pr_debug("%d: wakeup: top=%p\n", 3833 btv->c.nr, wakeup->top); 3834 wakeup->top->vb.ts = ts; 3835 wakeup->top->vb.field_count = btv->field_count; 3836 wakeup->top->vb.state = state; 3837 wake_up(&wakeup->top->vb.done); 3838 } 3839 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) { 3840 if (irq_debug > 1) 3841 pr_debug("%d: wakeup: bottom=%p\n", 3842 btv->c.nr, wakeup->bottom); 3843 wakeup->bottom->vb.ts = ts; 3844 wakeup->bottom->vb.field_count = btv->field_count; 3845 wakeup->bottom->vb.state = state; 3846 wake_up(&wakeup->bottom->vb.done); 3847 } 3848 } 3849 } 3850 3851 static void 3852 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup, 3853 unsigned int state) 3854 { 3855 struct timeval ts; 3856 3857 if (NULL == wakeup) 3858 return; 3859 3860 do_gettimeofday(&ts); 3861 wakeup->vb.ts = ts; 3862 wakeup->vb.field_count = btv->field_count; 3863 wakeup->vb.state = state; 3864 wake_up(&wakeup->vb.done); 3865 } 3866 3867 static void bttv_irq_timeout(unsigned long data) 3868 { 3869 struct bttv *btv = (struct bttv *)data; 3870 struct bttv_buffer_set old,new; 3871 struct bttv_buffer *ovbi; 3872 struct bttv_buffer *item; 3873 unsigned long flags; 3874 3875 if (bttv_verbose) { 3876 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ", 3877 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total, 3878 btread(BT848_RISC_COUNT)); 3879 bttv_print_irqbits(btread(BT848_INT_STAT),0); 3880 pr_cont("\n"); 3881 } 3882 3883 spin_lock_irqsave(&btv->s_lock,flags); 3884 3885 /* deactivate stuff */ 3886 memset(&new,0,sizeof(new)); 3887 old = btv->curr; 3888 ovbi = btv->cvbi; 3889 btv->curr = new; 3890 btv->cvbi = NULL; 3891 btv->loop_irq = 0; 3892 bttv_buffer_activate_video(btv, &new); 3893 bttv_buffer_activate_vbi(btv, NULL); 3894 bttv_set_dma(btv, 0); 3895 3896 /* wake up */ 3897 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR); 3898 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR); 3899 3900 /* cancel all outstanding capture / vbi requests */ 3901 while (!list_empty(&btv->capture)) { 3902 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue); 3903 list_del(&item->vb.queue); 3904 item->vb.state = VIDEOBUF_ERROR; 3905 wake_up(&item->vb.done); 3906 } 3907 while (!list_empty(&btv->vcapture)) { 3908 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue); 3909 list_del(&item->vb.queue); 3910 item->vb.state = VIDEOBUF_ERROR; 3911 wake_up(&item->vb.done); 3912 } 3913 3914 btv->errors++; 3915 spin_unlock_irqrestore(&btv->s_lock,flags); 3916 } 3917 3918 static void 3919 bttv_irq_wakeup_top(struct bttv *btv) 3920 { 3921 struct bttv_buffer *wakeup = btv->curr.top; 3922 3923 if (NULL == wakeup) 3924 return; 3925 3926 spin_lock(&btv->s_lock); 3927 btv->curr.top_irq = 0; 3928 btv->curr.top = NULL; 3929 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0); 3930 3931 do_gettimeofday(&wakeup->vb.ts); 3932 wakeup->vb.field_count = btv->field_count; 3933 wakeup->vb.state = VIDEOBUF_DONE; 3934 wake_up(&wakeup->vb.done); 3935 spin_unlock(&btv->s_lock); 3936 } 3937 3938 static inline int is_active(struct btcx_riscmem *risc, u32 rc) 3939 { 3940 if (rc < risc->dma) 3941 return 0; 3942 if (rc > risc->dma + risc->size) 3943 return 0; 3944 return 1; 3945 } 3946 3947 static void 3948 bttv_irq_switch_video(struct bttv *btv) 3949 { 3950 struct bttv_buffer_set new; 3951 struct bttv_buffer_set old; 3952 dma_addr_t rc; 3953 3954 spin_lock(&btv->s_lock); 3955 3956 /* new buffer set */ 3957 bttv_irq_next_video(btv, &new); 3958 rc = btread(BT848_RISC_COUNT); 3959 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) || 3960 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) { 3961 btv->framedrop++; 3962 if (debug_latency) 3963 bttv_irq_debug_low_latency(btv, rc); 3964 spin_unlock(&btv->s_lock); 3965 return; 3966 } 3967 3968 /* switch over */ 3969 old = btv->curr; 3970 btv->curr = new; 3971 btv->loop_irq &= ~1; 3972 bttv_buffer_activate_video(btv, &new); 3973 bttv_set_dma(btv, 0); 3974 3975 /* switch input */ 3976 if (UNSET != btv->new_input) { 3977 video_mux(btv,btv->new_input); 3978 btv->new_input = UNSET; 3979 } 3980 3981 /* wake up finished buffers */ 3982 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE); 3983 spin_unlock(&btv->s_lock); 3984 } 3985 3986 static void 3987 bttv_irq_switch_vbi(struct bttv *btv) 3988 { 3989 struct bttv_buffer *new = NULL; 3990 struct bttv_buffer *old; 3991 u32 rc; 3992 3993 spin_lock(&btv->s_lock); 3994 3995 if (!list_empty(&btv->vcapture)) 3996 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue); 3997 old = btv->cvbi; 3998 3999 rc = btread(BT848_RISC_COUNT); 4000 if (NULL != old && (is_active(&old->top, rc) || 4001 is_active(&old->bottom, rc))) { 4002 btv->framedrop++; 4003 if (debug_latency) 4004 bttv_irq_debug_low_latency(btv, rc); 4005 spin_unlock(&btv->s_lock); 4006 return; 4007 } 4008 4009 /* switch */ 4010 btv->cvbi = new; 4011 btv->loop_irq &= ~4; 4012 bttv_buffer_activate_vbi(btv, new); 4013 bttv_set_dma(btv, 0); 4014 4015 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE); 4016 spin_unlock(&btv->s_lock); 4017 } 4018 4019 static irqreturn_t bttv_irq(int irq, void *dev_id) 4020 { 4021 u32 stat,astat; 4022 u32 dstat; 4023 int count; 4024 struct bttv *btv; 4025 int handled = 0; 4026 4027 btv=(struct bttv *)dev_id; 4028 4029 count=0; 4030 while (1) { 4031 /* get/clear interrupt status bits */ 4032 stat=btread(BT848_INT_STAT); 4033 astat=stat&btread(BT848_INT_MASK); 4034 if (!astat) 4035 break; 4036 handled = 1; 4037 btwrite(stat,BT848_INT_STAT); 4038 4039 /* get device status bits */ 4040 dstat=btread(BT848_DSTATUS); 4041 4042 if (irq_debug) { 4043 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ", 4044 btv->c.nr, count, btv->field_count, 4045 stat>>28, btread(BT848_RISC_COUNT)); 4046 bttv_print_irqbits(stat,astat); 4047 if (stat & BT848_INT_HLOCK) 4048 pr_cont(" HLOC => %s", 4049 dstat & BT848_DSTATUS_HLOC 4050 ? "yes" : "no"); 4051 if (stat & BT848_INT_VPRES) 4052 pr_cont(" PRES => %s", 4053 dstat & BT848_DSTATUS_PRES 4054 ? "yes" : "no"); 4055 if (stat & BT848_INT_FMTCHG) 4056 pr_cont(" NUML => %s", 4057 dstat & BT848_DSTATUS_NUML 4058 ? "625" : "525"); 4059 pr_cont("\n"); 4060 } 4061 4062 if (astat&BT848_INT_VSYNC) 4063 btv->field_count++; 4064 4065 if ((astat & BT848_INT_GPINT) && btv->remote) { 4066 bttv_input_irq(btv); 4067 } 4068 4069 if (astat & BT848_INT_I2CDONE) { 4070 btv->i2c_done = stat; 4071 wake_up(&btv->i2c_queue); 4072 } 4073 4074 if ((astat & BT848_INT_RISCI) && (stat & (4<<28))) 4075 bttv_irq_switch_vbi(btv); 4076 4077 if ((astat & BT848_INT_RISCI) && (stat & (2<<28))) 4078 bttv_irq_wakeup_top(btv); 4079 4080 if ((astat & BT848_INT_RISCI) && (stat & (1<<28))) 4081 bttv_irq_switch_video(btv); 4082 4083 if ((astat & BT848_INT_HLOCK) && btv->opt_automute) 4084 audio_mute(btv, btv->mute); /* trigger automute */ 4085 4086 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) { 4087 pr_info("%d: %s%s @ %08x,", 4088 btv->c.nr, 4089 (astat & BT848_INT_SCERR) ? "SCERR" : "", 4090 (astat & BT848_INT_OCERR) ? "OCERR" : "", 4091 btread(BT848_RISC_COUNT)); 4092 bttv_print_irqbits(stat,astat); 4093 pr_cont("\n"); 4094 if (bttv_debug) 4095 bttv_print_riscaddr(btv); 4096 } 4097 if (fdsr && astat & BT848_INT_FDSR) { 4098 pr_info("%d: FDSR @ %08x\n", 4099 btv->c.nr, btread(BT848_RISC_COUNT)); 4100 if (bttv_debug) 4101 bttv_print_riscaddr(btv); 4102 } 4103 4104 count++; 4105 if (count > 4) { 4106 4107 if (count > 8 || !(astat & BT848_INT_GPINT)) { 4108 btwrite(0, BT848_INT_MASK); 4109 4110 pr_err("%d: IRQ lockup, cleared int mask [", 4111 btv->c.nr); 4112 } else { 4113 pr_err("%d: IRQ lockup, clearing GPINT from int mask [", 4114 btv->c.nr); 4115 4116 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT), 4117 BT848_INT_MASK); 4118 }; 4119 4120 bttv_print_irqbits(stat,astat); 4121 4122 pr_cont("]\n"); 4123 } 4124 } 4125 btv->irq_total++; 4126 if (handled) 4127 btv->irq_me++; 4128 return IRQ_RETVAL(handled); 4129 } 4130 4131 4132 /* ----------------------------------------------------------------------- */ 4133 /* initialitation */ 4134 4135 static struct video_device *vdev_init(struct bttv *btv, 4136 const struct video_device *template, 4137 const char *type_name) 4138 { 4139 struct video_device *vfd; 4140 4141 vfd = video_device_alloc(); 4142 if (NULL == vfd) 4143 return NULL; 4144 *vfd = *template; 4145 vfd->v4l2_dev = &btv->c.v4l2_dev; 4146 vfd->release = video_device_release; 4147 vfd->debug = bttv_debug; 4148 video_set_drvdata(vfd, btv); 4149 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)", 4150 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "", 4151 type_name, bttv_tvcards[btv->c.type].name); 4152 return vfd; 4153 } 4154 4155 static void bttv_unregister_video(struct bttv *btv) 4156 { 4157 if (btv->video_dev) { 4158 if (video_is_registered(btv->video_dev)) 4159 video_unregister_device(btv->video_dev); 4160 else 4161 video_device_release(btv->video_dev); 4162 btv->video_dev = NULL; 4163 } 4164 if (btv->vbi_dev) { 4165 if (video_is_registered(btv->vbi_dev)) 4166 video_unregister_device(btv->vbi_dev); 4167 else 4168 video_device_release(btv->vbi_dev); 4169 btv->vbi_dev = NULL; 4170 } 4171 if (btv->radio_dev) { 4172 if (video_is_registered(btv->radio_dev)) 4173 video_unregister_device(btv->radio_dev); 4174 else 4175 video_device_release(btv->radio_dev); 4176 btv->radio_dev = NULL; 4177 } 4178 } 4179 4180 /* register video4linux devices */ 4181 static int __devinit bttv_register_video(struct bttv *btv) 4182 { 4183 if (no_overlay > 0) 4184 pr_notice("Overlay support disabled\n"); 4185 4186 /* video */ 4187 btv->video_dev = vdev_init(btv, &bttv_video_template, "video"); 4188 4189 if (NULL == btv->video_dev) 4190 goto err; 4191 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER, 4192 video_nr[btv->c.nr]) < 0) 4193 goto err; 4194 pr_info("%d: registered device %s\n", 4195 btv->c.nr, video_device_node_name(btv->video_dev)); 4196 if (device_create_file(&btv->video_dev->dev, 4197 &dev_attr_card)<0) { 4198 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr); 4199 goto err; 4200 } 4201 4202 /* vbi */ 4203 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi"); 4204 4205 if (NULL == btv->vbi_dev) 4206 goto err; 4207 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI, 4208 vbi_nr[btv->c.nr]) < 0) 4209 goto err; 4210 pr_info("%d: registered device %s\n", 4211 btv->c.nr, video_device_node_name(btv->vbi_dev)); 4212 4213 if (!btv->has_radio) 4214 return 0; 4215 /* radio */ 4216 btv->radio_dev = vdev_init(btv, &radio_template, "radio"); 4217 if (NULL == btv->radio_dev) 4218 goto err; 4219 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO, 4220 radio_nr[btv->c.nr]) < 0) 4221 goto err; 4222 pr_info("%d: registered device %s\n", 4223 btv->c.nr, video_device_node_name(btv->radio_dev)); 4224 4225 /* all done */ 4226 return 0; 4227 4228 err: 4229 bttv_unregister_video(btv); 4230 return -1; 4231 } 4232 4233 4234 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */ 4235 /* response on cards with no firmware is not enabled by OF */ 4236 static void pci_set_command(struct pci_dev *dev) 4237 { 4238 #if defined(__powerpc__) 4239 unsigned int cmd; 4240 4241 pci_read_config_dword(dev, PCI_COMMAND, &cmd); 4242 cmd = (cmd | PCI_COMMAND_MEMORY ); 4243 pci_write_config_dword(dev, PCI_COMMAND, cmd); 4244 #endif 4245 } 4246 4247 static int __devinit bttv_probe(struct pci_dev *dev, 4248 const struct pci_device_id *pci_id) 4249 { 4250 int result; 4251 unsigned char lat; 4252 struct bttv *btv; 4253 4254 if (bttv_num == BTTV_MAX) 4255 return -ENOMEM; 4256 pr_info("Bt8xx card found (%d)\n", bttv_num); 4257 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL); 4258 if (btv == NULL) { 4259 pr_err("out of memory\n"); 4260 return -ENOMEM; 4261 } 4262 btv->c.nr = bttv_num; 4263 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name), 4264 "bttv%d", btv->c.nr); 4265 4266 /* initialize structs / fill in defaults */ 4267 mutex_init(&btv->lock); 4268 spin_lock_init(&btv->s_lock); 4269 spin_lock_init(&btv->gpio_lock); 4270 init_waitqueue_head(&btv->i2c_queue); 4271 INIT_LIST_HEAD(&btv->c.subs); 4272 INIT_LIST_HEAD(&btv->capture); 4273 INIT_LIST_HEAD(&btv->vcapture); 4274 v4l2_prio_init(&btv->prio); 4275 4276 init_timer(&btv->timeout); 4277 btv->timeout.function = bttv_irq_timeout; 4278 btv->timeout.data = (unsigned long)btv; 4279 4280 btv->i2c_rc = -1; 4281 btv->tuner_type = UNSET; 4282 btv->new_input = UNSET; 4283 btv->has_radio=radio[btv->c.nr]; 4284 4285 /* pci stuff (init, get irq/mmio, ... */ 4286 btv->c.pci = dev; 4287 btv->id = dev->device; 4288 if (pci_enable_device(dev)) { 4289 pr_warn("%d: Can't enable device\n", btv->c.nr); 4290 return -EIO; 4291 } 4292 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) { 4293 pr_warn("%d: No suitable DMA available\n", btv->c.nr); 4294 return -EIO; 4295 } 4296 if (!request_mem_region(pci_resource_start(dev,0), 4297 pci_resource_len(dev,0), 4298 btv->c.v4l2_dev.name)) { 4299 pr_warn("%d: can't request iomem (0x%llx)\n", 4300 btv->c.nr, 4301 (unsigned long long)pci_resource_start(dev, 0)); 4302 return -EBUSY; 4303 } 4304 pci_set_master(dev); 4305 pci_set_command(dev); 4306 4307 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev); 4308 if (result < 0) { 4309 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr); 4310 goto fail0; 4311 } 4312 4313 btv->revision = dev->revision; 4314 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); 4315 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n", 4316 bttv_num, btv->id, btv->revision, pci_name(dev), 4317 btv->c.pci->irq, lat, 4318 (unsigned long long)pci_resource_start(dev, 0)); 4319 schedule(); 4320 4321 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000); 4322 if (NULL == btv->bt848_mmio) { 4323 pr_err("%d: ioremap() failed\n", btv->c.nr); 4324 result = -EIO; 4325 goto fail1; 4326 } 4327 4328 /* identify card */ 4329 bttv_idcard(btv); 4330 4331 /* disable irqs, register irq handler */ 4332 btwrite(0, BT848_INT_MASK); 4333 result = request_irq(btv->c.pci->irq, bttv_irq, 4334 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv); 4335 if (result < 0) { 4336 pr_err("%d: can't get IRQ %d\n", 4337 bttv_num, btv->c.pci->irq); 4338 goto fail1; 4339 } 4340 4341 if (0 != bttv_handle_chipset(btv)) { 4342 result = -EIO; 4343 goto fail2; 4344 } 4345 4346 /* init options from insmod args */ 4347 btv->opt_combfilter = combfilter; 4348 btv->opt_lumafilter = lumafilter; 4349 btv->opt_automute = automute; 4350 btv->opt_chroma_agc = chroma_agc; 4351 btv->opt_adc_crush = adc_crush; 4352 btv->opt_vcr_hack = vcr_hack; 4353 btv->opt_whitecrush_upper = whitecrush_upper; 4354 btv->opt_whitecrush_lower = whitecrush_lower; 4355 btv->opt_uv_ratio = uv_ratio; 4356 btv->opt_full_luma_range = full_luma_range; 4357 btv->opt_coring = coring; 4358 4359 /* fill struct bttv with some useful defaults */ 4360 btv->init.btv = btv; 4361 btv->init.ov.w.width = 320; 4362 btv->init.ov.w.height = 240; 4363 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24); 4364 btv->init.width = 320; 4365 btv->init.height = 240; 4366 btv->input = 0; 4367 4368 /* initialize hardware */ 4369 if (bttv_gpio) 4370 bttv_gpio_tracking(btv,"pre-init"); 4371 4372 bttv_risc_init_main(btv); 4373 init_bt848(btv); 4374 4375 /* gpio */ 4376 btwrite(0x00, BT848_GPIO_REG_INP); 4377 btwrite(0x00, BT848_GPIO_OUT_EN); 4378 if (bttv_verbose) 4379 bttv_gpio_tracking(btv,"init"); 4380 4381 /* needs to be done before i2c is registered */ 4382 bttv_init_card1(btv); 4383 4384 /* register i2c + gpio */ 4385 init_bttv_i2c(btv); 4386 4387 /* some card-specific stuff (needs working i2c) */ 4388 bttv_init_card2(btv); 4389 bttv_init_tuner(btv); 4390 init_irqreg(btv); 4391 4392 /* register video4linux + input */ 4393 if (!bttv_tvcards[btv->c.type].no_video) { 4394 bttv_register_video(btv); 4395 bt848_bright(btv,32768); 4396 bt848_contrast(btv, 27648); 4397 bt848_hue(btv,32768); 4398 bt848_sat(btv,32768); 4399 audio_mute(btv, 1); 4400 set_input(btv, 0, btv->tvnorm); 4401 bttv_crop_reset(&btv->crop[0], btv->tvnorm); 4402 btv->crop[1] = btv->crop[0]; /* current = default */ 4403 disclaim_vbi_lines(btv); 4404 disclaim_video_lines(btv); 4405 } 4406 4407 /* add subdevices and autoload dvb-bt8xx if needed */ 4408 if (bttv_tvcards[btv->c.type].has_dvb) { 4409 bttv_sub_add_device(&btv->c, "dvb"); 4410 request_modules(btv); 4411 } 4412 4413 if (!disable_ir) { 4414 init_bttv_i2c_ir(btv); 4415 bttv_input_init(btv); 4416 } 4417 4418 /* everything is fine */ 4419 bttv_num++; 4420 return 0; 4421 4422 fail2: 4423 free_irq(btv->c.pci->irq,btv); 4424 4425 fail1: 4426 v4l2_device_unregister(&btv->c.v4l2_dev); 4427 4428 fail0: 4429 if (btv->bt848_mmio) 4430 iounmap(btv->bt848_mmio); 4431 release_mem_region(pci_resource_start(btv->c.pci,0), 4432 pci_resource_len(btv->c.pci,0)); 4433 return result; 4434 } 4435 4436 static void __devexit bttv_remove(struct pci_dev *pci_dev) 4437 { 4438 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev); 4439 struct bttv *btv = to_bttv(v4l2_dev); 4440 4441 if (bttv_verbose) 4442 pr_info("%d: unloading\n", btv->c.nr); 4443 4444 if (bttv_tvcards[btv->c.type].has_dvb) 4445 flush_request_modules(btv); 4446 4447 /* shutdown everything (DMA+IRQs) */ 4448 btand(~15, BT848_GPIO_DMA_CTL); 4449 btwrite(0, BT848_INT_MASK); 4450 btwrite(~0x0, BT848_INT_STAT); 4451 btwrite(0x0, BT848_GPIO_OUT_EN); 4452 if (bttv_gpio) 4453 bttv_gpio_tracking(btv,"cleanup"); 4454 4455 /* tell gpio modules we are leaving ... */ 4456 btv->shutdown=1; 4457 bttv_input_fini(btv); 4458 bttv_sub_del_devices(&btv->c); 4459 4460 /* unregister i2c_bus + input */ 4461 fini_bttv_i2c(btv); 4462 4463 /* unregister video4linux */ 4464 bttv_unregister_video(btv); 4465 4466 /* free allocated memory */ 4467 btcx_riscmem_free(btv->c.pci,&btv->main); 4468 4469 /* free ressources */ 4470 free_irq(btv->c.pci->irq,btv); 4471 iounmap(btv->bt848_mmio); 4472 release_mem_region(pci_resource_start(btv->c.pci,0), 4473 pci_resource_len(btv->c.pci,0)); 4474 4475 v4l2_device_unregister(&btv->c.v4l2_dev); 4476 bttvs[btv->c.nr] = NULL; 4477 kfree(btv); 4478 4479 return; 4480 } 4481 4482 #ifdef CONFIG_PM 4483 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state) 4484 { 4485 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev); 4486 struct bttv *btv = to_bttv(v4l2_dev); 4487 struct bttv_buffer_set idle; 4488 unsigned long flags; 4489 4490 dprintk("%d: suspend %d\n", btv->c.nr, state.event); 4491 4492 /* stop dma + irqs */ 4493 spin_lock_irqsave(&btv->s_lock,flags); 4494 memset(&idle, 0, sizeof(idle)); 4495 btv->state.video = btv->curr; 4496 btv->state.vbi = btv->cvbi; 4497 btv->state.loop_irq = btv->loop_irq; 4498 btv->curr = idle; 4499 btv->loop_irq = 0; 4500 bttv_buffer_activate_video(btv, &idle); 4501 bttv_buffer_activate_vbi(btv, NULL); 4502 bttv_set_dma(btv, 0); 4503 btwrite(0, BT848_INT_MASK); 4504 spin_unlock_irqrestore(&btv->s_lock,flags); 4505 4506 /* save bt878 state */ 4507 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN); 4508 btv->state.gpio_data = gpio_read(); 4509 4510 /* save pci state */ 4511 pci_save_state(pci_dev); 4512 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) { 4513 pci_disable_device(pci_dev); 4514 btv->state.disabled = 1; 4515 } 4516 return 0; 4517 } 4518 4519 static int bttv_resume(struct pci_dev *pci_dev) 4520 { 4521 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev); 4522 struct bttv *btv = to_bttv(v4l2_dev); 4523 unsigned long flags; 4524 int err; 4525 4526 dprintk("%d: resume\n", btv->c.nr); 4527 4528 /* restore pci state */ 4529 if (btv->state.disabled) { 4530 err=pci_enable_device(pci_dev); 4531 if (err) { 4532 pr_warn("%d: Can't enable device\n", btv->c.nr); 4533 return err; 4534 } 4535 btv->state.disabled = 0; 4536 } 4537 err=pci_set_power_state(pci_dev, PCI_D0); 4538 if (err) { 4539 pci_disable_device(pci_dev); 4540 pr_warn("%d: Can't enable device\n", btv->c.nr); 4541 btv->state.disabled = 1; 4542 return err; 4543 } 4544 4545 pci_restore_state(pci_dev); 4546 4547 /* restore bt878 state */ 4548 bttv_reinit_bt848(btv); 4549 gpio_inout(0xffffff, btv->state.gpio_enable); 4550 gpio_write(btv->state.gpio_data); 4551 4552 /* restart dma */ 4553 spin_lock_irqsave(&btv->s_lock,flags); 4554 btv->curr = btv->state.video; 4555 btv->cvbi = btv->state.vbi; 4556 btv->loop_irq = btv->state.loop_irq; 4557 bttv_buffer_activate_video(btv, &btv->curr); 4558 bttv_buffer_activate_vbi(btv, btv->cvbi); 4559 bttv_set_dma(btv, 0); 4560 spin_unlock_irqrestore(&btv->s_lock,flags); 4561 return 0; 4562 } 4563 #endif 4564 4565 static struct pci_device_id bttv_pci_tbl[] = { 4566 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0}, 4567 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0}, 4568 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0}, 4569 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0}, 4570 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0}, 4571 {0,} 4572 }; 4573 4574 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl); 4575 4576 static struct pci_driver bttv_pci_driver = { 4577 .name = "bttv", 4578 .id_table = bttv_pci_tbl, 4579 .probe = bttv_probe, 4580 .remove = __devexit_p(bttv_remove), 4581 #ifdef CONFIG_PM 4582 .suspend = bttv_suspend, 4583 .resume = bttv_resume, 4584 #endif 4585 }; 4586 4587 static int __init bttv_init_module(void) 4588 { 4589 int ret; 4590 4591 bttv_num = 0; 4592 4593 pr_info("driver version %s loaded\n", BTTV_VERSION); 4594 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME) 4595 gbuffers = 2; 4596 if (gbufsize > BTTV_MAX_FBUF) 4597 gbufsize = BTTV_MAX_FBUF; 4598 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK; 4599 if (bttv_verbose) 4600 pr_info("using %d buffers with %dk (%d pages) each for capture\n", 4601 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT); 4602 4603 bttv_check_chipset(); 4604 4605 ret = bus_register(&bttv_sub_bus_type); 4606 if (ret < 0) { 4607 pr_warn("bus_register error: %d\n", ret); 4608 return ret; 4609 } 4610 ret = pci_register_driver(&bttv_pci_driver); 4611 if (ret < 0) 4612 bus_unregister(&bttv_sub_bus_type); 4613 4614 return ret; 4615 } 4616 4617 static void __exit bttv_cleanup_module(void) 4618 { 4619 pci_unregister_driver(&bttv_pci_driver); 4620 bus_unregister(&bttv_sub_bus_type); 4621 } 4622 4623 module_init(bttv_init_module); 4624 module_exit(bttv_cleanup_module); 4625 4626 /* 4627 * Local variables: 4628 * c-basic-offset: 8 4629 * End: 4630 */ 4631