This source file includes following definitions.
- uwb_rc_strerror
- uwb_rc_cmd_async
- uwb_rc_cmd_done
- __uwb_rc_cmd
- uwb_rc_cmd
- uwb_rc_vcmd
- uwb_rc_reset
- uwbd_msg_handle_reset
- uwb_rc_reset_all
- uwb_rc_pre_reset
- uwb_rc_post_reset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28 static
29 const 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
48 const 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
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)
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);
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);
97 out:
98 if (needtofree == 1)
99 kfree(cmd);
100 return result < 0 ? result : 0;
101 }
102 EXPORT_SYMBOL_GPL(uwb_rc_cmd_async);
103
104 struct uwb_rc_cmd_done_params {
105 struct completion completion;
106 struct uwb_rceb *reply;
107 ssize_t reply_size;
108 };
109
110 static 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 static
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)
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(¶ms.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, ¶ms);
174 if (result)
175 return result;
176
177 wait_for_completion(¶ms.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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)
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 }
232 EXPORT_SYMBOL_GPL(uwb_rc_cmd);
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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)
260 {
261 return __uwb_rc_cmd(rc, cmd_name, cmd, cmd_size, NULL, 0,
262 expected_type, expected_event, preply);
263 }
264 EXPORT_SYMBOL_GPL(uwb_rc_vcmd);
265
266
267
268
269
270
271
272
273
274
275
276
277 int 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 }
302 error_cmd:
303 kfree(cmd);
304 error_kzalloc:
305 mutex_unlock(&rc->uwb_dev.mutex);
306 return result;
307 }
308
309 int 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;
321 error:
322
323
324 msleep(1000);
325 uwb_rc_reset_all(rc);
326 return ret;
327 }
328
329
330
331
332
333
334
335
336 void 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);
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 }
351 EXPORT_SYMBOL_GPL(uwb_rc_reset_all);
352
353 void 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 }
361 EXPORT_SYMBOL_GPL(uwb_rc_pre_reset);
362
363 int 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;
376 out:
377 return ret;
378 }
379 EXPORT_SYMBOL_GPL(uwb_rc_post_reset);