main.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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 cmd_ds_mesh_access mesh_access;
  197. memset(&mesh_access, 0, sizeof(mesh_access));
  198. lbs_prepare_and_send_command(to_net_dev(dev)->priv,
  199. CMD_MESH_ACCESS,
  200. CMD_ACT_MESH_GET_ANYCAST,
  201. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  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 cmd_ds_mesh_access mesh_access;
  211. uint32_t datum;
  212. memset(&mesh_access, 0, sizeof(mesh_access));
  213. sscanf(buf, "%x", &datum);
  214. mesh_access.data[0] = cpu_to_le32(datum);
  215. lbs_prepare_and_send_command((to_net_dev(dev))->priv,
  216. CMD_MESH_ACCESS,
  217. CMD_ACT_MESH_SET_ANYCAST,
  218. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  219. return strlen(buf);
  220. }
  221. int lbs_add_rtap(struct lbs_private *priv);
  222. void lbs_remove_rtap(struct lbs_private *priv);
  223. /**
  224. * Get function for sysfs attribute rtap
  225. */
  226. static ssize_t lbs_rtap_get(struct device *dev,
  227. struct device_attribute *attr, char * buf)
  228. {
  229. struct lbs_private *priv = to_net_dev(dev)->priv;
  230. return snprintf(buf, 5, "0x%X\n", priv->monitormode);
  231. }
  232. /**
  233. * Set function for sysfs attribute rtap
  234. */
  235. static ssize_t lbs_rtap_set(struct device *dev,
  236. struct device_attribute *attr, const char * buf, size_t count)
  237. {
  238. int monitor_mode;
  239. struct lbs_private *priv = to_net_dev(dev)->priv;
  240. sscanf(buf, "%x", &monitor_mode);
  241. if (monitor_mode != LBS_MONITOR_OFF) {
  242. if(priv->monitormode == monitor_mode)
  243. return strlen(buf);
  244. if (priv->monitormode == LBS_MONITOR_OFF) {
  245. if (priv->infra_open || priv->mesh_open)
  246. return -EBUSY;
  247. if (priv->mode == IW_MODE_INFRA)
  248. lbs_send_deauthentication(priv);
  249. else if (priv->mode == IW_MODE_ADHOC)
  250. lbs_stop_adhoc_network(priv);
  251. lbs_add_rtap(priv);
  252. }
  253. priv->monitormode = monitor_mode;
  254. }
  255. else {
  256. if (priv->monitormode == LBS_MONITOR_OFF)
  257. return strlen(buf);
  258. priv->monitormode = LBS_MONITOR_OFF;
  259. lbs_remove_rtap(priv);
  260. if (priv->currenttxskb) {
  261. dev_kfree_skb_any(priv->currenttxskb);
  262. priv->currenttxskb = NULL;
  263. }
  264. /* Wake queues, command thread, etc. */
  265. lbs_host_to_card_done(priv);
  266. }
  267. lbs_prepare_and_send_command(priv,
  268. CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
  269. CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
  270. return strlen(buf);
  271. }
  272. /**
  273. * lbs_rtap attribute to be exported per mshX interface
  274. * through sysfs (/sys/class/net/mshX/libertas-rtap)
  275. */
  276. static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get,
  277. lbs_rtap_set );
  278. /**
  279. * anycast_mask attribute to be exported per mshX interface
  280. * through sysfs (/sys/class/net/mshX/anycast_mask)
  281. */
  282. static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
  283. static ssize_t lbs_autostart_enabled_get(struct device *dev,
  284. struct device_attribute *attr, char * buf)
  285. {
  286. struct cmd_ds_mesh_access mesh_access;
  287. memset(&mesh_access, 0, sizeof(mesh_access));
  288. lbs_prepare_and_send_command(to_net_dev(dev)->priv,
  289. CMD_MESH_ACCESS,
  290. CMD_ACT_MESH_GET_AUTOSTART_ENABLED,
  291. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  292. return sprintf(buf, "%d\n", le32_to_cpu(mesh_access.data[0]));
  293. }
  294. static ssize_t lbs_autostart_enabled_set(struct device *dev,
  295. struct device_attribute *attr, const char * buf, size_t count)
  296. {
  297. struct cmd_ds_mesh_access mesh_access;
  298. uint32_t datum;
  299. struct lbs_private *priv = (to_net_dev(dev))->priv;
  300. int ret;
  301. memset(&mesh_access, 0, sizeof(mesh_access));
  302. sscanf(buf, "%d", &datum);
  303. mesh_access.data[0] = cpu_to_le32(datum);
  304. ret = lbs_prepare_and_send_command(priv,
  305. CMD_MESH_ACCESS,
  306. CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
  307. CMD_OPTION_WAITFORRSP, 0, (void *)&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. /* Get the supported Data rates */
  712. ret = lbs_prepare_and_send_command(priv, CMD_802_11_DATA_RATE,
  713. CMD_ACT_GET_TX_RATE,
  714. CMD_OPTION_WAITFORRSP, 0, NULL);
  715. if (ret) {
  716. ret = -1;
  717. goto done;
  718. }
  719. /* Disable mesh autostart */
  720. if (priv->mesh_dev) {
  721. memset(&mesh_access, 0, sizeof(mesh_access));
  722. mesh_access.data[0] = cpu_to_le32(0);
  723. ret = lbs_prepare_and_send_command(priv,
  724. CMD_MESH_ACCESS,
  725. CMD_ACT_MESH_SET_AUTOSTART_ENABLED,
  726. CMD_OPTION_WAITFORRSP, 0, (void *)&mesh_access);
  727. if (ret) {
  728. ret = -1;
  729. goto done;
  730. }
  731. priv->mesh_autostart_enabled = 0;
  732. }
  733. ret = 0;
  734. done:
  735. lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
  736. return ret;
  737. }
  738. /**
  739. * This function handles the timeout of command sending.
  740. * It will re-send the same command again.
  741. */
  742. static void command_timer_fn(unsigned long data)
  743. {
  744. struct lbs_private *priv = (struct lbs_private *)data;
  745. struct cmd_ctrl_node *ptempnode;
  746. struct cmd_ds_command *cmd;
  747. unsigned long flags;
  748. ptempnode = priv->cur_cmd;
  749. if (ptempnode == NULL) {
  750. lbs_deb_fw("ptempnode empty\n");
  751. return;
  752. }
  753. cmd = (struct cmd_ds_command *)ptempnode->bufvirtualaddr;
  754. if (!cmd) {
  755. lbs_deb_fw("cmd is NULL\n");
  756. return;
  757. }
  758. lbs_deb_fw("command_timer_fn fired, cmd %x\n", cmd->command);
  759. if (!priv->fw_ready)
  760. return;
  761. spin_lock_irqsave(&priv->driver_lock, flags);
  762. priv->cur_cmd = NULL;
  763. spin_unlock_irqrestore(&priv->driver_lock, flags);
  764. lbs_deb_fw("re-sending same command because of timeout\n");
  765. lbs_queue_cmd(priv, ptempnode, 0);
  766. wake_up_interruptible(&priv->waitq);
  767. return;
  768. }
  769. static int lbs_init_adapter(struct lbs_private *priv)
  770. {
  771. size_t bufsize;
  772. int i, ret = 0;
  773. /* Allocate buffer to store the BSSID list */
  774. bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
  775. priv->networks = kzalloc(bufsize, GFP_KERNEL);
  776. if (!priv->networks) {
  777. lbs_pr_err("Out of memory allocating beacons\n");
  778. ret = -1;
  779. goto out;
  780. }
  781. /* Initialize scan result lists */
  782. INIT_LIST_HEAD(&priv->network_free_list);
  783. INIT_LIST_HEAD(&priv->network_list);
  784. for (i = 0; i < MAX_NETWORK_COUNT; i++) {
  785. list_add_tail(&priv->networks[i].list,
  786. &priv->network_free_list);
  787. }
  788. priv->lbs_ps_confirm_sleep.seqnum = cpu_to_le16(++priv->seqnum);
  789. priv->lbs_ps_confirm_sleep.command =
  790. cpu_to_le16(CMD_802_11_PS_MODE);
  791. priv->lbs_ps_confirm_sleep.size =
  792. cpu_to_le16(sizeof(struct PS_CMD_ConfirmSleep));
  793. priv->lbs_ps_confirm_sleep.action =
  794. cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
  795. memset(priv->current_addr, 0xff, ETH_ALEN);
  796. priv->connect_status = LBS_DISCONNECTED;
  797. priv->mesh_connect_status = LBS_DISCONNECTED;
  798. priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
  799. priv->mode = IW_MODE_INFRA;
  800. priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
  801. priv->currentpacketfilter = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
  802. priv->radioon = RADIO_ON;
  803. priv->auto_rate = 1;
  804. priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
  805. priv->psmode = LBS802_11POWERMODECAM;
  806. priv->psstate = PS_STATE_FULL_POWER;
  807. mutex_init(&priv->lock);
  808. setup_timer(&priv->command_timer, command_timer_fn,
  809. (unsigned long)priv);
  810. INIT_LIST_HEAD(&priv->cmdfreeq);
  811. INIT_LIST_HEAD(&priv->cmdpendingq);
  812. spin_lock_init(&priv->driver_lock);
  813. init_waitqueue_head(&priv->cmd_pending);
  814. /* Allocate the command buffers */
  815. if (lbs_allocate_cmd_buffer(priv)) {
  816. lbs_pr_err("Out of memory allocating command buffers\n");
  817. ret = -1;
  818. }
  819. out:
  820. return ret;
  821. }
  822. static void lbs_free_adapter(struct lbs_private *priv)
  823. {
  824. lbs_deb_fw("free command buffer\n");
  825. lbs_free_cmd_buffer(priv);
  826. lbs_deb_fw("free command_timer\n");
  827. del_timer(&priv->command_timer);
  828. lbs_deb_fw("free scan results table\n");
  829. kfree(priv->networks);
  830. priv->networks = NULL;
  831. }
  832. /**
  833. * @brief This function adds the card. it will probe the
  834. * card, allocate the lbs_priv and initialize the device.
  835. *
  836. * @param card A pointer to card
  837. * @return A pointer to struct lbs_private structure
  838. */
  839. struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
  840. {
  841. struct net_device *dev = NULL;
  842. struct lbs_private *priv = NULL;
  843. lbs_deb_enter(LBS_DEB_NET);
  844. /* Allocate an Ethernet device and register it */
  845. dev = alloc_etherdev(sizeof(struct lbs_private));
  846. if (!dev) {
  847. lbs_pr_err("init ethX device failed\n");
  848. goto done;
  849. }
  850. priv = dev->priv;
  851. if (lbs_init_adapter(priv)) {
  852. lbs_pr_err("failed to initialize adapter structure.\n");
  853. goto err_init_adapter;
  854. }
  855. priv->dev = dev;
  856. priv->card = card;
  857. priv->mesh_open = 0;
  858. priv->infra_open = 0;
  859. /* Setup the OS Interface to our functions */
  860. dev->open = lbs_dev_open;
  861. dev->hard_start_xmit = lbs_hard_start_xmit;
  862. dev->stop = lbs_eth_stop;
  863. dev->set_mac_address = lbs_set_mac_address;
  864. dev->tx_timeout = lbs_tx_timeout;
  865. dev->get_stats = lbs_get_stats;
  866. dev->watchdog_timeo = 5 * HZ;
  867. dev->ethtool_ops = &lbs_ethtool_ops;
  868. #ifdef WIRELESS_EXT
  869. dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
  870. #endif
  871. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  872. dev->set_multicast_list = lbs_set_multicast_list;
  873. SET_NETDEV_DEV(dev, dmdev);
  874. priv->rtap_net_dev = NULL;
  875. lbs_deb_thread("Starting main thread...\n");
  876. init_waitqueue_head(&priv->waitq);
  877. priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
  878. if (IS_ERR(priv->main_thread)) {
  879. lbs_deb_thread("Error creating main thread.\n");
  880. goto err_init_adapter;
  881. }
  882. priv->work_thread = create_singlethread_workqueue("lbs_worker");
  883. INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
  884. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  885. INIT_WORK(&priv->sync_channel, lbs_sync_channel);
  886. goto done;
  887. err_init_adapter:
  888. lbs_free_adapter(priv);
  889. free_netdev(dev);
  890. priv = NULL;
  891. done:
  892. lbs_deb_leave_args(LBS_DEB_NET, "priv %p", priv);
  893. return priv;
  894. }
  895. EXPORT_SYMBOL_GPL(lbs_add_card);
  896. int lbs_remove_card(struct lbs_private *priv)
  897. {
  898. struct net_device *dev = priv->dev;
  899. union iwreq_data wrqu;
  900. lbs_deb_enter(LBS_DEB_MAIN);
  901. lbs_remove_rtap(priv);
  902. dev = priv->dev;
  903. device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
  904. cancel_delayed_work(&priv->scan_work);
  905. cancel_delayed_work(&priv->assoc_work);
  906. destroy_workqueue(priv->work_thread);
  907. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  908. priv->psmode = LBS802_11POWERMODECAM;
  909. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  910. }
  911. memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
  912. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  913. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  914. /* Stop the thread servicing the interrupts */
  915. priv->surpriseremoved = 1;
  916. kthread_stop(priv->main_thread);
  917. lbs_free_adapter(priv);
  918. priv->dev = NULL;
  919. free_netdev(dev);
  920. lbs_deb_leave(LBS_DEB_MAIN);
  921. return 0;
  922. }
  923. EXPORT_SYMBOL_GPL(lbs_remove_card);
  924. int lbs_start_card(struct lbs_private *priv)
  925. {
  926. struct net_device *dev = priv->dev;
  927. int ret = -1;
  928. lbs_deb_enter(LBS_DEB_MAIN);
  929. /* poke the firmware */
  930. ret = lbs_setup_firmware(priv);
  931. if (ret)
  932. goto done;
  933. /* init 802.11d */
  934. lbs_init_11d(priv);
  935. if (register_netdev(dev)) {
  936. lbs_pr_err("cannot register ethX device\n");
  937. goto done;
  938. }
  939. if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
  940. lbs_pr_err("cannot register lbs_rtap attribute\n");
  941. lbs_debugfs_init_one(priv, dev);
  942. lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
  943. ret = 0;
  944. done:
  945. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  946. return ret;
  947. }
  948. EXPORT_SYMBOL_GPL(lbs_start_card);
  949. int lbs_stop_card(struct lbs_private *priv)
  950. {
  951. struct net_device *dev = priv->dev;
  952. int ret = -1;
  953. struct cmd_ctrl_node *cmdnode;
  954. unsigned long flags;
  955. lbs_deb_enter(LBS_DEB_MAIN);
  956. netif_stop_queue(priv->dev);
  957. netif_carrier_off(priv->dev);
  958. lbs_debugfs_remove_one(priv);
  959. /* Flush pending command nodes */
  960. spin_lock_irqsave(&priv->driver_lock, flags);
  961. list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
  962. cmdnode->cmdwaitqwoken = 1;
  963. wake_up_interruptible(&cmdnode->cmdwait_q);
  964. }
  965. spin_unlock_irqrestore(&priv->driver_lock, flags);
  966. unregister_netdev(dev);
  967. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  968. return ret;
  969. }
  970. EXPORT_SYMBOL_GPL(lbs_stop_card);
  971. /**
  972. * @brief This function adds mshX interface
  973. *
  974. * @param priv A pointer to the struct lbs_private structure
  975. * @return 0 if successful, -X otherwise
  976. */
  977. int lbs_add_mesh(struct lbs_private *priv, struct device *dev)
  978. {
  979. struct net_device *mesh_dev = NULL;
  980. int ret = 0;
  981. lbs_deb_enter(LBS_DEB_MESH);
  982. /* Allocate a virtual mesh device */
  983. if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
  984. lbs_deb_mesh("init mshX device failed\n");
  985. ret = -ENOMEM;
  986. goto done;
  987. }
  988. mesh_dev->priv = priv;
  989. priv->mesh_dev = mesh_dev;
  990. mesh_dev->open = lbs_dev_open;
  991. mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
  992. mesh_dev->stop = lbs_mesh_stop;
  993. mesh_dev->get_stats = lbs_get_stats;
  994. mesh_dev->set_mac_address = lbs_set_mac_address;
  995. mesh_dev->ethtool_ops = &lbs_ethtool_ops;
  996. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
  997. sizeof(priv->dev->dev_addr));
  998. SET_NETDEV_DEV(priv->mesh_dev, dev);
  999. #ifdef WIRELESS_EXT
  1000. mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
  1001. #endif
  1002. /* Register virtual mesh interface */
  1003. ret = register_netdev(mesh_dev);
  1004. if (ret) {
  1005. lbs_pr_err("cannot register mshX virtual interface\n");
  1006. goto err_free;
  1007. }
  1008. ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1009. if (ret)
  1010. goto err_unregister;
  1011. /* Everything successful */
  1012. ret = 0;
  1013. goto done;
  1014. err_unregister:
  1015. unregister_netdev(mesh_dev);
  1016. err_free:
  1017. free_netdev(mesh_dev);
  1018. done:
  1019. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  1020. return ret;
  1021. }
  1022. EXPORT_SYMBOL_GPL(lbs_add_mesh);
  1023. void lbs_remove_mesh(struct lbs_private *priv)
  1024. {
  1025. struct net_device *mesh_dev;
  1026. lbs_deb_enter(LBS_DEB_MAIN);
  1027. if (!priv)
  1028. goto out;
  1029. mesh_dev = priv->mesh_dev;
  1030. netif_stop_queue(mesh_dev);
  1031. netif_carrier_off(priv->mesh_dev);
  1032. sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
  1033. unregister_netdev(mesh_dev);
  1034. priv->mesh_dev = NULL ;
  1035. free_netdev(mesh_dev);
  1036. out:
  1037. lbs_deb_leave(LBS_DEB_MAIN);
  1038. }
  1039. EXPORT_SYMBOL_GPL(lbs_remove_mesh);
  1040. /**
  1041. * @brief This function finds the CFP in
  1042. * region_cfp_table based on region and band parameter.
  1043. *
  1044. * @param region The region code
  1045. * @param band The band
  1046. * @param cfp_no A pointer to CFP number
  1047. * @return A pointer to CFP
  1048. */
  1049. struct chan_freq_power *lbs_get_region_cfp_table(u8 region, u8 band, int *cfp_no)
  1050. {
  1051. int i, end;
  1052. lbs_deb_enter(LBS_DEB_MAIN);
  1053. end = ARRAY_SIZE(region_cfp_table);
  1054. for (i = 0; i < end ; i++) {
  1055. lbs_deb_main("region_cfp_table[i].region=%d\n",
  1056. region_cfp_table[i].region);
  1057. if (region_cfp_table[i].region == region) {
  1058. *cfp_no = region_cfp_table[i].cfp_no_BG;
  1059. lbs_deb_leave(LBS_DEB_MAIN);
  1060. return region_cfp_table[i].cfp_BG;
  1061. }
  1062. }
  1063. lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
  1064. return NULL;
  1065. }
  1066. int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
  1067. {
  1068. int ret = 0;
  1069. int i = 0;
  1070. struct chan_freq_power *cfp;
  1071. int cfp_no;
  1072. lbs_deb_enter(LBS_DEB_MAIN);
  1073. memset(priv->region_channel, 0, sizeof(priv->region_channel));
  1074. {
  1075. cfp = lbs_get_region_cfp_table(region, band, &cfp_no);
  1076. if (cfp != NULL) {
  1077. priv->region_channel[i].nrcfp = cfp_no;
  1078. priv->region_channel[i].CFP = cfp;
  1079. } else {
  1080. lbs_deb_main("wrong region code %#x in band B/G\n",
  1081. region);
  1082. ret = -1;
  1083. goto out;
  1084. }
  1085. priv->region_channel[i].valid = 1;
  1086. priv->region_channel[i].region = region;
  1087. priv->region_channel[i].band = band;
  1088. i++;
  1089. }
  1090. out:
  1091. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1092. return ret;
  1093. }
  1094. /**
  1095. * @brief This function handles the interrupt. it will change PS
  1096. * state if applicable. it will wake up main_thread to handle
  1097. * the interrupt event as well.
  1098. *
  1099. * @param dev A pointer to net_device structure
  1100. * @return n/a
  1101. */
  1102. void lbs_interrupt(struct lbs_private *priv)
  1103. {
  1104. lbs_deb_enter(LBS_DEB_THREAD);
  1105. lbs_deb_thread("lbs_interrupt: intcounter=%d\n", priv->intcounter);
  1106. if (spin_trylock(&priv->driver_lock)) {
  1107. spin_unlock(&priv->driver_lock);
  1108. printk(KERN_CRIT "%s called without driver_lock held\n", __func__);
  1109. WARN_ON(1);
  1110. }
  1111. priv->intcounter++;
  1112. if (priv->psstate == PS_STATE_SLEEP)
  1113. priv->psstate = PS_STATE_AWAKE;
  1114. wake_up_interruptible(&priv->waitq);
  1115. lbs_deb_leave(LBS_DEB_THREAD);
  1116. }
  1117. EXPORT_SYMBOL_GPL(lbs_interrupt);
  1118. int lbs_reset_device(struct lbs_private *priv)
  1119. {
  1120. int ret;
  1121. lbs_deb_enter(LBS_DEB_MAIN);
  1122. ret = lbs_prepare_and_send_command(priv, CMD_802_11_RESET,
  1123. CMD_ACT_HALT, 0, 0, NULL);
  1124. msleep_interruptible(10);
  1125. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  1126. return ret;
  1127. }
  1128. EXPORT_SYMBOL_GPL(lbs_reset_device);
  1129. static int __init lbs_init_module(void)
  1130. {
  1131. lbs_deb_enter(LBS_DEB_MAIN);
  1132. lbs_debugfs_init();
  1133. lbs_deb_leave(LBS_DEB_MAIN);
  1134. return 0;
  1135. }
  1136. static void __exit lbs_exit_module(void)
  1137. {
  1138. lbs_deb_enter(LBS_DEB_MAIN);
  1139. lbs_debugfs_remove();
  1140. lbs_deb_leave(LBS_DEB_MAIN);
  1141. }
  1142. /*
  1143. * rtap interface support fuctions
  1144. */
  1145. static int lbs_rtap_open(struct net_device *dev)
  1146. {
  1147. /* Yes, _stop_ the queue. Because we don't support injection */
  1148. netif_carrier_off(dev);
  1149. netif_stop_queue(dev);
  1150. return 0;
  1151. }
  1152. static int lbs_rtap_stop(struct net_device *dev)
  1153. {
  1154. return 0;
  1155. }
  1156. static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1157. {
  1158. netif_stop_queue(dev);
  1159. return NETDEV_TX_BUSY;
  1160. }
  1161. static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
  1162. {
  1163. struct lbs_private *priv = dev->priv;
  1164. return &priv->stats;
  1165. }
  1166. void lbs_remove_rtap(struct lbs_private *priv)
  1167. {
  1168. if (priv->rtap_net_dev == NULL)
  1169. return;
  1170. unregister_netdev(priv->rtap_net_dev);
  1171. free_netdev(priv->rtap_net_dev);
  1172. priv->rtap_net_dev = NULL;
  1173. }
  1174. int lbs_add_rtap(struct lbs_private *priv)
  1175. {
  1176. int rc = 0;
  1177. struct net_device *rtap_dev;
  1178. if (priv->rtap_net_dev)
  1179. return -EPERM;
  1180. rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
  1181. if (rtap_dev == NULL)
  1182. return -ENOMEM;
  1183. memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
  1184. rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1185. rtap_dev->open = lbs_rtap_open;
  1186. rtap_dev->stop = lbs_rtap_stop;
  1187. rtap_dev->get_stats = lbs_rtap_get_stats;
  1188. rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
  1189. rtap_dev->set_multicast_list = lbs_set_multicast_list;
  1190. rtap_dev->priv = priv;
  1191. rc = register_netdev(rtap_dev);
  1192. if (rc) {
  1193. free_netdev(rtap_dev);
  1194. return rc;
  1195. }
  1196. priv->rtap_net_dev = rtap_dev;
  1197. return 0;
  1198. }
  1199. module_init(lbs_init_module);
  1200. module_exit(lbs_exit_module);
  1201. MODULE_DESCRIPTION("Libertas WLAN Driver Library");
  1202. MODULE_AUTHOR("Marvell International Ltd.");
  1203. MODULE_LICENSE("GPL");