main.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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/delay.h>
  7. #include <linux/freezer.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/if_arp.h>
  11. #include <net/iw_handler.h>
  12. #include "host.h"
  13. #include "sbi.h"
  14. #include "decl.h"
  15. #include "dev.h"
  16. #include "fw.h"
  17. #include "wext.h"
  18. #include "debugfs.h"
  19. #include "assoc.h"
  20. #define DRIVER_RELEASE_VERSION "320.p0"
  21. const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  22. #ifdef DEBUG
  23. "-dbg"
  24. #endif
  25. "";
  26. #ifdef ENABLE_PM
  27. static struct pm_dev *wlan_pm_dev = NULL;
  28. #endif
  29. #define WLAN_TX_PWR_DEFAULT 20 /*100mW */
  30. #define WLAN_TX_PWR_US_DEFAULT 20 /*100mW */
  31. #define WLAN_TX_PWR_JP_DEFAULT 16 /*50mW */
  32. #define WLAN_TX_PWR_FR_DEFAULT 20 /*100mW */
  33. #define WLAN_TX_PWR_EMEA_DEFAULT 20 /*100mW */
  34. /* Format { channel, frequency (MHz), maxtxpower } */
  35. /* band: 'B/G', region: USA FCC/Canada IC */
  36. static struct chan_freq_power channel_freq_power_US_BG[] = {
  37. {1, 2412, WLAN_TX_PWR_US_DEFAULT},
  38. {2, 2417, WLAN_TX_PWR_US_DEFAULT},
  39. {3, 2422, WLAN_TX_PWR_US_DEFAULT},
  40. {4, 2427, WLAN_TX_PWR_US_DEFAULT},
  41. {5, 2432, WLAN_TX_PWR_US_DEFAULT},
  42. {6, 2437, WLAN_TX_PWR_US_DEFAULT},
  43. {7, 2442, WLAN_TX_PWR_US_DEFAULT},
  44. {8, 2447, WLAN_TX_PWR_US_DEFAULT},
  45. {9, 2452, WLAN_TX_PWR_US_DEFAULT},
  46. {10, 2457, WLAN_TX_PWR_US_DEFAULT},
  47. {11, 2462, WLAN_TX_PWR_US_DEFAULT}
  48. };
  49. /* band: 'B/G', region: Europe ETSI */
  50. static struct chan_freq_power channel_freq_power_EU_BG[] = {
  51. {1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
  52. {2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
  53. {3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
  54. {4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
  55. {5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
  56. {6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
  57. {7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
  58. {8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
  59. {9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
  60. {10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
  61. {11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
  62. {12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
  63. {13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
  64. };
  65. /* band: 'B/G', region: Spain */
  66. static struct chan_freq_power channel_freq_power_SPN_BG[] = {
  67. {10, 2457, WLAN_TX_PWR_DEFAULT},
  68. {11, 2462, WLAN_TX_PWR_DEFAULT}
  69. };
  70. /* band: 'B/G', region: France */
  71. static struct chan_freq_power channel_freq_power_FR_BG[] = {
  72. {10, 2457, WLAN_TX_PWR_FR_DEFAULT},
  73. {11, 2462, WLAN_TX_PWR_FR_DEFAULT},
  74. {12, 2467, WLAN_TX_PWR_FR_DEFAULT},
  75. {13, 2472, WLAN_TX_PWR_FR_DEFAULT}
  76. };
  77. /* band: 'B/G', region: Japan */
  78. static struct chan_freq_power channel_freq_power_JPN_BG[] = {
  79. {1, 2412, WLAN_TX_PWR_JP_DEFAULT},
  80. {2, 2417, WLAN_TX_PWR_JP_DEFAULT},
  81. {3, 2422, WLAN_TX_PWR_JP_DEFAULT},
  82. {4, 2427, WLAN_TX_PWR_JP_DEFAULT},
  83. {5, 2432, WLAN_TX_PWR_JP_DEFAULT},
  84. {6, 2437, WLAN_TX_PWR_JP_DEFAULT},
  85. {7, 2442, WLAN_TX_PWR_JP_DEFAULT},
  86. {8, 2447, WLAN_TX_PWR_JP_DEFAULT},
  87. {9, 2452, WLAN_TX_PWR_JP_DEFAULT},
  88. {10, 2457, WLAN_TX_PWR_JP_DEFAULT},
  89. {11, 2462, WLAN_TX_PWR_JP_DEFAULT},
  90. {12, 2467, WLAN_TX_PWR_JP_DEFAULT},
  91. {13, 2472, WLAN_TX_PWR_JP_DEFAULT},
  92. {14, 2484, WLAN_TX_PWR_JP_DEFAULT}
  93. };
  94. /**
  95. * the structure for channel, frequency and power
  96. */
  97. struct region_cfp_table {
  98. u8 region;
  99. struct chan_freq_power *cfp_BG;
  100. int cfp_no_BG;
  101. };
  102. /**
  103. * the structure for the mapping between region and CFP
  104. */
  105. static struct region_cfp_table region_cfp_table[] = {
  106. {0x10, /*US FCC */
  107. channel_freq_power_US_BG,
  108. sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
  109. }
  110. ,
  111. {0x20, /*CANADA IC */
  112. channel_freq_power_US_BG,
  113. sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
  114. }
  115. ,
  116. {0x30, /*EU*/ channel_freq_power_EU_BG,
  117. sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
  118. }
  119. ,
  120. {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
  121. sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
  122. }
  123. ,
  124. {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
  125. sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
  126. }
  127. ,
  128. {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
  129. sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
  130. }
  131. ,
  132. /*Add new region here */
  133. };
  134. /**
  135. * the rates supported by the card
  136. */
  137. u8 libertas_wlan_data_rates[WLAN_SUPPORTED_RATES] =
  138. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  139. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  140. };
  141. /**
  142. * the rates supported
  143. */
  144. u8 libertas_supported_rates[G_SUPPORTED_RATES] =
  145. { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  146. 0 };
  147. /**
  148. * the rates supported for ad-hoc G mode
  149. */
  150. u8 libertas_adhoc_rates_g[G_SUPPORTED_RATES] =
  151. { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  152. 0 };
  153. /**
  154. * the rates supported for ad-hoc B mode
  155. */
  156. u8 libertas_adhoc_rates_b[4] = { 0x82, 0x84, 0x8b, 0x96 };
  157. /**
  158. * the global variable of a pointer to wlan_private
  159. * structure variable
  160. */
  161. static wlan_private *wlanpriv = NULL;
  162. #define MAX_DEVS 5
  163. static struct net_device *libertas_devs[MAX_DEVS];
  164. static int libertas_found = 0;
  165. /**
  166. * the table to keep region code
  167. */
  168. u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  169. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  170. static u8 *default_fw_name = "usb8388.bin";
  171. /**
  172. * Attributes exported through sysfs
  173. */
  174. /**
  175. * @brief Get function for sysfs attribute libertas_mpp
  176. */
  177. static ssize_t libertas_mpp_get(struct device * dev,
  178. struct device_attribute *attr, char * buf) {
  179. struct cmd_ds_mesh_access mesh_access;
  180. memset(&mesh_access, 0, sizeof(mesh_access));
  181. libertas_prepare_and_send_command(to_net_dev(dev)->priv,
  182. cmd_mesh_access,
  183. cmd_act_mesh_get_mpp,
  184. cmd_option_waitforrsp, 0, (void *)&mesh_access);
  185. return snprintf(buf, 3, "%d\n", mesh_access.data[0]);
  186. }
  187. /**
  188. * @brief Set function for sysfs attribute libertas_mpp
  189. */
  190. static ssize_t libertas_mpp_set(struct device * dev,
  191. struct device_attribute *attr, const char * buf, size_t count) {
  192. struct cmd_ds_mesh_access mesh_access;
  193. memset(&mesh_access, 0, sizeof(mesh_access));
  194. sscanf(buf, "%d", &(mesh_access.data[0]));
  195. libertas_prepare_and_send_command((to_net_dev(dev))->priv,
  196. cmd_mesh_access,
  197. cmd_act_mesh_set_mpp,
  198. cmd_option_waitforrsp, 0, (void *)&mesh_access);
  199. return strlen(buf);
  200. }
  201. /**
  202. * libertas_mpp attribute to be exported per mshX interface
  203. * through sysfs (/sys/class/net/mshX/libertas-mpp)
  204. */
  205. static DEVICE_ATTR(libertas_mpp, 0644, libertas_mpp_get,
  206. libertas_mpp_set );
  207. /**
  208. * @brief Check if the device can be open and wait if necessary.
  209. *
  210. * @param dev A pointer to net_device structure
  211. * @return 0
  212. *
  213. * For USB adapter, on some systems the device open handler will be
  214. * called before FW ready. Use the following flag check and wait
  215. * function to work around the issue.
  216. *
  217. */
  218. static int pre_open_check(struct net_device *dev) {
  219. wlan_private *priv = (wlan_private *) dev->priv;
  220. wlan_adapter *adapter = priv->adapter;
  221. int i = 0;
  222. while (!adapter->fw_ready && i < 20) {
  223. i++;
  224. msleep_interruptible(100);
  225. }
  226. if (!adapter->fw_ready) {
  227. lbs_pr_info("FW not ready, pre_open_check() return failure\n");
  228. LEAVE();
  229. return -1;
  230. }
  231. return 0;
  232. }
  233. /**
  234. * @brief This function opens the device
  235. *
  236. * @param dev A pointer to net_device structure
  237. * @return 0
  238. */
  239. static int wlan_dev_open(struct net_device *dev)
  240. {
  241. wlan_private *priv = (wlan_private *) dev->priv;
  242. wlan_adapter *adapter = priv->adapter;
  243. ENTER();
  244. priv->open = 1;
  245. if (adapter->connect_status == libertas_connected) {
  246. netif_carrier_on(priv->wlan_dev.netdev);
  247. } else
  248. netif_carrier_off(priv->wlan_dev.netdev);
  249. LEAVE();
  250. return 0;
  251. }
  252. /**
  253. * @brief This function opens the mshX interface
  254. *
  255. * @param dev A pointer to net_device structure
  256. * @return 0
  257. */
  258. static int mesh_open(struct net_device *dev)
  259. {
  260. wlan_private *priv = (wlan_private *) dev->priv ;
  261. if(pre_open_check(dev) == -1)
  262. return -1;
  263. priv->mesh_open = 1 ;
  264. netif_start_queue(priv->mesh_dev);
  265. if (priv->infra_open == 0)
  266. return wlan_dev_open(priv->wlan_dev.netdev) ;
  267. return 0;
  268. }
  269. /**
  270. * @brief This function opens the ethX interface
  271. *
  272. * @param dev A pointer to net_device structure
  273. * @return 0
  274. */
  275. static int wlan_open(struct net_device *dev)
  276. {
  277. wlan_private *priv = (wlan_private *) dev->priv ;
  278. if(pre_open_check(dev) == -1)
  279. return -1;
  280. priv->infra_open = 1 ;
  281. netif_wake_queue(priv->wlan_dev.netdev);
  282. if (priv->open == 0)
  283. return wlan_dev_open(priv->wlan_dev.netdev) ;
  284. return 0;
  285. }
  286. static int wlan_dev_close(struct net_device *dev)
  287. {
  288. wlan_private *priv = dev->priv;
  289. ENTER();
  290. netif_carrier_off(priv->wlan_dev.netdev);
  291. priv->open = 0;
  292. LEAVE();
  293. return 0;
  294. }
  295. /**
  296. * @brief This function closes the mshX interface
  297. *
  298. * @param dev A pointer to net_device structure
  299. * @return 0
  300. */
  301. static int mesh_close(struct net_device *dev)
  302. {
  303. wlan_private *priv = (wlan_private *) (dev->priv);
  304. priv->mesh_open = 0;
  305. netif_stop_queue(priv->mesh_dev);
  306. if (priv->infra_open == 0)
  307. return wlan_dev_close( ((wlan_private *) dev->priv)->wlan_dev.netdev) ;
  308. else
  309. return 0;
  310. }
  311. /**
  312. * @brief This function closes the ethX interface
  313. *
  314. * @param dev A pointer to net_device structure
  315. * @return 0
  316. */
  317. static int wlan_close(struct net_device *dev) {
  318. wlan_private *priv = (wlan_private *) dev->priv;
  319. netif_stop_queue(priv->wlan_dev.netdev);
  320. priv->infra_open = 0;
  321. if (priv->mesh_open == 0)
  322. return wlan_dev_close( ((wlan_private *) dev->priv)->wlan_dev.netdev) ;
  323. else
  324. return 0;
  325. }
  326. #ifdef ENABLE_PM
  327. /**
  328. * @brief This function is a callback function. it is called by
  329. * kernel to enter or exit power saving mode.
  330. *
  331. * @param pmdev A pointer to pm_dev
  332. * @param pmreq pm_request_t
  333. * @param pmdata A pointer to pmdata
  334. * @return 0 or -1
  335. */
  336. static int wlan_pm_callback(struct pm_dev *pmdev, pm_request_t pmreq,
  337. void *pmdata)
  338. {
  339. wlan_private *priv = wlanpriv;
  340. wlan_adapter *adapter = priv->adapter;
  341. struct net_device *dev = priv->wlan_dev.netdev;
  342. lbs_pr_debug(1, "WPRM_PM_CALLBACK: pmreq = %d.\n", pmreq);
  343. switch (pmreq) {
  344. case PM_SUSPEND:
  345. lbs_pr_debug(1, "WPRM_PM_CALLBACK: enter PM_SUSPEND.\n");
  346. /* in associated mode */
  347. if (adapter->connect_status == libertas_connected) {
  348. if ((adapter->psstate != PS_STATE_SLEEP)
  349. ) {
  350. lbs_pr_debug(1,
  351. "wlan_pm_callback: can't enter sleep mode\n");
  352. return -1;
  353. } else {
  354. /*
  355. * Detach the network interface
  356. * if the network is running
  357. */
  358. if (netif_running(dev)) {
  359. netif_device_detach(dev);
  360. lbs_pr_debug(1,
  361. "netif_device_detach().\n");
  362. }
  363. libertas_sbi_suspend(priv);
  364. }
  365. break;
  366. }
  367. /* in non associated mode */
  368. /*
  369. * Detach the network interface
  370. * if the network is running
  371. */
  372. if (netif_running(dev))
  373. netif_device_detach(dev);
  374. /*
  375. * Storing and restoring of the regs be taken care
  376. * at the driver rest will be done at wlan driver
  377. * this makes driver independent of the card
  378. */
  379. libertas_sbi_suspend(priv);
  380. break;
  381. case PM_RESUME:
  382. /* in associated mode */
  383. if (adapter->connect_status == libertas_connected) {
  384. {
  385. /*
  386. * Bring the inteface up first
  387. * This case should not happen still ...
  388. */
  389. libertas_sbi_resume(priv);
  390. /*
  391. * Attach the network interface
  392. * if the network is running
  393. */
  394. if (netif_running(dev)) {
  395. netif_device_attach(dev);
  396. lbs_pr_debug(1,
  397. "after netif_device_attach().\n");
  398. }
  399. lbs_pr_debug(1,
  400. "After netif attach, in associated mode.\n");
  401. }
  402. break;
  403. }
  404. /* in non associated mode */
  405. /*
  406. * Bring the inteface up first
  407. * This case should not happen still ...
  408. */
  409. libertas_sbi_resume(priv);
  410. if (netif_running(dev))
  411. netif_device_attach(dev);
  412. lbs_pr_debug(1, "after netif attach, in NON associated mode.\n");
  413. break;
  414. }
  415. return 0;
  416. }
  417. #endif /* ENABLE_PM */
  418. static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  419. {
  420. int ret = 0;
  421. wlan_private *priv = dev->priv;
  422. ENTER();
  423. if (priv->wlan_dev.dnld_sent || priv->adapter->TxLockFlag) {
  424. priv->stats.tx_dropped++;
  425. goto done;
  426. }
  427. netif_stop_queue(priv->wlan_dev.netdev);
  428. if (libertas_process_tx(priv, skb) == 0)
  429. dev->trans_start = jiffies;
  430. done:
  431. LEAVE();
  432. return ret;
  433. }
  434. /**
  435. * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
  436. *
  437. */
  438. static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
  439. {
  440. wlan_private *priv = dev->priv;
  441. ENTER();
  442. SET_MESH_FRAME(skb);
  443. LEAVE();
  444. return wlan_hard_start_xmit(skb, priv->wlan_dev.netdev);
  445. }
  446. /**
  447. * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
  448. *
  449. */
  450. static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev) {
  451. ENTER();
  452. UNSET_MESH_FRAME(skb);
  453. LEAVE();
  454. return wlan_hard_start_xmit(skb, dev);
  455. }
  456. static void wlan_tx_timeout(struct net_device *dev)
  457. {
  458. wlan_private *priv = (wlan_private *) dev->priv;
  459. ENTER();
  460. lbs_pr_err("tx watch dog timeout!\n");
  461. priv->wlan_dev.dnld_sent = DNLD_RES_RECEIVED;
  462. dev->trans_start = jiffies;
  463. if (priv->adapter->currenttxskb) {
  464. if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
  465. /* If we are here, we have not received feedback from
  466. the previous packet. Assume TX_FAIL and move on. */
  467. priv->adapter->eventcause = 0x01000000;
  468. libertas_send_tx_feedback(priv);
  469. } else
  470. wake_up_interruptible(&priv->mainthread.waitq);
  471. } else if (priv->adapter->connect_status == libertas_connected)
  472. netif_wake_queue(priv->wlan_dev.netdev);
  473. LEAVE();
  474. }
  475. /**
  476. * @brief This function returns the network statistics
  477. *
  478. * @param dev A pointer to wlan_private structure
  479. * @return A pointer to net_device_stats structure
  480. */
  481. static struct net_device_stats *wlan_get_stats(struct net_device *dev)
  482. {
  483. wlan_private *priv = (wlan_private *) dev->priv;
  484. return &priv->stats;
  485. }
  486. static int wlan_set_mac_address(struct net_device *dev, void *addr)
  487. {
  488. int ret = 0;
  489. wlan_private *priv = (wlan_private *) dev->priv;
  490. wlan_adapter *adapter = priv->adapter;
  491. struct sockaddr *phwaddr = addr;
  492. ENTER();
  493. memset(adapter->current_addr, 0, ETH_ALEN);
  494. /* dev->dev_addr is 8 bytes */
  495. lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
  496. lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
  497. memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
  498. ret = libertas_prepare_and_send_command(priv, cmd_802_11_mac_address,
  499. cmd_act_set,
  500. cmd_option_waitforrsp, 0, NULL);
  501. if (ret) {
  502. lbs_pr_debug(1, "set mac address failed.\n");
  503. ret = -1;
  504. goto done;
  505. }
  506. lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
  507. memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
  508. memcpy(((wlan_private *) dev->priv)->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
  509. done:
  510. LEAVE();
  511. return ret;
  512. }
  513. static int wlan_copy_multicast_address(wlan_adapter * adapter,
  514. struct net_device *dev)
  515. {
  516. int i = 0;
  517. struct dev_mc_list *mcptr = dev->mc_list;
  518. for (i = 0; i < dev->mc_count; i++) {
  519. memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  520. mcptr = mcptr->next;
  521. }
  522. return i;
  523. }
  524. static void wlan_set_multicast_list(struct net_device *dev)
  525. {
  526. wlan_private *priv = dev->priv;
  527. wlan_adapter *adapter = priv->adapter;
  528. int oldpacketfilter;
  529. ENTER();
  530. oldpacketfilter = adapter->currentpacketfilter;
  531. if (dev->flags & IFF_PROMISC) {
  532. lbs_pr_debug(1, "enable Promiscuous mode\n");
  533. adapter->currentpacketfilter |=
  534. cmd_act_mac_promiscuous_enable;
  535. adapter->currentpacketfilter &=
  536. ~(cmd_act_mac_all_multicast_enable |
  537. cmd_act_mac_multicast_enable);
  538. } else {
  539. /* Multicast */
  540. adapter->currentpacketfilter &=
  541. ~cmd_act_mac_promiscuous_enable;
  542. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  543. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  544. lbs_pr_debug(1, "Enabling All Multicast!\n");
  545. adapter->currentpacketfilter |=
  546. cmd_act_mac_all_multicast_enable;
  547. adapter->currentpacketfilter &=
  548. ~cmd_act_mac_multicast_enable;
  549. } else {
  550. adapter->currentpacketfilter &=
  551. ~cmd_act_mac_all_multicast_enable;
  552. if (!dev->mc_count) {
  553. lbs_pr_debug(1, "No multicast addresses - "
  554. "disabling multicast!\n");
  555. adapter->currentpacketfilter &=
  556. ~cmd_act_mac_multicast_enable;
  557. } else {
  558. int i;
  559. adapter->currentpacketfilter |=
  560. cmd_act_mac_multicast_enable;
  561. adapter->nr_of_multicastmacaddr =
  562. wlan_copy_multicast_address(adapter, dev);
  563. lbs_pr_debug(1, "Multicast addresses: %d\n",
  564. dev->mc_count);
  565. for (i = 0; i < dev->mc_count; i++) {
  566. lbs_pr_debug(1, "Multicast address %d:"
  567. "%x %x %x %x %x %x\n", i,
  568. adapter->multicastlist[i][0],
  569. adapter->multicastlist[i][1],
  570. adapter->multicastlist[i][2],
  571. adapter->multicastlist[i][3],
  572. adapter->multicastlist[i][4],
  573. adapter->multicastlist[i][5]);
  574. }
  575. /* set multicast addresses to firmware */
  576. libertas_prepare_and_send_command(priv,
  577. cmd_mac_multicast_adr,
  578. cmd_act_set, 0, 0,
  579. NULL);
  580. }
  581. }
  582. }
  583. if (adapter->currentpacketfilter != oldpacketfilter) {
  584. libertas_set_mac_packet_filter(priv);
  585. }
  586. LEAVE();
  587. }
  588. /**
  589. * @brief This function hanldes the major job in WLAN driver.
  590. * it handles the event generated by firmware, rx data received
  591. * from firmware and tx data sent from kernel.
  592. *
  593. * @param data A pointer to wlan_thread structure
  594. * @return 0
  595. */
  596. static int wlan_service_main_thread(void *data)
  597. {
  598. struct wlan_thread *thread = data;
  599. wlan_private *priv = thread->priv;
  600. wlan_adapter *adapter = priv->adapter;
  601. wait_queue_t wait;
  602. u8 ireg = 0;
  603. ENTER();
  604. wlan_activate_thread(thread);
  605. init_waitqueue_entry(&wait, current);
  606. for (;;) {
  607. lbs_pr_debug(1, "main-thread 111: intcounter=%d "
  608. "currenttxskb=%p dnld_sent=%d\n",
  609. adapter->intcounter,
  610. adapter->currenttxskb, priv->wlan_dev.dnld_sent);
  611. add_wait_queue(&thread->waitq, &wait);
  612. set_current_state(TASK_INTERRUPTIBLE);
  613. spin_lock_irq(&adapter->driver_lock);
  614. if ((adapter->psstate == PS_STATE_SLEEP) ||
  615. (!adapter->intcounter
  616. && (priv->wlan_dev.dnld_sent || adapter->cur_cmd ||
  617. list_empty(&adapter->cmdpendingq)))) {
  618. lbs_pr_debug(1,
  619. "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  620. adapter->connect_status, adapter->intcounter,
  621. adapter->psmode, adapter->psstate);
  622. spin_unlock_irq(&adapter->driver_lock);
  623. schedule();
  624. } else
  625. spin_unlock_irq(&adapter->driver_lock);
  626. lbs_pr_debug(1,
  627. "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
  628. "dnld_sent=%d\n", adapter->intcounter,
  629. adapter->currenttxskb, priv->wlan_dev.dnld_sent);
  630. set_current_state(TASK_RUNNING);
  631. remove_wait_queue(&thread->waitq, &wait);
  632. try_to_freeze();
  633. lbs_pr_debug(1, "main-thread 333: intcounter=%d currenttxskb=%p "
  634. "dnld_sent=%d\n",
  635. adapter->intcounter,
  636. adapter->currenttxskb, priv->wlan_dev.dnld_sent);
  637. if (kthread_should_stop()
  638. || adapter->surpriseremoved) {
  639. lbs_pr_debug(1,
  640. "main-thread: break from main thread: surpriseremoved=0x%x\n",
  641. adapter->surpriseremoved);
  642. break;
  643. }
  644. spin_lock_irq(&adapter->driver_lock);
  645. if (adapter->intcounter) {
  646. u8 int_status;
  647. adapter->intcounter = 0;
  648. int_status = libertas_sbi_get_int_status(priv, &ireg);
  649. if (int_status) {
  650. lbs_pr_debug(1,
  651. "main-thread: reading HOST_INT_STATUS_REG failed\n");
  652. spin_unlock_irq(&adapter->driver_lock);
  653. continue;
  654. }
  655. adapter->hisregcpy |= ireg;
  656. }
  657. lbs_pr_debug(1, "main-thread 444: intcounter=%d currenttxskb=%p "
  658. "dnld_sent=%d\n",
  659. adapter->intcounter,
  660. adapter->currenttxskb, priv->wlan_dev.dnld_sent);
  661. /* command response? */
  662. if (adapter->hisregcpy & his_cmdupldrdy) {
  663. lbs_pr_debug(1, "main-thread: cmd response ready.\n");
  664. adapter->hisregcpy &= ~his_cmdupldrdy;
  665. spin_unlock_irq(&adapter->driver_lock);
  666. libertas_process_rx_command(priv);
  667. spin_lock_irq(&adapter->driver_lock);
  668. }
  669. /* Any Card Event */
  670. if (adapter->hisregcpy & his_cardevent) {
  671. lbs_pr_debug(1, "main-thread: Card Event Activity.\n");
  672. adapter->hisregcpy &= ~his_cardevent;
  673. if (libertas_sbi_read_event_cause(priv)) {
  674. lbs_pr_alert(
  675. "main-thread: libertas_sbi_read_event_cause failed.\n");
  676. spin_unlock_irq(&adapter->driver_lock);
  677. continue;
  678. }
  679. spin_unlock_irq(&adapter->driver_lock);
  680. libertas_process_event(priv);
  681. } else
  682. spin_unlock_irq(&adapter->driver_lock);
  683. /* Check if we need to confirm Sleep Request received previously */
  684. if (adapter->psstate == PS_STATE_PRE_SLEEP) {
  685. if (!priv->wlan_dev.dnld_sent && !adapter->cur_cmd) {
  686. if (adapter->connect_status ==
  687. libertas_connected) {
  688. lbs_pr_debug(1,
  689. "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
  690. "dnld_sent=%d cur_cmd=%p, confirm now\n",
  691. adapter->intcounter,
  692. adapter->currenttxskb,
  693. priv->wlan_dev.dnld_sent,
  694. adapter->cur_cmd);
  695. libertas_ps_confirm_sleep(priv,
  696. (u16) adapter->psmode);
  697. } else {
  698. /* workaround for firmware sending
  699. * deauth/linkloss event immediately
  700. * after sleep request, remove this
  701. * after firmware fixes it
  702. */
  703. adapter->psstate = PS_STATE_AWAKE;
  704. lbs_pr_alert(
  705. "main-thread: ignore PS_SleepConfirm in non-connected state\n");
  706. }
  707. }
  708. }
  709. /* The PS state is changed during processing of Sleep Request
  710. * event above
  711. */
  712. if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
  713. (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
  714. continue;
  715. /* Execute the next command */
  716. if (!priv->wlan_dev.dnld_sent && !priv->adapter->cur_cmd)
  717. libertas_execute_next_command(priv);
  718. /* Wake-up command waiters which can't sleep in
  719. * libertas_prepare_and_send_command
  720. */
  721. if (!adapter->nr_cmd_pending)
  722. wake_up_all(&adapter->cmd_pending);
  723. libertas_tx_runqueue(priv);
  724. }
  725. del_timer(&adapter->command_timer);
  726. adapter->nr_cmd_pending = 0;
  727. wake_up_all(&adapter->cmd_pending);
  728. wlan_deactivate_thread(thread);
  729. LEAVE();
  730. return 0;
  731. }
  732. /**
  733. * @brief This function adds the card. it will probe the
  734. * card, allocate the wlan_priv and initialize the device.
  735. *
  736. * @param card A pointer to card
  737. * @return A pointer to wlan_private structure
  738. */
  739. wlan_private *wlan_add_card(void *card)
  740. {
  741. struct net_device *dev = NULL;
  742. struct net_device *mesh_dev = NULL;
  743. wlan_private *priv = NULL;
  744. ENTER();
  745. /* Allocate an Ethernet device and register it */
  746. if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
  747. lbs_pr_alert( "Init ethernet device failed!\n");
  748. return NULL;
  749. }
  750. priv = dev->priv;
  751. /* allocate buffer for wlan_adapter */
  752. if (!(priv->adapter = kmalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
  753. lbs_pr_alert( "Allocate buffer for wlan_adapter failed!\n");
  754. goto err_kmalloc;
  755. }
  756. /* Allocate a virtual mesh device */
  757. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  758. lbs_pr_debug(1, "Init ethernet device failed!\n");
  759. return NULL;
  760. }
  761. /* Both intervaces share the priv structure */
  762. mesh_dev->priv = priv;
  763. /* init wlan_adapter */
  764. memset(priv->adapter, 0, sizeof(wlan_adapter));
  765. priv->wlan_dev.netdev = dev;
  766. priv->wlan_dev.card = card;
  767. priv->mesh_open = 0;
  768. priv->infra_open = 0;
  769. priv->mesh_dev = mesh_dev;
  770. wlanpriv = priv;
  771. SET_MODULE_OWNER(dev);
  772. SET_MODULE_OWNER(mesh_dev);
  773. /* Setup the OS Interface to our functions */
  774. dev->open = wlan_open;
  775. dev->hard_start_xmit = wlan_pre_start_xmit;
  776. dev->stop = wlan_close;
  777. dev->do_ioctl = libertas_do_ioctl;
  778. dev->set_mac_address = wlan_set_mac_address;
  779. mesh_dev->open = mesh_open;
  780. mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
  781. mesh_dev->stop = mesh_close;
  782. mesh_dev->do_ioctl = libertas_do_ioctl;
  783. memcpy(mesh_dev->dev_addr, wlanpriv->wlan_dev.netdev->dev_addr,
  784. sizeof(wlanpriv->wlan_dev.netdev->dev_addr));
  785. #define WLAN_WATCHDOG_TIMEOUT (5 * HZ)
  786. dev->tx_timeout = wlan_tx_timeout;
  787. dev->get_stats = wlan_get_stats;
  788. dev->watchdog_timeo = WLAN_WATCHDOG_TIMEOUT;
  789. dev->ethtool_ops = &libertas_ethtool_ops;
  790. mesh_dev->get_stats = wlan_get_stats;
  791. mesh_dev->ethtool_ops = &libertas_ethtool_ops;
  792. #ifdef WIRELESS_EXT
  793. dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  794. mesh_dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  795. #endif
  796. #define NETIF_F_DYNALLOC 16
  797. dev->features |= NETIF_F_DYNALLOC;
  798. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  799. dev->set_multicast_list = wlan_set_multicast_list;
  800. INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
  801. INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
  802. spin_lock_init(&priv->adapter->driver_lock);
  803. init_waitqueue_head(&priv->adapter->cmd_pending);
  804. priv->adapter->nr_cmd_pending = 0;
  805. lbs_pr_debug(1, "Starting kthread...\n");
  806. priv->mainthread.priv = priv;
  807. wlan_create_thread(wlan_service_main_thread,
  808. &priv->mainthread, "wlan_main_service");
  809. priv->assoc_thread =
  810. create_singlethread_workqueue("libertas_assoc");
  811. INIT_DELAYED_WORK(&priv->assoc_work, wlan_association_worker);
  812. /*
  813. * Register the device. Fillup the private data structure with
  814. * relevant information from the card and request for the required
  815. * IRQ.
  816. */
  817. if (libertas_sbi_register_dev(priv) < 0) {
  818. lbs_pr_info("failed to register wlan device!\n");
  819. goto err_registerdev;
  820. }
  821. /* init FW and HW */
  822. if (libertas_init_fw(priv)) {
  823. lbs_pr_debug(1, "Firmware Init failed\n");
  824. goto err_registerdev;
  825. }
  826. if (register_netdev(dev)) {
  827. lbs_pr_err("Cannot register network device!\n");
  828. goto err_init_fw;
  829. }
  830. /* Register virtual mesh interface */
  831. if (register_netdev(mesh_dev)) {
  832. lbs_pr_info("Cannot register mesh virtual interface!\n");
  833. goto err_init_fw;
  834. }
  835. lbs_pr_info("%s: Marvell Wlan 802.11 adapter ", dev->name);
  836. libertas_debugfs_init_one(priv, dev);
  837. if (libertas_found == MAX_DEVS)
  838. goto err_init_fw;
  839. libertas_devs[libertas_found] = dev;
  840. libertas_found++;
  841. #ifdef ENABLE_PM
  842. if (!(wlan_pm_dev = pm_register(PM_UNKNOWN_DEV, 0, wlan_pm_callback)))
  843. lbs_pr_alert( "failed to register PM callback\n");
  844. #endif
  845. if (device_create_file(&(mesh_dev->dev), &dev_attr_libertas_mpp))
  846. goto err_create_file;
  847. LEAVE();
  848. return priv;
  849. err_create_file:
  850. device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
  851. err_init_fw:
  852. libertas_sbi_unregister_dev(priv);
  853. err_registerdev:
  854. destroy_workqueue(priv->assoc_thread);
  855. /* Stop the thread servicing the interrupts */
  856. wake_up_interruptible(&priv->mainthread.waitq);
  857. wlan_terminate_thread(&priv->mainthread);
  858. kfree(priv->adapter);
  859. err_kmalloc:
  860. free_netdev(dev);
  861. free_netdev(mesh_dev);
  862. wlanpriv = NULL;
  863. LEAVE();
  864. return NULL;
  865. }
  866. static void wake_pending_cmdnodes(wlan_private *priv)
  867. {
  868. struct cmd_ctrl_node *cmdnode;
  869. unsigned long flags;
  870. spin_lock_irqsave(&priv->adapter->driver_lock, flags);
  871. list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
  872. cmdnode->cmdwaitqwoken = 1;
  873. wake_up_interruptible(&cmdnode->cmdwait_q);
  874. }
  875. spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
  876. }
  877. int wlan_remove_card(void *card)
  878. {
  879. wlan_private *priv = libertas_sbi_get_priv(card);
  880. wlan_adapter *adapter;
  881. struct net_device *dev;
  882. struct net_device *mesh_dev;
  883. union iwreq_data wrqu;
  884. int i;
  885. ENTER();
  886. if (!priv) {
  887. LEAVE();
  888. return 0;
  889. }
  890. adapter = priv->adapter;
  891. if (!adapter) {
  892. LEAVE();
  893. return 0;
  894. }
  895. dev = priv->wlan_dev.netdev;
  896. mesh_dev = priv->mesh_dev;
  897. netif_stop_queue(mesh_dev);
  898. netif_stop_queue(priv->wlan_dev.netdev);
  899. netif_carrier_off(priv->wlan_dev.netdev);
  900. wake_pending_cmdnodes(priv);
  901. device_remove_file(&(mesh_dev->dev), &dev_attr_libertas_mpp);
  902. unregister_netdev(mesh_dev);
  903. unregister_netdev(dev);
  904. cancel_delayed_work(&priv->assoc_work);
  905. destroy_workqueue(priv->assoc_thread);
  906. if (adapter->psmode == wlan802_11powermodemax_psp) {
  907. adapter->psmode = wlan802_11powermodecam;
  908. libertas_ps_wakeup(priv, cmd_option_waitforrsp);
  909. }
  910. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  911. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  912. wireless_send_event(priv->wlan_dev.netdev, SIOCGIWAP, &wrqu, NULL);
  913. #ifdef ENABLE_PM
  914. pm_unregister(wlan_pm_dev);
  915. #endif
  916. adapter->surpriseremoved = 1;
  917. /* Stop the thread servicing the interrupts */
  918. wlan_terminate_thread(&priv->mainthread);
  919. libertas_debugfs_remove_one(priv);
  920. lbs_pr_debug(1, "Free adapter\n");
  921. libertas_free_adapter(priv);
  922. for (i = 0; i<libertas_found; i++) {
  923. if (libertas_devs[i]==priv->wlan_dev.netdev) {
  924. libertas_devs[i] = libertas_devs[--libertas_found];
  925. libertas_devs[libertas_found] = NULL ;
  926. break ;
  927. }
  928. }
  929. lbs_pr_debug(1, "Unregister finish\n");
  930. priv->wlan_dev.netdev = NULL;
  931. priv->mesh_dev = NULL ;
  932. free_netdev(mesh_dev);
  933. free_netdev(dev);
  934. wlanpriv = NULL;
  935. LEAVE();
  936. return 0;
  937. }
  938. /**
  939. * @brief This function finds the CFP in
  940. * region_cfp_table based on region and band parameter.
  941. *
  942. * @param region The region code
  943. * @param band The band
  944. * @param cfp_no A pointer to CFP number
  945. * @return A pointer to CFP
  946. */
  947. struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  948. {
  949. int i, end;
  950. ENTER();
  951. end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
  952. for (i = 0; i < end ; i++) {
  953. lbs_pr_debug(1, "region_cfp_table[i].region=%d\n",
  954. region_cfp_table[i].region);
  955. if (region_cfp_table[i].region == region) {
  956. *cfp_no = region_cfp_table[i].cfp_no_BG;
  957. LEAVE();
  958. return region_cfp_table[i].cfp_BG;
  959. }
  960. }
  961. LEAVE();
  962. return NULL;
  963. }
  964. int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
  965. {
  966. wlan_adapter *adapter = priv->adapter;
  967. int i = 0;
  968. struct chan_freq_power *cfp;
  969. int cfp_no;
  970. ENTER();
  971. memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
  972. {
  973. cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
  974. if (cfp != NULL) {
  975. adapter->region_channel[i].nrcfp = cfp_no;
  976. adapter->region_channel[i].CFP = cfp;
  977. } else {
  978. lbs_pr_debug(1, "wrong region code %#x in band B-G\n",
  979. region);
  980. return -1;
  981. }
  982. adapter->region_channel[i].valid = 1;
  983. adapter->region_channel[i].region = region;
  984. adapter->region_channel[i].band = band;
  985. i++;
  986. }
  987. LEAVE();
  988. return 0;
  989. }
  990. /**
  991. * @brief This function handles the interrupt. it will change PS
  992. * state if applicable. it will wake up main_thread to handle
  993. * the interrupt event as well.
  994. *
  995. * @param dev A pointer to net_device structure
  996. * @return n/a
  997. */
  998. void libertas_interrupt(struct net_device *dev)
  999. {
  1000. wlan_private *priv = dev->priv;
  1001. ENTER();
  1002. lbs_pr_debug(1, "libertas_interrupt: intcounter=%d\n",
  1003. priv->adapter->intcounter);
  1004. priv->adapter->intcounter++;
  1005. if (priv->adapter->psstate == PS_STATE_SLEEP) {
  1006. priv->adapter->psstate = PS_STATE_AWAKE;
  1007. netif_wake_queue(dev);
  1008. }
  1009. wake_up_interruptible(&priv->mainthread.waitq);
  1010. LEAVE();
  1011. }
  1012. static int wlan_init_module(void)
  1013. {
  1014. int ret = 0;
  1015. ENTER();
  1016. if (libertas_fw_name == NULL) {
  1017. libertas_fw_name = default_fw_name;
  1018. }
  1019. libertas_debugfs_init();
  1020. if (libertas_sbi_register()) {
  1021. ret = -1;
  1022. libertas_debugfs_remove();
  1023. goto done;
  1024. }
  1025. done:
  1026. LEAVE();
  1027. return ret;
  1028. }
  1029. static void wlan_cleanup_module(void)
  1030. {
  1031. int i;
  1032. ENTER();
  1033. for (i = 0; i<libertas_found; i++) {
  1034. wlan_private *priv = libertas_devs[i]->priv;
  1035. reset_device(priv);
  1036. }
  1037. libertas_sbi_unregister();
  1038. libertas_debugfs_remove();
  1039. LEAVE();
  1040. }
  1041. module_init(wlan_init_module);
  1042. module_exit(wlan_cleanup_module);
  1043. MODULE_DESCRIPTION("M-WLAN Driver");
  1044. MODULE_AUTHOR("Marvell International Ltd.");
  1045. MODULE_LICENSE("GPL");