main.c 29 KB

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