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