wl3501_cs.c 57 KB

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