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