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 type_show(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 address_show(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 atmaddress_show(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;
38 spin_lock_irqsave(&adev->lock, flags);
39 list_for_each_entry(aaddr, &adev->local, entry) {
40 count += scnprintf(buf + count, PAGE_SIZE - count,
41 "%1phN.%2phN.%10phN.%6phN.%1phN\n",
42 &aaddr->addr.sas_addr.prv[0],
43 &aaddr->addr.sas_addr.prv[1],
44 &aaddr->addr.sas_addr.prv[3],
45 &aaddr->addr.sas_addr.prv[13],
46 &aaddr->addr.sas_addr.prv[19]);
48 spin_unlock_irqrestore(&adev->lock, flags);
53 static ssize_t atmindex_show(struct device *cdev,
54 struct device_attribute *attr, char *buf)
56 struct atm_dev *adev = to_atm_dev(cdev);
58 return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number);
61 static ssize_t carrier_show(struct device *cdev,
62 struct device_attribute *attr, char *buf)
64 struct atm_dev *adev = to_atm_dev(cdev);
66 return scnprintf(buf, PAGE_SIZE, "%d\n",
67 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
70 static ssize_t link_rate_show(struct device *cdev,
71 struct device_attribute *attr, char *buf)
73 struct atm_dev *adev = to_atm_dev(cdev);
76 /* show the link rate, not the data rate */
77 switch (adev->link_rate) {
79 link_rate = 155520000;
82 link_rate = 622080000;
88 link_rate = adev->link_rate * 8 * 53;
90 return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate);
93 static DEVICE_ATTR_RO(address);
94 static DEVICE_ATTR_RO(atmaddress);
95 static DEVICE_ATTR_RO(atmindex);
96 static DEVICE_ATTR_RO(carrier);
97 static DEVICE_ATTR_RO(type);
98 static DEVICE_ATTR_RO(link_rate);
100 static struct device_attribute *atm_attrs[] = {
101 &dev_attr_atmaddress,
111 static int atm_uevent(const struct device *cdev, struct kobj_uevent_env *env)
113 const struct atm_dev *adev;
118 adev = to_atm_dev(cdev);
120 if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
126 static void atm_release(struct device *cdev)
128 struct atm_dev *adev = to_atm_dev(cdev);
133 static struct class atm_class = {
135 .dev_release = atm_release,
136 .dev_uevent = atm_uevent,
139 int atm_register_sysfs(struct atm_dev *adev, struct device *parent)
141 struct device *cdev = &adev->class_dev;
144 cdev->class = &atm_class;
145 cdev->parent = parent;
146 dev_set_drvdata(cdev, adev);
148 dev_set_name(cdev, "%s%d", adev->type, adev->number);
149 err = device_register(cdev);
153 for (i = 0; atm_attrs[i]; i++) {
154 err = device_create_file(cdev, atm_attrs[i]);
162 for (j = 0; j < i; j++)
163 device_remove_file(cdev, atm_attrs[j]);
168 void atm_unregister_sysfs(struct atm_dev *adev)
170 struct device *cdev = &adev->class_dev;
175 int __init atm_sysfs_init(void)
177 return class_register(&atm_class);
180 void __exit atm_sysfs_exit(void)
182 class_unregister(&atm_class);