sysfs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /* Let only one thread read the log at a time, blocking others */
  98. while (wl->fwlog_size == 0) {
  99. DEFINE_WAIT(wait);
  100. prepare_to_wait_exclusive(&wl->fwlog_waitq,
  101. &wait,
  102. TASK_INTERRUPTIBLE);
  103. if (wl->fwlog_size != 0) {
  104. finish_wait(&wl->fwlog_waitq, &wait);
  105. break;
  106. }
  107. mutex_unlock(&wl->mutex);
  108. schedule();
  109. finish_wait(&wl->fwlog_waitq, &wait);
  110. if (signal_pending(current))
  111. return -ERESTARTSYS;
  112. ret = mutex_lock_interruptible(&wl->mutex);
  113. if (ret < 0)
  114. return -ERESTARTSYS;
  115. }
  116. /* Check if the fwlog is still valid */
  117. if (wl->fwlog_size < 0) {
  118. mutex_unlock(&wl->mutex);
  119. return 0;
  120. }
  121. /* Seeking is not supported - old logs are not kept. Disregard pos. */
  122. len = min_t(size_t, count, wl->fwlog_size);
  123. wl->fwlog_size -= len;
  124. memcpy(buffer, wl->fwlog, len);
  125. /* Make room for new messages */
  126. memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
  127. mutex_unlock(&wl->mutex);
  128. return len;
  129. }
  130. static struct bin_attribute fwlog_attr = {
  131. .attr = {.name = "fwlog", .mode = S_IRUSR},
  132. .read = wl1271_sysfs_read_fwlog,
  133. };
  134. int wlcore_sysfs_init(struct wl1271 *wl)
  135. {
  136. int ret;
  137. /* Create sysfs file to control bt coex state */
  138. ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
  139. if (ret < 0) {
  140. wl1271_error("failed to create sysfs file bt_coex_state");
  141. goto out;
  142. }
  143. /* Create sysfs file to get HW PG version */
  144. ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
  145. if (ret < 0) {
  146. wl1271_error("failed to create sysfs file hw_pg_ver");
  147. goto out_bt_coex_state;
  148. }
  149. /* Create sysfs file for the FW log */
  150. ret = device_create_bin_file(wl->dev, &fwlog_attr);
  151. if (ret < 0) {
  152. wl1271_error("failed to create sysfs file fwlog");
  153. goto out_hw_pg_ver;
  154. }
  155. goto out;
  156. out_hw_pg_ver:
  157. device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
  158. out_bt_coex_state:
  159. device_remove_file(wl->dev, &dev_attr_bt_coex_state);
  160. out:
  161. return ret;
  162. }
  163. void wlcore_sysfs_free(struct wl1271 *wl)
  164. {
  165. device_remove_bin_file(wl->dev, &fwlog_attr);
  166. device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
  167. device_remove_file(wl->dev, &dev_attr_bt_coex_state);
  168. }