1 // SPDX-License-Identifier: GPL-2.0
3 * driver.c - device id matching, driver model, etc.
5 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
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>
13 #include <linux/pnp.h>
16 static int compare_func(const char *ida
, const char *idb
)
20 /* we only need to compare the last 4 chars */
21 for (i
= 3; i
< 7; i
++) {
23 idb
[i
] != 'X' && toupper(ida
[i
]) != toupper(idb
[i
]))
29 int compare_pnp_id(struct pnp_id
*pos
, const char *id
)
31 if (!pos
|| !id
|| (strlen(id
) != 7))
33 if (memcmp(id
, "ANYDEVS", 7) == 0)
36 if (memcmp(pos
->id
, id
, 3) == 0)
37 if (compare_func(pos
->id
, id
) == 1)
44 static const struct pnp_device_id
*match_device(const struct pnp_driver
*drv
,
47 const struct pnp_device_id
*drv_id
= drv
->id_table
;
53 if (compare_pnp_id(dev
->id
, drv_id
->id
))
60 int pnp_device_attach(struct pnp_dev
*pnp_dev
)
62 mutex_lock(&pnp_lock
);
63 if (pnp_dev
->status
!= PNP_READY
) {
64 mutex_unlock(&pnp_lock
);
67 pnp_dev
->status
= PNP_ATTACHED
;
68 mutex_unlock(&pnp_lock
);
71 EXPORT_SYMBOL(pnp_device_attach
);
73 void pnp_device_detach(struct pnp_dev
*pnp_dev
)
75 mutex_lock(&pnp_lock
);
76 if (pnp_dev
->status
== PNP_ATTACHED
)
77 pnp_dev
->status
= PNP_READY
;
78 mutex_unlock(&pnp_lock
);
80 EXPORT_SYMBOL(pnp_device_detach
);
82 static int pnp_device_probe(struct device
*dev
)
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
);
91 error
= pnp_device_attach(pnp_dev
);
95 if (pnp_dev
->active
== 0) {
96 if (!(pnp_drv
->flags
& PNP_DRIVER_RES_DO_NOT_CHANGE
)) {
97 error
= pnp_activate_dev(pnp_dev
);
101 } else if ((pnp_drv
->flags
& PNP_DRIVER_RES_DISABLE
)
102 == PNP_DRIVER_RES_DISABLE
) {
103 error
= pnp_disable_dev(pnp_dev
);
108 if (pnp_drv
->probe
) {
109 dev_id
= match_device(pnp_drv
, pnp_dev
);
111 error
= pnp_drv
->probe(pnp_dev
, dev_id
);
114 pnp_dev
->driver
= pnp_drv
;
122 pnp_device_detach(pnp_dev
);
126 static void pnp_device_remove(struct device
*dev
)
128 struct pnp_dev
*pnp_dev
= to_pnp_dev(dev
);
129 struct pnp_driver
*drv
= pnp_dev
->driver
;
133 drv
->remove(pnp_dev
);
134 pnp_dev
->driver
= NULL
;
137 if (pnp_dev
->active
&&
138 (!drv
|| !(drv
->flags
& PNP_DRIVER_RES_DO_NOT_CHANGE
)))
139 pnp_disable_dev(pnp_dev
);
141 pnp_device_detach(pnp_dev
);
144 static void pnp_device_shutdown(struct device
*dev
)
146 struct pnp_dev
*pnp_dev
= to_pnp_dev(dev
);
147 struct pnp_driver
*drv
= pnp_dev
->driver
;
149 if (drv
&& drv
->shutdown
)
150 drv
->shutdown(pnp_dev
);
153 static int pnp_bus_match(struct device
*dev
, const struct device_driver
*drv
)
155 struct pnp_dev
*pnp_dev
= to_pnp_dev(dev
);
156 const struct pnp_driver
*pnp_drv
= to_pnp_driver(drv
);
158 if (match_device(pnp_drv
, pnp_dev
) == NULL
)
163 static int __pnp_bus_suspend(struct device
*dev
, pm_message_t state
)
165 struct pnp_dev
*pnp_dev
= to_pnp_dev(dev
);
166 struct pnp_driver
*pnp_drv
= pnp_dev
->driver
;
172 if (pnp_drv
->driver
.pm
&& pnp_drv
->driver
.pm
->suspend
) {
173 error
= pnp_drv
->driver
.pm
->suspend(dev
);
174 suspend_report_result(dev
, pnp_drv
->driver
.pm
->suspend
, error
);
179 if (pnp_drv
->suspend
) {
180 error
= pnp_drv
->suspend(pnp_dev
, state
);
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
)) {
187 error
= pnp_stop_dev(pnp_dev
);
192 if (pnp_can_suspend(pnp_dev
))
193 pnp_dev
->protocol
->suspend(pnp_dev
, state
);
197 static int pnp_bus_suspend(struct device
*dev
)
199 return __pnp_bus_suspend(dev
, PMSG_SUSPEND
);
202 static int pnp_bus_freeze(struct device
*dev
)
204 return __pnp_bus_suspend(dev
, PMSG_FREEZE
);
207 static int pnp_bus_poweroff(struct device
*dev
)
209 return __pnp_bus_suspend(dev
, PMSG_HIBERNATE
);
212 static int pnp_bus_resume(struct device
*dev
)
214 struct pnp_dev
*pnp_dev
= to_pnp_dev(dev
);
215 struct pnp_driver
*pnp_drv
= pnp_dev
->driver
;
221 if (pnp_dev
->protocol
->resume
) {
222 error
= pnp_dev
->protocol
->resume(pnp_dev
);
227 if (pnp_can_write(pnp_dev
)) {
228 error
= pnp_start_dev(pnp_dev
);
233 if (pnp_drv
->driver
.pm
&& pnp_drv
->driver
.pm
->resume
) {
234 error
= pnp_drv
->driver
.pm
->resume(dev
);
239 if (pnp_drv
->resume
) {
240 error
= pnp_drv
->resume(pnp_dev
);
248 static const struct dev_pm_ops pnp_bus_dev_pm_ops
= {
249 /* Suspend callbacks */
250 .suspend
= pnp_bus_suspend
,
251 .resume
= pnp_bus_resume
,
252 /* Hibernate callbacks */
253 .freeze
= pnp_bus_freeze
,
254 .thaw
= pnp_bus_resume
,
255 .poweroff
= pnp_bus_poweroff
,
256 .restore
= pnp_bus_resume
,
259 const struct bus_type pnp_bus_type
= {
261 .match
= pnp_bus_match
,
262 .probe
= pnp_device_probe
,
263 .remove
= pnp_device_remove
,
264 .shutdown
= pnp_device_shutdown
,
265 .pm
= &pnp_bus_dev_pm_ops
,
266 .dev_groups
= pnp_dev_groups
,
269 bool dev_is_pnp(const struct device
*dev
)
271 return dev
->bus
== &pnp_bus_type
;
273 EXPORT_SYMBOL_GPL(dev_is_pnp
);
275 int pnp_register_driver(struct pnp_driver
*drv
)
277 drv
->driver
.name
= drv
->name
;
278 drv
->driver
.bus
= &pnp_bus_type
;
280 return driver_register(&drv
->driver
);
282 EXPORT_SYMBOL(pnp_register_driver
);
284 void pnp_unregister_driver(struct pnp_driver
*drv
)
286 driver_unregister(&drv
->driver
);
288 EXPORT_SYMBOL(pnp_unregister_driver
);
291 * pnp_add_id - adds an EISA id to the specified device
292 * @dev: pointer to the desired device
293 * @id: pointer to an EISA id string
295 struct pnp_id
*pnp_add_id(struct pnp_dev
*dev
, const char *id
)
297 struct pnp_id
*dev_id
, *ptr
;
299 dev_id
= kzalloc(sizeof(struct pnp_id
), GFP_KERNEL
);
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';
314 while (ptr
&& ptr
->next
)