blob: f49da5814bc3c9828829ac2be54ed58b2fbc82a9 [file] [log] [blame]
Roman Kagan656d98b2006-06-29 12:36:34 -07001/* ATM driver model support. */
2
Roman Kagan656d98b2006-06-29 12:36:34 -07003#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Roman Kagan656d98b2006-06-29 12:36:34 -07005#include <linux/init.h>
6#include <linux/kobject.h>
7#include <linux/atmdev.h>
8#include "common.h"
9#include "resources.h"
10
11#define to_atm_dev(cldev) container_of(cldev, struct atm_dev, class_dev)
12
Kay Sieversef395922007-12-30 23:16:06 -080013static ssize_t show_type(struct device *cdev,
14 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070015{
16 struct atm_dev *adev = to_atm_dev(cdev);
17 return sprintf(buf, "%s\n", adev->type);
18}
19
Kay Sieversef395922007-12-30 23:16:06 -080020static ssize_t show_address(struct device *cdev,
21 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070022{
23 char *pos = buf;
24 struct atm_dev *adev = to_atm_dev(cdev);
25 int i;
26
27 for (i = 0; i < (ESI_LEN - 1); i++)
28 pos += sprintf(pos, "%02x:", adev->esi[i]);
29 pos += sprintf(pos, "%02x\n", adev->esi[i]);
30
31 return pos - buf;
32}
33
Kay Sieversef395922007-12-30 23:16:06 -080034static ssize_t show_atmaddress(struct device *cdev,
35 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070036{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090037 unsigned long flags;
Roman Kagan656d98b2006-06-29 12:36:34 -070038 char *pos = buf;
39 struct atm_dev *adev = to_atm_dev(cdev);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090040 struct atm_dev_addr *aaddr;
Roman Kagan656d98b2006-06-29 12:36:34 -070041 int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
42 int i, j;
43
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090044 spin_lock_irqsave(&adev->lock, flags);
45 list_for_each_entry(aaddr, &adev->local, entry) {
Joe Perchesf0a6cb12010-01-26 11:40:03 +000046 for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
Roman Kagan656d98b2006-06-29 12:36:34 -070047 if (j == *fmt) {
48 pos += sprintf(pos, ".");
49 ++fmt;
50 j = 0;
51 }
Joe Perchesf0a6cb12010-01-26 11:40:03 +000052 pos += sprintf(pos, "%02x",
53 aaddr->addr.sas_addr.prv[i]);
Roman Kagan656d98b2006-06-29 12:36:34 -070054 }
55 pos += sprintf(pos, "\n");
56 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090057 spin_unlock_irqrestore(&adev->lock, flags);
Roman Kagan656d98b2006-06-29 12:36:34 -070058
59 return pos - buf;
60}
61
Dan Williamse7a46b42011-05-27 04:51:54 +000062static ssize_t show_atmindex(struct device *cdev,
63 struct device_attribute *attr, char *buf)
64{
65 struct atm_dev *adev = to_atm_dev(cdev);
66
67 return sprintf(buf, "%d\n", adev->number);
68}
69
Kay Sieversef395922007-12-30 23:16:06 -080070static ssize_t show_carrier(struct device *cdev,
71 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070072{
73 char *pos = buf;
74 struct atm_dev *adev = to_atm_dev(cdev);
75
76 pos += sprintf(pos, "%d\n",
77 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090078
Roman Kagan656d98b2006-06-29 12:36:34 -070079 return pos - buf;
80}
81
Kay Sieversef395922007-12-30 23:16:06 -080082static ssize_t show_link_rate(struct device *cdev,
83 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070084{
85 char *pos = buf;
86 struct atm_dev *adev = to_atm_dev(cdev);
87 int link_rate;
88
89 /* show the link rate, not the data rate */
90 switch (adev->link_rate) {
Joe Perchesf0a6cb12010-01-26 11:40:03 +000091 case ATM_OC3_PCR:
92 link_rate = 155520000;
93 break;
94 case ATM_OC12_PCR:
95 link_rate = 622080000;
96 break;
97 case ATM_25_PCR:
98 link_rate = 25600000;
99 break;
100 default:
101 link_rate = adev->link_rate * 8 * 53;
Roman Kagan656d98b2006-06-29 12:36:34 -0700102 }
103 pos += sprintf(pos, "%d\n", link_rate);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900104
Roman Kagan656d98b2006-06-29 12:36:34 -0700105 return pos - buf;
106}
107
Kay Sieversef395922007-12-30 23:16:06 -0800108static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
109static DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL);
Dan Williamse7a46b42011-05-27 04:51:54 +0000110static DEVICE_ATTR(atmindex, S_IRUGO, show_atmindex, NULL);
Kay Sieversef395922007-12-30 23:16:06 -0800111static DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL);
112static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
113static DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL);
Roman Kagan656d98b2006-06-29 12:36:34 -0700114
Kay Sieversef395922007-12-30 23:16:06 -0800115static struct device_attribute *atm_attrs[] = {
116 &dev_attr_atmaddress,
117 &dev_attr_address,
Dan Williamse7a46b42011-05-27 04:51:54 +0000118 &dev_attr_atmindex,
Kay Sieversef395922007-12-30 23:16:06 -0800119 &dev_attr_carrier,
120 &dev_attr_type,
121 &dev_attr_link_rate,
Roman Kagan656d98b2006-06-29 12:36:34 -0700122 NULL
123};
124
Kay Sieversef395922007-12-30 23:16:06 -0800125
126static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env)
Roman Kagan656d98b2006-06-29 12:36:34 -0700127{
128 struct atm_dev *adev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700129
130 if (!cdev)
131 return -ENODEV;
132
133 adev = to_atm_dev(cdev);
134 if (!adev)
135 return -ENODEV;
136
Kay Sievers7eff2e72007-08-14 15:15:12 +0200137 if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
Roman Kagan656d98b2006-06-29 12:36:34 -0700138 return -ENOMEM;
139
Roman Kagan656d98b2006-06-29 12:36:34 -0700140 return 0;
141}
142
Kay Sieversef395922007-12-30 23:16:06 -0800143static void atm_release(struct device *cdev)
Roman Kagan656d98b2006-06-29 12:36:34 -0700144{
145 struct atm_dev *adev = to_atm_dev(cdev);
146
147 kfree(adev);
148}
149
150static struct class atm_class = {
151 .name = "atm",
Kay Sieversef395922007-12-30 23:16:06 -0800152 .dev_release = atm_release,
153 .dev_uevent = atm_uevent,
Roman Kagan656d98b2006-06-29 12:36:34 -0700154};
155
Dan Williamsd9ca6762010-12-08 19:40:47 +0000156int atm_register_sysfs(struct atm_dev *adev, struct device *parent)
Roman Kagan656d98b2006-06-29 12:36:34 -0700157{
Kay Sieversef395922007-12-30 23:16:06 -0800158 struct device *cdev = &adev->class_dev;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700159 int i, j, err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700160
161 cdev->class = &atm_class;
Dan Williamsd9ca6762010-12-08 19:40:47 +0000162 cdev->parent = parent;
Kay Sieversef395922007-12-30 23:16:06 -0800163 dev_set_drvdata(cdev, adev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700164
Kay Sieversfb28ad32008-11-10 13:55:14 -0800165 dev_set_name(cdev, "%s%d", adev->type, adev->number);
Kay Sieversef395922007-12-30 23:16:06 -0800166 err = device_register(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700167 if (err < 0)
168 return err;
169
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700170 for (i = 0; atm_attrs[i]; i++) {
Kay Sieversef395922007-12-30 23:16:06 -0800171 err = device_create_file(cdev, atm_attrs[i]);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700172 if (err)
173 goto err_out;
174 }
Roman Kagan656d98b2006-06-29 12:36:34 -0700175
176 return 0;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700177
178err_out:
179 for (j = 0; j < i; j++)
Kay Sieversef395922007-12-30 23:16:06 -0800180 device_remove_file(cdev, atm_attrs[j]);
181 device_del(cdev);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700182 return err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700183}
184
185void atm_unregister_sysfs(struct atm_dev *adev)
186{
Kay Sieversef395922007-12-30 23:16:06 -0800187 struct device *cdev = &adev->class_dev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700188
Kay Sieversef395922007-12-30 23:16:06 -0800189 device_del(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700190}
191
192int __init atm_sysfs_init(void)
193{
194 return class_register(&atm_class);
195}
196
197void __exit atm_sysfs_exit(void)
198{
199 class_unregister(&atm_class);
200}