main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. kfree(priv->adapter);
  740. free_netdev(dev);
  741. done:
  742. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  743. return ret;
  744. }
  745. EXPORT_SYMBOL_GPL(libertas_activate_card);
  746. /**
  747. * @brief This function adds mshX interface
  748. *
  749. * @param priv A pointer to the wlan_private structure
  750. * @return 0 if successful, -X otherwise
  751. */
  752. int libertas_add_mesh(wlan_private *priv, struct device *dev)
  753. {
  754. struct net_device *mesh_dev = NULL;
  755. int ret = 0;
  756. lbs_deb_enter(LBS_DEB_MESH);
  757. /* Allocate a virtual mesh device */
  758. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  759. lbs_deb_mesh("init mshX device failed\n");
  760. ret = -ENOMEM;
  761. goto done;
  762. }
  763. mesh_dev->priv = priv;
  764. priv->mesh_dev = mesh_dev;
  765. SET_MODULE_OWNER(mesh_dev);
  766. mesh_dev->open = mesh_open;
  767. mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
  768. mesh_dev->stop = mesh_close;
  769. mesh_dev->do_ioctl = libertas_do_ioctl;
  770. mesh_dev->get_stats = wlan_get_stats;
  771. mesh_dev->ethtool_ops = &libertas_ethtool_ops;
  772. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  773. sizeof(priv->dev->dev_addr));
  774. SET_NETDEV_DEV(priv->mesh_dev, dev);
  775. #ifdef WIRELESS_EXT
  776. mesh_dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  777. #endif
  778. #define NETIF_F_DYNALLOC 16
  779. /* Register virtual mesh interface */
  780. ret = register_netdev(mesh_dev);
  781. if (ret) {
  782. lbs_pr_err("cannot register mshX virtual interface\n");
  783. goto err_free;
  784. }
  785. ret = device_create_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
  786. if (ret)
  787. goto err_unregister;
  788. /* Everything successful */
  789. ret = 0;
  790. goto done;
  791. err_unregister:
  792. unregister_netdev(mesh_dev);
  793. err_free:
  794. free_netdev(mesh_dev);
  795. done:
  796. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  797. return ret;
  798. }
  799. EXPORT_SYMBOL_GPL(libertas_add_mesh);
  800. static void wake_pending_cmdnodes(wlan_private *priv)
  801. {
  802. struct cmd_ctrl_node *cmdnode;
  803. unsigned long flags;
  804. lbs_deb_enter(LBS_DEB_CMD);
  805. spin_lock_irqsave(&priv->adapter->driver_lock, flags);
  806. list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
  807. cmdnode->cmdwaitqwoken = 1;
  808. wake_up_interruptible(&cmdnode->cmdwait_q);
  809. }
  810. spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
  811. }
  812. int libertas_remove_card(wlan_private *priv)
  813. {
  814. wlan_adapter *adapter;
  815. struct net_device *dev;
  816. union iwreq_data wrqu;
  817. lbs_deb_enter(LBS_DEB_NET);
  818. if (!priv)
  819. goto out;
  820. adapter = priv->adapter;
  821. if (!adapter)
  822. goto out;
  823. dev = priv->dev;
  824. netif_stop_queue(priv->dev);
  825. netif_carrier_off(priv->dev);
  826. wake_pending_cmdnodes(priv);
  827. unregister_netdev(dev);
  828. cancel_delayed_work(&priv->assoc_work);
  829. destroy_workqueue(priv->assoc_thread);
  830. if (adapter->psmode == wlan802_11powermodemax_psp) {
  831. adapter->psmode = wlan802_11powermodecam;
  832. libertas_ps_wakeup(priv, cmd_option_waitforrsp);
  833. }
  834. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  835. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  836. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  837. adapter->surpriseremoved = 1;
  838. /* Stop the thread servicing the interrupts */
  839. wlan_terminate_thread(&priv->mainthread);
  840. libertas_debugfs_remove_one(priv);
  841. lbs_deb_net("free adapter\n");
  842. libertas_free_adapter(priv);
  843. lbs_deb_net("unregister finish\n");
  844. priv->dev = NULL;
  845. free_netdev(dev);
  846. out:
  847. lbs_deb_leave(LBS_DEB_NET);
  848. return 0;
  849. }
  850. EXPORT_SYMBOL_GPL(libertas_remove_card);
  851. void libertas_remove_mesh(wlan_private *priv)
  852. {
  853. struct net_device *mesh_dev;
  854. lbs_deb_enter(LBS_DEB_NET);
  855. if (!priv)
  856. goto out;
  857. mesh_dev = priv->mesh_dev;
  858. netif_stop_queue(mesh_dev);
  859. netif_carrier_off(priv->mesh_dev);
  860. device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
  861. unregister_netdev(mesh_dev);
  862. priv->mesh_dev = NULL ;
  863. free_netdev(mesh_dev);
  864. out:
  865. lbs_deb_leave(LBS_DEB_NET);
  866. }
  867. EXPORT_SYMBOL_GPL(libertas_remove_mesh);
  868. /**
  869. * @brief This function finds the CFP in
  870. * region_cfp_table based on region and band parameter.
  871. *
  872. * @param region The region code
  873. * @param band The band
  874. * @param cfp_no A pointer to CFP number
  875. * @return A pointer to CFP
  876. */
  877. struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  878. {
  879. int i, end;
  880. lbs_deb_enter(LBS_DEB_MAIN);
  881. end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
  882. for (i = 0; i < end ; i++) {
  883. lbs_deb_main("region_cfp_table[i].region=%d\n",
  884. region_cfp_table[i].region);
  885. if (region_cfp_table[i].region == region) {
  886. *cfp_no = region_cfp_table[i].cfp_no_BG;
  887. lbs_deb_leave(LBS_DEB_MAIN);
  888. return region_cfp_table[i].cfp_BG;
  889. }
  890. }
  891. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  892. return NULL;
  893. }
  894. int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
  895. {
  896. wlan_adapter *adapter = priv->adapter;
  897. int ret = 0;
  898. int i = 0;
  899. struct chan_freq_power *cfp;
  900. int cfp_no;
  901. lbs_deb_enter(LBS_DEB_MAIN);
  902. memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
  903. {
  904. cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
  905. if (cfp != NULL) {
  906. adapter->region_channel[i].nrcfp = cfp_no;
  907. adapter->region_channel[i].CFP = cfp;
  908. } else {
  909. lbs_deb_main("wrong region code %#x in band B/G\n",
  910. region);
  911. ret = -1;
  912. goto out;
  913. }
  914. adapter->region_channel[i].valid = 1;
  915. adapter->region_channel[i].region = region;
  916. adapter->region_channel[i].band = band;
  917. i++;
  918. }
  919. out:
  920. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  921. return ret;
  922. }
  923. /**
  924. * @brief This function handles the interrupt. it will change PS
  925. * state if applicable. it will wake up main_thread to handle
  926. * the interrupt event as well.
  927. *
  928. * @param dev A pointer to net_device structure
  929. * @return n/a
  930. */
  931. void libertas_interrupt(struct net_device *dev)
  932. {
  933. wlan_private *priv = dev->priv;
  934. lbs_deb_enter(LBS_DEB_THREAD);
  935. lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
  936. priv->adapter->intcounter);
  937. priv->adapter->intcounter++;
  938. if (priv->adapter->psstate == PS_STATE_SLEEP) {
  939. priv->adapter->psstate = PS_STATE_AWAKE;
  940. netif_wake_queue(dev);
  941. netif_wake_queue(priv->mesh_dev);
  942. }
  943. wake_up_interruptible(&priv->mainthread.waitq);
  944. lbs_deb_leave(LBS_DEB_THREAD);
  945. }
  946. EXPORT_SYMBOL_GPL(libertas_interrupt);
  947. static int libertas_init_module(void)
  948. {
  949. lbs_deb_enter(LBS_DEB_MAIN);
  950. libertas_debugfs_init();
  951. lbs_deb_leave(LBS_DEB_MAIN);
  952. return 0;
  953. }
  954. static void libertas_exit_module(void)
  955. {
  956. lbs_deb_enter(LBS_DEB_MAIN);
  957. libertas_debugfs_remove();
  958. lbs_deb_leave(LBS_DEB_MAIN);
  959. }
  960. module_init(libertas_init_module);
  961. module_exit(libertas_exit_module);
  962. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  963. MODULE_AUTHOR("Marvell International Ltd.");
  964. MODULE_LICENSE("GPL");