main.c 35 KB

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