1*23cd1385SRemy Bohmer /* 2*23cd1385SRemy Bohmer * <linux/usb/gadget.h> 3*23cd1385SRemy Bohmer * 4*23cd1385SRemy Bohmer * We call the USB code inside a Linux-based peripheral device a "gadget" 5*23cd1385SRemy Bohmer * driver, except for the hardware-specific bus glue. One USB host can 6*23cd1385SRemy Bohmer * master many USB gadgets, but the gadgets are only slaved to one host. 7*23cd1385SRemy Bohmer * 8*23cd1385SRemy Bohmer * 9*23cd1385SRemy Bohmer * (C) Copyright 2002-2004 by David Brownell 10*23cd1385SRemy Bohmer * All Rights Reserved. 11*23cd1385SRemy Bohmer * 12*23cd1385SRemy Bohmer * This software is licensed under the GNU GPL version 2. 13*23cd1385SRemy Bohmer * 14*23cd1385SRemy Bohmer * Ported to U-boot by: Thomas Smits <ts.smits@gmail.com> and 15*23cd1385SRemy Bohmer * Remy Bohmer <linux@bohmer.net> 16*23cd1385SRemy Bohmer */ 17*23cd1385SRemy Bohmer 18*23cd1385SRemy Bohmer #ifndef __LINUX_USB_GADGET_H 19*23cd1385SRemy Bohmer #define __LINUX_USB_GADGET_H 20*23cd1385SRemy Bohmer 21*23cd1385SRemy Bohmer #include <linux/list.h> 22*23cd1385SRemy Bohmer 23*23cd1385SRemy Bohmer struct usb_ep; 24*23cd1385SRemy Bohmer 25*23cd1385SRemy Bohmer /** 26*23cd1385SRemy Bohmer * struct usb_request - describes one i/o request 27*23cd1385SRemy Bohmer * @buf: Buffer used for data. Always provide this; some controllers 28*23cd1385SRemy Bohmer * only use PIO, or don't use DMA for some endpoints. 29*23cd1385SRemy Bohmer * @dma: DMA address corresponding to 'buf'. If you don't set this 30*23cd1385SRemy Bohmer * field, and the usb controller needs one, it is responsible 31*23cd1385SRemy Bohmer * for mapping and unmapping the buffer. 32*23cd1385SRemy Bohmer * @length: Length of that data 33*23cd1385SRemy Bohmer * @no_interrupt: If true, hints that no completion irq is needed. 34*23cd1385SRemy Bohmer * Helpful sometimes with deep request queues that are handled 35*23cd1385SRemy Bohmer * directly by DMA controllers. 36*23cd1385SRemy Bohmer * @zero: If true, when writing data, makes the last packet be "short" 37*23cd1385SRemy Bohmer * by adding a zero length packet as needed; 38*23cd1385SRemy Bohmer * @short_not_ok: When reading data, makes short packets be 39*23cd1385SRemy Bohmer * treated as errors (queue stops advancing till cleanup). 40*23cd1385SRemy Bohmer * @complete: Function called when request completes, so this request and 41*23cd1385SRemy Bohmer * its buffer may be re-used. 42*23cd1385SRemy Bohmer * Reads terminate with a short packet, or when the buffer fills, 43*23cd1385SRemy Bohmer * whichever comes first. When writes terminate, some data bytes 44*23cd1385SRemy Bohmer * will usually still be in flight (often in a hardware fifo). 45*23cd1385SRemy Bohmer * Errors (for reads or writes) stop the queue from advancing 46*23cd1385SRemy Bohmer * until the completion function returns, so that any transfers 47*23cd1385SRemy Bohmer * invalidated by the error may first be dequeued. 48*23cd1385SRemy Bohmer * @context: For use by the completion callback 49*23cd1385SRemy Bohmer * @list: For use by the gadget driver. 50*23cd1385SRemy Bohmer * @status: Reports completion code, zero or a negative errno. 51*23cd1385SRemy Bohmer * Normally, faults block the transfer queue from advancing until 52*23cd1385SRemy Bohmer * the completion callback returns. 53*23cd1385SRemy Bohmer * Code "-ESHUTDOWN" indicates completion caused by device disconnect, 54*23cd1385SRemy Bohmer * or when the driver disabled the endpoint. 55*23cd1385SRemy Bohmer * @actual: Reports bytes transferred to/from the buffer. For reads (OUT 56*23cd1385SRemy Bohmer * transfers) this may be less than the requested length. If the 57*23cd1385SRemy Bohmer * short_not_ok flag is set, short reads are treated as errors 58*23cd1385SRemy Bohmer * even when status otherwise indicates successful completion. 59*23cd1385SRemy Bohmer * Note that for writes (IN transfers) some data bytes may still 60*23cd1385SRemy Bohmer * reside in a device-side FIFO when the request is reported as 61*23cd1385SRemy Bohmer * complete. 62*23cd1385SRemy Bohmer * 63*23cd1385SRemy Bohmer * These are allocated/freed through the endpoint they're used with. The 64*23cd1385SRemy Bohmer * hardware's driver can add extra per-request data to the memory it returns, 65*23cd1385SRemy Bohmer * which often avoids separate memory allocations (potential failures), 66*23cd1385SRemy Bohmer * later when the request is queued. 67*23cd1385SRemy Bohmer * 68*23cd1385SRemy Bohmer * Request flags affect request handling, such as whether a zero length 69*23cd1385SRemy Bohmer * packet is written (the "zero" flag), whether a short read should be 70*23cd1385SRemy Bohmer * treated as an error (blocking request queue advance, the "short_not_ok" 71*23cd1385SRemy Bohmer * flag), or hinting that an interrupt is not required (the "no_interrupt" 72*23cd1385SRemy Bohmer * flag, for use with deep request queues). 73*23cd1385SRemy Bohmer * 74*23cd1385SRemy Bohmer * Bulk endpoints can use any size buffers, and can also be used for interrupt 75*23cd1385SRemy Bohmer * transfers. interrupt-only endpoints can be much less functional. 76*23cd1385SRemy Bohmer */ 77*23cd1385SRemy Bohmer // NOTE this is analagous to 'struct urb' on the host side, 78*23cd1385SRemy Bohmer // except that it's thinner and promotes more pre-allocation. 79*23cd1385SRemy Bohmer 80*23cd1385SRemy Bohmer struct usb_request { 81*23cd1385SRemy Bohmer void *buf; 82*23cd1385SRemy Bohmer unsigned length; 83*23cd1385SRemy Bohmer dma_addr_t dma; 84*23cd1385SRemy Bohmer 85*23cd1385SRemy Bohmer unsigned no_interrupt:1; 86*23cd1385SRemy Bohmer unsigned zero:1; 87*23cd1385SRemy Bohmer unsigned short_not_ok:1; 88*23cd1385SRemy Bohmer 89*23cd1385SRemy Bohmer void (*complete)(struct usb_ep *ep, 90*23cd1385SRemy Bohmer struct usb_request *req); 91*23cd1385SRemy Bohmer void *context; 92*23cd1385SRemy Bohmer struct list_head list; 93*23cd1385SRemy Bohmer 94*23cd1385SRemy Bohmer int status; 95*23cd1385SRemy Bohmer unsigned actual; 96*23cd1385SRemy Bohmer }; 97*23cd1385SRemy Bohmer 98*23cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 99*23cd1385SRemy Bohmer 100*23cd1385SRemy Bohmer /* endpoint-specific parts of the api to the usb controller hardware. 101*23cd1385SRemy Bohmer * unlike the urb model, (de)multiplexing layers are not required. 102*23cd1385SRemy Bohmer * (so this api could slash overhead if used on the host side...) 103*23cd1385SRemy Bohmer * 104*23cd1385SRemy Bohmer * note that device side usb controllers commonly differ in how many 105*23cd1385SRemy Bohmer * endpoints they support, as well as their capabilities. 106*23cd1385SRemy Bohmer */ 107*23cd1385SRemy Bohmer struct usb_ep_ops { 108*23cd1385SRemy Bohmer int (*enable) (struct usb_ep *ep, 109*23cd1385SRemy Bohmer const struct usb_endpoint_descriptor *desc); 110*23cd1385SRemy Bohmer int (*disable) (struct usb_ep *ep); 111*23cd1385SRemy Bohmer 112*23cd1385SRemy Bohmer struct usb_request *(*alloc_request) (struct usb_ep *ep, 113*23cd1385SRemy Bohmer gfp_t gfp_flags); 114*23cd1385SRemy Bohmer void (*free_request) (struct usb_ep *ep, struct usb_request *req); 115*23cd1385SRemy Bohmer 116*23cd1385SRemy Bohmer int (*queue) (struct usb_ep *ep, struct usb_request *req, 117*23cd1385SRemy Bohmer gfp_t gfp_flags); 118*23cd1385SRemy Bohmer int (*dequeue) (struct usb_ep *ep, struct usb_request *req); 119*23cd1385SRemy Bohmer 120*23cd1385SRemy Bohmer int (*set_halt) (struct usb_ep *ep, int value); 121*23cd1385SRemy Bohmer int (*fifo_status) (struct usb_ep *ep); 122*23cd1385SRemy Bohmer void (*fifo_flush) (struct usb_ep *ep); 123*23cd1385SRemy Bohmer }; 124*23cd1385SRemy Bohmer 125*23cd1385SRemy Bohmer /** 126*23cd1385SRemy Bohmer * struct usb_ep - device side representation of USB endpoint 127*23cd1385SRemy Bohmer * @name:identifier for the endpoint, such as "ep-a" or "ep9in-bulk" 128*23cd1385SRemy Bohmer * @ops: Function pointers used to access hardware-specific operations. 129*23cd1385SRemy Bohmer * @ep_list:the gadget's ep_list holds all of its endpoints 130*23cd1385SRemy Bohmer * @maxpacket:The maximum packet size used on this endpoint. The initial 131*23cd1385SRemy Bohmer * value can sometimes be reduced (hardware allowing), according to 132*23cd1385SRemy Bohmer * the endpoint descriptor used to configure the endpoint. 133*23cd1385SRemy Bohmer * @driver_data:for use by the gadget driver. all other fields are 134*23cd1385SRemy Bohmer * read-only to gadget drivers. 135*23cd1385SRemy Bohmer * 136*23cd1385SRemy Bohmer * the bus controller driver lists all the general purpose endpoints in 137*23cd1385SRemy Bohmer * gadget->ep_list. the control endpoint (gadget->ep0) is not in that list, 138*23cd1385SRemy Bohmer * and is accessed only in response to a driver setup() callback. 139*23cd1385SRemy Bohmer */ 140*23cd1385SRemy Bohmer struct usb_ep { 141*23cd1385SRemy Bohmer void *driver_data; 142*23cd1385SRemy Bohmer const char *name; 143*23cd1385SRemy Bohmer const struct usb_ep_ops *ops; 144*23cd1385SRemy Bohmer struct list_head ep_list; 145*23cd1385SRemy Bohmer unsigned maxpacket:16; 146*23cd1385SRemy Bohmer }; 147*23cd1385SRemy Bohmer 148*23cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 149*23cd1385SRemy Bohmer 150*23cd1385SRemy Bohmer /** 151*23cd1385SRemy Bohmer * usb_ep_enable - configure endpoint, making it usable 152*23cd1385SRemy Bohmer * @ep:the endpoint being configured. may not be the endpoint named "ep0". 153*23cd1385SRemy Bohmer * drivers discover endpoints through the ep_list of a usb_gadget. 154*23cd1385SRemy Bohmer * @desc:descriptor for desired behavior. caller guarantees this pointer 155*23cd1385SRemy Bohmer * remains valid until the endpoint is disabled; the data byte order 156*23cd1385SRemy Bohmer * is little-endian (usb-standard). 157*23cd1385SRemy Bohmer * 158*23cd1385SRemy Bohmer * when configurations are set, or when interface settings change, the driver 159*23cd1385SRemy Bohmer * will enable or disable the relevant endpoints. while it is enabled, an 160*23cd1385SRemy Bohmer * endpoint may be used for i/o until the driver receives a disconnect() from 161*23cd1385SRemy Bohmer * the host or until the endpoint is disabled. 162*23cd1385SRemy Bohmer * 163*23cd1385SRemy Bohmer * the ep0 implementation (which calls this routine) must ensure that the 164*23cd1385SRemy Bohmer * hardware capabilities of each endpoint match the descriptor provided 165*23cd1385SRemy Bohmer * for it. for example, an endpoint named "ep2in-bulk" would be usable 166*23cd1385SRemy Bohmer * for interrupt transfers as well as bulk, but it likely couldn't be used 167*23cd1385SRemy Bohmer * for iso transfers or for endpoint 14. some endpoints are fully 168*23cd1385SRemy Bohmer * configurable, with more generic names like "ep-a". (remember that for 169*23cd1385SRemy Bohmer * USB, "in" means "towards the USB master".) 170*23cd1385SRemy Bohmer * 171*23cd1385SRemy Bohmer * returns zero, or a negative error code. 172*23cd1385SRemy Bohmer */ 173*23cd1385SRemy Bohmer static inline int 174*23cd1385SRemy Bohmer usb_ep_enable (struct usb_ep *ep, const struct usb_endpoint_descriptor *desc) 175*23cd1385SRemy Bohmer { 176*23cd1385SRemy Bohmer return ep->ops->enable (ep, desc); 177*23cd1385SRemy Bohmer } 178*23cd1385SRemy Bohmer 179*23cd1385SRemy Bohmer /** 180*23cd1385SRemy Bohmer * usb_ep_disable - endpoint is no longer usable 181*23cd1385SRemy Bohmer * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0". 182*23cd1385SRemy Bohmer * 183*23cd1385SRemy Bohmer * no other task may be using this endpoint when this is called. 184*23cd1385SRemy Bohmer * any pending and uncompleted requests will complete with status 185*23cd1385SRemy Bohmer * indicating disconnect (-ESHUTDOWN) before this call returns. 186*23cd1385SRemy Bohmer * gadget drivers must call usb_ep_enable() again before queueing 187*23cd1385SRemy Bohmer * requests to the endpoint. 188*23cd1385SRemy Bohmer * 189*23cd1385SRemy Bohmer * returns zero, or a negative error code. 190*23cd1385SRemy Bohmer */ 191*23cd1385SRemy Bohmer static inline int 192*23cd1385SRemy Bohmer usb_ep_disable (struct usb_ep *ep) 193*23cd1385SRemy Bohmer { 194*23cd1385SRemy Bohmer return ep->ops->disable (ep); 195*23cd1385SRemy Bohmer } 196*23cd1385SRemy Bohmer 197*23cd1385SRemy Bohmer /** 198*23cd1385SRemy Bohmer * usb_ep_alloc_request - allocate a request object to use with this endpoint 199*23cd1385SRemy Bohmer * @ep:the endpoint to be used with with the request 200*23cd1385SRemy Bohmer * @gfp_flags:GFP_* flags to use 201*23cd1385SRemy Bohmer * 202*23cd1385SRemy Bohmer * Request objects must be allocated with this call, since they normally 203*23cd1385SRemy Bohmer * need controller-specific setup and may even need endpoint-specific 204*23cd1385SRemy Bohmer * resources such as allocation of DMA descriptors. 205*23cd1385SRemy Bohmer * Requests may be submitted with usb_ep_queue(), and receive a single 206*23cd1385SRemy Bohmer * completion callback. Free requests with usb_ep_free_request(), when 207*23cd1385SRemy Bohmer * they are no longer needed. 208*23cd1385SRemy Bohmer * 209*23cd1385SRemy Bohmer * Returns the request, or null if one could not be allocated. 210*23cd1385SRemy Bohmer */ 211*23cd1385SRemy Bohmer static inline struct usb_request * 212*23cd1385SRemy Bohmer usb_ep_alloc_request (struct usb_ep *ep, gfp_t gfp_flags) 213*23cd1385SRemy Bohmer { 214*23cd1385SRemy Bohmer return ep->ops->alloc_request (ep, gfp_flags); 215*23cd1385SRemy Bohmer } 216*23cd1385SRemy Bohmer 217*23cd1385SRemy Bohmer /** 218*23cd1385SRemy Bohmer * usb_ep_free_request - frees a request object 219*23cd1385SRemy Bohmer * @ep:the endpoint associated with the request 220*23cd1385SRemy Bohmer * @req:the request being freed 221*23cd1385SRemy Bohmer * 222*23cd1385SRemy Bohmer * Reverses the effect of usb_ep_alloc_request(). 223*23cd1385SRemy Bohmer * Caller guarantees the request is not queued, and that it will 224*23cd1385SRemy Bohmer * no longer be requeued (or otherwise used). 225*23cd1385SRemy Bohmer */ 226*23cd1385SRemy Bohmer static inline void 227*23cd1385SRemy Bohmer usb_ep_free_request (struct usb_ep *ep, struct usb_request *req) 228*23cd1385SRemy Bohmer { 229*23cd1385SRemy Bohmer ep->ops->free_request (ep, req); 230*23cd1385SRemy Bohmer } 231*23cd1385SRemy Bohmer 232*23cd1385SRemy Bohmer /** 233*23cd1385SRemy Bohmer * usb_ep_queue - queues (submits) an I/O request to an endpoint. 234*23cd1385SRemy Bohmer * @ep:the endpoint associated with the request 235*23cd1385SRemy Bohmer * @req:the request being submitted 236*23cd1385SRemy Bohmer * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't 237*23cd1385SRemy Bohmer * pre-allocate all necessary memory with the request. 238*23cd1385SRemy Bohmer * 239*23cd1385SRemy Bohmer * This tells the device controller to perform the specified request through 240*23cd1385SRemy Bohmer * that endpoint (reading or writing a buffer). When the request completes, 241*23cd1385SRemy Bohmer * including being canceled by usb_ep_dequeue(), the request's completion 242*23cd1385SRemy Bohmer * routine is called to return the request to the driver. Any endpoint 243*23cd1385SRemy Bohmer * (except control endpoints like ep0) may have more than one transfer 244*23cd1385SRemy Bohmer * request queued; they complete in FIFO order. Once a gadget driver 245*23cd1385SRemy Bohmer * submits a request, that request may not be examined or modified until it 246*23cd1385SRemy Bohmer * is given back to that driver through the completion callback. 247*23cd1385SRemy Bohmer * 248*23cd1385SRemy Bohmer * Each request is turned into one or more packets. The controller driver 249*23cd1385SRemy Bohmer * never merges adjacent requests into the same packet. OUT transfers 250*23cd1385SRemy Bohmer * will sometimes use data that's already buffered in the hardware. 251*23cd1385SRemy Bohmer * Drivers can rely on the fact that the first byte of the request's buffer 252*23cd1385SRemy Bohmer * always corresponds to the first byte of some USB packet, for both 253*23cd1385SRemy Bohmer * IN and OUT transfers. 254*23cd1385SRemy Bohmer * 255*23cd1385SRemy Bohmer * Bulk endpoints can queue any amount of data; the transfer is packetized 256*23cd1385SRemy Bohmer * automatically. The last packet will be short if the request doesn't fill it 257*23cd1385SRemy Bohmer * out completely. Zero length packets (ZLPs) should be avoided in portable 258*23cd1385SRemy Bohmer * protocols since not all usb hardware can successfully handle zero length 259*23cd1385SRemy Bohmer * packets. (ZLPs may be explicitly written, and may be implicitly written if 260*23cd1385SRemy Bohmer * the request 'zero' flag is set.) Bulk endpoints may also be used 261*23cd1385SRemy Bohmer * for interrupt transfers; but the reverse is not true, and some endpoints 262*23cd1385SRemy Bohmer * won't support every interrupt transfer. (Such as 768 byte packets.) 263*23cd1385SRemy Bohmer * 264*23cd1385SRemy Bohmer * Interrupt-only endpoints are less functional than bulk endpoints, for 265*23cd1385SRemy Bohmer * example by not supporting queueing or not handling buffers that are 266*23cd1385SRemy Bohmer * larger than the endpoint's maxpacket size. They may also treat data 267*23cd1385SRemy Bohmer * toggle differently. 268*23cd1385SRemy Bohmer * 269*23cd1385SRemy Bohmer * Control endpoints ... after getting a setup() callback, the driver queues 270*23cd1385SRemy Bohmer * one response (even if it would be zero length). That enables the 271*23cd1385SRemy Bohmer * status ack, after transfering data as specified in the response. Setup 272*23cd1385SRemy Bohmer * functions may return negative error codes to generate protocol stalls. 273*23cd1385SRemy Bohmer * (Note that some USB device controllers disallow protocol stall responses 274*23cd1385SRemy Bohmer * in some cases.) When control responses are deferred (the response is 275*23cd1385SRemy Bohmer * written after the setup callback returns), then usb_ep_set_halt() may be 276*23cd1385SRemy Bohmer * used on ep0 to trigger protocol stalls. 277*23cd1385SRemy Bohmer * 278*23cd1385SRemy Bohmer * For periodic endpoints, like interrupt or isochronous ones, the usb host 279*23cd1385SRemy Bohmer * arranges to poll once per interval, and the gadget driver usually will 280*23cd1385SRemy Bohmer * have queued some data to transfer at that time. 281*23cd1385SRemy Bohmer * 282*23cd1385SRemy Bohmer * Returns zero, or a negative error code. Endpoints that are not enabled 283*23cd1385SRemy Bohmer * report errors; errors will also be 284*23cd1385SRemy Bohmer * reported when the usb peripheral is disconnected. 285*23cd1385SRemy Bohmer */ 286*23cd1385SRemy Bohmer static inline int 287*23cd1385SRemy Bohmer usb_ep_queue (struct usb_ep *ep, struct usb_request *req, gfp_t gfp_flags) 288*23cd1385SRemy Bohmer { 289*23cd1385SRemy Bohmer return ep->ops->queue (ep, req, gfp_flags); 290*23cd1385SRemy Bohmer } 291*23cd1385SRemy Bohmer 292*23cd1385SRemy Bohmer /** 293*23cd1385SRemy Bohmer * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint 294*23cd1385SRemy Bohmer * @ep:the endpoint associated with the request 295*23cd1385SRemy Bohmer * @req:the request being canceled 296*23cd1385SRemy Bohmer * 297*23cd1385SRemy Bohmer * if the request is still active on the endpoint, it is dequeued and its 298*23cd1385SRemy Bohmer * completion routine is called (with status -ECONNRESET); else a negative 299*23cd1385SRemy Bohmer * error code is returned. 300*23cd1385SRemy Bohmer * 301*23cd1385SRemy Bohmer * note that some hardware can't clear out write fifos (to unlink the request 302*23cd1385SRemy Bohmer * at the head of the queue) except as part of disconnecting from usb. such 303*23cd1385SRemy Bohmer * restrictions prevent drivers from supporting configuration changes, 304*23cd1385SRemy Bohmer * even to configuration zero (a "chapter 9" requirement). 305*23cd1385SRemy Bohmer */ 306*23cd1385SRemy Bohmer static inline int usb_ep_dequeue (struct usb_ep *ep, struct usb_request *req) 307*23cd1385SRemy Bohmer { 308*23cd1385SRemy Bohmer return ep->ops->dequeue (ep, req); 309*23cd1385SRemy Bohmer } 310*23cd1385SRemy Bohmer 311*23cd1385SRemy Bohmer /** 312*23cd1385SRemy Bohmer * usb_ep_set_halt - sets the endpoint halt feature. 313*23cd1385SRemy Bohmer * @ep: the non-isochronous endpoint being stalled 314*23cd1385SRemy Bohmer * 315*23cd1385SRemy Bohmer * Use this to stall an endpoint, perhaps as an error report. 316*23cd1385SRemy Bohmer * Except for control endpoints, 317*23cd1385SRemy Bohmer * the endpoint stays halted (will not stream any data) until the host 318*23cd1385SRemy Bohmer * clears this feature; drivers may need to empty the endpoint's request 319*23cd1385SRemy Bohmer * queue first, to make sure no inappropriate transfers happen. 320*23cd1385SRemy Bohmer * 321*23cd1385SRemy Bohmer * Note that while an endpoint CLEAR_FEATURE will be invisible to the 322*23cd1385SRemy Bohmer * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the 323*23cd1385SRemy Bohmer * current altsetting, see usb_ep_clear_halt(). When switching altsettings, 324*23cd1385SRemy Bohmer * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints. 325*23cd1385SRemy Bohmer * 326*23cd1385SRemy Bohmer * Returns zero, or a negative error code. On success, this call sets 327*23cd1385SRemy Bohmer * underlying hardware state that blocks data transfers. 328*23cd1385SRemy Bohmer * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any 329*23cd1385SRemy Bohmer * transfer requests are still queued, or if the controller hardware 330*23cd1385SRemy Bohmer * (usually a FIFO) still holds bytes that the host hasn't collected. 331*23cd1385SRemy Bohmer */ 332*23cd1385SRemy Bohmer static inline int 333*23cd1385SRemy Bohmer usb_ep_set_halt (struct usb_ep *ep) 334*23cd1385SRemy Bohmer { 335*23cd1385SRemy Bohmer return ep->ops->set_halt (ep, 1); 336*23cd1385SRemy Bohmer } 337*23cd1385SRemy Bohmer 338*23cd1385SRemy Bohmer /** 339*23cd1385SRemy Bohmer * usb_ep_clear_halt - clears endpoint halt, and resets toggle 340*23cd1385SRemy Bohmer * @ep:the bulk or interrupt endpoint being reset 341*23cd1385SRemy Bohmer * 342*23cd1385SRemy Bohmer * Use this when responding to the standard usb "set interface" request, 343*23cd1385SRemy Bohmer * for endpoints that aren't reconfigured, after clearing any other state 344*23cd1385SRemy Bohmer * in the endpoint's i/o queue. 345*23cd1385SRemy Bohmer * 346*23cd1385SRemy Bohmer * Returns zero, or a negative error code. On success, this call clears 347*23cd1385SRemy Bohmer * the underlying hardware state reflecting endpoint halt and data toggle. 348*23cd1385SRemy Bohmer * Note that some hardware can't support this request (like pxa2xx_udc), 349*23cd1385SRemy Bohmer * and accordingly can't correctly implement interface altsettings. 350*23cd1385SRemy Bohmer */ 351*23cd1385SRemy Bohmer static inline int 352*23cd1385SRemy Bohmer usb_ep_clear_halt (struct usb_ep *ep) 353*23cd1385SRemy Bohmer { 354*23cd1385SRemy Bohmer return ep->ops->set_halt (ep, 0); 355*23cd1385SRemy Bohmer } 356*23cd1385SRemy Bohmer 357*23cd1385SRemy Bohmer /** 358*23cd1385SRemy Bohmer * usb_ep_fifo_status - returns number of bytes in fifo, or error 359*23cd1385SRemy Bohmer * @ep: the endpoint whose fifo status is being checked. 360*23cd1385SRemy Bohmer * 361*23cd1385SRemy Bohmer * FIFO endpoints may have "unclaimed data" in them in certain cases, 362*23cd1385SRemy Bohmer * such as after aborted transfers. Hosts may not have collected all 363*23cd1385SRemy Bohmer * the IN data written by the gadget driver (and reported by a request 364*23cd1385SRemy Bohmer * completion). The gadget driver may not have collected all the data 365*23cd1385SRemy Bohmer * written OUT to it by the host. Drivers that need precise handling for 366*23cd1385SRemy Bohmer * fault reporting or recovery may need to use this call. 367*23cd1385SRemy Bohmer * 368*23cd1385SRemy Bohmer * This returns the number of such bytes in the fifo, or a negative 369*23cd1385SRemy Bohmer * errno if the endpoint doesn't use a FIFO or doesn't support such 370*23cd1385SRemy Bohmer * precise handling. 371*23cd1385SRemy Bohmer */ 372*23cd1385SRemy Bohmer static inline int 373*23cd1385SRemy Bohmer usb_ep_fifo_status (struct usb_ep *ep) 374*23cd1385SRemy Bohmer { 375*23cd1385SRemy Bohmer if (ep->ops->fifo_status) 376*23cd1385SRemy Bohmer return ep->ops->fifo_status (ep); 377*23cd1385SRemy Bohmer else 378*23cd1385SRemy Bohmer return -EOPNOTSUPP; 379*23cd1385SRemy Bohmer } 380*23cd1385SRemy Bohmer 381*23cd1385SRemy Bohmer /** 382*23cd1385SRemy Bohmer * usb_ep_fifo_flush - flushes contents of a fifo 383*23cd1385SRemy Bohmer * @ep: the endpoint whose fifo is being flushed. 384*23cd1385SRemy Bohmer * 385*23cd1385SRemy Bohmer * This call may be used to flush the "unclaimed data" that may exist in 386*23cd1385SRemy Bohmer * an endpoint fifo after abnormal transaction terminations. The call 387*23cd1385SRemy Bohmer * must never be used except when endpoint is not being used for any 388*23cd1385SRemy Bohmer * protocol translation. 389*23cd1385SRemy Bohmer */ 390*23cd1385SRemy Bohmer static inline void 391*23cd1385SRemy Bohmer usb_ep_fifo_flush (struct usb_ep *ep) 392*23cd1385SRemy Bohmer { 393*23cd1385SRemy Bohmer if (ep->ops->fifo_flush) 394*23cd1385SRemy Bohmer ep->ops->fifo_flush (ep); 395*23cd1385SRemy Bohmer } 396*23cd1385SRemy Bohmer 397*23cd1385SRemy Bohmer 398*23cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 399*23cd1385SRemy Bohmer 400*23cd1385SRemy Bohmer struct usb_gadget; 401*23cd1385SRemy Bohmer 402*23cd1385SRemy Bohmer /* the rest of the api to the controller hardware: device operations, 403*23cd1385SRemy Bohmer * which don't involve endpoints (or i/o). 404*23cd1385SRemy Bohmer */ 405*23cd1385SRemy Bohmer struct usb_gadget_ops { 406*23cd1385SRemy Bohmer int (*get_frame)(struct usb_gadget *); 407*23cd1385SRemy Bohmer int (*wakeup)(struct usb_gadget *); 408*23cd1385SRemy Bohmer int (*set_selfpowered) (struct usb_gadget *, int is_selfpowered); 409*23cd1385SRemy Bohmer int (*vbus_session) (struct usb_gadget *, int is_active); 410*23cd1385SRemy Bohmer int (*vbus_draw) (struct usb_gadget *, unsigned mA); 411*23cd1385SRemy Bohmer int (*pullup) (struct usb_gadget *, int is_on); 412*23cd1385SRemy Bohmer int (*ioctl)(struct usb_gadget *, 413*23cd1385SRemy Bohmer unsigned code, unsigned long param); 414*23cd1385SRemy Bohmer }; 415*23cd1385SRemy Bohmer 416*23cd1385SRemy Bohmer struct device { 417*23cd1385SRemy Bohmer void *driver_data; /* data private to the driver */ 418*23cd1385SRemy Bohmer }; 419*23cd1385SRemy Bohmer 420*23cd1385SRemy Bohmer /** 421*23cd1385SRemy Bohmer * struct usb_gadget - represents a usb slave device 422*23cd1385SRemy Bohmer * @ops: Function pointers used to access hardware-specific operations. 423*23cd1385SRemy Bohmer * @ep0: Endpoint zero, used when reading or writing responses to 424*23cd1385SRemy Bohmer * driver setup() requests 425*23cd1385SRemy Bohmer * @ep_list: List of other endpoints supported by the device. 426*23cd1385SRemy Bohmer * @speed: Speed of current connection to USB host. 427*23cd1385SRemy Bohmer * @is_dualspeed: True if the controller supports both high and full speed 428*23cd1385SRemy Bohmer * operation. If it does, the gadget driver must also support both. 429*23cd1385SRemy Bohmer * @is_otg: True if the USB device port uses a Mini-AB jack, so that the 430*23cd1385SRemy Bohmer * gadget driver must provide a USB OTG descriptor. 431*23cd1385SRemy Bohmer * @is_a_peripheral: False unless is_otg, the "A" end of a USB cable 432*23cd1385SRemy Bohmer * is in the Mini-AB jack, and HNP has been used to switch roles 433*23cd1385SRemy Bohmer * so that the "A" device currently acts as A-Peripheral, not A-Host. 434*23cd1385SRemy Bohmer * @a_hnp_support: OTG device feature flag, indicating that the A-Host 435*23cd1385SRemy Bohmer * supports HNP at this port. 436*23cd1385SRemy Bohmer * @a_alt_hnp_support: OTG device feature flag, indicating that the A-Host 437*23cd1385SRemy Bohmer * only supports HNP on a different root port. 438*23cd1385SRemy Bohmer * @b_hnp_enable: OTG device feature flag, indicating that the A-Host 439*23cd1385SRemy Bohmer * enabled HNP support. 440*23cd1385SRemy Bohmer * @name: Identifies the controller hardware type. Used in diagnostics 441*23cd1385SRemy Bohmer * and sometimes configuration. 442*23cd1385SRemy Bohmer * @dev: Driver model state for this abstract device. 443*23cd1385SRemy Bohmer * 444*23cd1385SRemy Bohmer * Gadgets have a mostly-portable "gadget driver" implementing device 445*23cd1385SRemy Bohmer * functions, handling all usb configurations and interfaces. Gadget 446*23cd1385SRemy Bohmer * drivers talk to hardware-specific code indirectly, through ops vectors. 447*23cd1385SRemy Bohmer * That insulates the gadget driver from hardware details, and packages 448*23cd1385SRemy Bohmer * the hardware endpoints through generic i/o queues. The "usb_gadget" 449*23cd1385SRemy Bohmer * and "usb_ep" interfaces provide that insulation from the hardware. 450*23cd1385SRemy Bohmer * 451*23cd1385SRemy Bohmer * Except for the driver data, all fields in this structure are 452*23cd1385SRemy Bohmer * read-only to the gadget driver. That driver data is part of the 453*23cd1385SRemy Bohmer * "driver model" infrastructure in 2.6 (and later) kernels, and for 454*23cd1385SRemy Bohmer * earlier systems is grouped in a similar structure that's not known 455*23cd1385SRemy Bohmer * to the rest of the kernel. 456*23cd1385SRemy Bohmer * 457*23cd1385SRemy Bohmer * Values of the three OTG device feature flags are updated before the 458*23cd1385SRemy Bohmer * setup() call corresponding to USB_REQ_SET_CONFIGURATION, and before 459*23cd1385SRemy Bohmer * driver suspend() calls. They are valid only when is_otg, and when the 460*23cd1385SRemy Bohmer * device is acting as a B-Peripheral (so is_a_peripheral is false). 461*23cd1385SRemy Bohmer */ 462*23cd1385SRemy Bohmer struct usb_gadget { 463*23cd1385SRemy Bohmer /* readonly to gadget driver */ 464*23cd1385SRemy Bohmer const struct usb_gadget_ops *ops; 465*23cd1385SRemy Bohmer struct usb_ep *ep0; 466*23cd1385SRemy Bohmer struct list_head ep_list; /* of usb_ep */ 467*23cd1385SRemy Bohmer enum usb_device_speed speed; 468*23cd1385SRemy Bohmer unsigned is_dualspeed:1; 469*23cd1385SRemy Bohmer unsigned is_otg:1; 470*23cd1385SRemy Bohmer unsigned is_a_peripheral:1; 471*23cd1385SRemy Bohmer unsigned b_hnp_enable:1; 472*23cd1385SRemy Bohmer unsigned a_hnp_support:1; 473*23cd1385SRemy Bohmer unsigned a_alt_hnp_support:1; 474*23cd1385SRemy Bohmer const char *name; 475*23cd1385SRemy Bohmer struct device dev; 476*23cd1385SRemy Bohmer }; 477*23cd1385SRemy Bohmer 478*23cd1385SRemy Bohmer static inline void set_gadget_data (struct usb_gadget *gadget, void *data) 479*23cd1385SRemy Bohmer { 480*23cd1385SRemy Bohmer gadget->dev.driver_data = data; 481*23cd1385SRemy Bohmer } 482*23cd1385SRemy Bohmer 483*23cd1385SRemy Bohmer static inline void *get_gadget_data (struct usb_gadget *gadget) 484*23cd1385SRemy Bohmer { 485*23cd1385SRemy Bohmer return gadget->dev.driver_data; 486*23cd1385SRemy Bohmer } 487*23cd1385SRemy Bohmer 488*23cd1385SRemy Bohmer /* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */ 489*23cd1385SRemy Bohmer #define gadget_for_each_ep(tmp,gadget) \ 490*23cd1385SRemy Bohmer list_for_each_entry(tmp, &(gadget)->ep_list, ep_list) 491*23cd1385SRemy Bohmer 492*23cd1385SRemy Bohmer 493*23cd1385SRemy Bohmer /** 494*23cd1385SRemy Bohmer * gadget_is_dualspeed - return true iff the hardware handles high speed 495*23cd1385SRemy Bohmer * @g: controller that might support both high and full speeds 496*23cd1385SRemy Bohmer */ 497*23cd1385SRemy Bohmer static inline int gadget_is_dualspeed(struct usb_gadget *g) 498*23cd1385SRemy Bohmer { 499*23cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED 500*23cd1385SRemy Bohmer /* runtime test would check "g->is_dualspeed" ... that might be 501*23cd1385SRemy Bohmer * useful to work around hardware bugs, but is mostly pointless 502*23cd1385SRemy Bohmer */ 503*23cd1385SRemy Bohmer return 1; 504*23cd1385SRemy Bohmer #else 505*23cd1385SRemy Bohmer return 0; 506*23cd1385SRemy Bohmer #endif 507*23cd1385SRemy Bohmer } 508*23cd1385SRemy Bohmer 509*23cd1385SRemy Bohmer /** 510*23cd1385SRemy Bohmer * gadget_is_otg - return true iff the hardware is OTG-ready 511*23cd1385SRemy Bohmer * @g: controller that might have a Mini-AB connector 512*23cd1385SRemy Bohmer * 513*23cd1385SRemy Bohmer * This is a runtime test, since kernels with a USB-OTG stack sometimes 514*23cd1385SRemy Bohmer * run on boards which only have a Mini-B (or Mini-A) connector. 515*23cd1385SRemy Bohmer */ 516*23cd1385SRemy Bohmer static inline int gadget_is_otg(struct usb_gadget *g) 517*23cd1385SRemy Bohmer { 518*23cd1385SRemy Bohmer #ifdef CONFIG_USB_OTG 519*23cd1385SRemy Bohmer return g->is_otg; 520*23cd1385SRemy Bohmer #else 521*23cd1385SRemy Bohmer return 0; 522*23cd1385SRemy Bohmer #endif 523*23cd1385SRemy Bohmer } 524*23cd1385SRemy Bohmer 525*23cd1385SRemy Bohmer 526*23cd1385SRemy Bohmer /** 527*23cd1385SRemy Bohmer * usb_gadget_frame_number - returns the current frame number 528*23cd1385SRemy Bohmer * @gadget: controller that reports the frame number 529*23cd1385SRemy Bohmer * 530*23cd1385SRemy Bohmer * Returns the usb frame number, normally eleven bits from a SOF packet, 531*23cd1385SRemy Bohmer * or negative errno if this device doesn't support this capability. 532*23cd1385SRemy Bohmer */ 533*23cd1385SRemy Bohmer static inline int usb_gadget_frame_number (struct usb_gadget *gadget) 534*23cd1385SRemy Bohmer { 535*23cd1385SRemy Bohmer return gadget->ops->get_frame (gadget); 536*23cd1385SRemy Bohmer } 537*23cd1385SRemy Bohmer 538*23cd1385SRemy Bohmer /** 539*23cd1385SRemy Bohmer * usb_gadget_wakeup - tries to wake up the host connected to this gadget 540*23cd1385SRemy Bohmer * @gadget: controller used to wake up the host 541*23cd1385SRemy Bohmer * 542*23cd1385SRemy Bohmer * Returns zero on success, else negative error code if the hardware 543*23cd1385SRemy Bohmer * doesn't support such attempts, or its support has not been enabled 544*23cd1385SRemy Bohmer * by the usb host. Drivers must return device descriptors that report 545*23cd1385SRemy Bohmer * their ability to support this, or hosts won't enable it. 546*23cd1385SRemy Bohmer * 547*23cd1385SRemy Bohmer * This may also try to use SRP to wake the host and start enumeration, 548*23cd1385SRemy Bohmer * even if OTG isn't otherwise in use. OTG devices may also start 549*23cd1385SRemy Bohmer * remote wakeup even when hosts don't explicitly enable it. 550*23cd1385SRemy Bohmer */ 551*23cd1385SRemy Bohmer static inline int usb_gadget_wakeup (struct usb_gadget *gadget) 552*23cd1385SRemy Bohmer { 553*23cd1385SRemy Bohmer if (!gadget->ops->wakeup) 554*23cd1385SRemy Bohmer return -EOPNOTSUPP; 555*23cd1385SRemy Bohmer return gadget->ops->wakeup (gadget); 556*23cd1385SRemy Bohmer } 557*23cd1385SRemy Bohmer 558*23cd1385SRemy Bohmer /** 559*23cd1385SRemy Bohmer * usb_gadget_set_selfpowered - sets the device selfpowered feature. 560*23cd1385SRemy Bohmer * @gadget:the device being declared as self-powered 561*23cd1385SRemy Bohmer * 562*23cd1385SRemy Bohmer * this affects the device status reported by the hardware driver 563*23cd1385SRemy Bohmer * to reflect that it now has a local power supply. 564*23cd1385SRemy Bohmer * 565*23cd1385SRemy Bohmer * returns zero on success, else negative errno. 566*23cd1385SRemy Bohmer */ 567*23cd1385SRemy Bohmer static inline int 568*23cd1385SRemy Bohmer usb_gadget_set_selfpowered (struct usb_gadget *gadget) 569*23cd1385SRemy Bohmer { 570*23cd1385SRemy Bohmer if (!gadget->ops->set_selfpowered) 571*23cd1385SRemy Bohmer return -EOPNOTSUPP; 572*23cd1385SRemy Bohmer return gadget->ops->set_selfpowered (gadget, 1); 573*23cd1385SRemy Bohmer } 574*23cd1385SRemy Bohmer 575*23cd1385SRemy Bohmer /** 576*23cd1385SRemy Bohmer * usb_gadget_clear_selfpowered - clear the device selfpowered feature. 577*23cd1385SRemy Bohmer * @gadget:the device being declared as bus-powered 578*23cd1385SRemy Bohmer * 579*23cd1385SRemy Bohmer * this affects the device status reported by the hardware driver. 580*23cd1385SRemy Bohmer * some hardware may not support bus-powered operation, in which 581*23cd1385SRemy Bohmer * case this feature's value can never change. 582*23cd1385SRemy Bohmer * 583*23cd1385SRemy Bohmer * returns zero on success, else negative errno. 584*23cd1385SRemy Bohmer */ 585*23cd1385SRemy Bohmer static inline int 586*23cd1385SRemy Bohmer usb_gadget_clear_selfpowered (struct usb_gadget *gadget) 587*23cd1385SRemy Bohmer { 588*23cd1385SRemy Bohmer if (!gadget->ops->set_selfpowered) 589*23cd1385SRemy Bohmer return -EOPNOTSUPP; 590*23cd1385SRemy Bohmer return gadget->ops->set_selfpowered (gadget, 0); 591*23cd1385SRemy Bohmer } 592*23cd1385SRemy Bohmer 593*23cd1385SRemy Bohmer /** 594*23cd1385SRemy Bohmer * usb_gadget_vbus_connect - Notify controller that VBUS is powered 595*23cd1385SRemy Bohmer * @gadget:The device which now has VBUS power. 596*23cd1385SRemy Bohmer * 597*23cd1385SRemy Bohmer * This call is used by a driver for an external transceiver (or GPIO) 598*23cd1385SRemy Bohmer * that detects a VBUS power session starting. Common responses include 599*23cd1385SRemy Bohmer * resuming the controller, activating the D+ (or D-) pullup to let the 600*23cd1385SRemy Bohmer * host detect that a USB device is attached, and starting to draw power 601*23cd1385SRemy Bohmer * (8mA or possibly more, especially after SET_CONFIGURATION). 602*23cd1385SRemy Bohmer * 603*23cd1385SRemy Bohmer * Returns zero on success, else negative errno. 604*23cd1385SRemy Bohmer */ 605*23cd1385SRemy Bohmer static inline int 606*23cd1385SRemy Bohmer usb_gadget_vbus_connect(struct usb_gadget *gadget) 607*23cd1385SRemy Bohmer { 608*23cd1385SRemy Bohmer if (!gadget->ops->vbus_session) 609*23cd1385SRemy Bohmer return -EOPNOTSUPP; 610*23cd1385SRemy Bohmer return gadget->ops->vbus_session (gadget, 1); 611*23cd1385SRemy Bohmer } 612*23cd1385SRemy Bohmer 613*23cd1385SRemy Bohmer /** 614*23cd1385SRemy Bohmer * usb_gadget_vbus_draw - constrain controller's VBUS power usage 615*23cd1385SRemy Bohmer * @gadget:The device whose VBUS usage is being described 616*23cd1385SRemy Bohmer * @mA:How much current to draw, in milliAmperes. This should be twice 617*23cd1385SRemy Bohmer * the value listed in the configuration descriptor bMaxPower field. 618*23cd1385SRemy Bohmer * 619*23cd1385SRemy Bohmer * This call is used by gadget drivers during SET_CONFIGURATION calls, 620*23cd1385SRemy Bohmer * reporting how much power the device may consume. For example, this 621*23cd1385SRemy Bohmer * could affect how quickly batteries are recharged. 622*23cd1385SRemy Bohmer * 623*23cd1385SRemy Bohmer * Returns zero on success, else negative errno. 624*23cd1385SRemy Bohmer */ 625*23cd1385SRemy Bohmer static inline int 626*23cd1385SRemy Bohmer usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) 627*23cd1385SRemy Bohmer { 628*23cd1385SRemy Bohmer if (!gadget->ops->vbus_draw) 629*23cd1385SRemy Bohmer return -EOPNOTSUPP; 630*23cd1385SRemy Bohmer return gadget->ops->vbus_draw (gadget, mA); 631*23cd1385SRemy Bohmer } 632*23cd1385SRemy Bohmer 633*23cd1385SRemy Bohmer /** 634*23cd1385SRemy Bohmer * usb_gadget_vbus_disconnect - notify controller about VBUS session end 635*23cd1385SRemy Bohmer * @gadget:the device whose VBUS supply is being described 636*23cd1385SRemy Bohmer * 637*23cd1385SRemy Bohmer * This call is used by a driver for an external transceiver (or GPIO) 638*23cd1385SRemy Bohmer * that detects a VBUS power session ending. Common responses include 639*23cd1385SRemy Bohmer * reversing everything done in usb_gadget_vbus_connect(). 640*23cd1385SRemy Bohmer * 641*23cd1385SRemy Bohmer * Returns zero on success, else negative errno. 642*23cd1385SRemy Bohmer */ 643*23cd1385SRemy Bohmer static inline int 644*23cd1385SRemy Bohmer usb_gadget_vbus_disconnect(struct usb_gadget *gadget) 645*23cd1385SRemy Bohmer { 646*23cd1385SRemy Bohmer if (!gadget->ops->vbus_session) 647*23cd1385SRemy Bohmer return -EOPNOTSUPP; 648*23cd1385SRemy Bohmer return gadget->ops->vbus_session (gadget, 0); 649*23cd1385SRemy Bohmer } 650*23cd1385SRemy Bohmer 651*23cd1385SRemy Bohmer /** 652*23cd1385SRemy Bohmer * usb_gadget_connect - software-controlled connect to USB host 653*23cd1385SRemy Bohmer * @gadget:the peripheral being connected 654*23cd1385SRemy Bohmer * 655*23cd1385SRemy Bohmer * Enables the D+ (or potentially D-) pullup. The host will start 656*23cd1385SRemy Bohmer * enumerating this gadget when the pullup is active and a VBUS session 657*23cd1385SRemy Bohmer * is active (the link is powered). This pullup is always enabled unless 658*23cd1385SRemy Bohmer * usb_gadget_disconnect() has been used to disable it. 659*23cd1385SRemy Bohmer * 660*23cd1385SRemy Bohmer * Returns zero on success, else negative errno. 661*23cd1385SRemy Bohmer */ 662*23cd1385SRemy Bohmer static inline int 663*23cd1385SRemy Bohmer usb_gadget_connect (struct usb_gadget *gadget) 664*23cd1385SRemy Bohmer { 665*23cd1385SRemy Bohmer if (!gadget->ops->pullup) 666*23cd1385SRemy Bohmer return -EOPNOTSUPP; 667*23cd1385SRemy Bohmer return gadget->ops->pullup (gadget, 1); 668*23cd1385SRemy Bohmer } 669*23cd1385SRemy Bohmer 670*23cd1385SRemy Bohmer /** 671*23cd1385SRemy Bohmer * usb_gadget_disconnect - software-controlled disconnect from USB host 672*23cd1385SRemy Bohmer * @gadget:the peripheral being disconnected 673*23cd1385SRemy Bohmer * 674*23cd1385SRemy Bohmer * Disables the D+ (or potentially D-) pullup, which the host may see 675*23cd1385SRemy Bohmer * as a disconnect (when a VBUS session is active). Not all systems 676*23cd1385SRemy Bohmer * support software pullup controls. 677*23cd1385SRemy Bohmer * 678*23cd1385SRemy Bohmer * This routine may be used during the gadget driver bind() call to prevent 679*23cd1385SRemy Bohmer * the peripheral from ever being visible to the USB host, unless later 680*23cd1385SRemy Bohmer * usb_gadget_connect() is called. For example, user mode components may 681*23cd1385SRemy Bohmer * need to be activated before the system can talk to hosts. 682*23cd1385SRemy Bohmer * 683*23cd1385SRemy Bohmer * Returns zero on success, else negative errno. 684*23cd1385SRemy Bohmer */ 685*23cd1385SRemy Bohmer static inline int 686*23cd1385SRemy Bohmer usb_gadget_disconnect (struct usb_gadget *gadget) 687*23cd1385SRemy Bohmer { 688*23cd1385SRemy Bohmer if (!gadget->ops->pullup) 689*23cd1385SRemy Bohmer return -EOPNOTSUPP; 690*23cd1385SRemy Bohmer return gadget->ops->pullup (gadget, 0); 691*23cd1385SRemy Bohmer } 692*23cd1385SRemy Bohmer 693*23cd1385SRemy Bohmer 694*23cd1385SRemy Bohmer 695*23cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 696*23cd1385SRemy Bohmer 697*23cd1385SRemy Bohmer /** 698*23cd1385SRemy Bohmer * struct usb_gadget_driver - driver for usb 'slave' devices 699*23cd1385SRemy Bohmer * @speed: Highest speed the driver handles. 700*23cd1385SRemy Bohmer * @bind: Invoked when the driver is bound to a gadget, usually 701*23cd1385SRemy Bohmer * after registering the driver. 702*23cd1385SRemy Bohmer * At that point, ep0 is fully initialized, and ep_list holds 703*23cd1385SRemy Bohmer * the currently-available endpoints. 704*23cd1385SRemy Bohmer * Called in a context that permits sleeping. 705*23cd1385SRemy Bohmer * @setup: Invoked for ep0 control requests that aren't handled by 706*23cd1385SRemy Bohmer * the hardware level driver. Most calls must be handled by 707*23cd1385SRemy Bohmer * the gadget driver, including descriptor and configuration 708*23cd1385SRemy Bohmer * management. The 16 bit members of the setup data are in 709*23cd1385SRemy Bohmer * USB byte order. Called in_interrupt; this may not sleep. Driver 710*23cd1385SRemy Bohmer * queues a response to ep0, or returns negative to stall. 711*23cd1385SRemy Bohmer * @disconnect: Invoked after all transfers have been stopped, 712*23cd1385SRemy Bohmer * when the host is disconnected. May be called in_interrupt; this 713*23cd1385SRemy Bohmer * may not sleep. Some devices can't detect disconnect, so this might 714*23cd1385SRemy Bohmer * not be called except as part of controller shutdown. 715*23cd1385SRemy Bohmer * @unbind: Invoked when the driver is unbound from a gadget, 716*23cd1385SRemy Bohmer * usually from rmmod (after a disconnect is reported). 717*23cd1385SRemy Bohmer * Called in a context that permits sleeping. 718*23cd1385SRemy Bohmer * @suspend: Invoked on USB suspend. May be called in_interrupt. 719*23cd1385SRemy Bohmer * @resume: Invoked on USB resume. May be called in_interrupt. 720*23cd1385SRemy Bohmer * 721*23cd1385SRemy Bohmer * Devices are disabled till a gadget driver successfully bind()s, which 722*23cd1385SRemy Bohmer * means the driver will handle setup() requests needed to enumerate (and 723*23cd1385SRemy Bohmer * meet "chapter 9" requirements) then do some useful work. 724*23cd1385SRemy Bohmer * 725*23cd1385SRemy Bohmer * If gadget->is_otg is true, the gadget driver must provide an OTG 726*23cd1385SRemy Bohmer * descriptor during enumeration, or else fail the bind() call. In such 727*23cd1385SRemy Bohmer * cases, no USB traffic may flow until both bind() returns without 728*23cd1385SRemy Bohmer * having called usb_gadget_disconnect(), and the USB host stack has 729*23cd1385SRemy Bohmer * initialized. 730*23cd1385SRemy Bohmer * 731*23cd1385SRemy Bohmer * Drivers use hardware-specific knowledge to configure the usb hardware. 732*23cd1385SRemy Bohmer * endpoint addressing is only one of several hardware characteristics that 733*23cd1385SRemy Bohmer * are in descriptors the ep0 implementation returns from setup() calls. 734*23cd1385SRemy Bohmer * 735*23cd1385SRemy Bohmer * Except for ep0 implementation, most driver code shouldn't need change to 736*23cd1385SRemy Bohmer * run on top of different usb controllers. It'll use endpoints set up by 737*23cd1385SRemy Bohmer * that ep0 implementation. 738*23cd1385SRemy Bohmer * 739*23cd1385SRemy Bohmer * The usb controller driver handles a few standard usb requests. Those 740*23cd1385SRemy Bohmer * include set_address, and feature flags for devices, interfaces, and 741*23cd1385SRemy Bohmer * endpoints (the get_status, set_feature, and clear_feature requests). 742*23cd1385SRemy Bohmer * 743*23cd1385SRemy Bohmer * Accordingly, the driver's setup() callback must always implement all 744*23cd1385SRemy Bohmer * get_descriptor requests, returning at least a device descriptor and 745*23cd1385SRemy Bohmer * a configuration descriptor. Drivers must make sure the endpoint 746*23cd1385SRemy Bohmer * descriptors match any hardware constraints. Some hardware also constrains 747*23cd1385SRemy Bohmer * other descriptors. (The pxa250 allows only configurations 1, 2, or 3). 748*23cd1385SRemy Bohmer * 749*23cd1385SRemy Bohmer * The driver's setup() callback must also implement set_configuration, 750*23cd1385SRemy Bohmer * and should also implement set_interface, get_configuration, and 751*23cd1385SRemy Bohmer * get_interface. Setting a configuration (or interface) is where 752*23cd1385SRemy Bohmer * endpoints should be activated or (config 0) shut down. 753*23cd1385SRemy Bohmer * 754*23cd1385SRemy Bohmer * (Note that only the default control endpoint is supported. Neither 755*23cd1385SRemy Bohmer * hosts nor devices generally support control traffic except to ep0.) 756*23cd1385SRemy Bohmer * 757*23cd1385SRemy Bohmer * Most devices will ignore USB suspend/resume operations, and so will 758*23cd1385SRemy Bohmer * not provide those callbacks. However, some may need to change modes 759*23cd1385SRemy Bohmer * when the host is not longer directing those activities. For example, 760*23cd1385SRemy Bohmer * local controls (buttons, dials, etc) may need to be re-enabled since 761*23cd1385SRemy Bohmer * the (remote) host can't do that any longer; or an error state might 762*23cd1385SRemy Bohmer * be cleared, to make the device behave identically whether or not 763*23cd1385SRemy Bohmer * power is maintained. 764*23cd1385SRemy Bohmer */ 765*23cd1385SRemy Bohmer struct usb_gadget_driver { 766*23cd1385SRemy Bohmer enum usb_device_speed speed; 767*23cd1385SRemy Bohmer int (*bind)(struct usb_gadget *); 768*23cd1385SRemy Bohmer void (*unbind)(struct usb_gadget *); 769*23cd1385SRemy Bohmer int (*setup)(struct usb_gadget *, 770*23cd1385SRemy Bohmer const struct usb_ctrlrequest *); 771*23cd1385SRemy Bohmer void (*disconnect)(struct usb_gadget *); 772*23cd1385SRemy Bohmer void (*suspend)(struct usb_gadget *); 773*23cd1385SRemy Bohmer void (*resume)(struct usb_gadget *); 774*23cd1385SRemy Bohmer }; 775*23cd1385SRemy Bohmer 776*23cd1385SRemy Bohmer 777*23cd1385SRemy Bohmer 778*23cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 779*23cd1385SRemy Bohmer 780*23cd1385SRemy Bohmer /* driver modules register and unregister, as usual. 781*23cd1385SRemy Bohmer * these calls must be made in a context that can sleep. 782*23cd1385SRemy Bohmer * 783*23cd1385SRemy Bohmer * these will usually be implemented directly by the hardware-dependent 784*23cd1385SRemy Bohmer * usb bus interface driver, which will only support a single driver. 785*23cd1385SRemy Bohmer */ 786*23cd1385SRemy Bohmer 787*23cd1385SRemy Bohmer /** 788*23cd1385SRemy Bohmer * usb_gadget_register_driver - register a gadget driver 789*23cd1385SRemy Bohmer * @driver:the driver being registered 790*23cd1385SRemy Bohmer * 791*23cd1385SRemy Bohmer * Call this in your gadget driver's module initialization function, 792*23cd1385SRemy Bohmer * to tell the underlying usb controller driver about your driver. 793*23cd1385SRemy Bohmer * The driver's bind() function will be called to bind it to a 794*23cd1385SRemy Bohmer * gadget before this registration call returns. It's expected that 795*23cd1385SRemy Bohmer * the bind() functions will be in init sections. 796*23cd1385SRemy Bohmer * This function must be called in a context that can sleep. 797*23cd1385SRemy Bohmer */ 798*23cd1385SRemy Bohmer int usb_gadget_register_driver (struct usb_gadget_driver *driver); 799*23cd1385SRemy Bohmer 800*23cd1385SRemy Bohmer /** 801*23cd1385SRemy Bohmer * usb_gadget_unregister_driver - unregister a gadget driver 802*23cd1385SRemy Bohmer * @driver:the driver being unregistered 803*23cd1385SRemy Bohmer * 804*23cd1385SRemy Bohmer * Call this in your gadget driver's module cleanup function, 805*23cd1385SRemy Bohmer * to tell the underlying usb controller that your driver is 806*23cd1385SRemy Bohmer * going away. If the controller is connected to a USB host, 807*23cd1385SRemy Bohmer * it will first disconnect(). The driver is also requested 808*23cd1385SRemy Bohmer * to unbind() and clean up any device state, before this procedure 809*23cd1385SRemy Bohmer * finally returns. It's expected that the unbind() functions 810*23cd1385SRemy Bohmer * will in in exit sections, so may not be linked in some kernels. 811*23cd1385SRemy Bohmer * This function must be called in a context that can sleep. 812*23cd1385SRemy Bohmer */ 813*23cd1385SRemy Bohmer int usb_gadget_unregister_driver (struct usb_gadget_driver *driver); 814*23cd1385SRemy Bohmer 815*23cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 816*23cd1385SRemy Bohmer 817*23cd1385SRemy Bohmer /* utility to simplify dealing with string descriptors */ 818*23cd1385SRemy Bohmer 819*23cd1385SRemy Bohmer /** 820*23cd1385SRemy Bohmer * struct usb_string - wraps a C string and its USB id 821*23cd1385SRemy Bohmer * @id:the (nonzero) ID for this string 822*23cd1385SRemy Bohmer * @s:the string, in UTF-8 encoding 823*23cd1385SRemy Bohmer * 824*23cd1385SRemy Bohmer * If you're using usb_gadget_get_string(), use this to wrap a string 825*23cd1385SRemy Bohmer * together with its ID. 826*23cd1385SRemy Bohmer */ 827*23cd1385SRemy Bohmer struct usb_string { 828*23cd1385SRemy Bohmer u8 id; 829*23cd1385SRemy Bohmer const char *s; 830*23cd1385SRemy Bohmer }; 831*23cd1385SRemy Bohmer 832*23cd1385SRemy Bohmer /** 833*23cd1385SRemy Bohmer * struct usb_gadget_strings - a set of USB strings in a given language 834*23cd1385SRemy Bohmer * @language:identifies the strings' language (0x0409 for en-us) 835*23cd1385SRemy Bohmer * @strings:array of strings with their ids 836*23cd1385SRemy Bohmer * 837*23cd1385SRemy Bohmer * If you're using usb_gadget_get_string(), use this to wrap all the 838*23cd1385SRemy Bohmer * strings for a given language. 839*23cd1385SRemy Bohmer */ 840*23cd1385SRemy Bohmer struct usb_gadget_strings { 841*23cd1385SRemy Bohmer u16 language; /* 0x0409 for en-us */ 842*23cd1385SRemy Bohmer struct usb_string *strings; 843*23cd1385SRemy Bohmer }; 844*23cd1385SRemy Bohmer 845*23cd1385SRemy Bohmer /* put descriptor for string with that id into buf (buflen >= 256) */ 846*23cd1385SRemy Bohmer int usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf); 847*23cd1385SRemy Bohmer 848*23cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 849*23cd1385SRemy Bohmer 850*23cd1385SRemy Bohmer /* utility to simplify managing config descriptors */ 851*23cd1385SRemy Bohmer 852*23cd1385SRemy Bohmer /* write vector of descriptors into buffer */ 853*23cd1385SRemy Bohmer int usb_descriptor_fillbuf(void *, unsigned, 854*23cd1385SRemy Bohmer const struct usb_descriptor_header **); 855*23cd1385SRemy Bohmer 856*23cd1385SRemy Bohmer /* build config descriptor from single descriptor vector */ 857*23cd1385SRemy Bohmer int usb_gadget_config_buf(const struct usb_config_descriptor *config, 858*23cd1385SRemy Bohmer void *buf, unsigned buflen, const struct usb_descriptor_header **desc); 859*23cd1385SRemy Bohmer 860*23cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/ 861*23cd1385SRemy Bohmer 862*23cd1385SRemy Bohmer /* utility wrapping a simple endpoint selection policy */ 863*23cd1385SRemy Bohmer 864*23cd1385SRemy Bohmer extern struct usb_ep *usb_ep_autoconfig (struct usb_gadget *, 865*23cd1385SRemy Bohmer struct usb_endpoint_descriptor *); 866*23cd1385SRemy Bohmer 867*23cd1385SRemy Bohmer extern void usb_ep_autoconfig_reset (struct usb_gadget *); 868*23cd1385SRemy Bohmer 869*23cd1385SRemy Bohmer extern int usb_gadget_handle_interrupts(void); 870*23cd1385SRemy Bohmer 871*23cd1385SRemy Bohmer #endif /* __LINUX_USB_GADGET_H */ 872