main.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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 lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  24. #ifdef DEBUG
  25. "-dbg"
  26. #endif
  27. "";
  28. /* Module parameters */
  29. unsigned int lbs_debug;
  30. EXPORT_SYMBOL_GPL(lbs_debug);
  31. module_param_named(libertas_debug, lbs_debug, int, 0644);
  32. #define LBS_TX_PWR_DEFAULT 20 /*100mW */
  33. #define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
  34. #define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
  35. #define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
  36. #define LBS_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, LBS_TX_PWR_US_DEFAULT},
  41. {2, 2417, LBS_TX_PWR_US_DEFAULT},
  42. {3, 2422, LBS_TX_PWR_US_DEFAULT},
  43. {4, 2427, LBS_TX_PWR_US_DEFAULT},
  44. {5, 2432, LBS_TX_PWR_US_DEFAULT},
  45. {6, 2437, LBS_TX_PWR_US_DEFAULT},
  46. {7, 2442, LBS_TX_PWR_US_DEFAULT},
  47. {8, 2447, LBS_TX_PWR_US_DEFAULT},
  48. {9, 2452, LBS_TX_PWR_US_DEFAULT},
  49. {10, 2457, LBS_TX_PWR_US_DEFAULT},
  50. {11, 2462, LBS_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, LBS_TX_PWR_EMEA_DEFAULT},
  55. {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
  56. {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
  57. {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
  58. {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
  59. {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
  60. {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
  61. {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
  62. {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
  63. {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
  64. {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
  65. {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
  66. {13, 2472, LBS_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, LBS_TX_PWR_DEFAULT},
  71. {11, 2462, LBS_TX_PWR_DEFAULT}
  72. };
  73. /* band: 'B/G', region: France */
  74. static struct chan_freq_power channel_freq_power_FR_BG[] = {
  75. {10, 2457, LBS_TX_PWR_FR_DEFAULT},
  76. {11, 2462, LBS_TX_PWR_FR_DEFAULT},
  77. {12, 2467, LBS_TX_PWR_FR_DEFAULT},
  78. {13, 2472, LBS_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, LBS_TX_PWR_JP_DEFAULT},
  83. {2, 2417, LBS_TX_PWR_JP_DEFAULT},
  84. {3, 2422, LBS_TX_PWR_JP_DEFAULT},
  85. {4, 2427, LBS_TX_PWR_JP_DEFAULT},
  86. {5, 2432, LBS_TX_PWR_JP_DEFAULT},
  87. {6, 2437, LBS_TX_PWR_JP_DEFAULT},
  88. {7, 2442, LBS_TX_PWR_JP_DEFAULT},
  89. {8, 2447, LBS_TX_PWR_JP_DEFAULT},
  90. {9, 2452, LBS_TX_PWR_JP_DEFAULT},
  91. {10, 2457, LBS_TX_PWR_JP_DEFAULT},
  92. {11, 2462, LBS_TX_PWR_JP_DEFAULT},
  93. {12, 2467, LBS_TX_PWR_JP_DEFAULT},
  94. {13, 2472, LBS_TX_PWR_JP_DEFAULT},
  95. {14, 2484, LBS_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 lbs_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 lbs_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 lbs_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 lbs_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 lbs_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. lbs_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 lbs_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. lbs_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 lbs_add_rtap(struct lbs_private *priv);
  221. void lbs_remove_rtap(struct lbs_private *priv);
  222. /**
  223. * Get function for sysfs attribute rtap
  224. */
  225. static ssize_t lbs_rtap_get(struct device *dev,
  226. struct device_attribute *attr, char * buf)
  227. {
  228. struct lbs_private *priv = to_net_dev(dev)->priv;
  229. return snprintf(buf, 5, "0x%X\n", priv->monitormode);
  230. }
  231. /**
  232. * Set function for sysfs attribute rtap
  233. */
  234. static ssize_t lbs_rtap_set(struct device *dev,
  235. struct device_attribute *attr, const char * buf, size_t count)
  236. {
  237. int monitor_mode;
  238. struct lbs_private *priv = to_net_dev(dev)->priv;
  239. sscanf(buf, "%x", &monitor_mode);
  240. if (monitor_mode != LBS_MONITOR_OFF) {
  241. if(priv->monitormode == monitor_mode)
  242. return strlen(buf);
  243. if (priv->monitormode == LBS_MONITOR_OFF) {
  244. if (priv->mode == IW_MODE_INFRA)
  245. lbs_send_deauthentication(priv);
  246. else if (priv->mode == IW_MODE_ADHOC)
  247. lbs_stop_adhoc_network(priv);
  248. lbs_add_rtap(priv);
  249. }
  250. priv->monitormode = monitor_mode;
  251. }
  252. else {
  253. if (priv->monitormode == LBS_MONITOR_OFF)
  254. return strlen(buf);
  255. priv->monitormode = LBS_MONITOR_OFF;
  256. lbs_remove_rtap(priv);
  257. if (priv->currenttxskb) {
  258. dev_kfree_skb_any(priv->currenttxskb);
  259. priv->currenttxskb = NULL;
  260. }
  261. /* Wake queues, command thread, etc. */
  262. lbs_host_to_card_done(priv);
  263. }
  264. lbs_prepare_and_send_command(priv,
  265. CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
  266. CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
  267. return strlen(buf);
  268. }
  269. /**
  270. * lbs_rtap attribute to be exported per mshX interface
  271. * through sysfs (/sys/class/net/mshX/libertas-rtap)
  272. */
  273. static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get,
  274. lbs_rtap_set );
  275. /**
  276. * anycast_mask attribute to be exported per mshX interface
  277. * through sysfs (/sys/class/net/mshX/anycast_mask)
  278. */
  279. static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
  280. static ssize_t lbs_autostart_enabled_get(struct device *dev,
  281. struct device_attribute *attr, char * buf)
  282. {
  283. struct cmd_ds_mesh_access mesh_access;
  284. memset(&mesh_access, 0, sizeof(mesh_access));
  285. lbs_prepare_and_send_command(to_net_dev(dev)->priv,
  286. CMD_MESH_ACCESS,
  287. CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
  288. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  289. return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
  290. }
  291. static ssize_t lbs_autostart_enabled_set(struct device *dev,
  292. struct device_attribute *attr, const char * buf, size_t count)
  293. {
  294. struct cmd_ds_mesh_access mesh_access;
  295. uint32_t datum;
  296. struct lbs_private *priv = (to_net_dev(dev))->priv;
  297. int ret;
  298. memset(&mesh_access, 0, sizeof(mesh_access));
  299. sscanf(buf, "%d", &datum);
  300. mesh_access.data[0] = cpu_to_le32(datum);
  301. ret = lbs_prepare_and_send_command(priv,
  302. CMD_MESH_ACCESS,
  303. CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
  304. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  305. if (ret == 0)
  306. priv->mesh_autostart_enabled = datum ? 1 : 0;
  307. return strlen(buf);
  308. }
  309. static DEVICE_ATTR(autostart_enabled, 0644,
  310. lbs_autostart_enabled_get, lbs_autostart_enabled_set);
  311. static struct attribute *lbs_mesh_sysfs_entries[] = {
  312. &dev_attr_anycast_mask.attr,
  313. &dev_attr_autostart_enabled.attr,
  314. NULL,
  315. };
  316. static struct attribute_group lbs_mesh_attr_group = {
  317. .attrs = lbs_mesh_sysfs_entries,
  318. };
  319. /**
  320. * @brief Check if the device can be open and wait if necessary.
  321. *
  322. * @param dev A pointer to net_device structure
  323. * @return 0
  324. *
  325. * For USB adapter, on some systems the device open handler will be
  326. * called before FW ready. Use the following flag check and wait
  327. * function to work around the issue.
  328. *
  329. */
  330. static int pre_open_check(struct net_device *dev)
  331. {
  332. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  333. int i = 0;
  334. while (!priv->fw_ready && i < 20) {
  335. i++;
  336. msleep_interruptible(100);
  337. }
  338. if (!priv->fw_ready) {
  339. lbs_pr_err("firmware not ready\n");
  340. return -1;
  341. }
  342. return 0;
  343. }
  344. /**
  345. * @brief This function opens the device
  346. *
  347. * @param dev A pointer to net_device structure
  348. * @return 0
  349. */
  350. static int lbs_dev_open(struct net_device *dev)
  351. {
  352. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  353. lbs_deb_enter(LBS_DEB_NET);
  354. priv->open = 1;
  355. if (priv->connect_status == LBS_CONNECTED)
  356. netif_carrier_on(priv->dev);
  357. else
  358. netif_carrier_off(priv->dev);
  359. if (priv->mesh_dev) {
  360. if (priv->mesh_connect_status == LBS_CONNECTED)
  361. netif_carrier_on(priv->mesh_dev);
  362. else
  363. netif_carrier_off(priv->mesh_dev);
  364. }
  365. lbs_deb_leave(LBS_DEB_NET);
  366. return 0;
  367. }
  368. /**
  369. * @brief This function opens the mshX interface
  370. *
  371. * @param dev A pointer to net_device structure
  372. * @return 0
  373. */
  374. static int lbs_mesh_open(struct net_device *dev)
  375. {
  376. struct lbs_private *priv = (struct lbs_private *) dev->priv ;
  377. if (pre_open_check(dev) == -1)
  378. return -1;
  379. priv->mesh_open = 1 ;
  380. netif_wake_queue(priv->mesh_dev);
  381. priv->mesh_connect_status = LBS_CONNECTED;
  382. netif_carrier_on(priv->mesh_dev);
  383. netif_wake_queue(priv->mesh_dev);
  384. if (priv->infra_open == 0)
  385. return lbs_dev_open(priv->dev) ;
  386. return 0;
  387. }
  388. /**
  389. * @brief This function opens the ethX interface
  390. *
  391. * @param dev A pointer to net_device structure
  392. * @return 0
  393. */
  394. static int lbs_open(struct net_device *dev)
  395. {
  396. struct lbs_private *priv = (struct lbs_private *) dev->priv ;
  397. if(pre_open_check(dev) == -1)
  398. return -1;
  399. priv->infra_open = 1 ;
  400. netif_wake_queue(priv->dev);
  401. if (priv->open == 0)
  402. return lbs_dev_open(priv->dev) ;
  403. return 0;
  404. }
  405. static int lbs_dev_close(struct net_device *dev)
  406. {
  407. struct lbs_private *priv = dev->priv;
  408. lbs_deb_enter(LBS_DEB_NET);
  409. netif_carrier_off(priv->dev);
  410. priv->open = 0;
  411. lbs_deb_leave(LBS_DEB_NET);
  412. return 0;
  413. }
  414. /**
  415. * @brief This function closes the mshX interface
  416. *
  417. * @param dev A pointer to net_device structure
  418. * @return 0
  419. */
  420. static int lbs_mesh_close(struct net_device *dev)
  421. {
  422. struct lbs_private *priv = (struct lbs_private *) (dev->priv);
  423. priv->mesh_open = 0;
  424. netif_stop_queue(priv->mesh_dev);
  425. if (priv->infra_open == 0)
  426. return lbs_dev_close(dev);
  427. else
  428. return 0;
  429. }
  430. /**
  431. * @brief This function closes the ethX interface
  432. *
  433. * @param dev A pointer to net_device structure
  434. * @return 0
  435. */
  436. static int lbs_close(struct net_device *dev)
  437. {
  438. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  439. netif_stop_queue(dev);
  440. priv->infra_open = 0;
  441. if (priv->mesh_open == 0)
  442. return lbs_dev_close(dev);
  443. else
  444. return 0;
  445. }
  446. static int lbs_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  447. {
  448. int ret = 0;
  449. struct lbs_private *priv = dev->priv;
  450. lbs_deb_enter(LBS_DEB_TX);
  451. /* We could return NETDEV_TX_BUSY here, but I'd actually
  452. like to get the point where we can BUG() */
  453. if (priv->dnld_sent) {
  454. lbs_pr_err("%s while dnld_sent\n", __func__);
  455. priv->stats.tx_dropped++;
  456. goto done;
  457. }
  458. if (priv->currenttxskb) {
  459. lbs_pr_err("%s while TX skb pending\n", __func__);
  460. priv->stats.tx_dropped++;
  461. goto done;
  462. }
  463. netif_stop_queue(priv->dev);
  464. if (priv->mesh_dev)
  465. netif_stop_queue(priv->mesh_dev);
  466. if (lbs_process_tx(priv, skb) == 0)
  467. dev->trans_start = jiffies;
  468. done:
  469. lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
  470. return ret;
  471. }
  472. /**
  473. * @brief Mark mesh packets and handover them to lbs_hard_start_xmit
  474. *
  475. */
  476. static int lbs_mesh_pre_start_xmit(struct sk_buff *skb,
  477. struct net_device *dev)
  478. {
  479. struct lbs_private *priv = dev->priv;
  480. int ret;
  481. lbs_deb_enter(LBS_DEB_MESH);
  482. if (priv->monitormode != LBS_MONITOR_OFF) {
  483. netif_stop_queue(dev);
  484. return -EOPNOTSUPP;
  485. }
  486. SET_MESH_FRAME(skb);
  487. ret = lbs_hard_start_xmit(skb, priv->mesh_dev);
  488. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  489. return ret;
  490. }
  491. /**
  492. * @brief Mark non-mesh packets and handover them to lbs_hard_start_xmit
  493. *
  494. */
  495. static int lbs_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
  496. {
  497. struct lbs_private *priv = dev->priv;
  498. int ret;
  499. lbs_deb_enter(LBS_DEB_TX);
  500. if (priv->monitormode != LBS_MONITOR_OFF) {
  501. netif_stop_queue(dev);
  502. return -EOPNOTSUPP;
  503. }
  504. UNSET_MESH_FRAME(skb);
  505. ret = lbs_hard_start_xmit(skb, dev);
  506. lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
  507. return ret;
  508. }
  509. static void lbs_tx_timeout(struct net_device *dev)
  510. {
  511. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  512. lbs_deb_enter(LBS_DEB_TX);
  513. lbs_pr_err("tx watch dog timeout\n");
  514. dev->trans_start = jiffies;
  515. if (priv->currenttxskb) {
  516. priv->eventcause = 0x01000000;
  517. lbs_send_tx_feedback(priv);
  518. }
  519. /* XX: Shouldn't we also call into the hw-specific driver
  520. to kick it somehow? */
  521. lbs_host_to_card_done(priv);
  522. lbs_deb_leave(LBS_DEB_TX);
  523. }
  524. void lbs_host_to_card_done(struct lbs_private *priv)
  525. {
  526. unsigned long flags;
  527. spin_lock_irqsave(&priv->driver_lock, flags);
  528. priv->dnld_sent = DNLD_RES_RECEIVED;
  529. /* Wake main thread if commands are pending */
  530. if (!priv->cur_cmd)
  531. wake_up_interruptible(&priv->waitq);
  532. /* Don't wake netif queues if we're in monitor mode and
  533. a TX packet is already pending, or if there are commands
  534. queued to be sent. */
  535. if (!priv->currenttxskb && list_empty(&priv->cmdpendingq)) {
  536. if (priv->dev && priv->connect_status == LBS_CONNECTED)
  537. netif_wake_queue(priv->dev);
  538. if (priv->mesh_dev && priv->mesh_connect_status == LBS_CONNECTED)
  539. netif_wake_queue(priv->mesh_dev);
  540. }
  541. spin_unlock_irqrestore(&priv->driver_lock, flags);
  542. }
  543. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  544. /**
  545. * @brief This function returns the network statistics
  546. *
  547. * @param dev A pointer to struct lbs_private structure
  548. * @return A pointer to net_device_stats structure
  549. */
  550. static struct net_device_stats *lbs_get_stats(struct net_device *dev)
  551. {
  552. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  553. return &priv->stats;
  554. }
  555. static int lbs_set_mac_address(struct net_device *dev, void *addr)
  556. {
  557. int ret = 0;
  558. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  559. struct sockaddr *phwaddr = addr;
  560. lbs_deb_enter(LBS_DEB_NET);
  561. /* In case it was called from the mesh device */
  562. dev = priv->dev ;
  563. memset(priv->current_addr, 0, ETH_ALEN);
  564. /* dev->dev_addr is 8 bytes */
  565. lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
  566. lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
  567. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  568. ret = lbs_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
  569. CMD_ACT_SET,
  570. CMD_OPTION_WAITFORRSP, 0, NULL);
  571. if (ret) {
  572. lbs_deb_net("set MAC address failed\n");
  573. ret = -1;
  574. goto done;
  575. }
  576. lbs_deb_hex(LBS_DEB_NET, "priv->macaddr", priv->current_addr, ETH_ALEN);
  577. memcpy(dev->dev_addr, priv->current_addr, ETH_ALEN);
  578. if (priv->mesh_dev)
  579. memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
  580. done:
  581. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  582. return ret;
  583. }
  584. static int lbs_copy_multicast_address(struct lbs_private *priv,
  585. struct net_device *dev)
  586. {
  587. int i = 0;
  588. struct dev_mc_list *mcptr = dev->mc_list;
  589. for (i = 0; i < dev->mc_count; i++) {
  590. memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  591. mcptr = mcptr->next;
  592. }
  593. return i;
  594. }
  595. static void lbs_set_multicast_list(struct net_device *dev)
  596. {
  597. struct lbs_private *priv = dev->priv;
  598. int oldpacketfilter;
  599. DECLARE_MAC_BUF(mac);
  600. lbs_deb_enter(LBS_DEB_NET);
  601. oldpacketfilter = priv->currentpacketfilter;
  602. if (dev->flags & IFF_PROMISC) {
  603. lbs_deb_net("enable promiscuous mode\n");
  604. priv->currentpacketfilter |=
  605. CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  606. priv->currentpacketfilter &=
  607. ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  608. CMD_ACT_MAC_MULTICAST_ENABLE);
  609. } else {
  610. /* Multicast */
  611. priv->currentpacketfilter &=
  612. ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  613. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  614. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  615. lbs_deb_net( "enabling all multicast\n");
  616. priv->currentpacketfilter |=
  617. CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  618. priv->currentpacketfilter &=
  619. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  620. } else {
  621. priv->currentpacketfilter &=
  622. ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  623. if (!dev->mc_count) {
  624. lbs_deb_net("no multicast addresses, "
  625. "disabling multicast\n");
  626. priv->currentpacketfilter &=
  627. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  628. } else {
  629. int i;
  630. priv->currentpacketfilter |=
  631. CMD_ACT_MAC_MULTICAST_ENABLE;
  632. priv->nr_of_multicastmacaddr =
  633. lbs_copy_multicast_address(priv, dev);
  634. lbs_deb_net("multicast addresses: %d\n",
  635. dev->mc_count);
  636. for (i = 0; i < dev->mc_count; i++) {
  637. lbs_deb_net("Multicast address %d:%s\n",
  638. i, print_mac(mac,
  639. priv->multicastlist[i]));
  640. }
  641. /* send multicast addresses to firmware */
  642. lbs_prepare_and_send_command(priv,
  643. CMD_MAC_MULTICAST_ADR,
  644. CMD_ACT_SET, 0, 0,
  645. NULL);
  646. }
  647. }
  648. }
  649. if (priv->currentpacketfilter != oldpacketfilter) {
  650. lbs_set_mac_packet_filter(priv);
  651. }
  652. lbs_deb_leave(LBS_DEB_NET);
  653. }
  654. /**
  655. * @brief This function handles the major jobs in the LBS driver.
  656. * It handles all events generated by firmware, RX data received
  657. * from firmware and TX data sent from kernel.
  658. *
  659. * @param data A pointer to lbs_thread structure
  660. * @return 0
  661. */
  662. static int lbs_thread(void *data)
  663. {
  664. struct net_device *dev = data;
  665. struct lbs_private *priv = dev->priv;
  666. wait_queue_t wait;
  667. u8 ireg = 0;
  668. lbs_deb_enter(LBS_DEB_THREAD);
  669. init_waitqueue_entry(&wait, current);
  670. set_freezable();
  671. for (;;) {
  672. lbs_deb_thread( "main-thread 111: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  673. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  674. add_wait_queue(&priv->waitq, &wait);
  675. set_current_state(TASK_INTERRUPTIBLE);
  676. spin_lock_irq(&priv->driver_lock);
  677. if ((priv->psstate == PS_STATE_SLEEP) ||
  678. (!priv->intcounter && (priv->dnld_sent || priv->cur_cmd || list_empty(&priv->cmdpendingq)))) {
  679. lbs_deb_thread("main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  680. priv->connect_status, priv->intcounter,
  681. priv->psmode, priv->psstate);
  682. spin_unlock_irq(&priv->driver_lock);
  683. schedule();
  684. } else
  685. spin_unlock_irq(&priv->driver_lock);
  686. lbs_deb_thread("main-thread 222 (waking up): intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  687. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  688. set_current_state(TASK_RUNNING);
  689. remove_wait_queue(&priv->waitq, &wait);
  690. try_to_freeze();
  691. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  692. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  693. if (kthread_should_stop() || priv->surpriseremoved) {
  694. lbs_deb_thread("main-thread: break from main thread: surpriseremoved=0x%x\n",
  695. priv->surpriseremoved);
  696. break;
  697. }
  698. spin_lock_irq(&priv->driver_lock);
  699. if (priv->intcounter) {
  700. u8 int_status;
  701. priv->intcounter = 0;
  702. int_status = priv->hw_get_int_status(priv, &ireg);
  703. if (int_status) {
  704. lbs_deb_thread("main-thread: reading HOST_INT_STATUS_REG failed\n");
  705. spin_unlock_irq(&priv->driver_lock);
  706. continue;
  707. }
  708. priv->hisregcpy |= ireg;
  709. }
  710. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  711. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  712. /* command response? */
  713. if (priv->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
  714. lbs_deb_thread("main-thread: cmd response ready\n");
  715. priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
  716. spin_unlock_irq(&priv->driver_lock);
  717. lbs_process_rx_command(priv);
  718. spin_lock_irq(&priv->driver_lock);
  719. }
  720. /* Any Card Event */
  721. if (priv->hisregcpy & MRVDRV_CARDEVENT) {
  722. lbs_deb_thread("main-thread: Card Event Activity\n");
  723. priv->hisregcpy &= ~MRVDRV_CARDEVENT;
  724. if (priv->hw_read_event_cause(priv)) {
  725. lbs_pr_alert("main-thread: hw_read_event_cause failed\n");
  726. spin_unlock_irq(&priv->driver_lock);
  727. continue;
  728. }
  729. spin_unlock_irq(&priv->driver_lock);
  730. lbs_process_event(priv);
  731. } else
  732. spin_unlock_irq(&priv->driver_lock);
  733. /* Check if we need to confirm Sleep Request received previously */
  734. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  735. !priv->dnld_sent && !priv->cur_cmd) {
  736. if (priv->connect_status == LBS_CONNECTED) {
  737. lbs_deb_thread("main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p dnld_sent=%d cur_cmd=%p, confirm now\n",
  738. priv->intcounter, priv->currenttxskb, priv->dnld_sent, priv->cur_cmd);
  739. lbs_ps_confirm_sleep(priv, (u16) priv->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. priv->psstate = PS_STATE_AWAKE;
  747. lbs_pr_alert("main-thread: ignore PS_SleepConfirm in non-connected state\n");
  748. }
  749. }
  750. /* The PS state is changed during processing of Sleep Request
  751. * event above
  752. */
  753. if ((priv->psstate == PS_STATE_SLEEP) ||
  754. (priv->psstate == PS_STATE_PRE_SLEEP))
  755. continue;
  756. /* Execute the next command */
  757. if (!priv->dnld_sent && !priv->cur_cmd)
  758. lbs_execute_next_command(priv);
  759. /* Wake-up command waiters which can't sleep in
  760. * lbs_prepare_and_send_command
  761. */
  762. if (!list_empty(&priv->cmdpendingq))
  763. wake_up_all(&priv->cmd_pending);
  764. lbs_tx_runqueue(priv);
  765. }
  766. del_timer(&priv->command_timer);
  767. wake_up_all(&priv->cmd_pending);
  768. lbs_deb_leave(LBS_DEB_THREAD);
  769. return 0;
  770. }
  771. /**
  772. * @brief This function downloads firmware image, gets
  773. * HW spec from firmware and set basic parameters to
  774. * firmware.
  775. *
  776. * @param priv A pointer to struct lbs_private structure
  777. * @return 0 or -1
  778. */
  779. static int lbs_setup_firmware(struct lbs_private *priv)
  780. {
  781. int ret = -1;
  782. struct cmd_ds_mesh_access mesh_access;
  783. lbs_deb_enter(LBS_DEB_FW);
  784. /*
  785. * Read MAC address from HW
  786. */
  787. memset(priv->current_addr, 0xff, ETH_ALEN);
  788. ret = lbs_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
  789. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  790. if (ret) {
  791. ret = -1;
  792. goto done;
  793. }
  794. lbs_set_mac_packet_filter(priv);
  795. /* Get the supported Data rates */
  796. ret = lbs_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
  797. CMD_ACT_GET_TX_RATE,
  798. CMD_OPTION_WAITFORRSP, 0, NULL);
  799. if (ret) {
  800. ret = -1;
  801. goto done;
  802. }
  803. /* Disable mesh autostart */
  804. if (priv->mesh_dev) {
  805. memset(&mesh_access, 0, sizeof(mesh_access));
  806. mesh_access.data[0] = cpu_to_le32(0);
  807. ret = lbs_prepare_and_send_command(priv,
  808. CMD_MESH_ACCESS,
  809. CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
  810. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  811. if (ret) {
  812. ret = -1;
  813. goto done;
  814. }
  815. priv->mesh_autostart_enabled = 0;
  816. }
  817. ret = 0;
  818. done:
  819. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  820. return ret;
  821. }
  822. /**
  823. * This function handles the timeout of command sending.
  824. * It will re-send the same command again.
  825. */
  826. static void command_timer_fn(unsigned long data)
  827. {
  828. struct lbs_private *priv = (struct lbs_private *)data;
  829. struct cmd_ctrl_node *ptempnode;
  830. struct cmd_ds_command *cmd;
  831. unsigned long flags;
  832. ptempnode = priv->cur_cmd;
  833. if (ptempnode == NULL) {
  834. lbs_deb_fw("ptempnode empty\n");
  835. return;
  836. }
  837. cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
  838. if (!cmd) {
  839. lbs_deb_fw("cmd is NULL\n");
  840. return;
  841. }
  842. lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
  843. if (!priv->fw_ready)
  844. return;
  845. spin_lock_irqsave(&priv->driver_lock, flags);
  846. priv->cur_cmd = NULL;
  847. spin_unlock_irqrestore(&priv->driver_lock, flags);
  848. lbs_deb_fw("re-sending same command because of timeout\n");
  849. lbs_queue_cmd(priv, ptempnode, 0);
  850. wake_up_interruptible(&priv->waitq);
  851. return;
  852. }
  853. static int lbs_init_adapter(struct lbs_private *priv)
  854. {
  855. size_t bufsize;
  856. int i, ret = 0;
  857. /* Allocate buffer to store the BSSID list */
  858. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  859. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  860. if (!priv->networks) {
  861. lbs_pr_err("Out of memory allocating beacons\n");
  862. ret = -1;
  863. goto out;
  864. }
  865. /* Initialize scan result lists */
  866. INIT_LIST_HEAD(&priv->network_free_list);
  867. INIT_LIST_HEAD(&priv->network_list);
  868. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  869. list_add_tail(&priv->networks[i].list,
  870. &priv->network_free_list);
  871. }
  872. priv->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++priv->seqnum);
  873. priv->lbs_ps_confirm_sleep.command =
  874. cpu_to_le16(CMD_802_11_PS_MODE);
  875. priv->lbs_ps_confirm_sleep.size =
  876. cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
  877. priv->lbs_ps_confirm_sleep.action =
  878. cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  879. memset(priv->current_addr, 0xff, ETH_ALEN);
  880. priv->connect_status = LBS_DISCONNECTED;
  881. priv->mesh_connect_status = LBS_DISCONNECTED;
  882. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  883. priv->mode = IW_MODE_INFRA;
  884. priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  885. priv->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  886. priv->radioon = RADIO_ON;
  887. priv->auto_rate = 1;
  888. priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  889. priv->psmode = LBS802_11POWERMODECAM;
  890. priv->psstate = PS_STATE_FULL_POWER;
  891. mutex_init(&priv->lock);
  892. memset(&priv->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
  893. priv->tx_queue_idx = 0;
  894. spin_lock_init(&priv->txqueue_lock);
  895. setup_timer(&priv->command_timer, command_timer_fn,
  896. (unsigned long)priv);
  897. INIT_LIST_HEAD(&priv->cmdfreeq);
  898. INIT_LIST_HEAD(&priv->cmdpendingq);
  899. spin_lock_init(&priv->driver_lock);
  900. init_waitqueue_head(&priv->cmd_pending);
  901. /* Allocate the command buffers */
  902. if (lbs_allocate_cmd_buffer(priv)) {
  903. lbs_pr_err("Out of memory allocating command buffers\n");
  904. ret = -1;
  905. }
  906. out:
  907. return ret;
  908. }
  909. static void lbs_free_adapter(struct lbs_private *priv)
  910. {
  911. lbs_deb_fw("free command buffer\n");
  912. lbs_free_cmd_buffer(priv);
  913. lbs_deb_fw("free command_timer\n");
  914. del_timer(&priv->command_timer);
  915. lbs_deb_fw("free scan results table\n");
  916. kfree(priv->networks);
  917. priv->networks = NULL;
  918. }
  919. /**
  920. * @brief This function adds the card. it will probe the
  921. * card, allocate the lbs_priv and initialize the device.
  922. *
  923. * @param card A pointer to card
  924. * @return A pointer to struct lbs_private structure
  925. */
  926. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  927. {
  928. struct net_device *dev = NULL;
  929. struct lbs_private *priv = NULL;
  930. lbs_deb_enter(LBS_DEB_NET);
  931. /* Allocate an Ethernet device and register it */
  932. dev = alloc_etherdev(sizeof(struct lbs_private));
  933. if (!dev) {
  934. lbs_pr_err("init ethX device failed\n");
  935. goto done;
  936. }
  937. priv = dev->priv;
  938. if (lbs_init_adapter(priv)) {
  939. lbs_pr_err("failed to initialize adapter structure.\n");
  940. goto err_init_adapter;
  941. }
  942. priv->dev = dev;
  943. priv->card = card;
  944. priv->mesh_open = 0;
  945. priv->infra_open = 0;
  946. /* Setup the OS Interface to our functions */
  947. dev->open = lbs_open;
  948. dev->hard_start_xmit = lbs_pre_start_xmit;
  949. dev->stop = lbs_close;
  950. dev->set_mac_address = lbs_set_mac_address;
  951. dev->tx_timeout = lbs_tx_timeout;
  952. dev->get_stats = lbs_get_stats;
  953. dev->watchdog_timeo = 5 * HZ;
  954. dev->ethtool_ops = &lbs_ethtool_ops;
  955. #ifdef WIRELESS_EXT
  956. dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
  957. #endif
  958. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  959. dev->set_multicast_list = lbs_set_multicast_list;
  960. SET_NETDEV_DEV(dev, dmdev);
  961. priv->rtap_net_dev = NULL;
  962. lbs_deb_thread("Starting main thread...\n");
  963. init_waitqueue_head(&priv->waitq);
  964. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  965. if (IS_ERR(priv->main_thread)) {
  966. lbs_deb_thread("Error creating main thread.\n");
  967. goto err_init_adapter;
  968. }
  969. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  970. INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
  971. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  972. INIT_WORK(&priv->sync_channel, lbs_sync_channel);
  973. goto done;
  974. err_init_adapter:
  975. lbs_free_adapter(priv);
  976. free_netdev(dev);
  977. priv = NULL;
  978. done:
  979. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  980. return priv;
  981. }
  982. EXPORT_SYMBOL_GPL(lbs_add_card);
  983. int lbs_remove_card(struct lbs_private *priv)
  984. {
  985. struct net_device *dev = priv->dev;
  986. union iwreq_data wrqu;
  987. lbs_deb_enter(LBS_DEB_MAIN);
  988. lbs_remove_rtap(priv);
  989. dev = priv->dev;
  990. device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
  991. cancel_delayed_work(&priv->scan_work);
  992. cancel_delayed_work(&priv->assoc_work);
  993. destroy_workqueue(priv->work_thread);
  994. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  995. priv->psmode = LBS802_11POWERMODECAM;
  996. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  997. }
  998. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  999. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1000. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1001. /* Stop the thread servicing the interrupts */
  1002. priv->surpriseremoved = 1;
  1003. kthread_stop(priv->main_thread);
  1004. lbs_free_adapter(priv);
  1005. priv->dev = NULL;
  1006. free_netdev(dev);
  1007. lbs_deb_leave(LBS_DEB_MAIN);
  1008. return 0;
  1009. }
  1010. EXPORT_SYMBOL_GPL(lbs_remove_card);
  1011. int lbs_start_card(struct lbs_private *priv)
  1012. {
  1013. struct net_device *dev = priv->dev;
  1014. int ret = -1;
  1015. lbs_deb_enter(LBS_DEB_MAIN);
  1016. /* poke the firmware */
  1017. ret = lbs_setup_firmware(priv);
  1018. if (ret)
  1019. goto done;
  1020. /* init 802.11d */
  1021. lbs_init_11d(priv);
  1022. if (register_netdev(dev)) {
  1023. lbs_pr_err("cannot register ethX device\n");
  1024. goto done;
  1025. }
  1026. if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
  1027. lbs_pr_err("cannot register lbs_rtap attribute\n");
  1028. lbs_debugfs_init_one(priv, dev);
  1029. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  1030. ret = 0;
  1031. done:
  1032. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1033. return ret;
  1034. }
  1035. EXPORT_SYMBOL_GPL(lbs_start_card);
  1036. int lbs_stop_card(struct lbs_private *priv)
  1037. {
  1038. struct net_device *dev = priv->dev;
  1039. int ret = -1;
  1040. struct cmd_ctrl_node *cmdnode;
  1041. unsigned long flags;
  1042. lbs_deb_enter(LBS_DEB_MAIN);
  1043. netif_stop_queue(priv->dev);
  1044. netif_carrier_off(priv->dev);
  1045. lbs_debugfs_remove_one(priv);
  1046. /* Flush pending command nodes */
  1047. spin_lock_irqsave(&priv->driver_lock, flags);
  1048. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  1049. cmdnode->cmdwaitqwoken = 1;
  1050. wake_up_interruptible(&cmdnode->cmdwait_q);
  1051. }
  1052. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1053. unregister_netdev(dev);
  1054. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1055. return ret;
  1056. }
  1057. EXPORT_SYMBOL_GPL(lbs_stop_card);
  1058. /**
  1059. * @brief This function adds mshX interface
  1060. *
  1061. * @param priv A pointer to the struct lbs_private structure
  1062. * @return 0 if successful, -X otherwise
  1063. */
  1064. int lbs_add_mesh(struct lbs_private *priv, struct device *dev)
  1065. {
  1066. struct net_device *mesh_dev = NULL;
  1067. int ret = 0;
  1068. lbs_deb_enter(LBS_DEB_MESH);
  1069. /* Allocate a virtual mesh device */
  1070. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1071. lbs_deb_mesh("init mshX device failed\n");
  1072. ret = -ENOMEM;
  1073. goto done;
  1074. }
  1075. mesh_dev->priv = priv;
  1076. priv->mesh_dev = mesh_dev;
  1077. mesh_dev->open = lbs_mesh_open;
  1078. mesh_dev->hard_start_xmit = lbs_mesh_pre_start_xmit;
  1079. mesh_dev->stop = lbs_mesh_close;
  1080. mesh_dev->get_stats = lbs_get_stats;
  1081. mesh_dev->set_mac_address = lbs_set_mac_address;
  1082. mesh_dev->ethtool_ops = &lbs_ethtool_ops;
  1083. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1084. sizeof(priv->dev->dev_addr));
  1085. SET_NETDEV_DEV(priv->mesh_dev, dev);
  1086. #ifdef WIRELESS_EXT
  1087. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1088. #endif
  1089. /* Register virtual mesh interface */
  1090. ret = register_netdev(mesh_dev);
  1091. if (ret) {
  1092. lbs_pr_err("cannot register mshX virtual interface\n");
  1093. goto err_free;
  1094. }
  1095. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1096. if (ret)
  1097. goto err_unregister;
  1098. /* Everything successful */
  1099. ret = 0;
  1100. goto done;
  1101. err_unregister:
  1102. unregister_netdev(mesh_dev);
  1103. err_free:
  1104. free_netdev(mesh_dev);
  1105. done:
  1106. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1107. return ret;
  1108. }
  1109. EXPORT_SYMBOL_GPL(lbs_add_mesh);
  1110. void lbs_remove_mesh(struct lbs_private *priv)
  1111. {
  1112. struct net_device *mesh_dev;
  1113. lbs_deb_enter(LBS_DEB_MAIN);
  1114. if (!priv)
  1115. goto out;
  1116. mesh_dev = priv->mesh_dev;
  1117. netif_stop_queue(mesh_dev);
  1118. netif_carrier_off(priv->mesh_dev);
  1119. sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1120. unregister_netdev(mesh_dev);
  1121. priv->mesh_dev = NULL ;
  1122. free_netdev(mesh_dev);
  1123. out:
  1124. lbs_deb_leave(LBS_DEB_MAIN);
  1125. }
  1126. EXPORT_SYMBOL_GPL(lbs_remove_mesh);
  1127. /**
  1128. * @brief This function finds the CFP in
  1129. * region_cfp_table based on region and band parameter.
  1130. *
  1131. * @param region The region code
  1132. * @param band The band
  1133. * @param cfp_no A pointer to CFP number
  1134. * @return A pointer to CFP
  1135. */
  1136. struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  1137. {
  1138. int i, end;
  1139. lbs_deb_enter(LBS_DEB_MAIN);
  1140. end = ARRAY_SIZE(region_cfp_table);
  1141. for (i = 0; i < end ; i++) {
  1142. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1143. region_cfp_table[i].region);
  1144. if (region_cfp_table[i].region == region) {
  1145. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1146. lbs_deb_leave(LBS_DEB_MAIN);
  1147. return region_cfp_table[i].cfp_BG;
  1148. }
  1149. }
  1150. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1151. return NULL;
  1152. }
  1153. int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
  1154. {
  1155. int ret = 0;
  1156. int i = 0;
  1157. struct chan_freq_power *cfp;
  1158. int cfp_no;
  1159. lbs_deb_enter(LBS_DEB_MAIN);
  1160. memset(priv->region_channel, 0, sizeof(priv->region_channel));
  1161. {
  1162. cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
  1163. if (cfp != NULL) {
  1164. priv->region_channel[i].nrcfp = cfp_no;
  1165. priv->region_channel[i].CFP = cfp;
  1166. } else {
  1167. lbs_deb_main("wrong region code %#x in band B/G\n",
  1168. region);
  1169. ret = -1;
  1170. goto out;
  1171. }
  1172. priv->region_channel[i].valid = 1;
  1173. priv->region_channel[i].region = region;
  1174. priv->region_channel[i].band = band;
  1175. i++;
  1176. }
  1177. out:
  1178. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1179. return ret;
  1180. }
  1181. /**
  1182. * @brief This function handles the interrupt. it will change PS
  1183. * state if applicable. it will wake up main_thread to handle
  1184. * the interrupt event as well.
  1185. *
  1186. * @param dev A pointer to net_device structure
  1187. * @return n/a
  1188. */
  1189. void lbs_interrupt(struct net_device *dev)
  1190. {
  1191. struct lbs_private *priv = dev->priv;
  1192. lbs_deb_enter(LBS_DEB_THREAD);
  1193. lbs_deb_thread("lbs_interrupt: intcounter=%d\n",
  1194. priv->intcounter);
  1195. priv->intcounter++;
  1196. if (priv->psstate == PS_STATE_SLEEP) {
  1197. priv->psstate = PS_STATE_AWAKE;
  1198. netif_wake_queue(dev);
  1199. if (priv->mesh_dev)
  1200. netif_wake_queue(priv->mesh_dev);
  1201. }
  1202. wake_up_interruptible(&priv->waitq);
  1203. lbs_deb_leave(LBS_DEB_THREAD);
  1204. }
  1205. EXPORT_SYMBOL_GPL(lbs_interrupt);
  1206. int lbs_reset_device(struct lbs_private *priv)
  1207. {
  1208. int ret;
  1209. lbs_deb_enter(LBS_DEB_MAIN);
  1210. ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
  1211. CMD_ACT_HALT, 0, 0, NULL);
  1212. msleep_interruptible(10);
  1213. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1214. return ret;
  1215. }
  1216. EXPORT_SYMBOL_GPL(lbs_reset_device);
  1217. static int __init lbs_init_module(void)
  1218. {
  1219. lbs_deb_enter(LBS_DEB_MAIN);
  1220. lbs_debugfs_init();
  1221. lbs_deb_leave(LBS_DEB_MAIN);
  1222. return 0;
  1223. }
  1224. static void __exit lbs_exit_module(void)
  1225. {
  1226. lbs_deb_enter(LBS_DEB_MAIN);
  1227. lbs_debugfs_remove();
  1228. lbs_deb_leave(LBS_DEB_MAIN);
  1229. }
  1230. /*
  1231. * rtap interface support fuctions
  1232. */
  1233. static int lbs_rtap_open(struct net_device *dev)
  1234. {
  1235. netif_carrier_off(dev);
  1236. netif_stop_queue(dev);
  1237. return 0;
  1238. }
  1239. static int lbs_rtap_stop(struct net_device *dev)
  1240. {
  1241. return 0;
  1242. }
  1243. static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1244. {
  1245. netif_stop_queue(dev);
  1246. return -EOPNOTSUPP;
  1247. }
  1248. static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
  1249. {
  1250. struct lbs_private *priv = dev->priv;
  1251. return &priv->ieee->stats;
  1252. }
  1253. void lbs_remove_rtap(struct lbs_private *priv)
  1254. {
  1255. if (priv->rtap_net_dev == NULL)
  1256. return;
  1257. unregister_netdev(priv->rtap_net_dev);
  1258. free_ieee80211(priv->rtap_net_dev);
  1259. priv->rtap_net_dev = NULL;
  1260. }
  1261. int lbs_add_rtap(struct lbs_private *priv)
  1262. {
  1263. int rc = 0;
  1264. if (priv->rtap_net_dev)
  1265. return -EPERM;
  1266. priv->rtap_net_dev = alloc_ieee80211(0);
  1267. if (priv->rtap_net_dev == NULL)
  1268. return -ENOMEM;
  1269. priv->ieee = netdev_priv(priv->rtap_net_dev);
  1270. strcpy(priv->rtap_net_dev->name, "rtap%d");
  1271. priv->rtap_net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1272. priv->rtap_net_dev->open = lbs_rtap_open;
  1273. priv->rtap_net_dev->stop = lbs_rtap_stop;
  1274. priv->rtap_net_dev->get_stats = lbs_rtap_get_stats;
  1275. priv->rtap_net_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
  1276. priv->rtap_net_dev->set_multicast_list = lbs_set_multicast_list;
  1277. priv->rtap_net_dev->priv = priv;
  1278. priv->ieee->iw_mode = IW_MODE_MONITOR;
  1279. rc = register_netdev(priv->rtap_net_dev);
  1280. if (rc) {
  1281. free_ieee80211(priv->rtap_net_dev);
  1282. priv->rtap_net_dev = NULL;
  1283. return rc;
  1284. }
  1285. return 0;
  1286. }
  1287. module_init(lbs_init_module);
  1288. module_exit(lbs_exit_module);
  1289. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1290. MODULE_AUTHOR("Marvell International Ltd.");
  1291. MODULE_LICENSE("GPL");