3 * Life cycle of devices
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
26 #include <linux/kernel.h>
27 #include <linux/device.h>
28 #include <linux/err.h>
29 #include <linux/kdev_t.h>
30 #include <linux/random.h>
31 #include "uwb-internal.h"
34 #include <linux/uwb/debug.h>
37 /* We initialize addresses to 0xff (invalid, as it is bcast) */
38 static inline void uwb_dev_addr_init(struct uwb_dev_addr
*addr
)
40 memset(&addr
->data
, 0xff, sizeof(addr
->data
));
43 static inline void uwb_mac_addr_init(struct uwb_mac_addr
*addr
)
45 memset(&addr
->data
, 0xff, sizeof(addr
->data
));
48 /* @returns !0 if a device @addr is a broadcast address */
49 static inline int uwb_dev_addr_bcast(const struct uwb_dev_addr
*addr
)
51 static const struct uwb_dev_addr bcast
= { .data
= { 0xff, 0xff } };
52 return !uwb_dev_addr_cmp(addr
, &bcast
);
56 * Add callback @new to be called when an event occurs in @rc.
58 int uwb_notifs_register(struct uwb_rc
*rc
, struct uwb_notifs_handler
*new)
60 if (mutex_lock_interruptible(&rc
->notifs_chain
.mutex
))
62 list_add(&new->list_node
, &rc
->notifs_chain
.list
);
63 mutex_unlock(&rc
->notifs_chain
.mutex
);
66 EXPORT_SYMBOL_GPL(uwb_notifs_register
);
69 * Remove event handler (callback)
71 int uwb_notifs_deregister(struct uwb_rc
*rc
, struct uwb_notifs_handler
*entry
)
73 if (mutex_lock_interruptible(&rc
->notifs_chain
.mutex
))
75 list_del(&entry
->list_node
);
76 mutex_unlock(&rc
->notifs_chain
.mutex
);
79 EXPORT_SYMBOL_GPL(uwb_notifs_deregister
);
82 * Notify all event handlers of a given event on @rc
84 * We are called with a valid reference to the device, or NULL if the
85 * event is not for a particular event (e.g., a BG join event).
87 void uwb_notify(struct uwb_rc
*rc
, struct uwb_dev
*uwb_dev
, enum uwb_notifs event
)
89 struct uwb_notifs_handler
*handler
;
90 if (mutex_lock_interruptible(&rc
->notifs_chain
.mutex
))
92 if (!list_empty(&rc
->notifs_chain
.list
)) {
93 list_for_each_entry(handler
, &rc
->notifs_chain
.list
, list_node
) {
94 handler
->cb(handler
->data
, uwb_dev
, event
);
97 mutex_unlock(&rc
->notifs_chain
.mutex
);
101 * Release the backing device of a uwb_dev that has been dynamically allocated.
103 static void uwb_dev_sys_release(struct device
*dev
)
105 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
107 d_fnstart(4, NULL
, "(dev %p uwb_dev %p)\n", dev
, uwb_dev
);
108 uwb_bce_put(uwb_dev
->bce
);
109 d_printf(0, &uwb_dev
->dev
, "uwb_dev %p freed\n", uwb_dev
);
110 memset(uwb_dev
, 0x69, sizeof(*uwb_dev
));
112 d_fnend(4, NULL
, "(dev %p uwb_dev %p) = void\n", dev
, uwb_dev
);
116 * Initialize a UWB device instance
118 * Alloc, zero and call this function.
120 void uwb_dev_init(struct uwb_dev
*uwb_dev
)
122 mutex_init(&uwb_dev
->mutex
);
123 device_initialize(&uwb_dev
->dev
);
124 uwb_dev
->dev
.release
= uwb_dev_sys_release
;
125 uwb_dev_addr_init(&uwb_dev
->dev_addr
);
126 uwb_mac_addr_init(&uwb_dev
->mac_addr
);
127 bitmap_fill(uwb_dev
->streams
, UWB_NUM_GLOBAL_STREAMS
);
130 static ssize_t
uwb_dev_EUI_48_show(struct device
*dev
,
131 struct device_attribute
*attr
, char *buf
)
133 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
134 char addr
[UWB_ADDR_STRSIZE
];
136 uwb_mac_addr_print(addr
, sizeof(addr
), &uwb_dev
->mac_addr
);
137 return sprintf(buf
, "%s\n", addr
);
139 static DEVICE_ATTR(EUI_48
, S_IRUGO
, uwb_dev_EUI_48_show
, NULL
);
141 static ssize_t
uwb_dev_DevAddr_show(struct device
*dev
,
142 struct device_attribute
*attr
, char *buf
)
144 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
145 char addr
[UWB_ADDR_STRSIZE
];
147 uwb_dev_addr_print(addr
, sizeof(addr
), &uwb_dev
->dev_addr
);
148 return sprintf(buf
, "%s\n", addr
);
150 static DEVICE_ATTR(DevAddr
, S_IRUGO
, uwb_dev_DevAddr_show
, NULL
);
153 * Show the BPST of this device.
155 * Calculated from the receive time of the device's beacon and it's
158 static ssize_t
uwb_dev_BPST_show(struct device
*dev
,
159 struct device_attribute
*attr
, char *buf
)
161 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
162 struct uwb_beca_e
*bce
;
163 struct uwb_beacon_frame
*bf
;
167 mutex_lock(&bce
->mutex
);
168 bf
= (struct uwb_beacon_frame
*)bce
->be
->BeaconInfo
;
169 bpst
= bce
->be
->wBPSTOffset
170 - (u16
)(bf
->Beacon_Slot_Number
* UWB_BEACON_SLOT_LENGTH_US
);
171 mutex_unlock(&bce
->mutex
);
173 return sprintf(buf
, "%d\n", bpst
);
175 static DEVICE_ATTR(BPST
, S_IRUGO
, uwb_dev_BPST_show
, NULL
);
178 * Show the IEs a device is beaconing
180 * We need to access the beacon cache, so we just lock it really
181 * quick, print the IEs and unlock.
183 * We have a reference on the cache entry, so that should be
186 static ssize_t
uwb_dev_IEs_show(struct device
*dev
,
187 struct device_attribute
*attr
, char *buf
)
189 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
191 return uwb_bce_print_IEs(uwb_dev
, uwb_dev
->bce
, buf
, PAGE_SIZE
);
193 static DEVICE_ATTR(IEs
, S_IRUGO
| S_IWUSR
, uwb_dev_IEs_show
, NULL
);
195 static ssize_t
uwb_dev_LQE_show(struct device
*dev
,
196 struct device_attribute
*attr
, char *buf
)
198 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
199 struct uwb_beca_e
*bce
= uwb_dev
->bce
;
202 mutex_lock(&bce
->mutex
);
203 result
= stats_show(&uwb_dev
->bce
->lqe_stats
, buf
);
204 mutex_unlock(&bce
->mutex
);
208 static ssize_t
uwb_dev_LQE_store(struct device
*dev
,
209 struct device_attribute
*attr
,
210 const char *buf
, size_t size
)
212 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
213 struct uwb_beca_e
*bce
= uwb_dev
->bce
;
216 mutex_lock(&bce
->mutex
);
217 result
= stats_store(&uwb_dev
->bce
->lqe_stats
, buf
, size
);
218 mutex_unlock(&bce
->mutex
);
221 static DEVICE_ATTR(LQE
, S_IRUGO
| S_IWUSR
, uwb_dev_LQE_show
, uwb_dev_LQE_store
);
223 static ssize_t
uwb_dev_RSSI_show(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
226 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
227 struct uwb_beca_e
*bce
= uwb_dev
->bce
;
230 mutex_lock(&bce
->mutex
);
231 result
= stats_show(&uwb_dev
->bce
->rssi_stats
, buf
);
232 mutex_unlock(&bce
->mutex
);
236 static ssize_t
uwb_dev_RSSI_store(struct device
*dev
,
237 struct device_attribute
*attr
,
238 const char *buf
, size_t size
)
240 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
241 struct uwb_beca_e
*bce
= uwb_dev
->bce
;
244 mutex_lock(&bce
->mutex
);
245 result
= stats_store(&uwb_dev
->bce
->rssi_stats
, buf
, size
);
246 mutex_unlock(&bce
->mutex
);
249 static DEVICE_ATTR(RSSI
, S_IRUGO
| S_IWUSR
, uwb_dev_RSSI_show
, uwb_dev_RSSI_store
);
252 static struct attribute
*dev_attrs
[] = {
253 &dev_attr_EUI_48
.attr
,
254 &dev_attr_DevAddr
.attr
,
262 static struct attribute_group dev_attr_group
= {
266 static struct attribute_group
*groups
[] = {
272 * Device SYSFS registration
276 static int __uwb_dev_sys_add(struct uwb_dev
*uwb_dev
, struct device
*parent_dev
)
281 d_fnstart(4, NULL
, "(uwb_dev %p parent_dev %p)\n", uwb_dev
, parent_dev
);
282 BUG_ON(parent_dev
== NULL
);
285 /* Device sysfs files are only useful for neighbor devices not
286 local radio controllers. */
287 if (&uwb_dev
->rc
->uwb_dev
!= uwb_dev
)
288 dev
->groups
= groups
;
289 dev
->parent
= parent_dev
;
290 dev_set_drvdata(dev
, uwb_dev
);
292 result
= device_add(dev
);
293 d_fnend(4, NULL
, "(uwb_dev %p parent_dev %p) = %d\n", uwb_dev
, parent_dev
, result
);
298 static void __uwb_dev_sys_rm(struct uwb_dev
*uwb_dev
)
300 d_fnstart(4, NULL
, "(uwb_dev %p)\n", uwb_dev
);
301 dev_set_drvdata(&uwb_dev
->dev
, NULL
);
302 device_del(&uwb_dev
->dev
);
303 d_fnend(4, NULL
, "(uwb_dev %p) = void\n", uwb_dev
);
308 * Register and initialize a new UWB device
310 * Did you call uwb_dev_init() on it?
312 * @parent_rc: is the parent radio controller who has the link to the
313 * device. When registering the UWB device that is a UWB
314 * Radio Controller, we point back to it.
316 * If registering the device that is part of a radio, caller has set
317 * rc->uwb_dev->dev. Otherwise it is to be left NULL--a new one will
320 int uwb_dev_add(struct uwb_dev
*uwb_dev
, struct device
*parent_dev
,
321 struct uwb_rc
*parent_rc
)
326 BUG_ON(uwb_dev
== NULL
);
327 BUG_ON(parent_dev
== NULL
);
328 BUG_ON(parent_rc
== NULL
);
330 mutex_lock(&uwb_dev
->mutex
);
332 uwb_dev
->rc
= parent_rc
;
333 result
= __uwb_dev_sys_add(uwb_dev
, parent_dev
);
335 printk(KERN_ERR
"UWB: unable to register dev %s with sysfs: %d\n",
336 dev_name(dev
), result
);
337 mutex_unlock(&uwb_dev
->mutex
);
342 void uwb_dev_rm(struct uwb_dev
*uwb_dev
)
344 mutex_lock(&uwb_dev
->mutex
);
345 __uwb_dev_sys_rm(uwb_dev
);
346 mutex_unlock(&uwb_dev
->mutex
);
351 int __uwb_dev_try_get(struct device
*dev
, void *__target_uwb_dev
)
353 struct uwb_dev
*target_uwb_dev
= __target_uwb_dev
;
354 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
355 if (uwb_dev
== target_uwb_dev
) {
356 uwb_dev_get(uwb_dev
);
364 * Given a UWB device descriptor, validate and refcount it
366 * @returns NULL if the device does not exist or is quiescing; the ptr to
369 struct uwb_dev
*uwb_dev_try_get(struct uwb_rc
*rc
, struct uwb_dev
*uwb_dev
)
371 if (uwb_dev_for_each(rc
, __uwb_dev_try_get
, uwb_dev
))
376 EXPORT_SYMBOL_GPL(uwb_dev_try_get
);
380 * Remove a device from the system [grunt for other functions]
382 int __uwb_dev_offair(struct uwb_dev
*uwb_dev
, struct uwb_rc
*rc
)
384 struct device
*dev
= &uwb_dev
->dev
;
385 char macbuf
[UWB_ADDR_STRSIZE
], devbuf
[UWB_ADDR_STRSIZE
];
387 d_fnstart(3, NULL
, "(dev %p [uwb_dev %p], uwb_rc %p)\n", dev
, uwb_dev
, rc
);
388 uwb_mac_addr_print(macbuf
, sizeof(macbuf
), &uwb_dev
->mac_addr
);
389 uwb_dev_addr_print(devbuf
, sizeof(devbuf
), &uwb_dev
->dev_addr
);
390 dev_info(dev
, "uwb device (mac %s dev %s) disconnected from %s %s\n",
392 rc
? rc
->uwb_dev
.dev
.parent
->bus
->name
: "n/a",
393 rc
? dev_name(rc
->uwb_dev
.dev
.parent
) : "");
395 uwb_dev_put(uwb_dev
); /* for the creation in _onair() */
396 d_fnend(3, NULL
, "(dev %p [uwb_dev %p], uwb_rc %p) = 0\n", dev
, uwb_dev
, rc
);
402 * A device went off the air, clean up after it!
404 * This is called by the UWB Daemon (through the beacon purge function
405 * uwb_bcn_cache_purge) when it is detected that a device has been in
406 * radio silence for a while.
408 * If this device is actually a local radio controller we don't need
409 * to go through the offair process, as it is not registered as that.
411 * NOTE: uwb_bcn_cache.mutex is held!
413 void uwbd_dev_offair(struct uwb_beca_e
*bce
)
415 struct uwb_dev
*uwb_dev
;
417 uwb_dev
= bce
->uwb_dev
;
419 uwb_notify(uwb_dev
->rc
, uwb_dev
, UWB_NOTIF_OFFAIR
);
420 __uwb_dev_offair(uwb_dev
, uwb_dev
->rc
);
426 * A device went on the air, start it up!
428 * This is called by the UWB Daemon when it is detected that a device
429 * has popped up in the radio range of the radio controller.
431 * It will just create the freaking device, register the beacon and
432 * stuff and yatla, done.
435 * NOTE: uwb_beca.mutex is held, bce->mutex is held
437 void uwbd_dev_onair(struct uwb_rc
*rc
, struct uwb_beca_e
*bce
)
440 struct device
*dev
= &rc
->uwb_dev
.dev
;
441 struct uwb_dev
*uwb_dev
;
442 char macbuf
[UWB_ADDR_STRSIZE
], devbuf
[UWB_ADDR_STRSIZE
];
444 uwb_mac_addr_print(macbuf
, sizeof(macbuf
), bce
->mac_addr
);
445 uwb_dev_addr_print(devbuf
, sizeof(devbuf
), &bce
->dev_addr
);
446 uwb_dev
= kzalloc(sizeof(struct uwb_dev
), GFP_KERNEL
);
447 if (uwb_dev
== NULL
) {
448 dev_err(dev
, "new device %s: Cannot allocate memory\n",
452 uwb_dev_init(uwb_dev
); /* This sets refcnt to one, we own it */
453 uwb_dev
->mac_addr
= *bce
->mac_addr
;
454 uwb_dev
->dev_addr
= bce
->dev_addr
;
455 dev_set_name(&uwb_dev
->dev
, macbuf
);
456 result
= uwb_dev_add(uwb_dev
, &rc
->uwb_dev
.dev
, rc
);
458 dev_err(dev
, "new device %s: cannot instantiate device\n",
462 /* plug the beacon cache */
463 bce
->uwb_dev
= uwb_dev
;
465 uwb_bce_get(bce
); /* released in uwb_dev_sys_release() */
466 dev_info(dev
, "uwb device (mac %s dev %s) connected to %s %s\n",
467 macbuf
, devbuf
, rc
->uwb_dev
.dev
.parent
->bus
->name
,
468 dev_name(rc
->uwb_dev
.dev
.parent
));
469 uwb_notify(rc
, uwb_dev
, UWB_NOTIF_ONAIR
);
478 * Iterate over the list of UWB devices, calling a @function on each
480 * See docs for bus_for_each()....
482 * @rc: radio controller for the devices.
483 * @function: function to call.
484 * @priv: data to pass to @function.
485 * @returns: 0 if no invocation of function() returned a value
486 * different to zero. That value otherwise.
488 int uwb_dev_for_each(struct uwb_rc
*rc
, uwb_dev_for_each_f function
, void *priv
)
490 return device_for_each_child(&rc
->uwb_dev
.dev
, priv
, function
);
492 EXPORT_SYMBOL_GPL(uwb_dev_for_each
);