blob: 3b48aef8f4ebeeaa345a17e82e1ff0a6f38f7b0a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/pnp.h>
10#include "base.h"
11
12LIST_HEAD(pnp_cards);
Adrian Bunkb449f632005-11-07 01:01:48 -080013static LIST_HEAD(pnp_card_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070015static const struct pnp_card_device_id *match_card(struct pnp_card_driver *drv,
16 struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070018 const struct pnp_card_device_id *drv_id = drv->id_table;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070019
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070020 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;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 for (;;) {
25 int found;
26 struct pnp_dev *dev;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070027
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -060028 if (i == PNP_MAX_DEVICES ||
29 !*drv_id->devs[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return drv_id;
31 found = 0;
32 card_for_each_dev(card, dev) {
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -060033 if (compare_pnp_id(dev->id,
34 drv_id->devs[i].id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 found = 1;
36 break;
37 }
38 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070039 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 break;
41 i++;
42 }
43 }
44 drv_id++;
45 }
46 return NULL;
47}
48
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070049static void card_remove(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 dev->card_link = NULL;
52}
Bjorn Helgaas982c6092006-03-27 01:17:08 -080053
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070054static void card_remove_first(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070056 struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (!dev->card || !drv)
59 return;
60 if (drv->remove)
61 drv->remove(dev->card_link);
62 drv->link.remove = &card_remove;
63 kfree(dev->card_link);
64 card_remove(dev);
65}
66
Jesper Juhla9adb8d2006-06-25 05:47:17 -070067static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Jesper Juhla9adb8d2006-06-25 05:47:17 -070069 const struct pnp_card_device_id *id;
70 struct pnp_card_link *clink;
71 struct pnp_dev *dev;
72
73 if (!drv->probe)
74 return 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070075 id = match_card(drv, card);
Jesper Juhla9adb8d2006-06-25 05:47:17 -070076 if (!id)
77 return 0;
78
79 clink = pnp_alloc(sizeof(*clink));
80 if (!clink)
81 return 0;
82 clink->card = card;
83 clink->driver = drv;
84 clink->pm_state = PMSG_ON;
85
86 if (drv->probe(clink, id) >= 0)
87 return 1;
88
89 /* Recovery */
90 card_for_each_dev(card, dev) {
91 if (dev->card_link == clink)
92 pnp_release_card_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Jesper Juhla9adb8d2006-06-25 05:47:17 -070094 kfree(clink);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return 0;
96}
97
98/**
99 * pnp_add_card_id - adds an EISA id to the specified card
100 * @id: pointer to a pnp_id structure
101 * @card: pointer to the desired card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700103int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700105 struct pnp_id *ptr;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 id->next = NULL;
108 ptr = card->id;
109 while (ptr && ptr->next)
110 ptr = ptr->next;
111 if (ptr)
112 ptr->next = id;
113 else
114 card->id = id;
115 return 0;
116}
117
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700118static void pnp_free_card_ids(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700120 struct pnp_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct pnp_id *next;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 id = card->id;
124 while (id) {
125 next = id->next;
126 kfree(id);
127 id = next;
128 }
129}
130
131static void pnp_release_card(struct device *dmdev)
132{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700133 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 pnp_free_card_ids(card);
136 kfree(card);
137}
138
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700139static ssize_t pnp_show_card_name(struct device *dmdev,
140 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 char *str = buf;
143 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700144
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700145 str += sprintf(str, "%s\n", card->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return (str - buf);
147}
148
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700149static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700151static ssize_t pnp_show_card_ids(struct device *dmdev,
152 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 char *str = buf;
155 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700156 struct pnp_id *pos = card->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 while (pos) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700159 str += sprintf(str, "%s\n", pos->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 pos = pos->next;
161 }
162 return (str - buf);
163}
164
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700165static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167static int pnp_interface_attach_card(struct pnp_card *card)
168{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700169 int rc = device_create_file(&card->dev, &dev_attr_name);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700170
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700171 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 Helgaas1e0aa9a2007-08-15 10:32:08 -0600180err_name:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700181 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700189int pnp_add_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int error;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700192 struct list_head *pos, *temp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700193
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700194 sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number,
195 card->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 card->dev.parent = &card->protocol->dev;
197 card->dev.bus = NULL;
198 card->dev.release = &pnp_release_card;
199 error = device_register(&card->dev);
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700200 if (error) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700201 pnp_err("sysfs failure, card '%s' will be unavailable",
202 card->dev.bus_id);
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700203 return error;
204 }
205
206 pnp_interface_attach_card(card);
207 spin_lock(&pnp_lock);
208 list_add_tail(&card->global_list, &pnp_cards);
209 list_add_tail(&card->protocol_list, &card->protocol->cards);
210 spin_unlock(&pnp_lock);
211
212 /* we wait until now to add devices in order to ensure the drivers
213 * will be able to use all of the related devices on the card
214 * without waiting an unreasonable length of time */
215 list_for_each(pos, &card->devices) {
216 struct pnp_dev *dev = card_to_pnp_dev(pos);
217 __pnp_add_device(dev);
218 }
219
220 /* match with card drivers */
221 list_for_each_safe(pos, temp, &pnp_card_drivers) {
222 struct pnp_card_driver *drv =
223 list_entry(pos, struct pnp_card_driver,
224 global_list);
225 card_probe(card, drv);
226 }
227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230/**
231 * pnp_remove_card - removes a PnP card from the PnP Layer
232 * @card: pointer to the card to remove
233 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700234void pnp_remove_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 struct list_head *pos, *temp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 device_unregister(&card->dev);
239 spin_lock(&pnp_lock);
240 list_del(&card->global_list);
241 list_del(&card->protocol_list);
242 spin_unlock(&pnp_lock);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700243 list_for_each_safe(pos, temp, &card->devices) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct pnp_dev *dev = card_to_pnp_dev(pos);
245 pnp_remove_card_device(dev);
246 }
247}
248
249/**
250 * pnp_add_card_device - adds a device to the specified card
251 * @card: pointer to the card to add to
252 * @dev: pointer to the device to add
253 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700254int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 dev->dev.parent = &card->dev;
257 dev->card_link = NULL;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700258 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x",
259 dev->protocol->number, card->number, dev->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 spin_lock(&pnp_lock);
261 dev->card = card;
262 list_add_tail(&dev->card_list, &card->devices);
263 spin_unlock(&pnp_lock);
264 return 0;
265}
266
267/**
268 * pnp_remove_card_device- removes a device from the specified card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * @dev: pointer to the device to remove
270 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700271void pnp_remove_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 spin_lock(&pnp_lock);
274 dev->card = NULL;
275 list_del(&dev->card_list);
276 spin_unlock(&pnp_lock);
277 __pnp_remove_device(dev);
278}
279
280/**
281 * pnp_request_card_device - Searches for a PnP device under the specified card
Martin Waitz3d410882005-06-23 22:05:21 -0700282 * @clink: pointer to the card link, cannot be NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * @id: pointer to a PnP ID structure that explains the rules for finding the device
284 * @from: Starting place to search from. If NULL it will start from the begining.
285 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700286struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
287 const char *id, struct pnp_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700289 struct list_head *pos;
290 struct pnp_dev *dev;
291 struct pnp_card_driver *drv;
292 struct pnp_card *card;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!clink || !id)
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700295 return NULL;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 card = clink->card;
298 drv = clink->driver;
299 if (!from) {
300 pos = card->devices.next;
301 } else {
302 if (from->card != card)
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700303 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 pos = from->card_list.next;
305 }
306 while (pos != &card->devices) {
307 dev = card_to_pnp_dev(pos);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700308 if ((!dev->card_link) && compare_pnp_id(dev->id, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto found;
310 pos = pos->next;
311 }
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return NULL;
314
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600315found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 dev->card_link = clink;
317 dev->dev.driver = &drv->link.driver;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800318 if (pnp_bus_type.probe(&dev->dev))
319 goto err_out;
320 if (device_bind_driver(&dev->dev))
321 goto err_out;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return dev;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800324
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600325err_out:
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800326 dev->dev.driver = NULL;
327 dev->card_link = NULL;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800328 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/**
332 * pnp_release_card_device - call this when the driver no longer needs the device
333 * @dev: pointer to the PnP device stucture
334 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700335void pnp_release_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700337 struct pnp_card_driver *drv = dev->card_link->driver;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 drv->link.remove = &card_remove;
340 device_release_driver(&dev->dev);
341 drv->link.remove = &card_remove_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100344/*
345 * suspend/resume callbacks
346 */
347static int card_suspend(struct pnp_dev *dev, pm_message_t state)
348{
349 struct pnp_card_link *link = dev->card_link;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700350
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100351 if (link->pm_state.event == state.event)
352 return 0;
353 link->pm_state = state;
354 return link->driver->suspend(link, state);
355}
356
357static int card_resume(struct pnp_dev *dev)
358{
359 struct pnp_card_link *link = dev->card_link;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700360
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100361 if (link->pm_state.event == PM_EVENT_ON)
362 return 0;
363 link->pm_state = PMSG_ON;
364 link->driver->resume(link);
365 return 0;
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/**
369 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
370 * @drv: pointer to the driver to register
371 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700372int pnp_register_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800374 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct list_head *pos, *temp;
376
377 drv->link.name = drv->name;
378 drv->link.id_table = NULL; /* this will disable auto matching */
379 drv->link.flags = drv->flags;
380 drv->link.probe = NULL;
381 drv->link.remove = &card_remove_first;
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100382 drv->link.suspend = drv->suspend ? card_suspend : NULL;
383 drv->link.resume = drv->resume ? card_resume : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800385 error = pnp_register_driver(&drv->link);
386 if (error < 0)
387 return error;
Adam Belayd1d051b22006-01-20 09:29:27 +0100388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 spin_lock(&pnp_lock);
390 list_add_tail(&drv->global_list, &pnp_card_drivers);
391 spin_unlock(&pnp_lock);
Adam Belayd1d051b22006-01-20 09:29:27 +0100392
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700393 list_for_each_safe(pos, temp, &pnp_cards) {
394 struct pnp_card *card =
395 list_entry(pos, struct pnp_card, global_list);
396 card_probe(card, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/**
402 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
403 * @drv: pointer to the driver to unregister
404 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700405void pnp_unregister_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 spin_lock(&pnp_lock);
408 list_del(&drv->global_list);
409 spin_unlock(&pnp_lock);
410 pnp_unregister_driver(&drv->link);
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413EXPORT_SYMBOL(pnp_request_card_device);
414EXPORT_SYMBOL(pnp_release_card_device);
415EXPORT_SYMBOL(pnp_register_card_driver);
416EXPORT_SYMBOL(pnp_unregister_card_driver);