PNP: Lindent all source files
[deliverable/linux.git] / drivers / pnp / card.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
8#include <linux/module.h>
9#include <linux/slab.h>
1da177e4
LT
10#include <linux/pnp.h>
11#include "base.h"
12
13LIST_HEAD(pnp_cards);
b449f63c 14static LIST_HEAD(pnp_card_drivers);
1da177e4 15
9dd78466
BH
16static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
17 struct pnp_card *card)
1da177e4 18{
9dd78466
BH
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)) {
1da177e4
LT
22 int i = 0;
23 for (;;) {
24 int found;
25 struct pnp_dev *dev;
9dd78466
BH
26 if (i == PNP_MAX_DEVICES
27 || !*drv_id->devs[i].id)
1da177e4
LT
28 return drv_id;
29 found = 0;
30 card_for_each_dev(card, dev) {
9dd78466
BH
31 if (compare_pnp_id
32 (dev->id, drv_id->devs[i].id)) {
1da177e4
LT
33 found = 1;
34 break;
35 }
36 }
9dd78466 37 if (!found)
1da177e4
LT
38 break;
39 i++;
40 }
41 }
42 drv_id++;
43 }
44 return NULL;
45}
46
9dd78466 47static void card_remove(struct pnp_dev *dev)
1da177e4
LT
48{
49 dev->card_link = NULL;
50}
982c6094 51
9dd78466 52static void card_remove_first(struct pnp_dev *dev)
1da177e4 53{
9dd78466 54 struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
1da177e4
LT
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
a9adb8db 64static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
1da177e4 65{
a9adb8db
JJ
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;
9dd78466 72 id = match_card(drv, card);
a9adb8db
JJ
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);
1da177e4 90 }
a9adb8db 91 kfree(clink);
1da177e4
LT
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
9dd78466 102int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card)
1da177e4 103{
9dd78466 104 struct pnp_id *ptr;
1da177e4
LT
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
9dd78466 120static void pnp_free_card_ids(struct pnp_card *card)
1da177e4 121{
9dd78466 122 struct pnp_id *id;
1da177e4
LT
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
134static void pnp_release_card(struct device *dmdev)
135{
9dd78466 136 struct pnp_card *card = to_pnp_card(dmdev);
1da177e4
LT
137 pnp_free_card_ids(card);
138 kfree(card);
139}
140
9dd78466
BH
141static ssize_t pnp_show_card_name(struct device *dmdev,
142 struct device_attribute *attr, char *buf)
1da177e4
LT
143{
144 char *str = buf;
145 struct pnp_card *card = to_pnp_card(dmdev);
9dd78466 146 str += sprintf(str, "%s\n", card->name);
1da177e4
LT
147 return (str - buf);
148}
149
9dd78466 150static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
1da177e4 151
9dd78466
BH
152static ssize_t pnp_show_card_ids(struct device *dmdev,
153 struct device_attribute *attr, char *buf)
1da177e4
LT
154{
155 char *str = buf;
156 struct pnp_card *card = to_pnp_card(dmdev);
9dd78466 157 struct pnp_id *pos = card->id;
1da177e4
LT
158
159 while (pos) {
9dd78466 160 str += sprintf(str, "%s\n", pos->id);
1da177e4
LT
161 pos = pos->next;
162 }
163 return (str - buf);
164}
165
9dd78466 166static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
1da177e4
LT
167
168static int pnp_interface_attach_card(struct pnp_card *card)
169{
9dd78466
BH
170 int rc = device_create_file(&card->dev, &dev_attr_name);
171 if (rc)
172 return rc;
bfc7ee20 173
9dd78466
BH
174 rc = device_create_file(&card->dev, &dev_attr_card_id);
175 if (rc)
176 goto err_name;
bfc7ee20 177
1da177e4 178 return 0;
bfc7ee20 179
9dd78466
BH
180 err_name:
181 device_remove_file(&card->dev, &dev_attr_name);
bfc7ee20 182 return rc;
1da177e4
LT
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
9dd78466 190int pnp_add_card(struct pnp_card *card)
1da177e4
LT
191{
192 int error;
9dd78466 193 struct list_head *pos, *temp;
1da177e4
LT
194 if (!card || !card->protocol)
195 return -EINVAL;
196
9dd78466
BH
197 sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number,
198 card->number);
1da177e4
LT
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 */
9dd78466 214 list_for_each(pos, &card->devices) {
1da177e4
LT
215 struct pnp_dev *dev = card_to_pnp_dev(pos);
216 __pnp_add_device(dev);
217 }
218
219 /* match with card drivers */
9dd78466
BH
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);
1da177e4
LT
225 }
226 } else
9dd78466
BH
227 pnp_err("sysfs failure, card '%s' will be unavailable",
228 card->dev.bus_id);
1da177e4
LT
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
9dd78466 237void pnp_remove_card(struct pnp_card *card)
1da177e4
LT
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);
9dd78466 247 list_for_each_safe(pos, temp, &card->devices) {
1da177e4
LT
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
9dd78466 259int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
1da177e4
LT
260{
261 if (!card || !dev || !dev->protocol)
262 return -EINVAL;
263 dev->dev.parent = &card->dev;
264 dev->card_link = NULL;
9dd78466
BH
265 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x",
266 dev->protocol->number, card->number, dev->number);
1da177e4
LT
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
1da177e4
LT
276 * @dev: pointer to the device to remove
277 */
278
9dd78466 279void pnp_remove_card_device(struct pnp_dev *dev)
1da177e4
LT
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
3d41088f 290 * @clink: pointer to the card link, cannot be NULL
1da177e4
LT
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
9dd78466
BH
295struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
296 const char *id, struct pnp_dev *from)
1da177e4 297{
9dd78466
BH
298 struct list_head *pos;
299 struct pnp_dev *dev;
300 struct pnp_card_driver *drv;
301 struct pnp_card *card;
1da177e4
LT
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);
9dd78466 315 if ((!dev->card_link) && compare_pnp_id(dev->id, id))
1da177e4
LT
316 goto found;
317 pos = pos->next;
318 }
319
9dd78466 320 done:
1da177e4
LT
321 return NULL;
322
9dd78466 323 found:
1da177e4
LT
324 dev->card_link = clink;
325 dev->dev.driver = &drv->link.driver;
bfc7ee20
JG
326 if (pnp_bus_type.probe(&dev->dev))
327 goto err_out;
328 if (device_bind_driver(&dev->dev))
329 goto err_out;
330
1da177e4 331 return dev;
bfc7ee20 332
9dd78466 333 err_out:
bfc7ee20
JG
334 dev->dev.driver = NULL;
335 dev->card_link = NULL;
bfc7ee20 336 return NULL;
1da177e4
LT
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
9dd78466 344void pnp_release_card_device(struct pnp_dev *dev)
1da177e4 345{
9dd78466 346 struct pnp_card_driver *drv = dev->card_link->driver;
1da177e4
LT
347 if (!drv)
348 return;
1da177e4
LT
349 drv->link.remove = &card_remove;
350 device_release_driver(&dev->dev);
351 drv->link.remove = &card_remove_first;
1da177e4
LT
352}
353
4c98cfef
TI
354/*
355 * suspend/resume callbacks
356 */
357static 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
366static 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
1da177e4
LT
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
9dd78466 381int pnp_register_card_driver(struct pnp_card_driver *drv)
1da177e4 382{
982c6094 383 int error;
1da177e4
LT
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;
4c98cfef
TI
391 drv->link.suspend = drv->suspend ? card_suspend : NULL;
392 drv->link.resume = drv->resume ? card_resume : NULL;
1da177e4 393
982c6094
BH
394 error = pnp_register_driver(&drv->link);
395 if (error < 0)
396 return error;
d1d051b2 397
1da177e4
LT
398 spin_lock(&pnp_lock);
399 list_add_tail(&drv->global_list, &pnp_card_drivers);
400 spin_unlock(&pnp_lock);
d1d051b2 401
9dd78466
BH
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);
1da177e4 406 }
982c6094 407 return 0;
1da177e4
LT
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
9dd78466 415void pnp_unregister_card_driver(struct pnp_card_driver *drv)
1da177e4
LT
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
b449f63c 423#if 0
1da177e4
LT
424EXPORT_SYMBOL(pnp_add_card);
425EXPORT_SYMBOL(pnp_remove_card);
426EXPORT_SYMBOL(pnp_add_card_device);
427EXPORT_SYMBOL(pnp_remove_card_device);
428EXPORT_SYMBOL(pnp_add_card_id);
9dd78466 429#endif /* 0 */
1da177e4
LT
430EXPORT_SYMBOL(pnp_request_card_device);
431EXPORT_SYMBOL(pnp_release_card_device);
432EXPORT_SYMBOL(pnp_register_card_driver);
433EXPORT_SYMBOL(pnp_unregister_card_driver);
This page took 0.408695 seconds and 5 git commands to generate.