blob: b438f48e891d185c2933828ee2b9080de3097564 [file] [log] [blame]
Michael Buesch367f8992006-02-28 15:32:19 +01001/*
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
37static 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);
47out:
48 return ret;
49}
50
51static 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
Michael Bueschb35d6492006-04-13 02:32:58 +020074static 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
87static 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
Michael Buesch367f8992006-02-28 15:32:19 +0100106static ssize_t bcm43xx_attr_sprom_show(struct device *dev,
107 struct device_attribute *attr,
108 char *buf)
109{
Michael Bueschb35d6492006-04-13 02:32:58 +0200110 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100111 u16 *sprom;
112 unsigned long flags;
Michael Bueschb35d6492006-04-13 02:32:58 +0200113 int err;
Michael Buesch367f8992006-02-28 15:32:19 +0100114
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;
Michael Bueschefccb642006-03-11 13:39:14 +0100123 bcm43xx_lock_mmio(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100124 assert(bcm->initialized);
125 err = bcm43xx_sprom_read(bcm, sprom);
Michael Bueschb35d6492006-04-13 02:32:58 +0200126 if (!err)
127 err = sprom2hex(sprom, buf, PAGE_SIZE);
Michael Bueschefccb642006-03-11 13:39:14 +0100128 bcm43xx_unlock_mmio(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100129 kfree(sprom);
130
Michael Bueschb35d6492006-04-13 02:32:58 +0200131 return err;
Michael Buesch367f8992006-02-28 15:32:19 +0100132}
133
134static ssize_t bcm43xx_attr_sprom_store(struct device *dev,
135 struct device_attribute *attr,
136 const char *buf, size_t count)
137{
Michael Bueschb35d6492006-04-13 02:32:58 +0200138 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100139 u16 *sprom;
140 unsigned long flags;
Michael Bueschb35d6492006-04-13 02:32:58 +0200141 int err;
Michael Buesch367f8992006-02-28 15:32:19 +0100142
143 if (!capable(CAP_NET_ADMIN))
144 return -EPERM;
145
Michael Buesch367f8992006-02-28 15:32:19 +0100146 sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
147 GFP_KERNEL);
148 if (!sprom)
149 return -ENOMEM;
Michael Bueschb35d6492006-04-13 02:32:58 +0200150 err = hex2sprom(sprom, buf, count);
151 if (err)
152 goto out_kfree;
Michael Bueschefccb642006-03-11 13:39:14 +0100153 bcm43xx_lock_mmio(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100154 assert(bcm->initialized);
155 err = bcm43xx_sprom_write(bcm, sprom);
Michael Bueschefccb642006-03-11 13:39:14 +0100156 bcm43xx_unlock_mmio(bcm, flags);
Michael Bueschb35d6492006-04-13 02:32:58 +0200157out_kfree:
Michael Buesch367f8992006-02-28 15:32:19 +0100158 kfree(sprom);
159
160 return err ? err : count;
161
162}
163
Michael Bueschb35d6492006-04-13 02:32:58 +0200164static DEVICE_ATTR(sprom, 0600,
165 bcm43xx_attr_sprom_show,
166 bcm43xx_attr_sprom_store);
167
Michael Buesch367f8992006-02-28 15:32:19 +0100168static ssize_t bcm43xx_attr_interfmode_show(struct device *dev,
169 struct device_attribute *attr,
170 char *buf)
171{
Michael Bueschb35d6492006-04-13 02:32:58 +0200172 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100173 unsigned long flags;
174 int err;
175 ssize_t count = 0;
176
177 if (!capable(CAP_NET_ADMIN))
178 return -EPERM;
179
Michael Bueschefccb642006-03-11 13:39:14 +0100180 bcm43xx_lock(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100181 assert(bcm->initialized);
182
Michael Buesche9357c02006-03-13 19:27:34 +0100183 switch (bcm43xx_current_radio(bcm)->interfmode) {
Michael Buesch367f8992006-02-28 15:32:19 +0100184 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
Michael Bueschefccb642006-03-11 13:39:14 +0100198 bcm43xx_unlock(bcm, flags);
199
Michael Buesch367f8992006-02-28 15:32:19 +0100200 return err ? err : count;
201
202}
203
204static ssize_t bcm43xx_attr_interfmode_store(struct device *dev,
205 struct device_attribute *attr,
206 const char *buf, size_t count)
207{
Michael Bueschb35d6492006-04-13 02:32:58 +0200208 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100209 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
Michael Bueschefccb642006-03-11 13:39:14 +0100234 bcm43xx_lock_mmio(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100235 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
Michael Bueschefccb642006-03-11 13:39:14 +0100243 bcm43xx_unlock_mmio(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100244
245 return err ? err : count;
246}
247
Michael Bueschb35d6492006-04-13 02:32:58 +0200248static DEVICE_ATTR(interference, 0644,
249 bcm43xx_attr_interfmode_show,
250 bcm43xx_attr_interfmode_store);
251
Michael Buesch367f8992006-02-28 15:32:19 +0100252static ssize_t bcm43xx_attr_preamble_show(struct device *dev,
253 struct device_attribute *attr,
254 char *buf)
255{
Michael Bueschb35d6492006-04-13 02:32:58 +0200256 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100257 unsigned long flags;
258 int err;
259 ssize_t count;
260
261 if (!capable(CAP_NET_ADMIN))
262 return -EPERM;
263
Michael Bueschefccb642006-03-11 13:39:14 +0100264 bcm43xx_lock(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100265 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;
Michael Bueschefccb642006-03-11 13:39:14 +0100273 bcm43xx_unlock(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100274
275 return err ? err : count;
276}
277
278static ssize_t bcm43xx_attr_preamble_store(struct device *dev,
279 struct device_attribute *attr,
280 const char *buf, size_t count)
281{
Michael Bueschb35d6492006-04-13 02:32:58 +0200282 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100283 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;
Michael Bueschefccb642006-03-11 13:39:14 +0100293 bcm43xx_lock(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100294 assert(bcm->initialized);
295
296 bcm->short_preamble = !!value;
297
298 err = 0;
Michael Bueschefccb642006-03-11 13:39:14 +0100299 bcm43xx_unlock(bcm, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100300
301 return err ? err : count;
302}
303
Michael Bueschb35d6492006-04-13 02:32:58 +0200304static DEVICE_ATTR(shortpreamble, 0644,
305 bcm43xx_attr_preamble_show,
306 bcm43xx_attr_preamble_store);
307
Michael Buesch367f8992006-02-28 15:32:19 +0100308int bcm43xx_sysfs_register(struct bcm43xx_private *bcm)
309{
310 struct device *dev = &bcm->pci_dev->dev;
Michael Buesch367f8992006-02-28 15:32:19 +0100311 int err;
312
313 assert(bcm->initialized);
314
Michael Bueschb35d6492006-04-13 02:32:58 +0200315 err = device_create_file(dev, &dev_attr_sprom);
Michael Buesch367f8992006-02-28 15:32:19 +0100316 if (err)
317 goto out;
Michael Bueschb35d6492006-04-13 02:32:58 +0200318 err = device_create_file(dev, &dev_attr_interference);
Michael Buesch367f8992006-02-28 15:32:19 +0100319 if (err)
320 goto err_remove_sprom;
Michael Bueschb35d6492006-04-13 02:32:58 +0200321 err = device_create_file(dev, &dev_attr_shortpreamble);
Michael Buesch367f8992006-02-28 15:32:19 +0100322 if (err)
323 goto err_remove_interfmode;
324
325out:
326 return err;
327err_remove_interfmode:
Michael Bueschb35d6492006-04-13 02:32:58 +0200328 device_remove_file(dev, &dev_attr_interference);
Michael Buesch367f8992006-02-28 15:32:19 +0100329err_remove_sprom:
Michael Bueschb35d6492006-04-13 02:32:58 +0200330 device_remove_file(dev, &dev_attr_sprom);
Michael Buesch367f8992006-02-28 15:32:19 +0100331 goto out;
332}
333
334void bcm43xx_sysfs_unregister(struct bcm43xx_private *bcm)
335{
336 struct device *dev = &bcm->pci_dev->dev;
Michael Buesch367f8992006-02-28 15:32:19 +0100337
Michael Bueschb35d6492006-04-13 02:32:58 +0200338 device_remove_file(dev, &dev_attr_shortpreamble);
339 device_remove_file(dev, &dev_attr_interference);
340 device_remove_file(dev, &dev_attr_sprom);
Michael Buesch367f8992006-02-28 15:32:19 +0100341}