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