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