Inaky Perez-Gonzalez | 0612edf | 2008-09-17 16:34:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Ultra Wide Band |
| 3 | * UWB basic command support and radio reset |
| 4 | * |
| 5 | * Copyright (C) 2005-2006 Intel Corporation |
| 6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License version |
| 10 | * 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 | * 02110-1301, USA. |
| 21 | * |
| 22 | * |
| 23 | * FIXME: |
| 24 | * |
| 25 | * - docs |
| 26 | * |
| 27 | * - Now we are serializing (using the uwb_dev->mutex) the command |
| 28 | * execution; it should be parallelized as much as possible some |
| 29 | * day. |
| 30 | */ |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/err.h> |
David Vrabel | 0396c21 | 2009-08-25 16:41:06 +0100 | [diff] [blame] | 33 | #include <linux/delay.h> |
Inaky Perez-Gonzalez | 0612edf | 2008-09-17 16:34:07 +0100 | [diff] [blame] | 34 | |
| 35 | #include "uwb-internal.h" |
Inaky Perez-Gonzalez | 0612edf | 2008-09-17 16:34:07 +0100 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Command result codes (WUSB1.0[T8-69]) |
| 39 | */ |
| 40 | static |
| 41 | const char *__strerror[] = { |
| 42 | "success", |
| 43 | "failure", |
| 44 | "hardware failure", |
| 45 | "no more slots", |
| 46 | "beacon is too large", |
| 47 | "invalid parameter", |
| 48 | "unsupported power level", |
| 49 | "time out (wa) or invalid ie data (whci)", |
| 50 | "beacon size exceeded", |
| 51 | "cancelled", |
| 52 | "invalid state", |
| 53 | "invalid size", |
| 54 | "ack not recieved", |
| 55 | "no more asie notification", |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | /** Return a string matching the given error code */ |
| 60 | const char *uwb_rc_strerror(unsigned code) |
| 61 | { |
| 62 | if (code == 255) |
| 63 | return "time out"; |
| 64 | if (code >= ARRAY_SIZE(__strerror)) |
| 65 | return "unknown error"; |
| 66 | return __strerror[code]; |
| 67 | } |
| 68 | |
| 69 | int uwb_rc_cmd_async(struct uwb_rc *rc, const char *cmd_name, |
| 70 | struct uwb_rccb *cmd, size_t cmd_size, |
| 71 | u8 expected_type, u16 expected_event, |
| 72 | uwb_rc_cmd_cb_f cb, void *arg) |
| 73 | { |
| 74 | struct device *dev = &rc->uwb_dev.dev; |
| 75 | struct uwb_rc_neh *neh; |
| 76 | int needtofree = 0; |
| 77 | int result; |
| 78 | |
| 79 | uwb_dev_lock(&rc->uwb_dev); /* Protect against rc->priv being removed */ |
| 80 | if (rc->priv == NULL) { |
| 81 | uwb_dev_unlock(&rc->uwb_dev); |
| 82 | return -ESHUTDOWN; |
| 83 | } |
| 84 | |
| 85 | if (rc->filter_cmd) { |
| 86 | needtofree = rc->filter_cmd(rc, &cmd, &cmd_size); |
| 87 | if (needtofree < 0 && needtofree != -ENOANO) { |
| 88 | dev_err(dev, "%s: filter error: %d\n", |
| 89 | cmd_name, needtofree); |
| 90 | uwb_dev_unlock(&rc->uwb_dev); |
| 91 | return needtofree; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | neh = uwb_rc_neh_add(rc, cmd, expected_type, expected_event, cb, arg); |
| 96 | if (IS_ERR(neh)) { |
| 97 | result = PTR_ERR(neh); |
| 98 | goto out; |
| 99 | } |
| 100 | |
| 101 | result = rc->cmd(rc, cmd, cmd_size); |
| 102 | uwb_dev_unlock(&rc->uwb_dev); |
| 103 | if (result < 0) |
| 104 | uwb_rc_neh_rm(rc, neh); |
| 105 | else |
| 106 | uwb_rc_neh_arm(rc, neh); |
| 107 | uwb_rc_neh_put(neh); |
| 108 | out: |
| 109 | if (needtofree == 1) |
| 110 | kfree(cmd); |
| 111 | return result < 0 ? result : 0; |
| 112 | } |
| 113 | EXPORT_SYMBOL_GPL(uwb_rc_cmd_async); |
| 114 | |
| 115 | struct uwb_rc_cmd_done_params { |
| 116 | struct completion completion; |
| 117 | struct uwb_rceb *reply; |
| 118 | ssize_t reply_size; |
| 119 | }; |
| 120 | |
| 121 | static void uwb_rc_cmd_done(struct uwb_rc *rc, void *arg, |
| 122 | struct uwb_rceb *reply, ssize_t reply_size) |
| 123 | { |
| 124 | struct uwb_rc_cmd_done_params *p = (struct uwb_rc_cmd_done_params *)arg; |
| 125 | |
| 126 | if (reply_size > 0) { |
| 127 | if (p->reply) |
| 128 | reply_size = min(p->reply_size, reply_size); |
| 129 | else |
| 130 | p->reply = kmalloc(reply_size, GFP_ATOMIC); |
| 131 | |
| 132 | if (p->reply) |
| 133 | memcpy(p->reply, reply, reply_size); |
| 134 | else |
| 135 | reply_size = -ENOMEM; |
| 136 | } |
| 137 | p->reply_size = reply_size; |
| 138 | complete(&p->completion); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * Generic function for issuing commands to the Radio Control Interface |
| 144 | * |
| 145 | * @rc: UWB Radio Control descriptor |
| 146 | * @cmd_name: Name of the command being issued (for error messages) |
| 147 | * @cmd: Pointer to rccb structure containing the command; |
| 148 | * normally you embed this structure as the first member of |
| 149 | * the full command structure. |
| 150 | * @cmd_size: Size of the whole command buffer pointed to by @cmd. |
| 151 | * @reply: Pointer to where to store the reply |
| 152 | * @reply_size: @reply's size |
| 153 | * @expected_type: Expected type in the return event |
| 154 | * @expected_event: Expected event code in the return event |
| 155 | * @preply: Here a pointer to where the event data is received will |
| 156 | * be stored. Once done with the data, free with kfree(). |
| 157 | * |
| 158 | * This function is generic; it works for commands that return a fixed |
| 159 | * and known size or for commands that return a variable amount of data. |
| 160 | * |
| 161 | * If a buffer is provided, that is used, although it could be chopped |
| 162 | * to the maximum size of the buffer. If the buffer is NULL, then one |
| 163 | * be allocated in *preply with the whole contents of the reply. |
| 164 | * |
| 165 | * @rc needs to be referenced |
| 166 | */ |
| 167 | static |
| 168 | ssize_t __uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name, |
| 169 | struct uwb_rccb *cmd, size_t cmd_size, |
| 170 | struct uwb_rceb *reply, size_t reply_size, |
| 171 | u8 expected_type, u16 expected_event, |
| 172 | struct uwb_rceb **preply) |
| 173 | { |
| 174 | ssize_t result = 0; |
| 175 | struct device *dev = &rc->uwb_dev.dev; |
| 176 | struct uwb_rc_cmd_done_params params; |
| 177 | |
| 178 | init_completion(¶ms.completion); |
| 179 | params.reply = reply; |
| 180 | params.reply_size = reply_size; |
| 181 | |
| 182 | result = uwb_rc_cmd_async(rc, cmd_name, cmd, cmd_size, |
| 183 | expected_type, expected_event, |
| 184 | uwb_rc_cmd_done, ¶ms); |
| 185 | if (result) |
| 186 | return result; |
| 187 | |
| 188 | wait_for_completion(¶ms.completion); |
| 189 | |
| 190 | if (preply) |
| 191 | *preply = params.reply; |
| 192 | |
| 193 | if (params.reply_size < 0) |
| 194 | dev_err(dev, "%s: confirmation event 0x%02x/%04x/%02x " |
| 195 | "reception failed: %d\n", cmd_name, |
| 196 | expected_type, expected_event, cmd->bCommandContext, |
| 197 | (int)params.reply_size); |
| 198 | return params.reply_size; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /** |
| 203 | * Generic function for issuing commands to the Radio Control Interface |
| 204 | * |
| 205 | * @rc: UWB Radio Control descriptor |
| 206 | * @cmd_name: Name of the command being issued (for error messages) |
| 207 | * @cmd: Pointer to rccb structure containing the command; |
| 208 | * normally you embed this structure as the first member of |
| 209 | * the full command structure. |
| 210 | * @cmd_size: Size of the whole command buffer pointed to by @cmd. |
| 211 | * @reply: Pointer to the beginning of the confirmation event |
| 212 | * buffer. Normally bigger than an 'struct hwarc_rceb'. |
| 213 | * You need to fill out reply->bEventType and reply->wEvent (in |
| 214 | * cpu order) as the function will use them to verify the |
| 215 | * confirmation event. |
| 216 | * @reply_size: Size of the reply buffer |
| 217 | * |
| 218 | * The function checks that the length returned in the reply is at |
| 219 | * least as big as @reply_size; if not, it will be deemed an error and |
| 220 | * -EIO returned. |
| 221 | * |
| 222 | * @rc needs to be referenced |
| 223 | */ |
| 224 | ssize_t uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name, |
| 225 | struct uwb_rccb *cmd, size_t cmd_size, |
| 226 | struct uwb_rceb *reply, size_t reply_size) |
| 227 | { |
| 228 | struct device *dev = &rc->uwb_dev.dev; |
| 229 | ssize_t result; |
| 230 | |
| 231 | result = __uwb_rc_cmd(rc, cmd_name, |
| 232 | cmd, cmd_size, reply, reply_size, |
| 233 | reply->bEventType, reply->wEvent, NULL); |
| 234 | |
| 235 | if (result > 0 && result < reply_size) { |
| 236 | dev_err(dev, "%s: not enough data returned for decoding reply " |
| 237 | "(%zu bytes received vs at least %zu needed)\n", |
| 238 | cmd_name, result, reply_size); |
| 239 | result = -EIO; |
| 240 | } |
| 241 | return result; |
| 242 | } |
| 243 | EXPORT_SYMBOL_GPL(uwb_rc_cmd); |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * Generic function for issuing commands to the Radio Control |
| 248 | * Interface that return an unknown amount of data |
| 249 | * |
| 250 | * @rc: UWB Radio Control descriptor |
| 251 | * @cmd_name: Name of the command being issued (for error messages) |
| 252 | * @cmd: Pointer to rccb structure containing the command; |
| 253 | * normally you embed this structure as the first member of |
| 254 | * the full command structure. |
| 255 | * @cmd_size: Size of the whole command buffer pointed to by @cmd. |
| 256 | * @expected_type: Expected type in the return event |
| 257 | * @expected_event: Expected event code in the return event |
| 258 | * @preply: Here a pointer to where the event data is received will |
| 259 | * be stored. Once done with the data, free with kfree(). |
| 260 | * |
| 261 | * The function checks that the length returned in the reply is at |
| 262 | * least as big as a 'struct uwb_rceb *'; if not, it will be deemed an |
| 263 | * error and -EIO returned. |
| 264 | * |
| 265 | * @rc needs to be referenced |
| 266 | */ |
| 267 | ssize_t uwb_rc_vcmd(struct uwb_rc *rc, const char *cmd_name, |
| 268 | struct uwb_rccb *cmd, size_t cmd_size, |
| 269 | u8 expected_type, u16 expected_event, |
| 270 | struct uwb_rceb **preply) |
| 271 | { |
| 272 | return __uwb_rc_cmd(rc, cmd_name, cmd, cmd_size, NULL, 0, |
| 273 | expected_type, expected_event, preply); |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(uwb_rc_vcmd); |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * Reset a UWB Host Controller (and all radio settings) |
| 280 | * |
| 281 | * @rc: Host Controller descriptor |
| 282 | * @returns: 0 if ok, < 0 errno code on error |
| 283 | * |
| 284 | * We put the command on kmalloc'ed memory as some arches cannot do |
| 285 | * USB from the stack. The reply event is copied from an stage buffer, |
| 286 | * so it can be in the stack. See WUSB1.0[8.6.2.4] for more details. |
| 287 | */ |
| 288 | int uwb_rc_reset(struct uwb_rc *rc) |
| 289 | { |
| 290 | int result = -ENOMEM; |
| 291 | struct uwb_rc_evt_confirm reply; |
| 292 | struct uwb_rccb *cmd; |
| 293 | size_t cmd_size = sizeof(*cmd); |
| 294 | |
| 295 | mutex_lock(&rc->uwb_dev.mutex); |
| 296 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 297 | if (cmd == NULL) |
| 298 | goto error_kzalloc; |
| 299 | cmd->bCommandType = UWB_RC_CET_GENERAL; |
| 300 | cmd->wCommand = cpu_to_le16(UWB_RC_CMD_RESET); |
| 301 | reply.rceb.bEventType = UWB_RC_CET_GENERAL; |
| 302 | reply.rceb.wEvent = UWB_RC_CMD_RESET; |
| 303 | result = uwb_rc_cmd(rc, "RESET", cmd, cmd_size, |
| 304 | &reply.rceb, sizeof(reply)); |
| 305 | if (result < 0) |
| 306 | goto error_cmd; |
| 307 | if (reply.bResultCode != UWB_RC_RES_SUCCESS) { |
| 308 | dev_err(&rc->uwb_dev.dev, |
| 309 | "RESET: command execution failed: %s (%d)\n", |
| 310 | uwb_rc_strerror(reply.bResultCode), reply.bResultCode); |
| 311 | result = -EIO; |
| 312 | } |
| 313 | error_cmd: |
| 314 | kfree(cmd); |
| 315 | error_kzalloc: |
| 316 | mutex_unlock(&rc->uwb_dev.mutex); |
| 317 | return result; |
| 318 | } |
| 319 | |
| 320 | int uwbd_msg_handle_reset(struct uwb_event *evt) |
| 321 | { |
| 322 | struct uwb_rc *rc = evt->rc; |
| 323 | int ret; |
| 324 | |
Inaky Perez-Gonzalez | 0612edf | 2008-09-17 16:34:07 +0100 | [diff] [blame] | 325 | dev_info(&rc->uwb_dev.dev, "resetting radio controller\n"); |
| 326 | ret = rc->reset(rc); |
David Vrabel | 0396c21 | 2009-08-25 16:41:06 +0100 | [diff] [blame] | 327 | if (ret < 0) { |
Inaky Perez-Gonzalez | 0612edf | 2008-09-17 16:34:07 +0100 | [diff] [blame] | 328 | dev_err(&rc->uwb_dev.dev, "failed to reset hardware: %d\n", ret); |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 329 | goto error; |
| 330 | } |
| 331 | return 0; |
| 332 | error: |
David Vrabel | 0396c21 | 2009-08-25 16:41:06 +0100 | [diff] [blame] | 333 | /* Nothing can be done except try the reset again. Wait a bit |
| 334 | to avoid reset loops during probe() or remove(). */ |
| 335 | msleep(1000); |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 336 | uwb_rc_reset_all(rc); |
Inaky Perez-Gonzalez | 0612edf | 2008-09-17 16:34:07 +0100 | [diff] [blame] | 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * uwb_rc_reset_all - request a reset of the radio controller and PALs |
| 342 | * @rc: the radio controller of the hardware device to be reset. |
| 343 | * |
| 344 | * The full hardware reset of the radio controller and all the PALs |
| 345 | * will be scheduled. |
| 346 | */ |
| 347 | void uwb_rc_reset_all(struct uwb_rc *rc) |
| 348 | { |
| 349 | struct uwb_event *evt; |
| 350 | |
| 351 | evt = kzalloc(sizeof(struct uwb_event), GFP_ATOMIC); |
| 352 | if (unlikely(evt == NULL)) |
| 353 | return; |
| 354 | |
| 355 | evt->rc = __uwb_rc_get(rc); /* will be put by uwbd's uwbd_event_handle() */ |
| 356 | evt->ts_jiffies = jiffies; |
| 357 | evt->type = UWB_EVT_TYPE_MSG; |
| 358 | evt->message = UWB_EVT_MSG_RESET; |
| 359 | |
| 360 | uwbd_event_queue(evt); |
| 361 | } |
| 362 | EXPORT_SYMBOL_GPL(uwb_rc_reset_all); |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 363 | |
| 364 | void uwb_rc_pre_reset(struct uwb_rc *rc) |
| 365 | { |
| 366 | rc->stop(rc); |
| 367 | uwbd_flush(rc); |
| 368 | |
David Vrabel | 6fae35f | 2008-11-17 15:53:42 +0000 | [diff] [blame] | 369 | uwb_radio_reset_state(rc); |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 370 | uwb_rsv_remove_all(rc); |
| 371 | } |
| 372 | EXPORT_SYMBOL_GPL(uwb_rc_pre_reset); |
| 373 | |
David Vrabel | 0396c21 | 2009-08-25 16:41:06 +0100 | [diff] [blame] | 374 | int uwb_rc_post_reset(struct uwb_rc *rc) |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 375 | { |
| 376 | int ret; |
| 377 | |
| 378 | ret = rc->start(rc); |
| 379 | if (ret) |
David Vrabel | 0396c21 | 2009-08-25 16:41:06 +0100 | [diff] [blame] | 380 | goto out; |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 381 | ret = uwb_rc_mac_addr_set(rc, &rc->uwb_dev.mac_addr); |
| 382 | if (ret) |
David Vrabel | 0396c21 | 2009-08-25 16:41:06 +0100 | [diff] [blame] | 383 | goto out; |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 384 | ret = uwb_rc_dev_addr_set(rc, &rc->uwb_dev.dev_addr); |
| 385 | if (ret) |
David Vrabel | 0396c21 | 2009-08-25 16:41:06 +0100 | [diff] [blame] | 386 | goto out; |
| 387 | out: |
| 388 | return ret; |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 389 | } |
| 390 | EXPORT_SYMBOL_GPL(uwb_rc_post_reset); |