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>
45 #define MY_NAME "pci_hotplug"
47 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
48 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
49 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
50 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
56 #define DRIVER_VERSION "0.5"
57 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
58 #define DRIVER_DESC "PCI Hot Plug PCI Core"
61 //////////////////////////////////////////////////////////////////
63 static LIST_HEAD(pci_hotplug_slot_list
);
64 static DEFINE_MUTEX(pci_hp_mutex
);
66 #ifdef CONFIG_HOTPLUG_PCI_CPCI
67 extern int cpci_hotplug_init(int debug
);
68 extern void cpci_hotplug_exit(void);
70 static inline int cpci_hotplug_init(int debug
) { return 0; }
71 static inline void cpci_hotplug_exit(void) { }
74 /* Weee, fun with macros... */
75 #define GET_STATUS(name,type) \
76 static int get_##name (struct hotplug_slot *slot, type *value) \
78 struct hotplug_slot_ops *ops = slot->ops; \
80 if (!try_module_get(ops->owner)) \
82 if (ops->get_##name) \
83 retval = ops->get_##name(slot, value); \
85 *value = slot->info->name; \
86 module_put(ops->owner); \
90 GET_STATUS(power_status
, u8
)
91 GET_STATUS(attention_status
, u8
)
92 GET_STATUS(latch_status
, u8
)
93 GET_STATUS(adapter_status
, u8
)
95 static ssize_t
power_read_file(struct pci_slot
*slot
, char *buf
)
100 retval
= get_power_status(slot
->hotplug
, &value
);
103 retval
= sprintf (buf
, "%d\n", value
);
108 static ssize_t
power_write_file(struct pci_slot
*pci_slot
, const char *buf
,
111 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
112 unsigned long lpower
;
116 lpower
= simple_strtoul (buf
, NULL
, 10);
117 power
= (u8
)(lpower
& 0xff);
118 dbg ("power = %d\n", power
);
120 if (!try_module_get(slot
->ops
->owner
)) {
126 if (slot
->ops
->disable_slot
)
127 retval
= slot
->ops
->disable_slot(slot
);
131 if (slot
->ops
->enable_slot
)
132 retval
= slot
->ops
->enable_slot(slot
);
136 err ("Illegal value specified for power\n");
139 module_put(slot
->ops
->owner
);
147 static struct pci_slot_attribute hotplug_slot_attr_power
= {
148 .attr
= {.name
= "power", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
149 .show
= power_read_file
,
150 .store
= power_write_file
153 static ssize_t
attention_read_file(struct pci_slot
*slot
, char *buf
)
158 retval
= get_attention_status(slot
->hotplug
, &value
);
161 retval
= sprintf(buf
, "%d\n", value
);
167 static ssize_t
attention_write_file(struct pci_slot
*slot
, const char *buf
,
170 struct hotplug_slot_ops
*ops
= slot
->hotplug
->ops
;
171 unsigned long lattention
;
175 lattention
= simple_strtoul (buf
, NULL
, 10);
176 attention
= (u8
)(lattention
& 0xff);
177 dbg (" - attention = %d\n", attention
);
179 if (!try_module_get(ops
->owner
)) {
183 if (ops
->set_attention_status
)
184 retval
= ops
->set_attention_status(slot
->hotplug
, attention
);
185 module_put(ops
->owner
);
193 static struct pci_slot_attribute hotplug_slot_attr_attention
= {
194 .attr
= {.name
= "attention", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
195 .show
= attention_read_file
,
196 .store
= attention_write_file
199 static ssize_t
latch_read_file(struct pci_slot
*slot
, char *buf
)
204 retval
= get_latch_status(slot
->hotplug
, &value
);
207 retval
= sprintf (buf
, "%d\n", value
);
213 static struct pci_slot_attribute hotplug_slot_attr_latch
= {
214 .attr
= {.name
= "latch", .mode
= S_IFREG
| S_IRUGO
},
215 .show
= latch_read_file
,
218 static ssize_t
presence_read_file(struct pci_slot
*slot
, char *buf
)
223 retval
= get_adapter_status(slot
->hotplug
, &value
);
226 retval
= sprintf (buf
, "%d\n", value
);
232 static struct pci_slot_attribute hotplug_slot_attr_presence
= {
233 .attr
= {.name
= "adapter", .mode
= S_IFREG
| S_IRUGO
},
234 .show
= presence_read_file
,
237 static ssize_t
test_write_file(struct pci_slot
*pci_slot
, const char *buf
,
240 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
245 ltest
= simple_strtoul (buf
, NULL
, 10);
246 test
= (u32
)(ltest
& 0xffffffff);
247 dbg ("test = %d\n", test
);
249 if (!try_module_get(slot
->ops
->owner
)) {
253 if (slot
->ops
->hardware_test
)
254 retval
= slot
->ops
->hardware_test(slot
, test
);
255 module_put(slot
->ops
->owner
);
263 static struct pci_slot_attribute hotplug_slot_attr_test
= {
264 .attr
= {.name
= "test", .mode
= S_IFREG
| S_IRUGO
| S_IWUSR
},
265 .store
= test_write_file
268 static bool has_power_file(struct pci_slot
*pci_slot
)
270 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
271 if ((!slot
) || (!slot
->ops
))
273 if ((slot
->ops
->enable_slot
) ||
274 (slot
->ops
->disable_slot
) ||
275 (slot
->ops
->get_power_status
))
280 static bool has_attention_file(struct pci_slot
*pci_slot
)
282 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
283 if ((!slot
) || (!slot
->ops
))
285 if ((slot
->ops
->set_attention_status
) ||
286 (slot
->ops
->get_attention_status
))
291 static bool has_latch_file(struct pci_slot
*pci_slot
)
293 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
294 if ((!slot
) || (!slot
->ops
))
296 if (slot
->ops
->get_latch_status
)
301 static bool has_adapter_file(struct pci_slot
*pci_slot
)
303 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
304 if ((!slot
) || (!slot
->ops
))
306 if (slot
->ops
->get_adapter_status
)
311 static bool has_test_file(struct pci_slot
*pci_slot
)
313 struct hotplug_slot
*slot
= pci_slot
->hotplug
;
314 if ((!slot
) || (!slot
->ops
))
316 if (slot
->ops
->hardware_test
)
321 static int fs_add_slot(struct pci_slot
*slot
)
325 /* Create symbolic link to the hotplug driver module */
326 pci_hp_create_module_link(slot
);
328 if (has_power_file(slot
)) {
329 retval
= sysfs_create_file(&slot
->kobj
,
330 &hotplug_slot_attr_power
.attr
);
335 if (has_attention_file(slot
)) {
336 retval
= sysfs_create_file(&slot
->kobj
,
337 &hotplug_slot_attr_attention
.attr
);
342 if (has_latch_file(slot
)) {
343 retval
= sysfs_create_file(&slot
->kobj
,
344 &hotplug_slot_attr_latch
.attr
);
349 if (has_adapter_file(slot
)) {
350 retval
= sysfs_create_file(&slot
->kobj
,
351 &hotplug_slot_attr_presence
.attr
);
356 if (has_test_file(slot
)) {
357 retval
= sysfs_create_file(&slot
->kobj
,
358 &hotplug_slot_attr_test
.attr
);
366 if (has_adapter_file(slot
))
367 sysfs_remove_file(&slot
->kobj
,
368 &hotplug_slot_attr_presence
.attr
);
370 if (has_latch_file(slot
))
371 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
373 if (has_attention_file(slot
))
374 sysfs_remove_file(&slot
->kobj
,
375 &hotplug_slot_attr_attention
.attr
);
377 if (has_power_file(slot
))
378 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
380 pci_hp_remove_module_link(slot
);
385 static void fs_remove_slot(struct pci_slot
*slot
)
387 if (has_power_file(slot
))
388 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_power
.attr
);
390 if (has_attention_file(slot
))
391 sysfs_remove_file(&slot
->kobj
,
392 &hotplug_slot_attr_attention
.attr
);
394 if (has_latch_file(slot
))
395 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_latch
.attr
);
397 if (has_adapter_file(slot
))
398 sysfs_remove_file(&slot
->kobj
,
399 &hotplug_slot_attr_presence
.attr
);
401 if (has_test_file(slot
))
402 sysfs_remove_file(&slot
->kobj
, &hotplug_slot_attr_test
.attr
);
404 pci_hp_remove_module_link(slot
);
407 static struct hotplug_slot
*get_slot_from_name (const char *name
)
409 struct hotplug_slot
*slot
;
410 struct list_head
*tmp
;
412 list_for_each (tmp
, &pci_hotplug_slot_list
) {
413 slot
= list_entry (tmp
, struct hotplug_slot
, slot_list
);
414 if (strcmp(hotplug_slot_name(slot
), name
) == 0)
421 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
422 * @bus: bus this slot is on
423 * @slot: pointer to the &struct hotplug_slot to register
424 * @devnr: device number
425 * @name: name registered with kobject core
426 * @owner: caller module owner
427 * @mod_name: caller module name
429 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
430 * userspace interaction to the slot.
432 * Returns 0 if successful, anything else for an error.
434 int __pci_hp_register(struct hotplug_slot
*slot
, struct pci_bus
*bus
,
435 int devnr
, const char *name
,
436 struct module
*owner
, const char *mod_name
)
439 struct pci_slot
*pci_slot
;
443 if ((slot
->info
== NULL
) || (slot
->ops
== NULL
))
445 if (slot
->release
== NULL
) {
446 dbg("Why are you trying to register a hotplug slot "
447 "without a proper release function?\n");
451 slot
->ops
->owner
= owner
;
452 slot
->ops
->mod_name
= mod_name
;
454 mutex_lock(&pci_hp_mutex
);
456 * No problems if we call this interface from both ACPI_PCI_SLOT
457 * driver and call it here again. If we've already created the
458 * pci_slot, the interface will simply bump the refcount.
460 pci_slot
= pci_create_slot(bus
, devnr
, name
, slot
);
461 if (IS_ERR(pci_slot
)) {
462 result
= PTR_ERR(pci_slot
);
466 slot
->pci_slot
= pci_slot
;
467 pci_slot
->hotplug
= slot
;
469 list_add(&slot
->slot_list
, &pci_hotplug_slot_list
);
471 result
= fs_add_slot(pci_slot
);
472 kobject_uevent(&pci_slot
->kobj
, KOBJ_ADD
);
473 dbg("Added slot %s to the list\n", name
);
475 mutex_unlock(&pci_hp_mutex
);
480 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
481 * @hotplug: pointer to the &struct hotplug_slot to deregister
483 * The @slot must have been registered with the pci hotplug subsystem
484 * previously with a call to pci_hp_register().
486 * Returns 0 if successful, anything else for an error.
488 int pci_hp_deregister(struct hotplug_slot
*hotplug
)
490 struct hotplug_slot
*temp
;
491 struct pci_slot
*slot
;
496 mutex_lock(&pci_hp_mutex
);
497 temp
= get_slot_from_name(hotplug_slot_name(hotplug
));
498 if (temp
!= hotplug
) {
499 mutex_unlock(&pci_hp_mutex
);
503 list_del(&hotplug
->slot_list
);
505 slot
= hotplug
->pci_slot
;
506 fs_remove_slot(slot
);
507 dbg("Removed slot %s from the list\n", hotplug_slot_name(hotplug
));
509 hotplug
->release(hotplug
);
510 slot
->hotplug
= NULL
;
511 pci_destroy_slot(slot
);
512 mutex_unlock(&pci_hp_mutex
);
518 * pci_hp_change_slot_info - changes the slot's information structure in the core
519 * @hotplug: pointer to the slot whose info has changed
520 * @info: pointer to the info copy into the slot's info structure
522 * @slot must have been registered with the pci
523 * hotplug subsystem previously with a call to pci_hp_register().
525 * Returns 0 if successful, anything else for an error.
527 int __must_check
pci_hp_change_slot_info(struct hotplug_slot
*hotplug
,
528 struct hotplug_slot_info
*info
)
530 struct pci_slot
*slot
;
531 if (!hotplug
|| !info
)
533 slot
= hotplug
->pci_slot
;
535 memcpy(hotplug
->info
, info
, sizeof(struct hotplug_slot_info
));
540 static int __init
pci_hotplug_init (void)
544 result
= cpci_hotplug_init(debug
);
546 err ("cpci_hotplug_init with error %d\n", result
);
550 info (DRIVER_DESC
" version: " DRIVER_VERSION
"\n");
556 static void __exit
pci_hotplug_exit (void)
561 module_init(pci_hotplug_init
);
562 module_exit(pci_hotplug_exit
);
564 MODULE_AUTHOR(DRIVER_AUTHOR
);
565 MODULE_DESCRIPTION(DRIVER_DESC
);
566 MODULE_LICENSE("GPL");
567 module_param(debug
, bool, 0644);
568 MODULE_PARM_DESC(debug
, "Debugging mode enabled or not");
570 EXPORT_SYMBOL_GPL(__pci_hp_register
);
571 EXPORT_SYMBOL_GPL(pci_hp_deregister
);
572 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info
);