2 * SL811HS HCD (Host Controller Driver) for USB.
4 * Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
5 * Copyright (C) 2004-2005 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>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/kernel.h>
38 #include <linux/delay.h>
39 #include <linux/ioport.h>
40 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <linux/smp_lock.h>
43 #include <linux/errno.h>
44 #include <linux/init.h>
45 #include <linux/timer.h>
46 #include <linux/list.h>
47 #include <linux/interrupt.h>
48 #include <linux/usb.h>
49 #include <linux/usb_sl811.h>
50 #include <linux/platform_device.h>
54 #include <asm/system.h>
55 #include <asm/byteorder.h>
57 #include "../core/hcd.h"
61 MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
62 MODULE_LICENSE("GPL");
64 #define DRIVER_VERSION "19 May 2005"
68 # define STUB_DEBUG_FILE
71 /* for now, use only one transfer register bank */
74 /* this doesn't understand urb->iso_frame_desc[], but if you had a driver
75 * that just queued one ISO frame per URB then iso transfers "should" work
76 * using the normal urb status fields.
83 static const char hcd_name[] = "sl811-hcd";
85 /*-------------------------------------------------------------------------*/
87 static void port_power(struct sl811 *sl811, int is_on)
89 struct usb_hcd *hcd = sl811_to_hcd(sl811);
91 /* hub is inactive unless the port is powered */
93 if (sl811->port1 & (1 << USB_PORT_FEAT_POWER))
96 sl811->port1 = (1 << USB_PORT_FEAT_POWER);
97 sl811->irq_enable = SL11H_INTMASK_INSRMV;
98 hcd->self.controller->power.power_state = PMSG_ON;
101 sl811->irq_enable = 0;
102 hcd->state = HC_STATE_HALT;
103 hcd->self.controller->power.power_state = PMSG_SUSPEND;
106 sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
107 sl811_write(sl811, SL11H_IRQ_STATUS, ~0);
109 if (sl811->board && sl811->board->port_power) {
110 /* switch VBUS, at 500mA unless hub power budget gets set */
111 DBG("power %s\n", is_on ? "on" : "off");
112 sl811->board->port_power(hcd->self.controller, is_on);
115 /* reset as thoroughly as we can */
116 if (sl811->board && sl811->board->reset)
117 sl811->board->reset(hcd->self.controller);
119 sl811_write(sl811, SL11H_CTLREG1, SL11H_CTL1MASK_SE0);
123 sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
124 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
125 sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT);
126 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
128 // if !is_on, put into lowpower mode now
131 /*-------------------------------------------------------------------------*/
133 /* This is a PIO-only HCD. Queueing appends URBs to the endpoint's queue,
134 * and may start I/O. Endpoint queues are scanned during completion irq
135 * handlers (one per packet: ACK, NAK, faults, etc) and urb cancellation.
137 * Using an external DMA engine to copy a packet at a time could work,
138 * though setup/teardown costs may be too big to make it worthwhile.
141 /* SETUP starts a new control request. Devices are not allowed to
142 * STALL or NAK these; they must cancel any pending control requests.
144 static void setup_packet(
146 struct sl811h_ep *ep,
154 void __iomem *data_reg;
156 addr = SL811HS_PACKET_BUF(bank == 0);
157 len = sizeof(struct usb_ctrlrequest);
158 data_reg = sl811->data_reg;
159 sl811_write_buf(sl811, addr, urb->setup_packet, len);
161 /* autoincrementing */
162 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
163 writeb(len, data_reg);
164 writeb(SL_SETUP /* | ep->epnum */, data_reg);
165 writeb(usb_pipedevice(urb->pipe), data_reg);
167 /* always OUT/data0 */ ;
168 sl811_write(sl811, bank + SL11H_HOSTCTLREG,
169 control | SL11H_HCTLMASK_OUT);
171 PACKET("SETUP qh%p\n", ep);
174 /* STATUS finishes control requests, often after IN or OUT data packets */
175 static void status_packet(
177 struct sl811h_ep *ep,
184 void __iomem *data_reg;
186 do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe);
187 data_reg = sl811->data_reg;
189 /* autoincrementing */
190 sl811_write(sl811, bank + SL11H_BUFADDRREG, 0);
192 writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg);
193 writeb(usb_pipedevice(urb->pipe), data_reg);
195 /* always data1; sometimes IN */
196 control |= SL11H_HCTLMASK_TOGGLE;
198 control |= SL11H_HCTLMASK_OUT;
199 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
201 PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "",
202 do_out ? "out" : "in", ep);
205 /* IN packets can be used with any type of endpoint. here we just
206 * start the transfer, data from the peripheral may arrive later.
207 * urb->iso_frame_desc is currently ignored here...
209 static void in_packet(
211 struct sl811h_ep *ep,
219 void __iomem *data_reg;
221 /* avoid losing data on overflow */
223 addr = SL811HS_PACKET_BUF(bank == 0);
224 if (!(control & SL11H_HCTLMASK_ISOCH)
225 && usb_gettoggle(urb->dev, ep->epnum, 0))
226 control |= SL11H_HCTLMASK_TOGGLE;
227 data_reg = sl811->data_reg;
229 /* autoincrementing */
230 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
231 writeb(len, data_reg);
232 writeb(SL_IN | ep->epnum, data_reg);
233 writeb(usb_pipedevice(urb->pipe), data_reg);
235 sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
236 ep->length = min((int)len,
237 urb->transfer_buffer_length - urb->actual_length);
238 PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
239 !!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len);
242 /* OUT packets can be used with any type of endpoint.
243 * urb->iso_frame_desc is currently ignored here...
245 static void out_packet(
247 struct sl811h_ep *ep,
256 void __iomem *data_reg;
258 buf = urb->transfer_buffer + urb->actual_length;
261 len = min((int)ep->maxpacket,
262 urb->transfer_buffer_length - urb->actual_length);
264 if (!(control & SL11H_HCTLMASK_ISOCH)
265 && usb_gettoggle(urb->dev, ep->epnum, 1))
266 control |= SL11H_HCTLMASK_TOGGLE;
267 addr = SL811HS_PACKET_BUF(bank == 0);
268 data_reg = sl811->data_reg;
270 sl811_write_buf(sl811, addr, buf, len);
272 /* autoincrementing */
273 sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
274 writeb(len, data_reg);
275 writeb(SL_OUT | ep->epnum, data_reg);
276 writeb(usb_pipedevice(urb->pipe), data_reg);
278 sl811_write(sl811, bank + SL11H_HOSTCTLREG,
279 control | SL11H_HCTLMASK_OUT);
281 PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
282 !!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len);
285 /*-------------------------------------------------------------------------*/
287 /* caller updates on-chip enables later */
289 static inline void sofirq_on(struct sl811 *sl811)
291 if (sl811->irq_enable & SL11H_INTMASK_SOFINTR)
293 VDBG("sof irq on\n");
294 sl811->irq_enable |= SL11H_INTMASK_SOFINTR;
297 static inline void sofirq_off(struct sl811 *sl811)
299 if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR))
301 VDBG("sof irq off\n");
302 sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR;
305 /*-------------------------------------------------------------------------*/
307 /* pick the next endpoint for a transaction, and issue it.
308 * frames start with periodic transfers (after whatever is pending
309 * from the previous frame), and the rest of the time is async
310 * transfers, scheduled round-robin.
312 static struct sl811h_ep *start(struct sl811 *sl811, u8 bank)
314 struct sl811h_ep *ep;
319 /* use endpoint at schedule head */
320 if (sl811->next_periodic) {
321 ep = sl811->next_periodic;
322 sl811->next_periodic = ep->next;
324 if (sl811->next_async)
325 ep = sl811->next_async;
326 else if (!list_empty(&sl811->async))
327 ep = container_of(sl811->async.next,
328 struct sl811h_ep, schedule);
330 /* could set up the first fullspeed periodic
331 * transfer for the next frame ...
337 if ((bank && sl811->active_b == ep) || sl811->active_a == ep)
341 if (ep->schedule.next == &sl811->async)
342 sl811->next_async = NULL;
344 sl811->next_async = container_of(ep->schedule.next,
345 struct sl811h_ep, schedule);
348 if (unlikely(list_empty(&ep->hep->urb_list))) {
349 DBG("empty %p queue?\n", ep);
353 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
354 control = ep->defctrl;
356 /* if this frame doesn't have enough time left to transfer this
357 * packet, wait till the next frame. too-simple algorithm...
359 fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6;
360 fclock -= 100; /* setup takes not much time */
361 if (urb->dev->speed == USB_SPEED_LOW) {
362 if (control & SL11H_HCTLMASK_PREAMBLE) {
363 /* also note erratum 1: some hubs won't work */
366 fclock -= ep->maxpacket << 8;
368 /* erratum 2: AFTERSOF only works for fullspeed */
371 sl811->stat_overrun++;
376 fclock -= 12000 / 19; /* 19 64byte packets/msec */
379 sl811->stat_overrun++;
380 control |= SL11H_HCTLMASK_AFTERSOF;
382 /* throttle bulk/control irq noise */
383 } else if (ep->nak_count)
384 control |= SL11H_HCTLMASK_AFTERSOF;
388 switch (ep->nextpid) {
390 in_packet(sl811, ep, urb, bank, control);
393 out_packet(sl811, ep, urb, bank, control);
396 setup_packet(sl811, ep, urb, bank, control);
398 case USB_PID_ACK: /* for control status */
399 status_packet(sl811, ep, urb, bank, control);
402 DBG("bad ep%p pid %02x\n", ep, ep->nextpid);
408 #define MIN_JIFFIES ((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2)
410 static inline void start_transfer(struct sl811 *sl811)
412 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
414 if (sl811->active_a == NULL) {
415 sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF));
416 if (sl811->active_a != NULL)
417 sl811->jiffies_a = jiffies + MIN_JIFFIES;
420 if (sl811->active_b == NULL) {
421 sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF));
422 if (sl811->active_b != NULL)
423 sl811->jiffies_b = jiffies + MIN_JIFFIES;
428 static void finish_request(
430 struct sl811h_ep *ep,
432 struct pt_regs *regs,
434 ) __releases(sl811->lock) __acquires(sl811->lock)
438 if (usb_pipecontrol(urb->pipe))
439 ep->nextpid = USB_PID_SETUP;
441 spin_lock(&urb->lock);
442 if (urb->status == -EINPROGRESS)
443 urb->status = status;
445 spin_unlock(&urb->lock);
447 spin_unlock(&sl811->lock);
448 usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, regs);
449 spin_lock(&sl811->lock);
451 /* leave active endpoints in the schedule */
452 if (!list_empty(&ep->hep->urb_list))
455 /* async deschedule? */
456 if (!list_empty(&ep->schedule)) {
457 list_del_init(&ep->schedule);
458 if (ep == sl811->next_async)
459 sl811->next_async = NULL;
463 /* periodic deschedule */
464 DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
465 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
466 struct sl811h_ep *temp;
467 struct sl811h_ep **prev = &sl811->periodic[i];
469 while (*prev && ((temp = *prev) != ep))
473 sl811->load[i] -= ep->load;
475 ep->branch = PERIODIC_SIZE;
476 sl811->periodic_count--;
477 sl811_to_hcd(sl811)->self.bandwidth_allocated
478 -= ep->load / ep->period;
479 if (ep == sl811->next_periodic)
480 sl811->next_periodic = ep->next;
482 /* we might turn SOFs back on again for the async schedule */
483 if (sl811->periodic_count == 0)
488 done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank, struct pt_regs *regs)
492 int urbstat = -EINPROGRESS;
497 status = sl811_read(sl811, bank + SL11H_PKTSTATREG);
499 urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
501 /* we can safely ignore NAKs */
502 if (status & SL11H_STATMASK_NAK) {
503 // PACKET("...NAK_%02x qh%p\n", bank, ep);
508 /* ACK advances transfer, toggle, and maybe queue */
509 } else if (status & SL11H_STATMASK_ACK) {
510 struct usb_device *udev = urb->dev;
514 /* urb->iso_frame_desc is currently ignored here... */
516 ep->nak_count = ep->error_count = 0;
517 switch (ep->nextpid) {
519 // PACKET("...ACK/out_%02x qh%p\n", bank, ep);
520 urb->actual_length += ep->length;
521 usb_dotoggle(udev, ep->epnum, 1);
522 if (urb->actual_length
523 == urb->transfer_buffer_length) {
524 if (usb_pipecontrol(urb->pipe))
525 ep->nextpid = USB_PID_ACK;
527 /* some bulk protocols terminate OUT transfers
528 * by a short packet, using ZLPs not padding.
530 else if (ep->length < ep->maxpacket
531 || !(urb->transfer_flags
537 // PACKET("...ACK/in_%02x qh%p\n", bank, ep);
538 buf = urb->transfer_buffer + urb->actual_length;
540 len = ep->maxpacket - sl811_read(sl811,
541 bank + SL11H_XFERCNTREG);
542 if (len > ep->length) {
544 urb->status = -EOVERFLOW;
546 urb->actual_length += len;
547 sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
549 usb_dotoggle(udev, ep->epnum, 0);
550 if (urb->actual_length == urb->transfer_buffer_length)
552 else if (len < ep->maxpacket) {
553 if (urb->transfer_flags & URB_SHORT_NOT_OK)
554 urbstat = -EREMOTEIO;
558 if (usb_pipecontrol(urb->pipe)
559 && (urbstat == -EREMOTEIO
562 /* NOTE if the status stage STALLs (why?),
563 * this reports the wrong urb status.
565 spin_lock(&urb->lock);
566 if (urb->status == -EINPROGRESS)
567 urb->status = urbstat;
568 spin_unlock(&urb->lock);
571 ep->nextpid = USB_PID_ACK;
575 // PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
576 if (urb->transfer_buffer_length == urb->actual_length)
577 ep->nextpid = USB_PID_ACK;
578 else if (usb_pipeout(urb->pipe)) {
579 usb_settoggle(udev, 0, 1, 1);
580 ep->nextpid = USB_PID_OUT;
582 usb_settoggle(udev, 0, 0, 1);
583 ep->nextpid = USB_PID_IN;
587 // PACKET("...ACK/status_%02x qh%p\n", bank, ep);
592 /* STALL stops all transfers */
593 } else if (status & SL11H_STATMASK_STALL) {
594 PACKET("...STALL_%02x qh%p\n", bank, ep);
595 ep->nak_count = ep->error_count = 0;
598 /* error? retry, until "3 strikes" */
599 } else if (++ep->error_count >= 3) {
600 if (status & SL11H_STATMASK_TMOUT)
601 urbstat = -ETIMEDOUT;
602 else if (status & SL11H_STATMASK_OVF)
603 urbstat = -EOVERFLOW;
607 PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
608 bank, status, ep, urbstat);
611 if (urb && (urbstat != -EINPROGRESS || urb->status != -EINPROGRESS))
612 finish_request(sl811, ep, urb, regs, urbstat);
615 static inline u8 checkdone(struct sl811 *sl811)
620 if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
621 ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
622 if (ctl & SL11H_HCTLMASK_ARM)
623 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
624 DBG("%s DONE_A: ctrl %02x sts %02x\n",
625 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
627 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
628 irqstat |= SL11H_INTMASK_DONE_A;
631 if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
632 ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
633 if (ctl & SL11H_HCTLMASK_ARM)
634 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
635 DBG("%s DONE_B: ctrl %02x sts %02x\n",
636 (ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
638 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
639 irqstat |= SL11H_INTMASK_DONE_A;
645 static irqreturn_t sl811h_irq(struct usb_hcd *hcd, struct pt_regs *regs)
647 struct sl811 *sl811 = hcd_to_sl811(hcd);
649 irqreturn_t ret = IRQ_NONE;
650 unsigned retries = 5;
652 spin_lock(&sl811->lock);
655 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
657 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
658 irqstat &= sl811->irq_enable;
662 /* this may no longer be necessary ... */
664 irqstat = checkdone(sl811);
670 /* USB packets, not necessarily handled in the order they're
671 * issued ... that's fine if they're different endpoints.
673 if (irqstat & SL11H_INTMASK_DONE_A) {
674 done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF), regs);
675 sl811->active_a = NULL;
679 if (irqstat & SL11H_INTMASK_DONE_B) {
680 done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF), regs);
681 sl811->active_b = NULL;
685 if (irqstat & SL11H_INTMASK_SOFINTR) {
688 index = sl811->frame++ % (PERIODIC_SIZE - 1);
691 /* be graceful about almost-inevitable periodic schedule
692 * overruns: continue the previous frame's transfers iff
693 * this one has nothing scheduled.
695 if (sl811->next_periodic) {
696 // ERR("overrun to slot %d\n", index);
697 sl811->stat_overrun++;
699 if (sl811->periodic[index])
700 sl811->next_periodic = sl811->periodic[index];
703 /* khubd manages debouncing and wakeup */
704 if (irqstat & SL11H_INTMASK_INSRMV) {
705 sl811->stat_insrmv++;
707 /* most stats are reset for each VBUS session */
708 sl811->stat_wake = 0;
712 sl811->stat_lost = 0;
715 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
717 sl811->irq_enable = SL11H_INTMASK_INSRMV;
718 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
720 /* usbcore nukes other pending transactions on disconnect */
721 if (sl811->active_a) {
722 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
723 finish_request(sl811, sl811->active_a,
724 container_of(sl811->active_a
725 ->hep->urb_list.next,
726 struct urb, urb_list),
728 sl811->active_a = NULL;
731 if (sl811->active_b) {
732 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
733 finish_request(sl811, sl811->active_b,
734 container_of(sl811->active_b
735 ->hep->urb_list.next,
736 struct urb, urb_list),
738 sl811->active_b = NULL;
742 /* port status seems weird 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 * NOTE: the periodic schedule is a sparse tree, with the load for
781 * each branch minimized. see fig 3.5 in the OHCI spec for example.
783 #define MAX_PERIODIC_LOAD 500 /* out of 1000 usec */
785 static int balance(struct sl811 *sl811, u16 period, u16 load)
787 int i, branch = -ENOSPC;
789 /* search for the least loaded schedule branch of that period
790 * which has enough bandwidth left unreserved.
792 for (i = 0; i < period ; i++) {
793 if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
796 for (j = i; j < PERIODIC_SIZE; j += period) {
797 if ((sl811->load[j] + load)
801 if (j < PERIODIC_SIZE)
809 /*-------------------------------------------------------------------------*/
811 static int sl811h_urb_enqueue(
813 struct usb_host_endpoint *hep,
817 struct sl811 *sl811 = hcd_to_sl811(hcd);
818 struct usb_device *udev = urb->dev;
819 unsigned int pipe = urb->pipe;
820 int is_out = !usb_pipein(pipe);
821 int type = usb_pipetype(pipe);
822 int epnum = usb_pipeendpoint(pipe);
823 struct sl811h_ep *ep = NULL;
829 if (type == PIPE_ISOCHRONOUS)
833 /* avoid all allocations within spinlocks */
835 ep = kzalloc(sizeof *ep, mem_flags);
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)) {
855 INIT_LIST_HEAD(&ep->schedule);
856 ep->udev = usb_get_dev(udev);
858 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
859 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
860 usb_settoggle(udev, epnum, is_out, 0);
862 if (type == PIPE_CONTROL)
863 ep->nextpid = USB_PID_SETUP;
865 ep->nextpid = USB_PID_OUT;
867 ep->nextpid = USB_PID_IN;
869 if (ep->maxpacket > H_MAXPACKET) {
870 /* iso packets up to 240 bytes could work... */
871 DBG("dev %d ep%d maxpacket %d\n",
872 udev->devnum, epnum, ep->maxpacket);
877 if (udev->speed == USB_SPEED_LOW) {
878 /* send preamble for external hub? */
879 if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
880 ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
883 case PIPE_ISOCHRONOUS:
885 if (urb->interval > PERIODIC_SIZE)
886 urb->interval = PERIODIC_SIZE;
887 ep->period = urb->interval;
888 ep->branch = PERIODIC_SIZE;
889 if (type == PIPE_ISOCHRONOUS)
890 ep->defctrl |= SL11H_HCTLMASK_ISOCH;
891 ep->load = usb_calc_bus_time(udev->speed, !is_out,
892 (type == PIPE_ISOCHRONOUS),
893 usb_maxpacket(udev, pipe, is_out))
902 /* maybe put endpoint into schedule */
906 if (list_empty(&ep->schedule))
907 list_add_tail(&ep->schedule, &sl811->async);
909 case PIPE_ISOCHRONOUS:
911 urb->interval = ep->period;
912 if (ep->branch < PERIODIC_SIZE) {
913 /* NOTE: the phase is correct here, but the value
914 * needs offsetting by the transfer queue depth.
915 * All current drivers ignore start_frame, so this
916 * is unlikely to ever matter...
918 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
923 retval = balance(sl811, ep->period, ep->load);
928 urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
931 /* sort each schedule branch by period (slow before fast)
932 * to share the faster parts of the tree without needing
933 * dummy/placeholder nodes
935 DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
936 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
937 struct sl811h_ep **prev = &sl811->periodic[i];
938 struct sl811h_ep *here = *prev;
940 while (here && ep != here) {
941 if (ep->period > here->period)
950 sl811->load[i] += ep->load;
952 sl811->periodic_count++;
953 hcd->self.bandwidth_allocated += ep->load / ep->period;
957 /* in case of unlink-during-submit */
958 spin_lock(&urb->lock);
959 if (urb->status != -EINPROGRESS) {
960 spin_unlock(&urb->lock);
961 finish_request(sl811, ep, urb, NULL, 0);
966 spin_unlock(&urb->lock);
968 start_transfer(sl811);
969 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
971 spin_unlock_irqrestore(&sl811->lock, flags);
975 static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
977 struct sl811 *sl811 = hcd_to_sl811(hcd);
978 struct usb_host_endpoint *hep;
980 struct sl811h_ep *ep;
983 spin_lock_irqsave(&sl811->lock, flags);
990 /* finish right away if this urb can't be active ...
991 * note that some drivers wrongly expect delays
993 if (ep->hep->urb_list.next != &urb->urb_list) {
994 /* not front of queue? never active */
996 /* for active transfers, we expect an IRQ */
997 } else if (sl811->active_a == ep) {
998 if (time_before_eq(sl811->jiffies_a, jiffies)) {
999 /* happens a lot with lowspeed?? */
1000 DBG("giveup on DONE_A: ctrl %02x sts %02x\n",
1002 SL811_EP_A(SL11H_HOSTCTLREG)),
1004 SL811_EP_A(SL11H_PKTSTATREG)));
1005 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
1007 sl811->active_a = NULL;
1011 } else if (sl811->active_b == ep) {
1012 if (time_before_eq(sl811->jiffies_a, jiffies)) {
1013 /* happens a lot with lowspeed?? */
1014 DBG("giveup on DONE_B: ctrl %02x sts %02x\n",
1016 SL811_EP_B(SL11H_HOSTCTLREG)),
1018 SL811_EP_B(SL11H_PKTSTATREG)));
1019 sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
1021 sl811->active_b = NULL;
1026 /* front of queue for inactive endpoint */
1030 finish_request(sl811, ep, urb, NULL, 0);
1032 VDBG("dequeue, urb %p active %s; wait4irq\n", urb,
1033 (sl811->active_a == ep) ? "A" : "B");
1037 spin_unlock_irqrestore(&sl811->lock, flags);
1042 sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1044 struct sl811h_ep *ep = hep->hcpriv;
1049 /* assume we'd just wait for the irq */
1050 if (!list_empty(&hep->urb_list))
1052 if (!list_empty(&hep->urb_list))
1053 WARN("ep %p not empty?\n", ep);
1055 usb_put_dev(ep->udev);
1061 sl811h_get_frame(struct usb_hcd *hcd)
1063 struct sl811 *sl811 = hcd_to_sl811(hcd);
1065 /* wrong except while periodic transfers are scheduled;
1066 * never matches the on-the-wire frame;
1067 * subject to overruns.
1069 return sl811->frame;
1073 /*-------------------------------------------------------------------------*/
1075 /* the virtual root hub timer IRQ checks for hub status */
1077 sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
1079 struct sl811 *sl811 = hcd_to_sl811(hcd);
1081 unsigned long flags;
1083 /* non-SMP HACK: use root hub timer as i/o watchdog
1084 * this seems essential when SOF IRQs aren't in use...
1086 local_irq_save(flags);
1087 if (!timer_pending(&sl811->timer)) {
1088 if (sl811h_irq( /* ~0, */ hcd, NULL) != IRQ_NONE)
1091 local_irq_restore(flags);
1094 if (!(sl811->port1 & (0xffff << 16)))
1097 /* tell khubd port 1 changed */
1103 sl811h_hub_descriptor (
1104 struct sl811 *sl811,
1105 struct usb_hub_descriptor *desc
1109 desc->bDescriptorType = 0x29;
1110 desc->bHubContrCurrent = 0;
1112 desc->bNbrPorts = 1;
1113 desc->bDescLength = 9;
1115 /* per-port power switching (gang of one!), or none */
1116 desc->bPwrOn2PwrGood = 0;
1117 if (sl811->board && sl811->board->port_power) {
1118 desc->bPwrOn2PwrGood = sl811->board->potpg;
1119 if (!desc->bPwrOn2PwrGood)
1120 desc->bPwrOn2PwrGood = 10;
1125 /* no overcurrent errors detection/handling */
1128 desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp);
1130 /* two bitmaps: ports removable, and legacy PortPwrCtrlMask */
1131 desc->bitmap[0] = 0 << 1;
1132 desc->bitmap[1] = ~0;
1136 sl811h_timer(unsigned long _sl811)
1138 struct sl811 *sl811 = (void *) _sl811;
1139 unsigned long flags;
1141 u8 signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
1142 const u32 mask = (1 << USB_PORT_FEAT_CONNECTION)
1143 | (1 << USB_PORT_FEAT_ENABLE)
1144 | (1 << USB_PORT_FEAT_LOWSPEED);
1146 spin_lock_irqsave(&sl811->lock, flags);
1148 /* stop special signaling */
1149 sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
1150 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1153 irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);
1155 switch (signaling) {
1156 case SL11H_CTL1MASK_SE0:
1158 sl811->port1 = (1 << USB_PORT_FEAT_C_RESET)
1159 | (1 << USB_PORT_FEAT_POWER);
1161 /* don't wrongly ack RD */
1162 if (irqstat & SL11H_INTMASK_INSRMV)
1163 irqstat &= ~SL11H_INTMASK_RD;
1165 case SL11H_CTL1MASK_K:
1166 DBG("end resume\n");
1167 sl811->port1 &= ~(1 << USB_PORT_FEAT_SUSPEND);
1170 DBG("odd timer signaling: %02x\n", signaling);
1173 sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
1175 if (irqstat & SL11H_INTMASK_RD) {
1176 /* usbcore nukes all pending transactions on disconnect */
1177 if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION))
1178 sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
1179 | (1 << USB_PORT_FEAT_C_ENABLE);
1180 sl811->port1 &= ~mask;
1181 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1183 sl811->port1 |= mask;
1184 if (irqstat & SL11H_INTMASK_DP)
1185 sl811->port1 &= ~(1 << USB_PORT_FEAT_LOWSPEED);
1186 sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
1189 if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION)) {
1190 u8 ctrl2 = SL811HS_CTL2_INIT;
1192 sl811->irq_enable |= SL11H_INTMASK_DONE_A;
1194 sl811->irq_enable |= SL11H_INTMASK_DONE_B;
1196 if (sl811->port1 & (1 << USB_PORT_FEAT_LOWSPEED)) {
1197 sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
1198 ctrl2 |= SL811HS_CTL2MASK_DSWAP;
1201 /* start SOFs flowing, kickstarting with A registers */
1202 sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
1203 sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
1204 sl811_write(sl811, SL811HS_CTLREG2, ctrl2);
1206 /* autoincrementing */
1207 sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
1208 writeb(SL_SOF, sl811->data_reg);
1209 writeb(0, sl811->data_reg);
1210 sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
1211 SL11H_HCTLMASK_ARM);
1213 /* khubd provides debounce delay */
1217 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1220 sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
1221 spin_unlock_irqrestore(&sl811->lock, flags);
1226 struct usb_hcd *hcd,
1233 struct sl811 *sl811 = hcd_to_sl811(hcd);
1235 unsigned long flags;
1237 spin_lock_irqsave(&sl811->lock, flags);
1240 case ClearHubFeature:
1243 case C_HUB_OVER_CURRENT:
1244 case C_HUB_LOCAL_POWER:
1250 case ClearPortFeature:
1251 if (wIndex != 1 || wLength != 0)
1255 case USB_PORT_FEAT_ENABLE:
1256 sl811->port1 &= (1 << USB_PORT_FEAT_POWER);
1258 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1259 sl811->irq_enable = SL11H_INTMASK_INSRMV;
1260 sl811_write(sl811, SL11H_IRQ_ENABLE,
1263 case USB_PORT_FEAT_SUSPEND:
1264 if (!(sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)))
1267 /* 20 msec of resume/K signaling, other irqs blocked */
1268 DBG("start resume...\n");
1269 sl811->irq_enable = 0;
1270 sl811_write(sl811, SL11H_IRQ_ENABLE,
1272 sl811->ctrl1 |= SL11H_CTL1MASK_K;
1273 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1275 mod_timer(&sl811->timer, jiffies
1276 + msecs_to_jiffies(20));
1278 case USB_PORT_FEAT_POWER:
1279 port_power(sl811, 0);
1281 case USB_PORT_FEAT_C_ENABLE:
1282 case USB_PORT_FEAT_C_SUSPEND:
1283 case USB_PORT_FEAT_C_CONNECTION:
1284 case USB_PORT_FEAT_C_OVER_CURRENT:
1285 case USB_PORT_FEAT_C_RESET:
1290 sl811->port1 &= ~(1 << wValue);
1292 case GetHubDescriptor:
1293 sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
1296 *(__le32 *) buf = cpu_to_le32(0);
1301 *(__le32 *) buf = cpu_to_le32(sl811->port1);
1304 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
1306 DBG("GetPortStatus %08x\n", sl811->port1);
1308 case SetPortFeature:
1309 if (wIndex != 1 || wLength != 0)
1312 case USB_PORT_FEAT_SUSPEND:
1313 if (sl811->port1 & (1 << USB_PORT_FEAT_RESET))
1315 if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE)))
1318 DBG("suspend...\n");
1319 sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
1320 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1322 case USB_PORT_FEAT_POWER:
1323 port_power(sl811, 1);
1325 case USB_PORT_FEAT_RESET:
1326 if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
1328 if (!(sl811->port1 & (1 << USB_PORT_FEAT_POWER)))
1331 /* 50 msec of reset/SE0 signaling, irqs blocked */
1332 sl811->irq_enable = 0;
1333 sl811_write(sl811, SL11H_IRQ_ENABLE,
1335 sl811->ctrl1 = SL11H_CTL1MASK_SE0;
1336 sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
1337 sl811->port1 |= (1 << USB_PORT_FEAT_RESET);
1338 mod_timer(&sl811->timer, jiffies
1339 + msecs_to_jiffies(50));
1344 sl811->port1 |= 1 << wValue;
1349 /* "protocol stall" on error */
1353 spin_unlock_irqrestore(&sl811->lock, flags);
1360 sl811h_bus_suspend(struct usb_hcd *hcd)
1363 DBG("%s\n", __FUNCTION__);
1368 sl811h_bus_resume(struct usb_hcd *hcd)
1371 DBG("%s\n", __FUNCTION__);
1377 #define sl811h_bus_suspend NULL
1378 #define sl811h_bus_resume NULL
1383 /*-------------------------------------------------------------------------*/
1385 #ifdef STUB_DEBUG_FILE
1387 static inline void create_debug_file(struct sl811 *sl811) { }
1388 static inline void remove_debug_file(struct sl811 *sl811) { }
1392 #include <linux/proc_fs.h>
1393 #include <linux/seq_file.h>
1395 static void dump_irq(struct seq_file *s, char *label, u8 mask)
1397 seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
1398 (mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
1399 (mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
1400 (mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
1401 (mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
1402 (mask & SL11H_INTMASK_RD) ? " rd" : "",
1403 (mask & SL11H_INTMASK_DP) ? " dp" : "");
1406 static int proc_sl811h_show(struct seq_file *s, void *unused)
1408 struct sl811 *sl811 = s->private;
1409 struct sl811h_ep *ep;
1412 seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
1413 sl811_to_hcd(sl811)->product_desc,
1414 hcd_name, DRIVER_VERSION,
1417 seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
1418 seq_printf(s, "current session: done_a %ld done_b %ld "
1419 "wake %ld sof %ld overrun %ld lost %ld\n\n",
1420 sl811->stat_a, sl811->stat_b,
1421 sl811->stat_wake, sl811->stat_sof,
1422 sl811->stat_overrun, sl811->stat_lost);
1424 spin_lock_irq(&sl811->lock);
1426 if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
1427 seq_printf(s, "(suspended)\n\n");
1429 u8 t = sl811_read(sl811, SL11H_CTLREG1);
1431 seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
1432 (t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
1433 ({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
1434 case SL11H_CTL1MASK_NORMAL: s = ""; break;
1435 case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
1436 case SL11H_CTL1MASK_K: s = " k/resume"; break;
1437 default: s = "j"; break;
1439 (t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
1440 (t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");
1442 dump_irq(s, "irq_enable",
1443 sl811_read(sl811, SL11H_IRQ_ENABLE));
1444 dump_irq(s, "irq_status",
1445 sl811_read(sl811, SL11H_IRQ_STATUS));
1446 seq_printf(s, "frame clocks remaining: %d\n",
1447 sl811_read(sl811, SL11H_SOFTMRREG) << 6);
1450 seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
1451 sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
1452 sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
1453 seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
1454 sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
1455 sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
1456 seq_printf(s, "\n");
1457 list_for_each_entry (ep, &sl811->async, schedule) {
1460 seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
1462 (ep == sl811->active_a) ? "(A) " : "",
1463 (ep == sl811->active_b) ? "(B) " : "",
1465 ({ char *s; switch (ep->nextpid) {
1466 case USB_PID_IN: s = "in"; break;
1467 case USB_PID_OUT: s = "out"; break;
1468 case USB_PID_SETUP: s = "setup"; break;
1469 case USB_PID_ACK: s = "status"; break;
1470 default: s = "?"; break;
1473 ep->nak_count, ep->error_count);
1474 list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
1475 seq_printf(s, " urb%p, %d/%d\n", urb,
1477 urb->transfer_buffer_length);
1480 if (!list_empty(&sl811->async))
1481 seq_printf(s, "\n");
1483 seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
1485 for (i = 0; i < PERIODIC_SIZE; i++) {
1486 ep = sl811->periodic[i];
1489 seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);
1491 /* DUMB: prints shared entries multiple times */
1494 " %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
1496 (ep == sl811->active_a) ? "(A) " : "",
1497 (ep == sl811->active_b) ? "(B) " : "",
1499 (ep->udev->speed == USB_SPEED_FULL)
1501 ep->udev->devnum, ep->epnum,
1502 (ep->epnum == 0) ? ""
1503 : ((ep->nextpid == USB_PID_IN)
1506 ep->maxpacket, ep->error_count);
1511 spin_unlock_irq(&sl811->lock);
1512 seq_printf(s, "\n");
1517 static int proc_sl811h_open(struct inode *inode, struct file *file)
1519 return single_open(file, proc_sl811h_show, PDE(inode)->data);
1522 static struct file_operations proc_ops = {
1523 .open = proc_sl811h_open,
1525 .llseek = seq_lseek,
1526 .release = single_release,
1529 /* expect just one sl811 per system */
1530 static const char proc_filename[] = "driver/sl811h";
1532 static void create_debug_file(struct sl811 *sl811)
1534 struct proc_dir_entry *pde;
1536 pde = create_proc_entry(proc_filename, 0, NULL);
1540 pde->proc_fops = &proc_ops;
1545 static void remove_debug_file(struct sl811 *sl811)
1548 remove_proc_entry(proc_filename, NULL);
1553 /*-------------------------------------------------------------------------*/
1556 sl811h_stop(struct usb_hcd *hcd)
1558 struct sl811 *sl811 = hcd_to_sl811(hcd);
1559 unsigned long flags;
1561 del_timer_sync(&hcd->rh_timer);
1563 spin_lock_irqsave(&sl811->lock, flags);
1564 port_power(sl811, 0);
1565 spin_unlock_irqrestore(&sl811->lock, flags);
1569 sl811h_start(struct usb_hcd *hcd)
1571 struct sl811 *sl811 = hcd_to_sl811(hcd);
1573 /* chip has been reset, VBUS power is off */
1574 hcd->state = HC_STATE_RUNNING;
1577 if (!device_can_wakeup(hcd->self.controller))
1578 device_init_wakeup(hcd->self.controller,
1579 sl811->board->can_wakeup);
1580 hcd->power_budget = sl811->board->power * 2;
1583 /* enable power and interupts */
1584 port_power(sl811, 1);
1589 /*-------------------------------------------------------------------------*/
1591 static struct hc_driver sl811h_hc_driver = {
1592 .description = hcd_name,
1593 .hcd_priv_size = sizeof(struct sl811),
1596 * generic hardware linkage
1599 .flags = HCD_USB11 | HCD_MEMORY,
1601 /* Basic lifecycle operations */
1602 .start = sl811h_start,
1603 .stop = sl811h_stop,
1606 * managing i/o requests and associated device resources
1608 .urb_enqueue = sl811h_urb_enqueue,
1609 .urb_dequeue = sl811h_urb_dequeue,
1610 .endpoint_disable = sl811h_endpoint_disable,
1613 * periodic schedule support
1615 .get_frame_number = sl811h_get_frame,
1620 .hub_status_data = sl811h_hub_status_data,
1621 .hub_control = sl811h_hub_control,
1622 .bus_suspend = sl811h_bus_suspend,
1623 .bus_resume = sl811h_bus_resume,
1626 /*-------------------------------------------------------------------------*/
1628 static int __devexit
1629 sl811h_remove(struct platform_device *dev)
1631 struct usb_hcd *hcd = platform_get_drvdata(dev);
1632 struct sl811 *sl811 = hcd_to_sl811(hcd);
1633 struct resource *res;
1635 remove_debug_file(sl811);
1636 usb_remove_hcd(hcd);
1638 /* some platforms may use IORESOURCE_IO */
1639 res = platform_get_resource(dev, IORESOURCE_MEM, 1);
1641 iounmap(sl811->data_reg);
1643 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1645 iounmap(sl811->addr_reg);
1651 static int __devinit
1652 sl811h_probe(struct platform_device *dev)
1654 struct usb_hcd *hcd;
1655 struct sl811 *sl811;
1656 struct resource *addr, *data;
1658 void __iomem *addr_reg;
1659 void __iomem *data_reg;
1663 /* basic sanity checks first. board-specific init logic should
1664 * have initialized these three resources and probably board
1665 * specific platform_data. we don't probe for IRQs, and do only
1666 * minimal sanity checking.
1668 irq = platform_get_irq(dev, 0);
1669 if (dev->num_resources < 3 || irq < 0)
1672 /* refuse to confuse usbcore */
1673 if (dev->dev.dma_mask) {
1674 DBG("no we won't dma\n");
1678 /* the chip may be wired for either kind of addressing */
1679 addr = platform_get_resource(dev, IORESOURCE_MEM, 0);
1680 data = platform_get_resource(dev, IORESOURCE_MEM, 1);
1682 if (!addr || !data) {
1683 addr = platform_get_resource(dev, IORESOURCE_IO, 0);
1684 data = platform_get_resource(dev, IORESOURCE_IO, 1);
1689 addr_reg = (void __iomem *) addr->start;
1690 data_reg = (void __iomem *) data->start;
1692 addr_reg = ioremap(addr->start, 1);
1693 if (addr_reg == NULL) {
1698 data_reg = ioremap(data->start, 1);
1699 if (data_reg == NULL) {
1705 /* allocate and initialize hcd */
1706 hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev->dev.bus_id);
1711 hcd->rsrc_start = addr->start;
1712 sl811 = hcd_to_sl811(hcd);
1714 spin_lock_init(&sl811->lock);
1715 INIT_LIST_HEAD(&sl811->async);
1716 sl811->board = dev->dev.platform_data;
1717 init_timer(&sl811->timer);
1718 sl811->timer.function = sl811h_timer;
1719 sl811->timer.data = (unsigned long) sl811;
1720 sl811->addr_reg = addr_reg;
1721 sl811->data_reg = data_reg;
1723 spin_lock_irq(&sl811->lock);
1724 port_power(sl811, 0);
1725 spin_unlock_irq(&sl811->lock);
1728 tmp = sl811_read(sl811, SL11H_HWREVREG);
1731 hcd->product_desc = "SL811HS v1.2";
1734 hcd->product_desc = "SL811HS v1.5";
1737 /* reject case 0, SL11S is less functional */
1738 DBG("chiprev %02x\n", tmp);
1743 /* The chip's IRQ is level triggered, active high. A requirement
1744 * for platform device setup is to cope with things like signal
1745 * inverters (e.g. CF is active low) or working only with edge
1746 * triggers (e.g. most ARM CPUs). Initial driver stress testing
1747 * was on a system with single edge triggering, so most sorts of
1748 * triggering arrangement should work.
1750 retval = usb_add_hcd(hcd, irq, SA_INTERRUPT | SA_SHIRQ);
1754 create_debug_file(sl811);
1766 DBG("init error, %d\n", retval);
1772 /* for this device there's no useful distinction between the controller
1773 * and its root hub, except that the root hub only gets direct PM calls
1774 * when CONFIG_USB_SUSPEND is enabled.
1778 sl811h_suspend(struct platform_device *dev, pm_message_t state)
1780 struct usb_hcd *hcd = platform_get_drvdata(dev);
1781 struct sl811 *sl811 = hcd_to_sl811(hcd);
1784 if (state.event == PM_EVENT_FREEZE)
1785 retval = sl811h_bus_suspend(hcd);
1786 else if (state.event == PM_EVENT_SUSPEND)
1787 port_power(sl811, 0);
1789 dev->dev.power.power_state = state;
1794 sl811h_resume(struct platform_device *dev)
1796 struct usb_hcd *hcd = platform_get_drvdata(dev);
1797 struct sl811 *sl811 = hcd_to_sl811(hcd);
1799 /* with no "check to see if VBUS is still powered" board hook,
1800 * let's assume it'd only be powered to enable remote wakeup.
1802 if (dev->dev.power.power_state.event == PM_EVENT_SUSPEND
1803 || !device_can_wakeup(&hcd->self.root_hub->dev)) {
1805 port_power(sl811, 1);
1806 usb_root_hub_lost_power(hcd->self.root_hub);
1810 dev->dev.power.power_state = PMSG_ON;
1811 return sl811h_bus_resume(hcd);
1816 #define sl811h_suspend NULL
1817 #define sl811h_resume NULL
1822 /* this driver is exported so sl811_cs can depend on it */
1823 struct platform_driver sl811h_driver = {
1824 .probe = sl811h_probe,
1825 .remove = __devexit_p(sl811h_remove),
1827 .suspend = sl811h_suspend,
1828 .resume = sl811h_resume,
1830 .name = (char *) hcd_name,
1831 .owner = THIS_MODULE,
1834 EXPORT_SYMBOL(sl811h_driver);
1836 /*-------------------------------------------------------------------------*/
1838 static int __init sl811h_init(void)
1843 INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
1844 return platform_driver_register(&sl811h_driver);
1846 module_init(sl811h_init);
1848 static void __exit sl811h_cleanup(void)
1850 platform_driver_unregister(&sl811h_driver);
1852 module_exit(sl811h_cleanup);