main.c 35 KB

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