1 // SPDX-License-Identifier: GPL-2.0
2 /* ATM driver model support. */
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/init.h>
7 #include <linux/kobject.h>
8 #include <linux/atmdev.h>
10 #include "resources.h"
12 #define to_atm_dev(cldev) container_of(cldev, struct atm_dev, class_dev)
14 static ssize_t
show_type(struct device
*cdev
,
15 struct device_attribute
*attr
, char *buf
)
17 struct atm_dev
*adev
= to_atm_dev(cdev
);
19 return scnprintf(buf
, PAGE_SIZE
, "%s\n", adev
->type
);
22 static ssize_t
show_address(struct device
*cdev
,
23 struct device_attribute
*attr
, char *buf
)
25 struct atm_dev
*adev
= to_atm_dev(cdev
);
27 return scnprintf(buf
, PAGE_SIZE
, "%pM\n", adev
->esi
);
30 static ssize_t
show_atmaddress(struct device
*cdev
,
31 struct device_attribute
*attr
, char *buf
)
34 struct atm_dev
*adev
= to_atm_dev(cdev
);
35 struct atm_dev_addr
*aaddr
;
36 int bin
[] = { 1, 2, 10, 6, 1 }, *fmt
= bin
;
39 spin_lock_irqsave(&adev
->lock
, flags
);
40 list_for_each_entry(aaddr
, &adev
->local
, entry
) {
41 for (i
= 0, j
= 0; i
< ATM_ESA_LEN
; ++i
, ++j
) {
43 count
+= scnprintf(buf
+ count
,
44 PAGE_SIZE
- count
, ".");
48 count
+= scnprintf(buf
+ count
,
49 PAGE_SIZE
- count
, "%02x",
50 aaddr
->addr
.sas_addr
.prv
[i
]);
52 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
54 spin_unlock_irqrestore(&adev
->lock
, flags
);
59 static ssize_t
show_atmindex(struct device
*cdev
,
60 struct device_attribute
*attr
, char *buf
)
62 struct atm_dev
*adev
= to_atm_dev(cdev
);
64 return scnprintf(buf
, PAGE_SIZE
, "%d\n", adev
->number
);
67 static ssize_t
show_carrier(struct device
*cdev
,
68 struct device_attribute
*attr
, char *buf
)
70 struct atm_dev
*adev
= to_atm_dev(cdev
);
72 return scnprintf(buf
, PAGE_SIZE
, "%d\n",
73 adev
->signal
== ATM_PHY_SIG_LOST
? 0 : 1);
76 static ssize_t
show_link_rate(struct device
*cdev
,
77 struct device_attribute
*attr
, char *buf
)
79 struct atm_dev
*adev
= to_atm_dev(cdev
);
82 /* show the link rate, not the data rate */
83 switch (adev
->link_rate
) {
85 link_rate
= 155520000;
88 link_rate
= 622080000;
94 link_rate
= adev
->link_rate
* 8 * 53;
96 return scnprintf(buf
, PAGE_SIZE
, "%d\n", link_rate
);
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
);
106 static struct device_attribute
*atm_attrs
[] = {
107 &dev_attr_atmaddress
,
117 static int atm_uevent(struct device
*cdev
, struct kobj_uevent_env
*env
)
119 struct atm_dev
*adev
;
124 adev
= to_atm_dev(cdev
);
128 if (add_uevent_var(env
, "NAME=%s%d", adev
->type
, adev
->number
))
134 static void atm_release(struct device
*cdev
)
136 struct atm_dev
*adev
= to_atm_dev(cdev
);
141 static struct class atm_class
= {
143 .dev_release
= atm_release
,
144 .dev_uevent
= atm_uevent
,
147 int atm_register_sysfs(struct atm_dev
*adev
, struct device
*parent
)
149 struct device
*cdev
= &adev
->class_dev
;
152 cdev
->class = &atm_class
;
153 cdev
->parent
= parent
;
154 dev_set_drvdata(cdev
, adev
);
156 dev_set_name(cdev
, "%s%d", adev
->type
, adev
->number
);
157 err
= device_register(cdev
);
161 for (i
= 0; atm_attrs
[i
]; i
++) {
162 err
= device_create_file(cdev
, atm_attrs
[i
]);
170 for (j
= 0; j
< i
; j
++)
171 device_remove_file(cdev
, atm_attrs
[j
]);
176 void atm_unregister_sysfs(struct atm_dev
*adev
)
178 struct device
*cdev
= &adev
->class_dev
;
183 int __init
atm_sysfs_init(void)
185 return class_register(&atm_class
);
188 void __exit
atm_sysfs_exit(void)
190 class_unregister(&atm_class
);