1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include <linux/usb/wusb.h>
40#include <linux/slab.h>
41#include <linux/export.h>
42#include "wusbhc.h"
43
44
45int wusbhc_mmcie_create(struct wusbhc *wusbhc)
46{
47 u8 mmcies = wusbhc->mmcies_max;
48 wusbhc->mmcie = kcalloc(mmcies, sizeof(wusbhc->mmcie[0]), GFP_KERNEL);
49 if (wusbhc->mmcie == NULL)
50 return -ENOMEM;
51 mutex_init(&wusbhc->mmcie_mutex);
52 return 0;
53}
54
55
56void wusbhc_mmcie_destroy(struct wusbhc *wusbhc)
57{
58 kfree(wusbhc->mmcie);
59}
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94int wusbhc_mmcie_set(struct wusbhc *wusbhc, u8 interval, u8 repeat_cnt,
95 struct wuie_hdr *wuie)
96{
97 int result = -ENOBUFS;
98 unsigned handle, itr;
99
100
101 mutex_lock(&wusbhc->mmcie_mutex);
102 switch (wuie->bIEIdentifier) {
103 case WUIE_ID_HOST_INFO:
104
105 handle = wusbhc->mmcies_max - 1;
106 break;
107 case WUIE_ID_ISOCH_DISCARD:
108 dev_err(wusbhc->dev, "Special ordering case for WUIE ID 0x%x "
109 "unimplemented\n", wuie->bIEIdentifier);
110 result = -ENOSYS;
111 goto error_unlock;
112 default:
113
114 handle = ~0;
115 for (itr = 0; itr < wusbhc->mmcies_max - 1; itr++) {
116 if (wusbhc->mmcie[itr] == wuie) {
117 handle = itr;
118 break;
119 }
120 if (wusbhc->mmcie[itr] == NULL)
121 handle = itr;
122 }
123 if (handle == ~0)
124 goto error_unlock;
125 }
126 result = (wusbhc->mmcie_add)(wusbhc, interval, repeat_cnt, handle,
127 wuie);
128 if (result >= 0)
129 wusbhc->mmcie[handle] = wuie;
130error_unlock:
131 mutex_unlock(&wusbhc->mmcie_mutex);
132 return result;
133}
134EXPORT_SYMBOL_GPL(wusbhc_mmcie_set);
135
136
137
138
139
140
141void wusbhc_mmcie_rm(struct wusbhc *wusbhc, struct wuie_hdr *wuie)
142{
143 int result;
144 unsigned handle, itr;
145
146 mutex_lock(&wusbhc->mmcie_mutex);
147 for (itr = 0; itr < wusbhc->mmcies_max; itr++) {
148 if (wusbhc->mmcie[itr] == wuie) {
149 handle = itr;
150 goto found;
151 }
152 }
153 mutex_unlock(&wusbhc->mmcie_mutex);
154 return;
155
156found:
157 result = (wusbhc->mmcie_rm)(wusbhc, handle);
158 if (result == 0)
159 wusbhc->mmcie[itr] = NULL;
160 mutex_unlock(&wusbhc->mmcie_mutex);
161}
162EXPORT_SYMBOL_GPL(wusbhc_mmcie_rm);
163
164static int wusbhc_mmc_start(struct wusbhc *wusbhc)
165{
166 int ret;
167
168 mutex_lock(&wusbhc->mutex);
169 ret = wusbhc->start(wusbhc);
170 if (ret >= 0)
171 wusbhc->active = 1;
172 mutex_unlock(&wusbhc->mutex);
173
174 return ret;
175}
176
177static void wusbhc_mmc_stop(struct wusbhc *wusbhc)
178{
179 mutex_lock(&wusbhc->mutex);
180 wusbhc->active = 0;
181 wusbhc->stop(wusbhc, WUSB_CHANNEL_STOP_DELAY_MS);
182 mutex_unlock(&wusbhc->mutex);
183}
184
185
186
187
188
189
190
191
192int wusbhc_start(struct wusbhc *wusbhc)
193{
194 int result;
195 struct device *dev = wusbhc->dev;
196
197 WARN_ON(wusbhc->wuie_host_info != NULL);
198 BUG_ON(wusbhc->uwb_rc == NULL);
199
200 result = wusbhc_rsv_establish(wusbhc);
201 if (result < 0) {
202 dev_err(dev, "cannot establish cluster reservation: %d\n",
203 result);
204 goto error_rsv_establish;
205 }
206
207 result = wusbhc_devconnect_start(wusbhc);
208 if (result < 0) {
209 dev_err(dev, "error enabling device connections: %d\n",
210 result);
211 goto error_devconnect_start;
212 }
213
214 result = wusbhc_sec_start(wusbhc);
215 if (result < 0) {
216 dev_err(dev, "error starting security in the HC: %d\n",
217 result);
218 goto error_sec_start;
219 }
220
221 result = wusbhc->set_num_dnts(wusbhc, wusbhc->dnts_interval,
222 wusbhc->dnts_num_slots);
223 if (result < 0) {
224 dev_err(dev, "Cannot set DNTS parameters: %d\n", result);
225 goto error_set_num_dnts;
226 }
227 result = wusbhc_mmc_start(wusbhc);
228 if (result < 0) {
229 dev_err(dev, "error starting wusbch: %d\n", result);
230 goto error_wusbhc_start;
231 }
232
233 return 0;
234
235error_wusbhc_start:
236 wusbhc_sec_stop(wusbhc);
237error_set_num_dnts:
238error_sec_start:
239 wusbhc_devconnect_stop(wusbhc);
240error_devconnect_start:
241 wusbhc_rsv_terminate(wusbhc);
242error_rsv_establish:
243 return result;
244}
245
246
247
248
249
250
251
252void wusbhc_stop(struct wusbhc *wusbhc)
253{
254 wusbhc_mmc_stop(wusbhc);
255 wusbhc_sec_stop(wusbhc);
256 wusbhc_devconnect_stop(wusbhc);
257 wusbhc_rsv_terminate(wusbhc);
258}
259
260
261
262
263
264
265
266
267int wusbhc_chid_set(struct wusbhc *wusbhc, const struct wusb_ckhdid *chid)
268{
269 int result = 0;
270
271 if (memcmp(chid, &wusb_ckhdid_zero, sizeof(*chid)) == 0)
272 chid = NULL;
273
274 mutex_lock(&wusbhc->mutex);
275 if (chid) {
276 if (wusbhc->active) {
277 mutex_unlock(&wusbhc->mutex);
278 return -EBUSY;
279 }
280 wusbhc->chid = *chid;
281 }
282
283
284
285 if ((chid) && (wusbhc->uwb_rc == NULL)) {
286 wusbhc->uwb_rc = uwb_rc_get_by_grandpa(wusbhc->dev->parent);
287 if (wusbhc->uwb_rc == NULL) {
288 result = -ENODEV;
289 dev_err(wusbhc->dev,
290 "Cannot get associated UWB Host Controller\n");
291 goto error_rc_get;
292 }
293
294 result = wusbhc_pal_register(wusbhc);
295 if (result < 0) {
296 dev_err(wusbhc->dev, "Cannot register as a UWB PAL\n");
297 goto error_pal_register;
298 }
299 }
300 mutex_unlock(&wusbhc->mutex);
301
302 if (chid)
303 result = uwb_radio_start(&wusbhc->pal);
304 else if (wusbhc->uwb_rc)
305 uwb_radio_stop(&wusbhc->pal);
306
307 return result;
308
309error_pal_register:
310 uwb_rc_put(wusbhc->uwb_rc);
311 wusbhc->uwb_rc = NULL;
312error_rc_get:
313 mutex_unlock(&wusbhc->mutex);
314
315 return result;
316}
317EXPORT_SYMBOL_GPL(wusbhc_chid_set);
318