1 // SPDX-License-Identifier: GPL-2.0-only
4 * UWB basic command support and radio reset
6 * Copyright (C) 2005-2006 Intel Corporation
13 * - Now we are serializing (using the uwb_dev->mutex) the command
14 * execution; it should be parallelized as much as possible some
17 #include <linux/kernel.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
21 #include <linux/export.h>
23 #include "uwb-internal.h"
26 * Command result codes (WUSB1.0[T8-69])
29 const char *__strerror[] = {
34 "beacon is too large",
36 "unsupported power level",
37 "time out (wa) or invalid ie data (whci)",
38 "beacon size exceeded",
43 "no more asie notification",
47 /** Return a string matching the given error code */
48 const char *uwb_rc_strerror(unsigned code)
52 if (code >= ARRAY_SIZE(__strerror))
53 return "unknown error";
54 return __strerror[code];
57 int uwb_rc_cmd_async(struct uwb_rc *rc, const char *cmd_name,
58 struct uwb_rccb *cmd, size_t cmd_size,
59 u8 expected_type, u16 expected_event,
60 uwb_rc_cmd_cb_f cb, void *arg)
62 struct device *dev = &rc->uwb_dev.dev;
63 struct uwb_rc_neh *neh;
67 uwb_dev_lock(&rc->uwb_dev); /* Protect against rc->priv being removed */
68 if (rc->priv == NULL) {
69 uwb_dev_unlock(&rc->uwb_dev);
74 needtofree = rc->filter_cmd(rc, &cmd, &cmd_size);
75 if (needtofree < 0 && needtofree != -ENOANO) {
76 dev_err(dev, "%s: filter error: %d\n",
77 cmd_name, needtofree);
78 uwb_dev_unlock(&rc->uwb_dev);
83 neh = uwb_rc_neh_add(rc, cmd, expected_type, expected_event, cb, arg);
85 result = PTR_ERR(neh);
86 uwb_dev_unlock(&rc->uwb_dev);
90 result = rc->cmd(rc, cmd, cmd_size);
91 uwb_dev_unlock(&rc->uwb_dev);
93 uwb_rc_neh_rm(rc, neh);
95 uwb_rc_neh_arm(rc, neh);
100 return result < 0 ? result : 0;
102 EXPORT_SYMBOL_GPL(uwb_rc_cmd_async);
104 struct uwb_rc_cmd_done_params {
105 struct completion completion;
106 struct uwb_rceb *reply;
110 static void uwb_rc_cmd_done(struct uwb_rc *rc, void *arg,
111 struct uwb_rceb *reply, ssize_t reply_size)
113 struct uwb_rc_cmd_done_params *p = (struct uwb_rc_cmd_done_params *)arg;
115 if (reply_size > 0) {
117 reply_size = min(p->reply_size, reply_size);
119 p->reply = kmalloc(reply_size, GFP_ATOMIC);
122 memcpy(p->reply, reply, reply_size);
124 reply_size = -ENOMEM;
126 p->reply_size = reply_size;
127 complete(&p->completion);
132 * Generic function for issuing commands to the Radio Control Interface
134 * @rc: UWB Radio Control descriptor
135 * @cmd_name: Name of the command being issued (for error messages)
136 * @cmd: Pointer to rccb structure containing the command;
137 * normally you embed this structure as the first member of
138 * the full command structure.
139 * @cmd_size: Size of the whole command buffer pointed to by @cmd.
140 * @reply: Pointer to where to store the reply
141 * @reply_size: @reply's size
142 * @expected_type: Expected type in the return event
143 * @expected_event: Expected event code in the return event
144 * @preply: Here a pointer to where the event data is received will
145 * be stored. Once done with the data, free with kfree().
147 * This function is generic; it works for commands that return a fixed
148 * and known size or for commands that return a variable amount of data.
150 * If a buffer is provided, that is used, although it could be chopped
151 * to the maximum size of the buffer. If the buffer is NULL, then one
152 * be allocated in *preply with the whole contents of the reply.
154 * @rc needs to be referenced
157 ssize_t __uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name,
158 struct uwb_rccb *cmd, size_t cmd_size,
159 struct uwb_rceb *reply, size_t reply_size,
160 u8 expected_type, u16 expected_event,
161 struct uwb_rceb **preply)
164 struct device *dev = &rc->uwb_dev.dev;
165 struct uwb_rc_cmd_done_params params;
167 init_completion(¶ms.completion);
168 params.reply = reply;
169 params.reply_size = reply_size;
171 result = uwb_rc_cmd_async(rc, cmd_name, cmd, cmd_size,
172 expected_type, expected_event,
173 uwb_rc_cmd_done, ¶ms);
177 wait_for_completion(¶ms.completion);
180 *preply = params.reply;
182 if (params.reply_size < 0)
183 dev_err(dev, "%s: confirmation event 0x%02x/%04x/%02x "
184 "reception failed: %d\n", cmd_name,
185 expected_type, expected_event, cmd->bCommandContext,
186 (int)params.reply_size);
187 return params.reply_size;
192 * Generic function for issuing commands to the Radio Control Interface
194 * @rc: UWB Radio Control descriptor
195 * @cmd_name: Name of the command being issued (for error messages)
196 * @cmd: Pointer to rccb structure containing the command;
197 * normally you embed this structure as the first member of
198 * the full command structure.
199 * @cmd_size: Size of the whole command buffer pointed to by @cmd.
200 * @reply: Pointer to the beginning of the confirmation event
201 * buffer. Normally bigger than an 'struct hwarc_rceb'.
202 * You need to fill out reply->bEventType and reply->wEvent (in
203 * cpu order) as the function will use them to verify the
204 * confirmation event.
205 * @reply_size: Size of the reply buffer
207 * The function checks that the length returned in the reply is at
208 * least as big as @reply_size; if not, it will be deemed an error and
211 * @rc needs to be referenced
213 ssize_t uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name,
214 struct uwb_rccb *cmd, size_t cmd_size,
215 struct uwb_rceb *reply, size_t reply_size)
217 struct device *dev = &rc->uwb_dev.dev;
220 result = __uwb_rc_cmd(rc, cmd_name,
221 cmd, cmd_size, reply, reply_size,
222 reply->bEventType, reply->wEvent, NULL);
224 if (result > 0 && result < reply_size) {
225 dev_err(dev, "%s: not enough data returned for decoding reply "
226 "(%zu bytes received vs at least %zu needed)\n",
227 cmd_name, result, reply_size);
232 EXPORT_SYMBOL_GPL(uwb_rc_cmd);
236 * Generic function for issuing commands to the Radio Control
237 * Interface that return an unknown amount of data
239 * @rc: UWB Radio Control descriptor
240 * @cmd_name: Name of the command being issued (for error messages)
241 * @cmd: Pointer to rccb structure containing the command;
242 * normally you embed this structure as the first member of
243 * the full command structure.
244 * @cmd_size: Size of the whole command buffer pointed to by @cmd.
245 * @expected_type: Expected type in the return event
246 * @expected_event: Expected event code in the return event
247 * @preply: Here a pointer to where the event data is received will
248 * be stored. Once done with the data, free with kfree().
250 * The function checks that the length returned in the reply is at
251 * least as big as a 'struct uwb_rceb *'; if not, it will be deemed an
252 * error and -EIO returned.
254 * @rc needs to be referenced
256 ssize_t uwb_rc_vcmd(struct uwb_rc *rc, const char *cmd_name,
257 struct uwb_rccb *cmd, size_t cmd_size,
258 u8 expected_type, u16 expected_event,
259 struct uwb_rceb **preply)
261 return __uwb_rc_cmd(rc, cmd_name, cmd, cmd_size, NULL, 0,
262 expected_type, expected_event, preply);
264 EXPORT_SYMBOL_GPL(uwb_rc_vcmd);
268 * Reset a UWB Host Controller (and all radio settings)
270 * @rc: Host Controller descriptor
271 * @returns: 0 if ok, < 0 errno code on error
273 * We put the command on kmalloc'ed memory as some arches cannot do
274 * USB from the stack. The reply event is copied from an stage buffer,
275 * so it can be in the stack. See WUSB1.0[8.6.2.4] for more details.
277 int uwb_rc_reset(struct uwb_rc *rc)
279 int result = -ENOMEM;
280 struct uwb_rc_evt_confirm reply;
281 struct uwb_rccb *cmd;
282 size_t cmd_size = sizeof(*cmd);
284 mutex_lock(&rc->uwb_dev.mutex);
285 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
288 cmd->bCommandType = UWB_RC_CET_GENERAL;
289 cmd->wCommand = cpu_to_le16(UWB_RC_CMD_RESET);
290 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
291 reply.rceb.wEvent = UWB_RC_CMD_RESET;
292 result = uwb_rc_cmd(rc, "RESET", cmd, cmd_size,
293 &reply.rceb, sizeof(reply));
296 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
297 dev_err(&rc->uwb_dev.dev,
298 "RESET: command execution failed: %s (%d)\n",
299 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
305 mutex_unlock(&rc->uwb_dev.mutex);
309 int uwbd_msg_handle_reset(struct uwb_event *evt)
311 struct uwb_rc *rc = evt->rc;
314 dev_info(&rc->uwb_dev.dev, "resetting radio controller\n");
317 dev_err(&rc->uwb_dev.dev, "failed to reset hardware: %d\n", ret);
322 /* Nothing can be done except try the reset again. Wait a bit
323 to avoid reset loops during probe() or remove(). */
325 uwb_rc_reset_all(rc);
330 * uwb_rc_reset_all - request a reset of the radio controller and PALs
331 * @rc: the radio controller of the hardware device to be reset.
333 * The full hardware reset of the radio controller and all the PALs
336 void uwb_rc_reset_all(struct uwb_rc *rc)
338 struct uwb_event *evt;
340 evt = kzalloc(sizeof(struct uwb_event), GFP_ATOMIC);
341 if (unlikely(evt == NULL))
344 evt->rc = __uwb_rc_get(rc); /* will be put by uwbd's uwbd_event_handle() */
345 evt->ts_jiffies = jiffies;
346 evt->type = UWB_EVT_TYPE_MSG;
347 evt->message = UWB_EVT_MSG_RESET;
349 uwbd_event_queue(evt);
351 EXPORT_SYMBOL_GPL(uwb_rc_reset_all);
353 void uwb_rc_pre_reset(struct uwb_rc *rc)
358 uwb_radio_reset_state(rc);
359 uwb_rsv_remove_all(rc);
361 EXPORT_SYMBOL_GPL(uwb_rc_pre_reset);
363 int uwb_rc_post_reset(struct uwb_rc *rc)
370 ret = uwb_rc_mac_addr_set(rc, &rc->uwb_dev.mac_addr);
373 ret = uwb_rc_dev_addr_set(rc, &rc->uwb_dev.dev_addr);
379 EXPORT_SYMBOL_GPL(uwb_rc_post_reset);