sl811-hcd.c 45.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * SL811HS HCD (Host Controller Driver) for USB.
 *
 * Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
D
David Brownell 已提交
5 6
 * Copyright (C) 2004-2005 David Brownell
 *
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17
 * Periodic scheduling is based on Roman's OHCI code
 * 	Copyright (C) 1999 Roman Weissgaerber
 *
 * The SL811HS controller handles host side USB (like the SL11H, but with
 * another register set and SOF generation) as well as peripheral side USB
 * (like the SL811S).  This driver version doesn't implement the Gadget API
 * for the peripheral role; or OTG (that'd need much external circuitry).
 *
 * For documentation, see the SL811HS spec and the "SL811HS Embedded Host"
 * document (providing significant pieces missing from that spec); plus
 * the SL811S spec if you want peripheral side info.
D
David Brownell 已提交
18
 */
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/*
 * Status:  Passed basic stress testing, works with hubs, mice, keyboards,
 * and usb-storage.
 *
 * TODO:
 * - usb suspend/resume triggered by sl811 (with USB_SUSPEND)
 * - various issues noted in the code
 * - performance work; use both register banks; ...
 * - use urb->iso_frame_desc[] with ISO transfers
 */

#undef	VERBOSE
#undef	PACKET_TRACE

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/usb.h>
47
#include <linux/usb/sl811.h>
48
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59 60

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/system.h>
#include <asm/byteorder.h>

#include "../core/hcd.h"
#include "sl811.h"


MODULE_DESCRIPTION("SL811HS USB Host Controller Driver");
MODULE_LICENSE("GPL");
61
MODULE_ALIAS("platform:sl811-hcd");
L
Linus Torvalds 已提交
62

D
David Brownell 已提交
63
#define DRIVER_VERSION	"19 May 2005"
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114


#ifndef DEBUG
#	define	STUB_DEBUG_FILE
#endif

/* for now, use only one transfer register bank */
#undef	USE_B

/* this doesn't understand urb->iso_frame_desc[], but if you had a driver
 * that just queued one ISO frame per URB then iso transfers "should" work
 * using the normal urb status fields.
 */
#define	DISABLE_ISO

// #define	QUIRK2
#define	QUIRK3

static const char hcd_name[] = "sl811-hcd";

/*-------------------------------------------------------------------------*/

static void port_power(struct sl811 *sl811, int is_on)
{
	struct usb_hcd	*hcd = sl811_to_hcd(sl811);

	/* hub is inactive unless the port is powered */
	if (is_on) {
		if (sl811->port1 & (1 << USB_PORT_FEAT_POWER))
			return;

		sl811->port1 = (1 << USB_PORT_FEAT_POWER);
		sl811->irq_enable = SL11H_INTMASK_INSRMV;
	} else {
		sl811->port1 = 0;
		sl811->irq_enable = 0;
		hcd->state = HC_STATE_HALT;
	}
	sl811->ctrl1 = 0;
	sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
	sl811_write(sl811, SL11H_IRQ_STATUS, ~0);

	if (sl811->board && sl811->board->port_power) {
		/* switch VBUS, at 500mA unless hub power budget gets set */
		DBG("power %s\n", is_on ? "on" : "off");
		sl811->board->port_power(hcd->self.controller, is_on);
	}

	/* reset as thoroughly as we can */
	if (sl811->board && sl811->board->reset)
		sl811->board->reset(hcd->self.controller);
D
David Brownell 已提交
115 116 117 118
	else {
		sl811_write(sl811, SL11H_CTLREG1, SL11H_CTL1MASK_SE0);
		mdelay(20);
	}
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131

	sl811_write(sl811, SL11H_IRQ_ENABLE, 0);
	sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
	sl811_write(sl811, SL811HS_CTLREG2, SL811HS_CTL2_INIT);
	sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);

	// if !is_on, put into lowpower mode now
}

/*-------------------------------------------------------------------------*/

/* This is a PIO-only HCD.  Queueing appends URBs to the endpoint's queue,
 * and may start I/O.  Endpoint queues are scanned during completion irq
132
 * handlers (one per packet: ACK, NAK, faults, etc) and urb cancellation.
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
 *
 * Using an external DMA engine to copy a packet at a time could work,
 * though setup/teardown costs may be too big to make it worthwhile.
 */

/* SETUP starts a new control request.  Devices are not allowed to
 * STALL or NAK these; they must cancel any pending control requests.
 */
static void setup_packet(
	struct sl811		*sl811,
	struct sl811h_ep	*ep,
	struct urb		*urb,
	u8			bank,
	u8			control
)
{
	u8			addr;
	u8			len;
	void __iomem		*data_reg;

	addr = SL811HS_PACKET_BUF(bank == 0);
	len = sizeof(struct usb_ctrlrequest);
	data_reg = sl811->data_reg;
	sl811_write_buf(sl811, addr, urb->setup_packet, len);

	/* autoincrementing */
	sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
	writeb(len, data_reg);
	writeb(SL_SETUP /* | ep->epnum */, data_reg);
	writeb(usb_pipedevice(urb->pipe), data_reg);

	/* always OUT/data0 */ ;
	sl811_write(sl811, bank + SL11H_HOSTCTLREG,
			control | SL11H_HCTLMASK_OUT);
	ep->length = 0;
	PACKET("SETUP qh%p\n", ep);
}

/* STATUS finishes control requests, often after IN or OUT data packets */
static void status_packet(
	struct sl811		*sl811,
	struct sl811h_ep	*ep,
	struct urb		*urb,
	u8			bank,
	u8			control
)
{
	int			do_out;
	void __iomem		*data_reg;

	do_out = urb->transfer_buffer_length && usb_pipein(urb->pipe);
	data_reg = sl811->data_reg;

	/* autoincrementing */
	sl811_write(sl811, bank + SL11H_BUFADDRREG, 0);
	writeb(0, data_reg);
	writeb((do_out ? SL_OUT : SL_IN) /* | ep->epnum */, data_reg);
	writeb(usb_pipedevice(urb->pipe), data_reg);

	/* always data1; sometimes IN */
	control |= SL11H_HCTLMASK_TOGGLE;
	if (do_out)
		control |= SL11H_HCTLMASK_OUT;
	sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
	ep->length = 0;
	PACKET("STATUS%s/%s qh%p\n", ep->nak_count ? "/retry" : "",
			do_out ? "out" : "in", ep);
}

/* IN packets can be used with any type of endpoint. here we just
 * start the transfer, data from the peripheral may arrive later.
 * urb->iso_frame_desc is currently ignored here...
 */
static void in_packet(
	struct sl811		*sl811,
	struct sl811h_ep	*ep,
	struct urb		*urb,
	u8			bank,
	u8			control
)
{
	u8			addr;
	u8			len;
	void __iomem		*data_reg;

	/* avoid losing data on overflow */
	len = ep->maxpacket;
	addr = SL811HS_PACKET_BUF(bank == 0);
	if (!(control & SL11H_HCTLMASK_ISOCH)
			&& usb_gettoggle(urb->dev, ep->epnum, 0))
		control |= SL11H_HCTLMASK_TOGGLE;
	data_reg = sl811->data_reg;

	/* autoincrementing */
	sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
	writeb(len, data_reg);
	writeb(SL_IN | ep->epnum, data_reg);
	writeb(usb_pipedevice(urb->pipe), data_reg);

	sl811_write(sl811, bank + SL11H_HOSTCTLREG, control);
	ep->length = min((int)len,
			urb->transfer_buffer_length - urb->actual_length);
	PACKET("IN%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
			!!usb_gettoggle(urb->dev, ep->epnum, 0), ep, len);
}

/* OUT packets can be used with any type of endpoint.
 * urb->iso_frame_desc is currently ignored here...
 */
