wl3501_cs.c 54 KB

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