Home | History | Annotate | Line # | Download | only in usb
usbdivar.h revision 1.109.2.11
      1 /*	$NetBSD: usbdivar.h,v 1.109.2.11 2015/02/22 14:50:07 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Discussion about locking in the USB code:
     35  *
     36  * The host controller presents one lock at IPL_SOFTUSB (aka IPL_SOFTNET).
     37  *
     38  * List of hardware interface methods, and whether the lock is held
     39  * when each is called by this module:
     40  *
     41  *	BUS METHOD		LOCK	NOTES
     42  *	----------------------- -------	-------------------------
     43  *	ubm_open		-	might want to take lock?
     44  *	ubm_softint		x
     45  *	ubm_dopoll		-	might want to take lock?
     46  *	ubm_allocx		-
     47  *	ubm_freex		-
     48  *	ubm_getlock 		-	Called at attach time
     49  *	ubm_newdev		-	Will take lock
     50 	ubm_rhctrl
     51  *
     52  *	PIPE METHOD		LOCK	NOTES
     53  *	----------------------- -------	-------------------------
     54  *	upm_transfer		-
     55  *	upm_start		-	might want to take lock?
     56  *	upm_abort		x
     57  *	upm_close		x
     58  *	upm_cleartoggle		-
     59  *	upm_done		x
     60  *
     61  * The above semantics are likely to change.  Little performance
     62  * evaluation has been done on this code and the locking strategy.
     63  *
     64  * USB functions known to expect the lock taken include (this list is
     65  * probably not exhaustive):
     66  *    usb_transfer_complete()
     67  *    usb_insert_transfer()
     68  *    usb_start_next()
     69  *
     70  */
     71 
     72 #include <sys/callout.h>
     73 #include <sys/mutex.h>
     74 #include <sys/bus.h>
     75 
     76 /* From usb_mem.h */
     77 struct usb_dma_block;
     78 typedef struct {
     79 	struct usb_dma_block *udma_block;
     80 	u_int udma_offs;
     81 } usb_dma_t;
     82 
     83 struct usbd_xfer;
     84 struct usbd_pipe;
     85 struct usbd_port;
     86 
     87 struct usbd_endpoint {
     88 	usb_endpoint_descriptor_t *ue_edesc;
     89 	int			ue_refcnt;
     90 	int			ue_toggle;
     91 };
     92 
     93 struct usbd_bus_methods {
     94 	usbd_status	      (*ubm_open)(struct usbd_pipe *);
     95 	void		      (*ubm_softint)(void *);
     96 	void		      (*ubm_dopoll)(struct usbd_bus *);
     97 	struct usbd_xfer *    (*ubm_allocx)(struct usbd_bus *);
     98 	void		      (*ubm_freex)(struct usbd_bus *, struct usbd_xfer *);
     99 	void		      (*ubm_getlock)(struct usbd_bus *, kmutex_t **);
    100 	usbd_status	      (*ubm_newdev)(device_t, usbd_bus_handle, int,
    101 					    int, int, struct usbd_port *);
    102 
    103 	int			(*ubm_rhctrl)(struct usbd_bus *,
    104 				    usb_device_request_t *, void *, int);
    105 };
    106 
    107 struct usbd_pipe_methods {
    108 	usbd_status	      (*upm_transfer)(usbd_xfer_handle);
    109 	usbd_status	      (*upm_start)(usbd_xfer_handle);
    110 	void		      (*upm_abort)(usbd_xfer_handle);
    111 	void		      (*upm_close)(usbd_pipe_handle);
    112 	void		      (*upm_cleartoggle)(usbd_pipe_handle);
    113 	void		      (*upm_done)(usbd_xfer_handle);
    114 };
    115 
    116 #if 0 /* notyet */
    117 struct usbd_tt {
    118 	struct usbd_hub	       *hub;
    119 };
    120 #endif
    121 
    122 struct usbd_port {
    123 	usb_port_status_t	up_status;
    124 	uint16_t		up_power;	/* mA of current on port */
    125 	uint8_t			up_portno;
    126 	uint8_t			up_restartcnt;
    127 #define USBD_RESTART_MAX 5
    128 	uint8_t			up_reattach;
    129 	struct usbd_device     *up_dev;		/* Connected device */
    130 	struct usbd_device     *up_parent;	/* The ports hub */
    131 #if 0
    132 	struct usbd_tt	       *up_tt;	/* Transaction translator (if any) */
    133 #endif
    134 };
    135 
    136 struct usbd_hub {
    137 	usbd_status	      (*uh_explore)(usbd_device_handle hub);
    138 	void		       *uh_hubsoftc;
    139 	usb_hub_descriptor_t	uh_hubdesc;
    140 	struct usbd_port        uh_ports[1];
    141 };
    142 
    143 /*****/
    144 
    145 struct usbd_bus {
    146 	/* Filled by HC driver */
    147 	void			*ub_hcpriv;
    148 	int			ub_revision;	/* USB revision */
    149 #define USBREV_UNKNOWN	0
    150 #define USBREV_PRE_1_0	1
    151 #define USBREV_1_0	2
    152 #define USBREV_1_1	3
    153 #define USBREV_2_0	4
    154 #define USBREV_3_0	5
    155 #define USBREV_STR { "unknown", "pre 1.0", "1.0", "1.1", "2.0", "3.0" }
    156 
    157 	const struct usbd_bus_methods *ub_methods;
    158 	uint32_t		ub_pipesize;	/* size of a pipe struct */
    159 	bool			ub_usedma;	/* Does this HC support DMA */
    160 	int			ub_dmaflags;
    161 	bus_dma_tag_t		ub_dmatag;	/* DMA tag */
    162 
    163 	/* Filled by usb driver */
    164 	kmutex_t		*ub_lock;
    165 	struct usbd_device      *ub_roothub;
    166 	uint8_t			ub_rhaddr;	/* roothub address */
    167 	uint8_t			ub_rhconf;	/* roothub configuration */
    168 	usbd_device_handle	ub_devices[USB_MAX_DEVICES];
    169 	kcondvar_t              ub_needsexplore_cv;
    170 	char			ub_needsexplore;/* a hub a signalled a change */
    171 	char			ub_usepolling;
    172 	device_t		ub_usbctl;
    173 	struct usb_device_stats	ub_stats;
    174 
    175 	void		       *ub_soft; /* soft interrupt cookie */
    176 };
    177 
    178 struct usbd_device {
    179 	struct usbd_bus	       *ud_bus;		/* our controller */
    180 	struct usbd_pipe       *ud_pipe0;	/* pipe 0 */
    181 	uint8_t			ud_addr;	/* device addess */
    182 	uint8_t			ud_config;	/* current configuration # */
    183 	uint8_t			ud_depth;	/* distance from root hub */
    184 	uint8_t			ud_speed;	/* low/full/high speed */
    185 	uint8_t			ud_selfpowered;	/* flag for self powered */
    186 	uint16_t		ud_power;	/* mA the device uses */
    187 	int16_t			ud_langid;	/* language for strings */
    188 #define USBD_NOLANG (-1)
    189 	usb_event_cookie_t	ud_cookie;	/* unique connection id */
    190 	struct usbd_port       *ud_powersrc;	/* upstream hub port, or 0 */
    191 	struct usbd_device     *ud_myhub;	/* upstream hub */
    192 	struct usbd_port       *ud_myhsport;	/* closest high speed port */
    193 	struct usbd_endpoint	ud_ep0;		/* for pipe 0 */
    194 	usb_endpoint_descriptor_t ud_ep0desc;	/* for pipe 0 */
    195 	struct usbd_interface  *ud_ifaces;	/* array of all interfaces */
    196 	usb_device_descriptor_t ud_ddesc;	/* device descriptor */
    197 	usb_config_descriptor_t *ud_cdesc;	/* full config descr */
    198 	const struct usbd_quirks     *ud_quirks;/* device quirks, always set */
    199 	struct usbd_hub	       *ud_hub;		/* only if this is a hub */
    200 	int			ud_subdevlen;	/* array length of following */
    201 	device_t	       *ud_subdevs;	/* sub-devices */
    202 	int			ud_nifaces_claimed; /* number of ifaces in use */
    203 	void		       *ud_hcpriv;
    204 };
    205 
    206 struct usbd_interface {
    207 	struct usbd_device     *ui_dev;
    208 	usb_interface_descriptor_t *ui_idesc;
    209 	int			ui_index;
    210 	int			ui_altindex;
    211 	struct usbd_endpoint   *ui_endpoints;
    212 	void		       *ui_priv;
    213 	LIST_HEAD(, usbd_pipe)	ui_pipes;
    214 };
    215 
    216 struct usbd_pipe {
    217 	struct usbd_interface  *up_iface;
    218 	struct usbd_device     *up_dev;
    219 	struct usbd_endpoint   *up_endpoint;
    220 	int			up_refcnt;
    221 	char			up_running;
    222 	char			up_aborting;
    223 	SIMPLEQ_HEAD(, usbd_xfer) up_queue;
    224 	LIST_ENTRY(usbd_pipe)	up_next;
    225 	struct usb_task		up_async_task;
    226 
    227 	usbd_xfer_handle	up_intrxfer; /* used for repeating requests */
    228 	char			up_repeat;
    229 	int			up_interval;
    230 	uint8_t			up_flags;
    231 
    232 	/* Filled by HC driver. */
    233 	const struct usbd_pipe_methods *up_methods;
    234 };
    235 
    236 struct usbd_xfer {
    237 	struct usbd_pipe       *ux_pipe;
    238 	void		       *ux_priv;
    239 	void		       *ux_buffer;
    240 	kcondvar_t		ux_cv;
    241 	uint32_t		ux_length;
    242 	uint32_t		ux_actlen;
    243 	uint16_t		ux_flags;
    244 	uint32_t		ux_timeout;
    245 	usbd_status		ux_status;
    246 	usbd_callback		ux_callback;
    247 	volatile uint8_t	ux_done;
    248 	uint8_t			ux_state;	/* used for DIAGNOSTIC */
    249 #define XFER_FREE 0x46
    250 #define XFER_BUSY 0x55
    251 #define XFER_ONQU 0x9e
    252 
    253 	/* For control pipe */
    254 	usb_device_request_t	ux_request;
    255 
    256 	/* For isoc */
    257 	uint16_t		*ux_frlengths;
    258 	int			ux_nframes;
    259 
    260 	/* For memory allocation */
    261 	struct usbd_device     *ux_dev;
    262 	usb_dma_t		ux_dmabuf;
    263 	void		       *ux_buf;
    264 	uint32_t		ux_bufsize;
    265 
    266 	uint8_t			ux_rqflags;
    267 #define URQ_REQUEST	0x01
    268 #define URQ_AUTO_BUFFER	0x10
    269 
    270 	SIMPLEQ_ENTRY(usbd_xfer) ux_next;
    271 
    272 	void		       *ux_hcpriv;	/* private use by the HC driver */
    273 	uint8_t			ux_hcflags;	/* private use by the HC driver */
    274 #define UXFER_ABORTING	0x01	/* xfer is aborting. */
    275 #define UXFER_ABORTWAIT	0x02	/* abort completion is being awaited. */
    276 	kcondvar_t		ux_hccv;	/* private use by the HC driver */
    277 
    278 	struct callout		ux_callout;
    279 };
    280 
    281 void usbd_init(void);
    282 void usbd_finish(void);
    283 
    284 #if defined(USB_DEBUG)
    285 void usbd_dump_iface(struct usbd_interface *);
    286 void usbd_dump_device(struct usbd_device *);
    287 void usbd_dump_endpoint(struct usbd_endpoint *);
    288 void usbd_dump_queue(usbd_pipe_handle);
    289 void usbd_dump_pipe(usbd_pipe_handle);
    290 #endif
    291 
    292 /* Routines from usb_subr.c */
    293 int		usbctlprint(void *, const char *);
    294 void		usb_delay_ms_locked(usbd_bus_handle, u_int, kmutex_t *);
    295 void		usb_delay_ms(usbd_bus_handle, u_int);
    296 void		usbd_delay_ms_locked(usbd_device_handle, u_int, kmutex_t *);
    297 void		usbd_delay_ms(usbd_device_handle, u_int);
    298 usbd_status	usbd_reset_port(usbd_device_handle, int, usb_port_status_t *);
    299 usbd_status	usbd_setup_pipe(usbd_device_handle,
    300 				usbd_interface_handle,
    301 				struct usbd_endpoint *, int,
    302 				usbd_pipe_handle *);
    303 usbd_status	usbd_setup_pipe_flags(usbd_device_handle,
    304 				      usbd_interface_handle,
    305 				      struct usbd_endpoint *, int,
    306 				      usbd_pipe_handle *,
    307 				      uint8_t);
    308 usbd_status	usbd_new_device(device_t, usbd_bus_handle, int, int, int,
    309 				struct usbd_port *);
    310 usbd_status	usbd_reattach_device(device_t, usbd_device_handle,
    311 				     int, const int *);
    312 
    313 void		usbd_remove_device(usbd_device_handle, struct usbd_port *);
    314 int		usbd_printBCD(char *, size_t, int);
    315 usbd_status	usbd_fill_iface_data(usbd_device_handle, int, int);
    316 void		usb_free_device(usbd_device_handle);
    317 
    318 usbd_status	usb_insert_transfer(usbd_xfer_handle);
    319 void		usb_transfer_complete(usbd_xfer_handle);
    320 int		usb_disconnect_port(struct usbd_port *, device_t, int);
    321 
    322 void		usbd_kill_pipe(usbd_pipe_handle);
    323 usbd_status	usbd_attach_roothub(device_t, usbd_device_handle);
    324 usbd_status	usbd_probe_and_attach(device_t, usbd_device_handle, int, int);
    325 usbd_status	usbd_get_initial_ddesc(usbd_device_handle,
    326 				       usb_device_descriptor_t *);
    327 
    328 /* Routines from usb.c */
    329 void		usb_needs_explore(usbd_device_handle);
    330 void		usb_needs_reattach(usbd_device_handle);
    331 void		usb_schedsoftintr(struct usbd_bus *);
    332 
    333 /*
    334  * These macros reflect the current locking scheme.  They might change.
    335  */
    336 
    337 #define usbd_lock_pipe(p)	mutex_enter((p)->up_dev->ud_bus->ub_lock)
    338 #define usbd_unlock_pipe(p)	mutex_exit((p)->up_dev->ud_bus->ub_lock)
    339