1*d914a3c1Smrg /* $NetBSD: pci_drvname.c,v 1.3 2021/12/11 19:24:19 mrg Exp $ */
21e4a2ee3Smrg
31e4a2ee3Smrg /*
41e4a2ee3Smrg * Copyright (c) 2014 Matthew R. Green
51e4a2ee3Smrg * All rights reserved.
61e4a2ee3Smrg *
71e4a2ee3Smrg * Redistribution and use in source and binary forms, with or without
81e4a2ee3Smrg * modification, are permitted provided that the following conditions
91e4a2ee3Smrg * are met:
101e4a2ee3Smrg * 1. Redistributions of source code must retain the above copyright
111e4a2ee3Smrg * notice, this list of conditions and the following disclaimer.
121e4a2ee3Smrg * 2. Redistributions in binary form must reproduce the above copyright
131e4a2ee3Smrg * notice, this list of conditions and the following disclaimer in the
141e4a2ee3Smrg * documentation and/or other materials provided with the distribution.
151e4a2ee3Smrg *
161e4a2ee3Smrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171e4a2ee3Smrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181e4a2ee3Smrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191e4a2ee3Smrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201e4a2ee3Smrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211e4a2ee3Smrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221e4a2ee3Smrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231e4a2ee3Smrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241e4a2ee3Smrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251e4a2ee3Smrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261e4a2ee3Smrg * SUCH DAMAGE.
271e4a2ee3Smrg */
281e4a2ee3Smrg
291e4a2ee3Smrg #include <sys/cdefs.h>
30*d914a3c1Smrg __RCSID("$NetBSD: pci_drvname.c,v 1.3 2021/12/11 19:24:19 mrg Exp $");
311e4a2ee3Smrg
321e4a2ee3Smrg #include <sys/types.h>
331e4a2ee3Smrg #include <sys/ioctl.h>
341e4a2ee3Smrg #include <string.h>
351e4a2ee3Smrg
361e4a2ee3Smrg #include <pci.h>
371e4a2ee3Smrg
381e4a2ee3Smrg #include <dev/pci/pciio.h>
391e4a2ee3Smrg
401e4a2ee3Smrg /*
411e4a2ee3Smrg * pci_drvname:
421e4a2ee3Smrg *
431e4a2ee3Smrg * What's the driver name for a PCI device?
441e4a2ee3Smrg */
451e4a2ee3Smrg int
pci_drvname(int fd,u_int device,u_int func,char * name,size_t len)461e4a2ee3Smrg pci_drvname(int fd, u_int device, u_int func, char *name, size_t len)
471e4a2ee3Smrg {
481e4a2ee3Smrg struct pciio_drvname drvname;
491e4a2ee3Smrg int rv;
501e4a2ee3Smrg
511e4a2ee3Smrg drvname.device = device;
521e4a2ee3Smrg drvname.function = func;
531e4a2ee3Smrg
541e4a2ee3Smrg rv = ioctl(fd, PCI_IOC_DRVNAME, &drvname);
551e4a2ee3Smrg if (rv == -1)
561e4a2ee3Smrg return -1;
571e4a2ee3Smrg
581e4a2ee3Smrg strlcpy(name, drvname.name, len);
591e4a2ee3Smrg return 0;
601e4a2ee3Smrg }
6162ee7dc2Smrg
6262ee7dc2Smrg /*
6362ee7dc2Smrg * pci_drvnameonbus:
6462ee7dc2Smrg *
6562ee7dc2Smrg * What's the driver name for a PCI device on any PCI bus?
6662ee7dc2Smrg */
6762ee7dc2Smrg int
pci_drvnameonbus(int fd,u_int bus,u_int device,u_int func,char * name,size_t len)6862ee7dc2Smrg pci_drvnameonbus(int fd, u_int bus, u_int device, u_int func, char *name,
6962ee7dc2Smrg size_t len)
7062ee7dc2Smrg {
7162ee7dc2Smrg struct pciio_drvnameonbus drvname;
7262ee7dc2Smrg int rv;
7362ee7dc2Smrg
7462ee7dc2Smrg drvname.bus = bus;
7562ee7dc2Smrg drvname.device = device;
7662ee7dc2Smrg drvname.function = func;
7762ee7dc2Smrg
7862ee7dc2Smrg rv = ioctl(fd, PCI_IOC_DRVNAMEONBUS, &drvname);
7962ee7dc2Smrg if (rv == -1)
8062ee7dc2Smrg return -1;
8162ee7dc2Smrg
8262ee7dc2Smrg strlcpy(name, drvname.name, len);
8362ee7dc2Smrg return 0;
8462ee7dc2Smrg }
85