xref: /netbsd/sys/dev/pcmcia/if_cnw.c (revision 22dee55a)
1 /*	$NetBSD: if_cnw.c,v 1.68 2020/01/29 13:54:41 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Michael Eriksson.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1996, 1997 Berkeley Software Design, Inc.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that this notice is retained,
38  * the conditions in the following notices are met, and terms applying
39  * to contributors in the following notices also apply to Berkeley
40  * Software Design, Inc.
41  *
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *      This product includes software developed by
50  *	Berkeley Software Design, Inc.
51  * 4. Neither the name of the Berkeley Software Design, Inc. nor the names
52  *    of its contributors may be used to endorse or promote products derived
53  *    from this software without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  * Paul Borman, December 1996
68  *
69  * This driver is derived from a generic frame work which is
70  * Copyright(c) 1994,1995,1996
71  * Yoichi Shinoda, Yoshitaka Tokugawa, WIDE Project, Wildboar Project
72  * and Foretune.  All rights reserved.
73  *
74  * A linux driver was used as the "hardware reference manual" (i.e.,
75  * to determine registers and a general outline of how the card works)
76  * That driver is publically available and copyright
77  *
78  * John Markus Bj�rndalen
79  * Department of Computer Science
80  * University of Troms�
81  * Norway
82  * johnm@staff.cs.uit.no, http://www.cs.uit.no/~johnm/
83  */
84 
85 /*
86  * This is a driver for the Xircom CreditCard Netwave (also known as
87  * the Netwave Airsurfer) wireless LAN PCMCIA adapter.
88  *
89  * When this driver was developed, the Linux Netwave driver was used
90  * as a hardware manual. That driver is Copyright (c) 1997 University
91  * of Troms�, Norway. It is part of the Linux pcmcia-cs package that
92  * can be found at http://pcmcia-cs.sourceforge.net/. The most recent
93  * version of the pcmcia-cs package when this driver was written was
94  * 3.0.6.
95  *
96  * Unfortunately, a lot of explicit numeric constants were used in the
97  * Linux driver. I have tried to use symbolic names whenever possible,
98  * but since I don't have any real hardware documentation, there's
99  * still one or two "magic numbers" :-(.
100  *
101  * Driver limitations: This driver doesn't do multicasting or receiver
102  * promiscuity, because of missing hardware documentation. I couldn't
103  * get receiver promiscuity to work, and I haven't even tried
104  * multicast. Volunteers are welcome, of course :-).
105  */
106 
107 #include <sys/cdefs.h>
108 __KERNEL_RCSID(0, "$NetBSD: if_cnw.c,v 1.68 2020/01/29 13:54:41 thorpej Exp $");
109 
110 #include "opt_inet.h"
111 
112 #include <sys/param.h>
113 #include <sys/systm.h>
114 #include <sys/device.h>
115 #include <sys/socket.h>
116 #include <sys/mbuf.h>
117 #include <sys/ioctl.h>
118 #include <sys/proc.h>
119 #include <sys/kauth.h>
120 
121 #include <net/if.h>
122 
123 #include <dev/pcmcia/if_cnwreg.h>
124 #include <dev/pcmcia/if_cnwioctl.h>
125 
126 #include <dev/pcmcia/pcmciareg.h>
127 #include <dev/pcmcia/pcmciavar.h>
128 #include <dev/pcmcia/pcmciadevs.h>
129 
130 #include <net/if_dl.h>
131 #include <net/if_ether.h>
132 #include <net/bpf.h>
133 
134 #ifdef INET
135 #include <netinet/in.h>
136 #include <netinet/in_systm.h>
137 #include <netinet/in_var.h>
138 #include <netinet/ip.h>
139 #include <netinet/if_inarp.h>
140 #endif
141 
142 /*
143  * Let these be patchable variables, initialized from macros that can
144  * be set in the kernel config file. Someone with lots of spare time
145  * could probably write a nice Netwave configuration program to do
146  * this a little bit more elegantly :-).
147  */
148 #ifndef CNW_DOMAIN
149 #define CNW_DOMAIN	0x100
150 #endif
151 int cnw_domain = CNW_DOMAIN;		/* Domain */
152 #ifndef CNW_SCRAMBLEKEY
153 #define CNW_SCRAMBLEKEY 0
154 #endif
155 int cnw_skey = CNW_SCRAMBLEKEY;		/* Scramble key */
156 
157 /*
158  * The card appears to work much better when we only allow one packet
159  * "in the air" at a time.  This is done by not allowing another packet
160  * on the card, even if there is room.  Turning this off will allow the
161  * driver to stuff packets on the card as soon as a transmit buffer is
162  * available.  This does increase the number of collisions, though.
163  * We can que a second packet if there are transmit buffers available,
164  * but we do not actually send the packet until the last packet has
165  * been written.
166  */
167 #define	ONE_AT_A_TIME
168 
169 /*
170  * Netwave cards choke if we try to use io memory address >= 0x400.
171  * Even though, CIS tuple does not talk about this.
172  * Use memory mapped access.
173  */
174 #define MEMORY_MAPPED
175 
176 static int	cnw_match(device_t, cfdata_t, void *);
177 static void	cnw_attach(device_t, device_t, void *);
178 static int	cnw_detach(device_t, int);
179 
180 static int	cnw_activate(device_t, enum devact);
181 
182 struct cnw_softc {
183 	device_t sc_dev;		    /* Device glue (must be first) */
184 	struct ethercom sc_ethercom;	    /* Ethernet common part */
185 	int sc_domain;			    /* Netwave domain */
186 	int sc_skey;			    /* Netwave scramble key */
187 	struct cnwstats sc_stats;
188 
189 	/* PCMCIA-specific stuff */
190 	struct pcmcia_function *sc_pf;	    /* PCMCIA function */
191 #ifndef MEMORY_MAPPED
192 	struct pcmcia_io_handle sc_pcioh;   /* PCMCIA I/O space handle */
193 	int sc_iowin;			    /*   ...window */
194 	bus_space_tag_t sc_iot;		    /*   ...bus_space tag */
195 	bus_space_handle_t sc_ioh;	    /*   ...bus_space handle */
196 #endif
197 	struct pcmcia_mem_handle sc_pcmemh; /* PCMCIA memory handle */
198 	bus_size_t sc_memoff;		    /*   ...offset */
199 	int sc_memwin;			    /*   ...window */
200 	bus_space_tag_t sc_memt;	    /*   ...bus_space tag */
201 	bus_space_handle_t sc_memh;	    /*   ...bus_space handle */
202 	void *sc_ih;			    /* Interrupt cookie */
203 	struct timeval sc_txlast;	    /* When the last xmit was made */
204 	int sc_active;			    /* Currently xmitting a packet */
205 
206 	int sc_resource;		    /* Resources alloc'ed on attach */
207 #define CNW_RES_PCIC	1
208 #define CNW_RES_IO	2
209 #define CNW_RES_MEM	4
210 #define CNW_RES_NET	8
211 };
212 
213 CFATTACH_DECL_NEW(cnw, sizeof(struct cnw_softc),
214     cnw_match, cnw_attach, cnw_detach, cnw_activate);
215 
216 static void cnw_reset(struct cnw_softc *);
217 static void cnw_init(struct cnw_softc *);
218 static int cnw_enable(struct cnw_softc *sc);
219 static void cnw_disable(struct cnw_softc *sc);
220 static void cnw_start(struct ifnet *);
221 static void cnw_transmit(struct cnw_softc *, struct mbuf *);
222 static struct mbuf *cnw_read(struct cnw_softc *);
223 static void cnw_recv(struct cnw_softc *);
224 static int cnw_intr(void *arg);
225 static int cnw_ioctl(struct ifnet *, u_long, void *);
226 static void cnw_watchdog(struct ifnet *);
227 static int cnw_setdomain(struct cnw_softc *, int);
228 static int cnw_setkey(struct cnw_softc *, int);
229 
230 /* ---------------------------------------------------------------- */
231 
232 /* Help routines */
233 static int wait_WOC(struct cnw_softc *, int);
234 static int read16(struct cnw_softc *, int);
235 static int cnw_cmd(struct cnw_softc *, int, int, int, int);
236 
237 /*
238  * Wait until the WOC (Write Operation Complete) bit in the
239  * ASR (Adapter Status Register) is asserted.
240  */
241 static int
wait_WOC(struct cnw_softc * sc,int line)242 wait_WOC(struct cnw_softc *sc, int line)
243 {
244 	int i, asr;
245 
246 	for (i = 0; i < 5000; i++) {
247 #ifndef MEMORY_MAPPED
248 		asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
249 #else
250 		asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
251 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
252 #endif
253 		if (asr & CNW_ASR_WOC)
254 			return (0);
255 		DELAY(100);
256 	}
257 	if (line > 0)
258 		printf("%s: wedged at line %d\n", device_xname(sc->sc_dev), line);
259 	return (1);
260 }
261 #define WAIT_WOC(sc) wait_WOC(sc, __LINE__)
262 
263 
264 /*
265  * Read a 16 bit value from the card.
266  */
267 static int
read16(struct cnw_softc * sc,int offset)268 read16(struct cnw_softc *sc, int offset)
269 {
270 	int hi, lo;
271 	int offs = sc->sc_memoff + offset;
272 
273 	/* This could presumably be done more efficient with
274 	 * bus_space_read_2(), but I don't know anything about the
275 	 * byte sex guarantees... Besides, this is pretty cheap as
276 	 * well :-)
277 	 */
278 	lo = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs);
279 	hi = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs + 1);
280 	return ((hi << 8) | lo);
281 }
282 
283 
284 /*
285  * Send a command to the card by writing it to the command buffer.
286  */
287 int
cnw_cmd(struct cnw_softc * sc,int cmd,int count,int arg1,int arg2)288 cnw_cmd(struct cnw_softc *sc, int cmd, int count, int arg1, int arg2)
289 {
290 	int ptr = sc->sc_memoff + CNW_EREG_CB;
291 
292 	if (wait_WOC(sc, 0)) {
293 		printf("%s: wedged when issuing cmd 0x%x\n",
294 		    device_xname(sc->sc_dev), cmd);
295 		/*
296 		 * We'll continue anyway, as that's probably the best
297 		 * thing we can do; at least the user knows there's a
298 		 * problem, and can reset the interface with ifconfig
299 		 * down/up.
300 		 */
301 	}
302 
303 	bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr, cmd);
304 	if (count > 0) {
305 		bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr + 1, arg1);
306 		if (count > 1)
307 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
308 			    ptr + 2, arg2);
309 	}
310 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
311 	    ptr + count + 1, CNW_CMD_EOC);
312 	return (0);
313 }
314 #define CNW_CMD0(sc, cmd) \
315     do { cnw_cmd(sc, cmd, 0, 0, 0); } while (0)
316 #define CNW_CMD1(sc, cmd, arg1)	\
317     do { cnw_cmd(sc, cmd, 1, arg1 , 0); } while (0)
318 #define CNW_CMD2(sc, cmd, arg1, arg2) \
319     do { cnw_cmd(sc, cmd, 2, arg1, arg2); } while (0)
320 
321 /* ---------------------------------------------------------------- */
322 
323 /*
324  * Reset the hardware.
325  */
326 static void
cnw_reset(struct cnw_softc * sc)327 cnw_reset(struct cnw_softc *sc)
328 {
329 #ifdef CNW_DEBUG
330 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
331 		printf("%s: resetting\n", device_xname(sc->sc_dev));
332 #endif
333 	wait_WOC(sc, 0);
334 #ifndef MEMORY_MAPPED
335 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, CNW_PMR_RESET);
336 #else
337 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
338 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, CNW_PMR_RESET);
339 #endif
340 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
341 	    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_WOC);
342 #ifndef MEMORY_MAPPED
343 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, 0);
344 #else
345 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
346 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, 0);
347 #endif
348 }
349 
350 
351 /*
352  * Initialize the card.
353  */
354 static void
cnw_init(struct cnw_softc * sc)355 cnw_init(struct cnw_softc *sc)
356 {
357 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
358 	const u_int8_t rxmode =
359 	    CNW_RXCONF_RXENA | CNW_RXCONF_BCAST | CNW_RXCONF_AMP;
360 
361 	/* Reset the card */
362 	cnw_reset(sc);
363 
364 	/* Issue a NOP to check the card */
365 	CNW_CMD0(sc, CNW_CMD_NOP);
366 
367 	/* Set up receive configuration */
368 	CNW_CMD1(sc, CNW_CMD_SRC,
369 	    rxmode | ((ifp->if_flags & IFF_PROMISC) ? CNW_RXCONF_PRO : 0));
370 
371 	/* Set up transmit configuration */
372 	CNW_CMD1(sc, CNW_CMD_STC, CNW_TXCONF_TXENA);
373 
374 	/* Set domain */
375 	CNW_CMD2(sc, CNW_CMD_SMD, sc->sc_domain, sc->sc_domain >> 8);
376 
377 	/* Set scramble key */
378 	CNW_CMD2(sc, CNW_CMD_SSK, sc->sc_skey, sc->sc_skey >> 8);
379 
380 	/* Enable interrupts */
381 	WAIT_WOC(sc);
382 #ifndef MEMORY_MAPPED
383 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
384 	    CNW_REG_IMR, CNW_IMR_IENA | CNW_IMR_RFU1);
385 #else
386 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
387 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_IMR,
388 	    CNW_IMR_IENA | CNW_IMR_RFU1);
389 #endif
390 
391 	/* Enable receiver */
392 	CNW_CMD0(sc, CNW_CMD_ER);
393 
394 	/* "Set the IENA bit in COR" */
395 	WAIT_WOC(sc);
396 #ifndef MEMORY_MAPPED
397 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_COR,
398 	    CNW_COR_IENA | CNW_COR_LVLREQ);
399 #else
400 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
401 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_COR,
402 	    CNW_COR_IENA | CNW_COR_LVLREQ);
403 #endif
404 }
405 
406 
407 /*
408  * Enable and initialize the card.
409  */
410 static int
cnw_enable(struct cnw_softc * sc)411 cnw_enable(struct cnw_softc *sc)
412 {
413 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
414 
415 	if ((ifp->if_flags & IFF_RUNNING) != 0)
416 		return (0);
417 
418 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET, cnw_intr, sc);
419 	if (sc->sc_ih == NULL) {
420 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt handler\n");
421 		return (EIO);
422 	}
423 	if (pcmcia_function_enable(sc->sc_pf) != 0) {
424 		aprint_error_dev(sc->sc_dev, "couldn't enable card\n");
425 		return (EIO);
426 	}
427 	sc->sc_resource |= CNW_RES_PCIC;
428 	cnw_init(sc);
429 	ifp->if_flags &= ~IFF_OACTIVE;
430 	ifp->if_flags |= IFF_RUNNING;
431 	return (0);
432 }
433 
434 
435 /*
436  * Stop and disable the card.
437  */
438 static void
cnw_disable(struct cnw_softc * sc)439 cnw_disable(struct cnw_softc *sc)
440 {
441 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
442 
443 	if ((ifp->if_flags & IFF_RUNNING) == 0)
444 		return;
445 
446 	pcmcia_function_disable(sc->sc_pf);
447 	sc->sc_resource &= ~CNW_RES_PCIC;
448 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
449 	ifp->if_flags &= ~IFF_RUNNING;
450 	ifp->if_timer = 0;
451 }
452 
453 
454 /*
455  * Match the hardware we handle.
456  */
457 static int
cnw_match(device_t parent,cfdata_t match,void * aux)458 cnw_match(device_t parent, cfdata_t match, void *aux)
459 {
460 	struct pcmcia_attach_args *pa = aux;
461 
462 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
463 	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_801)
464 		return 1;
465 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
466 	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_802)
467 		return 1;
468 	return 0;
469 }
470 
471 
472 /*
473  * Attach the card.
474  */
475 static void
cnw_attach(device_t parent,device_t self,void * aux)476 cnw_attach(device_t parent, device_t self, void *aux)
477 {
478 	struct cnw_softc *sc = device_private(self);
479 	struct pcmcia_attach_args *pa = aux;
480 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
481 	u_int8_t macaddr[ETHER_ADDR_LEN];
482 	int i;
483 	bus_size_t memsize;
484 
485 	sc->sc_dev = self;
486 	sc->sc_resource = 0;
487 
488 	/* Enable the card */
489 	sc->sc_pf = pa->pf;
490 	pcmcia_function_init(sc->sc_pf, SIMPLEQ_FIRST(&sc->sc_pf->cfe_head));
491 	if (pcmcia_function_enable(sc->sc_pf)) {
492 		aprint_error_dev(self, "function enable failed\n");
493 		return;
494 	}
495 	sc->sc_resource |= CNW_RES_PCIC;
496 
497 	/* Map I/O register and "memory" */
498 #ifndef MEMORY_MAPPED
499 	if (pcmcia_io_alloc(sc->sc_pf, 0, CNW_IO_SIZE, CNW_IO_SIZE,
500 	    &sc->sc_pcioh) != 0) {
501 		aprint_error_dev(self, "can't allocate i/o space\n");
502 		goto fail;
503 	}
504 	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO16, &sc->sc_pcioh,
505 	    &sc->sc_iowin) != 0) {
506 		aprint_error_dev(self, "can't map i/o space\n");
507 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
508 		goto fail;
509 	}
510 	sc->sc_iot = sc->sc_pcioh.iot;
511 	sc->sc_ioh = sc->sc_pcioh.ioh;
512 	sc->sc_resource |= CNW_RES_IO;
513 #endif
514 #ifndef MEMORY_MAPPED
515 	memsize = CNW_MEM_SIZE;
516 #else
517 	memsize = CNW_MEM_SIZE + CNW_IOM_SIZE;
518 #endif
519 	if (pcmcia_mem_alloc(sc->sc_pf, memsize, &sc->sc_pcmemh) != 0) {
520 		aprint_error_dev(self, "can't allocate memory\n");
521 		goto fail;
522 	}
523 	if (pcmcia_mem_map(sc->sc_pf, PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON,
524 	    CNW_MEM_ADDR, memsize, &sc->sc_pcmemh, &sc->sc_memoff,
525 	    &sc->sc_memwin) != 0) {
526 		aprint_error_dev(self, "can't map memory\n");
527 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
528 		goto fail;
529 	}
530 	sc->sc_memt = sc->sc_pcmemh.memt;
531 	sc->sc_memh = sc->sc_pcmemh.memh;
532 	sc->sc_resource |= CNW_RES_MEM;
533 
534 	/* Finish setup of softc */
535 	sc->sc_domain = cnw_domain;
536 	sc->sc_skey = cnw_skey;
537 
538 	/* Get MAC address */
539 	cnw_reset(sc);
540 	for (i = 0; i < ETHER_ADDR_LEN; i++)
541 		macaddr[i] = bus_space_read_1(sc->sc_memt, sc->sc_memh,
542 		    sc->sc_memoff + CNW_EREG_PA + i);
543 	printf("%s: address %s\n", device_xname(sc->sc_dev),
544 	    ether_sprintf(macaddr));
545 
546 	/* Set up ifnet structure */
547 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
548 	ifp->if_softc = sc;
549 	ifp->if_start = cnw_start;
550 	ifp->if_ioctl = cnw_ioctl;
551 	ifp->if_watchdog = cnw_watchdog;
552 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;
553 	IFQ_SET_READY(&ifp->if_snd);
554 
555 	/* Attach the interface */
556 	if_attach(ifp);
557 	if_deferred_start_init(ifp, NULL);
558 	ether_ifattach(ifp, macaddr);
559 
560 	sc->sc_resource |= CNW_RES_NET;
561 
562 	ifp->if_baudrate = IF_Mbps(1);
563 
564 	/* Disable the card now, and turn it on when the interface goes up */
565 	pcmcia_function_disable(sc->sc_pf);
566 	sc->sc_resource &= ~CNW_RES_PCIC;
567 	return;
568 
569 fail:
570 #ifndef MEMORY_MAPPED
571 	if ((sc->sc_resource & CNW_RES_IO) != 0) {
572 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
573 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
574 		sc->sc_resource &= ~CNW_RES_IO;
575 	}
576 #endif
577 	if ((sc->sc_resource & CNW_RES_PCIC) != 0) {
578 		pcmcia_function_disable(sc->sc_pf);
579 		sc->sc_resource &= ~CNW_RES_PCIC;
580 	}
581 }
582 
583 /*
584  * Start outputting on the interface.
585  */
586 static void
cnw_start(struct ifnet * ifp)587 cnw_start(struct ifnet *ifp)
588 {
589 	struct cnw_softc *sc = ifp->if_softc;
590 	struct mbuf *m0;
591 	int lif;
592 	int asr;
593 #ifdef ONE_AT_A_TIME
594 	struct timeval now;
595 #endif
596 
597 #ifdef CNW_DEBUG
598 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
599 		printf("%s: cnw_start\n", ifp->if_xname);
600 	if (ifp->if_flags & IFF_OACTIVE)
601 		printf("%s: cnw_start reentered\n", ifp->if_xname);
602 #endif
603 
604 	ifp->if_flags |= IFF_OACTIVE;
605 
606 	for (;;) {
607 #ifdef ONE_AT_A_TIME
608 		microtime(&now);
609 		now.tv_sec -= sc->sc_txlast.tv_sec;
610 		now.tv_usec -= sc->sc_txlast.tv_usec;
611 		if (now.tv_usec < 0) {
612 			now.tv_usec += 1000000;
613 			now.tv_sec--;
614 		}
615 
616 		/*
617 		 * Don't ship this packet out until the last
618 		 * packet has left the building.
619 		 * If we have not tried to send a packet for 1/5
620 		 * a second then we assume we lost an interrupt,
621 		 * lets go on and send the next packet anyhow.
622 		 *
623 		 * I suppose we could check to see if it is okay
624 		 * to put additional packets on the card (beyond
625 		 * the one already waiting to be sent) but I don't
626 		 * think we would get any improvement in speed as
627 		 * we should have ample time to put the next packet
628 		 * on while this one is going out.
629 		 */
630 		if (sc->sc_active && now.tv_sec == 0 && now.tv_usec < 200000)
631 			break;
632 #endif
633 
634 		/* Make sure the link integrity field is on */
635 		WAIT_WOC(sc);
636 		lif = bus_space_read_1(sc->sc_memt, sc->sc_memh,
637 		    sc->sc_memoff + CNW_EREG_LIF);
638 		if (lif == 0) {
639 #ifdef CNW_DEBUG
640 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
641 				printf("%s: link integrity %d\n", ifp->if_xname, lif);
642 #endif
643 			break;
644 		}
645 
646 		/* Is there any buffer space available on the card? */
647 		WAIT_WOC(sc);
648 #ifndef MEMORY_MAPPED
649 		asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
650 #else
651 		asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
652 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
653 #endif
654 		if (!(asr & CNW_ASR_TXBA)) {
655 #ifdef CNW_DEBUG
656 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
657 				printf("%s: no buffer space\n", ifp->if_xname);
658 #endif
659 			break;
660 		}
661 
662 		sc->sc_stats.nws_tx++;
663 
664 		IFQ_DEQUEUE(&ifp->if_snd, m0);
665 		if (m0 == 0)
666 			break;
667 
668 		bpf_mtap(ifp, m0, BPF_D_OUT);
669 
670 		cnw_transmit(sc, m0);
671 		if_statinc(ifp, if_opackets);
672 		ifp->if_timer = 3; /* start watchdog timer */
673 
674 		microtime(&sc->sc_txlast);
675 		sc->sc_active = 1;
676 	}
677 
678 	ifp->if_flags &= ~IFF_OACTIVE;
679 }
680 
681 /*
682  * Transmit a packet.
683  */
684 static void
cnw_transmit(struct cnw_softc * sc,struct mbuf * m0)685 cnw_transmit(struct cnw_softc *sc, struct mbuf *m0)
686 {
687 	int buffer, bufsize, bufoffset, bufptr, bufspace, len, mbytes, n;
688 	struct mbuf *m;
689 	u_int8_t *mptr;
690 
691 	/* Get buffer info from card */
692 	buffer = read16(sc, CNW_EREG_TDP);
693 	bufsize = read16(sc, CNW_EREG_TDP + 2);
694 	bufoffset = read16(sc, CNW_EREG_TDP + 4);
695 #ifdef CNW_DEBUG
696 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
697 		printf("%s: cnw_transmit b=0x%x s=%d o=0x%x\n",
698 		    device_xname(sc->sc_dev), buffer, bufsize, bufoffset);
699 #endif
700 
701 	/* Copy data from mbuf chain to card buffers */
702 	bufptr = sc->sc_memoff + buffer + bufoffset;
703 	bufspace = bufsize;
704 	len = 0;
705 	for (m = m0; m; ) {
706 		mptr = mtod(m, u_int8_t *);
707 		mbytes = m->m_len;
708 		len += mbytes;
709 		while (mbytes > 0) {
710 			if (bufspace == 0) {
711 				buffer = read16(sc, buffer);
712 				bufptr = sc->sc_memoff + buffer + bufoffset;
713 				bufspace = bufsize;
714 #ifdef CNW_DEBUG
715 				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
716 					printf("%s:   next buffer @0x%x\n",
717 					    device_xname(sc->sc_dev), buffer);
718 #endif
719 			}
720 			n = mbytes <= bufspace ? mbytes : bufspace;
721 			bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
722 			    bufptr, mptr, n);
723 			bufptr += n;
724 			bufspace -= n;
725 			mptr += n;
726 			mbytes -= n;
727 		}
728 		m = m0 = m_free(m);
729 	}
730 
731 	/* Issue transmit command */
732 	CNW_CMD2(sc, CNW_CMD_TL, len, len >> 8);
733 }
734 
735 
736 /*
737  * Pull a packet from the card into an mbuf chain.
738  */
739 static struct mbuf *
cnw_read(struct cnw_softc * sc)740 cnw_read(struct cnw_softc *sc)
741 {
742 	struct mbuf *m, *top, **mp;
743 	int totbytes, buffer, bufbytes, bufptr, mbytes, n;
744 	u_int8_t *mptr;
745 
746 	WAIT_WOC(sc);
747 	totbytes = read16(sc, CNW_EREG_RDP);
748 #ifdef CNW_DEBUG
749 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
750 		printf("%s: recv %d bytes\n", device_xname(sc->sc_dev), totbytes);
751 #endif
752 	buffer = CNW_EREG_RDP + 2;
753 	bufbytes = 0;
754 	bufptr = 0; /* XXX make gcc happy */
755 
756 	MGETHDR(m, M_DONTWAIT, MT_DATA);
757 	if (m == 0)
758 		return (0);
759 	m_set_rcvif(m, &sc->sc_ethercom.ec_if);
760 	m->m_pkthdr.len = totbytes;
761 	mbytes = MHLEN;
762 	top = 0;
763 	mp = &top;
764 
765 	while (totbytes > 0) {
766 		if (top) {
767 			MGET(m, M_DONTWAIT, MT_DATA);
768 			if (m == 0) {
769 				m_freem(top);
770 				return (0);
771 			}
772 			mbytes = MLEN;
773 		}
774 		if (totbytes >= MINCLSIZE) {
775 			MCLGET(m, M_DONTWAIT);
776 			if ((m->m_flags & M_EXT) == 0) {
777 				m_free(m);
778 				m_freem(top);
779 				return (0);
780 			}
781 			mbytes = MCLBYTES;
782 		}
783 		if (!top) {
784 			int pad = ALIGN(sizeof(struct ether_header)) -
785 			    sizeof(struct ether_header);
786 			m->m_data += pad;
787 			mbytes -= pad;
788 		}
789 		mptr = mtod(m, u_int8_t *);
790 		mbytes = m->m_len = uimin(totbytes, mbytes);
791 		totbytes -= mbytes;
792 		while (mbytes > 0) {
793 			if (bufbytes == 0) {
794 				buffer = read16(sc, buffer);
795 				bufbytes = read16(sc, buffer + 2);
796 				bufptr = sc->sc_memoff + buffer +
797 				    read16(sc, buffer + 4);
798 #ifdef CNW_DEBUG
799 				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
800 					printf("%s:   %d bytes @0x%x+0x%lx\n",
801 					    device_xname(sc->sc_dev), bufbytes,
802 					    buffer, (u_long)(bufptr - buffer -
803 					    sc->sc_memoff));
804 #endif
805 			}
806 			n = mbytes <= bufbytes ? mbytes : bufbytes;
807 			bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
808 			    bufptr, mptr, n);
809 			bufbytes -= n;
810 			bufptr += n;
811 			mbytes -= n;
812 			mptr += n;
813 		}
814 		*mp = m;
815 		mp = &m->m_next;
816 	}
817 
818 	return (top);
819 }
820 
821 
822 /*
823  * Handle received packets.
824  */
825 static void
cnw_recv(struct cnw_softc * sc)826 cnw_recv(struct cnw_softc *sc)
827 {
828 	int rser;
829 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
830 	struct mbuf *m;
831 
832 	for (;;) {
833 		WAIT_WOC(sc);
834 		rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
835 		    sc->sc_memoff + CNW_EREG_RSER);
836 		if (!(rser & CNW_RSER_RXAVAIL))
837 			return;
838 
839 		/* Pull packet off card */
840 		m = cnw_read(sc);
841 
842 		/* Acknowledge packet */
843 		CNW_CMD0(sc, CNW_CMD_SRP);
844 
845 		/* Did we manage to get the packet from the interface? */
846 		if (m == 0) {
847 			if_statinc(ifp, if_ierrors);
848 			return;
849 		}
850 
851 		/* Pass the packet up. */
852 		if_percpuq_enqueue(ifp->if_percpuq, m);
853 	}
854 }
855 
856 
857 /*
858  * Interrupt handler.
859  */
860 static int
cnw_intr(void * arg)861 cnw_intr(void *arg)
862 {
863 	struct cnw_softc *sc = arg;
864 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
865 	int ret, status, rser, tser;
866 
867 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0 ||
868 	    !device_is_active(sc->sc_dev))
869 		return (0);
870 	ifp->if_timer = 0;	/* stop watchdog timer */
871 
872 	ret = 0;
873 	for (;;) {
874 		WAIT_WOC(sc);
875 #ifndef MEMORY_MAPPED
876 		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
877 		    CNW_REG_CCSR);
878 #else
879 		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
880 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_CCSR);
881 #endif
882 		if (!(status & 0x02))
883 			/* No more commands, or shared IRQ */
884 			return (ret);
885 		ret = 1;
886 #ifndef MEMORY_MAPPED
887 		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
888 #else
889 		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
890 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
891 #endif
892 
893 		/* Anything to receive? */
894 		if (status & CNW_ASR_RXRDY) {
895 			sc->sc_stats.nws_rx++;
896 			cnw_recv(sc);
897 		}
898 
899 		/* Receive error */
900 		if (status & CNW_ASR_RXERR) {
901 			/*
902 			 * I get a *lot* of spurious receive errors
903 			 * (many per second), even when the interface
904 			 * is quiescent, so we don't increment
905 			 * if_ierrors here.
906 			 */
907 			rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
908 			    sc->sc_memoff + CNW_EREG_RSER);
909 
910 			/* RX statistics */
911 			sc->sc_stats.nws_rxerr++;
912 			if (rser & CNW_RSER_RXBIG)
913 				sc->sc_stats.nws_rxframe++;
914 			if (rser & CNW_RSER_RXCRC)
915 				sc->sc_stats.nws_rxcrcerror++;
916 			if (rser & CNW_RSER_RXOVERRUN)
917 				sc->sc_stats.nws_rxoverrun++;
918 			if (rser & CNW_RSER_RXOVERFLOW)
919 				sc->sc_stats.nws_rxoverflow++;
920 			if (rser & CNW_RSER_RXERR)
921 				sc->sc_stats.nws_rxerrors++;
922 			if (rser & CNW_RSER_RXAVAIL)
923 				sc->sc_stats.nws_rxavail++;
924 
925 			/* Clear error bits in RSER */
926 			WAIT_WOC(sc);
927 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
928 			    sc->sc_memoff + CNW_EREG_RSERW,
929 			    CNW_RSER_RXERR |
930 			    (rser & (CNW_RSER_RXCRC | CNW_RSER_RXBIG)));
931 			/* Clear RXERR in ASR */
932 			WAIT_WOC(sc);
933 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
934 			    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_RXERR);
935 		}
936 
937 		/* Transmit done */
938 		if (status & CNW_ASR_TXDN) {
939 			tser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
940 						CNW_EREG_TSER);
941 
942 			/* TX statistics */
943 			if (tser & CNW_TSER_TXERR)
944 				sc->sc_stats.nws_txerrors++;
945 			if (tser & CNW_TSER_TXNOAP)
946 				sc->sc_stats.nws_txlostcd++;
947 			if (tser & CNW_TSER_TXGU)
948 				sc->sc_stats.nws_txabort++;
949 
950 			if (tser & CNW_TSER_TXOK) {
951 				sc->sc_stats.nws_txokay++;
952 				sc->sc_stats.nws_txretries[status & 0xf]++;
953 				WAIT_WOC(sc);
954 				bus_space_write_1(sc->sc_memt, sc->sc_memh,
955 				    sc->sc_memoff + CNW_EREG_TSERW,
956 				    CNW_TSER_TXOK | CNW_TSER_RTRY);
957 			}
958 
959 			if (tser & CNW_TSER_ERROR) {
960 				if_statinc(ifp, if_oerrors);
961 				WAIT_WOC(sc);
962 				bus_space_write_1(sc->sc_memt, sc->sc_memh,
963 				    sc->sc_memoff + CNW_EREG_TSERW,
964 				    (tser & CNW_TSER_ERROR) |
965 				    CNW_TSER_RTRY);
966 			}
967 
968 			sc->sc_active = 0;
969 			ifp->if_flags &= ~IFF_OACTIVE;
970 
971 			/* Continue to send packets from the queue */
972 			if_schedule_deferred_start(&sc->sc_ethercom.ec_if);
973 		}
974 
975 	}
976 }
977 
978 
979 /*
980  * Handle device ioctls.
981  */
982 static int
cnw_ioctl(struct ifnet * ifp,u_long cmd,void * data)983 cnw_ioctl(struct ifnet *ifp, u_long cmd, void *data)
984 {
985 	struct cnw_softc *sc = ifp->if_softc;
986 	struct ifaddr *ifa = (struct ifaddr *)data;
987 	struct ifreq *ifr = (struct ifreq *)data;
988 	int s, error = 0;
989 	struct lwp *l = curlwp;	/*XXX*/
990 
991 	switch (cmd) {
992 	case SIOCINITIFADDR:
993 	case SIOCSIFFLAGS:
994 	case SIOCADDMULTI:
995 	case SIOCDELMULTI:
996 	case SIOCGCNWDOMAIN:
997 	case SIOCGCNWSTATS:
998 		break;
999 	case SIOCSCNWDOMAIN:
1000 	case SIOCSCNWKEY:
1001 		error = kauth_authorize_network(l->l_cred,
1002 		    KAUTH_NETWORK_INTERFACE,
1003 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
1004 		    NULL);
1005 		if (error)
1006 			return (error);
1007 		break;
1008 	case SIOCGCNWSTATUS:
1009 		error = kauth_authorize_network(l->l_cred,
1010 		    KAUTH_NETWORK_INTERFACE,
1011 		    KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, ifp, KAUTH_ARG(cmd),
1012 		    NULL);
1013 		if (error)
1014 			return (error);
1015 		break;
1016 	default:
1017 		return (EINVAL);
1018 	}
1019 
1020 	s = splnet();
1021 
1022 	switch (cmd) {
1023 
1024 	case SIOCINITIFADDR:
1025 		if (!(ifp->if_flags & IFF_RUNNING) &&
1026 		    (error = cnw_enable(sc)) != 0)
1027 			break;
1028 		ifp->if_flags |= IFF_UP;
1029 		cnw_init(sc);
1030 		switch (ifa->ifa_addr->sa_family) {
1031 #ifdef INET
1032 		case AF_INET:
1033 			arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
1034 			break;
1035 #endif
1036 		default:
1037 			break;
1038 		}
1039 		break;
1040 
1041 	case SIOCSIFFLAGS:
1042 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1043 			break;
1044 		/* XXX re-use ether_ioctl() */
1045 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1046 		case IFF_RUNNING:
1047 			/*
1048 			 * The interface is marked down and it is running, so
1049 			 * stop it.
1050 			 */
1051 			cnw_disable(sc);
1052 			break;
1053 		case IFF_UP:
1054 			/*
1055 			 * The interface is marked up and it is stopped, so
1056 			 * start it.
1057 			 */
1058 			error = cnw_enable(sc);
1059 			break;
1060 		default:
1061 			/* IFF_PROMISC may be changed */
1062 			cnw_init(sc);
1063 			break;
1064 		}
1065 		break;
1066 
1067 	case SIOCADDMULTI:
1068 	case SIOCDELMULTI:
1069 		/* Update our multicast list. */
1070 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1071 			if (ifp->if_flags & IFF_RUNNING)
1072 				cnw_init(sc);
1073 			error = 0;
1074 		}
1075 		break;
1076 
1077 	case SIOCGCNWDOMAIN:
1078 		ifr->ifr_domain = sc->sc_domain;
1079 		break;
1080 
1081 	case SIOCSCNWDOMAIN:
1082 		error = cnw_setdomain(sc, ifr->ifr_domain);
1083 		break;
1084 
1085 	case SIOCSCNWKEY:
1086 		error = cnw_setkey(sc, ifr->ifr_key);
1087 		break;
1088 
1089 	case SIOCGCNWSTATUS:
1090 		if ((ifp->if_flags & IFF_RUNNING) == 0)
1091 			break;
1092 		bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
1093 		    sc->sc_memoff + CNW_EREG_CB,
1094 		    ((struct cnwstatus *)data)->data,
1095 		    sizeof(((struct cnwstatus *)data)->data));
1096 		break;
1097 
1098 	case SIOCGCNWSTATS:
1099 		memcpy((void *)&(((struct cnwistats *)data)->stats),
1100 		    (void *)&sc->sc_stats, sizeof(struct cnwstats));
1101 			break;
1102 
1103 	default:
1104 		error = ether_ioctl(ifp, cmd, data);
1105 		break;
1106 	}
1107 
1108 	splx(s);
1109 	return (error);
1110 }
1111 
1112 
1113 /*
1114  * Device timeout/watchdog routine. Entered if the device neglects to
1115  * generate an interrupt after a transmit has been started on it.
1116  */
1117 static void
cnw_watchdog(struct ifnet * ifp)1118 cnw_watchdog(struct ifnet *ifp)
1119 {
1120 	struct cnw_softc *sc = ifp->if_softc;
1121 
1122 	printf("%s: device timeout; card reset\n", device_xname(sc->sc_dev));
1123 	if_statinc(ifp, if_oerrors);
1124 	cnw_init(sc);
1125 }
1126 
1127 static int
cnw_setdomain(struct cnw_softc * sc,int domain)1128 cnw_setdomain(struct cnw_softc *sc, int domain)
1129 {
1130 	int s;
1131 
1132 	if (domain & ~0x1ff)
1133 		return EINVAL;
1134 
1135 	s = splnet();
1136 	CNW_CMD2(sc, CNW_CMD_SMD, domain, domain >> 8);
1137 	splx(s);
1138 
1139 	sc->sc_domain = domain;
1140 	return 0;
1141 }
1142 
1143 static int
cnw_setkey(struct cnw_softc * sc,int key)1144 cnw_setkey(struct cnw_softc *sc, int key)
1145 {
1146 	int s;
1147 
1148 	if (key & ~0xffff)
1149 		return EINVAL;
1150 
1151 	s = splnet();
1152 	CNW_CMD2(sc, CNW_CMD_SSK, key, key >> 8);
1153 	splx(s);
1154 
1155 	sc->sc_skey = key;
1156 	return 0;
1157 }
1158 
1159 static int
cnw_activate(device_t self,enum devact act)1160 cnw_activate(device_t self, enum devact act)
1161 {
1162 	struct cnw_softc *sc = device_private(self);
1163 
1164 	switch (act) {
1165 	case DVACT_DEACTIVATE:
1166 		if_deactivate(&sc->sc_ethercom.ec_if);
1167 		return 0;
1168 	default:
1169 		return EOPNOTSUPP;
1170 	}
1171 }
1172 
1173 static int
cnw_detach(device_t self,int flags)1174 cnw_detach(device_t self, int flags)
1175 {
1176 	struct cnw_softc *sc = device_private(self);
1177 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1178 
1179 	/* cnw_disable() checks IFF_RUNNING */
1180 	cnw_disable(sc);
1181 
1182 	if ((sc->sc_resource & CNW_RES_NET) != 0) {
1183 		ether_ifdetach(ifp);
1184 		if_detach(ifp);
1185 	}
1186 
1187 #ifndef MEMORY_MAPPED
1188 	/* unmap and free our i/o windows */
1189 	if ((sc->sc_resource & CNW_RES_IO) != 0) {
1190 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
1191 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
1192 	}
1193 #endif
1194 
1195 	/* unmap and free our memory windows */
1196 	if ((sc->sc_resource & CNW_RES_MEM) != 0) {
1197 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwin);
1198 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
1199 	}
1200 
1201 	return (0);
1202 }
1203