bcm43xx_sysfs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. Broadcom BCM43xx wireless driver
  3. SYSFS support routines
  4. Copyright (c) 2006 Michael Buesch <mbuesch@freenet.de>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; see the file COPYING. If not, write to
  15. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. */
  18. #include "bcm43xx_sysfs.h"
  19. #include "bcm43xx.h"
  20. #include "bcm43xx_main.h"
  21. #include "bcm43xx_radio.h"
  22. #include <linux/capability.h>
  23. #define GENERIC_FILESIZE 64
  24. static int get_integer(const char *buf, size_t count)
  25. {
  26. char tmp[10 + 1] = { 0 };
  27. int ret = -EINVAL;
  28. if (count == 0)
  29. goto out;
  30. count = min(count, (size_t)10);
  31. memcpy(tmp, buf, count);
  32. ret = simple_strtol(tmp, NULL, 10);
  33. out:
  34. return ret;
  35. }
  36. static int get_boolean(const char *buf, size_t count)
  37. {
  38. if (count != 0) {
  39. if (buf[0] == '1')
  40. return 1;
  41. if (buf[0] == '0')
  42. return 0;
  43. if (count >= 4 && memcmp(buf, "true", 4) == 0)
  44. return 1;
  45. if (count >= 5 && memcmp(buf, "false", 5) == 0)
  46. return 0;
  47. if (count >= 3 && memcmp(buf, "yes", 3) == 0)
  48. return 1;
  49. if (count >= 2 && memcmp(buf, "no", 2) == 0)
  50. return 0;
  51. if (count >= 2 && memcmp(buf, "on", 2) == 0)
  52. return 1;
  53. if (count >= 3 && memcmp(buf, "off", 3) == 0)
  54. return 0;
  55. }
  56. return -EINVAL;
  57. }
  58. static ssize_t bcm43xx_attr_sprom_show(struct device *dev,
  59. struct device_attribute *attr,
  60. char *buf)
  61. {
  62. struct bcm43xx_private *bcm = devattr_to_bcm(attr, attr_sprom);
  63. u16 *sprom;
  64. unsigned long flags;
  65. int i, err;
  66. if (!capable(CAP_NET_ADMIN))
  67. return -EPERM;
  68. assert(BCM43xx_SPROM_SIZE * sizeof(u16) <= PAGE_SIZE);
  69. sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
  70. GFP_KERNEL);
  71. if (!sprom)
  72. return -ENOMEM;
  73. bcm43xx_lock_mmio(bcm, flags);
  74. assert(bcm->initialized);
  75. err = bcm43xx_sprom_read(bcm, sprom);
  76. if (!err) {
  77. for (i = 0; i < BCM43xx_SPROM_SIZE; i++) {
  78. buf[i * 2] = sprom[i] & 0x00FF;
  79. buf[i * 2 + 1] = (sprom[i] & 0xFF00) >> 8;
  80. }
  81. }
  82. bcm43xx_unlock_mmio(bcm, flags);
  83. kfree(sprom);
  84. return err ? err : BCM43xx_SPROM_SIZE * sizeof(u16);
  85. }
  86. static ssize_t bcm43xx_attr_sprom_store(struct device *dev,
  87. struct device_attribute *attr,
  88. const char *buf, size_t count)
  89. {
  90. struct bcm43xx_private *bcm = devattr_to_bcm(attr, attr_sprom);
  91. u16 *sprom;
  92. unsigned long flags;
  93. int i, err;
  94. if (!capable(CAP_NET_ADMIN))
  95. return -EPERM;
  96. if (count != BCM43xx_SPROM_SIZE * sizeof(u16))
  97. return -EINVAL;
  98. sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
  99. GFP_KERNEL);
  100. if (!sprom)
  101. return -ENOMEM;
  102. for (i = 0; i < BCM43xx_SPROM_SIZE; i++) {
  103. sprom[i] = buf[i * 2] & 0xFF;
  104. sprom[i] |= ((u16)(buf[i * 2 + 1] & 0xFF)) << 8;
  105. }
  106. bcm43xx_lock_mmio(bcm, flags);
  107. assert(bcm->initialized);
  108. err = bcm43xx_sprom_write(bcm, sprom);
  109. bcm43xx_unlock_mmio(bcm, flags);
  110. kfree(sprom);
  111. return err ? err : count;
  112. }
  113. static ssize_t bcm43xx_attr_interfmode_show(struct device *dev,
  114. struct device_attribute *attr,
  115. char *buf)
  116. {
  117. struct bcm43xx_private *bcm = devattr_to_bcm(attr, attr_interfmode);
  118. unsigned long flags;
  119. int err;
  120. ssize_t count = 0;
  121. if (!capable(CAP_NET_ADMIN))
  122. return -EPERM;
  123. bcm43xx_lock(bcm, flags);
  124. assert(bcm->initialized);
  125. switch (bcm43xx_current_radio(bcm)->interfmode) {
  126. case BCM43xx_RADIO_INTERFMODE_NONE:
  127. count = snprintf(buf, PAGE_SIZE, "0 (No Interference Mitigation)\n");
  128. break;
  129. case BCM43xx_RADIO_INTERFMODE_NONWLAN:
  130. count = snprintf(buf, PAGE_SIZE, "1 (Non-WLAN Interference Mitigation)\n");
  131. break;
  132. case BCM43xx_RADIO_INTERFMODE_MANUALWLAN:
  133. count = snprintf(buf, PAGE_SIZE, "2 (WLAN Interference Mitigation)\n");
  134. break;
  135. default:
  136. assert(0);
  137. }
  138. err = 0;
  139. bcm43xx_unlock(bcm, flags);
  140. return err ? err : count;
  141. }
  142. static ssize_t bcm43xx_attr_interfmode_store(struct device *dev,
  143. struct device_attribute *attr,
  144. const char *buf, size_t count)
  145. {
  146. struct bcm43xx_private *bcm = devattr_to_bcm(attr, attr_interfmode);
  147. unsigned long flags;
  148. int err;
  149. int mode;
  150. if (!capable(CAP_NET_ADMIN))
  151. return -EPERM;
  152. mode = get_integer(buf, count);
  153. switch (mode) {
  154. case 0:
  155. mode = BCM43xx_RADIO_INTERFMODE_NONE;
  156. break;
  157. case 1:
  158. mode = BCM43xx_RADIO_INTERFMODE_NONWLAN;
  159. break;
  160. case 2:
  161. mode = BCM43xx_RADIO_INTERFMODE_MANUALWLAN;
  162. break;
  163. case 3:
  164. mode = BCM43xx_RADIO_INTERFMODE_AUTOWLAN;
  165. break;
  166. default:
  167. return -EINVAL;
  168. }
  169. bcm43xx_lock_mmio(bcm, flags);
  170. assert(bcm->initialized);
  171. err = bcm43xx_radio_set_interference_mitigation(bcm, mode);
  172. if (err) {
  173. printk(KERN_ERR PFX "Interference Mitigation not "
  174. "supported by device\n");
  175. }
  176. bcm43xx_unlock_mmio(bcm, flags);
  177. return err ? err : count;
  178. }
  179. static ssize_t bcm43xx_attr_preamble_show(struct device *dev,
  180. struct device_attribute *attr,
  181. char *buf)
  182. {
  183. struct bcm43xx_private *bcm = devattr_to_bcm(attr, attr_preamble);
  184. unsigned long flags;
  185. int err;
  186. ssize_t count;
  187. if (!capable(CAP_NET_ADMIN))
  188. return -EPERM;
  189. bcm43xx_lock(bcm, flags);
  190. assert(bcm->initialized);
  191. if (bcm->short_preamble)
  192. count = snprintf(buf, PAGE_SIZE, "1 (Short Preamble enabled)\n");
  193. else
  194. count = snprintf(buf, PAGE_SIZE, "0 (Short Preamble disabled)\n");
  195. err = 0;
  196. bcm43xx_unlock(bcm, flags);
  197. return err ? err : count;
  198. }
  199. static ssize_t bcm43xx_attr_preamble_store(struct device *dev,
  200. struct device_attribute *attr,
  201. const char *buf, size_t count)
  202. {
  203. struct bcm43xx_private *bcm = devattr_to_bcm(attr, attr_preamble);
  204. unsigned long flags;
  205. int err;
  206. int value;
  207. if (!capable(CAP_NET_ADMIN))
  208. return -EPERM;
  209. value = get_boolean(buf, count);
  210. if (value < 0)
  211. return value;
  212. bcm43xx_lock(bcm, flags);
  213. assert(bcm->initialized);
  214. bcm->short_preamble = !!value;
  215. err = 0;
  216. bcm43xx_unlock(bcm, flags);
  217. return err ? err : count;
  218. }
  219. int bcm43xx_sysfs_register(struct bcm43xx_private *bcm)
  220. {
  221. struct device *dev = &bcm->pci_dev->dev;
  222. struct bcm43xx_sysfs *sysfs = &bcm->sysfs;
  223. int err;
  224. assert(bcm->initialized);
  225. sysfs->attr_sprom.attr.name = "sprom";
  226. sysfs->attr_sprom.attr.owner = THIS_MODULE;
  227. sysfs->attr_sprom.attr.mode = 0600;
  228. sysfs->attr_sprom.show = bcm43xx_attr_sprom_show;
  229. sysfs->attr_sprom.store = bcm43xx_attr_sprom_store;
  230. err = device_create_file(dev, &sysfs->attr_sprom);
  231. if (err)
  232. goto out;
  233. sysfs->attr_interfmode.attr.name = "interference";
  234. sysfs->attr_interfmode.attr.owner = THIS_MODULE;
  235. sysfs->attr_interfmode.attr.mode = 0600;
  236. sysfs->attr_interfmode.show = bcm43xx_attr_interfmode_show;
  237. sysfs->attr_interfmode.store = bcm43xx_attr_interfmode_store;
  238. err = device_create_file(dev, &sysfs->attr_interfmode);
  239. if (err)
  240. goto err_remove_sprom;
  241. sysfs->attr_preamble.attr.name = "shortpreamble";
  242. sysfs->attr_preamble.attr.owner = THIS_MODULE;
  243. sysfs->attr_preamble.attr.mode = 0600;
  244. sysfs->attr_preamble.show = bcm43xx_attr_preamble_show;
  245. sysfs->attr_preamble.store = bcm43xx_attr_preamble_store;
  246. err = device_create_file(dev, &sysfs->attr_preamble);
  247. if (err)
  248. goto err_remove_interfmode;
  249. out:
  250. return err;
  251. err_remove_interfmode:
  252. device_remove_file(dev, &sysfs->attr_interfmode);
  253. err_remove_sprom:
  254. device_remove_file(dev, &sysfs->attr_sprom);
  255. goto out;
  256. }
  257. void bcm43xx_sysfs_unregister(struct bcm43xx_private *bcm)
  258. {
  259. struct device *dev = &bcm->pci_dev->dev;
  260. struct bcm43xx_sysfs *sysfs = &bcm->sysfs;
  261. device_remove_file(dev, &sysfs->attr_preamble);
  262. device_remove_file(dev, &sysfs->attr_interfmode);
  263. device_remove_file(dev, &sysfs->attr_sprom);
  264. }