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>
11 #include <linux/pnp.h>
15 static LIST_HEAD(pnp_card_drivers);
18 static const struct pnp_card_device_id * match_card(struct pnp_card_driver * drv, struct pnp_card * card)
20 const struct pnp_card_device_id * drv_id = drv->id_table;
22 if (compare_pnp_id(card->id,drv_id->id)) {
27 if (i == PNP_MAX_DEVICES || ! *drv_id->devs[i].id)
30 card_for_each_dev(card, dev) {
31 if (compare_pnp_id(dev->id, drv_id->devs[i].id)) {
46 static void card_remove(struct pnp_dev * dev)
48 dev->card_link = NULL;
51 static void card_remove_first(struct pnp_dev * dev)
53 struct pnp_card_driver * drv = to_pnp_card_driver(dev->driver);
54 if (!dev->card || !drv)
57 drv->remove(dev->card_link);
58 drv->link.remove = &card_remove;
59 kfree(dev->card_link);
63 static int card_probe(struct pnp_card * card, struct pnp_card_driver * drv)
65 const struct pnp_card_device_id *id = match_card(drv,card);
67 struct pnp_card_link * clink = pnp_alloc(sizeof(struct pnp_card_link));
73 if (drv->probe(clink, id)>=0)
77 card_for_each_dev(card, dev) {
78 if (dev->card_link == clink)
79 pnp_release_card_device(dev);
90 * pnp_add_card_id - adds an EISA id to the specified card
91 * @id: pointer to a pnp_id structure
92 * @card: pointer to the desired card
96 int pnp_add_card_id(struct pnp_id *id, struct pnp_card * card)
105 while (ptr && ptr->next)
114 static void pnp_free_card_ids(struct pnp_card * card)
128 static void pnp_release_card(struct device *dmdev)
130 struct pnp_card * card = to_pnp_card(dmdev);
131 pnp_free_card_ids(card);
136 static ssize_t pnp_show_card_name(struct device *dmdev, struct device_attribute *attr, char *buf)
139 struct pnp_card *card = to_pnp_card(dmdev);
140 str += sprintf(str,"%s\n", card->name);
144 static DEVICE_ATTR(name,S_IRUGO,pnp_show_card_name,NULL);
146 static ssize_t pnp_show_card_ids(struct device *dmdev, struct device_attribute *attr, char *buf)
149 struct pnp_card *card = to_pnp_card(dmdev);
150 struct pnp_id * pos = card->id;
153 str += sprintf(str,"%s\n", pos->id);
159 static DEVICE_ATTR(card_id,S_IRUGO,pnp_show_card_ids,NULL);
161 static int pnp_interface_attach_card(struct pnp_card *card)
163 device_create_file(&card->dev,&dev_attr_name);
164 device_create_file(&card->dev,&dev_attr_card_id);
169 * pnp_add_card - adds a PnP card to the PnP Layer
170 * @card: pointer to the card to add
173 int pnp_add_card(struct pnp_card * card)
176 struct list_head * pos, * temp;
177 if (!card || !card->protocol)
180 sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number, card->number);
181 card->dev.parent = &card->protocol->dev;
182 card->dev.bus = NULL;
183 card->dev.release = &pnp_release_card;
184 error = device_register(&card->dev);
187 pnp_interface_attach_card(card);
188 spin_lock(&pnp_lock);
189 list_add_tail(&card->global_list, &pnp_cards);
190 list_add_tail(&card->protocol_list, &card->protocol->cards);
191 spin_unlock(&pnp_lock);
193 /* we wait until now to add devices in order to ensure the drivers
194 * will be able to use all of the related devices on the card
195 * without waiting any unresonable length of time */
196 list_for_each(pos,&card->devices){
197 struct pnp_dev *dev = card_to_pnp_dev(pos);
198 __pnp_add_device(dev);
201 /* match with card drivers */
202 list_for_each_safe(pos,temp,&pnp_card_drivers){
203 struct pnp_card_driver * drv = list_entry(pos, struct pnp_card_driver, global_list);
204 card_probe(card,drv);
207 pnp_err("sysfs failure, card '%s' will be unavailable", card->dev.bus_id);
212 * pnp_remove_card - removes a PnP card from the PnP Layer
213 * @card: pointer to the card to remove
216 void pnp_remove_card(struct pnp_card * card)
218 struct list_head *pos, *temp;
221 device_unregister(&card->dev);
222 spin_lock(&pnp_lock);
223 list_del(&card->global_list);
224 list_del(&card->protocol_list);
225 spin_unlock(&pnp_lock);
226 list_for_each_safe(pos,temp,&card->devices){
227 struct pnp_dev *dev = card_to_pnp_dev(pos);
228 pnp_remove_card_device(dev);
233 * pnp_add_card_device - adds a device to the specified card
234 * @card: pointer to the card to add to
235 * @dev: pointer to the device to add
238 int pnp_add_card_device(struct pnp_card * card, struct pnp_dev * dev)
240 if (!card || !dev || !dev->protocol)
242 dev->dev.parent = &card->dev;
243 dev->card_link = NULL;
244 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x", dev->protocol->number,
245 card->number,dev->number);
246 spin_lock(&pnp_lock);
248 list_add_tail(&dev->card_list, &card->devices);
249 spin_unlock(&pnp_lock);
254 * pnp_remove_card_device- removes a device from the specified card
255 * @dev: pointer to the device to remove
258 void pnp_remove_card_device(struct pnp_dev * dev)
260 spin_lock(&pnp_lock);
262 list_del(&dev->card_list);
263 spin_unlock(&pnp_lock);
264 __pnp_remove_device(dev);
268 * pnp_request_card_device - Searches for a PnP device under the specified card
269 * @clink: pointer to the card link, cannot be NULL
270 * @id: pointer to a PnP ID structure that explains the rules for finding the device
271 * @from: Starting place to search from. If NULL it will start from the begining.
274 struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from)
276 struct list_head * pos;
277 struct pnp_dev * dev;
278 struct pnp_card_driver * drv;
279 struct pnp_card * card;
285 pos = card->devices.next;
287 if (from->card != card)
289 pos = from->card_list.next;
291 while (pos != &card->devices) {
292 dev = card_to_pnp_dev(pos);
293 if ((!dev->card_link) && compare_pnp_id(dev->id,id))
302 down_write(&dev->dev.bus->subsys.rwsem);
303 dev->card_link = clink;
304 dev->dev.driver = &drv->link.driver;
305 if (drv->link.driver.probe) {
306 if (drv->link.driver.probe(&dev->dev)) {
307 dev->dev.driver = NULL;
308 dev->card_link = NULL;
309 up_write(&dev->dev.bus->subsys.rwsem);
313 device_bind_driver(&dev->dev);
314 up_write(&dev->dev.bus->subsys.rwsem);
320 * pnp_release_card_device - call this when the driver no longer needs the device
321 * @dev: pointer to the PnP device stucture
324 void pnp_release_card_device(struct pnp_dev * dev)
326 struct pnp_card_driver * drv = dev->card_link->driver;
329 down_write(&dev->dev.bus->subsys.rwsem);
330 drv->link.remove = &card_remove;
331 device_release_driver(&dev->dev);
332 drv->link.remove = &card_remove_first;
333 up_write(&dev->dev.bus->subsys.rwsem);
337 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
338 * @drv: pointer to the driver to register
341 int pnp_register_card_driver(struct pnp_card_driver * drv)
344 struct list_head *pos, *temp;
346 drv->link.name = drv->name;
347 drv->link.id_table = NULL; /* this will disable auto matching */
348 drv->link.flags = drv->flags;
349 drv->link.probe = NULL;
350 drv->link.remove = &card_remove_first;
352 spin_lock(&pnp_lock);
353 list_add_tail(&drv->global_list, &pnp_card_drivers);
354 spin_unlock(&pnp_lock);
355 pnp_register_driver(&drv->link);
357 list_for_each_safe(pos,temp,&pnp_cards){
358 struct pnp_card *card = list_entry(pos, struct pnp_card, global_list);
359 count += card_probe(card,drv);
365 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
366 * @drv: pointer to the driver to unregister
369 void pnp_unregister_card_driver(struct pnp_card_driver * drv)
371 spin_lock(&pnp_lock);
372 list_del(&drv->global_list);
373 spin_unlock(&pnp_lock);
374 pnp_unregister_driver(&drv->link);
378 EXPORT_SYMBOL(pnp_add_card);
379 EXPORT_SYMBOL(pnp_remove_card);
380 EXPORT_SYMBOL(pnp_add_card_device);
381 EXPORT_SYMBOL(pnp_remove_card_device);
382 EXPORT_SYMBOL(pnp_add_card_id);
384 EXPORT_SYMBOL(pnp_request_card_device);
385 EXPORT_SYMBOL(pnp_release_card_device);
386 EXPORT_SYMBOL(pnp_register_card_driver);
387 EXPORT_SYMBOL(pnp_unregister_card_driver);