[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / pci / slot.c
blob8c02b6c53bdbd483ce6161bc92ba83f0352bae51
1 /*
2 * drivers/pci/slot.c
3 * Copyright (C) 2006 Matthew Wilcox <matthew@wil.cx>
4 * Copyright (C) 2006-2009 Hewlett-Packard Development Company, L.P.
5 * Alex Chiang <achiang@hp.com>
6 */
8 #include <linux/kobject.h>
9 #include <linux/pci.h>
10 #include <linux/err.h>
11 #include "pci.h"
13 struct kset *pci_slots_kset;
14 EXPORT_SYMBOL_GPL(pci_slots_kset);
16 static ssize_t pci_slot_attr_show(struct kobject *kobj,
17 struct attribute *attr, char *buf)
19 struct pci_slot *slot = to_pci_slot(kobj);
20 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
21 return attribute->show ? attribute->show(slot, buf) : -EIO;
24 static ssize_t pci_slot_attr_store(struct kobject *kobj,
25 struct attribute *attr, const char *buf, size_t len)
27 struct pci_slot *slot = to_pci_slot(kobj);
28 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
29 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
32 static struct sysfs_ops pci_slot_sysfs_ops = {
33 .show = pci_slot_attr_show,
34 .store = pci_slot_attr_store,
37 static ssize_t address_read_file(struct pci_slot *slot, char *buf)
39 if (slot->number == 0xff)
40 return sprintf(buf, "%04x:%02x\n",
41 pci_domain_nr(slot->bus),
42 slot->bus->number);
43 else
44 return sprintf(buf, "%04x:%02x:%02x\n",
45 pci_domain_nr(slot->bus),
46 slot->bus->number,
47 slot->number);
50 static void pci_slot_release(struct kobject *kobj)
52 struct pci_dev *dev;
53 struct pci_slot *slot = to_pci_slot(kobj);
55 dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
56 slot->number, pci_slot_name(slot));
58 list_for_each_entry(dev, &slot->bus->devices, bus_list)
59 if (PCI_SLOT(dev->devfn) == slot->number)
60 dev->slot = NULL;
62 list_del(&slot->list);
64 kfree(slot);
67 static struct pci_slot_attribute pci_slot_attr_address =
68 __ATTR(address, (S_IFREG | S_IRUGO), address_read_file, NULL);
70 static struct attribute *pci_slot_default_attrs[] = {
71 &pci_slot_attr_address.attr,
72 NULL,
75 static struct kobj_type pci_slot_ktype = {
76 .sysfs_ops = &pci_slot_sysfs_ops,
77 .release = &pci_slot_release,
78 .default_attrs = pci_slot_default_attrs,
81 static char *make_slot_name(const char *name)
83 char *new_name;
84 int len, max, dup;
86 new_name = kstrdup(name, GFP_KERNEL);
87 if (!new_name)
88 return NULL;
91 * Make sure we hit the realloc case the first time through the
92 * loop. 'len' will be strlen(name) + 3 at that point which is
93 * enough space for "name-X" and the trailing NUL.
95 len = strlen(name) + 2;
96 max = 1;
97 dup = 1;
99 for (;;) {
100 struct kobject *dup_slot;
101 dup_slot = kset_find_obj(pci_slots_kset, new_name);
102 if (!dup_slot)
103 break;
104 kobject_put(dup_slot);
105 if (dup == max) {
106 len++;
107 max *= 10;
108 kfree(new_name);
109 new_name = kmalloc(len, GFP_KERNEL);
110 if (!new_name)
111 break;
113 sprintf(new_name, "%s-%d", name, dup++);
116 return new_name;
119 static int rename_slot(struct pci_slot *slot, const char *name)
121 int result = 0;
122 char *slot_name;
124 if (strcmp(pci_slot_name(slot), name) == 0)
125 return result;
127 slot_name = make_slot_name(name);
128 if (!slot_name)
129 return -ENOMEM;
131 result = kobject_rename(&slot->kobj, slot_name);
132 kfree(slot_name);
134 return result;
137 static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
139 struct pci_slot *slot;
141 * We already hold pci_bus_sem so don't worry
143 list_for_each_entry(slot, &parent->slots, list)
144 if (slot->number == slot_nr) {
145 kobject_get(&slot->kobj);
146 return slot;
149 return NULL;
153 * pci_create_slot - create or increment refcount for physical PCI slot
154 * @parent: struct pci_bus of parent bridge
155 * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
156 * @name: user visible string presented in /sys/bus/pci/slots/<name>
157 * @hotplug: set if caller is hotplug driver, NULL otherwise
159 * PCI slots have first class attributes such as address, speed, width,
160 * and a &struct pci_slot is used to manage them. This interface will
161 * either return a new &struct pci_slot to the caller, or if the pci_slot
162 * already exists, its refcount will be incremented.
164 * Slots are uniquely identified by a @pci_bus, @slot_nr tuple.
166 * There are known platforms with broken firmware that assign the same
167 * name to multiple slots. Workaround these broken platforms by renaming
168 * the slots on behalf of the caller. If firmware assigns name N to
169 * multiple slots:
171 * The first slot is assigned N
172 * The second slot is assigned N-1
173 * The third slot is assigned N-2
174 * etc.
176 * Placeholder slots:
177 * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
178 * a slot. There is one notable exception - pSeries (rpaphp), where the
179 * @slot_nr cannot be determined until a device is actually inserted into
180 * the slot. In this scenario, the caller may pass -1 for @slot_nr.
182 * The following semantics are imposed when the caller passes @slot_nr ==
183 * -1. First, we no longer check for an existing %struct pci_slot, as there
184 * may be many slots with @slot_nr of -1. The other change in semantics is
185 * user-visible, which is the 'address' parameter presented in sysfs will
186 * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
187 * %struct pci_bus and bb is the bus number. In other words, the devfn of
188 * the 'placeholder' slot will not be displayed.
190 struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
191 const char *name,
192 struct hotplug_slot *hotplug)
194 struct pci_dev *dev;
195 struct pci_slot *slot;
196 int err = 0;
197 char *slot_name = NULL;
199 down_write(&pci_bus_sem);
201 if (slot_nr == -1)
202 goto placeholder;
205 * Hotplug drivers are allowed to rename an existing slot,
206 * but only if not already claimed.
208 slot = get_slot(parent, slot_nr);
209 if (slot) {
210 if (hotplug) {
211 if ((err = slot->hotplug ? -EBUSY : 0)
212 || (err = rename_slot(slot, name))) {
213 kobject_put(&slot->kobj);
214 slot = NULL;
215 goto err;
218 goto out;
221 placeholder:
222 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
223 if (!slot) {
224 err = -ENOMEM;
225 goto err;
228 slot->bus = parent;
229 slot->number = slot_nr;
231 slot->kobj.kset = pci_slots_kset;
233 slot_name = make_slot_name(name);
234 if (!slot_name) {
235 err = -ENOMEM;
236 goto err;
239 err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
240 "%s", slot_name);
241 if (err)
242 goto err;
244 INIT_LIST_HEAD(&slot->list);
245 list_add(&slot->list, &parent->slots);
247 list_for_each_entry(dev, &parent->devices, bus_list)
248 if (PCI_SLOT(dev->devfn) == slot_nr)
249 dev->slot = slot;
251 dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
252 slot_nr, pci_slot_name(slot));
254 out:
255 kfree(slot_name);
256 up_write(&pci_bus_sem);
257 return slot;
258 err:
259 kfree(slot);
260 slot = ERR_PTR(err);
261 goto out;
263 EXPORT_SYMBOL_GPL(pci_create_slot);
266 * pci_renumber_slot - update %struct pci_slot -> number
267 * @slot: &struct pci_slot to update
268 * @slot_nr: new number for slot
270 * The primary purpose of this interface is to allow callers who earlier
271 * created a placeholder slot in pci_create_slot() by passing a -1 as
272 * slot_nr, to update their %struct pci_slot with the correct @slot_nr.
274 void pci_renumber_slot(struct pci_slot *slot, int slot_nr)
276 struct pci_slot *tmp;
278 down_write(&pci_bus_sem);
280 list_for_each_entry(tmp, &slot->bus->slots, list) {
281 WARN_ON(tmp->number == slot_nr);
282 goto out;
285 slot->number = slot_nr;
286 out:
287 up_write(&pci_bus_sem);
289 EXPORT_SYMBOL_GPL(pci_renumber_slot);
292 * pci_destroy_slot - decrement refcount for physical PCI slot
293 * @slot: struct pci_slot to decrement
295 * %struct pci_slot is refcounted, so destroying them is really easy; we
296 * just call kobject_put on its kobj and let our release methods do the
297 * rest.
299 void pci_destroy_slot(struct pci_slot *slot)
301 dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
302 slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
304 down_write(&pci_bus_sem);
305 kobject_put(&slot->kobj);
306 up_write(&pci_bus_sem);
308 EXPORT_SYMBOL_GPL(pci_destroy_slot);
310 #if defined(CONFIG_HOTPLUG_PCI) || defined(CONFIG_HOTPLUG_PCI_MODULE)
311 #include <linux/pci_hotplug.h>
313 * pci_hp_create_link - create symbolic link to the hotplug driver module.
314 * @pci_slot: struct pci_slot
316 * Helper function for pci_hotplug_core.c to create symbolic link to
317 * the hotplug driver module.
319 void pci_hp_create_module_link(struct pci_slot *pci_slot)
321 struct hotplug_slot *slot = pci_slot->hotplug;
322 struct kobject *kobj = NULL;
323 int no_warn;
325 if (!slot || !slot->ops)
326 return;
327 kobj = kset_find_obj(module_kset, slot->ops->mod_name);
328 if (!kobj)
329 return;
330 no_warn = sysfs_create_link(&pci_slot->kobj, kobj, "module");
331 kobject_put(kobj);
333 EXPORT_SYMBOL_GPL(pci_hp_create_module_link);
336 * pci_hp_remove_link - remove symbolic link to the hotplug driver module.
337 * @pci_slot: struct pci_slot
339 * Helper function for pci_hotplug_core.c to remove symbolic link to
340 * the hotplug driver module.
342 void pci_hp_remove_module_link(struct pci_slot *pci_slot)
344 sysfs_remove_link(&pci_slot->kobj, "module");
346 EXPORT_SYMBOL_GPL(pci_hp_remove_module_link);
347 #endif
349 static int pci_slot_init(void)
351 struct kset *pci_bus_kset;
353 pci_bus_kset = bus_get_kset(&pci_bus_type);
354 pci_slots_kset = kset_create_and_add("slots", NULL,
355 &pci_bus_kset->kobj);
356 if (!pci_slots_kset) {
357 printk(KERN_ERR "PCI: Slot initialization failure\n");
358 return -ENOMEM;
360 return 0;
363 subsys_initcall(pci_slot_init);