main.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  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/etherdevice.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/kthread.h>
  12. #include <linux/kfifo.h>
  13. #include <linux/stddef.h>
  14. #include <linux/ieee80211.h>
  15. #include <net/iw_handler.h>
  16. #include "host.h"
  17. #include "decl.h"
  18. #include "dev.h"
  19. #include "wext.h"
  20. #include "debugfs.h"
  21. #include "scan.h"
  22. #include "assoc.h"
  23. #include "cmd.h"
  24. #define DRIVER_RELEASE_VERSION "323.p0"
  25. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  26. #ifdef DEBUG
  27. "-dbg"
  28. #endif
  29. "";
  30. /* Module parameters */
  31. unsigned int lbs_debug;
  32. EXPORT_SYMBOL_GPL(lbs_debug);
  33. module_param_named(libertas_debug, lbs_debug, int, 0644);
  34. /* This global structure is used to send the confirm_sleep command as
  35. * fast as possible down to the firmware. */
  36. struct cmd_confirm_sleep confirm_sleep;
  37. #define LBS_TX_PWR_DEFAULT 20 /*100mW */
  38. #define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
  39. #define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
  40. #define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
  41. #define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
  42. /* Format { channel, frequency (MHz), maxtxpower } */
  43. /* band: 'B/G', region: USA FCC/Canada IC */
  44. static struct chan_freq_power channel_freq_power_US_BG[] = {
  45. {1, 2412, LBS_TX_PWR_US_DEFAULT},
  46. {2, 2417, LBS_TX_PWR_US_DEFAULT},
  47. {3, 2422, LBS_TX_PWR_US_DEFAULT},
  48. {4, 2427, LBS_TX_PWR_US_DEFAULT},
  49. {5, 2432, LBS_TX_PWR_US_DEFAULT},
  50. {6, 2437, LBS_TX_PWR_US_DEFAULT},
  51. {7, 2442, LBS_TX_PWR_US_DEFAULT},
  52. {8, 2447, LBS_TX_PWR_US_DEFAULT},
  53. {9, 2452, LBS_TX_PWR_US_DEFAULT},
  54. {10, 2457, LBS_TX_PWR_US_DEFAULT},
  55. {11, 2462, LBS_TX_PWR_US_DEFAULT}
  56. };
  57. /* band: 'B/G', region: Europe ETSI */
  58. static struct chan_freq_power channel_freq_power_EU_BG[] = {
  59. {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
  60. {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
  61. {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
  62. {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
  63. {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
  64. {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
  65. {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
  66. {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
  67. {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
  68. {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
  69. {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
  70. {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
  71. {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
  72. };
  73. /* band: 'B/G', region: Spain */
  74. static struct chan_freq_power channel_freq_power_SPN_BG[] = {
  75. {10, 2457, LBS_TX_PWR_DEFAULT},
  76. {11, 2462, LBS_TX_PWR_DEFAULT}
  77. };
  78. /* band: 'B/G', region: France */
  79. static struct chan_freq_power channel_freq_power_FR_BG[] = {
  80. {10, 2457, LBS_TX_PWR_FR_DEFAULT},
  81. {11, 2462, LBS_TX_PWR_FR_DEFAULT},
  82. {12, 2467, LBS_TX_PWR_FR_DEFAULT},
  83. {13, 2472, LBS_TX_PWR_FR_DEFAULT}
  84. };
  85. /* band: 'B/G', region: Japan */
  86. static struct chan_freq_power channel_freq_power_JPN_BG[] = {
  87. {1, 2412, LBS_TX_PWR_JP_DEFAULT},
  88. {2, 2417, LBS_TX_PWR_JP_DEFAULT},
  89. {3, 2422, LBS_TX_PWR_JP_DEFAULT},
  90. {4, 2427, LBS_TX_PWR_JP_DEFAULT},
  91. {5, 2432, LBS_TX_PWR_JP_DEFAULT},
  92. {6, 2437, LBS_TX_PWR_JP_DEFAULT},
  93. {7, 2442, LBS_TX_PWR_JP_DEFAULT},
  94. {8, 2447, LBS_TX_PWR_JP_DEFAULT},
  95. {9, 2452, LBS_TX_PWR_JP_DEFAULT},
  96. {10, 2457, LBS_TX_PWR_JP_DEFAULT},
  97. {11, 2462, LBS_TX_PWR_JP_DEFAULT},
  98. {12, 2467, LBS_TX_PWR_JP_DEFAULT},
  99. {13, 2472, LBS_TX_PWR_JP_DEFAULT},
  100. {14, 2484, LBS_TX_PWR_JP_DEFAULT}
  101. };
  102. /**
  103. * the structure for channel, frequency and power
  104. */
  105. struct region_cfp_table {
  106. u8 region;
  107. struct chan_freq_power *cfp_BG;
  108. int cfp_no_BG;
  109. };
  110. /**
  111. * the structure for the mapping between region and CFP
  112. */
  113. static struct region_cfp_table region_cfp_table[] = {
  114. {0x10, /*US FCC */
  115. channel_freq_power_US_BG,
  116. ARRAY_SIZE(channel_freq_power_US_BG),
  117. }
  118. ,
  119. {0x20, /*CANADA IC */
  120. channel_freq_power_US_BG,
  121. ARRAY_SIZE(channel_freq_power_US_BG),
  122. }
  123. ,
  124. {0x30, /*EU*/ channel_freq_power_EU_BG,
  125. ARRAY_SIZE(channel_freq_power_EU_BG),
  126. }
  127. ,
  128. {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
  129. ARRAY_SIZE(channel_freq_power_SPN_BG),
  130. }
  131. ,
  132. {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
  133. ARRAY_SIZE(channel_freq_power_FR_BG),
  134. }
  135. ,
  136. {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
  137. ARRAY_SIZE(channel_freq_power_JPN_BG),
  138. }
  139. ,
  140. /*Add new region here */
  141. };
  142. /**
  143. * the table to keep region code
  144. */
  145. u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  146. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  147. /**
  148. * 802.11b/g supported bitrates (in 500Kb/s units)
  149. */
  150. u8 lbs_bg_rates[MAX_RATES] =
  151. { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  152. 0x00, 0x00 };
  153. /**
  154. * FW rate table. FW refers to rates by their index in this table, not by the
  155. * rate value itself. Values of 0x00 are
  156. * reserved positions.
  157. */
  158. static u8 fw_data_rates[MAX_RATES] =
  159. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  160. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  161. };
  162. /**
  163. * @brief use index to get the data rate
  164. *
  165. * @param idx The index of data rate
  166. * @return data rate or 0
  167. */
  168. u32 lbs_fw_index_to_data_rate(u8 idx)
  169. {
  170. if (idx >= sizeof(fw_data_rates))
  171. idx = 0;
  172. return fw_data_rates[idx];
  173. }
  174. /**
  175. * @brief use rate to get the index
  176. *
  177. * @param rate data rate
  178. * @return index or 0
  179. */
  180. u8 lbs_data_rate_to_fw_index(u32 rate)
  181. {
  182. u8 i;
  183. if (!rate)
  184. return 0;
  185. for (i = 0; i < sizeof(fw_data_rates); i++) {
  186. if (rate == fw_data_rates[i])
  187. return i;
  188. }
  189. return 0;
  190. }
  191. /**
  192. * Attributes exported through sysfs
  193. */
  194. /**
  195. * @brief Get function for sysfs attribute anycast_mask
  196. */
  197. static ssize_t lbs_anycast_get(struct device *dev,
  198. struct device_attribute *attr, char * buf)
  199. {
  200. struct lbs_private *priv = netdev_priv(to_net_dev(dev));
  201. struct cmd_ds_mesh_access mesh_access;
  202. int ret;
  203. memset(&mesh_access, 0, sizeof(mesh_access));
  204. ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
  205. if (ret)
  206. return ret;
  207. return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
  208. }
  209. /**
  210. * @brief Set function for sysfs attribute anycast_mask
  211. */
  212. static ssize_t lbs_anycast_set(struct device *dev,
  213. struct device_attribute *attr, const char * buf, size_t count)
  214. {
  215. struct lbs_private *priv = netdev_priv(to_net_dev(dev));
  216. struct cmd_ds_mesh_access mesh_access;
  217. uint32_t datum;
  218. int ret;
  219. memset(&mesh_access, 0, sizeof(mesh_access));
  220. sscanf(buf, "%x", &datum);
  221. mesh_access.data[0] = cpu_to_le32(datum);
  222. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
  223. if (ret)
  224. return ret;
  225. return strlen(buf);
  226. }
  227. /**
  228. * @brief Get function for sysfs attribute prb_rsp_limit
  229. */
  230. static ssize_t lbs_prb_rsp_limit_get(struct device *dev,
  231. struct device_attribute *attr, char *buf)
  232. {
  233. struct lbs_private *priv = netdev_priv(to_net_dev(dev));
  234. struct cmd_ds_mesh_access mesh_access;
  235. int ret;
  236. u32 retry_limit;
  237. memset(&mesh_access, 0, sizeof(mesh_access));
  238. mesh_access.data[0] = cpu_to_le32(CMD_ACT_GET);
  239. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
  240. &mesh_access);
  241. if (ret)
  242. return ret;
  243. retry_limit = le32_to_cpu(mesh_access.data[1]);
  244. return snprintf(buf, 10, "%d\n", retry_limit);
  245. }
  246. /**
  247. * @brief Set function for sysfs attribute prb_rsp_limit
  248. */
  249. static ssize_t lbs_prb_rsp_limit_set(struct device *dev,
  250. struct device_attribute *attr, const char *buf, size_t count)
  251. {
  252. struct lbs_private *priv = netdev_priv(to_net_dev(dev));
  253. struct cmd_ds_mesh_access mesh_access;
  254. int ret;
  255. unsigned long retry_limit;
  256. memset(&mesh_access, 0, sizeof(mesh_access));
  257. mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
  258. if (!strict_strtoul(buf, 10, &retry_limit))
  259. return -ENOTSUPP;
  260. if (retry_limit > 15)
  261. return -ENOTSUPP;
  262. mesh_access.data[1] = cpu_to_le32(retry_limit);
  263. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
  264. &mesh_access);
  265. if (ret)
  266. return ret;
  267. return strlen(buf);
  268. }
  269. static int lbs_add_rtap(struct lbs_private *priv);
  270. static void lbs_remove_rtap(struct lbs_private *priv);
  271. static int lbs_add_mesh(struct lbs_private *priv);
  272. static void lbs_remove_mesh(struct lbs_private *priv);
  273. /**
  274. * Get function for sysfs attribute rtap
  275. */
  276. static ssize_t lbs_rtap_get(struct device *dev,
  277. struct device_attribute *attr, char * buf)
  278. {
  279. struct lbs_private *priv = netdev_priv(to_net_dev(dev));
  280. return snprintf(buf, 5, "0x%X\n", priv->monitormode);
  281. }
  282. /**
  283. * Set function for sysfs attribute rtap
  284. */
  285. static ssize_t lbs_rtap_set(struct device *dev,
  286. struct device_attribute *attr, const char * buf, size_t count)
  287. {
  288. int monitor_mode;
  289. struct lbs_private *priv = netdev_priv(to_net_dev(dev));
  290. sscanf(buf, "%x", &monitor_mode);
  291. if (monitor_mode) {
  292. if (priv->monitormode == monitor_mode)
  293. return strlen(buf);
  294. if (!priv->monitormode) {
  295. if (priv->infra_open || priv->mesh_open)
  296. return -EBUSY;
  297. if (priv->mode == IW_MODE_INFRA)
  298. lbs_cmd_80211_deauthenticate(priv,
  299. priv->curbssparams.bssid,
  300. WLAN_REASON_DEAUTH_LEAVING);
  301. else if (priv->mode == IW_MODE_ADHOC)
  302. lbs_adhoc_stop(priv);
  303. lbs_add_rtap(priv);
  304. }
  305. priv->monitormode = monitor_mode;
  306. } else {
  307. if (!priv->monitormode)
  308. return strlen(buf);
  309. priv->monitormode = 0;
  310. lbs_remove_rtap(priv);
  311. if (priv->currenttxskb) {
  312. dev_kfree_skb_any(priv->currenttxskb);
  313. priv->currenttxskb = NULL;
  314. }
  315. /* Wake queues, command thread, etc. */
  316. lbs_host_to_card_done(priv);
  317. }
  318. lbs_prepare_and_send_command(priv,
  319. CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
  320. CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
  321. return strlen(buf);
  322. }
  323. /**
  324. * lbs_rtap attribute to be exported per ethX interface
  325. * through sysfs (/sys/class/net/ethX/lbs_rtap)
  326. */
  327. static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
  328. /**
  329. * Get function for sysfs attribute mesh
  330. */
  331. static ssize_t lbs_mesh_get(struct device *dev,
  332. struct device_attribute *attr, char * buf)
  333. {
  334. struct lbs_private *priv = netdev_priv(to_net_dev(dev));
  335. return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
  336. }
  337. /**
  338. * Set function for sysfs attribute mesh
  339. */
  340. static ssize_t lbs_mesh_set(struct device *dev,
  341. struct device_attribute *attr, const char * buf, size_t count)
  342. {
  343. struct lbs_private *priv = netdev_priv(to_net_dev(dev));
  344. int enable;
  345. int ret, action = CMD_ACT_MESH_CONFIG_STOP;
  346. sscanf(buf, "%x", &enable);
  347. enable = !!enable;
  348. if (enable == !!priv->mesh_dev)
  349. return count;
  350. if (enable)
  351. action = CMD_ACT_MESH_CONFIG_START;
  352. ret = lbs_mesh_config(priv, action, priv->curbssparams.channel);
  353. if (ret)
  354. return ret;
  355. if (enable)
  356. lbs_add_mesh(priv);
  357. else
  358. lbs_remove_mesh(priv);
  359. return count;
  360. }
  361. /**
  362. * lbs_mesh attribute to be exported per ethX interface
  363. * through sysfs (/sys/class/net/ethX/lbs_mesh)
  364. */
  365. static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
  366. /**
  367. * anycast_mask attribute to be exported per mshX interface
  368. * through sysfs (/sys/class/net/mshX/anycast_mask)
  369. */
  370. static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
  371. /**
  372. * prb_rsp_limit attribute to be exported per mshX interface
  373. * through sysfs (/sys/class/net/mshX/prb_rsp_limit)
  374. */
  375. static DEVICE_ATTR(prb_rsp_limit, 0644, lbs_prb_rsp_limit_get,
  376. lbs_prb_rsp_limit_set);
  377. static struct attribute *lbs_mesh_sysfs_entries[] = {
  378. &dev_attr_anycast_mask.attr,
  379. &dev_attr_prb_rsp_limit.attr,
  380. NULL,
  381. };
  382. static struct attribute_group lbs_mesh_attr_group = {
  383. .attrs = lbs_mesh_sysfs_entries,
  384. };
  385. /**
  386. * @brief This function opens the ethX or mshX interface
  387. *
  388. * @param dev A pointer to net_device structure
  389. * @return 0 or -EBUSY if monitor mode active
  390. */
  391. static int lbs_dev_open(struct net_device *dev)
  392. {
  393. struct lbs_private *priv = netdev_priv(dev) ;
  394. int ret = 0;
  395. lbs_deb_enter(LBS_DEB_NET);
  396. spin_lock_irq(&priv->driver_lock);
  397. if (priv->monitormode) {
  398. ret = -EBUSY;
  399. goto out;
  400. }
  401. if (dev == priv->mesh_dev) {
  402. priv->mesh_open = 1;
  403. priv->mesh_connect_status = LBS_CONNECTED;
  404. netif_carrier_on(dev);
  405. } else {
  406. priv->infra_open = 1;
  407. if (priv->connect_status == LBS_CONNECTED)
  408. netif_carrier_on(dev);
  409. else
  410. netif_carrier_off(dev);
  411. }
  412. if (!priv->tx_pending_len)
  413. netif_wake_queue(dev);
  414. out:
  415. spin_unlock_irq(&priv->driver_lock);
  416. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  417. return ret;
  418. }
  419. /**
  420. * @brief This function closes the mshX interface
  421. *
  422. * @param dev A pointer to net_device structure
  423. * @return 0
  424. */
  425. static int lbs_mesh_stop(struct net_device *dev)
  426. {
  427. struct lbs_private *priv = dev->ml_priv;
  428. lbs_deb_enter(LBS_DEB_MESH);
  429. spin_lock_irq(&priv->driver_lock);
  430. priv->mesh_open = 0;
  431. priv->mesh_connect_status = LBS_DISCONNECTED;
  432. netif_stop_queue(dev);
  433. netif_carrier_off(dev);
  434. spin_unlock_irq(&priv->driver_lock);
  435. schedule_work(&priv->mcast_work);
  436. lbs_deb_leave(LBS_DEB_MESH);
  437. return 0;
  438. }
  439. /**
  440. * @brief This function closes the ethX interface
  441. *
  442. * @param dev A pointer to net_device structure
  443. * @return 0
  444. */
  445. static int lbs_eth_stop(struct net_device *dev)
  446. {
  447. struct lbs_private *priv = netdev_priv(dev);
  448. lbs_deb_enter(LBS_DEB_NET);
  449. spin_lock_irq(&priv->driver_lock);
  450. priv->infra_open = 0;
  451. netif_stop_queue(dev);
  452. spin_unlock_irq(&priv->driver_lock);
  453. schedule_work(&priv->mcast_work);
  454. lbs_deb_leave(LBS_DEB_NET);
  455. return 0;
  456. }
  457. static void lbs_tx_timeout(struct net_device *dev)
  458. {
  459. struct lbs_private *priv = netdev_priv(dev);
  460. lbs_deb_enter(LBS_DEB_TX);
  461. lbs_pr_err("tx watch dog timeout\n");
  462. dev->trans_start = jiffies;
  463. if (priv->currenttxskb)
  464. lbs_send_tx_feedback(priv, 0);
  465. /* XX: Shouldn't we also call into the hw-specific driver
  466. to kick it somehow? */
  467. lbs_host_to_card_done(priv);
  468. /* More often than not, this actually happens because the
  469. firmware has crapped itself -- rather than just a very
  470. busy medium. So send a harmless command, and if/when
  471. _that_ times out, we'll kick it in the head. */
  472. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  473. 0, 0, NULL);
  474. lbs_deb_leave(LBS_DEB_TX);
  475. }
  476. void lbs_host_to_card_done(struct lbs_private *priv)
  477. {
  478. unsigned long flags;
  479. lbs_deb_enter(LBS_DEB_THREAD);
  480. spin_lock_irqsave(&priv->driver_lock, flags);
  481. priv->dnld_sent = DNLD_RES_RECEIVED;
  482. /* Wake main thread if commands are pending */
  483. if (!priv->cur_cmd || priv->tx_pending_len > 0)
  484. wake_up_interruptible(&priv->waitq);
  485. spin_unlock_irqrestore(&priv->driver_lock, flags);
  486. lbs_deb_leave(LBS_DEB_THREAD);
  487. }
  488. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  489. /**
  490. * @brief This function returns the network statistics
  491. *
  492. * @param dev A pointer to struct lbs_private structure
  493. * @return A pointer to net_device_stats structure
  494. */
  495. static struct net_device_stats *lbs_get_stats(struct net_device *dev)
  496. {
  497. struct lbs_private *priv = netdev_priv(dev);
  498. lbs_deb_enter(LBS_DEB_NET);
  499. return &priv->stats;
  500. }
  501. static int lbs_set_mac_address(struct net_device *dev, void *addr)
  502. {
  503. int ret = 0;
  504. struct lbs_private *priv = netdev_priv(dev);
  505. struct sockaddr *phwaddr = addr;
  506. struct cmd_ds_802_11_mac_address cmd;
  507. lbs_deb_enter(LBS_DEB_NET);
  508. /* In case it was called from the mesh device */
  509. dev = priv->dev;
  510. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  511. cmd.action = cpu_to_le16(CMD_ACT_SET);
  512. memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
  513. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  514. if (ret) {
  515. lbs_deb_net("set MAC address failed\n");
  516. goto done;
  517. }
  518. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  519. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  520. if (priv->mesh_dev)
  521. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  522. done:
  523. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  524. return ret;
  525. }
  526. static inline int mac_in_list(unsigned char *list, int list_len,
  527. unsigned char *mac)
  528. {
  529. while (list_len) {
  530. if (!memcmp(list, mac, ETH_ALEN))
  531. return 1;
  532. list += ETH_ALEN;
  533. list_len--;
  534. }
  535. return 0;
  536. }
  537. static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
  538. struct net_device *dev, int nr_addrs)
  539. {
  540. int i = nr_addrs;
  541. struct dev_mc_list *mc_list;
  542. if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
  543. return nr_addrs;
  544. netif_addr_lock_bh(dev);
  545. for (mc_list = dev->mc_list; mc_list; mc_list = mc_list->next) {
  546. if (mac_in_list(cmd->maclist, nr_addrs, mc_list->dmi_addr)) {
  547. lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
  548. mc_list->dmi_addr);
  549. continue;
  550. }
  551. if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
  552. break;
  553. memcpy(&cmd->maclist[6*i], mc_list->dmi_addr, ETH_ALEN);
  554. lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
  555. mc_list->dmi_addr);
  556. i++;
  557. }
  558. netif_addr_unlock_bh(dev);
  559. if (mc_list)
  560. return -EOVERFLOW;
  561. return i;
  562. }
  563. static void lbs_set_mcast_worker(struct work_struct *work)
  564. {
  565. struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
  566. struct cmd_ds_mac_multicast_adr mcast_cmd;
  567. int dev_flags;
  568. int nr_addrs;
  569. int old_mac_control = priv->mac_control;
  570. lbs_deb_enter(LBS_DEB_NET);
  571. dev_flags = priv->dev->flags;
  572. if (priv->mesh_dev)
  573. dev_flags |= priv->mesh_dev->flags;
  574. if (dev_flags & IFF_PROMISC) {
  575. priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  576. priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  577. CMD_ACT_MAC_MULTICAST_ENABLE);
  578. goto out_set_mac_control;
  579. } else if (dev_flags & IFF_ALLMULTI) {
  580. do_allmulti:
  581. priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  582. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  583. CMD_ACT_MAC_MULTICAST_ENABLE);
  584. goto out_set_mac_control;
  585. }
  586. /* Once for priv->dev, again for priv->mesh_dev if it exists */
  587. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
  588. if (nr_addrs >= 0 && priv->mesh_dev)
  589. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
  590. if (nr_addrs < 0)
  591. goto do_allmulti;
  592. if (nr_addrs) {
  593. int size = offsetof(struct cmd_ds_mac_multicast_adr,
  594. maclist[6*nr_addrs]);
  595. mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
  596. mcast_cmd.hdr.size = cpu_to_le16(size);
  597. mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
  598. lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
  599. priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
  600. } else
  601. priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
  602. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  603. CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
  604. out_set_mac_control:
  605. if (priv->mac_control != old_mac_control)
  606. lbs_set_mac_control(priv);
  607. lbs_deb_leave(LBS_DEB_NET);
  608. }
  609. static void lbs_set_multicast_list(struct net_device *dev)
  610. {
  611. struct lbs_private *priv = netdev_priv(dev);
  612. schedule_work(&priv->mcast_work);
  613. }
  614. /**
  615. * @brief This function handles the major jobs in the LBS driver.
  616. * It handles all events generated by firmware, RX data received
  617. * from firmware and TX data sent from kernel.
  618. *
  619. * @param data A pointer to lbs_thread structure
  620. * @return 0
  621. */
  622. static int lbs_thread(void *data)
  623. {
  624. struct net_device *dev = data;
  625. struct lbs_private *priv = netdev_priv(dev);
  626. wait_queue_t wait;
  627. lbs_deb_enter(LBS_DEB_THREAD);
  628. init_waitqueue_entry(&wait, current);
  629. for (;;) {
  630. int shouldsleep;
  631. u8 resp_idx;
  632. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  633. priv->currenttxskb, priv->dnld_sent);
  634. add_wait_queue(&priv->waitq, &wait);
  635. set_current_state(TASK_INTERRUPTIBLE);
  636. spin_lock_irq(&priv->driver_lock);
  637. if (kthread_should_stop())
  638. shouldsleep = 0; /* Bye */
  639. else if (priv->surpriseremoved)
  640. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  641. else if (priv->psstate == PS_STATE_SLEEP)
  642. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  643. else if (priv->cmd_timed_out)
  644. shouldsleep = 0; /* Command timed out. Recover */
  645. else if (!priv->fw_ready)
  646. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  647. else if (priv->dnld_sent)
  648. shouldsleep = 1; /* Something is en route to the device already */
  649. else if (priv->tx_pending_len > 0)
  650. shouldsleep = 0; /* We've a packet to send */
  651. else if (priv->resp_len[priv->resp_idx])
  652. shouldsleep = 0; /* We have a command response */
  653. else if (priv->cur_cmd)
  654. shouldsleep = 1; /* Can't send a command; one already running */
  655. else if (!list_empty(&priv->cmdpendingq))
  656. shouldsleep = 0; /* We have a command to send */
  657. else if (__kfifo_len(priv->event_fifo))
  658. shouldsleep = 0; /* We have an event to process */
  659. else
  660. shouldsleep = 1; /* No command */
  661. if (shouldsleep) {
  662. lbs_deb_thread("sleeping, connect_status %d, "
  663. "psmode %d, psstate %d\n",
  664. priv->connect_status,
  665. priv->psmode, priv->psstate);
  666. spin_unlock_irq(&priv->driver_lock);
  667. schedule();
  668. } else
  669. spin_unlock_irq(&priv->driver_lock);
  670. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  671. priv->currenttxskb, priv->dnld_sent);
  672. set_current_state(TASK_RUNNING);
  673. remove_wait_queue(&priv->waitq, &wait);
  674. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  675. priv->currenttxskb, priv->dnld_sent);
  676. if (kthread_should_stop()) {
  677. lbs_deb_thread("break from main thread\n");
  678. break;
  679. }
  680. if (priv->surpriseremoved) {
  681. lbs_deb_thread("adapter removed; waiting to die...\n");
  682. continue;
  683. }
  684. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  685. priv->currenttxskb, priv->dnld_sent);
  686. /* Process any pending command response */
  687. spin_lock_irq(&priv->driver_lock);
  688. resp_idx = priv->resp_idx;
  689. if (priv->resp_len[resp_idx]) {
  690. spin_unlock_irq(&priv->driver_lock);
  691. lbs_process_command_response(priv,
  692. priv->resp_buf[resp_idx],
  693. priv->resp_len[resp_idx]);
  694. spin_lock_irq(&priv->driver_lock);
  695. priv->resp_len[resp_idx] = 0;
  696. }
  697. spin_unlock_irq(&priv->driver_lock);
  698. /* command timeout stuff */
  699. if (priv->cmd_timed_out && priv->cur_cmd) {
  700. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  701. if (++priv->nr_retries > 3) {
  702. lbs_pr_info("Excessive timeouts submitting "
  703. "command 0x%04x\n",
  704. le16_to_cpu(cmdnode->cmdbuf->command));
  705. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  706. priv->nr_retries = 0;
  707. if (priv->reset_card)
  708. priv->reset_card(priv);
  709. } else {
  710. priv->cur_cmd = NULL;
  711. priv->dnld_sent = DNLD_RES_RECEIVED;
  712. lbs_pr_info("requeueing command 0x%04x due "
  713. "to timeout (#%d)\n",
  714. le16_to_cpu(cmdnode->cmdbuf->command),
  715. priv->nr_retries);
  716. /* Stick it back at the _top_ of the pending queue
  717. for immediate resubmission */
  718. list_add(&cmdnode->list, &priv->cmdpendingq);
  719. }
  720. }
  721. priv->cmd_timed_out = 0;
  722. /* Process hardware events, e.g. card removed, link lost */
  723. spin_lock_irq(&priv->driver_lock);
  724. while (__kfifo_len(priv->event_fifo)) {
  725. u32 event;
  726. __kfifo_get(priv->event_fifo, (unsigned char *) &event,
  727. sizeof(event));
  728. spin_unlock_irq(&priv->driver_lock);
  729. lbs_process_event(priv, event);
  730. spin_lock_irq(&priv->driver_lock);
  731. }
  732. spin_unlock_irq(&priv->driver_lock);
  733. if (!priv->fw_ready)
  734. continue;
  735. /* Check if we need to confirm Sleep Request received previously */
  736. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  737. !priv->dnld_sent && !priv->cur_cmd) {
  738. if (priv->connect_status == LBS_CONNECTED) {
  739. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  740. "dnld_sent %d, cur_cmd %p\n",
  741. priv->currenttxskb, priv->dnld_sent,
  742. priv->cur_cmd);
  743. lbs_ps_confirm_sleep(priv);
  744. } else {
  745. /* workaround for firmware sending
  746. * deauth/linkloss event immediately
  747. * after sleep request; remove this
  748. * after firmware fixes it
  749. */
  750. priv->psstate = PS_STATE_AWAKE;
  751. lbs_pr_alert("ignore PS_SleepConfirm in "
  752. "non-connected state\n");
  753. }
  754. }
  755. /* The PS state is changed during processing of Sleep Request
  756. * event above
  757. */
  758. if ((priv->psstate == PS_STATE_SLEEP) ||
  759. (priv->psstate == PS_STATE_PRE_SLEEP))
  760. continue;
  761. /* Execute the next command */
  762. if (!priv->dnld_sent && !priv->cur_cmd)
  763. lbs_execute_next_command(priv);
  764. /* Wake-up command waiters which can't sleep in
  765. * lbs_prepare_and_send_command
  766. */
  767. if (!list_empty(&priv->cmdpendingq))
  768. wake_up_all(&priv->cmd_pending);
  769. spin_lock_irq(&priv->driver_lock);
  770. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  771. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  772. priv->tx_pending_buf,
  773. priv->tx_pending_len);
  774. if (ret) {
  775. lbs_deb_tx("host_to_card failed %d\n", ret);
  776. priv->dnld_sent = DNLD_RES_RECEIVED;
  777. }
  778. priv->tx_pending_len = 0;
  779. if (!priv->currenttxskb) {
  780. /* We can wake the queues immediately if we aren't
  781. waiting for TX feedback */
  782. if (priv->connect_status == LBS_CONNECTED)
  783. netif_wake_queue(priv->dev);
  784. if (priv->mesh_dev &&
  785. priv->mesh_connect_status == LBS_CONNECTED)
  786. netif_wake_queue(priv->mesh_dev);
  787. }
  788. }
  789. spin_unlock_irq(&priv->driver_lock);
  790. }
  791. del_timer(&priv->command_timer);
  792. wake_up_all(&priv->cmd_pending);
  793. lbs_deb_leave(LBS_DEB_THREAD);
  794. return 0;
  795. }
  796. static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
  797. struct cmd_header *cmd)
  798. {
  799. lbs_deb_enter(LBS_DEB_FW);
  800. netif_device_detach(priv->dev);
  801. if (priv->mesh_dev)
  802. netif_device_detach(priv->mesh_dev);
  803. priv->fw_ready = 0;
  804. lbs_deb_leave(LBS_DEB_FW);
  805. return 0;
  806. }
  807. int lbs_suspend(struct lbs_private *priv)
  808. {
  809. struct cmd_header cmd;
  810. int ret;
  811. lbs_deb_enter(LBS_DEB_FW);
  812. if (priv->wol_criteria == 0xffffffff) {
  813. lbs_pr_info("Suspend attempt without configuring wake params!\n");
  814. return -EINVAL;
  815. }
  816. memset(&cmd, 0, sizeof(cmd));
  817. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
  818. sizeof(cmd), lbs_suspend_callback, 0);
  819. if (ret)
  820. lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
  821. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  822. return ret;
  823. }
  824. EXPORT_SYMBOL_GPL(lbs_suspend);
  825. void lbs_resume(struct lbs_private *priv)
  826. {
  827. lbs_deb_enter(LBS_DEB_FW);
  828. priv->fw_ready = 1;
  829. /* Firmware doesn't seem to give us RX packets any more
  830. until we send it some command. Might as well update */
  831. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  832. 0, 0, NULL);
  833. netif_device_attach(priv->dev);
  834. if (priv->mesh_dev)
  835. netif_device_attach(priv->mesh_dev);
  836. lbs_deb_leave(LBS_DEB_FW);
  837. }
  838. EXPORT_SYMBOL_GPL(lbs_resume);
  839. /**
  840. * @brief This function downloads firmware image, gets
  841. * HW spec from firmware and set basic parameters to
  842. * firmware.
  843. *
  844. * @param priv A pointer to struct lbs_private structure
  845. * @return 0 or -1
  846. */
  847. static int lbs_setup_firmware(struct lbs_private *priv)
  848. {
  849. int ret = -1;
  850. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  851. lbs_deb_enter(LBS_DEB_FW);
  852. /* Read MAC address from firmware */
  853. memset(priv->current_addr, 0xff, ETH_ALEN);
  854. ret = lbs_update_hw_spec(priv);
  855. if (ret)
  856. goto done;
  857. /* Read power levels if available */
  858. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  859. if (ret == 0) {
  860. priv->txpower_cur = curlevel;
  861. priv->txpower_min = minlevel;
  862. priv->txpower_max = maxlevel;
  863. }
  864. lbs_set_mac_control(priv);
  865. done:
  866. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  867. return ret;
  868. }
  869. /**
  870. * This function handles the timeout of command sending.
  871. * It will re-send the same command again.
  872. */
  873. static void command_timer_fn(unsigned long data)
  874. {
  875. struct lbs_private *priv = (struct lbs_private *)data;
  876. unsigned long flags;
  877. lbs_deb_enter(LBS_DEB_CMD);
  878. spin_lock_irqsave(&priv->driver_lock, flags);
  879. if (!priv->cur_cmd)
  880. goto out;
  881. lbs_pr_info("command 0x%04x timed out\n",
  882. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  883. priv->cmd_timed_out = 1;
  884. wake_up_interruptible(&priv->waitq);
  885. out:
  886. spin_unlock_irqrestore(&priv->driver_lock, flags);
  887. lbs_deb_leave(LBS_DEB_CMD);
  888. }
  889. static void lbs_sync_channel_worker(struct work_struct *work)
  890. {
  891. struct lbs_private *priv = container_of(work, struct lbs_private,
  892. sync_channel);
  893. lbs_deb_enter(LBS_DEB_MAIN);
  894. if (lbs_update_channel(priv))
  895. lbs_pr_info("Channel synchronization failed.");
  896. lbs_deb_leave(LBS_DEB_MAIN);
  897. }
  898. static int lbs_init_adapter(struct lbs_private *priv)
  899. {
  900. size_t bufsize;
  901. int i, ret = 0;
  902. lbs_deb_enter(LBS_DEB_MAIN);
  903. /* Allocate buffer to store the BSSID list */
  904. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  905. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  906. if (!priv->networks) {
  907. lbs_pr_err("Out of memory allocating beacons\n");
  908. ret = -1;
  909. goto out;
  910. }
  911. /* Initialize scan result lists */
  912. INIT_LIST_HEAD(&priv->network_free_list);
  913. INIT_LIST_HEAD(&priv->network_list);
  914. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  915. list_add_tail(&priv->networks[i].list,
  916. &priv->network_free_list);
  917. }
  918. memset(priv->current_addr, 0xff, ETH_ALEN);
  919. priv->connect_status = LBS_DISCONNECTED;
  920. priv->mesh_connect_status = LBS_DISCONNECTED;
  921. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  922. priv->mode = IW_MODE_INFRA;
  923. priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  924. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  925. priv->radio_on = 1;
  926. priv->enablehwauto = 1;
  927. priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  928. priv->psmode = LBS802_11POWERMODECAM;
  929. priv->psstate = PS_STATE_FULL_POWER;
  930. mutex_init(&priv->lock);
  931. setup_timer(&priv->command_timer, command_timer_fn,
  932. (unsigned long)priv);
  933. INIT_LIST_HEAD(&priv->cmdfreeq);
  934. INIT_LIST_HEAD(&priv->cmdpendingq);
  935. spin_lock_init(&priv->driver_lock);
  936. init_waitqueue_head(&priv->cmd_pending);
  937. /* Allocate the command buffers */
  938. if (lbs_allocate_cmd_buffer(priv)) {
  939. lbs_pr_err("Out of memory allocating command buffers\n");
  940. ret = -ENOMEM;
  941. goto out;
  942. }
  943. priv->resp_idx = 0;
  944. priv->resp_len[0] = priv->resp_len[1] = 0;
  945. /* Create the event FIFO */
  946. priv->event_fifo = kfifo_alloc(sizeof(u32) * 16, GFP_KERNEL, NULL);
  947. if (IS_ERR(priv->event_fifo)) {
  948. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  949. ret = -ENOMEM;
  950. goto out;
  951. }
  952. out:
  953. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  954. return ret;
  955. }
  956. static void lbs_free_adapter(struct lbs_private *priv)
  957. {
  958. lbs_deb_enter(LBS_DEB_MAIN);
  959. lbs_free_cmd_buffer(priv);
  960. if (priv->event_fifo)
  961. kfifo_free(priv->event_fifo);
  962. del_timer(&priv->command_timer);
  963. kfree(priv->networks);
  964. priv->networks = NULL;
  965. lbs_deb_leave(LBS_DEB_MAIN);
  966. }
  967. /**
  968. * @brief This function adds the card. it will probe the
  969. * card, allocate the lbs_priv and initialize the device.
  970. *
  971. * @param card A pointer to card
  972. * @return A pointer to struct lbs_private structure
  973. */
  974. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  975. {
  976. struct net_device *dev = NULL;
  977. struct lbs_private *priv = NULL;
  978. lbs_deb_enter(LBS_DEB_MAIN);
  979. /* Allocate an Ethernet device and register it */
  980. dev = alloc_etherdev(sizeof(struct lbs_private));
  981. if (!dev) {
  982. lbs_pr_err("init ethX device failed\n");
  983. goto done;
  984. }
  985. priv = netdev_priv(dev);
  986. if (lbs_init_adapter(priv)) {
  987. lbs_pr_err("failed to initialize adapter structure.\n");
  988. goto err_init_adapter;
  989. }
  990. priv->dev = dev;
  991. priv->card = card;
  992. priv->mesh_open = 0;
  993. priv->infra_open = 0;
  994. /* Setup the OS Interface to our functions */
  995. dev->open = lbs_dev_open;
  996. dev->hard_start_xmit = lbs_hard_start_xmit;
  997. dev->stop = lbs_eth_stop;
  998. dev->set_mac_address = lbs_set_mac_address;
  999. dev->tx_timeout = lbs_tx_timeout;
  1000. dev->get_stats = lbs_get_stats;
  1001. dev->watchdog_timeo = 5 * HZ;
  1002. dev->ethtool_ops = &lbs_ethtool_ops;
  1003. #ifdef WIRELESS_EXT
  1004. dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
  1005. #endif
  1006. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1007. dev->set_multicast_list = lbs_set_multicast_list;
  1008. SET_NETDEV_DEV(dev, dmdev);
  1009. priv->rtap_net_dev = NULL;
  1010. lbs_deb_thread("Starting main thread...\n");
  1011. init_waitqueue_head(&priv->waitq);
  1012. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  1013. if (IS_ERR(priv->main_thread)) {
  1014. lbs_deb_thread("Error creating main thread.\n");
  1015. goto err_init_adapter;
  1016. }
  1017. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  1018. INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
  1019. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  1020. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  1021. INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
  1022. sprintf(priv->mesh_ssid, "mesh");
  1023. priv->mesh_ssid_len = 4;
  1024. priv->wol_criteria = 0xffffffff;
  1025. priv->wol_gpio = 0xff;
  1026. goto done;
  1027. err_init_adapter:
  1028. lbs_free_adapter(priv);
  1029. free_netdev(dev);
  1030. priv = NULL;
  1031. done:
  1032. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  1033. return priv;
  1034. }
  1035. EXPORT_SYMBOL_GPL(lbs_add_card);
  1036. void lbs_remove_card(struct lbs_private *priv)
  1037. {
  1038. struct net_device *dev = priv->dev;
  1039. union iwreq_data wrqu;
  1040. lbs_deb_enter(LBS_DEB_MAIN);
  1041. lbs_remove_mesh(priv);
  1042. lbs_remove_rtap(priv);
  1043. dev = priv->dev;
  1044. cancel_delayed_work_sync(&priv->scan_work);
  1045. cancel_delayed_work_sync(&priv->assoc_work);
  1046. cancel_work_sync(&priv->mcast_work);
  1047. /* worker thread destruction blocks on the in-flight command which
  1048. * should have been cleared already in lbs_stop_card().
  1049. */
  1050. lbs_deb_main("destroying worker thread\n");
  1051. destroy_workqueue(priv->work_thread);
  1052. lbs_deb_main("done destroying worker thread\n");
  1053. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  1054. priv->psmode = LBS802_11POWERMODECAM;
  1055. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  1056. }
  1057. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  1058. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1059. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1060. /* Stop the thread servicing the interrupts */
  1061. priv->surpriseremoved = 1;
  1062. kthread_stop(priv->main_thread);
  1063. lbs_free_adapter(priv);
  1064. priv->dev = NULL;
  1065. free_netdev(dev);
  1066. lbs_deb_leave(LBS_DEB_MAIN);
  1067. }
  1068. EXPORT_SYMBOL_GPL(lbs_remove_card);
  1069. int lbs_start_card(struct lbs_private *priv)
  1070. {
  1071. struct net_device *dev = priv->dev;
  1072. int ret = -1;
  1073. lbs_deb_enter(LBS_DEB_MAIN);
  1074. /* poke the firmware */
  1075. ret = lbs_setup_firmware(priv);
  1076. if (ret)
  1077. goto done;
  1078. /* init 802.11d */
  1079. lbs_init_11d(priv);
  1080. if (register_netdev(dev)) {
  1081. lbs_pr_err("cannot register ethX device\n");
  1082. goto done;
  1083. }
  1084. lbs_update_channel(priv);
  1085. /* 5.0.16p0 is known to NOT support any mesh */
  1086. if (priv->fwrelease > 0x05001000) {
  1087. /* Enable mesh, if supported, and work out which TLV it uses.
  1088. 0x100 + 291 is an unofficial value used in 5.110.20.pXX
  1089. 0x100 + 37 is the official value used in 5.110.21.pXX
  1090. but we check them in that order because 20.pXX doesn't
  1091. give an error -- it just silently fails. */
  1092. /* 5.110.20.pXX firmware will fail the command if the channel
  1093. doesn't match the existing channel. But only if the TLV
  1094. is correct. If the channel is wrong, _BOTH_ versions will
  1095. give an error to 0x100+291, and allow 0x100+37 to succeed.
  1096. It's just that 5.110.20.pXX will not have done anything
  1097. useful */
  1098. priv->mesh_tlv = 0x100 + 291;
  1099. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  1100. priv->curbssparams.channel)) {
  1101. priv->mesh_tlv = 0x100 + 37;
  1102. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  1103. priv->curbssparams.channel))
  1104. priv->mesh_tlv = 0;
  1105. }
  1106. if (priv->mesh_tlv) {
  1107. lbs_add_mesh(priv);
  1108. if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
  1109. lbs_pr_err("cannot register lbs_mesh attribute\n");
  1110. /* While rtap isn't related to mesh, only mesh-enabled
  1111. * firmware implements the rtap functionality via
  1112. * CMD_802_11_MONITOR_MODE.
  1113. */
  1114. if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
  1115. lbs_pr_err("cannot register lbs_rtap attribute\n");
  1116. }
  1117. }
  1118. lbs_debugfs_init_one(priv, dev);
  1119. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  1120. ret = 0;
  1121. done:
  1122. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1123. return ret;
  1124. }
  1125. EXPORT_SYMBOL_GPL(lbs_start_card);
  1126. void lbs_stop_card(struct lbs_private *priv)
  1127. {
  1128. struct net_device *dev = priv->dev;
  1129. struct cmd_ctrl_node *cmdnode;
  1130. unsigned long flags;
  1131. lbs_deb_enter(LBS_DEB_MAIN);
  1132. if (!priv)
  1133. goto out;
  1134. netif_stop_queue(priv->dev);
  1135. netif_carrier_off(priv->dev);
  1136. lbs_debugfs_remove_one(priv);
  1137. if (priv->mesh_tlv) {
  1138. device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
  1139. device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
  1140. }
  1141. /* Delete the timeout of the currently processing command */
  1142. del_timer_sync(&priv->command_timer);
  1143. /* Flush pending command nodes */
  1144. spin_lock_irqsave(&priv->driver_lock, flags);
  1145. lbs_deb_main("clearing pending commands\n");
  1146. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  1147. cmdnode->result = -ENOENT;
  1148. cmdnode->cmdwaitqwoken = 1;
  1149. wake_up_interruptible(&cmdnode->cmdwait_q);
  1150. }
  1151. /* Flush the command the card is currently processing */
  1152. if (priv->cur_cmd) {
  1153. lbs_deb_main("clearing current command\n");
  1154. priv->cur_cmd->result = -ENOENT;
  1155. priv->cur_cmd->cmdwaitqwoken = 1;
  1156. wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
  1157. }
  1158. lbs_deb_main("done clearing commands\n");
  1159. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1160. unregister_netdev(dev);
  1161. out:
  1162. lbs_deb_leave(LBS_DEB_MAIN);
  1163. }
  1164. EXPORT_SYMBOL_GPL(lbs_stop_card);
  1165. /**
  1166. * @brief This function adds mshX interface
  1167. *
  1168. * @param priv A pointer to the struct lbs_private structure
  1169. * @return 0 if successful, -X otherwise
  1170. */
  1171. static int lbs_add_mesh(struct lbs_private *priv)
  1172. {
  1173. struct net_device *mesh_dev = NULL;
  1174. int ret = 0;
  1175. lbs_deb_enter(LBS_DEB_MESH);
  1176. /* Allocate a virtual mesh device */
  1177. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1178. lbs_deb_mesh("init mshX device failed\n");
  1179. ret = -ENOMEM;
  1180. goto done;
  1181. }
  1182. mesh_dev->ml_priv = priv;
  1183. priv->mesh_dev = mesh_dev;
  1184. mesh_dev->open = lbs_dev_open;
  1185. mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
  1186. mesh_dev->stop = lbs_mesh_stop;
  1187. mesh_dev->get_stats = lbs_get_stats;
  1188. mesh_dev->set_mac_address = lbs_set_mac_address;
  1189. mesh_dev->ethtool_ops = &lbs_ethtool_ops;
  1190. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1191. sizeof(priv->dev->dev_addr));
  1192. SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
  1193. #ifdef WIRELESS_EXT
  1194. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1195. #endif
  1196. mesh_dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1197. mesh_dev->set_multicast_list = lbs_set_multicast_list;
  1198. /* Register virtual mesh interface */
  1199. ret = register_netdev(mesh_dev);
  1200. if (ret) {
  1201. lbs_pr_err("cannot register mshX virtual interface\n");
  1202. goto err_free;
  1203. }
  1204. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1205. if (ret)
  1206. goto err_unregister;
  1207. lbs_persist_config_init(mesh_dev);
  1208. /* Everything successful */
  1209. ret = 0;
  1210. goto done;
  1211. err_unregister:
  1212. unregister_netdev(mesh_dev);
  1213. err_free:
  1214. free_netdev(mesh_dev);
  1215. done:
  1216. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1217. return ret;
  1218. }
  1219. static void lbs_remove_mesh(struct lbs_private *priv)
  1220. {
  1221. struct net_device *mesh_dev;
  1222. mesh_dev = priv->mesh_dev;
  1223. if (!mesh_dev)
  1224. return;
  1225. lbs_deb_enter(LBS_DEB_MESH);
  1226. netif_stop_queue(mesh_dev);
  1227. netif_carrier_off(mesh_dev);
  1228. sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1229. lbs_persist_config_remove(mesh_dev);
  1230. unregister_netdev(mesh_dev);
  1231. priv->mesh_dev = NULL;
  1232. free_netdev(mesh_dev);
  1233. lbs_deb_leave(LBS_DEB_MESH);
  1234. }
  1235. /**
  1236. * @brief This function finds the CFP in
  1237. * region_cfp_table based on region and band parameter.
  1238. *
  1239. * @param region The region code
  1240. * @param band The band
  1241. * @param cfp_no A pointer to CFP number
  1242. * @return A pointer to CFP
  1243. */
  1244. struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
  1245. {
  1246. int i, end;
  1247. lbs_deb_enter(LBS_DEB_MAIN);
  1248. end = ARRAY_SIZE(region_cfp_table);
  1249. for (i = 0; i < end ; i++) {
  1250. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1251. region_cfp_table[i].region);
  1252. if (region_cfp_table[i].region == region) {
  1253. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1254. lbs_deb_leave(LBS_DEB_MAIN);
  1255. return region_cfp_table[i].cfp_BG;
  1256. }
  1257. }
  1258. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1259. return NULL;
  1260. }
  1261. int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
  1262. {
  1263. int ret = 0;
  1264. int i = 0;
  1265. struct chan_freq_power *cfp;
  1266. int cfp_no;
  1267. lbs_deb_enter(LBS_DEB_MAIN);
  1268. memset(priv->region_channel, 0, sizeof(priv->region_channel));
  1269. cfp = lbs_get_region_cfp_table(region, &cfp_no);
  1270. if (cfp != NULL) {
  1271. priv->region_channel[i].nrcfp = cfp_no;
  1272. priv->region_channel[i].CFP = cfp;
  1273. } else {
  1274. lbs_deb_main("wrong region code %#x in band B/G\n",
  1275. region);
  1276. ret = -1;
  1277. goto out;
  1278. }
  1279. priv->region_channel[i].valid = 1;
  1280. priv->region_channel[i].region = region;
  1281. priv->region_channel[i].band = band;
  1282. i++;
  1283. out:
  1284. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1285. return ret;
  1286. }
  1287. void lbs_queue_event(struct lbs_private *priv, u32 event)
  1288. {
  1289. unsigned long flags;
  1290. lbs_deb_enter(LBS_DEB_THREAD);
  1291. spin_lock_irqsave(&priv->driver_lock, flags);
  1292. if (priv->psstate == PS_STATE_SLEEP)
  1293. priv->psstate = PS_STATE_AWAKE;
  1294. __kfifo_put(priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  1295. wake_up_interruptible(&priv->waitq);
  1296. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1297. lbs_deb_leave(LBS_DEB_THREAD);
  1298. }
  1299. EXPORT_SYMBOL_GPL(lbs_queue_event);
  1300. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  1301. {
  1302. lbs_deb_enter(LBS_DEB_THREAD);
  1303. if (priv->psstate == PS_STATE_SLEEP)
  1304. priv->psstate = PS_STATE_AWAKE;
  1305. /* Swap buffers by flipping the response index */
  1306. BUG_ON(resp_idx > 1);
  1307. priv->resp_idx = resp_idx;
  1308. wake_up_interruptible(&priv->waitq);
  1309. lbs_deb_leave(LBS_DEB_THREAD);
  1310. }
  1311. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  1312. static int __init lbs_init_module(void)
  1313. {
  1314. lbs_deb_enter(LBS_DEB_MAIN);
  1315. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  1316. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  1317. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  1318. confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  1319. lbs_debugfs_init();
  1320. lbs_deb_leave(LBS_DEB_MAIN);
  1321. return 0;
  1322. }
  1323. static void __exit lbs_exit_module(void)
  1324. {
  1325. lbs_deb_enter(LBS_DEB_MAIN);
  1326. lbs_debugfs_remove();
  1327. lbs_deb_leave(LBS_DEB_MAIN);
  1328. }
  1329. /*
  1330. * rtap interface support fuctions
  1331. */
  1332. static int lbs_rtap_open(struct net_device *dev)
  1333. {
  1334. /* Yes, _stop_ the queue. Because we don't support injection */
  1335. lbs_deb_enter(LBS_DEB_MAIN);
  1336. netif_carrier_off(dev);
  1337. netif_stop_queue(dev);
  1338. lbs_deb_leave(LBS_DEB_LEAVE);
  1339. return 0;
  1340. }
  1341. static int lbs_rtap_stop(struct net_device *dev)
  1342. {
  1343. lbs_deb_enter(LBS_DEB_MAIN);
  1344. lbs_deb_leave(LBS_DEB_MAIN);
  1345. return 0;
  1346. }
  1347. static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1348. {
  1349. netif_stop_queue(dev);
  1350. return NETDEV_TX_BUSY;
  1351. }
  1352. static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
  1353. {
  1354. struct lbs_private *priv = dev->ml_priv;
  1355. lbs_deb_enter(LBS_DEB_NET);
  1356. return &priv->stats;
  1357. }
  1358. static void lbs_remove_rtap(struct lbs_private *priv)
  1359. {
  1360. lbs_deb_enter(LBS_DEB_MAIN);
  1361. if (priv->rtap_net_dev == NULL)
  1362. goto out;
  1363. unregister_netdev(priv->rtap_net_dev);
  1364. free_netdev(priv->rtap_net_dev);
  1365. priv->rtap_net_dev = NULL;
  1366. out:
  1367. lbs_deb_leave(LBS_DEB_MAIN);
  1368. }
  1369. static int lbs_add_rtap(struct lbs_private *priv)
  1370. {
  1371. int ret = 0;
  1372. struct net_device *rtap_dev;
  1373. lbs_deb_enter(LBS_DEB_MAIN);
  1374. if (priv->rtap_net_dev) {
  1375. ret = -EPERM;
  1376. goto out;
  1377. }
  1378. rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
  1379. if (rtap_dev == NULL) {
  1380. ret = -ENOMEM;
  1381. goto out;
  1382. }
  1383. memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
  1384. rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1385. rtap_dev->open = lbs_rtap_open;
  1386. rtap_dev->stop = lbs_rtap_stop;
  1387. rtap_dev->get_stats = lbs_rtap_get_stats;
  1388. rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
  1389. rtap_dev->ml_priv = priv;
  1390. SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
  1391. ret = register_netdev(rtap_dev);
  1392. if (ret) {
  1393. free_netdev(rtap_dev);
  1394. goto out;
  1395. }
  1396. priv->rtap_net_dev = rtap_dev;
  1397. out:
  1398. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1399. return ret;
  1400. }
  1401. module_init(lbs_init_module);
  1402. module_exit(lbs_exit_module);
  1403. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1404. MODULE_AUTHOR("Marvell International Ltd.");
  1405. MODULE_LICENSE("GPL");