adummy.c 4.0 KB

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