blob: 31ad9fc3f701a3e8ed7b0c37c916c5f98f07248a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * card.c - contains functions for managing groups of PnP devices
3 *
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +01008#include <linux/mutex.h>
Bjorn Helgaase4366752008-04-28 16:33:56 -06009#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/pnp.h>
Rene Hermane86b19c2008-07-25 19:44:42 -070012#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "base.h"
14
15LIST_HEAD(pnp_cards);
Adrian Bunkb449f632005-11-07 01:01:48 -080016static LIST_HEAD(pnp_card_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070018static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
19 struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070021 const struct pnp_card_device_id *drv_id = drv->id_table;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070022
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070023 while (*drv_id->id) {
24 if (compare_pnp_id(card->id, drv_id->id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 int i = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 for (;;) {
28 int found;
29 struct pnp_dev *dev;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070030
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -060031 if (i == PNP_MAX_DEVICES ||
32 !*drv_id->devs[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return drv_id;
34 found = 0;
35 card_for_each_dev(card, dev) {
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -060036 if (compare_pnp_id(dev->id,
37 drv_id->devs[i].id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 found = 1;
39 break;
40 }
41 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070042 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 break;
44 i++;
45 }
46 }
47 drv_id++;
48 }
49 return NULL;
50}
51
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070052static void card_remove(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 dev->card_link = NULL;
55}
Bjorn Helgaas982c6092006-03-27 01:17:08 -080056
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070057static void card_remove_first(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070059 struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (!dev->card || !drv)
62 return;
63 if (drv->remove)
64 drv->remove(dev->card_link);
65 drv->link.remove = &card_remove;
66 kfree(dev->card_link);
67 card_remove(dev);
68}
69
Jesper Juhla9adb8d2006-06-25 05:47:17 -070070static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Jesper Juhla9adb8d2006-06-25 05:47:17 -070072 const struct pnp_card_device_id *id;
73 struct pnp_card_link *clink;
74 struct pnp_dev *dev;
75
76 if (!drv->probe)
77 return 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070078 id = match_card(drv, card);
Jesper Juhla9adb8d2006-06-25 05:47:17 -070079 if (!id)
80 return 0;
81
82 clink = pnp_alloc(sizeof(*clink));
83 if (!clink)
84 return 0;
85 clink->card = card;
86 clink->driver = drv;
87 clink->pm_state = PMSG_ON;
88
89 if (drv->probe(clink, id) >= 0)
90 return 1;
91
92 /* Recovery */
93 card_for_each_dev(card, dev) {
94 if (dev->card_link == clink)
95 pnp_release_card_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Jesper Juhla9adb8d2006-06-25 05:47:17 -070097 kfree(clink);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
99}
100
101/**
102 * pnp_add_card_id - adds an EISA id to the specified card
103 * @id: pointer to a pnp_id structure
104 * @card: pointer to the desired card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
Adrian Bunk25cdcd02008-07-25 19:46:21 -0700106static struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Bjorn Helgaase4366752008-04-28 16:33:56 -0600108 struct pnp_id *dev_id, *ptr;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700109
Bjorn Helgaase4366752008-04-28 16:33:56 -0600110 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
111 if (!dev_id)
112 return NULL;
113
114 dev_id->id[0] = id[0];
115 dev_id->id[1] = id[1];
116 dev_id->id[2] = id[2];
117 dev_id->id[3] = tolower(id[3]);
118 dev_id->id[4] = tolower(id[4]);
119 dev_id->id[5] = tolower(id[5]);
120 dev_id->id[6] = tolower(id[6]);
121 dev_id->id[7] = '\0';
122
123 dev_id->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ptr = card->id;
125 while (ptr && ptr->next)
126 ptr = ptr->next;
127 if (ptr)
Bjorn Helgaase4366752008-04-28 16:33:56 -0600128 ptr->next = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 else
Bjorn Helgaase4366752008-04-28 16:33:56 -0600130 card->id = dev_id;
131
132 return dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700135static void pnp_free_card_ids(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700137 struct pnp_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct pnp_id *next;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 id = card->id;
141 while (id) {
142 next = id->next;
143 kfree(id);
144 id = next;
145 }
146}
147
148static void pnp_release_card(struct device *dmdev)
149{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700150 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 pnp_free_card_ids(card);
153 kfree(card);
154}
155
Bjorn Helgaas6bf2aab2008-04-28 16:33:58 -0600156struct pnp_card *pnp_alloc_card(struct pnp_protocol *protocol, int id, char *pnpid)
157{
158 struct pnp_card *card;
159 struct pnp_id *dev_id;
160
161 card = kzalloc(sizeof(struct pnp_card), GFP_KERNEL);
162 if (!card)
163 return NULL;
164
165 card->protocol = protocol;
166 card->number = id;
167
168 card->dev.parent = &card->protocol->dev;
Kay Sieversc85e37c2009-01-06 10:44:38 -0800169 dev_set_name(&card->dev, "%02x:%02x", card->protocol->number, card->number);
Bjorn Helgaas6bf2aab2008-04-28 16:33:58 -0600170
Yang Hongyang2f4f27d2009-04-06 19:01:18 -0700171 card->dev.coherent_dma_mask = DMA_BIT_MASK(24);
Rene Hermane86b19c2008-07-25 19:44:42 -0700172 card->dev.dma_mask = &card->dev.coherent_dma_mask;
173
Bjorn Helgaas6bf2aab2008-04-28 16:33:58 -0600174 dev_id = pnp_add_card_id(card, pnpid);
175 if (!dev_id) {
176 kfree(card);
177 return NULL;
178 }
179
180 return card;
181}
182
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700183static ssize_t pnp_show_card_name(struct device *dmdev,
184 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 char *str = buf;
187 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700188
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700189 str += sprintf(str, "%s\n", card->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return (str - buf);
191}
192
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700193static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700195static ssize_t pnp_show_card_ids(struct device *dmdev,
196 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 char *str = buf;
199 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700200 struct pnp_id *pos = card->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 while (pos) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700203 str += sprintf(str, "%s\n", pos->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 pos = pos->next;
205 }
206 return (str - buf);
207}
208
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700209static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211static int pnp_interface_attach_card(struct pnp_card *card)
212{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700213 int rc = device_create_file(&card->dev, &dev_attr_name);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700214
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700215 if (rc)
216 return rc;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800217
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700218 rc = device_create_file(&card->dev, &dev_attr_card_id);
219 if (rc)
220 goto err_name;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800223
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600224err_name:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700225 device_remove_file(&card->dev, &dev_attr_name);
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800226 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229/**
230 * pnp_add_card - adds a PnP card to the PnP Layer
231 * @card: pointer to the card to add
232 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700233int pnp_add_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int error;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700236 struct list_head *pos, *temp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 card->dev.bus = NULL;
239 card->dev.release = &pnp_release_card;
240 error = device_register(&card->dev);
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700241 if (error) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700242 dev_err(&card->dev, "could not register (err=%d)\n", error);
Levente Kurusa75365d02013-12-19 16:03:36 +0100243 put_device(&card->dev);
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700244 return error;
245 }
246
247 pnp_interface_attach_card(card);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100248 mutex_lock(&pnp_lock);
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700249 list_add_tail(&card->global_list, &pnp_cards);
250 list_add_tail(&card->protocol_list, &card->protocol->cards);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100251 mutex_unlock(&pnp_lock);
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700252
253 /* we wait until now to add devices in order to ensure the drivers
254 * will be able to use all of the related devices on the card
255 * without waiting an unreasonable length of time */
256 list_for_each(pos, &card->devices) {
257 struct pnp_dev *dev = card_to_pnp_dev(pos);
258 __pnp_add_device(dev);
259 }
260
261 /* match with card drivers */
262 list_for_each_safe(pos, temp, &pnp_card_drivers) {
263 struct pnp_card_driver *drv =
264 list_entry(pos, struct pnp_card_driver,
265 global_list);
266 card_probe(card, drv);
267 }
268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/**
272 * pnp_remove_card - removes a PnP card from the PnP Layer
273 * @card: pointer to the card to remove
274 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700275void pnp_remove_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct list_head *pos, *temp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 device_unregister(&card->dev);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100280 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 list_del(&card->global_list);
282 list_del(&card->protocol_list);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100283 mutex_unlock(&pnp_lock);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700284 list_for_each_safe(pos, temp, &card->devices) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct pnp_dev *dev = card_to_pnp_dev(pos);
286 pnp_remove_card_device(dev);
287 }
288}
289
290/**
291 * pnp_add_card_device - adds a device to the specified card
292 * @card: pointer to the card to add to
293 * @dev: pointer to the device to add
294 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700295int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 dev->dev.parent = &card->dev;
298 dev->card_link = NULL;
Kay Sieversc85e37c2009-01-06 10:44:38 -0800299 dev_set_name(&dev->dev, "%02x:%02x.%02x",
300 dev->protocol->number, card->number, dev->number);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100301 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 dev->card = card;
303 list_add_tail(&dev->card_list, &card->devices);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100304 mutex_unlock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
306}
307
308/**
309 * pnp_remove_card_device- removes a device from the specified card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * @dev: pointer to the device to remove
311 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700312void pnp_remove_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100314 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 dev->card = NULL;
316 list_del(&dev->card_list);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100317 mutex_unlock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 __pnp_remove_device(dev);
319}
320
321/**
322 * pnp_request_card_device - Searches for a PnP device under the specified card
Martin Waitz3d410882005-06-23 22:05:21 -0700323 * @clink: pointer to the card link, cannot be NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * @id: pointer to a PnP ID structure that explains the rules for finding the device
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300325 * @from: Starting place to search from. If NULL it will start from the beginning.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700327struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
328 const char *id, struct pnp_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700330 struct list_head *pos;
331 struct pnp_dev *dev;
332 struct pnp_card_driver *drv;
333 struct pnp_card *card;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (!clink || !id)
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700336 return NULL;
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 card = clink->card;
339 drv = clink->driver;
340 if (!from) {
341 pos = card->devices.next;
342 } else {
343 if (from->card != card)
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700344 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 pos = from->card_list.next;
346 }
347 while (pos != &card->devices) {
348 dev = card_to_pnp_dev(pos);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700349 if ((!dev->card_link) && compare_pnp_id(dev->id, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto found;
351 pos = pos->next;
352 }
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return NULL;
355
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600356found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 dev->card_link = clink;
358 dev->dev.driver = &drv->link.driver;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800359 if (pnp_bus_type.probe(&dev->dev))
360 goto err_out;
361 if (device_bind_driver(&dev->dev))
362 goto err_out;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return dev;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800365
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600366err_out:
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800367 dev->dev.driver = NULL;
368 dev->card_link = NULL;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800369 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372/**
373 * pnp_release_card_device - call this when the driver no longer needs the device
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300374 * @dev: pointer to the PnP device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700376void pnp_release_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700378 struct pnp_card_driver *drv = dev->card_link->driver;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 drv->link.remove = &card_remove;
381 device_release_driver(&dev->dev);
382 drv->link.remove = &card_remove_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100385/*
386 * suspend/resume callbacks
387 */
388static int card_suspend(struct pnp_dev *dev, pm_message_t state)
389{
390 struct pnp_card_link *link = dev->card_link;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700391
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100392 if (link->pm_state.event == state.event)
393 return 0;
394 link->pm_state = state;
395 return link->driver->suspend(link, state);
396}
397
398static int card_resume(struct pnp_dev *dev)
399{
400 struct pnp_card_link *link = dev->card_link;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700401
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100402 if (link->pm_state.event == PM_EVENT_ON)
403 return 0;
404 link->pm_state = PMSG_ON;
405 link->driver->resume(link);
406 return 0;
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/**
410 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
411 * @drv: pointer to the driver to register
412 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700413int pnp_register_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800415 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct list_head *pos, *temp;
417
418 drv->link.name = drv->name;
419 drv->link.id_table = NULL; /* this will disable auto matching */
420 drv->link.flags = drv->flags;
421 drv->link.probe = NULL;
422 drv->link.remove = &card_remove_first;
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100423 drv->link.suspend = drv->suspend ? card_suspend : NULL;
424 drv->link.resume = drv->resume ? card_resume : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800426 error = pnp_register_driver(&drv->link);
427 if (error < 0)
428 return error;
Adam Belayd1d051b22006-01-20 09:29:27 +0100429
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100430 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 list_add_tail(&drv->global_list, &pnp_card_drivers);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100432 mutex_unlock(&pnp_lock);
Adam Belayd1d051b22006-01-20 09:29:27 +0100433
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700434 list_for_each_safe(pos, temp, &pnp_cards) {
435 struct pnp_card *card =
436 list_entry(pos, struct pnp_card, global_list);
437 card_probe(card, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/**
443 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
444 * @drv: pointer to the driver to unregister
445 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700446void pnp_unregister_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100448 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 list_del(&drv->global_list);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100450 mutex_unlock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 pnp_unregister_driver(&drv->link);
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454EXPORT_SYMBOL(pnp_request_card_device);
455EXPORT_SYMBOL(pnp_release_card_device);
456EXPORT_SYMBOL(pnp_register_card_driver);
457EXPORT_SYMBOL(pnp_unregister_card_driver);