1 /*
2  * Copyright (C) 2019 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include "mt76.h"
18 
19 struct sk_buff *
20 mt76_mcu_msg_alloc(const void *data, int head_len,
21 		   int data_len, int tail_len)
22 {
23 	struct sk_buff *skb;
24 
25 	skb = alloc_skb(head_len + data_len + tail_len,
26 			GFP_KERNEL);
27 	if (!skb)
28 		return NULL;
29 
30 	skb_reserve(skb, head_len);
31 	if (data && data_len)
32 		skb_put_data(skb, data, data_len);
33 
34 	return skb;
35 }
36 EXPORT_SYMBOL_GPL(mt76_mcu_msg_alloc);
37 
38 /* mmio */
39 struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,
40 				      unsigned long expires)
41 {
42 	unsigned long timeout;
43 
44 	if (!time_is_after_jiffies(expires))
45 		return NULL;
46 
47 	timeout = expires - jiffies;
48 	wait_event_timeout(dev->mmio.mcu.wait,
49 			   !skb_queue_empty(&dev->mmio.mcu.res_q),
50 			   timeout);
51 	return skb_dequeue(&dev->mmio.mcu.res_q);
52 }
53 EXPORT_SYMBOL_GPL(mt76_mcu_get_response);
54 
55 void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb)
56 {
57 	skb_queue_tail(&dev->mmio.mcu.res_q, skb);
58 	wake_up(&dev->mmio.mcu.wait);
59 }
60 EXPORT_SYMBOL_GPL(mt76_mcu_rx_event);
61