cpufreq: mediatek: fix build error
[linux-2.6/btrfs-unstable.git] / drivers / pci / hotplug / pci_hotplug_core.c
blobd1fab97d6b01ff4cb2900ac1336ed0ae69852a39
1 /*
2 * PCI HotPlug Controller Core
4 * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2001-2002 IBM Corp.
7 * All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
17 * NON INFRINGEMENT. See the GNU General Public License for more
18 * details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Send feedback to <kristen.c.accardi@intel.com>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/list.h>
33 #include <linux/kobject.h>
34 #include <linux/sysfs.h>
35 #include <linux/pagemap.h>
36 #include <linux/init.h>
37 #include <linux/mount.h>
38 #include <linux/namei.h>
39 #include <linux/mutex.h>
40 #include <linux/pci.h>
41 #include <linux/pci_hotplug.h>
42 #include <asm/uaccess.h>
43 #include "../pci.h"
44 #include "cpci_hotplug.h"
46 #define MY_NAME "pci_hotplug"
48 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
49 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
50 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
51 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
54 /* local variables */
55 static bool debug;
57 #define DRIVER_VERSION "0.5"
58 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
59 #define DRIVER_DESC "PCI Hot Plug PCI Core"
62 static LIST_HEAD(pci_hotplug_slot_list);
63 static DEFINE_MUTEX(pci_hp_mutex);
65 /* Weee, fun with macros... */
66 #define GET_STATUS(name, type) \
67 static int get_##name(struct hotplug_slot *slot, type *value) \
68 { \
69 struct hotplug_slot_ops *ops = slot->ops; \
70 int retval = 0; \
71 if (!try_module_get(ops->owner)) \
72 return -ENODEV; \
73 if (ops->get_##name) \
74 retval = ops->get_##name(slot, value); \
75 else \
76 *value = slot->info->name; \
77 module_put(ops->owner); \
78 return retval; \
81 GET_STATUS(power_status, u8)
82 GET_STATUS(attention_status, u8)
83 GET_STATUS(latch_status, u8)
84 GET_STATUS(adapter_status, u8)
86 static ssize_t power_read_file(struct pci_slot *pci_slot, char *buf)
88 int retval;
89 u8 value;
91 retval = get_power_status(pci_slot->hotplug, &value);
92 if (retval)
93 return retval;
95 return sprintf(buf, "%d\n", value);
98 static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
99 size_t count)
101 struct hotplug_slot *slot = pci_slot->hotplug;
102 unsigned long lpower;
103 u8 power;
104 int retval = 0;
106 lpower = simple_strtoul(buf, NULL, 10);
107 power = (u8)(lpower & 0xff);
108 dbg("power = %d\n", power);
110 if (!try_module_get(slot->ops->owner)) {
111 retval = -ENODEV;
112 goto exit;
114 switch (power) {
115 case 0:
116 if (slot->ops->disable_slot)
117 retval = slot->ops->disable_slot(slot);
118 break;
120 case 1:
121 if (slot->ops->enable_slot)
122 retval = slot->ops->enable_slot(slot);
123 break;
125 default:
126 err("Illegal value specified for power\n");
127 retval = -EINVAL;
129 module_put(slot->ops->owner);
131 exit:
132 if (retval)
133 return retval;
134 return count;
137 static struct pci_slot_attribute hotplug_slot_attr_power = {
138 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
139 .show = power_read_file,
140 .store = power_write_file
143 static ssize_t attention_read_file(struct pci_slot *pci_slot, char *buf)
145 int retval;
146 u8 value;
148 retval = get_attention_status(pci_slot->hotplug, &value);
149 if (retval)
150 return retval;
152 return sprintf(buf, "%d\n", value);
155 static ssize_t attention_write_file(struct pci_slot *pci_slot, const char *buf,
156 size_t count)
158 struct hotplug_slot_ops *ops = pci_slot->hotplug->ops;
159 unsigned long lattention;
160 u8 attention;
161 int retval = 0;
163 lattention = simple_strtoul(buf, NULL, 10);
164 attention = (u8)(lattention & 0xff);
165 dbg(" - attention = %d\n", attention);
167 if (!try_module_get(ops->owner)) {
168 retval = -ENODEV;
169 goto exit;
171 if (ops->set_attention_status)
172 retval = ops->set_attention_status(pci_slot->hotplug, attention);
173 module_put(ops->owner);
175 exit:
176 if (retval)
177 return retval;
178 return count;
181 static struct pci_slot_attribute hotplug_slot_attr_attention = {
182 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
183 .show = attention_read_file,
184 .store = attention_write_file
187 static ssize_t latch_read_file(struct pci_slot *pci_slot, char *buf)
189 int retval;
190 u8 value;
192 retval = get_latch_status(pci_slot->hotplug, &value);
193 if (retval)
194 return retval;
196 return sprintf(buf, "%d\n", value);
199 static struct pci_slot_attribute hotplug_slot_attr_latch = {
200 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
201 .show = latch_read_file,
204 static ssize_t presence_read_file(struct pci_slot *pci_slot, char *buf)
206 int retval;
207 u8 value;
209 retval = get_adapter_status(pci_slot->hotplug, &value);
210 if (retval)
211 return retval;
213 return sprintf(buf, "%d\n", value);
216 static struct pci_slot_attribute hotplug_slot_attr_presence = {
217 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
218 .show = presence_read_file,
221 static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
222 size_t count)
224 struct hotplug_slot *slot = pci_slot->hotplug;
225 unsigned long ltest;
226 u32 test;
227 int retval = 0;
229 ltest = simple_strtoul (buf, NULL, 10);
230 test = (u32)(ltest & 0xffffffff);
231 dbg("test = %d\n", test);
233 if (!try_module_get(slot->ops->owner)) {
234 retval = -ENODEV;
235 goto exit;
237 if (slot->ops->hardware_test)
238 retval = slot->ops->hardware_test(slot, test);
239 module_put(slot->ops->owner);
241 exit:
242 if (retval)
243 return retval;
244 return count;
247 static struct pci_slot_attribute hotplug_slot_attr_test = {
248 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
249 .store = test_write_file
252 static bool has_power_file(struct pci_slot *pci_slot)
254 struct hotplug_slot *slot = pci_slot->hotplug;
256 if ((!slot) || (!slot->ops))
257 return false;
258 if ((slot->ops->enable_slot) ||
259 (slot->ops->disable_slot) ||
260 (slot->ops->get_power_status))
261 return true;
262 return false;
265 static bool has_attention_file(struct pci_slot *pci_slot)
267 struct hotplug_slot *slot = pci_slot->hotplug;
269 if ((!slot) || (!slot->ops))
270 return false;
271 if ((slot->ops->set_attention_status) ||
272 (slot->ops->get_attention_status))
273 return true;
274 return false;
277 static bool has_latch_file(struct pci_slot *pci_slot)
279 struct hotplug_slot *slot = pci_slot->hotplug;
281 if ((!slot) || (!slot->ops))
282 return false;
283 if (slot->ops->get_latch_status)
284 return true;
285 return false;
288 static bool has_adapter_file(struct pci_slot *pci_slot)
290 struct hotplug_slot *slot = pci_slot->hotplug;
292 if ((!slot) || (!slot->ops))
293 return false;
294 if (slot->ops->get_adapter_status)
295 return true;
296 return false;
299 static bool has_test_file(struct pci_slot *pci_slot)
301 struct hotplug_slot *slot = pci_slot->hotplug;
303 if ((!slot) || (!slot->ops))
304 return false;
305 if (slot->ops->hardware_test)
306 return true;
307 return false;
310 static int fs_add_slot(struct pci_slot *pci_slot)
312 int retval = 0;
314 /* Create symbolic link to the hotplug driver module */
315 pci_hp_create_module_link(pci_slot);
317 if (has_power_file(pci_slot)) {
318 retval = sysfs_create_file(&pci_slot->kobj,
319 &hotplug_slot_attr_power.attr);
320 if (retval)
321 goto exit_power;
324 if (has_attention_file(pci_slot)) {
325 retval = sysfs_create_file(&pci_slot->kobj,
326 &hotplug_slot_attr_attention.attr);
327 if (retval)
328 goto exit_attention;
331 if (has_latch_file(pci_slot)) {
332 retval = sysfs_create_file(&pci_slot->kobj,
333 &hotplug_slot_attr_latch.attr);
334 if (retval)
335 goto exit_latch;
338 if (has_adapter_file(pci_slot)) {
339 retval = sysfs_create_file(&pci_slot->kobj,
340 &hotplug_slot_attr_presence.attr);
341 if (retval)
342 goto exit_adapter;
345 if (has_test_file(pci_slot)) {
346 retval = sysfs_create_file(&pci_slot->kobj,
347 &hotplug_slot_attr_test.attr);
348 if (retval)
349 goto exit_test;
352 goto exit;
354 exit_test:
355 if (has_adapter_file(pci_slot))
356 sysfs_remove_file(&pci_slot->kobj,
357 &hotplug_slot_attr_presence.attr);
358 exit_adapter:
359 if (has_latch_file(pci_slot))
360 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
361 exit_latch:
362 if (has_attention_file(pci_slot))
363 sysfs_remove_file(&pci_slot->kobj,
364 &hotplug_slot_attr_attention.attr);
365 exit_attention:
366 if (has_power_file(pci_slot))
367 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
368 exit_power:
369 pci_hp_remove_module_link(pci_slot);
370 exit:
371 return retval;
374 static void fs_remove_slot(struct pci_slot *pci_slot)
376 if (has_power_file(pci_slot))
377 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
379 if (has_attention_file(pci_slot))
380 sysfs_remove_file(&pci_slot->kobj,
381 &hotplug_slot_attr_attention.attr);
383 if (has_latch_file(pci_slot))
384 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
386 if (has_adapter_file(pci_slot))
387 sysfs_remove_file(&pci_slot->kobj,
388 &hotplug_slot_attr_presence.attr);
390 if (has_test_file(pci_slot))
391 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_test.attr);
393 pci_hp_remove_module_link(pci_slot);
396 static struct hotplug_slot *get_slot_from_name(const char *name)
398 struct hotplug_slot *slot;
399 struct list_head *tmp;
401 list_for_each(tmp, &pci_hotplug_slot_list) {
402 slot = list_entry(tmp, struct hotplug_slot, slot_list);
403 if (strcmp(hotplug_slot_name(slot), name) == 0)
404 return slot;
406 return NULL;
410 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
411 * @bus: bus this slot is on
412 * @slot: pointer to the &struct hotplug_slot to register
413 * @devnr: device number
414 * @name: name registered with kobject core
415 * @owner: caller module owner
416 * @mod_name: caller module name
418 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
419 * userspace interaction to the slot.
421 * Returns 0 if successful, anything else for an error.
423 int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
424 int devnr, const char *name,
425 struct module *owner, const char *mod_name)
427 int result;
428 struct pci_slot *pci_slot;
430 if (slot == NULL)
431 return -ENODEV;
432 if ((slot->info == NULL) || (slot->ops == NULL))
433 return -EINVAL;
434 if (slot->release == NULL) {
435 dbg("Why are you trying to register a hotplug slot without a proper release function?\n");
436 return -EINVAL;
439 slot->ops->owner = owner;
440 slot->ops->mod_name = mod_name;
442 mutex_lock(&pci_hp_mutex);
444 * No problems if we call this interface from both ACPI_PCI_SLOT
445 * driver and call it here again. If we've already created the
446 * pci_slot, the interface will simply bump the refcount.
448 pci_slot = pci_create_slot(bus, devnr, name, slot);
449 if (IS_ERR(pci_slot)) {
450 result = PTR_ERR(pci_slot);
451 goto out;
454 slot->pci_slot = pci_slot;
455 pci_slot->hotplug = slot;
457 list_add(&slot->slot_list, &pci_hotplug_slot_list);
459 result = fs_add_slot(pci_slot);
460 kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
461 dbg("Added slot %s to the list\n", name);
462 out:
463 mutex_unlock(&pci_hp_mutex);
464 return result;
466 EXPORT_SYMBOL_GPL(__pci_hp_register);
469 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
470 * @slot: pointer to the &struct hotplug_slot to deregister
472 * The @slot must have been registered with the pci hotplug subsystem
473 * previously with a call to pci_hp_register().
475 * Returns 0 if successful, anything else for an error.
477 int pci_hp_deregister(struct hotplug_slot *slot)
479 struct hotplug_slot *temp;
480 struct pci_slot *pci_slot;
482 if (!slot)
483 return -ENODEV;
485 mutex_lock(&pci_hp_mutex);
486 temp = get_slot_from_name(hotplug_slot_name(slot));
487 if (temp != slot) {
488 mutex_unlock(&pci_hp_mutex);
489 return -ENODEV;
492 list_del(&slot->slot_list);
494 pci_slot = slot->pci_slot;
495 fs_remove_slot(pci_slot);
496 dbg("Removed slot %s from the list\n", hotplug_slot_name(slot));
498 slot->release(slot);
499 pci_slot->hotplug = NULL;
500 pci_destroy_slot(pci_slot);
501 mutex_unlock(&pci_hp_mutex);
503 return 0;
505 EXPORT_SYMBOL_GPL(pci_hp_deregister);
508 * pci_hp_change_slot_info - changes the slot's information structure in the core
509 * @slot: pointer to the slot whose info has changed
510 * @info: pointer to the info copy into the slot's info structure
512 * @slot must have been registered with the pci
513 * hotplug subsystem previously with a call to pci_hp_register().
515 * Returns 0 if successful, anything else for an error.
517 int pci_hp_change_slot_info(struct hotplug_slot *slot,
518 struct hotplug_slot_info *info)
520 if (!slot || !info)
521 return -ENODEV;
523 memcpy(slot->info, info, sizeof(struct hotplug_slot_info));
525 return 0;
527 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);
529 static int __init pci_hotplug_init(void)
531 int result;
533 result = cpci_hotplug_init(debug);
534 if (result) {
535 err("cpci_hotplug_init with error %d\n", result);
536 return result;
539 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
540 return result;
543 static void __exit pci_hotplug_exit(void)
545 cpci_hotplug_exit();
548 module_init(pci_hotplug_init);
549 module_exit(pci_hotplug_exit);
551 MODULE_AUTHOR(DRIVER_AUTHOR);
552 MODULE_DESCRIPTION(DRIVER_DESC);
553 MODULE_LICENSE("GPL");
554 module_param(debug, bool, 0644);
555 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");