wl3501_cs.c 57 KB

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