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 	int length = head_len + data_len + tail_len;
13 	struct sk_buff *skb;
14 
15 	skb = alloc_skb(length, GFP_KERNEL);
16 	if (!skb)
17 		return NULL;
18 
19 	memset(skb->head, 0, length);
20 	skb_reserve(skb, head_len);
21 
22 	if (data && data_len)
23 		skb_put_data(skb, data, data_len);
24 
25 	return skb;
26 }
27 EXPORT_SYMBOL_GPL(mt76_mcu_msg_alloc);
28 
29 struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,
30 				      unsigned long expires)
31 {
32 	unsigned long timeout;
33 
34 	if (!time_is_after_jiffies(expires))
35 		return NULL;
36 
37 	timeout = expires - jiffies;
38 	wait_event_timeout(dev->mcu.wait,
39 			   (!skb_queue_empty(&dev->mcu.res_q) ||
40 			    test_bit(MT76_MCU_RESET, &dev->phy.state)),
41 			   timeout);
42 	return skb_dequeue(&dev->mcu.res_q);
43 }
44 EXPORT_SYMBOL_GPL(mt76_mcu_get_response);
45 
46 void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb)
47 {
48 	skb_queue_tail(&dev->mcu.res_q, skb);
49 	wake_up(&dev->mcu.wait);
50 }
51 EXPORT_SYMBOL_GPL(mt76_mcu_rx_event);
52