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>
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>
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)
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 //////////////////////////////////////////////////////////////////
64 static LIST_HEAD(pci_hotplug_slot_list
);
65 static DEFINE_MUTEX(pci_hp_mutex
);
67 /* Weee, fun with macros... */
68 #define GET_STATUS(name,type) \
69 static int get_##name (struct hotplug_slot *slot, type *value) \
71 struct hotplug_slot_ops *ops = slot->ops; \
73 if (!try_module_get(ops->owner)) \
75 if (ops->get_##name) \
76 retval = ops->get_##name(slot, value); \
78 *value = slot->info->name; \
79 module_put(ops->owner); \
83 GET_STATUS(power_status
, u8
)
84 GET_STATUS(attention_status
, u8
)
85 GET_STATUS(latch_status
, u8
)
86 GET_STATUS(adapter_status
, u8
)
88 static ssize_t
power_read_file(struct pci_slot
*slot
, char *buf
)
93 retval
= get_power_status(slot
->hotplug
, &value
);
96 retval
= sprintf (buf
, "%d\n", value
);
101 static ssize_t
power_write_file(struct pci_slot
*pci_slot
, const char *buf
,
104 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
105 unsigned long lpower
;
109 lpower
= simple_strtoul (buf
, NULL
, 10);
110 power
= (u8
)(lpower
& 0xff);
111 dbg ("power = %d\n", power
);
113 if (!try_module_get(slot
->ops
->owner
)) {
119 if (slot
->ops
->disable_slot
)
120 retval
= slot
->ops
->disable_slot(slot
);
124 if (slot
->ops
->enable_slot
)
125 retval
= slot
->ops
->enable_slot(slot
);
129 err ("Illegal value specified for power\n");
132 module_put(slot
->ops
->owner
);
140 static struct pci_slot_attribute hotplug_slot_attr_power
= {
141 .attr
= {.name
= "power", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
142 .show
= power_read_file
,
143 .store
= power_write_file
146 static ssize_t
attention_read_file(struct pci_slot
*slot
, char *buf
)
151 retval
= get_attention_status(slot
->hotplug
, &value
);
154 retval
= sprintf(buf
, "%d\n", value
);
160 static ssize_t
attention_write_file(struct pci_slot
*slot
, const char *buf
,
163 struct hotplug_slot_ops
*ops
= slot
->hotplug
->ops
;
164 unsigned long lattention
;
168 lattention
= simple_strtoul (buf
, NULL
, 10);
169 attention
= (u8
)(lattention
& 0xff);
170 dbg (" - attention = %d\n", attention
);
172 if (!try_module_get(ops
->owner
)) {
176 if (ops
->set_attention_status
)
177 retval
= ops
->set_attention_status(slot
->hotplug
, attention
);
178 module_put(ops
->owner
);
186 static struct pci_slot_attribute hotplug_slot_attr_attention
= {
187 .attr
= {.name
= "attention", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
188 .show
= attention_read_file
,
189 .store
= attention_write_file
192 static ssize_t
latch_read_file(struct pci_slot
*slot
, char *buf
)
197 retval
= get_latch_status(slot
->hotplug
, &value
);
200 retval
= sprintf (buf
, "%d\n", value
);
206 static struct pci_slot_attribute hotplug_slot_attr_latch
= {
207 .attr
= {.name
= "latch", .mode
= S_IFREG
| S_IRUGO
},
208 .show
= latch_read_file
,
211 static ssize_t
presence_read_file(struct pci_slot
*slot
, char *buf
)
216 retval
= get_adapter_status(slot
->hotplug
, &value
);
219 retval
= sprintf (buf
, "%d\n", value
);
225 static struct pci_slot_attribute hotplug_slot_attr_presence
= {
226 .attr
= {.name
= "adapter", .mode
= S_IFREG
| S_IRUGO
},
227 .show
= presence_read_file
,
230 static ssize_t
test_write_file(struct pci_slot
*pci_slot
, const char *buf
,
233 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
238 ltest
= simple_strtoul (buf
, NULL
, 10);
239 test
= (u32
)(ltest
& 0xffffffff);
240 dbg ("test = %d\n", test
);
242 if (!try_module_get(slot
->ops
->owner
)) {
246 if (slot
->ops
->hardware_test
)
247 retval
= slot
->ops
->hardware_test(slot
, test
);
248 module_put(slot
->ops
->owner
);
256 static struct pci_slot_attribute hotplug_slot_attr_test
= {
257 .attr
= {.name
= "test", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
258 .store
= test_write_file
261 static bool has_power_file(struct pci_slot
*pci_slot
)
263 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
264 if ((!slot
) || (!slot
->ops
))
266 if ((slot
->ops
->enable_slot
) ||
267 (slot
->ops
->disable_slot
) ||
268 (slot
->ops
->get_power_status
))
273 static bool has_attention_file(struct pci_slot
*pci_slot
)
275 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
276 if ((!slot
) || (!slot
->ops
))
278 if ((slot
->ops
->set_attention_status
) ||
279 (slot
->ops
->get_attention_status
))
284 static bool has_latch_file(struct pci_slot
*pci_slot
)
286 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
287 if ((!slot
) || (!slot
->ops
))
289 if (slot
->ops
->get_latch_status
)
294 static bool has_adapter_file(struct pci_slot
*pci_slot
)
296 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
297 if ((!slot
) || (!slot
->ops
))
299 if (slot
->ops
->get_adapter_status
)
304 static bool has_test_file(struct pci_slot
*pci_slot
)
306 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
307 if ((!slot
) || (!slot
->ops
))
309 if (slot
->ops
->hardware_test
)
314 static int fs_add_slot(struct pci_slot
*slot
)
318 /* Create symbolic link to the hotplug driver module */
319 pci_hp_create_module_link(slot
);
321 if (has_power_file(slot
)) {
322 retval
= sysfs_create_file(&slot
->kobj
,
323 &hotplug_slot_attr_power
.attr
);
328 if (has_attention_file(slot
)) {
329 retval
= sysfs_create_file(&slot
->kobj
,
330 &hotplug_slot_attr_attention
.attr
);
335 if (has_latch_file(slot
)) {
336 retval
= sysfs_create_file(&slot
->kobj
,
337 &hotplug_slot_attr_latch
.attr
);
342 if (has_adapter_file(slot
)) {
343 retval
= sysfs_create_file(&slot
->kobj
,
344 &hotplug_slot_attr_presence
.attr
);
349 if (has_test_file(slot
)) {
350 retval
= sysfs_create_file(&slot
->kobj
,
351 &hotplug_slot_attr_test
.attr
);
359 if (has_adapter_file(slot
))
360 sysfs_remove_file(&slot
->kobj
,
361 &hotplug_slot_attr_presence
.attr
);
363 if (has_latch_file(slot
))
364 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
366 if (has_attention_file(slot
))
367 sysfs_remove_file(&slot
->kobj
,
368 &hotplug_slot_attr_attention
.attr
);
370 if (has_power_file(slot
))
371 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
373 pci_hp_remove_module_link(slot
);
378 static void fs_remove_slot(struct pci_slot
*slot
)
380 if (has_power_file(slot
))
381 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
383 if (has_attention_file(slot
))
384 sysfs_remove_file(&slot
->kobj
,
385 &hotplug_slot_attr_attention
.attr
);
387 if (has_latch_file(slot
))
388 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
390 if (has_adapter_file(slot
))
391 sysfs_remove_file(&slot
->kobj
,
392 &hotplug_slot_attr_presence
.attr
);
394 if (has_test_file(slot
))
395 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_test
.attr
);
397 pci_hp_remove_module_link(slot
);
400 static struct hotplug_slot
*get_slot_from_name (const char *name
)
402 struct hotplug_slot
*slot
;
403 struct list_head
*tmp
;
405 list_for_each (tmp
, &pci_hotplug_slot_list
) {
406 slot
= list_entry (tmp
, struct hotplug_slot
, slot_list
);
407 if (strcmp(hotplug_slot_name(slot
), name
) == 0)
414 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
415 * @bus: bus this slot is on
416 * @slot: pointer to the &struct hotplug_slot to register
417 * @devnr: device number
418 * @name: name registered with kobject core
419 * @owner: caller module owner
420 * @mod_name: caller module name
422 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
423 * userspace interaction to the slot.
425 * Returns 0 if successful, anything else for an error.
427 int __pci_hp_register(struct hotplug_slot
*slot
, struct pci_bus
*bus
,
428 int devnr
, const char *name
,
429 struct module
*owner
, const char *mod_name
)
432 struct pci_slot
*pci_slot
;
436 if ((slot
->info
== NULL
) || (slot
->ops
== NULL
))
438 if (slot
->release
== NULL
) {
439 dbg("Why are you trying to register a hotplug slot "
440 "without a proper release function?\n");
444 slot
->ops
->owner
= owner
;
445 slot
->ops
->mod_name
= mod_name
;
447 mutex_lock(&pci_hp_mutex
);
449 * No problems if we call this interface from both ACPI_PCI_SLOT
450 * driver and call it here again. If we've already created the
451 * pci_slot, the interface will simply bump the refcount.
453 pci_slot
= pci_create_slot(bus
, devnr
, name
, slot
);
454 if (IS_ERR(pci_slot
)) {
455 result
= PTR_ERR(pci_slot
);
459 slot
->pci_slot
= pci_slot
;
460 pci_slot
->hotplug
= slot
;
462 list_add(&slot
->slot_list
, &pci_hotplug_slot_list
);
464 result
= fs_add_slot(pci_slot
);
465 kobject_uevent(&pci_slot
->kobj
, KOBJ_ADD
);
466 dbg("Added slot %s to the list\n", name
);
468 mutex_unlock(&pci_hp_mutex
);
473 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
474 * @hotplug: pointer to the &struct hotplug_slot to deregister
476 * The @slot must have been registered with the pci hotplug subsystem
477 * previously with a call to pci_hp_register().
479 * Returns 0 if successful, anything else for an error.
481 int pci_hp_deregister(struct hotplug_slot
*hotplug
)
483 struct hotplug_slot
*temp
;
484 struct pci_slot
*slot
;
489 mutex_lock(&pci_hp_mutex
);
490 temp
= get_slot_from_name(hotplug_slot_name(hotplug
));
491 if (temp
!= hotplug
) {
492 mutex_unlock(&pci_hp_mutex
);
496 list_del(&hotplug
->slot_list
);
498 slot
= hotplug
->pci_slot
;
499 fs_remove_slot(slot
);
500 dbg("Removed slot %s from the list\n", hotplug_slot_name(hotplug
));
502 hotplug
->release(hotplug
);
503 slot
->hotplug
= NULL
;
504 pci_destroy_slot(slot
);
505 mutex_unlock(&pci_hp_mutex
);
511 * pci_hp_change_slot_info - changes the slot's information structure in the core
512 * @hotplug: pointer to the slot whose info has changed
513 * @info: pointer to the info copy into the slot's info structure
515 * @slot must have been registered with the pci
516 * hotplug subsystem previously with a call to pci_hp_register().
518 * Returns 0 if successful, anything else for an error.
520 int pci_hp_change_slot_info(struct hotplug_slot
*hotplug
,
521 struct hotplug_slot_info
*info
)
523 if (!hotplug
|| !info
)
526 memcpy(hotplug
->info
, info
, sizeof(struct hotplug_slot_info
));
531 static int __init
pci_hotplug_init (void)
535 result
= cpci_hotplug_init(debug
);
537 err ("cpci_hotplug_init with error %d\n", result
);
541 info (DRIVER_DESC
" version: " DRIVER_VERSION
"\n");
547 static void __exit
pci_hotplug_exit (void)
552 module_init(pci_hotplug_init
);
553 module_exit(pci_hotplug_exit
);
555 MODULE_AUTHOR(DRIVER_AUTHOR
);
556 MODULE_DESCRIPTION(DRIVER_DESC
);
557 MODULE_LICENSE("GPL");
558 module_param(debug
, bool, 0644);
559 MODULE_PARM_DESC(debug
, "Debugging mode enabled or not");
561 EXPORT_SYMBOL_GPL(__pci_hp_register
);
562 EXPORT_SYMBOL_GPL(pci_hp_deregister
);
563 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info
);