2 * card.c - contains functions for managing groups of PnP devices
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
12 #ifdef CONFIG_PNP_DEBUG
18 #include <linux/pnp.h>
22 LIST_HEAD(pnp_card_drivers);
25 static const struct pnp_card_device_id * match_card(struct pnp_card_driver * drv, struct pnp_card * card)
27 const struct pnp_card_device_id * drv_id = drv->id_table;
29 if (compare_pnp_id(card->id,drv_id->id)) {
34 if (i == PNP_MAX_DEVICES || ! *drv_id->devs[i].id)
37 card_for_each_dev(card, dev) {
38 if (compare_pnp_id(dev->id, 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);
61 if (!dev->card || !drv)
64 drv->remove(dev->card_link);
65 drv->link.remove = &card_remove;
66 kfree(dev->card_link);
70 static int card_probe(struct pnp_card * card, struct pnp_card_driver * drv)
72 const struct pnp_card_device_id *id = match_card(drv,card);
74 struct pnp_card_link * clink = pnp_alloc(sizeof(struct pnp_card_link));
80 if (drv->probe(clink, id)>=0)
84 card_for_each_dev(card, dev) {
85 if (dev->card_link == clink)
86 pnp_release_card_device(dev);
97 * pnp_add_card_id - adds an EISA id to the specified card
98 * @id: pointer to a pnp_id structure
99 * @card: pointer to the desired card
103 int pnp_add_card_id(struct pnp_id *id, struct pnp_card * card)
112 while (ptr && ptr->next)
121 static void pnp_free_card_ids(struct pnp_card * card)
135 static void pnp_release_card(struct device *dmdev)
137 struct pnp_card * card = to_pnp_card(dmdev);
138 pnp_free_card_ids(card);
143 static ssize_t pnp_show_card_name(struct device *dmdev, struct device_attribute *attr, char *buf)
146 struct pnp_card *card = to_pnp_card(dmdev);
147 str += sprintf(str,"%s\n", card->name);
151 static DEVICE_ATTR(name,S_IRUGO,pnp_show_card_name,NULL);
153 static ssize_t pnp_show_card_ids(struct device *dmdev, struct device_attribute *attr, char *buf)
156 struct pnp_card *card = to_pnp_card(dmdev);
157 struct pnp_id * pos = card->id;
160 str += sprintf(str,"%s\n", pos->id);
166 static DEVICE_ATTR(card_id,S_IRUGO,pnp_show_card_ids,NULL);
168 static int pnp_interface_attach_card(struct pnp_card *card)
170 device_create_file(&card->dev,&dev_attr_name);
171 device_create_file(&card->dev,&dev_attr_card_id);
176 * pnp_add_card - adds a PnP card to the PnP Layer
177 * @card: pointer to the card to add
180 int pnp_add_card(struct pnp_card * card)
183 struct list_head * pos, * temp;
184 if (!card || !card->protocol)
187 sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number, card->number);
188 card->dev.parent = &card->protocol->dev;
189 card->dev.bus = NULL;
190 card->dev.release = &pnp_release_card;
191 error = device_register(&card->dev);
194 pnp_interface_attach_card(card);
195 spin_lock(&pnp_lock);
196 list_add_tail(&card->global_list, &pnp_cards);
197 list_add_tail(&card->protocol_list, &card->protocol->cards);
198 spin_unlock(&pnp_lock);
200 /* we wait until now to add devices in order to ensure the drivers
201 * will be able to use all of the related devices on the card
202 * without waiting any unresonable length of time */
203 list_for_each(pos,&card->devices){
204 struct pnp_dev *dev = card_to_pnp_dev(pos);
205 __pnp_add_device(dev);
208 /* match with card drivers */
209 list_for_each_safe(pos,temp,&pnp_card_drivers){
210 struct pnp_card_driver * drv = list_entry(pos, struct pnp_card_driver, global_list);
211 card_probe(card,drv);
214 pnp_err("sysfs failure, card '%s' will be unavailable", card->dev.bus_id);
219 * pnp_remove_card - removes a PnP card from the PnP Layer
220 * @card: pointer to the card to remove
223 void pnp_remove_card(struct pnp_card * card)
225 struct list_head *pos, *temp;
228 device_unregister(&card->dev);
229 spin_lock(&pnp_lock);
230 list_del(&card->global_list);
231 list_del(&card->protocol_list);
232 spin_unlock(&pnp_lock);
233 list_for_each_safe(pos,temp,&card->devices){
234 struct pnp_dev *dev = card_to_pnp_dev(pos);
235 pnp_remove_card_device(dev);
240 * pnp_add_card_device - adds a device to the specified card
241 * @card: pointer to the card to add to
242 * @dev: pointer to the device to add
245 int pnp_add_card_device(struct pnp_card * card, struct pnp_dev * dev)
247 if (!card || !dev || !dev->protocol)
249 dev->dev.parent = &card->dev;
250 dev->card_link = NULL;
251 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x", dev->protocol->number,
252 card->number,dev->number);
253 spin_lock(&pnp_lock);
255 list_add_tail(&dev->card_list, &card->devices);
256 spin_unlock(&pnp_lock);
261 * pnp_remove_card_device- removes a device from the specified card
262 * @dev: pointer to the device to remove
265 void pnp_remove_card_device(struct pnp_dev * dev)
267 spin_lock(&pnp_lock);
269 list_del(&dev->card_list);
270 spin_unlock(&pnp_lock);
271 __pnp_remove_device(dev);
275 * pnp_request_card_device - Searches for a PnP device under the specified card
276 * @clink: pointer to the card link, cannot be NULL
277 * @id: pointer to a PnP ID structure that explains the rules for finding the device
278 * @from: Starting place to search from. If NULL it will start from the begining.
281 struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from)
283 struct list_head * pos;
284 struct pnp_dev * dev;
285 struct pnp_card_driver * drv;
286 struct pnp_card * card;
292 pos = card->devices.next;
294 if (from->card != card)
296 pos = from->card_list.next;
298 while (pos != &card->devices) {
299 dev = card_to_pnp_dev(pos);
300 if ((!dev->card_link) && compare_pnp_id(dev->id,id))
309 down_write(&dev->dev.bus->subsys.rwsem);
310 dev->card_link = clink;
311 dev->dev.driver = &drv->link.driver;
312 if (drv->link.driver.probe) {
313 if (drv->link.driver.probe(&dev->dev)) {
314 dev->dev.driver = NULL;
315 dev->card_link = NULL;
316 up_write(&dev->dev.bus->subsys.rwsem);
320 device_bind_driver(&dev->dev);
321 up_write(&dev->dev.bus->subsys.rwsem);
327 * pnp_release_card_device - call this when the driver no longer needs the device
328 * @dev: pointer to the PnP device stucture
331 void pnp_release_card_device(struct pnp_dev * dev)
333 struct pnp_card_driver * drv = dev->card_link->driver;
336 down_write(&dev->dev.bus->subsys.rwsem);
337 drv->link.remove = &card_remove;
338 device_release_driver(&dev->dev);
339 drv->link.remove = &card_remove_first;
340 up_write(&dev->dev.bus->subsys.rwsem);
344 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
345 * @drv: pointer to the driver to register
348 int pnp_register_card_driver(struct pnp_card_driver * drv)
351 struct list_head *pos, *temp;
353 drv->link.name = drv->name;
354 drv->link.id_table = NULL; /* this will disable auto matching */
355 drv->link.flags = drv->flags;
356 drv->link.probe = NULL;
357 drv->link.remove = &card_remove_first;
359 spin_lock(&pnp_lock);
360 list_add_tail(&drv->global_list, &pnp_card_drivers);
361 spin_unlock(&pnp_lock);
362 pnp_register_driver(&drv->link);
364 list_for_each_safe(pos,temp,&pnp_cards){
365 struct pnp_card *card = list_entry(pos, struct pnp_card, global_list);
366 count += card_probe(card,drv);
372 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
373 * @drv: pointer to the driver to unregister
376 void pnp_unregister_card_driver(struct pnp_card_driver * drv)
378 spin_lock(&pnp_lock);
379 list_del(&drv->global_list);
380 spin_unlock(&pnp_lock);
381 pnp_unregister_driver(&drv->link);
384 EXPORT_SYMBOL(pnp_add_card);
385 EXPORT_SYMBOL(pnp_remove_card);
386 EXPORT_SYMBOL(pnp_add_card_device);
387 EXPORT_SYMBOL(pnp_remove_card_device);
388 EXPORT_SYMBOL(pnp_add_card_id);
389 EXPORT_SYMBOL(pnp_request_card_device);
390 EXPORT_SYMBOL(pnp_release_card_device);
391 EXPORT_SYMBOL(pnp_register_card_driver);
392 EXPORT_SYMBOL(pnp_unregister_card_driver);