• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/uwb/
1
2
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>
12
13#include "uwb-internal.h"
14
15static int uwb_rc_index_match(struct device *dev, void *data)
16{
17	int *index = data;
18	struct uwb_rc *rc = dev_get_drvdata(dev);
19
20	if (rc->index == *index)
21		return 1;
22	return 0;
23}
24
25static struct uwb_rc *uwb_rc_find_by_index(int index)
26{
27	struct device *dev;
28	struct uwb_rc *rc = NULL;
29
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;
34}
35
36static int uwb_rc_new_index(void)
37{
38	int index = 0;
39
40	for (;;) {
41		if (!uwb_rc_find_by_index(index))
42			return index;
43		if (++index < 0)
44			index = 0;
45	}
46}
47
48/**
49 * Release the backing device of a uwb_rc that has been dynamically allocated.
50 */
51static void uwb_rc_sys_release(struct device *dev)
52{
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);
55
56	uwb_rc_ie_release(rc);
57	kfree(rc);
58}
59
60
61void uwb_rc_init(struct uwb_rc *rc)
62{
63	struct uwb_dev *uwb_dev = &rc->uwb_dev;
64
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);
79}
80EXPORT_SYMBOL_GPL(uwb_rc_init);
81
82
83struct uwb_rc *uwb_rc_alloc(void)
84{
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;
91}
92EXPORT_SYMBOL_GPL(uwb_rc_alloc);
93
94static struct attribute *rc_attrs[] = {
95		&dev_attr_mac_address.attr,
96		&dev_attr_scan.attr,
97		&dev_attr_beacon.attr,
98		NULL,
99};
100
101static struct attribute_group rc_attr_group = {
102	.attrs = rc_attrs,
103};
104
105/*
106 * Registration of sysfs specific stuff
107 */
108static int uwb_rc_sys_add(struct uwb_rc *rc)
109{
110	return sysfs_create_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
111}
112
113
114static void __uwb_rc_sys_rm(struct uwb_rc *rc)
115{
116	sysfs_remove_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
117}
118
119/**
120 * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it
121 * @rc:  the radio controller.
122 *
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.
127 */
128static
129int uwb_rc_mac_addr_setup(struct uwb_rc *rc)
130{
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;
136
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;
141	}
142
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);
146
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;
153		}
154	}
155	uwb_dev->mac_addr = addr;
156	return 0;
157}
158
159
160
161static int uwb_rc_setup(struct uwb_rc *rc)
162{
163	int result;
164	struct device *dev = &rc->uwb_dev.dev;
165
166	result = uwb_radio_setup(rc);
167	if (result < 0) {
168		dev_err(dev, "cannot setup UWB radio: %d\n", result);
169		goto error;
170	}
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;
175	}
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;
180	}
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;
185	}
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;
190	}
191	uwb_dbg_add_rc(rc);
192	return 0;
193
194error_rsv_setup:
195	uwb_rc_ie_release(rc);
196error_ie_setup:
197error:
198	return result;
199}
200
201
202/**
203 * Register a new UWB radio controller
204 *
205 * Did you call uwb_rc_init() on your rc?
206 *
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.
209 *
210 * @parent_dev is our real device, the one that provides the actual UWB device
211 */
212int uwb_rc_add(struct uwb_rc *rc, struct device *parent_dev, void *priv)
213{
214	int result;
215	struct device *dev;
216	char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
217
218	rc->index = uwb_rc_new_index();
219
220	dev = &rc->uwb_dev.dev;
221	dev_set_name(dev, "uwb%d", rc->index);
222
223	rc->priv = priv;
224
225	init_waitqueue_head(&rc->uwbd.wq);
226	INIT_LIST_HEAD(&rc->uwbd.event_list);
227	spin_lock_init(&rc->uwbd.event_list_lock);
228
229	uwbd_start(rc);
230
231	result = rc->start(rc);
232	if (result < 0)
233		goto error_rc_start;
234
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;
239	}
240
241	result = uwb_dev_add(&rc->uwb_dev, parent_dev, rc);
242	if (result < 0 && result != -EADDRNOTAVAIL)
243		goto error_dev_add;
244
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;
250	}
251
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;
259
260error_sys_add:
261	uwb_dev_rm(&rc->uwb_dev);
262error_dev_add:
263error_rc_setup:
264	rc->stop(rc);
265error_rc_start:
266	uwbd_stop(rc);
267	return result;
268}
269EXPORT_SYMBOL_GPL(uwb_rc_add);
270
271
272static int uwb_dev_offair_helper(struct device *dev, void *priv)
273{
274	struct uwb_dev *uwb_dev = to_uwb_dev(dev);
275
276	return __uwb_dev_offair(uwb_dev, uwb_dev->rc);
277}
278
279/*
280 * Remove a Radio Controller; stop beaconing/scanning, disconnect all children
281 */
282void uwb_rc_rm(struct uwb_rc *rc)
283{
284	rc->ready = 0;
285
286	uwb_dbg_del_rc(rc);
287	uwb_rsv_remove_all(rc);
288	uwb_radio_shutdown(rc);
289
290	rc->stop(rc);
291
292	uwbd_stop(rc);
293	uwb_rc_neh_destroy(rc);
294
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);
306}
307EXPORT_SYMBOL_GPL(uwb_rc_rm);
308
309static int find_rc_try_get(struct device *dev, void *data)
310{
311	struct uwb_rc *target_rc = data;
312	struct uwb_rc *rc = dev_get_drvdata(dev);
313
314	if (rc == NULL) {
315		WARN_ON(1);
316		return 0;
317	}
318	if (rc == target_rc) {
319		if (rc->ready == 0)
320			return 0;
321		else
322			return 1;
323	}
324	return 0;
325}
326
327/**
328 * Given a radio controller descriptor, validate and refcount it
329 *
330 * @returns NULL if the rc does not exist or is quiescing; the ptr to
331 *               it otherwise.
332 */
333struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *target_rc)
334{
335	struct device *dev;
336	struct uwb_rc *rc = NULL;
337
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);
343	}
344	return rc;
345}
346EXPORT_SYMBOL_GPL(__uwb_rc_try_get);
347
348/*
349 * RC get for external refcount acquirers...
350 *
351 * Increments the refcount of the device and it's backend modules
352 */
353static inline struct uwb_rc *uwb_rc_get(struct uwb_rc *rc)
354{
355	if (rc->ready == 0)
356		return NULL;
357	uwb_dev_get(&rc->uwb_dev);
358	return rc;
359}
360
361static int find_rc_grandpa(struct device *dev, void *data)
362{
363	struct device *grandpa_dev = data;
364	struct uwb_rc *rc = dev_get_drvdata(dev);
365
366	if (rc->uwb_dev.dev.parent->parent == grandpa_dev) {
367		rc = uwb_rc_get(rc);
368		return 1;
369	}
370	return 0;
371}
372
373/**
374 * Locate and refcount a radio controller given a common grand-parent
375 *
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.
379 *
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.
383 *
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.
388 */
389struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *grandpa_dev)
390{
391	struct device *dev;
392	struct uwb_rc *rc = NULL;
393
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;
399}
400EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa);
401
402/**
403 * Find a radio controller by device address
404 *
405 * @returns the pointer to the radio controller, properly referenced
406 */
407static int find_rc_dev(struct device *dev, void *data)
408{
409	struct uwb_dev_addr *addr = data;
410	struct uwb_rc *rc = dev_get_drvdata(dev);
411
412	if (rc == NULL) {
413		WARN_ON(1);
414		return 0;
415	}
416	if (!uwb_dev_addr_cmp(&rc->uwb_dev.dev_addr, addr)) {
417		rc = uwb_rc_get(rc);
418		return 1;
419	}
420	return 0;
421}
422
423struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *addr)
424{
425	struct device *dev;
426	struct uwb_rc *rc = NULL;
427
428	dev = class_find_device(&uwb_rc_class, NULL, (void *)addr,
429				find_rc_dev);
430	if (dev)
431		rc = dev_get_drvdata(dev);
432
433	return rc;
434}
435EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev);
436
437/**
438 * Drop a reference on a radio controller
439 *
440 * This is the version that should be done by entities external to the
441 * UWB Radio Control stack (ie: clients of the API).
442 */
443void uwb_rc_put(struct uwb_rc *rc)
444{
445	__uwb_rc_put(rc);
446}
447EXPORT_SYMBOL_GPL(uwb_rc_put);
448