wl3501_cs.c 58 KB

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