if_ed_pccard.c revision 59313
1292915Sdim/* 2292915Sdim * Copyright (c) 1995, David Greenman 3292915Sdim * All rights reserved. 4292915Sdim * 5292915Sdim * Redistribution and use in source and binary forms, with or without 6292915Sdim * modification, are permitted provided that the following conditions 7292915Sdim * are met: 8292915Sdim * 1. Redistributions of source code must retain the above copyright 9292915Sdim * notice unmodified, this list of conditions, and the following 10292915Sdim * disclaimer. 11292915Sdim * 2. Redistributions in binary form must reproduce the above copyright 12292915Sdim * notice, this list of conditions and the following disclaimer in the 13292915Sdim * documentation and/or other materials provided with the distribution. 14292915Sdim * 15292915Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16292915Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17292915Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18292915Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19292915Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20292915Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21292915Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22292915Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23292915Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24292915Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25292915Sdim * SUCH DAMAGE. 26292915Sdim * 27292915Sdim * $FreeBSD: head/sys/dev/ed/if_ed_pccard.c 59313 2000-04-17 04:39:38Z imp $ 28292915Sdim */ 29292915Sdim 30292915Sdim#include <sys/param.h> 31292915Sdim#include <sys/systm.h> 32292915Sdim#include <sys/socket.h> 33292915Sdim#include <sys/kernel.h> 34292915Sdim 35292915Sdim#include <sys/module.h> 36292915Sdim#include <sys/bus.h> 37292915Sdim#include <machine/bus.h> 38292915Sdim 39292915Sdim#include <net/ethernet.h> 40292915Sdim#include <net/if.h> 41292915Sdim#include <net/if_arp.h> 42292915Sdim#include <net/if_mib.h> 43292915Sdim 44292915Sdim#include <dev/ed/if_edvar.h> 45292915Sdim#include <dev/pccard/pccardvar.h> 46292915Sdim 47292915Sdim/* 48292915Sdim * PC-Card (PCMCIA) specific code. 49292915Sdim */ 50292915Sdimstatic int ed_pccard_probe(device_t); 51292915Sdimstatic int ed_pccard_attach(device_t); 52292915Sdimstatic int ed_pccard_detach(device_t); 53292915Sdim 54292915Sdimstatic device_method_t ed_pccard_methods[] = { 55292915Sdim /* Device interface */ 56292915Sdim DEVMETHOD(device_probe, ed_pccard_probe), 57292915Sdim DEVMETHOD(device_attach, ed_pccard_attach), 58292915Sdim DEVMETHOD(device_detach, ed_pccard_detach), 59292915Sdim 60292915Sdim { 0, 0 } 61292915Sdim}; 62292915Sdim 63292915Sdimstatic driver_t ed_pccard_driver = { 64292915Sdim "ed", 65292915Sdim ed_pccard_methods, 66292915Sdim sizeof(struct ed_softc) 67292915Sdim}; 68292915Sdim 69292915Sdimstatic devclass_t ed_pccard_devclass; 70292915Sdim 71292915SdimDRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_pccard_devclass, 0, 0); 72292915Sdim 73292915Sdim/* 74292915Sdim * ed_pccard_detach - unload the driver and clear the table. 75292915Sdim * XXX TODO: 76 * This is usually called when the card is ejected, but 77 * can be caused by a modunload of a controller driver. 78 * The idea is to reset the driver's view of the device 79 * and ensure that any driver entry points such as 80 * read and write do not hang. 81 */ 82static int 83ed_pccard_detach(device_t dev) 84{ 85 struct ed_softc *sc = device_get_softc(dev); 86 struct ifnet *ifp = &sc->arpcom.ac_if; 87 88 if (sc->gone) { 89 device_printf(dev, "already unloaded\n"); 90 return (0); 91 } 92 ed_stop(sc); 93 ifp->if_flags &= ~IFF_RUNNING; 94 if_detach(ifp); 95 sc->gone = 1; 96 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle); 97 ed_release_resources(dev); 98 return (0); 99} 100 101/* 102 * Probe framework for pccards. Replicates the standard framework, 103 * minus the pccard driver registration and ignores the ether address 104 * supplied (from the CIS), relying on the probe to find it instead. 105 */ 106static int 107ed_pccard_probe(device_t dev) 108{ 109 int error; 110 111 error = ed_probe_WD80x3(dev); 112 if (error == 0) 113 goto end; 114 ed_release_resources(dev); 115 116 error = ed_probe_Novell(dev); 117 if (error == 0) 118 goto end; 119 ed_release_resources(dev); 120 121end: 122 if (error == 0) 123 error = ed_alloc_irq(dev, 0, 0); 124 125 ed_release_resources(dev); 126 return (error); 127} 128 129static int 130ed_pccard_attach(device_t dev) 131{ 132 struct ed_softc *sc = device_get_softc(dev); 133 int flags = device_get_flags(dev); 134 int error; 135 int i; 136 u_char sum; 137 u_char ether_addr[ETHER_ADDR_LEN]; 138 139 if (sc->port_used > 0) 140 ed_alloc_port(dev, sc->port_rid, sc->port_used); 141 if (sc->mem_used) 142 ed_alloc_memory(dev, sc->mem_rid, sc->mem_used); 143 ed_alloc_irq(dev, sc->irq_rid, 0); 144 145 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, 146 edintr, sc, &sc->irq_handle); 147 if (error) { 148 printf("setup intr failed %d \n", error); 149 ed_release_resources(dev); 150 return (error); 151 } 152 153 pccard_get_ether(dev, ether_addr); 154 for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++) 155 sum |= ether_addr[i]; 156 if (sum) 157 bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 158 159 error = ed_attach(sc, device_get_unit(dev), flags); 160 return (error); 161} 162