main.c 39 KB

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