Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/* ATM driver model support. */
  3
  4#include <linux/kernel.h>
  5#include <linux/slab.h>
  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
 14static ssize_t show_type(struct device *cdev,
 15			 struct device_attribute *attr, char *buf)
 16{
 17	struct atm_dev *adev = to_atm_dev(cdev);
 18
 19	return scnprintf(buf, PAGE_SIZE, "%s\n", adev->type);
 20}
 21
 22static ssize_t show_address(struct device *cdev,
 23			    struct device_attribute *attr, char *buf)
 24{
 25	struct atm_dev *adev = to_atm_dev(cdev);
 26
 27	return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi);
 28}
 29
 30static ssize_t show_atmaddress(struct device *cdev,
 31			       struct device_attribute *attr, char *buf)
 32{
 33	unsigned long flags;
 34	struct atm_dev *adev = to_atm_dev(cdev);
 35	struct atm_dev_addr *aaddr;
 36	int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
 37	int i, j, count = 0;
 38
 39	spin_lock_irqsave(&adev->lock, flags);
 40	list_for_each_entry(aaddr, &adev->local, entry) {
 41		for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
 42			if (j == *fmt) {
 43				count += scnprintf(buf + count,
 44						   PAGE_SIZE - count, ".");
 45				++fmt;
 46				j = 0;
 47			}
 48			count += scnprintf(buf + count,
 49					   PAGE_SIZE - count, "%02x",
 50					   aaddr->addr.sas_addr.prv[i]);
 51		}
 52		count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
 53	}
 54	spin_unlock_irqrestore(&adev->lock, flags);
 55
 56	return count;
 57}
 58
 59static ssize_t show_atmindex(struct device *cdev,
 60			     struct device_attribute *attr, char *buf)
 61{
 62	struct atm_dev *adev = to_atm_dev(cdev);
 63
 64	return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number);
 65}
 66
 67static ssize_t show_carrier(struct device *cdev,
 68			    struct device_attribute *attr, char *buf)
 69{
 70	struct atm_dev *adev = to_atm_dev(cdev);
 71
 72	return scnprintf(buf, PAGE_SIZE, "%d\n",
 73			 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
 74}
 75
 76static ssize_t show_link_rate(struct device *cdev,
 77			      struct device_attribute *attr, char *buf)
 78{
 79	struct atm_dev *adev = to_atm_dev(cdev);
 80	int link_rate;
 81
 82	/* show the link rate, not the data rate */
 83	switch (adev->link_rate) {
 84	case ATM_OC3_PCR:
 85		link_rate = 155520000;
 86		break;
 87	case ATM_OC12_PCR:
 88		link_rate = 622080000;
 89		break;
 90	case ATM_25_PCR:
 91		link_rate = 25600000;
 92		break;
 93	default:
 94		link_rate = adev->link_rate * 8 * 53;
 95	}
 96	return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate);
 97}
 98
 99static DEVICE_ATTR(address, 0444, show_address, NULL);
