blob: 350bf62b2ae30023b251070544781328dbd30220 [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);
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000017
18 return scnprintf(buf, PAGE_SIZE, "%s\n", adev->type);
Roman Kagan656d98b2006-06-29 12:36:34 -070019}
20
Kay Sieversef395922007-12-30 23:16:06 -080021static ssize_t show_address(struct device *cdev,
22 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070023{
Roman Kagan656d98b2006-06-29 12:36:34 -070024 struct atm_dev *adev = to_atm_dev(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -070025
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000026 return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi);
Roman Kagan656d98b2006-06-29 12:36:34 -070027}
28
Kay Sieversef395922007-12-30 23:16:06 -080029static ssize_t show_atmaddress(struct device *cdev,
30 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070031{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090032 unsigned long flags;
Roman Kagan656d98b2006-06-29 12:36:34 -070033 struct atm_dev *adev = to_atm_dev(cdev);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090034 struct atm_dev_addr *aaddr;
Roman Kagan656d98b2006-06-29 12:36:34 -070035 int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000036 int i, j, count = 0;
Roman Kagan656d98b2006-06-29 12:36:34 -070037
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090038 spin_lock_irqsave(&adev->lock, flags);
39 list_for_each_entry(aaddr, &adev->local, entry) {
Joe Perchesf0a6cb12010-01-26 11:40:03 +000040 for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
Roman Kagan656d98b2006-06-29 12:36:34 -070041 if (j == *fmt) {
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000042 count += scnprintf(buf + count,
43 PAGE_SIZE - count, ".");
Roman Kagan656d98b2006-06-29 12:36:34 -070044 ++fmt;
45 j = 0;
46 }
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000047 count += scnprintf(buf + count,
48 PAGE_SIZE - count, "%02x",
49 aaddr->addr.sas_addr.prv[i]);
Roman Kagan656d98b2006-06-29 12:36:34 -070050 }
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000051 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
Roman Kagan656d98b2006-06-29 12:36:34 -070052 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090053 spin_unlock_irqrestore(&adev->lock, flags);
Roman Kagan656d98b2006-06-29 12:36:34 -070054
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000055 return count;
Roman Kagan656d98b2006-06-29 12:36:34 -070056}
57
Dan Williamse7a46b42011-05-27 04:51:54 +000058static ssize_t show_atmindex(struct device *cdev,
59 struct device_attribute *attr, char *buf)
60{
61 struct atm_dev *adev = to_atm_dev(cdev);
62
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000063 return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number);
Dan Williamse7a46b42011-05-27 04:51:54 +000064}
65
Kay Sieversef395922007-12-30 23:16:06 -080066static ssize_t show_carrier(struct device *cdev,
67 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070068{
Roman Kagan656d98b2006-06-29 12:36:34 -070069 struct atm_dev *adev = to_atm_dev(cdev);
70
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000071 return scnprintf(buf, PAGE_SIZE, "%d\n",
72 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
Roman Kagan656d98b2006-06-29 12:36:34 -070073}
74
Kay Sieversef395922007-12-30 23:16:06 -080075static ssize_t show_link_rate(struct device *cdev,
76 struct device_attribute *attr, char *buf)
Roman Kagan656d98b2006-06-29 12:36:34 -070077{
Roman Kagan656d98b2006-06-29 12:36:34 -070078 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 }
chas williams - CONTRACTOR3c417772012-12-17 06:00:01 +000095 return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate);
Roman Kagan656d98b2006-06-29 12:36:34 -070096}
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);
Dan Williamse7a46b42011-05-27 04:51:54 +0000100static DEVICE_ATTR(atmindex, S_IRUGO, show_atmindex, NULL);
Kay Sieversef395922007-12-30 23:16:06 -0800101static DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL);
102static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
103static DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL);
Roman Kagan656d98b2006-06-29 12:36:34 -0700104
Kay Sieversef395922007-12-30 23:16:06 -0800105static struct device_attribute *atm_attrs[] = {
106 &dev_attr_atmaddress,
107 &dev_attr_address,
Dan Williamse7a46b42011-05-27 04:51:54 +0000108 &dev_attr_atmindex,
Kay Sieversef395922007-12-30 23:16:06 -0800109 &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
Dan Williamsd9ca6762010-12-08 19:40:47 +0000146int atm_register_sysfs(struct atm_dev *adev, struct device *parent)
Roman Kagan656d98b2006-06-29 12:36:34 -0700147{
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;
Dan Williamsd9ca6762010-12-08 19:40:47 +0000152 cdev->parent = parent;
Kay Sieversef395922007-12-30 23:16:06 -0800153 dev_set_drvdata(cdev, adev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700154
Kay Sieversfb28ad32008-11-10 13:55:14 -0800155 dev_set_name(cdev, "%s%d", adev->type, adev->number);
Kay Sieversef395922007-12-30 23:16:06 -0800156 err = device_register(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700157 if (err < 0)
158 return err;
159
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700160 for (i = 0; atm_attrs[i]; i++) {
Kay Sieversef395922007-12-30 23:16:06 -0800161 err = device_create_file(cdev, atm_attrs[i]);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700162 if (err)
163 goto err_out;
164 }
Roman Kagan656d98b2006-06-29 12:36:34 -0700165
166 return 0;
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700167
168err_out:
169 for (j = 0; j < i; j++)
Kay Sieversef395922007-12-30 23:16:06 -0800170 device_remove_file(cdev, atm_attrs[j]);
171 device_del(cdev);
Jeff Garzik97f80bc2006-10-20 19:48:42 -0700172 return err;
Roman Kagan656d98b2006-06-29 12:36:34 -0700173}
174
175void atm_unregister_sysfs(struct atm_dev *adev)
176{
Kay Sieversef395922007-12-30 23:16:06 -0800177 struct device *cdev = &adev->class_dev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700178
Kay Sieversef395922007-12-30 23:16:06 -0800179 device_del(cdev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700180}
181
182int __init atm_sysfs_init(void)
183{
184 return class_register(&atm_class);
185}
186
187void __exit atm_sysfs_exit(void)
188{
189 class_unregister(&atm_class);
190}