2 * SL811HS HCD (Host Controller Driver) for USB.
4 * Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
5 * Copyright (C) 2004 David Brownell
7 * Periodic scheduling is based on Roman's OHCI code
8 * Copyright (C) 1999 Roman Weissgaerber
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).
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.
21 * Status: Passed basic stress testing, works with hubs, mice, keyboards,
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
34 #include <linux/config.h>
36 #ifdef CONFIG_USB_DEBUG
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>
60 #include <asm/system.h>
61 #include <asm/byteorder.h>
67 MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
68 MODULE_LICENSE("GPL");
70 #define DRIVER_VERSION "15 Dec 2004"
74 # define STUB_DEBUG_FILE
77 /* for now, use only one transfer register bank */
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.
89 static const char hcd_name[] = "sl811-hcd";
91 /*-------------------------------------------------------------------------*/
94 static void port_power(struct sl811 *sl811, int is_on)
96 struct usb_hcd *hcd = sl811_to_hcd(sl811);
98 /* hub is inactive unless the port is powered */
100 if (sl811->port1 & (1 << USB_PORT_FEAT_POWER))
103 sl811->port1 = (1 << USB_PORT_FEAT_POWER);
104 sl811->irq_enable = SL11H_INTMASK_INSRMV;
105 hcd->self.controller->power.power_state = PMSG_ON;
108 sl811->irq_enable = 0;
109 hcd->state = HC_STATE_HALT;
110 hcd->self.controller->power.power_state = PMSG_SUSPEND;
113 sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
114 sl811_write(sl811, SL11H_IRQ_STATUS, ~0);
116 if (sl811->board && sl811->board->port_power) {
117 /* switch VBUS, at 500mA unless hub power budget gets set */
118 DBG("power %s\n", is_on ? "on" : "off");
119 sl811->board->port_power(hcd->self.controller, is_on);
122 /* reset as thoroughly as we can */
123 if (sl811->board && sl811->board->reset)
124 sl811->board->reset(hcd->self.controller);
126 sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
127 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
128 sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT);
129 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
131 // if !is_on, put into lowpower mode now
134 /*-------------------------------------------------------------------------*/
136 /* This is a PIO-only HCD. Queueing appends URBs to the endpoint's queue,
137 * and may start I/O. Endpoint queues are scanned during completion irq
138 * handlers (one per packet: ACK, NAK, faults, etc) and urb cancelation.
140 * Using an external DMA engine to copy a packet at a time could work,
141 * though setup/teardown costs may be too big to make it worthwhile.
144 /* SETUP starts a new control request. Devices are not allowed to
145 * STALL or NAK these; they must cancel any pending control requests.
147 static void setup_packet(
149 struct sl811h_ep *ep,
157 void __iomem *data_reg;
159 addr = SL811HS_PACKET_BUF(bank == 0);
160 len = sizeof(struct usb_ctrlrequest);
161 data_reg = sl811->data_reg;
162 sl811_write_buf(sl811, addr, urb->setup_packet, len);
164 /* autoincrementing */
165 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
166 writeb(len, data_reg);
167 writeb(SL_SETUP /* | ep->epnum */, data_reg);
168 writeb(usb_pipedevice(urb->pipe), data_reg);
170 /* always OUT/data0 */ ;
171 sl811_write(sl811, bank + SL11H_HOSTCTLREG,
172 control | SL11H_HCTLMASK_OUT);
174 PACKET("SETUP qh%p\n", ep);
177 /* STATUS finishes control requests, often after IN or OUT data packets */
178 static void status_packet(
180 struct sl811h_ep *ep,
187 void __iomem *data_reg;
189 do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe);
190 data_reg = sl811->data_reg;
192 /* autoincrementing */
193 sl811_write(sl811, bank + SL11H_BUFADDRREG, 0);
195 writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg);
196 writeb(usb_pipedevice(urb->pipe), data_reg);
198 /* always data1; sometimes IN */
199 control |= SL11H_HCTLMASK_TOGGLE;
201 control |= SL11H_HCTLMASK_OUT;
202 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
204 PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "",
205 do_out ? "out" : "in", ep);
208 /* IN packets can be used with any type of endpoint. here we just
209 * start the transfer, data from the peripheral may arrive later.
210 * urb->iso_frame_desc is currently ignored here...
212 static void in_packet(
214 struct sl811h_ep *ep,
222 void __iomem *data_reg;
224 /* avoid losing data on overflow */
226 addr = SL811HS_PACKET_BUF(bank == 0);
227 if (!(control & SL11H_HCTLMASK_ISOCH)
228 && usb_gettoggle(urb->dev, ep->epnum, 0))
229 control |= SL11H_HCTLMASK_TOGGLE;
230 data_reg = sl811->data_reg;
232 /* autoincrementing */
233 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
234 writeb(len, data_reg);
235 writeb(SL_IN | ep->epnum, data_reg);
236 writeb(usb_pipedevice(urb->pipe), data_reg);
238 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
239 ep->length = min((int)len,
240 urb->transfer_buffer_length - urb->actual_length);
241 PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
242 !!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len);
245 /* OUT packets can be used with any type of endpoint.
246 * urb->iso_frame_desc is currently ignored here...
248 static void out_packet(
250 struct sl811h_ep *ep,
259 void __iomem *data_reg;
261 buf = urb->transfer_buffer + urb->actual_length;
264 len = min((int)ep->maxpacket,
265 urb->transfer_buffer_length - urb->actual_length);
267 if (!(control & SL11H_HCTLMASK_ISOCH)
268 && usb_gettoggle(urb->dev, ep->epnum, 1))
269 control |= SL11H_HCTLMASK_TOGGLE;
270 addr = SL811HS_PACKET_BUF(bank == 0);
271 data_reg = sl811->data_reg;
273 sl811_write_buf(sl811, addr, buf, len);
275 /* autoincrementing */
276 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
277 writeb(len, data_reg);
278 writeb(SL_OUT | ep->epnum, data_reg);
279 writeb(usb_pipedevice(urb->pipe), data_reg);
281 sl811_write(sl811, bank + SL11H_HOSTCTLREG,
282 control | SL11H_HCTLMASK_OUT);
284 PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
285 !!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len);
288 /*-------------------------------------------------------------------------*/
290 /* caller updates on-chip enables later */
292 static inline void sofirq_on(struct sl811 *sl811)
294 if (sl811->irq_enable & SL11H_INTMASK_SOFINTR)
296 VDBG("sof irq on\n");
297 sl811->irq_enable |= SL11H_INTMASK_SOFINTR;
300 static inline void sofirq_off(struct sl811 *sl811)
302 if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR))
304 VDBG("sof irq off\n");
305 sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR;
308 /*-------------------------------------------------------------------------*/
310 /* pick the next endpoint for a transaction, and issue it.
311 * frames start with periodic transfers (after whatever is pending
312 * from the previous frame), and the rest of the time is async
313 * transfers, scheduled round-robin.
315 static struct sl811h_ep *start(struct sl811 *sl811, u8 bank)
317 struct sl811h_ep *ep;
322 /* use endpoint at schedule head */
323 if (sl811->next_periodic) {
324 ep = sl811->next_periodic;
325 sl811->next_periodic = ep->next;
327 if (sl811->next_async)
328 ep = sl811->next_async;
329 else if (!list_empty(&sl811->async))
330 ep = container_of(sl811->async.next,
331 struct sl811h_ep, schedule);
333 /* could set up the first fullspeed periodic
334 * transfer for the next frame ...
340 if ((bank && sl811->active_b == ep) || sl811->active_a == ep)
344 if (ep->schedule.next == &sl811->async)
345 sl811->next_async = NULL;
347 sl811->next_async = container_of(ep->schedule.next,
348 struct sl811h_ep, schedule);
351 if (unlikely(list_empty(&ep->hep->urb_list))) {
352 DBG("empty %p queue?\n", ep);
356 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
357 control = ep->defctrl;
359 /* if this frame doesn't have enough time left to transfer this
360 * packet, wait till the next frame. too-simple algorithm...
362 fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6;
363 fclock -= 100; /* setup takes not much time */
364 if (urb->dev->speed == USB_SPEED_LOW) {
365 if (control & SL11H_HCTLMASK_PREAMBLE) {
366 /* also note erratum 1: some hubs won't work */
369 fclock -= ep->maxpacket << 8;
371 /* erratum 2: AFTERSOF only works for fullspeed */
374 sl811->stat_overrun++;
379 fclock -= 12000 / 19; /* 19 64byte packets/msec */
382 sl811->stat_overrun++;
383 control |= SL11H_HCTLMASK_AFTERSOF;
385 /* throttle bulk/control irq noise */
386 } else if (ep->nak_count)
387 control |= SL11H_HCTLMASK_AFTERSOF;
391 switch (ep->nextpid) {
393 in_packet(sl811, ep, urb, bank, control);
396 out_packet(sl811, ep, urb, bank, control);
399 setup_packet(sl811, ep, urb, bank, control);
401 case USB_PID_ACK: /* for control status */
402 status_packet(sl811, ep, urb, bank, control);
405 DBG("bad ep%p pid %02x\n", ep, ep->nextpid);
411 #define MIN_JIFFIES ((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2)
413 static inline void start_transfer(struct sl811 *sl811)
415 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
417 if (sl811->active_a == NULL) {
418 sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF));
419 if (sl811->active_a != NULL)
420 sl811->jiffies_a = jiffies + MIN_JIFFIES;
423 if (sl811->active_b == NULL) {
424 sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF));
425 if (sl811->active_b != NULL)
426 sl811->jiffies_b = jiffies + MIN_JIFFIES;
431 static void finish_request(
433 struct sl811h_ep *ep,
435 struct pt_regs *regs,
437 ) __releases(sl811->lock) __acquires(sl811->lock)
441 if (usb_pipecontrol(urb->pipe))
442 ep->nextpid = USB_PID_SETUP;
444 spin_lock(&urb->lock);
445 if (urb->status == -EINPROGRESS)
446 urb->status = status;
447 spin_unlock(&urb->lock);
449 spin_unlock(&sl811->lock);
450 usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, regs);
451 spin_lock(&sl811->lock);
453 /* leave active endpoints in the schedule */
454 if (!list_empty(&ep->hep->urb_list))
457 /* async deschedule? */
458 if (!list_empty(&ep->schedule)) {
459 list_del_init(&ep->schedule);
460 if (ep == sl811->next_async)
461 sl811->next_async = NULL;
465 /* periodic deschedule */
466 DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
467 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
468 struct sl811h_ep *temp;
469 struct sl811h_ep **prev = &sl811->periodic[i];
471 while (*prev && ((temp = *prev) != ep))
475 sl811->load[i] -= ep->load;
477 ep->branch = PERIODIC_SIZE;
478 sl811->periodic_count--;
479 sl811_to_hcd(sl811)->self.bandwidth_allocated
480 -= ep->load / ep->period;
481 if (ep == sl811->next_periodic)
482 sl811->next_periodic = ep->next;
484 /* we might turn SOFs back on again for the async schedule */
485 if (sl811->periodic_count == 0)
490 done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank, struct pt_regs *regs)
494 int urbstat = -EINPROGRESS;
499 status = sl811_read(sl811, bank + SL11H_PKTSTATREG);
501 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
503 /* we can safely ignore NAKs */
504 if (status & SL11H_STATMASK_NAK) {
505 // PACKET("...NAK_%02x qh%p\n", bank, ep);
510 /* ACK advances transfer, toggle, and maybe queue */
511 } else if (status & SL11H_STATMASK_ACK) {
512 struct usb_device *udev = urb->dev;
516 /* urb->iso_frame_desc is currently ignored here... */
518 ep->nak_count = ep->error_count = 0;
519 switch (ep->nextpid) {
521 // PACKET("...ACK/out_%02x qh%p\n", bank, ep);
522 urb->actual_length += ep->length;
523 usb_dotoggle(udev, ep->epnum, 1);
524 if (urb->actual_length
525 == urb->transfer_buffer_length) {
526 if (usb_pipecontrol(urb->pipe))
527 ep->nextpid = USB_PID_ACK;
529 /* some bulk protocols terminate OUT transfers
530 * by a short packet, using ZLPs not padding.
532 else if (ep->length < ep->maxpacket
533 || !(urb->transfer_flags
539 // PACKET("...ACK/in_%02x qh%p\n", bank, ep);
540 buf = urb->transfer_buffer + urb->actual_length;
542 len = ep->maxpacket - sl811_read(sl811,
543 bank + SL11H_XFERCNTREG);
544 if (len > ep->length) {
546 urb->status = -EOVERFLOW;
548 urb->actual_length += len;
549 sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
551 usb_dotoggle(udev, ep->epnum, 0);
552 if (urb->actual_length == urb->transfer_buffer_length)
554 else if (len < ep->maxpacket) {
555 if (urb->transfer_flags & URB_SHORT_NOT_OK)
556 urbstat = -EREMOTEIO;
560 if (usb_pipecontrol(urb->pipe)
561 && (urbstat == -EREMOTEIO
564 /* NOTE if the status stage STALLs (why?),
565 * this reports the wrong urb status.
567 spin_lock(&urb->lock);
568 if (urb->status == -EINPROGRESS)
569 urb->status = urbstat;
570 spin_unlock(&urb->lock);
573 ep->nextpid = USB_PID_ACK;
577 // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
578 if (urb->transfer_buffer_length == urb->actual_length)
579 ep->nextpid = USB_PID_ACK;
580 else if (usb_pipeout(urb->pipe)) {
581 usb_settoggle(udev, 0, 1, 1);
582 ep->nextpid = USB_PID_OUT;
584 usb_settoggle(udev, 0, 0, 1);
585 ep->nextpid = USB_PID_IN;
589 // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
594 /* STALL stops all transfers */
595 } else if (status & SL11H_STATMASK_STALL) {
596 PACKET("...STALL_%02x qh%p\n", bank, ep);
597 ep->nak_count = ep->error_count = 0;
600 /* error? retry, until "3 strikes" */
601 } else if (++ep->error_count >= 3) {
602 if (status & SL11H_STATMASK_TMOUT)
603 urbstat = -ETIMEDOUT;
604 else if (status & SL11H_STATMASK_OVF)
605 urbstat = -EOVERFLOW;
609 PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
610 bank, status, ep, urbstat);
613 if (urb && (urbstat != -EINPROGRESS || urb->status != -EINPROGRESS))
614 finish_request(sl811, ep, urb, regs, urbstat);
617 static inline u8 checkdone(struct sl811 *sl811)
622 if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
623 ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
624 if (ctl & SL11H_HCTLMASK_ARM)
625 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
626 DBG("%s DONE_A: ctrl %02x sts %02x\n",
627 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
629 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
630 irqstat |= SL11H_INTMASK_DONE_A;
633 if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
634 ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
635 if (ctl & SL11H_HCTLMASK_ARM)
636 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
637 DBG("%s DONE_B: ctrl %02x sts %02x\n",
638 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
640 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
641 irqstat |= SL11H_INTMASK_DONE_A;
647 static irqreturn_t sl811h_irq(struct usb_hcd *hcd, struct pt_regs *regs)
649 struct sl811 *sl811 = hcd_to_sl811(hcd);
651 irqreturn_t ret = IRQ_NONE;
652 unsigned retries = 5;
654 spin_lock(&sl811->lock);
657 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
659 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
660 irqstat &= sl811->irq_enable;
664 /* this may no longer be necessary ... */
665 if (irqstat == 0 && ret == IRQ_NONE) {
666 irqstat = checkdone(sl811);
667 if (irqstat /* && irq != ~0 */ )
672 /* USB packets, not necessarily handled in the order they're
673 * issued ... that's fine if they're different endpoints.
675 if (irqstat & SL11H_INTMASK_DONE_A) {
676 done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF), regs);
677 sl811->active_a = NULL;
681 if (irqstat & SL11H_INTMASK_DONE_B) {
682 done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF), regs);
683 sl811->active_b = NULL;
687 if (irqstat & SL11H_INTMASK_SOFINTR) {
690 index = sl811->frame++ % (PERIODIC_SIZE - 1);
693 /* be graceful about almost-inevitable periodic schedule
694 * overruns: continue the previous frame's transfers iff
695 * this one has nothing scheduled.
697 if (sl811->next_periodic) {
698 // ERR("overrun to slot %d\n", index);
699 sl811->stat_overrun++;
701 if (sl811->periodic[index])
702 sl811->next_periodic = sl811->periodic[index];
705 /* khubd manages debouncing and wakeup */
706 if (irqstat & SL11H_INTMASK_INSRMV) {
707 sl811->stat_insrmv++;
709 /* most stats are reset for each VBUS session */
710 sl811->stat_wake = 0;
714 sl811->stat_lost = 0;
717 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
719 sl811->irq_enable = SL11H_INTMASK_INSRMV;
720 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
722 /* usbcore nukes other pending transactions on disconnect */
723 if (sl811->active_a) {
724 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
725 finish_request(sl811, sl811->active_a,
726 container_of(sl811->active_a->hep->urb_list.next,
727 struct urb, urb_list),
729 sl811->active_a = NULL;
732 if (sl811->active_b) {
733 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
734 finish_request(sl811, sl811->active_b,
735 container_of(sl811->active_b->hep->urb_list.next,
736 struct urb, urb_list),
738 sl811->active_b = NULL;
742 /* port status seems wierd until after reset, so
743 * force the reset and make khubd clean up later.
745 sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
746 | (1 << USB_PORT_FEAT_CONNECTION);
748 } else if (irqstat & SL11H_INTMASK_RD) {
749 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) {
751 sl811->port1 |= 1 << USB_PORT_FEAT_C_SUSPEND;
754 irqstat &= ~SL11H_INTMASK_RD;
758 if (sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
759 start_transfer(sl811);
765 if (sl811->periodic_count == 0 && list_empty(&sl811->async))
767 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
769 spin_unlock(&sl811->lock);
774 /*-------------------------------------------------------------------------*/
776 /* usb 1.1 says max 90% of a frame is available for periodic transfers.
777 * this driver doesn't promise that much since it's got to handle an
778 * IRQ per packet; irq handling latencies also use up that time.
780 #define MAX_PERIODIC_LOAD 500 /* out of 1000 usec */
782 static int balance(struct sl811 *sl811, u16 period, u16 load)
784 int i, branch = -ENOSPC;
786 /* search for the least loaded schedule branch of that period
787 * which has enough bandwidth left unreserved.
789 for (i = 0; i < period ; i++) {
790 if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
793 for (j = i; j < PERIODIC_SIZE; j += period) {
794 if ((sl811->load[j] + load)
798 if (j < PERIODIC_SIZE)
806 /*-------------------------------------------------------------------------*/
808 static int sl811h_urb_enqueue(
810 struct usb_host_endpoint *hep,
814 struct sl811 *sl811 = hcd_to_sl811(hcd);
815 struct usb_device *udev = urb->dev;
816 unsigned int pipe = urb->pipe;
817 int is_out = !usb_pipein(pipe);
818 int type = usb_pipetype(pipe);
819 int epnum = usb_pipeendpoint(pipe);
820 struct sl811h_ep *ep = NULL;
826 if (type == PIPE_ISOCHRONOUS)
830 /* avoid all allocations within spinlocks */
832 ep = kcalloc(1, sizeof *ep, mem_flags);
833 /* set hep, otherwise sl811h_ep *start(struct sl811 *sl811, u8 bank) crashes */
837 spin_lock_irqsave(&sl811->lock, flags);
839 /* don't submit to a dead or disabled port */
840 if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
841 || !HC_IS_RUNNING(hcd->state)) {
854 INIT_LIST_HEAD(&ep->schedule);
855 ep->udev = usb_get_dev(udev);
857 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
858 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
859 usb_settoggle(udev, epnum, is_out, 0);
861 if (type == PIPE_CONTROL)
862 ep->nextpid = USB_PID_SETUP;
864 ep->nextpid = USB_PID_OUT;
866 ep->nextpid = USB_PID_IN;
868 if (ep->maxpacket > H_MAXPACKET) {
869 /* iso packets up to 240 bytes could work... */
870 DBG("dev %d ep%d maxpacket %d\n",
871 udev->devnum, epnum, ep->maxpacket);
876 if (udev->speed == USB_SPEED_LOW) {
877 /* send preamble for external hub? */
878 if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
879 ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
882 case PIPE_ISOCHRONOUS:
884 if (urb->interval > PERIODIC_SIZE)
885 urb->interval = PERIODIC_SIZE;
886 ep->period = urb->interval;
887 ep->branch = PERIODIC_SIZE;
888 if (type == PIPE_ISOCHRONOUS)
889 ep->defctrl |= SL11H_HCTLMASK_ISOCH;
890 ep->load = usb_calc_bus_time(udev->speed, !is_out,
891 (type == PIPE_ISOCHRONOUS),
892 usb_maxpacket(udev, pipe, is_out))
900 /* maybe put endpoint into schedule */
904 if (list_empty(&ep->schedule))
905 list_add_tail(&ep->schedule, &sl811->async);
907 case PIPE_ISOCHRONOUS:
909 urb->interval = ep->period;
910 if (ep->branch < PERIODIC_SIZE)
913 retval = balance(sl811, ep->period, ep->load);
918 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
921 /* sort each schedule branch by period (slow before fast)
922 * to share the faster parts of the tree without needing
923 * dummy/placeholder nodes
925 DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
926 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
927 struct sl811h_ep **prev = &sl811->periodic[i];
928 struct sl811h_ep *here = *prev;
930 while (here && ep != here) {
931 if (ep->period > here->period)
940 sl811->load[i] += ep->load;
942 sl811->periodic_count++;
943 hcd->self.bandwidth_allocated += ep->load / ep->period;
947 /* in case of unlink-during-submit */
948 spin_lock(&urb->lock);
949 if (urb->status != -EINPROGRESS) {
950 spin_unlock(&urb->lock);
951 finish_request(sl811, ep, urb, NULL, 0);
956 /* when uncommented, causes this error on mouse open:
957 drivers/usb/input/usbmouse.c: can't resubmit intr, sl811-hcd-1/input0, status -22
958 urb->hcpriv = hep; */
960 spin_unlock(&urb->lock);
962 start_transfer(sl811);
963 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
965 spin_unlock_irqrestore(&sl811->lock, flags);
969 static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
971 struct sl811 *sl811 = hcd_to_sl811(hcd);
972 struct usb_host_endpoint *hep = urb->hcpriv;
974 struct sl811h_ep *ep;
980 spin_lock_irqsave(&sl811->lock, flags);
983 /* finish right away if this urb can't be active ...
984 * note that some drivers wrongly expect delays
986 if (ep->hep->urb_list.next != &urb->urb_list) {
987 /* not front of queue? never active */
989 /* for active transfers, we expect an IRQ */
990 } else if (sl811->active_a == ep) {
991 if (time_before_eq(sl811->jiffies_a, jiffies)) {
992 /* happens a lot with lowspeed?? */
993 DBG("giveup on DONE_A: ctrl %02x sts %02x\n",
995 SL811_EP_A(SL11H_HOSTCTLREG)),
997 SL811_EP_A(SL11H_PKTSTATREG)));
998 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
1000 sl811->active_a = NULL;
1004 } else if (sl811->active_b == ep) {
1005 if (time_before_eq(sl811->jiffies_a, jiffies)) {
1006 /* happens a lot with lowspeed?? */
1007 DBG("giveup on DONE_B: ctrl %02x sts %02x\n",
1009 SL811_EP_B(SL11H_HOSTCTLREG)),
1011 SL811_EP_B(SL11H_PKTSTATREG)));
1012 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
1014 sl811->active_b = NULL;
1019 /* front of queue for inactive endpoint */
1023 finish_request(sl811, ep, urb, NULL, 0);
1025 VDBG("dequeue, urb %p active %s; wait4irq\n", urb,
1026 (sl811->active_a == ep) ? "A" : "B");
1029 spin_unlock_irqrestore(&sl811->lock, flags);
1034 sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1036 struct sl811h_ep *ep = hep->hcpriv;
1041 /* assume we'd just wait for the irq */
1042 if (!list_empty(&hep->urb_list))
1044 if (!list_empty(&hep->urb_list))
1045 WARN("ep %p not empty?\n", ep);
1047 usb_put_dev(ep->udev);
1053 sl811h_get_frame(struct usb_hcd *hcd)
1055 struct sl811 *sl811 = hcd_to_sl811(hcd);
1057 /* wrong except while periodic transfers are scheduled;
1058 * never matches the on-the-wire frame;
1059 * subject to overruns.
1061 return sl811->frame;
1065 /*-------------------------------------------------------------------------*/
1067 /* the virtual root hub timer IRQ checks for hub status */
1069 sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
1071 struct sl811 *sl811 = hcd_to_sl811(hcd);
1073 unsigned long flags;
1075 /* non-SMP HACK: use root hub timer as i/o watchdog
1076 * this seems essential when SOF IRQs aren't in use...
1078 local_irq_save(flags);
1079 if (!timer_pending(&sl811->timer)) {
1080 if (sl811h_irq( /* ~0, */ hcd, NULL) != IRQ_NONE)
1083 local_irq_restore(flags);
1086 if (!(sl811->port1 & (0xffff << 16)))
1089 /* tell khubd port 1 changed */
1095 sl811h_hub_descriptor (
1096 struct sl811 *sl811,
1097 struct usb_hub_descriptor *desc
1101 desc->bDescriptorType = 0x29;
1102 desc->bHubContrCurrent = 0;
1104 desc->bNbrPorts = 1;
1105 desc->bDescLength = 9;
1107 /* per-port power switching (gang of one!), or none */
1108 desc->bPwrOn2PwrGood = 0;
1109 if (sl811->board && sl811->board->port_power) {
1110 desc->bPwrOn2PwrGood = sl811->board->potpg;
1111 if (!desc->bPwrOn2PwrGood)
1112 desc->bPwrOn2PwrGood = 10;
1117 /* no overcurrent errors detection/handling */
1120 desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp);
1122 /* two bitmaps: ports removable, and legacy PortPwrCtrlMask */
1123 desc->bitmap[0] = 1 << 1;
1124 desc->bitmap[1] = ~0;
1128 sl811h_timer(unsigned long _sl811)
1130 struct sl811 *sl811 = (void *) _sl811;
1131 unsigned long flags;
1133 u8 signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
1134 const u32 mask = (1 << USB_PORT_FEAT_CONNECTION)
1135 | (1 << USB_PORT_FEAT_ENABLE)
1136 | (1 << USB_PORT_FEAT_LOWSPEED);
1138 spin_lock_irqsave(&sl811->lock, flags);
1140 /* stop special signaling */
1141 sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
1142 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1145 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);
1147 switch (signaling) {
1148 case SL11H_CTL1MASK_SE0:
1150 sl811->port1 = (1 << USB_PORT_FEAT_C_RESET)
1151 | (1 << USB_PORT_FEAT_POWER);
1153 /* don't wrongly ack RD */
1154 if (irqstat & SL11H_INTMASK_INSRMV)
1155 irqstat &= ~SL11H_INTMASK_RD;
1157 case SL11H_CTL1MASK_K:
1158 DBG("end resume\n");
1159 sl811->port1 &= ~(1 << USB_PORT_FEAT_SUSPEND);
1162 DBG("odd timer signaling: %02x\n", signaling);
1165 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
1167 if (irqstat & SL11H_INTMASK_RD) {
1168 /* usbcore nukes all pending transactions on disconnect */
1169 if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION))
1170 sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
1171 | (1 << USB_PORT_FEAT_C_ENABLE);
1172 sl811->port1 &= ~mask;
1173 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1175 sl811->port1 |= mask;
1176 if (irqstat & SL11H_INTMASK_DP)
1177 sl811->port1 &= ~(1 << USB_PORT_FEAT_LOWSPEED);
1178 sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
1181 if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION)) {
1182 u8 ctrl2 = SL811HS_CTL2_INIT;
1184 sl811->irq_enable |= SL11H_INTMASK_DONE_A;
1186 sl811->irq_enable |= SL11H_INTMASK_DONE_B;
1188 if (sl811->port1 & (1 << USB_PORT_FEAT_LOWSPEED)) {
1189 sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
1190 ctrl2 |= SL811HS_CTL2MASK_DSWAP;
1193 /* start SOFs flowing, kickstarting with A registers */
1194 sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
1195 sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
1196 sl811_write(sl811, SL811HS_CTLREG2, ctrl2);
1198 /* autoincrementing */
1199 sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
1200 writeb(SL_SOF, sl811->data_reg);
1201 writeb(0, sl811->data_reg);
1202 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
1203 SL11H_HCTLMASK_ARM);
1205 /* khubd provides debounce delay */
1209 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1212 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
1213 spin_unlock_irqrestore(&sl811->lock, flags);
1218 struct usb_hcd *hcd,
1225 struct sl811 *sl811 = hcd_to_sl811(hcd);
1227 unsigned long flags;
1229 spin_lock_irqsave(&sl811->lock, flags);
1232 case ClearHubFeature:
1235 case C_HUB_OVER_CURRENT:
1236 case C_HUB_LOCAL_POWER:
1242 case ClearPortFeature:
1243 if (wIndex != 1 || wLength != 0)
1247 case USB_PORT_FEAT_ENABLE:
1248 sl811->port1 &= (1 << USB_PORT_FEAT_POWER);
1250 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1251 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1252 sl811_write(sl811, SL11H_IRQ_ENABLE,
1255 case USB_PORT_FEAT_SUSPEND:
1256 if (!(sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)))
1259 /* 20 msec of resume/K signaling, other irqs blocked */
1260 DBG("start resume...\n");
1261 sl811->irq_enable = 0;
1262 sl811_write(sl811, SL11H_IRQ_ENABLE,
1264 sl811->ctrl1 |= SL11H_CTL1MASK_K;
1265 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1267 mod_timer(&sl811->timer, jiffies
1268 + msecs_to_jiffies(20));
1270 case USB_PORT_FEAT_POWER:
1271 port_power(sl811, 0);
1273 case USB_PORT_FEAT_C_ENABLE:
1274 case USB_PORT_FEAT_C_SUSPEND:
1275 case USB_PORT_FEAT_C_CONNECTION:
1276 case USB_PORT_FEAT_C_OVER_CURRENT:
1277 case USB_PORT_FEAT_C_RESET:
1282 sl811->port1 &= ~(1 << wValue);
1284 case GetHubDescriptor:
1285 sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
1288 *(__le32 *) buf = cpu_to_le32(0);
1293 *(__le32 *) buf = cpu_to_le32(sl811->port1);
1296 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
1298 DBG("GetPortStatus %08x\n", sl811->port1);
1300 case SetPortFeature:
1301 if (wIndex != 1 || wLength != 0)
1304 case USB_PORT_FEAT_SUSPEND:
1305 if (sl811->port1 & (1 << USB_PORT_FEAT_RESET))
1307 if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE)))
1310 DBG("suspend...\n");
1311 sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
1312 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1314 case USB_PORT_FEAT_POWER:
1315 port_power(sl811, 1);
1317 case USB_PORT_FEAT_RESET:
1318 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
1320 if (!(sl811->port1 & (1 << USB_PORT_FEAT_POWER)))
1323 /* 50 msec of reset/SE0 signaling, irqs blocked */
1324 sl811->irq_enable = 0;
1325 sl811_write(sl811, SL11H_IRQ_ENABLE,
1327 sl811->ctrl1 = SL11H_CTL1MASK_SE0;
1328 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1329 sl811->port1 |= (1 << USB_PORT_FEAT_RESET);
1330 mod_timer(&sl811->timer, jiffies
1331 + msecs_to_jiffies(50));
1336 sl811->port1 |= 1 << wValue;
1341 /* "protocol stall" on error */
1345 spin_unlock_irqrestore(&sl811->lock, flags);
1352 sl811h_hub_suspend(struct usb_hcd *hcd)
1355 DBG("%s\n", __FUNCTION__);
1360 sl811h_hub_resume(struct usb_hcd *hcd)
1363 DBG("%s\n", __FUNCTION__);
1369 #define sl811h_hub_suspend NULL
1370 #define sl811h_hub_resume NULL
1375 /*-------------------------------------------------------------------------*/
1377 #ifdef STUB_DEBUG_FILE
1379 static inline void create_debug_file(struct sl811 *sl811) { }
1380 static inline void remove_debug_file(struct sl811 *sl811) { }
1384 #include <linux/proc_fs.h>
1385 #include <linux/seq_file.h>
1387 static void dump_irq(struct seq_file *s, char *label, u8 mask)
1389 seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
1390 (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
1391 (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
1392 (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
1393 (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
1394 (mask & SL11H_INTMASK_RD) ? " rd" : "",
1395 (mask & SL11H_INTMASK_DP) ? " dp" : "");
1398 static int proc_sl811h_show(struct seq_file *s, void *unused)
1400 struct sl811 *sl811 = s->private;
1401 struct sl811h_ep *ep;
1404 seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
1405 sl811_to_hcd(sl811)->product_desc,
1406 hcd_name, DRIVER_VERSION,
1409 seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
1410 seq_printf(s, "current session: done_a %ld done_b %ld "
1411 "wake %ld sof %ld overrun %ld lost %ld\n\n",
1412 sl811->stat_a, sl811->stat_b,
1413 sl811->stat_wake, sl811->stat_sof,
1414 sl811->stat_overrun, sl811->stat_lost);
1416 spin_lock_irq(&sl811->lock);
1418 if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
1419 seq_printf(s, "(suspended)\n\n");
1421 u8 t = sl811_read(sl811, SL11H_CTLREG1);
1423 seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
1424 (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
1425 ({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
1426 case SL11H_CTL1MASK_NORMAL: s = ""; break;
1427 case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
1428 case SL11H_CTL1MASK_K: s = " k/resume"; break;
1429 default: s = "j"; break;
1431 (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
1432 (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");
1434 dump_irq(s, "irq_enable",
1435 sl811_read(sl811, SL11H_IRQ_ENABLE));
1436 dump_irq(s, "irq_status",
1437 sl811_read(sl811, SL11H_IRQ_STATUS));
1438 seq_printf(s, "frame clocks remaining: %d\n",
1439 sl811_read(sl811, SL11H_SOFTMRREG) << 6);
1442 seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
1443 sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
1444 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
1445 seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
1446 sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
1447 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
1448 seq_printf(s, "\n");
1449 list_for_each_entry (ep, &sl811->async, schedule) {
1452 seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
1454 (ep == sl811->active_a) ? "(A) " : "",
1455 (ep == sl811->active_b) ? "(B) " : "",
1457 ({ char *s; switch (ep->nextpid) {
1458 case USB_PID_IN: s = "in"; break;
1459 case USB_PID_OUT: s = "out"; break;
1460 case USB_PID_SETUP: s = "setup"; break;
1461 case USB_PID_ACK: s = "status"; break;
1462 default: s = "?"; break;
1465 ep->nak_count, ep->error_count);
1466 list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
1467 seq_printf(s, " urb%p, %d/%d\n", urb,
1469 urb->transfer_buffer_length);
1472 if (!list_empty(&sl811->async))
1473 seq_printf(s, "\n");
1475 seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
1477 for (i = 0; i < PERIODIC_SIZE; i++) {
1478 ep = sl811->periodic[i];
1481 seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);
1483 /* DUMB: prints shared entries multiple times */
1486 " %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
1488 (ep == sl811->active_a) ? "(A) " : "",
1489 (ep == sl811->active_b) ? "(B) " : "",
1491 (ep->udev->speed == USB_SPEED_FULL)
1493 ep->udev->devnum, ep->epnum,
1494 (ep->epnum == 0) ? ""
1495 : ((ep->nextpid == USB_PID_IN)
1498 ep->maxpacket, ep->error_count);
1503 spin_unlock_irq(&sl811->lock);
1504 seq_printf(s, "\n");
1509 static int proc_sl811h_open(struct inode *inode, struct file *file)
1511 return single_open(file, proc_sl811h_show, PDE(inode)->data);
1514 static struct file_operations proc_ops = {
1515 .open = proc_sl811h_open,
1517 .llseek = seq_lseek,
1518 .release = single_release,
1521 /* expect just one sl811 per system */
1522 static const char proc_filename[] = "driver/sl811h";
1524 static void create_debug_file(struct sl811 *sl811)
1526 struct proc_dir_entry *pde;
1528 pde = create_proc_entry(proc_filename, 0, NULL);
1532 pde->proc_fops = &proc_ops;
1537 static void remove_debug_file(struct sl811 *sl811)
1540 remove_proc_entry(proc_filename, NULL);
1545 /*-------------------------------------------------------------------------*/
1548 sl811h_stop(struct usb_hcd *hcd)
1550 struct sl811 *sl811 = hcd_to_sl811(hcd);
1551 unsigned long flags;
1553 del_timer_sync(&hcd->rh_timer);
1555 spin_lock_irqsave(&sl811->lock, flags);
1556 port_power(sl811, 0);
1557 spin_unlock_irqrestore(&sl811->lock, flags);
1561 sl811h_start(struct usb_hcd *hcd)
1563 struct sl811 *sl811 = hcd_to_sl811(hcd);
1565 /* chip has been reset, VBUS power is off */
1566 hcd->state = HC_STATE_RUNNING;
1569 hcd->can_wakeup = sl811->board->can_wakeup;
1570 hcd->power_budget = sl811->board->power * 2;
1573 // enable power and interupts
1574 port_power(sl811, 1);
1579 /*-------------------------------------------------------------------------*/
1581 static struct hc_driver sl811h_hc_driver = {
1582 .description = hcd_name,
1583 .hcd_priv_size = sizeof(struct sl811),
1586 * generic hardware linkage
1589 .flags = HCD_USB11 | HCD_MEMORY,
1591 /* Basic lifecycle operations */
1592 .start = sl811h_start,
1593 .stop = sl811h_stop,
1596 * managing i/o requests and associated device resources
1598 .urb_enqueue = sl811h_urb_enqueue,
1599 .urb_dequeue = sl811h_urb_dequeue,
1600 .endpoint_disable = sl811h_endpoint_disable,
1603 * periodic schedule support
1605 .get_frame_number = sl811h_get_frame,
1610 .hub_status_data = sl811h_hub_status_data,
1611 .hub_control = sl811h_hub_control,
1612 .hub_suspend = sl811h_hub_suspend,
1613 .hub_resume = sl811h_hub_resume,
1616 /*-------------------------------------------------------------------------*/
1618 static int __init_or_module
1619 sl811h_remove(struct device *dev)
1621 struct usb_hcd *hcd = dev_get_drvdata(dev);
1622 struct sl811 *sl811 = hcd_to_sl811(hcd);
1623 struct platform_device *pdev;
1624 struct resource *res;
1626 DBG("sl811h_remove\n");
1627 pdev = container_of(dev, struct platform_device, dev);
1629 remove_debug_file(sl811);
1630 usb_remove_hcd(hcd);
1632 #ifndef CONFIG_USB_SL811_CS
1633 iounmap(sl811->data_reg);
1635 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1636 if (res != NULL) release_mem_region(res->start, 1);
1637 else DBG("res: %d\n", res);
1638 #ifndef CONFIG_USB_SL811_CS
1639 iounmap(sl811->addr_reg);
1641 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1642 if (res != NULL) release_mem_region(res->start, 1);
1643 else DBG("res: %d\n", res);
1649 #define resource_len(r) (((r)->end - (r)->start) + 1)
1651 /***************************************************************************
1652 * This routine tests the Read/Write functionality of SL811HS registers
1654 * 1) Store original register value into a buffer
1655 * 2) Write to registers with a RAMP pattern. (10, 11, 12, ..., 255)
1656 * 3) Read from register
1657 * 4) Compare the written value with the read value and make sure they are
1659 * 5) Restore the original register value
1661 * Input: hci = data structure for the host controller
1663 * Return: 1 = passed; 0 = failed
1664 **************************************************************************/
1665 static int test_registers(struct sl811 * sl811)
1670 for (i = 0x10; i < 256; i++) {
1671 /* save the original buffer */
1672 buf[i] = sl811_read(sl811, i);
1674 /* Write the new data to the buffer */
1675 sl811_write(sl811, i, i);
1678 /* compare the written data */
1679 for (i = 0x10; i < 256; i++) {
1680 data = sl811_read(sl811, i);
1682 DBG("Pattern test failed!! value = 0x%x, s/b 0x%x\n",
1689 /* restore the data */
1690 for (i = 0x10; i < 256; i++) {
1691 sl811_write(sl811, i, buf[i]);
1698 int sl811h_probe(struct device *dev)
1700 struct usb_hcd *hcd;
1701 struct sl811 *sl811;
1702 struct platform_device *pdev;
1703 struct resource *addr, *data;
1705 void __iomem *addr_reg;
1706 void __iomem *data_reg;
1710 /* basic sanity checks first. board-specific init logic should
1711 * have initialized these three resources and probably board
1712 * specific platform_data. we don't probe for IRQs, and do only
1713 * minimal sanity checking.
1715 pdev = container_of(dev, struct platform_device, dev);
1716 if (pdev->num_resources < 3)
1719 addr = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1720 data = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1721 irq = platform_get_irq(pdev, 0);
1722 if (!addr || !data || irq < 0)
1725 /* refuse to confuse usbcore */
1726 if (dev->dma_mask) {
1727 DBG("no we won't dma\n");
1731 if (!request_mem_region(addr->start, 1, hcd_name)) {
1735 #ifdef CONFIG_USB_SL811_CS
1736 addr_reg = (void *) addr->start;
1738 addr_reg = ioremap(addr->start, resource_len(addr));
1740 if (addr_reg == NULL) {
1745 if (!request_mem_region(data->start, 1, hcd_name)) {
1750 #ifdef CONFIG_USB_SL811_CS
1751 data_reg = (void *) data->start;
1753 data_reg = ioremap(data->start, resource_len(data));
1755 if (data_reg == NULL) {
1760 DBG("SL811_HCD: addr_reg: 0x%04x, data_reg: 0x%04x\n",
1761 (int) addr_reg, (int) data_reg);
1763 /* allocate and initialize hcd */
1764 hcd = usb_create_hcd(&sl811h_hc_driver, dev, dev->bus_id);
1769 hcd->rsrc_start = addr->start;
1771 sl811 = hcd_to_sl811(hcd);
1773 spin_lock_init(&sl811->lock);
1774 INIT_LIST_HEAD(&sl811->async);
1775 sl811->board = dev->platform_data;
1776 init_timer(&sl811->timer);
1777 sl811->timer.function = sl811h_timer;
1778 sl811->timer.data = (unsigned long) sl811;
1779 sl811->addr_reg = addr_reg;
1780 sl811->data_reg = data_reg;
1782 if (test_registers(sl811) == 0) {
1783 DBG("ERROR: register test failed!\n");
1786 else DBG("sl811 register test passed.\n");
1789 spin_lock_irq(&sl811->lock);
1790 port_power(sl811, 0);
1791 spin_unlock_irq(&sl811->lock);
1794 tmp = sl811_read(sl811, SL11H_HWREVREG);
1797 hcd->product_desc = "SL811HS v1.2";
1800 hcd->product_desc = "SL811HS v1.5";
1803 /* reject case 0, SL11S is less functional */
1804 DBG("chiprev %02x\n", tmp);
1808 /* sl811s would need a different handler for this irq */
1810 #if !defined(CONFIG_USB_SL811_CS) && defined(CONFIG_ARM)
1811 /* Cypress docs say the IRQ is IRQT_HIGH ... */
1812 set_irq_type(irq, IRQT_RISING);
1815 retval = usb_add_hcd(hcd, irq, SA_INTERRUPT);
1819 INFO("%s, irq %d\n", hcd->product_desc, irq);
1821 create_debug_file(sl811);
1822 DBG("sl811_hcd initialized\n");
1829 #ifndef CONFIG_USB_SL811_CS
1833 release_mem_region(data->start, 1);
1835 #ifndef CONFIG_USB_SL811_CS
1839 release_mem_region(addr->start, 1);
1842 DBG("init error, %d\n", retval);
1845 EXPORT_SYMBOL (sl811h_probe);
1849 /* for this device there's no useful distinction between the controller
1850 * and its root hub, except that the root hub only gets direct PM calls
1851 * when CONFIG_USB_SUSPEND is enabled.
1855 sl811h_suspend(struct device *dev, pm_message_t state, u32 phase)
1857 struct usb_hcd *hcd = dev_get_drvdata(dev);
1858 struct sl811 *sl811 = hcd_to_sl811(hcd);
1861 if (phase != SUSPEND_POWER_DOWN)
1864 if (state <= PM_SUSPEND_MEM)
1865 retval = sl811h_hub_suspend(hcd);
1867 port_power(sl811, 0);
1869 dev->power.power_state = state;
1874 sl811h_resume(struct device *dev, u32 phase)
1876 struct usb_hcd *hcd = dev_get_drvdata(dev);
1877 struct sl811 *sl811 = hcd_to_sl811(hcd);
1879 if (phase != RESUME_POWER_ON)
1882 /* with no "check to see if VBUS is still powered" board hook,
1883 * let's assume it'd only be powered to enable remote wakeup.
1885 if (dev->power.power_state > PM_SUSPEND_MEM
1886 || !hcd->can_wakeup) {
1888 port_power(sl811, 1);
1892 dev->power.power_state = PMSG_ON;
1893 return sl811h_hub_resume(hcd);
1898 #define sl811h_suspend NULL
1899 #define sl811h_resume NULL
1904 struct device_driver sl811h_driver = {
1905 .name = (char *) hcd_name,
1906 .bus = &platform_bus_type,
1908 .probe = sl811h_probe,
1909 .remove = sl811h_remove,
1911 .suspend = sl811h_suspend,
1912 .resume = sl811h_resume,
1914 EXPORT_SYMBOL(sl811h_driver);
1916 /*-------------------------------------------------------------------------*/
1918 static int __init sl811h_init(void)
1923 INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
1924 return driver_register(&sl811h_driver);
1926 module_init(sl811h_init);
1928 static void __exit sl811h_cleanup(void)
1930 driver_unregister(&sl811h_driver);
1932 module_exit(sl811h_cleanup);