Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
656d98b0 RK |
2 | /* ATM driver model support. */ |
3 | ||
656d98b0 | 4 | #include <linux/kernel.h> |
5a0e3ad6 | 5 | #include <linux/slab.h> |
656d98b0 RK |
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 | ||
ef39592f KS |
14 | static ssize_t show_type(struct device *cdev, |
15 | struct device_attribute *attr, char *buf) | |
656d98b0 RK |
16 | { |
17 | struct atm_dev *adev = to_atm_dev(cdev); | |
3c417771 C |
18 | |
19 | return scnprintf(buf, PAGE_SIZE, "%s\n", adev->type); | |
656d98b0 RK |
20 | } |
21 | ||
ef39592f KS |
22 | static ssize_t show_address(struct device *cdev, |
23 | struct device_attribute *attr, char *buf) | |
656d98b0 | 24 | { |
656d98b0 | 25 | struct atm_dev *adev = to_atm_dev(cdev); |
656d98b0 | 26 | |
3c417771 | 27 | return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi); |
656d98b0 RK |
28 | } |
29 | ||
ef39592f KS |
30 | static ssize_t show_atmaddress(struct device *cdev, |
31 | struct device_attribute *attr, char *buf) | |
656d98b0 | 32 | { |
f7d57453 | 33 | unsigned long flags; |
656d98b0 | 34 | struct atm_dev *adev = to_atm_dev(cdev); |
f7d57453 | 35 | struct atm_dev_addr *aaddr; |
656d98b0 | 36 | int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin; |
3c417771 | 37 | int i, j, count = 0; |
656d98b0 | 38 | |
f7d57453 YH |
39 | spin_lock_irqsave(&adev->lock, flags); |
40 | list_for_each_entry(aaddr, &adev->local, entry) { | |
f0a6cb11 | 41 | for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) { |
656d98b0 | 42 | if (j == *fmt) { |
3c417771 C |
43 | count += scnprintf(buf + count, |
44 | PAGE_SIZE - count, "."); | |
656d98b0 RK |
45 | ++fmt; |
46 | j = 0; | |
47 | } | |
3c417771 C |
48 | count += scnprintf(buf + count, |
49 | PAGE_SIZE - count, "%02x", | |
50 | aaddr->addr.sas_addr.prv[i]); | |
656d98b0 | 51 | } |
3c417771 | 52 | count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); |
656d98b0 | 53 | } |
f7d57453 | 54 | spin_unlock_irqrestore(&adev->lock, flags); |
656d98b0 | 55 | |
3c417771 | 56 | return count; |
656d98b0 RK |
57 | } |
58 | ||
e7a46b4d DW |
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 | ||
3c417771 | 64 | return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number); |
e7a46b4d DW |
65 | } |
66 | ||
ef39592f KS |
67 | static ssize_t show_carrier(struct device *cdev, |
68 | struct device_attribute *attr, char *buf) | |
656d98b0 | 69 | { |
656d98b0 RK |
70 | struct atm_dev *adev = to_atm_dev(cdev); |
71 | ||
3c417771 C |
72 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
73 | adev->signal == ATM_PHY_SIG_LOST ? 0 : 1); | |
656d98b0 RK |
74 | } |
75 | ||
ef39592f KS |
76 | static ssize_t show_link_rate(struct device *cdev, |
77 | struct device_attribute *attr, char *buf) | |
656d98b0 | 78 | { |
656d98b0 RK |
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) { | |
f0a6cb11 JP |
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; | |
656d98b0 | 95 | } |
3c417771 | 96 | return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate); |
656d98b0 RK |
97 | } |
98 | ||
ef39592f KS |
99 | static DEVICE_ATTR(address, S_IRUGO, show_address, NULL); |
100 | static DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL); | |
e7a46b4d | 101 | static DEVICE_ATTR(atmindex, S_IRUGO, show_atmindex, NULL); |
ef39592f KS |
102 | static DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL); |
103 | static DEVICE_ATTR(type, S_IRUGO, show_type, NULL); | |
104 | static DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL); | |
105 | ||
106 | static struct device_attribute *atm_attrs[] = { | |
107 | &dev_attr_atmaddress, | |
108 | &dev_attr_address, | |
e7a46b4d | 109 | &dev_attr_atmindex, |
ef39592f KS |
110 | &dev_attr_carrier, |
111 | &dev_attr_type, | |
112 | &dev_attr_link_rate, | |
656d98b0 RK |
113 | NULL |
114 | }; | |
115 | ||
ef39592f KS |
116 | |
117 | static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env) | |
656d98b0 RK |
118 | { |
119 | struct atm_dev *adev; | |
656d98b0 RK |
120 | |
121 | if (!cdev) | |
122 | return -ENODEV; | |
123 | ||
124 | adev = to_atm_dev(cdev); | |
125 | if (!adev) | |
126 | return -ENODEV; | |
127 | ||
7eff2e7a | 128 | if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number)) |
656d98b0 RK |
129 | return -ENOMEM; |
130 | ||
656d98b0 RK |
131 | return 0; |
132 | } | |
133 | ||
ef39592f | 134 | static void atm_release(struct device *cdev) |
656d98b0 RK |
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", | |
ef39592f KS |
143 | .dev_release = atm_release, |
144 | .dev_uevent = atm_uevent, | |
656d98b0 RK |
145 | }; |
146 | ||
d9ca676b | 147 | int atm_register_sysfs(struct atm_dev *adev, struct device *parent) |
656d98b0 | 148 | { |
ef39592f | 149 | struct device *cdev = &adev->class_dev; |
97f80bc6 | 150 | int i, j, err; |
656d98b0 RK |
151 | |
152 | cdev->class = &atm_class; | |
d9ca676b | 153 | cdev->parent = parent; |
ef39592f | 154 | dev_set_drvdata(cdev, adev); |
656d98b0 | 155 | |
fb28ad35 | 156 | dev_set_name(cdev, "%s%d", adev->type, adev->number); |
ef39592f | 157 | err = device_register(cdev); |
656d98b0 RK |
158 | if (err < 0) |
159 | return err; | |
160 | ||
97f80bc6 | 161 | for (i = 0; atm_attrs[i]; i++) { |
ef39592f | 162 | err = device_create_file(cdev, atm_attrs[i]); |
97f80bc6 JG |
163 | if (err) |
164 | goto err_out; | |
165 | } | |
656d98b0 RK |
166 | |
167 | return 0; | |
97f80bc6 JG |
168 | |
169 | err_out: | |
170 | for (j = 0; j < i; j++) | |
ef39592f KS |
171 | device_remove_file(cdev, atm_attrs[j]); |
172 | device_del(cdev); | |
97f80bc6 | 173 | return err; |
656d98b0 RK |
174 | } |
175 | ||
176 | void atm_unregister_sysfs(struct atm_dev *adev) | |
177 | { | |
ef39592f | 178 | struct device *cdev = &adev->class_dev; |
656d98b0 | 179 | |
ef39592f | 180 | device_del(cdev); |
656d98b0 RK |
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 | } |