tdfx_pci.c revision 215334
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: head/sys/dev/tdfx/tdfx_pci.c 215334 2010-11-15 05:25:51Z dougb $");
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
78static devclass_t tdfx_devclass;
79
80
81static int tdfx_count = 0;
82
83
84/* Set up the boot probe/attach routines */
85static 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
93MALLOC_DEFINE(M_TDFX,"tdfx_driver","3DFX Graphics[/2D]/3D Accelerator(s)");
94
95/* Char. Dev. file operations structure */
96static 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
106static int
107tdfx_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
136static int
137tdfx_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.
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	tdfx_info->devt->si_drv1 = tdfx_info;
259
260	return 0;
261}
262
263static int
264tdfx_detach(device_t dev) {
265	struct tdfx_softc* tdfx_info;
266	int retval;
267	tdfx_info = device_get_softc(dev);
268
269	/* Delete allocated resource, of course */
270	bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid,
271			tdfx_info->memrange);
272
273	/* Release extended Voodoo3/Banshee resources */
274	if(pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE ||
275			pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) {
276		if(tdfx_info->memrange2 != NULL)
277			bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid2,
278				tdfx_info->memrange);
279	/*	if(tdfx_info->piorange != NULL)
280			bus_release_resource(dev, SYS_RES_IOPORT, tdfx_info->piorid,
281				tdfx_info->piorange);*/
282	}
283
284	/* Though it is safe to leave the WRCOMB support since the
285		mem driver checks for it, we should remove it in order
286		to free an MTRR for another device */
287	retval = tdfx_clrmtrr(dev);
288#ifdef DEBUG
289	if(retval != 0)
290		printf("tdfx: For some reason, I couldn't clear the mtrr\n");
291#endif
292	/* Remove device entry when it can no longer be accessed */
293   destroy_dev(tdfx_info->devt);
294	return(0);
295}
296
297static int
298tdfx_shutdown(device_t dev) {
299#ifdef DEBUG
300	device_printf(dev, "tdfx: Device Shutdown\n");
301#endif
302	return 0;
303}
304
305static int
306tdfx_clrmtrr(device_t dev) {
307	/* This function removes the MTRR set by the attach call, so it can be used
308	 * in the future by other drivers.
309	 */
310	int retval, act;
311	struct tdfx_softc *tdfx_info = device_get_softc(dev);
312
313	act = MEMRANGE_SET_REMOVE;
314	retval = mem_range_attr_set(&tdfx_info->mrdesc, &act);
315	return retval;
316}
317
318static int
319tdfx_setmtrr(device_t dev) {
320	/*
321	 * This is the MTRR setting function for the 3dfx card. It is called from
322	 * tdfx_attach. If we can't set the MTRR properly, it's not the end of the
323	 * world. We can still continue, just with slightly (very slightly) degraded
324	 * performance.
325	 */
326	int retval = 0, act;
327	struct tdfx_softc *tdfx_info = device_get_softc(dev);
328
329	/* The older Voodoo cards have a shorter memrange than the newer ones */
330	if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1) || (pci_get_devid(dev) ==
331			PCI_DEVICE_3DFX_VOODOO2)) {
332		tdfx_info->mrdesc.mr_len = 0x400000;
333
334		/* The memory descriptor is described as the top 15 bits of the real
335			address */
336		tdfx_info->mrdesc.mr_base = tdfx_info->addr0 & 0xfffe0000;
337	}
338	else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) ||
339			(pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE)) {
340		tdfx_info->mrdesc.mr_len = 0x1000000;
341		/* The Voodoo3 and Banshee LFB is the second memory address */
342		/* The memory descriptor is described as the top 15 bits of the real
343			address */
344		tdfx_info->mrdesc.mr_base = tdfx_info->addr1 & 0xfffe0000;
345	}
346	else
347		 return 0;
348	/*
349    *	The Alliance Pro Motion AT3D was not mentioned in the linux
350	 * driver as far as MTRR support goes, so I just won't put the
351	 * code in here for it. This is where it should go, though.
352	 */
353
354	/* Firstly, try to set write combining */
355	tdfx_info->mrdesc.mr_flags = MDF_WRITECOMBINE;
356	bcopy("tdfx", &tdfx_info->mrdesc.mr_owner, 4);
357	act = MEMRANGE_SET_UPDATE;
358	retval = mem_range_attr_set(&tdfx_info->mrdesc, &act);
359
360	if(retval == 0) {
361#ifdef DEBUG
362		device_printf(dev, "MTRR Set Correctly for tdfx\n");
363#endif
364	} else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO2) ||
365		(pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1)) {
366		/* if, for some reason we can't set the WRCOMB range with the V1/V2, we
367		 * can still possibly use the UNCACHEABLE region for it instead, and help
368		 * out in a small way */
369		tdfx_info->mrdesc.mr_flags = MDF_UNCACHEABLE;
370		/* This length of 1000h was taken from the linux device driver... */
371		tdfx_info->mrdesc.mr_len = 0x1000;
372
373		/*
374		 * If, for some reason, we can't set the MTRR (N/A?) we may still continue
375		 */
376#ifdef DEBUG
377		device_printf(dev, "MTRR Set Type Uncacheable %x\n",
378		    (u_int32_t)tdfx_info->mrdesc.mr_base);
379#endif
380	}
381#ifdef DEBUG
382	else {
383		device_printf(dev, "Couldn't Set MTRR\n");
384		return 0;
385	}
386#endif
387	return 0;
388}
389
390static int
391tdfx_open(struct cdev *dev, int flags, int fmt, struct thread *td)
392{
393	/*
394	 *	The open cdev method handles open(2) calls to /dev/3dfx[n]
395	 * We can pretty much allow any opening of the device.
396	 */
397	struct tdfx_softc *tdfx_info = dev->si_drv1;
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
407static int
408tdfx_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 = dev->si_drv1;
415	if(tdfx_info->busy == 0) return EBADF;
416	tdfx_info->busy = 0;
417#ifdef	DEBUG
418	printf("Closed by #%d\n", td->td_proc->p_pid);
419#endif
420	return 0;
421}
422
423static int
424tdfx_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
425    int nprot, vm_memattr_t *memattr)
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 = dev->si_drv1; */
438	/************************/
439
440	struct tdfx_softc* tdfx_info[2];
441
442	tdfx_info[0] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 0);
443
444	/* If, for some reason, its not configured, we bail out */
445	if(tdfx_info[0] == NULL) {
446#ifdef	DEBUG
447	   printf("tdfx: tdfx_info (softc) is NULL\n");
448#endif
449	   return -1;
450	}
451
452	/* We must stay within the bound of our address space */
453	if((offset & 0xff000000) == tdfx_info[0]->addr0) {
454		offset &= 0xffffff;
455		*paddr = rman_get_start(tdfx_info[0]->memrange) + offset;
456		return 0;
457	}
458
459	if(tdfx_count > 1) {
460		tdfx_info[1] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 1);
461		if((offset & 0xff000000) == tdfx_info[1]->addr0) {
462			offset &= 0xffffff;
463			*paddr = rman_get_start(tdfx_info[1]->memrange) +
464			    offset;
465			return 0;
466		}
467	}
468
469	/* See if the Banshee/V3 LFB is being requested */
470	/*if(tdfx_info->memrange2 != NULL && (offset & 0xff000000) ==
471			tdfx_info->addr1) {
472	  	offset &= 0xffffff;
473		return atop(rman_get_start(tdfx_info[1]->memrange2) + offset);
474	}*/ /* VoodooNG code */
475
476	/* The ret call */
477	/* atop -> address to page
478	 * rman_get_start, get the (struct resource*)->r_start member,
479	 * the mapping base address.
480	 */
481	return -1;
482}
483
484static int
485tdfx_query_boards(void) {
486	/*
487    *	This returns the number of installed tdfx cards, we have been keeping
488	 * count, look at tdfx_attach
489	 */
490	return tdfx_count;
491}
492
493static int
494tdfx_query_fetch(u_int cmd, struct tdfx_pio_data *piod)
495{
496	/* XXX Comment this later, after careful inspection and spring cleaning :) */
497	/* Various return values 8bit-32bit */
498	u_int8_t  ret_byte;
499	u_int16_t ret_word;
500	u_int32_t ret_dword;
501	struct tdfx_softc* tdfx_info = NULL;
502
503	/* This one depend on the tdfx_* structs being properly initialized */
504
505	/*piod->device &= 0xf;*/
506	if((piod == NULL) ||(tdfx_count <= piod->device) ||
507			(piod->device < 0)) {
508#ifdef DEBUG
509		printf("tdfx: Bad device or internal struct in tdfx_query_fetch\n");
510#endif
511		return -EINVAL;
512	}
513
514	tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
515			piod->device);
516
517	if(tdfx_info == NULL) return -ENXIO;
518
519	/* We must restrict the size reads from the port, since to high or low of a
520	 * size witll result in wrong data being passed, and that's bad */
521	/* A few of these were pulled during the attach phase */
522	switch(piod->port) {
523		case PCI_VENDOR_ID_FREEBSD:
524			if(piod->size != 2) return -EINVAL;
525			copyout(&tdfx_info->vendor, piod->value, piod->size);
526			return 0;
527		case PCI_DEVICE_ID_FREEBSD:
528			if(piod->size != 2) return -EINVAL;
529			copyout(&tdfx_info->type, piod->value, piod->size);
530			return 0;
531		case PCI_BASE_ADDRESS_0_FREEBSD:
532			if(piod->size != 4) return -EINVAL;
533			copyout(&tdfx_info->addr0, piod->value, piod->size);
534			return 0;
535		case PCI_BASE_ADDRESS_1_FREEBSD:
536			if(piod->size != 4) return -EINVAL;
537			copyout(&tdfx_info->addr1, piod->value, piod->size);
538			return 0;
539		case PCI_PRIBUS_FREEBSD:
540			if(piod->size != 1) return -EINVAL;
541			break;
542		case PCI_IOBASE_0_FREEBSD:
543			if(piod->size != 2) return -EINVAL;
544			break;
545		case PCI_IOLIMIT_0_FREEBSD:
546			if(piod->size != 2) return -EINVAL;
547			break;
548		case SST1_PCI_SPECIAL1_FREEBSD:
549			if(piod->size != 4) return -EINVAL;
550			break;
551		case PCI_REVISION_ID_FREEBSD:
552			if(piod->size != 1) return -EINVAL;
553			break;
554		case SST1_PCI_SPECIAL4_FREEBSD:
555			if(piod->size != 4) return -EINVAL;
556			break;
557		default:
558			return -EINVAL;
559	}
560
561
562	/* Read the value and return */
563	switch(piod->size) {
564		case 1:
565			ret_byte = pci_read_config(tdfx_info[piod->device].dev,
566					piod->port, 1);
567			copyout(&ret_byte, piod->value, 1);
568			break;
569		case 2:
570			ret_word = pci_read_config(tdfx_info[piod->device].dev,
571					piod->port, 2);
572			copyout(&ret_word, piod->value, 2);
573			break;
574		case 4:
575			ret_dword = pci_read_config(tdfx_info[piod->device].dev,
576					piod->port, 4);
577			copyout(&ret_dword, piod->value, 4);
578			break;
579		default:
580			return -EINVAL;
581	}
582	return 0;
583}
584
585static int
586tdfx_query_update(u_int cmd, struct tdfx_pio_data *piod)
587{
588	/* XXX Comment this later, after careful inspection and spring cleaning :) */
589	/* Return vals */
590	u_int8_t  ret_byte;
591	u_int16_t ret_word;
592	u_int32_t ret_dword;
593
594	/* Port vals, mask */
595	u_int32_t retval, preval, mask;
596	struct tdfx_softc* tdfx_info = NULL;
597
598
599	if((piod == NULL) || (piod->device >= (tdfx_count &
600					0xf))) {
601#ifdef DEBUG
602		printf("tdfx: Bad struct or device in tdfx_query_update\n");
603#endif
604		return -EINVAL;
605	}
606
607	tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
608			piod->device);
609	if(tdfx_info == NULL) return -ENXIO;
610	/* Code below this line in the fuction was taken from the
611	 * Linux driver and converted for freebsd. */
612
613	/* Check the size for all the ports, to make sure stuff doesn't get messed up
614	 * by poorly written clients */
615
616	switch(piod->port) {
617		case PCI_COMMAND_FREEBSD:
618			if(piod->size != 2) return -EINVAL;
619			break;
620		case SST1_PCI_SPECIAL1_FREEBSD:
621			if(piod->size != 4) return -EINVAL;
622			break;
623		case SST1_PCI_SPECIAL2_FREEBSD:
624			if(piod->size != 4) return -EINVAL;
625			break;
626		case SST1_PCI_SPECIAL3_FREEBSD:
627			if(piod->size != 4) return -EINVAL;
628			break;
629		case SST1_PCI_SPECIAL4_FREEBSD:
630			if(piod->size != 4) return -EINVAL;
631			break;
632		default:
633			return -EINVAL;
634	}
635	/* Read the current value */
636	retval = pci_read_config(tdfx_info->dev, piod->port & ~3, 4);
637
638	/* These set up a mask to use, since apparently they wanted to write 4 bytes
639	 * at once to the ports */
640	switch (piod->size) {
641		case 1:
642			copyin(piod->value, &ret_byte, 1);
643			preval = ret_byte << (8 * (piod->port & 0x3));
644			mask = 0xff << (8 * (piod->port & 0x3));
645			break;
646		case 2:
647			copyin(piod->value, &ret_word, 2);
648			preval = ret_word << (8 * (piod->port & 0x3));
649			mask = 0xffff << (8 * (piod->port & 0x3));
650			break;
651		case 4:
652			copyin(piod->value, &ret_dword, 4);
653			preval = ret_dword;
654			mask = ~0;
655			break;
656		default:
657			return -EINVAL;
658	}
659	/* Finally, combine the values and write it to the port */
660	retval = (retval & ~mask) | preval;
661	pci_write_config(tdfx_info->dev, piod->port & ~3, retval, 4);
662
663	return 0;
664}
665
666/* For both of these, I added a variable named workport of type u_int so
667 * that I could eliminate the warning about my data type size. The
668 * applications expect the port to be of type short, so I needed to change
669 * this within the function */
670static int
671tdfx_do_pio_rd(struct tdfx_pio_data *piod)
672{
673	/* Return val */
674	u_int8_t  ret_byte;
675	u_int 	 workport;
676	struct tdfx_softc *tdfx_info =
677		(struct tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device);
678
679	/* Restricts the access of ports other than those we use */
680	if(((piod->port != VGA_INPUT_STATUS_1C) || (piod->port != SC_INDEX) ||
681		(piod->port != SC_DATA) || (piod->port != VGA_MISC_OUTPUT_READ)) &&
682		(piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max))
683		return -EPERM;
684
685	/* All VGA STATUS REGS are byte registers, size should never be > 1 */
686	if(piod->size != 1) {
687		return -EINVAL;
688	}
689
690	/* Write the data to the intended port */
691	workport = piod->port;
692	ret_byte = inb(workport);
693	copyout(&ret_byte, piod->value, sizeof(u_int8_t));
694	return 0;
695}
696
697static int
698tdfx_do_pio_wt(struct tdfx_pio_data *piod)
699{
700	/* return val */
701	u_int8_t  ret_byte;
702	u_int		 workport;
703	struct tdfx_softc *tdfx_info = (struct
704			tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device);
705	/* Replace old switch w/ massive if(...) */
706	/* Restricts the access of ports other than those we use */
707	if(((piod->port != SC_INDEX) && (piod->port != SC_DATA) &&
708		(piod->port != VGA_MISC_OUTPUT_READ)) /* Can't write VGA_ST_1C */ &&
709		(piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max))
710		return -EPERM;
711
712	/* All VGA STATUS REGS are byte registers, size should never be > 1 */
713	if(piod->size != 1) {
714		return -EINVAL;
715	}
716
717	/* Write the data to the intended port */
718	copyin(piod->value, &ret_byte, sizeof(u_int8_t));
719	workport = piod->port;
720	outb(workport, ret_byte);
721	return 0;
722}
723
724static int
725tdfx_do_query(u_int cmd, struct tdfx_pio_data *piod)
726{
727	/* There are three sub-commands to the query 0x33 */
728	switch(_IOC_NR(cmd)) {
729		case 2:
730			return tdfx_query_boards();
731			break;
732		case 3:
733			return tdfx_query_fetch(cmd, piod);
734			break;
735		case 4:
736			return tdfx_query_update(cmd, piod);
737			break;
738		default:
739			/* In case we are thrown a bogus sub-command! */
740#ifdef DEBUG
741			printf("Bad Sub-cmd: 0x%x\n", _IOC_NR(cmd));
742#endif
743			return -EINVAL;
744	}
745}
746
747static int
748tdfx_do_pio(u_int cmd, struct tdfx_pio_data *piod)
749{
750	/* Two types of PIO, INPUT and OUTPUT, as the name suggests */
751	switch(_IOC_DIR(cmd)) {
752		case IOCV_OUT:
753			return tdfx_do_pio_rd(piod);
754			break;
755		case IOCV_IN:
756			return tdfx_do_pio_wt(piod);
757			break;
758		default:
759			return -EINVAL;
760	}
761}
762
763/* Calls to ioctl(2) eventually end up here. Unhandled ioctls return an ENXIO,
764 * normally, you would read in the data pointed to by data, then write your
765 * output to it. The ioctl *should* normally return zero if everything is
766 * alright, but 3dfx didn't make it that way...
767 *
768 * For all of the ioctl code, in the event of a real error,
769 * we return -Exxxx rather than simply Exxxx. The reason for this
770 * is that the ioctls actually RET information back to the program
771 * sometimes, rather than filling it in the passed structure. We
772 * want to distinguish errors from useful data, and maintain compatibility.
773 *
774 * There is this portion of the proc struct called p_retval[], we can store a
775 * return value in td->td_retval[0] and place the return value if it is positive
776 * in there, then we can return 0 (good). If the return value is negative, we
777 * can return -retval and the error should be properly handled.
778 */
779static int
780tdfx_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
781{
782	int retval = 0;
783	struct tdfx_pio_data *piod = (struct tdfx_pio_data*)data;
784#ifdef	DEBUG
785	printf("IOCTL'd by #%d, cmd: 0x%x, data: %p\n", td->td_proc->p_pid, (u_int32_t)cmd,
786			piod);
787#endif
788	switch(_IOC_TYPE(cmd)) {
789		/* Return the real error if negative, or simply stick the valid return
790		 * in td->td_retval */
791	case 0x33:
792			/* The '3'(0x33) type IOCTL is for querying the installed cards */
793			if((retval = tdfx_do_query(cmd, piod)) > 0) td->td_retval[0] = retval;
794			else return -retval;
795			break;
796		case 0:
797			/* The 0 type IOCTL is for programmed I/O methods */
798			if((tdfx_do_pio(cmd, piod)) > 0) td->td_retval[0] = retval;
799			else return -retval;
800			break;
801		default:
802			/* Technically, we won't reach this from linux emu, but when glide
803			 * finally gets ported, watch out! */
804#ifdef DEBUG
805			printf("Bad IOCTL from #%d\n", td->td_proc->p_pid);
806#endif
807			return ENXIO;
808	}
809
810	return 0;
811}
812
813/* This is the device driver struct. This is sent to the driver subsystem to
814 * register the method structure and the info strcut space for this particular
815 * instance of the driver.
816 */
817static driver_t tdfx_driver = {
818	"tdfx",
819	tdfx_methods,
820	sizeof(struct tdfx_softc),
821};
822
823/* Tell Mr. Kernel about us! */
824DRIVER_MODULE(tdfx, pci, tdfx_driver, tdfx_devclass, 0, 0);
825MODULE_DEPEND(tdfx, mem, 1, 1, 1);
826MODULE_VERSION(tdfx, 1);
827