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