Blame view

drivers/atm/adummy.c 4.08 KB
09c434b8a   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
fb2964499   Chas Williams   [ATM]: [adummy] d...
2
3
4
  /*
   * adummy.c: a dummy ATM driver
   */
fb2964499   Chas Williams   [ATM]: [adummy] d...
5
  #include <linux/module.h>
fb2964499   Chas Williams   [ATM]: [adummy] d...
6
7
  #include <linux/kernel.h>
  #include <linux/skbuff.h>
fb2964499   Chas Williams   [ATM]: [adummy] d...
8
9
10
11
12
13
  #include <linux/errno.h>
  #include <linux/types.h>
  #include <linux/string.h>
  #include <linux/delay.h>
  #include <linux/init.h>
  #include <linux/mm.h>
fb2964499   Chas Williams   [ATM]: [adummy] d...
14
15
  #include <linux/timer.h>
  #include <linux/interrupt.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
16
  #include <linux/slab.h>
fb2964499   Chas Williams   [ATM]: [adummy] d...
17
18
  #include <asm/io.h>
  #include <asm/byteorder.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
19
  #include <linux/uaccess.h>
fb2964499   Chas Williams   [ATM]: [adummy] d...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  
  #include <linux/atmdev.h>
  #include <linux/atm.h>
  #include <linux/sonet.h>
  
  /* version definition */
  
  #define DRV_VERSION "1.0"
  
  #define DEV_LABEL "adummy"
  
  #define ADUMMY_DEV(dev) ((struct adummy_dev *) (dev)->dev_data)
  
  struct adummy_dev {
  	struct atm_dev *atm_dev;
  
  	struct list_head entry;
  };
  
  /* globals */
  
  static LIST_HEAD(adummy_devs);
