zd_chip.c 40 KB

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