cpqphp_sysfs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/mutex.h>
  37. #include <linux/debugfs.h>
  38. #include "cpqphp.h"
  39. static DEFINE_MUTEX(cpqphp_mutex);
  40. static int show_ctrl (struct controller *ctrl, char *buf)
  41. {
  42. char *out = buf;
  43. int index;
  44. struct pci_resource *res;
  45. out += sprintf(buf, "Free resources: memory\n");
  46. index = 11;
  47. res = ctrl->mem_head;
  48. while (res && index--) {
  49. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  50. res = res->next;
  51. }
  52. out += sprintf(out, "Free resources: prefetchable memory\n");
  53. index = 11;
  54. res = ctrl->p_mem_head;
  55. while (res && index--) {
  56. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  57. res = res->next;
  58. }
  59. out += sprintf(out, "Free resources: IO\n");
  60. index = 11;
  61. res = ctrl->io_head;
  62. while (res && index--) {
  63. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  64. res = res->next;
  65. }
  66. out += sprintf(out, "Free resources: bus numbers\n");
  67. index = 11;
  68. res = ctrl->bus_head;
  69. while (res && index--) {
  70. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  71. res = res->next;
  72. }
  73. return out - buf;
  74. }
  75. static int show_dev (struct controller *ctrl, char *buf)
  76. {
  77. char * out = buf;
  78. int index;
  79. struct pci_resource *res;
  80. struct pci_func *new_slot;
  81. struct slot *slot;
  82. slot = ctrl->slot;
  83. while (slot) {
  84. new_slot = cpqhp_slot_find(slot->bus, slot->device, 0);
  85. if (!new_slot)
  86. break;
  87. out += sprintf(out, "assigned resources: memory\n");
  88. index = 11;
  89. res = new_slot->mem_head;
  90. while (res && index--) {
  91. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  92. res = res->next;
  93. }
  94. out += sprintf(out, "assigned resources: prefetchable memory\n");
  95. index = 11;
  96. res = new_slot->p_mem_head;
  97. while (res && index--) {
  98. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  99. res = res->next;
  100. }
  101. out += sprintf(out, "assigned resources: IO\n");
  102. index = 11;
  103. res = new_slot->io_head;
  104. while (res && index--) {
  105. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  106. res = res->next;
  107. }
  108. out += sprintf(out, "assigned resources: bus numbers\n");
  109. index = 11;
  110. res = new_slot->bus_head;
  111. while (res && index--) {
  112. out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
  113. res = res->next;
  114. }
  115. slot=slot->next;
  116. }
  117. return out - buf;
  118. }
  119. static int spew_debug_info(struct controller *ctrl, char *data, int size)
  120. {
  121. int used;
  122. used = size - show_ctrl(ctrl, data);
  123. used = (size - used) - show_dev(ctrl, &data[used]);
  124. return used;
  125. }
  126. struct ctrl_dbg {
  127. int size;
  128. char *data;
  129. struct controller *ctrl;
  130. };
  131. #define MAX_OUTPUT (4*PAGE_SIZE)
  132. static int open(struct inode *inode, struct file *file)
  133. {
  134. struct controller *ctrl = inode->i_private;
  135. struct ctrl_dbg *dbg;
  136. int retval = -ENOMEM;
  137. mutex_lock(&cpqphp_mutex);
  138. dbg = kmalloc(sizeof(*dbg), GFP_KERNEL);
  139. if (!dbg)
  140. goto exit;
  141. dbg->data = kmalloc(MAX_OUTPUT, GFP_KERNEL);
  142. if (!dbg->data) {
  143. kfree(dbg);
  144. goto exit;
  145. }
  146. dbg->size = spew_debug_info(ctrl, dbg->data, MAX_OUTPUT);
  147. file->private_data = dbg;
  148. retval = 0;
  149. exit:
  150. mutex_unlock(&cpqphp_mutex);
  151. return retval;
  152. }
  153. static loff_t lseek(struct file *file, loff_t off, int whence)
  154. {
  155. struct ctrl_dbg *dbg;
  156. loff_t new = -1;
  157. mutex_lock(&cpqphp_mutex);
  158. dbg = file->private_data;
  159. switch (whence) {
  160. case 0:
  161. new = off;
  162. break;
  163. case 1:
  164. new = file->f_pos + off;
  165. break;
  166. }
  167. if (new < 0 || new > dbg->size) {
  168. mutex_unlock(&cpqphp_mutex);
  169. return -EINVAL;
  170. }
  171. mutex_unlock(&cpqphp_mutex);
  172. return (file->f_pos = new);
  173. }
  174. static ssize_t read(struct file *file, char __user *buf,
  175. size_t nbytes, loff_t *ppos)
  176. {
  177. struct ctrl_dbg *dbg = file->private_data;
  178. return simple_read_from_buffer(buf, nbytes, ppos, dbg->data, dbg->size);
  179. }
  180. static int release(struct inode *inode, struct file *file)
  181. {
  182. struct ctrl_dbg *dbg = file->private_data;
  183. kfree(dbg->data);
  184. kfree(dbg);
  185. return 0;
  186. }
  187. static const struct file_operations debug_ops = {
  188. .owner = THIS_MODULE,
  189. .open = open,
  190. .llseek = lseek,
  191. .read = read,
  192. .release = release,
  193. };
  194. static struct dentry *root;
  195. void cpqhp_initialize_debugfs(void)
  196. {
  197. if (!root)
  198. root = debugfs_create_dir("cpqhp", NULL);
  199. }
  200. void cpqhp_shutdown_debugfs(void)
  201. {
  202. debugfs_remove(root);
  203. }
  204. void cpqhp_create_debugfs_files(struct controller *ctrl)
  205. {
  206. ctrl->dentry = debugfs_create_file(dev_name(&ctrl->pci_dev->dev),
  207. S_IRUGO, root, ctrl, &debug_ops);
  208. }
  209. void cpqhp_remove_debugfs_files(struct controller *ctrl)
  210. {
  211. if (ctrl->dentry)
  212. debugfs_remove(ctrl->dentry);
  213. ctrl->dentry = NULL;
  214. }