atm_sysfs.c 3.9 KB

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