blob: d606a163b1d73fe539f16f122d8f577cf881da50 [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>
Bjorn Helgaase4366752008-04-28 16:33:56 -06008#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070020
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070021 while (*drv_id->id) {
22 if (compare_pnp_id(card->id, drv_id->id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 int i = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 for (;;) {
26 int found;
27 struct pnp_dev *dev;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070028
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -060029 if (i == PNP_MAX_DEVICES ||
30 !*drv_id->devs[i].id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return drv_id;
32 found = 0;
33 card_for_each_dev(card, dev) {
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -060034 if (compare_pnp_id(dev->id,
35 drv_id->devs[i].id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 found = 1;
37 break;
38 }
39 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070040 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 break;
42 i++;
43 }
44 }
45 drv_id++;
46 }
47 return NULL;
48}
49
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070050static void card_remove(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 dev->card_link = NULL;
53}
Bjorn Helgaas982c6092006-03-27 01:17:08 -080054
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070055static void card_remove_first(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070057 struct pnp_card_driver *drv = to_pnp_card_driver(dev->driver);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!dev->card || !drv)
60 return;
61 if (drv->remove)
62 drv->remove(dev->card_link);
63 drv->link.remove = &card_remove;
64 kfree(dev->card_link);
65 card_remove(dev);
66}
67
Jesper Juhla9adb8d2006-06-25 05:47:17 -070068static int card_probe(struct pnp_card *card, struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Jesper Juhla9adb8d2006-06-25 05:47:17 -070070 const struct pnp_card_device_id *id;
71 struct pnp_card_link *clink;
72 struct pnp_dev *dev;
73
74 if (!drv->probe)
75 return 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070076 id = match_card(drv, card);
Jesper Juhla9adb8d2006-06-25 05:47:17 -070077 if (!id)
78 return 0;
79
80 clink = pnp_alloc(sizeof(*clink));
81 if (!clink)
82 return 0;
83 clink->card = card;
84 clink->driver = drv;
85 clink->pm_state = PMSG_ON;
86
87 if (drv->probe(clink, id) >= 0)
88 return 1;
89
90 /* Recovery */
91 card_for_each_dev(card, dev) {
92 if (dev->card_link == clink)
93 pnp_release_card_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
Jesper Juhla9adb8d2006-06-25 05:47:17 -070095 kfree(clink);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97}
98
99/**
100 * pnp_add_card_id - adds an EISA id to the specified card
101 * @id: pointer to a pnp_id structure
102 * @card: pointer to the desired card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Bjorn Helgaase4366752008-04-28 16:33:56 -0600104struct pnp_id *pnp_add_card_id(struct pnp_card *card, char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Bjorn Helgaase4366752008-04-28 16:33:56 -0600106 struct pnp_id *dev_id, *ptr;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700107
Bjorn Helgaase4366752008-04-28 16:33:56 -0600108 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
109 if (!dev_id)
110 return NULL;
111
112 dev_id->id[0] = id[0];
113 dev_id->id[1] = id[1];
114 dev_id->id[2] = id[2];
115 dev_id->id[3] = tolower(id[3]);
116 dev_id->id[4] = tolower(id[4]);
117 dev_id->id[5] = tolower(id[5]);
118 dev_id->id[6] = tolower(id[6]);
119 dev_id->id[7] = '\0';
120
121 dev_id->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 ptr = card->id;
123 while (ptr && ptr->next)
124 ptr = ptr->next;
125 if (ptr)
Bjorn Helgaase4366752008-04-28 16:33:56 -0600126 ptr->next = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 else
Bjorn Helgaase4366752008-04-28 16:33:56 -0600128 card->id = dev_id;
129
130 return dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700133static void pnp_free_card_ids(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700135 struct pnp_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct pnp_id *next;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 id = card->id;
139 while (id) {
140 next = id->next;
141 kfree(id);
142 id = next;
143 }
144}
145
146static void pnp_release_card(struct device *dmdev)
147{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700148 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 pnp_free_card_ids(card);
151 kfree(card);
152}
153
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700154static ssize_t pnp_show_card_name(struct device *dmdev,
155 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 char *str = buf;
158 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700159
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700160 str += sprintf(str, "%s\n", card->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return (str - buf);
162}
163
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700164static DEVICE_ATTR(name, S_IRUGO, pnp_show_card_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700166static ssize_t pnp_show_card_ids(struct device *dmdev,
167 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 char *str = buf;
170 struct pnp_card *card = to_pnp_card(dmdev);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700171 struct pnp_id *pos = card->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 while (pos) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700174 str += sprintf(str, "%s\n", pos->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 pos = pos->next;
176 }
177 return (str - buf);
178}
179
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700180static DEVICE_ATTR(card_id, S_IRUGO, pnp_show_card_ids, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182static int pnp_interface_attach_card(struct pnp_card *card)
183{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700184 int rc = device_create_file(&card->dev, &dev_attr_name);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700185
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700186 if (rc)
187 return rc;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800188
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700189 rc = device_create_file(&card->dev, &dev_attr_card_id);
190 if (rc)
191 goto err_name;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800194
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600195err_name:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700196 device_remove_file(&card->dev, &dev_attr_name);
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800197 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200/**
201 * pnp_add_card - adds a PnP card to the PnP Layer
202 * @card: pointer to the card to add
203 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700204int pnp_add_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int error;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700207 struct list_head *pos, *temp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700208
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700209 sprintf(card->dev.bus_id, "%02x:%02x", card->protocol->number,
210 card->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 card->dev.parent = &card->protocol->dev;
212 card->dev.bus = NULL;
213 card->dev.release = &pnp_release_card;
214 error = device_register(&card->dev);
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700215 if (error) {
Bjorn Helgaasa05d0782007-10-16 23:31:10 -0700216 dev_err(&card->dev, "could not register (err=%d)\n", error);
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700217 return error;
218 }
219
220 pnp_interface_attach_card(card);
221 spin_lock(&pnp_lock);
222 list_add_tail(&card->global_list, &pnp_cards);
223 list_add_tail(&card->protocol_list, &card->protocol->cards);
224 spin_unlock(&pnp_lock);
225
226 /* we wait until now to add devices in order to ensure the drivers
227 * will be able to use all of the related devices on the card
228 * without waiting an unreasonable length of time */
229 list_for_each(pos, &card->devices) {
230 struct pnp_dev *dev = card_to_pnp_dev(pos);
231 __pnp_add_device(dev);
232 }
233
234 /* match with card drivers */
235 list_for_each_safe(pos, temp, &pnp_card_drivers) {
236 struct pnp_card_driver *drv =
237 list_entry(pos, struct pnp_card_driver,
238 global_list);
239 card_probe(card, drv);
240 }
241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244/**
245 * pnp_remove_card - removes a PnP card from the PnP Layer
246 * @card: pointer to the card to remove
247 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700248void pnp_remove_card(struct pnp_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct list_head *pos, *temp;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 device_unregister(&card->dev);
253 spin_lock(&pnp_lock);
254 list_del(&card->global_list);
255 list_del(&card->protocol_list);
256 spin_unlock(&pnp_lock);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700257 list_for_each_safe(pos, temp, &card->devices) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct pnp_dev *dev = card_to_pnp_dev(pos);
259 pnp_remove_card_device(dev);
260 }
261}
262
263/**
264 * pnp_add_card_device - adds a device to the specified card
265 * @card: pointer to the card to add to
266 * @dev: pointer to the device to add
267 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700268int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dev->dev.parent = &card->dev;
271 dev->card_link = NULL;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700272 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "%02x:%02x.%02x",
273 dev->protocol->number, card->number, dev->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 spin_lock(&pnp_lock);
275 dev->card = card;
276 list_add_tail(&dev->card_list, &card->devices);
277 spin_unlock(&pnp_lock);
278 return 0;
279}
280
281/**
282 * pnp_remove_card_device- removes a device from the specified card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * @dev: pointer to the device to remove
284 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700285void pnp_remove_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 spin_lock(&pnp_lock);
288 dev->card = NULL;
289 list_del(&dev->card_list);
290 spin_unlock(&pnp_lock);
291 __pnp_remove_device(dev);
292}
293
294/**
295 * pnp_request_card_device - Searches for a PnP device under the specified card
Martin Waitz3d410882005-06-23 22:05:21 -0700296 * @clink: pointer to the card link, cannot be NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * @id: pointer to a PnP ID structure that explains the rules for finding the device
298 * @from: Starting place to search from. If NULL it will start from the begining.
299 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700300struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
301 const char *id, struct pnp_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700303 struct list_head *pos;
304 struct pnp_dev *dev;
305 struct pnp_card_driver *drv;
306 struct pnp_card *card;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!clink || !id)
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700309 return NULL;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 card = clink->card;
312 drv = clink->driver;
313 if (!from) {
314 pos = card->devices.next;
315 } else {
316 if (from->card != card)
Bjorn Helgaas5bfc43a2007-10-16 23:31:09 -0700317 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 pos = from->card_list.next;
319 }
320 while (pos != &card->devices) {
321 dev = card_to_pnp_dev(pos);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700322 if ((!dev->card_link) && compare_pnp_id(dev->id, id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto found;
324 pos = pos->next;
325 }
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return NULL;
328
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600329found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 dev->card_link = clink;
331 dev->dev.driver = &drv->link.driver;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800332 if (pnp_bus_type.probe(&dev->dev))
333 goto err_out;
334 if (device_bind_driver(&dev->dev))
335 goto err_out;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return dev;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800338
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600339err_out:
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800340 dev->dev.driver = NULL;
341 dev->card_link = NULL;
Jeff Garzikbfc7ee22006-12-06 20:35:33 -0800342 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345/**
346 * pnp_release_card_device - call this when the driver no longer needs the device
347 * @dev: pointer to the PnP device stucture
348 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700349void pnp_release_card_device(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700351 struct pnp_card_driver *drv = dev->card_link->driver;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 drv->link.remove = &card_remove;
354 device_release_driver(&dev->dev);
355 drv->link.remove = &card_remove_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100358/*
359 * suspend/resume callbacks
360 */
361static int card_suspend(struct pnp_dev *dev, pm_message_t state)
362{
363 struct pnp_card_link *link = dev->card_link;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700364
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100365 if (link->pm_state.event == state.event)
366 return 0;
367 link->pm_state = state;
368 return link->driver->suspend(link, state);
369}
370
371static int card_resume(struct pnp_dev *dev)
372{
373 struct pnp_card_link *link = dev->card_link;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700374
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100375 if (link->pm_state.event == PM_EVENT_ON)
376 return 0;
377 link->pm_state = PMSG_ON;
378 link->driver->resume(link);
379 return 0;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/**
383 * pnp_register_card_driver - registers a PnP card driver with the PnP Layer
384 * @drv: pointer to the driver to register
385 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700386int pnp_register_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800388 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct list_head *pos, *temp;
390
391 drv->link.name = drv->name;
392 drv->link.id_table = NULL; /* this will disable auto matching */
393 drv->link.flags = drv->flags;
394 drv->link.probe = NULL;
395 drv->link.remove = &card_remove_first;
Takashi Iwai4c98cfe2005-11-29 09:09:32 +0100396 drv->link.suspend = drv->suspend ? card_suspend : NULL;
397 drv->link.resume = drv->resume ? card_resume : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800399 error = pnp_register_driver(&drv->link);
400 if (error < 0)
401 return error;
Adam Belayd1d051b22006-01-20 09:29:27 +0100402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 spin_lock(&pnp_lock);
404 list_add_tail(&drv->global_list, &pnp_card_drivers);
405 spin_unlock(&pnp_lock);
Adam Belayd1d051b22006-01-20 09:29:27 +0100406
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700407 list_for_each_safe(pos, temp, &pnp_cards) {
408 struct pnp_card *card =
409 list_entry(pos, struct pnp_card, global_list);
410 card_probe(card, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Bjorn Helgaas982c6092006-03-27 01:17:08 -0800412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415/**
416 * pnp_unregister_card_driver - unregisters a PnP card driver from the PnP Layer
417 * @drv: pointer to the driver to unregister
418 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700419void pnp_unregister_card_driver(struct pnp_card_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 spin_lock(&pnp_lock);
422 list_del(&drv->global_list);
423 spin_unlock(&pnp_lock);
424 pnp_unregister_driver(&drv->link);
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427EXPORT_SYMBOL(pnp_request_card_device);
428EXPORT_SYMBOL(pnp_release_card_device);
429EXPORT_SYMBOL(pnp_register_card_driver);
430EXPORT_SYMBOL(pnp_unregister_card_driver);