Linux Audio

Check our new training course

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