blob: a379a38c196c4c105799ef857389b0ebfb4d9af7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * card.c - contains functions for managing groups of PnP devices
3 *
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
5 *
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/pnp.h>
11#include "base.h"
12
13LIST_HEAD(pnp_cards);
Adrian Bunkb449f632005-11-07 01:01:48 -080014static LIST_HEAD(pnp_card_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070016static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
17 struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070019 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 int i = 0;
23 for (;;) {
24 int found;
25 struct pnp_dev *dev;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070026 if (i == PNP_MAX_DEVICES
27 || !*drv_id->devs[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return drv_id;
29 found = 0;
30 card_for_each_dev(card, dev) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070031 if (compare_pnp_id
32 (dev->id, drv_id->devs[i].id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 found = 1;
34 break;
35 }
36 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070037 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 break;
39 i++;
40 }
41 }
42 drv_id++;
43 }
44 return NULL;
45}
46
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070047static void card_remove(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 dev->card_link = NULL;
50}
Bjorn Helgaas982c6092006-03-27 01:17:08 -080051
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070052static void card_remove_first(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070054 struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 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
Jesper Juhla9adb8d2006-06-25 05:47:17 -070064static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Jesper Juhla9adb8d2006-06-25 05:47:17 -070066 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;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070072 id = match_card(drv, card);
Jesper Juhla9adb8d2006-06-25 05:47:17 -070073 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Jesper Juhla9adb8d2006-06-25 05:47:17 -070091 kfree(clink);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700102int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700104 struct pnp_id *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700120static void pnp_free_card_ids(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700122 struct pnp_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700136 struct pnp_card *card = to_pnp_card(dmdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 pnp_free_card_ids(card);
138 kfree(card);
139}
140
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700141static ssize_t pnp_show_card_name(struct device *dmdev,
142 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 char *str = buf;
145 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700146 str += sprintf(str, "%s\n", card->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return (str - buf);
148}
149
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700150static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700152static ssize_t pnp_show_card_ids(struct device *dmdev,
153 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 char *str = buf;
156 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700157 struct pnp_id *pos = card->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 while (pos) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700160 str += sprintf(str, "%s\n", pos->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 pos = pos->next;
162 }
163 return (str - buf);
164}
165
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700166static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168static int pnp_interface_attach_card(struct pnp_card *card)
169{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700170 int rc = device_create_file(&card->dev, &dev_attr_name);
171 if (rc)
172 return rc;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800173
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700174 rc = device_create_file(&card->dev, &dev_attr_card_id);
175 if (rc)
176 goto err_name;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 0;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800179
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700180 err_name:
181 device_remove_file(&card->dev, &dev_attr_name);
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800182 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/**
186 * pnp_add_card - adds a PnP card to the PnP Layer
187 * @card: pointer to the card to add
188 */
189
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700190int pnp_add_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 int error;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700193 struct list_head *pos, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (!card || !card->protocol)
195 return -EINVAL;
196
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700197 sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number,
198 card->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700214 list_for_each(pos, &card->devices) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct pnp_dev *dev = card_to_pnp_dev(pos);
216 __pnp_add_device(dev);
217 }
218
219 /* match with card drivers */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700220 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 } else
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700227 pnp_err("sysfs failure, card '%s' will be unavailable",
228 card->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700237void pnp_remove_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
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);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700247 list_for_each_safe(pos, temp, &card->devices) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700259int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 if (!card || !dev || !dev->protocol)
262 return -EINVAL;
263 dev->dev.parent = &card->dev;
264 dev->card_link = NULL;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700265 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x",
266 dev->protocol->number, card->number, dev->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * @dev: pointer to the device to remove
277 */
278
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700279void pnp_remove_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
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
Martin Waitz3d410882005-06-23 22:05:21 -0700290 * @clink: pointer to the card link, cannot be NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * @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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700295struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
296 const char *id, struct pnp_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700298 struct list_head *pos;
299 struct pnp_dev *dev;
300 struct pnp_card_driver *drv;
301 struct pnp_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 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);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700315 if ((!dev->card_link) && compare_pnp_id(dev->id, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 goto found;
317 pos = pos->next;
318 }
319
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700320 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return NULL;
322
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700323 found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 dev->card_link = clink;
325 dev->dev.driver = &drv->link.driver;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800326 if (pnp_bus_type.probe(&dev->dev))
327 goto err_out;
328 if (device_bind_driver(&dev->dev))
329 goto err_out;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return dev;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800332
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700333 err_out:
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800334 dev->dev.driver = NULL;
335 dev->card_link = NULL;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800336 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700344void pnp_release_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700346 struct pnp_card_driver *drv = dev->card_link->driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (!drv)
348 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 drv->link.remove = &card_remove;
350 device_release_driver(&dev->dev);
351 drv->link.remove = &card_remove_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100354/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/**
377 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
378 * @drv: pointer to the driver to register
379 */
380
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700381int pnp_register_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800383 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 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;
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100391 drv->link.suspend = drv->suspend ? card_suspend : NULL;
392 drv->link.resume = drv->resume ? card_resume : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800394 error = pnp_register_driver(&drv->link);
395 if (error < 0)
396 return error;
Adam Belayd1d051b22006-01-20 09:29:27 +0100397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_lock(&pnp_lock);
399 list_add_tail(&drv->global_list, &pnp_card_drivers);
400 spin_unlock(&pnp_lock);
Adam Belayd1d051b22006-01-20 09:29:27 +0100401
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700402 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
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
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700415void pnp_unregister_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 spin_lock(&pnp_lock);
418 list_del(&drv->global_list);
419 spin_unlock(&pnp_lock);
420 pnp_unregister_driver(&drv->link);
421}
422
Adrian Bunkb449f632005-11-07 01:01:48 -0800423#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424EXPORT_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);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700429#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430EXPORT_SYMBOL(pnp_request_card_device);
431EXPORT_SYMBOL(pnp_release_card_device);
432EXPORT_SYMBOL(pnp_register_card_driver);
433EXPORT_SYMBOL(pnp_unregister_card_driver);