Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v5.2-rc5 379 lines 11 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Ultra Wide Band 4 * UWB basic command support and radio reset 5 * 6 * Copyright (C) 2005-2006 Intel Corporation 7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> 8 * 9 * FIXME: 10 * 11 * - docs 12 * 13 * - Now we are serializing (using the uwb_dev->mutex) the command 14 * execution; it should be parallelized as much as possible some 15 * day. 16 */ 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> 22 23#include "uwb-internal.h" 24 25/** 26 * Command result codes (WUSB1.0[T8-69]) 27 */ 28static 29const char *__strerror[] = { 30 "success", 31 "failure", 32 "hardware failure", 33 "no more slots", 34 "beacon is too large", 35 "invalid parameter", 36 "unsupported power level", 37 "time out (wa) or invalid ie data (whci)", 38 "beacon size exceeded", 39 "cancelled", 40 "invalid state", 41 "invalid size", 42 "ack not received", 43 "no more asie notification", 44}; 45 46 47/** Return a string matching the given error code */ 48const char *uwb_rc_strerror(unsigned code) 49{ 50 if (code == 255) 51 return "time out"; 52 if (code >= ARRAY_SIZE(__strerror)) 53 return "unknown error"; 54 return __strerror[code]; 55} 56 57int 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) 61{ 62 struct device *dev = &rc->uwb_dev.dev; 63 struct uwb_rc_neh *neh; 64 int needtofree = 0; 65 int result; 66 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); 70 return -ESHUTDOWN; 71 } 72 73 if (rc->filter_cmd) { 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); 79 return needtofree; 80 } 81 } 82 83 neh = uwb_rc_neh_add(rc, cmd, expected_type, expected_event, cb, arg); 84 if (IS_ERR(neh)) { 85 result = PTR_ERR(neh); 86 uwb_dev_unlock(&rc->uwb_dev); 87 goto out; 88 } 89 90 result = rc->cmd(rc, cmd, cmd_size); 91 uwb_dev_unlock(&rc->uwb_dev); 92 if (result < 0) 93 uwb_rc_neh_rm(rc, neh); 94 else 95 uwb_rc_neh_arm(rc, neh); 96 uwb_rc_neh_put(neh); 97out: 98 if (needtofree == 1) 99 kfree(cmd); 100 return result < 0 ? result : 0; 101} 102EXPORT_SYMBOL_GPL(uwb_rc_cmd_async); 103 104struct uwb_rc_cmd_done_params { 105 struct completion completion; 106 struct uwb_rceb *reply; 107 ssize_t reply_size; 108}; 109 110static void uwb_rc_cmd_done(struct uwb_rc *rc, void *arg, 111 struct uwb_rceb *reply, ssize_t reply_size) 112{ 113 struct uwb_rc_cmd_done_params *p = (struct uwb_rc_cmd_done_params *)arg; 114 115 if (reply_size > 0) { 116 if (p->reply) 117 reply_size = min(p->reply_size, reply_size); 118 else 119 p->reply = kmalloc(reply_size, GFP_ATOMIC); 120 121 if (p->reply) 122 memcpy(p->reply, reply, reply_size); 123 else 124 reply_size = -ENOMEM; 125 } 126 p->reply_size = reply_size; 127 complete(&p->completion); 128} 129 130 131/** 132 * Generic function for issuing commands to the Radio Control Interface 133 * 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(). 146 * 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. 149 * 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. 153 * 154 * @rc needs to be referenced 155 */ 156static 157ssize_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) 162{ 163 ssize_t result = 0; 164 struct device *dev = &rc->uwb_dev.dev; 165 struct uwb_rc_cmd_done_params params; 166 167 init_completion(&params.completion); 168 params.reply = reply; 169 params.reply_size = reply_size; 170 171 result = uwb_rc_cmd_async(rc, cmd_name, cmd, cmd_size, 172 expected_type, expected_event, 173 uwb_rc_cmd_done, &params); 174 if (result) 175 return result; 176 177 wait_for_completion(&params.completion); 178 179 if (preply) 180 *preply = params.reply; 181 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; 188} 189 190 191/** 192 * Generic function for issuing commands to the Radio Control Interface 193 * 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 206 * 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 209 * -EIO returned. 210 * 211 * @rc needs to be referenced 212 */ 213ssize_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) 216{ 217 struct device *dev = &rc->uwb_dev.dev; 218 ssize_t result; 219 220 result = __uwb_rc_cmd(rc, cmd_name, 221 cmd, cmd_size, reply, reply_size, 222 reply->bEventType, reply->wEvent, NULL); 223 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); 228 result = -EIO; 229 } 230 return result; 231} 232EXPORT_SYMBOL_GPL(uwb_rc_cmd); 233 234 235/** 236 * Generic function for issuing commands to the Radio Control 237 * Interface that return an unknown amount of data 238 * 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(). 249 * 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. 253 * 254 * @rc needs to be referenced 255 */ 256ssize_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) 260{ 261 return __uwb_rc_cmd(rc, cmd_name, cmd, cmd_size, NULL, 0, 262 expected_type, expected_event, preply); 263} 264EXPORT_SYMBOL_GPL(uwb_rc_vcmd); 265 266 267/** 268 * Reset a UWB Host Controller (and all radio settings) 269 * 270 * @rc: Host Controller descriptor 271 * @returns: 0 if ok, < 0 errno code on error 272 * 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. 276 */ 277int uwb_rc_reset(struct uwb_rc *rc) 278{ 279 int result = -ENOMEM; 280 struct uwb_rc_evt_confirm reply; 281 struct uwb_rccb *cmd; 282 size_t cmd_size = sizeof(*cmd); 283 284 mutex_lock(&rc->uwb_dev.mutex); 285 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 286 if (cmd == NULL) 287 goto error_kzalloc; 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)); 294 if (result < 0) 295 goto error_cmd; 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); 300 result = -EIO; 301 } 302error_cmd: 303 kfree(cmd); 304error_kzalloc: 305 mutex_unlock(&rc->uwb_dev.mutex); 306 return result; 307} 308 309int uwbd_msg_handle_reset(struct uwb_event *evt) 310{ 311 struct uwb_rc *rc = evt->rc; 312 int ret; 313 314 dev_info(&rc->uwb_dev.dev, "resetting radio controller\n"); 315 ret = rc->reset(rc); 316 if (ret < 0) { 317 dev_err(&rc->uwb_dev.dev, "failed to reset hardware: %d\n", ret); 318 goto error; 319 } 320 return 0; 321error: 322 /* Nothing can be done except try the reset again. Wait a bit 323 to avoid reset loops during probe() or remove(). */ 324 msleep(1000); 325 uwb_rc_reset_all(rc); 326 return ret; 327} 328 329/** 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. 332 * 333 * The full hardware reset of the radio controller and all the PALs 334 * will be scheduled. 335 */ 336void uwb_rc_reset_all(struct uwb_rc *rc) 337{ 338 struct uwb_event *evt; 339 340 evt = kzalloc(sizeof(struct uwb_event), GFP_ATOMIC); 341 if (unlikely(evt == NULL)) 342 return; 343 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; 348 349 uwbd_event_queue(evt); 350} 351EXPORT_SYMBOL_GPL(uwb_rc_reset_all); 352 353void uwb_rc_pre_reset(struct uwb_rc *rc) 354{ 355 rc->stop(rc); 356 uwbd_flush(rc); 357 358 uwb_radio_reset_state(rc); 359 uwb_rsv_remove_all(rc); 360} 361EXPORT_SYMBOL_GPL(uwb_rc_pre_reset); 362 363int uwb_rc_post_reset(struct uwb_rc *rc) 364{ 365 int ret; 366 367 ret = rc->start(rc); 368 if (ret) 369 goto out; 370 ret = uwb_rc_mac_addr_set(rc, &rc->uwb_dev.mac_addr); 371 if (ret) 372 goto out; 373 ret = uwb_rc_dev_addr_set(rc, &rc->uwb_dev.dev_addr); 374 if (ret) 375 goto out; 376out: 377 return ret; 378} 379EXPORT_SYMBOL_GPL(uwb_rc_post_reset);