1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
|
/*-
* Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
* Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
* Copyright (c) 2000 Michael Smith
* Copyright (c) 2000 BSDi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include "opt_acpi.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/ioccom.h>
#include <sys/reboot.h>
#include <sys/sysctl.h>
#include <sys/ctype.h>
#include <machine/clock.h>
#include <machine/resource.h>
#include "acpi.h"
#include <dev/acpica/acpivar.h>
#include <dev/acpica/acpiio.h>
MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
/*
* Hooks for the ACPI CA debugging infrastructure
*/
#define _COMPONENT BUS_MANAGER
MODULE_NAME("ACPI")
/*
* Character device
*/
static d_open_t acpiopen;
static d_close_t acpiclose;
static d_ioctl_t acpiioctl;
#define CDEV_MAJOR 152
static struct cdevsw acpi_cdevsw = {
acpiopen,
acpiclose,
noread,
nowrite,
acpiioctl,
nopoll,
nommap,
nostrategy,
"acpi",
CDEV_MAJOR,
nodump,
nopsize,
0,
-1
};
static const char* sleep_state_names[] = {
"S0", "S1", "S2", "S3", "S4", "S4B", "S5" };
static void acpi_identify(driver_t *driver, device_t parent);
static int acpi_probe(device_t dev);
static int acpi_attach(device_t dev);
static device_t acpi_add_child(device_t bus, int order, const char *name, int unit);
static int acpi_print_resources(struct resource_list *rl, const char *name, int type,
const char *format);
static int acpi_print_child(device_t bus, device_t child);
static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
u_long count);
static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
u_long *countp);
static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
u_long start, u_long end, u_long count, u_int flags);
static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
static void acpi_probe_children(device_t bus);
static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
static void acpi_shutdown_pre_sync(void *arg, int howto);
static void acpi_shutdown_final(void *arg, int howto);
static void acpi_enable_fixed_events(struct acpi_softc *sc);
#ifdef ACPI_DEBUG
static void acpi_set_debugging(void);
#endif
static void acpi_system_eventhandler_sleep(void *arg, int state);
static void acpi_system_eventhandler_wakeup(void *arg, int state);
static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
static device_method_t acpi_methods[] = {
/* Device interface */
DEVMETHOD(device_identify, acpi_identify),
DEVMETHOD(device_probe, acpi_probe),
DEVMETHOD(device_attach, acpi_attach),
DEVMETHOD(device_shutdown, bus_generic_shutdown),
DEVMETHOD(device_suspend, bus_generic_suspend),
DEVMETHOD(device_resume, bus_generic_resume),
/* Bus interface */
DEVMETHOD(bus_add_child, acpi_add_child),
DEVMETHOD(bus_print_child, acpi_print_child),
DEVMETHOD(bus_read_ivar, acpi_read_ivar),
DEVMETHOD(bus_write_ivar, acpi_write_ivar),
DEVMETHOD(bus_set_resource, acpi_set_resource),
DEVMETHOD(bus_get_resource, acpi_get_resource),
DEVMETHOD(bus_alloc_resource, acpi_alloc_resource),
DEVMETHOD(bus_release_resource, acpi_release_resource),
DEVMETHOD(bus_driver_added, bus_generic_driver_added),
DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
{0, 0}
};
static driver_t acpi_driver = {
"acpi",
acpi_methods,
sizeof(struct acpi_softc),
};
devclass_t acpi_devclass;
DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, 0, 0);
SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
/*
* Detect ACPI, perform early initialisation
*/
static void
acpi_identify(driver_t *driver, device_t parent)
{
device_t child;
ACPI_PHYSICAL_ADDRESS rsdp;
int error;
#ifdef ENABLE_DEBUGGER
char *debugpoint = getenv("debug.acpi.debugger");
#endif
FUNCTION_TRACE(__FUNCTION__);
if(!cold){
printf("Don't load this driver from userland!!\n");
return ;
}
/*
* Make sure we're not being doubly invoked.
*/
if (device_find_child(parent, "acpi", 0) != NULL)
return_VOID;
#ifdef ACPI_DEBUG
acpi_set_debugging();
#endif
/*
* Start up ACPICA
*/
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "init"))
acpi_EnterDebugger();
#endif
if ((error = AcpiInitializeSubsystem()) != AE_OK) {
printf("ACPI: initialisation failed: %s\n", acpi_strerror(error));
return_VOID;
}
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "tables"))
acpi_EnterDebugger();
#endif
if (((error = AcpiFindRootPointer(&rsdp)) != AE_OK) ||
((error = AcpiLoadTables(rsdp)) != AE_OK)) {
printf("ACPI: table load failed: %s\n", acpi_strerror(error));
return_VOID;
}
/*
* Attach the actual ACPI device.
*/
if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
device_printf(parent, "ACPI: could not attach\n");
return_VOID;
}
}
/*
* Fetch some descriptive data from ACPI to put in our attach message
*/
static int
acpi_probe(device_t dev)
{
ACPI_TABLE_HEADER th;
char buf[20];
int error;
FUNCTION_TRACE(__FUNCTION__);
if ((error = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th)) != AE_OK) {
device_printf(dev, "couldn't get XSDT header: %s\n", acpi_strerror(error));
return_VALUE(ENXIO);
}
sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
device_set_desc_copy(dev, buf);
return_VALUE(0);
}
static int
acpi_attach(device_t dev)
{
struct acpi_softc *sc;
int error;
#ifdef ENABLE_DEBUGGER
char *debugpoint = getenv("debug.acpi.debugger");
#endif
FUNCTION_TRACE(__FUNCTION__);
sc = device_get_softc(dev);
bzero(sc, sizeof(*sc));
sc->acpi_dev = dev;
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "spaces"))
acpi_EnterDebugger();
#endif
/*
* Install the default address space handlers.
*/
if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
ADDRESS_SPACE_SYSTEM_MEMORY,
ACPI_DEFAULT_HANDLER,
NULL, NULL)) != AE_OK) {
device_printf(dev, "could not initialise SystemMemory handler: %s\n", acpi_strerror(error));
return_VALUE(ENXIO);
}
if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
ADDRESS_SPACE_SYSTEM_IO,
ACPI_DEFAULT_HANDLER,
NULL, NULL)) != AE_OK) {
device_printf(dev, "could not initialise SystemIO handler: %s\n", acpi_strerror(error));
return_VALUE(ENXIO);
}
if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
ADDRESS_SPACE_PCI_CONFIG,
ACPI_DEFAULT_HANDLER,
NULL, NULL)) != AE_OK) {
device_printf(dev, "could not initialise PciConfig handler: %s\n", acpi_strerror(error));
return_VALUE(ENXIO);
}
/*
* Bring ACPI fully online.
*
* Note that we request that device _STA and _INI methods not be run (ACPI_NO_DEVICE_INIT)
* and the final object initialisation pass be skipped (ACPI_NO_OBJECT_INIT).
*
* XXX We need to arrange for the object init pass after we have attached all our
* child devices.
*/
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "enable"))
acpi_EnterDebugger();
#endif
if ((error = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT)) != AE_OK) {
device_printf(dev, "could not enable ACPI: %s\n", acpi_strerror(error));
return_VALUE(ENXIO);
}
/*
* Setup our sysctl tree.
*
* XXX: This doesn't check to make sure that none of these fail.
*/
sysctl_ctx_init(&sc->acpi_sysctl_ctx);
sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
device_get_name(dev), CTLFLAG_RD, 0, "");
SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
/*
* Dispatch the default sleep state to devices.
* TBD: should be configured from userland policy manager.
*/
sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
acpi_enable_fixed_events(sc);
/*
* Scan the namespace and attach/initialise children.
*/
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "probe"))
acpi_EnterDebugger();
#endif
if (!acpi_disabled("bus"))
acpi_probe_children(dev);
/*
* Register our shutdown handlers
*/
EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
/*
* Register our acpi event handlers.
* XXX should be configurable eg. via userland policy manager.
*/
EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
/*
* Flag our initial states.
*/
sc->acpi_enabled = 1;
sc->acpi_sstate = ACPI_STATE_S0;
/*
* Create the control device
*/
sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi");
sc->acpi_dev_t->si_drv1 = sc;
#ifdef ENABLE_DEBUGGER
if (debugpoint && !strcmp(debugpoint, "running"))
acpi_EnterDebugger();
#endif
return_VALUE(0);
}
/*
* Handle a new device being added
*/
static device_t
acpi_add_child(device_t bus, int order, const char *name, int unit)
{
struct acpi_device *ad;
device_t child;
if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL)
return(NULL);
bzero(ad, sizeof(*ad));
resource_list_init(&ad->ad_rl);
child = device_add_child_ordered(bus, order, name, unit);
if (child != NULL)
device_set_ivars(child, ad);
return(child);
}
/*
* Print child device resource usage
*/
static int
acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format)
{
struct resource_list_entry *rle;
int printed, retval;
printed = 0;
retval = 0;
if (!SLIST_FIRST(rl))
return(0);
/* Yes, this is kinda cheating */
SLIST_FOREACH(rle, rl, link) {
if (rle->type == type) {
if (printed == 0)
retval += printf(" %s ", name);
else if (printed > 0)
retval += printf(",");
printed++;
retval += printf(format, rle->start);
if (rle->count > 1) {
retval += printf("-");
retval += printf(format, rle->start +
rle->count - 1);
}
}
}
return(retval);
}
static int
acpi_print_child(device_t bus, device_t child)
{
struct acpi_device *adev = device_get_ivars(child);
struct resource_list *rl = &adev->ad_rl;
int retval = 0;
retval += bus_print_child_header(bus, child);
retval += acpi_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
retval += acpi_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
retval += bus_print_child_footer(bus, child);
return(retval);
}
/*
* Handle per-device ivars
*/
static int
acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
{
struct acpi_device *ad;
if ((ad = device_get_ivars(child)) == NULL) {
printf("device has no ivars\n");
return(ENOENT);
}
switch(index) {
/* ACPI ivars */
case ACPI_IVAR_HANDLE:
*(ACPI_HANDLE *)result = ad->ad_handle;
break;
case ACPI_IVAR_MAGIC:
*(int *)result = ad->ad_magic;
break;
case ACPI_IVAR_PRIVATE:
*(void **)result = ad->ad_private;
break;
default:
panic("bad ivar read request (%d)\n", index);
return(ENOENT);
}
return(0);
}
static int
acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
{
struct acpi_device *ad;
if ((ad = device_get_ivars(child)) == NULL) {
printf("device has no ivars\n");
return(ENOENT);
}
switch(index) {
/* ACPI ivars */
case ACPI_IVAR_HANDLE:
ad->ad_handle = (ACPI_HANDLE)value;
break;
case ACPI_IVAR_MAGIC:
ad->ad_magic = (int )value;
break;
case ACPI_IVAR_PRIVATE:
ad->ad_private = (void *)value;
break;
default:
panic("bad ivar write request (%d)\n", index);
return(ENOENT);
}
return(0);
}
/*
* Handle child resource allocation/removal
*/
static int
acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
{
struct acpi_device *ad = device_get_ivars(child);
struct resource_list *rl = &ad->ad_rl;
resource_list_add(rl, type, rid, start, start + count -1, count);
return(0);
}
static int
acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
{
struct acpi_device *ad = device_get_ivars(child);
struct resource_list *rl = &ad->ad_rl;
struct resource_list_entry *rle;
rle = resource_list_find(rl, type, rid);
if (!rle)
return(ENOENT);
if (startp)
*startp = rle->start;
if (countp)
*countp = rle->count;
return(0);
}
static struct resource *
acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
u_long start, u_long end, u_long count, u_int flags)
{
struct acpi_device *ad = device_get_ivars(child);
struct resource_list *rl = &ad->ad_rl;
return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
}
static int
acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
{
struct acpi_device *ad = device_get_ivars(child);
struct resource_list *rl = &ad->ad_rl;
return(resource_list_release(rl, bus, child, type, rid, r));
}
/*
* Scan relevant portions of the ACPI namespace and attach child devices.
*
* Note that we only expect to find devices in the \_TZ_, \_SI_ and \_SB_ scopes,
* and \_TZ_ becomes obsolete in the ACPI 2.0 spec.
*/
static void
acpi_probe_children(device_t bus)
{
ACPI_HANDLE parent;
static char *scopes[] = {"\\_TZ_", "\\_SI", "\\_SB_", NULL};
int i;
FUNCTION_TRACE(__FUNCTION__);
/*
* Create any static children by calling device identify methods.
*/
DEBUG_PRINT(TRACE_OBJECTS, ("device identify routines\n"));
bus_generic_probe(bus);
/*
* Scan the namespace and insert placeholders for all the devices that
* we find.
*
* Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
* we want to create nodes for all devices, not just those that are currently
* present. (This assumes that we don't want to create/remove devices as they
* appear, which might be smarter.)
*/
DEBUG_PRINT(TRACE_OBJECTS, ("namespace scan\n"));
for (i = 0; scopes[i] != NULL; i++)
if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK)
AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
/*
* Scan all of the child devices we have created and let them probe/attach.
*/
DEBUG_PRINT(TRACE_OBJECTS, ("first bus_generic_attach\n"));
bus_generic_attach(bus);
/*
* Some of these children may have attached others as part of their attach
* process (eg. the root PCI bus driver), so rescan.
*/
DEBUG_PRINT(TRACE_OBJECTS, ("second bus_generic_attach\n"));
bus_generic_attach(bus);
return_VOID;
}
/*
* Evaluate a child device and determine whether we might attach a device to
* it.
*/
static ACPI_STATUS
acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
{
ACPI_OBJECT_TYPE type;
device_t child, bus = (device_t)context;
FUNCTION_TRACE(__FUNCTION__);
/*
* Skip this device if we think we'll have trouble with it.
*/
if (acpi_avoid(handle))
return_ACPI_STATUS(AE_OK);
if (AcpiGetType(handle, &type) == AE_OK) {
switch(type) {
case ACPI_TYPE_DEVICE:
case ACPI_TYPE_PROCESSOR:
case ACPI_TYPE_THERMAL:
case ACPI_TYPE_POWER:
if (acpi_disabled("children"))
break;
/*
* Create a placeholder device for this node. Sort the placeholder
* so that the probe/attach passes will run breadth-first.
*/
DEBUG_PRINT(TRACE_OBJECTS, ("scanning '%s'\n", acpi_name(handle)))
child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
acpi_set_handle(child, handle);
DEBUG_EXEC(device_probe_and_attach(child));
break;
}
}
return_ACPI_STATUS(AE_OK);
}
static void
acpi_shutdown_pre_sync(void *arg, int howto)
{
/*
* Disable all ACPI events before soft off, otherwise the system
* will be turned on again on some laptops.
*
* XXX this should probably be restricted to masking some events just
* before powering down, since we may still need ACPI during the
* shutdown process.
*/
acpi_Disable((struct acpi_softc *)arg);
}
static void
acpi_shutdown_final(void *arg, int howto)
{
ACPI_STATUS status;
if (howto & RB_POWEROFF) {
printf("Power system off using ACPI...\n");
if ((status = AcpiEnterSleepState(ACPI_STATE_S5)) != AE_OK) {
printf("ACPI power-off failed - %s\n", acpi_strerror(status));
} else {
DELAY(1000000);
printf("ACPI power-off failed - timeout\n");
}
}
}
static void
acpi_enable_fixed_events(struct acpi_softc *sc)
{
static int first_time = 1;
#define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
/* Enable and clear fixed events and install handlers. */
if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
acpi_eventhandler_power_button_for_sleep, sc);
if (first_time) {
device_printf(sc->acpi_dev, MSGFORMAT, "power");
}
}
if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
acpi_eventhandler_sleep_button_for_sleep, sc);
if (first_time) {
device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
}
}
first_time = 0;
}
/*
* Match a HID string against a device
*/
BOOLEAN
acpi_MatchHid(device_t dev, char *hid)
{
ACPI_HANDLE h;
ACPI_DEVICE_INFO devinfo;
ACPI_STATUS error;
if (hid == NULL)
return(FALSE);
if ((h = acpi_get_handle(dev)) == NULL)
return(FALSE);
if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
return(FALSE);
if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
return(TRUE);
return(FALSE);
}
/*
* Perform the tedious double-get procedure required for fetching something into
* an ACPI_BUFFER that has not been initialised.
*/
ACPI_STATUS
acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf)
{
ACPI_STATUS status;
buf->Length = 0;
buf->Pointer = NULL;
if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW)
return(status);
if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL)
return(AE_NO_MEMORY);
return(func(handle, buf));
}
/*
* Allocate a buffer with a preset data size.
*/
ACPI_BUFFER *
acpi_AllocBuffer(int size)
{
ACPI_BUFFER *buf;
if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
return(NULL);
buf->Length = size;
buf->Pointer = (void *)(buf + 1);
return(buf);
}
static ACPI_STATUS __inline
acpi_wakeup(UINT8 state)
{
UINT16 Count;
ACPI_STATUS Status;
ACPI_OBJECT_LIST Arg_list;
ACPI_OBJECT Arg;
ACPI_OBJECT Objects[3]; /* package plus 2 number objects */
ACPI_BUFFER ReturnBuffer;
FUNCTION_TRACE_U32(__FUNCTION__, state);
/* wait for the WAK_STS bit */
Count = 0;
while (!(AcpiHwRegisterBitAccess(ACPI_READ, ACPI_MTX_LOCK, WAK_STS))) {
AcpiOsSleepUsec(1000);
/*
* Some BIOSes don't set WAK_STS at all,
* give up waiting for wakeup if we time out.
*/
if (Count > 1000) {
break; /* giving up */
}
Count++;
}
/*
* Evaluate the _WAK method
*/
MEMSET(&Arg_list, 0, sizeof(Arg_list));
Arg_list.Count = 1;
Arg_list.Pointer = &Arg;
MEMSET(&Arg, 0, sizeof(Arg));
Arg.Type = ACPI_TYPE_INTEGER;
Arg.Integer.Value = state;
/* Set up _WAK result code buffer */
MEMSET(Objects, 0, sizeof(Objects));
ReturnBuffer.Length = sizeof(Objects);
ReturnBuffer.Pointer = Objects;
AcpiEvaluateObject (NULL, "\\_WAK", &Arg_list, &ReturnBuffer);
Status = AE_OK;
/* Check result code for _WAK */
if (Objects[0].Type != ACPI_TYPE_PACKAGE ||
Objects[1].Type != ACPI_TYPE_INTEGER ||
Objects[2].Type != ACPI_TYPE_INTEGER) {
/*
* In many BIOSes, _WAK doesn't return a result code.
* We don't need to worry about it too much :-).
*/
DEBUG_PRINT (ACPI_INFO,
("acpi_wakeup: _WAK result code is corrupted, "
"but should be OK.\n"));
} else {
/* evaluate status code */
switch (Objects[1].Integer.Value) {
case 0x00000001:
DEBUG_PRINT (ACPI_ERROR,
("acpi_wakeup: Wake was signaled "
"but failed due to lack of power.\n"));
Status = AE_ERROR;
break;
case 0x00000002:
DEBUG_PRINT (ACPI_ERROR,
("acpi_wakeup: Wake was signaled "
"but failed due to thermal condition.\n"));
Status = AE_ERROR;
break;
}
/* evaluate PSS code */
if (Objects[2].Integer.Value == 0) {
DEBUG_PRINT (ACPI_ERROR,
("acpi_wakeup: The targeted S-state "
"was not entered because of too much current "
"being drawn from the power supply.\n"));
Status = AE_ERROR;
}
}
return_ACPI_STATUS(Status);
}
/*
* Set the system sleep state
*
* Currently we only support S1 and S5
*/
ACPI_STATUS
acpi_SetSleepState(struct acpi_softc *sc, int state)
{
ACPI_STATUS status = AE_OK;
FUNCTION_TRACE_U32(__FUNCTION__, state);
switch (state) {
case ACPI_STATE_S0: /* XXX only for testing */
status = AcpiEnterSleepState((UINT8)state);
if (status != AE_OK) {
device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status));
}
break;
case ACPI_STATE_S1:
/*
* Inform all devices that we are going to sleep.
*/
if (DEVICE_SUSPEND(root_bus) != 0) {
/*
* Re-wake the system.
*
* XXX note that a better two-pass approach with a 'veto' pass
* followed by a "real thing" pass would be better, but the
* current bus interface does not provide for this.
*/
DEVICE_RESUME(root_bus);
return_ACPI_STATUS(AE_ERROR);
}
sc->acpi_sstate = state;
status = AcpiEnterSleepState((UINT8)state);
if (status != AE_OK) {
device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", acpi_strerror(status));
break;
}
acpi_wakeup((UINT8)state);
DEVICE_RESUME(root_bus);
sc->acpi_sstate = ACPI_STATE_S0;
acpi_enable_fixed_events(sc);
break;
case ACPI_STATE_S5:
/*
* Shut down cleanly and power off. This will call us back through the
* shutdown handlers.
*/
shutdown_nice(RB_POWEROFF);
break;
default:
status = AE_BAD_PARAMETER;
break;
}
return_ACPI_STATUS(status);
}
/*
* Enable/Disable ACPI
*/
ACPI_STATUS
acpi_Enable(struct acpi_softc *sc)
{
ACPI_STATUS status;
u_int32_t flags;
FUNCTION_TRACE(__FUNCTION__);
flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
if (!sc->acpi_enabled) {
status = AcpiEnableSubsystem(flags);
} else {
status = AE_OK;
}
if (status == AE_OK)
sc->acpi_enabled = 1;
return_ACPI_STATUS(status);
}
ACPI_STATUS
acpi_Disable(struct acpi_softc *sc)
{
ACPI_STATUS status;
FUNCTION_TRACE(__FUNCTION__);
if (sc->acpi_enabled) {
status = AcpiDisable();
} else {
status = AE_OK;
}
if (status == AE_OK)
sc->acpi_enabled = 0;
return_ACPI_STATUS(status);
}
/*
* Returns true if the device is actually present and should
* be attached to. This requires the present, enabled, UI-visible
* and diagnostics-passed bits to be set.
*/
BOOLEAN
acpi_DeviceIsPresent(device_t dev)
{
ACPI_HANDLE h;
ACPI_DEVICE_INFO devinfo;
ACPI_STATUS error;
if ((h = acpi_get_handle(dev)) == NULL)
return(FALSE);
if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK)
return(FALSE);
if ((devinfo.Valid & ACPI_VALID_HID) && (devinfo.CurrentStatus & 0xf))
return(TRUE);
return(FALSE);
}
/*
* Evaluate a path that should return an integer.
*/
ACPI_STATUS
acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
{
ACPI_STATUS error;
ACPI_BUFFER buf;
ACPI_OBJECT param;
if (handle == NULL)
handle = ACPI_ROOT_OBJECT;
buf.Pointer = ¶m;
buf.Length = sizeof(param);
if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) {
if (param.Type == ACPI_TYPE_INTEGER) {
*number = param.Integer.Value;
} else {
error = AE_TYPE;
}
}
return(error);
}
/*
* ACPI Event Handlers
*/
/* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
static void
acpi_system_eventhandler_sleep(void *arg, int state)
{
FUNCTION_TRACE_U32(__FUNCTION__, state);
if (state >= ACPI_STATE_S0 && state <= ACPI_STATE_S5)
acpi_SetSleepState((struct acpi_softc *)arg, state);
return_VOID;
}
static void
acpi_system_eventhandler_wakeup(void *arg, int state)
{
FUNCTION_TRACE_U32(__FUNCTION__, state);
/* Well, what to do? :-) */
return_VOID;
}
/*
* ACPICA Event Handlers (FixedEvent, also called from button notify handler)
*/
UINT32
acpi_eventhandler_power_button_for_sleep(void *context)
{
struct acpi_softc *sc = (struct acpi_softc *)context;
FUNCTION_TRACE(__FUNCTION__);
EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
return_VALUE(INTERRUPT_HANDLED);
}
UINT32
acpi_eventhandler_power_button_for_wakeup(void *context)
{
struct acpi_softc *sc = (struct acpi_softc *)context;
FUNCTION_TRACE(__FUNCTION__);
EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
return_VALUE(INTERRUPT_HANDLED);
}
UINT32
acpi_eventhandler_sleep_button_for_sleep(void *context)
{
struct acpi_softc *sc = (struct acpi_softc *)context;
FUNCTION_TRACE(__FUNCTION__);
EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
return_VALUE(INTERRUPT_HANDLED);
}
UINT32
acpi_eventhandler_sleep_button_for_wakeup(void *context)
{
struct acpi_softc *sc = (struct acpi_softc *)context;
FUNCTION_TRACE(__FUNCTION__);
EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
return_VALUE(INTERRUPT_HANDLED);
}
/*
* XXX This is kinda ugly, and should not be here.
*/
struct acpi_staticbuf {
ACPI_BUFFER buffer;
char data[512];
};
char *
acpi_strerror(ACPI_STATUS excep)
{
static struct acpi_staticbuf buf;
buf.buffer.Length = 512;
buf.buffer.Pointer = &buf.data[0];
if (AcpiFormatException(excep, &buf.buffer) == AE_OK)
return(buf.buffer.Pointer);
return("(error formatting exception)");
}
char *
acpi_name(ACPI_HANDLE handle)
{
static struct acpi_staticbuf buf;
buf.buffer.Length = 512;
buf.buffer.Pointer = &buf.data[0];
if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK)
return(buf.buffer.Pointer);
return("(unknown path)");
}
/*
* Debugging/bug-avoidance. Avoid trying to fetch info on various
* parts of the namespace.
*/
int
acpi_avoid(ACPI_HANDLE handle)
{
char *cp, *np;
int len;
np = acpi_name(handle);
if (*np == '\\')
np++;
if ((cp = getenv("debug.acpi.avoid")) == NULL)
return(0);
/* scan the avoid list checking for a match */
for (;;) {
while ((*cp != 0) && isspace(*cp))
cp++;
if (*cp == 0)
break;
len = 0;
while ((cp[len] != 0) && !isspace(cp[len]))
len++;
if (!strncmp(cp, np, len)) {
DEBUG_PRINT(TRACE_OBJECTS, ("avoiding '%s'\n", np));
return(1);
}
cp += len;
}
return(0);
}
/*
* Debugging/bug-avoidance. Disable ACPI subsystem components.
*/
int
acpi_disabled(char *subsys)
{
char *cp;
int len;
if ((cp = getenv("debug.acpi.disable")) == NULL)
return(0);
if (!strcmp(cp, "all"))
return(1);
/* scan the disable list checking for a match */
for (;;) {
while ((*cp != 0) && isspace(*cp))
cp++;
if (*cp == 0)
break;
len = 0;
while ((cp[len] != 0) && !isspace(cp[len]))
len++;
if (!strncmp(cp, subsys, len)) {
DEBUG_PRINT(TRACE_OBJECTS, ("disabled '%s'\n", subsys));
return(1);
}
cp += len;
}
return(0);
}
/*
* Control interface.
*
* We multiplex ioctls for all participating ACPI devices here. Individual
* drivers wanting to be accessible via /dev/acpi should use the register/deregister
* interface to make their handlers visible.
*/
struct acpi_ioctl_hook
{
TAILQ_ENTRY(acpi_ioctl_hook) link;
u_long cmd;
int (* fn)(u_long cmd, caddr_t addr, void *arg);
void *arg;
};
static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks;
static int acpi_ioctl_hooks_initted;
/*
* Register an ioctl handler.
*/
int
acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
{
struct acpi_ioctl_hook *hp;
if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
return(ENOMEM);
hp->cmd = cmd;
hp->fn = fn;
hp->arg = arg;
if (acpi_ioctl_hooks_initted == 0) {
TAILQ_INIT(&acpi_ioctl_hooks);
acpi_ioctl_hooks_initted = 1;
}
TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
return(0);
}
/*
* Deregister an ioctl handler.
*/
void
acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
{
struct acpi_ioctl_hook *hp;
TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
if ((hp->cmd == cmd) && (hp->fn == fn))
break;
if (hp != NULL) {
TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
free(hp, M_ACPIDEV);
}
}
static int
acpiopen(dev_t dev, int flag, int fmt, struct proc *p)
{
return(0);
}
static int
acpiclose(dev_t dev, int flag, int fmt, struct proc *p)
{
return(0);
}
static int
acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
{
struct acpi_softc *sc;
struct acpi_ioctl_hook *hp;
int error, xerror, state;
error = state = 0;
sc = dev->si_drv1;
/*
* Scan the list of registered ioctls, looking for handlers.
*/
if (acpi_ioctl_hooks_initted) {
TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
if (hp->cmd == cmd) {
xerror = hp->fn(cmd, addr, hp->arg);
if (xerror != 0)
error = xerror;
goto out;
}
}
}
/*
* Core system ioctls.
*/
switch (cmd) {
case ACPIIO_ENABLE:
if (ACPI_FAILURE(acpi_Enable(sc)))
error = ENXIO;
break;
case ACPIIO_DISABLE:
if (ACPI_FAILURE(acpi_Disable(sc)))
error = ENXIO;
break;
case ACPIIO_SETSLPSTATE:
if (!sc->acpi_enabled) {
error = ENXIO;
break;
}
state = *(int *)addr;
if (state >= ACPI_STATE_S0 && state <= ACPI_STATE_S5) {
acpi_SetSleepState(sc, state);
} else {
error = EINVAL;
}
break;
default:
if (error == 0)
error = EINVAL;
break;
}
out:
return(error);
}
static int
acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
{
char sleep_state[10];
int error;
u_int new_state, old_state;
old_state = *(u_int *)oidp->oid_arg1;
if (old_state > ACPI_STATE_S5)
strcpy(sleep_state, "unknown");
else
strncpy(sleep_state, sleep_state_names[old_state],
sizeof(sleep_state_names[old_state]));
error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
if (error == 0 && req->newptr != NULL) {
for (new_state = ACPI_STATE_S0; new_state <= ACPI_STATE_S5; new_state++)
if (strncmp(sleep_state, sleep_state_names[new_state],
sizeof(sleep_state)) == 0)
break;
if (new_state != old_state && new_state <= ACPI_STATE_S5)
*(u_int *)oidp->oid_arg1 = new_state;
else
error = EINVAL;
}
return(error);
}
#ifdef ACPI_DEBUG
/*
* Support for parsing debug options from the kernel environment.
*
* Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
* by specifying the names of the bits in the debug.acpi.layer and
* debug.acpi.level environment variables. Bits may be unset by
* prefixing the bit name with !.
*/
struct debugtag
{
char *name;
UINT32 value;
};
static struct debugtag dbg_layer[] = {
{"GLOBAL", 0x00000001},
{"COMMON", 0x00000002},
{"PARSER", 0x00000004},
{"DISPATCHER", 0x00000008},
{"INTERPRETER", 0x00000010},
{"NAMESPACE", 0x00000020},
{"RESOURCE_MANAGER", 0x00000040},
{"TABLE_MANAGER", 0x00000080},
{"EVENT_HANDLING", 0x00000100},
{"HARDWARE", 0x00000200},
{"MISCELLANEOUS", 0x00000400},
{"OS_DEPENDENT", 0x00000800},
{"BUS_MANAGER", 0x00001000},
{"PROCESSOR_CONTROL", 0x00002000},
{"SYSTEM_CONTROL", 0x00004000},
{"THERMAL_CONTROL", 0x00008000},
{"POWER_CONTROL", 0x00010000},
{"EMBEDDED_CONTROLLER", 0x00020000},
{"BATTERY", 0x00040000},
{"DEBUGGER", 0x00100000},
{"ALL_COMPONENTS", 0x001FFFFF},
{NULL, 0}
};
static struct debugtag dbg_level[] = {
{"ACPI_OK", 0x00000001},
{"ACPI_INFO", 0x00000002},
{"ACPI_WARN", 0x00000004},
{"ACPI_ERROR", 0x00000008},
{"ACPI_FATAL", 0x00000010},
{"ACPI_DEBUG_OBJECT", 0x00000020},
{"ACPI_ALL", 0x0000003F},
{"TRACE_PARSE", 0x00000100},
{"TRACE_DISPATCH", 0x00000200},
{"TRACE_LOAD", 0x00000400},
{"TRACE_EXEC", 0x00000800},
{"TRACE_NAMES", 0x00001000},
{"TRACE_OPREGION", 0x00002000},
{"TRACE_BFIELD", 0x00004000},
{"TRACE_TRASH", 0x00008000},
{"TRACE_TABLES", 0x00010000},
{"TRACE_FUNCTIONS", 0x00020000},
{"TRACE_VALUES", 0x00040000},
{"TRACE_OBJECTS", 0x00080000},
{"TRACE_ALLOCATIONS", 0x00100000},
{"TRACE_RESOURCES", 0x00200000},
{"TRACE_IO", 0x00400000},
{"TRACE_INTERRUPTS", 0x00800000},
{"TRACE_USER_REQUESTS", 0x01000000},
{"TRACE_PACKAGE", 0x02000000},
{"TRACE_MUTEX", 0x04000000},
{"TRACE_ALL", 0x0FFFFF00},
{"VERBOSE_AML_DISASSEMBLE", 0x10000000},
{"VERBOSE_INFO", 0x20000000},
{"VERBOSE_TABLES", 0x40000000},
{"VERBOSE_EVENTS", 0x80000000},
{"VERBOSE_ALL", 0xF0000000},
{NULL, 0}
};
static void
acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
{
char *ep;
int i, l;
int set;
while (*cp) {
if (isspace(*cp)) {
cp++;
continue;
}
ep = cp;
while (*ep && !isspace(*ep))
ep++;
if (*cp == '!') {
set = 0;
cp++;
if (cp == ep)
continue;
} else {
set = 1;
}
l = ep - cp;
for (i = 0; tag[i].name != NULL; i++) {
if (!strncmp(cp, tag[i].name, l)) {
if (set) {
*flag |= tag[i].value;
} else {
*flag &= ~tag[i].value;
}
printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
}
}
cp = ep;
}
}
static void
acpi_set_debugging(void)
{
char *cp;
AcpiDbgLayer = 0;
AcpiDbgLevel = 0;
if ((cp = getenv("debug.acpi.layer")) != NULL)
acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
if ((cp = getenv("debug.acpi.level")) != NULL)
acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
}
#endif
|