Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 2 | /* ATM driver model support. */ |
| 3 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 4 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/slab.h> |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 6 | #include <linux/init.h> |
| 7 | #include <linux/kobject.h> |
| 8 | #include <linux/atmdev.h> |
| 9 | #include "common.h" |
| 10 | #include "resources.h" |
| 11 | |
| 12 | #define to_atm_dev(cldev) container_of(cldev, struct atm_dev, class_dev) |
| 13 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 14 | static ssize_t show_type(struct device *cdev, |
| 15 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 16 | { |
| 17 | struct atm_dev *adev = to_atm_dev(cdev); |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 18 | |
| 19 | return scnprintf(buf, PAGE_SIZE, "%s\n", adev->type); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 20 | } |
| 21 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 22 | static ssize_t show_address(struct device *cdev, |
| 23 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 24 | { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 25 | struct atm_dev *adev = to_atm_dev(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 26 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 27 | return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 30 | static ssize_t show_atmaddress(struct device *cdev, |
| 31 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 32 | { |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 33 | unsigned long flags; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 34 | struct atm_dev *adev = to_atm_dev(cdev); |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 35 | struct atm_dev_addr *aaddr; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 36 | int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin; |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 37 | int i, j, count = 0; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 38 | |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 39 | spin_lock_irqsave(&adev->lock, flags); |
| 40 | list_for_each_entry(aaddr, &adev->local, entry) { |
Joe Perches | f0a6cb1 | 2010-01-26 11:40:03 +0000 | [diff] [blame] | 41 | for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 42 | if (j == *fmt) { |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 43 | count += scnprintf(buf + count, |
| 44 | PAGE_SIZE - count, "."); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 45 | ++fmt; |
| 46 | j = 0; |
| 47 | } |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 48 | count += scnprintf(buf + count, |
| 49 | PAGE_SIZE - count, "%02x", |
| 50 | aaddr->addr.sas_addr.prv[i]); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 51 | } |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 52 | count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 53 | } |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 54 | spin_unlock_irqrestore(&adev->lock, flags); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 55 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 56 | return count; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 59 | static ssize_t show_atmindex(struct device *cdev, |
| 60 | struct device_attribute *attr, char *buf) |
| 61 | { |
| 62 | struct atm_dev *adev = to_atm_dev(cdev); |
| 63 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 64 | return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number); |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 67 | static ssize_t show_carrier(struct device *cdev, |
| 68 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 69 | { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 70 | struct atm_dev *adev = to_atm_dev(cdev); |
| 71 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 72 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
| 73 | adev->signal == ATM_PHY_SIG_LOST ? 0 : 1); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 76 | static ssize_t show_link_rate(struct device *cdev, |
| 77 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 78 | { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 79 | struct atm_dev *adev = to_atm_dev(cdev); |
| 80 | int link_rate; |
| 81 | |
| 82 | /* show the link rate, not the data rate */ |
| 83 | switch (adev->link_rate) { |
Joe Perches | f0a6cb1 | 2010-01-26 11:40:03 +0000 | [diff] [blame] | 84 | case ATM_OC3_PCR: |
| 85 | link_rate = 155520000; |
| 86 | break; |
| 87 | case ATM_OC12_PCR: |
| 88 | link_rate = 622080000; |
| 89 | break; |
| 90 | case ATM_25_PCR: |
| 91 | link_rate = 25600000; |
| 92 | break; |
| 93 | default: |
| 94 | link_rate = adev->link_rate * 8 * 53; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 95 | } |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 96 | return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Joe Perches | d644406 | 2018-03-23 15:54:38 -0700 | [diff] [blame] | 99 | static DEVICE_ATTR(address, 0444, show_address, NULL); |
| 100 | static DEVICE_ATTR(atmaddress, 0444, show_atmaddress, NULL); |
| 101 | static DEVICE_ATTR(atmindex, 0444, show_atmindex, NULL); |
| 102 | static DEVICE_ATTR(carrier, 0444, show_carrier, NULL); |
| 103 | static DEVICE_ATTR(type, 0444, show_type, NULL); |
| 104 | static DEVICE_ATTR(link_rate, 0444, show_link_rate, NULL); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 105 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 106 | static struct device_attribute *atm_attrs[] = { |
| 107 | &dev_attr_atmaddress, |
| 108 | &dev_attr_address, |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 109 | &dev_attr_atmindex, |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 110 | &dev_attr_carrier, |
| 111 | &dev_attr_type, |
| 112 | &dev_attr_link_rate, |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 113 | NULL |
| 114 | }; |
| 115 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 116 | |
| 117 | static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 118 | { |
| 119 | struct atm_dev *adev; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 120 | |
| 121 | if (!cdev) |
| 122 | return -ENODEV; |
| 123 | |
| 124 | adev = to_atm_dev(cdev); |
| 125 | if (!adev) |
| 126 | return -ENODEV; |
| 127 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 128 | if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number)) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 129 | return -ENOMEM; |
| 130 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 134 | static void atm_release(struct device *cdev) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 135 | { |
| 136 | struct atm_dev *adev = to_atm_dev(cdev); |
| 137 | |
| 138 | kfree(adev); |
| 139 | } |
| 140 | |
| 141 | static struct class atm_class = { |
| 142 | .name = "atm", |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 143 | .dev_release = atm_release, |
| 144 | .dev_uevent = atm_uevent, |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Dan Williams | d9ca676 | 2010-12-08 19:40:47 +0000 | [diff] [blame] | 147 | int atm_register_sysfs(struct atm_dev *adev, struct device *parent) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 148 | { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 149 | struct device *cdev = &adev->class_dev; |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 150 | int i, j, err; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 151 | |
| 152 | cdev->class = &atm_class; |
Dan Williams | d9ca676 | 2010-12-08 19:40:47 +0000 | [diff] [blame] | 153 | cdev->parent = parent; |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 154 | dev_set_drvdata(cdev, adev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 155 | |
Kay Sievers | fb28ad3 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 156 | dev_set_name(cdev, "%s%d", adev->type, adev->number); |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 157 | err = device_register(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 158 | if (err < 0) |
| 159 | return err; |
| 160 | |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 161 | for (i = 0; atm_attrs[i]; i++) { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 162 | err = device_create_file(cdev, atm_attrs[i]); |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 163 | if (err) |
| 164 | goto err_out; |
| 165 | } |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 166 | |
| 167 | return 0; |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 168 | |
| 169 | err_out: |
| 170 | for (j = 0; j < i; j++) |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 171 | device_remove_file(cdev, atm_attrs[j]); |
| 172 | device_del(cdev); |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 173 | return err; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void atm_unregister_sysfs(struct atm_dev *adev) |
| 177 | { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 178 | struct device *cdev = &adev->class_dev; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 179 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 180 | device_del(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | int __init atm_sysfs_init(void) |
| 184 | { |
| 185 | return class_register(&atm_class); |
| 186 | } |
| 187 | |
| 188 | void __exit atm_sysfs_exit(void) |
| 189 | { |
| 190 | class_unregister(&atm_class); |
| 191 | } |