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