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