main.c 39 KB

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