atm_sysfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* ATM driver model support. */
  2. #include <linux/config.h>
  3. #include <linux/kernel.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 class_device *cdev, 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 class_device *cdev, char *buf)
  16. {
  17. char *pos = buf;
  18. struct atm_dev *adev = to_atm_dev(cdev);
  19. int i;
  20. for (i = 0; i < (ESI_LEN - 1); i++)
  21. pos += sprintf(pos, "%02x:", adev->esi[i]);
  22. pos += sprintf(pos, "%02x\n", adev->esi[i]);
  23. return pos - buf;
  24. }
  25. static ssize_t show_atmaddress(struct class_device *cdev, char *buf)
  26. {
  27. unsigned long flags;
  28. char *pos = buf;
  29. struct atm_dev *adev = to_atm_dev(cdev);
  30. struct atm_dev_addr *aaddr;
  31. int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
  32. int i, j;
  33. spin_lock_irqsave(&adev->lock, flags);
  34. list_for_each_entry(aaddr, &adev->local, entry) {
  35. for(i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
  36. if (j == *fmt) {
  37. pos += sprintf(pos, ".");
  38. ++fmt;
  39. j = 0;
  40. }
  41. pos += sprintf(pos, "%02x", aaddr->addr.sas_addr.prv[i]);
  42. }
  43. pos += sprintf(pos, "\n");
  44. }
  45. spin_unlock_irqrestore(&adev->lock, flags);
  46. return pos - buf;
  47. }
  48. static ssize_t show_carrier(struct class_device *cdev, char *buf)
  49. {
  50. char *pos = buf;
  51. struct atm_dev *adev = to_atm_dev(cdev);
  52. pos += sprintf(pos, "%d\n",
  53. adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
  54. return pos - buf;
  55. }
  56. static ssize_t show_link_rate(struct class_device *cdev, char *buf)
  57. {
  58. char *pos = buf;
  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. pos += sprintf(pos, "%d\n", link_rate);
  76. return pos - buf;
  77. }
  78. static CLASS_DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  79. static CLASS_DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL);
  80. static CLASS_DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL);
  81. static CLASS_DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  82. static CLASS_DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL);
  83. static struct class_device_attribute *atm_attrs[] = {
  84. &class_device_attr_atmaddress,
  85. &class_device_attr_address,
  86. &class_device_attr_carrier,
  87. &class_device_attr_type,
  88. &class_device_attr_link_rate,
  89. NULL
  90. };
  91. static int atm_uevent(struct class_device *cdev, char **envp, int num_envp, char *buf, int size)
  92. {
  93. struct atm_dev *adev;
  94. int i = 0, len = 0;
  95. if (!cdev)
  96. return -ENODEV;
  97. adev = to_atm_dev(cdev);
  98. if (!adev)
  99. return -ENODEV;
  100. if (add_uevent_var(envp, num_envp, &i, buf, size, &len,
  101. "NAME=%s%d", adev->type, adev->number))
  102. return -ENOMEM;
  103. envp[i] = NULL;
  104. return 0;
  105. }
  106. static void atm_release(struct class_device *cdev)
  107. {
  108. struct atm_dev *adev = to_atm_dev(cdev);
  109. kfree(adev);
  110. }
  111. static struct class atm_class = {
  112. .name = "atm",
  113. .release = atm_release,
  114. .uevent = atm_uevent,
  115. };
  116. int atm_register_sysfs(struct atm_dev *adev)
  117. {
  118. struct class_device *cdev = &adev->class_dev;
  119. int i, err;
  120. cdev->class = &atm_class;
  121. class_set_devdata(cdev, adev);
  122. snprintf(cdev->class_id, BUS_ID_SIZE, "%s%d", adev->type, adev->number);
  123. err = class_device_register(cdev);
  124. if (err < 0)
  125. return err;
  126. for (i = 0; atm_attrs[i]; i++)
  127. class_device_create_file(cdev, atm_attrs[i]);
  128. return 0;
  129. }
  130. void atm_unregister_sysfs(struct atm_dev *adev)
  131. {
  132. struct class_device *cdev = &adev->class_dev;
  133. class_device_del(cdev);
  134. }
  135. int __init atm_sysfs_init(void)
  136. {
  137. return class_register(&atm_class);
  138. }
  139. void __exit atm_sysfs_exit(void)
  140. {
  141. class_unregister(&atm_class);
  142. }