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