1 // SPDX-License-Identifier: GPL-2.0
3 * HWA Host Controller Driver
4 * Wire Adapter Control/Data Streaming Iface (WUSB1.0[8])
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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).
14 * Check out the Design-overview.txt file in the source documentation
15 * for other details on the implementation.
19 * driver glue with the driver API, workqueue daemon
21 * lc RC instance life cycle management (create, destroy...)
23 * hcd glue with the USB API Host Controller Interface API.
25 * nep Notification EndPoint management: collect notifications
26 * and queue them with the workqueue daemon.
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).
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
36 * xfer Transfer management -- this is all the code that gets a
37 * buffer and pushes it to a device (or viceversa). *
39 * Some day a lot of this code will be shared between this driver and
40 * the drivers for DWA (xfer, rpipe).
42 * All starts at driver.c:hwahc_probe(), when one of this guys is
43 * connected. hwahc_disconnect() stops it.
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.
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
58 * We also have a timer workqueue going from devconnect.c that
59 * schedules in hwahc_devconnect_create().
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
66 #ifndef __HWAHC_INTERNAL_H__
67 #define __HWAHC_INTERNAL_H__
69 #include <linux/completion.h>
70 #include <linux/usb.h>
71 #include <linux/mutex.h>
72 #include <linux/spinlock.h>
73 #include <linux/uwb.h>
74 #include <linux/usb/wusb.h>
75 #include <linux/usb/wusb-wa.h>
79 extern void wa_urb_enqueue_run(struct work_struct *ws);
80 extern void wa_process_errored_transfers_run(struct work_struct *ws);
85 * @descr's fields are kept in LE, as we need to send it back and
88 * @wa is referenced when set
90 * @segs_available is the number of requests segments that still can
91 * be submitted to the controller without overloading
92 * it. It is initialized to descr->wRequests when
95 * A rpipe supports a max of descr->wRequests at the same time; before
96 * submitting seg_lock has to be taken. If segs_avail > 0, then we can
97 * submit; if not, we have to queue them.
101 struct usb_rpipe_descriptor descr;
102 struct usb_host_endpoint *ep;
105 struct list_head seg_list;
106 struct list_head list_node;
107 atomic_t segs_available;
108 u8 buffer[1]; /* For reads/writes on USB */
113 WA_DTI_TRANSFER_RESULT_PENDING,
114 WA_DTI_ISOC_PACKET_STATUS_PENDING,
115 WA_DTI_BUF_IN_DATA_PENDING
120 * The Alereon HWA expects the data frames in isochronous transfer
121 * requests to be concatenated and not sent as separate packets.
123 WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC = 0x01,
125 * The Alereon HWA can be instructed to not send transfer notifications
126 * as an optimization.
128 WUSB_QUIRK_ALEREON_HWA_DISABLE_XFER_NOTIFICATIONS = 0x02,
131 enum wa_vendor_specific_requests {
132 WA_REQ_ALEREON_DISABLE_XFER_NOTIFICATIONS = 0x4C,
133 WA_REQ_ALEREON_FEATURE_SET = 0x01,
134 WA_REQ_ALEREON_FEATURE_CLEAR = 0x00,
137 #define WA_MAX_BUF_IN_URBS 4
139 * Instance of a HWA Host Controller
141 * Except where a more specific lock/mutex applies or atomic, all
142 * fields protected by @mutex.
144 * @wa_descr Can be accessed without locking because it is in
145 * the same area where the device descriptors were
146 * read, so it is guaranteed to exist unmodified while
149 * Endianess has been converted to CPU's.
151 * @nep_* can be accessed without locking as its processing is
152 * serialized; we submit a NEP URB and it comes to
153 * hwahc_nep_cb(), which won't issue another URB until it is
154 * done processing it.
158 * List of active transfers to verify existence from a xfer id
159 * gotten from the xfer result message. Can't use urb->list because
160 * it goes by endpoint, and we don't know the endpoint at the time
161 * when we get the xfer result message. We can't really rely on the
162 * pointer (will have to change for 64 bits) as the xfer id is 32 bits.
164 * @xfer_delayed_list: List of transfers that need to be started
165 * (with a workqueue, because they were
166 * submitted from an atomic context).
168 * FIXME: this needs to be layered up: a wusbhc layer (for sharing
169 * commonalities with WHCI), a wa layer (for sharing
170 * commonalities with DWA-RC).
173 struct usb_device *usb_dev;
174 struct usb_interface *usb_iface;
176 /* HC to deliver notifications */
182 const struct usb_endpoint_descriptor *dto_epd, *dti_epd;
183 const struct usb_wa_descriptor *wa_descr;
185 struct urb *nep_urb; /* Notification EndPoint [lockless] */
188 size_t nep_buffer_size;
190 atomic_t notifs_queued;
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 */
199 * dti_state is used to track the state of the dti_urb. When dti_state
200 * is WA_DTI_ISOC_PACKET_STATUS_PENDING, dti_isoc_xfer_in_progress and
201 * dti_isoc_xfer_seg identify which xfer the incoming isoc packet
204 enum wa_dti_state dti_state;
205 u32 dti_isoc_xfer_in_progress;
206 u8 dti_isoc_xfer_seg;
207 struct urb *dti_urb; /* URB for reading xfer results */
208 /* URBs for reading data in */
209 struct urb buf_in_urbs[WA_MAX_BUF_IN_URBS];
210 int active_buf_in_urbs; /* number of buf_in_urbs active. */
211 struct edc dti_edc; /* DTI error density counter */
215 unsigned long dto_in_use; /* protect dto endoint serialization */
217 s32 status; /* For reading status */
219 struct list_head xfer_list;
220 struct list_head xfer_delayed_list;
221 struct list_head xfer_errored_list;
223 * lock for the above xfer lists. Can be taken while a xfer->lock is
224 * held but not in the reverse order.
226 spinlock_t xfer_list_lock;
227 struct work_struct xfer_enqueue_work;
228 struct work_struct xfer_error_work;
229 atomic_t xfer_id_count;
231 kernel_ulong_t quirks;
235 extern int wa_create(struct wahc *wa, struct usb_interface *iface,
237 extern void __wa_destroy(struct wahc *wa);
238 extern int wa_dti_start(struct wahc *wa);
239 void wa_reset_all(struct wahc *wa);
242 /* Miscellaneous constants */
244 /** Max number of EPROTO errors we tolerate on the NEP in a
246 HWAHC_EPROTO_MAX = 16,
247 /** Period of time for EPROTO errors (in jiffies) */
248 HWAHC_EPROTO_PERIOD = 4 * HZ,
252 /* Notification endpoint handling */
253 extern int wa_nep_create(struct wahc *, struct usb_interface *);
254 extern void wa_nep_destroy(struct wahc *);
256 static inline int wa_nep_arm(struct wahc *wa, gfp_t gfp_mask)
258 struct urb *urb = wa->nep_urb;
259 urb->transfer_buffer = wa->nep_buffer;
260 urb->transfer_buffer_length = wa->nep_buffer_size;
261 return usb_submit_urb(urb, gfp_mask);
264 static inline void wa_nep_disarm(struct wahc *wa)
266 usb_kill_urb(wa->nep_urb);
271 static inline void wa_rpipe_init(struct wahc *wa)
273 INIT_LIST_HEAD(&wa->rpipe_delayed_list);
274 spin_lock_init(&wa->rpipe_lock);
275 mutex_init(&wa->rpipe_mutex);
278 static inline void wa_init(struct wahc *wa)
282 edc_init(&wa->nep_edc);
283 atomic_set(&wa->notifs_queued, 0);
284 wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
286 edc_init(&wa->dti_edc);
287 INIT_LIST_HEAD(&wa->xfer_list);
288 INIT_LIST_HEAD(&wa->xfer_delayed_list);
289 INIT_LIST_HEAD(&wa->xfer_errored_list);
290 spin_lock_init(&wa->xfer_list_lock);
291 INIT_WORK(&wa->xfer_enqueue_work, wa_urb_enqueue_run);
292 INIT_WORK(&wa->xfer_error_work, wa_process_errored_transfers_run);
294 atomic_set(&wa->xfer_id_count, 1);
295 /* init the buf in URBs */
296 for (index = 0; index < WA_MAX_BUF_IN_URBS; ++index)
297 usb_init_urb(&(wa->buf_in_urbs[index]));
298 wa->active_buf_in_urbs = 0;
302 * Destroy a pipe (when refcount drops to zero)
304 * Assumes it has been moved to the "QUIESCING" state.
307 extern void rpipe_destroy(struct kref *_rpipe);
309 void __rpipe_get(struct wa_rpipe *rpipe)
311 kref_get(&rpipe->refcnt);
313 extern int rpipe_get_by_ep(struct wahc *, struct usb_host_endpoint *,
314 struct urb *, gfp_t);
315 static inline void rpipe_put(struct wa_rpipe *rpipe)
317 kref_put(&rpipe->refcnt, rpipe_destroy);
320 extern void rpipe_ep_disable(struct wahc *, struct usb_host_endpoint *);
321 extern void rpipe_clear_feature_stalled(struct wahc *,
322 struct usb_host_endpoint *);
323 extern int wa_rpipes_create(struct wahc *);
324 extern void wa_rpipes_destroy(struct wahc *);
325 static inline void rpipe_avail_dec(struct wa_rpipe *rpipe)
327 atomic_dec(&rpipe->segs_available);
331 * Returns true if the rpipe is ready to submit more segments.
333 static inline int rpipe_avail_inc(struct wa_rpipe *rpipe)
335 return atomic_inc_return(&rpipe->segs_available) > 0
336 && !list_empty(&rpipe->seg_list);
340 /* Transferring data */
341 extern int wa_urb_enqueue(struct wahc *, struct usb_host_endpoint *,
342 struct urb *, gfp_t);
343 extern int wa_urb_dequeue(struct wahc *, struct urb *, int);
344 extern void wa_handle_notif_xfer(struct wahc *, struct wa_notif_hdr *);
349 * FIXME: Refcounting for the actual @hwahc object is not correct; I
350 * mean, this should be refcounting on the HCD underneath, but
351 * it is not. In any case, the semantics for HCD refcounting
352 * are *weird*...on refcount reaching zero it just frees
353 * it...no RC specific function is called...unless I miss
356 * FIXME: has to go away in favour of a 'struct' hcd based solution
358 static inline struct wahc *wa_get(struct wahc *wa)
360 usb_get_intf(wa->usb_iface);
364 static inline void wa_put(struct wahc *wa)
366 usb_put_intf(wa->usb_iface);
370 static inline int __wa_feature(struct wahc *wa, unsigned op, u16 feature)
372 return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
373 op ? USB_REQ_SET_FEATURE : USB_REQ_CLEAR_FEATURE,
374 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
376 wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
377 NULL, 0, USB_CTRL_SET_TIMEOUT);
381 static inline int __wa_set_feature(struct wahc *wa, u16 feature)
383 return __wa_feature(wa, 1, feature);
387 static inline int __wa_clear_feature(struct wahc *wa, u16 feature)
389 return __wa_feature(wa, 0, feature);
394 * Return the status of a Wire Adapter
396 * @wa: Wire Adapter instance
397 * @returns < 0 errno code on error, or status bitmap as described
398 * in WUSB1.0[8.3.1.6].
400 * NOTE: need malloc, some arches don't take USB from the stack
403 s32 __wa_get_status(struct wahc *wa)
406 result = usb_control_msg(
407 wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0),
409 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
410 0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
411 &wa->status, sizeof(wa->status), USB_CTRL_GET_TIMEOUT);
419 * Waits until the Wire Adapter's status matches @mask/@value
421 * @wa: Wire Adapter instance.
422 * @returns < 0 errno code on error, otherwise status.
424 * Loop until the WAs status matches the mask and value (status & mask
425 * == value). Timeout if it doesn't happen.
427 * FIXME: is there an official specification on how long status
430 static inline s32 __wa_wait_status(struct wahc *wa, u32 mask, u32 value)
436 result = __wa_get_status(wa);
437 if ((result & mask) == value)
443 } while (result >= 0);
448 /** Command @hwahc to stop, @returns 0 if ok, < 0 errno code on error */
449 static inline int __wa_stop(struct wahc *wa)
452 struct device *dev = &wa->usb_iface->dev;
454 result = __wa_clear_feature(wa, WA_ENABLE);
455 if (result < 0 && result != -ENODEV) {
456 dev_err(dev, "error commanding HC to stop: %d\n", result);
459 result = __wa_wait_status(wa, WA_ENABLE, 0);
460 if (result < 0 && result != -ENODEV)
461 dev_err(dev, "error waiting for HC to stop: %d\n", result);
467 #endif /* #ifndef __HWAHC_INTERNAL_H__ */