2 * PCI HotPlug Controller Core
4 * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2001-2002 IBM Corp.
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
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>
27 * Greg Kroah-Hartman <greg@kroah.com>
28 * Scott Murray <scottm@somanetworks.com>
31 #include <linux/module.h> /* try_module_get & module_put */
32 #include <linux/moduleparam.h>
33 #include <linux/kernel.h>
34 #include <linux/types.h>
35 #include <linux/list.h>
36 #include <linux/kobject.h>
37 #include <linux/sysfs.h>
38 #include <linux/pagemap.h>
39 #include <linux/init.h>
40 #include <linux/mount.h>
41 #include <linux/namei.h>
42 #include <linux/mutex.h>
43 #include <linux/pci.h>
44 #include <linux/pci_hotplug.h>
45 #include <linux/uaccess.h>
47 #include "cpci_hotplug.h"
49 #define MY_NAME "pci_hotplug"
51 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
52 #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
53 #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
54 #define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
59 static LIST_HEAD(pci_hotplug_slot_list
);
60 static DEFINE_MUTEX(pci_hp_mutex
);
62 /* Weee, fun with macros... */
63 #define GET_STATUS(name, type) \
64 static int get_##name(struct hotplug_slot *slot, type *value) \
66 struct hotplug_slot_ops *ops = slot->ops; \
68 if (!try_module_get(ops->owner)) \
70 if (ops->get_##name) \
71 retval = ops->get_##name(slot, value); \
73 *value = slot->info->name; \
74 module_put(ops->owner); \
78 GET_STATUS(power_status
, u8
)
79 GET_STATUS(attention_status
, u8
)
80 GET_STATUS(latch_status
, u8
)
81 GET_STATUS(adapter_status
, u8
)
83 static ssize_t
power_read_file(struct pci_slot
*pci_slot
, char *buf
)
88 retval
= get_power_status(pci_slot
->hotplug
, &value
);
92 return sprintf(buf
, "%d\n", value
);
95 static ssize_t
power_write_file(struct pci_slot
*pci_slot
, const char *buf
,
98 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
103 lpower
= simple_strtoul(buf
, NULL
, 10);
104 power
= (u8
)(lpower
& 0xff);
105 dbg("power = %d\n", power
);
107 if (!try_module_get(slot
->ops
->owner
)) {
113 if (slot
->ops
->disable_slot
)
114 retval
= slot
->ops
->disable_slot(slot
);
118 if (slot
->ops
->enable_slot
)
119 retval
= slot
->ops
->enable_slot(slot
);
123 err("Illegal value specified for power\n");
126 module_put(slot
->ops
->owner
);
134 static struct pci_slot_attribute hotplug_slot_attr_power
= {
135 .attr
= {.name
= "power", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
136 .show
= power_read_file
,
137 .store
= power_write_file
140 static ssize_t
attention_read_file(struct pci_slot
*pci_slot
, char *buf
)
145 retval
= get_attention_status(pci_slot
->hotplug
, &value
);
149 return sprintf(buf
, "%d\n", value
);
152 static ssize_t
attention_write_file(struct pci_slot
*pci_slot
, const char *buf
,
155 struct hotplug_slot_ops
*ops
= pci_slot
->hotplug
->ops
;
156 unsigned long lattention
;
160 lattention
= simple_strtoul(buf
, NULL
, 10);
161 attention
= (u8
)(lattention
& 0xff);
162 dbg(" - attention = %d\n", attention
);
164 if (!try_module_get(ops
->owner
)) {
168 if (ops
->set_attention_status
)
169 retval
= ops
->set_attention_status(pci_slot
->hotplug
, attention
);
170 module_put(ops
->owner
);
178 static struct pci_slot_attribute hotplug_slot_attr_attention
= {
179 .attr
= {.name
= "attention", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
180 .show
= attention_read_file
,
181 .store
= attention_write_file
184 static ssize_t
latch_read_file(struct pci_slot
*pci_slot
, char *buf
)
189 retval
= get_latch_status(pci_slot
->hotplug
, &value
);
193 return sprintf(buf
, "%d\n", value
);
196 static struct pci_slot_attribute hotplug_slot_attr_latch
= {
197 .attr
= {.name
= "latch", .mode
= S_IFREG
| S_IRUGO
},
198 .show
= latch_read_file
,
201 static ssize_t
presence_read_file(struct pci_slot
*pci_slot
, char *buf
)
206 retval
= get_adapter_status(pci_slot
->hotplug
, &value
);
210 return sprintf(buf
, "%d\n", value
);
213 static struct pci_slot_attribute hotplug_slot_attr_presence
= {
214 .attr
= {.name
= "adapter", .mode
= S_IFREG
| S_IRUGO
},
215 .show
= presence_read_file
,
218 static ssize_t
test_write_file(struct pci_slot
*pci_slot
, const char *buf
,
221 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
226 ltest
= simple_strtoul(buf
, NULL
, 10);
227 test
= (u32
)(ltest
& 0xffffffff);
228 dbg("test = %d\n", test
);
230 if (!try_module_get(slot
->ops
->owner
)) {
234 if (slot
->ops
->hardware_test
)
235 retval
= slot
->ops
->hardware_test(slot
, test
);
236 module_put(slot
->ops
->owner
);
244 static struct pci_slot_attribute hotplug_slot_attr_test
= {
245 .attr
= {.name
= "test", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
246 .store
= test_write_file
249 static bool has_power_file(struct pci_slot
*pci_slot
)
251 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
253 if ((!slot
) || (!slot
->ops
))
255 if ((slot
->ops
->enable_slot
) ||
256 (slot
->ops
->disable_slot
) ||
257 (slot
->ops
->get_power_status
))
262 static bool has_attention_file(struct pci_slot
*pci_slot
)
264 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
266 if ((!slot
) || (!slot
->ops
))
268 if ((slot
->ops
->set_attention_status
) ||
269 (slot
->ops
->get_attention_status
))
274 static bool has_latch_file(struct pci_slot
*pci_slot
)
276 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
278 if ((!slot
) || (!slot
->ops
))
280 if (slot
->ops
->get_latch_status
)
285 static bool has_adapter_file(struct pci_slot
*pci_slot
)
287 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
289 if ((!slot
) || (!slot
->ops
))
291 if (slot
->ops
->get_adapter_status
)
296 static bool has_test_file(struct pci_slot
*pci_slot
)
298 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
300 if ((!slot
) || (!slot
->ops
))
302 if (slot
->ops
->hardware_test
)
307 static int fs_add_slot(struct pci_slot
*pci_slot
)
311 /* Create symbolic link to the hotplug driver module */
312 pci_hp_create_module_link(pci_slot
);
314 if (has_power_file(pci_slot
)) {
315 retval
= sysfs_create_file(&pci_slot
->kobj
,
316 &hotplug_slot_attr_power
.attr
);
321 if (has_attention_file(pci_slot
)) {
322 retval
= sysfs_create_file(&pci_slot
->kobj
,
323 &hotplug_slot_attr_attention
.attr
);
328 if (has_latch_file(pci_slot
)) {
329 retval
= sysfs_create_file(&pci_slot
->kobj
,
330 &hotplug_slot_attr_latch
.attr
);
335 if (has_adapter_file(pci_slot
)) {
336 retval
= sysfs_create_file(&pci_slot
->kobj
,
337 &hotplug_slot_attr_presence
.attr
);
342 if (has_test_file(pci_slot
)) {
343 retval
= sysfs_create_file(&pci_slot
->kobj
,
344 &hotplug_slot_attr_test
.attr
);
352 if (has_adapter_file(pci_slot
))
353 sysfs_remove_file(&pci_slot
->kobj
,
354 &hotplug_slot_attr_presence
.attr
);
356 if (has_latch_file(pci_slot
))
357 sysfs_remove_file(&pci_slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
359 if (has_attention_file(pci_slot
))
360 sysfs_remove_file(&pci_slot
->kobj
,
361 &hotplug_slot_attr_attention
.attr
);
363 if (has_power_file(pci_slot
))
364 sysfs_remove_file(&pci_slot
->kobj
, &hotplug_slot_attr_power
.attr
);
366 pci_hp_remove_module_link(pci_slot
);
371 static void fs_remove_slot(struct pci_slot
*pci_slot
)
373 if (has_power_file(pci_slot
))
374 sysfs_remove_file(&pci_slot
->kobj
, &hotplug_slot_attr_power
.attr
);
376 if (has_attention_file(pci_slot
))
377 sysfs_remove_file(&pci_slot
->kobj
,
378 &hotplug_slot_attr_attention
.attr
);
380 if (has_latch_file(pci_slot
))
381 sysfs_remove_file(&pci_slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
383 if (has_adapter_file(pci_slot
))
384 sysfs_remove_file(&pci_slot
->kobj
,
385 &hotplug_slot_attr_presence
.attr
);
387 if (has_test_file(pci_slot
))
388 sysfs_remove_file(&pci_slot
->kobj
, &hotplug_slot_attr_test
.attr
);
390 pci_hp_remove_module_link(pci_slot
);
393 static struct hotplug_slot
*get_slot_from_name(const char *name
)
395 struct hotplug_slot
*slot
;
397 list_for_each_entry(slot
, &pci_hotplug_slot_list
, slot_list
) {
398 if (strcmp(hotplug_slot_name(slot
), name
) == 0)
405 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
406 * @bus: bus this slot is on
407 * @slot: pointer to the &struct hotplug_slot to register
408 * @devnr: device number
409 * @name: name registered with kobject core
410 * @owner: caller module owner
411 * @mod_name: caller module name
413 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
414 * userspace interaction to the slot.
416 * Returns 0 if successful, anything else for an error.
418 int __pci_hp_register(struct hotplug_slot
*slot
, struct pci_bus
*bus
,
419 int devnr
, const char *name
,
420 struct module
*owner
, const char *mod_name
)
423 struct pci_slot
*pci_slot
;
427 if ((slot
->info
== NULL
) || (slot
->ops
== NULL
))
429 if (slot
->release
== NULL
) {
430 dbg("Why are you trying to register a hotplug slot without a proper release function?\n");
434 slot
->ops
->owner
= owner
;
435 slot
->ops
->mod_name
= mod_name
;
437 mutex_lock(&pci_hp_mutex
);
439 * No problems if we call this interface from both ACPI_PCI_SLOT
440 * driver and call it here again. If we've already created the
441 * pci_slot, the interface will simply bump the refcount.
443 pci_slot
= pci_create_slot(bus
, devnr
, name
, slot
);
444 if (IS_ERR(pci_slot
)) {
445 result
= PTR_ERR(pci_slot
);
449 slot
->pci_slot
= pci_slot
;
450 pci_slot
->hotplug
= slot
;
452 list_add(&slot
->slot_list
, &pci_hotplug_slot_list
);
454 result
= fs_add_slot(pci_slot
);
455 kobject_uevent(&pci_slot
->kobj
, KOBJ_ADD
);
456 dbg("Added slot %s to the list\n", name
);
458 mutex_unlock(&pci_hp_mutex
);
461 EXPORT_SYMBOL_GPL(__pci_hp_register
);
464 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
465 * @slot: pointer to the &struct hotplug_slot to deregister
467 * The @slot must have been registered with the pci hotplug subsystem
468 * previously with a call to pci_hp_register().
470 * Returns 0 if successful, anything else for an error.
472 int pci_hp_deregister(struct hotplug_slot
*slot
)
474 struct hotplug_slot
*temp
;
475 struct pci_slot
*pci_slot
;
480 mutex_lock(&pci_hp_mutex
);
481 temp
= get_slot_from_name(hotplug_slot_name(slot
));
483 mutex_unlock(&pci_hp_mutex
);
487 list_del(&slot
->slot_list
);
489 pci_slot
= slot
->pci_slot
;
490 fs_remove_slot(pci_slot
);
491 dbg("Removed slot %s from the list\n", hotplug_slot_name(slot
));
494 pci_slot
->hotplug
= NULL
;
495 pci_destroy_slot(pci_slot
);
496 mutex_unlock(&pci_hp_mutex
);
500 EXPORT_SYMBOL_GPL(pci_hp_deregister
);
503 * pci_hp_change_slot_info - changes the slot's information structure in the core
504 * @slot: pointer to the slot whose info has changed
505 * @info: pointer to the info copy into the slot's info structure
507 * @slot must have been registered with the pci
508 * hotplug subsystem previously with a call to pci_hp_register().
510 * Returns 0 if successful, anything else for an error.
512 int pci_hp_change_slot_info(struct hotplug_slot
*slot
,
513 struct hotplug_slot_info
*info
)
518 memcpy(slot
->info
, info
, sizeof(struct hotplug_slot_info
));
522 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info
);
524 static int __init
pci_hotplug_init(void)
528 result
= cpci_hotplug_init(debug
);
530 err("cpci_hotplug_init with error %d\n", result
);
536 device_initcall(pci_hotplug_init
);
539 * not really modular, but the easiest way to keep compat with existing
540 * bootargs behaviour is to continue using module_param here.
542 module_param(debug
, bool, 0644);
543 MODULE_PARM_DESC(debug
, "Debugging mode enabled or not");