static void out_packet(
	struct sl811		*sl811,
	struct sl811h_ep	*ep,
	struct urb		*urb,
	u8			bank,
	u8			control
)
{
	void			*buf;
	u8			addr;
	u8			len;
	void __iomem		*data_reg;

	buf = urb->transfer_buffer + urb->actual_length;
	prefetch(buf);

	len = min((int)ep->maxpacket,
			urb->transfer_buffer_length - urb->actual_length);

	if (!(control & SL11H_HCTLMASK_ISOCH)
			&& usb_gettoggle(urb->dev, ep->epnum, 1))
		control |= SL11H_HCTLMASK_TOGGLE;
	addr = SL811HS_PACKET_BUF(bank == 0);
	data_reg = sl811->data_reg;

	sl811_write_buf(sl811, addr, buf, len);

	/* autoincrementing */
	sl811_write(sl811, bank + SL11H_BUFADDRREG, addr);
	writeb(len, data_reg);
	writeb(SL_OUT | ep->epnum, data_reg);
	writeb(usb_pipedevice(urb->pipe), data_reg);

	sl811_write(sl811, bank + SL11H_HOSTCTLREG,
			control | SL11H_HCTLMASK_OUT);
	ep->length = len;
	PACKET("OUT%s/%d qh%p len%d\n", ep->nak_count ? "/retry" : "",
			!!usb_gettoggle(urb->dev, ep->epnum, 1), ep, len);
}

/*-------------------------------------------------------------------------*/

/* caller updates on-chip enables later */

static inline void sofirq_on(struct sl811 *sl811)
{
	if (sl811->irq_enable & SL11H_INTMASK_SOFINTR)
		return;
	VDBG("sof irq on\n");
	sl811->irq_enable |= SL11H_INTMASK_SOFINTR;
}

static inline void sofirq_off(struct sl811 *sl811)
{
	if (!(sl811->irq_enable & SL11H_INTMASK_SOFINTR))
		return;
	VDBG("sof irq off\n");
	sl811->irq_enable &= ~SL11H_INTMASK_SOFINTR;
}

/*-------------------------------------------------------------------------*/

/* pick the next endpoint for a transaction, and issue it.
 * frames start with periodic transfers (after whatever is pending
 * from the previous frame), and the rest of the time is async
 * transfers, scheduled round-robin.
 */
static struct sl811h_ep	*start(struct sl811 *sl811, u8 bank)
{
	struct sl811h_ep	*ep;
	struct urb		*urb;
	int			fclock;
	u8			control;

	/* use endpoint at schedule head */
	if (sl811->next_periodic) {
		ep = sl811->next_periodic;
		sl811->next_periodic = ep->next;
	} else {
		if (sl811->next_async)
			ep = sl811->next_async;
		else if (!list_empty(&sl811->async))
			ep = container_of(sl811->async.next,
					struct sl811h_ep, schedule);
		else {
			/* could set up the first fullspeed periodic
			 * transfer for the next frame ...
			 */
			return NULL;
		}

#ifdef USE_B
		if ((bank && sl811->active_b == ep) || sl811->active_a == ep)
			return NULL;
#endif

		if (ep->schedule.next == &sl811->async)
			sl811->next_async = NULL;
		else
			sl811->next_async = container_of(ep->schedule.next,
					struct sl811h_ep, schedule);
	}

	if (unlikely(list_empty(&ep->hep->urb_list))) {
		DBG("empty %p queue?\n", ep);
		return NULL;
	}

	urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);
	control = ep->defctrl;

	/* if this frame doesn't have enough time left to transfer this
	 * packet, wait till the next frame.  too-simple algorithm...
	 */
	fclock = sl811_read(sl811, SL11H_SOFTMRREG) << 6;
	fclock -= 100;		/* setup takes not much time */
	if (urb->dev->speed == USB_SPEED_LOW) {
		if (control & SL11H_HCTLMASK_PREAMBLE) {
			/* also note erratum 1: some hubs won't work */
			fclock -= 800;
		}
		fclock -= ep->maxpacket << 8;

		/* erratum 2: AFTERSOF only works for fullspeed */
		if (fclock < 0) {
			if (ep->period)
				sl811->stat_overrun++;
			sofirq_on(sl811);
			return NULL;
		}
	} else {
		fclock -= 12000 / 19;	/* 19 64byte packets/msec */
		if (fclock < 0) {
			if (ep->period)
				sl811->stat_overrun++;
			control |= SL11H_HCTLMASK_AFTERSOF;

		/* throttle bulk/control irq noise */
		} else if (ep->nak_count)
			control |= SL11H_HCTLMASK_AFTERSOF;
	}


	switch (ep->nextpid) {
	case USB_PID_IN:
		in_packet(sl811, ep, urb, bank, control);
		break;
	case USB_PID_OUT:
		out_packet(sl811, ep, urb, bank, control);
		break;
	case USB_PID_SETUP:
		setup_packet(sl811, ep, urb, bank, control);
		break;
	case USB_PID_ACK:		/* for control status */
		status_packet(sl811, ep, urb, bank, control);
		break;
	default:
		DBG("bad ep%p pid %02x\n", ep, ep->nextpid);
		ep = NULL;
	}
	return ep;
}

#define MIN_JIFFIES	((msecs_to_jiffies(2) > 1) ? msecs_to_jiffies(2) : 2)

static inline void start_transfer(struct sl811 *sl811)
{
	if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
		return;
	if (sl811->active_a == NULL) {
		sl811->active_a = start(sl811, SL811_EP_A(SL811_HOST_BUF));
		if (sl811->active_a != NULL)
			sl811->jiffies_a = jiffies + MIN_JIFFIES;
	}
#ifdef USE_B
	if (sl811->active_b == NULL) {
		sl811->active_b = start(sl811, SL811_EP_B(SL811_HOST_BUF));
		if (sl811->active_b != NULL)
			sl811->jiffies_b = jiffies + MIN_JIFFIES;
	}
#endif
}

static void finish_request(
	struct sl811		*sl811,
	struct sl811h_ep	*ep,
	struct urb		*urb,
	int			status
) __releases(sl811->lock) __acquires(sl811->lock)
{
	unsigned		i;

	if (usb_pipecontrol(urb->pipe))
		ep->nextpid = USB_PID_SETUP;

437
	usb_hcd_unlink_urb_from_ep(sl811_to_hcd(sl811), urb);
L
Linus Torvalds 已提交
438
	spin_unlock(&sl811->lock);
A
Alan Stern 已提交
439
	usb_hcd_giveback_urb(sl811_to_hcd(sl811), urb, status);
L
Linus Torvalds 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	spin_lock(&sl811->lock);

	/* leave active endpoints in the schedule */
	if (!list_empty(&ep->hep->urb_list))
		return;

	/* async deschedule? */
	if (!list_empty(&ep->schedule)) {
		list_del_init(&ep->schedule);
		if (ep == sl811->next_async)
			sl811->next_async = NULL;
		return;
	}

	/* periodic deschedule */
	DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
	for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
		struct sl811h_ep	*temp;
		struct sl811h_ep	**prev = &sl811->periodic[i];

		while (*prev && ((temp = *prev) != ep))
			prev = &temp->next;
		if (*prev)
			*prev = ep->next;
		sl811->load[i] -= ep->load;
D
David Brownell 已提交
465
	}
L
Linus Torvalds 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478
	ep->branch = PERIODIC_SIZE;
	sl811->periodic_count--;
	sl811_to_hcd(sl811)->self.bandwidth_allocated
		-= ep->load / ep->period;
	if (ep == sl811->next_periodic)
		sl811->next_periodic = ep->next;

	/* we might turn SOFs back on again for the async schedule */
	if (sl811->periodic_count == 0)
		sofirq_off(sl811);
}

