Linux v2.6.13
[linux-2.6/next.git] / drivers / pnp / driver.c
blob1d037c2a82acb0a5bfbd8002ae0274128fb01ee3
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 static int count_devices(struct device * dev, void * c)
165 int * count = c;
166 (*count)++;
167 return 0;
170 int pnp_register_driver(struct pnp_driver *drv)
172 int count;
174 pnp_dbg("the driver '%s' has been registered", drv->name);
176 drv->driver.name = drv->name;
177 drv->driver.bus = &pnp_bus_type;
178 drv->driver.probe = pnp_device_probe;
179 drv->driver.remove = pnp_device_remove;
181 count = driver_register(&drv->driver);
183 /* get the number of initial matches */
184 if (count >= 0){
185 count = 0;
186 driver_for_each_device(&drv->driver, NULL, &count, count_devices);
188 return count;
191 void pnp_unregister_driver(struct pnp_driver *drv)
193 driver_unregister(&drv->driver);
194 pnp_dbg("the driver '%s' has been unregistered", drv->name);
198 * pnp_add_id - adds an EISA id to the specified device
199 * @id: pointer to a pnp_id structure
200 * @dev: pointer to the desired device
204 int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev)
206 struct pnp_id *ptr;
207 if (!id)
208 return -EINVAL;
209 if (!dev)
210 return -EINVAL;
211 id->next = NULL;
212 ptr = dev->id;
213 while (ptr && ptr->next)
214 ptr = ptr->next;
215 if (ptr)
216 ptr->next = id;
217 else
218 dev->id = id;
219 return 0;
222 EXPORT_SYMBOL(pnp_register_driver);
223 EXPORT_SYMBOL(pnp_unregister_driver);
224 EXPORT_SYMBOL(pnp_add_id);
225 EXPORT_SYMBOL(pnp_device_attach);
226 EXPORT_SYMBOL(pnp_device_detach);