main.c 37 KB

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