static void
479
done(struct sl811 *sl811, struct sl811h_ep *ep, u8 bank)
L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
{
	u8			status;
	struct urb		*urb;
	int			urbstat = -EINPROGRESS;

	if (unlikely(!ep))
		return;

	status = sl811_read(sl811, bank + SL11H_PKTSTATREG);

	urb = container_of(ep->hep->urb_list.next, struct urb, urb_list);

	/* we can safely ignore NAKs */
	if (status & SL11H_STATMASK_NAK) {
		// PACKET("...NAK_%02x qh%p\n", bank, ep);
		if (!ep->period)
			ep->nak_count++;
		ep->error_count = 0;

	/* ACK advances transfer, toggle, and maybe queue */
	} else if (status & SL11H_STATMASK_ACK) {
		struct usb_device	*udev = urb->dev;
		int			len;
		unsigned char		*buf;

		/* urb->iso_frame_desc is currently ignored here... */

		ep->nak_count = ep->error_count = 0;
		switch (ep->nextpid) {
		case USB_PID_OUT:
			// PACKET("...ACK/out_%02x qh%p\n", bank, ep);
			urb->actual_length += ep->length;
			usb_dotoggle(udev, ep->epnum, 1);
			if (urb->actual_length
					== urb->transfer_buffer_length) {
				if (usb_pipecontrol(urb->pipe))
					ep->nextpid = USB_PID_ACK;

				/* some bulk protocols terminate OUT transfers
				 * by a short packet, using ZLPs not padding.
				 */
				else if (ep->length < ep->maxpacket
						|| !(urb->transfer_flags
							& URB_ZERO_PACKET))
					urbstat = 0;
			}
			break;
		case USB_PID_IN:
			// PACKET("...ACK/in_%02x qh%p\n", bank, ep);
			buf = urb->transfer_buffer + urb->actual_length;
			prefetchw(buf);
			len = ep->maxpacket - sl811_read(sl811,
						bank + SL11H_XFERCNTREG);
			if (len > ep->length) {
				len = ep->length;
535
				urbstat = -EOVERFLOW;
L
Linus Torvalds 已提交
536 537 538 539 540
			}
			urb->actual_length += len;
			sl811_read_buf(sl811, SL811HS_PACKET_BUF(bank == 0),
					buf, len);
			usb_dotoggle(udev, ep->epnum, 0);
541 542 543 544 545 546 547 548
			if (urbstat == -EINPROGRESS &&
					(len < ep->maxpacket ||
						urb->actual_length ==
						urb->transfer_buffer_length)) {
				if (usb_pipecontrol(urb->pipe))
					ep->nextpid = USB_PID_ACK;
				else
					urbstat = 0;
L
Linus Torvalds 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
			}
			break;
		case USB_PID_SETUP:
			// PACKET("...ACK/setup_%02x qh%p\n", bank, ep);
			if (urb->transfer_buffer_length == urb->actual_length)
				ep->nextpid = USB_PID_ACK;
			else if (usb_pipeout(urb->pipe)) {
				usb_settoggle(udev, 0, 1, 1);
				ep->nextpid = USB_PID_OUT;
			} else {
				usb_settoggle(udev, 0, 0, 1);
				ep->nextpid = USB_PID_IN;
			}
			break;
		case USB_PID_ACK:
			// PACKET("...ACK/status_%02x qh%p\n", bank, ep);
			urbstat = 0;
			break;
		}

	/* STALL stops all transfers */
	} else if (status & SL11H_STATMASK_STALL) {
		PACKET("...STALL_%02x qh%p\n", bank, ep);
		ep->nak_count = ep->error_count = 0;
		urbstat = -EPIPE;

	/* error? retry, until "3 strikes" */
	} else if (++ep->error_count >= 3) {
		if (status & SL11H_STATMASK_TMOUT)
578
			urbstat = -ETIME;
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586 587
		else if (status & SL11H_STATMASK_OVF)
			urbstat = -EOVERFLOW;
		else
			urbstat = -EPROTO;
		ep->error_count = 0;
		PACKET("...3STRIKES_%02x %02x qh%p stat %d\n",
				bank, status, ep, urbstat);
	}

588
	if (urbstat != -EINPROGRESS || urb->unlinked)
589
		finish_request(sl811, ep, urb, urbstat);
L
Linus Torvalds 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
}

static inline u8 checkdone(struct sl811 *sl811)
{
	u8	ctl;
	u8	irqstat = 0;

	if (sl811->active_a && time_before_eq(sl811->jiffies_a, jiffies)) {
		ctl = sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG));
		if (ctl & SL11H_HCTLMASK_ARM)
			sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
		DBG("%s DONE_A: ctrl %02x sts %02x\n",
			(ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
			ctl,
			sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
		irqstat |= SL11H_INTMASK_DONE_A;
	}
#ifdef	USE_B
	if (sl811->active_b && time_before_eq(sl811->jiffies_b, jiffies)) {
		ctl = sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG));
		if (ctl & SL11H_HCTLMASK_ARM)
			sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
		DBG("%s DONE_B: ctrl %02x sts %02x\n",
			(ctl & SL11H_HCTLMASK_ARM) ? "timeout" : "lost",
			ctl,
			sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
		irqstat |= SL11H_INTMASK_DONE_A;
	}
#endif
	return irqstat;
}

622
static irqreturn_t sl811h_irq(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
{
	struct sl811	*sl811 = hcd_to_sl811(hcd);
	u8		irqstat;
	irqreturn_t	ret = IRQ_NONE;
	unsigned	retries = 5;

	spin_lock(&sl811->lock);

retry:
	irqstat = sl811_read(sl811, SL11H_IRQ_STATUS) & ~SL11H_INTMASK_DP;
	if (irqstat) {
		sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);
		irqstat &= sl811->irq_enable;
	}

#ifdef	QUIRK2
	/* this may no longer be necessary ... */
D
David Brownell 已提交
640
	if (irqstat == 0) {
L
Linus Torvalds 已提交
641
		irqstat = checkdone(sl811);
D
David Brownell 已提交
642
		if (irqstat)
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650
			sl811->stat_lost++;
	}
#endif

	/* USB packets, not necessarily handled in the order they're
	 * issued ... that's fine if they're different endpoints.
	 */
	if (irqstat & SL11H_INTMASK_DONE_A) {
651
		done(sl811, sl811->active_a, SL811_EP_A(SL811_HOST_BUF));
L
Linus Torvalds 已提交
652 653 654 655 656
		sl811->active_a = NULL;
		sl811->stat_a++;
	}
#ifdef USE_B
	if (irqstat & SL11H_INTMASK_DONE_B) {
657
		done(sl811, sl811->active_b, SL811_EP_B(SL811_HOST_BUF));
L
Linus Torvalds 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
		sl811->active_b = NULL;
		sl811->stat_b++;
	}
#endif
	if (irqstat & SL11H_INTMASK_SOFINTR) {
		unsigned index;

		index = sl811->frame++ % (PERIODIC_SIZE - 1);
		sl811->stat_sof++;

		/* be graceful about almost-inevitable periodic schedule
		 * overruns:  continue the previous frame's transfers iff
		 * this one has nothing scheduled.
		 */
		if (sl811->next_periodic) {
			// ERR("overrun to slot %d\n", index);
			sl811->stat_overrun++;
		}
		if (sl811->periodic[index])
			sl811->next_periodic = sl811->periodic[index];
	}

	/* khubd manages debouncing and wakeup */
	if (irqstat & SL11H_INTMASK_INSRMV) {
		sl811->stat_insrmv++;

		/* most stats are reset for each VBUS session */
		sl811->stat_wake = 0;
		sl811->stat_sof = 0;
		sl811->stat_a = 0;
		sl811->stat_b = 0;
		sl811->stat_lost = 0;

		sl811->ctrl1 = 0;
		sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);

		sl811->irq_enable = SL11H_INTMASK_INSRMV;
		sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);

		/* usbcore nukes other pending transactions on disconnect */
		if (sl811->active_a) {
			sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG), 0);
			finish_request(sl811, sl811->active_a,
D
David Brownell 已提交
701 702
				container_of(sl811->active_a
						->hep->urb_list.next,
L
Linus Torvalds 已提交
703
					struct urb, urb_list),
704
				-ESHUTDOWN);
L
Linus Torvalds 已提交
705 706 707 708 709 710
			sl811->active_a = NULL;
		}
#ifdef	USE_B
		if (sl811->active_b) {
			sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG), 0);
			finish_request(sl811, sl811->active_b,
D
David Brownell 已提交
711 712
				container_of(sl811->active_b
						->hep->urb_list.next,
L
Linus Torvalds 已提交
713 714 715 716 717 718
					struct urb, urb_list),
				NULL, -ESHUTDOWN);
			sl811->active_b = NULL;
		}
#endif

