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