Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
est.c
Go to the documentation of this file.
1 /*
2  * Ultra Wide Band Radio Control
3  * Event Size Tables management
4  *
5  * Copyright (C) 2005-2006 Intel Corporation
6  * Inaky Perez-Gonzalez <[email protected]>
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 #include <linux/slab.h>
44 #include <linux/export.h>
45 
46 #include "uwb-internal.h"
47 
48 struct uwb_est {
52  const struct uwb_est_entry *entry;
53 };
54 
55 static struct uwb_est *uwb_est;
56 static u8 uwb_est_size;
57 static u8 uwb_est_used;
58 static DEFINE_RWLOCK(uwb_est_lock);
59 
65 static
66 struct uwb_est_entry uwb_est_00_00xx[] = {
67  [UWB_RC_EVT_IE_RCV] = {
68  .size = sizeof(struct uwb_rc_evt_ie_rcv),
70  },
71  [UWB_RC_EVT_BEACON] = {
72  .size = sizeof(struct uwb_rc_evt_beacon),
74  },
76  .size = sizeof(struct uwb_rc_evt_beacon_size),
77  },
79  .size = sizeof(struct uwb_rc_evt_bpoie_change),
81  wBPOIELength),
82  },
84  .size = sizeof(struct uwb_rc_evt_bp_slot_change),
85  },
87  .size = sizeof(struct uwb_rc_evt_bp_switch_ie_rcv),
89  },
91  .size = sizeof(struct uwb_rc_evt_dev_addr_conflict),
92  },
94  .size = sizeof(struct uwb_rc_evt_drp_avail)
95  },
96  [UWB_RC_EVT_DRP] = {
97  .size = sizeof(struct uwb_rc_evt_drp),
98  .offset = 1 + offsetof(struct uwb_rc_evt_drp, ie_length),
99  },
101  .size = sizeof(struct uwb_rc_evt_bp_switch_status),
102  },
104  .size = sizeof(struct uwb_rc_evt_cmd_frame_rcv),
106  },
108  .size = sizeof(struct uwb_rc_evt_channel_change_ie_rcv),
110  },
112  .size = sizeof(struct uwb_rc_evt_confirm),
113  },
115  .size = sizeof(struct uwb_rc_evt_dev_addr_mgmt) },
116  [UWB_RC_CMD_GET_IE] = {
117  .size = sizeof(struct uwb_rc_evt_get_ie),
118  .offset = 1 + offsetof(struct uwb_rc_evt_get_ie, wIELength),
119  },
120  [UWB_RC_CMD_RESET] = {
121  .size = sizeof(struct uwb_rc_evt_confirm),
122  },
123  [UWB_RC_CMD_SCAN] = {
124  .size = sizeof(struct uwb_rc_evt_confirm),
125  },
127  .size = sizeof(struct uwb_rc_evt_confirm),
128  },
130  .size = sizeof(struct uwb_rc_evt_set_drp_ie),
131  },
132  [UWB_RC_CMD_SET_IE] = {
133  .size = sizeof(struct uwb_rc_evt_set_ie),
134  },
136  .size = sizeof(struct uwb_rc_evt_confirm),
137  },
139  .size = sizeof(struct uwb_rc_evt_confirm),
140  },
141  [UWB_RC_CMD_SLEEP] = {
142  .size = sizeof(struct uwb_rc_evt_confirm),
143  },
145  .size = sizeof(struct uwb_rc_evt_confirm),
146  },
148  .size = sizeof(struct uwb_rc_evt_confirm),
149  },
150  [UWB_RC_CMD_BP_MERGE] = {
151  .size = sizeof(struct uwb_rc_evt_confirm),
152  },
154  .size = sizeof(struct uwb_rc_evt_confirm),
155  },
157  .size = sizeof(struct uwb_rc_evt_confirm),
158  },
159 };
160 
161 static
162 struct uwb_est_entry uwb_est_01_00xx[] = {
164  .size = sizeof(struct uwb_rc_evt_daa_energy_detected),
165  },
167  .size = sizeof(struct uwb_rc_evt_set_daa_energy_mask),
168  },
170  .size = sizeof(struct uwb_rc_evt_set_notification_filter_ex),
171  },
172 };
173 
181 int uwb_est_create(void)
182 {
183  int result;
184 
185  uwb_est_size = 2;
186  uwb_est_used = 0;
187  uwb_est = kcalloc(uwb_est_size, sizeof(uwb_est[0]), GFP_KERNEL);
188  if (uwb_est == NULL)
189  return -ENOMEM;
190 
191  result = uwb_est_register(UWB_RC_CET_GENERAL, 0, 0xffff, 0xffff,
192  uwb_est_00_00xx, ARRAY_SIZE(uwb_est_00_00xx));
193  if (result < 0)
194  goto out;
195  result = uwb_est_register(UWB_RC_CET_EX_TYPE_1, 0, 0xffff, 0xffff,
196  uwb_est_01_00xx, ARRAY_SIZE(uwb_est_01_00xx));
197 out:
198  return result;
199 }
200 
201 
203 void uwb_est_destroy(void)
204 {
205  kfree(uwb_est);
206  uwb_est = NULL;
207  uwb_est_size = uwb_est_used = 0;
208 }
209 
210 
216 static
217 int uwb_est_grow(void)
218 {
219  size_t actual_size = uwb_est_size * sizeof(uwb_est[0]);
220  void *new = kmalloc(2 * actual_size, GFP_ATOMIC);
221  if (new == NULL)
222  return -ENOMEM;
223  memcpy(new, uwb_est, actual_size);
224  memset(new + actual_size, 0, actual_size);
225  kfree(uwb_est);
226  uwb_est = new;
227  uwb_est_size *= 2;
228  return 0;
229 }
230 
231 
255 /* FIXME: add bus type to vendor/product code */
257  const struct uwb_est_entry *entry, size_t entries)
258 {
259  unsigned long flags;
260  unsigned itr;
261  u16 type_event_high;
262  int result = 0;
263 
264  write_lock_irqsave(&uwb_est_lock, flags);
265  if (uwb_est_used == uwb_est_size) {
266  result = uwb_est_grow();
267  if (result < 0)
268  goto out;
269  }
270  /* Find the right spot to insert it in */
271  type_event_high = type << 8 | event_high;
272  for (itr = 0; itr < uwb_est_used; itr++)
273  if (uwb_est[itr].type_event_high < type
274  && uwb_est[itr].vendor < vendor
275  && uwb_est[itr].product < product)
276  break;
277 
278  /* Shift others to make room for the new one? */
279  if (itr < uwb_est_used)
280  memmove(&uwb_est[itr+1], &uwb_est[itr], uwb_est_used - itr);
281  uwb_est[itr].type_event_high = type << 8 | event_high;
282  uwb_est[itr].vendor = vendor;
283  uwb_est[itr].product = product;
284  uwb_est[itr].entry = entry;
285  uwb_est[itr].entries = entries;
286  uwb_est_used++;
287 out:
288  write_unlock_irqrestore(&uwb_est_lock, flags);
289  return result;
290 }
292 
293 
308  const struct uwb_est_entry *entry, size_t entries)
309 {
310  unsigned long flags;
311  unsigned itr;
312  struct uwb_est est_cmp = {
313  .type_event_high = type << 8 | event_high,
314  .vendor = vendor,
315  .product = product,
316  .entry = entry,
317  .entries = entries
318  };
319  write_lock_irqsave(&uwb_est_lock, flags);
320  for (itr = 0; itr < uwb_est_used; itr++)
321  if (!memcmp(&uwb_est[itr], &est_cmp, sizeof(est_cmp)))
322  goto found;
323  write_unlock_irqrestore(&uwb_est_lock, flags);
324  return -ENOENT;
325 
326 found:
327  if (itr < uwb_est_used - 1) /* Not last one? move ones above */
328  memmove(&uwb_est[itr], &uwb_est[itr+1], uwb_est_used - itr - 1);
329  uwb_est_used--;
330  write_unlock_irqrestore(&uwb_est_lock, flags);
331  return 0;
332 }
334 
335 
353 static
354 ssize_t uwb_est_get_size(struct uwb_rc *uwb_rc, struct uwb_est *est,
355  u8 event_low, const struct uwb_rceb *rceb,
356  size_t rceb_size)
357 {
358  unsigned offset;
359  ssize_t size;
360  struct device *dev = &uwb_rc->uwb_dev.dev;
361  const struct uwb_est_entry *entry;
362 
363  size = -ENOENT;
364  if (event_low >= est->entries) { /* in range? */
365  dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: event %u out of range\n",
366  est, est->type_event_high, est->vendor, est->product,
367  est->entries, event_low);
368  goto out;
369  }
370  size = -ENOENT;
371  entry = &est->entry[event_low];
372  if (entry->size == 0 && entry->offset == 0) { /* unknown? */
373  dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: event %u unknown\n",
374  est, est->type_event_high, est->vendor, est->product,
375  est->entries, event_low);
376  goto out;
377  }
378  offset = entry->offset; /* extra fries with that? */
379  if (offset == 0)
380  size = entry->size;
381  else {
382  /* Ops, got an extra size field at 'offset'--read it */
383  const void *ptr = rceb;
384  size_t type_size = 0;
385  offset--;
386  size = -ENOSPC; /* enough data for more? */
387  switch (entry->type) {
388  case UWB_EST_16: type_size = sizeof(__le16); break;
389  case UWB_EST_8: type_size = sizeof(u8); break;
390  default: BUG();
391  }
392  if (offset + type_size > rceb_size) {
393  dev_err(dev, "EST %p 0x%04x/%04x/%04x[%u]: "
394  "not enough data to read extra size\n",
395  est, est->type_event_high, est->vendor,
396  est->product, est->entries);
397  goto out;
398  }
399  size = entry->size;
400  ptr += offset;
401  switch (entry->type) {
402  case UWB_EST_16: size += le16_to_cpu(*(__le16 *)ptr); break;
403  case UWB_EST_8: size += *(u8 *)ptr; break;
404  default: BUG();
405  }
406  }
407 out:
408  return size;
409 }
410 
411 
430 ssize_t uwb_est_find_size(struct uwb_rc *rc, const struct uwb_rceb *rceb,
431  size_t rceb_size)
432 {
433  /* FIXME: add vendor/product data */
434  ssize_t size;
435  struct device *dev = &rc->uwb_dev.dev;
436  unsigned long flags;
437  unsigned itr;
438  u16 type_event_high, event;
439  u8 *ptr = (u8 *) rceb;
440 
441  read_lock_irqsave(&uwb_est_lock, flags);
442  size = -ENOSPC;
443  if (rceb_size < sizeof(*rceb))
444  goto out;
445  event = le16_to_cpu(rceb->wEvent);
446  type_event_high = rceb->bEventType << 8 | (event & 0xff00) >> 8;
447  for (itr = 0; itr < uwb_est_used; itr++) {
448  if (uwb_est[itr].type_event_high != type_event_high)
449  continue;
450  size = uwb_est_get_size(rc, &uwb_est[itr],
451  event & 0x00ff, rceb, rceb_size);
452  /* try more tables that might handle the same type */
453  if (size != -ENOENT)
454  goto out;
455  }
456  dev_dbg(dev, "event 0x%02x/%04x/%02x: no handlers available; "
457  "RCEB %02x %02x %02x %02x\n",
458  (unsigned) rceb->bEventType,
459  (unsigned) le16_to_cpu(rceb->wEvent),
460  (unsigned) rceb->bEventContext,
461  ptr[0], ptr[1], ptr[2], ptr[3]);
462  size = -ENOENT;
463 out:
464  read_unlock_irqrestore(&uwb_est_lock, flags);
465  return size;
466 }