Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[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-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 #include <asm/unaligned.h>
55
56 #include "../core/hcd.h"
57 #include "sl811.h"
58
59
60 MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
61 MODULE_LICENSE("GPL");
62 MODULE_ALIAS("platform:sl811-hcd");
63
64 #define DRIVER_VERSION  "19 May 2005"
65
66
67 #ifndef DEBUG
68 #       define  STUB_DEBUG_FILE
69 #endif
70
71 /* for now, use only one transfer register bank */
72 #undef  USE_B
73
74 /* this doesn't understand urb->iso_frame_desc[], but if you had a driver
75  * that just queued one ISO frame per URB then iso transfers "should" work
76  * using the normal urb status fields.
77  */
78 #define DISABLE_ISO
79
80 // #define      QUIRK2
81 #define QUIRK3
82
83 static const char hcd_name[] = "sl811-hcd";
84
85 /*-------------------------------------------------------------------------*/
86
87 static void port_power(struct sl811 *sl811, int is_on)
88 {
89         struct usb_hcd  *hcd = sl811_to_hcd(sl811);
90
91         /* hub is inactive unless the port is powered */
92         if (is_on) {
93                 if (sl811->port1 & (1 << USB_PORT_FEAT_POWER))
94                         return;
95
96                 sl811->port1 = (1 << USB_PORT_FEAT_POWER);
97                 sl811->irq_enable = SL11H_INTMASK_INSRMV;
98         } else {
99                 sl811->port1 = 0;
100                 sl811->irq_enable = 0;
101                 hcd->state = HC_STATE_HALT;
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_t(u32, 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_t(u32, 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         usb_hcd_unlink_urb_from_ep(sl811_to_hcd(sl811), urb);
439         spin_unlock(&sl811->lock);
440         usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, status);
441         spin_lock(&sl811->lock);
442
443         /* leave active endpoints in the schedule */
444         if (!list_empty(&ep->hep->urb_list))
445                 return;
446
447         /* async deschedule? */
448         if (!list_empty(&ep->schedule)) {
449                 list_del_init(&ep->schedule);
450                 if (ep == sl811->next_async)
451                         sl811->next_async = NULL;
452                 return;
453         }
454
455         /* periodic deschedule */
456         DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
457         for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
458                 struct sl811h_ep        *temp;
459                 struct sl811h_ep        **prev = &sl811->periodic[i];
460
461                 while (*prev && ((temp = *prev) != ep))
462                         prev = &temp->next;
463                 if (*prev)
464                         *prev = ep->next;
465                 sl811->load[i] -= ep->load;
466         }
467         ep->branch = PERIODIC_SIZE;
468         sl811->periodic_count--;
469         sl811_to_hcd(sl811)->self.bandwidth_allocated
470                 -= ep->load / ep->period;
471         if (ep == sl811->next_periodic)
472                 sl811->next_periodic = ep->next;
473
474         /* we might turn SOFs back on again for the async schedule */
475         if (sl811->periodic_count == 0)
476                 sofirq_off(sl811);
477 }
478
479 static void
480 done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank)
481 {
482         u8                      status;
483         struct urb              *urb;
484         int                     urbstat = -EINPROGRESS;
485
486         if (unlikely(!ep))
487                 return;
488
489         status = sl811_read(sl811, bank + SL11H_PKTSTATREG);
490
491         urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
492
493         /* we can safely ignore NAKs */
494         if (status & SL11H_STATMASK_NAK) {
495                 // PACKET("...NAK_%02x qh%p\n", bank, ep);
496                 if (!ep->period)
497                         ep->nak_count++;
498                 ep->error_count = 0;
499
500         /* ACK advances transfer, toggle, and maybe queue */
501         } else if (status & SL11H_STATMASK_ACK) {
502                 struct usb_device       *udev = urb->dev;
503                 int                     len;
504                 unsigned char           *buf;
505
506                 /* urb->iso_frame_desc is currently ignored here... */
507
508                 ep->nak_count = ep->error_count = 0;
509                 switch (ep->nextpid) {
510                 case USB_PID_OUT:
511                         // PACKET("...ACK/out_%02x qh%p\n", bank, ep);
512                         urb->actual_length += ep->length;
513                         usb_dotoggle(udev, ep->epnum, 1);
514                         if (urb->actual_length
515                                         == urb->transfer_buffer_length) {
516                                 if (usb_pipecontrol(urb->pipe))
517                                         ep->nextpid = USB_PID_ACK;
518
519                                 /* some bulk protocols terminate OUT transfers
520                                  * by a short packet, using ZLPs not padding.
521                                  */
522                                 else if (ep->length < ep->maxpacket
523                                                 || !(urb->transfer_flags
524                                                         & URB_ZERO_PACKET))
525                                         urbstat = 0;
526                         }
527                         break;
528                 case USB_PID_IN:
529                         // PACKET("...ACK/in_%02x qh%p\n", bank, ep);
530                         buf = urb->transfer_buffer + urb->actual_length;
531                         prefetchw(buf);
532                         len = ep->maxpacket - sl811_read(sl811,
533                                                 bank + SL11H_XFERCNTREG);
534                         if (len > ep->length) {
535                                 len = ep->length;
536                                 urbstat = -EOVERFLOW;
537                         }
538                         urb->actual_length += len;
539                         sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
540                                         buf, len);
541                         usb_dotoggle(udev, ep->epnum, 0);
542                         if (urbstat == -EINPROGRESS &&
543                                         (len < ep->maxpacket ||
544                                                 urb->actual_length ==
545                                                 urb->transfer_buffer_length)) {
546                                 if (usb_pipecontrol(urb->pipe))
547                                         ep->nextpid = USB_PID_ACK;
548                                 else
549                                         urbstat = 0;
550                         }
551                         break;
552                 case USB_PID_SETUP:
553                         // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
554                         if (urb->transfer_buffer_length == urb->actual_length)
555                                 ep->nextpid = USB_PID_ACK;
556                         else if (usb_pipeout(urb->pipe)) {
557                                 usb_settoggle(udev, 0, 1, 1);
558                                 ep->nextpid = USB_PID_OUT;
559                         } else {
560                                 usb_settoggle(udev, 0, 0, 1);
561                                 ep->nextpid = USB_PID_IN;
562                         }
563                         break;
564                 case USB_PID_ACK:
565                         // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
566                         urbstat = 0;
567                         break;
568                 }
569
570         /* STALL stops all transfers */
571         } else if (status & SL11H_STATMASK_STALL) {
572                 PACKET("...STALL_%02x qh%p\n", bank, ep);
573                 ep->nak_count = ep->error_count = 0;
574                 urbstat = -EPIPE;
575
576         /* error? retry, until "3 strikes" */
577         } else if (++ep->error_count >= 3) {
578                 if (status & SL11H_STATMASK_TMOUT)
579                         urbstat = -ETIME;
580                 else if (status & SL11H_STATMASK_OVF)
581                         urbstat = -EOVERFLOW;
582                 else
583                         urbstat = -EPROTO;
584                 ep->error_count = 0;
585                 PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
586                                 bank, status, ep, urbstat);
587         }
588
589         if (urbstat != -EINPROGRESS || urb->unlinked)
590                 finish_request(sl811, ep, urb, urbstat);
591 }
592
593 static inline u8 checkdone(struct sl811 *sl811)
594 {
595         u8      ctl;
596         u8      irqstat = 0;
597
598         if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
599                 ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
600                 if (ctl & SL11H_HCTLMASK_ARM)
601                         sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
602                 DBG("%s DONE_A: ctrl %02x sts %02x\n",
603                         (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
604                         ctl,
605                         sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
606                 irqstat |= SL11H_INTMASK_DONE_A;
607         }
608 #ifdef  USE_B
609         if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
610                 ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
611                 if (ctl & SL11H_HCTLMASK_ARM)
612                         sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
613                 DBG("%s DONE_B: ctrl %02x sts %02x\n",
614                         (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
615                         ctl,
616                         sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
617                 irqstat |= SL11H_INTMASK_DONE_A;
618         }
619 #endif
620         return irqstat;
621 }
622
623 static irqreturn_t sl811h_irq(struct usb_hcd *hcd)
624 {
625         struct sl811    *sl811 = hcd_to_sl811(hcd);
626         u8              irqstat;
627         irqreturn_t     ret = IRQ_NONE;
628         unsigned        retries = 5;
629
630         spin_lock(&sl811->lock);
631
632 retry:
633         irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
634         if (irqstat) {
635                 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
636                 irqstat &= sl811->irq_enable;
637         }
638
639 #ifdef  QUIRK2
640         /* this may no longer be necessary ... */
641         if (irqstat == 0) {
642                 irqstat = checkdone(sl811);
643                 if (irqstat)
644                         sl811->stat_lost++;
645         }
646 #endif
647
648         /* USB packets, not necessarily handled in the order they're
649          * issued ... that's fine if they're different endpoints.
650          */
651         if (irqstat & SL11H_INTMASK_DONE_A) {
652                 done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF));
653                 sl811->active_a = NULL;
654                 sl811->stat_a++;
655         }
656 #ifdef USE_B
657         if (irqstat & SL11H_INTMASK_DONE_B) {
658                 done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF));
659                 sl811->active_b = NULL;
660                 sl811->stat_b++;
661         }
662 #endif
663         if (irqstat & SL11H_INTMASK_SOFINTR) {
664                 unsigned index;
665
666                 index = sl811->frame++ % (PERIODIC_SIZE - 1);
667                 sl811->stat_sof++;
668
669                 /* be graceful about almost-inevitable periodic schedule
670                  * overruns:  continue the previous frame's transfers iff
671                  * this one has nothing scheduled.
672                  */
673                 if (sl811->next_periodic) {
674                         // ERR("overrun to slot %d\n", index);
675                         sl811->stat_overrun++;
676                 }
677                 if (sl811->periodic[index])
678                         sl811->next_periodic = sl811->periodic[index];
679         }
680
681         /* khubd manages debouncing and wakeup */
682         if (irqstat & SL11H_INTMASK_INSRMV) {
683                 sl811->stat_insrmv++;
684
685                 /* most stats are reset for each VBUS session */
686                 sl811->stat_wake = 0;
687                 sl811->stat_sof = 0;
688                 sl811->stat_a = 0;
689                 sl811->stat_b = 0;
690                 sl811->stat_lost = 0;
691
692                 sl811->ctrl1 = 0;
693                 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
694
695                 sl811->irq_enable = SL11H_INTMASK_INSRMV;
696                 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
697
698                 /* usbcore nukes other pending transactions on disconnect */
699                 if (sl811->active_a) {
700                         sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
701                         finish_request(sl811, sl811->active_a,
702                                 container_of(sl811->active_a
703                                                 ->hep->urb_list.next,
704                                         struct urb, urb_list),
705                                 -ESHUTDOWN);
706                         sl811->active_a = NULL;
707                 }
708 #ifdef  USE_B
709                 if (sl811->active_b) {
710                         sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
711                         finish_request(sl811, sl811->active_b,
712                                 container_of(sl811->active_b
713                                                 ->hep->urb_list.next,
714                                         struct urb, urb_list),
715                                 NULL, -ESHUTDOWN);
716                         sl811->active_b = NULL;
717                 }
718 #endif
719
720                 /* port status seems weird until after reset, so
721                  * force the reset and make khubd clean up later.
722                  */
723                 if (sl811->stat_insrmv & 1)
724                         sl811->port1 |= 1 << USB_PORT_FEAT_CONNECTION;
725                 else
726                         sl811->port1 &= ~(1 << USB_PORT_FEAT_CONNECTION);
727
728                 sl811->port1 |= 1 << USB_PORT_FEAT_C_CONNECTION;
729
730         } else if (irqstat & SL11H_INTMASK_RD) {
731                 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) {
732                         DBG("wakeup\n");
733                         sl811->port1 |= 1 << USB_PORT_FEAT_C_SUSPEND;
734                         sl811->stat_wake++;
735                 } else
736                         irqstat &= ~SL11H_INTMASK_RD;
737         }
738
739         if (irqstat) {
740                 if (sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
741                         start_transfer(sl811);
742                 ret = IRQ_HANDLED;
743                 if (retries--)
744                         goto retry;
745         }
746
747         if (sl811->periodic_count == 0 && list_empty(&sl811->async))
748                 sofirq_off(sl811);
749         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
750
751         spin_unlock(&sl811->lock);
752
753         return ret;
754 }
755
756 /*-------------------------------------------------------------------------*/
757
758 /* usb 1.1 says max 90% of a frame is available for periodic transfers.
759  * this driver doesn't promise that much since it's got to handle an
760  * IRQ per packet; irq handling latencies also use up that time.
761  *
762  * NOTE:  the periodic schedule is a sparse tree, with the load for
763  * each branch minimized.  see fig 3.5 in the OHCI spec for example.
764  */
765 #define MAX_PERIODIC_LOAD       500     /* out of 1000 usec */
766
767 static int balance(struct sl811 *sl811, u16 period, u16 load)
768 {
769         int     i, branch = -ENOSPC;
770
771         /* search for the least loaded schedule branch of that period
772          * which has enough bandwidth left unreserved.
773          */
774         for (i = 0; i < period ; i++) {
775                 if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
776                         int     j;
777
778                         for (j = i; j < PERIODIC_SIZE; j += period) {
779                                 if ((sl811->load[j] + load)
780                                                 > MAX_PERIODIC_LOAD)
781                                         break;
782                         }
783                         if (j < PERIODIC_SIZE)
784                                 continue;
785                         branch = i;
786                 }
787         }
788         return branch;
789 }
790
791 /*-------------------------------------------------------------------------*/
792
793 static int sl811h_urb_enqueue(
794         struct usb_hcd          *hcd,
795         struct urb              *urb,
796         gfp_t                   mem_flags
797 ) {
798         struct sl811            *sl811 = hcd_to_sl811(hcd);
799         struct usb_device       *udev = urb->dev;
800         unsigned int            pipe = urb->pipe;
801         int                     is_out = !usb_pipein(pipe);
802         int                     type = usb_pipetype(pipe);
803         int                     epnum = usb_pipeendpoint(pipe);
804         struct sl811h_ep        *ep = NULL;
805         unsigned long           flags;
806         int                     i;
807         int                     retval;
808         struct usb_host_endpoint        *hep = urb->ep;
809
810 #ifdef  DISABLE_ISO
811         if (type == PIPE_ISOCHRONOUS)
812                 return -ENOSPC;
813 #endif
814
815         /* avoid all allocations within spinlocks */
816         if (!hep->hcpriv)
817                 ep = kzalloc(sizeof *ep, mem_flags);
818
819         spin_lock_irqsave(&sl811->lock, flags);
820
821         /* don't submit to a dead or disabled port */
822         if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
823                         || !HC_IS_RUNNING(hcd->state)) {
824                 retval = -ENODEV;
825                 kfree(ep);
826                 goto fail_not_linked;
827         }
828         retval = usb_hcd_link_urb_to_ep(hcd, urb);
829         if (retval) {
830                 kfree(ep);
831                 goto fail_not_linked;
832         }
833
834         if (hep->hcpriv) {
835                 kfree(ep);
836                 ep = hep->hcpriv;
837         } else if (!ep) {
838                 retval = -ENOMEM;
839                 goto fail;
840
841         } else {
842                 INIT_LIST_HEAD(&ep->schedule);
843                 ep->udev = udev;
844                 ep->epnum = epnum;
845                 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
846                 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
847                 usb_settoggle(udev, epnum, is_out, 0);
848
849                 if (type == PIPE_CONTROL)
850                         ep->nextpid = USB_PID_SETUP;
851                 else if (is_out)
852                         ep->nextpid = USB_PID_OUT;
853                 else
854                         ep->nextpid = USB_PID_IN;
855
856                 if (ep->maxpacket > H_MAXPACKET) {
857                         /* iso packets up to 240 bytes could work... */
858                         DBG("dev %d ep%d maxpacket %d\n",
859                                 udev->devnum, epnum, ep->maxpacket);
860                         retval = -EINVAL;
861                         goto fail;
862                 }
863
864                 if (udev->speed == USB_SPEED_LOW) {
865                         /* send preamble for external hub? */
866                         if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
867                                 ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
868                 }
869                 switch (type) {
870                 case PIPE_ISOCHRONOUS:
871                 case PIPE_INTERRUPT:
872                         if (urb->interval > PERIODIC_SIZE)
873                                 urb->interval = PERIODIC_SIZE;
874                         ep->period = urb->interval;
875                         ep->branch = PERIODIC_SIZE;
876                         if (type == PIPE_ISOCHRONOUS)
877                                 ep->defctrl |= SL11H_HCTLMASK_ISOCH;
878                         ep->load = usb_calc_bus_time(udev->speed, !is_out,
879                                 (type == PIPE_ISOCHRONOUS),
880                                 usb_maxpacket(udev, pipe, is_out))
881                                         / 1000;
882                         break;
883                 }
884
885                 ep->hep = hep;
886                 hep->hcpriv = ep;
887         }
888
889         /* maybe put endpoint into schedule */
890         switch (type) {
891         case PIPE_CONTROL:
892         case PIPE_BULK:
893                 if (list_empty(&ep->schedule))
894                         list_add_tail(&ep->schedule, &sl811->async);
895                 break;
896         case PIPE_ISOCHRONOUS:
897         case PIPE_INTERRUPT:
898                 urb->interval = ep->period;
899                 if (ep->branch < PERIODIC_SIZE) {
900                         /* NOTE:  the phase is correct here, but the value
901                          * needs offsetting by the transfer queue depth.
902                          * All current drivers ignore start_frame, so this
903                          * is unlikely to ever matter...
904                          */
905                         urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
906                                                 + ep->branch;
907                         break;
908                 }
909
910                 retval = balance(sl811, ep->period, ep->load);
911                 if (retval < 0)
912                         goto fail;
913                 ep->branch = retval;
914                 retval = 0;
915                 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
916                                         + ep->branch;
917
918                 /* sort each schedule branch by period (slow before fast)
919                  * to share the faster parts of the tree without needing
920                  * dummy/placeholder nodes
921                  */
922                 DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
923                 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
924                         struct sl811h_ep        **prev = &sl811->periodic[i];
925                         struct sl811h_ep        *here = *prev;
926
927                         while (here && ep != here) {
928                                 if (ep->period > here->period)
929                                         break;
930                                 prev = &here->next;
931                                 here = *prev;
932                         }
933                         if (ep != here) {
934                                 ep->next = here;
935                                 *prev = ep;
936                         }
937                         sl811->load[i] += ep->load;
938                 }
939                 sl811->periodic_count++;
940                 hcd->self.bandwidth_allocated += ep->load / ep->period;
941                 sofirq_on(sl811);
942         }
943
944         urb->hcpriv = hep;
945         start_transfer(sl811);
946         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
947 fail:
948         if (retval)
949                 usb_hcd_unlink_urb_from_ep(hcd, urb);
950 fail_not_linked:
951         spin_unlock_irqrestore(&sl811->lock, flags);
952         return retval;
953 }
954
955 static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
956 {
957         struct sl811            *sl811 = hcd_to_sl811(hcd);
958         struct usb_host_endpoint *hep;
959         unsigned long           flags;
960         struct sl811h_ep        *ep;
961         int                     retval;
962
963         spin_lock_irqsave(&sl811->lock, flags);
964         retval = usb_hcd_check_unlink_urb(hcd, urb, status);
965         if (retval)
966                 goto fail;
967
968         hep = urb->hcpriv;
969         ep = hep->hcpriv;
970         if (ep) {
971                 /* finish right away if this urb can't be active ...
972                  * note that some drivers wrongly expect delays
973                  */
974                 if (ep->hep->urb_list.next != &urb->urb_list) {
975                         /* not front of queue?  never active */
976
977                 /* for active transfers, we expect an IRQ */
978                 } else if (sl811->active_a == ep) {
979                         if (time_before_eq(sl811->jiffies_a, jiffies)) {
980                                 /* happens a lot with lowspeed?? */
981                                 DBG("giveup on DONE_A: ctrl %02x sts %02x\n",
982                                         sl811_read(sl811,
983                                                 SL811_EP_A(SL11H_HOSTCTLREG)),
984                                         sl811_read(sl811,
985                                                 SL811_EP_A(SL11H_PKTSTATREG)));
986                                 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
987                                                 0);
988                                 sl811->active_a = NULL;
989                         } else
990                                 urb = NULL;
991 #ifdef  USE_B
992                 } else if (sl811->active_b == ep) {
993                         if (time_before_eq(sl811->jiffies_a, jiffies)) {
994                                 /* happens a lot with lowspeed?? */
995                                 DBG("giveup on DONE_B: ctrl %02x sts %02x\n",
996                                         sl811_read(sl811,
997                                                 SL811_EP_B(SL11H_HOSTCTLREG)),
998                                         sl811_read(sl811,
999                                                 SL811_EP_B(SL11H_PKTSTATREG)));
1000                                 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
1001                                                 0);
1002                                 sl811->active_b = NULL;
1003                         } else
1004                                 urb = NULL;
1005 #endif
1006                 } else {
1007                         /* front of queue for inactive endpoint */
1008                 }
1009
1010                 if (urb)
1011                         finish_request(sl811, ep, urb, 0);
1012                 else
1013                         VDBG("dequeue, urb %p active %s; wait4irq\n", urb,
1014                                 (sl811->active_a == ep) ? "A" : "B");
1015         } else
1016                 retval = -EINVAL;
1017  fail:
1018         spin_unlock_irqrestore(&sl811->lock, flags);
1019         return retval;
1020 }
1021
1022 static void
1023 sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1024 {
1025         struct sl811h_ep        *ep = hep->hcpriv;
1026
1027         if (!ep)
1028                 return;
1029
1030         /* assume we'd just wait for the irq */
1031         if (!list_empty(&hep->urb_list))
1032                 msleep(3);
1033         if (!list_empty(&hep->urb_list))
1034                 WARNING("ep %p not empty?\n", ep);
1035
1036         kfree(ep);
1037         hep->hcpriv = NULL;
1038 }
1039
1040 static int
1041 sl811h_get_frame(struct usb_hcd *hcd)
1042 {
1043         struct sl811 *sl811 = hcd_to_sl811(hcd);
1044
1045         /* wrong except while periodic transfers are scheduled;
1046          * never matches the on-the-wire frame;
1047          * subject to overruns.
1048          */
1049         return sl811->frame;
1050 }
1051
1052
1053 /*-------------------------------------------------------------------------*/
1054
1055 /* the virtual root hub timer IRQ checks for hub status */
1056 static int
1057 sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
1058 {
1059         struct sl811 *sl811 = hcd_to_sl811(hcd);
1060 #ifdef  QUIRK3
1061         unsigned long flags;
1062
1063         /* non-SMP HACK: use root hub timer as i/o watchdog
1064          * this seems essential when SOF IRQs aren't in use...
1065          */
1066         local_irq_save(flags);
1067         if (!timer_pending(&sl811->timer)) {
1068                 if (sl811h_irq( /* ~0, */ hcd) != IRQ_NONE)
1069                         sl811->stat_lost++;
1070         }
1071         local_irq_restore(flags);
1072 #endif
1073
1074         if (!(sl811->port1 & (0xffff << 16)))
1075                 return 0;
1076
1077         /* tell khubd port 1 changed */
1078         *buf = (1 << 1);
1079         return 1;
1080 }
1081
1082 static void
1083 sl811h_hub_descriptor (
1084         struct sl811                    *sl811,
1085         struct usb_hub_descriptor       *desc
1086 ) {
1087         u16             temp = 0;
1088
1089         desc->bDescriptorType = 0x29;
1090         desc->bHubContrCurrent = 0;
1091
1092         desc->bNbrPorts = 1;
1093         desc->bDescLength = 9;
1094
1095         /* per-port power switching (gang of one!), or none */
1096         desc->bPwrOn2PwrGood = 0;
1097         if (sl811->board && sl811->board->port_power) {
1098                 desc->bPwrOn2PwrGood = sl811->board->potpg;
1099                 if (!desc->bPwrOn2PwrGood)
1100                         desc->bPwrOn2PwrGood = 10;
1101                 temp = 0x0001;
1102         } else
1103                 temp = 0x0002;
1104
1105         /* no overcurrent errors detection/handling */
1106         temp |= 0x0010;
1107
1108         desc->wHubCharacteristics = cpu_to_le16(temp);
1109
1110         /* two bitmaps:  ports removable, and legacy PortPwrCtrlMask */
1111         desc->bitmap[0] = 0 << 1;
1112         desc->bitmap[1] = ~0;
1113 }
1114
1115 static void
1116 sl811h_timer(unsigned long _sl811)
1117 {
1118         struct sl811    *sl811 = (void *) _sl811;
1119         unsigned long   flags;
1120         u8              irqstat;
1121         u8              signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
1122         const u32       mask = (1 << USB_PORT_FEAT_CONNECTION)
1123                                 | (1 << USB_PORT_FEAT_ENABLE)
1124                                 | (1 << USB_PORT_FEAT_LOWSPEED);
1125
1126         spin_lock_irqsave(&sl811->lock, flags);
1127
1128         /* stop special signaling */
1129         sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
1130         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1131         udelay(3);
1132
1133         irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);
1134
1135         switch (signaling) {
1136         case SL11H_CTL1MASK_SE0:
1137                 DBG("end reset\n");
1138                 sl811->port1 = (1 << USB_PORT_FEAT_C_RESET)
1139                                 | (1 << USB_PORT_FEAT_POWER);
1140                 sl811->ctrl1 = 0;
1141                 /* don't wrongly ack RD */
1142                 if (irqstat & SL11H_INTMASK_INSRMV)
1143                         irqstat &= ~SL11H_INTMASK_RD;
1144                 break;
1145         case SL11H_CTL1MASK_K:
1146                 DBG("end resume\n");
1147                 sl811->port1 &= ~(1 << USB_PORT_FEAT_SUSPEND);
1148                 break;
1149         default:
1150                 DBG("odd timer signaling: %02x\n", signaling);
1151                 break;
1152         }
1153         sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
1154
1155         if (irqstat & SL11H_INTMASK_RD) {
1156                 /* usbcore nukes all pending transactions on disconnect */
1157                 if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION))
1158                         sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
1159                                         | (1 << USB_PORT_FEAT_C_ENABLE);
1160                 sl811->port1 &= ~mask;
1161                 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1162         } else {
1163                 sl811->port1 |= mask;
1164                 if (irqstat & SL11H_INTMASK_DP)
1165                         sl811->port1 &= ~(1 << USB_PORT_FEAT_LOWSPEED);
1166                 sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
1167         }
1168
1169         if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION)) {
1170                 u8      ctrl2 = SL811HS_CTL2_INIT;
1171
1172                 sl811->irq_enable |= SL11H_INTMASK_DONE_A;
1173 #ifdef USE_B
1174                 sl811->irq_enable |= SL11H_INTMASK_DONE_B;
1175 #endif
1176                 if (sl811->port1 & (1 << USB_PORT_FEAT_LOWSPEED)) {
1177                         sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
1178                         ctrl2 |= SL811HS_CTL2MASK_DSWAP;
1179                 }
1180
1181                 /* start SOFs flowing, kickstarting with A registers */
1182                 sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
1183                 sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
1184                 sl811_write(sl811, SL811HS_CTLREG2, ctrl2);
1185
1186                 /* autoincrementing */
1187                 sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
1188                 writeb(SL_SOF, sl811->data_reg);
1189                 writeb(0, sl811->data_reg);
1190                 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
1191                                 SL11H_HCTLMASK_ARM);
1192
1193                 /* khubd provides debounce delay */
1194         } else {
1195                 sl811->ctrl1 = 0;
1196         }
1197         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1198
1199         /* reenable irqs */
1200         sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
1201         spin_unlock_irqrestore(&sl811->lock, flags);
1202 }
1203
1204 static int
1205 sl811h_hub_control(
1206         struct usb_hcd  *hcd,
1207         u16             typeReq,
1208         u16             wValue,
1209         u16             wIndex,
1210         char            *buf,
1211         u16             wLength
1212 ) {
1213         struct sl811    *sl811 = hcd_to_sl811(hcd);
1214         int             retval = 0;
1215         unsigned long   flags;
1216
1217         spin_lock_irqsave(&sl811->lock, flags);
1218
1219         switch (typeReq) {
1220         case ClearHubFeature:
1221         case SetHubFeature:
1222                 switch (wValue) {
1223                 case C_HUB_OVER_CURRENT:
1224                 case C_HUB_LOCAL_POWER:
1225                         break;
1226                 default:
1227                         goto error;
1228                 }
1229                 break;
1230         case ClearPortFeature:
1231                 if (wIndex != 1 || wLength != 0)
1232                         goto error;
1233
1234                 switch (wValue) {
1235                 case USB_PORT_FEAT_ENABLE:
1236                         sl811->port1 &= (1 << USB_PORT_FEAT_POWER);
1237                         sl811->ctrl1 = 0;
1238                         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1239                         sl811->irq_enable = SL11H_INTMASK_INSRMV;
1240                         sl811_write(sl811, SL11H_IRQ_ENABLE,
1241                                                 sl811->irq_enable);
1242                         break;
1243                 case USB_PORT_FEAT_SUSPEND:
1244                         if (!(sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)))
1245                                 break;
1246
1247                         /* 20 msec of resume/K signaling, other irqs blocked */
1248                         DBG("start resume...\n");
1249                         sl811->irq_enable = 0;
1250                         sl811_write(sl811, SL11H_IRQ_ENABLE,
1251                                                 sl811->irq_enable);
1252                         sl811->ctrl1 |= SL11H_CTL1MASK_K;
1253                         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1254
1255                         mod_timer(&sl811->timer, jiffies
1256                                         + msecs_to_jiffies(20));
1257                         break;
1258                 case USB_PORT_FEAT_POWER:
1259                         port_power(sl811, 0);
1260                         break;
1261                 case USB_PORT_FEAT_C_ENABLE:
1262                 case USB_PORT_FEAT_C_SUSPEND:
1263                 case USB_PORT_FEAT_C_CONNECTION:
1264                 case USB_PORT_FEAT_C_OVER_CURRENT:
1265                 case USB_PORT_FEAT_C_RESET:
1266                         break;
1267                 default:
1268                         goto error;
1269                 }
1270                 sl811->port1 &= ~(1 << wValue);
1271                 break;
1272         case GetHubDescriptor:
1273                 sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
1274                 break;
1275         case GetHubStatus:
1276                 put_unaligned_le32(0, buf);
1277                 break;
1278         case GetPortStatus:
1279                 if (wIndex != 1)
1280                         goto error;
1281                 put_unaligned_le32(sl811->port1, buf);
1282
1283 #ifndef VERBOSE
1284         if (*(u16*)(buf+2))     /* only if wPortChange is interesting */
1285 #endif
1286                 DBG("GetPortStatus %08x\n", sl811->port1);
1287                 break;
1288         case SetPortFeature:
1289                 if (wIndex != 1 || wLength != 0)
1290                         goto error;
1291                 switch (wValue) {
1292                 case USB_PORT_FEAT_SUSPEND:
1293                         if (sl811->port1 & (1 << USB_PORT_FEAT_RESET))
1294                                 goto error;
1295                         if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE)))
1296                                 goto error;
1297
1298                         DBG("suspend...\n");
1299                         sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
1300                         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1301                         break;
1302                 case USB_PORT_FEAT_POWER:
1303                         port_power(sl811, 1);
1304                         break;
1305                 case USB_PORT_FEAT_RESET:
1306                         if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
1307                                 goto error;
1308                         if (!(sl811->port1 & (1 << USB_PORT_FEAT_POWER)))
1309                                 break;
1310
1311                         /* 50 msec of reset/SE0 signaling, irqs blocked */
1312                         sl811->irq_enable = 0;
1313                         sl811_write(sl811, SL11H_IRQ_ENABLE,
1314                                                 sl811->irq_enable);
1315                         sl811->ctrl1 = SL11H_CTL1MASK_SE0;
1316                         sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1317                         sl811->port1 |= (1 << USB_PORT_FEAT_RESET);
1318                         mod_timer(&sl811->timer, jiffies
1319                                         + msecs_to_jiffies(50));
1320                         break;
1321                 default:
1322                         goto error;
1323                 }
1324                 sl811->port1 |= 1 << wValue;
1325                 break;
1326
1327         default:
1328 error:
1329                 /* "protocol stall" on error */
1330                 retval = -EPIPE;
1331         }
1332
1333         spin_unlock_irqrestore(&sl811->lock, flags);
1334         return retval;
1335 }
1336
1337 #ifdef  CONFIG_PM
1338
1339 static int
1340 sl811h_bus_suspend(struct usb_hcd *hcd)
1341 {
1342         // SOFs off
1343         DBG("%s\n", __func__);
1344         return 0;
1345 }
1346
1347 static int
1348 sl811h_bus_resume(struct usb_hcd *hcd)
1349 {
1350         // SOFs on
1351         DBG("%s\n", __func__);
1352         return 0;
1353 }
1354
1355 #else
1356
1357 #define sl811h_bus_suspend      NULL
1358 #define sl811h_bus_resume       NULL
1359
1360 #endif
1361
1362
1363 /*-------------------------------------------------------------------------*/
1364
1365 #ifdef STUB_DEBUG_FILE
1366
1367 static inline void create_debug_file(struct sl811 *sl811) { }
1368 static inline void remove_debug_file(struct sl811 *sl811) { }
1369
1370 #else
1371
1372 #include <linux/proc_fs.h>
1373 #include <linux/seq_file.h>
1374
1375 static void dump_irq(struct seq_file *s, char *label, u8 mask)
1376 {
1377         seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
1378                 (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
1379                 (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
1380                 (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
1381                 (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
1382                 (mask & SL11H_INTMASK_RD) ? " rd" : "",
1383                 (mask & SL11H_INTMASK_DP) ? " dp" : "");
1384 }
1385
1386 static int proc_sl811h_show(struct seq_file *s, void *unused)
1387 {
1388         struct sl811            *sl811 = s->private;
1389         struct sl811h_ep        *ep;
1390         unsigned                i;
1391
1392         seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
1393                 sl811_to_hcd(sl811)->product_desc,
1394                 hcd_name, DRIVER_VERSION,
1395                 sl811->port1);
1396
1397         seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
1398         seq_printf(s, "current session:  done_a %ld done_b %ld "
1399                         "wake %ld sof %ld overrun %ld lost %ld\n\n",
1400                 sl811->stat_a, sl811->stat_b,
1401                 sl811->stat_wake, sl811->stat_sof,
1402                 sl811->stat_overrun, sl811->stat_lost);
1403
1404         spin_lock_irq(&sl811->lock);
1405
1406         if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
1407                 seq_printf(s, "(suspended)\n\n");
1408         else {
1409                 u8      t = sl811_read(sl811, SL11H_CTLREG1);
1410
1411                 seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
1412                         (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
1413                         ({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
1414                         case SL11H_CTL1MASK_NORMAL: s = ""; break;
1415                         case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
1416                         case SL11H_CTL1MASK_K: s = " k/resume"; break;
1417                         default: s = "j"; break;
1418                         }; s; }),
1419                         (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
1420                         (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");
1421
1422                 dump_irq(s, "irq_enable",
1423                                 sl811_read(sl811, SL11H_IRQ_ENABLE));
1424                 dump_irq(s, "irq_status",
1425                                 sl811_read(sl811, SL11H_IRQ_STATUS));
1426                 seq_printf(s, "frame clocks remaining:  %d\n",
1427                                 sl811_read(sl811, SL11H_SOFTMRREG) << 6);
1428         }
1429
1430         seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
1431                 sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
1432                 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
1433         seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
1434                 sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
1435                 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
1436         seq_printf(s, "\n");
1437         list_for_each_entry (ep, &sl811->async, schedule) {
1438                 struct urb              *urb;
1439
1440                 seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
1441                                         " nak %d err %d\n",
1442                         (ep == sl811->active_a) ? "(A) " : "",
1443                         (ep == sl811->active_b) ? "(B) " : "",
1444                         ep, ep->epnum,
1445                         ({ char *s; switch (ep->nextpid) {
1446                         case USB_PID_IN: s = "in"; break;
1447                         case USB_PID_OUT: s = "out"; break;
1448                         case USB_PID_SETUP: s = "setup"; break;
1449                         case USB_PID_ACK: s = "status"; break;
1450                         default: s = "?"; break;
1451                         }; s;}),
1452                         ep->maxpacket,
1453                         ep->nak_count, ep->error_count);
1454                 list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
1455                         seq_printf(s, "  urb%p, %d/%d\n", urb,
1456                                 urb->actual_length,
1457                                 urb->transfer_buffer_length);
1458                 }
1459         }
1460         if (!list_empty(&sl811->async))
1461                 seq_printf(s, "\n");
1462
1463         seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
1464
1465         for (i = 0; i < PERIODIC_SIZE; i++) {
1466                 ep = sl811->periodic[i];
1467                 if (!ep)
1468                         continue;
1469                 seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);
1470
1471                 /* DUMB: prints shared entries multiple times */
1472                 do {
1473                         seq_printf(s,
1474                                 "   %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
1475                                         "err %d\n",
1476                                 (ep == sl811->active_a) ? "(A) " : "",
1477                                 (ep == sl811->active_b) ? "(B) " : "",
1478                                 ep->period, ep,
1479                                 (ep->udev->speed == USB_SPEED_FULL)
1480                                         ? "" : "ls ",
1481                                 ep->udev->devnum, ep->epnum,
1482                                 (ep->epnum == 0) ? ""
1483                                         : ((ep->nextpid == USB_PID_IN)
1484                                                 ? "in"
1485                                                 : "out"),
1486                                 ep->maxpacket, ep->error_count);
1487                         ep = ep->next;
1488                 } while (ep);
1489         }
1490
1491         spin_unlock_irq(&sl811->lock);
1492         seq_printf(s, "\n");
1493
1494         return 0;
1495 }
1496
1497 static int proc_sl811h_open(struct inode *inode, struct file *file)
1498 {
1499         return single_open(file, proc_sl811h_show, PDE(inode)->data);
1500 }
1501
1502 static const struct file_operations proc_ops = {
1503         .open           = proc_sl811h_open,
1504         .read           = seq_read,
1505         .llseek         = seq_lseek,
1506         .release        = single_release,
1507 };
1508
1509 /* expect just one sl811 per system */
1510 static const char proc_filename[] = "driver/sl811h";
1511
1512 static void create_debug_file(struct sl811 *sl811)
1513 {
1514         sl811->pde = proc_create_data(proc_filename, 0, NULL, &proc_ops, sl811);
1515 }
1516
1517 static void remove_debug_file(struct sl811 *sl811)
1518 {
1519         if (sl811->pde)
1520                 remove_proc_entry(proc_filename, NULL);
1521 }
1522
1523 #endif
1524
1525 /*-------------------------------------------------------------------------*/
1526
1527 static void
1528 sl811h_stop(struct usb_hcd *hcd)
1529 {
1530         struct sl811    *sl811 = hcd_to_sl811(hcd);
1531         unsigned long   flags;
1532
1533         del_timer_sync(&hcd->rh_timer);
1534
1535         spin_lock_irqsave(&sl811->lock, flags);
1536         port_power(sl811, 0);
1537         spin_unlock_irqrestore(&sl811->lock, flags);
1538 }
1539
1540 static int
1541 sl811h_start(struct usb_hcd *hcd)
1542 {
1543         struct sl811            *sl811 = hcd_to_sl811(hcd);
1544
1545         /* chip has been reset, VBUS power is off */
1546         hcd->state = HC_STATE_RUNNING;
1547
1548         if (sl811->board) {
1549                 if (!device_can_wakeup(hcd->self.controller))
1550                         device_init_wakeup(hcd->self.controller,
1551                                 sl811->board->can_wakeup);
1552                 hcd->power_budget = sl811->board->power * 2;
1553         }
1554
1555         /* enable power and interrupts */
1556         port_power(sl811, 1);
1557
1558         return 0;
1559 }
1560
1561 /*-------------------------------------------------------------------------*/
1562
1563 static struct hc_driver sl811h_hc_driver = {
1564         .description =          hcd_name,
1565         .hcd_priv_size =        sizeof(struct sl811),
1566
1567         /*
1568          * generic hardware linkage
1569          */
1570         .irq =                  sl811h_irq,
1571         .flags =                HCD_USB11 | HCD_MEMORY,
1572
1573         /* Basic lifecycle operations */
1574         .start =                sl811h_start,
1575         .stop =                 sl811h_stop,
1576
1577         /*
1578          * managing i/o requests and associated device resources
1579          */
1580         .urb_enqueue =          sl811h_urb_enqueue,
1581         .urb_dequeue =          sl811h_urb_dequeue,
1582         .endpoint_disable =     sl811h_endpoint_disable,
1583
1584         /*
1585          * periodic schedule support
1586          */
1587         .get_frame_number =     sl811h_get_frame,
1588
1589         /*
1590          * root hub support
1591          */
1592         .hub_status_data =      sl811h_hub_status_data,
1593         .hub_control =          sl811h_hub_control,
1594         .bus_suspend =          sl811h_bus_suspend,
1595         .bus_resume =           sl811h_bus_resume,
1596 };
1597
1598 /*-------------------------------------------------------------------------*/
1599
1600 static int __devexit
1601 sl811h_remove(struct platform_device *dev)
1602 {
1603         struct usb_hcd          *hcd = platform_get_drvdata(dev);
1604         struct sl811            *sl811 = hcd_to_sl811(hcd);
1605         struct resource         *res;
1606
1607         remove_debug_file(sl811);
1608         usb_remove_hcd(hcd);
1609
1610         /* some platforms may use IORESOURCE_IO */
1611         res = platform_get_resource(dev, IORESOURCE_MEM, 1);
1612         if (res)
1613                 iounmap(sl811->data_reg);
1614
1615         res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1616         if (res)
1617                 iounmap(sl811->addr_reg);
1618
1619         usb_put_hcd(hcd);
1620         return 0;
1621 }
1622
1623 static int __devinit
1624 sl811h_probe(struct platform_device *dev)
1625 {
1626         struct usb_hcd          *hcd;
1627         struct sl811            *sl811;
1628         struct resource         *addr, *data, *ires;
1629         int                     irq;
1630         void __iomem            *addr_reg;
1631         void __iomem            *data_reg;
1632         int                     retval;
1633         u8                      tmp, ioaddr = 0;
1634         unsigned long           irqflags;
1635
1636         /* basic sanity checks first.  board-specific init logic should
1637          * have initialized these three resources and probably board
1638          * specific platform_data.  we don't probe for IRQs, and do only
1639          * minimal sanity checking.
1640          */
1641         ires = platform_get_resource(dev, IORESOURCE_IRQ, 0);
1642         if (dev->num_resources < 3 || !ires)
1643                 return -ENODEV;
1644
1645         irq = ires->start;
1646         irqflags = ires->flags & IRQF_TRIGGER_MASK;
1647
1648         /* refuse to confuse usbcore */
1649         if (dev->dev.dma_mask) {
1650                 DBG("no we won't dma\n");
1651                 return -EINVAL;
1652         }
1653
1654         /* the chip may be wired for either kind of addressing */
1655         addr = platform_get_resource(dev, IORESOURCE_MEM, 0);
1656         data = platform_get_resource(dev, IORESOURCE_MEM, 1);
1657         retval = -EBUSY;
1658         if (!addr || !data) {
1659                 addr = platform_get_resource(dev, IORESOURCE_IO, 0);
1660                 data = platform_get_resource(dev, IORESOURCE_IO, 1);
1661                 if (!addr || !data)
1662                         return -ENODEV;
1663                 ioaddr = 1;
1664                 /*
1665                  * NOTE: 64-bit resource->start is getting truncated
1666                  * to avoid compiler warning, assuming that ->start
1667                  * is always 32-bit for this case
1668                  */
1669                 addr_reg = (void __iomem *) (unsigned long) addr->start;
1670                 data_reg = (void __iomem *) (unsigned long) data->start;
1671         } else {
1672                 addr_reg = ioremap(addr->start, 1);
1673                 if (addr_reg == NULL) {
1674                         retval = -ENOMEM;
1675                         goto err2;
1676                 }
1677
1678                 data_reg = ioremap(data->start, 1);
1679                 if (data_reg == NULL) {
1680                         retval = -ENOMEM;
1681                         goto err4;
1682                 }
1683         }
1684
1685         /* allocate and initialize hcd */
1686         hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev_name(&dev->dev));
1687         if (!hcd) {
1688                 retval = -ENOMEM;
1689                 goto err5;
1690         }
1691         hcd->rsrc_start = addr->start;
1692         sl811 = hcd_to_sl811(hcd);
1693
1694         spin_lock_init(&sl811->lock);
1695         INIT_LIST_HEAD(&sl811->async);
1696         sl811->board = dev->dev.platform_data;
1697         init_timer(&sl811->timer);
1698         sl811->timer.function = sl811h_timer;
1699         sl811->timer.data = (unsigned long) sl811;
1700         sl811->addr_reg = addr_reg;
1701         sl811->data_reg = data_reg;
1702
1703         spin_lock_irq(&sl811->lock);
1704         port_power(sl811, 0);
1705         spin_unlock_irq(&sl811->lock);
1706         msleep(200);
1707
1708         tmp = sl811_read(sl811, SL11H_HWREVREG);
1709         switch (tmp >> 4) {
1710         case 1:
1711                 hcd->product_desc = "SL811HS v1.2";
1712                 break;
1713         case 2:
1714                 hcd->product_desc = "SL811HS v1.5";
1715                 break;
1716         default:
1717                 /* reject case 0, SL11S is less functional */
1718                 DBG("chiprev %02x\n", tmp);
1719                 retval = -ENXIO;
1720                 goto err6;
1721         }
1722
1723         /* The chip's IRQ is level triggered, active high.  A requirement
1724          * for platform device setup is to cope with things like signal
1725          * inverters (e.g. CF is active low) or working only with edge
1726          * triggers (e.g. most ARM CPUs).  Initial driver stress testing
1727          * was on a system with single edge triggering, so most sorts of
1728          * triggering arrangement should work.
1729          *
1730          * Use resource IRQ flags if set by platform device setup.
1731          */
1732         irqflags |= IRQF_SHARED;
1733         retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | irqflags);
1734         if (retval != 0)
1735                 goto err6;
1736
1737         create_debug_file(sl811);
1738         return retval;
1739
1740  err6:
1741         usb_put_hcd(hcd);
1742  err5:
1743         if (!ioaddr)
1744                 iounmap(data_reg);
1745  err4:
1746         if (!ioaddr)
1747                 iounmap(addr_reg);
1748  err2:
1749         DBG("init error, %d\n", retval);
1750         return retval;
1751 }
1752
1753 #ifdef  CONFIG_PM
1754
1755 /* for this device there's no useful distinction between the controller
1756  * and its root hub, except that the root hub only gets direct PM calls
1757  * when CONFIG_USB_SUSPEND is enabled.
1758  */
1759
1760 static int
1761 sl811h_suspend(struct platform_device *dev, pm_message_t state)
1762 {
1763         struct usb_hcd  *hcd = platform_get_drvdata(dev);
1764         struct sl811    *sl811 = hcd_to_sl811(hcd);
1765         int             retval = 0;
1766
1767         switch (state.event) {
1768         case PM_EVENT_FREEZE:
1769                 retval = sl811h_bus_suspend(hcd);
1770                 break;
1771         case PM_EVENT_SUSPEND:
1772         case PM_EVENT_HIBERNATE:
1773         case PM_EVENT_PRETHAW:          /* explicitly discard hw state */
1774                 port_power(sl811, 0);
1775                 break;
1776         }
1777         return retval;
1778 }
1779
1780 static int
1781 sl811h_resume(struct platform_device *dev)
1782 {
1783         struct usb_hcd  *hcd = platform_get_drvdata(dev);
1784         struct sl811    *sl811 = hcd_to_sl811(hcd);
1785
1786         /* with no "check to see if VBUS is still powered" board hook,
1787          * let's assume it'd only be powered to enable remote wakeup.
1788          */
1789         if (!sl811->port1 || !device_can_wakeup(&hcd->self.root_hub->dev)) {
1790                 sl811->port1 = 0;
1791                 port_power(sl811, 1);
1792                 usb_root_hub_lost_power(hcd->self.root_hub);
1793                 return 0;
1794         }
1795
1796         return sl811h_bus_resume(hcd);
1797 }
1798
1799 #else
1800
1801 #define sl811h_suspend  NULL
1802 #define sl811h_resume   NULL
1803
1804 #endif
1805
1806
1807 /* this driver is exported so sl811_cs can depend on it */
1808 struct platform_driver sl811h_driver = {
1809         .probe =        sl811h_probe,
1810         .remove =       __devexit_p(sl811h_remove),
1811
1812         .suspend =      sl811h_suspend,
1813         .resume =       sl811h_resume,
1814         .driver = {
1815                 .name = (char *) hcd_name,
1816                 .owner = THIS_MODULE,
1817         },
1818 };
1819 EXPORT_SYMBOL(sl811h_driver);
1820
1821 /*-------------------------------------------------------------------------*/
1822
1823 static int __init sl811h_init(void)
1824 {
1825         if (usb_disabled())
1826                 return -ENODEV;
1827
1828         INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
1829         return platform_driver_register(&sl811h_driver);
1830 }
1831 module_init(sl811h_init);
1832
1833 static void __exit sl811h_cleanup(void)
1834 {
1835         platform_driver_unregister(&sl811h_driver);
1836 }
1837 module_exit(sl811h_cleanup);