tdfx_pci.c revision 64085
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 64085 2000-08-01 05:10:29Z 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 = 0x14;	/* 2nd mem map */
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		return atop(rman_get_start(tdfx_info->memrange2) + offset);
480	}
481
482	if((offset >= 0x1000000) || (offset < 0)) {
483#ifdef  DEBUG
484	   printf("tdfx: offset %x out of range\n", offset);
485#endif
486	   return -1;
487	}
488
489	/* atop -> address to page
490	 * rman_get_start, get the (struct resource*)->r_start member,
491	 * the mapping base address.
492	 */
493	return atop(rman_get_start(tdfx_info->memrange) + offset);
494}
495
496static int
497tdfx_query_boards(void) {
498	/*
499    *	This returns the number of installed tdfx cards, we have been keeping
500	 * count, look at tdfx_attach
501	 */
502	return tdfx_count;
503}
504
505static int
506tdfx_query_fetch(u_int cmd, struct tdfx_pio_data *piod)
507{
508	/* XXX Comment this later, after careful inspection and spring cleaning :) */
509	/* Various return values 8bit-32bit */
510	u_int8_t  ret_byte;
511	u_int16_t ret_word;
512	u_int32_t ret_dword;
513	struct tdfx_softc* tdfx_info = NULL;
514
515	/* This one depend on the tdfx_* structs being properly initialized */
516
517	/*piod->device &= 0xf;*/
518	if((piod == NULL) ||(tdfx_count <= piod->device) ||
519			(piod->device < 0)) {
520#ifdef DEBUG
521		printf("tdfx: Bad device or internal struct in tdfx_query_fetch\n");
522#endif
523		return -EINVAL;
524	}
525
526	tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
527			piod->device);
528
529	if(tdfx_info == NULL) return -ENXIO;
530
531	/* We must restrict the size reads from the port, since to high or low of a
532	 * size witll result in wrong data being passed, and that's bad */
533	/* A few of these were pulled during the attach phase */
534	switch(piod->port) {
535		case PCI_VENDOR_ID_FREEBSD:
536			if(piod->size != 2) return -EINVAL;
537			copyout(&tdfx_info->vendor, piod->value, piod->size);
538			return 0;
539		case PCI_DEVICE_ID_FREEBSD:
540			if(piod->size != 2) return -EINVAL;
541			copyout(&tdfx_info->type, piod->value, piod->size);
542			return 0;
543		case PCI_BASE_ADDRESS_0_FREEBSD:
544			if(piod->size != 4) return -EINVAL;
545			copyout(&tdfx_info->addr0, piod->value, piod->size);
546			return 0;
547		case SST1_PCI_SPECIAL1_FREEBSD:
548			if(piod->size != 4) return -EINVAL;
549			break;
550		case PCI_REVISION_ID_FREEBSD:
551			if(piod->size != 1) return -EINVAL;
552			break;
553		case SST1_PCI_SPECIAL4_FREEBSD:
554			if(piod->size != 4) return -EINVAL;
555			break;
556		default:
557			return -EINVAL;
558	}
559
560
561	/* Read the value and return */
562	switch(piod->size) {
563		case 1:
564			ret_byte = pci_read_config(tdfx_info[piod->device].dev,
565					piod->port, 1);
566			copyout(&ret_byte, piod->value, 1);
567			break;
568		case 2:
569			ret_word = pci_read_config(tdfx_info[piod->device].dev,
570					piod->port, 2);
571			copyout(&ret_word, piod->value, 2);
572			break;
573		case 4:
574			ret_dword = pci_read_config(tdfx_info[piod->device].dev,
575					piod->port, 4);
576			copyout(&ret_dword, piod->value, 4);
577			break;
578		default:
579			return -EINVAL;
580	}
581	return 0;
582}
583
584static int
585tdfx_query_update(u_int cmd, struct tdfx_pio_data *piod)
586{
587	/* XXX Comment this later, after careful inspection and spring cleaning :) */
588	/* Return vals */
589	u_int8_t  ret_byte;
590	u_int16_t ret_word;
591	u_int32_t ret_dword;
592
593	/* Port vals, mask */
594	u_int32_t retval, preval, mask;
595	struct tdfx_softc* tdfx_info = NULL;
596
597
598	if((piod == NULL) || (piod->device >= (tdfx_count &
599					0xf))) {
600#ifdef DEBUG
601		printf("tdfx: Bad struct or device in tdfx_query_update\n");
602#endif
603		return -EINVAL;
604	}
605
606	tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
607			piod->device);
608	if(tdfx_info == NULL) return -ENXIO;
609	/* Code below this line in the fuction was taken from the
610	 * Linux driver and converted for freebsd. */
611
612	/* Check the size for all the ports, to make sure stuff doesn't get messed up
613	 * by poorly written clients */
614
615	switch(piod->port) {
616		case PCI_COMMAND_FREEBSD:
617			if(piod->size != 2) return -EINVAL;
618			break;
619		case SST1_PCI_SPECIAL1_FREEBSD:
620			if(piod->size != 4) return -EINVAL;
621			break;
622		case SST1_PCI_SPECIAL2_FREEBSD:
623			if(piod->size != 4) return -EINVAL;
624			break;
625		case SST1_PCI_SPECIAL3_FREEBSD:
626			if(piod->size != 4) return -EINVAL;
627			break;
628		case SST1_PCI_SPECIAL4_FREEBSD:
629			if(piod->size != 4) return -EINVAL;
630			break;
631		default:
632			return -EINVAL;
633	}
634	/* Read the current value */
635	retval = pci_read_config(tdfx_info->dev, piod->port & ~3, 4);
636
637	/* These set up a mask to use, since apparently they wanted to write 4 bytes
638	 * at once to the ports */
639	switch (piod->size) {
640		case 1:
641			copyin(piod->value, &ret_byte, 1);
642			preval = ret_byte << (8 * (piod->port & 0x3));
643			mask = 0xff << (8 * (piod->port & 0x3));
644			break;
645		case 2:
646			copyin(piod->value, &ret_word, 2);
647			preval = ret_word << (8 * (piod->port & 0x3));
648			mask = 0xffff << (8 * (piod->port & 0x3));
649			break;
650		case 4:
651			copyin(piod->value, &ret_dword, 4);
652			preval = ret_dword;
653			mask = ~0;
654			break;
655		default:
656			return -EINVAL;
657	}
658	/* Finally, combine the values and write it to the port */
659	retval = (retval & ~mask) | preval;
660	pci_write_config(tdfx_info->dev, piod->port & ~3, retval, 4);
661
662	return 0;
663}
664
665/* For both of these, I added a variable named workport of type u_int so
666 * that I could eliminate the warning about my data type size. The
667 * applications expect the port to be of type short, so I needed to change
668 * this within the function */
669static int
670tdfx_do_pio_rd(struct tdfx_pio_data *piod)
671{
672	/* Return val */
673	u_int8_t  ret_byte;
674	u_int 	 workport;
675	/* Restricts the access of ports other than those we use */
676	if((piod->port != VGA_INPUT_STATUS_1C) || (piod->port != SC_INDEX) ||
677		(piod->port != SC_DATA) || (piod->port != VGA_MISC_OUTPUT_READ))
678		return -EPERM;
679
680	/* All VGA STATUS REGS are byte registers, size should never be > 1 */
681	if(piod->size != 1) {
682		return -EINVAL;
683	}
684
685	/* Write the data to the intended port */
686	workport = piod->port;
687	ret_byte = inb(workport);
688	copyout(&ret_byte, piod->value, sizeof(u_int8_t));
689	return 0;
690}
691
692static int
693tdfx_do_pio_wt(struct tdfx_pio_data *piod)
694{
695	/* return val */
696	u_int8_t  ret_byte;
697	u_int		 workport;
698	/* Replace old switch w/ massive if(...) */
699	/* Restricts the access of ports other than those we use */
700	if((piod->port != SC_INDEX) && (piod->port != SC_DATA) &&
701		(piod->port != VGA_MISC_OUTPUT_READ)) /* Can't write VGA_ST_1C */
702		return -EPERM;
703
704	/* All VGA STATUS REGS are byte registers, size should never be > 1 */
705	if(piod->size != 1) {
706		return -EINVAL;
707	}
708
709	/* Write the data to the intended port */
710	copyin(piod->value, &ret_byte, sizeof(u_int8_t));
711	workport = piod->port;
712	outb(workport, ret_byte);
713	return 0;
714}
715
716static int
717tdfx_do_query(u_int cmd, struct tdfx_pio_data *piod)
718{
719	/* There are three sub-commands to the query 0x33 */
720	switch(_IOC_NR(cmd)) {
721		case 2:
722			return tdfx_query_boards();
723			break;
724		case 3:
725			return tdfx_query_fetch(cmd, piod);
726			break;
727		case 4:
728			return tdfx_query_update(cmd, piod);
729			break;
730		default:
731			/* In case we are thrown a bogus sub-command! */
732#ifdef DEBUG
733			printf("Bad Sub-cmd: 0x%x\n", _IOC_NR(cmd));
734#endif
735			return -EINVAL;
736	};
737}
738
739static int
740tdfx_do_pio(u_int cmd, struct tdfx_pio_data *piod)
741{
742	/* Two types of PIO, INPUT and OUTPUT, as the name suggests */
743	switch(_IOC_DIR(cmd)) {
744		case IOCV_OUT:
745			return tdfx_do_pio_rd(piod);
746			break;
747		case IOCV_IN:
748			return tdfx_do_pio_wt(piod);
749			break;
750		default:
751			return -EINVAL;
752	};
753}
754
755/* Calls to ioctl(2) eventually end up here. Unhandled ioctls return an ENXIO,
756 * normally, you would read in the data pointed to by data, then write your
757 * output to it. The ioctl *should* normally return zero if everything is
758 * alright, but 3dfx didn't make it that way...
759 *
760 * For all of the ioctl code, in the event of a real error,
761 * we return -Exxxx rather than simply Exxxx. The reason for this
762 * is that the ioctls actually RET information back to the program
763 * sometimes, rather than filling it in the passed structure. We
764 * want to distinguish errors from useful data, and maintain compatibility.
765 *
766 * There is this portion of the proc struct called p_retval[], we can store a
767 * return value in p->p_retval[0] and place the return value if it is positive
768 * in there, then we can return 0 (good). If the return value is negative, we
769 * can return -retval and the error should be properly handled.
770 */
771static int
772tdfx_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc * p)
773{
774	int retval = 0;
775	struct tdfx_pio_data *piod = (struct tdfx_pio_data*)data;
776#ifdef	DEBUG
777	printf("IOCTL'd by #%d, cmd: 0x%x, data: 0x%x\n", p->p_pid, (u_int32_t)cmd,
778			(unsigned int)piod);
779#endif
780	switch(_IOC_TYPE(cmd)) {
781		/* Return the real error if negative, or simply stick the valid return
782		 * in p->p_retval */
783	case 0x33:
784			/* The '3'(0x33) type IOCTL is for querying the installed cards */
785			if((retval = tdfx_do_query(cmd, piod)) > 0) p->p_retval[0] = retval;
786			else return -retval;
787			break;
788		case 0:
789			/* The 0 type IOCTL is for programmed I/O methods */
790			if((tdfx_do_pio(cmd, piod)) > 0) p->p_retval[0] = retval;
791			else return -retval;
792			break;
793		default:
794			/* Technically, we won't reach this from linux emu, but when glide
795			 * finally gets ported, watch out! */
796#ifdef DEBUG
797			printf("Bad IOCTL from #%d\n", p->p_pid);
798#endif
799			return ENXIO;
800	}
801
802	return 0;
803}
804
805#ifdef TDFX_LINUX
806/*
807 * Linux emulation IOCTL for /dev/tdfx
808 */
809static int
810linux_ioctl_tdfx(struct proc* p, struct linux_ioctl_args* args)
811{
812   int error = 0;
813   u_long cmd = args->cmd & 0xffff;
814
815   /* The structure passed to ioctl has two shorts, one int
816      and one void*. */
817   char d_pio[2*sizeof(short) + sizeof(int) + sizeof(void*)];
818
819   struct file *fp = p->p_fd->fd_ofiles[args->fd];
820
821   /* We simply copy the data and send it right to ioctl */
822   copyin((caddr_t)args->arg, &d_pio, sizeof(d_pio));
823   error = fo_ioctl(fp, cmd, (caddr_t)&d_pio, p);
824   return error;
825}
826#endif /* TDFX_LINUX */
827
828
829/* This is the device driver struct. This is sent to the driver subsystem to
830 * register the method structure and the info strcut space for this particular
831 * instance of the driver.
832 */
833static driver_t tdfx_driver = {
834	"tdfx",
835	tdfx_methods,
836	sizeof(struct tdfx_softc),
837};
838
839/* Tell Mr. Kernel about us! */
840DRIVER_MODULE(tdfx, pci, tdfx_driver, tdfx_devclass, 0, 0);
841
842
843#endif	/* NPCI */
844