719
		/* port status seems weird until after reset, so
L
Linus Torvalds 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
		 * force the reset and make khubd clean up later.
		 */
		sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
				| (1 << USB_PORT_FEAT_CONNECTION);

	} else if (irqstat & SL11H_INTMASK_RD) {
		if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)) {
			DBG("wakeup\n");
			sl811->port1 |= 1 << USB_PORT_FEAT_C_SUSPEND;
			sl811->stat_wake++;
		} else
			irqstat &= ~SL11H_INTMASK_RD;
	}

	if (irqstat) {
		if (sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
			start_transfer(sl811);
		ret = IRQ_HANDLED;
		if (retries--)
			goto retry;
	}

D
David Brownell 已提交
742
	if (sl811->periodic_count == 0 && list_empty(&sl811->async))
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755
		sofirq_off(sl811);
	sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);

	spin_unlock(&sl811->lock);

	return ret;
}

/*-------------------------------------------------------------------------*/

/* usb 1.1 says max 90% of a frame is available for periodic transfers.
 * this driver doesn't promise that much since it's got to handle an
 * IRQ per packet; irq handling latencies also use up that time.
756 757 758
 *
 * NOTE:  the periodic schedule is a sparse tree, with the load for
 * each branch minimized.  see fig 3.5 in the OHCI spec for example.
L
Linus Torvalds 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
 */
#define	MAX_PERIODIC_LOAD	500	/* out of 1000 usec */

static int balance(struct sl811 *sl811, u16 period, u16 load)
{
	int	i, branch = -ENOSPC;

	/* search for the least loaded schedule branch of that period
	 * which has enough bandwidth left unreserved.
	 */
	for (i = 0; i < period ; i++) {
		if (branch < 0 || sl811->load[branch] > sl811->load[i]) {
			int	j;

			for (j = i; j < PERIODIC_SIZE; j += period) {
				if ((sl811->load[j] + load)
						> MAX_PERIODIC_LOAD)
					break;
			}
			if (j < PERIODIC_SIZE)
				continue;
D
David Brownell 已提交
780
			branch = i;
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788 789 790
		}
	}
	return branch;
}

/*-------------------------------------------------------------------------*/

static int sl811h_urb_enqueue(
	struct usb_hcd		*hcd,
	struct urb		*urb,
A
Al Viro 已提交
791
	gfp_t			mem_flags
L
Linus Torvalds 已提交
792 793 794 795 796 797 798 799 800 801
) {
	struct sl811		*sl811 = hcd_to_sl811(hcd);
	struct usb_device	*udev = urb->dev;
	unsigned int		pipe = urb->pipe;
	int			is_out = !usb_pipein(pipe);
	int			type = usb_pipetype(pipe);
	int			epnum = usb_pipeendpoint(pipe);
	struct sl811h_ep	*ep = NULL;
	unsigned long		flags;
	int			i;
802 803
	int			retval;
	struct usb_host_endpoint	*hep = urb->ep;
L
Linus Torvalds 已提交
804 805 806 807 808 809 810 811

#ifdef	DISABLE_ISO
	if (type == PIPE_ISOCHRONOUS)
		return -ENOSPC;
#endif

	/* avoid all allocations within spinlocks */
	if (!hep->hcpriv)
812
		ep = kzalloc(sizeof *ep, mem_flags);
L
Linus Torvalds 已提交
813 814 815 816 817 818 819

	spin_lock_irqsave(&sl811->lock, flags);

	/* don't submit to a dead or disabled port */
	if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE))
			|| !HC_IS_RUNNING(hcd->state)) {
		retval = -ENODEV;
820
		kfree(ep);
821 822 823 824 825 826
		goto fail_not_linked;
	}
	retval = usb_hcd_link_urb_to_ep(hcd, urb);
	if (retval) {
		kfree(ep);
		goto fail_not_linked;
L
Linus Torvalds 已提交
827 828 829 830 831 832 833 834 835 836 837
	}

	if (hep->hcpriv) {
		kfree(ep);
		ep = hep->hcpriv;
	} else if (!ep) {
		retval = -ENOMEM;
		goto fail;

	} else {
		INIT_LIST_HEAD(&ep->schedule);
838
		ep->udev = udev;
L
Linus Torvalds 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
		ep->epnum = epnum;
		ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
		ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
		usb_settoggle(udev, epnum, is_out, 0);

		if (type == PIPE_CONTROL)
			ep->nextpid = USB_PID_SETUP;
		else if (is_out)
			ep->nextpid = USB_PID_OUT;
		else
			ep->nextpid = USB_PID_IN;

		if (ep->maxpacket > H_MAXPACKET) {
			/* iso packets up to 240 bytes could work... */
			DBG("dev %d ep%d maxpacket %d\n",
				udev->devnum, epnum, ep->maxpacket);
			retval = -EINVAL;
			goto fail;
		}

		if (udev->speed == USB_SPEED_LOW) {
			/* send preamble for external hub? */
			if (!(sl811->ctrl1 & SL11H_CTL1MASK_LSPD))
				ep->defctrl |= SL11H_HCTLMASK_PREAMBLE;
		}
		switch (type) {
		case PIPE_ISOCHRONOUS:
		case PIPE_INTERRUPT:
			if (urb->interval > PERIODIC_SIZE)
				urb->interval = PERIODIC_SIZE;
			ep->period = urb->interval;
			ep->branch = PERIODIC_SIZE;
			if (type == PIPE_ISOCHRONOUS)
				ep->defctrl |= SL11H_HCTLMASK_ISOCH;
			ep->load = usb_calc_bus_time(udev->speed, !is_out,
				(type == PIPE_ISOCHRONOUS),
				usb_maxpacket(udev, pipe, is_out))
					/ 1000;
			break;
		}

D
David Brownell 已提交
880
		ep->hep = hep;
L
Linus Torvalds 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893
		hep->hcpriv = ep;
	}

	/* maybe put endpoint into schedule */
	switch (type) {
	case PIPE_CONTROL:
	case PIPE_BULK:
		if (list_empty(&ep->schedule))
			list_add_tail(&ep->schedule, &sl811->async);
		break;
	case PIPE_ISOCHRONOUS:
	case PIPE_INTERRUPT:
		urb->interval = ep->period;
894 895 896 897 898 899 900 901
		if (ep->branch < PERIODIC_SIZE) {
			/* NOTE:  the phase is correct here, but the value
			 * needs offsetting by the transfer queue depth.
			 * All current drivers ignore start_frame, so this
			 * is unlikely to ever matter...
			 */
			urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
						+ ep->branch;
L
Linus Torvalds 已提交
902
			break;
903
		}
L
Linus Torvalds 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942

		retval = balance(sl811, ep->period, ep->load);
		if (retval < 0)
			goto fail;
		ep->branch = retval;
		retval = 0;
		urb->start_frame = (sl811->frame & (PERIODIC_SIZE - 1))
					+ ep->branch;

		/* sort each schedule branch by period (slow before fast)
		 * to share the faster parts of the tree without needing
		 * dummy/placeholder nodes
		 */
		DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
		for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
			struct sl811h_ep	**prev = &sl811->periodic[i];
			struct sl811h_ep	*here = *prev;

			while (here && ep != here) {
				if (ep->period > here->period)
					break;
				prev = &here->next;
				here = *prev;
			}
			if (ep != here) {
				ep->next = here;
				*prev = ep;
			}
			sl811->load[i] += ep->load;
		}
		sl811->periodic_count++;
		hcd->self.bandwidth_allocated += ep->load / ep->period;
		sofirq_on(sl811);
	}

	urb->hcpriv = hep;
	start_transfer(sl811);
	sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
fail:
943 944 945
	if (retval)
		usb_hcd_unlink_urb_from_ep(hcd, urb);
fail_not_linked:
L
Linus Torvalds 已提交
946 947 948 949
	spin_unlock_irqrestore(&sl811->lock, flags);
	return retval;
}

