bcm43xx_sysfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. mutex_lock(&bcm->mutex);
  99. spin_lock_irqsave(&bcm->irq_lock, flags);
  100. err = bcm43xx_sprom_read(bcm, sprom);
  101. if (!err)
  102. err = sprom2hex(sprom, buf, PAGE_SIZE);
  103. mmiowb();
  104. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  105. mutex_unlock(&bcm->mutex);
  106. kfree(sprom);
  107. return err;
  108. }
  109. static ssize_t bcm43xx_attr_sprom_store(struct device *dev,
  110. struct device_attribute *attr,
  111. const char *buf, size_t count)
  112. {
  113. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  114. u16 *sprom;
  115. unsigned long flags;
  116. int err;
  117. if (!capable(CAP_NET_ADMIN))
  118. return -EPERM;
  119. sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
  120. GFP_KERNEL);
  121. if (!sprom)
  122. return -ENOMEM;
  123. err = hex2sprom(sprom, buf, count);
  124. if (err)
  125. goto out_kfree;
  126. mutex_lock(&bcm->mutex);
  127. spin_lock_irqsave(&bcm->irq_lock, flags);
  128. spin_lock(&bcm->leds_lock);
  129. err = bcm43xx_sprom_write(bcm, sprom);
  130. mmiowb();
  131. spin_unlock(&bcm->leds_lock);
  132. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  133. mutex_unlock(&bcm->mutex);
  134. out_kfree:
  135. kfree(sprom);
  136. return err ? err : count;
  137. }
  138. static DEVICE_ATTR(sprom, 0600,
  139. bcm43xx_attr_sprom_show,
  140. bcm43xx_attr_sprom_store);
  141. static ssize_t bcm43xx_attr_interfmode_show(struct device *dev,
  142. struct device_attribute *attr,
  143. char *buf)
  144. {
  145. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  146. ssize_t count = 0;
  147. if (!capable(CAP_NET_ADMIN))
  148. return -EPERM;
  149. mutex_lock(&bcm->mutex);
  150. switch (bcm43xx_current_radio(bcm)->interfmode) {
  151. case BCM43xx_RADIO_INTERFMODE_NONE:
  152. count = snprintf(buf, PAGE_SIZE, "0 (No Interference Mitigation)\n");
  153. break;
  154. case BCM43xx_RADIO_INTERFMODE_NONWLAN:
  155. count = snprintf(buf, PAGE_SIZE, "1 (Non-WLAN Interference Mitigation)\n");
  156. break;
  157. case BCM43xx_RADIO_INTERFMODE_MANUALWLAN:
  158. count = snprintf(buf, PAGE_SIZE, "2 (WLAN Interference Mitigation)\n");
  159. break;
  160. default:
  161. assert(0);
  162. }
  163. mutex_unlock(&bcm->mutex);
  164. return count;
  165. }
  166. static ssize_t bcm43xx_attr_interfmode_store(struct device *dev,
  167. struct device_attribute *attr,
  168. const char *buf, size_t count)
  169. {
  170. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  171. unsigned long flags;
  172. int err;
  173. int mode;
  174. if (!capable(CAP_NET_ADMIN))
  175. return -EPERM;
  176. mode = get_integer(buf, count);
  177. switch (mode) {
  178. case 0:
  179. mode = BCM43xx_RADIO_INTERFMODE_NONE;
  180. break;
  181. case 1:
  182. mode = BCM43xx_RADIO_INTERFMODE_NONWLAN;
  183. break;
  184. case 2:
  185. mode = BCM43xx_RADIO_INTERFMODE_MANUALWLAN;
  186. break;
  187. case 3:
  188. mode = BCM43xx_RADIO_INTERFMODE_AUTOWLAN;
  189. break;
  190. default:
  191. return -EINVAL;
  192. }
  193. mutex_lock(&bcm->mutex);
  194. spin_lock_irqsave(&bcm->irq_lock, flags);
  195. err = bcm43xx_radio_set_interference_mitigation(bcm, mode);
  196. if (err) {
  197. printk(KERN_ERR PFX "Interference Mitigation not "
  198. "supported by device\n");
  199. }
  200. mmiowb();
  201. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  202. mutex_unlock(&bcm->mutex);
  203. return err ? err : count;
  204. }
  205. static DEVICE_ATTR(interference, 0644,
  206. bcm43xx_attr_interfmode_show,
  207. bcm43xx_attr_interfmode_store);
  208. static ssize_t bcm43xx_attr_preamble_show(struct device *dev,
  209. struct device_attribute *attr,
  210. char *buf)
  211. {
  212. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  213. ssize_t count;
  214. if (!capable(CAP_NET_ADMIN))
  215. return -EPERM;
  216. mutex_lock(&bcm->mutex);
  217. if (bcm->short_preamble)
  218. count = snprintf(buf, PAGE_SIZE, "1 (Short Preamble enabled)\n");
  219. else
  220. count = snprintf(buf, PAGE_SIZE, "0 (Short Preamble disabled)\n");
  221. mutex_unlock(&bcm->mutex);
  222. return 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 value;
  231. if (!capable(CAP_NET_ADMIN))
  232. return -EPERM;
  233. value = get_boolean(buf, count);
  234. if (value < 0)
  235. return value;
  236. mutex_lock(&bcm->mutex);
  237. spin_lock_irqsave(&bcm->irq_lock, flags);
  238. bcm->short_preamble = !!value;
  239. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  240. mutex_unlock(&bcm->mutex);
  241. return count;
  242. }
  243. static DEVICE_ATTR(shortpreamble, 0644,
  244. bcm43xx_attr_preamble_show,
  245. bcm43xx_attr_preamble_store);
  246. static ssize_t bcm43xx_attr_phymode_store(struct device *dev,
  247. struct device_attribute *attr,
  248. const char *buf, size_t count)
  249. {
  250. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  251. int phytype;
  252. int err = -EINVAL;
  253. if (count < 1)
  254. goto out;
  255. switch (buf[0]) {
  256. case 'a': case 'A':
  257. phytype = BCM43xx_PHYTYPE_A;
  258. break;
  259. case 'b': case 'B':
  260. phytype = BCM43xx_PHYTYPE_B;
  261. break;
  262. case 'g': case 'G':
  263. phytype = BCM43xx_PHYTYPE_G;
  264. break;
  265. default:
  266. goto out;
  267. }
  268. bcm43xx_periodic_tasks_delete(bcm);
  269. mutex_lock(&(bcm)->mutex);
  270. err = bcm43xx_select_wireless_core(bcm, phytype);
  271. if (!err)
  272. bcm43xx_periodic_tasks_setup(bcm);
  273. mutex_unlock(&(bcm)->mutex);
  274. if (err == -ESRCH)
  275. err = -ENODEV;
  276. out:
  277. return err ? err : count;
  278. }
  279. static ssize_t bcm43xx_attr_phymode_show(struct device *dev,
  280. struct device_attribute *attr,
  281. char *buf)
  282. {
  283. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  284. ssize_t count = 0;
  285. mutex_lock(&(bcm)->mutex);
  286. switch (bcm43xx_current_phy(bcm)->type) {
  287. case BCM43xx_PHYTYPE_A:
  288. snprintf(buf, PAGE_SIZE, "A");
  289. break;
  290. case BCM43xx_PHYTYPE_B:
  291. snprintf(buf, PAGE_SIZE, "B");
  292. break;
  293. case BCM43xx_PHYTYPE_G:
  294. snprintf(buf, PAGE_SIZE, "G");
  295. break;
  296. default:
  297. assert(0);
  298. }
  299. mutex_unlock(&(bcm)->mutex);
  300. return count;
  301. }
  302. static DEVICE_ATTR(phymode, 0644,
  303. bcm43xx_attr_phymode_show,
  304. bcm43xx_attr_phymode_store);
  305. static ssize_t bcm43xx_attr_microcode_show(struct device *dev,
  306. struct device_attribute *attr,
  307. char *buf)
  308. {
  309. unsigned long flags;
  310. struct bcm43xx_private *bcm = dev_to_bcm(dev);
  311. ssize_t count = 0;
  312. u16 status;
  313. if (!capable(CAP_NET_ADMIN))
  314. return -EPERM;
  315. mutex_lock(&(bcm)->mutex);
  316. spin_lock_irqsave(&bcm->irq_lock, flags);
  317. status = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
  318. BCM43xx_UCODE_STATUS);
  319. spin_unlock_irqrestore(&bcm->irq_lock, flags);
  320. mutex_unlock(&(bcm)->mutex);
  321. switch (status) {
  322. case 0x0000:
  323. count = snprintf(buf, PAGE_SIZE, "0x%.4x (invalid)\n",
  324. status);
  325. break;
  326. case 0x0001:
  327. count = snprintf(buf, PAGE_SIZE, "0x%.4x (init)\n",
  328. status);
  329. break;
  330. case 0x0002:
  331. count = snprintf(buf, PAGE_SIZE, "0x%.4x (active)\n",
  332. status);
  333. break;
  334. case 0x0003:
  335. count = snprintf(buf, PAGE_SIZE, "0x%.4x (suspended)\n",
  336. status);
  337. break;
  338. case 0x0004:
  339. count = snprintf(buf, PAGE_SIZE, "0x%.4x (asleep)\n",
  340. status);
  341. break;
  342. default:
  343. count = snprintf(buf, PAGE_SIZE, "0x%.4x (unknown)\n",
  344. status);
  345. break;
  346. }
  347. return count;
  348. }
  349. static DEVICE_ATTR(microcodestatus, 0444,
  350. bcm43xx_attr_microcode_show,
  351. NULL);
  352. int bcm43xx_sysfs_register(struct bcm43xx_private *bcm)
  353. {
  354. struct device *dev = &bcm->pci_dev->dev;
  355. int err;
  356. assert(bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED);
  357. err = device_create_file(dev, &dev_attr_sprom);
  358. if (err)
  359. goto out;
  360. err = device_create_file(dev, &dev_attr_interference);
  361. if (err)
  362. goto err_remove_sprom;
  363. err = device_create_file(dev, &dev_attr_shortpreamble);
  364. if (err)
  365. goto err_remove_interfmode;
  366. err = device_create_file(dev, &dev_attr_phymode);
  367. if (err)
  368. goto err_remove_shortpreamble;
  369. err = device_create_file(dev, &dev_attr_microcodestatus);
  370. if (err)
  371. goto err_remove_phymode;
  372. out:
  373. return err;
  374. err_remove_phymode:
  375. device_remove_file(dev, &dev_attr_phymode);
  376. err_remove_shortpreamble:
  377. device_remove_file(dev, &dev_attr_shortpreamble);
  378. err_remove_interfmode:
  379. device_remove_file(dev, &dev_attr_interference);
  380. err_remove_sprom:
  381. device_remove_file(dev, &dev_attr_sprom);
  382. goto out;
  383. }
  384. void bcm43xx_sysfs_unregister(struct bcm43xx_private *bcm)
  385. {
  386. struct device *dev = &bcm->pci_dev->dev;
  387. device_remove_file(dev, &dev_attr_microcodestatus);
  388. device_remove_file(dev, &dev_attr_phymode);
  389. device_remove_file(dev, &dev_attr_shortpreamble);
  390. device_remove_file(dev, &dev_attr_interference);
  391. device_remove_file(dev, &dev_attr_sprom);
  392. }