blob: 9ef07eda2c437be45c4fbba4a984c2ca04d929c9 [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>
4#include <linux/init.h>
5#include <linux/kobject.h>
6#include <linux/atmdev.h>
7#include "common.h"
8#include "resources.h"
9
10#define to_atm_dev(cldev) container_of(cldev, struct atm_dev, class_dev)
11
12static ssize_t show_type(struct class_device *cdev, char *buf)
13{
14 struct atm_dev *adev = to_atm_dev(cdev);
15 return sprintf(buf, "%s\n", adev->type);
16}
17
18static ssize_t show_address(struct class_device *cdev, char *buf)
19{
20 char *pos = buf;
21 struct atm_dev *adev = to_atm_dev(cdev);
22 int i;
23
24 for (i = 0; i < (ESI_LEN - 1); i++)
25 pos += sprintf(pos, "%02x:", adev->esi[i]);
26 pos += sprintf(pos, "%02x\n", adev->esi[i]);
27
28 return pos - buf;
29}
30
31static ssize_t show_atmaddress(struct class_device *cdev, char *buf)
32{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090033 unsigned long flags;
Roman Kagan656d98b2006-06-29 12:36:34 -070034 char *pos = buf;
35 struct atm_dev *adev = to_atm_dev(cdev);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090036 struct atm_dev_addr *aaddr;
Roman Kagan656d98b2006-06-29 12:36:34 -070037 int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
38 int i, j;
39
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090040 spin_lock_irqsave(&adev->lock, flags);
41 list_for_each_entry(aaddr, &adev->local, entry) {
Roman Kagan656d98b2006-06-29 12:36:34 -070042 for(i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
43 if (j == *fmt) {
44 pos += sprintf(pos, ".");
45 ++fmt;
46 j = 0;
47 }
48 pos += sprintf(pos, "%02x", aaddr->addr.sas_addr.prv[i]);
49 }
50 pos += sprintf(pos, "\n");
51 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090052 spin_unlock_irqrestore(&adev->lock, flags);
Roman Kagan656d98b2006-06-29 12:36:34 -070053
54 return pos - buf;
55}
56
57static ssize_t show_carrier(struct class_device *cdev, char *buf)
58{
59 char *pos = buf;
60 struct atm_dev *adev = to_atm_dev(cdev);
61
62 pos += sprintf(pos, "%d\n",
63 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090064
Roman Kagan656d98b2006-06-29 12:36:34 -070065 return pos - buf;
66}
67
68static ssize_t show_link_rate(struct class_device *cdev, char *buf)
69{
70 char *pos = buf;
71 struct atm_dev *adev = to_atm_dev(cdev);
72 int link_rate;
73
74 /* show the link rate, not the data rate */
75 switch (adev->link_rate) {
76 case ATM_OC3_PCR:
77 link_rate = 155520000;
78 break;
79 case ATM_OC12_PCR:
80 link_rate = 622080000;
81 break;
82 case ATM_25_PCR:
83 link_rate = 25600000;
84 break;
85 default:
86 link_rate = adev->link_rate * 8 * 53;
87 }
88 pos += sprintf(pos, "%d\n", link_rate);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090089
Roman Kagan656d98b2006-06-29 12:36:34 -070090 return pos - buf;
91}
92
93static CLASS_DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
94static CLASS_DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL);
95static CLASS_DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL);
96static CLASS_DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
97static CLASS_DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL);
98
99static struct class_device_attribute *atm_attrs[] = {
100 &class_device_attr_atmaddress,
101 &class_device_attr_address,
102 &class_device_attr_carrier,
103 &class_device_attr_type,
104 &class_device_attr_link_rate,
105 NULL
106};
107
Kay Sievers7eff2e72007-08-14 15:15:12 +0200108static int atm_uevent(struct class_device *cdev, struct kobj_uevent_env *env)
Roman Kagan656d98b2006-06-29 12:36:34 -0700109{
110 struct atm_dev *adev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700111
112 if (!cdev)
113 return -ENODEV;
114
115 adev = to_atm_dev(cdev);
116 if (!adev)
117 return -ENODEV;
118
Kay Sievers7eff2e72007-08-14 15:15:12 +0200119 if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
Roman Kagan656d98b2006-06-29 12:36:34 -0700120 return -ENOMEM;
121
Roman Kagan656d98b2006-06-29 12:36:34 -0700122 return 0;
123}
124
125static void atm_release(struct class_device *cdev)
126{
127 struct atm_dev *adev = to_atm_dev(cdev);
128
129 kfree(adev);
130}
131
132static struct class atm_class = {
133 .name = "atm",
134 .release = atm_release,
135 .uevent = atm_uevent,
136};
137
138int atm_register_sysfs(struct atm_dev *adev)
139{
140 struct class_device *cdev = &adev->class_dev;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700141 int i, j, err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700142
143 cdev->class = &atm_class;
144 class_set_devdata(cdev, adev);
145
146 snprintf(cdev->class_id, BUS_ID_SIZE, "%s%d", adev->type, adev->number);
147 err = class_device_register(cdev);
148 if (err < 0)
149 return err;
150
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700151 for (i = 0; atm_attrs[i]; i++) {
152 err = class_device_create_file(cdev, atm_attrs[i]);
153 if (err)
154 goto err_out;
155 }
Roman Kagan656d98b2006-06-29 12:36:34 -0700156
157 return 0;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700158
159err_out:
160 for (j = 0; j < i; j++)
161 class_device_remove_file(cdev, atm_attrs[j]);
162 class_device_del(cdev);
163 return err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700164}
165
166void atm_unregister_sysfs(struct atm_dev *adev)
167{
168 struct class_device *cdev = &adev->class_dev;
169
170 class_device_del(cdev);
171}
172
173int __init atm_sysfs_init(void)
174{
175 return class_register(&atm_class);
176}
177
178void __exit atm_sysfs_exit(void)
179{
180 class_unregister(&atm_class);
181}