blob: ebd07e906906465ae0455020fd2acc2c5443515a [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +01002/*
3 * Wireless USB Host Controller
4 * sysfs glue, wusbcore module support and life cycle management
5 *
6 *
7 * Copyright (C) 2005-2006 Intel Corporation
8 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version
12 * 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 *
24 *
25 * Creation/destruction of wusbhc is split in two parts; that that
26 * doesn't require the HCD to be added (wusbhc_{create,destroy}) and
27 * the one that requires (phase B, wusbhc_b_{create,destroy}).
28 *
29 * This is so because usb_add_hcd() will start the HC, and thus, all
Uwe Kleine-König421f91d2010-06-11 12:17:00 +020030 * the HC specific stuff has to be already initialized (like sysfs
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010031 * thingies).
32 */
33#include <linux/device.h>
34#include <linux/module.h>
35#include "wusbhc.h"
36
37/**
38 * Extract the wusbhc that corresponds to a USB Host Controller class device
39 *
40 * WARNING! Apply only if @dev is that of a
41 * wusbhc.usb_hcd.self->class_dev; otherwise, you loose.
42 */
43static struct wusbhc *usbhc_dev_to_wusbhc(struct device *dev)
44{
45 struct usb_bus *usb_bus = dev_get_drvdata(dev);
46 struct usb_hcd *usb_hcd = bus_to_hcd(usb_bus);
47 return usb_hcd_to_wusbhc(usb_hcd);
48}
49
50/*
51 * Show & store the current WUSB trust timeout
52 *
53 * We don't do locking--it is an 'atomic' value.
54 *
55 * The units that we store/show are always MILLISECONDS. However, the
56 * value of trust_timeout is jiffies.
57 */
58static ssize_t wusb_trust_timeout_show(struct device *dev,
Rahul Bedarkara7737e32014-01-04 14:13:10 +053059 struct device_attribute *attr,
60 char *buf)
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010061{
62 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
63
64 return scnprintf(buf, PAGE_SIZE, "%u\n", wusbhc->trust_timeout);
65}
66
67static ssize_t wusb_trust_timeout_store(struct device *dev,
68 struct device_attribute *attr,
69 const char *buf, size_t size)
70{
71 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
72 ssize_t result = -ENOSYS;
73 unsigned trust_timeout;
74
75 result = sscanf(buf, "%u", &trust_timeout);
76 if (result != 1) {
77 result = -EINVAL;
78 goto out;
79 }
Thomas Pugliese7d9852a2013-06-06 10:40:49 -050080 wusbhc->trust_timeout = min_t(unsigned, trust_timeout, 500);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010081 cancel_delayed_work(&wusbhc->keep_alive_timer);
82 flush_workqueue(wusbd);
83 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
Thomas Pugliese7d9852a2013-06-06 10:40:49 -050084 msecs_to_jiffies(wusbhc->trust_timeout / 2));
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010085out:
86 return result < 0 ? result : size;
87}
Julia Lawall42cda7d2016-10-29 21:36:56 +020088static DEVICE_ATTR_RW(wusb_trust_timeout);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010089
90/*
David Vrabelfca10c82009-04-08 17:36:30 +000091 * Show the current WUSB CHID.
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010092 */
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);
David Vrabelfca10c82009-04-08 17:36:30 +000097 const struct wusb_ckhdid *chid;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010098 ssize_t result = 0;
99
100 if (wusbhc->wuie_host_info != NULL)
David Vrabelfca10c82009-04-08 17:36:30 +0000101 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
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100108 return result;
109}
110
111/*
David Vrabelfca10c82009-04-08 17:36:30 +0000112 * Store a new CHID.
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100113 *
David Vrabelfca10c82009-04-08 17:36:30 +0000114 * - Write an all zeros CHID and it will stop the controller
115 * - Write a non-zero CHID and it will start it.
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100116 *
117 * See wusbhc_chid_set() for more info.
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}
Julia Lawall42cda7d2016-10-29 21:36:56 +0200148static DEVICE_ATTR_RW(wusb_chid);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100149
David Vrabelc3f22d92009-10-12 15:45:18 +0000150
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}
Julia Lawall42cda7d2016-10-29 21:36:56 +0200177static DEVICE_ATTR_RW(wusb_phy_rate);
David Vrabelc3f22d92009-10-12 15:45:18 +0000178
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500179static ssize_t wusb_dnts_show(struct device *dev,
180 struct device_attribute *attr,
181 char *buf)
182{
183 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
184
185 return sprintf(buf, "num slots: %d\ninterval: %dms\n",
186 wusbhc->dnts_num_slots, wusbhc->dnts_interval);
187}
188
189static ssize_t wusb_dnts_store(struct device *dev,
190 struct device_attribute *attr,
191 const char *buf, size_t size)
192{
193 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
194 uint8_t num_slots, interval;
195 ssize_t result;
196
197 result = sscanf(buf, "%hhu %hhu", &num_slots, &interval);
198
199 if (result != 2)
200 return -EINVAL;
201
202 wusbhc->dnts_num_slots = num_slots;
203 wusbhc->dnts_interval = interval;
204
205 return size;
206}
Julia Lawall42cda7d2016-10-29 21:36:56 +0200207static DEVICE_ATTR_RW(wusb_dnts);
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500208
Thomas Pugliesef265d4d2013-06-18 13:31:26 -0500209static ssize_t wusb_retry_count_show(struct device *dev,
210 struct device_attribute *attr,
211 char *buf)
212{
213 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
214
215 return sprintf(buf, "%d\n", wusbhc->retry_count);
216}
217
218static ssize_t wusb_retry_count_store(struct device *dev,
219 struct device_attribute *attr,
220 const char *buf, size_t size)
221{
222 struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
223 uint8_t retry_count;
224 ssize_t result;
225
226 result = sscanf(buf, "%hhu", &retry_count);
227
228 if (result != 1)
229 return -EINVAL;
230
Rahul Bedarkara7737e32014-01-04 14:13:10 +0530231 wusbhc->retry_count = max_t(uint8_t, retry_count,
232 WUSB_RETRY_COUNT_MAX);
Thomas Pugliesef265d4d2013-06-18 13:31:26 -0500233
234 return size;
235}
Julia Lawall42cda7d2016-10-29 21:36:56 +0200236static DEVICE_ATTR_RW(wusb_retry_count);
Thomas Pugliesef265d4d2013-06-18 13:31:26 -0500237
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100238/* Group all the WUSBHC attributes */
239static struct attribute *wusbhc_attrs[] = {
240 &dev_attr_wusb_trust_timeout.attr,
241 &dev_attr_wusb_chid.attr,
David Vrabelc3f22d92009-10-12 15:45:18 +0000242 &dev_attr_wusb_phy_rate.attr,
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500243 &dev_attr_wusb_dnts.attr,
Thomas Pugliesef265d4d2013-06-18 13:31:26 -0500244 &dev_attr_wusb_retry_count.attr,
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100245 NULL,
246};
247
Arvind Yadav4b514252017-08-04 17:36:47 +0530248static const struct attribute_group wusbhc_attr_group = {
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100249 .name = NULL, /* we want them in the same directory */
250 .attrs = wusbhc_attrs,
251};
252
253/*
254 * Create a wusbhc instance
255 *
256 * NOTEs:
257 *
258 * - assumes *wusbhc has been zeroed and wusbhc->usb_hcd has been
259 * initialized but not added.
260 *
261 * - fill out ports_max, mmcies_max and mmcie_{add,rm} before calling.
262 *
263 * - fill out wusbhc->uwb_rc and refcount it before calling
264 * - fill out the wusbhc->sec_modes array
265 */
266int wusbhc_create(struct wusbhc *wusbhc)
267{
268 int result = 0;
269
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500270 /* set defaults. These can be overwritten using sysfs attributes. */
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100271 wusbhc->trust_timeout = WUSB_TRUST_TIMEOUT_MS;
David Vrabelc3f22d92009-10-12 15:45:18 +0000272 wusbhc->phy_rate = UWB_PHY_RATE_INVALID - 1;
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500273 wusbhc->dnts_num_slots = 4;
274 wusbhc->dnts_interval = 2;
Thomas Pugliesef265d4d2013-06-18 13:31:26 -0500275 wusbhc->retry_count = WUSB_RETRY_COUNT_INFINITE;
David Vrabelc3f22d92009-10-12 15:45:18 +0000276
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100277 mutex_init(&wusbhc->mutex);
278 result = wusbhc_mmcie_create(wusbhc);
279 if (result < 0)
280 goto error_mmcie_create;
281 result = wusbhc_devconnect_create(wusbhc);
282 if (result < 0)
283 goto error_devconnect_create;
284 result = wusbhc_rh_create(wusbhc);
285 if (result < 0)
286 goto error_rh_create;
287 result = wusbhc_sec_create(wusbhc);
288 if (result < 0)
289 goto error_sec_create;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100290 return 0;
291
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100292error_sec_create:
293 wusbhc_rh_destroy(wusbhc);
294error_rh_create:
295 wusbhc_devconnect_destroy(wusbhc);
296error_devconnect_create:
297 wusbhc_mmcie_destroy(wusbhc);
298error_mmcie_create:
299 return result;
300}
301EXPORT_SYMBOL_GPL(wusbhc_create);
302
303static inline struct kobject *wusbhc_kobj(struct wusbhc *wusbhc)
304{
305 return &wusbhc->usb_hcd.self.controller->kobj;
306}
307
308/*
309 * Phase B of a wusbhc instance creation
310 *
311 * Creates fields that depend on wusbhc->usb_hcd having been
312 * added. This is where we create the sysfs files in
313 * /sys/class/usb_host/usb_hostX/.
314 *
315 * NOTE: Assumes wusbhc->usb_hcd has been already added by the upper
316 * layer (hwahc or whci)
317 */
318int wusbhc_b_create(struct wusbhc *wusbhc)
319{
320 int result = 0;
321 struct device *dev = wusbhc->usb_hcd.self.controller;
322
323 result = sysfs_create_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
324 if (result < 0) {
Rahul Bedarkara7737e32014-01-04 14:13:10 +0530325 dev_err(dev, "Cannot register WUSBHC attributes: %d\n",
326 result);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100327 goto error_create_attr_group;
328 }
David Vrabelb60066c2008-09-17 16:34:40 +0100329
David Vrabelb60066c2008-09-17 16:34:40 +0100330 return 0;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100331error_create_attr_group:
332 return result;
333}
334EXPORT_SYMBOL_GPL(wusbhc_b_create);
335
336void wusbhc_b_destroy(struct wusbhc *wusbhc)
337{
David Vrabelb60066c2008-09-17 16:34:40 +0100338 wusbhc_pal_unregister(wusbhc);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100339 sysfs_remove_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
340}
341EXPORT_SYMBOL_GPL(wusbhc_b_destroy);
342
343void wusbhc_destroy(struct wusbhc *wusbhc)
344{
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100345 wusbhc_sec_destroy(wusbhc);
346 wusbhc_rh_destroy(wusbhc);
347 wusbhc_devconnect_destroy(wusbhc);
348 wusbhc_mmcie_destroy(wusbhc);
349}
350EXPORT_SYMBOL_GPL(wusbhc_destroy);
351
352struct workqueue_struct *wusbd;
353EXPORT_SYMBOL_GPL(wusbd);
354
355/*
356 * WUSB Cluster ID allocation map
357 *
358 * Each WUSB bus in a channel is identified with a Cluster Id in the
359 * unauth address pace (WUSB1.0[4.3]). We take the range 0xe0 to 0xff
360 * (that's space for 31 WUSB controllers, as 0xff can't be taken). We
361 * start taking from 0xff, 0xfe, 0xfd... (hence the += or -= 0xff).
362 *
363 * For each one we taken, we pin it in the bitap
364 */
365#define CLUSTER_IDS 32
366static DECLARE_BITMAP(wusb_cluster_id_table, CLUSTER_IDS);
367static DEFINE_SPINLOCK(wusb_cluster_ids_lock);
368
369/*
370 * Get a WUSB Cluster ID
371 *
372 * Need to release with wusb_cluster_id_put() when done w/ it.
373 */
374/* FIXME: coordinate with the choose_addres() from the USB stack */
375/* we want to leave the top of the 128 range for cluster addresses and
376 * the bottom for device addresses (as we map them one on one with
377 * ports). */
378u8 wusb_cluster_id_get(void)
379{
380 u8 id;
381 spin_lock(&wusb_cluster_ids_lock);
382 id = find_first_zero_bit(wusb_cluster_id_table, CLUSTER_IDS);
Akinobu Mita962f3ff2011-03-02 20:35:28 +0900383 if (id >= CLUSTER_IDS) {
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100384 id = 0;
385 goto out;
386 }
387 set_bit(id, wusb_cluster_id_table);
388 id = (u8) 0xff - id;
389out:
390 spin_unlock(&wusb_cluster_ids_lock);
391 return id;
392
393}
394EXPORT_SYMBOL_GPL(wusb_cluster_id_get);
395
396/*
397 * Release a WUSB Cluster ID
398 *
399 * Obtained it with wusb_cluster_id_get()
400 */
401void wusb_cluster_id_put(u8 id)
402{
403 id = 0xff - id;
404 BUG_ON(id >= CLUSTER_IDS);
405 spin_lock(&wusb_cluster_ids_lock);
406 WARN_ON(!test_bit(id, wusb_cluster_id_table));
407 clear_bit(id, wusb_cluster_id_table);
408 spin_unlock(&wusb_cluster_ids_lock);
409}
410EXPORT_SYMBOL_GPL(wusb_cluster_id_put);
411
412/**
413 * wusbhc_giveback_urb - return an URB to the USB core
414 * @wusbhc: the host controller the URB is from.
415 * @urb: the URB.
416 * @status: the URB's status.
417 *
418 * Return an URB to the USB core doing some additional WUSB specific
419 * processing.
420 *
421 * - After a successful transfer, update the trust timeout timestamp
422 * for the WUSB device.
423 *
Rahul Bedarkar1076e7a2014-01-04 12:37:52 +0530424 * - [WUSB] sections 4.13 and 7.5.1 specify the stop retransmission
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100425 * condition for the WCONNECTACK_IE is that the host has observed
426 * the associated device responding to a control transfer.
427 */
428void wusbhc_giveback_urb(struct wusbhc *wusbhc, struct urb *urb, int status)
429{
Rahul Bedarkara7737e32014-01-04 14:13:10 +0530430 struct wusb_dev *wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc,
431 urb->dev);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100432
David Vrabel5936ac72009-04-08 17:36:32 +0000433 if (status == 0 && wusb_dev) {
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100434 wusb_dev->entry_ts = jiffies;
435
David Vrabel5936ac72009-04-08 17:36:32 +0000436 /* wusbhc_devconnect_acked() can't be called from
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100437 atomic context so defer it to a work queue. */
438 if (!list_empty(&wusb_dev->cack_node))
439 queue_work(wusbd, &wusb_dev->devconnect_acked_work);
David Vrabel5936ac72009-04-08 17:36:32 +0000440 else
441 wusb_dev_put(wusb_dev);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100442 }
443
444 usb_hcd_giveback_urb(&wusbhc->usb_hcd, urb, status);
445}
446EXPORT_SYMBOL_GPL(wusbhc_giveback_urb);
447
448/**
449 * wusbhc_reset_all - reset the HC hardware
450 * @wusbhc: the host controller to reset.
451 *
452 * Request a full hardware reset of the chip. This will also reset
453 * the radio controller and any other PALs.
454 */
455void wusbhc_reset_all(struct wusbhc *wusbhc)
456{
Thomas Pugliesea8995752013-06-24 14:26:35 -0500457 if (wusbhc->uwb_rc)
458 uwb_rc_reset_all(wusbhc->uwb_rc);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100459}
460EXPORT_SYMBOL_GPL(wusbhc_reset_all);
461
462static struct notifier_block wusb_usb_notifier = {
463 .notifier_call = wusb_usb_ncb,
464 .priority = INT_MAX /* Need to be called first of all */
465};
466
467static int __init wusbcore_init(void)
468{
469 int result;
470 result = wusb_crypto_init();
471 if (result < 0)
472 goto error_crypto_init;
473 /* WQ is singlethread because we need to serialize notifications */
474 wusbd = create_singlethread_workqueue("wusbd");
475 if (wusbd == NULL) {
476 result = -ENOMEM;
477 printk(KERN_ERR "WUSB-core: Cannot create wusbd workqueue\n");
478 goto error_wusbd_create;
479 }
480 usb_register_notify(&wusb_usb_notifier);
481 bitmap_zero(wusb_cluster_id_table, CLUSTER_IDS);
482 set_bit(0, wusb_cluster_id_table); /* reserve Cluster ID 0xff */
483 return 0;
484
485error_wusbd_create:
486 wusb_crypto_exit();
487error_crypto_init:
488 return result;
489
490}
491module_init(wusbcore_init);
492
493static void __exit wusbcore_exit(void)
494{
495 clear_bit(0, wusb_cluster_id_table);
496 if (!bitmap_empty(wusb_cluster_id_table, CLUSTER_IDS)) {
Tejun Heo125918d2015-02-13 14:37:53 -0800497 printk(KERN_ERR "BUG: WUSB Cluster IDs not released on exit: %*pb\n",
498 CLUSTER_IDS, wusb_cluster_id_table);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100499 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");