xref: /netbsd-src/lib/libpci/pci_device.c (revision d8c110686926247d432b594f9071f6f1a2aeecb3)
1*d8c11068Slukem /*	$NetBSD: pci_device.c,v 1.2 2003/03/08 09:53:45 lukem Exp $	*/
216487fdfSthorpej 
316487fdfSthorpej /*
416487fdfSthorpej  * Copyright 2001 Wasabi Systems, Inc.
516487fdfSthorpej  * All rights reserved.
616487fdfSthorpej  *
716487fdfSthorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
816487fdfSthorpej  *
916487fdfSthorpej  * Redistribution and use in source and binary forms, with or without
1016487fdfSthorpej  * modification, are permitted provided that the following conditions
1116487fdfSthorpej  * are met:
1216487fdfSthorpej  * 1. Redistributions of source code must retain the above copyright
1316487fdfSthorpej  *    notice, this list of conditions and the following disclaimer.
1416487fdfSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1516487fdfSthorpej  *    notice, this list of conditions and the following disclaimer in the
1616487fdfSthorpej  *    documentation and/or other materials provided with the distribution.
1716487fdfSthorpej  * 3. All advertising materials mentioning features or use of this software
1816487fdfSthorpej  *    must display the following acknowledgement:
1916487fdfSthorpej  *	This product includes software developed for the NetBSD Project by
2016487fdfSthorpej  *	Wasabi Systems, Inc.
2116487fdfSthorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2216487fdfSthorpej  *    or promote products derived from this software without specific prior
2316487fdfSthorpej  *    written permission.
2416487fdfSthorpej  *
2516487fdfSthorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2616487fdfSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2716487fdfSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2816487fdfSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
2916487fdfSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3016487fdfSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3116487fdfSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3216487fdfSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3316487fdfSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3416487fdfSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3516487fdfSthorpej  * POSSIBILITY OF SUCH DAMAGE.
3616487fdfSthorpej  */
3716487fdfSthorpej 
3816487fdfSthorpej /*
3916487fdfSthorpej  * Interface to PCI config space registers for a specific PCI device.
4016487fdfSthorpej  */
4116487fdfSthorpej 
42*d8c11068Slukem #include <sys/cdefs.h>
43*d8c11068Slukem __RCSID("$NetBSD: pci_device.c,v 1.2 2003/03/08 09:53:45 lukem Exp $");
44*d8c11068Slukem 
4516487fdfSthorpej #include <sys/types.h>
4616487fdfSthorpej #include <sys/ioctl.h>
4716487fdfSthorpej 
4816487fdfSthorpej #include <pci.h>
4916487fdfSthorpej 
5016487fdfSthorpej #include <dev/pci/pciio.h>
5116487fdfSthorpej 
5216487fdfSthorpej /*
5316487fdfSthorpej  * pcidev_conf_read:
5416487fdfSthorpej  *
5516487fdfSthorpej  *	Read a config word for a given bus/device/function.
5616487fdfSthorpej  */
5716487fdfSthorpej int
pcidev_conf_read(int fd,u_int reg,uint32_t * valp)5816487fdfSthorpej pcidev_conf_read(int fd, u_int reg, uint32_t *valp)
5916487fdfSthorpej {
6016487fdfSthorpej 	struct pciio_cfgreg r;
6116487fdfSthorpej 	int rv;
6216487fdfSthorpej 
6316487fdfSthorpej 	r.reg = reg;
6416487fdfSthorpej 
6516487fdfSthorpej 	rv = ioctl(fd, PCI_IOC_CFGREAD, &r);
6616487fdfSthorpej 	if (rv == -1) {
6716487fdfSthorpej 		/* errno already set. */
6816487fdfSthorpej 		return (-1);
6916487fdfSthorpej 	}
7016487fdfSthorpej 
7116487fdfSthorpej 	*valp = r.val;
7216487fdfSthorpej 	return (0);
7316487fdfSthorpej }
7416487fdfSthorpej 
7516487fdfSthorpej /*
7616487fdfSthorpej  * pcidev_conf_write:
7716487fdfSthorpej  *
7816487fdfSthorpej  *	Write a config word for a given bus/device/function.
7916487fdfSthorpej  */
8016487fdfSthorpej int
pcidev_conf_write(int fd,u_int reg,uint32_t val)8116487fdfSthorpej pcidev_conf_write(int fd, u_int reg, uint32_t val)
8216487fdfSthorpej {
8316487fdfSthorpej 	struct pciio_cfgreg r;
8416487fdfSthorpej 
8516487fdfSthorpej 	r.reg = reg;
8616487fdfSthorpej 	r.val = val;
8716487fdfSthorpej 
8816487fdfSthorpej 	return (ioctl(fd, PCI_IOC_CFGWRITE, &r));
8916487fdfSthorpej }
90