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