1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/kernel.h>
26#include <linux/device.h>
27#include <linux/err.h>
28#include <linux/kdev_t.h>
29#include <linux/random.h>
30#include "uwb-internal.h"
31
32
33static inline void uwb_dev_addr_init(struct uwb_dev_addr *addr)
34{
35 memset(&addr->data, 0xff, sizeof(addr->data));
36}
37
38static inline void uwb_mac_addr_init(struct uwb_mac_addr *addr)
39{
40 memset(&addr->data, 0xff, sizeof(addr->data));
41}
42
43
44static inline int uwb_dev_addr_bcast(const struct uwb_dev_addr *addr)
45{
46 static const struct uwb_dev_addr bcast = { .data = { 0xff, 0xff } };
47 return !uwb_dev_addr_cmp(addr, &bcast);
48}
49
50
51
52
53int uwb_notifs_register(struct uwb_rc *rc, struct uwb_notifs_handler *new)
54{
55 if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
56 return -ERESTARTSYS;
57 list_add(&new->list_node, &rc->notifs_chain.list);
58 mutex_unlock(&rc->notifs_chain.mutex);
59 return 0;
60}
61EXPORT_SYMBOL_GPL(uwb_notifs_register);
62
63
64
65
66int uwb_notifs_deregister(struct uwb_rc *rc, struct uwb_notifs_handler *entry)
67{
68 if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
69 return -ERESTARTSYS;
70 list_del(&entry->list_node);
71 mutex_unlock(&rc->notifs_chain.mutex);
72 return 0;
73}
74EXPORT_SYMBOL_GPL(uwb_notifs_deregister);
75
76
77
78
79
80
81
82void uwb_notify(struct uwb_rc *rc, struct uwb_dev *uwb_dev, enum uwb_notifs event)
83{
84 struct uwb_notifs_handler *handler;
85 if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
86 return;
87 if (!list_empty(&rc->notifs_chain.list)) {
88 list_for_each_entry(handler, &rc->notifs_chain.list, list_node) {
89 handler->cb(handler->data, uwb_dev, event);
90 }
91 }
92 mutex_unlock(&rc->notifs_chain.mutex);
93}
94
95
96
97
98static void uwb_dev_sys_release(struct device *dev)
99{
100 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
101
102 uwb_bce_put(uwb_dev->bce);
103 memset(uwb_dev, 0x69, sizeof(*uwb_dev));
104 kfree(uwb_dev);
105}
106
107
108
109
110
111
112void uwb_dev_init(struct uwb_dev *uwb_dev)
113{
114 mutex_init(&uwb_dev->mutex);
115 device_initialize(&uwb_dev->dev);
116 uwb_dev->dev.release = uwb_dev_sys_release;
117 uwb_dev_addr_init(&uwb_dev->dev_addr);
118 uwb_mac_addr_init(&uwb_dev->mac_addr);
119 bitmap_fill(uwb_dev->streams, UWB_NUM_GLOBAL_STREAMS);
120}
121
122static ssize_t uwb_dev_EUI_48_show(struct device *dev,
123 struct device_attribute *attr, char *buf)
124{
125 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
126 char addr[UWB_ADDR_STRSIZE];
127
128 uwb_mac_addr_print(addr, sizeof(addr), &uwb_dev->mac_addr);
129 return sprintf(buf, "%s\n", addr);
130}
131static DEVICE_ATTR(EUI_48, S_IRUGO, uwb_dev_EUI_48_show, NULL);
132
133static ssize_t uwb_dev_DevAddr_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
135{
136 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
137 char addr[UWB_ADDR_STRSIZE];
138
139 uwb_dev_addr_print(addr, sizeof(addr), &uwb_dev->dev_addr);
140 return sprintf(buf, "%s\n", addr);
141}
142static DEVICE_ATTR(DevAddr, S_IRUGO, uwb_dev_DevAddr_show, NULL);
143
144
145
146
147
148
149
150static ssize_t uwb_dev_BPST_show(struct device *dev,
151 struct device_attribute *attr, char *buf)
152{
153 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
154 struct uwb_beca_e *bce;
155 struct uwb_beacon_frame *bf;
156 u16 bpst;
157
158 bce = uwb_dev->bce;
159 mutex_lock(&bce->mutex);
160 bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo;
161 bpst = bce->be->wBPSTOffset
162 - (u16)(bf->Beacon_Slot_Number * UWB_BEACON_SLOT_LENGTH_US);
163 mutex_unlock(&bce->mutex);
164
165 return sprintf(buf, "%d\n", bpst);
166}
167static DEVICE_ATTR(BPST, S_IRUGO, uwb_dev_BPST_show, NULL);
168
169
170
171
172
173
174
175
176
177
178static ssize_t uwb_dev_IEs_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
180{
181 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
182
183 return uwb_bce_print_IEs(uwb_dev, uwb_dev->bce, buf, PAGE_SIZE);
184}
185static DEVICE_ATTR(IEs, S_IRUGO | S_IWUSR, uwb_dev_IEs_show, NULL);
186
187static ssize_t uwb_dev_LQE_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
189{
190 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
191 struct uwb_beca_e *bce = uwb_dev->bce;
192 size_t result;
193
194 mutex_lock(&bce->mutex);
195 result = stats_show(&uwb_dev->bce->lqe_stats, buf);
196 mutex_unlock(&bce->mutex);
197 return result;
198}
199
200static ssize_t uwb_dev_LQE_store(struct device *dev,
201 struct device_attribute *attr,
202 const char *buf, size_t size)
203{
204 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
205 struct uwb_beca_e *bce = uwb_dev->bce;
206 ssize_t result;
207
208 mutex_lock(&bce->mutex);
209 result = stats_store(&uwb_dev->bce->lqe_stats, buf, size);
210 mutex_unlock(&bce->mutex);
211 return result;
212}
213static DEVICE_ATTR(LQE, S_IRUGO | S_IWUSR, uwb_dev_LQE_show, uwb_dev_LQE_store);
214
215static ssize_t uwb_dev_RSSI_show(struct device *dev,
216 struct device_attribute *attr, char *buf)
217{
218 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
219 struct uwb_beca_e *bce = uwb_dev->bce;
220 size_t result;
221
222 mutex_lock(&bce->mutex);
223 result = stats_show(&uwb_dev->bce->rssi_stats, buf);
224 mutex_unlock(&bce->mutex);
225 return result;
226}
227
228static ssize_t uwb_dev_RSSI_store(struct device *dev,
229 struct device_attribute *attr,
230 const char *buf, size_t size)
231{
232 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
233 struct uwb_beca_e *bce = uwb_dev->bce;
234 ssize_t result;
235
236 mutex_lock(&bce->mutex);
237 result = stats_store(&uwb_dev->bce->rssi_stats, buf, size);
238 mutex_unlock(&bce->mutex);
239 return result;
240}
241static DEVICE_ATTR(RSSI, S_IRUGO | S_IWUSR, uwb_dev_RSSI_show, uwb_dev_RSSI_store);
242
243
244static struct attribute *dev_attrs[] = {
245 &dev_attr_EUI_48.attr,
246 &dev_attr_DevAddr.attr,
247 &dev_attr_BPST.attr,
248 &dev_attr_IEs.attr,
249 &dev_attr_LQE.attr,
250 &dev_attr_RSSI.attr,
251 NULL,
252};
253
254static struct attribute_group dev_attr_group = {
255 .attrs = dev_attrs,
256};
257
258static struct attribute_group *groups[] = {
259 &dev_attr_group,
260 NULL,
261};
262
263
264
265
266
267
268static int __uwb_dev_sys_add(struct uwb_dev *uwb_dev, struct device *parent_dev)
269{
270 struct device *dev;
271
272 dev = &uwb_dev->dev;
273
274
275 if (&uwb_dev->rc->uwb_dev != uwb_dev)
276 dev->groups = groups;
277 dev->parent = parent_dev;
278 dev_set_drvdata(dev, uwb_dev);
279
280 return device_add(dev);
281}
282
283
284static void __uwb_dev_sys_rm(struct uwb_dev *uwb_dev)
285{
286 dev_set_drvdata(&uwb_dev->dev, NULL);
287 device_del(&uwb_dev->dev);
288}
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304int uwb_dev_add(struct uwb_dev *uwb_dev, struct device *parent_dev,
305 struct uwb_rc *parent_rc)
306{
307 int result;
308 struct device *dev;
309
310 BUG_ON(uwb_dev == NULL);
311 BUG_ON(parent_dev == NULL);
312 BUG_ON(parent_rc == NULL);
313
314 mutex_lock(&uwb_dev->mutex);
315 dev = &uwb_dev->dev;
316 uwb_dev->rc = parent_rc;
317 result = __uwb_dev_sys_add(uwb_dev, parent_dev);
318 if (result < 0)
319 printk(KERN_ERR "UWB: unable to register dev %s with sysfs: %d\n",
320 dev_name(dev), result);
321 mutex_unlock(&uwb_dev->mutex);
322 return result;
323}
324
325
326void uwb_dev_rm(struct uwb_dev *uwb_dev)
327{
328 mutex_lock(&uwb_dev->mutex);
329 __uwb_dev_sys_rm(uwb_dev);
330 mutex_unlock(&uwb_dev->mutex);
331}
332
333
334static
335int __uwb_dev_try_get(struct device *dev, void *__target_uwb_dev)
336{
337 struct uwb_dev *target_uwb_dev = __target_uwb_dev;
338 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
339 if (uwb_dev == target_uwb_dev) {
340 uwb_dev_get(uwb_dev);
341 return 1;
342 } else
343 return 0;
344}
345
346
347
348
349
350
351
352
353struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev)
354{
355 if (uwb_dev_for_each(rc, __uwb_dev_try_get, uwb_dev))
356 return uwb_dev;
357 else
358 return NULL;
359}
360EXPORT_SYMBOL_GPL(uwb_dev_try_get);
361
362
363
364
365
366int __uwb_dev_offair(struct uwb_dev *uwb_dev, struct uwb_rc *rc)
367{
368 struct device *dev = &uwb_dev->dev;
369 char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
370
371 uwb_mac_addr_print(macbuf, sizeof(macbuf), &uwb_dev->mac_addr);
372 uwb_dev_addr_print(devbuf, sizeof(devbuf), &uwb_dev->dev_addr);
373 dev_info(dev, "uwb device (mac %s dev %s) disconnected from %s %s\n",
374 macbuf, devbuf,
375 rc ? rc->uwb_dev.dev.parent->bus->name : "n/a",
376 rc ? dev_name(rc->uwb_dev.dev.parent) : "");
377 uwb_dev_rm(uwb_dev);
378 list_del(&uwb_dev->bce->node);
379 uwb_bce_put(uwb_dev->bce);
380 uwb_dev_put(uwb_dev);
381
382 return 0;
383}
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398void uwbd_dev_offair(struct uwb_beca_e *bce)
399{
400 struct uwb_dev *uwb_dev;
401
402 uwb_dev = bce->uwb_dev;
403 if (uwb_dev) {
404 uwb_notify(uwb_dev->rc, uwb_dev, UWB_NOTIF_OFFAIR);
405 __uwb_dev_offair(uwb_dev, uwb_dev->rc);
406 }
407}
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422void uwbd_dev_onair(struct uwb_rc *rc, struct uwb_beca_e *bce)
423{
424 int result;
425 struct device *dev = &rc->uwb_dev.dev;
426 struct uwb_dev *uwb_dev;
427 char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
428
429 uwb_mac_addr_print(macbuf, sizeof(macbuf), bce->mac_addr);
430 uwb_dev_addr_print(devbuf, sizeof(devbuf), &bce->dev_addr);
431 uwb_dev = kzalloc(sizeof(struct uwb_dev), GFP_KERNEL);
432 if (uwb_dev == NULL) {
433 dev_err(dev, "new device %s: Cannot allocate memory\n",
434 macbuf);
435 return;
436 }
437 uwb_dev_init(uwb_dev);
438 uwb_dev->mac_addr = *bce->mac_addr;
439 uwb_dev->dev_addr = bce->dev_addr;
440 dev_set_name(&uwb_dev->dev, macbuf);
441 result = uwb_dev_add(uwb_dev, &rc->uwb_dev.dev, rc);
442 if (result < 0) {
443 dev_err(dev, "new device %s: cannot instantiate device\n",
444 macbuf);
445 goto error_dev_add;
446 }
447
448 bce->uwb_dev = uwb_dev;
449 uwb_dev->bce = bce;
450 uwb_bce_get(bce);
451 dev_info(dev, "uwb device (mac %s dev %s) connected to %s %s\n",
452 macbuf, devbuf, rc->uwb_dev.dev.parent->bus->name,
453 dev_name(rc->uwb_dev.dev.parent));
454 uwb_notify(rc, uwb_dev, UWB_NOTIF_ONAIR);
455 return;
456
457error_dev_add:
458 kfree(uwb_dev);
459 return;
460}
461
462
463
464
465
466
467
468
469
470
471
472
473int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f function, void *priv)
474{
475 return device_for_each_child(&rc->uwb_dev.dev, priv, function);
476}
477EXPORT_SYMBOL_GPL(uwb_dev_for_each);
478