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