wl3501_cs.c 57 KB

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