100static DEVICE_ATTR(atmaddress, 0444, show_atmaddress, NULL);
101static DEVICE_ATTR(atmindex, 0444, show_atmindex, NULL);
102static DEVICE_ATTR(carrier, 0444, show_carrier, NULL);
103static DEVICE_ATTR(type, 0444, show_type, NULL);
104static DEVICE_ATTR(link_rate, 0444, show_link_rate, NULL);
105
106static struct device_attribute *atm_attrs[] = {
107	&dev_attr_atmaddress,
108	&dev_attr_address,
109	&dev_attr_atmindex,
110	&dev_attr_carrier,
111	&dev_attr_type,
112	&dev_attr_link_rate,
113	NULL
114};
115
116
117static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env)
118{
119	struct atm_dev *adev;
120
121	if (!cdev)
122		return -ENODEV;
123
124	adev = to_atm_dev(cdev);
125	if (!adev)
126		return -ENODEV;
127
128	if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
129		return -ENOMEM;
130
131	return 0;
132}
133
134static void atm_release(struct device *cdev)
135{
136	struct atm_dev *adev = to_atm_dev(cdev);
137
138	kfree(adev);
139}
140
141static struct class atm_class = {
142	.name		= "atm",
143	.dev_release	= atm_release,
144	.dev_uevent		= atm_uevent,
145};
146
147int atm_register_sysfs(struct atm_dev *adev, struct device *parent)
148{
149	struct device *cdev = &adev->class_dev;
150	int i, j, err;
151
152	cdev->class = &atm_class;
153	cdev->parent = parent;
154	dev_set_drvdata(cdev, adev);
155
156	dev_set_name(cdev, "%s%d", adev->type, adev->number);
157	err = device_register(cdev);
158	if (err < 0)
159		return err;
160
161	for (i = 0; atm_attrs[i]; i++) {
162		err = device_create_file(cdev, atm_attrs[i]);
163		if (err)
164			goto err_out;
165	}
166
167	return 0;
168
169err_out:
170	for (j = 0; j < i; j++)
171		device_remove_file(cdev, atm_attrs[j]);
172	device_del(cdev);
173	return err;
174}
175
176void atm_unregister_sysfs(struct atm_dev *adev)
177{
178	struct device *cdev = &adev->class_dev;
179
180	device_del(cdev);
181}
182
183int __init atm_sysfs_init(void)
184{
185	return class_register(&atm_class);
186}
187
188void __exit atm_sysfs_exit(void)
189{
190	class_unregister(&atm_class);
191}
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/* ATM driver model support. */
  3
  4#include <linux/kernel.h>
  5#include <linux/slab.h>
  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
 14static ssize_t type_show(struct device *cdev,
 15			 struct device_attribute *attr, char *buf)
 16{
 17	struct atm_dev *adev = to_atm_dev(cdev);
 18
 19	return scnprintf(buf, PAGE_SIZE, "%s\n", adev->type);
 20}
 21
 22static ssize_t address_show(struct device *cdev,
 23			    struct device_attribute *attr, char *buf)
 24{
 25	struct atm_dev *adev = to_atm_dev(cdev);
 26
 27	return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi);
 28}
 29
 30static ssize_t atmaddress_show(struct device *cdev,
 31			       struct device_attribute *attr, char *buf)
 32{
 33	unsigned long flags;
 34	struct atm_dev *adev = to_atm_dev(cdev);
 35	struct atm_dev_addr *aaddr;
 36	int count = 0;
 
 37
 38	spin_lock_irqsave(&adev->lock, flags);
 39	list_for_each_entry(aaddr, &adev->local, entry) {
 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]);
 
 
 
 
 
 47	}
 48	spin_unlock_irqrestore(&adev->lock, flags);
 49
 50	return count;
 51}
 52
 53static ssize_t atmindex_show(struct device *cdev,
 54			     struct device_attribute *attr, char *buf)
 55{
 56	struct atm_dev *adev = to_atm_dev(cdev);
 57
 58	return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number);
 59}
 60
 61static ssize_t carrier_show(struct device *cdev,
 62			    struct device_attribute *attr, char *buf)
 63{
 64	struct atm_dev *adev = to_atm_dev(cdev);
 65
 66	return scnprintf(buf, PAGE_SIZE, "%d\n",
 67			 adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
 68}
 69
 70static ssize_t link_rate_show(struct device *cdev,
 71			      struct device_attribute *attr, char *buf)
 72{
 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) {
 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;
 89	}
 90	return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate);
 91}
 92
 93static DEVICE_ATTR_RO(address);
 94static DEVICE_ATTR_RO(atmaddress);
 95static DEVICE_ATTR_RO(atmindex);
 96static DEVICE_ATTR_RO(carrier);
 97static DEVICE_ATTR_RO(type);
 98static DEVICE_ATTR_RO(link_rate);
 99
100static struct device_attribute *atm_attrs[] = {
101	&dev_attr_atmaddress,
102	&dev_attr_address,
103	&dev_attr_atmindex,
104	&dev_attr_carrier,
105	&dev_attr_type,
106	&dev_attr_link_rate,
107	NULL
108};
109
110
111static int atm_uevent(const struct device *cdev, struct kobj_uevent_env *env)
112{
113	const struct atm_dev *adev;
114
115	if (!cdev)
116		return -ENODEV;
117
118	adev = to_atm_dev(cdev);
119	if (!adev)
120		return -ENODEV;
121
122	if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
123		return -ENOMEM;
124
125	return 0;
126}
127
128static void atm_release(struct device *cdev)
129{
130	struct atm_dev *adev = to_atm_dev(cdev);
131
132	kfree(adev);
133}
134
135static struct class atm_class = {
136	.name		= "atm",
137	.dev_release	= atm_release,
138	.dev_uevent		= atm_uevent,
139};
140
141int atm_register_sysfs(struct atm_dev *adev, struct device *parent)
142{
143	struct device *cdev = &adev->class_dev;
144	int i, j, err;
145
146	cdev->class = &atm_class;
147	cdev->parent = parent;
148	dev_set_drvdata(cdev, adev);
149
150	dev_set_name(cdev, "%s%d", adev->type, adev->number);
151	err = device_register(cdev);
152	if (err < 0)
153		return err;
154
155	for (i = 0; atm_attrs[i]; i++) {
156		err = device_create_file(cdev, atm_attrs[i]);
157		if (err)
158			goto err_out;
159	}
160
161	return 0;
162
163err_out:
164	for (j = 0; j < i; j++)
165		device_remove_file(cdev, atm_attrs[j]);
166	device_del(cdev);
167	return err;
168}
169
170void atm_unregister_sysfs(struct atm_dev *adev)
171{
172	struct device *cdev = &adev->class_dev;
173
174	device_del(cdev);
175}
176
177int __init atm_sysfs_init(void)
178{
179	return class_register(&atm_class);
180}
181
182void __exit atm_sysfs_exit(void)
183{
184	class_unregister(&atm_class);
185}