Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 2 | /* ATM driver model support. */ |
| 3 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 4 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/slab.h> |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 6 | #include <linux/init.h> |
| 7 | #include <linux/kobject.h> |
| 8 | #include <linux/atmdev.h> |
| 9 | #include "common.h" |
| 10 | #include "resources.h" |
| 11 | |
| 12 | #define to_atm_dev(cldev) container_of(cldev, struct atm_dev, class_dev) |
| 13 | |
YueHaibing | 48afdae | 2021-05-20 21:36:45 +0800 | [diff] [blame] | 14 | static ssize_t type_show(struct device *cdev, |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 15 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 16 | { |
| 17 | struct atm_dev *adev = to_atm_dev(cdev); |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 18 | |
| 19 | return scnprintf(buf, PAGE_SIZE, "%s\n", adev->type); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 20 | } |
| 21 | |
YueHaibing | 48afdae | 2021-05-20 21:36:45 +0800 | [diff] [blame] | 22 | static ssize_t address_show(struct device *cdev, |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 23 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 24 | { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 25 | struct atm_dev *adev = to_atm_dev(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 26 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 27 | return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 28 | } |
| 29 | |
YueHaibing | 48afdae | 2021-05-20 21:36:45 +0800 | [diff] [blame] | 30 | static ssize_t atmaddress_show(struct device *cdev, |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 31 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 32 | { |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 33 | unsigned long flags; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 34 | struct atm_dev *adev = to_atm_dev(cdev); |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 35 | struct atm_dev_addr *aaddr; |
Andy Shevchenko | 79ac522 | 2020-01-23 16:20:02 +0200 | [diff] [blame] | 36 | int 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) { |
Andy Shevchenko | 79ac522 | 2020-01-23 16:20:02 +0200 | [diff] [blame] | 40 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 41 | "%1phN.%2phN.%10phN.%6phN.%1phN\n", |
| 42 | &aaddr->addr.sas_addr.prv[0], |
| 43 | &aaddr->addr.sas_addr.prv[1], |
| 44 | &aaddr->addr.sas_addr.prv[3], |
| 45 | &aaddr->addr.sas_addr.prv[13], |
| 46 | &aaddr->addr.sas_addr.prv[19]); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 47 | } |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 48 | spin_unlock_irqrestore(&adev->lock, flags); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 49 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 50 | return count; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 51 | } |
| 52 | |
YueHaibing | 48afdae | 2021-05-20 21:36:45 +0800 | [diff] [blame] | 53 | static ssize_t atmindex_show(struct device *cdev, |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 54 | struct device_attribute *attr, char *buf) |
| 55 | { |
| 56 | struct atm_dev *adev = to_atm_dev(cdev); |
| 57 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 58 | return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number); |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 59 | } |
| 60 | |
YueHaibing | 48afdae | 2021-05-20 21:36:45 +0800 | [diff] [blame] | 61 | static ssize_t carrier_show(struct device *cdev, |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 62 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 63 | { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 64 | struct atm_dev *adev = to_atm_dev(cdev); |
| 65 | |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 66 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
| 67 | adev->signal == ATM_PHY_SIG_LOST ? 0 : 1); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 68 | } |
| 69 | |
YueHaibing | 48afdae | 2021-05-20 21:36:45 +0800 | [diff] [blame] | 70 | static ssize_t link_rate_show(struct device *cdev, |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 71 | struct device_attribute *attr, char *buf) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 72 | { |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 73 | struct atm_dev *adev = to_atm_dev(cdev); |
| 74 | int link_rate; |
| 75 | |
| 76 | /* show the link rate, not the data rate */ |
| 77 | switch (adev->link_rate) { |
Joe Perches | f0a6cb1 | 2010-01-26 11:40:03 +0000 | [diff] [blame] | 78 | case ATM_OC3_PCR: |
| 79 | link_rate = 155520000; |
| 80 | break; |
| 81 | case ATM_OC12_PCR: |
| 82 | link_rate = 622080000; |
| 83 | break; |
| 84 | case ATM_25_PCR: |
| 85 | link_rate = 25600000; |
| 86 | break; |
| 87 | default: |
| 88 | link_rate = adev->link_rate * 8 * 53; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 89 | } |
chas williams - CONTRACTOR | 3c41777 | 2012-12-17 06:00:01 +0000 | [diff] [blame] | 90 | return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 91 | } |
| 92 | |
YueHaibing | 48afdae | 2021-05-20 21:36:45 +0800 | [diff] [blame] | 93 | static DEVICE_ATTR_RO(address); |
| 94 | static DEVICE_ATTR_RO(atmaddress); |
| 95 | static DEVICE_ATTR_RO(atmindex); |
| 96 | static DEVICE_ATTR_RO(carrier); |
| 97 | static DEVICE_ATTR_RO(type); |
| 98 | static DEVICE_ATTR_RO(link_rate); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 99 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 100 | static struct device_attribute *atm_attrs[] = { |
| 101 | &dev_attr_atmaddress, |
| 102 | &dev_attr_address, |
Dan Williams | e7a46b4 | 2011-05-27 04:51:54 +0000 | [diff] [blame] | 103 | &dev_attr_atmindex, |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 104 | &dev_attr_carrier, |
| 105 | &dev_attr_type, |
| 106 | &dev_attr_link_rate, |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 107 | NULL |
| 108 | }; |
| 109 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 110 | |
| 111 | static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 112 | { |
| 113 | struct atm_dev *adev; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 114 | |
| 115 | if (!cdev) |
| 116 | return -ENODEV; |
| 117 | |
| 118 | adev = to_atm_dev(cdev); |
| 119 | if (!adev) |
| 120 | return -ENODEV; |
| 121 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 122 | if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number)) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 123 | return -ENOMEM; |
| 124 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 128 | static void atm_release(struct device *cdev) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 129 | { |
| 130 | struct atm_dev *adev = to_atm_dev(cdev); |
| 131 | |
| 132 | kfree(adev); |
| 133 | } |
| 134 | |
| 135 | static struct class atm_class = { |
| 136 | .name = "atm", |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 137 | .dev_release = atm_release, |
| 138 | .dev_uevent = atm_uevent, |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Dan Williams | d9ca676 | 2010-12-08 19:40:47 +0000 | [diff] [blame] | 141 | int atm_register_sysfs(struct atm_dev *adev, struct device *parent) |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 142 | { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 143 | struct device *cdev = &adev->class_dev; |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 144 | int i, j, err; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 145 | |
| 146 | cdev->class = &atm_class; |
Dan Williams | d9ca676 | 2010-12-08 19:40:47 +0000 | [diff] [blame] | 147 | cdev->parent = parent; |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 148 | dev_set_drvdata(cdev, adev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 149 | |
Kay Sievers | fb28ad3 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 150 | dev_set_name(cdev, "%s%d", adev->type, adev->number); |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 151 | err = device_register(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 152 | if (err < 0) |
| 153 | return err; |
| 154 | |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 155 | for (i = 0; atm_attrs[i]; i++) { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 156 | err = device_create_file(cdev, atm_attrs[i]); |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 157 | if (err) |
| 158 | goto err_out; |
| 159 | } |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 160 | |
| 161 | return 0; |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 162 | |
| 163 | err_out: |
| 164 | for (j = 0; j < i; j++) |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 165 | device_remove_file(cdev, atm_attrs[j]); |
| 166 | device_del(cdev); |
Jeff Garzik | 97f80bc | 2006-10-20 19:48:42 -0700 | [diff] [blame] | 167 | return err; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void atm_unregister_sysfs(struct atm_dev *adev) |
| 171 | { |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 172 | struct device *cdev = &adev->class_dev; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 173 | |
Kay Sievers | ef39592 | 2007-12-30 23:16:06 -0800 | [diff] [blame] | 174 | device_del(cdev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | int __init atm_sysfs_init(void) |
| 178 | { |
| 179 | return class_register(&atm_class); |
| 180 | } |
| 181 | |
| 182 | void __exit atm_sysfs_exit(void) |
| 183 | { |
| 184 | class_unregister(&atm_class); |
| 185 | } |