zd_chip.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. /* zd_chip.c
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. /* This file implements all the hardware specific functions for the ZD1211
  18. * and ZD1211B chips. Support for the ZD1211B was possible after Timothy
  19. * Legge sent me a ZD1211B device. Thank you Tim. -- Uli
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include "zd_def.h"
  24. #include "zd_chip.h"
  25. #include "zd_ieee80211.h"
  26. #include "zd_mac.h"
  27. #include "zd_rf.h"
  28. void zd_chip_init(struct zd_chip *chip,
  29. struct net_device *netdev,
  30. struct usb_interface *intf)
  31. {
  32. memset(chip, 0, sizeof(*chip));
  33. mutex_init(&chip->mutex);
  34. zd_usb_init(&chip->usb, netdev, intf);
  35. zd_rf_init(&chip->rf);
  36. }
  37. void zd_chip_clear(struct zd_chip *chip)
  38. {
  39. ZD_ASSERT(!mutex_is_locked(&chip->mutex));
  40. zd_usb_clear(&chip->usb);
  41. zd_rf_clear(&chip->rf);
  42. mutex_destroy(&chip->mutex);
  43. ZD_MEMCLEAR(chip, sizeof(*chip));
  44. }
  45. static int scnprint_mac_oui(struct zd_chip *chip, char *buffer, size_t size)
  46. {
  47. u8 *addr = zd_usb_to_netdev(&chip->usb)->dev_addr;
  48. return scnprintf(buffer, size, "%02x-%02x-%02x",
  49. addr[0], addr[1], addr[2]);
  50. }
  51. /* Prints an identifier line, which will support debugging. */
  52. static int scnprint_id(struct zd_chip *chip, char *buffer, size_t size)
  53. {
  54. int i = 0;
  55. i = scnprintf(buffer, size, "zd1211%s chip ",
  56. zd_chip_is_zd1211b(chip) ? "b" : "");
  57. i += zd_usb_scnprint_id(&chip->usb, buffer+i, size-i);
  58. i += scnprintf(buffer+i, size-i, " ");
  59. i += scnprint_mac_oui(chip, buffer+i, size-i);
  60. i += scnprintf(buffer+i, size-i, " ");
  61. i += zd_rf_scnprint_id(&chip->rf, buffer+i, size-i);
  62. i += scnprintf(buffer+i, size-i, " pa%1x %c%c%c%c%c", chip->pa_type,
  63. chip->patch_cck_gain ? 'g' : '-',
  64. chip->patch_cr157 ? '7' : '-',
  65. chip->patch_6m_band_edge ? '6' : '-',
  66. chip->new_phy_layout ? 'N' : '-',
  67. chip->al2230s_bit ? 'S' : '-');
  68. return i;
  69. }
  70. static void print_id(struct zd_chip *chip)
  71. {
  72. char buffer[80];
  73. scnprint_id(chip, buffer, sizeof(buffer));
  74. buffer[sizeof(buffer)-1] = 0;
  75. dev_info(zd_chip_dev(chip), "%s\n", buffer);
  76. }
  77. static zd_addr_t inc_addr(zd_addr_t addr)
  78. {
  79. u16 a = (u16)addr;
  80. /* Control registers use byte addressing, but everything else uses word
  81. * addressing. */
  82. if ((a & 0xf000) == CR_START)
  83. a += 2;
  84. else
  85. a += 1;
  86. return (zd_addr_t)a;
  87. }
  88. /* Read a variable number of 32-bit values. Parameter count is not allowed to
  89. * exceed USB_MAX_IOREAD32_COUNT.
  90. */
  91. int zd_ioread32v_locked(struct zd_chip *chip, u32 *values, const zd_addr_t *addr,
  92. unsigned int count)
  93. {
  94. int r;
  95. int i;
  96. zd_addr_t *a16;
  97. u16 *v16;
  98. unsigned int count16;
  99. if (count > USB_MAX_IOREAD32_COUNT)
  100. return -EINVAL;
  101. /* Allocate a single memory block for values and addresses. */
  102. count16 = 2*count;
  103. a16 = (zd_addr_t *) kmalloc(count16 * (sizeof(zd_addr_t) + sizeof(u16)),
  104. GFP_KERNEL);
  105. if (!a16) {
  106. dev_dbg_f(zd_chip_dev(chip),
  107. "error ENOMEM in allocation of a16\n");
  108. r = -ENOMEM;
  109. goto out;
  110. }
  111. v16 = (u16 *)(a16 + count16);
  112. for (i = 0; i < count; i++) {
  113. int j = 2*i;
  114. /* We read the high word always first. */
  115. a16[j] = inc_addr(addr[i]);
  116. a16[j+1] = addr[i];
  117. }
  118. r = zd_ioread16v_locked(chip, v16, a16, count16);
  119. if (r) {
  120. dev_dbg_f(zd_chip_dev(chip),
  121. "error: zd_ioread16v_locked. Error number %d\n", r);
  122. goto out;
  123. }
  124. for (i = 0; i < count; i++) {
  125. int j = 2*i;
  126. values[i] = (v16[j] << 16) | v16[j+1];
  127. }
  128. out:
  129. kfree((void *)a16);
  130. return r;
  131. }
  132. int _zd_iowrite32v_locked(struct zd_chip *chip, const struct zd_ioreq32 *ioreqs,
  133. unsigned int count)
  134. {
  135. int i, j, r;
  136. struct zd_ioreq16 *ioreqs16;
  137. unsigned int count16;
  138. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  139. if (count == 0)
  140. return 0;
  141. if (count > USB_MAX_IOWRITE32_COUNT)
  142. return -EINVAL;
  143. /* Allocate a single memory block for values and addresses. */
  144. count16 = 2*count;
  145. ioreqs16 = kmalloc(count16 * sizeof(struct zd_ioreq16), GFP_KERNEL);
  146. if (!ioreqs16) {
  147. r = -ENOMEM;
  148. dev_dbg_f(zd_chip_dev(chip),
  149. "error %d in ioreqs16 allocation\n", r);
  150. goto out;
  151. }
  152. for (i = 0; i < count; i++) {
  153. j = 2*i;
  154. /* We write the high word always first. */
  155. ioreqs16[j].value = ioreqs[i].value >> 16;
  156. ioreqs16[j].addr = inc_addr(ioreqs[i].addr);
  157. ioreqs16[j+1].value = ioreqs[i].value;
  158. ioreqs16[j+1].addr = ioreqs[i].addr;
  159. }
  160. r = zd_usb_iowrite16v(&chip->usb, ioreqs16, count16);
  161. #ifdef DEBUG
  162. if (r) {
  163. dev_dbg_f(zd_chip_dev(chip),
  164. "error %d in zd_usb_write16v\n", r);
  165. }
  166. #endif /* DEBUG */
  167. out:
  168. kfree(ioreqs16);
  169. return r;
  170. }
  171. int zd_iowrite16a_locked(struct zd_chip *chip,
  172. const struct zd_ioreq16 *ioreqs, unsigned int count)
  173. {
  174. int r;
  175. unsigned int i, j, t, max;
  176. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  177. for (i = 0; i < count; i += j + t) {
  178. t = 0;
  179. max = count-i;
  180. if (max > USB_MAX_IOWRITE16_COUNT)
  181. max = USB_MAX_IOWRITE16_COUNT;
  182. for (j = 0; j < max; j++) {
  183. if (!ioreqs[i+j].addr) {
  184. t = 1;
  185. break;
  186. }
  187. }
  188. r = zd_usb_iowrite16v(&chip->usb, &ioreqs[i], j);
  189. if (r) {
  190. dev_dbg_f(zd_chip_dev(chip),
  191. "error zd_usb_iowrite16v. Error number %d\n",
  192. r);
  193. return r;
  194. }
  195. }
  196. return 0;
  197. }
  198. /* Writes a variable number of 32 bit registers. The functions will split
  199. * that in several USB requests. A split can be forced by inserting an IO
  200. * request with an zero address field.
  201. */
  202. int zd_iowrite32a_locked(struct zd_chip *chip,
  203. const struct zd_ioreq32 *ioreqs, unsigned int count)
  204. {
  205. int r;
  206. unsigned int i, j, t, max;
  207. for (i = 0; i < count; i += j + t) {
  208. t = 0;
  209. max = count-i;
  210. if (max > USB_MAX_IOWRITE32_COUNT)
  211. max = USB_MAX_IOWRITE32_COUNT;
  212. for (j = 0; j < max; j++) {
  213. if (!ioreqs[i+j].addr) {
  214. t = 1;
  215. break;
  216. }
  217. }
  218. r = _zd_iowrite32v_locked(chip, &ioreqs[i], j);
  219. if (r) {
  220. dev_dbg_f(zd_chip_dev(chip),
  221. "error _zd_iowrite32v_locked."
  222. " Error number %d\n", r);
  223. return r;
  224. }
  225. }
  226. return 0;
  227. }
  228. int zd_ioread16(struct zd_chip *chip, zd_addr_t addr, u16 *value)
  229. {
  230. int r;
  231. mutex_lock(&chip->mutex);
  232. r = zd_ioread16_locked(chip, value, addr);
  233. mutex_unlock(&chip->mutex);
  234. return r;
  235. }
  236. int zd_ioread32(struct zd_chip *chip, zd_addr_t addr, u32 *value)
  237. {
  238. int r;
  239. mutex_lock(&chip->mutex);
  240. r = zd_ioread32_locked(chip, value, addr);
  241. mutex_unlock(&chip->mutex);
  242. return r;
  243. }
  244. int zd_iowrite16(struct zd_chip *chip, zd_addr_t addr, u16 value)
  245. {
  246. int r;
  247. mutex_lock(&chip->mutex);
  248. r = zd_iowrite16_locked(chip, value, addr);
  249. mutex_unlock(&chip->mutex);
  250. return r;
  251. }
  252. int zd_iowrite32(struct zd_chip *chip, zd_addr_t addr, u32 value)
  253. {
  254. int r;
  255. mutex_lock(&chip->mutex);
  256. r = zd_iowrite32_locked(chip, value, addr);
  257. mutex_unlock(&chip->mutex);
  258. return r;
  259. }
  260. int zd_ioread32v(struct zd_chip *chip, const zd_addr_t *addresses,
  261. u32 *values, unsigned int count)
  262. {
  263. int r;
  264. mutex_lock(&chip->mutex);
  265. r = zd_ioread32v_locked(chip, values, addresses, count);
  266. mutex_unlock(&chip->mutex);
  267. return r;
  268. }
  269. int zd_iowrite32a(struct zd_chip *chip, const struct zd_ioreq32 *ioreqs,
  270. unsigned int count)
  271. {
  272. int r;
  273. mutex_lock(&chip->mutex);
  274. r = zd_iowrite32a_locked(chip, ioreqs, count);
  275. mutex_unlock(&chip->mutex);
  276. return r;
  277. }
  278. static int read_pod(struct zd_chip *chip, u8 *rf_type)
  279. {
  280. int r;
  281. u32 value;
  282. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  283. r = zd_ioread32_locked(chip, &value, E2P_POD);
  284. if (r)
  285. goto error;
  286. dev_dbg_f(zd_chip_dev(chip), "E2P_POD %#010x\n", value);
  287. /* FIXME: AL2230 handling (Bit 7 in POD) */
  288. *rf_type = value & 0x0f;
  289. chip->pa_type = (value >> 16) & 0x0f;
  290. chip->patch_cck_gain = (value >> 8) & 0x1;
  291. chip->patch_cr157 = (value >> 13) & 0x1;
  292. chip->patch_6m_band_edge = (value >> 21) & 0x1;
  293. chip->new_phy_layout = (value >> 31) & 0x1;
  294. chip->al2230s_bit = (value >> 7) & 0x1;
  295. chip->link_led = ((value >> 4) & 1) ? LED1 : LED2;
  296. chip->supports_tx_led = 1;
  297. if (value & (1 << 24)) { /* LED scenario */
  298. if (value & (1 << 29))
  299. chip->supports_tx_led = 0;
  300. }
  301. dev_dbg_f(zd_chip_dev(chip),
  302. "RF %s %#01x PA type %#01x patch CCK %d patch CR157 %d "
  303. "patch 6M %d new PHY %d link LED%d tx led %d\n",
  304. zd_rf_name(*rf_type), *rf_type,
  305. chip->pa_type, chip->patch_cck_gain,
  306. chip->patch_cr157, chip->patch_6m_band_edge,
  307. chip->new_phy_layout,
  308. chip->link_led == LED1 ? 1 : 2,
  309. chip->supports_tx_led);
  310. return 0;
  311. error:
  312. *rf_type = 0;
  313. chip->pa_type = 0;
  314. chip->patch_cck_gain = 0;
  315. chip->patch_cr157 = 0;
  316. chip->patch_6m_band_edge = 0;
  317. chip->new_phy_layout = 0;
  318. return r;
  319. }
  320. /* MAC address: if custom mac addresses are to to be used CR_MAC_ADDR_P1 and
  321. * CR_MAC_ADDR_P2 must be overwritten
  322. */
  323. int zd_write_mac_addr(struct zd_chip *chip, const u8 *mac_addr)
  324. {
  325. int r;
  326. struct zd_ioreq32 reqs[2] = {
  327. [0] = { .addr = CR_MAC_ADDR_P1 },
  328. [1] = { .addr = CR_MAC_ADDR_P2 },
  329. };
  330. DECLARE_MAC_BUF(mac);
  331. reqs[0].value = (mac_addr[3] << 24)
  332. | (mac_addr[2] << 16)
  333. | (mac_addr[1] << 8)
  334. | mac_addr[0];
  335. reqs[1].value = (mac_addr[5] << 8)
  336. | mac_addr[4];
  337. dev_dbg_f(zd_chip_dev(chip),
  338. "mac addr %s\n", print_mac(mac, mac_addr));
  339. mutex_lock(&chip->mutex);
  340. r = zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  341. mutex_unlock(&chip->mutex);
  342. return r;
  343. }
  344. int zd_read_regdomain(struct zd_chip *chip, u8 *regdomain)
  345. {
  346. int r;
  347. u32 value;
  348. mutex_lock(&chip->mutex);
  349. r = zd_ioread32_locked(chip, &value, E2P_SUBID);
  350. mutex_unlock(&chip->mutex);
  351. if (r)
  352. return r;
  353. *regdomain = value >> 16;
  354. dev_dbg_f(zd_chip_dev(chip), "regdomain: %#04x\n", *regdomain);
  355. return 0;
  356. }
  357. static int read_values(struct zd_chip *chip, u8 *values, size_t count,
  358. zd_addr_t e2p_addr, u32 guard)
  359. {
  360. int r;
  361. int i;
  362. u32 v;
  363. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  364. for (i = 0;;) {
  365. r = zd_ioread32_locked(chip, &v,
  366. (zd_addr_t)((u16)e2p_addr+i/2));
  367. if (r)
  368. return r;
  369. v -= guard;
  370. if (i+4 < count) {
  371. values[i++] = v;
  372. values[i++] = v >> 8;
  373. values[i++] = v >> 16;
  374. values[i++] = v >> 24;
  375. continue;
  376. }
  377. for (;i < count; i++)
  378. values[i] = v >> (8*(i%3));
  379. return 0;
  380. }
  381. }
  382. static int read_pwr_cal_values(struct zd_chip *chip)
  383. {
  384. return read_values(chip, chip->pwr_cal_values,
  385. E2P_CHANNEL_COUNT, E2P_PWR_CAL_VALUE1,
  386. 0);
  387. }
  388. static int read_pwr_int_values(struct zd_chip *chip)
  389. {
  390. return read_values(chip, chip->pwr_int_values,
  391. E2P_CHANNEL_COUNT, E2P_PWR_INT_VALUE1,
  392. E2P_PWR_INT_GUARD);
  393. }
  394. static int read_ofdm_cal_values(struct zd_chip *chip)
  395. {
  396. int r;
  397. int i;
  398. static const zd_addr_t addresses[] = {
  399. E2P_36M_CAL_VALUE1,
  400. E2P_48M_CAL_VALUE1,
  401. E2P_54M_CAL_VALUE1,
  402. };
  403. for (i = 0; i < 3; i++) {
  404. r = read_values(chip, chip->ofdm_cal_values[i],
  405. E2P_CHANNEL_COUNT, addresses[i], 0);
  406. if (r)
  407. return r;
  408. }
  409. return 0;
  410. }
  411. static int read_cal_int_tables(struct zd_chip *chip)
  412. {
  413. int r;
  414. r = read_pwr_cal_values(chip);
  415. if (r)
  416. return r;
  417. r = read_pwr_int_values(chip);
  418. if (r)
  419. return r;
  420. r = read_ofdm_cal_values(chip);
  421. if (r)
  422. return r;
  423. return 0;
  424. }
  425. /* phy means physical registers */
  426. int zd_chip_lock_phy_regs(struct zd_chip *chip)
  427. {
  428. int r;
  429. u32 tmp;
  430. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  431. r = zd_ioread32_locked(chip, &tmp, CR_REG1);
  432. if (r) {
  433. dev_err(zd_chip_dev(chip), "error ioread32(CR_REG1): %d\n", r);
  434. return r;
  435. }
  436. tmp &= ~UNLOCK_PHY_REGS;
  437. r = zd_iowrite32_locked(chip, tmp, CR_REG1);
  438. if (r)
  439. dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r);
  440. return r;
  441. }
  442. int zd_chip_unlock_phy_regs(struct zd_chip *chip)
  443. {
  444. int r;
  445. u32 tmp;
  446. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  447. r = zd_ioread32_locked(chip, &tmp, CR_REG1);
  448. if (r) {
  449. dev_err(zd_chip_dev(chip),
  450. "error ioread32(CR_REG1): %d\n", r);
  451. return r;
  452. }
  453. tmp |= UNLOCK_PHY_REGS;
  454. r = zd_iowrite32_locked(chip, tmp, CR_REG1);
  455. if (r)
  456. dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r);
  457. return r;
  458. }
  459. /* CR157 can be optionally patched by the EEPROM for original ZD1211 */
  460. static int patch_cr157(struct zd_chip *chip)
  461. {
  462. int r;
  463. u16 value;
  464. if (!chip->patch_cr157)
  465. return 0;
  466. r = zd_ioread16_locked(chip, &value, E2P_PHY_REG);
  467. if (r)
  468. return r;
  469. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value >> 8);
  470. return zd_iowrite32_locked(chip, value >> 8, CR157);
  471. }
  472. /*
  473. * 6M band edge can be optionally overwritten for certain RF's
  474. * Vendor driver says: for FCC regulation, enabled per HWFeature 6M band edge
  475. * bit (for AL2230, AL2230S)
  476. */
  477. static int patch_6m_band_edge(struct zd_chip *chip, u8 channel)
  478. {
  479. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  480. if (!chip->patch_6m_band_edge)
  481. return 0;
  482. return zd_rf_patch_6m_band_edge(&chip->rf, channel);
  483. }
  484. /* Generic implementation of 6M band edge patching, used by most RFs via
  485. * zd_rf_generic_patch_6m() */
  486. int zd_chip_generic_patch_6m_band(struct zd_chip *chip, int channel)
  487. {
  488. struct zd_ioreq16 ioreqs[] = {
  489. { CR128, 0x14 }, { CR129, 0x12 }, { CR130, 0x10 },
  490. { CR47, 0x1e },
  491. };
  492. /* FIXME: Channel 11 is not the edge for all regulatory domains. */
  493. if (channel == 1 || channel == 11)
  494. ioreqs[0].value = 0x12;
  495. dev_dbg_f(zd_chip_dev(chip), "patching for channel %d\n", channel);
  496. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  497. }
  498. static int zd1211_hw_reset_phy(struct zd_chip *chip)
  499. {
  500. static const struct zd_ioreq16 ioreqs[] = {
  501. { CR0, 0x0a }, { CR1, 0x06 }, { CR2, 0x26 },
  502. { CR3, 0x38 }, { CR4, 0x80 }, { CR9, 0xa0 },
  503. { CR10, 0x81 }, { CR11, 0x00 }, { CR12, 0x7f },
  504. { CR13, 0x8c }, { CR14, 0x80 }, { CR15, 0x3d },
  505. { CR16, 0x20 }, { CR17, 0x1e }, { CR18, 0x0a },
  506. { CR19, 0x48 }, { CR20, 0x0c }, { CR21, 0x0c },
  507. { CR22, 0x23 }, { CR23, 0x90 }, { CR24, 0x14 },
  508. { CR25, 0x40 }, { CR26, 0x10 }, { CR27, 0x19 },
  509. { CR28, 0x7f }, { CR29, 0x80 }, { CR30, 0x4b },
  510. { CR31, 0x60 }, { CR32, 0x43 }, { CR33, 0x08 },
  511. { CR34, 0x06 }, { CR35, 0x0a }, { CR36, 0x00 },
  512. { CR37, 0x00 }, { CR38, 0x38 }, { CR39, 0x0c },
  513. { CR40, 0x84 }, { CR41, 0x2a }, { CR42, 0x80 },
  514. { CR43, 0x10 }, { CR44, 0x12 }, { CR46, 0xff },
  515. { CR47, 0x1E }, { CR48, 0x26 }, { CR49, 0x5b },
  516. { CR64, 0xd0 }, { CR65, 0x04 }, { CR66, 0x58 },
  517. { CR67, 0xc9 }, { CR68, 0x88 }, { CR69, 0x41 },
  518. { CR70, 0x23 }, { CR71, 0x10 }, { CR72, 0xff },
  519. { CR73, 0x32 }, { CR74, 0x30 }, { CR75, 0x65 },
  520. { CR76, 0x41 }, { CR77, 0x1b }, { CR78, 0x30 },
  521. { CR79, 0x68 }, { CR80, 0x64 }, { CR81, 0x64 },
  522. { CR82, 0x00 }, { CR83, 0x00 }, { CR84, 0x00 },
  523. { CR85, 0x02 }, { CR86, 0x00 }, { CR87, 0x00 },
  524. { CR88, 0xff }, { CR89, 0xfc }, { CR90, 0x00 },
  525. { CR91, 0x00 }, { CR92, 0x00 }, { CR93, 0x08 },
  526. { CR94, 0x00 }, { CR95, 0x00 }, { CR96, 0xff },
  527. { CR97, 0xe7 }, { CR98, 0x00 }, { CR99, 0x00 },
  528. { CR100, 0x00 }, { CR101, 0xae }, { CR102, 0x02 },
  529. { CR103, 0x00 }, { CR104, 0x03 }, { CR105, 0x65 },
  530. { CR106, 0x04 }, { CR107, 0x00 }, { CR108, 0x0a },
  531. { CR109, 0xaa }, { CR110, 0xaa }, { CR111, 0x25 },
  532. { CR112, 0x25 }, { CR113, 0x00 }, { CR119, 0x1e },
  533. { CR125, 0x90 }, { CR126, 0x00 }, { CR127, 0x00 },
  534. { },
  535. { CR5, 0x00 }, { CR6, 0x00 }, { CR7, 0x00 },
  536. { CR8, 0x00 }, { CR9, 0x20 }, { CR12, 0xf0 },
  537. { CR20, 0x0e }, { CR21, 0x0e }, { CR27, 0x10 },
  538. { CR44, 0x33 }, { CR47, 0x1E }, { CR83, 0x24 },
  539. { CR84, 0x04 }, { CR85, 0x00 }, { CR86, 0x0C },
  540. { CR87, 0x12 }, { CR88, 0x0C }, { CR89, 0x00 },
  541. { CR90, 0x10 }, { CR91, 0x08 }, { CR93, 0x00 },
  542. { CR94, 0x01 }, { CR95, 0x00 }, { CR96, 0x50 },
  543. { CR97, 0x37 }, { CR98, 0x35 }, { CR101, 0x13 },
  544. { CR102, 0x27 }, { CR103, 0x27 }, { CR104, 0x18 },
  545. { CR105, 0x12 }, { CR109, 0x27 }, { CR110, 0x27 },
  546. { CR111, 0x27 }, { CR112, 0x27 }, { CR113, 0x27 },
  547. { CR114, 0x27 }, { CR115, 0x26 }, { CR116, 0x24 },
  548. { CR117, 0xfc }, { CR118, 0xfa }, { CR120, 0x4f },
  549. { CR125, 0xaa }, { CR127, 0x03 }, { CR128, 0x14 },
  550. { CR129, 0x12 }, { CR130, 0x10 }, { CR131, 0x0C },
  551. { CR136, 0xdf }, { CR137, 0x40 }, { CR138, 0xa0 },
  552. { CR139, 0xb0 }, { CR140, 0x99 }, { CR141, 0x82 },
  553. { CR142, 0x54 }, { CR143, 0x1c }, { CR144, 0x6c },
  554. { CR147, 0x07 }, { CR148, 0x4c }, { CR149, 0x50 },
  555. { CR150, 0x0e }, { CR151, 0x18 }, { CR160, 0xfe },
  556. { CR161, 0xee }, { CR162, 0xaa }, { CR163, 0xfa },
  557. { CR164, 0xfa }, { CR165, 0xea }, { CR166, 0xbe },
  558. { CR167, 0xbe }, { CR168, 0x6a }, { CR169, 0xba },
  559. { CR170, 0xba }, { CR171, 0xba },
  560. /* Note: CR204 must lead the CR203 */
  561. { CR204, 0x7d },
  562. { },
  563. { CR203, 0x30 },
  564. };
  565. int r, t;
  566. dev_dbg_f(zd_chip_dev(chip), "\n");
  567. r = zd_chip_lock_phy_regs(chip);
  568. if (r)
  569. goto out;
  570. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  571. if (r)
  572. goto unlock;
  573. r = patch_cr157(chip);
  574. unlock:
  575. t = zd_chip_unlock_phy_regs(chip);
  576. if (t && !r)
  577. r = t;
  578. out:
  579. return r;
  580. }
  581. static int zd1211b_hw_reset_phy(struct zd_chip *chip)
  582. {
  583. static const struct zd_ioreq16 ioreqs[] = {
  584. { CR0, 0x14 }, { CR1, 0x06 }, { CR2, 0x26 },
  585. { CR3, 0x38 }, { CR4, 0x80 }, { CR9, 0xe0 },
  586. { CR10, 0x81 },
  587. /* power control { { CR11, 1 << 6 }, */
  588. { CR11, 0x00 },
  589. { CR12, 0xf0 }, { CR13, 0x8c }, { CR14, 0x80 },
  590. { CR15, 0x3d }, { CR16, 0x20 }, { CR17, 0x1e },
  591. { CR18, 0x0a }, { CR19, 0x48 },
  592. { CR20, 0x10 }, /* Org:0x0E, ComTrend:RalLink AP */
  593. { CR21, 0x0e }, { CR22, 0x23 }, { CR23, 0x90 },
  594. { CR24, 0x14 }, { CR25, 0x40 }, { CR26, 0x10 },
  595. { CR27, 0x10 }, { CR28, 0x7f }, { CR29, 0x80 },
  596. { CR30, 0x4b }, /* ASIC/FWT, no jointly decoder */
  597. { CR31, 0x60 }, { CR32, 0x43 }, { CR33, 0x08 },
  598. { CR34, 0x06 }, { CR35, 0x0a }, { CR36, 0x00 },
  599. { CR37, 0x00 }, { CR38, 0x38 }, { CR39, 0x0c },
  600. { CR40, 0x84 }, { CR41, 0x2a }, { CR42, 0x80 },
  601. { CR43, 0x10 }, { CR44, 0x33 }, { CR46, 0xff },
  602. { CR47, 0x1E }, { CR48, 0x26 }, { CR49, 0x5b },
  603. { CR64, 0xd0 }, { CR65, 0x04 }, { CR66, 0x58 },
  604. { CR67, 0xc9 }, { CR68, 0x88 }, { CR69, 0x41 },
  605. { CR70, 0x23 }, { CR71, 0x10 }, { CR72, 0xff },
  606. { CR73, 0x32 }, { CR74, 0x30 }, { CR75, 0x65 },
  607. { CR76, 0x41 }, { CR77, 0x1b }, { CR78, 0x30 },
  608. { CR79, 0xf0 }, { CR80, 0x64 }, { CR81, 0x64 },
  609. { CR82, 0x00 }, { CR83, 0x24 }, { CR84, 0x04 },
  610. { CR85, 0x00 }, { CR86, 0x0c }, { CR87, 0x12 },
  611. { CR88, 0x0c }, { CR89, 0x00 }, { CR90, 0x58 },
  612. { CR91, 0x04 }, { CR92, 0x00 }, { CR93, 0x00 },
  613. { CR94, 0x01 },
  614. { CR95, 0x20 }, /* ZD1211B */
  615. { CR96, 0x50 }, { CR97, 0x37 }, { CR98, 0x35 },
  616. { CR99, 0x00 }, { CR100, 0x01 }, { CR101, 0x13 },
  617. { CR102, 0x27 }, { CR103, 0x27 }, { CR104, 0x18 },
  618. { CR105, 0x12 }, { CR106, 0x04 }, { CR107, 0x00 },
  619. { CR108, 0x0a }, { CR109, 0x27 }, { CR110, 0x27 },
  620. { CR111, 0x27 }, { CR112, 0x27 }, { CR113, 0x27 },
  621. { CR114, 0x27 }, { CR115, 0x26 }, { CR116, 0x24 },
  622. { CR117, 0xfc }, { CR118, 0xfa }, { CR119, 0x1e },
  623. { CR125, 0x90 }, { CR126, 0x00 }, { CR127, 0x00 },
  624. { CR128, 0x14 }, { CR129, 0x12 }, { CR130, 0x10 },
  625. { CR131, 0x0c }, { CR136, 0xdf }, { CR137, 0xa0 },
  626. { CR138, 0xa8 }, { CR139, 0xb4 }, { CR140, 0x98 },
  627. { CR141, 0x82 }, { CR142, 0x53 }, { CR143, 0x1c },
  628. { CR144, 0x6c }, { CR147, 0x07 }, { CR148, 0x40 },
  629. { CR149, 0x40 }, /* Org:0x50 ComTrend:RalLink AP */
  630. { CR150, 0x14 }, /* Org:0x0E ComTrend:RalLink AP */
  631. { CR151, 0x18 }, { CR159, 0x70 }, { CR160, 0xfe },
  632. { CR161, 0xee }, { CR162, 0xaa }, { CR163, 0xfa },
  633. { CR164, 0xfa }, { CR165, 0xea }, { CR166, 0xbe },
  634. { CR167, 0xbe }, { CR168, 0x6a }, { CR169, 0xba },
  635. { CR170, 0xba }, { CR171, 0xba },
  636. /* Note: CR204 must lead the CR203 */
  637. { CR204, 0x7d },
  638. {},
  639. { CR203, 0x30 },
  640. };
  641. int r, t;
  642. dev_dbg_f(zd_chip_dev(chip), "\n");
  643. r = zd_chip_lock_phy_regs(chip);
  644. if (r)
  645. goto out;
  646. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  647. t = zd_chip_unlock_phy_regs(chip);
  648. if (t && !r)
  649. r = t;
  650. out:
  651. return r;
  652. }
  653. static int hw_reset_phy(struct zd_chip *chip)
  654. {
  655. return zd_chip_is_zd1211b(chip) ? zd1211b_hw_reset_phy(chip) :
  656. zd1211_hw_reset_phy(chip);
  657. }
  658. static int zd1211_hw_init_hmac(struct zd_chip *chip)
  659. {
  660. static const struct zd_ioreq32 ioreqs[] = {
  661. { CR_ZD1211_RETRY_MAX, 0x2 },
  662. { CR_RX_THRESHOLD, 0x000c0640 },
  663. };
  664. dev_dbg_f(zd_chip_dev(chip), "\n");
  665. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  666. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  667. }
  668. static int zd1211b_hw_init_hmac(struct zd_chip *chip)
  669. {
  670. static const struct zd_ioreq32 ioreqs[] = {
  671. { CR_ZD1211B_RETRY_MAX, 0x02020202 },
  672. { CR_ZD1211B_TX_PWR_CTL4, 0x007f003f },
  673. { CR_ZD1211B_TX_PWR_CTL3, 0x007f003f },
  674. { CR_ZD1211B_TX_PWR_CTL2, 0x003f001f },
  675. { CR_ZD1211B_TX_PWR_CTL1, 0x001f000f },
  676. { CR_ZD1211B_AIFS_CTL1, 0x00280028 },
  677. { CR_ZD1211B_AIFS_CTL2, 0x008C003C },
  678. { CR_ZD1211B_TXOP, 0x01800824 },
  679. { CR_RX_THRESHOLD, 0x000c0eff, },
  680. };
  681. dev_dbg_f(zd_chip_dev(chip), "\n");
  682. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  683. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  684. }
  685. static int hw_init_hmac(struct zd_chip *chip)
  686. {
  687. int r;
  688. static const struct zd_ioreq32 ioreqs[] = {
  689. { CR_ACK_TIMEOUT_EXT, 0x20 },
  690. { CR_ADDA_MBIAS_WARMTIME, 0x30000808 },
  691. { CR_SNIFFER_ON, 0 },
  692. { CR_RX_FILTER, STA_RX_FILTER },
  693. { CR_GROUP_HASH_P1, 0x00 },
  694. { CR_GROUP_HASH_P2, 0x80000000 },
  695. { CR_REG1, 0xa4 },
  696. { CR_ADDA_PWR_DWN, 0x7f },
  697. { CR_BCN_PLCP_CFG, 0x00f00401 },
  698. { CR_PHY_DELAY, 0x00 },
  699. { CR_ACK_TIMEOUT_EXT, 0x80 },
  700. { CR_ADDA_PWR_DWN, 0x00 },
  701. { CR_ACK_TIME_80211, 0x100 },
  702. { CR_RX_PE_DELAY, 0x70 },
  703. { CR_PS_CTRL, 0x10000000 },
  704. { CR_RTS_CTS_RATE, 0x02030203 },
  705. { CR_AFTER_PNP, 0x1 },
  706. { CR_WEP_PROTECT, 0x114 },
  707. { CR_IFS_VALUE, IFS_VALUE_DEFAULT },
  708. };
  709. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  710. r = zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  711. if (r)
  712. return r;
  713. return zd_chip_is_zd1211b(chip) ?
  714. zd1211b_hw_init_hmac(chip) : zd1211_hw_init_hmac(chip);
  715. }
  716. struct aw_pt_bi {
  717. u32 atim_wnd_period;
  718. u32 pre_tbtt;
  719. u32 beacon_interval;
  720. };
  721. static int get_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  722. {
  723. int r;
  724. static const zd_addr_t aw_pt_bi_addr[] =
  725. { CR_ATIM_WND_PERIOD, CR_PRE_TBTT, CR_BCN_INTERVAL };
  726. u32 values[3];
  727. r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr,
  728. ARRAY_SIZE(aw_pt_bi_addr));
  729. if (r) {
  730. memset(s, 0, sizeof(*s));
  731. return r;
  732. }
  733. s->atim_wnd_period = values[0];
  734. s->pre_tbtt = values[1];
  735. s->beacon_interval = values[2];
  736. return 0;
  737. }
  738. static int set_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  739. {
  740. struct zd_ioreq32 reqs[3];
  741. if (s->beacon_interval <= 5)
  742. s->beacon_interval = 5;
  743. if (s->pre_tbtt < 4 || s->pre_tbtt >= s->beacon_interval)
  744. s->pre_tbtt = s->beacon_interval - 1;
  745. if (s->atim_wnd_period >= s->pre_tbtt)
  746. s->atim_wnd_period = s->pre_tbtt - 1;
  747. reqs[0].addr = CR_ATIM_WND_PERIOD;
  748. reqs[0].value = s->atim_wnd_period;
  749. reqs[1].addr = CR_PRE_TBTT;
  750. reqs[1].value = s->pre_tbtt;
  751. reqs[2].addr = CR_BCN_INTERVAL;
  752. reqs[2].value = s->beacon_interval;
  753. return zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  754. }
  755. static int set_beacon_interval(struct zd_chip *chip, u32 interval)
  756. {
  757. int r;
  758. struct aw_pt_bi s;
  759. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  760. r = get_aw_pt_bi(chip, &s);
  761. if (r)
  762. return r;
  763. s.beacon_interval = interval;
  764. return set_aw_pt_bi(chip, &s);
  765. }
  766. int zd_set_beacon_interval(struct zd_chip *chip, u32 interval)
  767. {
  768. int r;
  769. mutex_lock(&chip->mutex);
  770. r = set_beacon_interval(chip, interval);
  771. mutex_unlock(&chip->mutex);
  772. return r;
  773. }
  774. static int hw_init(struct zd_chip *chip)
  775. {
  776. int r;
  777. dev_dbg_f(zd_chip_dev(chip), "\n");
  778. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  779. r = hw_reset_phy(chip);
  780. if (r)
  781. return r;
  782. r = hw_init_hmac(chip);
  783. if (r)
  784. return r;
  785. return set_beacon_interval(chip, 100);
  786. }
  787. static zd_addr_t fw_reg_addr(struct zd_chip *chip, u16 offset)
  788. {
  789. return (zd_addr_t)((u16)chip->fw_regs_base + offset);
  790. }
  791. #ifdef DEBUG
  792. static int dump_cr(struct zd_chip *chip, const zd_addr_t addr,
  793. const char *addr_string)
  794. {
  795. int r;
  796. u32 value;
  797. r = zd_ioread32_locked(chip, &value, addr);
  798. if (r) {
  799. dev_dbg_f(zd_chip_dev(chip),
  800. "error reading %s. Error number %d\n", addr_string, r);
  801. return r;
  802. }
  803. dev_dbg_f(zd_chip_dev(chip), "%s %#010x\n",
  804. addr_string, (unsigned int)value);
  805. return 0;
  806. }
  807. static int test_init(struct zd_chip *chip)
  808. {
  809. int r;
  810. r = dump_cr(chip, CR_AFTER_PNP, "CR_AFTER_PNP");
  811. if (r)
  812. return r;
  813. r = dump_cr(chip, CR_GPI_EN, "CR_GPI_EN");
  814. if (r)
  815. return r;
  816. return dump_cr(chip, CR_INTERRUPT, "CR_INTERRUPT");
  817. }
  818. static void dump_fw_registers(struct zd_chip *chip)
  819. {
  820. const zd_addr_t addr[4] = {
  821. fw_reg_addr(chip, FW_REG_FIRMWARE_VER),
  822. fw_reg_addr(chip, FW_REG_USB_SPEED),
  823. fw_reg_addr(chip, FW_REG_FIX_TX_RATE),
  824. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  825. };
  826. int r;
  827. u16 values[4];
  828. r = zd_ioread16v_locked(chip, values, (const zd_addr_t*)addr,
  829. ARRAY_SIZE(addr));
  830. if (r) {
  831. dev_dbg_f(zd_chip_dev(chip), "error %d zd_ioread16v_locked\n",
  832. r);
  833. return;
  834. }
  835. dev_dbg_f(zd_chip_dev(chip), "FW_FIRMWARE_VER %#06hx\n", values[0]);
  836. dev_dbg_f(zd_chip_dev(chip), "FW_USB_SPEED %#06hx\n", values[1]);
  837. dev_dbg_f(zd_chip_dev(chip), "FW_FIX_TX_RATE %#06hx\n", values[2]);
  838. dev_dbg_f(zd_chip_dev(chip), "FW_LINK_STATUS %#06hx\n", values[3]);
  839. }
  840. #endif /* DEBUG */
  841. static int print_fw_version(struct zd_chip *chip)
  842. {
  843. int r;
  844. u16 version;
  845. r = zd_ioread16_locked(chip, &version,
  846. fw_reg_addr(chip, FW_REG_FIRMWARE_VER));
  847. if (r)
  848. return r;
  849. dev_info(zd_chip_dev(chip),"firmware version %04hx\n", version);
  850. return 0;
  851. }
  852. static int set_mandatory_rates(struct zd_chip *chip, enum ieee80211_std std)
  853. {
  854. u32 rates;
  855. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  856. /* This sets the mandatory rates, which only depend from the standard
  857. * that the device is supporting. Until further notice we should try
  858. * to support 802.11g also for full speed USB.
  859. */
  860. switch (std) {
  861. case IEEE80211B:
  862. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M;
  863. break;
  864. case IEEE80211G:
  865. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M|
  866. CR_RATE_6M|CR_RATE_12M|CR_RATE_24M;
  867. break;
  868. default:
  869. return -EINVAL;
  870. }
  871. return zd_iowrite32_locked(chip, rates, CR_MANDATORY_RATE_TBL);
  872. }
  873. int zd_chip_set_rts_cts_rate_locked(struct zd_chip *chip,
  874. u8 rts_rate, int preamble)
  875. {
  876. int rts_mod = ZD_RX_CCK;
  877. u32 value = 0;
  878. /* Modulation bit */
  879. if (ZD_MODULATION_TYPE(rts_rate) == ZD_OFDM)
  880. rts_mod = ZD_RX_OFDM;
  881. dev_dbg_f(zd_chip_dev(chip), "rts_rate=%x preamble=%x\n",
  882. rts_rate, preamble);
  883. value |= ZD_PURE_RATE(rts_rate) << RTSCTS_SH_RTS_RATE;
  884. value |= rts_mod << RTSCTS_SH_RTS_MOD_TYPE;
  885. value |= preamble << RTSCTS_SH_RTS_PMB_TYPE;
  886. value |= preamble << RTSCTS_SH_CTS_PMB_TYPE;
  887. /* We always send 11M self-CTS messages, like the vendor driver. */
  888. value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_CTS_RATE;
  889. value |= ZD_RX_CCK << RTSCTS_SH_CTS_MOD_TYPE;
  890. return zd_iowrite32_locked(chip, value, CR_RTS_CTS_RATE);
  891. }
  892. int zd_chip_enable_hwint(struct zd_chip *chip)
  893. {
  894. int r;
  895. mutex_lock(&chip->mutex);
  896. r = zd_iowrite32_locked(chip, HWINT_ENABLED, CR_INTERRUPT);
  897. mutex_unlock(&chip->mutex);
  898. return r;
  899. }
  900. static int disable_hwint(struct zd_chip *chip)
  901. {
  902. return zd_iowrite32_locked(chip, HWINT_DISABLED, CR_INTERRUPT);
  903. }
  904. int zd_chip_disable_hwint(struct zd_chip *chip)
  905. {
  906. int r;
  907. mutex_lock(&chip->mutex);
  908. r = disable_hwint(chip);
  909. mutex_unlock(&chip->mutex);
  910. return r;
  911. }
  912. static int read_fw_regs_offset(struct zd_chip *chip)
  913. {
  914. int r;
  915. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  916. r = zd_ioread16_locked(chip, (u16*)&chip->fw_regs_base,
  917. FWRAW_REGS_ADDR);
  918. if (r)
  919. return r;
  920. dev_dbg_f(zd_chip_dev(chip), "fw_regs_base: %#06hx\n",
  921. (u16)chip->fw_regs_base);
  922. return 0;
  923. }
  924. /* Read mac address using pre-firmware interface */
  925. int zd_chip_read_mac_addr_fw(struct zd_chip *chip, u8 *addr)
  926. {
  927. dev_dbg_f(zd_chip_dev(chip), "\n");
  928. return zd_usb_read_fw(&chip->usb, E2P_MAC_ADDR_P1, addr,
  929. ETH_ALEN);
  930. }
  931. int zd_chip_init_hw(struct zd_chip *chip)
  932. {
  933. int r;
  934. u8 rf_type;
  935. dev_dbg_f(zd_chip_dev(chip), "\n");
  936. mutex_lock(&chip->mutex);
  937. #ifdef DEBUG
  938. r = test_init(chip);
  939. if (r)
  940. goto out;
  941. #endif
  942. r = zd_iowrite32_locked(chip, 1, CR_AFTER_PNP);
  943. if (r)
  944. goto out;
  945. r = read_fw_regs_offset(chip);
  946. if (r)
  947. goto out;
  948. /* GPI is always disabled, also in the other driver.
  949. */
  950. r = zd_iowrite32_locked(chip, 0, CR_GPI_EN);
  951. if (r)
  952. goto out;
  953. r = zd_iowrite32_locked(chip, CWIN_SIZE, CR_CWMIN_CWMAX);
  954. if (r)
  955. goto out;
  956. /* Currently we support IEEE 802.11g for full and high speed USB.
  957. * It might be discussed, whether we should suppport pure b mode for
  958. * full speed USB.
  959. */
  960. r = set_mandatory_rates(chip, IEEE80211G);
  961. if (r)
  962. goto out;
  963. /* Disabling interrupts is certainly a smart thing here.
  964. */
  965. r = disable_hwint(chip);
  966. if (r)
  967. goto out;
  968. r = read_pod(chip, &rf_type);
  969. if (r)
  970. goto out;
  971. r = hw_init(chip);
  972. if (r)
  973. goto out;
  974. r = zd_rf_init_hw(&chip->rf, rf_type);
  975. if (r)
  976. goto out;
  977. r = print_fw_version(chip);
  978. if (r)
  979. goto out;
  980. #ifdef DEBUG
  981. dump_fw_registers(chip);
  982. r = test_init(chip);
  983. if (r)
  984. goto out;
  985. #endif /* DEBUG */
  986. r = read_cal_int_tables(chip);
  987. if (r)
  988. goto out;
  989. print_id(chip);
  990. out:
  991. mutex_unlock(&chip->mutex);
  992. return r;
  993. }
  994. static int update_pwr_int(struct zd_chip *chip, u8 channel)
  995. {
  996. u8 value = chip->pwr_int_values[channel - 1];
  997. return zd_iowrite16_locked(chip, value, CR31);
  998. }
  999. static int update_pwr_cal(struct zd_chip *chip, u8 channel)
  1000. {
  1001. u8 value = chip->pwr_cal_values[channel-1];
  1002. return zd_iowrite16_locked(chip, value, CR68);
  1003. }
  1004. static int update_ofdm_cal(struct zd_chip *chip, u8 channel)
  1005. {
  1006. struct zd_ioreq16 ioreqs[3];
  1007. ioreqs[0].addr = CR67;
  1008. ioreqs[0].value = chip->ofdm_cal_values[OFDM_36M_INDEX][channel-1];
  1009. ioreqs[1].addr = CR66;
  1010. ioreqs[1].value = chip->ofdm_cal_values[OFDM_48M_INDEX][channel-1];
  1011. ioreqs[2].addr = CR65;
  1012. ioreqs[2].value = chip->ofdm_cal_values[OFDM_54M_INDEX][channel-1];
  1013. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1014. }
  1015. static int update_channel_integration_and_calibration(struct zd_chip *chip,
  1016. u8 channel)
  1017. {
  1018. int r;
  1019. if (!zd_rf_should_update_pwr_int(&chip->rf))
  1020. return 0;
  1021. r = update_pwr_int(chip, channel);
  1022. if (r)
  1023. return r;
  1024. if (zd_chip_is_zd1211b(chip)) {
  1025. static const struct zd_ioreq16 ioreqs[] = {
  1026. { CR69, 0x28 },
  1027. {},
  1028. { CR69, 0x2a },
  1029. };
  1030. r = update_ofdm_cal(chip, channel);
  1031. if (r)
  1032. return r;
  1033. r = update_pwr_cal(chip, channel);
  1034. if (r)
  1035. return r;
  1036. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1037. if (r)
  1038. return r;
  1039. }
  1040. return 0;
  1041. }
  1042. /* The CCK baseband gain can be optionally patched by the EEPROM */
  1043. static int patch_cck_gain(struct zd_chip *chip)
  1044. {
  1045. int r;
  1046. u32 value;
  1047. if (!chip->patch_cck_gain || !zd_rf_should_patch_cck_gain(&chip->rf))
  1048. return 0;
  1049. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1050. r = zd_ioread32_locked(chip, &value, E2P_PHY_REG);
  1051. if (r)
  1052. return r;
  1053. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value & 0xff);
  1054. return zd_iowrite16_locked(chip, value & 0xff, CR47);
  1055. }
  1056. int zd_chip_set_channel(struct zd_chip *chip, u8 channel)
  1057. {
  1058. int r, t;
  1059. mutex_lock(&chip->mutex);
  1060. r = zd_chip_lock_phy_regs(chip);
  1061. if (r)
  1062. goto out;
  1063. r = zd_rf_set_channel(&chip->rf, channel);
  1064. if (r)
  1065. goto unlock;
  1066. r = update_channel_integration_and_calibration(chip, channel);
  1067. if (r)
  1068. goto unlock;
  1069. r = patch_cck_gain(chip);
  1070. if (r)
  1071. goto unlock;
  1072. r = patch_6m_band_edge(chip, channel);
  1073. if (r)
  1074. goto unlock;
  1075. r = zd_iowrite32_locked(chip, 0, CR_CONFIG_PHILIPS);
  1076. unlock:
  1077. t = zd_chip_unlock_phy_regs(chip);
  1078. if (t && !r)
  1079. r = t;
  1080. out:
  1081. mutex_unlock(&chip->mutex);
  1082. return r;
  1083. }
  1084. u8 zd_chip_get_channel(struct zd_chip *chip)
  1085. {
  1086. u8 channel;
  1087. mutex_lock(&chip->mutex);
  1088. channel = chip->rf.channel;
  1089. mutex_unlock(&chip->mutex);
  1090. return channel;
  1091. }
  1092. int zd_chip_control_leds(struct zd_chip *chip, enum led_status status)
  1093. {
  1094. const zd_addr_t a[] = {
  1095. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  1096. CR_LED,
  1097. };
  1098. int r;
  1099. u16 v[ARRAY_SIZE(a)];
  1100. struct zd_ioreq16 ioreqs[ARRAY_SIZE(a)] = {
  1101. [0] = { fw_reg_addr(chip, FW_REG_LED_LINK_STATUS) },
  1102. [1] = { CR_LED },
  1103. };
  1104. u16 other_led;
  1105. mutex_lock(&chip->mutex);
  1106. r = zd_ioread16v_locked(chip, v, (const zd_addr_t *)a, ARRAY_SIZE(a));
  1107. if (r)
  1108. goto out;
  1109. other_led = chip->link_led == LED1 ? LED2 : LED1;
  1110. switch (status) {
  1111. case LED_OFF:
  1112. ioreqs[0].value = FW_LINK_OFF;
  1113. ioreqs[1].value = v[1] & ~(LED1|LED2);
  1114. break;
  1115. case LED_SCANNING:
  1116. ioreqs[0].value = FW_LINK_OFF;
  1117. ioreqs[1].value = v[1] & ~other_led;
  1118. if (get_seconds() % 3 == 0) {
  1119. ioreqs[1].value &= ~chip->link_led;
  1120. } else {
  1121. ioreqs[1].value |= chip->link_led;
  1122. }
  1123. break;
  1124. case LED_ASSOCIATED:
  1125. ioreqs[0].value = FW_LINK_TX;
  1126. ioreqs[1].value = v[1] & ~other_led;
  1127. ioreqs[1].value |= chip->link_led;
  1128. break;
  1129. default:
  1130. r = -EINVAL;
  1131. goto out;
  1132. }
  1133. if (v[0] != ioreqs[0].value || v[1] != ioreqs[1].value) {
  1134. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1135. if (r)
  1136. goto out;
  1137. }
  1138. r = 0;
  1139. out:
  1140. mutex_unlock(&chip->mutex);
  1141. return r;
  1142. }
  1143. int zd_chip_set_basic_rates_locked(struct zd_chip *chip, u16 cr_rates)
  1144. {
  1145. ZD_ASSERT((cr_rates & ~(CR_RATES_80211B | CR_RATES_80211G)) == 0);
  1146. dev_dbg_f(zd_chip_dev(chip), "%x\n", cr_rates);
  1147. return zd_iowrite32_locked(chip, cr_rates, CR_BASIC_RATE_TBL);
  1148. }
  1149. static int ofdm_qual_db(u8 status_quality, u8 zd_rate, unsigned int size)
  1150. {
  1151. static const u16 constants[] = {
  1152. 715, 655, 585, 540, 470, 410, 360, 315,
  1153. 270, 235, 205, 175, 150, 125, 105, 85,
  1154. 65, 50, 40, 25, 15
  1155. };
  1156. int i;
  1157. u32 x;
  1158. /* It seems that their quality parameter is somehow per signal
  1159. * and is now transferred per bit.
  1160. */
  1161. switch (zd_rate) {
  1162. case ZD_OFDM_RATE_6M:
  1163. case ZD_OFDM_RATE_12M:
  1164. case ZD_OFDM_RATE_24M:
  1165. size *= 2;
  1166. break;
  1167. case ZD_OFDM_RATE_9M:
  1168. case ZD_OFDM_RATE_18M:
  1169. case ZD_OFDM_RATE_36M:
  1170. case ZD_OFDM_RATE_54M:
  1171. size *= 4;
  1172. size /= 3;
  1173. break;
  1174. case ZD_OFDM_RATE_48M:
  1175. size *= 3;
  1176. size /= 2;
  1177. break;
  1178. default:
  1179. return -EINVAL;
  1180. }
  1181. x = (10000 * status_quality)/size;
  1182. for (i = 0; i < ARRAY_SIZE(constants); i++) {
  1183. if (x > constants[i])
  1184. break;
  1185. }
  1186. switch (zd_rate) {
  1187. case ZD_OFDM_RATE_6M:
  1188. case ZD_OFDM_RATE_9M:
  1189. i += 3;
  1190. break;
  1191. case ZD_OFDM_RATE_12M:
  1192. case ZD_OFDM_RATE_18M:
  1193. i += 5;
  1194. break;
  1195. case ZD_OFDM_RATE_24M:
  1196. case ZD_OFDM_RATE_36M:
  1197. i += 9;
  1198. break;
  1199. case ZD_OFDM_RATE_48M:
  1200. case ZD_OFDM_RATE_54M:
  1201. i += 15;
  1202. break;
  1203. default:
  1204. return -EINVAL;
  1205. }
  1206. return i;
  1207. }
  1208. static int ofdm_qual_percent(u8 status_quality, u8 zd_rate, unsigned int size)
  1209. {
  1210. int r;
  1211. r = ofdm_qual_db(status_quality, zd_rate, size);
  1212. ZD_ASSERT(r >= 0);
  1213. if (r < 0)
  1214. r = 0;
  1215. r = (r * 100)/29;
  1216. return r <= 100 ? r : 100;
  1217. }
  1218. static unsigned int log10times100(unsigned int x)
  1219. {
  1220. static const u8 log10[] = {
  1221. 0,
  1222. 0, 30, 47, 60, 69, 77, 84, 90, 95, 100,
  1223. 104, 107, 111, 114, 117, 120, 123, 125, 127, 130,
  1224. 132, 134, 136, 138, 139, 141, 143, 144, 146, 147,
  1225. 149, 150, 151, 153, 154, 155, 156, 157, 159, 160,
  1226. 161, 162, 163, 164, 165, 166, 167, 168, 169, 169,
  1227. 170, 171, 172, 173, 174, 174, 175, 176, 177, 177,
  1228. 178, 179, 179, 180, 181, 181, 182, 183, 183, 184,
  1229. 185, 185, 186, 186, 187, 188, 188, 189, 189, 190,
  1230. 190, 191, 191, 192, 192, 193, 193, 194, 194, 195,
  1231. 195, 196, 196, 197, 197, 198, 198, 199, 199, 200,
  1232. 200, 200, 201, 201, 202, 202, 202, 203, 203, 204,
  1233. 204, 204, 205, 205, 206, 206, 206, 207, 207, 207,
  1234. 208, 208, 208, 209, 209, 210, 210, 210, 211, 211,
  1235. 211, 212, 212, 212, 213, 213, 213, 213, 214, 214,
  1236. 214, 215, 215, 215, 216, 216, 216, 217, 217, 217,
  1237. 217, 218, 218, 218, 219, 219, 219, 219, 220, 220,
  1238. 220, 220, 221, 221, 221, 222, 222, 222, 222, 223,
  1239. 223, 223, 223, 224, 224, 224, 224,
  1240. };
  1241. return x < ARRAY_SIZE(log10) ? log10[x] : 225;
  1242. }
  1243. enum {
  1244. MAX_CCK_EVM_DB = 45,
  1245. };
  1246. static int cck_evm_db(u8 status_quality)
  1247. {
  1248. return (20 * log10times100(status_quality)) / 100;
  1249. }
  1250. static int cck_snr_db(u8 status_quality)
  1251. {
  1252. int r = MAX_CCK_EVM_DB - cck_evm_db(status_quality);
  1253. ZD_ASSERT(r >= 0);
  1254. return r;
  1255. }
  1256. static int cck_qual_percent(u8 status_quality)
  1257. {
  1258. int r;
  1259. r = cck_snr_db(status_quality);
  1260. r = (100*r)/17;
  1261. return r <= 100 ? r : 100;
  1262. }
  1263. static inline u8 zd_rate_from_ofdm_plcp_header(const void *rx_frame)
  1264. {
  1265. return ZD_OFDM | zd_ofdm_plcp_header_rate(rx_frame);
  1266. }
  1267. u8 zd_rx_qual_percent(const void *rx_frame, unsigned int size,
  1268. const struct rx_status *status)
  1269. {
  1270. return (status->frame_status&ZD_RX_OFDM) ?
  1271. ofdm_qual_percent(status->signal_quality_ofdm,
  1272. zd_rate_from_ofdm_plcp_header(rx_frame),
  1273. size) :
  1274. cck_qual_percent(status->signal_quality_cck);
  1275. }
  1276. u8 zd_rx_strength_percent(u8 rssi)
  1277. {
  1278. int r = (rssi*100) / 41;
  1279. if (r > 100)
  1280. r = 100;
  1281. return (u8) r;
  1282. }
  1283. u16 zd_rx_rate(const void *rx_frame, const struct rx_status *status)
  1284. {
  1285. static const u16 ofdm_rates[] = {
  1286. [ZD_OFDM_PLCP_RATE_6M] = 60,
  1287. [ZD_OFDM_PLCP_RATE_9M] = 90,
  1288. [ZD_OFDM_PLCP_RATE_12M] = 120,
  1289. [ZD_OFDM_PLCP_RATE_18M] = 180,
  1290. [ZD_OFDM_PLCP_RATE_24M] = 240,
  1291. [ZD_OFDM_PLCP_RATE_36M] = 360,
  1292. [ZD_OFDM_PLCP_RATE_48M] = 480,
  1293. [ZD_OFDM_PLCP_RATE_54M] = 540,
  1294. };
  1295. u16 rate;
  1296. if (status->frame_status & ZD_RX_OFDM) {
  1297. /* Deals with PLCP OFDM rate (not zd_rates) */
  1298. u8 ofdm_rate = zd_ofdm_plcp_header_rate(rx_frame);
  1299. rate = ofdm_rates[ofdm_rate & 0xf];
  1300. } else {
  1301. switch (zd_cck_plcp_header_signal(rx_frame)) {
  1302. case ZD_CCK_PLCP_SIGNAL_1M:
  1303. rate = 10;
  1304. break;
  1305. case ZD_CCK_PLCP_SIGNAL_2M:
  1306. rate = 20;
  1307. break;
  1308. case ZD_CCK_PLCP_SIGNAL_5M5:
  1309. rate = 55;
  1310. break;
  1311. case ZD_CCK_PLCP_SIGNAL_11M:
  1312. rate = 110;
  1313. break;
  1314. default:
  1315. rate = 0;
  1316. }
  1317. }
  1318. return rate;
  1319. }
  1320. int zd_chip_switch_radio_on(struct zd_chip *chip)
  1321. {
  1322. int r;
  1323. mutex_lock(&chip->mutex);
  1324. r = zd_switch_radio_on(&chip->rf);
  1325. mutex_unlock(&chip->mutex);
  1326. return r;
  1327. }
  1328. int zd_chip_switch_radio_off(struct zd_chip *chip)
  1329. {
  1330. int r;
  1331. mutex_lock(&chip->mutex);
  1332. r = zd_switch_radio_off(&chip->rf);
  1333. mutex_unlock(&chip->mutex);
  1334. return r;
  1335. }
  1336. int zd_chip_enable_int(struct zd_chip *chip)
  1337. {
  1338. int r;
  1339. mutex_lock(&chip->mutex);
  1340. r = zd_usb_enable_int(&chip->usb);
  1341. mutex_unlock(&chip->mutex);
  1342. return r;
  1343. }
  1344. void zd_chip_disable_int(struct zd_chip *chip)
  1345. {
  1346. mutex_lock(&chip->mutex);
  1347. zd_usb_disable_int(&chip->usb);
  1348. mutex_unlock(&chip->mutex);
  1349. }
  1350. int zd_chip_enable_rx(struct zd_chip *chip)
  1351. {
  1352. int r;
  1353. mutex_lock(&chip->mutex);
  1354. r = zd_usb_enable_rx(&chip->usb);
  1355. mutex_unlock(&chip->mutex);
  1356. return r;
  1357. }
  1358. void zd_chip_disable_rx(struct zd_chip *chip)
  1359. {
  1360. mutex_lock(&chip->mutex);
  1361. zd_usb_disable_rx(&chip->usb);
  1362. mutex_unlock(&chip->mutex);
  1363. }
  1364. int zd_rfwritev_locked(struct zd_chip *chip,
  1365. const u32* values, unsigned int count, u8 bits)
  1366. {
  1367. int r;
  1368. unsigned int i;
  1369. for (i = 0; i < count; i++) {
  1370. r = zd_rfwrite_locked(chip, values[i], bits);
  1371. if (r)
  1372. return r;
  1373. }
  1374. return 0;
  1375. }
  1376. /*
  1377. * We can optionally program the RF directly through CR regs, if supported by
  1378. * the hardware. This is much faster than the older method.
  1379. */
  1380. int zd_rfwrite_cr_locked(struct zd_chip *chip, u32 value)
  1381. {
  1382. struct zd_ioreq16 ioreqs[] = {
  1383. { CR244, (value >> 16) & 0xff },
  1384. { CR243, (value >> 8) & 0xff },
  1385. { CR242, value & 0xff },
  1386. };
  1387. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1388. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1389. }
  1390. int zd_rfwritev_cr_locked(struct zd_chip *chip,
  1391. const u32 *values, unsigned int count)
  1392. {
  1393. int r;
  1394. unsigned int i;
  1395. for (i = 0; i < count; i++) {
  1396. r = zd_rfwrite_cr_locked(chip, values[i]);
  1397. if (r)
  1398. return r;
  1399. }
  1400. return 0;
  1401. }
  1402. int zd_chip_set_multicast_hash(struct zd_chip *chip,
  1403. struct zd_mc_hash *hash)
  1404. {
  1405. struct zd_ioreq32 ioreqs[] = {
  1406. { CR_GROUP_HASH_P1, hash->low },
  1407. { CR_GROUP_HASH_P2, hash->high },
  1408. };
  1409. return zd_iowrite32a(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1410. }