cpqphp_sysfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Compaq Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Send feedback to <greg@kroah.com>
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/types.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/pci.h>
  35. #include <linux/pci_hotplug.h>
  36. #include <linux/smp_lock.h>
  37. #include <linux/debugfs.h>
  38. #include "cpqphp.h"
  39. static int show_ctrl (struct controller *ctrl, char *buf)
  40. {
  41. char *out = buf;
  42. int index;
  43. struct pci_resource *res;
  44. out += sprintf(buf, "Free resources: memory\n");
  45. index = 11;
  46. res = ctrl->mem_head;
  47. while (res && index--) {
  48. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  49. res = res->next;
  50. }
  51. out += sprintf(out, "Free resources: prefetchable memory\n");
  52. index = 11;
  53. res = ctrl->p_mem_head;
  54. while (res && index--) {
  55. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  56. res = res->next;
  57. }
  58. out += sprintf(out, "Free resources: IO\n");
  59. index = 11;
  60. res = ctrl->io_head;
  61. while (res && index--) {
  62. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  63. res = res->next;
  64. }
  65. out += sprintf(out, "Free resources: bus numbers\n");
  66. index = 11;
  67. res = ctrl->bus_head;
  68. while (res && index--) {
  69. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  70. res = res->next;
  71. }
  72. return out - buf;
  73. }
  74. static int show_dev (struct controller *ctrl, char *buf)
  75. {
  76. char * out = buf;
  77. int index;
  78. struct pci_resource *res;
  79. struct pci_func *new_slot;
  80. struct slot *slot;
  81. slot = ctrl->slot;
  82. while (slot) {
  83. new_slot = cpqhp_slot_find(slot->bus, slot->device, 0);
  84. if (!new_slot)
  85. break;
  86. out += sprintf(out, "assigned resources: memory\n");
  87. index = 11;
  88. res = new_slot->mem_head;
  89. while (res && index--) {
  90. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  91. res = res->next;
  92. }
  93. out += sprintf(out, "assigned resources: prefetchable memory\n");
  94. index = 11;
  95. res = new_slot->p_mem_head;
  96. while (res && index--) {
  97. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  98. res = res->next;
  99. }
  100. out += sprintf(out, "assigned resources: IO\n");
  101. index = 11;
  102. res = new_slot->io_head;
  103. while (res && index--) {
  104. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  105. res = res->next;
  106. }
  107. out += sprintf(out, "assigned resources: bus numbers\n");
  108. index = 11;
  109. res = new_slot->bus_head;
  110. while (res && index--) {
  111. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  112. res = res->next;
  113. }
  114. slot=slot->next;
  115. }
  116. return out - buf;
  117. }
  118. static int spew_debug_info(struct controller *ctrl, char *data, int size)
  119. {
  120. int used;
  121. used = size - show_ctrl(ctrl, data);
  122. used = (size - used) - show_dev(ctrl, &data[used]);
  123. return used;
  124. }
  125. struct ctrl_dbg {
  126. int size;
  127. char *data;
  128. struct controller *ctrl;
  129. };
  130. #define MAX_OUTPUT (4*PAGE_SIZE)
  131. static int open(struct inode *inode, struct file *file)
  132. {
  133. struct controller *ctrl = inode->i_private;
  134. struct ctrl_dbg *dbg;
  135. int retval = -ENOMEM;
  136. lock_kernel();
  137. dbg = kmalloc(sizeof(*dbg), GFP_KERNEL);
  138. if (!dbg)
  139. goto exit;
  140. dbg->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
  141. if (!dbg->data) {
  142. kfree(dbg);
  143. goto exit;
  144. }
  145. dbg->size = spew_debug_info(ctrl, dbg->data, MAX_OUTPUT);
  146. file->private_data = dbg;
  147. retval = 0;
  148. exit:
  149. unlock_kernel();
  150. return retval;
  151. }
  152. static loff_t lseek(struct file *file, loff_t off, int whence)
  153. {
  154. struct ctrl_dbg *dbg;
  155. loff_t new = -1;
  156. lock_kernel();
  157. dbg = file->private_data;
  158. switch (whence) {
  159. case 0:
  160. new = off;
  161. break;
  162. case 1:
  163. new = file->f_pos + off;
  164. break;
  165. }
  166. if (new < 0 || new > dbg->size) {
  167. unlock_kernel();
  168. return -EINVAL;
  169. }
  170. unlock_kernel();
  171. return (file->f_pos = new);
  172. }
  173. static ssize_t read(struct file *file, char __user *buf,
  174. size_t nbytes, loff_t *ppos)
  175. {
  176. struct ctrl_dbg *dbg = file->private_data;
  177. return simple_read_from_buffer(buf, nbytes, ppos, dbg->data, dbg->size);
  178. }
  179. static int release(struct inode *inode, struct file *file)
  180. {
  181. struct ctrl_dbg *dbg = file->private_data;
  182. kfree(dbg->data);
  183. kfree(dbg);
  184. return 0;
  185. }
  186. static const struct file_operations debug_ops = {
  187. .owner = THIS_MODULE,
  188. .open = open,
  189. .llseek = lseek,
  190. .read = read,
  191. .release = release,
  192. };
  193. static struct dentry *root;
  194. void cpqhp_initialize_debugfs(void)
  195. {
  196. if (!root)
  197. root = debugfs_create_dir("cpqhp", NULL);
  198. }
  199. void cpqhp_shutdown_debugfs(void)
  200. {
  201. debugfs_remove(root);
  202. }
  203. void cpqhp_create_debugfs_files(struct controller *ctrl)
  204. {
  205. ctrl->dentry = debugfs_create_file(dev_name(&ctrl->pci_dev->dev),
  206. S_IRUGO, root, ctrl, &debug_ops);
  207. }
  208. void cpqhp_remove_debugfs_files(struct controller *ctrl)
  209. {
  210. if (ctrl->dentry)
  211. debugfs_remove(ctrl->dentry);
  212. ctrl->dentry = NULL;
  213. }