rtl8821au.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /* Realtek RTL8821AU USB WiFi: see rtl8821au.h.
  2. *
  3. * Bring-up follows Linux rtw88 (rtw88xxa_power_on() and friends) step by
  4. * step; the comments name the Linux function a block comes from. The
  5. * register tables and power sequences are not in the kernel image: they are
  6. * read from /ramfs/firmware/rtl8821a.tbl (built by mkrtwtbl.c from
  7. * third_party/rtw88), the 8051 firmware from /ramfs/firmware/rtw8821a_fw.bin.
  8. *
  9. * USB: registers are vendor control requests (bRequest 5, wValue = address).
  10. * Every TX queue is mapped to one DMA channel and therefore one bulk-OUT
  11. * endpoint, because the xHCI driver keeps a single bulk-OUT ring and would
  12. * reconfigure it on every endpoint switch. RX aggregation is off, so one
  13. * bulk-IN transfer carries one packet (the parser still walks several).
  14. *
  15. * Page-allocated buffers only: the kernel image must stay below 0x90000. */
  16. #include "rtl8821au.h"
  17. #include "rtl8821a_tbl.h"
  18. #include "ccmp.h"
  19. #include "mm.h"
  20. #include "pci.h"
  21. #include "ramfs.h"
  22. #include "serial.h"
  23. #include "timer.h"
  24. #include "usb.h"
  25. #include "wifi.h"
  26. #define FW_PATH "firmware/rtw8821a_fw.bin"
  27. #define TBL_PATH "firmware/rtl8821a.tbl"
  28. /* ---- registers (Linux rtw88 reg.h) -------------------------------------- */
  29. #define REG_SYS_FUNC_EN 0x0002
  30. #define REG_APS_FSMCO 0x0004
  31. #define REG_SYS_CLKR 0x0008
  32. #define REG_RSV_CTRL 0x001C
  33. #define REG_RF_CTRL 0x001F
  34. #define REG_AFE_CTRL3 0x002C
  35. #define REG_EFUSE_CTRL 0x0030
  36. #define REG_LDO_EFUSE_CTRL 0x0034
  37. #define REG_ACLK_MON 0x003E
  38. #define REG_GPIO_MUXCFG 0x0040
  39. #define REG_LED_CFG 0x004C
  40. #define REG_WL_BT_PWR_CTRL 0x0068
  41. #define REG_SYS_SDIO_CTRL 0x0070
  42. #define REG_RF_B_CTRL 0x0076
  43. #define REG_LDO_SWR_CTRL 0x007C
  44. #define REG_MCUFW_CTRL 0x0080
  45. #define REG_HIMR0 0x00B0
  46. #define REG_HIMR1 0x00B8
  47. #define REG_EFUSE_ACCESS 0x00CF
  48. #define REG_SYS_CFG1 0x00F0
  49. #define REG_CR 0x0100
  50. #define REG_TXDMA_PQ_MAP 0x010C
  51. #define REG_TRXFF_BNDY 0x0114
  52. #define REG_HMETFR 0x01CC
  53. #define REG_LLT_INIT 0x01E0
  54. #define REG_RQPN 0x0200
  55. #define REG_DWBCN0_CTRL 0x0208
  56. #define REG_TXDMA_OFFSET_CHK 0x020C
  57. #define REG_RQPN_NPQ 0x0214
  58. #define REG_DWBCN1_CTRL 0x0228
  59. #define REG_RXDMA_STATUS 0x0288
  60. #define REG_RXDMA_MODE 0x0290
  61. #define REG_EARLY_MODE_CONTROL 0x02BC
  62. #define REG_TXPKT_EMPTY 0x041A
  63. #define REG_FWHW_TXQ_CTRL 0x0420
  64. #define REG_HWSEQ_CTRL 0x0423
  65. #define REG_BCNQ_BDNY 0x0424
  66. #define REG_MGQ_BDNY 0x0425
  67. #define REG_SPEC_SIFS 0x0428
  68. #define REG_RETRY_LIMIT 0x042A
  69. #define REG_RRSR 0x0440
  70. #define REG_ARFR0 0x0444
  71. #define REG_ARFRH0 0x0448
  72. #define REG_ARFR1_V1 0x044C
  73. #define REG_ARFRH1_V1 0x0450
  74. #define REG_CCK_CHECK 0x0454
  75. #define REG_AMPDU_MAX_TIME 0x0456
  76. #define REG_AMPDU_MAX_LENGTH 0x0458
  77. #define REG_WMAC_LBK_BF_HD 0x045D
  78. #define REG_FAST_EDCA_CTRL 0x0460
  79. #define REG_DATA_SC 0x0483
  80. #define REG_ARFR2_V1 0x048C
  81. #define REG_ARFRH2_V1 0x0490
  82. #define REG_ARFR3_V1 0x0494
  83. #define REG_ARFRH3_V1 0x0498
  84. #define REG_QUEUE_CTRL 0x04C6
  85. #define REG_SINGLE_AMPDU_CTRL 0x04C7
  86. #define REG_MAX_AGGR_NUM 0x04CA
  87. #define REG_BAR_MODE_CTRL 0x04CC
  88. #define REG_TX_RPT_TIME 0x04F0
  89. #define REG_EDCA_VO_PARAM 0x0500
  90. #define REG_EDCA_VI_PARAM 0x0504
  91. #define REG_EDCA_BE_PARAM 0x0508
  92. #define REG_EDCA_BK_PARAM 0x050C
  93. #define REG_BCNTCFG 0x0510
  94. #define REG_PIFS 0x0512
  95. #define REG_SIFS 0x0514
  96. #define REG_TBTT_PROHIBIT 0x0540
  97. #define REG_BCN_CTRL 0x0550
  98. #define REG_DRVERLYINT 0x0558
  99. #define REG_BCNDMATIM 0x0559
  100. #define REG_USTIME_TSF 0x055C
  101. #define REG_BCN_MAX_ERR 0x055D
  102. #define REG_RCR 0x0608
  103. #define REG_RX_PKT_LIMIT 0x060C
  104. #define REG_RX_DRVINFO_SZ 0x060F
  105. #define REG_MACID 0x0610
  106. #define REG_BSSID 0x0618
  107. #define REG_MAR 0x0620
  108. #define REG_USTIME_EDCA 0x0638
  109. #define REG_MAC_SPEC_SIFS 0x063A
  110. #define REG_ACKTO 0x0640
  111. #define REG_NAV_CTRL 0x0650
  112. #define REG_WMAC_TRXPTCL_CTL 0x0668
  113. #define REG_SEC_CMD 0x0670
  114. #define REG_RXFLTMAP0 0x06A0
  115. #define REG_RXFLTMAP1 0x06A2
  116. #define REG_RXFLTMAP2 0x06A4
  117. #define REG_AID 0x06A8
  118. #define REG_USB3_RXITV 0xF050
  119. #define REG_USB_HRPWM 0xFE58
  120. /* baseband / RF (Jaguar) */
  121. #define REG_RXPSEL 0x0808
  122. #define REG_TXPSEL 0x080C
  123. #define REG_L1PKTH 0x0848
  124. #define REG_CLKTRK 0x0860
  125. #define REG_ADCCLK 0x08AC
  126. #define REG_HSSI_READ 0x08B0
  127. #define REG_FPGA0_XCD_RF_PARA 0x08B4
  128. #define REG_ADC160 0x08C4
  129. #define REG_CCK_RX 0x0A04
  130. #define REG_3WIRE_SWA 0x0C00
  131. #define REG_TXSCALE_A 0x0C1C
  132. #define REG_TXAGC_A_CCK 0x0C20
  133. #define REG_RXIGI_A 0x0C50
  134. #define REG_TX_PWR_TRAINING_A 0x0C54
  135. #define REG_LSSI_WRITE_A 0x0C90
  136. #define REG_RFE_PINMUX_A 0x0CB0
  137. #define REG_RFE_INV_A 0x0CB4
  138. #define REG_PI_READ_A 0x0D04
  139. #define REG_SI_READ_A 0x0D08
  140. #define REG_TXSCALE_B 0x0E1C
  141. #define BIT(n) (1u << (n))
  142. #define RCR_DEFAULT (BIT(31) | BIT(30) | BIT(29) | BIT(20) | BIT(14) | \
  143. BIT(28) | BIT(3) | BIT(2) | BIT(1)) /* APP_FCS/MIC/ICV,
  144. PKTCTL_DLEN, HTC_LOC_CTRL, APP_PHYSTS, AB, AM, APM */
  145. #define RCR_CBSSID_DATA BIT(6)
  146. #define RCR_CBSSID_BCN BIT(7)
  147. #define FW_READY_LEGACY 0xC6 /* RAM_DL_SEL|WINTINI_RDY|CHK_RPT|RDY */
  148. /* TX descriptor queue selects and rates (rtw88 tx.h/main.h) */
  149. #define QSEL_BE 0
  150. #define QSEL_MGMT 18
  151. #define RATE_1M 0x00
  152. #define RATE_6M 0x04
  153. #define RATE_12M 0x06
  154. #define RATEID_BG 6
  155. #define RATEID_G 7
  156. #define RATEID_B 8
  157. #define TXDESC_SZ 40
  158. #define RXDESC_SZ 24
  159. #define RX_BUF_SZ 8192
  160. #define TX_BUF_SZ 4096
  161. /* DMA mappings (rtw88 main.h enum rtw_dma_mapping) */
  162. #define DMA_LOW 1
  163. #define DMA_NORMAL 2
  164. /* Scan dwell times (ms). */
  165. #define DWELL_ACTIVE 70
  166. #define DWELL_PASSIVE 120
  167. static const unsigned char scan_chans[] = {
  168. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  169. 36, 40, 44, 48, 52, 56, 60, 64,
  170. 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
  171. 149, 153, 157, 161, 165,
  172. };
  173. /* Supported USB IDs: Linux rtw8821au.c. */
  174. static const unsigned short usb_ids[][2] = {
  175. { 0x0bda, 0x0811 }, { 0x0bda, 0x0820 }, { 0x0bda, 0x0821 },
  176. { 0x0bda, 0x8822 }, { 0x0bda, 0x0823 }, { 0x0bda, 0xa811 },
  177. { 0x0411, 0x0242 }, { 0x0411, 0x029b }, { 0x04bb, 0x0953 },
  178. { 0x056e, 0x4007 }, { 0x056e, 0x400e }, { 0x056e, 0x400f },
  179. { 0x0846, 0x9052 }, { 0x0e66, 0x0023 }, { 0x2001, 0x3314 },
  180. { 0x2001, 0x3318 }, { 0x2019, 0xab32 }, { 0x20f4, 0x804b },
  181. { 0x2357, 0x011e }, { 0x2357, 0x011f }, { 0x2357, 0x0120 },
  182. { 0x3823, 0x6249 }, { 0x7392, 0xa811 }, { 0x7392, 0xa812 },
  183. { 0x7392, 0xa813 }, { 0x7392, 0xb611 },
  184. };
  185. static struct {
  186. /* USB */
  187. int present;
  188. unsigned int ctrl, addr, mps, port, gen;
  189. int low;
  190. unsigned int vendor, product;
  191. unsigned int ep_in, ep_in_mps, ep_out[4], n_out, ep_out_mps;
  192. unsigned int ep_tx; /* the one bulk-OUT endpoint used */
  193. unsigned int io_errs;
  194. /* chip */
  195. int up, fatal;
  196. unsigned int cut;
  197. unsigned char *io; /* control-transfer data (page) */
  198. unsigned char *efuse; /* 512-byte logical map (page) */
  199. unsigned char *rxbuf; /* 8 KB, inside 16 KB of pages */
  200. unsigned char *txbuf; /* one page */
  201. const unsigned char *tbl;
  202. unsigned int tbl_size;
  203. unsigned char mac[6];
  204. unsigned int rfe_cond; /* rtw_phy_setup_phy_cond() rfe */
  205. unsigned int bb_swing_2g, bb_swing_5g, crystal;
  206. unsigned int fw_ver, fw_sub;
  207. char fw_name[24];
  208. const char *state;
  209. /* radio */
  210. unsigned int chan;
  211. int band5;
  212. /* scan */
  213. int scanning;
  214. unsigned int scan_idx, scan_next;
  215. /* link */
  216. unsigned char bssid[6];
  217. unsigned char tk[16];
  218. int have_tk;
  219. unsigned int rx_pkts, rx_crc, tx_pkts, tx_errs;
  220. int last_rssi;
  221. } R;
  222. /* ---- small helpers ------------------------------------------------------- */
  223. static void rmemcpy(void *d, const void *s, unsigned int n)
  224. {
  225. unsigned char *dd = d;
  226. const unsigned char *ss = s;
  227. while (n--)
  228. *dd++ = *ss++;
  229. }
  230. static void rmemzero(void *d, unsigned int n)
  231. {
  232. unsigned char *dd = d;
  233. while (n--)
  234. *dd++ = 0;
  235. }
  236. static void udelay(unsigned int us)
  237. {
  238. while (us--)
  239. port_outb(0x80, 0);
  240. }
  241. /* Busy-wait: SYS_WIFI runs with interrupts off (syscall_stub.S), so the
  242. * millisecond tick does not advance inside a call. */
  243. static void mdelay(unsigned int ms)
  244. {
  245. while (ms--)
  246. udelay(1000);
  247. }
  248. static void rlog(const char *s)
  249. {
  250. serial_puts(SERIAL_COM1, s);
  251. }
  252. static void loghex(const char *s, unsigned int v)
  253. {
  254. serial_puts(SERIAL_COM1, s);
  255. serial_puthex(SERIAL_COM1, v);
  256. serial_puts(SERIAL_COM1, "\n");
  257. }
  258. static unsigned int le32(const unsigned char *p)
  259. {
  260. return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
  261. }
  262. static void put32(unsigned char *p, unsigned int v)
  263. {
  264. p[0] = v; p[1] = v >> 8; p[2] = v >> 16; p[3] = v >> 24;
  265. }
  266. static unsigned int ffs32(unsigned int m)
  267. {
  268. unsigned int s = 0;
  269. while (m && !(m & 1)) {
  270. m >>= 1;
  271. s++;
  272. }
  273. return s;
  274. }
  275. /* ---- register access (rtw_usb_read/rtw_usb_write) ------------------------ */
  276. static void io_fail(void)
  277. {
  278. if (++R.io_errs > 16 && !R.fatal) {
  279. rlog("rtl8821au: too many USB errors, device gone?\n");
  280. R.fatal = 1;
  281. }
  282. }
  283. static unsigned int rd(unsigned int a, unsigned int len)
  284. {
  285. int n;
  286. if (R.fatal)
  287. return 0xffffffffu;
  288. R.io[0] = R.io[1] = R.io[2] = R.io[3] = 0;
  289. n = usb_control_in(R.ctrl, R.addr, R.mps, R.low, 0xC0, 0x05,
  290. (unsigned short)a, 0, R.io, len);
  291. if (n != (int)len) {
  292. io_fail();
  293. return 0xffffffffu;
  294. }
  295. R.io_errs = 0;
  296. return le32(R.io);
  297. }
  298. static void wr(unsigned int a, unsigned int v, unsigned int len)
  299. {
  300. int n;
  301. if (R.fatal)
  302. return;
  303. put32(R.io, v);
  304. n = usb_control_out_data(R.ctrl, R.addr, R.mps, R.low, 0x40, 0x05,
  305. (unsigned short)a, 0, R.io, len);
  306. if (n < 0)
  307. io_fail();
  308. else
  309. R.io_errs = 0;
  310. }
  311. static unsigned int rd8(unsigned int a) { return rd(a, 1) & 0xff; }
  312. static unsigned int rd16(unsigned int a) { return rd(a, 2) & 0xffff; }
  313. static unsigned int rd32(unsigned int a) { return rd(a, 4); }
  314. static void wr8(unsigned int a, unsigned int v) { wr(a, v & 0xff, 1); }
  315. static void wr16(unsigned int a, unsigned int v) { wr(a, v & 0xffff, 2); }
  316. static void wr32(unsigned int a, unsigned int v) { wr(a, v, 4); }
  317. static void set8(unsigned int a, unsigned int b) { wr8(a, rd8(a) | b); }
  318. static void clr8(unsigned int a, unsigned int b) { wr8(a, rd8(a) & ~b); }
  319. static void set16(unsigned int a, unsigned int b) { wr16(a, rd16(a) | b); }
  320. static void clr16(unsigned int a, unsigned int b) { wr16(a, rd16(a) & ~b); }
  321. static void set32(unsigned int a, unsigned int b) { wr32(a, rd32(a) | b); }
  322. static void wr32m(unsigned int a, unsigned int mask, unsigned int v)
  323. {
  324. unsigned int s = ffs32(mask);
  325. wr32(a, (rd32(a) & ~mask) | ((v << s) & mask));
  326. }
  327. static unsigned int rd32m(unsigned int a, unsigned int mask)
  328. {
  329. return (rd32(a) & mask) >> ffs32(mask);
  330. }
  331. /* Bulk write of a block (firmware download), 196 bytes per request. */
  332. static int wr_block(unsigned int a, const unsigned char *p, unsigned int n)
  333. {
  334. if (R.fatal)
  335. return -1;
  336. if (usb_control_out_data(R.ctrl, R.addr, R.mps, R.low, 0x40, 0x05,
  337. (unsigned short)a, 0, p, n) < 0) {
  338. io_fail();
  339. return -1;
  340. }
  341. return 0;
  342. }
  343. /* ---- RF access (rtw88xxa_phy_read_rf / rtw_phy_write_rf_reg_sipi) ------ */
  344. #define RFREG_MASK 0xfffffu
  345. static unsigned int rf_read(unsigned int addr, unsigned int mask)
  346. {
  347. unsigned int pi;
  348. addr &= 0xff;
  349. pi = rd32m(REG_3WIRE_SWA, 0x4);
  350. wr32m(REG_HSSI_READ, 0xff, addr);
  351. udelay(20);
  352. return rd32m(pi ? REG_PI_READ_A : REG_SI_READ_A, mask);
  353. }
  354. static void rf_write(unsigned int addr, unsigned int mask, unsigned int data)
  355. {
  356. addr &= 0xff;
  357. mask &= RFREG_MASK;
  358. if (mask != RFREG_MASK)
  359. data = (rf_read(addr, RFREG_MASK) & ~mask) |
  360. ((data << ffs32(mask)) & mask);
  361. wr32(REG_LSSI_WRITE_A, ((addr << 20) | (data & 0xfffff)) & 0x0fffffff);
  362. udelay(13);
  363. }
  364. /* ---- table blob ---------------------------------------------------------- */
  365. static const unsigned char *tbl_section(unsigned int id, unsigned int *len)
  366. {
  367. unsigned int off = 12, n, i, sid, slen;
  368. if (!R.tbl)
  369. return 0;
  370. n = le32(R.tbl + 8);
  371. for (i = 0; i < n && off + 8 <= R.tbl_size; i++) {
  372. sid = le32(R.tbl + off);
  373. slen = le32(R.tbl + off + 4);
  374. if (off + 8 + slen > R.tbl_size)
  375. return 0;
  376. if (sid == id) {
  377. *len = slen;
  378. return R.tbl + off + 8;
  379. }
  380. off += 8 + slen;
  381. }
  382. return 0;
  383. }
  384. static int load_blobs(void)
  385. {
  386. unsigned int size;
  387. const unsigned char *t = ramfs_lookup(TBL_PATH, &size);
  388. if (!t || size < 12 || le32(t) != RTL_TBL_MAGIC ||
  389. le32(t + 4) != RTL_TBL_VERSION) {
  390. R.state = "missing /ramfs/" TBL_PATH;
  391. return -1;
  392. }
  393. R.tbl = t;
  394. R.tbl_size = size;
  395. return 0;
  396. }
  397. /* rtw_pwr_seq_parser(): USB interface mask, cut mask from the chip. */
  398. static int pwr_seq(unsigned int id)
  399. {
  400. unsigned int len, i, cut_mask = 1u << (R.cut + 1);
  401. const unsigned char *p = tbl_section(id, &len);
  402. if (!p)
  403. return -1;
  404. for (i = 0; i + 8 <= len; i += 8) {
  405. unsigned int off = p[i] | (p[i + 1] << 8);
  406. unsigned int cm = p[i + 2], im = p[i + 3], cmd = p[i + 5];
  407. unsigned int mask = p[i + 6], val = p[i + 7], t;
  408. if (cmd == RTL_PWR_CMD_END)
  409. break;
  410. if (!(im & 0x02) || !(cm & cut_mask))
  411. continue;
  412. switch (cmd) {
  413. case RTL_PWR_CMD_WRITE:
  414. wr8(off, (rd8(off) & ~mask) | (val & mask));
  415. break;
  416. case RTL_PWR_CMD_POLLING:
  417. for (t = 0; t < 5000; t++) {
  418. if ((rd8(off) & mask) == (val & mask))
  419. break;
  420. udelay(50);
  421. }
  422. if (t == 5000) {
  423. loghex("rtl8821au: power poll timeout at ", off);
  424. return -1;
  425. }
  426. break;
  427. case RTL_PWR_CMD_DELAY:
  428. if (val == RTL_PWR_DELAY_US)
  429. udelay(off);
  430. else
  431. mdelay(off);
  432. break;
  433. default:
  434. break;
  435. }
  436. if (R.fatal)
  437. return -1;
  438. }
  439. return 0;
  440. }
  441. /* rtw_parse_tbl_phy_cond() + check_positive() for the 8812A/8821A rules. */
  442. static int cond_ok(unsigned int c, unsigned int c2)
  443. {
  444. unsigned int cut = (c >> 24) & 0xf, pkg = (c >> 12) & 0xf;
  445. unsigned int intf = (c >> 8) & 0xf, rfe = c & 0xff;
  446. unsigned int drv_cut = R.cut ? R.cut : 15;
  447. (void)c2; /* 8821A: glna/gpa/alna/apa types are all 0 */
  448. if (cut && cut != drv_cut)
  449. return 0;
  450. if (pkg && pkg != 15)
  451. return 0;
  452. if (intf && intf != 0x2) /* INTF_USB */
  453. return 0;
  454. if (!(rfe & 0x0f))
  455. return 1;
  456. if ((rfe & R.rfe_cond) != rfe)
  457. return 0;
  458. /* Any type check against the all-zero driver types fails unless the
  459. * table also asks for type 0. */
  460. if ((rfe & 1) && (c2 & 0xff))
  461. return 0;
  462. if ((rfe & 2) && ((c2 >> 8) & 0xff))
  463. return 0;
  464. if ((rfe & 4) && ((c2 >> 16) & 0xff))
  465. return 0;
  466. if ((rfe & 8) && ((c2 >> 24) & 0xff))
  467. return 0;
  468. return 1;
  469. }
  470. static void do_cfg(unsigned int id, unsigned int a, unsigned int v)
  471. {
  472. switch (id) {
  473. case RTL_TBL_MAC:
  474. wr8(a, v);
  475. break;
  476. case RTL_TBL_AGC:
  477. wr32(a, v);
  478. break;
  479. case RTL_TBL_BB:
  480. if (a == 0xfe)
  481. mdelay(50);
  482. else if (a == 0xfd)
  483. mdelay(5);
  484. else if (a == 0xfc)
  485. mdelay(1);
  486. else if (a == 0xfb)
  487. udelay(60);
  488. else if (a == 0xfa)
  489. udelay(5);
  490. else if (a == 0xf9)
  491. udelay(1);
  492. else
  493. wr32(a, v);
  494. break;
  495. case RTL_TBL_RF_A:
  496. if (a == 0xffe)
  497. mdelay(50);
  498. else if (a == 0xfe)
  499. udelay(110);
  500. else {
  501. rf_write(a, RFREG_MASK, v);
  502. udelay(1);
  503. }
  504. break;
  505. }
  506. }
  507. static int load_table(unsigned int id)
  508. {
  509. unsigned int len, i, pos_c = 0, pos_c2 = 0;
  510. int matched = 1, skipped = 0;
  511. const unsigned char *p = tbl_section(id, &len);
  512. if (!p)
  513. return -1;
  514. for (i = 0; i + 8 <= len; i += 8) {
  515. unsigned int a = le32(p + i), v = le32(p + i + 4);
  516. if (a & BIT(31)) { /* pos: branch marker */
  517. switch ((a >> 28) & 3) {
  518. case 3: /* ENDIF */
  519. matched = 1;
  520. skipped = 0;
  521. break;
  522. case 2: /* ELSE */
  523. matched = !skipped;
  524. break;
  525. default: /* IF / ELIF */
  526. pos_c = a;
  527. pos_c2 = v;
  528. break;
  529. }
  530. } else if (a & BIT(30)) { /* neg: evaluate */
  531. if (!skipped) {
  532. if (cond_ok(pos_c, pos_c2)) {
  533. matched = 1;
  534. skipped = 1;
  535. } else {
  536. matched = 0;
  537. skipped = 0;
  538. }
  539. } else {
  540. matched = 0;
  541. }
  542. } else if (matched) {
  543. do_cfg(id, a, v);
  544. }
  545. if (R.fatal)
  546. return -1;
  547. }
  548. return 0;
  549. }
  550. /* ---- eFuse (rtw_dump_physical_efuse_map + logical map) ------------------ */
  551. static int read_efuse(void)
  552. {
  553. unsigned char *phy = R.rxbuf; /* scratch: 512 bytes */
  554. unsigned char *log_map = R.efuse;
  555. unsigned int addr, ctl, t, p, i;
  556. wr8(REG_EFUSE_ACCESS, 0x69);
  557. set16(REG_SYS_FUNC_EN, BIT(12));
  558. set16(REG_SYS_CLKR, BIT(5) | BIT(1));
  559. wr32m(REG_LDO_EFUSE_CTRL, BIT(8) | BIT(9), 0);
  560. ctl = rd32(REG_EFUSE_CTRL);
  561. for (addr = 0; addr < 512; addr++) {
  562. ctl &= ~(0xffu | (0x3ffu << 8));
  563. ctl |= addr << 8;
  564. wr32(REG_EFUSE_CTRL, ctl & ~BIT(31));
  565. for (t = 0; t < 1000; t++) {
  566. ctl = rd32(REG_EFUSE_CTRL);
  567. if (ctl & BIT(31))
  568. break;
  569. udelay(1);
  570. }
  571. if (t == 1000 || R.fatal) {
  572. wr8(REG_EFUSE_ACCESS, 0);
  573. return -1;
  574. }
  575. phy[addr] = ctl & 0xff;
  576. }
  577. wr8(REG_EFUSE_ACCESS, 0);
  578. for (i = 0; i < 512; i++)
  579. log_map[i] = 0xff;
  580. for (p = 0; p < 512;) {
  581. unsigned int h1 = phy[p], h2 = p + 1 < 512 ? phy[p + 1] : 0xff;
  582. unsigned int blk, wen, w;
  583. if (h1 == 0xff || ((h1 & 0x1f) == 0xf && h2 == 0xff))
  584. break;
  585. if ((h1 & 0x1f) == 0xf) {
  586. blk = ((h2 & 0xf0) >> 1) | ((h1 >> 5) & 7);
  587. wen = h2 & 0xf;
  588. p += 2;
  589. } else {
  590. blk = (h1 & 0xf0) >> 4;
  591. wen = h1 & 0xf;
  592. p += 1;
  593. }
  594. for (w = 0; w < 4; w++) {
  595. unsigned int li = (blk << 3) + (w << 1);
  596. if (wen & (1u << w))
  597. continue;
  598. if (p + 1 >= 512 || li + 1 >= 512)
  599. return -1;
  600. log_map[li] = phy[p];
  601. log_map[li + 1] = phy[p + 1];
  602. p += 2;
  603. }
  604. }
  605. return 0;
  606. }
  607. /* rtw88xxa_read_efuse() + rtw_chip_efuse_info_setup() fixups. */
  608. static void parse_efuse(void)
  609. {
  610. const unsigned char *m = R.efuse;
  611. unsigned int pa = m[0xbc], lna2 = m[0xbd], lna5 = m[0xbf];
  612. unsigned int ext_lna_2g, ext_pa_2g, ext_lna_5g, ext_pa_5g, btcoex;
  613. rmemcpy(R.mac, m + 0x107, 6);
  614. R.crystal = m[0xb9] == 0xff ? 0x20 : m[0xb9];
  615. R.bb_swing_2g = m[0xc6] == 0xff ? 0 : m[0xc6];
  616. R.bb_swing_5g = m[0xc7] == 0xff ? 0 : m[0xc7];
  617. if (pa == 0xff)
  618. pa = 0;
  619. if (lna2 == 0xff)
  620. lna2 = 0;
  621. if (lna5 == 0xff)
  622. lna5 = 0;
  623. ext_pa_2g = (pa & BIT(4)) ? 1 : 0;
  624. ext_lna_2g = (lna2 & BIT(3)) ? 1 : 0;
  625. ext_pa_5g = (pa & BIT(0)) ? 1 : 0;
  626. ext_lna_5g = (lna5 & BIT(3)) ? 1 : 0;
  627. btcoex = m[0xc1] != 0xff && (m[0xc1] & 0xe0) == 0x20;
  628. R.rfe_cond = ext_lna_2g | (ext_pa_2g << 1) | (ext_lna_5g << 2) |
  629. (ext_pa_5g << 3) | (btcoex << 4);
  630. if (R.mac[0] & 1 || (R.mac[0] | R.mac[1] | R.mac[2] | R.mac[3] |
  631. R.mac[4] | R.mac[5]) == 0) {
  632. /* No valid address programmed: a fixed locally administered
  633. * one, so the device is still usable. */
  634. static const unsigned char fallback[6] = {
  635. 0x02, 0x4e, 0x45, 0x42, 0x08, 0x21 };
  636. rmemcpy(R.mac, fallback, 6);
  637. }
  638. }
  639. /* ---- power / MAC init ---------------------------------------------------- */
  640. /* rtw88xxa_reset_8051() */
  641. static void reset_8051(void)
  642. {
  643. unsigned int v;
  644. clr8(REG_RSV_CTRL, BIT(1));
  645. clr8(REG_RSV_CTRL + 1, BIT(0));
  646. v = rd8(REG_SYS_FUNC_EN + 1);
  647. wr8(REG_SYS_FUNC_EN + 1, v & ~BIT(2));
  648. clr8(REG_RSV_CTRL, BIT(1));
  649. set8(REG_RSV_CTRL + 1, BIT(0));
  650. wr8(REG_SYS_FUNC_EN + 1, v | BIT(2));
  651. }
  652. /* rtw88xxau_hw_reset(): undo a previous run that was not shut down. */
  653. static void hw_reset(void)
  654. {
  655. unsigned int v, t;
  656. if (!(rd8(REG_MCUFW_CTRL) & BIT(7)))
  657. return;
  658. reset_8051();
  659. wr8(REG_MCUFW_CTRL, 0);
  660. set32(REG_FPGA0_XCD_RF_PARA, BIT(6));
  661. clr8(REG_SYS_FUNC_EN, BIT(0) | BIT(1));
  662. wr8(REG_RF_CTRL, 0);
  663. wr16(REG_CR, 0);
  664. set8(REG_APS_FSMCO + 1, BIT(1));
  665. for (t = 0; t < 5000; t++) {
  666. v = rd8(REG_APS_FSMCO + 1);
  667. if (!(v & BIT(1)))
  668. break;
  669. udelay(1);
  670. }
  671. wr8(REG_APS_FSMCO + 1, v | BIT(0));
  672. clr8(REG_SYS_FUNC_EN + 1, BIT(4) | BIT(7));
  673. set8(REG_SYS_FUNC_EN + 1, BIT(4) | BIT(7));
  674. }
  675. static int llt_write(unsigned int a, unsigned int d)
  676. {
  677. unsigned int n;
  678. wr32(REG_LLT_INIT, BIT(30) | (a << 8) | d);
  679. for (n = 0; n <= 20; n++)
  680. if (!(rd32(REG_LLT_INIT) & (BIT(31) | BIT(30))))
  681. return 0;
  682. return -1;
  683. }
  684. /* rtw88xxa_llt_init(): reserved pages at the top of the 256-page TX FIFO. */
  685. static int llt_init(unsigned int boundary)
  686. {
  687. unsigned int i;
  688. for (i = 0; i < boundary - 1; i++)
  689. if (llt_write(i, i + 1) < 0)
  690. return -1;
  691. if (llt_write(boundary - 1, 0xff) < 0)
  692. return -1;
  693. for (i = boundary; i < 255; i++)
  694. if (llt_write(i, i + 1) < 0)
  695. return -1;
  696. return llt_write(255, boundary);
  697. }
  698. static void cpu_enable(int on)
  699. {
  700. if (on) {
  701. set8(REG_RSV_CTRL + 1, BIT(0));
  702. set8(REG_SYS_FUNC_EN + 1, BIT(2));
  703. } else {
  704. clr8(REG_SYS_FUNC_EN + 1, BIT(2));
  705. clr8(REG_RSV_CTRL + 1, BIT(0));
  706. }
  707. }
  708. /* __rtw_download_firmware_legacy() with rtw_usb_write_firmware_page(). */
  709. static int download_fw(void)
  710. {
  711. unsigned int size, page, off, n, t, v;
  712. const unsigned char *fw = ramfs_lookup(FW_PATH, &size);
  713. if (!fw || size < 64) {
  714. R.state = "missing /ramfs/" FW_PATH;
  715. return -1;
  716. }
  717. R.fw_ver = fw[4] | (fw[5] << 8);
  718. R.fw_sub = fw[6];
  719. fw += 32; /* rtw_fw_hdr_legacy */
  720. size -= 32;
  721. cpu_enable(0);
  722. cpu_enable(1);
  723. set8(REG_MCUFW_CTRL, BIT(0)); /* MCUFWDL_EN */
  724. for (t = 0; t < 10 && !(rd8(REG_MCUFW_CTRL) & BIT(0)); t++) {
  725. set8(REG_MCUFW_CTRL, BIT(0));
  726. mdelay(20);
  727. }
  728. wr32(REG_MCUFW_CTRL, rd32(REG_MCUFW_CTRL) & ~BIT(19)); /* ROM_DLEN */
  729. set8(REG_MCUFW_CTRL, BIT(2)); /* clear FWDL_CHK_RPT */
  730. for (page = 0; page * 4096 < size; page++) {
  731. unsigned int psz = size - page * 4096;
  732. if (psz > 4096)
  733. psz = 4096;
  734. wr32m(REG_MCUFW_CTRL, 0x70000, page);
  735. for (off = 0; off < psz; off += n) {
  736. n = psz - off >= 196 ? 196 : (psz - off >= 8 ? 8 : 1);
  737. if (wr_block(0x1000 + off, fw + page * 4096 + off,
  738. n) < 0)
  739. return -1;
  740. }
  741. }
  742. for (t = 0; t < 1000; t++) {
  743. if (rd8(REG_MCUFW_CTRL) & BIT(2))
  744. break;
  745. udelay(50);
  746. }
  747. clr8(REG_MCUFW_CTRL, BIT(0));
  748. if (t == 1000) {
  749. R.state = "firmware download not acknowledged";
  750. return -1;
  751. }
  752. /* download_firmware_validate_legacy() */
  753. v = rd32(REG_MCUFW_CTRL);
  754. v |= BIT(1);
  755. v &= ~BIT(6);
  756. wr32(REG_MCUFW_CTRL, v);
  757. cpu_enable(0);
  758. cpu_enable(1);
  759. for (t = 0; t < 20; t++) {
  760. if ((rd32(REG_MCUFW_CTRL) & FW_READY_LEGACY) == FW_READY_LEGACY)
  761. return 0;
  762. mdelay(20);
  763. }
  764. R.state = "firmware did not start";
  765. return -1;
  766. }
  767. /* ---- channel / band (rtw88xxa_set_channel and helpers) ------------------- */
  768. /* rtw88xxa_get_bb_swing(): two bits per path in the eFuse byte. */
  769. static unsigned int bb_swing(int band5, int path_b)
  770. {
  771. static const unsigned int s2s[4] = { 0x200, 0x16a, 0x101, 0x0b6 };
  772. unsigned int v = band5 ? R.bb_swing_5g : R.bb_swing_2g;
  773. return s2s[(path_b ? v >> 2 : v) & 3];
  774. }
  775. static void switch_band(int band5)
  776. {
  777. unsigned int t;
  778. /* rtw8821a_set_ext_band_switch(): no BT coex, no antenna diversity */
  779. wr32m(REG_LED_CFG, BIT(23), 0);
  780. wr32m(REG_LED_CFG, BIT(24), 1);
  781. wr32m(REG_RFE_INV_A, 0xf, 7);
  782. wr32m(REG_RFE_INV_A, 0xf0, 7);
  783. wr32m(REG_RFE_INV_A, BIT(29) | BIT(28), band5 ? 2 : 1);
  784. if (!band5) {
  785. set32(REG_RXPSEL, BIT(28) | BIT(29));
  786. /* rtw8821a_phy_set_rfe_reg_24g() */
  787. wr32m(REG_RFE_PINMUX_A, 0xf000, 0x7);
  788. wr32m(REG_RFE_PINMUX_A, 0xf0, 0x7);
  789. if (R.rfe_cond & 1) {
  790. wr32m(REG_RFE_INV_A, BIT(20), 1);
  791. wr32m(REG_RFE_INV_A, BIT(22), 0);
  792. wr32m(REG_RFE_PINMUX_A, 0x7, 0x2);
  793. wr32m(REG_RFE_PINMUX_A, 0x700, 0x2);
  794. } else {
  795. wr32m(REG_RFE_INV_A, BIT(20), 0);
  796. wr32m(REG_RFE_INV_A, BIT(22), 0);
  797. wr32m(REG_RFE_PINMUX_A, 0x7, 0x7);
  798. wr32m(REG_RFE_PINMUX_A, 0x700, 0x7);
  799. }
  800. wr32m(REG_TXSCALE_A, 0xf00, 0);
  801. wr32m(REG_TXPSEL, 0xf0, 1);
  802. wr32m(REG_CCK_RX, 0x0f000000, 1);
  803. wr32m(REG_RRSR, 0xfffff, 0x15f); /* 1/2/5.5/11/6/12/24 */
  804. clr8(REG_CCK_CHECK, BIT(7));
  805. } else {
  806. /* rtw8821a_phy_set_rfe_reg_5g() */
  807. wr32m(REG_RFE_PINMUX_A, 0xf000, 0x5);
  808. wr32m(REG_RFE_PINMUX_A, 0xf0, 0x4);
  809. wr32m(REG_RFE_INV_A, BIT(20), 0);
  810. wr32m(REG_RFE_INV_A, BIT(22), 0);
  811. wr32m(REG_RFE_PINMUX_A, 0x7, 0x7);
  812. wr32m(REG_RFE_PINMUX_A, 0x700, 0x7);
  813. set8(REG_CCK_CHECK, BIT(7));
  814. for (t = 0; t < 50; t++) {
  815. if ((rd16(REG_TXPKT_EMPTY) & 0x30) == 0x30)
  816. break;
  817. udelay(50);
  818. }
  819. set32(REG_RXPSEL, BIT(28) | BIT(29));
  820. wr32m(REG_TXSCALE_A, 0xf00, 1);
  821. wr32m(REG_TXPSEL, 0xf0, 0);
  822. wr32m(REG_CCK_RX, 0x0f000000, 0xf);
  823. wr32m(REG_RRSR, 0xfffff, 0x150); /* 6/12/24 */
  824. }
  825. /* rtw88xxa_set_channel_bb_swing() */
  826. wr32m(REG_TXSCALE_A, 0xffe00000u, bb_swing(band5, 0));
  827. wr32m(REG_TXSCALE_B, 0xffe00000u, bb_swing(band5, 1));
  828. R.band5 = band5;
  829. }
  830. /* Channel group of the eFuse power tables (rtw_get_channel_group). */
  831. static unsigned int chan_group(unsigned int ch)
  832. {
  833. if (ch <= 14)
  834. return ch <= 2 ? 0 : ch <= 5 ? 1 : ch <= 8 ? 2 :
  835. ch <= 11 ? 3 : ch <= 13 ? 4 : 5;
  836. if (ch <= 42) return 0;
  837. if (ch <= 50) return 1;
  838. if (ch <= 58) return 2;
  839. if (ch <= 64) return 3;
  840. if (ch <= 106) return 4;
  841. if (ch <= 114) return 5;
  842. if (ch <= 122) return 6;
  843. if (ch <= 130) return 7;
  844. if (ch <= 138) return 8;
  845. if (ch <= 144) return 9;
  846. if (ch <= 155) return 10;
  847. if (ch <= 161) return 11;
  848. if (ch <= 171) return 12;
  849. return 13;
  850. }
  851. static int s4(unsigned int nib)
  852. {
  853. nib &= 0xf;
  854. return nib & 8 ? (int)nib - 16 : (int)nib;
  855. }
  856. static unsigned int clamp_idx(int v)
  857. {
  858. return v < 0 ? 0 : v > 0x3f ? 0x3f : (unsigned int)v;
  859. }
  860. /* TX power: the path-A eFuse base indices (rtw_phy_get_2g/5g_tx_power_index
  861. * without the by-rate and regulatory-limit tables), written to the TX AGC
  862. * registers for CCK, OFDM and HT MCS0-7. */
  863. static void set_tx_power(unsigned int ch)
  864. {
  865. const unsigned char *p = R.efuse + 0x10; /* txpwr_idx_table[A] */
  866. unsigned int g = chan_group(ch), cck, ofdm, ht, v;
  867. int base, train, i;
  868. unsigned int word;
  869. if (ch <= 14) {
  870. cck = p[g < 6 ? g : 5];
  871. base = p[6 + (g < 5 ? g : 4)];
  872. if (cck == 0xff)
  873. cck = 0x20;
  874. if (base == 0xff) {
  875. base = 0x20;
  876. ofdm = ht = 0x20;
  877. } else {
  878. ofdm = clamp_idx(base + s4(p[11]));
  879. ht = clamp_idx(base + s4(p[11] >> 4));
  880. }
  881. } else {
  882. base = p[18 + g];
  883. cck = 0;
  884. if (base == 0xff) {
  885. ofdm = ht = 0x20;
  886. } else {
  887. ofdm = clamp_idx(base + s4(p[32]));
  888. ht = clamp_idx(base + s4(p[32] >> 4));
  889. }
  890. }
  891. cck = clamp_idx((int)cck);
  892. if (ch <= 14)
  893. wr32(REG_TXAGC_A_CCK, cck * 0x01010101u);
  894. v = ofdm * 0x01010101u;
  895. wr32(REG_TXAGC_A_CCK + 0x4, v);
  896. wr32(REG_TXAGC_A_CCK + 0x8, v);
  897. v = ht * 0x01010101u;
  898. wr32(REG_TXAGC_A_CCK + 0xc, v);
  899. wr32(REG_TXAGC_A_CCK + 0x10, v);
  900. /* rtw88xxa_tx_power_training() */
  901. word = 0;
  902. train = (int)ht;
  903. for (i = 0; i < 3; i++) {
  904. train -= i == 0 ? 10 : i == 1 ? 8 : 6;
  905. word |= (unsigned int)(train < 2 ? 2 : train) << (i * 8);
  906. }
  907. wr32m(REG_TX_PWR_TRAINING_A, 0xffffff, word);
  908. }
  909. static void set_channel(unsigned int ch)
  910. {
  911. unsigned int fc, mod;
  912. int band5 = ch > 14;
  913. if (ch == R.chan)
  914. return;
  915. if (band5 != R.band5 || !R.chan)
  916. switch_band(band5);
  917. /* rtw88xxa_switch_channel() */
  918. if (ch >= 36 && ch <= 48)
  919. fc = 0x494;
  920. else if (ch >= 50 && ch <= 64)
  921. fc = 0x453;
  922. else if (ch >= 100 && ch <= 116)
  923. fc = 0x452;
  924. else if (ch >= 118)
  925. fc = 0x412;
  926. else
  927. fc = 0x96a;
  928. wr32m(REG_CLKTRK, 0x1ffe0000, fc);
  929. if (ch >= 36 && ch <= 64)
  930. mod = 0x101;
  931. else if (ch >= 100 && ch <= 140)
  932. mod = 0x301;
  933. else if (ch > 140)
  934. mod = 0x501;
  935. else
  936. mod = 0;
  937. rf_write(0x18, BIT(18) | BIT(17) | BIT(16) | BIT(9) | BIT(8), mod);
  938. rf_write(0x18, 0xff, ch);
  939. /* rtw88xxa_post_set_bw_mode(): 20 MHz, 1T1R */
  940. clr16(REG_WMAC_TRXPTCL_CTL, BIT(7) | BIT(8));
  941. wr8(REG_DATA_SC, 0);
  942. wr32m(REG_ADCCLK, 0x003003C3, 0x00300200);
  943. wr32m(REG_ADC160, BIT(30), 0);
  944. wr32m(REG_L1PKTH, 0x03C00000, 8);
  945. /* rtw88xxa_set_channel_rf(): RF 0x18 BW bits = 20 MHz */
  946. rf_write(0x18, BIT(11) | BIT(10), 3);
  947. set_tx_power(ch);
  948. R.chan = ch;
  949. }
  950. /* ---- power on (rtw88xxa_power_on) ---------------------------------------- */
  951. static int power_on(void)
  952. {
  953. /* page_table_8821a / rqpn: everything on one DMA queue (LOW with 3-4
  954. * bulk-OUT endpoints, NORMAL with 2). */
  955. unsigned int hq = 8, nq = 0, lq = 0, exq = 0, pub, boundary = 248;
  956. unsigned int dma = R.n_out >= 3 ? DMA_LOW : DMA_NORMAL, v;
  957. if (R.n_out >= 3)
  958. lq = 8;
  959. if (R.n_out >= 4)
  960. exq = 4;
  961. hw_reset();
  962. /* rtw88xxau_init_power_on() */
  963. if (pwr_seq(RTL_TBL_PWR_ON) < 0) {
  964. R.state = "power-on sequence failed";
  965. return -1;
  966. }
  967. wr16(REG_CR, 0);
  968. set16(REG_CR, 0x063F); /* HCI/MAC TX+RX DMA, protocol, sched, sec, 32K */
  969. if (rd8(REG_SYS_CFG1 + 3) & BIT(0))
  970. set8(REG_LDO_SWR_CTRL, BIT(6));
  971. /* rtw_set_trx_fifo_info(): 256 pages, 8 reserved -> boundary 248 */
  972. if (llt_init(boundary) < 0) {
  973. R.state = "LLT init failed";
  974. return -1;
  975. }
  976. set32(REG_TXDMA_OFFSET_CHK, BIT(9));
  977. if (download_fw() < 0)
  978. return -1;
  979. wr8(REG_HMETFR, 0xf);
  980. if (load_table(RTL_TBL_MAC) < 0) {
  981. R.state = "MAC table failed";
  982. return -1;
  983. }
  984. /* rtw88xxau_init_queue_reserved_page() */
  985. pub = boundary - hq - lq - nq - exq - 1;
  986. wr32(REG_RQPN_NPQ, nq | (exq << 16));
  987. wr32(REG_RQPN, BIT(31) | hq | (lq << 8) | (pub << 16));
  988. /* rtw88xxau_init_tx_buffer_boundary() */
  989. wr8(REG_BCNQ_BDNY, boundary);
  990. wr8(REG_MGQ_BDNY, boundary);
  991. wr8(REG_WMAC_LBK_BF_HD, boundary);
  992. wr8(REG_TRXFF_BNDY, boundary);
  993. wr8(REG_DWBCN0_CTRL + 1, boundary);
  994. /* rtw88xxau_init_queue_priority(), all queues on `dma`; RX
  995. * aggregation (bit 2) off */
  996. v = rd16(REG_TXDMA_PQ_MAP) & 0x3;
  997. v |= (dma << 4) | (dma << 6) | (dma << 8) | (dma << 10) |
  998. (dma << 12) | (dma << 14);
  999. wr16(REG_TXDMA_PQ_MAP, v);
  1000. wr16(REG_TRXFF_BNDY + 2, 16128 - 128 - 1);
  1001. wr8(REG_RX_DRVINFO_SZ, 4);
  1002. wr32(REG_HIMR0, 0);
  1003. wr32(REG_HIMR1, 0);
  1004. wr32m(REG_CR, 0x30000, 0); /* net type: no link */
  1005. /* rtw88xxa_init_wmac_setting() */
  1006. wr16(REG_RXFLTMAP0, 0xffff);
  1007. wr16(REG_RXFLTMAP1, 0x0400);
  1008. wr16(REG_RXFLTMAP2, 0xffff);
  1009. wr32(REG_MAR, 0xffffffff);
  1010. wr32(REG_MAR + 4, 0xffffffff);
  1011. /* rtw88xxa_init_adaptive_ctrl() */
  1012. wr32m(REG_RRSR, 0xfffff, 0xffff1);
  1013. wr16(REG_RETRY_LIMIT, 0x3030);
  1014. /* rtw88xxa_init_edca() */
  1015. wr16(REG_SPEC_SIFS, 0x100a);
  1016. wr16(REG_MAC_SPEC_SIFS, 0x100a);
  1017. wr16(REG_SIFS, 0x100a);
  1018. wr16(REG_SIFS + 2, 0x100a);
  1019. wr32(REG_EDCA_BE_PARAM, 0x005EA42B);
  1020. wr32(REG_EDCA_BK_PARAM, 0x0000A44F);
  1021. wr32(REG_EDCA_VI_PARAM, 0x005EA324);
  1022. wr32(REG_EDCA_VO_PARAM, 0x002FA226);
  1023. wr8(REG_USTIME_TSF, 0x50);
  1024. wr8(REG_USTIME_EDCA, 0x50);
  1025. set8(REG_FWHW_TXQ_CTRL, BIT(7));
  1026. wr8(REG_ACKTO, 0x80);
  1027. /* rtw88xxau_tx_aggregation() */
  1028. wr32m(REG_DWBCN0_CTRL, 0xf0, 6);
  1029. wr8(REG_DWBCN1_CTRL, 6 << 1);
  1030. /* rtw88xxa_init_beacon_parameters() */
  1031. wr16(REG_BCN_CTRL, (BIT(4) << 8) | BIT(4));
  1032. wr32m(REG_TBTT_PROHIBIT, 0xfffff, 0x04 | (0x64 << 8));
  1033. wr8(REG_DRVERLYINT, 0x05);
  1034. wr8(REG_BCNDMATIM, 0x02);
  1035. wr16(REG_BCNTCFG, 0x4413);
  1036. wr8(REG_BCN_MAX_ERR, 0xff);
  1037. /* rtw_usb_init_burst_pkt_len(): high speed, 512-byte bursts */
  1038. wr8(REG_RXDMA_MODE, (3 << 2) | BIT(1) | (1 << 4));
  1039. set16(REG_TXDMA_OFFSET_CHK, BIT(9));
  1040. wr8(REG_USB3_RXITV, 0x01);
  1041. wr16(REG_RXDMA_STATUS, 0x7400);
  1042. wr8(REG_RXDMA_STATUS + 1, 0xf5);
  1043. wr8(REG_AMPDU_MAX_TIME, 0x5e);
  1044. wr32(REG_AMPDU_MAX_LENGTH, 0xffffffff);
  1045. wr8(REG_USTIME_TSF, 0x50);
  1046. wr8(REG_USTIME_EDCA, 0x50);
  1047. set8(REG_SINGLE_AMPDU_CTRL, BIT(7));
  1048. wr8(REG_RX_PKT_LIMIT, 0x18);
  1049. wr8(REG_PIFS, 0x00);
  1050. wr16(REG_MAX_AGGR_NUM, 0x1f1f);
  1051. wr8(REG_FWHW_TXQ_CTRL, 0x80);
  1052. wr32(REG_FAST_EDCA_CTRL, 0x03087777);
  1053. set8(REG_RSV_CTRL, BIT(5) | BIT(6));
  1054. wr32(REG_ARFR0, 0x00000010);
  1055. wr32(REG_ARFRH0, 0xfffff000);
  1056. wr32(REG_ARFR1_V1, 0x00000010);
  1057. wr32(REG_ARFRH1_V1, 0x003ff000);
  1058. wr32(REG_ARFR2_V1, 0x00000015);
  1059. wr32(REG_ARFRH2_V1, 0x003ff000);
  1060. wr32(REG_ARFR3_V1, 0x00000015);
  1061. wr32(REG_ARFRH3_V1, 0xffcff000);
  1062. set8(REG_CR, BIT(6) | BIT(7)); /* MACTXEN | MACRXEN */
  1063. /* rtw88xxa_phy_bb_config() */
  1064. v = rd8(REG_SYS_FUNC_EN) | BIT(2);
  1065. wr8(REG_SYS_FUNC_EN, v);
  1066. wr8(REG_SYS_FUNC_EN, v | BIT(0) | BIT(1));
  1067. wr8(REG_RF_CTRL, 7);
  1068. wr8(REG_RF_B_CTRL, 7);
  1069. if (load_table(RTL_TBL_BB) < 0 || load_table(RTL_TBL_AGC) < 0) {
  1070. R.state = "BB table failed";
  1071. return -1;
  1072. }
  1073. wr32m(REG_AFE_CTRL3, 0x00FFF000,
  1074. (R.crystal & 0x3f) | ((R.crystal & 0x3f) << 6));
  1075. /* rtw88xxa_phy_rf_config() */
  1076. if (load_table(RTL_TBL_RF_A) < 0) {
  1077. R.state = "RF table failed";
  1078. return -1;
  1079. }
  1080. R.chan = 0;
  1081. R.band5 = 0;
  1082. switch_band(0);
  1083. wr32(REG_SEC_CMD, BIT(31) | BIT(30)); /* clear the key CAM */
  1084. wr8(REG_HWSEQ_CTRL, 0xff);
  1085. wr32(REG_BAR_MODE_CTRL, 0x0201ffff);
  1086. wr8(REG_NAV_CTRL + 2, 0);
  1087. clr8(REG_GPIO_MUXCFG, BIT(5));
  1088. clr8(REG_QUEUE_CTRL, BIT(3));
  1089. wr8(REG_FWHW_TXQ_CTRL + 1, 0x0f);
  1090. wr8(REG_EARLY_MODE_CONTROL + 3, 0x01);
  1091. wr16(REG_TX_RPT_TIME, 0x3df0);
  1092. wr8(REG_SYS_SDIO_CTRL, 0);
  1093. wr8(REG_ACLK_MON, 0);
  1094. wr8(REG_USB_HRPWM, 0);
  1095. set32(REG_FWHW_TXQ_CTRL, BIT(12));
  1096. /* rtw_core_start(): RX filter; then our address */
  1097. wr32(REG_RCR, RCR_DEFAULT);
  1098. {
  1099. unsigned int i;
  1100. for (i = 0; i < 6; i++) {
  1101. wr8(REG_MACID + i, R.mac[i]);
  1102. wr8(REG_BSSID + i, 0);
  1103. }
  1104. }
  1105. set_channel(1);
  1106. return R.fatal ? -1 : 0;
  1107. }
  1108. /* rtw88xxa_power_off(), without BT coex. */
  1109. static void power_off(void)
  1110. {
  1111. unsigned int cr = rd8(REG_CR);
  1112. if (cr == 0 || cr == 0xea || R.fatal)
  1113. return;
  1114. wr32(REG_HIMR0, 0);
  1115. wr32(REG_HIMR1, 0);
  1116. wr8(REG_CR, 0);
  1117. pwr_seq(RTL_TBL_PWR_LPS);
  1118. if (rd8(REG_MCUFW_CTRL) & BIT(7))
  1119. reset_8051();
  1120. clr8(REG_SYS_FUNC_EN + 1, BIT(2));
  1121. wr8(REG_MCUFW_CTRL, 0);
  1122. pwr_seq(RTL_TBL_PWR_OFF);
  1123. }
  1124. /* ---- RX ------------------------------------------------------------------ */
  1125. static int cck_rx_pwr(unsigned int lna, unsigned int vga)
  1126. {
  1127. static const signed char lna_gain[] = { 15, -1, -17, 0, -30, -38 };
  1128. if (lna > 5 || lna == 3)
  1129. return -100;
  1130. return lna_gain[lna] - 2 * (int)vga;
  1131. }
  1132. static void rx_packet(const unsigned char *d, unsigned int avail)
  1133. {
  1134. unsigned int w0, len, drv, shift, rate, off;
  1135. const unsigned char *phy, *f;
  1136. int rssi = -100;
  1137. while (avail >= RXDESC_SZ) {
  1138. w0 = le32(d);
  1139. len = w0 & 0x3fff;
  1140. drv = ((w0 >> 16) & 0xf) * 8;
  1141. shift = (w0 >> 24) & 3;
  1142. rate = le32(d + 12) & 0x7f;
  1143. off = RXDESC_SZ + drv + shift;
  1144. if (!len || off + len > avail)
  1145. return;
  1146. phy = d + RXDESC_SZ + shift;
  1147. f = d + off;
  1148. if (le32(d + 8) & BIT(28)) {
  1149. /* C2H firmware event: not used. */
  1150. } else if (w0 & BIT(14)) {
  1151. R.rx_crc++;
  1152. } else if (len > 4 + 24) {
  1153. if (drv && (w0 & BIT(26))) {
  1154. if (rate <= 3)
  1155. rssi = cck_rx_pwr((le32(phy + 4) >> 13) & 7,
  1156. (le32(phy + 4) >> 8) & 0x1f);
  1157. else
  1158. rssi = (int)(phy[0] & 0x7f) - 110;
  1159. }
  1160. R.rx_pkts++;
  1161. R.last_rssi = rssi;
  1162. wifi_rx_frame(f, len - 4, rssi, 0, R.chan);
  1163. }
  1164. off = (off + len + 7) & ~7u;
  1165. if (off >= avail)
  1166. return;
  1167. d += off;
  1168. avail -= off;
  1169. }
  1170. }
  1171. /* Drain the RX FIFO: every completed bulk-IN transfer is parsed, then the
  1172. * next one is armed; stop after ~0.3 ms without data or 64 packets (the
  1173. * tick is frozen inside a syscall, so the bounds are counts). */
  1174. static int drain_rx(void)
  1175. {
  1176. unsigned int frames = 0, idle = 0;
  1177. int n;
  1178. while (frames < 64) {
  1179. n = usb_bulk_in(R.ctrl, R.addr, R.ep_in, R.ep_in_mps,
  1180. R.rxbuf, RX_BUF_SZ);
  1181. if (n < 0) {
  1182. io_fail();
  1183. return -1;
  1184. }
  1185. if (n == 0) {
  1186. if (++idle > 6)
  1187. break;
  1188. udelay(50);
  1189. continue;
  1190. }
  1191. idle = 0;
  1192. frames++;
  1193. rx_packet(R.rxbuf, (unsigned int)n);
  1194. }
  1195. return 0;
  1196. }
  1197. /* ---- TX ------------------------------------------------------------------ */
  1198. static int tx_frame(const unsigned char *frame, unsigned int len, int mgmt,
  1199. int protect)
  1200. {
  1201. unsigned char *b = R.txbuf, *f;
  1202. unsigned int pad, rate, rate_id, qsel, i, sum, w;
  1203. if (!R.up || R.fatal)
  1204. return -1;
  1205. if (len + TXDESC_SZ + 8 + 8 > TX_BUF_SZ)
  1206. return -1;
  1207. if (protect && !R.have_tk)
  1208. return -1;
  1209. /* A transfer of an exact multiple of 512 bytes would need a
  1210. * zero-length packet; the vendor drivers shift the frame by 8 bytes
  1211. * (PKT_OFFSET 1) instead. */
  1212. pad = ((len + (protect ? 8 : 0) + TXDESC_SZ) % 512) == 0 ? 8 : 0;
  1213. f = b + TXDESC_SZ + pad;
  1214. rmemcpy(f, frame, len);
  1215. if (protect) {
  1216. if (ccmp_tx_encrypt(f, len, 24, R.tk) < 0)
  1217. return -1;
  1218. len += 8;
  1219. }
  1220. if (mgmt) {
  1221. rate = R.band5 ? RATE_6M : RATE_1M;
  1222. rate_id = R.band5 ? RATEID_G : RATEID_B;
  1223. qsel = QSEL_MGMT;
  1224. } else {
  1225. rate = RATE_12M;
  1226. rate_id = R.band5 ? RATEID_G : RATEID_BG;
  1227. qsel = QSEL_BE;
  1228. }
  1229. rmemzero(b, TXDESC_SZ + pad);
  1230. /* rtw_tx_fill_tx_desc() */
  1231. put32(b + 0, len | ((TXDESC_SZ + pad) << 16) |
  1232. ((f[4] & 1) ? BIT(24) : 0) | BIT(26) | BIT(31));
  1233. put32(b + 4, (qsel << 8) | (rate_id << 16) | ((pad ? 1 : 0) << 24));
  1234. put32(b + 12, BIT(8) | BIT(10)); /* USE_RATE, DISDATAFB */
  1235. put32(b + 16, rate | (0x1f << 8)); /* old_datarate_fb_limit */
  1236. put32(b + 28, 1u << 24); /* DMA_TXAGG_NUM 1 */
  1237. put32(b + 32, BIT(15)); /* EN_HWSEQ */
  1238. /* fill_txdesc_checksum_common(): XOR of the first 16 le16 words */
  1239. sum = 0;
  1240. for (i = 0; i < 32; i += 2)
  1241. sum ^= b[i] | (b[i + 1] << 8);
  1242. w = le32(b + 28) & ~0xffffu;
  1243. put32(b + 28, w | (sum & 0xffff));
  1244. if (usb_bulk_out(R.ctrl, R.addr, R.ep_tx, R.ep_out_mps, b,
  1245. TXDESC_SZ + pad + len) != 0) {
  1246. R.tx_errs++;
  1247. io_fail();
  1248. return -1;
  1249. }
  1250. R.tx_pkts++;
  1251. return 0;
  1252. }
  1253. static void send_probe_req(void)
  1254. {
  1255. static const unsigned char rates24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96,
  1256. 0x0c, 0x12, 0x18, 0x24,
  1257. 50, 4, 0x30, 0x48, 0x60, 0x6c };
  1258. static const unsigned char rates5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24,
  1259. 0xb0, 0x48, 0x60, 0x6c };
  1260. unsigned char f[64];
  1261. unsigned int o;
  1262. rmemzero(f, 24);
  1263. f[0] = 0x40; /* probe request */
  1264. rmemcpy(f + 10, R.mac, 6);
  1265. for (o = 0; o < 6; o++)
  1266. f[4 + o] = f[16 + o] = 0xff;
  1267. o = 24;
  1268. f[o++] = 0; /* wildcard SSID */
  1269. f[o++] = 0;
  1270. if (R.band5) {
  1271. rmemcpy(f + o, rates5, sizeof(rates5));
  1272. o += sizeof(rates5);
  1273. } else {
  1274. rmemcpy(f + o, rates24, sizeof(rates24));
  1275. o += sizeof(rates24);
  1276. }
  1277. tx_frame(f, o, 1, 0);
  1278. }
  1279. /* ---- scan ---------------------------------------------------------------- */
  1280. static int chan_active(unsigned int ch)
  1281. {
  1282. return ch <= 14 || (ch >= 36 && ch <= 48);
  1283. }
  1284. static void scan_step(void)
  1285. {
  1286. unsigned int ch;
  1287. if (R.scan_idx >= sizeof(scan_chans)) {
  1288. R.scanning = 0;
  1289. wifi_scan_done(1);
  1290. return;
  1291. }
  1292. ch = scan_chans[R.scan_idx++];
  1293. set_channel(ch);
  1294. if (chan_active(ch))
  1295. send_probe_req();
  1296. R.scan_next = get_ticks_ms() +
  1297. (chan_active(ch) ? DWELL_ACTIVE : DWELL_PASSIVE);
  1298. }
  1299. /* ---- public API ---------------------------------------------------------- */
  1300. static int check_present(void)
  1301. {
  1302. if (!R.present)
  1303. return 0;
  1304. if (R.port != 0xFFu && !usb_port_connected(R.ctrl, R.port)) {
  1305. R.present = 0;
  1306. R.fatal = 1;
  1307. return 0;
  1308. }
  1309. return 1;
  1310. }
  1311. int rtl8821au_present(void)
  1312. {
  1313. return check_present();
  1314. }
  1315. const char *rtl8821au_chip_name(void)
  1316. {
  1317. return "RTL8821AU USB";
  1318. }
  1319. const char *rtl8821au_fw_name(void)
  1320. {
  1321. return R.up ? R.fw_name : "";
  1322. }
  1323. int rtl8821au_fatal(void)
  1324. {
  1325. return R.up && (R.fatal || !check_present());
  1326. }
  1327. static int alloc_buffers(void)
  1328. {
  1329. unsigned int rx;
  1330. if (R.io)
  1331. return 0;
  1332. R.io = (unsigned char *)mm_alloc_page();
  1333. R.efuse = (unsigned char *)mm_alloc_page();
  1334. R.txbuf = (unsigned char *)mm_alloc_page();
  1335. rx = mm_alloc_pages(4);
  1336. if (!R.io || !R.efuse || !R.txbuf || !rx) {
  1337. R.io = 0;
  1338. return -1;
  1339. }
  1340. /* An 8 KB-aligned 8 KB window never crosses a 64 KB boundary (one
  1341. * xHCI TRB) and is contiguous for EHCI's qTD pages. */
  1342. R.rxbuf = (unsigned char *)((rx + 8191) & ~8191u);
  1343. return 0;
  1344. }
  1345. static void fmt_fw_name(void)
  1346. {
  1347. static const char pre[] = "rtw8821a ";
  1348. unsigned int i = 0, v, d = 10000;
  1349. int started = 0;
  1350. for (; pre[i]; i++)
  1351. R.fw_name[i] = pre[i];
  1352. v = R.fw_ver;
  1353. for (; d; d /= 10) {
  1354. if (v / d || started || d == 1) {
  1355. R.fw_name[i++] = (char)('0' + v / d);
  1356. started = 1;
  1357. }
  1358. v %= d;
  1359. }
  1360. R.fw_name[i++] = '.';
  1361. v = R.fw_sub;
  1362. if (v >= 10)
  1363. R.fw_name[i++] = (char)('0' + v / 10 % 10);
  1364. R.fw_name[i++] = (char)('0' + v % 10);
  1365. R.fw_name[i] = 0;
  1366. }
  1367. int rtl8821au_up(unsigned char mac[6])
  1368. {
  1369. if (!check_present()) {
  1370. R.state = "no RTL8821AU stick";
  1371. return -1;
  1372. }
  1373. if (R.up && !R.fatal) {
  1374. rmemcpy(mac, R.mac, 6);
  1375. return 0;
  1376. }
  1377. R.up = 0;
  1378. R.fatal = 0;
  1379. R.io_errs = 0;
  1380. R.scanning = 0;
  1381. R.have_tk = 0;
  1382. if (alloc_buffers() < 0) {
  1383. R.state = "out of memory";
  1384. return -1;
  1385. }
  1386. if (load_blobs() < 0)
  1387. return -1;
  1388. R.cut = (rd32(REG_SYS_CFG1) >> 12) & 0xf;
  1389. rlog("rtl8821au: power on, cut ");
  1390. serial_putdec(SERIAL_COM1, R.cut);
  1391. rlog("\n");
  1392. /* rtw_chip_efuse_info_setup(): power on the MAC, read the eFuse. */
  1393. wr8(REG_RSV_CTRL, 0);
  1394. wr8(REG_LDO_SWR_CTRL, (rd32(REG_SYS_CFG1) & BIT(24)) ? 0xC3 : 0x83);
  1395. if (pwr_seq(RTL_TBL_PWR_ON) < 0 || read_efuse() < 0) {
  1396. R.state = R.fatal ? "USB I/O failed" : "eFuse read failed";
  1397. return -1;
  1398. }
  1399. parse_efuse();
  1400. rlog("rtl8821au: eFuse ok, rfe ");
  1401. serial_puthex(SERIAL_COM1, R.rfe_cond);
  1402. rlog("\n");
  1403. power_off();
  1404. R.state = 0;
  1405. if (power_on() < 0) {
  1406. if (R.fatal)
  1407. R.state = "USB I/O failed";
  1408. else if (!R.state)
  1409. R.state = "power on failed";
  1410. loghex("rtl8821au: bring-up failed, MCUFW_CTRL ",
  1411. rd32(REG_MCUFW_CTRL));
  1412. rlog("rtl8821au: ");
  1413. rlog(R.state);
  1414. rlog("\n");
  1415. return -1;
  1416. }
  1417. fmt_fw_name();
  1418. R.up = 1;
  1419. R.state = "ready";
  1420. rmemcpy(mac, R.mac, 6);
  1421. rlog("rtl8821au: up, firmware ");
  1422. rlog(R.fw_name);
  1423. rlog("\n");
  1424. return 0;
  1425. }
  1426. void rtl8821au_down(void)
  1427. {
  1428. R.scanning = 0;
  1429. R.have_tk = 0;
  1430. if (R.up && check_present() && !R.fatal)
  1431. power_off();
  1432. R.up = 0;
  1433. R.chan = 0;
  1434. R.state = "down";
  1435. }
  1436. const char *rtl8821au_state(void)
  1437. {
  1438. return R.state ? R.state : "not started";
  1439. }
  1440. int rtl8821au_poll(void)
  1441. {
  1442. if (!R.up)
  1443. return -1;
  1444. if (R.fatal || !check_present())
  1445. return -1;
  1446. if (drain_rx() < 0)
  1447. return -1;
  1448. if (R.scanning && (int)(get_ticks_ms() - R.scan_next) >= 0)
  1449. scan_step();
  1450. return R.fatal ? -1 : 0;
  1451. }
  1452. int rtl8821au_scan_start(void)
  1453. {
  1454. if (!R.up || R.fatal)
  1455. return -1;
  1456. wr32(REG_RCR, RCR_DEFAULT);
  1457. R.scanning = 1;
  1458. R.scan_idx = 0;
  1459. scan_step();
  1460. return 0;
  1461. }
  1462. int rtl8821au_scan_running(void)
  1463. {
  1464. return R.scanning;
  1465. }
  1466. int rtl8821au_prepare_connect(const unsigned char bssid[6],
  1467. unsigned int chan, unsigned int beacon_int)
  1468. {
  1469. unsigned int i;
  1470. (void)beacon_int;
  1471. if (!R.up || R.fatal)
  1472. return -1;
  1473. R.scanning = 0;
  1474. R.have_tk = 0;
  1475. rmemcpy(R.bssid, bssid, 6);
  1476. set_channel(chan);
  1477. for (i = 0; i < 6; i++)
  1478. wr8(REG_BSSID + i, bssid[i]);
  1479. wr32(REG_RCR, RCR_DEFAULT);
  1480. drain_rx();
  1481. return R.fatal ? -1 : 0;
  1482. }
  1483. int rtl8821au_set_assoc(unsigned short aid, unsigned int beacon_int,
  1484. unsigned int dtim)
  1485. {
  1486. (void)beacon_int;
  1487. (void)dtim;
  1488. if (!R.up || R.fatal)
  1489. return -1;
  1490. wr32m(REG_AID, 0x7ff, aid);
  1491. wr32m(REG_CR, 0x30000, 2); /* RTW_NET_MGD_LINKED */
  1492. /* Only our BSS's data and beacons from here on. */
  1493. wr32(REG_RCR, RCR_DEFAULT | RCR_CBSSID_DATA | RCR_CBSSID_BCN);
  1494. return R.fatal ? -1 : 0;
  1495. }
  1496. void rtl8821au_disconnect(void)
  1497. {
  1498. unsigned int i;
  1499. R.have_tk = 0;
  1500. if (!R.up || R.fatal)
  1501. return;
  1502. wr32m(REG_CR, 0x30000, 0);
  1503. wr32m(REG_AID, 0x7ff, 0);
  1504. for (i = 0; i < 6; i++)
  1505. wr8(REG_BSSID + i, 0);
  1506. wr32(REG_RCR, RCR_DEFAULT);
  1507. }
  1508. int rtl8821au_tx_mgmt(const unsigned char *frame, unsigned int len)
  1509. {
  1510. return tx_frame(frame, len, 1, 0);
  1511. }
  1512. int rtl8821au_tx_data(const unsigned char *frame, unsigned int len,
  1513. int protect)
  1514. {
  1515. return tx_frame(frame, len, 0, protect);
  1516. }
  1517. int rtl8821au_set_key(int pairwise, int keyidx, const unsigned char key[16],
  1518. const unsigned char rsc[6])
  1519. {
  1520. (void)keyidx;
  1521. (void)rsc;
  1522. /* The group key is only used for RX, which wifi.c decrypts. */
  1523. if (pairwise) {
  1524. rmemcpy(R.tk, key, 16);
  1525. R.have_tk = 1;
  1526. }
  1527. return 0;
  1528. }
  1529. void rtl8821au_clear_keys(void)
  1530. {
  1531. rmemzero(R.tk, 16);
  1532. R.have_tk = 0;
  1533. }
  1534. /* ---- USB probe ----------------------------------------------------------- */
  1535. static int parse_config(void)
  1536. {
  1537. unsigned char *c = R.io;
  1538. unsigned int total, o, n_out = 0, in = 0, in_mps = 512, out_mps = 512;
  1539. int n;
  1540. n = usb_control_in(R.ctrl, R.addr, R.mps, R.low, 0x80, 0x06,
  1541. 0x0200, 0, c, 9);
  1542. if (n < 9)
  1543. return -1;
  1544. total = c[2] | (c[3] << 8);
  1545. if (total > 512)
  1546. total = 512;
  1547. n = usb_control_in(R.ctrl, R.addr, R.mps, R.low, 0x80, 0x06,
  1548. 0x0200, 0, c, total);
  1549. if (n < 9)
  1550. return -1;
  1551. total = (unsigned int)n;
  1552. for (o = 0; o + 2 <= total && c[o] >= 2; o += c[o]) {
  1553. if (c[o + 1] == 4 && o + 9 <= total && c[o + 2] != 0)
  1554. break; /* only interface 0 */
  1555. if (c[o + 1] != 5 || o + 7 > total)
  1556. continue;
  1557. if ((c[o + 3] & 3) != 2)
  1558. continue; /* bulk only */
  1559. if (c[o + 2] & 0x80) {
  1560. if (!in) {
  1561. in = c[o + 2] & 0x0f;
  1562. in_mps = c[o + 4] | (c[o + 5] << 8);
  1563. }
  1564. } else if (n_out < 4) {
  1565. R.ep_out[n_out++] = c[o + 2] & 0x0f;
  1566. out_mps = c[o + 4] | (c[o + 5] << 8);
  1567. }
  1568. }
  1569. if (!in || n_out < 2)
  1570. return -1;
  1571. R.ep_in = in;
  1572. R.ep_in_mps = in_mps ? in_mps : 512;
  1573. R.n_out = n_out;
  1574. R.ep_out_mps = out_mps ? out_mps : 512;
  1575. /* dma_mapping_to_ep(): HIGH 0, NORMAL 1, LOW 2, EXTRA 3 */
  1576. R.ep_tx = R.ep_out[n_out >= 3 ? 2 : 1];
  1577. if (usb_control_out(R.ctrl, R.addr, R.mps, R.low, 0x00, 0x09,
  1578. c[5], 0) < 0)
  1579. return -1;
  1580. return 0;
  1581. }
  1582. static int device_probe(unsigned int ctrl, unsigned int addr,
  1583. unsigned int mps, int low_speed, unsigned int port,
  1584. unsigned int vendor, unsigned int product)
  1585. {
  1586. unsigned int i;
  1587. for (i = 0; i < sizeof(usb_ids) / sizeof(usb_ids[0]); i++)
  1588. if (usb_ids[i][0] == vendor && usb_ids[i][1] == product)
  1589. break;
  1590. if (i == sizeof(usb_ids) / sizeof(usb_ids[0]))
  1591. return 0;
  1592. if (!usb_bulk_supported(ctrl)) {
  1593. rlog("rtl8821au: stick on a controller without bulk "
  1594. "transfers (UHCI/OHCI), ignored\n");
  1595. return 0;
  1596. }
  1597. if (alloc_buffers() < 0)
  1598. return 0;
  1599. /* A re-enumeration resets the chip: a running instance is dead. */
  1600. if (R.up)
  1601. R.fatal = 1;
  1602. R.ctrl = ctrl;
  1603. R.addr = addr;
  1604. R.mps = mps;
  1605. R.low = low_speed;
  1606. R.port = port;
  1607. R.vendor = vendor;
  1608. R.product = product;
  1609. R.gen = usb_generation();
  1610. R.present = 1;
  1611. R.io_errs = 0;
  1612. if (parse_config() < 0) {
  1613. rlog("rtl8821au: unexpected USB configuration\n");
  1614. R.present = 0;
  1615. return 0;
  1616. }
  1617. if (!R.up)
  1618. R.fatal = 0;
  1619. R.state = "firmware not loaded";
  1620. rlog("rtl8821au: found ");
  1621. serial_puthex(SERIAL_COM1, (vendor << 16) | product);
  1622. rlog(", bulk in ");
  1623. serial_putdec(SERIAL_COM1, R.ep_in);
  1624. rlog(", tx ep ");
  1625. serial_putdec(SERIAL_COM1, R.ep_tx);
  1626. rlog(" of ");
  1627. serial_putdec(SERIAL_COM1, R.n_out);
  1628. rlog("\n");
  1629. return 1;
  1630. }
  1631. void rtl8821au_register(void)
  1632. {
  1633. usb_register_device_probe(device_probe);
  1634. }