atm_sysfs.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
  30. int i, j, count = 0;
  31. spin_lock_irqsave(&adev->lock, flags);
  32. list_for_each_entry(aaddr, &adev->local, entry) {
  33. for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
  34. if (j == *fmt) {
  35. count += scnprintf(buf + count,
  36. PAGE_SIZE - count, ".");
  37. ++fmt;
  38. j = 0;
  39. }
  40. count += scnprintf(buf + count,
  41. PAGE_SIZE - count, "%02x",
  42. aaddr->addr.sas_addr.prv[i]);
  43. }
  44. count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
  45. }
  46. spin_unlock_irqrestore(&adev->lock, flags);
  47. return count;
  48. }
  49. static ssize_t show_atmindex(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", adev->number);
  54. }
  55. static ssize_t show_carrier(struct device *cdev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. struct atm_dev *adev = to_atm_dev(cdev);
  59. return scnprintf(buf, PAGE_SIZE, "%d\n",
  60. adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
  61. }
  62. static ssize_t show_link_rate(struct device *cdev,
  63. struct device_attribute *attr, char *buf)
  64. {
  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. return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate);
  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(atmindex, S_IRUGO, show_atmindex, 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_atmindex,
  93. &dev_attr_carrier,
  94. &dev_attr_type,
  95. &dev_attr_link_rate,
  96. NULL
  97. };
  98. static int atm_uevent(struct device *cdev, struct kobj_uevent_env *env)
  99. {
  100. struct atm_dev *adev;
  101. if (!cdev)
  102. return -ENODEV;
  103. adev = to_atm_dev(cdev);
  104. if (!adev)
  105. return -ENODEV;
  106. if (add_uevent_var(env, "NAME=%s%d", adev->type, adev->number))
  107. return -ENOMEM;
  108. return 0;
  109. }
  110. static void atm_release(struct device *cdev)
  111. {
  112. struct atm_dev *adev = to_atm_dev(cdev);
  113. kfree(adev);
  114. }
  115. static struct class atm_class = {
  116. .name = "atm",
  117. .dev_release = atm_release,
  118. .dev_uevent = atm_uevent,
  119. };
  120. int atm_register_sysfs(struct atm_dev *adev, struct device *parent)
  121. {
  122. struct device *cdev = &adev->class_dev;
  123. int i, j, err;
  124. cdev->class = &atm_class;
  125. cdev->parent = parent;
  126. dev_set_drvdata(cdev, adev);
  127. dev_set_name(cdev, "%s%d", adev->type, adev->number);
  128. err = device_register(cdev);
  129. if (err < 0)
  130. return err;
  131. for (i = 0; atm_attrs[i]; i++) {
  132. err = device_create_file(cdev, atm_attrs[i]);
  133. if (err)
  134. goto err_out;
  135. }
  136. return 0;
  137. err_out:
  138. for (j = 0; j < i; j++)
  139. device_remove_file(cdev, atm_attrs[j]);
  140. device_del(cdev);
  141. return err;
  142. }
  143. void atm_unregister_sysfs(struct atm_dev *adev)
  144. {
  145. struct device *cdev = &adev->class_dev;
  146. device_del(cdev);
  147. }
  148. int __init atm_sysfs_init(void)
  149. {
  150. return class_register(&atm_class);
  151. }
  152. void __exit atm_sysfs_exit(void)
  153. {
  154. class_unregister(&atm_class);
  155. }