PCI: Move EXPORT_SYMBOL so it immediately follows function/variable
[deliverable/linux.git] / drivers / pci / hotplug / pci_hotplug_core.c
CommitLineData
1da177e4
LT
1/*
2 * PCI HotPlug Controller Core
3 *
4 * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2001-2002 IBM Corp.
6 *
7 * All rights reserved.
8 *
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.
13 *
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.
19 *
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.
23 *
fb5f4d7a 24 * Send feedback to <kristen.c.accardi@intel.com>
1da177e4
LT
25 *
26 */
27
1da177e4
LT
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>
7a54f25c
GKH
33#include <linux/kobject.h>
34#include <linux/sysfs.h>
1da177e4 35#include <linux/pagemap.h>
1da177e4
LT
36#include <linux/init.h>
37#include <linux/mount.h>
38#include <linux/namei.h>
95cb9093 39#include <linux/mutex.h>
1da177e4 40#include <linux/pci.h>
7a54f25c 41#include <linux/pci_hotplug.h>
1da177e4 42#include <asm/uaccess.h>
f46753c5 43#include "../pci.h"
c3139ba2 44#include "cpci_hotplug.h"
1da177e4
LT
45
46#define MY_NAME "pci_hotplug"
47
66bef8c0 48#define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
1da177e4
LT
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)
52
53
54/* local variables */
90ab5ee9 55static bool debug;
1da177e4
LT
56
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"
60
61
62//////////////////////////////////////////////////////////////////
63
64static LIST_HEAD(pci_hotplug_slot_list);
95cb9093 65static DEFINE_MUTEX(pci_hp_mutex);
1da177e4 66
1da177e4
LT
67/* Weee, fun with macros... */
68#define GET_STATUS(name,type) \
69static int get_##name (struct hotplug_slot *slot, type *value) \
70{ \
71 struct hotplug_slot_ops *ops = slot->ops; \
72 int retval = 0; \
bd1d9855 73 if (!try_module_get(ops->owner)) \
c8761fe8
ZY
74 return -ENODEV; \
75 if (ops->get_##name) \
76 retval = ops->get_##name(slot, value); \
77 else \
78 *value = slot->info->name; \
79 module_put(ops->owner); \
1da177e4
LT
80 return retval; \
81}
82
83GET_STATUS(power_status, u8)
84GET_STATUS(attention_status, u8)
85GET_STATUS(latch_status, u8)
86GET_STATUS(adapter_status, u8)
1da177e4 87
f46753c5 88static ssize_t power_read_file(struct pci_slot *slot, char *buf)
1da177e4
LT
89{
90 int retval;
91 u8 value;
92
f46753c5 93 retval = get_power_status(slot->hotplug, &value);
1da177e4
LT
94 if (retval)
95 goto exit;
96 retval = sprintf (buf, "%d\n", value);
97exit:
98 return retval;
99}
100
f46753c5 101static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
1da177e4
LT
102 size_t count)
103{
f46753c5 104 struct hotplug_slot *slot = pci_slot->hotplug;
1da177e4
LT
105 unsigned long lpower;
106 u8 power;
107 int retval = 0;
108
109 lpower = simple_strtoul (buf, NULL, 10);
110 power = (u8)(lpower & 0xff);
111 dbg ("power = %d\n", power);
112
113 if (!try_module_get(slot->ops->owner)) {
114 retval = -ENODEV;
115 goto exit;
116 }
117 switch (power) {
118 case 0:
119 if (slot->ops->disable_slot)
120 retval = slot->ops->disable_slot(slot);
121 break;
122
123 case 1:
124 if (slot->ops->enable_slot)
125 retval = slot->ops->enable_slot(slot);
126 break;
127
128 default:
129 err ("Illegal value specified for power\n");
130 retval = -EINVAL;
131 }
132 module_put(slot->ops->owner);
133
f7625980 134exit:
1da177e4
LT
135 if (retval)
136 return retval;
137 return count;
138}
139
f46753c5 140static struct pci_slot_attribute hotplug_slot_attr_power = {
1da177e4
LT
141 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
142 .show = power_read_file,
143 .store = power_write_file
144};
145
f46753c5 146static ssize_t attention_read_file(struct pci_slot *slot, char *buf)
1da177e4
LT
147{
148 int retval;
149 u8 value;
150
f46753c5 151 retval = get_attention_status(slot->hotplug, &value);
1da177e4
LT
152 if (retval)
153 goto exit;
f46753c5 154 retval = sprintf(buf, "%d\n", value);
1da177e4
LT
155
156exit:
157 return retval;
158}
159
f46753c5 160static ssize_t attention_write_file(struct pci_slot *slot, const char *buf,
1da177e4
LT
161 size_t count)
162{
f46753c5 163 struct hotplug_slot_ops *ops = slot->hotplug->ops;
1da177e4
LT
164 unsigned long lattention;
165 u8 attention;
166 int retval = 0;
167
168 lattention = simple_strtoul (buf, NULL, 10);
169 attention = (u8)(lattention & 0xff);
170 dbg (" - attention = %d\n", attention);
171
f46753c5 172 if (!try_module_get(ops->owner)) {
1da177e4
LT
173 retval = -ENODEV;
174 goto exit;
175 }
f46753c5
AC
176 if (ops->set_attention_status)
177 retval = ops->set_attention_status(slot->hotplug, attention);
178 module_put(ops->owner);
1da177e4 179
f7625980 180exit:
1da177e4
LT
181 if (retval)
182 return retval;
183 return count;
184}
185
f46753c5 186static struct pci_slot_attribute hotplug_slot_attr_attention = {
1da177e4
LT
187 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
188 .show = attention_read_file,
189 .store = attention_write_file
190};
191
f46753c5 192static ssize_t latch_read_file(struct pci_slot *slot, char *buf)
1da177e4
LT
193{
194 int retval;
195 u8 value;
196
f46753c5 197 retval = get_latch_status(slot->hotplug, &value);
1da177e4
LT
198 if (retval)
199 goto exit;
200 retval = sprintf (buf, "%d\n", value);
201
202exit:
203 return retval;
204}
205
f46753c5 206static struct pci_slot_attribute hotplug_slot_attr_latch = {
1da177e4
LT
207 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
208 .show = latch_read_file,
209};
210
f46753c5 211static ssize_t presence_read_file(struct pci_slot *slot, char *buf)
1da177e4
LT
212{
213 int retval;
214 u8 value;
215
f46753c5 216 retval = get_adapter_status(slot->hotplug, &value);
1da177e4
LT
217 if (retval)
218 goto exit;
219 retval = sprintf (buf, "%d\n", value);
220
221exit:
222 return retval;
223}
224
f46753c5 225static struct pci_slot_attribute hotplug_slot_attr_presence = {
1da177e4
LT
226 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
227 .show = presence_read_file,
228};
229
f46753c5 230static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
1da177e4
LT
231 size_t count)
232{
f46753c5 233 struct hotplug_slot *slot = pci_slot->hotplug;
1da177e4
LT
234 unsigned long ltest;
235 u32 test;
236 int retval = 0;
237
238 ltest = simple_strtoul (buf, NULL, 10);
239 test = (u32)(ltest & 0xffffffff);
240 dbg ("test = %d\n", test);
241
242 if (!try_module_get(slot->ops->owner)) {
243 retval = -ENODEV;
244 goto exit;
245 }
246 if (slot->ops->hardware_test)
247 retval = slot->ops->hardware_test(slot, test);
248 module_put(slot->ops->owner);
249
f7625980 250exit:
1da177e4
LT
251 if (retval)
252 return retval;
253 return count;
254}
255
f46753c5 256static struct pci_slot_attribute hotplug_slot_attr_test = {
1da177e4
LT
257 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
258 .store = test_write_file
259};
260
498a8faf 261static bool has_power_file(struct pci_slot *pci_slot)
1da177e4 262{
f46753c5 263 struct hotplug_slot *slot = pci_slot->hotplug;
1da177e4 264 if ((!slot) || (!slot->ops))
498a8faf 265 return false;
1da177e4
LT
266 if ((slot->ops->enable_slot) ||
267 (slot->ops->disable_slot) ||
268 (slot->ops->get_power_status))
498a8faf
KK
269 return true;
270 return false;
1da177e4
LT
271}
272
498a8faf 273static bool has_attention_file(struct pci_slot *pci_slot)
1da177e4 274{
f46753c5 275 struct hotplug_slot *slot = pci_slot->hotplug;
1da177e4 276 if ((!slot) || (!slot->ops))
498a8faf 277 return false;
1da177e4
LT
278 if ((slot->ops->set_attention_status) ||
279 (slot->ops->get_attention_status))
498a8faf
KK
280 return true;
281 return false;
1da177e4
LT
282}
283
498a8faf 284static bool has_latch_file(struct pci_slot *pci_slot)
1da177e4 285{
f46753c5 286 struct hotplug_slot *slot = pci_slot->hotplug;
1da177e4 287 if ((!slot) || (!slot->ops))
498a8faf 288 return false;
1da177e4 289 if (slot->ops->get_latch_status)
498a8faf
KK
290 return true;
291 return false;
1da177e4
LT
292}
293
498a8faf 294static bool has_adapter_file(struct pci_slot *pci_slot)
1da177e4 295{
f46753c5 296 struct hotplug_slot *slot = pci_slot->hotplug;
1da177e4 297 if ((!slot) || (!slot->ops))
498a8faf 298 return false;
1da177e4 299 if (slot->ops->get_adapter_status)
498a8faf
KK
300 return true;
301 return false;
1da177e4
LT
302}
303
498a8faf 304static bool has_test_file(struct pci_slot *pci_slot)
1da177e4 305{
f46753c5 306 struct hotplug_slot *slot = pci_slot->hotplug;
1da177e4 307 if ((!slot) || (!slot->ops))
498a8faf 308 return false;
1da177e4 309 if (slot->ops->hardware_test)
498a8faf
KK
310 return true;
311 return false;
1da177e4
LT
312}
313
f46753c5 314static int fs_add_slot(struct pci_slot *slot)
1da177e4 315{
660a0e8f 316 int retval = 0;
1da177e4 317
c825bc94
KK
318 /* Create symbolic link to the hotplug driver module */
319 pci_hp_create_module_link(slot);
320
498a8faf
KK
321 if (has_power_file(slot)) {
322 retval = sysfs_create_file(&slot->kobj,
323 &hotplug_slot_attr_power.attr);
660a0e8f
GKH
324 if (retval)
325 goto exit_power;
326 }
1da177e4 327
498a8faf 328 if (has_attention_file(slot)) {
660a0e8f
GKH
329 retval = sysfs_create_file(&slot->kobj,
330 &hotplug_slot_attr_attention.attr);
331 if (retval)
332 goto exit_attention;
333 }
1da177e4 334
498a8faf 335 if (has_latch_file(slot)) {
660a0e8f
GKH
336 retval = sysfs_create_file(&slot->kobj,
337 &hotplug_slot_attr_latch.attr);
338 if (retval)
339 goto exit_latch;
340 }
1da177e4 341
498a8faf 342 if (has_adapter_file(slot)) {
660a0e8f
GKH
343 retval = sysfs_create_file(&slot->kobj,
344 &hotplug_slot_attr_presence.attr);
345 if (retval)
346 goto exit_adapter;
347 }
1da177e4 348
498a8faf 349 if (has_test_file(slot)) {
660a0e8f
GKH
350 retval = sysfs_create_file(&slot->kobj,
351 &hotplug_slot_attr_test.attr);
352 if (retval)
353 goto exit_test;
354 }
355
356 goto exit;
357
358exit_test:
498a8faf
KK
359 if (has_adapter_file(slot))
360 sysfs_remove_file(&slot->kobj,
361 &hotplug_slot_attr_presence.attr);
660a0e8f 362exit_adapter:
498a8faf 363 if (has_latch_file(slot))
660a0e8f 364 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
660a0e8f 365exit_latch:
498a8faf
KK
366 if (has_attention_file(slot))
367 sysfs_remove_file(&slot->kobj,
368 &hotplug_slot_attr_attention.attr);
660a0e8f 369exit_attention:
498a8faf 370 if (has_power_file(slot))
660a0e8f
GKH
371 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
372exit_power:
c825bc94 373 pci_hp_remove_module_link(slot);
660a0e8f
GKH
374exit:
375 return retval;
1da177e4
LT
376}
377
f46753c5 378static void fs_remove_slot(struct pci_slot *slot)
1da177e4 379{
498a8faf 380 if (has_power_file(slot))
1da177e4
LT
381 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
382
498a8faf
KK
383 if (has_attention_file(slot))
384 sysfs_remove_file(&slot->kobj,
385 &hotplug_slot_attr_attention.attr);
1da177e4 386
498a8faf 387 if (has_latch_file(slot))
1da177e4
LT
388 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
389
498a8faf
KK
390 if (has_adapter_file(slot))
391 sysfs_remove_file(&slot->kobj,
392 &hotplug_slot_attr_presence.attr);
1da177e4 393
498a8faf 394 if (has_test_file(slot))
1da177e4 395 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
c825bc94
KK
396
397 pci_hp_remove_module_link(slot);
1da177e4
LT
398}
399
400static struct hotplug_slot *get_slot_from_name (const char *name)
401{
402 struct hotplug_slot *slot;
403 struct list_head *tmp;
404
405 list_for_each (tmp, &pci_hotplug_slot_list) {
406 slot = list_entry (tmp, struct hotplug_slot, slot_list);
58319b80 407 if (strcmp(hotplug_slot_name(slot), name) == 0)
95cb9093 408 return slot;
1da177e4 409 }
95cb9093 410 return NULL;
1da177e4
LT
411}
412
413/**
c825bc94 414 * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
65b943f6 415 * @bus: bus this slot is on
1da177e4 416 * @slot: pointer to the &struct hotplug_slot to register
c825bc94 417 * @devnr: device number
1359f270 418 * @name: name registered with kobject core
503998ca
RD
419 * @owner: caller module owner
420 * @mod_name: caller module name
1da177e4
LT
421 *
422 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
423 * userspace interaction to the slot.
424 *
425 * Returns 0 if successful, anything else for an error.
426 */
c825bc94
KK
427int __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)
1da177e4
LT
430{
431 int result;
f46753c5 432 struct pci_slot *pci_slot;
1da177e4
LT
433
434 if (slot == NULL)
435 return -ENODEV;
436 if ((slot->info == NULL) || (slot->ops == NULL))
437 return -EINVAL;
438 if (slot->release == NULL) {
a6f29a98 439 dbg("Why are you trying to register a hotplug slot "
1da177e4
LT
440 "without a proper release function?\n");
441 return -EINVAL;
442 }
443
c825bc94
KK
444 slot->ops->owner = owner;
445 slot->ops->mod_name = mod_name;
95cb9093 446
c825bc94 447 mutex_lock(&pci_hp_mutex);
8344b568
AC
448 /*
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.
452 */
c825bc94 453 pci_slot = pci_create_slot(bus, devnr, name, slot);
95cb9093
KK
454 if (IS_ERR(pci_slot)) {
455 result = PTR_ERR(pci_slot);
5fe6cc60 456 goto out;
1da177e4 457 }
64dbcac3 458
f46753c5
AC
459 slot->pci_slot = pci_slot;
460 pci_slot->hotplug = slot;
461
f46753c5 462 list_add(&slot->slot_list, &pci_hotplug_slot_list);
f46753c5
AC
463
464 result = fs_add_slot(pci_slot);
465 kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
1359f270 466 dbg("Added slot %s to the list\n", name);
95cb9093
KK
467out:
468 mutex_unlock(&pci_hp_mutex);
1da177e4
LT
469 return result;
470}
b7fe9434 471EXPORT_SYMBOL_GPL(__pci_hp_register);
1da177e4
LT
472
473/**
474 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
65b943f6 475 * @hotplug: pointer to the &struct hotplug_slot to deregister
1da177e4
LT
476 *
477 * The @slot must have been registered with the pci hotplug subsystem
478 * previously with a call to pci_hp_register().
479 *
480 * Returns 0 if successful, anything else for an error.
481 */
f46753c5 482int pci_hp_deregister(struct hotplug_slot *hotplug)
1da177e4
LT
483{
484 struct hotplug_slot *temp;
f46753c5 485 struct pci_slot *slot;
1da177e4 486
f46753c5 487 if (!hotplug)
1da177e4
LT
488 return -ENODEV;
489
95cb9093 490 mutex_lock(&pci_hp_mutex);
58319b80 491 temp = get_slot_from_name(hotplug_slot_name(hotplug));
95cb9093
KK
492 if (temp != hotplug) {
493 mutex_unlock(&pci_hp_mutex);
1da177e4 494 return -ENODEV;
95cb9093 495 }
1da177e4 496
f46753c5 497 list_del(&hotplug->slot_list);
f46753c5
AC
498
499 slot = hotplug->pci_slot;
500 fs_remove_slot(slot);
58319b80 501 dbg("Removed slot %s from the list\n", hotplug_slot_name(hotplug));
f46753c5
AC
502
503 hotplug->release(hotplug);
504 slot->hotplug = NULL;
505 pci_destroy_slot(slot);
95cb9093 506 mutex_unlock(&pci_hp_mutex);
f46753c5 507
1da177e4
LT
508 return 0;
509}
b7fe9434 510EXPORT_SYMBOL_GPL(pci_hp_deregister);
1da177e4
LT
511
512/**
513 * pci_hp_change_slot_info - changes the slot's information structure in the core
65b943f6 514 * @hotplug: pointer to the slot whose info has changed
1da177e4
LT
515 * @info: pointer to the info copy into the slot's info structure
516 *
f7625980 517 * @slot must have been registered with the pci
1da177e4
LT
518 * hotplug subsystem previously with a call to pci_hp_register().
519 *
520 * Returns 0 if successful, anything else for an error.
521 */
d67aed63
BH
522int pci_hp_change_slot_info(struct hotplug_slot *hotplug,
523 struct hotplug_slot_info *info)
1da177e4 524{
f46753c5 525 if (!hotplug || !info)
1da177e4
LT
526 return -ENODEV;
527
f46753c5 528 memcpy(hotplug->info, info, sizeof(struct hotplug_slot_info));
1da177e4
LT
529
530 return 0;
531}
b7fe9434 532EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);
1da177e4
LT
533
534static int __init pci_hotplug_init (void)
535{
536 int result;
0fed80f7 537
1da177e4
LT
538 result = cpci_hotplug_init(debug);
539 if (result) {
540 err ("cpci_hotplug_init with error %d\n", result);
f46753c5 541 goto err_cpci;
1da177e4
LT
542 }
543
544 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
81ace5cd 545
f46753c5 546err_cpci:
1da177e4
LT
547 return result;
548}
549
550static void __exit pci_hotplug_exit (void)
551{
552 cpci_hotplug_exit();
1da177e4
LT
553}
554
555module_init(pci_hotplug_init);
556module_exit(pci_hotplug_exit);
557
558MODULE_AUTHOR(DRIVER_AUTHOR);
559MODULE_DESCRIPTION(DRIVER_DESC);
560MODULE_LICENSE("GPL");
561module_param(debug, bool, 0644);
562MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
This page took 0.758919 seconds and 5 git commands to generate.