main.c 39 KB

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