treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / staging / uwb / lc-dev.c
blob3e5c07fd6b10ac8f35b1e393a8e54159042077cf
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Ultra Wide Band
4 * Life cycle of devices
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * FIXME: docs
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/device.h>
14 #include <linux/export.h>
15 #include <linux/err.h>
16 #include <linux/kdev_t.h>
17 #include <linux/random.h>
18 #include <linux/stat.h>
19 #include "uwb-internal.h"
21 /* We initialize addresses to 0xff (invalid, as it is bcast) */
22 static inline void uwb_dev_addr_init(struct uwb_dev_addr *addr)
24 memset(&addr->data, 0xff, sizeof(addr->data));
27 static inline void uwb_mac_addr_init(struct uwb_mac_addr *addr)
29 memset(&addr->data, 0xff, sizeof(addr->data));
33 * Add callback @new to be called when an event occurs in @rc.
35 int uwb_notifs_register(struct uwb_rc *rc, struct uwb_notifs_handler *new)
37 if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
38 return -ERESTARTSYS;
39 list_add(&new->list_node, &rc->notifs_chain.list);
40 mutex_unlock(&rc->notifs_chain.mutex);
41 return 0;
43 EXPORT_SYMBOL_GPL(uwb_notifs_register);
46 * Remove event handler (callback)
48 int uwb_notifs_deregister(struct uwb_rc *rc, struct uwb_notifs_handler *entry)
50 if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
51 return -ERESTARTSYS;
52 list_del(&entry->list_node);
53 mutex_unlock(&rc->notifs_chain.mutex);
54 return 0;
56 EXPORT_SYMBOL_GPL(uwb_notifs_deregister);
59 * Notify all event handlers of a given event on @rc
61 * We are called with a valid reference to the device, or NULL if the
62 * event is not for a particular event (e.g., a BG join event).
64 void uwb_notify(struct uwb_rc *rc, struct uwb_dev *uwb_dev, enum uwb_notifs event)
66 struct uwb_notifs_handler *handler;
67 if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
68 return;
69 if (!list_empty(&rc->notifs_chain.list)) {
70 list_for_each_entry(handler, &rc->notifs_chain.list, list_node) {
71 handler->cb(handler->data, uwb_dev, event);
74 mutex_unlock(&rc->notifs_chain.mutex);
78 * Release the backing device of a uwb_dev that has been dynamically allocated.
80 static void uwb_dev_sys_release(struct device *dev)
82 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
84 uwb_bce_put(uwb_dev->bce);
85 memset(uwb_dev, 0x69, sizeof(*uwb_dev));
86 kfree(uwb_dev);
90 * Initialize a UWB device instance
92 * Alloc, zero and call this function.
94 void uwb_dev_init(struct uwb_dev *uwb_dev)
96 mutex_init(&uwb_dev->mutex);
97 device_initialize(&uwb_dev->dev);
98 uwb_dev->dev.release = uwb_dev_sys_release;
99 uwb_dev_addr_init(&uwb_dev->dev_addr);
100 uwb_mac_addr_init(&uwb_dev->mac_addr);
101 bitmap_fill(uwb_dev->streams, UWB_NUM_GLOBAL_STREAMS);
104 static ssize_t uwb_dev_EUI_48_show(struct device *dev,
105 struct device_attribute *attr, char *buf)
107 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
108 char addr[UWB_ADDR_STRSIZE];
110 uwb_mac_addr_print(addr, sizeof(addr), &uwb_dev->mac_addr);
111 return sprintf(buf, "%s\n", addr);
113 static DEVICE_ATTR(EUI_48, S_IRUGO, uwb_dev_EUI_48_show, NULL);
115 static ssize_t uwb_dev_DevAddr_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
118 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
119 char addr[UWB_ADDR_STRSIZE];
121 uwb_dev_addr_print(addr, sizeof(addr), &uwb_dev->dev_addr);
122 return sprintf(buf, "%s\n", addr);
124 static DEVICE_ATTR(DevAddr, S_IRUGO, uwb_dev_DevAddr_show, NULL);
127 * Show the BPST of this device.
129 * Calculated from the receive time of the device's beacon and it's
130 * slot number.
132 static ssize_t uwb_dev_BPST_show(struct device *dev,
133 struct device_attribute *attr, char *buf)
135 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
136 struct uwb_beca_e *bce;
137 struct uwb_beacon_frame *bf;
138 u16 bpst;
140 bce = uwb_dev->bce;
141 mutex_lock(&bce->mutex);
142 bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo;
143 bpst = bce->be->wBPSTOffset
144 - (u16)(bf->Beacon_Slot_Number * UWB_BEACON_SLOT_LENGTH_US);
145 mutex_unlock(&bce->mutex);
147 return sprintf(buf, "%d\n", bpst);
149 static DEVICE_ATTR(BPST, S_IRUGO, uwb_dev_BPST_show, NULL);
152 * Show the IEs a device is beaconing
154 * We need to access the beacon cache, so we just lock it really
155 * quick, print the IEs and unlock.
157 * We have a reference on the cache entry, so that should be
158 * quite safe.
160 static ssize_t uwb_dev_IEs_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
163 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
165 return uwb_bce_print_IEs(uwb_dev, uwb_dev->bce, buf, PAGE_SIZE);
167 static DEVICE_ATTR(IEs, S_IRUGO | S_IWUSR, uwb_dev_IEs_show, NULL);
169 static ssize_t uwb_dev_LQE_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
173 struct uwb_beca_e *bce = uwb_dev->bce;
174 size_t result;
176 mutex_lock(&bce->mutex);
177 result = stats_show(&uwb_dev->bce->lqe_stats, buf);
178 mutex_unlock(&bce->mutex);
179 return result;
182 static ssize_t uwb_dev_LQE_store(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf, size_t size)
186 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
187 struct uwb_beca_e *bce = uwb_dev->bce;
188 ssize_t result;
190 mutex_lock(&bce->mutex);
191 result = stats_store(&uwb_dev->bce->lqe_stats, buf, size);
192 mutex_unlock(&bce->mutex);
193 return result;
195 static DEVICE_ATTR(LQE, S_IRUGO | S_IWUSR, uwb_dev_LQE_show, uwb_dev_LQE_store);
197 static ssize_t uwb_dev_RSSI_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
200 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
201 struct uwb_beca_e *bce = uwb_dev->bce;
202 size_t result;
204 mutex_lock(&bce->mutex);
205 result = stats_show(&uwb_dev->bce->rssi_stats, buf);
206 mutex_unlock(&bce->mutex);
207 return result;
210 static ssize_t uwb_dev_RSSI_store(struct device *dev,
211 struct device_attribute *attr,
212 const char *buf, size_t size)
214 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
215 struct uwb_beca_e *bce = uwb_dev->bce;
216 ssize_t result;
218 mutex_lock(&bce->mutex);
219 result = stats_store(&uwb_dev->bce->rssi_stats, buf, size);
220 mutex_unlock(&bce->mutex);
221 return result;
223 static DEVICE_ATTR(RSSI, S_IRUGO | S_IWUSR, uwb_dev_RSSI_show, uwb_dev_RSSI_store);
226 static struct attribute *uwb_dev_attrs[] = {
227 &dev_attr_EUI_48.attr,
228 &dev_attr_DevAddr.attr,
229 &dev_attr_BPST.attr,
230 &dev_attr_IEs.attr,
231 &dev_attr_LQE.attr,
232 &dev_attr_RSSI.attr,
233 NULL,
235 ATTRIBUTE_GROUPS(uwb_dev);
237 /* UWB bus type. */
238 struct bus_type uwb_bus_type = {
239 .name = "uwb",
240 .dev_groups = uwb_dev_groups,
244 * Device SYSFS registration
246 static int __uwb_dev_sys_add(struct uwb_dev *uwb_dev, struct device *parent_dev)
248 struct device *dev;
250 dev = &uwb_dev->dev;
251 dev->parent = parent_dev;
252 dev_set_drvdata(dev, uwb_dev);
254 return device_add(dev);
258 static void __uwb_dev_sys_rm(struct uwb_dev *uwb_dev)
260 dev_set_drvdata(&uwb_dev->dev, NULL);
261 device_del(&uwb_dev->dev);
266 * Register and initialize a new UWB device
268 * Did you call uwb_dev_init() on it?
270 * @parent_rc: is the parent radio controller who has the link to the
271 * device. When registering the UWB device that is a UWB
272 * Radio Controller, we point back to it.
274 * If registering the device that is part of a radio, caller has set
275 * rc->uwb_dev->dev. Otherwise it is to be left NULL--a new one will
276 * be allocated.
278 int uwb_dev_add(struct uwb_dev *uwb_dev, struct device *parent_dev,
279 struct uwb_rc *parent_rc)
281 int result;
282 struct device *dev;
284 BUG_ON(uwb_dev == NULL);
285 BUG_ON(parent_dev == NULL);
286 BUG_ON(parent_rc == NULL);
288 mutex_lock(&uwb_dev->mutex);
289 dev = &uwb_dev->dev;
290 uwb_dev->rc = parent_rc;
291 result = __uwb_dev_sys_add(uwb_dev, parent_dev);
292 if (result < 0)
293 printk(KERN_ERR "UWB: unable to register dev %s with sysfs: %d\n",
294 dev_name(dev), result);
295 mutex_unlock(&uwb_dev->mutex);
296 return result;
300 void uwb_dev_rm(struct uwb_dev *uwb_dev)
302 mutex_lock(&uwb_dev->mutex);
303 __uwb_dev_sys_rm(uwb_dev);
304 mutex_unlock(&uwb_dev->mutex);
308 static
309 int __uwb_dev_try_get(struct device *dev, void *__target_uwb_dev)
311 struct uwb_dev *target_uwb_dev = __target_uwb_dev;
312 struct uwb_dev *uwb_dev = to_uwb_dev(dev);
313 if (uwb_dev == target_uwb_dev) {
314 uwb_dev_get(uwb_dev);
315 return 1;
316 } else
317 return 0;
322 * Given a UWB device descriptor, validate and refcount it
324 * @returns NULL if the device does not exist or is quiescing; the ptr to
325 * it otherwise.
327 struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev)
329 if (uwb_dev_for_each(rc, __uwb_dev_try_get, uwb_dev))
330 return uwb_dev;
331 else
332 return NULL;
334 EXPORT_SYMBOL_GPL(uwb_dev_try_get);
338 * Remove a device from the system [grunt for other functions]
340 int __uwb_dev_offair(struct uwb_dev *uwb_dev, struct uwb_rc *rc)
342 struct device *dev = &uwb_dev->dev;
343 char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
345 uwb_mac_addr_print(macbuf, sizeof(macbuf), &uwb_dev->mac_addr);
346 uwb_dev_addr_print(devbuf, sizeof(devbuf), &uwb_dev->dev_addr);
347 dev_info(dev, "uwb device (mac %s dev %s) disconnected from %s %s\n",
348 macbuf, devbuf,
349 uwb_dev->dev.bus->name,
350 rc ? dev_name(&(rc->uwb_dev.dev)) : "");
351 uwb_dev_rm(uwb_dev);
352 list_del(&uwb_dev->bce->node);
353 uwb_bce_put(uwb_dev->bce);
354 uwb_dev_put(uwb_dev); /* for the creation in _onair() */
356 return 0;
361 * A device went off the air, clean up after it!
363 * This is called by the UWB Daemon (through the beacon purge function
364 * uwb_bcn_cache_purge) when it is detected that a device has been in
365 * radio silence for a while.
367 * If this device is actually a local radio controller we don't need
368 * to go through the offair process, as it is not registered as that.
370 * NOTE: uwb_bcn_cache.mutex is held!
372 void uwbd_dev_offair(struct uwb_beca_e *bce)
374 struct uwb_dev *uwb_dev;
376 uwb_dev = bce->uwb_dev;
377 if (uwb_dev) {
378 uwb_notify(uwb_dev->rc, uwb_dev, UWB_NOTIF_OFFAIR);
379 __uwb_dev_offair(uwb_dev, uwb_dev->rc);
385 * A device went on the air, start it up!
387 * This is called by the UWB Daemon when it is detected that a device
388 * has popped up in the radio range of the radio controller.
390 * It will just create the freaking device, register the beacon and
391 * stuff and yatla, done.
394 * NOTE: uwb_beca.mutex is held, bce->mutex is held
396 void uwbd_dev_onair(struct uwb_rc *rc, struct uwb_beca_e *bce)
398 int result;
399 struct device *dev = &rc->uwb_dev.dev;
400 struct uwb_dev *uwb_dev;
401 char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
403 uwb_mac_addr_print(macbuf, sizeof(macbuf), bce->mac_addr);
404 uwb_dev_addr_print(devbuf, sizeof(devbuf), &bce->dev_addr);
405 uwb_dev = kzalloc(sizeof(struct uwb_dev), GFP_KERNEL);
406 if (uwb_dev == NULL) {
407 dev_err(dev, "new device %s: Cannot allocate memory\n",
408 macbuf);
409 return;
411 uwb_dev_init(uwb_dev); /* This sets refcnt to one, we own it */
412 uwb_dev->dev.bus = &uwb_bus_type;
413 uwb_dev->mac_addr = *bce->mac_addr;
414 uwb_dev->dev_addr = bce->dev_addr;
415 dev_set_name(&uwb_dev->dev, "%s", macbuf);
417 /* plug the beacon cache */
418 bce->uwb_dev = uwb_dev;
419 uwb_dev->bce = bce;
420 uwb_bce_get(bce); /* released in uwb_dev_sys_release() */
422 result = uwb_dev_add(uwb_dev, &rc->uwb_dev.dev, rc);
423 if (result < 0) {
424 dev_err(dev, "new device %s: cannot instantiate device\n",
425 macbuf);
426 goto error_dev_add;
429 dev_info(dev, "uwb device (mac %s dev %s) connected to %s %s\n",
430 macbuf, devbuf, uwb_dev->dev.bus->name,
431 dev_name(&(rc->uwb_dev.dev)));
432 uwb_notify(rc, uwb_dev, UWB_NOTIF_ONAIR);
433 return;
435 error_dev_add:
436 bce->uwb_dev = NULL;
437 uwb_bce_put(bce);
438 kfree(uwb_dev);
439 return;
443 * Iterate over the list of UWB devices, calling a @function on each
445 * See docs for bus_for_each()....
447 * @rc: radio controller for the devices.
448 * @function: function to call.
449 * @priv: data to pass to @function.
450 * @returns: 0 if no invocation of function() returned a value
451 * different to zero. That value otherwise.
453 int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f function, void *priv)
455 return device_for_each_child(&rc->uwb_dev.dev, priv, function);
457 EXPORT_SYMBOL_GPL(uwb_dev_for_each);