a379a38c196c4c105799ef857389b0ebfb4d9af7
[safe/jmp/linux-2.6] / drivers / pnp / card.c
1 /*
2  * card.c - contains functions for managing groups of PnP devices
3  *
4  * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
5  *
6  */
7
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/pnp.h>
11 #include "base.h"
12
13 LIST_HEAD(pnp_cards);
14 static LIST_HEAD(pnp_card_drivers);
15
16 static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
17                                                    struct pnp_card *card)
18 {
19         const struct pnp_card_device_id *drv_id = drv->id_table;
20         while (*drv_id->id) {
21                 if (compare_pnp_id(card->id, drv_id->id)) {
22                         int i = 0;
23                         for (;;) {
24                                 int found;
25                                 struct pnp_dev *dev;
26                                 if (i == PNP_MAX_DEVICES
27                                     || !*drv_id->devs[i].id)
28                                         return drv_id;
29                                 found = 0;
30                                 card_for_each_dev(card, dev) {
31                                         if (compare_pnp_id
32                                             (dev->id, drv_id->devs[i].id)) {
33                                                 found = 1;
34                                                 break;
35                                         }
36                                 }
37                                 if (!found)
38                                         break;
39                                 i++;
40                         }
41                 }
42                 drv_id++;
43         }
44         return NULL;
45 }
46
47 static void card_remove(struct pnp_dev *dev)
48 {
49         dev->card_link = NULL;
50 }
51
52 static void card_remove_first(struct pnp_dev *dev)
53 {
54         struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
55         if (!dev->card || !drv)
56                 return;
57         if (drv->remove)
58                 drv->remove(dev->card_link);
59         drv->link.remove = &card_remove;
60         kfree(dev->card_link);
61         card_remove(dev);
62 }
63
64 static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
65 {
66         const struct pnp_card_device_id *id;
67         struct pnp_card_link *clink;
68         struct pnp_dev *dev;
69
70         if (!drv->probe)
71                 return 0;
72         id = match_card(drv, card);
73         if (!id)
74                 return 0;
75
76         clink = pnp_alloc(sizeof(*clink));
77         if (!clink)
78                 return 0;
79         clink->card = card;
80         clink->driver = drv;
81         clink->pm_state = PMSG_ON;
82
83         if (drv->probe(clink, id) >= 0)
84                 return 1;
85
86         /* Recovery */
87         card_for_each_dev(card, dev) {
88                 if (dev->card_link == clink)
89                         pnp_release_card_device(dev);
90         }
91         kfree(clink);
92         return 0;
93 }
94
95 /**
96  * pnp_add_card_id - adds an EISA id to the specified card
97  * @id: pointer to a pnp_id structure
98  * @card: pointer to the desired card
99  *
100  */
101
102 int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card)
103 {
104         struct pnp_id *ptr;
105         if (!id)
106                 return -EINVAL;
107         if (!card)
108                 return -EINVAL;
109         id->next = NULL;
110         ptr = card->id;
111         while (ptr && ptr->next)
112                 ptr = ptr->next;
113         if (ptr)
114                 ptr->next = id;
115         else
116                 card->id = id;
117         return 0;
118 }
119
120 static void pnp_free_card_ids(struct pnp_card *card)
121 {
122         struct pnp_id *id;
123         struct pnp_id *next;
124         if (!card)
125                 return;
126         id = card->id;
127         while (id) {
128                 next = id->next;
129                 kfree(id);
130                 id = next;
131         }
132 }
133
134 static void pnp_release_card(struct device *dmdev)
135 {
136         struct pnp_card *card = to_pnp_card(dmdev);
137         pnp_free_card_ids(card);
138         kfree(card);
139 }
140
141 static ssize_t pnp_show_card_name(struct device *dmdev,
142                                   struct device_attribute *attr, char *buf)
143 {
144         char *str = buf;
145         struct pnp_card *card = to_pnp_card(dmdev);
146         str += sprintf(str, "%s\n", card->name);
147         return (str - buf);
148 }
149
150 static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
151
152 static ssize_t pnp_show_card_ids(struct device *dmdev,
153                                  struct device_attribute *attr, char *buf)
154 {
155         char *str = buf;
156         struct pnp_card *card = to_pnp_card(dmdev);
157         struct pnp_id *pos = card->id;
158
159         while (pos) {
160                 str += sprintf(str, "%s\n", pos->id);
161                 pos = pos->next;
162         }
163         return (str - buf);
164 }
165
166 static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
167
168 static int pnp_interface_attach_card(struct pnp_card *card)
169 {
170         int rc = device_create_file(&card->dev, &dev_attr_name);
171         if (rc)
172                 return rc;
173
174         rc = device_create_file(&card->dev, &dev_attr_card_id);
175         if (rc)
176                 goto err_name;
177
178         return 0;
179
180       err_name:
181         device_remove_file(&card->dev, &dev_attr_name);
182         return rc;
183 }
184
185 /**
186  * pnp_add_card - adds a PnP card to the PnP Layer
187  * @card: pointer to the card to add
188  */
189
190 int pnp_add_card(struct pnp_card *card)
191 {
192         int error;
193         struct list_head *pos, *temp;
194         if (!card || !card->protocol)
195                 return -EINVAL;
196
197         sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number,
198                 card->number);
199         card->dev.parent = &card->protocol->dev;
200         card->dev.bus = NULL;
201         card->dev.release = &pnp_release_card;
202         error = device_register(&card->dev);
203
204         if (error == 0) {
205                 pnp_interface_attach_card(card);
206                 spin_lock(&pnp_lock);
207                 list_add_tail(&card->global_list, &pnp_cards);
208                 list_add_tail(&card->protocol_list, &card->protocol->cards);
209                 spin_unlock(&pnp_lock);
210
211                 /* we wait until now to add devices in order to ensure the drivers
212                  * will be able to use all of the related devices on the card
213                  * without waiting any unresonable length of time */
214                 list_for_each(pos, &card->devices) {
215                         struct pnp_dev *dev = card_to_pnp_dev(pos);
216                         __pnp_add_device(dev);
217                 }
218
219                 /* match with card drivers */
220                 list_for_each_safe(pos, temp, &pnp_card_drivers) {
221                         struct pnp_card_driver *drv =
222                             list_entry(pos, struct pnp_card_driver,
223                                        global_list);
224                         card_probe(card, drv);
225                 }
226         } else
227                 pnp_err("sysfs failure, card '%s' will be unavailable",
228                         card->dev.bus_id);
229         return error;
230 }
231
232 /**
233  * pnp_remove_card - removes a PnP card from the PnP Layer
234  * @card: pointer to the card to remove
235  */
236
237 void pnp_remove_card(struct pnp_card *card)
238 {
239         struct list_head *pos, *temp;
240         if (!card)
241                 return;
242         device_unregister(&card->dev);
243         spin_lock(&pnp_lock);
244         list_del(&card->global_list);
245         list_del(&card->protocol_list);
246         spin_unlock(&pnp_lock);
247         list_for_each_safe(pos, temp, &card->devices) {
248                 struct pnp_dev *dev = card_to_pnp_dev(pos);
249                 pnp_remove_card_device(dev);
250         }
251 }
252
253 /**
254  * pnp_add_card_device - adds a device to the specified card
255  * @card: pointer to the card to add to
256  * @dev: pointer to the device to add
257  */
258
259 int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
260 {
261         if (!card || !dev || !dev->protocol)
262                 return -EINVAL;
263         dev->dev.parent = &card->dev;
264         dev->card_link = NULL;
265         snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x",
266                  dev->protocol->number, card->number, dev->number);
267         spin_lock(&pnp_lock);
268         dev->card = card;
269         list_add_tail(&dev->card_list, &card->devices);
270         spin_unlock(&pnp_lock);
271         return 0;
272 }
273
274 /**
275  * pnp_remove_card_device- removes a device from the specified card
276  * @dev: pointer to the device to remove
277  */
278
279 void pnp_remove_card_device(struct pnp_dev *dev)
280 {
281         spin_lock(&pnp_lock);
282         dev->card = NULL;
283         list_del(&dev->card_list);
284         spin_unlock(&pnp_lock);
285         __pnp_remove_device(dev);
286 }
287
288 /**
289  * pnp_request_card_device - Searches for a PnP device under the specified card
290  * @clink: pointer to the card link, cannot be NULL
291  * @id: pointer to a PnP ID structure that explains the rules for finding the device
292  * @from: Starting place to search from. If NULL it will start from the begining.
293  */
294
295 struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
296                                         const char *id, struct pnp_dev *from)
297 {
298         struct list_head *pos;
299         struct pnp_dev *dev;
300         struct pnp_card_driver *drv;
301         struct pnp_card *card;
302         if (!clink || !id)
303                 goto done;
304         card = clink->card;
305         drv = clink->driver;
306         if (!from) {
307                 pos = card->devices.next;
308         } else {
309                 if (from->card != card)
310                         goto done;
311                 pos = from->card_list.next;
312         }
313         while (pos != &card->devices) {
314                 dev = card_to_pnp_dev(pos);
315                 if ((!dev->card_link) && compare_pnp_id(dev->id, id))
316                         goto found;
317                 pos = pos->next;
318         }
319
320       done:
321         return NULL;
322
323       found:
324         dev->card_link = clink;
325         dev->dev.driver = &drv->link.driver;
326         if (pnp_bus_type.probe(&dev->dev))
327                 goto err_out;
328         if (device_bind_driver(&dev->dev))
329                 goto err_out;
330
331         return dev;
332
333       err_out:
334         dev->dev.driver = NULL;
335         dev->card_link = NULL;
336         return NULL;
337 }
338
339 /**
340  * pnp_release_card_device - call this when the driver no longer needs the device
341  * @dev: pointer to the PnP device stucture
342  */
343
344 void pnp_release_card_device(struct pnp_dev *dev)
345 {
346         struct pnp_card_driver *drv = dev->card_link->driver;
347         if (!drv)
348                 return;
349         drv->link.remove = &card_remove;
350         device_release_driver(&dev->dev);
351         drv->link.remove = &card_remove_first;
352 }
353
354 /*
355  * suspend/resume callbacks
356  */
357 static int card_suspend(struct pnp_dev *dev, pm_message_t state)
358 {
359         struct pnp_card_link *link = dev->card_link;
360         if (link->pm_state.event == state.event)
361                 return 0;
362         link->pm_state = state;
363         return link->driver->suspend(link, state);
364 }
365
366 static int card_resume(struct pnp_dev *dev)
367 {
368         struct pnp_card_link *link = dev->card_link;
369         if (link->pm_state.event == PM_EVENT_ON)
370                 return 0;
371         link->pm_state = PMSG_ON;
372         link->driver->resume(link);
373         return 0;
374 }
375
376 /**
377  * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
378  * @drv: pointer to the driver to register
379  */
380
381 int pnp_register_card_driver(struct pnp_card_driver *drv)
382 {
383         int error;
384         struct list_head *pos, *temp;
385
386         drv->link.name = drv->name;
387         drv->link.id_table = NULL;      /* this will disable auto matching */
388         drv->link.flags = drv->flags;
389         drv->link.probe = NULL;
390         drv->link.remove = &card_remove_first;
391         drv->link.suspend = drv->suspend ? card_suspend : NULL;
392         drv->link.resume = drv->resume ? card_resume : NULL;
393
394         error = pnp_register_driver(&drv->link);
395         if (error < 0)
396                 return error;
397
398         spin_lock(&pnp_lock);
399         list_add_tail(&drv->global_list, &pnp_card_drivers);
400         spin_unlock(&pnp_lock);
401
402         list_for_each_safe(pos, temp, &pnp_cards) {
403                 struct pnp_card *card =
404                     list_entry(pos, struct pnp_card, global_list);
405                 card_probe(card, drv);
406         }
407         return 0;
408 }
409
410 /**
411  * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
412  * @drv: pointer to the driver to unregister
413  */
414
415 void pnp_unregister_card_driver(struct pnp_card_driver *drv)
416 {
417         spin_lock(&pnp_lock);
418         list_del(&drv->global_list);
419         spin_unlock(&pnp_lock);
420         pnp_unregister_driver(&drv->link);
421 }
422
423 #if 0
424 EXPORT_SYMBOL(pnp_add_card);
425 EXPORT_SYMBOL(pnp_remove_card);
426 EXPORT_SYMBOL(pnp_add_card_device);
427 EXPORT_SYMBOL(pnp_remove_card_device);
428 EXPORT_SYMBOL(pnp_add_card_id);
429 #endif /*  0  */
430 EXPORT_SYMBOL(pnp_request_card_device);
431 EXPORT_SYMBOL(pnp_release_card_device);
432 EXPORT_SYMBOL(pnp_register_card_driver);
433 EXPORT_SYMBOL(pnp_unregister_card_driver);