1 /*
2 *******************************************************************************
3 *                                            sun project ---- usb host module
4 *
5 *                             Copyright(C), 2006-2008, SoftWinners Co., Ltd.
6 *                                                  All Rights Reserved
7 *
8 * File Name :
9 *
10 * Author : GLHuang(HoLiGun)
11 *
12 * Version : 1.0
13 *
14 * Date : 2008.05.xx
15 *
16 * Description :
17 *
18 * History :
19 ********************************************************************************************************************
20 */
21 
22 #ifndef __USB_HCD_H__
23 #define __USB_HCD_H__
24 
25 #include <usb_host_common.h>
26 //#include <usb/osal/RT-Thread/_os_timer.h>
27 #include <hal_osal.h>
28 
29 
30 //USB Packet IDs (PIDs)
31 
32 #define USB_PID_UNDEF_0         0xf0
33 #define USB_PID_OUT             0xe1
34 #define USB_PID_ACK             0xd2
35 #define USB_PID_DATA0           0xc3
36 #define USB_PID_PING            0xb4    /* USB 2.0 */
37 #define USB_PID_SOF             0xa5
38 #define USB_PID_NYET            0x96    /* USB 2.0 */
39 #define USB_PID_DATA2           0x87    /* USB 2.0 */
40 #define USB_PID_SPLIT           0x78    /* USB 2.0 */
41 #define USB_PID_IN              0x69
42 #define USB_PID_NAK             0x5a
43 #define USB_PID_DATA1           0x4b
44 #define USB_PID_PREAMBLE        0x3c    /* Token mode */
45 #define USB_PID_ERR             0x3c    /* USB 2.0: handshake mode */
46 #define USB_PID_SETUP           0x2d
47 #define USB_PID_STALL           0x1e
48 #define USB_PID_MDATA           0x0f    /* USB 2.0 */
49 
50 
51 /* (shifted) direction/type/recipient from the USB 2.0 spec, table 9.2 */
52 #define DeviceRequest \
53     ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
54 #define DeviceOutRequest \
55     ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
56 
57 #define InterfaceRequest \
58     ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
59 
60 #define EndpointRequest \
61     ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
62 #define EndpointOutRequest \
63     ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
64 
65 /* class requests from the USB 2.0 hub spec, table 11-15 */
66 /* GetBusState and SetHubDescriptor are optional, omitted */
67 #define ClearHubFeature     (0x2000 | USB_REQ_CLEAR_FEATURE)
68 #define ClearPortFeature    (0x2300 | USB_REQ_CLEAR_FEATURE)
69 #define GetHubDescriptor    (0xa000 | USB_REQ_GET_DESCRIPTOR)
70 #define GetHubStatus        (0xa000 | USB_REQ_GET_STATUS)
71 #define GetPortStatus       (0xa300 | USB_REQ_GET_STATUS)
72 #define SetHubFeature       (0x2000 | USB_REQ_SET_FEATURE)
73 #define SetPortFeature      (0x2300 | USB_REQ_SET_FEATURE)
74 
75 
76 #define HC_GEN_DEV___ACTIVE     0x01
77 #define HC_GEN_DEV___SUSPEND        0x04
78 #define HC_GEN_DEV___TRANSIENT      0x80
79 
80 #define HC_GEN_DEV_STATE_HALT       0
81 #define HC_GEN_DEV_STATE_RUNNING    (HC_GEN_DEV___ACTIVE)
82 #define HC_GEN_DEV_STATE_QUIESCING  (HC_GEN_DEV___SUSPEND|HC_GEN_DEV___TRANSIENT|HC_GEN_DEV___ACTIVE)
83 #define HC_GEN_DEV_STATE_RESUMING   (HC_GEN_DEV___SUSPEND|HC_GEN_DEV___TRANSIENT)
84 #define HC_GEN_DEV_STATE_SUSPENDED  (HC_GEN_DEV___SUSPEND)
85 #define HC_GEN_DEV_IS_RUNNING(state) ((state) & HC_GEN_DEV___ACTIVE)
86 #define HC_GEN_DEV_IS_SUSPENDED(state) ((state) & HC_GEN_DEV___SUSPEND)
87 
88 
89 struct hc_gen_dev
90 {
91     /* usb_bus.hcpriv points to this */
92     struct usb_virt_bus     self;       /* hcd is-a bus */
93 
94     const char      *product_desc;  /* product/vendor string */
95     char            irq_descr[24];  /* driver + bus # */
96 
97     //  struct timer_list   rh_timer;   //rh的timer
98     //  USB_OS_KERNEL_SOFT_TIMER *rh_timer; //其到时处理函数为rh_timer_func()
99     osal_timer_t rh_timer;
100     /* drives root-hub polling */
101     struct urb      *status_urb;    //查询root hub status的urb
102     /* the current status urb */
103 
104     /*
105      * hardware info/state
106      */
107     const struct hc_driver  *driver;    /* hw-specific hooks */
108     u8 saw_irq ;
109     u8 can_wakeup;      /* hw supports wakeup? */
110     u8 remote_wakeup;   /* sw should use wakeup? */
111     u8 rh_registered;   /* is root hub registered? */
112 
113     /* The next flag is a stopgap, to be removed when all the HCDs
114      * support the new root-hub polling mechanism. */
115     u8 uses_new_polling;
116     u8 poll_rh;         /* poll for rh status? */
117     u8 poll_pending;    /* status has changed? */
118     int irq;            /* irq allocated */
119     /* memory/io resource length */
120     u32 power_budget;   /* in mA, 0 = no limit */
121     int state;          //如HC_GEN_DEV_STATE_RUNNING
122 
123     /* more shared queuing code would be good; it should support
124      * smarter scheduling, handle transaction translators, etc;
125      * input size of periodic table to an interrupt scheduler.
126      * (ohci 32, uhci 1024, ehci 256/512/1024).
127      */
128 
129     /* The HC driver's private data is stored at the end of
130      * this structure.
131      */
132     u32 hcd_priv[1];
133 };
134 
135 
hcd_to_bus(struct hc_gen_dev * hcd)136 static inline struct usb_virt_bus *hcd_to_bus(struct hc_gen_dev *hcd)
137 {
138     return &hcd->self;
139 }
140 
bus_to_hcd(struct usb_virt_bus * bus)141 static inline struct hc_gen_dev *bus_to_hcd(struct usb_virt_bus *bus)
142 {
143     return container_of(bus, struct hc_gen_dev, self);
144 }
145 
146 #define HC_DRIVER_FLAG_HCD_MEMORY   0x0001      /* HC regs use memory (else I/O) */
147 #define HC_DRIVER_FLAG_HCD_USB11    0x0010      /* USB 1.1 */
148 #define HC_DRIVER_FLAG_HCD_USB2     0x0020      /* USB 2.0 */
149 
150 #define HUB_EVNET_PLUGIN_COMPLETE   0x01
151 #define HUB_EVNET_PLUGOUT_COMPLETE  0x02
152 
153 //*****************************
154 //具体hc的驱动注册上来的
155 //具体包含如下4类:
156 //包括如下部分:
157 //1,中断处理
158 //2,start/stop
159 //3,queue/dequeue,ep en
160 //4,hub queue/dequeue ,suspend/resume
161 //*****************************
162 struct hc_driver
163 {
164     const char  *description;   /* "ehci-hcd" etc */
165     const char  *product_desc;  /* product/vendor string */
166     u32         hcd_priv_size;      //size of private data,如hc_priv结构
167 
168     /* irq handler */
169     irqreturn_t(*irq)(struct hc_gen_dev *hcd);   //可以考虑放到下面,其实都无所谓,只是个irq申请
170     s32 flags;          //如HC_DRIVER_FLAG_HCD_USB2
171 
172     /* called to init HCD and root hub */
173     int(*reset)(struct hc_gen_dev *hcd);
174     int(*start)(struct hc_gen_dev *hcd);
175 
176     /* NOTE:  these suspend/resume calls relate to the HC as
177      * a whole, not just the root hub; they're for bus glue.
178      */
179     s32(*suspend)(struct hc_gen_dev *hcd);      // called after all devices were suspended
180     s32(*resume)(struct hc_gen_dev *hcd);       // called before any devices get resumed
181     void (*stop)(struct hc_gen_dev *hcd);       // cleanly make HCD stop writing memory and doing I/O
182     int(*get_frame_number)(struct hc_gen_dev *hcd);     // return current frame number
183     int(*urb_enqueue)(struct hc_gen_dev *hcd,
184                       struct urb *urb, unsigned mem_flags);
185     //manage i/o requests, device state
186     //如hcd_ops_urb_enqueue()
187 
188     int(*urb_dequeue)(struct hc_gen_dev *hcd, struct urb *urb);
189     void (*endpoint_disable)(struct hc_gen_dev *hcd,
190                              struct usb_host_virt_endpoint *ep);
191 
192     int(*hub_status_data)(struct hc_gen_dev *hcd, char *buf);
193     int(*hub_control)(struct hc_gen_dev *hcd,
194                       u16 typeReq,
195                       u16 wValue,
196                       u16 wIndex,
197                       char *buf,
198                       u16 wLength);
199     int(*hub_suspend)(struct hc_gen_dev *);
200     int(*hub_resume)(struct hc_gen_dev *);
201     int(*start_port_reset)(struct hc_gen_dev *, u32 port_num);
202     void (*hub_irq_enable)(struct hc_gen_dev *);
203 
204     /* Needed only if port-change IRQs are level-triggered */
205     s32(*hub_notify)(struct hc_gen_dev *, u32 event);       //such as HUB_EVNET_PLUGIN_COMPLETE
206 };
207 
208 int usb_hcd_link_urb_to_ep(struct hc_gen_dev *hcd, struct urb *urb);
209 void usb_hcd_unlink_urb_from_ep(struct hc_gen_dev *hcd, struct urb *urb);
210 void usb_hcd_start_port_resume(struct usb_virt_bus *bus, int portnum);
211 void usb_hcd_end_port_resume(struct usb_virt_bus *bus, int portnum);
212 int usb_hcd_check_unlink_urb(struct hc_gen_dev *hcd, struct urb *urb);
213 
214 struct hc_gen_dev *usb_create_hc_gen_dev(const struct hc_driver *driver, const char *bus_name);
215 s32 usb_add_hc_gen_dev(struct hc_gen_dev *hcd, u32  irqnum, u32 irqflags);
216 void usb_remove_hc_gen_dev(struct hc_gen_dev *hcd);
217 void usb_hcd_poll_rh_status(struct hc_gen_dev *hcd);
218 void usb_hcd_giveback_urb(struct hc_gen_dev *hcd, struct urb *urb);
219 
220 int hcd_ops_get_frame_number(struct usb_host_virt_dev *udev);
221 int hcd_ops_submit_urb(struct urb *urb, unsigned mem_flags);
222 int hcd_ops_unlink_urb(struct urb *urb, int status);
223 void *hcd_ops_buffer_alloc(struct usb_virt_bus  *bus, u32 size, u32 mem_flags, u32 *dma);
224 void hcd_ops_buffer_free(struct usb_virt_bus   *bus, u32    size, u32    *addr, u32 *dma);
225 void hcd_ops_endpoint_disable(struct usb_host_virt_dev *udev, struct usb_host_virt_endpoint *ep);
226 
227 int usb_hub_clear_tt_buffer(struct urb *urb);
228 
229 void usb_gen_hcd_init(void);
230 void usb_gen_hcd_exit(void);
231 
232 #endif
233 
234 
235 
236