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