blob: b2bc5a9b6245f2c9ff1371a765cbcdbda986e911 [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 Bueschefa6a372006-06-27 21:38:40 +0200123 mutex_lock(&bcm->mutex);
124 spin_lock_irqsave(&bcm->irq_lock, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100125 err = bcm43xx_sprom_read(bcm, sprom);
Michael Bueschb35d6492006-04-13 02:32:58 +0200126 if (!err)
127 err = sprom2hex(sprom, buf, PAGE_SIZE);
Michael Buesch78ff56a2006-06-05 20:24:10 +0200128 mmiowb();
Michael Bueschefa6a372006-06-27 21:38:40 +0200129 spin_unlock_irqrestore(&bcm->irq_lock, flags);
130 mutex_unlock(&bcm->mutex);
Michael Buesch367f8992006-02-28 15:32:19 +0100131 kfree(sprom);
132
Michael Bueschb35d6492006-04-13 02:32:58 +0200133 return err;
Michael Buesch367f8992006-02-28 15:32:19 +0100134}
135
136static ssize_t bcm43xx_attr_sprom_store(struct device *dev,
137 struct device_attribute *attr,
138 const char *buf, size_t count)
139{
Michael Bueschb35d6492006-04-13 02:32:58 +0200140 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100141 u16 *sprom;
142 unsigned long flags;
Michael Bueschb35d6492006-04-13 02:32:58 +0200143 int err;
Michael Buesch367f8992006-02-28 15:32:19 +0100144
145 if (!capable(CAP_NET_ADMIN))
146 return -EPERM;
147
Michael Buesch367f8992006-02-28 15:32:19 +0100148 sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom),
149 GFP_KERNEL);
150 if (!sprom)
151 return -ENOMEM;
Michael Bueschb35d6492006-04-13 02:32:58 +0200152 err = hex2sprom(sprom, buf, count);
153 if (err)
154 goto out_kfree;
Michael Bueschefa6a372006-06-27 21:38:40 +0200155 mutex_lock(&bcm->mutex);
156 spin_lock_irqsave(&bcm->irq_lock, flags);
157 spin_lock(&bcm->leds_lock);
Michael Buesch367f8992006-02-28 15:32:19 +0100158 err = bcm43xx_sprom_write(bcm, sprom);
Michael Buesch78ff56a2006-06-05 20:24:10 +0200159 mmiowb();
Michael Bueschefa6a372006-06-27 21:38:40 +0200160 spin_unlock(&bcm->leds_lock);
161 spin_unlock_irqrestore(&bcm->irq_lock, flags);
162 mutex_unlock(&bcm->mutex);
Michael Bueschb35d6492006-04-13 02:32:58 +0200163out_kfree:
Michael Buesch367f8992006-02-28 15:32:19 +0100164 kfree(sprom);
165
166 return err ? err : count;
167
168}
169
Michael Bueschb35d6492006-04-13 02:32:58 +0200170static DEVICE_ATTR(sprom, 0600,
171 bcm43xx_attr_sprom_show,
172 bcm43xx_attr_sprom_store);
173
Michael Buesch367f8992006-02-28 15:32:19 +0100174static ssize_t bcm43xx_attr_interfmode_show(struct device *dev,
175 struct device_attribute *attr,
176 char *buf)
177{
Michael Bueschb35d6492006-04-13 02:32:58 +0200178 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100179 int err;
180 ssize_t count = 0;
181
182 if (!capable(CAP_NET_ADMIN))
183 return -EPERM;
184
Michael Bueschefa6a372006-06-27 21:38:40 +0200185 mutex_lock(&bcm->mutex);
Michael Buesch367f8992006-02-28 15:32:19 +0100186
Michael Buesche9357c02006-03-13 19:27:34 +0100187 switch (bcm43xx_current_radio(bcm)->interfmode) {
Michael Buesch367f8992006-02-28 15:32:19 +0100188 case BCM43xx_RADIO_INTERFMODE_NONE:
189 count = snprintf(buf, PAGE_SIZE, "0 (No Interference Mitigation)\n");
190 break;
191 case BCM43xx_RADIO_INTERFMODE_NONWLAN:
192 count = snprintf(buf, PAGE_SIZE, "1 (Non-WLAN Interference Mitigation)\n");
193 break;
194 case BCM43xx_RADIO_INTERFMODE_MANUALWLAN:
195 count = snprintf(buf, PAGE_SIZE, "2 (WLAN Interference Mitigation)\n");
196 break;
197 default:
198 assert(0);
199 }
200 err = 0;
201
Michael Bueschefa6a372006-06-27 21:38:40 +0200202 mutex_unlock(&bcm->mutex);
Michael Bueschefccb642006-03-11 13:39:14 +0100203
Michael Buesch367f8992006-02-28 15:32:19 +0100204 return err ? err : count;
205
206}
207
208static ssize_t bcm43xx_attr_interfmode_store(struct device *dev,
209 struct device_attribute *attr,
210 const char *buf, size_t count)
211{
Michael Bueschb35d6492006-04-13 02:32:58 +0200212 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100213 unsigned long flags;
214 int err;
215 int mode;
216
217 if (!capable(CAP_NET_ADMIN))
218 return -EPERM;
219
220 mode = get_integer(buf, count);
221 switch (mode) {
222 case 0:
223 mode = BCM43xx_RADIO_INTERFMODE_NONE;
224 break;
225 case 1:
226 mode = BCM43xx_RADIO_INTERFMODE_NONWLAN;
227 break;
228 case 2:
229 mode = BCM43xx_RADIO_INTERFMODE_MANUALWLAN;
230 break;
231 case 3:
232 mode = BCM43xx_RADIO_INTERFMODE_AUTOWLAN;
233 break;
234 default:
235 return -EINVAL;
236 }
237
Michael Bueschefa6a372006-06-27 21:38:40 +0200238 mutex_lock(&bcm->mutex);
239 spin_lock_irqsave(&bcm->irq_lock, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100240
241 err = bcm43xx_radio_set_interference_mitigation(bcm, mode);
242 if (err) {
243 printk(KERN_ERR PFX "Interference Mitigation not "
244 "supported by device\n");
245 }
Michael Buesch78ff56a2006-06-05 20:24:10 +0200246 mmiowb();
Michael Bueschefa6a372006-06-27 21:38:40 +0200247 spin_unlock_irqrestore(&bcm->irq_lock, flags);
248 mutex_unlock(&bcm->mutex);
Michael Buesch367f8992006-02-28 15:32:19 +0100249
250 return err ? err : count;
251}
252
Michael Bueschb35d6492006-04-13 02:32:58 +0200253static DEVICE_ATTR(interference, 0644,
254 bcm43xx_attr_interfmode_show,
255 bcm43xx_attr_interfmode_store);
256
Michael Buesch367f8992006-02-28 15:32:19 +0100257static ssize_t bcm43xx_attr_preamble_show(struct device *dev,
258 struct device_attribute *attr,
259 char *buf)
260{
Michael Bueschb35d6492006-04-13 02:32:58 +0200261 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100262 int err;
263 ssize_t count;
264
265 if (!capable(CAP_NET_ADMIN))
266 return -EPERM;
267
Michael Bueschefa6a372006-06-27 21:38:40 +0200268 mutex_lock(&bcm->mutex);
Michael Buesch367f8992006-02-28 15:32:19 +0100269
270 if (bcm->short_preamble)
271 count = snprintf(buf, PAGE_SIZE, "1 (Short Preamble enabled)\n");
272 else
273 count = snprintf(buf, PAGE_SIZE, "0 (Short Preamble disabled)\n");
274
275 err = 0;
Michael Bueschefa6a372006-06-27 21:38:40 +0200276 mutex_unlock(&bcm->mutex);
Michael Buesch367f8992006-02-28 15:32:19 +0100277
278 return err ? err : count;
279}
280
281static ssize_t bcm43xx_attr_preamble_store(struct device *dev,
282 struct device_attribute *attr,
283 const char *buf, size_t count)
284{
Michael Bueschb35d6492006-04-13 02:32:58 +0200285 struct bcm43xx_private *bcm = dev_to_bcm(dev);
Michael Buesch367f8992006-02-28 15:32:19 +0100286 unsigned long flags;
287 int err;
288 int value;
289
290 if (!capable(CAP_NET_ADMIN))
291 return -EPERM;
292
293 value = get_boolean(buf, count);
294 if (value < 0)
295 return value;
Michael Bueschefa6a372006-06-27 21:38:40 +0200296 mutex_lock(&bcm->mutex);
297 spin_lock_irqsave(&bcm->irq_lock, flags);
Michael Buesch367f8992006-02-28 15:32:19 +0100298
299 bcm->short_preamble = !!value;
300
301 err = 0;
Michael Bueschefa6a372006-06-27 21:38:40 +0200302 spin_unlock_irqrestore(&bcm->irq_lock, flags);
303 mutex_unlock(&bcm->mutex);
Michael Buesch367f8992006-02-28 15:32:19 +0100304
305 return err ? err : count;
306}
307
Michael Bueschb35d6492006-04-13 02:32:58 +0200308static DEVICE_ATTR(shortpreamble, 0644,
309 bcm43xx_attr_preamble_show,
310 bcm43xx_attr_preamble_store);
311
Michael Buesch58e55282006-07-08 22:02:18 +0200312static ssize_t bcm43xx_attr_phymode_store(struct device *dev,
313 struct device_attribute *attr,
314 const char *buf, size_t count)
315{
316 struct bcm43xx_private *bcm = dev_to_bcm(dev);
317 int phytype;
318 int err = -EINVAL;
319
320 if (count < 1)
321 goto out;
322 switch (buf[0]) {
323 case 'a': case 'A':
324 phytype = BCM43xx_PHYTYPE_A;
325 break;
326 case 'b': case 'B':
327 phytype = BCM43xx_PHYTYPE_B;
328 break;
329 case 'g': case 'G':
330 phytype = BCM43xx_PHYTYPE_G;
331 break;
332 default:
333 goto out;
334 }
335
Larry Finger7d4b0392006-08-21 09:43:44 -0500336 bcm43xx_periodic_tasks_delete(bcm);
John W. Linvillef1207ba2006-07-27 18:10:00 -0400337 mutex_lock(&(bcm)->mutex);
Michael Buesch58e55282006-07-08 22:02:18 +0200338 err = bcm43xx_select_wireless_core(bcm, phytype);
Larry Finger7d4b0392006-08-21 09:43:44 -0500339 if (!err)
340 bcm43xx_periodic_tasks_setup(bcm);
John W. Linvillef1207ba2006-07-27 18:10:00 -0400341 mutex_unlock(&(bcm)->mutex);
Michael Buesch58e55282006-07-08 22:02:18 +0200342 if (err == -ESRCH)
343 err = -ENODEV;
344
345out:
346 return err ? err : count;
347}
348
349static ssize_t bcm43xx_attr_phymode_show(struct device *dev,
350 struct device_attribute *attr,
351 char *buf)
352{
353 struct bcm43xx_private *bcm = dev_to_bcm(dev);
354 ssize_t count = 0;
355
John W. Linvillef1207ba2006-07-27 18:10:00 -0400356 mutex_lock(&(bcm)->mutex);
Michael Buesch58e55282006-07-08 22:02:18 +0200357 switch (bcm43xx_current_phy(bcm)->type) {
358 case BCM43xx_PHYTYPE_A:
359 snprintf(buf, PAGE_SIZE, "A");
360 break;
361 case BCM43xx_PHYTYPE_B:
362 snprintf(buf, PAGE_SIZE, "B");
363 break;
364 case BCM43xx_PHYTYPE_G:
365 snprintf(buf, PAGE_SIZE, "G");
366 break;
367 default:
368 assert(0);
369 }
John W. Linvillef1207ba2006-07-27 18:10:00 -0400370 mutex_unlock(&(bcm)->mutex);
Michael Buesch58e55282006-07-08 22:02:18 +0200371
372 return count;
373}
374
375static DEVICE_ATTR(phymode, 0644,
376 bcm43xx_attr_phymode_show,
377 bcm43xx_attr_phymode_store);
378
Larry Finger87d26322006-09-07 10:12:11 -0500379static ssize_t bcm43xx_attr_microcode_show(struct device *dev,
380 struct device_attribute *attr,
381 char *buf)
382{
383 unsigned long flags;
384 struct bcm43xx_private *bcm = dev_to_bcm(dev);
385 ssize_t count = 0;
386 u16 status;
387
388 if (!capable(CAP_NET_ADMIN))
389 return -EPERM;
390
391 mutex_lock(&(bcm)->mutex);
392 spin_lock_irqsave(&bcm->irq_lock, flags);
393 status = bcm43xx_shm_read16(bcm, BCM43xx_SHM_SHARED,
394 BCM43xx_UCODE_STATUS);
395
396 spin_unlock_irqrestore(&bcm->irq_lock, flags);
397 mutex_unlock(&(bcm)->mutex);
398 switch (status) {
399 case 0x0000:
400 count = snprintf(buf, PAGE_SIZE, "0x%.4x (invalid)\n",
401 status);
402 break;
403 case 0x0001:
404 count = snprintf(buf, PAGE_SIZE, "0x%.4x (init)\n",
405 status);
406 break;
407 case 0x0002:
408 count = snprintf(buf, PAGE_SIZE, "0x%.4x (active)\n",
409 status);
410 break;
411 case 0x0003:
412 count = snprintf(buf, PAGE_SIZE, "0x%.4x (suspended)\n",
413 status);
414 break;
415 case 0x0004:
416 count = snprintf(buf, PAGE_SIZE, "0x%.4x (asleep)\n",
417 status);
418 break;
419 default:
420 count = snprintf(buf, PAGE_SIZE, "0x%.4x (unknown)\n",
421 status);
422 break;
423 }
424
425 return count;
426}
427
428static DEVICE_ATTR(microcodestatus, 0444,
429 bcm43xx_attr_microcode_show,
430 NULL);
431
Michael Buesch367f8992006-02-28 15:32:19 +0100432int bcm43xx_sysfs_register(struct bcm43xx_private *bcm)
433{
434 struct device *dev = &bcm->pci_dev->dev;
Michael Buesch367f8992006-02-28 15:32:19 +0100435 int err;
436
Michael Buesch78ff56a2006-06-05 20:24:10 +0200437 assert(bcm43xx_status(bcm) == BCM43xx_STAT_INITIALIZED);
Michael Buesch367f8992006-02-28 15:32:19 +0100438
Michael Bueschb35d6492006-04-13 02:32:58 +0200439 err = device_create_file(dev, &dev_attr_sprom);
Michael Buesch367f8992006-02-28 15:32:19 +0100440 if (err)
441 goto out;
Michael Bueschb35d6492006-04-13 02:32:58 +0200442 err = device_create_file(dev, &dev_attr_interference);
Michael Buesch367f8992006-02-28 15:32:19 +0100443 if (err)
444 goto err_remove_sprom;
Michael Bueschb35d6492006-04-13 02:32:58 +0200445 err = device_create_file(dev, &dev_attr_shortpreamble);
Michael Buesch367f8992006-02-28 15:32:19 +0100446 if (err)
447 goto err_remove_interfmode;
Michael Buesch58e55282006-07-08 22:02:18 +0200448 err = device_create_file(dev, &dev_attr_phymode);
449 if (err)
450 goto err_remove_shortpreamble;
Larry Finger87d26322006-09-07 10:12:11 -0500451 err = device_create_file(dev, &dev_attr_microcodestatus);
452 if (err)
453 goto err_remove_phymode;
Michael Buesch367f8992006-02-28 15:32:19 +0100454
455out:
456 return err;
Larry Finger87d26322006-09-07 10:12:11 -0500457err_remove_phymode:
458 device_remove_file(dev, &dev_attr_phymode);
Michael Buesch58e55282006-07-08 22:02:18 +0200459err_remove_shortpreamble:
460 device_remove_file(dev, &dev_attr_shortpreamble);
Michael Buesch367f8992006-02-28 15:32:19 +0100461err_remove_interfmode:
Michael Bueschb35d6492006-04-13 02:32:58 +0200462 device_remove_file(dev, &dev_attr_interference);
Michael Buesch367f8992006-02-28 15:32:19 +0100463err_remove_sprom:
Michael Bueschb35d6492006-04-13 02:32:58 +0200464 device_remove_file(dev, &dev_attr_sprom);
Michael Buesch367f8992006-02-28 15:32:19 +0100465 goto out;
466}
467
468void bcm43xx_sysfs_unregister(struct bcm43xx_private *bcm)
469{
470 struct device *dev = &bcm->pci_dev->dev;
Michael Buesch367f8992006-02-28 15:32:19 +0100471
Larry Finger87d26322006-09-07 10:12:11 -0500472 device_remove_file(dev, &dev_attr_microcodestatus);
Michael Buesch58e55282006-07-08 22:02:18 +0200473 device_remove_file(dev, &dev_attr_phymode);
Michael Bueschb35d6492006-04-13 02:32:58 +0200474 device_remove_file(dev, &dev_attr_shortpreamble);
475 device_remove_file(dev, &dev_attr_interference);
476 device_remove_file(dev, &dev_attr_sprom);
Michael Buesch367f8992006-02-28 15:32:19 +0100477}