Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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 | |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 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 | |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 106 | static ssize_t bcm43xx_attr_sprom_show(struct device *dev, |
| 107 | struct device_attribute *attr, |
| 108 | char *buf) |
| 109 | { |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 110 | struct bcm43xx_private *bcm = dev_to_bcm(dev); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 111 | u16 *sprom; |
| 112 | unsigned long flags; |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 113 | int err; |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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; |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 123 | bcm43xx_lock_mmio(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 124 | assert(bcm->initialized); |
| 125 | err = bcm43xx_sprom_read(bcm, sprom); |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 126 | if (!err) |
| 127 | err = sprom2hex(sprom, buf, PAGE_SIZE); |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 128 | bcm43xx_unlock_mmio(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 129 | kfree(sprom); |
| 130 | |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 131 | return err; |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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 | { |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 138 | struct bcm43xx_private *bcm = dev_to_bcm(dev); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 139 | u16 *sprom; |
| 140 | unsigned long flags; |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 141 | int err; |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 142 | |
| 143 | if (!capable(CAP_NET_ADMIN)) |
| 144 | return -EPERM; |
| 145 | |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 146 | sprom = kmalloc(BCM43xx_SPROM_SIZE * sizeof(*sprom), |
| 147 | GFP_KERNEL); |
| 148 | if (!sprom) |
| 149 | return -ENOMEM; |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 150 | err = hex2sprom(sprom, buf, count); |
| 151 | if (err) |
| 152 | goto out_kfree; |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 153 | bcm43xx_lock_mmio(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 154 | assert(bcm->initialized); |
| 155 | err = bcm43xx_sprom_write(bcm, sprom); |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 156 | bcm43xx_unlock_mmio(bcm, flags); |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 157 | out_kfree: |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 158 | kfree(sprom); |
| 159 | |
| 160 | return err ? err : count; |
| 161 | |
| 162 | } |
| 163 | |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 164 | static DEVICE_ATTR(sprom, 0600, |
| 165 | bcm43xx_attr_sprom_show, |
| 166 | bcm43xx_attr_sprom_store); |
| 167 | |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 168 | static ssize_t bcm43xx_attr_interfmode_show(struct device *dev, |
| 169 | struct device_attribute *attr, |
| 170 | char *buf) |
| 171 | { |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 172 | struct bcm43xx_private *bcm = dev_to_bcm(dev); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 173 | unsigned long flags; |
| 174 | int err; |
| 175 | ssize_t count = 0; |
| 176 | |
| 177 | if (!capable(CAP_NET_ADMIN)) |
| 178 | return -EPERM; |
| 179 | |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 180 | bcm43xx_lock(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 181 | assert(bcm->initialized); |
| 182 | |
Michael Buesch | e9357c0 | 2006-03-13 19:27:34 +0100 | [diff] [blame] | 183 | switch (bcm43xx_current_radio(bcm)->interfmode) { |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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 | |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 198 | bcm43xx_unlock(bcm, flags); |
| 199 | |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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 | { |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 208 | struct bcm43xx_private *bcm = dev_to_bcm(dev); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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 | |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 234 | bcm43xx_lock_mmio(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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 | |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 243 | bcm43xx_unlock_mmio(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 244 | |
| 245 | return err ? err : count; |
| 246 | } |
| 247 | |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 248 | static DEVICE_ATTR(interference, 0644, |
| 249 | bcm43xx_attr_interfmode_show, |
| 250 | bcm43xx_attr_interfmode_store); |
| 251 | |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 252 | static ssize_t bcm43xx_attr_preamble_show(struct device *dev, |
| 253 | struct device_attribute *attr, |
| 254 | char *buf) |
| 255 | { |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 256 | struct bcm43xx_private *bcm = dev_to_bcm(dev); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 257 | unsigned long flags; |
| 258 | int err; |
| 259 | ssize_t count; |
| 260 | |
| 261 | if (!capable(CAP_NET_ADMIN)) |
| 262 | return -EPERM; |
| 263 | |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 264 | bcm43xx_lock(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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; |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 273 | bcm43xx_unlock(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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 | { |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 282 | struct bcm43xx_private *bcm = dev_to_bcm(dev); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 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; |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 293 | bcm43xx_lock(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 294 | assert(bcm->initialized); |
| 295 | |
| 296 | bcm->short_preamble = !!value; |
| 297 | |
| 298 | err = 0; |
Michael Buesch | efccb64 | 2006-03-11 13:39:14 +0100 | [diff] [blame] | 299 | bcm43xx_unlock(bcm, flags); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 300 | |
| 301 | return err ? err : count; |
| 302 | } |
| 303 | |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 304 | static DEVICE_ATTR(shortpreamble, 0644, |
| 305 | bcm43xx_attr_preamble_show, |
| 306 | bcm43xx_attr_preamble_store); |
| 307 | |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 308 | int bcm43xx_sysfs_register(struct bcm43xx_private *bcm) |
| 309 | { |
| 310 | struct device *dev = &bcm->pci_dev->dev; |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 311 | int err; |
| 312 | |
| 313 | assert(bcm->initialized); |
| 314 | |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 315 | err = device_create_file(dev, &dev_attr_sprom); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 316 | if (err) |
| 317 | goto out; |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 318 | err = device_create_file(dev, &dev_attr_interference); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 319 | if (err) |
| 320 | goto err_remove_sprom; |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 321 | err = device_create_file(dev, &dev_attr_shortpreamble); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 322 | if (err) |
| 323 | goto err_remove_interfmode; |
| 324 | |
| 325 | out: |
| 326 | return err; |
| 327 | err_remove_interfmode: |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 328 | device_remove_file(dev, &dev_attr_interference); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 329 | err_remove_sprom: |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 330 | device_remove_file(dev, &dev_attr_sprom); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 331 | goto out; |
| 332 | } |
| 333 | |
| 334 | void bcm43xx_sysfs_unregister(struct bcm43xx_private *bcm) |
| 335 | { |
| 336 | struct device *dev = &bcm->pci_dev->dev; |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 337 | |
Michael Buesch | b35d649 | 2006-04-13 02:32:58 +0200 | [diff] [blame^] | 338 | device_remove_file(dev, &dev_attr_shortpreamble); |
| 339 | device_remove_file(dev, &dev_attr_interference); |
| 340 | device_remove_file(dev, &dev_attr_sprom); |
Michael Buesch | 367f899 | 2006-02-28 15:32:19 +0100 | [diff] [blame] | 341 | } |