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. {
  165. struct cmd_ds_mesh_access mesh_access;
  166. memset(&mesh_access, 0, sizeof(mesh_access));
  167. libertas_prepare_and_send_command(to_net_dev(dev)->priv,
  168. cmd_mesh_access,
  169. cmd_act_mesh_get_anycast,
  170. cmd_option_waitforrsp, 0, (void *)&mesh_access);
  171. return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
  172. }
  173. /**
  174. * @brief Set function for sysfs attribute anycast_mask
  175. */
  176. static ssize_t libertas_anycast_set(struct device * dev,
  177. struct device_attribute *attr, const char * buf, size_t count)
  178. {
  179. struct cmd_ds_mesh_access mesh_access;
  180. uint32_t datum;
  181. memset(&mesh_access, 0, sizeof(mesh_access));
  182. sscanf(buf, "%x", &datum);
  183. mesh_access.data[0] = cpu_to_le32(datum);
  184. libertas_prepare_and_send_command((to_net_dev(dev))->priv,
  185. cmd_mesh_access,
  186. cmd_act_mesh_set_anycast,
  187. cmd_option_waitforrsp, 0, (void *)&mesh_access);
  188. return strlen(buf);
  189. }
  190. /**
  191. * anycast_mask attribute to be exported per mshX interface
  192. * through sysfs (/sys/class/net/mshX/anycast_mask)
  193. */
  194. static DEVICE_ATTR(anycast_mask, 0644, libertas_anycast_get, libertas_anycast_set);
  195. /**
  196. * @brief Check if the device can be open and wait if necessary.
  197. *
  198. * @param dev A pointer to net_device structure
  199. * @return 0
  200. *
  201. * For USB adapter, on some systems the device open handler will be
  202. * called before FW ready. Use the following flag check and wait
  203. * function to work around the issue.
  204. *
  205. */
  206. static int pre_open_check(struct net_device *dev)
  207. {
  208. wlan_private *priv = (wlan_private *) dev->priv;
  209. wlan_adapter *adapter = priv->adapter;
  210. int i = 0;
  211. while (!adapter->fw_ready && i < 20) {
  212. i++;
  213. msleep_interruptible(100);
  214. }
  215. if (!adapter->fw_ready) {
  216. lbs_pr_err("firmware not ready\n");
  217. return -1;
  218. }
  219. return 0;
  220. }
  221. /**
  222. * @brief This function opens the device
  223. *
  224. * @param dev A pointer to net_device structure
  225. * @return 0
  226. */
  227. static int wlan_dev_open(struct net_device *dev)
  228. {
  229. wlan_private *priv = (wlan_private *) dev->priv;
  230. wlan_adapter *adapter = priv->adapter;
  231. lbs_deb_enter(LBS_DEB_NET);
  232. priv->open = 1;
  233. if (adapter->connect_status == libertas_connected) {
  234. netif_carrier_on(priv->dev);
  235. netif_carrier_on(priv->mesh_dev);
  236. } else {
  237. netif_carrier_off(priv->dev);
  238. netif_carrier_off(priv->mesh_dev);
  239. }
  240. lbs_deb_leave(LBS_DEB_NET);
  241. return 0;
  242. }
  243. /**
  244. * @brief This function opens the mshX interface
  245. *
  246. * @param dev A pointer to net_device structure
  247. * @return 0
  248. */
  249. static int mesh_open(struct net_device *dev)
  250. {
  251. wlan_private *priv = (wlan_private *) dev->priv ;
  252. if (pre_open_check(dev) == -1)
  253. return -1;
  254. priv->mesh_open = 1 ;
  255. netif_wake_queue(priv->mesh_dev);
  256. if (priv->infra_open == 0)
  257. return wlan_dev_open(priv->dev) ;
  258. return 0;
  259. }
  260. /**
  261. * @brief This function opens the ethX interface
  262. *
  263. * @param dev A pointer to net_device structure
  264. * @return 0
  265. */
  266. static int wlan_open(struct net_device *dev)
  267. {
  268. wlan_private *priv = (wlan_private *) dev->priv ;
  269. if(pre_open_check(dev) == -1)
  270. return -1;
  271. priv->infra_open = 1 ;
  272. netif_wake_queue(priv->dev);
  273. if (priv->open == 0)
  274. return wlan_dev_open(priv->dev) ;
  275. return 0;
  276. }
  277. static int wlan_dev_close(struct net_device *dev)
  278. {
  279. wlan_private *priv = dev->priv;
  280. lbs_deb_enter(LBS_DEB_NET);
  281. netif_carrier_off(priv->dev);
  282. priv->open = 0;
  283. lbs_deb_leave(LBS_DEB_NET);
  284. return 0;
  285. }
  286. /**
  287. * @brief This function closes the mshX interface
  288. *
  289. * @param dev A pointer to net_device structure
  290. * @return 0
  291. */
  292. static int mesh_close(struct net_device *dev)
  293. {
  294. wlan_private *priv = (wlan_private *) (dev->priv);
  295. priv->mesh_open = 0;
  296. netif_stop_queue(priv->mesh_dev);
  297. if (priv->infra_open == 0)
  298. return wlan_dev_close(dev);
  299. else
  300. return 0;
  301. }
  302. /**
  303. * @brief This function closes the ethX interface
  304. *
  305. * @param dev A pointer to net_device structure
  306. * @return 0
  307. */
  308. static int wlan_close(struct net_device *dev)
  309. {
  310. wlan_private *priv = (wlan_private *) dev->priv;
  311. netif_stop_queue(dev);
  312. priv->infra_open = 0;
  313. if (priv->mesh_open == 0)
  314. return wlan_dev_close(dev);
  315. else
  316. return 0;
  317. }
  318. static int wlan_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  319. {
  320. int ret = 0;
  321. wlan_private *priv = dev->priv;
  322. lbs_deb_enter(LBS_DEB_NET);
  323. if (priv->dnld_sent || priv->adapter->TxLockFlag) {
  324. priv->stats.tx_dropped++;
  325. goto done;
  326. }
  327. netif_stop_queue(priv->dev);
  328. netif_stop_queue(priv->mesh_dev);
  329. if (libertas_process_tx(priv, skb) == 0)
  330. dev->trans_start = jiffies;
  331. done:
  332. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  333. return ret;
  334. }
  335. /**
  336. * @brief Mark mesh packets and handover them to wlan_hard_start_xmit
  337. *
  338. */
  339. static int mesh_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
  340. {
  341. wlan_private *priv = dev->priv;
  342. int ret;
  343. lbs_deb_enter(LBS_DEB_MESH);
  344. SET_MESH_FRAME(skb);
  345. ret = wlan_hard_start_xmit(skb, priv->dev);
  346. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  347. return ret;
  348. }
  349. /**
  350. * @brief Mark non-mesh packets and handover them to wlan_hard_start_xmit
  351. *
  352. */
  353. static int wlan_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
  354. {
  355. int ret;
  356. lbs_deb_enter(LBS_DEB_NET);
  357. UNSET_MESH_FRAME(skb);
  358. ret = wlan_hard_start_xmit(skb, dev);
  359. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  360. return ret;
  361. }
  362. static void wlan_tx_timeout(struct net_device *dev)
  363. {
  364. wlan_private *priv = (wlan_private *) dev->priv;
  365. lbs_deb_enter(LBS_DEB_TX);
  366. lbs_pr_err("tx watch dog timeout\n");
  367. priv->dnld_sent = DNLD_RES_RECEIVED;
  368. dev->trans_start = jiffies;
  369. if (priv->adapter->currenttxskb) {
  370. if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
  371. /* If we are here, we have not received feedback from
  372. the previous packet. Assume TX_FAIL and move on. */
  373. priv->adapter->eventcause = 0x01000000;
  374. libertas_send_tx_feedback(priv);
  375. } else
  376. wake_up_interruptible(&priv->mainthread.waitq);
  377. } else if (priv->adapter->connect_status == libertas_connected) {
  378. netif_wake_queue(priv->dev);
  379. netif_wake_queue(priv->mesh_dev);
  380. }
  381. lbs_deb_leave(LBS_DEB_TX);
  382. }
  383. /**
  384. * @brief This function returns the network statistics
  385. *
  386. * @param dev A pointer to wlan_private structure
  387. * @return A pointer to net_device_stats structure
  388. */
  389. static struct net_device_stats *wlan_get_stats(struct net_device *dev)
  390. {
  391. wlan_private *priv = (wlan_private *) dev->priv;
  392. return &priv->stats;
  393. }
  394. static int wlan_set_mac_address(struct net_device *dev, void *addr)
  395. {
  396. int ret = 0;
  397. wlan_private *priv = (wlan_private *) dev->priv;
  398. wlan_adapter *adapter = priv->adapter;
  399. struct sockaddr *phwaddr = addr;
  400. lbs_deb_enter(LBS_DEB_NET);
  401. /* In case it was called from the mesh device */
  402. dev = priv->dev ;
  403. memset(adapter->current_addr, 0, ETH_ALEN);
  404. /* dev->dev_addr is 8 bytes */
  405. lbs_dbg_hex("dev->dev_addr:", dev->dev_addr, ETH_ALEN);
  406. lbs_dbg_hex("addr:", phwaddr->sa_data, ETH_ALEN);
  407. memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
  408. ret = libertas_prepare_and_send_command(priv, cmd_802_11_mac_address,
  409. cmd_act_set,
  410. cmd_option_waitforrsp, 0, NULL);
  411. if (ret) {
  412. lbs_deb_net("set MAC address failed\n");
  413. ret = -1;
  414. goto done;
  415. }
  416. lbs_dbg_hex("adapter->macaddr:", adapter->current_addr, ETH_ALEN);
  417. memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
  418. if (priv->mesh_dev)
  419. memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
  420. done:
  421. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  422. return ret;
  423. }
  424. static int wlan_copy_multicast_address(wlan_adapter * adapter,
  425. struct net_device *dev)
  426. {
  427. int i = 0;
  428. struct dev_mc_list *mcptr = dev->mc_list;
  429. for (i = 0; i < dev->mc_count; i++) {
  430. memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  431. mcptr = mcptr->next;
  432. }
  433. return i;
  434. }
  435. static void wlan_set_multicast_list(struct net_device *dev)
  436. {
  437. wlan_private *priv = dev->priv;
  438. wlan_adapter *adapter = priv->adapter;
  439. int oldpacketfilter;
  440. lbs_deb_enter(LBS_DEB_NET);
  441. oldpacketfilter = adapter->currentpacketfilter;
  442. if (dev->flags & IFF_PROMISC) {
  443. lbs_deb_net("enable promiscuous mode\n");
  444. adapter->currentpacketfilter |=
  445. cmd_act_mac_promiscuous_enable;
  446. adapter->currentpacketfilter &=
  447. ~(cmd_act_mac_all_multicast_enable |
  448. cmd_act_mac_multicast_enable);
  449. } else {
  450. /* Multicast */
  451. adapter->currentpacketfilter &=
  452. ~cmd_act_mac_promiscuous_enable;
  453. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  454. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  455. lbs_deb_net( "enabling all multicast\n");
  456. adapter->currentpacketfilter |=
  457. cmd_act_mac_all_multicast_enable;
  458. adapter->currentpacketfilter &=
  459. ~cmd_act_mac_multicast_enable;
  460. } else {
  461. adapter->currentpacketfilter &=
  462. ~cmd_act_mac_all_multicast_enable;
  463. if (!dev->mc_count) {
  464. lbs_deb_net("no multicast addresses, "
  465. "disabling multicast\n");
  466. adapter->currentpacketfilter &=
  467. ~cmd_act_mac_multicast_enable;
  468. } else {
  469. int i;
  470. adapter->currentpacketfilter |=
  471. cmd_act_mac_multicast_enable;
  472. adapter->nr_of_multicastmacaddr =
  473. wlan_copy_multicast_address(adapter, dev);
  474. lbs_deb_net("multicast addresses: %d\n",
  475. dev->mc_count);
  476. for (i = 0; i < dev->mc_count; i++) {
  477. lbs_deb_net("Multicast address %d:"
  478. MAC_FMT "\n", i,
  479. adapter->multicastlist[i][0],
  480. adapter->multicastlist[i][1],
  481. adapter->multicastlist[i][2],
  482. adapter->multicastlist[i][3],
  483. adapter->multicastlist[i][4],
  484. adapter->multicastlist[i][5]);
  485. }
  486. /* send multicast addresses to firmware */
  487. libertas_prepare_and_send_command(priv,
  488. cmd_mac_multicast_adr,
  489. cmd_act_set, 0, 0,
  490. NULL);
  491. }
  492. }
  493. }
  494. if (adapter->currentpacketfilter != oldpacketfilter) {
  495. libertas_set_mac_packet_filter(priv);
  496. }
  497. lbs_deb_leave(LBS_DEB_NET);
  498. }
  499. /**
  500. * @brief This function handles the major jobs in the WLAN driver.
  501. * It handles all events generated by firmware, RX data received
  502. * from firmware and TX data sent from kernel.
  503. *
  504. * @param data A pointer to wlan_thread structure
  505. * @return 0
  506. */
  507. static int wlan_service_main_thread(void *data)
  508. {
  509. struct wlan_thread *thread = data;
  510. wlan_private *priv = thread->priv;
  511. wlan_adapter *adapter = priv->adapter;
  512. wait_queue_t wait;
  513. u8 ireg = 0;
  514. lbs_deb_enter(LBS_DEB_THREAD);
  515. wlan_activate_thread(thread);
  516. init_waitqueue_entry(&wait, current);
  517. for (;;) {
  518. lbs_deb_thread( "main-thread 111: intcounter=%d "
  519. "currenttxskb=%p dnld_sent=%d\n",
  520. adapter->intcounter,
  521. adapter->currenttxskb, priv->dnld_sent);
  522. add_wait_queue(&thread->waitq, &wait);
  523. set_current_state(TASK_INTERRUPTIBLE);
  524. spin_lock_irq(&adapter->driver_lock);
  525. if ((adapter->psstate == PS_STATE_SLEEP) ||
  526. (!adapter->intcounter
  527. && (priv->dnld_sent || adapter->cur_cmd ||
  528. list_empty(&adapter->cmdpendingq)))) {
  529. lbs_deb_thread(
  530. "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  531. adapter->connect_status, adapter->intcounter,
  532. adapter->psmode, adapter->psstate);
  533. spin_unlock_irq(&adapter->driver_lock);
  534. schedule();
  535. } else
  536. spin_unlock_irq(&adapter->driver_lock);
  537. lbs_deb_thread(
  538. "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
  539. "dnld_sent=%d\n", adapter->intcounter,
  540. adapter->currenttxskb, priv->dnld_sent);
  541. set_current_state(TASK_RUNNING);
  542. remove_wait_queue(&thread->waitq, &wait);
  543. try_to_freeze();
  544. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
  545. "dnld_sent=%d\n",
  546. adapter->intcounter,
  547. adapter->currenttxskb, priv->dnld_sent);
  548. if (kthread_should_stop()
  549. || adapter->surpriseremoved) {
  550. lbs_deb_thread(
  551. "main-thread: break from main thread: surpriseremoved=0x%x\n",
  552. adapter->surpriseremoved);
  553. break;
  554. }
  555. spin_lock_irq(&adapter->driver_lock);
  556. if (adapter->intcounter) {
  557. u8 int_status;
  558. adapter->intcounter = 0;
  559. int_status = priv->hw_get_int_status(priv, &ireg);
  560. if (int_status) {
  561. lbs_deb_thread(
  562. "main-thread: reading HOST_INT_STATUS_REG failed\n");
  563. spin_unlock_irq(&adapter->driver_lock);
  564. continue;
  565. }
  566. adapter->hisregcpy |= ireg;
  567. }
  568. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
  569. "dnld_sent=%d\n",
  570. adapter->intcounter,
  571. adapter->currenttxskb, priv->dnld_sent);
  572. /* command response? */
  573. if (adapter->hisregcpy & his_cmdupldrdy) {
  574. lbs_deb_thread("main-thread: cmd response ready\n");
  575. adapter->hisregcpy &= ~his_cmdupldrdy;
  576. spin_unlock_irq(&adapter->driver_lock);
  577. libertas_process_rx_command(priv);
  578. spin_lock_irq(&adapter->driver_lock);
  579. }
  580. /* Any Card Event */
  581. if (adapter->hisregcpy & his_cardevent) {
  582. lbs_deb_thread("main-thread: Card Event Activity\n");
  583. adapter->hisregcpy &= ~his_cardevent;
  584. if (priv->hw_read_event_cause(priv)) {
  585. lbs_pr_alert(
  586. "main-thread: hw_read_event_cause failed\n");
  587. spin_unlock_irq(&adapter->driver_lock);
  588. continue;
  589. }
  590. spin_unlock_irq(&adapter->driver_lock);
  591. libertas_process_event(priv);
  592. } else
  593. spin_unlock_irq(&adapter->driver_lock);
  594. /* Check if we need to confirm Sleep Request received previously */
  595. if (adapter->psstate == PS_STATE_PRE_SLEEP) {
  596. if (!priv->dnld_sent && !adapter->cur_cmd) {
  597. if (adapter->connect_status ==
  598. libertas_connected) {
  599. lbs_deb_thread(
  600. "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
  601. "dnld_sent=%d cur_cmd=%p, confirm now\n",
  602. adapter->intcounter,
  603. adapter->currenttxskb,
  604. priv->dnld_sent,
  605. adapter->cur_cmd);
  606. libertas_ps_confirm_sleep(priv,
  607. (u16) adapter->psmode);
  608. } else {
  609. /* workaround for firmware sending
  610. * deauth/linkloss event immediately
  611. * after sleep request, remove this
  612. * after firmware fixes it
  613. */
  614. adapter->psstate = PS_STATE_AWAKE;
  615. lbs_pr_alert(
  616. "main-thread: ignore PS_SleepConfirm in non-connected state\n");
  617. }
  618. }
  619. }
  620. /* The PS state is changed during processing of Sleep Request
  621. * event above
  622. */
  623. if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
  624. (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
  625. continue;
  626. /* Execute the next command */
  627. if (!priv->dnld_sent && !priv->adapter->cur_cmd)
  628. libertas_execute_next_command(priv);
  629. /* Wake-up command waiters which can't sleep in
  630. * libertas_prepare_and_send_command
  631. */
  632. if (!adapter->nr_cmd_pending)
  633. wake_up_all(&adapter->cmd_pending);
  634. libertas_tx_runqueue(priv);
  635. }
  636. del_timer(&adapter->command_timer);
  637. adapter->nr_cmd_pending = 0;
  638. wake_up_all(&adapter->cmd_pending);
  639. wlan_deactivate_thread(thread);
  640. lbs_deb_leave(LBS_DEB_THREAD);
  641. return 0;
  642. }
  643. /**
  644. * @brief This function adds the card. it will probe the
  645. * card, allocate the wlan_priv and initialize the device.
  646. *
  647. * @param card A pointer to card
  648. * @return A pointer to wlan_private structure
  649. */
  650. wlan_private *libertas_add_card(void *card, struct device *dmdev)
  651. {
  652. struct net_device *dev = NULL;
  653. wlan_private *priv = NULL;
  654. lbs_deb_enter(LBS_DEB_NET);
  655. /* Allocate an Ethernet device and register it */
  656. if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
  657. lbs_pr_err("init ethX device failed\n");
  658. return NULL;
  659. }
  660. priv = dev->priv;
  661. /* allocate buffer for wlan_adapter */
  662. if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
  663. lbs_pr_err("allocate buffer for wlan_adapter failed\n");
  664. goto err_kzalloc;
  665. }
  666. priv->dev = dev;
  667. priv->card = card;
  668. priv->mesh_open = 0;
  669. priv->infra_open = 0;
  670. SET_MODULE_OWNER(dev);
  671. /* Setup the OS Interface to our functions */
  672. dev->open = wlan_open;
  673. dev->hard_start_xmit = wlan_pre_start_xmit;
  674. dev->stop = wlan_close;
  675. dev->set_mac_address = wlan_set_mac_address;
  676. dev->tx_timeout = wlan_tx_timeout;
  677. dev->get_stats = wlan_get_stats;
  678. dev->watchdog_timeo = 5 * HZ;
  679. dev->ethtool_ops = &libertas_ethtool_ops;
  680. #ifdef WIRELESS_EXT
  681. dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  682. #endif
  683. #define NETIF_F_DYNALLOC 16
  684. dev->features |= NETIF_F_DYNALLOC;
  685. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  686. dev->set_multicast_list = wlan_set_multicast_list;
  687. SET_NETDEV_DEV(dev, dmdev);
  688. INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
  689. INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
  690. spin_lock_init(&priv->adapter->driver_lock);
  691. init_waitqueue_head(&priv->adapter->cmd_pending);
  692. priv->adapter->nr_cmd_pending = 0;
  693. goto done;
  694. err_kzalloc:
  695. free_netdev(dev);
  696. priv = NULL;
  697. done:
  698. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  699. return priv;
  700. }
  701. EXPORT_SYMBOL_GPL(libertas_add_card);
  702. int libertas_activate_card(wlan_private *priv, char *fw_name)
  703. {
  704. struct net_device *dev = priv->dev;
  705. int ret = -1;
  706. lbs_deb_enter(LBS_DEB_MAIN);
  707. lbs_deb_thread("Starting kthread...\n");
  708. priv->mainthread.priv = priv;
  709. wlan_create_thread(wlan_service_main_thread,
  710. &priv->mainthread, "wlan_main_service");
  711. priv->assoc_thread =
  712. create_singlethread_workqueue("libertas_assoc");
  713. INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
  714. INIT_WORK(&priv->sync_channel, libertas_sync_channel);
  715. /*
  716. * Register the device. Fillup the private data structure with
  717. * relevant information from the card and request for the required
  718. * IRQ.
  719. */
  720. if (priv->hw_register_dev(priv) < 0) {
  721. lbs_pr_err("failed to register WLAN device\n");
  722. goto err_registerdev;
  723. }
  724. /* init FW and HW */
  725. if (fw_name && libertas_init_fw(priv, fw_name)) {
  726. lbs_pr_err("firmware init failed\n");
  727. goto err_registerdev;
  728. }
  729. if (register_netdev(dev)) {
  730. lbs_pr_err("cannot register ethX device\n");
  731. goto err_init_fw;
  732. }
  733. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  734. libertas_debugfs_init_one(priv, dev);
  735. ret = 0;
  736. goto done;
  737. err_init_fw:
  738. priv->hw_unregister_dev(priv);
  739. err_registerdev:
  740. destroy_workqueue(priv->assoc_thread);
  741. /* Stop the thread servicing the interrupts */
  742. wake_up_interruptible(&priv->mainthread.waitq);
  743. wlan_terminate_thread(&priv->mainthread);
  744. done:
  745. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  746. return ret;
  747. }
  748. EXPORT_SYMBOL_GPL(libertas_activate_card);
  749. /**
  750. * @brief This function adds mshX interface
  751. *
  752. * @param priv A pointer to the wlan_private structure
  753. * @return 0 if successful, -X otherwise
  754. */
  755. int libertas_add_mesh(wlan_private *priv, struct device *dev)
  756. {
  757. struct net_device *mesh_dev = NULL;
  758. int ret = 0;
  759. lbs_deb_enter(LBS_DEB_MESH);
  760. /* Allocate a virtual mesh device */
  761. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  762. lbs_deb_mesh("init mshX device failed\n");
  763. ret = -ENOMEM;
  764. goto done;
  765. }
  766. mesh_dev->priv = priv;
  767. priv->mesh_dev = mesh_dev;
  768. SET_MODULE_OWNER(mesh_dev);
  769. mesh_dev->open = mesh_open;
  770. mesh_dev->hard_start_xmit = mesh_pre_start_xmit;
  771. mesh_dev->stop = mesh_close;
  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");