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