Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 1 | /* This program is free software; you can redistribute it and/or modify |
| 2 | * it under the terms of the GNU General Public License version 2 |
| 3 | * as published by the Free Software Foundation. |
| 4 | * |
| 5 | * This program is distributed in the hope that it will be useful, |
| 6 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 7 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 8 | * GNU General Public License for more details. |
| 9 | * |
| 10 | * Authors: |
| 11 | * Alexander Aring <aar@pengutronix.de> |
| 12 | * |
| 13 | * Based on: net/wireless/sysfs.c |
| 14 | */ |
| 15 | |
| 16 | #include <linux/device.h> |
| 17 | |
| 18 | #include <net/cfg802154.h> |
| 19 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 20 | #include "core.h" |
| 21 | |
| 22 | static inline struct cfg802154_registered_device * |
| 23 | dev_to_rdev(struct device *dev) |
| 24 | { |
| 25 | return container_of(dev, struct cfg802154_registered_device, |
| 26 | wpan_phy.dev); |
| 27 | } |
| 28 | |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 29 | #define MASTER_SHOW_COMPLEX(name, format_string, args...) \ |
| 30 | static ssize_t name ## _show(struct device *dev, \ |
| 31 | struct device_attribute *attr, char *buf) \ |
| 32 | { \ |
| 33 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \ |
| 34 | int ret; \ |
| 35 | \ |
| 36 | mutex_lock(&phy->pib_lock); \ |
| 37 | ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \ |
| 38 | mutex_unlock(&phy->pib_lock); \ |
| 39 | return ret; \ |
| 40 | } \ |
| 41 | static DEVICE_ATTR_RO(name) |
| 42 | |
| 43 | #define MASTER_SHOW(field, format_string) \ |
| 44 | MASTER_SHOW_COMPLEX(field, format_string, phy->field) |
| 45 | |
| 46 | MASTER_SHOW(current_channel, "%d"); |
| 47 | MASTER_SHOW(current_page, "%d"); |
| 48 | MASTER_SHOW(transmit_power, "%d +- 1 dB"); |
| 49 | MASTER_SHOW(cca_mode, "%d"); |
| 50 | |
| 51 | static ssize_t channels_supported_show(struct device *dev, |
| 52 | struct device_attribute *attr, |
| 53 | char *buf) |
| 54 | { |
| 55 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); |
| 56 | int ret; |
| 57 | int i, len = 0; |
| 58 | |
| 59 | mutex_lock(&phy->pib_lock); |
| 60 | for (i = 0; i < 32; i++) { |
| 61 | ret = snprintf(buf + len, PAGE_SIZE - len, |
| 62 | "%#09x\n", phy->channels_supported[i]); |
| 63 | if (ret < 0) |
| 64 | break; |
| 65 | len += ret; |
| 66 | } |
| 67 | mutex_unlock(&phy->pib_lock); |
| 68 | return len; |
| 69 | } |
| 70 | static DEVICE_ATTR_RO(channels_supported); |
| 71 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 72 | static void wpan_phy_release(struct device *dev) |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 73 | { |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 74 | struct cfg802154_registered_device *rdev = dev_to_rdev(dev); |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 75 | |
Alexander Aring | a5dd1d7 | 2014-11-02 04:18:35 +0100 | [diff] [blame^] | 76 | cfg802154_dev_free(rdev); |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static struct attribute *pmib_attrs[] = { |
| 80 | &dev_attr_current_channel.attr, |
| 81 | &dev_attr_current_page.attr, |
| 82 | &dev_attr_channels_supported.attr, |
| 83 | &dev_attr_transmit_power.attr, |
| 84 | &dev_attr_cca_mode.attr, |
| 85 | NULL, |
| 86 | }; |
| 87 | ATTRIBUTE_GROUPS(pmib); |
| 88 | |
| 89 | struct class wpan_phy_class = { |
| 90 | .name = "ieee802154", |
| 91 | .dev_release = wpan_phy_release, |
| 92 | .dev_groups = pmib_groups, |
| 93 | }; |
| 94 | |
| 95 | int wpan_phy_sysfs_init(void) |
| 96 | { |
| 97 | return class_register(&wpan_phy_class); |
| 98 | } |
| 99 | |
| 100 | void wpan_phy_sysfs_exit(void) |
| 101 | { |
| 102 | class_unregister(&wpan_phy_class); |
| 103 | } |