wl3501_cs.c 54 KB

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