Home | History | Annotate | Line # | Download | only in usb
usbdivar.h revision 1.93.8.5
      1 /*	$NetBSD: usbdivar.h,v 1.93.8.5 2012/02/20 02:12:24 mrg Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/usbdivar.h,v 1.11 1999/11/17 22:33:51 n_hibma Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10  * Carlstedt Research & Technology.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/callout.h>
     35 #include <sys/mutex.h>
     36 
     37 /*
     38  * Discussion about locking in the USB code:
     39  *
     40  * There are two locks presented by the host controller: the interrupt lock
     41  * and the thread lock.  The interrupt lock, either a spin or adaptive mutex,
     42  * manages hardware state and anything else touched in an interrupt context.
     43  * The thread lock has everything else.
     44  *
     45  * List of hardware interface methods, and which locks are held when each
     46  * is called by this module:
     47  *
     48  *	BUS METHOD		INTR	THREAD  NOTES
     49  *	----------------------- ------- -------	-------------------------
     50  *	open_pipe		-	-	might want to take thread lock?
     51  *	soft_intr		-	x	sometimes called with intr lock also held -- perhaps a problem?
     52  *	do_poll			-	-	might want to take thread lock?
     53  *	allocm			-	-
     54  *	freem			-	-
     55  *	allocx			-	-
     56  *	freex			-	-
     57  *	get_locks 		-	-	Called at attach time
     58  *
     59  *	PIPE METHOD		INTR	THREAD  NOTES
     60  *	----------------------- ------- -------	-------------------------
     61  *	transfer		-	-
     62  *	start			-	-
     63  *	abort			-	x
     64  *	close			-	x
     65  *	cleartoggle		-	-
     66  *	done			-	x
     67  *
     68  * The above semantics are likely to change.
     69  *
     70  * USB functions known to expect the thread lock taken include:
     71  *    usb_transfer_complete()
     72  *    usb_insert_transfer()
     73  *    usb_start_next()
     74  *
     75  */
     76 
     77 /* From usb_mem.h */
     78 struct usb_dma_block;
     79 typedef struct {
     80 	struct usb_dma_block *block;
     81 	u_int offs;
     82 } usb_dma_t;
     83 
     84 struct usbd_xfer;
     85 struct usbd_pipe;
     86 
     87 struct usbd_endpoint {
     88 	usb_endpoint_descriptor_t *edesc;
     89 	int			refcnt;
     90 	int datatoggle;
     91 };
     92 
     93 struct usbd_bus_methods {
     94 	usbd_status	      (*open_pipe)(struct usbd_pipe *pipe);
     95 	void		      (*soft_intr)(void *);
     96 	void		      (*do_poll)(struct usbd_bus *);
     97 	usbd_status	      (*allocm)(struct usbd_bus *, usb_dma_t *,
     98 					u_int32_t bufsize);
     99 	void		      (*freem)(struct usbd_bus *, usb_dma_t *);
    100 	struct usbd_xfer *    (*allocx)(struct usbd_bus *);
    101 	void		      (*freex)(struct usbd_bus *, struct usbd_xfer *);
    102 	void		      (*get_locks)(struct usbd_bus *,
    103 					kmutex_t **, kmutex_t **);
    104 };
    105 
    106 struct usbd_pipe_methods {
    107 	usbd_status	      (*transfer)(usbd_xfer_handle xfer);
    108 	usbd_status	      (*start)(usbd_xfer_handle xfer);
    109 	void		      (*abort)(usbd_xfer_handle xfer);
    110 	void		      (*close)(usbd_pipe_handle pipe);
    111 	void		      (*cleartoggle)(usbd_pipe_handle pipe);
    112 	void		      (*done)(usbd_xfer_handle xfer);
    113 };
    114 
    115 #if 0 /* notyet */
    116 struct usbd_tt {
    117 	struct usbd_hub	       *hub;
    118 };
    119 #endif
    120 
    121 struct usbd_port {
    122 	usb_port_status_t	status;
    123 	u_int16_t		power;	/* mA of current on port */
    124 	u_int8_t		portno;
    125 	u_int8_t		restartcnt;
    126 #define USBD_RESTART_MAX 5
    127 	u_int8_t		reattach;
    128 	struct usbd_device     *device;	/* Connected device */
    129 	struct usbd_device     *parent;	/* The ports hub */
    130 #if 0
    131 	struct usbd_tt	       *tt; /* Transaction translator (if any) */
    132 #endif
    133 };
    134 
    135 struct usbd_hub {
    136 	usbd_status	      (*explore)(usbd_device_handle hub);
    137 	void		       *hubsoftc;
    138 	usb_hub_descriptor_t	hubdesc;
    139 	struct usbd_port        ports[1];
    140 };
    141 
    142 /*****/
    143 
    144 struct usbd_bus {
    145 	/* Filled by HC driver */
    146 	void			*hci_private;
    147 	const struct usbd_bus_methods *methods;
    148 	u_int32_t		pipe_size; /* size of a pipe struct */
    149 	/* Filled by usb driver */
    150 	kmutex_t		*intr_lock;
    151 	kmutex_t		*lock;
    152 	struct usbd_device      *root_hub;
    153 	usbd_device_handle	devices[USB_MAX_DEVICES];
    154 	kcondvar_t              needs_explore_cv;
    155 	char			needs_explore;/* a hub a signalled a change */
    156 	char			use_polling;
    157 	device_t		usbctl;
    158 	struct usb_device_stats	stats;
    159 	int 			intr_context;
    160 	u_int			no_intrs;
    161 	int			usbrev;	/* USB revision */
    162 #define USBREV_UNKNOWN	0
    163 #define USBREV_PRE_1_0	1
    164 #define USBREV_1_0	2
    165 #define USBREV_1_1	3
    166 #define USBREV_2_0	4
    167 #define USBREV_STR { "unknown", "pre 1.0", "1.0", "1.1", "2.0" }
    168 
    169 	void		       *soft; /* soft interrupt cookie */
    170 	bus_dma_tag_t		dmatag;	/* DMA tag */
    171 };
    172 
    173 struct usbd_device {
    174 	struct usbd_bus	       *bus;           /* our controller */
    175 	struct usbd_pipe       *default_pipe;  /* pipe 0 */
    176 	u_int8_t		address;       /* device addess */
    177 	u_int8_t		config;	       /* current configuration # */
    178 	u_int8_t		depth;         /* distance from root hub */
    179 	u_int8_t		speed;         /* low/full/high speed */
    180 	u_int8_t		self_powered;  /* flag for self powered */
    181 	u_int16_t		power;         /* mA the device uses */
    182 	int16_t			langid;	       /* language for strings */
    183 #define USBD_NOLANG (-1)
    184 	usb_event_cookie_t	cookie;	       /* unique connection id */
    185 	struct usbd_port       *powersrc;      /* upstream hub port, or 0 */
    186 	struct usbd_device     *myhub; 	       /* upstream hub */
    187 	struct usbd_port       *myhsport;      /* closest high speed port */
    188 	struct usbd_endpoint	def_ep;	       /* for pipe 0 */
    189 	usb_endpoint_descriptor_t def_ep_desc; /* for pipe 0 */
    190 	struct usbd_interface  *ifaces;        /* array of all interfaces */
    191 	usb_device_descriptor_t ddesc;         /* device descriptor */
    192 	usb_config_descriptor_t *cdesc;	       /* full config descr */
    193 	const struct usbd_quirks     *quirks;  /* device quirks, always set */
    194 	struct usbd_hub	       *hub;           /* only if this is a hub */
    195 	int			subdevlen;     /* array length of following */
    196 	device_t	       *subdevs;       /* sub-devices */
    197 	int			nifaces_claimed; /* number of ifaces in use */
    198 };
    199 
    200 struct usbd_interface {
    201 	struct usbd_device     *device;
    202 	usb_interface_descriptor_t *idesc;
    203 	int			index;
    204 	int			altindex;
    205 	struct usbd_endpoint   *endpoints;
    206 	void		       *priv;
    207 	LIST_HEAD(, usbd_pipe)	pipes;
    208 };
    209 
    210 struct usbd_pipe {
    211 	struct usbd_interface  *iface;
    212 	struct usbd_device     *device;
    213 	struct usbd_endpoint   *endpoint;
    214 	int			refcnt;
    215 	char			running;
    216 	char			aborting;
    217 	SIMPLEQ_HEAD(, usbd_xfer) queue;
    218 	LIST_ENTRY(usbd_pipe)	next;
    219 
    220 	usbd_xfer_handle	intrxfer; /* used for repeating requests */
    221 	char			repeat;
    222 	int			interval;
    223 
    224 	/* Filled by HC driver. */
    225 	const struct usbd_pipe_methods *methods;
    226 };
    227 
    228 struct usbd_xfer {
    229 	struct usbd_pipe       *pipe;
    230 	void		       *priv;
    231 	void		       *buffer;
    232 	kcondvar_t		cv;
    233 	u_int32_t		length;
    234 	u_int32_t		actlen;
    235 	u_int16_t		flags;
    236 	u_int32_t		timeout;
    237 	usbd_status		status;
    238 	usbd_callback		callback;
    239 	volatile u_int8_t	done;
    240 	u_int8_t		busy_free;	/* used for DIAGNOSTIC */
    241 #define XFER_FREE 0x46
    242 #define XFER_BUSY 0x55
    243 #define XFER_ONQU 0x9e
    244 
    245 	/* For control pipe */
    246 	usb_device_request_t	request;
    247 
    248 	/* For isoc */
    249 	u_int16_t		*frlengths;
    250 	int			nframes;
    251 
    252 	/* For memory allocation */
    253 	struct usbd_device     *device;
    254 	usb_dma_t		dmabuf;
    255 
    256 	u_int8_t		rqflags;
    257 #define URQ_REQUEST	0x01
    258 #define URQ_AUTO_DMABUF	0x10
    259 #define URQ_DEV_DMABUF	0x20
    260 
    261 	SIMPLEQ_ENTRY(usbd_xfer) next;
    262 
    263 	void		       *hcpriv; /* private use by the HC driver */
    264 	u_int8_t		hcflags; /* private use by the HC driver */
    265 #define UXFER_ABORTING	0x01	/* xfer is aborting. */
    266 #define UXFER_ABORTWAIT	0x02	/* abort completion is being awaited. */
    267 	kcondvar_t		hccv; /* private use by the HC driver */
    268 
    269         struct callout timeout_handle;
    270 };
    271 
    272 void usbd_init(void);
    273 void usbd_finish(void);
    274 
    275 #if defined(USB_DEBUG) || defined(EHCI_DEBUG)
    276 void usbd_dump_iface(struct usbd_interface *iface);
    277 void usbd_dump_device(struct usbd_device *dev);
    278 void usbd_dump_endpoint(struct usbd_endpoint *endp);
    279 void usbd_dump_queue(usbd_pipe_handle pipe);
    280 void usbd_dump_pipe(usbd_pipe_handle pipe);
    281 #endif
    282 
    283 /* Routines from usb_subr.c */
    284 int		usbctlprint(void *, const char *);
    285 void		usb_delay_ms(usbd_bus_handle, u_int);
    286 usbd_status	usbd_reset_port(usbd_device_handle, int, usb_port_status_t *);
    287 usbd_status	usbd_setup_pipe(usbd_device_handle dev,
    288 				usbd_interface_handle iface,
    289 				struct usbd_endpoint *, int,
    290 				usbd_pipe_handle *pipe);
    291 usbd_status	usbd_new_device(device_t, usbd_bus_handle, int, int, int,
    292                                 struct usbd_port *);
    293 usbd_status	usbd_reattach_device(device_t, usbd_device_handle,
    294                                      int, const int *);
    295 
    296 void		usbd_remove_device(usbd_device_handle, struct usbd_port *);
    297 int		usbd_printBCD(char *, size_t, int);
    298 usbd_status	usbd_fill_iface_data(usbd_device_handle, int, int);
    299 void		usb_free_device(usbd_device_handle);
    300 
    301 usbd_status	usb_insert_transfer(usbd_xfer_handle);
    302 void		usb_transfer_complete(usbd_xfer_handle);
    303 int		usb_disconnect_port(struct usbd_port *, device_t, int);
    304 
    305 /* Routines from usb.c */
    306 void		usb_needs_explore(usbd_device_handle);
    307 void		usb_needs_reattach(usbd_device_handle);
    308 void		usb_schedsoftintr(struct usbd_bus *);
    309 
    310 /*
    311  * These macros help while not all host controllers are ported to the MP code.
    312  */
    313 #define usbd_mutex_enter(m)	do { \
    314 	if (m) { \
    315 		s = -1; \
    316 		mutex_enter(m); \
    317 	} else \
    318 		s = splusb(); \
    319 } while (0)
    320 
    321 #define usbd_mutex_exit(m)	do { \
    322 	if (m) { \
    323 		s = -1; \
    324 		mutex_exit(m); \
    325 	} else \
    326 		splx(s); \
    327 } while (0)
    328 
    329 #define usbd_lock_pipe(p)	usbd_mutex_enter((p)->device->bus->lock)
    330 #define usbd_unlock_pipe(p)	usbd_mutex_exit((p)->device->bus->lock)
    331