zd_chip.c 39 KB

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