wa-hc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 management: 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 hub_wq 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. extern void wa_process_errored_transfers_run(struct work_struct *ws);
  92. /**
  93. * RPipe instance
  94. *
  95. * @descr's fields are kept in LE, as we need to send it back and
  96. * forth.
  97. *
  98. * @wa is referenced when set
  99. *
  100. * @segs_available is the number of requests segments that still can
  101. * be submitted to the controller without overloading
  102. * it. It is initialized to descr->wRequests when
  103. * aiming.
  104. *
  105. * A rpipe supports a max of descr->wRequests at the same time; before
  106. * submitting seg_lock has to be taken. If segs_avail > 0, then we can
  107. * submit; if not, we have to queue them.
  108. */
  109. struct wa_rpipe {
  110. struct kref refcnt;
  111. struct usb_rpipe_descriptor descr;
  112. struct usb_host_endpoint *ep;
  113. struct wahc *wa;
  114. spinlock_t seg_lock;
  115. struct list_head seg_list;
  116. struct list_head list_node;
  117. atomic_t segs_available;
  118. u8 buffer[1]; /* For reads/writes on USB */
  119. };
  120. enum wa_dti_state {
  121. WA_DTI_TRANSFER_RESULT_PENDING,
  122. WA_DTI_ISOC_PACKET_STATUS_PENDING,
  123. WA_DTI_BUF_IN_DATA_PENDING
  124. };
  125. enum wa_quirks {
  126. /*
  127. * The Alereon HWA expects the data frames in isochronous transfer
  128. * requests to be concatenated and not sent as separate packets.
  129. */
  130. WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC = 0x01,
  131. /*
  132. * The Alereon HWA can be instructed to not send transfer notifications
  133. * as an optimization.
  134. */
  135. WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS = 0x02,
  136. };
  137. enum wa_vendor_specific_requests {
  138. WA_REQ_ALEREON_DISABLE_XFER_NOTIFICATIONS = 0x4C,
  139. WA_REQ_ALEREON_FEATURE_SET = 0x01,
  140. WA_REQ_ALEREON_FEATURE_CLEAR = 0x00,
  141. };
  142. #define WA_MAX_BUF_IN_URBS 4
  143. /**
  144. * Instance of a HWA Host Controller
  145. *
  146. * Except where a more specific lock/mutex applies or atomic, all
  147. * fields protected by @mutex.
  148. *
  149. * @wa_descr Can be accessed without locking because it is in
  150. * the same area where the device descriptors were
  151. * read, so it is guaranteed to exist unmodified while
  152. * the device exists.
  153. *
  154. * Endianess has been converted to CPU's.
  155. *
  156. * @nep_* can be accessed without locking as its processing is
  157. * serialized; we submit a NEP URB and it comes to
  158. * hwahc_nep_cb(), which won't issue another URB until it is
  159. * done processing it.
  160. *
  161. * @xfer_list:
  162. *
  163. * List of active transfers to verify existence from a xfer id
  164. * gotten from the xfer result message. Can't use urb->list because
  165. * it goes by endpoint, and we don't know the endpoint at the time
  166. * when we get the xfer result message. We can't really rely on the
  167. * pointer (will have to change for 64 bits) as the xfer id is 32 bits.
  168. *
  169. * @xfer_delayed_list: List of transfers that need to be started
  170. * (with a workqueue, because they were
  171. * submitted from an atomic context).
  172. *
  173. * FIXME: this needs to be layered up: a wusbhc layer (for sharing
  174. * commonalities with WHCI), a wa layer (for sharing
  175. * commonalities with DWA-RC).
  176. */
  177. struct wahc {
  178. struct usb_device *usb_dev;
  179. struct usb_interface *usb_iface;
  180. /* HC to deliver notifications */
  181. union {
  182. struct wusbhc *wusb;
  183. struct dwahc *dwa;
  184. };
  185. const struct usb_endpoint_descriptor *dto_epd, *dti_epd;
  186. const struct usb_wa_descriptor *wa_descr;
  187. struct urb *nep_urb; /* Notification EndPoint [lockless] */
  188. struct edc nep_edc;
  189. void *nep_buffer;
  190. size_t nep_buffer_size;
  191. atomic_t notifs_queued;
  192. u16 rpipes;
  193. unsigned long *rpipe_bm; /* rpipe usage bitmap */
  194. struct list_head rpipe_delayed_list; /* delayed RPIPES. */
  195. spinlock_t rpipe_lock; /* protect rpipe_bm and delayed list */
  196. struct mutex rpipe_mutex; /* assigning resources to endpoints */
  197. /*
  198. * dti_state is used to track the state of the dti_urb. When dti_state
  199. * is WA_DTI_ISOC_PACKET_STATUS_PENDING, dti_isoc_xfer_in_progress and
  200. * dti_isoc_xfer_seg identify which xfer the incoming isoc packet
  201. * status refers to.
  202. */
  203. enum wa_dti_state dti_state;
  204. u32 dti_isoc_xfer_in_progress;
  205. u8 dti_isoc_xfer_seg;
  206. struct urb *dti_urb; /* URB for reading xfer results */
  207. /* URBs for reading data in */
  208. struct urb buf_in_urbs[WA_MAX_BUF_IN_URBS];
  209. int active_buf_in_urbs; /* number of buf_in_urbs active. */
  210. struct edc dti_edc; /* DTI error density counter */
  211. void *dti_buf;
  212. size_t dti_buf_size;
  213. unsigned long dto_in_use; /* protect dto endoint serialization */
  214. s32 status; /* For reading status */
  215. struct list_head xfer_list;
  216. struct list_head xfer_delayed_list;
  217. struct list_head xfer_errored_list;
  218. /*
  219. * lock for the above xfer lists. Can be taken while a xfer->lock is
  220. * held but not in the reverse order.
  221. */
  222. spinlock_t xfer_list_lock;
  223. struct work_struct xfer_enqueue_work;
  224. struct work_struct xfer_error_work;
  225. atomic_t xfer_id_count;
  226. kernel_ulong_t quirks;
  227. };
  228. extern int wa_create(struct wahc *wa, struct usb_interface *iface,
  229. kernel_ulong_t);
  230. extern void __wa_destroy(struct wahc *wa);
  231. extern int wa_dti_start(struct wahc *wa);
  232. void wa_reset_all(struct wahc *wa);
  233. /* Miscellaneous constants */
  234. enum {
  235. /** Max number of EPROTO errors we tolerate on the NEP in a
  236. * period of time */
  237. HWAHC_EPROTO_MAX = 16,
  238. /** Period of time for EPROTO errors (in jiffies) */
  239. HWAHC_EPROTO_PERIOD = 4 * HZ,
  240. };
  241. /* Notification endpoint handling */
  242. extern int wa_nep_create(struct wahc *, struct usb_interface *);
  243. extern void wa_nep_destroy(struct wahc *);
  244. static inline int wa_nep_arm(struct wahc *wa, gfp_t gfp_mask)
  245. {
  246. struct urb *urb = wa->nep_urb;
  247. urb->transfer_buffer = wa->nep_buffer;
  248. urb->transfer_buffer_length = wa->nep_buffer_size;
  249. return usb_submit_urb(urb, gfp_mask);
  250. }
  251. static inline void wa_nep_disarm(struct wahc *wa)
  252. {
  253. usb_kill_urb(wa->nep_urb);
  254. }
  255. /* RPipes */
  256. static inline void wa_rpipe_init(struct wahc *wa)
  257. {
  258. INIT_LIST_HEAD(&wa->rpipe_delayed_list);
  259. spin_lock_init(&wa->rpipe_lock);
  260. mutex_init(&wa->rpipe_mutex);
  261. }
  262. static inline void wa_init(struct wahc *wa)
  263. {
  264. int index;
  265. edc_init(&wa->nep_edc);
  266. atomic_set(&wa->notifs_queued, 0);
  267. wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
  268. wa_rpipe_init(wa);
  269. edc_init(&wa->dti_edc);
  270. INIT_LIST_HEAD(&wa->xfer_list);
  271. INIT_LIST_HEAD(&wa->xfer_delayed_list);
  272. INIT_LIST_HEAD(&wa->xfer_errored_list);
  273. spin_lock_init(&wa->xfer_list_lock);
  274. INIT_WORK(&wa->xfer_enqueue_work, wa_urb_enqueue_run);
  275. INIT_WORK(&wa->xfer_error_work, wa_process_errored_transfers_run);
  276. wa->dto_in_use = 0;
  277. atomic_set(&wa->xfer_id_count, 1);
  278. /* init the buf in URBs */
  279. for (index = 0; index < WA_MAX_BUF_IN_URBS; ++index)
  280. usb_init_urb(&(wa->buf_in_urbs[index]));
  281. wa->active_buf_in_urbs = 0;
  282. }
  283. /**
  284. * Destroy a pipe (when refcount drops to zero)
  285. *
  286. * Assumes it has been moved to the "QUIESCING" state.
  287. */
  288. struct wa_xfer;
  289. extern void rpipe_destroy(struct kref *_rpipe);
  290. static inline
  291. void __rpipe_get(struct wa_rpipe *rpipe)
  292. {
  293. kref_get(&rpipe->refcnt);
  294. }
  295. extern int rpipe_get_by_ep(struct wahc *, struct usb_host_endpoint *,
  296. struct urb *, gfp_t);
  297. static inline void rpipe_put(struct wa_rpipe *rpipe)
  298. {
  299. kref_put(&rpipe->refcnt, rpipe_destroy);
  300. }
  301. extern void rpipe_ep_disable(struct wahc *, struct usb_host_endpoint *);
  302. extern void rpipe_clear_feature_stalled(struct wahc *,
  303. struct usb_host_endpoint *);
  304. extern int wa_rpipes_create(struct wahc *);
  305. extern void wa_rpipes_destroy(struct wahc *);
  306. static inline void rpipe_avail_dec(struct wa_rpipe *rpipe)
  307. {
  308. atomic_dec(&rpipe->segs_available);
  309. }
  310. /**
  311. * Returns true if the rpipe is ready to submit more segments.
  312. */
  313. static inline int rpipe_avail_inc(struct wa_rpipe *rpipe)
  314. {
  315. return atomic_inc_return(&rpipe->segs_available) > 0
  316. && !list_empty(&rpipe->seg_list);
  317. }
  318. /* Transferring data */
  319. extern int wa_urb_enqueue(struct wahc *, struct usb_host_endpoint *,
  320. struct urb *, gfp_t);
  321. extern int wa_urb_dequeue(struct wahc *, struct urb *, int);
  322. extern void wa_handle_notif_xfer(struct wahc *, struct wa_notif_hdr *);
  323. /* Misc
  324. *
  325. * FIXME: Refcounting for the actual @hwahc object is not correct; I
  326. * mean, this should be refcounting on the HCD underneath, but
  327. * it is not. In any case, the semantics for HCD refcounting
  328. * are *weird*...on refcount reaching zero it just frees
  329. * it...no RC specific function is called...unless I miss
  330. * something.
  331. *
  332. * FIXME: has to go away in favour of a 'struct' hcd based solution
  333. */
  334. static inline struct wahc *wa_get(struct wahc *wa)
  335. {
  336. usb_get_intf(wa->usb_iface);
  337. return wa;
  338. }
  339. static inline void wa_put(struct wahc *wa)
  340. {
  341. usb_put_intf(wa->usb_iface);
  342. }
  343. static inline int __wa_feature(struct wahc *wa, unsigned op, u16 feature)
  344. {
  345. return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
  346. op ? USB_REQ_SET_FEATURE : USB_REQ_CLEAR_FEATURE,
  347. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  348. feature,
  349. wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  350. NULL, 0, USB_CTRL_SET_TIMEOUT);
  351. }
  352. static inline int __wa_set_feature(struct wahc *wa, u16 feature)
  353. {
  354. return __wa_feature(wa, 1, feature);
  355. }
  356. static inline int __wa_clear_feature(struct wahc *wa, u16 feature)
  357. {
  358. return __wa_feature(wa, 0, feature);
  359. }
  360. /**
  361. * Return the status of a Wire Adapter
  362. *
  363. * @wa: Wire Adapter instance
  364. * @returns < 0 errno code on error, or status bitmap as described
  365. * in WUSB1.0[8.3.1.6].
  366. *
  367. * NOTE: need malloc, some arches don't take USB from the stack
  368. */
  369. static inline
  370. s32 __wa_get_status(struct wahc *wa)
  371. {
  372. s32 result;
  373. result = usb_control_msg(
  374. wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0),
  375. USB_REQ_GET_STATUS,
  376. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  377. 0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  378. &wa->status, sizeof(wa->status), USB_CTRL_GET_TIMEOUT);
  379. if (result >= 0)
  380. result = wa->status;
  381. return result;
  382. }
  383. /**
  384. * Waits until the Wire Adapter's status matches @mask/@value
  385. *
  386. * @wa: Wire Adapter instance.
  387. * @returns < 0 errno code on error, otherwise status.
  388. *
  389. * Loop until the WAs status matches the mask and value (status & mask
  390. * == value). Timeout if it doesn't happen.
  391. *
  392. * FIXME: is there an official specification on how long status
  393. * changes can take?
  394. */
  395. static inline s32 __wa_wait_status(struct wahc *wa, u32 mask, u32 value)
  396. {
  397. s32 result;
  398. unsigned loops = 10;
  399. do {
  400. msleep(50);
  401. result = __wa_get_status(wa);
  402. if ((result & mask) == value)
  403. break;
  404. if (loops-- == 0) {
  405. result = -ETIMEDOUT;
  406. break;
  407. }
  408. } while (result >= 0);
  409. return result;
  410. }
  411. /** Command @hwahc to stop, @returns 0 if ok, < 0 errno code on error */
  412. static inline int __wa_stop(struct wahc *wa)
  413. {
  414. int result;
  415. struct device *dev = &wa->usb_iface->dev;
  416. result = __wa_clear_feature(wa, WA_ENABLE);
  417. if (result < 0 && result != -ENODEV) {
  418. dev_err(dev, "error commanding HC to stop: %d\n", result);
  419. goto out;
  420. }
  421. result = __wa_wait_status(wa, WA_ENABLE, 0);
  422. if (result < 0 && result != -ENODEV)
  423. dev_err(dev, "error waiting for HC to stop: %d\n", result);
  424. out:
  425. return 0;
  426. }
  427. #endif /* #ifndef __HWAHC_INTERNAL_H__ */