wa-hc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * HWA Host Controller Driver
  4. * Wire Adapter Control/Data Streaming Iface (WUSB1.0[8])
  5. *
  6. * Copyright (C) 2005-2006 Intel Corporation
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * This driver implements a USB Host Controller (struct usb_hcd) for a
  10. * Wireless USB Host Controller based on the Wireless USB 1.0
  11. * Host-Wire-Adapter specification (in layman terms, a USB-dongle that
  12. * implements a Wireless USB host).
  13. *
  14. * Check out the Design-overview.txt file in the source documentation
  15. * for other details on the implementation.
  16. *
  17. * Main blocks:
  18. *
  19. * driver glue with the driver API, workqueue daemon
  20. *
  21. * lc RC instance life cycle management (create, destroy...)
  22. *
  23. * hcd glue with the USB API Host Controller Interface API.
  24. *
  25. * nep Notification EndPoint management: collect notifications
  26. * and queue them with the workqueue daemon.
  27. *
  28. * Handle notifications as coming from the NEP. Sends them
  29. * off others to their respective modules (eg: connect,
  30. * disconnect and reset go to devconnect).
  31. *
  32. * rpipe Remote Pipe management; rpipe is what we use to write
  33. * to an endpoint on a WUSB device that is connected to a
  34. * HWA RC.
  35. *
  36. * xfer Transfer management -- this is all the code that gets a
  37. * buffer and pushes it to a device (or viceversa). *
  38. *
  39. * Some day a lot of this code will be shared between this driver and
  40. * the drivers for DWA (xfer, rpipe).
  41. *
  42. * All starts at driver.c:hwahc_probe(), when one of this guys is
  43. * connected. hwahc_disconnect() stops it.
  44. *
  45. * During operation, the main driver is devices connecting or
  46. * disconnecting. They cause the HWA RC to send notifications into
  47. * nep.c:hwahc_nep_cb() that will dispatch them to
  48. * notif.c:wa_notif_dispatch(). From there they will fan to cause
  49. * device connects, disconnects, etc.
  50. *
  51. * Note much of the activity is difficult to follow. For example a
  52. * device connect goes to devconnect, which will cause the "fake" root
  53. * hub port to show a connect and stop there. Then hub_wq will notice
  54. * and call into the rh.c:hwahc_rc_port_reset() code to authenticate
  55. * the device (and this might require user intervention) and enable
  56. * the port.
  57. *
  58. * We also have a timer workqueue going from devconnect.c that
  59. * schedules in hwahc_devconnect_create().
  60. *
  61. * The rest of the traffic is in the usual entry points of a USB HCD,
  62. * which are hooked up in driver.c:hwahc_rc_driver, and defined in
  63. * hcd.c.
  64. */
  65. #ifndef __HWAHC_INTERNAL_H__
  66. #define __HWAHC_INTERNAL_H__
  67. #include <linux/completion.h>
  68. #include <linux/usb.h>
  69. #include <linux/mutex.h>
  70. #include <linux/spinlock.h>
  71. #include <linux/uwb.h>
  72. #include <linux/usb/wusb.h>
  73. #include <linux/usb/wusb-wa.h>
  74. struct wusbhc;
  75. struct wahc;
  76. extern void wa_urb_enqueue_run(struct work_struct *ws);
  77. extern void wa_process_errored_transfers_run(struct work_struct *ws);
  78. /**
  79. * RPipe instance
  80. *
  81. * @descr's fields are kept in LE, as we need to send it back and
  82. * forth.
  83. *
  84. * @wa is referenced when set
  85. *
  86. * @segs_available is the number of requests segments that still can
  87. * be submitted to the controller without overloading
  88. * it. It is initialized to descr->wRequests when
  89. * aiming.
  90. *
  91. * A rpipe supports a max of descr->wRequests at the same time; before
  92. * submitting seg_lock has to be taken. If segs_avail > 0, then we can
  93. * submit; if not, we have to queue them.
  94. */
  95. struct wa_rpipe {
  96. struct kref refcnt;
  97. struct usb_rpipe_descriptor descr;
  98. struct usb_host_endpoint *ep;
  99. struct wahc *wa;
  100. spinlock_t seg_lock;
  101. struct list_head seg_list;
  102. struct list_head list_node;
  103. atomic_t segs_available;
  104. u8 buffer[1]; /* For reads/writes on USB */
  105. };
  106. enum wa_dti_state {
  107. WA_DTI_TRANSFER_RESULT_PENDING,
  108. WA_DTI_ISOC_PACKET_STATUS_PENDING,
  109. WA_DTI_BUF_IN_DATA_PENDING
  110. };
  111. enum wa_quirks {
  112. /*
  113. * The Alereon HWA expects the data frames in isochronous transfer
  114. * requests to be concatenated and not sent as separate packets.
  115. */
  116. WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC = 0x01,
  117. /*
  118. * The Alereon HWA can be instructed to not send transfer notifications
  119. * as an optimization.
  120. */
  121. WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS = 0x02,
  122. };
  123. enum wa_vendor_specific_requests {
  124. WA_REQ_ALEREON_DISABLE_XFER_NOTIFICATIONS = 0x4C,
  125. WA_REQ_ALEREON_FEATURE_SET = 0x01,
  126. WA_REQ_ALEREON_FEATURE_CLEAR = 0x00,
  127. };
  128. #define WA_MAX_BUF_IN_URBS 4
  129. /**
  130. * Instance of a HWA Host Controller
  131. *
  132. * Except where a more specific lock/mutex applies or atomic, all
  133. * fields protected by @mutex.
  134. *
  135. * @wa_descr Can be accessed without locking because it is in
  136. * the same area where the device descriptors were
  137. * read, so it is guaranteed to exist unmodified while
  138. * the device exists.
  139. *
  140. * Endianess has been converted to CPU's.
  141. *
  142. * @nep_* can be accessed without locking as its processing is
  143. * serialized; we submit a NEP URB and it comes to
  144. * hwahc_nep_cb(), which won't issue another URB until it is
  145. * done processing it.
  146. *
  147. * @xfer_list:
  148. *
  149. * List of active transfers to verify existence from a xfer id
  150. * gotten from the xfer result message. Can't use urb->list because
  151. * it goes by endpoint, and we don't know the endpoint at the time
  152. * when we get the xfer result message. We can't really rely on the
  153. * pointer (will have to change for 64 bits) as the xfer id is 32 bits.
  154. *
  155. * @xfer_delayed_list: List of transfers that need to be started
  156. * (with a workqueue, because they were
  157. * submitted from an atomic context).
  158. *
  159. * FIXME: this needs to be layered up: a wusbhc layer (for sharing
  160. * commonalities with WHCI), a wa layer (for sharing
  161. * commonalities with DWA-RC).
  162. */
  163. struct wahc {
  164. struct usb_device *usb_dev;
  165. struct usb_interface *usb_iface;
  166. /* HC to deliver notifications */
  167. union {
  168. struct wusbhc *wusb;
  169. struct dwahc *dwa;
  170. };
  171. const struct usb_endpoint_descriptor *dto_epd, *dti_epd;
  172. const struct usb_wa_descriptor *wa_descr;
  173. struct urb *nep_urb; /* Notification EndPoint [lockless] */
  174. struct edc nep_edc;
  175. void *nep_buffer;
  176. size_t nep_buffer_size;
  177. atomic_t notifs_queued;
  178. u16 rpipes;
  179. unsigned long *rpipe_bm; /* rpipe usage bitmap */
  180. struct list_head rpipe_delayed_list; /* delayed RPIPES. */
  181. spinlock_t rpipe_lock; /* protect rpipe_bm and delayed list */
  182. struct mutex rpipe_mutex; /* assigning resources to endpoints */
  183. /*
  184. * dti_state is used to track the state of the dti_urb. When dti_state
  185. * is WA_DTI_ISOC_PACKET_STATUS_PENDING, dti_isoc_xfer_in_progress and
  186. * dti_isoc_xfer_seg identify which xfer the incoming isoc packet
  187. * status refers to.
  188. */
  189. enum wa_dti_state dti_state;
  190. u32 dti_isoc_xfer_in_progress;
  191. u8 dti_isoc_xfer_seg;
  192. struct urb *dti_urb; /* URB for reading xfer results */
  193. /* URBs for reading data in */
  194. struct urb buf_in_urbs[WA_MAX_BUF_IN_URBS];
  195. int active_buf_in_urbs; /* number of buf_in_urbs active. */
  196. struct edc dti_edc; /* DTI error density counter */
  197. void *dti_buf;
  198. size_t dti_buf_size;
  199. unsigned long dto_in_use; /* protect dto endoint serialization */
  200. s32 status; /* For reading status */
  201. struct list_head xfer_list;
  202. struct list_head xfer_delayed_list;
  203. struct list_head xfer_errored_list;
  204. /*
  205. * lock for the above xfer lists. Can be taken while a xfer->lock is
  206. * held but not in the reverse order.
  207. */
  208. spinlock_t xfer_list_lock;
  209. struct work_struct xfer_enqueue_work;
  210. struct work_struct xfer_error_work;
  211. atomic_t xfer_id_count;
  212. kernel_ulong_t quirks;
  213. };
  214. extern int wa_create(struct wahc *wa, struct usb_interface *iface,
  215. kernel_ulong_t);
  216. extern void __wa_destroy(struct wahc *wa);
  217. extern int wa_dti_start(struct wahc *wa);
  218. void wa_reset_all(struct wahc *wa);
  219. /* Miscellaneous constants */
  220. enum {
  221. /** Max number of EPROTO errors we tolerate on the NEP in a
  222. * period of time */
  223. HWAHC_EPROTO_MAX = 16,
  224. /** Period of time for EPROTO errors (in jiffies) */
  225. HWAHC_EPROTO_PERIOD = 4 * HZ,
  226. };
  227. /* Notification endpoint handling */
  228. extern int wa_nep_create(struct wahc *, struct usb_interface *);
  229. extern void wa_nep_destroy(struct wahc *);
  230. static inline int wa_nep_arm(struct wahc *wa, gfp_t gfp_mask)
  231. {
  232. struct urb *urb = wa->nep_urb;
  233. urb->transfer_buffer = wa->nep_buffer;
  234. urb->transfer_buffer_length = wa->nep_buffer_size;
  235. return usb_submit_urb(urb, gfp_mask);
  236. }
  237. static inline void wa_nep_disarm(struct wahc *wa)
  238. {
  239. usb_kill_urb(wa->nep_urb);
  240. }
  241. /* RPipes */
  242. static inline void wa_rpipe_init(struct wahc *wa)
  243. {
  244. INIT_LIST_HEAD(&wa->rpipe_delayed_list);
  245. spin_lock_init(&wa->rpipe_lock);
  246. mutex_init(&wa->rpipe_mutex);
  247. }
  248. static inline void wa_init(struct wahc *wa)
  249. {
  250. int index;
  251. edc_init(&wa->nep_edc);
  252. atomic_set(&wa->notifs_queued, 0);
  253. wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
  254. wa_rpipe_init(wa);
  255. edc_init(&wa->dti_edc);
  256. INIT_LIST_HEAD(&wa->xfer_list);
  257. INIT_LIST_HEAD(&wa->xfer_delayed_list);
  258. INIT_LIST_HEAD(&wa->xfer_errored_list);
  259. spin_lock_init(&wa->xfer_list_lock);
  260. INIT_WORK(&wa->xfer_enqueue_work, wa_urb_enqueue_run);
  261. INIT_WORK(&wa->xfer_error_work, wa_process_errored_transfers_run);
  262. wa->dto_in_use = 0;
  263. atomic_set(&wa->xfer_id_count, 1);
  264. /* init the buf in URBs */
  265. for (index = 0; index < WA_MAX_BUF_IN_URBS; ++index)
  266. usb_init_urb(&(wa->buf_in_urbs[index]));
  267. wa->active_buf_in_urbs = 0;
  268. }
  269. /**
  270. * Destroy a pipe (when refcount drops to zero)
  271. *
  272. * Assumes it has been moved to the "QUIESCING" state.
  273. */
  274. struct wa_xfer;
  275. extern void rpipe_destroy(struct kref *_rpipe);
  276. static inline
  277. void __rpipe_get(struct wa_rpipe *rpipe)
  278. {
  279. kref_get(&rpipe->refcnt);
  280. }
  281. extern int rpipe_get_by_ep(struct wahc *, struct usb_host_endpoint *,
  282. struct urb *, gfp_t);
  283. static inline void rpipe_put(struct wa_rpipe *rpipe)
  284. {
  285. kref_put(&rpipe->refcnt, rpipe_destroy);
  286. }
  287. extern void rpipe_ep_disable(struct wahc *, struct usb_host_endpoint *);
  288. extern void rpipe_clear_feature_stalled(struct wahc *,
  289. struct usb_host_endpoint *);
  290. extern int wa_rpipes_create(struct wahc *);
  291. extern void wa_rpipes_destroy(struct wahc *);
  292. static inline void rpipe_avail_dec(struct wa_rpipe *rpipe)
  293. {
  294. atomic_dec(&rpipe->segs_available);
  295. }
  296. /**
  297. * Returns true if the rpipe is ready to submit more segments.
  298. */
  299. static inline int rpipe_avail_inc(struct wa_rpipe *rpipe)
  300. {
  301. return atomic_inc_return(&rpipe->segs_available) > 0
  302. && !list_empty(&rpipe->seg_list);
  303. }
  304. /* Transferring data */
  305. extern int wa_urb_enqueue(struct wahc *, struct usb_host_endpoint *,
  306. struct urb *, gfp_t);
  307. extern int wa_urb_dequeue(struct wahc *, struct urb *, int);
  308. extern void wa_handle_notif_xfer(struct wahc *, struct wa_notif_hdr *);
  309. /* Misc
  310. *
  311. * FIXME: Refcounting for the actual @hwahc object is not correct; I
  312. * mean, this should be refcounting on the HCD underneath, but
  313. * it is not. In any case, the semantics for HCD refcounting
  314. * are *weird*...on refcount reaching zero it just frees
  315. * it...no RC specific function is called...unless I miss
  316. * something.
  317. *
  318. * FIXME: has to go away in favour of a 'struct' hcd based solution
  319. */
  320. static inline struct wahc *wa_get(struct wahc *wa)
  321. {
  322. usb_get_intf(wa->usb_iface);
  323. return wa;
  324. }
  325. static inline void wa_put(struct wahc *wa)
  326. {
  327. usb_put_intf(wa->usb_iface);
  328. }
  329. static inline int __wa_feature(struct wahc *wa, unsigned op, u16 feature)
  330. {
  331. return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  332. op ? USB_REQ_SET_FEATURE : USB_REQ_CLEAR_FEATURE,
  333. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  334. feature,
  335. wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  336. NULL, 0, USB_CTRL_SET_TIMEOUT);
  337. }
  338. static inline int __wa_set_feature(struct wahc *wa, u16 feature)
  339. {
  340. return __wa_feature(wa, 1, feature);
  341. }
  342. static inline int __wa_clear_feature(struct wahc *wa, u16 feature)
  343. {
  344. return __wa_feature(wa, 0, feature);
  345. }
  346. /**
  347. * Return the status of a Wire Adapter
  348. *
  349. * @wa: Wire Adapter instance
  350. * @returns < 0 errno code on error, or status bitmap as described
  351. * in WUSB1.0[8.3.1.6].
  352. *
  353. * NOTE: need malloc, some arches don't take USB from the stack
  354. */
  355. static inline
  356. s32 __wa_get_status(struct wahc *wa)
  357. {
  358. s32 result;
  359. result = usb_control_msg(
  360. wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0),
  361. USB_REQ_GET_STATUS,
  362. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  363. 0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  364. &wa->status, sizeof(wa->status), USB_CTRL_GET_TIMEOUT);
  365. if (result >= 0)
  366. result = wa->status;
  367. return result;
  368. }
  369. /**
  370. * Waits until the Wire Adapter's status matches @mask/@value
  371. *
  372. * @wa: Wire Adapter instance.
  373. * @returns < 0 errno code on error, otherwise status.
  374. *
  375. * Loop until the WAs status matches the mask and value (status & mask
  376. * == value). Timeout if it doesn't happen.
  377. *
  378. * FIXME: is there an official specification on how long status
  379. * changes can take?
  380. */
  381. static inline s32 __wa_wait_status(struct wahc *wa, u32 mask, u32 value)
  382. {
  383. s32 result;
  384. unsigned loops = 10;
  385. do {
  386. msleep(50);
  387. result = __wa_get_status(wa);
  388. if ((result & mask) == value)
  389. break;
  390. if (loops-- == 0) {
  391. result = -ETIMEDOUT;
  392. break;
  393. }
  394. } while (result >= 0);
  395. return result;
  396. }
  397. /** Command @hwahc to stop, @returns 0 if ok, < 0 errno code on error */
  398. static inline int __wa_stop(struct wahc *wa)
  399. {
  400. int result;
  401. struct device *dev = &wa->usb_iface->dev;
  402. result = __wa_clear_feature(wa, WA_ENABLE);
  403. if (result < 0 && result != -ENODEV) {
  404. dev_err(dev, "error commanding HC to stop: %d\n", result);
  405. goto out;
  406. }
  407. result = __wa_wait_status(wa, WA_ENABLE, 0);
  408. if (result < 0 && result != -ENODEV)
  409. dev_err(dev, "error waiting for HC to stop: %d\n", result);
  410. out:
  411. return 0;
  412. }
  413. #endif /* #ifndef __HWAHC_INTERNAL_H__ */