main.c 42 KB

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