atm_sysfs.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* ATM driver model support. */
  2. #include <linux/kernel.h>
  3. #include <linux/slab.h>
  4. #include <linux/init.h>
  5. #include <linux/kobject.h>
  6. #include <linux/atmdev.h>
  7. #include "common.h"
  8. #include "resources.h"
  9. #define to_atm_dev(cldev) container_of(cldev, struct atm_dev, class_dev)
  10. static ssize_t show_type(struct device *cdev,
  11. struct device_attribute *attr, char *buf)
  12. {
  13. struct atm_dev *adev = to_atm_dev(cdev);
  14. return sprintf(buf, "%s\n", adev->type);
  15. }
  16. static ssize_t show_address(struct device *cdev,
  17. struct device_attribute *attr, char *buf)
  18. {
  19. char *pos = buf;
  20. struct atm_dev *adev = to_atm_dev(cdev);
  21. int i;
  22. for (i = 0; i < (ESI_LEN - 1); i++)
  23. pos += sprintf(pos, "%02x:", adev->esi[i]);
  24. pos += sprintf(pos, "%02x\n", adev->esi[i]);
  25. return pos - buf;
  26. }
  27. static ssize_t show_atmaddress(struct device *cdev,
  28. struct device_attribute *attr, char *buf)
  29. {
  30. unsigned long flags;
  31. char *pos = buf;
  32. struct atm_dev *adev = to_atm_dev(cdev);
  33. struct atm_dev_addr *aaddr;
  34. int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
  35. int i, j;
  36. spin_lock_irqsave(&adev->lock, flags);
  37. list_for_each_entry(aaddr, &adev->local, entry) {
  38. for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
  39. if (j == *fmt) {
  40. pos += sprintf(pos, ".");
  41. ++fmt;
  42. j = 0;
  43. }
  44. pos += sprintf(pos, "%02x",
  45. aaddr->addr.sas_addr.prv[i]);
  46. }
  47. pos += sprintf(pos, "\n");
  48. }
  49. spin_unlock_irqrestore(&adev->lock, flags);
  50. return pos - buf;
  51. }
  52. static ssize_t show_carrier(struct device *cdev,
  53. struct device_attribute *attr, char *buf)
  54. {
  55. char *pos = buf;
  56. struct atm_dev *adev = to_atm_dev(cdev);
  57. pos += sprintf(pos, "%d\n",
  58. adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
  59. return pos - buf;
  60. }
  61. static ssize_t show_link_rate(struct device *cdev,
  62. struct device_attribute *attr, char *buf)
  63. {
  64. char *pos = buf;
  65. struct atm_dev *adev = to_atm_dev(cdev);
  66. int link_rate;
  67. /* show the link rate, not the data rate */
  68. switch (adev->link_rate) {
  69. case ATM_OC3_PCR:
  70. link_rate = 155520000;
  71. break;
  72. case ATM_OC12_PCR:
  73. link_rate = 622080000;
  74. break;
  75. case ATM_25_PCR:
  76. link_rate = 25600000;
  77. break;
  78. default:
  79. link_rate = adev->link_rate * 8 * 53;
  80. }
  81. pos += sprintf(pos, "%d\n", link_rate);
  82. return pos - buf;
  83. }
  84. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  85. static DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL);
  86. static DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL);
  87. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  88. static DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL);
  89. static struct device_attribute *atm_attrs[] = {
  90. &dev_attr_atmaddress,
  91. &dev_attr_address,
  92. &dev_attr_carrier,
  93. &dev_attr_type,
  94. &dev_attr_link_rate,
  95. NULL
  96. };
  97. static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env)
  98. {
  99. struct atm_dev *adev;
  100. if (!cdev)
  101. return -ENODEV;
  102. adev = to_atm_dev(cdev);
  103. if (!adev)
  104. return -ENODEV;
  105. if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
  106. return -ENOMEM;
  107. return 0;
  108. }
  109. static void atm_release(struct device *cdev)
  110. {
  111. struct atm_dev *adev = to_atm_dev(cdev);
  112. kfree(adev);
  113. }
  114. static struct class atm_class = {
  115. .name = "atm",
  116. .dev_release = atm_release,
  117. .dev_uevent = atm_uevent,
  118. };
  119. int atm_register_sysfs(struct atm_dev *adev)
  120. {
  121. struct device *cdev = &adev->class_dev;
  122. int i, j, err;
  123. cdev->class = &atm_class;
  124. dev_set_drvdata(cdev, adev);
  125. dev_set_name(cdev, "%s%d", adev->type, adev->number);
  126. err = device_register(cdev);
  127. if (err < 0)
  128. return err;
  129. for (i = 0; atm_attrs[i]; i++) {
  130. err = device_create_file(cdev, atm_attrs[i]);
  131. if (err)
  132. goto err_out;
  133. }
  134. return 0;
  135. err_out:
  136. for (j = 0; j < i; j++)
  137. device_remove_file(cdev, atm_attrs[j]);
  138. device_del(cdev);
  139. return err;
  140. }
  141. void atm_unregister_sysfs(struct atm_dev *adev)
  142. {
  143. struct device *cdev = &adev->class_dev;
  144. device_del(cdev);
  145. }
  146. int __init atm_sysfs_init(void)
  147. {
  148. return class_register(&atm_class);
  149. }
  150. void __exit atm_sysfs_exit(void)
  151. {
  152. class_unregister(&atm_class);
  153. }