Pull bugzilla-5737 into release branch
[linux-2.6] / drivers / net / wireless / bcm43xx / bcm43xx_sysfs.c
1 /*
2
3   Broadcom BCM43xx wireless driver
4
5   SYSFS support routines
6
7   Copyright (c) 2006 Michael Buesch <mbuesch@freenet.de>
8
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.
13
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.
18
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.
23
24 */
25
26 #include "bcm43xx_sysfs.h"
27 #include "bcm43xx.h"
28 #include "bcm43xx_main.h"
29 #include "bcm43xx_radio.h"
30
31 #include <linux/capability.h>
32
33
34 #define GENERIC_FILESIZE        64
35
36
37 static int get_integer(const char *buf, size_t count)
38 {
39         char tmp[10 + 1] = { 0 };
40         int ret = -EINVAL;
41
42         if (count == 0)
43                 goto out;
44         count = min(count, (size_t)10);
45         memcpy(tmp, buf, count);
46         ret = simple_strtol(tmp, NULL, 10);
47 out:
48         return ret;
49 }
50
51 static int get_boolean(const char *buf, size_t count)
52 {
53         if (count != 0) {
54                 if (buf[0] == '1')
55                         return 1;
56                 if (buf[0] == '0')
57                         return 0;
58                 if (count >= 4 && memcmp(buf, "true", 4) == 0)
59                         return 1;
60                 if (count >= 5 && memcmp(buf, "false", 5) == 0)
61                         return 0;
62                 if (count >= 3 && memcmp(buf, "yes", 3) == 0)
63                         return 1;
64                 if (count >= 2 && memcmp(buf, "no", 2) == 0)
65                         return 0;
66                 if (count >= 2 && memcmp(buf, "on", 2) == 0)
67                         return 1;
68                 if (count >= 3 && memcmp(buf, "off", 3) == 0)
69                         return 0;
70         }
71         return -EINVAL;
72 }
73
74 static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len)
75 {
76         int i, pos = 0;
77
78         for (i = 0; i < BCM43xx_SPROM_SIZE; i++) {
79                 pos += snprintf(buf + pos, buf_len - pos - 1,
80                                 "%04X", swab16(sprom[i]) & 0xFFFF);
81         }
82         pos += snprintf(buf + pos, buf_len - pos - 1, "\n");
83
84         return pos + 1;
85 }
86
87 static int hex2sprom(u16 *sprom, const char *dump, size_t len)
88 {
89         char tmp[5] = { 0 };
90         int cnt = 0;
91         unsigned long parsed;
92
93         if (len < BCM43xx_SPROM_SIZE * sizeof(u16) * 2)
94                 return -EINVAL;
95
96         while (cnt < BCM43xx_SPROM_SIZE) {
97                 memcpy(tmp, dump, 4);
98                 dump += 4;
99                 parsed = simple_strtoul(tmp, NULL, 16);
100                 sprom[cnt++] = swab16((u16)parsed);
101         }
102
103         return 0;
104 }
105
106 static ssize_t bcm43xx_attr_sprom_show(struct device *dev,
107                                        struct device_attribute *attr,
108                                        char *buf)
109 {
110         struct bcm43xx_private *bcm = dev_to_bcm(dev);
111         u16 *sprom;
112         unsigned long flags;
113         int err;
114
115         if (!capable(CAP_NET_ADMIN))
116                 return -EPERM;
117
118         assert(BCM43xx_SPROM_SIZE * sizeof(u16) <= PAGE_SIZE);
119         sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
120                         GFP_KERNEL);
121         if (!sprom)
122                 return -ENOMEM;
123         bcm43xx_lock_mmio(bcm, flags);
124         assert(bcm->initialized);
125         err = bcm43xx_sprom_read(bcm, sprom);
126         if (!err)
127                 err = sprom2hex(sprom, buf, PAGE_SIZE);
128         bcm43xx_unlock_mmio(bcm, flags);
129         kfree(sprom);
130
131         return err;
132 }
133
134 static ssize_t bcm43xx_attr_sprom_store(struct device *dev,
135                                         struct device_attribute *attr,
136                                         const char *buf, size_t count)
137 {
138         struct bcm43xx_private *bcm = dev_to_bcm(dev);
139         u16 *sprom;
140         unsigned long flags;
141         int err;
142
143         if (!capable(CAP_NET_ADMIN))
144                 return -EPERM;
145
146         sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
147                         GFP_KERNEL);
148         if (!sprom)
149                 return -ENOMEM;
150         err = hex2sprom(sprom, buf, count);
151         if (err)
152                 goto out_kfree;
153         bcm43xx_lock_mmio(bcm, flags);
154         assert(bcm->initialized);
155         err = bcm43xx_sprom_write(bcm, sprom);
156         bcm43xx_unlock_mmio(bcm, flags);
157 out_kfree:
158         kfree(sprom);
159
160         return err ? err : count;
161
162 }
163
164 static DEVICE_ATTR(sprom, 0600,
165                    bcm43xx_attr_sprom_show,
166                    bcm43xx_attr_sprom_store);
167
168 static ssize_t bcm43xx_attr_interfmode_show(struct device *dev,
169                                             struct device_attribute *attr,
170                                             char *buf)
171 {
172         struct bcm43xx_private *bcm = dev_to_bcm(dev);
173         unsigned long flags;
174         int err;
175         ssize_t count = 0;
176
177         if (!capable(CAP_NET_ADMIN))
178                 return -EPERM;
179
180         bcm43xx_lock(bcm, flags);
181         assert(bcm->initialized);
182
183         switch (bcm43xx_current_radio(bcm)->interfmode) {
184         case BCM43xx_RADIO_INTERFMODE_NONE:
185                 count = snprintf(buf, PAGE_SIZE, "0 (No Interference Mitigation)\n");
186                 break;
187         case BCM43xx_RADIO_INTERFMODE_NONWLAN:
188                 count = snprintf(buf, PAGE_SIZE, "1 (Non-WLAN Interference Mitigation)\n");
189                 break;
190         case BCM43xx_RADIO_INTERFMODE_MANUALWLAN:
191                 count = snprintf(buf, PAGE_SIZE, "2 (WLAN Interference Mitigation)\n");
192                 break;
193         default:
194                 assert(0);
195         }
196         err = 0;
197
198         bcm43xx_unlock(bcm, flags);
199
200         return err ? err : count;
201
202 }
203
204 static ssize_t bcm43xx_attr_interfmode_store(struct device *dev,
205                                              struct device_attribute *attr,
206                                              const char *buf, size_t count)
207 {
208         struct bcm43xx_private *bcm = dev_to_bcm(dev);
209         unsigned long flags;
210         int err;
211         int mode;
212
213         if (!capable(CAP_NET_ADMIN))
214                 return -EPERM;
215
216         mode = get_integer(buf, count);
217         switch (mode) {
218         case 0:
219                 mode = BCM43xx_RADIO_INTERFMODE_NONE;
220                 break;
221         case 1:
222                 mode = BCM43xx_RADIO_INTERFMODE_NONWLAN;
223                 break;
224         case 2:
225                 mode = BCM43xx_RADIO_INTERFMODE_MANUALWLAN;
226                 break;
227         case 3:
228                 mode = BCM43xx_RADIO_INTERFMODE_AUTOWLAN;
229                 break;
230         default:
231                 return -EINVAL;
232         }
233
234         bcm43xx_lock_mmio(bcm, flags);
235         assert(bcm->initialized);
236
237         err = bcm43xx_radio_set_interference_mitigation(bcm, mode);
238         if (err) {
239                 printk(KERN_ERR PFX "Interference Mitigation not "
240                                     "supported by device\n");
241         }
242
243         bcm43xx_unlock_mmio(bcm, flags);
244
245         return err ? err : count;
246 }
247
248 static DEVICE_ATTR(interference, 0644,
249                    bcm43xx_attr_interfmode_show,
250                    bcm43xx_attr_interfmode_store);
251
252 static ssize_t bcm43xx_attr_preamble_show(struct device *dev,
253                                           struct device_attribute *attr,
254                                           char *buf)
255 {
256         struct bcm43xx_private *bcm = dev_to_bcm(dev);
257         unsigned long flags;
258         int err;
259         ssize_t count;
260
261         if (!capable(CAP_NET_ADMIN))
262                 return -EPERM;
263
264         bcm43xx_lock(bcm, flags);
265         assert(bcm->initialized);
266
267         if (bcm->short_preamble)
268                 count = snprintf(buf, PAGE_SIZE, "1 (Short Preamble enabled)\n");
269         else
270                 count = snprintf(buf, PAGE_SIZE, "0 (Short Preamble disabled)\n");
271
272         err = 0;
273         bcm43xx_unlock(bcm, flags);
274
275         return err ? err : count;
276 }
277
278 static ssize_t bcm43xx_attr_preamble_store(struct device *dev,
279                                            struct device_attribute *attr,
280                                            const char *buf, size_t count)
281 {
282         struct bcm43xx_private *bcm = dev_to_bcm(dev);
283         unsigned long flags;
284         int err;
285         int value;
286
287         if (!capable(CAP_NET_ADMIN))
288                 return -EPERM;
289
290         value = get_boolean(buf, count);
291         if (value < 0)
292                 return value;
293         bcm43xx_lock(bcm, flags);
294         assert(bcm->initialized);
295
296         bcm->short_preamble = !!value;
297
298         err = 0;
299         bcm43xx_unlock(bcm, flags);
300
301         return err ? err : count;
302 }
303
304 static DEVICE_ATTR(shortpreamble, 0644,
305                    bcm43xx_attr_preamble_show,
306                    bcm43xx_attr_preamble_store);
307
308 int bcm43xx_sysfs_register(struct bcm43xx_private *bcm)
309 {
310         struct device *dev = &bcm->pci_dev->dev;
311         int err;
312
313         assert(bcm->initialized);
314
315         err = device_create_file(dev, &dev_attr_sprom);
316         if (err)
317                 goto out;
318         err = device_create_file(dev, &dev_attr_interference);
319         if (err)
320                 goto err_remove_sprom;
321         err = device_create_file(dev, &dev_attr_shortpreamble);
322         if (err)
323                 goto err_remove_interfmode;
324
325 out:
326         return err;
327 err_remove_interfmode:
328         device_remove_file(dev, &dev_attr_interference);
329 err_remove_sprom:
330         device_remove_file(dev, &dev_attr_sprom);
331         goto out;
332 }
333
334 void bcm43xx_sysfs_unregister(struct bcm43xx_private *bcm)
335 {
336         struct device *dev = &bcm->pci_dev->dev;
337
338         device_remove_file(dev, &dev_attr_shortpreamble);
339         device_remove_file(dev, &dev_attr_interference);
340         device_remove_file(dev, &dev_attr_sprom);
341 }