wl3501_cs.c 56 KB

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