1 /* 2 Copyright 1999, Be Incorporated. All Rights Reserved. 3 This file may be used under the terms of the Be Sample Code License. 4 5 Other authors: 6 Mark Watson; 7 Rudolf Cornelissen 3/2002-4/2006. 8 */ 9 10 /* standard kernel driver stuff */ 11 #include <KernelExport.h> 12 #include <ISA.h> 13 #include <PCI.h> 14 #include <OS.h> 15 #include <driver_settings.h> 16 #include <malloc.h> 17 #include <stdlib.h> // for strtoXX 18 #include "AGP.h" 19 20 /* this is for the standardized portion of the driver API */ 21 /* currently only one operation is defined: B_GET_ACCELERANT_SIGNATURE */ 22 #include <graphic_driver.h> 23 24 /* this is for sprintf() */ 25 #include <stdio.h> 26 27 /* this is for string compares */ 28 #include <string.h> 29 30 /* The private interface between the accelerant and the kernel driver. */ 31 #include "DriverInterface.h" 32 #include "macros.h" 33 34 #define get_pci(o, s) (*pci_bus->read_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s)) 35 #define set_pci(o, s, v) (*pci_bus->write_pci_config)(pcii->bus, pcii->device, pcii->function, (o), (s), (v)) 36 37 #define MAX_DEVICES 8 38 39 #define DEVICE_FORMAT "%04x_%04x_%02x%02x%02x" // apsed 40 41 /* Tell the kernel what revision of the driver API we support */ 42 int32 api_version = B_CUR_DRIVER_API_VERSION; // apsed, was 2, is 2 in R5 43 44 /* these structures are private to the kernel driver */ 45 typedef struct device_info device_info; 46 47 typedef struct { 48 timer te; /* timer entry for add_timer() */ 49 device_info *di; /* pointer to the owning device */ 50 bigtime_t when_target; /* when we're supposed to wake up */ 51 } timer_info; 52 53 struct device_info { 54 uint32 is_open; /* a count of how many times the devices has been opened */ 55 area_id shared_area; /* the area shared between the driver and all of the accelerants */ 56 shared_info *si; /* a pointer to the shared area, for convenience */ 57 vuint32 *regs; /* kernel's pointer to memory mapped registers */ 58 pci_info pcii; /* a convenience copy of the pci info for this device */ 59 char name[B_OS_NAME_LENGTH]; /* where we keep the name of the device for publishing and comparing */ 60 }; 61 62 typedef struct { 63 uint32 count; /* number of devices actually found */ 64 benaphore kernel; /* for serializing opens/closes */ 65 char *device_names[MAX_DEVICES+1]; /* device name pointer storage */ 66 device_info di[MAX_DEVICES]; /* device specific stuff */ 67 } DeviceData; 68 69 /* prototypes for our private functions */ 70 static status_t open_hook (const char* name, uint32 flags, void** cookie); 71 static status_t close_hook (void* dev); 72 static status_t free_hook (void* dev); 73 static status_t read_hook (void* dev, off_t pos, void* buf, size_t* len); 74 static status_t write_hook (void* dev, off_t pos, const void* buf, size_t* len); 75 static status_t control_hook (void* dev, uint32 msg, void *buf, size_t len); 76 static status_t map_device(device_info *di); 77 static void unmap_device(device_info *di); 78 static void probe_devices(void); 79 static int32 eng_interrupt(void *data); 80 81 static DeviceData *pd; 82 static isa_module_info *isa_bus = NULL; 83 static pci_module_info *pci_bus = NULL; 84 static agp_module_info *agp_bus = NULL; 85 static device_hooks graphics_device_hooks = { 86 open_hook, 87 close_hook, 88 free_hook, 89 control_hook, 90 read_hook, 91 write_hook, 92 NULL, 93 NULL, 94 NULL, 95 NULL 96 }; 97 98 #define VENDOR_ID_VIA 0x1106 /* Via */ 99 100 static uint16 via_device_list[] = { 101 0x3022, /* CLE266 Unichrome Pro (CLE3022) */ 102 0x3108, /* K8M800 Unichrome Pro (unknown chiptype) */ 103 0x3122, /* CLE266 Unichrome Pro (CLE3122) */ 104 0x3205, /* KM400 Unichrome (VT3205) */ 105 0x7205, /* KM400 Unichrome (VT7205) */ 106 0 107 }; 108 109 static struct { 110 uint16 vendor; 111 uint16 *devices; 112 } SupportedDevices[] = { 113 {VENDOR_ID_VIA, via_device_list}, 114 {0x0000, NULL} 115 }; 116 117 static settings current_settings = { // see comments in skel.settings 118 // for driver 119 DRIVER_PREFIX ".accelerant", 120 false, // dumprom 121 // for accelerant 122 0x00000000, // logmask 123 0, // memory 124 true, // usebios 125 true, // hardcursor 126 false, // switchhead 127 false, // force_pci 128 false, // unhide_fw 129 true, // pgm_panel 130 }; 131 132 static void dumprom (void *rom, uint32 size) 133 { 134 int fd; 135 uint32 cnt; 136 137 fd = open ("/boot/home/" DRIVER_PREFIX ".rom", O_WRONLY | O_CREAT, 0666); 138 if (fd < 0) return; 139 140 /* apparantly max. 32kb may be written at once; 141 * the ROM size is a multiple of that anyway. */ 142 for (cnt = 0; (cnt < size); cnt += 32768) 143 write (fd, ((void *)(((uint8 *)rom) + cnt)), 32768); 144 close (fd); 145 } 146 147 /* return 1 if vblank interrupt has occured */ 148 static int caused_vbi(vuint32 * regs) 149 { 150 // return (ENG_REG32(RG32_CRTC_INTS) & 0x00000001); 151 return 0; 152 } 153 154 /* clear the vblank interrupt */ 155 static void clear_vbi(vuint32 * regs) 156 { 157 // ENG_REG32(RG32_CRTC_INTS) = 0x00000001; 158 } 159 160 static void enable_vbi(vuint32 * regs) 161 { 162 /* clear the vblank interrupt */ 163 // ENG_REG32(RG32_CRTC_INTS) = 0x00000001; 164 /* enable nVidia interrupt source vblank */ 165 // ENG_REG32(RG32_CRTC_INTE) |= 0x00000001; 166 /* enable nVidia interrupt system hardware (b0-1) */ 167 // ENG_REG32(RG32_MAIN_INTE) = 0x00000001; 168 } 169 170 static void disable_vbi(vuint32 * regs) 171 { 172 /* disable nVidia interrupt source vblank */ 173 // ENG_REG32(RG32_CRTC_INTE) &= 0xfffffffe; 174 /* clear the vblank interrupt */ 175 // ENG_REG32(RG32_CRTC_INTS) = 0x00000001; 176 /* disable nVidia interrupt system hardware (b0-1) */ 177 // ENG_REG32(RG32_MAIN_INTE) = 0x00000000; 178 } 179 180 /* 181 init_hardware() - Returns B_OK if one is 182 found, otherwise returns B_ERROR so the driver will be unloaded. 183 */ 184 status_t 185 init_hardware(void) { 186 long pci_index = 0; 187 pci_info pcii; 188 bool found_one = false; 189 190 /* choke if we can't find the PCI bus */ 191 if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK) 192 return B_ERROR; 193 194 /* choke if we can't find the ISA bus */ 195 if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK) 196 { 197 put_module(B_PCI_MODULE_NAME); 198 return B_ERROR; 199 } 200 201 /* while there are more pci devices */ 202 while ((*pci_bus->get_nth_pci_info)(pci_index, &pcii) == B_NO_ERROR) { 203 int vendor = 0; 204 205 /* if we match a supported vendor */ 206 while (SupportedDevices[vendor].vendor) { 207 if (SupportedDevices[vendor].vendor == pcii.vendor_id) { 208 uint16 *devices = SupportedDevices[vendor].devices; 209 /* while there are more supported devices */ 210 while (*devices) { 211 /* if we match a supported device */ 212 if (*devices == pcii.device_id ) { 213 214 found_one = true; 215 goto done; 216 } 217 /* next supported device */ 218 devices++; 219 } 220 } 221 vendor++; 222 } 223 /* next pci_info struct, please */ 224 pci_index++; 225 } 226 227 done: 228 /* put away the module manager */ 229 put_module(B_PCI_MODULE_NAME); 230 return (found_one ? B_OK : B_ERROR); 231 } 232 233 status_t 234 init_driver(void) { 235 void *settings_handle; 236 237 // get driver/accelerant settings, apsed 238 settings_handle = load_driver_settings (DRIVER_PREFIX ".settings"); 239 if (settings_handle != NULL) { 240 const char *item; 241 char *end; 242 uint32 value; 243 244 // for driver 245 item = get_driver_parameter (settings_handle, "accelerant", "", ""); 246 if ((strlen (item) > 0) && (strlen (item) < sizeof (current_settings.accelerant) - 1)) { 247 strcpy (current_settings.accelerant, item); 248 } 249 current_settings.dumprom = get_driver_boolean_parameter (settings_handle, "dumprom", false, false); 250 251 // for accelerant 252 item = get_driver_parameter (settings_handle, "logmask", "0x00000000", "0x00000000"); 253 value = strtoul (item, &end, 0); 254 if (*end == '\0') current_settings.logmask = value; 255 256 item = get_driver_parameter (settings_handle, "memory", "0", "0"); 257 value = strtoul (item, &end, 0); 258 if (*end == '\0') current_settings.memory = value; 259 260 current_settings.hardcursor = get_driver_boolean_parameter (settings_handle, "hardcursor", false, false); 261 current_settings.usebios = get_driver_boolean_parameter (settings_handle, "usebios", false, false); 262 current_settings.switchhead = get_driver_boolean_parameter (settings_handle, "switchhead", false, false); 263 current_settings.force_pci = get_driver_boolean_parameter (settings_handle, "force_pci", false, false); 264 current_settings.unhide_fw = get_driver_boolean_parameter (settings_handle, "unhide_fw", false, false); 265 current_settings.pgm_panel = get_driver_boolean_parameter (settings_handle, "pgm_panel", false, false); 266 267 unload_driver_settings (settings_handle); 268 } 269 270 /* get a handle for the pci bus */ 271 if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK) 272 return B_ERROR; 273 274 /* get a handle for the isa bus */ 275 if (get_module(B_ISA_MODULE_NAME, (module_info **)&isa_bus) != B_OK) 276 { 277 put_module(B_PCI_MODULE_NAME); 278 return B_ERROR; 279 } 280 281 /* get a handle for the agp bus if it exists */ 282 get_module(B_AGP_MODULE_NAME, (module_info **)&agp_bus); 283 284 /* driver private data */ 285 pd = (DeviceData *)calloc(1, sizeof(DeviceData)); 286 if (!pd) { 287 put_module(B_PCI_MODULE_NAME); 288 return B_ERROR; 289 } 290 /* initialize the benaphore */ 291 INIT_BEN(pd->kernel); 292 /* find all of our supported devices */ 293 probe_devices(); 294 return B_OK; 295 } 296 297 const char ** 298 publish_devices(void) { 299 /* return the list of supported devices */ 300 return (const char **)pd->device_names; 301 } 302 303 device_hooks * 304 find_device(const char *name) { 305 int index = 0; 306 while (pd->device_names[index]) { 307 if (strcmp(name, pd->device_names[index]) == 0) 308 return &graphics_device_hooks; 309 index++; 310 } 311 return NULL; 312 313 } 314 315 void uninit_driver(void) { 316 317 /* free the driver data */ 318 DELETE_BEN(pd->kernel); 319 free(pd); 320 pd = NULL; 321 322 /* put the pci module away */ 323 put_module(B_PCI_MODULE_NAME); 324 put_module(B_ISA_MODULE_NAME); 325 326 /* put the agp module away if it's there */ 327 if (agp_bus) put_module(B_AGP_MODULE_NAME); 328 } 329 330 static status_t map_device(device_info *di) 331 { 332 char buffer[B_OS_NAME_LENGTH]; /*memory for device name*/ 333 shared_info *si = di->si; 334 uint32 tmpUlong; 335 pci_info *pcii = &(di->pcii); 336 system_info sysinfo; 337 338 /*storage for the physical to virtual table (used for dma buffer)*/ 339 // physical_entry physical_memory[2]; 340 // #define G400_DMA_BUFFER_SIZE 1024*1024 341 342 /* variables for making copy of ROM */ 343 uint8* rom_temp; 344 area_id rom_area; 345 346 /* Nvidia cards have registers in [0] and framebuffer in [1] */ 347 int registers = 1; 348 int frame_buffer = 0; 349 // int pseudo_dma = 2; 350 351 /* enable memory mapped IO, disable VGA I/O - this is defined in the PCI standard */ 352 tmpUlong = get_pci(PCI_command, 2); 353 /* enable PCI access */ 354 tmpUlong |= PCI_command_memory; 355 /* enable busmastering */ 356 tmpUlong |= PCI_command_master; 357 /* disable ISA I/O access */ 358 tmpUlong &= ~PCI_command_io; 359 set_pci(PCI_command, 2, tmpUlong); 360 361 /*work out which version of BeOS is running*/ 362 get_system_info(&sysinfo); 363 if (0)//sysinfo.kernel_build_date[0]=='J')/*FIXME - better ID version*/ 364 { 365 si->use_clone_bugfix = 1; 366 } 367 else 368 { 369 si->use_clone_bugfix = 0; 370 } 371 372 /* work out a name for the register mapping */ 373 sprintf(buffer, DEVICE_FORMAT " regs", 374 di->pcii.vendor_id, di->pcii.device_id, 375 di->pcii.bus, di->pcii.device, di->pcii.function); 376 377 /* get a virtual memory address for the registers*/ 378 si->regs_area = map_physical_memory( 379 buffer, 380 /* WARNING: Nvidia needs to map regs as viewed from PCI space! */ 381 (void *) di->pcii.u.h0.base_registers_pci[registers], 382 di->pcii.u.h0.base_register_sizes[registers], 383 B_ANY_KERNEL_ADDRESS, 384 (si->use_clone_bugfix ? B_READ_AREA|B_WRITE_AREA : 0), 385 (void **)&(di->regs)); 386 si->clone_bugfix_regs = (uint32 *) di->regs; 387 388 /* if mapping registers to vmem failed then pass on error */ 389 if (si->regs_area < 0) return si->regs_area; 390 391 /* work out a name for the ROM mapping*/ 392 sprintf(buffer, DEVICE_FORMAT " rom", 393 di->pcii.vendor_id, di->pcii.device_id, 394 di->pcii.bus, di->pcii.device, di->pcii.function); 395 396 /* disable ROM shadowing, we want the guaranteed exact contents of the chip */ 397 /* warning: 398 * don't touch: (confirmed) NV04, NV05, NV05-M64, NV11 all shutoff otherwise. 399 * NV18, NV28 and NV34 keep working. 400 * confirmed NV28 and NV34 to use upper part of shadowed ROM for scratch purposes, 401 * however the actual ROM content (so the used part) is intact (confirmed). */ 402 //set_pci(ENCFG_ROMSHADOW, 4, 0); 403 404 /* get ROM memory mapped base adress - this is defined in the PCI standard */ 405 tmpUlong = get_pci(PCI_rom_base, 4); 406 if (tmpUlong) 407 { 408 /* ROM was assigned an adress, so enable ROM decoding - see PCI standard */ 409 tmpUlong |= 0x00000001; 410 set_pci(PCI_rom_base, 4, tmpUlong); 411 412 rom_area = map_physical_memory( 413 buffer, 414 (void *)di->pcii.u.h0.rom_base_pci, 415 di->pcii.u.h0.rom_size, 416 B_ANY_KERNEL_ADDRESS, 417 B_READ_AREA, 418 (void **)&(rom_temp) 419 ); 420 421 /* check if we got the BIOS signature (might fail on laptops..) */ 422 if (rom_temp[0]!=0x55 || rom_temp[1]!=0xaa) 423 { 424 /* apparantly no ROM is mapped here */ 425 delete_area(rom_area); 426 rom_area = -1; 427 /* force using ISA legacy map as fall-back */ 428 tmpUlong = 0x00000000; 429 } 430 } 431 432 if (!tmpUlong) 433 { 434 /* ROM was not assigned an adress, fetch it from ISA legacy memory map! */ 435 rom_area = map_physical_memory( 436 buffer, 437 (void *)0x000c0000, 438 65536, 439 B_ANY_KERNEL_ADDRESS, 440 B_READ_AREA, 441 (void **)&(rom_temp) 442 ); 443 } 444 445 /* if mapping ROM to vmem failed then clean up and pass on error */ 446 if (rom_area < 0) { 447 delete_area(si->regs_area); 448 si->regs_area = -1; 449 return rom_area; 450 } 451 452 /* dump ROM to file if selected in skel.settings 453 * (ROM always fits in 64Kb: checked TNT1 - FX5950) */ 454 if (current_settings.dumprom) dumprom (rom_temp, 65536); 455 /* make a copy of ROM for future reference */ 456 memcpy (si->rom_mirror, rom_temp, 65536); 457 458 /* disable ROM decoding - this is defined in the PCI standard, and delete the area */ 459 tmpUlong = get_pci(PCI_rom_base, 4); 460 tmpUlong &= 0xfffffffe; 461 set_pci(PCI_rom_base, 4, tmpUlong); 462 delete_area(rom_area); 463 464 /* work out a name for the framebuffer mapping*/ 465 sprintf(buffer, DEVICE_FORMAT " framebuffer", 466 di->pcii.vendor_id, di->pcii.device_id, 467 di->pcii.bus, di->pcii.device, di->pcii.function); 468 469 /* map the framebuffer into vmem, using Write Combining*/ 470 si->fb_area = map_physical_memory( 471 buffer, 472 /* WARNING: Nvidia needs to map framebuffer as viewed from PCI space! */ 473 (void *) di->pcii.u.h0.base_registers_pci[frame_buffer], 474 di->pcii.u.h0.base_register_sizes[frame_buffer], 475 B_ANY_KERNEL_BLOCK_ADDRESS | B_MTR_WC, 476 B_READ_AREA + B_WRITE_AREA, 477 &(si->framebuffer)); 478 479 /*if failed with write combining try again without*/ 480 if (si->fb_area < 0) { 481 si->fb_area = map_physical_memory( 482 buffer, 483 /* WARNING: Nvidia needs to map framebuffer as viewed from PCI space! */ 484 (void *) di->pcii.u.h0.base_registers_pci[frame_buffer], 485 di->pcii.u.h0.base_register_sizes[frame_buffer], 486 B_ANY_KERNEL_BLOCK_ADDRESS, 487 B_READ_AREA + B_WRITE_AREA, 488 &(si->framebuffer)); 489 } 490 491 /* if there was an error, delete our other areas and pass on error*/ 492 if (si->fb_area < 0) 493 { 494 delete_area(si->regs_area); 495 si->regs_area = -1; 496 return si->fb_area; 497 } 498 //fixme: retest for card coldstart and PCI/virt_mem mapping!! 499 /* remember the DMA address of the frame buffer for BDirectWindow?? purposes */ 500 si->framebuffer_pci = (void *) di->pcii.u.h0.base_registers_pci[frame_buffer]; 501 502 // remember settings for use here and in accelerant 503 si->settings = current_settings; 504 505 /* in any case, return the result */ 506 return si->fb_area; 507 } 508 509 static void unmap_device(device_info *di) { 510 shared_info *si = di->si; 511 uint32 tmpUlong; 512 pci_info *pcii = &(di->pcii); 513 514 /* disable memory mapped IO */ 515 tmpUlong = get_pci(PCI_command, 4); 516 tmpUlong &= 0xfffffffc; 517 set_pci(PCI_command, 4, tmpUlong); 518 /* delete the areas */ 519 if (si->regs_area >= 0) delete_area(si->regs_area); 520 if (si->fb_area >= 0) delete_area(si->fb_area); 521 si->regs_area = si->fb_area = -1; 522 si->framebuffer = NULL; 523 di->regs = NULL; 524 } 525 526 static void probe_devices(void) { 527 uint32 pci_index = 0; 528 uint32 count = 0; 529 device_info *di = pd->di; 530 531 /* while there are more pci devices */ 532 while ((count < MAX_DEVICES) && ((*pci_bus->get_nth_pci_info)(pci_index, &(di->pcii)) == B_NO_ERROR)) { 533 int vendor = 0; 534 535 /* if we match a supported vendor */ 536 while (SupportedDevices[vendor].vendor) { 537 if (SupportedDevices[vendor].vendor == di->pcii.vendor_id) { 538 uint16 *devices = SupportedDevices[vendor].devices; 539 /* while there are more supported devices */ 540 while (*devices) { 541 /* if we match a supported device */ 542 if (*devices == di->pcii.device_id ) { 543 /* publish the device name */ 544 sprintf(di->name, "graphics/" DEVICE_FORMAT, 545 di->pcii.vendor_id, di->pcii.device_id, 546 di->pcii.bus, di->pcii.device, di->pcii.function); 547 548 /* remember the name */ 549 pd->device_names[count] = di->name; 550 /* mark the driver as available for R/W open */ 551 di->is_open = 0; 552 /* mark areas as not yet created */ 553 di->shared_area = -1; 554 /* mark pointer to shared data as invalid */ 555 di->si = NULL; 556 /* inc pointer to device info */ 557 di++; 558 /* inc count */ 559 count++; 560 /* break out of these while loops */ 561 goto next_device; 562 } 563 /* next supported device */ 564 devices++; 565 } 566 } 567 vendor++; 568 } 569 next_device: 570 /* next pci_info struct, please */ 571 pci_index++; 572 } 573 /* propagate count */ 574 pd->count = count; 575 /* terminate list of device names with a null pointer */ 576 pd->device_names[pd->count] = NULL; 577 } 578 579 static uint32 thread_interrupt_work(int32 *flags, vuint32 *regs, shared_info *si) { 580 uint32 handled = B_HANDLED_INTERRUPT; 581 /* release the vblank semaphore */ 582 if (si->vblank >= 0) { 583 int32 blocked; 584 if ((get_sem_count(si->vblank, &blocked) == B_OK) && (blocked < 0)) { 585 release_sem_etc(si->vblank, -blocked, B_DO_NOT_RESCHEDULE); 586 handled = B_INVOKE_SCHEDULER; 587 } 588 } 589 return handled; 590 } 591 592 static int32 593 eng_interrupt(void *data) 594 { 595 int32 handled = B_UNHANDLED_INTERRUPT; 596 device_info *di = (device_info *)data; 597 shared_info *si = di->si; 598 int32 *flags = &(si->flags); 599 vuint32 *regs; 600 601 /* is someone already handling an interrupt for this device? */ 602 if (atomic_or(flags, SKD_HANDLER_INSTALLED) & SKD_HANDLER_INSTALLED) { 603 goto exit0; 604 } 605 /* get regs */ 606 regs = di->regs; 607 608 /* was it a VBI? */ 609 if (caused_vbi(regs)) { 610 /*clear the interrupt*/ 611 clear_vbi(regs); 612 /*release the semaphore*/ 613 handled = thread_interrupt_work(flags, regs, si); 614 } 615 616 /* note that we're not in the handler any more */ 617 atomic_and(flags, ~SKD_HANDLER_INSTALLED); 618 619 exit0: 620 return handled; 621 } 622 623 static status_t open_hook (const char* name, uint32 flags, void** cookie) { 624 int32 index = 0; 625 device_info *di; 626 shared_info *si; 627 thread_id thid; 628 thread_info thinfo; 629 status_t result = B_OK; 630 vuint32 *regs; 631 char shared_name[B_OS_NAME_LENGTH]; 632 633 /* find the device name in the list of devices */ 634 /* we're never passed a name we didn't publish */ 635 while (pd->device_names[index] && (strcmp(name, pd->device_names[index]) != 0)) index++; 636 637 /* for convienience */ 638 di = &(pd->di[index]); 639 640 /* make sure no one else has write access to the common data */ 641 AQUIRE_BEN(pd->kernel); 642 643 /* if it's already open for writing */ 644 if (di->is_open) { 645 /* mark it open another time */ 646 goto mark_as_open; 647 } 648 /* create the shared area */ 649 sprintf(shared_name, DEVICE_FORMAT " shared", 650 di->pcii.vendor_id, di->pcii.device_id, 651 di->pcii.bus, di->pcii.device, di->pcii.function); 652 /* create this area with NO user-space read or write permissions, to prevent accidental dammage */ 653 di->shared_area = create_area(shared_name, (void **)&(di->si), B_ANY_KERNEL_ADDRESS, ((sizeof(shared_info) + (B_PAGE_SIZE - 1)) & ~(B_PAGE_SIZE - 1)), B_FULL_LOCK, 0); 654 if (di->shared_area < 0) { 655 /* return the error */ 656 result = di->shared_area; 657 goto done; 658 } 659 660 /* save a few dereferences */ 661 si = di->si; 662 663 /* save the vendor and device IDs */ 664 si->vendor_id = di->pcii.vendor_id; 665 si->device_id = di->pcii.device_id; 666 si->revision = di->pcii.revision; 667 si->bus = di->pcii.bus; 668 si->device = di->pcii.device; 669 si->function = di->pcii.function; 670 671 /* device at bus #0, device #0, function #0 holds byte value at byte-index 0xf6 */ 672 si->ps.chip_rev = ((*pci_bus->read_pci_config)(0, 0, 0, 0xf6, 1)); 673 674 /* map the device */ 675 result = map_device(di); 676 if (result < 0) goto free_shared; 677 result = B_OK; 678 679 /* create a semaphore for vertical blank management */ 680 si->vblank = create_sem(0, di->name); 681 if (si->vblank < 0) { 682 result = si->vblank; 683 goto unmap; 684 } 685 686 /* change the owner of the semaphores to the opener's team */ 687 /* this is required because apps can't aquire kernel semaphores */ 688 thid = find_thread(NULL); 689 get_thread_info(thid, &thinfo); 690 set_sem_owner(si->vblank, thinfo.team); 691 692 /* assign local regs pointer for SAMPLExx() macros */ 693 regs = di->regs; 694 695 /* disable and clear any pending interrupts */ 696 disable_vbi(regs); 697 698 /* If there is a valid interrupt line assigned then set up interrupts */ 699 if ((di->pcii.u.h0.interrupt_pin == 0x00) || 700 (di->pcii.u.h0.interrupt_line == 0xff) || /* no IRQ assigned */ 701 (di->pcii.u.h0.interrupt_line <= 0x02)) /* system IRQ assigned */ 702 { 703 /* we are aborting! */ 704 /* Note: the R4 graphics driver kit lacks this statement!! */ 705 result = B_ERROR; 706 /* interrupt does not exist so exit without installing our handler */ 707 goto delete_the_sem; 708 } 709 else 710 { 711 /* otherwise install our interrupt handler */ 712 result = install_io_interrupt_handler(di->pcii.u.h0.interrupt_line, eng_interrupt, (void *)di, 0); 713 /* bail if we couldn't install the handler */ 714 if (result != B_OK) goto delete_the_sem; 715 } 716 717 mark_as_open: 718 /* mark the device open */ 719 di->is_open++; 720 721 /* send the cookie to the opener */ 722 *cookie = di; 723 724 goto done; 725 726 727 delete_the_sem: 728 delete_sem(si->vblank); 729 730 unmap: 731 unmap_device(di); 732 733 free_shared: 734 /* clean up our shared area */ 735 delete_area(di->shared_area); 736 di->shared_area = -1; 737 di->si = NULL; 738 739 done: 740 /* end of critical section */ 741 RELEASE_BEN(pd->kernel); 742 743 /* all done, return the status */ 744 return result; 745 } 746 747 /* ---------- 748 read_hook - does nothing, gracefully 749 ----- */ 750 static status_t 751 read_hook (void* dev, off_t pos, void* buf, size_t* len) 752 { 753 *len = 0; 754 return B_NOT_ALLOWED; 755 } 756 757 /* ---------- 758 write_hook - does nothing, gracefully 759 ----- */ 760 static status_t 761 write_hook (void* dev, off_t pos, const void* buf, size_t* len) 762 { 763 *len = 0; 764 return B_NOT_ALLOWED; 765 } 766 767 /* ---------- 768 close_hook - does nothing, gracefully 769 ----- */ 770 static status_t 771 close_hook (void* dev) 772 { 773 /* we don't do anything on close: there might be dup'd fd */ 774 return B_NO_ERROR; 775 } 776 777 /* ----------- 778 free_hook - close down the device 779 ----------- */ 780 static status_t 781 free_hook (void* dev) { 782 device_info *di = (device_info *)dev; 783 shared_info *si = di->si; 784 vuint32 *regs = di->regs; 785 786 /* lock the driver */ 787 AQUIRE_BEN(pd->kernel); 788 789 /* if opened multiple times, decrement the open count and exit */ 790 if (di->is_open > 1) 791 goto unlock_and_exit; 792 793 /* disable and clear any pending interrupts */ 794 disable_vbi(regs); 795 796 /* remove interrupt handler */ 797 remove_io_interrupt_handler(di->pcii.u.h0.interrupt_line, eng_interrupt, di); 798 799 /* delete the semaphores, ignoring any errors ('cause the owning team may have died on us) */ 800 delete_sem(si->vblank); 801 si->vblank = -1; 802 803 /* free regs and framebuffer areas */ 804 unmap_device(di); 805 806 /* clean up our shared area */ 807 delete_area(di->shared_area); 808 di->shared_area = -1; 809 di->si = NULL; 810 811 unlock_and_exit: 812 /* mark the device available */ 813 di->is_open--; 814 /* unlock the driver */ 815 RELEASE_BEN(pd->kernel); 816 /* all done */ 817 return B_OK; 818 } 819 820 /* ----------- 821 control_hook - where the real work is done 822 ----------- */ 823 static status_t 824 control_hook (void* dev, uint32 msg, void *buf, size_t len) { 825 device_info *di = (device_info *)dev; 826 status_t result = B_DEV_INVALID_IOCTL; 827 uint32 tmpUlong; 828 829 switch (msg) { 830 /* the only PUBLIC ioctl */ 831 case B_GET_ACCELERANT_SIGNATURE: { 832 char *sig = (char *)buf; 833 strcpy(sig, current_settings.accelerant); 834 result = B_OK; 835 } break; 836 837 /* PRIVATE ioctl from here on */ 838 case ENG_GET_PRIVATE_DATA: { 839 eng_get_private_data *gpd = (eng_get_private_data *)buf; 840 if (gpd->magic == VIA_PRIVATE_DATA_MAGIC) { 841 gpd->shared_info_area = di->shared_area; 842 result = B_OK; 843 } 844 } break; 845 case ENG_GET_PCI: { 846 eng_get_set_pci *gsp = (eng_get_set_pci *)buf; 847 if (gsp->magic == VIA_PRIVATE_DATA_MAGIC) { 848 pci_info *pcii = &(di->pcii); 849 gsp->value = get_pci(gsp->offset, gsp->size); 850 result = B_OK; 851 } 852 } break; 853 case ENG_SET_PCI: { 854 eng_get_set_pci *gsp = (eng_get_set_pci *)buf; 855 if (gsp->magic == VIA_PRIVATE_DATA_MAGIC) { 856 pci_info *pcii = &(di->pcii); 857 set_pci(gsp->offset, gsp->size, gsp->value); 858 result = B_OK; 859 } 860 } break; 861 case ENG_DEVICE_NAME: { // apsed 862 eng_device_name *dn = (eng_device_name *)buf; 863 if (dn->magic == VIA_PRIVATE_DATA_MAGIC) { 864 strcpy(dn->name, di->name); 865 result = B_OK; 866 } 867 } break; 868 case ENG_RUN_INTERRUPTS: { 869 eng_set_bool_state *ri = (eng_set_bool_state *)buf; 870 if (ri->magic == VIA_PRIVATE_DATA_MAGIC) { 871 vuint32 *regs = di->regs; 872 if (ri->do_it) { 873 enable_vbi(regs); 874 } else { 875 disable_vbi(regs); 876 } 877 result = B_OK; 878 } 879 } break; 880 case ENG_GET_NTH_AGP_INFO: { 881 eng_nth_agp_info *nai = (eng_nth_agp_info *)buf; 882 if (nai->magic == VIA_PRIVATE_DATA_MAGIC) { 883 nai->exist = false; 884 nai->agp_bus = false; 885 if (agp_bus) { 886 nai->agp_bus = true; 887 if ((*agp_bus->get_nth_agp_info)(nai->index, &(nai->agpi)) == B_NO_ERROR) { 888 nai->exist = true; 889 } 890 } 891 result = B_OK; 892 } 893 } break; 894 case ENG_ENABLE_AGP: { 895 eng_cmd_agp *nca = (eng_cmd_agp *)buf; 896 if (nca->magic == VIA_PRIVATE_DATA_MAGIC) { 897 if (agp_bus) { 898 nca->agp_bus = true; 899 (*agp_bus->enable_agp)(&(nca->cmd)); 900 } else { 901 nca->agp_bus = false; 902 nca->cmd = 0; 903 } 904 result = B_OK; 905 } 906 } break; 907 case ENG_ISA_OUT: { 908 eng_in_out_isa *io_isa = (eng_in_out_isa *)buf; 909 if (io_isa->magic == VIA_PRIVATE_DATA_MAGIC) { 910 pci_info *pcii = &(di->pcii); 911 912 /* lock the driver: 913 * no other graphics card may have ISA I/O enabled when we enter */ 914 AQUIRE_BEN(pd->kernel); 915 916 /* enable ISA I/O access */ 917 tmpUlong = get_pci(PCI_command, 2); 918 tmpUlong |= PCI_command_io; 919 set_pci(PCI_command, 2, tmpUlong); 920 921 if (io_isa->size == 1) 922 isa_bus->write_io_8(io_isa->adress, (uint8)io_isa->data); 923 else 924 isa_bus->write_io_16(io_isa->adress, io_isa->data); 925 result = B_OK; 926 927 /* disable ISA I/O access */ 928 tmpUlong = get_pci(PCI_command, 2); 929 tmpUlong &= ~PCI_command_io; 930 set_pci(PCI_command, 2, tmpUlong); 931 932 /* end of critical section */ 933 RELEASE_BEN(pd->kernel); 934 } 935 } break; 936 case ENG_ISA_IN: { 937 eng_in_out_isa *io_isa = (eng_in_out_isa *)buf; 938 if (io_isa->magic == VIA_PRIVATE_DATA_MAGIC) { 939 pci_info *pcii = &(di->pcii); 940 941 /* lock the driver: 942 * no other graphics card may have ISA I/O enabled when we enter */ 943 AQUIRE_BEN(pd->kernel); 944 945 /* enable ISA I/O access */ 946 tmpUlong = get_pci(PCI_command, 2); 947 tmpUlong |= PCI_command_io; 948 set_pci(PCI_command, 2, tmpUlong); 949 950 if (io_isa->size == 1) 951 io_isa->data = isa_bus->read_io_8(io_isa->adress); 952 else 953 io_isa->data = isa_bus->read_io_16(io_isa->adress); 954 result = B_OK; 955 956 /* disable ISA I/O access */ 957 tmpUlong = get_pci(PCI_command, 2); 958 tmpUlong &= ~PCI_command_io; 959 set_pci(PCI_command, 2, tmpUlong); 960 961 /* end of critical section */ 962 RELEASE_BEN(pd->kernel); 963 } 964 } break; 965 } 966 return result; 967 } 968