main.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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. /* ATIM params */
  835. adapter->atimwindow = 0;
  836. adapter->connect_status = LIBERTAS_DISCONNECTED;
  837. memset(adapter->current_addr, 0xff, ETH_ALEN);
  838. /* scan type */
  839. adapter->scantype = CMD_SCAN_TYPE_ACTIVE;
  840. /* scan mode */
  841. adapter->scanmode = CMD_BSS_TYPE_ANY;
  842. /* 802.11 specific */
  843. adapter->secinfo.wep_enabled = 0;
  844. for (i = 0; i < sizeof(adapter->wep_keys) / sizeof(adapter->wep_keys[0]);
  845. i++)
  846. memset(&adapter->wep_keys[i], 0, sizeof(struct enc_key));
  847. adapter->wep_tx_keyidx = 0;
  848. adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  849. adapter->mode = IW_MODE_INFRA;
  850. adapter->pending_assoc_req = NULL;
  851. adapter->in_progress_assoc_req = NULL;
  852. /* Initialize scan result lists */
  853. INIT_LIST_HEAD(&adapter->network_free_list);
  854. INIT_LIST_HEAD(&adapter->network_list);
  855. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  856. list_add_tail(&adapter->networks[i].list,
  857. &adapter->network_free_list);
  858. }
  859. mutex_init(&adapter->lock);
  860. memset(&adapter->curbssparams, 0, sizeof(adapter->curbssparams));
  861. adapter->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  862. /* PnP and power profile */
  863. adapter->surpriseremoved = 0;
  864. adapter->currentpacketfilter =
  865. CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  866. adapter->radioon = RADIO_ON;
  867. adapter->auto_rate = 1;
  868. adapter->cur_rate = 0;
  869. adapter->adhoc_grate_enabled = 0;
  870. adapter->beaconperiod = MRVDRV_BEACON_INTERVAL;
  871. // set default capabilities
  872. adapter->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  873. adapter->psmode = WLAN802_11POWERMODECAM;
  874. adapter->multipledtim = MRVDRV_DEFAULT_MULTIPLE_DTIM;
  875. adapter->listeninterval = MRVDRV_DEFAULT_LISTEN_INTERVAL;
  876. adapter->psstate = PS_STATE_FULL_POWER;
  877. adapter->needtowakeup = 0;
  878. adapter->locallisteninterval = 0; /* default value in firmware will be used */
  879. adapter->intcounter = 0;
  880. adapter->currenttxskb = NULL;
  881. memset(&adapter->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
  882. adapter->tx_queue_idx = 0;
  883. spin_lock_init(&adapter->txqueue_lock);
  884. return;
  885. }
  886. static int libertas_init_fw(wlan_private * priv)
  887. {
  888. int ret = -1;
  889. wlan_adapter *adapter = priv->adapter;
  890. lbs_deb_enter(LBS_DEB_FW);
  891. /* Allocate adapter structure */
  892. if ((ret = wlan_allocate_adapter(priv)) != 0)
  893. goto done;
  894. /* init adapter structure */
  895. wlan_init_adapter(priv);
  896. /* init timer etc. */
  897. setup_timer(&adapter->command_timer, command_timer_fn,
  898. (unsigned long)priv);
  899. /* download fimrware etc. */
  900. if ((ret = wlan_setup_station_hw(priv)) != 0) {
  901. del_timer_sync(&adapter->command_timer);
  902. goto done;
  903. }
  904. /* init 802.11d */
  905. libertas_init_11d(priv);
  906. ret = 0;
  907. done:
  908. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  909. return ret;
  910. }
  911. /**
  912. * @brief This function adds the card. it will probe the
  913. * card, allocate the wlan_priv and initialize the device.
  914. *
  915. * @param card A pointer to card
  916. * @return A pointer to wlan_private structure
  917. */
  918. wlan_private *libertas_add_card(void *card, struct device *dmdev)
  919. {
  920. struct net_device *dev = NULL;
  921. wlan_private *priv = NULL;
  922. lbs_deb_enter(LBS_DEB_NET);
  923. /* Allocate an Ethernet device and register it */
  924. if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
  925. lbs_pr_err("init ethX device failed\n");
  926. return NULL;
  927. }
  928. priv = dev->priv;
  929. /* allocate buffer for wlan_adapter */
  930. if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
  931. lbs_pr_err("allocate buffer for wlan_adapter failed\n");
  932. goto err_kzalloc;
  933. }
  934. priv->dev = dev;
  935. priv->card = card;
  936. priv->mesh_open = 0;
  937. priv->infra_open = 0;
  938. SET_MODULE_OWNER(dev);
  939. /* Setup the OS Interface to our functions */
  940. dev->open = libertas_open;
  941. dev->hard_start_xmit = libertas_pre_start_xmit;
  942. dev->stop = libertas_close;
  943. dev->set_mac_address = libertas_set_mac_address;
  944. dev->tx_timeout = libertas_tx_timeout;
  945. dev->get_stats = libertas_get_stats;
  946. dev->watchdog_timeo = 5 * HZ;
  947. dev->ethtool_ops = &libertas_ethtool_ops;
  948. #ifdef WIRELESS_EXT
  949. dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  950. #endif
  951. #define NETIF_F_DYNALLOC 16
  952. dev->features |= NETIF_F_DYNALLOC;
  953. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  954. dev->set_multicast_list = libertas_set_multicast_list;
  955. SET_NETDEV_DEV(dev, dmdev);
  956. INIT_LIST_HEAD(&priv->adapter->cmdfreeq);
  957. INIT_LIST_HEAD(&priv->adapter->cmdpendingq);
  958. spin_lock_init(&priv->adapter->driver_lock);
  959. init_waitqueue_head(&priv->adapter->cmd_pending);
  960. priv->adapter->nr_cmd_pending = 0;
  961. goto done;
  962. err_kzalloc:
  963. free_netdev(dev);
  964. priv = NULL;
  965. done:
  966. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  967. return priv;
  968. }
  969. EXPORT_SYMBOL_GPL(libertas_add_card);
  970. int libertas_activate_card(wlan_private *priv)
  971. {
  972. struct net_device *dev = priv->dev;
  973. int ret = -1;
  974. lbs_deb_enter(LBS_DEB_MAIN);
  975. lbs_deb_thread("Starting main thread...\n");
  976. init_waitqueue_head(&priv->waitq);
  977. priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
  978. if (IS_ERR(priv->main_thread)) {
  979. lbs_deb_thread("Error creating main thread.\n");
  980. goto done;
  981. }
  982. priv->assoc_thread =
  983. create_singlethread_workqueue("libertas_assoc");
  984. INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
  985. INIT_WORK(&priv->sync_channel, libertas_sync_channel);
  986. /*
  987. * Register the device. Fillup the private data structure with
  988. * relevant information from the card and request for the required
  989. * IRQ.
  990. */
  991. if (priv->hw_register_dev(priv) < 0) {
  992. lbs_pr_err("failed to register WLAN device\n");
  993. goto err_registerdev;
  994. }
  995. /* init FW and HW */
  996. if (libertas_init_fw(priv)) {
  997. lbs_pr_err("firmware init failed\n");
  998. goto err_registerdev;
  999. }
  1000. if (register_netdev(dev)) {
  1001. lbs_pr_err("cannot register ethX device\n");
  1002. goto err_init_fw;
  1003. }
  1004. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  1005. libertas_debugfs_init_one(priv, dev);
  1006. ret = 0;
  1007. goto done;
  1008. err_init_fw:
  1009. priv->hw_unregister_dev(priv);
  1010. err_registerdev:
  1011. destroy_workqueue(priv->assoc_thread);
  1012. /* Stop the thread servicing the interrupts */
  1013. wake_up_interruptible(&priv->waitq);
  1014. kthread_stop(priv->main_thread);
  1015. done:
  1016. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  1017. return ret;
  1018. }
  1019. EXPORT_SYMBOL_GPL(libertas_activate_card);
  1020. /**
  1021. * @brief This function adds mshX interface
  1022. *
  1023. * @param priv A pointer to the wlan_private structure
  1024. * @return 0 if successful, -X otherwise
  1025. */
  1026. int libertas_add_mesh(wlan_private *priv, struct device *dev)
  1027. {
  1028. struct net_device *mesh_dev = NULL;
  1029. int ret = 0;
  1030. lbs_deb_enter(LBS_DEB_MESH);
  1031. /* Allocate a virtual mesh device */
  1032. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1033. lbs_deb_mesh("init mshX device failed\n");
  1034. ret = -ENOMEM;
  1035. goto done;
  1036. }
  1037. mesh_dev->priv = priv;
  1038. priv->mesh_dev = mesh_dev;
  1039. SET_MODULE_OWNER(mesh_dev);
  1040. mesh_dev->open = libertas_mesh_open;
  1041. mesh_dev->hard_start_xmit = libertas_mesh_pre_start_xmit;
  1042. mesh_dev->stop = libertas_mesh_close;
  1043. mesh_dev->get_stats = libertas_get_stats;
  1044. mesh_dev->set_mac_address = libertas_set_mac_address;
  1045. mesh_dev->ethtool_ops = &libertas_ethtool_ops;
  1046. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1047. sizeof(priv->dev->dev_addr));
  1048. SET_NETDEV_DEV(priv->mesh_dev, dev);
  1049. #ifdef WIRELESS_EXT
  1050. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1051. #endif
  1052. #define NETIF_F_DYNALLOC 16
  1053. /* Register virtual mesh interface */
  1054. ret = register_netdev(mesh_dev);
  1055. if (ret) {
  1056. lbs_pr_err("cannot register mshX virtual interface\n");
  1057. goto err_free;
  1058. }
  1059. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
  1060. if (ret)
  1061. goto err_unregister;
  1062. /* Everything successful */
  1063. ret = 0;
  1064. goto done;
  1065. err_unregister:
  1066. unregister_netdev(mesh_dev);
  1067. err_free:
  1068. free_netdev(mesh_dev);
  1069. done:
  1070. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1071. return ret;
  1072. }
  1073. EXPORT_SYMBOL_GPL(libertas_add_mesh);
  1074. static void wake_pending_cmdnodes(wlan_private *priv)
  1075. {
  1076. struct cmd_ctrl_node *cmdnode;
  1077. unsigned long flags;
  1078. lbs_deb_enter(LBS_DEB_HOST);
  1079. spin_lock_irqsave(&priv->adapter->driver_lock, flags);
  1080. list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
  1081. cmdnode->cmdwaitqwoken = 1;
  1082. wake_up_interruptible(&cmdnode->cmdwait_q);
  1083. }
  1084. spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
  1085. }
  1086. int libertas_remove_card(wlan_private *priv)
  1087. {
  1088. wlan_adapter *adapter;
  1089. struct net_device *dev;
  1090. union iwreq_data wrqu;
  1091. lbs_deb_enter(LBS_DEB_NET);
  1092. if (!priv)
  1093. goto out;
  1094. adapter = priv->adapter;
  1095. if (!adapter)
  1096. goto out;
  1097. dev = priv->dev;
  1098. netif_stop_queue(priv->dev);
  1099. netif_carrier_off(priv->dev);
  1100. wake_pending_cmdnodes(priv);
  1101. unregister_netdev(dev);
  1102. cancel_delayed_work(&priv->assoc_work);
  1103. destroy_workqueue(priv->assoc_thread);
  1104. if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
  1105. adapter->psmode = WLAN802_11POWERMODECAM;
  1106. libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  1107. }
  1108. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  1109. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1110. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1111. adapter->surpriseremoved = 1;
  1112. /* Stop the thread servicing the interrupts */
  1113. kthread_stop(priv->main_thread);
  1114. libertas_debugfs_remove_one(priv);
  1115. lbs_deb_net("free adapter\n");
  1116. libertas_free_adapter(priv);
  1117. lbs_deb_net("unregister finish\n");
  1118. priv->dev = NULL;
  1119. free_netdev(dev);
  1120. out:
  1121. lbs_deb_leave(LBS_DEB_NET);
  1122. return 0;
  1123. }
  1124. EXPORT_SYMBOL_GPL(libertas_remove_card);
  1125. void libertas_remove_mesh(wlan_private *priv)
  1126. {
  1127. struct net_device *mesh_dev;
  1128. lbs_deb_enter(LBS_DEB_NET);
  1129. if (!priv)
  1130. goto out;
  1131. mesh_dev = priv->mesh_dev;
  1132. netif_stop_queue(mesh_dev);
  1133. netif_carrier_off(priv->mesh_dev);
  1134. sysfs_remove_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
  1135. unregister_netdev(mesh_dev);
  1136. priv->mesh_dev = NULL ;
  1137. free_netdev(mesh_dev);
  1138. out:
  1139. lbs_deb_leave(LBS_DEB_NET);
  1140. }
  1141. EXPORT_SYMBOL_GPL(libertas_remove_mesh);
  1142. /**
  1143. * @brief This function finds the CFP in
  1144. * region_cfp_table based on region and band parameter.
  1145. *
  1146. * @param region The region code
  1147. * @param band The band
  1148. * @param cfp_no A pointer to CFP number
  1149. * @return A pointer to CFP
  1150. */
  1151. struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  1152. {
  1153. int i, end;
  1154. lbs_deb_enter(LBS_DEB_MAIN);
  1155. end = sizeof(region_cfp_table)/sizeof(struct region_cfp_table);
  1156. for (i = 0; i < end ; i++) {
  1157. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1158. region_cfp_table[i].region);
  1159. if (region_cfp_table[i].region == region) {
  1160. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1161. lbs_deb_leave(LBS_DEB_MAIN);
  1162. return region_cfp_table[i].cfp_BG;
  1163. }
  1164. }
  1165. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1166. return NULL;
  1167. }
  1168. int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
  1169. {
  1170. wlan_adapter *adapter = priv->adapter;
  1171. int ret = 0;
  1172. int i = 0;
  1173. struct chan_freq_power *cfp;
  1174. int cfp_no;
  1175. lbs_deb_enter(LBS_DEB_MAIN);
  1176. memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
  1177. {
  1178. cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
  1179. if (cfp != NULL) {
  1180. adapter->region_channel[i].nrcfp = cfp_no;
  1181. adapter->region_channel[i].CFP = cfp;
  1182. } else {
  1183. lbs_deb_main("wrong region code %#x in band B/G\n",
  1184. region);
  1185. ret = -1;
  1186. goto out;
  1187. }
  1188. adapter->region_channel[i].valid = 1;
  1189. adapter->region_channel[i].region = region;
  1190. adapter->region_channel[i].band = band;
  1191. i++;
  1192. }
  1193. out:
  1194. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1195. return ret;
  1196. }
  1197. /**
  1198. * @brief This function handles the interrupt. it will change PS
  1199. * state if applicable. it will wake up main_thread to handle
  1200. * the interrupt event as well.
  1201. *
  1202. * @param dev A pointer to net_device structure
  1203. * @return n/a
  1204. */
  1205. void libertas_interrupt(struct net_device *dev)
  1206. {
  1207. wlan_private *priv = dev->priv;
  1208. lbs_deb_enter(LBS_DEB_THREAD);
  1209. lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
  1210. priv->adapter->intcounter);
  1211. priv->adapter->intcounter++;
  1212. if (priv->adapter->psstate == PS_STATE_SLEEP) {
  1213. priv->adapter->psstate = PS_STATE_AWAKE;
  1214. netif_wake_queue(dev);
  1215. if (priv->mesh_dev)
  1216. netif_wake_queue(priv->mesh_dev);
  1217. }
  1218. wake_up_interruptible(&priv->waitq);
  1219. lbs_deb_leave(LBS_DEB_THREAD);
  1220. }
  1221. EXPORT_SYMBOL_GPL(libertas_interrupt);
  1222. int libertas_reset_device(wlan_private *priv)
  1223. {
  1224. int ret;
  1225. lbs_deb_enter(LBS_DEB_MAIN);
  1226. ret = libertas_prepare_and_send_command(priv, CMD_802_11_RESET,
  1227. CMD_ACT_HALT, 0, 0, NULL);
  1228. msleep_interruptible(10);
  1229. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1230. return ret;
  1231. }
  1232. EXPORT_SYMBOL_GPL(libertas_reset_device);
  1233. static int libertas_init_module(void)
  1234. {
  1235. lbs_deb_enter(LBS_DEB_MAIN);
  1236. libertas_debugfs_init();
  1237. lbs_deb_leave(LBS_DEB_MAIN);
  1238. return 0;
  1239. }
  1240. static void libertas_exit_module(void)
  1241. {
  1242. lbs_deb_enter(LBS_DEB_MAIN);
  1243. libertas_debugfs_remove();
  1244. lbs_deb_leave(LBS_DEB_MAIN);
  1245. }
  1246. module_init(libertas_init_module);
  1247. module_exit(libertas_exit_module);
  1248. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1249. MODULE_AUTHOR("Marvell International Ltd.");
  1250. MODULE_LICENSE("GPL");