wa-hc.h 12 KB

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