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 80#ifndef __HWAHC_INTERNAL_H__ 81#define __HWAHC_INTERNAL_H__ 82 83#include <linux/completion.h> 84#include <linux/usb.h> 85#include <linux/mutex.h> 86#include <linux/spinlock.h> 87#include <linux/uwb.h> 88#include <linux/usb/wusb.h> 89#include <linux/usb/wusb-wa.h> 90 91struct wusbhc; 92struct wahc; 93extern void wa_urb_enqueue_run(struct work_struct *ws); 94 95/** 96 * RPipe instance 97 * 98 * @descr's fields are kept in LE, as we need to send it back and 99 * forth. 100 * 101 * @wa is referenced when set 102 * 103 * @segs_available is the number of requests segments that still can 104 * be submitted to the controller without overloading 105 * it. It is initialized to descr->wRequests when 106 * aiming. 107 * 108 * A rpipe supports a max of descr->wRequests at the same time; before 109 * submitting seg_lock has to be taken. If segs_avail > 0, then we can 110 * submit; if not, we have to queue them. 111 */ 112struct wa_rpipe { 113 struct kref refcnt; 114 struct usb_rpipe_descriptor descr; 115 struct usb_host_endpoint *ep; 116 struct wahc *wa; 117 spinlock_t seg_lock; 118 struct list_head seg_list; 119 atomic_t segs_available; 120 u8 buffer[1]; /* For reads/writes on USB */ 121}; 122 123 124struct wahc { 125 struct usb_device *usb_dev; 126 struct usb_interface *usb_iface; 127 128 /* HC to deliver notifications */ 129 union { 130 struct wusbhc *wusb; 131 struct dwahc *dwa; 132 }; 133 134 const struct usb_endpoint_descriptor *dto_epd, *dti_epd; 135 const struct usb_wa_descriptor *wa_descr; 136 137 struct urb *nep_urb; /* Notification EndPoint [lockless] */ 138 struct edc nep_edc; 139 void *nep_buffer; 140 size_t nep_buffer_size; 141 142 atomic_t notifs_queued; 143 144 u16 rpipes; 145 unsigned long *rpipe_bm; /* rpipe usage bitmap */ 146 spinlock_t rpipe_bm_lock; /* protect rpipe_bm */ 147 struct mutex rpipe_mutex; /* assigning resources to endpoints */ 148 149 struct urb *dti_urb; /* URB for reading xfer results */ 150 struct urb *buf_in_urb; /* URB for reading data in */ 151 struct edc dti_edc; /* DTI error density counter */ 152 struct wa_xfer_result *xfer_result; /* real size = dti_ep maxpktsize */ 153 size_t xfer_result_size; 154 155 s32 status; /* For reading status */ 156 157 struct list_head xfer_list; 158 struct list_head xfer_delayed_list; 159 spinlock_t xfer_list_lock; 160 struct work_struct xfer_work; 161 atomic_t xfer_id_count; 162}; 163 164 165extern int wa_create(struct wahc *wa, struct usb_interface *iface); 166extern void __wa_destroy(struct wahc *wa); 167void wa_reset_all(struct wahc *wa); 168 169 170/* Miscellaneous constants */ 171enum { 172 /** Max number of EPROTO errors we tolerate on the NEP in a 173 * period of time */ 174 HWAHC_EPROTO_MAX = 16, 175 /** Period of time for EPROTO errors (in jiffies) */ 176 HWAHC_EPROTO_PERIOD = 4 * HZ, 177}; 178 179 180/* Notification endpoint handling */ 181extern int wa_nep_create(struct wahc *, struct usb_interface *); 182extern void wa_nep_destroy(struct wahc *); 183 184static inline int wa_nep_arm(struct wahc *wa, gfp_t gfp_mask) 185{ 186 struct urb *urb = wa->nep_urb; 187 urb->transfer_buffer = wa->nep_buffer; 188 urb->transfer_buffer_length = wa->nep_buffer_size; 189 return usb_submit_urb(urb, gfp_mask); 190} 191 192static inline void wa_nep_disarm(struct wahc *wa) 193{ 194 usb_kill_urb(wa->nep_urb); 195} 196 197 198/* RPipes */ 199static inline void wa_rpipe_init(struct wahc *wa) 200{ 201 spin_lock_init(&wa->rpipe_bm_lock); 202 mutex_init(&wa->rpipe_mutex); 203} 204 205static inline void wa_init(struct wahc *wa) 206{ 207 edc_init(&wa->nep_edc); 208 atomic_set(&wa->notifs_queued, 0); 209 wa_rpipe_init(wa); 210 edc_init(&wa->dti_edc); 211 INIT_LIST_HEAD(&wa->xfer_list); 212 INIT_LIST_HEAD(&wa->xfer_delayed_list); 213 spin_lock_init(&wa->xfer_list_lock); 214 INIT_WORK(&wa->xfer_work, wa_urb_enqueue_run); 215 atomic_set(&wa->xfer_id_count, 1); 216} 217 218/** 219 * Destroy a pipe (when refcount drops to zero) 220 * 221 * Assumes it has been moved to the "QUIESCING" state. 222 */ 223struct wa_xfer; 224extern void rpipe_destroy(struct kref *_rpipe); 225static inline 226void __rpipe_get(struct wa_rpipe *rpipe) 227{ 228 kref_get(&rpipe->refcnt); 229} 230extern int rpipe_get_by_ep(struct wahc *, struct usb_host_endpoint *, 231 struct urb *, gfp_t); 232static inline void rpipe_put(struct wa_rpipe *rpipe) 233{ 234 kref_put(&rpipe->refcnt, rpipe_destroy); 235 236} 237extern void rpipe_ep_disable(struct wahc *, struct usb_host_endpoint *); 238extern int wa_rpipes_create(struct wahc *); 239extern void wa_rpipes_destroy(struct wahc *); 240static inline void rpipe_avail_dec(struct wa_rpipe *rpipe) 241{ 242 atomic_dec(&rpipe->segs_available); 243} 244 245/** 246 * Returns true if the rpipe is ready to submit more segments. 247 */ 248static inline int rpipe_avail_inc(struct wa_rpipe *rpipe) 249{ 250 return atomic_inc_return(&rpipe->segs_available) > 0 251 && !list_empty(&rpipe->seg_list); 252} 253 254 255/* Transferring data */ 256extern int wa_urb_enqueue(struct wahc *, struct usb_host_endpoint *, 257 struct urb *, gfp_t); 258extern int wa_urb_dequeue(struct wahc *, struct urb *); 259extern void wa_handle_notif_xfer(struct wahc *, struct wa_notif_hdr *); 260 261 262static inline struct wahc *wa_get(struct wahc *wa) 263{ 264 usb_get_intf(wa->usb_iface); 265 return wa; 266} 267 268static inline void wa_put(struct wahc *wa) 269{ 270 usb_put_intf(wa->usb_iface); 271} 272 273 274static inline int __wa_feature(struct wahc *wa, unsigned op, u16 feature) 275{ 276 return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0), 277 op ? USB_REQ_SET_FEATURE : USB_REQ_CLEAR_FEATURE, 278 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 279 feature, 280 wa->usb_iface->cur_altsetting->desc.bInterfaceNumber, 281 NULL, 0, 1000); 282} 283 284 285static inline int __wa_set_feature(struct wahc *wa, u16 feature) 286{ 287 return __wa_feature(wa, 1, feature); 288} 289 290 291static inline int __wa_clear_feature(struct wahc *wa, u16 feature) 292{ 293 return __wa_feature(wa, 0, feature); 294} 295 296 297/** 298 * Return the status of a Wire Adapter 299 * 300 * @wa: Wire Adapter instance 301 * @returns < 0 errno code on error, or status bitmap as described 302 * in WUSB1.0[8.3.1.6]. 303 * 304 * NOTE: need malloc, some arches don't take USB from the stack 305 */ 306static inline 307s32 __wa_get_status(struct wahc *wa) 308{ 309 s32 result; 310 result = usb_control_msg( 311 wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0), 312 USB_REQ_GET_STATUS, 313 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 314 0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber, 315 &wa->status, sizeof(wa->status), 316 1000); 317 if (result >= 0) 318 result = wa->status; 319 return result; 320} 321 322 323static inline s32 __wa_wait_status(struct wahc *wa, u32 mask, u32 value) 324{ 325 s32 result; 326 unsigned loops = 10; 327 do { 328 msleep(50); 329 result = __wa_get_status(wa); 330 if ((result & mask) == value) 331 break; 332 if (loops-- == 0) { 333 result = -ETIMEDOUT; 334 break; 335 } 336 } while (result >= 0); 337 return result; 338} 339 340 341/** Command @hwahc to stop, @returns 0 if ok, < 0 errno code on error */ 342static inline int __wa_stop(struct wahc *wa) 343{ 344 int result; 345 struct device *dev = &wa->usb_iface->dev; 346 347 result = __wa_clear_feature(wa, WA_ENABLE); 348 if (result < 0 && result != -ENODEV) { 349 dev_err(dev, "error commanding HC to stop: %d\n", result); 350 goto out; 351 } 352 result = __wa_wait_status(wa, WA_ENABLE, 0); 353 if (result < 0 && result != -ENODEV) 354 dev_err(dev, "error waiting for HC to stop: %d\n", result); 355out: 356 return 0; 357} 358 359 360#endif /* #ifndef __HWAHC_INTERNAL_H__ */ 361