]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/awi/if_awi_pccard.c
Use __FBSDID().
[FreeBSD/FreeBSD.git] / sys / dev / awi / if_awi_pccard.c
1 /*
2  * Copyright (c) 2000 Atsushi Onoe <onoe@sm.sony.co.jp>
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 ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
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 #include <net/ethernet.h>
45
46 #include <net80211/ieee80211.h>
47
48 #include <dev/awi/am79c930reg.h>
49 #include <dev/awi/am79c930var.h>
50 #include <dev/awi/awireg.h>
51 #include <dev/awi/awivar.h>
52
53 #include <dev/pccard/pccardvar.h>
54 #include <dev/pccard/pccarddevs.h>
55 #include "card_if.h"
56
57 struct awi_pccard_softc {
58         struct awi_softc        sc_awi;
59
60         u_int8_t        sc_version[AWI_BANNER_LEN];
61         int             sc_intr_mask;
62         void            *sc_intrhand;
63         struct resource *sc_irq_res;
64         int             sc_irq_rid;
65         struct resource *sc_port_res;
66         int             sc_port_rid;
67         struct resource *sc_mem_res;
68         int             sc_mem_rid;
69 };
70
71 static const struct pccard_product awi_pccard_products[] = {
72         PCMCIA_CARD(AMD, AM79C930, 0),
73         PCMCIA_CARD(BAY, STACK_650, 0),
74         PCMCIA_CARD(BAY, STACK_660, 0),
75         PCMCIA_CARD(BAY, SURFER_PRO, 0),
76         PCMCIA_CARD(ICOM, SL200, 0),
77         PCMCIA_CARD(NOKIA, C020_WLAN, 0),
78         PCMCIA_CARD(FARALLON, SKYLINE, 0),
79         PCMCIA_CARD(ZOOM, AIR_4000, 0),
80         { NULL }
81 };
82
83 static int
84 awi_pccard_match(device_t dev)
85 {
86         const struct pccard_product *pp;
87
88         if ((pp = pccard_product_lookup(dev, awi_pccard_products,
89             sizeof(awi_pccard_products[0]), NULL)) != NULL) {
90                 if (pp->pp_name != NULL)
91                         device_set_desc(dev, pp->pp_name);
92                 return 0;
93         }
94         return ENXIO;
95 }
96
97 /*
98  * Initialize the device - called from Slot manager.
99  */
100 static int
101 awi_pccard_probe(device_t dev)
102 {
103         struct awi_pccard_softc *psc = device_get_softc(dev);
104         struct awi_softc *sc = &psc->sc_awi;
105         int error = 0;
106
107         psc->sc_port_rid = 0;
108         psc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
109             &psc->sc_port_rid, 0, ~0, 16, RF_ACTIVE);
110         if (!psc->sc_port_res)
111                 return ENOMEM;
112
113         sc->sc_chip.sc_iot = rman_get_bustag(psc->sc_port_res);
114         sc->sc_chip.sc_ioh = rman_get_bushandle(psc->sc_port_res);
115         am79c930_chip_init(&sc->sc_chip, 0);
116         DELAY(1000); 
117
118         awi_read_bytes(sc, AWI_BANNER, psc->sc_version, AWI_BANNER_LEN);
119         if (memcmp(psc->sc_version, "PCnetMobile:", 12) != 0)  {
120                 device_printf(dev, "awi_pccard_probe: bad banner: %12D\n",
121                     psc->sc_version, " ");
122                 error = ENXIO;
123         } else
124                 device_set_desc(dev, psc->sc_version);
125         bus_release_resource(dev, SYS_RES_IOPORT, psc->sc_port_rid,
126             psc->sc_port_res);
127
128         return error;
129 }
130
131 static int
132 awi_pccard_attach(device_t dev)
133 {
134         struct awi_pccard_softc *psc = device_get_softc(dev);
135         struct awi_softc *sc = &psc->sc_awi;
136         struct ifnet *ifp = &sc->sc_ec.ac_if;
137         int error = 0;
138
139         psc->sc_port_res = 0;
140         psc->sc_irq_res = 0;
141         psc->sc_mem_res = 0;
142         psc->sc_intrhand = 0;
143
144         ifp->if_name = "awi";
145         ifp->if_unit = device_get_unit(dev);
146         if (ifp->if_name == NULL) {
147                 printf("awi%d: awi_pccard_attach: cannot get device name\n",
148                     device_get_unit(dev));
149                 goto fail;
150         }
151         snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname),
152             "%s%d", ifp->if_name, ifp->if_unit);
153
154         psc->sc_port_rid = 0;
155         psc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
156             &psc->sc_port_rid, 0, ~0, 16, RF_ACTIVE);
157         if (!psc->sc_port_res) {
158                 device_printf(dev, "awi_pccard_attach: port alloc failed\n");
159                 goto fail;
160         }
161         sc->sc_chip.sc_iot = rman_get_bustag(psc->sc_port_res);
162         sc->sc_chip.sc_ioh = rman_get_bushandle(psc->sc_port_res);
163
164         psc->sc_irq_rid = 0;
165         psc->sc_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
166             &psc->sc_irq_rid, 0, ~0, 1, RF_ACTIVE);
167         if (!psc->sc_irq_res) {
168                 device_printf(dev, "awi_pccard_attach: irq alloc failed\n");
169                 goto fail;
170         }
171
172         psc->sc_mem_rid = 0;
173 #if 1
174         /*
175          * XXX: awi needs to access memory with 8bit,
176          * but pccardd apparently maps memory with MDF_16BITS flag.
177          * So memory mapped access is disabled and use IO port instead.
178          */
179         psc->sc_mem_res = 0;
180 #else
181         psc->sc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
182             &psc->sc_mem_rid, 0, ~0, 0x8000, RF_ACTIVE);
183 #endif
184         if (psc->sc_mem_res) {
185                 sc->sc_chip.sc_memt = rman_get_bustag(psc->sc_mem_res);
186                 sc->sc_chip.sc_memh = rman_get_bushandle(psc->sc_mem_res);
187                 am79c930_chip_init(&sc->sc_chip, 1);
188         } else
189                 am79c930_chip_init(&sc->sc_chip, 0);
190
191         error = bus_setup_intr(dev, psc->sc_irq_res, INTR_TYPE_NET,
192             (void (*)(void *))awi_intr, sc, &psc->sc_intrhand);
193         if (error) {
194                 device_printf(dev, "awi_pccard_attach: intr setup failed\n");
195                 goto fail;
196         }
197
198         sc->sc_cansleep = 1;
199         sc->sc_enabled = 1;
200         sc->sc_ifp = &sc->sc_ec.ac_if;
201
202         error = awi_attach(sc);
203         sc->sc_enabled = 0;     /*XXX*/
204         if (error == 0)
205                 return 0;
206         device_printf(dev, "awi_pccard_attach: awi_attach failed\n");
207
208   fail:
209         if (psc->sc_intrhand) {
210                 bus_teardown_intr(dev, psc->sc_irq_res, psc->sc_intrhand);
211                 psc->sc_intrhand = 0;
212         }
213         if (psc->sc_port_res) {
214                 bus_release_resource(dev, SYS_RES_IOPORT, psc->sc_port_rid,
215                     psc->sc_port_res);
216                 psc->sc_port_res = 0;
217         }
218         if (psc->sc_irq_res) {
219                 bus_release_resource(dev, SYS_RES_IRQ, psc->sc_irq_rid,
220                     psc->sc_irq_res);
221                 psc->sc_irq_res = 0;
222         }
223         if (psc->sc_mem_res) {
224                 bus_release_resource(dev, SYS_RES_MEMORY, psc->sc_mem_rid,
225                     psc->sc_mem_res);
226                 psc->sc_mem_res = 0;
227         }
228         if (error == 0)
229                 error = ENXIO;
230         return error;
231 }
232
233 static int
234 awi_pccard_detach(device_t dev)
235 {
236         struct awi_pccard_softc *psc = device_get_softc(dev);
237         struct awi_softc *sc = &psc->sc_awi;
238         struct ifnet *ifp = &sc->sc_ec.ac_if;
239
240         ether_ifdetach(ifp);
241         ifp->if_flags &= ~IFF_RUNNING; 
242         if (psc->sc_intrhand) {
243                 bus_teardown_intr(dev, psc->sc_irq_res, psc->sc_intrhand);
244                 psc->sc_intrhand = 0;
245         }
246         if (psc->sc_port_res) {
247                 bus_release_resource(dev, SYS_RES_IOPORT, psc->sc_port_rid,
248                     psc->sc_port_res);
249                 psc->sc_port_res = 0;
250         }
251         if (psc->sc_irq_res) {
252                 bus_release_resource(dev, SYS_RES_IRQ, psc->sc_irq_rid,
253                     psc->sc_irq_res);
254                 psc->sc_irq_res = 0;
255         }
256         if (psc->sc_mem_res) {
257                 bus_release_resource(dev, SYS_RES_MEMORY, psc->sc_mem_rid,
258                     psc->sc_mem_res);
259                 psc->sc_mem_res = 0;
260         }
261         return 0;
262 }
263
264 static device_method_t awi_pccard_methods[] = {
265         /* Device interface */
266         DEVMETHOD(device_probe,         pccard_compat_probe),
267         DEVMETHOD(device_attach,        pccard_compat_attach),
268         DEVMETHOD(device_detach,        awi_pccard_detach),
269
270         /* Card interface */
271         DEVMETHOD(card_compat_match,    awi_pccard_match),
272         DEVMETHOD(card_compat_probe,    awi_pccard_probe),
273         DEVMETHOD(card_compat_attach,   awi_pccard_attach),
274
275         { 0, 0 }
276 };
277
278 static driver_t awi_pccard_driver = {
279         "awi",
280         awi_pccard_methods,
281         sizeof(struct awi_pccard_softc),
282 };
283
284 extern devclass_t awi_devclass;
285
286 DRIVER_MODULE(awi, pccard, awi_pccard_driver, awi_devclass, 0, 0);
287 MODULE_DEPEND(awi, rc4, 1, 1, 1);