GNU Linux-libre 4.9.292-gnu1
[releases.git] / drivers / hid / intel-ish-hid / ipc / utils.h
1 /*
2  * Utility macros of ISH
3  *
4  * Copyright (c) 2014-2016, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  */
15 #ifndef UTILS__H
16 #define UTILS__H
17
18 #define WAIT_FOR_SEND_SLICE     (HZ / 10)
19 #define WAIT_FOR_CONNECT_SLICE  (HZ / 10)
20
21 /*
22  * Waits for specified event when a thread that triggers event can't signal
23  * Also, waits *at_least* `timeinc` after condition is satisfied
24  */
25 #define timed_wait_for(timeinc, condition)                      \
26         do {                                                    \
27                 int completed = 0;                              \
28                 do {                                            \
29                         unsigned long   j;                      \
30                         int     done = 0;                       \
31                                                                 \
32                         completed = (condition);                \
33                         for (j = jiffies, done = 0; !done; ) {  \
34                                 schedule_timeout(timeinc);      \
35                                 if (time_is_before_eq_jiffies(j + timeinc)) \
36                                         done = 1;               \
37                         }                                       \
38                 } while (!(completed));                         \
39         } while (0)
40
41
42 /*
43  * Waits for specified event when a thread that triggers event
44  * can't signal with timeout (use whenever we may hang)
45  */
46 #define timed_wait_for_timeout(timeinc, condition, timeout)     \
47         do {                                                    \
48                 int     t = timeout;                            \
49                 do {                                            \
50                         unsigned long   j;                      \
51                         int     done = 0;                       \
52                                                                 \
53                         for (j = jiffies, done = 0; !done; ) {  \
54                                 schedule_timeout(timeinc);      \
55                                 if (time_is_before_eq_jiffies(j + timeinc)) \
56                                         done = 1;               \
57                         } \
58                         t -= timeinc;                           \
59                         if (t <= 0)                             \
60                                 break;                          \
61                 } while (!(condition));                         \
62         } while (0)
63
64 #endif /* UTILS__H */