main.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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. lbs_deb_enter(LBS_DEB_NET);
  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. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  360. return ret;
  361. }
  362. /**
  363. * @brief This function closes the mshX interface
  364. *
  365. * @param dev A pointer to net_device structure
  366. * @return 0
  367. */
  368. static int lbs_mesh_stop(struct net_device *dev)
  369. {
  370. struct lbs_private *priv = (struct lbs_private *) (dev->priv);
  371. lbs_deb_enter(LBS_DEB_MESH);
  372. spin_lock_irq(&priv->driver_lock);
  373. priv->mesh_open = 0;
  374. priv->mesh_connect_status = LBS_DISCONNECTED;
  375. netif_stop_queue(dev);
  376. netif_carrier_off(dev);
  377. spin_unlock_irq(&priv->driver_lock);
  378. lbs_deb_leave(LBS_DEB_MESH);
  379. return 0;
  380. }
  381. /**
  382. * @brief This function closes the ethX interface
  383. *
  384. * @param dev A pointer to net_device structure
  385. * @return 0
  386. */
  387. static int lbs_eth_stop(struct net_device *dev)
  388. {
  389. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  390. lbs_deb_enter(LBS_DEB_NET);
  391. spin_lock_irq(&priv->driver_lock);
  392. priv->infra_open = 0;
  393. netif_stop_queue(dev);
  394. spin_unlock_irq(&priv->driver_lock);
  395. lbs_deb_leave(LBS_DEB_NET);
  396. return 0;
  397. }
  398. static void lbs_tx_timeout(struct net_device *dev)
  399. {
  400. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  401. lbs_deb_enter(LBS_DEB_TX);
  402. lbs_pr_err("tx watch dog timeout\n");
  403. dev->trans_start = jiffies;
  404. if (priv->currenttxskb) {
  405. priv->eventcause = 0x01000000;
  406. lbs_send_tx_feedback(priv);
  407. }
  408. /* XX: Shouldn't we also call into the hw-specific driver
  409. to kick it somehow? */
  410. lbs_host_to_card_done(priv);
  411. /* More often than not, this actually happens because the
  412. firmware has crapped itself -- rather than just a very
  413. busy medium. So send a harmless command, and if/when
  414. _that_ times out, we'll kick it in the head. */
  415. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  416. 0, 0, NULL);
  417. lbs_deb_leave(LBS_DEB_TX);
  418. }
  419. void lbs_host_to_card_done(struct lbs_private *priv)
  420. {
  421. unsigned long flags;
  422. lbs_deb_enter(LBS_DEB_THREAD);
  423. spin_lock_irqsave(&priv->driver_lock, flags);
  424. priv->dnld_sent = DNLD_RES_RECEIVED;
  425. /* Wake main thread if commands are pending */
  426. if (!priv->cur_cmd || priv->tx_pending_len > 0)
  427. wake_up_interruptible(&priv->waitq);
  428. spin_unlock_irqrestore(&priv->driver_lock, flags);
  429. lbs_deb_leave(LBS_DEB_THREAD);
  430. }
  431. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  432. /**
  433. * @brief This function returns the network statistics
  434. *
  435. * @param dev A pointer to struct lbs_private structure
  436. * @return A pointer to net_device_stats structure
  437. */
  438. static struct net_device_stats *lbs_get_stats(struct net_device *dev)
  439. {
  440. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  441. lbs_deb_enter(LBS_DEB_NET);
  442. return &priv->stats;
  443. }
  444. static int lbs_set_mac_address(struct net_device *dev, void *addr)
  445. {
  446. int ret = 0;
  447. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  448. struct sockaddr *phwaddr = addr;
  449. lbs_deb_enter(LBS_DEB_NET);
  450. /* In case it was called from the mesh device */
  451. dev = priv->dev ;
  452. memset(priv->current_addr, 0, ETH_ALEN);
  453. /* dev->dev_addr is 8 bytes */
  454. lbs_deb_hex(LBS_DEB_NET, "dev->dev_addr", dev->dev_addr, ETH_ALEN);
  455. lbs_deb_hex(LBS_DEB_NET, "addr", phwaddr->sa_data, ETH_ALEN);
  456. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  457. ret = lbs_prepare_and_send_command(priv, CMD_802_11_MAC_ADDRESS,
  458. CMD_ACT_SET,
  459. CMD_OPTION_WAITFORRSP, 0, NULL);
  460. if (ret) {
  461. lbs_deb_net("set MAC address failed\n");
  462. ret = -1;
  463. goto done;
  464. }
  465. lbs_deb_hex(LBS_DEB_NET, "priv->macaddr", priv->current_addr, ETH_ALEN);
  466. memcpy(dev->dev_addr, priv->current_addr, ETH_ALEN);
  467. if (priv->mesh_dev)
  468. memcpy(priv->mesh_dev->dev_addr, priv->current_addr, ETH_ALEN);
  469. done:
  470. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  471. return ret;
  472. }
  473. static int lbs_copy_multicast_address(struct lbs_private *priv,
  474. struct net_device *dev)
  475. {
  476. int i = 0;
  477. struct dev_mc_list *mcptr = dev->mc_list;
  478. for (i = 0; i < dev->mc_count; i++) {
  479. memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  480. mcptr = mcptr->next;
  481. }
  482. return i;
  483. }
  484. static void lbs_set_multicast_list(struct net_device *dev)
  485. {
  486. struct lbs_private *priv = dev->priv;
  487. int oldpacketfilter;
  488. DECLARE_MAC_BUF(mac);
  489. lbs_deb_enter(LBS_DEB_NET);
  490. oldpacketfilter = priv->currentpacketfilter;
  491. if (dev->flags & IFF_PROMISC) {
  492. lbs_deb_net("enable promiscuous mode\n");
  493. priv->currentpacketfilter |=
  494. CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  495. priv->currentpacketfilter &=
  496. ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  497. CMD_ACT_MAC_MULTICAST_ENABLE);
  498. } else {
  499. /* Multicast */
  500. priv->currentpacketfilter &=
  501. ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  502. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  503. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  504. lbs_deb_net( "enabling all multicast\n");
  505. priv->currentpacketfilter |=
  506. CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  507. priv->currentpacketfilter &=
  508. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  509. } else {
  510. priv->currentpacketfilter &=
  511. ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  512. if (!dev->mc_count) {
  513. lbs_deb_net("no multicast addresses, "
  514. "disabling multicast\n");
  515. priv->currentpacketfilter &=
  516. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  517. } else {
  518. int i;
  519. priv->currentpacketfilter |=
  520. CMD_ACT_MAC_MULTICAST_ENABLE;
  521. priv->nr_of_multicastmacaddr =
  522. lbs_copy_multicast_address(priv, dev);
  523. lbs_deb_net("multicast addresses: %d\n",
  524. dev->mc_count);
  525. for (i = 0; i < dev->mc_count; i++) {
  526. lbs_deb_net("Multicast address %d: %s\n",
  527. i, print_mac(mac,
  528. priv->multicastlist[i]));
  529. }
  530. /* send multicast addresses to firmware */
  531. lbs_prepare_and_send_command(priv,
  532. CMD_MAC_MULTICAST_ADR,
  533. CMD_ACT_SET, 0, 0,
  534. NULL);
  535. }
  536. }
  537. }
  538. if (priv->currentpacketfilter != oldpacketfilter) {
  539. lbs_set_mac_packet_filter(priv);
  540. }
  541. lbs_deb_leave(LBS_DEB_NET);
  542. }
  543. /**
  544. * @brief This function handles the major jobs in the LBS driver.
  545. * It handles all events generated by firmware, RX data received
  546. * from firmware and TX data sent from kernel.
  547. *
  548. * @param data A pointer to lbs_thread structure
  549. * @return 0
  550. */
  551. static int lbs_thread(void *data)
  552. {
  553. struct net_device *dev = data;
  554. struct lbs_private *priv = dev->priv;
  555. wait_queue_t wait;
  556. u8 ireg = 0;
  557. lbs_deb_enter(LBS_DEB_THREAD);
  558. init_waitqueue_entry(&wait, current);
  559. for (;;) {
  560. int shouldsleep;
  561. lbs_deb_thread( "main-thread 111: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  562. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  563. add_wait_queue(&priv->waitq, &wait);
  564. set_current_state(TASK_INTERRUPTIBLE);
  565. spin_lock_irq(&priv->driver_lock);
  566. if (kthread_should_stop())
  567. shouldsleep = 0; /* Bye */
  568. else if (priv->surpriseremoved)
  569. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  570. else if (priv->psstate == PS_STATE_SLEEP)
  571. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  572. else if (priv->intcounter)
  573. shouldsleep = 0; /* Interrupt pending. Deal with it now */
  574. else if (priv->cmd_timed_out)
  575. shouldsleep = 0; /* Command timed out. Recover */
  576. else if (!priv->fw_ready)
  577. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  578. else if (priv->dnld_sent)
  579. shouldsleep = 1; /* Something is en route to the device already */
  580. else if (priv->tx_pending_len > 0)
  581. shouldsleep = 0; /* We've a packet to send */
  582. else if (priv->cur_cmd)
  583. shouldsleep = 1; /* Can't send a command; one already running */
  584. else if (!list_empty(&priv->cmdpendingq))
  585. shouldsleep = 0; /* We have a command to send */
  586. else
  587. shouldsleep = 1; /* No command */
  588. if (shouldsleep) {
  589. lbs_deb_thread("main-thread sleeping... Conn=%d IntC=%d PS_mode=%d PS_State=%d\n",
  590. priv->connect_status, priv->intcounter,
  591. priv->psmode, priv->psstate);
  592. spin_unlock_irq(&priv->driver_lock);
  593. schedule();
  594. } else
  595. spin_unlock_irq(&priv->driver_lock);
  596. lbs_deb_thread("main-thread 222 (waking up): intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  597. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  598. set_current_state(TASK_RUNNING);
  599. remove_wait_queue(&priv->waitq, &wait);
  600. lbs_deb_thread("main-thread 333: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  601. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  602. if (kthread_should_stop()) {
  603. lbs_deb_thread("main-thread: break from main thread\n");
  604. break;
  605. }
  606. if (priv->surpriseremoved) {
  607. lbs_deb_thread("adapter removed; waiting to die...\n");
  608. continue;
  609. }
  610. spin_lock_irq(&priv->driver_lock);
  611. if (priv->intcounter) {
  612. u8 int_status;
  613. priv->intcounter = 0;
  614. int_status = priv->hw_get_int_status(priv, &ireg);
  615. if (int_status) {
  616. lbs_deb_thread("main-thread: reading HOST_INT_STATUS_REG failed\n");
  617. spin_unlock_irq(&priv->driver_lock);
  618. continue;
  619. }
  620. priv->hisregcpy |= ireg;
  621. }
  622. lbs_deb_thread("main-thread 444: intcounter=%d currenttxskb=%p dnld_sent=%d\n",
  623. priv->intcounter, priv->currenttxskb, priv->dnld_sent);
  624. /* command response? */
  625. if (priv->hisregcpy & MRVDRV_CMD_UPLD_RDY) {
  626. lbs_deb_thread("main-thread: cmd response ready\n");
  627. priv->hisregcpy &= ~MRVDRV_CMD_UPLD_RDY;
  628. spin_unlock_irq(&priv->driver_lock);
  629. lbs_process_rx_command(priv);
  630. spin_lock_irq(&priv->driver_lock);
  631. }
  632. if (priv->cmd_timed_out && priv->cur_cmd) {
  633. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  634. if (++priv->nr_retries > 10) {
  635. lbs_pr_info("Excessive timeouts submitting command %x\n",
  636. le16_to_cpu(cmdnode->cmdbuf->command));
  637. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  638. priv->nr_retries = 0;
  639. } else {
  640. priv->cur_cmd = NULL;
  641. lbs_pr_info("requeueing command %x due to timeout (#%d)\n",
  642. le16_to_cpu(cmdnode->cmdbuf->command), priv->nr_retries);
  643. /* Stick it back at the _top_ of the pending queue
  644. for immediate resubmission */
  645. list_add(&cmdnode->list, &priv->cmdpendingq);
  646. }
  647. }
  648. priv->cmd_timed_out = 0;
  649. /* Any Card Event */
  650. if (priv->hisregcpy & MRVDRV_CARDEVENT) {
  651. lbs_deb_thread("main-thread: Card Event Activity\n");
  652. priv->hisregcpy &= ~MRVDRV_CARDEVENT;
  653. if (priv->hw_read_event_cause(priv)) {
  654. lbs_pr_alert("main-thread: hw_read_event_cause failed\n");
  655. spin_unlock_irq(&priv->driver_lock);
  656. continue;
  657. }
  658. spin_unlock_irq(&priv->driver_lock);
  659. lbs_process_event(priv);
  660. } else
  661. spin_unlock_irq(&priv->driver_lock);
  662. if (!priv->fw_ready)
  663. continue;
  664. /* Check if we need to confirm Sleep Request received previously */
  665. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  666. !priv->dnld_sent && !priv->cur_cmd) {
  667. if (priv->connect_status == LBS_CONNECTED) {
  668. lbs_deb_thread("main_thread: PRE_SLEEP--intcounter=%d currenttxskb=%p dnld_sent=%d cur_cmd=%p, confirm now\n",
  669. priv->intcounter, priv->currenttxskb, priv->dnld_sent, priv->cur_cmd);
  670. lbs_ps_confirm_sleep(priv, (u16) priv->psmode);
  671. } else {
  672. /* workaround for firmware sending
  673. * deauth/linkloss event immediately
  674. * after sleep request; remove this
  675. * after firmware fixes it
  676. */
  677. priv->psstate = PS_STATE_AWAKE;
  678. lbs_pr_alert("main-thread: ignore PS_SleepConfirm in non-connected state\n");
  679. }
  680. }
  681. /* The PS state is changed during processing of Sleep Request
  682. * event above
  683. */
  684. if ((priv->psstate == PS_STATE_SLEEP) ||
  685. (priv->psstate == PS_STATE_PRE_SLEEP))
  686. continue;
  687. /* Execute the next command */
  688. if (!priv->dnld_sent && !priv->cur_cmd)
  689. lbs_execute_next_command(priv);
  690. /* Wake-up command waiters which can't sleep in
  691. * lbs_prepare_and_send_command
  692. */
  693. if (!list_empty(&priv->cmdpendingq))
  694. wake_up_all(&priv->cmd_pending);
  695. spin_lock_irq(&priv->driver_lock);
  696. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  697. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  698. priv->tx_pending_buf,
  699. priv->tx_pending_len);
  700. if (ret) {
  701. lbs_deb_tx("host_to_card failed %d\n", ret);
  702. priv->dnld_sent = DNLD_RES_RECEIVED;
  703. }
  704. priv->tx_pending_len = 0;
  705. if (!priv->currenttxskb) {
  706. /* We can wake the queues immediately if we aren't
  707. waiting for TX feedback */
  708. if (priv->connect_status == LBS_CONNECTED)
  709. netif_wake_queue(priv->dev);
  710. if (priv->mesh_dev &&
  711. priv->mesh_connect_status == LBS_CONNECTED)
  712. netif_wake_queue(priv->mesh_dev);
  713. }
  714. }
  715. spin_unlock_irq(&priv->driver_lock);
  716. }
  717. del_timer(&priv->command_timer);
  718. wake_up_all(&priv->cmd_pending);
  719. lbs_deb_leave(LBS_DEB_THREAD);
  720. return 0;
  721. }
  722. static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
  723. struct cmd_header *cmd)
  724. {
  725. lbs_deb_enter(LBS_DEB_FW);
  726. netif_device_detach(priv->dev);
  727. if (priv->mesh_dev)
  728. netif_device_detach(priv->mesh_dev);
  729. priv->fw_ready = 0;
  730. lbs_deb_leave(LBS_DEB_FW);
  731. return 0;
  732. }
  733. int lbs_suspend(struct lbs_private *priv)
  734. {
  735. struct cmd_header cmd;
  736. int ret;
  737. lbs_deb_enter(LBS_DEB_FW);
  738. if (priv->wol_criteria == 0xffffffff) {
  739. lbs_pr_info("Suspend attempt without configuring wake params!\n");
  740. return -EINVAL;
  741. }
  742. memset(&cmd, 0, sizeof(cmd));
  743. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
  744. sizeof(cmd), lbs_suspend_callback, 0);
  745. if (ret)
  746. lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
  747. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  748. return ret;
  749. }
  750. EXPORT_SYMBOL_GPL(lbs_suspend);
  751. int lbs_resume(struct lbs_private *priv)
  752. {
  753. lbs_deb_enter(LBS_DEB_FW);
  754. priv->fw_ready = 1;
  755. /* Firmware doesn't seem to give us RX packets any more
  756. until we send it some command. Might as well update */
  757. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  758. 0, 0, NULL);
  759. netif_device_attach(priv->dev);
  760. if (priv->mesh_dev)
  761. netif_device_attach(priv->mesh_dev);
  762. lbs_deb_leave(LBS_DEB_FW);
  763. return 0;
  764. }
  765. EXPORT_SYMBOL_GPL(lbs_resume);
  766. /**
  767. * @brief This function downloads firmware image, gets
  768. * HW spec from firmware and set basic parameters to
  769. * firmware.
  770. *
  771. * @param priv A pointer to struct lbs_private structure
  772. * @return 0 or -1
  773. */
  774. static int lbs_setup_firmware(struct lbs_private *priv)
  775. {
  776. int ret = -1;
  777. lbs_deb_enter(LBS_DEB_FW);
  778. /*
  779. * Read MAC address from HW
  780. */
  781. memset(priv->current_addr, 0xff, ETH_ALEN);
  782. ret = lbs_update_hw_spec(priv);
  783. if (ret) {
  784. ret = -1;
  785. goto done;
  786. }
  787. lbs_set_mac_packet_filter(priv);
  788. ret = lbs_get_data_rate(priv);
  789. if (ret < 0) {
  790. ret = -1;
  791. goto done;
  792. }
  793. ret = 0;
  794. done:
  795. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  796. return ret;
  797. }
  798. /**
  799. * This function handles the timeout of command sending.
  800. * It will re-send the same command again.
  801. */
  802. static void command_timer_fn(unsigned long data)
  803. {
  804. struct lbs_private *priv = (struct lbs_private *)data;
  805. unsigned long flags;
  806. lbs_deb_enter(LBS_DEB_CMD);
  807. spin_lock_irqsave(&priv->driver_lock, flags);
  808. if (!priv->cur_cmd) {
  809. lbs_pr_info("Command timer expired; no pending command\n");
  810. goto out;
  811. }
  812. lbs_pr_info("Command %x timed out\n", le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  813. priv->cmd_timed_out = 1;
  814. wake_up_interruptible(&priv->waitq);
  815. out:
  816. spin_unlock_irqrestore(&priv->driver_lock, flags);
  817. lbs_deb_leave(LBS_DEB_CMD);
  818. }
  819. static void lbs_sync_channel_worker(struct work_struct *work)
  820. {
  821. struct lbs_private *priv = container_of(work, struct lbs_private,
  822. sync_channel);
  823. lbs_deb_enter(LBS_DEB_MAIN);
  824. if (lbs_update_channel(priv))
  825. lbs_pr_info("Channel synchronization failed.");
  826. lbs_deb_leave(LBS_DEB_MAIN);
  827. }
  828. static int lbs_init_adapter(struct lbs_private *priv)
  829. {
  830. size_t bufsize;
  831. int i, ret = 0;
  832. lbs_deb_enter(LBS_DEB_MAIN);
  833. /* Allocate buffer to store the BSSID list */
  834. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  835. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  836. if (!priv->networks) {
  837. lbs_pr_err("Out of memory allocating beacons\n");
  838. ret = -1;
  839. goto out;
  840. }
  841. /* Initialize scan result lists */
  842. INIT_LIST_HEAD(&priv->network_free_list);
  843. INIT_LIST_HEAD(&priv->network_list);
  844. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  845. list_add_tail(&priv->networks[i].list,
  846. &priv->network_free_list);
  847. }
  848. priv->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++priv->seqnum);
  849. priv->lbs_ps_confirm_sleep.command =
  850. cpu_to_le16(CMD_802_11_PS_MODE);
  851. priv->lbs_ps_confirm_sleep.size =
  852. cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
  853. priv->lbs_ps_confirm_sleep.action =
  854. cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  855. memset(priv->current_addr, 0xff, ETH_ALEN);
  856. priv->connect_status = LBS_DISCONNECTED;
  857. priv->mesh_connect_status = LBS_DISCONNECTED;
  858. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  859. priv->mode = IW_MODE_INFRA;
  860. priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  861. priv->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  862. priv->radioon = RADIO_ON;
  863. priv->auto_rate = 1;
  864. priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  865. priv->psmode = LBS802_11POWERMODECAM;
  866. priv->psstate = PS_STATE_FULL_POWER;
  867. mutex_init(&priv->lock);
  868. setup_timer(&priv->command_timer, command_timer_fn,
  869. (unsigned long)priv);
  870. INIT_LIST_HEAD(&priv->cmdfreeq);
  871. INIT_LIST_HEAD(&priv->cmdpendingq);
  872. spin_lock_init(&priv->driver_lock);
  873. init_waitqueue_head(&priv->cmd_pending);
  874. /* Allocate the command buffers */
  875. if (lbs_allocate_cmd_buffer(priv)) {
  876. lbs_pr_err("Out of memory allocating command buffers\n");
  877. ret = -1;
  878. }
  879. out:
  880. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  881. return ret;
  882. }
  883. static void lbs_free_adapter(struct lbs_private *priv)
  884. {
  885. lbs_deb_enter(LBS_DEB_MAIN);
  886. lbs_free_cmd_buffer(priv);
  887. del_timer(&priv->command_timer);
  888. kfree(priv->networks);
  889. priv->networks = NULL;
  890. lbs_deb_leave(LBS_DEB_MAIN);
  891. }
  892. /**
  893. * @brief This function adds the card. it will probe the
  894. * card, allocate the lbs_priv and initialize the device.
  895. *
  896. * @param card A pointer to card
  897. * @return A pointer to struct lbs_private structure
  898. */
  899. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  900. {
  901. struct net_device *dev = NULL;
  902. struct lbs_private *priv = NULL;
  903. lbs_deb_enter(LBS_DEB_MAIN);
  904. /* Allocate an Ethernet device and register it */
  905. dev = alloc_etherdev(sizeof(struct lbs_private));
  906. if (!dev) {
  907. lbs_pr_err("init ethX device failed\n");
  908. goto done;
  909. }
  910. priv = dev->priv;
  911. if (lbs_init_adapter(priv)) {
  912. lbs_pr_err("failed to initialize adapter structure.\n");
  913. goto err_init_adapter;
  914. }
  915. priv->dev = dev;
  916. priv->card = card;
  917. priv->mesh_open = 0;
  918. priv->infra_open = 0;
  919. /* Setup the OS Interface to our functions */
  920. dev->open = lbs_dev_open;
  921. dev->hard_start_xmit = lbs_hard_start_xmit;
  922. dev->stop = lbs_eth_stop;
  923. dev->set_mac_address = lbs_set_mac_address;
  924. dev->tx_timeout = lbs_tx_timeout;
  925. dev->get_stats = lbs_get_stats;
  926. dev->watchdog_timeo = 5 * HZ;
  927. dev->ethtool_ops = &lbs_ethtool_ops;
  928. #ifdef WIRELESS_EXT
  929. dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
  930. #endif
  931. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  932. dev->set_multicast_list = lbs_set_multicast_list;
  933. SET_NETDEV_DEV(dev, dmdev);
  934. priv->rtap_net_dev = NULL;
  935. lbs_deb_thread("Starting main thread...\n");
  936. init_waitqueue_head(&priv->waitq);
  937. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  938. if (IS_ERR(priv->main_thread)) {
  939. lbs_deb_thread("Error creating main thread.\n");
  940. goto err_init_adapter;
  941. }
  942. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  943. INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
  944. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  945. INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
  946. sprintf(priv->mesh_ssid, "mesh");
  947. priv->mesh_ssid_len = 4;
  948. priv->wol_criteria = 0xffffffff;
  949. priv->wol_gpio = 0xff;
  950. goto done;
  951. err_init_adapter:
  952. lbs_free_adapter(priv);
  953. free_netdev(dev);
  954. priv = NULL;
  955. done:
  956. lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
  957. return priv;
  958. }
  959. EXPORT_SYMBOL_GPL(lbs_add_card);
  960. int lbs_remove_card(struct lbs_private *priv)
  961. {
  962. struct net_device *dev = priv->dev;
  963. union iwreq_data wrqu;
  964. lbs_deb_enter(LBS_DEB_MAIN);
  965. lbs_remove_mesh(priv);
  966. lbs_remove_rtap(priv);
  967. dev = priv->dev;
  968. cancel_delayed_work(&priv->scan_work);
  969. cancel_delayed_work(&priv->assoc_work);
  970. destroy_workqueue(priv->work_thread);
  971. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  972. priv->psmode = LBS802_11POWERMODECAM;
  973. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  974. }
  975. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  976. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  977. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  978. /* Stop the thread servicing the interrupts */
  979. priv->surpriseremoved = 1;
  980. kthread_stop(priv->main_thread);
  981. lbs_free_adapter(priv);
  982. priv->dev = NULL;
  983. free_netdev(dev);
  984. lbs_deb_leave(LBS_DEB_MAIN);
  985. return 0;
  986. }
  987. EXPORT_SYMBOL_GPL(lbs_remove_card);
  988. int lbs_start_card(struct lbs_private *priv)
  989. {
  990. struct net_device *dev = priv->dev;
  991. int ret = -1;
  992. lbs_deb_enter(LBS_DEB_MAIN);
  993. /* poke the firmware */
  994. ret = lbs_setup_firmware(priv);
  995. if (ret)
  996. goto done;
  997. /* init 802.11d */
  998. lbs_init_11d(priv);
  999. if (register_netdev(dev)) {
  1000. lbs_pr_err("cannot register ethX device\n");
  1001. goto done;
  1002. }
  1003. if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
  1004. lbs_pr_err("cannot register lbs_rtap attribute\n");
  1005. lbs_update_channel(priv);
  1006. /* 5.0.16p0 is known to NOT support any mesh */
  1007. if (priv->fwrelease > 0x05001000) {
  1008. /* Enable mesh, if supported, and work out which TLV it uses.
  1009. 0x100 + 291 is an unofficial value used in 5.110.20.pXX
  1010. 0x100 + 37 is the official value used in 5.110.21.pXX
  1011. but we check them in that order because 20.pXX doesn't
  1012. give an error -- it just silently fails. */
  1013. /* 5.110.20.pXX firmware will fail the command if the channel
  1014. doesn't match the existing channel. But only if the TLV
  1015. is correct. If the channel is wrong, _BOTH_ versions will
  1016. give an error to 0x100+291, and allow 0x100+37 to succeed.
  1017. It's just that 5.110.20.pXX will not have done anything
  1018. useful */
  1019. priv->mesh_tlv = 0x100 + 291;
  1020. if (lbs_mesh_config(priv, 1, priv->curbssparams.channel)) {
  1021. priv->mesh_tlv = 0x100 + 37;
  1022. if (lbs_mesh_config(priv, 1, priv->curbssparams.channel))
  1023. priv->mesh_tlv = 0;
  1024. }
  1025. if (priv->mesh_tlv) {
  1026. lbs_add_mesh(priv);
  1027. if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
  1028. lbs_pr_err("cannot register lbs_mesh attribute\n");
  1029. }
  1030. }
  1031. lbs_debugfs_init_one(priv, dev);
  1032. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  1033. ret = 0;
  1034. done:
  1035. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1036. return ret;
  1037. }
  1038. EXPORT_SYMBOL_GPL(lbs_start_card);
  1039. int lbs_stop_card(struct lbs_private *priv)
  1040. {
  1041. struct net_device *dev = priv->dev;
  1042. int ret = -1;
  1043. struct cmd_ctrl_node *cmdnode;
  1044. unsigned long flags;
  1045. lbs_deb_enter(LBS_DEB_MAIN);
  1046. netif_stop_queue(priv->dev);
  1047. netif_carrier_off(priv->dev);
  1048. lbs_debugfs_remove_one(priv);
  1049. device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
  1050. if (priv->mesh_tlv)
  1051. device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
  1052. /* Flush pending command nodes */
  1053. spin_lock_irqsave(&priv->driver_lock, flags);
  1054. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  1055. cmdnode->result = -ENOENT;
  1056. cmdnode->cmdwaitqwoken = 1;
  1057. wake_up_interruptible(&cmdnode->cmdwait_q);
  1058. }
  1059. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1060. unregister_netdev(dev);
  1061. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1062. return ret;
  1063. }
  1064. EXPORT_SYMBOL_GPL(lbs_stop_card);
  1065. /**
  1066. * @brief This function adds mshX interface
  1067. *
  1068. * @param priv A pointer to the struct lbs_private structure
  1069. * @return 0 if successful, -X otherwise
  1070. */
  1071. static int lbs_add_mesh(struct lbs_private *priv)
  1072. {
  1073. struct net_device *mesh_dev = NULL;
  1074. int ret = 0;
  1075. lbs_deb_enter(LBS_DEB_MESH);
  1076. /* Allocate a virtual mesh device */
  1077. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  1078. lbs_deb_mesh("init mshX device failed\n");
  1079. ret = -ENOMEM;
  1080. goto done;
  1081. }
  1082. mesh_dev->priv = priv;
  1083. priv->mesh_dev = mesh_dev;
  1084. mesh_dev->open = lbs_dev_open;
  1085. mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
  1086. mesh_dev->stop = lbs_mesh_stop;
  1087. mesh_dev->get_stats = lbs_get_stats;
  1088. mesh_dev->set_mac_address = lbs_set_mac_address;
  1089. mesh_dev->ethtool_ops = &lbs_ethtool_ops;
  1090. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  1091. sizeof(priv->dev->dev_addr));
  1092. SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
  1093. #ifdef WIRELESS_EXT
  1094. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1095. #endif
  1096. /* Register virtual mesh interface */
  1097. ret = register_netdev(mesh_dev);
  1098. if (ret) {
  1099. lbs_pr_err("cannot register mshX virtual interface\n");
  1100. goto err_free;
  1101. }
  1102. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1103. if (ret)
  1104. goto err_unregister;
  1105. /* Everything successful */
  1106. ret = 0;
  1107. goto done;
  1108. err_unregister:
  1109. unregister_netdev(mesh_dev);
  1110. err_free:
  1111. free_netdev(mesh_dev);
  1112. done:
  1113. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1114. return ret;
  1115. }
  1116. static void lbs_remove_mesh(struct lbs_private *priv)
  1117. {
  1118. struct net_device *mesh_dev;
  1119. mesh_dev = priv->mesh_dev;
  1120. if (!mesh_dev)
  1121. return;
  1122. lbs_deb_enter(LBS_DEB_MESH);
  1123. netif_stop_queue(mesh_dev);
  1124. netif_carrier_off(priv->mesh_dev);
  1125. sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1126. unregister_netdev(mesh_dev);
  1127. priv->mesh_dev = NULL;
  1128. free_netdev(mesh_dev);
  1129. lbs_deb_leave(LBS_DEB_MESH);
  1130. }
  1131. /**
  1132. * @brief This function finds the CFP in
  1133. * region_cfp_table based on region and band parameter.
  1134. *
  1135. * @param region The region code
  1136. * @param band The band
  1137. * @param cfp_no A pointer to CFP number
  1138. * @return A pointer to CFP
  1139. */
  1140. struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  1141. {
  1142. int i, end;
  1143. lbs_deb_enter(LBS_DEB_MAIN);
  1144. end = ARRAY_SIZE(region_cfp_table);
  1145. for (i = 0; i < end ; i++) {
  1146. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1147. region_cfp_table[i].region);
  1148. if (region_cfp_table[i].region == region) {
  1149. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1150. lbs_deb_leave(LBS_DEB_MAIN);
  1151. return region_cfp_table[i].cfp_BG;
  1152. }
  1153. }
  1154. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1155. return NULL;
  1156. }
  1157. int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
  1158. {
  1159. int ret = 0;
  1160. int i = 0;
  1161. struct chan_freq_power *cfp;
  1162. int cfp_no;
  1163. lbs_deb_enter(LBS_DEB_MAIN);
  1164. memset(priv->region_channel, 0, sizeof(priv->region_channel));
  1165. cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
  1166. if (cfp != NULL) {
  1167. priv->region_channel[i].nrcfp = cfp_no;
  1168. priv->region_channel[i].CFP = cfp;
  1169. } else {
  1170. lbs_deb_main("wrong region code %#x in band B/G\n",
  1171. region);
  1172. ret = -1;
  1173. goto out;
  1174. }
  1175. priv->region_channel[i].valid = 1;
  1176. priv->region_channel[i].region = region;
  1177. priv->region_channel[i].band = band;
  1178. i++;
  1179. out:
  1180. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1181. return ret;
  1182. }
  1183. /**
  1184. * @brief This function handles the interrupt. it will change PS
  1185. * state if applicable. it will wake up main_thread to handle
  1186. * the interrupt event as well.
  1187. *
  1188. * @param dev A pointer to net_device structure
  1189. * @return n/a
  1190. */
  1191. void lbs_interrupt(struct lbs_private *priv)
  1192. {
  1193. lbs_deb_enter(LBS_DEB_THREAD);
  1194. lbs_deb_thread("lbs_interrupt: intcounter=%d\n", priv->intcounter);
  1195. priv->intcounter++;
  1196. if (priv->psstate == PS_STATE_SLEEP)
  1197. priv->psstate = PS_STATE_AWAKE;
  1198. wake_up_interruptible(&priv->waitq);
  1199. lbs_deb_leave(LBS_DEB_THREAD);
  1200. }
  1201. EXPORT_SYMBOL_GPL(lbs_interrupt);
  1202. static int __init lbs_init_module(void)
  1203. {
  1204. lbs_deb_enter(LBS_DEB_MAIN);
  1205. lbs_debugfs_init();
  1206. lbs_deb_leave(LBS_DEB_MAIN);
  1207. return 0;
  1208. }
  1209. static void __exit lbs_exit_module(void)
  1210. {
  1211. lbs_deb_enter(LBS_DEB_MAIN);
  1212. lbs_debugfs_remove();
  1213. lbs_deb_leave(LBS_DEB_MAIN);
  1214. }
  1215. /*
  1216. * rtap interface support fuctions
  1217. */
  1218. static int lbs_rtap_open(struct net_device *dev)
  1219. {
  1220. /* Yes, _stop_ the queue. Because we don't support injection */
  1221. lbs_deb_enter(LBS_DEB_MAIN);
  1222. netif_carrier_off(dev);
  1223. netif_stop_queue(dev);
  1224. lbs_deb_leave(LBS_DEB_LEAVE);
  1225. return 0;
  1226. }
  1227. static int lbs_rtap_stop(struct net_device *dev)
  1228. {
  1229. lbs_deb_enter(LBS_DEB_MAIN);
  1230. lbs_deb_leave(LBS_DEB_MAIN);
  1231. return 0;
  1232. }
  1233. static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1234. {
  1235. netif_stop_queue(dev);
  1236. return NETDEV_TX_BUSY;
  1237. }
  1238. static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
  1239. {
  1240. struct lbs_private *priv = dev->priv;
  1241. lbs_deb_enter(LBS_DEB_NET);
  1242. return &priv->stats;
  1243. }
  1244. static void lbs_remove_rtap(struct lbs_private *priv)
  1245. {
  1246. lbs_deb_enter(LBS_DEB_MAIN);
  1247. if (priv->rtap_net_dev == NULL)
  1248. return;
  1249. unregister_netdev(priv->rtap_net_dev);
  1250. free_netdev(priv->rtap_net_dev);
  1251. priv->rtap_net_dev = NULL;
  1252. lbs_deb_leave(LBS_DEB_MAIN);
  1253. }
  1254. static int lbs_add_rtap(struct lbs_private *priv)
  1255. {
  1256. int ret = 0;
  1257. struct net_device *rtap_dev;
  1258. lbs_deb_enter(LBS_DEB_MAIN);
  1259. if (priv->rtap_net_dev) {
  1260. ret = -EPERM;
  1261. goto out;
  1262. }
  1263. rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
  1264. if (rtap_dev == NULL) {
  1265. ret = -ENOMEM;
  1266. goto out;
  1267. }
  1268. memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
  1269. rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1270. rtap_dev->open = lbs_rtap_open;
  1271. rtap_dev->stop = lbs_rtap_stop;
  1272. rtap_dev->get_stats = lbs_rtap_get_stats;
  1273. rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
  1274. rtap_dev->set_multicast_list = lbs_set_multicast_list;
  1275. rtap_dev->priv = priv;
  1276. ret = register_netdev(rtap_dev);
  1277. if (ret) {
  1278. free_netdev(rtap_dev);
  1279. goto out;
  1280. }
  1281. priv->rtap_net_dev = rtap_dev;
  1282. out:
  1283. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1284. return ret;
  1285. }
  1286. module_init(lbs_init_module);
  1287. module_exit(lbs_exit_module);
  1288. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1289. MODULE_AUTHOR("Marvell International Ltd.");
  1290. MODULE_LICENSE("GPL");