main.c 41 KB

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