wl3501_cs.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * WL3501 Wireless LAN PCMCIA Card Driver for Linux
  4. * Written originally for Linux 2.0.30 by Fox Chen, mhchen@golf.ccl.itri.org.tw
  5. * Ported to 2.2, 2.4 & 2.5 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. * Wireless extensions in 2.4 by Gustavo Niemeyer <niemeyer@conectiva.com>
  7. *
  8. * References used by Fox Chen while writing the original driver for 2.0.30:
  9. *
  10. * 1. WL24xx packet drivers (tooasm.asm)
  11. * 2. Access Point Firmware Interface Specification for IEEE 802.11 SUTRO
  12. * 3. IEEE 802.11
  13. * 4. Linux network driver (/usr/src/linux/drivers/net)
  14. * 5. ISA card driver - wl24.c
  15. * 6. Linux PCMCIA skeleton driver - skeleton.c
  16. * 7. Linux PCMCIA 3c589 network driver - 3c589_cs.c
  17. *
  18. * Tested with WL2400 firmware 1.2, Linux 2.0.30, and pcmcia-cs-2.9.12
  19. * 1. Performance: about 165 Kbytes/sec in TCP/IP with Ad-Hoc mode.
  20. * rsh 192.168.1.3 "dd if=/dev/zero bs=1k count=1000" > /dev/null
  21. * (Specification 2M bits/sec. is about 250 Kbytes/sec., but we must deduct
  22. * ETHER/IP/UDP/TCP header, and acknowledgement overhead)
  23. *
  24. * Tested with Planet AP in 2.4.17, 184 Kbytes/s in UDP in Infrastructure mode,
  25. * 173 Kbytes/s in TCP.
  26. *
  27. * Tested with Planet AP in 2.5.73-bk, 216 Kbytes/s in Infrastructure mode
  28. * with a SMP machine (dual pentium 100), using pktgen, 432 pps (pkt_size = 60)
  29. */
  30. #include <linux/delay.h>
  31. #include <linux/types.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/in.h>
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/if_arp.h>
  38. #include <linux/ioport.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/etherdevice.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/slab.h>
  43. #include <linux/string.h>
  44. #include <linux/wireless.h>
  45. #include <net/cfg80211.h>
  46. #include <net/iw_handler.h>
  47. #include <pcmcia/cistpl.h>
  48. #include <pcmcia/cisreg.h>
  49. #include <pcmcia/ds.h>
  50. #include <asm/io.h>
  51. #include <linux/uaccess.h>
  52. #include "wl3501.h"
  53. #ifndef __i386__
  54. #define slow_down_io()
  55. #endif
  56. /* For rough constant delay */
  57. #define WL3501_NOPLOOP(n) { int x = 0; while (x++ < n) slow_down_io(); }
  58. #define wl3501_outb(a, b) { outb(a, b); slow_down_io(); }
  59. #define wl3501_outb_p(a, b) { outb_p(a, b); slow_down_io(); }
  60. #define wl3501_outsb(a, b, c) { outsb(a, b, c); slow_down_io(); }
  61. #define WL3501_RELEASE_TIMEOUT (25 * HZ)
  62. #define WL3501_MAX_ADHOC_TRIES 16
  63. #define WL3501_RESUME 0
  64. #define WL3501_SUSPEND 1
  65. static int wl3501_config(struct pcmcia_device *link);
  66. static void wl3501_release(struct pcmcia_device *link);
  67. static const struct {
  68. int reg_domain;
  69. int min, max, deflt;
  70. } iw_channel_table[] = {
  71. {
  72. .reg_domain = IW_REG_DOMAIN_FCC,
  73. .min = 1,
  74. .max = 11,
  75. .deflt = 1,
  76. },
  77. {
  78. .reg_domain = IW_REG_DOMAIN_DOC,
  79. .min = 1,
  80. .max = 11,
  81. .deflt = 1,
  82. },
  83. {
  84. .reg_domain = IW_REG_DOMAIN_ETSI,
  85. .min = 1,
  86. .max = 13,
  87. .deflt = 1,
  88. },
  89. {
  90. .reg_domain = IW_REG_DOMAIN_SPAIN,
  91. .min = 10,
  92. .max = 11,
  93. .deflt = 10,
  94. },
  95. {
  96. .reg_domain = IW_REG_DOMAIN_FRANCE,
  97. .min = 10,
  98. .max = 13,
  99. .deflt = 10,
  100. },
  101. {
  102. .reg_domain = IW_REG_DOMAIN_MKK,
  103. .min = 14,
  104. .max = 14,
  105. .deflt = 14,
  106. },
  107. {
  108. .reg_domain = IW_REG_DOMAIN_MKK1,
  109. .min = 1,
  110. .max = 14,
  111. .deflt = 1,
  112. },
  113. {
  114. .reg_domain = IW_REG_DOMAIN_ISRAEL,
  115. .min = 3,
  116. .max = 9,
  117. .deflt = 9,
  118. },
  119. };
  120. /**
  121. * iw_valid_channel - validate channel in regulatory domain
  122. * @reg_comain - regulatory domain
  123. * @channel - channel to validate
  124. *
  125. * Returns 0 if invalid in the specified regulatory domain, non-zero if valid.
  126. */
  127. static int iw_valid_channel(int reg_domain, int channel)
  128. {
  129. int i, rc = 0;
  130. for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
  131. if (reg_domain == iw_channel_table[i].reg_domain) {
  132. rc = channel >= iw_channel_table[i].min &&
  133. channel <= iw_channel_table[i].max;
  134. break;
  135. }
  136. return rc;
  137. }
  138. /**
  139. * iw_default_channel - get default channel for a regulatory domain
  140. * @reg_comain - regulatory domain
  141. *
  142. * Returns the default channel for a regulatory domain
  143. */
  144. static int iw_default_channel(int reg_domain)
  145. {
  146. int i, rc = 1;
  147. for (i = 0; i < ARRAY_SIZE(iw_channel_table); i++)
  148. if (reg_domain == iw_channel_table[i].reg_domain) {
  149. rc = iw_channel_table[i].deflt;
  150. break;
  151. }
  152. return rc;
  153. }
  154. static void iw_set_mgmt_info_element(enum iw_mgmt_info_element_ids id,
  155. struct iw_mgmt_info_element *el,
  156. void *value, int len)
  157. {
  158. el->id = id;
  159. el->len = len;
  160. memcpy(el->data, value, len);
  161. }
  162. static void iw_copy_mgmt_info_element(struct iw_mgmt_info_element *to,
  163. struct iw_mgmt_info_element *from)
  164. {
  165. iw_set_mgmt_info_element(from->id, to, from->data, from->len);
  166. }
  167. static inline void wl3501_switch_page(struct wl3501_card *this, u8 page)
  168. {
  169. wl3501_outb(page, this->base_addr + WL3501_NIC_BSS);
  170. }
  171. /*
  172. * Get Ethernet MAC address.
  173. *
  174. * WARNING: We switch to FPAGE0 and switc back again.
  175. * Making sure there is no other WL function beening called by ISR.
  176. */
  177. static int wl3501_get_flash_mac_addr(struct wl3501_card *this)
  178. {
  179. int base_addr = this->base_addr;
  180. /* get MAC addr */
  181. wl3501_outb(WL3501_BSS_FPAGE3, base_addr + WL3501_NIC_BSS); /* BSS */
  182. wl3501_outb(0x00, base_addr + WL3501_NIC_LMAL); /* LMAL */
  183. wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH); /* LMAH */
  184. /* wait for reading EEPROM */
  185. WL3501_NOPLOOP(100);
  186. this->mac_addr[0] = inb(base_addr + WL3501_NIC_IODPA);
  187. WL3501_NOPLOOP(100);
  188. this->mac_addr[1] = inb(base_addr + WL3501_NIC_IODPA);
  189. WL3501_NOPLOOP(100);
  190. this->mac_addr[2] = inb(base_addr + WL3501_NIC_IODPA);
  191. WL3501_NOPLOOP(100);
  192. this->mac_addr[3] = inb(base_addr + WL3501_NIC_IODPA);
  193. WL3501_NOPLOOP(100);
  194. this->mac_addr[4] = inb(base_addr + WL3501_NIC_IODPA);
  195. WL3501_NOPLOOP(100);
  196. this->mac_addr[5] = inb(base_addr + WL3501_NIC_IODPA);
  197. WL3501_NOPLOOP(100);
  198. this->reg_domain = inb(base_addr + WL3501_NIC_IODPA);
  199. WL3501_NOPLOOP(100);
  200. wl3501_outb(WL3501_BSS_FPAGE0, base_addr + WL3501_NIC_BSS);
  201. wl3501_outb(0x04, base_addr + WL3501_NIC_LMAL);
  202. wl3501_outb(0x40, base_addr + WL3501_NIC_LMAH);
  203. WL3501_NOPLOOP(100);
  204. this->version[0] = inb(base_addr + WL3501_NIC_IODPA);
  205. WL3501_NOPLOOP(100);
  206. this->version[1] = inb(base_addr + WL3501_NIC_IODPA);
  207. /* switch to SRAM Page 0 (for safety) */
  208. wl3501_switch_page(this, WL3501_BSS_SPAGE0);
  209. /* The MAC addr should be 00:60:... */
  210. return this->mac_addr[0] == 0x00 && this->mac_addr[1] == 0x60;
  211. }
  212. /**
  213. * wl3501_set_to_wla - Move 'size' bytes from PC to card
  214. * @dest: Card addressing space
  215. * @src: PC addressing space
  216. * @size: Bytes to move
  217. *
  218. * Move 'size' bytes from PC to card. (Shouldn't be interrupted)
  219. */
  220. static void wl3501_set_to_wla(struct wl3501_card *this, u16 dest, void *src,
  221. int size)
  222. {
  223. /* switch to SRAM Page 0 */
  224. wl3501_switch_page(this, (dest & 0x8000) ? WL3501_BSS_SPAGE1 :
  225. WL3501_BSS_SPAGE0);
  226. /* set LMAL and LMAH */
  227. wl3501_outb(dest & 0xff, this->base_addr + WL3501_NIC_LMAL);
  228. wl3501_outb(((dest >> 8) & 0x7f), this->base_addr + WL3501_NIC_LMAH);
  229. /* rep out to Port A */
  230. wl3501_outsb(this->base_addr + WL3501_NIC_IODPA, src, size);
  231. }
  232. /**
  233. * wl3501_get_from_wla - Move 'size' bytes from card to PC
  234. * @src: Card addressing space
  235. * @dest: PC addressing space
  236. * @size: Bytes to move
  237. *
  238. * Move 'size' bytes from card to PC. (Shouldn't be interrupted)
  239. */
  240. static void wl3501_get_from_wla(struct wl3501_card *this, u16 src, void *dest,
  241. int size)
  242. {
  243. /* switch to SRAM Page 0 */
  244. wl3501_switch_page(this, (src & 0x8000) ? WL3501_BSS_SPAGE1 :
  245. WL3501_BSS_SPAGE0);
  246. /* set LMAL and LMAH */
  247. wl3501_outb(src & 0xff, this->base_addr + WL3501_NIC_LMAL);
  248. wl3501_outb((src >> 8) & 0x7f, this->base_addr + WL3501_NIC_LMAH);
  249. /* rep get from Port A */
  250. insb(this->base_addr + WL3501_NIC_IODPA, dest, size);
  251. }
  252. /*
  253. * Get/Allocate a free Tx Data Buffer
  254. *
  255. * *--------------*-----------------*----------------------------------*
  256. * | PLCP | MAC Header | DST SRC Data ... |
  257. * | (24 bytes) | (30 bytes) | (6) (6) (Ethernet Row Data) |
  258. * *--------------*-----------------*----------------------------------*
  259. * \ \- IEEE 802.11 -/ \-------------- len --------------/
  260. * \-struct wl3501_80211_tx_hdr--/ \-------- Ethernet Frame -------/
  261. *
  262. * Return = Position in Card
  263. */
  264. static u16 wl3501_get_tx_buffer(struct wl3501_card *this, u16 len)
  265. {
  266. u16 next, blk_cnt = 0, zero = 0;
  267. u16 full_len = sizeof(struct wl3501_80211_tx_hdr) + len;
  268. u16 ret = 0;
  269. if (full_len > this->tx_buffer_cnt * 254)
  270. goto out;
  271. ret = this->tx_buffer_head;
  272. while (full_len) {
  273. if (full_len < 254)
  274. full_len = 0;
  275. else
  276. full_len -= 254;
  277. wl3501_get_from_wla(this, this->tx_buffer_head, &next,
  278. sizeof(next));
  279. if (!full_len)
  280. wl3501_set_to_wla(this, this->tx_buffer_head, &zero,
  281. sizeof(zero));
  282. this->tx_buffer_head = next;
  283. blk_cnt++;
  284. /* if buffer is not enough */
  285. if (!next && full_len) {
  286. this->tx_buffer_head = ret;
  287. ret = 0;
  288. goto out;
  289. }
  290. }
  291. this->tx_buffer_cnt -= blk_cnt;
  292. out:
  293. return ret;
  294. }
  295. /*
  296. * Free an allocated Tx Buffer. ptr must be correct position.
  297. */
  298. static void wl3501_free_tx_buffer(struct wl3501_card *this, u16 ptr)
  299. {
  300. /* check if all space is not free */
  301. if (!this->tx_buffer_head)
  302. this->tx_buffer_head = ptr;
  303. else
  304. wl3501_set_to_wla(this, this->tx_buffer_tail,
  305. &ptr, sizeof(ptr));
  306. while (ptr) {
  307. u16 next;
  308. this->tx_buffer_cnt++;
  309. wl3501_get_from_wla(this, ptr, &next, sizeof(next));
  310. this->tx_buffer_tail = ptr;
  311. ptr = next;
  312. }
  313. }
  314. static int wl3501_esbq_req_test(struct wl3501_card *this)
  315. {
  316. u8 tmp = 0;
  317. wl3501_get_from_wla(this, this->esbq_req_head + 3, &tmp, sizeof(tmp));
  318. return tmp & 0x80;
  319. }
  320. static void wl3501_esbq_req(struct wl3501_card *this, u16 *ptr)
  321. {
  322. u16 tmp = 0;
  323. wl3501_set_to_wla(this, this->esbq_req_head, ptr, 2);
  324. wl3501_set_to_wla(this, this->esbq_req_head + 2, &tmp, sizeof(tmp));
  325. this->esbq_req_head += 4;
  326. if (this->esbq_req_head >= this->esbq_req_end)
  327. this->esbq_req_head = this->esbq_req_start;
  328. }
  329. static int wl3501_esbq_exec(struct wl3501_card *this, void *sig, int sig_size)
  330. {
  331. int rc = -EIO;
  332. if (wl3501_esbq_req_test(this)) {
  333. u16 ptr = wl3501_get_tx_buffer(this, sig_size);
  334. if (ptr) {
  335. wl3501_set_to_wla(this, ptr, sig, sig_size);
  336. wl3501_esbq_req(this, &ptr);
  337. rc = 0;
  338. }
  339. }
  340. return rc;
  341. }
  342. static int wl3501_request_mib(struct wl3501_card *this, u8 index, void *bf)
  343. {
  344. struct wl3501_get_req sig = {
  345. .sig_id = WL3501_SIG_GET_REQ,
  346. .mib_attrib = index,
  347. };
  348. unsigned long flags;
  349. int rc = -EIO;
  350. spin_lock_irqsave(&this->lock, flags);
  351. if (wl3501_esbq_req_test(this)) {
  352. u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
  353. if (ptr) {
  354. wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
  355. wl3501_esbq_req(this, &ptr);
  356. this->sig_get_confirm.mib_status = 255;
  357. rc = 0;
  358. }
  359. }
  360. spin_unlock_irqrestore(&this->lock, flags);
  361. return rc;
  362. }
  363. static int wl3501_get_mib_value(struct wl3501_card *this, u8 index,
  364. void *bf, int size)
  365. {
  366. int rc;
  367. rc = wl3501_request_mib(this, index, bf);
  368. if (rc)
  369. return rc;
  370. rc = wait_event_interruptible(this->wait,
  371. this->sig_get_confirm.mib_status != 255);
  372. if (rc)
  373. return rc;
  374. memcpy(bf, this->sig_get_confirm.mib_value, size);
  375. return 0;
  376. }
  377. static int wl3501_pwr_mgmt(struct wl3501_card *this, int suspend)
  378. {
  379. struct wl3501_pwr_mgmt_req sig = {
  380. .sig_id = WL3501_SIG_PWR_MGMT_REQ,
  381. .pwr_save = suspend,
  382. .wake_up = !suspend,
  383. .receive_dtims = 10,
  384. };
  385. unsigned long flags;
  386. int rc = -EIO;
  387. spin_lock_irqsave(&this->lock, flags);
  388. if (wl3501_esbq_req_test(this)) {
  389. u16 ptr = wl3501_get_tx_buffer(this, sizeof(sig));
  390. if (ptr) {
  391. wl3501_set_to_wla(this, ptr, &sig, sizeof(sig));
  392. wl3501_esbq_req(this, &ptr);
  393. this->sig_pwr_mgmt_confirm.status = 255;
  394. spin_unlock_irqrestore(&this->lock, flags);
  395. rc = wait_event_interruptible(this->wait,
  396. this->sig_pwr_mgmt_confirm.status != 255);
  397. printk(KERN_INFO "%s: %s status=%d\n", __func__,
  398. suspend ? "suspend" : "resume",
  399. this->sig_pwr_mgmt_confirm.status);
  400. goto out;
  401. }
  402. }
  403. spin_unlock_irqrestore(&this->lock, flags);
  404. out:
  405. return rc;
  406. }
  407. /**
  408. * wl3501_send_pkt - Send a packet.
  409. * @this - card
  410. *
  411. * Send a packet.
  412. *
  413. * data = Ethernet raw frame. (e.g. data[0] - data[5] is Dest MAC Addr,
  414. * data[6] - data[11] is Src MAC Addr)
  415. * Ref: IEEE 802.11
  416. */
  417. static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
  418. {
  419. u16 bf, sig_bf, next, tmplen, pktlen;
  420. struct wl3501_md_req sig = {
  421. .sig_id = WL3501_SIG_MD_REQ,
  422. };
  423. size_t sig_addr_len = sizeof(sig.addr);
  424. u8 *pdata = (char *)data;
  425. int rc = -EIO;
  426. if (wl3501_esbq_req_test(this)) {
  427. sig_bf = wl3501_get_tx_buffer(this, sizeof(sig));
  428. rc = -ENOMEM;
  429. if (!sig_bf) /* No free buffer available */
  430. goto out;
  431. bf = wl3501_get_tx_buffer(this, len + 26 + 24);
  432. if (!bf) {
  433. /* No free buffer available */
  434. wl3501_free_tx_buffer(this, sig_bf);
  435. goto out;
  436. }
  437. rc = 0;
  438. memcpy(&sig.addr, pdata, sig_addr_len);
  439. pktlen = len - sig_addr_len;
  440. pdata += sig_addr_len;
  441. sig.data = bf;
  442. if (((*pdata) * 256 + (*(pdata + 1))) > 1500) {
  443. u8 addr4[ETH_ALEN] = {
  444. [0] = 0xAA, [1] = 0xAA, [2] = 0x03, [4] = 0x00,
  445. };
  446. wl3501_set_to_wla(this, bf + 2 +
  447. offsetof(struct wl3501_tx_hdr, addr4),
  448. addr4, sizeof(addr4));
  449. sig.size = pktlen + 24 + 4 + 6;
  450. if (pktlen > (254 - sizeof(struct wl3501_tx_hdr))) {
  451. tmplen = 254 - sizeof(struct wl3501_tx_hdr);
  452. pktlen -= tmplen;
  453. } else {
  454. tmplen = pktlen;
  455. pktlen = 0;
  456. }
  457. wl3501_set_to_wla(this,
  458. bf + 2 + sizeof(struct wl3501_tx_hdr),
  459. pdata, tmplen);
  460. pdata += tmplen;
  461. wl3501_get_from_wla(this, bf, &next, sizeof(next));
  462. bf = next;
  463. } else {
  464. sig.size = pktlen + 24 + 4 - 2;
  465. pdata += 2;
  466. pktlen -= 2;
  467. if (pktlen > (254 - sizeof(struct wl3501_tx_hdr) + 6)) {
  468. tmplen = 254 - sizeof(struct wl3501_tx_hdr) + 6;
  469. pktlen -= tmplen;
  470. } else {
  471. tmplen = pktlen;
  472. pktlen = 0;
  473. }
  474. wl3501_set_to_wla(this, bf + 2 +
  475. offsetof(struct wl3501_tx_hdr, addr4),
  476. pdata, tmplen);
  477. pdata += tmplen;
  478. wl3501_get_from_wla(this, bf, &next, sizeof(next));
  479. bf = next;
  480. }
  481. while (pktlen > 0) {
  482. if (pktlen > 254) {
  483. tmplen = 254;
  484. pktlen -= 254;
  485. } else {
  486. tmplen = pktlen;
  487. pktlen = 0;
  488. }
  489. wl3501_set_to_wla(this, bf + 2, pdata, tmplen);
  490. pdata += tmplen;
  491. wl3501_get_from_wla(this, bf, &next, sizeof(next));
  492. bf = next;
  493. }
  494. wl3501_set_to_wla(this, sig_bf, &sig, sizeof(sig));
  495. wl3501_esbq_req(this, &sig_bf);
  496. }
  497. out:
  498. return rc;
  499. }
  500. static int wl3501_mgmt_resync(struct wl3501_card *this)
  501. {
  502. struct wl3501_resync_req sig = {
  503. .sig_id = WL3501_SIG_RESYNC_REQ,
  504. };
  505. return wl3501_esbq_exec(this, &sig, sizeof(sig));
  506. }
  507. static inline int wl3501_fw_bss_type(struct wl3501_card *this)
  508. {
  509. return this->net_type == IW_MODE_INFRA ? WL3501_NET_TYPE_INFRA :
  510. WL3501_NET_TYPE_ADHOC;
  511. }
  512. static inline int wl3501_fw_cap_info(struct wl3501_card *this)
  513. {
  514. return this->net_type == IW_MODE_INFRA ? WL3501_MGMT_CAPABILITY_ESS :
  515. WL3501_MGMT_CAPABILITY_IBSS;
  516. }
  517. static int wl3501_mgmt_scan(struct wl3501_card *this, u16 chan_time)
  518. {
  519. struct wl3501_scan_req sig = {
  520. .sig_id = WL3501_SIG_SCAN_REQ,
  521. .scan_type = WL3501_SCAN_TYPE_ACTIVE,
  522. .probe_delay = 0x10,
  523. .min_chan_time = chan_time,
  524. .max_chan_time = chan_time,
  525. .bss_type = wl3501_fw_bss_type(this),
  526. };
  527. this->bss_cnt = this->join_sta_bss = 0;
  528. return wl3501_esbq_exec(this, &sig, sizeof(sig));
  529. }
  530. static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
  531. {
  532. struct wl3501_join_req sig = {
  533. .sig_id = WL3501_SIG_JOIN_REQ,
  534. .timeout = 10,
  535. .req.ds_pset = {
  536. .el = {
  537. .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
  538. .len = 1,
  539. },
  540. .chan = this->chan,
  541. },
  542. };
  543. memcpy(&sig.req, &this->bss_set[stas].req, sizeof(sig.req));
  544. return wl3501_esbq_exec(this, &sig, sizeof(sig));
  545. }
  546. static int wl3501_mgmt_start(struct wl3501_card *this)
  547. {
  548. struct wl3501_start_req sig = {
  549. .sig_id = WL3501_SIG_START_REQ,
  550. .beacon_period = 400,
  551. .dtim_period = 1,
  552. .ds_pset = {
  553. .el = {
  554. .id = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
  555. .len = 1,
  556. },
  557. .chan = this->chan,
  558. },
  559. .bss_basic_rset = {
  560. .el = {
  561. .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
  562. .len = 2,
  563. },
  564. .data_rate_labels = {
  565. [0] = IW_MGMT_RATE_LABEL_MANDATORY |
  566. IW_MGMT_RATE_LABEL_1MBIT,
  567. [1] = IW_MGMT_RATE_LABEL_MANDATORY |
  568. IW_MGMT_RATE_LABEL_2MBIT,
  569. },
  570. },
  571. .operational_rset = {
  572. .el = {
  573. .id = IW_MGMT_INFO_ELEMENT_SUPPORTED_RATES,
  574. .len = 2,
  575. },
  576. .data_rate_labels = {
  577. [0] = IW_MGMT_RATE_LABEL_MANDATORY |
  578. IW_MGMT_RATE_LABEL_1MBIT,
  579. [1] = IW_MGMT_RATE_LABEL_MANDATORY |
  580. IW_MGMT_RATE_LABEL_2MBIT,
  581. },
  582. },
  583. .ibss_pset = {
  584. .el = {
  585. .id = IW_MGMT_INFO_ELEMENT_IBSS_PARAMETER_SET,
  586. .len = 2,
  587. },
  588. .atim_window = 10,
  589. },
  590. .bss_type = wl3501_fw_bss_type(this),
  591. .cap_info = wl3501_fw_cap_info(this),
  592. };
  593. iw_copy_mgmt_info_element(&sig.ssid.el, &this->essid.el);
  594. iw_copy_mgmt_info_element(&this->keep_essid.el, &this->essid.el);
  595. return wl3501_esbq_exec(this, &sig, sizeof(sig));
  596. }
  597. static void wl3501_mgmt_scan_confirm(struct wl3501_card *this, u16 addr)
  598. {
  599. u16 i = 0;
  600. int matchflag = 0;
  601. struct wl3501_scan_confirm sig;
  602. pr_debug("entry");
  603. wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
  604. if (sig.status == WL3501_STATUS_SUCCESS) {
  605. pr_debug("success");
  606. if ((this->net_type == IW_MODE_INFRA &&
  607. (sig.req.cap_info & WL3501_MGMT_CAPABILITY_ESS)) ||
  608. (this->net_type == IW_MODE_ADHOC &&
  609. (sig.req.cap_info & WL3501_MGMT_CAPABILITY_IBSS)) ||
  610. this->net_type == IW_MODE_AUTO) {
  611. if (!this->essid.el.len)
  612. matchflag = 1;
  613. else if (this->essid.el.len == 3 &&
  614. !memcmp(this->essid.essid, "ANY", 3))
  615. matchflag = 1;
  616. else if (this->essid.el.len != sig.req.ssid.el.len)
  617. matchflag = 0;
  618. else if (memcmp(this->essid.essid, sig.req.ssid.essid,
  619. this->essid.el.len))
  620. matchflag = 0;
  621. else
  622. matchflag = 1;
  623. if (matchflag) {
  624. for (i = 0; i < this->bss_cnt; i++) {
  625. if (ether_addr_equal_unaligned(this->bss_set[i].req.bssid,
  626. sig.req.bssid)) {
  627. matchflag = 0;
  628. break;
  629. }
  630. }
  631. }
  632. if (matchflag && (i < 20)) {
  633. memcpy(&this->bss_set[i].req,
  634. &sig.req, sizeof(sig.req));
  635. this->bss_cnt++;
  636. this->rssi = sig.rssi;
  637. this->bss_set[i].rssi = sig.rssi;
  638. }
  639. }
  640. } else if (sig.status == WL3501_STATUS_TIMEOUT) {
  641. pr_debug("timeout");
  642. this->join_sta_bss = 0;
  643. for (i = this->join_sta_bss; i < this->bss_cnt; i++)
  644. if (!wl3501_mgmt_join(this, i))
  645. break;
  646. this->join_sta_bss = i;
  647. if (this->join_sta_bss == this->bss_cnt) {
  648. if (this->net_type == IW_MODE_INFRA)
  649. wl3501_mgmt_scan(this, 100);
  650. else {
  651. this->adhoc_times++;
  652. if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
  653. wl3501_mgmt_start(this);
  654. else
  655. wl3501_mgmt_scan(this, 100);
  656. }
  657. }
  658. }
  659. }
  660. /**
  661. * wl3501_block_interrupt - Mask interrupt from SUTRO
  662. * @this - card
  663. *
  664. * Mask interrupt from SUTRO. (i.e. SUTRO cannot interrupt the HOST)
  665. * Return: 1 if interrupt is originally enabled
  666. */
  667. static int wl3501_block_interrupt(struct wl3501_card *this)
  668. {
  669. u8 old = inb(this->base_addr + WL3501_NIC_GCR);
  670. u8 new = old & (~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC |
  671. WL3501_GCR_ENECINT));
  672. wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
  673. return old & WL3501_GCR_ENECINT;
  674. }
  675. /**
  676. * wl3501_unblock_interrupt - Enable interrupt from SUTRO
  677. * @this - card
  678. *
  679. * Enable interrupt from SUTRO. (i.e. SUTRO can interrupt the HOST)
  680. * Return: 1 if interrupt is originally enabled
  681. */
  682. static int wl3501_unblock_interrupt(struct wl3501_card *this)
  683. {
  684. u8 old = inb(this->base_addr + WL3501_NIC_GCR);
  685. u8 new = (old & ~(WL3501_GCR_ECINT | WL3501_GCR_INT2EC)) |
  686. WL3501_GCR_ENECINT;
  687. wl3501_outb(new, this->base_addr + WL3501_NIC_GCR);
  688. return old & WL3501_GCR_ENECINT;
  689. }
  690. /**
  691. * wl3501_receive - Receive data from Receive Queue.
  692. *
  693. * Receive data from Receive Queue.
  694. *
  695. * @this: card
  696. * @bf: address of host
  697. * @size: size of buffer.
  698. */
  699. static u16 wl3501_receive(struct wl3501_card *this, u8 *bf, u16 size)
  700. {
  701. u16 next_addr, next_addr1;
  702. u8 *data = bf + 12;
  703. size -= 12;
  704. wl3501_get_from_wla(this, this->start_seg + 2,
  705. &next_addr, sizeof(next_addr));
  706. if (size > WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr)) {
  707. wl3501_get_from_wla(this,
  708. this->start_seg +
  709. sizeof(struct wl3501_rx_hdr), data,
  710. WL3501_BLKSZ -
  711. sizeof(struct wl3501_rx_hdr));
  712. size -= WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
  713. data += WL3501_BLKSZ - sizeof(struct wl3501_rx_hdr);
  714. } else {
  715. wl3501_get_from_wla(this,
  716. this->start_seg +
  717. sizeof(struct wl3501_rx_hdr),
  718. data, size);
  719. size = 0;
  720. }
  721. while (size > 0) {
  722. if (size > WL3501_BLKSZ - 5) {
  723. wl3501_get_from_wla(this, next_addr + 5, data,
  724. WL3501_BLKSZ - 5);
  725. size -= WL3501_BLKSZ - 5;
  726. data += WL3501_BLKSZ - 5;
  727. wl3501_get_from_wla(this, next_addr + 2, &next_addr1,
  728. sizeof(next_addr1));
  729. next_addr = next_addr1;
  730. } else {
  731. wl3501_get_from_wla(this, next_addr + 5, data, size);
  732. size = 0;
  733. }
  734. }
  735. return 0;
  736. }
  737. static void wl3501_esbq_req_free(struct wl3501_card *this)
  738. {
  739. u8 tmp;
  740. u16 addr;
  741. if (this->esbq_req_head == this->esbq_req_tail)
  742. goto out;
  743. wl3501_get_from_wla(this, this->esbq_req_tail + 3, &tmp, sizeof(tmp));
  744. if (!(tmp & 0x80))
  745. goto out;
  746. wl3501_get_from_wla(this, this->esbq_req_tail, &addr, sizeof(addr));
  747. wl3501_free_tx_buffer(this, addr);
  748. this->esbq_req_tail += 4;
  749. if (this->esbq_req_tail >= this->esbq_req_end)
  750. this->esbq_req_tail = this->esbq_req_start;
  751. out:
  752. return;
  753. }
  754. static int wl3501_esbq_confirm(struct wl3501_card *this)
  755. {
  756. u8 tmp;
  757. wl3501_get_from_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
  758. return tmp & 0x80;
  759. }
  760. static void wl3501_online(struct net_device *dev)
  761. {
  762. struct wl3501_card *this = netdev_priv(dev);
  763. printk(KERN_INFO "%s: Wireless LAN online. BSSID: %pM\n",
  764. dev->name, this->bssid);
  765. netif_wake_queue(dev);
  766. }
  767. static void wl3501_esbq_confirm_done(struct wl3501_card *this)
  768. {
  769. u8 tmp = 0;
  770. wl3501_set_to_wla(this, this->esbq_confirm + 3, &tmp, sizeof(tmp));
  771. this->esbq_confirm += 4;
  772. if (this->esbq_confirm >= this->esbq_confirm_end)
  773. this->esbq_confirm = this->esbq_confirm_start;
  774. }
  775. static int wl3501_mgmt_auth(struct wl3501_card *this)
  776. {
  777. struct wl3501_auth_req sig = {
  778. .sig_id = WL3501_SIG_AUTH_REQ,
  779. .type = WL3501_SYS_TYPE_OPEN,
  780. .timeout = 1000,
  781. };
  782. pr_debug("entry");
  783. memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
  784. return wl3501_esbq_exec(this, &sig, sizeof(sig));
  785. }
  786. static int wl3501_mgmt_association(struct wl3501_card *this)
  787. {
  788. struct wl3501_assoc_req sig = {
  789. .sig_id = WL3501_SIG_ASSOC_REQ,
  790. .timeout = 1000,
  791. .listen_interval = 5,
  792. .cap_info = this->cap_info,
  793. };
  794. pr_debug("entry");
  795. memcpy(sig.mac_addr, this->bssid, ETH_ALEN);
  796. return wl3501_esbq_exec(this, &sig, sizeof(sig));
  797. }
  798. static void wl3501_mgmt_join_confirm(struct net_device *dev, u16 addr)
  799. {
  800. struct wl3501_card *this = netdev_priv(dev);
  801. struct wl3501_join_confirm sig;
  802. pr_debug("entry");
  803. wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
  804. if (sig.status == WL3501_STATUS_SUCCESS) {
  805. if (this->net_type == IW_MODE_INFRA) {
  806. if (this->join_sta_bss < this->bss_cnt) {
  807. const int i = this->join_sta_bss;
  808. memcpy(this->bssid,
  809. this->bss_set[i].req.bssid, ETH_ALEN);
  810. this->chan = this->bss_set[i].req.ds_pset.chan;
  811. iw_copy_mgmt_info_element(&this->keep_essid.el,
  812. &this->bss_set[i].req.ssid.el);
  813. wl3501_mgmt_auth(this);
  814. }
  815. } else {
  816. const int i = this->join_sta_bss;
  817. memcpy(&this->bssid, &this->bss_set[i].req.bssid, ETH_ALEN);
  818. this->chan = this->bss_set[i].req.ds_pset.chan;
  819. iw_copy_mgmt_info_element(&this->keep_essid.el,
  820. &this->bss_set[i].req.ssid.el);
  821. wl3501_online(dev);
  822. }
  823. } else {
  824. int i;
  825. this->join_sta_bss++;
  826. for (i = this->join_sta_bss; i < this->bss_cnt; i++)
  827. if (!wl3501_mgmt_join(this, i))
  828. break;
  829. this->join_sta_bss = i;
  830. if (this->join_sta_bss == this->bss_cnt) {
  831. if (this->net_type == IW_MODE_INFRA)
  832. wl3501_mgmt_scan(this, 100);
  833. else {
  834. this->adhoc_times++;
  835. if (this->adhoc_times > WL3501_MAX_ADHOC_TRIES)
  836. wl3501_mgmt_start(this);
  837. else
  838. wl3501_mgmt_scan(this, 100);
  839. }
  840. }
  841. }
  842. }
  843. static inline void wl3501_alarm_interrupt(struct net_device *dev,
  844. struct wl3501_card *this)
  845. {
  846. if (this->net_type == IW_MODE_INFRA) {
  847. printk(KERN_INFO "Wireless LAN offline\n");
  848. netif_stop_queue(dev);
  849. wl3501_mgmt_resync(this);
  850. }
  851. }
  852. static inline void wl3501_md_confirm_interrupt(struct net_device *dev,
  853. struct wl3501_card *this,
  854. u16 addr)
  855. {
  856. struct wl3501_md_confirm sig;
  857. pr_debug("entry");
  858. wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
  859. wl3501_free_tx_buffer(this, sig.data);
  860. if (netif_queue_stopped(dev))
  861. netif_wake_queue(dev);
  862. }
  863. static inline void wl3501_md_ind_interrupt(struct net_device *dev,
  864. struct wl3501_card *this, u16 addr)
  865. {
  866. struct wl3501_md_ind sig;
  867. struct sk_buff *skb;
  868. u8 rssi, addr4[ETH_ALEN];
  869. u16 pkt_len;
  870. wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
  871. this->start_seg = sig.data;
  872. wl3501_get_from_wla(this,
  873. sig.data + offsetof(struct wl3501_rx_hdr, rssi),
  874. &rssi, sizeof(rssi));
  875. this->rssi = rssi <= 63 ? (rssi * 100) / 64 : 255;
  876. wl3501_get_from_wla(this,
  877. sig.data +
  878. offsetof(struct wl3501_rx_hdr, addr4),
  879. &addr4, sizeof(addr4));
  880. if (!(addr4[0] == 0xAA && addr4[1] == 0xAA &&
  881. addr4[2] == 0x03 && addr4[4] == 0x00)) {
  882. printk(KERN_INFO "Unsupported packet type!\n");
  883. return;
  884. }
  885. pkt_len = sig.size + 12 - 24 - 4 - 6;
  886. skb = dev_alloc_skb(pkt_len + 5);
  887. if (!skb) {
  888. printk(KERN_WARNING "%s: Can't alloc a sk_buff of size %d.\n",
  889. dev->name, pkt_len);
  890. dev->stats.rx_dropped++;
  891. } else {
  892. skb->dev = dev;
  893. skb_reserve(skb, 2); /* IP headers on 16 bytes boundaries */
  894. skb_copy_to_linear_data(skb, (unsigned char *)&sig.addr,
  895. sizeof(sig.addr));
  896. wl3501_receive(this, skb->data, pkt_len);
  897. skb_put(skb, pkt_len);
  898. skb->protocol = eth_type_trans(skb, dev);
  899. dev->stats.rx_packets++;
  900. dev->stats.rx_bytes += skb->len;
  901. netif_rx(skb);
  902. }
  903. }
  904. static inline void wl3501_get_confirm_interrupt(struct wl3501_card *this,
  905. u16 addr, void *sig, int size)
  906. {
  907. pr_debug("entry");
  908. wl3501_get_from_wla(this, addr, &this->sig_get_confirm,
  909. sizeof(this->sig_get_confirm));
  910. wake_up(&this->wait);
  911. }
  912. static inline void wl3501_start_confirm_interrupt(struct net_device *dev,
  913. struct wl3501_card *this,
  914. u16 addr)
  915. {
  916. struct wl3501_start_confirm sig;
  917. pr_debug("entry");
  918. wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
  919. if (sig.status == WL3501_STATUS_SUCCESS)
  920. netif_wake_queue(dev);
  921. }
  922. static inline void wl3501_assoc_confirm_interrupt(struct net_device *dev,
  923. u16 addr)
  924. {
  925. struct wl3501_card *this = netdev_priv(dev);
  926. struct wl3501_assoc_confirm sig;
  927. pr_debug("entry");
  928. wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
  929. if (sig.status == WL3501_STATUS_SUCCESS)
  930. wl3501_online(dev);
  931. }
  932. static inline void wl3501_auth_confirm_interrupt(struct wl3501_card *this,
  933. u16 addr)
  934. {
  935. struct wl3501_auth_confirm sig;
  936. pr_debug("entry");
  937. wl3501_get_from_wla(this, addr, &sig, sizeof(sig));
  938. if (sig.status == WL3501_STATUS_SUCCESS)
  939. wl3501_mgmt_association(this);
  940. else
  941. wl3501_mgmt_resync(this);
  942. }
  943. static inline void wl3501_rx_interrupt(struct net_device *dev)
  944. {
  945. int morepkts;
  946. u16 addr;
  947. u8 sig_id;
  948. struct wl3501_card *this = netdev_priv(dev);
  949. pr_debug("entry");
  950. loop:
  951. morepkts = 0;
  952. if (!wl3501_esbq_confirm(this))
  953. goto free;
  954. wl3501_get_from_wla(this, this->esbq_confirm, &addr, sizeof(addr));
  955. wl3501_get_from_wla(this, addr + 2, &sig_id, sizeof(sig_id));
  956. switch (sig_id) {
  957. case WL3501_SIG_DEAUTH_IND:
  958. case WL3501_SIG_DISASSOC_IND:
  959. case WL3501_SIG_ALARM:
  960. wl3501_alarm_interrupt(dev, this);
  961. break;
  962. case WL3501_SIG_MD_CONFIRM:
  963. wl3501_md_confirm_interrupt(dev, this, addr);
  964. break;
  965. case WL3501_SIG_MD_IND:
  966. wl3501_md_ind_interrupt(dev, this, addr);
  967. break;
  968. case WL3501_SIG_GET_CONFIRM:
  969. wl3501_get_confirm_interrupt(this, addr,
  970. &this->sig_get_confirm,
  971. sizeof(this->sig_get_confirm));
  972. break;
  973. case WL3501_SIG_PWR_MGMT_CONFIRM:
  974. wl3501_get_confirm_interrupt(this, addr,
  975. &this->sig_pwr_mgmt_confirm,
  976. sizeof(this->sig_pwr_mgmt_confirm));
  977. break;
  978. case WL3501_SIG_START_CONFIRM:
  979. wl3501_start_confirm_interrupt(dev, this, addr);
  980. break;
  981. case WL3501_SIG_SCAN_CONFIRM:
  982. wl3501_mgmt_scan_confirm(this, addr);
  983. break;
  984. case WL3501_SIG_JOIN_CONFIRM:
  985. wl3501_mgmt_join_confirm(dev, addr);
  986. break;
  987. case WL3501_SIG_ASSOC_CONFIRM:
  988. wl3501_assoc_confirm_interrupt(dev, addr);
  989. break;
  990. case WL3501_SIG_AUTH_CONFIRM:
  991. wl3501_auth_confirm_interrupt(this, addr);
  992. break;
  993. case WL3501_SIG_RESYNC_CONFIRM:
  994. wl3501_mgmt_resync(this); /* FIXME: should be resync_confirm */
  995. break;
  996. }
  997. wl3501_esbq_confirm_done(this);
  998. morepkts = 1;
  999. /* free request if necessary */
  1000. free:
  1001. wl3501_esbq_req_free(this);
  1002. if (morepkts)
  1003. goto loop;
  1004. }
  1005. static inline void wl3501_ack_interrupt(struct wl3501_card *this)
  1006. {
  1007. wl3501_outb(WL3501_GCR_ECINT, this->base_addr + WL3501_NIC_GCR);
  1008. }
  1009. /**
  1010. * wl3501_interrupt - Hardware interrupt from card.
  1011. * @irq - Interrupt number
  1012. * @dev_id - net_device
  1013. *
  1014. * We must acknowledge the interrupt as soon as possible, and block the
  1015. * interrupt from the same card immediately to prevent re-entry.
  1016. *
  1017. * Before accessing the Control_Status_Block, we must lock SUTRO first.
  1018. * On the other hand, to prevent SUTRO from malfunctioning, we must
  1019. * unlock the SUTRO as soon as possible.
  1020. */
  1021. static irqreturn_t wl3501_interrupt(int irq, void *dev_id)
  1022. {
  1023. struct net_device *dev = dev_id;
  1024. struct wl3501_card *this;
  1025. this = netdev_priv(dev);
  1026. spin_lock(&this->lock);
  1027. wl3501_ack_interrupt(this);
  1028. wl3501_block_interrupt(this);
  1029. wl3501_rx_interrupt(dev);
  1030. wl3501_unblock_interrupt(this);
  1031. spin_unlock(&this->lock);
  1032. return IRQ_HANDLED;
  1033. }
  1034. static int wl3501_reset_board(struct wl3501_card *this)
  1035. {
  1036. u8 tmp = 0;
  1037. int i, rc = 0;
  1038. /* Coreset */
  1039. wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
  1040. wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
  1041. wl3501_outb_p(WL3501_GCR_CORESET, this->base_addr + WL3501_NIC_GCR);
  1042. /* Reset SRAM 0x480 to zero */
  1043. wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
  1044. /* Start up */
  1045. wl3501_outb_p(0, this->base_addr + WL3501_NIC_GCR);
  1046. WL3501_NOPLOOP(1024 * 50);
  1047. wl3501_unblock_interrupt(this); /* acme: was commented */
  1048. /* Polling Self_Test_Status */
  1049. for (i = 0; i < 10000; i++) {
  1050. wl3501_get_from_wla(this, 0x480, &tmp, sizeof(tmp));
  1051. if (tmp == 'W') {
  1052. /* firmware complete all test successfully */
  1053. tmp = 'A';
  1054. wl3501_set_to_wla(this, 0x480, &tmp, sizeof(tmp));
  1055. goto out;
  1056. }
  1057. WL3501_NOPLOOP(10);
  1058. }
  1059. printk(KERN_WARNING "%s: failed to reset the board!\n", __func__);
  1060. rc = -ENODEV;
  1061. out:
  1062. return rc;
  1063. }
  1064. static int wl3501_init_firmware(struct wl3501_card *this)
  1065. {
  1066. u16 ptr, next;
  1067. int rc = wl3501_reset_board(this);
  1068. if (rc)
  1069. goto fail;
  1070. this->card_name[0] = '\0';
  1071. wl3501_get_from_wla(this, 0x1a00,
  1072. this->card_name, sizeof(this->card_name));
  1073. this->card_name[sizeof(this->card_name) - 1] = '\0';
  1074. this->firmware_date[0] = '\0';
  1075. wl3501_get_from_wla(this, 0x1a40,
  1076. this->firmware_date, sizeof(this->firmware_date));
  1077. this->firmware_date[sizeof(this->firmware_date) - 1] = '\0';
  1078. /* Switch to SRAM Page 0 */
  1079. wl3501_switch_page(this, WL3501_BSS_SPAGE0);
  1080. /* Read parameter from card */
  1081. wl3501_get_from_wla(this, 0x482, &this->esbq_req_start, 2);
  1082. wl3501_get_from_wla(this, 0x486, &this->esbq_req_end, 2);
  1083. wl3501_get_from_wla(this, 0x488, &this->esbq_confirm_start, 2);
  1084. wl3501_get_from_wla(this, 0x48c, &this->esbq_confirm_end, 2);
  1085. wl3501_get_from_wla(this, 0x48e, &this->tx_buffer_head, 2);
  1086. wl3501_get_from_wla(this, 0x492, &this->tx_buffer_size, 2);
  1087. this->esbq_req_tail = this->esbq_req_head = this->esbq_req_start;
  1088. this->esbq_req_end += this->esbq_req_start;
  1089. this->esbq_confirm = this->esbq_confirm_start;
  1090. this->esbq_confirm_end += this->esbq_confirm_start;
  1091. /* Initial Tx Buffer */
  1092. this->tx_buffer_cnt = 1;
  1093. ptr = this->tx_buffer_head;
  1094. next = ptr + WL3501_BLKSZ;
  1095. while ((next - this->tx_buffer_head) < this->tx_buffer_size) {
  1096. this->tx_buffer_cnt++;
  1097. wl3501_set_to_wla(this, ptr, &next, sizeof(next));
  1098. ptr = next;
  1099. next = ptr + WL3501_BLKSZ;
  1100. }
  1101. rc = 0;
  1102. next = 0;
  1103. wl3501_set_to_wla(this, ptr, &next, sizeof(next));
  1104. this->tx_buffer_tail = ptr;
  1105. out:
  1106. return rc;
  1107. fail:
  1108. printk(KERN_WARNING "%s: failed!\n", __func__);
  1109. goto out;
  1110. }
  1111. static int wl3501_close(struct net_device *dev)
  1112. {
  1113. struct wl3501_card *this = netdev_priv(dev);
  1114. unsigned long flags;
  1115. struct pcmcia_device *link;
  1116. link = this->p_dev;
  1117. spin_lock_irqsave(&this->lock, flags);
  1118. link->open--;
  1119. /* Stop wl3501_hard_start_xmit() from now on */
  1120. netif_stop_queue(dev);
  1121. wl3501_ack_interrupt(this);
  1122. /* Mask interrupts from the SUTRO */
  1123. wl3501_block_interrupt(this);
  1124. printk(KERN_INFO "%s: WL3501 closed\n", dev->name);
  1125. spin_unlock_irqrestore(&this->lock, flags);
  1126. return 0;
  1127. }
  1128. /**
  1129. * wl3501_reset - Reset the SUTRO.
  1130. * @dev - network device
  1131. *
  1132. * It is almost the same as wl3501_open(). In fact, we may just wl3501_close()
  1133. * and wl3501_open() again, but I wouldn't like to free_irq() when the driver
  1134. * is running. It seems to be dangerous.
  1135. */
  1136. static int wl3501_reset(struct net_device *dev)
  1137. {
  1138. struct wl3501_card *this = netdev_priv(dev);
  1139. int rc = -ENODEV;
  1140. unsigned long flags;
  1141. spin_lock_irqsave(&this->lock, flags);
  1142. wl3501_block_interrupt(this);
  1143. if (wl3501_init_firmware(this)) {
  1144. printk(KERN_WARNING "%s: Can't initialize Firmware!\n",
  1145. dev->name);
  1146. /* Free IRQ, and mark IRQ as unused */
  1147. free_irq(dev->irq, dev);
  1148. goto out;
  1149. }
  1150. /*
  1151. * Queue has to be started only when the Card is Started
  1152. */
  1153. netif_stop_queue(dev);
  1154. this->adhoc_times = 0;
  1155. wl3501_ack_interrupt(this);
  1156. wl3501_unblock_interrupt(this);
  1157. wl3501_mgmt_scan(this, 100);
  1158. pr_debug("%s: device reset", dev->name);
  1159. rc = 0;
  1160. out:
  1161. spin_unlock_irqrestore(&this->lock, flags);
  1162. return rc;
  1163. }
  1164. static void wl3501_tx_timeout(struct net_device *dev)
  1165. {
  1166. struct net_device_stats *stats = &dev->stats;
  1167. int rc;
  1168. stats->tx_errors++;
  1169. rc = wl3501_reset(dev);
  1170. if (rc)
  1171. printk(KERN_ERR "%s: Error %d resetting card on Tx timeout!\n",
  1172. dev->name, rc);
  1173. else {
  1174. netif_trans_update(dev); /* prevent tx timeout */
  1175. netif_wake_queue(dev);
  1176. }
  1177. }
  1178. /*
  1179. * Return : 0 - OK
  1180. * 1 - Could not transmit (dev_queue_xmit will queue it)
  1181. * and try to sent it later
  1182. */
  1183. static netdev_tx_t wl3501_hard_start_xmit(struct sk_buff *skb,
  1184. struct net_device *dev)
  1185. {
  1186. int enabled, rc;
  1187. struct wl3501_card *this = netdev_priv(dev);
  1188. unsigned long flags;
  1189. spin_lock_irqsave(&this->lock, flags);
  1190. enabled = wl3501_block_interrupt(this);
  1191. rc = wl3501_send_pkt(this, skb->data, skb->len);
  1192. if (enabled)
  1193. wl3501_unblock_interrupt(this);
  1194. if (rc) {
  1195. ++dev->stats.tx_dropped;
  1196. netif_stop_queue(dev);
  1197. } else {
  1198. ++dev->stats.tx_packets;
  1199. dev->stats.tx_bytes += skb->len;
  1200. kfree_skb(skb);
  1201. if (this->tx_buffer_cnt < 2)
  1202. netif_stop_queue(dev);
  1203. }
  1204. spin_unlock_irqrestore(&this->lock, flags);
  1205. return NETDEV_TX_OK;
  1206. }
  1207. static int wl3501_open(struct net_device *dev)
  1208. {
  1209. int rc = -ENODEV;
  1210. struct wl3501_card *this = netdev_priv(dev);
  1211. unsigned long flags;
  1212. struct pcmcia_device *link;
  1213. link = this->p_dev;
  1214. spin_lock_irqsave(&this->lock, flags);
  1215. if (!pcmcia_dev_present(link))
  1216. goto out;
  1217. netif_device_attach(dev);
  1218. link->open++;
  1219. /* Initial WL3501 firmware */
  1220. pr_debug("%s: Initialize WL3501 firmware...", dev->name);
  1221. if (wl3501_init_firmware(this))
  1222. goto fail;
  1223. /* Initial device variables */
  1224. this->adhoc_times = 0;
  1225. /* Acknowledge Interrupt, for cleaning last state */
  1226. wl3501_ack_interrupt(this);
  1227. /* Enable interrupt from card after all */
  1228. wl3501_unblock_interrupt(this);
  1229. wl3501_mgmt_scan(this, 100);
  1230. rc = 0;
  1231. pr_debug("%s: WL3501 opened", dev->name);
  1232. printk(KERN_INFO "%s: Card Name: %s\n"
  1233. "%s: Firmware Date: %s\n",
  1234. dev->name, this->card_name,
  1235. dev->name, this->firmware_date);
  1236. out:
  1237. spin_unlock_irqrestore(&this->lock, flags);
  1238. return rc;
  1239. fail:
  1240. printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
  1241. goto out;
  1242. }
  1243. static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
  1244. {
  1245. struct wl3501_card *this = netdev_priv(dev);
  1246. struct iw_statistics *wstats = &this->wstats;
  1247. u32 value; /* size checked: it is u32 */
  1248. memset(wstats, 0, sizeof(*wstats));
  1249. wstats->status = netif_running(dev);
  1250. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
  1251. &value, sizeof(value)))
  1252. wstats->discard.code += value;
  1253. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
  1254. &value, sizeof(value)))
  1255. wstats->discard.code += value;
  1256. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
  1257. &value, sizeof(value)))
  1258. wstats->discard.code += value;
  1259. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
  1260. &value, sizeof(value)))
  1261. wstats->discard.retries = value;
  1262. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
  1263. &value, sizeof(value)))
  1264. wstats->discard.misc += value;
  1265. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
  1266. &value, sizeof(value)))
  1267. wstats->discard.misc += value;
  1268. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
  1269. &value, sizeof(value)))
  1270. wstats->discard.misc += value;
  1271. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
  1272. &value, sizeof(value)))
  1273. wstats->discard.misc += value;
  1274. return wstats;
  1275. }
  1276. /**
  1277. * wl3501_detach - deletes a driver "instance"
  1278. * @link - FILL_IN
  1279. *
  1280. * This deletes a driver "instance". The device is de-registered with Card
  1281. * Services. If it has been released, all local data structures are freed.
  1282. * Otherwise, the structures will be freed when the device is released.
  1283. */
  1284. static void wl3501_detach(struct pcmcia_device *link)
  1285. {
  1286. struct net_device *dev = link->priv;
  1287. /* If the device is currently configured and active, we won't actually
  1288. * delete it yet. Instead, it is marked so that when the release()
  1289. * function is called, that will trigger a proper detach(). */
  1290. while (link->open > 0)
  1291. wl3501_close(dev);
  1292. netif_device_detach(dev);
  1293. wl3501_release(link);
  1294. unregister_netdev(dev);
  1295. if (link->priv)
  1296. free_netdev(link->priv);
  1297. }
  1298. static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
  1299. union iwreq_data *wrqu, char *extra)
  1300. {
  1301. strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
  1302. return 0;
  1303. }
  1304. static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
  1305. union iwreq_data *wrqu, char *extra)
  1306. {
  1307. struct wl3501_card *this = netdev_priv(dev);
  1308. int channel = wrqu->freq.m;
  1309. int rc = -EINVAL;
  1310. if (iw_valid_channel(this->reg_domain, channel)) {
  1311. this->chan = channel;
  1312. rc = wl3501_reset(dev);
  1313. }
  1314. return rc;
  1315. }
  1316. static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
  1317. union iwreq_data *wrqu, char *extra)
  1318. {
  1319. struct wl3501_card *this = netdev_priv(dev);
  1320. wrqu->freq.m = 100000 *
  1321. ieee80211_channel_to_frequency(this->chan, NL80211_BAND_2GHZ);
  1322. wrqu->freq.e = 1;
  1323. return 0;
  1324. }
  1325. static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
  1326. union iwreq_data *wrqu, char *extra)
  1327. {
  1328. int rc = -EINVAL;
  1329. if (wrqu->mode == IW_MODE_INFRA ||
  1330. wrqu->mode == IW_MODE_ADHOC ||
  1331. wrqu->mode == IW_MODE_AUTO) {
  1332. struct wl3501_card *this = netdev_priv(dev);
  1333. this->net_type = wrqu->mode;
  1334. rc = wl3501_reset(dev);
  1335. }
  1336. return rc;
  1337. }
  1338. static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
  1339. union iwreq_data *wrqu, char *extra)
  1340. {
  1341. struct wl3501_card *this = netdev_priv(dev);
  1342. wrqu->mode = this->net_type;
  1343. return 0;
  1344. }
  1345. static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
  1346. union iwreq_data *wrqu, char *extra)
  1347. {
  1348. struct wl3501_card *this = netdev_priv(dev);
  1349. wrqu->sens.value = this->rssi;
  1350. wrqu->sens.disabled = !wrqu->sens.value;
  1351. wrqu->sens.fixed = 1;
  1352. return 0;
  1353. }
  1354. static int wl3501_get_range(struct net_device *dev,
  1355. struct iw_request_info *info,
  1356. union iwreq_data *wrqu, char *extra)
  1357. {
  1358. struct iw_range *range = (struct iw_range *)extra;
  1359. /* Set the length (very important for backward compatibility) */
  1360. wrqu->data.length = sizeof(*range);
  1361. /* Set all the info we don't care or don't know about to zero */
  1362. memset(range, 0, sizeof(*range));
  1363. /* Set the Wireless Extension versions */
  1364. range->we_version_compiled = WIRELESS_EXT;
  1365. range->we_version_source = 1;
  1366. range->throughput = 2 * 1000 * 1000; /* ~2 Mb/s */
  1367. /* FIXME: study the code to fill in more fields... */
  1368. return 0;
  1369. }
  1370. static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
  1371. union iwreq_data *wrqu, char *extra)
  1372. {
  1373. struct wl3501_card *this = netdev_priv(dev);
  1374. int rc = -EINVAL;
  1375. /* FIXME: we support other ARPHRDs...*/
  1376. if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
  1377. goto out;
  1378. if (is_broadcast_ether_addr(wrqu->ap_addr.sa_data)) {
  1379. /* FIXME: rescan? */
  1380. } else
  1381. memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
  1382. /* FIXME: rescan? deassoc & scan? */
  1383. rc = 0;
  1384. out:
  1385. return rc;
  1386. }
  1387. static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
  1388. union iwreq_data *wrqu, char *extra)
  1389. {
  1390. struct wl3501_card *this = netdev_priv(dev);
  1391. wrqu->ap_addr.sa_family = ARPHRD_ETHER;
  1392. memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
  1393. return 0;
  1394. }
  1395. static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
  1396. union iwreq_data *wrqu, char *extra)
  1397. {
  1398. /*
  1399. * FIXME: trigger scanning with a reset, yes, I'm lazy
  1400. */
  1401. return wl3501_reset(dev);
  1402. }
  1403. static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
  1404. union iwreq_data *wrqu, char *extra)
  1405. {
  1406. struct wl3501_card *this = netdev_priv(dev);
  1407. int i;
  1408. char *current_ev = extra;
  1409. struct iw_event iwe;
  1410. for (i = 0; i < this->bss_cnt; ++i) {
  1411. iwe.cmd = SIOCGIWAP;
  1412. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1413. memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].req.bssid, ETH_ALEN);
  1414. current_ev = iwe_stream_add_event(info, current_ev,
  1415. extra + IW_SCAN_MAX_DATA,
  1416. &iwe, IW_EV_ADDR_LEN);
  1417. iwe.cmd = SIOCGIWESSID;
  1418. iwe.u.data.flags = 1;
  1419. iwe.u.data.length = this->bss_set[i].req.ssid.el.len;
  1420. current_ev = iwe_stream_add_point(info, current_ev,
  1421. extra + IW_SCAN_MAX_DATA,
  1422. &iwe,
  1423. this->bss_set[i].req.ssid.essid);
  1424. iwe.cmd = SIOCGIWMODE;
  1425. iwe.u.mode = this->bss_set[i].req.bss_type;
  1426. current_ev = iwe_stream_add_event(info, current_ev,
  1427. extra + IW_SCAN_MAX_DATA,
  1428. &iwe, IW_EV_UINT_LEN);
  1429. iwe.cmd = SIOCGIWFREQ;
  1430. iwe.u.freq.m = this->bss_set[i].req.ds_pset.chan;
  1431. iwe.u.freq.e = 0;
  1432. current_ev = iwe_stream_add_event(info, current_ev,
  1433. extra + IW_SCAN_MAX_DATA,
  1434. &iwe, IW_EV_FREQ_LEN);
  1435. iwe.cmd = SIOCGIWENCODE;
  1436. if (this->bss_set[i].req.cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
  1437. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1438. else
  1439. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1440. iwe.u.data.length = 0;
  1441. current_ev = iwe_stream_add_point(info, current_ev,
  1442. extra + IW_SCAN_MAX_DATA,
  1443. &iwe, NULL);
  1444. }
  1445. /* Length of data */
  1446. wrqu->data.length = (current_ev - extra);
  1447. wrqu->data.flags = 0; /* FIXME: set properly these flags */
  1448. return 0;
  1449. }
  1450. static int wl3501_set_essid(struct net_device *dev,
  1451. struct iw_request_info *info,
  1452. union iwreq_data *wrqu, char *extra)
  1453. {
  1454. struct wl3501_card *this = netdev_priv(dev);
  1455. if (wrqu->data.flags) {
  1456. iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
  1457. &this->essid.el,
  1458. extra, wrqu->data.length);
  1459. } else { /* We accept any ESSID */
  1460. iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
  1461. &this->essid.el, "ANY", 3);
  1462. }
  1463. return wl3501_reset(dev);
  1464. }
  1465. static int wl3501_get_essid(struct net_device *dev,
  1466. struct iw_request_info *info,
  1467. union iwreq_data *wrqu, char *extra)
  1468. {
  1469. struct wl3501_card *this = netdev_priv(dev);
  1470. unsigned long flags;
  1471. spin_lock_irqsave(&this->lock, flags);
  1472. wrqu->essid.flags = 1;
  1473. wrqu->essid.length = this->essid.el.len;
  1474. memcpy(extra, this->essid.essid, this->essid.el.len);
  1475. spin_unlock_irqrestore(&this->lock, flags);
  1476. return 0;
  1477. }
  1478. static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
  1479. union iwreq_data *wrqu, char *extra)
  1480. {
  1481. struct wl3501_card *this = netdev_priv(dev);
  1482. if (wrqu->data.length > sizeof(this->nick))
  1483. return -E2BIG;
  1484. strlcpy(this->nick, extra, wrqu->data.length);
  1485. return 0;
  1486. }
  1487. static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
  1488. union iwreq_data *wrqu, char *extra)
  1489. {
  1490. struct wl3501_card *this = netdev_priv(dev);
  1491. strlcpy(extra, this->nick, 32);
  1492. wrqu->data.length = strlen(extra);
  1493. return 0;
  1494. }
  1495. static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
  1496. union iwreq_data *wrqu, char *extra)
  1497. {
  1498. /*
  1499. * FIXME: have to see from where to get this info, perhaps this card
  1500. * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
  1501. * common with the Planet Access Points. -acme
  1502. */
  1503. wrqu->bitrate.value = 2000000;
  1504. wrqu->bitrate.fixed = 1;
  1505. return 0;
  1506. }
  1507. static int wl3501_get_rts_threshold(struct net_device *dev,
  1508. struct iw_request_info *info,
  1509. union iwreq_data *wrqu, char *extra)
  1510. {
  1511. u16 threshold; /* size checked: it is u16 */
  1512. struct wl3501_card *this = netdev_priv(dev);
  1513. int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
  1514. &threshold, sizeof(threshold));
  1515. if (!rc) {
  1516. wrqu->rts.value = threshold;
  1517. wrqu->rts.disabled = threshold >= 2347;
  1518. wrqu->rts.fixed = 1;
  1519. }
  1520. return rc;
  1521. }
  1522. static int wl3501_get_frag_threshold(struct net_device *dev,
  1523. struct iw_request_info *info,
  1524. union iwreq_data *wrqu, char *extra)
  1525. {
  1526. u16 threshold; /* size checked: it is u16 */
  1527. struct wl3501_card *this = netdev_priv(dev);
  1528. int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
  1529. &threshold, sizeof(threshold));
  1530. if (!rc) {
  1531. wrqu->frag.value = threshold;
  1532. wrqu->frag.disabled = threshold >= 2346;
  1533. wrqu->frag.fixed = 1;
  1534. }
  1535. return rc;
  1536. }
  1537. static int wl3501_get_txpow(struct net_device *dev,
  1538. struct iw_request_info *info,
  1539. union iwreq_data *wrqu, char *extra)
  1540. {
  1541. u16 txpow;
  1542. struct wl3501_card *this = netdev_priv(dev);
  1543. int rc = wl3501_get_mib_value(this,
  1544. WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
  1545. &txpow, sizeof(txpow));
  1546. if (!rc) {
  1547. wrqu->txpower.value = txpow;
  1548. wrqu->txpower.disabled = 0;
  1549. /*
  1550. * From the MIB values I think this can be configurable,
  1551. * as it lists several tx power levels -acme
  1552. */
  1553. wrqu->txpower.fixed = 0;
  1554. wrqu->txpower.flags = IW_TXPOW_MWATT;
  1555. }
  1556. return rc;
  1557. }
  1558. static int wl3501_get_retry(struct net_device *dev,
  1559. struct iw_request_info *info,
  1560. union iwreq_data *wrqu, char *extra)
  1561. {
  1562. u8 retry; /* size checked: it is u8 */
  1563. struct wl3501_card *this = netdev_priv(dev);
  1564. int rc = wl3501_get_mib_value(this,
  1565. WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
  1566. &retry, sizeof(retry));
  1567. if (rc)
  1568. goto out;
  1569. if (wrqu->retry.flags & IW_RETRY_LONG) {
  1570. wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
  1571. goto set_value;
  1572. }
  1573. rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
  1574. &retry, sizeof(retry));
  1575. if (rc)
  1576. goto out;
  1577. wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_SHORT;
  1578. set_value:
  1579. wrqu->retry.value = retry;
  1580. wrqu->retry.disabled = 0;
  1581. out:
  1582. return rc;
  1583. }
  1584. static int wl3501_get_encode(struct net_device *dev,
  1585. struct iw_request_info *info,
  1586. union iwreq_data *wrqu, char *extra)
  1587. {
  1588. u8 implemented, restricted, keys[100], len_keys, tocopy;
  1589. struct wl3501_card *this = netdev_priv(dev);
  1590. int rc = wl3501_get_mib_value(this,
  1591. WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
  1592. &implemented, sizeof(implemented));
  1593. if (rc)
  1594. goto out;
  1595. if (!implemented) {
  1596. wrqu->encoding.flags = IW_ENCODE_DISABLED;
  1597. goto out;
  1598. }
  1599. rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
  1600. &restricted, sizeof(restricted));
  1601. if (rc)
  1602. goto out;
  1603. wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
  1604. IW_ENCODE_OPEN;
  1605. rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
  1606. &len_keys, sizeof(len_keys));
  1607. if (rc)
  1608. goto out;
  1609. rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
  1610. keys, len_keys);
  1611. if (rc)
  1612. goto out;
  1613. tocopy = min_t(u16, len_keys, wrqu->encoding.length);
  1614. tocopy = min_t(u8, tocopy, 100);
  1615. wrqu->encoding.length = tocopy;
  1616. memcpy(extra, keys, tocopy);
  1617. out:
  1618. return rc;
  1619. }
  1620. static int wl3501_get_power(struct net_device *dev,
  1621. struct iw_request_info *info,
  1622. union iwreq_data *wrqu, char *extra)
  1623. {
  1624. u8 pwr_state;
  1625. struct wl3501_card *this = netdev_priv(dev);
  1626. int rc = wl3501_get_mib_value(this,
  1627. WL3501_MIB_ATTR_CURRENT_PWR_STATE,
  1628. &pwr_state, sizeof(pwr_state));
  1629. if (rc)
  1630. goto out;
  1631. wrqu->power.disabled = !pwr_state;
  1632. wrqu->power.flags = IW_POWER_ON;
  1633. out:
  1634. return rc;
  1635. }
  1636. static const iw_handler wl3501_handler[] = {
  1637. IW_HANDLER(SIOCGIWNAME, wl3501_get_name),
  1638. IW_HANDLER(SIOCSIWFREQ, wl3501_set_freq),
  1639. IW_HANDLER(SIOCGIWFREQ, wl3501_get_freq),
  1640. IW_HANDLER(SIOCSIWMODE, wl3501_set_mode),
  1641. IW_HANDLER(SIOCGIWMODE, wl3501_get_mode),
  1642. IW_HANDLER(SIOCGIWSENS, wl3501_get_sens),
  1643. IW_HANDLER(SIOCGIWRANGE, wl3501_get_range),
  1644. IW_HANDLER(SIOCSIWSPY, iw_handler_set_spy),
  1645. IW_HANDLER(SIOCGIWSPY, iw_handler_get_spy),
  1646. IW_HANDLER(SIOCSIWTHRSPY, iw_handler_set_thrspy),
  1647. IW_HANDLER(SIOCGIWTHRSPY, iw_handler_get_thrspy),
  1648. IW_HANDLER(SIOCSIWAP, wl3501_set_wap),
  1649. IW_HANDLER(SIOCGIWAP, wl3501_get_wap),
  1650. IW_HANDLER(SIOCSIWSCAN, wl3501_set_scan),
  1651. IW_HANDLER(SIOCGIWSCAN, wl3501_get_scan),
  1652. IW_HANDLER(SIOCSIWESSID, wl3501_set_essid),
  1653. IW_HANDLER(SIOCGIWESSID, wl3501_get_essid),
  1654. IW_HANDLER(SIOCSIWNICKN, wl3501_set_nick),
  1655. IW_HANDLER(SIOCGIWNICKN, wl3501_get_nick),
  1656. IW_HANDLER(SIOCGIWRATE, wl3501_get_rate),
  1657. IW_HANDLER(SIOCGIWRTS, wl3501_get_rts_threshold),
  1658. IW_HANDLER(SIOCGIWFRAG, wl3501_get_frag_threshold),
  1659. IW_HANDLER(SIOCGIWTXPOW, wl3501_get_txpow),
  1660. IW_HANDLER(SIOCGIWRETRY, wl3501_get_retry),
  1661. IW_HANDLER(SIOCGIWENCODE, wl3501_get_encode),
  1662. IW_HANDLER(SIOCGIWPOWER, wl3501_get_power),
  1663. };
  1664. static const struct iw_handler_def wl3501_handler_def = {
  1665. .num_standard = ARRAY_SIZE(wl3501_handler),
  1666. .standard = (iw_handler *)wl3501_handler,
  1667. .get_wireless_stats = wl3501_get_wireless_stats,
  1668. };
  1669. static const struct net_device_ops wl3501_netdev_ops = {
  1670. .ndo_open = wl3501_open,
  1671. .ndo_stop = wl3501_close,
  1672. .ndo_start_xmit = wl3501_hard_start_xmit,
  1673. .ndo_tx_timeout = wl3501_tx_timeout,
  1674. .ndo_set_mac_address = eth_mac_addr,
  1675. .ndo_validate_addr = eth_validate_addr,
  1676. };
  1677. static int wl3501_probe(struct pcmcia_device *p_dev)
  1678. {
  1679. struct net_device *dev;
  1680. struct wl3501_card *this;
  1681. /* The io structure describes IO port mapping */
  1682. p_dev->resource[0]->end = 16;
  1683. p_dev->resource[0]->flags = IO_DATA_PATH_WIDTH_8;
  1684. /* General socket configuration */
  1685. p_dev->config_flags = CONF_ENABLE_IRQ;
  1686. p_dev->config_index = 1;
  1687. dev = alloc_etherdev(sizeof(struct wl3501_card));
  1688. if (!dev)
  1689. goto out_link;
  1690. dev->netdev_ops = &wl3501_netdev_ops;
  1691. dev->watchdog_timeo = 5 * HZ;
  1692. this = netdev_priv(dev);
  1693. this->wireless_data.spy_data = &this->spy_data;
  1694. this->p_dev = p_dev;
  1695. dev->wireless_data = &this->wireless_data;
  1696. dev->wireless_handlers = &wl3501_handler_def;
  1697. netif_stop_queue(dev);
  1698. p_dev->priv = dev;
  1699. return wl3501_config(p_dev);
  1700. out_link:
  1701. return -ENOMEM;
  1702. }
  1703. static int wl3501_config(struct pcmcia_device *link)
  1704. {
  1705. struct net_device *dev = link->priv;
  1706. int i = 0, j, ret;
  1707. struct wl3501_card *this;
  1708. /* Try allocating IO ports. This tries a few fixed addresses. If you
  1709. * want, you can also read the card's config table to pick addresses --
  1710. * see the serial driver for an example. */
  1711. link->io_lines = 5;
  1712. for (j = 0x280; j < 0x400; j += 0x20) {
  1713. /* The '^0x300' is so that we probe 0x300-0x3ff first, then
  1714. * 0x200-0x2ff, and so on, because this seems safer */
  1715. link->resource[0]->start = j;
  1716. link->resource[1]->start = link->resource[0]->start + 0x10;
  1717. i = pcmcia_request_io(link);
  1718. if (i == 0)
  1719. break;
  1720. }
  1721. if (i != 0)
  1722. goto failed;
  1723. /* Now allocate an interrupt line. Note that this does not actually
  1724. * assign a handler to the interrupt. */
  1725. ret = pcmcia_request_irq(link, wl3501_interrupt);
  1726. if (ret)
  1727. goto failed;
  1728. ret = pcmcia_enable_device(link);
  1729. if (ret)
  1730. goto failed;
  1731. dev->irq = link->irq;
  1732. dev->base_addr = link->resource[0]->start;
  1733. SET_NETDEV_DEV(dev, &link->dev);
  1734. if (register_netdev(dev)) {
  1735. printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
  1736. goto failed;
  1737. }
  1738. this = netdev_priv(dev);
  1739. this->base_addr = dev->base_addr;
  1740. if (!wl3501_get_flash_mac_addr(this)) {
  1741. printk(KERN_WARNING "%s: Can't read MAC addr in flash ROM?\n",
  1742. dev->name);
  1743. unregister_netdev(dev);
  1744. goto failed;
  1745. }
  1746. for (i = 0; i < 6; i++)
  1747. dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
  1748. /* print probe information */
  1749. printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, "
  1750. "MAC addr in flash ROM:%pM\n",
  1751. dev->name, this->base_addr, (int)dev->irq,
  1752. dev->dev_addr);
  1753. /*
  1754. * Initialize card parameters - added by jss
  1755. */
  1756. this->net_type = IW_MODE_INFRA;
  1757. this->bss_cnt = 0;
  1758. this->join_sta_bss = 0;
  1759. this->adhoc_times = 0;
  1760. iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
  1761. "ANY", 3);
  1762. this->card_name[0] = '\0';
  1763. this->firmware_date[0] = '\0';
  1764. this->rssi = 255;
  1765. this->chan = iw_default_channel(this->reg_domain);
  1766. strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
  1767. spin_lock_init(&this->lock);
  1768. init_waitqueue_head(&this->wait);
  1769. netif_start_queue(dev);
  1770. return 0;
  1771. failed:
  1772. wl3501_release(link);
  1773. return -ENODEV;
  1774. }
  1775. static void wl3501_release(struct pcmcia_device *link)
  1776. {
  1777. pcmcia_disable_device(link);
  1778. }
  1779. static int wl3501_suspend(struct pcmcia_device *link)
  1780. {
  1781. struct net_device *dev = link->priv;
  1782. wl3501_pwr_mgmt(netdev_priv(dev), WL3501_SUSPEND);
  1783. if (link->open)
  1784. netif_device_detach(dev);
  1785. return 0;
  1786. }
  1787. static int wl3501_resume(struct pcmcia_device *link)
  1788. {
  1789. struct net_device *dev = link->priv;
  1790. wl3501_pwr_mgmt(netdev_priv(dev), WL3501_RESUME);
  1791. if (link->open) {
  1792. wl3501_reset(dev);
  1793. netif_device_attach(dev);
  1794. }
  1795. return 0;
  1796. }
  1797. static const struct pcmcia_device_id wl3501_ids[] = {
  1798. PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
  1799. PCMCIA_DEVICE_NULL
  1800. };
  1801. MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
  1802. static struct pcmcia_driver wl3501_driver = {
  1803. .owner = THIS_MODULE,
  1804. .name = "wl3501_cs",
  1805. .probe = wl3501_probe,
  1806. .remove = wl3501_detach,
  1807. .id_table = wl3501_ids,
  1808. .suspend = wl3501_suspend,
  1809. .resume = wl3501_resume,
  1810. };
  1811. module_pcmcia_driver(wl3501_driver);
  1812. MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
  1813. "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
  1814. "Gustavo Niemeyer <niemeyer@conectiva.com>");
  1815. MODULE_DESCRIPTION("Planet wl3501 wireless driver");
  1816. MODULE_LICENSE("GPL");