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