sysfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * This file is part of wlcore
  3. *
  4. * Copyright (C) 2013 Texas Instruments Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #include "wlcore.h"
  22. #include "debug.h"
  23. #include "ps.h"
  24. #include "sysfs.h"
  25. static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
  26. struct device_attribute *attr,
  27. char *buf)
  28. {
  29. struct wl1271 *wl = dev_get_drvdata(dev);
  30. ssize_t len;
  31. len = PAGE_SIZE;
  32. mutex_lock(&wl->mutex);
  33. len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
  34. wl->sg_enabled);
  35. mutex_unlock(&wl->mutex);
  36. return len;
  37. }
  38. static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
  39. struct device_attribute *attr,
  40. const char *buf, size_t count)
  41. {
  42. struct wl1271 *wl = dev_get_drvdata(dev);
  43. unsigned long res;
  44. int ret;
  45. ret = kstrtoul(buf, 10, &res);
  46. if (ret < 0) {
  47. wl1271_warning("incorrect value written to bt_coex_mode");
  48. return count;
  49. }
  50. mutex_lock(&wl->mutex);
  51. res = !!res;
  52. if (res == wl->sg_enabled)
  53. goto out;
  54. wl->sg_enabled = res;
  55. if (unlikely(wl->state != WLCORE_STATE_ON))
  56. goto out;
  57. ret = wl1271_ps_elp_wakeup(wl);
  58. if (ret < 0)
  59. goto out;
  60. wl1271_acx_sg_enable(wl, wl->sg_enabled);
  61. wl1271_ps_elp_sleep(wl);
  62. out:
  63. mutex_unlock(&wl->mutex);
  64. return count;
  65. }
  66. static DEVICE_ATTR(bt_coex_state, 0644,
  67. wl1271_sysfs_show_bt_coex_state,
  68. wl1271_sysfs_store_bt_coex_state);
  69. static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
  70. struct device_attribute *attr,
  71. char *buf)
  72. {
  73. struct wl1271 *wl = dev_get_drvdata(dev);
  74. ssize_t len;
  75. len = PAGE_SIZE;
  76. mutex_lock(&wl->mutex);
  77. if (wl->hw_pg_ver >= 0)
  78. len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
  79. else
  80. len = snprintf(buf, len, "n/a\n");
  81. mutex_unlock(&wl->mutex);
  82. return len;
  83. }
  84. static DEVICE_ATTR(hw_pg_ver, 0444, wl1271_sysfs_show_hw_pg_ver, NULL);
  85. static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
  86. struct bin_attribute *bin_attr,
  87. char *buffer, loff_t pos, size_t count)
  88. {
  89. struct device *dev = container_of(kobj, struct device, kobj);
  90. struct wl1271 *wl = dev_get_drvdata(dev);
  91. ssize_t len;
  92. int ret;
  93. ret = mutex_lock_interruptible(&wl->mutex);
  94. if (ret < 0)
  95. return -ERESTARTSYS;
  96. /* Check if the fwlog is still valid */
  97. if (wl->fwlog_size < 0) {
  98. mutex_unlock(&wl->mutex);
  99. return 0;
  100. }
  101. /* Seeking is not supported - old logs are not kept. Disregard pos. */
  102. len = min_t(size_t, count, wl->fwlog_size);
  103. wl->fwlog_size -= len;
  104. memcpy(buffer, wl->fwlog, len);
  105. /* Make room for new messages */
  106. memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
  107. mutex_unlock(&wl->mutex);
  108. return len;
  109. }
  110. static const struct bin_attribute fwlog_attr = {
  111. .attr = { .name = "fwlog", .mode = 0400 },
  112. .read = wl1271_sysfs_read_fwlog,
  113. };
  114. int wlcore_sysfs_init(struct wl1271 *wl)
  115. {
  116. int ret;
  117. /* Create sysfs file to control bt coex state */
  118. ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
  119. if (ret < 0) {
  120. wl1271_error("failed to create sysfs file bt_coex_state");
  121. goto out;
  122. }
  123. /* Create sysfs file to get HW PG version */
  124. ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
  125. if (ret < 0) {
  126. wl1271_error("failed to create sysfs file hw_pg_ver");
  127. goto out_bt_coex_state;
  128. }
  129. /* Create sysfs file for the FW log */
  130. ret = device_create_bin_file(wl->dev, &fwlog_attr);
  131. if (ret < 0) {
  132. wl1271_error("failed to create sysfs file fwlog");
  133. goto out_hw_pg_ver;
  134. }
  135. goto out;
  136. out_hw_pg_ver:
  137. device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
  138. out_bt_coex_state:
  139. device_remove_file(wl->dev, &dev_attr_bt_coex_state);
  140. out:
  141. return ret;
  142. }
  143. void wlcore_sysfs_free(struct wl1271 *wl)
  144. {
  145. device_remove_bin_file(wl->dev, &fwlog_attr);
  146. device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
  147. device_remove_file(wl->dev, &dev_attr_bt_coex_state);
  148. }