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