wl3501_cs.c 58 KB

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