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