Pull bugzilla-9429 into release branch
[pv_ops_mirror.git] / drivers / net / wireless / b43 / sysfs.c
blobf4faff6a7d6c43e4a862988edf914ecbc44ce3cd
1 /*
3 Broadcom B43 wireless driver
5 SYSFS support routines
7 Copyright (c) 2006 Michael Buesch <mb@bu3sch.de>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
22 Boston, MA 02110-1301, USA.
26 #include <linux/capability.h>
27 #include <linux/io.h>
29 #include "b43.h"
30 #include "sysfs.h"
31 #include "main.h"
32 #include "phy.h"
34 #define GENERIC_FILESIZE 64
36 static int get_integer(const char *buf, size_t count)
38 char tmp[10 + 1] = { 0 };
39 int ret = -EINVAL;
41 if (count == 0)
42 goto out;
43 count = min(count, (size_t) 10);
44 memcpy(tmp, buf, count);
45 ret = simple_strtol(tmp, NULL, 10);
46 out:
47 return ret;
50 static int get_boolean(const char *buf, size_t count)
52 if (count != 0) {
53 if (buf[0] == '1')
54 return 1;
55 if (buf[0] == '0')
56 return 0;
57 if (count >= 4 && memcmp(buf, "true", 4) == 0)
58 return 1;
59 if (count >= 5 && memcmp(buf, "false", 5) == 0)
60 return 0;
61 if (count >= 3 && memcmp(buf, "yes", 3) == 0)
62 return 1;
63 if (count >= 2 && memcmp(buf, "no", 2) == 0)
64 return 0;
65 if (count >= 2 && memcmp(buf, "on", 2) == 0)
66 return 1;
67 if (count >= 3 && memcmp(buf, "off", 3) == 0)
68 return 0;
70 return -EINVAL;
73 static ssize_t b43_attr_interfmode_show(struct device *dev,
74 struct device_attribute *attr,
75 char *buf)
77 struct b43_wldev *wldev = dev_to_b43_wldev(dev);
78 ssize_t count = 0;
80 if (!capable(CAP_NET_ADMIN))
81 return -EPERM;
83 mutex_lock(&wldev->wl->mutex);
85 switch (wldev->phy.interfmode) {
86 case B43_INTERFMODE_NONE:
87 count =
88 snprintf(buf, PAGE_SIZE,
89 "0 (No Interference Mitigation)\n");
90 break;
91 case B43_INTERFMODE_NONWLAN:
92 count =
93 snprintf(buf, PAGE_SIZE,
94 "1 (Non-WLAN Interference Mitigation)\n");
95 break;
96 case B43_INTERFMODE_MANUALWLAN:
97 count =
98 snprintf(buf, PAGE_SIZE,
99 "2 (WLAN Interference Mitigation)\n");
100 break;
101 default:
102 B43_WARN_ON(1);
105 mutex_unlock(&wldev->wl->mutex);
107 return count;
110 static ssize_t b43_attr_interfmode_store(struct device *dev,
111 struct device_attribute *attr,
112 const char *buf, size_t count)
114 struct b43_wldev *wldev = dev_to_b43_wldev(dev);
115 unsigned long flags;
116 int err;
117 int mode;
119 if (!capable(CAP_NET_ADMIN))
120 return -EPERM;
122 mode = get_integer(buf, count);
123 switch (mode) {
124 case 0:
125 mode = B43_INTERFMODE_NONE;
126 break;
127 case 1:
128 mode = B43_INTERFMODE_NONWLAN;
129 break;
130 case 2:
131 mode = B43_INTERFMODE_MANUALWLAN;
132 break;
133 case 3:
134 mode = B43_INTERFMODE_AUTOWLAN;
135 break;
136 default:
137 return -EINVAL;
140 mutex_lock(&wldev->wl->mutex);
141 spin_lock_irqsave(&wldev->wl->irq_lock, flags);
143 err = b43_radio_set_interference_mitigation(wldev, mode);
144 if (err) {
145 b43err(wldev->wl, "Interference Mitigation not "
146 "supported by device\n");
148 mmiowb();
149 spin_unlock_irqrestore(&wldev->wl->irq_lock, flags);
150 mutex_unlock(&wldev->wl->mutex);
152 return err ? err : count;
155 static DEVICE_ATTR(interference, 0644,
156 b43_attr_interfmode_show, b43_attr_interfmode_store);
158 static ssize_t b43_attr_preamble_show(struct device *dev,
159 struct device_attribute *attr, char *buf)
161 struct b43_wldev *wldev = dev_to_b43_wldev(dev);
162 ssize_t count;
164 if (!capable(CAP_NET_ADMIN))
165 return -EPERM;
167 mutex_lock(&wldev->wl->mutex);
169 if (wldev->short_preamble)
170 count =
171 snprintf(buf, PAGE_SIZE, "1 (Short Preamble enabled)\n");
172 else
173 count =
174 snprintf(buf, PAGE_SIZE, "0 (Short Preamble disabled)\n");
176 mutex_unlock(&wldev->wl->mutex);
178 return count;
181 static ssize_t b43_attr_preamble_store(struct device *dev,
182 struct device_attribute *attr,
183 const char *buf, size_t count)
185 struct b43_wldev *wldev = dev_to_b43_wldev(dev);
186 unsigned long flags;
187 int value;
189 if (!capable(CAP_NET_ADMIN))
190 return -EPERM;
192 value = get_boolean(buf, count);
193 if (value < 0)
194 return value;
195 mutex_lock(&wldev->wl->mutex);
196 spin_lock_irqsave(&wldev->wl->irq_lock, flags);
198 wldev->short_preamble = !!value;
200 spin_unlock_irqrestore(&wldev->wl->irq_lock, flags);
201 mutex_unlock(&wldev->wl->mutex);
203 return count;
206 static DEVICE_ATTR(shortpreamble, 0644,
207 b43_attr_preamble_show, b43_attr_preamble_store);
209 int b43_sysfs_register(struct b43_wldev *wldev)
211 struct device *dev = wldev->dev->dev;
212 int err;
214 B43_WARN_ON(b43_status(wldev) != B43_STAT_INITIALIZED);
216 err = device_create_file(dev, &dev_attr_interference);
217 if (err)
218 goto out;
219 err = device_create_file(dev, &dev_attr_shortpreamble);
220 if (err)
221 goto err_remove_interfmode;
223 out:
224 return err;
225 err_remove_interfmode:
226 device_remove_file(dev, &dev_attr_interference);
227 goto out;
230 void b43_sysfs_unregister(struct b43_wldev *wldev)
232 struct device *dev = wldev->dev->dev;
234 device_remove_file(dev, &dev_attr_shortpreamble);
235 device_remove_file(dev, &dev_attr_interference);