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