main.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /**
  2. * This file contains the major functions in WLAN
  3. * driver. It includes init, exit, open, close and main
  4. * thread etc..
  5. */
  6. #include <linux/moduleparam.h>
  7. #include <linux/delay.h>
  8. #include <linux/freezer.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/if_arp.h>
  12. #include <linux/kthread.h>
  13. #include <net/iw_handler.h>
  14. #include <net/ieee80211.h>
  15. #include "host.h"
  16. #include "decl.h"
  17. #include "dev.h"
  18. #include "wext.h"
  19. #include "debugfs.h"
  20. #include "assoc.h"
  21. #include "join.h"
  22. #include "cmd.h"
  23. #define DRIVER_RELEASE_VERSION "323.p0"
  24. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  25. #ifdef DEBUG
  26. "-dbg"
  27. #endif
  28. "";
  29. /* Module parameters */
  30. unsigned int lbs_debug;
  31. EXPORT_SYMBOL_GPL(lbs_debug);
  32. module_param_named(libertas_debug, lbs_debug, int, 0644);
  33. #define LBS_TX_PWR_DEFAULT 20 /*100mW */
  34. #define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
  35. #define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
  36. #define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
  37. #define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
  38. /* Format { channel, frequency (MHz), maxtxpower } */
  39. /* band: 'B/G', region: USA FCC/Canada IC */
  40. static struct chan_freq_power channel_freq_power_US_BG[] = {
  41. {1, 2412, LBS_TX_PWR_US_DEFAULT},
  42. {2, 2417, LBS_TX_PWR_US_DEFAULT},
  43. {3, 2422, LBS_TX_PWR_US_DEFAULT},
  44. {4, 2427, LBS_TX_PWR_US_DEFAULT},
  45. {5, 2432, LBS_TX_PWR_US_DEFAULT},
  46. {6, 2437, LBS_TX_PWR_US_DEFAULT},
  47. {7, 2442, LBS_TX_PWR_US_DEFAULT},
  48. {8, 2447, LBS_TX_PWR_US_DEFAULT},
  49. {9, 2452, LBS_TX_PWR_US_DEFAULT},
  50. {10, 2457, LBS_TX_PWR_US_DEFAULT},
  51. {11, 2462, LBS_TX_PWR_US_DEFAULT}
  52. };
  53. /* band: 'B/G', region: Europe ETSI */
  54. static struct chan_freq_power channel_freq_power_EU_BG[] = {
  55. {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
  56. {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
  57. {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
  58. {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
  59. {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
  60. {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
  61. {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
  62. {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
  63. {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
  64. {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
  65. {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
  66. {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
  67. {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
  68. };
  69. /* band: 'B/G', region: Spain */
  70. static struct chan_freq_power channel_freq_power_SPN_BG[] = {
  71. {10, 2457, LBS_TX_PWR_DEFAULT},
  72. {11, 2462, LBS_TX_PWR_DEFAULT}
  73. };
  74. /* band: 'B/G', region: France */
  75. static struct chan_freq_power channel_freq_power_FR_BG[] = {
  76. {10, 2457, LBS_TX_PWR_FR_DEFAULT},
  77. {11, 2462, LBS_TX_PWR_FR_DEFAULT},
  78. {12, 2467, LBS_TX_PWR_FR_DEFAULT},
  79. {13, 2472, LBS_TX_PWR_FR_DEFAULT}
  80. };
  81. /* band: 'B/G', region: Japan */
  82. static struct chan_freq_power channel_freq_power_JPN_BG[] = {
  83. {1, 2412, LBS_TX_PWR_JP_DEFAULT},
  84. {2, 2417, LBS_TX_PWR_JP_DEFAULT},
  85. {3, 2422, LBS_TX_PWR_JP_DEFAULT},
  86. {4, 2427, LBS_TX_PWR_JP_DEFAULT},
  87. {5, 2432, LBS_TX_PWR_JP_DEFAULT},
  88. {6, 2437, LBS_TX_PWR_JP_DEFAULT},
  89. {7, 2442, LBS_TX_PWR_JP_DEFAULT},
  90. {8, 2447, LBS_TX_PWR_JP_DEFAULT},
  91. {9, 2452, LBS_TX_PWR_JP_DEFAULT},
  92. {10, 2457, LBS_TX_PWR_JP_DEFAULT},
  93. {11, 2462, LBS_TX_PWR_JP_DEFAULT},
  94. {12, 2467, LBS_TX_PWR_JP_DEFAULT},
  95. {13, 2472, LBS_TX_PWR_JP_DEFAULT},
  96. {14, 2484, LBS_TX_PWR_JP_DEFAULT}
  97. };
  98. /**
  99. * the structure for channel, frequency and power
  100. */
  101. struct region_cfp_table {
  102. u8 region;
  103. struct chan_freq_power *cfp_BG;
  104. int cfp_no_BG;
  105. };
  106. /**
  107. * the structure for the mapping between region and CFP
  108. */
  109. static struct region_cfp_table region_cfp_table[] = {
  110. {0x10, /*US FCC */
  111. channel_freq_power_US_BG,
  112. ARRAY_SIZE(channel_freq_power_US_BG),
  113. }
  114. ,
  115. {0x20, /*CANADA IC */
  116. channel_freq_power_US_BG,
  117. ARRAY_SIZE(channel_freq_power_US_BG),
  118. }
  119. ,
  120. {0x30, /*EU*/ channel_freq_power_EU_BG,
  121. ARRAY_SIZE(channel_freq_power_EU_BG),
  122. }
  123. ,
  124. {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
  125. ARRAY_SIZE(channel_freq_power_SPN_BG),
  126. }
  127. ,
  128. {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
  129. ARRAY_SIZE(channel_freq_power_FR_BG),
  130. }
  131. ,
  132. {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
  133. ARRAY_SIZE(channel_freq_power_JPN_BG),
  134. }
  135. ,
  136. /*Add new region here */
  137. };
  138. /**
  139. * the table to keep region code
  140. */
  141. u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  142. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  143. /**
  144. * 802.11b/g supported bitrates (in 500Kb/s units)
  145. */
  146. u8 lbs_bg_rates[MAX_RATES] =
  147. { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  148. 0x00, 0x00 };
  149. /**
  150. * FW rate table. FW refers to rates by their index in this table, not by the
  151. * rate value itself. Values of 0x00 are
  152. * reserved positions.
  153. */
  154. static u8 fw_data_rates[MAX_RATES] =
  155. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  156. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  157. };
  158. /**
  159. * @brief use index to get the data rate
  160. *
  161. * @param idx The index of data rate
  162. * @return data rate or 0
  163. */
  164. u32 lbs_fw_index_to_data_rate(u8 idx)
  165. {
  166. if (idx >= sizeof(fw_data_rates))
  167. idx = 0;
  168. return fw_data_rates[idx];
  169. }
  170. /**
  171. * @brief use rate to get the index
  172. *
  173. * @param rate data rate
  174. * @return index or 0
  175. */
  176. u8 lbs_data_rate_to_fw_index(u32 rate)
  177. {
  178. u8 i;
  179. if (!rate)
  180. return 0;
  181. for (i = 0; i < sizeof(fw_data_rates); i++) {
  182. if (rate == fw_data_rates[i])
  183. return i;
  184. }
  185. return 0;
  186. }
  187. /**
  188. * Attributes exported through sysfs
  189. */
  190. /**
  191. * @brief Get function for sysfs attribute anycast_mask
  192. */
  193. static ssize_t lbs_anycast_get(struct device *dev,
  194. struct device_attribute *attr, char * buf)
  195. {
  196. struct lbs_private *priv = to_net_dev(dev)->priv;
  197. struct cmd_ds_mesh_access mesh_access;
  198. int ret;
  199. memset(&mesh_access, 0, sizeof(mesh_access));
  200. ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
  201. if (ret)
  202. return ret;
  203. return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
  204. }
  205. /**
  206. * @brief Set function for sysfs attribute anycast_mask
  207. */
  208. static ssize_t lbs_anycast_set(struct device *dev,
  209. struct device_attribute *attr, const char * buf, size_t count)
  210. {
  211. struct lbs_private *priv = to_net_dev(dev)->priv;
  212. struct cmd_ds_mesh_access mesh_access;
  213. uint32_t datum;
  214. int ret;
  215. memset(&mesh_access, 0, sizeof(mesh_access));
  216. sscanf(buf, "%x", &datum);
  217. mesh_access.data[0] = cpu_to_le32(datum);
  218. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
  219. if (ret)
  220. return ret;
  221. return strlen(buf);
  222. }
  223. static int lbs_add_rtap(struct lbs_private *priv);
  224. static void lbs_remove_rtap(struct lbs_private *priv);
  225. static int lbs_add_mesh(struct lbs_private *priv);
  226. static void lbs_remove_mesh(struct lbs_private *priv);
  227. /**
  228. * Get function for sysfs attribute rtap
  229. */
  230. static ssize_t lbs_rtap_get(struct device *dev,
  231. struct device_attribute *attr, char * buf)
  232. {
  233. struct lbs_private *priv = to_net_dev(dev)->priv;
  234. return snprintf(buf, 5, "0x%X\n", priv->monitormode);
  235. }
  236. /**
  237. * Set function for sysfs attribute rtap
  238. */
  239. static ssize_t lbs_rtap_set(struct device *dev,
  240. struct device_attribute *attr, const char * buf, size_t count)
  241. {
  242. int monitor_mode;
  243. struct lbs_private *priv = to_net_dev(dev)->priv;
  244. sscanf(buf, "%x", &monitor_mode);
  245. if (monitor_mode != LBS_MONITOR_OFF) {
  246. if(priv->monitormode == monitor_mode)
  247. return strlen(buf);
  248. if (priv->monitormode == LBS_MONITOR_OFF) {
  249. if (priv->infra_open || priv->mesh_open)
  250. return -EBUSY;
  251. if (priv->mode == IW_MODE_INFRA)
  252. lbs_send_deauthentication(priv);
  253. else if (priv->mode == IW_MODE_ADHOC)
  254. lbs_stop_adhoc_network(priv);
  255. lbs_add_rtap(priv);
  256. }
  257. priv->monitormode = monitor_mode;
  258. }
  259. else {
  260. if (priv->monitormode == LBS_MONITOR_OFF)
  261. return strlen(buf);
  262. priv->monitormode = LBS_MONITOR_OFF;
  263. lbs_remove_rtap(priv);
  264. if (priv->currenttxskb) {
  265. dev_kfree_skb_any(priv->currenttxskb);
  266. priv->currenttxskb = NULL;
  267. }
  268. /* Wake queues, command thread, etc. */
  269. lbs_host_to_card_done(priv);
  270. }
  271. lbs_prepare_and_send_command(priv,
  272. CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
  273. CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
  274. return strlen(buf);
  275. }
  276. /**
  277. * lbs_rtap attribute to be exported per ethX interface
  278. * through sysfs (/sys/class/net/ethX/lbs_rtap)
  279. */
  280. static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
  281. /**
  282. * Get function for sysfs attribute mesh
  283. */
  284. static ssize_t lbs_mesh_get(struct device *dev,
  285. struct device_attribute *attr, char * buf)
  286. {
  287. struct lbs_private *priv = to_net_dev(dev)->priv;
  288. return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
  289. }
  290. /**
  291. * Set function for sysfs attribute mesh
  292. */
  293. static ssize_t lbs_mesh_set(struct device *dev,
  294. struct device_attribute *attr, const char * buf, size_t count)
  295. {
  296. struct lbs_private *priv = to_net_dev(dev)->priv;
  297. int enable;
  298. int ret;
  299. sscanf(buf, "%x", &enable);
  300. enable = !!enable;
  301. if (enable == !!priv->mesh_dev)
  302. return count;
  303. ret = lbs_mesh_config(priv, enable);
  304. if (ret)
  305. return ret;
  306. if (enable)
  307. lbs_add_mesh(priv);
  308. else
  309. lbs_remove_mesh(priv);
  310. return count;
  311. }
  312. /**
  313. * lbs_mesh attribute to be exported per ethX interface
  314. * through sysfs (/sys/class/net/ethX/lbs_mesh)
  315. */
  316. static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
  317. /**
  318. * anycast_mask attribute to be exported per mshX interface
  319. * through sysfs (/sys/class/net/mshX/anycast_mask)
  320. */
  321. static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
  322. static struct attribute *lbs_mesh_sysfs_entries[] = {
  323. &dev_attr_anycast_mask.attr,
  324. NULL,
  325. };
  326. static struct attribute_group lbs_mesh_attr_group = {
  327. .attrs = lbs_mesh_sysfs_entries,
  328. };
  329. /**
  330. * @brief This function opens the ethX or mshX interface
  331. *
  332. * @param dev A pointer to net_device structure
  333. * @return 0 or -EBUSY if monitor mode active
  334. */
  335. static int lbs_dev_open(struct net_device *dev)
  336. {
  337. struct lbs_private *priv = (struct lbs_private *) dev->priv ;
  338. int ret = 0;
  339. spin_lock_irq(&priv->driver_lock);
  340. if (priv->monitormode != LBS_MONITOR_OFF) {
  341. ret = -EBUSY;
  342. goto out;
  343. }
  344. if (dev == priv->mesh_dev) {
  345. priv->mesh_open = 1;
  346. priv->mesh_connect_status = LBS_CONNECTED;
  347. netif_carrier_on(dev);
  348. } else {
  349. priv->infra_open = 1;
  350. if (priv->connect_status == LBS_CONNECTED)
  351. netif_carrier_on(dev);
  352. else
  353. netif_carrier_off(dev);
  354. }
  355. if (!priv->tx_pending_len)
  356. netif_wake_queue(dev);
  357. out:
  358. spin_unlock_irq(&priv->driver_lock);
  359. return ret;
  360. }
  361. /**
  362. * @brief This function closes the mshX interface
  363. *
  364. * @param dev A pointer to net_device structure
  365. * @return 0
  366. */
  367. static int lbs_mesh_stop(struct net_device *dev)
  368. {
  369. struct lbs_private *priv = (struct lbs_private *) (dev->priv);
  370. spin_lock_irq(&priv->driver_lock);
  371. priv->mesh_open = 0;
  372. priv->mesh_connect_status = LBS_DISCONNECTED;
  373. netif_stop_queue(dev);
  374. netif_carrier_off(dev);
  375. spin_unlock_irq(&priv->driver_lock);
  376. return 0;
  377. }
  378. /**
  379. * @brief This function closes the ethX interface
  380. *
  381. * @param dev A pointer to net_device structure
  382. * @return 0
  383. */
  384. static int lbs_eth_stop(struct net_device *dev)
  385. {
  386. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  387. spin_lock_irq(&priv->driver_lock);
  388. priv->infra_open = 0;
  389. netif_stop_queue(dev);
  390. spin_unlock_irq(&priv->driver_lock);
  391. return 0;
  392. }
  393. static void lbs_tx_timeout(struct net_device *dev)
  394. {
  395. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  396. lbs_deb_enter(LBS_DEB_TX);
  397. lbs_pr_err("tx watch dog timeout\n");
  398. dev->trans_start = jiffies;
  399. if (priv->currenttxskb) {
  400. priv->eventcause = 0x01000000;
  401. lbs_send_tx_feedback(priv);
  402. }
  403. /* XX: Shouldn't we also call into the hw-specific driver
  404. to kick it somehow? */
  405. lbs_host_to_card_done(priv);
  406. lbs_deb_leave(LBS_DEB_TX);
  407. }
  408. void lbs_host_to_card_done(struct lbs_private *priv)
  409. {
  410. unsigned long flags;
  411. spin_lock_irqsave(&priv->driver_lock, flags);
  412. priv->dnld_sent = DNLD_RES_RECEIVED;
  413. /* Wake main thread if commands are pending */
  414. if (!priv->cur_cmd || priv->tx_pending_len > 0)
  415. wake_up_interruptible(&priv->waitq);
  416. spin_unlock_irqrestore(&priv->driver_lock, flags);
  417. }
  418. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  419. /**
  420. * @brief This function returns the network statistics
  421. *
  422. * @param dev A pointer to struct lbs_private structure
  423. * @return A pointer to net_device_stats structure
  424. */
  425. static struct net_device_stats *lbs_get_stats(struct net_device *dev)
  426. {
  427. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  428. return &priv->stats;
  429. }
  430. static int lbs_set_mac_address(struct net_device *dev, void *addr)
  431. {
  432. int ret = 0;
  433. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  434. struct sockaddr *phwaddr = addr;
  435. lbs_deb_enter(LBS_DEB_NET);
  436. /* In case it was called from the mesh device */
  437. dev = priv->dev ;
  438. memset(priv->current_addr, 0, ETH_ALEN);
  439. /* dev->dev_addr is 8 bytes */
  440. lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
  441. lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
  442. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  443. ret = lbs_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
  444. CMD_ACT_SET,
  445. CMD_OPTION_WAITFORRSP, 0, NULL);
  446. if (ret) {
  447. lbs_deb_net("set MAC address failed\n");
  448. ret = -1;
  449. goto done;
  450. }
  451. lbs_deb_hex(LBS_DEB_NET, "priv->macaddr", priv->current_addr, ETH_ALEN);
  452. memcpy(dev->dev_addr, priv->current_addr, ETH_ALEN);
  453. if (priv->mesh_dev)
  454. memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
  455. done:
  456. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  457. return ret;
  458. }
  459. static int lbs_copy_multicast_address(struct lbs_private *priv,
  460. struct net_device *dev)
  461. {
  462. int i = 0;
  463. struct dev_mc_list *mcptr = dev->mc_list;
  464. for (i = 0; i < dev->mc_count; i++) {
  465. memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  466. mcptr = mcptr->next;
  467. }
  468. return i;
  469. }
  470. static void lbs_set_multicast_list(struct net_device *dev)
  471. {
  472. struct lbs_private *priv = dev->priv;
  473. int oldpacketfilter;
  474. DECLARE_MAC_BUF(mac);
  475. lbs_deb_enter(LBS_DEB_NET);
  476. oldpacketfilter = priv->currentpacketfilter;
  477. if (dev->flags & IFF_PROMISC) {
  478. lbs_deb_net("enable promiscuous mode\n");
  479. priv->currentpacketfilter |=
  480. CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  481. priv->currentpacketfilter &=
  482. ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  483. CMD_ACT_MAC_MULTICAST_ENABLE);
  484. } else {
  485. /* Multicast */
  486. priv->currentpacketfilter &=
  487. ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  488. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  489. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  490. lbs_deb_net( "enabling all multicast\n");
  491. priv->currentpacketfilter |=
  492. CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  493. priv->currentpacketfilter &=
  494. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  495. } else {
  496. priv->currentpacketfilter &=
  497. ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  498. if (!dev->mc_count) {
  499. lbs_deb_net("no multicast addresses, "
  500. "disabling multicast\n");
  501. priv->currentpacketfilter &=
  502. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  503. } else {
  504. int i;
  505. priv->currentpacketfilter |=
  506. CMD_ACT_MAC_MULTICAST_ENABLE;
  507. priv->nr_of_multicastmacaddr =
  508. lbs_copy_multicast_address(priv, dev);
  509. lbs_deb_net("multicast addresses: %d\n",
  510. dev->mc_count);
  511. for (i = 0; i < dev->mc_count; i++) {
  512. lbs_deb_net("Multicast address %d:%s\n",
  513. i, print_mac(mac,
  514. priv->multicastlist[i]));
  515. }
  516. /* send multicast addresses to firmware */
  517. lbs_prepare_and_send_command(priv,
  518. CMD_MAC_MULTICAST_ADR,
  519. CMD_ACT_SET, 0, 0,
  520. NULL);
  521. }
  522. }
  523. }
  524. if (priv->currentpacketfilter != oldpacketfilter) {
  525. lbs_set_mac_packet_filter(priv);
  526. }
  527. lbs_deb_leave(LBS_DEB_NET);
  528. }
  529. /**
  530. * @brief This function handles the major jobs in the LBS driver.
  531. * It handles all events generated by firmware, RX data received
  532. * from firmware and TX data sent from kernel.
  533. *
  534. * @param data A pointer to lbs_thread structure
  535. * @return 0
  536. */
  537. static int lbs_thread(void *data)
  538. {
  539. struct net_device *dev = data;
  540. struct lbs_private *priv = dev->priv;
  541. wait_queue_t wait;
  542. u8 ireg = 0;
  543. lbs_deb_enter(LBS_DEB_THREAD);
  544. init_waitqueue_entry(&wait, current);
  545. set_freezable();
  546. for (;;) {
  547. int shouldsleep;
  548. lbs_deb_thread( "main-thread 111: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  549. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  550. add_wait_queue(&priv->waitq, &wait);
  551. set_current_state(TASK_INTERRUPTIBLE);
  552. spin_lock_irq(&priv->driver_lock);
  553. if (priv->surpriseremoved)
  554. shouldsleep = 0; /* Bye */
  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. try_to_freeze();
  584. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  585. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  586. if (kthread_should_stop() || priv->surpriseremoved) {
  587. lbs_deb_thread("main-thread: break from main thread: surpriseremoved=0x%x\n",
  588. priv->surpriseremoved);
  589. break;
  590. }
  591. spin_lock_irq(&priv->driver_lock);
  592. if (priv->intcounter) {
  593. u8 int_status;
  594. priv->intcounter = 0;
  595. int_status = priv->hw_get_int_status(priv, &ireg);
  596. if (int_status) {
  597. lbs_deb_thread("main-thread: reading HOST_INT_STATUS_REG failed\n");
  598. spin_unlock_irq(&priv->driver_lock);
  599. continue;
  600. }
  601. priv->hisregcpy |= ireg;
  602. }
  603. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  604. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  605. /* command response? */
  606. if (priv->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
  607. lbs_deb_thread("main-thread: cmd response ready\n");
  608. priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
  609. spin_unlock_irq(&priv->driver_lock);
  610. lbs_process_rx_command(priv);
  611. spin_lock_irq(&priv->driver_lock);
  612. }
  613. /* Any Card Event */
  614. if (priv->hisregcpy & MRVDRV_CARDEVENT) {
  615. lbs_deb_thread("main-thread: Card Event Activity\n");
  616. priv->hisregcpy &= ~MRVDRV_CARDEVENT;
  617. if (priv->hw_read_event_cause(priv)) {
  618. lbs_pr_alert("main-thread: hw_read_event_cause failed\n");
  619. spin_unlock_irq(&priv->driver_lock);
  620. continue;
  621. }
  622. spin_unlock_irq(&priv->driver_lock);
  623. lbs_process_event(priv);
  624. } else
  625. spin_unlock_irq(&priv->driver_lock);
  626. if (!priv->fw_ready)
  627. continue;
  628. /* Check if we need to confirm Sleep Request received previously */
  629. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  630. !priv->dnld_sent && !priv->cur_cmd) {
  631. if (priv->connect_status == LBS_CONNECTED) {
  632. lbs_deb_thread("main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p dnld_sent=%d cur_cmd=%p, confirm now\n",
  633. priv->intcounter, priv->currenttxskb, priv->dnld_sent, priv->cur_cmd);
  634. lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
  635. } else {
  636. /* workaround for firmware sending
  637. * deauth/linkloss event immediately
  638. * after sleep request; remove this
  639. * after firmware fixes it
  640. */
  641. priv->psstate = PS_STATE_AWAKE;
  642. lbs_pr_alert("main-thread: ignore PS_SleepConfirm in non-connected state\n");
  643. }
  644. }
  645. /* The PS state is changed during processing of Sleep Request
  646. * event above
  647. */
  648. if ((priv->psstate == PS_STATE_SLEEP) ||
  649. (priv->psstate == PS_STATE_PRE_SLEEP))
  650. continue;
  651. /* Execute the next command */
  652. if (!priv->dnld_sent && !priv->cur_cmd)
  653. lbs_execute_next_command(priv);
  654. /* Wake-up command waiters which can't sleep in
  655. * lbs_prepare_and_send_command
  656. */
  657. if (!list_empty(&priv->cmdpendingq))
  658. wake_up_all(&priv->cmd_pending);
  659. spin_lock_irq(&priv->driver_lock);
  660. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  661. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  662. priv->tx_pending_buf,
  663. priv->tx_pending_len);
  664. if (ret) {
  665. lbs_deb_tx("host_to_card failed %d\n", ret);
  666. priv->dnld_sent = DNLD_RES_RECEIVED;
  667. }
  668. priv->tx_pending_len = 0;
  669. if (!priv->currenttxskb) {
  670. /* We can wake the queues immediately if we aren't
  671. waiting for TX feedback */
  672. if (priv->connect_status == LBS_CONNECTED)
  673. netif_wake_queue(priv->dev);
  674. if (priv->mesh_dev &&
  675. priv->mesh_connect_status == LBS_CONNECTED)
  676. netif_wake_queue(priv->mesh_dev);
  677. }
  678. }
  679. spin_unlock_irq(&priv->driver_lock);
  680. }
  681. del_timer(&priv->command_timer);
  682. wake_up_all(&priv->cmd_pending);
  683. lbs_deb_leave(LBS_DEB_THREAD);
  684. return 0;
  685. }
  686. /**
  687. * @brief This function downloads firmware image, gets
  688. * HW spec from firmware and set basic parameters to
  689. * firmware.
  690. *
  691. * @param priv A pointer to struct lbs_private structure
  692. * @return 0 or -1
  693. */
  694. static int lbs_setup_firmware(struct lbs_private *priv)
  695. {
  696. int ret = -1;
  697. lbs_deb_enter(LBS_DEB_FW);
  698. /*
  699. * Read MAC address from HW
  700. */
  701. memset(priv->current_addr, 0xff, ETH_ALEN);
  702. ret = lbs_update_hw_spec(priv);
  703. if (ret) {
  704. ret = -1;
  705. goto done;
  706. }
  707. lbs_set_mac_packet_filter(priv);
  708. ret = lbs_get_data_rate(priv);
  709. if (ret < 0) {
  710. ret = -1;
  711. goto done;
  712. }
  713. ret = 0;
  714. done:
  715. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  716. return ret;
  717. }
  718. /**
  719. * This function handles the timeout of command sending.
  720. * It will re-send the same command again.
  721. */
  722. static void command_timer_fn(unsigned long data)
  723. {
  724. struct lbs_private *priv = (struct lbs_private *)data;
  725. struct cmd_ctrl_node *node;
  726. unsigned long flags;
  727. node = priv->cur_cmd;
  728. if (node == NULL) {
  729. lbs_deb_fw("ptempnode empty\n");
  730. return;
  731. }
  732. if (!node->cmdbuf) {
  733. lbs_deb_fw("cmd is NULL\n");
  734. return;
  735. }
  736. lbs_pr_info("command %x timed out\n", le16_to_cpu(node->cmdbuf->command));
  737. if (!priv->fw_ready)
  738. return;
  739. spin_lock_irqsave(&priv->driver_lock, flags);
  740. priv->cur_cmd = NULL;
  741. spin_unlock_irqrestore(&priv->driver_lock, flags);
  742. lbs_deb_fw("re-sending same command because of timeout\n");
  743. lbs_queue_cmd(priv, node, 0);
  744. wake_up_interruptible(&priv->waitq);
  745. return;
  746. }
  747. static int lbs_init_adapter(struct lbs_private *priv)
  748. {
  749. size_t bufsize;
  750. int i, ret = 0;
  751. /* Allocate buffer to store the BSSID list */
  752. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  753. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  754. if (!priv->networks) {
  755. lbs_pr_err("Out of memory allocating beacons\n");
  756. ret = -1;
  757. goto out;
  758. }
  759. /* Initialize scan result lists */
  760. INIT_LIST_HEAD(&priv->network_free_list);
  761. INIT_LIST_HEAD(&priv->network_list);
  762. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  763. list_add_tail(&priv->networks[i].list,
  764. &priv->network_free_list);
  765. }
  766. priv->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++priv->seqnum);
  767. priv->lbs_ps_confirm_sleep.command =
  768. cpu_to_le16(CMD_802_11_PS_MODE);
  769. priv->lbs_ps_confirm_sleep.size =
  770. cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
  771. priv->lbs_ps_confirm_sleep.action =
  772. cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  773. memset(priv->current_addr, 0xff, ETH_ALEN);
  774. priv->connect_status = LBS_DISCONNECTED;
  775. priv->mesh_connect_status = LBS_DISCONNECTED;
  776. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  777. priv->mode = IW_MODE_INFRA;
  778. priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  779. priv->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  780. priv->radioon = RADIO_ON;
  781. priv->auto_rate = 1;
  782. priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  783. priv->psmode = LBS802_11POWERMODECAM;
  784. priv->psstate = PS_STATE_FULL_POWER;
  785. mutex_init(&priv->lock);
  786. setup_timer(&priv->command_timer, command_timer_fn,
  787. (unsigned long)priv);
  788. INIT_LIST_HEAD(&priv->cmdfreeq);
  789. INIT_LIST_HEAD(&priv->cmdpendingq);
  790. spin_lock_init(&priv->driver_lock);
  791. init_waitqueue_head(&priv->cmd_pending);
  792. /* Allocate the command buffers */
  793. if (lbs_allocate_cmd_buffer(priv)) {
  794. lbs_pr_err("Out of memory allocating command buffers\n");
  795. ret = -1;
  796. }
  797. out:
  798. return ret;
  799. }
  800. static void lbs_free_adapter(struct lbs_private *priv)
  801. {
  802. lbs_deb_fw("free command buffer\n");
  803. lbs_free_cmd_buffer(priv);
  804. lbs_deb_fw("free command_timer\n");
  805. del_timer(&priv->command_timer);
  806. lbs_deb_fw("free scan results table\n");
  807. kfree(priv->networks);
  808. priv->networks = NULL;
  809. }
  810. /**
  811. * @brief This function adds the card. it will probe the
  812. * card, allocate the lbs_priv and initialize the device.
  813. *
  814. * @param card A pointer to card
  815. * @return A pointer to struct lbs_private structure
  816. */
  817. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  818. {
  819. struct net_device *dev = NULL;
  820. struct lbs_private *priv = NULL;
  821. lbs_deb_enter(LBS_DEB_NET);
  822. /* Allocate an Ethernet device and register it */
  823. dev = alloc_etherdev(sizeof(struct lbs_private));
  824. if (!dev) {
  825. lbs_pr_err("init ethX device failed\n");
  826. goto done;
  827. }
  828. priv = dev->priv;
  829. if (lbs_init_adapter(priv)) {
  830. lbs_pr_err("failed to initialize adapter structure.\n");
  831. goto err_init_adapter;
  832. }
  833. priv->dev = dev;
  834. priv->card = card;
  835. priv->mesh_open = 0;
  836. priv->infra_open = 0;
  837. /* Setup the OS Interface to our functions */
  838. dev->open = lbs_dev_open;
  839. dev->hard_start_xmit = lbs_hard_start_xmit;
  840. dev->stop = lbs_eth_stop;
  841. dev->set_mac_address = lbs_set_mac_address;
  842. dev->tx_timeout = lbs_tx_timeout;
  843. dev->get_stats = lbs_get_stats;
  844. dev->watchdog_timeo = 5 * HZ;
  845. dev->ethtool_ops = &lbs_ethtool_ops;
  846. #ifdef WIRELESS_EXT
  847. dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
  848. #endif
  849. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  850. dev->set_multicast_list = lbs_set_multicast_list;
  851. SET_NETDEV_DEV(dev, dmdev);
  852. priv->rtap_net_dev = NULL;
  853. lbs_deb_thread("Starting main thread...\n");
  854. init_waitqueue_head(&priv->waitq);
  855. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  856. if (IS_ERR(priv->main_thread)) {
  857. lbs_deb_thread("Error creating main thread.\n");
  858. goto err_init_adapter;
  859. }
  860. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  861. INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
  862. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  863. INIT_WORK(&priv->sync_channel, lbs_sync_channel);
  864. sprintf(priv->mesh_ssid, "mesh");
  865. priv->mesh_ssid_len = 4;
  866. goto done;
  867. err_init_adapter:
  868. lbs_free_adapter(priv);
  869. free_netdev(dev);
  870. priv = NULL;
  871. done:
  872. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  873. return priv;
  874. }
  875. EXPORT_SYMBOL_GPL(lbs_add_card);
  876. int lbs_remove_card(struct lbs_private *priv)
  877. {
  878. struct net_device *dev = priv->dev;
  879. union iwreq_data wrqu;
  880. lbs_deb_enter(LBS_DEB_MAIN);
  881. lbs_remove_mesh(priv);
  882. lbs_remove_rtap(priv);
  883. dev = priv->dev;
  884. cancel_delayed_work(&priv->scan_work);
  885. cancel_delayed_work(&priv->assoc_work);
  886. destroy_workqueue(priv->work_thread);
  887. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  888. priv->psmode = LBS802_11POWERMODECAM;
  889. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  890. }
  891. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  892. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  893. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  894. /* Stop the thread servicing the interrupts */
  895. priv->surpriseremoved = 1;
  896. kthread_stop(priv->main_thread);
  897. lbs_free_adapter(priv);
  898. priv->dev = NULL;
  899. free_netdev(dev);
  900. lbs_deb_leave(LBS_DEB_MAIN);
  901. return 0;
  902. }
  903. EXPORT_SYMBOL_GPL(lbs_remove_card);
  904. int lbs_start_card(struct lbs_private *priv)
  905. {
  906. struct net_device *dev = priv->dev;
  907. int ret = -1;
  908. lbs_deb_enter(LBS_DEB_MAIN);
  909. /* poke the firmware */
  910. ret = lbs_setup_firmware(priv);
  911. if (ret)
  912. goto done;
  913. /* init 802.11d */
  914. lbs_init_11d(priv);
  915. if (register_netdev(dev)) {
  916. lbs_pr_err("cannot register ethX device\n");
  917. goto done;
  918. }
  919. if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
  920. lbs_pr_err("cannot register lbs_rtap attribute\n");
  921. if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
  922. lbs_pr_err("cannot register lbs_mesh attribute\n");
  923. lbs_debugfs_init_one(priv, dev);
  924. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  925. ret = 0;
  926. done:
  927. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  928. return ret;
  929. }
  930. EXPORT_SYMBOL_GPL(lbs_start_card);
  931. int lbs_stop_card(struct lbs_private *priv)
  932. {
  933. struct net_device *dev = priv->dev;
  934. int ret = -1;
  935. struct cmd_ctrl_node *cmdnode;
  936. unsigned long flags;
  937. lbs_deb_enter(LBS_DEB_MAIN);
  938. netif_stop_queue(priv->dev);
  939. netif_carrier_off(priv->dev);
  940. lbs_debugfs_remove_one(priv);
  941. device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
  942. device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
  943. /* Flush pending command nodes */
  944. spin_lock_irqsave(&priv->driver_lock, flags);
  945. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  946. cmdnode->cmdwaitqwoken = 1;
  947. wake_up_interruptible(&cmdnode->cmdwait_q);
  948. }
  949. spin_unlock_irqrestore(&priv->driver_lock, flags);
  950. unregister_netdev(dev);
  951. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  952. return ret;
  953. }
  954. EXPORT_SYMBOL_GPL(lbs_stop_card);
  955. /**
  956. * @brief This function adds mshX interface
  957. *
  958. * @param priv A pointer to the struct lbs_private structure
  959. * @return 0 if successful, -X otherwise
  960. */
  961. static int lbs_add_mesh(struct lbs_private *priv)
  962. {
  963. struct net_device *mesh_dev = NULL;
  964. int ret = 0;
  965. lbs_deb_enter(LBS_DEB_MESH);
  966. /* Allocate a virtual mesh device */
  967. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  968. lbs_deb_mesh("init mshX device failed\n");
  969. ret = -ENOMEM;
  970. goto done;
  971. }
  972. mesh_dev->priv = priv;
  973. priv->mesh_dev = mesh_dev;
  974. mesh_dev->open = lbs_dev_open;
  975. mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
  976. mesh_dev->stop = lbs_mesh_stop;
  977. mesh_dev->get_stats = lbs_get_stats;
  978. mesh_dev->set_mac_address = lbs_set_mac_address;
  979. mesh_dev->ethtool_ops = &lbs_ethtool_ops;
  980. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  981. sizeof(priv->dev->dev_addr));
  982. SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
  983. #ifdef WIRELESS_EXT
  984. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  985. #endif
  986. /* Register virtual mesh interface */
  987. ret = register_netdev(mesh_dev);
  988. if (ret) {
  989. lbs_pr_err("cannot register mshX virtual interface\n");
  990. goto err_free;
  991. }
  992. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  993. if (ret)
  994. goto err_unregister;
  995. /* Everything successful */
  996. ret = 0;
  997. goto done;
  998. err_unregister:
  999. unregister_netdev(mesh_dev);
  1000. err_free:
  1001. free_netdev(mesh_dev);
  1002. done:
  1003. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1004. return ret;
  1005. }
  1006. EXPORT_SYMBOL_GPL(lbs_add_mesh);
  1007. static void lbs_remove_mesh(struct lbs_private *priv)
  1008. {
  1009. struct net_device *mesh_dev;
  1010. lbs_deb_enter(LBS_DEB_MAIN);
  1011. if (!priv)
  1012. goto out;
  1013. mesh_dev = priv->mesh_dev;
  1014. if (!mesh_dev)
  1015. goto out;
  1016. netif_stop_queue(mesh_dev);
  1017. netif_carrier_off(priv->mesh_dev);
  1018. sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1019. unregister_netdev(mesh_dev);
  1020. priv->mesh_dev = NULL;
  1021. free_netdev(mesh_dev);
  1022. out:
  1023. lbs_deb_leave(LBS_DEB_MAIN);
  1024. }
  1025. EXPORT_SYMBOL_GPL(lbs_remove_mesh);
  1026. /**
  1027. * @brief This function finds the CFP in
  1028. * region_cfp_table based on region and band parameter.
  1029. *
  1030. * @param region The region code
  1031. * @param band The band
  1032. * @param cfp_no A pointer to CFP number
  1033. * @return A pointer to CFP
  1034. */
  1035. struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  1036. {
  1037. int i, end;
  1038. lbs_deb_enter(LBS_DEB_MAIN);
  1039. end = ARRAY_SIZE(region_cfp_table);
  1040. for (i = 0; i < end ; i++) {
  1041. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1042. region_cfp_table[i].region);
  1043. if (region_cfp_table[i].region == region) {
  1044. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1045. lbs_deb_leave(LBS_DEB_MAIN);
  1046. return region_cfp_table[i].cfp_BG;
  1047. }
  1048. }
  1049. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1050. return NULL;
  1051. }
  1052. int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
  1053. {
  1054. int ret = 0;
  1055. int i = 0;
  1056. struct chan_freq_power *cfp;
  1057. int cfp_no;
  1058. lbs_deb_enter(LBS_DEB_MAIN);
  1059. memset(priv->region_channel, 0, sizeof(priv->region_channel));
  1060. {
  1061. cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
  1062. if (cfp != NULL) {
  1063. priv->region_channel[i].nrcfp = cfp_no;
  1064. priv->region_channel[i].CFP = cfp;
  1065. } else {
  1066. lbs_deb_main("wrong region code %#x in band B/G\n",
  1067. region);
  1068. ret = -1;
  1069. goto out;
  1070. }
  1071. priv->region_channel[i].valid = 1;
  1072. priv->region_channel[i].region = region;
  1073. priv->region_channel[i].band = band;
  1074. i++;
  1075. }
  1076. out:
  1077. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1078. return ret;
  1079. }
  1080. /**
  1081. * @brief This function handles the interrupt. it will change PS
  1082. * state if applicable. it will wake up main_thread to handle
  1083. * the interrupt event as well.
  1084. *
  1085. * @param dev A pointer to net_device structure
  1086. * @return n/a
  1087. */
  1088. void lbs_interrupt(struct lbs_private *priv)
  1089. {
  1090. lbs_deb_enter(LBS_DEB_THREAD);
  1091. lbs_deb_thread("lbs_interrupt: intcounter=%d\n", priv->intcounter);
  1092. if (spin_trylock(&priv->driver_lock)) {
  1093. spin_unlock(&priv->driver_lock);
  1094. printk(KERN_CRIT "%s called without driver_lock held\n", __func__);
  1095. WARN_ON(1);
  1096. }
  1097. priv->intcounter++;
  1098. if (priv->psstate == PS_STATE_SLEEP)
  1099. priv->psstate = PS_STATE_AWAKE;
  1100. wake_up_interruptible(&priv->waitq);
  1101. lbs_deb_leave(LBS_DEB_THREAD);
  1102. }
  1103. EXPORT_SYMBOL_GPL(lbs_interrupt);
  1104. int lbs_reset_device(struct lbs_private *priv)
  1105. {
  1106. int ret;
  1107. lbs_deb_enter(LBS_DEB_MAIN);
  1108. ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
  1109. CMD_ACT_HALT, 0, 0, NULL);
  1110. msleep_interruptible(10);
  1111. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1112. return ret;
  1113. }
  1114. EXPORT_SYMBOL_GPL(lbs_reset_device);
  1115. static int __init lbs_init_module(void)
  1116. {
  1117. lbs_deb_enter(LBS_DEB_MAIN);
  1118. lbs_debugfs_init();
  1119. lbs_deb_leave(LBS_DEB_MAIN);
  1120. return 0;
  1121. }
  1122. static void __exit lbs_exit_module(void)
  1123. {
  1124. lbs_deb_enter(LBS_DEB_MAIN);
  1125. lbs_debugfs_remove();
  1126. lbs_deb_leave(LBS_DEB_MAIN);
  1127. }
  1128. /*
  1129. * rtap interface support fuctions
  1130. */
  1131. static int lbs_rtap_open(struct net_device *dev)
  1132. {
  1133. /* Yes, _stop_ the queue. Because we don't support injection */
  1134. netif_carrier_off(dev);
  1135. netif_stop_queue(dev);
  1136. return 0;
  1137. }
  1138. static int lbs_rtap_stop(struct net_device *dev)
  1139. {
  1140. return 0;
  1141. }
  1142. static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1143. {
  1144. netif_stop_queue(dev);
  1145. return NETDEV_TX_BUSY;
  1146. }
  1147. static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
  1148. {
  1149. struct lbs_private *priv = dev->priv;
  1150. return &priv->stats;
  1151. }
  1152. static void lbs_remove_rtap(struct lbs_private *priv)
  1153. {
  1154. if (priv->rtap_net_dev == NULL)
  1155. return;
  1156. unregister_netdev(priv->rtap_net_dev);
  1157. free_netdev(priv->rtap_net_dev);
  1158. priv->rtap_net_dev = NULL;
  1159. }
  1160. static int lbs_add_rtap(struct lbs_private *priv)
  1161. {
  1162. int rc = 0;
  1163. struct net_device *rtap_dev;
  1164. if (priv->rtap_net_dev)
  1165. return -EPERM;
  1166. rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
  1167. if (rtap_dev == NULL)
  1168. return -ENOMEM;
  1169. memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
  1170. rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1171. rtap_dev->open = lbs_rtap_open;
  1172. rtap_dev->stop = lbs_rtap_stop;
  1173. rtap_dev->get_stats = lbs_rtap_get_stats;
  1174. rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
  1175. rtap_dev->set_multicast_list = lbs_set_multicast_list;
  1176. rtap_dev->priv = priv;
  1177. rc = register_netdev(rtap_dev);
  1178. if (rc) {
  1179. free_netdev(rtap_dev);
  1180. return rc;
  1181. }
  1182. priv->rtap_net_dev = rtap_dev;
  1183. return 0;
  1184. }
  1185. module_init(lbs_init_module);
  1186. module_exit(lbs_exit_module);
  1187. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1188. MODULE_AUTHOR("Marvell International Ltd.");
  1189. MODULE_LICENSE("GPL");