kbuild: add distclean info to 'make help' and more details for 'clean'
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pci / hotplug / pci_hotplug_core.c
blobb7b378df89e3ffeb2a3fda1debb70558a02b401d
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 <greg@kroah.com>
26 * Filesystem portion based on work done by Pat Mochel on ddfs/driverfs
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/list.h>
35 #include <linux/pagemap.h>
36 #include <linux/slab.h>
37 #include <linux/smp_lock.h>
38 #include <linux/init.h>
39 #include <linux/mount.h>
40 #include <linux/namei.h>
41 #include <linux/pci.h>
42 #include <asm/uaccess.h>
43 #include <linux/kobject.h>
44 #include <linux/sysfs.h>
45 #include "pci_hotplug.h"
48 #define MY_NAME "pci_hotplug"
50 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __FUNCTION__ , ## arg); } while (0)
51 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
52 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
53 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
56 /* local variables */
57 static int debug;
59 #define DRIVER_VERSION "0.5"
60 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
61 #define DRIVER_DESC "PCI Hot Plug PCI Core"
64 //////////////////////////////////////////////////////////////////
66 static LIST_HEAD(pci_hotplug_slot_list);
68 struct subsystem pci_hotplug_slots_subsys;
70 static ssize_t hotplug_slot_attr_show(struct kobject *kobj,
71 struct attribute *attr, char *buf)
73 struct hotplug_slot *slot = to_hotplug_slot(kobj);
74 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
75 return attribute->show ? attribute->show(slot, buf) : -EIO;
78 static ssize_t hotplug_slot_attr_store(struct kobject *kobj,
79 struct attribute *attr, const char *buf, size_t len)
81 struct hotplug_slot *slot = to_hotplug_slot(kobj);
82 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
83 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
86 static struct sysfs_ops hotplug_slot_sysfs_ops = {
87 .show = hotplug_slot_attr_show,
88 .store = hotplug_slot_attr_store,
91 static void hotplug_slot_release(struct kobject *kobj)
93 struct hotplug_slot *slot = to_hotplug_slot(kobj);
94 if (slot->release)
95 slot->release(slot);
98 static struct kobj_type hotplug_slot_ktype = {
99 .sysfs_ops = &hotplug_slot_sysfs_ops,
100 .release = &hotplug_slot_release,
103 decl_subsys_name(pci_hotplug_slots, slots, &hotplug_slot_ktype, NULL);
105 /* these strings match up with the values in pci_bus_speed */
106 static char *pci_bus_speed_strings[] = {
107 "33 MHz PCI", /* 0x00 */
108 "66 MHz PCI", /* 0x01 */
109 "66 MHz PCIX", /* 0x02 */
110 "100 MHz PCIX", /* 0x03 */
111 "133 MHz PCIX", /* 0x04 */
112 NULL, /* 0x05 */
113 NULL, /* 0x06 */
114 NULL, /* 0x07 */
115 NULL, /* 0x08 */
116 "66 MHz PCIX 266", /* 0x09 */
117 "100 MHz PCIX 266", /* 0x0a */
118 "133 MHz PCIX 266", /* 0x0b */
119 NULL, /* 0x0c */
120 NULL, /* 0x0d */
121 NULL, /* 0x0e */
122 NULL, /* 0x0f */
123 NULL, /* 0x10 */
124 "66 MHz PCIX 533", /* 0x11 */
125 "100 MHz PCIX 533", /* 0x12 */
126 "133 MHz PCIX 533", /* 0x13 */
127 "25 GBps PCI-E", /* 0x14 */
130 #ifdef CONFIG_HOTPLUG_PCI_CPCI
131 extern int cpci_hotplug_init(int debug);
132 extern void cpci_hotplug_exit(void);
133 #else
134 static inline int cpci_hotplug_init(int debug) { return 0; }
135 static inline void cpci_hotplug_exit(void) { }
136 #endif
138 /* Weee, fun with macros... */
139 #define GET_STATUS(name,type) \
140 static int get_##name (struct hotplug_slot *slot, type *value) \
142 struct hotplug_slot_ops *ops = slot->ops; \
143 int retval = 0; \
144 if (try_module_get(ops->owner)) { \
145 if (ops->get_##name) \
146 retval = ops->get_##name (slot, value); \
147 else \
148 *value = slot->info->name; \
149 module_put(ops->owner); \
151 return retval; \
154 GET_STATUS(power_status, u8)
155 GET_STATUS(attention_status, u8)
156 GET_STATUS(latch_status, u8)
157 GET_STATUS(adapter_status, u8)
158 GET_STATUS(address, u32)
159 GET_STATUS(max_bus_speed, enum pci_bus_speed)
160 GET_STATUS(cur_bus_speed, enum pci_bus_speed)
162 static ssize_t power_read_file (struct hotplug_slot *slot, char *buf)
164 int retval;
165 u8 value;
167 retval = get_power_status (slot, &value);
168 if (retval)
169 goto exit;
170 retval = sprintf (buf, "%d\n", value);
171 exit:
172 return retval;
175 static ssize_t power_write_file (struct hotplug_slot *slot, const char *buf,
176 size_t count)
178 unsigned long lpower;
179 u8 power;
180 int retval = 0;
182 lpower = simple_strtoul (buf, NULL, 10);
183 power = (u8)(lpower & 0xff);
184 dbg ("power = %d\n", power);
186 if (!try_module_get(slot->ops->owner)) {
187 retval = -ENODEV;
188 goto exit;
190 switch (power) {
191 case 0:
192 if (slot->ops->disable_slot)
193 retval = slot->ops->disable_slot(slot);
194 break;
196 case 1:
197 if (slot->ops->enable_slot)
198 retval = slot->ops->enable_slot(slot);
199 break;
201 default:
202 err ("Illegal value specified for power\n");
203 retval = -EINVAL;
205 module_put(slot->ops->owner);
207 exit:
208 if (retval)
209 return retval;
210 return count;
213 static struct hotplug_slot_attribute hotplug_slot_attr_power = {
214 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
215 .show = power_read_file,
216 .store = power_write_file
219 static ssize_t attention_read_file (struct hotplug_slot *slot, char *buf)
221 int retval;
222 u8 value;
224 retval = get_attention_status (slot, &value);
225 if (retval)
226 goto exit;
227 retval = sprintf (buf, "%d\n", value);
229 exit:
230 return retval;
233 static ssize_t attention_write_file (struct hotplug_slot *slot, const char *buf,
234 size_t count)
236 unsigned long lattention;
237 u8 attention;
238 int retval = 0;
240 lattention = simple_strtoul (buf, NULL, 10);
241 attention = (u8)(lattention & 0xff);
242 dbg (" - attention = %d\n", attention);
244 if (!try_module_get(slot->ops->owner)) {
245 retval = -ENODEV;
246 goto exit;
248 if (slot->ops->set_attention_status)
249 retval = slot->ops->set_attention_status(slot, attention);
250 module_put(slot->ops->owner);
252 exit:
253 if (retval)
254 return retval;
255 return count;
258 static struct hotplug_slot_attribute hotplug_slot_attr_attention = {
259 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
260 .show = attention_read_file,
261 .store = attention_write_file
264 static ssize_t latch_read_file (struct hotplug_slot *slot, char *buf)
266 int retval;
267 u8 value;
269 retval = get_latch_status (slot, &value);
270 if (retval)
271 goto exit;
272 retval = sprintf (buf, "%d\n", value);
274 exit:
275 return retval;
278 static struct hotplug_slot_attribute hotplug_slot_attr_latch = {
279 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
280 .show = latch_read_file,
283 static ssize_t presence_read_file (struct hotplug_slot *slot, char *buf)
285 int retval;
286 u8 value;
288 retval = get_adapter_status (slot, &value);
289 if (retval)
290 goto exit;
291 retval = sprintf (buf, "%d\n", value);
293 exit:
294 return retval;
297 static struct hotplug_slot_attribute hotplug_slot_attr_presence = {
298 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
299 .show = presence_read_file,
302 static ssize_t address_read_file (struct hotplug_slot *slot, char *buf)
304 int retval;
305 u32 address;
307 retval = get_address (slot, &address);
308 if (retval)
309 goto exit;
310 retval = sprintf (buf, "%04x:%02x:%02x\n",
311 (address >> 16) & 0xffff,
312 (address >> 8) & 0xff,
313 address & 0xff);
315 exit:
316 return retval;
319 static struct hotplug_slot_attribute hotplug_slot_attr_address = {
320 .attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
321 .show = address_read_file,
324 static char *unknown_speed = "Unknown bus speed";
326 static ssize_t max_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
328 char *speed_string;
329 int retval;
330 enum pci_bus_speed value;
332 retval = get_max_bus_speed (slot, &value);
333 if (retval)
334 goto exit;
336 if (value == PCI_SPEED_UNKNOWN)
337 speed_string = unknown_speed;
338 else
339 speed_string = pci_bus_speed_strings[value];
341 retval = sprintf (buf, "%s\n", speed_string);
343 exit:
344 return retval;
347 static struct hotplug_slot_attribute hotplug_slot_attr_max_bus_speed = {
348 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO},
349 .show = max_bus_speed_read_file,
352 static ssize_t cur_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
354 char *speed_string;
355 int retval;
356 enum pci_bus_speed value;
358 retval = get_cur_bus_speed (slot, &value);
359 if (retval)
360 goto exit;
362 if (value == PCI_SPEED_UNKNOWN)
363 speed_string = unknown_speed;
364 else
365 speed_string = pci_bus_speed_strings[value];
367 retval = sprintf (buf, "%s\n", speed_string);
369 exit:
370 return retval;
373 static struct hotplug_slot_attribute hotplug_slot_attr_cur_bus_speed = {
374 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO},
375 .show = cur_bus_speed_read_file,
378 static ssize_t test_write_file (struct hotplug_slot *slot, const char *buf,
379 size_t count)
381 unsigned long ltest;
382 u32 test;
383 int retval = 0;
385 ltest = simple_strtoul (buf, NULL, 10);
386 test = (u32)(ltest & 0xffffffff);
387 dbg ("test = %d\n", test);
389 if (!try_module_get(slot->ops->owner)) {
390 retval = -ENODEV;
391 goto exit;
393 if (slot->ops->hardware_test)
394 retval = slot->ops->hardware_test(slot, test);
395 module_put(slot->ops->owner);
397 exit:
398 if (retval)
399 return retval;
400 return count;
403 static struct hotplug_slot_attribute hotplug_slot_attr_test = {
404 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
405 .store = test_write_file
408 static int has_power_file (struct hotplug_slot *slot)
410 if ((!slot) || (!slot->ops))
411 return -ENODEV;
412 if ((slot->ops->enable_slot) ||
413 (slot->ops->disable_slot) ||
414 (slot->ops->get_power_status))
415 return 0;
416 return -ENOENT;
419 static int has_attention_file (struct hotplug_slot *slot)
421 if ((!slot) || (!slot->ops))
422 return -ENODEV;
423 if ((slot->ops->set_attention_status) ||
424 (slot->ops->get_attention_status))
425 return 0;
426 return -ENOENT;
429 static int has_latch_file (struct hotplug_slot *slot)
431 if ((!slot) || (!slot->ops))
432 return -ENODEV;
433 if (slot->ops->get_latch_status)
434 return 0;
435 return -ENOENT;
438 static int has_adapter_file (struct hotplug_slot *slot)
440 if ((!slot) || (!slot->ops))
441 return -ENODEV;
442 if (slot->ops->get_adapter_status)
443 return 0;
444 return -ENOENT;
447 static int has_address_file (struct hotplug_slot *slot)
449 if ((!slot) || (!slot->ops))
450 return -ENODEV;
451 if (slot->ops->get_address)
452 return 0;
453 return -ENOENT;
456 static int has_max_bus_speed_file (struct hotplug_slot *slot)
458 if ((!slot) || (!slot->ops))
459 return -ENODEV;
460 if (slot->ops->get_max_bus_speed)
461 return 0;
462 return -ENOENT;
465 static int has_cur_bus_speed_file (struct hotplug_slot *slot)
467 if ((!slot) || (!slot->ops))
468 return -ENODEV;
469 if (slot->ops->get_cur_bus_speed)
470 return 0;
471 return -ENOENT;
474 static int has_test_file (struct hotplug_slot *slot)
476 if ((!slot) || (!slot->ops))
477 return -ENODEV;
478 if (slot->ops->hardware_test)
479 return 0;
480 return -ENOENT;
483 static int fs_add_slot (struct hotplug_slot *slot)
485 if (has_power_file(slot) == 0)
486 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_power.attr);
488 if (has_attention_file(slot) == 0)
489 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
491 if (has_latch_file(slot) == 0)
492 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
494 if (has_adapter_file(slot) == 0)
495 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
497 if (has_address_file(slot) == 0)
498 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_address.attr);
500 if (has_max_bus_speed_file(slot) == 0)
501 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
503 if (has_cur_bus_speed_file(slot) == 0)
504 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
506 if (has_test_file(slot) == 0)
507 sysfs_create_file(&slot->kobj, &hotplug_slot_attr_test.attr);
509 return 0;
512 static void fs_remove_slot (struct hotplug_slot *slot)
514 if (has_power_file(slot) == 0)
515 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
517 if (has_attention_file(slot) == 0)
518 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
520 if (has_latch_file(slot) == 0)
521 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
523 if (has_adapter_file(slot) == 0)
524 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
526 if (has_address_file(slot) == 0)
527 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
529 if (has_max_bus_speed_file(slot) == 0)
530 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
532 if (has_cur_bus_speed_file(slot) == 0)
533 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
535 if (has_test_file(slot) == 0)
536 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
539 static struct hotplug_slot *get_slot_from_name (const char *name)
541 struct hotplug_slot *slot;
542 struct list_head *tmp;
544 list_for_each (tmp, &pci_hotplug_slot_list) {
545 slot = list_entry (tmp, struct hotplug_slot, slot_list);
546 if (strcmp(slot->name, name) == 0)
547 return slot;
549 return NULL;
553 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
554 * @slot: pointer to the &struct hotplug_slot to register
556 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
557 * userspace interaction to the slot.
559 * Returns 0 if successful, anything else for an error.
561 int pci_hp_register (struct hotplug_slot *slot)
563 int result;
565 if (slot == NULL)
566 return -ENODEV;
567 if ((slot->info == NULL) || (slot->ops == NULL))
568 return -EINVAL;
569 if (slot->release == NULL) {
570 dbg("Why are you trying to register a hotplug slot"
571 "without a proper release function?\n");
572 return -EINVAL;
575 kobject_set_name(&slot->kobj, "%s", slot->name);
576 kobj_set_kset_s(slot, pci_hotplug_slots_subsys);
578 /* this can fail if we have already registered a slot with the same name */
579 if (kobject_register(&slot->kobj)) {
580 err("Unable to register kobject");
581 return -EINVAL;
584 list_add (&slot->slot_list, &pci_hotplug_slot_list);
586 result = fs_add_slot (slot);
587 dbg ("Added slot %s to the list\n", slot->name);
588 return result;
592 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
593 * @slot: pointer to the &struct hotplug_slot to deregister
595 * The @slot must have been registered with the pci hotplug subsystem
596 * previously with a call to pci_hp_register().
598 * Returns 0 if successful, anything else for an error.
600 int pci_hp_deregister (struct hotplug_slot *slot)
602 struct hotplug_slot *temp;
604 if (slot == NULL)
605 return -ENODEV;
607 temp = get_slot_from_name (slot->name);
608 if (temp != slot) {
609 return -ENODEV;
611 list_del (&slot->slot_list);
613 fs_remove_slot (slot);
614 dbg ("Removed slot %s from the list\n", slot->name);
615 kobject_unregister(&slot->kobj);
616 return 0;
620 * pci_hp_change_slot_info - changes the slot's information structure in the core
621 * @slot: pointer to the slot whose info has changed
622 * @info: pointer to the info copy into the slot's info structure
624 * @slot must have been registered with the pci
625 * hotplug subsystem previously with a call to pci_hp_register().
627 * Returns 0 if successful, anything else for an error.
629 int pci_hp_change_slot_info (struct hotplug_slot *slot, struct hotplug_slot_info *info)
631 if ((slot == NULL) || (info == NULL))
632 return -ENODEV;
635 * check all fields in the info structure, and update timestamps
636 * for the files referring to the fields that have now changed.
638 if ((has_power_file(slot) == 0) &&
639 (slot->info->power_status != info->power_status))
640 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_power.attr);
642 if ((has_attention_file(slot) == 0) &&
643 (slot->info->attention_status != info->attention_status))
644 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
646 if ((has_latch_file(slot) == 0) &&
647 (slot->info->latch_status != info->latch_status))
648 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
650 if ((has_adapter_file(slot) == 0) &&
651 (slot->info->adapter_status != info->adapter_status))
652 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
654 if ((has_address_file(slot) == 0) &&
655 (slot->info->address != info->address))
656 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_address.attr);
658 if ((has_max_bus_speed_file(slot) == 0) &&
659 (slot->info->max_bus_speed != info->max_bus_speed))
660 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
662 if ((has_cur_bus_speed_file(slot) == 0) &&
663 (slot->info->cur_bus_speed != info->cur_bus_speed))
664 sysfs_update_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
666 memcpy (slot->info, info, sizeof (struct hotplug_slot_info));
668 return 0;
671 static int __init pci_hotplug_init (void)
673 int result;
675 kset_set_kset_s(&pci_hotplug_slots_subsys, pci_bus_type.subsys);
676 result = subsystem_register(&pci_hotplug_slots_subsys);
677 if (result) {
678 err("Register subsys with error %d\n", result);
679 goto exit;
681 result = cpci_hotplug_init(debug);
682 if (result) {
683 err ("cpci_hotplug_init with error %d\n", result);
684 goto err_subsys;
687 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
688 goto exit;
690 err_subsys:
691 subsystem_unregister(&pci_hotplug_slots_subsys);
692 exit:
693 return result;
696 static void __exit pci_hotplug_exit (void)
698 cpci_hotplug_exit();
699 subsystem_unregister(&pci_hotplug_slots_subsys);
702 module_init(pci_hotplug_init);
703 module_exit(pci_hotplug_exit);
705 MODULE_AUTHOR(DRIVER_AUTHOR);
706 MODULE_DESCRIPTION(DRIVER_DESC);
707 MODULE_LICENSE("GPL");
708 module_param(debug, bool, 0644);
709 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
711 EXPORT_SYMBOL_GPL(pci_hotplug_slots_subsys);
712 EXPORT_SYMBOL_GPL(pci_hp_register);
713 EXPORT_SYMBOL_GPL(pci_hp_deregister);
714 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);