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