950
static int sl811h_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
L
Linus Torvalds 已提交
951 952
{
	struct sl811		*sl811 = hcd_to_sl811(hcd);
D
David Brownell 已提交
953
	struct usb_host_endpoint *hep;
L
Linus Torvalds 已提交
954 955
	unsigned long		flags;
	struct sl811h_ep	*ep;
956
	int			retval;
L
Linus Torvalds 已提交
957

D
David Brownell 已提交
958
	spin_lock_irqsave(&sl811->lock, flags);
959 960
	retval = usb_hcd_check_unlink_urb(hcd, urb, status);
	if (retval)
D
David Brownell 已提交
961
		goto fail;
L
Linus Torvalds 已提交
962

963
	hep = urb->hcpriv;
L
Linus Torvalds 已提交
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
	ep = hep->hcpriv;
	if (ep) {
		/* finish right away if this urb can't be active ...
		 * note that some drivers wrongly expect delays
		 */
		if (ep->hep->urb_list.next != &urb->urb_list) {
			/* not front of queue?  never active */

		/* for active transfers, we expect an IRQ */
		} else if (sl811->active_a == ep) {
			if (time_before_eq(sl811->jiffies_a, jiffies)) {
				/* happens a lot with lowspeed?? */
				DBG("giveup on DONE_A: ctrl %02x sts %02x\n",
					sl811_read(sl811,
						SL811_EP_A(SL11H_HOSTCTLREG)),
					sl811_read(sl811,
						SL811_EP_A(SL11H_PKTSTATREG)));
				sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
						0);
				sl811->active_a = NULL;
			} else
				urb = NULL;
#ifdef	USE_B
		} else if (sl811->active_b == ep) {
			if (time_before_eq(sl811->jiffies_a, jiffies)) {
				/* happens a lot with lowspeed?? */
				DBG("giveup on DONE_B: ctrl %02x sts %02x\n",
					sl811_read(sl811,
						SL811_EP_B(SL11H_HOSTCTLREG)),
					sl811_read(sl811,
						SL811_EP_B(SL11H_PKTSTATREG)));
				sl811_write(sl811, SL811_EP_B(SL11H_HOSTCTLREG),
						0);
				sl811->active_b = NULL;
			} else
				urb = NULL;
#endif
		} else {
			/* front of queue for inactive endpoint */
		}

		if (urb)
1006
			finish_request(sl811, ep, urb, 0);
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011
		else
			VDBG("dequeue, urb %p active %s; wait4irq\n", urb,
				(sl811->active_a == ep) ? "A" : "B");
	} else
		retval = -EINVAL;
1012
 fail:
L
Linus Torvalds 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	spin_unlock_irqrestore(&sl811->lock, flags);
	return retval;
}

static void
sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
{
	struct sl811h_ep	*ep = hep->hcpriv;

	if (!ep)
		return;

	/* assume we'd just wait for the irq */
	if (!list_empty(&hep->urb_list))
		msleep(3);
	if (!list_empty(&hep->urb_list))
1029
		WARNING("ep %p not empty?\n", ep);
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

	kfree(ep);
	hep->hcpriv = NULL;
}

static int
sl811h_get_frame(struct usb_hcd *hcd)
{
	struct sl811 *sl811 = hcd_to_sl811(hcd);

	/* wrong except while periodic transfers are scheduled;
	 * never matches the on-the-wire frame;
	 * subject to overruns.
	 */
	return sl811->frame;
}


/*-------------------------------------------------------------------------*/

/* the virtual root hub timer IRQ checks for hub status */
static int
sl811h_hub_status_data(struct usb_hcd *hcd, char *buf)
{
	struct sl811 *sl811 = hcd_to_sl811(hcd);
#ifdef	QUIRK3
	unsigned long flags;

	/* non-SMP HACK: use root hub timer as i/o watchdog
	 * this seems essential when SOF IRQs aren't in use...
	 */
	local_irq_save(flags);
	if (!timer_pending(&sl811->timer)) {
1063
		if (sl811h_irq( /* ~0, */ hcd) != IRQ_NONE)
L
Linus Torvalds 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
			sl811->stat_lost++;
	}
	local_irq_restore(flags);
#endif

	if (!(sl811->port1 & (0xffff << 16)))
		return 0;

	/* tell khubd port 1 changed */
	*buf = (1 << 1);
	return 1;
}

static void
sl811h_hub_descriptor (
	struct sl811			*sl811,
	struct usb_hub_descriptor	*desc
) {
	u16		temp = 0;

	desc->bDescriptorType = 0x29;
	desc->bHubContrCurrent = 0;

	desc->bNbrPorts = 1;
	desc->bDescLength = 9;

	/* per-port power switching (gang of one!), or none */
	desc->bPwrOn2PwrGood = 0;
	if (sl811->board && sl811->board->port_power) {
		desc->bPwrOn2PwrGood = sl811->board->potpg;
		if (!desc->bPwrOn2PwrGood)
			desc->bPwrOn2PwrGood = 10;
		temp = 0x0001;
	} else
		temp = 0x0002;

	/* no overcurrent errors detection/handling */
	temp |= 0x0010;

A
Al Viro 已提交
1103
	desc->wHubCharacteristics = cpu_to_le16(temp);
L
Linus Torvalds 已提交
1104 1105

	/* two bitmaps:  ports removable, and legacy PortPwrCtrlMask */
1106
	desc->bitmap[0] = 0 << 1;
L
Linus Torvalds 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
	desc->bitmap[1] = ~0;
}

static void
sl811h_timer(unsigned long _sl811)
{
	struct sl811 	*sl811 = (void *) _sl811;
	unsigned long	flags;
	u8		irqstat;
	u8		signaling = sl811->ctrl1 & SL11H_CTL1MASK_FORCE;
	const u32	mask = (1 << USB_PORT_FEAT_CONNECTION)
				| (1 << USB_PORT_FEAT_ENABLE)
				| (1 << USB_PORT_FEAT_LOWSPEED);

	spin_lock_irqsave(&sl811->lock, flags);

	/* stop special signaling */
	sl811->ctrl1 &= ~SL11H_CTL1MASK_FORCE;
	sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
	udelay(3);

	irqstat = sl811_read(sl811, SL11H_IRQ_STATUS);

	switch (signaling) {
	case SL11H_CTL1MASK_SE0:
		DBG("end reset\n");
		sl811->port1 = (1 << USB_PORT_FEAT_C_RESET)
				| (1 << USB_PORT_FEAT_POWER);
		sl811->ctrl1 = 0;
		/* don't wrongly ack RD */
		if (irqstat & SL11H_INTMASK_INSRMV)
			irqstat &= ~SL11H_INTMASK_RD;
		break;
	case SL11H_CTL1MASK_K:
		DBG("end resume\n");
		sl811->port1 &= ~(1 << USB_PORT_FEAT_SUSPEND);
		break;
	default:
		DBG("odd timer signaling: %02x\n", signaling);
		break;
	}
	sl811_write(sl811, SL11H_IRQ_STATUS, irqstat);

	if (irqstat & SL11H_INTMASK_RD) {
		/* usbcore nukes all pending transactions on disconnect */
		if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION))
			sl811->port1 |= (1 << USB_PORT_FEAT_C_CONNECTION)
					| (1 << USB_PORT_FEAT_C_ENABLE);
		sl811->port1 &= ~mask;
		sl811->irq_enable = SL11H_INTMASK_INSRMV;
	} else {
		sl811->port1 |= mask;
		if (irqstat & SL11H_INTMASK_DP)
			sl811->port1 &= ~(1 << USB_PORT_FEAT_LOWSPEED);
		sl811->irq_enable = SL11H_INTMASK_INSRMV | SL11H_INTMASK_RD;
	}

	if (sl811->port1 & (1 << USB_PORT_FEAT_CONNECTION)) {
		u8	ctrl2 = SL811HS_CTL2_INIT;

		sl811->irq_enable |= SL11H_INTMASK_DONE_A;
#ifdef USE_B
		sl811->irq_enable |= SL11H_INTMASK_DONE_B;
#endif
		if (sl811->port1 & (1 << USB_PORT_FEAT_LOWSPEED)) {
			sl811->ctrl1 |= SL11H_CTL1MASK_LSPD;
			ctrl2 |= SL811HS_CTL2MASK_DSWAP;
		}

		/* start SOFs flowing, kickstarting with A registers */
		sl811->ctrl1 |= SL11H_CTL1MASK_SOF_ENA;
		sl811_write(sl811, SL11H_SOFLOWREG, 0xe0);
		sl811_write(sl811, SL811HS_CTLREG2, ctrl2);

		/* autoincrementing */
		sl811_write(sl811, SL811_EP_A(SL11H_BUFLNTHREG), 0);
		writeb(SL_SOF, sl811->data_reg);
		writeb(0, sl811->data_reg);
		sl811_write(sl811, SL811_EP_A(SL11H_HOSTCTLREG),
				SL11H_HCTLMASK_ARM);

		/* khubd provides debounce delay */
	} else {
		sl811->ctrl1 = 0;
	}
	sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);

	/* reenable irqs */
	sl811_write(sl811, SL11H_IRQ_ENABLE, sl811->irq_enable);
	spin_unlock_irqrestore(&sl811->lock, flags);
}

