Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * driver.c - device id matching, driver model, etc. |
| 3 | * |
| 4 | * Copyright 2002 Adam Belay <ambx1@neo.rr.com> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/config.h> |
| 9 | #include <linux/string.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/ctype.h> |
| 13 | #include <linux/slab.h> |
| 14 | |
| 15 | #ifdef CONFIG_PNP_DEBUG |
| 16 | #define DEBUG |
| 17 | #else |
| 18 | #undef DEBUG |
| 19 | #endif |
| 20 | |
| 21 | #include <linux/pnp.h> |
| 22 | #include "base.h" |
| 23 | |
| 24 | static int compare_func(const char *ida, const char *idb) |
| 25 | { |
| 26 | int i; |
| 27 | /* we only need to compare the last 4 chars */ |
| 28 | for (i=3; i<7; i++) |
| 29 | { |
| 30 | if (ida[i] != 'X' && |
| 31 | idb[i] != 'X' && |
| 32 | toupper(ida[i]) != toupper(idb[i])) |
| 33 | return 0; |
| 34 | } |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | int compare_pnp_id(struct pnp_id *pos, const char *id) |
| 39 | { |
| 40 | if (!pos || !id || (strlen(id) != 7)) |
| 41 | return 0; |
| 42 | if (memcmp(id,"ANYDEVS",7)==0) |
| 43 | return 1; |
| 44 | while (pos){ |
| 45 | if (memcmp(pos->id,id,3)==0) |
| 46 | if (compare_func(pos->id,id)==1) |
| 47 | return 1; |
| 48 | pos = pos->next; |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static const struct pnp_device_id * match_device(struct pnp_driver *drv, struct pnp_dev *dev) |
| 54 | { |
| 55 | const struct pnp_device_id *drv_id = drv->id_table; |
| 56 | if (!drv_id) |
| 57 | return NULL; |
| 58 | |
| 59 | while (*drv_id->id) { |
| 60 | if (compare_pnp_id(dev->id, drv_id->id)) |
| 61 | return drv_id; |
| 62 | drv_id++; |
| 63 | } |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | int pnp_device_attach(struct pnp_dev *pnp_dev) |
| 68 | { |
| 69 | spin_lock(&pnp_lock); |
| 70 | if(pnp_dev->status != PNP_READY){ |
| 71 | spin_unlock(&pnp_lock); |
| 72 | return -EBUSY; |
| 73 | } |
| 74 | pnp_dev->status = PNP_ATTACHED; |
| 75 | spin_unlock(&pnp_lock); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | void pnp_device_detach(struct pnp_dev *pnp_dev) |
| 80 | { |
| 81 | spin_lock(&pnp_lock); |
| 82 | if (pnp_dev->status == PNP_ATTACHED) |
| 83 | pnp_dev->status = PNP_READY; |
| 84 | spin_unlock(&pnp_lock); |
| 85 | pnp_disable_dev(pnp_dev); |
| 86 | } |
| 87 | |
| 88 | static int pnp_device_probe(struct device *dev) |
| 89 | { |
| 90 | int error; |
| 91 | struct pnp_driver *pnp_drv; |
| 92 | struct pnp_dev *pnp_dev; |
| 93 | const struct pnp_device_id *dev_id = NULL; |
| 94 | pnp_dev = to_pnp_dev(dev); |
| 95 | pnp_drv = to_pnp_driver(dev->driver); |
| 96 | |
| 97 | pnp_dbg("match found with the PnP device '%s' and the driver '%s'", dev->bus_id,pnp_drv->name); |
| 98 | |
| 99 | error = pnp_device_attach(pnp_dev); |
| 100 | if (error < 0) |
| 101 | return error; |
| 102 | |
| 103 | if (pnp_dev->active == 0) { |
| 104 | if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)) { |
| 105 | error = pnp_activate_dev(pnp_dev); |
| 106 | if (error < 0) |
| 107 | return error; |
| 108 | } |
| 109 | } else if ((pnp_drv->flags & PNP_DRIVER_RES_DISABLE) |
| 110 | == PNP_DRIVER_RES_DISABLE) { |
| 111 | error = pnp_disable_dev(pnp_dev); |
| 112 | if (error < 0) |
| 113 | return error; |
| 114 | } |
| 115 | error = 0; |
| 116 | if (pnp_drv->probe) { |
| 117 | dev_id = match_device(pnp_drv, pnp_dev); |
| 118 | if (dev_id != NULL) |
| 119 | error = pnp_drv->probe(pnp_dev, dev_id); |
| 120 | } |
| 121 | if (error >= 0){ |
| 122 | pnp_dev->driver = pnp_drv; |
| 123 | error = 0; |
| 124 | } else |
| 125 | goto fail; |
| 126 | return error; |
| 127 | |
| 128 | fail: |
| 129 | pnp_device_detach(pnp_dev); |
| 130 | return error; |
| 131 | } |
| 132 | |
| 133 | static int pnp_device_remove(struct device *dev) |
| 134 | { |
| 135 | struct pnp_dev * pnp_dev = to_pnp_dev(dev); |
| 136 | struct pnp_driver * drv = pnp_dev->driver; |
| 137 | |
| 138 | if (drv) { |
| 139 | if (drv->remove) |
| 140 | drv->remove(pnp_dev); |
| 141 | pnp_dev->driver = NULL; |
| 142 | } |
| 143 | pnp_device_detach(pnp_dev); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int pnp_bus_match(struct device *dev, struct device_driver *drv) |
| 148 | { |
| 149 | struct pnp_dev * pnp_dev = to_pnp_dev(dev); |
| 150 | struct pnp_driver * pnp_drv = to_pnp_driver(drv); |
| 151 | if (match_device(pnp_drv, pnp_dev) == NULL) |
| 152 | return 0; |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | struct bus_type pnp_bus_type = { |
| 158 | .name = "pnp", |
| 159 | .match = pnp_bus_match, |
| 160 | }; |
| 161 | |
| 162 | |
mochel@digitalimplant.org | 8d618af | 2005-03-21 11:07:54 -0800 | [diff] [blame^] | 163 | static int count_devices(struct device * dev, void * c) |
| 164 | { |
| 165 | int * count = c; |
| 166 | (*count)++; |
| 167 | return 0; |
| 168 | } |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | int pnp_register_driver(struct pnp_driver *drv) |
| 171 | { |
| 172 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | pnp_dbg("the driver '%s' has been registered", drv->name); |
| 175 | |
| 176 | drv->driver.name = drv->name; |
| 177 | drv->driver.bus = &pnp_bus_type; |
| 178 | drv->driver.probe = pnp_device_probe; |
| 179 | drv->driver.remove = pnp_device_remove; |
| 180 | |
| 181 | count = driver_register(&drv->driver); |
| 182 | |
| 183 | /* get the number of initial matches */ |
| 184 | if (count >= 0){ |
| 185 | count = 0; |
mochel@digitalimplant.org | 8d618af | 2005-03-21 11:07:54 -0800 | [diff] [blame^] | 186 | driver_for_each_device(&drv->driver, NULL, &count, count_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | return count; |
| 189 | } |
| 190 | |
| 191 | void pnp_unregister_driver(struct pnp_driver *drv) |
| 192 | { |
| 193 | driver_unregister(&drv->driver); |
| 194 | pnp_dbg("the driver '%s' has been unregistered", drv->name); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * pnp_add_id - adds an EISA id to the specified device |
| 199 | * @id: pointer to a pnp_id structure |
| 200 | * @dev: pointer to the desired device |
| 201 | * |
| 202 | */ |
| 203 | |
| 204 | int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev) |
| 205 | { |
| 206 | struct pnp_id *ptr; |
| 207 | if (!id) |
| 208 | return -EINVAL; |
| 209 | if (!dev) |
| 210 | return -EINVAL; |
| 211 | id->next = NULL; |
| 212 | ptr = dev->id; |
| 213 | while (ptr && ptr->next) |
| 214 | ptr = ptr->next; |
| 215 | if (ptr) |
| 216 | ptr->next = id; |
| 217 | else |
| 218 | dev->id = id; |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | EXPORT_SYMBOL(pnp_register_driver); |
| 223 | EXPORT_SYMBOL(pnp_unregister_driver); |
| 224 | EXPORT_SYMBOL(pnp_add_id); |
| 225 | EXPORT_SYMBOL(pnp_device_attach); |
| 226 | EXPORT_SYMBOL(pnp_device_detach); |