wl3501_cs.c 58 KB

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