static int
sl811h_hub_control(
	struct usb_hcd	*hcd,
	u16		typeReq,
	u16		wValue,
	u16		wIndex,
	char		*buf,
	u16		wLength
) {
	struct sl811	*sl811 = hcd_to_sl811(hcd);
	int		retval = 0;
	unsigned long	flags;

	spin_lock_irqsave(&sl811->lock, flags);

	switch (typeReq) {
	case ClearHubFeature:
	case SetHubFeature:
		switch (wValue) {
		case C_HUB_OVER_CURRENT:
		case C_HUB_LOCAL_POWER:
			break;
		default:
			goto error;
		}
		break;
	case ClearPortFeature:
		if (wIndex != 1 || wLength != 0)
			goto error;

		switch (wValue) {
		case USB_PORT_FEAT_ENABLE:
			sl811->port1 &= (1 << USB_PORT_FEAT_POWER);
			sl811->ctrl1 = 0;
			sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
			sl811->irq_enable = SL11H_INTMASK_INSRMV;
			sl811_write(sl811, SL11H_IRQ_ENABLE,
						sl811->irq_enable);
			break;
		case USB_PORT_FEAT_SUSPEND:
			if (!(sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND)))
				break;

			/* 20 msec of resume/K signaling, other irqs blocked */
			DBG("start resume...\n");
			sl811->irq_enable = 0;
			sl811_write(sl811, SL11H_IRQ_ENABLE,
						sl811->irq_enable);
			sl811->ctrl1 |= SL11H_CTL1MASK_K;
			sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);

			mod_timer(&sl811->timer, jiffies
					+ msecs_to_jiffies(20));
			break;
		case USB_PORT_FEAT_POWER:
			port_power(sl811, 0);
			break;
		case USB_PORT_FEAT_C_ENABLE:
		case USB_PORT_FEAT_C_SUSPEND:
		case USB_PORT_FEAT_C_CONNECTION:
		case USB_PORT_FEAT_C_OVER_CURRENT:
		case USB_PORT_FEAT_C_RESET:
			break;
		default:
			goto error;
		}
		sl811->port1 &= ~(1 << wValue);
		break;
	case GetHubDescriptor:
		sl811h_hub_descriptor(sl811, (struct usb_hub_descriptor *) buf);
		break;
	case GetHubStatus:
		*(__le32 *) buf = cpu_to_le32(0);
		break;
	case GetPortStatus:
		if (wIndex != 1)
			goto error;
		*(__le32 *) buf = cpu_to_le32(sl811->port1);

#ifndef	VERBOSE
	if (*(u16*)(buf+2))	/* only if wPortChange is interesting */
#endif
		DBG("GetPortStatus %08x\n", sl811->port1);
		break;
	case SetPortFeature:
		if (wIndex != 1 || wLength != 0)
			goto error;
		switch (wValue) {
		case USB_PORT_FEAT_SUSPEND:
			if (sl811->port1 & (1 << USB_PORT_FEAT_RESET))
				goto error;
			if (!(sl811->port1 & (1 << USB_PORT_FEAT_ENABLE)))
				goto error;

			DBG("suspend...\n");
			sl811->ctrl1 &= ~SL11H_CTL1MASK_SOF_ENA;
			sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
			break;
		case USB_PORT_FEAT_POWER:
			port_power(sl811, 1);
			break;
		case USB_PORT_FEAT_RESET:
			if (sl811->port1 & (1 << USB_PORT_FEAT_SUSPEND))
				goto error;
			if (!(sl811->port1 & (1 << USB_PORT_FEAT_POWER)))
				break;

			/* 50 msec of reset/SE0 signaling, irqs blocked */
			sl811->irq_enable = 0;
			sl811_write(sl811, SL11H_IRQ_ENABLE,
						sl811->irq_enable);
			sl811->ctrl1 = SL11H_CTL1MASK_SE0;
			sl811_write(sl811, SL11H_CTLREG1, sl811->ctrl1);
			sl811->port1 |= (1 << USB_PORT_FEAT_RESET);
			mod_timer(&sl811->timer, jiffies
					+ msecs_to_jiffies(50));
			break;
		default:
			goto error;
		}
		sl811->port1 |= 1 << wValue;
		break;

	default:
error:
		/* "protocol stall" on error */
		retval = -EPIPE;
	}

	spin_unlock_irqrestore(&sl811->lock, flags);
	return retval;
}

#ifdef	CONFIG_PM

