1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
28 * Contact Information:
29 * Intel Linux Wireless <linuxwifi@intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
34 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 *****************************************************************************/
65 #include <linux/sched.h>
66 #include <linux/export.h>
69 #include "notif-wait.h"
72 void iwl_notification_wait_init(struct iwl_notif_wait_data
*notif_wait
)
74 spin_lock_init(¬if_wait
->notif_wait_lock
);
75 INIT_LIST_HEAD(¬if_wait
->notif_waits
);
76 init_waitqueue_head(¬if_wait
->notif_waitq
);
78 IWL_EXPORT_SYMBOL(iwl_notification_wait_init
);
80 bool iwl_notification_wait(struct iwl_notif_wait_data
*notif_wait
,
81 struct iwl_rx_packet
*pkt
)
83 bool triggered
= false;
85 if (!list_empty(¬if_wait
->notif_waits
)) {
86 struct iwl_notification_wait
*w
;
88 spin_lock(¬if_wait
->notif_wait_lock
);
89 list_for_each_entry(w
, ¬if_wait
->notif_waits
, list
) {
94 * If it already finished (triggered) or has been
95 * aborted then don't evaluate it again to avoid races,
96 * Otherwise the function could be called again even
97 * though it returned true before
99 if (w
->triggered
|| w
->aborted
)
102 for (i
= 0; i
< w
->n_cmds
; i
++) {
103 u16 rec_id
= WIDE_ID(pkt
->hdr
.group_id
,
106 if (w
->cmds
[i
] == rec_id
||
107 (!iwl_cmd_groupid(w
->cmds
[i
]) &&
108 DEF_ID(w
->cmds
[i
]) == rec_id
)) {
116 if (!w
->fn
|| w
->fn(notif_wait
, pkt
, w
->fn_data
)) {
121 spin_unlock(¬if_wait
->notif_wait_lock
);
126 IWL_EXPORT_SYMBOL(iwl_notification_wait
);
128 void iwl_abort_notification_waits(struct iwl_notif_wait_data
*notif_wait
)
130 struct iwl_notification_wait
*wait_entry
;
132 spin_lock(¬if_wait
->notif_wait_lock
);
133 list_for_each_entry(wait_entry
, ¬if_wait
->notif_waits
, list
)
134 wait_entry
->aborted
= true;
135 spin_unlock(¬if_wait
->notif_wait_lock
);
137 wake_up_all(¬if_wait
->notif_waitq
);
139 IWL_EXPORT_SYMBOL(iwl_abort_notification_waits
);
142 iwl_init_notification_wait(struct iwl_notif_wait_data
*notif_wait
,
143 struct iwl_notification_wait
*wait_entry
,
144 const u16
*cmds
, int n_cmds
,
145 bool (*fn
)(struct iwl_notif_wait_data
*notif_wait
,
146 struct iwl_rx_packet
*pkt
, void *data
),
149 if (WARN_ON(n_cmds
> MAX_NOTIF_CMDS
))
150 n_cmds
= MAX_NOTIF_CMDS
;
153 wait_entry
->fn_data
= fn_data
;
154 wait_entry
->n_cmds
= n_cmds
;
155 memcpy(wait_entry
->cmds
, cmds
, n_cmds
* sizeof(u16
));
156 wait_entry
->triggered
= false;
157 wait_entry
->aborted
= false;
159 spin_lock_bh(¬if_wait
->notif_wait_lock
);
160 list_add(&wait_entry
->list
, ¬if_wait
->notif_waits
);
161 spin_unlock_bh(¬if_wait
->notif_wait_lock
);
163 IWL_EXPORT_SYMBOL(iwl_init_notification_wait
);
165 void iwl_remove_notification(struct iwl_notif_wait_data
*notif_wait
,
166 struct iwl_notification_wait
*wait_entry
)
168 spin_lock_bh(¬if_wait
->notif_wait_lock
);
169 list_del(&wait_entry
->list
);
170 spin_unlock_bh(¬if_wait
->notif_wait_lock
);
172 IWL_EXPORT_SYMBOL(iwl_remove_notification
);
174 int iwl_wait_notification(struct iwl_notif_wait_data
*notif_wait
,
175 struct iwl_notification_wait
*wait_entry
,
176 unsigned long timeout
)
180 ret
= wait_event_timeout(notif_wait
->notif_waitq
,
181 wait_entry
->triggered
|| wait_entry
->aborted
,
184 iwl_remove_notification(notif_wait
, wait_entry
);
186 if (wait_entry
->aborted
)
189 /* return value is always >= 0 */
194 IWL_EXPORT_SYMBOL(iwl_wait_notification
);