debugfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/dcache.h>
  3. #include <linux/debugfs.h>
  4. #include <linux/delay.h>
  5. #include <linux/hardirq.h>
  6. #include <linux/mm.h>
  7. #include <linux/string.h>
  8. #include <linux/slab.h>
  9. #include <linux/export.h>
  10. #include "decl.h"
  11. #include "cmd.h"
  12. #include "debugfs.h"
  13. static struct dentry *lbs_dir;
  14. static char *szStates[] = {
  15. "Connected",
  16. "Disconnected"
  17. };
  18. #ifdef PROC_DEBUG
  19. static void lbs_debug_init(struct lbs_private *priv);
  20. #endif
  21. static ssize_t write_file_dummy(struct file *file, const char __user *buf,
  22. size_t count, loff_t *ppos)
  23. {
  24. return -EINVAL;
  25. }
  26. static const size_t len = PAGE_SIZE;
  27. static ssize_t lbs_dev_info(struct file *file, char __user *userbuf,
  28. size_t count, loff_t *ppos)
  29. {
  30. struct lbs_private *priv = file->private_data;
  31. size_t pos = 0;
  32. unsigned long addr = get_zeroed_page(GFP_KERNEL);
  33. char *buf = (char *)addr;
  34. ssize_t res;
  35. if (!buf)
  36. return -ENOMEM;
  37. pos += snprintf(buf+pos, len-pos, "state = %s\n",
  38. szStates[priv->connect_status]);
  39. pos += snprintf(buf+pos, len-pos, "region_code = %02x\n",
  40. (u32) priv->regioncode);
  41. res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  42. free_page(addr);
  43. return res;
  44. }
  45. static ssize_t lbs_sleepparams_write(struct file *file,
  46. const char __user *user_buf, size_t count,
  47. loff_t *ppos)
  48. {
  49. struct lbs_private *priv = file->private_data;
  50. ssize_t ret;
  51. struct sleep_params sp;
  52. int p1, p2, p3, p4, p5, p6;
  53. char *buf;
  54. buf = memdup_user_nul(user_buf, min(count, len - 1));
  55. if (IS_ERR(buf))
  56. return PTR_ERR(buf);
  57. ret = sscanf(buf, "%d %d %d %d %d %d", &p1, &p2, &p3, &p4, &p5, &p6);
  58. if (ret != 6) {
  59. ret = -EINVAL;
  60. goto out_unlock;
  61. }
  62. sp.sp_error = p1;
  63. sp.sp_offset = p2;
  64. sp.sp_stabletime = p3;
  65. sp.sp_calcontrol = p4;
  66. sp.sp_extsleepclk = p5;
  67. sp.sp_reserved = p6;
  68. ret = lbs_cmd_802_11_sleep_params(priv, CMD_ACT_SET, &sp);
  69. if (!ret)
  70. ret = count;
  71. else if (ret > 0)
  72. ret = -EINVAL;
  73. out_unlock:
  74. kfree(buf);
  75. return ret;
  76. }
  77. static ssize_t lbs_sleepparams_read(struct file *file, char __user *userbuf,
  78. size_t count, loff_t *ppos)
  79. {
  80. struct lbs_private *priv = file->private_data;
  81. ssize_t ret;
  82. size_t pos = 0;
  83. struct sleep_params sp;
  84. unsigned long addr = get_zeroed_page(GFP_KERNEL);
  85. char *buf = (char *)addr;
  86. if (!buf)
  87. return -ENOMEM;
  88. ret = lbs_cmd_802_11_sleep_params(priv, CMD_ACT_GET, &sp);
  89. if (ret)
  90. goto out_unlock;
  91. pos += snprintf(buf, len, "%d %d %d %d %d %d\n", sp.sp_error,
  92. sp.sp_offset, sp.sp_stabletime,
  93. sp.sp_calcontrol, sp.sp_extsleepclk,
  94. sp.sp_reserved);
  95. ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  96. out_unlock:
  97. free_page(addr);
  98. return ret;
  99. }
  100. static ssize_t lbs_host_sleep_write(struct file *file,
  101. const char __user *user_buf, size_t count,
  102. loff_t *ppos)
  103. {
  104. struct lbs_private *priv = file->private_data;
  105. ssize_t ret;
  106. int host_sleep;
  107. char *buf;
  108. buf = memdup_user_nul(user_buf, min(count, len - 1));
  109. if (IS_ERR(buf))
  110. return PTR_ERR(buf);
  111. ret = sscanf(buf, "%d", &host_sleep);
  112. if (ret != 1) {
  113. ret = -EINVAL;
  114. goto out_unlock;
  115. }
  116. if (host_sleep == 0)
  117. ret = lbs_set_host_sleep(priv, 0);
  118. else if (host_sleep == 1) {
  119. if (priv->wol_criteria == EHS_REMOVE_WAKEUP) {
  120. netdev_info(priv->dev,
  121. "wake parameters not configured\n");
  122. ret = -EINVAL;
  123. goto out_unlock;
  124. }
  125. ret = lbs_set_host_sleep(priv, 1);
  126. } else {
  127. netdev_err(priv->dev, "invalid option\n");
  128. ret = -EINVAL;
  129. }
  130. if (!ret)
  131. ret = count;
  132. out_unlock:
  133. kfree(buf);
  134. return ret;
  135. }
  136. static ssize_t lbs_host_sleep_read(struct file *file, char __user *userbuf,
  137. size_t count, loff_t *ppos)
  138. {
  139. struct lbs_private *priv = file->private_data;
  140. ssize_t ret;
  141. size_t pos = 0;
  142. unsigned long addr = get_zeroed_page(GFP_KERNEL);
  143. char *buf = (char *)addr;
  144. if (!buf)
  145. return -ENOMEM;
  146. pos += snprintf(buf, len, "%d\n", priv->is_host_sleep_activated);
  147. ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  148. free_page(addr);
  149. return ret;
  150. }
  151. /*
  152. * When calling CMD_802_11_SUBSCRIBE_EVENT with CMD_ACT_GET, me might
  153. * get a bunch of vendor-specific TLVs (a.k.a. IEs) back from the
  154. * firmware. Here's an example:
  155. * 04 01 02 00 00 00 05 01 02 00 00 00 06 01 02 00
  156. * 00 00 07 01 02 00 3c 00 00 00 00 00 00 00 03 03
  157. * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  158. *
  159. * The 04 01 is the TLV type (here TLV_TYPE_RSSI_LOW), 02 00 is the length,
  160. * 00 00 are the data bytes of this TLV. For this TLV, their meaning is
  161. * defined in mrvlietypes_thresholds
  162. *
  163. * This function searches in this TLV data chunk for a given TLV type
  164. * and returns a pointer to the first data byte of the TLV, or to NULL
  165. * if the TLV hasn't been found.
  166. */
  167. static void *lbs_tlv_find(uint16_t tlv_type, const uint8_t *tlv, uint16_t size)
  168. {
  169. struct mrvl_ie_header *tlv_h;
  170. uint16_t length;
  171. ssize_t pos = 0;
  172. while (pos < size) {
  173. tlv_h = (struct mrvl_ie_header *) tlv;
  174. if (!tlv_h->len)
  175. return NULL;
  176. if (tlv_h->type == cpu_to_le16(tlv_type))
  177. return tlv_h;
  178. length = le16_to_cpu(tlv_h->len) + sizeof(*tlv_h);
  179. pos += length;
  180. tlv += length;
  181. }
  182. return NULL;
  183. }
  184. static ssize_t lbs_threshold_read(uint16_t tlv_type, uint16_t event_mask,
  185. struct file *file, char __user *userbuf,
  186. size_t count, loff_t *ppos)
  187. {
  188. struct cmd_ds_802_11_subscribe_event *subscribed;
  189. struct mrvl_ie_thresholds *got;
  190. struct lbs_private *priv = file->private_data;
  191. ssize_t ret = 0;
  192. size_t pos = 0;
  193. char *buf;
  194. u8 value;
  195. u8 freq;
  196. int events = 0;
  197. buf = (char *)get_zeroed_page(GFP_KERNEL);
  198. if (!buf)
  199. return -ENOMEM;
  200. subscribed = kzalloc(sizeof(*subscribed), GFP_KERNEL);
  201. if (!subscribed) {
  202. ret = -ENOMEM;
  203. goto out_page;
  204. }
  205. subscribed->hdr.size = cpu_to_le16(sizeof(*subscribed));
  206. subscribed->action = cpu_to_le16(CMD_ACT_GET);
  207. ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, subscribed);
  208. if (ret)
  209. goto out_cmd;
  210. got = lbs_tlv_find(tlv_type, subscribed->tlv, sizeof(subscribed->tlv));
  211. if (got) {
  212. value = got->value;
  213. freq = got->freq;
  214. events = le16_to_cpu(subscribed->events);
  215. pos += snprintf(buf, len, "%d %d %d\n", value, freq,
  216. !!(events & event_mask));
  217. }
  218. ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  219. out_cmd:
  220. kfree(subscribed);
  221. out_page:
  222. free_page((unsigned long)buf);
  223. return ret;
  224. }
  225. static ssize_t lbs_threshold_write(uint16_t tlv_type, uint16_t event_mask,
  226. struct file *file,
  227. const char __user *userbuf, size_t count,
  228. loff_t *ppos)
  229. {
  230. struct cmd_ds_802_11_subscribe_event *events;
  231. struct mrvl_ie_thresholds *tlv;
  232. struct lbs_private *priv = file->private_data;
  233. int value, freq, new_mask;
  234. uint16_t curr_mask;
  235. char *buf;
  236. int ret;
  237. buf = memdup_user_nul(userbuf, min(count, len - 1));
  238. if (IS_ERR(buf))
  239. return PTR_ERR(buf);
  240. ret = sscanf(buf, "%d %d %d", &value, &freq, &new_mask);
  241. if (ret != 3) {
  242. ret = -EINVAL;
  243. goto out_page;
  244. }
  245. events = kzalloc(sizeof(*events), GFP_KERNEL);
  246. if (!events) {
  247. ret = -ENOMEM;
  248. goto out_page;
  249. }
  250. events->hdr.size = cpu_to_le16(sizeof(*events));
  251. events->action = cpu_to_le16(CMD_ACT_GET);
  252. ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, events);
  253. if (ret)
  254. goto out_events;
  255. curr_mask = le16_to_cpu(events->events);
  256. if (new_mask)
  257. new_mask = curr_mask | event_mask;
  258. else
  259. new_mask = curr_mask & ~event_mask;
  260. /* Now everything is set and we can send stuff down to the firmware */
  261. tlv = (void *)events->tlv;
  262. events->action = cpu_to_le16(CMD_ACT_SET);
  263. events->events = cpu_to_le16(new_mask);
  264. tlv->header.type = cpu_to_le16(tlv_type);
  265. tlv->header.len = cpu_to_le16(sizeof(*tlv) - sizeof(tlv->header));
  266. tlv->value = value;
  267. if (tlv_type != TLV_TYPE_BCNMISS)
  268. tlv->freq = freq;
  269. /* The command header, the action, the event mask, and one TLV */
  270. events->hdr.size = cpu_to_le16(sizeof(events->hdr) + 4 + sizeof(*tlv));
  271. ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, events);
  272. if (!ret)
  273. ret = count;
  274. out_events:
  275. kfree(events);
  276. out_page:
  277. kfree(buf);
  278. return ret;
  279. }
  280. static ssize_t lbs_lowrssi_read(struct file *file, char __user *userbuf,
  281. size_t count, loff_t *ppos)
  282. {
  283. return lbs_threshold_read(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
  284. file, userbuf, count, ppos);
  285. }
  286. static ssize_t lbs_lowrssi_write(struct file *file, const char __user *userbuf,
  287. size_t count, loff_t *ppos)
  288. {
  289. return lbs_threshold_write(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
  290. file, userbuf, count, ppos);
  291. }
  292. static ssize_t lbs_lowsnr_read(struct file *file, char __user *userbuf,
  293. size_t count, loff_t *ppos)
  294. {
  295. return lbs_threshold_read(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
  296. file, userbuf, count, ppos);
  297. }
  298. static ssize_t lbs_lowsnr_write(struct file *file, const char __user *userbuf,
  299. size_t count, loff_t *ppos)
  300. {
  301. return lbs_threshold_write(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
  302. file, userbuf, count, ppos);
  303. }
  304. static ssize_t lbs_failcount_read(struct file *file, char __user *userbuf,
  305. size_t count, loff_t *ppos)
  306. {
  307. return lbs_threshold_read(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
  308. file, userbuf, count, ppos);
  309. }
  310. static ssize_t lbs_failcount_write(struct file *file, const char __user *userbuf,
  311. size_t count, loff_t *ppos)
  312. {
  313. return lbs_threshold_write(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
  314. file, userbuf, count, ppos);
  315. }
  316. static ssize_t lbs_highrssi_read(struct file *file, char __user *userbuf,
  317. size_t count, loff_t *ppos)
  318. {
  319. return lbs_threshold_read(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
  320. file, userbuf, count, ppos);
  321. }
  322. static ssize_t lbs_highrssi_write(struct file *file, const char __user *userbuf,
  323. size_t count, loff_t *ppos)
  324. {
  325. return lbs_threshold_write(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
  326. file, userbuf, count, ppos);
  327. }
  328. static ssize_t lbs_highsnr_read(struct file *file, char __user *userbuf,
  329. size_t count, loff_t *ppos)
  330. {
  331. return lbs_threshold_read(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
  332. file, userbuf, count, ppos);
  333. }
  334. static ssize_t lbs_highsnr_write(struct file *file, const char __user *userbuf,
  335. size_t count, loff_t *ppos)
  336. {
  337. return lbs_threshold_write(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
  338. file, userbuf, count, ppos);
  339. }
  340. static ssize_t lbs_bcnmiss_read(struct file *file, char __user *userbuf,
  341. size_t count, loff_t *ppos)
  342. {
  343. return lbs_threshold_read(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
  344. file, userbuf, count, ppos);
  345. }
  346. static ssize_t lbs_bcnmiss_write(struct file *file, const char __user *userbuf,
  347. size_t count, loff_t *ppos)
  348. {
  349. return lbs_threshold_write(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
  350. file, userbuf, count, ppos);
  351. }
  352. static ssize_t lbs_rdmac_read(struct file *file, char __user *userbuf,
  353. size_t count, loff_t *ppos)
  354. {
  355. struct lbs_private *priv = file->private_data;
  356. ssize_t pos = 0;
  357. int ret;
  358. unsigned long addr = get_zeroed_page(GFP_KERNEL);
  359. char *buf = (char *)addr;
  360. u32 val = 0;
  361. if (!buf)
  362. return -ENOMEM;
  363. ret = lbs_get_reg(priv, CMD_MAC_REG_ACCESS, priv->mac_offset, &val);
  364. mdelay(10);
  365. if (!ret) {
  366. pos = snprintf(buf, len, "MAC[0x%x] = 0x%08x\n",
  367. priv->mac_offset, val);
  368. ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  369. }
  370. free_page(addr);
  371. return ret;
  372. }
  373. static ssize_t lbs_rdmac_write(struct file *file,
  374. const char __user *userbuf,
  375. size_t count, loff_t *ppos)
  376. {
  377. struct lbs_private *priv = file->private_data;
  378. char *buf;
  379. buf = memdup_user_nul(userbuf, min(count, len - 1));
  380. if (IS_ERR(buf))
  381. return PTR_ERR(buf);
  382. priv->mac_offset = simple_strtoul(buf, NULL, 16);
  383. kfree(buf);
  384. return count;
  385. }
  386. static ssize_t lbs_wrmac_write(struct file *file,
  387. const char __user *userbuf,
  388. size_t count, loff_t *ppos)
  389. {
  390. struct lbs_private *priv = file->private_data;
  391. ssize_t res;
  392. u32 offset, value;
  393. char *buf;
  394. buf = memdup_user_nul(userbuf, min(count, len - 1));
  395. if (IS_ERR(buf))
  396. return PTR_ERR(buf);
  397. res = sscanf(buf, "%x %x", &offset, &value);
  398. if (res != 2) {
  399. res = -EFAULT;
  400. goto out_unlock;
  401. }
  402. res = lbs_set_reg(priv, CMD_MAC_REG_ACCESS, offset, value);
  403. mdelay(10);
  404. if (!res)
  405. res = count;
  406. out_unlock:
  407. kfree(buf);
  408. return res;
  409. }
  410. static ssize_t lbs_rdbbp_read(struct file *file, char __user *userbuf,
  411. size_t count, loff_t *ppos)
  412. {
  413. struct lbs_private *priv = file->private_data;
  414. ssize_t pos = 0;
  415. int ret;
  416. unsigned long addr = get_zeroed_page(GFP_KERNEL);
  417. char *buf = (char *)addr;
  418. u32 val;
  419. if (!buf)
  420. return -ENOMEM;
  421. ret = lbs_get_reg(priv, CMD_BBP_REG_ACCESS, priv->bbp_offset, &val);
  422. mdelay(10);
  423. if (!ret) {
  424. pos = snprintf(buf, len, "BBP[0x%x] = 0x%08x\n",
  425. priv->bbp_offset, val);
  426. ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  427. }
  428. free_page(addr);
  429. return ret;
  430. }
  431. static ssize_t lbs_rdbbp_write(struct file *file,
  432. const char __user *userbuf,
  433. size_t count, loff_t *ppos)
  434. {
  435. struct lbs_private *priv = file->private_data;
  436. char *buf;
  437. buf = memdup_user_nul(userbuf, min(count, len - 1));
  438. if (IS_ERR(buf))
  439. return PTR_ERR(buf);
  440. priv->bbp_offset = simple_strtoul(buf, NULL, 16);
  441. kfree(buf);
  442. return count;
  443. }
  444. static ssize_t lbs_wrbbp_write(struct file *file,
  445. const char __user *userbuf,
  446. size_t count, loff_t *ppos)
  447. {
  448. struct lbs_private *priv = file->private_data;
  449. ssize_t res;
  450. u32 offset, value;
  451. char *buf;
  452. buf = memdup_user_nul(userbuf, min(count, len - 1));
  453. if (IS_ERR(buf))
  454. return PTR_ERR(buf);
  455. res = sscanf(buf, "%x %x", &offset, &value);
  456. if (res != 2) {
  457. res = -EFAULT;
  458. goto out_unlock;
  459. }
  460. res = lbs_set_reg(priv, CMD_BBP_REG_ACCESS, offset, value);
  461. mdelay(10);
  462. if (!res)
  463. res = count;
  464. out_unlock:
  465. kfree(buf);
  466. return res;
  467. }
  468. static ssize_t lbs_rdrf_read(struct file *file, char __user *userbuf,
  469. size_t count, loff_t *ppos)
  470. {
  471. struct lbs_private *priv = file->private_data;
  472. ssize_t pos = 0;
  473. int ret;
  474. unsigned long addr = get_zeroed_page(GFP_KERNEL);
  475. char *buf = (char *)addr;
  476. u32 val;
  477. if (!buf)
  478. return -ENOMEM;
  479. ret = lbs_get_reg(priv, CMD_RF_REG_ACCESS, priv->rf_offset, &val);
  480. mdelay(10);
  481. if (!ret) {
  482. pos = snprintf(buf, len, "RF[0x%x] = 0x%08x\n",
  483. priv->rf_offset, val);
  484. ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
  485. }
  486. free_page(addr);
  487. return ret;
  488. }
  489. static ssize_t lbs_rdrf_write(struct file *file,
  490. const char __user *userbuf,
  491. size_t count, loff_t *ppos)
  492. {
  493. struct lbs_private *priv = file->private_data;
  494. char *buf;
  495. buf = memdup_user_nul(userbuf, min(count, len - 1));
  496. if (IS_ERR(buf))
  497. return PTR_ERR(buf);
  498. priv->rf_offset = simple_strtoul(buf, NULL, 16);
  499. kfree(buf);
  500. return count;
  501. }
  502. static ssize_t lbs_wrrf_write(struct file *file,
  503. const char __user *userbuf,
  504. size_t count, loff_t *ppos)
  505. {
  506. struct lbs_private *priv = file->private_data;
  507. ssize_t res;
  508. u32 offset, value;
  509. char *buf;
  510. buf = memdup_user_nul(userbuf, min(count, len - 1));
  511. if (IS_ERR(buf))
  512. return PTR_ERR(buf);
  513. res = sscanf(buf, "%x %x", &offset, &value);
  514. if (res != 2) {
  515. res = -EFAULT;
  516. goto out_unlock;
  517. }
  518. res = lbs_set_reg(priv, CMD_RF_REG_ACCESS, offset, value);
  519. mdelay(10);
  520. if (!res)
  521. res = count;
  522. out_unlock:
  523. kfree(buf);
  524. return res;
  525. }
  526. #define FOPS(fread, fwrite) { \
  527. .owner = THIS_MODULE, \
  528. .open = simple_open, \
  529. .read = (fread), \
  530. .write = (fwrite), \
  531. .llseek = generic_file_llseek, \
  532. }
  533. struct lbs_debugfs_files {
  534. const char *name;
  535. umode_t perm;
  536. struct file_operations fops;
  537. };
  538. static const struct lbs_debugfs_files debugfs_files[] = {
  539. { "info", 0444, FOPS(lbs_dev_info, write_file_dummy), },
  540. { "sleepparams", 0644, FOPS(lbs_sleepparams_read,
  541. lbs_sleepparams_write), },
  542. { "hostsleep", 0644, FOPS(lbs_host_sleep_read,
  543. lbs_host_sleep_write), },
  544. };
  545. static const struct lbs_debugfs_files debugfs_events_files[] = {
  546. {"low_rssi", 0644, FOPS(lbs_lowrssi_read,
  547. lbs_lowrssi_write), },
  548. {"low_snr", 0644, FOPS(lbs_lowsnr_read,
  549. lbs_lowsnr_write), },
  550. {"failure_count", 0644, FOPS(lbs_failcount_read,
  551. lbs_failcount_write), },
  552. {"beacon_missed", 0644, FOPS(lbs_bcnmiss_read,
  553. lbs_bcnmiss_write), },
  554. {"high_rssi", 0644, FOPS(lbs_highrssi_read,
  555. lbs_highrssi_write), },
  556. {"high_snr", 0644, FOPS(lbs_highsnr_read,
  557. lbs_highsnr_write), },
  558. };
  559. static const struct lbs_debugfs_files debugfs_regs_files[] = {
  560. {"rdmac", 0644, FOPS(lbs_rdmac_read, lbs_rdmac_write), },
  561. {"wrmac", 0600, FOPS(NULL, lbs_wrmac_write), },
  562. {"rdbbp", 0644, FOPS(lbs_rdbbp_read, lbs_rdbbp_write), },
  563. {"wrbbp", 0600, FOPS(NULL, lbs_wrbbp_write), },
  564. {"rdrf", 0644, FOPS(lbs_rdrf_read, lbs_rdrf_write), },
  565. {"wrrf", 0600, FOPS(NULL, lbs_wrrf_write), },
  566. };
  567. void lbs_debugfs_init(void)
  568. {
  569. if (!lbs_dir)
  570. lbs_dir = debugfs_create_dir("lbs_wireless", NULL);
  571. }
  572. void lbs_debugfs_remove(void)
  573. {
  574. debugfs_remove(lbs_dir);
  575. }
  576. void lbs_debugfs_init_one(struct lbs_private *priv, struct net_device *dev)
  577. {
  578. int i;
  579. const struct lbs_debugfs_files *files;
  580. if (!lbs_dir)
  581. goto exit;
  582. priv->debugfs_dir = debugfs_create_dir(dev->name, lbs_dir);
  583. if (!priv->debugfs_dir)
  584. goto exit;
  585. for (i=0; i<ARRAY_SIZE(debugfs_files); i++) {
  586. files = &debugfs_files[i];
  587. priv->debugfs_files[i] = debugfs_create_file(files->name,
  588. files->perm,
  589. priv->debugfs_dir,
  590. priv,
  591. &files->fops);
  592. }
  593. priv->events_dir = debugfs_create_dir("subscribed_events", priv->debugfs_dir);
  594. if (!priv->events_dir)
  595. goto exit;
  596. for (i=0; i<ARRAY_SIZE(debugfs_events_files); i++) {
  597. files = &debugfs_events_files[i];
  598. priv->debugfs_events_files[i] = debugfs_create_file(files->name,
  599. files->perm,
  600. priv->events_dir,
  601. priv,
  602. &files->fops);
  603. }
  604. priv->regs_dir = debugfs_create_dir("registers", priv->debugfs_dir);
  605. if (!priv->regs_dir)
  606. goto exit;
  607. for (i=0; i<ARRAY_SIZE(debugfs_regs_files); i++) {
  608. files = &debugfs_regs_files[i];
  609. priv->debugfs_regs_files[i] = debugfs_create_file(files->name,
  610. files->perm,
  611. priv->regs_dir,
  612. priv,
  613. &files->fops);
  614. }
  615. #ifdef PROC_DEBUG
  616. lbs_debug_init(priv);
  617. #endif
  618. exit:
  619. return;
  620. }
  621. void lbs_debugfs_remove_one(struct lbs_private *priv)
  622. {
  623. int i;
  624. for(i=0; i<ARRAY_SIZE(debugfs_regs_files); i++)
  625. debugfs_remove(priv->debugfs_regs_files[i]);
  626. debugfs_remove(priv->regs_dir);
  627. for(i=0; i<ARRAY_SIZE(debugfs_events_files); i++)
  628. debugfs_remove(priv->debugfs_events_files[i]);
  629. debugfs_remove(priv->events_dir);
  630. #ifdef PROC_DEBUG
  631. debugfs_remove(priv->debugfs_debug);
  632. #endif
  633. for(i=0; i<ARRAY_SIZE(debugfs_files); i++)
  634. debugfs_remove(priv->debugfs_files[i]);
  635. debugfs_remove(priv->debugfs_dir);
  636. }
  637. /* debug entry */
  638. #ifdef PROC_DEBUG
  639. #define item_size(n) (FIELD_SIZEOF(struct lbs_private, n))
  640. #define item_addr(n) (offsetof(struct lbs_private, n))
  641. struct debug_data {
  642. char name[32];
  643. u32 size;
  644. size_t addr;
  645. };
  646. /* To debug any member of struct lbs_private, simply add one line here.
  647. */
  648. static struct debug_data items[] = {
  649. {"psmode", item_size(psmode), item_addr(psmode)},
  650. {"psstate", item_size(psstate), item_addr(psstate)},
  651. };
  652. static int num_of_items = ARRAY_SIZE(items);
  653. /**
  654. * lbs_debugfs_read - proc read function
  655. *
  656. * @file: file to read
  657. * @userbuf: pointer to buffer
  658. * @count: number of bytes to read
  659. * @ppos: read data starting position
  660. *
  661. * returns: amount of data read or negative error code
  662. */
  663. static ssize_t lbs_debugfs_read(struct file *file, char __user *userbuf,
  664. size_t count, loff_t *ppos)
  665. {
  666. int val = 0;
  667. size_t pos = 0;
  668. ssize_t res;
  669. char *p;
  670. int i;
  671. struct debug_data *d;
  672. unsigned long addr = get_zeroed_page(GFP_KERNEL);
  673. char *buf = (char *)addr;
  674. if (!buf)
  675. return -ENOMEM;
  676. p = buf;
  677. d = file->private_data;
  678. for (i = 0; i < num_of_items; i++) {
  679. if (d[i].size == 1)
  680. val = *((u8 *) d[i].addr);
  681. else if (d[i].size == 2)
  682. val = *((u16 *) d[i].addr);
  683. else if (d[i].size == 4)
  684. val = *((u32 *) d[i].addr);
  685. else if (d[i].size == 8)
  686. val = *((u64 *) d[i].addr);
  687. pos += sprintf(p + pos, "%s=%d\n", d[i].name, val);
  688. }
  689. res = simple_read_from_buffer(userbuf, count, ppos, p, pos);
  690. free_page(addr);
  691. return res;
  692. }
  693. /**
  694. * lbs_debugfs_write - proc write function
  695. *
  696. * @f: file pointer
  697. * @buf: pointer to data buffer
  698. * @cnt: data number to write
  699. * @ppos: file position
  700. *
  701. * returns: amount of data written
  702. */
  703. static ssize_t lbs_debugfs_write(struct file *f, const char __user *buf,
  704. size_t cnt, loff_t *ppos)
  705. {
  706. int r, i;
  707. char *pdata;
  708. char *p;
  709. char *p0;
  710. char *p1;
  711. char *p2;
  712. struct debug_data *d = f->private_data;
  713. if (cnt == 0)
  714. return 0;
  715. pdata = memdup_user_nul(buf, cnt);
  716. if (IS_ERR(pdata))
  717. return PTR_ERR(pdata);
  718. p0 = pdata;
  719. for (i = 0; i < num_of_items; i++) {
  720. do {
  721. p = strstr(p0, d[i].name);
  722. if (p == NULL)
  723. break;
  724. p1 = strchr(p, '\n');
  725. if (p1 == NULL)
  726. break;
  727. p0 = p1++;
  728. p2 = strchr(p, '=');
  729. if (!p2)
  730. break;
  731. p2++;
  732. r = simple_strtoul(p2, NULL, 0);
  733. if (d[i].size == 1)
  734. *((u8 *) d[i].addr) = (u8) r;
  735. else if (d[i].size == 2)
  736. *((u16 *) d[i].addr) = (u16) r;
  737. else if (d[i].size == 4)
  738. *((u32 *) d[i].addr) = (u32) r;
  739. else if (d[i].size == 8)
  740. *((u64 *) d[i].addr) = (u64) r;
  741. break;
  742. } while (1);
  743. }
  744. kfree(pdata);
  745. return (ssize_t)cnt;
  746. }
  747. static const struct file_operations lbs_debug_fops = {
  748. .owner = THIS_MODULE,
  749. .open = simple_open,
  750. .write = lbs_debugfs_write,
  751. .read = lbs_debugfs_read,
  752. .llseek = default_llseek,
  753. };
  754. /**
  755. * lbs_debug_init - create debug proc file
  756. *
  757. * @priv: pointer to &struct lbs_private
  758. *
  759. * returns: N/A
  760. */
  761. static void lbs_debug_init(struct lbs_private *priv)
  762. {
  763. int i;
  764. if (!priv->debugfs_dir)
  765. return;
  766. for (i = 0; i < num_of_items; i++)
  767. items[i].addr += (size_t) priv;
  768. priv->debugfs_debug = debugfs_create_file("debug", 0644,
  769. priv->debugfs_dir, &items[0],
  770. &lbs_debug_fops);
  771. }
  772. #endif