main.c 40 KB

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