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