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