]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/uwb/lc-rc.c
Merge git://git.infradead.org/iommu-2.6
[linux-2.6-omap-h63xx.git] / drivers / uwb / lc-rc.c
1 /*
2  * Ultra Wide Band
3  * Life cycle of radio controllers
4  *
5  * Copyright (C) 2005-2006 Intel Corporation
6  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7  *
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.
11  *
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.
16  *
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
20  * 02110-1301, USA.
21  *
22  *
23  * FIXME: docs
24  *
25  * A UWB radio controller is also a UWB device, so it embeds one...
26  *
27  * List of RCs comes from the 'struct class uwb_rc_class'.
28  */
29
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>
38
39 #define D_LOCAL 1
40 #include <linux/uwb/debug.h>
41 #include "uwb-internal.h"
42
43 static int uwb_rc_index_match(struct device *dev, void *data)
44 {
45         int *index = data;
46         struct uwb_rc *rc = dev_get_drvdata(dev);
47
48         if (rc->index == *index)
49                 return 1;
50         return 0;
51 }
52
53 static struct uwb_rc *uwb_rc_find_by_index(int index)
54 {
55         struct device *dev;
56         struct uwb_rc *rc = NULL;
57
58         dev = class_find_device(&uwb_rc_class, NULL, &index, uwb_rc_index_match);
59         if (dev)
60                 rc = dev_get_drvdata(dev);
61         return rc;
62 }
63
64 static int uwb_rc_new_index(void)
65 {
66         int index = 0;
67
68         for (;;) {
69                 if (!uwb_rc_find_by_index(index))
70                         return index;
71                 if (++index < 0)
72                         index = 0;
73         }
74 }
75
76 /**
77  * Release the backing device of a uwb_rc that has been dynamically allocated.
78  */
79 static void uwb_rc_sys_release(struct device *dev)
80 {
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);
83
84         uwb_rc_neh_destroy(rc);
85         uwb_rc_ie_release(rc);
86         d_printf(1, dev, "freed uwb_rc %p\n", rc);
87         kfree(rc);
88 }
89
90
91 void uwb_rc_init(struct uwb_rc *rc)
92 {
93         struct uwb_dev *uwb_dev = &rc->uwb_dev;
94
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);
99         rc->beaconing = -1;
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);
104         uwb_rc_ie_init(rc);
105         uwb_rsv_init(rc);
106         uwb_rc_pal_init(rc);
107 }
108 EXPORT_SYMBOL_GPL(uwb_rc_init);
109
110
111 struct uwb_rc *uwb_rc_alloc(void)
112 {
113         struct uwb_rc *rc;
114         rc = kzalloc(sizeof(*rc), GFP_KERNEL);
115         if (rc == NULL)
116                 return NULL;
117         uwb_rc_init(rc);
118         return rc;
119 }
120 EXPORT_SYMBOL_GPL(uwb_rc_alloc);
121
122 static struct attribute *rc_attrs[] = {
123                 &dev_attr_mac_address.attr,
124                 &dev_attr_scan.attr,
125                 &dev_attr_beacon.attr,
126                 NULL,
127 };
128
129 static struct attribute_group rc_attr_group = {
130         .attrs = rc_attrs,
131 };
132
133 /*
134  * Registration of sysfs specific stuff
135  */
136 static int uwb_rc_sys_add(struct uwb_rc *rc)
137 {
138         return sysfs_create_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
139 }
140
141
142 static void __uwb_rc_sys_rm(struct uwb_rc *rc)
143 {
144         sysfs_remove_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
145 }
146
147 /**
148  * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it
149  * @rc:  the radio controller.
150  *
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.
155  */
156 static
157 int uwb_rc_mac_addr_setup(struct uwb_rc *rc)
158 {
159         int result;
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;
164
165         result = uwb_rc_mac_addr_get(rc, &addr);
166         if (result < 0) {
167                 dev_err(dev, "cannot retrieve UWB EUI-48 address: %d\n", result);
168                 return result;
169         }
170
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);
174
175                 result = uwb_rc_mac_addr_set(rc, &addr);
176                 if (result < 0) {
177                         uwb_mac_addr_print(devname, sizeof(devname), &addr);
178                         dev_err(dev, "cannot set EUI-48 address %s: %d\n",
179                                 devname, result);
180                         return result;
181                 }
182         }
183         uwb_dev->mac_addr = addr;
184         return 0;
185 }
186
187
188
189 static int uwb_rc_setup(struct uwb_rc *rc)
190 {
191         int result;
192         struct device *dev = &rc->uwb_dev.dev;
193
194         result = uwb_rc_reset(rc);
195         if (result < 0) {
196                 dev_err(dev, "cannot reset UWB radio: %d\n", result);
197                 goto error;
198         }
199         result = uwb_rc_mac_addr_setup(rc);
200         if (result < 0) {
201                 dev_err(dev, "cannot setup UWB MAC address: %d\n", result);
202                 goto error;
203         }
204         result = uwb_rc_dev_addr_assign(rc);
205         if (result < 0) {
206                 dev_err(dev, "cannot assign UWB DevAddr: %d\n", result);
207                 goto error;
208         }
209         result = uwb_rc_ie_setup(rc);
210         if (result < 0) {
211                 dev_err(dev, "cannot setup IE subsystem: %d\n", result);
212                 goto error_ie_setup;
213         }
214         result = uwb_rsv_setup(rc);
215         if (result < 0) {
216                 dev_err(dev, "cannot setup reservation subsystem: %d\n", result);
217                 goto error_rsv_setup;
218         }
219         uwb_dbg_add_rc(rc);
220         return 0;
221
222 error_rsv_setup:
223         uwb_rc_ie_release(rc);
224 error_ie_setup:
225 error:
226         return result;
227 }
228
229
230 /**
231  * Register a new UWB radio controller
232  *
233  * Did you call uwb_rc_init() on your rc?
234  *
235  * We assume that this is being called with a > 0 refcount on
236  * it [through ops->{get|put}_device(). We'll take our own, though.
237  *
238  * @parent_dev is our real device, the one that provides the actual UWB device
239  */
240 int uwb_rc_add(struct uwb_rc *rc, struct device *parent_dev, void *priv)
241 {
242         int result;
243         struct device *dev;
244         char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
245
246         rc->index = uwb_rc_new_index();
247
248         dev = &rc->uwb_dev.dev;
249         dev_set_name(dev, "uwb%d", rc->index);
250
251         rc->priv = priv;
252
253         result = rc->start(rc);
254         if (result < 0)
255                 goto error_rc_start;
256
257         result = uwb_rc_setup(rc);
258         if (result < 0) {
259                 dev_err(dev, "cannot setup UWB radio controller: %d\n", result);
260                 goto error_rc_setup;
261         }
262
263         result = uwb_dev_add(&rc->uwb_dev, parent_dev, rc);
264         if (result < 0 && result != -EADDRNOTAVAIL)
265                 goto error_dev_add;
266
267         result = uwb_rc_sys_add(rc);
268         if (result < 0) {
269                 dev_err(parent_dev, "cannot register UWB radio controller "
270                         "dev attributes: %d\n", result);
271                 goto error_sys_add;
272         }
273
274         uwb_mac_addr_print(macbuf, sizeof(macbuf), &rc->uwb_dev.mac_addr);
275         uwb_dev_addr_print(devbuf, sizeof(devbuf), &rc->uwb_dev.dev_addr);
276         dev_info(dev,
277                  "new uwb radio controller (mac %s dev %s) on %s %s\n",
278                  macbuf, devbuf, parent_dev->bus->name, dev_name(parent_dev));
279         rc->ready = 1;
280         return 0;
281
282 error_sys_add:
283         uwb_dev_rm(&rc->uwb_dev);
284 error_dev_add:
285 error_rc_setup:
286         rc->stop(rc);
287         uwbd_flush(rc);
288 error_rc_start:
289         return result;
290 }
291 EXPORT_SYMBOL_GPL(uwb_rc_add);
292
293
294 static int uwb_dev_offair_helper(struct device *dev, void *priv)
295 {
296         struct uwb_dev *uwb_dev = to_uwb_dev(dev);
297
298         return __uwb_dev_offair(uwb_dev, uwb_dev->rc);
299 }
300
301 /*
302  * Remove a Radio Controller; stop beaconing/scanning, disconnect all children
303  */
304 void uwb_rc_rm(struct uwb_rc *rc)
305 {
306         rc->ready = 0;
307
308         uwb_dbg_del_rc(rc);
309         uwb_rsv_cleanup(rc);
310         uwb_rc_ie_rm(rc, UWB_IDENTIFICATION_IE);
311         if (rc->beaconing >= 0)
312                 uwb_rc_beacon(rc, -1, 0);
313         if (rc->scan_type != UWB_SCAN_DISABLED)
314                 uwb_rc_scan(rc, rc->scanning, UWB_SCAN_DISABLED, 0);
315         uwb_rc_reset(rc);
316
317         rc->stop(rc);
318         uwbd_flush(rc);
319
320         uwb_dev_lock(&rc->uwb_dev);
321         rc->priv = NULL;
322         rc->cmd = NULL;
323         uwb_dev_unlock(&rc->uwb_dev);
324         mutex_lock(&uwb_beca.mutex);
325         uwb_dev_for_each(rc, uwb_dev_offair_helper, NULL);
326         __uwb_rc_sys_rm(rc);
327         mutex_unlock(&uwb_beca.mutex);
328         uwb_dev_rm(&rc->uwb_dev);
329 }
330 EXPORT_SYMBOL_GPL(uwb_rc_rm);
331
332 static int find_rc_try_get(struct device *dev, void *data)
333 {
334         struct uwb_rc *target_rc = data;
335         struct uwb_rc *rc = dev_get_drvdata(dev);
336
337         if (rc == NULL) {
338                 WARN_ON(1);
339                 return 0;
340         }
341         if (rc == target_rc) {
342                 if (rc->ready == 0)
343                         return 0;
344                 else
345                         return 1;
346         }
347         return 0;
348 }
349
350 /**
351  * Given a radio controller descriptor, validate and refcount it
352  *
353  * @returns NULL if the rc does not exist or is quiescing; the ptr to
354  *               it otherwise.
355  */
356 struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *target_rc)
357 {
358         struct device *dev;
359         struct uwb_rc *rc = NULL;
360
361         dev = class_find_device(&uwb_rc_class, NULL, target_rc,
362                                 find_rc_try_get);
363         if (dev) {
364                 rc = dev_get_drvdata(dev);
365                 __uwb_rc_get(rc);
366         }
367         return rc;
368 }
369 EXPORT_SYMBOL_GPL(__uwb_rc_try_get);
370
371 /*
372  * RC get for external refcount acquirers...
373  *
374  * Increments the refcount of the device and it's backend modules
375  */
376 static inline struct uwb_rc *uwb_rc_get(struct uwb_rc *rc)
377 {
378         if (rc->ready == 0)
379                 return NULL;
380         uwb_dev_get(&rc->uwb_dev);
381         return rc;
382 }
383
384 static int find_rc_grandpa(struct device *dev, void *data)
385 {
386         struct device *grandpa_dev = data;
387         struct uwb_rc *rc = dev_get_drvdata(dev);
388
389         if (rc->uwb_dev.dev.parent->parent == grandpa_dev) {
390                 rc = uwb_rc_get(rc);
391                 return 1;
392         }
393         return 0;
394 }
395
396 /**
397  * Locate and refcount a radio controller given a common grand-parent
398  *
399  * @grandpa_dev  Pointer to the 'grandparent' device structure.
400  * @returns NULL If the rc does not exist or is quiescing; the ptr to
401  *               it otherwise, properly referenced.
402  *
403  * The Radio Control interface (or the UWB Radio Controller) is always
404  * an interface of a device. The parent is the interface, the
405  * grandparent is the device that encapsulates the interface.
406  *
407  * There is no need to lock around as the "grandpa" would be
408  * refcounted by the target, and to remove the referemes, the
409  * uwb_rc_class->sem would have to be taken--we hold it, ergo we
410  * should be safe.
411  */
412 struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *grandpa_dev)
413 {
414         struct device *dev;
415         struct uwb_rc *rc = NULL;
416
417         dev = class_find_device(&uwb_rc_class, NULL, (void *)grandpa_dev,
418                                 find_rc_grandpa);
419         if (dev)
420                 rc = dev_get_drvdata(dev);
421         return rc;
422 }
423 EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa);
424
425 /**
426  * Find a radio controller by device address
427  *
428  * @returns the pointer to the radio controller, properly referenced
429  */
430 static int find_rc_dev(struct device *dev, void *data)
431 {
432         struct uwb_dev_addr *addr = data;
433         struct uwb_rc *rc = dev_get_drvdata(dev);
434
435         if (rc == NULL) {
436                 WARN_ON(1);
437                 return 0;
438         }
439         if (!uwb_dev_addr_cmp(&rc->uwb_dev.dev_addr, addr)) {
440                 rc = uwb_rc_get(rc);
441                 return 1;
442         }
443         return 0;
444 }
445
446 struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *addr)
447 {
448         struct device *dev;
449         struct uwb_rc *rc = NULL;
450
451         dev = class_find_device(&uwb_rc_class, NULL, (void *)addr,
452                                 find_rc_dev);
453         if (dev)
454                 rc = dev_get_drvdata(dev);
455
456         return rc;
457 }
458 EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev);
459
460 /**
461  * Drop a reference on a radio controller
462  *
463  * This is the version that should be done by entities external to the
464  * UWB Radio Control stack (ie: clients of the API).
465  */
466 void uwb_rc_put(struct uwb_rc *rc)
467 {
468         __uwb_rc_put(rc);
469 }
470 EXPORT_SYMBOL_GPL(uwb_rc_put);
471
472 /*
473  *
474  *
475  */
476 ssize_t uwb_rc_print_IEs(struct uwb_rc *uwb_rc, char *buf, size_t size)
477 {
478         ssize_t result;
479         struct uwb_rc_evt_get_ie *ie_info;
480         struct uwb_buf_ctx ctx;
481
482         result = uwb_rc_get_ie(uwb_rc, &ie_info);
483         if (result < 0)
484                 goto error_get_ie;
485         ctx.buf = buf;
486         ctx.size = size;
487         ctx.bytes = 0;
488         uwb_ie_for_each(&uwb_rc->uwb_dev, uwb_ie_dump_hex, &ctx,
489                         ie_info->IEData, result - sizeof(*ie_info));
490         result = ctx.bytes;
491         kfree(ie_info);
492 error_get_ie:
493         return result;
494 }
495