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