if_malo.c 54 KB

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