uwb: remove unused #include <version.h>
[deliverable/linux.git] / drivers / uwb / drp-ie.c
1 /*
2 * UWB DRP IE management.
3 *
4 * Copyright (C) 2005-2006 Intel Corporation
5 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include <linux/kernel.h>
20 #include <linux/random.h>
21 #include <linux/uwb.h>
22
23 #include "uwb-internal.h"
24
25 /*
26 * Allocate a DRP IE.
27 *
28 * To save having to free/allocate a DRP IE when its MAS changes,
29 * enough memory is allocated for the maxiumum number of DRP
30 * allocation fields. This gives an overhead per reservation of up to
31 * (UWB_NUM_ZONES - 1) * 4 = 60 octets.
32 */
33 static struct uwb_ie_drp *uwb_drp_ie_alloc(void)
34 {
35 struct uwb_ie_drp *drp_ie;
36 unsigned tiebreaker;
37
38 drp_ie = kzalloc(sizeof(struct uwb_ie_drp) +
39 UWB_NUM_ZONES * sizeof(struct uwb_drp_alloc),
40 GFP_KERNEL);
41 if (drp_ie) {
42 drp_ie->hdr.element_id = UWB_IE_DRP;
43
44 get_random_bytes(&tiebreaker, sizeof(unsigned));
45 uwb_ie_drp_set_tiebreaker(drp_ie, tiebreaker & 1);
46 }
47 return drp_ie;
48 }
49
50
51 /*
52 * Fill a DRP IE's allocation fields from a MAS bitmap.
53 */
54 static void uwb_drp_ie_from_bm(struct uwb_ie_drp *drp_ie,
55 struct uwb_mas_bm *mas)
56 {
57 int z, i, num_fields = 0, next = 0;
58 struct uwb_drp_alloc *zones;
59 __le16 current_bmp;
60 DECLARE_BITMAP(tmp_bmp, UWB_NUM_MAS);
61 DECLARE_BITMAP(tmp_mas_bm, UWB_MAS_PER_ZONE);
62
63 zones = drp_ie->allocs;
64
65 bitmap_copy(tmp_bmp, mas->bm, UWB_NUM_MAS);
66
67 /* Determine unique MAS bitmaps in zones from bitmap. */
68 for (z = 0; z < UWB_NUM_ZONES; z++) {
69 bitmap_copy(tmp_mas_bm, tmp_bmp, UWB_MAS_PER_ZONE);
70 if (bitmap_weight(tmp_mas_bm, UWB_MAS_PER_ZONE) > 0) {
71 bool found = false;
72 current_bmp = (__le16) *tmp_mas_bm;
73 for (i = 0; i < next; i++) {
74 if (current_bmp == zones[i].mas_bm) {
75 zones[i].zone_bm |= 1 << z;
76 found = true;
77 break;
78 }
79 }
80 if (!found) {
81 num_fields++;
82 zones[next].zone_bm = 1 << z;
83 zones[next].mas_bm = current_bmp;
84 next++;
85 }
86 }
87 bitmap_shift_right(tmp_bmp, tmp_bmp, UWB_MAS_PER_ZONE, UWB_NUM_MAS);
88 }
89
90 /* Store in format ready for transmission (le16). */
91 for (i = 0; i < num_fields; i++) {
92 drp_ie->allocs[i].zone_bm = cpu_to_le16(zones[i].zone_bm);
93 drp_ie->allocs[i].mas_bm = cpu_to_le16(zones[i].mas_bm);
94 }
95
96 drp_ie->hdr.length = sizeof(struct uwb_ie_drp) - sizeof(struct uwb_ie_hdr)
97 + num_fields * sizeof(struct uwb_drp_alloc);
98 }
99
100 /**
101 * uwb_drp_ie_update - update a reservation's DRP IE
102 * @rsv: the reservation
103 */
104 int uwb_drp_ie_update(struct uwb_rsv *rsv)
105 {
106 struct device *dev = &rsv->rc->uwb_dev.dev;
107 struct uwb_ie_drp *drp_ie;
108 int reason_code, status;
109
110 switch (rsv->state) {
111 case UWB_RSV_STATE_NONE:
112 kfree(rsv->drp_ie);
113 rsv->drp_ie = NULL;
114 return 0;
115 case UWB_RSV_STATE_O_INITIATED:
116 reason_code = UWB_DRP_REASON_ACCEPTED;
117 status = 0;
118 break;
119 case UWB_RSV_STATE_O_PENDING:
120 reason_code = UWB_DRP_REASON_ACCEPTED;
121 status = 0;
122 break;
123 case UWB_RSV_STATE_O_MODIFIED:
124 reason_code = UWB_DRP_REASON_MODIFIED;
125 status = 1;
126 break;
127 case UWB_RSV_STATE_O_ESTABLISHED:
128 reason_code = UWB_DRP_REASON_ACCEPTED;
129 status = 1;
130 break;
131 case UWB_RSV_STATE_T_ACCEPTED:
132 reason_code = UWB_DRP_REASON_ACCEPTED;
133 status = 1;
134 break;
135 case UWB_RSV_STATE_T_DENIED:
136 reason_code = UWB_DRP_REASON_DENIED;
137 status = 0;
138 break;
139 default:
140 dev_dbg(dev, "rsv with unhandled state (%d)\n", rsv->state);
141 return -EINVAL;
142 }
143
144 if (rsv->drp_ie == NULL) {
145 rsv->drp_ie = uwb_drp_ie_alloc();
146 if (rsv->drp_ie == NULL)
147 return -ENOMEM;
148 }
149 drp_ie = rsv->drp_ie;
150
151 uwb_ie_drp_set_owner(drp_ie, uwb_rsv_is_owner(rsv));
152 uwb_ie_drp_set_status(drp_ie, status);
153 uwb_ie_drp_set_reason_code(drp_ie, reason_code);
154 uwb_ie_drp_set_stream_index(drp_ie, rsv->stream);
155 uwb_ie_drp_set_type(drp_ie, rsv->type);
156
157 if (uwb_rsv_is_owner(rsv)) {
158 switch (rsv->target.type) {
159 case UWB_RSV_TARGET_DEV:
160 drp_ie->dev_addr = rsv->target.dev->dev_addr;
161 break;
162 case UWB_RSV_TARGET_DEVADDR:
163 drp_ie->dev_addr = rsv->target.devaddr;
164 break;
165 }
166 } else
167 drp_ie->dev_addr = rsv->owner->dev_addr;
168
169 uwb_drp_ie_from_bm(drp_ie, &rsv->mas);
170
171 rsv->ie_valid = true;
172 return 0;
173 }
174
175 /*
176 * Set MAS bits from given MAS bitmap in a single zone of large bitmap.
177 *
178 * We are given a zone id and the MAS bitmap of bits that need to be set in
179 * this zone. Note that this zone may already have bits set and this only
180 * adds settings - we cannot simply assign the MAS bitmap contents to the
181 * zone contents. We iterate over the the bits (MAS) in the zone and set the
182 * bits that are set in the given MAS bitmap.
183 */
184 static
185 void uwb_drp_ie_single_zone_to_bm(struct uwb_mas_bm *bm, u8 zone, u16 mas_bm)
186 {
187 int mas;
188 u16 mas_mask;
189
190 for (mas = 0; mas < UWB_MAS_PER_ZONE; mas++) {
191 mas_mask = 1 << mas;
192 if (mas_bm & mas_mask)
193 set_bit(zone * UWB_NUM_ZONES + mas, bm->bm);
194 }
195 }
196
197 /**
198 * uwb_drp_ie_zones_to_bm - convert DRP allocation fields to a bitmap
199 * @mas: MAS bitmap that will be populated to correspond to the
200 * allocation fields in the DRP IE
201 * @drp_ie: the DRP IE that contains the allocation fields.
202 *
203 * The input format is an array of MAS allocation fields (16 bit Zone
204 * bitmap, 16 bit MAS bitmap) as described in [ECMA-368] section
205 * 16.8.6. The output is a full 256 bit MAS bitmap.
206 *
207 * We go over all the allocation fields, for each allocation field we
208 * know which zones are impacted. We iterate over all the zones
209 * impacted and call a function that will set the correct MAS bits in
210 * each zone.
211 */
212 void uwb_drp_ie_to_bm(struct uwb_mas_bm *bm, const struct uwb_ie_drp *drp_ie)
213 {
214 int numallocs = (drp_ie->hdr.length - 4) / 4;
215 const struct uwb_drp_alloc *alloc;
216 int cnt;
217 u16 zone_bm, mas_bm;
218 u8 zone;
219 u16 zone_mask;
220
221 for (cnt = 0; cnt < numallocs; cnt++) {
222 alloc = &drp_ie->allocs[cnt];
223 zone_bm = le16_to_cpu(alloc->zone_bm);
224 mas_bm = le16_to_cpu(alloc->mas_bm);
225 for (zone = 0; zone < UWB_NUM_ZONES; zone++) {
226 zone_mask = 1 << zone;
227 if (zone_bm & zone_mask)
228 uwb_drp_ie_single_zone_to_bm(bm, zone, mas_bm);
229 }
230 }
231 }
This page took 0.035471 seconds and 6 git commands to generate.