2 * card.c - contains functions for managing groups of PnP devices
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
7 #include <linux/module.h>
8 #include <linux/ctype.h>
9 #include <linux/slab.h>
10 #include <linux/pnp.h>
14 static LIST_HEAD(pnp_card_drivers);
16 static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
17 struct pnp_card *card)
19 const struct pnp_card_device_id *drv_id = drv->id_table;
22 if (compare_pnp_id(card->id, drv_id->id)) {
29 if (i == PNP_MAX_DEVICES ||
33 card_for_each_dev(card, dev) {
34 if (compare_pnp_id(dev->id,
35 drv_id->devs[i].id)) {
50 static void card_remove(struct pnp_dev *dev)
52 dev->card_link = NULL;
55 static void card_remove_first(struct pnp_dev *dev)
57 struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
59 if (!dev->card || !drv)
62 drv->remove(dev->card_link);
63 drv->link.remove = &card_remove;
64 kfree(dev->card_link);
68 static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
70 const struct pnp_card_device_id *id;
71 struct pnp_card_link *clink;
76 id = match_card(drv, card);
80 clink = pnp_alloc(sizeof(*clink));
85 clink->pm_state = PMSG_ON;
87 if (drv->probe(clink, id) >= 0)
91 card_for_each_dev(card, dev) {
92 if (dev->card_link == clink)
93 pnp_release_card_device(dev);
100 * pnp_add_card_id - adds an EISA id to the specified card
101 * @id: pointer to a pnp_id structure
102 * @card: pointer to the desired card
104 struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
106 struct pnp_id *dev_id, *ptr;
108 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
112 dev_id->id[0] = id[0];
113 dev_id->id[1] = id[1];
114 dev_id->id[2] = id[2];
115 dev_id->id[3] = tolower(id[3]);
116 dev_id->id[4] = tolower(id[4]);
117 dev_id->id[5] = tolower(id[5]);
118 dev_id->id[6] = tolower(id[6]);
119 dev_id->id[7] = '\0';
123 while (ptr && ptr->next)
133 static void pnp_free_card_ids(struct pnp_card *card)
146 static void pnp_release_card(struct device *dmdev)
148 struct pnp_card *card = to_pnp_card(dmdev);
150 pnp_free_card_ids(card);
154 static ssize_t pnp_show_card_name(struct device *dmdev,
155 struct device_attribute *attr, char *buf)
158 struct pnp_card *card = to_pnp_card(dmdev);
160 str += sprintf(str, "%s\n", card->name);
164 static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
166 static ssize_t pnp_show_card_ids(struct device *dmdev,
167 struct device_attribute *attr, char *buf)
170 struct pnp_card *card = to_pnp_card(dmdev);
171 struct pnp_id *pos = card->id;
174 str += sprintf(str, "%s\n", pos->id);
180 static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
182 static int pnp_interface_attach_card(struct pnp_card *card)
184 int rc = device_create_file(&card->dev, &dev_attr_name);
189 rc = device_create_file(&card->dev, &dev_attr_card_id);
196 device_remove_file(&card->dev, &dev_attr_name);
201 * pnp_add_card - adds a PnP card to the PnP Layer
202 * @card: pointer to the card to add
204 int pnp_add_card(struct pnp_card *card)
207 struct list_head *pos, *temp;
209 sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number,
211 card->dev.parent = &card->protocol->dev;
212 card->dev.bus = NULL;
213 card->dev.release = &pnp_release_card;
214 error = device_register(&card->dev);
216 dev_err(&card->dev, "could not register (err=%d)\n", error);
220 pnp_interface_attach_card(card);
221 spin_lock(&pnp_lock);
222 list_add_tail(&card->global_list, &pnp_cards);
223 list_add_tail(&card->protocol_list, &card->protocol->cards);
224 spin_unlock(&pnp_lock);
226 /* we wait until now to add devices in order to ensure the drivers
227 * will be able to use all of the related devices on the card
228 * without waiting an unreasonable length of time */
229 list_for_each(pos, &card->devices) {
230 struct pnp_dev *dev = card_to_pnp_dev(pos);
231 __pnp_add_device(dev);
234 /* match with card drivers */
235 list_for_each_safe(pos, temp, &pnp_card_drivers) {
236 struct pnp_card_driver *drv =
237 list_entry(pos, struct pnp_card_driver,
239 card_probe(card, drv);
245 * pnp_remove_card - removes a PnP card from the PnP Layer
246 * @card: pointer to the card to remove
248 void pnp_remove_card(struct pnp_card *card)
250 struct list_head *pos, *temp;
252 device_unregister(&card->dev);
253 spin_lock(&pnp_lock);
254 list_del(&card->global_list);
255 list_del(&card->protocol_list);
256 spin_unlock(&pnp_lock);
257 list_for_each_safe(pos, temp, &card->devices) {
258 struct pnp_dev *dev = card_to_pnp_dev(pos);
259 pnp_remove_card_device(dev);
264 * pnp_add_card_device - adds a device to the specified card
265 * @card: pointer to the card to add to
266 * @dev: pointer to the device to add
268 int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
270 dev->dev.parent = &card->dev;
271 dev->card_link = NULL;
272 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x",
273 dev->protocol->number, card->number, dev->number);
274 spin_lock(&pnp_lock);
276 list_add_tail(&dev->card_list, &card->devices);
277 spin_unlock(&pnp_lock);
282 * pnp_remove_card_device- removes a device from the specified card
283 * @dev: pointer to the device to remove
285 void pnp_remove_card_device(struct pnp_dev *dev)
287 spin_lock(&pnp_lock);
289 list_del(&dev->card_list);
290 spin_unlock(&pnp_lock);
291 __pnp_remove_device(dev);
295 * pnp_request_card_device - Searches for a PnP device under the specified card
296 * @clink: pointer to the card link, cannot be NULL
297 * @id: pointer to a PnP ID structure that explains the rules for finding the device
298 * @from: Starting place to search from. If NULL it will start from the begining.
300 struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
301 const char *id, struct pnp_dev *from)
303 struct list_head *pos;
305 struct pnp_card_driver *drv;
306 struct pnp_card *card;
314 pos = card->devices.next;
316 if (from->card != card)
318 pos = from->card_list.next;
320 while (pos != &card->devices) {
321 dev = card_to_pnp_dev(pos);
322 if ((!dev->card_link) && compare_pnp_id(dev->id, id))
330 dev->card_link = clink;
331 dev->dev.driver = &drv->link.driver;
332 if (pnp_bus_type.probe(&dev->dev))
334 if (device_bind_driver(&dev->dev))
340 dev->dev.driver = NULL;
341 dev->card_link = NULL;
346 * pnp_release_card_device - call this when the driver no longer needs the device
347 * @dev: pointer to the PnP device stucture
349 void pnp_release_card_device(struct pnp_dev *dev)
351 struct pnp_card_driver *drv = dev->card_link->driver;
353 drv->link.remove = &card_remove;
354 device_release_driver(&dev->dev);
355 drv->link.remove = &card_remove_first;
359 * suspend/resume callbacks
361 static int card_suspend(struct pnp_dev *dev, pm_message_t state)
363 struct pnp_card_link *link = dev->card_link;
365 if (link->pm_state.event == state.event)
367 link->pm_state = state;
368 return link->driver->suspend(link, state);
371 static int card_resume(struct pnp_dev *dev)
373 struct pnp_card_link *link = dev->card_link;
375 if (link->pm_state.event == PM_EVENT_ON)
377 link->pm_state = PMSG_ON;
378 link->driver->resume(link);
383 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
384 * @drv: pointer to the driver to register
386 int pnp_register_card_driver(struct pnp_card_driver *drv)
389 struct list_head *pos, *temp;
391 drv->link.name = drv->name;
392 drv->link.id_table = NULL; /* this will disable auto matching */
393 drv->link.flags = drv->flags;
394 drv->link.probe = NULL;
395 drv->link.remove = &card_remove_first;
396 drv->link.suspend = drv->suspend ? card_suspend : NULL;
397 drv->link.resume = drv->resume ? card_resume : NULL;
399 error = pnp_register_driver(&drv->link);
403 spin_lock(&pnp_lock);
404 list_add_tail(&drv->global_list, &pnp_card_drivers);
405 spin_unlock(&pnp_lock);
407 list_for_each_safe(pos, temp, &pnp_cards) {
408 struct pnp_card *card =
409 list_entry(pos, struct pnp_card, global_list);
410 card_probe(card, drv);
416 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
417 * @drv: pointer to the driver to unregister
419 void pnp_unregister_card_driver(struct pnp_card_driver *drv)
421 spin_lock(&pnp_lock);
422 list_del(&drv->global_list);
423 spin_unlock(&pnp_lock);
424 pnp_unregister_driver(&drv->link);
427 EXPORT_SYMBOL(pnp_request_card_device);
428 EXPORT_SYMBOL(pnp_release_card_device);
429 EXPORT_SYMBOL(pnp_register_card_driver);
430 EXPORT_SYMBOL(pnp_unregister_card_driver);