e90953a9c9fb882a6abaff1f30aabd1c75cec14d
[safe/jmp/linux-2.6] / 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-2005 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/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/kernel.h>
37 #include <linux/delay.h>
38 #include <linux/ioport.h>
39 #include <linux/sched.h>
40 #include <linux/slab.h>
41 #include <linux/errno.h>
42 #include <linux/init.h>
43 #include <linux/timer.h>
44 #include <linux/list.h>
45 #include <linux/interrupt.h>
46 #include <linux/usb.h>
47 #include <linux/usb/sl811.h>
48 #include <linux/platform_device.h>
49
50 #include <asm/io.h>
51 #include <asm/irq.h>
52 #include <asm/system.h>
53 #include <asm/byteorder.h>
54
55 #include "../core/hcd.h"
56 #include "sl811.h"
57
58
59 MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
60 MODULE_LICENSE("GPL");
61
62 #define DRIVER_VERSION  "19 May 2005"
63
64
65 #ifndef DEBUG
66 #       define  STUB_DEBUG_FILE
67 #endif
68
69 /* for now, use only one transfer register bank */
70 #undef  USE_B
71
72 /* this doesn't understand urb->iso_frame_desc[], but if you had a driver
73  * that just queued one ISO frame per URB then iso transfers "should" work
74  * using the normal urb status fields.
75  */
76 #define DISABLE_ISO
77
78 // #define      QUIRK2
79 #define QUIRK3
80
81 static const char hcd_name[] = "sl811-hcd";
82
83 /*-------------------------------------------------------------------------*/
84
85 static void port_power(struct sl811 *sl811, int is_on)
86 {
87         struct usb_hcd  *hcd = sl811_to_hcd(sl811);
88
89         /* hub is inactive unless the port is powered */
90         if (is_on) {
91                 if (sl811->port1 & (1 << USB_PORT_FEAT_POWER))
92                         return;
93
94                 sl811->port1 = (1 << USB_PORT_FEAT_POWER);
95                 sl811->irq_enable = SL11H_INTMASK_INSRMV;
96                 hcd->self.controller->power.power_state = PMSG_ON;
97         } else {
98                 sl811->port1 = 0;
99                 sl811->irq_enable = 0;
100                 hcd->state = HC_STATE_HALT;
101                 hcd->self.controller->power.power_state = PMSG_SUSPEND;
102         }
103         sl811->ctrl1 = 0;
104         sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
105         sl811_write(sl811, SL11H_IRQ_STATUS, ~0);
106
107         if (sl811->board && sl811->board->port_power) {
108                 /* switch VBUS, at 500mA unless hub power budget gets set */
109                 DBG("power %s\n", is_on ? "on" : "off");
110                 sl811->board->port_power(hcd->self.controller, is_on);
111         }
112
113         /* reset as thoroughly as we can */
114         if (sl811->board && sl811->board->reset)
115                 sl811->board->reset(hcd->self.controller);
116         else {
117                 sl811_write(sl811, SL11H_CTLREG1, SL11H_CTL1MASK_SE0);
118                 mdelay(20);
119         }
120
121         sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
122         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
123         sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT);
124         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
125
126         // if !is_on, put into lowpower mode now
127 }
128
129 /*-------------------------------------------------------------------------*/
130
131 /* This is a PIO-only HCD.  Queueing appends URBs to the endpoint's queue,
132  * and may start I/O.  Endpoint queues are scanned during completion irq
133  * handlers (one per packet: ACK, NAK, faults, etc) and urb cancellation.
134  *
135  * Using an external DMA engine to copy a packet at a time could work,
136  * though setup/teardown costs may be too big to make it worthwhile.
137  */
138
139 /* SETUP starts a new control request.  Devices are not allowed to
140  * STALL or NAK these; they must cancel any pending control requests.
141  */
142 static void setup_packet(
143         struct sl811            *sl811,
144         struct sl811h_ep        *ep,
145         struct urb              *urb,
146         u8                      bank,
147         u8                      control
148 )
149 {
150         u8                      addr;
151         u8                      len;
152         void __iomem            *data_reg;
153
154         addr = SL811HS_PACKET_BUF(bank == 0);
155         len = sizeof(struct usb_ctrlrequest);
156         data_reg = sl811->data_reg;
157         sl811_write_buf(sl811, addr, urb->setup_packet, len);
158
159         /* autoincrementing */
160         sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
161         writeb(len, data_reg);
162         writeb(SL_SETUP /* | ep->epnum */, data_reg);
163         writeb(usb_pipedevice(urb->pipe), data_reg);
164
165         /* always OUT/data0 */ ;
166         sl811_write(sl811, bank + SL11H_HOSTCTLREG,
167                         control | SL11H_HCTLMASK_OUT);
168         ep->length = 0;
169         PACKET("SETUP qh%p\n", ep);
170 }
171
172 /* STATUS finishes control requests, often after IN or OUT data packets */
173 static void status_packet(
174         struct sl811            *sl811,
175         struct sl811h_ep        *ep,
176         struct urb              *urb,
177         u8                      bank,
178         u8                      control
179 )
180 {
181         int                     do_out;
182         void __iomem            *data_reg;
183
184         do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe);
185         data_reg = sl811->data_reg;
186
187         /* autoincrementing */
188         sl811_write(sl811, bank + SL11H_BUFADDRREG, 0);
189         writeb(0, data_reg);
190         writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg);
191         writeb(usb_pipedevice(urb->pipe), data_reg);
192
193         /* always data1; sometimes IN */
194         control |= SL11H_HCTLMASK_TOGGLE;
195         if (do_out)
196                 control |= SL11H_HCTLMASK_OUT;
197         sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
198         ep->length = 0;
199         PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "",
200                         do_out ? "out" : "in", ep);
201 }
202
203 /* IN packets can be used with any type of endpoint. here we just
204  * start the transfer, data from the peripheral may arrive later.
205  * urb->iso_frame_desc is currently ignored here...
206  */
207 static void in_packet(
208         struct sl811            *sl811,
209         struct sl811h_ep        *ep,
210         struct urb              *urb,
211         u8                      bank,
212         u8                      control
213 )
214 {
215         u8                      addr;
216         u8                      len;
217         void __iomem            *data_reg;
218
219         /* avoid losing data on overflow */
220         len = ep->maxpacket;
221         addr = SL811HS_PACKET_BUF(bank == 0);
222         if (!(control & SL11H_HCTLMASK_ISOCH)
223                         && usb_gettoggle(urb->dev, ep->epnum, 0))
224                 control |= SL11H_HCTLMASK_TOGGLE;
225         data_reg = sl811->data_reg;
226
227         /* autoincrementing */
228         sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
229         writeb(len, data_reg);
230         writeb(SL_IN | ep->epnum, data_reg);
231         writeb(usb_pipedevice(urb->pipe), data_reg);
232
233         sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
234         ep->length = min((int)len,
235                         urb->transfer_buffer_length - urb->actual_length);
236         PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
237                         !!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len);
238 }
239
240 /* OUT packets can be used with any type of endpoint.
241  * urb->iso_frame_desc is currently ignored here...
242  */
243 static void out_packet(
244         struct sl811            *sl811,
245         struct sl811h_ep        *ep,
246         struct urb              *urb,
247         u8                      bank,
248         u8                      control
249 )
250 {
251         void                    *buf;
252         u8                      addr;
253         u8                      len;
254         void __iomem            *data_reg;
255
256         buf = urb->transfer_buffer + urb->actual_length;
257         prefetch(buf);
258
259         len = min((int)ep->maxpacket,
260                         urb->transfer_buffer_length - urb->actual_length);
261
262         if (!(control & SL11H_HCTLMASK_ISOCH)
263                         && usb_gettoggle(urb->dev, ep->epnum, 1))
264                 control |= SL11H_HCTLMASK_TOGGLE;
265         addr = SL811HS_PACKET_BUF(bank == 0);
266         data_reg = sl811->data_reg;
267
268         sl811_write_buf(sl811, addr, buf, len);
269
270         /* autoincrementing */
271         sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
272         writeb(len, data_reg);
273         writeb(SL_OUT | ep->epnum, data_reg);
274         writeb(usb_pipedevice(urb->pipe), data_reg);
275
276         sl811_write(sl811, bank + SL11H_HOSTCTLREG,
277                         control | SL11H_HCTLMASK_OUT);
278         ep->length = len;
279         PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
280                         !!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len);
281 }
282
283 /*-------------------------------------------------------------------------*/
284
285 /* caller updates on-chip enables later */
286
287 static inline void sofirq_on(struct sl811 *sl811)
288 {
289         if (sl811->irq_enable & SL11H_INTMASK_SOFINTR)
290                 return;
291         VDBG("sof irq on\n");
292         sl811->irq_enable |= SL11H_INTMASK_SOFINTR;
293 }
294
295 static inline void sofirq_off(struct sl811 *sl811)
296 {
297         if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR))
298                 return;
299         VDBG("sof irq off\n");
300         sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR;
301 }
302
303 /*-------------------------------------------------------------------------*/
304
305 /* pick the next endpoint for a transaction, and issue it.
306  * frames start with periodic transfers (after whatever is pending
307  * from the previous frame), and the rest of the time is async
308  * transfers, scheduled round-robin.
309  */
310 static struct sl811h_ep *start(struct sl811 *sl811, u8 bank)
311 {
312         struct sl811h_ep        *ep;
313         struct urb              *urb;
314         int                     fclock;
315         u8                      control;
316
317         /* use endpoint at schedule head */
318         if (sl811->next_periodic) {
319                 ep = sl811->next_periodic;
320                 sl811->next_periodic = ep->next;
321         } else {
322                 if (sl811->next_async)
323                         ep = sl811->next_async;
324                 else if (!list_empty(&sl811->async))
325                         ep = container_of(sl811->async.next,
326                                         struct sl811h_ep, schedule);
327                 else {
328                         /* could set up the first fullspeed periodic
329                          * transfer for the next frame ...
330                          */
331                         return NULL;
332                 }
333
334 #ifdef USE_B
335                 if ((bank && sl811->active_b == ep) || sl811->active_a == ep)
336                         return NULL;
337 #endif
338
339                 if (ep->schedule.next == &sl811->async)
340                         sl811->next_async = NULL;
341                 else
342                         sl811->next_async = container_of(ep->schedule.next,
343                                         struct sl811h_ep, schedule);
344         }
345
346         if (unlikely(list_empty(&ep->hep->urb_list))) {
347                 DBG("empty %p queue?\n", ep);
348                 return NULL;
349         }
350
351         urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
352         control = ep->defctrl;
353
354         /* if this frame doesn't have enough time left to transfer this
355          * packet, wait till the next frame.  too-simple algorithm...
356          */
357         fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6;
358         fclock -= 100;          /* setup takes not much time */
359         if (urb->dev->speed == USB_SPEED_LOW) {
360                 if (control & SL11H_HCTLMASK_PREAMBLE) {
361                         /* also note erratum 1: some hubs won't work */
362                         fclock -= 800;
363                 }
364                 fclock -= ep->maxpacket << 8;
365
366                 /* erratum 2: AFTERSOF only works for fullspeed */
367                 if (fclock < 0) {
368                         if (ep->period)
369                                 sl811->stat_overrun++;
370                         sofirq_on(sl811);
371                         return NULL;
372                 }
373         } else {
374                 fclock -= 12000 / 19;   /* 19 64byte packets/msec */
375                 if (fclock < 0) {
376                         if (ep->period)
377                                 sl811->stat_overrun++;
378                         control |= SL11H_HCTLMASK_AFTERSOF;
379
380                 /* throttle bulk/control irq noise */
381                 } else if (ep->nak_count)
382                         control |= SL11H_HCTLMASK_AFTERSOF;
383         }
384
385
386         switch (ep->nextpid) {
387         case USB_PID_IN:
388                 in_packet(sl811, ep, urb, bank, control);
389                 break;
390         case USB_PID_OUT:
391                 out_packet(sl811, ep, urb, bank, control);
392                 break;
393         case USB_PID_SETUP:
394                 setup_packet(sl811, ep, urb, bank, control);
395                 break;
396         case USB_PID_ACK:               /* for control status */
397                 status_packet(sl811, ep, urb, bank, control);
398                 break;
399         default:
400                 DBG("bad ep%p pid %02x\n", ep, ep->nextpid);
401                 ep = NULL;
402         }
403         return ep;
404 }
405
406 #define MIN_JIFFIES     ((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2)
407
408 static inline void start_transfer(struct sl811 *sl811)
409 {
410         if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
411                 return;
412         if (sl811->active_a == NULL) {
413                 sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF));
414                 if (sl811->active_a != NULL)
415                         sl811->jiffies_a = jiffies + MIN_JIFFIES;
416         }
417 #ifdef USE_B
418         if (sl811->active_b == NULL) {
419                 sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF));
420                 if (sl811->active_b != NULL)
421                         sl811->jiffies_b = jiffies + MIN_JIFFIES;
422         }
423 #endif
424 }
425
426 static void finish_request(
427         struct sl811            *sl811,
428         struct sl811h_ep        *ep,
429         struct urb              *urb,
430         int                     status
431 ) __releases(sl811->lock) __acquires(sl811->lock)
432 {
433         unsigned                i;
434
435         if (usb_pipecontrol(urb->pipe))
436                 ep->nextpid = USB_PID_SETUP;
437
438         spin_lock(&urb->lock);
439         if (urb->status == -EINPROGRESS)
440                 urb->status = status;
441         spin_unlock(&urb->lock);
442
443         usb_hcd_unlink_urb_from_ep(sl811_to_hcd(sl811), urb);
444         spin_unlock(&sl811->lock);
445         usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb);
446         spin_lock(&sl811->lock);
447
448         /* leave active endpoints in the schedule */
449         if (!list_empty(&ep->hep->urb_list))
450                 return;
451
452         /* async deschedule? */
453         if (!list_empty(&ep->schedule)) {
454                 list_del_init(&ep->schedule);
455                 if (ep == sl811->next_async)
456                         sl811->next_async = NULL;
457                 return;
458         }
459
460         /* periodic deschedule */
461         DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
462         for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
463                 struct sl811h_ep        *temp;
464                 struct sl811h_ep        **prev = &sl811->periodic[i];
465
466                 while (*prev && ((temp = *prev) != ep))
467                         prev = &temp->next;
468                 if (*prev)
469                         *prev = ep->next;
470                 sl811->load[i] -= ep->load;
471         }
472         ep->branch = PERIODIC_SIZE;
473         sl811->periodic_count--;
474         sl811_to_hcd(sl811)->self.bandwidth_allocated
475                 -= ep->load / ep->period;
476         if (ep == sl811->next_periodic)
477                 sl811->next_periodic = ep->next;
478
479         /* we might turn SOFs back on again for the async schedule */
480         if (sl811->periodic_count == 0)
481                 sofirq_off(sl811);
482 }
483
484 static void
485 done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank)
486 {
487         u8                      status;
488         struct urb              *urb;
489         int                     urbstat = -EINPROGRESS;
490
491         if (unlikely(!ep))
492                 return;
493
494         status = sl811_read(sl811, bank + SL11H_PKTSTATREG);
495
496         urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
497
498         /* we can safely ignore NAKs */
499         if (status & SL11H_STATMASK_NAK) {
500                 // PACKET("...NAK_%02x qh%p\n", bank, ep);
501                 if (!ep->period)
502                         ep->nak_count++;
503                 ep->error_count = 0;
504
505         /* ACK advances transfer, toggle, and maybe queue */
506         } else if (status & SL11H_STATMASK_ACK) {
507                 struct usb_device       *udev = urb->dev;
508                 int                     len;
509                 unsigned char           *buf;
510
511                 /* urb->iso_frame_desc is currently ignored here... */
512
513                 ep->nak_count = ep->error_count = 0;
514                 switch (ep->nextpid) {
515                 case USB_PID_OUT:
516                         // PACKET("...ACK/out_%02x qh%p\n", bank, ep);
517                         urb->actual_length += ep->length;
518                         usb_dotoggle(udev, ep->epnum, 1);
519                         if (urb->actual_length
520                                         == urb->transfer_buffer_length) {
521                                 if (usb_pipecontrol(urb->pipe))
522                                         ep->nextpid = USB_PID_ACK;
523
524                                 /* some bulk protocols terminate OUT transfers
525                                  * by a short packet, using ZLPs not padding.
526                                  */
527                                 else if (ep->length < ep->maxpacket
528                                                 || !(urb->transfer_flags
529                                                         & URB_ZERO_PACKET))
530                                         urbstat = 0;
531                         }
532                         break;
533                 case USB_PID_IN:
534                         // PACKET("...ACK/in_%02x qh%p\n", bank, ep);
535                         buf = urb->transfer_buffer + urb->actual_length;
536                         prefetchw(buf);
537                         len = ep->maxpacket - sl811_read(sl811,
538                                                 bank + SL11H_XFERCNTREG);
539                         if (len > ep->length) {
540                                 len = ep->length;
541                                 urb->status = -EOVERFLOW;
542                         }
543                         urb->actual_length += len;
544                         sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
545                                         buf, len);
546                         usb_dotoggle(udev, ep->epnum, 0);
547                         if (urb->actual_length == urb->transfer_buffer_length
548                                         || len < ep->maxpacket)
549                                 urbstat = 0;
550                         if (usb_pipecontrol(urb->pipe) && urbstat == 0) {
551
552                                 /* NOTE if the status stage STALLs (why?),
553                                  * this reports the wrong urb status.
554                                  */
555                                 spin_lock(&urb->lock);
556                                 if (urb->status == -EINPROGRESS)
557                                         urb->status = urbstat;
558                                 spin_unlock(&urb->lock);
559
560                                 urb = NULL;
561                                 ep->nextpid = USB_PID_ACK;
562                         }
563                         break;
564                 case USB_PID_SETUP:
565                         // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
566                         if (urb->transfer_buffer_length == urb->actual_length)
567                                 ep->nextpid = USB_PID_ACK;
568                         else if (usb_pipeout(urb->pipe)) {
569                                 usb_settoggle(udev, 0, 1, 1);
570                                 ep->nextpid = USB_PID_OUT;
571                         } else {
572                                 usb_settoggle(udev, 0, 0, 1);
573                                 ep->nextpid = USB_PID_IN;
574                         }
575                         break;
576                 case USB_PID_ACK:
577                         // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
578                         urbstat = 0;
579                         break;
580                 }
581
582         /* STALL stops all transfers */
583         } else if (status & SL11H_STATMASK_STALL) {
584                 PACKET("...STALL_%02x qh%p\n", bank, ep);
585                 ep->nak_count = ep->error_count = 0;
586                 urbstat = -EPIPE;
587
588         /* error? retry, until "3 strikes" */
589         } else if (++ep->error_count >= 3) {
590                 if (status & SL11H_STATMASK_TMOUT)
591                         urbstat = -ETIME;
592                 else if (status & SL11H_STATMASK_OVF)
593                         urbstat = -EOVERFLOW;
594                 else
595                         urbstat = -EPROTO;
596                 ep->error_count = 0;
597                 PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
598                                 bank, status, ep, urbstat);
599         }
600
601         if (urb && (urbstat != -EINPROGRESS || urb->status != -EINPROGRESS))
602                 finish_request(sl811, ep, urb, urbstat);
603 }
604
605 static inline u8 checkdone(struct sl811 *sl811)
606 {
607         u8      ctl;
608         u8      irqstat = 0;
609
610         if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
611                 ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
612                 if (ctl & SL11H_HCTLMASK_ARM)
613                         sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
614                 DBG("%s DONE_A: ctrl %02x sts %02x\n",
615                         (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
616                         ctl,
617                         sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
618                 irqstat |= SL11H_INTMASK_DONE_A;
619         }
620 #ifdef  USE_B
621         if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
622                 ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
623                 if (ctl & SL11H_HCTLMASK_ARM)
624                         sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
625                 DBG("%s DONE_B: ctrl %02x sts %02x\n",
626                         (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
627                         ctl,
628                         sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
629                 irqstat |= SL11H_INTMASK_DONE_A;
630         }
631 #endif
632         return irqstat;
633 }
634
635 static irqreturn_t sl811h_irq(struct usb_hcd *hcd)
636 {
637         struct sl811    *sl811 = hcd_to_sl811(hcd);
638         u8              irqstat;
639         irqreturn_t     ret = IRQ_NONE;
640         unsigned        retries = 5;
641
642         spin_lock(&sl811->lock);
643
644 retry:
645         irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
646         if (irqstat) {
647                 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
648                 irqstat &= sl811->irq_enable;
649         }
650
651 #ifdef  QUIRK2
652         /* this may no longer be necessary ... */
653         if (irqstat == 0) {
654                 irqstat = checkdone(sl811);
655                 if (irqstat)
656                         sl811->stat_lost++;
657         }
658 #endif
659
660         /* USB packets, not necessarily handled in the order they're
661          * issued ... that's fine if they're different endpoints.
662          */
663         if (irqstat & SL11H_INTMASK_DONE_A) {
664                 done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF));
665                 sl811->active_a = NULL;
666                 sl811->stat_a++;
667         }
668 #ifdef USE_B
669         if (irqstat & SL11H_INTMASK_DONE_B) {
670                 done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF));
671                 sl811->active_b = NULL;
672                 sl811->stat_b++;
673         }
674 #endif
675         if (irqstat & SL11H_INTMASK_SOFINTR) {
676                 unsigned index;
677
678                 index = sl811->frame++ % (PERIODIC_SIZE - 1);
679                 sl811->stat_sof++;
680
681                 /* be graceful about almost-inevitable periodic schedule
682                  * overruns:  continue the previous frame's transfers iff
683                  * this one has nothing scheduled.
684                  */
685                 if (sl811->next_periodic) {
686                         // ERR("overrun to slot %d\n", index);
687                         sl811->stat_overrun++;
688                 }
689                 if (sl811->periodic[index])
690                         sl811->next_periodic = sl811->periodic[index];
691         }
692
693         /* khubd manages debouncing and wakeup */
694         if (irqstat & SL11H_INTMASK_INSRMV) {
695                 sl811->stat_insrmv++;
696
697                 /* most stats are reset for each VBUS session */
698                 sl811->stat_wake = 0;
699                 sl811->stat_sof = 0;
700                 sl811->stat_a = 0;
701                 sl811->stat_b = 0;
702                 sl811->stat_lost = 0;
703
704                 sl811->ctrl1 = 0;
705                 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
706
707                 sl811->irq_enable = SL11H_INTMASK_INSRMV;
708                 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
709
710                 /* usbcore nukes other pending transactions on disconnect */
711                 if (sl811->active_a) {
712                         sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
713                         finish_request(sl811, sl811->active_a,
714                                 container_of(sl811->active_a
715                                                 ->hep->urb_list.next,
716                                         struct urb, urb_list),
717                                 -ESHUTDOWN);
718                         sl811->active_a = NULL;
719                 }
720 #ifdef  USE_B
721                 if (sl811->active_b) {
722                         sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
723                         finish_request(sl811, sl811->active_b,
724                                 container_of(sl811->active_b
725                                                 ->hep->urb_list.next,
726                                         struct urb, urb_list),
727                                 NULL, -ESHUTDOWN);
728                         sl811->active_b = NULL;
729                 }
730 #endif
731
732                 /* port status seems weird until after reset, so
733                  * force the reset and make khubd clean up later.
734                  */
735                 sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
736                                 | (1 << USB_PORT_FEAT_CONNECTION);
737
738         } else if (irqstat & SL11H_INTMASK_RD) {
739                 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) {
740                         DBG("wakeup\n");
741                         sl811->port1 |= 1 << USB_PORT_FEAT_C_SUSPEND;
742                         sl811->stat_wake++;
743                 } else
744                         irqstat &= ~SL11H_INTMASK_RD;
745         }
746
747         if (irqstat) {
748                 if (sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
749                         start_transfer(sl811);
750                 ret = IRQ_HANDLED;
751                 if (retries--)
752                         goto retry;
753         }
754
755         if (sl811->periodic_count == 0 && list_empty(&sl811->async))
756                 sofirq_off(sl811);
757         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
758
759         spin_unlock(&sl811->lock);
760
761         return ret;
762 }
763
764 /*-------------------------------------------------------------------------*/
765
766 /* usb 1.1 says max 90% of a frame is available for periodic transfers.
767  * this driver doesn't promise that much since it's got to handle an
768  * IRQ per packet; irq handling latencies also use up that time.
769  *
770  * NOTE:  the periodic schedule is a sparse tree, with the load for
771  * each branch minimized.  see fig 3.5 in the OHCI spec for example.
772  */
773 #define MAX_PERIODIC_LOAD       500     /* out of 1000 usec */
774
775 static int balance(struct sl811 *sl811, u16 period, u16 load)
776 {
777         int     i, branch = -ENOSPC;
778
779         /* search for the least loaded schedule branch of that period
780          * which has enough bandwidth left unreserved.
781          */
782         for (i = 0; i < period ; i++) {
783                 if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
784                         int     j;
785
786                         for (j = i; j < PERIODIC_SIZE; j += period) {
787                                 if ((sl811->load[j] + load)
788                                                 > MAX_PERIODIC_LOAD)
789                                         break;
790                         }
791                         if (j < PERIODIC_SIZE)
792                                 continue;
793                         branch = i;
794                 }
795         }
796         return branch;
797 }
798
799 /*-------------------------------------------------------------------------*/
800
801 static int sl811h_urb_enqueue(
802         struct usb_hcd          *hcd,
803         struct urb              *urb,
804         gfp_t                   mem_flags
805 ) {
806         struct sl811            *sl811 = hcd_to_sl811(hcd);
807         struct usb_device       *udev = urb->dev;
808         unsigned int            pipe = urb->pipe;
809         int                     is_out = !usb_pipein(pipe);
810         int                     type = usb_pipetype(pipe);
811         int                     epnum = usb_pipeendpoint(pipe);
812         struct sl811h_ep        *ep = NULL;
813         unsigned long           flags;
814         int                     i;
815         int                     retval;
816         struct usb_host_endpoint        *hep = urb->ep;
817
818 #ifdef  DISABLE_ISO
819         if (type == PIPE_ISOCHRONOUS)
820                 return -ENOSPC;
821 #endif
822
823         /* avoid all allocations within spinlocks */
824         if (!hep->hcpriv)
825                 ep = kzalloc(sizeof *ep, mem_flags);
826
827         spin_lock_irqsave(&sl811->lock, flags);
828
829         /* don't submit to a dead or disabled port */
830         if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
831                         || !HC_IS_RUNNING(hcd->state)) {
832                 retval = -ENODEV;
833                 kfree(ep);
834                 goto fail_not_linked;
835         }
836         retval = usb_hcd_link_urb_to_ep(hcd, urb);
837         if (retval) {
838                 kfree(ep);
839                 goto fail_not_linked;
840         }
841
842         if (hep->hcpriv) {
843                 kfree(ep);
844                 ep = hep->hcpriv;
845         } else if (!ep) {
846                 retval = -ENOMEM;
847                 goto fail;
848
849         } else {
850                 INIT_LIST_HEAD(&ep->schedule);
851                 ep->udev = udev;
852                 ep->epnum = epnum;
853                 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
854                 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
855                 usb_settoggle(udev, epnum, is_out, 0);
856
857                 if (type == PIPE_CONTROL)
858                         ep->nextpid = USB_PID_SETUP;
859                 else if (is_out)
860                         ep->nextpid = USB_PID_OUT;
861                 else
862                         ep->nextpid = USB_PID_IN;
863
864                 if (ep->maxpacket > H_MAXPACKET) {
865                         /* iso packets up to 240 bytes could work... */
866                         DBG("dev %d ep%d maxpacket %d\n",
867                                 udev->devnum, epnum, ep->maxpacket);
868                         retval = -EINVAL;
869                         goto fail;
870                 }
871
872                 if (udev->speed == USB_SPEED_LOW) {
873                         /* send preamble for external hub? */
874                         if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
875                                 ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
876                 }
877                 switch (type) {
878                 case PIPE_ISOCHRONOUS:
879                 case PIPE_INTERRUPT:
880                         if (urb->interval > PERIODIC_SIZE)
881                                 urb->interval = PERIODIC_SIZE;
882                         ep->period = urb->interval;
883                         ep->branch = PERIODIC_SIZE;
884                         if (type == PIPE_ISOCHRONOUS)
885                                 ep->defctrl |= SL11H_HCTLMASK_ISOCH;
886                         ep->load = usb_calc_bus_time(udev->speed, !is_out,
887                                 (type == PIPE_ISOCHRONOUS),
888                                 usb_maxpacket(udev, pipe, is_out))
889                                         / 1000;
890                         break;
891                 }
892
893                 ep->hep = hep;
894                 hep->hcpriv = ep;
895         }
896
897         /* maybe put endpoint into schedule */
898         switch (type) {
899         case PIPE_CONTROL:
900         case PIPE_BULK:
901                 if (list_empty(&ep->schedule))
902                         list_add_tail(&ep->schedule, &sl811->async);
903                 break;
904         case PIPE_ISOCHRONOUS:
905         case PIPE_INTERRUPT:
906                 urb->interval = ep->period;
907                 if (ep->branch < PERIODIC_SIZE) {
908                         /* NOTE:  the phase is correct here, but the value
909                          * needs offsetting by the transfer queue depth.
910                          * All current drivers ignore start_frame, so this
911                          * is unlikely to ever matter...
912                          */
913                         urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
914                                                 + ep->branch;
915                         break;
916                 }
917
918                 retval = balance(sl811, ep->period, ep->load);
919                 if (retval < 0)
920                         goto fail;
921                 ep->branch = retval;
922                 retval = 0;
923                 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
924                                         + ep->branch;
925
926                 /* sort each schedule branch by period (slow before fast)
927                  * to share the faster parts of the tree without needing
928                  * dummy/placeholder nodes
929                  */
930                 DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
931                 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
932                         struct sl811h_ep        **prev = &sl811->periodic[i];
933                         struct sl811h_ep        *here = *prev;
934
935                         while (here && ep != here) {
936                                 if (ep->period > here->period)
937                                         break;
938                                 prev = &here->next;
939                                 here = *prev;
940                         }
941                         if (ep != here) {
942                                 ep->next = here;
943                                 *prev = ep;
944                         }
945                         sl811->load[i] += ep->load;
946                 }
947                 sl811->periodic_count++;
948                 hcd->self.bandwidth_allocated += ep->load / ep->period;
949                 sofirq_on(sl811);
950         }
951
952         urb->hcpriv = hep;
953         start_transfer(sl811);
954         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
955 fail:
956         if (retval)
957                 usb_hcd_unlink_urb_from_ep(hcd, urb);
958 fail_not_linked:
959         spin_unlock_irqrestore(&sl811->lock, flags);
960         return retval;
961 }
962
963 static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
964 {
965         struct sl811            *sl811 = hcd_to_sl811(hcd);
966         struct usb_host_endpoint *hep;
967         unsigned long           flags;
968         struct sl811h_ep        *ep;
969         int                     retval;
970
971         spin_lock_irqsave(&sl811->lock, flags);
972         retval = usb_hcd_check_unlink_urb(hcd, urb, status);
973         if (retval)
974                 goto fail;
975
976         hep = urb->hcpriv;
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, 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  fail:
1026         spin_unlock_irqrestore(&sl811->lock, flags);
1027         return retval;
1028 }
1029
1030 static void
1031 sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1032 {
1033         struct sl811h_ep        *ep = hep->hcpriv;
1034
1035         if (!ep)
1036                 return;
1037
1038         /* assume we'd just wait for the irq */
1039         if (!list_empty(&hep->urb_list))
1040                 msleep(3);
1041         if (!list_empty(&hep->urb_list))
1042                 WARN("ep %p not empty?\n", ep);
1043
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, */ hcd) != 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] = 0 << 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_bus_suspend(struct usb_hcd *hcd)
1349 {
1350         // SOFs off
1351         DBG("%s\n", __FUNCTION__);
1352         return 0;
1353 }
1354
1355 static int
1356 sl811h_bus_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_bus_suspend      NULL
1366 #define sl811h_bus_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 const 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
1561         /* chip has been reset, VBUS power is off */
1562         hcd->state = HC_STATE_RUNNING;
1563
1564         if (sl811->board) {
1565                 if (!device_can_wakeup(hcd->self.controller))
1566                         device_init_wakeup(hcd->self.controller,
1567                                 sl811->board->can_wakeup);
1568                 hcd->power_budget = sl811->board->power * 2;
1569         }
1570
1571         /* enable power and interupts */
1572         port_power(sl811, 1);
1573
1574         return 0;
1575 }
1576
1577 /*-------------------------------------------------------------------------*/
1578
1579 static struct hc_driver sl811h_hc_driver = {
1580         .description =          hcd_name,
1581         .hcd_priv_size =        sizeof(struct sl811),
1582
1583         /*
1584          * generic hardware linkage
1585          */
1586         .irq =                  sl811h_irq,
1587         .flags =                HCD_USB11 | HCD_MEMORY,
1588
1589         /* Basic lifecycle operations */
1590         .start =                sl811h_start,
1591         .stop =                 sl811h_stop,
1592
1593         /*
1594          * managing i/o requests and associated device resources
1595          */
1596         .urb_enqueue =          sl811h_urb_enqueue,
1597         .urb_dequeue =          sl811h_urb_dequeue,
1598         .endpoint_disable =     sl811h_endpoint_disable,
1599
1600         /*
1601          * periodic schedule support
1602          */
1603         .get_frame_number =     sl811h_get_frame,
1604
1605         /*
1606          * root hub support
1607          */
1608         .hub_status_data =      sl811h_hub_status_data,
1609         .hub_control =          sl811h_hub_control,
1610         .bus_suspend =          sl811h_bus_suspend,
1611         .bus_resume =           sl811h_bus_resume,
1612 };
1613
1614 /*-------------------------------------------------------------------------*/
1615
1616 static int __devexit
1617 sl811h_remove(struct platform_device *dev)
1618 {
1619         struct usb_hcd          *hcd = platform_get_drvdata(dev);
1620         struct sl811            *sl811 = hcd_to_sl811(hcd);
1621         struct resource         *res;
1622
1623         remove_debug_file(sl811);
1624         usb_remove_hcd(hcd);
1625
1626         /* some platforms may use IORESOURCE_IO */
1627         res = platform_get_resource(dev, IORESOURCE_MEM, 1);
1628         if (res)
1629                 iounmap(sl811->data_reg);
1630
1631         res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1632         if (res)
1633                 iounmap(sl811->addr_reg);
1634
1635         usb_put_hcd(hcd);
1636         return 0;
1637 }
1638
1639 static int __devinit
1640 sl811h_probe(struct platform_device *dev)
1641 {
1642         struct usb_hcd          *hcd;
1643         struct sl811            *sl811;
1644         struct resource         *addr, *data;
1645         int                     irq;
1646         void __iomem            *addr_reg;
1647         void __iomem            *data_reg;
1648         int                     retval;
1649         u8                      tmp, ioaddr = 0;
1650
1651         /* basic sanity checks first.  board-specific init logic should
1652          * have initialized these three resources and probably board
1653          * specific platform_data.  we don't probe for IRQs, and do only
1654          * minimal sanity checking.
1655          */
1656         irq = platform_get_irq(dev, 0);
1657         if (dev->num_resources < 3 || irq < 0)
1658                 return -ENODEV;
1659
1660         /* refuse to confuse usbcore */
1661         if (dev->dev.dma_mask) {
1662                 DBG("no we won't dma\n");
1663                 return -EINVAL;
1664         }
1665
1666         /* the chip may be wired for either kind of addressing */
1667         addr = platform_get_resource(dev, IORESOURCE_MEM, 0);
1668         data = platform_get_resource(dev, IORESOURCE_MEM, 1);
1669         retval = -EBUSY;
1670         if (!addr || !data) {
1671                 addr = platform_get_resource(dev, IORESOURCE_IO, 0);
1672                 data = platform_get_resource(dev, IORESOURCE_IO, 1);
1673                 if (!addr || !data)
1674                         return -ENODEV;
1675                 ioaddr = 1;
1676                 /*
1677                  * NOTE: 64-bit resource->start is getting truncated
1678                  * to avoid compiler warning, assuming that ->start
1679                  * is always 32-bit for this case
1680                  */
1681                 addr_reg = (void __iomem *) (unsigned long) addr->start;
1682                 data_reg = (void __iomem *) (unsigned long) data->start;
1683         } else {
1684                 addr_reg = ioremap(addr->start, 1);
1685                 if (addr_reg == NULL) {
1686                         retval = -ENOMEM;
1687                         goto err2;
1688                 }
1689
1690                 data_reg = ioremap(data->start, 1);
1691                 if (data_reg == NULL) {
1692                         retval = -ENOMEM;
1693                         goto err4;
1694                 }
1695         }
1696
1697         /* allocate and initialize hcd */
1698         hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev->dev.bus_id);
1699         if (!hcd) {
1700                 retval = -ENOMEM;
1701                 goto err5;
1702         }
1703         hcd->rsrc_start = addr->start;
1704         sl811 = hcd_to_sl811(hcd);
1705
1706         spin_lock_init(&sl811->lock);
1707         INIT_LIST_HEAD(&sl811->async);
1708         sl811->board = dev->dev.platform_data;
1709         init_timer(&sl811->timer);
1710         sl811->timer.function = sl811h_timer;
1711         sl811->timer.data = (unsigned long) sl811;
1712         sl811->addr_reg = addr_reg;
1713         sl811->data_reg = data_reg;
1714
1715         spin_lock_irq(&sl811->lock);
1716         port_power(sl811, 0);
1717         spin_unlock_irq(&sl811->lock);
1718         msleep(200);
1719
1720         tmp = sl811_read(sl811, SL11H_HWREVREG);
1721         switch (tmp >> 4) {
1722         case 1:
1723                 hcd->product_desc = "SL811HS v1.2";
1724                 break;
1725         case 2:
1726                 hcd->product_desc = "SL811HS v1.5";
1727                 break;
1728         default:
1729                 /* reject case 0, SL11S is less functional */
1730                 DBG("chiprev %02x\n", tmp);
1731                 retval = -ENXIO;
1732                 goto err6;
1733         }
1734
1735         /* The chip's IRQ is level triggered, active high.  A requirement
1736          * for platform device setup is to cope with things like signal
1737          * inverters (e.g. CF is active low) or working only with edge
1738          * triggers (e.g. most ARM CPUs).  Initial driver stress testing
1739          * was on a system with single edge triggering, so most sorts of
1740          * triggering arrangement should work.
1741          */
1742         retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
1743         if (retval != 0)
1744                 goto err6;
1745
1746         create_debug_file(sl811);
1747         return retval;
1748
1749  err6:
1750         usb_put_hcd(hcd);
1751  err5:
1752         if (!ioaddr)
1753                 iounmap(data_reg);
1754  err4:
1755         if (!ioaddr)
1756                 iounmap(addr_reg);
1757  err2:
1758         DBG("init error, %d\n", retval);
1759         return retval;
1760 }
1761
1762 #ifdef  CONFIG_PM
1763
1764 /* for this device there's no useful distinction between the controller
1765  * and its root hub, except that the root hub only gets direct PM calls
1766  * when CONFIG_USB_SUSPEND is enabled.
1767  */
1768
1769 static int
1770 sl811h_suspend(struct platform_device *dev, pm_message_t state)
1771 {
1772         struct usb_hcd  *hcd = platform_get_drvdata(dev);
1773         struct sl811    *sl811 = hcd_to_sl811(hcd);
1774         int             retval = 0;
1775
1776         switch (state.event) {
1777         case PM_EVENT_FREEZE:
1778                 retval = sl811h_bus_suspend(hcd);
1779                 break;
1780         case PM_EVENT_SUSPEND:
1781         case PM_EVENT_PRETHAW:          /* explicitly discard hw state */
1782                 port_power(sl811, 0);
1783                 break;
1784         }
1785         if (retval == 0)
1786                 dev->dev.power.power_state = state;
1787         return retval;
1788 }
1789
1790 static int
1791 sl811h_resume(struct platform_device *dev)
1792 {
1793         struct usb_hcd  *hcd = platform_get_drvdata(dev);
1794         struct sl811    *sl811 = hcd_to_sl811(hcd);
1795
1796         /* with no "check to see if VBUS is still powered" board hook,
1797          * let's assume it'd only be powered to enable remote wakeup.
1798          */
1799         if (dev->dev.power.power_state.event == PM_EVENT_SUSPEND
1800                         || !device_can_wakeup(&hcd->self.root_hub->dev)) {
1801                 sl811->port1 = 0;
1802                 port_power(sl811, 1);
1803                 usb_root_hub_lost_power(hcd->self.root_hub);
1804                 return 0;
1805         }
1806
1807         dev->dev.power.power_state = PMSG_ON;
1808         return sl811h_bus_resume(hcd);
1809 }
1810
1811 #else
1812
1813 #define sl811h_suspend  NULL
1814 #define sl811h_resume   NULL
1815
1816 #endif
1817
1818
1819 /* this driver is exported so sl811_cs can depend on it */
1820 struct platform_driver sl811h_driver = {
1821         .probe =        sl811h_probe,
1822         .remove =       __devexit_p(sl811h_remove),
1823
1824         .suspend =      sl811h_suspend,
1825         .resume =       sl811h_resume,
1826         .driver = {
1827                 .name = (char *) hcd_name,
1828                 .owner = THIS_MODULE,
1829         },
1830 };
1831 EXPORT_SYMBOL(sl811h_driver);
1832
1833 /*-------------------------------------------------------------------------*/
1834
1835 static int __init sl811h_init(void)
1836 {
1837         if (usb_disabled())
1838                 return -ENODEV;
1839
1840         INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
1841         return platform_driver_register(&sl811h_driver);
1842 }
1843 module_init(sl811h_init);
1844
1845 static void __exit sl811h_cleanup(void)
1846 {
1847         platform_driver_unregister(&sl811h_driver);
1848 }
1849 module_exit(sl811h_cleanup);