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