Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / netif / ex / if_ex_pccard.c
1 /*-
2  * Copyright (c) 2000 Mitsuru IWASAKI
3  * 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/sys/dev/ex/if_ex_pccard.c,v 1.2.2.1 2001/03/05 05:33:20 imp Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33
34 #include <sys/module.h>
35 #include <sys/bus.h>
36
37 #include <machine/bus.h>
38 #include <machine/resource.h>
39 #include <sys/rman.h>
40
41 #include <net/if.h>
42 #include <net/if_arp.h>
43 #include <net/if_media.h> 
44
45
46 #include <dev/ex/if_exreg.h>
47 #include <dev/ex/if_exvar.h>
48
49 #include <dev/pccard/pccardvar.h>
50
51 /* Bus Front End Functions */
52 static int      ex_pccard_probe         __P((device_t));
53 static int      ex_pccard_attach        __P((device_t));
54 static int      ex_pccard_detach        __P((device_t));
55
56 static device_method_t ex_pccard_methods[] = {
57         /* Device interface */
58         DEVMETHOD(device_probe,         ex_pccard_probe),
59         DEVMETHOD(device_attach,        ex_pccard_attach),
60         DEVMETHOD(device_detach,        ex_pccard_detach),
61
62         { 0, 0 }
63 };
64
65 static driver_t ex_pccard_driver = {
66         "ex",
67         ex_pccard_methods,
68         sizeof(struct ex_softc),
69 };
70
71 extern devclass_t ex_devclass;
72
73 DRIVER_MODULE(ex, pccard, ex_pccard_driver, ex_devclass, 0, 0);
74
75 static int
76 ex_pccard_probe(device_t dev)
77 {
78         u_int           iobase;
79         u_int           irq;
80
81         iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0);
82         if (!iobase) {
83                 printf("ex: no iobase?\n");
84                 return(ENXIO);
85         }
86
87         if (bootverbose)
88                 printf("ex: ex_pccard_probe() found card at 0x%03x\n", iobase);
89
90         irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0);
91
92         if (irq == 0) {
93                 printf("ex: invalid IRQ.\n");
94                 return(ENXIO);
95         }
96
97         return(0);
98 }
99
100 static int
101 ex_pccard_attach(device_t dev)
102 {
103         struct ex_softc *       sc = device_get_softc(dev);
104         int                     error = 0;
105         int                     i;
106         u_char                  sum;
107         u_char                  ether_addr[ETHER_ADDR_LEN];
108
109         sc->dev = dev;
110         sc->ioport_rid = 0;
111         sc->irq_rid = 0;
112
113         if ((error = ex_alloc_resources(dev)) != 0) {
114                 device_printf(dev, "ex_alloc_resources() failed!\n");
115                 goto bad;
116         }
117
118         /*
119          * Fill in several fields of the softc structure:
120          *      - I/O base address.
121          *      - Hardware Ethernet address.
122          *      - IRQ number.
123          */
124         sc->iobase = rman_get_start(sc->ioport);
125         sc->irq_no = rman_get_start(sc->irq);
126
127         pccard_get_ether(dev, ether_addr);
128         for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
129                 sum |= ether_addr[i];
130         if (sum)
131                 bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
132
133         if ((error = ex_attach(dev)) != 0) {
134                 device_printf(dev, "ex_attach() failed!\n");
135                 goto bad;
136         }
137
138         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
139                                 ex_intr, (void *)sc, &sc->ih);
140         if (error) {
141                 device_printf(dev, "bus_setup_intr() failed!\n");
142                 goto bad;
143         }
144
145         return(0);
146 bad:
147         ex_release_resources(dev);
148         return (error);
149 }
150
151 static int
152 ex_pccard_detach(device_t dev)
153 {
154         struct ex_softc         *sc = device_get_softc(dev);
155         struct ifnet            *ifp = &sc->arpcom.ac_if;
156
157         ex_stop(sc);
158         ifp->if_flags &= ~IFF_RUNNING;
159         if_detach(ifp);
160         ex_release_resources(dev);
161         return (0);
162 }