3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/device.h>
7 #include <linux/random.h>
8 #include <linux/kdev_t.h>
9 #include <linux/etherdevice.h>
10 #include <linux/usb.h>
11 #include <linux/slab.h>
13 #include "uwb-internal.h"
15 static int uwb_rc_index_match(struct device
*dev
, void *data
)
18 struct uwb_rc
*rc
= dev_get_drvdata(dev
);
20 if (rc
->index
== *index
)
25 static struct uwb_rc
*uwb_rc_find_by_index(int index
)
28 struct uwb_rc
*rc
= NULL
;
30 dev
= class_find_device(&uwb_rc_class
, NULL
, &index
, uwb_rc_index_match
);
32 rc
= dev_get_drvdata(dev
);
36 static int uwb_rc_new_index(void)
41 if (!uwb_rc_find_by_index(index
))
49 * Release the backing device of a uwb_rc that has been dynamically allocated.
51 static void uwb_rc_sys_release(struct device
*dev
)
53 struct uwb_dev
*uwb_dev
= container_of(dev
, struct uwb_dev
, dev
);
54 struct uwb_rc
*rc
= container_of(uwb_dev
, struct uwb_rc
, uwb_dev
);
56 uwb_rc_ie_release(rc
);
61 void uwb_rc_init(struct uwb_rc
*rc
)
63 struct uwb_dev
*uwb_dev
= &rc
->uwb_dev
;
65 uwb_dev_init(uwb_dev
);
66 rc
->uwb_dev
.dev
.class = &uwb_rc_class
;
67 rc
->uwb_dev
.dev
.release
= uwb_rc_sys_release
;
68 uwb_rc_neh_create(rc
);
70 rc
->scan_type
= UWB_SCAN_DISABLED
;
71 INIT_LIST_HEAD(&rc
->notifs_chain
.list
);
72 mutex_init(&rc
->notifs_chain
.mutex
);
73 INIT_LIST_HEAD(&rc
->uwb_beca
.list
);
74 mutex_init(&rc
->uwb_beca
.mutex
);
75 uwb_drp_avail_init(rc
);
80 EXPORT_SYMBOL_GPL(uwb_rc_init
);
83 struct uwb_rc
*uwb_rc_alloc(void)
86 rc
= kzalloc(sizeof(*rc
), GFP_KERNEL
);
92 EXPORT_SYMBOL_GPL(uwb_rc_alloc
);
94 static struct attribute
*rc_attrs
[] = {
95 &dev_attr_mac_address
.attr
,
97 &dev_attr_beacon
.attr
,
101 static struct attribute_group rc_attr_group
= {
106 * Registration of sysfs specific stuff
108 static int uwb_rc_sys_add(struct uwb_rc
*rc
)
110 return sysfs_create_group(&rc
->uwb_dev
.dev
.kobj
, &rc_attr_group
);
114 static void __uwb_rc_sys_rm(struct uwb_rc
*rc
)
116 sysfs_remove_group(&rc
->uwb_dev
.dev
.kobj
, &rc_attr_group
);
120 * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it
121 * @rc: the radio controller.
123 * If the EUI-48 address is 00:00:00:00:00:00 or FF:FF:FF:FF:FF:FF
124 * then a random locally administered EUI-48 is generated and set on
125 * the device. The probability of address collisions is sufficiently
126 * unlikely (1/2^40 = 9.1e-13) that they're not checked for.
129 int uwb_rc_mac_addr_setup(struct uwb_rc
*rc
)
132 struct device
*dev
= &rc
->uwb_dev
.dev
;
133 struct uwb_dev
*uwb_dev
= &rc
->uwb_dev
;
134 char devname
[UWB_ADDR_STRSIZE
];
135 struct uwb_mac_addr addr
;
137 result
= uwb_rc_mac_addr_get(rc
, &addr
);
139 dev_err(dev
, "cannot retrieve UWB EUI-48 address: %d\n", result
);
143 if (uwb_mac_addr_unset(&addr
) || uwb_mac_addr_bcast(&addr
)) {
144 addr
.data
[0] = 0x02; /* locally adminstered and unicast */
145 get_random_bytes(&addr
.data
[1], sizeof(addr
.data
)-1);
147 result
= uwb_rc_mac_addr_set(rc
, &addr
);
149 uwb_mac_addr_print(devname
, sizeof(devname
), &addr
);
150 dev_err(dev
, "cannot set EUI-48 address %s: %d\n",
155 uwb_dev
->mac_addr
= addr
;
161 static int uwb_rc_setup(struct uwb_rc
*rc
)
164 struct device
*dev
= &rc
->uwb_dev
.dev
;
166 result
= uwb_radio_setup(rc
);
168 dev_err(dev
, "cannot setup UWB radio: %d\n", result
);
171 result
= uwb_rc_mac_addr_setup(rc
);
173 dev_err(dev
, "cannot setup UWB MAC address: %d\n", result
);
176 result
= uwb_rc_dev_addr_assign(rc
);
178 dev_err(dev
, "cannot assign UWB DevAddr: %d\n", result
);
181 result
= uwb_rc_ie_setup(rc
);
183 dev_err(dev
, "cannot setup IE subsystem: %d\n", result
);
186 result
= uwb_rsv_setup(rc
);
188 dev_err(dev
, "cannot setup reservation subsystem: %d\n", result
);
189 goto error_rsv_setup
;
195 uwb_rc_ie_release(rc
);
203 * Register a new UWB radio controller
205 * Did you call uwb_rc_init() on your rc?
207 * We assume that this is being called with a > 0 refcount on
208 * it [through ops->{get|put}_device(). We'll take our own, though.
210 * @parent_dev is our real device, the one that provides the actual UWB device
212 int uwb_rc_add(struct uwb_rc
*rc
, struct device
*parent_dev
, void *priv
)
216 char macbuf
[UWB_ADDR_STRSIZE
], devbuf
[UWB_ADDR_STRSIZE
];
218 rc
->index
= uwb_rc_new_index();
220 dev
= &rc
->uwb_dev
.dev
;
221 dev_set_name(dev
, "uwb%d", rc
->index
);
225 init_waitqueue_head(&rc
->uwbd
.wq
);
226 INIT_LIST_HEAD(&rc
->uwbd
.event_list
);
227 spin_lock_init(&rc
->uwbd
.event_list_lock
);
231 result
= rc
->start(rc
);
235 result
= uwb_rc_setup(rc
);
237 dev_err(dev
, "cannot setup UWB radio controller: %d\n", result
);
241 result
= uwb_dev_add(&rc
->uwb_dev
, parent_dev
, rc
);
242 if (result
< 0 && result
!= -EADDRNOTAVAIL
)
245 result
= uwb_rc_sys_add(rc
);
247 dev_err(parent_dev
, "cannot register UWB radio controller "
248 "dev attributes: %d\n", result
);
252 uwb_mac_addr_print(macbuf
, sizeof(macbuf
), &rc
->uwb_dev
.mac_addr
);
253 uwb_dev_addr_print(devbuf
, sizeof(devbuf
), &rc
->uwb_dev
.dev_addr
);
255 "new uwb radio controller (mac %s dev %s) on %s %s\n",
256 macbuf
, devbuf
, parent_dev
->bus
->name
, dev_name(parent_dev
));
261 uwb_dev_rm(&rc
->uwb_dev
);
269 EXPORT_SYMBOL_GPL(uwb_rc_add
);
272 static int uwb_dev_offair_helper(struct device
*dev
, void *priv
)
274 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
276 return __uwb_dev_offair(uwb_dev
, uwb_dev
->rc
);
280 * Remove a Radio Controller; stop beaconing/scanning, disconnect all children
282 void uwb_rc_rm(struct uwb_rc
*rc
)
287 uwb_rsv_remove_all(rc
);
288 uwb_radio_shutdown(rc
);
293 uwb_rc_neh_destroy(rc
);
295 uwb_dev_lock(&rc
->uwb_dev
);
298 uwb_dev_unlock(&rc
->uwb_dev
);
299 mutex_lock(&rc
->uwb_beca
.mutex
);
300 uwb_dev_for_each(rc
, uwb_dev_offair_helper
, NULL
);
302 mutex_unlock(&rc
->uwb_beca
.mutex
);
304 uwb_beca_release(rc
);
305 uwb_dev_rm(&rc
->uwb_dev
);
307 EXPORT_SYMBOL_GPL(uwb_rc_rm
);
309 static int find_rc_try_get(struct device
*dev
, void *data
)
311 struct uwb_rc
*target_rc
= data
;
312 struct uwb_rc
*rc
= dev_get_drvdata(dev
);
318 if (rc
== target_rc
) {
328 * Given a radio controller descriptor, validate and refcount it
330 * @returns NULL if the rc does not exist or is quiescing; the ptr to
333 struct uwb_rc
*__uwb_rc_try_get(struct uwb_rc
*target_rc
)
336 struct uwb_rc
*rc
= NULL
;
338 dev
= class_find_device(&uwb_rc_class
, NULL
, target_rc
,
341 rc
= dev_get_drvdata(dev
);
346 EXPORT_SYMBOL_GPL(__uwb_rc_try_get
);
349 * RC get for external refcount acquirers...
351 * Increments the refcount of the device and it's backend modules
353 static inline struct uwb_rc
*uwb_rc_get(struct uwb_rc
*rc
)
357 uwb_dev_get(&rc
->uwb_dev
);
361 static int find_rc_grandpa(struct device
*dev
, void *data
)
363 struct device
*grandpa_dev
= data
;
364 struct uwb_rc
*rc
= dev_get_drvdata(dev
);
366 if (rc
->uwb_dev
.dev
.parent
->parent
== grandpa_dev
) {
374 * Locate and refcount a radio controller given a common grand-parent
376 * @grandpa_dev Pointer to the 'grandparent' device structure.
377 * @returns NULL If the rc does not exist or is quiescing; the ptr to
378 * it otherwise, properly referenced.
380 * The Radio Control interface (or the UWB Radio Controller) is always
381 * an interface of a device. The parent is the interface, the
382 * grandparent is the device that encapsulates the interface.
384 * There is no need to lock around as the "grandpa" would be
385 * refcounted by the target, and to remove the referemes, the
386 * uwb_rc_class->sem would have to be taken--we hold it, ergo we
389 struct uwb_rc
*uwb_rc_get_by_grandpa(const struct device
*grandpa_dev
)
392 struct uwb_rc
*rc
= NULL
;
394 dev
= class_find_device(&uwb_rc_class
, NULL
, (void *)grandpa_dev
,
397 rc
= dev_get_drvdata(dev
);
400 EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa
);
403 * Find a radio controller by device address
405 * @returns the pointer to the radio controller, properly referenced
407 static int find_rc_dev(struct device
*dev
, void *data
)
409 struct uwb_dev_addr
*addr
= data
;
410 struct uwb_rc
*rc
= dev_get_drvdata(dev
);
416 if (!uwb_dev_addr_cmp(&rc
->uwb_dev
.dev_addr
, addr
)) {
423 struct uwb_rc
*uwb_rc_get_by_dev(const struct uwb_dev_addr
*addr
)
426 struct uwb_rc
*rc
= NULL
;
428 dev
= class_find_device(&uwb_rc_class
, NULL
, (void *)addr
,
431 rc
= dev_get_drvdata(dev
);
435 EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev
);
438 * Drop a reference on a radio controller
440 * This is the version that should be done by entities external to the
441 * UWB Radio Control stack (ie: clients of the API).
443 void uwb_rc_put(struct uwb_rc
*rc
)
447 EXPORT_SYMBOL_GPL(uwb_rc_put
);