This source file includes following definitions.
- os_enter_line_edit_mode
- os_exit_line_edit_mode
- acpi_os_initialize
- acpi_os_terminate
- acpi_os_get_root_pointer
- acpi_os_predefined_override
- acpi_os_table_override
- acpi_os_physical_table_override
- acpi_os_enter_sleep
- acpi_os_redirect_output
- acpi_os_printf
- acpi_os_vprintf
- acpi_os_get_line
- acpi_os_map_memory
- acpi_os_unmap_memory
- acpi_os_allocate
- acpi_os_allocate_zeroed
- acpi_os_free
- acpi_os_create_semaphore
- acpi_os_delete_semaphore
- acpi_os_wait_semaphore
- acpi_os_signal_semaphore
- acpi_os_create_semaphore
- acpi_os_delete_semaphore
- acpi_os_wait_semaphore
- acpi_os_signal_semaphore
- acpi_os_create_lock
- acpi_os_delete_lock
- acpi_os_acquire_lock
- acpi_os_release_lock
- acpi_os_install_interrupt_handler
- acpi_os_remove_interrupt_handler
- acpi_os_stall
- acpi_os_sleep
- acpi_os_get_timer
- acpi_os_read_pci_configuration
- acpi_os_write_pci_configuration
- acpi_os_read_port
- acpi_os_write_port
- acpi_os_read_memory
- acpi_os_write_memory
- acpi_os_readable
- acpi_os_writable
- acpi_os_signal
- acpi_os_get_thread_id
- acpi_os_execute
- acpi_os_get_thread_id
- acpi_os_execute
- acpi_os_wait_events_complete
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include <acpi/acpi.h>
15 #include "accommon.h"
16 #include "amlcode.h"
17 #include "acparser.h"
18 #include "acdebug.h"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25 #include <semaphore.h>
26 #include <pthread.h>
27 #include <errno.h>
28
29 #define _COMPONENT ACPI_OS_SERVICES
30 ACPI_MODULE_NAME("osunixxf")
31
32
33 void
34 ae_table_override(struct acpi_table_header *existing_table,
35 struct acpi_table_header **new_table);
36
37 typedef void *(*PTHREAD_CALLBACK) (void *);
38
39
40
41 #define ACPI_VPRINTF_BUFFER_SIZE 512
42 #define _ASCII_NEWLINE '\n'
43
44
45
46 #ifdef ACPI_EXEC_APP
47 #include <termios.h>
48
49 struct termios original_term_attributes;
50 int term_attributes_were_set = 0;
51
52 acpi_status acpi_ut_read_line(char *buffer, u32 buffer_length, u32 *bytes_read);
53
54 static void os_enter_line_edit_mode(void);
55
56 static void os_exit_line_edit_mode(void);
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 static void os_enter_line_edit_mode(void)
85 {
86 struct termios local_term_attributes;
87
88 term_attributes_were_set = 0;
89
90
91
92 if (!isatty(STDIN_FILENO)) {
93 return;
94 }
95
96
97
98 if (tcgetattr(STDIN_FILENO, &original_term_attributes)) {
99 fprintf(stderr, "Could not get terminal attributes!\n");
100 return;
101 }
102
103
104
105 memcpy(&local_term_attributes, &original_term_attributes,
106 sizeof(struct termios));
107
108 local_term_attributes.c_lflag &= ~(ICANON | ECHO);
109 local_term_attributes.c_cc[VMIN] = 1;
110 local_term_attributes.c_cc[VTIME] = 0;
111
112 if (tcsetattr(STDIN_FILENO, TCSANOW, &local_term_attributes)) {
113 fprintf(stderr, "Could not set terminal attributes!\n");
114 return;
115 }
116
117 term_attributes_were_set = 1;
118 }
119
120 static void os_exit_line_edit_mode(void)
121 {
122
123 if (!term_attributes_were_set) {
124 return;
125 }
126
127
128
129 if (tcsetattr(STDIN_FILENO, TCSANOW, &original_term_attributes)) {
130 fprintf(stderr, "Could not restore terminal attributes!\n");
131 }
132 }
133
134 #else
135
136
137
138 #define os_enter_line_edit_mode()
139 #define os_exit_line_edit_mode()
140 #endif
141
142
143
144
145
146
147
148
149
150
151
152
153
154 acpi_status acpi_os_initialize(void)
155 {
156 acpi_status status;
157
158 acpi_gbl_output_file = stdout;
159
160 os_enter_line_edit_mode();
161
162 status = acpi_os_create_lock(&acpi_gbl_print_lock);
163 if (ACPI_FAILURE(status)) {
164 return (status);
165 }
166
167 return (AE_OK);
168 }
169
170 acpi_status acpi_os_terminate(void)
171 {
172
173 os_exit_line_edit_mode();
174 return (AE_OK);
175 }
176
177 #ifndef ACPI_USE_NATIVE_RSDP_POINTER
178
179
180
181
182
183
184
185
186
187
188
189
190 acpi_physical_address acpi_os_get_root_pointer(void)
191 {
192
193 return (0);
194 }
195 #endif
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 acpi_status
212 acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
213 acpi_string *new_val)
214 {
215
216 if (!init_val || !new_val) {
217 return (AE_BAD_PARAMETER);
218 }
219
220 *new_val = NULL;
221 return (AE_OK);
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 acpi_status
240 acpi_os_table_override(struct acpi_table_header *existing_table,
241 struct acpi_table_header **new_table)
242 {
243
244 if (!existing_table || !new_table) {
245 return (AE_BAD_PARAMETER);
246 }
247
248 *new_table = NULL;
249
250 #ifdef ACPI_EXEC_APP
251
252 ae_table_override(existing_table, new_table);
253 return (AE_OK);
254 #else
255
256 return (AE_NO_ACPI_TABLES);
257 #endif
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 acpi_status
277 acpi_os_physical_table_override(struct acpi_table_header *existing_table,
278 acpi_physical_address *new_address,
279 u32 *new_table_length)
280 {
281
282 return (AE_SUPPORT);
283 }
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 acpi_status acpi_os_enter_sleep(u8 sleep_state, u32 rega_value, u32 regb_value)
302 {
303
304 return (AE_OK);
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319 void acpi_os_redirect_output(void *destination)
320 {
321
322 acpi_gbl_output_file = destination;
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 void ACPI_INTERNAL_VAR_XFACE acpi_os_printf(const char *fmt, ...)
339 {
340 va_list args;
341 u8 flags;
342
343 flags = acpi_gbl_db_output_flags;
344 if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) {
345
346
347
348 if (acpi_gbl_debug_file) {
349
350
351
352 va_start(args, fmt);
353 vfprintf(acpi_gbl_debug_file, fmt, args);
354 va_end(args);
355 } else {
356
357
358 flags |= ACPI_DB_CONSOLE_OUTPUT;
359 }
360 }
361
362 if (flags & ACPI_DB_CONSOLE_OUTPUT) {
363 va_start(args, fmt);
364 vfprintf(acpi_gbl_output_file, fmt, args);
365 va_end(args);
366 }
367 }
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384 void acpi_os_vprintf(const char *fmt, va_list args)
385 {
386 u8 flags;
387 char buffer[ACPI_VPRINTF_BUFFER_SIZE];
388
389
390
391
392
393
394
395
396
397
398
399 vsnprintf(buffer, ACPI_VPRINTF_BUFFER_SIZE, fmt, args);
400
401 flags = acpi_gbl_db_output_flags;
402 if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) {
403
404
405
406 if (acpi_gbl_debug_file) {
407
408
409
410 fputs(buffer, acpi_gbl_debug_file);
411 } else {
412
413
414 flags |= ACPI_DB_CONSOLE_OUTPUT;
415 }
416 }
417
418 if (flags & ACPI_DB_CONSOLE_OUTPUT) {
419 fputs(buffer, acpi_gbl_output_file);
420 }
421 }
422
423 #ifndef ACPI_EXEC_APP
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440 acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
441 {
442 int input_char;
443 u32 end_of_line;
444
445
446
447 for (end_of_line = 0;; end_of_line++) {
448 if (end_of_line >= buffer_length) {
449 return (AE_BUFFER_OVERFLOW);
450 }
451
452 if ((input_char = getchar()) == EOF) {
453 return (AE_ERROR);
454 }
455
456 if (!input_char || input_char == _ASCII_NEWLINE) {
457 break;
458 }
459
460 buffer[end_of_line] = (char)input_char;
461 }
462
463
464
465 buffer[end_of_line] = 0;
466
467
468
469 if (bytes_read) {
470 *bytes_read = end_of_line;
471 }
472
473 return (AE_OK);
474 }
475 #endif
476
477 #ifndef ACPI_USE_NATIVE_MEMORY_MAPPING
478
479
480
481
482
483
484
485
486
487
488
489
490
491 void *acpi_os_map_memory(acpi_physical_address where, acpi_size length)
492 {
493
494 return (ACPI_TO_POINTER((acpi_size)where));
495 }
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511 void acpi_os_unmap_memory(void *where, acpi_size length)
512 {
513
514 return;
515 }
516 #endif
517
518
519
520
521
522
523
524
525
526
527
528
529
530 void *acpi_os_allocate(acpi_size size)
531 {
532 void *mem;
533
534 mem = (void *)malloc((size_t) size);
535 return (mem);
536 }
537
538 #ifdef USE_NATIVE_ALLOCATE_ZEROED
539
540
541
542
543
544
545
546
547
548
549
550
551 void *acpi_os_allocate_zeroed(acpi_size size)
552 {
553 void *mem;
554
555 mem = (void *)calloc(1, (size_t) size);
556 return (mem);
557 }
558 #endif
559
560
561
562
563
564
565
566
567
568
569
570
571
572 void acpi_os_free(void *mem)
573 {
574
575 free(mem);
576 }
577
578 #ifdef ACPI_SINGLE_THREADED
579
580
581
582
583
584
585
586
587
588
589 acpi_status
590 acpi_os_create_semaphore(u32 max_units,
591 u32 initial_units, acpi_handle *out_handle)
592 {
593 *out_handle = (acpi_handle)1;
594 return (AE_OK);
595 }
596
597 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
598 {
599 return (AE_OK);
600 }
601
602 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
603 {
604 return (AE_OK);
605 }
606
607 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
608 {
609 return (AE_OK);
610 }
611
612 #else
613
614
615
616
617
618
619
620
621
622
623
624
625
626 acpi_status
627 acpi_os_create_semaphore(u32 max_units,
628 u32 initial_units, acpi_handle *out_handle)
629 {
630 sem_t *sem;
631
632 if (!out_handle) {
633 return (AE_BAD_PARAMETER);
634 }
635 #ifdef __APPLE__
636 {
637 static int semaphore_count = 0;
638 char semaphore_name[32];
639
640 snprintf(semaphore_name, sizeof(semaphore_name), "acpi_sem_%d",
641 semaphore_count++);
642 printf("%s\n", semaphore_name);
643 sem =
644 sem_open(semaphore_name, O_EXCL | O_CREAT, 0755,
645 initial_units);
646 if (!sem) {
647 return (AE_NO_MEMORY);
648 }
649 sem_unlink(semaphore_name);
650 }
651
652 #else
653 sem = acpi_os_allocate(sizeof(sem_t));
654 if (!sem) {
655 return (AE_NO_MEMORY);
656 }
657
658 if (sem_init(sem, 0, initial_units) == -1) {
659 acpi_os_free(sem);
660 return (AE_BAD_PARAMETER);
661 }
662 #endif
663
664 *out_handle = (acpi_handle)sem;
665 return (AE_OK);
666 }
667
668
669
670
671
672
673
674
675
676
677
678
679
680 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
681 {
682 sem_t *sem = (sem_t *) handle;
683
684 if (!sem) {
685 return (AE_BAD_PARAMETER);
686 }
687 #ifdef __APPLE__
688 if (sem_close(sem) == -1) {
689 return (AE_BAD_PARAMETER);
690 }
691 #else
692 if (sem_destroy(sem) == -1) {
693 return (AE_BAD_PARAMETER);
694 }
695 #endif
696
697 return (AE_OK);
698 }
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714 acpi_status
715 acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 msec_timeout)
716 {
717 acpi_status status = AE_OK;
718 sem_t *sem = (sem_t *) handle;
719 int ret_val;
720 #ifndef ACPI_USE_ALTERNATE_TIMEOUT
721 struct timespec time;
722 #endif
723
724 if (!sem) {
725 return (AE_BAD_PARAMETER);
726 }
727
728 switch (msec_timeout) {
729
730
731
732
733
734
735
736 case 0:
737
738 if (sem_trywait(sem) == -1) {
739 status = (AE_TIME);
740 }
741 break;
742
743
744
745 case ACPI_WAIT_FOREVER:
746
747 while (((ret_val = sem_wait(sem)) == -1) && (errno == EINTR)) {
748 continue;
749 }
750 if (ret_val != 0) {
751 status = (AE_TIME);
752 }
753 break;
754
755
756
757 default:
758
759 #ifdef ACPI_USE_ALTERNATE_TIMEOUT
760
761
762
763
764 while (msec_timeout) {
765 if (sem_trywait(sem) == 0) {
766
767
768 return (AE_OK);
769 }
770
771 if (msec_timeout >= 10) {
772 msec_timeout -= 10;
773 usleep(10 * ACPI_USEC_PER_MSEC);
774 } else {
775 msec_timeout--;
776 usleep(ACPI_USEC_PER_MSEC);
777 }
778 }
779 status = (AE_TIME);
780 #else
781
782
783
784
785 if (clock_gettime(CLOCK_REALTIME, &time) == -1) {
786 perror("clock_gettime");
787 return (AE_TIME);
788 }
789
790 time.tv_sec += (msec_timeout / ACPI_MSEC_PER_SEC);
791 time.tv_nsec +=
792 ((msec_timeout % ACPI_MSEC_PER_SEC) * ACPI_NSEC_PER_MSEC);
793
794
795
796 if (time.tv_nsec >= ACPI_NSEC_PER_SEC) {
797 time.tv_sec += (time.tv_nsec / ACPI_NSEC_PER_SEC);
798 time.tv_nsec = (time.tv_nsec % ACPI_NSEC_PER_SEC);
799 }
800
801 while (((ret_val = sem_timedwait(sem, &time)) == -1)
802 && (errno == EINTR)) {
803 continue;
804
805 }
806
807 if (ret_val != 0) {
808 if (errno != ETIMEDOUT) {
809 perror("sem_timedwait");
810 }
811 status = (AE_TIME);
812 }
813 #endif
814 break;
815 }
816
817 return (status);
818 }
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
834 {
835 sem_t *sem = (sem_t *) handle;
836
837 if (!sem) {
838 return (AE_BAD_PARAMETER);
839 }
840
841 if (sem_post(sem) == -1) {
842 return (AE_LIMIT);
843 }
844
845 return (AE_OK);
846 }
847
848 #endif
849
850
851
852
853
854
855
856
857
858 acpi_status acpi_os_create_lock(acpi_spinlock * out_handle)
859 {
860
861 return (acpi_os_create_semaphore(1, 1, out_handle));
862 }
863
864 void acpi_os_delete_lock(acpi_spinlock handle)
865 {
866 acpi_os_delete_semaphore(handle);
867 }
868
869 acpi_cpu_flags acpi_os_acquire_lock(acpi_handle handle)
870 {
871 acpi_os_wait_semaphore(handle, 1, 0xFFFF);
872 return (0);
873 }
874
875 void acpi_os_release_lock(acpi_spinlock handle, acpi_cpu_flags flags)
876 {
877 acpi_os_signal_semaphore(handle, 1);
878 }
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895 u32
896 acpi_os_install_interrupt_handler(u32 interrupt_number,
897 acpi_osd_handler service_routine,
898 void *context)
899 {
900
901 return (AE_OK);
902 }
903
904
905
906
907
908
909
910
911
912
913
914
915
916 acpi_status
917 acpi_os_remove_interrupt_handler(u32 interrupt_number,
918 acpi_osd_handler service_routine)
919 {
920
921 return (AE_OK);
922 }
923
924
925
926
927
928
929
930
931
932
933
934
935
936 void acpi_os_stall(u32 microseconds)
937 {
938
939 if (microseconds) {
940 usleep(microseconds);
941 }
942 }
943
944
945
946
947
948
949
950
951
952
953
954
955
956 void acpi_os_sleep(u64 milliseconds)
957 {
958
959
960
961 sleep(milliseconds / ACPI_MSEC_PER_SEC);
962
963
964
965
966
967 usleep((milliseconds % ACPI_MSEC_PER_SEC) * ACPI_USEC_PER_MSEC);
968 }
969
970
971
972
973
974
975
976
977
978
979
980
981
982 u64 acpi_os_get_timer(void)
983 {
984 struct timeval time;
985
986
987
988 gettimeofday(&time, NULL);
989
990
991
992 return (((u64)time.tv_sec * ACPI_100NSEC_PER_SEC) +
993 ((u64)time.tv_usec * ACPI_100NSEC_PER_USEC));
994 }
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011 acpi_status
1012 acpi_os_read_pci_configuration(struct acpi_pci_id *pci_id,
1013 u32 pci_register, u64 *value, u32 width)
1014 {
1015
1016 *value = 0;
1017 return (AE_OK);
1018 }
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035 acpi_status
1036 acpi_os_write_pci_configuration(struct acpi_pci_id *pci_id,
1037 u32 pci_register, u64 value, u32 width)
1038 {
1039
1040 return (AE_OK);
1041 }
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057 acpi_status acpi_os_read_port(acpi_io_address address, u32 *value, u32 width)
1058 {
1059
1060 switch (width) {
1061 case 8:
1062
1063 *value = 0xFF;
1064 break;
1065
1066 case 16:
1067
1068 *value = 0xFFFF;
1069 break;
1070
1071 case 32:
1072
1073 *value = 0xFFFFFFFF;
1074 break;
1075
1076 default:
1077
1078 return (AE_BAD_PARAMETER);
1079 }
1080
1081 return (AE_OK);
1082 }
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098 acpi_status acpi_os_write_port(acpi_io_address address, u32 value, u32 width)
1099 {
1100
1101 return (AE_OK);
1102 }
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119 acpi_status
1120 acpi_os_read_memory(acpi_physical_address address, u64 *value, u32 width)
1121 {
1122
1123 switch (width) {
1124 case 8:
1125 case 16:
1126 case 32:
1127 case 64:
1128
1129 *value = 0;
1130 break;
1131
1132 default:
1133
1134 return (AE_BAD_PARAMETER);
1135 }
1136 return (AE_OK);
1137 }
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153 acpi_status
1154 acpi_os_write_memory(acpi_physical_address address, u64 value, u32 width)
1155 {
1156
1157 return (AE_OK);
1158 }
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173 u8 acpi_os_readable(void *pointer, acpi_size length)
1174 {
1175
1176 return (TRUE);
1177 }
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192 u8 acpi_os_writable(void *pointer, acpi_size length)
1193 {
1194
1195 return (TRUE);
1196 }
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211 acpi_status acpi_os_signal(u32 function, void *info)
1212 {
1213
1214 switch (function) {
1215 case ACPI_SIGNAL_FATAL:
1216
1217 break;
1218
1219 case ACPI_SIGNAL_BREAKPOINT:
1220
1221 break;
1222
1223 default:
1224
1225 break;
1226 }
1227
1228 return (AE_OK);
1229 }
1230
1231
1232
1233 #ifndef ACPI_SINGLE_THREADED
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246 acpi_thread_id acpi_os_get_thread_id(void)
1247 {
1248 pthread_t thread;
1249
1250 thread = pthread_self();
1251 return (ACPI_CAST_PTHREAD_T(thread));
1252 }
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268 acpi_status
1269 acpi_os_execute(acpi_execute_type type,
1270 acpi_osd_exec_callback function, void *context)
1271 {
1272 pthread_t thread;
1273 int ret;
1274
1275 ret =
1276 pthread_create(&thread, NULL, (PTHREAD_CALLBACK) function, context);
1277 if (ret) {
1278 acpi_os_printf("Create thread failed");
1279 }
1280 return (0);
1281 }
1282
1283 #else
1284 acpi_thread_id acpi_os_get_thread_id(void)
1285 {
1286 return (1);
1287 }
1288
1289 acpi_status
1290 acpi_os_execute(acpi_execute_type type,
1291 acpi_osd_exec_callback function, void *context)
1292 {
1293
1294 function(context);
1295
1296 return (AE_OK);
1297 }
1298
1299 #endif
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314 void acpi_os_wait_events_complete(void)
1315 {
1316 return;
1317 }