1 // SPDX-License-Identifier: GPL-2.0
3 * card.c - contains functions for managing groups of PnP devices
5 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/ctype.h>
11 #include <linux/slab.h>
12 #include <linux/pnp.h>
13 #include <linux/dma-mapping.h>
17 static LIST_HEAD(pnp_card_drivers);
19 static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
20 struct pnp_card *card)
22 const struct pnp_card_device_id *drv_id = drv->id_table;
25 if (compare_pnp_id(card->id, drv_id->id)) {
32 if (i == PNP_MAX_DEVICES ||
36 card_for_each_dev(card, dev) {
37 if (compare_pnp_id(dev->id,
38 drv_id->devs[i].id)) {
53 static void card_remove(struct pnp_dev *dev)
55 dev->card_link = NULL;
58 static void card_remove_first(struct pnp_dev *dev)
60 struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
62 if (!dev->card || !drv)
65 drv->remove(dev->card_link);
66 drv->link.remove = &card_remove;
67 kfree(dev->card_link);
71 static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
73 const struct pnp_card_device_id *id;
74 struct pnp_card_link *clink;
79 id = match_card(drv, card);
83 clink = kzalloc(sizeof(*clink), GFP_KERNEL);
88 clink->pm_state = PMSG_ON;
90 if (drv->probe(clink, id) >= 0)
94 card_for_each_dev(card, dev) {
95 if (dev->card_link == clink)
96 pnp_release_card_device(dev);
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
107 static struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
109 struct pnp_id *dev_id, *ptr;
111 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
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';
126 while (ptr && ptr->next)
136 static void pnp_free_card_ids(struct pnp_card *card)
149 static void pnp_release_card(struct device *dmdev)
151 struct pnp_card *card = to_pnp_card(dmdev);
153 pnp_free_card_ids(card);
157 struct pnp_card *pnp_alloc_card(struct pnp_protocol *protocol, int id, char *pnpid)
159 struct pnp_card *card;
160 struct pnp_id *dev_id;
162 card = kzalloc(sizeof(struct pnp_card), GFP_KERNEL);
166 card->protocol = protocol;
169 card->dev.parent = &card->protocol->dev;
170 dev_set_name(&card->dev, "%02x:%02x", card->protocol->number, card->number);
172 card->dev.coherent_dma_mask = DMA_BIT_MASK(24);
173 card->dev.dma_mask = &card->dev.coherent_dma_mask;
175 dev_id = pnp_add_card_id(card, pnpid);
184 static ssize_t name_show(struct device *dmdev,
185 struct device_attribute *attr, char *buf)
188 struct pnp_card *card = to_pnp_card(dmdev);
190 str += sprintf(str, "%s\n", card->name);
194 static DEVICE_ATTR_RO(name);
196 static ssize_t card_id_show(struct device *dmdev,
197 struct device_attribute *attr, char *buf)
200 struct pnp_card *card = to_pnp_card(dmdev);
201 struct pnp_id *pos = card->id;
204 str += sprintf(str, "%s\n", pos->id);
210 static DEVICE_ATTR_RO(card_id);
212 static int pnp_interface_attach_card(struct pnp_card *card)
214 int rc = device_create_file(&card->dev, &dev_attr_name);
219 rc = device_create_file(&card->dev, &dev_attr_card_id);
226 device_remove_file(&card->dev, &dev_attr_name);
231 * pnp_add_card - adds a PnP card to the PnP Layer
232 * @card: pointer to the card to add
234 int pnp_add_card(struct pnp_card *card)
237 struct list_head *pos, *temp;
239 card->dev.bus = NULL;
240 card->dev.release = &pnp_release_card;
241 error = device_register(&card->dev);
243 dev_err(&card->dev, "could not register (err=%d)\n", error);
244 put_device(&card->dev);
248 pnp_interface_attach_card(card);
249 mutex_lock(&pnp_lock);
250 list_add_tail(&card->global_list, &pnp_cards);
251 list_add_tail(&card->protocol_list, &card->protocol->cards);
252 mutex_unlock(&pnp_lock);
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);
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,
267 card_probe(card, drv);
273 * pnp_remove_card - removes a PnP card from the PnP Layer
274 * @card: pointer to the card to remove
276 void pnp_remove_card(struct pnp_card *card)
278 struct list_head *pos, *temp;
280 device_unregister(&card->dev);
281 mutex_lock(&pnp_lock);
282 list_del(&card->global_list);
283 list_del(&card->protocol_list);
284 mutex_unlock(&pnp_lock);
285 list_for_each_safe(pos, temp, &card->devices) {
286 struct pnp_dev *dev = card_to_pnp_dev(pos);
287 pnp_remove_card_device(dev);
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
296 int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
298 dev->dev.parent = &card->dev;
299 dev->card_link = NULL;
300 dev_set_name(&dev->dev, "%02x:%02x.%02x",
301 dev->protocol->number, card->number, dev->number);
302 mutex_lock(&pnp_lock);
304 list_add_tail(&dev->card_list, &card->devices);
305 mutex_unlock(&pnp_lock);
310 * pnp_remove_card_device- removes a device from the specified card
311 * @dev: pointer to the device to remove
313 void pnp_remove_card_device(struct pnp_dev *dev)
315 mutex_lock(&pnp_lock);
317 list_del(&dev->card_list);
318 mutex_unlock(&pnp_lock);
319 __pnp_remove_device(dev);
323 * pnp_request_card_device - Searches for a PnP device under the specified card
324 * @clink: pointer to the card link, cannot be NULL
325 * @id: pointer to a PnP ID structure that explains the rules for finding the device
326 * @from: Starting place to search from. If NULL it will start from the beginning.
328 struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
329 const char *id, struct pnp_dev *from)
331 struct list_head *pos;
333 struct pnp_card_driver *drv;
334 struct pnp_card *card;
342 pos = card->devices.next;
344 if (from->card != card)
346 pos = from->card_list.next;
348 while (pos != &card->devices) {
349 dev = card_to_pnp_dev(pos);
350 if ((!dev->card_link) && compare_pnp_id(dev->id, id))
358 dev->card_link = clink;
359 dev->dev.driver = &drv->link.driver;
360 if (pnp_bus_type.probe(&dev->dev))
362 if (device_bind_driver(&dev->dev))
368 dev->dev.driver = NULL;
369 dev->card_link = NULL;
372 EXPORT_SYMBOL(pnp_request_card_device);
375 * pnp_release_card_device - call this when the driver no longer needs the device
376 * @dev: pointer to the PnP device structure
378 void pnp_release_card_device(struct pnp_dev *dev)
380 struct pnp_card_driver *drv = dev->card_link->driver;
382 drv->link.remove = &card_remove;
383 device_release_driver(&dev->dev);
384 drv->link.remove = &card_remove_first;
386 EXPORT_SYMBOL(pnp_release_card_device);
389 * suspend/resume callbacks
391 static int card_suspend(struct pnp_dev *dev, pm_message_t state)
393 struct pnp_card_link *link = dev->card_link;
395 if (link->pm_state.event == state.event)
397 link->pm_state = state;
398 return link->driver->suspend(link, state);
401 static int card_resume(struct pnp_dev *dev)
403 struct pnp_card_link *link = dev->card_link;
405 if (link->pm_state.event == PM_EVENT_ON)
407 link->pm_state = PMSG_ON;
408 link->driver->resume(link);
413 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
414 * @drv: pointer to the driver to register
416 int pnp_register_card_driver(struct pnp_card_driver *drv)
419 struct list_head *pos, *temp;
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;
426 drv->link.suspend = drv->suspend ? card_suspend : NULL;
427 drv->link.resume = drv->resume ? card_resume : NULL;
429 error = pnp_register_driver(&drv->link);
433 mutex_lock(&pnp_lock);
434 list_add_tail(&drv->global_list, &pnp_card_drivers);
435 mutex_unlock(&pnp_lock);
437 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);
444 EXPORT_SYMBOL(pnp_register_card_driver);
447 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
448 * @drv: pointer to the driver to unregister
450 void pnp_unregister_card_driver(struct pnp_card_driver *drv)
452 mutex_lock(&pnp_lock);
453 list_del(&drv->global_list);
454 mutex_unlock(&pnp_lock);
455 pnp_unregister_driver(&drv->link);
457 EXPORT_SYMBOL(pnp_unregister_card_driver);