main.c 37 KB

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