Home | History | Annotate | Line # | Download | only in if_ndis
if_ndis_pccard.c revision 1.4
      1 /*-
      2  * Copyright (c) 2003
      3  *	Bill Paul <wpaul (at) windriver.com>.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by Bill Paul.
     16  * 4. Neither the name of the author nor the names of any co-contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     30  * THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_ndis_pccard.c,v 1.3 2007/12/11 12:36:02 lukem Exp $");
     35 #ifdef __FreeBSD__
     36 __FBSDID("$FreeBSD: src/sys/dev/if_ndis/if_ndis_pccard.c,v 1.6.2.3 2005/03/31 04:24:36 wpaul Exp $");
     37 #endif
     38 
     39 #include <sys/ctype.h>
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/module.h>
     44 #include <sys/socket.h>
     45 #include <sys/queue.h>
     46 #include <sys/sysctl.h>
     47 
     48 #include <net/if.h>
     49 #include <net/if_arp.h>
     50 #include <net/if_media.h>
     51 
     52 #include <sys/bus.h>
     53 #include <machine/resource.h>
     54 #include <sys/bus.h>
     55 #include <sys/rman.h>
     56 
     57 #include <net80211/ieee80211_var.h>
     58 
     59 #include <compat/ndis/pe_var.h>
     60 #include <compat/ndis/resource_var.h>
     61 #include <compat/ndis/ntoskrnl_var.h>
     62 #include <compat/ndis/ndis_var.h>
     63 #include <compat/ndis/cfg_var.h>
     64 #include <dev/if_ndis/if_ndisvar.h>
     65 
     66 #include <dev/pccard/pccardvar.h>
     67 #include "card_if.h"
     68 
     69 #include "ndis_driver_data.h"
     70 
     71 #ifdef NDIS_PCMCIA_DEV_TABLE
     72 
     73 MODULE_DEPEND(ndis, pccard, 1, 1, 1);
     74 MODULE_DEPEND(ndis, ether, 1, 1, 1);
     75 MODULE_DEPEND(ndis, wlan, 1, 1, 1);
     76 MODULE_DEPEND(ndis, ndisapi, 1, 1, 1);
     77 
     78 /*
     79  * Various supported device vendors/types and their names.
     80  * These are defined in the ndis_driver_data.h file.
     81  */
     82 static struct ndis_pccard_type ndis_devs[] = {
     83 #ifdef NDIS_PCMCIA_DEV_TABLE
     84 	NDIS_PCMCIA_DEV_TABLE
     85 #endif
     86 	{ NULL, NULL, NULL }
     87 };
     88 
     89 static int ndis_probe_pccard	(device_t);
     90 static int ndis_attach_pccard	(device_t);
     91 static struct resource_list *ndis_get_resource_list
     92 				(device_t, device_t);
     93 extern int ndisdrv_modevent	(module_t, int, void *);
     94 extern int ndis_attach		(device_t);
     95 extern int ndis_shutdown	(device_t);
     96 extern int ndis_detach		(device_t);
     97 extern int ndis_suspend		(device_t);
     98 extern int ndis_resume		(device_t);
     99 
    100 extern unsigned char drv_data[];
    101 
    102 static device_method_t ndis_methods[] = {
    103 	/* Device interface */
    104 	DEVMETHOD(device_probe,		ndis_probe_pccard),
    105 	DEVMETHOD(device_attach,	ndis_attach_pccard),
    106 	DEVMETHOD(device_detach,	ndis_detach),
    107 	DEVMETHOD(device_shutdown,	ndis_shutdown),
    108 	DEVMETHOD(device_suspend,	ndis_suspend),
    109 	DEVMETHOD(device_resume,	ndis_resume),
    110 
    111 	/* Bus interface. */
    112 
    113 	/*
    114 	 * This is an awful kludge, but we need it becase pccard
    115 	 * does not implement a bus_get_resource_list() method.
    116 	 */
    117 
    118 	DEVMETHOD(bus_get_resource_list, ndis_get_resource_list),
    119 
    120 	{ 0, 0 }
    121 };
    122 
    123 static driver_t ndis_driver = {
    124 #ifdef NDIS_DEVNAME
    125 	NDIS_DEVNAME,
    126 #else
    127 	"ndis",
    128 #endif
    129 	ndis_methods,
    130 	sizeof(struct ndis_softc)
    131 };
    132 
    133 static devclass_t ndis_devclass;
    134 
    135 #ifdef NDIS_MODNAME
    136 #define NDIS_MODNAME_OVERRIDE_PCMCIA(x)					\
    137 	DRIVER_MODULE(x, pccard, ndis_driver, ndis_devclass,		\
    138 		ndisdrv_modevent, 0)
    139 NDIS_MODNAME_OVERRIDE_PCMCIA(NDIS_MODNAME);
    140 #else
    141 DRIVER_MODULE(ndis, pccard, ndis_driver, ndis_devclass, ndisdrv_modevent, 0);
    142 #endif
    143 
    144 /*
    145  * Probe for an NDIS device. Check the PCI vendor and device
    146  * IDs against our list and return a device name if we find a match.
    147  */
    148 static int
    149 ndis_probe_pccard(device_t dev)
    150 {
    151 	struct ndis_pccard_type	*t;
    152 	const char		*prodstr, *vendstr;
    153 	int			error;
    154 	driver_object		*drv;
    155 
    156 	drv = windrv_lookup(0, "PCCARD Bus");
    157 	if (drv == NULL)
    158 		return(ENXIO);
    159 
    160 	t = ndis_devs;
    161 
    162 	error = pccard_get_product_str(dev, &prodstr);
    163 	if (error)
    164 		return(error);
    165 	error = pccard_get_vendor_str(dev, &vendstr);
    166 	if (error)
    167 		return(error);
    168 
    169 	while(t->ndis_name != NULL) {
    170 		if (ndis_strcasecmp(vendstr, t->ndis_vid) == 0 &&
    171 		    ndis_strcasecmp(prodstr, t->ndis_did) == 0) {
    172 			device_set_desc(dev, t->ndis_name);
    173 			/* Create PDO for this device instance */
    174 			windrv_create_pdo(drv, dev);
    175 			return(0);
    176 		}
    177 		t++;
    178 	}
    179 
    180 	return(ENXIO);
    181 }
    182 
    183 /*
    184  * Attach the interface. Allocate softc structures, do ifmedia
    185  * setup and ethernet/BPF attach.
    186  */
    187 static int
    188 ndis_attach_pccard(device_t dev)
    189 {
    190 	struct ndis_softc	*sc;
    191 	int			unit, error = 0, rid;
    192 	struct ndis_pccard_type	*t;
    193 	int			devidx = 0;
    194 	const char		*prodstr, *vendstr;
    195 
    196 	sc = device_get_softc(dev);
    197 	unit = device_get_unit(dev);
    198 	sc->ndis_dev = dev;
    199 	resource_list_init(&sc->ndis_rl);
    200 
    201 	sc->ndis_io_rid = 0;
    202 	sc->ndis_res_io = bus_alloc_resource(dev,
    203 	    SYS_RES_IOPORT, &sc->ndis_io_rid,
    204 	    0, ~0, 1, RF_ACTIVE);
    205 	if (sc->ndis_res_io == NULL) {
    206 		device_printf(dev,
    207 		    "couldn't map iospace\n");
    208 		error = ENXIO;
    209 		goto fail;
    210 	}
    211 	sc->ndis_rescnt++;
    212 	resource_list_add(&sc->ndis_rl, SYS_RES_IOPORT, rid,
    213 	    rman_get_start(sc->ndis_res_io), rman_get_end(sc->ndis_res_io),
    214 	    rman_get_size(sc->ndis_res_io));
    215 
    216 	rid = 0;
    217 	sc->ndis_irq = bus_alloc_resource(dev,
    218 	    SYS_RES_IRQ, &rid, 0, ~0, 1,
    219 	    RF_SHAREABLE | RF_ACTIVE);
    220 	if (sc->ndis_irq == NULL) {
    221 		device_printf(dev,
    222 		    "couldn't map interrupt\n");
    223 		error = ENXIO;
    224 		goto fail;
    225 	}
    226 	sc->ndis_rescnt++;
    227 	resource_list_add(&sc->ndis_rl, SYS_RES_IRQ, rid,
    228 	    rman_get_start(sc->ndis_irq), rman_get_start(sc->ndis_irq), 1);
    229 
    230 	sc->ndis_iftype = PCMCIABus;
    231 
    232 	/* Figure out exactly which device we matched. */
    233 
    234 	t = ndis_devs;
    235 
    236 	error = pccard_get_product_str(dev, &prodstr);
    237 	if (error)
    238 		return(error);
    239 	error = pccard_get_vendor_str(dev, &vendstr);
    240 	if (error)
    241 		return(error);
    242 
    243 	while(t->ndis_name != NULL) {
    244 		if (ndis_strcasecmp(vendstr, t->ndis_vid) == 0 &&
    245 		    ndis_strcasecmp(prodstr, t->ndis_did) == 0)
    246 			break;
    247 		t++;
    248 		devidx++;
    249 	}
    250 
    251 	sc->ndis_devidx = devidx;
    252 
    253 	error = ndis_attach(dev);
    254 
    255 fail:
    256 	return(error);
    257 }
    258 
    259 static struct resource_list *
    260 ndis_get_resource_list(device_t dev, device_t child)
    261 {
    262 	struct ndis_softc	*sc;
    263 
    264 	sc = device_get_softc(dev);
    265 	return (&sc->ndis_rl);
    266 }
    267 
    268 #endif /* NDIS_PCI_DEV_TABLE */
    269 
    270 #define NDIS_AM_RID 3
    271 
    272 int
    273 ndis_alloc_amem(void *arg)
    274 {
    275 	struct ndis_softc	*sc;
    276 	int			error, rid;
    277 
    278 	if (arg == NULL)
    279 		return(EINVAL);
    280 
    281 	sc = arg;
    282 	rid = NDIS_AM_RID;
    283 	sc->ndis_res_am = bus_alloc_resource(sc->ndis_dev, SYS_RES_MEMORY,
    284 	    &rid, 0UL, ~0UL, 0x1000, RF_ACTIVE);
    285 
    286 	if (sc->ndis_res_am == NULL) {
    287 		device_printf(sc->ndis_dev,
    288 		    "failed to allocate attribute memory\n");
    289 		return(ENXIO);
    290 	}
    291 	sc->ndis_rescnt++;
    292 	resource_list_add(&sc->ndis_rl, SYS_RES_MEMORY, rid,
    293 	    rman_get_start(sc->ndis_res_am), rman_get_end(sc->ndis_res_am),
    294 	    rman_get_size(sc->ndis_res_am));
    295 
    296 	error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->ndis_dev),
    297 	    sc->ndis_dev, rid, 0, NULL);
    298 
    299 	if (error) {
    300 		device_printf(sc->ndis_dev,
    301 		    "CARD_SET_MEMORY_OFFSET() returned 0x%x\n", error);
    302 		return(error);
    303 	}
    304 
    305 	error = CARD_SET_RES_FLAGS(device_get_parent(sc->ndis_dev),
    306 	    sc->ndis_dev, SYS_RES_MEMORY, rid, PCCARD_A_MEM_ATTR);
    307 
    308 	if (error) {
    309 		device_printf(sc->ndis_dev,
    310 		    "CARD_SET_RES_FLAGS() returned 0x%x\n", error);
    311 		return(error);
    312 	}
    313 
    314 	sc->ndis_am_rid = rid;
    315 
    316 	return(0);
    317 }
    318 
    319 void
    320 ndis_free_amem(void *arg)
    321 {
    322 	struct ndis_softc	*sc;
    323 
    324 	if (arg == NULL)
    325 		return;
    326 
    327 	sc = arg;
    328 
    329 	if (sc->ndis_res_am != NULL)
    330 		bus_release_resource(sc->ndis_dev, SYS_RES_MEMORY,
    331 		    sc->ndis_am_rid, sc->ndis_res_am);
    332 	resource_list_free(&sc->ndis_rl);
    333 
    334 	return;
    335 }
    336