rio-sysfs.c 5.0 KB

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