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