atm_sysfs.c 4.2 KB

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