blob: 799c631f0fed0c1e8210d2d61e3fdf3afd27aea9 [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
Kay Sieversef395922007-12-30 23:16:06 -080062static ssize_t show_carrier(struct device *cdev,
63 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070064{
65 char *pos = buf;
66 struct atm_dev *adev = to_atm_dev(cdev);
67
68 pos += sprintf(pos, "%d\n",
69 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090070
Roman Kagan656d98b2006-06-29 12:36:34 -070071 return pos - buf;
72}
73
Kay Sieversef395922007-12-30 23:16:06 -080074static ssize_t show_link_rate(struct device *cdev,
75 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070076{
77 char *pos = buf;
78 struct atm_dev *adev = to_atm_dev(cdev);
79 int link_rate;
80
81 /* show the link rate, not the data rate */
82 switch (adev->link_rate) {
Joe Perchesf0a6cb12010-01-26 11:40:03 +000083 case ATM_OC3_PCR:
84 link_rate = 155520000;
85 break;
86 case ATM_OC12_PCR:
87 link_rate = 622080000;
88 break;
89 case ATM_25_PCR:
90 link_rate = 25600000;
91 break;
92 default:
93 link_rate = adev->link_rate * 8 * 53;
Roman Kagan656d98b2006-06-29 12:36:34 -070094 }
95 pos += sprintf(pos, "%d\n", link_rate);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090096
Roman Kagan656d98b2006-06-29 12:36:34 -070097 return pos - buf;
98}
99
Kay Sieversef395922007-12-30 23:16:06 -0800100static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
101static DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL);
102static DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL);
103static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
104static DEVICE_ATTR(link_rate, S_IRUGO, 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,
109 &dev_attr_carrier,
110 &dev_attr_type,
111 &dev_attr_link_rate,
Roman Kagan656d98b2006-06-29 12:36:34 -0700112 NULL
113};
114
Kay Sieversef395922007-12-30 23:16:06 -0800115
116static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env)
Roman Kagan656d98b2006-06-29 12:36:34 -0700117{
118 struct atm_dev *adev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700119
120 if (!cdev)
121 return -ENODEV;
122
123 adev = to_atm_dev(cdev);
124 if (!adev)
125 return -ENODEV;
126
Kay Sievers7eff2e72007-08-14 15:15:12 +0200127 if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
Roman Kagan656d98b2006-06-29 12:36:34 -0700128 return -ENOMEM;
129
Roman Kagan656d98b2006-06-29 12:36:34 -0700130 return 0;
131}
132
Kay Sieversef395922007-12-30 23:16:06 -0800133static void atm_release(struct device *cdev)
Roman Kagan656d98b2006-06-29 12:36:34 -0700134{
135 struct atm_dev *adev = to_atm_dev(cdev);
136
137 kfree(adev);
138}
139
140static struct class atm_class = {
141 .name = "atm",
Kay Sieversef395922007-12-30 23:16:06 -0800142 .dev_release = atm_release,
143 .dev_uevent = atm_uevent,
Roman Kagan656d98b2006-06-29 12:36:34 -0700144};
145
146int atm_register_sysfs(struct atm_dev *adev)
147{
Kay Sieversef395922007-12-30 23:16:06 -0800148 struct device *cdev = &adev->class_dev;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700149 int i, j, err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700150
151 cdev->class = &atm_class;
Kay Sieversef395922007-12-30 23:16:06 -0800152 dev_set_drvdata(cdev, adev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700153
Kay Sieversfb28ad32008-11-10 13:55:14 -0800154 dev_set_name(cdev, "%s%d", adev->type, adev->number);
Kay Sieversef395922007-12-30 23:16:06 -0800155 err = device_register(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700156 if (err < 0)
157 return err;
158
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700159 for (i = 0; atm_attrs[i]; i++) {
Kay Sieversef395922007-12-30 23:16:06 -0800160 err = device_create_file(cdev, atm_attrs[i]);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700161 if (err)
162 goto err_out;
163 }
Roman Kagan656d98b2006-06-29 12:36:34 -0700164
165 return 0;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700166
167err_out:
168 for (j = 0; j < i; j++)
Kay Sieversef395922007-12-30 23:16:06 -0800169 device_remove_file(cdev, atm_attrs[j]);
170 device_del(cdev);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700171 return err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700172}
173
174void atm_unregister_sysfs(struct atm_dev *adev)
175{
Kay Sieversef395922007-12-30 23:16:06 -0800176 struct device *cdev = &adev->class_dev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700177
Kay Sieversef395922007-12-30 23:16:06 -0800178 device_del(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700179}
180
181int __init atm_sysfs_init(void)
182{
183 return class_register(&atm_class);
184}
185
186void __exit atm_sysfs_exit(void)
187{
188 class_unregister(&atm_class);
189}