main.c 29 KB

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