zd_chip.c 38 KB

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