1 /* net/atm/resources.c - Statically allocated resources */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
6 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7 * 2002/01 - don't free the whole struct sock on sk->destruct time,
8 * use the default destruct function initialized by sock_init_data */
11 #include <linux/config.h>
12 #include <linux/ctype.h>
13 #include <linux/string.h>
14 #include <linux/atmdev.h>
15 #include <linux/sonet.h>
16 #include <linux/kernel.h> /* for barrier */
17 #include <linux/module.h>
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
20 #include <net/sock.h> /* for struct sock */
23 #include "resources.h"
28 DEFINE_SPINLOCK(atm_dev_lock);
30 static struct atm_dev *__alloc_atm_dev(const char *type)
34 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
37 memset(dev, 0, sizeof(*dev));
39 dev->signal = ATM_PHY_SIG_UNKNOWN;
40 dev->link_rate = ATM_OC3_PCR;
41 spin_lock_init(&dev->lock);
42 INIT_LIST_HEAD(&dev->local);
47 static void __free_atm_dev(struct atm_dev *dev)
52 static struct atm_dev *__atm_dev_lookup(int number)
57 list_for_each(p, &atm_devs) {
58 dev = list_entry(p, struct atm_dev, dev_list);
59 if ((dev->ops) && (dev->number == number)) {
67 struct atm_dev *atm_dev_lookup(int number)
71 spin_lock(&atm_dev_lock);
72 dev = __atm_dev_lookup(number);
73 spin_unlock(&atm_dev_lock);
77 struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops,
78 int number, unsigned long *flags)
80 struct atm_dev *dev, *inuse;
82 dev = __alloc_atm_dev(type);
84 printk(KERN_ERR "atm_dev_register: no space for dev %s\n",
88 spin_lock(&atm_dev_lock);
90 if ((inuse = __atm_dev_lookup(number))) {
92 spin_unlock(&atm_dev_lock);
99 while ((inuse = __atm_dev_lookup(dev->number))) {
109 memset(&dev->flags, 0, sizeof(dev->flags));
110 memset(&dev->stats, 0, sizeof(dev->stats));
111 atomic_set(&dev->refcnt, 1);
112 list_add_tail(&dev->dev_list, &atm_devs);
113 spin_unlock(&atm_dev_lock);
115 if (atm_proc_dev_register(dev) < 0) {
116 printk(KERN_ERR "atm_dev_register: "
117 "atm_proc_dev_register failed for dev %s\n",
119 spin_lock(&atm_dev_lock);
120 list_del(&dev->dev_list);
121 spin_unlock(&atm_dev_lock);
130 void atm_dev_deregister(struct atm_dev *dev)
132 unsigned long warning_time;
134 atm_proc_dev_deregister(dev);
136 spin_lock(&atm_dev_lock);
137 list_del(&dev->dev_list);
138 spin_unlock(&atm_dev_lock);
140 warning_time = jiffies;
141 while (atomic_read(&dev->refcnt) != 1) {
143 if ((jiffies - warning_time) > 10 * HZ) {
144 printk(KERN_EMERG "atm_dev_deregister: waiting for "
145 "dev %d to become free. Usage count = %d\n",
146 dev->number, atomic_read(&dev->refcnt));
147 warning_time = jiffies;
154 void shutdown_atm_dev(struct atm_dev *dev)
156 if (atomic_read(&dev->refcnt) > 1) {
157 set_bit(ATM_DF_CLOSE, &dev->flags);
160 if (dev->ops->dev_close)
161 dev->ops->dev_close(dev);
162 atm_dev_deregister(dev);
166 static void copy_aal_stats(struct k_atm_aal_stats *from,
167 struct atm_aal_stats *to)
169 #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
175 static void subtract_aal_stats(struct k_atm_aal_stats *from,
176 struct atm_aal_stats *to)
178 #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
184 static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg, int zero)
186 struct atm_dev_stats tmp;
189 copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
190 copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
191 copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
193 error = copy_to_user(arg, &tmp, sizeof(tmp));
194 if (zero && !error) {
195 subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
196 subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
197 subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
199 return error ? -EFAULT : 0;
203 int atm_dev_ioctl(unsigned int cmd, void __user *arg)
206 int error, len, number, size = 0;
209 int *tmp_buf, *tmp_p;
210 struct atm_iobuf __user *iobuf = arg;
211 struct atmif_sioc __user *sioc = arg;
214 if (get_user(buf, &iobuf->buffer))
216 if (get_user(len, &iobuf->length))
218 spin_lock(&atm_dev_lock);
219 list_for_each(p, &atm_devs)
222 spin_unlock(&atm_dev_lock);
225 tmp_buf = kmalloc(size, GFP_ATOMIC);
227 spin_unlock(&atm_dev_lock);
231 list_for_each(p, &atm_devs) {
232 dev = list_entry(p, struct atm_dev, dev_list);
233 *tmp_p++ = dev->number;
235 spin_unlock(&atm_dev_lock);
236 error = ((copy_to_user(buf, tmp_buf, size)) ||
237 put_user(size, &iobuf->length))
245 if (get_user(buf, &sioc->arg))
247 if (get_user(len, &sioc->length))
249 if (get_user(number, &sioc->number))
252 if (!(dev = atm_dev_lookup(number)))
257 size = strlen(dev->type) + 1;
258 if (copy_to_user(buf, dev->type, size)) {
265 if (copy_to_user(buf, dev->esi, size)) {
274 for (i = 0; i < ESI_LEN; i++)
283 unsigned char esi[ESI_LEN];
285 if (!capable(CAP_NET_ADMIN)) {
289 if (copy_from_user(esi, buf, ESI_LEN)) {
293 memcpy(dev->esi, esi, ESI_LEN);
298 if (!capable(CAP_NET_ADMIN)) {
304 size = sizeof(struct atm_dev_stats);
305 error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
310 size = sizeof(struct atm_cirange);
311 if (copy_to_user(buf, &dev->ci_range, size)) {
316 case ATM_GETLINKRATE:
318 if (copy_to_user(buf, &dev->link_rate, size)) {
324 if (!capable(CAP_NET_ADMIN)) {
332 if (!capable(CAP_NET_ADMIN)) {
337 struct sockaddr_atmsvc addr;
339 if (copy_from_user(&addr, buf, sizeof(addr))) {
343 if (cmd == ATM_ADDADDR)
344 error = atm_add_addr(dev, &addr);
346 error = atm_del_addr(dev, &addr);
350 error = atm_get_addr(dev, buf, len);
354 /* may return 0, but later on size == 0 means "don't
356 error = put_user(size, &sioc->length)
360 if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
361 __ATM_LM_XTLOC((int) (unsigned long) buf) >
362 __ATM_LM_XTRMT((int) (unsigned long) buf)) {
371 case SONET_SETFRAMING:
372 if (!capable(CAP_NET_ADMIN)) {
378 if (!dev->ops->ioctl) {
382 size = dev->ops->ioctl(dev, cmd, buf);
384 error = (size == -ENOIOCTLCMD ? -EINVAL : size);
390 error = put_user(size, &sioc->length)
399 static __inline__ void *dev_get_idx(loff_t left)
403 list_for_each(p, &atm_devs) {
407 return (p != &atm_devs) ? p : NULL;
410 void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
412 spin_lock(&atm_dev_lock);
413 return *pos ? dev_get_idx(*pos) : (void *) 1;
416 void atm_dev_seq_stop(struct seq_file *seq, void *v)
418 spin_unlock(&atm_dev_lock);
421 void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
424 v = (v == (void *)1) ? atm_devs.next : ((struct list_head *)v)->next;
425 return (v == &atm_devs) ? NULL : v;
429 EXPORT_SYMBOL(atm_dev_register);
430 EXPORT_SYMBOL(atm_dev_deregister);
431 EXPORT_SYMBOL(atm_dev_lookup);
432 EXPORT_SYMBOL(shutdown_atm_dev);