sysfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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, S_IRUGO | S_IWUSR,
  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, S_IRUGO,
  85. wl1271_sysfs_show_hw_pg_ver, NULL);
  86. static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
  87. struct bin_attribute *bin_attr,
  88. char *buffer, loff_t pos, size_t count)
  89. {
  90. struct device *dev = container_of(kobj, struct device, kobj);
  91. struct wl1271 *wl = dev_get_drvdata(dev);
  92. ssize_t len;
  93. int ret;
  94. ret = mutex_lock_interruptible(&wl->mutex);
  95. if (ret < 0)
  96. return -ERESTARTSYS;
  97. /* Check if the fwlog is still valid */
  98. if (wl->fwlog_size < 0) {
  99. mutex_unlock(&wl->mutex);
  100. return 0;
  101. }
  102. /* Seeking is not supported - old logs are not kept. Disregard pos. */
  103. len = min_t(size_t, count, wl->fwlog_size);
  104. wl->fwlog_size -= len;
  105. memcpy(buffer, wl->fwlog, len);
  106. /* Make room for new messages */
  107. memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
  108. mutex_unlock(&wl->mutex);
  109. return len;
  110. }
  111. static struct bin_attribute fwlog_attr = {
  112. .attr = {.name = "fwlog", .mode = S_IRUSR},
  113. .read = wl1271_sysfs_read_fwlog,
  114. };
  115. int wlcore_sysfs_init(struct wl1271 *wl)
  116. {
  117. int ret;
  118. /* Create sysfs file to control bt coex state */
  119. ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
  120. if (ret < 0) {
  121. wl1271_error("failed to create sysfs file bt_coex_state");
  122. goto out;
  123. }
  124. /* Create sysfs file to get HW PG version */
  125. ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
  126. if (ret < 0) {
  127. wl1271_error("failed to create sysfs file hw_pg_ver");
  128. goto out_bt_coex_state;
  129. }
  130. /* Create sysfs file for the FW log */
  131. ret = device_create_bin_file(wl->dev, &fwlog_attr);
  132. if (ret < 0) {
  133. wl1271_error("failed to create sysfs file fwlog");
  134. goto out_hw_pg_ver;
  135. }
  136. goto out;
  137. out_hw_pg_ver:
  138. device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
  139. out_bt_coex_state:
  140. device_remove_file(wl->dev, &dev_attr_bt_coex_state);
  141. out:
  142. return ret;
  143. }
  144. void wlcore_sysfs_free(struct wl1271 *wl)
  145. {
  146. device_remove_bin_file(wl->dev, &fwlog_attr);
  147. device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
  148. device_remove_file(wl->dev, &dev_attr_bt_coex_state);
  149. }