wl3501_cs.c 54 KB

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