wl3501_cs.c 56 KB

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