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