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