c69fb76e8   Karl Hiramoto   atm/adummy: add s...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  static ssize_t __set_signal(struct device *dev,
  		struct device_attribute *attr,
  		const char *buf, size_t len)
  {
  	struct atm_dev *atm_dev = container_of(dev, struct atm_dev, class_dev);
  	int signal;
  
  	if (sscanf(buf, "%d", &signal) == 1) {
  
  		if (signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND)
  			signal = ATM_PHY_SIG_UNKNOWN;
  
  		atm_dev_signal_change(atm_dev, signal);
  		return 1;
  	}
  	return -EINVAL;
  }
  
  static ssize_t __show_signal(struct device *dev,
  	struct device_attribute *attr, char *buf)
  {
  	struct atm_dev *atm_dev = container_of(dev, struct atm_dev, class_dev);
  	return sprintf(buf, "%d
  ", atm_dev->signal);
  }
  static DEVICE_ATTR(signal, 0644, __show_signal, __set_signal);
  
  static struct attribute *adummy_attrs[] = {
  	&dev_attr_signal.attr,
  	NULL
  };
444826a99   Amitoj Kaur Chawla   atm: adummy: cons...
73
  static const struct attribute_group adummy_group_attrs = {
c69fb76e8   Karl Hiramoto   atm/adummy: add s...
74
75
76
  	.name = NULL, /* We want them in dev's root folder */
  	.attrs = adummy_attrs
  };
fb2964499   Chas Williams   [ATM]: [adummy] d...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  static int __init
  adummy_start(struct atm_dev *dev)
  {
  	dev->ci_range.vpi_bits = 4;
  	dev->ci_range.vci_bits = 12;
  
  	return 0;
  }
  
  static int
  adummy_open(struct atm_vcc *vcc)
  {
  	short vpi = vcc->vpi;
  	int vci = vcc->vci;
  
  	if (vci == ATM_VCI_UNSPEC || vpi == ATM_VPI_UNSPEC)
  		return 0;
  
  	set_bit(ATM_VF_ADDR, &vcc->flags);
  	set_bit(ATM_VF_READY, &vcc->flags);
  
  	return 0;
  }
  
  static void
  adummy_close(struct atm_vcc *vcc)
  {
  	clear_bit(ATM_VF_READY, &vcc->flags);
  	clear_bit(ATM_VF_ADDR, &vcc->flags);
  }
  
  static int
  adummy_send(struct atm_vcc *vcc, struct sk_buff *skb)
  {
  	if (vcc->pop)
  		vcc->pop(vcc, skb);
  	else
  		dev_kfree_skb_any(skb);
  	atomic_inc(&vcc->stats->tx);
  
  	return 0;
  }
  
  static int
  adummy_proc_read(struct atm_dev *dev, loff_t *pos, char *page)
  {
  	int left = *pos;
  
  	if (!left--)
  		return sprintf(page, "version %s
  ", DRV_VERSION);
  
  	return 0;
  }
46c4b7a56   Bhumika Goyal   atm: make atmdev_...
131
  static const struct atmdev_ops adummy_ops =
fb2964499   Chas Williams   [ATM]: [adummy] d...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  {
  	.open =		adummy_open,
  	.close =	adummy_close,	
  	.send =		adummy_send,
  	.proc_read =	adummy_proc_read,
  	.owner =	THIS_MODULE
  };
  
  static int __init adummy_init(void)
  {
  	struct atm_dev *atm_dev;
  	struct adummy_dev *adummy_dev;
  	int err = 0;
  
  	printk(KERN_ERR "adummy: version %s
  ", DRV_VERSION);
0c1cca1d8   Om Narasimhan   [ATM]: kmalloc to...
148
  	adummy_dev = kzalloc(sizeof(struct adummy_dev),
fb2964499   Chas Williams   [ATM]: [adummy] d...
149
150
  						   GFP_KERNEL);
  	if (!adummy_dev) {
0c1cca1d8   Om Narasimhan   [ATM]: kmalloc to...
151
152
  		printk(KERN_ERR DEV_LABEL ": kzalloc() failed
  ");
fb2964499   Chas Williams   [ATM]: [adummy] d...
153
154
155
  		err = -ENOMEM;
  		goto out;
  	}
d9ca676bc   Dan Williams   atm: correct sysf...
156
  	atm_dev = atm_dev_register(DEV_LABEL, NULL, &adummy_ops, -1, NULL);
fb2964499   Chas Williams   [ATM]: [adummy] d...
157
158
159
160
161
162
163
164
165
  	if (!atm_dev) {
  		printk(KERN_ERR DEV_LABEL ": atm_dev_register() failed
  ");
  		err = -ENODEV;
  		goto out_kfree;
  	}
  
  	adummy_dev->atm_dev = atm_dev;
  	atm_dev->dev_data = adummy_dev;
c69fb76e8   Karl Hiramoto   atm/adummy: add s...
166
167
168
  	if (sysfs_create_group(&atm_dev->class_dev.kobj, &adummy_group_attrs))
  		dev_err(&atm_dev->class_dev, "Could not register attrs for adummy
  ");
fb2964499   Chas Williams   [ATM]: [adummy] d...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  	if (adummy_start(atm_dev)) {
  		printk(KERN_ERR DEV_LABEL ": adummy_start() failed
  ");
  		err = -ENODEV;
  		goto out_unregister;
  	}
  
  	list_add(&adummy_dev->entry, &adummy_devs);
  out:
  	return err;
  
  out_unregister:
  	atm_dev_deregister(atm_dev);
  out_kfree:
  	kfree(adummy_dev);
  	goto out;
  }
  
  static void __exit adummy_cleanup(void)
  {
  	struct adummy_dev *adummy_dev, *next;
  
  	list_for_each_entry_safe(adummy_dev, next, &adummy_devs, entry) {
  		atm_dev_deregister(adummy_dev->atm_dev);
  		kfree(adummy_dev);
  	}
  }
  
  module_init(adummy_init);
  module_exit(adummy_cleanup);
  
  MODULE_AUTHOR("chas williams <chas@cmf.nrl.navy.mil>");
  MODULE_DESCRIPTION("dummy ATM driver");
  MODULE_LICENSE("GPL");