]> git.enpas.org Git - openwrt.git/blob - target/linux/adm5120/files/drivers/usb/host/adm5120-hub.c
c431b8e1c652408153e9cd827da4656c566d1d6a
[openwrt.git] / target / linux / adm5120 / files / drivers / usb / host / adm5120-hub.c
1 /*
2  * ADM5120 HCD (Host Controller Driver) for USB
3  *
4  * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
5  *
6  * This file was derived from: drivers/usb/host/ohci-hub.c
7  *   (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
8  *   (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
9  *
10  * This file is licenced under the GPL.
11  */
12
13 /*-------------------------------------------------------------------------*/
14
15 /*
16  * ADM5120 Root Hub ... the nonsharable stuff
17  */
18
19 #define dbg_port(hc,label,num,value) \
20         admhc_dbg(hc, \
21                 "%s port%d " \
22                 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
23                 label, num, value, \
24                 (value & ADMHC_PS_PRSC) ? " PRSC" : "", \
25                 (value & ADMHC_PS_OCIC) ? " OCIC" : "", \
26                 (value & ADMHC_PS_PSSC) ? " PSSC" : "", \
27                 (value & ADMHC_PS_PESC) ? " PESC" : "", \
28                 (value & ADMHC_PS_CSC) ? " CSC" : "", \
29                 \
30                 (value & ADMHC_PS_LSDA) ? " LSDA" : "", \
31                 (value & ADMHC_PS_PPS) ? " PPS" : "", \
32                 (value & ADMHC_PS_PRS) ? " PRS" : "", \
33                 (value & ADMHC_PS_POCI) ? " POCI" : "", \
34                 (value & ADMHC_PS_PSS) ? " PSS" : "", \
35                 \
36                 (value & ADMHC_PS_PES) ? " PES" : "", \
37                 (value & ADMHC_PS_CCS) ? " CCS" : "" \
38                 );
39
40 #define dbg_port_write(hc,label,num,value) \
41         admhc_dbg(hc, \
42                 "%s port%d " \
43                 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
44                 label, num, value, \
45                 (value & ADMHC_PS_PRSC) ? " PRSC" : "", \
46                 (value & ADMHC_PS_OCIC) ? " OCIC" : "", \
47                 (value & ADMHC_PS_PSSC) ? " PSSC" : "", \
48                 (value & ADMHC_PS_PESC) ? " PESC" : "", \
49                 (value & ADMHC_PS_CSC) ? " CSC" : "", \
50                 \
51                 (value & ADMHC_PS_CPP) ? " CPP" : "", \
52                 (value & ADMHC_PS_SPP) ? " SPP" : "", \
53                 (value & ADMHC_PS_SPR) ? " SPR" : "", \
54                 (value & ADMHC_PS_CPS) ? " CPS" : "", \
55                 (value & ADMHC_PS_SPS) ? " SPS" : "", \
56                 \
57                 (value & ADMHC_PS_SPE) ? " SPE" : "", \
58                 (value & ADMHC_PS_CPE) ? " CPE" : "" \
59                 );
60
61 /*-------------------------------------------------------------------------*/
62
63 /* hcd->hub_irq_enable() */
64 static void admhc_hub_irq_enable(struct usb_hcd *hcd)
65 {
66         struct admhcd   *ahcd = hcd_to_admhcd(hcd);
67
68         spin_lock_irq(&ahcd->lock);
69         if (!ahcd->autostop)
70                 del_timer(&hcd->rh_timer);      /* Prevent next poll */
71         admhc_intr_enable(ahcd, ADMHC_INTR_INSM);
72         spin_unlock_irq(&ahcd->lock);
73 }
74
75 /*-------------------------------------------------------------------------*/
76
77 /* build "status change" packet (one or two bytes) from HC registers */
78
79 static int
80 admhc_hub_status_data(struct usb_hcd *hcd, char *buf)
81 {
82         struct admhcd   *ahcd = hcd_to_admhcd(hcd);
83         int             i, changed = 0, length = 1;
84         int             any_connected = 0;
85         unsigned long   flags;
86         u32             status;
87
88         spin_lock_irqsave(&ahcd->lock, flags);
89         if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
90                 goto done;
91
92         /* init status */
93         status = admhc_read_rhdesc(ahcd);
94         if (status & (ADMHC_RH_LPSC | ADMHC_RH_OCIC))
95                 buf [0] = changed = 1;
96         else
97                 buf [0] = 0;
98         if (ahcd->num_ports > 7) {
99                 buf [1] = 0;
100                 length++;
101         }
102
103         /* look at each port */
104         for (i = 0; i < ahcd->num_ports; i++) {
105                 status = admhc_read_portstatus(ahcd, i);
106
107                 /* can't autostop if ports are connected */
108                 any_connected |= (status & ADMHC_PS_CCS);
109
110                 if (status & (ADMHC_PS_CSC | ADMHC_PS_PESC | ADMHC_PS_PSSC
111                                 | ADMHC_PS_OCIC | ADMHC_PS_PRSC)) {
112                         changed = 1;
113                         if (i < 7)
114                             buf [0] |= 1 << (i + 1);
115                         else
116                             buf [1] |= 1 << (i - 7);
117                 }
118         }
119
120         hcd->poll_rh = admhc_root_hub_state_changes(ahcd, changed,
121                         any_connected);
122
123 done:
124         spin_unlock_irqrestore(&ahcd->lock, flags);
125
126         return changed ? length : 0;
127 }
128
129 /*-------------------------------------------------------------------------*/
130
131 static int admhc_get_hub_descriptor(struct admhcd *ahcd, char *buf)
132 {
133         struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
134         u32 rh = admhc_read_rhdesc(ahcd);
135         u16 temp;
136
137         desc->bDescriptorType = USB_DT_HUB;     /* Hub-descriptor */
138         desc->bPwrOn2PwrGood = ADMHC_POTPGT/2;  /* use default value */
139         desc->bHubContrCurrent = 0x00;          /* 0mA */
140
141         desc->bNbrPorts = ahcd->num_ports;
142         temp = 1 + (ahcd->num_ports / 8);
143         desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
144
145         /* FIXME */
146         temp = 0;
147         if (rh & ADMHC_RH_NPS)          /* no power switching? */
148             temp |= 0x0002;
149         if (rh & ADMHC_RH_PSM)          /* per-port power switching? */
150             temp |= 0x0001;
151         if (rh & ADMHC_RH_NOCP)         /* no overcurrent reporting? */
152             temp |= 0x0010;
153         else if (rh & ADMHC_RH_OCPM)    /* per-port overcurrent reporting? */
154             temp |= 0x0008;
155         desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ahcd, temp);
156
157         /* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
158         desc->bitmap[0] = 0;
159         desc->bitmap[0] = ~0;
160
161         return 0;
162 }
163
164 static int admhc_get_hub_status(struct admhcd *ahcd, char *buf)
165 {
166         struct usb_hub_status *hs = (struct usb_hub_status *)buf;
167         u32 t = admhc_read_rhdesc(ahcd);
168         u16 status, change;
169
170         status = 0;
171         status |= (t & ADMHC_RH_LPS) ? HUB_STATUS_LOCAL_POWER : 0;
172         status |= (t & ADMHC_RH_OCI) ? HUB_STATUS_OVERCURRENT : 0;
173
174         change = 0;
175         change |= (t & ADMHC_RH_LPSC) ? HUB_CHANGE_LOCAL_POWER : 0;
176         change |= (t & ADMHC_RH_OCIC) ? HUB_CHANGE_OVERCURRENT : 0;
177
178         hs->wHubStatus = (__force __u16)cpu_to_hc16(ahcd, status);
179         hs->wHubChange = (__force __u16)cpu_to_hc16(ahcd, change);
180
181         return 0;
182 }
183
184 static int admhc_get_port_status(struct admhcd *ahcd, unsigned port, char *buf)
185 {
186         struct usb_port_status *ps = (struct usb_port_status *)buf;
187         u32 t = admhc_read_portstatus(ahcd, port);
188         u16 status, change;
189
190         status = 0;
191         status |= (t & ADMHC_PS_CCS) ? USB_PORT_STAT_CONNECTION : 0;
192         status |= (t & ADMHC_PS_PES) ? USB_PORT_STAT_ENABLE : 0;
193         status |= (t & ADMHC_PS_PSS) ? USB_PORT_STAT_SUSPEND : 0;
194         status |= (t & ADMHC_PS_POCI) ? USB_PORT_STAT_OVERCURRENT : 0;
195         status |= (t & ADMHC_PS_PRS) ? USB_PORT_STAT_RESET : 0;
196         status |= (t & ADMHC_PS_PPS) ? USB_PORT_STAT_POWER : 0;
197         status |= (t & ADMHC_PS_LSDA) ? USB_PORT_STAT_LOW_SPEED : 0;
198
199         change = 0;
200         change |= (t & ADMHC_PS_CSC) ? USB_PORT_STAT_C_CONNECTION : 0;
201         change |= (t & ADMHC_PS_PESC) ? USB_PORT_STAT_C_ENABLE : 0;
202         change |= (t & ADMHC_PS_PSSC) ? USB_PORT_STAT_C_SUSPEND : 0;
203         change |= (t & ADMHC_PS_OCIC) ? USB_PORT_STAT_C_OVERCURRENT : 0;
204         change |= (t & ADMHC_PS_PRSC) ? USB_PORT_STAT_C_RESET : 0;
205
206         ps->wPortStatus = (__force __u16)cpu_to_hc16(ahcd, status);
207         ps->wPortChange = (__force __u16)cpu_to_hc16(ahcd, change);
208
209         return 0;
210 }
211
212 /*-------------------------------------------------------------------------*/
213
214 #ifdef  CONFIG_USB_OTG
215
216 static int admhc_start_port_reset(struct usb_hcd *hcd, unsigned port)
217 {
218         struct admhcd   *ahcd = hcd_to_admhcd(hcd);
219         u32                     status;
220
221         if (!port)
222                 return -EINVAL;
223         port--;
224
225         /* start port reset before HNP protocol times out */
226         status = admhc_read_portstatus(ahcd, port);
227         if (!(status & ADMHC_PS_CCS))
228                 return -ENODEV;
229
230         /* khubd will finish the reset later */
231         admhc_write_portstatus(ahcd, port, ADMHC_PS_PRS);
232         return 0;
233 }
234
235 static void start_hnp(struct admhcd *ahcd);
236
237 #else
238
239 #define admhc_start_port_reset          NULL
240
241 #endif
242
243 /*-------------------------------------------------------------------------*/
244
245
246 /* See usb 7.1.7.5:  root hubs must issue at least 50 msec reset signaling,
247  * not necessarily continuous ... to guard against resume signaling.
248  * The short timeout is safe for non-root hubs, and is backward-compatible
249  * with earlier Linux hosts.
250  */
251 #ifdef  CONFIG_USB_SUSPEND
252 #define PORT_RESET_MSEC         50
253 #else
254 #define PORT_RESET_MSEC         10
255 #endif
256
257 /* this timer value might be vendor-specific ... */
258 #define PORT_RESET_HW_MSEC      10
259
260 /* wrap-aware logic morphed from <linux/jiffies.h> */
261 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
262
263 /* called from some task, normally khubd */
264 static inline int admhc_port_reset(struct admhcd *ahcd, unsigned port)
265 {
266         u32 t;
267
268         admhc_vdbg(ahcd, "reset port%d\n", port);
269         t = admhc_read_portstatus(ahcd, port);
270         if (!(t & ADMHC_PS_CCS))
271                 return -ENODEV;
272
273         admhc_write_portstatus(ahcd, port, ADMHC_PS_SPR);
274         mdelay(10);
275         admhc_write_portstatus(ahcd, port, (ADMHC_PS_SPE | ADMHC_PS_CSC));
276         mdelay(100);
277
278         return 0;
279 }
280
281 static inline int admhc_port_enable(struct admhcd *ahcd, unsigned port)
282 {
283         u32 t;
284
285         admhc_vdbg(ahcd, "enable port%d\n", port);
286         t = admhc_read_portstatus(ahcd, port);
287         if (!(t & ADMHC_PS_CCS))
288                 return -ENODEV;
289
290         admhc_write_portstatus(ahcd, port, ADMHC_PS_SPE);
291
292         return 0;
293 }
294
295 static inline int admhc_port_disable(struct admhcd *ahcd, unsigned port)
296 {
297         u32 t;
298
299         admhc_vdbg(ahcd, "disable port%d\n", port);
300         t = admhc_read_portstatus(ahcd, port);
301         if (!(t & ADMHC_PS_CCS))
302                 return -ENODEV;
303
304         admhc_write_portstatus(ahcd, port, ADMHC_PS_CPE);
305
306         return 0;
307 }
308
309 static inline int admhc_port_write(struct admhcd *ahcd, unsigned port,
310                 u32 val)
311 {
312 #ifdef ADMHC_VERBOSE_DEBUG
313         dbg_port_write(ahcd, "write", port, val);
314 #endif
315         admhc_write_portstatus(ahcd, port, val);
316
317         return 0;
318 }
319
320 static int admhc_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
321                 u16 wIndex, char *buf, u16 wLength)
322 {
323         struct admhcd   *ahcd = hcd_to_admhcd(hcd);
324         int             ports = hcd_to_bus(hcd)->root_hub->maxchild;
325         int             ret = 0;
326
327         if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
328                 return -ESHUTDOWN;
329
330         switch (typeReq) {
331         case ClearHubFeature:
332                 switch (wValue) {
333                 case C_HUB_OVER_CURRENT:
334 #if 0                   /* FIXME */
335                         admhc_writel(ahcd, ADMHC_RH_OCIC,
336                                         &ahcd->regs->roothub.status);
337 #endif
338                 case C_HUB_LOCAL_POWER:
339                         break;
340                 default:
341                         goto error;
342                 }
343                 break;
344         case ClearPortFeature:
345                 if (!wIndex || wIndex > ports)
346                         goto error;
347                 wIndex--;
348
349                 switch (wValue) {
350                 case USB_PORT_FEAT_ENABLE:
351                         ret = admhc_port_disable(ahcd, wIndex);
352                         break;
353                 case USB_PORT_FEAT_SUSPEND:
354                         ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CPS);
355                         break;
356                 case USB_PORT_FEAT_POWER:
357                         ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CPP);
358                         break;
359                 case USB_PORT_FEAT_C_CONNECTION:
360                         ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CSC);
361                         break;
362                 case USB_PORT_FEAT_C_ENABLE:
363                         ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PESC);
364                         break;
365                 case USB_PORT_FEAT_C_SUSPEND:
366                         ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PSSC);
367                         break;
368                 case USB_PORT_FEAT_C_OVER_CURRENT:
369                         ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_OCIC);
370                         break;
371                 case USB_PORT_FEAT_C_RESET:
372                         ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PRSC);
373                         break;
374                 default:
375                         goto error;
376                 }
377                 break;
378         case GetHubDescriptor:
379                 ret = admhc_get_hub_descriptor(ahcd, buf);
380                 break;
381         case GetHubStatus:
382                 ret = admhc_get_hub_status(ahcd, buf);
383                 break;
384         case GetPortStatus:
385                 if (!wIndex || wIndex > ports)
386                         goto error;
387                 wIndex--;
388
389                 ret = admhc_get_port_status(ahcd, wIndex, buf);
390                 break;
391         case SetHubFeature:
392                 switch (wValue) {
393                 case C_HUB_OVER_CURRENT:
394                         /* FIXME:  this can be cleared, yes? */
395                 case C_HUB_LOCAL_POWER:
396                         break;
397                 default:
398                         goto error;
399                 }
400                 break;
401         case SetPortFeature:
402                 if (!wIndex || wIndex > ports)
403                         goto error;
404                 wIndex--;
405
406                 switch (wValue) {
407                 case USB_PORT_FEAT_ENABLE:
408                         ret = admhc_port_enable(ahcd, wIndex);
409                         break;
410                 case USB_PORT_FEAT_RESET:
411                         ret = admhc_port_reset(ahcd, wIndex);
412                         break;
413                 case USB_PORT_FEAT_SUSPEND:
414 #ifdef  CONFIG_USB_OTG
415                         if (hcd->self.otg_port == (wIndex + 1)
416                                         && hcd->self.b_hnp_enable)
417                                 start_hnp(ahcd);
418                         else
419 #endif
420                         ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_SPS);
421                         break;
422                 case USB_PORT_FEAT_POWER:
423                         ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_SPP);
424                         break;
425                 default:
426                         goto error;
427                 }
428                 break;
429
430         default:
431 error:
432                 /* "protocol stall" on error */
433                 ret = -EPIPE;
434         }
435
436         return ret;
437 }
438