blob: 0fdbdfd194746212da4bf04b132d4bb6db318722 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Roman Kagan656d98b2006-06-29 12:36:34 -07002/* ATM driver model support. */
3
Roman Kagan656d98b2006-06-29 12:36:34 -07004#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Roman Kagan656d98b2006-06-29 12:36:34 -07006#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
YueHaibing48afdae2021-05-20 21:36:45 +080014static ssize_t type_show(struct device *cdev,
Kay Sieversef395922007-12-30 23:16:06 -080015 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070016{
17 struct atm_dev *adev = to_atm_dev(cdev);
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000018
19 return scnprintf(buf, PAGE_SIZE, "%s\n", adev->type);
Roman Kagan656d98b2006-06-29 12:36:34 -070020}
21
YueHaibing48afdae2021-05-20 21:36:45 +080022static ssize_t address_show(struct device *cdev,
Kay Sieversef395922007-12-30 23:16:06 -080023 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070024{
Roman Kagan656d98b2006-06-29 12:36:34 -070025 struct atm_dev *adev = to_atm_dev(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -070026
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000027 return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi);
Roman Kagan656d98b2006-06-29 12:36:34 -070028}
29
YueHaibing48afdae2021-05-20 21:36:45 +080030static ssize_t atmaddress_show(struct device *cdev,
Kay Sieversef395922007-12-30 23:16:06 -080031 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070032{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090033 unsigned long flags;
Roman Kagan656d98b2006-06-29 12:36:34 -070034 struct atm_dev *adev = to_atm_dev(cdev);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090035 struct atm_dev_addr *aaddr;
Andy Shevchenko79ac5222020-01-23 16:20:02 +020036 int count = 0;
Roman Kagan656d98b2006-06-29 12:36:34 -070037
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090038 spin_lock_irqsave(&adev->lock, flags);
39 list_for_each_entry(aaddr, &adev->local, entry) {
Andy Shevchenko79ac5222020-01-23 16:20:02 +020040 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]);
Roman Kagan656d98b2006-06-29 12:36:34 -070047 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090048 spin_unlock_irqrestore(&adev->lock, flags);
Roman Kagan656d98b2006-06-29 12:36:34 -070049
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000050 return count;
Roman Kagan656d98b2006-06-29 12:36:34 -070051}
52
YueHaibing48afdae2021-05-20 21:36:45 +080053static ssize_t atmindex_show(struct device *cdev,
Dan Williamse7a46b42011-05-27 04:51:54 +000054 struct device_attribute *attr, char *buf)
55{
56 struct atm_dev *adev = to_atm_dev(cdev);
57
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000058 return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number);
Dan Williamse7a46b42011-05-27 04:51:54 +000059}
60
YueHaibing48afdae2021-05-20 21:36:45 +080061static ssize_t carrier_show(struct device *cdev,
Kay Sieversef395922007-12-30 23:16:06 -080062 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070063{
Roman Kagan656d98b2006-06-29 12:36:34 -070064 struct atm_dev *adev = to_atm_dev(cdev);
65
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000066 return scnprintf(buf, PAGE_SIZE, "%d\n",
67 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
Roman Kagan656d98b2006-06-29 12:36:34 -070068}
69
YueHaibing48afdae2021-05-20 21:36:45 +080070static ssize_t link_rate_show(struct device *cdev,
Kay Sieversef395922007-12-30 23:16:06 -080071 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070072{
Roman Kagan656d98b2006-06-29 12:36:34 -070073 struct atm_dev *adev = to_atm_dev(cdev);
74 int link_rate;
75
76 /* show the link rate, not the data rate */
77 switch (adev->link_rate) {
Joe Perchesf0a6cb12010-01-26 11:40:03 +000078 case ATM_OC3_PCR:
79 link_rate = 155520000;
80 break;
81 case ATM_OC12_PCR:
82 link_rate = 622080000;
83 break;
84 case ATM_25_PCR:
85 link_rate = 25600000;
86 break;
87 default:
88 link_rate = adev->link_rate * 8 * 53;
Roman Kagan656d98b2006-06-29 12:36:34 -070089 }
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000090 return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate);
Roman Kagan656d98b2006-06-29 12:36:34 -070091}
92
YueHaibing48afdae2021-05-20 21:36:45 +080093static DEVICE_ATTR_RO(address);
94static DEVICE_ATTR_RO(atmaddress);
95static DEVICE_ATTR_RO(atmindex);
96static DEVICE_ATTR_RO(carrier);
97static DEVICE_ATTR_RO(type);
98static DEVICE_ATTR_RO(link_rate);
Roman Kagan656d98b2006-06-29 12:36:34 -070099
Kay Sieversef395922007-12-30 23:16:06 -0800100static struct device_attribute *atm_attrs[] = {
101 &dev_attr_atmaddress,
102 &dev_attr_address,
Dan Williamse7a46b42011-05-27 04:51:54 +0000103 &dev_attr_atmindex,
Kay Sieversef395922007-12-30 23:16:06 -0800104 &dev_attr_carrier,
105 &dev_attr_type,
106 &dev_attr_link_rate,
Roman Kagan656d98b2006-06-29 12:36:34 -0700107 NULL
108};
109
Kay Sieversef395922007-12-30 23:16:06 -0800110
111static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env)
Roman Kagan656d98b2006-06-29 12:36:34 -0700112{
113 struct atm_dev *adev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700114
115 if (!cdev)
116 return -ENODEV;
117
118 adev = to_atm_dev(cdev);
119 if (!adev)
120 return -ENODEV;
121
Kay Sievers7eff2e72007-08-14 15:15:12 +0200122 if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
Roman Kagan656d98b2006-06-29 12:36:34 -0700123 return -ENOMEM;
124
Roman Kagan656d98b2006-06-29 12:36:34 -0700125 return 0;
126}
127
Kay Sieversef395922007-12-30 23:16:06 -0800128static void atm_release(struct device *cdev)
Roman Kagan656d98b2006-06-29 12:36:34 -0700129{
130 struct atm_dev *adev = to_atm_dev(cdev);
131
132 kfree(adev);
133}
134
135static struct class atm_class = {
136 .name = "atm",
Kay Sieversef395922007-12-30 23:16:06 -0800137 .dev_release = atm_release,
138 .dev_uevent = atm_uevent,
Roman Kagan656d98b2006-06-29 12:36:34 -0700139};
140
Dan Williamsd9ca6762010-12-08 19:40:47 +0000141int atm_register_sysfs(struct atm_dev *adev, struct device *parent)
Roman Kagan656d98b2006-06-29 12:36:34 -0700142{
Kay Sieversef395922007-12-30 23:16:06 -0800143 struct device *cdev = &adev->class_dev;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700144 int i, j, err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700145
146 cdev->class = &atm_class;
Dan Williamsd9ca6762010-12-08 19:40:47 +0000147 cdev->parent = parent;
Kay Sieversef395922007-12-30 23:16:06 -0800148 dev_set_drvdata(cdev, adev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700149
Kay Sieversfb28ad32008-11-10 13:55:14 -0800150 dev_set_name(cdev, "%s%d", adev->type, adev->number);
Kay Sieversef395922007-12-30 23:16:06 -0800151 err = device_register(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700152 if (err < 0)
153 return err;
154
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700155 for (i = 0; atm_attrs[i]; i++) {
Kay Sieversef395922007-12-30 23:16:06 -0800156 err = device_create_file(cdev, atm_attrs[i]);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700157 if (err)
158 goto err_out;
159 }
Roman Kagan656d98b2006-06-29 12:36:34 -0700160
161 return 0;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700162
163err_out:
164 for (j = 0; j < i; j++)
Kay Sieversef395922007-12-30 23:16:06 -0800165 device_remove_file(cdev, atm_attrs[j]);
166 device_del(cdev);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700167 return err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700168}
169
170void atm_unregister_sysfs(struct atm_dev *adev)
171{
Kay Sieversef395922007-12-30 23:16:06 -0800172 struct device *cdev = &adev->class_dev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700173
Kay Sieversef395922007-12-30 23:16:06 -0800174 device_del(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700175}
176
177int __init atm_sysfs_init(void)
178{
179 return class_register(&atm_class);
180}
181
182void __exit atm_sysfs_exit(void)
183{
184 class_unregister(&atm_class);
185}