1 /*- 2 * Copyright (c) 2000-2001 by Coleman Kane <cokane@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Gardner Buchanan. 16 * 4. The name of Gardner Buchanan may not be used to endorse or promote 17 * products derived from this software without specific prior written 18 * permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 /* 3dfx driver for FreeBSD 4.x - Finished 11 May 2000, 12:25AM ET 36 * 37 * Copyright (C) 2000-2001, by Coleman Kane <cokane@FreeBSD.org>, 38 * based upon the 3dfx driver written for linux, by Daryll Straus, Jon Taylor, 39 * and Jens Axboe, located at http://linux.3dfx.com. 40 */ 41 42 #include <sys/param.h> 43 44 #include <sys/bus.h> 45 #include <sys/cdefs.h> 46 #include <sys/conf.h> 47 #include <sys/fcntl.h> 48 #include <sys/file.h> 49 #include <sys/filedesc.h> 50 #include <sys/filio.h> 51 #include <sys/ioccom.h> 52 #include <sys/kernel.h> 53 #include <sys/module.h> 54 #include <sys/malloc.h> 55 #include <sys/mman.h> 56 #include <sys/signalvar.h> 57 #include <sys/systm.h> 58 #include <sys/uio.h> 59 60 #include <dev/pci/pcivar.h> 61 #include <dev/pci/pcireg.h> 62 63 #include <vm/vm.h> 64 #include <vm/vm_kern.h> 65 #include <vm/pmap.h> 66 #include <vm/vm_extern.h> 67 68 /* rman.h depends on machine/bus.h */ 69 #include <machine/resource.h> 70 #include <machine/bus.h> 71 #include <sys/rman.h> 72 73 #include <dev/tdfx/tdfx_io.h> 74 #include <dev/tdfx/tdfx_vars.h> 75 #include <dev/tdfx/tdfx_pci.h> 76 77 78 static devclass_t tdfx_devclass; 79 80 81 static int tdfx_count = 0; 82 83 84 /* Set up the boot probe/attach routines */ 85 static device_method_t tdfx_methods[] = { 86 DEVMETHOD(device_probe, tdfx_probe), 87 DEVMETHOD(device_attach, tdfx_attach), 88 DEVMETHOD(device_detach, tdfx_detach), 89 DEVMETHOD(device_shutdown, tdfx_shutdown), 90 { 0, 0 } 91 }; 92 93 MALLOC_DEFINE(M_TDFX,"tdfx_driver","3DFX Graphics[/2D]/3D Accelerator(s)"); 94 95 /* Char. Dev. file operations structure */ 96 static struct cdevsw tdfx_cdev = { 97 .d_version = D_VERSION, 98 .d_flags = D_NEEDGIANT, 99 .d_open = tdfx_open, 100 .d_close = tdfx_close, 101 .d_ioctl = tdfx_ioctl, 102 .d_mmap = tdfx_mmap, 103 .d_name = "tdfx", 104 }; 105 106 static int 107 tdfx_probe(device_t dev) 108 { 109 /* 110 * probe routine called on kernel boot to register supported devices. We get 111 * a device structure to work with, and we can test the VENDOR/DEVICE IDs to 112 * see if this PCI device is one that we support. Return BUS_PRROBE_DEFAULT 113 * if yes, ENXIO if not. 114 */ 115 switch(pci_get_devid(dev)) { 116 case PCI_DEVICE_ALLIANCE_AT3D: 117 device_set_desc(dev, "ProMotion At3D 3D Accelerator"); 118 return BUS_PROBE_DEFAULT; 119 case PCI_DEVICE_3DFX_VOODOO2: 120 device_set_desc(dev, "3DFX Voodoo II 3D Accelerator"); 121 return BUS_PROBE_DEFAULT; 122 /*case PCI_DEVICE_3DFX_BANSHEE: 123 device_set_desc(dev, "3DFX Voodoo Banshee 2D/3D Graphics Accelerator"); 124 return BUS_PROBE_DEFAULT; 125 case PCI_DEVICE_3DFX_VOODOO3: 126 device_set_desc(dev, "3DFX Voodoo3 2D/3D Graphics Accelerator"); 127 return BUS_PROBE_DEFAULT;*/ 128 case PCI_DEVICE_3DFX_VOODOO1: 129 device_set_desc(dev, "3DFX Voodoo Graphics 3D Accelerator"); 130 return BUS_PROBE_DEFAULT; 131 }; 132 133 return ENXIO; 134 } 135 136 static int 137 tdfx_attach(device_t dev) { 138 /* 139 * The attach routine is called after the probe routine successfully says it 140 * supports a given card. We now proceed to initialize this card for use with 141 * the system. I want to map the device memory for userland allocation and 142 * fill an information structure with information on this card. I'd also like 143 * to set Write Combining with the MTRR code so that we can hopefully speed 144 * up memory writes. The last thing is to register the character device 145 * interface to the card, so we can open it from /dev/3dfxN, where N is a 146 * small, whole number. 147 */ 148 struct tdfx_softc *tdfx_info; 149 u_long val; 150 /* rid value tells bus_alloc_resource where to find the addresses of ports or 151 * of memory ranges in the PCI config space*/ 152 int rid = PCIR_BAR(0); 153 154 /* Increment the card counter (for the ioctl code) */ 155 tdfx_count++; 156 157 /* Enable MemMap on Voodoo */ 158 val = pci_read_config(dev, PCIR_COMMAND, 2); 159 val |= (PCIM_CMD_MEMEN); 160 pci_write_config(dev, PCIR_COMMAND, val, 2); 161 val = pci_read_config(dev, PCIR_COMMAND, 2); 162 163 /* Fill the soft config struct with info about this device*/ 164 tdfx_info = device_get_softc(dev); 165 tdfx_info->dev = dev; 166 tdfx_info->vendor = pci_get_vendor(dev); 167 tdfx_info->type = pci_get_devid(dev) >> 16; 168 tdfx_info->bus = pci_get_bus(dev); 169 tdfx_info->dv = pci_get_slot(dev); 170 tdfx_info->curFile = NULL; 171 172 /* 173 * Get the Memory Location from the PCI Config, mask out lower word, since 174 * the config space register is only one word long (this is nicer than a 175 * bitshift). 176 */ 177 tdfx_info->addr0 = (pci_read_config(dev, 0x10, 4) & 0xffff0000); 178 #ifdef DEBUG 179 device_printf(dev, "Base0 @ 0x%x\n", tdfx_info->addr0); 180 #endif 181 /* Notify the VM that we will be mapping some memory later */ 182 tdfx_info->memrange = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 183 &rid, RF_ACTIVE | RF_SHAREABLE); 184 if(tdfx_info->memrange == NULL) { 185 #ifdef DEBUG 186 device_printf(dev, "Error mapping mem, won't be able to use mmap()\n"); 187 #endif 188 tdfx_info->memrid = 0; 189 } 190 else { 191 tdfx_info->memrid = rid; 192 #ifdef DEBUG 193 device_printf(dev, "Mapped to: 0x%x\n", 194 (unsigned int)rman_get_start(tdfx_info->memrange)); 195 #endif 196 } 197 198 /* Setup for Voodoo3 and Banshee, PIO and an extram Memrange */ 199 if(pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3 || 200 pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE) { 201 rid = 0x14; /* 2nd mem map */ 202 tdfx_info->addr1 = (pci_read_config(dev, 0x14, 4) & 0xffff0000); 203 #ifdef DEBUG 204 device_printf(dev, "Base1 @ 0x%x\n", tdfx_info->addr1); 205 #endif 206 tdfx_info->memrange2 = bus_alloc_resource_any(dev, 207 SYS_RES_MEMORY, &rid, RF_ACTIVE | RF_SHAREABLE); 208 if(tdfx_info->memrange2 == NULL) { 209 #ifdef DEBUG 210 device_printf(dev, "Mem1 couldn't be allocated, glide may not work."); 211 #endif 212 tdfx_info->memrid2 = 0; 213 } 214 else { 215 tdfx_info->memrid2 = rid; 216 } 217 /* Now to map the PIO stuff */ 218 rid = PCIR_IOBASE0_2; 219 tdfx_info->pio0 = pci_read_config(dev, 0x2c, 2); 220 tdfx_info->pio0max = pci_read_config(dev, 0x30, 2) + tdfx_info->pio0; 221 tdfx_info->piorange = bus_alloc_resource_any(dev, 222 SYS_RES_IOPORT, &rid, RF_ACTIVE | RF_SHAREABLE); 223 if(tdfx_info->piorange == NULL) { 224 #ifdef DEBUG 225 device_printf(dev, "Couldn't map PIO range."); 226 #endif 227 tdfx_info->piorid = 0; 228 } 229 else { 230 tdfx_info->piorid = rid; 231 } 232 } else { 233 tdfx_info->addr1 = 0; 234 tdfx_info->memrange2 = NULL; 235 tdfx_info->piorange = NULL; 236 } 237 238 /* 239 * Set Writecombining, or at least Uncacheable for the memory region, if we 240 * are able to 241 */ 242 243 if(tdfx_setmtrr(dev) != 0) { 244 #ifdef DEBUG 245 device_printf(dev, "Some weird error setting MTRRs"); 246 #endif 247 return -1; 248 } 249 250 /* 251 * make_dev registers the cdev to access the 3dfx card from /dev 252 * use hex here for the dev num, simply to provide better support if > 10 253 * voodoo cards, for the mad. The user must set the link, or use MAKEDEV. 254 * Why would we want that many voodoo cards anyhow? 255 */ 256 tdfx_info->devt = make_dev(&tdfx_cdev, device_get_unit(dev), 257 UID_ROOT, GID_WHEEL, 0600, "3dfx%x", device_get_unit(dev)); 258 259 return 0; 260 } 261 262 static int 263 tdfx_detach(device_t dev) { 264 struct tdfx_softc* tdfx_info; 265 int retval; 266 tdfx_info = device_get_softc(dev); 267 268 /* Delete allocated resource, of course */ 269 bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid, 270 tdfx_info->memrange); 271 272 /* Release extended Voodoo3/Banshee resources */ 273 if(pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE || 274 pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) { 275 if(tdfx_info->memrange2 != NULL) 276 bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid2, 277 tdfx_info->memrange); 278 /* if(tdfx_info->piorange != NULL) 279 bus_release_resource(dev, SYS_RES_IOPORT, tdfx_info->piorid, 280 tdfx_info->piorange);*/ 281 } 282 283 /* Though it is safe to leave the WRCOMB support since the 284 mem driver checks for it, we should remove it in order 285 to free an MTRR for another device */ 286 retval = tdfx_clrmtrr(dev); 287 #ifdef DEBUG 288 if(retval != 0) 289 printf("tdfx: For some reason, I couldn't clear the mtrr\n"); 290 #endif 291 /* Remove device entry when it can no longer be accessed */ 292 destroy_dev(tdfx_info->devt); 293 return(0); 294 } 295 296 static int 297 tdfx_shutdown(device_t dev) { 298 #ifdef DEBUG 299 device_printf(dev, "tdfx: Device Shutdown\n"); 300 #endif 301 return 0; 302 } 303 304 static int 305 tdfx_clrmtrr(device_t dev) { 306 /* This function removes the MTRR set by the attach call, so it can be used 307 * in the future by other drivers. 308 */ 309 int retval, act; 310 struct tdfx_softc *tdfx_info = device_get_softc(dev); 311 312 act = MEMRANGE_SET_REMOVE; 313 retval = mem_range_attr_set(&tdfx_info->mrdesc, &act); 314 return retval; 315 } 316 317 static int 318 tdfx_setmtrr(device_t dev) { 319 /* 320 * This is the MTRR setting function for the 3dfx card. It is called from 321 * tdfx_attach. If we can't set the MTRR properly, it's not the end of the 322 * world. We can still continue, just with slightly (very slightly) degraded 323 * performance. 324 */ 325 int retval = 0, act; 326 struct tdfx_softc *tdfx_info = device_get_softc(dev); 327 328 /* The older Voodoo cards have a shorter memrange than the newer ones */ 329 if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1) || (pci_get_devid(dev) == 330 PCI_DEVICE_3DFX_VOODOO2)) { 331 tdfx_info->mrdesc.mr_len = 0x400000; 332 333 /* The memory descriptor is described as the top 15 bits of the real 334 address */ 335 tdfx_info->mrdesc.mr_base = tdfx_info->addr0 & 0xfffe0000; 336 } 337 else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) || 338 (pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE)) { 339 tdfx_info->mrdesc.mr_len = 0x1000000; 340 /* The Voodoo3 and Banshee LFB is the second memory address */ 341 /* The memory descriptor is described as the top 15 bits of the real 342 address */ 343 tdfx_info->mrdesc.mr_base = tdfx_info->addr1 & 0xfffe0000; 344 } 345 else 346 return 0; 347 /* 348 * The Alliance Pro Motion AT3D was not mentioned in the linux 349 * driver as far as MTRR support goes, so I just won't put the 350 * code in here for it. This is where it should go, though. 351 */ 352 353 /* Firstly, try to set write combining */ 354 tdfx_info->mrdesc.mr_flags = MDF_WRITECOMBINE; 355 bcopy("tdfx", &tdfx_info->mrdesc.mr_owner, 4); 356 act = MEMRANGE_SET_UPDATE; 357 retval = mem_range_attr_set(&tdfx_info->mrdesc, &act); 358 359 if(retval == 0) { 360 #ifdef DEBUG 361 device_printf(dev, "MTRR Set Correctly for tdfx\n"); 362 #endif 363 } else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO2) || 364 (pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1)) { 365 /* if, for some reason we can't set the WRCOMB range with the V1/V2, we 366 * can still possibly use the UNCACHEABLE region for it instead, and help 367 * out in a small way */ 368 tdfx_info->mrdesc.mr_flags = MDF_UNCACHEABLE; 369 /* This length of 1000h was taken from the linux device driver... */ 370 tdfx_info->mrdesc.mr_len = 0x1000; 371 372 /* 373 * If, for some reason, we can't set the MTRR (N/A?) we may still continue 374 */ 375 #ifdef DEBUG 376 device_printf(dev, "MTRR Set Type Uncacheable %x\n", 377 (u_int32_t)tdfx_info->mrdesc.mr_base); 378 #endif 379 } 380 #ifdef DEBUG 381 else { 382 device_printf(dev, "Couldn't Set MTRR\n"); 383 return 0; 384 } 385 #endif 386 return 0; 387 } 388 389 static int 390 tdfx_open(struct cdev *dev, int flags, int fmt, struct thread *td) 391 { 392 /* 393 * The open cdev method handles open(2) calls to /dev/3dfx[n] 394 * We can pretty much allow any opening of the device. 395 */ 396 struct tdfx_softc *tdfx_info = devclass_get_softc(tdfx_devclass, 397 UNIT(minor(dev))); 398 if(tdfx_info->busy != 0) return EBUSY; 399 #ifdef DEBUG 400 printf("3dfx: Opened by #%d\n", td->td_proc->p_pid); 401 #endif 402 /* Set the driver as busy */ 403 tdfx_info->busy++; 404 return 0; 405 } 406 407 static int 408 tdfx_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 409 { 410 /* 411 * The close cdev method handles close(2) calls to /dev/3dfx[n] 412 * We'll always want to close the device when it's called. 413 */ 414 struct tdfx_softc *tdfx_info = devclass_get_softc(tdfx_devclass, 415 UNIT(minor(dev))); 416 if(tdfx_info->busy == 0) return EBADF; 417 tdfx_info->busy = 0; 418 #ifdef DEBUG 419 printf("Closed by #%d\n", td->td_proc->p_pid); 420 #endif 421 return 0; 422 } 423 424 static int 425 tdfx_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 426 { 427 /* 428 * mmap(2) is called by a user process to request that an area of memory 429 * associated with this device be mapped for the process to work with. Nprot 430 * holds the protections requested, PROT_READ, PROT_WRITE, or both. 431 */ 432 433 /**** OLD GET CONFIG ****/ 434 /* struct tdfx_softc* tdfx_info; */ 435 436 /* Get the configuration for our card XXX*/ 437 /*tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 438 UNIT(minor(dev)));*/ 439 /************************/ 440 441 struct tdfx_softc* tdfx_info[2]; 442 443 tdfx_info[0] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 0); 444 445 /* If, for some reason, its not configured, we bail out */ 446 if(tdfx_info[0] == NULL) { 447 #ifdef DEBUG 448 printf("tdfx: tdfx_info (softc) is NULL\n"); 449 #endif 450 return -1; 451 } 452 453 /* We must stay within the bound of our address space */ 454 if((offset & 0xff000000) == tdfx_info[0]->addr0) { 455 offset &= 0xffffff; 456 *paddr = rman_get_start(tdfx_info[0]->memrange) + offset; 457 return 0; 458 } 459 460 if(tdfx_count > 1) { 461 tdfx_info[1] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 1); 462 if((offset & 0xff000000) == tdfx_info[1]->addr0) { 463 offset &= 0xffffff; 464 *paddr = rman_get_start(tdfx_info[1]->memrange) + 465 offset; 466 return 0; 467 } 468 } 469 470 /* See if the Banshee/V3 LFB is being requested */ 471 /*if(tdfx_info->memrange2 != NULL && (offset & 0xff000000) == 472 tdfx_info->addr1) { 473 offset &= 0xffffff; 474 return atop(rman_get_start(tdfx_info[1]->memrange2) + offset); 475 }*/ /* VoodooNG code */ 476 477 /* The ret call */ 478 /* atop -> address to page 479 * rman_get_start, get the (struct resource*)->r_start member, 480 * the mapping base address. 481 */ 482 return -1; 483 } 484 485 static int 486 tdfx_query_boards(void) { 487 /* 488 * This returns the number of installed tdfx cards, we have been keeping 489 * count, look at tdfx_attach 490 */ 491 return tdfx_count; 492 } 493 494 static int 495 tdfx_query_fetch(u_int cmd, struct tdfx_pio_data *piod) 496 { 497 /* XXX Comment this later, after careful inspection and spring cleaning :) */ 498 /* Various return values 8bit-32bit */ 499 u_int8_t ret_byte; 500 u_int16_t ret_word; 501 u_int32_t ret_dword; 502 struct tdfx_softc* tdfx_info = NULL; 503 504 /* This one depend on the tdfx_* structs being properly initialized */ 505 506 /*piod->device &= 0xf;*/ 507 if((piod == NULL) ||(tdfx_count <= piod->device) || 508 (piod->device < 0)) { 509 #ifdef DEBUG 510 printf("tdfx: Bad device or internal struct in tdfx_query_fetch\n"); 511 #endif 512 return -EINVAL; 513 } 514 515 tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 516 piod->device); 517 518 if(tdfx_info == NULL) return -ENXIO; 519 520 /* We must restrict the size reads from the port, since to high or low of a 521 * size witll result in wrong data being passed, and that's bad */ 522 /* A few of these were pulled during the attach phase */ 523 switch(piod->port) { 524 case PCI_VENDOR_ID_FREEBSD: 525 if(piod->size != 2) return -EINVAL; 526 copyout(&tdfx_info->vendor, piod->value, piod->size); 527 return 0; 528 case PCI_DEVICE_ID_FREEBSD: 529 if(piod->size != 2) return -EINVAL; 530 copyout(&tdfx_info->type, piod->value, piod->size); 531 return 0; 532 case PCI_BASE_ADDRESS_0_FREEBSD: 533 if(piod->size != 4) return -EINVAL; 534 copyout(&tdfx_info->addr0, piod->value, piod->size); 535 return 0; 536 case PCI_BASE_ADDRESS_1_FREEBSD: 537 if(piod->size != 4) return -EINVAL; 538 copyout(&tdfx_info->addr1, piod->value, piod->size); 539 return 0; 540 case PCI_PRIBUS_FREEBSD: 541 if(piod->size != 1) return -EINVAL; 542 break; 543 case PCI_IOBASE_0_FREEBSD: 544 if(piod->size != 2) return -EINVAL; 545 break; 546 case PCI_IOLIMIT_0_FREEBSD: 547 if(piod->size != 2) return -EINVAL; 548 break; 549 case SST1_PCI_SPECIAL1_FREEBSD: 550 if(piod->size != 4) return -EINVAL; 551 break; 552 case PCI_REVISION_ID_FREEBSD: 553 if(piod->size != 1) return -EINVAL; 554 break; 555 case SST1_PCI_SPECIAL4_FREEBSD: 556 if(piod->size != 4) return -EINVAL; 557 break; 558 default: 559 return -EINVAL; 560 } 561 562 563 /* Read the value and return */ 564 switch(piod->size) { 565 case 1: 566 ret_byte = pci_read_config(tdfx_info[piod->device].dev, 567 piod->port, 1); 568 copyout(&ret_byte, piod->value, 1); 569 break; 570 case 2: 571 ret_word = pci_read_config(tdfx_info[piod->device].dev, 572 piod->port, 2); 573 copyout(&ret_word, piod->value, 2); 574 break; 575 case 4: 576 ret_dword = pci_read_config(tdfx_info[piod->device].dev, 577 piod->port, 4); 578 copyout(&ret_dword, piod->value, 4); 579 break; 580 default: 581 return -EINVAL; 582 } 583 return 0; 584 } 585 586 static int 587 tdfx_query_update(u_int cmd, struct tdfx_pio_data *piod) 588 { 589 /* XXX Comment this later, after careful inspection and spring cleaning :) */ 590 /* Return vals */ 591 u_int8_t ret_byte; 592 u_int16_t ret_word; 593 u_int32_t ret_dword; 594 595 /* Port vals, mask */ 596 u_int32_t retval, preval, mask; 597 struct tdfx_softc* tdfx_info = NULL; 598 599 600 if((piod == NULL) || (piod->device >= (tdfx_count & 601 0xf))) { 602 #ifdef DEBUG 603 printf("tdfx: Bad struct or device in tdfx_query_update\n"); 604 #endif 605 return -EINVAL; 606 } 607 608 tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 609 piod->device); 610 if(tdfx_info == NULL) return -ENXIO; 611 /* Code below this line in the fuction was taken from the 612 * Linux driver and converted for freebsd. */ 613 614 /* Check the size for all the ports, to make sure stuff doesn't get messed up 615 * by poorly written clients */ 616 617 switch(piod->port) { 618 case PCI_COMMAND_FREEBSD: 619 if(piod->size != 2) return -EINVAL; 620 break; 621 case SST1_PCI_SPECIAL1_FREEBSD: 622 if(piod->size != 4) return -EINVAL; 623 break; 624 case SST1_PCI_SPECIAL2_FREEBSD: 625 if(piod->size != 4) return -EINVAL; 626 break; 627 case SST1_PCI_SPECIAL3_FREEBSD: 628 if(piod->size != 4) return -EINVAL; 629 break; 630 case SST1_PCI_SPECIAL4_FREEBSD: 631 if(piod->size != 4) return -EINVAL; 632 break; 633 default: 634 return -EINVAL; 635 } 636 /* Read the current value */ 637 retval = pci_read_config(tdfx_info->dev, piod->port & ~3, 4); 638 639 /* These set up a mask to use, since apparently they wanted to write 4 bytes 640 * at once to the ports */ 641 switch (piod->size) { 642 case 1: 643 copyin(piod->value, &ret_byte, 1); 644 preval = ret_byte << (8 * (piod->port & 0x3)); 645 mask = 0xff << (8 * (piod->port & 0x3)); 646 break; 647 case 2: 648 copyin(piod->value, &ret_word, 2); 649 preval = ret_word << (8 * (piod->port & 0x3)); 650 mask = 0xffff << (8 * (piod->port & 0x3)); 651 break; 652 case 4: 653 copyin(piod->value, &ret_dword, 4); 654 preval = ret_dword; 655 mask = ~0; 656 break; 657 default: 658 return -EINVAL; 659 } 660 /* Finally, combine the values and write it to the port */ 661 retval = (retval & ~mask) | preval; 662 pci_write_config(tdfx_info->dev, piod->port & ~3, retval, 4); 663 664 return 0; 665 } 666 667 /* For both of these, I added a variable named workport of type u_int so 668 * that I could eliminate the warning about my data type size. The 669 * applications expect the port to be of type short, so I needed to change 670 * this within the function */ 671 static int 672 tdfx_do_pio_rd(struct tdfx_pio_data *piod) 673 { 674 /* Return val */ 675 u_int8_t ret_byte; 676 u_int workport; 677 struct tdfx_softc *tdfx_info = 678 (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device); 679 680 /* Restricts the access of ports other than those we use */ 681 if(((piod->port != VGA_INPUT_STATUS_1C) || (piod->port != SC_INDEX) || 682 (piod->port != SC_DATA) || (piod->port != VGA_MISC_OUTPUT_READ)) && 683 (piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max)) 684 return -EPERM; 685 686 /* All VGA STATUS REGS are byte registers, size should never be > 1 */ 687 if(piod->size != 1) { 688 return -EINVAL; 689 } 690 691 /* Write the data to the intended port */ 692 workport = piod->port; 693 ret_byte = inb(workport); 694 copyout(&ret_byte, piod->value, sizeof(u_int8_t)); 695 return 0; 696 } 697 698 static int 699 tdfx_do_pio_wt(struct tdfx_pio_data *piod) 700 { 701 /* return val */ 702 u_int8_t ret_byte; 703 u_int workport; 704 struct tdfx_softc *tdfx_info = (struct 705 tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device); 706 /* Replace old switch w/ massive if(...) */ 707 /* Restricts the access of ports other than those we use */ 708 if(((piod->port != SC_INDEX) && (piod->port != SC_DATA) && 709 (piod->port != VGA_MISC_OUTPUT_READ)) /* Can't write VGA_ST_1C */ && 710 (piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max)) 711 return -EPERM; 712 713 /* All VGA STATUS REGS are byte registers, size should never be > 1 */ 714 if(piod->size != 1) { 715 return -EINVAL; 716 } 717 718 /* Write the data to the intended port */ 719 copyin(piod->value, &ret_byte, sizeof(u_int8_t)); 720 workport = piod->port; 721 outb(workport, ret_byte); 722 return 0; 723 } 724 725 static int 726 tdfx_do_query(u_int cmd, struct tdfx_pio_data *piod) 727 { 728 /* There are three sub-commands to the query 0x33 */ 729 switch(_IOC_NR(cmd)) { 730 case 2: 731 return tdfx_query_boards(); 732 break; 733 case 3: 734 return tdfx_query_fetch(cmd, piod); 735 break; 736 case 4: 737 return tdfx_query_update(cmd, piod); 738 break; 739 default: 740 /* In case we are thrown a bogus sub-command! */ 741 #ifdef DEBUG 742 printf("Bad Sub-cmd: 0x%x\n", _IOC_NR(cmd)); 743 #endif 744 return -EINVAL; 745 } 746 } 747 748 static int 749 tdfx_do_pio(u_int cmd, struct tdfx_pio_data *piod) 750 { 751 /* Two types of PIO, INPUT and OUTPUT, as the name suggests */ 752 switch(_IOC_DIR(cmd)) { 753 case IOCV_OUT: 754 return tdfx_do_pio_rd(piod); 755 break; 756 case IOCV_IN: 757 return tdfx_do_pio_wt(piod); 758 break; 759 default: 760 return -EINVAL; 761 } 762 } 763 764 /* Calls to ioctl(2) eventually end up here. Unhandled ioctls return an ENXIO, 765 * normally, you would read in the data pointed to by data, then write your 766 * output to it. The ioctl *should* normally return zero if everything is 767 * alright, but 3dfx didn't make it that way... 768 * 769 * For all of the ioctl code, in the event of a real error, 770 * we return -Exxxx rather than simply Exxxx. The reason for this 771 * is that the ioctls actually RET information back to the program 772 * sometimes, rather than filling it in the passed structure. We 773 * want to distinguish errors from useful data, and maintain compatibility. 774 * 775 * There is this portion of the proc struct called p_retval[], we can store a 776 * return value in td->td_retval[0] and place the return value if it is positive 777 * in there, then we can return 0 (good). If the return value is negative, we 778 * can return -retval and the error should be properly handled. 779 */ 780 static int 781 tdfx_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 782 { 783 int retval = 0; 784 struct tdfx_pio_data *piod = (struct tdfx_pio_data*)data; 785 #ifdef DEBUG 786 printf("IOCTL'd by #%d, cmd: 0x%x, data: %p\n", td->td_proc->p_pid, (u_int32_t)cmd, 787 piod); 788 #endif 789 switch(_IOC_TYPE(cmd)) { 790 /* Return the real error if negative, or simply stick the valid return 791 * in td->td_retval */ 792 case 0x33: 793 /* The '3'(0x33) type IOCTL is for querying the installed cards */ 794 if((retval = tdfx_do_query(cmd, piod)) > 0) td->td_retval[0] = retval; 795 else return -retval; 796 break; 797 case 0: 798 /* The 0 type IOCTL is for programmed I/O methods */ 799 if((tdfx_do_pio(cmd, piod)) > 0) td->td_retval[0] = retval; 800 else return -retval; 801 break; 802 default: 803 /* Technically, we won't reach this from linux emu, but when glide 804 * finally gets ported, watch out! */ 805 #ifdef DEBUG 806 printf("Bad IOCTL from #%d\n", td->td_proc->p_pid); 807 #endif 808 return ENXIO; 809 } 810 811 return 0; 812 } 813 814 /* This is the device driver struct. This is sent to the driver subsystem to 815 * register the method structure and the info strcut space for this particular 816 * instance of the driver. 817 */ 818 static driver_t tdfx_driver = { 819 "tdfx", 820 tdfx_methods, 821 sizeof(struct tdfx_softc), 822 }; 823 824 /* Tell Mr. Kernel about us! */ 825 DRIVER_MODULE(tdfx, pci, tdfx_driver, tdfx_devclass, 0, 0); 826 MODULE_VERSION(tdfx, 1); 827