main.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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/etherdevice.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/kthread.h>
  12. #include <linux/kfifo.h>
  13. #include <linux/stddef.h>
  14. #include <linux/ieee80211.h>
  15. #include <net/iw_handler.h>
  16. #include "host.h"
  17. #include "decl.h"
  18. #include "dev.h"
  19. #include "wext.h"
  20. #include "debugfs.h"
  21. #include "scan.h"
  22. #include "assoc.h"
  23. #include "cmd.h"
  24. #define DRIVER_RELEASE_VERSION "323.p0"
  25. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  26. #ifdef DEBUG
  27. "-dbg"
  28. #endif
  29. "";
  30. /* Module parameters */
  31. unsigned int lbs_debug;
  32. EXPORT_SYMBOL_GPL(lbs_debug);
  33. module_param_named(libertas_debug, lbs_debug, int, 0644);
  34. /* This global structure is used to send the confirm_sleep command as
  35. * fast as possible down to the firmware. */
  36. struct cmd_confirm_sleep confirm_sleep;
  37. #define LBS_TX_PWR_DEFAULT 20 /*100mW */
  38. #define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
  39. #define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
  40. #define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
  41. #define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
  42. /* Format { channel, frequency (MHz), maxtxpower } */
  43. /* band: 'B/G', region: USA FCC/Canada IC */
  44. static struct chan_freq_power channel_freq_power_US_BG[] = {
  45. {1, 2412, LBS_TX_PWR_US_DEFAULT},
  46. {2, 2417, LBS_TX_PWR_US_DEFAULT},
  47. {3, 2422, LBS_TX_PWR_US_DEFAULT},
  48. {4, 2427, LBS_TX_PWR_US_DEFAULT},
  49. {5, 2432, LBS_TX_PWR_US_DEFAULT},
  50. {6, 2437, LBS_TX_PWR_US_DEFAULT},
  51. {7, 2442, LBS_TX_PWR_US_DEFAULT},
  52. {8, 2447, LBS_TX_PWR_US_DEFAULT},
  53. {9, 2452, LBS_TX_PWR_US_DEFAULT},
  54. {10, 2457, LBS_TX_PWR_US_DEFAULT},
  55. {11, 2462, LBS_TX_PWR_US_DEFAULT}
  56. };
  57. /* band: 'B/G', region: Europe ETSI */
  58. static struct chan_freq_power channel_freq_power_EU_BG[] = {
  59. {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
  60. {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
  61. {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
  62. {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
  63. {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
  64. {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
  65. {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
  66. {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
  67. {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
  68. {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
  69. {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
  70. {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
  71. {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
  72. };
  73. /* band: 'B/G', region: Spain */
  74. static struct chan_freq_power channel_freq_power_SPN_BG[] = {
  75. {10, 2457, LBS_TX_PWR_DEFAULT},
  76. {11, 2462, LBS_TX_PWR_DEFAULT}
  77. };
  78. /* band: 'B/G', region: France */
  79. static struct chan_freq_power channel_freq_power_FR_BG[] = {
  80. {10, 2457, LBS_TX_PWR_FR_DEFAULT},
  81. {11, 2462, LBS_TX_PWR_FR_DEFAULT},
  82. {12, 2467, LBS_TX_PWR_FR_DEFAULT},
  83. {13, 2472, LBS_TX_PWR_FR_DEFAULT}
  84. };
  85. /* band: 'B/G', region: Japan */
  86. static struct chan_freq_power channel_freq_power_JPN_BG[] = {
  87. {1, 2412, LBS_TX_PWR_JP_DEFAULT},
  88. {2, 2417, LBS_TX_PWR_JP_DEFAULT},
  89. {3, 2422, LBS_TX_PWR_JP_DEFAULT},
  90. {4, 2427, LBS_TX_PWR_JP_DEFAULT},
  91. {5, 2432, LBS_TX_PWR_JP_DEFAULT},
  92. {6, 2437, LBS_TX_PWR_JP_DEFAULT},
  93. {7, 2442, LBS_TX_PWR_JP_DEFAULT},
  94. {8, 2447, LBS_TX_PWR_JP_DEFAULT},
  95. {9, 2452, LBS_TX_PWR_JP_DEFAULT},
  96. {10, 2457, LBS_TX_PWR_JP_DEFAULT},
  97. {11, 2462, LBS_TX_PWR_JP_DEFAULT},
  98. {12, 2467, LBS_TX_PWR_JP_DEFAULT},
  99. {13, 2472, LBS_TX_PWR_JP_DEFAULT},
  100. {14, 2484, LBS_TX_PWR_JP_DEFAULT}
  101. };
  102. /**
  103. * the structure for channel, frequency and power
  104. */
  105. struct region_cfp_table {
  106. u8 region;
  107. struct chan_freq_power *cfp_BG;
  108. int cfp_no_BG;
  109. };
  110. /**
  111. * the structure for the mapping between region and CFP
  112. */
  113. static struct region_cfp_table region_cfp_table[] = {
  114. {0x10, /*US FCC */
  115. channel_freq_power_US_BG,
  116. ARRAY_SIZE(channel_freq_power_US_BG),
  117. }
  118. ,
  119. {0x20, /*CANADA IC */
  120. channel_freq_power_US_BG,
  121. ARRAY_SIZE(channel_freq_power_US_BG),
  122. }
  123. ,
  124. {0x30, /*EU*/ channel_freq_power_EU_BG,
  125. ARRAY_SIZE(channel_freq_power_EU_BG),
  126. }
  127. ,
  128. {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
  129. ARRAY_SIZE(channel_freq_power_SPN_BG),
  130. }
  131. ,
  132. {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
  133. ARRAY_SIZE(channel_freq_power_FR_BG),
  134. }
  135. ,
  136. {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
  137. ARRAY_SIZE(channel_freq_power_JPN_BG),
  138. }
  139. ,
  140. /*Add new region here */
  141. };
  142. /**
  143. * the table to keep region code
  144. */
  145. u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  146. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  147. /**
  148. * 802.11b/g supported bitrates (in 500Kb/s units)
  149. */
  150. u8 lbs_bg_rates[MAX_RATES] =
  151. { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  152. 0x00, 0x00 };
  153. /**
  154. * FW rate table. FW refers to rates by their index in this table, not by the
  155. * rate value itself. Values of 0x00 are
  156. * reserved positions.
  157. */
  158. static u8 fw_data_rates[MAX_RATES] =
  159. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  160. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  161. };
  162. /**
  163. * @brief use index to get the data rate
  164. *
  165. * @param idx The index of data rate
  166. * @return data rate or 0
  167. */
  168. u32 lbs_fw_index_to_data_rate(u8 idx)
  169. {
  170. if (idx >= sizeof(fw_data_rates))
  171. idx = 0;
  172. return fw_data_rates[idx];
  173. }
  174. /**
  175. * @brief use rate to get the index
  176. *
  177. * @param rate data rate
  178. * @return index or 0
  179. */
  180. u8 lbs_data_rate_to_fw_index(u32 rate)
  181. {
  182. u8 i;
  183. if (!rate)
  184. return 0;
  185. for (i = 0; i < sizeof(fw_data_rates); i++) {
  186. if (rate == fw_data_rates[i])
  187. return i;
  188. }
  189. return 0;
  190. }
  191. /**
  192. * Attributes exported through sysfs
  193. */
  194. /**
  195. * @brief Get function for sysfs attribute anycast_mask
  196. */
  197. static ssize_t lbs_anycast_get(struct device *dev,
  198. struct device_attribute *attr, char * buf)
  199. {
  200. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  201. struct cmd_ds_mesh_access mesh_access;
  202. int ret;
  203. memset(&mesh_access, 0, sizeof(mesh_access));
  204. ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
  205. if (ret)
  206. return ret;
  207. return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
  208. }
  209. /**
  210. * @brief Set function for sysfs attribute anycast_mask
  211. */
  212. static ssize_t lbs_anycast_set(struct device *dev,
  213. struct device_attribute *attr, const char * buf, size_t count)
  214. {
  215. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  216. struct cmd_ds_mesh_access mesh_access;
  217. uint32_t datum;
  218. int ret;
  219. memset(&mesh_access, 0, sizeof(mesh_access));
  220. sscanf(buf, "%x", &datum);
  221. mesh_access.data[0] = cpu_to_le32(datum);
  222. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
  223. if (ret)
  224. return ret;
  225. return strlen(buf);
  226. }
  227. /**
  228. * @brief Get function for sysfs attribute prb_rsp_limit
  229. */
  230. static ssize_t lbs_prb_rsp_limit_get(struct device *dev,
  231. struct device_attribute *attr, char *buf)
  232. {
  233. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  234. struct cmd_ds_mesh_access mesh_access;
  235. int ret;
  236. u32 retry_limit;
  237. memset(&mesh_access, 0, sizeof(mesh_access));
  238. mesh_access.data[0] = cpu_to_le32(CMD_ACT_GET);
  239. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
  240. &mesh_access);
  241. if (ret)
  242. return ret;
  243. retry_limit = le32_to_cpu(mesh_access.data[1]);
  244. return snprintf(buf, 10, "%d\n", retry_limit);
  245. }
  246. /**
  247. * @brief Set function for sysfs attribute prb_rsp_limit
  248. */
  249. static ssize_t lbs_prb_rsp_limit_set(struct device *dev,
  250. struct device_attribute *attr, const char *buf, size_t count)
  251. {
  252. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  253. struct cmd_ds_mesh_access mesh_access;
  254. int ret;
  255. unsigned long retry_limit;
  256. memset(&mesh_access, 0, sizeof(mesh_access));
  257. mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
  258. if (!strict_strtoul(buf, 10, &retry_limit))
  259. return -ENOTSUPP;
  260. if (retry_limit > 15)
  261. return -ENOTSUPP;
  262. mesh_access.data[1] = cpu_to_le32(retry_limit);
  263. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
  264. &mesh_access);
  265. if (ret)
  266. return ret;
  267. return strlen(buf);
  268. }
  269. static int lbs_add_rtap(struct lbs_private *priv);
  270. static void lbs_remove_rtap(struct lbs_private *priv);
  271. static int lbs_add_mesh(struct lbs_private *priv);
  272. static void lbs_remove_mesh(struct lbs_private *priv);
  273. /**
  274. * Get function for sysfs attribute rtap
  275. */
  276. static ssize_t lbs_rtap_get(struct device *dev,
  277. struct device_attribute *attr, char * buf)
  278. {
  279. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  280. return snprintf(buf, 5, "0x%X\n", priv->monitormode);
  281. }
  282. /**
  283. * Set function for sysfs attribute rtap
  284. */
  285. static ssize_t lbs_rtap_set(struct device *dev,
  286. struct device_attribute *attr, const char * buf, size_t count)
  287. {
  288. int monitor_mode;
  289. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  290. sscanf(buf, "%x", &monitor_mode);
  291. if (monitor_mode) {
  292. if (priv->monitormode == monitor_mode)
  293. return strlen(buf);
  294. if (!priv->monitormode) {
  295. if (priv->infra_open || priv->mesh_open)
  296. return -EBUSY;
  297. if (priv->mode == IW_MODE_INFRA)
  298. lbs_cmd_80211_deauthenticate(priv,
  299. priv->curbssparams.bssid,
  300. WLAN_REASON_DEAUTH_LEAVING);
  301. else if (priv->mode == IW_MODE_ADHOC)
  302. lbs_adhoc_stop(priv);
  303. lbs_add_rtap(priv);
  304. }
  305. priv->monitormode = monitor_mode;
  306. } else {
  307. if (!priv->monitormode)
  308. return strlen(buf);
  309. priv->monitormode = 0;
  310. lbs_remove_rtap(priv);
  311. if (priv->currenttxskb) {
  312. dev_kfree_skb_any(priv->currenttxskb);
  313. priv->currenttxskb = NULL;
  314. }
  315. /* Wake queues, command thread, etc. */
  316. lbs_host_to_card_done(priv);
  317. }
  318. lbs_prepare_and_send_command(priv,
  319. CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
  320. CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
  321. return strlen(buf);
  322. }
  323. /**
  324. * lbs_rtap attribute to be exported per ethX interface
  325. * through sysfs (/sys/class/net/ethX/lbs_rtap)
  326. */
  327. static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
  328. /**
  329. * Get function for sysfs attribute mesh
  330. */
  331. static ssize_t lbs_mesh_get(struct device *dev,
  332. struct device_attribute *attr, char * buf)
  333. {
  334. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  335. return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
  336. }
  337. /**
  338. * Set function for sysfs attribute mesh
  339. */
  340. static ssize_t lbs_mesh_set(struct device *dev,
  341. struct device_attribute *attr, const char * buf, size_t count)
  342. {
  343. struct lbs_private *priv = to_net_dev(dev)->ml_priv;
  344. int enable;
  345. int ret, action = CMD_ACT_MESH_CONFIG_STOP;
  346. sscanf(buf, "%x", &enable);
  347. enable = !!enable;
  348. if (enable == !!priv->mesh_dev)
  349. return count;
  350. if (enable)
  351. action = CMD_ACT_MESH_CONFIG_START;
  352. ret = lbs_mesh_config(priv, action, priv->curbssparams.channel);
  353. if (ret)
  354. return ret;
  355. if (enable)
  356. lbs_add_mesh(priv);
  357. else
  358. lbs_remove_mesh(priv);
  359. return count;
  360. }
  361. /**
  362. * lbs_mesh attribute to be exported per ethX interface
  363. * through sysfs (/sys/class/net/ethX/lbs_mesh)
  364. */
  365. static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
  366. /**
  367. * anycast_mask attribute to be exported per mshX interface
  368. * through sysfs (/sys/class/net/mshX/anycast_mask)
  369. */
  370. static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
  371. /**
  372. * prb_rsp_limit attribute to be exported per mshX interface
  373. * through sysfs (/sys/class/net/mshX/prb_rsp_limit)
  374. */
  375. static DEVICE_ATTR(prb_rsp_limit, 0644, lbs_prb_rsp_limit_get,
  376. lbs_prb_rsp_limit_set);
  377. static struct attribute *lbs_mesh_sysfs_entries[] = {
  378. &dev_attr_anycast_mask.attr,
  379. &dev_attr_prb_rsp_limit.attr,
  380. NULL,
  381. };
  382. static struct attribute_group lbs_mesh_attr_group = {
  383. .attrs = lbs_mesh_sysfs_entries,
  384. };
  385. /**
  386. * @brief This function opens the ethX or mshX interface
  387. *
  388. * @param dev A pointer to net_device structure
  389. * @return 0 or -EBUSY if monitor mode active
  390. */
  391. static int lbs_dev_open(struct net_device *dev)
  392. {
  393. struct lbs_private *priv = dev->ml_priv;
  394. int ret = 0;
  395. lbs_deb_enter(LBS_DEB_NET);
  396. spin_lock_irq(&priv->driver_lock);
  397. if (priv->monitormode) {
  398. ret = -EBUSY;
  399. goto out;
  400. }
  401. if (dev == priv->mesh_dev) {
  402. priv->mesh_open = 1;
  403. priv->mesh_connect_status = LBS_CONNECTED;
  404. netif_carrier_on(dev);
  405. } else {
  406. priv->infra_open = 1;
  407. if (priv->connect_status == LBS_CONNECTED)
  408. netif_carrier_on(dev);
  409. else
  410. netif_carrier_off(dev);
  411. }
  412. if (!priv->tx_pending_len)
  413. netif_wake_queue(dev);
  414. out:
  415. spin_unlock_irq(&priv->driver_lock);
  416. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  417. return ret;
  418. }
  419. /**
  420. * @brief This function closes the mshX interface
  421. *
  422. * @param dev A pointer to net_device structure
  423. * @return 0
  424. */
  425. static int lbs_mesh_stop(struct net_device *dev)
  426. {
  427. struct lbs_private *priv = dev->ml_priv;
  428. lbs_deb_enter(LBS_DEB_MESH);
  429. spin_lock_irq(&priv->driver_lock);
  430. priv->mesh_open = 0;
  431. priv->mesh_connect_status = LBS_DISCONNECTED;
  432. netif_stop_queue(dev);
  433. netif_carrier_off(dev);
  434. spin_unlock_irq(&priv->driver_lock);
  435. schedule_work(&priv->mcast_work);
  436. lbs_deb_leave(LBS_DEB_MESH);
  437. return 0;
  438. }
  439. /**
  440. * @brief This function closes the ethX interface
  441. *
  442. * @param dev A pointer to net_device structure
  443. * @return 0
  444. */
  445. static int lbs_eth_stop(struct net_device *dev)
  446. {
  447. struct lbs_private *priv = dev->ml_priv;
  448. lbs_deb_enter(LBS_DEB_NET);
  449. spin_lock_irq(&priv->driver_lock);
  450. priv->infra_open = 0;
  451. netif_stop_queue(dev);
  452. spin_unlock_irq(&priv->driver_lock);
  453. schedule_work(&priv->mcast_work);
  454. lbs_deb_leave(LBS_DEB_NET);
  455. return 0;
  456. }
  457. static void lbs_tx_timeout(struct net_device *dev)
  458. {
  459. struct lbs_private *priv = dev->ml_priv;
  460. lbs_deb_enter(LBS_DEB_TX);
  461. lbs_pr_err("tx watch dog timeout\n");
  462. dev->trans_start = jiffies;
  463. if (priv->currenttxskb)
  464. lbs_send_tx_feedback(priv, 0);
  465. /* XX: Shouldn't we also call into the hw-specific driver
  466. to kick it somehow? */
  467. lbs_host_to_card_done(priv);
  468. /* More often than not, this actually happens because the
  469. firmware has crapped itself -- rather than just a very
  470. busy medium. So send a harmless command, and if/when
  471. _that_ times out, we'll kick it in the head. */
  472. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  473. 0, 0, NULL);
  474. lbs_deb_leave(LBS_DEB_TX);
  475. }
  476. void lbs_host_to_card_done(struct lbs_private *priv)
  477. {
  478. unsigned long flags;
  479. lbs_deb_enter(LBS_DEB_THREAD);
  480. spin_lock_irqsave(&priv->driver_lock, flags);
  481. priv->dnld_sent = DNLD_RES_RECEIVED;
  482. /* Wake main thread if commands are pending */
  483. if (!priv->cur_cmd || priv->tx_pending_len > 0)
  484. wake_up_interruptible(&priv->waitq);
  485. spin_unlock_irqrestore(&priv->driver_lock, flags);
  486. lbs_deb_leave(LBS_DEB_THREAD);
  487. }
  488. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  489. static int lbs_set_mac_address(struct net_device *dev, void *addr)
  490. {
  491. int ret = 0;
  492. struct lbs_private *priv = dev->ml_priv;
  493. struct sockaddr *phwaddr = addr;
  494. struct cmd_ds_802_11_mac_address cmd;
  495. lbs_deb_enter(LBS_DEB_NET);
  496. /* In case it was called from the mesh device */
  497. dev = priv->dev;
  498. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  499. cmd.action = cpu_to_le16(CMD_ACT_SET);
  500. memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
  501. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  502. if (ret) {
  503. lbs_deb_net("set MAC address failed\n");
  504. goto done;
  505. }
  506. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  507. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  508. if (priv->mesh_dev)
  509. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  510. done:
  511. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  512. return ret;
  513. }
  514. static inline int mac_in_list(unsigned char *list, int list_len,
  515. unsigned char *mac)
  516. {
  517. while (list_len) {
  518. if (!memcmp(list, mac, ETH_ALEN))
  519. return 1;
  520. list += ETH_ALEN;
  521. list_len--;
  522. }
  523. return 0;
  524. }
  525. static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
  526. struct net_device *dev, int nr_addrs)
  527. {
  528. int i = nr_addrs;
  529. struct dev_mc_list *mc_list;
  530. if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
  531. return nr_addrs;
  532. netif_addr_lock_bh(dev);
  533. for (mc_list = dev->mc_list; mc_list; mc_list = mc_list->next) {
  534. if (mac_in_list(cmd->maclist, nr_addrs, mc_list->dmi_addr)) {
  535. lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
  536. mc_list->dmi_addr);
  537. continue;
  538. }
  539. if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
  540. break;
  541. memcpy(&cmd->maclist[6*i], mc_list->dmi_addr, ETH_ALEN);
  542. lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
  543. mc_list->dmi_addr);
  544. i++;
  545. }
  546. netif_addr_unlock_bh(dev);
  547. if (mc_list)
  548. return -EOVERFLOW;
  549. return i;
  550. }
  551. static void lbs_set_mcast_worker(struct work_struct *work)
  552. {
  553. struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
  554. struct cmd_ds_mac_multicast_adr mcast_cmd;
  555. int dev_flags;
  556. int nr_addrs;
  557. int old_mac_control = priv->mac_control;
  558. lbs_deb_enter(LBS_DEB_NET);
  559. dev_flags = priv->dev->flags;
  560. if (priv->mesh_dev)
  561. dev_flags |= priv->mesh_dev->flags;
  562. if (dev_flags & IFF_PROMISC) {
  563. priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  564. priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  565. CMD_ACT_MAC_MULTICAST_ENABLE);
  566. goto out_set_mac_control;
  567. } else if (dev_flags & IFF_ALLMULTI) {
  568. do_allmulti:
  569. priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  570. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  571. CMD_ACT_MAC_MULTICAST_ENABLE);
  572. goto out_set_mac_control;
  573. }
  574. /* Once for priv->dev, again for priv->mesh_dev if it exists */
  575. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
  576. if (nr_addrs >= 0 && priv->mesh_dev)
  577. nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
  578. if (nr_addrs < 0)
  579. goto do_allmulti;
  580. if (nr_addrs) {
  581. int size = offsetof(struct cmd_ds_mac_multicast_adr,
  582. maclist[6*nr_addrs]);
  583. mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
  584. mcast_cmd.hdr.size = cpu_to_le16(size);
  585. mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
  586. lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
  587. priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
  588. } else
  589. priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
  590. priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
  591. CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
  592. out_set_mac_control:
  593. if (priv->mac_control != old_mac_control)
  594. lbs_set_mac_control(priv);
  595. lbs_deb_leave(LBS_DEB_NET);
  596. }
  597. static void lbs_set_multicast_list(struct net_device *dev)
  598. {
  599. struct lbs_private *priv = dev->ml_priv;
  600. schedule_work(&priv->mcast_work);
  601. }
  602. /**
  603. * @brief This function handles the major jobs in the LBS driver.
  604. * It handles all events generated by firmware, RX data received
  605. * from firmware and TX data sent from kernel.
  606. *
  607. * @param data A pointer to lbs_thread structure
  608. * @return 0
  609. */
  610. static int lbs_thread(void *data)
  611. {
  612. struct net_device *dev = data;
  613. struct lbs_private *priv = dev->ml_priv;
  614. wait_queue_t wait;
  615. lbs_deb_enter(LBS_DEB_THREAD);
  616. init_waitqueue_entry(&wait, current);
  617. for (;;) {
  618. int shouldsleep;
  619. u8 resp_idx;
  620. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  621. priv->currenttxskb, priv->dnld_sent);
  622. add_wait_queue(&priv->waitq, &wait);
  623. set_current_state(TASK_INTERRUPTIBLE);
  624. spin_lock_irq(&priv->driver_lock);
  625. if (kthread_should_stop())
  626. shouldsleep = 0; /* Bye */
  627. else if (priv->surpriseremoved)
  628. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  629. else if (priv->psstate == PS_STATE_SLEEP)
  630. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  631. else if (priv->cmd_timed_out)
  632. shouldsleep = 0; /* Command timed out. Recover */
  633. else if (!priv->fw_ready)
  634. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  635. else if (priv->dnld_sent)
  636. shouldsleep = 1; /* Something is en route to the device already */
  637. else if (priv->tx_pending_len > 0)
  638. shouldsleep = 0; /* We've a packet to send */
  639. else if (priv->resp_len[priv->resp_idx])
  640. shouldsleep = 0; /* We have a command response */
  641. else if (priv->cur_cmd)
  642. shouldsleep = 1; /* Can't send a command; one already running */
  643. else if (!list_empty(&priv->cmdpendingq))
  644. shouldsleep = 0; /* We have a command to send */
  645. else if (__kfifo_len(priv->event_fifo))
  646. shouldsleep = 0; /* We have an event to process */
  647. else
  648. shouldsleep = 1; /* No command */
  649. if (shouldsleep) {
  650. lbs_deb_thread("sleeping, connect_status %d, "
  651. "psmode %d, psstate %d\n",
  652. priv->connect_status,
  653. priv->psmode, priv->psstate);
  654. spin_unlock_irq(&priv->driver_lock);
  655. schedule();
  656. } else
  657. spin_unlock_irq(&priv->driver_lock);
  658. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  659. priv->currenttxskb, priv->dnld_sent);
  660. set_current_state(TASK_RUNNING);
  661. remove_wait_queue(&priv->waitq, &wait);
  662. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  663. priv->currenttxskb, priv->dnld_sent);
  664. if (kthread_should_stop()) {
  665. lbs_deb_thread("break from main thread\n");
  666. break;
  667. }
  668. if (priv->surpriseremoved) {
  669. lbs_deb_thread("adapter removed; waiting to die...\n");
  670. continue;
  671. }
  672. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  673. priv->currenttxskb, priv->dnld_sent);
  674. /* Process any pending command response */
  675. spin_lock_irq(&priv->driver_lock);
  676. resp_idx = priv->resp_idx;
  677. if (priv->resp_len[resp_idx]) {
  678. spin_unlock_irq(&priv->driver_lock);
  679. lbs_process_command_response(priv,
  680. priv->resp_buf[resp_idx],
  681. priv->resp_len[resp_idx]);
  682. spin_lock_irq(&priv->driver_lock);
  683. priv->resp_len[resp_idx] = 0;
  684. }
  685. spin_unlock_irq(&priv->driver_lock);
  686. /* command timeout stuff */
  687. if (priv->cmd_timed_out && priv->cur_cmd) {
  688. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  689. if (++priv->nr_retries > 3) {
  690. lbs_pr_info("Excessive timeouts submitting "
  691. "command 0x%04x\n",
  692. le16_to_cpu(cmdnode->cmdbuf->command));
  693. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  694. priv->nr_retries = 0;
  695. if (priv->reset_card)
  696. priv->reset_card(priv);
  697. } else {
  698. priv->cur_cmd = NULL;
  699. priv->dnld_sent = DNLD_RES_RECEIVED;
  700. lbs_pr_info("requeueing command 0x%04x due "
  701. "to timeout (#%d)\n",
  702. le16_to_cpu(cmdnode->cmdbuf->command),
  703. priv->nr_retries);
  704. /* Stick it back at the _top_ of the pending queue
  705. for immediate resubmission */
  706. list_add(&cmdnode->list, &priv->cmdpendingq);
  707. }
  708. }
  709. priv->cmd_timed_out = 0;
  710. /* Process hardware events, e.g. card removed, link lost */
  711. spin_lock_irq(&priv->driver_lock);
  712. while (__kfifo_len(priv->event_fifo)) {
  713. u32 event;
  714. __kfifo_get(priv->event_fifo, (unsigned char *) &event,
  715. sizeof(event));
  716. spin_unlock_irq(&priv->driver_lock);
  717. lbs_process_event(priv, event);
  718. spin_lock_irq(&priv->driver_lock);
  719. }
  720. spin_unlock_irq(&priv->driver_lock);
  721. if (!priv->fw_ready)
  722. continue;
  723. /* Check if we need to confirm Sleep Request received previously */
  724. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  725. !priv->dnld_sent && !priv->cur_cmd) {
  726. if (priv->connect_status == LBS_CONNECTED) {
  727. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  728. "dnld_sent %d, cur_cmd %p\n",
  729. priv->currenttxskb, priv->dnld_sent,
  730. priv->cur_cmd);
  731. lbs_ps_confirm_sleep(priv);
  732. } else {
  733. /* workaround for firmware sending
  734. * deauth/linkloss event immediately
  735. * after sleep request; remove this
  736. * after firmware fixes it
  737. */
  738. priv->psstate = PS_STATE_AWAKE;
  739. lbs_pr_alert("ignore PS_SleepConfirm in "
  740. "non-connected state\n");
  741. }
  742. }
  743. /* The PS state is changed during processing of Sleep Request
  744. * event above
  745. */
  746. if ((priv->psstate == PS_STATE_SLEEP) ||
  747. (priv->psstate == PS_STATE_PRE_SLEEP))
  748. continue;
  749. /* Execute the next command */
  750. if (!priv->dnld_sent && !priv->cur_cmd)
  751. lbs_execute_next_command(priv);
  752. /* Wake-up command waiters which can't sleep in
  753. * lbs_prepare_and_send_command
  754. */
  755. if (!list_empty(&priv->cmdpendingq))
  756. wake_up_all(&priv->cmd_pending);
  757. spin_lock_irq(&priv->driver_lock);
  758. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  759. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  760. priv->tx_pending_buf,
  761. priv->tx_pending_len);
  762. if (ret) {
  763. lbs_deb_tx("host_to_card failed %d\n", ret);
  764. priv->dnld_sent = DNLD_RES_RECEIVED;
  765. }
  766. priv->tx_pending_len = 0;
  767. if (!priv->currenttxskb) {
  768. /* We can wake the queues immediately if we aren't
  769. waiting for TX feedback */
  770. if (priv->connect_status == LBS_CONNECTED)
  771. netif_wake_queue(priv->dev);
  772. if (priv->mesh_dev &&
  773. priv->mesh_connect_status == LBS_CONNECTED)
  774. netif_wake_queue(priv->mesh_dev);
  775. }
  776. }
  777. spin_unlock_irq(&priv->driver_lock);
  778. }
  779. del_timer(&priv->command_timer);
  780. wake_up_all(&priv->cmd_pending);
  781. lbs_deb_leave(LBS_DEB_THREAD);
  782. return 0;
  783. }
  784. static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
  785. struct cmd_header *cmd)
  786. {
  787. lbs_deb_enter(LBS_DEB_FW);
  788. netif_device_detach(priv->dev);
  789. if (priv->mesh_dev)
  790. netif_device_detach(priv->mesh_dev);
  791. priv->fw_ready = 0;
  792. lbs_deb_leave(LBS_DEB_FW);
  793. return 0;
  794. }
  795. int lbs_suspend(struct lbs_private *priv)
  796. {
  797. struct cmd_header cmd;
  798. int ret;
  799. lbs_deb_enter(LBS_DEB_FW);
  800. if (priv->wol_criteria == 0xffffffff) {
  801. lbs_pr_info("Suspend attempt without configuring wake params!\n");
  802. return -EINVAL;
  803. }
  804. memset(&cmd, 0, sizeof(cmd));
  805. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
  806. sizeof(cmd), lbs_suspend_callback, 0);
  807. if (ret)
  808. lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
  809. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  810. return ret;
  811. }
  812. EXPORT_SYMBOL_GPL(lbs_suspend);
  813. void lbs_resume(struct lbs_private *priv)
  814. {
  815. lbs_deb_enter(LBS_DEB_FW);
  816. priv->fw_ready = 1;
  817. /* Firmware doesn't seem to give us RX packets any more
  818. until we send it some command. Might as well update */
  819. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  820. 0, 0, NULL);
  821. netif_device_attach(priv->dev);
  822. if (priv->mesh_dev)
  823. netif_device_attach(priv->mesh_dev);
  824. lbs_deb_leave(LBS_DEB_FW);
  825. }
  826. EXPORT_SYMBOL_GPL(lbs_resume);
  827. /**
  828. * @brief This function gets the HW spec from the firmware and sets
  829. * some basic parameters.
  830. *
  831. * @param priv A pointer to struct lbs_private structure
  832. * @return 0 or -1
  833. */
  834. static int lbs_setup_firmware(struct lbs_private *priv)
  835. {
  836. int ret = -1;
  837. s16 curlevel = 0, minlevel = 0, maxlevel = 0;
  838. lbs_deb_enter(LBS_DEB_FW);
  839. /* Read MAC address from firmware */
  840. memset(priv->current_addr, 0xff, ETH_ALEN);
  841. ret = lbs_update_hw_spec(priv);
  842. if (ret)
  843. goto done;
  844. /* Read power levels if available */
  845. ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
  846. if (ret == 0) {
  847. priv->txpower_cur = curlevel;
  848. priv->txpower_min = minlevel;
  849. priv->txpower_max = maxlevel;
  850. }
  851. lbs_set_mac_control(priv);
  852. done:
  853. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  854. return ret;
  855. }
  856. /**
  857. * This function handles the timeout of command sending.
  858. * It will re-send the same command again.
  859. */
  860. static void command_timer_fn(unsigned long data)
  861. {
  862. struct lbs_private *priv = (struct lbs_private *)data;
  863. unsigned long flags;
  864. lbs_deb_enter(LBS_DEB_CMD);
  865. spin_lock_irqsave(&priv->driver_lock, flags);
  866. if (!priv->cur_cmd)
  867. goto out;
  868. lbs_pr_info("command 0x%04x timed out\n",
  869. le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  870. priv->cmd_timed_out = 1;
  871. wake_up_interruptible(&priv->waitq);
  872. out:
  873. spin_unlock_irqrestore(&priv->driver_lock, flags);
  874. lbs_deb_leave(LBS_DEB_CMD);
  875. }
  876. static void lbs_sync_channel_worker(struct work_struct *work)
  877. {
  878. struct lbs_private *priv = container_of(work, struct lbs_private,
  879. sync_channel);
  880. lbs_deb_enter(LBS_DEB_MAIN);
  881. if (lbs_update_channel(priv))
  882. lbs_pr_info("Channel synchronization failed.");
  883. lbs_deb_leave(LBS_DEB_MAIN);
  884. }
  885. static int lbs_init_adapter(struct lbs_private *priv)
  886. {
  887. size_t bufsize;
  888. int i, ret = 0;
  889. lbs_deb_enter(LBS_DEB_MAIN);
  890. /* Allocate buffer to store the BSSID list */
  891. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  892. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  893. if (!priv->networks) {
  894. lbs_pr_err("Out of memory allocating beacons\n");
  895. ret = -1;
  896. goto out;
  897. }
  898. /* Initialize scan result lists */
  899. INIT_LIST_HEAD(&priv->network_free_list);
  900. INIT_LIST_HEAD(&priv->network_list);
  901. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  902. list_add_tail(&priv->networks[i].list,
  903. &priv->network_free_list);
  904. }
  905. memset(priv->current_addr, 0xff, ETH_ALEN);
  906. priv->connect_status = LBS_DISCONNECTED;
  907. priv->mesh_connect_status = LBS_DISCONNECTED;
  908. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  909. priv->mode = IW_MODE_INFRA;
  910. priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  911. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  912. priv->radio_on = 1;
  913. priv->enablehwauto = 1;
  914. priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  915. priv->psmode = LBS802_11POWERMODECAM;
  916. priv->psstate = PS_STATE_FULL_POWER;
  917. mutex_init(&priv->lock);
  918. setup_timer(&priv->command_timer, command_timer_fn,
  919. (unsigned long)priv);
  920. INIT_LIST_HEAD(&priv->cmdfreeq);
  921. INIT_LIST_HEAD(&priv->cmdpendingq);
  922. spin_lock_init(&priv->driver_lock);
  923. init_waitqueue_head(&priv->cmd_pending);
  924. /* Allocate the command buffers */
  925. if (lbs_allocate_cmd_buffer(priv)) {
  926. lbs_pr_err("Out of memory allocating command buffers\n");
  927. ret = -ENOMEM;
  928. goto out;
  929. }
  930. priv->resp_idx = 0;
  931. priv->resp_len[0] = priv->resp_len[1] = 0;
  932. /* Create the event FIFO */
  933. priv->event_fifo = kfifo_alloc(sizeof(u32) * 16, GFP_KERNEL, NULL);
  934. if (IS_ERR(priv->event_fifo)) {
  935. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  936. ret = -ENOMEM;
  937. goto out;
  938. }
  939. out:
  940. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  941. return ret;
  942. }
  943. static void lbs_free_adapter(struct lbs_private *priv)
  944. {
  945. lbs_deb_enter(LBS_DEB_MAIN);
  946. lbs_free_cmd_buffer(priv);
  947. if (priv->event_fifo)
  948. kfifo_free(priv->event_fifo);
  949. del_timer(&priv->command_timer);
  950. kfree(priv->networks);
  951. priv->networks = NULL;
  952. lbs_deb_leave(LBS_DEB_MAIN);
  953. }
  954. /**
  955. * @brief This function adds the card. it will probe the
  956. * card, allocate the lbs_priv and initialize the device.
  957. *
  958. * @param card A pointer to card
  959. * @return A pointer to struct lbs_private structure
  960. */
  961. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  962. {
  963. struct net_device *dev = NULL;
  964. struct lbs_private *priv = NULL;
  965. lbs_deb_enter(LBS_DEB_MAIN);
  966. /* Allocate an Ethernet device and register it */
  967. dev = alloc_etherdev(sizeof(struct lbs_private));
  968. if (!dev) {
  969. lbs_pr_err("init ethX device failed\n");
  970. goto done;
  971. }
  972. priv = netdev_priv(dev);
  973. dev->ml_priv = priv;
  974. if (lbs_init_adapter(priv)) {
  975. lbs_pr_err("failed to initialize adapter structure.\n");
  976. goto err_init_adapter;
  977. }
  978. priv->dev = dev;
  979. priv->card = card;
  980. priv->mesh_open = 0;
  981. priv->infra_open = 0;
  982. /* Setup the OS Interface to our functions */
  983. dev->open = lbs_dev_open;
  984. dev->hard_start_xmit = lbs_hard_start_xmit;
  985. dev->stop = lbs_eth_stop;
  986. dev->set_mac_address = lbs_set_mac_address;
  987. dev->tx_timeout = lbs_tx_timeout;
  988. dev->watchdog_timeo = 5 * HZ;
  989. dev->ethtool_ops = &lbs_ethtool_ops;
  990. #ifdef WIRELESS_EXT
  991. dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
  992. #endif
  993. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  994. dev->set_multicast_list = lbs_set_multicast_list;
  995. SET_NETDEV_DEV(dev, dmdev);
  996. priv->rtap_net_dev = NULL;
  997. lbs_deb_thread("Starting main thread...\n");
  998. init_waitqueue_head(&priv->waitq);
  999. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  1000. if (IS_ERR(priv->main_thread)) {
  1001. lbs_deb_thread("Error creating main thread.\n");
  1002. goto err_init_adapter;
  1003. }
  1004. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  1005. INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
  1006. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  1007. INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
  1008. INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
  1009. sprintf(priv->mesh_ssid, "mesh");
  1010. priv->mesh_ssid_len = 4;
  1011. priv->wol_criteria = 0xffffffff;
  1012. priv->wol_gpio = 0xff;
  1013. goto done;
  1014. err_init_adapter:
  1015. lbs_free_adapter(priv);
  1016. free_netdev(dev);
  1017. priv = NULL;
  1018. done:
  1019. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  1020. return priv;
  1021. }
  1022. EXPORT_SYMBOL_GPL(lbs_add_card);
  1023. void lbs_remove_card(struct lbs_private *priv)
  1024. {
  1025. struct net_device *dev = priv->dev;
  1026. union iwreq_data wrqu;
  1027. lbs_deb_enter(LBS_DEB_MAIN);
  1028. lbs_remove_mesh(priv);
  1029. lbs_remove_rtap(priv);
  1030. dev = priv->dev;
  1031. cancel_delayed_work_sync(&priv->scan_work);
  1032. cancel_delayed_work_sync(&priv->assoc_work);
  1033. cancel_work_sync(&priv->mcast_work);
  1034. /* worker thread destruction blocks on the in-flight command which
  1035. * should have been cleared already in lbs_stop_card().
  1036. */
  1037. lbs_deb_main("destroying worker thread\n");
  1038. destroy_workqueue(priv->work_thread);
  1039. lbs_deb_main("done destroying worker thread\n");
  1040. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  1041. priv->psmode = LBS802_11POWERMODECAM;
  1042. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  1043. }
  1044. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  1045. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1046. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1047. /* Stop the thread servicing the interrupts */
  1048. priv->surpriseremoved = 1;
  1049. kthread_stop(priv->main_thread);
  1050. lbs_free_adapter(priv);
  1051. priv->dev = NULL;
  1052. free_netdev(dev);
  1053. lbs_deb_leave(LBS_DEB_MAIN);
  1054. }
  1055. EXPORT_SYMBOL_GPL(lbs_remove_card);
  1056. int lbs_start_card(struct lbs_private *priv)
  1057. {
  1058. struct net_device *dev = priv->dev;
  1059. int ret = -1;
  1060. lbs_deb_enter(LBS_DEB_MAIN);
  1061. /* poke the firmware */
  1062. ret = lbs_setup_firmware(priv);
  1063. if (ret)
  1064. goto done;
  1065. /* init 802.11d */
  1066. lbs_init_11d(priv);
  1067. if (register_netdev(dev)) {
  1068. lbs_pr_err("cannot register ethX device\n");
  1069. goto done;
  1070. }
  1071. lbs_update_channel(priv);
  1072. /* 5.0.16p0 is known to NOT support any mesh */
  1073. if (priv->fwrelease > 0x05001000) {
  1074. /* Enable mesh, if supported, and work out which TLV it uses.
  1075. 0x100 + 291 is an unofficial value used in 5.110.20.pXX
  1076. 0x100 + 37 is the official value used in 5.110.21.pXX
  1077. but we check them in that order because 20.pXX doesn't
  1078. give an error -- it just silently fails. */
  1079. /* 5.110.20.pXX firmware will fail the command if the channel
  1080. doesn't match the existing channel. But only if the TLV
  1081. is correct. If the channel is wrong, _BOTH_ versions will
  1082. give an error to 0x100+291, and allow 0x100+37 to succeed.
  1083. It's just that 5.110.20.pXX will not have done anything
  1084. useful */
  1085. priv->mesh_tlv = 0x100 + 291;
  1086. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  1087. priv->curbssparams.channel)) {
  1088. priv->mesh_tlv = 0x100 + 37;
  1089. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  1090. priv->curbssparams.channel))
  1091. priv->mesh_tlv = 0;
  1092. }
  1093. if (priv->mesh_tlv) {
  1094. lbs_add_mesh(priv);
  1095. if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
  1096. lbs_pr_err("cannot register lbs_mesh attribute\n");
  1097. /* While rtap isn't related to mesh, only mesh-enabled
  1098. * firmware implements the rtap functionality via
  1099. * CMD_802_11_MONITOR_MODE.
  1100. */
  1101. if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
  1102. lbs_pr_err("cannot register lbs_rtap attribute\n");
  1103. }
  1104. }
  1105. lbs_debugfs_init_one(priv, dev);
  1106. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  1107. ret = 0;
  1108. done:
  1109. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1110. return ret;
  1111. }
  1112. EXPORT_SYMBOL_GPL(lbs_start_card);
  1113. void lbs_stop_card(struct lbs_private *priv)
  1114. {
  1115. struct net_device *dev;
  1116. struct cmd_ctrl_node *cmdnode;
  1117. unsigned long flags;
  1118. lbs_deb_enter(LBS_DEB_MAIN);
  1119. if (!priv)
  1120. goto out;
  1121. dev = priv->dev;
  1122. netif_stop_queue(dev);
  1123. netif_carrier_off(dev);
  1124. lbs_debugfs_remove_one(priv);
  1125. if (priv->mesh_tlv) {
  1126. device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
  1127. device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
  1128. }
  1129. /* Delete the timeout of the currently processing command */
  1130. del_timer_sync(&priv->command_timer);
  1131. /* Flush pending command nodes */
  1132. spin_lock_irqsave(&priv->driver_lock, flags);
  1133. lbs_deb_main("clearing pending commands\n");
  1134. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  1135. cmdnode->result = -ENOENT;
  1136. cmdnode->cmdwaitqwoken = 1;
  1137. wake_up_interruptible(&cmdnode->cmdwait_q);
  1138. }
  1139. /* Flush the command the card is currently processing */
  1140. if (priv->cur_cmd) {
  1141. lbs_deb_main("clearing current command\n");
  1142. priv->cur_cmd->result = -ENOENT;
  1143. priv->cur_cmd->cmdwaitqwoken = 1;
  1144. wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
  1145. }
  1146. lbs_deb_main("done clearing commands\n");
  1147. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1148. unregister_netdev(dev);
  1149. out:
  1150. lbs_deb_leave(LBS_DEB_MAIN);
  1151. }
  1152. EXPORT_SYMBOL_GPL(lbs_stop_card);
  1153. /**
  1154. * @brief This function adds mshX interface
  1155. *
  1156. * @param priv A pointer to the struct lbs_private structure
  1157. * @return 0 if successful, -X otherwise
  1158. */
  1159. static int lbs_add_mesh(struct lbs_private *priv)
  1160. {
  1161. struct net_device *mesh_dev = NULL;
  1162. int ret = 0;
  1163. lbs_deb_enter(LBS_DEB_MESH);
  1164. /* Allocate a virtual mesh device */
  1165. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1166. lbs_deb_mesh("init mshX device failed\n");
  1167. ret = -ENOMEM;
  1168. goto done;
  1169. }
  1170. mesh_dev->ml_priv = priv;
  1171. priv->mesh_dev = mesh_dev;
  1172. mesh_dev->open = lbs_dev_open;
  1173. mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
  1174. mesh_dev->stop = lbs_mesh_stop;
  1175. mesh_dev->set_mac_address = lbs_set_mac_address;
  1176. mesh_dev->ethtool_ops = &lbs_ethtool_ops;
  1177. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1178. sizeof(priv->dev->dev_addr));
  1179. SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
  1180. #ifdef WIRELESS_EXT
  1181. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1182. #endif
  1183. mesh_dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1184. mesh_dev->set_multicast_list = lbs_set_multicast_list;
  1185. /* Register virtual mesh interface */
  1186. ret = register_netdev(mesh_dev);
  1187. if (ret) {
  1188. lbs_pr_err("cannot register mshX virtual interface\n");
  1189. goto err_free;
  1190. }
  1191. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1192. if (ret)
  1193. goto err_unregister;
  1194. lbs_persist_config_init(mesh_dev);
  1195. /* Everything successful */
  1196. ret = 0;
  1197. goto done;
  1198. err_unregister:
  1199. unregister_netdev(mesh_dev);
  1200. err_free:
  1201. free_netdev(mesh_dev);
  1202. done:
  1203. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1204. return ret;
  1205. }
  1206. static void lbs_remove_mesh(struct lbs_private *priv)
  1207. {
  1208. struct net_device *mesh_dev;
  1209. mesh_dev = priv->mesh_dev;
  1210. if (!mesh_dev)
  1211. return;
  1212. lbs_deb_enter(LBS_DEB_MESH);
  1213. netif_stop_queue(mesh_dev);
  1214. netif_carrier_off(mesh_dev);
  1215. sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1216. lbs_persist_config_remove(mesh_dev);
  1217. unregister_netdev(mesh_dev);
  1218. priv->mesh_dev = NULL;
  1219. free_netdev(mesh_dev);
  1220. lbs_deb_leave(LBS_DEB_MESH);
  1221. }
  1222. /**
  1223. * @brief This function finds the CFP in
  1224. * region_cfp_table based on region and band parameter.
  1225. *
  1226. * @param region The region code
  1227. * @param band The band
  1228. * @param cfp_no A pointer to CFP number
  1229. * @return A pointer to CFP
  1230. */
  1231. struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
  1232. {
  1233. int i, end;
  1234. lbs_deb_enter(LBS_DEB_MAIN);
  1235. end = ARRAY_SIZE(region_cfp_table);
  1236. for (i = 0; i < end ; i++) {
  1237. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1238. region_cfp_table[i].region);
  1239. if (region_cfp_table[i].region == region) {
  1240. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1241. lbs_deb_leave(LBS_DEB_MAIN);
  1242. return region_cfp_table[i].cfp_BG;
  1243. }
  1244. }
  1245. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1246. return NULL;
  1247. }
  1248. int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
  1249. {
  1250. int ret = 0;
  1251. int i = 0;
  1252. struct chan_freq_power *cfp;
  1253. int cfp_no;
  1254. lbs_deb_enter(LBS_DEB_MAIN);
  1255. memset(priv->region_channel, 0, sizeof(priv->region_channel));
  1256. cfp = lbs_get_region_cfp_table(region, &cfp_no);
  1257. if (cfp != NULL) {
  1258. priv->region_channel[i].nrcfp = cfp_no;
  1259. priv->region_channel[i].CFP = cfp;
  1260. } else {
  1261. lbs_deb_main("wrong region code %#x in band B/G\n",
  1262. region);
  1263. ret = -1;
  1264. goto out;
  1265. }
  1266. priv->region_channel[i].valid = 1;
  1267. priv->region_channel[i].region = region;
  1268. priv->region_channel[i].band = band;
  1269. i++;
  1270. out:
  1271. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1272. return ret;
  1273. }
  1274. void lbs_queue_event(struct lbs_private *priv, u32 event)
  1275. {
  1276. unsigned long flags;
  1277. lbs_deb_enter(LBS_DEB_THREAD);
  1278. spin_lock_irqsave(&priv->driver_lock, flags);
  1279. if (priv->psstate == PS_STATE_SLEEP)
  1280. priv->psstate = PS_STATE_AWAKE;
  1281. __kfifo_put(priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  1282. wake_up_interruptible(&priv->waitq);
  1283. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1284. lbs_deb_leave(LBS_DEB_THREAD);
  1285. }
  1286. EXPORT_SYMBOL_GPL(lbs_queue_event);
  1287. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  1288. {
  1289. lbs_deb_enter(LBS_DEB_THREAD);
  1290. if (priv->psstate == PS_STATE_SLEEP)
  1291. priv->psstate = PS_STATE_AWAKE;
  1292. /* Swap buffers by flipping the response index */
  1293. BUG_ON(resp_idx > 1);
  1294. priv->resp_idx = resp_idx;
  1295. wake_up_interruptible(&priv->waitq);
  1296. lbs_deb_leave(LBS_DEB_THREAD);
  1297. }
  1298. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  1299. static int __init lbs_init_module(void)
  1300. {
  1301. lbs_deb_enter(LBS_DEB_MAIN);
  1302. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  1303. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  1304. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  1305. confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  1306. lbs_debugfs_init();
  1307. lbs_deb_leave(LBS_DEB_MAIN);
  1308. return 0;
  1309. }
  1310. static void __exit lbs_exit_module(void)
  1311. {
  1312. lbs_deb_enter(LBS_DEB_MAIN);
  1313. lbs_debugfs_remove();
  1314. lbs_deb_leave(LBS_DEB_MAIN);
  1315. }
  1316. /*
  1317. * rtap interface support fuctions
  1318. */
  1319. static int lbs_rtap_open(struct net_device *dev)
  1320. {
  1321. /* Yes, _stop_ the queue. Because we don't support injection */
  1322. lbs_deb_enter(LBS_DEB_MAIN);
  1323. netif_carrier_off(dev);
  1324. netif_stop_queue(dev);
  1325. lbs_deb_leave(LBS_DEB_LEAVE);
  1326. return 0;
  1327. }
  1328. static int lbs_rtap_stop(struct net_device *dev)
  1329. {
  1330. lbs_deb_enter(LBS_DEB_MAIN);
  1331. lbs_deb_leave(LBS_DEB_MAIN);
  1332. return 0;
  1333. }
  1334. static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1335. {
  1336. netif_stop_queue(dev);
  1337. return NETDEV_TX_BUSY;
  1338. }
  1339. static void lbs_remove_rtap(struct lbs_private *priv)
  1340. {
  1341. lbs_deb_enter(LBS_DEB_MAIN);
  1342. if (priv->rtap_net_dev == NULL)
  1343. goto out;
  1344. unregister_netdev(priv->rtap_net_dev);
  1345. free_netdev(priv->rtap_net_dev);
  1346. priv->rtap_net_dev = NULL;
  1347. out:
  1348. lbs_deb_leave(LBS_DEB_MAIN);
  1349. }
  1350. static int lbs_add_rtap(struct lbs_private *priv)
  1351. {
  1352. int ret = 0;
  1353. struct net_device *rtap_dev;
  1354. lbs_deb_enter(LBS_DEB_MAIN);
  1355. if (priv->rtap_net_dev) {
  1356. ret = -EPERM;
  1357. goto out;
  1358. }
  1359. rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
  1360. if (rtap_dev == NULL) {
  1361. ret = -ENOMEM;
  1362. goto out;
  1363. }
  1364. memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
  1365. rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1366. rtap_dev->open = lbs_rtap_open;
  1367. rtap_dev->stop = lbs_rtap_stop;
  1368. rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
  1369. rtap_dev->ml_priv = priv;
  1370. SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
  1371. ret = register_netdev(rtap_dev);
  1372. if (ret) {
  1373. free_netdev(rtap_dev);
  1374. goto out;
  1375. }
  1376. priv->rtap_net_dev = rtap_dev;
  1377. out:
  1378. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1379. return ret;
  1380. }
  1381. module_init(lbs_init_module);
  1382. module_exit(lbs_exit_module);
  1383. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1384. MODULE_AUTHOR("Marvell International Ltd.");
  1385. MODULE_LICENSE("GPL");