zd_chip.c 39 KB

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