rio-sysfs.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * RapidIO sysfs attributes and support
  3. *
  4. * Copyright 2005 MontaVista Software, Inc.
  5. * Matt Porter <mporter@kernel.crashing.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/kernel.h>
  14. #include <linux/rio.h>
  15. #include <linux/rio_drv.h>
  16. #include <linux/stat.h>
  17. #include <linux/sched.h> /* for capable() */
  18. #include "rio.h"
  19. /* Sysfs support */
  20. #define rio_config_attr(field, format_string) \
  21. static ssize_t \
  22. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  23. { \
  24. struct rio_dev *rdev = to_rio_dev(dev); \
  25. \
  26. return sprintf(buf, format_string, rdev->field); \
  27. } \
  28. rio_config_attr(did, "0x%04x\n");
  29. rio_config_attr(vid, "0x%04x\n");
  30. rio_config_attr(device_rev, "0x%08x\n");
  31. rio_config_attr(asm_did, "0x%04x\n");
  32. rio_config_attr(asm_vid, "0x%04x\n");
  33. rio_config_attr(asm_rev, "0x%04x\n");
  34. static ssize_t routes_show(struct device *dev, struct device_attribute *attr, char *buf)
  35. {
  36. struct rio_dev *rdev = to_rio_dev(dev);
  37. char *str = buf;
  38. int i;
  39. if (!rdev->rswitch)
  40. goto out;
  41. for (i = 0; i < RIO_MAX_ROUTE_ENTRIES; i++) {
  42. if (rdev->rswitch->route_table[i] == RIO_INVALID_ROUTE)
  43. continue;
  44. str +=
  45. sprintf(str, "%04x %02x\n", i,
  46. rdev->rswitch->route_table[i]);
  47. }
  48. out:
  49. return (str - buf);
  50. }
  51. struct device_attribute rio_dev_attrs[] = {
  52. __ATTR_RO(did),
  53. __ATTR_RO(vid),
  54. __ATTR_RO(device_rev),
  55. __ATTR_RO(asm_did),
  56. __ATTR_RO(asm_vid),
  57. __ATTR_RO(asm_rev),
  58. __ATTR_RO(routes),
  59. __ATTR_NULL,
  60. };
  61. static ssize_t
  62. rio_read_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
  63. {
  64. struct rio_dev *dev =
  65. to_rio_dev(container_of(kobj, struct device, kobj));
  66. unsigned int size = 0x100;
  67. loff_t init_off = off;
  68. u8 *data = (u8 *) buf;
  69. /* Several chips lock up trying to read undefined config space */
  70. if (capable(CAP_SYS_ADMIN))
  71. size = 0x200000;
  72. if (off > size)
  73. return 0;
  74. if (off + count > size) {
  75. size -= off;
  76. count = size;
  77. } else {
  78. size = count;
  79. }
  80. if ((off & 1) && size) {
  81. u8 val;
  82. rio_read_config_8(dev, off, &val);
  83. data[off - init_off] = val;
  84. off++;
  85. size--;
  86. }
  87. if ((off & 3) && size > 2) {
  88. u16 val;
  89. rio_read_config_16(dev, off, &val);
  90. data[off - init_off] = (val >> 8) & 0xff;
  91. data[off - init_off + 1] = val & 0xff;
  92. off += 2;
  93. size -= 2;
  94. }
  95. while (size > 3) {
  96. u32 val;
  97. rio_read_config_32(dev, off, &val);
  98. data[off - init_off] = (val >> 24) & 0xff;
  99. data[off - init_off + 1] = (val >> 16) & 0xff;
  100. data[off - init_off + 2] = (val >> 8) & 0xff;
  101. data[off - init_off + 3] = val & 0xff;
  102. off += 4;
  103. size -= 4;
  104. }
  105. if (size >= 2) {
  106. u16 val;
  107. rio_read_config_16(dev, off, &val);
  108. data[off - init_off] = (val >> 8) & 0xff;
  109. data[off - init_off + 1] = val & 0xff;
  110. off += 2;
  111. size -= 2;
  112. }
  113. if (size > 0) {
  114. u8 val;
  115. rio_read_config_8(dev, off, &val);
  116. data[off - init_off] = val;
  117. off++;
  118. --size;
  119. }
  120. return count;
  121. }
  122. static ssize_t
  123. rio_write_config(struct kobject *kobj, char *buf, loff_t off, size_t count)
  124. {
  125. struct rio_dev *dev =
  126. to_rio_dev(container_of(kobj, struct device, kobj));
  127. unsigned int size = count;
  128. loff_t init_off = off;
  129. u8 *data = (u8 *) buf;
  130. if (off > 0x200000)
  131. return 0;
  132. if (off + count > 0x200000) {
  133. size = 0x200000 - off;
  134. count = size;
  135. }
  136. if ((off & 1) && size) {
  137. rio_write_config_8(dev, off, data[off - init_off]);
  138. off++;
  139. size--;
  140. }
  141. if ((off & 3) && (size > 2)) {
  142. u16 val = data[off - init_off + 1];
  143. val |= (u16) data[off - init_off] << 8;
  144. rio_write_config_16(dev, off, val);
  145. off += 2;
  146. size -= 2;
  147. }
  148. while (size > 3) {
  149. u32 val = data[off - init_off + 3];
  150. val |= (u32) data[off - init_off + 2] << 8;
  151. val |= (u32) data[off - init_off + 1] << 16;
  152. val |= (u32) data[off - init_off] << 24;
  153. rio_write_config_32(dev, off, val);
  154. off += 4;
  155. size -= 4;
  156. }
  157. if (size >= 2) {
  158. u16 val = data[off - init_off + 1];
  159. val |= (u16) data[off - init_off] << 8;
  160. rio_write_config_16(dev, off, val);
  161. off += 2;
  162. size -= 2;
  163. }
  164. if (size) {
  165. rio_write_config_8(dev, off, data[off - init_off]);
  166. off++;
  167. --size;
  168. }
  169. return count;
  170. }
  171. static struct bin_attribute rio_config_attr = {
  172. .attr = {
  173. .name = "config",
  174. .mode = S_IRUGO | S_IWUSR,
  175. .owner = THIS_MODULE,
  176. },
  177. .size = 0x200000,
  178. .read = rio_read_config,
  179. .write = rio_write_config,
  180. };
  181. /**
  182. * rio_create_sysfs_dev_files - create RIO specific sysfs files
  183. * @rdev: device whose entries should be created
  184. *
  185. * Create files when @rdev is added to sysfs.
  186. */
  187. int rio_create_sysfs_dev_files(struct rio_dev *rdev)
  188. {
  189. sysfs_create_bin_file(&rdev->dev.kobj, &rio_config_attr);
  190. return 0;
  191. }
  192. /**
  193. * rio_remove_sysfs_dev_files - cleanup RIO specific sysfs files
  194. * @rdev: device whose entries we should free
  195. *
  196. * Cleanup when @rdev is removed from sysfs.
  197. */
  198. void rio_remove_sysfs_dev_files(struct rio_dev *rdev)
  199. {
  200. sysfs_remove_bin_file(&rdev->dev.kobj, &rio_config_attr);
  201. }