xref: /netbsd-src/sys/dev/usb/if_atu.c (revision aa73cae19608873cc4d1f712c4a0f8f8435f1ffa)
1 /*	$NetBSD: if_atu.c,v 1.5 2005/02/27 00:27:51 perry Exp $ */
2 /*	$OpenBSD: if_atu.c,v 1.48 2004/12/30 01:53:21 dlg Exp $ */
3 /*
4  * Copyright (c) 2003, 2004
5  *	Daan Vreeken <Danovitsch@Vitsch.net>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Daan Vreeken.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Daan Vreeken AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Daan Vreeken OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * Atmel AT76c503 / AT76c503a / AT76c505 / AT76c505a  USB WLAN driver
37  * version 0.5 - 2004-08-03
38  *
39  * Originally written by Daan Vreeken <Danovitsch @ Vitsch . net>
40  *  http://vitsch.net/bsd/atuwi
41  *
42  * Contributed to by :
43  *  Chris Whitehouse, Alistair Phillips, Peter Pilka, Martijn van Buul,
44  *  Suihong Liang, Arjan van Leeuwen, Stuart Walsh
45  *
46  * Ported to OpenBSD by Theo de Raadt and David Gwynne.
47  * Ported to NetBSD by Jesse Off
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: if_atu.c,v 1.5 2005/02/27 00:27:51 perry Exp $");
52 
53 #include "bpfilter.h"
54 
55 #include <sys/param.h>
56 #include <sys/sockio.h>
57 #include <sys/mbuf.h>
58 #include <sys/kernel.h>
59 #include <sys/socket.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
62 #include <sys/kthread.h>
63 #include <sys/queue.h>
64 #include <sys/device.h>
65 
66 #include <machine/bus.h>
67 
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdivar.h>
72 
73 #include <dev/usb/usbdevs.h>
74 
75 #include <dev/microcode/atmel/atmel_intersil_fw.h>
76 #include <dev/microcode/atmel/atmel_rfmd2958-smc_fw.h>
77 #include <dev/microcode/atmel/atmel_rfmd2958_fw.h>
78 #include <dev/microcode/atmel/atmel_rfmd_fw.h>
79 
80 #if NBPFILTER > 0
81 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
82 #include <net/bpf.h>
83 #endif
84 
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/if_media.h>
88 #include <net/if_ether.h>
89 
90 #ifdef INET
91 #include <netinet/in.h>
92 #include <netinet/if_ether.h>
93 #endif
94 
95 #include <net80211/ieee80211_var.h>
96 #include <net80211/ieee80211_radiotap.h>
97 
98 #ifdef USB_DEBUG
99 #define ATU_DEBUG
100 #endif
101 
102 #include <dev/usb/if_atureg.h>
103 
104 #ifdef ATU_DEBUG
105 #define DPRINTF(x)	do { if (atudebug) printf x; } while (0)
106 #define DPRINTFN(n,x)	do { if (atudebug>(n)) printf x; } while (0)
107 int atudebug = 1;
108 #else
109 #define DPRINTF(x)
110 #define DPRINTFN(n,x)
111 #endif
112 
113 /*
114  * Various supported device vendors/products/radio type.
115  */
116 struct atu_type atu_devs[] = {
117 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_BW002,
118 	  RadioRFMD,		ATU_NO_QUIRK },
119 	{ USB_VENDOR_BELKIN,	USB_PRODUCT_BELKIN_F5D6050,
120 	  RadioRFMD,		ATU_NO_QUIRK },
121 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_AT76C503A,
122 	  RadioIntersil,	ATU_NO_QUIRK },
123 	{ USB_VENDOR_LEXAR,	USB_PRODUCT_LEXAR_2662WAR,
124 	  RadioRFMD,		ATU_NO_QUIRK },
125 	{ USB_VENDOR_LINKSYS2,	USB_PRODUCT_LINKSYS2_WUSB11,
126 	  RadioRFMD,		ATU_NO_QUIRK },
127 	{ USB_VENDOR_LINKSYS3,	USB_PRODUCT_LINKSYS3_WUSB11V28,
128 	  RadioRFMD2958,	ATU_NO_QUIRK },
129 	{ USB_VENDOR_NETGEAR2,	USB_PRODUCT_NETGEAR2_MA101B,
130 	  RadioRFMD,		ATU_NO_QUIRK },
131 	{ USB_VENDOR_ACERP,	USB_PRODUCT_ACERP_AWL400,
132 	  RadioRFMD,		ATU_NO_QUIRK },
133 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_WL1130,
134 	  RadioRFMD2958,	ATU_NO_QUIRK },
135 	{ USB_VENDOR_LINKSYS3,	USB_PRODUCT_LINKSYS3_WUSB11V28,
136 	  RadioRFMD2958,	ATU_NO_QUIRK },
137 	{ USB_VENDOR_AINCOMM,	USB_PRODUCT_AINCOMM_AWU2000B,
138 	  RadioRFMD2958,	ATU_NO_QUIRK },
139 	/* SMC2662 V.4 */
140 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_AT76C505A,
141 	  RadioRFMD2958_SMC,	ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
142 	{ USB_VENDOR_ACERP,	USB_PRODUCT_ACERP_AWL300,
143 	  RadioIntersil,	ATU_NO_QUIRK },
144 };
145 
146 struct atu_radfirm {
147 	enum	atu_radio_type atur_type;
148 	unsigned char	*atur_internal;
149 	size_t		atur_internal_sz;
150 	unsigned char	*atur_external;
151 	size_t		atur_external_sz;
152 } atu_radfirm[] = {
153 	{ RadioRFMD,
154 	  atmel_fw_rfmd_int,		sizeof(atmel_fw_rfmd_int),
155 	  atmel_fw_rfmd_ext,		sizeof(atmel_fw_rfmd_ext) },
156 	{ RadioRFMD2958,
157 	  atmel_fw_rfmd2958_int,	sizeof(atmel_fw_rfmd2958_int),
158 	  atmel_fw_rfmd2958_ext,	sizeof(atmel_fw_rfmd2958_ext) },
159 	{ RadioRFMD2958_SMC,
160 	  atmel_fw_rfmd2958_smc_int,	sizeof(atmel_fw_rfmd2958_smc_int),
161 	  atmel_fw_rfmd2958_smc_ext,	sizeof(atmel_fw_rfmd2958_smc_ext) },
162 	{ RadioIntersil,
163 	  atmel_fw_intersil_int,	sizeof(atmel_fw_intersil_int),
164 	  atmel_fw_intersil_ext,	sizeof(atmel_fw_intersil_ext) }
165 };
166 
167 int	atu_newbuf(struct atu_softc *, struct atu_chain *, struct mbuf *);
168 void	atu_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
169 void	atu_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
170 void	atu_start(struct ifnet *);
171 int	atu_ioctl(struct ifnet *, u_long, caddr_t);
172 int	atu_init(struct ifnet *);
173 void	atu_stop(struct ifnet *, int);
174 void	atu_watchdog(struct ifnet *);
175 usbd_status atu_usb_request(struct atu_softc *sc, u_int8_t type,
176 	    u_int8_t request, u_int16_t value, u_int16_t index,
177 	    u_int16_t length, u_int8_t *data);
178 int	atu_send_command(struct atu_softc *sc, u_int8_t *command, int size);
179 int	atu_get_cmd_status(struct atu_softc *sc, u_int8_t cmd,
180 	    u_int8_t *status);
181 int	atu_wait_completion(struct atu_softc *sc, u_int8_t cmd,
182 	    u_int8_t *status);
183 int	atu_send_mib(struct atu_softc *sc, u_int8_t type,
184 	    u_int8_t size, u_int8_t index, void *data);
185 int	atu_get_mib(struct atu_softc *sc, u_int8_t type,
186 	    u_int8_t size, u_int8_t index, u_int8_t *buf);
187 #if 0
188 int	atu_start_ibss(struct atu_softc *sc);
189 #endif
190 int	atu_start_scan(struct atu_softc *sc);
191 int	atu_switch_radio(struct atu_softc *sc, int state);
192 int	atu_initial_config(struct atu_softc *sc);
193 int	atu_join(struct atu_softc *sc, struct ieee80211_node *node);
194 int8_t	atu_get_dfu_state(struct atu_softc *sc);
195 u_int8_t atu_get_opmode(struct atu_softc *sc, u_int8_t *mode);
196 void	atu_internal_firmware(struct device *);
197 void	atu_external_firmware(struct device *);
198 int	atu_get_card_config(struct atu_softc *sc);
199 int	atu_media_change(struct ifnet *ifp);
200 void	atu_media_status(struct ifnet *ifp, struct ifmediareq *req);
201 int	atu_tx_list_init(struct atu_softc *);
202 int	atu_rx_list_init(struct atu_softc *);
203 void	atu_xfer_list_free(struct atu_softc *sc, struct atu_chain *ch,
204 	    int listlen);
205 int	atu_set_wepkey(struct atu_softc *sc, int nr, u_int8_t *key, int len);
206 
207 #ifdef ATU_DEBUG
208 void	atu_debug_print(struct atu_softc *sc);
209 #endif
210 
211 void atu_task(void *);
212 int atu_newstate(struct ieee80211com *, enum ieee80211_state, int);
213 int atu_tx_start(struct atu_softc *, struct ieee80211_node *,
214     struct atu_chain *, struct mbuf *);
215 void atu_complete_attach(struct atu_softc *);
216 u_int8_t atu_calculate_padding(int);
217 
218 USB_DECLARE_DRIVER(atu);
219 
220 usbd_status
221 atu_usb_request(struct atu_softc *sc, u_int8_t type,
222     u_int8_t request, u_int16_t value, u_int16_t index, u_int16_t length,
223     u_int8_t *data)
224 {
225 	usb_device_request_t	req;
226 	usbd_xfer_handle	xfer;
227 	usbd_status		err;
228 	int			total_len = 0, s;
229 
230 	req.bmRequestType = type;
231 	req.bRequest = request;
232 	USETW(req.wValue, value);
233 	USETW(req.wIndex, index);
234 	USETW(req.wLength, length);
235 
236 #ifdef ATU_DEBUG
237 	if (atudebug) {
238 		DPRINTFN(20, ("%s: req=%02x val=%02x ind=%02x "
239 		    "len=%02x\n", USBDEVNAME(sc->atu_dev), request,
240 		    value, index, length));
241 	}
242 #endif /* ATU_DEBUG */
243 
244 	s = splnet();
245 
246 	xfer = usbd_alloc_xfer(sc->atu_udev);
247 	usbd_setup_default_xfer(xfer, sc->atu_udev, 0, 500000, &req, data,
248 	    length, USBD_SHORT_XFER_OK, 0);
249 
250 	err = usbd_sync_transfer(xfer);
251 
252 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
253 
254 #ifdef ATU_DEBUG
255 	if (atudebug) {
256 		if (type & UT_READ) {
257 			DPRINTFN(20, ("%s: transfered 0x%x bytes in\n",
258 			    USBDEVNAME(sc->atu_dev), total_len));
259 		} else {
260 			if (total_len != length)
261 				DPRINTF(("%s: wrote only %x bytes\n",
262 				    USBDEVNAME(sc->atu_dev), total_len));
263 		}
264 	}
265 #endif /* ATU_DEBUG */
266 
267 	usbd_free_xfer(xfer);
268 
269 	splx(s);
270 	return(err);
271 }
272 
273 int
274 atu_send_command(struct atu_softc *sc, u_int8_t *command, int size)
275 {
276 	return atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000,
277 	    0x0000, size, command);
278 }
279 
280 int
281 atu_get_cmd_status(struct atu_softc *sc, u_int8_t cmd, u_int8_t *status)
282 {
283 	/*
284 	 * all other drivers (including Windoze) request 40 bytes of status
285 	 * and get a short-xfer of just 6 bytes. we can save 34 bytes of
286 	 * buffer if we just request those 6 bytes in the first place :)
287 	 */
288 	/*
289 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd,
290 	    0x0000, 40, status);
291 	*/
292 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd,
293 	    0x0000, 6, status);
294 }
295 
296 int
297 atu_wait_completion(struct atu_softc *sc, u_int8_t cmd, u_int8_t *status)
298 {
299 	int			idle_count = 0, err;
300 	u_int8_t		statusreq[6];
301 
302 	DPRINTFN(15, ("%s: wait-completion: cmd=%02x\n",
303 	    USBDEVNAME(sc->atu_dev), cmd));
304 
305 	while (1) {
306 		err = atu_get_cmd_status(sc, cmd, statusreq);
307 		if (err)
308 			return err;
309 
310 #ifdef ATU_DEBUG
311 		if (atudebug) {
312 			DPRINTFN(20, ("%s: status=%s cmd=%02x\n",
313 			    USBDEVNAME(sc->atu_dev),
314 			ether_sprintf(statusreq), cmd));
315 		}
316 #endif /* ATU_DEBUG */
317 
318 		/*
319 		 * during normal operations waiting on STATUS_IDLE
320 		 * will never happen more than once
321 		 */
322 		if ((statusreq[5] == STATUS_IDLE) && (idle_count++ > 20)) {
323 			DPRINTF(("%s: idle_count > 20!\n",
324 			    USBDEVNAME(sc->atu_dev)));
325 			return 0;
326 		}
327 
328 		if ((statusreq[5] != STATUS_IN_PROGRESS) &&
329 		    (statusreq[5] != STATUS_IDLE)) {
330 			if (status != NULL)
331 				*status = statusreq[5];
332 			return 0;
333 		}
334 		usbd_delay_ms(sc->atu_udev, 25);
335 	}
336 }
337 
338 int
339 atu_send_mib(struct atu_softc *sc, u_int8_t type, u_int8_t size,
340     u_int8_t index, void *data)
341 {
342 	int				err;
343 	struct atu_cmd_set_mib		request;
344 
345 	/*
346 	 * We don't construct a MIB packet first and then memcpy it into an
347 	 * Atmel-command-packet, we just construct it the right way at once :)
348 	 */
349 
350 	memset(&request, 0, sizeof(request));
351 
352 	request.AtCmd = CMD_SET_MIB;
353 	USETW(request.AtSize, size + 4);
354 
355 	request.MIBType = type;
356 	request.MIBSize = size;
357 	request.MIBIndex = index;
358 	request.MIBReserved = 0;
359 
360 	/*
361 	 * For 1 and 2 byte requests we assume a direct value,
362 	 * everything bigger than 2 bytes we assume a pointer to the data
363 	 */
364 	switch (size) {
365 	case 0:
366 		break;
367 	case 1:
368 		request.data[0]=(long)data & 0x000000ff;
369 		break;
370 	case 2:
371 		request.data[0]=(long)data & 0x000000ff;
372 		request.data[1]=(long)data >> 8;
373 		break;
374 	default:
375 		memcpy(request.data, data, size);
376 		break;
377 	}
378 
379 	err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000,
380 	    0x0000, size+8, (uByte *)&request);
381 	if (err)
382 		return (err);
383 
384 	DPRINTFN(15, ("%s: sendmib : waitcompletion...\n",
385 	    USBDEVNAME(sc->atu_dev)));
386 	return atu_wait_completion(sc, CMD_SET_MIB, NULL);
387 }
388 
389 int
390 atu_get_mib(struct atu_softc *sc, u_int8_t type, u_int8_t size,
391     u_int8_t index, u_int8_t *buf)
392 {
393 
394 	/* linux/at76c503.c - 478 */
395 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x033,
396 	    type << 8, index, size, buf);
397 }
398 
399 #if 0
400 int
401 atu_start_ibss(struct atu_softc *sc)
402 {
403 	int				err;
404 	struct atu_cmd_start_ibss	Request;
405 
406 	Request.Cmd = CMD_START_IBSS;
407 	Request.Reserved = 0;
408 	Request.Size = sizeof(Request) - 4;
409 
410 	memset(Request.BSSID, 0x00, sizeof(Request.BSSID));
411 	memset(Request.SSID, 0x00, sizeof(Request.SSID));
412 	memcpy(Request.SSID, sc->atu_ssid, sc->atu_ssidlen);
413 	Request.SSIDSize = sc->atu_ssidlen;
414 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
415 		Request.Channel = (u_int8_t)sc->atu_desired_channel;
416 	else
417 		Request.Channel = ATU_DEFAULT_CHANNEL;
418 	Request.BSSType = AD_HOC_MODE;
419 	memset(Request.Res, 0x00, sizeof(Request.Res));
420 
421 	/* Write config to adapter */
422 	err = atu_send_command(sc, (u_int8_t *)&Request, sizeof(Request));
423 	if (err) {
424 		DPRINTF(("%s: start ibss failed!\n",
425 		    USBDEVNAME(sc->atu_dev)));
426 		return err;
427 	}
428 
429 	/* Wait for the adapter to do it's thing */
430 	err = atu_wait_completion(sc, CMD_START_IBSS, NULL);
431 	if (err) {
432 		DPRINTF(("%s: error waiting for start_ibss\n",
433 		    USBDEVNAME(sc->atu_dev)));
434 		return err;
435 	}
436 
437 	/* Get the current BSSID */
438 	err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, sc->atu_bssid);
439 	if (err) {
440 		DPRINTF(("%s: could not get BSSID!\n",
441 		    USBDEVNAME(sc->atu_dev)));
442 		return err;
443 	}
444 
445 	DPRINTF(("%s: started a new IBSS (BSSID=%s)\n",
446 	    USBDEVNAME(sc->atu_dev), ether_sprintf(sc->atu_bssid)));
447 	return 0;
448 }
449 #endif
450 
451 int
452 atu_start_scan(struct atu_softc *sc)
453 {
454 	struct atu_cmd_do_scan		Scan;
455 	usbd_status			err;
456 	int				Cnt;
457 
458 	memset(&Scan, 0, sizeof(Scan));
459 
460 	Scan.Cmd = CMD_START_SCAN;
461 	Scan.Reserved = 0;
462 	USETW(Scan.Size, sizeof(Scan) - 4);
463 
464 	/* use the broadcast BSSID (in active scan) */
465 	for (Cnt=0; Cnt<6; Cnt++)
466 		Scan.BSSID[Cnt] = 0xff;
467 
468 	memset(Scan.SSID, 0x00, sizeof(Scan.SSID));
469 	memcpy(Scan.SSID, sc->atu_ssid, sc->atu_ssidlen);
470 	Scan.SSID_Len = sc->atu_ssidlen;
471 
472 	/* default values for scan */
473 	Scan.ScanType = ATU_SCAN_ACTIVE;
474 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
475 		Scan.Channel = (u_int8_t)sc->atu_desired_channel;
476 	else
477 		Scan.Channel = sc->atu_channel;
478 
479 	/* we like scans to be quick :) */
480 	/* the time we wait before sending probe's */
481 	USETW(Scan.ProbeDelay, 0);
482 	/* the time we stay on one channel */
483 	USETW(Scan.MinChannelTime, 100);
484 	USETW(Scan.MaxChannelTime, 200);
485 	/* wether or not we scan all channels */
486 	Scan.InternationalScan = 0xc1;
487 
488 #ifdef ATU_DEBUG
489 	if (atudebug) {
490 		DPRINTFN(20, ("%s: scan cmd len=%02lx\n",
491 		    USBDEVNAME(sc->atu_dev), (unsigned long)sizeof(Scan)));
492 	}
493 #endif /* ATU_DEBUG */
494 
495 	/* Write config to adapter */
496 	err = atu_send_command(sc, (u_int8_t *)&Scan, sizeof(Scan));
497 	if (err)
498 		return err;
499 
500 	/*
501 	 * We don't wait for the command to finish... the mgmt-thread will do
502 	 * that for us
503 	 */
504 	/*
505 	err = atu_wait_completion(sc, CMD_START_SCAN, NULL);
506 	if (err)
507 		return err;
508 	*/
509 	return 0;
510 }
511 
512 int
513 atu_switch_radio(struct atu_softc *sc, int state)
514 {
515 	usbd_status		err;
516 	struct atu_cmd		CmdRadio;
517 
518 	if (sc->atu_radio == RadioIntersil) {
519 		/*
520 		 * Intersil doesn't seem to need/support switching the radio
521 		 * on/off
522 		 */
523 		return 0;
524 	}
525 
526 	memset(&CmdRadio, 0, sizeof(CmdRadio));
527 	CmdRadio.Cmd = CMD_RADIO_ON;
528 
529 	if (sc->atu_radio_on != state) {
530 		if (state == 0)
531 			CmdRadio.Cmd = CMD_RADIO_OFF;
532 
533 		err = atu_send_command(sc, (u_int8_t *)&CmdRadio,
534 		    sizeof(CmdRadio));
535 		if (err)
536 			return err;
537 
538 		err = atu_wait_completion(sc, CmdRadio.Cmd, NULL);
539 		if (err)
540 			return err;
541 
542 		DPRINTFN(10, ("%s: radio turned %s\n",
543 		    USBDEVNAME(sc->atu_dev), state ? "on" : "off"));
544 		sc->atu_radio_on = state;
545 	}
546 	return 0;
547 }
548 
549 int
550 atu_initial_config(struct atu_softc *sc)
551 {
552 	struct ieee80211com		*ic = &sc->sc_ic;
553 	usbd_status			err;
554 /*	u_int8_t			rates[4] = {0x82, 0x84, 0x8B, 0x96};*/
555 	u_int8_t			rates[4] = {0x82, 0x04, 0x0B, 0x16};
556 	struct atu_cmd_card_config	cmd;
557 	u_int8_t			reg_domain;
558 
559 	DPRINTFN(10, ("%s: sending mac-addr\n", USBDEVNAME(sc->atu_dev)));
560 	err = atu_send_mib(sc, MIB_MAC_ADDR__ADDR, ic->ic_myaddr);
561 	if (err) {
562 		DPRINTF(("%s: error setting mac-addr\n",
563 		    USBDEVNAME(sc->atu_dev)));
564 		return err;
565 	}
566 
567 	/*
568 	DPRINTF(("%s: sending reg-domain\n", USBDEVNAME(sc->atu_dev)));
569 	err = atu_send_mib(sc, MIB_PHY__REG_DOMAIN, NR(0x30));
570 	if (err) {
571 		DPRINTF(("%s: error setting mac-addr\n",
572 		    USBDEVNAME(sc->atu_dev)));
573 		return err;
574 	}
575 	*/
576 
577 	memset(&cmd, 0, sizeof(cmd));
578 	cmd.Cmd = CMD_STARTUP;
579 	cmd.Reserved = 0;
580 	USETW(cmd.Size, sizeof(cmd) - 4);
581 
582 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
583 		cmd.Channel = (u_int8_t)sc->atu_desired_channel;
584 	else
585 		cmd.Channel = sc->atu_channel;
586 	cmd.AutoRateFallback = 1;
587 	memcpy(cmd.BasicRateSet, rates, 4);
588 
589 	/* ShortRetryLimit should be 7 according to 802.11 spec */
590 	cmd.ShortRetryLimit = 7;
591 	USETW(cmd.RTS_Threshold, 2347);
592 	USETW(cmd.FragThreshold, 2346);
593 
594 	/* Doesn't seem to work, but we'll set it to 1 anyway */
595 	cmd.PromiscuousMode = 1;
596 
597 	/* this goes into the beacon we transmit */
598 	if (sc->atu_encrypt == ATU_WEP_OFF)
599 		cmd.PrivacyInvoked = 0;
600 	else
601 		cmd.PrivacyInvoked = 1;
602 
603 	cmd.ExcludeUnencrypted = 0;
604 	cmd.EncryptionType = sc->atu_wepkeylen;
605 
606 	/* Setting the SSID here doesn't seem to do anything */
607 	memset(cmd.SSID, 0, sizeof(cmd.SSID));
608 	memcpy(cmd.SSID, sc->atu_ssid, sc->atu_ssidlen);
609 	cmd.SSID_Len = sc->atu_ssidlen;
610 
611 	cmd.WEP_DefaultKeyID = sc->atu_wepkey;
612 	memcpy(cmd.WEP_DefaultKey, sc->atu_wepkeys,
613 	    sizeof(cmd.WEP_DefaultKey));
614 
615 	cmd.ShortPreamble = 1;
616 	cmd.ShortPreamble = 0;
617 	USETW(cmd.BeaconPeriod, 100);
618 	/* cmd.BeaconPeriod = 65535; */
619 
620 	/*
621 	 * TODO:
622 	 * read reg domain MIB_PHY @ 0x17 (1 byte), (reply = 0x30)
623 	 * we should do something usefull with this info. right now it's just
624 	 * ignored
625 	 */
626 	err = atu_get_mib(sc, MIB_PHY__REG_DOMAIN, &reg_domain);
627 	if (err) {
628 		DPRINTF(("%s: could not get regdomain!\n",
629 		    USBDEVNAME(sc->atu_dev)));
630 	} else {
631 		DPRINTF(("%s: in reg domain 0x%x according to the "
632 		    "adapter\n", USBDEVNAME(sc->atu_dev), reg_domain));
633 	}
634 
635 #ifdef ATU_DEBUG
636 	if (atudebug) {
637 		DPRINTFN(20, ("%s: configlen=%02lx\n", USBDEVNAME(sc->atu_dev),
638 		    (unsigned long)sizeof(cmd)));
639 	}
640 #endif /* ATU_DEBUG */
641 
642 	/* Windoze : driver says exclude-unencrypted=1 & encr-type=1 */
643 
644 	err = atu_send_command(sc, (u_int8_t *)&cmd, sizeof(cmd));
645 	if (err)
646 		return err;
647 	err = atu_wait_completion(sc, CMD_STARTUP, NULL);
648 	if (err)
649 		return err;
650 
651 	/* Turn on radio now */
652 	err = atu_switch_radio(sc, 1);
653 	if (err)
654 		return err;
655 
656 	/* preamble type = short */
657 	err = atu_send_mib(sc, MIB_LOCAL__PREAMBLE, NR(PREAMBLE_SHORT));
658 	if (err)
659 		return err;
660 
661 	/* frag = 1536 */
662 	err = atu_send_mib(sc, MIB_MAC__FRAG, NR(2346));
663 	if (err)
664 		return err;
665 
666 	/* rts = 1536 */
667 	err = atu_send_mib(sc, MIB_MAC__RTS, NR(2347));
668 	if (err)
669 		return err;
670 
671 	/* auto rate fallback = 1 */
672 	err = atu_send_mib(sc, MIB_LOCAL__AUTO_RATE_FALLBACK, NR(1));
673 	if (err)
674 		return err;
675 
676 	/* power mode = full on, no power saving */
677 	err = atu_send_mib(sc, MIB_MAC_MGMT__POWER_MODE,
678 	    NR(POWER_MODE_ACTIVE));
679 	if (err)
680 		return err;
681 
682 	DPRINTFN(10, ("%s: completed initial config\n",
683 	   USBDEVNAME(sc->atu_dev)));
684 	return 0;
685 }
686 
687 int
688 atu_join(struct atu_softc *sc, struct ieee80211_node *node)
689 {
690 	struct atu_cmd_join		join;
691 	u_int8_t			status;
692 	usbd_status			err;
693 
694 	memset(&join, 0, sizeof(join));
695 
696 	join.Cmd = CMD_JOIN;
697 	join.Reserved = 0x00;
698 	USETW(join.Size, sizeof(join) - 4);
699 
700 	DPRINTFN(15, ("%s: pre-join sc->atu_bssid=%s\n",
701 	    USBDEVNAME(sc->atu_dev), ether_sprintf(sc->atu_bssid)));
702 	DPRINTFN(15, ("%s: mode=%d\n", USBDEVNAME(sc->atu_dev),
703 	    sc->atu_mode));
704 	memcpy(join.bssid, node->ni_bssid, IEEE80211_ADDR_LEN);
705 	memset(join.essid, 0x00, 32);
706 	memcpy(join.essid, node->ni_essid, node->ni_esslen);
707 	join.essid_size = node->ni_esslen;
708 	if (node->ni_capinfo & IEEE80211_CAPINFO_IBSS)
709 		join.bss_type = AD_HOC_MODE;
710 	else
711 		join.bss_type = INFRASTRUCTURE_MODE;
712 	join.channel = ieee80211_chan2ieee(&sc->sc_ic, node->ni_chan);
713 
714 	USETW(join.timeout, ATU_JOIN_TIMEOUT);
715 	join.reserved = 0x00;
716 
717 	DPRINTFN(10, ("%s: trying to join BSSID=%s\n",
718 	    USBDEVNAME(sc->atu_dev), ether_sprintf(join.bssid)));
719 	err = atu_send_command(sc, (u_int8_t *)&join, sizeof(join));
720 	if (err) {
721 		DPRINTF(("%s: ERROR trying to join IBSS\n",
722 		    USBDEVNAME(sc->atu_dev)));
723 		return err;
724 	}
725 	err = atu_wait_completion(sc, CMD_JOIN, &status);
726 	if (err) {
727 		DPRINTF(("%s: error joining BSS!\n",
728 		    USBDEVNAME(sc->atu_dev)));
729 		return err;
730 	}
731 	if (status != STATUS_COMPLETE) {
732 		DPRINTF(("%s: error joining... [status=%02x]\n",
733 		    USBDEVNAME(sc->atu_dev), status));
734 		return status;
735 	} else {
736 		DPRINTFN(10, ("%s: joined BSS\n", USBDEVNAME(sc->atu_dev)));
737 	}
738 	return err;
739 }
740 
741 /*
742  * Get the state of the DFU unit
743  */
744 int8_t
745 atu_get_dfu_state(struct atu_softc *sc)
746 {
747 	u_int8_t	state;
748 
749 	if (atu_usb_request(sc, DFU_GETSTATE, 0, 0, 1, &state))
750 		return -1;
751 	return state;
752 }
753 
754 /*
755  * Get MAC opmode
756  */
757 u_int8_t
758 atu_get_opmode(struct atu_softc *sc, u_int8_t *mode)
759 {
760 
761 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33, 0x0001,
762 	    0x0000, 1, mode);
763 }
764 
765 /*
766  * Upload the internal firmware into the device
767  */
768 void
769 atu_internal_firmware(struct device *arg)
770 {
771 	struct atu_softc *sc = (struct atu_softc *)arg;
772 	u_char	state, *ptr = NULL, *firm = NULL, status[6];
773 	int block_size, block = 0, err, i;
774 	size_t	bytes_left = 0;
775 
776 	/*
777 	 * Uploading firmware is done with the DFU (Device Firmware Upgrade)
778 	 * interface. See "Universal Serial Bus - Device Class Specification
779 	 * for Device Firmware Upgrade" pdf for details of the protocol.
780 	 * Maybe this could be moved to a seperate 'firmware driver' once more
781 	 * device drivers need it... For now we'll just do it here.
782 	 *
783 	 * Just for your information, the Atmel's DFU descriptor looks like
784 	 * this:
785 	 *
786 	 * 07		size
787 	 * 21		type
788 	 * 01		capabilities : only firmware download, need reset
789 	 *		  after download
790 	 * 13 05	detach timeout : max 1299ms between DFU_DETACH and
791 	 *		  reset
792 	 * 00 04	max bytes of firmware per transaction : 1024
793 	 */
794 
795 	/* Choose the right firmware for the device */
796 	for (i = 0; i < sizeof(atu_radfirm)/sizeof(atu_radfirm[0]); i++)
797 		if (sc->atu_radio == atu_radfirm[i].atur_type) {
798 			firm = atu_radfirm[i].atur_internal;
799 			bytes_left = atu_radfirm[i].atur_internal_sz;
800 		}
801 
802 	if (firm == NULL) {
803 		printf("%s: no firmware found\n", USBDEVNAME(sc->atu_dev));
804 		return;
805 	}
806 
807 	ptr = firm;
808 	state = atu_get_dfu_state(sc);
809 
810 	while (block >= 0 && state > 0) {
811 		switch (state) {
812 		case DFUState_DnLoadSync:
813 			/* get DFU status */
814 			err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0 , 6,
815 			    status);
816 			if (err) {
817 				DPRINTF(("%s: dfu_getstatus failed!\n",
818 				    USBDEVNAME(sc->atu_dev)));
819 				return;
820 			}
821 			/* success means state => DnLoadIdle */
822 			state = DFUState_DnLoadIdle;
823 			continue;
824 			break;
825 
826 		case DFUState_DFUIdle:
827 		case DFUState_DnLoadIdle:
828 			if (bytes_left>=DFU_MaxBlockSize)
829 				block_size = DFU_MaxBlockSize;
830 			else
831 				block_size = bytes_left;
832 			DPRINTFN(15, ("%s: firmware block %d\n",
833 			    USBDEVNAME(sc->atu_dev), block));
834 
835 			err = atu_usb_request(sc, DFU_DNLOAD, block++, 0,
836 			    block_size, ptr);
837 			if (err) {
838 				DPRINTF(("%s: dfu_dnload failed\n",
839 				    USBDEVNAME(sc->atu_dev)));
840 				return;
841 			}
842 
843 			ptr += block_size;
844 			bytes_left -= block_size;
845 			if (block_size == 0)
846 				block = -1;
847 			break;
848 
849 		default:
850 			usbd_delay_ms(sc->atu_udev, 100);
851 			DPRINTFN(20, ("%s: sleeping for a while\n",
852 			    USBDEVNAME(sc->atu_dev)));
853 			break;
854 		}
855 
856 		state = atu_get_dfu_state(sc);
857 	}
858 
859 	if (state != DFUState_ManifestSync) {
860 		DPRINTF(("%s: state != manifestsync... eek!\n",
861 		    USBDEVNAME(sc->atu_dev)));
862 	}
863 
864 	err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0, 6, status);
865 	if (err) {
866 		DPRINTF(("%s: dfu_getstatus failed!\n",
867 		    USBDEVNAME(sc->atu_dev)));
868 		return;
869 	}
870 
871 	DPRINTFN(15, ("%s: sending remap\n", USBDEVNAME(sc->atu_dev)));
872 	err = atu_usb_request(sc, DFU_REMAP, 0, 0, 0, NULL);
873 	if ((err) && (! sc->atu_quirk & ATU_QUIRK_NO_REMAP)) {
874 		DPRINTF(("%s: remap failed!\n", USBDEVNAME(sc->atu_dev)));
875 		return;
876 	}
877 
878 	/* after a lot of trying and measuring I found out the device needs
879 	 * about 56 miliseconds after sending the remap command before
880 	 * it's ready to communicate again. So we'll wait just a little bit
881 	 * longer than that to be sure...
882 	 */
883 	usbd_delay_ms(sc->atu_udev, 56+100);
884 
885 	printf("%s: reattaching after firmware upload\n",
886 	    USBDEVNAME(sc->atu_dev));
887 	usb_needs_reattach(sc->atu_udev);
888 }
889 
890 void
891 atu_external_firmware(struct device *arg)
892 {
893 	struct atu_softc *sc = (struct atu_softc *)arg;
894 	u_char	*ptr = NULL, *firm = NULL;
895 	int	block_size, block = 0, err, i;
896 	size_t	bytes_left = 0;
897 
898 	for (i = 0; i < sizeof(atu_radfirm)/sizeof(atu_radfirm[0]); i++)
899 		if (sc->atu_radio == atu_radfirm[i].atur_type) {
900 			firm = atu_radfirm[i].atur_external;
901 			bytes_left = atu_radfirm[i].atur_external_sz;
902 		}
903 
904 	if (firm == NULL) {
905 		printf("%s: no firmware found\n", USBDEVNAME(sc->atu_dev));
906 		return;
907 	}
908 	ptr = firm;
909 
910 	while (bytes_left) {
911 		if (bytes_left > 1024)
912 			block_size = 1024;
913 		else
914 			block_size = bytes_left;
915 
916 		DPRINTFN(15, ("%s: block:%d size:%d\n",
917 		    USBDEVNAME(sc->atu_dev), block, block_size));
918 		err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e,
919 		    0x0802, block, block_size, ptr);
920 		if (err) {
921 			DPRINTF(("%s: could not load external firmware "
922 			    "block\n", USBDEVNAME(sc->atu_dev)));
923 			return;
924 		}
925 
926 		ptr += block_size;
927 		block++;
928 		bytes_left -= block_size;
929 	}
930 
931 	err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0802,
932 	    block, 0, NULL);
933 	if (err) {
934 		DPRINTF(("%s: could not load last zero-length firmware "
935 		    "block\n", USBDEVNAME(sc->atu_dev)));
936 		return;
937 	}
938 
939 	/*
940 	 * The SMC2662w V.4 seems to require some time to do it's thing with
941 	 * the external firmware... 20 ms isn't enough, but 21 ms works 100
942 	 * times out of 100 tries. We'll wait a bit longer just to be sure
943 	 */
944 	if (sc->atu_quirk & ATU_QUIRK_FW_DELAY)
945 		usbd_delay_ms(sc->atu_udev, 21 + 100);
946 
947 	DPRINTFN(10, ("%s: external firmware upload done\n",
948 	    USBDEVNAME(sc->atu_dev)));
949 	/* complete configuration after the firmwares have been uploaded */
950 	atu_complete_attach(sc);
951 }
952 
953 int
954 atu_get_card_config(struct atu_softc *sc)
955 {
956 	struct ieee80211com		*ic = &sc->sc_ic;
957 	struct atu_rfmd_conf		rfmd_conf;
958 	struct atu_intersil_conf	intersil_conf;
959 	int				err;
960 
961 	switch (sc->atu_radio) {
962 
963 	case RadioRFMD:
964 	case RadioRFMD2958:
965 	case RadioRFMD2958_SMC:
966 		err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33,
967 		    0x0a02, 0x0000, sizeof(rfmd_conf),
968 		    (u_int8_t *)&rfmd_conf);
969 		if (err) {
970 			DPRINTF(("%s: could not get rfmd config!\n",
971 			    USBDEVNAME(sc->atu_dev)));
972 			return err;
973 		}
974 		memcpy(ic->ic_myaddr, rfmd_conf.MACAddr, IEEE80211_ADDR_LEN);
975 		break;
976 
977 	case RadioIntersil:
978 		err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33,
979 		    0x0902, 0x0000, sizeof(intersil_conf),
980 		    (u_int8_t *)&intersil_conf);
981 		if (err) {
982 			DPRINTF(("%s: could not get intersil config!\n",
983 			    USBDEVNAME(sc->atu_dev)));
984 			return err;
985 		}
986 		memcpy(ic->ic_myaddr, intersil_conf.MACAddr,
987 		    IEEE80211_ADDR_LEN);
988 		break;
989 	}
990 	return 0;
991 }
992 
993 /*
994  * Probe for an AT76c503 chip.
995  */
996 USB_MATCH(atu)
997 {
998 	USB_MATCH_START(atu, uaa);
999 	int			i;
1000 
1001 	if (!uaa->iface)
1002 		return(UMATCH_NONE);
1003 
1004 	for (i = 0; i < sizeof(atu_devs)/sizeof(atu_devs[0]); i++) {
1005 		struct atu_type *t = &atu_devs[i];
1006 
1007 		if (uaa->vendor == t->atu_vid &&
1008 		    uaa->product == t->atu_pid) {
1009 			return(UMATCH_VENDOR_PRODUCT);
1010 		}
1011 	}
1012 	return(UMATCH_NONE);
1013 }
1014 
1015 int
1016 atu_media_change(struct ifnet *ifp)
1017 {
1018 #ifdef ATU_DEBUG
1019 	struct atu_softc	*sc = ifp->if_softc;
1020 #endif /* ATU_DEBUG */
1021 	int			err;
1022 
1023 	DPRINTFN(10, ("%s: atu_media_change\n", USBDEVNAME(sc->atu_dev)));
1024 
1025 	err = ieee80211_media_change(ifp);
1026 	if (err == ENETRESET) {
1027 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
1028 		    (IFF_RUNNING|IFF_UP))
1029 			atu_init(ifp);
1030 		err = 0;
1031 	}
1032 
1033 	return (err);
1034 }
1035 
1036 void
1037 atu_media_status(struct ifnet *ifp, struct ifmediareq *req)
1038 {
1039 #ifdef ATU_DEBUG
1040 	struct atu_softc	*sc = ifp->if_softc;
1041 #endif /* ATU_DEBUG */
1042 
1043 	DPRINTFN(10, ("%s: atu_media_status\n", USBDEVNAME(sc->atu_dev)));
1044 
1045 	ieee80211_media_status(ifp, req);
1046 }
1047 
1048 void
1049 atu_task(void *arg)
1050 {
1051 	struct atu_softc	*sc = (struct atu_softc *)arg;
1052 	struct ieee80211com	*ic = &sc->sc_ic;
1053 	usbd_status		err;
1054 	int			s;
1055 
1056 	DPRINTFN(10, ("%s: atu_task\n", USBDEVNAME(sc->atu_dev)));
1057 
1058 	if (sc->sc_state != ATU_S_OK)
1059 		return;
1060 
1061 	switch (sc->sc_cmd) {
1062 	case ATU_C_SCAN:
1063 
1064 		err = atu_start_scan(sc);
1065 		if (err) {
1066 			DPRINTFN(1, ("%s: atu_init: couldn't start scan!\n",
1067 			    USBDEVNAME(sc->atu_dev)));
1068 			return;
1069 		}
1070 
1071 		err = atu_wait_completion(sc, CMD_START_SCAN, NULL);
1072 		if (err) {
1073 			DPRINTF(("%s: atu_init: error waiting for scan\n",
1074 			    USBDEVNAME(sc->atu_dev)));
1075 			return;
1076 		}
1077 
1078 		DPRINTF(("%s: ==========================> END OF SCAN!\n",
1079 		    USBDEVNAME(sc->atu_dev)));
1080 
1081 		s = splnet();
1082 		/* ieee80211_next_scan(ifp); */
1083 		ieee80211_end_scan(ic);
1084 		splx(s);
1085 
1086 		DPRINTF(("%s: ----------------------======> END OF SCAN2!\n",
1087 		    USBDEVNAME(sc->atu_dev)));
1088 		break;
1089 
1090 	case ATU_C_JOIN:
1091 		atu_join(sc, ic->ic_bss);
1092 	}
1093 }
1094 
1095 int
1096 atu_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1097 {
1098 	struct ifnet		*ifp = &ic->ic_if;
1099 	struct atu_softc	*sc = ifp->if_softc;
1100 	enum ieee80211_state	ostate = ic->ic_state;
1101 
1102 	DPRINTFN(10, ("%s: atu_newstate: %s -> %s\n", USBDEVNAME(sc->atu_dev),
1103 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate]));
1104 
1105 	switch (nstate) {
1106 	case IEEE80211_S_SCAN:
1107 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
1108 		    sizeof(ic->ic_chan_active));
1109 		ieee80211_free_allnodes(ic);
1110 
1111 		/* tell the event thread that we want a scan */
1112 		sc->sc_cmd = ATU_C_SCAN;
1113 		usb_add_task(sc->atu_udev, &sc->sc_task);
1114 
1115 		/* handle this ourselves */
1116 		ic->ic_state = nstate;
1117 		return (0);
1118 
1119 	case IEEE80211_S_AUTH:
1120 	case IEEE80211_S_RUN:
1121 		if (ostate == IEEE80211_S_SCAN) {
1122 			sc->sc_cmd = ATU_C_JOIN;
1123 			usb_add_task(sc->atu_udev, &sc->sc_task);
1124 		}
1125 		break;
1126 	default:
1127 		/* nothing to do */
1128 		break;
1129 	}
1130 
1131 	return (*sc->sc_newstate)(ic, nstate, arg);
1132 }
1133 
1134 /*
1135  * Attach the interface. Allocate softc structures, do
1136  * setup and ethernet/BPF attach.
1137  */
1138 USB_ATTACH(atu)
1139 {
1140 	USB_ATTACH_START(atu, sc, uaa);
1141 	char				devinfo[1024];
1142 	usbd_status			err;
1143 	usbd_device_handle		dev = uaa->device;
1144 	u_int8_t			mode, channel;
1145 	int i;
1146 
1147 	sc->sc_state = ATU_S_UNCONFIG;
1148 
1149 	usbd_devinfo(uaa->device, 0, devinfo, sizeof devinfo);
1150 	USB_ATTACH_SETUP;
1151 	printf("%s: %s\n", USBDEVNAME(sc->atu_dev), devinfo);
1152 
1153 	err = usbd_set_config_no(dev, ATU_CONFIG_NO, 1);
1154 	if (err) {
1155 		printf("%s: setting config no failed\n",
1156 		    USBDEVNAME(sc->atu_dev));
1157 		USB_ATTACH_ERROR_RETURN;
1158 	}
1159 
1160 	err = usbd_device2interface_handle(dev, ATU_IFACE_IDX, &sc->atu_iface);
1161 	if (err) {
1162 		printf("%s: getting interface handle failed\n",
1163 		    USBDEVNAME(sc->atu_dev));
1164 		USB_ATTACH_ERROR_RETURN;
1165 	}
1166 
1167 	sc->atu_unit = self->dv_unit;
1168 	sc->atu_udev = dev;
1169 
1170 	/*
1171 	 * look up the radio_type for the device
1172 	 * basically does the same as USB_MATCH
1173 	 */
1174 	for (i = 0; i < sizeof(atu_devs)/sizeof(atu_devs[0]); i++) {
1175 		struct atu_type *t = &atu_devs[i];
1176 
1177 		if (uaa->vendor == t->atu_vid &&
1178 		    uaa->product == t->atu_pid) {
1179 			sc->atu_radio = t->atu_radio;
1180 			sc->atu_quirk = t->atu_quirk;
1181 		}
1182 	}
1183 
1184 	/*
1185 	 * Check in the interface descriptor if we're in DFU mode
1186 	 * If we're in DFU mode, we upload the external firmware
1187 	 * If we're not, the PC must have rebooted without power-cycling
1188 	 * the device.. I've tried this out, a reboot only requeres the
1189 	 * external firmware to be reloaded :)
1190 	 *
1191 	 * Hmm. The at76c505a doesn't report a DFU descriptor when it's
1192 	 * in DFU mode... Let's just try to get the opmode
1193 	 */
1194 	err = atu_get_opmode(sc, &mode);
1195 	DPRINTFN(20, ("%s: opmode: %d\n", USBDEVNAME(sc->atu_dev), mode));
1196 	if (err || (mode != MODE_NETCARD && mode != MODE_NOFLASHNETCARD)) {
1197 		DPRINTF(("%s: starting internal firmware download\n",
1198 		    USBDEVNAME(sc->atu_dev)));
1199 
1200 		atu_internal_firmware((struct device *)sc);
1201 		/*
1202 		 * atu_internal_firmware will cause a reset of the device
1203 		 * so we don't want to do any more configuration after this
1204 		 * point.
1205 		 */
1206 		USB_ATTACH_SUCCESS_RETURN;
1207 	}
1208 
1209 	uaa->iface = sc->atu_iface;
1210 
1211 	if (mode != MODE_NETCARD) {
1212 		DPRINTFN(15, ("%s: device needs external firmware\n",
1213 		    USBDEVNAME(sc->atu_dev)));
1214 
1215 		if (mode != MODE_NOFLASHNETCARD) {
1216 			DPRINTF(("%s: unexpected opmode=%d\n",
1217 			    USBDEVNAME(sc->atu_dev), mode));
1218 		}
1219 
1220 		/*
1221 		 * There is no difference in opmode before and after external
1222 		 * firmware upload with the SMC2662 V.4 . So instead we'll try
1223 		 * to read the channel number. If we succeed, external
1224 		 * firmwaremust have been already uploaded...
1225 		 */
1226 		if (sc->atu_radio != RadioIntersil) {
1227 			err = atu_get_mib(sc, MIB_PHY__CHANNEL, &channel);
1228 			if (!err) {
1229 				DPRINTF(("%s: external firmware has already"
1230 				    " been downloaded\n",
1231 				    USBDEVNAME(sc->atu_dev)));
1232 				atu_complete_attach(sc);
1233 				USB_ATTACH_SUCCESS_RETURN;
1234 			}
1235 		}
1236 
1237 		atu_external_firmware((struct device *)sc);
1238 
1239 		/*
1240 		 * atu_external_firmware will call atu_complete_attach after
1241 		 * it's finished so we can just return.
1242 		 */
1243 	} else {
1244 		/* all the firmwares are in place, so complete the attach */
1245 		atu_complete_attach(sc);
1246 	}
1247 
1248 	USB_ATTACH_SUCCESS_RETURN;
1249 }
1250 
1251 void
1252 atu_complete_attach(struct atu_softc *sc)
1253 {
1254 	struct ieee80211com		*ic = &sc->sc_ic;
1255 	struct ifnet			*ifp = &ic->ic_if;
1256 	usb_interface_descriptor_t	*id;
1257 	usb_endpoint_descriptor_t	*ed;
1258 	usbd_status			err;
1259 	int				i;
1260 #ifdef ATU_DEBUG
1261 	struct atu_fw			fw;
1262 #endif
1263 
1264 	id = usbd_get_interface_descriptor(sc->atu_iface);
1265 
1266 	/* Find endpoints. */
1267 	for (i = 0; i < id->bNumEndpoints; i++) {
1268 		ed = usbd_interface2endpoint_descriptor(sc->atu_iface, i);
1269 		if (!ed) {
1270 			DPRINTF(("%s: num_endp:%d\n", USBDEVNAME(sc->atu_dev),
1271 			    sc->atu_iface->idesc->bNumEndpoints));
1272 			DPRINTF(("%s: couldn't get ep %d\n",
1273 			    USBDEVNAME(sc->atu_dev), i));
1274 			return;
1275 		}
1276 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1277 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1278 			sc->atu_ed[ATU_ENDPT_RX] = ed->bEndpointAddress;
1279 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1280 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1281 			sc->atu_ed[ATU_ENDPT_TX] = ed->bEndpointAddress;
1282 		}
1283 	}
1284 
1285 	/* read device config & get MAC address */
1286 	err = atu_get_card_config(sc);
1287 	if (err) {
1288 		printf("\n%s: could not get card cfg!\n",
1289 		    USBDEVNAME(sc->atu_dev));
1290 		return;
1291 	}
1292 
1293 #ifdef ATU_DEBUG
1294 	/* DEBUG : try to get firmware version */
1295 	err = atu_get_mib(sc, MIB_FW_VERSION, sizeof(fw), 0,
1296 	    (u_int8_t *)&fw);
1297 	if (!err) {
1298 		DPRINTFN(15, ("%s: firmware: maj:%d min:%d patch:%d "
1299 		    "build:%d\n", USBDEVNAME(sc->atu_dev), fw.major, fw.minor,
1300 		    fw.patch, fw.build));
1301 	} else {
1302 		DPRINTF(("%s: get firmware version failed\n",
1303 		    USBDEVNAME(sc->atu_dev)));
1304 	}
1305 #endif /* ATU_DEBUG */
1306 
1307 	/* Show the world our MAC address */
1308 	printf("%s: MAC address %s\n", USBDEVNAME(sc->atu_dev),
1309 	    ether_sprintf(ic->ic_myaddr));
1310 
1311 	sc->atu_cdata.atu_tx_inuse = 0;
1312 	sc->atu_encrypt = ATU_WEP_OFF;
1313 	sc->atu_wepkeylen = ATU_WEP_104BITS;
1314 	sc->atu_wepkey = 0;
1315 
1316 	bzero(sc->atu_bssid, ETHER_ADDR_LEN);
1317 	sc->atu_ssidlen = strlen(ATU_DEFAULT_SSID);
1318 	memcpy(sc->atu_ssid, ATU_DEFAULT_SSID, sc->atu_ssidlen);
1319 	sc->atu_channel = ATU_DEFAULT_CHANNEL;
1320 	sc->atu_desired_channel = IEEE80211_CHAN_ANY;
1321 	sc->atu_mode = INFRASTRUCTURE_MODE;
1322 	sc->atu_encrypt = ATU_WEP_OFF;
1323 
1324 	ic->ic_softc = sc;
1325 	ic->ic_phytype = IEEE80211_T_DS;
1326 	ic->ic_opmode = IEEE80211_M_STA;
1327 	ic->ic_state = IEEE80211_S_INIT;
1328 #ifdef FIXME
1329 	ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_WEP | IEEE80211_C_SCANALL;
1330 #else
1331 	ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_WEP;
1332 #endif
1333 
1334 	i = 0;
1335 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[i++] = 2;
1336 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[i++] = 4;
1337 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[i++] = 11;
1338 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[i++] = 22;
1339 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = i;
1340 
1341 	for (i = 1; i <= 14; i++) {
1342 		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B |
1343 		    IEEE80211_CHAN_PASSIVE;
1344 		ic->ic_channels[i].ic_freq = ieee80211_ieee2mhz(i,
1345 		    ic->ic_channels[i].ic_flags);
1346 	}
1347 
1348 	ic->ic_ibss_chan = &ic->ic_channels[0];
1349 
1350 	ifp->if_softc = sc;
1351 	memcpy(ifp->if_xname, USBDEVNAME(sc->atu_dev), IFNAMSIZ);
1352 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1353 	ifp->if_init = atu_init;
1354 	ifp->if_stop = atu_stop;
1355 	ifp->if_start = atu_start;
1356 	ifp->if_ioctl = atu_ioctl;
1357 	ifp->if_watchdog = atu_watchdog;
1358 	ifp->if_mtu = ATU_DEFAULT_MTU;
1359 	IFQ_SET_READY(&ifp->if_snd);
1360 
1361 	/* Call MI attach routine. */
1362 	if_attach(ifp);
1363 	ieee80211_ifattach(ifp);
1364 
1365 	sc->sc_newstate = ic->ic_newstate;
1366 	ic->ic_newstate = atu_newstate;
1367 
1368 	/* setup ifmedia interface */
1369 	ieee80211_media_init(ifp, atu_media_change, atu_media_status);
1370 
1371 	usb_init_task(&sc->sc_task, atu_task, sc);
1372 
1373 	sc->sc_state = ATU_S_OK;
1374 }
1375 
1376 USB_DETACH(atu)
1377 {
1378 	USB_DETACH_START(atu, sc);
1379 	struct ifnet		*ifp = &sc->sc_ic.ic_if;
1380 
1381 	DPRINTFN(10, ("%s: atu_detach state=%d\n", USBDEVNAME(sc->atu_dev),
1382 	    sc->sc_state));
1383 
1384 	if (sc->sc_state != ATU_S_UNCONFIG) {
1385 		atu_stop(ifp, 1);
1386 
1387 		ieee80211_ifdetach(ifp);
1388 		if_detach(ifp);
1389 
1390 		if (sc->atu_ep[ATU_ENDPT_TX] != NULL)
1391 			usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_TX]);
1392 		if (sc->atu_ep[ATU_ENDPT_RX] != NULL)
1393 			usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_RX]);
1394 
1395 		usb_rem_task(sc->atu_udev, &sc->sc_task);
1396 	}
1397 
1398 	return(0);
1399 }
1400 
1401 int
1402 atu_activate(device_ptr_t self, enum devact act)
1403 {
1404 	struct atu_softc *sc = (struct atu_softc *)self;
1405 
1406 	switch (act) {
1407 	case DVACT_ACTIVATE:
1408 		return (EOPNOTSUPP);
1409 		break;
1410 	case DVACT_DEACTIVATE:
1411 		if (sc->sc_state != ATU_S_UNCONFIG) {
1412 			if_deactivate(&sc->atu_ec.ec_if);
1413 			sc->sc_state = ATU_S_DEAD;
1414 		}
1415 		break;
1416 	}
1417 	return (0);
1418 }
1419 
1420 /*
1421  * Initialize an RX descriptor and attach an MBUF cluster.
1422  */
1423 int
1424 atu_newbuf(struct atu_softc *sc, struct atu_chain *c, struct mbuf *m)
1425 {
1426 	struct mbuf		*m_new = NULL;
1427 
1428 	if (m == NULL) {
1429 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1430 		if (m_new == NULL) {
1431 			DPRINTF(("%s: no memory for rx list\n",
1432 			    USBDEVNAME(sc->atu_dev)));
1433 			return(ENOBUFS);
1434 		}
1435 
1436 		MCLGET(m_new, M_DONTWAIT);
1437 		if (!(m_new->m_flags & M_EXT)) {
1438 			DPRINTF(("%s: no memory for rx list\n",
1439 			    USBDEVNAME(sc->atu_dev)));
1440 			m_freem(m_new);
1441 			return(ENOBUFS);
1442 		}
1443 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1444 	} else {
1445 		m_new = m;
1446 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1447 		m_new->m_data = m_new->m_ext.ext_buf;
1448 	}
1449 	c->atu_mbuf = m_new;
1450 	return(0);
1451 }
1452 
1453 int
1454 atu_rx_list_init(struct atu_softc *sc)
1455 {
1456 	struct atu_cdata	*cd = &sc->atu_cdata;
1457 	struct atu_chain	*c;
1458 	int			i;
1459 
1460 	DPRINTFN(15, ("%s: atu_rx_list_init: enter\n",
1461 	    USBDEVNAME(sc->atu_dev)));
1462 
1463 	for (i = 0; i < ATU_RX_LIST_CNT; i++) {
1464 		c = &cd->atu_rx_chain[i];
1465 		c->atu_sc = sc;
1466 		c->atu_idx = i;
1467 		if (c->atu_xfer == NULL) {
1468 			c->atu_xfer = usbd_alloc_xfer(sc->atu_udev);
1469 			if (c->atu_xfer == NULL)
1470 				return (ENOBUFS);
1471 			c->atu_buf = usbd_alloc_buffer(c->atu_xfer,
1472 			    ATU_RX_BUFSZ);
1473 			if (c->atu_buf == NULL) /* XXX free xfer */
1474 				return (ENOBUFS);
1475 			if (atu_newbuf(sc, c, NULL) == ENOBUFS) /* XXX free? */
1476 				return(ENOBUFS);
1477 		}
1478 	}
1479 	return (0);
1480 }
1481 
1482 int
1483 atu_tx_list_init(struct atu_softc *sc)
1484 {
1485 	struct atu_cdata	*cd = &sc->atu_cdata;
1486 	struct atu_chain	*c;
1487 	int			i;
1488 
1489 	DPRINTFN(15, ("%s: atu_tx_list_init\n",
1490 	    USBDEVNAME(sc->atu_dev)));
1491 
1492 	SLIST_INIT(&cd->atu_tx_free);
1493 	sc->atu_cdata.atu_tx_inuse = 0;
1494 
1495 	for (i = 0; i < ATU_TX_LIST_CNT; i++) {
1496 		c = &cd->atu_tx_chain[i];
1497 		c->atu_sc = sc;
1498 		c->atu_idx = i;
1499 		if (c->atu_xfer == NULL) {
1500 			c->atu_xfer = usbd_alloc_xfer(sc->atu_udev);
1501 			if (c->atu_xfer == NULL)
1502 				return(ENOBUFS);
1503 			c->atu_mbuf = NULL;
1504 			c->atu_buf = usbd_alloc_buffer(c->atu_xfer,
1505 			    ATU_TX_BUFSZ);
1506 			if (c->atu_buf == NULL)
1507 				return(ENOBUFS); /* XXX free xfer */
1508 			SLIST_INSERT_HEAD(&cd->atu_tx_free, c, atu_list);
1509 		}
1510 	}
1511 	return(0);
1512 }
1513 
1514 void
1515 atu_xfer_list_free(struct atu_softc *sc, struct atu_chain *ch,
1516     int listlen)
1517 {
1518 	int			i;
1519 
1520 	/* Free resources. */
1521 	for (i = 0; i < listlen; i++) {
1522 		if (ch[i].atu_buf != NULL)
1523 			ch[i].atu_buf = NULL;
1524 		if (ch[i].atu_mbuf != NULL) {
1525 			m_freem(ch[i].atu_mbuf);
1526 			ch[i].atu_mbuf = NULL;
1527 		}
1528 		if (ch[i].atu_xfer != NULL) {
1529 			usbd_free_xfer(ch[i].atu_xfer);
1530 			ch[i].atu_xfer = NULL;
1531 		}
1532 	}
1533 }
1534 
1535 /*
1536  * A frame has been uploaded: pass the resulting mbuf chain up to
1537  * the higher level protocols.
1538  */
1539 void
1540 atu_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1541 {
1542 	struct atu_chain	*c = (struct atu_chain *)priv;
1543 	struct atu_softc	*sc = c->atu_sc;
1544 	struct ieee80211com	*ic = &sc->sc_ic;
1545 	struct ifnet		*ifp = &ic->ic_if;
1546 	struct atu_rx_hdr	*h;
1547 	struct ieee80211_frame	*wh;
1548 	struct ieee80211_node	*ni;
1549 	struct mbuf		*m;
1550 	u_int32_t		len;
1551 	int			s;
1552 
1553 	DPRINTFN(25, ("%s: atu_rxeof\n", USBDEVNAME(sc->atu_dev)));
1554 
1555 	if (sc->sc_state != ATU_S_OK)
1556 		return;
1557 
1558 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP))
1559 		goto done;
1560 
1561 	if (status != USBD_NORMAL_COMPLETION) {
1562 		DPRINTF(("%s: status != USBD_NORMAL_COMPLETION\n",
1563 		    USBDEVNAME(sc->atu_dev)));
1564 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1565 			return;
1566 		}
1567 #if 0
1568 		if (status == USBD_IOERROR) {
1569 			DPRINTF(("%s: rx: EEK! lost device?\n",
1570 			    USBDEVNAME(sc->atu_dev)));
1571 
1572 			/*
1573 			 * My experience with USBD_IOERROR is that trying to
1574 			 * restart the transfer will always fail and we'll
1575 			 * keep on looping restarting transfers untill someone
1576 			 * pulls the plug of the device.
1577 			 * So we don't restart the transfer, but just let it
1578 			 * die... If someone knows of a situation where we can
1579 			 * recover from USBD_IOERROR, let me know.
1580 			 */
1581 			splx(s);
1582 			return;
1583 		}
1584 #endif /* 0 */
1585 
1586 		if (usbd_ratecheck(&sc->atu_rx_notice)) {
1587 			DPRINTF(("%s: usb error on rx: %s\n",
1588 			    USBDEVNAME(sc->atu_dev), usbd_errstr(status)));
1589 		}
1590 		if (status == USBD_STALLED)
1591 			usbd_clear_endpoint_stall(
1592 			    sc->atu_ep[ATU_ENDPT_RX]);
1593 		goto done;
1594 	}
1595 
1596 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1597 
1598 	if (len <= 1) {
1599 		DPRINTF(("%s: atu_rxeof: too short\n",
1600 		    USBDEVNAME(sc->atu_dev)));
1601 		goto done;
1602 	}
1603 
1604 	h = (struct atu_rx_hdr *)c->atu_buf;
1605 	len = UGETW(h->length) - 4; /* XXX magic number */
1606 
1607 	m = c->atu_mbuf;
1608 	memcpy(mtod(m, char *), c->atu_buf + ATU_RX_HDRLEN, len);
1609 	m->m_pkthdr.rcvif = ifp;
1610 	m->m_pkthdr.len = m->m_len = len;
1611 
1612 	wh = mtod(m, struct ieee80211_frame *);
1613 	ni = ieee80211_find_rxnode(ic, wh);
1614 
1615 	ifp->if_ipackets++;
1616 
1617 	s = splnet();
1618 
1619 	if (atu_newbuf(sc, c, NULL) == ENOBUFS) {
1620 		ifp->if_ierrors++;
1621 		goto done1; /* XXX if we can't allocate, why restart it? */
1622 	}
1623 
1624 #if NBPFILTER > 0
1625 	if (ifp->if_bpf)
1626 		bpf_mtap(ifp->if_bpf, m);
1627 #endif
1628 
1629 	ieee80211_input(ifp, m, ni, h->rssi, UGETDW(h->rx_time));
1630 
1631 	ieee80211_release_node(ic, ni);
1632 done1:
1633 	splx(s);
1634 done:
1635 	/* Setup new transfer. */
1636 	usbd_setup_xfer(c->atu_xfer, sc->atu_ep[ATU_ENDPT_RX], c, c->atu_buf,
1637 	    ATU_RX_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1638 		atu_rxeof);
1639 	usbd_transfer(c->atu_xfer);
1640 }
1641 
1642 /*
1643  * A frame was downloaded to the chip. It's safe for us to clean up
1644  * the list buffers.
1645  */
1646 void
1647 atu_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1648 {
1649 	struct atu_chain	*c = (struct atu_chain *)priv;
1650 	struct atu_softc	*sc = c->atu_sc;
1651 	struct ifnet		*ifp = &sc->sc_ic.ic_if;
1652 	usbd_status		err;
1653 	int			s;
1654 
1655 	DPRINTFN(25, ("%s: atu_txeof status=%d\n", USBDEVNAME(sc->atu_dev),
1656 	    status));
1657 
1658 	if (status != USBD_NORMAL_COMPLETION) {
1659 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1660 			return;
1661 
1662 		DPRINTF(("%s: usb error on tx: %s\n", USBDEVNAME(sc->atu_dev),
1663 		    usbd_errstr(status)));
1664 		if (status == USBD_STALLED)
1665 			usbd_clear_endpoint_stall(sc->atu_ep[ATU_ENDPT_TX]);
1666 		return;
1667 	}
1668 
1669 	usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL, &err);
1670 
1671 	if (err)
1672 		ifp->if_oerrors++;
1673 	else
1674 		ifp->if_opackets++;
1675 
1676 	m_freem(c->atu_mbuf);
1677 	c->atu_mbuf = NULL;
1678 
1679 	s = splnet();
1680 	SLIST_INSERT_HEAD(&sc->atu_cdata.atu_tx_free, c, atu_list);
1681 	sc->atu_cdata.atu_tx_inuse--;
1682 	if (sc->atu_cdata.atu_tx_inuse == 0)
1683 		ifp->if_timer = 0;
1684 	ifp->if_flags &= ~IFF_OACTIVE;
1685 	splx(s);
1686 
1687 	atu_start(ifp);
1688 }
1689 
1690 u_int8_t
1691 atu_calculate_padding(int size)
1692 {
1693 	size %= 64;
1694 
1695 	if (size < 50)
1696 		return (50 - size);
1697 	if (size >=61)
1698 		return (64 + 50 - size);
1699 	return (0);
1700 }
1701 
1702 int
1703 atu_tx_start(struct atu_softc *sc, struct ieee80211_node *ni,
1704     struct atu_chain *c, struct mbuf *m)
1705 {
1706 	struct ifnet		*ifp = &sc->sc_ic.ic_if;
1707 	int			len;
1708 	struct atu_tx_hdr	*h;
1709 	usbd_status		err;
1710 	u_int8_t		pad;
1711 
1712 	DPRINTFN(25, ("%s: atu_tx_start\n", USBDEVNAME(sc->atu_dev)));
1713 
1714 	/* Don't try to send when we're shutting down the driver */
1715 	if (sc->sc_state != ATU_S_OK)
1716 		return(EIO);
1717 
1718 	/*
1719 	 * Copy the mbuf data into a contiguous buffer, leaving
1720 	 * enough room for the atmel headers
1721 	 */
1722 	len = m->m_pkthdr.len;
1723 
1724 	m_copydata(m, 0, m->m_pkthdr.len, c->atu_buf + ATU_TX_HDRLEN);
1725 
1726 	h = (struct atu_tx_hdr *)c->atu_buf;
1727 	memset(h, 0, ATU_TX_HDRLEN);
1728 	USETW(h->length, len);
1729 	h->tx_rate = 4; /* XXX rate = auto */
1730 	len += ATU_TX_HDRLEN;
1731 
1732 	pad = atu_calculate_padding(len);
1733 	len += pad;
1734 	h->padding = pad;
1735 
1736 	c->atu_length = len;
1737 	c->atu_mbuf = m;
1738 
1739 	usbd_setup_xfer(c->atu_xfer, sc->atu_ep[ATU_ENDPT_TX],
1740 	    c, c->atu_buf, c->atu_length, USBD_NO_COPY, ATU_TX_TIMEOUT,
1741 	    atu_txeof);
1742 
1743 	/* Let's get this thing into the air! */
1744 	c->atu_in_xfer = 1;
1745 	err = usbd_transfer(c->atu_xfer);
1746 	if (err != USBD_IN_PROGRESS) {
1747 		atu_stop(ifp, 0);
1748 		return(EIO);
1749 	}
1750 
1751 	return (0);
1752 }
1753 
1754 void
1755 atu_start(struct ifnet *ifp)
1756 {
1757 	struct atu_softc	*sc = ifp->if_softc;
1758 	struct ieee80211com	*ic = &sc->sc_ic;
1759 	struct atu_cdata	*cd = &sc->atu_cdata;
1760 	struct ieee80211_node	*ni;
1761 	struct ieee80211_frame	*wh;
1762 	struct atu_chain	*c;
1763 	struct mbuf		*m = NULL;
1764 	int			s;
1765 
1766 	DPRINTFN(25, ("%s: atu_start: enter\n", USBDEVNAME(sc->atu_dev)));
1767 
1768 	s = splnet();
1769 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
1770 		splx(s);
1771 		return;
1772 	}
1773 	if (ifp->if_flags & IFF_OACTIVE) {
1774 		DPRINTFN(30, ("%s: atu_start: IFF_OACTIVE\n",
1775 		    USBDEVNAME(sc->atu_dev)));
1776 		splx(s);
1777 		return;
1778 	}
1779 
1780 	for (;;) {
1781 		/* grab a TX buffer */
1782 		s = splnet();
1783 		c = SLIST_FIRST(&cd->atu_tx_free);
1784 		if (c != NULL) {
1785 			SLIST_REMOVE_HEAD(&cd->atu_tx_free, atu_list);
1786 			cd->atu_tx_inuse++;
1787 			if (cd->atu_tx_inuse == ATU_TX_LIST_CNT)
1788 				ifp->if_flags |= IFF_OACTIVE;
1789 		}
1790 		splx(s);
1791 		if (c == NULL) {
1792 			DPRINTFN(10, ("%s: out of tx xfers\n",
1793 			    USBDEVNAME(sc->atu_dev)));
1794 			ifp->if_flags |= IFF_OACTIVE;
1795 			break;
1796 		}
1797 
1798 		/*
1799 		 * Poll the management queue for frames, it has priority over
1800 		 * normal data frames.
1801 		 */
1802 		IF_DEQUEUE(&ic->ic_mgtq, m);
1803 		if (m == NULL) {
1804 			DPRINTFN(10, ("%s: atu_start: data packet\n",
1805 			    USBDEVNAME(sc->atu_dev)));
1806 			if (ic->ic_state != IEEE80211_S_RUN) {
1807 				DPRINTFN(25, ("%s: no data till running\n",
1808 				    USBDEVNAME(sc->atu_dev)));
1809 				/* put the xfer back on the list */
1810 				s = splnet();
1811 				SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
1812 				    atu_list);
1813 				cd->atu_tx_inuse--;
1814 				splx(s);
1815 				break;
1816 			}
1817 
1818 			IFQ_DEQUEUE(&ifp->if_snd, m);
1819 			if (m == NULL) {
1820 				DPRINTFN(25, ("%s: nothing to send\n",
1821 				    USBDEVNAME(sc->atu_dev)));
1822 				s = splnet();
1823 				SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
1824 				    atu_list);
1825 				cd->atu_tx_inuse--;
1826 				splx(s);
1827 				break;
1828 			}
1829 
1830 			/* XXX bpf listener goes here */
1831 
1832 			m = ieee80211_encap(ifp, m, &ni);
1833 			if (m == NULL)
1834 				goto bad;
1835 			wh = mtod(m, struct ieee80211_frame *);
1836 		} else {
1837 			DPRINTFN(25, ("%s: atu_start: mgmt packet\n",
1838 			    USBDEVNAME(sc->atu_dev)));
1839 
1840 			/*
1841 			 * Hack!  The referenced node pointer is in the
1842 			 * rcvif field of the packet header.  This is
1843 			 * placed there by ieee80211_mgmt_output because
1844 			 * we need to hold the reference with the frame
1845 			 * and there's no other way (other than packet
1846 			 * tags which we consider too expensive to use)
1847 			 * to pass it along.
1848 			 */
1849 			ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1850 			m->m_pkthdr.rcvif = NULL;
1851 
1852 			wh = mtod(m, struct ieee80211_frame *);
1853 			/* sc->sc_stats.ast_tx_mgmt++; */
1854 		}
1855 
1856 		if (atu_tx_start(sc, ni, c, m)) {
1857 bad:
1858 			s = splnet();
1859 			SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
1860 			    atu_list);
1861 			cd->atu_tx_inuse--;
1862 			splx(s);
1863 			/* ifp_if_oerrors++; */
1864 			if (ni != NULL)
1865 				ieee80211_release_node(ic, ni);
1866 			continue;
1867 		}
1868 		ifp->if_timer = 5;
1869 	}
1870 }
1871 
1872 int
1873 atu_init(struct ifnet *ifp)
1874 {
1875 	struct atu_softc	*sc = ifp->if_softc;
1876 	struct ieee80211com	*ic = &sc->sc_ic;
1877 	struct atu_chain	*c;
1878 	usbd_status		err;
1879 	int			i, s;
1880 
1881 	s = splnet();
1882 
1883 	DPRINTFN(10, ("%s: atu_init\n", USBDEVNAME(sc->atu_dev)));
1884 
1885 	if (ifp->if_flags & IFF_RUNNING) {
1886 		splx(s);
1887 		return(0);
1888 	}
1889 
1890 	/* Init TX ring */
1891 	if (atu_tx_list_init(sc))
1892 		printf("%s: tx list init failed\n", USBDEVNAME(sc->atu_dev));
1893 
1894 	/* Init RX ring */
1895 	if (atu_rx_list_init(sc))
1896 		printf("%s: rx list init failed\n", USBDEVNAME(sc->atu_dev));
1897 
1898 	/* Load the multicast filter. */
1899 	/*atu_setmulti(sc); */
1900 
1901 	/* Open RX and TX pipes. */
1902 	err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_RX],
1903 	    USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_RX]);
1904 	if (err) {
1905 		DPRINTF(("%s: open rx pipe failed: %s\n",
1906 		    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
1907 		splx(s);
1908 		return(EIO);
1909 	}
1910 
1911 	err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_TX],
1912 	    USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_TX]);
1913 	if (err) {
1914 		DPRINTF(("%s: open tx pipe failed: %s\n",
1915 		    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
1916 		splx(s);
1917 		return(EIO);
1918 	}
1919 
1920 	/* Start up the receive pipe. */
1921 	for (i = 0; i < ATU_RX_LIST_CNT; i++) {
1922 		c = &sc->atu_cdata.atu_rx_chain[i];
1923 
1924 		usbd_setup_xfer(c->atu_xfer, sc->atu_ep[ATU_ENDPT_RX], c,
1925 		    c->atu_buf, ATU_RX_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
1926 		    USBD_NO_TIMEOUT, atu_rxeof);
1927 		usbd_transfer(c->atu_xfer);
1928 	}
1929 
1930 	DPRINTFN(10, ("%s: starting up using MAC=%s\n",
1931 	    USBDEVNAME(sc->atu_dev), ether_sprintf(ic->ic_myaddr)));
1932 
1933 	/* Do initial setup */
1934 	err = atu_initial_config(sc);
1935 	if (err) {
1936 		DPRINTF(("%s: initial config failed!\n",
1937 		    USBDEVNAME(sc->atu_dev)));
1938 		splx(s);
1939 		return(EIO);
1940 	}
1941 	DPRINTFN(10, ("%s: initialised transceiver\n",
1942 	    USBDEVNAME(sc->atu_dev)));
1943 
1944 	/* sc->atu_rxfilt = ATU_RXFILT_UNICAST|ATU_RXFILT_BROADCAST; */
1945 
1946 	/* If we want promiscuous mode, set the allframes bit. */
1947 	/*
1948 	if (ifp->if_flags & IFF_PROMISC)
1949 		sc->atu_rxfilt |= ATU_RXFILT_PROMISC;
1950 	*/
1951 
1952 	ifp->if_flags |= IFF_RUNNING;
1953 	ifp->if_flags &= ~IFF_OACTIVE;
1954 	splx(s);
1955 
1956 	/* XXX the following HAS to be replaced */
1957 	s = splnet();
1958 	err = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1959 	if (err)
1960 		DPRINTFN(1, ("%s: atu_init: error calling "
1961 		    "ieee80211_net_state", USBDEVNAME(sc->atu_dev)));
1962 	splx(s);
1963 
1964 	return 0;
1965 }
1966 
1967 #ifdef ATU_DEBUG
1968 void
1969 atu_debug_print(struct atu_softc *sc)
1970 {
1971 	usbd_status		err;
1972 	u_int8_t		tmp[32];
1973 
1974 	/* DEBUG */
1975 	if ((err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, tmp)))
1976 		return;
1977 	DPRINTF(("%s: DEBUG: current BSSID=%s\n", USBDEVNAME(sc->atu_dev),
1978 	    ether_sprintf(tmp)));
1979 
1980 	if ((err = atu_get_mib(sc, MIB_MAC_MGMT__BEACON_PERIOD, tmp)))
1981 		return;
1982 	DPRINTF(("%s: DEBUG: beacon period=%d\n", USBDEVNAME(sc->atu_dev),
1983 	    tmp[0]));
1984 
1985 	if ((err = atu_get_mib(sc, MIB_MAC_WEP__PRIVACY_INVOKED, tmp)))
1986 		return;
1987 	DPRINTF(("%s: DEBUG: privacy invoked=%d\n", USBDEVNAME(sc->atu_dev),
1988 	    tmp[0]));
1989 
1990 	if ((err = atu_get_mib(sc, MIB_MAC_WEP__ENCR_LEVEL, tmp)))
1991 		return;
1992 	DPRINTF(("%s: DEBUG: encr_level=%d\n", USBDEVNAME(sc->atu_dev),
1993 	    tmp[0]));
1994 
1995 	if ((err = atu_get_mib(sc, MIB_MAC_WEP__ICV_ERROR_COUNT, tmp)))
1996 		return;
1997 	DPRINTF(("%s: DEBUG: icv error count=%d\n", USBDEVNAME(sc->atu_dev),
1998 	    *(short *)tmp));
1999 
2000 	if ((err = atu_get_mib(sc, MIB_MAC_WEP__EXCLUDED_COUNT, tmp)))
2001 		return;
2002 	DPRINTF(("%s: DEBUG: wep excluded count=%d\n",
2003 	    USBDEVNAME(sc->atu_dev), *(short *)tmp));
2004 
2005 	if ((err = atu_get_mib(sc, MIB_MAC_MGMT__POWER_MODE, tmp)))
2006 		return;
2007 	DPRINTF(("%s: DEBUG: power mode=%d\n", USBDEVNAME(sc->atu_dev),
2008 	    tmp[0]));
2009 
2010 	if ((err = atu_get_mib(sc, MIB_PHY__CHANNEL, tmp)))
2011 		return;
2012 	DPRINTF(("%s: DEBUG: channel=%d\n", USBDEVNAME(sc->atu_dev), tmp[0]));
2013 
2014 	if ((err = atu_get_mib(sc, MIB_PHY__REG_DOMAIN, tmp)))
2015 		return;
2016 	DPRINTF(("%s: DEBUG: reg domain=%d\n", USBDEVNAME(sc->atu_dev),
2017 	    tmp[0]));
2018 
2019 	if ((err = atu_get_mib(sc, MIB_LOCAL__SSID_SIZE, tmp)))
2020 		return;
2021 	DPRINTF(("%s: DEBUG: ssid size=%d\n", USBDEVNAME(sc->atu_dev),
2022 	    tmp[0]));
2023 
2024 	if ((err = atu_get_mib(sc, MIB_LOCAL__BEACON_ENABLE, tmp)))
2025 		return;
2026 	DPRINTF(("%s: DEBUG: beacon enable=%d\n", USBDEVNAME(sc->atu_dev),
2027 	    tmp[0]));
2028 
2029 	if ((err = atu_get_mib(sc, MIB_LOCAL__AUTO_RATE_FALLBACK, tmp)))
2030 		return;
2031 	DPRINTF(("%s: DEBUG: auto rate fallback=%d\n",
2032 	    USBDEVNAME(sc->atu_dev), tmp[0]));
2033 
2034 	if ((err = atu_get_mib(sc, MIB_MAC_ADDR__ADDR, tmp)))
2035 		return;
2036 	DPRINTF(("%s: DEBUG: mac addr=%s\n", USBDEVNAME(sc->atu_dev),
2037 	    ether_sprintf(tmp)));
2038 
2039 	if ((err = atu_get_mib(sc, MIB_MAC__DESIRED_SSID, tmp)))
2040 		return;
2041 	DPRINTF(("%s: DEBUG: desired ssid=%s\n", USBDEVNAME(sc->atu_dev),
2042 	    tmp));
2043 
2044 	if ((err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_ESSID, tmp)))
2045 		return;
2046 	DPRINTF(("%s: DEBUG: current ESSID=%s\n", USBDEVNAME(sc->atu_dev),
2047 	    tmp));
2048 }
2049 #endif /* ATU_DEBUG */
2050 int
2051 atu_set_wepkey(struct atu_softc *sc, int nr, u_int8_t *key, int len)
2052 {
2053 	if ((len != 5) && (len != 13))
2054 		return EINVAL;
2055 
2056 	DPRINTFN(10, ("%s: changed wepkey %d (len=%d)\n",
2057 	    USBDEVNAME(sc->atu_dev), nr, len));
2058 
2059 	memcpy(sc->atu_wepkeys[nr], key, len);
2060 	if (len == 13)
2061 		sc->atu_wepkeylen = ATU_WEP_104BITS;
2062 	else
2063 		sc->atu_wepkeylen = ATU_WEP_40BITS;
2064 
2065 	atu_send_mib(sc, MIB_MAC_WEP__ENCR_LEVEL, NR(sc->atu_wepkeylen));
2066 	return atu_send_mib(sc, MIB_MAC_WEP__KEYS(nr), key);
2067 }
2068 
2069 int
2070 atu_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2071 {
2072 	struct atu_softc	*sc = ifp->if_softc;
2073 	struct ifreq		*ifr = (struct ifreq *)data;
2074 	struct ieee80211com	*ic = &sc->sc_ic;
2075 	int			err = 0, s;
2076 
2077 	s = splnet();
2078 	switch (command) {
2079 	case SIOCSIFMEDIA:
2080 	case SIOCGIFMEDIA:
2081 		err = ifmedia_ioctl(ifp, ifr, &ic->ic_media, command);
2082 		break;
2083 
2084 	default:
2085 		DPRINTFN(15, ("%s: ieee80211_ioctl (%lu)\n",
2086 		    USBDEVNAME(sc->atu_dev), command));
2087 		err = ieee80211_ioctl(ifp, command, data);
2088 		break;
2089 	}
2090 
2091 	if (err == ENETRESET) {
2092 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
2093 		    (IFF_RUNNING|IFF_UP)) {
2094 			DPRINTF(("%s: atu_ioctl(): netreset\n",
2095 			    USBDEVNAME(sc->atu_dev)));
2096 			atu_init(ifp);
2097 		}
2098 		err = 0;
2099 	}
2100 
2101 	splx(s);
2102 	return (err);
2103 }
2104 
2105 void
2106 atu_watchdog(struct ifnet *ifp)
2107 {
2108 	struct atu_softc	*sc = ifp->if_softc;
2109 	struct atu_chain	*c;
2110 	usbd_status		stat;
2111 	int			cnt, s;
2112 
2113 	DPRINTF(("%s: atu_watchdog\n", USBDEVNAME(sc->atu_dev)));
2114 
2115 	ifp->if_timer = 0;
2116 
2117 	if (sc->sc_state != ATU_S_OK || (ifp->if_flags & IFF_RUNNING) == 0)
2118 		return;
2119 
2120 	sc = ifp->if_softc;
2121 	s = splnet();
2122 	ifp->if_oerrors++;
2123 	DPRINTF(("%s: watchdog timeout\n", USBDEVNAME(sc->atu_dev)));
2124 
2125 	/*
2126 	 * TODO:
2127 	 * we should change this since we have multiple TX tranfers...
2128 	 */
2129 	for (cnt = 0; cnt < ATU_TX_LIST_CNT; cnt++) {
2130 		c = &sc->atu_cdata.atu_tx_chain[cnt];
2131 		if (c->atu_in_xfer) {
2132 			usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL,
2133 			    &stat);
2134 			atu_txeof(c->atu_xfer, c, stat);
2135 		}
2136 	}
2137 
2138 	if (ifp->if_snd.ifq_head != NULL)
2139 		atu_start(ifp);
2140 	splx(s);
2141 
2142 	ieee80211_watchdog(ifp);
2143 }
2144 
2145 /*
2146  * Stop the adapter and free any mbufs allocated to the
2147  * RX and TX lists.
2148  */
2149 void
2150 atu_stop(struct ifnet *ifp, int disable)
2151 {
2152 	struct atu_softc	*sc = ifp->if_softc;
2153 	struct atu_cdata	*cd;
2154 	usbd_status		err;
2155 	int s;
2156 
2157 	s = splnet();
2158 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2159 	ifp->if_timer = 0;
2160 
2161 	/* Stop transfers. */
2162 	if (sc->atu_ep[ATU_ENDPT_RX] != NULL) {
2163 		err = usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_RX]);
2164 		if (err) {
2165 			DPRINTF(("%s: abort rx pipe failed: %s\n",
2166 			    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
2167 		}
2168 		err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_RX]);
2169 		if (err) {
2170 			DPRINTF(("%s: close rx pipe failed: %s\n",
2171 			    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
2172 		}
2173 		sc->atu_ep[ATU_ENDPT_RX] = NULL;
2174 	}
2175 
2176 	if (sc->atu_ep[ATU_ENDPT_TX] != NULL) {
2177 		err = usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_TX]);
2178 		if (err) {
2179 			DPRINTF(("%s: abort tx pipe failed: %s\n",
2180 			    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
2181 		}
2182 		err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_TX]);
2183 		if (err) {
2184 			DPRINTF(("%s: close tx pipe failed: %s\n",
2185 			    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
2186 		}
2187 		sc->atu_ep[ATU_ENDPT_TX] = NULL;
2188 	}
2189 
2190 	/* Free RX/TX/MGMT list resources. */
2191 	cd = &sc->atu_cdata;
2192 	atu_xfer_list_free(sc, cd->atu_rx_chain, ATU_RX_LIST_CNT);
2193 	atu_xfer_list_free(sc, cd->atu_tx_chain, ATU_TX_LIST_CNT);
2194 
2195 	/* Let's be nice and turn off the radio before we leave */
2196 	atu_switch_radio(sc, 0);
2197 
2198 	splx(s);
2199 }
2200