diff options
-rw-r--r-- | drivers/uwb/est.c | 485 | ||||
-rw-r--r-- | drivers/uwb/neh.c | 616 | ||||
-rw-r--r-- | drivers/uwb/reset.c | 362 |
3 files changed, 1463 insertions, 0 deletions
diff --git a/drivers/uwb/est.c b/drivers/uwb/est.c new file mode 100644 index 000000000000..1667afb704d3 --- /dev/null +++ b/drivers/uwb/est.c | |||
@@ -0,0 +1,485 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band Radio Control | ||
3 | * Event Size Tables management | ||
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: docs | ||
24 | * | ||
25 | * Infrastructure, code and data tables for guessing the size of | ||
26 | * events received on the notification endpoints of UWB radio | ||
27 | * controllers. | ||
28 | * | ||
29 | * You define a table of events and for each, its size and how to get | ||
30 | * the extra size. | ||
31 | * | ||
32 | * ENTRY POINTS: | ||
33 | * | ||
34 | * uwb_est_{init/destroy}(): To initialize/release the EST subsystem. | ||
35 | * | ||
36 | * uwb_est_[u]register(): To un/register event size tables | ||
37 | * uwb_est_grow() | ||
38 | * | ||
39 | * uwb_est_find_size(): Get the size of an event | ||
40 | * uwb_est_get_size() | ||
41 | */ | ||
42 | #include <linux/spinlock.h> | ||
43 | #define D_LOCAL 0 | ||
44 | #include <linux/uwb/debug.h> | ||
45 | #include "uwb-internal.h" | ||
46 | |||
47 | |||
48 | struct uwb_est { | ||
49 | u16 type_event_high; | ||
50 | u16 vendor, product; | ||
51 | u8 entries; | ||
52 | const struct uwb_est_entry *entry; | ||
53 | }; | ||
54 | |||
55 | |||
56 | static struct uwb_est *uwb_est; | ||
57 | static u8 uwb_est_size; | ||
58 | static u8 uwb_est_used; | ||
59 | static DEFINE_RWLOCK(uwb_est_lock); | ||
60 | |||
61 | /** | ||
62 | * WUSB Standard Event Size Table, HWA-RC interface | ||
63 | * | ||
64 | * Sizes for events and notifications type 0 (general), high nibble 0. | ||
65 | */ | ||
66 | static | ||
67 | struct uwb_est_entry uwb_est_00_00xx[] = { | ||
68 | [UWB_RC_EVT_IE_RCV] = { | ||
69 | .size = sizeof(struct uwb_rc_evt_ie_rcv), | ||
70 | .offset = 1 + offsetof(struct uwb_rc_evt_ie_rcv, wIELength), | ||
71 | }, | ||
72 | [UWB_RC_EVT_BEACON] = { | ||
73 | .size = sizeof(struct uwb_rc_evt_beacon), | ||
74 | .offset = 1 + offsetof(struct uwb_rc_evt_beacon, wBeaconInfoLength), | ||
75 | }, | ||
76 | [UWB_RC_EVT_BEACON_SIZE] = { | ||
77 | .size = sizeof(struct uwb_rc_evt_beacon_size), | ||
78 | }, | ||
79 | [UWB_RC_EVT_BPOIE_CHANGE] = { | ||
80 | .size = sizeof(struct uwb_rc_evt_bpoie_change), | ||
81 | .offset = 1 + offsetof(struct uwb_rc_evt_bpoie_change, | ||
82 | wBPOIELength), | ||
83 | }, | ||
84 | [UWB_RC_EVT_BP_SLOT_CHANGE] = { | ||
85 | .size = sizeof(struct uwb_rc_evt_bp_slot_change), | ||
86 | }, | ||
87 | [UWB_RC_EVT_BP_SWITCH_IE_RCV] = { | ||
88 | .size = sizeof(struct uwb_rc_evt_bp_switch_ie_rcv), | ||
89 | .offset = 1 + offsetof(struct uwb_rc_evt_bp_switch_ie_rcv, wIELength), | ||
90 | }, | ||
91 | [UWB_RC_EVT_DEV_ADDR_CONFLICT] = { | ||
92 | .size = sizeof(struct uwb_rc_evt_dev_addr_conflict), | ||
93 | }, | ||
94 | [UWB_RC_EVT_DRP_AVAIL] = { | ||
95 | .size = sizeof(struct uwb_rc_evt_drp_avail) | ||
96 | }, | ||
97 | [UWB_RC_EVT_DRP] = { | ||
98 | .size = sizeof(struct uwb_rc_evt_drp), | ||
99 | .offset = 1 + offsetof(struct uwb_rc_evt_drp, ie_length), | ||
100 | }, | ||
101 | [UWB_RC_EVT_BP_SWITCH_STATUS] = { | ||
102 | .size = sizeof(struct uwb_rc_evt_bp_switch_status), | ||
103 | }, | ||
104 | [UWB_RC_EVT_CMD_FRAME_RCV] = { | ||
105 | .size = sizeof(struct uwb_rc_evt_cmd_frame_rcv), | ||
106 | .offset = 1 + offsetof(struct uwb_rc_evt_cmd_frame_rcv, dataLength), | ||
107 | }, | ||
108 | [UWB_RC_EVT_CHANNEL_CHANGE_IE_RCV] = { | ||
109 | .size = sizeof(struct uwb_rc_evt_channel_change_ie_rcv), | ||
110 | .offset = 1 + offsetof(struct uwb_rc_evt_channel_change_ie_rcv, wIELength), | ||
111 | }, | ||
112 | [UWB_RC_CMD_CHANNEL_CHANGE] = { | ||
113 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
114 | }, | ||
115 | [UWB_RC_CMD_DEV_ADDR_MGMT] = { | ||
116 | .size = sizeof(struct uwb_rc_evt_dev_addr_mgmt) }, | ||
117 | [UWB_RC_CMD_GET_IE] = { | ||
118 | .size = sizeof(struct uwb_rc_evt_get_ie), | ||
119 | .offset = 1 + offsetof(struct uwb_rc_evt_get_ie, wIELength), | ||
120 | }, | ||
121 | [UWB_RC_CMD_RESET] = { | ||
122 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
123 | }, | ||
124 | [UWB_RC_CMD_SCAN] = { | ||
125 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
126 | }, | ||
127 | [UWB_RC_CMD_SET_BEACON_FILTER] = { | ||
128 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
129 | }, | ||
130 | [UWB_RC_CMD_SET_DRP_IE] = { | ||
131 | .size = sizeof(struct uwb_rc_evt_set_drp_ie), | ||
132 | }, | ||
133 | [UWB_RC_CMD_SET_IE] = { | ||
134 | .size = sizeof(struct uwb_rc_evt_set_ie), | ||
135 | }, | ||
136 | [UWB_RC_CMD_SET_NOTIFICATION_FILTER] = { | ||
137 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
138 | }, | ||
139 | [UWB_RC_CMD_SET_TX_POWER] = { | ||
140 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
141 | }, | ||
142 | [UWB_RC_CMD_SLEEP] = { | ||
143 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
144 | }, | ||
145 | [UWB_RC_CMD_START_BEACON] = { | ||
146 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
147 | }, | ||
148 | [UWB_RC_CMD_STOP_BEACON] = { | ||
149 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
150 | }, | ||
151 | [UWB_RC_CMD_BP_MERGE] = { | ||
152 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
153 | }, | ||
154 | [UWB_RC_CMD_SEND_COMMAND_FRAME] = { | ||
155 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
156 | }, | ||
157 | [UWB_RC_CMD_SET_ASIE_NOTIF] = { | ||
158 | .size = sizeof(struct uwb_rc_evt_confirm), | ||
159 | }, | ||
160 | }; | ||
161 | |||
162 | static | ||
163 | struct uwb_est_entry uwb_est_01_00xx[] = { | ||
164 | [UWB_RC_DAA_ENERGY_DETECTED] = { | ||
165 | .size = sizeof(struct uwb_rc_evt_daa_energy_detected), | ||
166 | }, | ||
167 | [UWB_RC_SET_DAA_ENERGY_MASK] = { | ||
168 | .size = sizeof(struct uwb_rc_evt_set_daa_energy_mask), | ||
169 | }, | ||
170 | [UWB_RC_SET_NOTIFICATION_FILTER_EX] = { | ||
171 | .size = sizeof(struct uwb_rc_evt_set_notification_filter_ex), | ||
172 | }, | ||
173 | }; | ||
174 | |||
175 | /** | ||
176 | * Initialize the EST subsystem | ||
177 | * | ||
178 | * Register the standard tables also. | ||
179 | * | ||
180 | * FIXME: tag init | ||
181 | */ | ||
182 | int uwb_est_create(void) | ||
183 | { | ||
184 | int result; | ||
185 | |||
186 | uwb_est_size = 2; | ||
187 | uwb_est_used = 0; | ||
188 | uwb_est = kzalloc(uwb_est_size * sizeof(uwb_est[0]), GFP_KERNEL); | ||
189 | if (uwb_est == NULL) | ||
190 | return -ENOMEM; | ||
191 | |||
192 | result = uwb_est_register(UWB_RC_CET_GENERAL, 0, 0xffff, 0xffff, | ||
193 | uwb_est_00_00xx, ARRAY_SIZE(uwb_est_00_00xx)); | ||
194 | if (result < 0) | ||
195 | goto out; | ||
196 | result = uwb_est_register(UWB_RC_CET_EX_TYPE_1, 0, 0xffff, 0xffff, | ||
197 | uwb_est_01_00xx, ARRAY_SIZE(uwb_est_01_00xx)); | ||
198 | out: | ||
199 | return result; | ||
200 | } | ||
201 | |||
202 | |||
203 | /** Clean it up */ | ||
204 | void uwb_est_destroy(void) | ||
205 | { | ||
206 | kfree(uwb_est); | ||
207 | uwb_est = NULL; | ||
208 | uwb_est_size = uwb_est_used = 0; | ||
209 | } | ||
210 | |||
211 | |||
212 | /** | ||
213 | * Double the capacity of the EST table | ||
214 | * | ||
215 | * @returns 0 if ok, < 0 errno no error. | ||
216 | */ | ||
217 | static | ||
218 | int uwb_est_grow(void) | ||
219 | { | ||
220 | size_t actual_size = uwb_est_size * sizeof(uwb_est[0]); | ||
221 | void *new = kmalloc(2 * actual_size, GFP_ATOMIC); | ||
222 | if (new == NULL) | ||
223 | return -ENOMEM; | ||
224 | memcpy(new, uwb_est, actual_size); | ||
225 | memset(new + actual_size, 0, actual_size); | ||
226 | kfree(uwb_est); | ||
227 | uwb_est = new; | ||
228 | uwb_est_size *= 2; | ||
229 | return 0; | ||
230 | } | ||
231 | |||
232 | |||
233 | /** | ||
234 | * Register an event size table | ||
235 | * | ||
236 | * Makes room for it if the table is full, and then inserts it in the | ||
237 | * right position (entries are sorted by type, event_high, vendor and | ||
238 | * then product). | ||
239 | * | ||
240 | * @vendor: vendor code for matching against the device (0x0000 and | ||
241 | * 0xffff mean any); use 0x0000 to force all to match without | ||
242 | * checking possible vendor specific ones, 0xfffff to match | ||
243 | * after checking vendor specific ones. | ||
244 | * | ||
245 | * @product: product code from that vendor; same matching rules, use | ||
246 | * 0x0000 for not allowing vendor specific matches, 0xffff | ||
247 | * for allowing. | ||
248 | * | ||
249 | * This arragement just makes the tables sort differenty. Because the | ||
250 | * table is sorted by growing type-event_high-vendor-product, a zero | ||
251 | * vendor will match before than a 0x456a vendor, that will match | ||
252 | * before a 0xfffff vendor. | ||
253 | * | ||
254 | * @returns 0 if ok, < 0 errno on error (-ENOENT if not found). | ||
255 | */ | ||
256 | /* FIXME: add bus type to vendor/product code */ | ||
257 | int uwb_est_register(u8 type, u8 event_high, u16 vendor, u16 product, | ||
258 | const struct uwb_est_entry *entry, size_t entries) | ||
259 | { | ||
260 | unsigned long flags; | ||
261 | unsigned itr; | ||
262 | u16 type_event_high; | ||
263 | int result = 0; | ||
264 | |||
265 | write_lock_irqsave(&uwb_est_lock, flags); | ||
266 | if (uwb_est_used == uwb_est_size) { | ||
267 | result = uwb_est_grow(); | ||
268 | if (result < 0) | ||
269 | goto out; | ||
270 | } | ||
271 | /* Find the right spot to insert it in */ | ||
272 | type_event_high = type << 8 | event_high; | ||
273 | for (itr = 0; itr < uwb_est_used; itr++) | ||
274 | if (uwb_est[itr].type_event_high < type | ||
275 | && uwb_est[itr].vendor < vendor | ||
276 | && uwb_est[itr].product < product) | ||
277 | break; | ||
278 | |||
279 | /* Shift others to make room for the new one? */ | ||
280 | if (itr < uwb_est_used) | ||
281 | memmove(&uwb_est[itr+1], &uwb_est[itr], uwb_est_used - itr); | ||
282 | uwb_est[itr].type_event_high = type << 8 | event_high; | ||
283 | uwb_est[itr].vendor = vendor; | ||
284 | uwb_est[itr].product = product; | ||
285 | uwb_est[itr].entry = entry; | ||
286 | uwb_est[itr].entries = entries; | ||
287 | uwb_est_used++; | ||
288 | out: | ||
289 | write_unlock_irqrestore(&uwb_est_lock, flags); | ||
290 | return result; | ||
291 | } | ||
292 | EXPORT_SYMBOL_GPL(uwb_est_register); | ||
293 | |||
294 | |||
295 | /** | ||
296 | * Unregister an event size table | ||
297 | * | ||
298 | * This just removes the specified entry and moves the ones after it | ||
299 | * to fill in the gap. This is needed to keep the list sorted; no | ||
300 | * reallocation is done to reduce the size of the table. | ||
301 | * | ||
302 | * We unregister by all the data we used to register instead of by | ||
303 | * pointer to the @entry array because we might have used the same | ||
304 | * table for a bunch of IDs (for example). | ||
305 | * | ||
306 | * @returns 0 if ok, < 0 errno on error (-ENOENT if not found). | ||
307 | */ | ||
308 | int uwb_est_unregister(u8 type, u8 event_high, u16 vendor, u16 product, | ||
309 | const struct uwb_est_entry *entry, size_t entries) | ||
310 | { | ||
311 | unsigned long flags; | ||
312 | unsigned itr; | ||
313 | struct uwb_est est_cmp = { | ||
314 | .type_event_high = type << 8 | event_high, | ||
315 | .vendor = vendor, | ||
316 | .product = product, | ||
317 | .entry = entry, | ||
318 | .entries = entries | ||
319 | }; | ||
320 | write_lock_irqsave(&uwb_est_lock, flags); | ||
321 | for (itr = 0; itr < uwb_est_used; itr++) | ||
322 | if (!memcmp(&uwb_est[itr], &est_cmp, sizeof(est_cmp))) | ||
323 | goto found; | ||
324 | write_unlock_irqrestore(&uwb_est_lock, flags); | ||
325 | return -ENOENT; | ||
326 | |||
327 | found: | ||
328 | if (itr < uwb_est_used - 1) /* Not last one? move ones above */ | ||
329 | memmove(&uwb_est[itr], &uwb_est[itr+1], uwb_est_used - itr - 1); | ||
330 | uwb_est_used--; | ||
331 | write_unlock_irqrestore(&uwb_est_lock, flags); | ||
332 | return 0; | ||
333 | } | ||
334 | EXPORT_SYMBOL_GPL(uwb_est_unregister); | ||
335 | |||
336 | |||
337 | /** | ||
338 | * Get the size of an event from a table | ||
339 | * | ||
340 | * @rceb: pointer to the buffer with the event | ||
341 | * @rceb_size: size of the area pointed to by @rceb in bytes. | ||
342 | * @returns: > 0 Size of the event | ||
343 | * -ENOSPC An area big enough was not provided to look | ||
344 | * ahead into the event's guts and guess the size. | ||
345 | * -EINVAL Unknown event code (wEvent). | ||
346 | * | ||
347 | * This will look at the received RCEB and guess what is the total | ||
348 | * size. For variable sized events, it will look further ahead into | ||
349 | * their length field to see how much data should be read. | ||
350 | * | ||
351 | * Note this size is *not* final--the neh (Notification/Event Handle) | ||
352 | * might specificy an extra size to add. | ||
353 | */ | ||
354 | static | ||
355 | ssize_t uwb_est_get_size(struct uwb_rc *uwb_rc, struct uwb_est *est, | ||
356 | u8 event_low, const struct uwb_rceb *rceb, | ||
357 | size_t rceb_size) | ||
358 | { | ||
359 | unsigned offset; | ||
360 | ssize_t size; | ||
361 | struct device *dev = &uwb_rc->uwb_dev.dev; | ||
362 | const struct uwb_est_entry *entry; | ||
363 | |||
364 | size = -ENOENT; | ||
365 | if (event_low >= est->entries) { /* in range? */ | ||
366 | if (printk_ratelimit()) | ||
367 | dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: " | ||
368 | "event %u out of range\n", | ||
369 | est, est->type_event_high, est->vendor, | ||
370 | est->product, est->entries, | ||
371 | event_low); | ||
372 | goto out; | ||
373 | } | ||
374 | size = -ENOENT; | ||
375 | entry = &est->entry[event_low]; | ||
376 | if (entry->size == 0 && entry->offset == 0) { /* unknown? */ | ||
377 | if (printk_ratelimit()) | ||
378 | dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: " | ||
379 | "event %u unknown\n", | ||
380 | est, est->type_event_high, est->vendor, | ||
381 | est->product, est->entries, event_low); | ||
382 | goto out; | ||
383 | } | ||
384 | offset = entry->offset; /* extra fries with that? */ | ||
385 | if (offset == 0) | ||
386 | size = entry->size; | ||
387 | else { | ||
388 | /* Ops, got an extra size field at 'offset'--read it */ | ||
389 | const void *ptr = rceb; | ||
390 | size_t type_size = 0; | ||
391 | offset--; | ||
392 | size = -ENOSPC; /* enough data for more? */ | ||
393 | switch (entry->type) { | ||
394 | case UWB_EST_16: type_size = sizeof(__le16); break; | ||
395 | case UWB_EST_8: type_size = sizeof(u8); break; | ||
396 | default: BUG(); | ||
397 | } | ||
398 | if (offset + type_size >= rceb_size) { | ||
399 | if (printk_ratelimit()) | ||
400 | dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: " | ||
401 | "not enough data to read extra size\n", | ||
402 | est, est->type_event_high, est->vendor, | ||
403 | est->product, est->entries); | ||
404 | goto out; | ||
405 | } | ||
406 | size = entry->size; | ||
407 | ptr += offset; | ||
408 | switch (entry->type) { | ||
409 | case UWB_EST_16: size += le16_to_cpu(*(__le16 *)ptr); break; | ||
410 | case UWB_EST_8: size += *(u8 *)ptr; break; | ||
411 | default: BUG(); | ||
412 | } | ||
413 | } | ||
414 | out: | ||
415 | return size; | ||
416 | } | ||
417 | |||
418 | |||
419 | /** | ||
420 | * Guesses the size of a WA event | ||
421 | * | ||
422 | * @rceb: pointer to the buffer with the event | ||
423 | * @rceb_size: size of the area pointed to by @rceb in bytes. | ||
424 | * @returns: > 0 Size of the event | ||
425 | * -ENOSPC An area big enough was not provided to look | ||
426 | * ahead into the event's guts and guess the size. | ||
427 | * -EINVAL Unknown event code (wEvent). | ||
428 | * | ||
429 | * This will look at the received RCEB and guess what is the total | ||
430 | * size by checking all the tables registered with | ||
431 | * uwb_est_register(). For variable sized events, it will look further | ||
432 | * ahead into their length field to see how much data should be read. | ||
433 | * | ||
434 | * Note this size is *not* final--the neh (Notification/Event Handle) | ||
435 | * might specificy an extra size to add or replace. | ||
436 | */ | ||
437 | ssize_t uwb_est_find_size(struct uwb_rc *rc, const struct uwb_rceb *rceb, | ||
438 | size_t rceb_size) | ||
439 | { | ||
440 | /* FIXME: add vendor/product data */ | ||
441 | ssize_t size; | ||
442 | struct device *dev = &rc->uwb_dev.dev; | ||
443 | unsigned long flags; | ||
444 | unsigned itr; | ||
445 | u16 type_event_high, event; | ||
446 | u8 *ptr = (u8 *) rceb; | ||
447 | |||
448 | read_lock_irqsave(&uwb_est_lock, flags); | ||
449 | d_printf(2, dev, "Size query for event 0x%02x/%04x/%02x," | ||
450 | " buffer size %ld\n", | ||
451 | (unsigned) rceb->bEventType, | ||
452 | (unsigned) le16_to_cpu(rceb->wEvent), | ||
453 | (unsigned) rceb->bEventContext, | ||
454 | (long) rceb_size); | ||
455 | size = -ENOSPC; | ||
456 | if (rceb_size < sizeof(*rceb)) | ||
457 | goto out; | ||
458 | event = le16_to_cpu(rceb->wEvent); | ||
459 | type_event_high = rceb->bEventType << 8 | (event & 0xff00) >> 8; | ||
460 | for (itr = 0; itr < uwb_est_used; itr++) { | ||
461 | d_printf(3, dev, "Checking EST 0x%04x/%04x/%04x\n", | ||
462 | uwb_est[itr].type_event_high, uwb_est[itr].vendor, | ||
463 | uwb_est[itr].product); | ||
464 | if (uwb_est[itr].type_event_high != type_event_high) | ||
465 | continue; | ||
466 | size = uwb_est_get_size(rc, &uwb_est[itr], | ||
467 | event & 0x00ff, rceb, rceb_size); | ||
468 | /* try more tables that might handle the same type */ | ||
469 | if (size != -ENOENT) | ||
470 | goto out; | ||
471 | } | ||
472 | /* FIXME: downgrade to _dbg() */ | ||
473 | if (printk_ratelimit()) | ||
474 | dev_err(dev, "event 0x%02x/%04x/%02x: no handlers available; " | ||
475 | "RCEB %02x %02x %02x %02x\n", | ||
476 | (unsigned) rceb->bEventType, | ||
477 | (unsigned) le16_to_cpu(rceb->wEvent), | ||
478 | (unsigned) rceb->bEventContext, | ||
479 | ptr[0], ptr[1], ptr[2], ptr[3]); | ||
480 | size = -ENOENT; | ||
481 | out: | ||
482 | read_unlock_irqrestore(&uwb_est_lock, flags); | ||
483 | return size; | ||
484 | } | ||
485 | EXPORT_SYMBOL_GPL(uwb_est_find_size); | ||
diff --git a/drivers/uwb/neh.c b/drivers/uwb/neh.c new file mode 100644 index 000000000000..91b61480e1ee --- /dev/null +++ b/drivers/uwb/neh.c | |||
@@ -0,0 +1,616 @@ | |||
1 | /* | ||
2 | * WUSB Wire Adapter: Radio Control Interface (WUSB[8]) | ||
3 | * Notification and Event Handling | ||
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 | * The RC interface of the Host Wire Adapter (USB dongle) or WHCI PCI | ||
24 | * card delivers a stream of notifications and events to the | ||
25 | * notification end event endpoint or area. This code takes care of | ||
26 | * getting a buffer with that data, breaking it up in separate | ||
27 | * notifications and events and then deliver those. | ||
28 | * | ||
29 | * Events are answers to commands and they carry a context ID that | ||
30 | * associates them to the command. Notifications are that, | ||
31 | * notifications, they come out of the blue and have a context ID of | ||
32 | * zero. Think of the context ID kind of like a handler. The | ||
33 | * uwb_rc_neh_* code deals with managing context IDs. | ||
34 | * | ||
35 | * This is why you require a handle to operate on a UWB host. When you | ||
36 | * open a handle a context ID is assigned to you. | ||
37 | * | ||
38 | * So, as it is done is: | ||
39 | * | ||
40 | * 1. Add an event handler [uwb_rc_neh_add()] (assigns a ctx id) | ||
41 | * 2. Issue command [rc->cmd(rc, ...)] | ||
42 | * 3. Arm the timeout timer [uwb_rc_neh_arm()] | ||
43 | * 4, Release the reference to the neh [uwb_rc_neh_put()] | ||
44 | * 5. Wait for the callback | ||
45 | * 6. Command result (RCEB) is passed to the callback | ||
46 | * | ||
47 | * If (2) fails, you should remove the handle [uwb_rc_neh_rm()] | ||
48 | * instead of arming the timer. | ||
49 | * | ||
50 | * Handles are for using in *serialized* code, single thread. | ||
51 | * | ||
52 | * When the notification/event comes, the IRQ handler/endpoint | ||
53 | * callback passes the data read to uwb_rc_neh_grok() which will break | ||
54 | * it up in a discrete series of events, look up who is listening for | ||
55 | * them and execute the pertinent callbacks. | ||
56 | * | ||
57 | * If the reader detects an error while reading the data stream, call | ||
58 | * uwb_rc_neh_error(). | ||
59 | * | ||
60 | * CONSTRAINTS/ASSUMPTIONS: | ||
61 | * | ||
62 | * - Most notifications/events are small (less thank .5k), copying | ||
63 | * around is ok. | ||
64 | * | ||
65 | * - Notifications/events are ALWAYS smaller than PAGE_SIZE | ||
66 | * | ||
67 | * - Notifications/events always come in a single piece (ie: a buffer | ||
68 | * will always contain entire notifications/events). | ||
69 | * | ||
70 | * - we cannot know in advance how long each event is (because they | ||
71 | * lack a length field in their header--smart move by the standards | ||
72 | * body, btw). So we need a facility to get the event size given the | ||
73 | * header. This is what the EST code does (notif/Event Size | ||
74 | * Tables), check nest.c--as well, you can associate the size to | ||
75 | * the handle [w/ neh->extra_size()]. | ||
76 | * | ||
77 | * - Most notifications/events are fixed size; only a few are variable | ||
78 | * size (NEST takes care of that). | ||
79 | * | ||
80 | * - Listeners of events expect them, so they usually provide a | ||
81 | * buffer, as they know the size. Listeners to notifications don't, | ||
82 | * so we allocate their buffers dynamically. | ||
83 | */ | ||
84 | #include <linux/kernel.h> | ||
85 | #include <linux/timer.h> | ||
86 | #include <linux/err.h> | ||
87 | |||
88 | #include "uwb-internal.h" | ||
89 | #define D_LOCAL 0 | ||
90 | #include <linux/uwb/debug.h> | ||
91 | |||
92 | /* | ||
93 | * UWB Radio Controller Notification/Event Handle | ||
94 | * | ||
95 | * Represents an entity waiting for an event coming from the UWB Radio | ||
96 | * Controller with a given context id (context) and type (evt_type and | ||
97 | * evt). On reception of the notification/event, the callback (cb) is | ||
98 | * called with the event. | ||
99 | * | ||
100 | * If the timer expires before the event is received, the callback is | ||
101 | * called with -ETIMEDOUT as the event size. | ||
102 | */ | ||
103 | struct uwb_rc_neh { | ||
104 | struct kref kref; | ||
105 | |||
106 | struct uwb_rc *rc; | ||
107 | u8 evt_type; | ||
108 | __le16 evt; | ||
109 | u8 context; | ||
110 | uwb_rc_cmd_cb_f cb; | ||
111 | void *arg; | ||
112 | |||
113 | struct timer_list timer; | ||
114 | struct list_head list_node; | ||
115 | }; | ||
116 | |||
117 | static void uwb_rc_neh_timer(unsigned long arg); | ||
118 | |||
119 | static void uwb_rc_neh_release(struct kref *kref) | ||
120 | { | ||
121 | struct uwb_rc_neh *neh = container_of(kref, struct uwb_rc_neh, kref); | ||
122 | |||
123 | kfree(neh); | ||
124 | } | ||
125 | |||
126 | static void uwb_rc_neh_get(struct uwb_rc_neh *neh) | ||
127 | { | ||
128 | kref_get(&neh->kref); | ||
129 | } | ||
130 | |||
131 | /** | ||
132 | * uwb_rc_neh_put - release reference to a neh | ||
133 | * @neh: the neh | ||
134 | */ | ||
135 | void uwb_rc_neh_put(struct uwb_rc_neh *neh) | ||
136 | { | ||
137 | kref_put(&neh->kref, uwb_rc_neh_release); | ||
138 | } | ||
139 | |||
140 | |||
141 | /** | ||
142 | * Assigns @neh a context id from @rc's pool | ||
143 | * | ||
144 | * @rc: UWB Radio Controller descriptor; @rc->neh_lock taken | ||
145 | * @neh: Notification/Event Handle | ||
146 | * @returns 0 if context id was assigned ok; < 0 errno on error (if | ||
147 | * all the context IDs are taken). | ||
148 | * | ||
149 | * (assumes @wa is locked). | ||
150 | * | ||
151 | * NOTE: WUSB spec reserves context ids 0x00 for notifications and | ||
152 | * 0xff is invalid, so they must not be used. Initialization | ||
153 | * fills up those two in the bitmap so they are not allocated. | ||
154 | * | ||
155 | * We spread the allocation around to reduce the posiblity of two | ||
156 | * consecutive opened @neh's getting the same context ID assigned (to | ||
157 | * avoid surprises with late events that timed out long time ago). So | ||
158 | * first we search from where @rc->ctx_roll is, if not found, we | ||
159 | * search from zero. | ||
160 | */ | ||
161 | static | ||
162 | int __uwb_rc_ctx_get(struct uwb_rc *rc, struct uwb_rc_neh *neh) | ||
163 | { | ||
164 | int result; | ||
165 | result = find_next_zero_bit(rc->ctx_bm, UWB_RC_CTX_MAX, | ||
166 | rc->ctx_roll++); | ||
167 | if (result < UWB_RC_CTX_MAX) | ||
168 | goto found; | ||
169 | result = find_first_zero_bit(rc->ctx_bm, UWB_RC_CTX_MAX); | ||
170 | if (result < UWB_RC_CTX_MAX) | ||
171 | goto found; | ||
172 | return -ENFILE; | ||
173 | found: | ||
174 | set_bit(result, rc->ctx_bm); | ||
175 | neh->context = result; | ||
176 | return 0; | ||
177 | } | ||
178 | |||
179 | |||
180 | /** Releases @neh's context ID back to @rc (@rc->neh_lock is locked). */ | ||
181 | static | ||
182 | void __uwb_rc_ctx_put(struct uwb_rc *rc, struct uwb_rc_neh *neh) | ||
183 | { | ||
184 | struct device *dev = &rc->uwb_dev.dev; | ||
185 | if (neh->context == 0) | ||
186 | return; | ||
187 | if (test_bit(neh->context, rc->ctx_bm) == 0) { | ||
188 | dev_err(dev, "context %u not set in bitmap\n", | ||
189 | neh->context); | ||
190 | WARN_ON(1); | ||
191 | } | ||
192 | clear_bit(neh->context, rc->ctx_bm); | ||
193 | neh->context = 0; | ||
194 | } | ||
195 | |||
196 | /** | ||
197 | * uwb_rc_neh_add - add a neh for a radio controller command | ||
198 | * @rc: the radio controller | ||
199 | * @cmd: the radio controller command | ||
200 | * @expected_type: the type of the expected response event | ||
201 | * @expected_event: the expected event ID | ||
202 | * @cb: callback for when the event is received | ||
203 | * @arg: argument for the callback | ||
204 | * | ||
205 | * Creates a neh and adds it to the list of those waiting for an | ||
206 | * event. A context ID will be assigned to the command. | ||
207 | */ | ||
208 | struct uwb_rc_neh *uwb_rc_neh_add(struct uwb_rc *rc, struct uwb_rccb *cmd, | ||
209 | u8 expected_type, u16 expected_event, | ||
210 | uwb_rc_cmd_cb_f cb, void *arg) | ||
211 | { | ||
212 | int result; | ||
213 | unsigned long flags; | ||
214 | struct device *dev = &rc->uwb_dev.dev; | ||
215 | struct uwb_rc_neh *neh; | ||
216 | |||
217 | neh = kzalloc(sizeof(*neh), GFP_KERNEL); | ||
218 | if (neh == NULL) { | ||
219 | result = -ENOMEM; | ||
220 | goto error_kzalloc; | ||
221 | } | ||
222 | |||
223 | kref_init(&neh->kref); | ||
224 | INIT_LIST_HEAD(&neh->list_node); | ||
225 | init_timer(&neh->timer); | ||
226 | neh->timer.function = uwb_rc_neh_timer; | ||
227 | neh->timer.data = (unsigned long)neh; | ||
228 | |||
229 | neh->rc = rc; | ||
230 | neh->evt_type = expected_type; | ||
231 | neh->evt = cpu_to_le16(expected_event); | ||
232 | neh->cb = cb; | ||
233 | neh->arg = arg; | ||
234 | |||
235 | spin_lock_irqsave(&rc->neh_lock, flags); | ||
236 | result = __uwb_rc_ctx_get(rc, neh); | ||
237 | if (result >= 0) { | ||
238 | cmd->bCommandContext = neh->context; | ||
239 | list_add_tail(&neh->list_node, &rc->neh_list); | ||
240 | uwb_rc_neh_get(neh); | ||
241 | } | ||
242 | spin_unlock_irqrestore(&rc->neh_lock, flags); | ||
243 | if (result < 0) | ||
244 | goto error_ctx_get; | ||
245 | |||
246 | return neh; | ||
247 | |||
248 | error_ctx_get: | ||
249 | kfree(neh); | ||
250 | error_kzalloc: | ||
251 | dev_err(dev, "cannot open handle to radio controller: %d\n", result); | ||
252 | return ERR_PTR(result); | ||
253 | } | ||
254 | |||
255 | static void __uwb_rc_neh_rm(struct uwb_rc *rc, struct uwb_rc_neh *neh) | ||
256 | { | ||
257 | del_timer(&neh->timer); | ||
258 | __uwb_rc_ctx_put(rc, neh); | ||
259 | list_del(&neh->list_node); | ||
260 | } | ||
261 | |||
262 | /** | ||
263 | * uwb_rc_neh_rm - remove a neh. | ||
264 | * @rc: the radio controller | ||
265 | * @neh: the neh to remove | ||
266 | * | ||
267 | * Remove an active neh immediately instead of waiting for the event | ||
268 | * (or a time out). | ||
269 | */ | ||
270 | void uwb_rc_neh_rm(struct uwb_rc *rc, struct uwb_rc_neh *neh) | ||
271 | { | ||
272 | unsigned long flags; | ||
273 | |||
274 | spin_lock_irqsave(&rc->neh_lock, flags); | ||
275 | __uwb_rc_neh_rm(rc, neh); | ||
276 | spin_unlock_irqrestore(&rc->neh_lock, flags); | ||
277 | |||
278 | uwb_rc_neh_put(neh); | ||
279 | } | ||
280 | |||
281 | /** | ||
282 | * uwb_rc_neh_arm - arm an event handler timeout timer | ||
283 | * | ||
284 | * @rc: UWB Radio Controller | ||
285 | * @neh: Notification/event handler for @rc | ||
286 | * | ||
287 | * The timer is only armed if the neh is active. | ||
288 | */ | ||
289 | void uwb_rc_neh_arm(struct uwb_rc *rc, struct uwb_rc_neh *neh) | ||
290 | { | ||
291 | unsigned long flags; | ||
292 | |||
293 | spin_lock_irqsave(&rc->neh_lock, flags); | ||
294 | if (neh->context) | ||
295 | mod_timer(&neh->timer, | ||
296 | jiffies + msecs_to_jiffies(UWB_RC_CMD_TIMEOUT_MS)); | ||
297 | spin_unlock_irqrestore(&rc->neh_lock, flags); | ||
298 | } | ||
299 | |||
300 | static void uwb_rc_neh_cb(struct uwb_rc_neh *neh, struct uwb_rceb *rceb, size_t size) | ||
301 | { | ||
302 | (*neh->cb)(neh->rc, neh->arg, rceb, size); | ||
303 | uwb_rc_neh_put(neh); | ||
304 | } | ||
305 | |||
306 | static bool uwb_rc_neh_match(struct uwb_rc_neh *neh, const struct uwb_rceb *rceb) | ||
307 | { | ||
308 | return neh->evt_type == rceb->bEventType | ||
309 | && neh->evt == rceb->wEvent | ||
310 | && neh->context == rceb->bEventContext; | ||
311 | } | ||
312 | |||
313 | /** | ||
314 | * Find the handle waiting for a RC Radio Control Event | ||
315 | * | ||
316 | * @rc: UWB Radio Controller | ||
317 | * @rceb: Pointer to the RCEB buffer | ||
318 | * @event_size: Pointer to the size of the RCEB buffer. Might be | ||
319 | * adjusted to take into account the @neh->extra_size | ||
320 | * settings. | ||
321 | * | ||
322 | * If the listener has no buffer (NULL buffer), one is allocated for | ||
323 | * the right size (the amount of data received). @neh->ptr will point | ||
324 | * to the event payload, which always starts with a 'struct | ||
325 | * uwb_rceb'. kfree() it when done. | ||
326 | */ | ||
327 | static | ||
328 | struct uwb_rc_neh *uwb_rc_neh_lookup(struct uwb_rc *rc, | ||
329 | const struct uwb_rceb *rceb) | ||
330 | { | ||
331 | struct uwb_rc_neh *neh = NULL, *h; | ||
332 | unsigned long flags; | ||
333 | |||
334 | spin_lock_irqsave(&rc->neh_lock, flags); | ||
335 | |||
336 | list_for_each_entry(h, &rc->neh_list, list_node) { | ||
337 | if (uwb_rc_neh_match(h, rceb)) { | ||
338 | neh = h; | ||
339 | break; | ||
340 | } | ||
341 | } | ||
342 | |||
343 | if (neh) | ||
344 | __uwb_rc_neh_rm(rc, neh); | ||
345 | |||
346 | spin_unlock_irqrestore(&rc->neh_lock, flags); | ||
347 | |||
348 | return neh; | ||
349 | } | ||
350 | |||
351 | |||
352 | /** | ||
353 | * Process notifications coming from the radio control interface | ||
354 | * | ||
355 | * @rc: UWB Radio Control Interface descriptor | ||
356 | * @neh: Notification/Event Handler @neh->ptr points to | ||
357 | * @uwb_evt->buffer. | ||
358 | * | ||
359 | * This function is called by the event/notif handling subsystem when | ||
360 | * notifications arrive (hwarc_probe() arms a notification/event handle | ||
361 | * that calls back this function for every received notification; this | ||
362 | * function then will rearm itself). | ||
363 | * | ||
364 | * Notification data buffers are dynamically allocated by the NEH | ||
365 | * handling code in neh.c [uwb_rc_neh_lookup()]. What is actually | ||
366 | * allocated is space to contain the notification data. | ||
367 | * | ||
368 | * Buffers are prefixed with a Radio Control Event Block (RCEB) as | ||
369 | * defined by the WUSB Wired-Adapter Radio Control interface. We | ||
370 | * just use it for the notification code. | ||
371 | * | ||
372 | * On each case statement we just transcode endianess of the different | ||
373 | * fields. We declare a pointer to a RCI definition of an event, and | ||
374 | * then to a UWB definition of the same event (which are the same, | ||
375 | * remember). Event if we use different pointers | ||
376 | */ | ||
377 | static | ||
378 | void uwb_rc_notif(struct uwb_rc *rc, struct uwb_rceb *rceb, ssize_t size) | ||
379 | { | ||
380 | struct device *dev = &rc->uwb_dev.dev; | ||
381 | struct uwb_event *uwb_evt; | ||
382 | |||
383 | if (size == -ESHUTDOWN) | ||
384 | return; | ||
385 | if (size < 0) { | ||
386 | dev_err(dev, "ignoring event with error code %zu\n", | ||
387 | size); | ||
388 | return; | ||
389 | } | ||
390 | |||
391 | uwb_evt = kzalloc(sizeof(*uwb_evt), GFP_ATOMIC); | ||
392 | if (unlikely(uwb_evt == NULL)) { | ||
393 | dev_err(dev, "no memory to queue event 0x%02x/%04x/%02x\n", | ||
394 | rceb->bEventType, le16_to_cpu(rceb->wEvent), | ||
395 | rceb->bEventContext); | ||
396 | return; | ||
397 | } | ||
398 | uwb_evt->rc = __uwb_rc_get(rc); /* will be put by uwbd's uwbd_event_handle() */ | ||
399 | uwb_evt->ts_jiffies = jiffies; | ||
400 | uwb_evt->type = UWB_EVT_TYPE_NOTIF; | ||
401 | uwb_evt->notif.size = size; | ||
402 | uwb_evt->notif.rceb = rceb; | ||
403 | |||
404 | switch (le16_to_cpu(rceb->wEvent)) { | ||
405 | /* Trap some vendor specific events | ||
406 | * | ||
407 | * FIXME: move this to handling in ptc-est, where we | ||
408 | * register a NULL event handler for these two guys | ||
409 | * using the Intel IDs. | ||
410 | */ | ||
411 | case 0x0103: | ||
412 | dev_info(dev, "FIXME: DEVICE ADD\n"); | ||
413 | return; | ||
414 | case 0x0104: | ||
415 | dev_info(dev, "FIXME: DEVICE RM\n"); | ||
416 | return; | ||
417 | default: | ||
418 | break; | ||
419 | } | ||
420 | |||
421 | uwbd_event_queue(uwb_evt); | ||
422 | } | ||
423 | |||
424 | static void uwb_rc_neh_grok_event(struct uwb_rc *rc, struct uwb_rceb *rceb, size_t size) | ||
425 | { | ||
426 | struct device *dev = &rc->uwb_dev.dev; | ||
427 | struct uwb_rc_neh *neh; | ||
428 | struct uwb_rceb *notif; | ||
429 | |||
430 | if (rceb->bEventContext == 0) { | ||
431 | notif = kmalloc(size, GFP_ATOMIC); | ||
432 | if (notif) { | ||
433 | memcpy(notif, rceb, size); | ||
434 | uwb_rc_notif(rc, notif, size); | ||
435 | } else | ||
436 | dev_err(dev, "event 0x%02x/%04x/%02x (%zu bytes): no memory\n", | ||
437 | rceb->bEventType, le16_to_cpu(rceb->wEvent), | ||
438 | rceb->bEventContext, size); | ||
439 | } else { | ||
440 | neh = uwb_rc_neh_lookup(rc, rceb); | ||
441 | if (neh) | ||
442 | uwb_rc_neh_cb(neh, rceb, size); | ||
443 | else if (printk_ratelimit()) | ||
444 | dev_warn(dev, "event 0x%02x/%04x/%02x (%zu bytes): nobody cared\n", | ||
445 | rceb->bEventType, le16_to_cpu(rceb->wEvent), | ||
446 | rceb->bEventContext, size); | ||
447 | } | ||
448 | } | ||
449 | |||
450 | /** | ||
451 | * Given a buffer with one or more UWB RC events/notifications, break | ||
452 | * them up and dispatch them. | ||
453 | * | ||
454 | * @rc: UWB Radio Controller | ||
455 | * @buf: Buffer with the stream of notifications/events | ||
456 | * @buf_size: Amount of data in the buffer | ||
457 | * | ||
458 | * Note each notification/event starts always with a 'struct | ||
459 | * uwb_rceb', so the minimum size if 4 bytes. | ||
460 | * | ||
461 | * The device may pass us events formatted differently than expected. | ||
462 | * These are first filtered, potentially creating a new event in a new | ||
463 | * memory location. If a new event is created by the filter it is also | ||
464 | * freed here. | ||
465 | * | ||
466 | * For each notif/event, tries to guess the size looking at the EST | ||
467 | * tables, then looks for a neh that is waiting for that event and if | ||
468 | * found, copies the payload to the neh's buffer and calls it back. If | ||
469 | * not, the data is ignored. | ||
470 | * | ||
471 | * Note that if we can't find a size description in the EST tables, we | ||
472 | * still might find a size in the 'neh' handle in uwb_rc_neh_lookup(). | ||
473 | * | ||
474 | * Assumptions: | ||
475 | * | ||
476 | * @rc->neh_lock is NOT taken | ||
477 | * | ||
478 | * We keep track of various sizes here: | ||
479 | * size: contains the size of the buffer that is processed for the | ||
480 | * incoming event. this buffer may contain events that are not | ||
481 | * formatted as WHCI. | ||
482 | * real_size: the actual space taken by this event in the buffer. | ||
483 | * We need to keep track of the real size of an event to be able to | ||
484 | * advance the buffer correctly. | ||
485 | * event_size: the size of the event as expected by the core layer | ||
486 | * [OR] the size of the event after filtering. if the filtering | ||
487 | * created a new event in a new memory location then this is | ||
488 | * effectively the size of a new event buffer | ||
489 | */ | ||
490 | void uwb_rc_neh_grok(struct uwb_rc *rc, void *buf, size_t buf_size) | ||
491 | { | ||
492 | struct device *dev = &rc->uwb_dev.dev; | ||
493 | void *itr; | ||
494 | struct uwb_rceb *rceb; | ||
495 | size_t size, real_size, event_size; | ||
496 | int needtofree; | ||
497 | |||
498 | d_fnstart(3, dev, "(rc %p buf %p %zu buf_size)\n", rc, buf, buf_size); | ||
499 | d_printf(2, dev, "groking event block: %zu bytes\n", buf_size); | ||
500 | itr = buf; | ||
501 | size = buf_size; | ||
502 | while (size > 0) { | ||
503 | if (size < sizeof(*rceb)) { | ||
504 | dev_err(dev, "not enough data in event buffer to " | ||
505 | "process incoming events (%zu left, minimum is " | ||
506 | "%zu)\n", size, sizeof(*rceb)); | ||
507 | break; | ||
508 | } | ||
509 | |||
510 | rceb = itr; | ||
511 | if (rc->filter_event) { | ||
512 | needtofree = rc->filter_event(rc, &rceb, size, | ||
513 | &real_size, &event_size); | ||
514 | if (needtofree < 0 && needtofree != -ENOANO) { | ||
515 | dev_err(dev, "BUG: Unable to filter event " | ||
516 | "(0x%02x/%04x/%02x) from " | ||
517 | "device. \n", rceb->bEventType, | ||
518 | le16_to_cpu(rceb->wEvent), | ||
519 | rceb->bEventContext); | ||
520 | break; | ||
521 | } | ||
522 | } else | ||
523 | needtofree = -ENOANO; | ||
524 | /* do real processing if there was no filtering or the | ||
525 | * filtering didn't act */ | ||
526 | if (needtofree == -ENOANO) { | ||
527 | ssize_t ret = uwb_est_find_size(rc, rceb, size); | ||
528 | if (ret < 0) | ||
529 | break; | ||
530 | if (ret > size) { | ||
531 | dev_err(dev, "BUG: hw sent incomplete event " | ||
532 | "0x%02x/%04x/%02x (%zd bytes), only got " | ||
533 | "%zu bytes. We don't handle that.\n", | ||
534 | rceb->bEventType, le16_to_cpu(rceb->wEvent), | ||
535 | rceb->bEventContext, ret, size); | ||
536 | break; | ||
537 | } | ||
538 | real_size = event_size = ret; | ||
539 | } | ||
540 | uwb_rc_neh_grok_event(rc, rceb, event_size); | ||
541 | |||
542 | if (needtofree == 1) | ||
543 | kfree(rceb); | ||
544 | |||
545 | itr += real_size; | ||
546 | size -= real_size; | ||
547 | d_printf(2, dev, "consumed %zd bytes, %zu left\n", | ||
548 | event_size, size); | ||
549 | } | ||
550 | d_fnend(3, dev, "(rc %p buf %p %zu buf_size) = void\n", rc, buf, buf_size); | ||
551 | } | ||
552 | EXPORT_SYMBOL_GPL(uwb_rc_neh_grok); | ||
553 | |||
554 | |||
555 | /** | ||
556 | * The entity that reads from the device notification/event channel has | ||
557 | * detected an error. | ||
558 | * | ||
559 | * @rc: UWB Radio Controller | ||
560 | * @error: Errno error code | ||
561 | * | ||
562 | */ | ||
563 | void uwb_rc_neh_error(struct uwb_rc *rc, int error) | ||
564 | { | ||
565 | struct uwb_rc_neh *neh, *next; | ||
566 | unsigned long flags; | ||
567 | |||
568 | BUG_ON(error >= 0); | ||
569 | spin_lock_irqsave(&rc->neh_lock, flags); | ||
570 | list_for_each_entry_safe(neh, next, &rc->neh_list, list_node) { | ||
571 | __uwb_rc_neh_rm(rc, neh); | ||
572 | uwb_rc_neh_cb(neh, NULL, error); | ||
573 | } | ||
574 | spin_unlock_irqrestore(&rc->neh_lock, flags); | ||
575 | } | ||
576 | EXPORT_SYMBOL_GPL(uwb_rc_neh_error); | ||
577 | |||
578 | |||
579 | static void uwb_rc_neh_timer(unsigned long arg) | ||
580 | { | ||
581 | struct uwb_rc_neh *neh = (struct uwb_rc_neh *)arg; | ||
582 | struct uwb_rc *rc = neh->rc; | ||
583 | unsigned long flags; | ||
584 | |||
585 | spin_lock_irqsave(&rc->neh_lock, flags); | ||
586 | __uwb_rc_neh_rm(rc, neh); | ||
587 | spin_unlock_irqrestore(&rc->neh_lock, flags); | ||
588 | |||
589 | uwb_rc_neh_cb(neh, NULL, -ETIMEDOUT); | ||
590 | } | ||
591 | |||
592 | /** Initializes the @rc's neh subsystem | ||
593 | */ | ||
594 | void uwb_rc_neh_create(struct uwb_rc *rc) | ||
595 | { | ||
596 | spin_lock_init(&rc->neh_lock); | ||
597 | INIT_LIST_HEAD(&rc->neh_list); | ||
598 | set_bit(0, rc->ctx_bm); /* 0 is reserved (see [WUSB] table 8-65) */ | ||
599 | set_bit(0xff, rc->ctx_bm); /* and 0xff is invalid */ | ||
600 | rc->ctx_roll = 1; | ||
601 | } | ||
602 | |||
603 | |||
604 | /** Release's the @rc's neh subsystem */ | ||
605 | void uwb_rc_neh_destroy(struct uwb_rc *rc) | ||
606 | { | ||
607 | unsigned long flags; | ||
608 | struct uwb_rc_neh *neh, *next; | ||
609 | |||
610 | spin_lock_irqsave(&rc->neh_lock, flags); | ||
611 | list_for_each_entry_safe(neh, next, &rc->neh_list, list_node) { | ||
612 | __uwb_rc_neh_rm(rc, neh); | ||
613 | uwb_rc_neh_put(neh); | ||
614 | } | ||
615 | spin_unlock_irqrestore(&rc->neh_lock, flags); | ||
616 | } | ||
diff --git a/drivers/uwb/reset.c b/drivers/uwb/reset.c new file mode 100644 index 000000000000..8de856fa7958 --- /dev/null +++ b/drivers/uwb/reset.c | |||
@@ -0,0 +1,362 @@ | |||
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> | ||
33 | |||
34 | #include "uwb-internal.h" | ||
35 | #define D_LOCAL 0 | ||
36 | #include <linux/uwb/debug.h> | ||
37 | |||
38 | /** | ||
39 | * Command result codes (WUSB1.0[T8-69]) | ||
40 | */ | ||
41 | static | ||
42 | const char *__strerror[] = { | ||
43 | "success", | ||
44 | "failure", | ||
45 | "hardware failure", | ||
46 | "no more slots", | ||
47 | "beacon is too large", | ||
48 | "invalid parameter", | ||
49 | "unsupported power level", | ||
50 | "time out (wa) or invalid ie data (whci)", | ||
51 | "beacon size exceeded", | ||
52 | "cancelled", | ||
53 | "invalid state", | ||
54 | "invalid size", | ||
55 | "ack not recieved", | ||
56 | "no more asie notification", | ||
57 | }; | ||
58 | |||
59 | |||
60 | /** Return a string matching the given error code */ | ||
61 | const char *uwb_rc_strerror(unsigned code) | ||
62 | { | ||
63 | if (code == 255) | ||
64 | return "time out"; | ||
65 | if (code >= ARRAY_SIZE(__strerror)) | ||
66 | return "unknown error"; | ||
67 | return __strerror[code]; | ||
68 | } | ||
69 | |||
70 | int uwb_rc_cmd_async(struct uwb_rc *rc, const char *cmd_name, | ||
71 | struct uwb_rccb *cmd, size_t cmd_size, | ||
72 | u8 expected_type, u16 expected_event, | ||
73 | uwb_rc_cmd_cb_f cb, void *arg) | ||
74 | { | ||
75 | struct device *dev = &rc->uwb_dev.dev; | ||
76 | struct uwb_rc_neh *neh; | ||
77 | int needtofree = 0; | ||
78 | int result; | ||
79 | |||
80 | uwb_dev_lock(&rc->uwb_dev); /* Protect against rc->priv being removed */ | ||
81 | if (rc->priv == NULL) { | ||
82 | uwb_dev_unlock(&rc->uwb_dev); | ||
83 | return -ESHUTDOWN; | ||
84 | } | ||
85 | |||
86 | if (rc->filter_cmd) { | ||
87 | needtofree = rc->filter_cmd(rc, &cmd, &cmd_size); | ||
88 | if (needtofree < 0 && needtofree != -ENOANO) { | ||
89 | dev_err(dev, "%s: filter error: %d\n", | ||
90 | cmd_name, needtofree); | ||
91 | uwb_dev_unlock(&rc->uwb_dev); | ||
92 | return needtofree; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | neh = uwb_rc_neh_add(rc, cmd, expected_type, expected_event, cb, arg); | ||
97 | if (IS_ERR(neh)) { | ||
98 | result = PTR_ERR(neh); | ||
99 | goto out; | ||
100 | } | ||
101 | |||
102 | result = rc->cmd(rc, cmd, cmd_size); | ||
103 | uwb_dev_unlock(&rc->uwb_dev); | ||
104 | if (result < 0) | ||
105 | uwb_rc_neh_rm(rc, neh); | ||
106 | else | ||
107 | uwb_rc_neh_arm(rc, neh); | ||
108 | uwb_rc_neh_put(neh); | ||
109 | out: | ||
110 | if (needtofree == 1) | ||
111 | kfree(cmd); | ||
112 | return result < 0 ? result : 0; | ||
113 | } | ||
114 | EXPORT_SYMBOL_GPL(uwb_rc_cmd_async); | ||
115 | |||
116 | struct uwb_rc_cmd_done_params { | ||
117 | struct completion completion; | ||
118 | struct uwb_rceb *reply; | ||
119 | ssize_t reply_size; | ||
120 | }; | ||
121 | |||
122 | static void uwb_rc_cmd_done(struct uwb_rc *rc, void *arg, | ||
123 | struct uwb_rceb *reply, ssize_t reply_size) | ||
124 | { | ||
125 | struct uwb_rc_cmd_done_params *p = (struct uwb_rc_cmd_done_params *)arg; | ||
126 | |||
127 | if (reply_size > 0) { | ||
128 | if (p->reply) | ||
129 | reply_size = min(p->reply_size, reply_size); | ||
130 | else | ||
131 | p->reply = kmalloc(reply_size, GFP_ATOMIC); | ||
132 | |||
133 | if (p->reply) | ||
134 | memcpy(p->reply, reply, reply_size); | ||
135 | else | ||
136 | reply_size = -ENOMEM; | ||
137 | } | ||
138 | p->reply_size = reply_size; | ||
139 | complete(&p->completion); | ||
140 | } | ||
141 | |||
142 | |||
143 | /** | ||
144 | * Generic function for issuing commands to the Radio Control Interface | ||
145 | * | ||
146 | * @rc: UWB Radio Control descriptor | ||
147 | * @cmd_name: Name of the command being issued (for error messages) | ||
148 | * @cmd: Pointer to rccb structure containing the command; | ||
149 | * normally you embed this structure as the first member of | ||
150 | * the full command structure. | ||
151 | * @cmd_size: Size of the whole command buffer pointed to by @cmd. | ||
152 | * @reply: Pointer to where to store the reply | ||
153 | * @reply_size: @reply's size | ||
154 | * @expected_type: Expected type in the return event | ||
155 | * @expected_event: Expected event code in the return event | ||
156 | * @preply: Here a pointer to where the event data is received will | ||
157 | * be stored. Once done with the data, free with kfree(). | ||
158 | * | ||
159 | * This function is generic; it works for commands that return a fixed | ||
160 | * and known size or for commands that return a variable amount of data. | ||
161 | * | ||
162 | * If a buffer is provided, that is used, although it could be chopped | ||
163 | * to the maximum size of the buffer. If the buffer is NULL, then one | ||
164 | * be allocated in *preply with the whole contents of the reply. | ||
165 | * | ||
166 | * @rc needs to be referenced | ||
167 | */ | ||
168 | static | ||
169 | ssize_t __uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name, | ||
170 | struct uwb_rccb *cmd, size_t cmd_size, | ||
171 | struct uwb_rceb *reply, size_t reply_size, | ||
172 | u8 expected_type, u16 expected_event, | ||
173 | struct uwb_rceb **preply) | ||
174 | { | ||
175 | ssize_t result = 0; | ||
176 | struct device *dev = &rc->uwb_dev.dev; | ||
177 | struct uwb_rc_cmd_done_params params; | ||
178 | |||
179 | init_completion(¶ms.completion); | ||
180 | params.reply = reply; | ||
181 | params.reply_size = reply_size; | ||
182 | |||
183 | result = uwb_rc_cmd_async(rc, cmd_name, cmd, cmd_size, | ||
184 | expected_type, expected_event, | ||
185 | uwb_rc_cmd_done, ¶ms); | ||
186 | if (result) | ||
187 | return result; | ||
188 | |||
189 | wait_for_completion(¶ms.completion); | ||
190 | |||
191 | if (preply) | ||
192 | *preply = params.reply; | ||
193 | |||
194 | if (params.reply_size < 0) | ||
195 | dev_err(dev, "%s: confirmation event 0x%02x/%04x/%02x " | ||
196 | "reception failed: %d\n", cmd_name, | ||
197 | expected_type, expected_event, cmd->bCommandContext, | ||
198 | (int)params.reply_size); | ||
199 | return params.reply_size; | ||
200 | } | ||
201 | |||
202 | |||
203 | /** | ||
204 | * Generic function for issuing commands to the Radio Control Interface | ||
205 | * | ||
206 | * @rc: UWB Radio Control descriptor | ||
207 | * @cmd_name: Name of the command being issued (for error messages) | ||
208 | * @cmd: Pointer to rccb structure containing the command; | ||
209 | * normally you embed this structure as the first member of | ||
210 | * the full command structure. | ||
211 | * @cmd_size: Size of the whole command buffer pointed to by @cmd. | ||
212 | * @reply: Pointer to the beginning of the confirmation event | ||
213 | * buffer. Normally bigger than an 'struct hwarc_rceb'. | ||
214 | * You need to fill out reply->bEventType and reply->wEvent (in | ||
215 | * cpu order) as the function will use them to verify the | ||
216 | * confirmation event. | ||
217 | * @reply_size: Size of the reply buffer | ||
218 | * | ||
219 | * The function checks that the length returned in the reply is at | ||
220 | * least as big as @reply_size; if not, it will be deemed an error and | ||
221 | * -EIO returned. | ||
222 | * | ||
223 | * @rc needs to be referenced | ||
224 | */ | ||
225 | ssize_t uwb_rc_cmd(struct uwb_rc *rc, const char *cmd_name, | ||
226 | struct uwb_rccb *cmd, size_t cmd_size, | ||
227 | struct uwb_rceb *reply, size_t reply_size) | ||
228 | { | ||
229 | struct device *dev = &rc->uwb_dev.dev; | ||
230 | ssize_t result; | ||
231 | |||
232 | result = __uwb_rc_cmd(rc, cmd_name, | ||
233 | cmd, cmd_size, reply, reply_size, | ||
234 | reply->bEventType, reply->wEvent, NULL); | ||
235 | |||
236 | if (result > 0 && result < reply_size) { | ||
237 | dev_err(dev, "%s: not enough data returned for decoding reply " | ||
238 | "(%zu bytes received vs at least %zu needed)\n", | ||
239 | cmd_name, result, reply_size); | ||
240 | result = -EIO; | ||
241 | } | ||
242 | return result; | ||
243 | } | ||
244 | EXPORT_SYMBOL_GPL(uwb_rc_cmd); | ||
245 | |||
246 | |||
247 | /** | ||
248 | * Generic function for issuing commands to the Radio Control | ||
249 | * Interface that return an unknown amount of data | ||
250 | * | ||
251 | * @rc: UWB Radio Control descriptor | ||
252 | * @cmd_name: Name of the command being issued (for error messages) | ||
253 | * @cmd: Pointer to rccb structure containing the command; | ||
254 | * normally you embed this structure as the first member of | ||
255 | * the full command structure. | ||
256 | * @cmd_size: Size of the whole command buffer pointed to by @cmd. | ||
257 | * @expected_type: Expected type in the return event | ||
258 | * @expected_event: Expected event code in the return event | ||
259 | * @preply: Here a pointer to where the event data is received will | ||
260 | * be stored. Once done with the data, free with kfree(). | ||
261 | * | ||
262 | * The function checks that the length returned in the reply is at | ||
263 | * least as big as a 'struct uwb_rceb *'; if not, it will be deemed an | ||
264 | * error and -EIO returned. | ||
265 | * | ||
266 | * @rc needs to be referenced | ||
267 | */ | ||
268 | ssize_t uwb_rc_vcmd(struct uwb_rc *rc, const char *cmd_name, | ||
269 | struct uwb_rccb *cmd, size_t cmd_size, | ||
270 | u8 expected_type, u16 expected_event, | ||
271 | struct uwb_rceb **preply) | ||
272 | { | ||
273 | return __uwb_rc_cmd(rc, cmd_name, cmd, cmd_size, NULL, 0, | ||
274 | expected_type, expected_event, preply); | ||
275 | } | ||
276 | EXPORT_SYMBOL_GPL(uwb_rc_vcmd); | ||
277 | |||
278 | |||
279 | /** | ||
280 | * Reset a UWB Host Controller (and all radio settings) | ||
281 | * | ||
282 | * @rc: Host Controller descriptor | ||
283 | * @returns: 0 if ok, < 0 errno code on error | ||
284 | * | ||
285 | * We put the command on kmalloc'ed memory as some arches cannot do | ||
286 | * USB from the stack. The reply event is copied from an stage buffer, | ||
287 | * so it can be in the stack. See WUSB1.0[8.6.2.4] for more details. | ||
288 | */ | ||
289 | int uwb_rc_reset(struct uwb_rc *rc) | ||
290 | { | ||
291 | int result = -ENOMEM; | ||
292 | struct uwb_rc_evt_confirm reply; | ||
293 | struct uwb_rccb *cmd; | ||
294 | size_t cmd_size = sizeof(*cmd); | ||
295 | |||
296 | mutex_lock(&rc->uwb_dev.mutex); | ||
297 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); | ||
298 | if (cmd == NULL) | ||
299 | goto error_kzalloc; | ||
300 | cmd->bCommandType = UWB_RC_CET_GENERAL; | ||
301 | cmd->wCommand = cpu_to_le16(UWB_RC_CMD_RESET); | ||
302 | reply.rceb.bEventType = UWB_RC_CET_GENERAL; | ||
303 | reply.rceb.wEvent = UWB_RC_CMD_RESET; | ||
304 | result = uwb_rc_cmd(rc, "RESET", cmd, cmd_size, | ||
305 | &reply.rceb, sizeof(reply)); | ||
306 | if (result < 0) | ||
307 | goto error_cmd; | ||
308 | if (reply.bResultCode != UWB_RC_RES_SUCCESS) { | ||
309 | dev_err(&rc->uwb_dev.dev, | ||
310 | "RESET: command execution failed: %s (%d)\n", | ||
311 | uwb_rc_strerror(reply.bResultCode), reply.bResultCode); | ||
312 | result = -EIO; | ||
313 | } | ||
314 | error_cmd: | ||
315 | kfree(cmd); | ||
316 | error_kzalloc: | ||
317 | mutex_unlock(&rc->uwb_dev.mutex); | ||
318 | return result; | ||
319 | } | ||
320 | |||
321 | int uwbd_msg_handle_reset(struct uwb_event *evt) | ||
322 | { | ||
323 | struct uwb_rc *rc = evt->rc; | ||
324 | int ret; | ||
325 | |||
326 | /* Need to prevent the RC hardware module going away while in | ||
327 | the rc->reset() call. */ | ||
328 | if (!try_module_get(rc->owner)) | ||
329 | return 0; | ||
330 | |||
331 | dev_info(&rc->uwb_dev.dev, "resetting radio controller\n"); | ||
332 | ret = rc->reset(rc); | ||
333 | if (ret) | ||
334 | dev_err(&rc->uwb_dev.dev, "failed to reset hardware: %d\n", ret); | ||
335 | |||
336 | module_put(rc->owner); | ||
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); | ||