[NET] rename struct tcp_listen_opt to struct listen_sock
[linux-2.6.22.y-op.git] / drivers / pnp / driver.c
blobd64c1ca4fa76db7f77902f94b0baf840c78dc020
1 /*
2 * driver.c - device id matching, driver model, etc.
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
6 */
8 #include <linux/config.h>
9 #include <linux/string.h>
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/ctype.h>
13 #include <linux/slab.h>
15 #ifdef CONFIG_PNP_DEBUG
16 #define DEBUG
17 #else
18 #undef DEBUG
19 #endif
21 #include <linux/pnp.h>
22 #include "base.h"
24 static int compare_func(const char *ida, const char *idb)
26 int i;
27 /* we only need to compare the last 4 chars */
28 for (i=3; i<7; i++)
30 if (ida[i] != 'X' &&
31 idb[i] != 'X' &&
32 toupper(ida[i]) != toupper(idb[i]))
33 return 0;
35 return 1;
38 int compare_pnp_id(struct pnp_id *pos, const char *id)
40 if (!pos || !id || (strlen(id) != 7))
41 return 0;
42 if (memcmp(id,"ANYDEVS",7)==0)
43 return 1;
44 while (pos){
45 if (memcmp(pos->id,id,3)==0)
46 if (compare_func(pos->id,id)==1)
47 return 1;
48 pos = pos->next;
50 return 0;
53 static const struct pnp_device_id * match_device(struct pnp_driver *drv, struct pnp_dev *dev)
55 const struct pnp_device_id *drv_id = drv->id_table;
56 if (!drv_id)
57 return NULL;
59 while (*drv_id->id) {
60 if (compare_pnp_id(dev->id, drv_id->id))
61 return drv_id;
62 drv_id++;
64 return NULL;
67 int pnp_device_attach(struct pnp_dev *pnp_dev)
69 spin_lock(&pnp_lock);
70 if(pnp_dev->status != PNP_READY){
71 spin_unlock(&pnp_lock);
72 return -EBUSY;
74 pnp_dev->status = PNP_ATTACHED;
75 spin_unlock(&pnp_lock);
76 return 0;
79 void pnp_device_detach(struct pnp_dev *pnp_dev)
81 spin_lock(&pnp_lock);
82 if (pnp_dev->status == PNP_ATTACHED)
83 pnp_dev->status = PNP_READY;
84 spin_unlock(&pnp_lock);
85 pnp_disable_dev(pnp_dev);
88 static int pnp_device_probe(struct device *dev)
90 int error;
91 struct pnp_driver *pnp_drv;
92 struct pnp_dev *pnp_dev;
93 const struct pnp_device_id *dev_id = NULL;
94 pnp_dev = to_pnp_dev(dev);
95 pnp_drv = to_pnp_driver(dev->driver);
97 pnp_dbg("match found with the PnP device '%s' and the driver '%s'", dev->bus_id,pnp_drv->name);
99 error = pnp_device_attach(pnp_dev);
100 if (error < 0)
101 return error;
103 if (pnp_dev->active == 0) {
104 if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)) {
105 error = pnp_activate_dev(pnp_dev);
106 if (error < 0)
107 return error;
109 } else if ((pnp_drv->flags & PNP_DRIVER_RES_DISABLE)
110 == PNP_DRIVER_RES_DISABLE) {
111 error = pnp_disable_dev(pnp_dev);
112 if (error < 0)
113 return error;
115 error = 0;
116 if (pnp_drv->probe) {
117 dev_id = match_device(pnp_drv, pnp_dev);
118 if (dev_id != NULL)
119 error = pnp_drv->probe(pnp_dev, dev_id);
121 if (error >= 0){
122 pnp_dev->driver = pnp_drv;
123 error = 0;
124 } else
125 goto fail;
126 return error;
128 fail:
129 pnp_device_detach(pnp_dev);
130 return error;
133 static int pnp_device_remove(struct device *dev)
135 struct pnp_dev * pnp_dev = to_pnp_dev(dev);
136 struct pnp_driver * drv = pnp_dev->driver;
138 if (drv) {
139 if (drv->remove)
140 drv->remove(pnp_dev);
141 pnp_dev->driver = NULL;
143 pnp_device_detach(pnp_dev);
144 return 0;
147 static int pnp_bus_match(struct device *dev, struct device_driver *drv)
149 struct pnp_dev * pnp_dev = to_pnp_dev(dev);
150 struct pnp_driver * pnp_drv = to_pnp_driver(drv);
151 if (match_device(pnp_drv, pnp_dev) == NULL)
152 return 0;
153 return 1;
157 struct bus_type pnp_bus_type = {
158 .name = "pnp",
159 .match = pnp_bus_match,
163 int pnp_register_driver(struct pnp_driver *drv)
165 int count;
166 struct list_head *pos;
168 pnp_dbg("the driver '%s' has been registered", drv->name);
170 drv->driver.name = drv->name;
171 drv->driver.bus = &pnp_bus_type;
172 drv->driver.probe = pnp_device_probe;
173 drv->driver.remove = pnp_device_remove;
175 count = driver_register(&drv->driver);
177 /* get the number of initial matches */
178 if (count >= 0){
179 count = 0;
180 list_for_each(pos,&drv->driver.devices){
181 count++;
184 return count;
187 void pnp_unregister_driver(struct pnp_driver *drv)
189 driver_unregister(&drv->driver);
190 pnp_dbg("the driver '%s' has been unregistered", drv->name);
194 * pnp_add_id - adds an EISA id to the specified device
195 * @id: pointer to a pnp_id structure
196 * @dev: pointer to the desired device
200 int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev)
202 struct pnp_id *ptr;
203 if (!id)
204 return -EINVAL;
205 if (!dev)
206 return -EINVAL;
207 id->next = NULL;
208 ptr = dev->id;
209 while (ptr && ptr->next)
210 ptr = ptr->next;
211 if (ptr)
212 ptr->next = id;
213 else
214 dev->id = id;
215 return 0;
218 EXPORT_SYMBOL(pnp_register_driver);
219 EXPORT_SYMBOL(pnp_unregister_driver);
220 EXPORT_SYMBOL(pnp_add_id);
221 EXPORT_SYMBOL(pnp_device_attach);
222 EXPORT_SYMBOL(pnp_device_detach);