vserver 1.9.5.x5
[linux-2.6.git] / drivers / usb / host / sl811-hcd.c
1 /*
2  * SL811HS HCD (Host Controller Driver) for USB.
3  *
4  * Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
5  * Copyright (C) 2004 David Brownell
6  * 
7  * Periodic scheduling is based on Roman's OHCI code
8  *      Copyright (C) 1999 Roman Weissgaerber
9  *
10  * The SL811HS controller handles host side USB (like the SL11H, but with
11  * another register set and SOF generation) as well as peripheral side USB
12  * (like the SL811S).  This driver version doesn't implement the Gadget API
13  * for the peripheral role; or OTG (that'd need much external circuitry).
14  *
15  * For documentation, see the SL811HS spec and the "SL811HS Embedded Host"
16  * document (providing significant pieces missing from that spec); plus
17  * the SL811S spec if you want peripheral side info.
18  */ 
19
20 /*
21  * Status:  Passed basic stress testing, works with hubs, mice, keyboards,
22  * and usb-storage.
23  *
24  * TODO:
25  * - usb suspend/resume triggered by sl811 (with USB_SUSPEND)
26  * - various issues noted in the code
27  * - performance work; use both register banks; ...
28  * - use urb->iso_frame_desc[] with ISO transfers
29  */
30
31 #undef  VERBOSE
32 #undef  PACKET_TRACE
33
34 #include <linux/config.h>
35
36 #ifdef CONFIG_USB_DEBUG
37 #       define DEBUG
38 #else
39 #       undef DEBUG
40 #endif
41
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/kernel.h>
45 #include <linux/delay.h>
46 #include <linux/ioport.h>
47 #include <linux/sched.h>
48 #include <linux/slab.h>
49 #include <linux/smp_lock.h>
50 #include <linux/errno.h>
51 #include <linux/init.h>
52 #include <linux/timer.h>
53 #include <linux/list.h>
54 #include <linux/interrupt.h>
55 #include <linux/usb.h>
56 #include <linux/usb_sl811.h>
57
58 #include <asm/io.h>
59 #include <asm/irq.h>
60 #include <asm/system.h>
61 #include <asm/byteorder.h>
62
63 #include "../core/hcd.h"
64 #include "sl811.h"
65
66
67 MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
68 MODULE_LICENSE("GPL");
69
70 #define DRIVER_VERSION  "15 Dec 2004"
71
72
73 #ifndef DEBUG
74 #       define  STUB_DEBUG_FILE
75 #endif
76
77 /* for now, use only one transfer register bank */
78 #undef  USE_B
79
80 /* this doesn't understand urb->iso_frame_desc[], but if you had a driver
81  * that just queued one ISO frame per URB then iso transfers "should" work
82  * using the normal urb status fields.
83  */
84 #define DISABLE_ISO
85
86 // #define      QUIRK2
87 #define QUIRK3
88
89 static const char hcd_name[] = "sl811-hcd";
90
91 /*-------------------------------------------------------------------------*/
92
93 static irqreturn_t sl811h_irq(int irq, void *_hcd, struct pt_regs *regs);
94
95 static void port_power(struct sl811 *sl811, int is_on)
96 {
97         struct usb_hcd  *hcd = sl811_to_hcd(sl811);
98
99         /* hub is inactive unless the port is powered */
100         if (is_on) {
101                 if (sl811->port1 & (1 << USB_PORT_FEAT_POWER))
102                         return;
103
104                 sl811->port1 = (1 << USB_PORT_FEAT_POWER);
105                 sl811->irq_enable = SL11H_INTMASK_INSRMV;
106                 hcd->self.controller->power.power_state = PM_SUSPEND_ON;
107         } else {
108                 sl811->port1 = 0;
109                 sl811->irq_enable = 0;
110                 hcd->state = USB_STATE_HALT;
111                 hcd->self.controller->power.power_state = PM_SUSPEND_DISK;
112         }
113         sl811->ctrl1 = 0;
114         sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
115         sl811_write(sl811, SL11H_IRQ_STATUS, ~0);
116
117         if (sl811->board && sl811->board->port_power) {
118                 /* switch VBUS, at 500mA unless hub power budget gets set */
119                 DBG("power %s\n", is_on ? "on" : "off");
120                 sl811->board->port_power(hcd->self.controller, is_on);
121         }
122
123         /* reset as thoroughly as we can */
124         if (sl811->board && sl811->board->reset)
125                 sl811->board->reset(hcd->self.controller);
126
127         sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
128         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
129         sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT);
130         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
131
132         // if !is_on, put into lowpower mode now
133 }
134
135 /*-------------------------------------------------------------------------*/
136
137 /* This is a PIO-only HCD.  Queueing appends URBs to the endpoint's queue,
138  * and may start I/O.  Endpoint queues are scanned during completion irq
139  * handlers (one per packet: ACK, NAK, faults, etc) and urb cancelation.
140  *
141  * Using an external DMA engine to copy a packet at a time could work,
142  * though setup/teardown costs may be too big to make it worthwhile.
143  */
144
145 /* SETUP starts a new control request.  Devices are not allowed to
146  * STALL or NAK these; they must cancel any pending control requests.
147  */
148 static void setup_packet(
149         struct sl811            *sl811,
150         struct sl811h_ep        *ep,
151         struct urb              *urb,
152         u8                      bank,
153         u8                      control
154 )
155 {
156         u8                      addr;
157         u8                      len;
158         void __iomem            *data_reg;
159
160         addr = SL811HS_PACKET_BUF(bank == 0);
161         len = sizeof(struct usb_ctrlrequest);
162         data_reg = sl811->data_reg;
163         sl811_write_buf(sl811, addr, urb->setup_packet, len);
164
165         /* autoincrementing */
166         sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
167         writeb(len, data_reg);
168         writeb(SL_SETUP /* | ep->epnum */, data_reg);
169         writeb(usb_pipedevice(urb->pipe), data_reg);
170
171         /* always OUT/data0 */ ;
172         sl811_write(sl811, bank + SL11H_HOSTCTLREG,
173                         control | SL11H_HCTLMASK_OUT);
174         ep->length = 0;
175         PACKET("SETUP qh%p\n", ep);
176 }
177
178 /* STATUS finishes control requests, often after IN or OUT data packets */
179 static void status_packet(
180         struct sl811            *sl811,
181         struct sl811h_ep        *ep,
182         struct urb              *urb,
183         u8                      bank,
184         u8                      control
185 )
186 {
187         int                     do_out;
188         void __iomem            *data_reg;
189
190         do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe);
191         data_reg = sl811->data_reg;
192
193         /* autoincrementing */
194         sl811_write(sl811, bank + SL11H_BUFADDRREG, 0);
195         writeb(0, data_reg);
196         writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg);
197         writeb(usb_pipedevice(urb->pipe), data_reg);
198
199         /* always data1; sometimes IN */
200         control |= SL11H_HCTLMASK_TOGGLE;
201         if (do_out)
202                 control |= SL11H_HCTLMASK_OUT;
203         sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
204         ep->length = 0;
205         PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "",
206                         do_out ? "out" : "in", ep);
207 }
208
209 /* IN packets can be used with any type of endpoint. here we just
210  * start the transfer, data from the peripheral may arrive later.
211  * urb->iso_frame_desc is currently ignored here...
212  */
213 static void in_packet(
214         struct sl811            *sl811,
215         struct sl811h_ep        *ep,
216         struct urb              *urb,
217         u8                      bank,
218         u8                      control
219 )
220 {
221         u8                      addr;
222         u8                      len;
223         void __iomem            *data_reg;
224
225         /* avoid losing data on overflow */
226         len = ep->maxpacket;
227         addr = SL811HS_PACKET_BUF(bank == 0);
228         if (!(control & SL11H_HCTLMASK_ISOCH)
229                         && usb_gettoggle(urb->dev, ep->epnum, 0))
230                 control |= SL11H_HCTLMASK_TOGGLE;
231         data_reg = sl811->data_reg;
232
233         /* autoincrementing */
234         sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
235         writeb(len, data_reg);
236         writeb(SL_IN | ep->epnum, data_reg);
237         writeb(usb_pipedevice(urb->pipe), data_reg);
238
239         sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
240         ep->length = min((int)len,
241                         urb->transfer_buffer_length - urb->actual_length);
242         PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
243                         !!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len);
244 }
245
246 /* OUT packets can be used with any type of endpoint.
247  * urb->iso_frame_desc is currently ignored here...
248  */
249 static void out_packet(
250         struct sl811            *sl811,
251         struct sl811h_ep        *ep,
252         struct urb              *urb,
253         u8                      bank,
254         u8                      control
255 )
256 {
257         void                    *buf;
258         u8                      addr;
259         u8                      len;
260         void __iomem            *data_reg;
261
262         buf = urb->transfer_buffer + urb->actual_length;
263         prefetch(buf);
264
265         len = min((int)ep->maxpacket,
266                         urb->transfer_buffer_length - urb->actual_length);
267
268         if (!(control & SL11H_HCTLMASK_ISOCH)
269                         && usb_gettoggle(urb->dev, ep->epnum, 1))
270                 control |= SL11H_HCTLMASK_TOGGLE;
271         addr = SL811HS_PACKET_BUF(bank == 0);
272         data_reg = sl811->data_reg;
273
274         sl811_write_buf(sl811, addr, buf, len);
275
276         /* autoincrementing */
277         sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
278         writeb(len, data_reg);
279         writeb(SL_OUT | ep->epnum, data_reg);
280         writeb(usb_pipedevice(urb->pipe), data_reg);
281
282         sl811_write(sl811, bank + SL11H_HOSTCTLREG,
283                         control | SL11H_HCTLMASK_OUT);
284         ep->length = len;
285         PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
286                         !!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len);
287 }
288
289 /*-------------------------------------------------------------------------*/
290
291 /* caller updates on-chip enables later */
292
293 static inline void sofirq_on(struct sl811 *sl811)
294 {
295         if (sl811->irq_enable & SL11H_INTMASK_SOFINTR)
296                 return;
297         VDBG("sof irq on\n");
298         sl811->irq_enable |= SL11H_INTMASK_SOFINTR;
299 }
300
301 static inline void sofirq_off(struct sl811 *sl811)
302 {
303         if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR))
304                 return;
305         VDBG("sof irq off\n");
306         sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR;
307 }
308
309 /*-------------------------------------------------------------------------*/
310
311 /* pick the next endpoint for a transaction, and issue it.
312  * frames start with periodic transfers (after whatever is pending
313  * from the previous frame), and the rest of the time is async
314  * transfers, scheduled round-robin.
315  */
316 static struct sl811h_ep *start(struct sl811 *sl811, u8 bank)
317 {
318         struct sl811h_ep        *ep;
319         struct urb              *urb;
320         int                     fclock;
321         u8                      control;
322
323         /* use endpoint at schedule head */
324         if (sl811->next_periodic) {
325                 ep = sl811->next_periodic;
326                 sl811->next_periodic = ep->next;
327         } else {
328                 if (sl811->next_async)
329                         ep = sl811->next_async;
330                 else if (!list_empty(&sl811->async))
331                         ep = container_of(sl811->async.next,
332                                         struct sl811h_ep, schedule);
333                 else {
334                         /* could set up the first fullspeed periodic
335                          * transfer for the next frame ...
336                          */
337                         return NULL;
338                 }
339
340 #ifdef USE_B
341                 if ((bank && sl811->active_b == ep) || sl811->active_a == ep)
342                         return NULL;
343 #endif
344
345                 if (ep->schedule.next == &sl811->async)
346                         sl811->next_async = NULL;
347                 else
348                         sl811->next_async = container_of(ep->schedule.next,
349                                         struct sl811h_ep, schedule);
350         }
351
352         if (unlikely(list_empty(&ep->hep->urb_list))) {
353                 DBG("empty %p queue?\n", ep);
354                 return NULL;
355         }
356
357         urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
358         control = ep->defctrl;
359
360         /* if this frame doesn't have enough time left to transfer this
361          * packet, wait till the next frame.  too-simple algorithm...
362          */
363         fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6;
364         fclock -= 100;          /* setup takes not much time */
365         if (urb->dev->speed == USB_SPEED_LOW) {
366                 if (control & SL11H_HCTLMASK_PREAMBLE) {
367                         /* also note erratum 1: some hubs won't work */
368                         fclock -= 800;
369                 }
370                 fclock -= ep->maxpacket << 8;
371
372                 /* erratum 2: AFTERSOF only works for fullspeed */
373                 if (fclock < 0) {
374                         if (ep->period)
375                                 sl811->stat_overrun++;
376                         sofirq_on(sl811);
377                         return NULL;
378                 }
379         } else {
380                 fclock -= 12000 / 19;   /* 19 64byte packets/msec */
381                 if (fclock < 0) {
382                         if (ep->period)
383                                 sl811->stat_overrun++;
384                         control |= SL11H_HCTLMASK_AFTERSOF;
385
386                 /* throttle bulk/control irq noise */
387                 } else if (ep->nak_count)
388                         control |= SL11H_HCTLMASK_AFTERSOF;
389         }
390
391
392         switch (ep->nextpid) {
393         case USB_PID_IN:
394                 in_packet(sl811, ep, urb, bank, control);
395                 break;
396         case USB_PID_OUT:
397                 out_packet(sl811, ep, urb, bank, control);
398                 break;
399         case USB_PID_SETUP:
400                 setup_packet(sl811, ep, urb, bank, control);
401                 break;
402         case USB_PID_ACK:               /* for control status */
403                 status_packet(sl811, ep, urb, bank, control);
404                 break;
405         default:
406                 DBG("bad ep%p pid %02x\n", ep, ep->nextpid);
407                 ep = NULL;
408         }
409         return ep;
410 }
411
412 #define MIN_JIFFIES     ((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2)
413
414 static inline void start_transfer(struct sl811 *sl811)
415 {
416         if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
417                 return;
418         if (sl811->active_a == NULL) {
419                 sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF));
420                 if (sl811->active_a != NULL)
421                         sl811->jiffies_a = jiffies + MIN_JIFFIES;
422         }
423 #ifdef USE_B
424         if (sl811->active_b == NULL) {
425                 sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF));
426                 if (sl811->active_b != NULL)
427                         sl811->jiffies_b = jiffies + MIN_JIFFIES;
428         }
429 #endif
430 }
431
432 static void finish_request(
433         struct sl811            *sl811,
434         struct sl811h_ep        *ep,
435         struct urb              *urb,
436         struct pt_regs          *regs,
437         int                     status
438 ) __releases(sl811->lock) __acquires(sl811->lock)
439 {
440         unsigned                i;
441
442         if (usb_pipecontrol(urb->pipe))
443                 ep->nextpid = USB_PID_SETUP;
444
445         spin_lock(&urb->lock);
446         if (urb->status == -EINPROGRESS)
447                 urb->status = status;
448         spin_unlock(&urb->lock);
449
450         spin_unlock(&sl811->lock);
451         usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, regs);
452         spin_lock(&sl811->lock);
453
454         /* leave active endpoints in the schedule */
455         if (!list_empty(&ep->hep->urb_list))
456                 return;
457
458         /* async deschedule? */
459         if (!list_empty(&ep->schedule)) {
460                 list_del_init(&ep->schedule);
461                 if (ep == sl811->next_async)
462                         sl811->next_async = NULL;
463                 return;
464         }
465
466         /* periodic deschedule */
467         DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
468         for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
469                 struct sl811h_ep        *temp;
470                 struct sl811h_ep        **prev = &sl811->periodic[i];
471
472                 while (*prev && ((temp = *prev) != ep))
473                         prev = &temp->next;
474                 if (*prev)
475                         *prev = ep->next;
476                 sl811->load[i] -= ep->load;
477         }       
478         ep->branch = PERIODIC_SIZE;
479         sl811->periodic_count--;
480         sl811_to_hcd(sl811)->self.bandwidth_allocated
481                 -= ep->load / ep->period;
482         if (ep == sl811->next_periodic)
483                 sl811->next_periodic = ep->next;
484
485         /* we might turn SOFs back on again for the async schedule */
486         if (sl811->periodic_count == 0)
487                 sofirq_off(sl811);
488 }
489
490 static void
491 done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank, struct pt_regs *regs)
492 {
493         u8                      status;
494         struct urb              *urb;
495         int                     urbstat = -EINPROGRESS;
496
497         if (unlikely(!ep))
498                 return;
499
500         status = sl811_read(sl811, bank + SL11H_PKTSTATREG);
501
502         urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
503
504         /* we can safely ignore NAKs */
505         if (status & SL11H_STATMASK_NAK) {
506                 // PACKET("...NAK_%02x qh%p\n", bank, ep);
507                 if (!ep->period)
508                         ep->nak_count++;
509                 ep->error_count = 0;
510
511         /* ACK advances transfer, toggle, and maybe queue */
512         } else if (status & SL11H_STATMASK_ACK) {
513                 struct usb_device       *udev = urb->dev;
514                 int                     len;
515                 unsigned char           *buf;
516
517                 /* urb->iso_frame_desc is currently ignored here... */
518
519                 ep->nak_count = ep->error_count = 0;
520                 switch (ep->nextpid) {
521                 case USB_PID_OUT:
522                         // PACKET("...ACK/out_%02x qh%p\n", bank, ep);
523                         urb->actual_length += ep->length;
524                         usb_dotoggle(udev, ep->epnum, 1);
525                         if (urb->actual_length
526                                         == urb->transfer_buffer_length) {
527                                 if (usb_pipecontrol(urb->pipe))
528                                         ep->nextpid = USB_PID_ACK;
529
530                                 /* some bulk protocols terminate OUT transfers
531                                  * by a short packet, using ZLPs not padding.
532                                  */
533                                 else if (ep->length < ep->maxpacket
534                                                 || !(urb->transfer_flags
535                                                         & URB_ZERO_PACKET))
536                                         urbstat = 0;
537                         }
538                         break;
539                 case USB_PID_IN:
540                         // PACKET("...ACK/in_%02x qh%p\n", bank, ep);
541                         buf = urb->transfer_buffer + urb->actual_length;
542                         prefetchw(buf);
543                         len = ep->maxpacket - sl811_read(sl811,
544                                                 bank + SL11H_XFERCNTREG);
545                         if (len > ep->length) {
546                                 len = ep->length;
547                                 urb->status = -EOVERFLOW;
548                         }
549                         urb->actual_length += len;
550                         sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
551                                         buf, len);
552                         usb_dotoggle(udev, ep->epnum, 0);
553                         if (urb->actual_length == urb->transfer_buffer_length)
554                                 urbstat = 0;
555                         else if (len < ep->maxpacket) {
556                                 if (urb->transfer_flags & URB_SHORT_NOT_OK)
557                                         urbstat = -EREMOTEIO;
558                                 else
559                                         urbstat = 0;
560                         }
561                         if (usb_pipecontrol(urb->pipe)
562                                         && (urbstat == -EREMOTEIO
563                                                 || urbstat == 0)) {
564
565                                 /* NOTE if the status stage STALLs (why?),
566                                  * this reports the wrong urb status.
567                                  */
568                                 spin_lock(&urb->lock);
569                                 if (urb->status == -EINPROGRESS)
570                                         urb->status = urbstat;
571                                 spin_unlock(&urb->lock);
572
573                                 urb = NULL;
574                                 ep->nextpid = USB_PID_ACK;
575                         }
576                         break;
577                 case USB_PID_SETUP:
578                         // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
579                         if (urb->transfer_buffer_length == urb->actual_length)
580                                 ep->nextpid = USB_PID_ACK;
581                         else if (usb_pipeout(urb->pipe)) {
582                                 usb_settoggle(udev, 0, 1, 1);
583                                 ep->nextpid = USB_PID_OUT;
584                         } else {
585                                 usb_settoggle(udev, 0, 0, 1);
586                                 ep->nextpid = USB_PID_IN;
587                         }
588                         break;
589                 case USB_PID_ACK:
590                         // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
591                         urbstat = 0;
592                         break;
593                 }
594
595         /* STALL stops all transfers */
596         } else if (status & SL11H_STATMASK_STALL) {
597                 PACKET("...STALL_%02x qh%p\n", bank, ep);
598                 ep->nak_count = ep->error_count = 0;
599                 urbstat = -EPIPE;
600
601         /* error? retry, until "3 strikes" */
602         } else if (++ep->error_count >= 3) {
603                 if (status & SL11H_STATMASK_TMOUT)
604                         urbstat = -ETIMEDOUT;
605                 else if (status & SL11H_STATMASK_OVF)
606                         urbstat = -EOVERFLOW;
607                 else
608                         urbstat = -EPROTO;
609                 ep->error_count = 0;
610                 PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
611                                 bank, status, ep, urbstat);
612         }
613
614         if (urb && (urbstat != -EINPROGRESS || urb->status != -EINPROGRESS))
615                 finish_request(sl811, ep, urb, regs, urbstat);
616 }
617
618 static inline u8 checkdone(struct sl811 *sl811)
619 {
620         u8      ctl;
621         u8      irqstat = 0;
622
623         if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
624                 ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
625                 if (ctl & SL11H_HCTLMASK_ARM)
626                         sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
627                 DBG("%s DONE_A: ctrl %02x sts %02x\n",
628                         (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
629                         ctl,
630                         sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
631                 irqstat |= SL11H_INTMASK_DONE_A;
632         }
633 #ifdef  USE_B
634         if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
635                 ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
636                 if (ctl & SL11H_HCTLMASK_ARM)
637                         sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
638                 DBG("%s DONE_B: ctrl %02x sts %02x\n",
639                         (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
640                         ctl,
641                         sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
642                 irqstat |= SL11H_INTMASK_DONE_A;
643         }
644 #endif
645         return irqstat;
646 }
647
648 static irqreturn_t sl811h_irq(int irq, void *_hcd, struct pt_regs *regs)
649 {
650         struct usb_hcd  *hcd = _hcd;
651         struct sl811    *sl811 = hcd_to_sl811(hcd);
652         u8              irqstat;
653         irqreturn_t     ret = IRQ_NONE;
654         unsigned        retries = 5;
655
656         spin_lock(&sl811->lock);
657
658 retry:
659         irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
660         if (irqstat) {
661                 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
662                 irqstat &= sl811->irq_enable;
663         }
664
665 #ifdef  QUIRK2
666         /* this may no longer be necessary ... */
667         if (irqstat == 0 && ret == IRQ_NONE) {
668                 irqstat = checkdone(sl811);
669                 if (irqstat && irq != ~0)
670                         sl811->stat_lost++;
671         }
672 #endif
673
674         /* USB packets, not necessarily handled in the order they're
675          * issued ... that's fine if they're different endpoints.
676          */
677         if (irqstat & SL11H_INTMASK_DONE_A) {
678                 done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF), regs);
679                 sl811->active_a = NULL;
680                 sl811->stat_a++;
681         }
682 #ifdef USE_B
683         if (irqstat & SL11H_INTMASK_DONE_B) {
684                 done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF), regs);
685                 sl811->active_b = NULL;
686                 sl811->stat_b++;
687         }
688 #endif
689         if (irqstat & SL11H_INTMASK_SOFINTR) {
690                 unsigned index;
691
692                 index = sl811->frame++ % (PERIODIC_SIZE - 1);
693                 sl811->stat_sof++;
694
695                 /* be graceful about almost-inevitable periodic schedule
696                  * overruns:  continue the previous frame's transfers iff
697                  * this one has nothing scheduled.
698                  */
699                 if (sl811->next_periodic) {
700                         // ERR("overrun to slot %d\n", index);
701                         sl811->stat_overrun++;
702                 }
703                 if (sl811->periodic[index])
704                         sl811->next_periodic = sl811->periodic[index];
705         }
706
707         /* khubd manages debouncing and wakeup */
708         if (irqstat & SL11H_INTMASK_INSRMV) {
709                 sl811->stat_insrmv++;
710
711                 /* most stats are reset for each VBUS session */
712                 sl811->stat_wake = 0;
713                 sl811->stat_sof = 0;
714                 sl811->stat_a = 0;
715                 sl811->stat_b = 0;
716                 sl811->stat_lost = 0;
717
718                 sl811->ctrl1 = 0;
719                 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
720
721                 sl811->irq_enable = SL11H_INTMASK_INSRMV;
722                 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
723
724                 /* usbcore nukes other pending transactions on disconnect */
725                 if (sl811->active_a) {
726                         sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
727                         finish_request(sl811, sl811->active_a,
728                                 container_of(sl811->active_a->hep->urb_list.next,
729                                         struct urb, urb_list),
730                                 NULL, -ESHUTDOWN);
731                         sl811->active_a = NULL;
732                 }
733 #ifdef  USE_B
734                 if (sl811->active_b) {
735                         sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
736                         finish_request(sl811, sl811->active_b,
737                                 container_of(sl811->active_b->hep->urb_list.next,
738                                         struct urb, urb_list),
739                                 NULL, -ESHUTDOWN);
740                         sl811->active_b = NULL;
741                 }
742 #endif
743
744                 /* port status seems wierd until after reset, so
745                  * force the reset and make khubd clean up later.
746                  */
747                 sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
748                                 | (1 << USB_PORT_FEAT_CONNECTION);
749
750         } else if (irqstat & SL11H_INTMASK_RD) {
751                 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) {
752                         DBG("wakeup\n");
753                         sl811->port1 |= 1 << USB_PORT_FEAT_C_SUSPEND;
754                         sl811->stat_wake++;
755                 } else
756                         irqstat &= ~SL11H_INTMASK_RD;
757         }
758
759         if (irqstat) {
760                 if (sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
761                         start_transfer(sl811);
762                 ret = IRQ_HANDLED;
763                 hcd->saw_irq = 1;
764                 if (retries--)
765                         goto retry;
766         }
767
768         if (sl811->periodic_count == 0 && list_empty(&sl811->async)) 
769                 sofirq_off(sl811);
770         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
771
772         spin_unlock(&sl811->lock);
773
774         return ret;
775 }
776
777 /*-------------------------------------------------------------------------*/
778
779 /* usb 1.1 says max 90% of a frame is available for periodic transfers.
780  * this driver doesn't promise that much since it's got to handle an
781  * IRQ per packet; irq handling latencies also use up that time.
782  */
783 #define MAX_PERIODIC_LOAD       500     /* out of 1000 usec */
784
785 static int balance(struct sl811 *sl811, u16 period, u16 load)
786 {
787         int     i, branch = -ENOSPC;
788
789         /* search for the least loaded schedule branch of that period
790          * which has enough bandwidth left unreserved.
791          */
792         for (i = 0; i < period ; i++) {
793                 if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
794                         int     j;
795
796                         for (j = i; j < PERIODIC_SIZE; j += period) {
797                                 if ((sl811->load[j] + load)
798                                                 > MAX_PERIODIC_LOAD)
799                                         break;
800                         }
801                         if (j < PERIODIC_SIZE)
802                                 continue;
803                         branch = i; 
804                 }
805         }
806         return branch;
807 }
808
809 /*-------------------------------------------------------------------------*/
810
811 static int sl811h_urb_enqueue(
812         struct usb_hcd          *hcd,
813         struct usb_host_endpoint *hep,
814         struct urb              *urb,
815         int                     mem_flags
816 ) {
817         struct sl811            *sl811 = hcd_to_sl811(hcd);
818         struct usb_device       *udev = urb->dev;
819         unsigned int            pipe = urb->pipe;
820         int                     is_out = !usb_pipein(pipe);
821         int                     type = usb_pipetype(pipe);
822         int                     epnum = usb_pipeendpoint(pipe);
823         struct sl811h_ep        *ep = NULL;
824         unsigned long           flags;
825         int                     i;
826         int                     retval = 0;
827
828 #ifdef  DISABLE_ISO
829         if (type == PIPE_ISOCHRONOUS)
830                 return -ENOSPC;
831 #endif
832
833         /* avoid all allocations within spinlocks */
834         if (!hep->hcpriv)
835                 ep = kcalloc(1, sizeof *ep, mem_flags);
836
837         spin_lock_irqsave(&sl811->lock, flags);
838
839         /* don't submit to a dead or disabled port */
840         if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
841                         || !HCD_IS_RUNNING(hcd->state)) {
842                 retval = -ENODEV;
843                 goto fail;
844         }
845
846         if (hep->hcpriv) {
847                 kfree(ep);
848                 ep = hep->hcpriv;
849         } else if (!ep) {
850                 retval = -ENOMEM;
851                 goto fail;
852
853         } else {
854                 INIT_LIST_HEAD(&ep->schedule);
855                 ep->udev = usb_get_dev(udev);
856                 ep->epnum = epnum;
857                 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
858                 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
859                 usb_settoggle(udev, epnum, is_out, 0);
860
861                 if (type == PIPE_CONTROL)
862                         ep->nextpid = USB_PID_SETUP;
863                 else if (is_out)
864                         ep->nextpid = USB_PID_OUT;
865                 else
866                         ep->nextpid = USB_PID_IN;
867
868                 if (ep->maxpacket > H_MAXPACKET) {
869                         /* iso packets up to 240 bytes could work... */
870                         DBG("dev %d ep%d maxpacket %d\n",
871                                 udev->devnum, epnum, ep->maxpacket);
872                         retval = -EINVAL;
873                         goto fail;
874                 }
875
876                 if (udev->speed == USB_SPEED_LOW) {
877                         /* send preamble for external hub? */
878                         if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
879                                 ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
880                 }
881                 switch (type) {
882                 case PIPE_ISOCHRONOUS:
883                 case PIPE_INTERRUPT:
884                         if (urb->interval > PERIODIC_SIZE)
885                                 urb->interval = PERIODIC_SIZE;
886                         ep->period = urb->interval;
887                         ep->branch = PERIODIC_SIZE;
888                         if (type == PIPE_ISOCHRONOUS)
889                                 ep->defctrl |= SL11H_HCTLMASK_ISOCH;
890                         ep->load = usb_calc_bus_time(udev->speed, !is_out,
891                                 (type == PIPE_ISOCHRONOUS),
892                                 usb_maxpacket(udev, pipe, is_out))
893                                         / 1000;
894                         break;
895                 }
896
897                 hep->hcpriv = ep;
898         }
899
900         /* maybe put endpoint into schedule */
901         switch (type) {
902         case PIPE_CONTROL:
903         case PIPE_BULK:
904                 if (list_empty(&ep->schedule))
905                         list_add_tail(&ep->schedule, &sl811->async);
906                 break;
907         case PIPE_ISOCHRONOUS:
908         case PIPE_INTERRUPT:
909                 urb->interval = ep->period;
910                 if (ep->branch < PERIODIC_SIZE)
911                         break;
912
913                 retval = balance(sl811, ep->period, ep->load);
914                 if (retval < 0)
915                         goto fail;
916                 ep->branch = retval;
917                 retval = 0;
918                 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
919                                         + ep->branch;
920
921                 /* sort each schedule branch by period (slow before fast)
922                  * to share the faster parts of the tree without needing
923                  * dummy/placeholder nodes
924                  */
925                 DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
926                 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
927                         struct sl811h_ep        **prev = &sl811->periodic[i];
928                         struct sl811h_ep        *here = *prev;
929
930                         while (here && ep != here) {
931                                 if (ep->period > here->period)
932                                         break;
933                                 prev = &here->next;
934                                 here = *prev;
935                         }
936                         if (ep != here) {
937                                 ep->next = here;
938                                 *prev = ep;
939                         }
940                         sl811->load[i] += ep->load;
941                 }
942                 sl811->periodic_count++;
943                 hcd->self.bandwidth_allocated += ep->load / ep->period;
944                 sofirq_on(sl811);
945         }
946
947         /* in case of unlink-during-submit */
948         spin_lock(&urb->lock);
949         if (urb->status != -EINPROGRESS) {
950                 spin_unlock(&urb->lock);
951                 finish_request(sl811, ep, urb, NULL, 0);
952                 retval = 0;
953                 goto fail;
954         }
955         urb->hcpriv = hep;
956         spin_unlock(&urb->lock);
957
958         start_transfer(sl811);
959         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
960 fail:
961         spin_unlock_irqrestore(&sl811->lock, flags);
962         return retval;
963 }
964
965 static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
966 {
967         struct sl811            *sl811 = hcd_to_sl811(hcd);
968         struct usb_host_endpoint *hep = urb->hcpriv;
969         unsigned long           flags;
970         struct sl811h_ep        *ep;
971         int                     retval = 0;
972
973         if (!hep)
974                 return -EINVAL;
975
976         spin_lock_irqsave(&sl811->lock, flags);
977         ep = hep->hcpriv;
978         if (ep) {
979                 /* finish right away if this urb can't be active ...
980                  * note that some drivers wrongly expect delays
981                  */
982                 if (ep->hep->urb_list.next != &urb->urb_list) {
983                         /* not front of queue?  never active */
984
985                 /* for active transfers, we expect an IRQ */
986                 } else if (sl811->active_a == ep) {
987                         if (time_before_eq(sl811->jiffies_a, jiffies)) {
988                                 /* happens a lot with lowspeed?? */
989                                 DBG("giveup on DONE_A: ctrl %02x sts %02x\n",
990                                         sl811_read(sl811,
991                                                 SL811_EP_A(SL11H_HOSTCTLREG)),
992                                         sl811_read(sl811,
993                                                 SL811_EP_A(SL11H_PKTSTATREG)));
994                                 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
995                                                 0);
996                                 sl811->active_a = NULL;
997                         } else
998                                 urb = NULL;
999 #ifdef  USE_B
1000                 } else if (sl811->active_b == ep) {
1001                         if (time_before_eq(sl811->jiffies_a, jiffies)) {
1002                                 /* happens a lot with lowspeed?? */
1003                                 DBG("giveup on DONE_B: ctrl %02x sts %02x\n",
1004                                         sl811_read(sl811,
1005                                                 SL811_EP_B(SL11H_HOSTCTLREG)),
1006                                         sl811_read(sl811,
1007                                                 SL811_EP_B(SL11H_PKTSTATREG)));
1008                                 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
1009                                                 0);
1010                                 sl811->active_b = NULL;
1011                         } else
1012                                 urb = NULL;
1013 #endif
1014                 } else {
1015                         /* front of queue for inactive endpoint */
1016                 }
1017
1018                 if (urb)
1019                         finish_request(sl811, ep, urb, NULL, 0);
1020                 else
1021                         VDBG("dequeue, urb %p active %s; wait4irq\n", urb,
1022                                 (sl811->active_a == ep) ? "A" : "B");
1023         } else
1024                 retval = -EINVAL;
1025         spin_unlock_irqrestore(&sl811->lock, flags);
1026         return retval;
1027 }
1028
1029 static void
1030 sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1031 {
1032         struct sl811h_ep        *ep = hep->hcpriv;
1033
1034         if (!ep)
1035                 return;
1036
1037         /* assume we'd just wait for the irq */
1038         if (!list_empty(&hep->urb_list))
1039                 msleep(3);
1040         if (!list_empty(&hep->urb_list))
1041                 WARN("ep %p not empty?\n", ep);
1042
1043         usb_put_dev(ep->udev);
1044         kfree(ep);
1045         hep->hcpriv = NULL;
1046 }
1047
1048 static int
1049 sl811h_get_frame(struct usb_hcd *hcd)
1050 {
1051         struct sl811 *sl811 = hcd_to_sl811(hcd);
1052
1053         /* wrong except while periodic transfers are scheduled;
1054          * never matches the on-the-wire frame;
1055          * subject to overruns.
1056          */
1057         return sl811->frame;
1058 }
1059
1060
1061 /*-------------------------------------------------------------------------*/
1062
1063 /* the virtual root hub timer IRQ checks for hub status */
1064 static int
1065 sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
1066 {
1067         struct sl811 *sl811 = hcd_to_sl811(hcd);
1068 #ifdef  QUIRK3
1069         unsigned long flags;
1070
1071         /* non-SMP HACK: use root hub timer as i/o watchdog
1072          * this seems essential when SOF IRQs aren't in use...
1073          */
1074         local_irq_save(flags);
1075         if (!timer_pending(&sl811->timer)) {
1076                 if (sl811h_irq(~0, sl811, NULL) != IRQ_NONE)
1077                         sl811->stat_lost++;
1078         }
1079         local_irq_restore(flags);
1080 #endif
1081
1082         if (!(sl811->port1 & (0xffff << 16)))
1083                 return 0;
1084
1085         /* tell khubd port 1 changed */
1086         *buf = (1 << 1);
1087         return 1;
1088 }
1089
1090 static void
1091 sl811h_hub_descriptor (
1092         struct sl811                    *sl811,
1093         struct usb_hub_descriptor       *desc
1094 ) {
1095         u16             temp = 0;
1096
1097         desc->bDescriptorType = 0x29;
1098         desc->bHubContrCurrent = 0;
1099
1100         desc->bNbrPorts = 1;
1101         desc->bDescLength = 9;
1102
1103         /* per-port power switching (gang of one!), or none */
1104         desc->bPwrOn2PwrGood = 0;
1105         if (sl811->board && sl811->board->port_power) {
1106                 desc->bPwrOn2PwrGood = sl811->board->potpg;
1107                 if (!desc->bPwrOn2PwrGood)
1108                         desc->bPwrOn2PwrGood = 10;
1109                 temp = 0x0001;
1110         } else
1111                 temp = 0x0002;
1112
1113         /* no overcurrent errors detection/handling */
1114         temp |= 0x0010;
1115
1116         desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp);
1117
1118         /* two bitmaps:  ports removable, and legacy PortPwrCtrlMask */
1119         desc->bitmap[0] = 1 << 1;
1120         desc->bitmap[1] = ~0;
1121 }
1122
1123 static void
1124 sl811h_timer(unsigned long _sl811)
1125 {
1126         struct sl811    *sl811 = (void *) _sl811;
1127         unsigned long   flags;
1128         u8              irqstat;
1129         u8              signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
1130         const u32       mask = (1 << USB_PORT_FEAT_CONNECTION)
1131                                 | (1 << USB_PORT_FEAT_ENABLE)
1132                                 | (1 << USB_PORT_FEAT_LOWSPEED);
1133
1134         spin_lock_irqsave(&sl811->lock, flags);
1135
1136         /* stop special signaling */
1137         sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
1138         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1139         udelay(3);
1140
1141         irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);
1142
1143         switch (signaling) {
1144         case SL11H_CTL1MASK_SE0:
1145                 DBG("end reset\n");
1146                 sl811->port1 = (1 << USB_PORT_FEAT_C_RESET)
1147                                 | (1 << USB_PORT_FEAT_POWER);
1148                 sl811->ctrl1 = 0;
1149                 /* don't wrongly ack RD */
1150                 if (irqstat & SL11H_INTMASK_INSRMV)
1151                         irqstat &= ~SL11H_INTMASK_RD;
1152                 break;
1153         case SL11H_CTL1MASK_K:
1154                 DBG("end resume\n");
1155                 sl811->port1 &= ~(1 << USB_PORT_FEAT_SUSPEND);
1156                 break;
1157         default:
1158                 DBG("odd timer signaling: %02x\n", signaling);
1159                 break;
1160         }
1161         sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
1162
1163         if (irqstat & SL11H_INTMASK_RD) {
1164                 /* usbcore nukes all pending transactions on disconnect */
1165                 if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION))
1166                         sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
1167                                         | (1 << USB_PORT_FEAT_C_ENABLE);
1168                 sl811->port1 &= ~mask;
1169                 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1170         } else {
1171                 sl811->port1 |= mask;
1172                 if (irqstat & SL11H_INTMASK_DP)
1173                         sl811->port1 &= ~(1 << USB_PORT_FEAT_LOWSPEED);
1174                 sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
1175         }
1176
1177         if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION)) {
1178                 u8      ctrl2 = SL811HS_CTL2_INIT;
1179
1180                 sl811->irq_enable |= SL11H_INTMASK_DONE_A;
1181 #ifdef USE_B
1182                 sl811->irq_enable |= SL11H_INTMASK_DONE_B;
1183 #endif
1184                 if (sl811->port1 & (1 << USB_PORT_FEAT_LOWSPEED)) {
1185                         sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
1186                         ctrl2 |= SL811HS_CTL2MASK_DSWAP;
1187                 }
1188
1189                 /* start SOFs flowing, kickstarting with A registers */
1190                 sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
1191                 sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
1192                 sl811_write(sl811, SL811HS_CTLREG2, ctrl2);
1193
1194                 /* autoincrementing */
1195                 sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
1196                 writeb(SL_SOF, sl811->data_reg);
1197                 writeb(0, sl811->data_reg);
1198                 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
1199                                 SL11H_HCTLMASK_ARM);
1200
1201                 /* khubd provides debounce delay */
1202         } else {
1203                 sl811->ctrl1 = 0;
1204         }
1205         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1206
1207         /* reenable irqs */
1208         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
1209         spin_unlock_irqrestore(&sl811->lock, flags);
1210 }
1211
1212 static int
1213 sl811h_hub_control(
1214         struct usb_hcd  *hcd,
1215         u16             typeReq,
1216         u16             wValue,
1217         u16             wIndex,
1218         char            *buf,
1219         u16             wLength
1220 ) {
1221         struct sl811    *sl811 = hcd_to_sl811(hcd);
1222         int             retval = 0;
1223         unsigned long   flags;
1224
1225         spin_lock_irqsave(&sl811->lock, flags);
1226
1227         switch (typeReq) {
1228         case ClearHubFeature:
1229         case SetHubFeature:
1230                 switch (wValue) {
1231                 case C_HUB_OVER_CURRENT:
1232                 case C_HUB_LOCAL_POWER:
1233                         break;
1234                 default:
1235                         goto error;
1236                 }
1237                 break;
1238         case ClearPortFeature:
1239                 if (wIndex != 1 || wLength != 0)
1240                         goto error;
1241
1242                 switch (wValue) {
1243                 case USB_PORT_FEAT_ENABLE:
1244                         sl811->port1 &= (1 << USB_PORT_FEAT_POWER);
1245                         sl811->ctrl1 = 0;
1246                         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1247                         sl811->irq_enable = SL11H_INTMASK_INSRMV;
1248                         sl811_write(sl811, SL11H_IRQ_ENABLE,
1249                                                 sl811->irq_enable);
1250                         break;
1251                 case USB_PORT_FEAT_SUSPEND:
1252                         if (!(sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)))
1253                                 break;
1254
1255                         /* 20 msec of resume/K signaling, other irqs blocked */
1256                         DBG("start resume...\n");
1257                         sl811->irq_enable = 0;
1258                         sl811_write(sl811, SL11H_IRQ_ENABLE,
1259                                                 sl811->irq_enable);
1260                         sl811->ctrl1 |= SL11H_CTL1MASK_K;
1261                         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1262
1263                         mod_timer(&sl811->timer, jiffies
1264                                         + msecs_to_jiffies(20));
1265                         break;
1266                 case USB_PORT_FEAT_POWER:
1267                         port_power(sl811, 0);
1268                         break;
1269                 case USB_PORT_FEAT_C_ENABLE:
1270                 case USB_PORT_FEAT_C_SUSPEND:
1271                 case USB_PORT_FEAT_C_CONNECTION:
1272                 case USB_PORT_FEAT_C_OVER_CURRENT:
1273                 case USB_PORT_FEAT_C_RESET:
1274                         break;
1275                 default:
1276                         goto error;
1277                 }
1278                 sl811->port1 &= ~(1 << wValue);
1279                 break;
1280         case GetHubDescriptor:
1281                 sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
1282                 break;
1283         case GetHubStatus:
1284                 *(__le32 *) buf = cpu_to_le32(0);
1285                 break;
1286         case GetPortStatus:
1287                 if (wIndex != 1)
1288                         goto error;
1289                 *(__le32 *) buf = cpu_to_le32(sl811->port1);
1290
1291 #ifndef VERBOSE
1292         if (*(u16*)(buf+2))     /* only if wPortChange is interesting */
1293 #endif
1294                 DBG("GetPortStatus %08x\n", sl811->port1);
1295                 break;
1296         case SetPortFeature:
1297                 if (wIndex != 1 || wLength != 0)
1298                         goto error;
1299                 switch (wValue) {
1300                 case USB_PORT_FEAT_SUSPEND:
1301                         if (sl811->port1 & (1 << USB_PORT_FEAT_RESET))
1302                                 goto error;
1303                         if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE)))
1304                                 goto error;
1305
1306                         DBG("suspend...\n");
1307                         sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
1308                         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1309                         break;
1310                 case USB_PORT_FEAT_POWER:
1311                         port_power(sl811, 1);
1312                         break;
1313                 case USB_PORT_FEAT_RESET:
1314                         if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
1315                                 goto error;
1316                         if (!(sl811->port1 & (1 << USB_PORT_FEAT_POWER)))
1317                                 break;
1318
1319                         /* 50 msec of reset/SE0 signaling, irqs blocked */
1320                         sl811->irq_enable = 0;
1321                         sl811_write(sl811, SL11H_IRQ_ENABLE,
1322                                                 sl811->irq_enable);
1323                         sl811->ctrl1 = SL11H_CTL1MASK_SE0;
1324                         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1325                         sl811->port1 |= (1 << USB_PORT_FEAT_RESET);
1326                         mod_timer(&sl811->timer, jiffies
1327                                         + msecs_to_jiffies(50));
1328                         break;
1329                 default:
1330                         goto error;
1331                 }
1332                 sl811->port1 |= 1 << wValue;
1333                 break;
1334
1335         default:
1336 error:
1337                 /* "protocol stall" on error */
1338                 retval = -EPIPE;
1339         }
1340
1341         spin_unlock_irqrestore(&sl811->lock, flags);
1342         return retval;
1343 }
1344
1345 #ifdef  CONFIG_PM
1346
1347 static int
1348 sl811h_hub_suspend(struct usb_hcd *hcd)
1349 {
1350         // SOFs off
1351         DBG("%s\n", __FUNCTION__);
1352         return 0;
1353 }
1354
1355 static int
1356 sl811h_hub_resume(struct usb_hcd *hcd)
1357 {
1358         // SOFs on
1359         DBG("%s\n", __FUNCTION__);
1360         return 0;
1361 }
1362
1363 #else
1364
1365 #define sl811h_hub_suspend      NULL
1366 #define sl811h_hub_resume       NULL
1367
1368 #endif
1369
1370
1371 /*-------------------------------------------------------------------------*/
1372
1373 #ifdef STUB_DEBUG_FILE
1374
1375 static inline void create_debug_file(struct sl811 *sl811) { }
1376 static inline void remove_debug_file(struct sl811 *sl811) { }
1377
1378 #else
1379
1380 #include <linux/proc_fs.h>
1381 #include <linux/seq_file.h>
1382
1383 static void dump_irq(struct seq_file *s, char *label, u8 mask)
1384 {
1385         seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
1386                 (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
1387                 (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
1388                 (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
1389                 (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
1390                 (mask & SL11H_INTMASK_RD) ? " rd" : "",
1391                 (mask & SL11H_INTMASK_DP) ? " dp" : "");
1392 }
1393
1394 static int proc_sl811h_show(struct seq_file *s, void *unused)
1395 {
1396         struct sl811            *sl811 = s->private;
1397         struct sl811h_ep        *ep;
1398         unsigned                i;
1399
1400         seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
1401                 sl811_to_hcd(sl811)->product_desc,
1402                 hcd_name, DRIVER_VERSION,
1403                 sl811->port1);
1404
1405         seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
1406         seq_printf(s, "current session:  done_a %ld done_b %ld "
1407                         "wake %ld sof %ld overrun %ld lost %ld\n\n",
1408                 sl811->stat_a, sl811->stat_b,
1409                 sl811->stat_wake, sl811->stat_sof,
1410                 sl811->stat_overrun, sl811->stat_lost);
1411
1412         spin_lock_irq(&sl811->lock);
1413
1414         if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
1415                 seq_printf(s, "(suspended)\n\n");
1416         else {
1417                 u8      t = sl811_read(sl811, SL11H_CTLREG1);
1418
1419                 seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
1420                         (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
1421                         ({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
1422                         case SL11H_CTL1MASK_NORMAL: s = ""; break;
1423                         case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
1424                         case SL11H_CTL1MASK_K: s = " k/resume"; break;
1425                         default: s = "j"; break;
1426                         }; s; }),
1427                         (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
1428                         (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");
1429
1430                 dump_irq(s, "irq_enable",
1431                                 sl811_read(sl811, SL11H_IRQ_ENABLE));
1432                 dump_irq(s, "irq_status",
1433                                 sl811_read(sl811, SL11H_IRQ_STATUS));
1434                 seq_printf(s, "frame clocks remaining:  %d\n",
1435                                 sl811_read(sl811, SL11H_SOFTMRREG) << 6);
1436         }
1437
1438         seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
1439                 sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
1440                 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
1441         seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
1442                 sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
1443                 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
1444         seq_printf(s, "\n");
1445         list_for_each_entry (ep, &sl811->async, schedule) {
1446                 struct urb              *urb;
1447
1448                 seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
1449                                         " nak %d err %d\n",
1450                         (ep == sl811->active_a) ? "(A) " : "",
1451                         (ep == sl811->active_b) ? "(B) " : "",
1452                         ep, ep->epnum,
1453                         ({ char *s; switch (ep->nextpid) {
1454                         case USB_PID_IN: s = "in"; break;
1455                         case USB_PID_OUT: s = "out"; break;
1456                         case USB_PID_SETUP: s = "setup"; break;
1457                         case USB_PID_ACK: s = "status"; break;
1458                         default: s = "?"; break;
1459                         }; s;}),
1460                         ep->maxpacket,
1461                         ep->nak_count, ep->error_count);
1462                 list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
1463                         seq_printf(s, "  urb%p, %d/%d\n", urb,
1464                                 urb->actual_length,
1465                                 urb->transfer_buffer_length);
1466                 }
1467         }
1468         if (!list_empty(&sl811->async))
1469                 seq_printf(s, "\n");
1470
1471         seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
1472
1473         for (i = 0; i < PERIODIC_SIZE; i++) {
1474                 ep = sl811->periodic[i];
1475                 if (!ep)
1476                         continue;
1477                 seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);
1478
1479                 /* DUMB: prints shared entries multiple times */
1480                 do {
1481                         seq_printf(s,
1482                                 "   %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
1483                                         "err %d\n",
1484                                 (ep == sl811->active_a) ? "(A) " : "",
1485                                 (ep == sl811->active_b) ? "(B) " : "",
1486                                 ep->period, ep,
1487                                 (ep->udev->speed == USB_SPEED_FULL)
1488                                         ? "" : "ls ",
1489                                 ep->udev->devnum, ep->epnum,
1490                                 (ep->epnum == 0) ? ""
1491                                         : ((ep->nextpid == USB_PID_IN)
1492                                                 ? "in"
1493                                                 : "out"),
1494                                 ep->maxpacket, ep->error_count);
1495                         ep = ep->next;
1496                 } while (ep);
1497         }
1498
1499         spin_unlock_irq(&sl811->lock);
1500         seq_printf(s, "\n");
1501
1502         return 0;
1503 }
1504
1505 static int proc_sl811h_open(struct inode *inode, struct file *file)
1506 {
1507         return single_open(file, proc_sl811h_show, PDE(inode)->data);
1508 }
1509
1510 static struct file_operations proc_ops = {
1511         .open           = proc_sl811h_open,
1512         .read           = seq_read,
1513         .llseek         = seq_lseek,
1514         .release        = single_release,
1515 };
1516
1517 /* expect just one sl811 per system */
1518 static const char proc_filename[] = "driver/sl811h";
1519
1520 static void create_debug_file(struct sl811 *sl811)
1521 {
1522         struct proc_dir_entry *pde;
1523
1524         pde = create_proc_entry(proc_filename, 0, NULL);
1525         if (pde == NULL)
1526                 return;
1527
1528         pde->proc_fops = &proc_ops;
1529         pde->data = sl811;
1530         sl811->pde = pde;
1531 }
1532
1533 static void remove_debug_file(struct sl811 *sl811)
1534 {
1535         if (sl811->pde)
1536                 remove_proc_entry(proc_filename, NULL);
1537 }
1538
1539 #endif
1540
1541 /*-------------------------------------------------------------------------*/
1542
1543 static void
1544 sl811h_stop(struct usb_hcd *hcd)
1545 {
1546         struct sl811    *sl811 = hcd_to_sl811(hcd);
1547         unsigned long   flags;
1548
1549         del_timer_sync(&hcd->rh_timer);
1550
1551         spin_lock_irqsave(&sl811->lock, flags);
1552         port_power(sl811, 0);
1553         spin_unlock_irqrestore(&sl811->lock, flags);
1554 }
1555
1556 static int
1557 sl811h_start(struct usb_hcd *hcd)
1558 {
1559         struct sl811            *sl811 = hcd_to_sl811(hcd);
1560         struct usb_device       *udev;
1561
1562         /* chip has been reset, VBUS power is off */
1563
1564         udev = usb_alloc_dev(NULL, &hcd->self, 0);
1565         if (!udev)
1566                 return -ENOMEM;
1567
1568         udev->speed = USB_SPEED_FULL;
1569         hcd->state = USB_STATE_RUNNING;
1570
1571         if (sl811->board)
1572                 hcd->can_wakeup = sl811->board->can_wakeup;
1573
1574         if (hcd_register_root(udev, hcd) != 0) {
1575                 usb_put_dev(udev);
1576                 sl811h_stop(hcd);
1577                 return -ENODEV;
1578         }
1579
1580         if (sl811->board && sl811->board->power)
1581                 hub_set_power_budget(udev, sl811->board->power * 2);
1582
1583         return 0;
1584 }
1585
1586 /*-------------------------------------------------------------------------*/
1587
1588 static struct hc_driver sl811h_hc_driver = {
1589         .description =          hcd_name,
1590         .hcd_priv_size =        sizeof(struct sl811),
1591
1592         /*
1593          * generic hardware linkage
1594          */
1595         .flags =                HCD_USB11,
1596
1597         /*
1598          * managing i/o requests and associated device resources
1599          */
1600         .urb_enqueue =          sl811h_urb_enqueue,
1601         .urb_dequeue =          sl811h_urb_dequeue,
1602         .endpoint_disable =     sl811h_endpoint_disable,
1603
1604         /*
1605          * periodic schedule support
1606          */
1607         .get_frame_number =     sl811h_get_frame,
1608
1609         /*
1610          * root hub support
1611          */
1612         .hub_status_data =      sl811h_hub_status_data,
1613         .hub_control =          sl811h_hub_control,
1614         .hub_suspend =          sl811h_hub_suspend,
1615         .hub_resume =           sl811h_hub_resume,
1616 };
1617
1618 /*-------------------------------------------------------------------------*/
1619
1620 static int __init_or_module
1621 sl811h_remove(struct device *dev)
1622 {
1623         struct sl811            *sl811 = dev_get_drvdata(dev);
1624         struct usb_hcd          *hcd = sl811_to_hcd(sl811);
1625         struct platform_device  *pdev;
1626         struct resource         *res;
1627
1628         pdev = container_of(dev, struct platform_device, dev);
1629
1630         if (HCD_IS_RUNNING(hcd->state))
1631                 hcd->state = USB_STATE_QUIESCING;
1632
1633         usb_disconnect(&hcd->self.root_hub);
1634         remove_debug_file(sl811);
1635         sl811h_stop(hcd);
1636
1637         usb_deregister_bus(&hcd->self);
1638
1639         free_irq(hcd->irq, hcd);
1640
1641         iounmap(sl811->data_reg);
1642         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1643         release_mem_region(res->start, 1);
1644
1645         iounmap(sl811->addr_reg);
1646         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1647         release_mem_region(res->start, 1);
1648
1649         usb_put_hcd(hcd);
1650         return 0;
1651 }
1652
1653 #define resource_len(r) (((r)->end - (r)->start) + 1)
1654
1655 static int __init
1656 sl811h_probe(struct device *dev)
1657 {
1658         struct usb_hcd          *hcd;
1659         struct sl811            *sl811;
1660         struct platform_device  *pdev;
1661         struct resource         *addr, *data;
1662         int                     irq;
1663         void __iomem            *addr_reg;
1664         void __iomem            *data_reg;
1665         int                     retval;
1666         u8                      tmp;
1667
1668         /* basic sanity checks first.  board-specific init logic should
1669          * have initialized these three resources and probably board
1670          * specific platform_data.  we don't probe for IRQs, and do only
1671          * minimal sanity checking.
1672          */
1673         pdev = container_of(dev, struct platform_device, dev);
1674         if (pdev->num_resources < 3)
1675                 return -ENODEV;
1676
1677         addr = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1678         data = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1679         irq = platform_get_irq(pdev, 0);
1680         if (!addr || !data || irq < 0)
1681                 return -ENODEV;
1682
1683         /* refuse to confuse usbcore */
1684         if (dev->dma_mask) {
1685                 DBG("no we won't dma\n");
1686                 return -EINVAL;
1687         }
1688
1689         if (!request_mem_region(addr->start, 1, hcd_name)) {
1690                 retval = -EBUSY;
1691                 goto err1;
1692         }
1693         addr_reg = ioremap(addr->start, resource_len(addr));
1694         if (addr_reg == NULL) {
1695                 retval = -ENOMEM;
1696                 goto err2;
1697         }
1698
1699         if (!request_mem_region(data->start, 1, hcd_name)) {
1700                 retval = -EBUSY;
1701                 goto err3;
1702         }
1703         data_reg = ioremap(data->start, resource_len(addr));
1704         if (data_reg == NULL) {
1705                 retval = -ENOMEM;
1706                 goto err4;
1707         }
1708
1709         /* allocate and initialize hcd */
1710         hcd = usb_create_hcd(&sl811h_hc_driver);
1711         if (!hcd) {
1712                 retval = 0;
1713                 goto err5;
1714         }
1715         sl811 = hcd_to_sl811(hcd);
1716         dev_set_drvdata(dev, sl811);
1717
1718         hcd->self.controller = dev;
1719         hcd->self.bus_name = dev->bus_id;
1720         hcd->irq = irq;
1721         hcd->regs = addr_reg;
1722
1723         spin_lock_init(&sl811->lock);
1724         INIT_LIST_HEAD(&sl811->async);
1725         sl811->board = dev->platform_data;
1726         init_timer(&sl811->timer);
1727         sl811->timer.function = sl811h_timer;
1728         sl811->timer.data = (unsigned long) sl811;
1729         sl811->addr_reg = addr_reg;
1730         sl811->data_reg = data_reg;
1731
1732         spin_lock_irq(&sl811->lock);
1733         port_power(sl811, 0);
1734         spin_unlock_irq(&sl811->lock);
1735         msleep(200);
1736
1737         tmp = sl811_read(sl811, SL11H_HWREVREG);
1738         switch (tmp >> 4) {
1739         case 1:
1740                 hcd->product_desc = "SL811HS v1.2";
1741                 break;
1742         case 2:
1743                 hcd->product_desc = "SL811HS v1.5";
1744                 break;
1745         default:
1746                 /* reject case 0, SL11S is less functional */
1747                 DBG("chiprev %02x\n", tmp);
1748                 retval = -ENXIO;
1749                 goto err6;
1750         }
1751
1752         /* sl811s would need a different handler for this irq */
1753 #ifdef  CONFIG_ARM
1754         /* Cypress docs say the IRQ is IRQT_HIGH ... */
1755         set_irq_type(irq, IRQT_RISING);
1756 #endif
1757         retval = request_irq(irq, sl811h_irq, SA_INTERRUPT,
1758                         hcd->driver->description, hcd);
1759         if (retval != 0)
1760                 goto err6;
1761
1762         INFO("%s, irq %d\n", hcd->product_desc, irq);
1763
1764         retval = usb_register_bus(&hcd->self);
1765         if (retval < 0)
1766                 goto err7;
1767
1768         retval = sl811h_start(hcd);
1769         if (retval < 0)
1770                 goto err8;
1771
1772         create_debug_file(sl811);
1773         return 0;
1774
1775  err8:
1776         usb_deregister_bus(&hcd->self);
1777  err7:
1778         free_irq(hcd->irq, hcd);
1779  err6:
1780         usb_put_hcd(hcd);
1781  err5:
1782         iounmap(data_reg);
1783  err4:
1784         release_mem_region(data->start, 1);
1785  err3:
1786         iounmap(addr_reg);
1787  err2:
1788         release_mem_region(addr->start, 1);
1789  err1:
1790         DBG("init error, %d\n", retval);
1791         return retval;
1792 }
1793
1794 #ifdef  CONFIG_PM
1795
1796 /* for this device there's no useful distinction between the controller
1797  * and its root hub, except that the root hub only gets direct PM calls 
1798  * when CONFIG_USB_SUSPEND is enabled.
1799  */
1800
1801 static int
1802 sl811h_suspend(struct device *dev, u32 state, u32 phase)
1803 {
1804         struct sl811    *sl811 = dev_get_drvdata(dev);
1805         int             retval = 0;
1806
1807         if (phase != SUSPEND_POWER_DOWN)
1808                 return retval;
1809
1810         if (state <= PM_SUSPEND_MEM)
1811                 retval = sl811h_hub_suspend(sl811_to_hcd(sl811));
1812         else
1813                 port_power(sl811, 0);
1814         if (retval == 0)
1815                 dev->power.power_state = state;
1816         return retval;
1817 }
1818
1819 static int
1820 sl811h_resume(struct device *dev, u32 phase)
1821 {
1822         struct sl811    *sl811 = dev_get_drvdata(dev);
1823
1824         if (phase != RESUME_POWER_ON)
1825                 return 0;
1826
1827         /* with no "check to see if VBUS is still powered" board hook,
1828          * let's assume it'd only be powered to enable remote wakeup.
1829          */
1830         if (dev->power.power_state > PM_SUSPEND_MEM
1831                         || !sl811_to_hcd(sl811)->can_wakeup) {
1832                 sl811->port1 = 0;
1833                 port_power(sl811, 1);
1834                 return 0;
1835         }
1836
1837         dev->power.power_state = PM_SUSPEND_ON;
1838         return sl811h_hub_resume(sl811_to_hcd(sl811));
1839 }
1840
1841 #else
1842
1843 #define sl811h_suspend  NULL
1844 #define sl811h_resume   NULL
1845
1846 #endif
1847
1848
1849 static struct device_driver sl811h_driver = {
1850         .name =         (char *) hcd_name,
1851         .bus =          &platform_bus_type,
1852
1853         .probe =        sl811h_probe,
1854         .remove =       sl811h_remove,
1855
1856         .suspend =      sl811h_suspend,
1857         .resume =       sl811h_resume,
1858 };
1859
1860 /*-------------------------------------------------------------------------*/
1861  
1862 static int __init sl811h_init(void) 
1863 {
1864         if (usb_disabled())
1865                 return -ENODEV;
1866
1867         INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
1868         return driver_register(&sl811h_driver);
1869 }
1870 module_init(sl811h_init);
1871
1872 static void __exit sl811h_cleanup(void) 
1873 {       
1874         driver_unregister(&sl811h_driver);
1875 }
1876 module_exit(sl811h_cleanup);