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