main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 libertas_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 libertas_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 libertas_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 libertas_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 libertas_dev_open(priv->dev) ;
  276. return 0;
  277. }
  278. static int libertas_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 libertas_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 libertas_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 libertas_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 libertas_dev_close(dev);
  316. else
  317. return 0;
  318. }
  319. static int libertas_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 libertas_hard_start_xmit
  338. *
  339. */
  340. static int libertas_mesh_pre_start_xmit(struct sk_buff *skb,
  341. struct net_device *dev)
  342. {
  343. wlan_private *priv = dev->priv;
  344. int ret;
  345. lbs_deb_enter(LBS_DEB_MESH);
  346. SET_MESH_FRAME(skb);
  347. ret = libertas_hard_start_xmit(skb, priv->dev);
  348. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  349. return ret;
  350. }
  351. /**
  352. * @brief Mark non-mesh packets and handover them to libertas_hard_start_xmit
  353. *
  354. */
  355. static int libertas_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
  356. {
  357. int ret;
  358. lbs_deb_enter(LBS_DEB_NET);
  359. UNSET_MESH_FRAME(skb);
  360. ret = libertas_hard_start_xmit(skb, dev);
  361. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  362. return ret;
  363. }
  364. static void libertas_tx_timeout(struct net_device *dev)
  365. {
  366. wlan_private *priv = (wlan_private *) dev->priv;
  367. lbs_deb_enter(LBS_DEB_TX);
  368. lbs_pr_err("tx watch dog timeout\n");
  369. priv->dnld_sent = DNLD_RES_RECEIVED;
  370. dev->trans_start = jiffies;
  371. if (priv->adapter->currenttxskb) {
  372. if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
  373. /* If we are here, we have not received feedback from
  374. the previous packet. Assume TX_FAIL and move on. */
  375. priv->adapter->eventcause = 0x01000000;
  376. libertas_send_tx_feedback(priv);
  377. } else
  378. wake_up_interruptible(&priv->waitq);
  379. } else if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
  380. netif_wake_queue(priv->dev);
  381. netif_wake_queue(priv->mesh_dev);
  382. }
  383. lbs_deb_leave(LBS_DEB_TX);
  384. }
  385. /**
  386. * @brief This function returns the network statistics
  387. *
  388. * @param dev A pointer to wlan_private structure
  389. * @return A pointer to net_device_stats structure
  390. */
  391. static struct net_device_stats *libertas_get_stats(struct net_device *dev)
  392. {
  393. wlan_private *priv = (wlan_private *) dev->priv;
  394. return &priv->stats;
  395. }
  396. static int libertas_set_mac_address(struct net_device *dev, void *addr)
  397. {
  398. int ret = 0;
  399. wlan_private *priv = (wlan_private *) dev->priv;
  400. wlan_adapter *adapter = priv->adapter;
  401. struct sockaddr *phwaddr = addr;
  402. lbs_deb_enter(LBS_DEB_NET);
  403. /* In case it was called from the mesh device */
  404. dev = priv->dev ;
  405. memset(adapter->current_addr, 0, ETH_ALEN);
  406. /* dev->dev_addr is 8 bytes */
  407. lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
  408. lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
  409. memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
  410. ret = libertas_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
  411. CMD_ACT_SET,
  412. CMD_OPTION_WAITFORRSP, 0, NULL);
  413. if (ret) {
  414. lbs_deb_net("set MAC address failed\n");
  415. ret = -1;
  416. goto done;
  417. }
  418. lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
  419. memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
  420. if (priv->mesh_dev)
  421. memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
  422. done:
  423. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  424. return ret;
  425. }
  426. static int libertas_copy_multicast_address(wlan_adapter * adapter,
  427. struct net_device *dev)
  428. {
  429. int i = 0;
  430. struct dev_mc_list *mcptr = dev->mc_list;
  431. for (i = 0; i < dev->mc_count; i++) {
  432. memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  433. mcptr = mcptr->next;
  434. }
  435. return i;
  436. }
  437. static void libertas_set_multicast_list(struct net_device *dev)
  438. {
  439. wlan_private *priv = dev->priv;
  440. wlan_adapter *adapter = priv->adapter;
  441. int oldpacketfilter;
  442. lbs_deb_enter(LBS_DEB_NET);
  443. oldpacketfilter = adapter->currentpacketfilter;
  444. if (dev->flags & IFF_PROMISC) {
  445. lbs_deb_net("enable promiscuous mode\n");
  446. adapter->currentpacketfilter |=
  447. CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  448. adapter->currentpacketfilter &=
  449. ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  450. CMD_ACT_MAC_MULTICAST_ENABLE);
  451. } else {
  452. /* Multicast */
  453. adapter->currentpacketfilter &=
  454. ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  455. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  456. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  457. lbs_deb_net( "enabling all multicast\n");
  458. adapter->currentpacketfilter |=
  459. CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  460. adapter->currentpacketfilter &=
  461. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  462. } else {
  463. adapter->currentpacketfilter &=
  464. ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  465. if (!dev->mc_count) {
  466. lbs_deb_net("no multicast addresses, "
  467. "disabling multicast\n");
  468. adapter->currentpacketfilter &=
  469. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  470. } else {
  471. int i;
  472. adapter->currentpacketfilter |=
  473. CMD_ACT_MAC_MULTICAST_ENABLE;
  474. adapter->nr_of_multicastmacaddr =
  475. libertas_copy_multicast_address(adapter, dev);
  476. lbs_deb_net("multicast addresses: %d\n",
  477. dev->mc_count);
  478. for (i = 0; i < dev->mc_count; i++) {
  479. lbs_deb_net("Multicast address %d:"
  480. MAC_FMT "\n", i,
  481. adapter->multicastlist[i][0],
  482. adapter->multicastlist[i][1],
  483. adapter->multicastlist[i][2],
  484. adapter->multicastlist[i][3],
  485. adapter->multicastlist[i][4],
  486. adapter->multicastlist[i][5]);
  487. }
  488. /* send multicast addresses to firmware */
  489. libertas_prepare_and_send_command(priv,
  490. CMD_MAC_MULTICAST_ADR,
  491. CMD_ACT_SET, 0, 0,
  492. NULL);
  493. }
  494. }
  495. }
  496. if (adapter->currentpacketfilter != oldpacketfilter) {
  497. libertas_set_mac_packet_filter(priv);
  498. }
  499. lbs_deb_leave(LBS_DEB_NET);
  500. }
  501. /**
  502. * @brief This function handles the major jobs in the WLAN driver.
  503. * It handles all events generated by firmware, RX data received
  504. * from firmware and TX data sent from kernel.
  505. *
  506. * @param data A pointer to wlan_thread structure
  507. * @return 0
  508. */
  509. static int libertas_thread(void *data)
  510. {
  511. struct net_device *dev = data;
  512. wlan_private *priv = dev->priv;
  513. wlan_adapter *adapter = priv->adapter;
  514. wait_queue_t wait;
  515. u8 ireg = 0;
  516. lbs_deb_enter(LBS_DEB_THREAD);
  517. init_waitqueue_entry(&wait, current);
  518. for (;;) {
  519. lbs_deb_thread( "main-thread 111: intcounter=%d "
  520. "currenttxskb=%p dnld_sent=%d\n",
  521. adapter->intcounter,
  522. adapter->currenttxskb, priv->dnld_sent);
  523. add_wait_queue(&priv->waitq, &wait);
  524. set_current_state(TASK_INTERRUPTIBLE);
  525. spin_lock_irq(&adapter->driver_lock);
  526. if ((adapter->psstate == PS_STATE_SLEEP) ||
  527. (!adapter->intcounter
  528. && (priv->dnld_sent || adapter->cur_cmd ||
  529. list_empty(&adapter->cmdpendingq)))) {
  530. lbs_deb_thread(
  531. "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  532. adapter->connect_status, adapter->intcounter,
  533. adapter->psmode, adapter->psstate);
  534. spin_unlock_irq(&adapter->driver_lock);
  535. schedule();
  536. } else
  537. spin_unlock_irq(&adapter->driver_lock);
  538. lbs_deb_thread(
  539. "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
  540. "dnld_sent=%d\n", adapter->intcounter,
  541. adapter->currenttxskb, priv->dnld_sent);
  542. set_current_state(TASK_RUNNING);
  543. remove_wait_queue(&priv->waitq, &wait);
  544. try_to_freeze();
  545. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
  546. "dnld_sent=%d\n",
  547. adapter->intcounter,
  548. adapter->currenttxskb, priv->dnld_sent);
  549. if (kthread_should_stop()
  550. || adapter->surpriseremoved) {
  551. lbs_deb_thread(
  552. "main-thread: break from main thread: surpriseremoved=0x%x\n",
  553. adapter->surpriseremoved);
  554. break;
  555. }
  556. spin_lock_irq(&adapter->driver_lock);
  557. if (adapter->intcounter) {
  558. u8 int_status;
  559. adapter->intcounter = 0;
  560. int_status = priv->hw_get_int_status(priv, &ireg);
  561. if (int_status) {
  562. lbs_deb_thread(
  563. "main-thread: reading HOST_INT_STATUS_REG failed\n");
  564. spin_unlock_irq(&adapter->driver_lock);
  565. continue;
  566. }
  567. adapter->hisregcpy |= ireg;
  568. }
  569. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
  570. "dnld_sent=%d\n",
  571. adapter->intcounter,
  572. adapter->currenttxskb, priv->dnld_sent);
  573. /* command response? */
  574. if (adapter->hisregcpy & his_cmdupldrdy) {
  575. lbs_deb_thread("main-thread: cmd response ready\n");
  576. adapter->hisregcpy &= ~his_cmdupldrdy;
  577. spin_unlock_irq(&adapter->driver_lock);
  578. libertas_process_rx_command(priv);
  579. spin_lock_irq(&adapter->driver_lock);
  580. }
  581. /* Any Card Event */
  582. if (adapter->hisregcpy & his_cardevent) {
  583. lbs_deb_thread("main-thread: Card Event Activity\n");
  584. adapter->hisregcpy &= ~his_cardevent;
  585. if (priv->hw_read_event_cause(priv)) {
  586. lbs_pr_alert(
  587. "main-thread: hw_read_event_cause failed\n");
  588. spin_unlock_irq(&adapter->driver_lock);
  589. continue;
  590. }
  591. spin_unlock_irq(&adapter->driver_lock);
  592. libertas_process_event(priv);
  593. } else
  594. spin_unlock_irq(&adapter->driver_lock);
  595. /* Check if we need to confirm Sleep Request received previously */
  596. if (adapter->psstate == PS_STATE_PRE_SLEEP) {
  597. if (!priv->dnld_sent && !adapter->cur_cmd) {
  598. if (adapter->connect_status ==
  599. LIBERTAS_CONNECTED) {
  600. lbs_deb_thread(
  601. "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
  602. "dnld_sent=%d cur_cmd=%p, confirm now\n",
  603. adapter->intcounter,
  604. adapter->currenttxskb,
  605. priv->dnld_sent,
  606. adapter->cur_cmd);
  607. libertas_ps_confirm_sleep(priv,
  608. (u16) adapter->psmode);
  609. } else {
  610. /* workaround for firmware sending
  611. * deauth/linkloss event immediately
  612. * after sleep request, remove this
  613. * after firmware fixes it
  614. */
  615. adapter->psstate = PS_STATE_AWAKE;
  616. lbs_pr_alert(
  617. "main-thread: ignore PS_SleepConfirm in non-connected state\n");
  618. }
  619. }
  620. }
  621. /* The PS state is changed during processing of Sleep Request
  622. * event above
  623. */
  624. if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
  625. (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
  626. continue;
  627. /* Execute the next command */
  628. if (!priv->dnld_sent && !priv->adapter->cur_cmd)
  629. libertas_execute_next_command(priv);
  630. /* Wake-up command waiters which can't sleep in
  631. * libertas_prepare_and_send_command
  632. */
  633. if (!adapter->nr_cmd_pending)
  634. wake_up_all(&adapter->cmd_pending);
  635. libertas_tx_runqueue(priv);
  636. }
  637. del_timer(&adapter->command_timer);
  638. adapter->nr_cmd_pending = 0;
  639. wake_up_all(&adapter->cmd_pending);
  640. lbs_deb_leave(LBS_DEB_THREAD);
  641. return 0;
  642. }
  643. /**
  644. * @brief This function adds the card. it will probe the
  645. * card, allocate the wlan_priv and initialize the device.
  646. *
  647. * @param card A pointer to card
  648. * @return A pointer to wlan_private structure
  649. */
  650. wlan_private *libertas_add_card(void *card, struct device *dmdev)
  651. {
  652. struct net_device *dev = NULL;
  653. wlan_private *priv = NULL;
  654. lbs_deb_enter(LBS_DEB_NET);
  655. /* Allocate an Ethernet device and register it */
  656. if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
  657. lbs_pr_err("init ethX device failed\n");
  658. return NULL;
  659. }
  660. priv = dev->priv;
  661. /* allocate buffer for wlan_adapter */
  662. if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
  663. lbs_pr_err("allocate buffer for wlan_adapter failed\n");
  664. goto err_kzalloc;
  665. }
  666. priv->dev = dev;
  667. priv->card = card;
  668. priv->mesh_open = 0;
  669. priv->infra_open = 0;
  670. SET_MODULE_OWNER(dev);
  671. /* Setup the OS Interface to our functions */
  672. dev->open = libertas_open;
  673. dev->hard_start_xmit = libertas_pre_start_xmit;
  674. dev->stop = libertas_close;
  675. dev->set_mac_address = libertas_set_mac_address;
  676. dev->tx_timeout = libertas_tx_timeout;
  677. dev->get_stats = libertas_get_stats;
  678. dev->watchdog_timeo = 5 * HZ;
  679. dev->ethtool_ops = &libertas_ethtool_ops;
  680. #ifdef WIRELESS_EXT
  681. dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  682. #endif
  683. #define NETIF_F_DYNALLOC 16
  684. dev->features |= NETIF_F_DYNALLOC;
  685. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  686. dev->set_multicast_list = libertas_set_multicast_list;
  687. SET_NETDEV_DEV(dev, dmdev);
  688. INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
  689. INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
  690. spin_lock_init(&priv->adapter->driver_lock);
  691. init_waitqueue_head(&priv->adapter->cmd_pending);
  692. priv->adapter->nr_cmd_pending = 0;
  693. goto done;
  694. err_kzalloc:
  695. free_netdev(dev);
  696. priv = NULL;
  697. done:
  698. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  699. return priv;
  700. }
  701. EXPORT_SYMBOL_GPL(libertas_add_card);
  702. int libertas_activate_card(wlan_private *priv, char *fw_name)
  703. {
  704. struct net_device *dev = priv->dev;
  705. int ret = -1;
  706. lbs_deb_enter(LBS_DEB_MAIN);
  707. lbs_deb_thread("Starting main thread...\n");
  708. init_waitqueue_head(&priv->waitq);
  709. priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
  710. if (IS_ERR(priv->main_thread)) {
  711. lbs_deb_thread("Error creating main thread.\n");
  712. goto done;
  713. }
  714. priv->assoc_thread =
  715. create_singlethread_workqueue("libertas_assoc");
  716. INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
  717. INIT_WORK(&priv->sync_channel, libertas_sync_channel);
  718. /*
  719. * Register the device. Fillup the private data structure with
  720. * relevant information from the card and request for the required
  721. * IRQ.
  722. */
  723. if (priv->hw_register_dev(priv) < 0) {
  724. lbs_pr_err("failed to register WLAN device\n");
  725. goto err_registerdev;
  726. }
  727. /* init FW and HW */
  728. if (fw_name && libertas_init_fw(priv, fw_name)) {
  729. lbs_pr_err("firmware init failed\n");
  730. goto err_registerdev;
  731. }
  732. if (register_netdev(dev)) {
  733. lbs_pr_err("cannot register ethX device\n");
  734. goto err_init_fw;
  735. }
  736. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  737. libertas_debugfs_init_one(priv, dev);
  738. ret = 0;
  739. goto done;
  740. err_init_fw:
  741. priv->hw_unregister_dev(priv);
  742. err_registerdev:
  743. destroy_workqueue(priv->assoc_thread);
  744. /* Stop the thread servicing the interrupts */
  745. wake_up_interruptible(&priv->waitq);
  746. kthread_stop(priv->main_thread);
  747. done:
  748. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  749. return ret;
  750. }
  751. EXPORT_SYMBOL_GPL(libertas_activate_card);
  752. /**
  753. * @brief This function adds mshX interface
  754. *
  755. * @param priv A pointer to the wlan_private structure
  756. * @return 0 if successful, -X otherwise
  757. */
  758. int libertas_add_mesh(wlan_private *priv, struct device *dev)
  759. {
  760. struct net_device *mesh_dev = NULL;
  761. int ret = 0;
  762. lbs_deb_enter(LBS_DEB_MESH);
  763. /* Allocate a virtual mesh device */
  764. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  765. lbs_deb_mesh("init mshX device failed\n");
  766. ret = -ENOMEM;
  767. goto done;
  768. }
  769. mesh_dev->priv = priv;
  770. priv->mesh_dev = mesh_dev;
  771. SET_MODULE_OWNER(mesh_dev);
  772. mesh_dev->open = libertas_mesh_open;
  773. mesh_dev->hard_start_xmit = libertas_mesh_pre_start_xmit;
  774. mesh_dev->stop = libertas_mesh_close;
  775. mesh_dev->get_stats = libertas_get_stats;
  776. mesh_dev->set_mac_address = libertas_set_mac_address;
  777. mesh_dev->ethtool_ops = &libertas_ethtool_ops;
  778. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  779. sizeof(priv->dev->dev_addr));
  780. SET_NETDEV_DEV(priv->mesh_dev, dev);
  781. #ifdef WIRELESS_EXT
  782. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  783. #endif
  784. #define NETIF_F_DYNALLOC 16
  785. /* Register virtual mesh interface */
  786. ret = register_netdev(mesh_dev);
  787. if (ret) {
  788. lbs_pr_err("cannot register mshX virtual interface\n");
  789. goto err_free;
  790. }
  791. ret = device_create_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
  792. if (ret)
  793. goto err_unregister;
  794. /* Everything successful */
  795. ret = 0;
  796. goto done;
  797. err_unregister:
  798. unregister_netdev(mesh_dev);
  799. err_free:
  800. free_netdev(mesh_dev);
  801. done:
  802. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  803. return ret;
  804. }
  805. EXPORT_SYMBOL_GPL(libertas_add_mesh);
  806. static void wake_pending_cmdnodes(wlan_private *priv)
  807. {
  808. struct cmd_ctrl_node *cmdnode;
  809. unsigned long flags;
  810. lbs_deb_enter(LBS_DEB_CMD);
  811. spin_lock_irqsave(&priv->adapter->driver_lock, flags);
  812. list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
  813. cmdnode->cmdwaitqwoken = 1;
  814. wake_up_interruptible(&cmdnode->cmdwait_q);
  815. }
  816. spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
  817. }
  818. int libertas_remove_card(wlan_private *priv)
  819. {
  820. wlan_adapter *adapter;
  821. struct net_device *dev;
  822. union iwreq_data wrqu;
  823. lbs_deb_enter(LBS_DEB_NET);
  824. if (!priv)
  825. goto out;
  826. adapter = priv->adapter;
  827. if (!adapter)
  828. goto out;
  829. dev = priv->dev;
  830. netif_stop_queue(priv->dev);
  831. netif_carrier_off(priv->dev);
  832. wake_pending_cmdnodes(priv);
  833. unregister_netdev(dev);
  834. cancel_delayed_work(&priv->assoc_work);
  835. destroy_workqueue(priv->assoc_thread);
  836. if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
  837. adapter->psmode = WLAN802_11POWERMODECAM;
  838. libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  839. }
  840. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  841. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  842. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  843. adapter->surpriseremoved = 1;
  844. /* Stop the thread servicing the interrupts */
  845. kthread_stop(priv->main_thread);
  846. libertas_debugfs_remove_one(priv);
  847. lbs_deb_net("free adapter\n");
  848. libertas_free_adapter(priv);
  849. lbs_deb_net("unregister finish\n");
  850. priv->dev = NULL;
  851. free_netdev(dev);
  852. out:
  853. lbs_deb_leave(LBS_DEB_NET);
  854. return 0;
  855. }
  856. EXPORT_SYMBOL_GPL(libertas_remove_card);
  857. void libertas_remove_mesh(wlan_private *priv)
  858. {
  859. struct net_device *mesh_dev;
  860. lbs_deb_enter(LBS_DEB_NET);
  861. if (!priv)
  862. goto out;
  863. mesh_dev = priv->mesh_dev;
  864. netif_stop_queue(mesh_dev);
  865. netif_carrier_off(priv->mesh_dev);
  866. device_remove_file(&(mesh_dev->dev), &dev_attr_anycast_mask);
  867. unregister_netdev(mesh_dev);
  868. priv->mesh_dev = NULL ;
  869. free_netdev(mesh_dev);
  870. out:
  871. lbs_deb_leave(LBS_DEB_NET);
  872. }
  873. EXPORT_SYMBOL_GPL(libertas_remove_mesh);
  874. /**
  875. * @brief This function finds the CFP in
  876. * region_cfp_table based on region and band parameter.
  877. *
  878. * @param region The region code
  879. * @param band The band
  880. * @param cfp_no A pointer to CFP number
  881. * @return A pointer to CFP
  882. */
  883. struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  884. {
  885. int i, end;
  886. lbs_deb_enter(LBS_DEB_MAIN);
  887. end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
  888. for (i = 0; i < end ; i++) {
  889. lbs_deb_main("region_cfp_table[i].region=%d\n",
  890. region_cfp_table[i].region);
  891. if (region_cfp_table[i].region == region) {
  892. *cfp_no = region_cfp_table[i].cfp_no_BG;
  893. lbs_deb_leave(LBS_DEB_MAIN);
  894. return region_cfp_table[i].cfp_BG;
  895. }
  896. }
  897. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  898. return NULL;
  899. }
  900. int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
  901. {
  902. wlan_adapter *adapter = priv->adapter;
  903. int ret = 0;
  904. int i = 0;
  905. struct chan_freq_power *cfp;
  906. int cfp_no;
  907. lbs_deb_enter(LBS_DEB_MAIN);
  908. memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
  909. {
  910. cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
  911. if (cfp != NULL) {
  912. adapter->region_channel[i].nrcfp = cfp_no;
  913. adapter->region_channel[i].CFP = cfp;
  914. } else {
  915. lbs_deb_main("wrong region code %#x in band B/G\n",
  916. region);
  917. ret = -1;
  918. goto out;
  919. }
  920. adapter->region_channel[i].valid = 1;
  921. adapter->region_channel[i].region = region;
  922. adapter->region_channel[i].band = band;
  923. i++;
  924. }
  925. out:
  926. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  927. return ret;
  928. }
  929. /**
  930. * @brief This function handles the interrupt. it will change PS
  931. * state if applicable. it will wake up main_thread to handle
  932. * the interrupt event as well.
  933. *
  934. * @param dev A pointer to net_device structure
  935. * @return n/a
  936. */
  937. void libertas_interrupt(struct net_device *dev)
  938. {
  939. wlan_private *priv = dev->priv;
  940. lbs_deb_enter(LBS_DEB_THREAD);
  941. lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
  942. priv->adapter->intcounter);
  943. priv->adapter->intcounter++;
  944. if (priv->adapter->psstate == PS_STATE_SLEEP) {
  945. priv->adapter->psstate = PS_STATE_AWAKE;
  946. netif_wake_queue(dev);
  947. netif_wake_queue(priv->mesh_dev);
  948. }
  949. wake_up_interruptible(&priv->waitq);
  950. lbs_deb_leave(LBS_DEB_THREAD);
  951. }
  952. EXPORT_SYMBOL_GPL(libertas_interrupt);
  953. int libertas_reset_device(wlan_private *priv)
  954. {
  955. int ret;
  956. lbs_deb_enter(LBS_DEB_MAIN);
  957. ret = libertas_prepare_and_send_command(priv, CMD_802_11_RESET,
  958. CMD_ACT_HALT, 0, 0, NULL);
  959. msleep_interruptible(10);
  960. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  961. return ret;
  962. }
  963. EXPORT_SYMBOL_GPL(libertas_reset_device);
  964. static int libertas_init_module(void)
  965. {
  966. lbs_deb_enter(LBS_DEB_MAIN);
  967. libertas_debugfs_init();
  968. lbs_deb_leave(LBS_DEB_MAIN);
  969. return 0;
  970. }
  971. static void libertas_exit_module(void)
  972. {
  973. lbs_deb_enter(LBS_DEB_MAIN);
  974. libertas_debugfs_remove();
  975. lbs_deb_leave(LBS_DEB_MAIN);
  976. }
  977. module_init(libertas_init_module);
  978. module_exit(libertas_exit_module);
  979. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  980. MODULE_AUTHOR("Marvell International Ltd.");
  981. MODULE_LICENSE("GPL");