2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
7 * This file is licenced under GPL
10 /*-------------------------------------------------------------------------*/
13 * OHCI Root Hub ... the nonsharable stuff
16 #define dbg_port(hc,label,num,value) \
19 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
21 (value & ADMHC_PS_PRSC) ? " PRSC" : "", \
22 (value & ADMHC_PS_OCIC) ? " OCIC" : "", \
23 (value & ADMHC_PS_PSSC) ? " PSSC" : "", \
24 (value & ADMHC_PS_PESC) ? " PESC" : "", \
25 (value & ADMHC_PS_CSC) ? " CSC" : "", \
27 (value & ADMHC_PS_LSDA) ? " LSDA" : "", \
28 (value & ADMHC_PS_PPS) ? " PPS" : "", \
29 (value & ADMHC_PS_PRS) ? " PRS" : "", \
30 (value & ADMHC_PS_POCI) ? " POCI" : "", \
31 (value & ADMHC_PS_PSS) ? " PSS" : "", \
33 (value & ADMHC_PS_PES) ? " PES" : "", \
34 (value & ADMHC_PS_CCS) ? " CCS" : "" \
37 #define dbg_port_write(hc,label,num,value) \
40 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
42 (value & ADMHC_PS_PRSC) ? " PRSC" : "", \
43 (value & ADMHC_PS_OCIC) ? " OCIC" : "", \
44 (value & ADMHC_PS_PSSC) ? " PSSC" : "", \
45 (value & ADMHC_PS_PESC) ? " PESC" : "", \
46 (value & ADMHC_PS_CSC) ? " CSC" : "", \
48 (value & ADMHC_PS_CPP) ? " CPP" : "", \
49 (value & ADMHC_PS_SPP) ? " SPP" : "", \
50 (value & ADMHC_PS_SPR) ? " SPR" : "", \
51 (value & ADMHC_PS_CPS) ? " CPS" : "", \
52 (value & ADMHC_PS_SPS) ? " SPS" : "", \
54 (value & ADMHC_PS_SPE) ? " SPE" : "", \
55 (value & ADMHC_PS_CPE) ? " CPE" : "" \
58 /*-------------------------------------------------------------------------*/
60 /* hcd->hub_irq_enable() */
61 static void admhc_hub_irq_enable(struct usb_hcd *hcd)
63 struct admhcd *ahcd = hcd_to_admhcd(hcd);
65 spin_lock_irq(&ahcd->lock);
67 del_timer(&hcd->rh_timer); /* Prevent next poll */
68 admhc_intr_enable(ahcd, ADMHC_INTR_INSM);
69 spin_unlock_irq(&ahcd->lock);
72 /*-------------------------------------------------------------------------*/
74 /* build "status change" packet (one or two bytes) from HC registers */
77 admhc_hub_status_data(struct usb_hcd *hcd, char *buf)
79 struct admhcd *ahcd = hcd_to_admhcd(hcd);
80 int i, changed = 0, length = 1;
81 int any_connected = 0;
85 spin_lock_irqsave(&ahcd->lock, flags);
86 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
90 status = admhc_read_rhdesc(ahcd);
91 if (status & (ADMHC_RH_LPSC | ADMHC_RH_OCIC))
92 buf [0] = changed = 1;
95 if (ahcd->num_ports > 7) {
100 /* look at each port */
101 for (i = 0; i < ahcd->num_ports; i++) {
102 status = admhc_read_portstatus(ahcd, i);
104 /* can't autostop if ports are connected */
105 any_connected |= (status & ADMHC_PS_CCS);
107 if (status & (ADMHC_PS_CSC | ADMHC_PS_PESC | ADMHC_PS_PSSC
108 | ADMHC_PS_OCIC | ADMHC_PS_PRSC)) {
111 buf [0] |= 1 << (i + 1);
113 buf [1] |= 1 << (i - 7);
117 hcd->poll_rh = admhc_root_hub_state_changes(ahcd, changed,
121 spin_unlock_irqrestore(&ahcd->lock, flags);
123 return changed ? length : 0;
126 /*-------------------------------------------------------------------------*/
128 static int admhc_get_hub_descriptor(struct admhcd *ahcd, char *buf)
130 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
131 u32 rh = admhc_read_rhdesc(ahcd);
134 desc->bDescriptorType = USB_DT_HUB; /* Hub-descriptor */
135 desc->bPwrOn2PwrGood = ADMHC_POTPGT/2; /* use default value */
136 desc->bHubContrCurrent = 0x00; /* 0mA */
138 desc->bNbrPorts = ahcd->num_ports;
139 temp = 1 + (ahcd->num_ports / 8);
140 desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
144 if (rh & ADMHC_RH_NPS) /* no power switching? */
146 if (rh & ADMHC_RH_PSM) /* per-port power switching? */
148 if (rh & ADMHC_RH_NOCP) /* no overcurrent reporting? */
150 else if (rh & ADMHC_RH_OCPM) /* per-port overcurrent reporting? */
152 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ahcd, temp);
154 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
156 desc->bitmap[0] = ~0;
161 static int admhc_get_hub_status(struct admhcd *ahcd, char *buf)
163 struct usb_hub_status *hs = (struct usb_hub_status *)buf;
164 u32 t = admhc_read_rhdesc(ahcd);
168 status |= (t & ADMHC_RH_LPS) ? HUB_STATUS_LOCAL_POWER : 0;
169 status |= (t & ADMHC_RH_OCI) ? HUB_STATUS_OVERCURRENT : 0;
172 change |= (t & ADMHC_RH_LPSC) ? HUB_CHANGE_LOCAL_POWER : 0;
173 change |= (t & ADMHC_RH_OCIC) ? HUB_CHANGE_OVERCURRENT : 0;
175 hs->wHubStatus = (__force __u16)cpu_to_hc16(ahcd, status);
176 hs->wHubChange = (__force __u16)cpu_to_hc16(ahcd, change);
181 static int admhc_get_port_status(struct admhcd *ahcd, unsigned port, char *buf)
183 struct usb_port_status *ps = (struct usb_port_status *)buf;
184 u32 t = admhc_read_portstatus(ahcd, port);
188 status |= (t & ADMHC_PS_CCS) ? USB_PORT_STAT_CONNECTION : 0;
189 status |= (t & ADMHC_PS_PES) ? USB_PORT_STAT_ENABLE : 0;
190 status |= (t & ADMHC_PS_PSS) ? USB_PORT_STAT_SUSPEND : 0;
191 status |= (t & ADMHC_PS_POCI) ? USB_PORT_STAT_OVERCURRENT : 0;
192 status |= (t & ADMHC_PS_PRS) ? USB_PORT_STAT_RESET : 0;
193 status |= (t & ADMHC_PS_PPS) ? USB_PORT_STAT_POWER : 0;
194 status |= (t & ADMHC_PS_LSDA) ? USB_PORT_STAT_LOW_SPEED : 0;
197 change |= (t & ADMHC_PS_CSC) ? USB_PORT_STAT_C_CONNECTION : 0;
198 change |= (t & ADMHC_PS_PESC) ? USB_PORT_STAT_C_ENABLE : 0;
199 change |= (t & ADMHC_PS_PSSC) ? USB_PORT_STAT_C_SUSPEND : 0;
200 change |= (t & ADMHC_PS_OCIC) ? USB_PORT_STAT_C_OVERCURRENT : 0;
201 change |= (t & ADMHC_PS_PRSC) ? USB_PORT_STAT_C_RESET : 0;
203 ps->wPortStatus = (__force __u16)cpu_to_hc16(ahcd, status);
204 ps->wPortChange = (__force __u16)cpu_to_hc16(ahcd, change);
209 /*-------------------------------------------------------------------------*/
211 #ifdef CONFIG_USB_OTG
213 static int admhc_start_port_reset(struct usb_hcd *hcd, unsigned port)
215 struct admhcd *ahcd = hcd_to_admhcd(hcd);
222 /* start port reset before HNP protocol times out */
223 status = admhc_read_portstatus(ahcd, port);
224 if (!(status & ADMHC_PS_CCS))
227 /* khubd will finish the reset later */
228 admhc_write_portstatus(ahcd, port, ADMHC_PS_PRS);
232 static void start_hnp(struct admhcd *ahcd);
236 #define admhc_start_port_reset NULL
240 /*-------------------------------------------------------------------------*/
243 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
244 * not necessarily continuous ... to guard against resume signaling.
245 * The short timeout is safe for non-root hubs, and is backward-compatible
246 * with earlier Linux hosts.
248 #ifdef CONFIG_USB_SUSPEND
249 #define PORT_RESET_MSEC 50
251 #define PORT_RESET_MSEC 10
254 /* this timer value might be vendor-specific ... */
255 #define PORT_RESET_HW_MSEC 10
257 /* wrap-aware logic morphed from <linux/jiffies.h> */
258 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
260 /* called from some task, normally khubd */
261 static inline int admhc_port_reset(struct admhcd *ahcd, unsigned port)
265 admhc_vdbg(ahcd, "reset port%d\n", port);
266 t = admhc_read_portstatus(ahcd, port);
267 if (!(t & ADMHC_PS_CCS))
270 admhc_write_portstatus(ahcd, port, ADMHC_PS_SPR);
272 admhc_write_portstatus(ahcd, port, (ADMHC_PS_SPE | ADMHC_PS_CSC));
278 static inline int admhc_port_enable(struct admhcd *ahcd, unsigned port)
282 admhc_vdbg(ahcd, "enable port%d\n", port);
283 t = admhc_read_portstatus(ahcd, port);
284 if (!(t & ADMHC_PS_CCS))
287 admhc_write_portstatus(ahcd, port, ADMHC_PS_SPE);
292 static inline int admhc_port_disable(struct admhcd *ahcd, unsigned port)
296 admhc_vdbg(ahcd, "disable port%d\n", port);
297 t = admhc_read_portstatus(ahcd, port);
298 if (!(t & ADMHC_PS_CCS))
301 admhc_write_portstatus(ahcd, port, ADMHC_PS_CPE);
306 static inline int admhc_port_write(struct admhcd *ahcd, unsigned port,
309 #ifdef ADMHC_VERBOSE_DEBUG
310 dbg_port_write(ahcd, "write", port, val);
312 admhc_write_portstatus(ahcd, port, val);
317 static int admhc_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
318 u16 wIndex, char *buf, u16 wLength)
320 struct admhcd *ahcd = hcd_to_admhcd(hcd);
321 int ports = hcd_to_bus(hcd)->root_hub->maxchild;
324 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
328 case ClearHubFeature:
330 case C_HUB_OVER_CURRENT:
332 admhc_writel(ahcd, ADMHC_RH_OCIC,
333 &ahcd->regs->roothub.status);
335 case C_HUB_LOCAL_POWER:
341 case ClearPortFeature:
342 if (!wIndex || wIndex > ports)
347 case USB_PORT_FEAT_ENABLE:
348 ret = admhc_port_disable(ahcd, wIndex);
350 case USB_PORT_FEAT_SUSPEND:
351 ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CPS);
353 case USB_PORT_FEAT_POWER:
354 ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CPP);
356 case USB_PORT_FEAT_C_CONNECTION:
357 ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CSC);
359 case USB_PORT_FEAT_C_ENABLE:
360 ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PESC);
362 case USB_PORT_FEAT_C_SUSPEND:
363 ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PSSC);
365 case USB_PORT_FEAT_C_OVER_CURRENT:
366 ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_OCIC);
368 case USB_PORT_FEAT_C_RESET:
369 ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PRSC);
375 case GetHubDescriptor:
376 ret = admhc_get_hub_descriptor(ahcd, buf);
379 ret = admhc_get_hub_status(ahcd, buf);
382 if (!wIndex || wIndex > ports)
386 ret = admhc_get_port_status(ahcd, wIndex, buf);
390 case C_HUB_OVER_CURRENT:
391 /* FIXME: this can be cleared, yes? */
392 case C_HUB_LOCAL_POWER:
399 if (!wIndex || wIndex > ports)
404 case USB_PORT_FEAT_ENABLE:
405 ret = admhc_port_enable(ahcd, wIndex);
407 case USB_PORT_FEAT_RESET:
408 ret = admhc_port_reset(ahcd, wIndex);
410 case USB_PORT_FEAT_SUSPEND:
411 #ifdef CONFIG_USB_OTG
412 if (hcd->self.otg_port == (wIndex + 1)
413 && hcd->self.b_hnp_enable)
417 ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_SPS);
419 case USB_PORT_FEAT_POWER:
420 ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_SPP);
429 /* "protocol stall" on error */