main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /**
  2. * This file contains the major functions in WLAN
  3. * driver. It includes init, exit, open, close and main
  4. * thread etc..
  5. */
  6. #include <linux/moduleparam.h>
  7. #include <linux/delay.h>
  8. #include <linux/freezer.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/if_arp.h>
  12. #include <net/iw_handler.h>
  13. #include <net/ieee80211.h>
  14. #include "host.h"
  15. #include "decl.h"
  16. #include "dev.h"
  17. #include "wext.h"
  18. #include "debugfs.h"
  19. #include "assoc.h"
  20. #define DRIVER_RELEASE_VERSION "322.p0"
  21. const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  22. #ifdef DEBUG
  23. "-dbg"
  24. #endif
  25. "";
  26. /* Module parameters */
  27. unsigned int libertas_debug = 0;
  28. module_param(libertas_debug, int, 0644);
  29. EXPORT_SYMBOL_GPL(libertas_debug);
  30. #define WLAN_TX_PWR_DEFAULT 20 /*100mW */
  31. #define WLAN_TX_PWR_US_DEFAULT 20 /*100mW */
  32. #define WLAN_TX_PWR_JP_DEFAULT 16 /*50mW */
  33. #define WLAN_TX_PWR_FR_DEFAULT 20 /*100mW */
  34. #define WLAN_TX_PWR_EMEA_DEFAULT 20 /*100mW */
  35. /* Format { channel, frequency (MHz), maxtxpower } */
  36. /* band: 'B/G', region: USA FCC/Canada IC */
  37. static struct chan_freq_power channel_freq_power_US_BG[] = {
  38. {1, 2412, WLAN_TX_PWR_US_DEFAULT},
  39. {2, 2417, WLAN_TX_PWR_US_DEFAULT},
  40. {3, 2422, WLAN_TX_PWR_US_DEFAULT},
  41. {4, 2427, WLAN_TX_PWR_US_DEFAULT},
  42. {5, 2432, WLAN_TX_PWR_US_DEFAULT},
  43. {6, 2437, WLAN_TX_PWR_US_DEFAULT},
  44. {7, 2442, WLAN_TX_PWR_US_DEFAULT},
  45. {8, 2447, WLAN_TX_PWR_US_DEFAULT},
  46. {9, 2452, WLAN_TX_PWR_US_DEFAULT},
  47. {10, 2457, WLAN_TX_PWR_US_DEFAULT},
  48. {11, 2462, WLAN_TX_PWR_US_DEFAULT}
  49. };
  50. /* band: 'B/G', region: Europe ETSI */
  51. static struct chan_freq_power channel_freq_power_EU_BG[] = {
  52. {1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
  53. {2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
  54. {3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
  55. {4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
  56. {5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
  57. {6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
  58. {7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
  59. {8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
  60. {9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
  61. {10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
  62. {11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
  63. {12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
  64. {13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
  65. };
  66. /* band: 'B/G', region: Spain */
  67. static struct chan_freq_power channel_freq_power_SPN_BG[] = {
  68. {10, 2457, WLAN_TX_PWR_DEFAULT},
  69. {11, 2462, WLAN_TX_PWR_DEFAULT}
  70. };
  71. /* band: 'B/G', region: France */
  72. static struct chan_freq_power channel_freq_power_FR_BG[] = {
  73. {10, 2457, WLAN_TX_PWR_FR_DEFAULT},
  74. {11, 2462, WLAN_TX_PWR_FR_DEFAULT},
  75. {12, 2467, WLAN_TX_PWR_FR_DEFAULT},
  76. {13, 2472, WLAN_TX_PWR_FR_DEFAULT}
  77. };
  78. /* band: 'B/G', region: Japan */
  79. static struct chan_freq_power channel_freq_power_JPN_BG[] = {
  80. {1, 2412, WLAN_TX_PWR_JP_DEFAULT},
  81. {2, 2417, WLAN_TX_PWR_JP_DEFAULT},
  82. {3, 2422, WLAN_TX_PWR_JP_DEFAULT},
  83. {4, 2427, WLAN_TX_PWR_JP_DEFAULT},
  84. {5, 2432, WLAN_TX_PWR_JP_DEFAULT},
  85. {6, 2437, WLAN_TX_PWR_JP_DEFAULT},
  86. {7, 2442, WLAN_TX_PWR_JP_DEFAULT},
  87. {8, 2447, WLAN_TX_PWR_JP_DEFAULT},
  88. {9, 2452, WLAN_TX_PWR_JP_DEFAULT},
  89. {10, 2457, WLAN_TX_PWR_JP_DEFAULT},
  90. {11, 2462, WLAN_TX_PWR_JP_DEFAULT},
  91. {12, 2467, WLAN_TX_PWR_JP_DEFAULT},
  92. {13, 2472, WLAN_TX_PWR_JP_DEFAULT},
  93. {14, 2484, WLAN_TX_PWR_JP_DEFAULT}
  94. };
  95. /**
  96. * the structure for channel, frequency and power
  97. */
  98. struct region_cfp_table {
  99. u8 region;
  100. struct chan_freq_power *cfp_BG;
  101. int cfp_no_BG;
  102. };
  103. /**
  104. * the structure for the mapping between region and CFP
  105. */
  106. static struct region_cfp_table region_cfp_table[] = {
  107. {0x10, /*US FCC */
  108. channel_freq_power_US_BG,
  109. sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
  110. }
  111. ,
  112. {0x20, /*CANADA IC */
  113. channel_freq_power_US_BG,
  114. sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
  115. }
  116. ,
  117. {0x30, /*EU*/ channel_freq_power_EU_BG,
  118. sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
  119. }
  120. ,
  121. {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
  122. sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
  123. }
  124. ,
  125. {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
  126. sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
  127. }
  128. ,
  129. {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
  130. sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
  131. }
  132. ,
  133. /*Add new region here */
  134. };
  135. /**
  136. * the rates supported
  137. */
  138. u8 libertas_supported_rates[G_SUPPORTED_RATES] =
  139. { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  140. 0 };
  141. /**
  142. * the rates supported for ad-hoc G mode
  143. */
  144. u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
  145. { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  146. 0 };
  147. /**
  148. * the rates supported for ad-hoc B mode
  149. */
  150. u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
  151. /**
  152. * the table to keep region code
  153. */
  154. u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  155. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  156. /**
  157. * Attributes exported through sysfs
  158. */
  159. /**
  160. * @brief Get function for sysfs attribute libertas_mpp
  161. */
  162. static ssize_t libertas_mpp_get(struct device * dev,
  163. struct device_attribute *attr, char * buf) {
  164. struct cmd_ds_mesh_access mesh_access;
  165. memset(&mesh_access, 0, sizeof(mesh_access));
  166. libertas_prepare_and_send_command(to_net_dev(dev)->priv,
  167. cmd_mesh_access,
  168. cmd_act_mesh_get_mpp,
  169. cmd_option_waitforrsp, 0, (void *)&mesh_access);
  170. return snprintf(buf, 3, "%d\n", le32_to_cpu(mesh_access.data[0]));
  171. }
  172. /**
  173. * @brief Set function for sysfs attribute libertas_mpp
  174. */
  175. static ssize_t libertas_mpp_set(struct device * dev,
  176. struct device_attribute *attr, const char * buf, size_t count) {
  177. struct cmd_ds_mesh_access mesh_access;
  178. uint32_t datum;
  179. memset(&mesh_access, 0, sizeof(mesh_access));
  180. sscanf(buf, "%d", &datum);
  181. mesh_access.data[0] = cpu_to_le32(datum);
  182. libertas_prepare_and_send_command((to_net_dev(dev))->priv,
  183. cmd_mesh_access,
  184. cmd_act_mesh_set_mpp,
  185. cmd_option_waitforrsp, 0, (void *)&mesh_access);
  186. return strlen(buf);
  187. }
  188. /**
  189. * libertas_mpp attribute to be exported per mshX interface
  190. * through sysfs (/sys/class/net/mshX/libertas-mpp)
  191. */
  192. static DEVICE_ATTR(libertas_mpp, 0644, libertas_mpp_get, libertas_mpp_set );
  193. /**
  194. * @brief Check if the device can be open and wait if necessary.
  195. *
  196. * @param dev A pointer to net_device structure
  197. * @return 0
  198. *
  199. * For USB adapter, on some systems the device open handler will be
  200. * called before FW ready. Use the following flag check and wait
  201. * function to work around the issue.
  202. *
  203. */
  204. static int pre_open_check(struct net_device *dev)
  205. {
  206. wlan_private *priv = (wlan_private *) dev->priv;
  207. wlan_adapter *adapter = priv->adapter;
  208. int i = 0;
  209. while (!adapter->fw_ready && i < 20) {
  210. i++;
  211. msleep_interruptible(100);
  212. }
  213. if (!adapter->fw_ready) {
  214. lbs_pr_err("firmware not ready\n");
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. /**
  220. * @brief This function opens the device
  221. *
  222. * @param dev A pointer to net_device structure
  223. * @return 0
  224. */
  225. static int wlan_dev_open(struct net_device *dev)
  226. {
  227. wlan_private *priv = (wlan_private *) dev->priv;
  228. wlan_adapter *adapter = priv->adapter;
  229. lbs_deb_enter(LBS_DEB_NET);
  230. priv->open = 1;
  231. if (adapter->connect_status == libertas_connected) {
  232. netif_carrier_on(priv->dev);
  233. netif_carrier_on(priv->mesh_dev);
  234. } else {
  235. netif_carrier_off(priv->dev);
  236. netif_carrier_off(priv->mesh_dev);
  237. }
  238. lbs_deb_leave(LBS_DEB_NET);
  239. return 0;
  240. }
  241. /**
  242. * @brief This function opens the mshX interface
  243. *
  244. * @param dev A pointer to net_device structure
  245. * @return 0
  246. */
  247. static int mesh_open(struct net_device *dev)
  248. {
  249. wlan_private *priv = (wlan_private *) dev->priv ;
  250. if (pre_open_check(dev) == -1)
  251. return -1;
  252. priv->mesh_open = 1 ;
  253. netif_wake_queue(priv->mesh_dev);
  254. if (priv->infra_open == 0)
  255. return wlan_dev_open(priv->dev) ;
  256. return 0;
  257. }
  258. /**
  259. * @brief This function opens the ethX interface
  260. *
  261. * @param dev A pointer to net_device structure
  262. * @return 0
  263. */
  264. static int wlan_open(struct net_device *dev)
  265. {
  266. wlan_private *priv = (wlan_private *) dev->priv ;
  267. if(pre_open_check(dev) == -1)
  268. return -1;
  269. priv->infra_open = 1 ;
  270. netif_wake_queue(priv->dev);
  271. if (priv->open == 0)
  272. return wlan_dev_open(priv->dev) ;
  273. return 0;
  274. }
  275. static int wlan_dev_close(struct net_device *dev)
  276. {
  277. wlan_private *priv = dev->priv;
  278. lbs_deb_enter(LBS_DEB_NET);
  279. netif_carrier_off(priv->dev);
  280. priv->open = 0;
  281. lbs_deb_leave(LBS_DEB_NET);
  282. return 0;
  283. }
  284. /**
  285. * @brief This function closes the mshX interface
  286. *
  287. * @param dev A pointer to net_device structure
  288. * @return 0
  289. */
  290. static int mesh_close(struct net_device *dev)
  291. {
  292. wlan_private *priv = (wlan_private *) (dev->priv);
  293. priv->mesh_open = 0;
  294. netif_stop_queue(priv->mesh_dev);
  295. if (priv->infra_open == 0)
  296. return wlan_dev_close(dev);
  297. else
  298. return 0;
  299. }
  300. /**
  301. * @brief This function closes the ethX interface
  302. *
  303. * @param dev A pointer to net_device structure
  304. * @return 0
  305. */
  306. static int wlan_close(struct net_device *dev)
  307. {
  308. wlan_private *priv = (wlan_private *) dev->priv;
  309. netif_stop_queue(dev);
  310. priv->infra_open = 0;
  311. if (priv->mesh_open == 0)
  312. return wlan_dev_close(dev);
  313. else
  314. return 0;
  315. }
  316. static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  317. {
  318. int ret = 0;
  319. wlan_private *priv = dev->priv;
  320. lbs_deb_enter(LBS_DEB_NET);
  321. if (priv->dnld_sent || priv->adapter->TxLockFlag) {
  322. priv->stats.tx_dropped++;
  323. goto done;
  324. }
  325. netif_stop_queue(priv->dev);
  326. netif_stop_queue(priv->mesh_dev);
  327. if (libertas_process_tx(priv, skb) == 0)
  328. dev->trans_start = jiffies;
  329. done:
  330. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  331. return ret;
  332. }
  333. /**
  334. * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
  335. *
  336. */
  337. static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
  338. {
  339. wlan_private *priv = dev->priv;
  340. int ret;
  341. lbs_deb_enter(LBS_DEB_MESH);
  342. SET_MESH_FRAME(skb);
  343. ret = wlan_hard_start_xmit(skb, priv->dev);
  344. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  345. return ret;
  346. }
  347. /**
  348. * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
  349. *
  350. */
  351. static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
  352. {
  353. int ret;
  354. lbs_deb_enter(LBS_DEB_NET);
  355. UNSET_MESH_FRAME(skb);
  356. ret = wlan_hard_start_xmit(skb, dev);
  357. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  358. return ret;
  359. }
  360. static void wlan_tx_timeout(struct net_device *dev)
  361. {
  362. wlan_private *priv = (wlan_private *) dev->priv;
  363. lbs_deb_enter(LBS_DEB_TX);
  364. lbs_pr_err("tx watch dog timeout\n");
  365. priv->dnld_sent = DNLD_RES_RECEIVED;
  366. dev->trans_start = jiffies;
  367. if (priv->adapter->currenttxskb) {
  368. if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
  369. /* If we are here, we have not received feedback from
  370. the previous packet. Assume TX_FAIL and move on. */
  371. priv->adapter->eventcause = 0x01000000;
  372. libertas_send_tx_feedback(priv);
  373. } else
  374. wake_up_interruptible(&priv->mainthread.waitq);
  375. } else if (priv->adapter->connect_status == libertas_connected) {
  376. netif_wake_queue(priv->dev);
  377. netif_wake_queue(priv->mesh_dev);
  378. }
  379. lbs_deb_leave(LBS_DEB_TX);
  380. }
  381. /**
  382. * @brief This function returns the network statistics
  383. *
  384. * @param dev A pointer to wlan_private structure
  385. * @return A pointer to net_device_stats structure
  386. */
  387. static struct net_device_stats *wlan_get_stats(struct net_device *dev)
  388. {
  389. wlan_private *priv = (wlan_private *) dev->priv;
  390. return &priv->stats;
  391. }
  392. static int wlan_set_mac_address(struct net_device *dev, void *addr)
  393. {
  394. int ret = 0;
  395. wlan_private *priv = (wlan_private *) dev->priv;
  396. wlan_adapter *adapter = priv->adapter;
  397. struct sockaddr *phwaddr = addr;
  398. lbs_deb_enter(LBS_DEB_NET);
  399. /* In case it was called from the mesh device */
  400. dev = priv->dev ;
  401. memset(adapter->current_addr, 0, ETH_ALEN);
  402. /* dev->dev_addr is 8 bytes */
  403. lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
  404. lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
  405. memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
  406. ret = libertas_prepare_and_send_command(priv, cmd_802_11_mac_address,
  407. cmd_act_set,
  408. cmd_option_waitforrsp, 0, NULL);
  409. if (ret) {
  410. lbs_deb_net("set MAC address failed\n");
  411. ret = -1;
  412. goto done;
  413. }
  414. lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
  415. memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
  416. if (priv->mesh_dev)
  417. memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
  418. done:
  419. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  420. return ret;
  421. }
  422. static int wlan_copy_multicast_address(wlan_adapter * adapter,
  423. struct net_device *dev)
  424. {
  425. int i = 0;
  426. struct dev_mc_list *mcptr = dev->mc_list;
  427. for (i = 0; i < dev->mc_count; i++) {
  428. memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  429. mcptr = mcptr->next;
  430. }
  431. return i;
  432. }
  433. static void wlan_set_multicast_list(struct net_device *dev)
  434. {
  435. wlan_private *priv = dev->priv;
  436. wlan_adapter *adapter = priv->adapter;
  437. int oldpacketfilter;
  438. lbs_deb_enter(LBS_DEB_NET);
  439. oldpacketfilter = adapter->currentpacketfilter;
  440. if (dev->flags & IFF_PROMISC) {
  441. lbs_deb_net("enable promiscuous mode\n");
  442. adapter->currentpacketfilter |=
  443. cmd_act_mac_promiscuous_enable;
  444. adapter->currentpacketfilter &=
  445. ~(cmd_act_mac_all_multicast_enable |
  446. cmd_act_mac_multicast_enable);
  447. } else {
  448. /* Multicast */
  449. adapter->currentpacketfilter &=
  450. ~cmd_act_mac_promiscuous_enable;
  451. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  452. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  453. lbs_deb_net( "enabling all multicast\n");
  454. adapter->currentpacketfilter |=
  455. cmd_act_mac_all_multicast_enable;
  456. adapter->currentpacketfilter &=
  457. ~cmd_act_mac_multicast_enable;
  458. } else {
  459. adapter->currentpacketfilter &=
  460. ~cmd_act_mac_all_multicast_enable;
  461. if (!dev->mc_count) {
  462. lbs_deb_net("no multicast addresses, "
  463. "disabling multicast\n");
  464. adapter->currentpacketfilter &=
  465. ~cmd_act_mac_multicast_enable;
  466. } else {
  467. int i;
  468. adapter->currentpacketfilter |=
  469. cmd_act_mac_multicast_enable;
  470. adapter->nr_of_multicastmacaddr =
  471. wlan_copy_multicast_address(adapter, dev);
  472. lbs_deb_net("multicast addresses: %d\n",
  473. dev->mc_count);
  474. for (i = 0; i < dev->mc_count; i++) {
  475. lbs_deb_net("Multicast address %d:"
  476. MAC_FMT "\n", i,
  477. adapter->multicastlist[i][0],
  478. adapter->multicastlist[i][1],
  479. adapter->multicastlist[i][2],
  480. adapter->multicastlist[i][3],
  481. adapter->multicastlist[i][4],
  482. adapter->multicastlist[i][5]);
  483. }
  484. /* send multicast addresses to firmware */
  485. libertas_prepare_and_send_command(priv,
  486. cmd_mac_multicast_adr,
  487. cmd_act_set, 0, 0,
  488. NULL);
  489. }
  490. }
  491. }
  492. if (adapter->currentpacketfilter != oldpacketfilter) {
  493. libertas_set_mac_packet_filter(priv);
  494. }
  495. lbs_deb_leave(LBS_DEB_NET);
  496. }
  497. /**
  498. * @brief This function handles the major jobs in the WLAN driver.
  499. * It handles all events generated by firmware, RX data received
  500. * from firmware and TX data sent from kernel.
  501. *
  502. * @param data A pointer to wlan_thread structure
  503. * @return 0
  504. */
  505. static int wlan_service_main_thread(void *data)
  506. {
  507. struct wlan_thread *thread = data;
  508. wlan_private *priv = thread->priv;
  509. wlan_adapter *adapter = priv->adapter;
  510. wait_queue_t wait;
  511. u8 ireg = 0;
  512. lbs_deb_enter(LBS_DEB_THREAD);
  513. wlan_activate_thread(thread);
  514. init_waitqueue_entry(&wait, current);
  515. for (;;) {
  516. lbs_deb_thread( "main-thread 111: intcounter=%d "
  517. "currenttxskb=%p dnld_sent=%d\n",
  518. adapter->intcounter,
  519. adapter->currenttxskb, priv->dnld_sent);
  520. add_wait_queue(&thread->waitq, &wait);
  521. set_current_state(TASK_INTERRUPTIBLE);
  522. spin_lock_irq(&adapter->driver_lock);
  523. if ((adapter->psstate == PS_STATE_SLEEP) ||
  524. (!adapter->intcounter
  525. && (priv->dnld_sent || adapter->cur_cmd ||
  526. list_empty(&adapter->cmdpendingq)))) {
  527. lbs_deb_thread(
  528. "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  529. adapter->connect_status, adapter->intcounter,
  530. adapter->psmode, adapter->psstate);
  531. spin_unlock_irq(&adapter->driver_lock);
  532. schedule();
  533. } else
  534. spin_unlock_irq(&adapter->driver_lock);
  535. lbs_deb_thread(
  536. "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
  537. "dnld_sent=%d\n", adapter->intcounter,
  538. adapter->currenttxskb, priv->dnld_sent);
  539. set_current_state(TASK_RUNNING);
  540. remove_wait_queue(&thread->waitq, &wait);
  541. try_to_freeze();
  542. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
  543. "dnld_sent=%d\n",
  544. adapter->intcounter,
  545. adapter->currenttxskb, priv->dnld_sent);
  546. if (kthread_should_stop()
  547. || adapter->surpriseremoved) {
  548. lbs_deb_thread(
  549. "main-thread: break from main thread: surpriseremoved=0x%x\n",
  550. adapter->surpriseremoved);
  551. break;
  552. }
  553. spin_lock_irq(&adapter->driver_lock);
  554. if (adapter->intcounter) {
  555. u8 int_status;
  556. adapter->intcounter = 0;
  557. int_status = priv->hw_get_int_status(priv, &ireg);
  558. if (int_status) {
  559. lbs_deb_thread(
  560. "main-thread: reading HOST_INT_STATUS_REG failed\n");
  561. spin_unlock_irq(&adapter->driver_lock);
  562. continue;
  563. }
  564. adapter->hisregcpy |= ireg;
  565. }
  566. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
  567. "dnld_sent=%d\n",
  568. adapter->intcounter,
  569. adapter->currenttxskb, priv->dnld_sent);
  570. /* command response? */
  571. if (adapter->hisregcpy & his_cmdupldrdy) {
  572. lbs_deb_thread("main-thread: cmd response ready\n");
  573. adapter->hisregcpy &= ~his_cmdupldrdy;
  574. spin_unlock_irq(&adapter->driver_lock);
  575. libertas_process_rx_command(priv);
  576. spin_lock_irq(&adapter->driver_lock);
  577. }
  578. /* Any Card Event */
  579. if (adapter->hisregcpy & his_cardevent) {
  580. lbs_deb_thread("main-thread: Card Event Activity\n");
  581. adapter->hisregcpy &= ~his_cardevent;
  582. if (priv->hw_read_event_cause(priv)) {
  583. lbs_pr_alert(
  584. "main-thread: hw_read_event_cause failed\n");
  585. spin_unlock_irq(&adapter->driver_lock);
  586. continue;
  587. }
  588. spin_unlock_irq(&adapter->driver_lock);
  589. libertas_process_event(priv);
  590. } else
  591. spin_unlock_irq(&adapter->driver_lock);
  592. /* Check if we need to confirm Sleep Request received previously */
  593. if (adapter->psstate == PS_STATE_PRE_SLEEP) {
  594. if (!priv->dnld_sent && !adapter->cur_cmd) {
  595. if (adapter->connect_status ==
  596. libertas_connected) {
  597. lbs_deb_thread(
  598. "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
  599. "dnld_sent=%d cur_cmd=%p, confirm now\n",
  600. adapter->intcounter,
  601. adapter->currenttxskb,
  602. priv->dnld_sent,
  603. adapter->cur_cmd);
  604. libertas_ps_confirm_sleep(priv,
  605. (u16) adapter->psmode);
  606. } else {
  607. /* workaround for firmware sending
  608. * deauth/linkloss event immediately
  609. * after sleep request, remove this
  610. * after firmware fixes it
  611. */
  612. adapter->psstate = PS_STATE_AWAKE;
  613. lbs_pr_alert(
  614. "main-thread: ignore PS_SleepConfirm in non-connected state\n");
  615. }
  616. }
  617. }
  618. /* The PS state is changed during processing of Sleep Request
  619. * event above
  620. */
  621. if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
  622. (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
  623. continue;
  624. /* Execute the next command */
  625. if (!priv->dnld_sent && !priv->adapter->cur_cmd)
  626. libertas_execute_next_command(priv);
  627. /* Wake-up command waiters which can't sleep in
  628. * libertas_prepare_and_send_command
  629. */
  630. if (!adapter->nr_cmd_pending)
  631. wake_up_all(&adapter->cmd_pending);
  632. libertas_tx_runqueue(priv);
  633. }
  634. del_timer(&adapter->command_timer);
  635. adapter->nr_cmd_pending = 0;
  636. wake_up_all(&adapter->cmd_pending);
  637. wlan_deactivate_thread(thread);
  638. lbs_deb_leave(LBS_DEB_THREAD);
  639. return 0;
  640. }
  641. /**
  642. * @brief This function adds the card. it will probe the
  643. * card, allocate the wlan_priv and initialize the device.
  644. *
  645. * @param card A pointer to card
  646. * @return A pointer to wlan_private structure
  647. */
  648. wlan_private *libertas_add_card(void *card, struct device *dmdev)
  649. {
  650. struct net_device *dev = NULL;
  651. wlan_private *priv = NULL;
  652. lbs_deb_enter(LBS_DEB_NET);
  653. /* Allocate an Ethernet device and register it */
  654. if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
  655. lbs_pr_err("init ethX device failed\n");
  656. return NULL;
  657. }
  658. priv = dev->priv;
  659. /* allocate buffer for wlan_adapter */
  660. if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
  661. lbs_pr_err("allocate buffer for wlan_adapter failed\n");
  662. goto err_kzalloc;
  663. }
  664. priv->dev = dev;
  665. priv->card = card;
  666. priv->mesh_open = 0;
  667. priv->infra_open = 0;
  668. SET_MODULE_OWNER(dev);
  669. /* Setup the OS Interface to our functions */
  670. dev->open = wlan_open;
  671. dev->hard_start_xmit = wlan_pre_start_xmit;
  672. dev->stop = wlan_close;
  673. dev->do_ioctl = libertas_do_ioctl;
  674. dev->set_mac_address = wlan_set_mac_address;
  675. dev->tx_timeout = wlan_tx_timeout;
  676. dev->get_stats = wlan_get_stats;
  677. dev->watchdog_timeo = 5 * HZ;
  678. dev->ethtool_ops = &libertas_ethtool_ops;
  679. #ifdef WIRELESS_EXT
  680. dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  681. #endif
  682. #define NETIF_F_DYNALLOC 16
  683. dev->features |= NETIF_F_DYNALLOC;
  684. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  685. dev->set_multicast_list = wlan_set_multicast_list;
  686. SET_NETDEV_DEV(dev, dmdev);
  687. INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
  688. INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
  689. spin_lock_init(&priv->adapter->driver_lock);
  690. init_waitqueue_head(&priv->adapter->cmd_pending);
  691. priv->adapter->nr_cmd_pending = 0;
  692. goto done;
  693. err_kzalloc:
  694. free_netdev(dev);
  695. priv = NULL;
  696. done:
  697. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  698. return priv;
  699. }
  700. EXPORT_SYMBOL_GPL(libertas_add_card);
  701. int libertas_activate_card(wlan_private *priv, char *fw_name)
  702. {
  703. struct net_device *dev = priv->dev;
  704. int ret = -1;
  705. lbs_deb_enter(LBS_DEB_MAIN);
  706. lbs_deb_thread("Starting kthread...\n");
  707. priv->mainthread.priv = priv;
  708. wlan_create_thread(wlan_service_main_thread,
  709. &priv->mainthread, "wlan_main_service");
  710. priv->assoc_thread =
  711. create_singlethread_workqueue("libertas_assoc");
  712. INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
  713. /*
  714. * Register the device. Fillup the private data structure with
  715. * relevant information from the card and request for the required
  716. * IRQ.
  717. */
  718. if (priv->hw_register_dev(priv) < 0) {
  719. lbs_pr_err("failed to register WLAN device\n");
  720. goto err_registerdev;
  721. }
  722. /* init FW and HW */
  723. if (fw_name && libertas_init_fw(priv, fw_name)) {
  724. lbs_pr_err("firmware init failed\n");
  725. goto err_registerdev;
  726. }
  727. if (register_netdev(dev)) {
  728. lbs_pr_err("cannot register ethX device\n");
  729. goto err_init_fw;
  730. }
  731. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  732. libertas_debugfs_init_one(priv, dev);
  733. ret = 0;
  734. goto done;
  735. err_init_fw:
  736. priv->hw_unregister_dev(priv);
  737. err_registerdev:
  738. destroy_workqueue(priv->assoc_thread);
  739. /* Stop the thread servicing the interrupts */
  740. wake_up_interruptible(&priv->mainthread.waitq);
  741. wlan_terminate_thread(&priv->mainthread);
  742. done:
  743. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  744. return ret;
  745. }
  746. EXPORT_SYMBOL_GPL(libertas_activate_card);
  747. /**
  748. * @brief This function adds mshX interface
  749. *
  750. * @param priv A pointer to the wlan_private structure
  751. * @return 0 if successful, -X otherwise
  752. */
  753. int libertas_add_mesh(wlan_private *priv, struct device *dev)
  754. {
  755. struct net_device *mesh_dev = NULL;
  756. int ret = 0;
  757. lbs_deb_enter(LBS_DEB_MESH);
  758. /* Allocate a virtual mesh device */
  759. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  760. lbs_deb_mesh("init mshX device failed\n");
  761. ret = -ENOMEM;
  762. goto done;
  763. }
  764. mesh_dev->priv = priv;
  765. priv->mesh_dev = mesh_dev;
  766. SET_MODULE_OWNER(mesh_dev);
  767. mesh_dev->open = mesh_open;
  768. mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
  769. mesh_dev->stop = mesh_close;
  770. mesh_dev->do_ioctl = libertas_do_ioctl;
  771. mesh_dev->get_stats = wlan_get_stats;
  772. mesh_dev->set_mac_address = wlan_set_mac_address;
  773. mesh_dev->ethtool_ops = &libertas_ethtool_ops;
  774. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  775. sizeof(priv->dev->dev_addr));
  776. SET_NETDEV_DEV(priv->mesh_dev, dev);
  777. #ifdef WIRELESS_EXT
  778. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  779. #endif
  780. #define NETIF_F_DYNALLOC 16
  781. /* Register virtual mesh interface */
  782. ret = register_netdev(mesh_dev);
  783. if (ret) {
  784. lbs_pr_err("cannot register mshX virtual interface\n");
  785. goto err_free;
  786. }
  787. ret = device_create_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
  788. if (ret)
  789. goto err_unregister;
  790. /* Everything successful */
  791. ret = 0;
  792. goto done;
  793. err_unregister:
  794. unregister_netdev(mesh_dev);
  795. err_free:
  796. free_netdev(mesh_dev);
  797. done:
  798. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  799. return ret;
  800. }
  801. EXPORT_SYMBOL_GPL(libertas_add_mesh);
  802. static void wake_pending_cmdnodes(wlan_private *priv)
  803. {
  804. struct cmd_ctrl_node *cmdnode;
  805. unsigned long flags;
  806. lbs_deb_enter(LBS_DEB_CMD);
  807. spin_lock_irqsave(&priv->adapter->driver_lock, flags);
  808. list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
  809. cmdnode->cmdwaitqwoken = 1;
  810. wake_up_interruptible(&cmdnode->cmdwait_q);
  811. }
  812. spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
  813. }
  814. int libertas_remove_card(wlan_private *priv)
  815. {
  816. wlan_adapter *adapter;
  817. struct net_device *dev;
  818. union iwreq_data wrqu;
  819. lbs_deb_enter(LBS_DEB_NET);
  820. if (!priv)
  821. goto out;
  822. adapter = priv->adapter;
  823. if (!adapter)
  824. goto out;
  825. dev = priv->dev;
  826. netif_stop_queue(priv->dev);
  827. netif_carrier_off(priv->dev);
  828. wake_pending_cmdnodes(priv);
  829. unregister_netdev(dev);
  830. cancel_delayed_work(&priv->assoc_work);
  831. destroy_workqueue(priv->assoc_thread);
  832. if (adapter->psmode == wlan802_11powermodemax_psp) {
  833. adapter->psmode = wlan802_11powermodecam;
  834. libertas_ps_wakeup(priv, cmd_option_waitforrsp);
  835. }
  836. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  837. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  838. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  839. adapter->surpriseremoved = 1;
  840. /* Stop the thread servicing the interrupts */
  841. wlan_terminate_thread(&priv->mainthread);
  842. libertas_debugfs_remove_one(priv);
  843. lbs_deb_net("free adapter\n");
  844. libertas_free_adapter(priv);
  845. lbs_deb_net("unregister finish\n");
  846. priv->dev = NULL;
  847. free_netdev(dev);
  848. out:
  849. lbs_deb_leave(LBS_DEB_NET);
  850. return 0;
  851. }
  852. EXPORT_SYMBOL_GPL(libertas_remove_card);
  853. void libertas_remove_mesh(wlan_private *priv)
  854. {
  855. struct net_device *mesh_dev;
  856. lbs_deb_enter(LBS_DEB_NET);
  857. if (!priv)
  858. goto out;
  859. mesh_dev = priv->mesh_dev;
  860. netif_stop_queue(mesh_dev);
  861. netif_carrier_off(priv->mesh_dev);
  862. device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
  863. unregister_netdev(mesh_dev);
  864. priv->mesh_dev = NULL ;
  865. free_netdev(mesh_dev);
  866. out:
  867. lbs_deb_leave(LBS_DEB_NET);
  868. }
  869. EXPORT_SYMBOL_GPL(libertas_remove_mesh);
  870. /**
  871. * @brief This function finds the CFP in
  872. * region_cfp_table based on region and band parameter.
  873. *
  874. * @param region The region code
  875. * @param band The band
  876. * @param cfp_no A pointer to CFP number
  877. * @return A pointer to CFP
  878. */
  879. struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  880. {
  881. int i, end;
  882. lbs_deb_enter(LBS_DEB_MAIN);
  883. end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
  884. for (i = 0; i < end ; i++) {
  885. lbs_deb_main("region_cfp_table[i].region=%d\n",
  886. region_cfp_table[i].region);
  887. if (region_cfp_table[i].region == region) {
  888. *cfp_no = region_cfp_table[i].cfp_no_BG;
  889. lbs_deb_leave(LBS_DEB_MAIN);
  890. return region_cfp_table[i].cfp_BG;
  891. }
  892. }
  893. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  894. return NULL;
  895. }
  896. int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
  897. {
  898. wlan_adapter *adapter = priv->adapter;
  899. int ret = 0;
  900. int i = 0;
  901. struct chan_freq_power *cfp;
  902. int cfp_no;
  903. lbs_deb_enter(LBS_DEB_MAIN);
  904. memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
  905. {
  906. cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
  907. if (cfp != NULL) {
  908. adapter->region_channel[i].nrcfp = cfp_no;
  909. adapter->region_channel[i].CFP = cfp;
  910. } else {
  911. lbs_deb_main("wrong region code %#x in band B/G\n",
  912. region);
  913. ret = -1;
  914. goto out;
  915. }
  916. adapter->region_channel[i].valid = 1;
  917. adapter->region_channel[i].region = region;
  918. adapter->region_channel[i].band = band;
  919. i++;
  920. }
  921. out:
  922. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  923. return ret;
  924. }
  925. /**
  926. * @brief This function handles the interrupt. it will change PS
  927. * state if applicable. it will wake up main_thread to handle
  928. * the interrupt event as well.
  929. *
  930. * @param dev A pointer to net_device structure
  931. * @return n/a
  932. */
  933. void libertas_interrupt(struct net_device *dev)
  934. {
  935. wlan_private *priv = dev->priv;
  936. lbs_deb_enter(LBS_DEB_THREAD);
  937. lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
  938. priv->adapter->intcounter);
  939. priv->adapter->intcounter++;
  940. if (priv->adapter->psstate == PS_STATE_SLEEP) {
  941. priv->adapter->psstate = PS_STATE_AWAKE;
  942. netif_wake_queue(dev);
  943. netif_wake_queue(priv->mesh_dev);
  944. }
  945. wake_up_interruptible(&priv->mainthread.waitq);
  946. lbs_deb_leave(LBS_DEB_THREAD);
  947. }
  948. EXPORT_SYMBOL_GPL(libertas_interrupt);
  949. static int libertas_init_module(void)
  950. {
  951. lbs_deb_enter(LBS_DEB_MAIN);
  952. libertas_debugfs_init();
  953. lbs_deb_leave(LBS_DEB_MAIN);
  954. return 0;
  955. }
  956. static void libertas_exit_module(void)
  957. {
  958. lbs_deb_enter(LBS_DEB_MAIN);
  959. libertas_debugfs_remove();
  960. lbs_deb_leave(LBS_DEB_MAIN);
  961. }
  962. module_init(libertas_init_module);
  963. module_exit(libertas_exit_module);
  964. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  965. MODULE_AUTHOR("Marvell International Ltd.");
  966. MODULE_LICENSE("GPL");