1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2019 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
4  */
5 
6 #include "mt76.h"
7 
8 struct sk_buff *
9 mt76_mcu_msg_alloc(const void *data, int head_len,
10 		   int data_len, int tail_len)
11 {
12 	struct sk_buff *skb;
13 
14 	skb = alloc_skb(head_len + data_len + tail_len,
15 			GFP_KERNEL);
16 	if (!skb)
17 		return NULL;
18 
19 	skb_reserve(skb, head_len);
20 	if (data && data_len)
21 		skb_put_data(skb, data, data_len);
22 
23 	return skb;
24 }
25 EXPORT_SYMBOL_GPL(mt76_mcu_msg_alloc);
26 
27 /* mmio */
28 struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,
29 				      unsigned long expires)
30 {
31 	unsigned long timeout;
32 
33 	if (!time_is_after_jiffies(expires))
34 		return NULL;
35 
36 	timeout = expires - jiffies;
37 	wait_event_timeout(dev->mmio.mcu.wait,
38 			   !skb_queue_empty(&dev->mmio.mcu.res_q),
39 			   timeout);
40 	return skb_dequeue(&dev->mmio.mcu.res_q);
41 }
42 EXPORT_SYMBOL_GPL(mt76_mcu_get_response);
43 
44 void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb)
45 {
46 	skb_queue_tail(&dev->mmio.mcu.res_q, skb);
47 	wake_up(&dev->mmio.mcu.wait);
48 }
49 EXPORT_SYMBOL_GPL(mt76_mcu_rx_event);
50