Home | History | Annotate | Line # | Download | only in usb
usbdivar.h revision 1.109.2.9
      1  1.109.2.9     skrll /*	$NetBSD: usbdivar.h,v 1.109.2.9 2014/12/14 11:16:29 skrll 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.97       mrg  *	BUS METHOD		LOCK  NOTES
     42       1.97       mrg  *	----------------------- -------	-------------------------
     43  1.109.2.9     skrll  *	ubm_open		-	might want to take lock?
     44  1.109.2.9     skrll  *	ubm_softint		x
     45  1.109.2.9     skrll  *	ubm_dopoll		-	might want to take lock?
     46  1.109.2.9     skrll  *	ubm_allocx		-
     47  1.109.2.9     skrll  *	ubm_freex		-
     48  1.109.2.9     skrll  *	ubm_getlock 		-	Called at attach time
     49  1.109.2.9     skrll  *	ubm_newdev		-	Will take lock
     50  1.109.2.9     skrll 	ubm_rhctrl
     51       1.97       mrg  *
     52       1.97       mrg  *	PIPE METHOD		LOCK  NOTES
     53       1.97       mrg  *	----------------------- -------	-------------------------
     54       1.97       mrg  *	transfer		-
     55       1.97       mrg  *	start			-	might want to take lock?
     56       1.97       mrg  *	abort			x
     57       1.97       mrg  *	close			x
     58       1.97       mrg  *	cleartoggle		-
     59       1.97       mrg  *	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.109.2.5     skrll 	struct usb_dma_block *udma_block;
     80  1.109.2.5     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.109.2.5     skrll 	usb_endpoint_descriptor_t *ue_edesc;
     89  1.109.2.5     skrll 	int			ue_refcnt;
     90  1.109.2.5     skrll 	int			ue_toggle;
     91        1.1  augustss };
     92        1.1  augustss 
     93       1.29  augustss struct usbd_bus_methods {
     94  1.109.2.8     skrll 	usbd_status	      (*ubm_open)(struct usbd_pipe *);
     95  1.109.2.3     skrll 	void		      (*ubm_softint)(void *);
     96  1.109.2.3     skrll 	void		      (*ubm_dopoll)(struct usbd_bus *);
     97  1.109.2.3     skrll 	struct usbd_xfer *    (*ubm_allocx)(struct usbd_bus *);
     98  1.109.2.3     skrll 	void		      (*ubm_freex)(struct usbd_bus *, struct usbd_xfer *);
     99  1.109.2.3     skrll 	void		      (*ubm_getlock)(struct usbd_bus *, kmutex_t **);
    100  1.109.2.3     skrll 	usbd_status	      (*ubm_newdev)(device_t, usbd_bus_handle, int,
    101      1.105  jakllsch 					    int, int, struct usbd_port *);
    102  1.109.2.7     skrll 
    103  1.109.2.7     skrll 	int			(*ubm_rhctrl)(struct usbd_bus *,
    104  1.109.2.7     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.109.2.3     skrll 	usbd_status	      (*upm_transfer)(usbd_xfer_handle);
    109  1.109.2.3     skrll 	usbd_status	      (*upm_start)(usbd_xfer_handle);
    110  1.109.2.3     skrll 	void		      (*upm_abort)(usbd_xfer_handle);
    111  1.109.2.3     skrll 	void		      (*upm_close)(usbd_pipe_handle);
    112  1.109.2.3     skrll 	void		      (*upm_cleartoggle)(usbd_pipe_handle);
    113  1.109.2.3     skrll 	void		      (*upm_done)(usbd_xfer_handle);
    114        1.1  augustss };
    115        1.1  augustss 
    116       1.79  drochner #if 0 /* notyet */
    117       1.72  augustss struct usbd_tt {
    118       1.72  augustss 	struct usbd_hub	       *hub;
    119       1.72  augustss };
    120       1.79  drochner #endif
    121       1.72  augustss 
    122        1.1  augustss struct usbd_port {
    123  1.109.2.5     skrll 	usb_port_status_t	up_status;
    124  1.109.2.5     skrll 	uint16_t		up_power;	/* mA of current on port */
    125  1.109.2.5     skrll 	uint8_t			up_portno;
    126  1.109.2.5     skrll 	uint8_t			up_restartcnt;
    127       1.11  augustss #define USBD_RESTART_MAX 5
    128  1.109.2.5     skrll 	uint8_t			up_reattach;
    129  1.109.2.5     skrll 	struct usbd_device     *up_dev;		/* Connected device */
    130  1.109.2.5     skrll 	struct usbd_device     *up_parent;	/* The ports hub */
    131       1.79  drochner #if 0
    132  1.109.2.5     skrll 	struct usbd_tt	       *up_tt;	/* Transaction translator (if any) */
    133       1.79  drochner #endif
    134        1.1  augustss };
    135        1.1  augustss 
    136        1.1  augustss struct usbd_hub {
    137  1.109.2.5     skrll 	usbd_status	      (*uh_explore)(usbd_device_handle hub);
    138  1.109.2.5     skrll 	void		       *uh_hubsoftc;
    139  1.109.2.5     skrll 	usb_hub_descriptor_t	uh_hubdesc;
    140  1.109.2.5     skrll 	struct usbd_port        uh_ports[1];
    141        1.1  augustss };
    142        1.1  augustss 
    143        1.1  augustss /*****/
    144        1.1  augustss 
    145        1.1  augustss struct usbd_bus {
    146        1.1  augustss 	/* Filled by HC driver */
    147  1.109.2.5     skrll 	void			*ub_hcpriv;
    148  1.109.2.6     skrll 	int			ub_revision;	/* USB revision */
    149  1.109.2.6     skrll #define USBREV_UNKNOWN	0
    150  1.109.2.6     skrll #define USBREV_PRE_1_0	1
    151  1.109.2.6     skrll #define USBREV_1_0	2
    152  1.109.2.6     skrll #define USBREV_1_1	3
    153  1.109.2.6     skrll #define USBREV_2_0	4
    154  1.109.2.6     skrll #define USBREV_3_0	5
    155  1.109.2.6     skrll #define USBREV_STR { "unknown", "pre 1.0", "1.0", "1.1", "2.0", "3.0" }
    156  1.109.2.6     skrll 
    157  1.109.2.5     skrll 	const struct usbd_bus_methods *ub_methods;
    158  1.109.2.5     skrll 	uint32_t		ub_pipesize;	/* size of a pipe struct */
    159  1.109.2.6     skrll 	bool			ub_usedma;	/* Does this HC support DMA */
    160  1.109.2.6     skrll 	int			ub_dmaflags;
    161  1.109.2.6     skrll 	bus_dma_tag_t		ub_dmatag;	/* DMA tag */
    162  1.109.2.6     skrll 
    163        1.1  augustss 	/* Filled by usb driver */
    164  1.109.2.5     skrll 	kmutex_t		*ub_lock;
    165  1.109.2.5     skrll 	struct usbd_device      *ub_roothub;
    166  1.109.2.7     skrll 	uint8_t			ub_rhaddr;	/* roothub address */
    167  1.109.2.7     skrll 	uint8_t			ub_rhconf;	/* roothub configuration */
    168  1.109.2.5     skrll 	usbd_device_handle	ub_devices[USB_MAX_DEVICES];
    169  1.109.2.5     skrll 	kcondvar_t              ub_needsexplore_cv;
    170  1.109.2.5     skrll 	char			ub_needsexplore;/* a hub a signalled a change */
    171  1.109.2.5     skrll 	char			ub_usepolling;
    172  1.109.2.5     skrll 	device_t		ub_usbctl;
    173  1.109.2.5     skrll 	struct usb_device_stats	ub_stats;
    174       1.62  augustss 
    175  1.109.2.5     skrll 	void		       *ub_soft; /* soft interrupt cookie */
    176        1.1  augustss };
    177        1.1  augustss 
    178        1.1  augustss struct usbd_device {
    179  1.109.2.5     skrll 	struct usbd_bus	       *ud_bus;		/* our controller */
    180  1.109.2.5     skrll 	struct usbd_pipe       *ud_pipe0;	/* pipe 0 */
    181  1.109.2.5     skrll 	uint8_t			ud_addr;	/* device addess */
    182  1.109.2.5     skrll 	uint8_t			ud_config;	/* current configuration # */
    183  1.109.2.5     skrll 	uint8_t			ud_depth;	/* distance from root hub */
    184  1.109.2.5     skrll 	uint8_t			ud_speed;	/* low/full/high speed */
    185  1.109.2.5     skrll 	uint8_t			ud_selfpowered;	/* flag for self powered */
    186  1.109.2.5     skrll 	uint16_t		ud_power;	/* mA the device uses */
    187  1.109.2.5     skrll 	int16_t			ud_langid;	/* language for strings */
    188       1.13  augustss #define USBD_NOLANG (-1)
    189  1.109.2.5     skrll 	usb_event_cookie_t	ud_cookie;	/* unique connection id */
    190  1.109.2.5     skrll 	struct usbd_port       *ud_powersrc;	/* upstream hub port, or 0 */
    191  1.109.2.5     skrll 	struct usbd_device     *ud_myhub;	/* upstream hub */
    192  1.109.2.5     skrll 	struct usbd_port       *ud_myhsport;	/* closest high speed port */
    193  1.109.2.5     skrll 	struct usbd_endpoint	ud_ep0;		/* for pipe 0 */
    194  1.109.2.5     skrll 	usb_endpoint_descriptor_t ud_ep0desc;	/* for pipe 0 */
    195  1.109.2.5     skrll 	struct usbd_interface  *ud_ifaces;	/* array of all interfaces */
    196  1.109.2.5     skrll 	usb_device_descriptor_t ud_ddesc;	/* device descriptor */
    197  1.109.2.5     skrll 	usb_config_descriptor_t *ud_cdesc;	/* full config descr */
    198  1.109.2.5     skrll 	const struct usbd_quirks     *ud_quirks;/* device quirks, always set */
    199  1.109.2.5     skrll 	struct usbd_hub	       *ud_hub;		/* only if this is a hub */
    200  1.109.2.5     skrll 	int			ud_subdevlen;	/* array length of following */
    201  1.109.2.5     skrll 	device_t	       *ud_subdevs;	/* sub-devices */
    202  1.109.2.5     skrll 	int			ud_nifaces_claimed; /* number of ifaces in use */
    203  1.109.2.5     skrll 	void		       *ud_hcpriv;
    204        1.1  augustss };
    205        1.1  augustss 
    206        1.1  augustss struct usbd_interface {
    207  1.109.2.5     skrll 	struct usbd_device     *ui_dev;
    208  1.109.2.5     skrll 	usb_interface_descriptor_t *ui_idesc;
    209  1.109.2.5     skrll 	int			ui_index;
    210  1.109.2.5     skrll 	int			ui_altindex;
    211  1.109.2.5     skrll 	struct usbd_endpoint   *ui_endpoints;
    212  1.109.2.5     skrll 	void		       *ui_priv;
    213  1.109.2.5     skrll 	LIST_HEAD(, usbd_pipe)	ui_pipes;
    214        1.1  augustss };
    215        1.1  augustss 
    216        1.1  augustss struct usbd_pipe {
    217  1.109.2.5     skrll 	struct usbd_interface  *up_iface;
    218  1.109.2.5     skrll 	struct usbd_device     *up_dev;
    219  1.109.2.5     skrll 	struct usbd_endpoint   *up_endpoint;
    220  1.109.2.5     skrll 	int			up_refcnt;
    221  1.109.2.5     skrll 	char			up_running;
    222  1.109.2.5     skrll 	char			up_aborting;
    223  1.109.2.5     skrll 	SIMPLEQ_HEAD(, usbd_xfer) up_queue;
    224  1.109.2.5     skrll 	LIST_ENTRY(usbd_pipe)	up_next;
    225  1.109.2.5     skrll 	struct usb_task		up_async_task;
    226  1.109.2.5     skrll 
    227  1.109.2.5     skrll 	usbd_xfer_handle	up_intrxfer; /* used for repeating requests */
    228  1.109.2.5     skrll 	char			up_repeat;
    229  1.109.2.5     skrll 	int			up_interval;
    230  1.109.2.5     skrll 	uint8_t			up_flags;
    231       1.52  augustss 
    232        1.1  augustss 	/* Filled by HC driver. */
    233  1.109.2.5     skrll 	const struct usbd_pipe_methods *up_methods;
    234        1.1  augustss };
    235        1.1  augustss 
    236       1.40  augustss struct usbd_xfer {
    237  1.109.2.5     skrll 	struct usbd_pipe       *ux_pipe;
    238  1.109.2.5     skrll 	void		       *ux_priv;
    239  1.109.2.5     skrll 	void		       *ux_buffer;
    240  1.109.2.5     skrll 	kcondvar_t		ux_cv;
    241  1.109.2.5     skrll 	uint32_t		ux_length;
    242  1.109.2.5     skrll 	uint32_t		ux_actlen;
    243  1.109.2.5     skrll 	uint16_t		ux_flags;
    244  1.109.2.5     skrll 	uint32_t		ux_timeout;
    245  1.109.2.5     skrll 	usbd_status		ux_status;
    246  1.109.2.5     skrll 	usbd_callback		ux_callback;
    247  1.109.2.5     skrll 	volatile uint8_t	ux_done;
    248  1.109.2.5     skrll 	uint8_t			ux_state;	/* used for DIAGNOSTIC */
    249       1.77       chs #define XFER_FREE 0x46
    250       1.77       chs #define XFER_BUSY 0x55
    251       1.77       chs #define XFER_ONQU 0x9e
    252        1.1  augustss 
    253       1.29  augustss 	/* For control pipe */
    254  1.109.2.5     skrll 	usb_device_request_t	ux_request;
    255       1.29  augustss 
    256       1.29  augustss 	/* For isoc */
    257  1.109.2.5     skrll 	uint16_t		*ux_frlengths;
    258  1.109.2.5     skrll 	int			ux_nframes;
    259       1.29  augustss 
    260       1.29  augustss 	/* For memory allocation */
    261  1.109.2.5     skrll 	struct usbd_device     *ux_dev;
    262  1.109.2.5     skrll 	usb_dma_t		ux_dmabuf;
    263  1.109.2.5     skrll 	void		       *ux_buf;
    264  1.109.2.5     skrll 	uint32_t		ux_bufsize;
    265       1.29  augustss 
    266  1.109.2.5     skrll 	uint8_t			ux_rqflags;
    267       1.29  augustss #define URQ_REQUEST	0x01
    268  1.109.2.4     skrll #define URQ_AUTO_BUFFER	0x10
    269        1.1  augustss 
    270  1.109.2.5     skrll 	SIMPLEQ_ENTRY(usbd_xfer) ux_next;
    271        1.1  augustss 
    272  1.109.2.5     skrll 	void		       *ux_hcpriv;	/* private use by the HC driver */
    273  1.109.2.5     skrll 	uint8_t			ux_hcflags;	/* private use by the HC driver */
    274       1.74  augustss #define UXFER_ABORTING	0x01	/* xfer is aborting. */
    275       1.74  augustss #define UXFER_ABORTWAIT	0x02	/* abort completion is being awaited. */
    276  1.109.2.5     skrll 	kcondvar_t		ux_hccv;	/* private use by the HC driver */
    277       1.11  augustss 
    278  1.109.2.5     skrll 	struct callout		ux_callout;
    279        1.1  augustss };
    280        1.1  augustss 
    281       1.57  augustss void usbd_init(void);
    282       1.57  augustss void usbd_finish(void);
    283       1.64  augustss 
    284      1.103     skrll #if defined(USB_DEBUG) || defined(EHCI_DEBUG) || defined(OHCI_DEBUG)
    285  1.109.2.8     skrll void usbd_dump_iface(struct usbd_interface *);
    286  1.109.2.8     skrll void usbd_dump_device(struct usbd_device *);
    287  1.109.2.8     skrll void usbd_dump_endpoint(struct usbd_endpoint *);
    288  1.109.2.8     skrll void usbd_dump_queue(usbd_pipe_handle);
    289  1.109.2.8     skrll void usbd_dump_pipe(usbd_pipe_handle);
    290       1.67  augustss #endif
    291        1.1  augustss 
    292        1.1  augustss /* Routines from usb_subr.c */
    293       1.57  augustss int		usbctlprint(void *, const char *);
    294       1.97       mrg void		usb_delay_ms_locked(usbd_bus_handle, u_int, kmutex_t *);
    295       1.57  augustss void		usb_delay_ms(usbd_bus_handle, u_int);
    296       1.97       mrg void		usbd_delay_ms_locked(usbd_device_handle, u_int, kmutex_t *);
    297       1.96       mrg void		usbd_delay_ms(usbd_device_handle, u_int);
    298       1.71    itojun usbd_status	usbd_reset_port(usbd_device_handle, int, usb_port_status_t *);
    299  1.109.2.8     skrll usbd_status	usbd_setup_pipe(usbd_device_handle,
    300  1.109.2.8     skrll 				usbd_interface_handle,
    301       1.57  augustss 				struct usbd_endpoint *, int,
    302  1.109.2.8     skrll 				usbd_pipe_handle *);
    303  1.109.2.8     skrll usbd_status	usbd_setup_pipe_flags(usbd_device_handle,
    304  1.109.2.8     skrll 				      usbd_interface_handle,
    305      1.101  jmcneill 				      struct usbd_endpoint *, int,
    306  1.109.2.8     skrll 				      usbd_pipe_handle *,
    307  1.109.2.8     skrll 				      uint8_t);
    308       1.90  uebayasi usbd_status	usbd_new_device(device_t, usbd_bus_handle, int, int, int,
    309      1.102     skrll 				struct usbd_port *);
    310       1.90  uebayasi usbd_status	usbd_reattach_device(device_t, usbd_device_handle,
    311      1.102     skrll 				     int, const int *);
    312       1.88      kent 
    313       1.57  augustss void		usbd_remove_device(usbd_device_handle, struct usbd_port *);
    314       1.71    itojun int		usbd_printBCD(char *, size_t, int);
    315       1.71    itojun usbd_status	usbd_fill_iface_data(usbd_device_handle, int, int);
    316       1.57  augustss void		usb_free_device(usbd_device_handle);
    317       1.57  augustss 
    318       1.71    itojun usbd_status	usb_insert_transfer(usbd_xfer_handle);
    319       1.71    itojun void		usb_transfer_complete(usbd_xfer_handle);
    320       1.91    dyoung int		usb_disconnect_port(struct usbd_port *, device_t, int);
    321        1.1  augustss 
    322      1.105  jakllsch void		usbd_kill_pipe(usbd_pipe_handle);
    323      1.105  jakllsch usbd_status	usbd_attach_roothub(device_t, usbd_device_handle);
    324      1.105  jakllsch usbd_status	usbd_probe_and_attach(device_t, usbd_device_handle, int, int);
    325      1.105  jakllsch usbd_status	usbd_get_initial_ddesc(usbd_device_handle,
    326      1.105  jakllsch 				       usb_device_descriptor_t *);
    327      1.105  jakllsch 
    328        1.1  augustss /* Routines from usb.c */
    329       1.63  augustss void		usb_needs_explore(usbd_device_handle);
    330       1.73      joff void		usb_needs_reattach(usbd_device_handle);
    331       1.57  augustss void		usb_schedsoftintr(struct usbd_bus *);
    332       1.97       mrg 
    333       1.97       mrg /*
    334      1.107     skrll  * These macros reflect the current locking scheme.  They might change.
    335       1.97       mrg  */
    336       1.97       mrg 
    337  1.109.2.5     skrll #define usbd_lock_pipe(p)	mutex_enter((p)->up_dev->ud_bus->ub_lock)
    338  1.109.2.5     skrll #define usbd_unlock_pipe(p)	mutex_exit((p)->up_dev->ud_bus->ub_lock)
    339