Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2004 - 2007 rt2x00 SourceForge Project |
| 3 | <http://rt2x00.serialmonkey.com> |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the |
| 17 | Free Software Foundation, Inc., |
| 18 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | Module: rt2x00lib |
| 23 | Abstract: rt2x00 debugfs specific routines. |
| 24 | */ |
| 25 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 26 | #include <linux/debugfs.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/uaccess.h> |
| 30 | |
| 31 | #include "rt2x00.h" |
| 32 | #include "rt2x00lib.h" |
| 33 | |
| 34 | #define PRINT_LINE_LEN_MAX 32 |
| 35 | |
| 36 | struct rt2x00debug_intf { |
| 37 | /* |
| 38 | * Pointer to driver structure where |
| 39 | * this debugfs entry belongs to. |
| 40 | */ |
| 41 | struct rt2x00_dev *rt2x00dev; |
| 42 | |
| 43 | /* |
| 44 | * Reference to the rt2x00debug structure |
| 45 | * which can be used to communicate with |
| 46 | * the registers. |
| 47 | */ |
| 48 | const struct rt2x00debug *debug; |
| 49 | |
| 50 | /* |
| 51 | * Debugfs entries for: |
| 52 | * - driver folder |
| 53 | * - driver file |
| 54 | * - chipset file |
Ivo van Doorn | 643b252 | 2007-09-25 20:13:51 +0200 | [diff] [blame] | 55 | * - device flags file |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 56 | * - register offset/value files |
| 57 | * - eeprom offset/value files |
| 58 | * - bbp offset/value files |
| 59 | * - rf offset/value files |
| 60 | */ |
| 61 | struct dentry *driver_folder; |
| 62 | struct dentry *driver_entry; |
| 63 | struct dentry *chipset_entry; |
Ivo van Doorn | 643b252 | 2007-09-25 20:13:51 +0200 | [diff] [blame] | 64 | struct dentry *dev_flags; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 65 | struct dentry *csr_off_entry; |
| 66 | struct dentry *csr_val_entry; |
| 67 | struct dentry *eeprom_off_entry; |
| 68 | struct dentry *eeprom_val_entry; |
| 69 | struct dentry *bbp_off_entry; |
| 70 | struct dentry *bbp_val_entry; |
| 71 | struct dentry *rf_off_entry; |
| 72 | struct dentry *rf_val_entry; |
| 73 | |
| 74 | /* |
| 75 | * Driver and chipset files will use a data buffer |
| 76 | * that has been created in advance. This will simplify |
| 77 | * the code since we can use the debugfs functions. |
| 78 | */ |
| 79 | struct debugfs_blob_wrapper driver_blob; |
| 80 | struct debugfs_blob_wrapper chipset_blob; |
| 81 | |
| 82 | /* |
| 83 | * Requested offset for each register type. |
| 84 | */ |
| 85 | unsigned int offset_csr; |
| 86 | unsigned int offset_eeprom; |
| 87 | unsigned int offset_bbp; |
| 88 | unsigned int offset_rf; |
| 89 | }; |
| 90 | |
| 91 | static int rt2x00debug_file_open(struct inode *inode, struct file *file) |
| 92 | { |
| 93 | struct rt2x00debug_intf *intf = inode->i_private; |
| 94 | |
| 95 | file->private_data = inode->i_private; |
| 96 | |
| 97 | if (!try_module_get(intf->debug->owner)) |
| 98 | return -EBUSY; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int rt2x00debug_file_release(struct inode *inode, struct file *file) |
| 104 | { |
| 105 | struct rt2x00debug_intf *intf = file->private_data; |
| 106 | |
| 107 | module_put(intf->debug->owner); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \ |
| 113 | static ssize_t rt2x00debug_read_##__name(struct file *file, \ |
| 114 | char __user *buf, \ |
| 115 | size_t length, \ |
| 116 | loff_t *offset) \ |
| 117 | { \ |
| 118 | struct rt2x00debug_intf *intf = file->private_data; \ |
| 119 | const struct rt2x00debug *debug = intf->debug; \ |
| 120 | char line[16]; \ |
| 121 | size_t size; \ |
| 122 | __type value; \ |
| 123 | \ |
| 124 | if (*offset) \ |
| 125 | return 0; \ |
| 126 | \ |
| 127 | if (intf->offset_##__name >= debug->__name.word_count) \ |
| 128 | return -EINVAL; \ |
| 129 | \ |
| 130 | debug->__name.read(intf->rt2x00dev, \ |
| 131 | intf->offset_##__name, &value); \ |
| 132 | \ |
| 133 | size = sprintf(line, __format, value); \ |
| 134 | \ |
| 135 | if (copy_to_user(buf, line, size)) \ |
| 136 | return -EFAULT; \ |
| 137 | \ |
| 138 | *offset += size; \ |
| 139 | return size; \ |
| 140 | } |
| 141 | |
| 142 | #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \ |
| 143 | static ssize_t rt2x00debug_write_##__name(struct file *file, \ |
| 144 | const char __user *buf,\ |
| 145 | size_t length, \ |
| 146 | loff_t *offset) \ |
| 147 | { \ |
| 148 | struct rt2x00debug_intf *intf = file->private_data; \ |
| 149 | const struct rt2x00debug *debug = intf->debug; \ |
| 150 | char line[16]; \ |
| 151 | size_t size; \ |
| 152 | __type value; \ |
| 153 | \ |
| 154 | if (*offset) \ |
| 155 | return 0; \ |
| 156 | \ |
| 157 | if (!capable(CAP_NET_ADMIN)) \ |
| 158 | return -EPERM; \ |
| 159 | \ |
| 160 | if (intf->offset_##__name >= debug->__name.word_count) \ |
| 161 | return -EINVAL; \ |
| 162 | \ |
| 163 | if (copy_from_user(line, buf, length)) \ |
| 164 | return -EFAULT; \ |
| 165 | \ |
| 166 | size = strlen(line); \ |
| 167 | value = simple_strtoul(line, NULL, 0); \ |
| 168 | \ |
| 169 | debug->__name.write(intf->rt2x00dev, \ |
| 170 | intf->offset_##__name, value); \ |
| 171 | \ |
| 172 | *offset += size; \ |
| 173 | return size; \ |
| 174 | } |
| 175 | |
| 176 | #define RT2X00DEBUGFS_OPS(__name, __format, __type) \ |
| 177 | RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \ |
| 178 | RT2X00DEBUGFS_OPS_WRITE(__name, __type); \ |
| 179 | \ |
| 180 | static const struct file_operations rt2x00debug_fop_##__name = {\ |
| 181 | .owner = THIS_MODULE, \ |
| 182 | .read = rt2x00debug_read_##__name, \ |
| 183 | .write = rt2x00debug_write_##__name, \ |
| 184 | .open = rt2x00debug_file_open, \ |
| 185 | .release = rt2x00debug_file_release, \ |
| 186 | }; |
| 187 | |
| 188 | RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32); |
| 189 | RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16); |
| 190 | RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8); |
| 191 | RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32); |
| 192 | |
Ivo van Doorn | 643b252 | 2007-09-25 20:13:51 +0200 | [diff] [blame] | 193 | static ssize_t rt2x00debug_read_dev_flags(struct file *file, |
| 194 | char __user *buf, |
| 195 | size_t length, |
| 196 | loff_t *offset) |
| 197 | { |
| 198 | struct rt2x00debug_intf *intf = file->private_data; |
| 199 | char line[16]; |
| 200 | size_t size; |
| 201 | |
| 202 | if (*offset) |
| 203 | return 0; |
| 204 | |
| 205 | size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags); |
| 206 | |
| 207 | if (copy_to_user(buf, line, size)) |
| 208 | return -EFAULT; |
| 209 | |
| 210 | *offset += size; |
| 211 | return size; |
| 212 | } |
| 213 | |
| 214 | static const struct file_operations rt2x00debug_fop_dev_flags = { |
| 215 | .owner = THIS_MODULE, |
| 216 | .read = rt2x00debug_read_dev_flags, |
| 217 | .open = rt2x00debug_file_open, |
| 218 | .release = rt2x00debug_file_release, |
| 219 | }; |
| 220 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 221 | static struct dentry *rt2x00debug_create_file_driver(const char *name, |
| 222 | struct rt2x00debug_intf |
| 223 | *intf, |
| 224 | struct debugfs_blob_wrapper |
| 225 | *blob) |
| 226 | { |
| 227 | char *data; |
| 228 | |
| 229 | data = kzalloc(3 * PRINT_LINE_LEN_MAX, GFP_KERNEL); |
| 230 | if (!data) |
| 231 | return NULL; |
| 232 | |
| 233 | blob->data = data; |
| 234 | data += sprintf(data, "driver: %s\n", intf->rt2x00dev->ops->name); |
| 235 | data += sprintf(data, "version: %s\n", DRV_VERSION); |
| 236 | data += sprintf(data, "compiled: %s %s\n", __DATE__, __TIME__); |
| 237 | blob->size = strlen(blob->data); |
| 238 | |
| 239 | return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob); |
| 240 | } |
| 241 | |
| 242 | static struct dentry *rt2x00debug_create_file_chipset(const char *name, |
| 243 | struct rt2x00debug_intf |
| 244 | *intf, |
| 245 | struct |
| 246 | debugfs_blob_wrapper |
| 247 | *blob) |
| 248 | { |
| 249 | const struct rt2x00debug *debug = intf->debug; |
| 250 | char *data; |
| 251 | |
| 252 | data = kzalloc(4 * PRINT_LINE_LEN_MAX, GFP_KERNEL); |
| 253 | if (!data) |
| 254 | return NULL; |
| 255 | |
| 256 | blob->data = data; |
| 257 | data += sprintf(data, "csr length: %d\n", debug->csr.word_count); |
| 258 | data += sprintf(data, "eeprom length: %d\n", debug->eeprom.word_count); |
| 259 | data += sprintf(data, "bbp length: %d\n", debug->bbp.word_count); |
| 260 | data += sprintf(data, "rf length: %d\n", debug->rf.word_count); |
| 261 | blob->size = strlen(blob->data); |
| 262 | |
| 263 | return debugfs_create_blob(name, S_IRUGO, intf->driver_folder, blob); |
| 264 | } |
| 265 | |
| 266 | void rt2x00debug_register(struct rt2x00_dev *rt2x00dev) |
| 267 | { |
| 268 | const struct rt2x00debug *debug = rt2x00dev->ops->debugfs; |
| 269 | struct rt2x00debug_intf *intf; |
| 270 | |
| 271 | intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL); |
| 272 | if (!intf) { |
| 273 | ERROR(rt2x00dev, "Failed to allocate debug handler.\n"); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | intf->debug = debug; |
| 278 | intf->rt2x00dev = rt2x00dev; |
| 279 | rt2x00dev->debugfs_intf = intf; |
| 280 | |
| 281 | intf->driver_folder = |
| 282 | debugfs_create_dir(intf->rt2x00dev->ops->name, |
| 283 | rt2x00dev->hw->wiphy->debugfsdir); |
| 284 | if (IS_ERR(intf->driver_folder)) |
| 285 | goto exit; |
| 286 | |
| 287 | intf->driver_entry = |
| 288 | rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob); |
| 289 | if (IS_ERR(intf->driver_entry)) |
| 290 | goto exit; |
| 291 | |
| 292 | intf->chipset_entry = |
| 293 | rt2x00debug_create_file_chipset("chipset", |
| 294 | intf, &intf->chipset_blob); |
| 295 | if (IS_ERR(intf->chipset_entry)) |
| 296 | goto exit; |
| 297 | |
Ivo van Doorn | 643b252 | 2007-09-25 20:13:51 +0200 | [diff] [blame] | 298 | intf->dev_flags = debugfs_create_file("dev_flags", S_IRUGO, |
| 299 | intf->driver_folder, intf, |
| 300 | &rt2x00debug_fop_dev_flags); |
| 301 | if (IS_ERR(intf->dev_flags)) |
| 302 | goto exit; |
| 303 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 304 | #define RT2X00DEBUGFS_CREATE_ENTRY(__intf, __name) \ |
| 305 | ({ \ |
| 306 | (__intf)->__name##_off_entry = \ |
| 307 | debugfs_create_u32(__stringify(__name) "_offset", \ |
| 308 | S_IRUGO | S_IWUSR, \ |
| 309 | (__intf)->driver_folder, \ |
| 310 | &(__intf)->offset_##__name); \ |
| 311 | if (IS_ERR((__intf)->__name##_off_entry)) \ |
| 312 | goto exit; \ |
| 313 | \ |
| 314 | (__intf)->__name##_val_entry = \ |
| 315 | debugfs_create_file(__stringify(__name) "_value", \ |
| 316 | S_IRUGO | S_IWUSR, \ |
| 317 | (__intf)->driver_folder, \ |
| 318 | (__intf), &rt2x00debug_fop_##__name);\ |
| 319 | if (IS_ERR((__intf)->__name##_val_entry)) \ |
| 320 | goto exit; \ |
| 321 | }) |
| 322 | |
| 323 | RT2X00DEBUGFS_CREATE_ENTRY(intf, csr); |
| 324 | RT2X00DEBUGFS_CREATE_ENTRY(intf, eeprom); |
| 325 | RT2X00DEBUGFS_CREATE_ENTRY(intf, bbp); |
| 326 | RT2X00DEBUGFS_CREATE_ENTRY(intf, rf); |
| 327 | |
| 328 | #undef RT2X00DEBUGFS_CREATE_ENTRY |
| 329 | |
| 330 | return; |
| 331 | |
| 332 | exit: |
| 333 | rt2x00debug_deregister(rt2x00dev); |
| 334 | ERROR(rt2x00dev, "Failed to register debug handler.\n"); |
| 335 | |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev) |
| 340 | { |
| 341 | const struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf; |
| 342 | |
| 343 | if (unlikely(!intf)) |
| 344 | return; |
| 345 | |
| 346 | debugfs_remove(intf->rf_val_entry); |
| 347 | debugfs_remove(intf->rf_off_entry); |
| 348 | debugfs_remove(intf->bbp_val_entry); |
| 349 | debugfs_remove(intf->bbp_off_entry); |
| 350 | debugfs_remove(intf->eeprom_val_entry); |
| 351 | debugfs_remove(intf->eeprom_off_entry); |
| 352 | debugfs_remove(intf->csr_val_entry); |
| 353 | debugfs_remove(intf->csr_off_entry); |
Ivo van Doorn | 643b252 | 2007-09-25 20:13:51 +0200 | [diff] [blame] | 354 | debugfs_remove(intf->dev_flags); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 355 | debugfs_remove(intf->chipset_entry); |
| 356 | debugfs_remove(intf->driver_entry); |
| 357 | debugfs_remove(intf->driver_folder); |
| 358 | kfree(intf->chipset_blob.data); |
| 359 | kfree(intf->driver_blob.data); |
| 360 | kfree(intf); |
| 361 | |
| 362 | rt2x00dev->debugfs_intf = NULL; |
| 363 | } |