1 /*
2  * Copyright (c) 2001 by David Brownell
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 
20 /*-------------------------------------------------------------------------*/
21 
22 /*
23  * USB Host Controller Driver (usb_hcd) framework
24  *
25  * Since "struct usb_bus" is so thin, you can't share much code in it.
26  * This framework is a layer over that, and should be more sharable.
27  */
28 
29 /*-------------------------------------------------------------------------*/
30 
31 struct usb_hcd {	/* usb_bus.hcpriv points to this */
32 
33 	/*
34 	 * housekeeping
35 	 */
36 	struct usb_bus		*bus;		/* hcd is-a bus */
37 	struct list_head	hcd_list;
38 
39 	const char		*product_desc;
40 	const char		*description;	/* "ehci-hcd" etc */
41 
42 	struct timer_list	rh_timer;	/* drives root hub */
43 	struct list_head	dev_list;	/* devices on this bus */
44 	struct tq_struct	work;
45 
46 	/*
47 	 * hardware info/state
48 	 */
49 	struct hc_driver	*driver;	/* hw-specific hooks */
50 	int			irq;		/* irq allocated */
51 	void			*regs;		/* device memory/io */
52 
53 #ifdef	CONFIG_PCI
54 	/* a few non-PCI controllers exist, mostly for OHCI */
55 	struct pci_dev		*pdev;		/* pci is typical */
56 	int			region;		/* pci region for regs */
57 	u32			pci_state [16];	/* for PM state save */
58 	atomic_t		resume_count;	/* multiple resumes issue */
59 #endif
60 
61 	int			state;
62 #	define	__ACTIVE		0x01
63 #	define	__SLEEPY		0x02
64 #	define	__SUSPEND		0x04
65 #	define	__TRANSIENT		0x80
66 
67 #	define	USB_STATE_HALT		0
68 #	define	USB_STATE_RUNNING	(__ACTIVE)
69 #	define	USB_STATE_READY		(__ACTIVE|__SLEEPY)
70 #	define	USB_STATE_QUIESCING	(__SUSPEND|__TRANSIENT|__ACTIVE)
71 #	define	USB_STATE_RESUMING	(__SUSPEND|__TRANSIENT)
72 #	define	USB_STATE_SUSPENDED	(__SUSPEND)
73 
74 #define	HCD_IS_RUNNING(state) ((state) & __ACTIVE)
75 #define	HCD_IS_SUSPENDED(state) ((state) & __SUSPEND)
76 
77 	/* more shared queuing code would be good; it should support
78 	 * smarter scheduling, handle transaction translators, etc;
79 	 * input size of periodic table to an interrupt scheduler.
80 	 * (ohci 32, uhci 1024, ehci 256/512/1024).
81 	 */
82 };
83 
84 struct hcd_dev {	/* usb_device.hcpriv points to this */
85 	struct list_head	dev_list;	/* on this hcd */
86 	struct list_head	urb_list;	/* pending on this dev */
87 
88 	/* per-configuration HC/HCD state, such as QH or ED */
89 	void			*ep[32];
90 };
91 
92 // urb.hcpriv is really hardware-specific
93 
94 struct hcd_timeout {	/* timeouts we allocate */
95 	struct list_head	timeout_list;
96 	struct timer_list	timer;
97 };
98 
99 /*-------------------------------------------------------------------------*/
100 
101 /* each driver provides one of these, and hardware init support */
102 
103 struct hc_driver {
104 	const char	*description;	/* "ehci-hcd" etc */
105 
106 	/* irq handler */
107 	void	(*irq) (struct usb_hcd *hcd, struct pt_regs *regs);
108 
109 	int	flags;
110 #define	HCD_MEMORY	0x0001		/* HC regs use memory (else I/O) */
111 #define	HCD_USB11	0x0010		/* USB 1.1 */
112 #define	HCD_USB2	0x0020		/* USB 2.0 */
113 
114 	/* called to init HCD and root hub */
115 	int	(*start) (struct usb_hcd *hcd);
116 
117 	/* called after all devices were suspended */
118 	int	(*suspend) (struct usb_hcd *hcd, u32 state);
119 
120 	/* called before any devices get resumed */
121 	int	(*resume) (struct usb_hcd *hcd);
122 
123 	/* cleanly make HCD stop writing memory and doing I/O */
124 	void	(*stop) (struct usb_hcd *hcd);
125 
126 	/* return current frame number */
127 	int	(*get_frame_number) (struct usb_hcd *hcd);
128 
129 // FIXME: rework generic-to-specific HCD linkage (specific contains generic)
130 
131 	/* memory lifecycle */
132 	struct usb_hcd	*(*hcd_alloc) (void);
133 	void		(*hcd_free) (struct usb_hcd *hcd);
134 
135 	/* manage i/o requests, device state */
136 	int	(*urb_enqueue) (struct usb_hcd *hcd, struct urb *urb,
137 					int mem_flags);
138 	int	(*urb_dequeue) (struct usb_hcd *hcd, struct urb *urb);
139 
140 	// frees configuration resources -- allocated as needed during
141 	// urb_enqueue, and not freed by urb_dequeue
142 	void		(*free_config) (struct usb_hcd *hcd,
143 				struct usb_device *dev);
144 
145 	/* root hub support */
146 	int		(*hub_status_data) (struct usb_hcd *hcd, char *buf);
147 	int		(*hub_control) (struct usb_hcd *hcd,
148 				u16 typeReq, u16 wValue, u16 wIndex,
149 				char *buf, u16 wLength);
150 };
151 
152 extern void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb,
153 		struct pt_regs *regs);
154 
155 #ifdef CONFIG_PCI
156 
157 struct pci_device_id;
158 extern int usb_hcd_pci_probe (struct pci_dev *dev,
159 				const struct pci_device_id *id);
160 extern void usb_hcd_pci_remove (struct pci_dev *dev);
161 
162 #ifdef CONFIG_PM
163 // FIXME:  see Documentation/power/pci.txt (2.4.6 and later?)
164 // extern int usb_hcd_pci_save_state (struct pci_dev *dev, u32 state);
165 extern int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state);
166 extern int usb_hcd_pci_resume (struct pci_dev *dev);
167 // extern int usb_hcd_pci_enable_wake (struct pci_dev *dev, u32 state, int flg);
168 #endif /* CONFIG_PM */
169 
170 #endif /* CONFIG_PCI */
171 
172 /*-------------------------------------------------------------------------*/
173 
174 /*
175  * HCD Root Hub support
176  */
177 
178 #include "hub.h"
179 
180 /* (shifted) direction/type/recipient from the USB 2.0 spec, table 9.2 */
181 #define DeviceRequest \
182 	((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
183 #define DeviceOutRequest \
184 	((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
185 
186 #define InterfaceRequest \
187 	((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
188 
189 #define EndpointRequest \
190 	((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
191 #define EndpointOutRequest \
192 	((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
193 
194 /* table 9.6 standard features */
195 #define DEVICE_REMOTE_WAKEUP	1
196 #define ENDPOINT_HALT		0
197 
198 /* class requests from the USB 2.0 hub spec, table 11-15 */
199 /* GetBusState and SetHubDescriptor are optional, omitted */
200 #define ClearHubFeature		(0x2000 | USB_REQ_CLEAR_FEATURE)
201 #define ClearPortFeature	(0x2300 | USB_REQ_CLEAR_FEATURE)
202 #define GetHubDescriptor	(0xa000 | USB_REQ_GET_DESCRIPTOR)
203 #define GetHubStatus		(0xa000 | USB_REQ_GET_STATUS)
204 #define GetPortStatus		(0xa300 | USB_REQ_GET_STATUS)
205 #define SetHubFeature		(0x2000 | USB_REQ_SET_FEATURE)
206 #define SetPortFeature		(0x2300 | USB_REQ_SET_FEATURE)
207 
208 
209 /*-------------------------------------------------------------------------*/
210 
211 /*
212  * Generic bandwidth allocation constants/support
213  */
214 #define FRAME_TIME_USECS	1000L
215 #define BitTime(bytecount)  (7 * 8 * bytecount / 6)  /* with integer truncation */
216 		/* Trying not to use worst-case bit-stuffing
217                    of (7/6 * 8 * bytecount) = 9.33 * bytecount */
218 		/* bytecount = data payload byte count */
219 
220 #define NS_TO_US(ns)	((ns + 500L) / 1000L)
221 			/* convert & round nanoseconds to microseconds */
222 
223 extern void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb,
224 		int bustime, int isoc);
225 extern void usb_release_bandwidth (struct usb_device *dev, struct urb *urb,
226 		int isoc);
227 
228 /*
229  * Full/low speed bandwidth allocation constants/support.
230  */
231 #define BW_HOST_DELAY	1000L		/* nanoseconds */
232 #define BW_HUB_LS_SETUP	333L		/* nanoseconds */
233                         /* 4 full-speed bit times (est.) */
234 
235 #define FRAME_TIME_BITS         12000L		/* frame = 1 millisecond */
236 #define FRAME_TIME_MAX_BITS_ALLOC	(90L * FRAME_TIME_BITS / 100L)
237 #define FRAME_TIME_MAX_USECS_ALLOC	(90L * FRAME_TIME_USECS / 100L)
238 
239 extern int usb_check_bandwidth (struct usb_device *dev, struct urb *urb);
240 
241 /*
242  * Ceiling microseconds (typical) for that many bytes at high speed
243  * ISO is a bit less, no ACK ... from USB 2.0 spec, 5.11.3 (and needed
244  * to preallocate bandwidth)
245  */
246 #define USB2_HOST_DELAY	5	/* nsec, guess */
247 #define HS_USECS(bytes) NS_TO_US ( ((55 * 8 * 2083)/1000) \
248 	+ ((2083UL * (3167 + BitTime (bytes)))/1000) \
249 	+ USB2_HOST_DELAY)
250 #define HS_USECS_ISO(bytes) NS_TO_US ( ((long)(38 * 8 * 2.083)) \
251 	+ ((2083UL * (3167 + BitTime (bytes)))/1000) \
252 	+ USB2_HOST_DELAY)
253 
254 extern long usb_calc_bus_time (int speed, int is_input,
255 			int isoc, int bytecount);
256 
257 /*-------------------------------------------------------------------------*/
258 
259 /* hub.h ... DeviceRemovable in 2.4.2-ac11, gone in 2.4.10 */
260 // bleech -- resurfaced in 2.4.11 or 2.4.12
261 #define bitmap 	DeviceRemovable
262 
263 
264 /*-------------------------------------------------------------------------*/
265 
266 /* random stuff */
267 
268 #define	RUN_CONTEXT (in_irq () ? "in_irq" \
269 		: (in_interrupt () ? "in_interrupt" : "can sleep"))
270 
271 /* 2.5 changes ... */
272 
273 #ifndef container_of
274 #define	container_of	list_entry
275 #endif
276 
277 #define usb_get_urb(x) (x)
278 #define usb_put_urb(x)
279 
hcd_to_bus(struct usb_hcd * hcd)280 static inline struct usb_bus *hcd_to_bus (struct usb_hcd *hcd)
281 	{ return hcd->bus; }
282 
283 static inline void
usb_hub_tt_clear_buffer(struct usb_device * dev,int pipe)284 usb_hub_tt_clear_buffer (struct usb_device *dev, int pipe)
285 	{ }
286 
287 #define URB_ZERO_PACKET	USB_ZERO_PACKET
288 #define URB_ISO_ASAP	USB_ISO_ASAP
289