Home | History | Annotate | Line # | Download | only in pci
pci_usrreq.c revision 1.8.2.2
      1  1.8.2.1   darrenr /*	$NetBSD: pci_usrreq.c,v 1.8.2.2 2004/09/18 14:49:04 skrll Exp $	*/
      2      1.1   thorpej 
      3      1.1   thorpej /*
      4      1.1   thorpej  * Copyright 2001 Wasabi Systems, Inc.
      5      1.1   thorpej  * All rights reserved.
      6      1.1   thorpej  *
      7      1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8      1.1   thorpej  *
      9      1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10      1.1   thorpej  * modification, are permitted provided that the following conditions
     11      1.1   thorpej  * are met:
     12      1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13      1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14      1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16      1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17      1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18      1.1   thorpej  *    must display the following acknowledgement:
     19      1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     20      1.1   thorpej  *	Wasabi Systems, Inc.
     21      1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22      1.1   thorpej  *    or promote products derived from this software without specific prior
     23      1.1   thorpej  *    written permission.
     24      1.1   thorpej  *
     25      1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26      1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27      1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28      1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29      1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30      1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31      1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32      1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33      1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34      1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35      1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36      1.1   thorpej  */
     37      1.1   thorpej 
     38      1.1   thorpej /*
     39      1.1   thorpej  * User -> kernel interface for PCI bus access.
     40      1.1   thorpej  */
     41      1.3     lukem 
     42      1.3     lukem #include <sys/cdefs.h>
     43  1.8.2.1   darrenr __KERNEL_RCSID(0, "$NetBSD: pci_usrreq.c,v 1.8.2.2 2004/09/18 14:49:04 skrll Exp $");
     44      1.1   thorpej 
     45      1.1   thorpej #include <sys/param.h>
     46      1.1   thorpej #include <sys/conf.h>
     47      1.1   thorpej #include <sys/device.h>
     48      1.1   thorpej #include <sys/ioctl.h>
     49      1.1   thorpej #include <sys/proc.h>
     50      1.1   thorpej #include <sys/systm.h>
     51      1.1   thorpej #include <sys/errno.h>
     52      1.1   thorpej #include <sys/fcntl.h>
     53      1.1   thorpej 
     54      1.1   thorpej #include <dev/pci/pcireg.h>
     55      1.1   thorpej #include <dev/pci/pcivar.h>
     56      1.1   thorpej #include <dev/pci/pciio.h>
     57      1.1   thorpej 
     58      1.5   gehenna dev_type_open(pciopen);
     59      1.5   gehenna dev_type_ioctl(pciioctl);
     60      1.5   gehenna dev_type_mmap(pcimmap);
     61      1.5   gehenna 
     62      1.5   gehenna const struct cdevsw pci_cdevsw = {
     63      1.5   gehenna 	pciopen, nullclose, noread, nowrite, pciioctl,
     64      1.6  jdolecek 	nostop, notty, nopoll, pcimmap, nokqfilter,
     65      1.5   gehenna };
     66      1.1   thorpej 
     67      1.1   thorpej int
     68  1.8.2.2     skrll pciopen(dev_t dev, int flags, int mode, struct proc *p)
     69      1.1   thorpej {
     70      1.1   thorpej 	struct pci_softc *sc;
     71      1.1   thorpej 	int unit;
     72      1.1   thorpej 
     73      1.1   thorpej 	unit = minor(dev);
     74      1.1   thorpej 	sc = device_lookup(&pci_cd, unit);
     75      1.1   thorpej 	if (sc == NULL)
     76      1.1   thorpej 		return (ENXIO);
     77      1.1   thorpej 
     78      1.1   thorpej 	return (0);
     79      1.1   thorpej }
     80      1.1   thorpej 
     81      1.1   thorpej int
     82  1.8.2.2     skrll pciioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
     83      1.1   thorpej {
     84      1.1   thorpej 	struct pci_softc *sc = device_lookup(&pci_cd, minor(dev));
     85      1.1   thorpej 	struct pciio_bdf_cfgreg *bdfr = (void *) data;
     86      1.1   thorpej 	struct pciio_businfo *binfo = (void *) data;
     87      1.1   thorpej 	pcitag_t tag;
     88      1.1   thorpej 
     89      1.1   thorpej 	switch (cmd) {
     90      1.1   thorpej 	case PCI_IOC_BDF_CFGREAD:
     91      1.1   thorpej 	case PCI_IOC_BDF_CFGWRITE:
     92      1.1   thorpej 		if (bdfr->bus > 255 || bdfr->device >= sc->sc_maxndevs ||
     93      1.1   thorpej 		    bdfr->function > 7)
     94      1.1   thorpej 			return (EINVAL);
     95      1.1   thorpej 		tag = pci_make_tag(sc->sc_pc, bdfr->bus, bdfr->device,
     96      1.1   thorpej 		    bdfr->function);
     97      1.1   thorpej 		if (cmd == PCI_IOC_BDF_CFGREAD)
     98      1.1   thorpej 			bdfr->cfgreg.val = pci_conf_read(sc->sc_pc, tag,
     99      1.1   thorpej 			    bdfr->cfgreg.reg);
    100      1.1   thorpej 		else {
    101      1.1   thorpej 			if ((flag & FWRITE) == 0)
    102      1.1   thorpej 				return (EBADF);
    103      1.1   thorpej 			pci_conf_write(sc->sc_pc, tag, bdfr->cfgreg.reg,
    104      1.1   thorpej 			    bdfr->cfgreg.val);
    105      1.1   thorpej 		}
    106      1.1   thorpej 		break;
    107      1.1   thorpej 
    108      1.1   thorpej 	case PCI_IOC_BUSINFO:
    109      1.1   thorpej 		binfo->busno = sc->sc_bus;
    110      1.1   thorpej 		binfo->maxdevs = sc->sc_maxndevs;
    111      1.1   thorpej 		break;
    112      1.1   thorpej 
    113      1.1   thorpej 	default:
    114      1.1   thorpej 		return (ENOTTY);
    115      1.1   thorpej 	}
    116      1.1   thorpej 
    117      1.1   thorpej 	return (0);
    118      1.1   thorpej }
    119      1.1   thorpej 
    120      1.1   thorpej paddr_t
    121      1.1   thorpej pcimmap(dev_t dev, off_t offset, int prot)
    122      1.1   thorpej {
    123      1.2   thorpej #if 0
    124      1.1   thorpej 	struct pci_softc *sc = device_lookup(&pci_cd, minor(dev));
    125      1.1   thorpej 
    126      1.1   thorpej 	/*
    127      1.1   thorpej 	 * Since we allow mapping of the entire bus, we
    128      1.1   thorpej 	 * take the offset to be the address on the bus,
    129      1.1   thorpej 	 * and pass 0 as the offset into that range.
    130      1.1   thorpej 	 *
    131      1.1   thorpej 	 * XXX Need a way to deal with linear/prefetchable/etc.
    132      1.1   thorpej 	 */
    133      1.1   thorpej 	return (bus_space_mmap(sc->sc_memt, offset, 0, prot, 0));
    134      1.2   thorpej #else
    135      1.2   thorpej 	/* XXX Consider this further. */
    136      1.2   thorpej 	return (-1);
    137      1.2   thorpej #endif
    138      1.1   thorpej }
    139      1.1   thorpej 
    140      1.1   thorpej /*
    141      1.1   thorpej  * pci_devioctl:
    142      1.1   thorpej  *
    143      1.1   thorpej  *	PCI ioctls that can be performed on devices directly.
    144      1.1   thorpej  */
    145      1.1   thorpej int
    146      1.1   thorpej pci_devioctl(pci_chipset_tag_t pc, pcitag_t tag, u_long cmd, caddr_t data,
    147  1.8.2.2     skrll     int flag, struct proc *p)
    148      1.1   thorpej {
    149      1.1   thorpej 	struct pciio_cfgreg *r = (void *) data;
    150      1.1   thorpej 
    151      1.1   thorpej 	switch (cmd) {
    152      1.1   thorpej 	case PCI_IOC_CFGREAD:
    153      1.1   thorpej 	case PCI_IOC_CFGWRITE:
    154      1.1   thorpej 		if (cmd == PCI_IOC_CFGREAD)
    155      1.1   thorpej 			r->val = pci_conf_read(pc, tag, r->reg);
    156      1.1   thorpej 		else {
    157      1.1   thorpej 			if ((flag & FWRITE) == 0)
    158      1.1   thorpej 				return (EBADF);
    159      1.1   thorpej 			pci_conf_write(pc, tag, r->reg, r->val);
    160      1.1   thorpej 		}
    161      1.1   thorpej 		break;
    162      1.1   thorpej 
    163      1.1   thorpej 	default:
    164      1.4    atatat 		return (EPASSTHROUGH);
    165      1.1   thorpej 	}
    166      1.1   thorpej 
    167      1.1   thorpej 	return (0);
    168      1.1   thorpej }
    169