main.c 36 KB

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