main.c 29 KB

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