blob: 8759aa66da2827975f529d068d48370ec5258173 [file] [log] [blame]
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +01001/*
2 * Wireless USB Host Controller
3 * sysfs glue, wusbcore module support and life cycle management
4 *
5 *
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 *
23 *
24 * Creation/destruction of wusbhc is split in two parts; that that
25 * doesn't require the HCD to be added (wusbhc_{create,destroy}) and
26 * the one that requires (phase B, wusbhc_b_{create,destroy}).
27 *
28 * This is so because usb_add_hcd() will start the HC, and thus, all
Uwe Kleine-König421f91d2010-06-11 12:17:00 +020029 * the HC specific stuff has to be already initialized (like sysfs
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +010030 * thingies).
31 */
32#include <linux/device.h>
33#include <linux/module.h>
34#include "wusbhc.h"
35
36/**
37 * Extract the wusbhc that corresponds to a USB Host Controller class device
38 *
39 * WARNING! Apply only if @dev is that of a
40 * wusbhc.usb_hcd.self->class_dev; otherwise, you loose.
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 * Show & store the current WUSB trust timeout
51 *
52 * We don't do locking--it is an 'atomic' value.
53 *
54 * The units that we store/show are always MILLISECONDS. However, the
55 * value of trust_timeout is jiffies.
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 }
Thomas Pugliese7d9852a2013-06-06 10:40:49 -050078 wusbhc->trust_timeout = min_t(unsigned, trust_timeout, 500);
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +010079 cancel_delayed_work(&wusbhc->keep_alive_timer);
80 flush_workqueue(wusbd);
81 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
Thomas Pugliese7d9852a2013-06-06 10:40:49 -050082 msecs_to_jiffies(wusbhc->trust_timeout / 2));
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +010083out:
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/*
David Vrabelfca10c82009-04-08 17:36:30 +000090 * Show the current WUSB CHID.
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +010091 */
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);
David Vrabelfca10c82009-04-08 17:36:30 +000096 const struct wusb_ckhdid *chid;
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +010097 ssize_t result = 0;
98
99 if (wusbhc->wuie_host_info != NULL)
David Vrabelfca10c82009-04-08 17:36:30 +0000100 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
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100107 return result;
108}
109
110/*
David Vrabelfca10c82009-04-08 17:36:30 +0000111 * Store a new CHID.
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100112 *
David Vrabelfca10c82009-04-08 17:36:30 +0000113 * - Write an all zeros CHID and it will stop the controller
114 * - Write a non-zero CHID and it will start it.
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100115 *
116 * See wusbhc_chid_set() for more info.
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
David Vrabelc3f22d92009-10-12 15:45:18 +0000149
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
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500178static 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
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100208/* Group all the WUSBHC attributes */
209static struct attribute *wusbhc_attrs[] = {
210 &dev_attr_wusb_trust_timeout.attr,
211 &dev_attr_wusb_chid.attr,
David Vrabelc3f22d92009-10-12 15:45:18 +0000212 &dev_attr_wusb_phy_rate.attr,
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500213 &dev_attr_wusb_dnts.attr,
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100214 NULL,
215};
216
217static struct attribute_group wusbhc_attr_group = {
218 .name = NULL, /* we want them in the same directory */
219 .attrs = wusbhc_attrs,
220};
221
222/*
223 * Create a wusbhc instance
224 *
225 * NOTEs:
226 *
227 * - assumes *wusbhc has been zeroed and wusbhc->usb_hcd has been
228 * initialized but not added.
229 *
230 * - fill out ports_max, mmcies_max and mmcie_{add,rm} before calling.
231 *
232 * - fill out wusbhc->uwb_rc and refcount it before calling
233 * - fill out the wusbhc->sec_modes array
234 */
235int wusbhc_create(struct wusbhc *wusbhc)
236{
237 int result = 0;
238
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500239 /* set defaults. These can be overwritten using sysfs attributes. */
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100240 wusbhc->trust_timeout = WUSB_TRUST_TIMEOUT_MS;
David Vrabelc3f22d92009-10-12 15:45:18 +0000241 wusbhc->phy_rate = UWB_PHY_RATE_INVALID - 1;
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500242 wusbhc->dnts_num_slots = 4;
243 wusbhc->dnts_interval = 2;
David Vrabelc3f22d92009-10-12 15:45:18 +0000244
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100245 mutex_init(&wusbhc->mutex);
246 result = wusbhc_mmcie_create(wusbhc);
247 if (result < 0)
248 goto error_mmcie_create;
249 result = wusbhc_devconnect_create(wusbhc);
250 if (result < 0)
251 goto error_devconnect_create;
252 result = wusbhc_rh_create(wusbhc);
253 if (result < 0)
254 goto error_rh_create;
255 result = wusbhc_sec_create(wusbhc);
256 if (result < 0)
257 goto error_sec_create;
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100258 return 0;
259
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100260error_sec_create:
261 wusbhc_rh_destroy(wusbhc);
262error_rh_create:
263 wusbhc_devconnect_destroy(wusbhc);
264error_devconnect_create:
265 wusbhc_mmcie_destroy(wusbhc);
266error_mmcie_create:
267 return result;
268}
269EXPORT_SYMBOL_GPL(wusbhc_create);
270
271static inline struct kobject *wusbhc_kobj(struct wusbhc *wusbhc)
272{
273 return &wusbhc->usb_hcd.self.controller->kobj;
274}
275
276/*
277 * Phase B of a wusbhc instance creation
278 *
279 * Creates fields that depend on wusbhc->usb_hcd having been
280 * added. This is where we create the sysfs files in
281 * /sys/class/usb_host/usb_hostX/.
282 *
283 * NOTE: Assumes wusbhc->usb_hcd has been already added by the upper
284 * layer (hwahc or whci)
285 */
286int wusbhc_b_create(struct wusbhc *wusbhc)
287{
288 int result = 0;
289 struct device *dev = wusbhc->usb_hcd.self.controller;
290
291 result = sysfs_create_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
292 if (result < 0) {
293 dev_err(dev, "Cannot register WUSBHC attributes: %d\n", result);
294 goto error_create_attr_group;
295 }
David Vrabelb60066c2008-09-17 16:34:40 +0100296
297 result = wusbhc_pal_register(wusbhc);
298 if (result < 0)
299 goto error_pal_register;
300 return 0;
301
302error_pal_register:
303 sysfs_remove_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100304error_create_attr_group:
305 return result;
306}
307EXPORT_SYMBOL_GPL(wusbhc_b_create);
308
309void wusbhc_b_destroy(struct wusbhc *wusbhc)
310{
David Vrabelb60066c2008-09-17 16:34:40 +0100311 wusbhc_pal_unregister(wusbhc);
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100312 sysfs_remove_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
313}
314EXPORT_SYMBOL_GPL(wusbhc_b_destroy);
315
316void wusbhc_destroy(struct wusbhc *wusbhc)
317{
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100318 wusbhc_sec_destroy(wusbhc);
319 wusbhc_rh_destroy(wusbhc);
320 wusbhc_devconnect_destroy(wusbhc);
321 wusbhc_mmcie_destroy(wusbhc);
322}
323EXPORT_SYMBOL_GPL(wusbhc_destroy);
324
325struct workqueue_struct *wusbd;
326EXPORT_SYMBOL_GPL(wusbd);
327
328/*
329 * WUSB Cluster ID allocation map
330 *
331 * Each WUSB bus in a channel is identified with a Cluster Id in the
332 * unauth address pace (WUSB1.0[4.3]). We take the range 0xe0 to 0xff
333 * (that's space for 31 WUSB controllers, as 0xff can't be taken). We
334 * start taking from 0xff, 0xfe, 0xfd... (hence the += or -= 0xff).
335 *
336 * For each one we taken, we pin it in the bitap
337 */
338#define CLUSTER_IDS 32
339static DECLARE_BITMAP(wusb_cluster_id_table, CLUSTER_IDS);
340static DEFINE_SPINLOCK(wusb_cluster_ids_lock);
341
342/*
343 * Get a WUSB Cluster ID
344 *
345 * Need to release with wusb_cluster_id_put() when done w/ it.
346 */
347/* FIXME: coordinate with the choose_addres() from the USB stack */
348/* we want to leave the top of the 128 range for cluster addresses and
349 * the bottom for device addresses (as we map them one on one with
350 * ports). */
351u8 wusb_cluster_id_get(void)
352{
353 u8 id;
354 spin_lock(&wusb_cluster_ids_lock);
355 id = find_first_zero_bit(wusb_cluster_id_table, CLUSTER_IDS);
Akinobu Mita962f3ff2011-03-02 20:35:28 +0900356 if (id >= CLUSTER_IDS) {
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100357 id = 0;
358 goto out;
359 }
360 set_bit(id, wusb_cluster_id_table);
361 id = (u8) 0xff - id;
362out:
363 spin_unlock(&wusb_cluster_ids_lock);
364 return id;
365
366}
367EXPORT_SYMBOL_GPL(wusb_cluster_id_get);
368
369/*
370 * Release a WUSB Cluster ID
371 *
372 * Obtained it with wusb_cluster_id_get()
373 */
374void wusb_cluster_id_put(u8 id)
375{
376 id = 0xff - id;
377 BUG_ON(id >= CLUSTER_IDS);
378 spin_lock(&wusb_cluster_ids_lock);
379 WARN_ON(!test_bit(id, wusb_cluster_id_table));
380 clear_bit(id, wusb_cluster_id_table);
381 spin_unlock(&wusb_cluster_ids_lock);
382}
383EXPORT_SYMBOL_GPL(wusb_cluster_id_put);
384
385/**
386 * wusbhc_giveback_urb - return an URB to the USB core
387 * @wusbhc: the host controller the URB is from.
388 * @urb: the URB.
389 * @status: the URB's status.
390 *
391 * Return an URB to the USB core doing some additional WUSB specific
392 * processing.
393 *
394 * - After a successful transfer, update the trust timeout timestamp
395 * for the WUSB device.
396 *
397 * - [WUSB] sections 4.13 and 7.5.1 specifies the stop retrasmittion
398 * condition for the WCONNECTACK_IE is that the host has observed
399 * the associated device responding to a control transfer.
400 */
401void wusbhc_giveback_urb(struct wusbhc *wusbhc, struct urb *urb, int status)
402{
403 struct wusb_dev *wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
404
David Vrabel5936ac72009-04-08 17:36:32 +0000405 if (status == 0 && wusb_dev) {
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100406 wusb_dev->entry_ts = jiffies;
407
David Vrabel5936ac72009-04-08 17:36:32 +0000408 /* wusbhc_devconnect_acked() can't be called from
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100409 atomic context so defer it to a work queue. */
410 if (!list_empty(&wusb_dev->cack_node))
411 queue_work(wusbd, &wusb_dev->devconnect_acked_work);
David Vrabel5936ac72009-04-08 17:36:32 +0000412 else
413 wusb_dev_put(wusb_dev);
Inaky Perez-Gonzalez90ff96f22008-09-17 16:34:23 +0100414 }
415
416 usb_hcd_giveback_urb(&wusbhc->usb_hcd, urb, status);
417}
418EXPORT_SYMBOL_GPL(wusbhc_giveback_urb);
419
420/**
421 * wusbhc_reset_all - reset the HC hardware
422 * @wusbhc: the host controller to reset.
423 *
424 * Request a full hardware reset of the chip. This will also reset
425 * the radio controller and any other PALs.
426 */
427void wusbhc_reset_all(struct wusbhc *wusbhc)
428{
429 uwb_rc_reset_all(wusbhc->uwb_rc);
430}
431EXPORT_SYMBOL_GPL(wusbhc_reset_all);
432
433static struct notifier_block wusb_usb_notifier = {
434 .notifier_call = wusb_usb_ncb,
435 .priority = INT_MAX /* Need to be called first of all */
436};
437
438static int __init wusbcore_init(void)
439{
440 int result;
441 result = wusb_crypto_init();
442 if (result < 0)
443 goto error_crypto_init;
444 /* WQ is singlethread because we need to serialize notifications */
445 wusbd = create_singlethread_workqueue("wusbd");
446 if (wusbd == NULL) {
447 result = -ENOMEM;
448 printk(KERN_ERR "WUSB-core: Cannot create wusbd workqueue\n");
449 goto error_wusbd_create;
450 }
451 usb_register_notify(&wusb_usb_notifier);
452 bitmap_zero(wusb_cluster_id_table, CLUSTER_IDS);
453 set_bit(0, wusb_cluster_id_table); /* reserve Cluster ID 0xff */
454 return 0;
455
456error_wusbd_create:
457 wusb_crypto_exit();
458error_crypto_init:
459 return result;
460
461}
462module_init(wusbcore_init);
463
464static void __exit wusbcore_exit(void)
465{
466 clear_bit(0, wusb_cluster_id_table);
467 if (!bitmap_empty(wusb_cluster_id_table, CLUSTER_IDS)) {
468 char buf[256];
469 bitmap_scnprintf(buf, sizeof(buf), wusb_cluster_id_table,
470 CLUSTER_IDS);
471 printk(KERN_ERR "BUG: WUSB Cluster IDs not released "
472 "on exit: %s\n", buf);
473 WARN_ON(1);
474 }
475 usb_unregister_notify(&wusb_usb_notifier);
476 destroy_workqueue(wusbd);
477 wusb_crypto_exit();
478}
479module_exit(wusbcore_exit);
480
481MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
482MODULE_DESCRIPTION("Wireless USB core");
483MODULE_LICENSE("GPL");