blob: 8b47c9cdd64239d3e4442e9db812825d93da4cf7 [file] [log] [blame]
Inaky Perez-Gonzalez0612edf2008-09-17 16:34:07 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
David Vrabel0396c212009-08-25 16:41:06 +010034#include <linux/delay.h>
Paul Gortmaker475c0a62011-07-10 13:18:02 -040035#include <linux/export.h>
Inaky Perez-Gonzalez0612edf2008-09-17 16:34:07 +010036
37#include "uwb-internal.h"
Inaky Perez-Gonzalez0612edf2008-09-17 16:34:07 +010038
39/**
40 * Command result codes (WUSB1.0[T8-69])
41 */
42static
43const char *__strerror[] = {
44 "success",
45 "failure",
46 "hardware failure",
47 "no more slots",
48 "beacon is too large",
49 "invalid parameter",
50 "unsupported power level",
51 "time out (wa) or invalid ie data (whci)",
52 "beacon size exceeded",
53 "cancelled",
54 "invalid state",
55 "invalid size",
Lucas De Marchi25985ed2011-03-30 22:57:33 -030056 "ack not received",
Inaky Perez-Gonzalez0612edf2008-09-17 16:34:07 +010057 "no more asie notification",
58};
59
60
61/** Return a string matching the given error code */
62const char *uwb_rc_strerror(unsigned code)
63{
64 if (code == 255)
65 return "time out";
66 if (code >= ARRAY_SIZE(__strerror))
67 return "unknown error";
68 return __strerror[code];
69}
70
71int uwb_rc_cmd_async(struct uwb_rc *rc, const char *cmd_name,
72 struct uwb_rccb *cmd, size_t cmd_size,
73 u8 expected_type, u16 expected_event,
74 uwb_rc_cmd_cb_f cb, void *arg)
75{
76 struct device *dev = &rc->uwb_dev.dev;
77 struct uwb_rc_neh *neh;
78 int needtofree = 0;
79 int result;
80
81 uwb_dev_lock(&rc->uwb_dev); /* Protect against rc->priv being removed */
82 if (rc->priv == NULL) {
83 uwb_dev_unlock(&rc->uwb_dev);
84 return -ESHUTDOWN;
85 }
86
87 if (rc->filter_cmd) {
88 needtofree = rc->filter_cmd(rc, &cmd, &cmd_size);
89 if (needtofree < 0 && needtofree != -ENOANO) {
90 dev_err(dev, "%s: filter error: %d\n",
91 cmd_name, needtofree);
92 uwb_dev_unlock(&rc->uwb_dev);
93 return needtofree;
94 }
95 }
96
97 neh = uwb_rc_neh_add(rc, cmd, expected_type, expected_event, cb, arg);
98 if (IS_ERR(neh)) {
99 result = PTR_ERR(neh);
Alexey Khoroshilovffa5c412012-11-27 00:26:46 +0400100 uwb_dev_unlock(&rc->uwb_dev);
Inaky Perez-Gonzalez0612edf2008-09-17 16:34:07 +0100101 goto out;
102 }
103
104 result = rc->cmd(rc, cmd, cmd_size);
105 uwb_dev_unlock(&rc->uwb_dev);
106 if (result < 0)
107 uwb_rc_neh_rm(rc, neh);
108 else
109 uwb_rc_neh_arm(rc, neh);
110 uwb_rc_neh_put(neh);
111out:
112 if (needtofree == 1)
113 kfree(cmd);
114 return result < 0 ? result : 0;
115}
116EXPORT_SYMBOL_GPL(uwb_rc_cmd_async);
117
118struct uwb_rc_cmd_done_params {
119 struct completion completion;
120 struct uwb_rceb *reply;
121 ssize_t reply_size;
122};
123
124static void uwb_rc_cmd_done(struct uwb_rc *rc, void *arg,
125 struct uwb_rceb *reply, ssize_t reply_size)
126{
127 struct uwb_rc_cmd_done_params *p = (struct uwb_rc_cmd_done_params *)arg;
128
129 if (reply_size > 0) {
130 if (p->reply)
131 reply_size = min(p->reply_size, reply_size);
132 else
133 p->reply = kmalloc(reply_size, GFP_ATOMIC);
134
135 if (p->reply)
136 memcpy(p->reply, reply, reply_size);
137 else
138 reply_size = -ENOMEM;
139 }
140 p->reply_size = reply_size;
141 complete(&p->completion);
142}
143
144
145/**
146 * Generic function for issuing commands to the Radio Control Interface
147 *
148 * @rc: UWB Radio Control descriptor
149 * @cmd_name: Name of the command being issued (for error messages)
150 * @cmd: Pointer to rccb structure containing the command;
151 * normally you embed this structure as the first member of
152 * the full command structure.
153 * @cmd_size: Size of the whole command buffer pointed to by @cmd.
154 * @reply: Pointer to where to store the reply
155 * @reply_size: @reply's size
156 * @expected_type: Expected type in the return event
157 * @expected_event: Expected event code in the return event
158 * @preply: Here a pointer to where the event data is received will
159 * be stored. Once done with the data, free with kfree().
160 *
161 * This function is generic; it works for commands that return a fixed
162 * and known size or for commands that return a variable amount of data.
163 *
164 * If a buffer is provided, that is used, although it could be chopped
165 * to the maximum size of the buffer. If the buffer is NULL, then one
166 * be allocated in *preply with the whole contents of the reply.
167 *
168 * @rc needs to be referenced
169 */
170static
171ssize_t __uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name,
172 struct uwb_rccb *cmd, size_t cmd_size,
173 struct uwb_rceb *reply, size_t reply_size,
174 u8 expected_type, u16 expected_event,
175 struct uwb_rceb **preply)
176{
177 ssize_t result = 0;
178 struct device *dev = &rc->uwb_dev.dev;
179 struct uwb_rc_cmd_done_params params;
180
181 init_completion(&params.completion);
182 params.reply = reply;
183 params.reply_size = reply_size;
184
185 result = uwb_rc_cmd_async(rc, cmd_name, cmd, cmd_size,
186 expected_type, expected_event,
187 uwb_rc_cmd_done, &params);
188 if (result)
189 return result;
190
191 wait_for_completion(&params.completion);
192
193 if (preply)
194 *preply = params.reply;
195
196 if (params.reply_size < 0)
197 dev_err(dev, "%s: confirmation event 0x%02x/%04x/%02x "
198 "reception failed: %d\n", cmd_name,
199 expected_type, expected_event, cmd->bCommandContext,
200 (int)params.reply_size);
201 return params.reply_size;
202}
203
204
205/**
206 * Generic function for issuing commands to the Radio Control Interface
207 *
208 * @rc: UWB Radio Control descriptor
209 * @cmd_name: Name of the command being issued (for error messages)
210 * @cmd: Pointer to rccb structure containing the command;
211 * normally you embed this structure as the first member of
212 * the full command structure.
213 * @cmd_size: Size of the whole command buffer pointed to by @cmd.
214 * @reply: Pointer to the beginning of the confirmation event
215 * buffer. Normally bigger than an 'struct hwarc_rceb'.
216 * You need to fill out reply->bEventType and reply->wEvent (in
217 * cpu order) as the function will use them to verify the
218 * confirmation event.
219 * @reply_size: Size of the reply buffer
220 *
221 * The function checks that the length returned in the reply is at
222 * least as big as @reply_size; if not, it will be deemed an error and
223 * -EIO returned.
224 *
225 * @rc needs to be referenced
226 */
227ssize_t uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name,
228 struct uwb_rccb *cmd, size_t cmd_size,
229 struct uwb_rceb *reply, size_t reply_size)
230{
231 struct device *dev = &rc->uwb_dev.dev;
232 ssize_t result;
233
234 result = __uwb_rc_cmd(rc, cmd_name,
235 cmd, cmd_size, reply, reply_size,
236 reply->bEventType, reply->wEvent, NULL);
237
238 if (result > 0 && result < reply_size) {
239 dev_err(dev, "%s: not enough data returned for decoding reply "
240 "(%zu bytes received vs at least %zu needed)\n",
241 cmd_name, result, reply_size);
242 result = -EIO;
243 }
244 return result;
245}
246EXPORT_SYMBOL_GPL(uwb_rc_cmd);
247
248
249/**
250 * Generic function for issuing commands to the Radio Control
251 * Interface that return an unknown amount of data
252 *
253 * @rc: UWB Radio Control descriptor
254 * @cmd_name: Name of the command being issued (for error messages)
255 * @cmd: Pointer to rccb structure containing the command;
256 * normally you embed this structure as the first member of
257 * the full command structure.
258 * @cmd_size: Size of the whole command buffer pointed to by @cmd.
259 * @expected_type: Expected type in the return event
260 * @expected_event: Expected event code in the return event
261 * @preply: Here a pointer to where the event data is received will
262 * be stored. Once done with the data, free with kfree().
263 *
264 * The function checks that the length returned in the reply is at
265 * least as big as a 'struct uwb_rceb *'; if not, it will be deemed an
266 * error and -EIO returned.
267 *
268 * @rc needs to be referenced
269 */
270ssize_t uwb_rc_vcmd(struct uwb_rc *rc, const char *cmd_name,
271 struct uwb_rccb *cmd, size_t cmd_size,
272 u8 expected_type, u16 expected_event,
273 struct uwb_rceb **preply)
274{
275 return __uwb_rc_cmd(rc, cmd_name, cmd, cmd_size, NULL, 0,
276 expected_type, expected_event, preply);
277}
278EXPORT_SYMBOL_GPL(uwb_rc_vcmd);
279
280
281/**
282 * Reset a UWB Host Controller (and all radio settings)
283 *
284 * @rc: Host Controller descriptor
285 * @returns: 0 if ok, < 0 errno code on error
286 *
287 * We put the command on kmalloc'ed memory as some arches cannot do
288 * USB from the stack. The reply event is copied from an stage buffer,
289 * so it can be in the stack. See WUSB1.0[8.6.2.4] for more details.
290 */
291int uwb_rc_reset(struct uwb_rc *rc)
292{
293 int result = -ENOMEM;
294 struct uwb_rc_evt_confirm reply;
295 struct uwb_rccb *cmd;
296 size_t cmd_size = sizeof(*cmd);
297
298 mutex_lock(&rc->uwb_dev.mutex);
299 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
300 if (cmd == NULL)
301 goto error_kzalloc;
302 cmd->bCommandType = UWB_RC_CET_GENERAL;
303 cmd->wCommand = cpu_to_le16(UWB_RC_CMD_RESET);
304 reply.rceb.bEventType = UWB_RC_CET_GENERAL;
305 reply.rceb.wEvent = UWB_RC_CMD_RESET;
306 result = uwb_rc_cmd(rc, "RESET", cmd, cmd_size,
307 &reply.rceb, sizeof(reply));
308 if (result < 0)
309 goto error_cmd;
310 if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
311 dev_err(&rc->uwb_dev.dev,
312 "RESET: command execution failed: %s (%d)\n",
313 uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
314 result = -EIO;
315 }
316error_cmd:
317 kfree(cmd);
318error_kzalloc:
319 mutex_unlock(&rc->uwb_dev.mutex);
320 return result;
321}
322
323int uwbd_msg_handle_reset(struct uwb_event *evt)
324{
325 struct uwb_rc *rc = evt->rc;
326 int ret;
327
Inaky Perez-Gonzalez0612edf2008-09-17 16:34:07 +0100328 dev_info(&rc->uwb_dev.dev, "resetting radio controller\n");
329 ret = rc->reset(rc);
David Vrabel0396c212009-08-25 16:41:06 +0100330 if (ret < 0) {
Inaky Perez-Gonzalez0612edf2008-09-17 16:34:07 +0100331 dev_err(&rc->uwb_dev.dev, "failed to reset hardware: %d\n", ret);
David Vrabel307ba6d2008-11-07 17:37:33 +0000332 goto error;
333 }
334 return 0;
335error:
David Vrabel0396c212009-08-25 16:41:06 +0100336 /* Nothing can be done except try the reset again. Wait a bit
337 to avoid reset loops during probe() or remove(). */
338 msleep(1000);
David Vrabel307ba6d2008-11-07 17:37:33 +0000339 uwb_rc_reset_all(rc);
Inaky Perez-Gonzalez0612edf2008-09-17 16:34:07 +0100340 return ret;
341}
342
343/**
344 * uwb_rc_reset_all - request a reset of the radio controller and PALs
345 * @rc: the radio controller of the hardware device to be reset.
346 *
347 * The full hardware reset of the radio controller and all the PALs
348 * will be scheduled.
349 */
350void uwb_rc_reset_all(struct uwb_rc *rc)
351{
352 struct uwb_event *evt;
353
354 evt = kzalloc(sizeof(struct uwb_event), GFP_ATOMIC);
355 if (unlikely(evt == NULL))
356 return;
357
358 evt->rc = __uwb_rc_get(rc); /* will be put by uwbd's uwbd_event_handle() */
359 evt->ts_jiffies = jiffies;
360 evt->type = UWB_EVT_TYPE_MSG;
361 evt->message = UWB_EVT_MSG_RESET;
362
363 uwbd_event_queue(evt);
364}
365EXPORT_SYMBOL_GPL(uwb_rc_reset_all);
David Vrabel307ba6d2008-11-07 17:37:33 +0000366
367void uwb_rc_pre_reset(struct uwb_rc *rc)
368{
369 rc->stop(rc);
370 uwbd_flush(rc);
371
David Vrabel6fae35f2008-11-17 15:53:42 +0000372 uwb_radio_reset_state(rc);
David Vrabel307ba6d2008-11-07 17:37:33 +0000373 uwb_rsv_remove_all(rc);
374}
375EXPORT_SYMBOL_GPL(uwb_rc_pre_reset);
376
David Vrabel0396c212009-08-25 16:41:06 +0100377int uwb_rc_post_reset(struct uwb_rc *rc)
David Vrabel307ba6d2008-11-07 17:37:33 +0000378{
379 int ret;
380
381 ret = rc->start(rc);
382 if (ret)
David Vrabel0396c212009-08-25 16:41:06 +0100383 goto out;
David Vrabel307ba6d2008-11-07 17:37:33 +0000384 ret = uwb_rc_mac_addr_set(rc, &rc->uwb_dev.mac_addr);
385 if (ret)
David Vrabel0396c212009-08-25 16:41:06 +0100386 goto out;
David Vrabel307ba6d2008-11-07 17:37:33 +0000387 ret = uwb_rc_dev_addr_set(rc, &rc->uwb_dev.dev_addr);
388 if (ret)
David Vrabel0396c212009-08-25 16:41:06 +0100389 goto out;
390out:
391 return ret;
David Vrabel307ba6d2008-11-07 17:37:33 +0000392}
393EXPORT_SYMBOL_GPL(uwb_rc_post_reset);