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