drm: bridge: adv7511: remove s32 format from i2s capabilities
[drm/drm-misc.git] / drivers / net / wireless / mediatek / mt76 / mcu.c
blob3353012e8542984a081c863707cccb915d4a1504
1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (C) 2019 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
4 */
6 #include "mt76.h"
8 struct sk_buff *
9 __mt76_mcu_msg_alloc(struct mt76_dev *dev, const void *data,
10 int len, int data_len, gfp_t gfp)
12 const struct mt76_mcu_ops *ops = dev->mcu_ops;
13 struct sk_buff *skb;
15 len = max_t(int, len, data_len);
16 len = ops->headroom + len + ops->tailroom;
18 skb = alloc_skb(len, gfp);
19 if (!skb)
20 return NULL;
22 memset(skb->head, 0, len);
23 skb_reserve(skb, ops->headroom);
25 if (data && data_len)
26 skb_put_data(skb, data, data_len);
28 return skb;
30 EXPORT_SYMBOL_GPL(__mt76_mcu_msg_alloc);
32 struct sk_buff *mt76_mcu_get_response(struct mt76_dev *dev,
33 unsigned long expires)
35 unsigned long timeout;
37 if (!time_is_after_jiffies(expires))
38 return NULL;
40 timeout = expires - jiffies;
41 wait_event_timeout(dev->mcu.wait,
42 (!skb_queue_empty(&dev->mcu.res_q) ||
43 test_bit(MT76_MCU_RESET, &dev->phy.state)),
44 timeout);
45 return skb_dequeue(&dev->mcu.res_q);
47 EXPORT_SYMBOL_GPL(mt76_mcu_get_response);
49 void mt76_mcu_rx_event(struct mt76_dev *dev, struct sk_buff *skb)
51 skb_queue_tail(&dev->mcu.res_q, skb);
52 wake_up(&dev->mcu.wait);
54 EXPORT_SYMBOL_GPL(mt76_mcu_rx_event);
56 int mt76_mcu_send_and_get_msg(struct mt76_dev *dev, int cmd, const void *data,
57 int len, bool wait_resp, struct sk_buff **ret_skb)
59 struct sk_buff *skb;
61 if (dev->mcu_ops->mcu_send_msg)
62 return dev->mcu_ops->mcu_send_msg(dev, cmd, data, len, wait_resp);
64 skb = mt76_mcu_msg_alloc(dev, data, len);
65 if (!skb)
66 return -ENOMEM;
68 return mt76_mcu_skb_send_and_get_msg(dev, skb, cmd, wait_resp, ret_skb);
70 EXPORT_SYMBOL_GPL(mt76_mcu_send_and_get_msg);
72 int mt76_mcu_skb_send_and_get_msg(struct mt76_dev *dev, struct sk_buff *skb,
73 int cmd, bool wait_resp,
74 struct sk_buff **ret_skb)
76 unsigned int retry = 0;
77 struct sk_buff *orig_skb = NULL;
78 unsigned long expires;
79 int ret, seq;
81 if (ret_skb)
82 *ret_skb = NULL;
84 mutex_lock(&dev->mcu.mutex);
86 if (dev->mcu_ops->mcu_skb_prepare_msg) {
87 orig_skb = skb;
88 ret = dev->mcu_ops->mcu_skb_prepare_msg(dev, skb, cmd, &seq);
89 if (ret < 0)
90 goto out;
93 retry:
94 /* orig skb might be needed for retry, mcu_skb_send_msg consumes it */
95 if (orig_skb)
96 skb_get(orig_skb);
97 ret = dev->mcu_ops->mcu_skb_send_msg(dev, skb, cmd, &seq);
98 if (ret < 0)
99 goto out;
101 if (!wait_resp) {
102 ret = 0;
103 goto out;
106 expires = jiffies + dev->mcu.timeout;
108 do {
109 skb = mt76_mcu_get_response(dev, expires);
110 if (!skb && !test_bit(MT76_MCU_RESET, &dev->phy.state) &&
111 orig_skb && retry++ < dev->mcu_ops->max_retry) {
112 dev_err(dev->dev, "Retry message %08x (seq %d)\n",
113 cmd, seq);
114 skb = orig_skb;
115 goto retry;
118 ret = dev->mcu_ops->mcu_parse_response(dev, cmd, skb, seq);
119 if (!ret && ret_skb)
120 *ret_skb = skb;
121 else
122 dev_kfree_skb(skb);
123 } while (ret == -EAGAIN);
126 out:
127 dev_kfree_skb(orig_skb);
128 mutex_unlock(&dev->mcu.mutex);
130 return ret;
132 EXPORT_SYMBOL_GPL(mt76_mcu_skb_send_and_get_msg);
134 int __mt76_mcu_send_firmware(struct mt76_dev *dev, int cmd, const void *data,
135 int len, int max_len)
137 int err, cur_len;
139 while (len > 0) {
140 cur_len = min_t(int, max_len, len);
142 err = mt76_mcu_send_msg(dev, cmd, data, cur_len, false);
143 if (err)
144 return err;
146 data += cur_len;
147 len -= cur_len;
149 if (dev->queue_ops->tx_cleanup)
150 dev->queue_ops->tx_cleanup(dev,
151 dev->q_mcu[MT_MCUQ_FWDL],
152 false);
155 return 0;
157 EXPORT_SYMBOL_GPL(__mt76_mcu_send_firmware);