1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * HISTORY:
22 * 06/09/2003 Initial creation mridge@us.ibm.com
23 * -Ported
24 * updated - 01/09/2005 Updates from Intel to add functionality
25 *
26 * 01/03/2009 Márton Németh <nm127@freemail.hu>
27 * - Updated for Linux kernel 2.6.28
28 */
29
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34 #include <linux/fs.h>
35 #include <linux/blkdev.h>
36 #include <linux/ioctl.h>
37 #include <linux/pm.h>
38 #include <linux/acpi.h>
39 #include <linux/genhd.h>
40 #include <linux/dmi.h>
41 #include <linux/nls.h>
42
43 #include "ltp_acpi.h"
44
45 MODULE_AUTHOR("Martin Ridgeway <mridge@us.ibm.com>");
46 MODULE_AUTHOR("Alexey Kodanev <alexey.kodanev@oracle.com>");
47 MODULE_DESCRIPTION("ACPI LTP Test Driver");
48 MODULE_LICENSE("GPL");
49 ACPI_MODULE_NAME("LTP_ACPI")
50
51 #define prk_err(fmt, ...) \
52 pr_err(ACPI_TEST_NAME ": " fmt "\n", ##__VA_ARGS__)
53 #define prk_alert(fmt, ...) \
54 pr_alert(ACPI_TEST_NAME ": " fmt "\n", ##__VA_ARGS__)
55 #define prk_info(fmt, ...) \
56 pr_info(ACPI_TEST_NAME ": " fmt "\n", ##__VA_ARGS__)
57
acpi_failure(acpi_status status,const char * name)58 static int acpi_failure(acpi_status status, const char *name)
59 {
60 if (ACPI_FAILURE(status)) {
61 ACPI_EXCEPTION((AE_INFO, status, name));
62 return 1;
63 }
64 return 0;
65 }
66
67 /* points to the string of the last found object _STR */
68 static char *str_obj_result;
69
70 /* sysfs device path of the last found device */
71 static char *sysfs_path;
72
73 /* first found device with _CRS */
74 static acpi_handle res_handle;
75
get_str_object(acpi_handle handle)76 static acpi_status get_str_object(acpi_handle handle)
77 {
78 int res;
79 acpi_status status;
80 acpi_handle temp = 0;
81 union acpi_object *str_obj;
82 char *buf = NULL;
83
84 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
85
86 status = acpi_get_handle(handle, "_STR", &temp);
87
88 if (ACPI_SUCCESS(status) &&
89 !acpi_evaluate_object(handle, "_STR", NULL, &buffer)) {
90
91 str_obj = buffer.pointer;
92
93 buf = kmalloc(str_obj->buffer.length / 2, GFP_KERNEL);
94 if (!buf) {
95 kfree(str_obj);
96 return AE_NO_MEMORY;
97 }
98
99 res = utf16s_to_utf8s((wchar_t *)str_obj->buffer.pointer,
100 str_obj->buffer.length, UTF16_LITTLE_ENDIAN, buf,
101 str_obj->buffer.length / 2);
102
103 buf[res] = '\0';
104
105 kfree(str_obj_result);
106 str_obj_result = buf;
107 kfree(str_obj);
108 }
109
110 return status;
111 }
112
get_crs_object(acpi_handle handle)113 static void get_crs_object(acpi_handle handle)
114 {
115 acpi_status status;
116 acpi_handle temp;
117 if (!res_handle) {
118 status = acpi_get_handle(handle, METHOD_NAME__CRS, &temp);
119 if (ACPI_SUCCESS(status))
120 res_handle = handle;
121 }
122 }
123
get_sysfs_path(acpi_handle handle)124 static void get_sysfs_path(acpi_handle handle)
125 {
126 acpi_status status;
127 struct acpi_device *device;
128
129 kfree(sysfs_path);
130 sysfs_path = NULL;
131
132 status = acpi_bus_get_device(handle, &device);
133 if (ACPI_SUCCESS(status))
134 sysfs_path = kobject_get_path(&device->dev.kobj, GFP_KERNEL);
135 }
136
137 /* acpi handle of the last visited device */
138 static acpi_handle start_parent;
139
acpi_traverse(acpi_handle parent,acpi_handle child)140 static int acpi_traverse(acpi_handle parent, acpi_handle child)
141 {
142 static char indent[64];
143 const char * const ind_end = indent + 63;
144 static const char *ind = ind_end;
145 acpi_status status;
146 struct acpi_device_info *dev_info;
147 acpi_handle new_child;
148
149 if (!indent[0])
150 memset(indent, 0x20, 63);
151
152 while (parent) {
153 status = acpi_get_next_object(ACPI_TYPE_DEVICE,
154 parent, child, &new_child);
155
156 if (ACPI_FAILURE(status)) {
157 ind += 4;
158
159 child = parent;
160 status = acpi_get_parent(child, &parent);
161
162 /* no more devices */
163 if (ACPI_FAILURE(status)) {
164 start_parent = 0;
165 kfree(str_obj_result);
166 str_obj_result = NULL;
167 return 0;
168 }
169 continue;
170 }
171
172 status = acpi_get_object_info(new_child, &dev_info);
173 if (acpi_failure(status, "acpi_object_info failed"))
174 return 1;
175
176 get_sysfs_path(new_child);
177
178 get_crs_object(new_child);
179
180 if (ind < indent)
181 ind = indent;
182 else if (ind > ind_end)
183 ind = ind_end;
184
185 /*
186 * if we find _STR object we will stop here
187 * and save last visited child
188 */
189 if (ACPI_SUCCESS(get_str_object(new_child))) {
190 prk_info("%s%4.4s: has '_STR' '%s' path '%s'",
191 ind, (char *)&dev_info->name, str_obj_result,
192 (sysfs_path) ? sysfs_path : "no path");
193 ind -= 4;
194 start_parent = new_child;
195 kfree(dev_info);
196 return 0;
197 }
198 prk_info("%s%4.4s: path '%s'", ind, (char *)&dev_info->name,
199 (sysfs_path) ? sysfs_path : "no path");
200
201 ind -= 4;
202 parent = new_child;
203 child = 0;
204 kfree(dev_info);
205 }
206
207 return 0;
208 }
209
acpi_traverse_from_root(void)210 static int acpi_traverse_from_root(void)
211 {
212 acpi_status status;
213 struct acpi_device_info *dev_info;
214 acpi_handle parent = 0, child = 0;
215
216 if (!start_parent) {
217 status = acpi_get_handle(NULL, ACPI_NS_ROOT_PATH, &parent);
218 if (acpi_failure(status, "acpi_get_handle"))
219 return 1;
220 status = acpi_get_object_info(parent, &dev_info);
221 if (acpi_failure(status, "acpi_object_info failed"))
222 return 1;
223 prk_info("start from %4.4s", (char *)&dev_info->name);
224 } else {
225 /* continue with the last visited child */
226 parent = start_parent;
227 }
228
229 return acpi_traverse(parent, child);
230 }
231
232 /* first found device with _STR */
233 static acpi_handle dev_handle;
234 static int acpi_hw_reduced;
235
acpi_init(void)236 static int acpi_init(void)
237 {
238 acpi_status status;
239 acpi_handle parent_handle;
240 struct acpi_table_fadt *fadt;
241 struct acpi_table_header *table;
242 struct acpi_device_info *dev_info;
243
244 status = acpi_get_table(ACPI_SIG_FADT, 0, &table);
245 if (ACPI_SUCCESS(status)) {
246 fadt = (struct acpi_table_fadt *)table;
247 if (fadt->flags & ACPI_FADT_HW_REDUCED)
248 acpi_hw_reduced = 1;
249 }
250 if (acpi_hw_reduced)
251 prk_alert("Detected the Hardware-reduced ACPI mode");
252
253 prk_alert("TEST -- acpi_get_handle ");
254 status = acpi_get_handle(NULL, "\\_SB", &parent_handle);
255 if (acpi_failure(status, "acpi_get_handle"))
256 return 1;
257
258 /* get first device on SYS bus, it will be used in other tests */
259 while (acpi_get_next_object(ACPI_TYPE_DEVICE,
260 parent_handle, 0, &dev_handle) == 0) {
261 parent_handle = dev_handle;
262 }
263
264 status = acpi_get_object_info(dev_handle, &dev_info);
265 if (acpi_failure(status, "acpi_object_info failed"))
266 return 1;
267
268 prk_alert("ACPI object name %4.4s, type %d", (char *)&dev_info->name,
269 dev_info->type);
270 kfree(dev_info);
271
272 prk_alert("TEST -- acpi_get_parent ");
273 status = acpi_get_parent(dev_handle, &parent_handle);
274 return acpi_failure(status, "acpi_get_parent failed");
275 }
276
277 /*
278 * acpi_bus_notify
279 * ---------------
280 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
281 */
acpi_bus_notify(acpi_handle handle,u32 type,void * data)282 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
283 {
284 prk_alert("Register ACPI Bus Notify callback function");
285 }
286
acpi_test_notify_handler(void)287 static int acpi_test_notify_handler(void)
288 {
289 acpi_status status;
290
291 prk_alert("TEST -- acpi_install_notify_handler");
292
293 status = acpi_install_notify_handler(dev_handle,
294 ACPI_SYSTEM_NOTIFY, &acpi_bus_notify, NULL);
295
296 if (ACPI_SUCCESS(status)) {
297 prk_alert("TEST -- acpi_remove_notify_handler");
298 status = acpi_remove_notify_handler(dev_handle,
299 ACPI_SYSTEM_NOTIFY, &acpi_bus_notify);
300 return acpi_failure(status, "acpi_remove_notify_handler");
301 } else if (status != AE_ALREADY_EXISTS) {
302 return acpi_failure(status, "acpi_install_notify_handler");
303 }
304
305 return 0;
306 }
307
ltp_test_power_button_ev_handler(void * context)308 static u32 ltp_test_power_button_ev_handler(void *context)
309 {
310 prk_alert("ltp_test_power_button_ev_handler");
311 return 1;
312 }
313
ltp_test_sleep_button_ev_handler(void * context)314 static u32 ltp_test_sleep_button_ev_handler(void *context)
315 {
316 prk_alert("ltp_test_sleep_button_ev_handler");
317 return 1;
318 }
319
acpi_test_event_handler(void)320 static int acpi_test_event_handler(void)
321 {
322 int err = 0;
323 acpi_status status;
324
325 prk_alert("TEST -- acpi_install_fixed_event_handler");
326 if (acpi_hw_reduced) {
327 prk_alert("Skipped due to the HW-reduced mode");
328 return 0;
329 }
330 status = acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
331 ltp_test_power_button_ev_handler, NULL);
332
333 if (ACPI_SUCCESS(status)) {
334 prk_alert("TEST -- acpi_remove_fixed_event_handler");
335 status = acpi_remove_fixed_event_handler(
336 ACPI_EVENT_POWER_BUTTON,
337 ltp_test_power_button_ev_handler);
338 err = acpi_failure(status, "remove fixed event handler");
339 } else if (status != AE_ALREADY_EXISTS) {
340 err = acpi_failure(status, "install fixed event handler");
341 }
342
343 prk_alert("TEST -- acpi_install_fixed_event_handler");
344 status = acpi_install_fixed_event_handler(ACPI_EVENT_RTC,
345 ltp_test_sleep_button_ev_handler, NULL);
346
347 if (ACPI_SUCCESS(status)) {
348 prk_alert("TEST -- acpi_remove_fixed_event_handler");
349 status = acpi_remove_fixed_event_handler(
350 ACPI_EVENT_RTC,
351 ltp_test_sleep_button_ev_handler);
352 err |= acpi_failure(status, "remove fixed event handler");
353 } else if (status != AE_ALREADY_EXISTS) {
354 err |= acpi_failure(status, "install fixed event handler");
355 }
356
357 return err;
358 }
359
360 #ifndef ACPI_EC_UDELAY_GLK
361 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
362 #endif
363
acpi_global_lock(void)364 static int acpi_global_lock(void)
365 {
366 acpi_status status;
367 u32 global_lock = 0;
368
369 prk_alert("TEST -- acpi_acquire_global_lock ");
370 if (acpi_hw_reduced) {
371 prk_alert("Skipped due to the HW-reduced mode");
372 return 0;
373 }
374 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &global_lock);
375 if (acpi_failure(status, "acpi_acquire_global_lock"))
376 return 1;
377
378 prk_alert("TEST -- acpi_release_global_lock ");
379 status = acpi_release_global_lock(global_lock);
380 return acpi_failure(status, "acpi_release_global_lock");
381 }
382
acpi_test_bus(void)383 static int acpi_test_bus(void)
384 {
385 int state = 0;
386 acpi_status status;
387 acpi_handle bus_handle;
388 struct acpi_device *device;
389
390 status = acpi_get_handle(NULL, "\\_SB", &bus_handle);
391 if (acpi_failure(status, "acpi_get_handle"))
392 return 1;
393
394 prk_alert("TEST -- acpi_bus_get_device");
395 status = acpi_bus_get_device(bus_handle, &device);
396 if (acpi_failure(status, "acpi_bus_get_device"))
397 return 1;
398
399 prk_alert("TEST -- acpi_bus_update_power ");
400 status = acpi_bus_update_power(device->handle, &state);
401 if (acpi_failure(status, "error reading power state"))
402 return 1;
403
404 prk_info("acpi bus power state is %d", state);
405 return 0;
406 }
407
acpi_ec_io_ports(struct acpi_resource * resource,void * context)408 static acpi_status acpi_ec_io_ports(struct acpi_resource *resource,
409 void *context)
410 {
411 return 0;
412 }
413
acpi_test_resources(void)414 static int acpi_test_resources(void)
415 {
416 int err = 0;
417 acpi_status status;
418 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
419
420 /* skip if we don't find device with _CRC */
421 if (res_handle == 0)
422 return 0;
423
424 prk_alert("TEST -- acpi_get_current_resources");
425 status = acpi_get_current_resources(res_handle, &buffer);
426 err = acpi_failure(status, "failed get_current_resources");
427
428 #ifdef ACPI_FUTURE_USAGE
429 prk_alert("TEST -- acpi_get_possible_resources");
430 status = acpi_get_possible_resources(res_handle, &buffer);
431 err |= acpi_failure(status, "get_possible_resources");
432 #endif
433
434 prk_alert("TEST -- acpi_walk_resources ");
435 status = acpi_walk_resources(res_handle, METHOD_NAME__CRS,
436 acpi_ec_io_ports, NULL);
437 err |= acpi_failure(status, "Failed walk_resources");
438
439 return err;
440 }
441
acpi_sleep_test(void)442 static int acpi_sleep_test(void)
443 {
444 int err = 0;
445 acpi_status status;
446 u32 i;
447 u8 type_a, type_b;
448 prk_alert("TEST -- acpi_get_sleep_type_data ");
449
450 for (i = 0; i < ACPI_S_STATE_COUNT; ++i) {
451 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
452 if (ACPI_SUCCESS(status)) {
453 prk_info("get_sleep_type_data S%d a:%d b:%d",
454 i, type_a, type_b);
455 } else if (status != AE_NOT_FOUND) {
456 err |= 1;
457 }
458 }
459
460 return err;
461 }
462
acpi_test_register(void)463 static int acpi_test_register(void)
464 {
465 int i, err = 0;
466 u32 val;
467 acpi_status status;
468
469 prk_alert("TEST -- acpi_read_bit_register");
470 if (acpi_hw_reduced) {
471 prk_alert("Skipped due to the HW-reduced mode");
472 return 0;
473 }
474 /*
475 * ACPICA: Remove obsolete Flags parameter.
476 * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;
477 * a=commitdiff;h=d8c71b6d3b21cf21ad775e1cf6da95bf87bd5ad4
478 *
479 * ACPICA: Rename ACPI bit register access functions
480 * http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
481 * commit/?id=50ffba1bd3120b069617455545bc27bcf3cf7579
482 */
483 for (i = 0; i < ACPI_NUM_BITREG; ++i) {
484 status = acpi_read_bit_register(i, &val);
485 err |= acpi_failure(status, "acpi_read_bit_register");
486 if (ACPI_SUCCESS(status))
487 prk_alert("get register: %02x val: %04x", i, val);
488 }
489
490 return err;
491 }
492
ltp_get_dev_callback(acpi_handle obj,u32 depth,void * context,void ** ret)493 static acpi_status ltp_get_dev_callback(acpi_handle obj, u32 depth,
494 void *context, void **ret)
495 {
496 char *name = context;
497 char fullname[20];
498
499 /*
500 * Only SBA shows up in ACPI namespace, so its CSR space
501 * includes both SBA and IOC. Make SBA and IOC show up
502 * separately in PCI space.
503 */
504 sprintf(fullname, "%s SBA", name);
505 prk_info("get_dev_callback SBA name %s", fullname);
506 sprintf(fullname, "%s IOC", name);
507 prk_info("get_dev_callback IOC name %s", fullname);
508
509 return 0;
510 }
511
acpi_test_dev_callback(void)512 static int acpi_test_dev_callback(void)
513 {
514 acpi_status status;
515 prk_alert("TEST -- acpi_get_devices ");
516 status = acpi_get_devices(NULL, ltp_get_dev_callback, "LTP0001", NULL);
517 return acpi_failure(status, "acpi_get_devices");
518 }
519
520 static int current_test_case;
521 static int test_result;
522
device_release(struct device * dev)523 static void device_release(struct device *dev)
524 {
525 prk_info("device released");
526 }
527
528 static struct device tdev = {
529 .init_name = ACPI_TEST_NAME,
530 .release = device_release,
531 };
532
533 /* print test result to sysfs file */
sys_result(struct device * dev,struct device_attribute * attr,char * buf)534 static ssize_t sys_result(struct device *dev,
535 struct device_attribute *attr, char *buf)
536 {
537 return scnprintf(buf, PAGE_SIZE, "%d\n", test_result);
538 }
539 static DEVICE_ATTR(result, S_IRUSR, sys_result, NULL);
540
541 /* print found device description */
sys_str(struct device * dev,struct device_attribute * attr,char * buf)542 static ssize_t sys_str(struct device *dev,
543 struct device_attribute *attr, char *buf)
544 {
545 if (str_obj_result)
546 return scnprintf(buf, PAGE_SIZE, "%s", str_obj_result);
547 else
548 return 0;
549 }
550 static DEVICE_ATTR(str, S_IRUSR, sys_str, NULL);
551
552 /* print found device's sysfs path */
sys_path(struct device * dev,struct device_attribute * attr,char * buf)553 static ssize_t sys_path(struct device *dev,
554 struct device_attribute *attr, char *buf)
555 {
556 if (sysfs_path)
557 return scnprintf(buf, PAGE_SIZE, "%s", sysfs_path);
558 else
559 return 0;
560 }
561 static DEVICE_ATTR(path, S_IRUSR, sys_path, NULL);
562
sys_acpi_disabled(struct device * dev,struct device_attribute * attr,char * buf)563 static ssize_t sys_acpi_disabled(struct device *dev,
564 struct device_attribute *attr, char *buf)
565 {
566 return scnprintf(buf, PAGE_SIZE, "%d", acpi_disabled);
567 }
568 static DEVICE_ATTR(acpi_disabled, S_IRUSR, sys_acpi_disabled, NULL);
569
sys_tcase(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)570 static ssize_t sys_tcase(struct device *dev,
571 struct device_attribute *attr, const char *buf, size_t count)
572 {
573 sscanf(buf, "%d", ¤t_test_case);
574 prk_info("test-case %d", current_test_case);
575
576 switch (current_test_case) {
577 case ACPI_INIT:
578 test_result = acpi_init();
579 break;
580 case ACPI_TRAVERSE:
581 test_result = acpi_traverse_from_root();
582 break;
583 case ACPI_NOTIFY_HANDLER:
584 test_result = acpi_test_notify_handler();
585 break;
586 case ACPI_EVENT_HANDLER:
587 test_result = acpi_test_event_handler();
588 break;
589 case ACPI_GLOBAL_LOCK:
590 test_result = acpi_global_lock();
591 break;
592 case ACPI_TEST_BUS:
593 test_result = acpi_test_bus();
594 break;
595 case ACPI_TEST_RESOURCES:
596 test_result = acpi_test_resources();
597 break;
598 case ACPI_SLEEP_TEST:
599 test_result = acpi_sleep_test();
600 break;
601 case ACPI_TEST_REGISTER:
602 test_result = acpi_test_register();
603 break;
604 case ACPI_TEST_DEV_CALLBACK:
605 test_result = acpi_test_dev_callback();
606 break;
607 }
608
609 return count;
610 }
611 static DEVICE_ATTR(tcase, S_IWUSR, NULL, sys_tcase);
612
init_module(void)613 int init_module(void)
614 {
615 int err = 0;
616 prk_info("Starting module");
617
618 err = device_register(&tdev);
619 if (err) {
620 prk_err("Unable to register device");
621 goto err0;
622 }
623 prk_info("device registered");
624
625 err = device_create_file(&tdev, &dev_attr_result);
626 if (err) {
627 prk_err("Can't create sysfs file 'result'");
628 goto err1;
629 }
630
631 err = device_create_file(&tdev, &dev_attr_str);
632 if (err) {
633 prk_err("Can't create sysfs file 'str'");
634 goto err2;
635 }
636
637 err = device_create_file(&tdev, &dev_attr_tcase);
638 if (err) {
639 prk_err(": Can't create sysfs file 'tc'");
640 goto err3;
641 }
642
643 err = device_create_file(&tdev, &dev_attr_path);
644 if (err) {
645 prk_err(": Can't create sysfs file 'path'");
646 goto err4;
647 }
648
649 err = device_create_file(&tdev, &dev_attr_acpi_disabled);
650 if (err) {
651 prk_err("Can't create sysfs file 'acpi_disabled'");
652 goto err5;
653 }
654
655 return 0;
656
657 err5:
658 device_remove_file(&tdev, &dev_attr_path);
659 err4:
660 device_remove_file(&tdev, &dev_attr_tcase);
661 err3:
662 device_remove_file(&tdev, &dev_attr_str);
663 err2:
664 device_remove_file(&tdev, &dev_attr_result);
665 err1:
666 device_unregister(&tdev);
667 err0:
668 return err;
669 }
670
cleanup_module(void)671 void cleanup_module(void)
672 {
673 prk_info("Unloading module\n");
674
675 kfree(str_obj_result);
676 kfree(sysfs_path);
677
678 device_remove_file(&tdev, &dev_attr_result);
679 device_remove_file(&tdev, &dev_attr_str);
680 device_remove_file(&tdev, &dev_attr_tcase);
681 device_remove_file(&tdev, &dev_attr_path);
682 device_unregister(&tdev);
683 }
684