atm_sysfs.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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", aaddr->addr.sas_addr.prv[i]);
  44. }
  45. pos += sprintf(pos, "\n");
  46. }
  47. spin_unlock_irqrestore(&adev->lock, flags);
  48. return pos - buf;
  49. }
  50. static ssize_t show_carrier(struct device *cdev,
  51. struct device_attribute *attr, char *buf)
  52. {
  53. char *pos = buf;
  54. struct atm_dev *adev = to_atm_dev(cdev);
  55. pos += sprintf(pos, "%d\n",
  56. adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
  57. return pos - buf;
  58. }
  59. static ssize_t show_link_rate(struct device *cdev,
  60. struct device_attribute *attr, char *buf)
  61. {
  62. char *pos = buf;
  63. struct atm_dev *adev = to_atm_dev(cdev);
  64. int link_rate;
  65. /* show the link rate, not the data rate */
  66. switch (adev->link_rate) {
  67. case ATM_OC3_PCR:
  68. link_rate = 155520000;
  69. break;
  70. case ATM_OC12_PCR:
  71. link_rate = 622080000;
  72. break;
  73. case ATM_25_PCR:
  74. link_rate = 25600000;
  75. break;
  76. default:
  77. link_rate = adev->link_rate * 8 * 53;
  78. }
  79. pos += sprintf(pos, "%d\n", link_rate);
  80. return pos - buf;
  81. }
  82. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  83. static DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL);
  84. static DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL);
  85. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  86. static DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL);
  87. static struct device_attribute *atm_attrs[] = {
  88. &dev_attr_atmaddress,
  89. &dev_attr_address,
  90. &dev_attr_carrier,
  91. &dev_attr_type,
  92. &dev_attr_link_rate,
  93. NULL
  94. };
  95. static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env)
  96. {
  97. struct atm_dev *adev;
  98. if (!cdev)
  99. return -ENODEV;
  100. adev = to_atm_dev(cdev);
  101. if (!adev)
  102. return -ENODEV;
  103. if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
  104. return -ENOMEM;
  105. return 0;
  106. }
  107. static void atm_release(struct device *cdev)
  108. {
  109. struct atm_dev *adev = to_atm_dev(cdev);
  110. kfree(adev);
  111. }
  112. static struct class atm_class = {
  113. .name = "atm",
  114. .dev_release = atm_release,
  115. .dev_uevent = atm_uevent,
  116. };
  117. int atm_register_sysfs(struct atm_dev *adev)
  118. {
  119. struct device *cdev = &adev->class_dev;
  120. int i, j, err;
  121. cdev->class = &atm_class;
  122. dev_set_drvdata(cdev, adev);
  123. dev_set_name(cdev, "%s%d", adev->type, adev->number);
  124. err = device_register(cdev);
  125. if (err < 0)
  126. return err;
  127. for (i = 0; atm_attrs[i]; i++) {
  128. err = device_create_file(cdev, atm_attrs[i]);
  129. if (err)
  130. goto err_out;
  131. }
  132. return 0;
  133. err_out:
  134. for (j = 0; j < i; j++)
  135. device_remove_file(cdev, atm_attrs[j]);
  136. device_del(cdev);
  137. return err;
  138. }
  139. void atm_unregister_sysfs(struct atm_dev *adev)
  140. {
  141. struct device *cdev = &adev->class_dev;
  142. device_del(cdev);
  143. }
  144. int __init atm_sysfs_init(void)
  145. {
  146. return class_register(&atm_class);
  147. }
  148. void __exit atm_sysfs_exit(void)
  149. {
  150. class_unregister(&atm_class);
  151. }