wl3501_cs.c 55 KB

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