atm_sysfs.c 4.1 KB

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