main.c 38 KB

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