2 * core.c - contains all core device and protocol registration functions
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
9 #include <linux/types.h>
10 #include <linux/list.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <linux/dma-mapping.h>
21 static LIST_HEAD(pnp_protocols);
22 LIST_HEAD(pnp_global);
23 DEFINE_SPINLOCK(pnp_lock);
26 * ACPI or PNPBIOS should tell us about all platform devices, so we can
27 * skip some blind probes. ISAPNP typically enumerates only plug-in ISA
28 * devices, not built-in things like COM ports.
30 int pnp_platform_devices;
31 EXPORT_SYMBOL(pnp_platform_devices);
33 void *pnp_alloc(long size)
37 result = kzalloc(size, GFP_KERNEL);
39 printk(KERN_ERR "pnp: Out of Memory\n");
46 * pnp_protocol_register - adds a pnp protocol to the pnp layer
47 * @protocol: pointer to the corresponding pnp_protocol structure
49 * Ex protocols: ISAPNP, PNPBIOS, etc
52 int pnp_register_protocol(struct pnp_protocol *protocol)
55 struct list_head *pos;
60 INIT_LIST_HEAD(&protocol->devices);
61 INIT_LIST_HEAD(&protocol->cards);
65 /* assign the lowest unused number */
66 list_for_each(pos, &pnp_protocols) {
67 struct pnp_protocol *cur = to_pnp_protocol(pos);
68 if (cur->number == nodenum) {
74 list_add_tail(&protocol->protocol_list, &pnp_protocols);
75 spin_unlock(&pnp_lock);
77 protocol->number = nodenum;
78 sprintf(protocol->dev.bus_id, "pnp%d", nodenum);
79 return device_register(&protocol->dev);
83 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
84 * @protocol: pointer to the corresponding pnp_protocol structure
87 void pnp_unregister_protocol(struct pnp_protocol *protocol)
90 list_del(&protocol->protocol_list);
91 spin_unlock(&pnp_lock);
92 device_unregister(&protocol->dev);
95 static void pnp_free_ids(struct pnp_dev *dev)
109 static void pnp_release_device(struct device *dmdev)
111 struct pnp_dev *dev = to_pnp_dev(dmdev);
112 pnp_free_option(dev->independent);
113 pnp_free_option(dev->dependent);
118 int __pnp_add_device(struct pnp_dev *dev)
121 pnp_fixup_device(dev);
122 dev->dev.bus = &pnp_bus_type;
123 dev->dev.dma_mask = &dev->dma_mask;
124 dev->dma_mask = dev->dev.coherent_dma_mask = DMA_24BIT_MASK;
125 dev->dev.release = &pnp_release_device;
126 dev->status = PNP_READY;
127 spin_lock(&pnp_lock);
128 list_add_tail(&dev->global_list, &pnp_global);
129 list_add_tail(&dev->protocol_list, &dev->protocol->devices);
130 spin_unlock(&pnp_lock);
132 ret = device_register(&dev->dev);
134 pnp_interface_attach_device(dev);
139 * pnp_add_device - adds a pnp device to the pnp layer
140 * @dev: pointer to dev to add
142 * adds to driver model, name database, fixups, interface, etc.
145 int pnp_add_device(struct pnp_dev *dev)
147 if (!dev || !dev->protocol || dev->card)
149 dev->dev.parent = &dev->protocol->dev;
150 sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number,
152 return __pnp_add_device(dev);
155 void __pnp_remove_device(struct pnp_dev *dev)
157 spin_lock(&pnp_lock);
158 list_del(&dev->global_list);
159 list_del(&dev->protocol_list);
160 spin_unlock(&pnp_lock);
161 device_unregister(&dev->dev);
165 * pnp_remove_device - removes a pnp device from the pnp layer
166 * @dev: pointer to dev to add
168 * this function will free all mem used by dev
171 void pnp_remove_device(struct pnp_dev *dev)
173 if (!dev || dev->card)
175 __pnp_remove_device(dev);
179 static int __init pnp_init(void)
181 printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n");
182 return bus_register(&pnp_bus_type);
185 subsys_initcall(pnp_init);
188 EXPORT_SYMBOL(pnp_register_protocol);
189 EXPORT_SYMBOL(pnp_unregister_protocol);
190 EXPORT_SYMBOL(pnp_add_device);
191 EXPORT_SYMBOL(pnp_remove_device);