main.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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);
  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 (priv->surpriseremoved)
  552. shouldsleep = 0; /* Bye */
  553. else if (priv->psstate == PS_STATE_SLEEP)
  554. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  555. else if (priv->intcounter)
  556. shouldsleep = 0; /* Interrupt pending. Deal with it now */
  557. else if (!priv->fw_ready)
  558. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  559. else if (priv->dnld_sent)
  560. shouldsleep = 1; /* Something is en route to the device already */
  561. else if (priv->tx_pending_len > 0)
  562. shouldsleep = 0; /* We've a packet to send */
  563. else if (priv->cur_cmd)
  564. shouldsleep = 1; /* Can't send a command; one already running */
  565. else if (!list_empty(&priv->cmdpendingq))
  566. shouldsleep = 0; /* We have a command to send */
  567. else
  568. shouldsleep = 1; /* No command */
  569. if (shouldsleep) {
  570. lbs_deb_thread("main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  571. priv->connect_status, priv->intcounter,
  572. priv->psmode, priv->psstate);
  573. spin_unlock_irq(&priv->driver_lock);
  574. schedule();
  575. } else
  576. spin_unlock_irq(&priv->driver_lock);
  577. lbs_deb_thread("main-thread 222 (waking up): intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  578. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  579. set_current_state(TASK_RUNNING);
  580. remove_wait_queue(&priv->waitq, &wait);
  581. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  582. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  583. if (kthread_should_stop() || priv->surpriseremoved) {
  584. lbs_deb_thread("main-thread: break from main thread: surpriseremoved=0x%x\n",
  585. priv->surpriseremoved);
  586. break;
  587. }
  588. spin_lock_irq(&priv->driver_lock);
  589. if (priv->intcounter) {
  590. u8 int_status;
  591. priv->intcounter = 0;
  592. int_status = priv->hw_get_int_status(priv, &ireg);
  593. if (int_status) {
  594. lbs_deb_thread("main-thread: reading HOST_INT_STATUS_REG failed\n");
  595. spin_unlock_irq(&priv->driver_lock);
  596. continue;
  597. }
  598. priv->hisregcpy |= ireg;
  599. }
  600. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  601. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  602. /* command response? */
  603. if (priv->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
  604. lbs_deb_thread("main-thread: cmd response ready\n");
  605. priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
  606. spin_unlock_irq(&priv->driver_lock);
  607. lbs_process_rx_command(priv);
  608. spin_lock_irq(&priv->driver_lock);
  609. }
  610. /* Any Card Event */
  611. if (priv->hisregcpy & MRVDRV_CARDEVENT) {
  612. lbs_deb_thread("main-thread: Card Event Activity\n");
  613. priv->hisregcpy &= ~MRVDRV_CARDEVENT;
  614. if (priv->hw_read_event_cause(priv)) {
  615. lbs_pr_alert("main-thread: hw_read_event_cause failed\n");
  616. spin_unlock_irq(&priv->driver_lock);
  617. continue;
  618. }
  619. spin_unlock_irq(&priv->driver_lock);
  620. lbs_process_event(priv);
  621. } else
  622. spin_unlock_irq(&priv->driver_lock);
  623. if (!priv->fw_ready)
  624. continue;
  625. /* Check if we need to confirm Sleep Request received previously */
  626. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  627. !priv->dnld_sent && !priv->cur_cmd) {
  628. if (priv->connect_status == LBS_CONNECTED) {
  629. lbs_deb_thread("main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p dnld_sent=%d cur_cmd=%p, confirm now\n",
  630. priv->intcounter, priv->currenttxskb, priv->dnld_sent, priv->cur_cmd);
  631. lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
  632. } else {
  633. /* workaround for firmware sending
  634. * deauth/linkloss event immediately
  635. * after sleep request; remove this
  636. * after firmware fixes it
  637. */
  638. priv->psstate = PS_STATE_AWAKE;
  639. lbs_pr_alert("main-thread: ignore PS_SleepConfirm in non-connected state\n");
  640. }
  641. }
  642. /* The PS state is changed during processing of Sleep Request
  643. * event above
  644. */
  645. if ((priv->psstate == PS_STATE_SLEEP) ||
  646. (priv->psstate == PS_STATE_PRE_SLEEP))
  647. continue;
  648. /* Execute the next command */
  649. if (!priv->dnld_sent && !priv->cur_cmd)
  650. lbs_execute_next_command(priv);
  651. /* Wake-up command waiters which can't sleep in
  652. * lbs_prepare_and_send_command
  653. */
  654. if (!list_empty(&priv->cmdpendingq))
  655. wake_up_all(&priv->cmd_pending);
  656. spin_lock_irq(&priv->driver_lock);
  657. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  658. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  659. priv->tx_pending_buf,
  660. priv->tx_pending_len);
  661. if (ret) {
  662. lbs_deb_tx("host_to_card failed %d\n", ret);
  663. priv->dnld_sent = DNLD_RES_RECEIVED;
  664. }
  665. priv->tx_pending_len = 0;
  666. if (!priv->currenttxskb) {
  667. /* We can wake the queues immediately if we aren't
  668. waiting for TX feedback */
  669. if (priv->connect_status == LBS_CONNECTED)
  670. netif_wake_queue(priv->dev);
  671. if (priv->mesh_dev &&
  672. priv->mesh_connect_status == LBS_CONNECTED)
  673. netif_wake_queue(priv->mesh_dev);
  674. }
  675. }
  676. spin_unlock_irq(&priv->driver_lock);
  677. }
  678. del_timer(&priv->command_timer);
  679. wake_up_all(&priv->cmd_pending);
  680. lbs_deb_leave(LBS_DEB_THREAD);
  681. return 0;
  682. }
  683. static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
  684. struct cmd_header *cmd)
  685. {
  686. lbs_deb_fw("HOST_SLEEP_ACTIVATE succeeded\n");
  687. netif_device_detach(priv->dev);
  688. if (priv->mesh_dev)
  689. netif_device_detach(priv->mesh_dev);
  690. priv->fw_ready = 0;
  691. return 0;
  692. }
  693. int lbs_suspend(struct lbs_private *priv)
  694. {
  695. struct cmd_header cmd;
  696. int ret;
  697. if (priv->wol_criteria == 0xffffffff) {
  698. lbs_pr_info("Suspend attempt without configuring wake params!\n");
  699. return -EINVAL;
  700. }
  701. memset(&cmd, 0, sizeof(cmd));
  702. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
  703. sizeof(cmd), lbs_suspend_callback, 0);
  704. if (ret)
  705. lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
  706. return ret;
  707. }
  708. EXPORT_SYMBOL_GPL(lbs_suspend);
  709. int lbs_resume(struct lbs_private *priv)
  710. {
  711. priv->fw_ready = 1;
  712. /* Firmware doesn't seem to give us RX packets any more
  713. until we send it some command. Might as well update */
  714. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  715. 0, 0, NULL);
  716. netif_device_attach(priv->dev);
  717. if (priv->mesh_dev)
  718. netif_device_attach(priv->mesh_dev);
  719. return 0;
  720. }
  721. EXPORT_SYMBOL_GPL(lbs_resume);
  722. /**
  723. * @brief This function downloads firmware image, gets
  724. * HW spec from firmware and set basic parameters to
  725. * firmware.
  726. *
  727. * @param priv A pointer to struct lbs_private structure
  728. * @return 0 or -1
  729. */
  730. static int lbs_setup_firmware(struct lbs_private *priv)
  731. {
  732. int ret = -1;
  733. lbs_deb_enter(LBS_DEB_FW);
  734. /*
  735. * Read MAC address from HW
  736. */
  737. memset(priv->current_addr, 0xff, ETH_ALEN);
  738. ret = lbs_update_hw_spec(priv);
  739. if (ret) {
  740. ret = -1;
  741. goto done;
  742. }
  743. lbs_set_mac_packet_filter(priv);
  744. ret = lbs_get_data_rate(priv);
  745. if (ret < 0) {
  746. ret = -1;
  747. goto done;
  748. }
  749. ret = 0;
  750. done:
  751. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  752. return ret;
  753. }
  754. /**
  755. * This function handles the timeout of command sending.
  756. * It will re-send the same command again.
  757. */
  758. static void command_timer_fn(unsigned long data)
  759. {
  760. struct lbs_private *priv = (struct lbs_private *)data;
  761. struct cmd_ctrl_node *node;
  762. unsigned long flags;
  763. node = priv->cur_cmd;
  764. if (node == NULL) {
  765. lbs_deb_fw("ptempnode empty\n");
  766. return;
  767. }
  768. if (!node->cmdbuf) {
  769. lbs_deb_fw("cmd is NULL\n");
  770. return;
  771. }
  772. lbs_pr_info("command %x timed out\n", le16_to_cpu(node->cmdbuf->command));
  773. if (!priv->fw_ready)
  774. return;
  775. spin_lock_irqsave(&priv->driver_lock, flags);
  776. priv->cur_cmd = NULL;
  777. spin_unlock_irqrestore(&priv->driver_lock, flags);
  778. lbs_deb_fw("re-sending same command because of timeout\n");
  779. lbs_queue_cmd(priv, node, 0);
  780. wake_up_interruptible(&priv->waitq);
  781. return;
  782. }
  783. static int lbs_init_adapter(struct lbs_private *priv)
  784. {
  785. size_t bufsize;
  786. int i, ret = 0;
  787. /* Allocate buffer to store the BSSID list */
  788. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  789. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  790. if (!priv->networks) {
  791. lbs_pr_err("Out of memory allocating beacons\n");
  792. ret = -1;
  793. goto out;
  794. }
  795. /* Initialize scan result lists */
  796. INIT_LIST_HEAD(&priv->network_free_list);
  797. INIT_LIST_HEAD(&priv->network_list);
  798. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  799. list_add_tail(&priv->networks[i].list,
  800. &priv->network_free_list);
  801. }
  802. priv->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++priv->seqnum);
  803. priv->lbs_ps_confirm_sleep.command =
  804. cpu_to_le16(CMD_802_11_PS_MODE);
  805. priv->lbs_ps_confirm_sleep.size =
  806. cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
  807. priv->lbs_ps_confirm_sleep.action =
  808. cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  809. memset(priv->current_addr, 0xff, ETH_ALEN);
  810. priv->connect_status = LBS_DISCONNECTED;
  811. priv->mesh_connect_status = LBS_DISCONNECTED;
  812. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  813. priv->mode = IW_MODE_INFRA;
  814. priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  815. priv->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  816. priv->radioon = RADIO_ON;
  817. priv->auto_rate = 1;
  818. priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  819. priv->psmode = LBS802_11POWERMODECAM;
  820. priv->psstate = PS_STATE_FULL_POWER;
  821. mutex_init(&priv->lock);
  822. setup_timer(&priv->command_timer, command_timer_fn,
  823. (unsigned long)priv);
  824. INIT_LIST_HEAD(&priv->cmdfreeq);
  825. INIT_LIST_HEAD(&priv->cmdpendingq);
  826. spin_lock_init(&priv->driver_lock);
  827. init_waitqueue_head(&priv->cmd_pending);
  828. /* Allocate the command buffers */
  829. if (lbs_allocate_cmd_buffer(priv)) {
  830. lbs_pr_err("Out of memory allocating command buffers\n");
  831. ret = -1;
  832. }
  833. out:
  834. return ret;
  835. }
  836. static void lbs_free_adapter(struct lbs_private *priv)
  837. {
  838. lbs_deb_fw("free command buffer\n");
  839. lbs_free_cmd_buffer(priv);
  840. lbs_deb_fw("free command_timer\n");
  841. del_timer(&priv->command_timer);
  842. lbs_deb_fw("free scan results table\n");
  843. kfree(priv->networks);
  844. priv->networks = NULL;
  845. }
  846. /**
  847. * @brief This function adds the card. it will probe the
  848. * card, allocate the lbs_priv and initialize the device.
  849. *
  850. * @param card A pointer to card
  851. * @return A pointer to struct lbs_private structure
  852. */
  853. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  854. {
  855. struct net_device *dev = NULL;
  856. struct lbs_private *priv = NULL;
  857. lbs_deb_enter(LBS_DEB_NET);
  858. /* Allocate an Ethernet device and register it */
  859. dev = alloc_etherdev(sizeof(struct lbs_private));
  860. if (!dev) {
  861. lbs_pr_err("init ethX device failed\n");
  862. goto done;
  863. }
  864. priv = dev->priv;
  865. if (lbs_init_adapter(priv)) {
  866. lbs_pr_err("failed to initialize adapter structure.\n");
  867. goto err_init_adapter;
  868. }
  869. priv->dev = dev;
  870. priv->card = card;
  871. priv->mesh_open = 0;
  872. priv->infra_open = 0;
  873. /* Setup the OS Interface to our functions */
  874. dev->open = lbs_dev_open;
  875. dev->hard_start_xmit = lbs_hard_start_xmit;
  876. dev->stop = lbs_eth_stop;
  877. dev->set_mac_address = lbs_set_mac_address;
  878. dev->tx_timeout = lbs_tx_timeout;
  879. dev->get_stats = lbs_get_stats;
  880. dev->watchdog_timeo = 5 * HZ;
  881. dev->ethtool_ops = &lbs_ethtool_ops;
  882. #ifdef WIRELESS_EXT
  883. dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
  884. #endif
  885. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  886. dev->set_multicast_list = lbs_set_multicast_list;
  887. SET_NETDEV_DEV(dev, dmdev);
  888. priv->rtap_net_dev = NULL;
  889. lbs_deb_thread("Starting main thread...\n");
  890. init_waitqueue_head(&priv->waitq);
  891. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  892. if (IS_ERR(priv->main_thread)) {
  893. lbs_deb_thread("Error creating main thread.\n");
  894. goto err_init_adapter;
  895. }
  896. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  897. INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
  898. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  899. INIT_WORK(&priv->sync_channel, lbs_sync_channel);
  900. sprintf(priv->mesh_ssid, "mesh");
  901. priv->mesh_ssid_len = 4;
  902. priv->wol_criteria = 0xffffffff;
  903. priv->wol_gpio = 0xff;
  904. goto done;
  905. err_init_adapter:
  906. lbs_free_adapter(priv);
  907. free_netdev(dev);
  908. priv = NULL;
  909. done:
  910. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  911. return priv;
  912. }
  913. EXPORT_SYMBOL_GPL(lbs_add_card);
  914. int lbs_remove_card(struct lbs_private *priv)
  915. {
  916. struct net_device *dev = priv->dev;
  917. union iwreq_data wrqu;
  918. lbs_deb_enter(LBS_DEB_MAIN);
  919. lbs_remove_mesh(priv);
  920. lbs_remove_rtap(priv);
  921. dev = priv->dev;
  922. cancel_delayed_work(&priv->scan_work);
  923. cancel_delayed_work(&priv->assoc_work);
  924. destroy_workqueue(priv->work_thread);
  925. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  926. priv->psmode = LBS802_11POWERMODECAM;
  927. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  928. }
  929. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  930. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  931. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  932. /* Stop the thread servicing the interrupts */
  933. priv->surpriseremoved = 1;
  934. kthread_stop(priv->main_thread);
  935. lbs_free_adapter(priv);
  936. priv->dev = NULL;
  937. free_netdev(dev);
  938. lbs_deb_leave(LBS_DEB_MAIN);
  939. return 0;
  940. }
  941. EXPORT_SYMBOL_GPL(lbs_remove_card);
  942. int lbs_start_card(struct lbs_private *priv)
  943. {
  944. struct net_device *dev = priv->dev;
  945. int ret = -1;
  946. lbs_deb_enter(LBS_DEB_MAIN);
  947. /* poke the firmware */
  948. ret = lbs_setup_firmware(priv);
  949. if (ret)
  950. goto done;
  951. /* init 802.11d */
  952. lbs_init_11d(priv);
  953. if (register_netdev(dev)) {
  954. lbs_pr_err("cannot register ethX device\n");
  955. goto done;
  956. }
  957. if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
  958. lbs_pr_err("cannot register lbs_rtap attribute\n");
  959. if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
  960. lbs_pr_err("cannot register lbs_mesh attribute\n");
  961. lbs_debugfs_init_one(priv, dev);
  962. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  963. ret = 0;
  964. done:
  965. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  966. return ret;
  967. }
  968. EXPORT_SYMBOL_GPL(lbs_start_card);
  969. int lbs_stop_card(struct lbs_private *priv)
  970. {
  971. struct net_device *dev = priv->dev;
  972. int ret = -1;
  973. struct cmd_ctrl_node *cmdnode;
  974. unsigned long flags;
  975. lbs_deb_enter(LBS_DEB_MAIN);
  976. netif_stop_queue(priv->dev);
  977. netif_carrier_off(priv->dev);
  978. lbs_debugfs_remove_one(priv);
  979. device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
  980. device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
  981. /* Flush pending command nodes */
  982. spin_lock_irqsave(&priv->driver_lock, flags);
  983. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  984. cmdnode->cmdwaitqwoken = 1;
  985. wake_up_interruptible(&cmdnode->cmdwait_q);
  986. }
  987. spin_unlock_irqrestore(&priv->driver_lock, flags);
  988. unregister_netdev(dev);
  989. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  990. return ret;
  991. }
  992. EXPORT_SYMBOL_GPL(lbs_stop_card);
  993. /**
  994. * @brief This function adds mshX interface
  995. *
  996. * @param priv A pointer to the struct lbs_private structure
  997. * @return 0 if successful, -X otherwise
  998. */
  999. static int lbs_add_mesh(struct lbs_private *priv)
  1000. {
  1001. struct net_device *mesh_dev = NULL;
  1002. int ret = 0;
  1003. lbs_deb_enter(LBS_DEB_MESH);
  1004. /* Allocate a virtual mesh device */
  1005. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1006. lbs_deb_mesh("init mshX device failed\n");
  1007. ret = -ENOMEM;
  1008. goto done;
  1009. }
  1010. mesh_dev->priv = priv;
  1011. priv->mesh_dev = mesh_dev;
  1012. mesh_dev->open = lbs_dev_open;
  1013. mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
  1014. mesh_dev->stop = lbs_mesh_stop;
  1015. mesh_dev->get_stats = lbs_get_stats;
  1016. mesh_dev->set_mac_address = lbs_set_mac_address;
  1017. mesh_dev->ethtool_ops = &lbs_ethtool_ops;
  1018. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1019. sizeof(priv->dev->dev_addr));
  1020. SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
  1021. #ifdef WIRELESS_EXT
  1022. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1023. #endif
  1024. /* Register virtual mesh interface */
  1025. ret = register_netdev(mesh_dev);
  1026. if (ret) {
  1027. lbs_pr_err("cannot register mshX virtual interface\n");
  1028. goto err_free;
  1029. }
  1030. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1031. if (ret)
  1032. goto err_unregister;
  1033. /* Everything successful */
  1034. ret = 0;
  1035. goto done;
  1036. err_unregister:
  1037. unregister_netdev(mesh_dev);
  1038. err_free:
  1039. free_netdev(mesh_dev);
  1040. done:
  1041. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1042. return ret;
  1043. }
  1044. EXPORT_SYMBOL_GPL(lbs_add_mesh);
  1045. static void lbs_remove_mesh(struct lbs_private *priv)
  1046. {
  1047. struct net_device *mesh_dev;
  1048. lbs_deb_enter(LBS_DEB_MAIN);
  1049. if (!priv)
  1050. goto out;
  1051. mesh_dev = priv->mesh_dev;
  1052. if (!mesh_dev)
  1053. goto out;
  1054. netif_stop_queue(mesh_dev);
  1055. netif_carrier_off(priv->mesh_dev);
  1056. sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1057. unregister_netdev(mesh_dev);
  1058. priv->mesh_dev = NULL;
  1059. free_netdev(mesh_dev);
  1060. out:
  1061. lbs_deb_leave(LBS_DEB_MAIN);
  1062. }
  1063. EXPORT_SYMBOL_GPL(lbs_remove_mesh);
  1064. /**
  1065. * @brief This function finds the CFP in
  1066. * region_cfp_table based on region and band parameter.
  1067. *
  1068. * @param region The region code
  1069. * @param band The band
  1070. * @param cfp_no A pointer to CFP number
  1071. * @return A pointer to CFP
  1072. */
  1073. struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  1074. {
  1075. int i, end;
  1076. lbs_deb_enter(LBS_DEB_MAIN);
  1077. end = ARRAY_SIZE(region_cfp_table);
  1078. for (i = 0; i < end ; i++) {
  1079. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1080. region_cfp_table[i].region);
  1081. if (region_cfp_table[i].region == region) {
  1082. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1083. lbs_deb_leave(LBS_DEB_MAIN);
  1084. return region_cfp_table[i].cfp_BG;
  1085. }
  1086. }
  1087. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1088. return NULL;
  1089. }
  1090. int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
  1091. {
  1092. int ret = 0;
  1093. int i = 0;
  1094. struct chan_freq_power *cfp;
  1095. int cfp_no;
  1096. lbs_deb_enter(LBS_DEB_MAIN);
  1097. memset(priv->region_channel, 0, sizeof(priv->region_channel));
  1098. {
  1099. cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
  1100. if (cfp != NULL) {
  1101. priv->region_channel[i].nrcfp = cfp_no;
  1102. priv->region_channel[i].CFP = cfp;
  1103. } else {
  1104. lbs_deb_main("wrong region code %#x in band B/G\n",
  1105. region);
  1106. ret = -1;
  1107. goto out;
  1108. }
  1109. priv->region_channel[i].valid = 1;
  1110. priv->region_channel[i].region = region;
  1111. priv->region_channel[i].band = band;
  1112. i++;
  1113. }
  1114. out:
  1115. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1116. return ret;
  1117. }
  1118. /**
  1119. * @brief This function handles the interrupt. it will change PS
  1120. * state if applicable. it will wake up main_thread to handle
  1121. * the interrupt event as well.
  1122. *
  1123. * @param dev A pointer to net_device structure
  1124. * @return n/a
  1125. */
  1126. void lbs_interrupt(struct lbs_private *priv)
  1127. {
  1128. lbs_deb_enter(LBS_DEB_THREAD);
  1129. lbs_deb_thread("lbs_interrupt: intcounter=%d\n", priv->intcounter);
  1130. if (spin_trylock(&priv->driver_lock)) {
  1131. spin_unlock(&priv->driver_lock);
  1132. printk(KERN_CRIT "%s called without driver_lock held\n", __func__);
  1133. WARN_ON(1);
  1134. }
  1135. priv->intcounter++;
  1136. if (priv->psstate == PS_STATE_SLEEP)
  1137. priv->psstate = PS_STATE_AWAKE;
  1138. wake_up_interruptible(&priv->waitq);
  1139. lbs_deb_leave(LBS_DEB_THREAD);
  1140. }
  1141. EXPORT_SYMBOL_GPL(lbs_interrupt);
  1142. int lbs_reset_device(struct lbs_private *priv)
  1143. {
  1144. int ret;
  1145. lbs_deb_enter(LBS_DEB_MAIN);
  1146. ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
  1147. CMD_ACT_HALT, 0, 0, NULL);
  1148. msleep_interruptible(10);
  1149. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1150. return ret;
  1151. }
  1152. EXPORT_SYMBOL_GPL(lbs_reset_device);
  1153. static int __init lbs_init_module(void)
  1154. {
  1155. lbs_deb_enter(LBS_DEB_MAIN);
  1156. lbs_debugfs_init();
  1157. lbs_deb_leave(LBS_DEB_MAIN);
  1158. return 0;
  1159. }
  1160. static void __exit lbs_exit_module(void)
  1161. {
  1162. lbs_deb_enter(LBS_DEB_MAIN);
  1163. lbs_debugfs_remove();
  1164. lbs_deb_leave(LBS_DEB_MAIN);
  1165. }
  1166. /*
  1167. * rtap interface support fuctions
  1168. */
  1169. static int lbs_rtap_open(struct net_device *dev)
  1170. {
  1171. /* Yes, _stop_ the queue. Because we don't support injection */
  1172. netif_carrier_off(dev);
  1173. netif_stop_queue(dev);
  1174. return 0;
  1175. }
  1176. static int lbs_rtap_stop(struct net_device *dev)
  1177. {
  1178. return 0;
  1179. }
  1180. static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1181. {
  1182. netif_stop_queue(dev);
  1183. return NETDEV_TX_BUSY;
  1184. }
  1185. static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
  1186. {
  1187. struct lbs_private *priv = dev->priv;
  1188. return &priv->stats;
  1189. }
  1190. static void lbs_remove_rtap(struct lbs_private *priv)
  1191. {
  1192. if (priv->rtap_net_dev == NULL)
  1193. return;
  1194. unregister_netdev(priv->rtap_net_dev);
  1195. free_netdev(priv->rtap_net_dev);
  1196. priv->rtap_net_dev = NULL;
  1197. }
  1198. static int lbs_add_rtap(struct lbs_private *priv)
  1199. {
  1200. int rc = 0;
  1201. struct net_device *rtap_dev;
  1202. if (priv->rtap_net_dev)
  1203. return -EPERM;
  1204. rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
  1205. if (rtap_dev == NULL)
  1206. return -ENOMEM;
  1207. memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
  1208. rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1209. rtap_dev->open = lbs_rtap_open;
  1210. rtap_dev->stop = lbs_rtap_stop;
  1211. rtap_dev->get_stats = lbs_rtap_get_stats;
  1212. rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
  1213. rtap_dev->set_multicast_list = lbs_set_multicast_list;
  1214. rtap_dev->priv = priv;
  1215. rc = register_netdev(rtap_dev);
  1216. if (rc) {
  1217. free_netdev(rtap_dev);
  1218. return rc;
  1219. }
  1220. priv->rtap_net_dev = rtap_dev;
  1221. return 0;
  1222. }
  1223. module_init(lbs_init_module);
  1224. module_exit(lbs_exit_module);
  1225. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1226. MODULE_AUTHOR("Marvell International Ltd.");
  1227. MODULE_LICENSE("GPL");