Linux Audio

Check our new training course

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