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