atm_sysfs.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* ATM driver model support. */
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/kobject.h>
  5. #include <linux/atmdev.h>
  6. #include "common.h"
  7. #include "resources.h"
  8. #define to_atm_dev(cldev) container_of(cldev, struct atm_dev, class_dev)
  9. static ssize_t show_type(struct class_device *cdev, char *buf)
  10. {
  11. struct atm_dev *adev = to_atm_dev(cdev);
  12. return sprintf(buf, "%s\n", adev->type);
  13. }
  14. static ssize_t show_address(struct class_device *cdev, char *buf)
  15. {
  16. char *pos = buf;
  17. struct atm_dev *adev = to_atm_dev(cdev);
  18. int i;
  19. for (i = 0; i < (ESI_LEN - 1); i++)
  20. pos += sprintf(pos, "%02x:", adev->esi[i]);
  21. pos += sprintf(pos, "%02x\n", adev->esi[i]);
  22. return pos - buf;
  23. }
  24. static ssize_t show_atmaddress(struct class_device *cdev, char *buf)
  25. {
  26. unsigned long flags;
  27. char *pos = buf;
  28. struct atm_dev *adev = to_atm_dev(cdev);
  29. struct atm_dev_addr *aaddr;
  30. int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin;
  31. int i, j;
  32. spin_lock_irqsave(&adev->lock, flags);
  33. list_for_each_entry(aaddr, &adev->local, entry) {
  34. for(i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) {
  35. if (j == *fmt) {
  36. pos += sprintf(pos, ".");
  37. ++fmt;
  38. j = 0;
  39. }
  40. pos += sprintf(pos, "%02x", aaddr->addr.sas_addr.prv[i]);
  41. }
  42. pos += sprintf(pos, "\n");
  43. }
  44. spin_unlock_irqrestore(&adev->lock, flags);
  45. return pos - buf;
  46. }
  47. static ssize_t show_carrier(struct class_device *cdev, char *buf)
  48. {
  49. char *pos = buf;
  50. struct atm_dev *adev = to_atm_dev(cdev);
  51. pos += sprintf(pos, "%d\n",
  52. adev->signal == ATM_PHY_SIG_LOST ? 0 : 1);
  53. return pos - buf;
  54. }
  55. static ssize_t show_link_rate(struct class_device *cdev, char *buf)
  56. {
  57. char *pos = buf;
  58. struct atm_dev *adev = to_atm_dev(cdev);
  59. int link_rate;
  60. /* show the link rate, not the data rate */
  61. switch (adev->link_rate) {
  62. case ATM_OC3_PCR:
  63. link_rate = 155520000;
  64. break;
  65. case ATM_OC12_PCR:
  66. link_rate = 622080000;
  67. break;
  68. case ATM_25_PCR:
  69. link_rate = 25600000;
  70. break;
  71. default:
  72. link_rate = adev->link_rate * 8 * 53;
  73. }
  74. pos += sprintf(pos, "%d\n", link_rate);
  75. return pos - buf;
  76. }
  77. static CLASS_DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  78. static CLASS_DEVICE_ATTR(atmaddress, S_IRUGO, show_atmaddress, NULL);
  79. static CLASS_DEVICE_ATTR(carrier, S_IRUGO, show_carrier, NULL);
  80. static CLASS_DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  81. static CLASS_DEVICE_ATTR(link_rate, S_IRUGO, show_link_rate, NULL);
  82. static struct class_device_attribute *atm_attrs[] = {
  83. &class_device_attr_atmaddress,
  84. &class_device_attr_address,
  85. &class_device_attr_carrier,
  86. &class_device_attr_type,
  87. &class_device_attr_link_rate,
  88. NULL
  89. };
  90. static int atm_uevent(struct class_device *cdev, char **envp, int num_envp, char *buf, int size)
  91. {
  92. struct atm_dev *adev;
  93. int i = 0, len = 0;
  94. if (!cdev)
  95. return -ENODEV;
  96. adev = to_atm_dev(cdev);
  97. if (!adev)
  98. return -ENODEV;
  99. if (add_uevent_var(envp, num_envp, &i, buf, size, &len,
  100. "NAME=%s%d", adev->type, adev->number))
  101. return -ENOMEM;
  102. envp[i] = NULL;
  103. return 0;
  104. }
  105. static void atm_release(struct class_device *cdev)
  106. {
  107. struct atm_dev *adev = to_atm_dev(cdev);
  108. kfree(adev);
  109. }
  110. static struct class atm_class = {
  111. .name = "atm",
  112. .release = atm_release,
  113. .uevent = atm_uevent,
  114. };
  115. int atm_register_sysfs(struct atm_dev *adev)
  116. {
  117. struct class_device *cdev = &adev->class_dev;
  118. int i, j, err;
  119. cdev->class = &atm_class;
  120. class_set_devdata(cdev, adev);
  121. snprintf(cdev->class_id, BUS_ID_SIZE, "%s%d", adev->type, adev->number);
  122. err = class_device_register(cdev);
  123. if (err < 0)
  124. return err;
  125. for (i = 0; atm_attrs[i]; i++) {
  126. err = class_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. class_device_remove_file(cdev, atm_attrs[j]);
  134. class_device_del(cdev);
  135. return err;
  136. }
  137. void atm_unregister_sysfs(struct atm_dev *adev)
  138. {
  139. struct class_device *cdev = &adev->class_dev;
  140. class_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. }