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#include <linux/device.h>
33#include <linux/module.h>
34#include "wusbhc.h"
35
36
37
38
39
40
41
42static struct wusbhc *usbhc_dev_to_wusbhc(struct device *dev)
43{
44 struct usb_bus *usb_bus = dev_get_drvdata(dev);
45 struct usb_hcd *usb_hcd = bus_to_hcd(usb_bus);
46 return usb_hcd_to_wusbhc(usb_hcd);
47}
48
49
50
51
52
53
54
55
56
57static ssize_t wusb_trust_timeout_show(struct device *dev,
58 struct device_attribute *attr, char *buf)
59{
60 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
61
62 return scnprintf(buf, PAGE_SIZE, "%u\n", wusbhc->trust_timeout);
63}
64
65static ssize_t wusb_trust_timeout_store(struct device *dev,
66 struct device_attribute *attr,
67 const char *buf, size_t size)
68{
69 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
70 ssize_t result = -ENOSYS;
71 unsigned trust_timeout;
72
73 result = sscanf(buf, "%u", &trust_timeout);
74 if (result != 1) {
75 result = -EINVAL;
76 goto out;
77 }
78 wusbhc->trust_timeout = min_t(unsigned, trust_timeout, 500);
79 cancel_delayed_work(&wusbhc->keep_alive_timer);
80 flush_workqueue(wusbd);
81 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
82 msecs_to_jiffies(wusbhc->trust_timeout / 2));
83out:
84 return result < 0 ? result : size;
85}
86static DEVICE_ATTR(wusb_trust_timeout, 0644, wusb_trust_timeout_show,
87 wusb_trust_timeout_store);
88
89
90
91
92static ssize_t wusb_chid_show(struct device *dev,
93 struct device_attribute *attr, char *buf)
94{
95 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
96 const struct wusb_ckhdid *chid;
97 ssize_t result = 0;
98
99 if (wusbhc->wuie_host_info != NULL)
100 chid = &wusbhc->wuie_host_info->CHID;
101 else
102 chid = &wusb_ckhdid_zero;
103
104 result += ckhdid_printf(buf, PAGE_SIZE, chid);
105 result += sprintf(buf + result, "\n");
106
107 return result;
108}
109
110
111
112
113
114
115
116
117
118static ssize_t wusb_chid_store(struct device *dev,
119 struct device_attribute *attr,
120 const char *buf, size_t size)
121{
122 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
123 struct wusb_ckhdid chid;
124 ssize_t result;
125
126 result = sscanf(buf,
127 "%02hhx %02hhx %02hhx %02hhx "
128 "%02hhx %02hhx %02hhx %02hhx "
129 "%02hhx %02hhx %02hhx %02hhx "
130 "%02hhx %02hhx %02hhx %02hhx\n",
131 &chid.data[0] , &chid.data[1] ,
132 &chid.data[2] , &chid.data[3] ,
133 &chid.data[4] , &chid.data[5] ,
134 &chid.data[6] , &chid.data[7] ,
135 &chid.data[8] , &chid.data[9] ,
136 &chid.data[10], &chid.data[11],
137 &chid.data[12], &chid.data[13],
138 &chid.data[14], &chid.data[15]);
139 if (result != 16) {
140 dev_err(dev, "Unrecognized CHID (need 16 8-bit hex digits): "
141 "%d\n", (int)result);
142 return -EINVAL;
143 }
144 result = wusbhc_chid_set(wusbhc, &chid);
145 return result < 0 ? result : size;
146}
147static DEVICE_ATTR(wusb_chid, 0644, wusb_chid_show, wusb_chid_store);
148
149
150static ssize_t wusb_phy_rate_show(struct device *dev,
151 struct device_attribute *attr,
152 char *buf)
153{
154 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
155
156 return sprintf(buf, "%d\n", wusbhc->phy_rate);
157}
158
159static ssize_t wusb_phy_rate_store(struct device *dev,
160 struct device_attribute *attr,
161 const char *buf, size_t size)
162{
163 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
164 uint8_t phy_rate;
165 ssize_t result;
166
167 result = sscanf(buf, "%hhu", &phy_rate);
168 if (result != 1)
169 return -EINVAL;
170 if (phy_rate >= UWB_PHY_RATE_INVALID)
171 return -EINVAL;
172
173 wusbhc->phy_rate = phy_rate;
174 return size;
175}
176static DEVICE_ATTR(wusb_phy_rate, 0644, wusb_phy_rate_show, wusb_phy_rate_store);
177
178static ssize_t wusb_dnts_show(struct device *dev,
179 struct device_attribute *attr,
180 char *buf)
181{
182 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
183
184 return sprintf(buf, "num slots: %d\ninterval: %dms\n",
185 wusbhc->dnts_num_slots, wusbhc->dnts_interval);
186}
187
188static ssize_t wusb_dnts_store(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t size)
191{
192 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
193 uint8_t num_slots, interval;
194 ssize_t result;
195
196 result = sscanf(buf, "%hhu %hhu", &num_slots, &interval);
197
198 if (result != 2)
199 return -EINVAL;
200
201 wusbhc->dnts_num_slots = num_slots;
202 wusbhc->dnts_interval = interval;
203
204 return size;
205}
206static DEVICE_ATTR(wusb_dnts, 0644, wusb_dnts_show, wusb_dnts_store);
207
208static ssize_t wusb_retry_count_show(struct device *dev,
209 struct device_attribute *attr,
210 char *buf)
211{
212 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
213
214 return sprintf(buf, "%d\n", wusbhc->retry_count);
215}
216
217static ssize_t wusb_retry_count_store(struct device *dev,
218 struct device_attribute *attr,
219 const char *buf, size_t size)
220{
221 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
222 uint8_t retry_count;
223 ssize_t result;
224
225 result = sscanf(buf, "%hhu", &retry_count);
226
227 if (result != 1)
228 return -EINVAL;
229
230 wusbhc->retry_count = max_t(uint8_t, retry_count, WUSB_RETRY_COUNT_MAX);
231
232 return size;
233}
234static DEVICE_ATTR(wusb_retry_count, 0644, wusb_retry_count_show,
235 wusb_retry_count_store);
236
237
238static struct attribute *wusbhc_attrs[] = {
239 &dev_attr_wusb_trust_timeout.attr,
240 &dev_attr_wusb_chid.attr,
241 &dev_attr_wusb_phy_rate.attr,
242 &dev_attr_wusb_dnts.attr,
243 &dev_attr_wusb_retry_count.attr,
244 NULL,
245};
246
247static struct attribute_group wusbhc_attr_group = {
248 .name = NULL,
249 .attrs = wusbhc_attrs,
250};
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265int wusbhc_create(struct wusbhc *wusbhc)
266{
267 int result = 0;
268
269
270 wusbhc->trust_timeout = WUSB_TRUST_TIMEOUT_MS;
271 wusbhc->phy_rate = UWB_PHY_RATE_INVALID - 1;
272 wusbhc->dnts_num_slots = 4;
273 wusbhc->dnts_interval = 2;
274 wusbhc->retry_count = WUSB_RETRY_COUNT_INFINITE;
275
276 mutex_init(&wusbhc->mutex);
277 result = wusbhc_mmcie_create(wusbhc);
278 if (result < 0)
279 goto error_mmcie_create;
280 result = wusbhc_devconnect_create(wusbhc);
281 if (result < 0)
282 goto error_devconnect_create;
283 result = wusbhc_rh_create(wusbhc);
284 if (result < 0)
285 goto error_rh_create;
286 result = wusbhc_sec_create(wusbhc);
287 if (result < 0)
288 goto error_sec_create;
289 return 0;
290
291error_sec_create:
292 wusbhc_rh_destroy(wusbhc);
293error_rh_create:
294 wusbhc_devconnect_destroy(wusbhc);
295error_devconnect_create:
296 wusbhc_mmcie_destroy(wusbhc);
297error_mmcie_create:
298 return result;
299}
300EXPORT_SYMBOL_GPL(wusbhc_create);
301
302static inline struct kobject *wusbhc_kobj(struct wusbhc *wusbhc)
303{
304 return &wusbhc->usb_hcd.self.controller->kobj;
305}
306
307
308
309
310
311
312
313
314
315
316
317int wusbhc_b_create(struct wusbhc *wusbhc)
318{
319 int result = 0;
320 struct device *dev = wusbhc->usb_hcd.self.controller;
321
322 result = sysfs_create_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
323 if (result < 0) {
324 dev_err(dev, "Cannot register WUSBHC attributes: %d\n", result);
325 goto error_create_attr_group;
326 }
327
328 return 0;
329error_create_attr_group:
330 return result;
331}
332EXPORT_SYMBOL_GPL(wusbhc_b_create);
333
334void wusbhc_b_destroy(struct wusbhc *wusbhc)
335{
336 wusbhc_pal_unregister(wusbhc);
337 sysfs_remove_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
338}
339EXPORT_SYMBOL_GPL(wusbhc_b_destroy);
340
341void wusbhc_destroy(struct wusbhc *wusbhc)
342{
343 wusbhc_sec_destroy(wusbhc);
344 wusbhc_rh_destroy(wusbhc);
345 wusbhc_devconnect_destroy(wusbhc);
346 wusbhc_mmcie_destroy(wusbhc);
347}
348EXPORT_SYMBOL_GPL(wusbhc_destroy);
349
350struct workqueue_struct *wusbd;
351EXPORT_SYMBOL_GPL(wusbd);
352
353
354
355
356
357
358
359
360
361
362
363#define CLUSTER_IDS 32
364static DECLARE_BITMAP(wusb_cluster_id_table, CLUSTER_IDS);
365static DEFINE_SPINLOCK(wusb_cluster_ids_lock);
366
367
368
369
370
371
372
373
374
375
376u8 wusb_cluster_id_get(void)
377{
378 u8 id;
379 spin_lock(&wusb_cluster_ids_lock);
380 id = find_first_zero_bit(wusb_cluster_id_table, CLUSTER_IDS);
381 if (id >= CLUSTER_IDS) {
382 id = 0;
383 goto out;
384 }
385 set_bit(id, wusb_cluster_id_table);
386 id = (u8) 0xff - id;
387out:
388 spin_unlock(&wusb_cluster_ids_lock);
389 return id;
390
391}
392EXPORT_SYMBOL_GPL(wusb_cluster_id_get);
393
394
395
396
397
398
399void wusb_cluster_id_put(u8 id)
400{
401 id = 0xff - id;
402 BUG_ON(id >= CLUSTER_IDS);
403 spin_lock(&wusb_cluster_ids_lock);
404 WARN_ON(!test_bit(id, wusb_cluster_id_table));
405 clear_bit(id, wusb_cluster_id_table);
406 spin_unlock(&wusb_cluster_ids_lock);
407}
408EXPORT_SYMBOL_GPL(wusb_cluster_id_put);
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426void wusbhc_giveback_urb(struct wusbhc *wusbhc, struct urb *urb, int status)
427{
428 struct wusb_dev *wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
429
430 if (status == 0 && wusb_dev) {
431 wusb_dev->entry_ts = jiffies;
432
433
434
435 if (!list_empty(&wusb_dev->cack_node))
436 queue_work(wusbd, &wusb_dev->devconnect_acked_work);
437 else
438 wusb_dev_put(wusb_dev);
439 }
440
441 usb_hcd_giveback_urb(&wusbhc->usb_hcd, urb, status);
442}
443EXPORT_SYMBOL_GPL(wusbhc_giveback_urb);
444
445
446
447
448
449
450
451
452void wusbhc_reset_all(struct wusbhc *wusbhc)
453{
454 if (wusbhc->uwb_rc)
455 uwb_rc_reset_all(wusbhc->uwb_rc);
456}
457EXPORT_SYMBOL_GPL(wusbhc_reset_all);
458
459static struct notifier_block wusb_usb_notifier = {
460 .notifier_call = wusb_usb_ncb,
461 .priority = INT_MAX
462};
463
464static int __init wusbcore_init(void)
465{
466 int result;
467 result = wusb_crypto_init();
468 if (result < 0)
469 goto error_crypto_init;
470
471 wusbd = create_singlethread_workqueue("wusbd");
472 if (wusbd == NULL) {
473 result = -ENOMEM;
474 printk(KERN_ERR "WUSB-core: Cannot create wusbd workqueue\n");
475 goto error_wusbd_create;
476 }
477 usb_register_notify(&wusb_usb_notifier);
478 bitmap_zero(wusb_cluster_id_table, CLUSTER_IDS);
479 set_bit(0, wusb_cluster_id_table);
480 return 0;
481
482error_wusbd_create:
483 wusb_crypto_exit();
484error_crypto_init:
485 return result;
486
487}
488module_init(wusbcore_init);
489
490static void __exit wusbcore_exit(void)
491{
492 clear_bit(0, wusb_cluster_id_table);
493 if (!bitmap_empty(wusb_cluster_id_table, CLUSTER_IDS)) {
494 char buf[256];
495 bitmap_scnprintf(buf, sizeof(buf), wusb_cluster_id_table,
496 CLUSTER_IDS);
497 printk(KERN_ERR "BUG: WUSB Cluster IDs not released "
498 "on exit: %s\n", buf);
499 WARN_ON(1);
500 }
501 usb_unregister_notify(&wusb_usb_notifier);
502 destroy_workqueue(wusbd);
503 wusb_crypto_exit();
504}
505module_exit(wusbcore_exit);
506
507MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
508MODULE_DESCRIPTION("Wireless USB core");
509MODULE_LICENSE("GPL");
510