main.c 37 KB

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