2 * Wireless USB Host Controller
3 * sysfs glue, wusbcore module support and life cycle management
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
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}).
28 * This is so because usb_add_hcd() will start the HC, and thus, all
29 * the HC specific stuff has to be already initialized (like sysfs
32 #include <linux/device.h>
33 #include <linux/module.h>
37 * Extract the wusbhc that corresponds to a USB Host Controller class device
39 * WARNING! Apply only if @dev is that of a
40 * wusbhc.usb_hcd.self->class_dev; otherwise, you loose.
42 static struct wusbhc
*usbhc_dev_to_wusbhc(struct device
*dev
)
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
);
50 * Show & store the current WUSB trust timeout
52 * We don't do locking--it is an 'atomic' value.
54 * The units that we store/show are always MILLISECONDS. However, the
55 * value of trust_timeout is jiffies.
57 static ssize_t
wusb_trust_timeout_show(struct device
*dev
,
58 struct device_attribute
*attr
,
61 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
63 return scnprintf(buf
, PAGE_SIZE
, "%u\n", wusbhc
->trust_timeout
);
66 static ssize_t
wusb_trust_timeout_store(struct device
*dev
,
67 struct device_attribute
*attr
,
68 const char *buf
, size_t size
)
70 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
71 ssize_t result
= -ENOSYS
;
72 unsigned trust_timeout
;
74 result
= sscanf(buf
, "%u", &trust_timeout
);
79 wusbhc
->trust_timeout
= min_t(unsigned, trust_timeout
, 500);
80 cancel_delayed_work(&wusbhc
->keep_alive_timer
);
81 flush_workqueue(wusbd
);
82 queue_delayed_work(wusbd
, &wusbhc
->keep_alive_timer
,
83 msecs_to_jiffies(wusbhc
->trust_timeout
/ 2));
85 return result
< 0 ? result
: size
;
87 static DEVICE_ATTR(wusb_trust_timeout
, 0644, wusb_trust_timeout_show
,
88 wusb_trust_timeout_store
);
91 * Show the current WUSB CHID.
93 static ssize_t
wusb_chid_show(struct device
*dev
,
94 struct device_attribute
*attr
, char *buf
)
96 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
97 const struct wusb_ckhdid
*chid
;
100 if (wusbhc
->wuie_host_info
!= NULL
)
101 chid
= &wusbhc
->wuie_host_info
->CHID
;
103 chid
= &wusb_ckhdid_zero
;
105 result
+= ckhdid_printf(buf
, PAGE_SIZE
, chid
);
106 result
+= sprintf(buf
+ result
, "\n");
114 * - Write an all zeros CHID and it will stop the controller
115 * - Write a non-zero CHID and it will start it.
117 * See wusbhc_chid_set() for more info.
119 static ssize_t
wusb_chid_store(struct device
*dev
,
120 struct device_attribute
*attr
,
121 const char *buf
, size_t size
)
123 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
124 struct wusb_ckhdid chid
;
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]);
141 dev_err(dev
, "Unrecognized CHID (need 16 8-bit hex digits): "
142 "%d\n", (int)result
);
145 result
= wusbhc_chid_set(wusbhc
, &chid
);
146 return result
< 0 ? result
: size
;
148 static DEVICE_ATTR(wusb_chid
, 0644, wusb_chid_show
, wusb_chid_store
);
151 static ssize_t
wusb_phy_rate_show(struct device
*dev
,
152 struct device_attribute
*attr
,
155 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
157 return sprintf(buf
, "%d\n", wusbhc
->phy_rate
);
160 static ssize_t
wusb_phy_rate_store(struct device
*dev
,
161 struct device_attribute
*attr
,
162 const char *buf
, size_t size
)
164 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
168 result
= sscanf(buf
, "%hhu", &phy_rate
);
171 if (phy_rate
>= UWB_PHY_RATE_INVALID
)
174 wusbhc
->phy_rate
= phy_rate
;
177 static DEVICE_ATTR(wusb_phy_rate
, 0644, wusb_phy_rate_show
,
178 wusb_phy_rate_store
);
180 static ssize_t
wusb_dnts_show(struct device
*dev
,
181 struct device_attribute
*attr
,
184 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
186 return sprintf(buf
, "num slots: %d\ninterval: %dms\n",
187 wusbhc
->dnts_num_slots
, wusbhc
->dnts_interval
);
190 static ssize_t
wusb_dnts_store(struct device
*dev
,
191 struct device_attribute
*attr
,
192 const char *buf
, size_t size
)
194 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
195 uint8_t num_slots
, interval
;
198 result
= sscanf(buf
, "%hhu %hhu", &num_slots
, &interval
);
203 wusbhc
->dnts_num_slots
= num_slots
;
204 wusbhc
->dnts_interval
= interval
;
208 static DEVICE_ATTR(wusb_dnts
, 0644, wusb_dnts_show
, wusb_dnts_store
);
210 static ssize_t
wusb_retry_count_show(struct device
*dev
,
211 struct device_attribute
*attr
,
214 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
216 return sprintf(buf
, "%d\n", wusbhc
->retry_count
);
219 static ssize_t
wusb_retry_count_store(struct device
*dev
,
220 struct device_attribute
*attr
,
221 const char *buf
, size_t size
)
223 struct wusbhc
*wusbhc
= usbhc_dev_to_wusbhc(dev
);
227 result
= sscanf(buf
, "%hhu", &retry_count
);
232 wusbhc
->retry_count
= max_t(uint8_t, retry_count
,
233 WUSB_RETRY_COUNT_MAX
);
237 static DEVICE_ATTR(wusb_retry_count
, 0644, wusb_retry_count_show
,
238 wusb_retry_count_store
);
240 /* Group all the WUSBHC attributes */
241 static struct attribute
*wusbhc_attrs
[] = {
242 &dev_attr_wusb_trust_timeout
.attr
,
243 &dev_attr_wusb_chid
.attr
,
244 &dev_attr_wusb_phy_rate
.attr
,
245 &dev_attr_wusb_dnts
.attr
,
246 &dev_attr_wusb_retry_count
.attr
,
250 static struct attribute_group wusbhc_attr_group
= {
251 .name
= NULL
, /* we want them in the same directory */
252 .attrs
= wusbhc_attrs
,
256 * Create a wusbhc instance
260 * - assumes *wusbhc has been zeroed and wusbhc->usb_hcd has been
261 * initialized but not added.
263 * - fill out ports_max, mmcies_max and mmcie_{add,rm} before calling.
265 * - fill out wusbhc->uwb_rc and refcount it before calling
266 * - fill out the wusbhc->sec_modes array
268 int wusbhc_create(struct wusbhc
*wusbhc
)
272 /* set defaults. These can be overwritten using sysfs attributes. */
273 wusbhc
->trust_timeout
= WUSB_TRUST_TIMEOUT_MS
;
274 wusbhc
->phy_rate
= UWB_PHY_RATE_INVALID
- 1;
275 wusbhc
->dnts_num_slots
= 4;
276 wusbhc
->dnts_interval
= 2;
277 wusbhc
->retry_count
= WUSB_RETRY_COUNT_INFINITE
;
279 mutex_init(&wusbhc
->mutex
);
280 result
= wusbhc_mmcie_create(wusbhc
);
282 goto error_mmcie_create
;
283 result
= wusbhc_devconnect_create(wusbhc
);
285 goto error_devconnect_create
;
286 result
= wusbhc_rh_create(wusbhc
);
288 goto error_rh_create
;
289 result
= wusbhc_sec_create(wusbhc
);
291 goto error_sec_create
;
295 wusbhc_rh_destroy(wusbhc
);
297 wusbhc_devconnect_destroy(wusbhc
);
298 error_devconnect_create
:
299 wusbhc_mmcie_destroy(wusbhc
);
303 EXPORT_SYMBOL_GPL(wusbhc_create
);
305 static inline struct kobject
*wusbhc_kobj(struct wusbhc
*wusbhc
)
307 return &wusbhc
->usb_hcd
.self
.controller
->kobj
;
311 * Phase B of a wusbhc instance creation
313 * Creates fields that depend on wusbhc->usb_hcd having been
314 * added. This is where we create the sysfs files in
315 * /sys/class/usb_host/usb_hostX/.
317 * NOTE: Assumes wusbhc->usb_hcd has been already added by the upper
318 * layer (hwahc or whci)
320 int wusbhc_b_create(struct wusbhc
*wusbhc
)
323 struct device
*dev
= wusbhc
->usb_hcd
.self
.controller
;
325 result
= sysfs_create_group(wusbhc_kobj(wusbhc
), &wusbhc_attr_group
);
327 dev_err(dev
, "Cannot register WUSBHC attributes: %d\n",
329 goto error_create_attr_group
;
333 error_create_attr_group
:
336 EXPORT_SYMBOL_GPL(wusbhc_b_create
);
338 void wusbhc_b_destroy(struct wusbhc
*wusbhc
)
340 wusbhc_pal_unregister(wusbhc
);
341 sysfs_remove_group(wusbhc_kobj(wusbhc
), &wusbhc_attr_group
);
343 EXPORT_SYMBOL_GPL(wusbhc_b_destroy
);
345 void wusbhc_destroy(struct wusbhc
*wusbhc
)
347 wusbhc_sec_destroy(wusbhc
);
348 wusbhc_rh_destroy(wusbhc
);
349 wusbhc_devconnect_destroy(wusbhc
);
350 wusbhc_mmcie_destroy(wusbhc
);
352 EXPORT_SYMBOL_GPL(wusbhc_destroy
);
354 struct workqueue_struct
*wusbd
;
355 EXPORT_SYMBOL_GPL(wusbd
);
358 * WUSB Cluster ID allocation map
360 * Each WUSB bus in a channel is identified with a Cluster Id in the
361 * unauth address pace (WUSB1.0[4.3]). We take the range 0xe0 to 0xff
362 * (that's space for 31 WUSB controllers, as 0xff can't be taken). We
363 * start taking from 0xff, 0xfe, 0xfd... (hence the += or -= 0xff).
365 * For each one we taken, we pin it in the bitap
367 #define CLUSTER_IDS 32
368 static DECLARE_BITMAP(wusb_cluster_id_table
, CLUSTER_IDS
);
369 static DEFINE_SPINLOCK(wusb_cluster_ids_lock
);
372 * Get a WUSB Cluster ID
374 * Need to release with wusb_cluster_id_put() when done w/ it.
376 /* FIXME: coordinate with the choose_addres() from the USB stack */
377 /* we want to leave the top of the 128 range for cluster addresses and
378 * the bottom for device addresses (as we map them one on one with
380 u8
wusb_cluster_id_get(void)
383 spin_lock(&wusb_cluster_ids_lock
);
384 id
= find_first_zero_bit(wusb_cluster_id_table
, CLUSTER_IDS
);
385 if (id
>= CLUSTER_IDS
) {
389 set_bit(id
, wusb_cluster_id_table
);
392 spin_unlock(&wusb_cluster_ids_lock
);
396 EXPORT_SYMBOL_GPL(wusb_cluster_id_get
);
399 * Release a WUSB Cluster ID
401 * Obtained it with wusb_cluster_id_get()
403 void wusb_cluster_id_put(u8 id
)
406 BUG_ON(id
>= CLUSTER_IDS
);
407 spin_lock(&wusb_cluster_ids_lock
);
408 WARN_ON(!test_bit(id
, wusb_cluster_id_table
));
409 clear_bit(id
, wusb_cluster_id_table
);
410 spin_unlock(&wusb_cluster_ids_lock
);
412 EXPORT_SYMBOL_GPL(wusb_cluster_id_put
);
415 * wusbhc_giveback_urb - return an URB to the USB core
416 * @wusbhc: the host controller the URB is from.
418 * @status: the URB's status.
420 * Return an URB to the USB core doing some additional WUSB specific
423 * - After a successful transfer, update the trust timeout timestamp
424 * for the WUSB device.
426 * - [WUSB] sections 4.13 and 7.5.1 specify the stop retransmission
427 * condition for the WCONNECTACK_IE is that the host has observed
428 * the associated device responding to a control transfer.
430 void wusbhc_giveback_urb(struct wusbhc
*wusbhc
, struct urb
*urb
, int status
)
432 struct wusb_dev
*wusb_dev
= __wusb_dev_get_by_usb_dev(wusbhc
,
435 if (status
== 0 && wusb_dev
) {
436 wusb_dev
->entry_ts
= jiffies
;
438 /* wusbhc_devconnect_acked() can't be called from
439 atomic context so defer it to a work queue. */
440 if (!list_empty(&wusb_dev
->cack_node
))
441 queue_work(wusbd
, &wusb_dev
->devconnect_acked_work
);
443 wusb_dev_put(wusb_dev
);
446 usb_hcd_giveback_urb(&wusbhc
->usb_hcd
, urb
, status
);
448 EXPORT_SYMBOL_GPL(wusbhc_giveback_urb
);
451 * wusbhc_reset_all - reset the HC hardware
452 * @wusbhc: the host controller to reset.
454 * Request a full hardware reset of the chip. This will also reset
455 * the radio controller and any other PALs.
457 void wusbhc_reset_all(struct wusbhc
*wusbhc
)
460 uwb_rc_reset_all(wusbhc
->uwb_rc
);
462 EXPORT_SYMBOL_GPL(wusbhc_reset_all
);
464 static struct notifier_block wusb_usb_notifier
= {
465 .notifier_call
= wusb_usb_ncb
,
466 .priority
= INT_MAX
/* Need to be called first of all */
469 static int __init
wusbcore_init(void)
472 result
= wusb_crypto_init();
474 goto error_crypto_init
;
475 /* WQ is singlethread because we need to serialize notifications */
476 wusbd
= create_singlethread_workqueue("wusbd");
479 printk(KERN_ERR
"WUSB-core: Cannot create wusbd workqueue\n");
480 goto error_wusbd_create
;
482 usb_register_notify(&wusb_usb_notifier
);
483 bitmap_zero(wusb_cluster_id_table
, CLUSTER_IDS
);
484 set_bit(0, wusb_cluster_id_table
); /* reserve Cluster ID 0xff */
493 module_init(wusbcore_init
);
495 static void __exit
wusbcore_exit(void)
497 clear_bit(0, wusb_cluster_id_table
);
498 if (!bitmap_empty(wusb_cluster_id_table
, CLUSTER_IDS
)) {
499 printk(KERN_ERR
"BUG: WUSB Cluster IDs not released on exit: %*pb\n",
500 CLUSTER_IDS
, wusb_cluster_id_table
);
503 usb_unregister_notify(&wusb_usb_notifier
);
504 destroy_workqueue(wusbd
);
507 module_exit(wusbcore_exit
);
509 MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
510 MODULE_DESCRIPTION("Wireless USB core");
511 MODULE_LICENSE("GPL");