main.c 38 KB

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