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