Home | History | Annotate | Line # | Download | only in via
via_pci.c revision 1.1
      1 /*	$NetBSD: via_pci.c,v 1.1 2015/04/29 11:25:36 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: via_pci.c,v 1.1 2015/04/29 11:25:36 riastradh Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/device.h>
     37 #include <sys/systm.h>
     38 
     39 #include <linux/pci.h>
     40 
     41 #include <drm/drmP.h>
     42 #include <drm/drm_pciids.h>
     43 #include <drm/via_drm.h>
     44 
     45 #include "via_drv.h"
     46 
     47 struct viadrm_softc {
     48 	device_t		sc_dev;
     49 	struct pci_dev		sc_pci_dev;
     50 	struct drm_device	*sc_drm_dev;
     51 };
     52 
     53 static int	viadrm_match(device_t, cfdata_t, void *);
     54 static void	viadrm_attach(device_t, device_t, void *);
     55 static int	viadrm_detach(device_t, int);
     56 
     57 extern struct drm_driver *const via_drm_driver; /* XXX */
     58 
     59 CFATTACH_DECL_NEW(viadrmums, sizeof(struct viadrm_softc),
     60     viadrm_match, viadrm_attach, viadrm_detach, NULL);
     61 
     62 static const struct pci_device_id viadrm_pci_ids[] = {
     63 	viadrv_PCI_IDS
     64 };
     65 
     66 static const unsigned long *
     67 viadrm_lookup(const struct pci_attach_args *pa)
     68 {
     69 	unsigned i;
     70 
     71 	for (i = 0; i < __arraycount(viadrm_pci_ids); i++) {
     72 		KASSERT(viadrm_pci_ids[i].subvendor == PCI_ANY_ID);
     73 		KASSERT(viadrm_pci_ids[i].subdevice == PCI_ANY_ID);
     74 		KASSERT(viadrm_pci_ids[i].class == 0);
     75 		KASSERT(viadrm_pci_ids[i].class_mask == 0);
     76 		if (PCI_VENDOR(pa->pa_id) != viadrm_pci_ids[i].vendor)
     77 			continue;
     78 		if (PCI_PRODUCT(pa->pa_id) != viadrm_pci_ids[i].device)
     79 			continue;
     80 		return &viadrm_pci_ids[i].driver_data;
     81 	}
     82 
     83 	return NULL;
     84 }
     85 
     86 static int
     87 viadrm_match(device_t parent, cfdata_t match, void *aux)
     88 {
     89 	extern int viadrm_guarantee_initialized(void);
     90 	const struct pci_attach_args *const pa = aux;
     91 	int error;
     92 
     93 	error = viadrm_guarantee_initialized();
     94 	if (error) {
     95 		aprint_error("viadrm: failed to initialize: %d\n", error);
     96 		return 0;
     97 	}
     98 
     99 	if (viadrm_lookup(pa) == NULL)
    100 		return 0;
    101 
    102 	return 1;
    103 }
    104 
    105 static void
    106 viadrm_attach(device_t parent, device_t self, void *aux)
    107 {
    108 	struct viadrm_softc *const sc = device_private(self);
    109 	const struct pci_attach_args *const pa = aux;
    110 	const unsigned long *const cookiep = viadrm_lookup(pa);
    111 	int error;
    112 
    113 	KASSERT(cookiep != NULL);
    114 
    115 	/* XXX errno Linux->NetBSD */
    116 	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, via_drm_driver,
    117 	    *cookiep, &sc->sc_drm_dev);
    118 	if (error) {
    119 		aprint_error_dev(self, "unable to attach drm: %d\n", error);
    120 		return;
    121 	}
    122 }
    123 
    124 static int
    125 viadrm_detach(device_t self, int flags)
    126 {
    127 	struct viadrm_softc *const sc = device_private(self);
    128 	int error;
    129 
    130 	error = config_detach_children(self, flags);
    131 	if (error)
    132 		return error;
    133 	if (sc->sc_drm_dev == NULL)
    134 		goto out;
    135 	/* XXX errno Linux->NetBSD */
    136 	error = -drm_pci_detach(sc->sc_drm_dev, flags);
    137 	if (error)
    138 		return error;
    139 	sc->sc_drm_dev = NULL;
    140 out:	return 0;
    141 }
    142