xref: /freebsd/sys/dev/usb/usb_device.h (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _USB2_DEVICE_H_
28 #define	_USB2_DEVICE_H_
29 
30 struct usb2_symlink;
31 
32 #define	USB_DEFAULT_XFER_MAX 2
33 
34 struct usb2_clear_stall_msg {
35 	struct usb2_proc_msg hdr;
36 	struct usb2_device *udev;
37 };
38 
39 /*
40  * The following structure defines an USB pipe which is equal to an
41  * USB endpoint.
42  */
43 struct usb2_pipe {
44 	struct usb2_xfer_queue pipe_q;	/* queue of USB transfers */
45 
46 	struct usb2_xfer *xfer_block;	/* blocking USB transfer */
47 	struct usb2_endpoint_descriptor *edesc;
48 	struct usb2_pipe_methods *methods;	/* set by HC driver */
49 
50 	uint16_t isoc_next;
51 	uint16_t refcount;
52 
53 	uint8_t	toggle_next:1;		/* next data toggle value */
54 	uint8_t	is_stalled:1;		/* set if pipe is stalled */
55 	uint8_t	is_synced:1;		/* set if we a synchronised */
56 	uint8_t	unused:5;
57 	uint8_t	iface_index;		/* not used by "default pipe" */
58 };
59 
60 /*
61  * The following structure defines an USB interface.
62  */
63 struct usb2_interface {
64 	struct usb2_perm perm;		/* interface permissions */
65 	struct usb2_interface_descriptor *idesc;
66 	device_t subdev;
67 	uint8_t	alt_index;
68 	uint8_t	parent_iface_index;
69 };
70 
71 /*
72  * The following structure defines the USB device flags.
73  */
74 struct usb2_device_flags {
75 	uint8_t	usb2_mode:1;		/* USB mode (see USB_MODE_XXX) */
76 	uint8_t	self_powered:1;		/* set if USB device is self powered */
77 	uint8_t	suspended:1;		/* set if USB device is suspended */
78 	uint8_t	no_strings:1;		/* set if USB device does not support
79 					 * strings */
80 	uint8_t	remote_wakeup:1;	/* set if remote wakeup is enabled */
81 	uint8_t	uq_bus_powered:1;	/* set if BUS powered quirk is present */
82 	uint8_t	uq_power_claim:1;	/* set if power claim quirk is present */
83 };
84 
85 /*
86  * The following structure is used for power-save purposes. The data
87  * in this structure is protected by the USB BUS lock.
88  */
89 struct usb2_power_save {
90 	int	last_xfer_time;		/* copy of "ticks" */
91 	uint32_t type_refs[4];		/* transfer reference count */
92 	uint32_t read_refs;		/* data read references */
93 	uint32_t write_refs;		/* data write references */
94 	uint8_t	suspended;		/* set if USB device is suspended */
95 };
96 
97 /*
98  * The following structure defines an USB device. There exists one of
99  * these structures for every USB device.
100  */
101 struct usb2_device {
102 	struct usb2_clear_stall_msg cs_msg[2];	/* generic clear stall
103 						 * messages */
104 	struct usb2_perm perm;
105 	struct sx default_sx[2];
106 	struct mtx default_mtx[1];
107 	struct cv default_cv[2];
108 	struct usb2_interface ifaces[USB_IFACE_MAX];
109 	struct usb2_pipe default_pipe;	/* Control Endpoint 0 */
110 	struct usb2_pipe pipes[USB_EP_MAX];
111 	struct usb2_power_save pwr_save;/* power save data */
112 
113 	struct usb2_bus *bus;		/* our USB BUS */
114 	device_t parent_dev;		/* parent device */
115 	struct usb2_device *parent_hub;
116 	struct usb2_config_descriptor *cdesc;	/* full config descr */
117 	struct usb2_hub *hub;		/* only if this is a hub */
118 	struct usb_device *linux_dev;
119 	struct usb2_xfer *default_xfer[USB_DEFAULT_XFER_MAX];
120 	struct usb2_temp_data *usb2_template_ptr;
121 	struct usb2_pipe *pipe_curr;	/* current clear stall pipe */
122 	struct usb2_fifo *fifo[USB_FIFO_MAX];
123 	struct usb2_symlink *ugen_symlink;	/* our generic symlink */
124 
125 	uint32_t plugtime;		/* copy of "ticks" */
126 
127 	uint16_t refcount;
128 #define	USB_DEV_REF_MAX 0xffff
129 
130 	uint16_t power;			/* mA the device uses */
131 	uint16_t langid;		/* language for strings */
132 
133 	uint8_t	address;		/* device addess */
134 	uint8_t	device_index;		/* device index in "bus->devices" */
135 	uint8_t	curr_config_index;	/* current configuration index */
136 	uint8_t	curr_config_no;		/* current configuration number */
137 	uint8_t	depth;			/* distance from root HUB */
138 	uint8_t	speed;			/* low/full/high speed */
139 	uint8_t	port_index;		/* parent HUB port index */
140 	uint8_t	port_no;		/* parent HUB port number */
141 	uint8_t	hs_hub_addr;		/* high-speed HUB address */
142 	uint8_t	hs_port_no;		/* high-speed HUB port number */
143 	uint8_t	driver_added_refcount;	/* our driver added generation count */
144 	uint8_t	power_mode;		/* see USB_POWER_XXX */
145 
146 	/* the "flags" field is write-protected by "bus->mtx" */
147 
148 	struct usb2_device_flags flags;
149 
150 	struct usb2_endpoint_descriptor default_ep_desc;	/* for pipe 0 */
151 	struct usb2_device_descriptor ddesc;	/* device descriptor */
152 
153 	char	serial[64];		/* serial number */
154 	char	manufacturer[64];	/* manufacturer string */
155 	char	product[64];		/* product string */
156 };
157 
158 /* globals */
159 
160 extern int usb2_template;
161 
162 /* function prototypes */
163 
164 struct usb2_device *usb2_alloc_device(device_t parent_dev, struct usb2_bus *bus,
165 		    struct usb2_device *parent_hub, uint8_t depth,
166 		    uint8_t port_index, uint8_t port_no, uint8_t speed,
167 		    uint8_t usb2_mode);
168 struct usb2_pipe *usb2_get_pipe(struct usb2_device *udev, uint8_t iface_index,
169 		    const struct usb2_config *setup);
170 struct usb2_pipe *usb2_get_pipe_by_addr(struct usb2_device *udev, uint8_t ea_val);
171 usb2_error_t	usb2_interface_count(struct usb2_device *udev, uint8_t *count);
172 usb2_error_t	usb2_probe_and_attach(struct usb2_device *udev,
173 		    uint8_t iface_index);
174 usb2_error_t	usb2_reset_iface_endpoints(struct usb2_device *udev,
175 		    uint8_t iface_index);
176 usb2_error_t	usb2_set_config_index(struct usb2_device *udev, uint8_t index);
177 usb2_error_t	usb2_set_endpoint_stall(struct usb2_device *udev,
178 		    struct usb2_pipe *pipe, uint8_t do_stall);
179 usb2_error_t	usb2_suspend_resume(struct usb2_device *udev,
180 		    uint8_t do_suspend);
181 void	usb2_detach_device(struct usb2_device *udev, uint8_t iface_index,
182 	    uint8_t free_subdev);
183 void	usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len);
184 void	usb2_free_device(struct usb2_device *udev);
185 void	*usb2_find_descriptor(struct usb2_device *udev, void *id,
186 	    uint8_t iface_index, uint8_t type, uint8_t type_mask,
187 	    uint8_t subtype, uint8_t subtype_mask);
188 void	usb_linux_free_device(struct usb_device *dev);
189 uint8_t	usb2_peer_can_wakeup(struct usb2_device *udev);
190 struct usb2_pipe *usb2_pipe_foreach(struct usb2_device *udev, struct usb2_pipe *pipe);
191 
192 #endif					/* _USB2_DEVICE_H_ */
193