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> |
| 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 Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 12 | static ssize_t show_type(struct device *cdev, |
| 13 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 14 | { |
| 15 | struct atm_dev *adev = to_atm_dev(cdev); |
| 16 | return sprintf(buf, "%s\n", adev->type); |
| 17 | } |
| 18 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 19 | static ssize_t show_address(struct device *cdev, |
| 20 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 21 | { |
| 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 Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 33 | static ssize_t show_atmaddress(struct device *cdev, |
| 34 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 35 | { |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 36 | unsigned long flags; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 37 | char *pos = buf; |
| 38 | struct atm_dev *adev = to_atm_dev(cdev); |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 39 | struct atm_dev_addr *aaddr; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 40 | int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin; |
| 41 | int i, j; |
| 42 | |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 43 | spin_lock_irqsave(&adev->lock, flags); |
| 44 | list_for_each_entry(aaddr, &adev->local, entry) { |
Joe Perches | f0a6cb1 | 2010-01-26 11:40:03 +0000 | [diff] [blame] | 45 | for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 46 | if (j == *fmt) { |
| 47 | pos += sprintf(pos, "."); |
| 48 | ++fmt; |
| 49 | j = 0; |
| 50 | } |
Joe Perches | f0a6cb1 | 2010-01-26 11:40:03 +0000 | [diff] [blame] | 51 | pos += sprintf(pos, "%02x", |
| 52 | aaddr->addr.sas_addr.prv[i]); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 53 | } |
| 54 | pos += sprintf(pos, "\n"); |
| 55 | } |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 56 | spin_unlock_irqrestore(&adev->lock, flags); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 57 | |
| 58 | return pos - buf; |
| 59 | } |
| 60 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 61 | static ssize_t show_carrier(struct device *cdev, |
| 62 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 63 | { |
| 64 | char *pos = buf; |
| 65 | struct atm_dev *adev = to_atm_dev(cdev); |
| 66 | |
| 67 | pos += sprintf(pos, "%d\n", |
| 68 | adev->signal == ATM_PHY_SIG_LOST ? 0 : 1); |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 69 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 70 | return pos - buf; |
| 71 | } |
| 72 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 73 | static ssize_t show_link_rate(struct device *cdev, |
| 74 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 75 | { |
| 76 | char *pos = buf; |
| 77 | struct atm_dev *adev = to_atm_dev(cdev); |
| 78 | int link_rate; |
| 79 | |
| 80 | /* show the link rate, not the data rate */ |
| 81 | switch (adev->link_rate) { |
Joe Perches | f0a6cb1 | 2010-01-26 11:40:03 +0000 | [diff] [blame] | 82 | case ATM_OC3_PCR: |
| 83 | link_rate = 155520000; |
| 84 | break; |
| 85 | case ATM_OC12_PCR: |
| 86 | link_rate = 622080000; |
| 87 | break; |
| 88 | case ATM_25_PCR: |
| 89 | link_rate = 25600000; |
| 90 | break; |
| 91 | default: |
| 92 | link_rate = adev->link_rate * 8 * 53; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 93 | } |
| 94 | pos += sprintf(pos, "%d\n", link_rate); |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 95 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 96 | return pos - buf; |
| 97 | } |
| 98 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 99 | static DEVICE_ATTR(address, S_IRUGO, show_address, NULL); |
| 100 | static DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL); |
| 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, |
| 108 | &dev_attr_carrier, |
| 109 | &dev_attr_type, |
| 110 | &dev_attr_link_rate, |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 111 | NULL |
| 112 | }; |
| 113 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 114 | |
| 115 | static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 116 | { |
| 117 | struct atm_dev *adev; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 118 | |
| 119 | if (!cdev) |
| 120 | return -ENODEV; |
| 121 | |
| 122 | adev = to_atm_dev(cdev); |
| 123 | if (!adev) |
| 124 | return -ENODEV; |
| 125 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 126 | if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number)) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 127 | return -ENOMEM; |
| 128 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 132 | static void atm_release(struct device *cdev) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 133 | { |
| 134 | struct atm_dev *adev = to_atm_dev(cdev); |
| 135 | |
| 136 | kfree(adev); |
| 137 | } |
| 138 | |
| 139 | static struct class atm_class = { |
| 140 | .name = "atm", |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 141 | .dev_release = atm_release, |
| 142 | .dev_uevent = atm_uevent, |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | int atm_register_sysfs(struct atm_dev *adev) |
| 146 | { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 147 | struct device *cdev = &adev->class_dev; |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 148 | int i, j, err; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 149 | |
| 150 | cdev->class = &atm_class; |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 151 | dev_set_drvdata(cdev, adev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 152 | |
Kay Sievers | fb28ad3 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 153 | dev_set_name(cdev, "%s%d", adev->type, adev->number); |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 154 | err = device_register(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 155 | if (err < 0) |
| 156 | return err; |
| 157 | |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 158 | for (i = 0; atm_attrs[i]; i++) { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 159 | err = device_create_file(cdev, atm_attrs[i]); |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 160 | if (err) |
| 161 | goto err_out; |
| 162 | } |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 163 | |
| 164 | return 0; |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 165 | |
| 166 | err_out: |
| 167 | for (j = 0; j < i; j++) |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 168 | device_remove_file(cdev, atm_attrs[j]); |
| 169 | device_del(cdev); |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 170 | return err; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void atm_unregister_sysfs(struct atm_dev *adev) |
| 174 | { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 175 | struct device *cdev = &adev->class_dev; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 176 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 177 | device_del(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | int __init atm_sysfs_init(void) |
| 181 | { |
| 182 | return class_register(&atm_class); |
| 183 | } |
| 184 | |
| 185 | void __exit atm_sysfs_exit(void) |
| 186 | { |
| 187 | class_unregister(&atm_class); |
| 188 | } |