usb_device.h revision 191824
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_device.h 191824 2009-05-05 15:36:23Z thompsa $ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred *
14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184610Salfred * SUCH DAMAGE.
25184610Salfred */
26184610Salfred
27184610Salfred#ifndef _USB2_DEVICE_H_
28184610Salfred#define	_USB2_DEVICE_H_
29184610Salfred
30190191Sthompsastruct usb2_symlink;		/* UGEN */
31190180Sthompsastruct usb_device;		/* linux compat */
32184610Salfred
33184610Salfred#define	USB_DEFAULT_XFER_MAX 2
34184610Salfred
35190730Sthompsa/* "usb2_parse_config()" commands */
36190730Sthompsa
37190730Sthompsa#define	USB_CFG_ALLOC 0
38190730Sthompsa#define	USB_CFG_FREE 1
39190730Sthompsa#define	USB_CFG_INIT 2
40190730Sthompsa
41190730Sthompsa/* "usb2_unconfigure()" flags */
42190730Sthompsa
43190730Sthompsa#define	USB_UNCFG_FLAG_NONE 0x00
44190730Sthompsa#define	USB_UNCFG_FLAG_FREE_SUBDEV 0x01		/* subdevices are freed */
45190730Sthompsa#define	USB_UNCFG_FLAG_FREE_EP0	0x02		/* endpoint zero is freed */
46190730Sthompsa
47184610Salfredstruct usb2_clear_stall_msg {
48184610Salfred	struct usb2_proc_msg hdr;
49184610Salfred	struct usb2_device *udev;
50184610Salfred};
51184610Salfred
52184610Salfred/*
53184610Salfred * The following structure defines an USB pipe which is equal to an
54184610Salfred * USB endpoint.
55184610Salfred */
56184610Salfredstruct usb2_pipe {
57184610Salfred	struct usb2_xfer_queue pipe_q;	/* queue of USB transfers */
58184610Salfred
59184610Salfred	struct usb2_endpoint_descriptor *edesc;
60184610Salfred	struct usb2_pipe_methods *methods;	/* set by HC driver */
61184610Salfred
62184610Salfred	uint16_t isoc_next;
63184610Salfred	uint16_t refcount;
64184610Salfred
65184610Salfred	uint8_t	toggle_next:1;		/* next data toggle value */
66184610Salfred	uint8_t	is_stalled:1;		/* set if pipe is stalled */
67184610Salfred	uint8_t	is_synced:1;		/* set if we a synchronised */
68184610Salfred	uint8_t	unused:5;
69184610Salfred	uint8_t	iface_index;		/* not used by "default pipe" */
70184610Salfred};
71184610Salfred
72184610Salfred/*
73184610Salfred * The following structure defines an USB interface.
74184610Salfred */
75184610Salfredstruct usb2_interface {
76184610Salfred	struct usb2_interface_descriptor *idesc;
77184610Salfred	device_t subdev;
78184610Salfred	uint8_t	alt_index;
79184610Salfred	uint8_t	parent_iface_index;
80184610Salfred};
81184610Salfred
82184610Salfred/*
83184610Salfred * The following structure defines the USB device flags.
84184610Salfred */
85184610Salfredstruct usb2_device_flags {
86184610Salfred	uint8_t	usb2_mode:1;		/* USB mode (see USB_MODE_XXX) */
87184610Salfred	uint8_t	self_powered:1;		/* set if USB device is self powered */
88184610Salfred	uint8_t	no_strings:1;		/* set if USB device does not support
89184610Salfred					 * strings */
90184610Salfred	uint8_t	remote_wakeup:1;	/* set if remote wakeup is enabled */
91184610Salfred	uint8_t	uq_bus_powered:1;	/* set if BUS powered quirk is present */
92191824Sthompsa
93191824Sthompsa	/*
94191824Sthompsa	 * NOTE: Although the flags below will reach the same value
95191824Sthompsa	 * over time, but the instant values may differ, and
96191824Sthompsa	 * consequently the flags cannot be merged into one!
97191824Sthompsa	 */
98191824Sthompsa	uint8_t peer_suspended:1;	/* set if peer is suspended */
99191824Sthompsa	uint8_t self_suspended:1;	/* set if self is suspended */
100184610Salfred};
101184610Salfred
102184610Salfred/*
103186730Salfred * The following structure is used for power-save purposes. The data
104186730Salfred * in this structure is protected by the USB BUS lock.
105186730Salfred */
106186730Salfredstruct usb2_power_save {
107190181Sthompsa	usb2_ticks_t last_xfer_time;	/* copy of "ticks" */
108190181Sthompsa	usb2_size_t type_refs[4];	/* transfer reference count */
109190181Sthompsa	usb2_size_t read_refs;		/* data read references */
110190181Sthompsa	usb2_size_t write_refs;		/* data write references */
111186730Salfred};
112186730Salfred
113186730Salfred/*
114184610Salfred * The following structure defines an USB device. There exists one of
115184610Salfred * these structures for every USB device.
116184610Salfred */
117184610Salfredstruct usb2_device {
118184610Salfred	struct usb2_clear_stall_msg cs_msg[2];	/* generic clear stall
119184610Salfred						 * messages */
120184610Salfred	struct sx default_sx[2];
121184610Salfred	struct mtx default_mtx[1];
122184610Salfred	struct cv default_cv[2];
123190730Sthompsa	struct usb2_interface *ifaces;
124184610Salfred	struct usb2_pipe default_pipe;	/* Control Endpoint 0 */
125190730Sthompsa	struct usb2_pipe *pipes;
126186730Salfred	struct usb2_power_save pwr_save;/* power save data */
127184610Salfred	struct usb2_bus *bus;		/* our USB BUS */
128184610Salfred	device_t parent_dev;		/* parent device */
129184610Salfred	struct usb2_device *parent_hub;
130191395Sthompsa	struct usb2_device *parent_hs_hub;	/* high-speed parent HUB */
131184610Salfred	struct usb2_config_descriptor *cdesc;	/* full config descr */
132184610Salfred	struct usb2_hub *hub;		/* only if this is a hub */
133190180Sthompsa#if USB_HAVE_COMPAT_LINUX
134184610Salfred	struct usb_device *linux_dev;
135190180Sthompsa#endif
136184610Salfred	struct usb2_xfer *default_xfer[USB_DEFAULT_XFER_MAX];
137184610Salfred	struct usb2_temp_data *usb2_template_ptr;
138184610Salfred	struct usb2_pipe *pipe_curr;	/* current clear stall pipe */
139190191Sthompsa#if USB_HAVE_UGEN
140184610Salfred	struct usb2_fifo *fifo[USB_FIFO_MAX];
141184610Salfred	struct usb2_symlink *ugen_symlink;	/* our generic symlink */
142190730Sthompsa	struct cdev *default_dev;	/* Control Endpoint 0 device node */
143189110Sthompsa	LIST_HEAD(,usb2_fs_privdata) pd_list;
144190191Sthompsa	char	ugen_name[20];		/* name of ugenX.X device */
145190191Sthompsa#endif
146190181Sthompsa	usb2_ticks_t plugtime;		/* copy of "ticks" */
147184610Salfred
148191824Sthompsa	enum usb2_dev_state state;
149184610Salfred	uint16_t refcount;
150184610Salfred#define	USB_DEV_REF_MAX 0xffff
151184610Salfred
152184610Salfred	uint16_t power;			/* mA the device uses */
153184610Salfred	uint16_t langid;		/* language for strings */
154184610Salfred
155184610Salfred	uint8_t	address;		/* device addess */
156184610Salfred	uint8_t	device_index;		/* device index in "bus->devices" */
157184610Salfred	uint8_t	curr_config_index;	/* current configuration index */
158184610Salfred	uint8_t	curr_config_no;		/* current configuration number */
159184610Salfred	uint8_t	depth;			/* distance from root HUB */
160184610Salfred	uint8_t	speed;			/* low/full/high speed */
161184610Salfred	uint8_t	port_index;		/* parent HUB port index */
162184610Salfred	uint8_t	port_no;		/* parent HUB port number */
163184610Salfred	uint8_t	hs_hub_addr;		/* high-speed HUB address */
164184610Salfred	uint8_t	hs_port_no;		/* high-speed HUB port number */
165184610Salfred	uint8_t	driver_added_refcount;	/* our driver added generation count */
166184610Salfred	uint8_t	power_mode;		/* see USB_POWER_XXX */
167190730Sthompsa	uint8_t ifaces_max;		/* number of interfaces present */
168190730Sthompsa	uint8_t pipes_max;		/* number of pipes present */
169184610Salfred
170184610Salfred	/* the "flags" field is write-protected by "bus->mtx" */
171184610Salfred
172184610Salfred	struct usb2_device_flags flags;
173184610Salfred
174184610Salfred	struct usb2_endpoint_descriptor default_ep_desc;	/* for pipe 0 */
175184610Salfred	struct usb2_device_descriptor ddesc;	/* device descriptor */
176184610Salfred
177190191Sthompsa#if USB_HAVE_STRINGS
178184610Salfred	char	serial[64];		/* serial number */
179184610Salfred	char	manufacturer[64];	/* manufacturer string */
180184610Salfred	char	product[64];		/* product string */
181190191Sthompsa#endif
182184610Salfred};
183184610Salfred
184188987Sthompsa/* globals */
185188987Sthompsa
186188987Sthompsaextern int usb2_template;
187188987Sthompsa
188184610Salfred/* function prototypes */
189184610Salfred
190185948Sthompsastruct usb2_device *usb2_alloc_device(device_t parent_dev, struct usb2_bus *bus,
191185948Sthompsa		    struct usb2_device *parent_hub, uint8_t depth,
192185948Sthompsa		    uint8_t port_index, uint8_t port_no, uint8_t speed,
193185948Sthompsa		    uint8_t usb2_mode);
194185948Sthompsastruct usb2_pipe *usb2_get_pipe(struct usb2_device *udev, uint8_t iface_index,
195185948Sthompsa		    const struct usb2_config *setup);
196184610Salfredstruct usb2_pipe *usb2_get_pipe_by_addr(struct usb2_device *udev, uint8_t ea_val);
197185948Sthompsausb2_error_t	usb2_interface_count(struct usb2_device *udev, uint8_t *count);
198185948Sthompsausb2_error_t	usb2_probe_and_attach(struct usb2_device *udev,
199185948Sthompsa		    uint8_t iface_index);
200185948Sthompsausb2_error_t	usb2_reset_iface_endpoints(struct usb2_device *udev,
201185948Sthompsa		    uint8_t iface_index);
202185948Sthompsausb2_error_t	usb2_set_config_index(struct usb2_device *udev, uint8_t index);
203185948Sthompsausb2_error_t	usb2_set_endpoint_stall(struct usb2_device *udev,
204185948Sthompsa		    struct usb2_pipe *pipe, uint8_t do_stall);
205185948Sthompsausb2_error_t	usb2_suspend_resume(struct usb2_device *udev,
206185948Sthompsa		    uint8_t do_suspend);
207184610Salfredvoid	usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len);
208190730Sthompsavoid	usb2_free_device(struct usb2_device *, uint8_t);
209185948Sthompsavoid	*usb2_find_descriptor(struct usb2_device *udev, void *id,
210185948Sthompsa	    uint8_t iface_index, uint8_t type, uint8_t type_mask,
211185948Sthompsa	    uint8_t subtype, uint8_t subtype_mask);
212184610Salfredvoid	usb_linux_free_device(struct usb_device *dev);
213186730Salfreduint8_t	usb2_peer_can_wakeup(struct usb2_device *udev);
214188985Sthompsastruct usb2_pipe *usb2_pipe_foreach(struct usb2_device *udev, struct usb2_pipe *pipe);
215191494Sthompsavoid	usb2_set_device_state(struct usb2_device *udev,
216191824Sthompsa	    enum usb2_dev_state state);
217184610Salfred
218184610Salfred#endif					/* _USB2_DEVICE_H_ */
219