Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / pnp / core.c
blobaec83ec5ea23f2471c55fbbe3dafd073b296c42e
1 /*
2 * core.c - contains all core device and protocol registration functions
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
6 */
8 #include <linux/pnp.h>
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>
18 #include "base.h"
21 static LIST_HEAD(pnp_protocols);
22 LIST_HEAD(pnp_global);
23 DEFINE_SPINLOCK(pnp_lock);
25 void *pnp_alloc(long size)
27 void *result;
29 result = kmalloc(size, GFP_KERNEL);
30 if (!result){
31 printk(KERN_ERR "pnp: Out of Memory\n");
32 return NULL;
34 memset(result, 0, size);
35 return result;
38 /**
39 * pnp_protocol_register - adds a pnp protocol to the pnp layer
40 * @protocol: pointer to the corresponding pnp_protocol structure
42 * Ex protocols: ISAPNP, PNPBIOS, etc
45 int pnp_register_protocol(struct pnp_protocol *protocol)
47 int nodenum;
48 struct list_head * pos;
50 if (!protocol)
51 return -EINVAL;
53 INIT_LIST_HEAD(&protocol->devices);
54 INIT_LIST_HEAD(&protocol->cards);
55 nodenum = 0;
56 spin_lock(&pnp_lock);
58 /* assign the lowest unused number */
59 list_for_each(pos,&pnp_protocols) {
60 struct pnp_protocol * cur = to_pnp_protocol(pos);
61 if (cur->number == nodenum){
62 pos = &pnp_protocols;
63 nodenum++;
67 list_add_tail(&protocol->protocol_list, &pnp_protocols);
68 spin_unlock(&pnp_lock);
70 protocol->number = nodenum;
71 sprintf(protocol->dev.bus_id, "pnp%d", nodenum);
72 return device_register(&protocol->dev);
75 /**
76 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
77 * @protocol: pointer to the corresponding pnp_protocol structure
80 void pnp_unregister_protocol(struct pnp_protocol *protocol)
82 spin_lock(&pnp_lock);
83 list_del(&protocol->protocol_list);
84 spin_unlock(&pnp_lock);
85 device_unregister(&protocol->dev);
89 static void pnp_free_ids(struct pnp_dev *dev)
91 struct pnp_id * id;
92 struct pnp_id * next;
93 if (!dev)
94 return;
95 id = dev->id;
96 while (id) {
97 next = id->next;
98 kfree(id);
99 id = next;
103 static void pnp_release_device(struct device *dmdev)
105 struct pnp_dev * dev = to_pnp_dev(dmdev);
106 pnp_free_option(dev->independent);
107 pnp_free_option(dev->dependent);
108 pnp_free_ids(dev);
109 kfree(dev);
112 int __pnp_add_device(struct pnp_dev *dev)
114 int ret;
115 pnp_fixup_device(dev);
116 dev->dev.bus = &pnp_bus_type;
117 dev->dev.release = &pnp_release_device;
118 dev->status = PNP_READY;
119 spin_lock(&pnp_lock);
120 list_add_tail(&dev->global_list, &pnp_global);
121 list_add_tail(&dev->protocol_list, &dev->protocol->devices);
122 spin_unlock(&pnp_lock);
124 ret = device_register(&dev->dev);
125 if (ret == 0)
126 pnp_interface_attach_device(dev);
127 return ret;
131 * pnp_add_device - adds a pnp device to the pnp layer
132 * @dev: pointer to dev to add
134 * adds to driver model, name database, fixups, interface, etc.
137 int pnp_add_device(struct pnp_dev *dev)
139 if (!dev || !dev->protocol || dev->card)
140 return -EINVAL;
141 dev->dev.parent = &dev->protocol->dev;
142 sprintf(dev->dev.bus_id, "%02x:%02x", dev->protocol->number, dev->number);
143 return __pnp_add_device(dev);
146 void __pnp_remove_device(struct pnp_dev *dev)
148 spin_lock(&pnp_lock);
149 list_del(&dev->global_list);
150 list_del(&dev->protocol_list);
151 spin_unlock(&pnp_lock);
152 device_unregister(&dev->dev);
156 * pnp_remove_device - removes a pnp device from the pnp layer
157 * @dev: pointer to dev to add
159 * this function will free all mem used by dev
161 #if 0
162 void pnp_remove_device(struct pnp_dev *dev)
164 if (!dev || dev->card)
165 return;
166 __pnp_remove_device(dev);
168 #endif /* 0 */
170 static int __init pnp_init(void)
172 printk(KERN_INFO "Linux Plug and Play Support v0.97 (c) Adam Belay\n");
173 return bus_register(&pnp_bus_type);
176 subsys_initcall(pnp_init);
178 #if 0
179 EXPORT_SYMBOL(pnp_register_protocol);
180 EXPORT_SYMBOL(pnp_unregister_protocol);
181 EXPORT_SYMBOL(pnp_add_device);
182 EXPORT_SYMBOL(pnp_remove_device);
183 #endif /* 0 */