wl3501_cs.c 54 KB

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