RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / uwb / lc-rc.c
blobe10d5002d97fff6993da5d77b893bd6fc3732b2f
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/device.h>
6 #include <linux/err.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)
17 int *index = data;
18 struct uwb_rc *rc = dev_get_drvdata(dev);
20 if (rc->index == *index)
21 return 1;
22 return 0;
25 static struct uwb_rc *uwb_rc_find_by_index(int index)
27 struct device *dev;
28 struct uwb_rc *rc = NULL;
30 dev = class_find_device(&uwb_rc_class, NULL, &index, uwb_rc_index_match);
31 if (dev)
32 rc = dev_get_drvdata(dev);
33 return rc;
36 static int uwb_rc_new_index(void)
38 int index = 0;
40 for (;;) {
41 if (!uwb_rc_find_by_index(index))
42 return index;
43 if (++index < 0)
44 index = 0;
48 /**
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);
57 kfree(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);
69 rc->beaconing = -1;
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);
76 uwb_rc_ie_init(rc);
77 uwb_rsv_init(rc);
78 uwb_rc_pal_init(rc);
80 EXPORT_SYMBOL_GPL(uwb_rc_init);
83 struct uwb_rc *uwb_rc_alloc(void)
85 struct uwb_rc *rc;
86 rc = kzalloc(sizeof(*rc), GFP_KERNEL);
87 if (rc == NULL)
88 return NULL;
89 uwb_rc_init(rc);
90 return rc;
92 EXPORT_SYMBOL_GPL(uwb_rc_alloc);
94 static struct attribute *rc_attrs[] = {
95 &dev_attr_mac_address.attr,
96 &dev_attr_scan.attr,
97 &dev_attr_beacon.attr,
98 NULL,
101 static struct attribute_group rc_attr_group = {
102 .attrs = rc_attrs,
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.
128 static
129 int uwb_rc_mac_addr_setup(struct uwb_rc *rc)
131 int result;
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);
138 if (result < 0) {
139 dev_err(dev, "cannot retrieve UWB EUI-48 address: %d\n", result);
140 return 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);
148 if (result < 0) {
149 uwb_mac_addr_print(devname, sizeof(devname), &addr);
150 dev_err(dev, "cannot set EUI-48 address %s: %d\n",
151 devname, result);
152 return result;
155 uwb_dev->mac_addr = addr;
156 return 0;
161 static int uwb_rc_setup(struct uwb_rc *rc)
163 int result;
164 struct device *dev = &rc->uwb_dev.dev;
166 result = uwb_radio_setup(rc);
167 if (result < 0) {
168 dev_err(dev, "cannot setup UWB radio: %d\n", result);
169 goto error;
171 result = uwb_rc_mac_addr_setup(rc);
172 if (result < 0) {
173 dev_err(dev, "cannot setup UWB MAC address: %d\n", result);
174 goto error;
176 result = uwb_rc_dev_addr_assign(rc);
177 if (result < 0) {
178 dev_err(dev, "cannot assign UWB DevAddr: %d\n", result);
179 goto error;
181 result = uwb_rc_ie_setup(rc);
182 if (result < 0) {
183 dev_err(dev, "cannot setup IE subsystem: %d\n", result);
184 goto error_ie_setup;
186 result = uwb_rsv_setup(rc);
187 if (result < 0) {
188 dev_err(dev, "cannot setup reservation subsystem: %d\n", result);
189 goto error_rsv_setup;
191 uwb_dbg_add_rc(rc);
192 return 0;
194 error_rsv_setup:
195 uwb_rc_ie_release(rc);
196 error_ie_setup:
197 error:
198 return result;
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)
214 int result;
215 struct device *dev;
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);
223 rc->priv = priv;
225 init_waitqueue_head(&rc->uwbd.wq);
226 INIT_LIST_HEAD(&rc->uwbd.event_list);
227 spin_lock_init(&rc->uwbd.event_list_lock);
229 uwbd_start(rc);
231 result = rc->start(rc);
232 if (result < 0)
233 goto error_rc_start;
235 result = uwb_rc_setup(rc);
236 if (result < 0) {
237 dev_err(dev, "cannot setup UWB radio controller: %d\n", result);
238 goto error_rc_setup;
241 result = uwb_dev_add(&rc->uwb_dev, parent_dev, rc);
242 if (result < 0 && result != -EADDRNOTAVAIL)
243 goto error_dev_add;
245 result = uwb_rc_sys_add(rc);
246 if (result < 0) {
247 dev_err(parent_dev, "cannot register UWB radio controller "
248 "dev attributes: %d\n", result);
249 goto error_sys_add;
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);
254 dev_info(dev,
255 "new uwb radio controller (mac %s dev %s) on %s %s\n",
256 macbuf, devbuf, parent_dev->bus->name, dev_name(parent_dev));
257 rc->ready = 1;
258 return 0;
260 error_sys_add:
261 uwb_dev_rm(&rc->uwb_dev);
262 error_dev_add:
263 error_rc_setup:
264 rc->stop(rc);
265 error_rc_start:
266 uwbd_stop(rc);
267 return result;
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)
284 rc->ready = 0;
286 uwb_dbg_del_rc(rc);
287 uwb_rsv_remove_all(rc);
288 uwb_radio_shutdown(rc);
290 rc->stop(rc);
292 uwbd_stop(rc);
293 uwb_rc_neh_destroy(rc);
295 uwb_dev_lock(&rc->uwb_dev);
296 rc->priv = NULL;
297 rc->cmd = NULL;
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);
301 __uwb_rc_sys_rm(rc);
302 mutex_unlock(&rc->uwb_beca.mutex);
303 uwb_rsv_cleanup(rc);
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);
314 if (rc == NULL) {
315 WARN_ON(1);
316 return 0;
318 if (rc == target_rc) {
319 if (rc->ready == 0)
320 return 0;
321 else
322 return 1;
324 return 0;
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
331 * it otherwise.
333 struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *target_rc)
335 struct device *dev;
336 struct uwb_rc *rc = NULL;
338 dev = class_find_device(&uwb_rc_class, NULL, target_rc,
339 find_rc_try_get);
340 if (dev) {
341 rc = dev_get_drvdata(dev);
342 __uwb_rc_get(rc);
344 return rc;
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)
355 if (rc->ready == 0)
356 return NULL;
357 uwb_dev_get(&rc->uwb_dev);
358 return rc;
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) {
367 rc = uwb_rc_get(rc);
368 return 1;
370 return 0;
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
387 * should be safe.
389 struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *grandpa_dev)
391 struct device *dev;
392 struct uwb_rc *rc = NULL;
394 dev = class_find_device(&uwb_rc_class, NULL, (void *)grandpa_dev,
395 find_rc_grandpa);
396 if (dev)
397 rc = dev_get_drvdata(dev);
398 return rc;
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);
412 if (rc == NULL) {
413 WARN_ON(1);
414 return 0;
416 if (!uwb_dev_addr_cmp(&rc->uwb_dev.dev_addr, addr)) {
417 rc = uwb_rc_get(rc);
418 return 1;
420 return 0;
423 struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *addr)
425 struct device *dev;
426 struct uwb_rc *rc = NULL;
428 dev = class_find_device(&uwb_rc_class, NULL, (void *)addr,
429 find_rc_dev);
430 if (dev)
431 rc = dev_get_drvdata(dev);
433 return rc;
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)
445 __uwb_rc_put(rc);
447 EXPORT_SYMBOL_GPL(uwb_rc_put);