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