Do a major clean-up of the BUSDMA architecture. A large number of
[dragonfly.git] / sys / dev / netif / awi / if_awi_pccard.c
CommitLineData
984263bc
MD
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 $
1f7ab7c9 26 * $DragonFly: src/sys/dev/netif/awi/Attic/if_awi_pccard.c,v 1.17 2006/10/25 20:55:56 dillon Exp $
984263bc
MD
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32#include <sys/socket.h>
984263bc
MD
33#include <sys/module.h>
34#include <sys/bus.h>
984263bc
MD
35#include <sys/rman.h>
36
37#include <net/if.h>
38#include <net/if_arp.h>
39#include <net/if_media.h>
40#include <net/ethernet.h>
485c51eb
JS
41#include <netproto/802_11/ieee80211.h>
42#include <netproto/802_11/ieee80211_ioctl.h>
984263bc
MD
43
44#include <machine/clock.h>
45
1f2de5d4
MD
46#include "am79c930reg.h"
47#include "am79c930var.h"
48#include "awireg.h"
49#include "awivar.h"
984263bc 50
06d02e3b
JS
51#include <bus/pccard/pccardvar.h>
52#include <bus/pccard/pccarddevs.h>
53#include "card_if.h"
54
984263bc
MD
55struct 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
06d02e3b
JS
69static 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
81static int awi_pccard_match(device_t);
82static int awi_pccard_probe(device_t);
83static int awi_pccard_attach(device_t);
84static int awi_pccard_detach(device_t);
06d02e3b
JS
85
86static int
87awi_pccard_match(device_t dev)
88{
89 const struct pccard_product *pp;
90
91 if ((pp = pccard_product_lookup(dev, awi_pccard_products,
92 sizeof(awi_pccard_products[0]), NULL)) != NULL) {
93 if (pp->pp_name != NULL)
94 device_set_desc(dev, pp->pp_name);
95 return 0;
96 }
97 return ENXIO;
98}
99
984263bc
MD
100/*
101 * Initialize the device - called from Slot manager.
102 */
103static int
104awi_pccard_probe(device_t dev)
105{
106 struct awi_pccard_softc *psc = device_get_softc(dev);
107 struct awi_softc *sc = &psc->sc_awi;
108 int error = 0;
109
110 psc->sc_port_rid = 0;
111 psc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
112 &psc->sc_port_rid, 0, ~0, 16, RF_ACTIVE);
113 if (!psc->sc_port_res)
114 return ENOMEM;
115
116 sc->sc_chip.sc_iot = rman_get_bustag(psc->sc_port_res);
117 sc->sc_chip.sc_ioh = rman_get_bushandle(psc->sc_port_res);
118 am79c930_chip_init(&sc->sc_chip, 0);
119 DELAY(1000);
120
121 awi_read_bytes(sc, AWI_BANNER, psc->sc_version, AWI_BANNER_LEN);
122 if (memcmp(psc->sc_version, "PCnetMobile:", 12) != 0) {
123 device_printf(dev, "awi_pccard_probe: bad banner: %12D\n",
124 psc->sc_version, " ");
125 error = ENXIO;
126 } else
127 device_set_desc(dev, psc->sc_version);
128 bus_release_resource(dev, SYS_RES_IOPORT, psc->sc_port_rid,
129 psc->sc_port_res);
130
131 return error;
132}
133
134static int
135awi_pccard_attach(device_t dev)
136{
137 struct awi_pccard_softc *psc = device_get_softc(dev);
138 struct awi_softc *sc = &psc->sc_awi;
139 struct ifnet *ifp = &sc->sc_ec.ac_if;
140 int error = 0;
141
142 psc->sc_port_res = 0;
143 psc->sc_irq_res = 0;
144 psc->sc_mem_res = 0;
145 psc->sc_intrhand = 0;
146
4372b8e4 147 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
984263bc
MD
148
149 psc->sc_port_rid = 0;
150 psc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
151 &psc->sc_port_rid, 0, ~0, 16, RF_ACTIVE);
152 if (!psc->sc_port_res) {
153 device_printf(dev, "awi_pccard_attach: port alloc failed\n");
154 goto fail;
155 }
156 sc->sc_chip.sc_iot = rman_get_bustag(psc->sc_port_res);
157 sc->sc_chip.sc_ioh = rman_get_bushandle(psc->sc_port_res);
158
159 psc->sc_irq_rid = 0;
4e6d744d
JS
160 psc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
161 &psc->sc_irq_rid, RF_ACTIVE);
984263bc
MD
162 if (!psc->sc_irq_res) {
163 device_printf(dev, "awi_pccard_attach: irq alloc failed\n");
164 goto fail;
165 }
166
167 psc->sc_mem_rid = 0;
168#if 1
169 /*
170 * XXX: awi needs to access memory with 8bit,
171 * but pccardd apparently maps memory with MDF_16BITS flag.
172 * So memory mapped access is disabled and use IO port instead.
173 */
174 psc->sc_mem_res = 0;
175#else
176 psc->sc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
177 &psc->sc_mem_rid, 0, ~0, 0x8000, RF_ACTIVE);
178#endif
179 if (psc->sc_mem_res) {
180 sc->sc_chip.sc_memt = rman_get_bustag(psc->sc_mem_res);
181 sc->sc_chip.sc_memh = rman_get_bushandle(psc->sc_mem_res);
182 am79c930_chip_init(&sc->sc_chip, 1);
183 } else
184 am79c930_chip_init(&sc->sc_chip, 0);
185
984263bc
MD
186 sc->sc_enabled = 1;
187 sc->sc_ifp = &sc->sc_ec.ac_if;
188
189 error = awi_attach(sc);
190 sc->sc_enabled = 0; /*XXX*/
78195a76
MD
191
192 if (error) {
193 device_printf(dev, "awi_pccard_attach: awi_attach failed\n");
194 goto fail;
195 }
196
197 error = bus_setup_intr(dev, psc->sc_irq_res, INTR_NETSAFE,
198 (void (*)(void *))awi_intr, sc,
199 &psc->sc_intrhand, sc->sc_ifp->if_serializer);
200 if (error) {
201 device_printf(dev, "awi_pccard_attach: intr setup failed\n");
202 goto fail;
203 }
204 return 0;
984263bc
MD
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
231static int
232awi_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
4d74b9d5 238 lwkt_serialize_enter(ifp->if_serializer);
984263bc
MD
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 }
4d74b9d5 244 lwkt_serialize_exit(ifp->if_serializer);
cdf89432
SZ
245
246 ether_ifdetach(ifp);
247
984263bc
MD
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
266static device_method_t awi_pccard_methods[] = {
267 /* Device interface */
06d02e3b
JS
268 DEVMETHOD(device_probe, pccard_compat_probe),
269 DEVMETHOD(device_attach, pccard_compat_attach),
984263bc
MD
270 DEVMETHOD(device_detach, awi_pccard_detach),
271
06d02e3b
JS
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
984263bc
MD
277 { 0, 0 }
278};
279
280static driver_t awi_pccard_driver = {
281 "awi",
282 awi_pccard_methods,
283 sizeof(struct awi_pccard_softc),
284};
285
286extern devclass_t awi_devclass;
287
32832096 288DRIVER_MODULE(if_awi, pccard, awi_pccard_driver, awi_devclass, 0, 0);