Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * driver.c - device id matching, driver model, etc. | |
4 | * | |
5 | * Copyright 2002 Adam Belay <ambx1@neo.rr.com> | |
1da177e4 LT |
6 | */ |
7 | ||
1da177e4 LT |
8 | #include <linux/string.h> |
9 | #include <linux/list.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/ctype.h> | |
12 | #include <linux/slab.h> | |
1da177e4 LT |
13 | #include <linux/pnp.h> |
14 | #include "base.h" | |
15 | ||
16 | static int compare_func(const char *ida, const char *idb) | |
17 | { | |
18 | int i; | |
07d4e9af | 19 | |
1da177e4 | 20 | /* we only need to compare the last 4 chars */ |
9dd78466 | 21 | for (i = 3; i < 7; i++) { |
1da177e4 | 22 | if (ida[i] != 'X' && |
9dd78466 | 23 | idb[i] != 'X' && toupper(ida[i]) != toupper(idb[i])) |
1da177e4 LT |
24 | return 0; |
25 | } | |
26 | return 1; | |
27 | } | |
28 | ||
29 | int compare_pnp_id(struct pnp_id *pos, const char *id) | |
30 | { | |
31 | if (!pos || !id || (strlen(id) != 7)) | |
32 | return 0; | |
9dd78466 | 33 | if (memcmp(id, "ANYDEVS", 7) == 0) |
1da177e4 | 34 | return 1; |
9dd78466 BH |
35 | while (pos) { |
36 | if (memcmp(pos->id, id, 3) == 0) | |
37 | if (compare_func(pos->id, id) == 1) | |
1da177e4 LT |
38 | return 1; |
39 | pos = pos->next; | |
40 | } | |
41 | return 0; | |
42 | } | |
43 | ||
d69d8048 | 44 | static const struct pnp_device_id *match_device(const struct pnp_driver *drv, |
9dd78466 | 45 | struct pnp_dev *dev) |
1da177e4 LT |
46 | { |
47 | const struct pnp_device_id *drv_id = drv->id_table; | |
07d4e9af | 48 | |
1da177e4 LT |
49 | if (!drv_id) |
50 | return NULL; | |
51 | ||
52 | while (*drv_id->id) { | |
53 | if (compare_pnp_id(dev->id, drv_id->id)) | |
54 | return drv_id; | |
55 | drv_id++; | |
56 | } | |
57 | return NULL; | |
58 | } | |
59 | ||
60 | int pnp_device_attach(struct pnp_dev *pnp_dev) | |
61 | { | |
38f6b38d | 62 | mutex_lock(&pnp_lock); |
9dd78466 | 63 | if (pnp_dev->status != PNP_READY) { |
38f6b38d | 64 | mutex_unlock(&pnp_lock); |
1da177e4 LT |
65 | return -EBUSY; |
66 | } | |
67 | pnp_dev->status = PNP_ATTACHED; | |
38f6b38d | 68 | mutex_unlock(&pnp_lock); |
1da177e4 LT |
69 | return 0; |
70 | } | |
d02908ad | 71 | EXPORT_SYMBOL(pnp_device_attach); |
1da177e4 LT |
72 | |
73 | void pnp_device_detach(struct pnp_dev *pnp_dev) | |
74 | { | |
38f6b38d | 75 | mutex_lock(&pnp_lock); |
1da177e4 LT |
76 | if (pnp_dev->status == PNP_ATTACHED) |
77 | pnp_dev->status = PNP_READY; | |
38f6b38d | 78 | mutex_unlock(&pnp_lock); |
1da177e4 | 79 | } |
d02908ad | 80 | EXPORT_SYMBOL(pnp_device_detach); |
1da177e4 LT |
81 | |
82 | static int pnp_device_probe(struct device *dev) | |
83 | { | |
84 | int error; | |
85 | struct pnp_driver *pnp_drv; | |
86 | struct pnp_dev *pnp_dev; | |
87 | const struct pnp_device_id *dev_id = NULL; | |
88 | pnp_dev = to_pnp_dev(dev); | |
89 | pnp_drv = to_pnp_driver(dev->driver); | |
90 | ||
1da177e4 LT |
91 | error = pnp_device_attach(pnp_dev); |
92 | if (error < 0) | |
93 | return error; | |
94 | ||
95 | if (pnp_dev->active == 0) { | |
96 | if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)) { | |
97 | error = pnp_activate_dev(pnp_dev); | |
98 | if (error < 0) | |
99 | return error; | |
100 | } | |
101 | } else if ((pnp_drv->flags & PNP_DRIVER_RES_DISABLE) | |
9dd78466 | 102 | == PNP_DRIVER_RES_DISABLE) { |
1da177e4 LT |
103 | error = pnp_disable_dev(pnp_dev); |
104 | if (error < 0) | |
105 | return error; | |
106 | } | |
107 | error = 0; | |
108 | if (pnp_drv->probe) { | |
109 | dev_id = match_device(pnp_drv, pnp_dev); | |
110 | if (dev_id != NULL) | |
111 | error = pnp_drv->probe(pnp_dev, dev_id); | |
112 | } | |
9dd78466 | 113 | if (error >= 0) { |
1da177e4 LT |
114 | pnp_dev->driver = pnp_drv; |
115 | error = 0; | |
116 | } else | |
117 | goto fail; | |
a05d0781 | 118 | |
1da177e4 LT |
119 | return error; |
120 | ||
1e0aa9ad | 121 | fail: |
1da177e4 LT |
122 | pnp_device_detach(pnp_dev); |
123 | return error; | |
124 | } | |
125 | ||
fc7a6209 | 126 | static void pnp_device_remove(struct device *dev) |
1da177e4 | 127 | { |
9dd78466 BH |
128 | struct pnp_dev *pnp_dev = to_pnp_dev(dev); |
129 | struct pnp_driver *drv = pnp_dev->driver; | |
1da177e4 LT |
130 | |
131 | if (drv) { | |
132 | if (drv->remove) | |
133 | drv->remove(pnp_dev); | |
134 | pnp_dev->driver = NULL; | |
135 | } | |
e0f03e87 HK |
136 | |
137 | if (pnp_dev->active && | |
138 | (!drv || !(drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE))) | |
139 | pnp_disable_dev(pnp_dev); | |
140 | ||
1da177e4 | 141 | pnp_device_detach(pnp_dev); |
1da177e4 LT |
142 | } |
143 | ||
abd6633c DH |
144 | static void pnp_device_shutdown(struct device *dev) |
145 | { | |
146 | struct pnp_dev *pnp_dev = to_pnp_dev(dev); | |
147 | struct pnp_driver *drv = pnp_dev->driver; | |
148 | ||
149 | if (drv && drv->shutdown) | |
150 | drv->shutdown(pnp_dev); | |
151 | } | |
152 | ||
d69d8048 | 153 | static int pnp_bus_match(struct device *dev, const struct device_driver *drv) |
1da177e4 | 154 | { |
9dd78466 | 155 | struct pnp_dev *pnp_dev = to_pnp_dev(dev); |
d69d8048 | 156 | const struct pnp_driver *pnp_drv = to_pnp_driver(drv); |
07d4e9af | 157 | |
1da177e4 LT |
158 | if (match_device(pnp_drv, pnp_dev) == NULL) |
159 | return 0; | |
160 | return 1; | |
161 | } | |
162 | ||
eaf140b6 | 163 | static int __pnp_bus_suspend(struct device *dev, pm_message_t state) |
4c98cfef | 164 | { |
9dd78466 BH |
165 | struct pnp_dev *pnp_dev = to_pnp_dev(dev); |
166 | struct pnp_driver *pnp_drv = pnp_dev->driver; | |
68094e32 PO |
167 | int error; |
168 | ||
169 | if (!pnp_drv) | |
170 | return 0; | |
171 | ||
729377d5 SK |
172 | if (pnp_drv->driver.pm && pnp_drv->driver.pm->suspend) { |
173 | error = pnp_drv->driver.pm->suspend(dev); | |
a759de69 | 174 | suspend_report_result(dev, pnp_drv->driver.pm->suspend, error); |
729377d5 SK |
175 | if (error) |
176 | return error; | |
177 | } | |
178 | ||
68094e32 PO |
179 | if (pnp_drv->suspend) { |
180 | error = pnp_drv->suspend(pnp_dev, state); | |
181 | if (error) | |
182 | return error; | |
183 | } | |
184 | ||
62a0ec9d HG |
185 | /* can_write is necessary to be able to re-start the device on resume */ |
186 | if (pnp_can_disable(pnp_dev) && pnp_can_write(pnp_dev)) { | |
9dd78466 BH |
187 | error = pnp_stop_dev(pnp_dev); |
188 | if (error) | |
189 | return error; | |
68094e32 | 190 | } |
4c98cfef | 191 | |
01395d79 | 192 | if (pnp_can_suspend(pnp_dev)) |
fc30e68e | 193 | pnp_dev->protocol->suspend(pnp_dev, state); |
4c98cfef TI |
194 | return 0; |
195 | } | |
196 | ||
eaf140b6 SK |
197 | static int pnp_bus_suspend(struct device *dev) |
198 | { | |
199 | return __pnp_bus_suspend(dev, PMSG_SUSPEND); | |
200 | } | |
201 | ||
202 | static int pnp_bus_freeze(struct device *dev) | |
203 | { | |
204 | return __pnp_bus_suspend(dev, PMSG_FREEZE); | |
205 | } | |
206 | ||
8a37ea50 DT |
207 | static int pnp_bus_poweroff(struct device *dev) |
208 | { | |
209 | return __pnp_bus_suspend(dev, PMSG_HIBERNATE); | |
210 | } | |
211 | ||
68094e32 | 212 | static int pnp_bus_resume(struct device *dev) |
4c98cfef | 213 | { |
9dd78466 BH |
214 | struct pnp_dev *pnp_dev = to_pnp_dev(dev); |
215 | struct pnp_driver *pnp_drv = pnp_dev->driver; | |
68094e32 PO |
216 | int error; |
217 | ||
218 | if (!pnp_drv) | |
219 | return 0; | |
220 | ||
cc8e7a35 RW |
221 | if (pnp_dev->protocol->resume) { |
222 | error = pnp_dev->protocol->resume(pnp_dev); | |
223 | if (error) | |
224 | return error; | |
225 | } | |
fc30e68e | 226 | |
5d38998e | 227 | if (pnp_can_write(pnp_dev)) { |
68094e32 PO |
228 | error = pnp_start_dev(pnp_dev); |
229 | if (error) | |
230 | return error; | |
231 | } | |
4c98cfef | 232 | |
729377d5 SK |
233 | if (pnp_drv->driver.pm && pnp_drv->driver.pm->resume) { |
234 | error = pnp_drv->driver.pm->resume(dev); | |
235 | if (error) | |
236 | return error; | |
237 | } | |
238 | ||
5d38998e RH |
239 | if (pnp_drv->resume) { |
240 | error = pnp_drv->resume(pnp_dev); | |
241 | if (error) | |
242 | return error; | |
243 | } | |
68094e32 PO |
244 | |
245 | return 0; | |
4c98cfef | 246 | } |
1da177e4 | 247 | |
eaf140b6 | 248 | static const struct dev_pm_ops pnp_bus_dev_pm_ops = { |
8a37ea50 | 249 | /* Suspend callbacks */ |
eaf140b6 | 250 | .suspend = pnp_bus_suspend, |
eaf140b6 | 251 | .resume = pnp_bus_resume, |
8a37ea50 DT |
252 | /* Hibernate callbacks */ |
253 | .freeze = pnp_bus_freeze, | |
254 | .thaw = pnp_bus_resume, | |
255 | .poweroff = pnp_bus_poweroff, | |
256 | .restore = pnp_bus_resume, | |
eaf140b6 SK |
257 | }; |
258 | ||
2fb7e4dd | 259 | const struct bus_type pnp_bus_type = { |
07d4e9af BH |
260 | .name = "pnp", |
261 | .match = pnp_bus_match, | |
262 | .probe = pnp_device_probe, | |
263 | .remove = pnp_device_remove, | |
abd6633c | 264 | .shutdown = pnp_device_shutdown, |
eaf140b6 | 265 | .pm = &pnp_bus_dev_pm_ops, |
2df43901 | 266 | .dev_groups = pnp_dev_groups, |
1da177e4 LT |
267 | }; |
268 | ||
c7a50967 AS |
269 | bool dev_is_pnp(const struct device *dev) |
270 | { | |
271 | return dev->bus == &pnp_bus_type; | |
272 | } | |
273 | EXPORT_SYMBOL_GPL(dev_is_pnp); | |
274 | ||
1da177e4 LT |
275 | int pnp_register_driver(struct pnp_driver *drv) |
276 | { | |
1da177e4 LT |
277 | drv->driver.name = drv->name; |
278 | drv->driver.bus = &pnp_bus_type; | |
1da177e4 | 279 | |
982c6094 | 280 | return driver_register(&drv->driver); |
1da177e4 | 281 | } |
d02908ad | 282 | EXPORT_SYMBOL(pnp_register_driver); |
1da177e4 LT |
283 | |
284 | void pnp_unregister_driver(struct pnp_driver *drv) | |
285 | { | |
286 | driver_unregister(&drv->driver); | |
1da177e4 | 287 | } |
d02908ad | 288 | EXPORT_SYMBOL(pnp_unregister_driver); |
1da177e4 LT |
289 | |
290 | /** | |
291 | * pnp_add_id - adds an EISA id to the specified device | |
1da177e4 | 292 | * @dev: pointer to the desired device |
772defc6 | 293 | * @id: pointer to an EISA id string |
1da177e4 | 294 | */ |
620e112c | 295 | struct pnp_id *pnp_add_id(struct pnp_dev *dev, const char *id) |
1da177e4 | 296 | { |
772defc6 | 297 | struct pnp_id *dev_id, *ptr; |
07d4e9af | 298 | |
772defc6 BH |
299 | dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL); |
300 | if (!dev_id) | |
301 | return NULL; | |
302 | ||
303 | dev_id->id[0] = id[0]; | |
304 | dev_id->id[1] = id[1]; | |
305 | dev_id->id[2] = id[2]; | |
306 | dev_id->id[3] = tolower(id[3]); | |
307 | dev_id->id[4] = tolower(id[4]); | |
308 | dev_id->id[5] = tolower(id[5]); | |
309 | dev_id->id[6] = tolower(id[6]); | |
310 | dev_id->id[7] = '\0'; | |
311 | ||
312 | dev_id->next = NULL; | |
1da177e4 LT |
313 | ptr = dev->id; |
314 | while (ptr && ptr->next) | |
315 | ptr = ptr->next; | |
316 | if (ptr) | |
772defc6 | 317 | ptr->next = dev_id; |
1da177e4 | 318 | else |
772defc6 BH |
319 | dev->id = dev_id; |
320 | ||
321 | return dev_id; | |
1da177e4 | 322 | } |