bcm43xx_sysfs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 int sprom2hex(const u16 *sprom, char *buf, size_t buf_len)
  59. {
  60. int i, pos = 0;
  61. for (i = 0; i < BCM43xx_SPROM_SIZE; i++) {
  62. pos += snprintf(buf + pos, buf_len - pos - 1,
  63. "%04X", swab16(sprom[i]) & 0xFFFF);
  64. }
  65. pos += snprintf(buf + pos, buf_len - pos - 1, "\n");
  66. return pos + 1;
  67. }
  68. static int hex2sprom(u16 *sprom, const char *dump, size_t len)
  69. {
  70. char tmp[5] = { 0 };
  71. int cnt = 0;
  72. unsigned long parsed;
  73. if (len < BCM43xx_SPROM_SIZE * sizeof(u16) * 2)
  74. return -EINVAL;
  75. while (cnt < BCM43xx_SPROM_SIZE) {
  76. memcpy(tmp, dump, 4);
  77. dump += 4;
  78. parsed = simple_strtoul(tmp, NULL, 16);
  79. sprom[cnt++] = swab16((u16)parsed);
  80. }
  81. return 0;
  82. }
  83. static ssize_t bcm43xx_attr_sprom_show(struct device *dev,
  84. struct device_attribute *attr,
  85. char *buf)
  86. {
  87. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  88. u16 *sprom;
  89. unsigned long flags;
  90. int err;
  91. if (!capable(CAP_NET_ADMIN))
  92. return -EPERM;
  93. assert(BCM43xx_SPROM_SIZE * sizeof(u16) <= PAGE_SIZE);
  94. sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
  95. GFP_KERNEL);
  96. if (!sprom)
  97. return -ENOMEM;
  98. bcm43xx_lock_mmio(bcm, flags);
  99. assert(bcm->initialized);
  100. err = bcm43xx_sprom_read(bcm, sprom);
  101. if (!err)
  102. err = sprom2hex(sprom, buf, PAGE_SIZE);
  103. bcm43xx_unlock_mmio(bcm, flags);
  104. kfree(sprom);
  105. return err;
  106. }
  107. static ssize_t bcm43xx_attr_sprom_store(struct device *dev,
  108. struct device_attribute *attr,
  109. const char *buf, size_t count)
  110. {
  111. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  112. u16 *sprom;
  113. unsigned long flags;
  114. int err;
  115. if (!capable(CAP_NET_ADMIN))
  116. return -EPERM;
  117. sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
  118. GFP_KERNEL);
  119. if (!sprom)
  120. return -ENOMEM;
  121. err = hex2sprom(sprom, buf, count);
  122. if (err)
  123. goto out_kfree;
  124. bcm43xx_lock_mmio(bcm, flags);
  125. assert(bcm->initialized);
  126. err = bcm43xx_sprom_write(bcm, sprom);
  127. bcm43xx_unlock_mmio(bcm, flags);
  128. out_kfree:
  129. kfree(sprom);
  130. return err ? err : count;
  131. }
  132. static DEVICE_ATTR(sprom, 0600,
  133. bcm43xx_attr_sprom_show,
  134. bcm43xx_attr_sprom_store);
  135. static ssize_t bcm43xx_attr_interfmode_show(struct device *dev,
  136. struct device_attribute *attr,
  137. char *buf)
  138. {
  139. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  140. unsigned long flags;
  141. int err;
  142. ssize_t count = 0;
  143. if (!capable(CAP_NET_ADMIN))
  144. return -EPERM;
  145. bcm43xx_lock(bcm, flags);
  146. assert(bcm->initialized);
  147. switch (bcm43xx_current_radio(bcm)->interfmode) {
  148. case BCM43xx_RADIO_INTERFMODE_NONE:
  149. count = snprintf(buf, PAGE_SIZE, "0 (No Interference Mitigation)\n");
  150. break;
  151. case BCM43xx_RADIO_INTERFMODE_NONWLAN:
  152. count = snprintf(buf, PAGE_SIZE, "1 (Non-WLAN Interference Mitigation)\n");
  153. break;
  154. case BCM43xx_RADIO_INTERFMODE_MANUALWLAN:
  155. count = snprintf(buf, PAGE_SIZE, "2 (WLAN Interference Mitigation)\n");
  156. break;
  157. default:
  158. assert(0);
  159. }
  160. err = 0;
  161. bcm43xx_unlock(bcm, flags);
  162. return err ? err : count;
  163. }
  164. static ssize_t bcm43xx_attr_interfmode_store(struct device *dev,
  165. struct device_attribute *attr,
  166. const char *buf, size_t count)
  167. {
  168. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  169. unsigned long flags;
  170. int err;
  171. int mode;
  172. if (!capable(CAP_NET_ADMIN))
  173. return -EPERM;
  174. mode = get_integer(buf, count);
  175. switch (mode) {
  176. case 0:
  177. mode = BCM43xx_RADIO_INTERFMODE_NONE;
  178. break;
  179. case 1:
  180. mode = BCM43xx_RADIO_INTERFMODE_NONWLAN;
  181. break;
  182. case 2:
  183. mode = BCM43xx_RADIO_INTERFMODE_MANUALWLAN;
  184. break;
  185. case 3:
  186. mode = BCM43xx_RADIO_INTERFMODE_AUTOWLAN;
  187. break;
  188. default:
  189. return -EINVAL;
  190. }
  191. bcm43xx_lock_mmio(bcm, flags);
  192. assert(bcm->initialized);
  193. err = bcm43xx_radio_set_interference_mitigation(bcm, mode);
  194. if (err) {
  195. printk(KERN_ERR PFX "Interference Mitigation not "
  196. "supported by device\n");
  197. }
  198. bcm43xx_unlock_mmio(bcm, flags);
  199. return err ? err : count;
  200. }
  201. static DEVICE_ATTR(interference, 0644,
  202. bcm43xx_attr_interfmode_show,
  203. bcm43xx_attr_interfmode_store);
  204. static ssize_t bcm43xx_attr_preamble_show(struct device *dev,
  205. struct device_attribute *attr,
  206. char *buf)
  207. {
  208. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  209. unsigned long flags;
  210. int err;
  211. ssize_t count;
  212. if (!capable(CAP_NET_ADMIN))
  213. return -EPERM;
  214. bcm43xx_lock(bcm, flags);
  215. assert(bcm->initialized);
  216. if (bcm->short_preamble)
  217. count = snprintf(buf, PAGE_SIZE, "1 (Short Preamble enabled)\n");
  218. else
  219. count = snprintf(buf, PAGE_SIZE, "0 (Short Preamble disabled)\n");
  220. err = 0;
  221. bcm43xx_unlock(bcm, flags);
  222. return err ? err : count;
  223. }
  224. static ssize_t bcm43xx_attr_preamble_store(struct device *dev,
  225. struct device_attribute *attr,
  226. const char *buf, size_t count)
  227. {
  228. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  229. unsigned long flags;
  230. int err;
  231. int value;
  232. if (!capable(CAP_NET_ADMIN))
  233. return -EPERM;
  234. value = get_boolean(buf, count);
  235. if (value < 0)
  236. return value;
  237. bcm43xx_lock(bcm, flags);
  238. assert(bcm->initialized);
  239. bcm->short_preamble = !!value;
  240. err = 0;
  241. bcm43xx_unlock(bcm, flags);
  242. return err ? err : count;
  243. }
  244. static DEVICE_ATTR(shortpreamble, 0644,
  245. bcm43xx_attr_preamble_show,
  246. bcm43xx_attr_preamble_store);
  247. int bcm43xx_sysfs_register(struct bcm43xx_private *bcm)
  248. {
  249. struct device *dev = &bcm->pci_dev->dev;
  250. int err;
  251. assert(bcm->initialized);
  252. err = device_create_file(dev, &dev_attr_sprom);
  253. if (err)
  254. goto out;
  255. err = device_create_file(dev, &dev_attr_interference);
  256. if (err)
  257. goto err_remove_sprom;
  258. err = device_create_file(dev, &dev_attr_shortpreamble);
  259. if (err)
  260. goto err_remove_interfmode;
  261. out:
  262. return err;
  263. err_remove_interfmode:
  264. device_remove_file(dev, &dev_attr_interference);
  265. err_remove_sprom:
  266. device_remove_file(dev, &dev_attr_sprom);
  267. goto out;
  268. }
  269. void bcm43xx_sysfs_unregister(struct bcm43xx_private *bcm)
  270. {
  271. struct device *dev = &bcm->pci_dev->dev;
  272. device_remove_file(dev, &dev_attr_shortpreamble);
  273. device_remove_file(dev, &dev_attr_interference);
  274. device_remove_file(dev, &dev_attr_sprom);
  275. }