Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / drivers / pnp / card.c
blob97eeecfaef1b712682d7a4224682f4463c356050
1 /*
2 * card.c - contains functions for managing groups of PnP devices
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
6 */
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
12 #ifdef CONFIG_PNP_DEBUG
13 #define DEBUG
14 #else
15 #undef DEBUG
16 #endif
18 #include <linux/pnp.h>
19 #include "base.h"
21 LIST_HEAD(pnp_cards);
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;
28 while (*drv_id->id){
29 if (compare_pnp_id(card->id,drv_id->id)) {
30 int i = 0;
31 for (;;) {
32 int found;
33 struct pnp_dev *dev;
34 if (i == PNP_MAX_DEVICES || ! *drv_id->devs[i].id)
35 return drv_id;
36 found = 0;
37 card_for_each_dev(card, dev) {
38 if (compare_pnp_id(dev->id, drv_id->devs[i].id)) {
39 found = 1;
40 break;
43 if (! found)
44 break;
45 i++;
48 drv_id++;
50 return NULL;
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)
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);
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);
73 if (id) {
74 struct pnp_card_link * clink = pnp_alloc(sizeof(struct pnp_card_link));
75 if (!clink)
76 return 0;
77 clink->card = card;
78 clink->driver = drv;
79 if (drv->probe) {
80 if (drv->probe(clink, id)>=0)
81 return 1;
82 else {
83 struct pnp_dev * dev;
84 card_for_each_dev(card, dev) {
85 if (dev->card_link == clink)
86 pnp_release_card_device(dev);
88 kfree(clink);
90 } else
91 return 1;
93 return 0;
96 /**
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)
105 struct pnp_id * ptr;
106 if (!id)
107 return -EINVAL;
108 if (!card)
109 return -EINVAL;
110 id->next = NULL;
111 ptr = card->id;
112 while (ptr && ptr->next)
113 ptr = ptr->next;
114 if (ptr)
115 ptr->next = id;
116 else
117 card->id = id;
118 return 0;
121 static void pnp_free_card_ids(struct pnp_card * card)
123 struct pnp_id * id;
124 struct pnp_id *next;
125 if (!card)
126 return;
127 id = card->id;
128 while (id) {
129 next = id->next;
130 kfree(id);
131 id = next;
135 static void pnp_release_card(struct device *dmdev)
137 struct pnp_card * card = to_pnp_card(dmdev);
138 pnp_free_card_ids(card);
139 kfree(card);
143 static ssize_t pnp_show_card_name(struct device *dmdev, char *buf)
145 char *str = buf;
146 struct pnp_card *card = to_pnp_card(dmdev);
147 str += sprintf(str,"%s\n", card->name);
148 return (str - buf);
151 static DEVICE_ATTR(name,S_IRUGO,pnp_show_card_name,NULL);
153 static ssize_t pnp_show_card_ids(struct device *dmdev, char *buf)
155 char *str = buf;
156 struct pnp_card *card = to_pnp_card(dmdev);
157 struct pnp_id * pos = card->id;
159 while (pos) {
160 str += sprintf(str,"%s\n", pos->id);
161 pos = pos->next;
163 return (str - buf);
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);
172 return 0;
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)
182 int error;
183 struct list_head * pos, * temp;
184 if (!card || !card->protocol)
185 return -EINVAL;
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);
193 if (error == 0) {
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);
213 } else
214 pnp_err("sysfs failure, card '%s' will be unavailable", card->dev.bus_id);
215 return error;
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;
226 if (!card)
227 return;
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)
248 return -EINVAL;
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);
254 dev->card = card;
255 list_add_tail(&dev->card_list, &card->devices);
256 spin_unlock(&pnp_lock);
257 return 0;
261 * pnp_remove_card_device- removes a device from the specified card
262 * @card: pointer to the card to remove from
263 * @dev: pointer to the device to remove
266 void pnp_remove_card_device(struct pnp_dev * dev)
268 spin_lock(&pnp_lock);
269 dev->card = NULL;
270 list_del(&dev->card_list);
271 spin_unlock(&pnp_lock);
272 __pnp_remove_device(dev);
276 * pnp_request_card_device - Searches for a PnP device under the specified card
277 * @lcard: pointer to the card link, cannot be NULL
278 * @id: pointer to a PnP ID structure that explains the rules for finding the device
279 * @from: Starting place to search from. If NULL it will start from the begining.
282 struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from)
284 struct list_head * pos;
285 struct pnp_dev * dev;
286 struct pnp_card_driver * drv;
287 struct pnp_card * card;
288 if (!clink || !id)
289 goto done;
290 card = clink->card;
291 drv = clink->driver;
292 if (!from) {
293 pos = card->devices.next;
294 } else {
295 if (from->card != card)
296 goto done;
297 pos = from->card_list.next;
299 while (pos != &card->devices) {
300 dev = card_to_pnp_dev(pos);
301 if ((!dev->card_link) && compare_pnp_id(dev->id,id))
302 goto found;
303 pos = pos->next;
306 done:
307 return NULL;
309 found:
310 down_write(&dev->dev.bus->subsys.rwsem);
311 dev->card_link = clink;
312 dev->dev.driver = &drv->link.driver;
313 if (drv->link.driver.probe) {
314 if (drv->link.driver.probe(&dev->dev)) {
315 dev->dev.driver = NULL;
316 return NULL;
319 device_bind_driver(&dev->dev);
320 up_write(&dev->dev.bus->subsys.rwsem);
322 return dev;
326 * pnp_release_card_device - call this when the driver no longer needs the device
327 * @dev: pointer to the PnP device stucture
330 void pnp_release_card_device(struct pnp_dev * dev)
332 struct pnp_card_driver * drv = dev->card_link->driver;
333 if (!drv)
334 return;
335 down_write(&dev->dev.bus->subsys.rwsem);
336 drv->link.remove = &card_remove;
337 device_release_driver(&dev->dev);
338 drv->link.remove = &card_remove_first;
339 up_write(&dev->dev.bus->subsys.rwsem);
343 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
344 * @drv: pointer to the driver to register
347 int pnp_register_card_driver(struct pnp_card_driver * drv)
349 int count = 0;
350 struct list_head *pos, *temp;
352 drv->link.name = drv->name;
353 drv->link.id_table = NULL; /* this will disable auto matching */
354 drv->link.flags = drv->flags;
355 drv->link.probe = NULL;
356 drv->link.remove = &card_remove_first;
358 spin_lock(&pnp_lock);
359 list_add_tail(&drv->global_list, &pnp_card_drivers);
360 spin_unlock(&pnp_lock);
361 pnp_register_driver(&drv->link);
363 list_for_each_safe(pos,temp,&pnp_cards){
364 struct pnp_card *card = list_entry(pos, struct pnp_card, global_list);
365 count += card_probe(card,drv);
367 return count;
371 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
372 * @drv: pointer to the driver to unregister
375 void pnp_unregister_card_driver(struct pnp_card_driver * drv)
377 spin_lock(&pnp_lock);
378 list_del(&drv->global_list);
379 spin_unlock(&pnp_lock);
380 pnp_unregister_driver(&drv->link);
383 EXPORT_SYMBOL(pnp_add_card);
384 EXPORT_SYMBOL(pnp_remove_card);
385 EXPORT_SYMBOL(pnp_add_card_device);
386 EXPORT_SYMBOL(pnp_remove_card_device);
387 EXPORT_SYMBOL(pnp_add_card_id);
388 EXPORT_SYMBOL(pnp_request_card_device);
389 EXPORT_SYMBOL(pnp_release_card_device);
390 EXPORT_SYMBOL(pnp_register_card_driver);
391 EXPORT_SYMBOL(pnp_unregister_card_driver);