main.c 39 KB

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