treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 268
[linux-2.6-microblaze.git] / drivers / uwb / driver.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Ultra Wide Band
4  * Driver initialization, etc
5  *
6  * Copyright (C) 2005-2006 Intel Corporation
7  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8  *
9  * FIXME: docs
10  *
11  * Life cycle: FIXME: explain
12  *
13  *  UWB radio controller:
14  *
15  *    1. alloc a uwb_rc, zero it
16  *    2. call uwb_rc_init() on it to set it up + ops (won't do any
17  *       kind of allocation)
18  *    3. register (now it is owned by the UWB stack--deregister before
19  *       freeing/destroying).
20  *    4. It lives on it's own now (UWB stack handles)--when it
21  *       disconnects, call unregister()
22  *    5. free it.
23  *
24  *    Make sure you have a reference to the uwb_rc before calling
25  *    any of the UWB API functions.
26  *
27  * TODO:
28  *
29  * 1. Locking and life cycle management is crappy still. All entry
30  *    points to the UWB HCD API assume you have a reference on the
31  *    uwb_rc structure and that it won't go away. They mutex lock it
32  *    before doing anything.
33  */
34
35 #include <linux/kernel.h>
36 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/device.h>
39 #include <linux/err.h>
40 #include <linux/kdev_t.h>
41 #include <linux/random.h>
42
43 #include "uwb-internal.h"
44
45
46 /* UWB stack attributes (or 'global' constants) */
47
48
49 /**
50  * If a beacon disappears for longer than this, then we consider the
51  * device who was represented by that beacon to be gone.
52  *
53  * ECMA-368[17.2.3, last para] establishes that a device must not
54  * consider a device to be its neighbour if he doesn't receive a beacon
55  * for more than mMaxLostBeacons. mMaxLostBeacons is defined in
56  * ECMA-368[17.16] as 3; because we can get only one beacon per
57  * superframe, that'd be 3 * 65ms = 195 ~ 200 ms. Let's give it time
58  * for jitter and stuff and make it 500 ms.
59  */
60 unsigned long beacon_timeout_ms = 500;
61
62 static
63 ssize_t beacon_timeout_ms_show(struct class *class,
64                                 struct class_attribute *attr,
65                                 char *buf)
66 {
67         return scnprintf(buf, PAGE_SIZE, "%lu\n", beacon_timeout_ms);
68 }
69
70 static
71 ssize_t beacon_timeout_ms_store(struct class *class,
72                                 struct class_attribute *attr,
73                                 const char *buf, size_t size)
74 {
75         unsigned long bt;
76         ssize_t result;
77         result = sscanf(buf, "%lu", &bt);
78         if (result != 1)
79                 return -EINVAL;
80         beacon_timeout_ms = bt;
81         return size;
82 }
83 static CLASS_ATTR_RW(beacon_timeout_ms);
84
85 static struct attribute *uwb_class_attrs[] = {
86         &class_attr_beacon_timeout_ms.attr,
87         NULL,
88 };
89 ATTRIBUTE_GROUPS(uwb_class);
90
91 /** Device model classes */
92 struct class uwb_rc_class = {
93         .name        = "uwb_rc",
94         .class_groups = uwb_class_groups,
95 };
96
97
98 static int __init uwb_subsys_init(void)
99 {
100         int result = 0;
101
102         result = uwb_est_create();
103         if (result < 0) {
104                 printk(KERN_ERR "uwb: Can't initialize EST subsystem\n");
105                 goto error_est_init;
106         }
107
108         result = class_register(&uwb_rc_class);
109         if (result < 0)
110                 goto error_uwb_rc_class_register;
111
112         /* Register the UWB bus */
113         result = bus_register(&uwb_bus_type);
114         if (result) {
115                 pr_err("%s - registering bus driver failed\n", __func__);
116                 goto exit_bus;
117         }
118
119         uwb_dbg_init();
120         return 0;
121
122 exit_bus:
123         class_unregister(&uwb_rc_class);
124 error_uwb_rc_class_register:
125         uwb_est_destroy();
126 error_est_init:
127         return result;
128 }
129 module_init(uwb_subsys_init);
130
131 static void __exit uwb_subsys_exit(void)
132 {
133         uwb_dbg_exit();
134         bus_unregister(&uwb_bus_type);
135         class_unregister(&uwb_rc_class);
136         uwb_est_destroy();
137         return;
138 }
139 module_exit(uwb_subsys_exit);
140
141 MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
142 MODULE_DESCRIPTION("Ultra Wide Band core");
143 MODULE_LICENSE("GPL");