main.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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 table to keep region code
  138. */
  139. u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  140. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  141. /**
  142. * 802.11b/g supported bitrates (in 500Kb/s units)
  143. */
  144. u8 libertas_bg_rates[MAX_RATES] =
  145. { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  146. 0x00, 0x00 };
  147. /**
  148. * FW rate table. FW refers to rates by their index in this table, not by the
  149. * rate value itself. Values of 0x00 are
  150. * reserved positions.
  151. */
  152. static u8 fw_data_rates[MAX_RATES] =
  153. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  154. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  155. };
  156. /**
  157. * @brief use index to get the data rate
  158. *
  159. * @param idx The index of data rate
  160. * @return data rate or 0
  161. */
  162. u32 libertas_fw_index_to_data_rate(u8 idx)
  163. {
  164. if (idx >= sizeof(fw_data_rates))
  165. idx = 0;
  166. return fw_data_rates[idx];
  167. }
  168. /**
  169. * @brief use rate to get the index
  170. *
  171. * @param rate data rate
  172. * @return index or 0
  173. */
  174. u8 libertas_data_rate_to_fw_index(u32 rate)
  175. {
  176. u8 i;
  177. if (!rate)
  178. return 0;
  179. for (i = 0; i < sizeof(fw_data_rates); i++) {
  180. if (rate == fw_data_rates[i])
  181. return i;
  182. }
  183. return 0;
  184. }
  185. /**
  186. * Attributes exported through sysfs
  187. */
  188. /**
  189. * @brief Get function for sysfs attribute anycast_mask
  190. */
  191. static ssize_t libertas_anycast_get(struct device * dev,
  192. struct device_attribute *attr, char * buf)
  193. {
  194. struct cmd_ds_mesh_access mesh_access;
  195. memset(&mesh_access, 0, sizeof(mesh_access));
  196. libertas_prepare_and_send_command(to_net_dev(dev)->priv,
  197. CMD_MESH_ACCESS,
  198. CMD_ACT_MESH_GET_ANYCAST,
  199. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  200. return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
  201. }
  202. /**
  203. * @brief Set function for sysfs attribute anycast_mask
  204. */
  205. static ssize_t libertas_anycast_set(struct device * dev,
  206. struct device_attribute *attr, const char * buf, size_t count)
  207. {
  208. struct cmd_ds_mesh_access mesh_access;
  209. uint32_t datum;
  210. memset(&mesh_access, 0, sizeof(mesh_access));
  211. sscanf(buf, "%x", &datum);
  212. mesh_access.data[0] = cpu_to_le32(datum);
  213. libertas_prepare_and_send_command((to_net_dev(dev))->priv,
  214. CMD_MESH_ACCESS,
  215. CMD_ACT_MESH_SET_ANYCAST,
  216. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  217. return strlen(buf);
  218. }
  219. /**
  220. * anycast_mask attribute to be exported per mshX interface
  221. * through sysfs (/sys/class/net/mshX/anycast_mask)
  222. */
  223. static DEVICE_ATTR(anycast_mask, 0644, libertas_anycast_get, libertas_anycast_set);
  224. static ssize_t libertas_autostart_enabled_get(struct device * dev,
  225. struct device_attribute *attr, char * buf)
  226. {
  227. struct cmd_ds_mesh_access mesh_access;
  228. memset(&mesh_access, 0, sizeof(mesh_access));
  229. libertas_prepare_and_send_command(to_net_dev(dev)->priv,
  230. CMD_MESH_ACCESS,
  231. CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
  232. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  233. return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
  234. }
  235. static ssize_t libertas_autostart_enabled_set(struct device * dev,
  236. struct device_attribute *attr, const char * buf, size_t count)
  237. {
  238. struct cmd_ds_mesh_access mesh_access;
  239. uint32_t datum;
  240. memset(&mesh_access, 0, sizeof(mesh_access));
  241. sscanf(buf, "%d", &datum);
  242. mesh_access.data[0] = cpu_to_le32(datum);
  243. libertas_prepare_and_send_command((to_net_dev(dev))->priv,
  244. CMD_MESH_ACCESS,
  245. CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
  246. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  247. return strlen(buf);
  248. }
  249. static DEVICE_ATTR(autostart_enabled, 0644,
  250. libertas_autostart_enabled_get, libertas_autostart_enabled_set);
  251. static struct attribute *libertas_mesh_sysfs_entries[] = {
  252. &dev_attr_anycast_mask.attr,
  253. &dev_attr_autostart_enabled.attr,
  254. NULL,
  255. };
  256. static struct attribute_group libertas_mesh_attr_group = {
  257. .attrs = libertas_mesh_sysfs_entries,
  258. };
  259. /**
  260. * @brief Check if the device can be open and wait if necessary.
  261. *
  262. * @param dev A pointer to net_device structure
  263. * @return 0
  264. *
  265. * For USB adapter, on some systems the device open handler will be
  266. * called before FW ready. Use the following flag check and wait
  267. * function to work around the issue.
  268. *
  269. */
  270. static int pre_open_check(struct net_device *dev)
  271. {
  272. wlan_private *priv = (wlan_private *) dev->priv;
  273. wlan_adapter *adapter = priv->adapter;
  274. int i = 0;
  275. while (!adapter->fw_ready && i < 20) {
  276. i++;
  277. msleep_interruptible(100);
  278. }
  279. if (!adapter->fw_ready) {
  280. lbs_pr_err("firmware not ready\n");
  281. return -1;
  282. }
  283. return 0;
  284. }
  285. /**
  286. * @brief This function opens the device
  287. *
  288. * @param dev A pointer to net_device structure
  289. * @return 0
  290. */
  291. static int libertas_dev_open(struct net_device *dev)
  292. {
  293. wlan_private *priv = (wlan_private *) dev->priv;
  294. wlan_adapter *adapter = priv->adapter;
  295. lbs_deb_enter(LBS_DEB_NET);
  296. priv->open = 1;
  297. if (adapter->connect_status == LIBERTAS_CONNECTED) {
  298. netif_carrier_on(priv->dev);
  299. if (priv->mesh_dev)
  300. netif_carrier_on(priv->mesh_dev);
  301. } else {
  302. netif_carrier_off(priv->dev);
  303. if (priv->mesh_dev)
  304. netif_carrier_off(priv->mesh_dev);
  305. }
  306. lbs_deb_leave(LBS_DEB_NET);
  307. return 0;
  308. }
  309. /**
  310. * @brief This function opens the mshX interface
  311. *
  312. * @param dev A pointer to net_device structure
  313. * @return 0
  314. */
  315. static int libertas_mesh_open(struct net_device *dev)
  316. {
  317. wlan_private *priv = (wlan_private *) dev->priv ;
  318. if (pre_open_check(dev) == -1)
  319. return -1;
  320. priv->mesh_open = 1 ;
  321. netif_wake_queue(priv->mesh_dev);
  322. if (priv->infra_open == 0)
  323. return libertas_dev_open(priv->dev) ;
  324. return 0;
  325. }
  326. /**
  327. * @brief This function opens the ethX interface
  328. *
  329. * @param dev A pointer to net_device structure
  330. * @return 0
  331. */
  332. static int libertas_open(struct net_device *dev)
  333. {
  334. wlan_private *priv = (wlan_private *) dev->priv ;
  335. if(pre_open_check(dev) == -1)
  336. return -1;
  337. priv->infra_open = 1 ;
  338. netif_wake_queue(priv->dev);
  339. if (priv->open == 0)
  340. return libertas_dev_open(priv->dev) ;
  341. return 0;
  342. }
  343. static int libertas_dev_close(struct net_device *dev)
  344. {
  345. wlan_private *priv = dev->priv;
  346. lbs_deb_enter(LBS_DEB_NET);
  347. netif_carrier_off(priv->dev);
  348. priv->open = 0;
  349. lbs_deb_leave(LBS_DEB_NET);
  350. return 0;
  351. }
  352. /**
  353. * @brief This function closes the mshX interface
  354. *
  355. * @param dev A pointer to net_device structure
  356. * @return 0
  357. */
  358. static int libertas_mesh_close(struct net_device *dev)
  359. {
  360. wlan_private *priv = (wlan_private *) (dev->priv);
  361. priv->mesh_open = 0;
  362. netif_stop_queue(priv->mesh_dev);
  363. if (priv->infra_open == 0)
  364. return libertas_dev_close(dev);
  365. else
  366. return 0;
  367. }
  368. /**
  369. * @brief This function closes the ethX interface
  370. *
  371. * @param dev A pointer to net_device structure
  372. * @return 0
  373. */
  374. static int libertas_close(struct net_device *dev)
  375. {
  376. wlan_private *priv = (wlan_private *) dev->priv;
  377. netif_stop_queue(dev);
  378. priv->infra_open = 0;
  379. if (priv->mesh_open == 0)
  380. return libertas_dev_close(dev);
  381. else
  382. return 0;
  383. }
  384. static int libertas_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  385. {
  386. int ret = 0;
  387. wlan_private *priv = dev->priv;
  388. lbs_deb_enter(LBS_DEB_NET);
  389. if (priv->dnld_sent || priv->adapter->TxLockFlag) {
  390. priv->stats.tx_dropped++;
  391. goto done;
  392. }
  393. netif_stop_queue(priv->dev);
  394. if (priv->mesh_dev)
  395. netif_stop_queue(priv->mesh_dev);
  396. if (libertas_process_tx(priv, skb) == 0)
  397. dev->trans_start = jiffies;
  398. done:
  399. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  400. return ret;
  401. }
  402. /**
  403. * @brief Mark mesh packets and handover them to libertas_hard_start_xmit
  404. *
  405. */
  406. static int libertas_mesh_pre_start_xmit(struct sk_buff *skb,
  407. struct net_device *dev)
  408. {
  409. wlan_private *priv = dev->priv;
  410. int ret;
  411. lbs_deb_enter(LBS_DEB_MESH);
  412. SET_MESH_FRAME(skb);
  413. ret = libertas_hard_start_xmit(skb, priv->dev);
  414. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  415. return ret;
  416. }
  417. /**
  418. * @brief Mark non-mesh packets and handover them to libertas_hard_start_xmit
  419. *
  420. */
  421. static int libertas_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
  422. {
  423. int ret;
  424. lbs_deb_enter(LBS_DEB_NET);
  425. UNSET_MESH_FRAME(skb);
  426. ret = libertas_hard_start_xmit(skb, dev);
  427. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  428. return ret;
  429. }
  430. static void libertas_tx_timeout(struct net_device *dev)
  431. {
  432. wlan_private *priv = (wlan_private *) dev->priv;
  433. lbs_deb_enter(LBS_DEB_TX);
  434. lbs_pr_err("tx watch dog timeout\n");
  435. priv->dnld_sent = DNLD_RES_RECEIVED;
  436. dev->trans_start = jiffies;
  437. if (priv->adapter->currenttxskb) {
  438. if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
  439. /* If we are here, we have not received feedback from
  440. the previous packet. Assume TX_FAIL and move on. */
  441. priv->adapter->eventcause = 0x01000000;
  442. libertas_send_tx_feedback(priv);
  443. } else
  444. wake_up_interruptible(&priv->waitq);
  445. } else if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
  446. netif_wake_queue(priv->dev);
  447. if (priv->mesh_dev)
  448. netif_wake_queue(priv->mesh_dev);
  449. }
  450. lbs_deb_leave(LBS_DEB_TX);
  451. }
  452. /**
  453. * @brief This function returns the network statistics
  454. *
  455. * @param dev A pointer to wlan_private structure
  456. * @return A pointer to net_device_stats structure
  457. */
  458. static struct net_device_stats *libertas_get_stats(struct net_device *dev)
  459. {
  460. wlan_private *priv = (wlan_private *) dev->priv;
  461. return &priv->stats;
  462. }
  463. static int libertas_set_mac_address(struct net_device *dev, void *addr)
  464. {
  465. int ret = 0;
  466. wlan_private *priv = (wlan_private *) dev->priv;
  467. wlan_adapter *adapter = priv->adapter;
  468. struct sockaddr *phwaddr = addr;
  469. lbs_deb_enter(LBS_DEB_NET);
  470. /* In case it was called from the mesh device */
  471. dev = priv->dev ;
  472. memset(adapter->current_addr, 0, ETH_ALEN);
  473. /* dev->dev_addr is 8 bytes */
  474. lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
  475. lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
  476. memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
  477. ret = libertas_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
  478. CMD_ACT_SET,
  479. CMD_OPTION_WAITFORRSP, 0, NULL);
  480. if (ret) {
  481. lbs_deb_net("set MAC address failed\n");
  482. ret = -1;
  483. goto done;
  484. }
  485. lbs_deb_hex(LBS_DEB_NET, "adapter->macaddr", adapter->current_addr, ETH_ALEN);
  486. memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
  487. if (priv->mesh_dev)
  488. memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
  489. done:
  490. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  491. return ret;
  492. }
  493. static int libertas_copy_multicast_address(wlan_adapter * adapter,
  494. struct net_device *dev)
  495. {
  496. int i = 0;
  497. struct dev_mc_list *mcptr = dev->mc_list;
  498. for (i = 0; i < dev->mc_count; i++) {
  499. memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  500. mcptr = mcptr->next;
  501. }
  502. return i;
  503. }
  504. static void libertas_set_multicast_list(struct net_device *dev)
  505. {
  506. wlan_private *priv = dev->priv;
  507. wlan_adapter *adapter = priv->adapter;
  508. int oldpacketfilter;
  509. lbs_deb_enter(LBS_DEB_NET);
  510. oldpacketfilter = adapter->currentpacketfilter;
  511. if (dev->flags & IFF_PROMISC) {
  512. lbs_deb_net("enable promiscuous mode\n");
  513. adapter->currentpacketfilter |=
  514. CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  515. adapter->currentpacketfilter &=
  516. ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  517. CMD_ACT_MAC_MULTICAST_ENABLE);
  518. } else {
  519. /* Multicast */
  520. adapter->currentpacketfilter &=
  521. ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  522. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  523. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  524. lbs_deb_net( "enabling all multicast\n");
  525. adapter->currentpacketfilter |=
  526. CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  527. adapter->currentpacketfilter &=
  528. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  529. } else {
  530. adapter->currentpacketfilter &=
  531. ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  532. if (!dev->mc_count) {
  533. lbs_deb_net("no multicast addresses, "
  534. "disabling multicast\n");
  535. adapter->currentpacketfilter &=
  536. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  537. } else {
  538. int i;
  539. adapter->currentpacketfilter |=
  540. CMD_ACT_MAC_MULTICAST_ENABLE;
  541. adapter->nr_of_multicastmacaddr =
  542. libertas_copy_multicast_address(adapter, dev);
  543. lbs_deb_net("multicast addresses: %d\n",
  544. dev->mc_count);
  545. for (i = 0; i < dev->mc_count; i++) {
  546. lbs_deb_net("Multicast address %d:"
  547. MAC_FMT "\n", i,
  548. adapter->multicastlist[i][0],
  549. adapter->multicastlist[i][1],
  550. adapter->multicastlist[i][2],
  551. adapter->multicastlist[i][3],
  552. adapter->multicastlist[i][4],
  553. adapter->multicastlist[i][5]);
  554. }
  555. /* send multicast addresses to firmware */
  556. libertas_prepare_and_send_command(priv,
  557. CMD_MAC_MULTICAST_ADR,
  558. CMD_ACT_SET, 0, 0,
  559. NULL);
  560. }
  561. }
  562. }
  563. if (adapter->currentpacketfilter != oldpacketfilter) {
  564. libertas_set_mac_packet_filter(priv);
  565. }
  566. lbs_deb_leave(LBS_DEB_NET);
  567. }
  568. /**
  569. * @brief This function handles the major jobs in the WLAN driver.
  570. * It handles all events generated by firmware, RX data received
  571. * from firmware and TX data sent from kernel.
  572. *
  573. * @param data A pointer to wlan_thread structure
  574. * @return 0
  575. */
  576. static int libertas_thread(void *data)
  577. {
  578. struct net_device *dev = data;
  579. wlan_private *priv = dev->priv;
  580. wlan_adapter *adapter = priv->adapter;
  581. wait_queue_t wait;
  582. u8 ireg = 0;
  583. lbs_deb_enter(LBS_DEB_THREAD);
  584. init_waitqueue_entry(&wait, current);
  585. for (;;) {
  586. lbs_deb_thread( "main-thread 111: intcounter=%d "
  587. "currenttxskb=%p dnld_sent=%d\n",
  588. adapter->intcounter,
  589. adapter->currenttxskb, priv->dnld_sent);
  590. add_wait_queue(&priv->waitq, &wait);
  591. set_current_state(TASK_INTERRUPTIBLE);
  592. spin_lock_irq(&adapter->driver_lock);
  593. if ((adapter->psstate == PS_STATE_SLEEP) ||
  594. (!adapter->intcounter
  595. && (priv->dnld_sent || adapter->cur_cmd ||
  596. list_empty(&adapter->cmdpendingq)))) {
  597. lbs_deb_thread(
  598. "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  599. adapter->connect_status, adapter->intcounter,
  600. adapter->psmode, adapter->psstate);
  601. spin_unlock_irq(&adapter->driver_lock);
  602. schedule();
  603. } else
  604. spin_unlock_irq(&adapter->driver_lock);
  605. lbs_deb_thread(
  606. "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
  607. "dnld_sent=%d\n", adapter->intcounter,
  608. adapter->currenttxskb, priv->dnld_sent);
  609. set_current_state(TASK_RUNNING);
  610. remove_wait_queue(&priv->waitq, &wait);
  611. try_to_freeze();
  612. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
  613. "dnld_sent=%d\n",
  614. adapter->intcounter,
  615. adapter->currenttxskb, priv->dnld_sent);
  616. if (kthread_should_stop()
  617. || adapter->surpriseremoved) {
  618. lbs_deb_thread(
  619. "main-thread: break from main thread: surpriseremoved=0x%x\n",
  620. adapter->surpriseremoved);
  621. break;
  622. }
  623. spin_lock_irq(&adapter->driver_lock);
  624. if (adapter->intcounter) {
  625. u8 int_status;
  626. adapter->intcounter = 0;
  627. int_status = priv->hw_get_int_status(priv, &ireg);
  628. if (int_status) {
  629. lbs_deb_thread(
  630. "main-thread: reading HOST_INT_STATUS_REG failed\n");
  631. spin_unlock_irq(&adapter->driver_lock);
  632. continue;
  633. }
  634. adapter->hisregcpy |= ireg;
  635. }
  636. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
  637. "dnld_sent=%d\n",
  638. adapter->intcounter,
  639. adapter->currenttxskb, priv->dnld_sent);
  640. /* command response? */
  641. if (adapter->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
  642. lbs_deb_thread("main-thread: cmd response ready\n");
  643. adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
  644. spin_unlock_irq(&adapter->driver_lock);
  645. libertas_process_rx_command(priv);
  646. spin_lock_irq(&adapter->driver_lock);
  647. }
  648. /* Any Card Event */
  649. if (adapter->hisregcpy & MRVDRV_CARDEVENT) {
  650. lbs_deb_thread("main-thread: Card Event Activity\n");
  651. adapter->hisregcpy &= ~MRVDRV_CARDEVENT;
  652. if (priv->hw_read_event_cause(priv)) {
  653. lbs_pr_alert(
  654. "main-thread: hw_read_event_cause failed\n");
  655. spin_unlock_irq(&adapter->driver_lock);
  656. continue;
  657. }
  658. spin_unlock_irq(&adapter->driver_lock);
  659. libertas_process_event(priv);
  660. } else
  661. spin_unlock_irq(&adapter->driver_lock);
  662. /* Check if we need to confirm Sleep Request received previously */
  663. if (adapter->psstate == PS_STATE_PRE_SLEEP) {
  664. if (!priv->dnld_sent && !adapter->cur_cmd) {
  665. if (adapter->connect_status ==
  666. LIBERTAS_CONNECTED) {
  667. lbs_deb_thread(
  668. "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
  669. "dnld_sent=%d cur_cmd=%p, confirm now\n",
  670. adapter->intcounter,
  671. adapter->currenttxskb,
  672. priv->dnld_sent,
  673. adapter->cur_cmd);
  674. libertas_ps_confirm_sleep(priv,
  675. (u16) adapter->psmode);
  676. } else {
  677. /* workaround for firmware sending
  678. * deauth/linkloss event immediately
  679. * after sleep request, remove this
  680. * after firmware fixes it
  681. */
  682. adapter->psstate = PS_STATE_AWAKE;
  683. lbs_pr_alert(
  684. "main-thread: ignore PS_SleepConfirm in non-connected state\n");
  685. }
  686. }
  687. }
  688. /* The PS state is changed during processing of Sleep Request
  689. * event above
  690. */
  691. if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
  692. (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
  693. continue;
  694. /* Execute the next command */
  695. if (!priv->dnld_sent && !priv->adapter->cur_cmd)
  696. libertas_execute_next_command(priv);
  697. /* Wake-up command waiters which can't sleep in
  698. * libertas_prepare_and_send_command
  699. */
  700. if (!adapter->nr_cmd_pending)
  701. wake_up_all(&adapter->cmd_pending);
  702. libertas_tx_runqueue(priv);
  703. }
  704. del_timer(&adapter->command_timer);
  705. adapter->nr_cmd_pending = 0;
  706. wake_up_all(&adapter->cmd_pending);
  707. lbs_deb_leave(LBS_DEB_THREAD);
  708. return 0;
  709. }
  710. /**
  711. * @brief This function downloads firmware image, gets
  712. * HW spec from firmware and set basic parameters to
  713. * firmware.
  714. *
  715. * @param priv A pointer to wlan_private structure
  716. * @return 0 or -1
  717. */
  718. static int wlan_setup_station_hw(wlan_private * priv)
  719. {
  720. int ret = -1;
  721. wlan_adapter *adapter = priv->adapter;
  722. lbs_deb_enter(LBS_DEB_FW);
  723. ret = priv->hw_prog_firmware(priv);
  724. if (ret) {
  725. lbs_deb_fw("bootloader in invalid state\n");
  726. ret = -1;
  727. goto done;
  728. }
  729. /*
  730. * Read MAC address from HW
  731. */
  732. memset(adapter->current_addr, 0xff, ETH_ALEN);
  733. ret = libertas_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
  734. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  735. if (ret) {
  736. ret = -1;
  737. goto done;
  738. }
  739. libertas_set_mac_packet_filter(priv);
  740. /* Get the supported Data rates */
  741. ret = libertas_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
  742. CMD_ACT_GET_TX_RATE,
  743. CMD_OPTION_WAITFORRSP, 0, NULL);
  744. if (ret) {
  745. ret = -1;
  746. goto done;
  747. }
  748. ret = 0;
  749. done:
  750. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  751. return ret;
  752. }
  753. static void command_timer_fn(unsigned long data);
  754. /**
  755. * This function handles the timeout of command sending.
  756. * It will re-send the same command again.
  757. */
  758. static void command_timer_fn(unsigned long data)
  759. {
  760. wlan_private *priv = (wlan_private *)data;
  761. wlan_adapter *adapter = priv->adapter;
  762. struct cmd_ctrl_node *ptempnode;
  763. struct cmd_ds_command *cmd;
  764. unsigned long flags;
  765. ptempnode = adapter->cur_cmd;
  766. if (ptempnode == NULL) {
  767. lbs_deb_fw("ptempnode empty\n");
  768. return;
  769. }
  770. cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
  771. if (!cmd) {
  772. lbs_deb_fw("cmd is NULL\n");
  773. return;
  774. }
  775. lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
  776. if (!adapter->fw_ready)
  777. return;
  778. spin_lock_irqsave(&adapter->driver_lock, flags);
  779. adapter->cur_cmd = NULL;
  780. spin_unlock_irqrestore(&adapter->driver_lock, flags);
  781. lbs_deb_fw("re-sending same command because of timeout\n");
  782. libertas_queue_cmd(adapter, ptempnode, 0);
  783. wake_up_interruptible(&priv->waitq);
  784. return;
  785. }
  786. static void libertas_free_adapter(wlan_private * priv)
  787. {
  788. wlan_adapter *adapter = priv->adapter;
  789. if (!adapter) {
  790. lbs_deb_fw("why double free adapter?\n");
  791. return;
  792. }
  793. lbs_deb_fw("free command buffer\n");
  794. libertas_free_cmd_buffer(priv);
  795. lbs_deb_fw("free command_timer\n");
  796. del_timer(&adapter->command_timer);
  797. lbs_deb_fw("free scan results table\n");
  798. kfree(adapter->networks);
  799. adapter->networks = NULL;
  800. /* Free the adapter object itself */
  801. lbs_deb_fw("free adapter\n");
  802. kfree(adapter);
  803. priv->adapter = NULL;
  804. }
  805. static int wlan_allocate_adapter(wlan_private * priv)
  806. {
  807. size_t bufsize;
  808. wlan_adapter *adapter = priv->adapter;
  809. /* Allocate buffer to store the BSSID list */
  810. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  811. adapter->networks = kzalloc(bufsize, GFP_KERNEL);
  812. if (!adapter->networks) {
  813. lbs_pr_err("Out of memory allocating beacons\n");
  814. libertas_free_adapter(priv);
  815. return -ENOMEM;
  816. }
  817. /* Allocate the command buffers */
  818. libertas_allocate_cmd_buffer(priv);
  819. memset(&adapter->libertas_ps_confirm_sleep, 0, sizeof(struct PS_CMD_ConfirmSleep));
  820. adapter->libertas_ps_confirm_sleep.seqnum = cpu_to_le16(++adapter->seqnum);
  821. adapter->libertas_ps_confirm_sleep.command =
  822. cpu_to_le16(CMD_802_11_PS_MODE);
  823. adapter->libertas_ps_confirm_sleep.size =
  824. cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
  825. adapter->libertas_ps_confirm_sleep.result = 0;
  826. adapter->libertas_ps_confirm_sleep.action =
  827. cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  828. return 0;
  829. }
  830. static void wlan_init_adapter(wlan_private * priv)
  831. {
  832. wlan_adapter *adapter = priv->adapter;
  833. int i;
  834. adapter->connect_status = LIBERTAS_DISCONNECTED;
  835. memset(adapter->current_addr, 0xff, ETH_ALEN);
  836. /* scan type */
  837. adapter->scantype = CMD_SCAN_TYPE_ACTIVE;
  838. /* scan mode */
  839. adapter->scanmode = CMD_BSS_TYPE_ANY;
  840. /* 802.11 specific */
  841. adapter->secinfo.wep_enabled = 0;
  842. for (i = 0; i < sizeof(adapter->wep_keys) / sizeof(adapter->wep_keys[0]);
  843. i++)
  844. memset(&adapter->wep_keys[i], 0, sizeof(struct enc_key));
  845. adapter->wep_tx_keyidx = 0;
  846. adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  847. adapter->mode = IW_MODE_INFRA;
  848. adapter->pending_assoc_req = NULL;
  849. adapter->in_progress_assoc_req = NULL;
  850. /* Initialize scan result lists */
  851. INIT_LIST_HEAD(&adapter->network_free_list);
  852. INIT_LIST_HEAD(&adapter->network_list);
  853. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  854. list_add_tail(&adapter->networks[i].list,
  855. &adapter->network_free_list);
  856. }
  857. mutex_init(&adapter->lock);
  858. memset(&adapter->curbssparams, 0, sizeof(adapter->curbssparams));
  859. adapter->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  860. /* PnP and power profile */
  861. adapter->surpriseremoved = 0;
  862. adapter->currentpacketfilter =
  863. CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  864. adapter->radioon = RADIO_ON;
  865. adapter->auto_rate = 1;
  866. adapter->cur_rate = 0;
  867. // set default capabilities
  868. adapter->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  869. adapter->psmode = WLAN802_11POWERMODECAM;
  870. adapter->psstate = PS_STATE_FULL_POWER;
  871. adapter->needtowakeup = 0;
  872. adapter->intcounter = 0;
  873. adapter->currenttxskb = NULL;
  874. memset(&adapter->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
  875. adapter->tx_queue_idx = 0;
  876. spin_lock_init(&adapter->txqueue_lock);
  877. return;
  878. }
  879. static int libertas_init_fw(wlan_private * priv)
  880. {
  881. int ret = -1;
  882. wlan_adapter *adapter = priv->adapter;
  883. lbs_deb_enter(LBS_DEB_FW);
  884. /* Allocate adapter structure */
  885. if ((ret = wlan_allocate_adapter(priv)) != 0)
  886. goto done;
  887. /* init adapter structure */
  888. wlan_init_adapter(priv);
  889. /* init timer etc. */
  890. setup_timer(&adapter->command_timer, command_timer_fn,
  891. (unsigned long)priv);
  892. /* download fimrware etc. */
  893. if ((ret = wlan_setup_station_hw(priv)) != 0) {
  894. del_timer_sync(&adapter->command_timer);
  895. goto done;
  896. }
  897. /* init 802.11d */
  898. libertas_init_11d(priv);
  899. ret = 0;
  900. done:
  901. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  902. return ret;
  903. }
  904. /**
  905. * @brief This function adds the card. it will probe the
  906. * card, allocate the wlan_priv and initialize the device.
  907. *
  908. * @param card A pointer to card
  909. * @return A pointer to wlan_private structure
  910. */
  911. wlan_private *libertas_add_card(void *card, struct device *dmdev)
  912. {
  913. struct net_device *dev = NULL;
  914. wlan_private *priv = NULL;
  915. lbs_deb_enter(LBS_DEB_NET);
  916. /* Allocate an Ethernet device and register it */
  917. if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
  918. lbs_pr_err("init ethX device failed\n");
  919. return NULL;
  920. }
  921. priv = dev->priv;
  922. /* allocate buffer for wlan_adapter */
  923. if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
  924. lbs_pr_err("allocate buffer for wlan_adapter failed\n");
  925. goto err_kzalloc;
  926. }
  927. priv->dev = dev;
  928. priv->card = card;
  929. priv->mesh_open = 0;
  930. priv->infra_open = 0;
  931. SET_MODULE_OWNER(dev);
  932. /* Setup the OS Interface to our functions */
  933. dev->open = libertas_open;
  934. dev->hard_start_xmit = libertas_pre_start_xmit;
  935. dev->stop = libertas_close;
  936. dev->set_mac_address = libertas_set_mac_address;
  937. dev->tx_timeout = libertas_tx_timeout;
  938. dev->get_stats = libertas_get_stats;
  939. dev->watchdog_timeo = 5 * HZ;
  940. dev->ethtool_ops = &libertas_ethtool_ops;
  941. #ifdef WIRELESS_EXT
  942. dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  943. #endif
  944. #define NETIF_F_DYNALLOC 16
  945. dev->features |= NETIF_F_DYNALLOC;
  946. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  947. dev->set_multicast_list = libertas_set_multicast_list;
  948. SET_NETDEV_DEV(dev, dmdev);
  949. INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
  950. INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
  951. spin_lock_init(&priv->adapter->driver_lock);
  952. init_waitqueue_head(&priv->adapter->cmd_pending);
  953. priv->adapter->nr_cmd_pending = 0;
  954. goto done;
  955. err_kzalloc:
  956. free_netdev(dev);
  957. priv = NULL;
  958. done:
  959. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  960. return priv;
  961. }
  962. EXPORT_SYMBOL_GPL(libertas_add_card);
  963. int libertas_activate_card(wlan_private *priv)
  964. {
  965. struct net_device *dev = priv->dev;
  966. int ret = -1;
  967. lbs_deb_enter(LBS_DEB_MAIN);
  968. lbs_deb_thread("Starting main thread...\n");
  969. init_waitqueue_head(&priv->waitq);
  970. priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
  971. if (IS_ERR(priv->main_thread)) {
  972. lbs_deb_thread("Error creating main thread.\n");
  973. goto done;
  974. }
  975. priv->assoc_thread =
  976. create_singlethread_workqueue("libertas_assoc");
  977. INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
  978. INIT_WORK(&priv->sync_channel, libertas_sync_channel);
  979. /*
  980. * Register the device. Fillup the private data structure with
  981. * relevant information from the card and request for the required
  982. * IRQ.
  983. */
  984. if (priv->hw_register_dev(priv) < 0) {
  985. lbs_pr_err("failed to register WLAN device\n");
  986. goto err_registerdev;
  987. }
  988. /* init FW and HW */
  989. if (libertas_init_fw(priv)) {
  990. lbs_pr_err("firmware init failed\n");
  991. goto err_registerdev;
  992. }
  993. if (register_netdev(dev)) {
  994. lbs_pr_err("cannot register ethX device\n");
  995. goto err_init_fw;
  996. }
  997. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  998. libertas_debugfs_init_one(priv, dev);
  999. ret = 0;
  1000. goto done;
  1001. err_init_fw:
  1002. priv->hw_unregister_dev(priv);
  1003. err_registerdev:
  1004. destroy_workqueue(priv->assoc_thread);
  1005. /* Stop the thread servicing the interrupts */
  1006. wake_up_interruptible(&priv->waitq);
  1007. kthread_stop(priv->main_thread);
  1008. done:
  1009. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  1010. return ret;
  1011. }
  1012. EXPORT_SYMBOL_GPL(libertas_activate_card);
  1013. /**
  1014. * @brief This function adds mshX interface
  1015. *
  1016. * @param priv A pointer to the wlan_private structure
  1017. * @return 0 if successful, -X otherwise
  1018. */
  1019. int libertas_add_mesh(wlan_private *priv, struct device *dev)
  1020. {
  1021. struct net_device *mesh_dev = NULL;
  1022. int ret = 0;
  1023. lbs_deb_enter(LBS_DEB_MESH);
  1024. /* Allocate a virtual mesh device */
  1025. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1026. lbs_deb_mesh("init mshX device failed\n");
  1027. ret = -ENOMEM;
  1028. goto done;
  1029. }
  1030. mesh_dev->priv = priv;
  1031. priv->mesh_dev = mesh_dev;
  1032. SET_MODULE_OWNER(mesh_dev);
  1033. mesh_dev->open = libertas_mesh_open;
  1034. mesh_dev->hard_start_xmit = libertas_mesh_pre_start_xmit;
  1035. mesh_dev->stop = libertas_mesh_close;
  1036. mesh_dev->get_stats = libertas_get_stats;
  1037. mesh_dev->set_mac_address = libertas_set_mac_address;
  1038. mesh_dev->ethtool_ops = &libertas_ethtool_ops;
  1039. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1040. sizeof(priv->dev->dev_addr));
  1041. SET_NETDEV_DEV(priv->mesh_dev, dev);
  1042. #ifdef WIRELESS_EXT
  1043. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1044. #endif
  1045. #define NETIF_F_DYNALLOC 16
  1046. /* Register virtual mesh interface */
  1047. ret = register_netdev(mesh_dev);
  1048. if (ret) {
  1049. lbs_pr_err("cannot register mshX virtual interface\n");
  1050. goto err_free;
  1051. }
  1052. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
  1053. if (ret)
  1054. goto err_unregister;
  1055. /* Everything successful */
  1056. ret = 0;
  1057. goto done;
  1058. err_unregister:
  1059. unregister_netdev(mesh_dev);
  1060. err_free:
  1061. free_netdev(mesh_dev);
  1062. done:
  1063. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1064. return ret;
  1065. }
  1066. EXPORT_SYMBOL_GPL(libertas_add_mesh);
  1067. static void wake_pending_cmdnodes(wlan_private *priv)
  1068. {
  1069. struct cmd_ctrl_node *cmdnode;
  1070. unsigned long flags;
  1071. lbs_deb_enter(LBS_DEB_HOST);
  1072. spin_lock_irqsave(&priv->adapter->driver_lock, flags);
  1073. list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
  1074. cmdnode->cmdwaitqwoken = 1;
  1075. wake_up_interruptible(&cmdnode->cmdwait_q);
  1076. }
  1077. spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
  1078. }
  1079. int libertas_remove_card(wlan_private *priv)
  1080. {
  1081. wlan_adapter *adapter;
  1082. struct net_device *dev;
  1083. union iwreq_data wrqu;
  1084. lbs_deb_enter(LBS_DEB_NET);
  1085. if (!priv)
  1086. goto out;
  1087. adapter = priv->adapter;
  1088. if (!adapter)
  1089. goto out;
  1090. dev = priv->dev;
  1091. netif_stop_queue(priv->dev);
  1092. netif_carrier_off(priv->dev);
  1093. wake_pending_cmdnodes(priv);
  1094. unregister_netdev(dev);
  1095. cancel_delayed_work(&priv->assoc_work);
  1096. destroy_workqueue(priv->assoc_thread);
  1097. if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
  1098. adapter->psmode = WLAN802_11POWERMODECAM;
  1099. libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  1100. }
  1101. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  1102. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1103. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1104. adapter->surpriseremoved = 1;
  1105. /* Stop the thread servicing the interrupts */
  1106. kthread_stop(priv->main_thread);
  1107. libertas_debugfs_remove_one(priv);
  1108. lbs_deb_net("free adapter\n");
  1109. libertas_free_adapter(priv);
  1110. lbs_deb_net("unregister finish\n");
  1111. priv->dev = NULL;
  1112. free_netdev(dev);
  1113. out:
  1114. lbs_deb_leave(LBS_DEB_NET);
  1115. return 0;
  1116. }
  1117. EXPORT_SYMBOL_GPL(libertas_remove_card);
  1118. void libertas_remove_mesh(wlan_private *priv)
  1119. {
  1120. struct net_device *mesh_dev;
  1121. lbs_deb_enter(LBS_DEB_NET);
  1122. if (!priv)
  1123. goto out;
  1124. mesh_dev = priv->mesh_dev;
  1125. netif_stop_queue(mesh_dev);
  1126. netif_carrier_off(priv->mesh_dev);
  1127. sysfs_remove_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
  1128. unregister_netdev(mesh_dev);
  1129. priv->mesh_dev = NULL ;
  1130. free_netdev(mesh_dev);
  1131. out:
  1132. lbs_deb_leave(LBS_DEB_NET);
  1133. }
  1134. EXPORT_SYMBOL_GPL(libertas_remove_mesh);
  1135. /**
  1136. * @brief This function finds the CFP in
  1137. * region_cfp_table based on region and band parameter.
  1138. *
  1139. * @param region The region code
  1140. * @param band The band
  1141. * @param cfp_no A pointer to CFP number
  1142. * @return A pointer to CFP
  1143. */
  1144. struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  1145. {
  1146. int i, end;
  1147. lbs_deb_enter(LBS_DEB_MAIN);
  1148. end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
  1149. for (i = 0; i < end ; i++) {
  1150. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1151. region_cfp_table[i].region);
  1152. if (region_cfp_table[i].region == region) {
  1153. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1154. lbs_deb_leave(LBS_DEB_MAIN);
  1155. return region_cfp_table[i].cfp_BG;
  1156. }
  1157. }
  1158. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1159. return NULL;
  1160. }
  1161. int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
  1162. {
  1163. wlan_adapter *adapter = priv->adapter;
  1164. int ret = 0;
  1165. int i = 0;
  1166. struct chan_freq_power *cfp;
  1167. int cfp_no;
  1168. lbs_deb_enter(LBS_DEB_MAIN);
  1169. memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
  1170. {
  1171. cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
  1172. if (cfp != NULL) {
  1173. adapter->region_channel[i].nrcfp = cfp_no;
  1174. adapter->region_channel[i].CFP = cfp;
  1175. } else {
  1176. lbs_deb_main("wrong region code %#x in band B/G\n",
  1177. region);
  1178. ret = -1;
  1179. goto out;
  1180. }
  1181. adapter->region_channel[i].valid = 1;
  1182. adapter->region_channel[i].region = region;
  1183. adapter->region_channel[i].band = band;
  1184. i++;
  1185. }
  1186. out:
  1187. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1188. return ret;
  1189. }
  1190. /**
  1191. * @brief This function handles the interrupt. it will change PS
  1192. * state if applicable. it will wake up main_thread to handle
  1193. * the interrupt event as well.
  1194. *
  1195. * @param dev A pointer to net_device structure
  1196. * @return n/a
  1197. */
  1198. void libertas_interrupt(struct net_device *dev)
  1199. {
  1200. wlan_private *priv = dev->priv;
  1201. lbs_deb_enter(LBS_DEB_THREAD);
  1202. lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
  1203. priv->adapter->intcounter);
  1204. priv->adapter->intcounter++;
  1205. if (priv->adapter->psstate == PS_STATE_SLEEP) {
  1206. priv->adapter->psstate = PS_STATE_AWAKE;
  1207. netif_wake_queue(dev);
  1208. if (priv->mesh_dev)
  1209. netif_wake_queue(priv->mesh_dev);
  1210. }
  1211. wake_up_interruptible(&priv->waitq);
  1212. lbs_deb_leave(LBS_DEB_THREAD);
  1213. }
  1214. EXPORT_SYMBOL_GPL(libertas_interrupt);
  1215. int libertas_reset_device(wlan_private *priv)
  1216. {
  1217. int ret;
  1218. lbs_deb_enter(LBS_DEB_MAIN);
  1219. ret = libertas_prepare_and_send_command(priv, CMD_802_11_RESET,
  1220. CMD_ACT_HALT, 0, 0, NULL);
  1221. msleep_interruptible(10);
  1222. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1223. return ret;
  1224. }
  1225. EXPORT_SYMBOL_GPL(libertas_reset_device);
  1226. static int libertas_init_module(void)
  1227. {
  1228. lbs_deb_enter(LBS_DEB_MAIN);
  1229. libertas_debugfs_init();
  1230. lbs_deb_leave(LBS_DEB_MAIN);
  1231. return 0;
  1232. }
  1233. static void libertas_exit_module(void)
  1234. {
  1235. lbs_deb_enter(LBS_DEB_MAIN);
  1236. libertas_debugfs_remove();
  1237. lbs_deb_leave(LBS_DEB_MAIN);
  1238. }
  1239. module_init(libertas_init_module);
  1240. module_exit(libertas_exit_module);
  1241. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1242. MODULE_AUTHOR("Marvell International Ltd.");
  1243. MODULE_LICENSE("GPL");