zd_chip.c 37 KB

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