pandora: defconfig: update
[pandora-kernel.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 #include <linux/slab.h>
39 #include <linux/export.h>
40
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                 put_device(dev);
62         }
63
64         return rc;
65 }
66
67 static int uwb_rc_new_index(void)
68 {
69         int index = 0;
70
71         for (;;) {
72                 if (!uwb_rc_find_by_index(index))
73                         return index;
74                 if (++index < 0)
75                         index = 0;
76         }
77 }
78
79 /**
80  * Release the backing device of a uwb_rc that has been dynamically allocated.
81  */
82 static void uwb_rc_sys_release(struct device *dev)
83 {
84         struct uwb_dev *uwb_dev = container_of(dev, struct uwb_dev, dev);
85         struct uwb_rc *rc = container_of(uwb_dev, struct uwb_rc, uwb_dev);
86
87         uwb_rc_ie_release(rc);
88         kfree(rc);
89 }
90
91
92 void uwb_rc_init(struct uwb_rc *rc)
93 {
94         struct uwb_dev *uwb_dev = &rc->uwb_dev;
95
96         uwb_dev_init(uwb_dev);
97         rc->uwb_dev.dev.class = &uwb_rc_class;
98         rc->uwb_dev.dev.release = uwb_rc_sys_release;
99         uwb_rc_neh_create(rc);
100         rc->beaconing = -1;
101         rc->scan_type = UWB_SCAN_DISABLED;
102         INIT_LIST_HEAD(&rc->notifs_chain.list);
103         mutex_init(&rc->notifs_chain.mutex);
104         INIT_LIST_HEAD(&rc->uwb_beca.list);
105         mutex_init(&rc->uwb_beca.mutex);
106         uwb_drp_avail_init(rc);
107         uwb_rc_ie_init(rc);
108         uwb_rsv_init(rc);
109         uwb_rc_pal_init(rc);
110 }
111 EXPORT_SYMBOL_GPL(uwb_rc_init);
112
113
114 struct uwb_rc *uwb_rc_alloc(void)
115 {
116         struct uwb_rc *rc;
117         rc = kzalloc(sizeof(*rc), GFP_KERNEL);
118         if (rc == NULL)
119                 return NULL;
120         uwb_rc_init(rc);
121         return rc;
122 }
123 EXPORT_SYMBOL_GPL(uwb_rc_alloc);
124
125 static struct attribute *rc_attrs[] = {
126                 &dev_attr_mac_address.attr,
127                 &dev_attr_scan.attr,
128                 &dev_attr_beacon.attr,
129                 NULL,
130 };
131
132 static struct attribute_group rc_attr_group = {
133         .attrs = rc_attrs,
134 };
135
136 /*
137  * Registration of sysfs specific stuff
138  */
139 static int uwb_rc_sys_add(struct uwb_rc *rc)
140 {
141         return sysfs_create_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
142 }
143
144
145 static void __uwb_rc_sys_rm(struct uwb_rc *rc)
146 {
147         sysfs_remove_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
148 }
149
150 /**
151  * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it
152  * @rc:  the radio controller.
153  *
154  * If the EUI-48 address is 00:00:00:00:00:00 or FF:FF:FF:FF:FF:FF
155  * then a random locally administered EUI-48 is generated and set on
156  * the device.  The probability of address collisions is sufficiently
157  * unlikely (1/2^40 = 9.1e-13) that they're not checked for.
158  */
159 static
160 int uwb_rc_mac_addr_setup(struct uwb_rc *rc)
161 {
162         int result;
163         struct device *dev = &rc->uwb_dev.dev;
164         struct uwb_dev *uwb_dev = &rc->uwb_dev;
165         char devname[UWB_ADDR_STRSIZE];
166         struct uwb_mac_addr addr;
167
168         result = uwb_rc_mac_addr_get(rc, &addr);
169         if (result < 0) {
170                 dev_err(dev, "cannot retrieve UWB EUI-48 address: %d\n", result);
171                 return result;
172         }
173
174         if (uwb_mac_addr_unset(&addr) || uwb_mac_addr_bcast(&addr)) {
175                 addr.data[0] = 0x02; /* locally administered and unicast */
176                 get_random_bytes(&addr.data[1], sizeof(addr.data)-1);
177
178                 result = uwb_rc_mac_addr_set(rc, &addr);
179                 if (result < 0) {
180                         uwb_mac_addr_print(devname, sizeof(devname), &addr);
181                         dev_err(dev, "cannot set EUI-48 address %s: %d\n",
182                                 devname, result);
183                         return result;
184                 }
185         }
186         uwb_dev->mac_addr = addr;
187         return 0;
188 }
189
190
191
192 static int uwb_rc_setup(struct uwb_rc *rc)
193 {
194         int result;
195         struct device *dev = &rc->uwb_dev.dev;
196
197         result = uwb_radio_setup(rc);
198         if (result < 0) {
199                 dev_err(dev, "cannot setup UWB radio: %d\n", result);
200                 goto error;
201         }
202         result = uwb_rc_mac_addr_setup(rc);
203         if (result < 0) {
204                 dev_err(dev, "cannot setup UWB MAC address: %d\n", result);
205                 goto error;
206         }
207         result = uwb_rc_dev_addr_assign(rc);
208         if (result < 0) {
209                 dev_err(dev, "cannot assign UWB DevAddr: %d\n", result);
210                 goto error;
211         }
212         result = uwb_rc_ie_setup(rc);
213         if (result < 0) {
214                 dev_err(dev, "cannot setup IE subsystem: %d\n", result);
215                 goto error_ie_setup;
216         }
217         result = uwb_rsv_setup(rc);
218         if (result < 0) {
219                 dev_err(dev, "cannot setup reservation subsystem: %d\n", result);
220                 goto error_rsv_setup;
221         }
222         uwb_dbg_add_rc(rc);
223         return 0;
224
225 error_rsv_setup:
226         uwb_rc_ie_release(rc);
227 error_ie_setup:
228 error:
229         return result;
230 }
231
232
233 /**
234  * Register a new UWB radio controller
235  *
236  * Did you call uwb_rc_init() on your rc?
237  *
238  * We assume that this is being called with a > 0 refcount on
239  * it [through ops->{get|put}_device(). We'll take our own, though.
240  *
241  * @parent_dev is our real device, the one that provides the actual UWB device
242  */
243 int uwb_rc_add(struct uwb_rc *rc, struct device *parent_dev, void *priv)
244 {
245         int result;
246         struct device *dev;
247         char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
248
249         rc->index = uwb_rc_new_index();
250
251         dev = &rc->uwb_dev.dev;
252         dev_set_name(dev, "uwb%d", rc->index);
253
254         rc->priv = priv;
255
256         init_waitqueue_head(&rc->uwbd.wq);
257         INIT_LIST_HEAD(&rc->uwbd.event_list);
258         spin_lock_init(&rc->uwbd.event_list_lock);
259
260         uwbd_start(rc);
261
262         result = rc->start(rc);
263         if (result < 0)
264                 goto error_rc_start;
265
266         result = uwb_rc_setup(rc);
267         if (result < 0) {
268                 dev_err(dev, "cannot setup UWB radio controller: %d\n", result);
269                 goto error_rc_setup;
270         }
271
272         result = uwb_dev_add(&rc->uwb_dev, parent_dev, rc);
273         if (result < 0 && result != -EADDRNOTAVAIL)
274                 goto error_dev_add;
275
276         result = uwb_rc_sys_add(rc);
277         if (result < 0) {
278                 dev_err(parent_dev, "cannot register UWB radio controller "
279                         "dev attributes: %d\n", result);
280                 goto error_sys_add;
281         }
282
283         uwb_mac_addr_print(macbuf, sizeof(macbuf), &rc->uwb_dev.mac_addr);
284         uwb_dev_addr_print(devbuf, sizeof(devbuf), &rc->uwb_dev.dev_addr);
285         dev_info(dev,
286                  "new uwb radio controller (mac %s dev %s) on %s %s\n",
287                  macbuf, devbuf, parent_dev->bus->name, dev_name(parent_dev));
288         rc->ready = 1;
289         return 0;
290
291 error_sys_add:
292         uwb_dev_rm(&rc->uwb_dev);
293 error_dev_add:
294 error_rc_setup:
295         rc->stop(rc);
296 error_rc_start:
297         uwbd_stop(rc);
298         return result;
299 }
300 EXPORT_SYMBOL_GPL(uwb_rc_add);
301
302
303 static int uwb_dev_offair_helper(struct device *dev, void *priv)
304 {
305         struct uwb_dev *uwb_dev = to_uwb_dev(dev);
306
307         return __uwb_dev_offair(uwb_dev, uwb_dev->rc);
308 }
309
310 /*
311  * Remove a Radio Controller; stop beaconing/scanning, disconnect all children
312  */
313 void uwb_rc_rm(struct uwb_rc *rc)
314 {
315         rc->ready = 0;
316
317         uwb_dbg_del_rc(rc);
318         uwb_rsv_remove_all(rc);
319         uwb_radio_shutdown(rc);
320
321         rc->stop(rc);
322
323         uwbd_stop(rc);
324         uwb_rc_neh_destroy(rc);
325
326         uwb_dev_lock(&rc->uwb_dev);
327         rc->priv = NULL;
328         rc->cmd = NULL;
329         uwb_dev_unlock(&rc->uwb_dev);
330         mutex_lock(&rc->uwb_beca.mutex);
331         uwb_dev_for_each(rc, uwb_dev_offair_helper, NULL);
332         __uwb_rc_sys_rm(rc);
333         mutex_unlock(&rc->uwb_beca.mutex);
334         uwb_rsv_cleanup(rc);
335         uwb_beca_release(rc);
336         uwb_dev_rm(&rc->uwb_dev);
337 }
338 EXPORT_SYMBOL_GPL(uwb_rc_rm);
339
340 static int find_rc_try_get(struct device *dev, void *data)
341 {
342         struct uwb_rc *target_rc = data;
343         struct uwb_rc *rc = dev_get_drvdata(dev);
344
345         if (rc == NULL) {
346                 WARN_ON(1);
347                 return 0;
348         }
349         if (rc == target_rc) {
350                 if (rc->ready == 0)
351                         return 0;
352                 else
353                         return 1;
354         }
355         return 0;
356 }
357
358 /**
359  * Given a radio controller descriptor, validate and refcount it
360  *
361  * @returns NULL if the rc does not exist or is quiescing; the ptr to
362  *               it otherwise.
363  */
364 struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *target_rc)
365 {
366         struct device *dev;
367         struct uwb_rc *rc = NULL;
368
369         dev = class_find_device(&uwb_rc_class, NULL, target_rc,
370                                 find_rc_try_get);
371         if (dev) {
372                 rc = dev_get_drvdata(dev);
373                 __uwb_rc_get(rc);
374                 put_device(dev);
375         }
376
377         return rc;
378 }
379 EXPORT_SYMBOL_GPL(__uwb_rc_try_get);
380
381 /*
382  * RC get for external refcount acquirers...
383  *
384  * Increments the refcount of the device and it's backend modules
385  */
386 static inline struct uwb_rc *uwb_rc_get(struct uwb_rc *rc)
387 {
388         if (rc->ready == 0)
389                 return NULL;
390         uwb_dev_get(&rc->uwb_dev);
391         return rc;
392 }
393
394 static int find_rc_grandpa(struct device *dev, void *data)
395 {
396         struct device *grandpa_dev = data;
397         struct uwb_rc *rc = dev_get_drvdata(dev);
398
399         if (rc->uwb_dev.dev.parent->parent == grandpa_dev) {
400                 rc = uwb_rc_get(rc);
401                 return 1;
402         }
403         return 0;
404 }
405
406 /**
407  * Locate and refcount a radio controller given a common grand-parent
408  *
409  * @grandpa_dev  Pointer to the 'grandparent' device structure.
410  * @returns NULL If the rc does not exist or is quiescing; the ptr to
411  *               it otherwise, properly referenced.
412  *
413  * The Radio Control interface (or the UWB Radio Controller) is always
414  * an interface of a device. The parent is the interface, the
415  * grandparent is the device that encapsulates the interface.
416  *
417  * There is no need to lock around as the "grandpa" would be
418  * refcounted by the target, and to remove the referemes, the
419  * uwb_rc_class->sem would have to be taken--we hold it, ergo we
420  * should be safe.
421  */
422 struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *grandpa_dev)
423 {
424         struct device *dev;
425         struct uwb_rc *rc = NULL;
426
427         dev = class_find_device(&uwb_rc_class, NULL, (void *)grandpa_dev,
428                                 find_rc_grandpa);
429         if (dev) {
430                 rc = dev_get_drvdata(dev);
431                 put_device(dev);
432         }
433
434         return rc;
435 }
436 EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa);
437
438 /**
439  * Find a radio controller by device address
440  *
441  * @returns the pointer to the radio controller, properly referenced
442  */
443 static int find_rc_dev(struct device *dev, void *data)
444 {
445         struct uwb_dev_addr *addr = data;
446         struct uwb_rc *rc = dev_get_drvdata(dev);
447
448         if (rc == NULL) {
449                 WARN_ON(1);
450                 return 0;
451         }
452         if (!uwb_dev_addr_cmp(&rc->uwb_dev.dev_addr, addr)) {
453                 rc = uwb_rc_get(rc);
454                 return 1;
455         }
456         return 0;
457 }
458
459 struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *addr)
460 {
461         struct device *dev;
462         struct uwb_rc *rc = NULL;
463
464         dev = class_find_device(&uwb_rc_class, NULL, (void *)addr,
465                                 find_rc_dev);
466         if (dev) {
467                 rc = dev_get_drvdata(dev);
468                 put_device(dev);
469         }
470
471         return rc;
472 }
473 EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev);
474
475 /**
476  * Drop a reference on a radio controller
477  *
478  * This is the version that should be done by entities external to the
479  * UWB Radio Control stack (ie: clients of the API).
480  */
481 void uwb_rc_put(struct uwb_rc *rc)
482 {
483         __uwb_rc_put(rc);
484 }
485 EXPORT_SYMBOL_GPL(uwb_rc_put);