main.c 29 KB

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