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