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