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