/kern_2.6.32/drivers/net/wireless/libertas/main.c

http://omnia2droid.googlecode.com/
C | 1786 lines | 1229 code | 323 blank | 234 comment | 171 complexity | d3e96e40c1f8bd092a7cde696762a96e MD5 | raw file

✨ Summary
  1. /**
  2. * This file contains the major functions in WLAN
  3. * driver. It includes init, exit, open, close and main
  4. * thread etc..
  5. */
  6. #include <linux/moduleparam.h>
  7. #include <linux/delay.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/kthread.h>
  12. #include <linux/kfifo.h>
  13. #include <linux/stddef.h>
  14. #include <linux/ieee80211.h>
  15. #include <net/iw_handler.h>
  16. #include "host.h"
  17. #include "decl.h"
  18. #include "dev.h"
  19. #include "wext.h"
  20. #include "debugfs.h"
  21. #include "scan.h"
  22. #include "assoc.h"
  23. #include "cmd.h"
  24. #define DRIVER_RELEASE_VERSION "323.p0"
  25. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  26. #ifdef DEBUG
  27. "-dbg"
  28. #endif
  29. "";
  30. /* Module parameters */
  31. unsigned int lbs_debug;
  32. EXPORT_SYMBOL_GPL(lbs_debug);
  33. module_param_named(libertas_debug, lbs_debug, int, 0644);
  34. /* This global structure is used to send the confirm_sleep command as
  35. * fast as possible down to the firmware. */
  36. struct cmd_confirm_sleep confirm_sleep;
  37. #define LBS_TX_PWR_DEFAULT 20 /*100mW */
  38. #define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
  39. #define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
  40. #define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
  41. #define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
  42. /* Format { channel, frequency (MHz), maxtxpower } */
  43. /* band: 'B/G', region: USA FCC/Canada IC */
  44. static struct chan_freq_power channel_freq_power_US_BG[] = {
  45. {1, 2412, LBS_TX_PWR_US_DEFAULT},
  46. {2, 2417, LBS_TX_PWR_US_DEFAULT},
  47. {3, 2422, LBS_TX_PWR_US_DEFAULT},
  48. {4, 2427, LBS_TX_PWR_US_DEFAULT},
  49. {5, 2432, LBS_TX_PWR_US_DEFAULT},
  50. {6, 2437, LBS_TX_PWR_US_DEFAULT},
  51. {7, 2442, LBS_TX_PWR_US_DEFAULT},
  52. {8, 2447, LBS_TX_PWR_US_DEFAULT},
  53. {9, 2452, LBS_TX_PWR_US_DEFAULT},
  54. {10, 2457, LBS_TX_PWR_US_DEFAULT},
  55. {11, 2462, LBS_TX_PWR_US_DEFAULT}
  56. };
  57. /* band: 'B/G', region: Europe ETSI */
  58. static struct chan_freq_power channel_freq_power_EU_BG[] = {
  59. {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
  60. {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
  61. {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
  62. {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
  63. {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
  64. {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
  65. {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
  66. {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
  67. {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
  68. {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
  69. {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
  70. {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
  71. {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
  72. };
  73. /* band: 'B/G', region: Spain */
  74. static struct chan_freq_power channel_freq_power_SPN_BG[] = {
  75. {10, 2457, LBS_TX_PWR_DEFAULT},
  76. {11, 2462, LBS_TX_PWR_DEFAULT}
  77. };
  78. /* band: 'B/G', region: France */
  79. static struct chan_freq_power channel_freq_power_FR_BG[] = {
  80. {10, 2457, LBS_TX_PWR_FR_DEFAULT},
  81. {11, 2462, LBS_TX_PWR_FR_DEFAULT},
  82. {12, 2467, LBS_TX_PWR_FR_DEFAULT},
  83. {13, 2472, LBS_TX_PWR_FR_DEFAULT}
  84. };
  85. /* band: 'B/G', region: Japan */
  86. static struct chan_freq_power channel_freq_power_JPN_BG[] = {
  87. {1, 2412, LBS_TX_PWR_JP_DEFAULT},
  88. {2, 2417, LBS_TX_PWR_JP_DEFAULT},
  89. {3, 2422, LBS_TX_PWR_JP_DEFAULT},
  90. {4, 2427, LBS_TX_PWR_JP_DEFAULT},
  91. {5, 2432, LBS_TX_PWR_JP_DEFAULT},
  92. {6, 2437, LBS_TX_PWR_JP_DEFAULT},
  93. {7, 2442, LBS_TX_PWR_JP_DEFAULT},
  94. {8, 2447, LBS_TX_PWR_JP_DEFAULT},
  95. {9, 2452, LBS_TX_PWR_JP_DEFAULT},
  96. {10, 2457, LBS_TX_PWR_JP_DEFAULT},
  97. {11, 2462, LBS_TX_PWR_JP_DEFAULT},
  98. {12, 2467, LBS_TX_PWR_JP_DEFAULT},
  99. {13, 2472, LBS_TX_PWR_JP_DEFAULT},
  100. {14, 2484, LBS_TX_PWR_JP_DEFAULT}
  101. };
  102. /**
  103. * the structure for channel, frequency and power
  104. */
  105. struct region_cfp_table {
  106. u8 region;
  107. struct chan_freq_power *cfp_BG;
  108. int cfp_no_BG;
  109. };
  110. /**
  111. * the structure for the mapping between region and CFP
  112. */
  113. static struct region_cfp_table region_cfp_table[] = {
  114. {0x10, /*US FCC */
  115. channel_freq_power_US_BG,
  116. ARRAY_SIZE(channel_freq_power_US_BG),
  117. }
  118. ,
  119. {0x20, /*CANADA IC */
  120. channel_freq_power_US_BG,
  121. ARRAY_SIZE(channel_freq_power_US_BG),
  122. }
  123. ,
  124. {0x30, /*EU*/ channel_freq_power_EU_BG,
  125. ARRAY_SIZE(channel_freq_power_EU_BG),
  126. }
  127. ,
  128. {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
  129. ARRAY_SIZE(channel_freq_power_SPN_BG),
  130. }
  131. ,
  132. {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
  133. ARRAY_SIZE(channel_freq_power_FR_BG),
  134. }
  135. ,
  136. {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
  137. ARRAY_SIZE(channel_freq_power_JPN_BG),
  138. }
  139. ,
  140. /*Add new region here */
  141. };
  142. /**
  143. * the table to keep region code
  144. */
  145. u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  146. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  147. /**
  148. * 802.11b/g supported bitrates (in 500Kb/s units)
  149. */
  150. u8 lbs_bg_rates[MAX_RATES] =
  151. { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  152. 0x00, 0x00 };
  153. /**
  154. * FW rate table. FW refers to rates by their index in this table, not by the
  155. * rate value itself. Values of 0x00 are
  156. * reserved positions.
  157. */
  158. static u8 fw_data_rates[MAX_RATES] =
  159. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  160. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  161. };
  162. static int lbs_set_rf_reg(struct lbs_private* priv, u16 offset, u8 value)
  163. {
  164. struct lbs_offset_value offval;
  165. int ret;
  166. offval.offset = offset;
  167. offval.value = value;
  168. ret = lbs_prepare_and_send_command(priv,
  169. CMD_BBP_REG_ACCESS, CMD_ACT_SET,
  170. CMD_OPTION_WAITFORRSP, 0, &offval);
  171. return ret;
  172. }
  173. static int lbs_set_mac_reg(struct lbs_private* priv, u16 offset, u32 value)
  174. {
  175. struct lbs_offset_value offval;
  176. int ret;
  177. offval.offset = offset;
  178. offval.value = value;
  179. ret = lbs_prepare_and_send_command(priv,
  180. CMD_MAC_REG_ACCESS, CMD_ACT_SET,
  181. CMD_OPTION_WAITFORRSP, 0, &offval);
  182. return ret;
  183. }
  184. static int lbs_set_bbp_reg(struct lbs_private* priv, u16 offset, u8 value)
  185. {
  186. struct lbs_offset_value offval;
  187. int ret;
  188. offval.offset = offset;
  189. offval.value = value;
  190. ret = lbs_prepare_and_send_command(priv,
  191. CMD_RF_REG_ACCESS, CMD_ACT_SET,
  192. CMD_OPTION_WAITFORRSP, 0, &offval);
  193. return ret;
  194. }
  195. /**
  196. * @brief use index to get the data rate
  197. *
  198. * @param idx The index of data rate
  199. * @return data rate or 0
  200. */
  201. u32 lbs_fw_index_to_data_rate(u8 idx)
  202. {
  203. if (idx >= sizeof(fw_data_rates))
  204. idx = 0;
  205. return fw_data_rates[idx];
  206. }
  207. /**
  208. * @brief use rate to get the index
  209. *
  210. * @param rate data rate
  211. * @return index or 0
  212. */
  213. u8 lbs_data_rate_to_fw_index(u32 rate)
  214. {
  215. u8 i;
  216. if (!rate)
  217. return 0;
  218. for (i = 0; i < sizeof(fw_data_rates); i++) {
  219. if (rate == fw_data_rates[i])
  220. return i;
  221. }
  222. return 0;
  223. }
  224. /**
  225. * Attributes exported through sysfs
  226. */
  227. /**
  228. * @brief Get function for sysfs attribute anycast_mask
  229. */
  230. static ssize_t lbs_anycast_get(struct device *dev,
  231. struct device_attribute *attr, char * buf)
  232. {
  233. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  234. struct cmd_ds_mesh_access mesh_access;
  235. int ret;
  236. memset(&mesh_access, 0, sizeof(mesh_access));
  237. ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
  238. if (ret)
  239. return ret;
  240. return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
  241. }
  242. /**
  243. * @brief Set function for sysfs attribute anycast_mask
  244. */
  245. static ssize_t lbs_anycast_set(struct device *dev,
  246. struct device_attribute *attr, const char * buf, size_t count)
  247. {
  248. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  249. struct cmd_ds_mesh_access mesh_access;
  250. uint32_t datum;
  251. int ret;
  252. memset(&mesh_access, 0, sizeof(mesh_access));
  253. sscanf(buf, "%x", &datum);
  254. mesh_access.data[0] = cpu_to_le32(datum);
  255. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
  256. if (ret)
  257. return ret;
  258. return strlen(buf);
  259. }
  260. /**
  261. * @brief Get function for sysfs attribute prb_rsp_limit
  262. */
  263. static ssize_t lbs_prb_rsp_limit_get(struct device *dev,
  264. struct device_attribute *attr, char *buf)
  265. {
  266. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  267. struct cmd_ds_mesh_access mesh_access;
  268. int ret;
  269. u32 retry_limit;
  270. memset(&mesh_access, 0, sizeof(mesh_access));
  271. mesh_access.data[0] = cpu_to_le32(CMD_ACT_GET);
  272. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
  273. &mesh_access);
  274. if (ret)
  275. return ret;
  276. retry_limit = le32_to_cpu(mesh_access.data[1]);
  277. return snprintf(buf, 10, "%d\n", retry_limit);
  278. }
  279. /**
  280. * @brief Set function for sysfs attribute prb_rsp_limit
  281. */
  282. static ssize_t lbs_prb_rsp_limit_set(struct device *dev,
  283. struct device_attribute *attr, const char *buf, size_t count)
  284. {
  285. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  286. struct cmd_ds_mesh_access mesh_access;
  287. int ret;
  288. unsigned long retry_limit;
  289. memset(&mesh_access, 0, sizeof(mesh_access));
  290. mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
  291. if (!strict_strtoul(buf, 10, &retry_limit))
  292. return -ENOTSUPP;
  293. if (retry_limit > 15)
  294. return -ENOTSUPP;
  295. mesh_access.data[1] = cpu_to_le32(retry_limit);
  296. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
  297. &mesh_access);
  298. if (ret)
  299. return ret;
  300. return strlen(buf);
  301. }
  302. static int lbs_add_rtap(struct lbs_private *priv);
  303. static void lbs_remove_rtap(struct lbs_private *priv);
  304. static int lbs_add_mesh(struct lbs_private *priv);
  305. static void lbs_remove_mesh(struct lbs_private *priv);
  306. /**
  307. * Get function for sysfs attribute rtap
  308. */
  309. static ssize_t lbs_rtap_get(struct device *dev,
  310. struct device_attribute *attr, char * buf)
  311. {
  312. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  313. return snprintf(buf, 5, "0x%X\n", priv->monitormode);
  314. }
  315. /**
  316. * Set function for sysfs attribute rtap
  317. */
  318. static ssize_t lbs_rtap_set(struct device *dev,
  319. struct device_attribute *attr, const char * buf, size_t count)
  320. {
  321. int monitor_mode;
  322. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  323. sscanf(buf, "%x", &monitor_mode);
  324. if (monitor_mode) {
  325. if (priv->monitormode == monitor_mode)
  326. return strlen(buf);
  327. if (!priv->monitormode) {
  328. if (priv->infra_open || priv->mesh_open)
  329. return -EBUSY;
  330. if (priv->mode == IW_MODE_INFRA)
  331. lbs_cmd_80211_deauthenticate(priv,
  332. priv->curbssparams.bssid,
  333. WLAN_REASON_DEAUTH_LEAVING);
  334. else if (priv->mode == IW_MODE_ADHOC)
  335. lbs_adhoc_stop(priv);
  336. lbs_add_rtap(priv);
  337. }
  338. priv->monitormode = monitor_mode;
  339. } else {
  340. if (!priv->monitormode)
  341. return strlen(buf);
  342. priv->monitormode = 0;
  343. lbs_remove_rtap(priv);
  344. if (priv->currenttxskb) {
  345. dev_kfree_skb_any(priv->currenttxskb);
  346. priv->currenttxskb = NULL;
  347. }
  348. /* Wake queues, command thread, etc. */
  349. lbs_host_to_card_done(priv);
  350. }
  351. lbs_prepare_and_send_command(priv,
  352. CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
  353. CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
  354. return strlen(buf);
  355. }
  356. /**
  357. * lbs_rtap attribute to be exported per ethX interface
  358. * through sysfs (/sys/class/net/ethX/lbs_rtap)
  359. */
  360. static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
  361. /**
  362. * Get function for sysfs attribute mesh
  363. */
  364. static ssize_t lbs_mesh_get(struct device *dev,
  365. struct device_attribute *attr, char * buf)
  366. {
  367. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  368. return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
  369. }
  370. /**
  371. * Set function for sysfs attribute mesh
  372. */
  373. static ssize_t lbs_mesh_set(struct device *dev,
  374. struct device_attribute *attr, const char * buf, size_t count)
  375. {
  376. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  377. int enable;
  378. int ret, action = CMD_ACT_MESH_CONFIG_STOP;
  379. sscanf(buf, "%x", &enable);
  380. enable = !!enable;
  381. if (enable == !!priv->mesh_dev)
  382. return count;
  383. if (enable)
  384. action = CMD_ACT_MESH_CONFIG_START;
  385. ret = lbs_mesh_config(priv, action, priv->curbssparams.channel);
  386. if (ret)
  387. return ret;
  388. if (enable)
  389. lbs_add_mesh(priv);
  390. else
  391. lbs_remove_mesh(priv);
  392. return count;
  393. }
  394. /**
  395. * lbs_mesh attribute to be exported per ethX interface
  396. * through sysfs (/sys/class/net/ethX/lbs_mesh)
  397. */
  398. static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
  399. /**
  400. * anycast_mask attribute to be exported per mshX interface
  401. * through sysfs (/sys/class/net/mshX/anycast_mask)
  402. */
  403. static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
  404. /**
  405. * prb_rsp_limit attribute to be exported per mshX interface
  406. * through sysfs (/sys/class/net/mshX/prb_rsp_limit)
  407. */
  408. static DEVICE_ATTR(prb_rsp_limit, 0644, lbs_prb_rsp_limit_get,
  409. lbs_prb_rsp_limit_set);
  410. static struct attribute *lbs_mesh_sysfs_entries[] = {
  411. &dev_attr_anycast_mask.attr,
  412. &dev_attr_prb_rsp_limit.attr,
  413. NULL,
  414. };
  415. static struct attribute_group lbs_mesh_attr_group = {
  416. .attrs = lbs_mesh_sysfs_entries,
  417. };
  418. /**
  419. * @brief This function opens the ethX or mshX interface
  420. *
  421. * @param dev A pointer to net_device structure
  422. * @return 0 or -EBUSY if monitor mode active
  423. */
  424. static int lbs_dev_open(struct net_device *dev)
  425. {
  426. struct lbs_private *priv = dev->ml_priv;
  427. int ret = 0;
  428. lbs_deb_enter(LBS_DEB_NET);
  429. spin_lock_irq(&priv->driver_lock);
  430. if (priv->monitormode) {
  431. ret = -EBUSY;
  432. goto out;
  433. }
  434. if (dev == priv->mesh_dev) {
  435. priv->mesh_open = 1;
  436. priv->mesh_connect_status = LBS_CONNECTED;
  437. netif_carrier_on(dev);
  438. } else {
  439. priv->infra_open = 1;
  440. if (priv->connect_status == LBS_CONNECTED)
  441. netif_carrier_on(dev);
  442. else
  443. netif_carrier_off(dev);
  444. }
  445. if (!priv->tx_pending_len)
  446. netif_wake_queue(dev);
  447. out:
  448. spin_unlock_irq(&priv->driver_lock);
  449. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  450. return ret;
  451. }
  452. /**
  453. * @brief This function closes the mshX interface
  454. *
  455. * @param dev A pointer to net_device structure
  456. * @return 0
  457. */
  458. static int lbs_mesh_stop(struct net_device *dev)
  459. {
  460. struct lbs_private *priv = dev->ml_priv;
  461. lbs_deb_enter(LBS_DEB_MESH);
  462. spin_lock_irq(&priv->driver_lock);
  463. priv->mesh_open = 0;
  464. priv->mesh_connect_status = LBS_DISCONNECTED;
  465. netif_stop_queue(dev);
  466. netif_carrier_off(dev);
  467. spin_unlock_irq(&priv->driver_lock);
  468. schedule_work(&priv->mcast_work);
  469. lbs_deb_leave(LBS_DEB_MESH);
  470. return 0;
  471. }
  472. /**
  473. * @brief This function closes the ethX interface
  474. *
  475. * @param dev A pointer to net_device structure
  476. * @return 0
  477. */
  478. static int lbs_eth_stop(struct net_device *dev)
  479. {
  480. struct lbs_private *priv = dev->ml_priv;
  481. lbs_deb_enter(LBS_DEB_NET);
  482. spin_lock_irq(&priv->driver_lock);
  483. priv->infra_open = 0;
  484. netif_stop_queue(dev);
  485. spin_unlock_irq(&priv->driver_lock);
  486. schedule_work(&priv->mcast_work);
  487. lbs_deb_leave(LBS_DEB_NET);
  488. return 0;
  489. }
  490. static void lbs_tx_timeout(struct net_device *dev)
  491. {
  492. struct lbs_private *priv = dev->ml_priv;
  493. lbs_deb_enter(LBS_DEB_TX);
  494. lbs_pr_err("tx watch dog timeout\n");
  495. dev->trans_start = jiffies;
  496. if (priv->currenttxskb)
  497. lbs_send_tx_feedback(priv, 0);
  498. /* XX: Shouldn't we also call into the hw-specific driver
  499. to kick it somehow? */
  500. lbs_host_to_card_done(priv);
  501. /* More often than not, this actually happens because the
  502. firmware has crapped itself -- rather than just a very
  503. busy medium. So send a harmless command, and if/when
  504. _that_ times out, we'll kick it in the head. */
  505. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  506. 0, 0, NULL);
  507. lbs_deb_leave(LBS_DEB_TX);
  508. }
  509. void lbs_host_to_card_done(struct lbs_private *priv)
  510. {
  511. unsigned long flags;
  512. lbs_deb_enter(LBS_DEB_THREAD);
  513. spin_lock_irqsave(&priv->driver_lock, flags);
  514. priv->dnld_sent = DNLD_RES_RECEIVED;
  515. /* Wake main thread if commands are pending */
  516. if (!priv->cur_cmd || priv->tx_pending_len > 0)
  517. wake_up_interruptible(&priv->waitq);
  518. spin_unlock_irqrestore(&priv->driver_lock, flags);
  519. lbs_deb_leave(LBS_DEB_THREAD);
  520. }
  521. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  522. static int lbs_set_mac_address(struct net_device *dev, void *addr)
  523. {
  524. int ret = 0;
  525. struct lbs_private *priv = dev->ml_priv;
  526. struct sockaddr *phwaddr = addr;
  527. struct cmd_ds_802_11_mac_address cmd;
  528. lbs_deb_enter(LBS_DEB_NET);
  529. /* In case it was called from the mesh device */
  530. dev = priv->dev;
  531. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  532. cmd.action = cpu_to_le16(CMD_ACT_SET);
  533. memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
  534. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  535. if (ret) {
  536. lbs_deb_net("set MAC address failed\n");
  537. goto done;
  538. }
  539. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  540. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  541. if (priv->mesh_dev)
  542. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  543. done:
  544. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  545. return ret;
  546. }
  547. static inline int mac_in_list(unsigned char *list, int list_len,
  548. unsigned char *mac)
  549. {
  550. while (list_len) {
  551. if (!memcmp(list, mac, ETH_ALEN))
  552. return 1;
  553. list += ETH_ALEN;
  554. list_len--;
  555. }
  556. return 0;
  557. }
  558. static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
  559. struct net_device *dev, int nr_addrs)
  560. {
  561. int i = nr_addrs;
  562. struct dev_mc_list *mc_list;
  563. if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
  564. return nr_addrs;
  565. netif_addr_lock_bh(dev);
  566. for (mc_list = dev->mc_list; mc_list; mc_list = mc_list->next) {
  567. if (mac_in_list(cmd->maclist, nr_addrs, mc_list->dmi_addr)) {
  568. lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
  569. mc_list->dmi_addr);
  570. continue;
  571. }
  572. if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
  573. break;
  574. memcpy(&cmd->maclist[6*i], mc_list->dmi_addr, ETH_ALEN);
  575. lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
  576. mc_list->dmi_addr);
  577. i++;
  578. }
  579. netif_addr_unlock_bh(dev);
  580. if (mc_list)
  581. return -EOVERFLOW;
  582. return i;
  583. }
  584. static void lbs_set_mcast_worker(struct work_struct *work)
  585. {
  586. struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
  587. struct cmd_ds_mac_multicast_adr mcast_cmd;
  588. int dev_flags;
  589. int nr_addrs;
  590. int old_mac_control = priv->mac_control;
  591. lbs_deb_enter(LBS_DEB_NET);
  592. dev_flags = priv->dev->flags;
  593. if (priv->mesh_dev)
  594. dev_flags |= priv->mesh_dev->flags;
  595. if (dev_flags & IFF_PROMISC) {
  596. priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  597. priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  598. CMD_ACT_MAC_MULTICAST_ENABLE);
  599. goto out_set_mac_control;
  600. } else if (dev_flags & IFF_ALLMULTI) {
  601. do_allmulti:
  602. priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  603. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  604. CMD_ACT_MAC_MULTICAST_ENABLE);
  605. goto out_set_mac_control;
  606. }
  607. /* Once for priv->dev, again for priv->mesh_dev if it exists */
  608. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
  609. if (nr_addrs >= 0 && priv->mesh_dev)
  610. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
  611. if (nr_addrs < 0)
  612. goto do_allmulti;
  613. if (nr_addrs) {
  614. int size = offsetof(struct cmd_ds_mac_multicast_adr,
  615. maclist[6*nr_addrs]);
  616. mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
  617. mcast_cmd.hdr.size = cpu_to_le16(size);
  618. mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
  619. lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
  620. priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
  621. } else
  622. priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
  623. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  624. CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
  625. out_set_mac_control:
  626. if (priv->mac_control != old_mac_control)
  627. lbs_set_mac_control(priv);
  628. lbs_deb_leave(LBS_DEB_NET);
  629. }
  630. static void lbs_set_multicast_list(struct net_device *dev)
  631. {
  632. struct lbs_private *priv = dev->ml_priv;
  633. schedule_work(&priv->mcast_work);
  634. }
  635. /**
  636. * @brief This function handles the major jobs in the LBS driver.
  637. * It handles all events generated by firmware, RX data received
  638. * from firmware and TX data sent from kernel.
  639. *
  640. * @param data A pointer to lbs_thread structure
  641. * @return 0
  642. */
  643. static int lbs_thread(void *data)
  644. {
  645. struct net_device *dev = data;
  646. struct lbs_private *priv = dev->ml_priv;
  647. wait_queue_t wait;
  648. lbs_deb_enter(LBS_DEB_THREAD);
  649. init_waitqueue_entry(&wait, current);
  650. for (;;) {
  651. int shouldsleep;
  652. u8 resp_idx;
  653. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  654. priv->currenttxskb, priv->dnld_sent);
  655. add_wait_queue(&priv->waitq, &wait);
  656. set_current_state(TASK_INTERRUPTIBLE);
  657. spin_lock_irq(&priv->driver_lock);
  658. if (kthread_should_stop())
  659. shouldsleep = 0; /* Bye */
  660. else if (priv->surpriseremoved)
  661. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  662. else if (priv->psstate == PS_STATE_SLEEP)
  663. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  664. else if (priv->cmd_timed_out)
  665. shouldsleep = 0; /* Command timed out. Recover */
  666. else if (!priv->fw_ready)
  667. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  668. else if (priv->dnld_sent)
  669. shouldsleep = 1; /* Something is en route to the device already */
  670. else if (priv->tx_pending_len > 0)
  671. shouldsleep = 0; /* We've a packet to send */
  672. else if (priv->resp_len[priv->resp_idx])
  673. shouldsleep = 0; /* We have a command response */
  674. else if (priv->cur_cmd)
  675. shouldsleep = 1; /* Can't send a command; one already running */
  676. else if (!list_empty(&priv->cmdpendingq))
  677. shouldsleep = 0; /* We have a command to send */
  678. else if (__kfifo_len(priv->event_fifo))
  679. shouldsleep = 0; /* We have an event to process */
  680. else
  681. shouldsleep = 1; /* No command */
  682. if (shouldsleep) {
  683. lbs_deb_thread("sleeping, connect_status %d, "
  684. "psmode %d, psstate %d\n",
  685. priv->connect_status,
  686. priv->psmode, priv->psstate);
  687. spin_unlock_irq(&priv->driver_lock);
  688. schedule();
  689. } else
  690. spin_unlock_irq(&priv->driver_lock);
  691. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  692. priv->currenttxskb, priv->dnld_sent);
  693. set_current_state(TASK_RUNNING);
  694. remove_wait_queue(&priv->waitq, &wait);
  695. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  696. priv->currenttxskb, priv->dnld_sent);
  697. if (kthread_should_stop()) {
  698. lbs_deb_thread("break from main thread\n");
  699. break;
  700. }
  701. if (priv->surpriseremoved) {
  702. lbs_deb_thread("adapter removed; waiting to die...\n");
  703. continue;
  704. }
  705. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  706. priv->currenttxskb, priv->dnld_sent);
  707. /* Process any pending command response */
  708. spin_lock_irq(&priv->driver_lock);
  709. resp_idx = priv->resp_idx;
  710. if (priv->resp_len[resp_idx]) {
  711. spin_unlock_irq(&priv->driver_lock);
  712. lbs_process_command_response(priv,
  713. priv->resp_buf[resp_idx],
  714. priv->resp_len[resp_idx]);
  715. spin_lock_irq(&priv->driver_lock);
  716. priv->resp_len[resp_idx] = 0;
  717. }
  718. spin_unlock_irq(&priv->driver_lock);
  719. /* command timeout stuff */
  720. if (priv->cmd_timed_out && priv->cur_cmd) {
  721. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  722. if (++priv->nr_retries > 3) {
  723. lbs_pr_info("Excessive timeouts submitting "
  724. "command 0x%04x\n",
  725. le16_to_cpu(cmdnode->cmdbuf->command));
  726. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  727. priv->nr_retries = 0;
  728. if (priv->reset_card)
  729. priv->reset_card(priv);
  730. } else {
  731. priv->cur_cmd = NULL;
  732. priv->dnld_sent = DNLD_RES_RECEIVED;
  733. lbs_pr_info("requeueing command 0x%04x due "
  734. "to timeout (#%d)\n",
  735. le16_to_cpu(cmdnode->cmdbuf->command),
  736. priv->nr_retries);
  737. /* Stick it back at the _top_ of the pending queue
  738. for immediate resubmission */
  739. list_add(&cmdnode->list, &priv->cmdpendingq);
  740. }
  741. }
  742. priv->cmd_timed_out = 0;
  743. /* Process hardware events, e.g. card removed, link lost */
  744. spin_lock_irq(&priv->driver_lock);
  745. while (__kfifo_len(priv->event_fifo)) {
  746. u32 event;
  747. __kfifo_get(priv->event_fifo, (unsigned char *) &event,
  748. sizeof(event));
  749. spin_unlock_irq(&priv->driver_lock);
  750. lbs_process_event(priv, event);
  751. spin_lock_irq(&priv->driver_lock);
  752. }
  753. spin_unlock_irq(&priv->driver_lock);
  754. if (!priv->fw_ready)
  755. continue;
  756. /* Check if we need to confirm Sleep Request received previously */
  757. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  758. !priv->dnld_sent && !priv->cur_cmd) {
  759. if (priv->connect_status == LBS_CONNECTED) {
  760. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  761. "dnld_sent %d, cur_cmd %p\n",
  762. priv->currenttxskb, priv->dnld_sent,
  763. priv->cur_cmd);
  764. lbs_ps_confirm_sleep(priv);
  765. } else {
  766. /* workaround for firmware sending
  767. * deauth/linkloss event immediately
  768. * after sleep request; remove this
  769. * after firmware fixes it
  770. */
  771. priv->psstate = PS_STATE_AWAKE;
  772. lbs_pr_alert("ignore PS_SleepConfirm in "
  773. "non-connected state\n");
  774. }
  775. }
  776. /* The PS state is changed during processing of Sleep Request
  777. * event above
  778. */
  779. if ((priv->psstate == PS_STATE_SLEEP) ||
  780. (priv->psstate == PS_STATE_PRE_SLEEP))
  781. continue;
  782. /* Execute the next command */
  783. if (!priv->dnld_sent && !priv->cur_cmd)
  784. lbs_execute_next_command(priv);
  785. /* Wake-up command waiters which can't sleep in
  786. * lbs_prepare_and_send_command
  787. */
  788. if (!list_empty(&priv->cmdpendingq))
  789. wake_up_all(&priv->cmd_pending);
  790. spin_lock_irq(&priv->driver_lock);
  791. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  792. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  793. priv->tx_pending_buf,
  794. priv->tx_pending_len);
  795. if (ret) {
  796. lbs_deb_tx("host_to_card failed %d\n", ret);
  797. priv->dnld_sent = DNLD_RES_RECEIVED;
  798. }
  799. priv->tx_pending_len = 0;
  800. if (!priv->currenttxskb) {
  801. /* We can wake the queues immediately if we aren't
  802. waiting for TX feedback */
  803. if (priv->connect_status == LBS_CONNECTED)
  804. netif_wake_queue(priv->dev);
  805. if (priv->mesh_dev &&
  806. priv->mesh_connect_status == LBS_CONNECTED)
  807. netif_wake_queue(priv->mesh_dev);
  808. }
  809. }
  810. spin_unlock_irq(&priv->driver_lock);
  811. }
  812. del_timer(&priv->command_timer);
  813. wake_up_all(&priv->cmd_pending);
  814. lbs_deb_leave(LBS_DEB_THREAD);
  815. return 0;
  816. }
  817. static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
  818. struct cmd_header *cmd)
  819. {
  820. lbs_deb_enter(LBS_DEB_FW);
  821. netif_device_detach(priv->dev);
  822. if (priv->mesh_dev)
  823. netif_device_detach(priv->mesh_dev);
  824. priv->fw_ready = 0;
  825. lbs_deb_leave(LBS_DEB_FW);
  826. return 0;
  827. }
  828. int lbs_suspend(struct lbs_private *priv)
  829. {
  830. struct cmd_header cmd;
  831. int ret;
  832. lbs_deb_enter(LBS_DEB_FW);
  833. if (priv->wol_criteria == 0xffffffff) {
  834. lbs_pr_info("Suspend attempt without configuring wake params!\n");
  835. return -EINVAL;
  836. }
  837. memset(&cmd, 0, sizeof(cmd));
  838. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
  839. sizeof(cmd), lbs_suspend_callback, 0);
  840. if (ret)
  841. lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
  842. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  843. return ret;
  844. }
  845. EXPORT_SYMBOL_GPL(lbs_suspend);
  846. void lbs_resume(struct lbs_private *priv)
  847. {
  848. lbs_deb_enter(LBS_DEB_FW);
  849. priv->fw_ready = 1;
  850. /* Firmware doesn't seem to give us RX packets any more
  851. until we send it some command. Might as well update */
  852. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  853. 0, 0, NULL);
  854. netif_device_attach(priv->dev);
  855. if (priv->mesh_dev)
  856. netif_device_attach(priv->mesh_dev);
  857. lbs_deb_leave(LBS_DEB_FW);
  858. }
  859. EXPORT_SYMBOL_GPL(lbs_resume);
  860. /**
  861. * @brief This function gets the HW spec from the firmware and sets
  862. * some basic parameters.
  863. *
  864. * @param priv A pointer to struct lbs_private structure
  865. * @return 0 or -1
  866. */
  867. static int lbs_setup_firmware(struct lbs_private *priv)
  868. {
  869. int ret = -1;
  870. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  871. lbs_deb_enter(LBS_DEB_FW);
  872. /* Read MAC address from firmware */
  873. memset(priv->current_addr, 0xff, ETH_ALEN);
  874. ret = lbs_update_hw_spec(priv);
  875. if (ret)
  876. goto done;
  877. // lbs_set_antenna(priv,&ret,0);
  878. // printk("RF_ANT: %d, set to 3\n",ret);
  879. // ret=0xffff;
  880. // lbs_set_antenna(priv,&ret,1);
  881. // lbs_set_antenna(priv,&ret,0);
  882. // printk("RF_ANT after: %d\n",ret);
  883. // From the Iphone IOS: !!!!!!!
  884. // lbs_set_mac_reg(priv, 0xA240, 0xA3000);
  885. lbs_set_mac_reg(priv, 0xA5AC, 0xC8);
  886. lbs_set_mac_reg(priv, 0xA5B0, 0xC8 << 2);
  887. lbs_set_mac_reg(priv, 0xA5A8, 0xAF << 3);
  888. lbs_set_mac_reg(priv, 0xA5B4, 0xAF << 4);
  889. lbs_set_mac_reg(priv, 0xA5A4, 0xAF << 4);
  890. lbs_set_mac_reg(priv, 0xA58C, 0x40214);
  891. lbs_set_mac_reg(priv, 0xA5A0, 0x524D);
  892. lbs_set_mac_reg(priv, 0xA5F0, 0xA2271814);
  893. /* Read power levels if available */
  894. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  895. if (ret == 0) {
  896. priv->txpower_cur = curlevel;
  897. priv->txpower_min = minlevel;
  898. priv->txpower_max = maxlevel;
  899. }
  900. printk("txpower:%d min:%d max:%d\n",curlevel,minlevel,maxlevel);
  901. lbs_set_mac_control(priv);
  902. lbs_set_rf_reg(priv, 0x50, 0x50);
  903. lbs_set_rf_reg(priv, 0x6B, 0xAE);
  904. lbs_set_bbp_reg(priv, 0x4E, 0x1B);
  905. // lbs_get_mac_reg(priv, 0x2048, &macv);
  906. // lbs_set_mac_reg(priv, 0x2048, macv | 0x80);
  907. done:
  908. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  909. return ret;
  910. }
  911. /**
  912. * This function handles the timeout of command sending.
  913. * It will re-send the same command again.
  914. */
  915. static void command_timer_fn(unsigned long data)
  916. {
  917. struct lbs_private *priv = (struct lbs_private *)data;
  918. unsigned long flags;
  919. lbs_deb_enter(LBS_DEB_CMD);
  920. spin_lock_irqsave(&priv->driver_lock, flags);
  921. if (!priv->cur_cmd)
  922. goto out;
  923. lbs_pr_info("command 0x%04x timed out\n",
  924. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  925. priv->cmd_timed_out = 1;
  926. wake_up_interruptible(&priv->waitq);
  927. out:
  928. spin_unlock_irqrestore(&priv->driver_lock, flags);
  929. lbs_deb_leave(LBS_DEB_CMD);
  930. }
  931. static void lbs_sync_channel_worker(struct work_struct *work)
  932. {
  933. struct lbs_private *priv = container_of(work, struct lbs_private,
  934. sync_channel);
  935. lbs_deb_enter(LBS_DEB_MAIN);
  936. if (lbs_update_channel(priv))
  937. lbs_pr_info("Channel synchronization failed.");
  938. lbs_deb_leave(LBS_DEB_MAIN);
  939. }
  940. static int lbs_init_adapter(struct lbs_private *priv)
  941. {
  942. size_t bufsize;
  943. int i, ret = 0;
  944. lbs_deb_enter(LBS_DEB_MAIN);
  945. /* Allocate buffer to store the BSSID list */
  946. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  947. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  948. if (!priv->networks) {
  949. lbs_pr_err("Out of memory allocating beacons\n");
  950. ret = -1;
  951. goto out;
  952. }
  953. /* Initialize scan result lists */
  954. INIT_LIST_HEAD(&priv->network_free_list);
  955. INIT_LIST_HEAD(&priv->network_list);
  956. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  957. list_add_tail(&priv->networks[i].list,
  958. &priv->network_free_list);
  959. }
  960. memset(priv->current_addr, 0xff, ETH_ALEN);
  961. priv->connect_status = LBS_DISCONNECTED;
  962. priv->mesh_connect_status = LBS_DISCONNECTED;
  963. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  964. priv->mode = IW_MODE_INFRA;
  965. priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  966. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  967. priv->radio_on = 1;
  968. priv->enablehwauto = 1;
  969. priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  970. priv->psmode = LBS802_11POWERMODECAM;
  971. priv->psstate = PS_STATE_FULL_POWER;
  972. mutex_init(&priv->lock);
  973. setup_timer(&priv->command_timer, command_timer_fn,
  974. (unsigned long)priv);
  975. INIT_LIST_HEAD(&priv->cmdfreeq);
  976. INIT_LIST_HEAD(&priv->cmdpendingq);
  977. spin_lock_init(&priv->driver_lock);
  978. init_waitqueue_head(&priv->cmd_pending);
  979. /* Allocate the command buffers */
  980. if (lbs_allocate_cmd_buffer(priv)) {
  981. lbs_pr_err("Out of memory allocating command buffers\n");
  982. ret = -ENOMEM;
  983. goto out;
  984. }
  985. priv->resp_idx = 0;
  986. priv->resp_len[0] = priv->resp_len[1] = 0;
  987. /* Create the event FIFO */
  988. priv->event_fifo = kfifo_alloc(sizeof(u32) * 16, GFP_KERNEL, NULL);
  989. if (IS_ERR(priv->event_fifo)) {
  990. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  991. ret = -ENOMEM;
  992. goto out;
  993. }
  994. out:
  995. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  996. return ret;
  997. }
  998. static void lbs_free_adapter(struct lbs_private *priv)
  999. {
  1000. lbs_deb_enter(LBS_DEB_MAIN);
  1001. lbs_free_cmd_buffer(priv);
  1002. if (priv->event_fifo)
  1003. kfifo_free(priv->event_fifo);
  1004. del_timer(&priv->command_timer);
  1005. kfree(priv->networks);
  1006. priv->networks = NULL;
  1007. lbs_deb_leave(LBS_DEB_MAIN);
  1008. }
  1009. static const struct net_device_ops lbs_netdev_ops = {
  1010. .ndo_open = lbs_dev_open,
  1011. .ndo_stop = lbs_eth_stop,
  1012. .ndo_start_xmit = lbs_hard_start_xmit,
  1013. .ndo_set_mac_address = lbs_set_mac_address,
  1014. .ndo_tx_timeout = lbs_tx_timeout,
  1015. .ndo_set_multicast_list = lbs_set_multicast_list,
  1016. .ndo_change_mtu = eth_change_mtu,
  1017. .ndo_validate_addr = eth_validate_addr,
  1018. };
  1019. /**
  1020. * @brief This function adds the card. it will probe the
  1021. * card, allocate the lbs_priv and initialize the device.
  1022. *
  1023. * @param card A pointer to card
  1024. * @return A pointer to struct lbs_private structure
  1025. */
  1026. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  1027. {
  1028. struct net_device *dev = NULL;
  1029. struct lbs_private *priv = NULL;
  1030. lbs_deb_enter(LBS_DEB_MAIN);
  1031. /* Allocate an Ethernet device and register it */
  1032. dev = alloc_etherdev(sizeof(struct lbs_private));
  1033. if (!dev) {
  1034. lbs_pr_err("init wlanX device failed\n");
  1035. goto done;
  1036. }
  1037. priv = netdev_priv(dev);
  1038. dev->ml_priv = priv;
  1039. if (lbs_init_adapter(priv)) {
  1040. lbs_pr_err("failed to initialize adapter structure.\n");
  1041. goto err_init_adapter;
  1042. }
  1043. priv->dev = dev;
  1044. priv->card = card;
  1045. priv->mesh_open = 0;
  1046. priv->infra_open = 0;
  1047. /* Setup the OS Interface to our functions */
  1048. dev->netdev_ops = &lbs_netdev_ops;
  1049. dev->watchdog_timeo = 5 * HZ;
  1050. dev->ethtool_ops = &lbs_ethtool_ops;
  1051. #ifdef WIRELESS_EXT
  1052. dev->wireless_handlers = &lbs_handler_def;
  1053. #endif
  1054. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1055. SET_NETDEV_DEV(dev, dmdev);
  1056. priv->rtap_net_dev = NULL;
  1057. strcpy(dev->name, "wlan%d");
  1058. lbs_deb_thread("Starting main thread...\n");
  1059. init_waitqueue_head(&priv->waitq);
  1060. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  1061. if (IS_ERR(priv->main_thread)) {
  1062. lbs_deb_thread("Error creating main thread.\n");
  1063. goto err_init_adapter;
  1064. }
  1065. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  1066. INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
  1067. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  1068. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  1069. INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
  1070. sprintf(priv->mesh_ssid, "mesh");
  1071. priv->mesh_ssid_len = 4;
  1072. priv->wol_criteria = 0xffffffff;
  1073. priv->wol_gpio = 0xff;
  1074. goto done;
  1075. err_init_adapter:
  1076. lbs_free_adapter(priv);
  1077. free_netdev(dev);
  1078. priv = NULL;
  1079. done:
  1080. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  1081. return priv;
  1082. }
  1083. EXPORT_SYMBOL_GPL(lbs_add_card);
  1084. void lbs_remove_card(struct lbs_private *priv)
  1085. {
  1086. struct net_device *dev = priv->dev;
  1087. union iwreq_data wrqu;
  1088. lbs_deb_enter(LBS_DEB_MAIN);
  1089. lbs_remove_mesh(priv);
  1090. lbs_remove_rtap(priv);
  1091. dev = priv->dev;
  1092. cancel_delayed_work_sync(&priv->scan_work);
  1093. cancel_delayed_work_sync(&priv->assoc_work);
  1094. cancel_work_sync(&priv->mcast_work);
  1095. /* worker thread destruction blocks on the in-flight command which
  1096. * should have been cleared already in lbs_stop_card().
  1097. */
  1098. lbs_deb_main("destroying worker thread\n");
  1099. destroy_workqueue(priv->work_thread);
  1100. lbs_deb_main("done destroying worker thread\n");
  1101. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  1102. priv->psmode = LBS802_11POWERMODECAM;
  1103. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  1104. }
  1105. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  1106. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1107. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1108. /* Stop the thread servicing the interrupts */
  1109. priv->surpriseremoved = 1;
  1110. kthread_stop(priv->main_thread);
  1111. lbs_free_adapter(priv);
  1112. priv->dev = NULL;
  1113. free_netdev(dev);
  1114. lbs_deb_leave(LBS_DEB_MAIN);
  1115. }
  1116. EXPORT_SYMBOL_GPL(lbs_remove_card);
  1117. int lbs_start_card(struct lbs_private *priv)
  1118. {
  1119. struct net_device *dev = priv->dev;
  1120. int ret = -1;
  1121. lbs_deb_enter(LBS_DEB_MAIN);
  1122. /* poke the firmware */
  1123. ret = lbs_setup_firmware(priv);
  1124. if (ret)
  1125. goto done;
  1126. /* init 802.11d */
  1127. lbs_init_11d(priv);
  1128. if (register_netdev(dev)) {
  1129. lbs_pr_err("cannot register ethX device\n");
  1130. goto done;
  1131. }
  1132. lbs_update_channel(priv);
  1133. /* Check mesh FW version and appropriately send the mesh start
  1134. * command
  1135. */
  1136. if (priv->mesh_fw_ver == MESH_FW_OLD) {
  1137. /* Enable mesh, if supported, and work out which TLV it uses.
  1138. 0x100 + 291 is an unofficial value used in 5.110.20.pXX
  1139. 0x100 + 37 is the official value used in 5.110.21.pXX
  1140. but we check them in that order because 20.pXX doesn't
  1141. give an error -- it just silently fails. */
  1142. /* 5.110.20.pXX firmware will fail the command if the channel
  1143. doesn't match the existing channel. But only if the TLV
  1144. is correct. If the channel is wrong, _BOTH_ versions will
  1145. give an error to 0x100+291, and allow 0x100+37 to succeed.
  1146. It's just that 5.110.20.pXX will not have done anything
  1147. useful */
  1148. priv->mesh_tlv = TLV_TYPE_OLD_MESH_ID;
  1149. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  1150. priv->curbssparams.channel)) {
  1151. priv->mesh_tlv = TLV_TYPE_MESH_ID;
  1152. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  1153. priv->curbssparams.channel))
  1154. priv->mesh_tlv = 0;
  1155. }
  1156. } else if (priv->mesh_fw_ver == MESH_FW_NEW) {
  1157. /* 10.0.0.pXX new firmwares should succeed with TLV
  1158. * 0x100+37; Do not invoke command with old TLV.
  1159. */
  1160. priv->mesh_tlv = TLV_TYPE_MESH_ID;
  1161. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  1162. priv->curbssparams.channel))
  1163. priv->mesh_tlv = 0;
  1164. }
  1165. if (priv->mesh_tlv) {
  1166. lbs_add_mesh(priv);
  1167. if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
  1168. lbs_pr_err("cannot register lbs_mesh attribute\n");
  1169. /* While rtap isn't related to mesh, only mesh-enabled
  1170. * firmware implements the rtap functionality via
  1171. * CMD_802_11_MONITOR_MODE.
  1172. */
  1173. if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
  1174. lbs_pr_err("cannot register lbs_rtap attribute\n");
  1175. }
  1176. lbs_debugfs_init_one(priv, dev);
  1177. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  1178. ret = 0;
  1179. done:
  1180. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1181. return ret;
  1182. }
  1183. EXPORT_SYMBOL_GPL(lbs_start_card);
  1184. void lbs_stop_card(struct lbs_private *priv)
  1185. {
  1186. struct net_device *dev;
  1187. struct cmd_ctrl_node *cmdnode;
  1188. unsigned long flags;
  1189. lbs_deb_enter(LBS_DEB_MAIN);
  1190. if (!priv)
  1191. goto out;
  1192. dev = priv->dev;
  1193. netif_stop_queue(dev);
  1194. netif_carrier_off(dev);
  1195. lbs_debugfs_remove_one(priv);
  1196. if (priv->mesh_tlv) {
  1197. device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
  1198. device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
  1199. }
  1200. /* Delete the timeout of the currently processing command */
  1201. del_timer_sync(&priv->command_timer);
  1202. /* Flush pending command nodes */
  1203. spin_lock_irqsave(&priv->driver_lock, flags);
  1204. lbs_deb_main("clearing pending commands\n");
  1205. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  1206. cmdnode->result = -ENOENT;
  1207. cmdnode->cmdwaitqwoken = 1;
  1208. wake_up_interruptible(&cmdnode->cmdwait_q);
  1209. }
  1210. /* Flush the command the card is currently processing */
  1211. if (priv->cur_cmd) {
  1212. lbs_deb_main("clearing current command\n");
  1213. priv->cur_cmd->result = -ENOENT;
  1214. priv->cur_cmd->cmdwaitqwoken = 1;
  1215. wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
  1216. }
  1217. lbs_deb_main("done clearing commands\n");
  1218. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1219. unregister_netdev(dev);
  1220. out:
  1221. lbs_deb_leave(LBS_DEB_MAIN);
  1222. }
  1223. EXPORT_SYMBOL_GPL(lbs_stop_card);
  1224. static const struct net_device_ops mesh_netdev_ops = {
  1225. .ndo_open = lbs_dev_open,
  1226. .ndo_stop = lbs_mesh_stop,
  1227. .ndo_start_xmit = lbs_hard_start_xmit,
  1228. .ndo_set_mac_address = lbs_set_mac_address,
  1229. .ndo_set_multicast_list = lbs_set_multicast_list,
  1230. };
  1231. /**
  1232. * @brief This function adds mshX interface
  1233. *
  1234. * @param priv A pointer to the struct lbs_private structure
  1235. * @return 0 if successful, -X otherwise
  1236. */
  1237. static int lbs_add_mesh(struct lbs_private *priv)
  1238. {
  1239. struct net_device *mesh_dev = NULL;
  1240. int ret = 0;
  1241. lbs_deb_enter(LBS_DEB_MESH);
  1242. /* Allocate a virtual mesh device */
  1243. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1244. lbs_deb_mesh("init mshX device failed\n");
  1245. ret = -ENOMEM;
  1246. goto done;
  1247. }
  1248. mesh_dev->ml_priv = priv;
  1249. priv->mesh_dev = mesh_dev;
  1250. mesh_dev->netdev_ops = &mesh_netdev_ops;
  1251. mesh_dev->ethtool_ops = &lbs_ethtool_ops;
  1252. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1253. sizeof(priv->dev->dev_addr));
  1254. SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
  1255. #ifdef WIRELESS_EXT
  1256. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1257. #endif
  1258. mesh_dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1259. /* Register virtual mesh interface */
  1260. ret = register_netdev(mesh_dev);
  1261. if (ret) {
  1262. lbs_pr_err("cannot register mshX virtual interface\n");
  1263. goto err_free;
  1264. }
  1265. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1266. if (ret)
  1267. goto err_unregister;
  1268. lbs_persist_config_init(mesh_dev);
  1269. /* Everything successful */
  1270. ret = 0;
  1271. goto done;
  1272. err_unregister:
  1273. unregister_netdev(mesh_dev);
  1274. err_free:
  1275. free_netdev(mesh_dev);
  1276. done:
  1277. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1278. return ret;
  1279. }
  1280. static void lbs_remove_mesh(struct lbs_private *priv)
  1281. {
  1282. struct net_device *mesh_dev;
  1283. mesh_dev = priv->mesh_dev;
  1284. if (!mesh_dev)
  1285. return;
  1286. lbs_deb_enter(LBS_DEB_MESH);
  1287. netif_stop_queue(mesh_dev);
  1288. netif_carrier_off(mesh_dev);
  1289. sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1290. lbs_persist_config_remove(mesh_dev);
  1291. unregister_netdev(mesh_dev);
  1292. priv->mesh_dev = NULL;
  1293. free_netdev(mesh_dev);
  1294. lbs_deb_leave(LBS_DEB_MESH);
  1295. }
  1296. /**
  1297. * @brief This function finds the CFP in
  1298. * region_cfp_table based on region and band parameter.
  1299. *
  1300. * @param region The region code
  1301. * @param band The band
  1302. * @param cfp_no A pointer to CFP number
  1303. * @return A pointer to CFP
  1304. */
  1305. struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
  1306. {
  1307. int i, end;
  1308. lbs_deb_enter(LBS_DEB_MAIN);
  1309. end = ARRAY_SIZE(region_cfp_table);
  1310. for (i = 0; i < end ; i++) {
  1311. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1312. region_cfp_table[i].region);
  1313. if (region_cfp_table[i].region == region) {
  1314. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1315. lbs_deb_leave(LBS_DEB_MAIN);
  1316. return region_cfp_table[i].cfp_BG;
  1317. }
  1318. }
  1319. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1320. return NULL;
  1321. }
  1322. int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
  1323. {
  1324. int ret = 0;
  1325. int i = 0;
  1326. struct chan_freq_power *cfp;
  1327. int cfp_no;
  1328. lbs_deb_enter(LBS_DEB_MAIN);
  1329. memset(priv->region_channel, 0, sizeof(priv->region_channel));
  1330. cfp = lbs_get_region_cfp_table(region, &cfp_no);
  1331. if (cfp != NULL) {
  1332. priv->region_channel[i].nrcfp = cfp_no;
  1333. priv->region_channel[i].CFP = cfp;
  1334. } else {
  1335. lbs_deb_main("wrong region code %#x in band B/G\n",
  1336. region);
  1337. ret = -1;
  1338. goto out;
  1339. }
  1340. priv->region_channel[i].valid = 1;
  1341. priv->region_channel[i].region = region;
  1342. priv->region_channel[i].band = band;
  1343. i++;
  1344. out:
  1345. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1346. return ret;
  1347. }
  1348. void lbs_queue_event(struct lbs_private *priv, u32 event)
  1349. {
  1350. unsigned long flags;
  1351. lbs_deb_enter(LBS_DEB_THREAD);
  1352. spin_lock_irqsave(&priv->driver_lock, flags);
  1353. if (priv->psstate == PS_STATE_SLEEP)
  1354. priv->psstate = PS_STATE_AWAKE;
  1355. __kfifo_put(priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  1356. wake_up_interruptible(&priv->waitq);
  1357. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1358. lbs_deb_leave(LBS_DEB_THREAD);
  1359. }
  1360. EXPORT_SYMBOL_GPL(lbs_queue_event);
  1361. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  1362. {
  1363. lbs_deb_enter(LBS_DEB_THREAD);
  1364. if (priv->psstate == PS_STATE_SLEEP)
  1365. priv->psstate = PS_STATE_AWAKE;
  1366. /* Swap buffers by flipping the response index */
  1367. BUG_ON(resp_idx > 1);
  1368. priv->resp_idx = resp_idx;
  1369. wake_up_interruptible(&priv->waitq);
  1370. lbs_deb_leave(LBS_DEB_THREAD);
  1371. }
  1372. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  1373. static int __init lbs_init_module(void)
  1374. {
  1375. lbs_deb_enter(LBS_DEB_MAIN);
  1376. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  1377. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  1378. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  1379. confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  1380. lbs_debugfs_init();
  1381. lbs_deb_leave(LBS_DEB_MAIN);
  1382. return 0;
  1383. }
  1384. static void __exit lbs_exit_module(void)
  1385. {
  1386. lbs_deb_enter(LBS_DEB_MAIN);
  1387. lbs_debugfs_remove();
  1388. lbs_deb_leave(LBS_DEB_MAIN);
  1389. }
  1390. /*
  1391. * rtap interface support fuctions
  1392. */
  1393. static int lbs_rtap_open(struct net_device *dev)
  1394. {
  1395. /* Yes, _stop_ the queue. Because we don't support injection */
  1396. lbs_deb_enter(LBS_DEB_MAIN);
  1397. netif_carrier_off(dev);
  1398. netif_stop_queue(dev);
  1399. lbs_deb_leave(LBS_DEB_LEAVE);
  1400. return 0;
  1401. }
  1402. static int lbs_rtap_stop(struct net_device *dev)
  1403. {
  1404. lbs_deb_enter(LBS_DEB_MAIN);
  1405. lbs_deb_leave(LBS_DEB_MAIN);
  1406. return 0;
  1407. }
  1408. static netdev_tx_t lbs_rtap_hard_start_xmit(struct sk_buff *skb,
  1409. struct net_device *dev)
  1410. {
  1411. netif_stop_queue(dev);
  1412. return NETDEV_TX_BUSY;
  1413. }
  1414. static void lbs_remove_rtap(struct lbs_private *priv)
  1415. {
  1416. lbs_deb_enter(LBS_DEB_MAIN);
  1417. if (priv->rtap_net_dev == NULL)
  1418. goto out;
  1419. unregister_netdev(priv->rtap_net_dev);
  1420. free_netdev(priv->rtap_net_dev);
  1421. priv->rtap_net_dev = NULL;
  1422. out:
  1423. lbs_deb_leave(LBS_DEB_MAIN);
  1424. }
  1425. static const struct net_device_ops rtap_netdev_ops = {
  1426. .ndo_open = lbs_rtap_open,
  1427. .ndo_stop = lbs_rtap_stop,
  1428. .ndo_start_xmit = lbs_rtap_hard_start_xmit,
  1429. };
  1430. static int lbs_add_rtap(struct lbs_private *priv)
  1431. {
  1432. int ret = 0;
  1433. struct net_device *rtap_dev;
  1434. lbs_deb_enter(LBS_DEB_MAIN);
  1435. if (priv->rtap_net_dev) {
  1436. ret = -EPERM;
  1437. goto out;
  1438. }
  1439. rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
  1440. if (rtap_dev == NULL) {
  1441. ret = -ENOMEM;
  1442. goto out;
  1443. }
  1444. memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
  1445. rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1446. rtap_dev->netdev_ops = &rtap_netdev_ops;
  1447. rtap_dev->ml_priv = priv;
  1448. SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
  1449. ret = register_netdev(rtap_dev);
  1450. if (ret) {
  1451. free_netdev(rtap_dev);
  1452. goto out;
  1453. }
  1454. priv->rtap_net_dev = rtap_dev;
  1455. out:
  1456. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1457. return ret;
  1458. }
  1459. module_init(lbs_init_module);
  1460. module_exit(lbs_exit_module);
  1461. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1462. MODULE_AUTHOR("Marvell International Ltd.");
  1463. MODULE_LICENSE("GPL");