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