blob: cc6757dfa3f13e5cbb8feb087d0f8fd80f7b1651 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * driver.c - device id matching, driver model, etc.
4 *
5 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/string.h>
9#include <linux/list.h>
10#include <linux/module.h>
11#include <linux/ctype.h>
12#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/pnp.h>
14#include "base.h"
15
16static int compare_func(const char *ida, const char *idb)
17{
18 int i;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 /* we only need to compare the last 4 chars */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070021 for (i = 3; i < 7; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 if (ida[i] != 'X' &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070023 idb[i] != 'X' && toupper(ida[i]) != toupper(idb[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 return 0;
25 }
26 return 1;
27}
28
29int compare_pnp_id(struct pnp_id *pos, const char *id)
30{
31 if (!pos || !id || (strlen(id) != 7))
32 return 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070033 if (memcmp(id, "ANYDEVS", 7) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return 1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070035 while (pos) {
36 if (memcmp(pos->id, id, 3) == 0)
37 if (compare_func(pos->id, id) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return 1;
39 pos = pos->next;
40 }
41 return 0;
42}
43
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070044static const struct pnp_device_id *match_device(struct pnp_driver *drv,
45 struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 const struct pnp_device_id *drv_id = drv->id_table;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 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
60int pnp_device_attach(struct pnp_dev *pnp_dev)
61{
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +010062 mutex_lock(&pnp_lock);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070063 if (pnp_dev->status != PNP_READY) {
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +010064 mutex_unlock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return -EBUSY;
66 }
67 pnp_dev->status = PNP_ATTACHED;
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +010068 mutex_unlock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return 0;
70}
Jinchao Wangd02908a2021-06-24 10:09:29 +080071EXPORT_SYMBOL(pnp_device_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73void pnp_device_detach(struct pnp_dev *pnp_dev)
74{
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +010075 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (pnp_dev->status == PNP_ATTACHED)
77 pnp_dev->status = PNP_READY;
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +010078 mutex_unlock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
Jinchao Wangd02908a2021-06-24 10:09:29 +080080EXPORT_SYMBOL(pnp_device_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 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)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700102 == PNP_DRIVER_RES_DISABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 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 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700113 if (error >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 pnp_dev->driver = pnp_drv;
115 error = 0;
116 } else
117 goto fail;
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return error;
120
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600121fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 pnp_device_detach(pnp_dev);
123 return error;
124}
125
Uwe Kleine-Königfc7a6202021-07-13 21:35:22 +0200126static void pnp_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700128 struct pnp_dev *pnp_dev = to_pnp_dev(dev);
129 struct pnp_driver *drv = pnp_dev->driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (drv) {
132 if (drv->remove)
133 drv->remove(pnp_dev);
134 pnp_dev->driver = NULL;
135 }
Heiner Kallweite0f03e82015-12-16 07:33:22 +0100136
137 if (pnp_dev->active &&
138 (!drv || !(drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)))
139 pnp_disable_dev(pnp_dev);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 pnp_device_detach(pnp_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
David Härdemanabd66332009-09-21 17:04:52 -0700144static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static int pnp_bus_match(struct device *dev, struct device_driver *drv)
154{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700155 struct pnp_dev *pnp_dev = to_pnp_dev(dev);
156 struct pnp_driver *pnp_drv = to_pnp_driver(drv);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (match_device(pnp_drv, pnp_dev) == NULL)
159 return 0;
160 return 1;
161}
162
Shuah Khaneaf140b2013-07-26 15:07:26 -0600163static int __pnp_bus_suspend(struct device *dev, pm_message_t state)
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100164{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700165 struct pnp_dev *pnp_dev = to_pnp_dev(dev);
166 struct pnp_driver *pnp_drv = pnp_dev->driver;
Pierre Ossman68094e32005-11-29 09:09:32 +0100167 int error;
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100168
Pierre Ossman68094e32005-11-29 09:09:32 +0100169 if (!pnp_drv)
170 return 0;
171
Shuah Khan729377d2013-09-11 14:23:09 -0700172 if (pnp_drv->driver.pm && pnp_drv->driver.pm->suspend) {
173 error = pnp_drv->driver.pm->suspend(dev);
174 suspend_report_result(pnp_drv->driver.pm->suspend, error);
175 if (error)
176 return error;
177 }
178
Pierre Ossman68094e32005-11-29 09:09:32 +0100179 if (pnp_drv->suspend) {
180 error = pnp_drv->suspend(pnp_dev, state);
181 if (error)
182 return error;
183 }
184
Rene Herman5d389982008-02-06 01:40:05 -0800185 if (pnp_can_disable(pnp_dev)) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700186 error = pnp_stop_dev(pnp_dev);
187 if (error)
188 return error;
Pierre Ossman68094e32005-11-29 09:09:32 +0100189 }
190
Peter Hurley01395d72015-01-22 11:50:24 -0500191 if (pnp_can_suspend(pnp_dev))
Shaohua Lifc30e682007-07-20 10:03:20 +0800192 pnp_dev->protocol->suspend(pnp_dev, state);
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100193 return 0;
194}
195
Shuah Khaneaf140b2013-07-26 15:07:26 -0600196static int pnp_bus_suspend(struct device *dev)
197{
198 return __pnp_bus_suspend(dev, PMSG_SUSPEND);
199}
200
201static int pnp_bus_freeze(struct device *dev)
202{
203 return __pnp_bus_suspend(dev, PMSG_FREEZE);
204}
205
Dmitry Torokhov8a37ea52013-12-05 02:01:55 +0100206static int pnp_bus_poweroff(struct device *dev)
207{
208 return __pnp_bus_suspend(dev, PMSG_HIBERNATE);
209}
210
Pierre Ossman68094e32005-11-29 09:09:32 +0100211static int pnp_bus_resume(struct device *dev)
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100212{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700213 struct pnp_dev *pnp_dev = to_pnp_dev(dev);
214 struct pnp_driver *pnp_drv = pnp_dev->driver;
Pierre Ossman68094e32005-11-29 09:09:32 +0100215 int error;
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100216
Pierre Ossman68094e32005-11-29 09:09:32 +0100217 if (!pnp_drv)
218 return 0;
219
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100220 if (pnp_dev->protocol->resume) {
221 error = pnp_dev->protocol->resume(pnp_dev);
222 if (error)
223 return error;
224 }
Shaohua Lifc30e682007-07-20 10:03:20 +0800225
Rene Herman5d389982008-02-06 01:40:05 -0800226 if (pnp_can_write(pnp_dev)) {
Pierre Ossman68094e32005-11-29 09:09:32 +0100227 error = pnp_start_dev(pnp_dev);
228 if (error)
229 return error;
230 }
231
Shuah Khan729377d2013-09-11 14:23:09 -0700232 if (pnp_drv->driver.pm && pnp_drv->driver.pm->resume) {
233 error = pnp_drv->driver.pm->resume(dev);
234 if (error)
235 return error;
236 }
237
Rene Herman5d389982008-02-06 01:40:05 -0800238 if (pnp_drv->resume) {
239 error = pnp_drv->resume(pnp_dev);
240 if (error)
241 return error;
242 }
Pierre Ossman68094e32005-11-29 09:09:32 +0100243
244 return 0;
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Shuah Khaneaf140b2013-07-26 15:07:26 -0600247static const struct dev_pm_ops pnp_bus_dev_pm_ops = {
Dmitry Torokhov8a37ea52013-12-05 02:01:55 +0100248 /* Suspend callbacks */
Shuah Khaneaf140b2013-07-26 15:07:26 -0600249 .suspend = pnp_bus_suspend,
Shuah Khaneaf140b2013-07-26 15:07:26 -0600250 .resume = pnp_bus_resume,
Dmitry Torokhov8a37ea52013-12-05 02:01:55 +0100251 /* Hibernate callbacks */
252 .freeze = pnp_bus_freeze,
253 .thaw = pnp_bus_resume,
254 .poweroff = pnp_bus_poweroff,
255 .restore = pnp_bus_resume,
Shuah Khaneaf140b2013-07-26 15:07:26 -0600256};
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258struct bus_type pnp_bus_type = {
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700259 .name = "pnp",
260 .match = pnp_bus_match,
261 .probe = pnp_device_probe,
262 .remove = pnp_device_remove,
David Härdemanabd66332009-09-21 17:04:52 -0700263 .shutdown = pnp_device_shutdown,
Shuah Khaneaf140b2013-07-26 15:07:26 -0600264 .pm = &pnp_bus_dev_pm_ops,
Greg Kroah-Hartman2df43902013-10-06 23:55:42 -0700265 .dev_groups = pnp_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268int pnp_register_driver(struct pnp_driver *drv)
269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 drv->driver.name = drv->name;
271 drv->driver.bus = &pnp_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800273 return driver_register(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
Jinchao Wangd02908a2021-06-24 10:09:29 +0800275EXPORT_SYMBOL(pnp_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277void pnp_unregister_driver(struct pnp_driver *drv)
278{
279 driver_unregister(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
Jinchao Wangd02908a2021-06-24 10:09:29 +0800281EXPORT_SYMBOL(pnp_unregister_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283/**
284 * pnp_add_id - adds an EISA id to the specified device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * @dev: pointer to the desired device
Bjorn Helgaas772defc2008-04-28 16:33:52 -0600286 * @id: pointer to an EISA id string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
Thomas Renninger620e1122010-10-01 10:54:00 +0200288struct pnp_id *pnp_add_id(struct pnp_dev *dev, const char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Bjorn Helgaas772defc2008-04-28 16:33:52 -0600290 struct pnp_id *dev_id, *ptr;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700291
Bjorn Helgaas772defc2008-04-28 16:33:52 -0600292 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
293 if (!dev_id)
294 return NULL;
295
296 dev_id->id[0] = id[0];
297 dev_id->id[1] = id[1];
298 dev_id->id[2] = id[2];
299 dev_id->id[3] = tolower(id[3]);
300 dev_id->id[4] = tolower(id[4]);
301 dev_id->id[5] = tolower(id[5]);
302 dev_id->id[6] = tolower(id[6]);
303 dev_id->id[7] = '\0';
304
305 dev_id->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 ptr = dev->id;
307 while (ptr && ptr->next)
308 ptr = ptr->next;
309 if (ptr)
Bjorn Helgaas772defc2008-04-28 16:33:52 -0600310 ptr->next = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 else
Bjorn Helgaas772defc2008-04-28 16:33:52 -0600312 dev->id = dev_id;
313
314 return dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}