pinctrl: at91: enhance (debugfs) at91_gpio_dbg_show
[cascardo/linux.git] / tools / power / acpi / os_specific / service_layers / osunixxf.c
1 /******************************************************************************
2  *
3  * Module Name: osunixxf - UNIX OSL interfaces
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 /*
45  * These interfaces are required in order to compile the ASL compiler and the
46  * various ACPICA tools under Linux or other Unix-like system.
47  */
48 #include <acpi/acpi.h>
49 #include "accommon.h"
50 #include "amlcode.h"
51 #include "acparser.h"
52 #include "acdebug.h"
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <stdarg.h>
57 #include <unistd.h>
58 #include <sys/time.h>
59 #include <semaphore.h>
60 #include <pthread.h>
61 #include <errno.h>
62
63 #define _COMPONENT          ACPI_OS_SERVICES
64 ACPI_MODULE_NAME("osunixxf")
65
66 u8 acpi_gbl_debug_timeout = FALSE;
67
68 /* Upcalls to acpi_exec */
69
70 void
71 ae_table_override(struct acpi_table_header *existing_table,
72                   struct acpi_table_header **new_table);
73
74 typedef void *(*PTHREAD_CALLBACK) (void *);
75
76 /* Buffer used by acpi_os_vprintf */
77
78 #define ACPI_VPRINTF_BUFFER_SIZE    512
79 #define _ASCII_NEWLINE              '\n'
80
81 /* Terminal support for acpi_exec only */
82
83 #ifdef ACPI_EXEC_APP
84 #include <termios.h>
85
86 struct termios original_term_attributes;
87 int term_attributes_were_set = 0;
88
89 acpi_status acpi_ut_read_line(char *buffer, u32 buffer_length, u32 *bytes_read);
90
91 static void os_enter_line_edit_mode(void);
92
93 static void os_exit_line_edit_mode(void);
94
95 /******************************************************************************
96  *
97  * FUNCTION:    os_enter_line_edit_mode, os_exit_line_edit_mode
98  *
99  * PARAMETERS:  None
100  *
101  * RETURN:      None
102  *
103  * DESCRIPTION: Enter/Exit the raw character input mode for the terminal.
104  *
105  * Interactive line-editing support for the AML debugger. Used with the
106  * common/acgetline module.
107  *
108  * readline() is not used because of non-portability. It is not available
109  * on all systems, and if it is, often the package must be manually installed.
110  *
111  * Therefore, we use the POSIX tcgetattr/tcsetattr and do the minimal line
112  * editing that we need in acpi_os_get_line.
113  *
114  * If the POSIX tcgetattr/tcsetattr interfaces are unavailable, these
115  * calls will also work:
116  *     For os_enter_line_edit_mode: system ("stty cbreak -echo")
117  *     For os_exit_line_edit_mode: system ("stty cooked echo")
118  *
119  *****************************************************************************/
120
121 static void os_enter_line_edit_mode(void)
122 {
123         struct termios local_term_attributes;
124
125         /* Get and keep the original attributes */
126
127         if (tcgetattr(STDIN_FILENO, &original_term_attributes)) {
128                 fprintf(stderr, "Could not get terminal attributes!\n");
129                 return;
130         }
131
132         /* Set the new attributes to enable raw character input */
133
134         memcpy(&local_term_attributes, &original_term_attributes,
135                sizeof(struct termios));
136
137         local_term_attributes.c_lflag &= ~(ICANON | ECHO);
138         local_term_attributes.c_cc[VMIN] = 1;
139         local_term_attributes.c_cc[VTIME] = 0;
140
141         if (tcsetattr(STDIN_FILENO, TCSANOW, &local_term_attributes)) {
142                 fprintf(stderr, "Could not set terminal attributes!\n");
143                 return;
144         }
145
146         term_attributes_were_set = 1;
147 }
148
149 static void os_exit_line_edit_mode(void)
150 {
151
152         if (!term_attributes_were_set) {
153                 return;
154         }
155
156         /* Set terminal attributes back to the original values */
157
158         if (tcsetattr(STDIN_FILENO, TCSANOW, &original_term_attributes)) {
159                 fprintf(stderr, "Could not restore terminal attributes!\n");
160         }
161 }
162
163 #else
164
165 /* These functions are not needed for other ACPICA utilities */
166
167 #define os_enter_line_edit_mode()
168 #define os_exit_line_edit_mode()
169 #endif
170
171 /******************************************************************************
172  *
173  * FUNCTION:    acpi_os_initialize, acpi_os_terminate
174  *
175  * PARAMETERS:  None
176  *
177  * RETURN:      Status
178  *
179  * DESCRIPTION: Initialize and terminate this module.
180  *
181  *****************************************************************************/
182
183 acpi_status acpi_os_initialize(void)
184 {
185         acpi_status status;
186
187         acpi_gbl_output_file = stdout;
188
189         os_enter_line_edit_mode();
190
191         status = acpi_os_create_lock(&acpi_gbl_print_lock);
192         if (ACPI_FAILURE(status)) {
193                 return (status);
194         }
195
196         return (AE_OK);
197 }
198
199 acpi_status acpi_os_terminate(void)
200 {
201
202         os_exit_line_edit_mode();
203         return (AE_OK);
204 }
205
206 #ifndef ACPI_USE_NATIVE_RSDP_POINTER
207 /******************************************************************************
208  *
209  * FUNCTION:    acpi_os_get_root_pointer
210  *
211  * PARAMETERS:  None
212  *
213  * RETURN:      RSDP physical address
214  *
215  * DESCRIPTION: Gets the ACPI root pointer (RSDP)
216  *
217  *****************************************************************************/
218
219 acpi_physical_address acpi_os_get_root_pointer(void)
220 {
221
222         return (0);
223 }
224 #endif
225
226 /******************************************************************************
227  *
228  * FUNCTION:    acpi_os_predefined_override
229  *
230  * PARAMETERS:  init_val            - Initial value of the predefined object
231  *              new_val             - The new value for the object
232  *
233  * RETURN:      Status, pointer to value. Null pointer returned if not
234  *              overriding.
235  *
236  * DESCRIPTION: Allow the OS to override predefined names
237  *
238  *****************************************************************************/
239
240 acpi_status
241 acpi_os_predefined_override(const struct acpi_predefined_names * init_val,
242                             acpi_string * new_val)
243 {
244
245         if (!init_val || !new_val) {
246                 return (AE_BAD_PARAMETER);
247         }
248
249         *new_val = NULL;
250         return (AE_OK);
251 }
252
253 /******************************************************************************
254  *
255  * FUNCTION:    acpi_os_table_override
256  *
257  * PARAMETERS:  existing_table      - Header of current table (probably
258  *                                    firmware)
259  *              new_table           - Where an entire new table is returned.
260  *
261  * RETURN:      Status, pointer to new table. Null pointer returned if no
262  *              table is available to override
263  *
264  * DESCRIPTION: Return a different version of a table if one is available
265  *
266  *****************************************************************************/
267
268 acpi_status
269 acpi_os_table_override(struct acpi_table_header * existing_table,
270                        struct acpi_table_header ** new_table)
271 {
272
273         if (!existing_table || !new_table) {
274                 return (AE_BAD_PARAMETER);
275         }
276
277         *new_table = NULL;
278
279 #ifdef ACPI_EXEC_APP
280
281         ae_table_override(existing_table, new_table);
282         return (AE_OK);
283 #else
284
285         return (AE_NO_ACPI_TABLES);
286 #endif
287 }
288
289 /******************************************************************************
290  *
291  * FUNCTION:    acpi_os_physical_table_override
292  *
293  * PARAMETERS:  existing_table      - Header of current table (probably firmware)
294  *              new_address         - Where new table address is returned
295  *                                    (Physical address)
296  *              new_table_length    - Where new table length is returned
297  *
298  * RETURN:      Status, address/length of new table. Null pointer returned
299  *              if no table is available to override.
300  *
301  * DESCRIPTION: Returns AE_SUPPORT, function not used in user space.
302  *
303  *****************************************************************************/
304
305 acpi_status
306 acpi_os_physical_table_override(struct acpi_table_header * existing_table,
307                                 acpi_physical_address * new_address,
308                                 u32 *new_table_length)
309 {
310
311         return (AE_SUPPORT);
312 }
313
314 /******************************************************************************
315  *
316  * FUNCTION:    acpi_os_redirect_output
317  *
318  * PARAMETERS:  destination         - An open file handle/pointer
319  *
320  * RETURN:      None
321  *
322  * DESCRIPTION: Causes redirect of acpi_os_printf and acpi_os_vprintf
323  *
324  *****************************************************************************/
325
326 void acpi_os_redirect_output(void *destination)
327 {
328
329         acpi_gbl_output_file = destination;
330 }
331
332 /******************************************************************************
333  *
334  * FUNCTION:    acpi_os_printf
335  *
336  * PARAMETERS:  fmt, ...            - Standard printf format
337  *
338  * RETURN:      None
339  *
340  * DESCRIPTION: Formatted output. Note: very similar to acpi_os_vprintf
341  *              (performance), changes should be tracked in both functions.
342  *
343  *****************************************************************************/
344
345 void ACPI_INTERNAL_VAR_XFACE acpi_os_printf(const char *fmt, ...)
346 {
347         va_list args;
348         u8 flags;
349
350         flags = acpi_gbl_db_output_flags;
351         if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) {
352
353                 /* Output is directable to either a file (if open) or the console */
354
355                 if (acpi_gbl_debug_file) {
356
357                         /* Output file is open, send the output there */
358
359                         va_start(args, fmt);
360                         vfprintf(acpi_gbl_debug_file, fmt, args);
361                         va_end(args);
362                 } else {
363                         /* No redirection, send output to console (once only!) */
364
365                         flags |= ACPI_DB_CONSOLE_OUTPUT;
366                 }
367         }
368
369         if (flags & ACPI_DB_CONSOLE_OUTPUT) {
370                 va_start(args, fmt);
371                 vfprintf(acpi_gbl_output_file, fmt, args);
372                 va_end(args);
373         }
374 }
375
376 /******************************************************************************
377  *
378  * FUNCTION:    acpi_os_vprintf
379  *
380  * PARAMETERS:  fmt                 - Standard printf format
381  *              args                - Argument list
382  *
383  * RETURN:      None
384  *
385  * DESCRIPTION: Formatted output with argument list pointer. Note: very
386  *              similar to acpi_os_printf, changes should be tracked in both
387  *              functions.
388  *
389  *****************************************************************************/
390
391 void acpi_os_vprintf(const char *fmt, va_list args)
392 {
393         u8 flags;
394         char buffer[ACPI_VPRINTF_BUFFER_SIZE];
395
396         /*
397          * We build the output string in a local buffer because we may be
398          * outputting the buffer twice. Using vfprintf is problematic because
399          * some implementations modify the args pointer/structure during
400          * execution. Thus, we use the local buffer for portability.
401          *
402          * Note: Since this module is intended for use by the various ACPICA
403          * utilities/applications, we can safely declare the buffer on the stack.
404          * Also, This function is used for relatively small error messages only.
405          */
406         vsnprintf(buffer, ACPI_VPRINTF_BUFFER_SIZE, fmt, args);
407
408         flags = acpi_gbl_db_output_flags;
409         if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) {
410
411                 /* Output is directable to either a file (if open) or the console */
412
413                 if (acpi_gbl_debug_file) {
414
415                         /* Output file is open, send the output there */
416
417                         fputs(buffer, acpi_gbl_debug_file);
418                 } else {
419                         /* No redirection, send output to console (once only!) */
420
421                         flags |= ACPI_DB_CONSOLE_OUTPUT;
422                 }
423         }
424
425         if (flags & ACPI_DB_CONSOLE_OUTPUT) {
426                 fputs(buffer, acpi_gbl_output_file);
427         }
428 }
429
430 #ifndef ACPI_EXEC_APP
431 /******************************************************************************
432  *
433  * FUNCTION:    acpi_os_get_line
434  *
435  * PARAMETERS:  buffer              - Where to return the command line
436  *              buffer_length       - Maximum length of Buffer
437  *              bytes_read          - Where the actual byte count is returned
438  *
439  * RETURN:      Status and actual bytes read
440  *
441  * DESCRIPTION: Get the next input line from the terminal. NOTE: For the
442  *              acpi_exec utility, we use the acgetline module instead to
443  *              provide line-editing and history support.
444  *
445  *****************************************************************************/
446
447 acpi_status acpi_os_get_line(char *buffer, u32 buffer_length, u32 *bytes_read)
448 {
449         int input_char;
450         u32 end_of_line;
451
452         /* Standard acpi_os_get_line for all utilities except acpi_exec */
453
454         for (end_of_line = 0;; end_of_line++) {
455                 if (end_of_line >= buffer_length) {
456                         return (AE_BUFFER_OVERFLOW);
457                 }
458
459                 if ((input_char = getchar()) == EOF) {
460                         return (AE_ERROR);
461                 }
462
463                 if (!input_char || input_char == _ASCII_NEWLINE) {
464                         break;
465                 }
466
467                 buffer[end_of_line] = (char)input_char;
468         }
469
470         /* Null terminate the buffer */
471
472         buffer[end_of_line] = 0;
473
474         /* Return the number of bytes in the string */
475
476         if (bytes_read) {
477                 *bytes_read = end_of_line;
478         }
479
480         return (AE_OK);
481 }
482 #endif
483
484 #ifndef ACPI_USE_NATIVE_MEMORY_MAPPING
485 /******************************************************************************
486  *
487  * FUNCTION:    acpi_os_map_memory
488  *
489  * PARAMETERS:  where               - Physical address of memory to be mapped
490  *              length              - How much memory to map
491  *
492  * RETURN:      Pointer to mapped memory. Null on error.
493  *
494  * DESCRIPTION: Map physical memory into caller's address space
495  *
496  *****************************************************************************/
497
498 void *acpi_os_map_memory(acpi_physical_address where, acpi_size length)
499 {
500
501         return (ACPI_TO_POINTER((acpi_size) where));
502 }
503
504 /******************************************************************************
505  *
506  * FUNCTION:    acpi_os_unmap_memory
507  *
508  * PARAMETERS:  where               - Logical address of memory to be unmapped
509  *              length              - How much memory to unmap
510  *
511  * RETURN:      None.
512  *
513  * DESCRIPTION: Delete a previously created mapping. Where and Length must
514  *              correspond to a previous mapping exactly.
515  *
516  *****************************************************************************/
517
518 void acpi_os_unmap_memory(void *where, acpi_size length)
519 {
520
521         return;
522 }
523 #endif
524
525 /******************************************************************************
526  *
527  * FUNCTION:    acpi_os_allocate
528  *
529  * PARAMETERS:  size                - Amount to allocate, in bytes
530  *
531  * RETURN:      Pointer to the new allocation. Null on error.
532  *
533  * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
534  *
535  *****************************************************************************/
536
537 void *acpi_os_allocate(acpi_size size)
538 {
539         void *mem;
540
541         mem = (void *)malloc((size_t) size);
542         return (mem);
543 }
544
545 #ifdef USE_NATIVE_ALLOCATE_ZEROED
546 /******************************************************************************
547  *
548  * FUNCTION:    acpi_os_allocate_zeroed
549  *
550  * PARAMETERS:  size                - Amount to allocate, in bytes
551  *
552  * RETURN:      Pointer to the new allocation. Null on error.
553  *
554  * DESCRIPTION: Allocate and zero memory. Algorithm is dependent on the OS.
555  *
556  *****************************************************************************/
557
558 void *acpi_os_allocate_zeroed(acpi_size size)
559 {
560         void *mem;
561
562         mem = (void *)calloc(1, (size_t) size);
563         return (mem);
564 }
565 #endif
566
567 /******************************************************************************
568  *
569  * FUNCTION:    acpi_os_free
570  *
571  * PARAMETERS:  mem                 - Pointer to previously allocated memory
572  *
573  * RETURN:      None.
574  *
575  * DESCRIPTION: Free memory allocated via acpi_os_allocate
576  *
577  *****************************************************************************/
578
579 void acpi_os_free(void *mem)
580 {
581
582         free(mem);
583 }
584
585 #ifdef ACPI_SINGLE_THREADED
586 /******************************************************************************
587  *
588  * FUNCTION:    Semaphore stub functions
589  *
590  * DESCRIPTION: Stub functions used for single-thread applications that do
591  *              not require semaphore synchronization. Full implementations
592  *              of these functions appear after the stubs.
593  *
594  *****************************************************************************/
595
596 acpi_status
597 acpi_os_create_semaphore(u32 max_units,
598                          u32 initial_units, acpi_handle * out_handle)
599 {
600         *out_handle = (acpi_handle) 1;
601         return (AE_OK);
602 }
603
604 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
605 {
606         return (AE_OK);
607 }
608
609 acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
610 {
611         return (AE_OK);
612 }
613
614 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
615 {
616         return (AE_OK);
617 }
618
619 #else
620 /******************************************************************************
621  *
622  * FUNCTION:    acpi_os_create_semaphore
623  *
624  * PARAMETERS:  initial_units       - Units to be assigned to the new semaphore
625  *              out_handle          - Where a handle will be returned
626  *
627  * RETURN:      Status
628  *
629  * DESCRIPTION: Create an OS semaphore
630  *
631  *****************************************************************************/
632
633 acpi_status
634 acpi_os_create_semaphore(u32 max_units,
635                          u32 initial_units, acpi_handle * out_handle)
636 {
637         sem_t *sem;
638
639         if (!out_handle) {
640                 return (AE_BAD_PARAMETER);
641         }
642 #ifdef __APPLE__
643         {
644                 char *semaphore_name = tmpnam(NULL);
645
646                 sem =
647                     sem_open(semaphore_name, O_EXCL | O_CREAT, 0755,
648                              initial_units);
649                 if (!sem) {
650                         return (AE_NO_MEMORY);
651                 }
652                 sem_unlink(semaphore_name);     /* This just deletes the name */
653         }
654
655 #else
656         sem = acpi_os_allocate(sizeof(sem_t));
657         if (!sem) {
658                 return (AE_NO_MEMORY);
659         }
660
661         if (sem_init(sem, 0, initial_units) == -1) {
662                 acpi_os_free(sem);
663                 return (AE_BAD_PARAMETER);
664         }
665 #endif
666
667         *out_handle = (acpi_handle) sem;
668         return (AE_OK);
669 }
670
671 /******************************************************************************
672  *
673  * FUNCTION:    acpi_os_delete_semaphore
674  *
675  * PARAMETERS:  handle              - Handle returned by acpi_os_create_semaphore
676  *
677  * RETURN:      Status
678  *
679  * DESCRIPTION: Delete an OS semaphore
680  *
681  *****************************************************************************/
682
683 acpi_status acpi_os_delete_semaphore(acpi_handle handle)
684 {
685         sem_t *sem = (sem_t *) handle;
686
687         if (!sem) {
688                 return (AE_BAD_PARAMETER);
689         }
690
691         if (sem_destroy(sem) == -1) {
692                 return (AE_BAD_PARAMETER);
693         }
694
695         return (AE_OK);
696 }
697
698 /******************************************************************************
699  *
700  * FUNCTION:    acpi_os_wait_semaphore
701  *
702  * PARAMETERS:  handle              - Handle returned by acpi_os_create_semaphore
703  *              units               - How many units to wait for
704  *              msec_timeout        - How long to wait (milliseconds)
705  *
706  * RETURN:      Status
707  *
708  * DESCRIPTION: Wait for units
709  *
710  *****************************************************************************/
711
712 acpi_status
713 acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 msec_timeout)
714 {
715         acpi_status status = AE_OK;
716         sem_t *sem = (sem_t *) handle;
717 #ifndef ACPI_USE_ALTERNATE_TIMEOUT
718         struct timespec time;
719         int ret_val;
720 #endif
721
722         if (!sem) {
723                 return (AE_BAD_PARAMETER);
724         }
725
726         switch (msec_timeout) {
727                 /*
728                  * No Wait:
729                  * --------
730                  * A zero timeout value indicates that we shouldn't wait - just
731                  * acquire the semaphore if available otherwise return AE_TIME
732                  * (a.k.a. 'would block').
733                  */
734         case 0:
735
736                 if (sem_trywait(sem) == -1) {
737                         status = (AE_TIME);
738                 }
739                 break;
740
741                 /* Wait Indefinitely */
742
743         case ACPI_WAIT_FOREVER:
744
745                 if (sem_wait(sem)) {
746                         status = (AE_TIME);
747                 }
748                 break;
749
750                 /* Wait with msec_timeout */
751
752         default:
753
754 #ifdef ACPI_USE_ALTERNATE_TIMEOUT
755                 /*
756                  * Alternate timeout mechanism for environments where
757                  * sem_timedwait is not available or does not work properly.
758                  */
759                 while (msec_timeout) {
760                         if (sem_trywait(sem) == 0) {
761
762                                 /* Got the semaphore */
763                                 return (AE_OK);
764                         }
765
766                         if (msec_timeout >= 10) {
767                                 msec_timeout -= 10;
768                                 usleep(10 * ACPI_USEC_PER_MSEC);        /* ten milliseconds */
769                         } else {
770                                 msec_timeout--;
771                                 usleep(ACPI_USEC_PER_MSEC);     /* one millisecond */
772                         }
773                 }
774                 status = (AE_TIME);
775 #else
776                 /*
777                  * The interface to sem_timedwait is an absolute time, so we need to
778                  * get the current time, then add in the millisecond Timeout value.
779                  */
780                 if (clock_gettime(CLOCK_REALTIME, &time) == -1) {
781                         perror("clock_gettime");
782                         return (AE_TIME);
783                 }
784
785                 time.tv_sec += (msec_timeout / ACPI_MSEC_PER_SEC);
786                 time.tv_nsec +=
787                     ((msec_timeout % ACPI_MSEC_PER_SEC) * ACPI_NSEC_PER_MSEC);
788
789                 /* Handle nanosecond overflow (field must be less than one second) */
790
791                 if (time.tv_nsec >= ACPI_NSEC_PER_SEC) {
792                         time.tv_sec += (time.tv_nsec / ACPI_NSEC_PER_SEC);
793                         time.tv_nsec = (time.tv_nsec % ACPI_NSEC_PER_SEC);
794                 }
795
796                 while (((ret_val = sem_timedwait(sem, &time)) == -1)
797                        && (errno == EINTR)) {
798                         continue;
799                 }
800
801                 if (ret_val != 0) {
802                         if (errno != ETIMEDOUT) {
803                                 perror("sem_timedwait");
804                         }
805                         status = (AE_TIME);
806                 }
807 #endif
808                 break;
809         }
810
811         return (status);
812 }
813
814 /******************************************************************************
815  *
816  * FUNCTION:    acpi_os_signal_semaphore
817  *
818  * PARAMETERS:  handle              - Handle returned by acpi_os_create_semaphore
819  *              units               - Number of units to send
820  *
821  * RETURN:      Status
822  *
823  * DESCRIPTION: Send units
824  *
825  *****************************************************************************/
826
827 acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
828 {
829         sem_t *sem = (sem_t *) handle;
830
831         if (!sem) {
832                 return (AE_BAD_PARAMETER);
833         }
834
835         if (sem_post(sem) == -1) {
836                 return (AE_LIMIT);
837         }
838
839         return (AE_OK);
840 }
841
842 #endif                          /* ACPI_SINGLE_THREADED */
843
844 /******************************************************************************
845  *
846  * FUNCTION:    Spinlock interfaces
847  *
848  * DESCRIPTION: Map these interfaces to semaphore interfaces
849  *
850  *****************************************************************************/
851
852 acpi_status acpi_os_create_lock(acpi_spinlock * out_handle)
853 {
854
855         return (acpi_os_create_semaphore(1, 1, out_handle));
856 }
857
858 void acpi_os_delete_lock(acpi_spinlock handle)
859 {
860         acpi_os_delete_semaphore(handle);
861 }
862
863 acpi_cpu_flags acpi_os_acquire_lock(acpi_handle handle)
864 {
865         acpi_os_wait_semaphore(handle, 1, 0xFFFF);
866         return (0);
867 }
868
869 void acpi_os_release_lock(acpi_spinlock handle, acpi_cpu_flags flags)
870 {
871         acpi_os_signal_semaphore(handle, 1);
872 }
873
874 /******************************************************************************
875  *
876  * FUNCTION:    acpi_os_install_interrupt_handler
877  *
878  * PARAMETERS:  interrupt_number    - Level handler should respond to.
879  *              isr                 - Address of the ACPI interrupt handler
880  *              except_ptr          - Where status is returned
881  *
882  * RETURN:      Handle to the newly installed handler.
883  *
884  * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
885  *              OS-independent handler.
886  *
887  *****************************************************************************/
888
889 u32
890 acpi_os_install_interrupt_handler(u32 interrupt_number,
891                                   acpi_osd_handler service_routine,
892                                   void *context)
893 {
894
895         return (AE_OK);
896 }
897
898 /******************************************************************************
899  *
900  * FUNCTION:    acpi_os_remove_interrupt_handler
901  *
902  * PARAMETERS:  handle              - Returned when handler was installed
903  *
904  * RETURN:      Status
905  *
906  * DESCRIPTION: Uninstalls an interrupt handler.
907  *
908  *****************************************************************************/
909
910 acpi_status
911 acpi_os_remove_interrupt_handler(u32 interrupt_number,
912                                  acpi_osd_handler service_routine)
913 {
914
915         return (AE_OK);
916 }
917
918 /******************************************************************************
919  *
920  * FUNCTION:    acpi_os_stall
921  *
922  * PARAMETERS:  microseconds        - Time to sleep
923  *
924  * RETURN:      Blocks until sleep is completed.
925  *
926  * DESCRIPTION: Sleep at microsecond granularity
927  *
928  *****************************************************************************/
929
930 void acpi_os_stall(u32 microseconds)
931 {
932
933         if (microseconds) {
934                 usleep(microseconds);
935         }
936 }
937
938 /******************************************************************************
939  *
940  * FUNCTION:    acpi_os_sleep
941  *
942  * PARAMETERS:  milliseconds        - Time to sleep
943  *
944  * RETURN:      Blocks until sleep is completed.
945  *
946  * DESCRIPTION: Sleep at millisecond granularity
947  *
948  *****************************************************************************/
949
950 void acpi_os_sleep(u64 milliseconds)
951 {
952
953         /* Sleep for whole seconds */
954
955         sleep(milliseconds / ACPI_MSEC_PER_SEC);
956
957         /*
958          * Sleep for remaining microseconds.
959          * Arg to usleep() is in usecs and must be less than 1,000,000 (1 second).
960          */
961         usleep((milliseconds % ACPI_MSEC_PER_SEC) * ACPI_USEC_PER_MSEC);
962 }
963
964 /******************************************************************************
965  *
966  * FUNCTION:    acpi_os_get_timer
967  *
968  * PARAMETERS:  None
969  *
970  * RETURN:      Current time in 100 nanosecond units
971  *
972  * DESCRIPTION: Get the current system time
973  *
974  *****************************************************************************/
975
976 u64 acpi_os_get_timer(void)
977 {
978         struct timeval time;
979
980         /* This timer has sufficient resolution for user-space application code */
981
982         gettimeofday(&time, NULL);
983
984         /* (Seconds * 10^7 = 100ns(10^-7)) + (Microseconds(10^-6) * 10^1 = 100ns) */
985
986         return (((u64)time.tv_sec * ACPI_100NSEC_PER_SEC) +
987                 ((u64)time.tv_usec * ACPI_100NSEC_PER_USEC));
988 }
989
990 /******************************************************************************
991  *
992  * FUNCTION:    acpi_os_read_pci_configuration
993  *
994  * PARAMETERS:  pci_id              - Seg/Bus/Dev
995  *              pci_register        - Device Register
996  *              value               - Buffer where value is placed
997  *              width               - Number of bits
998  *
999  * RETURN:      Status
1000  *
1001  * DESCRIPTION: Read data from PCI configuration space
1002  *
1003  *****************************************************************************/
1004
1005 acpi_status
1006 acpi_os_read_pci_configuration(struct acpi_pci_id *pci_id,
1007                                u32 pci_register, u64 *value, u32 width)
1008 {
1009
1010         *value = 0;
1011         return (AE_OK);
1012 }
1013
1014 /******************************************************************************
1015  *
1016  * FUNCTION:    acpi_os_write_pci_configuration
1017  *
1018  * PARAMETERS:  pci_id              - Seg/Bus/Dev
1019  *              pci_register        - Device Register
1020  *              value               - Value to be written
1021  *              width               - Number of bits
1022  *
1023  * RETURN:      Status.
1024  *
1025  * DESCRIPTION: Write data to PCI configuration space
1026  *
1027  *****************************************************************************/
1028
1029 acpi_status
1030 acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id,
1031                                 u32 pci_register, u64 value, u32 width)
1032 {
1033
1034         return (AE_OK);
1035 }
1036
1037 /******************************************************************************
1038  *
1039  * FUNCTION:    acpi_os_read_port
1040  *
1041  * PARAMETERS:  address             - Address of I/O port/register to read
1042  *              value               - Where value is placed
1043  *              width               - Number of bits
1044  *
1045  * RETURN:      Value read from port
1046  *
1047  * DESCRIPTION: Read data from an I/O port or register
1048  *
1049  *****************************************************************************/
1050
1051 acpi_status acpi_os_read_port(acpi_io_address address, u32 *value, u32 width)
1052 {
1053
1054         switch (width) {
1055         case 8:
1056
1057                 *value = 0xFF;
1058                 break;
1059
1060         case 16:
1061
1062                 *value = 0xFFFF;
1063                 break;
1064
1065         case 32:
1066
1067                 *value = 0xFFFFFFFF;
1068                 break;
1069
1070         default:
1071
1072                 return (AE_BAD_PARAMETER);
1073         }
1074
1075         return (AE_OK);
1076 }
1077
1078 /******************************************************************************
1079  *
1080  * FUNCTION:    acpi_os_write_port
1081  *
1082  * PARAMETERS:  address             - Address of I/O port/register to write
1083  *              value               - Value to write
1084  *              width               - Number of bits
1085  *
1086  * RETURN:      None
1087  *
1088  * DESCRIPTION: Write data to an I/O port or register
1089  *
1090  *****************************************************************************/
1091
1092 acpi_status acpi_os_write_port(acpi_io_address address, u32 value, u32 width)
1093 {
1094
1095         return (AE_OK);
1096 }
1097
1098 /******************************************************************************
1099  *
1100  * FUNCTION:    acpi_os_read_memory
1101  *
1102  * PARAMETERS:  address             - Physical Memory Address to read
1103  *              value               - Where value is placed
1104  *              width               - Number of bits (8,16,32, or 64)
1105  *
1106  * RETURN:      Value read from physical memory address. Always returned
1107  *              as a 64-bit integer, regardless of the read width.
1108  *
1109  * DESCRIPTION: Read data from a physical memory address
1110  *
1111  *****************************************************************************/
1112
1113 acpi_status
1114 acpi_os_read_memory(acpi_physical_address address, u64 *value, u32 width)
1115 {
1116
1117         switch (width) {
1118         case 8:
1119         case 16:
1120         case 32:
1121         case 64:
1122
1123                 *value = 0;
1124                 break;
1125
1126         default:
1127
1128                 return (AE_BAD_PARAMETER);
1129         }
1130         return (AE_OK);
1131 }
1132
1133 /******************************************************************************
1134  *
1135  * FUNCTION:    acpi_os_write_memory
1136  *
1137  * PARAMETERS:  address             - Physical Memory Address to write
1138  *              value               - Value to write
1139  *              width               - Number of bits (8,16,32, or 64)
1140  *
1141  * RETURN:      None
1142  *
1143  * DESCRIPTION: Write data to a physical memory address
1144  *
1145  *****************************************************************************/
1146
1147 acpi_status
1148 acpi_os_write_memory(acpi_physical_address address, u64 value, u32 width)
1149 {
1150
1151         return (AE_OK);
1152 }
1153
1154 /******************************************************************************
1155  *
1156  * FUNCTION:    acpi_os_readable
1157  *
1158  * PARAMETERS:  pointer             - Area to be verified
1159  *              length              - Size of area
1160  *
1161  * RETURN:      TRUE if readable for entire length
1162  *
1163  * DESCRIPTION: Verify that a pointer is valid for reading
1164  *
1165  *****************************************************************************/
1166
1167 u8 acpi_os_readable(void *pointer, acpi_size length)
1168 {
1169
1170         return (TRUE);
1171 }
1172
1173 /******************************************************************************
1174  *
1175  * FUNCTION:    acpi_os_writable
1176  *
1177  * PARAMETERS:  pointer             - Area to be verified
1178  *              length              - Size of area
1179  *
1180  * RETURN:      TRUE if writable for entire length
1181  *
1182  * DESCRIPTION: Verify that a pointer is valid for writing
1183  *
1184  *****************************************************************************/
1185
1186 u8 acpi_os_writable(void *pointer, acpi_size length)
1187 {
1188
1189         return (TRUE);
1190 }
1191
1192 /******************************************************************************
1193  *
1194  * FUNCTION:    acpi_os_signal
1195  *
1196  * PARAMETERS:  function            - ACPI A signal function code
1197  *              info                - Pointer to function-dependent structure
1198  *
1199  * RETURN:      Status
1200  *
1201  * DESCRIPTION: Miscellaneous functions. Example implementation only.
1202  *
1203  *****************************************************************************/
1204
1205 acpi_status acpi_os_signal(u32 function, void *info)
1206 {
1207
1208         switch (function) {
1209         case ACPI_SIGNAL_FATAL:
1210
1211                 break;
1212
1213         case ACPI_SIGNAL_BREAKPOINT:
1214
1215                 break;
1216
1217         default:
1218
1219                 break;
1220         }
1221
1222         return (AE_OK);
1223 }
1224
1225 /* Optional multi-thread support */
1226
1227 #ifndef ACPI_SINGLE_THREADED
1228 /******************************************************************************
1229  *
1230  * FUNCTION:    acpi_os_get_thread_id
1231  *
1232  * PARAMETERS:  None
1233  *
1234  * RETURN:      Id of the running thread
1235  *
1236  * DESCRIPTION: Get the ID of the current (running) thread
1237  *
1238  *****************************************************************************/
1239
1240 acpi_thread_id acpi_os_get_thread_id(void)
1241 {
1242         pthread_t thread;
1243
1244         thread = pthread_self();
1245         return (ACPI_CAST_PTHREAD_T(thread));
1246 }
1247
1248 /******************************************************************************
1249  *
1250  * FUNCTION:    acpi_os_execute
1251  *
1252  * PARAMETERS:  type                - Type of execution
1253  *              function            - Address of the function to execute
1254  *              context             - Passed as a parameter to the function
1255  *
1256  * RETURN:      Status.
1257  *
1258  * DESCRIPTION: Execute a new thread
1259  *
1260  *****************************************************************************/
1261
1262 acpi_status
1263 acpi_os_execute(acpi_execute_type type,
1264                 acpi_osd_exec_callback function, void *context)
1265 {
1266         pthread_t thread;
1267         int ret;
1268
1269         ret =
1270             pthread_create(&thread, NULL, (PTHREAD_CALLBACK) function, context);
1271         if (ret) {
1272                 acpi_os_printf("Create thread failed");
1273         }
1274         return (0);
1275 }
1276
1277 #else                           /* ACPI_SINGLE_THREADED */
1278 acpi_thread_id acpi_os_get_thread_id(void)
1279 {
1280         return (1);
1281 }
1282
1283 acpi_status
1284 acpi_os_execute(acpi_execute_type type,
1285                 acpi_osd_exec_callback function, void *context)
1286 {
1287
1288         function(context);
1289
1290         return (AE_OK);
1291 }
1292
1293 #endif                          /* ACPI_SINGLE_THREADED */
1294
1295 /******************************************************************************
1296  *
1297  * FUNCTION:    acpi_os_wait_events_complete
1298  *
1299  * PARAMETERS:  None
1300  *
1301  * RETURN:      None
1302  *
1303  * DESCRIPTION: Wait for all asynchronous events to complete. This
1304  *              implementation does nothing.
1305  *
1306  *****************************************************************************/
1307
1308 void acpi_os_wait_events_complete(void)
1309 {
1310         return;
1311 }