adummy.c 4.1 KB

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