blob: 9610a9f08ff4d67f18049f8b58c003f715940238 [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 * card.c - contains functions for managing groups of PnP devices
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/module.h>
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +01009#include <linux/mutex.h>
Bjorn Helgaase4366752008-04-28 16:33:56 -060010#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/pnp.h>
Rene Hermane86b19c2008-07-25 19:44:42 -070013#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "base.h"
15
16LIST_HEAD(pnp_cards);
Adrian Bunkb449f632005-11-07 01:01:48 -080017static LIST_HEAD(pnp_card_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070019static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
20 struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070022 const struct pnp_card_device_id *drv_id = drv->id_table;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070023
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070024 while (*drv_id->id) {
25 if (compare_pnp_id(card->id, drv_id->id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int i = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 for (;;) {
29 int found;
30 struct pnp_dev *dev;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070031
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -060032 if (i == PNP_MAX_DEVICES ||
33 !*drv_id->devs[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return drv_id;
35 found = 0;
36 card_for_each_dev(card, dev) {
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -060037 if (compare_pnp_id(dev->id,
38 drv_id->devs[i].id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 found = 1;
40 break;
41 }
42 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070043 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 break;
45 i++;
46 }
47 }
48 drv_id++;
49 }
50 return NULL;
51}
52
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070053static void card_remove(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 dev->card_link = NULL;
56}
Bjorn Helgaas982c6092006-03-27 01:17:08 -080057
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070058static void card_remove_first(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070060 struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (!dev->card || !drv)
63 return;
64 if (drv->remove)
65 drv->remove(dev->card_link);
66 drv->link.remove = &card_remove;
67 kfree(dev->card_link);
68 card_remove(dev);
69}
70
Jesper Juhla9adb8d2006-06-25 05:47:17 -070071static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Jesper Juhla9adb8d2006-06-25 05:47:17 -070073 const struct pnp_card_device_id *id;
74 struct pnp_card_link *clink;
75 struct pnp_dev *dev;
76
77 if (!drv->probe)
78 return 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070079 id = match_card(drv, card);
Jesper Juhla9adb8d2006-06-25 05:47:17 -070080 if (!id)
81 return 0;
82
Heiner Kallweitb15fc7c2021-05-12 22:36:12 +020083 clink = kzalloc(sizeof(*clink), GFP_KERNEL);
Jesper Juhla9adb8d2006-06-25 05:47:17 -070084 if (!clink)
85 return 0;
86 clink->card = card;
87 clink->driver = drv;
88 clink->pm_state = PMSG_ON;
89
90 if (drv->probe(clink, id) >= 0)
91 return 1;
92
93 /* Recovery */
94 card_for_each_dev(card, dev) {
95 if (dev->card_link == clink)
96 pnp_release_card_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Jesper Juhla9adb8d2006-06-25 05:47:17 -070098 kfree(clink);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
100}
101
102/**
103 * pnp_add_card_id - adds an EISA id to the specified card
104 * @id: pointer to a pnp_id structure
105 * @card: pointer to the desired card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
Adrian Bunk25cdcd02008-07-25 19:46:21 -0700107static struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Bjorn Helgaase4366752008-04-28 16:33:56 -0600109 struct pnp_id *dev_id, *ptr;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700110
Bjorn Helgaase4366752008-04-28 16:33:56 -0600111 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
112 if (!dev_id)
113 return NULL;
114
115 dev_id->id[0] = id[0];
116 dev_id->id[1] = id[1];
117 dev_id->id[2] = id[2];
118 dev_id->id[3] = tolower(id[3]);
119 dev_id->id[4] = tolower(id[4]);
120 dev_id->id[5] = tolower(id[5]);
121 dev_id->id[6] = tolower(id[6]);
122 dev_id->id[7] = '\0';
123
124 dev_id->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 ptr = card->id;
126 while (ptr && ptr->next)
127 ptr = ptr->next;
128 if (ptr)
Bjorn Helgaase4366752008-04-28 16:33:56 -0600129 ptr->next = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 else
Bjorn Helgaase4366752008-04-28 16:33:56 -0600131 card->id = dev_id;
132
133 return dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700136static void pnp_free_card_ids(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700138 struct pnp_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct pnp_id *next;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 id = card->id;
142 while (id) {
143 next = id->next;
144 kfree(id);
145 id = next;
146 }
147}
148
149static void pnp_release_card(struct device *dmdev)
150{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700151 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 pnp_free_card_ids(card);
154 kfree(card);
155}
156
Bjorn Helgaas6bf2aab2008-04-28 16:33:58 -0600157struct pnp_card *pnp_alloc_card(struct pnp_protocol *protocol, int id, char *pnpid)
158{
159 struct pnp_card *card;
160 struct pnp_id *dev_id;
161
162 card = kzalloc(sizeof(struct pnp_card), GFP_KERNEL);
163 if (!card)
164 return NULL;
165
166 card->protocol = protocol;
167 card->number = id;
168
169 card->dev.parent = &card->protocol->dev;
Kay Sieversc85e37c2009-01-06 10:44:38 -0800170 dev_set_name(&card->dev, "%02x:%02x", card->protocol->number, card->number);
Bjorn Helgaas6bf2aab2008-04-28 16:33:58 -0600171
Yang Hongyang2f4f27d2009-04-06 19:01:18 -0700172 card->dev.coherent_dma_mask = DMA_BIT_MASK(24);
Rene Hermane86b19c2008-07-25 19:44:42 -0700173 card->dev.dma_mask = &card->dev.coherent_dma_mask;
174
Bjorn Helgaas6bf2aab2008-04-28 16:33:58 -0600175 dev_id = pnp_add_card_id(card, pnpid);
176 if (!dev_id) {
177 kfree(card);
178 return NULL;
179 }
180
181 return card;
182}
183
Zhen Lei39357872021-06-02 16:15:46 +0800184static ssize_t name_show(struct device *dmdev,
185 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 char *str = buf;
188 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700189
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700190 str += sprintf(str, "%s\n", card->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return (str - buf);
192}
193
Zhen Lei39357872021-06-02 16:15:46 +0800194static DEVICE_ATTR_RO(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Zhen Lei39357872021-06-02 16:15:46 +0800196static ssize_t card_id_show(struct device *dmdev,
197 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 char *str = buf;
200 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700201 struct pnp_id *pos = card->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 while (pos) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700204 str += sprintf(str, "%s\n", pos->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 pos = pos->next;
206 }
207 return (str - buf);
208}
209
Zhen Lei39357872021-06-02 16:15:46 +0800210static DEVICE_ATTR_RO(card_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212static int pnp_interface_attach_card(struct pnp_card *card)
213{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700214 int rc = device_create_file(&card->dev, &dev_attr_name);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700215
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700216 if (rc)
217 return rc;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800218
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700219 rc = device_create_file(&card->dev, &dev_attr_card_id);
220 if (rc)
221 goto err_name;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800224
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600225err_name:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700226 device_remove_file(&card->dev, &dev_attr_name);
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800227 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230/**
231 * pnp_add_card - adds a PnP card to the PnP Layer
232 * @card: pointer to the card to add
233 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700234int pnp_add_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int error;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700237 struct list_head *pos, *temp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 card->dev.bus = NULL;
240 card->dev.release = &pnp_release_card;
241 error = device_register(&card->dev);
Bjorn Helgaas5bfc43a02007-10-16 23:31:09 -0700242 if (error) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700243 dev_err(&card->dev, "could not register (err=%d)\n", error);
Levente Kurusa75365d02013-12-19 16:03:36 +0100244 put_device(&card->dev);
Bjorn Helgaas5bfc43a02007-10-16 23:31:09 -0700245 return error;
246 }
247
248 pnp_interface_attach_card(card);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100249 mutex_lock(&pnp_lock);
Bjorn Helgaas5bfc43a02007-10-16 23:31:09 -0700250 list_add_tail(&card->global_list, &pnp_cards);
251 list_add_tail(&card->protocol_list, &card->protocol->cards);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100252 mutex_unlock(&pnp_lock);
Bjorn Helgaas5bfc43a02007-10-16 23:31:09 -0700253
254 /* we wait until now to add devices in order to ensure the drivers
255 * will be able to use all of the related devices on the card
256 * without waiting an unreasonable length of time */
257 list_for_each(pos, &card->devices) {
258 struct pnp_dev *dev = card_to_pnp_dev(pos);
259 __pnp_add_device(dev);
260 }
261
262 /* match with card drivers */
263 list_for_each_safe(pos, temp, &pnp_card_drivers) {
264 struct pnp_card_driver *drv =
265 list_entry(pos, struct pnp_card_driver,
266 global_list);
267 card_probe(card, drv);
268 }
269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/**
273 * pnp_remove_card - removes a PnP card from the PnP Layer
274 * @card: pointer to the card to remove
275 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700276void pnp_remove_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct list_head *pos, *temp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 device_unregister(&card->dev);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100281 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 list_del(&card->global_list);
283 list_del(&card->protocol_list);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100284 mutex_unlock(&pnp_lock);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700285 list_for_each_safe(pos, temp, &card->devices) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct pnp_dev *dev = card_to_pnp_dev(pos);
287 pnp_remove_card_device(dev);
288 }
289}
290
291/**
292 * pnp_add_card_device - adds a device to the specified card
293 * @card: pointer to the card to add to
294 * @dev: pointer to the device to add
295 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700296int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 dev->dev.parent = &card->dev;
299 dev->card_link = NULL;
Kay Sieversc85e37c2009-01-06 10:44:38 -0800300 dev_set_name(&dev->dev, "%02x:%02x.%02x",
301 dev->protocol->number, card->number, dev->number);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100302 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 dev->card = card;
304 list_add_tail(&dev->card_list, &card->devices);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100305 mutex_unlock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
307}
308
309/**
310 * pnp_remove_card_device- removes a device from the specified card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * @dev: pointer to the device to remove
312 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700313void pnp_remove_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100315 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 dev->card = NULL;
317 list_del(&dev->card_list);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100318 mutex_unlock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 __pnp_remove_device(dev);
320}
321
322/**
323 * pnp_request_card_device - Searches for a PnP device under the specified card
Martin Waitz3d410882005-06-23 22:05:21 -0700324 * @clink: pointer to the card link, cannot be NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * @id: pointer to a PnP ID structure that explains the rules for finding the device
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300326 * @from: Starting place to search from. If NULL it will start from the beginning.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700328struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
329 const char *id, struct pnp_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700331 struct list_head *pos;
332 struct pnp_dev *dev;
333 struct pnp_card_driver *drv;
334 struct pnp_card *card;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!clink || !id)
Bjorn Helgaas5bfc43a02007-10-16 23:31:09 -0700337 return NULL;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 card = clink->card;
340 drv = clink->driver;
341 if (!from) {
342 pos = card->devices.next;
343 } else {
344 if (from->card != card)
Bjorn Helgaas5bfc43a02007-10-16 23:31:09 -0700345 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 pos = from->card_list.next;
347 }
348 while (pos != &card->devices) {
349 dev = card_to_pnp_dev(pos);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700350 if ((!dev->card_link) && compare_pnp_id(dev->id, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto found;
352 pos = pos->next;
353 }
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return NULL;
356
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600357found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dev->card_link = clink;
359 dev->dev.driver = &drv->link.driver;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800360 if (pnp_bus_type.probe(&dev->dev))
361 goto err_out;
362 if (device_bind_driver(&dev->dev))
363 goto err_out;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return dev;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800366
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600367err_out:
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800368 dev->dev.driver = NULL;
369 dev->card_link = NULL;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800370 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
Jinchao Wangd02908a2021-06-24 10:09:29 +0800372EXPORT_SYMBOL(pnp_request_card_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374/**
375 * pnp_release_card_device - call this when the driver no longer needs the device
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300376 * @dev: pointer to the PnP device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700378void pnp_release_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700380 struct pnp_card_driver *drv = dev->card_link->driver;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 drv->link.remove = &card_remove;
383 device_release_driver(&dev->dev);
384 drv->link.remove = &card_remove_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
Jinchao Wangd02908a2021-06-24 10:09:29 +0800386EXPORT_SYMBOL(pnp_release_card_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100388/*
389 * suspend/resume callbacks
390 */
391static int card_suspend(struct pnp_dev *dev, pm_message_t state)
392{
393 struct pnp_card_link *link = dev->card_link;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700394
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100395 if (link->pm_state.event == state.event)
396 return 0;
397 link->pm_state = state;
398 return link->driver->suspend(link, state);
399}
400
401static int card_resume(struct pnp_dev *dev)
402{
403 struct pnp_card_link *link = dev->card_link;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700404
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100405 if (link->pm_state.event == PM_EVENT_ON)
406 return 0;
407 link->pm_state = PMSG_ON;
408 link->driver->resume(link);
409 return 0;
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/**
413 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
414 * @drv: pointer to the driver to register
415 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700416int pnp_register_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800418 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct list_head *pos, *temp;
420
421 drv->link.name = drv->name;
422 drv->link.id_table = NULL; /* this will disable auto matching */
423 drv->link.flags = drv->flags;
424 drv->link.probe = NULL;
425 drv->link.remove = &card_remove_first;
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100426 drv->link.suspend = drv->suspend ? card_suspend : NULL;
427 drv->link.resume = drv->resume ? card_resume : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800429 error = pnp_register_driver(&drv->link);
430 if (error < 0)
431 return error;
Adam Belayd1d051b22006-01-20 09:29:27 +0100432
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100433 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 list_add_tail(&drv->global_list, &pnp_card_drivers);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100435 mutex_unlock(&pnp_lock);
Adam Belayd1d051b22006-01-20 09:29:27 +0100436
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700437 list_for_each_safe(pos, temp, &pnp_cards) {
438 struct pnp_card *card =
439 list_entry(pos, struct pnp_card, global_list);
440 card_probe(card, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
Jinchao Wangd02908a2021-06-24 10:09:29 +0800444EXPORT_SYMBOL(pnp_register_card_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446/**
447 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
448 * @drv: pointer to the driver to unregister
449 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700450void pnp_unregister_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100452 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 list_del(&drv->global_list);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100454 mutex_unlock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 pnp_unregister_driver(&drv->link);
456}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457EXPORT_SYMBOL(pnp_unregister_card_driver);