aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pnp/driver.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/pnp/driver.c')
-rw-r--r--drivers/pnp/driver.c222
1 files changed, 222 insertions, 0 deletions
diff --git a/drivers/pnp/driver.c b/drivers/pnp/driver.c
new file mode 100644
index 000000000000..d64c1ca4fa76
--- /dev/null
+++ b/drivers/pnp/driver.c
@@ -0,0 +1,222 @@
1/*
2 * driver.c - device id matching, driver model, etc.
3 *
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
5 *
6 */
7
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>
14
15#ifdef CONFIG_PNP_DEBUG
16 #define DEBUG
17#else
18 #undef DEBUG
19#endif
20
21#include <linux/pnp.h>
22#include "base.h"
23
24static int compare_func(const char *ida, const char *idb)
25{
26 int i;
27 /* we only need to compare the last 4 chars */
28 for (i=3; i<7; i++)
29 {
30 if (ida[i] != 'X' &&
31 idb[i] != 'X' &&
32 toupper(ida[i]) != toupper(idb[i]))
33 return 0;
34 }
35 return 1;
36}
37
38int compare_pnp_id(struct pnp_id *pos, const char *id)
39{
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;
49 }
50 return 0;
51}
52
53static const struct pnp_device_id * match_device(struct pnp_driver *drv, struct pnp_dev *dev)
54{
55 const struct pnp_device_id *drv_id = drv->id_table;
56 if (!drv_id)
57 return NULL;
58
59 while (*drv_id->id) {
60 if (compare_pnp_id(dev->id, drv_id->id))
61 return drv_id;
62 drv_id++;
63 }
64 return NULL;
65}
66
67int pnp_device_attach(struct pnp_dev *pnp_dev)
68{
69 spin_lock(&pnp_lock);
70 if(pnp_dev->status != PNP_READY){
71 spin_unlock(&pnp_lock);
72 return -EBUSY;
73 }
74 pnp_dev->status = PNP_ATTACHED;
75 spin_unlock(&pnp_lock);
76 return 0;
77}
78
79void pnp_device_detach(struct pnp_dev *pnp_dev)
80{
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);
86}
87
88static int pnp_device_probe(struct device *dev)
89{
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);
96
97 pnp_dbg("match found with the PnP device '%s' and the driver '%s'", dev->bus_id,pnp_drv->name);
98
99 error = pnp_device_attach(pnp_dev);
100 if (error < 0)
101 return error;
102
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;
108 }
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;
114 }
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);
120 }
121 if (error >= 0){
122 pnp_dev->driver = pnp_drv;
123 error = 0;
124 } else
125 goto fail;
126 return error;
127
128fail:
129 pnp_device_detach(pnp_dev);
130 return error;
131}
132
133static int pnp_device_remove(struct device *dev)
134{
135 struct pnp_dev * pnp_dev = to_pnp_dev(dev);
136 struct pnp_driver * drv = pnp_dev->driver;
137
138 if (drv) {
139 if (drv->remove)
140 drv->remove(pnp_dev);
141 pnp_dev->driver = NULL;
142 }
143 pnp_device_detach(pnp_dev);
144 return 0;
145}
146
147static int pnp_bus_match(struct device *dev, struct device_driver *drv)
148{
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;
154}
155
156
157struct bus_type pnp_bus_type = {
158 .name = "pnp",
159 .match = pnp_bus_match,
160};
161
162
163int pnp_register_driver(struct pnp_driver *drv)
164{
165 int count;
166 struct list_head *pos;
167
168 pnp_dbg("the driver '%s' has been registered", drv->name);
169
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;
174
175 count = driver_register(&drv->driver);
176
177 /* get the number of initial matches */
178 if (count >= 0){
179 count = 0;
180 list_for_each(pos,&drv->driver.devices){
181 count++;
182 }
183 }
184 return count;
185}
186
187void pnp_unregister_driver(struct pnp_driver *drv)
188{
189 driver_unregister(&drv->driver);
190 pnp_dbg("the driver '%s' has been unregistered", drv->name);
191}
192
193/**
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
197 *
198 */
199
200int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev)
201{
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;
216}
217
218EXPORT_SYMBOL(pnp_register_driver);
219EXPORT_SYMBOL(pnp_unregister_driver);
220EXPORT_SYMBOL(pnp_add_id);
221EXPORT_SYMBOL(pnp_device_attach);
222EXPORT_SYMBOL(pnp_device_detach);