atm_sysfs.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 scnprintf(buf, PAGE_SIZE, "%s\n", adev->type);
  15. }
  16. static ssize_t show_address(struct device *cdev,
  17. struct device_attribute *attr, char *buf)
  18. {
  19. struct atm_dev *adev = to_atm_dev(cdev);
  20. return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi);
  21. }
  22. static ssize_t show_atmaddress(struct device *cdev,
  23. struct device_attribute *attr, char *buf)
  24. {
  25. unsigned long flags;
  26. struct atm_dev *adev = to_atm_dev(cdev);
  27. struct atm_dev_addr *aaddr;
  28. int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
  29. int i, j, count = 0;
  30. spin_lock_irqsave(&adev->lock, flags);
  31. list_for_each_entry(aaddr, &adev->local, entry) {
  32. for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
  33. if (j == *fmt) {
  34. count += scnprintf(buf + count,
  35. PAGE_SIZE - count, ".");
  36. ++fmt;
  37. j = 0;
  38. }
  39. count += scnprintf(buf + count,
  40. PAGE_SIZE - count, "%02x",
  41. aaddr->addr.sas_addr.prv[i]);
  42. }
  43. count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
  44. }
  45. spin_unlock_irqrestore(&adev->lock, flags);
  46. return count;
  47. }
  48. static ssize_t show_atmindex(struct device *cdev,
  49. struct device_attribute *attr, char *buf)
  50. {
  51. struct atm_dev *adev = to_atm_dev(cdev);
  52. return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number);
  53. }
  54. static ssize_t show_carrier(struct device *cdev,
  55. struct device_attribute *attr, char *buf)
  56. {
  57. struct atm_dev *adev = to_atm_dev(cdev);
  58. return scnprintf(buf, PAGE_SIZE, "%d\n",
  59. adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
  60. }
  61. static ssize_t show_link_rate(struct device *cdev,
  62. struct device_attribute *attr, char *buf)
  63. {
  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. return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate);
  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(atmindex, S_IRUGO, show_atmindex, 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_atmindex,
  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, struct device *parent)
  120. {
  121. struct device *cdev = &adev->class_dev;
  122. int i, j, err;
  123. cdev->class = &atm_class;
  124. cdev->parent = parent;
  125. dev_set_drvdata(cdev, adev);
  126. dev_set_name(cdev, "%s%d", adev->type, adev->number);
  127. err = device_register(cdev);
  128. if (err < 0)
  129. return err;
  130. for (i = 0; atm_attrs[i]; i++) {
  131. err = device_create_file(cdev, atm_attrs[i]);
  132. if (err)
  133. goto err_out;
  134. }
  135. return 0;
  136. err_out:
  137. for (j = 0; j < i; j++)
  138. device_remove_file(cdev, atm_attrs[j]);
  139. device_del(cdev);
  140. return err;
  141. }
  142. void atm_unregister_sysfs(struct atm_dev *adev)
  143. {
  144. struct device *cdev = &adev->class_dev;
  145. device_del(cdev);
  146. }
  147. int __init atm_sysfs_init(void)
  148. {
  149. return class_register(&atm_class);
  150. }
  151. void __exit atm_sysfs_exit(void)
  152. {
  153. class_unregister(&atm_class);
  154. }