wl3501_cs.c 54 KB

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