main.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  1. /**
  2. * This file contains the major functions in WLAN
  3. * driver. It includes init, exit, open, close and main
  4. * thread etc..
  5. */
  6. #include <linux/moduleparam.h>
  7. #include <linux/delay.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/if_arp.h>
  11. #include <linux/kthread.h>
  12. #include <linux/kfifo.h>
  13. #include <net/iw_handler.h>
  14. #include <net/ieee80211.h>
  15. #include "host.h"
  16. #include "decl.h"
  17. #include "dev.h"
  18. #include "wext.h"
  19. #include "debugfs.h"
  20. #include "scan.h"
  21. #include "assoc.h"
  22. #include "cmd.h"
  23. #define DRIVER_RELEASE_VERSION "323.p0"
  24. const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
  25. #ifdef DEBUG
  26. "-dbg"
  27. #endif
  28. "";
  29. /* Module parameters */
  30. unsigned int lbs_debug;
  31. EXPORT_SYMBOL_GPL(lbs_debug);
  32. module_param_named(libertas_debug, lbs_debug, int, 0644);
  33. /* This global structure is used to send the confirm_sleep command as
  34. * fast as possible down to the firmware. */
  35. struct cmd_confirm_sleep confirm_sleep;
  36. #define LBS_TX_PWR_DEFAULT 20 /*100mW */
  37. #define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
  38. #define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
  39. #define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
  40. #define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
  41. /* Format { channel, frequency (MHz), maxtxpower } */
  42. /* band: 'B/G', region: USA FCC/Canada IC */
  43. static struct chan_freq_power channel_freq_power_US_BG[] = {
  44. {1, 2412, LBS_TX_PWR_US_DEFAULT},
  45. {2, 2417, LBS_TX_PWR_US_DEFAULT},
  46. {3, 2422, LBS_TX_PWR_US_DEFAULT},
  47. {4, 2427, LBS_TX_PWR_US_DEFAULT},
  48. {5, 2432, LBS_TX_PWR_US_DEFAULT},
  49. {6, 2437, LBS_TX_PWR_US_DEFAULT},
  50. {7, 2442, LBS_TX_PWR_US_DEFAULT},
  51. {8, 2447, LBS_TX_PWR_US_DEFAULT},
  52. {9, 2452, LBS_TX_PWR_US_DEFAULT},
  53. {10, 2457, LBS_TX_PWR_US_DEFAULT},
  54. {11, 2462, LBS_TX_PWR_US_DEFAULT}
  55. };
  56. /* band: 'B/G', region: Europe ETSI */
  57. static struct chan_freq_power channel_freq_power_EU_BG[] = {
  58. {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
  59. {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
  60. {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
  61. {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
  62. {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
  63. {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
  64. {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
  65. {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
  66. {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
  67. {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
  68. {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
  69. {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
  70. {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
  71. };
  72. /* band: 'B/G', region: Spain */
  73. static struct chan_freq_power channel_freq_power_SPN_BG[] = {
  74. {10, 2457, LBS_TX_PWR_DEFAULT},
  75. {11, 2462, LBS_TX_PWR_DEFAULT}
  76. };
  77. /* band: 'B/G', region: France */
  78. static struct chan_freq_power channel_freq_power_FR_BG[] = {
  79. {10, 2457, LBS_TX_PWR_FR_DEFAULT},
  80. {11, 2462, LBS_TX_PWR_FR_DEFAULT},
  81. {12, 2467, LBS_TX_PWR_FR_DEFAULT},
  82. {13, 2472, LBS_TX_PWR_FR_DEFAULT}
  83. };
  84. /* band: 'B/G', region: Japan */
  85. static struct chan_freq_power channel_freq_power_JPN_BG[] = {
  86. {1, 2412, LBS_TX_PWR_JP_DEFAULT},
  87. {2, 2417, LBS_TX_PWR_JP_DEFAULT},
  88. {3, 2422, LBS_TX_PWR_JP_DEFAULT},
  89. {4, 2427, LBS_TX_PWR_JP_DEFAULT},
  90. {5, 2432, LBS_TX_PWR_JP_DEFAULT},
  91. {6, 2437, LBS_TX_PWR_JP_DEFAULT},
  92. {7, 2442, LBS_TX_PWR_JP_DEFAULT},
  93. {8, 2447, LBS_TX_PWR_JP_DEFAULT},
  94. {9, 2452, LBS_TX_PWR_JP_DEFAULT},
  95. {10, 2457, LBS_TX_PWR_JP_DEFAULT},
  96. {11, 2462, LBS_TX_PWR_JP_DEFAULT},
  97. {12, 2467, LBS_TX_PWR_JP_DEFAULT},
  98. {13, 2472, LBS_TX_PWR_JP_DEFAULT},
  99. {14, 2484, LBS_TX_PWR_JP_DEFAULT}
  100. };
  101. /**
  102. * the structure for channel, frequency and power
  103. */
  104. struct region_cfp_table {
  105. u8 region;
  106. struct chan_freq_power *cfp_BG;
  107. int cfp_no_BG;
  108. };
  109. /**
  110. * the structure for the mapping between region and CFP
  111. */
  112. static struct region_cfp_table region_cfp_table[] = {
  113. {0x10, /*US FCC */
  114. channel_freq_power_US_BG,
  115. ARRAY_SIZE(channel_freq_power_US_BG),
  116. }
  117. ,
  118. {0x20, /*CANADA IC */
  119. channel_freq_power_US_BG,
  120. ARRAY_SIZE(channel_freq_power_US_BG),
  121. }
  122. ,
  123. {0x30, /*EU*/ channel_freq_power_EU_BG,
  124. ARRAY_SIZE(channel_freq_power_EU_BG),
  125. }
  126. ,
  127. {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
  128. ARRAY_SIZE(channel_freq_power_SPN_BG),
  129. }
  130. ,
  131. {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
  132. ARRAY_SIZE(channel_freq_power_FR_BG),
  133. }
  134. ,
  135. {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
  136. ARRAY_SIZE(channel_freq_power_JPN_BG),
  137. }
  138. ,
  139. /*Add new region here */
  140. };
  141. /**
  142. * the table to keep region code
  143. */
  144. u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  145. { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
  146. /**
  147. * 802.11b/g supported bitrates (in 500Kb/s units)
  148. */
  149. u8 lbs_bg_rates[MAX_RATES] =
  150. { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
  151. 0x00, 0x00 };
  152. /**
  153. * FW rate table. FW refers to rates by their index in this table, not by the
  154. * rate value itself. Values of 0x00 are
  155. * reserved positions.
  156. */
  157. static u8 fw_data_rates[MAX_RATES] =
  158. { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
  159. 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
  160. };
  161. /**
  162. * @brief use index to get the data rate
  163. *
  164. * @param idx The index of data rate
  165. * @return data rate or 0
  166. */
  167. u32 lbs_fw_index_to_data_rate(u8 idx)
  168. {
  169. if (idx >= sizeof(fw_data_rates))
  170. idx = 0;
  171. return fw_data_rates[idx];
  172. }
  173. /**
  174. * @brief use rate to get the index
  175. *
  176. * @param rate data rate
  177. * @return index or 0
  178. */
  179. u8 lbs_data_rate_to_fw_index(u32 rate)
  180. {
  181. u8 i;
  182. if (!rate)
  183. return 0;
  184. for (i = 0; i < sizeof(fw_data_rates); i++) {
  185. if (rate == fw_data_rates[i])
  186. return i;
  187. }
  188. return 0;
  189. }
  190. /**
  191. * Attributes exported through sysfs
  192. */
  193. /**
  194. * @brief Get function for sysfs attribute anycast_mask
  195. */
  196. static ssize_t lbs_anycast_get(struct device *dev,
  197. struct device_attribute *attr, char * buf)
  198. {
  199. struct lbs_private *priv = to_net_dev(dev)->priv;
  200. struct cmd_ds_mesh_access mesh_access;
  201. int ret;
  202. memset(&mesh_access, 0, sizeof(mesh_access));
  203. ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
  204. if (ret)
  205. return ret;
  206. return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
  207. }
  208. /**
  209. * @brief Set function for sysfs attribute anycast_mask
  210. */
  211. static ssize_t lbs_anycast_set(struct device *dev,
  212. struct device_attribute *attr, const char * buf, size_t count)
  213. {
  214. struct lbs_private *priv = to_net_dev(dev)->priv;
  215. struct cmd_ds_mesh_access mesh_access;
  216. uint32_t datum;
  217. int ret;
  218. memset(&mesh_access, 0, sizeof(mesh_access));
  219. sscanf(buf, "%x", &datum);
  220. mesh_access.data[0] = cpu_to_le32(datum);
  221. ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
  222. if (ret)
  223. return ret;
  224. return strlen(buf);
  225. }
  226. static int lbs_add_rtap(struct lbs_private *priv);
  227. static void lbs_remove_rtap(struct lbs_private *priv);
  228. static int lbs_add_mesh(struct lbs_private *priv);
  229. static void lbs_remove_mesh(struct lbs_private *priv);
  230. /**
  231. * Get function for sysfs attribute rtap
  232. */
  233. static ssize_t lbs_rtap_get(struct device *dev,
  234. struct device_attribute *attr, char * buf)
  235. {
  236. struct lbs_private *priv = to_net_dev(dev)->priv;
  237. return snprintf(buf, 5, "0x%X\n", priv->monitormode);
  238. }
  239. /**
  240. * Set function for sysfs attribute rtap
  241. */
  242. static ssize_t lbs_rtap_set(struct device *dev,
  243. struct device_attribute *attr, const char * buf, size_t count)
  244. {
  245. int monitor_mode;
  246. struct lbs_private *priv = to_net_dev(dev)->priv;
  247. sscanf(buf, "%x", &monitor_mode);
  248. if (monitor_mode) {
  249. if (priv->monitormode == monitor_mode)
  250. return strlen(buf);
  251. if (!priv->monitormode) {
  252. if (priv->infra_open || priv->mesh_open)
  253. return -EBUSY;
  254. if (priv->mode == IW_MODE_INFRA)
  255. lbs_send_deauthentication(priv);
  256. else if (priv->mode == IW_MODE_ADHOC)
  257. lbs_stop_adhoc_network(priv);
  258. lbs_add_rtap(priv);
  259. }
  260. priv->monitormode = monitor_mode;
  261. }
  262. else {
  263. if (!priv->monitormode)
  264. return strlen(buf);
  265. priv->monitormode = 0;
  266. lbs_remove_rtap(priv);
  267. if (priv->currenttxskb) {
  268. dev_kfree_skb_any(priv->currenttxskb);
  269. priv->currenttxskb = NULL;
  270. }
  271. /* Wake queues, command thread, etc. */
  272. lbs_host_to_card_done(priv);
  273. }
  274. lbs_prepare_and_send_command(priv,
  275. CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
  276. CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
  277. return strlen(buf);
  278. }
  279. /**
  280. * lbs_rtap attribute to be exported per ethX interface
  281. * through sysfs (/sys/class/net/ethX/lbs_rtap)
  282. */
  283. static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
  284. /**
  285. * Get function for sysfs attribute mesh
  286. */
  287. static ssize_t lbs_mesh_get(struct device *dev,
  288. struct device_attribute *attr, char * buf)
  289. {
  290. struct lbs_private *priv = to_net_dev(dev)->priv;
  291. return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
  292. }
  293. /**
  294. * Set function for sysfs attribute mesh
  295. */
  296. static ssize_t lbs_mesh_set(struct device *dev,
  297. struct device_attribute *attr, const char * buf, size_t count)
  298. {
  299. struct lbs_private *priv = to_net_dev(dev)->priv;
  300. int enable;
  301. int ret;
  302. sscanf(buf, "%x", &enable);
  303. enable = !!enable;
  304. if (enable == !!priv->mesh_dev)
  305. return count;
  306. ret = lbs_mesh_config(priv, enable, priv->curbssparams.channel);
  307. if (ret)
  308. return ret;
  309. if (enable)
  310. lbs_add_mesh(priv);
  311. else
  312. lbs_remove_mesh(priv);
  313. return count;
  314. }
  315. /**
  316. * lbs_mesh attribute to be exported per ethX interface
  317. * through sysfs (/sys/class/net/ethX/lbs_mesh)
  318. */
  319. static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
  320. /**
  321. * anycast_mask attribute to be exported per mshX interface
  322. * through sysfs (/sys/class/net/mshX/anycast_mask)
  323. */
  324. static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
  325. static struct attribute *lbs_mesh_sysfs_entries[] = {
  326. &dev_attr_anycast_mask.attr,
  327. NULL,
  328. };
  329. static struct attribute_group lbs_mesh_attr_group = {
  330. .attrs = lbs_mesh_sysfs_entries,
  331. };
  332. /**
  333. * @brief This function opens the ethX or mshX interface
  334. *
  335. * @param dev A pointer to net_device structure
  336. * @return 0 or -EBUSY if monitor mode active
  337. */
  338. static int lbs_dev_open(struct net_device *dev)
  339. {
  340. struct lbs_private *priv = (struct lbs_private *) dev->priv ;
  341. int ret = 0;
  342. lbs_deb_enter(LBS_DEB_NET);
  343. spin_lock_irq(&priv->driver_lock);
  344. if (priv->monitormode) {
  345. ret = -EBUSY;
  346. goto out;
  347. }
  348. if (dev == priv->mesh_dev) {
  349. priv->mesh_open = 1;
  350. priv->mesh_connect_status = LBS_CONNECTED;
  351. netif_carrier_on(dev);
  352. } else {
  353. priv->infra_open = 1;
  354. if (priv->connect_status == LBS_CONNECTED)
  355. netif_carrier_on(dev);
  356. else
  357. netif_carrier_off(dev);
  358. }
  359. if (!priv->tx_pending_len)
  360. netif_wake_queue(dev);
  361. out:
  362. spin_unlock_irq(&priv->driver_lock);
  363. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  364. return ret;
  365. }
  366. /**
  367. * @brief This function closes the mshX interface
  368. *
  369. * @param dev A pointer to net_device structure
  370. * @return 0
  371. */
  372. static int lbs_mesh_stop(struct net_device *dev)
  373. {
  374. struct lbs_private *priv = (struct lbs_private *) (dev->priv);
  375. lbs_deb_enter(LBS_DEB_MESH);
  376. spin_lock_irq(&priv->driver_lock);
  377. priv->mesh_open = 0;
  378. priv->mesh_connect_status = LBS_DISCONNECTED;
  379. netif_stop_queue(dev);
  380. netif_carrier_off(dev);
  381. spin_unlock_irq(&priv->driver_lock);
  382. lbs_deb_leave(LBS_DEB_MESH);
  383. return 0;
  384. }
  385. /**
  386. * @brief This function closes the ethX interface
  387. *
  388. * @param dev A pointer to net_device structure
  389. * @return 0
  390. */
  391. static int lbs_eth_stop(struct net_device *dev)
  392. {
  393. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  394. lbs_deb_enter(LBS_DEB_NET);
  395. spin_lock_irq(&priv->driver_lock);
  396. priv->infra_open = 0;
  397. netif_stop_queue(dev);
  398. spin_unlock_irq(&priv->driver_lock);
  399. lbs_deb_leave(LBS_DEB_NET);
  400. return 0;
  401. }
  402. static void lbs_tx_timeout(struct net_device *dev)
  403. {
  404. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  405. lbs_deb_enter(LBS_DEB_TX);
  406. lbs_pr_err("tx watch dog timeout\n");
  407. dev->trans_start = jiffies;
  408. if (priv->currenttxskb)
  409. lbs_send_tx_feedback(priv, 0);
  410. /* XX: Shouldn't we also call into the hw-specific driver
  411. to kick it somehow? */
  412. lbs_host_to_card_done(priv);
  413. /* More often than not, this actually happens because the
  414. firmware has crapped itself -- rather than just a very
  415. busy medium. So send a harmless command, and if/when
  416. _that_ times out, we'll kick it in the head. */
  417. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  418. 0, 0, NULL);
  419. lbs_deb_leave(LBS_DEB_TX);
  420. }
  421. void lbs_host_to_card_done(struct lbs_private *priv)
  422. {
  423. unsigned long flags;
  424. lbs_deb_enter(LBS_DEB_THREAD);
  425. spin_lock_irqsave(&priv->driver_lock, flags);
  426. priv->dnld_sent = DNLD_RES_RECEIVED;
  427. /* Wake main thread if commands are pending */
  428. if (!priv->cur_cmd || priv->tx_pending_len > 0)
  429. wake_up_interruptible(&priv->waitq);
  430. spin_unlock_irqrestore(&priv->driver_lock, flags);
  431. lbs_deb_leave(LBS_DEB_THREAD);
  432. }
  433. EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
  434. /**
  435. * @brief This function returns the network statistics
  436. *
  437. * @param dev A pointer to struct lbs_private structure
  438. * @return A pointer to net_device_stats structure
  439. */
  440. static struct net_device_stats *lbs_get_stats(struct net_device *dev)
  441. {
  442. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  443. lbs_deb_enter(LBS_DEB_NET);
  444. return &priv->stats;
  445. }
  446. static int lbs_set_mac_address(struct net_device *dev, void *addr)
  447. {
  448. int ret = 0;
  449. struct lbs_private *priv = (struct lbs_private *) dev->priv;
  450. struct sockaddr *phwaddr = addr;
  451. struct cmd_ds_802_11_mac_address cmd;
  452. lbs_deb_enter(LBS_DEB_NET);
  453. /* In case it was called from the mesh device */
  454. dev = priv->dev;
  455. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  456. cmd.action = cpu_to_le16(CMD_ACT_SET);
  457. memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
  458. ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
  459. if (ret) {
  460. lbs_deb_net("set MAC address failed\n");
  461. goto done;
  462. }
  463. memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
  464. memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  465. if (priv->mesh_dev)
  466. memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
  467. done:
  468. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  469. return ret;
  470. }
  471. static int lbs_copy_multicast_address(struct lbs_private *priv,
  472. struct net_device *dev)
  473. {
  474. int i = 0;
  475. struct dev_mc_list *mcptr = dev->mc_list;
  476. for (i = 0; i < dev->mc_count; i++) {
  477. memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
  478. mcptr = mcptr->next;
  479. }
  480. return i;
  481. }
  482. static void lbs_set_multicast_list(struct net_device *dev)
  483. {
  484. struct lbs_private *priv = dev->priv;
  485. int old_mac_control;
  486. DECLARE_MAC_BUF(mac);
  487. lbs_deb_enter(LBS_DEB_NET);
  488. old_mac_control = priv->mac_control;
  489. if (dev->flags & IFF_PROMISC) {
  490. lbs_deb_net("enable promiscuous mode\n");
  491. priv->mac_control |=
  492. CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  493. priv->mac_control &=
  494. ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
  495. CMD_ACT_MAC_MULTICAST_ENABLE);
  496. } else {
  497. /* Multicast */
  498. priv->mac_control &=
  499. ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
  500. if (dev->flags & IFF_ALLMULTI || dev->mc_count >
  501. MRVDRV_MAX_MULTICAST_LIST_SIZE) {
  502. lbs_deb_net( "enabling all multicast\n");
  503. priv->mac_control |=
  504. CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  505. priv->mac_control &=
  506. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  507. } else {
  508. priv->mac_control &=
  509. ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
  510. if (!dev->mc_count) {
  511. lbs_deb_net("no multicast addresses, "
  512. "disabling multicast\n");
  513. priv->mac_control &=
  514. ~CMD_ACT_MAC_MULTICAST_ENABLE;
  515. } else {
  516. int i;
  517. priv->mac_control |=
  518. CMD_ACT_MAC_MULTICAST_ENABLE;
  519. priv->nr_of_multicastmacaddr =
  520. lbs_copy_multicast_address(priv, dev);
  521. lbs_deb_net("multicast addresses: %d\n",
  522. dev->mc_count);
  523. for (i = 0; i < dev->mc_count; i++) {
  524. lbs_deb_net("Multicast address %d: %s\n",
  525. i, print_mac(mac,
  526. priv->multicastlist[i]));
  527. }
  528. /* send multicast addresses to firmware */
  529. lbs_prepare_and_send_command(priv,
  530. CMD_MAC_MULTICAST_ADR,
  531. CMD_ACT_SET, 0, 0,
  532. NULL);
  533. }
  534. }
  535. }
  536. if (priv->mac_control != old_mac_control)
  537. lbs_set_mac_control(priv);
  538. lbs_deb_leave(LBS_DEB_NET);
  539. }
  540. /**
  541. * @brief This function handles the major jobs in the LBS driver.
  542. * It handles all events generated by firmware, RX data received
  543. * from firmware and TX data sent from kernel.
  544. *
  545. * @param data A pointer to lbs_thread structure
  546. * @return 0
  547. */
  548. static int lbs_thread(void *data)
  549. {
  550. struct net_device *dev = data;
  551. struct lbs_private *priv = dev->priv;
  552. wait_queue_t wait;
  553. lbs_deb_enter(LBS_DEB_THREAD);
  554. init_waitqueue_entry(&wait, current);
  555. for (;;) {
  556. int shouldsleep;
  557. u8 resp_idx;
  558. lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
  559. priv->currenttxskb, priv->dnld_sent);
  560. add_wait_queue(&priv->waitq, &wait);
  561. set_current_state(TASK_INTERRUPTIBLE);
  562. spin_lock_irq(&priv->driver_lock);
  563. if (kthread_should_stop())
  564. shouldsleep = 0; /* Bye */
  565. else if (priv->surpriseremoved)
  566. shouldsleep = 1; /* We need to wait until we're _told_ to die */
  567. else if (priv->psstate == PS_STATE_SLEEP)
  568. shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
  569. else if (priv->cmd_timed_out)
  570. shouldsleep = 0; /* Command timed out. Recover */
  571. else if (!priv->fw_ready)
  572. shouldsleep = 1; /* Firmware not ready. We're waiting for it */
  573. else if (priv->dnld_sent)
  574. shouldsleep = 1; /* Something is en route to the device already */
  575. else if (priv->tx_pending_len > 0)
  576. shouldsleep = 0; /* We've a packet to send */
  577. else if (priv->cur_cmd)
  578. shouldsleep = 1; /* Can't send a command; one already running */
  579. else if (!list_empty(&priv->cmdpendingq))
  580. shouldsleep = 0; /* We have a command to send */
  581. else if (__kfifo_len(priv->event_fifo))
  582. shouldsleep = 0; /* We have an event to process */
  583. else if (priv->resp_len[priv->resp_idx])
  584. shouldsleep = 0; /* We have a command response */
  585. else
  586. shouldsleep = 1; /* No command */
  587. if (shouldsleep) {
  588. lbs_deb_thread("sleeping, connect_status %d, "
  589. "ps_mode %d, ps_state %d\n",
  590. priv->connect_status,
  591. priv->psmode, priv->psstate);
  592. spin_unlock_irq(&priv->driver_lock);
  593. schedule();
  594. } else
  595. spin_unlock_irq(&priv->driver_lock);
  596. lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
  597. priv->currenttxskb, priv->dnld_sent);
  598. set_current_state(TASK_RUNNING);
  599. remove_wait_queue(&priv->waitq, &wait);
  600. lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
  601. priv->currenttxskb, priv->dnld_sent);
  602. if (kthread_should_stop()) {
  603. lbs_deb_thread("break from main thread\n");
  604. break;
  605. }
  606. if (priv->surpriseremoved) {
  607. lbs_deb_thread("adapter removed; waiting to die...\n");
  608. continue;
  609. }
  610. lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
  611. priv->currenttxskb, priv->dnld_sent);
  612. /* Process any pending command response */
  613. spin_lock_irq(&priv->driver_lock);
  614. resp_idx = priv->resp_idx;
  615. if (priv->resp_len[resp_idx]) {
  616. spin_unlock_irq(&priv->driver_lock);
  617. lbs_process_command_response(priv,
  618. priv->resp_buf[resp_idx],
  619. priv->resp_len[resp_idx]);
  620. spin_lock_irq(&priv->driver_lock);
  621. priv->resp_len[resp_idx] = 0;
  622. }
  623. spin_unlock_irq(&priv->driver_lock);
  624. /* command timeout stuff */
  625. if (priv->cmd_timed_out && priv->cur_cmd) {
  626. struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
  627. if (++priv->nr_retries > 10) {
  628. lbs_pr_info("Excessive timeouts submitting command %x\n",
  629. le16_to_cpu(cmdnode->cmdbuf->command));
  630. lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
  631. priv->nr_retries = 0;
  632. } else {
  633. priv->cur_cmd = NULL;
  634. priv->dnld_sent = DNLD_RES_RECEIVED;
  635. lbs_pr_info("requeueing command %x due to timeout (#%d)\n",
  636. le16_to_cpu(cmdnode->cmdbuf->command), priv->nr_retries);
  637. /* Stick it back at the _top_ of the pending queue
  638. for immediate resubmission */
  639. list_add(&cmdnode->list, &priv->cmdpendingq);
  640. }
  641. }
  642. priv->cmd_timed_out = 0;
  643. /* Process hardware events, e.g. card removed, link lost */
  644. spin_lock_irq(&priv->driver_lock);
  645. while (__kfifo_len(priv->event_fifo)) {
  646. u32 event;
  647. __kfifo_get(priv->event_fifo, (unsigned char *) &event,
  648. sizeof(event));
  649. spin_unlock_irq(&priv->driver_lock);
  650. lbs_process_event(priv, event);
  651. spin_lock_irq(&priv->driver_lock);
  652. }
  653. spin_unlock_irq(&priv->driver_lock);
  654. if (!priv->fw_ready)
  655. continue;
  656. /* Check if we need to confirm Sleep Request received previously */
  657. if (priv->psstate == PS_STATE_PRE_SLEEP &&
  658. !priv->dnld_sent && !priv->cur_cmd) {
  659. if (priv->connect_status == LBS_CONNECTED) {
  660. lbs_deb_thread("pre-sleep, currenttxskb %p, "
  661. "dnld_sent %d, cur_cmd %p\n",
  662. priv->currenttxskb, priv->dnld_sent,
  663. priv->cur_cmd);
  664. lbs_ps_confirm_sleep(priv);
  665. } else {
  666. /* workaround for firmware sending
  667. * deauth/linkloss event immediately
  668. * after sleep request; remove this
  669. * after firmware fixes it
  670. */
  671. priv->psstate = PS_STATE_AWAKE;
  672. lbs_pr_alert("ignore PS_SleepConfirm in "
  673. "non-connected state\n");
  674. }
  675. }
  676. /* The PS state is changed during processing of Sleep Request
  677. * event above
  678. */
  679. if ((priv->psstate == PS_STATE_SLEEP) ||
  680. (priv->psstate == PS_STATE_PRE_SLEEP))
  681. continue;
  682. /* Execute the next command */
  683. if (!priv->dnld_sent && !priv->cur_cmd)
  684. lbs_execute_next_command(priv);
  685. /* Wake-up command waiters which can't sleep in
  686. * lbs_prepare_and_send_command
  687. */
  688. if (!list_empty(&priv->cmdpendingq))
  689. wake_up_all(&priv->cmd_pending);
  690. spin_lock_irq(&priv->driver_lock);
  691. if (!priv->dnld_sent && priv->tx_pending_len > 0) {
  692. int ret = priv->hw_host_to_card(priv, MVMS_DAT,
  693. priv->tx_pending_buf,
  694. priv->tx_pending_len);
  695. if (ret) {
  696. lbs_deb_tx("host_to_card failed %d\n", ret);
  697. priv->dnld_sent = DNLD_RES_RECEIVED;
  698. }
  699. priv->tx_pending_len = 0;
  700. if (!priv->currenttxskb) {
  701. /* We can wake the queues immediately if we aren't
  702. waiting for TX feedback */
  703. if (priv->connect_status == LBS_CONNECTED)
  704. netif_wake_queue(priv->dev);
  705. if (priv->mesh_dev &&
  706. priv->mesh_connect_status == LBS_CONNECTED)
  707. netif_wake_queue(priv->mesh_dev);
  708. }
  709. }
  710. spin_unlock_irq(&priv->driver_lock);
  711. }
  712. del_timer(&priv->command_timer);
  713. wake_up_all(&priv->cmd_pending);
  714. lbs_deb_leave(LBS_DEB_THREAD);
  715. return 0;
  716. }
  717. static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
  718. struct cmd_header *cmd)
  719. {
  720. lbs_deb_enter(LBS_DEB_FW);
  721. netif_device_detach(priv->dev);
  722. if (priv->mesh_dev)
  723. netif_device_detach(priv->mesh_dev);
  724. priv->fw_ready = 0;
  725. lbs_deb_leave(LBS_DEB_FW);
  726. return 0;
  727. }
  728. int lbs_suspend(struct lbs_private *priv)
  729. {
  730. struct cmd_header cmd;
  731. int ret;
  732. lbs_deb_enter(LBS_DEB_FW);
  733. if (priv->wol_criteria == 0xffffffff) {
  734. lbs_pr_info("Suspend attempt without configuring wake params!\n");
  735. return -EINVAL;
  736. }
  737. memset(&cmd, 0, sizeof(cmd));
  738. ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
  739. sizeof(cmd), lbs_suspend_callback, 0);
  740. if (ret)
  741. lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
  742. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  743. return ret;
  744. }
  745. EXPORT_SYMBOL_GPL(lbs_suspend);
  746. int lbs_resume(struct lbs_private *priv)
  747. {
  748. lbs_deb_enter(LBS_DEB_FW);
  749. priv->fw_ready = 1;
  750. /* Firmware doesn't seem to give us RX packets any more
  751. until we send it some command. Might as well update */
  752. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  753. 0, 0, NULL);
  754. netif_device_attach(priv->dev);
  755. if (priv->mesh_dev)
  756. netif_device_attach(priv->mesh_dev);
  757. lbs_deb_leave(LBS_DEB_FW);
  758. return 0;
  759. }
  760. EXPORT_SYMBOL_GPL(lbs_resume);
  761. /**
  762. * @brief This function downloads firmware image, gets
  763. * HW spec from firmware and set basic parameters to
  764. * firmware.
  765. *
  766. * @param priv A pointer to struct lbs_private structure
  767. * @return 0 or -1
  768. */
  769. static int lbs_setup_firmware(struct lbs_private *priv)
  770. {
  771. int ret = -1;
  772. lbs_deb_enter(LBS_DEB_FW);
  773. /*
  774. * Read MAC address from HW
  775. */
  776. memset(priv->current_addr, 0xff, ETH_ALEN);
  777. ret = lbs_update_hw_spec(priv);
  778. if (ret) {
  779. ret = -1;
  780. goto done;
  781. }
  782. lbs_set_mac_control(priv);
  783. ret = lbs_get_data_rate(priv);
  784. if (ret < 0) {
  785. ret = -1;
  786. goto done;
  787. }
  788. ret = 0;
  789. done:
  790. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  791. return ret;
  792. }
  793. /**
  794. * This function handles the timeout of command sending.
  795. * It will re-send the same command again.
  796. */
  797. static void command_timer_fn(unsigned long data)
  798. {
  799. struct lbs_private *priv = (struct lbs_private *)data;
  800. unsigned long flags;
  801. lbs_deb_enter(LBS_DEB_CMD);
  802. spin_lock_irqsave(&priv->driver_lock, flags);
  803. if (!priv->cur_cmd) {
  804. lbs_pr_info("Command timer expired; no pending command\n");
  805. goto out;
  806. }
  807. lbs_pr_info("Command %x timed out\n", le16_to_cpu(priv->cur_cmd->cmdbuf->command));
  808. priv->cmd_timed_out = 1;
  809. wake_up_interruptible(&priv->waitq);
  810. out:
  811. spin_unlock_irqrestore(&priv->driver_lock, flags);
  812. lbs_deb_leave(LBS_DEB_CMD);
  813. }
  814. static void lbs_sync_channel_worker(struct work_struct *work)
  815. {
  816. struct lbs_private *priv = container_of(work, struct lbs_private,
  817. sync_channel);
  818. lbs_deb_enter(LBS_DEB_MAIN);
  819. if (lbs_update_channel(priv))
  820. lbs_pr_info("Channel synchronization failed.");
  821. lbs_deb_leave(LBS_DEB_MAIN);
  822. }
  823. static int lbs_init_adapter(struct lbs_private *priv)
  824. {
  825. size_t bufsize;
  826. int i, ret = 0;
  827. lbs_deb_enter(LBS_DEB_MAIN);
  828. /* Allocate buffer to store the BSSID list */
  829. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  830. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  831. if (!priv->networks) {
  832. lbs_pr_err("Out of memory allocating beacons\n");
  833. ret = -1;
  834. goto out;
  835. }
  836. /* Initialize scan result lists */
  837. INIT_LIST_HEAD(&priv->network_free_list);
  838. INIT_LIST_HEAD(&priv->network_list);
  839. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  840. list_add_tail(&priv->networks[i].list,
  841. &priv->network_free_list);
  842. }
  843. memset(priv->current_addr, 0xff, ETH_ALEN);
  844. priv->connect_status = LBS_DISCONNECTED;
  845. priv->mesh_connect_status = LBS_DISCONNECTED;
  846. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  847. priv->mode = IW_MODE_INFRA;
  848. priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  849. priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  850. priv->radioon = RADIO_ON;
  851. priv->auto_rate = 1;
  852. priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  853. priv->psmode = LBS802_11POWERMODECAM;
  854. priv->psstate = PS_STATE_FULL_POWER;
  855. mutex_init(&priv->lock);
  856. setup_timer(&priv->command_timer, command_timer_fn,
  857. (unsigned long)priv);
  858. INIT_LIST_HEAD(&priv->cmdfreeq);
  859. INIT_LIST_HEAD(&priv->cmdpendingq);
  860. spin_lock_init(&priv->driver_lock);
  861. init_waitqueue_head(&priv->cmd_pending);
  862. /* Allocate the command buffers */
  863. if (lbs_allocate_cmd_buffer(priv)) {
  864. lbs_pr_err("Out of memory allocating command buffers\n");
  865. ret = -ENOMEM;
  866. goto out;
  867. }
  868. priv->resp_idx = 0;
  869. priv->resp_len[0] = priv->resp_len[1] = 0;
  870. /* Create the event FIFO */
  871. priv->event_fifo = kfifo_alloc(sizeof(u32) * 16, GFP_KERNEL, NULL);
  872. if (IS_ERR(priv->event_fifo)) {
  873. lbs_pr_err("Out of memory allocating event FIFO buffer\n");
  874. ret = -ENOMEM;
  875. goto out;
  876. }
  877. out:
  878. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  879. return ret;
  880. }
  881. static void lbs_free_adapter(struct lbs_private *priv)
  882. {
  883. lbs_deb_enter(LBS_DEB_MAIN);
  884. lbs_free_cmd_buffer(priv);
  885. if (priv->event_fifo)
  886. kfifo_free(priv->event_fifo);
  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, 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, &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. void lbs_queue_event(struct lbs_private *priv, u32 event)
  1184. {
  1185. unsigned long flags;
  1186. lbs_deb_enter(LBS_DEB_THREAD);
  1187. spin_lock_irqsave(&priv->driver_lock, flags);
  1188. if (priv->psstate == PS_STATE_SLEEP)
  1189. priv->psstate = PS_STATE_AWAKE;
  1190. __kfifo_put(priv->event_fifo, (unsigned char *) &event, sizeof(u32));
  1191. wake_up_interruptible(&priv->waitq);
  1192. spin_unlock_irqrestore(&priv->driver_lock, flags);
  1193. lbs_deb_leave(LBS_DEB_THREAD);
  1194. }
  1195. EXPORT_SYMBOL_GPL(lbs_queue_event);
  1196. void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
  1197. {
  1198. lbs_deb_enter(LBS_DEB_THREAD);
  1199. if (priv->psstate == PS_STATE_SLEEP)
  1200. priv->psstate = PS_STATE_AWAKE;
  1201. /* Swap buffers by flipping the response index */
  1202. BUG_ON(resp_idx > 1);
  1203. priv->resp_idx = resp_idx;
  1204. wake_up_interruptible(&priv->waitq);
  1205. lbs_deb_leave(LBS_DEB_THREAD);
  1206. }
  1207. EXPORT_SYMBOL_GPL(lbs_notify_command_response);
  1208. static int __init lbs_init_module(void)
  1209. {
  1210. lbs_deb_enter(LBS_DEB_MAIN);
  1211. memset(&confirm_sleep, 0, sizeof(confirm_sleep));
  1212. confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
  1213. confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
  1214. confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  1215. lbs_debugfs_init();
  1216. lbs_deb_leave(LBS_DEB_MAIN);
  1217. return 0;
  1218. }
  1219. static void __exit lbs_exit_module(void)
  1220. {
  1221. lbs_deb_enter(LBS_DEB_MAIN);
  1222. lbs_debugfs_remove();
  1223. lbs_deb_leave(LBS_DEB_MAIN);
  1224. }
  1225. /*
  1226. * rtap interface support fuctions
  1227. */
  1228. static int lbs_rtap_open(struct net_device *dev)
  1229. {
  1230. /* Yes, _stop_ the queue. Because we don't support injection */
  1231. lbs_deb_enter(LBS_DEB_MAIN);
  1232. netif_carrier_off(dev);
  1233. netif_stop_queue(dev);
  1234. lbs_deb_leave(LBS_DEB_LEAVE);
  1235. return 0;
  1236. }
  1237. static int lbs_rtap_stop(struct net_device *dev)
  1238. {
  1239. lbs_deb_enter(LBS_DEB_MAIN);
  1240. lbs_deb_leave(LBS_DEB_MAIN);
  1241. return 0;
  1242. }
  1243. static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1244. {
  1245. netif_stop_queue(dev);
  1246. return NETDEV_TX_BUSY;
  1247. }
  1248. static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
  1249. {
  1250. struct lbs_private *priv = dev->priv;
  1251. lbs_deb_enter(LBS_DEB_NET);
  1252. return &priv->stats;
  1253. }
  1254. static void lbs_remove_rtap(struct lbs_private *priv)
  1255. {
  1256. lbs_deb_enter(LBS_DEB_MAIN);
  1257. if (priv->rtap_net_dev == NULL)
  1258. return;
  1259. unregister_netdev(priv->rtap_net_dev);
  1260. free_netdev(priv->rtap_net_dev);
  1261. priv->rtap_net_dev = NULL;
  1262. lbs_deb_leave(LBS_DEB_MAIN);
  1263. }
  1264. static int lbs_add_rtap(struct lbs_private *priv)
  1265. {
  1266. int ret = 0;
  1267. struct net_device *rtap_dev;
  1268. lbs_deb_enter(LBS_DEB_MAIN);
  1269. if (priv->rtap_net_dev) {
  1270. ret = -EPERM;
  1271. goto out;
  1272. }
  1273. rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
  1274. if (rtap_dev == NULL) {
  1275. ret = -ENOMEM;
  1276. goto out;
  1277. }
  1278. memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
  1279. rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1280. rtap_dev->open = lbs_rtap_open;
  1281. rtap_dev->stop = lbs_rtap_stop;
  1282. rtap_dev->get_stats = lbs_rtap_get_stats;
  1283. rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
  1284. rtap_dev->set_multicast_list = lbs_set_multicast_list;
  1285. rtap_dev->priv = priv;
  1286. SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
  1287. ret = register_netdev(rtap_dev);
  1288. if (ret) {
  1289. free_netdev(rtap_dev);
  1290. goto out;
  1291. }
  1292. priv->rtap_net_dev = rtap_dev;
  1293. out:
  1294. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1295. return ret;
  1296. }
  1297. #ifndef CONFIG_IEEE80211
  1298. const char *escape_essid(const char *essid, u8 essid_len)
  1299. {
  1300. static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
  1301. const char *s = essid;
  1302. char *d = escaped;
  1303. if (ieee80211_is_empty_essid(essid, essid_len)) {
  1304. memcpy(escaped, "<hidden>", sizeof("<hidden>"));
  1305. return escaped;
  1306. }
  1307. essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
  1308. while (essid_len--) {
  1309. if (*s == '\0') {
  1310. *d++ = '\\';
  1311. *d++ = '0';
  1312. s++;
  1313. } else {
  1314. *d++ = *s++;
  1315. }
  1316. }
  1317. *d = '\0';
  1318. return escaped;
  1319. }
  1320. #endif
  1321. module_init(lbs_init_module);
  1322. module_exit(lbs_exit_module);
  1323. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1324. MODULE_AUTHOR("Marvell International Ltd.");
  1325. MODULE_LICENSE("GPL");