wusbhc.h 16 KB

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