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