if_malo_pcmcia.c revision 1.7.4.2 1 /* $NetBSD: if_malo_pcmcia.c,v 1.7.4.2 2017/02/05 13:40:45 skrll Exp $ */
2 /* $OpenBSD: if_malo.c,v 1.65 2009/03/29 21:53:53 sthen Exp $ */
3
4 /*
5 * Copyright (c) 2007 Marcus Glocker <mglocker (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <sys/cdefs.h>
21 __KERNEL_RCSID(0, "$NetBSD: if_malo_pcmcia.c,v 1.7.4.2 2017/02/05 13:40:45 skrll Exp $");
22
23 #ifdef _MODULE
24 #include <sys/module.h>
25 #endif
26
27 #include <sys/param.h>
28 #include <sys/bus.h>
29 #include <sys/condvar.h>
30 #include <sys/device.h>
31 #include <sys/intr.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/mbuf.h>
35 #include <sys/mutex.h>
36 #include <sys/pmf.h>
37 #include <sys/proc.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
40 #include <sys/systm.h>
41
42 #include <net/bpf.h>
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_ether.h>
46 #include <net/if_media.h>
47 #include <net/if_llc.h>
48
49 #include <net80211/ieee80211_var.h>
50 #include <net80211/ieee80211_radiotap.h>
51
52 #include <dev/firmload.h>
53
54 #include <dev/pcmcia/pcmciareg.h>
55 #include <dev/pcmcia/pcmciavar.h>
56 #include <dev/pcmcia/pcmciadevs.h>
57
58 #include <dev/pcmcia/if_malo_pcmciavar.h>
59 #include <dev/pcmcia/if_malo_pcmciareg.h>
60
61 /*
62 * Driver for the Marvell 88W8385 chip (Compact Flash).
63 */
64
65 #ifdef CMALO_DEBUG
66 int cmalo_d = 1;
67 #define DPRINTF(l, x...) do { if ((l) <= cmalo_d) printf(x); } while (0)
68 #else
69 #define DPRINTF(l, x...) do {} while (0)
70 #endif
71
72 static int malo_pcmcia_match(device_t, cfdata_t, void *);
73 static void malo_pcmcia_attach(device_t, device_t, void *);
74 static int malo_pcmcia_detach(device_t, int);
75 static int malo_pcmcia_activate(device_t, devact_t);
76
77 static int malo_pcmcia_validate_config(struct pcmcia_config_entry *);
78
79 static int malo_pcmcia_enable(struct malo_softc *);
80 static void malo_pcmcia_disable(struct malo_softc *);
81
82 static void cmalo_attach(void *);
83 static void cmalo_detach(void *);
84 static int cmalo_intr(void *);
85 static void cmalo_softintr(void *);
86
87 static void cmalo_start(struct ifnet *);
88 static int cmalo_ioctl(struct ifnet *, u_long, void *);
89 static int cmalo_init(struct ifnet *);
90 static void cmalo_watchdog(struct ifnet *);
91 static int cmalo_media_change(struct ifnet *);
92 static int cmalo_newstate(struct ieee80211com *, enum ieee80211_state,
93 int);
94
95 static int firmware_load(const char *, const char *, uint8_t **, size_t *);
96 static int cmalo_fw_alloc(struct malo_softc *);
97 static void cmalo_fw_free(struct malo_softc *);
98 static int cmalo_fw_load_helper(struct malo_softc *);
99 static int cmalo_fw_load_main(struct malo_softc *);
100
101 static void cmalo_stop(struct malo_softc *);
102 static void cmalo_intr_mask(struct malo_softc *, int);
103 static void cmalo_rx(struct malo_softc *);
104 static int cmalo_tx(struct malo_softc *, struct mbuf *);
105 static void cmalo_tx_done(struct malo_softc *);
106 static void cmalo_event(struct malo_softc *);
107 static void cmalo_select_network(struct malo_softc *);
108 static void cmalo_reflect_network(struct malo_softc *);
109 static int cmalo_wep(struct malo_softc *);
110 static int cmalo_rate2bitmap(int);
111
112 static void cmalo_hexdump(void *, int);
113 static int cmalo_cmd_get_hwspec(struct malo_softc *);
114 static int cmalo_cmd_rsp_hwspec(struct malo_softc *);
115 static int cmalo_cmd_set_reset(struct malo_softc *);
116 static int cmalo_cmd_set_scan(struct malo_softc *);
117 static int cmalo_cmd_rsp_scan(struct malo_softc *);
118 static int cmalo_parse_elements(struct malo_softc *, uint8_t *, int, int);
119 static int cmalo_cmd_set_auth(struct malo_softc *);
120 static int cmalo_cmd_set_wep(struct malo_softc *, uint16_t,
121 struct ieee80211_key *);
122 static int cmalo_cmd_set_snmp(struct malo_softc *, uint16_t);
123 static int cmalo_cmd_set_radio(struct malo_softc *, uint16_t);
124 static int cmalo_cmd_set_channel(struct malo_softc *, uint16_t);
125 static int cmalo_cmd_set_txpower(struct malo_softc *, int16_t);
126 static int cmalo_cmd_set_antenna(struct malo_softc *, uint16_t);
127 static int cmalo_cmd_set_macctrl(struct malo_softc *);
128 static int cmalo_cmd_set_macaddr(struct malo_softc *, uint8_t *);
129 static int cmalo_cmd_set_assoc(struct malo_softc *);
130 static int cmalo_cmd_rsp_assoc(struct malo_softc *);
131 static int cmalo_cmd_set_rate(struct malo_softc *, int);
132 static int cmalo_cmd_request(struct malo_softc *, uint16_t, int);
133 static int cmalo_cmd_response(struct malo_softc *);
134
135 /*
136 * PCMCIA bus.
137 */
138 struct malo_pcmcia_softc {
139 struct malo_softc sc_malo;
140
141 struct pcmcia_function *sc_pf;
142 struct pcmcia_io_handle sc_pcioh;
143 int sc_io_window;
144 void *sc_ih;
145 };
146
147 CFATTACH_DECL_NEW(malo_pcmcia, sizeof(struct malo_pcmcia_softc),
148 malo_pcmcia_match, malo_pcmcia_attach, malo_pcmcia_detach,
149 malo_pcmcia_activate);
150
151
152 static int
153 malo_pcmcia_match(device_t parent, cfdata_t match, void *aux)
154 {
155 struct pcmcia_attach_args *pa = aux;
156
157 if (pa->manufacturer == PCMCIA_VENDOR_AMBICOM &&
158 pa->product == PCMCIA_PRODUCT_AMBICOM_WL54CF)
159 return 1;
160
161 return 0;
162 }
163
164 static void
165 malo_pcmcia_attach(device_t parent, device_t self, void *aux)
166 {
167 struct malo_pcmcia_softc *psc = device_private(self);
168 struct malo_softc *sc = &psc->sc_malo;
169 struct pcmcia_attach_args *pa = aux;
170 struct pcmcia_config_entry *cfe;
171 int error;
172
173 sc->sc_dev = self;
174 psc->sc_pf = pa->pf;
175
176 error = pcmcia_function_configure(pa->pf, malo_pcmcia_validate_config);
177 if (error) {
178 aprint_error_dev(self, "configure failed, error=%d\n", error);
179 return;
180 }
181
182 sc->sc_soft_ih = softint_establish(SOFTINT_NET, cmalo_softintr, sc);
183 if (sc->sc_soft_ih == NULL) {
184 aprint_error_dev(self, "couldn't establish softint\n");
185 return;
186 }
187
188 malo_pcmcia_enable(sc);
189
190 cfe = pa->pf->cfe;
191 sc->sc_iot = cfe->iospace[0].handle.iot;
192 sc->sc_ioh = cfe->iospace[0].handle.ioh;
193
194 cmalo_attach(sc);
195 if (!(sc->sc_flags & MALO_DEVICE_ATTACHED))
196 goto fail;
197
198 if (pmf_device_register(self, NULL, NULL))
199 pmf_class_network_register(self, &sc->sc_if);
200 else
201 aprint_error_dev(self, "couldn't establish power handler\n");
202
203 fail:
204 malo_pcmcia_disable(sc);
205
206 if (sc->sc_flags & MALO_DEVICE_ATTACHED)
207 return;
208
209 softint_disestablish(sc->sc_soft_ih);
210 sc->sc_soft_ih = NULL;
211
212 pcmcia_function_unconfigure(pa->pf);
213 return;
214 }
215
216 static int
217 malo_pcmcia_detach(device_t dev, int flags)
218 {
219 struct malo_pcmcia_softc *psc = device_private(dev);
220 struct malo_softc *sc = &psc->sc_malo;
221
222 cmalo_detach(sc);
223 malo_pcmcia_disable(sc);
224 softint_disestablish(sc->sc_soft_ih);
225 sc->sc_soft_ih = NULL;
226 pcmcia_function_unconfigure(psc->sc_pf);
227
228 return 0;
229 }
230
231 static int
232 malo_pcmcia_activate(device_t dev, devact_t act)
233 {
234 struct malo_pcmcia_softc *psc = device_private(dev);
235 struct malo_softc *sc = &psc->sc_malo;
236 struct ifnet *ifp = &sc->sc_if;
237 int s;
238
239 s = splnet();
240 switch (act) {
241 case DVACT_DEACTIVATE:
242 if_deactivate(ifp);
243 break;
244 default:
245 return EOPNOTSUPP;
246 }
247 splx(s);
248
249 return 0;
250 }
251
252
253 int
254 malo_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
255 {
256
257 if (cfe->iftype != PCMCIA_IFTYPE_IO || cfe->num_iospace != 1)
258 return EINVAL;
259 /* Some cards have a memory space, but we don't use it. */
260 cfe->num_memspace = 0;
261 return 0;
262 }
263
264
265 static int
266 malo_pcmcia_enable(struct malo_softc *sc)
267 {
268 struct malo_pcmcia_softc *psc = (struct malo_pcmcia_softc *)sc;
269
270 /* establish interrupt */
271 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, cmalo_intr, sc);
272 if (psc->sc_ih == NULL) {
273 aprint_error(": can't establish interrupt\n");
274 return -1;
275 }
276
277 if (pcmcia_function_enable(psc->sc_pf)) {
278 aprint_error(": can't enable function\n");
279 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
280 return -1;
281 }
282 sc->sc_flags |= MALO_DEVICE_ENABLED;
283
284 return 0;
285 }
286
287 static void
288 malo_pcmcia_disable(struct malo_softc *sc)
289 {
290 struct malo_pcmcia_softc *psc = (struct malo_pcmcia_softc *)sc;
291
292 pcmcia_function_disable(psc->sc_pf);
293 if (psc->sc_ih)
294 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
295 psc->sc_ih = NULL;
296 sc->sc_flags &= ~MALO_DEVICE_ENABLED;
297 }
298
299
300 /*
301 * Driver.
302 */
303 static void
304 cmalo_attach(void *arg)
305 {
306 struct malo_softc *sc = arg;
307 struct ieee80211com *ic = &sc->sc_ic;
308 struct ifnet *ifp = &sc->sc_if;
309 int i;
310
311 /* disable interrupts */
312 cmalo_intr_mask(sc, 0);
313
314 /* load firmware */
315 if (cmalo_fw_alloc(sc) != 0 ||
316 cmalo_fw_load_helper(sc) != 0 ||
317 cmalo_fw_load_main(sc) != 0) {
318 /* free firmware */
319 cmalo_fw_free(sc);
320 return;
321 }
322 sc->sc_flags |= MALO_FW_LOADED;
323
324 /* allocate command buffer */
325 sc->sc_cmd = malloc(MALO_CMD_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
326
327 /* allocate data buffer */
328 sc->sc_data = malloc(MALO_DATA_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
329
330 /* enable interrupts */
331 cmalo_intr_mask(sc, 1);
332
333 /* we are context save here for FW commands */
334 sc->sc_cmd_ctxsave = 1;
335
336 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_VM);
337 cv_init(&sc->sc_cv, "malo");
338
339 /* get hardware specs */
340 cmalo_cmd_get_hwspec(sc);
341
342 /* setup interface */
343 ifp->if_softc = sc;
344 ifp->if_start = cmalo_start;
345 ifp->if_ioctl = cmalo_ioctl;
346 ifp->if_init = cmalo_init;
347 ifp->if_watchdog = cmalo_watchdog;
348 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
349 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
350 IFQ_SET_READY(&ifp->if_snd);
351
352 ic->ic_ifp = ifp;
353 ic->ic_phytype = IEEE80211_T_OFDM;
354 ic->ic_opmode = IEEE80211_M_STA;
355 ic->ic_state = IEEE80211_S_INIT;
356 ic->ic_caps = IEEE80211_C_MONITOR | IEEE80211_C_WEP;
357
358 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
359 ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
360
361 for (i = 0; i <= 14; i++) {
362 ic->ic_channels[i].ic_freq =
363 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
364 ic->ic_channels[i].ic_flags =
365 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
366 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
367 }
368
369 /* attach interface */
370 if_initialize(ifp);
371 ieee80211_ifattach(ic);
372 /* Use common softint-based if_input */
373 ifp->if_percpuq = if_percpuq_create(ifp);
374 if_register(ifp);
375
376 sc->sc_newstate = ic->ic_newstate;
377 ic->ic_newstate = cmalo_newstate;
378 ieee80211_media_init(ic, cmalo_media_change, ieee80211_media_status);
379
380 /* second attach line */
381 aprint_normal_dev(sc->sc_dev, "address %s\n",
382 ether_sprintf(ic->ic_myaddr));
383
384 ieee80211_announce(ic);
385
386 /* device attached */
387 sc->sc_flags |= MALO_DEVICE_ATTACHED;
388 }
389
390 static void
391 cmalo_detach(void *arg)
392 {
393 struct malo_softc *sc = arg;
394 struct ieee80211com *ic = &sc->sc_ic;
395 struct ifnet *ifp = &sc->sc_if;
396
397 if (!(sc->sc_flags & MALO_DEVICE_ATTACHED)) {
398 /* free firmware */
399 cmalo_fw_free(sc);
400
401 /* device was not properly attached */
402 return;
403 }
404
405 if (ifp->if_flags & IFF_RUNNING)
406 cmalo_stop(sc);
407
408 /* free command buffer */
409 if (sc->sc_cmd != NULL)
410 free(sc->sc_cmd, M_DEVBUF);
411
412 /* free data buffer */
413 if (sc->sc_data != NULL)
414 free(sc->sc_data, M_DEVBUF);
415
416 /* free firmware */
417 cmalo_fw_free(sc);
418
419 /* detach inferface */
420 ieee80211_ifdetach(ic);
421 if_detach(ifp);
422
423 mutex_destroy(&sc->sc_mtx);
424 cv_destroy(&sc->sc_cv);
425 }
426
427 static int
428 cmalo_intr(void *arg)
429 {
430 struct malo_softc *sc = arg;
431 uint16_t intr;
432
433 /* read interrupt reason */
434 intr = MALO_READ_2(sc, MALO_REG_HOST_INTR_CAUSE);
435 if (intr == 0)
436 /* interrupt not for us */
437 return 0;
438 if (intr == 0xffff)
439 /* card has been detached */
440 return 0;
441
442 /* disable interrupts */
443 cmalo_intr_mask(sc, 0);
444
445 DPRINTF(2, "%s: interrupt handler called (intr = 0x%04x)\n",
446 device_xname(sc->sc_dev), intr);
447
448 softint_schedule(sc->sc_soft_ih);
449 return 1;
450 }
451
452 static void
453 cmalo_softintr(void *arg)
454 {
455 struct malo_softc *sc = arg;
456 uint16_t intr;
457
458 /* read interrupt reason */
459 intr = MALO_READ_2(sc, MALO_REG_HOST_INTR_CAUSE);
460 if (intr == 0 || intr == 0xffff)
461 goto out;
462
463 /* acknowledge interrupt */
464 MALO_WRITE_2(sc, MALO_REG_HOST_INTR_CAUSE,
465 intr & MALO_VAL_HOST_INTR_MASK_ON);
466
467 if (intr & MALO_VAL_HOST_INTR_TX)
468 /* TX frame sent */
469 cmalo_tx_done(sc);
470 if (intr & MALO_VAL_HOST_INTR_RX)
471 /* RX frame received */
472 cmalo_rx(sc);
473 if (intr & MALO_VAL_HOST_INTR_CMD) {
474 /* command response */
475 mutex_enter(&sc->sc_mtx);
476 cv_signal(&sc->sc_cv);
477 mutex_exit(&sc->sc_mtx);
478 if (!sc->sc_cmd_ctxsave)
479 cmalo_cmd_response(sc);
480 }
481 if (intr & MALO_VAL_HOST_INTR_EVENT)
482 /* event */
483 cmalo_event(sc);
484
485 out:
486 /* enable interrupts */
487 cmalo_intr_mask(sc, 1);
488 }
489
490
491 /*
492 * Network functions
493 */
494 static void
495 cmalo_start(struct ifnet *ifp)
496 {
497 struct malo_softc *sc = ifp->if_softc;
498 struct mbuf *m;
499
500 /* don't transmit packets if interface is busy or down */
501 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
502 return;
503
504 IFQ_POLL(&ifp->if_snd, m);
505 if (m == NULL)
506 return;
507
508 IFQ_DEQUEUE(&ifp->if_snd, m);
509
510 if (ifp->if_bpf)
511 bpf_ops->bpf_mtap(ifp->if_bpf, m);
512
513 if (cmalo_tx(sc, m) != 0)
514 ifp->if_oerrors++;
515 }
516
517 static int
518 cmalo_ioctl(struct ifnet *ifp, u_long cmd, void *data)
519 {
520 struct malo_softc *sc = ifp->if_softc;
521 struct ieee80211com *ic = &sc->sc_ic;
522 int s, error = 0;
523
524 s = splnet();
525
526 switch (cmd) {
527 case SIOCSIFFLAGS:
528 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
529 break;
530 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
531 case IFF_RUNNING:
532 cmalo_stop(sc);
533 break;
534
535 case IFF_UP:
536 cmalo_init(ifp);
537 break;
538
539 default:
540 break;
541 }
542 error = 0;
543 break;
544
545 case SIOCADDMULTI:
546 case SIOCDELMULTI:
547 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET)
548 /* setup multicast filter, etc */
549 error = 0;
550 break;
551
552 default:
553 error = ieee80211_ioctl(ic, cmd, data);
554 break;
555 }
556
557 if (error == ENETRESET) {
558 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
559 (IFF_UP | IFF_RUNNING))
560 cmalo_init(ifp);
561 error = 0;
562 }
563
564 splx(s);
565
566 return error;
567 }
568
569 static int
570 cmalo_init(struct ifnet *ifp)
571 {
572 struct malo_softc *sc = ifp->if_softc;
573 struct ieee80211com *ic = &sc->sc_ic;
574
575 if (!(sc->sc_flags & MALO_DEVICE_ENABLED))
576 malo_pcmcia_enable(sc);
577
578 /* reload the firmware if necessary */
579 if (!(sc->sc_flags & MALO_FW_LOADED)) {
580 /* disable interrupts */
581 cmalo_intr_mask(sc, 0);
582
583 /* load firmware */
584 if (cmalo_fw_load_helper(sc) != 0)
585 return EIO;
586 if (cmalo_fw_load_main(sc) != 0)
587 return EIO;
588 sc->sc_flags |= MALO_FW_LOADED;
589
590 /* enable interrupts */
591 cmalo_intr_mask(sc, 1);
592 }
593
594 if (ifp->if_flags & IFF_RUNNING)
595 cmalo_stop(sc);
596
597 /* reset association state flag */
598 sc->sc_flags &= ~MALO_ASSOC_FAILED;
599
600 /* get current channel */
601 ic->ic_curchan = ic->ic_ibss_chan;
602 sc->sc_curchan = ieee80211_chan2ieee(ic, ic->ic_curchan);
603 DPRINTF(1, "%s: current channel is %d\n",
604 device_xname(sc->sc_dev), sc->sc_curchan);
605
606 /* setup device */
607 if (cmalo_cmd_set_macctrl(sc) != 0)
608 return EIO;
609 if (cmalo_cmd_set_txpower(sc, 15) != 0)
610 return EIO;
611 if (cmalo_cmd_set_antenna(sc, 1) != 0)
612 return EIO;
613 if (cmalo_cmd_set_antenna(sc, 2) != 0)
614 return EIO;
615 if (cmalo_cmd_set_radio(sc, 1) != 0)
616 return EIO;
617 if (cmalo_cmd_set_channel(sc, sc->sc_curchan) != 0)
618 return EIO;
619 if (cmalo_cmd_set_rate(sc, ic->ic_fixed_rate) != 0)
620 return EIO;
621 if (cmalo_cmd_set_snmp(sc, MALO_OID_RTSTRESH) != 0)
622 return EIO;
623 if (cmalo_cmd_set_snmp(sc, MALO_OID_SHORTRETRY) != 0)
624 return EIO;
625 if (cmalo_cmd_set_snmp(sc, MALO_OID_FRAGTRESH) != 0)
626 return EIO;
627 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
628 if (cmalo_cmd_set_macaddr(sc, ic->ic_myaddr) != 0)
629 return EIO;
630 if (ic->ic_flags & IEEE80211_F_PRIVACY)
631 if (cmalo_wep(sc) != 0)
632 return EIO;
633
634 /* device up */
635 ifp->if_flags |= IFF_RUNNING;
636 ifp->if_flags &= ~IFF_OACTIVE;
637
638 /* start network */
639 if (ic->ic_opmode != IEEE80211_M_MONITOR)
640 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
641 if (sc->sc_flags & MALO_ASSOC_FAILED)
642 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
643 else
644 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
645
646 /* we are not context save anymore for FW commands */
647 sc->sc_cmd_ctxsave = 0;
648
649 return 0;
650 }
651
652 static void
653 cmalo_watchdog(struct ifnet *ifp)
654 {
655 DPRINTF(2, "watchdog timeout\n");
656
657 /* accept TX packets again */
658 ifp->if_flags &= ~IFF_OACTIVE;
659 }
660
661 static int
662 cmalo_media_change(struct ifnet *ifp)
663 {
664 int error;
665
666 if ((error = ieee80211_media_change(ifp)) != ENETRESET)
667 return error;
668
669 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
670 cmalo_init(ifp);
671
672 return 0;
673 }
674
675 static int
676 cmalo_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
677 {
678 struct malo_softc *sc = ic->ic_ifp->if_softc;
679 enum ieee80211_state ostate;
680
681 ostate = ic->ic_state;
682
683 if (ostate == nstate)
684 goto out;
685
686 switch (nstate) {
687 case IEEE80211_S_INIT:
688 DPRINTF(1, "%s: newstate is IEEE80211_S_INIT\n",
689 device_xname(sc->sc_dev));
690 break;
691 case IEEE80211_S_SCAN:
692 DPRINTF(1, "%s: newstate is IEEE80211_S_SCAN\n",
693 device_xname(sc->sc_dev));
694 cmalo_cmd_set_scan(sc);
695 if (!sc->sc_net_num) {
696 /* no networks found */
697 DPRINTF(1, "%s: no networks found\n",
698 device_xname(sc->sc_dev));
699 break;
700 }
701 cmalo_select_network(sc);
702 cmalo_cmd_set_auth(sc);
703 cmalo_cmd_set_assoc(sc);
704 break;
705 case IEEE80211_S_AUTH:
706 DPRINTF(1, "%s: newstate is IEEE80211_S_AUTH\n",
707 device_xname(sc->sc_dev));
708 break;
709 case IEEE80211_S_ASSOC:
710 DPRINTF(1, "%s: newstate is IEEE80211_S_ASSOC\n",
711 device_xname(sc->sc_dev));
712 break;
713 case IEEE80211_S_RUN:
714 DPRINTF(1, "%s: newstate is IEEE80211_S_RUN\n",
715 device_xname(sc->sc_dev));
716 cmalo_reflect_network(sc);
717 break;
718 default:
719 break;
720 }
721
722 out:
723 return sc->sc_newstate(ic, nstate, arg);
724 }
725
726
727 static int
728 firmware_load(const char *dname, const char *iname, uint8_t **ucodep,
729 size_t *sizep)
730 {
731 firmware_handle_t fh;
732 int error;
733
734 if ((error = firmware_open(dname, iname, &fh)) != 0)
735 return error;
736 *sizep = firmware_get_size(fh);
737 if ((*ucodep = firmware_malloc(*sizep)) == NULL) {
738 firmware_close(fh);
739 return ENOMEM;
740 }
741 if ((error = firmware_read(fh, 0, *ucodep, *sizep)) != 0)
742 firmware_free(*ucodep, *sizep);
743 firmware_close(fh);
744
745 return error;
746 }
747
748 static int
749 cmalo_fw_alloc(struct malo_softc *sc)
750 {
751 const char *name_h = "malo8385-h";
752 const char *name_m = "malo8385-m";
753 int error;
754
755 if (sc->sc_fw_h == NULL) {
756 /* read helper firmware image */
757 error = firmware_load("malo", name_h, &sc->sc_fw_h,
758 &sc->sc_fw_h_size);
759 if (error != 0) {
760 aprint_error_dev(sc->sc_dev,
761 "error %d, could not read firmware %s\n",
762 error, name_h);
763 return EIO;
764 }
765 }
766
767 if (sc->sc_fw_m == NULL) {
768 /* read main firmware image */
769 error = firmware_load("malo", name_m, &sc->sc_fw_m,
770 &sc->sc_fw_m_size);
771 if (error != 0) {
772 aprint_error_dev(sc->sc_dev,
773 "error %d, could not read firmware %s\n",
774 error, name_m);
775 return EIO;
776 }
777 }
778
779 return 0;
780 }
781
782 static void
783 cmalo_fw_free(struct malo_softc *sc)
784 {
785
786 if (sc->sc_fw_h != NULL) {
787 firmware_free(sc->sc_fw_h, sc->sc_fw_h_size);
788 sc->sc_fw_h = NULL;
789 }
790
791 if (sc->sc_fw_m != NULL) {
792 firmware_free(sc->sc_fw_m, sc->sc_fw_m_size);
793 sc->sc_fw_m = NULL;
794 }
795 }
796
797 static int
798 cmalo_fw_load_helper(struct malo_softc *sc)
799 {
800 uint8_t val8;
801 uint16_t bsize, *uc;
802 int offset, i;
803
804 /* verify if the card is ready for firmware download */
805 val8 = MALO_READ_1(sc, MALO_REG_SCRATCH);
806 if (val8 == MALO_VAL_SCRATCH_FW_LOADED)
807 /* firmware already loaded */
808 return 0;
809 if (val8 != MALO_VAL_SCRATCH_READY) {
810 /* bad register value */
811 aprint_error_dev(sc->sc_dev,
812 "device not ready for FW download\n");
813 return EIO;
814 }
815
816 /* download the helper firmware */
817 for (offset = 0; offset < sc->sc_fw_h_size; offset += bsize) {
818 if (sc->sc_fw_h_size - offset >= MALO_FW_HELPER_BSIZE)
819 bsize = MALO_FW_HELPER_BSIZE;
820 else
821 bsize = sc->sc_fw_h_size - offset;
822
823 /* send a block in words and confirm it */
824 DPRINTF(3, "%s: download helper FW block (%d bytes, %d off)\n",
825 device_xname(sc->sc_dev), bsize, offset);
826 MALO_WRITE_2(sc, MALO_REG_CMD_WRITE_LEN, bsize);
827 uc = (uint16_t *)(sc->sc_fw_h + offset);
828 for (i = 0; i < bsize / 2; i++)
829 MALO_WRITE_2(sc, MALO_REG_CMD_WRITE, htole16(uc[i]));
830 MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_CMD_DL_OVER);
831 MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE,
832 MALO_VAL_CMD_DL_OVER);
833
834 /* poll for an acknowledgement */
835 for (i = 0; i < 50; i++) {
836 if (MALO_READ_1(sc, MALO_REG_CARD_STATUS) ==
837 MALO_VAL_CMD_DL_OVER)
838 break;
839 delay(1000);
840 }
841 if (i == 50) {
842 aprint_error_dev(sc->sc_dev,
843 "timeout while helper FW block download\n");
844 return EIO;
845 }
846 }
847
848 /* helper firmware download done */
849 MALO_WRITE_2(sc, MALO_REG_CMD_WRITE_LEN, 0);
850 MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_CMD_DL_OVER);
851 MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_CMD_DL_OVER);
852 DPRINTF(1, "%s: helper FW downloaded\n", device_xname(sc->sc_dev));
853
854 return 0;
855 }
856
857 static int
858 cmalo_fw_load_main(struct malo_softc *sc)
859 {
860 uint16_t val16, bsize = 0, *uc;
861 int offset, i, retry = 0;
862
863 /* verify if the helper firmware has been loaded correctly */
864 for (i = 0; i < 10; i++) {
865 if (MALO_READ_1(sc, MALO_REG_RBAL) == MALO_FW_HELPER_LOADED)
866 break;
867 delay(1000);
868 }
869 if (i == 10) {
870 aprint_error_dev(sc->sc_dev, "helper FW not loaded\n");
871 return EIO;
872 }
873 DPRINTF(1, "%s: helper FW loaded successfully\n",
874 device_xname(sc->sc_dev));
875
876 /* download the main firmware */
877 for (offset = 0; offset < sc->sc_fw_m_size; offset += bsize) {
878 val16 = MALO_READ_2(sc, MALO_REG_RBAL);
879 /*
880 * If the helper firmware serves us an odd integer then
881 * something went wrong and we retry to download the last
882 * block until we receive a good integer again, or give up.
883 */
884 if (val16 & 0x0001) {
885 if (retry > MALO_FW_MAIN_MAXRETRY) {
886 aprint_error_dev(sc->sc_dev,
887 "main FW download failed\n");
888 return EIO;
889 }
890 retry++;
891 offset -= bsize;
892 } else {
893 retry = 0;
894 bsize = val16;
895 }
896
897 /* send a block in words and confirm it */
898 DPRINTF(3, "%s: download main FW block (%d bytes, %d off)\n",
899 device_xname(sc->sc_dev), bsize, offset);
900 MALO_WRITE_2(sc, MALO_REG_CMD_WRITE_LEN, bsize);
901 uc = (uint16_t *)(sc->sc_fw_m + offset);
902 for (i = 0; i < bsize / 2; i++)
903 MALO_WRITE_2(sc, MALO_REG_CMD_WRITE, htole16(uc[i]));
904 MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_CMD_DL_OVER);
905 MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE,
906 MALO_VAL_CMD_DL_OVER);
907
908 /* poll for an acknowledgement */
909 for (i = 0; i < 5000; i++) {
910 if (MALO_READ_1(sc, MALO_REG_CARD_STATUS) ==
911 MALO_VAL_CMD_DL_OVER)
912 break;
913 }
914 if (i == 5000) {
915 aprint_error_dev(sc->sc_dev,
916 "timeout while main FW block download\n");
917 return EIO;
918 }
919 }
920
921 DPRINTF(1, "%s: main FW downloaded\n", device_xname(sc->sc_dev));
922
923 /* verify if the main firmware has been loaded correctly */
924 for (i = 0; i < 500; i++) {
925 if (MALO_READ_1(sc, MALO_REG_SCRATCH) ==
926 MALO_VAL_SCRATCH_FW_LOADED)
927 break;
928 delay(1000);
929 }
930 if (i == 500) {
931 aprint_error_dev(sc->sc_dev, "main FW not loaded\n");
932 return EIO;
933 }
934
935 DPRINTF(1, "%s: main FW loaded successfully\n",
936 device_xname(sc->sc_dev));
937
938 return 0;
939 }
940
941 static void
942 cmalo_stop(struct malo_softc *sc)
943 {
944 struct ieee80211com *ic = &sc->sc_ic;
945 struct ifnet *ifp = &sc->sc_if;
946
947 /* device down */
948 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
949
950 /* change device back to initial state */
951 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
952
953 /* reset device */
954 cmalo_cmd_set_reset(sc);
955 sc->sc_flags &= ~MALO_FW_LOADED;
956
957 if (sc->sc_flags & MALO_DEVICE_ENABLED)
958 malo_pcmcia_disable(sc);
959
960 DPRINTF(1, "%s: device down\n", device_xname(sc->sc_dev));
961 }
962
963 static void
964 cmalo_intr_mask(struct malo_softc *sc, int enable)
965 {
966 uint16_t val16;
967
968 val16 = MALO_READ_2(sc, MALO_REG_HOST_INTR_MASK);
969
970 DPRINTF(3, "%s: intr mask changed from 0x%04x ",
971 device_xname(sc->sc_dev), val16);
972
973 if (enable)
974 MALO_WRITE_2(sc, MALO_REG_HOST_INTR_MASK,
975 val16 & ~MALO_VAL_HOST_INTR_MASK_ON);
976 else
977 MALO_WRITE_2(sc, MALO_REG_HOST_INTR_MASK,
978 val16 | MALO_VAL_HOST_INTR_MASK_ON);
979
980 val16 = MALO_READ_2(sc, MALO_REG_HOST_INTR_MASK);
981
982 DPRINTF(3, "to 0x%04x\n", val16);
983 }
984
985 static void
986 cmalo_rx(struct malo_softc *sc)
987 {
988 struct ieee80211com *ic = &sc->sc_ic;
989 struct ifnet *ifp = &sc->sc_if;
990 struct malo_rx_desc *rxdesc;
991 struct mbuf *m;
992 uint8_t *data;
993 uint16_t psize;
994 int i;
995
996 /* read the whole RX packet which is always 802.3 */
997 psize = MALO_READ_2(sc, MALO_REG_DATA_READ_LEN);
998 if (psize > MALO_DATA_BUFFER_SIZE) {
999 aprint_error_dev(sc->sc_dev,
1000 "received data too large: %dbyte\n", psize);
1001 return;
1002 }
1003
1004 MALO_READ_MULTI_2(sc, MALO_REG_DATA_READ,
1005 (uint16_t *)sc->sc_data, psize / sizeof(uint16_t));
1006 if (psize & 0x0001)
1007 sc->sc_data[psize - 1] = MALO_READ_1(sc, MALO_REG_DATA_READ);
1008 MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_RX_DL_OVER);
1009 MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_RX_DL_OVER);
1010
1011 /* access RX packet descriptor */
1012 rxdesc = (struct malo_rx_desc *)sc->sc_data;
1013 rxdesc->status = le16toh(rxdesc->status);
1014 rxdesc->pkglen = le16toh(rxdesc->pkglen);
1015 rxdesc->pkgoffset = le32toh(rxdesc->pkgoffset);
1016
1017 DPRINTF(2, "RX status=%d, pkglen=%d, pkgoffset=%d\n",
1018 rxdesc->status, rxdesc->pkglen, rxdesc->pkgoffset);
1019
1020 if (rxdesc->status != MALO_RX_STATUS_OK)
1021 /* RX packet is not OK */
1022 return;
1023
1024 /* remove the LLC / SNAP header */
1025 data = sc->sc_data + rxdesc->pkgoffset;
1026 i = (ETHER_ADDR_LEN * 2) + sizeof(struct llc);
1027 memcpy(data + (ETHER_ADDR_LEN * 2), data + i, rxdesc->pkglen - i);
1028 rxdesc->pkglen -= sizeof(struct llc);
1029
1030 #define ETHER_ALIGN 2 /* XXX */
1031 /* prepare mbuf */
1032 m = m_devget(sc->sc_data + rxdesc->pkgoffset,
1033 rxdesc->pkglen, ETHER_ALIGN, ifp, NULL);
1034 if (m == NULL) {
1035 DPRINTF(1, "RX m_devget failed\n");
1036 ifp->if_ierrors++;
1037 return;
1038 }
1039
1040 /* push the frame up to the network stack if not in monitor mode */
1041 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1042 if_percpuq_enqueue(ifp->if_percpuq, m);
1043 }
1044
1045 static int
1046 cmalo_tx(struct malo_softc *sc, struct mbuf *m)
1047 {
1048 struct ifnet *ifp = &sc->sc_if;
1049 struct malo_tx_desc *txdesc = (struct malo_tx_desc *)sc->sc_data;
1050 uint8_t *data;
1051 uint16_t psize;
1052
1053 memset(sc->sc_data, 0, sizeof(*txdesc));
1054 psize = sizeof(*txdesc) + m->m_pkthdr.len;
1055 data = mtod(m, uint8_t *);
1056
1057 /* prepare TX descriptor */
1058 txdesc->pkgoffset = htole32(sizeof(*txdesc));
1059 txdesc->pkglen = htole16(m->m_pkthdr.len);
1060 memcpy(txdesc->dstaddr, data, ETHER_ADDR_LEN);
1061
1062 /* copy mbuf data to the buffer */
1063 m_copydata(m, 0, m->m_pkthdr.len, sc->sc_data + sizeof(*txdesc));
1064 m_freem(m);
1065
1066 /* send TX packet to the device */
1067 MALO_WRITE_2(sc, MALO_REG_DATA_WRITE_LEN, psize);
1068 MALO_WRITE_MULTI_2(sc, MALO_REG_DATA_WRITE,
1069 (uint16_t *)sc->sc_data, psize / sizeof(uint16_t));
1070 if (psize & 0x0001) {
1071 data = sc->sc_data;
1072 MALO_WRITE_1(sc, MALO_REG_DATA_WRITE, data[psize - 1]);
1073 }
1074 MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_TX_DL_OVER);
1075 MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_TX_DL_OVER);
1076
1077 ifp->if_flags |= IFF_OACTIVE;
1078 ifp->if_timer = 5;
1079
1080 DPRINTF(2, "%s: TX status=%d, pkglen=%d, pkgoffset=%zd\n",
1081 device_xname(sc->sc_dev), txdesc->status, le16toh(txdesc->pkglen),
1082 sizeof(*txdesc));
1083
1084 return 0;
1085 }
1086
1087 static void
1088 cmalo_tx_done(struct malo_softc *sc)
1089 {
1090 struct ifnet *ifp = &sc->sc_if;
1091 int s;
1092
1093 DPRINTF(2, "%s: TX done\n", device_xname(sc->sc_dev));
1094
1095 s = splnet();
1096 ifp->if_opackets++;
1097 ifp->if_flags &= ~IFF_OACTIVE;
1098 ifp->if_timer = 0;
1099 cmalo_start(ifp);
1100 splx(s);
1101 }
1102
1103 static void
1104 cmalo_event(struct malo_softc *sc)
1105 {
1106 uint16_t event;
1107
1108 /* read event reason */
1109 event = MALO_READ_2(sc, MALO_REG_CARD_STATUS);
1110 event &= MALO_VAL_CARD_STATUS_MASK;
1111 event = event >> 8;
1112
1113 switch (event) {
1114 case MALO_EVENT_DEAUTH:
1115 DPRINTF(1, "%s: got deauthentication event (0x%04x)\n",
1116 device_xname(sc->sc_dev), event);
1117 /* try to associate again */
1118 cmalo_cmd_set_assoc(sc);
1119 break;
1120 case MALO_EVENT_DISASSOC:
1121 DPRINTF(1, "%s: got disassociation event (0x%04x)\n",
1122 device_xname(sc->sc_dev), event);
1123 /* try to associate again */
1124 cmalo_cmd_set_assoc(sc);
1125 break;
1126 default:
1127 DPRINTF(1, "%s: got unknown event (0x%04x)\n",
1128 device_xname(sc->sc_dev), event);
1129 break;
1130 }
1131
1132 /* acknowledge event */
1133 MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_HOST_INTR_EVENT);
1134 }
1135
1136 static void
1137 cmalo_select_network(struct malo_softc *sc)
1138 {
1139 struct ieee80211com *ic = &sc->sc_ic;
1140 int i, best_rssi;
1141
1142 /* reset last selected network */
1143 sc->sc_net_cur = 0;
1144
1145 /* get desired network */
1146 if (ic->ic_des_esslen) {
1147 for (i = 0; i < sc->sc_net_num; i++) {
1148 if (!strcmp(ic->ic_des_essid, sc->sc_net[i].ssid)) {
1149 sc->sc_net_cur = i;
1150 DPRINTF(1, "%s: desired network found (%s)\n",
1151 device_xname(sc->sc_dev),
1152 ic->ic_des_essid);
1153 return;
1154 }
1155 }
1156 DPRINTF(1, "%s: desired network not found in scan results "
1157 "(%s)\n",
1158 device_xname(sc->sc_dev), ic->ic_des_essid);
1159 }
1160
1161 /* get network with best signal strength */
1162 best_rssi = sc->sc_net[0].rssi;
1163 for (i = 0; i < sc->sc_net_num; i++) {
1164 if (best_rssi < sc->sc_net[i].rssi) {
1165 best_rssi = sc->sc_net[i].rssi;
1166 sc->sc_net_cur = i;
1167 }
1168 }
1169 DPRINTF(1, "%s: best network found (%s)\n",
1170 device_xname(sc->sc_dev), sc->sc_net[sc->sc_net_cur].ssid);
1171 }
1172
1173 static void
1174 cmalo_reflect_network(struct malo_softc *sc)
1175 {
1176 struct ieee80211com *ic = &sc->sc_ic;
1177 uint8_t chan;
1178
1179 /* reflect active network to our 80211 stack */
1180
1181 /* BSSID */
1182 IEEE80211_ADDR_COPY(ic->ic_bss->ni_bssid,
1183 sc->sc_net[sc->sc_net_cur].bssid);
1184
1185 /* SSID */
1186 ic->ic_bss->ni_esslen = strlen(sc->sc_net[sc->sc_net_cur].ssid);
1187 memcpy(ic->ic_bss->ni_essid, sc->sc_net[sc->sc_net_cur].ssid,
1188 ic->ic_bss->ni_esslen);
1189
1190 /* channel */
1191 chan = sc->sc_net[sc->sc_net_cur].channel;
1192 ic->ic_curchan = &ic->ic_channels[chan];
1193 }
1194
1195 static int
1196 cmalo_wep(struct malo_softc *sc)
1197 {
1198 struct ieee80211com *ic = &sc->sc_ic;
1199 int i;
1200
1201 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1202 struct ieee80211_key *key = &ic->ic_crypto.cs_nw_keys[i];
1203
1204 if (!key->wk_keylen)
1205 continue;
1206
1207 DPRINTF(1, "%s: setting wep key for index %d\n",
1208 device_xname(sc->sc_dev), i);
1209
1210 cmalo_cmd_set_wep(sc, i, key);
1211 }
1212
1213 return 0;
1214 }
1215
1216 static int
1217 cmalo_rate2bitmap(int rate)
1218 {
1219 switch (rate) {
1220 /* CCK rates */
1221 case 0: return MALO_RATE_BITMAP_DS1;
1222 case 1: return MALO_RATE_BITMAP_DS2;
1223 case 2: return MALO_RATE_BITMAP_DS5;
1224 case 3: return MALO_RATE_BITMAP_DS11;
1225
1226 /* OFDM rates */
1227 case 4: return MALO_RATE_BITMAP_OFDM6;
1228 case 5: return MALO_RATE_BITMAP_OFDM9;
1229 case 6: return MALO_RATE_BITMAP_OFDM12;
1230 case 7: return MALO_RATE_BITMAP_OFDM18;
1231 case 8: return MALO_RATE_BITMAP_OFDM24;
1232 case 9: return MALO_RATE_BITMAP_OFDM36;
1233 case 10: return MALO_RATE_BITMAP_OFDM48;
1234 case 11: return MALO_RATE_BITMAP_OFDM54;
1235
1236 /* unknown rate: should not happen */
1237 default: return 0;
1238 }
1239 }
1240
1241 static void
1242 cmalo_hexdump(void *buf, int len)
1243 {
1244 #ifdef CMALO_DEBUG
1245 int i;
1246
1247 if (cmalo_d >= 2) {
1248 for (i = 0; i < len; i++) {
1249 if (i % 16 == 0)
1250 printf("%s%5i:", i ? "\n" : "", i);
1251 if (i % 4 == 0)
1252 printf(" ");
1253 printf("%02x", (int)*((u_char *)buf + i));
1254 }
1255 printf("\n");
1256 }
1257 #endif
1258 }
1259
1260 static int
1261 cmalo_cmd_get_hwspec(struct malo_softc *sc)
1262 {
1263 struct malo_cmd_header *hdr;
1264 struct malo_cmd_body_spec *body;
1265 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1266
1267 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1268 hdr->cmd = htole16(MALO_CMD_HWSPEC);
1269 hdr->size = htole16(sizeof(*body));
1270 hdr->seqnum = htole16(1);
1271 hdr->result = 0;
1272
1273 body = (struct malo_cmd_body_spec *)(hdr + 1);
1274 memset(body, 0, sizeof(*body));
1275 /* set all bits for MAC address, otherwise we won't get one back */
1276 memset(body->macaddr, 0xff, ETHER_ADDR_LEN);
1277
1278 /* process command request */
1279 if (cmalo_cmd_request(sc, psize, 0) != 0)
1280 return EIO;
1281
1282 /* process command repsonse */
1283 cmalo_cmd_response(sc);
1284
1285 return 0;
1286 }
1287
1288 static int
1289 cmalo_cmd_rsp_hwspec(struct malo_softc *sc)
1290 {
1291 struct ieee80211com *ic = &sc->sc_ic;
1292 struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
1293 struct malo_cmd_body_spec *body;
1294 int i;
1295
1296 body = (struct malo_cmd_body_spec *)(hdr + 1);
1297
1298 /* get our MAC address */
1299 for (i = 0; i < ETHER_ADDR_LEN; i++)
1300 ic->ic_myaddr[i] = body->macaddr[i];
1301
1302 return 0;
1303 }
1304
1305 static int
1306 cmalo_cmd_set_reset(struct malo_softc *sc)
1307 {
1308 struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
1309 const uint16_t psize = sizeof(*hdr);
1310
1311 hdr->cmd = htole16(MALO_CMD_RESET);
1312 hdr->size = 0;
1313 hdr->seqnum = htole16(1);
1314 hdr->result = 0;
1315
1316 /* process command request */
1317 if (cmalo_cmd_request(sc, psize, 1) != 0)
1318 return EIO;
1319
1320 /* give the device some time to finish the reset */
1321 delay(100);
1322
1323 return 0;
1324 }
1325
1326 static int
1327 cmalo_cmd_set_scan(struct malo_softc *sc)
1328 {
1329 struct ieee80211com *ic = &sc->sc_ic;
1330 struct malo_cmd_header *hdr;
1331 struct malo_cmd_body_scan *body;
1332 struct malo_cmd_tlv_ssid *body_ssid;
1333 struct malo_cmd_tlv_chanlist *body_chanlist;
1334 struct malo_cmd_tlv_rates *body_rates;
1335 uint16_t psize;
1336 int i;
1337
1338 psize = sizeof(*hdr) + sizeof(*body) +
1339 sizeof(*body_ssid) + sizeof(*body_chanlist) + sizeof(*body_rates);
1340
1341 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1342 hdr->cmd = htole16(MALO_CMD_SCAN);
1343 hdr->seqnum = htole16(1);
1344 hdr->result = 0;
1345
1346 body = (struct malo_cmd_body_scan *)(hdr + 1);
1347 body->bsstype = 0x03; /* any BSS */
1348 memset(body->bssid, 0xff, ETHER_ADDR_LEN);
1349
1350 body_ssid = (struct malo_cmd_tlv_ssid *)(body + 1);
1351 body_ssid->type = htole16(MALO_TLV_TYPE_SSID);
1352 body_ssid->size = htole16(0);
1353
1354 body_chanlist = (struct malo_cmd_tlv_chanlist *)(body_ssid + 1);
1355 body_chanlist->type = htole16(MALO_TLV_TYPE_CHANLIST);
1356 body_chanlist->size = htole16(sizeof(body_chanlist->data));
1357 for (i = 0; i < CHANNELS; i++) {
1358 body_chanlist->data[i].radiotype = 0x00;
1359 body_chanlist->data[i].channumber = (i + 1);
1360 body_chanlist->data[i].scantype = 0x00; /* active */
1361 body_chanlist->data[i].minscantime = htole16(0);
1362 body_chanlist->data[i].maxscantime = htole16(100);
1363 }
1364
1365 body_rates = (struct malo_cmd_tlv_rates *)(body_chanlist + 1);
1366 body_rates->type = htole16(MALO_TLV_TYPE_RATES);
1367 body_rates->size =
1368 htole16(ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates);
1369 memcpy(body_rates->data, ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates,
1370 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates);
1371 psize += le16toh(body_rates->size);
1372
1373 memset((char *)(body_rates + 1) + le16toh(body_rates->size), 0,
1374 sizeof(struct malo_cmd_tlv_numprobes));
1375
1376 hdr->size = htole16(psize - sizeof(*hdr));
1377
1378 /* process command request */
1379 if (cmalo_cmd_request(sc, psize, 0) != 0)
1380 return EIO;
1381
1382 /* process command repsonse */
1383 cmalo_cmd_response(sc);
1384
1385 return 0;
1386 }
1387
1388 static int
1389 cmalo_cmd_rsp_scan(struct malo_softc *sc)
1390 {
1391 struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
1392 struct malo_cmd_body_rsp_scan *body;
1393 struct malo_cmd_body_rsp_scan_set *set;
1394 int i;
1395
1396 memset(sc->sc_net, 0, sizeof(sc->sc_net));
1397
1398 body = (struct malo_cmd_body_rsp_scan *)(hdr + 1);
1399 body->bufsize = le16toh(body->bufsize);
1400
1401 DPRINTF(1, "bufsize=%d, APs=%d\n", body->bufsize, body->numofset);
1402 sc->sc_net_num = body->numofset;
1403
1404 set = (struct malo_cmd_body_rsp_scan_set *)(body + 1);
1405
1406 /* cycle through found networks */
1407 for (i = 0; i < body->numofset; i++) {
1408 set->size = le16toh(set->size);
1409 set->beaconintvl = le16toh(set->beaconintvl);
1410 set->capinfo = le16toh(set->capinfo);
1411
1412 DPRINTF(1, "size=%d, bssid=%s, rssi=%d, beaconintvl=%d, "
1413 "capinfo=0x%04x\n",
1414 set->size, ether_sprintf(set->bssid), set->rssi,
1415 set->beaconintvl, set->capinfo);
1416
1417 /* save scan results */
1418 memcpy(sc->sc_net[i].bssid, set->bssid, sizeof(set->bssid));
1419 sc->sc_net[i].rssi = set->rssi;
1420 memcpy(sc->sc_net[i].timestamp, set->timestamp,
1421 sizeof(set->timestamp));
1422 sc->sc_net[i].beaconintvl = set->beaconintvl;
1423 sc->sc_net[i].capinfo = set->capinfo;
1424
1425 cmalo_parse_elements(sc, set->data,
1426 set->size - (sizeof(*set) - sizeof(set->size)), i);
1427
1428 set = (struct malo_cmd_body_rsp_scan_set *)
1429 ((char *)set + sizeof(set->size) + set->size);
1430 }
1431
1432 return 0;
1433 }
1434
1435 static int
1436 cmalo_parse_elements(struct malo_softc *sc, uint8_t *buf, int size, int pos)
1437 {
1438 uint8_t eid, len;
1439 int i;
1440
1441 DPRINTF(2, "element_size=%d, element_pos=%d\n", size, pos);
1442
1443 for (i = 0; i < size; ) {
1444 eid = *(uint8_t *)(buf + i);
1445 i++;
1446 len = *(uint8_t *)(buf + i);
1447 i++;
1448 DPRINTF(2, "eid=%d, len=%d, ", eid, len);
1449
1450 switch (eid) {
1451 case IEEE80211_ELEMID_SSID:
1452 memcpy(sc->sc_net[pos].ssid, buf + i, len);
1453 DPRINTF(2, "ssid=%s\n", sc->sc_net[pos].ssid);
1454 break;
1455 case IEEE80211_ELEMID_RATES:
1456 memcpy(sc->sc_net[pos].rates, buf + i, len);
1457 DPRINTF(2, "rates\n");
1458 break;
1459 case IEEE80211_ELEMID_DSPARMS:
1460 sc->sc_net[pos].channel = *(uint8_t *)(buf + i);
1461 DPRINTF(2, "chnl=%d\n", sc->sc_net[pos].channel);
1462 break;
1463 default:
1464 DPRINTF(2, "unknown\n");
1465 break;
1466 }
1467
1468 i += len;
1469 }
1470
1471 return 0;
1472 }
1473
1474 static int
1475 cmalo_cmd_set_auth(struct malo_softc *sc)
1476 {
1477 struct malo_cmd_header *hdr;
1478 struct malo_cmd_body_auth *body;
1479 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1480
1481 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1482 hdr->cmd = htole16(MALO_CMD_AUTH);
1483 hdr->size = htole16(sizeof(*body));
1484 hdr->seqnum = htole16(1);
1485 hdr->result = 0;
1486
1487 body = (struct malo_cmd_body_auth *)(hdr + 1);
1488 memcpy(body->peermac, sc->sc_net[sc->sc_net_cur].bssid, ETHER_ADDR_LEN);
1489 body->authtype = 0;
1490
1491 /* process command request */
1492 if (cmalo_cmd_request(sc, psize, 0) != 0)
1493 return EIO;
1494
1495 /* process command repsonse */
1496 cmalo_cmd_response(sc);
1497
1498 return 0;
1499 }
1500
1501 static int
1502 cmalo_cmd_set_wep(struct malo_softc *sc, uint16_t index,
1503 struct ieee80211_key *key)
1504 {
1505 struct malo_cmd_header *hdr;
1506 struct malo_cmd_body_wep *body;
1507 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1508
1509 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1510 hdr->cmd = htole16(MALO_CMD_WEP);
1511 hdr->size = htole16(sizeof(*body));
1512 hdr->seqnum = htole16(1);
1513 hdr->result = 0;
1514
1515 body = (struct malo_cmd_body_wep *)(hdr + 1);
1516 memset(body, 0, sizeof(*body));
1517 body->action = htole16(MALO_WEP_ACTION_TYPE_ADD);
1518 body->key_index = htole16(index);
1519
1520 if (body->key_index == 0) {
1521 if (key->wk_keylen > 5)
1522 body->key_type_1 = MALO_WEP_KEY_TYPE_104BIT;
1523 else
1524 body->key_type_1 = MALO_WEP_KEY_TYPE_40BIT;
1525 memcpy(body->key_value_1, key->wk_key, key->wk_keylen);
1526 }
1527 if (body->key_index == 1) {
1528 if (key->wk_keylen > 5)
1529 body->key_type_2 = MALO_WEP_KEY_TYPE_104BIT;
1530 else
1531 body->key_type_2 = MALO_WEP_KEY_TYPE_40BIT;
1532 memcpy(body->key_value_2, key->wk_key, key->wk_keylen);
1533 }
1534 if (body->key_index == 2) {
1535 if (key->wk_keylen > 5)
1536 body->key_type_3 = MALO_WEP_KEY_TYPE_104BIT;
1537 else
1538 body->key_type_3 = MALO_WEP_KEY_TYPE_40BIT;
1539 memcpy(body->key_value_3, key->wk_key, key->wk_keylen);
1540 }
1541 if (body->key_index == 3) {
1542 if (key->wk_keylen > 5)
1543 body->key_type_4 = MALO_WEP_KEY_TYPE_104BIT;
1544 else
1545 body->key_type_4 = MALO_WEP_KEY_TYPE_40BIT;
1546 memcpy(body->key_value_4, key->wk_key, key->wk_keylen);
1547 }
1548
1549 /* process command request */
1550 if (cmalo_cmd_request(sc, psize, 0) != 0)
1551 return EIO;
1552
1553 /* process command repsonse */
1554 cmalo_cmd_response(sc);
1555
1556 return 0;
1557 }
1558
1559 static int
1560 cmalo_cmd_set_snmp(struct malo_softc *sc, uint16_t oid)
1561 {
1562 struct malo_cmd_header *hdr;
1563 struct malo_cmd_body_snmp *body;
1564 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1565
1566 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1567 hdr->cmd = htole16(MALO_CMD_SNMP);
1568 hdr->size = htole16(sizeof(*body));
1569 hdr->seqnum = htole16(1);
1570 hdr->result = 0;
1571
1572 body = (struct malo_cmd_body_snmp *)(hdr + 1);
1573 memset(body, 0, sizeof(*body));
1574 body->action = htole16(1);
1575
1576 switch (oid) {
1577 case MALO_OID_RTSTRESH:
1578 body->oid = htole16(MALO_OID_RTSTRESH);
1579 body->size = htole16(2);
1580 *(uint16_t *)body->data = htole16(2347);
1581 break;
1582 case MALO_OID_SHORTRETRY:
1583 body->oid = htole16(MALO_OID_SHORTRETRY);
1584 body->size = htole16(2);
1585 *(uint16_t *)body->data = htole16(4);
1586 break;
1587 case MALO_OID_FRAGTRESH:
1588 body->oid = htole16(MALO_OID_FRAGTRESH);
1589 body->size = htole16(2);
1590 *(uint16_t *)body->data = htole16(2346);
1591 break;
1592 case MALO_OID_80211D:
1593 body->oid = htole16(MALO_OID_80211D);
1594 body->size = htole16(2);
1595 *(uint16_t *)body->data = htole16(1);
1596 break;
1597 default:
1598 break;
1599 }
1600
1601 /* process command request */
1602 if (cmalo_cmd_request(sc, psize, 0) != 0)
1603 return EIO;
1604
1605 /* process command repsonse */
1606 cmalo_cmd_response(sc);
1607
1608 return 0;
1609 }
1610
1611 static int
1612 cmalo_cmd_set_radio(struct malo_softc *sc, uint16_t control)
1613 {
1614 struct malo_cmd_header *hdr;
1615 struct malo_cmd_body_radio *body;
1616 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1617
1618 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1619 hdr->cmd = htole16(MALO_CMD_RADIO);
1620 hdr->size = htole16(sizeof(*body));
1621 hdr->seqnum = htole16(1);
1622 hdr->result = 0;
1623
1624 body = (struct malo_cmd_body_radio *)(hdr + 1);
1625 body->action = htole16(1);
1626 if (control)
1627 body->control =
1628 htole16(MALO_CMD_RADIO_ON | MALO_CMD_RADIO_AUTO_P);
1629 else
1630 body->control = 0;
1631
1632 /* process command request */
1633 if (cmalo_cmd_request(sc, psize, 0) != 0)
1634 return EIO;
1635
1636 /* process command repsonse */
1637 cmalo_cmd_response(sc);
1638
1639 return 0;
1640 }
1641
1642 static int
1643 cmalo_cmd_set_channel(struct malo_softc *sc, uint16_t channel)
1644 {
1645 struct malo_cmd_header *hdr;
1646 struct malo_cmd_body_channel *body;
1647 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1648
1649 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1650 hdr->cmd = htole16(MALO_CMD_CHANNEL);
1651 hdr->size = htole16(sizeof(*body));
1652 hdr->seqnum = htole16(1);
1653 hdr->result = 0;
1654
1655 body = (struct malo_cmd_body_channel *)(hdr + 1);
1656 memset(body, 0, sizeof(*body));
1657 body->action = htole16(1);
1658 body->channel = htole16(channel);
1659
1660 /* process command request */
1661 if (cmalo_cmd_request(sc, psize, 0) != 0)
1662 return EIO;
1663
1664 /* process command repsonse */
1665 cmalo_cmd_response(sc);
1666
1667 return 0;
1668 }
1669
1670
1671 static int
1672 cmalo_cmd_set_txpower(struct malo_softc *sc, int16_t txpower)
1673 {
1674 struct malo_cmd_header *hdr;
1675 struct malo_cmd_body_txpower *body;
1676 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1677
1678 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1679 hdr->cmd = htole16(MALO_CMD_TXPOWER);
1680 hdr->size = htole16(sizeof(*body));
1681 hdr->seqnum = htole16(1);
1682 hdr->result = 0;
1683
1684 body = (struct malo_cmd_body_txpower *)(hdr + 1);
1685 body->action = htole16(1);
1686 body->txpower = htole16(txpower);
1687
1688 /* process command request */
1689 if (cmalo_cmd_request(sc, psize, 0) != 0)
1690 return EIO;
1691
1692 /* process command repsonse */
1693 cmalo_cmd_response(sc);
1694
1695 return 0;
1696 }
1697
1698 static int
1699 cmalo_cmd_set_antenna(struct malo_softc *sc, uint16_t action)
1700 {
1701 struct malo_cmd_header *hdr;
1702 struct malo_cmd_body_antenna *body;
1703 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1704
1705 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1706 hdr->cmd = htole16(MALO_CMD_ANTENNA);
1707 hdr->size = htole16(sizeof(*body));
1708 hdr->seqnum = htole16(1);
1709 hdr->result = 0;
1710
1711 body = (struct malo_cmd_body_antenna *)(hdr + 1);
1712 /* 1 = set RX, 2 = set TX */
1713 body->action = htole16(action);
1714
1715 switch (action) {
1716 case 1:
1717 /* set RX antenna */
1718 body->antenna_mode = htole16(0xffff);
1719 break;
1720
1721 case 2:
1722 /* set TX antenna */
1723 body->antenna_mode = htole16(2);
1724 break;
1725
1726 default:
1727 body->antenna_mode = 0;
1728 break;
1729 }
1730
1731 /* process command request */
1732 if (cmalo_cmd_request(sc, psize, 0) != 0)
1733 return EIO;
1734
1735 /* process command repsonse */
1736 cmalo_cmd_response(sc);
1737
1738 return 0;
1739 }
1740
1741 static int
1742 cmalo_cmd_set_macctrl(struct malo_softc *sc)
1743 {
1744 struct ieee80211com *ic = &sc->sc_ic;
1745 struct malo_cmd_header *hdr;
1746 struct malo_cmd_body_macctrl *body;
1747 uint16_t psize;
1748
1749 psize = sizeof(*hdr) + sizeof(*body);
1750
1751 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1752 hdr->cmd = htole16(MALO_CMD_MACCTRL);
1753 hdr->size = htole16(sizeof(*body));
1754 hdr->seqnum = htole16(1);
1755 hdr->result = 0;
1756
1757 body = (struct malo_cmd_body_macctrl *)(hdr + 1);
1758 memset(body, 0, sizeof(*body));
1759 body->action = htole16(MALO_CMD_MACCTRL_RX_ON | MALO_CMD_MACCTRL_TX_ON);
1760 if (ic->ic_opmode == IEEE80211_M_MONITOR)
1761 body->action |= htole16(MALO_CMD_MACCTRL_PROMISC_ON);
1762
1763 /* process command request */
1764 if (cmalo_cmd_request(sc, psize, 0) != 0)
1765 return EIO;
1766
1767 /* process command repsonse */
1768 cmalo_cmd_response(sc);
1769
1770 return 0;
1771 }
1772
1773 static int
1774 cmalo_cmd_set_macaddr(struct malo_softc *sc, uint8_t *macaddr)
1775 {
1776 struct malo_cmd_header *hdr;
1777 struct malo_cmd_body_macaddr *body;
1778 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1779
1780 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1781 hdr->cmd = htole16(MALO_CMD_MACADDR);
1782 hdr->size = htole16(sizeof(*body));
1783 hdr->seqnum = htole16(1);
1784 hdr->result = 0;
1785
1786 body = (struct malo_cmd_body_macaddr *)(hdr + 1);
1787 body->action = htole16(1);
1788 memcpy(body->macaddr, macaddr, ETHER_ADDR_LEN);
1789
1790 /* process command request */
1791 if (cmalo_cmd_request(sc, psize, 0) != 0)
1792 return EIO;
1793
1794 /* process command repsonse */
1795 cmalo_cmd_response(sc);
1796
1797 return 0;
1798 }
1799
1800 static int
1801 cmalo_cmd_set_assoc(struct malo_softc *sc)
1802 {
1803 struct malo_cmd_header *hdr;
1804 struct malo_cmd_body_assoc *body;
1805 struct malo_cmd_tlv_ssid *body_ssid;
1806 struct malo_cmd_tlv_phy *body_phy;
1807 struct malo_cmd_tlv_cf *body_cf;
1808 struct malo_cmd_tlv_rates *body_rates;
1809 struct malo_cmd_tlv_passeid *body_passeid;
1810 uint16_t psize;
1811
1812 psize = sizeof(*hdr) + sizeof(*body) + sizeof(*body_ssid) +
1813 sizeof(body_phy) + sizeof(*body_cf) + sizeof(*body_rates);
1814
1815 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1816 hdr->cmd = htole16(MALO_CMD_ASSOC);
1817 hdr->seqnum = htole16(1);
1818 hdr->result = 0;
1819
1820 body = (struct malo_cmd_body_assoc *)(hdr + 1);
1821 memset(body, 0, sizeof(*body));
1822 memcpy(body->peermac, sc->sc_net[sc->sc_net_cur].bssid, ETHER_ADDR_LEN);
1823 body->capinfo = htole16(sc->sc_net[sc->sc_net_cur].capinfo);
1824 body->listenintrv = htole16(10);
1825
1826 body_ssid = (struct malo_cmd_tlv_ssid *)(body + 1);
1827 body_ssid->type = htole16(MALO_TLV_TYPE_SSID);
1828 body_ssid->size = htole16(strlen(sc->sc_net[sc->sc_net_cur].ssid));
1829 memcpy(body_ssid->data, sc->sc_net[sc->sc_net_cur].ssid,
1830 le16toh(body_ssid->size));
1831 psize += le16toh(body_ssid->size);
1832
1833 body_phy = (struct malo_cmd_tlv_phy *)
1834 ((char *)(body_ssid + 1) + le16toh(body_ssid->size));
1835 body_phy->type = htole16(MALO_TLV_TYPE_PHY);
1836 body_phy->size = htole16(1);
1837 body_phy->data[0] = sc->sc_net[sc->sc_net_cur].channel;
1838 psize += le16toh(body_phy->size);
1839
1840 body_cf = (struct malo_cmd_tlv_cf *)
1841 ((char *)(body_phy + 1) + le16toh(body_phy->size));
1842 body_cf->type = htole16(MALO_TLV_TYPE_CF);
1843 body_cf->size = htole16(0);
1844
1845 body_rates = (struct malo_cmd_tlv_rates *)(body_cf + 1);
1846 body_rates->type = htole16(MALO_TLV_TYPE_RATES);
1847 body_rates->size = htole16(strlen(sc->sc_net[sc->sc_net_cur].rates));
1848 memcpy(body_rates->data, sc->sc_net[sc->sc_net_cur].rates,
1849 le16toh(body_rates->size));
1850 psize += le16toh(body_rates->size);
1851
1852 /* hack to correct FW's wrong generated rates-element-id */
1853 body_passeid = (struct malo_cmd_tlv_passeid *)
1854 ((char *)(body_rates + 1) + le16toh(body_rates->size));
1855 body_passeid->type = htole16(MALO_TLV_TYPE_PASSEID);
1856 body_passeid->size = body_rates->size;
1857 memcpy(body_passeid->data, body_rates->data, le16toh(body_rates->size));
1858 psize += le16toh(body_passeid->size);
1859
1860 hdr->size = htole16(psize - sizeof(*hdr));
1861
1862 /* process command request */
1863 if (!sc->sc_cmd_ctxsave) {
1864 if (cmalo_cmd_request(sc, psize, 1) != 0)
1865 return EIO;
1866 return 0;
1867 }
1868 if (cmalo_cmd_request(sc, psize, 0) != 0)
1869 return EIO;
1870
1871 /* process command repsonse */
1872 cmalo_cmd_response(sc);
1873
1874 return 0;
1875 }
1876
1877 static int
1878 cmalo_cmd_rsp_assoc(struct malo_softc *sc)
1879 {
1880 struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
1881 struct malo_cmd_body_rsp_assoc *body;
1882
1883 body = (struct malo_cmd_body_rsp_assoc *)(hdr + 1);
1884
1885 if (body->status) {
1886 DPRINTF(1, "%s: association failed (status %d)\n",
1887 device_xname(sc->sc_dev), body->status);
1888 sc->sc_flags |= MALO_ASSOC_FAILED;
1889 } else
1890 DPRINTF(1, "%s: association successful\n",
1891 device_xname(sc->sc_dev));
1892
1893 return 0;
1894 }
1895
1896 static int
1897 cmalo_cmd_set_rate(struct malo_softc *sc, int rate)
1898 {
1899 struct malo_cmd_header *hdr;
1900 struct malo_cmd_body_rate *body;
1901 const uint16_t psize = sizeof(*hdr) + sizeof(*body);
1902
1903 hdr = (struct malo_cmd_header *)sc->sc_cmd;
1904 hdr->cmd = htole16(MALO_CMD_RATE);
1905 hdr->size = htole16(sizeof(*body));
1906 hdr->seqnum = htole16(1);
1907 hdr->result = 0;
1908
1909 body = (struct malo_cmd_body_rate *)(hdr + 1);
1910 body->action = htole16(1);
1911 if (rate == IEEE80211_FIXED_RATE_NONE) {
1912 body->hwauto = htole16(1);
1913 body->ratebitmap = htole16(MALO_RATE_BITMAP_AUTO);
1914 } else {
1915 body->hwauto = 0;
1916 body->ratebitmap = htole16(cmalo_rate2bitmap(rate));
1917 }
1918
1919 /* process command request */
1920 if (cmalo_cmd_request(sc, psize, 0) != 0)
1921 return EIO;
1922
1923 /* process command repsonse */
1924 cmalo_cmd_response(sc);
1925
1926 return 0;
1927 }
1928
1929 static int
1930 cmalo_cmd_request(struct malo_softc *sc, uint16_t psize, int no_response)
1931 {
1932 uint8_t *cmd;
1933
1934 mutex_enter(&sc->sc_mtx);
1935
1936 cmalo_hexdump(sc->sc_cmd, psize);
1937
1938 /* send command request */
1939 MALO_WRITE_2(sc, MALO_REG_CMD_WRITE_LEN, psize);
1940 MALO_WRITE_MULTI_2(sc, MALO_REG_CMD_WRITE,
1941 (uint16_t *)sc->sc_cmd, psize / sizeof(uint16_t));
1942 if (psize & 0x0001) {
1943 cmd = sc->sc_cmd;
1944 MALO_WRITE_1(sc, MALO_REG_CMD_WRITE, cmd[psize - 1]);
1945 }
1946 MALO_WRITE_1(sc, MALO_REG_HOST_STATUS, MALO_VAL_CMD_DL_OVER);
1947 MALO_WRITE_2(sc, MALO_REG_CARD_INTR_CAUSE, MALO_VAL_CMD_DL_OVER);
1948
1949 if (no_response) {
1950 mutex_exit(&sc->sc_mtx);
1951
1952 /* we don't expect a response */
1953 return 0;
1954 }
1955
1956 /* wait for the command response */
1957 if (cv_timedwait_sig(&sc->sc_cv, &sc->sc_mtx, 500) == EWOULDBLOCK) {
1958 mutex_exit(&sc->sc_mtx);
1959 aprint_error_dev(sc->sc_dev,
1960 "timeout while waiting for cmd response\n");
1961 return EIO;
1962 }
1963 mutex_exit(&sc->sc_mtx);
1964
1965 return 0;
1966 }
1967
1968 static int
1969 cmalo_cmd_response(struct malo_softc *sc)
1970 {
1971 struct malo_cmd_header *hdr = (struct malo_cmd_header *)sc->sc_cmd;
1972 uint16_t psize;
1973 int s;
1974
1975 s = splnet();
1976
1977 #ifdef CMALO_DEBUG
1978 memset(sc->sc_cmd, 0, MALO_CMD_BUFFER_SIZE);
1979 #endif
1980
1981 /* read the whole command response */
1982 psize = MALO_READ_2(sc, MALO_REG_CMD_READ_LEN);
1983 if (psize > MALO_CMD_BUFFER_SIZE) {
1984 aprint_error_dev(sc->sc_dev,
1985 "command response too large: %dbyte\n", psize);
1986 return EIO;
1987 }
1988
1989 MALO_READ_MULTI_2(sc, MALO_REG_CMD_READ,
1990 (uint16_t *)sc->sc_cmd, psize / sizeof(uint16_t));
1991 if (psize & 0x0001)
1992 sc->sc_cmd[psize - 1] = MALO_READ_1(sc, MALO_REG_CMD_READ);
1993
1994 cmalo_hexdump(sc->sc_cmd, psize);
1995
1996 /*
1997 * We convert the header values into the machines correct endianess,
1998 * so we don't have to le16toh() all over the code. The body is
1999 * kept in the cards order, little endian. We need to take care
2000 * about the body endianess in the corresponding response routines.
2001 */
2002 hdr->cmd = le16toh(hdr->cmd);
2003 hdr->size = le16toh(hdr->size);
2004 hdr->seqnum = le16toh(hdr->seqnum);
2005 hdr->result = le16toh(hdr->result);
2006
2007 /* check for a valid command response */
2008 if (!(hdr->cmd & MALO_CMD_RESP)) {
2009 aprint_error_dev(sc->sc_dev,
2010 "got invalid command response (0x%04x)\n", hdr->cmd);
2011 splx(s);
2012 return EIO;
2013 }
2014 hdr->cmd &= ~MALO_CMD_RESP;
2015
2016 /* association cmd response is special */
2017 if (hdr->cmd == 0x0012)
2018 hdr->cmd = MALO_CMD_ASSOC;
2019
2020 /* to which command does the response belong */
2021 switch (hdr->cmd) {
2022 case MALO_CMD_HWSPEC:
2023 DPRINTF(1, "%s: got hwspec cmd response\n",
2024 device_xname(sc->sc_dev));
2025 cmalo_cmd_rsp_hwspec(sc);
2026 break;
2027 case MALO_CMD_RESET:
2028 /* reset will not send back a response */
2029 break;
2030 case MALO_CMD_SCAN:
2031 DPRINTF(1, "%s: got scan cmd response\n",
2032 device_xname(sc->sc_dev));
2033 cmalo_cmd_rsp_scan(sc);
2034 break;
2035 case MALO_CMD_AUTH:
2036 /* do nothing */
2037 DPRINTF(1, "%s: got auth cmd response\n",
2038 device_xname(sc->sc_dev));
2039 break;
2040 case MALO_CMD_WEP:
2041 /* do nothing */
2042 DPRINTF(1, "%s: got wep cmd response\n",
2043 device_xname(sc->sc_dev));
2044 break;
2045 case MALO_CMD_SNMP:
2046 /* do nothing */
2047 DPRINTF(1, "%s: got snmp cmd response\n",
2048 device_xname(sc->sc_dev));
2049 break;
2050 case MALO_CMD_RADIO:
2051 /* do nothing */
2052 DPRINTF(1, "%s: got radio cmd response\n",
2053 device_xname(sc->sc_dev));
2054 break;
2055 case MALO_CMD_CHANNEL:
2056 /* do nothing */
2057 DPRINTF(1, "%s: got channel cmd response\n",
2058 device_xname(sc->sc_dev));
2059 break;
2060 case MALO_CMD_TXPOWER:
2061 /* do nothing */
2062 DPRINTF(1, "%s: got txpower cmd response\n",
2063 device_xname(sc->sc_dev));
2064 break;
2065 case MALO_CMD_ANTENNA:
2066 /* do nothing */
2067 DPRINTF(1, "%s: got antenna cmd response\n",
2068 device_xname(sc->sc_dev));
2069 break;
2070 case MALO_CMD_MACCTRL:
2071 /* do nothing */
2072 DPRINTF(1, "%s: got macctrl cmd response\n",
2073 device_xname(sc->sc_dev));
2074 break;
2075 case MALO_CMD_MACADDR:
2076 /* do nothing */
2077 DPRINTF(1, "%s: got macaddr cmd response\n",
2078 device_xname(sc->sc_dev));
2079 break;
2080 case MALO_CMD_ASSOC:
2081 /* do nothing */
2082 DPRINTF(1, "%s: got assoc cmd response\n",
2083 device_xname(sc->sc_dev));
2084 cmalo_cmd_rsp_assoc(sc);
2085 break;
2086 case MALO_CMD_80211D:
2087 /* do nothing */
2088 DPRINTF(1, "%s: got 80211d cmd response\n",
2089 device_xname(sc->sc_dev));
2090 break;
2091 case MALO_CMD_BGSCAN_CONFIG:
2092 /* do nothing */
2093 DPRINTF(1, "%s: got bgscan config cmd response\n",
2094 device_xname(sc->sc_dev));
2095 break;
2096 case MALO_CMD_BGSCAN_QUERY:
2097 /* do nothing */
2098 DPRINTF(1, "%s: got bgscan query cmd response\n",
2099 device_xname(sc->sc_dev));
2100 break;
2101 case MALO_CMD_RATE:
2102 /* do nothing */
2103 DPRINTF(1, "%s: got rate cmd response\n",
2104 device_xname(sc->sc_dev));
2105 break;
2106 default:
2107 aprint_error_dev(sc->sc_dev,
2108 "got unknown cmd response (0x%04x)\n", hdr->cmd);
2109 break;
2110 }
2111
2112 splx(s);
2113
2114 return 0;
2115 }
2116
2117 #ifdef _MODULE
2118
2119 MODULE(MODULE_CLASS_DRIVER, malo_pcmcia, NULL);
2120
2121 CFDRIVER_DECL(malo_pcmcia, DV_IFNET, NULL);
2122 extern struct cfattach malo_pcmcia_ca;
2123 static int malo_pcmcialoc[] = { -1 };
2124 static struct cfparent pcmciaparent = {
2125 "pcmcia", NULL, DVUNIT_ANY
2126 };
2127 static struct cfdata malo_pcmcia_cfdata[] = {
2128 {
2129 .cf_name = "malo_pcmcia",
2130 .cf_atname = "malo",
2131 .cf_unit = 0,
2132 .cf_fstate = FSTATE_STAR,
2133 .cf_loc = malo_pcmcialoc,
2134 .cf_flags = 0,
2135 .cf_pspec = &pcmciaparent,
2136 },
2137 { NULL }
2138 };
2139
2140 static int
2141 malo_pcmcia_modcmd(modcmd_t cmd, void *arg)
2142 {
2143 int err;
2144
2145 switch (cmd) {
2146 case MODULE_CMD_INIT:
2147 err = config_cfdriver_attach(&malo_pcmcia_cd);
2148 if (err)
2149 return err;
2150 err = config_cfattach_attach("malo_pcmcia", &malo_pcmcia_ca);
2151 if (err) {
2152 config_cfdriver_detach(&malo_pcmcia_cd);
2153 return err;
2154 }
2155 err = config_cfdata_attach(malo_pcmcia_cfdata, 1);
2156 if (err) {
2157 config_cfattach_detach("malo_pcmcia", &malo_pcmcia_ca);
2158 config_cfdriver_detach(&malo_pcmcia_cd);
2159 return err;
2160 }
2161 return 0;
2162 case MODULE_CMD_FINI:
2163 err = config_cfdata_detach(malo_pcmcia_cfdata);
2164 if (err)
2165 return err;
2166 config_cfattach_detach("malo_pcmcia", &malo_pcmcia_ca);
2167 config_cfdriver_detach(&malo_pcmcia_cd);
2168 return 0;
2169 default:
2170 return ENOTTY;
2171 }
2172 }
2173 #endif
2174