bd60232bc86ddb37f79e0daae15fafcc95ac2a87
[dragonfly.git] / sys / dev / netif / 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  * $FreeBSD: src/sys/dev/awi/if_awi_pccard.c,v 1.5.2.1 2000/12/07 04:09:39 imp Exp $
26  * $DragonFly: src/sys/dev/netif/awi/Attic/if_awi_pccard.c,v 1.14 2005/11/28 17:13:41 dillon 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 #include <net/ethernet.h>
45 #include <netproto/802_11/ieee80211.h>
46 #include <netproto/802_11/ieee80211_ioctl.h>
47
48 #include <machine/clock.h>
49
50 #include "am79c930reg.h"
51 #include "am79c930var.h"
52 #include "awireg.h"
53 #include "awivar.h"
54
55 #include <bus/pccard/pccardvar.h>
56 #include <bus/pccard/pccarddevs.h>
57 #include "card_if.h"
58
59 struct awi_pccard_softc {
60         struct awi_softc        sc_awi;
61
62         u_int8_t        sc_version[AWI_BANNER_LEN];
63         int             sc_intr_mask;
64         void            *sc_intrhand;
65         struct resource *sc_irq_res;
66         int             sc_irq_rid;
67         struct resource *sc_port_res;
68         int             sc_port_rid;
69         struct resource *sc_mem_res;
70         int             sc_mem_rid;
71 };
72
73 static const struct pccard_product awi_pccard_products[] = {
74         PCMCIA_CARD(AMD, AM79C930, 0),
75         PCMCIA_CARD(BAY, STACK_650, 0),
76         PCMCIA_CARD(BAY, STACK_660, 0),
77         PCMCIA_CARD(BAY, SURFER_PRO, 0),
78         PCMCIA_CARD(ICOM, SL200, 0),
79         PCMCIA_CARD(NOKIA, C020_WLAN, 0),
80         PCMCIA_CARD(FARALLON, SKYLINE, 0),
81         PCMCIA_CARD(ZOOM, AIR_4000, 0),
82         { NULL }
83 };
84
85 static int awi_pccard_match(device_t);
86 static int awi_pccard_probe(device_t);
87 static int awi_pccard_attach(device_t);
88 static int awi_pccard_detach(device_t);
89
90 static int
91 awi_pccard_match(device_t dev)
92 {
93         const struct pccard_product *pp;
94
95         if ((pp = pccard_product_lookup(dev, awi_pccard_products,
96             sizeof(awi_pccard_products[0]), NULL)) != NULL) {
97                 if (pp->pp_name != NULL)
98                         device_set_desc(dev, pp->pp_name);
99                 return 0;
100         }
101         return ENXIO;
102 }
103
104 /*
105  * Initialize the device - called from Slot manager.
106  */
107 static int
108 awi_pccard_probe(device_t dev)
109 {
110         struct awi_pccard_softc *psc = device_get_softc(dev);
111         struct awi_softc *sc = &psc->sc_awi;
112         int error = 0;
113
114         psc->sc_port_rid = 0;
115         psc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
116             &psc->sc_port_rid, 0, ~0, 16, RF_ACTIVE);
117         if (!psc->sc_port_res)
118                 return ENOMEM;
119
120         sc->sc_chip.sc_iot = rman_get_bustag(psc->sc_port_res);
121         sc->sc_chip.sc_ioh = rman_get_bushandle(psc->sc_port_res);
122         am79c930_chip_init(&sc->sc_chip, 0);
123         DELAY(1000); 
124
125         awi_read_bytes(sc, AWI_BANNER, psc->sc_version, AWI_BANNER_LEN);
126         if (memcmp(psc->sc_version, "PCnetMobile:", 12) != 0)  {
127                 device_printf(dev, "awi_pccard_probe: bad banner: %12D\n",
128                     psc->sc_version, " ");
129                 error = ENXIO;
130         } else
131                 device_set_desc(dev, psc->sc_version);
132         bus_release_resource(dev, SYS_RES_IOPORT, psc->sc_port_rid,
133             psc->sc_port_res);
134
135         return error;
136 }
137
138 static int
139 awi_pccard_attach(device_t dev)
140 {
141         struct awi_pccard_softc *psc = device_get_softc(dev);
142         struct awi_softc *sc = &psc->sc_awi;
143         struct ifnet *ifp = &sc->sc_ec.ac_if;
144         int error = 0;
145
146         psc->sc_port_res = 0;
147         psc->sc_irq_res = 0;
148         psc->sc_mem_res = 0;
149         psc->sc_intrhand = 0;
150
151         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
152
153         psc->sc_port_rid = 0;
154         psc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
155             &psc->sc_port_rid, 0, ~0, 16, RF_ACTIVE);
156         if (!psc->sc_port_res) {
157                 device_printf(dev, "awi_pccard_attach: port alloc failed\n");
158                 goto fail;
159         }
160         sc->sc_chip.sc_iot = rman_get_bustag(psc->sc_port_res);
161         sc->sc_chip.sc_ioh = rman_get_bushandle(psc->sc_port_res);
162
163         psc->sc_irq_rid = 0;
164         psc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
165             &psc->sc_irq_rid, RF_ACTIVE);
166         if (!psc->sc_irq_res) {
167                 device_printf(dev, "awi_pccard_attach: irq alloc failed\n");
168                 goto fail;
169         }
170
171         psc->sc_mem_rid = 0;
172 #if 1
173         /*
174          * XXX: awi needs to access memory with 8bit,
175          * but pccardd apparently maps memory with MDF_16BITS flag.
176          * So memory mapped access is disabled and use IO port instead.
177          */
178         psc->sc_mem_res = 0;
179 #else
180         psc->sc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
181             &psc->sc_mem_rid, 0, ~0, 0x8000, RF_ACTIVE);
182 #endif
183         if (psc->sc_mem_res) {
184                 sc->sc_chip.sc_memt = rman_get_bustag(psc->sc_mem_res);
185                 sc->sc_chip.sc_memh = rman_get_bushandle(psc->sc_mem_res);
186                 am79c930_chip_init(&sc->sc_chip, 1);
187         } else
188                 am79c930_chip_init(&sc->sc_chip, 0);
189
190         sc->sc_enabled = 1;
191         sc->sc_ifp = &sc->sc_ec.ac_if;
192
193         error = awi_attach(sc);
194         sc->sc_enabled = 0;     /*XXX*/
195
196         if (error) {
197                 device_printf(dev, "awi_pccard_attach: awi_attach failed\n");
198                 goto fail;
199         }
200
201         error = bus_setup_intr(dev, psc->sc_irq_res, INTR_NETSAFE,
202                                (void (*)(void *))awi_intr, sc,
203                                &psc->sc_intrhand, sc->sc_ifp->if_serializer);
204         if (error) {
205                 device_printf(dev, "awi_pccard_attach: intr setup failed\n");
206                 goto fail;
207         }
208         return 0;
209
210   fail:
211         if (psc->sc_intrhand) {
212                 bus_teardown_intr(dev, psc->sc_irq_res, psc->sc_intrhand);
213                 psc->sc_intrhand = 0;
214         }
215         if (psc->sc_port_res) {
216                 bus_release_resource(dev, SYS_RES_IOPORT, psc->sc_port_rid,
217                     psc->sc_port_res);
218                 psc->sc_port_res = 0;
219         }
220         if (psc->sc_irq_res) {
221                 bus_release_resource(dev, SYS_RES_IRQ, psc->sc_irq_rid,
222                     psc->sc_irq_res);
223                 psc->sc_irq_res = 0;
224         }
225         if (psc->sc_mem_res) {
226                 bus_release_resource(dev, SYS_RES_MEMORY, psc->sc_mem_rid,
227                     psc->sc_mem_res);
228                 psc->sc_mem_res = 0;
229         }
230         if (error == 0)
231                 error = ENXIO;
232         return error;
233 }
234
235 static int
236 awi_pccard_detach(device_t dev)
237 {
238         struct awi_pccard_softc *psc = device_get_softc(dev);
239         struct awi_softc *sc = &psc->sc_awi;
240         struct ifnet *ifp = &sc->sc_ec.ac_if;
241
242         ether_ifdetach(ifp);
243         ifp->if_flags &= ~IFF_RUNNING; 
244         if (psc->sc_intrhand) {
245                 bus_teardown_intr(dev, psc->sc_irq_res, psc->sc_intrhand);
246                 psc->sc_intrhand = 0;
247         }
248         if (psc->sc_port_res) {
249                 bus_release_resource(dev, SYS_RES_IOPORT, psc->sc_port_rid,
250                     psc->sc_port_res);
251                 psc->sc_port_res = 0;
252         }
253         if (psc->sc_irq_res) {
254                 bus_release_resource(dev, SYS_RES_IRQ, psc->sc_irq_rid,
255                     psc->sc_irq_res);
256                 psc->sc_irq_res = 0;
257         }
258         if (psc->sc_mem_res) {
259                 bus_release_resource(dev, SYS_RES_MEMORY, psc->sc_mem_rid,
260                     psc->sc_mem_res);
261                 psc->sc_mem_res = 0;
262         }
263         return 0;
264 }
265
266 static device_method_t awi_pccard_methods[] = {
267         /* Device interface */
268         DEVMETHOD(device_probe,         pccard_compat_probe),
269         DEVMETHOD(device_attach,        pccard_compat_attach),
270         DEVMETHOD(device_detach,        awi_pccard_detach),
271
272         /* Card interface */
273         DEVMETHOD(card_compat_match,    awi_pccard_match),
274         DEVMETHOD(card_compat_probe,    awi_pccard_probe),
275         DEVMETHOD(card_compat_attach,   awi_pccard_attach),
276
277         { 0, 0 }
278 };
279
280 static driver_t awi_pccard_driver = {
281         "awi",
282         awi_pccard_methods,
283         sizeof(struct awi_pccard_softc),
284 };
285
286 extern devclass_t awi_devclass;
287
288 DRIVER_MODULE(if_awi, pccard, awi_pccard_driver, awi_devclass, 0, 0);