main.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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. lbs_deb_enter(LBS_DEB_NET);
  577. oldpacketfilter = adapter->currentpacketfilter;
  578. if (dev->flags & IFF_PROMISC) {
  579. lbs_deb_net("enable promiscuous mode\n");
  580. adapter->currentpacketfilter |=
  581. CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  582. adapter->currentpacketfilter &=
  583. ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  584. CMD_ACT_MAC_MULTICAST_ENABLE);
  585. } else {
  586. /* Multicast */
  587. adapter->currentpacketfilter &=
  588. ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  589. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  590. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  591. lbs_deb_net( "enabling all multicast\n");
  592. adapter->currentpacketfilter |=
  593. CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  594. adapter->currentpacketfilter &=
  595. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  596. } else {
  597. adapter->currentpacketfilter &=
  598. ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  599. if (!dev->mc_count) {
  600. lbs_deb_net("no multicast addresses, "
  601. "disabling multicast\n");
  602. adapter->currentpacketfilter &=
  603. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  604. } else {
  605. int i;
  606. adapter->currentpacketfilter |=
  607. CMD_ACT_MAC_MULTICAST_ENABLE;
  608. adapter->nr_of_multicastmacaddr =
  609. libertas_copy_multicast_address(adapter, dev);
  610. lbs_deb_net("multicast addresses: %d\n",
  611. dev->mc_count);
  612. for (i = 0; i < dev->mc_count; i++) {
  613. lbs_deb_net("Multicast address %d:"
  614. MAC_FMT "\n", i,
  615. adapter->multicastlist[i][0],
  616. adapter->multicastlist[i][1],
  617. adapter->multicastlist[i][2],
  618. adapter->multicastlist[i][3],
  619. adapter->multicastlist[i][4],
  620. adapter->multicastlist[i][5]);
  621. }
  622. /* send multicast addresses to firmware */
  623. libertas_prepare_and_send_command(priv,
  624. CMD_MAC_MULTICAST_ADR,
  625. CMD_ACT_SET, 0, 0,
  626. NULL);
  627. }
  628. }
  629. }
  630. if (adapter->currentpacketfilter != oldpacketfilter) {
  631. libertas_set_mac_packet_filter(priv);
  632. }
  633. lbs_deb_leave(LBS_DEB_NET);
  634. }
  635. /**
  636. * @brief This function handles the major jobs in the WLAN driver.
  637. * It handles all events generated by firmware, RX data received
  638. * from firmware and TX data sent from kernel.
  639. *
  640. * @param data A pointer to wlan_thread structure
  641. * @return 0
  642. */
  643. static int libertas_thread(void *data)
  644. {
  645. struct net_device *dev = data;
  646. wlan_private *priv = dev->priv;
  647. wlan_adapter *adapter = priv->adapter;
  648. wait_queue_t wait;
  649. u8 ireg = 0;
  650. lbs_deb_enter(LBS_DEB_THREAD);
  651. init_waitqueue_entry(&wait, current);
  652. set_freezable();
  653. for (;;) {
  654. lbs_deb_thread( "main-thread 111: intcounter=%d "
  655. "currenttxskb=%p dnld_sent=%d\n",
  656. adapter->intcounter,
  657. adapter->currenttxskb, priv->dnld_sent);
  658. add_wait_queue(&priv->waitq, &wait);
  659. set_current_state(TASK_INTERRUPTIBLE);
  660. spin_lock_irq(&adapter->driver_lock);
  661. if ((adapter->psstate == PS_STATE_SLEEP) ||
  662. (!adapter->intcounter
  663. && (priv->dnld_sent || adapter->cur_cmd ||
  664. list_empty(&adapter->cmdpendingq)))) {
  665. lbs_deb_thread(
  666. "main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  667. adapter->connect_status, adapter->intcounter,
  668. adapter->psmode, adapter->psstate);
  669. spin_unlock_irq(&adapter->driver_lock);
  670. schedule();
  671. } else
  672. spin_unlock_irq(&adapter->driver_lock);
  673. lbs_deb_thread(
  674. "main-thread 222 (waking up): intcounter=%d currenttxskb=%p "
  675. "dnld_sent=%d\n", adapter->intcounter,
  676. adapter->currenttxskb, priv->dnld_sent);
  677. set_current_state(TASK_RUNNING);
  678. remove_wait_queue(&priv->waitq, &wait);
  679. try_to_freeze();
  680. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p "
  681. "dnld_sent=%d\n",
  682. adapter->intcounter,
  683. adapter->currenttxskb, priv->dnld_sent);
  684. if (kthread_should_stop()
  685. || adapter->surpriseremoved) {
  686. lbs_deb_thread(
  687. "main-thread: break from main thread: surpriseremoved=0x%x\n",
  688. adapter->surpriseremoved);
  689. break;
  690. }
  691. spin_lock_irq(&adapter->driver_lock);
  692. if (adapter->intcounter) {
  693. u8 int_status;
  694. adapter->intcounter = 0;
  695. int_status = priv->hw_get_int_status(priv, &ireg);
  696. if (int_status) {
  697. lbs_deb_thread(
  698. "main-thread: reading HOST_INT_STATUS_REG failed\n");
  699. spin_unlock_irq(&adapter->driver_lock);
  700. continue;
  701. }
  702. adapter->hisregcpy |= ireg;
  703. }
  704. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p "
  705. "dnld_sent=%d\n",
  706. adapter->intcounter,
  707. adapter->currenttxskb, priv->dnld_sent);
  708. /* command response? */
  709. if (adapter->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
  710. lbs_deb_thread("main-thread: cmd response ready\n");
  711. adapter->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
  712. spin_unlock_irq(&adapter->driver_lock);
  713. libertas_process_rx_command(priv);
  714. spin_lock_irq(&adapter->driver_lock);
  715. }
  716. /* Any Card Event */
  717. if (adapter->hisregcpy & MRVDRV_CARDEVENT) {
  718. lbs_deb_thread("main-thread: Card Event Activity\n");
  719. adapter->hisregcpy &= ~MRVDRV_CARDEVENT;
  720. if (priv->hw_read_event_cause(priv)) {
  721. lbs_pr_alert(
  722. "main-thread: hw_read_event_cause failed\n");
  723. spin_unlock_irq(&adapter->driver_lock);
  724. continue;
  725. }
  726. spin_unlock_irq(&adapter->driver_lock);
  727. libertas_process_event(priv);
  728. } else
  729. spin_unlock_irq(&adapter->driver_lock);
  730. /* Check if we need to confirm Sleep Request received previously */
  731. if (adapter->psstate == PS_STATE_PRE_SLEEP) {
  732. if (!priv->dnld_sent && !adapter->cur_cmd) {
  733. if (adapter->connect_status ==
  734. LIBERTAS_CONNECTED) {
  735. lbs_deb_thread(
  736. "main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p "
  737. "dnld_sent=%d cur_cmd=%p, confirm now\n",
  738. adapter->intcounter,
  739. adapter->currenttxskb,
  740. priv->dnld_sent,
  741. adapter->cur_cmd);
  742. libertas_ps_confirm_sleep(priv,
  743. (u16) adapter->psmode);
  744. } else {
  745. /* workaround for firmware sending
  746. * deauth/linkloss event immediately
  747. * after sleep request, remove this
  748. * after firmware fixes it
  749. */
  750. adapter->psstate = PS_STATE_AWAKE;
  751. lbs_pr_alert(
  752. "main-thread: ignore PS_SleepConfirm in non-connected state\n");
  753. }
  754. }
  755. }
  756. /* The PS state is changed during processing of Sleep Request
  757. * event above
  758. */
  759. if ((priv->adapter->psstate == PS_STATE_SLEEP) ||
  760. (priv->adapter->psstate == PS_STATE_PRE_SLEEP))
  761. continue;
  762. /* Execute the next command */
  763. if (!priv->dnld_sent && !priv->adapter->cur_cmd)
  764. libertas_execute_next_command(priv);
  765. /* Wake-up command waiters which can't sleep in
  766. * libertas_prepare_and_send_command
  767. */
  768. if (!adapter->nr_cmd_pending)
  769. wake_up_all(&adapter->cmd_pending);
  770. libertas_tx_runqueue(priv);
  771. }
  772. del_timer(&adapter->command_timer);
  773. adapter->nr_cmd_pending = 0;
  774. wake_up_all(&adapter->cmd_pending);
  775. lbs_deb_leave(LBS_DEB_THREAD);
  776. return 0;
  777. }
  778. /**
  779. * @brief This function downloads firmware image, gets
  780. * HW spec from firmware and set basic parameters to
  781. * firmware.
  782. *
  783. * @param priv A pointer to wlan_private structure
  784. * @return 0 or -1
  785. */
  786. static int wlan_setup_firmware(wlan_private * priv)
  787. {
  788. int ret = -1;
  789. wlan_adapter *adapter = priv->adapter;
  790. struct cmd_ds_mesh_access mesh_access;
  791. lbs_deb_enter(LBS_DEB_FW);
  792. /*
  793. * Read MAC address from HW
  794. */
  795. memset(adapter->current_addr, 0xff, ETH_ALEN);
  796. ret = libertas_prepare_and_send_command(priv, CMD_GET_HW_SPEC,
  797. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  798. if (ret) {
  799. ret = -1;
  800. goto done;
  801. }
  802. libertas_set_mac_packet_filter(priv);
  803. /* Get the supported Data rates */
  804. ret = libertas_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
  805. CMD_ACT_GET_TX_RATE,
  806. CMD_OPTION_WAITFORRSP, 0, NULL);
  807. if (ret) {
  808. ret = -1;
  809. goto done;
  810. }
  811. /* Disable mesh autostart */
  812. if (priv->mesh_dev) {
  813. memset(&mesh_access, 0, sizeof(mesh_access));
  814. mesh_access.data[0] = cpu_to_le32(0);
  815. ret = libertas_prepare_and_send_command(priv,
  816. CMD_MESH_ACCESS,
  817. CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
  818. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  819. if (ret) {
  820. ret = -1;
  821. goto done;
  822. }
  823. priv->mesh_autostart_enabled = 0;
  824. }
  825. /* Set the boot2 version in firmware */
  826. ret = libertas_prepare_and_send_command(priv, CMD_SET_BOOT2_VER,
  827. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  828. ret = 0;
  829. done:
  830. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  831. return ret;
  832. }
  833. /**
  834. * This function handles the timeout of command sending.
  835. * It will re-send the same command again.
  836. */
  837. static void command_timer_fn(unsigned long data)
  838. {
  839. wlan_private *priv = (wlan_private *)data;
  840. wlan_adapter *adapter = priv->adapter;
  841. struct cmd_ctrl_node *ptempnode;
  842. struct cmd_ds_command *cmd;
  843. unsigned long flags;
  844. ptempnode = adapter->cur_cmd;
  845. if (ptempnode == NULL) {
  846. lbs_deb_fw("ptempnode empty\n");
  847. return;
  848. }
  849. cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
  850. if (!cmd) {
  851. lbs_deb_fw("cmd is NULL\n");
  852. return;
  853. }
  854. lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
  855. if (!adapter->fw_ready)
  856. return;
  857. spin_lock_irqsave(&adapter->driver_lock, flags);
  858. adapter->cur_cmd = NULL;
  859. spin_unlock_irqrestore(&adapter->driver_lock, flags);
  860. lbs_deb_fw("re-sending same command because of timeout\n");
  861. libertas_queue_cmd(adapter, ptempnode, 0);
  862. wake_up_interruptible(&priv->waitq);
  863. return;
  864. }
  865. static int libertas_init_adapter(wlan_private * priv)
  866. {
  867. wlan_adapter *adapter = priv->adapter;
  868. size_t bufsize;
  869. int i, ret = 0;
  870. /* Allocate buffer to store the BSSID list */
  871. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  872. adapter->networks = kzalloc(bufsize, GFP_KERNEL);
  873. if (!adapter->networks) {
  874. lbs_pr_err("Out of memory allocating beacons\n");
  875. ret = -1;
  876. goto out;
  877. }
  878. /* Initialize scan result lists */
  879. INIT_LIST_HEAD(&adapter->network_free_list);
  880. INIT_LIST_HEAD(&adapter->network_list);
  881. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  882. list_add_tail(&adapter->networks[i].list,
  883. &adapter->network_free_list);
  884. }
  885. adapter->libertas_ps_confirm_sleep.seqnum = cpu_to_le16(++adapter->seqnum);
  886. adapter->libertas_ps_confirm_sleep.command =
  887. cpu_to_le16(CMD_802_11_PS_MODE);
  888. adapter->libertas_ps_confirm_sleep.size =
  889. cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
  890. adapter->libertas_ps_confirm_sleep.action =
  891. cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  892. memset(adapter->current_addr, 0xff, ETH_ALEN);
  893. adapter->connect_status = LIBERTAS_DISCONNECTED;
  894. adapter->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  895. adapter->mode = IW_MODE_INFRA;
  896. adapter->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  897. adapter->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  898. adapter->radioon = RADIO_ON;
  899. adapter->auto_rate = 1;
  900. adapter->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  901. adapter->psmode = WLAN802_11POWERMODECAM;
  902. adapter->psstate = PS_STATE_FULL_POWER;
  903. mutex_init(&adapter->lock);
  904. memset(&adapter->tx_queue_ps, 0, NR_TX_QUEUE*sizeof(struct sk_buff*));
  905. adapter->tx_queue_idx = 0;
  906. spin_lock_init(&adapter->txqueue_lock);
  907. setup_timer(&adapter->command_timer, command_timer_fn,
  908. (unsigned long)priv);
  909. INIT_LIST_HEAD(&adapter->cmdfreeq);
  910. INIT_LIST_HEAD(&adapter->cmdpendingq);
  911. spin_lock_init(&adapter->driver_lock);
  912. init_waitqueue_head(&adapter->cmd_pending);
  913. adapter->nr_cmd_pending = 0;
  914. /* Allocate the command buffers */
  915. if (libertas_allocate_cmd_buffer(priv)) {
  916. lbs_pr_err("Out of memory allocating command buffers\n");
  917. ret = -1;
  918. }
  919. out:
  920. return ret;
  921. }
  922. static void libertas_free_adapter(wlan_private * priv)
  923. {
  924. wlan_adapter *adapter = priv->adapter;
  925. if (!adapter) {
  926. lbs_deb_fw("why double free adapter?\n");
  927. return;
  928. }
  929. lbs_deb_fw("free command buffer\n");
  930. libertas_free_cmd_buffer(priv);
  931. lbs_deb_fw("free command_timer\n");
  932. del_timer(&adapter->command_timer);
  933. lbs_deb_fw("free scan results table\n");
  934. kfree(adapter->networks);
  935. adapter->networks = NULL;
  936. /* Free the adapter object itself */
  937. lbs_deb_fw("free adapter\n");
  938. kfree(adapter);
  939. priv->adapter = NULL;
  940. }
  941. /**
  942. * @brief This function adds the card. it will probe the
  943. * card, allocate the wlan_priv and initialize the device.
  944. *
  945. * @param card A pointer to card
  946. * @return A pointer to wlan_private structure
  947. */
  948. wlan_private *libertas_add_card(void *card, struct device *dmdev)
  949. {
  950. struct net_device *dev = NULL;
  951. wlan_private *priv = NULL;
  952. lbs_deb_enter(LBS_DEB_NET);
  953. /* Allocate an Ethernet device and register it */
  954. if (!(dev = alloc_etherdev(sizeof(wlan_private)))) {
  955. lbs_pr_err("init ethX device failed\n");
  956. goto done;
  957. }
  958. priv = dev->priv;
  959. /* allocate buffer for wlan_adapter */
  960. if (!(priv->adapter = kzalloc(sizeof(wlan_adapter), GFP_KERNEL))) {
  961. lbs_pr_err("allocate buffer for wlan_adapter failed\n");
  962. goto err_kzalloc;
  963. }
  964. if (libertas_init_adapter(priv)) {
  965. lbs_pr_err("failed to initialize adapter structure.\n");
  966. goto err_init_adapter;
  967. }
  968. priv->dev = dev;
  969. priv->card = card;
  970. priv->mesh_open = 0;
  971. priv->infra_open = 0;
  972. priv->hotplug_device = dmdev;
  973. /* Setup the OS Interface to our functions */
  974. dev->open = libertas_open;
  975. dev->hard_start_xmit = libertas_pre_start_xmit;
  976. dev->stop = libertas_close;
  977. dev->set_mac_address = libertas_set_mac_address;
  978. dev->tx_timeout = libertas_tx_timeout;
  979. dev->get_stats = libertas_get_stats;
  980. dev->watchdog_timeo = 5 * HZ;
  981. dev->ethtool_ops = &libertas_ethtool_ops;
  982. #ifdef WIRELESS_EXT
  983. dev->wireless_handlers = (struct iw_handler_def *)&libertas_handler_def;
  984. #endif
  985. #define NETIF_F_DYNALLOC 16
  986. dev->features |= NETIF_F_DYNALLOC;
  987. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  988. dev->set_multicast_list = libertas_set_multicast_list;
  989. SET_NETDEV_DEV(dev, dmdev);
  990. priv->rtap_net_dev = NULL;
  991. if (device_create_file(dmdev, &dev_attr_libertas_rtap))
  992. goto err_init_adapter;
  993. lbs_deb_thread("Starting main thread...\n");
  994. init_waitqueue_head(&priv->waitq);
  995. priv->main_thread = kthread_run(libertas_thread, dev, "libertas_main");
  996. if (IS_ERR(priv->main_thread)) {
  997. lbs_deb_thread("Error creating main thread.\n");
  998. goto err_kthread_run;
  999. }
  1000. priv->work_thread = create_singlethread_workqueue("libertas_worker");
  1001. INIT_DELAYED_WORK(&priv->assoc_work, libertas_association_worker);
  1002. INIT_DELAYED_WORK(&priv->scan_work, libertas_scan_worker);
  1003. INIT_WORK(&priv->sync_channel, libertas_sync_channel);
  1004. goto done;
  1005. err_kthread_run:
  1006. device_remove_file(dmdev, &dev_attr_libertas_rtap);
  1007. err_init_adapter:
  1008. libertas_free_adapter(priv);
  1009. err_kzalloc:
  1010. free_netdev(dev);
  1011. priv = NULL;
  1012. done:
  1013. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  1014. return priv;
  1015. }
  1016. EXPORT_SYMBOL_GPL(libertas_add_card);
  1017. int libertas_remove_card(wlan_private *priv)
  1018. {
  1019. wlan_adapter *adapter = priv->adapter;
  1020. struct net_device *dev = priv->dev;
  1021. union iwreq_data wrqu;
  1022. lbs_deb_enter(LBS_DEB_MAIN);
  1023. libertas_remove_rtap(priv);
  1024. dev = priv->dev;
  1025. device_remove_file(priv->hotplug_device, &dev_attr_libertas_rtap);
  1026. cancel_delayed_work(&priv->scan_work);
  1027. cancel_delayed_work(&priv->assoc_work);
  1028. destroy_workqueue(priv->work_thread);
  1029. if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
  1030. adapter->psmode = WLAN802_11POWERMODECAM;
  1031. libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  1032. }
  1033. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  1034. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1035. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1036. /* Stop the thread servicing the interrupts */
  1037. adapter->surpriseremoved = 1;
  1038. kthread_stop(priv->main_thread);
  1039. libertas_free_adapter(priv);
  1040. priv->dev = NULL;
  1041. free_netdev(dev);
  1042. lbs_deb_leave(LBS_DEB_MAIN);
  1043. return 0;
  1044. }
  1045. EXPORT_SYMBOL_GPL(libertas_remove_card);
  1046. int libertas_start_card(wlan_private *priv)
  1047. {
  1048. struct net_device *dev = priv->dev;
  1049. int ret = -1;
  1050. lbs_deb_enter(LBS_DEB_MAIN);
  1051. /* poke the firmware */
  1052. ret = wlan_setup_firmware(priv);
  1053. if (ret)
  1054. goto done;
  1055. /* init 802.11d */
  1056. libertas_init_11d(priv);
  1057. if (register_netdev(dev)) {
  1058. lbs_pr_err("cannot register ethX device\n");
  1059. goto done;
  1060. }
  1061. libertas_debugfs_init_one(priv, dev);
  1062. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  1063. ret = 0;
  1064. done:
  1065. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1066. return ret;
  1067. }
  1068. EXPORT_SYMBOL_GPL(libertas_start_card);
  1069. int libertas_stop_card(wlan_private *priv)
  1070. {
  1071. struct net_device *dev = priv->dev;
  1072. int ret = -1;
  1073. struct cmd_ctrl_node *cmdnode;
  1074. unsigned long flags;
  1075. lbs_deb_enter(LBS_DEB_MAIN);
  1076. netif_stop_queue(priv->dev);
  1077. netif_carrier_off(priv->dev);
  1078. libertas_debugfs_remove_one(priv);
  1079. /* Flush pending command nodes */
  1080. spin_lock_irqsave(&priv->adapter->driver_lock, flags);
  1081. list_for_each_entry(cmdnode, &priv->adapter->cmdpendingq, list) {
  1082. cmdnode->cmdwaitqwoken = 1;
  1083. wake_up_interruptible(&cmdnode->cmdwait_q);
  1084. }
  1085. spin_unlock_irqrestore(&priv->adapter->driver_lock, flags);
  1086. unregister_netdev(dev);
  1087. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1088. return ret;
  1089. }
  1090. EXPORT_SYMBOL_GPL(libertas_stop_card);
  1091. /**
  1092. * @brief This function adds mshX interface
  1093. *
  1094. * @param priv A pointer to the wlan_private structure
  1095. * @return 0 if successful, -X otherwise
  1096. */
  1097. int libertas_add_mesh(wlan_private *priv, struct device *dev)
  1098. {
  1099. struct net_device *mesh_dev = NULL;
  1100. int ret = 0;
  1101. lbs_deb_enter(LBS_DEB_MESH);
  1102. /* Allocate a virtual mesh device */
  1103. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1104. lbs_deb_mesh("init mshX device failed\n");
  1105. ret = -ENOMEM;
  1106. goto done;
  1107. }
  1108. mesh_dev->priv = priv;
  1109. priv->mesh_dev = mesh_dev;
  1110. mesh_dev->open = libertas_mesh_open;
  1111. mesh_dev->hard_start_xmit = libertas_mesh_pre_start_xmit;
  1112. mesh_dev->stop = libertas_mesh_close;
  1113. mesh_dev->get_stats = libertas_get_stats;
  1114. mesh_dev->set_mac_address = libertas_set_mac_address;
  1115. mesh_dev->ethtool_ops = &libertas_ethtool_ops;
  1116. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1117. sizeof(priv->dev->dev_addr));
  1118. SET_NETDEV_DEV(priv->mesh_dev, dev);
  1119. #ifdef WIRELESS_EXT
  1120. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1121. #endif
  1122. #define NETIF_F_DYNALLOC 16
  1123. /* Register virtual mesh interface */
  1124. ret = register_netdev(mesh_dev);
  1125. if (ret) {
  1126. lbs_pr_err("cannot register mshX virtual interface\n");
  1127. goto err_free;
  1128. }
  1129. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
  1130. if (ret)
  1131. goto err_unregister;
  1132. /* Everything successful */
  1133. ret = 0;
  1134. goto done;
  1135. err_unregister:
  1136. unregister_netdev(mesh_dev);
  1137. err_free:
  1138. free_netdev(mesh_dev);
  1139. done:
  1140. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1141. return ret;
  1142. }
  1143. EXPORT_SYMBOL_GPL(libertas_add_mesh);
  1144. void libertas_remove_mesh(wlan_private *priv)
  1145. {
  1146. struct net_device *mesh_dev;
  1147. lbs_deb_enter(LBS_DEB_MAIN);
  1148. if (!priv)
  1149. goto out;
  1150. mesh_dev = priv->mesh_dev;
  1151. netif_stop_queue(mesh_dev);
  1152. netif_carrier_off(priv->mesh_dev);
  1153. sysfs_remove_group(&(mesh_dev->dev.kobj), &libertas_mesh_attr_group);
  1154. unregister_netdev(mesh_dev);
  1155. priv->mesh_dev = NULL ;
  1156. free_netdev(mesh_dev);
  1157. out:
  1158. lbs_deb_leave(LBS_DEB_MAIN);
  1159. }
  1160. EXPORT_SYMBOL_GPL(libertas_remove_mesh);
  1161. /**
  1162. * @brief This function finds the CFP in
  1163. * region_cfp_table based on region and band parameter.
  1164. *
  1165. * @param region The region code
  1166. * @param band The band
  1167. * @param cfp_no A pointer to CFP number
  1168. * @return A pointer to CFP
  1169. */
  1170. struct chan_freq_power *libertas_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  1171. {
  1172. int i, end;
  1173. lbs_deb_enter(LBS_DEB_MAIN);
  1174. end = ARRAY_SIZE(region_cfp_table);
  1175. for (i = 0; i < end ; i++) {
  1176. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1177. region_cfp_table[i].region);
  1178. if (region_cfp_table[i].region == region) {
  1179. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1180. lbs_deb_leave(LBS_DEB_MAIN);
  1181. return region_cfp_table[i].cfp_BG;
  1182. }
  1183. }
  1184. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1185. return NULL;
  1186. }
  1187. int libertas_set_regiontable(wlan_private * priv, u8 region, u8 band)
  1188. {
  1189. wlan_adapter *adapter = priv->adapter;
  1190. int ret = 0;
  1191. int i = 0;
  1192. struct chan_freq_power *cfp;
  1193. int cfp_no;
  1194. lbs_deb_enter(LBS_DEB_MAIN);
  1195. memset(adapter->region_channel, 0, sizeof(adapter->region_channel));
  1196. {
  1197. cfp = libertas_get_region_cfp_table(region, band, &cfp_no);
  1198. if (cfp != NULL) {
  1199. adapter->region_channel[i].nrcfp = cfp_no;
  1200. adapter->region_channel[i].CFP = cfp;
  1201. } else {
  1202. lbs_deb_main("wrong region code %#x in band B/G\n",
  1203. region);
  1204. ret = -1;
  1205. goto out;
  1206. }
  1207. adapter->region_channel[i].valid = 1;
  1208. adapter->region_channel[i].region = region;
  1209. adapter->region_channel[i].band = band;
  1210. i++;
  1211. }
  1212. out:
  1213. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1214. return ret;
  1215. }
  1216. /**
  1217. * @brief This function handles the interrupt. it will change PS
  1218. * state if applicable. it will wake up main_thread to handle
  1219. * the interrupt event as well.
  1220. *
  1221. * @param dev A pointer to net_device structure
  1222. * @return n/a
  1223. */
  1224. void libertas_interrupt(struct net_device *dev)
  1225. {
  1226. wlan_private *priv = dev->priv;
  1227. lbs_deb_enter(LBS_DEB_THREAD);
  1228. lbs_deb_thread("libertas_interrupt: intcounter=%d\n",
  1229. priv->adapter->intcounter);
  1230. priv->adapter->intcounter++;
  1231. if (priv->adapter->psstate == PS_STATE_SLEEP) {
  1232. priv->adapter->psstate = PS_STATE_AWAKE;
  1233. netif_wake_queue(dev);
  1234. if (priv->mesh_dev)
  1235. netif_wake_queue(priv->mesh_dev);
  1236. }
  1237. wake_up_interruptible(&priv->waitq);
  1238. lbs_deb_leave(LBS_DEB_THREAD);
  1239. }
  1240. EXPORT_SYMBOL_GPL(libertas_interrupt);
  1241. int libertas_reset_device(wlan_private *priv)
  1242. {
  1243. int ret;
  1244. lbs_deb_enter(LBS_DEB_MAIN);
  1245. ret = libertas_prepare_and_send_command(priv, CMD_802_11_RESET,
  1246. CMD_ACT_HALT, 0, 0, NULL);
  1247. msleep_interruptible(10);
  1248. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1249. return ret;
  1250. }
  1251. EXPORT_SYMBOL_GPL(libertas_reset_device);
  1252. static int libertas_init_module(void)
  1253. {
  1254. lbs_deb_enter(LBS_DEB_MAIN);
  1255. libertas_debugfs_init();
  1256. lbs_deb_leave(LBS_DEB_MAIN);
  1257. return 0;
  1258. }
  1259. static void libertas_exit_module(void)
  1260. {
  1261. lbs_deb_enter(LBS_DEB_MAIN);
  1262. libertas_debugfs_remove();
  1263. lbs_deb_leave(LBS_DEB_MAIN);
  1264. }
  1265. /*
  1266. * rtap interface support fuctions
  1267. */
  1268. static int libertas_rtap_open(struct net_device *dev)
  1269. {
  1270. netif_carrier_off(dev);
  1271. netif_stop_queue(dev);
  1272. return 0;
  1273. }
  1274. static int libertas_rtap_stop(struct net_device *dev)
  1275. {
  1276. return 0;
  1277. }
  1278. static int libertas_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1279. {
  1280. netif_stop_queue(dev);
  1281. return -EOPNOTSUPP;
  1282. }
  1283. static struct net_device_stats *libertas_rtap_get_stats(struct net_device *dev)
  1284. {
  1285. wlan_private *priv = dev->priv;
  1286. return &priv->ieee->stats;
  1287. }
  1288. void libertas_remove_rtap(wlan_private *priv)
  1289. {
  1290. if (priv->rtap_net_dev == NULL)
  1291. return;
  1292. unregister_netdev(priv->rtap_net_dev);
  1293. free_ieee80211(priv->rtap_net_dev);
  1294. priv->rtap_net_dev = NULL;
  1295. }
  1296. int libertas_add_rtap(wlan_private *priv)
  1297. {
  1298. int rc = 0;
  1299. if (priv->rtap_net_dev)
  1300. return -EPERM;
  1301. priv->rtap_net_dev = alloc_ieee80211(0);
  1302. if (priv->rtap_net_dev == NULL)
  1303. return -ENOMEM;
  1304. priv->ieee = netdev_priv(priv->rtap_net_dev);
  1305. strcpy(priv->rtap_net_dev->name, "rtap%d");
  1306. priv->rtap_net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1307. priv->rtap_net_dev->open = libertas_rtap_open;
  1308. priv->rtap_net_dev->stop = libertas_rtap_stop;
  1309. priv->rtap_net_dev->get_stats = libertas_rtap_get_stats;
  1310. priv->rtap_net_dev->hard_start_xmit = libertas_rtap_hard_start_xmit;
  1311. priv->rtap_net_dev->set_multicast_list = libertas_set_multicast_list;
  1312. priv->rtap_net_dev->priv = priv;
  1313. priv->ieee->iw_mode = IW_MODE_MONITOR;
  1314. rc = register_netdev(priv->rtap_net_dev);
  1315. if (rc) {
  1316. free_ieee80211(priv->rtap_net_dev);
  1317. priv->rtap_net_dev = NULL;
  1318. return rc;
  1319. }
  1320. return 0;
  1321. }
  1322. module_init(libertas_init_module);
  1323. module_exit(libertas_exit_module);
  1324. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1325. MODULE_AUTHOR("Marvell International Ltd.");
  1326. MODULE_LICENSE("GPL");