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