wl3501_cs.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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. dev->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. dev->stats.rx_packets++;
  925. dev->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 = &dev->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. ++dev->stats.tx_dropped;
  1224. netif_stop_queue(dev);
  1225. rc = NETDEV_TX_OK;
  1226. } else {
  1227. ++dev->stats.tx_packets;
  1228. dev->stats.tx_bytes += skb->len;
  1229. kfree_skb(skb);
  1230. if (this->tx_buffer_cnt < 2)
  1231. netif_stop_queue(dev);
  1232. }
  1233. spin_unlock_irqrestore(&this->lock, flags);
  1234. return rc;
  1235. }
  1236. static int wl3501_open(struct net_device *dev)
  1237. {
  1238. int rc = -ENODEV;
  1239. struct wl3501_card *this = netdev_priv(dev);
  1240. unsigned long flags;
  1241. struct pcmcia_device *link;
  1242. link = this->p_dev;
  1243. spin_lock_irqsave(&this->lock, flags);
  1244. if (!pcmcia_dev_present(link))
  1245. goto out;
  1246. netif_device_attach(dev);
  1247. link->open++;
  1248. /* Initial WL3501 firmware */
  1249. dprintk(1, "%s: Initialize WL3501 firmware...", dev->name);
  1250. if (wl3501_init_firmware(this))
  1251. goto fail;
  1252. /* Initial device variables */
  1253. this->adhoc_times = 0;
  1254. /* Acknowledge Interrupt, for cleaning last state */
  1255. wl3501_ack_interrupt(this);
  1256. /* Enable interrupt from card after all */
  1257. wl3501_unblock_interrupt(this);
  1258. wl3501_mgmt_scan(this, 100);
  1259. rc = 0;
  1260. dprintk(1, "%s: WL3501 opened", dev->name);
  1261. printk(KERN_INFO "%s: Card Name: %s\n"
  1262. "%s: Firmware Date: %s\n",
  1263. dev->name, this->card_name,
  1264. dev->name, this->firmware_date);
  1265. out:
  1266. spin_unlock_irqrestore(&this->lock, flags);
  1267. return rc;
  1268. fail:
  1269. printk(KERN_WARNING "%s: Can't initialize firmware!\n", dev->name);
  1270. goto out;
  1271. }
  1272. static struct iw_statistics *wl3501_get_wireless_stats(struct net_device *dev)
  1273. {
  1274. struct wl3501_card *this = netdev_priv(dev);
  1275. struct iw_statistics *wstats = &this->wstats;
  1276. u32 value; /* size checked: it is u32 */
  1277. memset(wstats, 0, sizeof(*wstats));
  1278. wstats->status = netif_running(dev);
  1279. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_ICV_ERROR_COUNT,
  1280. &value, sizeof(value)))
  1281. wstats->discard.code += value;
  1282. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_UNDECRYPTABLE_COUNT,
  1283. &value, sizeof(value)))
  1284. wstats->discard.code += value;
  1285. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_EXCLUDED_COUNT,
  1286. &value, sizeof(value)))
  1287. wstats->discard.code += value;
  1288. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RETRY_COUNT,
  1289. &value, sizeof(value)))
  1290. wstats->discard.retries = value;
  1291. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FAILED_COUNT,
  1292. &value, sizeof(value)))
  1293. wstats->discard.misc += value;
  1294. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_FAILURE_COUNT,
  1295. &value, sizeof(value)))
  1296. wstats->discard.misc += value;
  1297. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_ACK_FAILURE_COUNT,
  1298. &value, sizeof(value)))
  1299. wstats->discard.misc += value;
  1300. if (!wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAME_DUPLICATE_COUNT,
  1301. &value, sizeof(value)))
  1302. wstats->discard.misc += value;
  1303. return wstats;
  1304. }
  1305. static void wl3501_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1306. {
  1307. strlcpy(info->driver, wl3501_dev_info, sizeof(info->driver));
  1308. }
  1309. static const struct ethtool_ops ops = {
  1310. .get_drvinfo = wl3501_get_drvinfo
  1311. };
  1312. /**
  1313. * wl3501_detach - deletes a driver "instance"
  1314. * @link - FILL_IN
  1315. *
  1316. * This deletes a driver "instance". The device is de-registered with Card
  1317. * Services. If it has been released, all local data structures are freed.
  1318. * Otherwise, the structures will be freed when the device is released.
  1319. */
  1320. static void wl3501_detach(struct pcmcia_device *link)
  1321. {
  1322. struct net_device *dev = link->priv;
  1323. /* If the device is currently configured and active, we won't actually
  1324. * delete it yet. Instead, it is marked so that when the release()
  1325. * function is called, that will trigger a proper detach(). */
  1326. while (link->open > 0)
  1327. wl3501_close(dev);
  1328. netif_device_detach(dev);
  1329. wl3501_release(link);
  1330. if (link->priv)
  1331. free_netdev(link->priv);
  1332. return;
  1333. }
  1334. static int wl3501_get_name(struct net_device *dev, struct iw_request_info *info,
  1335. union iwreq_data *wrqu, char *extra)
  1336. {
  1337. strlcpy(wrqu->name, "IEEE 802.11-DS", sizeof(wrqu->name));
  1338. return 0;
  1339. }
  1340. static int wl3501_set_freq(struct net_device *dev, struct iw_request_info *info,
  1341. union iwreq_data *wrqu, char *extra)
  1342. {
  1343. struct wl3501_card *this = netdev_priv(dev);
  1344. int channel = wrqu->freq.m;
  1345. int rc = -EINVAL;
  1346. if (iw_valid_channel(this->reg_domain, channel)) {
  1347. this->chan = channel;
  1348. rc = wl3501_reset(dev);
  1349. }
  1350. return rc;
  1351. }
  1352. static int wl3501_get_freq(struct net_device *dev, struct iw_request_info *info,
  1353. union iwreq_data *wrqu, char *extra)
  1354. {
  1355. struct wl3501_card *this = netdev_priv(dev);
  1356. wrqu->freq.m = ieee80211_dsss_chan_to_freq(this->chan) * 100000;
  1357. wrqu->freq.e = 1;
  1358. return 0;
  1359. }
  1360. static int wl3501_set_mode(struct net_device *dev, struct iw_request_info *info,
  1361. union iwreq_data *wrqu, char *extra)
  1362. {
  1363. int rc = -EINVAL;
  1364. if (wrqu->mode == IW_MODE_INFRA ||
  1365. wrqu->mode == IW_MODE_ADHOC ||
  1366. wrqu->mode == IW_MODE_AUTO) {
  1367. struct wl3501_card *this = netdev_priv(dev);
  1368. this->net_type = wrqu->mode;
  1369. rc = wl3501_reset(dev);
  1370. }
  1371. return rc;
  1372. }
  1373. static int wl3501_get_mode(struct net_device *dev, struct iw_request_info *info,
  1374. union iwreq_data *wrqu, char *extra)
  1375. {
  1376. struct wl3501_card *this = netdev_priv(dev);
  1377. wrqu->mode = this->net_type;
  1378. return 0;
  1379. }
  1380. static int wl3501_get_sens(struct net_device *dev, struct iw_request_info *info,
  1381. union iwreq_data *wrqu, char *extra)
  1382. {
  1383. struct wl3501_card *this = netdev_priv(dev);
  1384. wrqu->sens.value = this->rssi;
  1385. wrqu->sens.disabled = !wrqu->sens.value;
  1386. wrqu->sens.fixed = 1;
  1387. return 0;
  1388. }
  1389. static int wl3501_get_range(struct net_device *dev,
  1390. struct iw_request_info *info,
  1391. union iwreq_data *wrqu, char *extra)
  1392. {
  1393. struct iw_range *range = (struct iw_range *)extra;
  1394. /* Set the length (very important for backward compatibility) */
  1395. wrqu->data.length = sizeof(*range);
  1396. /* Set all the info we don't care or don't know about to zero */
  1397. memset(range, 0, sizeof(*range));
  1398. /* Set the Wireless Extension versions */
  1399. range->we_version_compiled = WIRELESS_EXT;
  1400. range->we_version_source = 1;
  1401. range->throughput = 2 * 1000 * 1000; /* ~2 Mb/s */
  1402. /* FIXME: study the code to fill in more fields... */
  1403. return 0;
  1404. }
  1405. static int wl3501_set_wap(struct net_device *dev, struct iw_request_info *info,
  1406. union iwreq_data *wrqu, char *extra)
  1407. {
  1408. struct wl3501_card *this = netdev_priv(dev);
  1409. static const u8 bcast[ETH_ALEN] = { 255, 255, 255, 255, 255, 255 };
  1410. int rc = -EINVAL;
  1411. /* FIXME: we support other ARPHRDs...*/
  1412. if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
  1413. goto out;
  1414. if (!memcmp(bcast, wrqu->ap_addr.sa_data, ETH_ALEN)) {
  1415. /* FIXME: rescan? */
  1416. } else
  1417. memcpy(this->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
  1418. /* FIXME: rescan? deassoc & scan? */
  1419. rc = 0;
  1420. out:
  1421. return rc;
  1422. }
  1423. static int wl3501_get_wap(struct net_device *dev, struct iw_request_info *info,
  1424. union iwreq_data *wrqu, char *extra)
  1425. {
  1426. struct wl3501_card *this = netdev_priv(dev);
  1427. wrqu->ap_addr.sa_family = ARPHRD_ETHER;
  1428. memcpy(wrqu->ap_addr.sa_data, this->bssid, ETH_ALEN);
  1429. return 0;
  1430. }
  1431. static int wl3501_set_scan(struct net_device *dev, struct iw_request_info *info,
  1432. union iwreq_data *wrqu, char *extra)
  1433. {
  1434. /*
  1435. * FIXME: trigger scanning with a reset, yes, I'm lazy
  1436. */
  1437. return wl3501_reset(dev);
  1438. }
  1439. static int wl3501_get_scan(struct net_device *dev, struct iw_request_info *info,
  1440. union iwreq_data *wrqu, char *extra)
  1441. {
  1442. struct wl3501_card *this = netdev_priv(dev);
  1443. int i;
  1444. char *current_ev = extra;
  1445. struct iw_event iwe;
  1446. for (i = 0; i < this->bss_cnt; ++i) {
  1447. iwe.cmd = SIOCGIWAP;
  1448. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1449. memcpy(iwe.u.ap_addr.sa_data, this->bss_set[i].bssid, ETH_ALEN);
  1450. current_ev = iwe_stream_add_event(info, current_ev,
  1451. extra + IW_SCAN_MAX_DATA,
  1452. &iwe, IW_EV_ADDR_LEN);
  1453. iwe.cmd = SIOCGIWESSID;
  1454. iwe.u.data.flags = 1;
  1455. iwe.u.data.length = this->bss_set[i].ssid.el.len;
  1456. current_ev = iwe_stream_add_point(info, current_ev,
  1457. extra + IW_SCAN_MAX_DATA,
  1458. &iwe,
  1459. this->bss_set[i].ssid.essid);
  1460. iwe.cmd = SIOCGIWMODE;
  1461. iwe.u.mode = this->bss_set[i].bss_type;
  1462. current_ev = iwe_stream_add_event(info, current_ev,
  1463. extra + IW_SCAN_MAX_DATA,
  1464. &iwe, IW_EV_UINT_LEN);
  1465. iwe.cmd = SIOCGIWFREQ;
  1466. iwe.u.freq.m = this->bss_set[i].ds_pset.chan;
  1467. iwe.u.freq.e = 0;
  1468. current_ev = iwe_stream_add_event(info, current_ev,
  1469. extra + IW_SCAN_MAX_DATA,
  1470. &iwe, IW_EV_FREQ_LEN);
  1471. iwe.cmd = SIOCGIWENCODE;
  1472. if (this->bss_set[i].cap_info & WL3501_MGMT_CAPABILITY_PRIVACY)
  1473. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1474. else
  1475. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1476. iwe.u.data.length = 0;
  1477. current_ev = iwe_stream_add_point(info, current_ev,
  1478. extra + IW_SCAN_MAX_DATA,
  1479. &iwe, NULL);
  1480. }
  1481. /* Length of data */
  1482. wrqu->data.length = (current_ev - extra);
  1483. wrqu->data.flags = 0; /* FIXME: set properly these flags */
  1484. return 0;
  1485. }
  1486. static int wl3501_set_essid(struct net_device *dev,
  1487. struct iw_request_info *info,
  1488. union iwreq_data *wrqu, char *extra)
  1489. {
  1490. struct wl3501_card *this = netdev_priv(dev);
  1491. if (wrqu->data.flags) {
  1492. iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
  1493. &this->essid.el,
  1494. extra, wrqu->data.length);
  1495. } else { /* We accept any ESSID */
  1496. iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID,
  1497. &this->essid.el, "ANY", 3);
  1498. }
  1499. return wl3501_reset(dev);
  1500. }
  1501. static int wl3501_get_essid(struct net_device *dev,
  1502. struct iw_request_info *info,
  1503. union iwreq_data *wrqu, char *extra)
  1504. {
  1505. struct wl3501_card *this = netdev_priv(dev);
  1506. unsigned long flags;
  1507. spin_lock_irqsave(&this->lock, flags);
  1508. wrqu->essid.flags = 1;
  1509. wrqu->essid.length = this->essid.el.len;
  1510. memcpy(extra, this->essid.essid, this->essid.el.len);
  1511. spin_unlock_irqrestore(&this->lock, flags);
  1512. return 0;
  1513. }
  1514. static int wl3501_set_nick(struct net_device *dev, struct iw_request_info *info,
  1515. union iwreq_data *wrqu, char *extra)
  1516. {
  1517. struct wl3501_card *this = netdev_priv(dev);
  1518. if (wrqu->data.length > sizeof(this->nick))
  1519. return -E2BIG;
  1520. strlcpy(this->nick, extra, wrqu->data.length);
  1521. return 0;
  1522. }
  1523. static int wl3501_get_nick(struct net_device *dev, struct iw_request_info *info,
  1524. union iwreq_data *wrqu, char *extra)
  1525. {
  1526. struct wl3501_card *this = netdev_priv(dev);
  1527. strlcpy(extra, this->nick, 32);
  1528. wrqu->data.length = strlen(extra);
  1529. return 0;
  1530. }
  1531. static int wl3501_get_rate(struct net_device *dev, struct iw_request_info *info,
  1532. union iwreq_data *wrqu, char *extra)
  1533. {
  1534. /*
  1535. * FIXME: have to see from where to get this info, perhaps this card
  1536. * works at 1 Mbit/s too... for now leave at 2 Mbit/s that is the most
  1537. * common with the Planet Access Points. -acme
  1538. */
  1539. wrqu->bitrate.value = 2000000;
  1540. wrqu->bitrate.fixed = 1;
  1541. return 0;
  1542. }
  1543. static int wl3501_get_rts_threshold(struct net_device *dev,
  1544. struct iw_request_info *info,
  1545. union iwreq_data *wrqu, char *extra)
  1546. {
  1547. u16 threshold; /* size checked: it is u16 */
  1548. struct wl3501_card *this = netdev_priv(dev);
  1549. int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_RTS_THRESHOLD,
  1550. &threshold, sizeof(threshold));
  1551. if (!rc) {
  1552. wrqu->rts.value = threshold;
  1553. wrqu->rts.disabled = threshold >= 2347;
  1554. wrqu->rts.fixed = 1;
  1555. }
  1556. return rc;
  1557. }
  1558. static int wl3501_get_frag_threshold(struct net_device *dev,
  1559. struct iw_request_info *info,
  1560. union iwreq_data *wrqu, char *extra)
  1561. {
  1562. u16 threshold; /* size checked: it is u16 */
  1563. struct wl3501_card *this = netdev_priv(dev);
  1564. int rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_FRAG_THRESHOLD,
  1565. &threshold, sizeof(threshold));
  1566. if (!rc) {
  1567. wrqu->frag.value = threshold;
  1568. wrqu->frag.disabled = threshold >= 2346;
  1569. wrqu->frag.fixed = 1;
  1570. }
  1571. return rc;
  1572. }
  1573. static int wl3501_get_txpow(struct net_device *dev,
  1574. struct iw_request_info *info,
  1575. union iwreq_data *wrqu, char *extra)
  1576. {
  1577. u16 txpow;
  1578. struct wl3501_card *this = netdev_priv(dev);
  1579. int rc = wl3501_get_mib_value(this,
  1580. WL3501_MIB_ATTR_CURRENT_TX_PWR_LEVEL,
  1581. &txpow, sizeof(txpow));
  1582. if (!rc) {
  1583. wrqu->txpower.value = txpow;
  1584. wrqu->txpower.disabled = 0;
  1585. /*
  1586. * From the MIB values I think this can be configurable,
  1587. * as it lists several tx power levels -acme
  1588. */
  1589. wrqu->txpower.fixed = 0;
  1590. wrqu->txpower.flags = IW_TXPOW_MWATT;
  1591. }
  1592. return rc;
  1593. }
  1594. static int wl3501_get_retry(struct net_device *dev,
  1595. struct iw_request_info *info,
  1596. union iwreq_data *wrqu, char *extra)
  1597. {
  1598. u8 retry; /* size checked: it is u8 */
  1599. struct wl3501_card *this = netdev_priv(dev);
  1600. int rc = wl3501_get_mib_value(this,
  1601. WL3501_MIB_ATTR_LONG_RETRY_LIMIT,
  1602. &retry, sizeof(retry));
  1603. if (rc)
  1604. goto out;
  1605. if (wrqu->retry.flags & IW_RETRY_LONG) {
  1606. wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
  1607. goto set_value;
  1608. }
  1609. rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_SHORT_RETRY_LIMIT,
  1610. &retry, sizeof(retry));
  1611. if (rc)
  1612. goto out;
  1613. wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_SHORT;
  1614. set_value:
  1615. wrqu->retry.value = retry;
  1616. wrqu->retry.disabled = 0;
  1617. out:
  1618. return rc;
  1619. }
  1620. static int wl3501_get_encode(struct net_device *dev,
  1621. struct iw_request_info *info,
  1622. union iwreq_data *wrqu, char *extra)
  1623. {
  1624. u8 implemented, restricted, keys[100], len_keys, tocopy;
  1625. struct wl3501_card *this = netdev_priv(dev);
  1626. int rc = wl3501_get_mib_value(this,
  1627. WL3501_MIB_ATTR_PRIV_OPT_IMPLEMENTED,
  1628. &implemented, sizeof(implemented));
  1629. if (rc)
  1630. goto out;
  1631. if (!implemented) {
  1632. wrqu->encoding.flags = IW_ENCODE_DISABLED;
  1633. goto out;
  1634. }
  1635. rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_EXCLUDE_UNENCRYPTED,
  1636. &restricted, sizeof(restricted));
  1637. if (rc)
  1638. goto out;
  1639. wrqu->encoding.flags = restricted ? IW_ENCODE_RESTRICTED :
  1640. IW_ENCODE_OPEN;
  1641. rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS_LEN,
  1642. &len_keys, sizeof(len_keys));
  1643. if (rc)
  1644. goto out;
  1645. rc = wl3501_get_mib_value(this, WL3501_MIB_ATTR_WEP_KEY_MAPPINGS,
  1646. keys, len_keys);
  1647. if (rc)
  1648. goto out;
  1649. tocopy = min_t(u8, len_keys, wrqu->encoding.length);
  1650. tocopy = min_t(u8, tocopy, 100);
  1651. wrqu->encoding.length = tocopy;
  1652. memcpy(extra, keys, tocopy);
  1653. out:
  1654. return rc;
  1655. }
  1656. static int wl3501_get_power(struct net_device *dev,
  1657. struct iw_request_info *info,
  1658. union iwreq_data *wrqu, char *extra)
  1659. {
  1660. u8 pwr_state;
  1661. struct wl3501_card *this = netdev_priv(dev);
  1662. int rc = wl3501_get_mib_value(this,
  1663. WL3501_MIB_ATTR_CURRENT_PWR_STATE,
  1664. &pwr_state, sizeof(pwr_state));
  1665. if (rc)
  1666. goto out;
  1667. wrqu->power.disabled = !pwr_state;
  1668. wrqu->power.flags = IW_POWER_ON;
  1669. out:
  1670. return rc;
  1671. }
  1672. static const iw_handler wl3501_handler[] = {
  1673. [SIOCGIWNAME - SIOCIWFIRST] = wl3501_get_name,
  1674. [SIOCSIWFREQ - SIOCIWFIRST] = wl3501_set_freq,
  1675. [SIOCGIWFREQ - SIOCIWFIRST] = wl3501_get_freq,
  1676. [SIOCSIWMODE - SIOCIWFIRST] = wl3501_set_mode,
  1677. [SIOCGIWMODE - SIOCIWFIRST] = wl3501_get_mode,
  1678. [SIOCGIWSENS - SIOCIWFIRST] = wl3501_get_sens,
  1679. [SIOCGIWRANGE - SIOCIWFIRST] = wl3501_get_range,
  1680. [SIOCSIWSPY - SIOCIWFIRST] = iw_handler_set_spy,
  1681. [SIOCGIWSPY - SIOCIWFIRST] = iw_handler_get_spy,
  1682. [SIOCSIWTHRSPY - SIOCIWFIRST] = iw_handler_set_thrspy,
  1683. [SIOCGIWTHRSPY - SIOCIWFIRST] = iw_handler_get_thrspy,
  1684. [SIOCSIWAP - SIOCIWFIRST] = wl3501_set_wap,
  1685. [SIOCGIWAP - SIOCIWFIRST] = wl3501_get_wap,
  1686. [SIOCSIWSCAN - SIOCIWFIRST] = wl3501_set_scan,
  1687. [SIOCGIWSCAN - SIOCIWFIRST] = wl3501_get_scan,
  1688. [SIOCSIWESSID - SIOCIWFIRST] = wl3501_set_essid,
  1689. [SIOCGIWESSID - SIOCIWFIRST] = wl3501_get_essid,
  1690. [SIOCSIWNICKN - SIOCIWFIRST] = wl3501_set_nick,
  1691. [SIOCGIWNICKN - SIOCIWFIRST] = wl3501_get_nick,
  1692. [SIOCGIWRATE - SIOCIWFIRST] = wl3501_get_rate,
  1693. [SIOCGIWRTS - SIOCIWFIRST] = wl3501_get_rts_threshold,
  1694. [SIOCGIWFRAG - SIOCIWFIRST] = wl3501_get_frag_threshold,
  1695. [SIOCGIWTXPOW - SIOCIWFIRST] = wl3501_get_txpow,
  1696. [SIOCGIWRETRY - SIOCIWFIRST] = wl3501_get_retry,
  1697. [SIOCGIWENCODE - SIOCIWFIRST] = wl3501_get_encode,
  1698. [SIOCGIWPOWER - SIOCIWFIRST] = wl3501_get_power,
  1699. };
  1700. static const struct iw_handler_def wl3501_handler_def = {
  1701. .num_standard = ARRAY_SIZE(wl3501_handler),
  1702. .standard = (iw_handler *)wl3501_handler,
  1703. .get_wireless_stats = wl3501_get_wireless_stats,
  1704. };
  1705. static const struct net_device_ops wl3501_netdev_ops = {
  1706. .ndo_open = wl3501_open,
  1707. .ndo_stop = wl3501_close,
  1708. .ndo_start_xmit = wl3501_hard_start_xmit,
  1709. .ndo_tx_timeout = wl3501_tx_timeout,
  1710. .ndo_change_mtu = eth_change_mtu,
  1711. .ndo_set_mac_address = eth_mac_addr,
  1712. .ndo_validate_addr = eth_validate_addr,
  1713. };
  1714. /**
  1715. * wl3501_attach - creates an "instance" of the driver
  1716. *
  1717. * Creates an "instance" of the driver, allocating local data structures for
  1718. * one device. The device is registered with Card Services.
  1719. *
  1720. * The dev_link structure is initialized, but we don't actually configure the
  1721. * card at this point -- we wait until we receive a card insertion event.
  1722. */
  1723. static int wl3501_probe(struct pcmcia_device *p_dev)
  1724. {
  1725. struct net_device *dev;
  1726. struct wl3501_card *this;
  1727. /* The io structure describes IO port mapping */
  1728. p_dev->io.NumPorts1 = 16;
  1729. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  1730. p_dev->io.IOAddrLines = 5;
  1731. /* Interrupt setup */
  1732. p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT;
  1733. p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
  1734. p_dev->irq.Handler = wl3501_interrupt;
  1735. /* General socket configuration */
  1736. p_dev->conf.Attributes = CONF_ENABLE_IRQ;
  1737. p_dev->conf.IntType = INT_MEMORY_AND_IO;
  1738. p_dev->conf.ConfigIndex = 1;
  1739. dev = alloc_etherdev(sizeof(struct wl3501_card));
  1740. if (!dev)
  1741. goto out_link;
  1742. dev->netdev_ops = &wl3501_netdev_ops;
  1743. dev->watchdog_timeo = 5 * HZ;
  1744. this = netdev_priv(dev);
  1745. this->wireless_data.spy_data = &this->spy_data;
  1746. this->p_dev = p_dev;
  1747. dev->wireless_data = &this->wireless_data;
  1748. dev->wireless_handlers = &wl3501_handler_def;
  1749. SET_ETHTOOL_OPS(dev, &ops);
  1750. netif_stop_queue(dev);
  1751. p_dev->priv = p_dev->irq.Instance = dev;
  1752. return wl3501_config(p_dev);
  1753. out_link:
  1754. return -ENOMEM;
  1755. }
  1756. #define CS_CHECK(fn, ret) \
  1757. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  1758. /**
  1759. * wl3501_config - configure the PCMCIA socket and make eth device available
  1760. * @link - FILL_IN
  1761. *
  1762. * wl3501_config() is scheduled to run after a CARD_INSERTION event is
  1763. * received, to configure the PCMCIA socket, and to make the ethernet device
  1764. * available to the system.
  1765. */
  1766. static int wl3501_config(struct pcmcia_device *link)
  1767. {
  1768. struct net_device *dev = link->priv;
  1769. int i = 0, j, last_fn, last_ret;
  1770. struct wl3501_card *this;
  1771. /* Try allocating IO ports. This tries a few fixed addresses. If you
  1772. * want, you can also read the card's config table to pick addresses --
  1773. * see the serial driver for an example. */
  1774. for (j = 0x280; j < 0x400; j += 0x20) {
  1775. /* The '^0x300' is so that we probe 0x300-0x3ff first, then
  1776. * 0x200-0x2ff, and so on, because this seems safer */
  1777. link->io.BasePort1 = j;
  1778. link->io.BasePort2 = link->io.BasePort1 + 0x10;
  1779. i = pcmcia_request_io(link, &link->io);
  1780. if (i == 0)
  1781. break;
  1782. }
  1783. if (i != 0) {
  1784. cs_error(link, RequestIO, i);
  1785. goto failed;
  1786. }
  1787. /* Now allocate an interrupt line. Note that this does not actually
  1788. * assign a handler to the interrupt. */
  1789. CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
  1790. /* This actually configures the PCMCIA socket -- setting up the I/O
  1791. * windows and the interrupt mapping. */
  1792. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
  1793. dev->irq = link->irq.AssignedIRQ;
  1794. dev->base_addr = link->io.BasePort1;
  1795. SET_NETDEV_DEV(dev, &handle_to_dev(link));
  1796. if (register_netdev(dev)) {
  1797. printk(KERN_NOTICE "wl3501_cs: register_netdev() failed\n");
  1798. goto failed;
  1799. }
  1800. this = netdev_priv(dev);
  1801. /*
  1802. * At this point, the dev_node_t structure(s) should be initialized and
  1803. * arranged in a linked list at link->dev_node.
  1804. */
  1805. link->dev_node = &this->node;
  1806. this->base_addr = dev->base_addr;
  1807. if (!wl3501_get_flash_mac_addr(this)) {
  1808. printk(KERN_WARNING "%s: Cant read MAC addr in flash ROM?\n",
  1809. dev->name);
  1810. goto failed;
  1811. }
  1812. strcpy(this->node.dev_name, dev->name);
  1813. for (i = 0; i < 6; i++)
  1814. dev->dev_addr[i] = ((char *)&this->mac_addr)[i];
  1815. /* print probe information */
  1816. printk(KERN_INFO "%s: wl3501 @ 0x%3.3x, IRQ %d, "
  1817. "MAC addr in flash ROM:%pM\n",
  1818. dev->name, this->base_addr, (int)dev->irq,
  1819. dev->dev_addr);
  1820. /*
  1821. * Initialize card parameters - added by jss
  1822. */
  1823. this->net_type = IW_MODE_INFRA;
  1824. this->bss_cnt = 0;
  1825. this->join_sta_bss = 0;
  1826. this->adhoc_times = 0;
  1827. iw_set_mgmt_info_element(IW_MGMT_INFO_ELEMENT_SSID, &this->essid.el,
  1828. "ANY", 3);
  1829. this->card_name[0] = '\0';
  1830. this->firmware_date[0] = '\0';
  1831. this->rssi = 255;
  1832. this->chan = iw_default_channel(this->reg_domain);
  1833. strlcpy(this->nick, "Planet WL3501", sizeof(this->nick));
  1834. spin_lock_init(&this->lock);
  1835. init_waitqueue_head(&this->wait);
  1836. netif_start_queue(dev);
  1837. return 0;
  1838. cs_failed:
  1839. cs_error(link, last_fn, last_ret);
  1840. failed:
  1841. wl3501_release(link);
  1842. return -ENODEV;
  1843. }
  1844. /**
  1845. * wl3501_release - unregister the net, release PCMCIA configuration
  1846. * @arg - link
  1847. *
  1848. * After a card is removed, wl3501_release() will unregister the net device,
  1849. * and release the PCMCIA configuration. If the device is still open, this
  1850. * will be postponed until it is closed.
  1851. */
  1852. static void wl3501_release(struct pcmcia_device *link)
  1853. {
  1854. struct net_device *dev = link->priv;
  1855. /* Unlink the device chain */
  1856. if (link->dev_node)
  1857. unregister_netdev(dev);
  1858. pcmcia_disable_device(link);
  1859. }
  1860. static int wl3501_suspend(struct pcmcia_device *link)
  1861. {
  1862. struct net_device *dev = link->priv;
  1863. wl3501_pwr_mgmt(netdev_priv(dev), WL3501_SUSPEND);
  1864. if (link->open)
  1865. netif_device_detach(dev);
  1866. return 0;
  1867. }
  1868. static int wl3501_resume(struct pcmcia_device *link)
  1869. {
  1870. struct net_device *dev = link->priv;
  1871. wl3501_pwr_mgmt(netdev_priv(dev), WL3501_RESUME);
  1872. if (link->open) {
  1873. wl3501_reset(dev);
  1874. netif_device_attach(dev);
  1875. }
  1876. return 0;
  1877. }
  1878. static struct pcmcia_device_id wl3501_ids[] = {
  1879. PCMCIA_DEVICE_MANF_CARD(0xd601, 0x0001),
  1880. PCMCIA_DEVICE_NULL
  1881. };
  1882. MODULE_DEVICE_TABLE(pcmcia, wl3501_ids);
  1883. static struct pcmcia_driver wl3501_driver = {
  1884. .owner = THIS_MODULE,
  1885. .drv = {
  1886. .name = "wl3501_cs",
  1887. },
  1888. .probe = wl3501_probe,
  1889. .remove = wl3501_detach,
  1890. .id_table = wl3501_ids,
  1891. .suspend = wl3501_suspend,
  1892. .resume = wl3501_resume,
  1893. };
  1894. static int __init wl3501_init_module(void)
  1895. {
  1896. return pcmcia_register_driver(&wl3501_driver);
  1897. }
  1898. static void __exit wl3501_exit_module(void)
  1899. {
  1900. pcmcia_unregister_driver(&wl3501_driver);
  1901. }
  1902. module_init(wl3501_init_module);
  1903. module_exit(wl3501_exit_module);
  1904. MODULE_AUTHOR("Fox Chen <mhchen@golf.ccl.itri.org.tw>, "
  1905. "Arnaldo Carvalho de Melo <acme@conectiva.com.br>,"
  1906. "Gustavo Niemeyer <niemeyer@conectiva.com>");
  1907. MODULE_DESCRIPTION("Planet wl3501 wireless driver");
  1908. MODULE_LICENSE("GPL");