main.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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 <linux/kthread.h>
  13. #include <net/iw_handler.h>
  14. #include <net/ieee80211.h>
  15. #include "host.h"
  16. #include "decl.h"
  17. #include "dev.h"
  18. #include "wext.h"
  19. #include "debugfs.h"
  20. #include "assoc.h"
  21. #define DRIVER_RELEASE_VERSION "322.p1"
  22. const char libertas_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  23. #ifdef DEBUG
  24. "-dbg"
  25. #endif
  26. "";
  27. /* Module parameters */
  28. unsigned int libertas_debug = 0;
  29. module_param(libertas_debug, int, 0644);
  30. EXPORT_SYMBOL_GPL(libertas_debug);
  31. #define WLAN_TX_PWR_DEFAULT 20 /*100mW */
  32. #define WLAN_TX_PWR_US_DEFAULT 20 /*100mW */
  33. #define WLAN_TX_PWR_JP_DEFAULT 16 /*50mW */
  34. #define WLAN_TX_PWR_FR_DEFAULT 20 /*100mW */
  35. #define WLAN_TX_PWR_EMEA_DEFAULT 20 /*100mW */
  36. /* Format { channel, frequency (MHz), maxtxpower } */
  37. /* band: 'B/G', region: USA FCC/Canada IC */
  38. static struct chan_freq_power channel_freq_power_US_BG[] = {
  39. {1, 2412, WLAN_TX_PWR_US_DEFAULT},
  40. {2, 2417, WLAN_TX_PWR_US_DEFAULT},
  41. {3, 2422, WLAN_TX_PWR_US_DEFAULT},
  42. {4, 2427, WLAN_TX_PWR_US_DEFAULT},
  43. {5, 2432, WLAN_TX_PWR_US_DEFAULT},
  44. {6, 2437, WLAN_TX_PWR_US_DEFAULT},
  45. {7, 2442, WLAN_TX_PWR_US_DEFAULT},
  46. {8, 2447, WLAN_TX_PWR_US_DEFAULT},
  47. {9, 2452, WLAN_TX_PWR_US_DEFAULT},
  48. {10, 2457, WLAN_TX_PWR_US_DEFAULT},
  49. {11, 2462, WLAN_TX_PWR_US_DEFAULT}
  50. };
  51. /* band: 'B/G', region: Europe ETSI */
  52. static struct chan_freq_power channel_freq_power_EU_BG[] = {
  53. {1, 2412, WLAN_TX_PWR_EMEA_DEFAULT},
  54. {2, 2417, WLAN_TX_PWR_EMEA_DEFAULT},
  55. {3, 2422, WLAN_TX_PWR_EMEA_DEFAULT},
  56. {4, 2427, WLAN_TX_PWR_EMEA_DEFAULT},
  57. {5, 2432, WLAN_TX_PWR_EMEA_DEFAULT},
  58. {6, 2437, WLAN_TX_PWR_EMEA_DEFAULT},
  59. {7, 2442, WLAN_TX_PWR_EMEA_DEFAULT},
  60. {8, 2447, WLAN_TX_PWR_EMEA_DEFAULT},
  61. {9, 2452, WLAN_TX_PWR_EMEA_DEFAULT},
  62. {10, 2457, WLAN_TX_PWR_EMEA_DEFAULT},
  63. {11, 2462, WLAN_TX_PWR_EMEA_DEFAULT},
  64. {12, 2467, WLAN_TX_PWR_EMEA_DEFAULT},
  65. {13, 2472, WLAN_TX_PWR_EMEA_DEFAULT}
  66. };
  67. /* band: 'B/G', region: Spain */
  68. static struct chan_freq_power channel_freq_power_SPN_BG[] = {
  69. {10, 2457, WLAN_TX_PWR_DEFAULT},
  70. {11, 2462, WLAN_TX_PWR_DEFAULT}
  71. };
  72. /* band: 'B/G', region: France */
  73. static struct chan_freq_power channel_freq_power_FR_BG[] = {
  74. {10, 2457, WLAN_TX_PWR_FR_DEFAULT},
  75. {11, 2462, WLAN_TX_PWR_FR_DEFAULT},
  76. {12, 2467, WLAN_TX_PWR_FR_DEFAULT},
  77. {13, 2472, WLAN_TX_PWR_FR_DEFAULT}
  78. };
  79. /* band: 'B/G', region: Japan */
  80. static struct chan_freq_power channel_freq_power_JPN_BG[] = {
  81. {1, 2412, WLAN_TX_PWR_JP_DEFAULT},
  82. {2, 2417, WLAN_TX_PWR_JP_DEFAULT},
  83. {3, 2422, WLAN_TX_PWR_JP_DEFAULT},
  84. {4, 2427, WLAN_TX_PWR_JP_DEFAULT},
  85. {5, 2432, WLAN_TX_PWR_JP_DEFAULT},
  86. {6, 2437, WLAN_TX_PWR_JP_DEFAULT},
  87. {7, 2442, WLAN_TX_PWR_JP_DEFAULT},
  88. {8, 2447, WLAN_TX_PWR_JP_DEFAULT},
  89. {9, 2452, WLAN_TX_PWR_JP_DEFAULT},
  90. {10, 2457, WLAN_TX_PWR_JP_DEFAULT},
  91. {11, 2462, WLAN_TX_PWR_JP_DEFAULT},
  92. {12, 2467, WLAN_TX_PWR_JP_DEFAULT},
  93. {13, 2472, WLAN_TX_PWR_JP_DEFAULT},
  94. {14, 2484, WLAN_TX_PWR_JP_DEFAULT}
  95. };
  96. /**
  97. * the structure for channel, frequency and power
  98. */
  99. struct region_cfp_table {
  100. u8 region;
  101. struct chan_freq_power *cfp_BG;
  102. int cfp_no_BG;
  103. };
  104. /**
  105. * the structure for the mapping between region and CFP
  106. */
  107. static struct region_cfp_table region_cfp_table[] = {
  108. {0x10, /*US FCC */
  109. channel_freq_power_US_BG,
  110. sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
  111. }
  112. ,
  113. {0x20, /*CANADA IC */
  114. channel_freq_power_US_BG,
  115. sizeof(channel_freq_power_US_BG) / sizeof(struct chan_freq_power),
  116. }
  117. ,
  118. {0x30, /*EU*/ channel_freq_power_EU_BG,
  119. sizeof(channel_freq_power_EU_BG) / sizeof(struct chan_freq_power),
  120. }
  121. ,
  122. {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
  123. sizeof(channel_freq_power_SPN_BG) / sizeof(struct chan_freq_power),
  124. }
  125. ,
  126. {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
  127. sizeof(channel_freq_power_FR_BG) / sizeof(struct chan_freq_power),
  128. }
  129. ,
  130. {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
  131. sizeof(channel_freq_power_JPN_BG) / sizeof(struct chan_freq_power),
  132. }
  133. ,
  134. /*Add new region here */
  135. };
  136. /**
  137. * the table to keep region code
  138. */
  139. u16 libertas_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  140. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  141. /**
  142. * 802.11b/g supported bitrates (in 500Kb/s units)
  143. */
  144. u8 libertas_bg_rates[MAX_RATES] =
  145. { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  146. 0x00, 0x00 };
  147. /**
  148. * FW rate table. FW refers to rates by their index in this table, not by the
  149. * rate value itself. Values of 0x00 are
  150. * reserved positions.
  151. */
  152. static u8 fw_data_rates[MAX_RATES] =
  153. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  154. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  155. };
  156. /**
  157. * @brief use index to get the data rate
  158. *
  159. * @param idx The index of data rate
  160. * @return data rate or 0
  161. */
  162. u32 libertas_fw_index_to_data_rate(u8 idx)
  163. {
  164. if (idx >= sizeof(fw_data_rates))
  165. idx = 0;
  166. return fw_data_rates[idx];
  167. }
  168. /**
  169. * @brief use rate to get the index
  170. *
  171. * @param rate data rate
  172. * @return index or 0
  173. */
  174. u8 libertas_data_rate_to_fw_index(u32 rate)
  175. {
  176. u8 i;
  177. if (!rate)
  178. return 0;
  179. for (i = 0; i < sizeof(fw_data_rates); i++) {
  180. if (rate == fw_data_rates[i])
  181. return i;
  182. }
  183. return 0;
  184. }
  185. /**
  186. * Attributes exported through sysfs
  187. */
  188. /**
  189. * @brief Get function for sysfs attribute anycast_mask
  190. */
  191. static ssize_t libertas_anycast_get(struct device * dev,
  192. struct device_attribute *attr, char * buf)
  193. {
  194. struct cmd_ds_mesh_access mesh_access;
  195. memset(&mesh_access, 0, sizeof(mesh_access));
  196. libertas_prepare_and_send_command(to_net_dev(dev)->priv,
  197. CMD_MESH_ACCESS,
  198. CMD_ACT_MESH_GET_ANYCAST,
  199. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  200. return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
  201. }
  202. /**
  203. * @brief Set function for sysfs attribute anycast_mask
  204. */
  205. static ssize_t libertas_anycast_set(struct device * dev,
  206. struct device_attribute *attr, const char * buf, size_t count)
  207. {
  208. struct cmd_ds_mesh_access mesh_access;
  209. uint32_t datum;
  210. memset(&mesh_access, 0, sizeof(mesh_access));
  211. sscanf(buf, "%x", &datum);
  212. mesh_access.data[0] = cpu_to_le32(datum);
  213. libertas_prepare_and_send_command((to_net_dev(dev))->priv,
  214. CMD_MESH_ACCESS,
  215. CMD_ACT_MESH_SET_ANYCAST,
  216. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  217. return strlen(buf);
  218. }
  219. /**
  220. * anycast_mask attribute to be exported per mshX interface
  221. * through sysfs (/sys/class/net/mshX/anycast_mask)
  222. */
  223. static DEVICE_ATTR(anycast_mask, 0644, libertas_anycast_get, libertas_anycast_set);
  224. static ssize_t libertas_autostart_enabled_get(struct device * dev,
  225. struct device_attribute *attr, char * buf)
  226. {
  227. struct cmd_ds_mesh_access mesh_access;
  228. memset(&mesh_access, 0, sizeof(mesh_access));
  229. libertas_prepare_and_send_command(to_net_dev(dev)->priv,
  230. CMD_MESH_ACCESS,
  231. CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
  232. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  233. return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
  234. }
  235. static ssize_t libertas_autostart_enabled_set(struct device * dev,
  236. struct device_attribute *attr, const char * buf, size_t count)
  237. {
  238. struct cmd_ds_mesh_access mesh_access;
  239. uint32_t datum;
  240. memset(&mesh_access, 0, sizeof(mesh_access));
  241. sscanf(buf, "%d", &datum);
  242. mesh_access.data[0] = cpu_to_le32(datum);
  243. libertas_prepare_and_send_command((to_net_dev(dev))->priv,
  244. CMD_MESH_ACCESS,
  245. CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
  246. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  247. return strlen(buf);
  248. }
  249. static DEVICE_ATTR(autostart_enabled, 0644,
  250. libertas_autostart_enabled_get, libertas_autostart_enabled_set);
  251. static struct attribute *libertas_mesh_sysfs_entries[] = {
  252. &dev_attr_anycast_mask.attr,
  253. &dev_attr_autostart_enabled.attr,
  254. NULL,
  255. };
  256. static struct attribute_group libertas_mesh_attr_group = {
  257. .attrs = libertas_mesh_sysfs_entries,
  258. };
  259. /**
  260. * @brief Check if the device can be open and wait if necessary.
  261. *
  262. * @param dev A pointer to net_device structure
  263. * @return 0
  264. *
  265. * For USB adapter, on some systems the device open handler will be
  266. * called before FW ready. Use the following flag check and wait
  267. * function to work around the issue.
  268. *
  269. */
  270. static int pre_open_check(struct net_device *dev)
  271. {
  272. wlan_private *priv = (wlan_private *) dev->priv;
  273. wlan_adapter *adapter = priv->adapter;
  274. int i = 0;
  275. while (!adapter->fw_ready && i < 20) {
  276. i++;
  277. msleep_interruptible(100);
  278. }
  279. if (!adapter->fw_ready) {
  280. lbs_pr_err("firmware not ready\n");
  281. return -1;
  282. }
  283. return 0;
  284. }
  285. /**
  286. * @brief This function opens the device
  287. *
  288. * @param dev A pointer to net_device structure
  289. * @return 0
  290. */
  291. static int libertas_dev_open(struct net_device *dev)
  292. {
  293. wlan_private *priv = (wlan_private *) dev->priv;
  294. wlan_adapter *adapter = priv->adapter;
  295. lbs_deb_enter(LBS_DEB_NET);
  296. priv->open = 1;
  297. if (adapter->connect_status == LIBERTAS_CONNECTED) {
  298. netif_carrier_on(priv->dev);
  299. if (priv->mesh_dev)
  300. netif_carrier_on(priv->mesh_dev);
  301. } else {
  302. netif_carrier_off(priv->dev);
  303. if (priv->mesh_dev)
  304. netif_carrier_off(priv->mesh_dev);
  305. }
  306. lbs_deb_leave(LBS_DEB_NET);
  307. return 0;
  308. }
  309. /**
  310. * @brief This function opens the mshX interface
  311. *
  312. * @param dev A pointer to net_device structure
  313. * @return 0
  314. */
  315. static int libertas_mesh_open(struct net_device *dev)
  316. {
  317. wlan_private *priv = (wlan_private *) dev->priv ;
  318. if (pre_open_check(dev) == -1)
  319. return -1;
  320. priv->mesh_open = 1 ;
  321. netif_wake_queue(priv->mesh_dev);
  322. if (priv->infra_open == 0)
  323. return libertas_dev_open(priv->dev) ;
  324. return 0;
  325. }
  326. /**
  327. * @brief This function opens the ethX interface
  328. *
  329. * @param dev A pointer to net_device structure
  330. * @return 0
  331. */
  332. static int libertas_open(struct net_device *dev)
  333. {
  334. wlan_private *priv = (wlan_private *) dev->priv ;
  335. if(pre_open_check(dev) == -1)
  336. return -1;
  337. priv->infra_open = 1 ;
  338. netif_wake_queue(priv->dev);
  339. if (priv->open == 0)
  340. return libertas_dev_open(priv->dev) ;
  341. return 0;
  342. }
  343. static int libertas_dev_close(struct net_device *dev)
  344. {
  345. wlan_private *priv = dev->priv;
  346. lbs_deb_enter(LBS_DEB_NET);
  347. netif_carrier_off(priv->dev);
  348. priv->open = 0;
  349. lbs_deb_leave(LBS_DEB_NET);
  350. return 0;
  351. }
  352. /**
  353. * @brief This function closes the mshX interface
  354. *
  355. * @param dev A pointer to net_device structure
  356. * @return 0
  357. */
  358. static int libertas_mesh_close(struct net_device *dev)
  359. {
  360. wlan_private *priv = (wlan_private *) (dev->priv);
  361. priv->mesh_open = 0;
  362. netif_stop_queue(priv->mesh_dev);
  363. if (priv->infra_open == 0)
  364. return libertas_dev_close(dev);
  365. else
  366. return 0;
  367. }
  368. /**
  369. * @brief This function closes the ethX interface
  370. *
  371. * @param dev A pointer to net_device structure
  372. * @return 0
  373. */
  374. static int libertas_close(struct net_device *dev)
  375. {
  376. wlan_private *priv = (wlan_private *) dev->priv;
  377. netif_stop_queue(dev);
  378. priv->infra_open = 0;
  379. if (priv->mesh_open == 0)
  380. return libertas_dev_close(dev);
  381. else
  382. return 0;
  383. }
  384. static int libertas_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  385. {
  386. int ret = 0;
  387. wlan_private *priv = dev->priv;
  388. lbs_deb_enter(LBS_DEB_NET);
  389. if (priv->dnld_sent || priv->adapter->TxLockFlag) {
  390. priv->stats.tx_dropped++;
  391. goto done;
  392. }
  393. netif_stop_queue(priv->dev);
  394. if (priv->mesh_dev)
  395. netif_stop_queue(priv->mesh_dev);
  396. if (libertas_process_tx(priv, skb) == 0)
  397. dev->trans_start = jiffies;
  398. done:
  399. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  400. return ret;
  401. }
  402. /**
  403. * @brief Mark mesh packets and handover them to libertas_hard_start_xmit
  404. *
  405. */
  406. static int libertas_mesh_pre_start_xmit(struct sk_buff *skb,
  407. struct net_device *dev)
  408. {
  409. wlan_private *priv = dev->priv;
  410. int ret;
  411. lbs_deb_enter(LBS_DEB_MESH);
  412. SET_MESH_FRAME(skb);
  413. ret = libertas_hard_start_xmit(skb, priv->dev);
  414. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  415. return ret;
  416. }
  417. /**
  418. * @brief Mark non-mesh packets and handover them to libertas_hard_start_xmit
  419. *
  420. */
  421. static int libertas_pre_start_xmit(struct sk_buff *skb, struct net_device *dev)
  422. {
  423. int ret;
  424. lbs_deb_enter(LBS_DEB_NET);
  425. UNSET_MESH_FRAME(skb);
  426. ret = libertas_hard_start_xmit(skb, dev);
  427. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  428. return ret;
  429. }
  430. static void libertas_tx_timeout(struct net_device *dev)
  431. {
  432. wlan_private *priv = (wlan_private *) dev->priv;
  433. lbs_deb_enter(LBS_DEB_TX);
  434. lbs_pr_err("tx watch dog timeout\n");
  435. priv->dnld_sent = DNLD_RES_RECEIVED;
  436. dev->trans_start = jiffies;
  437. if (priv->adapter->currenttxskb) {
  438. if (priv->adapter->radiomode == WLAN_RADIOMODE_RADIOTAP) {
  439. /* If we are here, we have not received feedback from
  440. the previous packet. Assume TX_FAIL and move on. */
  441. priv->adapter->eventcause = 0x01000000;
  442. libertas_send_tx_feedback(priv);
  443. } else
  444. wake_up_interruptible(&priv->waitq);
  445. } else if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
  446. netif_wake_queue(priv->dev);
  447. if (priv->mesh_dev)
  448. netif_wake_queue(priv->mesh_dev);
  449. }
  450. lbs_deb_leave(LBS_DEB_TX);
  451. }
  452. /**
  453. * @brief This function returns the network statistics
  454. *
  455. * @param dev A pointer to wlan_private structure
  456. * @return A pointer to net_device_stats structure
  457. */
  458. static struct net_device_stats *libertas_get_stats(struct net_device *dev)
  459. {
  460. wlan_private *priv = (wlan_private *) dev->priv;
  461. return &priv->stats;
  462. }
  463. static int libertas_set_mac_address(struct net_device *dev, void *addr)
  464. {
  465. int ret = 0;
  466. wlan_private *priv = (wlan_private *) dev->priv;
  467. wlan_adapter *adapter = priv->adapter;
  468. struct sockaddr *phwaddr = addr;
  469. lbs_deb_enter(LBS_DEB_NET);
  470. /* In case it was called from the mesh device */
  471. dev = priv->dev ;
  472. memset(adapter->current_addr, 0, ETH_ALEN);
  473. /* dev->dev_addr is 8 bytes */
  474. lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
  475. lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
  476. memcpy(adapter->current_addr, phwaddr->sa_data, ETH_ALEN);
  477. ret = libertas_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
  478. CMD_ACT_SET,
  479. CMD_OPTION_WAITFORRSP, 0, NULL);
  480. if (ret) {
  481. lbs_deb_net("set MAC address failed\n");
  482. ret = -1;
  483. goto done;
  484. }
  485. lbs_deb_hex(LBS_DEB_NET, "adapter->macaddr", adapter->current_addr, ETH_ALEN);
  486. memcpy(dev->dev_addr, adapter->current_addr, ETH_ALEN);
  487. if (priv->mesh_dev)
  488. memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
  489. done:
  490. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  491. return ret;
  492. }
  493. static int libertas_copy_multicast_address(wlan_adapter * adapter,
  494. struct net_device *dev)
  495. {
  496. int i = 0;
  497. struct dev_mc_list *mcptr = dev->mc_list;
  498. for (i = 0; i < dev->mc_count; i++) {
  499. memcpy(&adapter->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  500. mcptr = mcptr->next;
  501. }
  502. return i;
  503. }
  504. static void libertas_set_multicast_list(struct net_device *dev)
  505. {
  506. wlan_private *priv = dev->priv;
  507. wlan_adapter *adapter = priv->adapter;
  508. int oldpacketfilter;
  509. lbs_deb_enter(LBS_DEB_NET);
  510. oldpacketfilter = adapter->currentpacketfilter;
  511. if (dev->flags & IFF_PROMISC) {
  512. lbs_deb_net("enable promiscuous mode\n");
  513. adapter->currentpacketfilter |=
  514. CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  515. adapter->currentpacketfilter &=
  516. ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  517. CMD_ACT_MAC_MULTICAST_ENABLE);
  518. } else {
  519. /* Multicast */
  520. adapter->currentpacketfilter &=
  521. ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  522. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  523. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  524. lbs_deb_net( "enabling all multicast\n");
  525. adapter->currentpacketfilter |=
  526. CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  527. adapter->currentpacketfilter &=
  528. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  529. } else {
  530. adapter->currentpacketfilter &=
  531. ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  532. if (!dev->mc_count) {
  533. lbs_deb_net("no multicast addresses, "
  534. "disabling multicast\n");
  535. adapter->currentpacketfilter &=
  536. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  537. } else {
  538. int i;
  539. adapter->currentpacketfilter |=
  540. CMD_ACT_MAC_MULTICAST_ENABLE;
  541. adapter->nr_of_multicastmacaddr =
  542. libertas_copy_multicast_address(adapter, dev);
  543. lbs_deb_net("multicast addresses: %d\n",
  544. dev->mc_count);
  545. for (i = 0; i < dev->mc_count; i++) {
  546. lbs_deb_net("Multicast address %d:"
  547. MAC_FMT "\n", i,
  548. adapter->multicastlist[i][0],
  549. adapter->multicastlist[i][1],
  550. adapter->multicastlist[i][2],
  551. adapter->multicastlist[i][3],
  552. adapter->multicastlist[i][4],
  553. adapter->multicastlist[i][5]);
  554. }
  555. /* send multicast addresses to firmware */
  556. libertas_prepare_and_send_command(priv,
  557. CMD_MAC_MULTICAST_ADR,
  558. CMD_ACT_SET, 0, 0,
  559. NULL);
  560. }
  561. }
  562. }
  563. if (adapter->currentpacketfilter != oldpacketfilter) {
  564. libertas_set_mac_packet_filter(priv);
  565. }
  566. lbs_deb_leave(LBS_DEB_NET);
  567. }
  568. /**
  569. * @brief This function handles the major jobs in the WLAN driver.
  570. * It handles all events generated by firmware, RX data received
  571. * from firmware and TX data sent from kernel.
  572. *
  573. * @param data A pointer to wlan_thread structure
  574. * @return 0
  575. */
  576. static int libertas_thread(void *data)
  577. {
  578. struct net_device *dev = data;
  579. wlan_private *priv = dev->priv;
  580. wlan_adapter *adapter = priv->adapter;
  581. wait_queue_t wait;
  582. u8 ireg = 0;
  583. lbs_deb_enter(LBS_DEB_THREAD);
  584. init_waitqueue_entry(&wait, current);
  585. for (;;) {
  586. lbs_deb_thread( "main-thread 111: intcounter=%d "
  587. "currenttxskb=%p dnld_sent=%d\n",
  588. adapter->intcounter,
  589. adapter->currenttxskb, priv->dnld_sent);
  590. add_wait_queue(&priv->waitq, &wait);
  591. set_current_state(TASK_INTERRUPTIBLE);
  592. spin_lock_irq(&adapter->driver_lock);
  593. if ((adapter->psstate == PS_STATE_SLEEP) ||
  594. (!adapter->intcounter
  595. && (priv->dnld_sent || adapter->cur_cmd ||
  596. list_empty(&adapter->cmdpendingq)))) {
  597. lbs_deb_thread(
  598. "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  599. adapter->connect_status, adapter->intcounter,
  600. adapter->psmode, adapter->psstate);
  601. spin_unlock_irq(&adapter->driver_lock);
  602. schedule();
  603. } else
  604. spin_unlock_irq(&adapter->driver_lock);
  605. lbs_deb_thread(
  606. "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
  607. "dnld_sent=%d\n", adapter->intcounter,
  608. adapter->currenttxskb, priv->dnld_sent);
  609. set_current_state(TASK_RUNNING);
  610. remove_wait_queue(&priv->waitq, &wait);
  611. try_to_freeze();
  612. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
  613. "dnld_sent=%d\n",
  614. adapter->intcounter,
  615. adapter->currenttxskb, priv->dnld_sent);
  616. if (kthread_should_stop()
  617. || adapter->surpriseremoved) {
  618. lbs_deb_thread(
  619. "main-thread: break from main thread: surpriseremoved=0x%x\n",
  620. adapter->surpriseremoved);
  621. break;
  622. }
  623. spin_lock_irq(&adapter->driver_lock);
  624. if (adapter->intcounter) {
  625. u8 int_status;
  626. adapter->intcounter = 0;
  627. int_status = priv->hw_get_int_status(priv, &ireg);
  628. if (int_status) {
  629. lbs_deb_thread(
  630. "main-thread: reading HOST_INT_STATUS_REG failed\n");
  631. spin_unlock_irq(&adapter->driver_lock);
  632. continue;
  633. }
  634. adapter->hisregcpy |= ireg;
  635. }
  636. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
  637. "dnld_sent=%d\n",
  638. adapter->intcounter,
  639. adapter->currenttxskb, priv->dnld_sent);
  640. /* command response? */
  641. if (adapter->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
  642. lbs_deb_thread("main-thread: cmd response ready\n");
  643. adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
  644. spin_unlock_irq(&adapter->driver_lock);
  645. libertas_process_rx_command(priv);
  646. spin_lock_irq(&adapter->driver_lock);
  647. }
  648. /* Any Card Event */
  649. if (adapter->hisregcpy & MRVDRV_CARDEVENT) {
  650. lbs_deb_thread("main-thread: Card Event Activity\n");
  651. adapter->hisregcpy &= ~MRVDRV_CARDEVENT;
  652. if (priv->hw_read_event_cause(priv)) {
  653. lbs_pr_alert(
  654. "main-thread: hw_read_event_cause failed\n");
  655. spin_unlock_irq(&adapter->driver_lock);
  656. continue;
  657. }
  658. spin_unlock_irq(&adapter->driver_lock);
  659. libertas_process_event(priv);
  660. } else
  661. spin_unlock_irq(&adapter->driver_lock);
  662. /* Check if we need to confirm Sleep Request received previously */
  663. if (adapter->psstate == PS_STATE_PRE_SLEEP) {
  664. if (!priv->dnld_sent && !adapter->cur_cmd) {
  665. if (adapter->connect_status ==
  666. LIBERTAS_CONNECTED) {
  667. lbs_deb_thread(
  668. "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
  669. "dnld_sent=%d cur_cmd=%p, confirm now\n",
  670. adapter->intcounter,
  671. adapter->currenttxskb,
  672. priv->dnld_sent,
  673. adapter->cur_cmd);
  674. libertas_ps_confirm_sleep(priv,
  675. (u16) adapter->psmode);
  676. } else {
  677. /* workaround for firmware sending
  678. * deauth/linkloss event immediately
  679. * after sleep request, remove this
  680. * after firmware fixes it
  681. */
  682. adapter->psstate = PS_STATE_AWAKE;
  683. lbs_pr_alert(
  684. "main-thread: ignore PS_SleepConfirm in non-connected state\n");
  685. }
  686. }
  687. }
  688. /* The PS state is changed during processing of Sleep Request
  689. * event above
  690. */
  691. if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
  692. (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
  693. continue;
  694. /* Execute the next command */
  695. if (!priv->dnld_sent && !priv->adapter->cur_cmd)
  696. libertas_execute_next_command(priv);
  697. /* Wake-up command waiters which can't sleep in
  698. * libertas_prepare_and_send_command
  699. */
  700. if (!adapter->nr_cmd_pending)
  701. wake_up_all(&adapter->cmd_pending);
  702. libertas_tx_runqueue(priv);
  703. }
  704. del_timer(&adapter->command_timer);
  705. adapter->nr_cmd_pending = 0;
  706. wake_up_all(&adapter->cmd_pending);
  707. lbs_deb_leave(LBS_DEB_THREAD);
  708. return 0;
  709. }
  710. /**
  711. * @brief This function downloads firmware image, gets
  712. * HW spec from firmware and set basic parameters to
  713. * firmware.
  714. *
  715. * @param priv A pointer to wlan_private structure
  716. * @return 0 or -1
  717. */
  718. static int wlan_setup_station_hw(wlan_private * priv)
  719. {
  720. int ret = -1;
  721. wlan_adapter *adapter = priv->adapter;
  722. lbs_deb_enter(LBS_DEB_FW);
  723. ret = priv->hw_prog_firmware(priv);
  724. if (ret) {
  725. lbs_deb_fw("bootloader in invalid state\n");
  726. ret = -1;
  727. goto done;
  728. }
  729. /*
  730. * Read MAC address from HW
  731. */
  732. memset(adapter->current_addr, 0xff, ETH_ALEN);
  733. ret = libertas_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
  734. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  735. if (ret) {
  736. ret = -1;
  737. goto done;
  738. }
  739. libertas_set_mac_packet_filter(priv);
  740. /* Get the supported Data rates */
  741. ret = libertas_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
  742. CMD_ACT_GET_TX_RATE,
  743. CMD_OPTION_WAITFORRSP, 0, NULL);
  744. if (ret) {
  745. ret = -1;
  746. goto done;
  747. }
  748. ret = 0;
  749. done:
  750. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  751. return ret;
  752. }
  753. static void command_timer_fn(unsigned long data);
  754. /**
  755. * This function handles the timeout of command sending.
  756. * It will re-send the same command again.
  757. */
  758. static void command_timer_fn(unsigned long data)
  759. {
  760. wlan_private *priv = (wlan_private *)data;
  761. wlan_adapter *adapter = priv->adapter;
  762. struct cmd_ctrl_node *ptempnode;
  763. struct cmd_ds_command *cmd;
  764. unsigned long flags;
  765. ptempnode = adapter->cur_cmd;
  766. if (ptempnode == NULL) {
  767. lbs_deb_fw("ptempnode empty\n");
  768. return;
  769. }
  770. cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
  771. if (!cmd) {
  772. lbs_deb_fw("cmd is NULL\n");
  773. return;
  774. }
  775. lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
  776. if (!adapter->fw_ready)
  777. return;
  778. spin_lock_irqsave(&adapter->driver_lock, flags);
  779. adapter->cur_cmd = NULL;
  780. spin_unlock_irqrestore(&adapter->driver_lock, flags);
  781. lbs_deb_fw("re-sending same command because of timeout\n");
  782. libertas_queue_cmd(adapter, ptempnode, 0);
  783. wake_up_interruptible(&priv->waitq);
  784. return;
  785. }
  786. static void libertas_free_adapter(wlan_private * priv)
  787. {
  788. wlan_adapter *adapter = priv->adapter;
  789. if (!adapter) {
  790. lbs_deb_fw("why double free adapter?\n");
  791. return;
  792. }
  793. lbs_deb_fw("free command buffer\n");
  794. libertas_free_cmd_buffer(priv);
  795. lbs_deb_fw("free command_timer\n");
  796. del_timer(&adapter->command_timer);
  797. lbs_deb_fw("free scan results table\n");
  798. kfree(adapter->networks);
  799. adapter->networks = NULL;
  800. /* Free the adapter object itself */
  801. lbs_deb_fw("free adapter\n");
  802. kfree(adapter);
  803. priv->adapter = NULL;
  804. }
  805. static int wlan_allocate_adapter(wlan_private * priv)
  806. {
  807. size_t bufsize;
  808. wlan_adapter *adapter = priv->adapter;
  809. /* Allocate buffer to store the BSSID list */
  810. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  811. adapter->networks = kzalloc(bufsize, GFP_KERNEL);
  812. if (!adapter->networks) {
  813. lbs_pr_err("Out of memory allocating beacons\n");
  814. libertas_free_adapter(priv);
  815. return -ENOMEM;
  816. }
  817. /* Allocate the command buffers */
  818. libertas_allocate_cmd_buffer(priv);
  819. memset(&adapter->libertas_ps_confirm_sleep, 0, sizeof(struct PS_CMD_ConfirmSleep));
  820. adapter->libertas_ps_confirm_sleep.seqnum = cpu_to_le16(++adapter->seqnum);
  821. adapter->libertas_ps_confirm_sleep.command =
  822. cpu_to_le16(CMD_802_11_PS_MODE);
  823. adapter->libertas_ps_confirm_sleep.size =
  824. cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
  825. adapter->libertas_ps_confirm_sleep.result = 0;
  826. adapter->libertas_ps_confirm_sleep.action =
  827. cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  828. return 0;
  829. }
  830. static void wlan_init_adapter(wlan_private * priv)
  831. {
  832. wlan_adapter *adapter = priv->adapter;
  833. int i;
  834. adapter->connect_status = LIBERTAS_DISCONNECTED;
  835. memset(adapter->current_addr, 0xff, ETH_ALEN);
  836. /* scan type */
  837. adapter->scantype = CMD_SCAN_TYPE_ACTIVE;
  838. /* scan mode */
  839. adapter->scanmode = CMD_BSS_TYPE_ANY;
  840. /* 802.11 specific */
  841. adapter->secinfo.wep_enabled = 0;
  842. for (i = 0; i < sizeof(adapter->wep_keys) / sizeof(adapter->wep_keys[0]);
  843. i++)
  844. memset(&adapter->wep_keys[i], 0, sizeof(struct enc_key));
  845. adapter->wep_tx_keyidx = 0;
  846. adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  847. adapter->mode = IW_MODE_INFRA;
  848. adapter->pending_assoc_req = NULL;
  849. adapter->in_progress_assoc_req = NULL;
  850. /* Initialize scan result lists */
  851. INIT_LIST_HEAD(&adapter->network_free_list);
  852. INIT_LIST_HEAD(&adapter->network_list);
  853. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  854. list_add_tail(&adapter->networks[i].list,
  855. &adapter->network_free_list);
  856. }
  857. mutex_init(&adapter->lock);
  858. memset(&adapter->curbssparams, 0, sizeof(adapter->curbssparams));
  859. adapter->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  860. /* PnP and power profile */
  861. adapter->surpriseremoved = 0;
  862. adapter->currentpacketfilter =
  863. CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  864. adapter->radioon = RADIO_ON;
  865. adapter->auto_rate = 1;
  866. adapter->cur_rate = 0;
  867. adapter->adhoc_grate_enabled = 0;
  868. adapter->beaconperiod = MRVDRV_BEACON_INTERVAL;
  869. // set default capabilities
  870. adapter->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  871. adapter->psmode = WLAN802_11POWERMODECAM;
  872. adapter->listeninterval = MRVDRV_DEFAULT_LISTEN_INTERVAL;
  873. adapter->psstate = PS_STATE_FULL_POWER;
  874. adapter->needtowakeup = 0;
  875. adapter->intcounter = 0;
  876. adapter->currenttxskb = NULL;
  877. memset(&adapter->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
  878. adapter->tx_queue_idx = 0;
  879. spin_lock_init(&adapter->txqueue_lock);
  880. return;
  881. }
  882. static int libertas_init_fw(wlan_private * priv)
  883. {
  884. int ret = -1;
  885. wlan_adapter *adapter = priv->adapter;
  886. lbs_deb_enter(LBS_DEB_FW);
  887. /* Allocate adapter structure */
  888. if ((ret = wlan_allocate_adapter(priv)) != 0)
  889. goto done;
  890. /* init adapter structure */
  891. wlan_init_adapter(priv);
  892. /* init timer etc. */
  893. setup_timer(&adapter->command_timer, command_timer_fn,
  894. (unsigned long)priv);
  895. /* download fimrware etc. */
  896. if ((ret = wlan_setup_station_hw(priv)) != 0) {
  897. del_timer_sync(&adapter->command_timer);
  898. goto done;
  899. }
  900. /* init 802.11d */
  901. libertas_init_11d(priv);
  902. ret = 0;
  903. done:
  904. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  905. return ret;
  906. }
  907. /**
  908. * @brief This function adds the card. it will probe the
  909. * card, allocate the wlan_priv and initialize the device.
  910. *
  911. * @param card A pointer to card
  912. * @return A pointer to wlan_private structure
  913. */
  914. wlan_private *libertas_add_card(void *card, struct device *dmdev)
  915. {
  916. struct net_device *dev = NULL;
  917. wlan_private *priv = NULL;
  918. lbs_deb_enter(LBS_DEB_NET);
  919. /* Allocate an Ethernet device and register it */
  920. if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
  921. lbs_pr_err("init ethX device failed\n");
  922. return NULL;
  923. }
  924. priv = dev->priv;
  925. /* allocate buffer for wlan_adapter */
  926. if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
  927. lbs_pr_err("allocate buffer for wlan_adapter failed\n");
  928. goto err_kzalloc;
  929. }
  930. priv->dev = dev;
  931. priv->card = card;
  932. priv->mesh_open = 0;
  933. priv->infra_open = 0;
  934. SET_MODULE_OWNER(dev);
  935. /* Setup the OS Interface to our functions */
  936. dev->open = libertas_open;
  937. dev->hard_start_xmit = libertas_pre_start_xmit;
  938. dev->stop = libertas_close;
  939. dev->set_mac_address = libertas_set_mac_address;
  940. dev->tx_timeout = libertas_tx_timeout;
  941. dev->get_stats = libertas_get_stats;
  942. dev->watchdog_timeo = 5 * HZ;
  943. dev->ethtool_ops = &libertas_ethtool_ops;
  944. #ifdef WIRELESS_EXT
  945. dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  946. #endif
  947. #define NETIF_F_DYNALLOC 16
  948. dev->features |= NETIF_F_DYNALLOC;
  949. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  950. dev->set_multicast_list = libertas_set_multicast_list;
  951. SET_NETDEV_DEV(dev, dmdev);
  952. INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
  953. INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
  954. spin_lock_init(&priv->adapter->driver_lock);
  955. init_waitqueue_head(&priv->adapter->cmd_pending);
  956. priv->adapter->nr_cmd_pending = 0;
  957. goto done;
  958. err_kzalloc:
  959. free_netdev(dev);
  960. priv = NULL;
  961. done:
  962. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  963. return priv;
  964. }
  965. EXPORT_SYMBOL_GPL(libertas_add_card);
  966. int libertas_activate_card(wlan_private *priv)
  967. {
  968. struct net_device *dev = priv->dev;
  969. int ret = -1;
  970. lbs_deb_enter(LBS_DEB_MAIN);
  971. lbs_deb_thread("Starting main thread...\n");
  972. init_waitqueue_head(&priv->waitq);
  973. priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
  974. if (IS_ERR(priv->main_thread)) {
  975. lbs_deb_thread("Error creating main thread.\n");
  976. goto done;
  977. }
  978. priv->assoc_thread =
  979. create_singlethread_workqueue("libertas_assoc");
  980. INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
  981. INIT_WORK(&priv->sync_channel, libertas_sync_channel);
  982. /*
  983. * Register the device. Fillup the private data structure with
  984. * relevant information from the card and request for the required
  985. * IRQ.
  986. */
  987. if (priv->hw_register_dev(priv) < 0) {
  988. lbs_pr_err("failed to register WLAN device\n");
  989. goto err_registerdev;
  990. }
  991. /* init FW and HW */
  992. if (libertas_init_fw(priv)) {
  993. lbs_pr_err("firmware init failed\n");
  994. goto err_registerdev;
  995. }
  996. if (register_netdev(dev)) {
  997. lbs_pr_err("cannot register ethX device\n");
  998. goto err_init_fw;
  999. }
  1000. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  1001. libertas_debugfs_init_one(priv, dev);
  1002. ret = 0;
  1003. goto done;
  1004. err_init_fw:
  1005. priv->hw_unregister_dev(priv);
  1006. err_registerdev:
  1007. destroy_workqueue(priv->assoc_thread);
  1008. /* Stop the thread servicing the interrupts */
  1009. wake_up_interruptible(&priv->waitq);
  1010. kthread_stop(priv->main_thread);
  1011. done:
  1012. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  1013. return ret;
  1014. }
  1015. EXPORT_SYMBOL_GPL(libertas_activate_card);
  1016. /**
  1017. * @brief This function adds mshX interface
  1018. *
  1019. * @param priv A pointer to the wlan_private structure
  1020. * @return 0 if successful, -X otherwise
  1021. */
  1022. int libertas_add_mesh(wlan_private *priv, struct device *dev)
  1023. {
  1024. struct net_device *mesh_dev = NULL;
  1025. int ret = 0;
  1026. lbs_deb_enter(LBS_DEB_MESH);
  1027. /* Allocate a virtual mesh device */
  1028. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1029. lbs_deb_mesh("init mshX device failed\n");
  1030. ret = -ENOMEM;
  1031. goto done;
  1032. }
  1033. mesh_dev->priv = priv;
  1034. priv->mesh_dev = mesh_dev;
  1035. SET_MODULE_OWNER(mesh_dev);
  1036. mesh_dev->open = libertas_mesh_open;
  1037. mesh_dev->hard_start_xmit = libertas_mesh_pre_start_xmit;
  1038. mesh_dev->stop = libertas_mesh_close;
  1039. mesh_dev->get_stats = libertas_get_stats;
  1040. mesh_dev->set_mac_address = libertas_set_mac_address;
  1041. mesh_dev->ethtool_ops = &libertas_ethtool_ops;
  1042. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1043. sizeof(priv->dev->dev_addr));
  1044. SET_NETDEV_DEV(priv->mesh_dev, dev);
  1045. #ifdef WIRELESS_EXT
  1046. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1047. #endif
  1048. #define NETIF_F_DYNALLOC 16
  1049. /* Register virtual mesh interface */
  1050. ret = register_netdev(mesh_dev);
  1051. if (ret) {
  1052. lbs_pr_err("cannot register mshX virtual interface\n");
  1053. goto err_free;
  1054. }
  1055. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
  1056. if (ret)
  1057. goto err_unregister;
  1058. /* Everything successful */
  1059. ret = 0;
  1060. goto done;
  1061. err_unregister:
  1062. unregister_netdev(mesh_dev);
  1063. err_free:
  1064. free_netdev(mesh_dev);
  1065. done:
  1066. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1067. return ret;
  1068. }
  1069. EXPORT_SYMBOL_GPL(libertas_add_mesh);
  1070. static void wake_pending_cmdnodes(wlan_private *priv)
  1071. {
  1072. struct cmd_ctrl_node *cmdnode;
  1073. unsigned long flags;
  1074. lbs_deb_enter(LBS_DEB_HOST);
  1075. spin_lock_irqsave(&priv->adapter->driver_lock, flags);
  1076. list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
  1077. cmdnode->cmdwaitqwoken = 1;
  1078. wake_up_interruptible(&cmdnode->cmdwait_q);
  1079. }
  1080. spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
  1081. }
  1082. int libertas_remove_card(wlan_private *priv)
  1083. {
  1084. wlan_adapter *adapter;
  1085. struct net_device *dev;
  1086. union iwreq_data wrqu;
  1087. lbs_deb_enter(LBS_DEB_NET);
  1088. if (!priv)
  1089. goto out;
  1090. adapter = priv->adapter;
  1091. if (!adapter)
  1092. goto out;
  1093. dev = priv->dev;
  1094. netif_stop_queue(priv->dev);
  1095. netif_carrier_off(priv->dev);
  1096. wake_pending_cmdnodes(priv);
  1097. unregister_netdev(dev);
  1098. cancel_delayed_work(&priv->assoc_work);
  1099. destroy_workqueue(priv->assoc_thread);
  1100. if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
  1101. adapter->psmode = WLAN802_11POWERMODECAM;
  1102. libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  1103. }
  1104. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  1105. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1106. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1107. adapter->surpriseremoved = 1;
  1108. /* Stop the thread servicing the interrupts */
  1109. kthread_stop(priv->main_thread);
  1110. libertas_debugfs_remove_one(priv);
  1111. lbs_deb_net("free adapter\n");
  1112. libertas_free_adapter(priv);
  1113. lbs_deb_net("unregister finish\n");
  1114. priv->dev = NULL;
  1115. free_netdev(dev);
  1116. out:
  1117. lbs_deb_leave(LBS_DEB_NET);
  1118. return 0;
  1119. }
  1120. EXPORT_SYMBOL_GPL(libertas_remove_card);
  1121. void libertas_remove_mesh(wlan_private *priv)
  1122. {
  1123. struct net_device *mesh_dev;
  1124. lbs_deb_enter(LBS_DEB_NET);
  1125. if (!priv)
  1126. goto out;
  1127. mesh_dev = priv->mesh_dev;
  1128. netif_stop_queue(mesh_dev);
  1129. netif_carrier_off(priv->mesh_dev);
  1130. sysfs_remove_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
  1131. unregister_netdev(mesh_dev);
  1132. priv->mesh_dev = NULL ;
  1133. free_netdev(mesh_dev);
  1134. out:
  1135. lbs_deb_leave(LBS_DEB_NET);
  1136. }
  1137. EXPORT_SYMBOL_GPL(libertas_remove_mesh);
  1138. /**
  1139. * @brief This function finds the CFP in
  1140. * region_cfp_table based on region and band parameter.
  1141. *
  1142. * @param region The region code
  1143. * @param band The band
  1144. * @param cfp_no A pointer to CFP number
  1145. * @return A pointer to CFP
  1146. */
  1147. struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  1148. {
  1149. int i, end;
  1150. lbs_deb_enter(LBS_DEB_MAIN);
  1151. end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
  1152. for (i = 0; i < end ; i++) {
  1153. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1154. region_cfp_table[i].region);
  1155. if (region_cfp_table[i].region == region) {
  1156. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1157. lbs_deb_leave(LBS_DEB_MAIN);
  1158. return region_cfp_table[i].cfp_BG;
  1159. }
  1160. }
  1161. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1162. return NULL;
  1163. }
  1164. int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
  1165. {
  1166. wlan_adapter *adapter = priv->adapter;
  1167. int ret = 0;
  1168. int i = 0;
  1169. struct chan_freq_power *cfp;
  1170. int cfp_no;
  1171. lbs_deb_enter(LBS_DEB_MAIN);
  1172. memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
  1173. {
  1174. cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
  1175. if (cfp != NULL) {
  1176. adapter->region_channel[i].nrcfp = cfp_no;
  1177. adapter->region_channel[i].CFP = cfp;
  1178. } else {
  1179. lbs_deb_main("wrong region code %#x in band B/G\n",
  1180. region);
  1181. ret = -1;
  1182. goto out;
  1183. }
  1184. adapter->region_channel[i].valid = 1;
  1185. adapter->region_channel[i].region = region;
  1186. adapter->region_channel[i].band = band;
  1187. i++;
  1188. }
  1189. out:
  1190. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1191. return ret;
  1192. }
  1193. /**
  1194. * @brief This function handles the interrupt. it will change PS
  1195. * state if applicable. it will wake up main_thread to handle
  1196. * the interrupt event as well.
  1197. *
  1198. * @param dev A pointer to net_device structure
  1199. * @return n/a
  1200. */
  1201. void libertas_interrupt(struct net_device *dev)
  1202. {
  1203. wlan_private *priv = dev->priv;
  1204. lbs_deb_enter(LBS_DEB_THREAD);
  1205. lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
  1206. priv->adapter->intcounter);
  1207. priv->adapter->intcounter++;
  1208. if (priv->adapter->psstate == PS_STATE_SLEEP) {
  1209. priv->adapter->psstate = PS_STATE_AWAKE;
  1210. netif_wake_queue(dev);
  1211. if (priv->mesh_dev)
  1212. netif_wake_queue(priv->mesh_dev);
  1213. }
  1214. wake_up_interruptible(&priv->waitq);
  1215. lbs_deb_leave(LBS_DEB_THREAD);
  1216. }
  1217. EXPORT_SYMBOL_GPL(libertas_interrupt);
  1218. int libertas_reset_device(wlan_private *priv)
  1219. {
  1220. int ret;
  1221. lbs_deb_enter(LBS_DEB_MAIN);
  1222. ret = libertas_prepare_and_send_command(priv, CMD_802_11_RESET,
  1223. CMD_ACT_HALT, 0, 0, NULL);
  1224. msleep_interruptible(10);
  1225. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1226. return ret;
  1227. }
  1228. EXPORT_SYMBOL_GPL(libertas_reset_device);
  1229. static int libertas_init_module(void)
  1230. {
  1231. lbs_deb_enter(LBS_DEB_MAIN);
  1232. libertas_debugfs_init();
  1233. lbs_deb_leave(LBS_DEB_MAIN);
  1234. return 0;
  1235. }
  1236. static void libertas_exit_module(void)
  1237. {
  1238. lbs_deb_enter(LBS_DEB_MAIN);
  1239. libertas_debugfs_remove();
  1240. lbs_deb_leave(LBS_DEB_MAIN);
  1241. }
  1242. module_init(libertas_init_module);
  1243. module_exit(libertas_exit_module);
  1244. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1245. MODULE_AUTHOR("Marvell International Ltd.");
  1246. MODULE_LICENSE("GPL");