xref: /minix3/lib/libpci/pci_device.c (revision 7eb99bda90fc3b15ed07bfd8809650d0818ad5f7)
1*7eb99bdaSLionel Sambuc /*	$NetBSD: pci_device.c,v 1.2 2003/03/08 09:53:45 lukem Exp $	*/
2*7eb99bdaSLionel Sambuc 
3*7eb99bdaSLionel Sambuc /*
4*7eb99bdaSLionel Sambuc  * Copyright 2001 Wasabi Systems, Inc.
5*7eb99bdaSLionel Sambuc  * All rights reserved.
6*7eb99bdaSLionel Sambuc  *
7*7eb99bdaSLionel Sambuc  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8*7eb99bdaSLionel Sambuc  *
9*7eb99bdaSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10*7eb99bdaSLionel Sambuc  * modification, are permitted provided that the following conditions
11*7eb99bdaSLionel Sambuc  * are met:
12*7eb99bdaSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*7eb99bdaSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*7eb99bdaSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
15*7eb99bdaSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
16*7eb99bdaSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
17*7eb99bdaSLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
18*7eb99bdaSLionel Sambuc  *    must display the following acknowledgement:
19*7eb99bdaSLionel Sambuc  *	This product includes software developed for the NetBSD Project by
20*7eb99bdaSLionel Sambuc  *	Wasabi Systems, Inc.
21*7eb99bdaSLionel Sambuc  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22*7eb99bdaSLionel Sambuc  *    or promote products derived from this software without specific prior
23*7eb99bdaSLionel Sambuc  *    written permission.
24*7eb99bdaSLionel Sambuc  *
25*7eb99bdaSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26*7eb99bdaSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27*7eb99bdaSLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28*7eb99bdaSLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29*7eb99bdaSLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30*7eb99bdaSLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31*7eb99bdaSLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32*7eb99bdaSLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33*7eb99bdaSLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34*7eb99bdaSLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35*7eb99bdaSLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
36*7eb99bdaSLionel Sambuc  */
37*7eb99bdaSLionel Sambuc 
38*7eb99bdaSLionel Sambuc /*
39*7eb99bdaSLionel Sambuc  * Interface to PCI config space registers for a specific PCI device.
40*7eb99bdaSLionel Sambuc  */
41*7eb99bdaSLionel Sambuc 
42*7eb99bdaSLionel Sambuc #include <sys/cdefs.h>
43*7eb99bdaSLionel Sambuc __RCSID("$NetBSD: pci_device.c,v 1.2 2003/03/08 09:53:45 lukem Exp $");
44*7eb99bdaSLionel Sambuc 
45*7eb99bdaSLionel Sambuc #include <sys/types.h>
46*7eb99bdaSLionel Sambuc #include <sys/ioctl.h>
47*7eb99bdaSLionel Sambuc 
48*7eb99bdaSLionel Sambuc #include <pci.h>
49*7eb99bdaSLionel Sambuc 
50*7eb99bdaSLionel Sambuc #include <dev/pci/pciio.h>
51*7eb99bdaSLionel Sambuc 
52*7eb99bdaSLionel Sambuc /*
53*7eb99bdaSLionel Sambuc  * pcidev_conf_read:
54*7eb99bdaSLionel Sambuc  *
55*7eb99bdaSLionel Sambuc  *	Read a config word for a given bus/device/function.
56*7eb99bdaSLionel Sambuc  */
57*7eb99bdaSLionel Sambuc int
pcidev_conf_read(int fd,u_int reg,uint32_t * valp)58*7eb99bdaSLionel Sambuc pcidev_conf_read(int fd, u_int reg, uint32_t *valp)
59*7eb99bdaSLionel Sambuc {
60*7eb99bdaSLionel Sambuc 	struct pciio_cfgreg r;
61*7eb99bdaSLionel Sambuc 	int rv;
62*7eb99bdaSLionel Sambuc 
63*7eb99bdaSLionel Sambuc 	r.reg = reg;
64*7eb99bdaSLionel Sambuc 
65*7eb99bdaSLionel Sambuc 	rv = ioctl(fd, PCI_IOC_CFGREAD, &r);
66*7eb99bdaSLionel Sambuc 	if (rv == -1) {
67*7eb99bdaSLionel Sambuc 		/* errno already set. */
68*7eb99bdaSLionel Sambuc 		return (-1);
69*7eb99bdaSLionel Sambuc 	}
70*7eb99bdaSLionel Sambuc 
71*7eb99bdaSLionel Sambuc 	*valp = r.val;
72*7eb99bdaSLionel Sambuc 	return (0);
73*7eb99bdaSLionel Sambuc }
74*7eb99bdaSLionel Sambuc 
75*7eb99bdaSLionel Sambuc /*
76*7eb99bdaSLionel Sambuc  * pcidev_conf_write:
77*7eb99bdaSLionel Sambuc  *
78*7eb99bdaSLionel Sambuc  *	Write a config word for a given bus/device/function.
79*7eb99bdaSLionel Sambuc  */
80*7eb99bdaSLionel Sambuc int
pcidev_conf_write(int fd,u_int reg,uint32_t val)81*7eb99bdaSLionel Sambuc pcidev_conf_write(int fd, u_int reg, uint32_t val)
82*7eb99bdaSLionel Sambuc {
83*7eb99bdaSLionel Sambuc 	struct pciio_cfgreg r;
84*7eb99bdaSLionel Sambuc 
85*7eb99bdaSLionel Sambuc 	r.reg = reg;
86*7eb99bdaSLionel Sambuc 	r.val = val;
87*7eb99bdaSLionel Sambuc 
88*7eb99bdaSLionel Sambuc 	return (ioctl(fd, PCI_IOC_CFGWRITE, &r));
89*7eb99bdaSLionel Sambuc }
90