main.c 39 KB

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