wl3501_cs.c 54 KB

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