Home | History | Annotate | Line # | Download | only in usb
usbdivar.h revision 1.128
      1 /*	$NetBSD: usbdivar.h,v 1.128 2021/06/13 00:13:24 riastradh 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 *, unsigned int);
     98 	void		      (*ubm_freex)(struct usbd_bus *, struct usbd_xfer *);
     99 	void		      (*ubm_abortx)(struct usbd_xfer *);
    100 	bool		      (*ubm_dying)(struct usbd_bus *);
    101 	void		      (*ubm_getlock)(struct usbd_bus *, kmutex_t **);
    102 	usbd_status	      (*ubm_newdev)(device_t, struct usbd_bus *, int,
    103 					    int, int, struct usbd_port *);
    104 
    105 	int			(*ubm_rhctrl)(struct usbd_bus *,
    106 				    usb_device_request_t *, void *, int);
    107 };
    108 
    109 struct usbd_pipe_methods {
    110 	int		      (*upm_init)(struct usbd_xfer *);
    111 	void		      (*upm_fini)(struct usbd_xfer *);
    112 	usbd_status	      (*upm_transfer)(struct usbd_xfer *);
    113 	usbd_status	      (*upm_start)(struct usbd_xfer *);
    114 	void		      (*upm_abort)(struct usbd_xfer *);
    115 	void		      (*upm_close)(struct usbd_pipe *);
    116 	void		      (*upm_cleartoggle)(struct usbd_pipe *);
    117 	void		      (*upm_done)(struct usbd_xfer *);
    118 };
    119 
    120 struct usbd_tt {
    121 	struct usbd_hub	       *utt_hub;
    122 };
    123 
    124 struct usbd_port {
    125 	usb_port_status_t	up_status;
    126 	uint16_t		up_power;	/* mA of current on port */
    127 	uint8_t			up_portno;
    128 	uint8_t			up_restartcnt;
    129 #define USBD_RESTART_MAX 5
    130 	uint8_t			up_reattach;
    131 	struct usbd_device     *up_dev;		/* Connected device */
    132 	struct usbd_device     *up_parent;	/* The ports hub */
    133 	struct usbd_tt	       *up_tt;	/* Transaction translator (if any) */
    134 };
    135 
    136 struct usbd_hub {
    137 	usbd_status	      (*uh_explore)(struct usbd_device *hub);
    138 	void		       *uh_hubsoftc;
    139 	usb_hub_descriptor_t	uh_hubdesc;
    140 	struct usbd_port        uh_ports[1];
    141 };
    142 
    143 /*****/
    144 /* 0, root, and 1->127 */
    145 #define USB_ROOTHUB_INDEX	1
    146 #define USB_TOTAL_DEVICES	(USB_MAX_DEVICES + 1)
    147 
    148 struct usbd_bus {
    149 	/* Filled by HC driver */
    150 	void			*ub_hcpriv;
    151 	int			ub_revision;	/* USB revision */
    152 #define USBREV_UNKNOWN	0
    153 #define USBREV_PRE_1_0	1
    154 #define USBREV_1_0	2
    155 #define USBREV_1_1	3
    156 #define USBREV_2_0	4
    157 #define USBREV_3_0	5
    158 #define USBREV_3_1	6
    159 #define USBREV_STR { "unknown", "pre 1.0", "1.0", "1.1", "2.0", "3.0", "3.1" }
    160 	int			ub_hctype;
    161 #define USBHCTYPE_UNKNOWN	0
    162 #define USBHCTYPE_MOTG		1
    163 #define USBHCTYPE_OHCI		2
    164 #define USBHCTYPE_UHCI		3
    165 #define USBHCTYPE_EHCI		4
    166 #define USBHCTYPE_XHCI		5
    167 #define USBHCTYPE_VHCI		6
    168 	int			ub_busnum;
    169 	const struct usbd_bus_methods
    170 			       *ub_methods;
    171 	uint32_t		ub_pipesize;	/* size of a pipe struct */
    172 	bool			ub_usedma;	/* Does this HC support DMA */
    173 	int			ub_dmaflags;
    174 	bus_dma_tag_t		ub_dmatag;	/* DMA tag */
    175 
    176 	/* Filled by usb driver */
    177 	kmutex_t	       *ub_lock;
    178 	struct usbd_device     *ub_roothub;
    179 	uint8_t			ub_rhaddr;	/* roothub address */
    180 	uint8_t			ub_rhconf;	/* roothub configuration */
    181 	struct usbd_device     *ub_devices[USB_TOTAL_DEVICES];
    182 	kcondvar_t              ub_needsexplore_cv;
    183 	char			ub_needsexplore;/* a hub a signalled a change */
    184 	char			ub_usepolling;
    185 	device_t		ub_usbctl;
    186 	struct usb_device_stats	ub_stats;
    187 
    188 	void		       *ub_soft; /* soft interrupt cookie */
    189 };
    190 
    191 struct usbd_device {
    192 	struct usbd_bus	       *ud_bus;		/* our controller */
    193 	struct usbd_pipe       *ud_pipe0;	/* pipe 0 */
    194 	uint8_t			ud_addr;	/* device addess */
    195 	uint8_t			ud_config;	/* current configuration # */
    196 	uint8_t			ud_depth;	/* distance from root hub */
    197 	uint8_t			ud_speed;	/* low/full/high speed */
    198 	uint8_t			ud_selfpowered;	/* flag for self powered */
    199 	uint16_t		ud_power;	/* mA the device uses */
    200 	int16_t			ud_langid;	/* language for strings */
    201 #define USBD_NOLANG (-1)
    202 	usb_event_cookie_t	ud_cookie;	/* unique connection id */
    203 	struct usbd_port       *ud_powersrc;	/* upstream hub port, or 0 */
    204 	struct usbd_device     *ud_myhub;	/* upstream hub */
    205 	struct usbd_port       *ud_myhsport;	/* closest high speed port */
    206 	struct usbd_endpoint	ud_ep0;		/* for pipe 0 */
    207 	usb_endpoint_descriptor_t
    208 				ud_ep0desc;	/* for pipe 0 */
    209 	struct usbd_interface  *ud_ifaces;	/* array of all interfaces */
    210 	usb_device_descriptor_t ud_ddesc;	/* device descriptor */
    211 	usb_config_descriptor_t *ud_cdesc;	/* full config descr */
    212 	usb_bos_descriptor_t	*ud_bdesc;	/* full BOS descr */
    213 	const struct usbd_quirks
    214 			       *ud_quirks;	/* device quirks, always set */
    215 	struct usbd_hub	       *ud_hub;		/* only if this is a hub */
    216 	u_int			ud_subdevlen;	/* array length of following */
    217 	device_t	       *ud_subdevs;	/* sub-devices */
    218 	int			ud_nifaces_claimed; /* number of ifaces in use */
    219 	void		       *ud_hcpriv;
    220 
    221 	char		       *ud_serial;	/* serial number, can be NULL */
    222 	char		       *ud_vendor;	/* vendor string, can be NULL */
    223 	char		       *ud_product;	/* product string can be NULL */
    224 };
    225 
    226 struct usbd_interface {
    227 	struct usbd_device     *ui_dev;
    228 	usb_interface_descriptor_t
    229 			       *ui_idesc;
    230 	int			ui_index;
    231 	int			ui_altindex;
    232 	struct usbd_endpoint   *ui_endpoints;
    233 	int64_t			ui_busy;	/* #pipes, or -1 if setting */
    234 };
    235 
    236 struct usbd_pipe {
    237 	struct usbd_interface  *up_iface;
    238 	struct usbd_device     *up_dev;
    239 	struct usbd_endpoint   *up_endpoint;
    240 	char			up_running;
    241 	char			up_aborting;
    242 	bool			up_serialise;
    243 	SIMPLEQ_HEAD(, usbd_xfer)
    244 			        up_queue;
    245 	struct usb_task		up_async_task;
    246 
    247 	struct usbd_xfer       *up_intrxfer; /* used for repeating requests */
    248 	char			up_repeat;
    249 	int			up_interval;
    250 	uint8_t			up_flags;
    251 
    252 	/* Filled by HC driver. */
    253 	const struct usbd_pipe_methods
    254 			       *up_methods;
    255 };
    256 
    257 struct usbd_xfer {
    258 	struct usbd_pipe       *ux_pipe;
    259 	void		       *ux_priv;
    260 	void		       *ux_buffer;
    261 	kcondvar_t		ux_cv;
    262 	uint32_t		ux_length;
    263 	uint32_t		ux_actlen;
    264 	uint16_t		ux_flags;
    265 	uint32_t		ux_timeout;
    266 	usbd_status		ux_status;
    267 	usbd_callback		ux_callback;
    268 	volatile uint8_t	ux_done;
    269 	uint8_t			ux_state;	/* used for DIAGNOSTIC */
    270 #define XFER_FREE 0x46
    271 #define XFER_BUSY 0x55
    272 #define XFER_ONQU 0x9e
    273 
    274 	/* For control pipe */
    275 	usb_device_request_t	ux_request;
    276 
    277 	/* For isoc */
    278 	uint16_t	       *ux_frlengths;
    279 	int			ux_nframes;
    280 
    281 	const struct usbd_pipe_methods *ux_methods;
    282 
    283 	/* For memory allocation and softc */
    284 	struct usbd_bus        *ux_bus;
    285 	usb_dma_t		ux_dmabuf;
    286 	void		       *ux_buf;
    287 	uint32_t		ux_bufsize;
    288 
    289 	uint8_t			ux_rqflags;
    290 #define URQ_REQUEST	0x01
    291 
    292 	SIMPLEQ_ENTRY(usbd_xfer)
    293 				ux_next;
    294 
    295 	void		       *ux_hcpriv;	/* private use by the HC driver */
    296 
    297 	struct usb_task		ux_aborttask;
    298 	struct callout		ux_callout;
    299 
    300 	/*
    301 	 * Protected by bus lock.
    302 	 *
    303 	 * - ux_timeout_set: The timeout is scheduled as a callout or
    304 	 *   usb task, and has not yet acquired the bus lock.
    305 	 *
    306 	 * - ux_timeout_reset: The xfer completed, and was resubmitted
    307 	 *   before the callout or task was able to acquire the bus
    308 	 *   lock, so one or the other needs to schedule a new callout.
    309 	 */
    310 	bool			ux_timeout_set;
    311 	bool			ux_timeout_reset;
    312 };
    313 
    314 void usbd_init(void);
    315 void usbd_finish(void);
    316 
    317 #if defined(USB_DEBUG)
    318 void usbd_dump_iface(struct usbd_interface *);
    319 void usbd_dump_device(struct usbd_device *);
    320 void usbd_dump_endpoint(struct usbd_endpoint *);
    321 void usbd_dump_queue(struct usbd_pipe *);
    322 void usbd_dump_pipe(struct usbd_pipe *);
    323 #endif
    324 
    325 /* Routines from usb_subr.c */
    326 int		usbctlprint(void *, const char *);
    327 void		usbd_get_device_strings(struct usbd_device *);
    328 void		usb_delay_ms_locked(struct usbd_bus *, u_int, kmutex_t *);
    329 void		usb_delay_ms(struct usbd_bus *, u_int);
    330 void		usbd_delay_ms_locked(struct usbd_device *, u_int, kmutex_t *);
    331 void		usbd_delay_ms(struct usbd_device *, u_int);
    332 usbd_status	usbd_reset_port(struct usbd_device *, int, usb_port_status_t *);
    333 usbd_status	usbd_setup_pipe(struct usbd_device *,
    334 				struct usbd_interface *,
    335 				struct usbd_endpoint *, int,
    336 				struct usbd_pipe **);
    337 usbd_status	usbd_setup_pipe_flags(struct usbd_device *,
    338 				      struct usbd_interface *,
    339 				      struct usbd_endpoint *, int,
    340 				      struct usbd_pipe **,
    341 				      uint8_t);
    342 usbd_status	usbd_new_device(device_t, struct usbd_bus *, int, int, int,
    343 				struct usbd_port *);
    344 usbd_status	usbd_reattach_device(device_t, struct usbd_device *,
    345 				     int, const int *);
    346 
    347 void		usbd_remove_device(struct usbd_device *, struct usbd_port *);
    348 bool		usbd_iface_locked(struct usbd_interface *);
    349 usbd_status	usbd_iface_lock(struct usbd_interface *);
    350 void		usbd_iface_unlock(struct usbd_interface *);
    351 usbd_status	usbd_iface_piperef(struct usbd_interface *);
    352 void		usbd_iface_pipeunref(struct usbd_interface *);
    353 usbd_status	usbd_fill_iface_data(struct usbd_device *, int, int);
    354 void		usb_free_device(struct usbd_device *);
    355 
    356 usbd_status	usb_insert_transfer(struct usbd_xfer *);
    357 void		usb_transfer_complete(struct usbd_xfer *);
    358 int		usb_disconnect_port(struct usbd_port *, device_t, int);
    359 
    360 usbd_status	usbd_endpoint_acquire(struct usbd_device *,
    361 		    struct usbd_endpoint *, int);
    362 void		usbd_endpoint_release(struct usbd_device *,
    363 		    struct usbd_endpoint *);
    364 
    365 void		usbd_kill_pipe(struct usbd_pipe *);
    366 usbd_status	usbd_attach_roothub(device_t, struct usbd_device *);
    367 usbd_status	usbd_probe_and_attach(device_t, struct usbd_device *, int, int);
    368 
    369 /* Routines from usb.c */
    370 void		usb_needs_explore(struct usbd_device *);
    371 void		usb_needs_reattach(struct usbd_device *);
    372 void		usb_schedsoftintr(struct usbd_bus *);
    373 
    374 static __inline int
    375 usbd_xfer_isread(struct usbd_xfer *xfer)
    376 {
    377 	if (xfer->ux_rqflags & URQ_REQUEST)
    378 		return xfer->ux_request.bmRequestType & UT_READ;
    379 
    380 	return xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress &
    381 	   UE_DIR_IN;
    382 }
    383 
    384 static __inline size_t
    385 usb_addr2dindex(int addr)
    386 {
    387 
    388 	return USB_ROOTHUB_INDEX + addr;
    389 }
    390 
    391 /*
    392  * These macros reflect the current locking scheme.  They might change.
    393  */
    394 
    395 #define usbd_lock_pipe(p)	mutex_enter((p)->up_dev->ud_bus->ub_lock)
    396 #define usbd_unlock_pipe(p)	mutex_exit((p)->up_dev->ud_bus->ub_lock)
    397