Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007, 2008, 2009 Siemens AG |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 13 | */ |
| 14 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/device.h> |
| 19 | |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame] | 20 | #include <net/cfg802154.h> |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 21 | |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 22 | #include "ieee802154.h" |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 23 | #include "sysfs.h" |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 24 | |
| 25 | static DEFINE_MUTEX(wpan_phy_mutex); |
| 26 | static int wpan_phy_idx; |
| 27 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 28 | static int wpan_phy_match(struct device *dev, const void *data) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 29 | { |
| 30 | return !strcmp(dev_name(dev), (const char *)data); |
| 31 | } |
| 32 | |
| 33 | struct wpan_phy *wpan_phy_find(const char *str) |
| 34 | { |
| 35 | struct device *dev; |
| 36 | |
| 37 | if (WARN_ON(!str)) |
| 38 | return NULL; |
| 39 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 40 | dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 41 | if (!dev) |
| 42 | return NULL; |
| 43 | |
| 44 | return container_of(dev, struct wpan_phy, dev); |
| 45 | } |
| 46 | EXPORT_SYMBOL(wpan_phy_find); |
| 47 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 48 | struct wpan_phy_iter_data { |
| 49 | int (*fn)(struct wpan_phy *phy, void *data); |
| 50 | void *data; |
| 51 | }; |
| 52 | |
| 53 | static int wpan_phy_iter(struct device *dev, void *_data) |
| 54 | { |
| 55 | struct wpan_phy_iter_data *wpid = _data; |
| 56 | struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 57 | |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 58 | return wpid->fn(phy, wpid->data); |
| 59 | } |
| 60 | |
| 61 | int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 62 | void *data) |
Dmitry Eremin-Solenikov | 1c889f4 | 2009-09-15 16:57:04 +0400 | [diff] [blame] | 63 | { |
| 64 | struct wpan_phy_iter_data wpid = { |
| 65 | .fn = fn, |
| 66 | .data = data, |
| 67 | }; |
| 68 | |
| 69 | return class_for_each_device(&wpan_phy_class, NULL, |
| 70 | &wpid, wpan_phy_iter); |
| 71 | } |
| 72 | EXPORT_SYMBOL(wpan_phy_for_each); |
| 73 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 74 | static int wpan_phy_idx_valid(int idx) |
| 75 | { |
| 76 | return idx >= 0; |
| 77 | } |
| 78 | |
| 79 | struct wpan_phy *wpan_phy_alloc(size_t priv_size) |
| 80 | { |
| 81 | struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size, |
| 82 | GFP_KERNEL); |
| 83 | |
Denis Kirjanov | a6c0f82 | 2010-05-23 05:45:45 +0000 | [diff] [blame] | 84 | if (!phy) |
| 85 | goto out; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 86 | mutex_lock(&wpan_phy_mutex); |
| 87 | phy->idx = wpan_phy_idx++; |
| 88 | if (unlikely(!wpan_phy_idx_valid(phy->idx))) { |
| 89 | wpan_phy_idx--; |
| 90 | mutex_unlock(&wpan_phy_mutex); |
| 91 | kfree(phy); |
Denis Kirjanov | a6c0f82 | 2010-05-23 05:45:45 +0000 | [diff] [blame] | 92 | goto out; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 93 | } |
| 94 | mutex_unlock(&wpan_phy_mutex); |
| 95 | |
| 96 | mutex_init(&phy->pib_lock); |
| 97 | |
| 98 | device_initialize(&phy->dev); |
| 99 | dev_set_name(&phy->dev, "wpan-phy%d", phy->idx); |
| 100 | |
| 101 | phy->dev.class = &wpan_phy_class; |
| 102 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 103 | return phy; |
Denis Kirjanov | a6c0f82 | 2010-05-23 05:45:45 +0000 | [diff] [blame] | 104 | |
| 105 | out: |
| 106 | return NULL; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 107 | } |
| 108 | EXPORT_SYMBOL(wpan_phy_alloc); |
| 109 | |
Dmitry Eremin-Solenikov | e9cf356 | 2009-09-28 19:01:20 +0400 | [diff] [blame] | 110 | int wpan_phy_register(struct wpan_phy *phy) |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 111 | { |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 112 | return device_add(&phy->dev); |
| 113 | } |
| 114 | EXPORT_SYMBOL(wpan_phy_register); |
| 115 | |
| 116 | void wpan_phy_unregister(struct wpan_phy *phy) |
| 117 | { |
| 118 | device_del(&phy->dev); |
| 119 | } |
| 120 | EXPORT_SYMBOL(wpan_phy_unregister); |
| 121 | |
| 122 | void wpan_phy_free(struct wpan_phy *phy) |
| 123 | { |
| 124 | put_device(&phy->dev); |
| 125 | } |
| 126 | EXPORT_SYMBOL(wpan_phy_free); |
| 127 | |
| 128 | static int __init wpan_phy_class_init(void) |
| 129 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 130 | int rc; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 131 | |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 132 | rc = wpan_phy_sysfs_init(); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 133 | if (rc) |
| 134 | goto err; |
| 135 | |
| 136 | rc = ieee802154_nl_init(); |
| 137 | if (rc) |
| 138 | goto err_nl; |
| 139 | |
| 140 | return 0; |
| 141 | err_nl: |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 142 | wpan_phy_sysfs_exit(); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 143 | err: |
| 144 | return rc; |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 145 | } |
Dmitry Eremin-Solenikov | 282a395 | 2009-11-12 23:58:23 +0300 | [diff] [blame] | 146 | subsys_initcall(wpan_phy_class_init); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 147 | |
| 148 | static void __exit wpan_phy_class_exit(void) |
| 149 | { |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 150 | ieee802154_nl_exit(); |
Alexander Aring | e23e9ec | 2014-10-28 18:21:32 +0100 | [diff] [blame] | 151 | wpan_phy_sysfs_exit(); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 152 | } |
| 153 | module_exit(wpan_phy_class_exit); |
| 154 | |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 155 | MODULE_LICENSE("GPL v2"); |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 156 | MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface"); |
| 157 | MODULE_AUTHOR("Dmitry Eremin-Solenikov"); |
Dmitry Eremin-Solenikov | 2bfb107 | 2009-08-14 16:13:12 +0400 | [diff] [blame] | 158 | |