V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / pci / hotplug / pci_hotplug_core.c
blob47bb0e1ff3faff303a72cbe17963da8e34f675fc
1 /*
2 * PCI HotPlug Controller Core
4 * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2001-2002 IBM Corp.
7 * All rights reserved.
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
18 * details.
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/slab.h>
37 #include <linux/init.h>
38 #include <linux/mount.h>
39 #include <linux/namei.h>
40 #include <linux/pci.h>
41 #include <linux/pci_hotplug.h>
42 #include <asm/uaccess.h>
44 #define MY_NAME "pci_hotplug"
46 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __FUNCTION__ , ## arg); } while (0)
47 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
48 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
49 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
52 /* local variables */
53 static int debug;
55 #define DRIVER_VERSION "0.5"
56 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
57 #define DRIVER_DESC "PCI Hot Plug PCI Core"
60 //////////////////////////////////////////////////////////////////
62 static LIST_HEAD(pci_hotplug_slot_list);
64 struct kset *pci_hotplug_slots_kset;
66 static ssize_t hotplug_slot_attr_show(struct kobject *kobj,
67 struct attribute *attr, char *buf)
69 struct hotplug_slot *slot = to_hotplug_slot(kobj);
70 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
71 return attribute->show ? attribute->show(slot, buf) : -EIO;
74 static ssize_t hotplug_slot_attr_store(struct kobject *kobj,
75 struct attribute *attr, const char *buf, size_t len)
77 struct hotplug_slot *slot = to_hotplug_slot(kobj);
78 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
79 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
82 static struct sysfs_ops hotplug_slot_sysfs_ops = {
83 .show = hotplug_slot_attr_show,
84 .store = hotplug_slot_attr_store,
87 static void hotplug_slot_release(struct kobject *kobj)
89 struct hotplug_slot *slot = to_hotplug_slot(kobj);
90 if (slot->release)
91 slot->release(slot);
94 static struct kobj_type hotplug_slot_ktype = {
95 .sysfs_ops = &hotplug_slot_sysfs_ops,
96 .release = &hotplug_slot_release,
99 /* these strings match up with the values in pci_bus_speed */
100 static char *pci_bus_speed_strings[] = {
101 "33 MHz PCI", /* 0x00 */
102 "66 MHz PCI", /* 0x01 */
103 "66 MHz PCIX", /* 0x02 */
104 "100 MHz PCIX", /* 0x03 */
105 "133 MHz PCIX", /* 0x04 */
106 NULL, /* 0x05 */
107 NULL, /* 0x06 */
108 NULL, /* 0x07 */
109 NULL, /* 0x08 */
110 "66 MHz PCIX 266", /* 0x09 */
111 "100 MHz PCIX 266", /* 0x0a */
112 "133 MHz PCIX 266", /* 0x0b */
113 NULL, /* 0x0c */
114 NULL, /* 0x0d */
115 NULL, /* 0x0e */
116 NULL, /* 0x0f */
117 NULL, /* 0x10 */
118 "66 MHz PCIX 533", /* 0x11 */
119 "100 MHz PCIX 533", /* 0x12 */
120 "133 MHz PCIX 533", /* 0x13 */
121 "25 GBps PCI-E", /* 0x14 */
124 #ifdef CONFIG_HOTPLUG_PCI_CPCI
125 extern int cpci_hotplug_init(int debug);
126 extern void cpci_hotplug_exit(void);
127 #else
128 static inline int cpci_hotplug_init(int debug) { return 0; }
129 static inline void cpci_hotplug_exit(void) { }
130 #endif
132 /* Weee, fun with macros... */
133 #define GET_STATUS(name,type) \
134 static int get_##name (struct hotplug_slot *slot, type *value) \
136 struct hotplug_slot_ops *ops = slot->ops; \
137 int retval = 0; \
138 if (try_module_get(ops->owner)) { \
139 if (ops->get_##name) \
140 retval = ops->get_##name (slot, value); \
141 else \
142 *value = slot->info->name; \
143 module_put(ops->owner); \
145 return retval; \
148 GET_STATUS(power_status, u8)
149 GET_STATUS(attention_status, u8)
150 GET_STATUS(latch_status, u8)
151 GET_STATUS(adapter_status, u8)
152 GET_STATUS(address, u32)
153 GET_STATUS(max_bus_speed, enum pci_bus_speed)
154 GET_STATUS(cur_bus_speed, enum pci_bus_speed)
156 static ssize_t power_read_file (struct hotplug_slot *slot, char *buf)
158 int retval;
159 u8 value;
161 retval = get_power_status (slot, &value);
162 if (retval)
163 goto exit;
164 retval = sprintf (buf, "%d\n", value);
165 exit:
166 return retval;
169 static ssize_t power_write_file (struct hotplug_slot *slot, const char *buf,
170 size_t count)
172 unsigned long lpower;
173 u8 power;
174 int retval = 0;
176 lpower = simple_strtoul (buf, NULL, 10);
177 power = (u8)(lpower & 0xff);
178 dbg ("power = %d\n", power);
180 if (!try_module_get(slot->ops->owner)) {
181 retval = -ENODEV;
182 goto exit;
184 switch (power) {
185 case 0:
186 if (slot->ops->disable_slot)
187 retval = slot->ops->disable_slot(slot);
188 break;
190 case 1:
191 if (slot->ops->enable_slot)
192 retval = slot->ops->enable_slot(slot);
193 break;
195 default:
196 err ("Illegal value specified for power\n");
197 retval = -EINVAL;
199 module_put(slot->ops->owner);
201 exit:
202 if (retval)
203 return retval;
204 return count;
207 static struct hotplug_slot_attribute hotplug_slot_attr_power = {
208 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
209 .show = power_read_file,
210 .store = power_write_file
213 static ssize_t attention_read_file (struct hotplug_slot *slot, char *buf)
215 int retval;
216 u8 value;
218 retval = get_attention_status (slot, &value);
219 if (retval)
220 goto exit;
221 retval = sprintf (buf, "%d\n", value);
223 exit:
224 return retval;
227 static ssize_t attention_write_file (struct hotplug_slot *slot, const char *buf,
228 size_t count)
230 unsigned long lattention;
231 u8 attention;
232 int retval = 0;
234 lattention = simple_strtoul (buf, NULL, 10);
235 attention = (u8)(lattention & 0xff);
236 dbg (" - attention = %d\n", attention);
238 if (!try_module_get(slot->ops->owner)) {
239 retval = -ENODEV;
240 goto exit;
242 if (slot->ops->set_attention_status)
243 retval = slot->ops->set_attention_status(slot, attention);
244 module_put(slot->ops->owner);
246 exit:
247 if (retval)
248 return retval;
249 return count;
252 static struct hotplug_slot_attribute hotplug_slot_attr_attention = {
253 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
254 .show = attention_read_file,
255 .store = attention_write_file
258 static ssize_t latch_read_file (struct hotplug_slot *slot, char *buf)
260 int retval;
261 u8 value;
263 retval = get_latch_status (slot, &value);
264 if (retval)
265 goto exit;
266 retval = sprintf (buf, "%d\n", value);
268 exit:
269 return retval;
272 static struct hotplug_slot_attribute hotplug_slot_attr_latch = {
273 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
274 .show = latch_read_file,
277 static ssize_t presence_read_file (struct hotplug_slot *slot, char *buf)
279 int retval;
280 u8 value;
282 retval = get_adapter_status (slot, &value);
283 if (retval)
284 goto exit;
285 retval = sprintf (buf, "%d\n", value);
287 exit:
288 return retval;
291 static struct hotplug_slot_attribute hotplug_slot_attr_presence = {
292 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
293 .show = presence_read_file,
296 static ssize_t address_read_file (struct hotplug_slot *slot, char *buf)
298 int retval;
299 u32 address;
301 retval = get_address (slot, &address);
302 if (retval)
303 goto exit;
304 retval = sprintf (buf, "%04x:%02x:%02x\n",
305 (address >> 16) & 0xffff,
306 (address >> 8) & 0xff,
307 address & 0xff);
309 exit:
310 return retval;
313 static struct hotplug_slot_attribute hotplug_slot_attr_address = {
314 .attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
315 .show = address_read_file,
318 static char *unknown_speed = "Unknown bus speed";
320 static ssize_t max_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
322 char *speed_string;
323 int retval;
324 enum pci_bus_speed value;
326 retval = get_max_bus_speed (slot, &value);
327 if (retval)
328 goto exit;
330 if (value == PCI_SPEED_UNKNOWN)
331 speed_string = unknown_speed;
332 else
333 speed_string = pci_bus_speed_strings[value];
335 retval = sprintf (buf, "%s\n", speed_string);
337 exit:
338 return retval;
341 static struct hotplug_slot_attribute hotplug_slot_attr_max_bus_speed = {
342 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO},
343 .show = max_bus_speed_read_file,
346 static ssize_t cur_bus_speed_read_file (struct hotplug_slot *slot, char *buf)
348 char *speed_string;
349 int retval;
350 enum pci_bus_speed value;
352 retval = get_cur_bus_speed (slot, &value);
353 if (retval)
354 goto exit;
356 if (value == PCI_SPEED_UNKNOWN)
357 speed_string = unknown_speed;
358 else
359 speed_string = pci_bus_speed_strings[value];
361 retval = sprintf (buf, "%s\n", speed_string);
363 exit:
364 return retval;
367 static struct hotplug_slot_attribute hotplug_slot_attr_cur_bus_speed = {
368 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO},
369 .show = cur_bus_speed_read_file,
372 static ssize_t test_write_file (struct hotplug_slot *slot, const char *buf,
373 size_t count)
375 unsigned long ltest;
376 u32 test;
377 int retval = 0;
379 ltest = simple_strtoul (buf, NULL, 10);
380 test = (u32)(ltest & 0xffffffff);
381 dbg ("test = %d\n", test);
383 if (!try_module_get(slot->ops->owner)) {
384 retval = -ENODEV;
385 goto exit;
387 if (slot->ops->hardware_test)
388 retval = slot->ops->hardware_test(slot, test);
389 module_put(slot->ops->owner);
391 exit:
392 if (retval)
393 return retval;
394 return count;
397 static struct hotplug_slot_attribute hotplug_slot_attr_test = {
398 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
399 .store = test_write_file
402 static int has_power_file (struct hotplug_slot *slot)
404 if ((!slot) || (!slot->ops))
405 return -ENODEV;
406 if ((slot->ops->enable_slot) ||
407 (slot->ops->disable_slot) ||
408 (slot->ops->get_power_status))
409 return 0;
410 return -ENOENT;
413 static int has_attention_file (struct hotplug_slot *slot)
415 if ((!slot) || (!slot->ops))
416 return -ENODEV;
417 if ((slot->ops->set_attention_status) ||
418 (slot->ops->get_attention_status))
419 return 0;
420 return -ENOENT;
423 static int has_latch_file (struct hotplug_slot *slot)
425 if ((!slot) || (!slot->ops))
426 return -ENODEV;
427 if (slot->ops->get_latch_status)
428 return 0;
429 return -ENOENT;
432 static int has_adapter_file (struct hotplug_slot *slot)
434 if ((!slot) || (!slot->ops))
435 return -ENODEV;
436 if (slot->ops->get_adapter_status)
437 return 0;
438 return -ENOENT;
441 static int has_address_file (struct hotplug_slot *slot)
443 if ((!slot) || (!slot->ops))
444 return -ENODEV;
445 if (slot->ops->get_address)
446 return 0;
447 return -ENOENT;
450 static int has_max_bus_speed_file (struct hotplug_slot *slot)
452 if ((!slot) || (!slot->ops))
453 return -ENODEV;
454 if (slot->ops->get_max_bus_speed)
455 return 0;
456 return -ENOENT;
459 static int has_cur_bus_speed_file (struct hotplug_slot *slot)
461 if ((!slot) || (!slot->ops))
462 return -ENODEV;
463 if (slot->ops->get_cur_bus_speed)
464 return 0;
465 return -ENOENT;
468 static int has_test_file (struct hotplug_slot *slot)
470 if ((!slot) || (!slot->ops))
471 return -ENODEV;
472 if (slot->ops->hardware_test)
473 return 0;
474 return -ENOENT;
477 static int fs_add_slot (struct hotplug_slot *slot)
479 int retval = 0;
481 if (has_power_file(slot) == 0) {
482 retval = sysfs_create_file(&slot->kobj, &hotplug_slot_attr_power.attr);
483 if (retval)
484 goto exit_power;
487 if (has_attention_file(slot) == 0) {
488 retval = sysfs_create_file(&slot->kobj,
489 &hotplug_slot_attr_attention.attr);
490 if (retval)
491 goto exit_attention;
494 if (has_latch_file(slot) == 0) {
495 retval = sysfs_create_file(&slot->kobj,
496 &hotplug_slot_attr_latch.attr);
497 if (retval)
498 goto exit_latch;
501 if (has_adapter_file(slot) == 0) {
502 retval = sysfs_create_file(&slot->kobj,
503 &hotplug_slot_attr_presence.attr);
504 if (retval)
505 goto exit_adapter;
508 if (has_address_file(slot) == 0) {
509 retval = sysfs_create_file(&slot->kobj,
510 &hotplug_slot_attr_address.attr);
511 if (retval)
512 goto exit_address;
515 if (has_max_bus_speed_file(slot) == 0) {
516 retval = sysfs_create_file(&slot->kobj,
517 &hotplug_slot_attr_max_bus_speed.attr);
518 if (retval)
519 goto exit_max_speed;
522 if (has_cur_bus_speed_file(slot) == 0) {
523 retval = sysfs_create_file(&slot->kobj,
524 &hotplug_slot_attr_cur_bus_speed.attr);
525 if (retval)
526 goto exit_cur_speed;
529 if (has_test_file(slot) == 0) {
530 retval = sysfs_create_file(&slot->kobj,
531 &hotplug_slot_attr_test.attr);
532 if (retval)
533 goto exit_test;
536 goto exit;
538 exit_test:
539 if (has_cur_bus_speed_file(slot) == 0)
540 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
542 exit_cur_speed:
543 if (has_max_bus_speed_file(slot) == 0)
544 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
546 exit_max_speed:
547 if (has_address_file(slot) == 0)
548 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
550 exit_address:
551 if (has_adapter_file(slot) == 0)
552 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
554 exit_adapter:
555 if (has_latch_file(slot) == 0)
556 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
558 exit_latch:
559 if (has_attention_file(slot) == 0)
560 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
562 exit_attention:
563 if (has_power_file(slot) == 0)
564 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
565 exit_power:
566 exit:
567 return retval;
570 static void fs_remove_slot (struct hotplug_slot *slot)
572 if (has_power_file(slot) == 0)
573 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
575 if (has_attention_file(slot) == 0)
576 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_attention.attr);
578 if (has_latch_file(slot) == 0)
579 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
581 if (has_adapter_file(slot) == 0)
582 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
584 if (has_address_file(slot) == 0)
585 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
587 if (has_max_bus_speed_file(slot) == 0)
588 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
590 if (has_cur_bus_speed_file(slot) == 0)
591 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_cur_bus_speed.attr);
593 if (has_test_file(slot) == 0)
594 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
597 static struct hotplug_slot *get_slot_from_name (const char *name)
599 struct hotplug_slot *slot;
600 struct list_head *tmp;
602 list_for_each (tmp, &pci_hotplug_slot_list) {
603 slot = list_entry (tmp, struct hotplug_slot, slot_list);
604 if (strcmp(slot->name, name) == 0)
605 return slot;
607 return NULL;
611 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
612 * @slot: pointer to the &struct hotplug_slot to register
614 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
615 * userspace interaction to the slot.
617 * Returns 0 if successful, anything else for an error.
619 int pci_hp_register (struct hotplug_slot *slot)
621 int result;
623 if (slot == NULL)
624 return -ENODEV;
625 if ((slot->info == NULL) || (slot->ops == NULL))
626 return -EINVAL;
627 if (slot->release == NULL) {
628 dbg("Why are you trying to register a hotplug slot"
629 "without a proper release function?\n");
630 return -EINVAL;
633 /* this can fail if we have already registered a slot with the same name */
634 slot->kobj.kset = pci_hotplug_slots_kset;
635 result = kobject_init_and_add(&slot->kobj, &hotplug_slot_ktype, NULL,
636 "%s", slot->name);
637 if (result) {
638 err("Unable to register kobject '%s'", slot->name);
639 return -EINVAL;
642 list_add (&slot->slot_list, &pci_hotplug_slot_list);
644 result = fs_add_slot (slot);
645 kobject_uevent(&slot->kobj, KOBJ_ADD);
646 dbg ("Added slot %s to the list\n", slot->name);
647 return result;
651 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
652 * @slot: pointer to the &struct hotplug_slot to deregister
654 * The @slot must have been registered with the pci hotplug subsystem
655 * previously with a call to pci_hp_register().
657 * Returns 0 if successful, anything else for an error.
659 int pci_hp_deregister (struct hotplug_slot *slot)
661 struct hotplug_slot *temp;
663 if (slot == NULL)
664 return -ENODEV;
666 temp = get_slot_from_name (slot->name);
667 if (temp != slot) {
668 return -ENODEV;
670 list_del (&slot->slot_list);
672 fs_remove_slot (slot);
673 dbg ("Removed slot %s from the list\n", slot->name);
674 kobject_put(&slot->kobj);
675 return 0;
679 * pci_hp_change_slot_info - changes the slot's information structure in the core
680 * @slot: pointer to the slot whose info has changed
681 * @info: pointer to the info copy into the slot's info structure
683 * @slot must have been registered with the pci
684 * hotplug subsystem previously with a call to pci_hp_register().
686 * Returns 0 if successful, anything else for an error.
688 int __must_check pci_hp_change_slot_info(struct hotplug_slot *slot,
689 struct hotplug_slot_info *info)
691 if ((slot == NULL) || (info == NULL))
692 return -ENODEV;
694 memcpy (slot->info, info, sizeof (struct hotplug_slot_info));
696 return 0;
699 static int __init pci_hotplug_init (void)
701 int result;
702 struct kset *pci_bus_kset;
704 pci_bus_kset = bus_get_kset(&pci_bus_type);
706 pci_hotplug_slots_kset = kset_create_and_add("slots", NULL,
707 &pci_bus_kset->kobj);
708 if (!pci_hotplug_slots_kset) {
709 result = -ENOMEM;
710 err("Register subsys error\n");
711 goto exit;
713 result = cpci_hotplug_init(debug);
714 if (result) {
715 err ("cpci_hotplug_init with error %d\n", result);
716 goto err_subsys;
719 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
720 goto exit;
722 err_subsys:
723 kset_unregister(pci_hotplug_slots_kset);
724 exit:
725 return result;
728 static void __exit pci_hotplug_exit (void)
730 cpci_hotplug_exit();
731 kset_unregister(pci_hotplug_slots_kset);
734 module_init(pci_hotplug_init);
735 module_exit(pci_hotplug_exit);
737 MODULE_AUTHOR(DRIVER_AUTHOR);
738 MODULE_DESCRIPTION(DRIVER_DESC);
739 MODULE_LICENSE("GPL");
740 module_param(debug, bool, 0644);
741 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
743 EXPORT_SYMBOL_GPL(pci_hotplug_slots_kset);
744 EXPORT_SYMBOL_GPL(pci_hp_register);
745 EXPORT_SYMBOL_GPL(pci_hp_deregister);
746 EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);