blob: 2384add453716e1eea74b380694921f2f27e7b66 [file] [log] [blame]
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +01001/*
2 * Wireless USB Host Controller
3 * Common infrastructure for WHCI and HWA WUSB-HC drivers
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 * This driver implements parts common to all Wireless USB Host
25 * Controllers (struct wusbhc, embedding a struct usb_hcd) and is used
26 * by:
27 *
28 * - hwahc: HWA, USB-dongle that implements a Wireless USB host
29 * controller, (Wireless USB 1.0 Host-Wire-Adapter specification).
30 *
31 * - whci: WHCI, a PCI card with a wireless host controller
32 * (Wireless Host Controller Interface 1.0 specification).
33 *
34 * Check out the Design-overview.txt file in the source documentation
35 * for other details on the implementation.
36 *
37 * Main blocks:
38 *
39 * rh Root Hub emulation (part of the HCD glue)
40 *
41 * devconnect Handle all the issues related to device connection,
42 * authentication, disconnection, timeout, reseting,
43 * keepalives, etc.
44 *
45 * mmc MMC IE broadcasting handling
46 *
47 * A host controller driver just initializes its stuff and as part of
48 * that, creates a 'struct wusbhc' instance that handles all the
49 * common WUSB mechanisms. Links in the function ops that are specific
50 * to it and then registers the host controller. Ready to run.
51 */
52
53#ifndef __WUSBHC_H__
54#define __WUSBHC_H__
55
56#include <linux/usb.h>
57#include <linux/list.h>
58#include <linux/mutex.h>
59#include <linux/kref.h>
60#include <linux/workqueue.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020061#include <linux/usb/hcd.h>
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010062#include <linux/uwb.h>
63#include <linux/usb/wusb.h>
64
David Vrabel4d2bea42008-10-27 15:42:31 +000065/*
66 * Time from a WUSB channel stop request to the last transmitted MMC.
67 *
68 * This needs to be > 4.096 ms in case no MMCs can be transmitted in
69 * zone 0.
70 */
71#define WUSB_CHANNEL_STOP_DELAY_MS 8
Thomas Pugliesef265d4d2013-06-18 13:31:26 -050072#define WUSB_RETRY_COUNT_MAX 15
73#define WUSB_RETRY_COUNT_INFINITE 0
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010074
75/**
76 * Wireless USB device
77 *
78 * Describe a WUSB device connected to the cluster. This struct
79 * belongs to the 'struct wusb_port' it is attached to and it is
80 * responsible for putting and clearing the pointer to it.
81 *
82 * Note this "complements" the 'struct usb_device' that the usb_hcd
83 * keeps for each connected USB device. However, it extends some
84 * information that is not available (there is no hcpriv ptr in it!)
85 * *and* most importantly, it's life cycle is different. It is created
86 * as soon as we get a DN_Connect (connect request notification) from
87 * the device through the WUSB host controller; the USB stack doesn't
88 * create the device until we authenticate it. FIXME: this will
89 * change.
90 *
91 * @bos: This is allocated when the BOS descriptors are read from
92 * the device and freed upon the wusb_dev struct dying.
93 * @wusb_cap_descr: points into @bos, and has been verified to be size
94 * safe.
95 */
96struct wusb_dev {
97 struct kref refcnt;
98 struct wusbhc *wusbhc;
99 struct list_head cack_node; /* Connect-Ack list */
Thomas Pugliese471e42a2013-12-02 15:39:45 -0600100 struct list_head rekey_node; /* GTK rekey list */
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100101 u8 port_idx;
102 u8 addr;
103 u8 beacon_type:4;
104 struct usb_encryption_descriptor ccm1_etd;
105 struct wusb_ckhdid cdid;
106 unsigned long entry_ts;
107 struct usb_bos_descriptor *bos;
108 struct usb_wireless_cap_descriptor *wusb_cap_descr;
109 struct uwb_mas_bm availability;
110 struct work_struct devconnect_acked_work;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100111 struct usb_device *usb_dev;
112};
113
114#define WUSB_DEV_ADDR_UNAUTH 0x80
115
116static inline void wusb_dev_init(struct wusb_dev *wusb_dev)
117{
118 kref_init(&wusb_dev->refcnt);
119 /* no need to init the cack_node */
120}
121
122extern void wusb_dev_destroy(struct kref *_wusb_dev);
123
124static inline struct wusb_dev *wusb_dev_get(struct wusb_dev *wusb_dev)
125{
126 kref_get(&wusb_dev->refcnt);
127 return wusb_dev;
128}
129
130static inline void wusb_dev_put(struct wusb_dev *wusb_dev)
131{
132 kref_put(&wusb_dev->refcnt, wusb_dev_destroy);
133}
134
135/**
Justin P. Mattock7aed9df2011-02-24 22:11:55 -0800136 * Wireless USB Host Controller root hub "fake" ports
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100137 * (state and device information)
138 *
139 * Wireless USB is wireless, so there are no ports; but we
140 * fake'em. Each RC can connect a max of devices at the same time
141 * (given in the Wireless Adapter descriptor, bNumPorts or WHCI's
142 * caps), referred to in wusbhc->ports_max.
143 *
144 * See rh.c for more information.
145 *
146 * The @status and @change use the same bits as in USB2.0[11.24.2.7],
147 * so we don't have to do much when getting the port's status.
148 *
149 * WUSB1.0[7.1], USB2.0[11.24.2.7.1,fig 11-10],
150 * include/linux/usb_ch9.h (#define USB_PORT_STAT_*)
151 */
152struct wusb_port {
153 u16 status;
154 u16 change;
155 struct wusb_dev *wusb_dev; /* connected device's info */
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100156 u32 ptk_tkid;
157};
158
159/**
160 * WUSB Host Controller specifics
161 *
162 * All fields that are common to all Wireless USB controller types
163 * (HWA and WHCI) are grouped here. Host Controller
164 * functions/operations that only deal with general Wireless USB HC
165 * issues use this data type to refer to the host.
166 *
Rahul Bedarkara7737e32014-01-04 14:13:10 +0530167 * @usb_hcd Instantiation of a USB host controller
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100168 * (initialized by upper layer [HWA=HC or WHCI].
169 *
170 * @dev Device that implements this; initialized by the
171 * upper layer (HWA-HC, WHCI...); this device should
172 * have a refcount.
173 *
174 * @trust_timeout After this time without hearing for device
175 * activity, we consider the device gone and we have to
176 * re-authenticate.
177 *
178 * Can be accessed w/o locking--however, read to a
179 * local variable then use.
180 *
181 * @chid WUSB Cluster Host ID: this is supposed to be a
182 * unique value that doesn't change across reboots (so
183 * that your devices do not require re-association).
184 *
185 * Read/Write protected by @mutex
186 *
187 * @dev_info This array has ports_max elements. It is used to
188 * give the HC information about the WUSB devices (see
189 * 'struct wusb_dev_info').
190 *
191 * For HWA we need to allocate it in heap; for WHCI it
192 * needs to be permanently mapped, so we keep it for
193 * both and make it easy. Call wusbhc->dev_info_set()
194 * to update an entry.
195 *
196 * @ports_max Number of simultaneous device connections (fake
197 * ports) this HC will take. Read-only.
198 *
Rahul Bedarkara7737e32014-01-04 14:13:10 +0530199 * @port Array of port status for each fake root port. Guaranteed to
Daniel Mack1537a362010-01-29 15:57:49 +0800200 * always be the same length during device existence
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100201 * [this allows for some unlocked but referenced reading].
202 *
203 * @mmcies_max Max number of Information Elements this HC can send
204 * in its MMC. Read-only.
205 *
David Vrabel4d2bea42008-10-27 15:42:31 +0000206 * @start Start the WUSB channel.
207 *
208 * @stop Stop the WUSB channel after the specified number of
209 * milliseconds. Channel Stop IEs should be transmitted
210 * as required by [WUSB] 4.16.2.1.
211 *
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100212 * @mmcie_add HC specific operation (WHCI or HWA) for adding an
213 * MMCIE.
214 *
215 * @mmcie_rm HC specific operation (WHCI or HWA) for removing an
216 * MMCIE.
217 *
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100218 * @set_ptk: Set the PTK and enable encryption for a device. Or, if
219 * the supplied key is NULL, disable encryption for that
220 * device.
221 *
222 * @set_gtk: Set the GTK to be used for all future broadcast packets
223 * (i.e., MMCs). With some hardware, setting the GTK may start
224 * MMC transmission.
225 *
226 * NOTE:
227 *
228 * - If wusb_dev->usb_dev is not NULL, then usb_dev is valid
229 * (wusb_dev has a refcount on it). Likewise, if usb_dev->wusb_dev
230 * is not NULL, usb_dev->wusb_dev is valid (usb_dev keeps a
231 * refcount on it).
232 *
233 * Most of the times when you need to use it, it will be non-NULL,
234 * so there is no real need to check for it (wusb_dev will
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300235 * disappear before usb_dev).
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100236 *
237 * - The following fields need to be filled out before calling
238 * wusbhc_create(): ports_max, mmcies_max, mmcie_{add,rm}.
239 *
240 * - there is no wusbhc_init() method, we do everything in
241 * wusbhc_create().
242 *
243 * - Creation is done in two phases, wusbhc_create() and
244 * wusbhc_create_b(); b are the parts that need to be called after
245 * calling usb_hcd_add(&wusbhc->usb_hcd).
246 */
247struct wusbhc {
248 struct usb_hcd usb_hcd; /* HAS TO BE 1st */
249 struct device *dev;
250 struct uwb_rc *uwb_rc;
251 struct uwb_pal pal;
252
253 unsigned trust_timeout; /* in jiffies */
David Vrabel6fae35f2008-11-17 15:53:42 +0000254 struct wusb_ckhdid chid;
David Vrabelc3f22d92009-10-12 15:45:18 +0000255 uint8_t phy_rate;
Thomas Pugliese8bf1d072013-06-18 13:31:25 -0500256 uint8_t dnts_num_slots;
257 uint8_t dnts_interval;
Thomas Pugliesef265d4d2013-06-18 13:31:26 -0500258 uint8_t retry_count;
David Vrabel6fae35f2008-11-17 15:53:42 +0000259 struct wuie_host_info *wuie_host_info;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100260
261 struct mutex mutex; /* locks everything else */
262 u16 cluster_id; /* Wireless USB Cluster ID */
263 struct wusb_port *port; /* Fake port status handling */
264 struct wusb_dev_info *dev_info; /* for Set Device Info mgmt */
265 u8 ports_max;
266 unsigned active:1; /* currently xmit'ing MMCs */
267 struct wuie_keep_alive keep_alive_ie; /* protected by mutex */
268 struct delayed_work keep_alive_timer;
269 struct list_head cack_list; /* Connect acknowledging */
270 size_t cack_count; /* protected by 'mutex' */
271 struct wuie_connect_ack cack_ie;
272 struct uwb_rsv *rsv; /* cluster bandwidth reservation */
273
274 struct mutex mmcie_mutex; /* MMC WUIE handling */
275 struct wuie_hdr **mmcie; /* WUIE array */
276 u8 mmcies_max;
277 /* FIXME: make wusbhc_ops? */
278 int (*start)(struct wusbhc *wusbhc);
David Vrabel4d2bea42008-10-27 15:42:31 +0000279 void (*stop)(struct wusbhc *wusbhc, int delay);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100280 int (*mmcie_add)(struct wusbhc *wusbhc, u8 interval, u8 repeat_cnt,
281 u8 handle, struct wuie_hdr *wuie);
282 int (*mmcie_rm)(struct wusbhc *wusbhc, u8 handle);
283 int (*dev_info_set)(struct wusbhc *, struct wusb_dev *wusb_dev);
284 int (*bwa_set)(struct wusbhc *wusbhc, s8 stream_index,
285 const struct uwb_mas_bm *);
286 int (*set_ptk)(struct wusbhc *wusbhc, u8 port_idx,
287 u32 tkid, const void *key, size_t key_size);
288 int (*set_gtk)(struct wusbhc *wusbhc,
289 u32 tkid, const void *key, size_t key_size);
290 int (*set_num_dnts)(struct wusbhc *wusbhc, u8 interval, u8 slots);
291
292 struct {
293 struct usb_key_descriptor descr;
294 u8 data[16]; /* GTK key data */
295 } __attribute__((packed)) gtk;
296 u8 gtk_index;
297 u32 gtk_tkid;
Thomas Pugliese471e42a2013-12-02 15:39:45 -0600298 struct work_struct gtk_rekey_work;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100299
300 struct usb_encryption_descriptor *ccm1_etd;
301};
302
303#define usb_hcd_to_wusbhc(u) container_of((u), struct wusbhc, usb_hcd)
304
305
306extern int wusbhc_create(struct wusbhc *);
307extern int wusbhc_b_create(struct wusbhc *);
308extern void wusbhc_b_destroy(struct wusbhc *);
309extern void wusbhc_destroy(struct wusbhc *);
310extern int wusb_dev_sysfs_add(struct wusbhc *, struct usb_device *,
311 struct wusb_dev *);
312extern void wusb_dev_sysfs_rm(struct wusb_dev *);
313extern int wusbhc_sec_create(struct wusbhc *);
314extern int wusbhc_sec_start(struct wusbhc *);
315extern void wusbhc_sec_stop(struct wusbhc *);
316extern void wusbhc_sec_destroy(struct wusbhc *);
317extern void wusbhc_giveback_urb(struct wusbhc *wusbhc, struct urb *urb,
318 int status);
319void wusbhc_reset_all(struct wusbhc *wusbhc);
320
321int wusbhc_pal_register(struct wusbhc *wusbhc);
322void wusbhc_pal_unregister(struct wusbhc *wusbhc);
323
324/*
325 * Return @usb_dev's @usb_hcd (properly referenced) or NULL if gone
326 *
327 * @usb_dev: USB device, UNLOCKED and referenced (or otherwise, safe ptr)
328 *
329 * This is a safe assumption as @usb_dev->bus is referenced all the
330 * time during the @usb_dev life cycle.
331 */
Rahul Bedarkara7737e32014-01-04 14:13:10 +0530332static inline
333struct usb_hcd *usb_hcd_get_by_usb_dev(struct usb_device *usb_dev)
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100334{
335 struct usb_hcd *usb_hcd;
336 usb_hcd = container_of(usb_dev->bus, struct usb_hcd, self);
337 return usb_get_hcd(usb_hcd);
338}
339
340/*
341 * Increment the reference count on a wusbhc.
342 *
343 * @wusbhc's life cycle is identical to that of the underlying usb_hcd.
344 */
345static inline struct wusbhc *wusbhc_get(struct wusbhc *wusbhc)
346{
347 return usb_get_hcd(&wusbhc->usb_hcd) ? wusbhc : NULL;
348}
349
350/*
351 * Return the wusbhc associated to a @usb_dev
352 *
353 * @usb_dev: USB device, UNLOCKED and referenced (or otherwise, safe ptr)
354 *
355 * @returns: wusbhc for @usb_dev; NULL if the @usb_dev is being torn down.
356 * WARNING: referenced at the usb_hcd level, unlocked
357 *
358 * FIXME: move offline
359 */
360static inline struct wusbhc *wusbhc_get_by_usb_dev(struct usb_device *usb_dev)
361{
362 struct wusbhc *wusbhc = NULL;
363 struct usb_hcd *usb_hcd;
364 if (usb_dev->devnum > 1 && !usb_dev->wusb) {
365 /* but root hubs */
366 dev_err(&usb_dev->dev, "devnum %d wusb %d\n", usb_dev->devnum,
367 usb_dev->wusb);
368 BUG_ON(usb_dev->devnum > 1 && !usb_dev->wusb);
369 }
370 usb_hcd = usb_hcd_get_by_usb_dev(usb_dev);
371 if (usb_hcd == NULL)
372 return NULL;
373 BUG_ON(usb_hcd->wireless == 0);
374 return wusbhc = usb_hcd_to_wusbhc(usb_hcd);
375}
376
377
378static inline void wusbhc_put(struct wusbhc *wusbhc)
379{
380 usb_put_hcd(&wusbhc->usb_hcd);
381}
382
David Vrabel6fae35f2008-11-17 15:53:42 +0000383int wusbhc_start(struct wusbhc *wusbhc);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100384void wusbhc_stop(struct wusbhc *wusbhc);
385extern int wusbhc_chid_set(struct wusbhc *, const struct wusb_ckhdid *);
386
387/* Device connect handling */
388extern int wusbhc_devconnect_create(struct wusbhc *);
389extern void wusbhc_devconnect_destroy(struct wusbhc *);
David Vrabel6fae35f2008-11-17 15:53:42 +0000390extern int wusbhc_devconnect_start(struct wusbhc *wusbhc);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100391extern void wusbhc_devconnect_stop(struct wusbhc *wusbhc);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100392extern void wusbhc_handle_dn(struct wusbhc *, u8 srcaddr,
393 struct wusb_dn_hdr *dn_hdr, size_t size);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100394extern void __wusbhc_dev_disable(struct wusbhc *wusbhc, u8 port);
395extern int wusb_usb_ncb(struct notifier_block *nb, unsigned long val,
396 void *priv);
397extern int wusb_set_dev_addr(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev,
398 u8 addr);
399
400/* Wireless USB fake Root Hub methods */
401extern int wusbhc_rh_create(struct wusbhc *);
402extern void wusbhc_rh_destroy(struct wusbhc *);
403
404extern int wusbhc_rh_status_data(struct usb_hcd *, char *);
405extern int wusbhc_rh_control(struct usb_hcd *, u16, u16, u16, char *, u16);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100406extern int wusbhc_rh_start_port_reset(struct usb_hcd *, unsigned);
407
408/* MMC handling */
409extern int wusbhc_mmcie_create(struct wusbhc *);
410extern void wusbhc_mmcie_destroy(struct wusbhc *);
411extern int wusbhc_mmcie_set(struct wusbhc *, u8 interval, u8 repeat_cnt,
412 struct wuie_hdr *);
413extern void wusbhc_mmcie_rm(struct wusbhc *, struct wuie_hdr *);
414
415/* Bandwidth reservation */
416int wusbhc_rsv_establish(struct wusbhc *wusbhc);
417void wusbhc_rsv_terminate(struct wusbhc *wusbhc);
418
419/*
420 * I've always said
421 * I wanted a wedding in a church...
422 *
423 * but lately I've been thinking about
424 * the Botanical Gardens.
425 *
426 * We could do it by the tulips.
427 * It'll be beautiful
428 *
429 * --Security!
430 */
431extern int wusb_dev_sec_add(struct wusbhc *, struct usb_device *,
432 struct wusb_dev *);
433extern void wusb_dev_sec_rm(struct wusb_dev *) ;
434extern int wusb_dev_4way_handshake(struct wusbhc *, struct wusb_dev *,
435 struct wusb_ckhdid *ck);
436void wusbhc_gtk_rekey(struct wusbhc *wusbhc);
David Vrabel4656d5d2008-10-27 17:12:33 +0000437int wusb_dev_update_address(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100438
439
440/* WUSB Cluster ID handling */
441extern u8 wusb_cluster_id_get(void);
442extern void wusb_cluster_id_put(u8);
443
444/*
445 * wusb_port_by_idx - return the port associated to a zero-based port index
446 *
447 * NOTE: valid without locking as long as wusbhc is referenced (as the
448 * number of ports doesn't change). The data pointed to has to
449 * be verified though :)
450 */
451static inline struct wusb_port *wusb_port_by_idx(struct wusbhc *wusbhc,
452 u8 port_idx)
453{
454 return &wusbhc->port[port_idx];
455}
456
457/*
458 * wusb_port_no_to_idx - Convert port number (per usb_dev->portnum) to
459 * a port_idx.
460 *
461 * USB stack USB ports are 1 based!!
462 *
463 * NOTE: only valid for WUSB devices!!!
464 */
465static inline u8 wusb_port_no_to_idx(u8 port_no)
466{
467 return port_no - 1;
468}
469
470extern struct wusb_dev *__wusb_dev_get_by_usb_dev(struct wusbhc *,
471 struct usb_device *);
472
473/*
474 * Return a referenced wusb_dev given a @usb_dev
475 *
476 * Returns NULL if the usb_dev is being torn down.
477 *
478 * FIXME: move offline
479 */
480static inline
481struct wusb_dev *wusb_dev_get_by_usb_dev(struct usb_device *usb_dev)
482{
483 struct wusbhc *wusbhc;
484 struct wusb_dev *wusb_dev;
485 wusbhc = wusbhc_get_by_usb_dev(usb_dev);
486 if (wusbhc == NULL)
487 return NULL;
488 mutex_lock(&wusbhc->mutex);
489 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, usb_dev);
490 mutex_unlock(&wusbhc->mutex);
491 wusbhc_put(wusbhc);
492 return wusb_dev;
493}
494
495/* Misc */
496
497extern struct workqueue_struct *wusbd;
498#endif /* #ifndef __WUSBHC_H__ */