main.c 37 KB

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