Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 1 | /* ATM driver model support. */ |
| 2 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 3 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 4 | #include <linux/slab.h> |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 5 | #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 Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 13 | static ssize_t show_type(struct device *cdev, |
| 14 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 15 | { |
| 16 | struct atm_dev *adev = to_atm_dev(cdev); |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 17 | |
| 18 | return scnprintf(buf, PAGE_SIZE, "%s\n", adev->type); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 21 | static ssize_t show_address(struct device *cdev, |
| 22 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 23 | { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 24 | struct atm_dev *adev = to_atm_dev(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 25 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 26 | return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 29 | static ssize_t show_atmaddress(struct device *cdev, |
| 30 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 31 | { |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 32 | unsigned long flags; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 33 | struct atm_dev *adev = to_atm_dev(cdev); |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 34 | struct atm_dev_addr *aaddr; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 35 | int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin; |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 36 | int i, j, count = 0; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 37 | |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 38 | spin_lock_irqsave(&adev->lock, flags); |
| 39 | list_for_each_entry(aaddr, &adev->local, entry) { |
Joe Perches | f0a6cb1 | 2010-01-26 11:40:03 +0000 | [diff] [blame] | 40 | for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 41 | if (j == *fmt) { |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 42 | count += scnprintf(buf + count, |
| 43 | PAGE_SIZE - count, "."); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 44 | ++fmt; |
| 45 | j = 0; |
| 46 | } |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 47 | count += scnprintf(buf + count, |
| 48 | PAGE_SIZE - count, "%02x", |
| 49 | aaddr->addr.sas_addr.prv[i]); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 50 | } |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 51 | count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 52 | } |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 53 | spin_unlock_irqrestore(&adev->lock, flags); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 54 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 55 | return count; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 58 | static 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 - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 63 | return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number); |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 66 | static ssize_t show_carrier(struct device *cdev, |
| 67 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 68 | { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 69 | struct atm_dev *adev = to_atm_dev(cdev); |
| 70 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 71 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
| 72 | adev->signal == ATM_PHY_SIG_LOST ? 0 : 1); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 75 | static ssize_t show_link_rate(struct device *cdev, |
| 76 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 77 | { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 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 Perches | f0a6cb1 | 2010-01-26 11:40:03 +0000 | [diff] [blame] | 83 | 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 Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 94 | } |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 95 | return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 98 | static DEVICE_ATTR(address, S_IRUGO, show_address, NULL); |
| 99 | static DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL); |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 100 | static DEVICE_ATTR(atmindex, S_IRUGO, show_atmindex, NULL); |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 101 | static DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL); |
| 102 | static DEVICE_ATTR(type, S_IRUGO, show_type, NULL); |
| 103 | static DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 104 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 105 | static struct device_attribute *atm_attrs[] = { |
| 106 | &dev_attr_atmaddress, |
| 107 | &dev_attr_address, |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 108 | &dev_attr_atmindex, |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 109 | &dev_attr_carrier, |
| 110 | &dev_attr_type, |
| 111 | &dev_attr_link_rate, |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 112 | NULL |
| 113 | }; |
| 114 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 115 | |
| 116 | static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 117 | { |
| 118 | struct atm_dev *adev; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 119 | |
| 120 | if (!cdev) |
| 121 | return -ENODEV; |
| 122 | |
| 123 | adev = to_atm_dev(cdev); |
| 124 | if (!adev) |
| 125 | return -ENODEV; |
| 126 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 127 | if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number)) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 128 | return -ENOMEM; |
| 129 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 133 | static void atm_release(struct device *cdev) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 134 | { |
| 135 | struct atm_dev *adev = to_atm_dev(cdev); |
| 136 | |
| 137 | kfree(adev); |
| 138 | } |
| 139 | |
| 140 | static struct class atm_class = { |
| 141 | .name = "atm", |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 142 | .dev_release = atm_release, |
| 143 | .dev_uevent = atm_uevent, |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Dan Williams | d9ca676 | 2010-12-08 19:40:47 +0000 | [diff] [blame] | 146 | int atm_register_sysfs(struct atm_dev *adev, struct device *parent) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 147 | { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 148 | struct device *cdev = &adev->class_dev; |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 149 | int i, j, err; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 150 | |
| 151 | cdev->class = &atm_class; |
Dan Williams | d9ca676 | 2010-12-08 19:40:47 +0000 | [diff] [blame] | 152 | cdev->parent = parent; |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 153 | dev_set_drvdata(cdev, adev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 154 | |
Kay Sievers | fb28ad35 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 155 | dev_set_name(cdev, "%s%d", adev->type, adev->number); |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 156 | err = device_register(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 157 | if (err < 0) |
| 158 | return err; |
| 159 | |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 160 | for (i = 0; atm_attrs[i]; i++) { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 161 | err = device_create_file(cdev, atm_attrs[i]); |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 162 | if (err) |
| 163 | goto err_out; |
| 164 | } |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 165 | |
| 166 | return 0; |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 167 | |
| 168 | err_out: |
| 169 | for (j = 0; j < i; j++) |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 170 | device_remove_file(cdev, atm_attrs[j]); |
| 171 | device_del(cdev); |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 172 | return err; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void atm_unregister_sysfs(struct atm_dev *adev) |
| 176 | { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 177 | struct device *cdev = &adev->class_dev; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 178 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 179 | device_del(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | int __init atm_sysfs_init(void) |
| 183 | { |
| 184 | return class_register(&atm_class); |
| 185 | } |
| 186 | |
| 187 | void __exit atm_sysfs_exit(void) |
| 188 | { |
| 189 | class_unregister(&atm_class); |
| 190 | } |