3 * Life cycle of radio controllers
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
25 * A UWB radio controller is also a UWB device, so it embeds one...
27 * List of RCs comes from the 'struct class uwb_rc_class'.
30 #include <linux/kernel.h>
31 #include <linux/string.h>
32 #include <linux/device.h>
33 #include <linux/err.h>
34 #include <linux/random.h>
35 #include <linux/kdev_t.h>
36 #include <linux/etherdevice.h>
37 #include <linux/usb.h>
40 #include <linux/uwb/debug.h>
41 #include "uwb-internal.h"
43 static int uwb_rc_index_match(struct device
*dev
, void *data
)
46 struct uwb_rc
*rc
= dev_get_drvdata(dev
);
48 if (rc
->index
== *index
)
53 static struct uwb_rc
*uwb_rc_find_by_index(int index
)
56 struct uwb_rc
*rc
= NULL
;
58 dev
= class_find_device(&uwb_rc_class
, NULL
, &index
, uwb_rc_index_match
);
60 rc
= dev_get_drvdata(dev
);
64 static int uwb_rc_new_index(void)
69 if (!uwb_rc_find_by_index(index
))
77 * Release the backing device of a uwb_rc that has been dynamically allocated.
79 static void uwb_rc_sys_release(struct device
*dev
)
81 struct uwb_dev
*uwb_dev
= container_of(dev
, struct uwb_dev
, dev
);
82 struct uwb_rc
*rc
= container_of(uwb_dev
, struct uwb_rc
, uwb_dev
);
84 uwb_rc_neh_destroy(rc
);
85 uwb_rc_ie_release(rc
);
86 d_printf(1, dev
, "freed uwb_rc %p\n", rc
);
91 void uwb_rc_init(struct uwb_rc
*rc
)
93 struct uwb_dev
*uwb_dev
= &rc
->uwb_dev
;
95 uwb_dev_init(uwb_dev
);
96 rc
->uwb_dev
.dev
.class = &uwb_rc_class
;
97 rc
->uwb_dev
.dev
.release
= uwb_rc_sys_release
;
98 uwb_rc_neh_create(rc
);
100 rc
->scan_type
= UWB_SCAN_DISABLED
;
101 INIT_LIST_HEAD(&rc
->notifs_chain
.list
);
102 mutex_init(&rc
->notifs_chain
.mutex
);
103 uwb_drp_avail_init(rc
);
108 EXPORT_SYMBOL_GPL(uwb_rc_init
);
111 struct uwb_rc
*uwb_rc_alloc(void)
114 rc
= kzalloc(sizeof(*rc
), GFP_KERNEL
);
120 EXPORT_SYMBOL_GPL(uwb_rc_alloc
);
122 static struct attribute
*rc_attrs
[] = {
123 &dev_attr_mac_address
.attr
,
125 &dev_attr_beacon
.attr
,
129 static struct attribute_group rc_attr_group
= {
134 * Registration of sysfs specific stuff
136 static int uwb_rc_sys_add(struct uwb_rc
*rc
)
138 return sysfs_create_group(&rc
->uwb_dev
.dev
.kobj
, &rc_attr_group
);
142 static void __uwb_rc_sys_rm(struct uwb_rc
*rc
)
144 sysfs_remove_group(&rc
->uwb_dev
.dev
.kobj
, &rc_attr_group
);
148 * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it
149 * @rc: the radio controller.
151 * If the EUI-48 address is 00:00:00:00:00:00 or FF:FF:FF:FF:FF:FF
152 * then a random locally administered EUI-48 is generated and set on
153 * the device. The probability of address collisions is sufficiently
154 * unlikely (1/2^40 = 9.1e-13) that they're not checked for.
157 int uwb_rc_mac_addr_setup(struct uwb_rc
*rc
)
160 struct device
*dev
= &rc
->uwb_dev
.dev
;
161 struct uwb_dev
*uwb_dev
= &rc
->uwb_dev
;
162 char devname
[UWB_ADDR_STRSIZE
];
163 struct uwb_mac_addr addr
;
165 result
= uwb_rc_mac_addr_get(rc
, &addr
);
167 dev_err(dev
, "cannot retrieve UWB EUI-48 address: %d\n", result
);
171 if (uwb_mac_addr_unset(&addr
) || uwb_mac_addr_bcast(&addr
)) {
172 addr
.data
[0] = 0x02; /* locally adminstered and unicast */
173 get_random_bytes(&addr
.data
[1], sizeof(addr
.data
)-1);
175 result
= uwb_rc_mac_addr_set(rc
, &addr
);
177 uwb_mac_addr_print(devname
, sizeof(devname
), &addr
);
178 dev_err(dev
, "cannot set EUI-48 address %s: %d\n",
183 uwb_dev
->mac_addr
= addr
;
189 static int uwb_rc_setup(struct uwb_rc
*rc
)
192 struct device
*dev
= &rc
->uwb_dev
.dev
;
194 result
= uwb_rc_reset(rc
);
196 dev_err(dev
, "cannot reset UWB radio: %d\n", result
);
199 result
= uwb_rc_mac_addr_setup(rc
);
201 dev_err(dev
, "cannot setup UWB MAC address: %d\n", result
);
204 result
= uwb_rc_dev_addr_assign(rc
);
206 dev_err(dev
, "cannot assign UWB DevAddr: %d\n", result
);
209 result
= uwb_rc_ie_setup(rc
);
211 dev_err(dev
, "cannot setup IE subsystem: %d\n", result
);
214 result
= uwb_rc_set_identification_ie(rc
);
216 dev_err(dev
, "cannot set Identification IE: %d\n",
218 goto error_set_id_ie
;
220 result
= uwb_rsv_setup(rc
);
222 dev_err(dev
, "cannot setup reservation subsystem: %d\n", result
);
223 goto error_rsv_setup
;
229 uwb_rc_ie_release(rc
);
237 * Register a new UWB radio controller
239 * Did you call uwb_rc_init() on your rc?
241 * We assume that this is being called with a > 0 refcount on
242 * it [through ops->{get|put}_device(). We'll take our own, though.
244 * @parent_dev is our real device, the one that provides the actual UWB device
246 int uwb_rc_add(struct uwb_rc
*rc
, struct device
*parent_dev
, void *priv
)
250 char macbuf
[UWB_ADDR_STRSIZE
], devbuf
[UWB_ADDR_STRSIZE
];
252 rc
->index
= uwb_rc_new_index();
254 dev
= &rc
->uwb_dev
.dev
;
255 dev_set_name(dev
, "uwb%d", rc
->index
);
259 result
= rc
->start(rc
);
263 result
= uwb_rc_setup(rc
);
265 dev_err(dev
, "cannot setup UWB radio controller: %d\n", result
);
269 result
= uwb_dev_add(&rc
->uwb_dev
, parent_dev
, rc
);
270 if (result
< 0 && result
!= -EADDRNOTAVAIL
)
273 result
= uwb_rc_sys_add(rc
);
275 dev_err(parent_dev
, "cannot register UWB radio controller "
276 "dev attributes: %d\n", result
);
280 uwb_mac_addr_print(macbuf
, sizeof(macbuf
), &rc
->uwb_dev
.mac_addr
);
281 uwb_dev_addr_print(devbuf
, sizeof(devbuf
), &rc
->uwb_dev
.dev_addr
);
283 "new uwb radio controller (mac %s dev %s) on %s %s\n",
284 macbuf
, devbuf
, parent_dev
->bus
->name
, dev_name(parent_dev
));
289 uwb_dev_rm(&rc
->uwb_dev
);
297 EXPORT_SYMBOL_GPL(uwb_rc_add
);
300 static int uwb_dev_offair_helper(struct device
*dev
, void *priv
)
302 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
304 return __uwb_dev_offair(uwb_dev
, uwb_dev
->rc
);
308 * Remove a Radio Controller; stop beaconing/scanning, disconnect all children
310 void uwb_rc_rm(struct uwb_rc
*rc
)
316 uwb_rc_ie_rm(rc
, UWB_IDENTIFICATION_IE
);
317 if (rc
->beaconing
>= 0)
318 uwb_rc_beacon(rc
, -1, 0);
319 if (rc
->scan_type
!= UWB_SCAN_DISABLED
)
320 uwb_rc_scan(rc
, rc
->scanning
, UWB_SCAN_DISABLED
, 0);
326 uwb_dev_lock(&rc
->uwb_dev
);
329 uwb_dev_unlock(&rc
->uwb_dev
);
330 mutex_lock(&uwb_beca
.mutex
);
331 uwb_dev_for_each(rc
, uwb_dev_offair_helper
, NULL
);
333 mutex_unlock(&uwb_beca
.mutex
);
334 uwb_dev_rm(&rc
->uwb_dev
);
336 EXPORT_SYMBOL_GPL(uwb_rc_rm
);
338 static int find_rc_try_get(struct device
*dev
, void *data
)
340 struct uwb_rc
*target_rc
= data
;
341 struct uwb_rc
*rc
= dev_get_drvdata(dev
);
347 if (rc
== target_rc
) {
357 * Given a radio controller descriptor, validate and refcount it
359 * @returns NULL if the rc does not exist or is quiescing; the ptr to
362 struct uwb_rc
*__uwb_rc_try_get(struct uwb_rc
*target_rc
)
365 struct uwb_rc
*rc
= NULL
;
367 dev
= class_find_device(&uwb_rc_class
, NULL
, target_rc
,
370 rc
= dev_get_drvdata(dev
);
375 EXPORT_SYMBOL_GPL(__uwb_rc_try_get
);
378 * RC get for external refcount acquirers...
380 * Increments the refcount of the device and it's backend modules
382 static inline struct uwb_rc
*uwb_rc_get(struct uwb_rc
*rc
)
386 uwb_dev_get(&rc
->uwb_dev
);
390 static int find_rc_grandpa(struct device
*dev
, void *data
)
392 struct device
*grandpa_dev
= data
;
393 struct uwb_rc
*rc
= dev_get_drvdata(dev
);
395 if (rc
->uwb_dev
.dev
.parent
->parent
== grandpa_dev
) {
403 * Locate and refcount a radio controller given a common grand-parent
405 * @grandpa_dev Pointer to the 'grandparent' device structure.
406 * @returns NULL If the rc does not exist or is quiescing; the ptr to
407 * it otherwise, properly referenced.
409 * The Radio Control interface (or the UWB Radio Controller) is always
410 * an interface of a device. The parent is the interface, the
411 * grandparent is the device that encapsulates the interface.
413 * There is no need to lock around as the "grandpa" would be
414 * refcounted by the target, and to remove the referemes, the
415 * uwb_rc_class->sem would have to be taken--we hold it, ergo we
418 struct uwb_rc
*uwb_rc_get_by_grandpa(const struct device
*grandpa_dev
)
421 struct uwb_rc
*rc
= NULL
;
423 dev
= class_find_device(&uwb_rc_class
, NULL
, (void *)grandpa_dev
,
426 rc
= dev_get_drvdata(dev
);
429 EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa
);
432 * Find a radio controller by device address
434 * @returns the pointer to the radio controller, properly referenced
436 static int find_rc_dev(struct device
*dev
, void *data
)
438 struct uwb_dev_addr
*addr
= data
;
439 struct uwb_rc
*rc
= dev_get_drvdata(dev
);
445 if (!uwb_dev_addr_cmp(&rc
->uwb_dev
.dev_addr
, addr
)) {
452 struct uwb_rc
*uwb_rc_get_by_dev(const struct uwb_dev_addr
*addr
)
455 struct uwb_rc
*rc
= NULL
;
457 dev
= class_find_device(&uwb_rc_class
, NULL
, (void *)addr
,
460 rc
= dev_get_drvdata(dev
);
464 EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev
);
467 * Drop a reference on a radio controller
469 * This is the version that should be done by entities external to the
470 * UWB Radio Control stack (ie: clients of the API).
472 void uwb_rc_put(struct uwb_rc
*rc
)
476 EXPORT_SYMBOL_GPL(uwb_rc_put
);
482 ssize_t
uwb_rc_print_IEs(struct uwb_rc
*uwb_rc
, char *buf
, size_t size
)
485 struct uwb_rc_evt_get_ie
*ie_info
;
486 struct uwb_buf_ctx ctx
;
488 result
= uwb_rc_get_ie(uwb_rc
, &ie_info
);
494 uwb_ie_for_each(&uwb_rc
->uwb_dev
, uwb_ie_dump_hex
, &ctx
,
495 ie_info
->IEData
, result
- sizeof(*ie_info
));