wusbhc.h 16 KB

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