wl3501_cs.c 54 KB

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