Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * scan.c - support for transforming the ACPI namespace into individual objects | |
3 | */ | |
4 | ||
5 | #include <linux/module.h> | |
6 | #include <linux/init.h> | |
9b6d97b6 | 7 | #include <linux/kernel.h> |
1da177e4 LT |
8 | #include <linux/acpi.h> |
9 | ||
10 | #include <acpi/acpi_drivers.h> | |
11 | #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */ | |
12 | ||
1da177e4 | 13 | #define _COMPONENT ACPI_BUS_COMPONENT |
f52fd66d | 14 | ACPI_MODULE_NAME("scan"); |
1da177e4 | 15 | #define STRUCT_TO_INT(s) (*((int*)&s)) |
4be44fcd | 16 | extern struct acpi_device *acpi_root; |
1da177e4 LT |
17 | |
18 | #define ACPI_BUS_CLASS "system_bus" | |
29b71a1c | 19 | #define ACPI_BUS_HID "LNXSYBUS" |
1da177e4 LT |
20 | #define ACPI_BUS_DEVICE_NAME "System Bus" |
21 | ||
22 | static LIST_HEAD(acpi_device_list); | |
e49bd2dd | 23 | static LIST_HEAD(acpi_bus_id_list); |
1da177e4 LT |
24 | DEFINE_SPINLOCK(acpi_device_lock); |
25 | LIST_HEAD(acpi_wakeup_device_list); | |
26 | ||
e49bd2dd | 27 | struct acpi_device_bus_id{ |
bb095854 | 28 | char bus_id[15]; |
e49bd2dd ZR |
29 | unsigned int instance_no; |
30 | struct list_head node; | |
1da177e4 | 31 | }; |
29b71a1c TR |
32 | |
33 | /* | |
34 | * Creates hid/cid(s) string needed for modalias and uevent | |
35 | * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: | |
36 | * char *modalias: "acpi:IBM0001:ACPI0001" | |
37 | */ | |
38 | int create_modalias(struct acpi_device *acpi_dev, char *modalias, int size){ | |
39 | ||
40 | int len; | |
41 | ||
42 | if (!acpi_dev->flags.hardware_id) | |
43 | return -ENODEV; | |
44 | ||
45 | len = snprintf(modalias, size, "acpi:%s:", | |
46 | acpi_dev->pnp.hardware_id); | |
47 | if (len < 0 || len >= size) | |
48 | return -EINVAL; | |
49 | size -= len; | |
50 | ||
51 | if (acpi_dev->flags.compatible_ids) { | |
52 | struct acpi_compatible_id_list *cid_list; | |
53 | int i; | |
54 | int count; | |
55 | ||
56 | cid_list = acpi_dev->pnp.cid_list; | |
57 | for (i = 0; i < cid_list->count; i++) { | |
58 | count = snprintf(&modalias[len], size, "%s:", | |
59 | cid_list->id[i].value); | |
60 | if (count < 0 || count >= size) { | |
61 | printk(KERN_ERR "acpi: %s cid[%i] exceeds event buffer size", | |
62 | acpi_dev->pnp.device_name, i); | |
63 | break; | |
64 | } | |
65 | len += count; | |
66 | size -= count; | |
67 | } | |
68 | } | |
69 | ||
70 | modalias[len] = '\0'; | |
71 | return len; | |
72 | } | |
73 | ||
74 | static ssize_t | |
75 | acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) { | |
76 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
77 | int len; | |
78 | ||
79 | /* Device has no HID and no CID or string is >1024 */ | |
80 | len = create_modalias(acpi_dev, buf, 1024); | |
81 | if (len <= 0) | |
82 | return 0; | |
83 | buf[len++] = '\n'; | |
84 | return len; | |
85 | } | |
86 | static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); | |
87 | ||
4be44fcd | 88 | static int acpi_eject_operation(acpi_handle handle, int lockable) |
1da177e4 | 89 | { |
1da177e4 LT |
90 | struct acpi_object_list arg_list; |
91 | union acpi_object arg; | |
92 | acpi_status status = AE_OK; | |
1da177e4 | 93 | |
1da177e4 LT |
94 | /* |
95 | * TBD: evaluate _PS3? | |
96 | */ | |
1da177e4 | 97 | |
1da177e4 LT |
98 | if (lockable) { |
99 | arg_list.count = 1; | |
100 | arg_list.pointer = &arg; | |
101 | arg.type = ACPI_TYPE_INTEGER; | |
102 | arg.integer.value = 0; | |
103 | acpi_evaluate_object(handle, "_LCK", &arg_list, NULL); | |
104 | } | |
1da177e4 | 105 | |
1da177e4 LT |
106 | arg_list.count = 1; |
107 | arg_list.pointer = &arg; | |
108 | arg.type = ACPI_TYPE_INTEGER; | |
109 | arg.integer.value = 1; | |
1da177e4 | 110 | |
1da177e4 LT |
111 | /* |
112 | * TBD: _EJD support. | |
113 | */ | |
1da177e4 | 114 | |
1da177e4 LT |
115 | status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); |
116 | if (ACPI_FAILURE(status)) { | |
4be44fcd | 117 | return (-ENODEV); |
1da177e4 | 118 | } |
1da177e4 | 119 | |
4be44fcd | 120 | return (0); |
1da177e4 LT |
121 | } |
122 | ||
1da177e4 | 123 | static ssize_t |
f883d9db PM |
124 | acpi_eject_store(struct device *d, struct device_attribute *attr, |
125 | const char *buf, size_t count) | |
1da177e4 | 126 | { |
4be44fcd LB |
127 | int result; |
128 | int ret = count; | |
129 | int islockable; | |
130 | acpi_status status; | |
131 | acpi_handle handle; | |
132 | acpi_object_type type = 0; | |
f883d9db | 133 | struct acpi_device *acpi_device = to_acpi_device(d); |
1da177e4 | 134 | |
1da177e4 LT |
135 | if ((!count) || (buf[0] != '1')) { |
136 | return -EINVAL; | |
137 | } | |
1da177e4 | 138 | #ifndef FORCE_EJECT |
f883d9db | 139 | if (acpi_device->driver == NULL) { |
1da177e4 LT |
140 | ret = -ENODEV; |
141 | goto err; | |
142 | } | |
143 | #endif | |
f883d9db PM |
144 | status = acpi_get_type(acpi_device->handle, &type); |
145 | if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) { | |
1da177e4 LT |
146 | ret = -ENODEV; |
147 | goto err; | |
148 | } | |
1da177e4 | 149 | |
f883d9db PM |
150 | islockable = acpi_device->flags.lockable; |
151 | handle = acpi_device->handle; | |
9b6d97b6 | 152 | |
f883d9db | 153 | result = acpi_bus_trim(acpi_device, 1); |
1da177e4 | 154 | |
1da177e4 LT |
155 | if (!result) |
156 | result = acpi_eject_operation(handle, islockable); | |
1da177e4 | 157 | |
1da177e4 LT |
158 | if (result) { |
159 | ret = -EBUSY; | |
160 | } | |
4be44fcd | 161 | err: |
1da177e4 | 162 | return ret; |
1da177e4 LT |
163 | } |
164 | ||
f883d9db | 165 | static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); |
1da177e4 | 166 | |
e49bd2dd ZR |
167 | static ssize_t |
168 | acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) { | |
169 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
1da177e4 | 170 | |
e49bd2dd | 171 | return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id); |
1da177e4 | 172 | } |
e49bd2dd | 173 | static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL); |
1da177e4 | 174 | |
e49bd2dd ZR |
175 | static ssize_t |
176 | acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) { | |
177 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
178 | struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; | |
179 | int result; | |
1da177e4 | 180 | |
e49bd2dd ZR |
181 | result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path); |
182 | if(result) | |
183 | goto end; | |
1da177e4 | 184 | |
e49bd2dd ZR |
185 | result = sprintf(buf, "%s\n", (char*)path.pointer); |
186 | kfree(path.pointer); | |
187 | end: | |
188 | return result; | |
1da177e4 | 189 | } |
e49bd2dd | 190 | static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL); |
1da177e4 | 191 | |
e49bd2dd | 192 | static int acpi_device_setup_files(struct acpi_device *dev) |
1da177e4 | 193 | { |
f883d9db PM |
194 | acpi_status status; |
195 | acpi_handle temp; | |
e49bd2dd | 196 | int result = 0; |
1da177e4 LT |
197 | |
198 | /* | |
e49bd2dd | 199 | * Devices gotten from FADT don't have a "path" attribute |
1da177e4 | 200 | */ |
e49bd2dd ZR |
201 | if(dev->handle) { |
202 | result = device_create_file(&dev->dev, &dev_attr_path); | |
203 | if(result) | |
204 | goto end; | |
1da177e4 LT |
205 | } |
206 | ||
e49bd2dd ZR |
207 | if(dev->flags.hardware_id) { |
208 | result = device_create_file(&dev->dev, &dev_attr_hid); | |
209 | if(result) | |
210 | goto end; | |
211 | } | |
1da177e4 | 212 | |
29b71a1c TR |
213 | if (dev->flags.hardware_id || dev->flags.compatible_ids){ |
214 | result = device_create_file(&dev->dev, &dev_attr_modalias); | |
215 | if(result) | |
216 | goto end; | |
217 | } | |
218 | ||
e49bd2dd ZR |
219 | /* |
220 | * If device has _EJ0, 'eject' file is created that is used to trigger | |
221 | * hot-removal function from userland. | |
222 | */ | |
f883d9db PM |
223 | status = acpi_get_handle(dev->handle, "_EJ0", &temp); |
224 | if (ACPI_SUCCESS(status)) | |
e49bd2dd ZR |
225 | result = device_create_file(&dev->dev, &dev_attr_eject); |
226 | end: | |
227 | return result; | |
1da177e4 LT |
228 | } |
229 | ||
f883d9db | 230 | static void acpi_device_remove_files(struct acpi_device *dev) |
1da177e4 | 231 | { |
f883d9db PM |
232 | acpi_status status; |
233 | acpi_handle temp; | |
1da177e4 | 234 | |
f883d9db PM |
235 | /* |
236 | * If device has _EJ0, 'eject' file is created that is used to trigger | |
237 | * hot-removal function from userland. | |
238 | */ | |
239 | status = acpi_get_handle(dev->handle, "_EJ0", &temp); | |
240 | if (ACPI_SUCCESS(status)) | |
241 | device_remove_file(&dev->dev, &dev_attr_eject); | |
1da177e4 | 242 | |
29b71a1c TR |
243 | if (dev->flags.hardware_id || dev->flags.compatible_ids) |
244 | device_remove_file(&dev->dev, &dev_attr_modalias); | |
245 | ||
e49bd2dd ZR |
246 | if(dev->flags.hardware_id) |
247 | device_remove_file(&dev->dev, &dev_attr_hid); | |
248 | if(dev->handle) | |
249 | device_remove_file(&dev->dev, &dev_attr_path); | |
1da177e4 | 250 | } |
1da177e4 | 251 | /* -------------------------------------------------------------------------- |
9e89dde2 | 252 | ACPI Bus operations |
1da177e4 | 253 | -------------------------------------------------------------------------- */ |
29b71a1c TR |
254 | |
255 | int acpi_match_device_ids(struct acpi_device *device, | |
256 | const struct acpi_device_id *ids) | |
257 | { | |
258 | const struct acpi_device_id *id; | |
259 | ||
260 | if (device->flags.hardware_id) { | |
261 | for (id = ids; id->id[0]; id++) { | |
262 | if (!strcmp((char*)id->id, device->pnp.hardware_id)) | |
263 | return 0; | |
264 | } | |
265 | } | |
266 | ||
267 | if (device->flags.compatible_ids) { | |
268 | struct acpi_compatible_id_list *cid_list = device->pnp.cid_list; | |
269 | int i; | |
270 | ||
271 | for (id = ids; id->id[0]; id++) { | |
272 | /* compare multiple _CID entries against driver ids */ | |
273 | for (i = 0; i < cid_list->count; i++) { | |
274 | if (!strcmp((char*)id->id, | |
275 | cid_list->id[i].value)) | |
276 | return 0; | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
281 | return -ENOENT; | |
282 | } | |
283 | EXPORT_SYMBOL(acpi_match_device_ids); | |
284 | ||
1890a97a | 285 | static void acpi_device_release(struct device *dev) |
1da177e4 | 286 | { |
1890a97a | 287 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
1da177e4 | 288 | |
1890a97a PM |
289 | kfree(acpi_dev->pnp.cid_list); |
290 | kfree(acpi_dev); | |
1da177e4 LT |
291 | } |
292 | ||
5d9464a4 | 293 | static int acpi_device_suspend(struct device *dev, pm_message_t state) |
1da177e4 | 294 | { |
5d9464a4 PM |
295 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
296 | struct acpi_driver *acpi_drv = acpi_dev->driver; | |
1da177e4 | 297 | |
5d9464a4 PM |
298 | if (acpi_drv && acpi_drv->ops.suspend) |
299 | return acpi_drv->ops.suspend(acpi_dev, state); | |
1da177e4 LT |
300 | return 0; |
301 | } | |
1da177e4 | 302 | |
5d9464a4 | 303 | static int acpi_device_resume(struct device *dev) |
9e89dde2 | 304 | { |
5d9464a4 PM |
305 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
306 | struct acpi_driver *acpi_drv = acpi_dev->driver; | |
1da177e4 | 307 | |
5d9464a4 PM |
308 | if (acpi_drv && acpi_drv->ops.resume) |
309 | return acpi_drv->ops.resume(acpi_dev); | |
d550d98d | 310 | return 0; |
1da177e4 LT |
311 | } |
312 | ||
5d9464a4 | 313 | static int acpi_bus_match(struct device *dev, struct device_driver *drv) |
9e89dde2 | 314 | { |
5d9464a4 PM |
315 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
316 | struct acpi_driver *acpi_drv = to_acpi_driver(drv); | |
1da177e4 | 317 | |
29b71a1c | 318 | return !acpi_match_device_ids(acpi_dev, acpi_drv->ids); |
5d9464a4 | 319 | } |
1da177e4 | 320 | |
5d9464a4 | 321 | static int acpi_device_uevent(struct device *dev, char **envp, int num_envp, |
29b71a1c | 322 | char *buffer, int buffer_size) |
1da177e4 | 323 | { |
5d9464a4 | 324 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
5d9464a4 | 325 | |
29b71a1c TR |
326 | strcpy(buffer, "MODALIAS="); |
327 | if (create_modalias(acpi_dev, buffer + 9, buffer_size - 9) > 0) { | |
328 | envp[0] = buffer; | |
329 | envp[1] = NULL; | |
9e89dde2 | 330 | } |
9e89dde2 | 331 | return 0; |
1da177e4 LT |
332 | } |
333 | ||
5d9464a4 PM |
334 | static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *); |
335 | static int acpi_start_single_object(struct acpi_device *); | |
336 | static int acpi_device_probe(struct device * dev) | |
1da177e4 | 337 | { |
5d9464a4 PM |
338 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
339 | struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); | |
340 | int ret; | |
341 | ||
342 | ret = acpi_bus_driver_init(acpi_dev, acpi_drv); | |
343 | if (!ret) { | |
c4168bff LS |
344 | if (acpi_dev->bus_ops.acpi_op_start) |
345 | acpi_start_single_object(acpi_dev); | |
5d9464a4 PM |
346 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
347 | "Found driver [%s] for device [%s]\n", | |
348 | acpi_drv->name, acpi_dev->pnp.bus_id)); | |
349 | get_device(dev); | |
350 | } | |
351 | return ret; | |
352 | } | |
1da177e4 | 353 | |
5d9464a4 PM |
354 | static int acpi_device_remove(struct device * dev) |
355 | { | |
356 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
357 | struct acpi_driver *acpi_drv = acpi_dev->driver; | |
358 | ||
359 | if (acpi_drv) { | |
360 | if (acpi_drv->ops.stop) | |
96333578 | 361 | acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type); |
5d9464a4 | 362 | if (acpi_drv->ops.remove) |
96333578 | 363 | acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type); |
1da177e4 | 364 | } |
5d9464a4 PM |
365 | acpi_dev->driver = NULL; |
366 | acpi_driver_data(dev) = NULL; | |
1da177e4 | 367 | |
5d9464a4 | 368 | put_device(dev); |
9e89dde2 ZR |
369 | return 0; |
370 | } | |
1da177e4 | 371 | |
5d9464a4 | 372 | static void acpi_device_shutdown(struct device *dev) |
1da177e4 | 373 | { |
5d9464a4 PM |
374 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
375 | struct acpi_driver *acpi_drv = acpi_dev->driver; | |
1da177e4 | 376 | |
5d9464a4 PM |
377 | if (acpi_drv && acpi_drv->ops.shutdown) |
378 | acpi_drv->ops.shutdown(acpi_dev); | |
1da177e4 | 379 | |
5d9464a4 | 380 | return ; |
1da177e4 LT |
381 | } |
382 | ||
55955aad | 383 | struct bus_type acpi_bus_type = { |
9e89dde2 ZR |
384 | .name = "acpi", |
385 | .suspend = acpi_device_suspend, | |
386 | .resume = acpi_device_resume, | |
5d9464a4 PM |
387 | .shutdown = acpi_device_shutdown, |
388 | .match = acpi_bus_match, | |
389 | .probe = acpi_device_probe, | |
390 | .remove = acpi_device_remove, | |
391 | .uevent = acpi_device_uevent, | |
9e89dde2 ZR |
392 | }; |
393 | ||
e49bd2dd | 394 | static int acpi_device_register(struct acpi_device *device, |
9e89dde2 | 395 | struct acpi_device *parent) |
1da177e4 | 396 | { |
4be44fcd | 397 | int result; |
e49bd2dd ZR |
398 | struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; |
399 | int found = 0; | |
9e89dde2 ZR |
400 | /* |
401 | * Linkage | |
402 | * ------- | |
403 | * Link this device to its parent and siblings. | |
404 | */ | |
405 | INIT_LIST_HEAD(&device->children); | |
406 | INIT_LIST_HEAD(&device->node); | |
407 | INIT_LIST_HEAD(&device->g_list); | |
408 | INIT_LIST_HEAD(&device->wakeup_list); | |
1da177e4 | 409 | |
e49bd2dd ZR |
410 | new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); |
411 | if (!new_bus_id) { | |
412 | printk(KERN_ERR PREFIX "Memory allocation error\n"); | |
413 | return -ENOMEM; | |
1da177e4 | 414 | } |
e49bd2dd | 415 | |
9e89dde2 | 416 | spin_lock(&acpi_device_lock); |
e49bd2dd ZR |
417 | /* |
418 | * Find suitable bus_id and instance number in acpi_bus_id_list | |
419 | * If failed, create one and link it into acpi_bus_id_list | |
420 | */ | |
421 | list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { | |
bb095854 | 422 | if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) { |
e49bd2dd ZR |
423 | acpi_device_bus_id->instance_no ++; |
424 | found = 1; | |
425 | kfree(new_bus_id); | |
426 | break; | |
427 | } | |
1da177e4 | 428 | } |
e49bd2dd ZR |
429 | if(!found) { |
430 | acpi_device_bus_id = new_bus_id; | |
bb095854 | 431 | strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device"); |
e49bd2dd ZR |
432 | acpi_device_bus_id->instance_no = 0; |
433 | list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); | |
1da177e4 | 434 | } |
e49bd2dd | 435 | sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no); |
1da177e4 | 436 | |
9e89dde2 ZR |
437 | if (device->parent) { |
438 | list_add_tail(&device->node, &device->parent->children); | |
439 | list_add_tail(&device->g_list, &device->parent->g_list); | |
440 | } else | |
441 | list_add_tail(&device->g_list, &acpi_device_list); | |
442 | if (device->wakeup.flags.valid) | |
443 | list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); | |
444 | spin_unlock(&acpi_device_lock); | |
1da177e4 | 445 | |
1890a97a PM |
446 | if (device->parent) |
447 | device->dev.parent = &parent->dev; | |
448 | device->dev.bus = &acpi_bus_type; | |
449 | device_initialize(&device->dev); | |
1890a97a | 450 | device->dev.release = &acpi_device_release; |
e49bd2dd ZR |
451 | result = device_add(&device->dev); |
452 | if(result) { | |
453 | printk("Error adding device %s", device->dev.bus_id); | |
454 | goto end; | |
1da177e4 | 455 | } |
1da177e4 | 456 | |
e49bd2dd ZR |
457 | result = acpi_device_setup_files(device); |
458 | if(result) | |
459 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id)); | |
1da177e4 | 460 | |
96333578 | 461 | device->removal_type = ACPI_BUS_REMOVAL_NORMAL; |
1da177e4 | 462 | return 0; |
e49bd2dd ZR |
463 | end: |
464 | spin_lock(&acpi_device_lock); | |
465 | if (device->parent) { | |
466 | list_del(&device->node); | |
467 | list_del(&device->g_list); | |
468 | } else | |
469 | list_del(&device->g_list); | |
470 | list_del(&device->wakeup_list); | |
471 | spin_unlock(&acpi_device_lock); | |
472 | return result; | |
1da177e4 LT |
473 | } |
474 | ||
9e89dde2 ZR |
475 | static void acpi_device_unregister(struct acpi_device *device, int type) |
476 | { | |
477 | spin_lock(&acpi_device_lock); | |
478 | if (device->parent) { | |
479 | list_del(&device->node); | |
480 | list_del(&device->g_list); | |
481 | } else | |
482 | list_del(&device->g_list); | |
1da177e4 | 483 | |
9e89dde2 | 484 | list_del(&device->wakeup_list); |
9e89dde2 | 485 | spin_unlock(&acpi_device_lock); |
1da177e4 | 486 | |
9e89dde2 | 487 | acpi_detach_data(device->handle, acpi_bus_data_handler); |
1890a97a | 488 | |
f883d9db | 489 | acpi_device_remove_files(device); |
1890a97a | 490 | device_unregister(&device->dev); |
1da177e4 LT |
491 | } |
492 | ||
9e89dde2 ZR |
493 | /* -------------------------------------------------------------------------- |
494 | Driver Management | |
495 | -------------------------------------------------------------------------- */ | |
1da177e4 | 496 | /** |
d758a8fa RD |
497 | * acpi_bus_driver_init - add a device to a driver |
498 | * @device: the device to add and initialize | |
499 | * @driver: driver for the device | |
500 | * | |
1da177e4 | 501 | * Used to initialize a device via its device driver. Called whenever a |
1890a97a | 502 | * driver is bound to a device. Invokes the driver's add() ops. |
1da177e4 LT |
503 | */ |
504 | static int | |
4be44fcd | 505 | acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver) |
1da177e4 | 506 | { |
4be44fcd | 507 | int result = 0; |
1da177e4 | 508 | |
1da177e4 LT |
509 | |
510 | if (!device || !driver) | |
d550d98d | 511 | return -EINVAL; |
1da177e4 LT |
512 | |
513 | if (!driver->ops.add) | |
d550d98d | 514 | return -ENOSYS; |
1da177e4 LT |
515 | |
516 | result = driver->ops.add(device); | |
517 | if (result) { | |
518 | device->driver = NULL; | |
519 | acpi_driver_data(device) = NULL; | |
d550d98d | 520 | return result; |
1da177e4 LT |
521 | } |
522 | ||
523 | device->driver = driver; | |
524 | ||
525 | /* | |
526 | * TBD - Configuration Management: Assign resources to device based | |
527 | * upon possible configuration and currently allocated resources. | |
528 | */ | |
529 | ||
4be44fcd LB |
530 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
531 | "Driver successfully bound to device\n")); | |
d550d98d | 532 | return 0; |
3fb02738 RS |
533 | } |
534 | ||
8713cbef | 535 | static int acpi_start_single_object(struct acpi_device *device) |
3fb02738 RS |
536 | { |
537 | int result = 0; | |
538 | struct acpi_driver *driver; | |
539 | ||
3fb02738 RS |
540 | |
541 | if (!(driver = device->driver)) | |
d550d98d | 542 | return 0; |
3fb02738 | 543 | |
1da177e4 LT |
544 | if (driver->ops.start) { |
545 | result = driver->ops.start(device); | |
546 | if (result && driver->ops.remove) | |
547 | driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL); | |
1da177e4 LT |
548 | } |
549 | ||
d550d98d | 550 | return result; |
1da177e4 LT |
551 | } |
552 | ||
9e89dde2 ZR |
553 | /** |
554 | * acpi_bus_register_driver - register a driver with the ACPI bus | |
555 | * @driver: driver being registered | |
556 | * | |
557 | * Registers a driver with the ACPI bus. Searches the namespace for all | |
558 | * devices that match the driver's criteria and binds. Returns zero for | |
559 | * success or a negative error status for failure. | |
560 | */ | |
561 | int acpi_bus_register_driver(struct acpi_driver *driver) | |
1da177e4 | 562 | { |
1890a97a | 563 | int ret; |
1da177e4 | 564 | |
9e89dde2 ZR |
565 | if (acpi_disabled) |
566 | return -ENODEV; | |
1890a97a PM |
567 | driver->drv.name = driver->name; |
568 | driver->drv.bus = &acpi_bus_type; | |
569 | driver->drv.owner = driver->owner; | |
1da177e4 | 570 | |
1890a97a PM |
571 | ret = driver_register(&driver->drv); |
572 | return ret; | |
9e89dde2 | 573 | } |
1da177e4 | 574 | |
9e89dde2 ZR |
575 | EXPORT_SYMBOL(acpi_bus_register_driver); |
576 | ||
577 | /** | |
578 | * acpi_bus_unregister_driver - unregisters a driver with the APIC bus | |
579 | * @driver: driver to unregister | |
580 | * | |
581 | * Unregisters a driver with the ACPI bus. Searches the namespace for all | |
582 | * devices that match the driver's criteria and unbinds. | |
583 | */ | |
584 | void acpi_bus_unregister_driver(struct acpi_driver *driver) | |
585 | { | |
1890a97a | 586 | driver_unregister(&driver->drv); |
9e89dde2 ZR |
587 | } |
588 | ||
589 | EXPORT_SYMBOL(acpi_bus_unregister_driver); | |
590 | ||
9e89dde2 ZR |
591 | /* -------------------------------------------------------------------------- |
592 | Device Enumeration | |
593 | -------------------------------------------------------------------------- */ | |
594 | acpi_status | |
595 | acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) | |
596 | { | |
597 | acpi_status status; | |
598 | acpi_handle tmp; | |
599 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; | |
600 | union acpi_object *obj; | |
601 | ||
602 | status = acpi_get_handle(handle, "_EJD", &tmp); | |
603 | if (ACPI_FAILURE(status)) | |
604 | return status; | |
605 | ||
606 | status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); | |
607 | if (ACPI_SUCCESS(status)) { | |
608 | obj = buffer.pointer; | |
609 | status = acpi_get_handle(NULL, obj->string.pointer, ejd); | |
610 | kfree(buffer.pointer); | |
611 | } | |
612 | return status; | |
613 | } | |
614 | EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); | |
615 | ||
616 | void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context) | |
617 | { | |
618 | ||
619 | /* TBD */ | |
620 | ||
621 | return; | |
622 | } | |
623 | ||
9e89dde2 ZR |
624 | static int acpi_bus_get_perf_flags(struct acpi_device *device) |
625 | { | |
626 | device->performance.state = ACPI_STATE_UNKNOWN; | |
627 | return 0; | |
628 | } | |
629 | ||
630 | static acpi_status | |
631 | acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device, | |
632 | union acpi_object *package) | |
633 | { | |
634 | int i = 0; | |
635 | union acpi_object *element = NULL; | |
636 | ||
637 | if (!device || !package || (package->package.count < 2)) | |
638 | return AE_BAD_PARAMETER; | |
639 | ||
640 | element = &(package->package.elements[0]); | |
641 | if (!element) | |
642 | return AE_BAD_PARAMETER; | |
643 | if (element->type == ACPI_TYPE_PACKAGE) { | |
644 | if ((element->package.count < 2) || | |
645 | (element->package.elements[0].type != | |
646 | ACPI_TYPE_LOCAL_REFERENCE) | |
647 | || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) | |
648 | return AE_BAD_DATA; | |
649 | device->wakeup.gpe_device = | |
650 | element->package.elements[0].reference.handle; | |
651 | device->wakeup.gpe_number = | |
652 | (u32) element->package.elements[1].integer.value; | |
653 | } else if (element->type == ACPI_TYPE_INTEGER) { | |
654 | device->wakeup.gpe_number = element->integer.value; | |
655 | } else | |
656 | return AE_BAD_DATA; | |
657 | ||
658 | element = &(package->package.elements[1]); | |
659 | if (element->type != ACPI_TYPE_INTEGER) { | |
660 | return AE_BAD_DATA; | |
661 | } | |
662 | device->wakeup.sleep_state = element->integer.value; | |
663 | ||
664 | if ((package->package.count - 2) > ACPI_MAX_HANDLES) { | |
665 | return AE_NO_MEMORY; | |
666 | } | |
667 | device->wakeup.resources.count = package->package.count - 2; | |
668 | for (i = 0; i < device->wakeup.resources.count; i++) { | |
669 | element = &(package->package.elements[i + 2]); | |
670 | if (element->type != ACPI_TYPE_ANY) { | |
671 | return AE_BAD_DATA; | |
1da177e4 | 672 | } |
9e89dde2 ZR |
673 | |
674 | device->wakeup.resources.handles[i] = element->reference.handle; | |
1da177e4 | 675 | } |
9e89dde2 ZR |
676 | |
677 | return AE_OK; | |
1da177e4 LT |
678 | } |
679 | ||
9e89dde2 | 680 | static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device) |
1da177e4 | 681 | { |
9e89dde2 ZR |
682 | acpi_status status = 0; |
683 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
684 | union acpi_object *package = NULL; | |
1da177e4 | 685 | |
29b71a1c TR |
686 | struct acpi_device_id button_device_ids[] = { |
687 | {"PNP0C0D", 0}, | |
688 | {"PNP0C0C", 0}, | |
689 | {"PNP0C0E", 0}, | |
690 | {"", 0}, | |
691 | }; | |
692 | ||
1da177e4 | 693 | |
9e89dde2 ZR |
694 | /* _PRW */ |
695 | status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer); | |
696 | if (ACPI_FAILURE(status)) { | |
697 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); | |
698 | goto end; | |
1da177e4 | 699 | } |
1da177e4 | 700 | |
9e89dde2 ZR |
701 | package = (union acpi_object *)buffer.pointer; |
702 | status = acpi_bus_extract_wakeup_device_power_package(device, package); | |
703 | if (ACPI_FAILURE(status)) { | |
704 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package")); | |
705 | goto end; | |
706 | } | |
1da177e4 | 707 | |
9e89dde2 | 708 | kfree(buffer.pointer); |
1da177e4 | 709 | |
9e89dde2 ZR |
710 | device->wakeup.flags.valid = 1; |
711 | /* Power button, Lid switch always enable wakeup */ | |
29b71a1c | 712 | if (!acpi_match_device_ids(device, button_device_ids)) |
9e89dde2 | 713 | device->wakeup.flags.run_wake = 1; |
1da177e4 | 714 | |
9e89dde2 ZR |
715 | end: |
716 | if (ACPI_FAILURE(status)) | |
717 | device->flags.wake_capable = 0; | |
d550d98d | 718 | return 0; |
1da177e4 | 719 | } |
1da177e4 | 720 | |
9e89dde2 | 721 | static int acpi_bus_get_power_flags(struct acpi_device *device) |
1da177e4 | 722 | { |
9e89dde2 ZR |
723 | acpi_status status = 0; |
724 | acpi_handle handle = NULL; | |
725 | u32 i = 0; | |
1a365616 | 726 | |
4be44fcd | 727 | |
9e89dde2 ZR |
728 | /* |
729 | * Power Management Flags | |
730 | */ | |
731 | status = acpi_get_handle(device->handle, "_PSC", &handle); | |
732 | if (ACPI_SUCCESS(status)) | |
733 | device->power.flags.explicit_get = 1; | |
734 | status = acpi_get_handle(device->handle, "_IRC", &handle); | |
735 | if (ACPI_SUCCESS(status)) | |
736 | device->power.flags.inrush_current = 1; | |
1da177e4 | 737 | |
9e89dde2 ZR |
738 | /* |
739 | * Enumerate supported power management states | |
740 | */ | |
741 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) { | |
742 | struct acpi_device_power_state *ps = &device->power.states[i]; | |
743 | char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' }; | |
1da177e4 | 744 | |
9e89dde2 ZR |
745 | /* Evaluate "_PRx" to se if power resources are referenced */ |
746 | acpi_evaluate_reference(device->handle, object_name, NULL, | |
747 | &ps->resources); | |
748 | if (ps->resources.count) { | |
749 | device->power.flags.power_resources = 1; | |
750 | ps->flags.valid = 1; | |
1da177e4 | 751 | } |
1da177e4 | 752 | |
9e89dde2 ZR |
753 | /* Evaluate "_PSx" to see if we can do explicit sets */ |
754 | object_name[2] = 'S'; | |
755 | status = acpi_get_handle(device->handle, object_name, &handle); | |
756 | if (ACPI_SUCCESS(status)) { | |
757 | ps->flags.explicit_set = 1; | |
758 | ps->flags.valid = 1; | |
1da177e4 | 759 | } |
1da177e4 | 760 | |
9e89dde2 ZR |
761 | /* State is valid if we have some power control */ |
762 | if (ps->resources.count || ps->flags.explicit_set) | |
763 | ps->flags.valid = 1; | |
1da177e4 | 764 | |
9e89dde2 ZR |
765 | ps->power = -1; /* Unknown - driver assigned */ |
766 | ps->latency = -1; /* Unknown - driver assigned */ | |
767 | } | |
1da177e4 | 768 | |
9e89dde2 ZR |
769 | /* Set defaults for D0 and D3 states (always valid) */ |
770 | device->power.states[ACPI_STATE_D0].flags.valid = 1; | |
771 | device->power.states[ACPI_STATE_D0].power = 100; | |
772 | device->power.states[ACPI_STATE_D3].flags.valid = 1; | |
773 | device->power.states[ACPI_STATE_D3].power = 0; | |
c8f7a62c | 774 | |
9e89dde2 | 775 | /* TBD: System wake support and resource requirements. */ |
c8f7a62c | 776 | |
9e89dde2 | 777 | device->power.state = ACPI_STATE_UNKNOWN; |
c8f7a62c | 778 | |
9e89dde2 ZR |
779 | return 0; |
780 | } | |
c8f7a62c | 781 | |
4be44fcd | 782 | static int acpi_bus_get_flags(struct acpi_device *device) |
1da177e4 | 783 | { |
4be44fcd LB |
784 | acpi_status status = AE_OK; |
785 | acpi_handle temp = NULL; | |
1da177e4 | 786 | |
1da177e4 LT |
787 | |
788 | /* Presence of _STA indicates 'dynamic_status' */ | |
789 | status = acpi_get_handle(device->handle, "_STA", &temp); | |
790 | if (ACPI_SUCCESS(status)) | |
791 | device->flags.dynamic_status = 1; | |
792 | ||
793 | /* Presence of _CID indicates 'compatible_ids' */ | |
794 | status = acpi_get_handle(device->handle, "_CID", &temp); | |
795 | if (ACPI_SUCCESS(status)) | |
796 | device->flags.compatible_ids = 1; | |
797 | ||
798 | /* Presence of _RMV indicates 'removable' */ | |
799 | status = acpi_get_handle(device->handle, "_RMV", &temp); | |
800 | if (ACPI_SUCCESS(status)) | |
801 | device->flags.removable = 1; | |
802 | ||
803 | /* Presence of _EJD|_EJ0 indicates 'ejectable' */ | |
804 | status = acpi_get_handle(device->handle, "_EJD", &temp); | |
805 | if (ACPI_SUCCESS(status)) | |
806 | device->flags.ejectable = 1; | |
807 | else { | |
808 | status = acpi_get_handle(device->handle, "_EJ0", &temp); | |
809 | if (ACPI_SUCCESS(status)) | |
810 | device->flags.ejectable = 1; | |
811 | } | |
812 | ||
813 | /* Presence of _LCK indicates 'lockable' */ | |
814 | status = acpi_get_handle(device->handle, "_LCK", &temp); | |
815 | if (ACPI_SUCCESS(status)) | |
816 | device->flags.lockable = 1; | |
817 | ||
818 | /* Presence of _PS0|_PR0 indicates 'power manageable' */ | |
819 | status = acpi_get_handle(device->handle, "_PS0", &temp); | |
820 | if (ACPI_FAILURE(status)) | |
821 | status = acpi_get_handle(device->handle, "_PR0", &temp); | |
822 | if (ACPI_SUCCESS(status)) | |
823 | device->flags.power_manageable = 1; | |
824 | ||
825 | /* Presence of _PRW indicates wake capable */ | |
826 | status = acpi_get_handle(device->handle, "_PRW", &temp); | |
827 | if (ACPI_SUCCESS(status)) | |
828 | device->flags.wake_capable = 1; | |
829 | ||
830 | /* TBD: Peformance management */ | |
831 | ||
d550d98d | 832 | return 0; |
1da177e4 LT |
833 | } |
834 | ||
4be44fcd LB |
835 | static void acpi_device_get_busid(struct acpi_device *device, |
836 | acpi_handle handle, int type) | |
1da177e4 | 837 | { |
4be44fcd LB |
838 | char bus_id[5] = { '?', 0 }; |
839 | struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; | |
840 | int i = 0; | |
1da177e4 LT |
841 | |
842 | /* | |
843 | * Bus ID | |
844 | * ------ | |
845 | * The device's Bus ID is simply the object name. | |
846 | * TBD: Shouldn't this value be unique (within the ACPI namespace)? | |
847 | */ | |
848 | switch (type) { | |
849 | case ACPI_BUS_TYPE_SYSTEM: | |
850 | strcpy(device->pnp.bus_id, "ACPI"); | |
851 | break; | |
852 | case ACPI_BUS_TYPE_POWER_BUTTON: | |
853 | strcpy(device->pnp.bus_id, "PWRF"); | |
854 | break; | |
855 | case ACPI_BUS_TYPE_SLEEP_BUTTON: | |
856 | strcpy(device->pnp.bus_id, "SLPF"); | |
857 | break; | |
858 | default: | |
859 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); | |
860 | /* Clean up trailing underscores (if any) */ | |
861 | for (i = 3; i > 1; i--) { | |
862 | if (bus_id[i] == '_') | |
863 | bus_id[i] = '\0'; | |
864 | else | |
865 | break; | |
866 | } | |
867 | strcpy(device->pnp.bus_id, bus_id); | |
868 | break; | |
869 | } | |
870 | } | |
871 | ||
ae843332 ZR |
872 | static int |
873 | acpi_video_bus_match(struct acpi_device *device) | |
874 | { | |
875 | acpi_handle h_dummy1; | |
876 | acpi_handle h_dummy2; | |
877 | acpi_handle h_dummy3; | |
878 | ||
879 | ||
880 | if (!device) | |
881 | return -EINVAL; | |
882 | ||
883 | /* Since there is no HID, CID for ACPI Video drivers, we have | |
884 | * to check well known required nodes for each feature we support. | |
885 | */ | |
886 | ||
887 | /* Does this device able to support video switching ? */ | |
888 | if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) && | |
889 | ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2))) | |
890 | return 0; | |
891 | ||
892 | /* Does this device able to retrieve a video ROM ? */ | |
893 | if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1))) | |
894 | return 0; | |
895 | ||
896 | /* Does this device able to configure which video head to be POSTed ? */ | |
897 | if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) && | |
898 | ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) && | |
899 | ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3))) | |
900 | return 0; | |
901 | ||
902 | return -ENODEV; | |
903 | } | |
904 | ||
54735266 ZR |
905 | /* |
906 | * acpi_bay_match - see if a device is an ejectable driver bay | |
907 | * | |
908 | * If an acpi object is ejectable and has one of the ACPI ATA methods defined, | |
909 | * then we can safely call it an ejectable drive bay | |
910 | */ | |
911 | static int acpi_bay_match(struct acpi_device *device){ | |
912 | acpi_status status; | |
913 | acpi_handle handle; | |
914 | acpi_handle tmp; | |
915 | acpi_handle phandle; | |
916 | ||
917 | handle = device->handle; | |
918 | ||
919 | status = acpi_get_handle(handle, "_EJ0", &tmp); | |
920 | if (ACPI_FAILURE(status)) | |
921 | return -ENODEV; | |
922 | ||
923 | if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) || | |
924 | (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) || | |
925 | (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) || | |
926 | (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp)))) | |
927 | return 0; | |
928 | ||
929 | if (acpi_get_parent(handle, &phandle)) | |
930 | return -ENODEV; | |
931 | ||
932 | if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) || | |
933 | (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) || | |
934 | (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) || | |
935 | (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp)))) | |
936 | return 0; | |
937 | ||
938 | return -ENODEV; | |
939 | } | |
940 | ||
4be44fcd LB |
941 | static void acpi_device_set_id(struct acpi_device *device, |
942 | struct acpi_device *parent, acpi_handle handle, | |
943 | int type) | |
1da177e4 | 944 | { |
4be44fcd LB |
945 | struct acpi_device_info *info; |
946 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
947 | char *hid = NULL; | |
948 | char *uid = NULL; | |
1da177e4 | 949 | struct acpi_compatible_id_list *cid_list = NULL; |
4be44fcd | 950 | acpi_status status; |
1da177e4 LT |
951 | |
952 | switch (type) { | |
953 | case ACPI_BUS_TYPE_DEVICE: | |
954 | status = acpi_get_object_info(handle, &buffer); | |
955 | if (ACPI_FAILURE(status)) { | |
4be44fcd | 956 | printk("%s: Error reading device info\n", __FUNCTION__); |
1da177e4 LT |
957 | return; |
958 | } | |
959 | ||
960 | info = buffer.pointer; | |
961 | if (info->valid & ACPI_VALID_HID) | |
962 | hid = info->hardware_id.value; | |
963 | if (info->valid & ACPI_VALID_UID) | |
964 | uid = info->unique_id.value; | |
965 | if (info->valid & ACPI_VALID_CID) | |
966 | cid_list = &info->compatibility_id; | |
967 | if (info->valid & ACPI_VALID_ADR) { | |
968 | device->pnp.bus_address = info->address; | |
969 | device->flags.bus_address = 1; | |
970 | } | |
ae843332 ZR |
971 | |
972 | if(!(info->valid & (ACPI_VALID_HID | ACPI_VALID_CID))){ | |
973 | status = acpi_video_bus_match(device); | |
974 | if(ACPI_SUCCESS(status)) | |
975 | hid = ACPI_VIDEO_HID; | |
54735266 ZR |
976 | |
977 | status = acpi_bay_match(device); | |
978 | if (ACPI_SUCCESS(status)) | |
979 | hid = ACPI_BAY_HID; | |
ae843332 | 980 | } |
1da177e4 LT |
981 | break; |
982 | case ACPI_BUS_TYPE_POWER: | |
983 | hid = ACPI_POWER_HID; | |
984 | break; | |
985 | case ACPI_BUS_TYPE_PROCESSOR: | |
986 | hid = ACPI_PROCESSOR_HID; | |
987 | break; | |
988 | case ACPI_BUS_TYPE_SYSTEM: | |
989 | hid = ACPI_SYSTEM_HID; | |
990 | break; | |
991 | case ACPI_BUS_TYPE_THERMAL: | |
992 | hid = ACPI_THERMAL_HID; | |
993 | break; | |
994 | case ACPI_BUS_TYPE_POWER_BUTTON: | |
995 | hid = ACPI_BUTTON_HID_POWERF; | |
996 | break; | |
997 | case ACPI_BUS_TYPE_SLEEP_BUTTON: | |
998 | hid = ACPI_BUTTON_HID_SLEEPF; | |
999 | break; | |
1000 | } | |
1001 | ||
1002 | /* | |
1003 | * \_SB | |
1004 | * ---- | |
1005 | * Fix for the system root bus device -- the only root-level device. | |
1006 | */ | |
c51a4de8 | 1007 | if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) { |
1da177e4 LT |
1008 | hid = ACPI_BUS_HID; |
1009 | strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); | |
1010 | strcpy(device->pnp.device_class, ACPI_BUS_CLASS); | |
1011 | } | |
1012 | ||
1013 | if (hid) { | |
1014 | strcpy(device->pnp.hardware_id, hid); | |
1015 | device->flags.hardware_id = 1; | |
1016 | } | |
1017 | if (uid) { | |
1018 | strcpy(device->pnp.unique_id, uid); | |
1019 | device->flags.unique_id = 1; | |
1020 | } | |
1021 | if (cid_list) { | |
1022 | device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL); | |
1023 | if (device->pnp.cid_list) | |
1024 | memcpy(device->pnp.cid_list, cid_list, cid_list->size); | |
1025 | else | |
1026 | printk(KERN_ERR "Memory allocation error\n"); | |
1027 | } | |
1028 | ||
02438d87 | 1029 | kfree(buffer.pointer); |
1da177e4 LT |
1030 | } |
1031 | ||
4be44fcd | 1032 | static int acpi_device_set_context(struct acpi_device *device, int type) |
1da177e4 LT |
1033 | { |
1034 | acpi_status status = AE_OK; | |
1035 | int result = 0; | |
1036 | /* | |
1037 | * Context | |
1038 | * ------- | |
1039 | * Attach this 'struct acpi_device' to the ACPI object. This makes | |
1040 | * resolutions from handle->device very efficient. Note that we need | |
1041 | * to be careful with fixed-feature devices as they all attach to the | |
1042 | * root object. | |
1043 | */ | |
4be44fcd | 1044 | if (type != ACPI_BUS_TYPE_POWER_BUTTON && |
1da177e4 LT |
1045 | type != ACPI_BUS_TYPE_SLEEP_BUTTON) { |
1046 | status = acpi_attach_data(device->handle, | |
4be44fcd | 1047 | acpi_bus_data_handler, device); |
1da177e4 LT |
1048 | |
1049 | if (ACPI_FAILURE(status)) { | |
1050 | printk("Error attaching device data\n"); | |
1051 | result = -ENODEV; | |
1052 | } | |
1053 | } | |
1054 | return result; | |
1055 | } | |
1056 | ||
4be44fcd | 1057 | static int acpi_bus_remove(struct acpi_device *dev, int rmdevice) |
1da177e4 | 1058 | { |
1da177e4 | 1059 | if (!dev) |
d550d98d | 1060 | return -EINVAL; |
1da177e4 | 1061 | |
96333578 | 1062 | dev->removal_type = ACPI_BUS_REMOVAL_EJECT; |
1890a97a | 1063 | device_release_driver(&dev->dev); |
1da177e4 LT |
1064 | |
1065 | if (!rmdevice) | |
d550d98d | 1066 | return 0; |
1da177e4 | 1067 | |
2786f6e3 RZ |
1068 | /* |
1069 | * unbind _ADR-Based Devices when hot removal | |
1070 | */ | |
1da177e4 LT |
1071 | if (dev->flags.bus_address) { |
1072 | if ((dev->parent) && (dev->parent->ops.unbind)) | |
1073 | dev->parent->ops.unbind(dev); | |
1074 | } | |
1da177e4 LT |
1075 | acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT); |
1076 | ||
d550d98d | 1077 | return 0; |
1da177e4 LT |
1078 | } |
1079 | ||
3fb02738 | 1080 | static int |
4be44fcd | 1081 | acpi_add_single_object(struct acpi_device **child, |
c4168bff LS |
1082 | struct acpi_device *parent, acpi_handle handle, int type, |
1083 | struct acpi_bus_ops *ops) | |
1da177e4 | 1084 | { |
4be44fcd LB |
1085 | int result = 0; |
1086 | struct acpi_device *device = NULL; | |
1da177e4 | 1087 | |
1da177e4 LT |
1088 | |
1089 | if (!child) | |
d550d98d | 1090 | return -EINVAL; |
1da177e4 | 1091 | |
36bcbec7 | 1092 | device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); |
1da177e4 | 1093 | if (!device) { |
6468463a | 1094 | printk(KERN_ERR PREFIX "Memory allocation error\n"); |
d550d98d | 1095 | return -ENOMEM; |
1da177e4 | 1096 | } |
1da177e4 LT |
1097 | |
1098 | device->handle = handle; | |
1099 | device->parent = parent; | |
c4168bff LS |
1100 | device->bus_ops = *ops; /* workround for not call .start */ |
1101 | ||
1da177e4 | 1102 | |
4be44fcd | 1103 | acpi_device_get_busid(device, handle, type); |
1da177e4 LT |
1104 | |
1105 | /* | |
1106 | * Flags | |
1107 | * ----- | |
1108 | * Get prior to calling acpi_bus_get_status() so we know whether | |
1109 | * or not _STA is present. Note that we only look for object | |
1110 | * handles -- cannot evaluate objects until we know the device is | |
1111 | * present and properly initialized. | |
1112 | */ | |
1113 | result = acpi_bus_get_flags(device); | |
1114 | if (result) | |
1115 | goto end; | |
1116 | ||
1117 | /* | |
1118 | * Status | |
1119 | * ------ | |
6940faba KT |
1120 | * See if the device is present. We always assume that non-Device |
1121 | * and non-Processor objects (e.g. thermal zones, power resources, | |
1122 | * etc.) are present, functioning, etc. (at least when parent object | |
1123 | * is present). Note that _STA has a different meaning for some | |
1124 | * objects (e.g. power resources) so we need to be careful how we use | |
1125 | * it. | |
1da177e4 LT |
1126 | */ |
1127 | switch (type) { | |
6940faba | 1128 | case ACPI_BUS_TYPE_PROCESSOR: |
1da177e4 LT |
1129 | case ACPI_BUS_TYPE_DEVICE: |
1130 | result = acpi_bus_get_status(device); | |
1131 | if (ACPI_FAILURE(result) || !device->status.present) { | |
1132 | result = -ENOENT; | |
1133 | goto end; | |
1134 | } | |
1135 | break; | |
1136 | default: | |
0c0e8921 BH |
1137 | STRUCT_TO_INT(device->status) = |
1138 | ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | | |
1139 | ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING; | |
1da177e4 LT |
1140 | break; |
1141 | } | |
1142 | ||
1143 | /* | |
1144 | * Initialize Device | |
1145 | * ----------------- | |
1146 | * TBD: Synch with Core's enumeration/initialization process. | |
1147 | */ | |
1148 | ||
1149 | /* | |
1150 | * Hardware ID, Unique ID, & Bus Address | |
1151 | * ------------------------------------- | |
1152 | */ | |
4be44fcd | 1153 | acpi_device_set_id(device, parent, handle, type); |
1da177e4 LT |
1154 | |
1155 | /* | |
1156 | * Power Management | |
1157 | * ---------------- | |
1158 | */ | |
1159 | if (device->flags.power_manageable) { | |
1160 | result = acpi_bus_get_power_flags(device); | |
1161 | if (result) | |
1162 | goto end; | |
1163 | } | |
1164 | ||
4be44fcd | 1165 | /* |
1da177e4 LT |
1166 | * Wakeup device management |
1167 | *----------------------- | |
1168 | */ | |
1169 | if (device->flags.wake_capable) { | |
1170 | result = acpi_bus_get_wakeup_device_flags(device); | |
1171 | if (result) | |
1172 | goto end; | |
1173 | } | |
1174 | ||
1175 | /* | |
1176 | * Performance Management | |
1177 | * ---------------------- | |
1178 | */ | |
1179 | if (device->flags.performance_manageable) { | |
1180 | result = acpi_bus_get_perf_flags(device); | |
1181 | if (result) | |
1182 | goto end; | |
1183 | } | |
1184 | ||
4be44fcd | 1185 | if ((result = acpi_device_set_context(device, type))) |
1da177e4 LT |
1186 | goto end; |
1187 | ||
e49bd2dd | 1188 | result = acpi_device_register(device, parent); |
1da177e4 LT |
1189 | |
1190 | /* | |
2786f6e3 | 1191 | * Bind _ADR-Based Devices when hot add |
1da177e4 LT |
1192 | */ |
1193 | if (device->flags.bus_address) { | |
1194 | if (device->parent && device->parent->ops.bind) | |
1195 | device->parent->ops.bind(device); | |
1196 | } | |
1197 | ||
4be44fcd | 1198 | end: |
1da177e4 LT |
1199 | if (!result) |
1200 | *child = device; | |
1201 | else { | |
6044ec88 | 1202 | kfree(device->pnp.cid_list); |
1da177e4 LT |
1203 | kfree(device); |
1204 | } | |
1205 | ||
d550d98d | 1206 | return result; |
1da177e4 | 1207 | } |
1da177e4 | 1208 | |
4be44fcd | 1209 | static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops) |
1da177e4 | 1210 | { |
4be44fcd LB |
1211 | acpi_status status = AE_OK; |
1212 | struct acpi_device *parent = NULL; | |
1213 | struct acpi_device *child = NULL; | |
1214 | acpi_handle phandle = NULL; | |
1215 | acpi_handle chandle = NULL; | |
1216 | acpi_object_type type = 0; | |
1217 | u32 level = 1; | |
1da177e4 | 1218 | |
1da177e4 LT |
1219 | |
1220 | if (!start) | |
d550d98d | 1221 | return -EINVAL; |
1da177e4 LT |
1222 | |
1223 | parent = start; | |
1224 | phandle = start->handle; | |
4be44fcd | 1225 | |
1da177e4 LT |
1226 | /* |
1227 | * Parse through the ACPI namespace, identify all 'devices', and | |
1228 | * create a new 'struct acpi_device' for each. | |
1229 | */ | |
1230 | while ((level > 0) && parent) { | |
1231 | ||
1232 | status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, | |
4be44fcd | 1233 | chandle, &chandle); |
1da177e4 LT |
1234 | |
1235 | /* | |
1236 | * If this scope is exhausted then move our way back up. | |
1237 | */ | |
1238 | if (ACPI_FAILURE(status)) { | |
1239 | level--; | |
1240 | chandle = phandle; | |
1241 | acpi_get_parent(phandle, &phandle); | |
1242 | if (parent->parent) | |
1243 | parent = parent->parent; | |
1244 | continue; | |
1245 | } | |
1246 | ||
1247 | status = acpi_get_type(chandle, &type); | |
1248 | if (ACPI_FAILURE(status)) | |
1249 | continue; | |
1250 | ||
1251 | /* | |
1252 | * If this is a scope object then parse it (depth-first). | |
1253 | */ | |
1254 | if (type == ACPI_TYPE_LOCAL_SCOPE) { | |
1255 | level++; | |
1256 | phandle = chandle; | |
1257 | chandle = NULL; | |
1258 | continue; | |
1259 | } | |
1260 | ||
1261 | /* | |
1262 | * We're only interested in objects that we consider 'devices'. | |
1263 | */ | |
1264 | switch (type) { | |
1265 | case ACPI_TYPE_DEVICE: | |
1266 | type = ACPI_BUS_TYPE_DEVICE; | |
1267 | break; | |
1268 | case ACPI_TYPE_PROCESSOR: | |
1269 | type = ACPI_BUS_TYPE_PROCESSOR; | |
1270 | break; | |
1271 | case ACPI_TYPE_THERMAL: | |
1272 | type = ACPI_BUS_TYPE_THERMAL; | |
1273 | break; | |
1274 | case ACPI_TYPE_POWER: | |
1275 | type = ACPI_BUS_TYPE_POWER; | |
1276 | break; | |
1277 | default: | |
1278 | continue; | |
1279 | } | |
1280 | ||
3fb02738 RS |
1281 | if (ops->acpi_op_add) |
1282 | status = acpi_add_single_object(&child, parent, | |
c4168bff | 1283 | chandle, type, ops); |
4be44fcd | 1284 | else |
3fb02738 RS |
1285 | status = acpi_bus_get_device(chandle, &child); |
1286 | ||
4be44fcd LB |
1287 | if (ACPI_FAILURE(status)) |
1288 | continue; | |
3fb02738 | 1289 | |
c4168bff | 1290 | if (ops->acpi_op_start && !(ops->acpi_op_add)) { |
3fb02738 RS |
1291 | status = acpi_start_single_object(child); |
1292 | if (ACPI_FAILURE(status)) | |
1293 | continue; | |
1294 | } | |
1da177e4 LT |
1295 | |
1296 | /* | |
1297 | * If the device is present, enabled, and functioning then | |
1298 | * parse its scope (depth-first). Note that we need to | |
1299 | * represent absent devices to facilitate PnP notifications | |
1300 | * -- but only the subtree head (not all of its children, | |
1301 | * which will be enumerated when the parent is inserted). | |
1302 | * | |
1303 | * TBD: Need notifications and other detection mechanisms | |
4be44fcd | 1304 | * in place before we can fully implement this. |
1da177e4 LT |
1305 | */ |
1306 | if (child->status.present) { | |
1307 | status = acpi_get_next_object(ACPI_TYPE_ANY, chandle, | |
1308 | NULL, NULL); | |
1309 | if (ACPI_SUCCESS(status)) { | |
1310 | level++; | |
1311 | phandle = chandle; | |
1312 | chandle = NULL; | |
1313 | parent = child; | |
1314 | } | |
1315 | } | |
1316 | } | |
1317 | ||
d550d98d | 1318 | return 0; |
1da177e4 | 1319 | } |
1da177e4 | 1320 | |
3fb02738 | 1321 | int |
4be44fcd LB |
1322 | acpi_bus_add(struct acpi_device **child, |
1323 | struct acpi_device *parent, acpi_handle handle, int type) | |
3fb02738 RS |
1324 | { |
1325 | int result; | |
1326 | struct acpi_bus_ops ops; | |
1327 | ||
c4168bff LS |
1328 | memset(&ops, 0, sizeof(ops)); |
1329 | ops.acpi_op_add = 1; | |
3fb02738 | 1330 | |
c4168bff LS |
1331 | result = acpi_add_single_object(child, parent, handle, type, &ops); |
1332 | if (!result) | |
3fb02738 | 1333 | result = acpi_bus_scan(*child, &ops); |
c4168bff | 1334 | |
d550d98d | 1335 | return result; |
3fb02738 | 1336 | } |
4be44fcd | 1337 | |
3fb02738 RS |
1338 | EXPORT_SYMBOL(acpi_bus_add); |
1339 | ||
4be44fcd | 1340 | int acpi_bus_start(struct acpi_device *device) |
3fb02738 RS |
1341 | { |
1342 | int result; | |
1343 | struct acpi_bus_ops ops; | |
1344 | ||
3fb02738 RS |
1345 | |
1346 | if (!device) | |
d550d98d | 1347 | return -EINVAL; |
3fb02738 RS |
1348 | |
1349 | result = acpi_start_single_object(device); | |
1350 | if (!result) { | |
1351 | memset(&ops, 0, sizeof(ops)); | |
1352 | ops.acpi_op_start = 1; | |
1353 | result = acpi_bus_scan(device, &ops); | |
1354 | } | |
d550d98d | 1355 | return result; |
3fb02738 | 1356 | } |
4be44fcd | 1357 | |
3fb02738 | 1358 | EXPORT_SYMBOL(acpi_bus_start); |
1da177e4 | 1359 | |
ceaba663 | 1360 | int acpi_bus_trim(struct acpi_device *start, int rmdevice) |
1da177e4 | 1361 | { |
4be44fcd LB |
1362 | acpi_status status; |
1363 | struct acpi_device *parent, *child; | |
1364 | acpi_handle phandle, chandle; | |
1365 | acpi_object_type type; | |
1366 | u32 level = 1; | |
1367 | int err = 0; | |
1368 | ||
1369 | parent = start; | |
1da177e4 LT |
1370 | phandle = start->handle; |
1371 | child = chandle = NULL; | |
1372 | ||
1373 | while ((level > 0) && parent && (!err)) { | |
1374 | status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, | |
4be44fcd | 1375 | chandle, &chandle); |
1da177e4 LT |
1376 | |
1377 | /* | |
1378 | * If this scope is exhausted then move our way back up. | |
1379 | */ | |
1380 | if (ACPI_FAILURE(status)) { | |
1381 | level--; | |
1382 | chandle = phandle; | |
1383 | acpi_get_parent(phandle, &phandle); | |
1384 | child = parent; | |
1385 | parent = parent->parent; | |
1386 | ||
1387 | if (level == 0) | |
1388 | err = acpi_bus_remove(child, rmdevice); | |
1389 | else | |
1390 | err = acpi_bus_remove(child, 1); | |
1391 | ||
1392 | continue; | |
1393 | } | |
1394 | ||
1395 | status = acpi_get_type(chandle, &type); | |
1396 | if (ACPI_FAILURE(status)) { | |
1397 | continue; | |
1398 | } | |
1399 | /* | |
1400 | * If there is a device corresponding to chandle then | |
1401 | * parse it (depth-first). | |
1402 | */ | |
1403 | if (acpi_bus_get_device(chandle, &child) == 0) { | |
1404 | level++; | |
1405 | phandle = chandle; | |
1406 | chandle = NULL; | |
1407 | parent = child; | |
1408 | } | |
1409 | continue; | |
1410 | } | |
1411 | return err; | |
1412 | } | |
ceaba663 KA |
1413 | EXPORT_SYMBOL_GPL(acpi_bus_trim); |
1414 | ||
1da177e4 | 1415 | |
4be44fcd | 1416 | static int acpi_bus_scan_fixed(struct acpi_device *root) |
1da177e4 | 1417 | { |
4be44fcd LB |
1418 | int result = 0; |
1419 | struct acpi_device *device = NULL; | |
c4168bff | 1420 | struct acpi_bus_ops ops; |
1da177e4 LT |
1421 | |
1422 | if (!root) | |
d550d98d | 1423 | return -ENODEV; |
1da177e4 | 1424 | |
c4168bff LS |
1425 | memset(&ops, 0, sizeof(ops)); |
1426 | ops.acpi_op_add = 1; | |
1427 | ops.acpi_op_start = 1; | |
1428 | ||
1da177e4 LT |
1429 | /* |
1430 | * Enumerate all fixed-feature devices. | |
1431 | */ | |
cee324b1 | 1432 | if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) { |
3fb02738 | 1433 | result = acpi_add_single_object(&device, acpi_root, |
4be44fcd | 1434 | NULL, |
c4168bff LS |
1435 | ACPI_BUS_TYPE_POWER_BUTTON, |
1436 | &ops); | |
3fb02738 | 1437 | } |
1da177e4 | 1438 | |
cee324b1 | 1439 | if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) { |
3fb02738 | 1440 | result = acpi_add_single_object(&device, acpi_root, |
4be44fcd | 1441 | NULL, |
c4168bff LS |
1442 | ACPI_BUS_TYPE_SLEEP_BUTTON, |
1443 | &ops); | |
3fb02738 | 1444 | } |
1da177e4 | 1445 | |
d550d98d | 1446 | return result; |
1da177e4 LT |
1447 | } |
1448 | ||
1da177e4 LT |
1449 | static int __init acpi_scan_init(void) |
1450 | { | |
1451 | int result; | |
3fb02738 | 1452 | struct acpi_bus_ops ops; |
1da177e4 | 1453 | |
1da177e4 LT |
1454 | |
1455 | if (acpi_disabled) | |
d550d98d | 1456 | return 0; |
1da177e4 | 1457 | |
c4168bff LS |
1458 | memset(&ops, 0, sizeof(ops)); |
1459 | ops.acpi_op_add = 1; | |
1460 | ops.acpi_op_start = 1; | |
1da177e4 | 1461 | |
5b327265 PM |
1462 | result = bus_register(&acpi_bus_type); |
1463 | if (result) { | |
1464 | /* We don't want to quit even if we failed to add suspend/resume */ | |
1465 | printk(KERN_ERR PREFIX "Could not register bus type\n"); | |
1466 | } | |
1467 | ||
1da177e4 LT |
1468 | /* |
1469 | * Create the root device in the bus's device tree | |
1470 | */ | |
3fb02738 | 1471 | result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT, |
c4168bff | 1472 | ACPI_BUS_TYPE_SYSTEM, &ops); |
1da177e4 LT |
1473 | if (result) |
1474 | goto Done; | |
1475 | ||
1476 | /* | |
1477 | * Enumerate devices in the ACPI namespace. | |
1478 | */ | |
1479 | result = acpi_bus_scan_fixed(acpi_root); | |
c4168bff | 1480 | if (!result) |
3fb02738 | 1481 | result = acpi_bus_scan(acpi_root, &ops); |
1da177e4 LT |
1482 | |
1483 | if (result) | |
1484 | acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL); | |
1485 | ||
4be44fcd | 1486 | Done: |
d550d98d | 1487 | return result; |
1da177e4 LT |
1488 | } |
1489 | ||
1490 | subsys_initcall(acpi_scan_init); |