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