blob: b5674dc2083d6766bbe4754d3951439fde141c0c [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
Kay Sieversef395922007-12-30 23:16:06 -080012static ssize_t show_type(struct device *cdev,
13 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070014{
15 struct atm_dev *adev = to_atm_dev(cdev);
16 return sprintf(buf, "%s\n", adev->type);
17}
18
Kay Sieversef395922007-12-30 23:16:06 -080019static ssize_t show_address(struct device *cdev,
20 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070021{
22 char *pos = buf;
23 struct atm_dev *adev = to_atm_dev(cdev);
24 int i;
25
26 for (i = 0; i < (ESI_LEN - 1); i++)
27 pos += sprintf(pos, "%02x:", adev->esi[i]);
28 pos += sprintf(pos, "%02x\n", adev->esi[i]);
29
30 return pos - buf;
31}
32
Kay Sieversef395922007-12-30 23:16:06 -080033static ssize_t show_atmaddress(struct device *cdev,
34 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070035{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090036 unsigned long flags;
Roman Kagan656d98b2006-06-29 12:36:34 -070037 char *pos = buf;
38 struct atm_dev *adev = to_atm_dev(cdev);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090039 struct atm_dev_addr *aaddr;
Roman Kagan656d98b2006-06-29 12:36:34 -070040 int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
41 int i, j;
42
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090043 spin_lock_irqsave(&adev->lock, flags);
44 list_for_each_entry(aaddr, &adev->local, entry) {
Roman Kagan656d98b2006-06-29 12:36:34 -070045 for(i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
46 if (j == *fmt) {
47 pos += sprintf(pos, ".");
48 ++fmt;
49 j = 0;
50 }
51 pos += sprintf(pos, "%02x", aaddr->addr.sas_addr.prv[i]);
52 }
53 pos += sprintf(pos, "\n");
54 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090055 spin_unlock_irqrestore(&adev->lock, flags);
Roman Kagan656d98b2006-06-29 12:36:34 -070056
57 return pos - buf;
58}
59
Kay Sieversef395922007-12-30 23:16:06 -080060static ssize_t show_carrier(struct device *cdev,
61 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070062{
63 char *pos = buf;
64 struct atm_dev *adev = to_atm_dev(cdev);
65
66 pos += sprintf(pos, "%d\n",
67 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090068
Roman Kagan656d98b2006-06-29 12:36:34 -070069 return pos - buf;
70}
71
Kay Sieversef395922007-12-30 23:16:06 -080072static ssize_t show_link_rate(struct device *cdev,
73 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070074{
75 char *pos = buf;
76 struct atm_dev *adev = to_atm_dev(cdev);
77 int link_rate;
78
79 /* show the link rate, not the data rate */
80 switch (adev->link_rate) {
81 case ATM_OC3_PCR:
82 link_rate = 155520000;
83 break;
84 case ATM_OC12_PCR:
85 link_rate = 622080000;
86 break;
87 case ATM_25_PCR:
88 link_rate = 25600000;
89 break;
90 default:
91 link_rate = adev->link_rate * 8 * 53;
92 }
93 pos += sprintf(pos, "%d\n", link_rate);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090094
Roman Kagan656d98b2006-06-29 12:36:34 -070095 return pos - buf;
96}
97
Kay Sieversef395922007-12-30 23:16:06 -080098static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
99static DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL);
100static DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL);
101static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
102static DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL);
Roman Kagan656d98b2006-06-29 12:36:34 -0700103
Kay Sieversef395922007-12-30 23:16:06 -0800104static struct device_attribute *atm_attrs[] = {
105 &dev_attr_atmaddress,
106 &dev_attr_address,
107 &dev_attr_carrier,
108 &dev_attr_type,
109 &dev_attr_link_rate,
Roman Kagan656d98b2006-06-29 12:36:34 -0700110 NULL
111};
112
Kay Sieversef395922007-12-30 23:16:06 -0800113
114static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env)
Roman Kagan656d98b2006-06-29 12:36:34 -0700115{
116 struct atm_dev *adev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700117
118 if (!cdev)
119 return -ENODEV;
120
121 adev = to_atm_dev(cdev);
122 if (!adev)
123 return -ENODEV;
124
Kay Sievers7eff2e72007-08-14 15:15:12 +0200125 if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
Roman Kagan656d98b2006-06-29 12:36:34 -0700126 return -ENOMEM;
127
Roman Kagan656d98b2006-06-29 12:36:34 -0700128 return 0;
129}
130
Kay Sieversef395922007-12-30 23:16:06 -0800131static void atm_release(struct device *cdev)
Roman Kagan656d98b2006-06-29 12:36:34 -0700132{
133 struct atm_dev *adev = to_atm_dev(cdev);
134
135 kfree(adev);
136}
137
138static struct class atm_class = {
139 .name = "atm",
Kay Sieversef395922007-12-30 23:16:06 -0800140 .dev_release = atm_release,
141 .dev_uevent = atm_uevent,
Roman Kagan656d98b2006-06-29 12:36:34 -0700142};
143
144int atm_register_sysfs(struct atm_dev *adev)
145{
Kay Sieversef395922007-12-30 23:16:06 -0800146 struct device *cdev = &adev->class_dev;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700147 int i, j, err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700148
149 cdev->class = &atm_class;
Kay Sieversef395922007-12-30 23:16:06 -0800150 dev_set_drvdata(cdev, adev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700151
Kay Sieversfb28ad32008-11-10 13:55:14 -0800152 dev_set_name(cdev, "%s%d", adev->type, adev->number);
Kay Sieversef395922007-12-30 23:16:06 -0800153 err = device_register(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700154 if (err < 0)
155 return err;
156
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700157 for (i = 0; atm_attrs[i]; i++) {
Kay Sieversef395922007-12-30 23:16:06 -0800158 err = device_create_file(cdev, atm_attrs[i]);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700159 if (err)
160 goto err_out;
161 }
Roman Kagan656d98b2006-06-29 12:36:34 -0700162
163 return 0;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700164
165err_out:
166 for (j = 0; j < i; j++)
Kay Sieversef395922007-12-30 23:16:06 -0800167 device_remove_file(cdev, atm_attrs[j]);
168 device_del(cdev);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700169 return err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700170}
171
172void atm_unregister_sysfs(struct atm_dev *adev)
173{
Kay Sieversef395922007-12-30 23:16:06 -0800174 struct device *cdev = &adev->class_dev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700175
Kay Sieversef395922007-12-30 23:16:06 -0800176 device_del(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700177}
178
179int __init atm_sysfs_init(void)
180{
181 return class_register(&atm_class);
182}
183
184void __exit atm_sysfs_exit(void)
185{
186 class_unregister(&atm_class);
187}