static int
1335
sl811h_bus_suspend(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
1336 1337
{
	// SOFs off
1338
	DBG("%s\n", __func__);
L
Linus Torvalds 已提交
1339 1340 1341 1342
	return 0;
}

static int
1343
sl811h_bus_resume(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
1344 1345
{
	// SOFs on
1346
	DBG("%s\n", __func__);
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351
	return 0;
}

#else

1352 1353
#define	sl811h_bus_suspend	NULL
#define	sl811h_bus_resume	NULL
L
Linus Torvalds 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496

#endif


/*-------------------------------------------------------------------------*/

#ifdef STUB_DEBUG_FILE

static inline void create_debug_file(struct sl811 *sl811) { }
static inline void remove_debug_file(struct sl811 *sl811) { }

#else

#include <linux/proc_fs.h>
#include <linux/seq_file.h>

static void dump_irq(struct seq_file *s, char *label, u8 mask)
{
	seq_printf(s, "%s %02x%s%s%s%s%s%s\n", label, mask,
		(mask & SL11H_INTMASK_DONE_A) ? " done_a" : "",
		(mask & SL11H_INTMASK_DONE_B) ? " done_b" : "",
		(mask & SL11H_INTMASK_SOFINTR) ? " sof" : "",
		(mask & SL11H_INTMASK_INSRMV) ? " ins/rmv" : "",
		(mask & SL11H_INTMASK_RD) ? " rd" : "",
		(mask & SL11H_INTMASK_DP) ? " dp" : "");
}

static int proc_sl811h_show(struct seq_file *s, void *unused)
{
	struct sl811		*sl811 = s->private;
	struct sl811h_ep	*ep;
	unsigned		i;

	seq_printf(s, "%s\n%s version %s\nportstatus[1] = %08x\n",
		sl811_to_hcd(sl811)->product_desc,
		hcd_name, DRIVER_VERSION,
		sl811->port1);

	seq_printf(s, "insert/remove: %ld\n", sl811->stat_insrmv);
	seq_printf(s, "current session:  done_a %ld done_b %ld "
			"wake %ld sof %ld overrun %ld lost %ld\n\n",
		sl811->stat_a, sl811->stat_b,
		sl811->stat_wake, sl811->stat_sof,
		sl811->stat_overrun, sl811->stat_lost);

	spin_lock_irq(&sl811->lock);

	if (sl811->ctrl1 & SL11H_CTL1MASK_SUSPEND)
		seq_printf(s, "(suspended)\n\n");
	else {
		u8	t = sl811_read(sl811, SL11H_CTLREG1);

		seq_printf(s, "ctrl1 %02x%s%s%s%s\n", t,
			(t & SL11H_CTL1MASK_SOF_ENA) ? " sofgen" : "",
			({char *s; switch (t & SL11H_CTL1MASK_FORCE) {
			case SL11H_CTL1MASK_NORMAL: s = ""; break;
			case SL11H_CTL1MASK_SE0: s = " se0/reset"; break;
			case SL11H_CTL1MASK_K: s = " k/resume"; break;
			default: s = "j"; break;
			}; s; }),
			(t & SL11H_CTL1MASK_LSPD) ? " lowspeed" : "",
			(t & SL11H_CTL1MASK_SUSPEND) ? " suspend" : "");

		dump_irq(s, "irq_enable",
				sl811_read(sl811, SL11H_IRQ_ENABLE));
		dump_irq(s, "irq_status",
				sl811_read(sl811, SL11H_IRQ_STATUS));
		seq_printf(s, "frame clocks remaining:  %d\n",
				sl811_read(sl811, SL11H_SOFTMRREG) << 6);
	}

	seq_printf(s, "A: qh%p ctl %02x sts %02x\n", sl811->active_a,
		sl811_read(sl811, SL811_EP_A(SL11H_HOSTCTLREG)),
		sl811_read(sl811, SL811_EP_A(SL11H_PKTSTATREG)));
	seq_printf(s, "B: qh%p ctl %02x sts %02x\n", sl811->active_b,
		sl811_read(sl811, SL811_EP_B(SL11H_HOSTCTLREG)),
		sl811_read(sl811, SL811_EP_B(SL11H_PKTSTATREG)));
	seq_printf(s, "\n");
	list_for_each_entry (ep, &sl811->async, schedule) {
		struct urb		*urb;

		seq_printf(s, "%s%sqh%p, ep%d%s, maxpacket %d"
					" nak %d err %d\n",
			(ep == sl811->active_a) ? "(A) " : "",
			(ep == sl811->active_b) ? "(B) " : "",
			ep, ep->epnum,
			({ char *s; switch (ep->nextpid) {
			case USB_PID_IN: s = "in"; break;
			case USB_PID_OUT: s = "out"; break;
			case USB_PID_SETUP: s = "setup"; break;
			case USB_PID_ACK: s = "status"; break;
			default: s = "?"; break;
			}; s;}),
			ep->maxpacket,
			ep->nak_count, ep->error_count);
		list_for_each_entry (urb, &ep->hep->urb_list, urb_list) {
			seq_printf(s, "  urb%p, %d/%d\n", urb,
				urb->actual_length,
				urb->transfer_buffer_length);
		}
	}
	if (!list_empty(&sl811->async))
		seq_printf(s, "\n");

	seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);

	for (i = 0; i < PERIODIC_SIZE; i++) {
		ep = sl811->periodic[i];
		if (!ep)
			continue;
		seq_printf(s, "%2d [%3d]:\n", i, sl811->load[i]);

		/* DUMB: prints shared entries multiple times */
		do {
			seq_printf(s,
				"   %s%sqh%d/%p (%sdev%d ep%d%s max %d) "
					"err %d\n",
				(ep == sl811->active_a) ? "(A) " : "",
				(ep == sl811->active_b) ? "(B) " : "",
				ep->period, ep,
				(ep->udev->speed == USB_SPEED_FULL)
					? "" : "ls ",
				ep->udev->devnum, ep->epnum,
				(ep->epnum == 0) ? ""
					: ((ep->nextpid == USB_PID_IN)
						? "in"
						: "out"),
				ep->maxpacket, ep->error_count);
			ep = ep->next;
		} while (ep);
	}

	spin_unlock_irq(&sl811->lock);
	seq_printf(s, "\n");

	return 0;
}

static int proc_sl811h_open(struct inode *inode, struct file *file)
{
	return single_open(file, proc_sl811h_show, PDE(inode)->data);
}

1497
static const struct file_operations proc_ops = {
L
Linus Torvalds 已提交
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
	.open		= proc_sl811h_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

/* expect just one sl811 per system */
static const char proc_filename[] = "driver/sl811h";

static void create_debug_file(struct sl811 *sl811)
{
1509
	sl811->pde = proc_create_data(proc_filename, 0, NULL, &proc_ops, sl811);
L
Linus Torvalds 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
}

static void remove_debug_file(struct sl811 *sl811)
{
	if (sl811->pde)
		remove_proc_entry(proc_filename, NULL);
}

#endif

/*-------------------------------------------------------------------------*/

static void
sl811h_stop(struct usb_hcd *hcd)
{
	struct sl811	*sl811 = hcd_to_sl811(hcd);
	unsigned long	flags;

	del_timer_sync(&hcd->rh_timer);

	spin_lock_irqsave(&sl811->lock, flags);
	port_power(sl811, 0);
	spin_unlock_irqrestore(&sl811->lock, flags);
}

static int
sl811h_start(struct usb_hcd *hcd)
{
	struct sl811		*sl811 = hcd_to_sl811(hcd);

	/* chip has been reset, VBUS power is off */
	hcd->state = HC_STATE_RUNNING;

1543
	if (sl811->board) {
1544 1545 1546
		if (!device_can_wakeup(hcd->self.controller))
			device_init_wakeup(hcd->self.controller,
				sl811->board->can_wakeup);
1547 1548
		hcd->power_budget = sl811->board->power * 2;
	}
L
Linus Torvalds 已提交
1549

1550
	/* enable power and interrupts */
D
David Brownell 已提交
1551 1552
	port_power(sl811, 1);

L
Linus Torvalds 已提交
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
	return 0;
}

/*-------------------------------------------------------------------------*/

static struct hc_driver sl811h_hc_driver = {
	.description =		hcd_name,
	.hcd_priv_size =	sizeof(struct sl811),

	/*
	 * generic hardware linkage
	 */
	.irq =			sl811h_irq,
	.flags =		HCD_USB11 | HCD_MEMORY,

	/* Basic lifecycle operations */
	.start =		sl811h_start,
	.stop =			sl811h_stop,

	/*
	 * managing i/o requests and associated device resources
	 */
	.urb_enqueue =		sl811h_urb_enqueue,
	.urb_dequeue =		sl811h_urb_dequeue,
	.endpoint_disable =	sl811h_endpoint_disable,

	/*
	 * periodic schedule support
	 */
	.get_frame_number =	sl811h_get_frame,

	/*
	 * root hub support
	 */
	.hub_status_data =	sl811h_hub_status_data,
	.hub_control =		sl811h_hub_control,
1589 1590
	.bus_suspend =		sl811h_bus_suspend,
	.bus_resume =		sl811h_bus_resume,
L
Linus Torvalds 已提交
1591 1592 1593 1594
};

/*-------------------------------------------------------------------------*/

D
David Brownell 已提交
1595
static int __devexit
1596
sl811h_remove(struct platform_device *dev)
L
Linus Torvalds 已提交
1597
{
1598
	struct usb_hcd		*hcd = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
1599 1600 1601 1602 1603 1604
	struct sl811		*sl811 = hcd_to_sl811(hcd);
	struct resource		*res;

	remove_debug_file(sl811);
	usb_remove_hcd(hcd);

D
David Brownell 已提交
1605
	/* some platforms may use IORESOURCE_IO */
1606
	res = platform_get_resource(dev, IORESOURCE_MEM, 1);
D
David Brownell 已提交
1607 1608
	if (res)
		iounmap(sl811->data_reg);
L
Linus Torvalds 已提交
1609

1610
	res = platform_get_resource(dev, IORESOURCE_MEM, 0);
D
David Brownell 已提交
1611 1612
	if (res)
		iounmap(sl811->addr_reg);
L
Linus Torvalds 已提交
1613 1614 1615 1616 1617

	usb_put_hcd(hcd);
	return 0;
}

D
David Brownell 已提交
1618
static int __devinit
1619
sl811h_probe(struct platform_device *dev)
L
Linus Torvalds 已提交
1620 1621 1622
{
	struct usb_hcd		*hcd;
	struct sl811		*sl811;
1623
	struct resource		*addr, *data, *ires;
L
Linus Torvalds 已提交
1624 1625 1626 1627
	int			irq;
	void __iomem		*addr_reg;
	void __iomem		*data_reg;
	int			retval;
D
David Brownell 已提交
1628
	u8			tmp, ioaddr = 0;
1629
	unsigned long		irqflags;
L
Linus Torvalds 已提交
1630 1631 1632 1633 1634 1635

	/* basic sanity checks first.  board-specific init logic should
	 * have initialized these three resources and probably board
	 * specific platform_data.  we don't probe for IRQs, and do only
	 * minimal sanity checking.
	 */
1636 1637
	ires = platform_get_resource(dev, IORESOURCE_IRQ, 0);
	if (dev->num_resources < 3 || !ires)
L
Linus Torvalds 已提交
1638 1639
		return -ENODEV;

1640 1641 1642
	irq = ires->start;
	irqflags = ires->flags & IRQF_TRIGGER_MASK;

L
Linus Torvalds 已提交
1643
	/* refuse to confuse usbcore */
1644
	if (dev->dev.dma_mask) {
L
Linus Torvalds 已提交
1645 1646 1647 1648
		DBG("no we won't dma\n");
		return -EINVAL;
	}

D
David Brownell 已提交
1649
	/* the chip may be wired for either kind of addressing */
1650 1651
	addr = platform_get_resource(dev, IORESOURCE_MEM, 0);
	data = platform_get_resource(dev, IORESOURCE_MEM, 1);
D
David Brownell 已提交
1652 1653
	retval = -EBUSY;
	if (!addr || !data) {
1654 1655
		addr = platform_get_resource(dev, IORESOURCE_IO, 0);
		data = platform_get_resource(dev, IORESOURCE_IO, 1);
D
David Brownell 已提交
1656 1657 1658
		if (!addr || !data)
			return -ENODEV;
		ioaddr = 1;
1659 1660 1661 1662 1663 1664 1665
		/*
		 * NOTE: 64-bit resource->start is getting truncated
		 * to avoid compiler warning, assuming that ->start
		 * is always 32-bit for this case
		 */
		addr_reg = (void __iomem *) (unsigned long) addr->start;
		data_reg = (void __iomem *) (unsigned long) data->start;
D
David Brownell 已提交
1666 1667 1668 1669 1670 1671
	} else {
		addr_reg = ioremap(addr->start, 1);
		if (addr_reg == NULL) {
			retval = -ENOMEM;
			goto err2;
		}
L
Linus Torvalds 已提交
1672

D
David Brownell 已提交
1673 1674 1675 1676 1677
		data_reg = ioremap(data->start, 1);
		if (data_reg == NULL) {
			retval = -ENOMEM;
			goto err4;
		}
L
Linus Torvalds 已提交
1678 1679 1680
	}

	/* allocate and initialize hcd */
1681
	hcd = usb_create_hcd(&sl811h_hc_driver, &dev->dev, dev_name(&dev->dev));
L
Linus Torvalds 已提交
1682 1683 1684 1685 1686 1687 1688 1689 1690
	if (!hcd) {
		retval = -ENOMEM;
		goto err5;
	}
	hcd->rsrc_start = addr->start;
	sl811 = hcd_to_sl811(hcd);

	spin_lock_init(&sl811->lock);
	INIT_LIST_HEAD(&sl811->async);
1691
	sl811->board = dev->dev.platform_data;
L
Linus Torvalds 已提交
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
	init_timer(&sl811->timer);
	sl811->timer.function = sl811h_timer;
	sl811->timer.data = (unsigned long) sl811;
	sl811->addr_reg = addr_reg;
	sl811->data_reg = data_reg;

	spin_lock_irq(&sl811->lock);
	port_power(sl811, 0);
	spin_unlock_irq(&sl811->lock);
	msleep(200);

	tmp = sl811_read(sl811, SL11H_HWREVREG);
	switch (tmp >> 4) {
	case 1:
		hcd->product_desc = "SL811HS v1.2";
		break;
	case 2:
		hcd->product_desc = "SL811HS v1.5";
		break;
	default:
		/* reject case 0, SL11S is less functional */
		DBG("chiprev %02x\n", tmp);
		retval = -ENXIO;
		goto err6;
	}

D
David Brownell 已提交
1718 1719 1720 1721 1722 1723
	/* The chip's IRQ is level triggered, active high.  A requirement
	 * for platform device setup is to cope with things like signal
	 * inverters (e.g. CF is active low) or working only with edge
	 * triggers (e.g. most ARM CPUs).  Initial driver stress testing
	 * was on a system with single edge triggering, so most sorts of
	 * triggering arrangement should work.
1724 1725
	 *
	 * Use resource IRQ flags if set by platform device setup.
D
David Brownell 已提交
1726
	 */
1727 1728
	irqflags |= IRQF_SHARED;
	retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | irqflags);
L
Linus Torvalds 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737
	if (retval != 0)
		goto err6;

	create_debug_file(sl811);
	return retval;

 err6:
	usb_put_hcd(hcd);
 err5:
D
David Brownell 已提交
1738 1739
	if (!ioaddr)
		iounmap(data_reg);
L
Linus Torvalds 已提交
1740
 err4:
D
David Brownell 已提交
1741 1742
	if (!ioaddr)
		iounmap(addr_reg);
L
Linus Torvalds 已提交
1743 1744 1745 1746 1747 1748 1749 1750
 err2:
	DBG("init error, %d\n", retval);
	return retval;
}

#ifdef	CONFIG_PM

/* for this device there's no useful distinction between the controller
D
David Brownell 已提交
1751
 * and its root hub, except that the root hub only gets direct PM calls
L
Linus Torvalds 已提交
1752 1753 1754 1755
 * when CONFIG_USB_SUSPEND is enabled.
 */

static int
1756
sl811h_suspend(struct platform_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
1757
{
1758
	struct usb_hcd	*hcd = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
1759 1760 1761
	struct sl811	*sl811 = hcd_to_sl811(hcd);
	int		retval = 0;

1762 1763
	switch (state.event) {
	case PM_EVENT_FREEZE:
1764
		retval = sl811h_bus_suspend(hcd);
1765 1766
		break;
	case PM_EVENT_SUSPEND:
1767
	case PM_EVENT_HIBERNATE:
1768
	case PM_EVENT_PRETHAW:		/* explicitly discard hw state */
L
Linus Torvalds 已提交
1769
		port_power(sl811, 0);
1770 1771
		break;
	}
L
Linus Torvalds 已提交
1772 1773 1774 1775
	return retval;
}

static int
1776
sl811h_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
1777
{
1778
	struct usb_hcd	*hcd = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
1779 1780 1781 1782 1783
	struct sl811	*sl811 = hcd_to_sl811(hcd);

	/* with no "check to see if VBUS is still powered" board hook,
	 * let's assume it'd only be powered to enable remote wakeup.
	 */
A
Alan Stern 已提交
1784
	if (!sl811->port1 || !device_can_wakeup(&hcd->self.root_hub->dev)) {
L
Linus Torvalds 已提交
1785 1786
		sl811->port1 = 0;
		port_power(sl811, 1);
1787
		usb_root_hub_lost_power(hcd->self.root_hub);
L
Linus Torvalds 已提交
1788 1789 1790
		return 0;
	}

1791
	return sl811h_bus_resume(hcd);
L
Linus Torvalds 已提交
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
}

#else

#define	sl811h_suspend	NULL
#define	sl811h_resume	NULL

#endif


D
David Brownell 已提交
1802
/* this driver is exported so sl811_cs can depend on it */
1803
struct platform_driver sl811h_driver = {
L
Linus Torvalds 已提交
1804
	.probe =	sl811h_probe,
D
David Brownell 已提交
1805
	.remove =	__devexit_p(sl811h_remove),
L
Linus Torvalds 已提交
1806 1807 1808

	.suspend =	sl811h_suspend,
	.resume =	sl811h_resume,
1809 1810 1811 1812
	.driver = {
		.name =	(char *) hcd_name,
		.owner = THIS_MODULE,
	},
L
Linus Torvalds 已提交
1813
};
D
David Brownell 已提交
1814
EXPORT_SYMBOL(sl811h_driver);
L
Linus Torvalds 已提交
1815 1816

/*-------------------------------------------------------------------------*/
D
David Brownell 已提交
1817 1818

static int __init sl811h_init(void)
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823
{
	if (usb_disabled())
		return -ENODEV;

	INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
1824
	return platform_driver_register(&sl811h_driver);
L
Linus Torvalds 已提交
1825 1826 1827
}
module_init(sl811h_init);

D
David Brownell 已提交
1828 1829
static void __exit sl811h_cleanup(void)
{
1830
	platform_driver_unregister(&sl811h_driver);
L
Linus Torvalds 已提交
1831 1832
}
module_exit(sl811h_cleanup);