wl3501_cs.c 56 KB

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