blob: 39b94ca5f65dc45f204a8fdeb5097834c5345404 [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
Kay Sieversef395922007-12-30 23:16:06 -080014static ssize_t show_type(struct device *cdev,
15 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
Kay Sieversef395922007-12-30 23:16:06 -080022static ssize_t show_address(struct device *cdev,
23 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
Kay Sieversef395922007-12-30 23:16:06 -080030static ssize_t show_atmaddress(struct device *cdev,
31 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;
Roman Kagan656d98b2006-06-29 12:36:34 -070036 int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000037 int i, j, count = 0;
Roman Kagan656d98b2006-06-29 12:36:34 -070038
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090039 spin_lock_irqsave(&adev->lock, flags);
40 list_for_each_entry(aaddr, &adev->local, entry) {
Joe Perchesf0a6cb12010-01-26 11:40:03 +000041 for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
Roman Kagan656d98b2006-06-29 12:36:34 -070042 if (j == *fmt) {
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000043 count += scnprintf(buf + count,
44 PAGE_SIZE - count, ".");
Roman Kagan656d98b2006-06-29 12:36:34 -070045 ++fmt;
46 j = 0;
47 }
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000048 count += scnprintf(buf + count,
49 PAGE_SIZE - count, "%02x",
50 aaddr->addr.sas_addr.prv[i]);
Roman Kagan656d98b2006-06-29 12:36:34 -070051 }
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000052 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
Roman Kagan656d98b2006-06-29 12:36:34 -070053 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090054 spin_unlock_irqrestore(&adev->lock, flags);
Roman Kagan656d98b2006-06-29 12:36:34 -070055
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000056 return count;
Roman Kagan656d98b2006-06-29 12:36:34 -070057}
58
Dan Williamse7a46b42011-05-27 04:51:54 +000059static 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 - CONTRACTOR3c417772012-12-17 06:00:01 +000064 return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number);
Dan Williamse7a46b42011-05-27 04:51:54 +000065}
66
Kay Sieversef395922007-12-30 23:16:06 -080067static ssize_t show_carrier(struct device *cdev,
68 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070069{
Roman Kagan656d98b2006-06-29 12:36:34 -070070 struct atm_dev *adev = to_atm_dev(cdev);
71
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000072 return scnprintf(buf, PAGE_SIZE, "%d\n",
73 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
Roman Kagan656d98b2006-06-29 12:36:34 -070074}
75
Kay Sieversef395922007-12-30 23:16:06 -080076static ssize_t show_link_rate(struct device *cdev,
77 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070078{
Roman Kagan656d98b2006-06-29 12:36:34 -070079 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 Perchesf0a6cb12010-01-26 11:40:03 +000084 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 Kagan656d98b2006-06-29 12:36:34 -070095 }
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000096 return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate);
Roman Kagan656d98b2006-06-29 12:36:34 -070097}
98
Joe Perchesd6444062018-03-23 15:54:38 -070099static DEVICE_ATTR(address, 0444, show_address, NULL);
100static DEVICE_ATTR(atmaddress, 0444, show_atmaddress, NULL);
101static DEVICE_ATTR(atmindex, 0444, show_atmindex, NULL);
102static DEVICE_ATTR(carrier, 0444, show_carrier, NULL);
103static DEVICE_ATTR(type, 0444, show_type, NULL);
104static DEVICE_ATTR(link_rate, 0444, show_link_rate, NULL);
Roman Kagan656d98b2006-06-29 12:36:34 -0700105
Kay Sieversef395922007-12-30 23:16:06 -0800106static struct device_attribute *atm_attrs[] = {
107 &dev_attr_atmaddress,
108 &dev_attr_address,
Dan Williamse7a46b42011-05-27 04:51:54 +0000109 &dev_attr_atmindex,
Kay Sieversef395922007-12-30 23:16:06 -0800110 &dev_attr_carrier,
111 &dev_attr_type,
112 &dev_attr_link_rate,
Roman Kagan656d98b2006-06-29 12:36:34 -0700113 NULL
114};
115
Kay Sieversef395922007-12-30 23:16:06 -0800116
117static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env)
Roman Kagan656d98b2006-06-29 12:36:34 -0700118{
119 struct atm_dev *adev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700120
121 if (!cdev)
122 return -ENODEV;
123
124 adev = to_atm_dev(cdev);
125 if (!adev)
126 return -ENODEV;
127
Kay Sievers7eff2e72007-08-14 15:15:12 +0200128 if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
Roman Kagan656d98b2006-06-29 12:36:34 -0700129 return -ENOMEM;
130
Roman Kagan656d98b2006-06-29 12:36:34 -0700131 return 0;
132}
133
Kay Sieversef395922007-12-30 23:16:06 -0800134static void atm_release(struct device *cdev)
Roman Kagan656d98b2006-06-29 12:36:34 -0700135{
136 struct atm_dev *adev = to_atm_dev(cdev);
137
138 kfree(adev);
139}
140
141static struct class atm_class = {
142 .name = "atm",
Kay Sieversef395922007-12-30 23:16:06 -0800143 .dev_release = atm_release,
144 .dev_uevent = atm_uevent,
Roman Kagan656d98b2006-06-29 12:36:34 -0700145};
146
Dan Williamsd9ca6762010-12-08 19:40:47 +0000147int atm_register_sysfs(struct atm_dev *adev, struct device *parent)
Roman Kagan656d98b2006-06-29 12:36:34 -0700148{
Kay Sieversef395922007-12-30 23:16:06 -0800149 struct device *cdev = &adev->class_dev;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700150 int i, j, err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700151
152 cdev->class = &atm_class;
Dan Williamsd9ca6762010-12-08 19:40:47 +0000153 cdev->parent = parent;
Kay Sieversef395922007-12-30 23:16:06 -0800154 dev_set_drvdata(cdev, adev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700155
Kay Sieversfb28ad32008-11-10 13:55:14 -0800156 dev_set_name(cdev, "%s%d", adev->type, adev->number);
Kay Sieversef395922007-12-30 23:16:06 -0800157 err = device_register(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700158 if (err < 0)
159 return err;
160
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700161 for (i = 0; atm_attrs[i]; i++) {
Kay Sieversef395922007-12-30 23:16:06 -0800162 err = device_create_file(cdev, atm_attrs[i]);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700163 if (err)
164 goto err_out;
165 }
Roman Kagan656d98b2006-06-29 12:36:34 -0700166
167 return 0;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700168
169err_out:
170 for (j = 0; j < i; j++)
Kay Sieversef395922007-12-30 23:16:06 -0800171 device_remove_file(cdev, atm_attrs[j]);
172 device_del(cdev);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700173 return err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700174}
175
176void atm_unregister_sysfs(struct atm_dev *adev)
177{
Kay Sieversef395922007-12-30 23:16:06 -0800178 struct device *cdev = &adev->class_dev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700179
Kay Sieversef395922007-12-30 23:16:06 -0800180 device_del(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700181}
182
183int __init atm_sysfs_init(void)
184{
185 return class_register(&atm_class);
186}
187
188void __exit atm_sysfs_exit(void)
189{
190 class_unregister(&atm_class);
191}